From patchwork Wed Mar 24 21:24:12 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 88063 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o2OLTekF014216 for ; Wed, 24 Mar 2010 21:29:40 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753417Ab0CXV3W (ORCPT ); Wed, 24 Mar 2010 17:29:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52242 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753134Ab0CXV3U (ORCPT ); Wed, 24 Mar 2010 17:29:20 -0400 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o2OLTKCV019038 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 24 Mar 2010 17:29:20 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o2OLTJ3x028338; Wed, 24 Mar 2010 17:29:19 -0400 Received: from amt.cnet (vpn-10-46.rdu.redhat.com [10.11.10.46]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o2OLTHbj008880; Wed, 24 Mar 2010 17:29:18 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id BFCE168AC68; Wed, 24 Mar 2010 18:28:13 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id o2OLSCZC022414; Wed, 24 Mar 2010 18:28:12 -0300 Message-Id: <20100324212726.102810660@amt.cnet> User-Agent: quilt/0.47-1 Date: Wed, 24 Mar 2010 18:24:12 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org Cc: Marcelo Tosatti Subject: [patch 4/8] test: export vm helpers References: <20100324212408.790319364@amt.cnet> Content-Disposition: inline; filename=vm-export-helpers X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Wed, 24 Mar 2010 21:29:40 +0000 (UTC) Index: qemu-kvm/kvm/user/test/x86/vm.c =================================================================== --- qemu-kvm.orig/kvm/user/test/x86/vm.c +++ qemu-kvm/kvm/user/test/x86/vm.c @@ -3,22 +3,9 @@ void print(const char *s); -#define PAGE_SIZE 4096ul -#define LARGE_PAGE_SIZE (512 * PAGE_SIZE) - 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; @@ -61,15 +48,11 @@ 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) - -static void install_pte(unsigned long *cr3, - int pte_level, +void install_pte(unsigned long *cr3, + int pte_level, void *virt, - unsigned long pte) + unsigned long pte, + unsigned long *pt_page) { int level; unsigned long *pt = cr3; @@ -78,7 +61,11 @@ static void install_pte(unsigned long *c for (level = 4; level > pte_level; --level) { offset = ((unsigned long)virt >> ((level-1) * 9 + 12)) & 511; 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; } @@ -108,58 +95,20 @@ static unsigned long get_pte(unsigned lo return pte; } -static void install_large_page(unsigned long *cr3, - unsigned long phys, - void *virt) +void install_large_page(unsigned long *cr3, + unsigned long phys, + void *virt) { - install_pte(cr3, 2, virt, phys | PTE_PRESENT | PTE_WRITE | PTE_PSE); + install_pte(cr3, 2, virt, phys | PTE_PRESENT | PTE_WRITE | PTE_PSE, 0); } -static void install_page(unsigned long *cr3, - unsigned long phys, - void *virt) +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() -{ - unsigned long cr3; - - asm volatile ( "mov %%cr3, %0" : "=r"(cr3) ); - return cr3; + install_pte(cr3, 1, virt, phys | PTE_PRESENT | PTE_WRITE, 0); } -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; -} struct gdt_table_descr { Index: qemu-kvm/kvm/user/test/x86/vm.h =================================================================== --- qemu-kvm.orig/kvm/user/test/x86/vm.h +++ qemu-kvm/kvm/user/test/x86/vm.h @@ -1,10 +1,72 @@ #ifndef VM_H #define VM_H +#define PAGE_SIZE 4096ul +#define LARGE_PAGE_SIZE (512 * PAGE_SIZE) + +#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 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