Skip to contentSkip to conversation

Crosspage mmio unit test

PATCH New Avi Kivity 1 participant 3 messages Apr 18, 2012 KVM

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Crosspage mmio unit test

Test that MMIO that crosses a page boundary is handled correctly.

Avi Kivity (2):
vm: add alloc_vpages()
emulator: test cross-page mmio

 lib/x86/vm.c   |    9 +++++++--
 lib/x86/vm.h   |    1 +
 x86/emulator.c |   20 +++++++++++++++++++-
 3 files changed, 27 insertions(+), 3 deletions(-)
Signature · 1 line
1.7.10
author
#1 lore raw

PATCH 1/2 vm: add alloc_vpages()

Allows allocating contiguous virtual address space.

---
 lib/x86/vm.c |    9 +++++++--
 lib/x86/vm.h |    1 +
 2 files changed, 8 insertions(+), 2 deletions(-)
lib/x86/vm.c +7 -2
index abbb0c9..550ec9b 100644
@@ -249,8 +249,13 @@ void *vmap(unsigned long long phys, unsigned long size)
     return mem;
 }
 
-void *alloc_vpage(void)
+void *alloc_vpages(ulong nr)
 {
-	vfree_top -= PAGE_SIZE;
+	vfree_top -= PAGE_SIZE * nr;
 	return vfree_top;
 }
+
+void *alloc_vpage(void)
+{
+    return alloc_vpages(1);
+}
lib/x86/vm.h +1 -0
index bf8fd52..71ab4a8 100644
@@ -21,6 +21,7 @@ void *vmalloc(unsigned long size);
 void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
+void *alloc_vpages(ulong nr);
 
 void install_pte(unsigned long *cr3,
                         int pte_level,
Signature · 1 line
1.7.10
author
#2 lore raw

PATCH 2/2 emulator: test cross-page mmio

In reply to Avi Kivity
---
 x86/emulator.c |   20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
x86/emulator.c +19 -1
index 33820f4..969944a 100644
@@ -694,6 +694,19 @@ static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page,
     handle_exception(MF_VECTOR, 0);
 }
 
+static void test_crosspage_mmio(volatile uint8_t *mem)
+{
+    volatile uint16_t w, *pw;
+
+    pw = (volatile uint16_t *)&mem[4095];
+    mem[4095] = 0x99;
+    mem[4096] = 0x77;
+    asm volatile("mov %1, %0" : "=r"(w) : "m"(*pw) : "memory");
+    report("cross-page mmio read", w == 0x7799);
+    asm volatile("mov %1, %0" : "=m"(*pw) : "r"((uint16_t)0x88aa));
+    report("cross-page mmio write", mem[4095] == 0xaa && mem[4096] == 0x88);
+}
+
 int main()
 {
 	void *mem;
@@ -703,7 +716,10 @@ int main()
 
 	setup_vm();
 	setup_idt();
-	mem = vmap(IORAM_BASE_PHYS, IORAM_LEN);
+	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);
 	insn_page = alloc_page();
 	alt_insn_page = alloc_page();
 	insn_ram = vmap(virt_to_phys(insn_page), 4096);
@@ -744,6 +760,8 @@ int main()
 
 	test_mmx_movq_mf(mem, insn_page, alt_insn_page, insn_ram);
 
+	test_crosspage_mmio(mem);
+
 	printf("\nSUMMARY: %d tests, %d failures\n", tests, fails);
 	return fails ? 1 : 0;
 }
Signature · 1 line
1.7.10