Skip to contentSkip to conversation

x86/svm: Add testing for L1 intercept bug

PATCH Superseded Kevin Cheng 4 participants 7 messages Dec 24, 2025 – May 27 KVM

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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]/

v2 -> v3:
- Added assertions to unsupported instructions test to verify that CPU does not support features enabling tested instructions [Yosry Ahmed]
- Added some comments [Yosry Ahmed]
- Changed insn_invpcid() into assembly [Yosry Ahmed]
- Added a check to the unsupported instruction test to ensure that L1's vmcb is not modified by KVM [Yosry Ahmed]
- Redacted reviewed-by tag for 2nd patch in series. I mistakingly added that

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

v1 -> v2:
- Added save/restore helpers for all intercepts as suggested by Yosry
- Reuse invpcid_safe() for added test as suggested by Yosry
- Include '-skinit' in unittests.cfg for added test target as pointed out by Yosry

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

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

 lib/x86/processor.h |   1 +
 x86/svm.c           |  18 +++-
 x86/svm.h           |  89 ++++++++++++++++--
 x86/svm_tests.c     | 224 +++++++++++++++++++++++++++++++-------------
 x86/unittests.cfg   |  11 ++-
 5 files changed, 262 insertions(+), 81 deletions(-)

--
2.52.0.351.gbe84eed79e-goog

#1 lore raw

PATCH v3 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       |  18 +++++--
 x86/svm.h       |  84 ++++++++++++++++++++++++++----
 x86/svm_tests.c | 136 ++++++++++++++++++++++++------------------------
 3 files changed, 159 insertions(+), 79 deletions(-)
x86/svm.c +15 -3
index de9eb19443caa..18b2538c6e8c7 100644
@@ -137,6 +137,18 @@ static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
 	seg->base = base;
 }
 
+void vmcb_save_intercepts(struct vmcb *vmcb, u32 *saved_intercepts)
+{
+	for (int i = 0; i < MAX_INTERCEPT; i++)
+		saved_intercepts[i] = vmcb->control.intercept[i];
+}
+
+void vmcb_restore_intercepts(struct vmcb *vmcb, u32 *saved_intercepts)
+{
+	for (int i = 0; i < MAX_INTERCEPT; i++)
+		vmcb->control.intercept[i] = saved_intercepts[i];
+}
+
 inline void vmmcall(void)
 {
 	asm volatile ("vmmcall" : : : "memory");
@@ -193,9 +205,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 +75 -9

122 lines not on this page · raw message

x86/svm_tests.c +69 -67

423 lines not on this page · raw message

Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#2 lore raw

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

Quoted 2 lines · Kevin Cheng

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

Please split this up.

1. Add helpers to set/clear bits.
2. Add helpers to save/restore intercepts.
3. Reorganize
4. Add "missing" intercepts.

Maybe #2 and #3 go together? But there's _so_ much going on here I genuinely can't tell.

maintainer
#3 lore raw

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

Quoted 7 lines · Sean Christopherson
Quoted 2 lines · Kevin Cheng

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

Please split this up.

Quoted 7 lines

1. Add helpers to set/clear bits.
2. Add helpers to save/restore intercepts.
3. Reorganize
4. Add "missing" intercepts.

Maybe #2 and #3 go together? But there's _so_ much going on here I genuinely can't tell.

I agree, splitting all the way can't hurt...

Paolo

#4 lore raw

PATCH v3 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]/

---
 lib/x86/processor.h |  1 +
 x86/svm.h           |  5 ++-
 x86/svm_tests.c     | 88 +++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   | 11 +++++-
 4 files changed, 103 insertions(+), 2 deletions(-)
lib/x86/processor.h +1 -0
index 42dd2d2a4787c..7e1c562aa7378 100644
@@ -358,6 +358,7 @@ struct x86_cpu_feature {
  * Extended Leafs, a.k.a. AMD defined
  */
 #define X86_FEATURE_SVM			X86_CPU_FEATURE(0x80000001, 0, ECX, 2)
+#define X86_FEATURE_SKINIT		X86_CPU_FEATURE(0x80000001, 0, ECX, 12)
 #define X86_FEATURE_PERFCTR_CORE	X86_CPU_FEATURE(0x80000001, 0, ECX, 23)
 #define X86_FEATURE_NX			X86_CPU_FEATURE(0x80000001, 0, EDX, 20)
 #define X86_FEATURE_GBPAGES		X86_CPU_FEATURE(0x80000001, 0, EDX, 26)
x86/svm.h +4 -1
index c22c252fed001..e2158ab0622bb 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 +88 -0

100 lines not on this page · raw message

x86/unittests.cfg +10 -1
index 522318d32bf68..27a1a68104362 100644
@@ -253,11 +253,20 @@ 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
 
+# RDPRU feature name is not defined in Qemu so it can't be excluded here. This
+# should be okay though since KVM does not advertise RDPRU by default anyways.
+[svm_unsupported_instruction_intercept_test]
+file = svm.flat
+test_args = "svm_unsupported_instruction_intercept_test"
+qemu_params = -cpu max,+svm,-rdtscp,-xsave,-invpcid,-skinit
+arch = x86_64
+groups = svm
+
 [svm_pause_filter]
 file = svm.flat
 test_args = pause_filter_test
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#5 lore raw
In reply to Kevin Cheng
Quoted 6 lines · Kevin Cheng

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]

Quoted 8 lines

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.

[...]

Applied to kvm-x86 next, even though the SKINIT test fails on the Turin system available to me. AIUI, there's a ucode bug or something for pre-production hardware?

Anyways, I mention that because if others see failures, then we'll probably need to figure out a way to automatically skip the SKINIT test (or maybe eat the #GP and treat it as XFAIL?).

[1/2] x86/svm: Add missing svm intercepts

      https://github.com/kvm-x86/kvm-unit-tests/commit/8322ddeda22b
[2/2] x86/svm: Add unsupported instruction intercept test
      https://github.com/kvm-x86/kvm-unit-tests/commit/7586f98f809e

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next

#6 lore raw
Quoted 20 lines · Sean Christopherson
Quoted 6 lines · Kevin Cheng

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]

Quoted 8 lines

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.

[...]

Applied to kvm-x86 next, even though the SKINIT test fails on the Turin system available to me. AIUI, there's a ucode bug or something for pre-production hardware?

Quoted 6 lines

Anyways, I mention that because if others see failures, then we'll probably need to figure out a way to automatically skip the SKINIT test (or maybe eat the #GP and treat it as XFAIL?).

[1/2] x86/svm: Add missing svm intercepts

      https://github.com/kvm-x86/kvm-unit-tests/commit/8322ddeda22b

So I believe there is no need to split that patch anymore since you already applied it, right?

Also, I think there was a newer version of this series:
https://lore.kernel.org/kvm/[email protected]/

Quoted 5 lines

[2/2] x86/svm: Add unsupported instruction intercept test

      https://github.com/kvm-x86/kvm-unit-tests/commit/7586f98f809e

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next