KVM: nested virt SMM fixes
v5 cover KVM: nested virt SMM fixes
Windows Server 2016 with Hyper-V enabled fails to boot on OVMF with SMM (OVMF_CODE-need-smm.fd). Turns out that the SMM emulation code in KVM does not handle nested virtualization very well, leading to a whole bunch of issues.
For example, Hyper-V uses descriptor table exiting (SECONDARY_EXEC_DESC) so when the SMM handler tries to switch from real mode a VM exit occurs and is forwarded to a clueless L1.
This series fixes it by switching the vcpu to !guest_mode, i.e. to the L1 state, before entering SMM and then switching back to L2 as part of emulating the RSM instruction.
Patches 1 and 2 are common for both Intel and AMD, patches 3-4 fix Intel, and patches 5-6 AMD.
v1->v2:
* Moved left_smm detection to emulator_set_hflags (couldn't quite get rid of the field despite my original claim) (Paolo)
* Moved the kvm_x86_ops->post_leave_smm() call a few statements down so it really runs after all state has been synced.
* Added the smi_allowed callback (new patch 2) to avoid running into WARN_ON_ONCE(vmx->nested.nested_run_pending) on Intel.
v2->v3:
* Ommitted patch 4 ("KVM: nVMX: save nested EPT information in SMRAM state save map") and replaced it with ("treat CR4.VMXE as reserved in SMM") (Paolo)
* Implemented smi_allowed on AMD to support SMI interception. Turns out Windows needs this when running on >1 vCPU.
* Eliminated internal SMM state on AMD and switched to using the SMM state save area in guest memory instead (Paolo)
v3->v4:
* Changed the order of operations in enter_smm(), now saving the original (and potentially L2) state into the SMM state save area.
* Made em_rsm() reload the SMM state save area if post_leave_smm() entered guest mode. This way, SMM handlers see and may change the actual state of the vCPU at the point where SMI was injected (Radim)
* In patch 4, switched to a different way of avoiding the problem of hitting the very check the patch is adding.
v4->v5:
* Removed patch 4 (CR4.VMXE protection in SMM, will be done separately), patch 3 bacame 4 and new patch 3 fixes a bug in load_vmcs12_host_state() which prevented SMM exit to L2 from working without first restoring the state from the SMM state save area.
* Eliminated the first restore from SMM state save area (Paolo)
* Tweaked the HF_SMM_MASK flag manipulation (Paolo)
Ladi Prosek (6):
KVM: x86: introduce ISA specific SMM entry/exit callbacks
KVM: x86: introduce ISA specific smi_allowed callback
KVM: nVMX: set IDTR and GDTR limits when loading L1 host state
KVM: nVMX: fix SMI injection in guest mode
KVM: nSVM: refactor nested_svm_vmrun
KVM: nSVM: fix SMI injection in guest mode
arch/x86/include/asm/kvm_emulate.h | 2 + arch/x86/include/asm/kvm_host.h | 7 ++ arch/x86/kvm/emulate.c | 9 ++ arch/x86/kvm/svm.c | 205 +++++++++++++++++++++++++------------ arch/x86/kvm/vmx.c | 79 ++++++++++++-- arch/x86/kvm/x86.c | 20 +++- 6 files changed, 245 insertions(+), 77 deletions(-)
PATCH v5 1/6 KVM: x86: introduce ISA specific SMM entry/exit callbacks
Entering and exiting SMM may require ISA specific handling under certain circumstances. This commit adds two new callbacks with empty implementations. Actual functionality will be added in following commits.
* pre_enter_smm() is to be called when injecting an SMM, before any SMM related vcpu state has been changed
* pre_leave_smm() is to be called when emulating the RSM instruction, when the vcpu is in real mode and before any SMM related vcpu state has been restored
--- arch/x86/include/asm/kvm_emulate.h | 2 ++ arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/emulate.c | 9 +++++++++ arch/x86/kvm/svm.c | 15 +++++++++++++++ arch/x86/kvm/vmx.c | 15 +++++++++++++++ arch/x86/kvm/x86.c | 15 ++++++++++++++- 6 files changed, 58 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_emulate.h +2 -0
@@ -225,6 +225,8 @@ struct x86_emulate_ops {unsigned (*get_hflags)(struct x86_emulate_ctxt *ctxt);void (*set_hflags)(struct x86_emulate_ctxt *ctxt,unsigned hflags); +int (*pre_leave_smm)(struct x86_emulate_ctxt *ctxt,u64 smbase); + }; typedef u32 __attribute__((vector_size(16))) sse128_t;
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1061,6 +1061,9 @@ struct kvm_x86_ops {void (*cancel_hv_timer)(struct kvm_vcpu *vcpu);void (*setup_mce)(struct kvm_vcpu *vcpu); + +int (*pre_enter_smm)(struct kvm_vcpu *vcpu,char *smstate); +int (*pre_leave_smm)(struct kvm_vcpu *vcpu,u64 smbase); };struct kvm_arch_async_pf {
arch/x86/kvm/emulate.c +9 -0
@@ -2591,6 +2591,15 @@ static int em_rsm(struct x86_emulate_ctxt *ctxt) ctxt->ops->set_msr (ctxt,MSR_EFER , efer); smbase = ctxt->ops->get_smbase (ctxt); + +/* +* Give pre_leave_smm() a chance to make ISA-specific changes to the +* vCPU state (e.g. enter guest mode) before loading state from the SMM +* state-save area. +*/ +if (ctxt->ops->pre_leave_smm (ctxt, smbase)) +return X86EMUL_UNHANDLEABLE ; +if (emulator_has_longmode (ctxt)) ret =rsm_load_state_64 (ctxt, smbase +0x8000 );else
arch/x86/kvm/svm.c +15 -0
@@ -5393,6 +5393,18 @@ static void svm_setup_mce(struct kvm_vcpu *vcpu) vcpu->arch.mcg_cap &=0x1ff ; } +static int svm_pre_enter_smm (struct kvm_vcpu *vcpu,char *smstate) +{ +/* TODO: Implement */ +return 0 ; +} + +static int svm_pre_leave_smm (struct kvm_vcpu *vcpu,u64 smbase) +{ +/* TODO: Implement */ +return 0 ; +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -5503,6 +5515,9 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .deliver_posted_interrupt = svm_deliver_avic_intr, .update_pi_irte = svm_update_pi_irte, .setup_mce = svm_setup_mce, + + .pre_enter_smm = svm_pre_enter_smm, + .pre_leave_smm = svm_pre_leave_smm, };static int __init svm_init (void )
arch/x86/kvm/vmx.c +15 -0
@@ -11941,6 +11941,18 @@ static void vmx_setup_mce(struct kvm_vcpu *vcpu) ~FEATURE_CONTROL_LMCE ; } +static int vmx_pre_enter_smm (struct kvm_vcpu *vcpu,char *smstate) +{ +/* TODO: Implement */ +return 0 ; +} + +static int vmx_pre_leave_smm (struct kvm_vcpu *vcpu,u64 smbase) +{ +/* TODO: Implement */ +return 0 ; +} +static struct kvm_x86_ops vmx_x86_ops__ro_after_init = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios,
@@ -12066,6 +12078,9 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {#endif .setup_mce = vmx_setup_mce, + + .pre_enter_smm = vmx_pre_enter_smm, + .pre_leave_smm = vmx_pre_leave_smm, };static int __init vmx_init (void )
arch/x86/kvm/x86.c +14 -1
@@ -5275,6 +5275,11 @@ static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flakvm_set_hflags (emul_to_vcpu (ctxt), emul_flags); } +static int emulator_pre_leave_smm (struct x86_emulate_ctxt *ctxt,u64 smbase) +{ +return kvm_x86_ops->pre_leave_smm (emul_to_vcpu (ctxt), smbase); +} +static const struct x86_emulate_ops emulate_ops = { .read_gpr = emulator_read_gpr, .write_gpr = emulator_write_gpr,
@@ -5316,6 +5321,7 @@ static const struct x86_emulate_ops emulate_ops = { .set_nmi_mask = emulator_set_nmi_mask, .get_hflags = emulator_get_hflags, .set_hflags = emulator_set_hflags, + .pre_leave_smm = emulator_pre_leave_smm, };static void toggle_interruptibility (struct kvm_vcpu *vcpu,u32 mask)
@@ -6641,13 +6647,20 @@ static void enter_smm(struct kvm_vcpu *vcpu)u32 cr0;trace_kvm_enter_smm (vcpu->vcpu_id, vcpu->arch.smbase, true); - vcpu->arch.hflags |=HF_SMM_MASK ;memset (buf,0 ,512 );if (guest_cpuid_has (vcpu,X86_FEATURE_LM ))enter_smm_save_state_64 (vcpu, buf);else enter_smm_save_state_32 (vcpu, buf); +/* +* Give pre_enter_smm() a chance to make ISA-specific changes to the +* vCPU state (e.g. leave guest mode) after we've saved the state into +* the SMM state-save area. +*/ + kvm_x86_ops->pre_enter_smm (vcpu, buf); + + vcpu->arch.hflags |=HF_SMM_MASK ;kvm_vcpu_write_guest (vcpu, vcpu->arch.smbase +0xfe00 , buf,sizeof (buf));if (kvm_x86_ops->get_nmi_mask (vcpu))
Signature · 1 line
2.13.5
PATCH v5 2/6 KVM: x86: introduce ISA specific smi_allowed callback
In reply to Ladi ProsekSimilar to NMI, there may be ISA specific reasons why an SMI cannot be injected into the guest. This commit adds a new smi_allowed callback to be implemented in following commits.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 6 ++++++ arch/x86/kvm/vmx.c | 6 ++++++ arch/x86/kvm/x86.c | 2 +- 4 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1062,6 +1062,7 @@ struct kvm_x86_ops {void (*setup_mce)(struct kvm_vcpu *vcpu); +int (*smi_allowed)(struct kvm_vcpu *vcpu);int (*pre_enter_smm)(struct kvm_vcpu *vcpu,char *smstate);int (*pre_leave_smm)(struct kvm_vcpu *vcpu,u64 smbase); };
arch/x86/kvm/svm.c +6 -0
@@ -5393,6 +5393,11 @@ static void svm_setup_mce(struct kvm_vcpu *vcpu) vcpu->arch.mcg_cap &=0x1ff ; } +static int svm_smi_allowed (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +static int svm_pre_enter_smm (struct kvm_vcpu *vcpu,char *smstate) {/* TODO: Implement */
@@ -5516,6 +5521,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .update_pi_irte = svm_update_pi_irte, .setup_mce = svm_setup_mce, + .smi_allowed = svm_smi_allowed, .pre_enter_smm = svm_pre_enter_smm, .pre_leave_smm = svm_pre_leave_smm, };
arch/x86/kvm/vmx.c +6 -0
@@ -11941,6 +11941,11 @@ static void vmx_setup_mce(struct kvm_vcpu *vcpu) ~FEATURE_CONTROL_LMCE ; } +static int vmx_smi_allowed (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +static int vmx_pre_enter_smm (struct kvm_vcpu *vcpu,char *smstate) {/* TODO: Implement */
@@ -12079,6 +12084,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .setup_mce = vmx_setup_mce, + .smi_allowed = vmx_smi_allowed, .pre_enter_smm = vmx_pre_enter_smm, .pre_leave_smm = vmx_pre_leave_smm, };
arch/x86/kvm/x86.c +1 -1
@@ -6432,7 +6432,7 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win) } kvm_x86_ops->queue_exception (vcpu); - }else if (vcpu->arch.smi_pending && !is_smm (vcpu)) { + }else if (vcpu->arch.smi_pending && !is_smm (vcpu) && kvm_x86_ops->smi_allowed (vcpu)) { vcpu->arch.smi_pending = false;enter_smm (vcpu); }else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed (vcpu)) {
Signature · 1 line
2.13.5
PATCH v5 3/6 KVM: nVMX: set IDTR and GDTR limits when loading L1 host state
In reply to Ladi ProsekIntel SDM 27.5.2 Loading Host Segment and Descriptor-Table Registers:
"The GDTR and IDTR limits are each set to FFFFH."
--- arch/x86/kvm/vmx.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/vmx.c +2 -0
@@ -11328,6 +11328,8 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,vmcs_writel (GUEST_SYSENTER_EIP , vmcs12->host_ia32_sysenter_eip);vmcs_writel (GUEST_IDTR_BASE , vmcs12->host_idtr_base);vmcs_writel (GUEST_GDTR_BASE , vmcs12->host_gdtr_base); +vmcs_write32 (GUEST_IDTR_LIMIT ,0xFFFF ); +vmcs_write32 (GUEST_GDTR_LIMIT ,0xFFFF );/* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ if (vmcs12->vm_exit_controls &VM_EXIT_CLEAR_BNDCFGS )
Signature · 1 line
2.13.5
PATCH v5 4/6 KVM: nVMX: fix SMI injection in guest mode
In reply to Ladi ProsekEntering SMM while running in guest mode wasn't working very well because several pieces of the vcpu state were left set up for nested operation.
Some of the issues observed:
* L1 was getting unexpected VM exits (using L1 interception controls but running in SMM execution environment)
* SMM handler couldn't write to vmx_set_cr4 because of incorrect validity checks predicated on nested.vmxon
* MMU was confused (walk_mmu was still set to nested_mmu)
Intel SDM actually prescribes the logical processor to "leave VMX operation" upon entering SMM in 34.14.1 Default Treatment of SMI Delivery. What we need to do is basically get out of guest mode and set nested.vmxon to false for the duration of SMM. All this completely transparent to L1, i.e. L1 is not given control and no L1 observable state changes.
To avoid code duplication this commit takes advantage of the existing nested vmexit and run functionality, perhaps at the cost of efficiency. To get out of guest mode, nested_vmx_vmexit with exit_reason == -1 is called, a trick already used in vmx_leave_nested. Re-entering is cleaner, using enter_vmx_non_root_mode.
This commit fixes running Windows Server 2016 with Hyper-V enabled in a VM with OVMF firmware (OVMF_CODE-need-smm.fd).
--- arch/x86/kvm/vmx.c | 60 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-)
arch/x86/kvm/vmx.c +49 -11
@@ -486,6 +486,14 @@ struct nested_vmx {u64 nested_vmx_cr4_fixed1;u64 nested_vmx_vmcs_enum;u64 nested_vmx_vmfunc_controls; + +/* SMM related state */ +struct { +/* in VMX operation on SMM entry? */ +bool vmxon; +/* in guest mode on SMM entry? */ +bool guest_mode; + } smm; };#define POSTED_INTR_ON 0
@@ -11426,8 +11434,11 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,leave_guest_mode (vcpu);if (likely (!vmx->fail)) { -prepare_vmcs12 (vcpu, vmcs12, exit_reason, exit_intr_info, - exit_qualification); +if (exit_reason ==-1 ) +sync_vmcs12 (vcpu, vmcs12); +else +prepare_vmcs12 (vcpu, vmcs12, exit_reason, exit_intr_info, + exit_qualification);if (nested_vmx_store_msr (vcpu, vmcs12->vm_exit_msr_store_addr, vmcs12->vm_exit_msr_store_count))
@@ -11491,7 +11502,7 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,*/ kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu); -if (enable_shadow_vmcs) +if (enable_shadow_vmcs && exit_reason !=-1 ) vmx->nested.sync_shadow_vmcs = true;/* in case we halted in L2 */
@@ -11515,12 +11526,13 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,INTR_INFO_VALID_MASK |INTR_TYPE_EXT_INTR ; } -trace_kvm_nested_vmexit_inject (vmcs12->vm_exit_reason, - vmcs12->exit_qualification, - vmcs12->idt_vectoring_info_field, - vmcs12->vm_exit_intr_info, - vmcs12->vm_exit_intr_error_code, -KVM_ISA_VMX ); +if (exit_reason !=-1 ) +trace_kvm_nested_vmexit_inject (vmcs12->vm_exit_reason, + vmcs12->exit_qualification, + vmcs12->idt_vectoring_info_field, + vmcs12->vm_exit_intr_info, + vmcs12->vm_exit_intr_error_code, +KVM_ISA_VMX );load_vmcs12_host_state (vcpu, vmcs12);
@@ -11945,18 +11957,44 @@ static void vmx_setup_mce(struct kvm_vcpu *vcpu)static int vmx_smi_allowed (struct kvm_vcpu *vcpu) { +/* we need a nested vmexit to enter SMM, postpone if run is pending */ +if (to_vmx (vcpu)->nested.nested_run_pending) +return 0 ;return 1 ; }static int vmx_pre_enter_smm (struct kvm_vcpu *vcpu,char *smstate) { -/* TODO: Implement */ +struct vcpu_vmx *vmx =to_vmx (vcpu); + + vmx->nested.smm.guest_mode =is_guest_mode (vcpu); +if (vmx->nested.smm.guest_mode) +nested_vmx_vmexit (vcpu,-1 ,0 ,0 ); + + vmx->nested.smm.vmxon = vmx->nested.vmxon; + vmx->nested.vmxon = false;return 0 ; }static int vmx_pre_leave_smm (struct kvm_vcpu *vcpu,u64 smbase) { -/* TODO: Implement */ +struct vcpu_vmx *vmx =to_vmx (vcpu); +int ret; + +if (vmx->nested.smm.vmxon) { + vmx->nested.vmxon = true; + vmx->nested.smm.vmxon = false; + } + +if (vmx->nested.smm.guest_mode) { + vcpu->arch.hflags &= ~HF_SMM_MASK ; + ret =enter_vmx_non_root_mode (vcpu, false); + vcpu->arch.hflags |=HF_SMM_MASK ; +if (ret) +return ret; + + vmx->nested.smm.guest_mode = false; + }return 0 ; }
Signature · 1 line
2.13.5
PATCH v5 5/6 KVM: nSVM: refactor nested_svm_vmrun
In reply to Ladi ProsekAnalogous to 858e25c06fb0 ("kvm: nVMX: Refactor nested_vmx_run()"), this commit splits nested_svm_vmrun into two parts. The newly introduced enter_svm_guest_mode modifies the vcpu state to transition from L1 to L2, while the code left in nested_svm_vmrun handles the VMRUN instruction.
--- arch/x86/kvm/svm.c | 132 ++++++++++++++++++++++++++++------------------------- 1 file changed, 69 insertions(+), 63 deletions(-)
arch/x86/kvm/svm.c +69 -63
@@ -2916,70 +2916,9 @@ static bool nested_vmcb_checks(struct vmcb *vmcb)return true; } -static bool nested_svm_vmrun (struct vcpu_svm *svm) +static void enter_svm_guest_mode (struct vcpu_svm *svm,u64 vmcb_gpa, +struct vmcb *nested_vmcb,struct page *page) { -struct vmcb *nested_vmcb; -struct vmcb *hsave = svm->nested.hsave; -struct vmcb *vmcb = svm->vmcb; -struct page *page; -u64 vmcb_gpa; - - vmcb_gpa = svm->vmcb->save.rax; - - nested_vmcb =nested_svm_map (svm, svm->vmcb->save.rax, &page); -if (!nested_vmcb) -return false; - -if (!nested_vmcb_checks (nested_vmcb)) { - nested_vmcb->control.exit_code =SVM_EXIT_ERR ; - nested_vmcb->control.exit_code_hi =0 ; - nested_vmcb->control.exit_info_1 =0 ; - nested_vmcb->control.exit_info_2 =0 ; - -nested_svm_unmap (page); - -return false; - } - -trace_kvm_nested_vmrun (svm->vmcb->save.rip, vmcb_gpa, - nested_vmcb->save.rip, - nested_vmcb->control.int_ctl, - nested_vmcb->control.event_inj, - nested_vmcb->control.nested_ctl); - -trace_kvm_nested_intercepts (nested_vmcb->control.intercept_cr &0xffff , - nested_vmcb->control.intercept_cr >>16 , - nested_vmcb->control.intercept_exceptions, - nested_vmcb->control.intercept); - -/* Clear internal status */ -kvm_clear_exception_queue (&svm->vcpu); -kvm_clear_interrupt_queue (&svm->vcpu); - -/* -* Save the old vmcb, so we don't need to pick what we save, but can -* restore everything when a VMEXIT occurs -*/ - hsave->save.es = vmcb->save.es; - hsave->save.cs = vmcb->save.cs; - hsave->save.ss = vmcb->save.ss; - hsave->save.ds = vmcb->save.ds; - hsave->save.gdtr = vmcb->save.gdtr; - hsave->save.idtr = vmcb->save.idtr; - hsave->save.efer = svm->vcpu.arch.efer; - hsave->save.cr0 =kvm_read_cr0 (&svm->vcpu); - hsave->save.cr4 = svm->vcpu.arch.cr4; - hsave->save.rflags =kvm_get_rflags (&svm->vcpu); - hsave->save.rip =kvm_rip_read (&svm->vcpu); - hsave->save.rsp = vmcb->save.rsp; - hsave->save.rax = vmcb->save.rax; -if (npt_enabled) - hsave->save.cr3 = vmcb->save.cr3; -else - hsave->save.cr3 =kvm_read_cr3 (&svm->vcpu); - -copy_vmcb_control_area (hsave, vmcb); -if (kvm_get_rflags (&svm->vcpu) &X86_EFLAGS_IF ) svm->vcpu.arch.hflags |=HF_HIF_MASK ;else
@@ -3072,6 +3011,73 @@ static bool nested_svm_vmrun(struct vcpu_svm *svm)enable_gif (svm);mark_all_dirty (svm->vmcb); +} + +static bool nested_svm_vmrun (struct vcpu_svm *svm) +{ +struct vmcb *nested_vmcb; +struct vmcb *hsave = svm->nested.hsave; +struct vmcb *vmcb = svm->vmcb; +struct page *page; +u64 vmcb_gpa; + + vmcb_gpa = svm->vmcb->save.rax; + + nested_vmcb =nested_svm_map (svm, svm->vmcb->save.rax, &page); +if (!nested_vmcb) +return false; + +if (!nested_vmcb_checks (nested_vmcb)) { + nested_vmcb->control.exit_code =SVM_EXIT_ERR ; + nested_vmcb->control.exit_code_hi =0 ; + nested_vmcb->control.exit_info_1 =0 ; + nested_vmcb->control.exit_info_2 =0 ; + +nested_svm_unmap (page); + +return false; + } + +trace_kvm_nested_vmrun (svm->vmcb->save.rip, vmcb_gpa, + nested_vmcb->save.rip, + nested_vmcb->control.int_ctl, + nested_vmcb->control.event_inj, + nested_vmcb->control.nested_ctl); + +trace_kvm_nested_intercepts (nested_vmcb->control.intercept_cr &0xffff , + nested_vmcb->control.intercept_cr >>16 , + nested_vmcb->control.intercept_exceptions, + nested_vmcb->control.intercept); + +/* Clear internal status */ +kvm_clear_exception_queue (&svm->vcpu); +kvm_clear_interrupt_queue (&svm->vcpu); + +/* +* Save the old vmcb, so we don't need to pick what we save, but can +* restore everything when a VMEXIT occurs +*/ + hsave->save.es = vmcb->save.es; + hsave->save.cs = vmcb->save.cs; + hsave->save.ss = vmcb->save.ss; + hsave->save.ds = vmcb->save.ds; + hsave->save.gdtr = vmcb->save.gdtr; + hsave->save.idtr = vmcb->save.idtr; + hsave->save.efer = svm->vcpu.arch.efer; + hsave->save.cr0 =kvm_read_cr0 (&svm->vcpu); + hsave->save.cr4 = svm->vcpu.arch.cr4; + hsave->save.rflags =kvm_get_rflags (&svm->vcpu); + hsave->save.rip =kvm_rip_read (&svm->vcpu); + hsave->save.rsp = vmcb->save.rsp; + hsave->save.rax = vmcb->save.rax; +if (npt_enabled) + hsave->save.cr3 = vmcb->save.cr3; +else + hsave->save.cr3 =kvm_read_cr3 (&svm->vcpu); + +copy_vmcb_control_area (hsave, vmcb); + +enter_svm_guest_mode (svm, vmcb_gpa, nested_vmcb, page);return true; }
Signature · 1 line
2.13.5
PATCH v5 6/6 KVM: nSVM: fix SMI injection in guest mode
In reply to Ladi ProsekEntering SMM while running in guest mode wasn't working very well because several pieces of the vcpu state were left set up for nested operation.
Some of the issues observed:
* L1 was getting unexpected VM exits (using L1 interception controls but running in SMM execution environment)
* MMU was confused (walk_mmu was still set to nested_mmu)
* INTERCEPT_SMI was not emulated for L1 (KVM never injected SVM_EXIT_SMI)
Intel SDM actually prescribes the logical processor to "leave VMX operation" upon entering SMM in 34.14.1 Default Treatment of SMI Delivery. AMD doesn't seem to document this but they provide fields in the SMM state-save area to stash the current state of SVM. What we need to do is basically get out of guest mode for the duration of SMM. All this completely transparent to L1, i.e. L1 is not given control and no L1 observable state changes.
To avoid code duplication this commit takes advantage of the existing nested vmexit and run functionality, perhaps at the cost of efficiency. To get out of guest mode, nested_svm_vmexit is called, unchanged. Re-entering is performed using enter_svm_guest_mode.
This commit fixes running Windows Server 2016 with Hyper-V enabled in a VM with OVMF firmware (OVMF_CODE-need-smm.fd).
--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/svm.c | 58 ++++++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 3 --- 3 files changed, 58 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1430,4 +1430,7 @@ static inline int kvm_cpu_get_apicid(int mps_cpu) #endif } +#define put_smstate (type, buf, offset, val) \ + *(type *)((buf) + (offset) - 0x7e00) = val +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/svm.c +55 -3
74 lines not on this page · raw message
arch/x86/kvm/x86.c +0 -3
@@ -6479,9 +6479,6 @@ static void process_nmi(struct kvm_vcpu *vcpu)kvm_make_request (KVM_REQ_EVENT , vcpu); } -#define put_smstate (type, buf, offset, val) \ - *(type *)((buf) + (offset) - 0x7e00) = val -static u32 enter_smm_get_segment_flags (struct kvm_segment *seg) {u32 flags =0 ;
Signature · 1 line
2.13.5
Quoted 42 lines · Ladi Prosek
Windows Server 2016 with Hyper-V enabled fails to boot on OVMF with SMM (OVMF_CODE-need-smm.fd). Turns out that the SMM emulation code in KVM does not handle nested virtualization very well, leading to a whole bunch of issues.
For example, Hyper-V uses descriptor table exiting (
SECONDARY_EXEC_DESC) so when the SMM handler tries to switch from real mode a VM exit occurs and is forwarded to a clueless L1.This series fixes it by switching the vcpu to !guest_mode, i.e. to the L1 state, before entering SMM and then switching back to L2 as part of emulating the RSM instruction.
Patches 1 and 2 are common for both Intel and AMD, patches 3-4 fix Intel, and patches 5-6 AMD.
v1->v2:
* Movedleft_smmdetection toemulator_set_hflags(couldn't quite get rid of the field despite my original claim) (Paolo)
* Moved thekvm_x86_ops->post_leave_smm()call a few statements down so it really runs after all state has been synced.
* Added thesmi_allowedcallback (new patch 2) to avoid running intoWARN_ON_ONCE(vmx->nested.nested_run_pending)on Intel.
v2->v3:
* Ommitted patch 4 ("KVM: nVMX: save nested EPT information in SMRAM state save map") and replaced it with ("treat CR4.VMXE as reserved in SMM") (Paolo)
* Implementedsmi_allowedon AMD to support SMI interception. Turns out Windows needs this when running on >1 vCPU.
* Eliminated internal SMM state on AMD and switched to using the SMM state save area in guest memory instead (Paolo)
v3->v4:
* Changed the order of operations inenter_smm(), now saving the original (and potentially L2) state into the SMM state save area.
* Madeem_rsm()reload the SMM state save area ifpost_leave_smm()entered guest mode. This way, SMM handlers see and may change the actual state of the vCPU at the point where SMI was injected (Radim)
* In patch 4, switched to a different way of avoiding the problem of hitting the very check the patch is adding.
Quoted 7 lines
v4->v5:
* Removed patch 4 (CR4.VMXE protection in SMM, will be done separately), patch 3 bacame 4 and new patch 3 fixes a bug inload_vmcs12_host_state()which prevented SMM exit to L2 from working without first restoring the state from the SMM state save area.
* Eliminated the first restore from SMM state save area (Paolo)
* Tweaked theHF_SMM_MASKflag manipulation (Paolo)
Queued, thanks for persisting!
Paolo
Quoted 51 lines · Paolo Bonzini
Quoted 42 lines · Ladi Prosek
Windows Server 2016 with Hyper-V enabled fails to boot on OVMF with SMM (OVMF_CODE-need-smm.fd). Turns out that the SMM emulation code in KVM does not handle nested virtualization very well, leading to a whole bunch of issues.
For example, Hyper-V uses descriptor table exiting (
SECONDARY_EXEC_DESC) so when the SMM handler tries to switch from real mode a VM exit occurs and is forwarded to a clueless L1.This series fixes it by switching the vcpu to !guest_mode, i.e. to the L1 state, before entering SMM and then switching back to L2 as part of emulating the RSM instruction.
Patches 1 and 2 are common for both Intel and AMD, patches 3-4 fix Intel, and patches 5-6 AMD.
v1->v2:
* Movedleft_smmdetection toemulator_set_hflags(couldn't quite get rid of the field despite my original claim) (Paolo)
* Moved thekvm_x86_ops->post_leave_smm()call a few statements down so it really runs after all state has been synced.
* Added thesmi_allowedcallback (new patch 2) to avoid running intoWARN_ON_ONCE(vmx->nested.nested_run_pending)on Intel.
v2->v3:
* Ommitted patch 4 ("KVM: nVMX: save nested EPT information in SMRAM state save map") and replaced it with ("treat CR4.VMXE as reserved in SMM") (Paolo)
* Implementedsmi_allowedon AMD to support SMI interception. Turns out Windows needs this when running on >1 vCPU.
* Eliminated internal SMM state on AMD and switched to using the SMM state save area in guest memory instead (Paolo)
v3->v4:
* Changed the order of operations inenter_smm(), now saving the original (and potentially L2) state into the SMM state save area.
* Madeem_rsm()reload the SMM state save area ifpost_leave_smm()entered guest mode. This way, SMM handlers see and may change the actual state of the vCPU at the point where SMI was injected (Radim)
* In patch 4, switched to a different way of avoiding the problem of hitting the very check the patch is adding.Quoted 7 lines
v4->v5:
* Removed patch 4 (CR4.VMXE protection in SMM, will be done separately), patch 3 bacame 4 and new patch 3 fixes a bug inload_vmcs12_host_state()which prevented SMM exit to L2 from working without first restoring the state from the SMM state save area.
* Eliminated the first restore from SMM state save area (Paolo)
* Tweaked theHF_SMM_MASKflag manipulation (Paolo)
Quoted 1 line
Queued, thanks for persisting!
Thank you and Radim for all the help!
Re: [PATCH v5 3/6] KVM: nVMX: set IDTR and GDTR limits when loading L1 host state
In reply to Ladi ProsekNice.
Quoted 25 lines · Ladi Prosek
Intel SDM 27.5.2 Loading Host Segment and Descriptor-Table Registers:
"The GDTR and IDTR limits are each set to FFFFH."
Signed-off-by Ladi Prosek <[email protected]>--- arch/x86/kvm/vmx.c | 2 ++ 1 file changed, 2 insertions(+)arch/x86/kvm/vmx.c +2
-1@@ -11328,6 +11328,8 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,vmcs_writel (GUEST_SYSENTER_EIP , vmcs12->host_ia32_sysenter_eip);vmcs_writel (GUEST_IDTR_BASE , vmcs12->host_idtr_base);vmcs_writel (GUEST_GDTR_BASE , vmcs12->host_gdtr_base); +vmcs_write32 (GUEST_IDTR_LIMIT ,0xFFFF ); +vmcs_write32 (GUEST_GDTR_LIMIT ,0xFFFF );/* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ if (vmcs12->vm_exit_controls &VM_EXIT_CLEAR_BNDCFGS ) --2.13.5