]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/lguest/page_tables.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/kyle/parisc-2.6.29
[mv-sheeva.git] / drivers / lguest / page_tables.c
index a7f64a9d67e009437b30e5007a208c25863be392..576a8318221c9dfe47dc28a660565094c9ef7753 100644 (file)
@@ -2,8 +2,8 @@
  * previous encounters.  It's functional, and as neat as it can be in the
  * circumstances, but be wary, for these things are subtle and break easily.
  * The Guest provides a virtual to physical mapping, but we can neither trust
- * it nor use it: we verify and convert it here to point the hardware to the
- * actual Guest pages when running the Guest. :*/
+ * it nor use it: we verify and convert it here then point the CPU to the
+ * converted Guest pages when running the Guest. :*/
 
 /* Copyright (C) Rusty Russell IBM Corporation 2006.
  * GPL v2 and any later version */
@@ -14,6 +14,7 @@
 #include <linux/percpu.h>
 #include <asm/tlbflush.h>
 #include <asm/uaccess.h>
+#include <asm/bootparam.h>
 #include "lg.h"
 
 /*M:008 We hold reference to pages, which prevents them from being swapped.
@@ -106,6 +107,10 @@ static unsigned long gpte_addr(pgd_t gpgd, unsigned long vaddr)
        BUG_ON(!(pgd_flags(gpgd) & _PAGE_PRESENT));
        return gpage + ((vaddr>>PAGE_SHIFT) % PTRS_PER_PTE) * sizeof(pte_t);
 }
+/*:*/
+
+/*M:014 get_pfn is slow: we could probably try to grab batches of pages here as
+ * an optimization (ie. pre-faulting). :*/
 
 /*H:350 This routine takes a page number given by the Guest and converts it to
  * an actual, physical page number.  It can fail for several reasons: the
@@ -113,24 +118,18 @@ static unsigned long gpte_addr(pgd_t gpgd, unsigned long vaddr)
  * and the page is read-only, or the write flag was set and the page was
  * shared so had to be copied, but we ran out of memory.
  *
- * This holds a reference to the page, so release_pte() is careful to
- * put that back. */
+ * This holds a reference to the page, so release_pte() is careful to put that
+ * back. */
 static unsigned long get_pfn(unsigned long virtpfn, int write)
 {
        struct page *page;
-       /* This value indicates failure. */
-       unsigned long ret = -1UL;
 
-       /* get_user_pages() is a complex interface: it gets the "struct
-        * vm_area_struct" and "struct page" assocated with a range of pages.
-        * It also needs the task's mmap_sem held, and is not very quick.
-        * It returns the number of pages it got. */
-       down_read(&current->mm->mmap_sem);
-       if (get_user_pages(current, current->mm, virtpfn << PAGE_SHIFT,
-                          1, write, 1, &page, NULL) == 1)
-               ret = page_to_pfn(page);
-       up_read(&current->mm->mmap_sem);
-       return ret;
+       /* gup me one page at this address please! */
+       if (get_user_pages_fast(virtpfn << PAGE_SHIFT, 1, write, &page) == 1)
+               return page_to_pfn(page);
+
+       /* This value indicates failure. */
+       return -1UL;
 }
 
 /*H:340 Converting a Guest page table entry to a shadow (ie. real) page table
@@ -169,7 +168,7 @@ static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write)
 /*H:460 And to complete the chain, release_pte() looks like this: */
 static void release_pte(pte_t pte)
 {
-       /* Remember that get_user_pages() took a reference to the page, in
+       /* Remember that get_user_pages_fast() took a reference to the page, in
         * get_pfn()?  We have to put it back now. */
        if (pte_flags(pte) & _PAGE_PRESENT)
                put_page(pfn_to_page(pte_pfn(pte)));
@@ -532,13 +531,13 @@ static void do_set_pte(struct lg_cpu *cpu, int idx,
  * all processes.  So when the page table above that address changes, we update
  * all the page tables, not just the current one.  This is rare.
  *
- * The benefit is that when we have to track a new page table, we can copy keep
- * all the kernel mappings.  This speeds up context switch immensely. */
+ * The benefit is that when we have to track a new page table, we can keep all
+ * the kernel mappings.  This speeds up context switch immensely. */
 void guest_set_pte(struct lg_cpu *cpu,
                   unsigned long gpgdir, unsigned long vaddr, pte_t gpte)
 {
-       /* Kernel mappings must be changed on all top levels.  Slow, but
-        * doesn't happen often. */
+       /* Kernel mappings must be changed on all top levels.  Slow, but doesn't
+        * happen often. */
        if (vaddr >= cpu->lg->kernel_address) {
                unsigned int i;
                for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++)
@@ -583,15 +582,82 @@ void guest_set_pmd(struct lguest *lg, unsigned long gpgdir, u32 idx)
                release_pgd(lg, lg->pgdirs[pgdir].pgdir + idx);
 }
 
+/* Once we know how much memory we have we can construct simple identity
+ * (which set virtual == physical) and linear mappings
+ * which will get the Guest far enough into the boot to create its own.
+ *
+ * We lay them out of the way, just below the initrd (which is why we need to
+ * know its size here). */
+static unsigned long setup_pagetables(struct lguest *lg,
+                                     unsigned long mem,
+                                     unsigned long initrd_size)
+{
+       pgd_t __user *pgdir;
+       pte_t __user *linear;
+       unsigned int mapped_pages, i, linear_pages, phys_linear;
+       unsigned long mem_base = (unsigned long)lg->mem_base;
+
+       /* We have mapped_pages frames to map, so we need
+        * linear_pages page tables to map them. */
+       mapped_pages = mem / PAGE_SIZE;
+       linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE;
+
+       /* We put the toplevel page directory page at the top of memory. */
+       pgdir = (pgd_t *)(mem + mem_base - initrd_size - PAGE_SIZE);
+
+       /* Now we use the next linear_pages pages as pte pages */
+       linear = (void *)pgdir - linear_pages * PAGE_SIZE;
+
+       /* Linear mapping is easy: put every page's address into the
+        * mapping in order. */
+       for (i = 0; i < mapped_pages; i++) {
+               pte_t pte;
+               pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER));
+               if (copy_to_user(&linear[i], &pte, sizeof(pte)) != 0)
+                       return -EFAULT;
+       }
+
+       /* The top level points to the linear page table pages above.
+        * We setup the identity and linear mappings here. */
+       phys_linear = (unsigned long)linear - mem_base;
+       for (i = 0; i < mapped_pages; i += PTRS_PER_PTE) {
+               pgd_t pgd;
+               pgd = __pgd((phys_linear + i * sizeof(pte_t)) |
+                           (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER));
+
+               if (copy_to_user(&pgdir[i / PTRS_PER_PTE], &pgd, sizeof(pgd))
+                   || copy_to_user(&pgdir[pgd_index(PAGE_OFFSET)
+                                          + i / PTRS_PER_PTE],
+                                   &pgd, sizeof(pgd)))
+                       return -EFAULT;
+       }
+
+       /* We return the top level (guest-physical) address: remember where
+        * this is. */
+       return (unsigned long)pgdir - mem_base;
+}
+
 /*H:500 (vii) Setting up the page tables initially.
  *
  * When a Guest is first created, the Launcher tells us where the toplevel of
  * its first page table is.  We set some things up here: */
-int init_guest_pagetable(struct lguest *lg, unsigned long pgtable)
+int init_guest_pagetable(struct lguest *lg)
 {
+       u64 mem;
+       u32 initrd_size;
+       struct boot_params __user *boot = (struct boot_params *)lg->mem_base;
+
+       /* Get the Guest memory size and the ramdisk size from the boot header
+        * located at lg->mem_base (Guest address 0). */
+       if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem))
+           || get_user(initrd_size, &boot->hdr.ramdisk_size))
+               return -EFAULT;
+
        /* We start on the first shadow page table, and give it a blank PGD
         * page. */
-       lg->pgdirs[0].gpgdir = pgtable;
+       lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size);
+       if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir))
+               return lg->pgdirs[0].gpgdir;
        lg->pgdirs[0].pgdir = (pgd_t *)get_zeroed_page(GFP_KERNEL);
        if (!lg->pgdirs[0].pgdir)
                return -ENOMEM;
@@ -704,12 +770,11 @@ static __init void populate_switcher_pte_page(unsigned int cpu,
 /* We've made it through the page table code.  Perhaps our tired brains are
  * still processing the details, or perhaps we're simply glad it's over.
  *
- * If nothing else, note that all this complexity in juggling shadow page
- * tables in sync with the Guest's page tables is for one reason: for most
- * Guests this page table dance determines how bad performance will be.  This
- * is why Xen uses exotic direct Guest pagetable manipulation, and why both
- * Intel and AMD have implemented shadow page table support directly into
- * hardware.
+ * If nothing else, note that all this complexity in juggling shadow page tables
+ * in sync with the Guest's page tables is for one reason: for most Guests this
+ * page table dance determines how bad performance will be.  This is why Xen
+ * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD
+ * have implemented shadow page table support directly into hardware.
  *
  * There is just one file remaining in the Host. */