Skip to contentSkip to conversation

x86/emulator: Add testcases for ENTER + emulated MMIO

PATCH Applied Sean Christopherson 1 participant 2 messages May 14 – May 27 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 x86/emulator: Add testcases for ENTER + emulated MMIO

Add testcases for stack pushes to emulated MMIO via the ENTER instruction to validate a pile of KVM changes needed to fix a use-after-free due to KVM referencing kernel stack data across a userspace exit. Test that ENTER is handled correctly if it splits non-MMIO => MMIO, MMIO => non-MMIO, and MMIO => MMIO pages.

Opportunistically add a comment explaining what the collection of pages are used for in the emulator test.

---
 x86/emulator.c   | 66 ++++++++++++++++++++++++++++--------------------
 x86/emulator64.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+), 28 deletions(-)
x86/emulator.c +38 -28
index 102e5efc..6d945d8a 100644
@@ -790,8 +790,7 @@ static void test_mov_pop_ss_code_db(void)
 
 int main(void)
 {
-	void *mem;
-	void *cross_mem;
+	void *base, *mmio, *split;
 
 	if (!is_fep_available)
 		report_skip("Skipping tests that require forced emulation, "
@@ -799,45 +798,56 @@ int main(void)
 
 	setup_vm();
 
-	mem = alloc_vpages(2);
-	install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem);
-	// install the page twice to test cross-page mmio
-	install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem + 4096);
-	cross_mem = vmap(virt_to_phys(alloc_pages(2)), 2 * PAGE_SIZE);
+	/*
+	 * Allocate five pages in the virtual address space, with pages, 0, 3,
+	 * and 4 pointing at normal memory, and pages 1 and 2 pointing at an
+	 * emulated MMIO range.  This combination of pages allows testing basic
+	 * emulated MMIO handling, as well as a variety of multi-page and page-
+	 * split scenarios.
+	 */
+	base = alloc_vpages(5);
+	install_page((void *)read_cr3(), virt_to_phys(alloc_pages(1)), base);
+	install_page((void *)read_cr3(), IORAM_BASE_PHYS, base + PAGE_SIZE);
+	install_page((void *)read_cr3(), IORAM_BASE_PHYS, base + 2 * PAGE_SIZE);
+	install_page((void *)read_cr3(), virt_to_phys(alloc_pages(1)), base + 3 * PAGE_SIZE);
+	install_page((void *)read_cr3(), virt_to_phys(alloc_pages(1)), base + 4 * PAGE_SIZE);
 
-	test_mov(mem);
-	test_simplealu(mem);
-	test_cmps(mem);
-	test_scas(mem);
-	test_smsw(mem);
+	mmio = base + PAGE_SIZE;
+	split = base + 3 * PAGE_SIZE;
+
+	test_mov(mmio);
+	test_simplealu(mmio);
+	test_cmps(mmio);
+	test_scas(mmio);
+	test_smsw(mmio);
 	test_lmsw();
 	test_stringio();
-	test_incdecnotneg(mem);
-	test_btc(mem);
-	test_bsfbsr(mem);
-	test_imul(mem);
-	test_sse(mem);
-	test_sse_exceptions(cross_mem);
-	test_shld_shrd(mem);
-	//test_lgdt_lidt(mem);
-	//test_lldt(mem);
-	test_ltr(mem);
+	test_incdecnotneg(mmio);
+	test_btc(mmio);
+	test_bsfbsr(mmio);
+	test_imul(mmio);
+	test_sse(mmio);
+	test_sse_exceptions(split);
+	test_shld_shrd(mmio);
+	//test_lgdt_lidt(mmio);
+	//test_lldt(mmio);
+	test_ltr(mmio);
 
 	if (is_fep_available) {
-		test_smsw_reg(mem);
-		test_nop(mem);
-		test_mov_dr(mem);
+		test_smsw_reg(mmio);
+		test_nop(mmio);
+		test_mov_dr(mmio);
 		test_illegal_lea();
 	}
 
-	test_crosspage_mmio(mem);
+	test_crosspage_mmio(mmio);
 
-	test_string_io_mmio(mem);
+	test_string_io_mmio(mmio);
 	test_illegal_movbe();
 	test_mov_pop_ss_code_db();
 
 #ifdef __x86_64__
-	test_emulator_64(mem);
+	test_emulator_64(mmio);
 #endif
 	return report_summary();
 }
x86/emulator64.c +64 -0

70 lines not on this page · raw message

base-commit: 9eb6c57313060d34f7e5b2ac6f90bb5873bbe2ff

Signature · 1 line
2.54.0.563.g4f69b47b94-goog
author reviewer Applied
#1 lore raw
Quoted 10 lines · Sean Christopherson

Add testcases for stack pushes to emulated MMIO via the ENTER instruction to validate a pile of KVM changes needed to fix a use-after-free due to KVM referencing kernel stack data across a userspace exit. Test that ENTER is handled correctly if it splits non-MMIO => MMIO, MMIO => non-MMIO, and MMIO => MMIO pages.

Opportunistically add a comment explaining what the collection of pages are used for in the emulator test.

[...]

Applied to kvm-x86 next, thanks!

[1/1] x86/emulator: Add testcases for ENTER + emulated MMIO

      https://github.com/kvm-x86/kvm-unit-tests/commit/20693e433cc1

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