Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 1/2] KVM: SVM: Don't clobber RFLAGS.RF loaded by a single-stepped IRET
@ 2026-11-25  6:29 Peng Hao
  2026-11-25  6:30 ` [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF Peng Hao
  0 siblings, 1 reply; 3+ messages in thread
From: Peng Hao @ 2026-11-25  6:29 UTC (permalink / raw)
  To: pbonzini, seanjc; +Cc: kvm

Without vNMI, KVM opens the NMI window after an NMI handler by
intercepting IRET and then single-stepping it with TF and RF set.  When
the single-step #DB arrives, disable_nmi_singlestep() clears TF and RF
again unless they were set in the RFLAGS snapshot taken when the
single-step was armed, i.e. before the IRET ran.

That is wrong for RF.  By the time the single-step trap is taken, the
stepped instruction has completed and the CPU has already cleared the RF
that KVM set, so RF in save.rflags is guest state.  It is only set if
the instruction loaded it, which is what the stepped IRET does when the
NMI interrupted a context that was about to resume from an instruction
breakpoint: the OS sets RF in the saved RFLAGS so the breakpoint is not
hit again, the NMI pushes that RFLAGS and the NMI handler's IRET pops
it.  KVM then clears RF based on the stale snapshot, and the guest takes
a second #DB on the same instruction.

In a Linux guest this shows up as a duplicate perf hardware breakpoint
event when a PMI happens to land right after the breakpoint has been
handled, e.g. while sampling with perf at a high rate.

If the single-step trap fired, fold the post-instruction RF into the
snapshot so that disable_nmi_singlestep() leaves it alone.  RF is still
cleared on the event injection path in svm_vcpu_run(), where the
instruction never ran and RF still holds KVM's value.

TF has the same problem in principle when the stepped IRET pops TF=1,
but there KVM cannot tell its own TF apart from the guest's after the
fact, so it is left as is.

Fixes: 44c11430b52c ("KVM: inject NMI after IRET from a previous NMI, not before.")
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 arch/x86/kvm/svm/svm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 581bc3a23c95..118c2ce4742d 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2087,6 +2087,18 @@ static int db_interception(struct kvm_vcpu *vcpu)
 	}
 
 	if (svm->nmi_singlestep) {
+		/*
+		 * If the single-step trap fired, the stepped instruction has
+		 * completed and RF now holds the guest's value: the CPU clears
+		 * RF when an instruction completes, unless the instruction
+		 * loaded RFLAGS, e.g. an IRET returning to an instruction
+		 * breakpoint that the guest has already handled.  Don't let
+		 * disable_nmi_singlestep() clear the guest's RF.
+		 */
+		if (svm->vmcb->save.dr6 & DR_STEP)
+			svm->nmi_singlestep_guest_rflags |=
+				svm->vmcb->save.rflags & X86_EFLAGS_RF;
+
 		disable_nmi_singlestep(svm);
 		/* Make sure we check for pending NMIs upon entry */
 		kvm_make_request(KVM_REQ_EVENT, vcpu);
-- 
2.43.7


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF
  2026-11-25  6:29 [PATCH 1/2] KVM: SVM: Don't clobber RFLAGS.RF loaded by a single-stepped IRET Peng Hao
@ 2026-11-25  6:30 ` Peng Hao
  2026-11-25  6:39   ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Peng Hao @ 2026-11-25  6:30 UTC (permalink / raw)
  To: pbonzini, seanjc; +Cc: kvm

Add a test for the case where KVM single-steps the IRET of an NMI
handler to find out when NMIs are unblocked (SVM without vNMI) and that
IRET returns to an instruction breakpoint with RF set in the popped
RFLAGS.

The guest arms an instruction breakpoint, sends itself an NMI that is
delivered at the breakpoint address, and from the NMI handler sends a
second NMI, which stays pending, and sets RF in the interrupt frame.
The second NMI is delivered once the first handler's IRET completes,
and when it returns the breakpoint must not fire.

Without the preceding fix the test fails on AMD with kvm_amd.vnmi=0:

  x86/nmi_iret_rf_test.c:80: READ_ONCE(db_count) == 0
  0x1 != 0x0 (READ_ONCE(db_count) != 0)

It passes with vNMI, and on Intel, where KVM doesn't single-step to
open the NMI window.

Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/nmi_iret_rf_test.c      | 109 ++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/nmi_iret_rf_test.c

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 17a4256e9081..d7df9df89de2 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -147,6 +147,7 @@ TEST_GEN_PROGS_x86 += x86/xapic_tpr_test
 TEST_GEN_PROGS_x86 += x86/xcr0_cpuid_test
 TEST_GEN_PROGS_x86 += x86/xss_msr_test
 TEST_GEN_PROGS_x86 += x86/debug_regs
+TEST_GEN_PROGS_x86 += x86/nmi_iret_rf_test
 TEST_GEN_PROGS_x86 += x86/tsc_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_pmu_caps_test
 TEST_GEN_PROGS_x86 += x86/xen_shinfo_test
diff --git a/tools/testing/selftests/kvm/x86/nmi_iret_rf_test.c b/tools/testing/selftests/kvm/x86/nmi_iret_rf_test.c
new file mode 100644
index 000000000000..9d2ced788329
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/nmi_iret_rf_test.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Verify that KVM doesn't clobber RFLAGS.RF loaded by the guest's IRET when
+ * KVM single-steps the IRET of an NMI handler to open the NMI window.
+ *
+ * Without vNMI, SVM detects the end of NMI blocking by intercepting IRET and
+ * then single-stepping it.  If that IRET returns to an instruction that has
+ * an enabled instruction breakpoint with RF set in the popped RFLAGS (which is
+ * what an OS does when it resumes after handling the breakpoint), the guest
+ * must not see a second #DB on that instruction.
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "apic.h"
+
+#define DR7_L0		BIT(0)
+#define DR7_GE		BIT(9)
+#define DR7_FIXED_1	BIT(10)
+
+extern char bp_target[];
+
+static int nmi_count;
+static int db_count;
+
+static u64 self_nmi_icr(void)
+{
+	return ((u64)x2apic_read_reg(APIC_ID) << 32) | APIC_DEST_PHYSICAL | APIC_DM_NMI;
+}
+
+static void send_self_nmi(void)
+{
+	x2apic_write_reg(APIC_ICR, self_nmi_icr());
+}
+
+static void guest_nmi_handler(struct ex_regs *regs)
+{
+	if (nmi_count++)
+		return;
+
+	GUEST_ASSERT_EQ(regs->rip, (u64)bp_target);
+
+	/*
+	 * NMIs are blocked, so this one stays pending until KVM sees the IRET
+	 * below complete.  Return to bp_target with RF set, as if resuming
+	 * after the instruction breakpoint was handled.
+	 */
+	send_self_nmi();
+	regs->rflags |= X86_EFLAGS_RF;
+}
+
+static void guest_db_handler(struct ex_regs *regs)
+{
+	db_count++;
+	regs->rflags |= X86_EFLAGS_RF;
+}
+
+static void guest_code(void)
+{
+	u64 icr;
+
+	x2apic_enable();
+
+	asm volatile("mov %0, %%dr0" : : "r"(bp_target));
+	asm volatile("mov %0, %%dr7" : : "r"((u64)(DR7_FIXED_1 | DR7_GE | DR7_L0)));
+
+	/*
+	 * Send the NMI with a bare WRMSR so that it is delivered with RIP
+	 * pointing at bp_target, i.e. before the breakpoint is checked.
+	 */
+	icr = self_nmi_icr();
+	asm volatile("wrmsr\n\t"
+		     "bp_target: nop"
+		     : : "c"(APIC_BASE_MSR + (APIC_ICR >> 4)),
+			 "a"((u32)icr), "d"((u32)(icr >> 32)) : "memory");
+
+	asm volatile("mov %0, %%dr7" : : "r"((u64)DR7_FIXED_1));
+
+	GUEST_ASSERT_EQ(READ_ONCE(nmi_count), 2);
+	GUEST_ASSERT_EQ(READ_ONCE(db_count), 0);
+	GUEST_DONE();
+}
+
+int main(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler);
+	vm_install_exception_handler(vm, DB_VECTOR, guest_db_handler);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT(uc);
+		break;
+	case UCALL_DONE:
+		break;
+	default:
+		TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+	}
+
+	kvm_vm_free(vm);
+	return 0;
+}
-- 
2.43.7


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF
  2026-11-25  6:30 ` [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF Peng Hao
@ 2026-11-25  6:39   ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-25  6:39 UTC (permalink / raw)
  To: Peng Hao; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: selftests: standalone nmi_iret_rf_test duplicates
  debug_regs harness
--

commit 3aebfc62612be05298e4bd8add5fc7086d476fc4
Author: Peng Hao <flyingpenghao@gmail.com>

KVM: selftests: Test that an NMI-window single-step preserves the guest's RF

This commit adds a standalone KVM x86 selftest verifying that RFLAGS.RF is
preserved across an NMI handler's IRET when KVM single-steps the IRET to
detect the end of NMI blocking.

> diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
> index 95867977beb3c..2ca17a1cb275a 100644
> --- a/tools/testing/selftests/kvm/Makefile.kvm
> +++ b/tools/testing/selftests/kvm/Makefile.kvm
> @@ -154,6 +154,7 @@ TEST_GEN_PROGS_x86 += x86/xapic_tpr_test
>  TEST_GEN_PROGS_x86 += x86/xcr0_cpuid_test
>  TEST_GEN_PROGS_x86 += x86/xss_msr_test
>  TEST_GEN_PROGS_x86 += x86/debug_regs
> +TEST_GEN_PROGS_x86 += x86/nmi_iret_rf_test
>  TEST_GEN_PROGS_x86 += x86/tsc_msrs_test
>  TEST_GEN_PROGS_x86 += x86/vmx_pmu_caps_test
>  TEST_GEN_PROGS_x86 += x86/xen_shinfo_test

[Severity: Low]
Could this test case be folded into tools/testing/selftests/kvm/x86/debug_regs.c
instead of adding a standalone x86/nmi_iret_rf_test binary?

debug_regs.c already sets up a single-vCPU VM with x2apic_enable() and a
DB_VECTOR exception handler to test hardware instruction breakpoints and
guest-handled #DB interactions with interrupt delivery and IRET.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125063000.92971-1-flyingpeng@tencent.com?part=2

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25  6:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  6:29 [PATCH 1/2] KVM: SVM: Don't clobber RFLAGS.RF loaded by a single-stepped IRET Peng Hao
2026-11-25  6:30 ` [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF Peng Hao
2026-11-25  6:39   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox