i386: KVM: Fix 'system_reset' failures when vCPU is in VMX root operation
cover i386: KVM: Fix 'system_reset' failures when vCPU is in VMX root operation
Changes since RFC:
- Call kvm_put_msr_feature_control() before kvm_put_sregs2() to achieve the same result [Paolo].
- Add Maxim's R-b to PATCH1.
It was discovered that Windows 11 with WSL2 (Hyper-V) enabled guests fail to reboot when QEMU's 'system_reset' command is issued. The problem appears to be that KVM_SET_SREGS2 fails because zeroed CR4 register value doesn't pass vmx_is_valid_cr4() check in KVM as certain bits can't be zero while in VMX root operation (post-VMXON). kvm_arch_put_registers() does call kvm_put_nested_state() which is supposed to kick vCPU out of VMX root operation, however, it only does so after kvm_put_sregs2() and there's a good reason for that: 'real' nested state requires e.g. EFER.SVME to be set.
The root cause of the issue seems to be that QEMU is doing quite a lot to forcefully reset a vCPU as KVM doesn't export kvm_vcpu_reset() (or, rather, it's super-set) yet. While all the numerous existing APIs for setting a vCPU state work fine for a newly created vCPU, using them for vCPU reset is a mess caused by various dependencies between different components of the state (VMX, SMM, MSRs, XCRs, CPUIDs, ...). It would've been possible to allow to set 'inconsistent' state and only validate it upon VCPU_RUN from the very beginning but that ship has long sailed for KVM. A new, dedicated API for vCPU reset is likely the way to go.
Resolve the immediate issue by setting MSR_IA32_FEATURE_CONTROL before kvm_put_sregs2() (and kvm_put_nested_state()), this ensures vCPU gets kicked out of VMX root operation.
Vitaly Kuznetsov (2):
i386: reset KVM nested state upon CPU reset
i386: do kvm_put_msr_feature_control() first thing when vCPU is reset
target/i386/kvm/kvm.c | 54 +++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 15 deletions(-)
Signature · 1 line
2.37.1
PATCH 1/2 i386: reset KVM nested state upon CPU reset
Make sure env->nested_state is cleaned up when a vCPU is reset, it may be stale after an incoming migration, kvm_arch_put_registers() may end up failing or putting vCPU in a weird state.
--- target/i386/kvm/kvm.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-)
target/i386/kvm/kvm.c +27 -10
@@ -1695,6 +1695,30 @@ static void kvm_init_xsave(CPUX86State *env) env->xsave_buf_len); } +static void kvm_init_nested_state (CPUX86State *env) +{ +struct kvm_vmx_nested_state_hdr *vmx_hdr; +uint32_t size; + +if (!env->nested_state) { +return ; + } + + size = env->nested_state->size; + +memset (env->nested_state,0 , size); + env->nested_state->size = size; + +if (cpu_has_vmx (env)) { + env->nested_state->format =KVM_STATE_NESTED_FORMAT_VMX ; + vmx_hdr = &env->nested_state->hdr.vmx; + vmx_hdr->vmxon_pa =-1ull ; + vmx_hdr->vmcs12_pa =-1ull ; + }else if (cpu_has_svm (env)) { + env->nested_state->format =KVM_STATE_NESTED_FORMAT_SVM ; + } +} +int kvm_arch_init_vcpu (CPUState *cs) {struct {
@@ -2122,19 +2146,10 @@ int kvm_arch_init_vcpu(CPUState *cs)assert (max_nested_state_len >= offsetof(struct kvm_nested_state , data));if (cpu_has_vmx (env) ||cpu_has_svm (env)) { -struct kvm_vmx_nested_state_hdr *vmx_hdr; - env->nested_state =g_malloc0 (max_nested_state_len); env->nested_state->size = max_nested_state_len; -if (cpu_has_vmx (env)) { - env->nested_state->format =KVM_STATE_NESTED_FORMAT_VMX ; - vmx_hdr = &env->nested_state->hdr.vmx; - vmx_hdr->vmxon_pa =-1ull ; - vmx_hdr->vmcs12_pa =-1ull ; - }else { - env->nested_state->format =KVM_STATE_NESTED_FORMAT_SVM ; - } +kvm_init_nested_state (env); } }
@@ -2199,6 +2214,8 @@ void kvm_arch_reset_vcpu(X86CPU *cpu)/* enabled by default */ env->poll_control_msr =1 ; +kvm_init_nested_state (env); +sev_es_set_reset_vector (CPU (cpu)); }
Signature · 1 line
2.37.1
PATCH 2/2 i386: do kvm_put_msr_feature_control() first thing when vCPU is reset
In reply to Vitaly Kuznetsovkvm_put_sregs2() fails to reset 'locked' CR4/CR0 bits upon vCPU reset when it is in VMX root operation. Do kvm_put_msr_feature_control() before kvm_put_sregs2() to (possibly) kick vCPU out of VMX root operation. It also seems logical to do kvm_put_msr_feature_control() before kvm_put_nested_state() and not after it, especially when 'real' nested state is set.
--- target/i386/kvm/kvm.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
target/i386/kvm/kvm.c +12 -5
@@ -4529,6 +4529,18 @@ int kvm_arch_put_registers(CPUState *cpu, int level)assert (cpu_is_stopped (cpu) ||qemu_cpu_is_self (cpu)); +/* +* Put MSR_IA32_FEATURE_CONTROL first, this ensures the VM gets out of VMX +* root operation upon vCPU reset. kvm_put_msr_feature_control() should also +* preceed kvm_put_nested_state() when 'real' nested state is set. +*/ +if (level >=KVM_PUT_RESET_STATE ) { + ret =kvm_put_msr_feature_control (x86_cpu); +if (ret <0 ) { +return ret; + } + } +/* must be before kvm_put_nested_state so that EFER.SVME is set */ ret = has_sregs2 ?kvm_put_sregs2 (x86_cpu) :kvm_put_sregs (x86_cpu);if (ret <0 ) {
@@ -4540,11 +4552,6 @@ int kvm_arch_put_registers(CPUState *cpu, int level)if (ret <0 ) {return ret; } - - ret =kvm_put_msr_feature_control (x86_cpu); -if (ret <0 ) { -return ret; - } }if (level ==KVM_PUT_FULL_STATE ) {
Signature · 1 line
2.37.1
Paolo Bonzini
AppliedQuoted 25 lines · Vitaly Kuznetsov
Changes since RFC:
- Callkvm_put_msr_feature_control()beforekvm_put_sregs2()to achieve the same result [Paolo].
- Add Maxim's R-b to PATCH1.It was discovered that Windows 11 with WSL2 (Hyper-V) enabled guests fail to reboot when QEMU's '
system_reset' command is issued. The problem appears to be thatKVM_SET_SREGS2fails because zeroed CR4 register value doesn't passvmx_is_valid_cr4()check in KVM as certain bits can't be zero while in VMX root operation (post-VMXON).kvm_arch_put_registers()does callkvm_put_nested_state()which is supposed to kick vCPU out of VMX root operation, however, it only does so afterkvm_put_sregs2()and there's a good reason for that: 'real' nested state requires e.g. EFER.SVME to be set.The root cause of the issue seems to be that QEMU is doing quite a lot to forcefully reset a vCPU as KVM doesn't export
kvm_vcpu_reset()(or, rather, it's super-set) yet. While all the numerous existing APIs for setting a vCPU state work fine for a newly created vCPU, using them for vCPU reset is a mess caused by various dependencies between different components of the state (VMX, SMM, MSRs, XCRs, CPUIDs, ...). It would've been possible to allow to set 'inconsistent' state and only validate it uponVCPU_RUNfrom the very beginning but that ship has long sailed for KVM. A new, dedicated API for vCPU reset is likely the way to go.
Quoted 3 lines
Resolve the immediate issue by setting
MSR_IA32_FEATURE_CONTROLbeforekvm_put_sregs2()(andkvm_put_nested_state()), this ensures vCPU gets kicked out of VMX root operation.
Queued, thanks.
Paolo