KVM: SVM: improve NMI window singlestep
v2 cover KVM: SVM: improve NMI window singlestep
On AMD hardware, Hyper-V doesn't work nested on KVM if L1 is started with more than one vCPU. The problem is in NMI which is heavily used by Windows on SMP systems.
This series fixes a couple of related issues with the current NMI singlestep logic and makes Windows with Hyper-V happy.
v1->v2:
* simplified patch 4 to call smp_send_reschedule instead of intercepting iret (Paolo)
* added patch 5 to suppress singlestep on GIF=0 and nested exit (Paolo)
* dropped most of the cover letter blurb because we now singlestep only over iret, as originally intended (Radim)
* still left nmi_singlestep_guest_rflags and the defensive code accessing it in -- it shouldn't be needed if we really singlestep only over iret but it is more robust this way (Radim)
* ran eventinj.flat, found no regression (baseline appears to be broken on nested, I'll look into it separately) (Paolo)
Ladi Prosek (5):
KVM: SVM: introduce disable_nmi_singlestep helper
KVM: nSVM: do not forward NMI window singlestep VM exits to L1
KVM: SVM: hide TF/RF flags used by NMI singlestep
KVM: SVM: don't NMI singlestep over event injection
KVM: SVM: suppress unnecessary NMI singlestep on GIF=0 and nested exit
PATCH v2 1/5 KVM: SVM: introduce disable_nmi_singlestep helper
Just moving the code to a new helper in preparation for following commits.
--- arch/x86/kvm/svm.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/svm.c +9 -4
@@ -963,6 +963,14 @@ static void svm_disable_lbrv(struct vcpu_svm *svm)set_msr_interception (msrpm,MSR_IA32_LASTINTTOIP ,0 ,0 ); } +static void disable_nmi_singlestep (struct vcpu_svm *svm) +{ + svm->nmi_singlestep = false; +if (!(svm->vcpu.guest_debug &KVM_GUESTDBG_SINGLESTEP )) + svm->vmcb->save.rflags &= + ~(X86_EFLAGS_TF |X86_EFLAGS_RF ); +} +/* Note: * This hash table is used to map VM_ID to a struct kvm_arch, * when handling AMD IOMMU GALOG notification to schedule in
@@ -2111,10 +2119,7 @@ static int db_interception(struct vcpu_svm *svm) }if (svm->nmi_singlestep) { - svm->nmi_singlestep = false; -if (!(svm->vcpu.guest_debug &KVM_GUESTDBG_SINGLESTEP )) - svm->vmcb->save.rflags &= - ~(X86_EFLAGS_TF |X86_EFLAGS_RF ); +disable_nmi_singlestep (svm); } if (svm->vcpu.guest_debug &
Signature · 1 line
2.9.3
PATCH v2 2/5 KVM: nSVM: do not forward NMI window singlestep VM exits to L1
In reply to Ladi ProsekNested hypervisor should not see singlestep VM exits if singlestepping was enabled internally by KVM. Windows is particularly sensitive to this and known to bluescreen on unexpected VM exits.
--- arch/x86/kvm/svm.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-)
arch/x86/kvm/svm.c +40 -5
@@ -189,6 +189,7 @@ struct vcpu_svm {struct nested_state nested;bool nmi_singlestep; +u64 nmi_singlestep_guest_rflags;unsigned int3_injected;unsigned long int3_rip;
@@ -966,9 +967,13 @@ static void svm_disable_lbrv(struct vcpu_svm *svm)static void disable_nmi_singlestep (struct vcpu_svm *svm) { svm->nmi_singlestep = false; -if (!(svm->vcpu.guest_debug &KVM_GUESTDBG_SINGLESTEP )) - svm->vmcb->save.rflags &= - ~(X86_EFLAGS_TF |X86_EFLAGS_RF ); +if (!(svm->vcpu.guest_debug &KVM_GUESTDBG_SINGLESTEP )) { +/* Clear our flags if they were not set by the guest */ +if (!(svm->nmi_singlestep_guest_rflags &X86_EFLAGS_TF )) + svm->vmcb->save.rflags &= ~X86_EFLAGS_TF ; +if (!(svm->nmi_singlestep_guest_rflags &X86_EFLAGS_RF )) + svm->vmcb->save.rflags &= ~X86_EFLAGS_RF ; + } }/* Note:
@@ -2538,6 +2543,31 @@ static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)return (value & mask) ?NESTED_EXIT_DONE :NESTED_EXIT_HOST ; } +/* DB exceptions for our internal use must not cause vmexit */ +static int nested_svm_intercept_db (struct vcpu_svm *svm) +{ +unsigned long dr6; + +/* if we're not singlestepping, it's not ours */ +if (!svm->nmi_singlestep) +return NESTED_EXIT_DONE ; + +/* if it's not a singlestep exception, it's not ours */ +if (kvm_get_dr (&svm->vcpu,6 , &dr6)) +return NESTED_EXIT_DONE ; +if (!(dr6 &DR6_BS )) +return NESTED_EXIT_DONE ; + +/* if the guest is singlestepping, it should get the vmexit */ +if (svm->nmi_singlestep_guest_rflags &X86_EFLAGS_TF ) { +disable_nmi_singlestep (svm); +return NESTED_EXIT_DONE ; + } + +/* it's ours, the nested hypervisor must not see this one */ +return NESTED_EXIT_HOST ; +} +static int nested_svm_exit_special (struct vcpu_svm *svm) {u32 exit_code = svm->vmcb->control.exit_code;
@@ -2593,8 +2623,12 @@ static int nested_svm_intercept(struct vcpu_svm *svm) } case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {u32 excp_bits =1 << (exit_code -SVM_EXIT_EXCP_BASE ); -if (svm->nested.intercept_exceptions & excp_bits) - vmexit =NESTED_EXIT_DONE ; +if (svm->nested.intercept_exceptions & excp_bits) { +if (exit_code ==SVM_EXIT_EXCP_BASE +DB_VECTOR ) + vmexit =nested_svm_intercept_db (svm); +else + vmexit =NESTED_EXIT_DONE ; + }/* async page fault always cause vmexit */ else if ((exit_code ==SVM_EXIT_EXCP_BASE +PF_VECTOR ) && svm->apf_reason !=0 )
@@ -4635,6 +4669,7 @@ static void enable_nmi_window(struct kvm_vcpu *vcpu)* Something prevents NMI from been injected. Single step over possible * problem (IRET or exception injection or interrupt shadow) */ + svm->nmi_singlestep_guest_rflags =svm_get_rflags (vcpu); svm->nmi_singlestep = true; svm->vmcb->save.rflags |= (X86_EFLAGS_TF |X86_EFLAGS_RF ); }
Signature · 1 line
2.9.3
PATCH v2 3/5 KVM: SVM: hide TF/RF flags used by NMI singlestep
In reply to Ladi ProsekThese flags are used internally by SVM so it's cleaner to not leak them to callers of svm_get_rflags. This is similar to how the TF flag is handled on KVM_GUESTDBG_SINGLESTEP by kvm_get_rflags and kvm_set_rflags.
Without this change, the flags may propagate from host VMCB to nested VMCB or vice versa while singlestepping over a nested VM enter/exit, and then get stuck in inappropriate places.
Example: NMI singlestepping is enabled while running L1 guest. The instruction to step over is VMRUN and nested vmrun emulation stashes rflags to hsave->save.rflags. Then if singlestepping is disabled while still in L2, TF/RF will be cleared from the nested VMCB but the next nested VM exit will restore them from hsave->save.rflags and cause an unexpected DB exception.
--- arch/x86/kvm/svm.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/svm.c +14 -1
@@ -1725,11 +1725,24 @@ static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)static unsigned long svm_get_rflags (struct kvm_vcpu *vcpu) { -return to_svm (vcpu)->vmcb->save.rflags; +struct vcpu_svm *svm =to_svm (vcpu); +unsigned long rflags = svm->vmcb->save.rflags; + +if (svm->nmi_singlestep) { +/* Hide our flags if they were not set by the guest */ +if (!(svm->nmi_singlestep_guest_rflags &X86_EFLAGS_TF )) + rflags &= ~X86_EFLAGS_TF ; +if (!(svm->nmi_singlestep_guest_rflags &X86_EFLAGS_RF )) + rflags &= ~X86_EFLAGS_RF ; + } +return rflags; }static void svm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags) { +if (to_svm (vcpu)->nmi_singlestep) + rflags |= (X86_EFLAGS_TF |X86_EFLAGS_RF ); +/* * Any change of EFLAGS.VM is accompanied by a reload of SS * (caused by either a task switch or an inter-privilege IRET),
Signature · 1 line
2.9.3
PATCH v2 4/5 KVM: SVM: don't NMI singlestep over event injection
In reply to Ladi ProsekSinglestepping is enabled by setting the TF flag and care must be taken to not let the guest see (and reuse at an inconvenient time) the modified rflag value. One such case is event injection, as part of which flags are pushed on the stack and restored later on iret.
This commit disables singlestepping when we're about to inject an event and forces an immediate exit for us to re-evaluate the NMI related state.
--- arch/x86/kvm/svm.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/x86/kvm/svm.c +16 -0
@@ -4823,6 +4823,22 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (svm->nested.exit_required))return ; +/* +* Disable singlestep if we're injecting an interrupt/exception. +* We don't want our modified rflags to be pushed on the stack where +* we might not be able to easily reset them if we disabled NMI +* singlestep later. +*/ +if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { +/* +* Event injection happens before external interrupts cause a +* vmexit and interrupts are disabled here, so smp_send_reschedule +* is enough to force an immediate vmexit. +*/ +disable_nmi_singlestep (svm); +smp_send_reschedule (vcpu->cpu); + } +pre_svm_run (svm);sync_lapic_to_cr8 (vcpu);
Signature · 1 line
2.9.3
PATCH v2 5/5 KVM: SVM: suppress unnecessary NMI singlestep on GIF=0 and nested exit
In reply to Ladi Prosekenable_nmi_window is supposed to be a no-op if we know that we'll see a VM exit by the time the NMI window opens. This commit adds two more cases:
* We intercept stgi so we don't need to singlestep on GIF=0.
* We emulate nested vmexit so we don't need to singlestep when nested VM exit is required.
--- arch/x86/kvm/svm.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kvm/svm.c +6 -0
@@ -4678,6 +4678,12 @@ static void enable_nmi_window(struct kvm_vcpu *vcpu) == HF_NMI_MASK)return ;/* IRET will cause a vm exit */ +if ((svm->vcpu.arch.hflags &HF_GIF_MASK ) ==0 ) +return ;/* STGI will cause a vm exit */ + +if (svm->nested.exit_required) +return ;/* we're not going to run the guest yet */ +/* * Something prevents NMI from been injected. Single step over possible * problem (IRET or exception injection or interrupt shadow)
Signature · 1 line
2.9.3
Hi Ladi,
2017-06-21 15:06 GMT+08:00 Ladi Prosek <[email protected]>:
Quoted 3 lines
On AMD hardware, Hyper-V doesn't work nested on KVM if L1 is started with more than one vCPU. The problem is in NMI which is heavily used by Windows on SMP systems.
Do you mean there is a PMU NMI watchdog to detect hardlockup in windows similar to in linux kernel, otherwise how to understand NMI is heavily used by Windows on SMP systems?
Regards,
Wanpeng Li
Quoted 9 lines · Wanpeng Li
Hi Ladi,
2017-06-21 15:06 GMT+08:00 Ladi Prosek <[email protected]>:Quoted 3 lines
On AMD hardware, Hyper-V doesn't work nested on KVM if L1 is started with more than one vCPU. The problem is in NMI which is heavily used by Windows on SMP systems.
Do you mean there is a PMU NMI watchdog to detect hardlockup in windows similar to in linux kernel, otherwise how to understand NMI is heavily used by Windows on SMP systems?
I think they just use it for IPI. I see ~60k NMI injections by the time Windows Server 2016 with Hyper-V enabled gets to the login screen. There are no NMI injections without Hyper-V and also no injections with Hyper-V on a single-vcpu VM.
As to what led to this design, your guess is as good as mine :)
Quoted 2 lines
Regards,
Wanpeng Li
Quoted 17 lines · Ladi Prosek
On AMD hardware, Hyper-V doesn't work nested on KVM if L1 is started with more than one vCPU. The problem is in NMI which is heavily used by Windows on SMP systems.
This series fixes a couple of related issues with the current NMI singlestep logic and makes Windows with Hyper-V happy.
v1->v2:* simplified patch 4 to call
smp_send_rescheduleinstead of intercepting iret (Paolo)* added patch 5 to suppress singlestep on GIF=0 and nested exit (Paolo)
* dropped most of the cover letter blurb because we now singlestep only over iret, as originally intended (Radim)
Quoted 6 lines
* still left
nmi_singlestep_guest_rflagsand the defensive code accessing it in -- it shouldn't be needed if we really singlestep only over iret but it is more robust this way (Radim)* ran eventinj.flat, found no regression (baseline appears to be broken on nested, I'll look into it separately) (Paolo)
Queued, thanks!
Paolo