The AMD APM previously stated that after an intercepted shutdown, the entire VMCB state is undefined. In practice, only the save area is undefined and most of the control area remains valid. Revision 3.45 of the APM now makes this explicit: "After an intercepted shutdown, the VMCB control area is valid (with the exception of offsets 60h, 61h, and 68h) and the VMCB state save area is undefined." KVM zeroes the entire VMCB before INITing the vCPU based on the old wording, discarding control area state that hardware preserves. Clear only the save area and the three undefined control area fields (int_ctl[15:0] and int_state) in line with the updated APM. Also explicitly clear EVENTINJ. Hardware clears it on #VMEXIT, but only if VMRUN fully entered the guest. Clearing it here should be unnecessary, since a shutdown can only happen if the guest actually ran. Do so anyway, as there is no reason to carry a stale event across the vCPU reset, and the cost is negligible. Signed-off-by: Shivansh Dhiman --- Changes in v2: * Explicitly clear EVENTINJ and EVENTINJ_ERR (Sean Christopherson). * Rebased on kvm-x86/next. v1: https://lore.kernel.org/all/20260824131824.6040-1-shivansh.dhiman@amd.com --- arch/x86/kvm/svm/svm.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 136069dfcd44..09ca864e5a38 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2213,18 +2213,31 @@ static int shutdown_interception(struct kvm_vcpu *vcpu) /* - * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put - * the VMCB in a known good state. Unfortuately, KVM doesn't have - * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking - * userspace. At a platform view, INIT is acceptable behavior as - * there exist bare metal platforms that automatically INIT the CPU - * in response to shutdown. + * The VMCB save area is undefined after a SHUTDOWN intercept, as are + * control area offsets 60h, 61h, and 68h (int_ctl[15:0] and int_state). + * INIT the vCPU to put the VMCB in a known good state. Unfortunately, + * KVM doesn't have KVM_MP_STATE_SHUTDOWN and can't add it without + * potentially breaking userspace. At a platform view, INIT is + * acceptable behavior as there exist bare metal platforms that + * automatically INIT the CPU in response to shutdown. * * The VM save area for SEV-ES guests has already been encrypted so it * cannot be reinitialized, i.e. synthesizing INIT is futile. */ if (!is_sev_es_guest(vcpu)) { - clear_page(svm->vmcb); + struct vmcb_control_area *control = &svm->vmcb->control; + + memset(&svm->vmcb->save, 0, sizeof(svm->vmcb->save)); + control->int_ctl &= ~GENMASK(15, 0); + control->int_state = 0; + + /* + * EVENTINJ is cleared on #VMEXIT, but only if VMRUN fully + * entered the guest. Manually clear the fields even though it + * should be redundant, as there is no downside to doing so. + */ + control->event_inj = 0; + control->event_inj_err = 0; #ifdef CONFIG_KVM_SMM if (is_smm(vcpu)) kvm_smm_changed(vcpu, false); base-commit: 6bd2905303c58679e303941b7d8ae8c074cd95ce -- 2.43.0