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