[PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2

From: Sohil Mehta

Date: Wed Dec 02 2026 - 03:05:06 EST


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