Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[firefly-linux-kernel-4.4.55.git] / drivers / lguest / core.c
index 0992523011328028e49983142b5f769fa4e572e0..6590558d1d31c600b23c8d50f48b4e3b1c461326 100644 (file)
@@ -21,8 +21,8 @@
 #include "lg.h"
 
 unsigned long switcher_addr;
+struct page **lg_switcher_pages;
 static struct vm_struct *switcher_vma;
-static struct page **switcher_page;
 
 /* This One Big lock protects all inter-guest data structures. */
 DEFINE_MUTEX(lguest_lock);
@@ -42,7 +42,6 @@ DEFINE_MUTEX(lguest_lock);
 static __init int map_switcher(void)
 {
        int i, err;
-       struct page **pagep;
 
        /*
         * Map the Switcher in to high memory.
@@ -52,13 +51,21 @@ static __init int map_switcher(void)
         * easy.
         */
 
+       /* We assume Switcher text fits into a single page. */
+       if (end_switcher_text - start_switcher_text > PAGE_SIZE) {
+               printk(KERN_ERR "lguest: switcher text too large (%zu)\n",
+                      end_switcher_text - start_switcher_text);
+               return -EINVAL;
+       }
+
        /*
         * We allocate an array of struct page pointers.  map_vm_area() wants
         * this, rather than just an array of pages.
         */
-       switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES,
-                               GFP_KERNEL);
-       if (!switcher_page) {
+       lg_switcher_pages = kmalloc(sizeof(lg_switcher_pages[0])
+                                   * TOTAL_SWITCHER_PAGES,
+                                   GFP_KERNEL);
+       if (!lg_switcher_pages) {
                err = -ENOMEM;
                goto out;
        }
@@ -68,25 +75,20 @@ static __init int map_switcher(void)
         * so we make sure they're zeroed.
         */
        for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
-               switcher_page[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
-               if (!switcher_page[i]) {
+               lg_switcher_pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
+               if (!lg_switcher_pages[i]) {
                        err = -ENOMEM;
                        goto free_some_pages;
                }
        }
 
-       switcher_addr = SWITCHER_ADDR;
-
        /*
-        * First we check that the Switcher won't overlap the fixmap area at
-        * the top of memory.  It's currently nowhere near, but it could have
-        * very strange effects if it ever happened.
+        * We place the Switcher underneath the fixmap area, which is the
+        * highest virtual address we can get.  This is important, since we
+        * tell the Guest it can't access this memory, so we want its ceiling
+        * as high as possible.
         */
-       if (switcher_addr + (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE > FIXADDR_START){
-               err = -ENOMEM;
-               printk("lguest: mapping switcher would thwack fixmap\n");
-               goto free_pages;
-       }
+       switcher_addr = FIXADDR_START - (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE;
 
        /*
         * Now we reserve the "virtual memory area" we want.  We might
@@ -107,11 +109,9 @@ static __init int map_switcher(void)
         * This code actually sets up the pages we've allocated to appear at
         * switcher_addr.  map_vm_area() takes the vma we allocated above, the
         * kind of pages we're mapping (kernel pages), and a pointer to our
-        * array of struct pages.  It increments that pointer, but we don't
-        * care.
+        * array of struct pages.
         */
-       pagep = switcher_page;
-       err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, &pagep);
+       err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, lg_switcher_pages);
        if (err) {
                printk("lguest: map_vm_area failed: %i\n", err);
                goto free_vma;
@@ -135,8 +135,8 @@ free_pages:
        i = TOTAL_SWITCHER_PAGES;
 free_some_pages:
        for (--i; i >= 0; i--)
-               __free_pages(switcher_page[i], 0);
-       kfree(switcher_page);
+               __free_pages(lg_switcher_pages[i], 0);
+       kfree(lg_switcher_pages);
 out:
        return err;
 }
@@ -151,8 +151,8 @@ static void unmap_switcher(void)
        vunmap(switcher_vma->addr);
        /* Now we just need to free the pages we copied the switcher into */
        for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
-               __free_pages(switcher_page[i], 0);
-       kfree(switcher_page);
+               __free_pages(lg_switcher_pages[i], 0);
+       kfree(lg_switcher_pages);
 }
 
 /*H:032
@@ -325,15 +325,10 @@ static int __init init(void)
        if (err)
                goto out;
 
-       /* Now we set up the pagetable implementation for the Guests. */
-       err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
-       if (err)
-               goto unmap;
-
        /* We might need to reserve an interrupt vector. */
        err = init_interrupts();
        if (err)
-               goto free_pgtables;
+               goto unmap;
 
        /* /dev/lguest needs to be registered. */
        err = lguest_device_init();
@@ -348,8 +343,6 @@ static int __init init(void)
 
 free_interrupts:
        free_interrupts();
-free_pgtables:
-       free_pagetables();
 unmap:
        unmap_switcher();
 out:
@@ -361,7 +354,6 @@ static void __exit fini(void)
 {
        lguest_device_remove();
        free_interrupts();
-       free_pagetables();
        unmap_switcher();
 
        lguest_arch_host_fini();