Skip to contentSkip to conversation

x86/svm: Add testing for L1 intercept bug

PATCH New Kevin Cheng 1 participant 3 messages Mar 12 KVM

lore
Share & tools
lore
#0 lore raw

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

v3 -> v4:
- Fixed incorrect intercept handling introduced by the svm intercept refactoring in TEST_BITMAP_ADDR
- Switched to using vmcb_save_intercepts() in handle_exception_in_l1
- Fixed incorrect reference to saved_intercept instead of saved_intercepts in test_msrpm_iopm_bitmap_addrs

v3: 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     | 229 +++++++++++++++++++++++++++++++-------------
 x86/unittests.cfg   |  11 ++-
 5 files changed, 266 insertions(+), 82 deletions(-)

--
2.53.0.851.ga537e3e6e9-goog

#1 lore raw

PATCH v4 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 | 141 +++++++++++++++++++++++++-----------------------
 3 files changed, 163 insertions(+), 80 deletions(-)
x86/svm.c +15 -3
index de9eb19443ca..18b2538c6e8c 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 +73 -68

433 lines not on this page · raw message

--
2.53.0.851.ga537e3e6e9-goog

#2 lore raw

PATCH v4 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 42dd2d2a4787..7e1c562aa737 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 c22c252fed00..e2158ab0622b 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 522318d32bf6..27a1a6810436 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.53.0.851.ga537e3e6e9-goog