diff mbox

[2/3] kvm test: export vm helpers

Message ID 20100705181654.691118903@amt.cnet (mailing list archive)
State New, archived
Headers show

Commit Message

Marcelo Tosatti July 5, 2010, 6:16 p.m. UTC
None
diff mbox

Patch

Index: qemu-kvm/kvm/test/x86/vm.c
===================================================================
--- qemu-kvm.orig/kvm/test/x86/vm.c
+++ qemu-kvm/kvm/test/x86/vm.c
@@ -14,16 +14,6 @@ 
 static void *free = 0;
 static void *vfree_top = 0;
 
-static unsigned long virt_to_phys(const void *virt) 
-{ 
-    return (unsigned long)virt;
-}
-
-static void *phys_to_virt(unsigned long phys)
-{
-    return (void *)phys;
-}
-
 void *memset(void *data, int c, unsigned long len)
 {
     char *s = data;
@@ -66,11 +56,6 @@  void free_page(void *page)
 extern char edata;
 static unsigned long end_of_memory;
 
-#define PTE_PRESENT (1ull << 0)
-#define PTE_PSE     (1ull << 7)
-#define PTE_WRITE   (1ull << 1)
-#define PTE_ADDR    (0xffffffffff000ull)
-
 #ifdef __x86_64__
 #define	PAGE_LEVEL	4
 #define	PGDIR_WIDTH	9
@@ -81,10 +66,11 @@  static unsigned long end_of_memory;
 #define	PGDIR_MASK	1023
 #endif
 
-static void install_pte(unsigned long *cr3, 
-			int pte_level, 
-			void *virt,
-			unsigned long pte)
+void install_pte(unsigned long *cr3,
+		 int pte_level,
+		 void *virt,
+		 unsigned long pte,
+		 unsigned long *pt_page)
 {
     int level;
     unsigned long *pt = cr3;
@@ -93,7 +79,11 @@  static void install_pte(unsigned long *c
     for (level = PAGE_LEVEL; level > pte_level; --level) {
 	offset = ((unsigned long)virt >> ((level-1) * PGDIR_WIDTH + 12)) & PGDIR_MASK;
 	if (!(pt[offset] & PTE_PRESENT)) {
-	    unsigned long *new_pt = alloc_page();
+	    unsigned long *new_pt = pt_page;
+            if (!new_pt)
+                new_pt = alloc_page();
+            else
+                pt_page = 0;
 	    memset(new_pt, 0, PAGE_SIZE);
 	    pt[offset] = virt_to_phys(new_pt) | PTE_PRESENT | PTE_WRITE;
 	}
@@ -123,58 +113,20 @@  static unsigned long get_pte(unsigned lo
     return pte;
 }
 
-static void install_large_page(unsigned long *cr3, 
-			       unsigned long phys,
-			       void *virt)
-{
-    install_pte(cr3, 2, virt, phys | PTE_PRESENT | PTE_WRITE | PTE_PSE);
-}
-
-static void install_page(unsigned long *cr3, 
-			 unsigned long phys,
-			 void *virt)
-{
-    install_pte(cr3, 1, virt, phys | PTE_PRESENT | PTE_WRITE);
-}
-
-static inline void load_cr3(unsigned long cr3)
-{
-    asm ( "mov %0, %%cr3" : : "r"(cr3) );
-}
-
-static inline unsigned long read_cr3()
+void install_large_page(unsigned long *cr3,
+                              unsigned long phys,
+                              void *virt)
 {
-    unsigned long cr3;
-
-    asm volatile ( "mov %%cr3, %0" : "=r"(cr3) );
-    return cr3;
+    install_pte(cr3, 2, virt, phys | PTE_PRESENT | PTE_WRITE | PTE_PSE, 0);
 }
 
-static inline void load_cr0(unsigned long cr0)
+void install_page(unsigned long *cr3,
+                  unsigned long phys,
+                  void *virt)
 {
-    asm volatile ( "mov %0, %%cr0" : : "r"(cr0) );
-}
-
-static inline unsigned long read_cr0()
-{
-    unsigned long cr0;
-
-    asm volatile ( "mov %%cr0, %0" : "=r"(cr0) );
-    return cr0;
+    install_pte(cr3, 1, virt, phys | PTE_PRESENT | PTE_WRITE, 0);
 }
 
-static inline void load_cr4(unsigned long cr4)
-{
-    asm volatile ( "mov %0, %%cr4" : : "r"(cr4) );
-}
-
-static inline unsigned long read_cr4()
-{
-    unsigned long cr4;
-
-    asm volatile ( "mov %%cr4, %0" : "=r"(cr4) );
-    return cr4;
-}
 
 struct gdt_table_descr
 {
Index: qemu-kvm/kvm/test/x86/vm.h
===================================================================
--- qemu-kvm.orig/kvm/test/x86/vm.h
+++ qemu-kvm/kvm/test/x86/vm.h
@@ -1,10 +1,85 @@ 
 #ifndef VM_H
 #define VM_H
 
+#define PAGE_SIZE 4096ul
+#ifdef __x86_64__
+#define LARGE_PAGE_SIZE (512 * PAGE_SIZE)
+#else
+#define LARGE_PAGE_SIZE (1024 * PAGE_SIZE)
+#endif
+
+#define PTE_PRESENT (1ull << 0)
+#define PTE_PSE     (1ull << 7)
+#define PTE_WRITE   (1ull << 1)
+#define PTE_ADDR    (0xffffffffff000ull)
+
 void setup_vm();
 
 void *vmalloc(unsigned long size);
 void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 
+void install_pte(unsigned long *cr3,
+                        int pte_level,
+                        void *virt,
+                        unsigned long pte,
+                        unsigned long *pt_page);
+
+void *alloc_page();
+
+void install_large_page(unsigned long *cr3,unsigned long phys,
+                               void *virt);
+void install_page(unsigned long *cr3, unsigned long phys, void *virt);
+
+static inline unsigned long virt_to_phys(const void *virt)
+{
+    return (unsigned long)virt;
+}
+
+static inline void *phys_to_virt(unsigned long phys)
+{
+    return (void *)phys;
+}
+
+
+static inline void load_cr3(unsigned long cr3)
+{
+    asm ( "mov %0, %%cr3" : : "r"(cr3) );
+}
+
+static inline unsigned long read_cr3()
+{
+    unsigned long cr3;
+
+    asm volatile ( "mov %%cr3, %0" : "=r"(cr3) );
+    return cr3;
+}
+
+static inline void load_cr0(unsigned long cr0)
+{
+    asm volatile ( "mov %0, %%cr0" : : "r"(cr0) );
+}
+
+static inline unsigned long read_cr0()
+{
+    unsigned long cr0;
+
+    asm volatile ( "mov %%cr0, %0" : "=r"(cr0) );
+    return cr0;
+}
+
+
+static inline void load_cr4(unsigned long cr4)
+{
+    asm volatile ( "mov %0, %%cr4" : : "r"(cr4) );
+}
+
+static inline unsigned long read_cr4()
+{
+    unsigned long cr4;
+
+    asm volatile ( "mov %%cr4, %0" : "=r"(cr4) );
+    return cr4;
+}
+
 #endif
Index: qemu-kvm/kvm/test/x86/emulator.c
===================================================================
--- qemu-kvm.orig/kvm/test/x86/emulator.c
+++ qemu-kvm/kvm/test/x86/emulator.c
@@ -220,14 +220,6 @@  jmpf:
     report("ljmp", res);
 }
 
-unsigned long read_cr0(void)
-{
-	unsigned long cr0;
-
-	asm volatile ("mov %%cr0, %0" : "=r"(cr0));
-	return cr0;
-}
-
 void test_smsw(void)
 {
 	char mem[16];