From: Sohil Mehta <sohil.mehta@intel.com>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Shuah Khan <skhan@linuxfoundation.org>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H . Peter Anvin" <hpa@zytor.com>, Xin Li <xin@zytor.com>,
Peter Zijlstra <peterz@infradead.org>,
Andrew Cooper <andrew.cooper3@citrix.com>,
Tom Lendacky <thomas.lendacky@amd.com>,
Nikunj A Dadhania <nikunj@amd.com>,
Shivansh Dhiman <shivansh.dhiman@amd.com>,
Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>,
David Woodhouse <dwmw@amazon.co.uk>,
Chao Gao <chao.gao@intel.com>,
Binbin Wu <binbin.wu@linux.intel.com>,
Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>,
"Chang S . Bae" <chang.seok.bae@intel.com>,
Sohil Mehta <sohil.mehta@intel.com>,
Yosry Ahmed <yosry@kernel.org>,
David Matlack <dmatlack@google.com>,
x86@kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2
Date: Wed, 2 Dec 2026 00:02:24 -0800 [thread overview]
Message-ID: <20261202080224.4165883-5-sohil.mehta@intel.com> (raw)
In-Reply-To: <20261202080224.4165883-1-sohil.mehta@intel.com>
KVM always intercepts #DB on VMX, so it handles every ICEBP that L2
executes, unless L1 intercepts #DB itself. L1 must still see the #DB of
an ICEBP like it would on bare metal.
Add a nested VMX test with three cases. Each one ends with L1 resuming
L2 with the event, and L2's #DB handler returning past the ICEBP:
1) L1 intercepts #DB. The VM-exit reports a privileged software
exception of length 1, at the ICEBP. This is the reference.
2) L1 doesn't intercept #DB, and the #DB's delivery faults on an
unmapped stack. The #PF VM-exit to L1 must report the #DB as in
case 1, in the IDT-vectoring information.
3) L1 injects an INT1 itself, and its delivery faults the same way.
The test needs nested VMX, but not FRED.
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
Without patch 3, L1 finds a hardware exception for the ICEBP's #DB in
case 2. Without patch 2, L2 loops on the ICEBP in case 1, and the test
hangs.
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
tools/testing/selftests/kvm/include/x86/vmx.h | 7 +
.../selftests/kvm/x86/vmx_icebp_test.c | 174 ++++++++++++++++++
3 files changed, 182 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86/vmx_icebp_test.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 95867977beb3..d65cc2ac35ed 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -143,6 +143,7 @@ TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
TEST_GEN_PROGS_x86 += x86/vmx_apicv_updates_test
TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
+TEST_GEN_PROGS_x86 += x86/vmx_icebp_test
TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 8fd97434eee5..6e6e4bc97f15 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -99,6 +99,13 @@
#define VMX_EPT_VPID_CAP_1G_PAGES 0x00020000
#define VMX_EPT_VPID_CAP_AD_BITS 0x00200000
+#define INTR_INFO_VECTOR_MASK 0x000000ff
+#define INTR_INFO_INTR_TYPE_MASK 0x00000700
+#define INTR_INFO_DELIVER_CODE_MASK 0x00000800
+#define INTR_INFO_VALID_MASK 0x80000000
+
+#define INTR_TYPE_PRIV_SW_EXCEPTION 0x00000500
+
#define EXIT_REASON_FAILED_VMENTRY 0x80000000
enum vmcs_field {
diff --git a/tools/testing/selftests/kvm/x86/vmx_icebp_test.c b/tools/testing/selftests/kvm/x86/vmx_icebp_test.c
new file mode 100644
index 000000000000..0d54c9838777
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmx_icebp_test.c
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Verify that L1 sees the #DB of an ICEBP (INT1) in L2 as a privileged software
+ * exception, like it would on bare metal, both when L1 intercepts the #DB and
+ * when its delivery faults into L1.
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "vmx.h"
+
+/* A canonical address that the guest page tables don't map. */
+#define UNMAPPED_RSP 0x700000000000
+
+static volatile int db_count;
+static volatile u64 db_rip;
+
+static void l2_db_handler(struct ex_regs *regs)
+{
+ db_count++;
+ db_rip = regs->rip;
+}
+
+/*
+ * L2 code. The #DB handler returns to the VMCALL after each ICEBP, and to the
+ * VMCALL after the NOP at which L1 injects an INT1. It's plain asm so that
+ * nothing touches the unmapped stack before the ICEBP or the injected INT1.
+ */
+extern char l2_icebp[], l2_icebp_bad_stack[], l2_icebp_bad_stack_insn[], l2_int1[];
+asm(".pushsection .text\n"
+ "l2_icebp:\n"
+ " .byte 0xf1\n"
+ " vmcall\n"
+ " ud2\n"
+ "l2_icebp_bad_stack:\n"
+ " movabs $" __stringify(UNMAPPED_RSP) ", %rsp\n"
+ "l2_icebp_bad_stack_insn:\n"
+ " .byte 0xf1\n"
+ " vmcall\n"
+ " ud2\n"
+ "l2_int1:\n"
+ " nop\n"
+ " vmcall\n"
+ " ud2\n"
+ ".popsection");
+
+/* Re-inject the event that L1 was told about, and check its delivery. */
+static void reinject(struct vmx_pages *vmx, u32 intr_info, u32 insn_len,
+ u64 expected_rip)
+{
+ db_count = 0;
+ vmwrite(EXCEPTION_BITMAP, 0);
+ vmwrite(VM_ENTRY_INTR_INFO_FIELD,
+ intr_info & (INTR_INFO_VECTOR_MASK | INTR_INFO_INTR_TYPE_MASK |
+ INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK));
+ vmwrite(VM_ENTRY_INSTRUCTION_LEN, insn_len);
+ vmwrite(GUEST_RSP, (u64)vmx->stack);
+
+ vmresume();
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_VMCALL);
+ GUEST_ASSERT_EQ(db_count, 1);
+ GUEST_ASSERT_EQ(db_rip, expected_rip);
+}
+
+/*
+ * Check the #PF exit that interrupted the delivery of an INT1 at @rip, then
+ * re-inject the INT1 and check that L2 takes it.
+ */
+static void check_int1_vectoring(struct vmx_pages *vmx, u64 rip)
+{
+ u32 idt_info = vmread(IDT_VECTORING_INFO_FIELD);
+
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_EXCEPTION_NMI);
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_INTR_INFO) & INTR_INFO_VECTOR_MASK, PF_VECTOR);
+ __GUEST_ASSERT(idt_info & INTR_INFO_VALID_MASK,
+ "No event in the IDT-vectoring info (0x%x)", idt_info);
+ GUEST_ASSERT_EQ(idt_info & INTR_INFO_VECTOR_MASK, DB_VECTOR);
+ GUEST_ASSERT_EQ(idt_info & INTR_INFO_INTR_TYPE_MASK, INTR_TYPE_PRIV_SW_EXCEPTION);
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_INSTRUCTION_LEN), 1);
+ GUEST_ASSERT_EQ(vmread(GUEST_RIP), rip);
+
+ reinject(vmx, idt_info, vmread(VM_EXIT_INSTRUCTION_LEN), rip + 1);
+}
+
+static void l1_icebp_intercepted(struct vmx_pages *vmx)
+{
+ u32 intr_info;
+
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+
+ prepare_vmcs(vmx, l2_icebp);
+ vmwrite(EXCEPTION_BITMAP, BIT(DB_VECTOR));
+
+ vmlaunch();
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_EXCEPTION_NMI);
+ intr_info = vmread(VM_EXIT_INTR_INFO);
+ GUEST_ASSERT_EQ(intr_info & INTR_INFO_VECTOR_MASK, DB_VECTOR);
+ GUEST_ASSERT_EQ(intr_info & INTR_INFO_INTR_TYPE_MASK, INTR_TYPE_PRIV_SW_EXCEPTION);
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_INSTRUCTION_LEN), 1);
+ GUEST_ASSERT_EQ(vmread(GUEST_RIP), (u64)l2_icebp);
+
+ reinject(vmx, intr_info, 1, (u64)l2_icebp + 1);
+ GUEST_DONE();
+}
+
+static void l1_icebp_delivery_fault(struct vmx_pages *vmx)
+{
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+
+ prepare_vmcs(vmx, l2_icebp_bad_stack);
+ vmwrite(EXCEPTION_BITMAP, BIT(PF_VECTOR));
+
+ vmlaunch();
+ check_int1_vectoring(vmx, (u64)l2_icebp_bad_stack_insn);
+ GUEST_DONE();
+}
+
+static void l1_int1_delivery_fault(struct vmx_pages *vmx)
+{
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+
+ prepare_vmcs(vmx, l2_int1);
+ vmwrite(EXCEPTION_BITMAP, BIT(PF_VECTOR));
+ vmwrite(GUEST_RSP, UNMAPPED_RSP);
+ vmwrite(VM_ENTRY_INTR_INFO_FIELD,
+ INTR_INFO_VALID_MASK | INTR_TYPE_PRIV_SW_EXCEPTION | DB_VECTOR);
+ vmwrite(VM_ENTRY_INSTRUCTION_LEN, 1);
+
+ vmlaunch();
+ check_int1_vectoring(vmx, (u64)l2_int1);
+ GUEST_DONE();
+}
+
+static void run_test(void *l1_guest_code)
+{
+ struct kvm_vcpu *vcpu;
+ gva_t vmx_pages_gva;
+ struct kvm_vm *vm;
+ struct ucall uc;
+
+ vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+ vcpu_alloc_vmx(vm, &vmx_pages_gva);
+ vcpu_args_set(vcpu, 1, vmx_pages_gva);
+ vm_install_exception_handler(vm, DB_VECTOR, l2_db_handler);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_DONE:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ default:
+ TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+ }
+
+ kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+
+ run_test(l1_icebp_intercepted);
+ run_test(l1_icebp_delivery_fault);
+ run_test(l1_int1_delivery_fault);
+
+ return 0;
+}
--
2.43.0
prev parent reply other threads:[~2026-12-02 8:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception Sohil Mehta
2026-12-02 8:02 ` [PATCH 1/4] KVM: x86: Track the event type of a queued exception Sohil Mehta
2026-12-02 8:02 ` [PATCH 2/4] KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interrupted Sohil Mehta
2026-12-02 8:02 ` [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception Sohil Mehta
2026-12-02 8:02 ` Sohil Mehta [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202080224.4165883-5-sohil.mehta@intel.com \
--to=sohil.mehta@intel.com \
--cc=Neeraj.Upadhyay@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=binbin.wu@linux.intel.com \
--cc=bp@alien8.de \
--cc=chang.seok.bae@intel.com \
--cc=chao.gao@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=dmatlack@google.com \
--cc=dwmw@amazon.co.uk \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maciej.wieczor-retman@intel.com \
--cc=mingo@redhat.com \
--cc=nikunj@amd.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=seanjc@google.com \
--cc=shivansh.dhiman@amd.com \
--cc=skhan@linuxfoundation.org \
--cc=tglx@kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®