lguest: don't share Switcher PTE pages between guests.
[cascardo/linux.git] / drivers / lguest / core.c
1 /*P:400
2  * This contains run_guest() which actually calls into the Host<->Guest
3  * Switcher and analyzes the return, such as determining if the Guest wants the
4  * Host to do something.  This file also contains useful helper routines.
5 :*/
6 #include <linux/module.h>
7 #include <linux/stringify.h>
8 #include <linux/stddef.h>
9 #include <linux/io.h>
10 #include <linux/mm.h>
11 #include <linux/vmalloc.h>
12 #include <linux/cpu.h>
13 #include <linux/freezer.h>
14 #include <linux/highmem.h>
15 #include <linux/slab.h>
16 #include <asm/paravirt.h>
17 #include <asm/pgtable.h>
18 #include <asm/uaccess.h>
19 #include <asm/poll.h>
20 #include <asm/asm-offsets.h>
21 #include "lg.h"
22
23 unsigned long switcher_addr;
24 struct page **lg_switcher_pages;
25 static struct vm_struct *switcher_vma;
26
27 /* This One Big lock protects all inter-guest data structures. */
28 DEFINE_MUTEX(lguest_lock);
29
30 /*H:010
31  * We need to set up the Switcher at a high virtual address.  Remember the
32  * Switcher is a few hundred bytes of assembler code which actually changes the
33  * CPU to run the Guest, and then changes back to the Host when a trap or
34  * interrupt happens.
35  *
36  * The Switcher code must be at the same virtual address in the Guest as the
37  * Host since it will be running as the switchover occurs.
38  *
39  * Trying to map memory at a particular address is an unusual thing to do, so
40  * it's not a simple one-liner.
41  */
42 static __init int map_switcher(void)
43 {
44         int i, err;
45         struct page **pagep;
46
47         /*
48          * Map the Switcher in to high memory.
49          *
50          * It turns out that if we choose the address 0xFFC00000 (4MB under the
51          * top virtual address), it makes setting up the page tables really
52          * easy.
53          */
54
55         /* We assume Switcher text fits into a single page. */
56         if (end_switcher_text - start_switcher_text > PAGE_SIZE) {
57                 printk(KERN_ERR "lguest: switcher text too large (%zu)\n",
58                        end_switcher_text - start_switcher_text);
59                 return -EINVAL;
60         }
61
62         /*
63          * We allocate an array of struct page pointers.  map_vm_area() wants
64          * this, rather than just an array of pages.
65          */
66         lg_switcher_pages = kmalloc(sizeof(lg_switcher_pages[0])
67                                     * TOTAL_SWITCHER_PAGES,
68                                     GFP_KERNEL);
69         if (!lg_switcher_pages) {
70                 err = -ENOMEM;
71                 goto out;
72         }
73
74         /*
75          * Now we actually allocate the pages.  The Guest will see these pages,
76          * so we make sure they're zeroed.
77          */
78         for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
79                 lg_switcher_pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
80                 if (!lg_switcher_pages[i]) {
81                         err = -ENOMEM;
82                         goto free_some_pages;
83                 }
84         }
85
86         switcher_addr = SWITCHER_ADDR;
87
88         /*
89          * First we check that the Switcher won't overlap the fixmap area at
90          * the top of memory.  It's currently nowhere near, but it could have
91          * very strange effects if it ever happened.
92          */
93         if (switcher_addr + (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE > FIXADDR_START){
94                 err = -ENOMEM;
95                 printk("lguest: mapping switcher would thwack fixmap\n");
96                 goto free_pages;
97         }
98
99         /*
100          * Now we reserve the "virtual memory area" we want.  We might
101          * not get it in theory, but in practice it's worked so far.
102          * The end address needs +1 because __get_vm_area allocates an
103          * extra guard page, so we need space for that.
104          */
105         switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE,
106                                      VM_ALLOC, switcher_addr, switcher_addr
107                                      + (TOTAL_SWITCHER_PAGES+1) * PAGE_SIZE);
108         if (!switcher_vma) {
109                 err = -ENOMEM;
110                 printk("lguest: could not map switcher pages high\n");
111                 goto free_pages;
112         }
113
114         /*
115          * This code actually sets up the pages we've allocated to appear at
116          * switcher_addr.  map_vm_area() takes the vma we allocated above, the
117          * kind of pages we're mapping (kernel pages), and a pointer to our
118          * array of struct pages.  It increments that pointer, but we don't
119          * care.
120          */
121         pagep = lg_switcher_pages;
122         err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, &pagep);
123         if (err) {
124                 printk("lguest: map_vm_area failed: %i\n", err);
125                 goto free_vma;
126         }
127
128         /*
129          * Now the Switcher is mapped at the right address, we can't fail!
130          * Copy in the compiled-in Switcher code (from x86/switcher_32.S).
131          */
132         memcpy(switcher_vma->addr, start_switcher_text,
133                end_switcher_text - start_switcher_text);
134
135         printk(KERN_INFO "lguest: mapped switcher at %p\n",
136                switcher_vma->addr);
137         /* And we succeeded... */
138         return 0;
139
140 free_vma:
141         vunmap(switcher_vma->addr);
142 free_pages:
143         i = TOTAL_SWITCHER_PAGES;
144 free_some_pages:
145         for (--i; i >= 0; i--)
146                 __free_pages(lg_switcher_pages[i], 0);
147         kfree(lg_switcher_pages);
148 out:
149         return err;
150 }
151 /*:*/
152
153 /* Cleaning up the mapping when the module is unloaded is almost... too easy. */
154 static void unmap_switcher(void)
155 {
156         unsigned int i;
157
158         /* vunmap() undoes *both* map_vm_area() and __get_vm_area(). */
159         vunmap(switcher_vma->addr);
160         /* Now we just need to free the pages we copied the switcher into */
161         for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
162                 __free_pages(lg_switcher_pages[i], 0);
163         kfree(lg_switcher_pages);
164 }
165
166 /*H:032
167  * Dealing With Guest Memory.
168  *
169  * Before we go too much further into the Host, we need to grok the routines
170  * we use to deal with Guest memory.
171  *
172  * When the Guest gives us (what it thinks is) a physical address, we can use
173  * the normal copy_from_user() & copy_to_user() on the corresponding place in
174  * the memory region allocated by the Launcher.
175  *
176  * But we can't trust the Guest: it might be trying to access the Launcher
177  * code.  We have to check that the range is below the pfn_limit the Launcher
178  * gave us.  We have to make sure that addr + len doesn't give us a false
179  * positive by overflowing, too.
180  */
181 bool lguest_address_ok(const struct lguest *lg,
182                        unsigned long addr, unsigned long len)
183 {
184         return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
185 }
186
187 /*
188  * This routine copies memory from the Guest.  Here we can see how useful the
189  * kill_lguest() routine we met in the Launcher can be: we return a random
190  * value (all zeroes) instead of needing to return an error.
191  */
192 void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes)
193 {
194         if (!lguest_address_ok(cpu->lg, addr, bytes)
195             || copy_from_user(b, cpu->lg->mem_base + addr, bytes) != 0) {
196                 /* copy_from_user should do this, but as we rely on it... */
197                 memset(b, 0, bytes);
198                 kill_guest(cpu, "bad read address %#lx len %u", addr, bytes);
199         }
200 }
201
202 /* This is the write (copy into Guest) version. */
203 void __lgwrite(struct lg_cpu *cpu, unsigned long addr, const void *b,
204                unsigned bytes)
205 {
206         if (!lguest_address_ok(cpu->lg, addr, bytes)
207             || copy_to_user(cpu->lg->mem_base + addr, b, bytes) != 0)
208                 kill_guest(cpu, "bad write address %#lx len %u", addr, bytes);
209 }
210 /*:*/
211
212 /*H:030
213  * Let's jump straight to the the main loop which runs the Guest.
214  * Remember, this is called by the Launcher reading /dev/lguest, and we keep
215  * going around and around until something interesting happens.
216  */
217 int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
218 {
219         /* We stop running once the Guest is dead. */
220         while (!cpu->lg->dead) {
221                 unsigned int irq;
222                 bool more;
223
224                 /* First we run any hypercalls the Guest wants done. */
225                 if (cpu->hcall)
226                         do_hypercalls(cpu);
227
228                 /*
229                  * It's possible the Guest did a NOTIFY hypercall to the
230                  * Launcher.
231                  */
232                 if (cpu->pending_notify) {
233                         /*
234                          * Does it just needs to write to a registered
235                          * eventfd (ie. the appropriate virtqueue thread)?
236                          */
237                         if (!send_notify_to_eventfd(cpu)) {
238                                 /* OK, we tell the main Launcher. */
239                                 if (put_user(cpu->pending_notify, user))
240                                         return -EFAULT;
241                                 return sizeof(cpu->pending_notify);
242                         }
243                 }
244
245                 /*
246                  * All long-lived kernel loops need to check with this horrible
247                  * thing called the freezer.  If the Host is trying to suspend,
248                  * it stops us.
249                  */
250                 try_to_freeze();
251
252                 /* Check for signals */
253                 if (signal_pending(current))
254                         return -ERESTARTSYS;
255
256                 /*
257                  * Check if there are any interrupts which can be delivered now:
258                  * if so, this sets up the hander to be executed when we next
259                  * run the Guest.
260                  */
261                 irq = interrupt_pending(cpu, &more);
262                 if (irq < LGUEST_IRQS)
263                         try_deliver_interrupt(cpu, irq, more);
264
265                 /*
266                  * Just make absolutely sure the Guest is still alive.  One of
267                  * those hypercalls could have been fatal, for example.
268                  */
269                 if (cpu->lg->dead)
270                         break;
271
272                 /*
273                  * If the Guest asked to be stopped, we sleep.  The Guest's
274                  * clock timer will wake us.
275                  */
276                 if (cpu->halted) {
277                         set_current_state(TASK_INTERRUPTIBLE);
278                         /*
279                          * Just before we sleep, make sure no interrupt snuck in
280                          * which we should be doing.
281                          */
282                         if (interrupt_pending(cpu, &more) < LGUEST_IRQS)
283                                 set_current_state(TASK_RUNNING);
284                         else
285                                 schedule();
286                         continue;
287                 }
288
289                 /*
290                  * OK, now we're ready to jump into the Guest.  First we put up
291                  * the "Do Not Disturb" sign:
292                  */
293                 local_irq_disable();
294
295                 /* Actually run the Guest until something happens. */
296                 lguest_arch_run_guest(cpu);
297
298                 /* Now we're ready to be interrupted or moved to other CPUs */
299                 local_irq_enable();
300
301                 /* Now we deal with whatever happened to the Guest. */
302                 lguest_arch_handle_trap(cpu);
303         }
304
305         /* Special case: Guest is 'dead' but wants a reboot. */
306         if (cpu->lg->dead == ERR_PTR(-ERESTART))
307                 return -ERESTART;
308
309         /* The Guest is dead => "No such file or directory" */
310         return -ENOENT;
311 }
312
313 /*H:000
314  * Welcome to the Host!
315  *
316  * By this point your brain has been tickled by the Guest code and numbed by
317  * the Launcher code; prepare for it to be stretched by the Host code.  This is
318  * the heart.  Let's begin at the initialization routine for the Host's lg
319  * module.
320  */
321 static int __init init(void)
322 {
323         int err;
324
325         /* Lguest can't run under Xen, VMI or itself.  It does Tricky Stuff. */
326         if (get_kernel_rpl() != 0) {
327                 printk("lguest is afraid of being a guest\n");
328                 return -EPERM;
329         }
330
331         /* First we put the Switcher up in very high virtual memory. */
332         err = map_switcher();
333         if (err)
334                 goto out;
335
336         /* We might need to reserve an interrupt vector. */
337         err = init_interrupts();
338         if (err)
339                 goto unmap;
340
341         /* /dev/lguest needs to be registered. */
342         err = lguest_device_init();
343         if (err)
344                 goto free_interrupts;
345
346         /* Finally we do some architecture-specific setup. */
347         lguest_arch_host_init();
348
349         /* All good! */
350         return 0;
351
352 free_interrupts:
353         free_interrupts();
354 unmap:
355         unmap_switcher();
356 out:
357         return err;
358 }
359
360 /* Cleaning up is just the same code, backwards.  With a little French. */
361 static void __exit fini(void)
362 {
363         lguest_device_remove();
364         free_interrupts();
365         unmap_switcher();
366
367         lguest_arch_host_fini();
368 }
369 /*:*/
370
371 /*
372  * The Host side of lguest can be a module.  This is a nice way for people to
373  * play with it.
374  */
375 module_init(init);
376 module_exit(fini);
377 MODULE_LICENSE("GPL");
378 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");