Skip to contentSkip to conversation

nVMX: Fix VMREAD/VMWRITE #PF tests

PATCH New Sean Christopherson 1 participant 4 messages Sep 30, 2022 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

cover nVMX: Fix VMREAD/VMWRITE #PF tests

Thanks to a recent gcc update, the VMREAD/VMWRITE #PF tests are now failing due to gcc-12 optimizing away a non-volatile flag that it thinks can't possibly change. Avoid the volatile shenanigans entirely by switching to ASM_TRY(), and dedup the two tests.

Sean Christopherson (3):
x86: Handle all known exceptions with ASM_TRY()
nVMX: Use ASM_TRY() for VMREAD and VMWRITE page fault tests
nVMX: Dedup the bulk of the VMREAD/VMWRITE #PF tests

 lib/x86/desc.c |  10 ++--
 x86/vmx.c      | 158 ++++++++++++++-----------------------------------
 2 files changed, 49 insertions(+), 119 deletions(-)

base-commit: d8a4f9e5e8d69d4ef257b40d6cd666bd2f63494e

Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author reviewer
#1 lore raw

PATCH 1/3 x86: Handle all known exceptions with ASM_TRY()

Install the ASM_TRY() exception handler for all known exception vectors so that ASM_TRY() can be used for other exceptions, e.g. #PF. ASM_TRY() might not Just Work in all cases, but there's no good reason to limit usage to just #DE, #UD, and #GP.

---
 lib/x86/desc.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/x86/desc.c +5 -5
index a20be92..b293ae4 100644
@@ -301,12 +301,12 @@ void setup_idt(void)
 
 	idt_initialized = true;
 	for (i = 0; i < 32; i++) {
-		if (idt_handlers[i])
-			set_idt_entry(i, idt_handlers[i], 0);
+		if (!idt_handlers[i])
+			continue;
+
+                set_idt_entry(i, idt_handlers[i], 0);
+                handle_exception(i, check_exception_table);
 	}
-	handle_exception(0, check_exception_table);
-	handle_exception(6, check_exception_table);
-	handle_exception(13, check_exception_table);
 }
 
 void load_idt(void)
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author reviewer
#2 lore raw

PATCH 2/3 nVMX: Use ASM_TRY() for VMREAD and VMWRITE page fault tests

In reply to Sean Christopherson

Use ASM_TRY() in the VMREAD and VMWRITE page fault tests to fix a bug where gcc-12 completely optimizes out handler_called. Because the flag isn't tagged volatile and the compiler is unaware that an exception may occur, gcc-12 thinks the value can only ever be 0.

Note, exception fixup effectively performs an exact RIP check, and using LAHF to save RFLAGS drops the fixed RFLAGS bit.

Opportunistically drop the 'noinline' as removing the global label makes the functions safe to inline/duplicate.

---
 x86/vmx.c | 135 ++++++++++++++++++++----------------------------------
 1 file changed, 49 insertions(+), 86 deletions(-)
x86/vmx.c +49 -86
index a13f2c9..0ae134d 100644
@@ -387,25 +387,7 @@ static void test_vmwrite_vmread(void)
 	free_page(vmcs);
 }
 
-ulong finish_fault;
-u8 sentinel;
-bool handler_called;
-
-static void pf_handler(struct ex_regs *regs)
-{
-	/*
-	 * check that RIP was not improperly advanced and that the
-	 * flags value was preserved.
-	 */
-	report(regs->rip < finish_fault, "RIP has not been advanced!");
-	report(((u8)regs->rflags == ((sentinel | 2) & 0xd7)),
-	       "The low byte of RFLAGS was preserved!");
-	regs->rip = finish_fault;
-	handler_called = true;
-
-}
-
-static void prep_flags_test_env(void **vpage, struct vmcs **vmcs, handler *old)
+static void prep_flags_test_env(void **vpage, struct vmcs **vmcs)
 {
 	/*
 	 * get an unbacked address that will cause a #PF
@@ -421,107 +403,88 @@ static void prep_flags_test_env(void **vpage, struct vmcs **vmcs, handler *old)
 	(*vmcs)->hdr.revision_id = basic.revision;
 	assert(!vmcs_clear(*vmcs));
 	assert(!make_vmcs_current(*vmcs));
-
-	*old = handle_exception(PF_VECTOR, &pf_handler);
 }
 
-static noinline void test_read_sentinel(void)
+static void test_read_sentinel(u8 sentinel)
 {
-	void *vpage;
+	unsigned long flags = sentinel;
+	unsigned int vector;
 	struct vmcs *vmcs;
-	handler old;
+	void *vpage;
 
-	prep_flags_test_env(&vpage, &vmcs, &old);
+	prep_flags_test_env(&vpage, &vmcs);
 
 	/*
-	 * set the proper label
+	 * Execute VMREAD with a not-PRESENT memory operand, and verify a #PF
+	 * occurred and RFLAGS were not modified.
 	 */
-	extern char finish_read_fault;
+	asm volatile ("sahf\n\t"
+		      ASM_TRY("1f")
+		      "vmread %[enc], %[val]\n\t"
+		      "1: lahf"
+		      : [val] "=m" (*(u64 *)vpage),
+			[flags] "+a" (flags)
+		      : [enc] "r" ((u64)GUEST_SEL_SS)
+		      : "cc");
 
-	finish_fault = (ulong)&finish_read_fault;
+	vector = exception_vector();
+	report(vector == PF_VECTOR,
+	       "Expected #PF on VMREAD, got exception 0x%x", vector);
 
-	/*
-	 * execute the vmread instruction that will cause a #PF
-	 */
-	handler_called = false;
-	asm volatile ("movb %[byte], %%ah\n\t"
-		      "sahf\n\t"
-		      "vmread %[enc], %[val]; finish_read_fault:"
-		      : [val] "=m" (*(u64 *)vpage)
-		      : [byte] "Krm" (sentinel),
-		      [enc] "r" ((u64)GUEST_SEL_SS)
-		      : "cc", "ah");
-	report(handler_called, "The #PF handler was invoked");
-
-	/*
-	 * restore the old #PF handler
-	 */
-	handle_exception(PF_VECTOR, old);
+	report((u8)flags == sentinel,
+	       "Expected RFLAGS 0x%x, got 0x%x", sentinel, (u8)flags);
 }
 
 static void test_vmread_flags_touch(void)
 {
 	/*
-	 * set up the sentinel value in the flags register. we
-	 * choose these two values because they candy-stripe
-	 * the 5 flags that sahf sets.
+	 * Test with two values to candy-stripe the 5 flags stored/loaded by
+	 * SAHF/LAHF.
 	 */
-	sentinel = 0x91;
-	test_read_sentinel();
-
-	sentinel = 0x45;
-	test_read_sentinel();
+	test_read_sentinel(0x91);
+	test_read_sentinel(0x45);
 }
 
-static noinline void test_write_sentinel(void)
+static void test_write_sentinel(u8 sentinel)
 {
-	void *vpage;
+	unsigned long flags = sentinel;
+	unsigned int vector;
 	struct vmcs *vmcs;
-	handler old;
+	void *vpage;
 
-	prep_flags_test_env(&vpage, &vmcs, &old);
+	prep_flags_test_env(&vpage, &vmcs);
 
 	/*
-	 * set the proper label
+	 * Execute VMWRITE with a not-PRESENT memory operand, and verify a #PF
+	 * occurred and RFLAGS were not modified.
 	 */
-	extern char finish_write_fault;
+	asm volatile ("sahf\n\t"
+		      ASM_TRY("1f")
+		      "vmwrite %[val], %[enc]\n\t"
+		      "1: lahf"
+		      : [val] "=m" (*(u64 *)vpage),
+			[flags] "+a" (flags)
+		      : [enc] "r" ((u64)GUEST_SEL_SS)
+		      : "cc");
 
-	finish_fault = (ulong)&finish_write_fault;
+	vector = exception_vector();
+	report(vector == PF_VECTOR,
+	       "Expected #PF on VMWRITE, got exception '0x%x'\n", vector);
 
-	/*
-	 * execute the vmwrite instruction that will cause a #PF
-	 */
-	handler_called = false;
-	asm volatile ("movb %[byte], %%ah\n\t"
-		      "sahf\n\t"
-		      "vmwrite %[val], %[enc]; finish_write_fault:"
-		      : [val] "=m" (*(u64 *)vpage)
-		      : [byte] "Krm" (sentinel),
-		      [enc] "r" ((u64)GUEST_SEL_SS)
-		      : "cc", "ah");
-	report(handler_called, "The #PF handler was invoked");
-
-	/*
-	 * restore the old #PF handler
-	 */
-	handle_exception(PF_VECTOR, old);
+	report((u8)flags == sentinel,
+	       "Expected RFLAGS 0x%x, got 0x%x", sentinel, (u8)flags);
 }
 
 static void test_vmwrite_flags_touch(void)
 {
 	/*
-	 * set up the sentinel value in the flags register. we
-	 * choose these two values because they candy-stripe
-	 * the 5 flags that sahf sets.
+	 * Test with two values to candy-stripe the 5 flags stored/loaded by
+	 * SAHF/LAHF.
 	 */
-	sentinel = 0x91;
-	test_write_sentinel();
-
-	sentinel = 0x45;
-	test_write_sentinel();
+	test_write_sentinel(0x91);
+	test_write_sentinel(0x45);
 }
 
-
 static void test_vmcs_high(void)
 {
 	struct vmcs *vmcs = alloc_page();
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author reviewer
#3 lore raw

PATCH 3/3 nVMX: Dedup the bulk of the VMREAD/VMWRITE #PF tests

In reply to Sean Christopherson

Dedup most of the VMREAD/VMWRITE #PF/flags tests, they are identical except for literally VMREAD vs. VMRITE. Don't bother adding a macro to emit VMREAD vs. VMWRITE, the macro required isn't any better than copy and paste.

---
 x86/vmx.c | 119 ++++++++++++++++++++----------------------------------
 1 file changed, 43 insertions(+), 76 deletions(-)
x86/vmx.c +43 -76
index 0ae134d..e28507c 100644
@@ -387,102 +387,69 @@ static void test_vmwrite_vmread(void)
 	free_page(vmcs);
 }
 
-static void prep_flags_test_env(void **vpage, struct vmcs **vmcs)
-{
-	/*
-	 * get an unbacked address that will cause a #PF
-	 */
-	*vpage = alloc_vpage();
-
-	/*
-	 * set up VMCS so we have something to read from
-	 */
-	*vmcs = alloc_page();
-
-	memset(*vmcs, 0, PAGE_SIZE);
-	(*vmcs)->hdr.revision_id = basic.revision;
-	assert(!vmcs_clear(*vmcs));
-	assert(!make_vmcs_current(*vmcs));
-}
-
-static void test_read_sentinel(u8 sentinel)
+static void __test_vmread_vmwrite_pf(bool vmread, u64 *val, u8 sentinel)
 {
 	unsigned long flags = sentinel;
 	unsigned int vector;
-	struct vmcs *vmcs;
-	void *vpage;
-
-	prep_flags_test_env(&vpage, &vmcs);
 
 	/*
-	 * Execute VMREAD with a not-PRESENT memory operand, and verify a #PF
-	 * occurred and RFLAGS were not modified.
+	 * Execute VMREAD/VMWRITE with a not-PRESENT memory operand, and verify
+	 * a #PF occurred and RFLAGS were not modified.
 	 */
-	asm volatile ("sahf\n\t"
-		      ASM_TRY("1f")
-		      "vmread %[enc], %[val]\n\t"
-		      "1: lahf"
-		      : [val] "=m" (*(u64 *)vpage),
-			[flags] "+a" (flags)
-		      : [enc] "r" ((u64)GUEST_SEL_SS)
-		      : "cc");
+	if (vmread)
+		asm volatile ("sahf\n\t"
+			      ASM_TRY("1f")
+			      "vmread %[enc], %[val]\n\t"
+			      "1: lahf"
+			      : [val] "=m" (*val),
+			        [flags] "+a" (flags)
+			      : [enc] "r" ((u64)GUEST_SEL_SS)
+			      : "cc");
+	else
+		asm volatile ("sahf\n\t"
+			      ASM_TRY("1f")
+			      "vmwrite %[val], %[enc]\n\t"
+			      "1: lahf"
+			      : [val] "=m" (*val),
+			        [flags] "+a" (flags)
+			      : [enc] "r" ((u64)GUEST_SEL_SS)
+			      : "cc");
 
 	vector = exception_vector();
 	report(vector == PF_VECTOR,
-	       "Expected #PF on VMREAD, got exception 0x%x", vector);
+	       "Expected #PF on %s, got exception '0x%x'\n",
+	       vmread ? "VMREAD" : "VMWRITE", vector);
 
 	report((u8)flags == sentinel,
 	       "Expected RFLAGS 0x%x, got 0x%x", sentinel, (u8)flags);
 }
 
+static void test_vmread_vmwrite_pf(bool vmread)
+{
+	struct vmcs *vmcs = alloc_page();
+	void *vpage = alloc_vpage();
+
+	memset(vmcs, 0, PAGE_SIZE);
+	vmcs->hdr.revision_id = basic.revision;
+	assert(!vmcs_clear(vmcs));
+	assert(!make_vmcs_current(vmcs));
+
+	/*
+	 * Test with two values to candy-stripe the 5 flags stored/loaded by
+	 * SAHF/LAHF.
+	 */
+	__test_vmread_vmwrite_pf(vmread, vpage, 0x91);
+	__test_vmread_vmwrite_pf(vmread, vpage, 0x45);
+}
+
 static void test_vmread_flags_touch(void)
 {
-	/*
-	 * Test with two values to candy-stripe the 5 flags stored/loaded by
-	 * SAHF/LAHF.
-	 */
-	test_read_sentinel(0x91);
-	test_read_sentinel(0x45);
-}
-
-static void test_write_sentinel(u8 sentinel)
-{
-	unsigned long flags = sentinel;
-	unsigned int vector;
-	struct vmcs *vmcs;
-	void *vpage;
-
-	prep_flags_test_env(&vpage, &vmcs);
-
-	/*
-	 * Execute VMWRITE with a not-PRESENT memory operand, and verify a #PF
-	 * occurred and RFLAGS were not modified.
-	 */
-	asm volatile ("sahf\n\t"
-		      ASM_TRY("1f")
-		      "vmwrite %[val], %[enc]\n\t"
-		      "1: lahf"
-		      : [val] "=m" (*(u64 *)vpage),
-			[flags] "+a" (flags)
-		      : [enc] "r" ((u64)GUEST_SEL_SS)
-		      : "cc");
-
-	vector = exception_vector();
-	report(vector == PF_VECTOR,
-	       "Expected #PF on VMWRITE, got exception '0x%x'\n", vector);
-
-	report((u8)flags == sentinel,
-	       "Expected RFLAGS 0x%x, got 0x%x", sentinel, (u8)flags);
+	test_vmread_vmwrite_pf(true);
 }
 
 static void test_vmwrite_flags_touch(void)
 {
-	/*
-	 * Test with two values to candy-stripe the 5 flags stored/loaded by
-	 * SAHF/LAHF.
-	 */
-	test_write_sentinel(0x91);
-	test_write_sentinel(0x45);
+	test_vmread_vmwrite_pf(false);
 }
 
 static void test_vmcs_high(void)
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog