Skip to contentSkip to conversation

x86/svm: Make nSVM MSR test useful

PATCH Applied Sean Christopherson 2 participants 12 messages Jun 5, 2025 – Jun 10, 2025 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

v2 cover x86/svm: Make nSVM MSR test useful

Fix the nSVM MSR interception test to actually detect failures, and expand its coverage to test:

- Out-of-range MSRs
- With all other bits in the MSPRM clear
- {RD,WR}MSR with interception *disabled*
- That L1 and L2 see the same value for RDMSR when interception is disabled

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

Sean Christopherson (8):
x86: nSVM: Actually report missed MSR intercepts as failures
x86: nSVM: Test MSRs just outside the ranges of the MSR Permissions
Map
x86: nSVM: Clean up variable types and names in test_msr_intercept()
x86: Expand the suite of bitops to cover all set/clear operations
x86: nVMX: Use set_bit() instead of test_and_set_bit() when return is
ignored
x86: nSVM: Set MSRPM bit on-demand when testing interception
x86: nSVM: Verify disabling {RD,WR}MSR interception behaves as
expected
x86: nSVM: Verify L1 and L2 see same MSR value when interception is
disabled

 lib/x86/asm/bitops.h |  86 +++++++++++++++-
 lib/x86/processor.h  |  12 ---
 x86/svm_tests.c      | 240 +++++++++++++++++++++++++++++++++----------
 x86/vmx_tests.c      |   4 +-
 4 files changed, 269 insertions(+), 73 deletions(-)

base-commit: 72d110d8286baf1b355301cc8c8bdb42be2663fb

Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer
#1 lore raw

PATCH v2 1/8 x86: nSVM: Actually report missed MSR intercepts as failures

Report an error if KVM fails to generate a nested VM-Exit due to MSR interception instead of eating the #GP and printing a "warning". Printing an innocuous message on failure makes the test completely worthless.

---
 x86/svm_tests.c | 76 ++++++++++++++++++-------------------------------
 1 file changed, 27 insertions(+), 49 deletions(-)
x86/svm_tests.c +27 -49
index 416b6aad..b738eb44 100644
@@ -312,7 +312,6 @@ static void prepare_msr_intercept(struct svm_test *test)
 {
 	default_prepare(test);
 	vmcb->control.intercept |= (1ULL << INTERCEPT_MSR_PROT);
-	vmcb->control.intercept_exceptions |= (1ULL << GP_VECTOR);
 	memset(msr_bitmap, 0xff, MSR_BITMAP_SIZE);
 }
 
@@ -320,6 +319,8 @@ static void test_msr_intercept(struct svm_test *test)
 {
 	unsigned long msr_value = 0xef8056791234abcd; /* Arbitrary value */
 	unsigned long msr_index;
+	u64 ignored;
+	int vector;
 
 	for (msr_index = 0; msr_index <= 0xc0011fff; msr_index++) {
 		if (msr_index == 0xC0010131 /* MSR_SEV_STATUS */) {
@@ -340,11 +341,13 @@ static void test_msr_intercept(struct svm_test *test)
 
 		test->scratch = -1;
 
-		rdmsr(msr_index);
-
-		/* Check that a read intercept occurred for MSR at msr_index */
-		if (test->scratch != msr_index)
-			report_fail("MSR 0x%lx read intercept", msr_index);
+		vector = rdmsr_safe(msr_index, &ignored);
+		if (vector)
+			report_fail("Expected RDMSR(0x%lx) to #VMEXIT, got exception '%u'",
+				    msr_index, vector);
+		else if (test->scratch != msr_index)
+			report_fail("Expected RDMSR(0x%lx) to #VMEXIT, got scratch '%ld",
+				    msr_index, test->scratch);
 
 		/*
 		 * Poor man approach to generate a value that
@@ -352,11 +355,13 @@ static void test_msr_intercept(struct svm_test *test)
 		 */
 		msr_value += (msr_value << 1);
 
-		wrmsr(msr_index, msr_value);
-
-		/* Check that a write intercept occurred for MSR with msr_value */
-		if (test->scratch != msr_value)
-			report_fail("MSR 0x%lx write intercept", msr_index);
+		vector = wrmsr_safe(msr_index, msr_value);
+		if (vector)
+			report_fail("Expected WRMSR(0x%0lx) to #VMEXIT, got exception '%u'",
+				    msr_index, vector);
+		else if (test->scratch != msr_value)
+			report_fail("Expected WRMSR(0x%lx) to #VMEXIT, got scratch '%ld' (wanted %ld)",
+				    msr_index, test->scratch, msr_value);
 	}
 
 	test->scratch = -2;
@@ -365,41 +370,13 @@ static void test_msr_intercept(struct svm_test *test)
 static bool msr_intercept_finished(struct svm_test *test)
 {
 	u32 exit_code = vmcb->control.exit_code;
-	u64 exit_info_1;
-	u8 *opcode;
 
-	if (exit_code == SVM_EXIT_MSR) {
-		exit_info_1 = vmcb->control.exit_info_1;
-	} else {
-		/*
-		 * If #GP exception occurs instead, check that it was
-		 * for RDMSR/WRMSR and set exit_info_1 accordingly.
-		 */
+	if (exit_code == SVM_EXIT_VMMCALL)
+		return true;
 
-		if (exit_code != (SVM_EXIT_EXCP_BASE + GP_VECTOR))
-			return true;
-
-		opcode = (u8 *)vmcb->save.rip;
-		if (opcode[0] != 0x0f)
-			return true;
-
-		switch (opcode[1]) {
-		case 0x30: /* WRMSR */
-			exit_info_1 = 1;
-			break;
-		case 0x32: /* RDMSR */
-			exit_info_1 = 0;
-			break;
-		default:
-			return true;
-		}
-
-		/*
-		 * Warn that #GP exception occurred instead.
-		 * RCX holds the MSR index.
-		 */
-		printf("%s 0x%lx #GP exception\n",
-		       exit_info_1 ? "WRMSR" : "RDMSR", get_regs().rcx);
+	if (exit_code != SVM_EXIT_MSR) {
+		report_fail("Wanted MSR VM-Exit, got reason 0x%x", exit_code);
+		return true;
 	}
 
 	/* Jump over RDMSR/WRMSR instruction */
@@ -413,9 +390,8 @@ static bool msr_intercept_finished(struct svm_test *test)
 	 *      RDX holds the upper 32 bits of the MSR value,
 	 *      while RAX hold its lower 32 bits.
 	 */
-	if (exit_info_1)
-		test->scratch =
-			((get_regs().rdx << 32) | (vmcb->save.rax & 0xffffffff));
+	if (vmcb->control.exit_info_1)
+		test->scratch = ((get_regs().rdx << 32) | (vmcb->save.rax & 0xffffffff));
 	else
 		test->scratch = get_regs().rcx;
 
@@ -3065,7 +3041,7 @@ static void svm_intr_intercept_mix_run_guest(volatile int *counter, int expected
 		*counter = 0;
 
 	sti();  // host IF value should not matter
-	clgi(); // vmrun will set back GI to 1
+	clgi(); // vmrun will set back GIF to 1
 
 	svm_vmrun();
 
@@ -3077,7 +3053,9 @@ static void svm_intr_intercept_mix_run_guest(volatile int *counter, int expected
 	if (counter)
 		report(*counter == 1, "Interrupt is expected");
 
-	report (vmcb->control.exit_code == expected_vmexit, "Test expected VM exit");
+	report(vmcb->control.exit_code == expected_vmexit,
+	       "Wanted VM-Exit reason 0x%x, got 0x%x",
+	       expected_vmexit, vmcb->control.exit_code);
 	report(vmcb->save.rflags & X86_EFLAGS_IF, "Guest should have EFLAGS.IF set now");
 	cli();
 }
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer
#2 lore raw

PATCH v2 2/8 x86: nSVM: Test MSRs just outside the ranges of the MSR Permissions Map

In reply to Sean Christopherson

Verify that KVM correctly intercepts MSRs outside of all MSRPM ranges. To keep the run time sane, test only MSRs just before/after each range. Odds are very good that any bug that affects one out-of-range MSR will affect all out-of-range MSRs.

---
 x86/svm_tests.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
x86/svm_tests.c +9 -6
index b738eb44..1795d7f6 100644
@@ -322,7 +322,7 @@ static void test_msr_intercept(struct svm_test *test)
 	u64 ignored;
 	int vector;
 
-	for (msr_index = 0; msr_index <= 0xc0011fff; msr_index++) {
+	for (msr_index = 0; msr_index <= 0xc0012000; msr_index++) {
 		if (msr_index == 0xC0010131 /* MSR_SEV_STATUS */) {
 			/*
 			 * Per section 15.34.10 "SEV_STATUS MSR" of AMD64 Architecture
@@ -333,11 +333,14 @@ static void test_msr_intercept(struct svm_test *test)
 			continue;
 		}
 
-		/* Skips gaps between supported MSR ranges */
-		if (msr_index == 0x2000)
-			msr_index = 0xc0000000;
-		else if (msr_index == 0xc0002000)
-			msr_index = 0xc0010000;
+		/*
+		 * Test one MSR just before and after each range, but otherwise
+		 * skips gaps between supported MSR ranges.
+		 */
+		if (msr_index == 0x2000 + 1)
+			msr_index = 0xc0000000 - 1;
+		else if (msr_index == 0xc0002000 + 1)
+			msr_index = 0xc0010000 - 1;
 
 		test->scratch = -1;
 
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer
#3 lore raw

PATCH v2 3/8 x86: nSVM: Clean up variable types and names in test_msr_intercept()

In reply to Sean Christopherson

Use a u64 for the MSR value and a u32 for the index to match the actual sizes of MSR values and indices, and opportunistically tweak the names to shorten line lengths and prepare for future enhancements.

---
 x86/svm_tests.c | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)
x86/svm_tests.c +21 -22
index 1795d7f6..5d06249f 100644
@@ -317,13 +317,12 @@ static void prepare_msr_intercept(struct svm_test *test)
 
 static void test_msr_intercept(struct svm_test *test)
 {
-	unsigned long msr_value = 0xef8056791234abcd; /* Arbitrary value */
-	unsigned long msr_index;
-	u64 ignored;
+	u64 ignored, arb_val = 0xef8056791234abcd; /* Arbitrary value */
 	int vector;
+	u32 msr;
 
-	for (msr_index = 0; msr_index <= 0xc0012000; msr_index++) {
-		if (msr_index == 0xC0010131 /* MSR_SEV_STATUS */) {
+	for (msr = 0; msr <= 0xc0012000; msr++) {
+		if (msr == 0xC0010131 /* MSR_SEV_STATUS */) {
 			/*
 			 * Per section 15.34.10 "SEV_STATUS MSR" of AMD64 Architecture
 			 * Programmer's Manual volume 2 - System Programming:
@@ -337,34 +336,34 @@ static void test_msr_intercept(struct svm_test *test)
 		 * Test one MSR just before and after each range, but otherwise
 		 * skips gaps between supported MSR ranges.
 		 */
-		if (msr_index == 0x2000 + 1)
-			msr_index = 0xc0000000 - 1;
-		else if (msr_index == 0xc0002000 + 1)
-			msr_index = 0xc0010000 - 1;
+		if (msr == 0x2000 + 1)
+			msr = 0xc0000000 - 1;
+		else if (msr == 0xc0002000 + 1)
+			msr = 0xc0010000 - 1;
 
 		test->scratch = -1;
 
-		vector = rdmsr_safe(msr_index, &ignored);
+		vector = rdmsr_safe(msr, &ignored);
 		if (vector)
-			report_fail("Expected RDMSR(0x%lx) to #VMEXIT, got exception '%u'",
-				    msr_index, vector);
-		else if (test->scratch != msr_index)
-			report_fail("Expected RDMSR(0x%lx) to #VMEXIT, got scratch '%ld",
-				    msr_index, test->scratch);
+			report_fail("Expected RDMSR(0x%x) to #VMEXIT, got exception '%u'",
+				    msr, vector);
+		else if (test->scratch != msr)
+			report_fail("Expected RDMSR(0x%x) to #VMEXIT, got scratch '%ld",
+				    msr, test->scratch);
 
 		/*
 		 * Poor man approach to generate a value that
 		 * seems arbitrary each time around the loop.
 		 */
-		msr_value += (msr_value << 1);
+		arb_val += (arb_val << 1);
 
-		vector = wrmsr_safe(msr_index, msr_value);
+		vector = wrmsr_safe(msr, arb_val);
 		if (vector)
-			report_fail("Expected WRMSR(0x%0lx) to #VMEXIT, got exception '%u'",
-				    msr_index, vector);
-		else if (test->scratch != msr_value)
-			report_fail("Expected WRMSR(0x%lx) to #VMEXIT, got scratch '%ld' (wanted %ld)",
-				    msr_index, test->scratch, msr_value);
+			report_fail("Expected WRMSR(0x%x) to #VMEXIT, got exception '%u'",
+				    msr, vector);
+		else if (test->scratch != arb_val)
+			report_fail("Expected WRMSR(0x%x) to #VMEXIT, got scratch '%ld' (wanted %ld)",
+				    msr, test->scratch, arb_val);
 	}
 
 	test->scratch = -2;
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer
#4 lore raw

PATCH v2 4/8 x86: Expand the suite of bitops to cover all set/clear operations

In reply to Sean Christopherson

Provide atomic and non-atomic APIs for clearing and setting bits, along with "test" versions to return the original value. Don't bother with "change" APIs, as they are highly unlikely to be needed.

Opportunistically move the existing definitions to bitops.h so that common code can access the helpers.

---
 lib/x86/asm/bitops.h | 86 +++++++++++++++++++++++++++++++++++++++++---
 lib/x86/processor.h  | 12 -------
 2 files changed, 81 insertions(+), 17 deletions(-)
lib/x86/asm/bitops.h +81 -5
index 54ec9c42..3ece1b67 100644
@@ -13,12 +13,88 @@
 
 #define HAVE_BUILTIN_FLS 1
 
-static inline void test_and_set_bit(long nr, unsigned long *addr)
+/*
+ * Macros to generate condition code outputs from inline assembly,
+ * The output operand must be type "bool".
+ */
+#ifdef __GCC_ASM_FLAG_OUTPUTS__
+# define CC_SET(c) "\n\t/* output condition code " #c "*/\n"
+# define CC_OUT(c) "=@cc" #c
+#else
+# define CC_SET(c) "\n\tset" #c " %[_cc_" #c "]\n"
+# define CC_OUT(c) [_cc_ ## c] "=qm"
+#endif
+
+static inline void __clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("btr %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline void __set_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("bts %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline bool __test_and_clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("btr %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
+}
+
+static inline bool __test_and_set_bit(int bit, void *__addr)
 {
-	asm volatile("lock; bts %1,%0"
-		     : "+m" (*addr)
-		     : "Ir" (nr)
-		     : "memory");
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("bts %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
+}
+
+static inline void clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("lock; btr %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline void set_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("lock; bts %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline bool test_and_clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("lock; btr %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
+}
+
+static inline bool test_and_set_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("lock; bts %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
 }
 
 #endif
lib/x86/processor.h +0 -12
index a0be04c5..5bc9ef89 100644
@@ -914,18 +914,6 @@ static inline bool is_canonical(u64 addr)
 	return (s64)(addr << shift_amt) >> shift_amt == addr;
 }
 
-static inline void clear_bit(int bit, u8 *addr)
-{
-	__asm__ __volatile__("lock; btr %1, %0"
-			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
-}
-
-static inline void set_bit(int bit, u8 *addr)
-{
-	__asm__ __volatile__("lock; bts %1, %0"
-			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
-}
-
 static inline void flush_tlb(void)
 {
 	ulong cr4;
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Reviewed-by on v2 4/8
#5 lore raw

Re: [kvm-unit-tests PATCH v2 4/8] x86: Expand the suite of bitops to cover all set/clear operations

Quoted 124 lines · Sean Christopherson

Provide atomic and non-atomic APIs for clearing and setting bits, along with "test" versions to return the original value. Don't bother with "change" APIs, as they are highly unlikely to be needed.

Opportunistically move the existing definitions to bitops.h so that common code can access the helpers.

---
 lib/x86/asm/bitops.h | 86 +++++++++++++++++++++++++++++++++++++++++---
 lib/x86/processor.h  | 12 -------
 2 files changed, 81 insertions(+), 17 deletions(-)
lib/x86/asm/bitops.h +81 -5
index 54ec9c42..3ece1b67 100644
@@ -13,12 +13,88 @@
 
 #define HAVE_BUILTIN_FLS 1
 
-static inline void test_and_set_bit(long nr, unsigned long *addr)
+/*
+ * Macros to generate condition code outputs from inline assembly,
+ * The output operand must be type "bool".
+ */
+#ifdef __GCC_ASM_FLAG_OUTPUTS__
+# define CC_SET(c) "\n\t/* output condition code " #c "*/\n"
+# define CC_OUT(c) "=@cc" #c
+#else
+# define CC_SET(c) "\n\tset" #c " %[_cc_" #c "]\n"
+# define CC_OUT(c) [_cc_ ## c] "=qm"
+#endif
+
+static inline void __clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("btr %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline void __set_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("bts %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline bool __test_and_clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("btr %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
+}
+
+static inline bool __test_and_set_bit(int bit, void *__addr)
 {
-	asm volatile("lock; bts %1,%0"
-		     : "+m" (*addr)
-		     : "Ir" (nr)
-		     : "memory");
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("bts %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
+}
+
+static inline void clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("lock; btr %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline void set_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+
+	__asm__ __volatile__("lock; bts %1, %0"
+			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
+}
+
+static inline bool test_and_clear_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("lock; btr %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
+}
+
+static inline bool test_and_set_bit(int bit, void *__addr)
+{
+	unsigned long *addr = __addr;
+	bool v;
+
+	__asm__ __volatile__("lock; bts %2, %1" CC_SET(c)
+			     : CC_OUT(c) (v), "+m" (*addr) : "Ir" (bit));
+	return v;
 }
 
 #endif
lib/x86/processor.h +0 -5
index a0be04c5..5bc9ef89 100644
@@ -914,18 +914,6 @@ static inline bool is_canonical(u64 addr)
 	return (s64)(addr << shift_amt) >> shift_amt == addr;
 }
 
-static inline void clear_bit(int bit, u8 *addr)
-{
-	__asm__ __volatile__("lock; btr %1, %0"
-			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
-}

lib/x86/processor.h static inline bool is_canonical(u64 addr) · line 919

Quoted 10 lines
lib/x86/processor.h +0 -7
-
-static inline void set_bit(int bit, u8 *addr)
-{
-	__asm__ __volatile__("lock; bts %1, %0"
-			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
-}
-
 static inline void flush_tlb(void)
 {
 	ulong cr4;

LGTM.

author reviewer
#6 lore raw

PATCH v2 5/8 x86: nVMX: Use set_bit() instead of test_and_set_bit() when return is ignored

In reply to Sean Christopherson

Use set_bit() in vmx_posted_interrupts_test_worker(), as the usage doesn't actually look at the original value.

No functional change intended.

---
 x86/vmx_tests.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/vmx_tests.c +2 -2
index 2f178227..53706000 100644
@@ -11122,8 +11122,8 @@ static void vmx_posted_interrupts_test_worker(void *data)
 	while (!args->in_guest)
 		pause();
 
-	test_and_set_bit(args->nr, args->pi_desc);
-	test_and_set_bit(256, args->pi_desc);
+	set_bit(args->nr, args->pi_desc);
+	set_bit(256, args->pi_desc);
 	apic_icr_write(PI_VECTOR, args->dest);
 }
 
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
Reviewed-by on v2 5/8
#7 lore raw

Re: [kvm-unit-tests PATCH v2 5/8] x86: nVMX: Use set_bit() instead of test_and_set_bit() when return is ignored

Quoted 15 lines · Sean Christopherson

Use set_bit() in vmx_posted_interrupts_test_worker(), as the usage doesn't actually look at the original value.

No functional change intended.

---
 x86/vmx_tests.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/vmx_tests.c +0 -0
index 2f178227..53706000 100644

x86/vmx_tests.c static void vmx_posted_interrupts_test_worker(void *data) · line 11129

Quoted 10 lines
x86/vmx_tests.c +2 -2
@@ -11122,8 +11122,8 @@ static void vmx_posted_interrupts_test_worker(void *data)
 	while (!args->in_guest)
 		pause();
 
-	test_and_set_bit(args->nr, args->pi_desc);
-	test_and_set_bit(256, args->pi_desc);
+	set_bit(args->nr, args->pi_desc);
+	set_bit(256, args->pi_desc);
 	apic_icr_write(PI_VECTOR, args->dest);
 }
 
author reviewer
#8 lore raw

PATCH v2 6/8 x86: nSVM: Set MSRPM bit on-demand when testing interception

In reply to Sean Christopherson

Set the to-be-tested {RD,WR}MSR's associated MSRPM bit on-demand when verifying KVM's emulation of MSR interception. Setting all bits in the bitmap creates a massive blind spot in the test coverage, e.g. KVM could completely ignore the bitmap, and the test would still pass.

Run the test with all bits initially clear, and all bits initially set, mostly in prepartion for adding coverage of MSR passthrough in a future commit.

---
 x86/svm_tests.c | 104 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 8 deletions(-)
x86/svm_tests.c +96 -8
index 5d06249f..ef8c2cec 100644
@@ -312,12 +312,41 @@ static void prepare_msr_intercept(struct svm_test *test)
 {
 	default_prepare(test);
 	vmcb->control.intercept |= (1ULL << INTERCEPT_MSR_PROT);
-	memset(msr_bitmap, 0xff, MSR_BITMAP_SIZE);
+
+	memset(msr_bitmap, 0, MSR_BITMAP_SIZE);
+}
+
+#define SVM_MSRPM_BYTES_PER_RANGE 2048
+#define SVM_BITS_PER_MSR 2
+#define SVM_MSRS_PER_BYTE 4
+#define SVM_MSRS_PER_RANGE 8192
+#define SVM_MSRPM_OFFSET_MASK (SVM_MSRS_PER_RANGE - 1)
+
+static int get_msrpm_bit_nr(u32 msr)
+{
+	int range_nr;
+
+	switch (msr & ~SVM_MSRPM_OFFSET_MASK) {
+	case 0:
+		range_nr = 0;
+		break;
+	case 0xc0000000:
+		range_nr = 1;
+		break;
+	case 0xc0010000:
+		range_nr = 2;
+		break;
+	default:
+		return - 1;
+	}
+
+	return range_nr * SVM_MSRPM_BYTES_PER_RANGE * BITS_PER_BYTE +
+	       (msr & SVM_MSRPM_OFFSET_MASK) * SVM_BITS_PER_MSR;
 }
 
-static void test_msr_intercept(struct svm_test *test)
+static void __test_msr_intercept(struct svm_test *test)
 {
-	u64 ignored, arb_val = 0xef8056791234abcd; /* Arbitrary value */
+	u64 val, arb_val = 0xef8056791234abcd; /* Arbitrary value */
 	int vector;
 	u32 msr;
 
@@ -341,9 +370,12 @@ static void test_msr_intercept(struct svm_test *test)
 		else if (msr == 0xc0002000 + 1)
 			msr = 0xc0010000 - 1;
 
+		test->scratch = msr;
+		vmmcall();
+
 		test->scratch = -1;
 
-		vector = rdmsr_safe(msr, &ignored);
+		vector = rdmsr_safe(msr, &val);
 		if (vector)
 			report_fail("Expected RDMSR(0x%x) to #VMEXIT, got exception '%u'",
 				    msr, vector);
@@ -351,12 +383,17 @@ static void test_msr_intercept(struct svm_test *test)
 			report_fail("Expected RDMSR(0x%x) to #VMEXIT, got scratch '%ld",
 				    msr, test->scratch);
 
+		test->scratch = BIT_ULL(32) | msr;
+		vmmcall();
+
 		/*
 		 * Poor man approach to generate a value that
 		 * seems arbitrary each time around the loop.
 		 */
 		arb_val += (arb_val << 1);
 
+		test->scratch = -1;
+
 		vector = wrmsr_safe(msr, arb_val);
 		if (vector)
 			report_fail("Expected WRMSR(0x%x) to #VMEXIT, got exception '%u'",
@@ -364,17 +401,69 @@ static void test_msr_intercept(struct svm_test *test)
 		else if (test->scratch != arb_val)
 			report_fail("Expected WRMSR(0x%x) to #VMEXIT, got scratch '%ld' (wanted %ld)",
 				    msr, test->scratch, arb_val);
+
+		test->scratch = BIT_ULL(33) | msr;
+		vmmcall();
 	}
+}
+
+static void test_msr_intercept(struct svm_test *test)
+{
+	__test_msr_intercept(test);
 
 	test->scratch = -2;
+	vmmcall();
+
+	__test_msr_intercept(test);
+
+	test->scratch = -3;
+}
+
+static void restore_msrpm_bit(int bit_nr, bool set)
+{
+	if (set)
+		__set_bit(bit_nr, msr_bitmap);
+	else
+		__clear_bit(bit_nr, msr_bitmap);
 }
 
 static bool msr_intercept_finished(struct svm_test *test)
 {
 	u32 exit_code = vmcb->control.exit_code;
+	bool all_set = false;
+	int bit_nr;
 
-	if (exit_code == SVM_EXIT_VMMCALL)
-		return true;
+	if (exit_code == SVM_EXIT_VMMCALL) {
+		vmcb->save.rip += 3;
+
+		if (test->scratch == -3)
+			return true;
+
+		if (test->scratch == -2) {
+			all_set = true;
+			memset(msr_bitmap, 0xff, MSR_BITMAP_SIZE);
+			return false;
+		}
+	
+		bit_nr = get_msrpm_bit_nr(test->scratch & -1u);
+		if (bit_nr < 0)
+			return false;
+
+		switch (test->scratch >> 32) {
+		case 0:
+			__set_bit(bit_nr, msr_bitmap);
+			return false;
+		case 1:
+			restore_msrpm_bit(bit_nr, all_set);
+			__set_bit(bit_nr + 1, msr_bitmap);
+			return false;
+		case 2:
+			restore_msrpm_bit(bit_nr + 1, all_set);
+			return false;
+		default:
+			return true;
+		}
+	}
 
 	if (exit_code != SVM_EXIT_MSR) {
 		report_fail("Wanted MSR VM-Exit, got reason 0x%x", exit_code);
@@ -402,8 +491,7 @@ static bool msr_intercept_finished(struct svm_test *test)
 
 static bool check_msr_intercept(struct svm_test *test)
 {
-	memset(msr_bitmap, 0, MSR_BITMAP_SIZE);
-	return (test->scratch == -2);
+	return (test->scratch == -3);
 }
 
 static void prepare_mode_switch(struct svm_test *test)
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer
#9 lore raw

PATCH v2 7/8 x86: nSVM: Verify disabling {RD,WR}MSR interception behaves as expected

In reply to Sean Christopherson

Extend nSVM's MSRPM test to verify that disabling MSR interception also behaves as expected, i.e. that {RD,WR}MSR don't exit, and doesn't set the VM on fire.

Disable x2APIC (if necessary) when writing x2APIC MSRs so that writes from L2 #GP instead of doing random things, e.g. sending IPIs and crashing the guest.

---
 x86/svm_tests.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)
x86/svm_tests.c +54 -1
index ef8c2cec..a89a234e 100644
@@ -308,12 +308,16 @@ static bool check_next_rip(struct svm_test *test)
 
 extern u8 *msr_bitmap;
 
+static bool is_x2apic;
+
 static void prepare_msr_intercept(struct svm_test *test)
 {
 	default_prepare(test);
 	vmcb->control.intercept |= (1ULL << INTERCEPT_MSR_PROT);
 
 	memset(msr_bitmap, 0, MSR_BITMAP_SIZE);
+
+	is_x2apic = is_x2apic_enabled();
 }
 
 #define SVM_MSRPM_BYTES_PER_RANGE 2048
@@ -404,6 +408,37 @@ static void __test_msr_intercept(struct svm_test *test)
 
 		test->scratch = BIT_ULL(33) | msr;
 		vmmcall();
+
+		if (get_msrpm_bit_nr(msr) < 0) {
+			report(msr == 0x2000 ||
+			       msr == 0xc0000000 - 1 || msr == 0xc0002000 ||
+			       msr == 0xc0010000 - 1 || msr == 0xc0012000,
+			       "MSR 0x%x not covered by an MSRPM range", msr);
+			continue;
+		}
+
+		/*
+		 * Verify that disabling interception for MSRs within an MSRPM
+		 * range behaves as expected.  Simply eat exceptions, the goal
+		 * is to verify interception, not MSR emulation/virtualization.
+		 */
+		test->scratch = -1;
+		(void)rdmsr_safe(msr, &val);
+		if (test->scratch != -1)
+			report_fail("RDMSR 0x%x, Wanted -1 (no intercept), got 0x%lx",
+				    msr, test->scratch);
+
+		test->scratch = BIT_ULL(34) | msr;
+		vmmcall();
+
+		test->scratch = -1;
+		(void)wrmsr_safe(msr, val);
+		if (test->scratch != -1)
+			report_fail("WRMSR 0x%x, Wanted -1 (no intercept), got 0x%lx",
+				    msr, test->scratch);
+
+		test->scratch = BIT_ULL(35) | msr;
+		vmmcall();
 	}
 }
 
@@ -434,6 +469,8 @@ static bool msr_intercept_finished(struct svm_test *test)
 	int bit_nr;
 
 	if (exit_code == SVM_EXIT_VMMCALL) {
+		u32 msr = test->scratch & -1u;
+
 		vmcb->save.rip += 3;
 
 		if (test->scratch == -3)
@@ -445,7 +482,7 @@ static bool msr_intercept_finished(struct svm_test *test)
 			return false;
 		}
 	
-		bit_nr = get_msrpm_bit_nr(test->scratch & -1u);
+		bit_nr = get_msrpm_bit_nr(msr);
 		if (bit_nr < 0)
 			return false;
 
@@ -459,6 +496,22 @@ static bool msr_intercept_finished(struct svm_test *test)
 			return false;
 		case 2:
 			restore_msrpm_bit(bit_nr + 1, all_set);
+			__clear_bit(bit_nr, msr_bitmap);
+			return false;
+		case 4:
+			restore_msrpm_bit(bit_nr, all_set);
+			__clear_bit(bit_nr + 1, msr_bitmap);
+			/*
+			 * Disable x2APIC so that WRMSR faults instead of doing
+			 * random things, e.g. sending IPIs.
+			 */
+			if (is_x2apic && msr >= 0x800 && msr <= 0x8ff)
+				reset_apic();
+			return false;
+		case 8:
+			restore_msrpm_bit(bit_nr + 1, all_set);
+			if (is_x2apic && msr >= 0x800 && msr <= 0x8ff)
+				enable_x2apic();
 			return false;
 		default:
 			return true;
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer
#10 lore raw

PATCH v2 8/8 x86: nSVM: Verify L1 and L2 see same MSR value when interception is disabled

In reply to Sean Christopherson

Expand the MSR interception test to verify that L1 and L2 get the same value on RDMSR when interception of the MSR is disabled in vmcb12's MSRPM.

Skip TSC for obvious reasons.

---
 x86/svm_tests.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
x86/svm_tests.c +12 -1
index a89a234e..80d5aeb1 100644
@@ -350,7 +350,7 @@ static int get_msrpm_bit_nr(u32 msr)
 
 static void __test_msr_intercept(struct svm_test *test)
 {
-	u64 val, arb_val = 0xef8056791234abcd; /* Arbitrary value */
+	u64 val, exp, arb_val = 0xef8056791234abcd; /* Arbitrary value */
 	int vector;
 	u32 msr;
 
@@ -417,6 +417,8 @@ static void __test_msr_intercept(struct svm_test *test)
 			continue;
 		}
 
+		exp = test->scratch;
+
 		/*
 		 * Verify that disabling interception for MSRs within an MSRPM
 		 * range behaves as expected.  Simply eat exceptions, the goal
@@ -428,6 +430,14 @@ static void __test_msr_intercept(struct svm_test *test)
 			report_fail("RDMSR 0x%x, Wanted -1 (no intercept), got 0x%lx",
 				    msr, test->scratch);
 
+		/*
+		 * Verify L1 and L2 see the same MSR value.  Skip TSC to avoid
+		 * false failures, as it's constantly changing.
+		 */
+		if (val != exp && msr != MSR_IA32_TSC)
+			report_fail("RDMSR 0x%x, wanted val '0%lx', got val '0x%lx'",
+				    msr, exp, val);
+
 		test->scratch = BIT_ULL(34) | msr;
 		vmmcall();
 
@@ -497,6 +507,7 @@ static bool msr_intercept_finished(struct svm_test *test)
 		case 2:
 			restore_msrpm_bit(bit_nr + 1, all_set);
 			__clear_bit(bit_nr, msr_bitmap);
+			(void)rdmsr_safe(msr, &test->scratch);
 			return false;
 		case 4:
 			restore_msrpm_bit(bit_nr, all_set);
Signature · 1 line
2.50.0.rc0.604.gd4ff7b7c86-goog
author reviewer Applied
#11 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

Fix the nSVM MSR interception test to actually detect failures, and expand its coverage to test:

- Out-of-range MSRs
- With all other bits in the MSPRM clear
- {RD,WR}MSR with interception *disabled*
- That L1 and L2 see the same value for RDMSR when interception is disabled

[...]

Applied to kvm-x86 next, thanks!

[1/8] x86: nSVM: Actually report missed MSR intercepts as failures

      https://github.com/kvm-x86/kvm-unit-tests/commit/27eeac46fac1
[2/8] x86: nSVM: Test MSRs just outside the ranges of the MSR Permissions Map
      https://github.com/kvm-x86/kvm-unit-tests/commit/573d62c697fe
[3/8] x86: nSVM: Clean up variable types and names in test_msr_intercept()
      https://github.com/kvm-x86/kvm-unit-tests/commit/165c839cc035
[4/8] x86: Expand the suite of bitops to cover all set/clear operations
      https://github.com/kvm-x86/kvm-unit-tests/commit/9fb56fa18056
[5/8] x86: nVMX: Use set_bit() instead of test_and_set_bit() when return is ignored
      https://github.com/kvm-x86/kvm-unit-tests/commit/a594bc6120d5
[6/8] x86: nSVM: Set MSRPM bit on-demand when testing interception
      https://github.com/kvm-x86/kvm-unit-tests/commit/1c7e7d1c7b57
[7/8] x86: nSVM: Verify disabling {RD,WR}MSR interception behaves as expected
      https://github.com/kvm-x86/kvm-unit-tests/commit/1e2a6424a1d2
[8/8] x86: nSVM: Verify L1 and L2 see same MSR value when interception is disabled
      https://github.com/kvm-x86/kvm-unit-tests/commit/bbafc5776737

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