Skip to contentSkip to conversation

x86/svm: Add testing for L1 intercept bug

PATCH Superseded Kevin Cheng 2 participants 6 messages Dec 5, 2025 – Dec 15, 2025 KVM

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover x86/svm: Add testing for L1 intercept bug

If a feature is not advertised to L1, L1 intercepts for instructions controlled by this feature should be ignored. Currently, the added test fails due to a bug in nested vm exit handling where vmcb12 intercepts are checked before vmcb02 intercepts, causing the #UD exception to never be injected into L2 if the L1 intercept is set. This is fixed in [0]

The first patch just adds the missing intercepts needed for testing and restructures the vmcb_control_area struct to make adding the missing intercepts less ugly. The second patch adds the test which disables all relevant features that have available instruction intercepts, and checks that the #UD exception is correctly delivered despite the L1 intercept being set.

[0] https://lore.kernel.org/all/[email protected]/

Kevin Cheng (2):
x86/svm: Add missing svm intercepts x86/svm: Add unsupported instruction intercept test

 x86/svm.c         |   6 +-
 x86/svm.h         |  87 +++++++++++++++++++---
 x86/svm_tests.c   | 186 ++++++++++++++++++++++++++++++++--------------
 x86/unittests.cfg |   9 ++-
 4 files changed, 218 insertions(+), 70 deletions(-)

--
2.52.0.223.gf5cc29aaa4-goog

#1 lore raw

PATCH 1/2 x86/svm: Add missing svm intercepts

Some intercepts are missing from the KUT svm testing. Add all missing intercepts and reorganize the svm intercept definition/setting/clearing.

---
 x86/svm.c       |   6 +--
 x86/svm.h       |  82 +++++++++++++++++++++++++++++++----
 x86/svm_tests.c | 111 ++++++++++++++++++++++++------------------------
 3 files changed, 131 insertions(+), 68 deletions(-)
x86/svm.c +3 -3
index de9eb19443caa..9a4c14e368cd4 100644
@@ -193,9 +193,9 @@ void vmcb_ident(struct vmcb *vmcb)
 	save->cr2 = read_cr2();
 	save->g_pat = rdmsr(MSR_IA32_CR_PAT);
 	save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
-	ctrl->intercept = (1ULL << INTERCEPT_VMRUN) |
-		(1ULL << INTERCEPT_VMMCALL) |
-		(1ULL << INTERCEPT_SHUTDOWN);
+	vmcb_set_intercept(INTERCEPT_VMRUN);
+	vmcb_set_intercept(INTERCEPT_VMMCALL);
+	vmcb_set_intercept(INTERCEPT_SHUTDOWN);
 	ctrl->iopm_base_pa = virt_to_phys(io_bitmap);
 	ctrl->msrpm_base_pa = virt_to_phys(msr_bitmap);
 
x86/svm.h +73 -9

114 lines not on this page · raw message

x86/svm_tests.c +55 -56

374 lines not on this page · raw message

Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
#2 lore raw

Re: [kvm-unit-tests PATCH 1/2] x86/svm: Add missing svm intercepts

Quoted 5 lines · Kevin Cheng

Some intercepts are missing from the KUT svm testing. Add all missing intercepts and reorganize the svm intercept definition/setting/clearing.

Nice cleanup!

[..]

x86/svm.h enum { · line 132

Quoted 13 lines
x86/svm.h +2 -7
@@ -69,13 +128,8 @@ enum {
 };
 
 struct __attribute__ ((__packed__)) vmcb_control_area {
-       u16 intercept_cr_read;
-       u16 intercept_cr_write;
-       u16 intercept_dr_read;
-       u16 intercept_dr_write;
-       u32 intercept_exceptions;
-       u64 intercept;
-       u8 reserved_1[40];
+       u32 intercept[MAX_INTERCEPT];
+       u8 reserved_1[36];

Maybe do "u32 reserved_1[15 - MAX_INTERCEPT];" like the KVM definition?

x86/svm.h enum { · line 135

Quoted 3 lines
x86/svm.h +0 -0
        u16 pause_filter_thresh;
        u16 pause_filter_count;
        u64 iopm_base_pa;

[..] \

x86/svm_tests.c static void test_dr(void) · line 2576

Quoted 6 lines
x86/svm_tests.c +1 -1
@@ -2574,7 +2573,7 @@ static void test_dr(void)
  */
 static void test_msrpm_iopm_bitmap_addrs(void)
 {
-       u64 saved_intercept = vmcb->control.intercept;
+       u32 saved_intercept = vmcb->control.intercept[INTERCEPT_WORD3];

I think hardcoding save/restore for the relevant intercept word here is a bit fragile (and leaks the abstraction). If the test is extended to update more intercepts that are not in INTERCEPT_WORD3, it can easily be missed.

How about introducing helpers to save/restore all intercepts and using them here (and in svm_vmload_vmsave() below)? We can define the array on the stack in the test and pass it to the helpers.

Something like this (untested):

static void test_msrpm_iopm_bitmap_addrs(void)

{
       u32 saved_intercepts[MAX_INTERCEPT];

       save_intercepts(vmcb, saved_intercepts);
       ...
       restore_intercepts(vmcb, saved_intercepts);
}
Quoted 80 lines
x86/svm_tests.c +13 -13
        u64 addr_beyond_limit = 1ull << cpuid_maxphyaddr();
        u64 addr = virt_to_phys(msr_bitmap) & (~((1ull << 12) - 1));
 
@@ -2615,7 +2614,7 @@ static void test_msrpm_iopm_bitmap_addrs(void)
        TEST_BITMAP_ADDR(saved_intercept, INTERCEPT_IOIO_PROT, addr,
                         SVM_EXIT_VMMCALL, "IOPM");
 
-       vmcb->control.intercept = saved_intercept;
+       vmcb->control.intercept[INTERCEPT_WORD3] = saved_intercept;
 }
 
 /*
@@ -2811,7 +2810,7 @@ static void vmload_vmsave_guest_main(struct svm_test *test)
 
 static void svm_vmload_vmsave(void)
 {
-       u32 intercept_saved = vmcb->control.intercept;
+       u32 intercept_saved = vmcb->control.intercept[INTERCEPT_WORD4];
 
        test_set_guest(vmload_vmsave_guest_main);
 
@@ -2819,8 +2818,8 @@ static void svm_vmload_vmsave(void)
         * Disabling intercept for VMLOAD and VMSAVE doesn't cause
         * respective #VMEXIT to host
         */
-       vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMLOAD);
-       vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMSAVE);
+       vmcb_clear_intercept(INTERCEPT_VMLOAD);
+       vmcb_clear_intercept(INTERCEPT_VMSAVE);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMMCALL, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMMCALL #VMEXIT");
@@ -2829,39 +2828,39 @@ static void svm_vmload_vmsave(void)
         * Enabling intercept for VMLOAD and VMSAVE causes respective
         * #VMEXIT to host
         */
-       vmcb->control.intercept |= (1ULL << INTERCEPT_VMLOAD);
+       vmcb_set_intercept(INTERCEPT_VMLOAD);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMLOAD, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMLOAD #VMEXIT");
-       vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMLOAD);
-       vmcb->control.intercept |= (1ULL << INTERCEPT_VMSAVE);
+       vmcb_clear_intercept(INTERCEPT_VMLOAD);
+       vmcb_set_intercept(INTERCEPT_VMSAVE);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMSAVE, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMSAVE #VMEXIT");
-       vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMSAVE);
+       vmcb_clear_intercept(INTERCEPT_VMSAVE);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMMCALL, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMMCALL #VMEXIT");
 
-       vmcb->control.intercept |= (1ULL << INTERCEPT_VMLOAD);
+       vmcb_set_intercept(INTERCEPT_VMLOAD);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMLOAD, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMLOAD #VMEXIT");
-       vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMLOAD);
+       vmcb_clear_intercept(INTERCEPT_VMLOAD);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMMCALL, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMMCALL #VMEXIT");
 
-       vmcb->control.intercept |= (1ULL << INTERCEPT_VMSAVE);
+       vmcb_set_intercept(INTERCEPT_VMSAVE);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMSAVE, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMSAVE #VMEXIT");
-       vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMSAVE);
+       vmcb_clear_intercept(INTERCEPT_VMSAVE);
        svm_vmrun();
        report(vmcb->control.exit_code == SVM_EXIT_VMMCALL, "Test "
               "VMLOAD/VMSAVE intercept: Expected VMMCALL #VMEXIT");
 
-       vmcb->control.intercept = intercept_saved;
+       vmcb->control.intercept[INTERCEPT_WORD4] = intercept_saved;
 }
 

[..]

#3 lore raw

PATCH 2/2 x86/svm: Add unsupported instruction intercept test

In reply to Kevin Cheng

Add tests that expect a nested vm exit, due to an unsupported instruction, to be handled by L0 even if L1 intercepts are set for that instruction.

The new test exercises bug fixed by:
https://lore.kernel.org/all/[email protected]/

---
 x86/svm.h         |  5 +++-
 x86/svm_tests.c   | 75 +++++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg |  9 +++++-
 3 files changed, 87 insertions(+), 2 deletions(-)
x86/svm.h +4 -1
index 93ef6f772c6ee..86d58c3100275 100644
@@ -406,7 +406,10 @@ struct __attribute__ ((__packed__)) vmcb {
 #define SVM_EXIT_MONITOR	0x08a
 #define SVM_EXIT_MWAIT		0x08b
 #define SVM_EXIT_MWAIT_COND	0x08c
-#define SVM_EXIT_NPF  		0x400
+#define SVM_EXIT_XSETBV		0x08d
+#define SVM_EXIT_RDPRU		0x08e
+#define SVM_EXIT_INVPCID	0x0a2
+#define SVM_EXIT_NPF		0x400
 
 #define SVM_EXIT_ERR		-1
 
x86/svm_tests.c +75 -0

87 lines not on this page · raw message

x86/unittests.cfg +8 -1
index 522318d32bf68..ec456d779b35c 100644
@@ -253,11 +253,18 @@ arch = x86_64
 [svm]
 file = svm.flat
 smp = 2
-test_args = "-pause_filter_test"
+test_args = "-pause_filter_test -svm_unsupported_instruction_intercept_test"
 qemu_params = -cpu max,+svm -m 4g
 arch = x86_64
 groups = svm
 
+[svm_unsupported_instruction_intercept_test]
+file = svm.flat
+test_args = "svm_unsupported_instruction_intercept_test"
+qemu_params = -cpu max,+svm,-rdtscp,-xsave,-invpcid
+arch = x86_64
+groups = svm
+
 [svm_pause_filter]
 file = svm.flat
 test_args = pause_filter_test
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
#4 lore raw

Re: [kvm-unit-tests PATCH 2/2] x86/svm: Add unsupported instruction intercept test

Quoted 9 lines · Kevin Cheng

Add tests that expect a nested vm exit, due to an unsupported instruction, to be handled by L0 even if L1 intercepts are set for that instruction.

The new test exercises bug fixed by:
https://lore.kernel.org/all/[email protected]/

[..]

Quoted 12 lines
x86/svm_tests.c +8 -0
@@ -3572,6 +3572,80 @@ static void svm_shutdown_intercept_test(void)
        report(vmcb->control.exit_code == SVM_EXIT_SHUTDOWN, "shutdown test passed");
 }
 
+struct InvpcidDesc {
+       uint64_t pcid : 12;
+       uint64_t reserved : 52;
+       uint64_t addr;
+};
+
+static void insn_invpcid(struct svm_test *test)
+{

x86/svm_tests.c static void svm_shutdown_intercept_test(void) · line 3592

Quoted 10 lines
x86/svm_tests.c +10 -0
+       struct InvpcidDesc desc = {0};
+       unsigned long type = 0;
+
+       __asm__ volatile (
+               "invpcid %1, %0"
+               :
+               : "r" (type), "m" (desc)
+               : "memory"
+       );
+}

Can we use invpcid_safe() and the descriptor already defined in processor.h?

Quoted 75 lines
x86/svm_tests.c +57 -0
+
+asm(
+       "insn_rdtscp: rdtscp;ret\n\t"
+       "insn_skinit: skinit;ret\n\t"
+       "insn_xsetbv: xor %eax, %eax; xor %edx, %edx; xor %ecx, %ecx; xsetbv;ret\n\t"
+       "insn_rdpru: xor %ecx, %ecx; rdpru;ret\n\t"
+);
+
+extern void insn_rdtscp(struct svm_test *test);
+extern void insn_skinit(struct svm_test *test);
+extern void insn_xsetbv(struct svm_test *test);
+extern void insn_rdpru(struct svm_test *test);
+
+struct insn_table {
+       const char *name;
+       u64 intercept;
+       void (*insn_func)(struct svm_test *test);
+       u32 reason;
+};
+
+static struct insn_table insn_table[] = {
+       { "RDTSCP", INTERCEPT_RDTSCP, insn_rdtscp, SVM_EXIT_RDTSCP},
+       { "SKINIT", INTERCEPT_SKINIT, insn_skinit, SVM_EXIT_SKINIT},
+       { "XSETBV", INTERCEPT_XSETBV, insn_xsetbv, SVM_EXIT_XSETBV},
+       { "RDPRU", INTERCEPT_RDPRU, insn_rdpru, SVM_EXIT_RDPRU},
+       { "INVPCID", INTERCEPT_INVPCID, insn_invpcid, SVM_EXIT_INVPCID},
+       { NULL },
+};
+
+/*
+ * Test that L1 does not intercept instructions that are not advertised in
+ * guest CPUID.
+ */
+static void svm_unsupported_instruction_intercept_test(void)
+{
+       u32 cur_insn;
+       u32 exit_code;
+
+       vmcb_set_intercept(INTERCEPT_EXCEPTION_OFFSET + UD_VECTOR);
+
+       for (cur_insn = 0; insn_table[cur_insn].name != NULL; ++cur_insn) {
+               test_set_guest(insn_table[cur_insn].insn_func);
+               vmcb_set_intercept(insn_table[cur_insn].intercept);
+               svm_vmrun();
+               exit_code = vmcb->control.exit_code;
+
+               if (exit_code == SVM_EXIT_EXCP_BASE + UD_VECTOR)
+                       report_pass("UD Exception injected");
+               else if (exit_code == insn_table[cur_insn].reason)
+                       report_fail("L1 should not intercept %s when instruction is not advertised in guest CPUID",
+                                   insn_table[cur_insn].name);
+               else
+                       report_fail("Unknown exit reason, 0x%x", exit_code);
+       }
+}
+
 struct svm_test svm_tests[] = {
        { "null", default_supported, default_prepare,
          default_prepare_gif_clear, null_test,
@@ -3713,6 +3787,7 @@ struct svm_test svm_tests[] = {
        TEST(svm_tsc_scale_test),
        TEST(pause_filter_test),
        TEST(svm_shutdown_intercept_test),
+       TEST(svm_unsupported_instruction_intercept_test),
        { NULL, NULL, NULL, NULL, NULL, NULL, NULL }
 };
 
x86/unittests.cfg +0 -0
index 522318d32bf68..ec456d779b35c 100644
@@ -253,11 +253,18 @@ arch = x86_64
 [svm]
 file = svm.flat
 smp = 2

x86/unittests.cfg arch = x86_64 · line 264

Quoted 10 lines
x86/unittests.cfg +5 -1
-test_args = "-pause_filter_test"
+test_args = "-pause_filter_test -svm_unsupported_instruction_intercept_test"
 qemu_params = -cpu max,+svm -m 4g
 arch = x86_64
 groups = svm
 
+[svm_unsupported_instruction_intercept_test]
+file = svm.flat
+test_args = "svm_unsupported_instruction_intercept_test"
+qemu_params = -cpu max,+svm,-rdtscp,-xsave,-invpcid

Does this cover all 5 instructions being tested?

Quoted 9 lines
x86/unittests.cfg +3 -1
+arch = x86_64
+groups = svm
+
 [svm_pause_filter]
 file = svm.flat
 test_args = pause_filter_test
--

2.52.0.223.gf5cc29aaa4-goog

#5 lore raw

Re: [kvm-unit-tests PATCH 2/2] x86/svm: Add unsupported instruction intercept test

Quoted 118 lines · Yosry Ahmed
Quoted 9 lines · Kevin Cheng

Add tests that expect a nested vm exit, due to an unsupported instruction, to be handled by L0 even if L1 intercepts are set for that instruction.

The new test exercises bug fixed by:
https://lore.kernel.org/all/[email protected]/

[..]

Quoted 12 lines
x86/svm_tests.c +8 -0
@@ -3572,6 +3572,80 @@ static void svm_shutdown_intercept_test(void)
        report(vmcb->control.exit_code == SVM_EXIT_SHUTDOWN, "shutdown test passed");
 }
 
+struct InvpcidDesc {
+       uint64_t pcid : 12;
+       uint64_t reserved : 52;
+       uint64_t addr;
+};
+
+static void insn_invpcid(struct svm_test *test)
+{

x86/svm_tests.c static void svm_shutdown_intercept_test(void) · line 3592

Quoted 10 lines
x86/svm_tests.c +10 -0
+       struct InvpcidDesc desc = {0};
+       unsigned long type = 0;
+
+       __asm__ volatile (
+               "invpcid %1, %0"
+               :
+               : "r" (type), "m" (desc)
+               : "memory"
+       );
+}

Can we use invpcid_safe() and the descriptor already defined in processor.h?

Quoted 81 lines
x86/svm_tests.c +57 -0
+
+asm(
+       "insn_rdtscp: rdtscp;ret\n\t"
+       "insn_skinit: skinit;ret\n\t"
+       "insn_xsetbv: xor %eax, %eax; xor %edx, %edx; xor %ecx, %ecx; xsetbv;ret\n\t"
+       "insn_rdpru: xor %ecx, %ecx; rdpru;ret\n\t"
+);
+
+extern void insn_rdtscp(struct svm_test *test);
+extern void insn_skinit(struct svm_test *test);
+extern void insn_xsetbv(struct svm_test *test);
+extern void insn_rdpru(struct svm_test *test);
+
+struct insn_table {
+       const char *name;
+       u64 intercept;
+       void (*insn_func)(struct svm_test *test);
+       u32 reason;
+};
+
+static struct insn_table insn_table[] = {
+       { "RDTSCP", INTERCEPT_RDTSCP, insn_rdtscp, SVM_EXIT_RDTSCP},
+       { "SKINIT", INTERCEPT_SKINIT, insn_skinit, SVM_EXIT_SKINIT},
+       { "XSETBV", INTERCEPT_XSETBV, insn_xsetbv, SVM_EXIT_XSETBV},
+       { "RDPRU", INTERCEPT_RDPRU, insn_rdpru, SVM_EXIT_RDPRU},
+       { "INVPCID", INTERCEPT_INVPCID, insn_invpcid, SVM_EXIT_INVPCID},
+       { NULL },
+};
+
+/*
+ * Test that L1 does not intercept instructions that are not advertised in
+ * guest CPUID.
+ */
+static void svm_unsupported_instruction_intercept_test(void)
+{
+       u32 cur_insn;
+       u32 exit_code;
+
+       vmcb_set_intercept(INTERCEPT_EXCEPTION_OFFSET + UD_VECTOR);
+
+       for (cur_insn = 0; insn_table[cur_insn].name != NULL; ++cur_insn) {
+               test_set_guest(insn_table[cur_insn].insn_func);
+               vmcb_set_intercept(insn_table[cur_insn].intercept);
+               svm_vmrun();
+               exit_code = vmcb->control.exit_code;
+
+               if (exit_code == SVM_EXIT_EXCP_BASE + UD_VECTOR)
+                       report_pass("UD Exception injected");
+               else if (exit_code == insn_table[cur_insn].reason)
+                       report_fail("L1 should not intercept %s when instruction is not advertised in guest CPUID",
+                                   insn_table[cur_insn].name);
+               else
+                       report_fail("Unknown exit reason, 0x%x", exit_code);
+       }
+}
+
 struct svm_test svm_tests[] = {
        { "null", default_supported, default_prepare,
          default_prepare_gif_clear, null_test,
@@ -3713,6 +3787,7 @@ struct svm_test svm_tests[] = {
        TEST(svm_tsc_scale_test),
        TEST(pause_filter_test),
        TEST(svm_shutdown_intercept_test),
+       TEST(svm_unsupported_instruction_intercept_test),
        { NULL, NULL, NULL, NULL, NULL, NULL, NULL }
 };
 
x86/unittests.cfg +1 -1
index 522318d32bf68..ec456d779b35c 100644
@@ -253,11 +253,18 @@ arch = x86_64
 [svm]
 file = svm.flat
 smp = 2
-test_args = "-pause_filter_test"
+test_args = "-pause_filter_test -svm_unsupported_instruction_intercept_test"
 qemu_params = -cpu max,+svm -m 4g
 arch = x86_64
 groups = svm
 
Quoted 6 lines

x86/unittests.cfg arch = x86_64 · line 264

Quoted 4 lines
x86/unittests.cfg +4 -0
+[svm_unsupported_instruction_intercept_test]
+file = svm.flat
+test_args = "svm_unsupported_instruction_intercept_test"
+qemu_params = -cpu max,+svm,-rdtscp,-xsave,-invpcid

Does this cover all 5 instructions being tested?

Sorry I missed SKINIT. rdpru feature name is not defined in Qemu so can't be excluded (qemu/target/i386/cpu.c under FEAT_8000_0008_EBX). This should be okay though since KVM does not advertise RDPRU by default.

Quoted 10 lines
Quoted 10 lines
x86/unittests.cfg +3 -1
+arch = x86_64
+groups = svm
+
 [svm_pause_filter]
 file = svm.flat
 test_args = pause_filter_test
--

2.52.0.223.gf5cc29aaa4-goog