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 --- 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