blob: 2cea0c80c9924962a48f6027d774ba1e0a4647ba [file] [log] [blame]
Rusty Russellf938d2c2007-07-26 10:41:02 -07001/*P:400 This contains run_guest() which actually calls into the Host<->Guest
2 * Switcher and analyzes the return, such as determining if the Guest wants the
3 * Host to do something. This file also contains useful helper routines, and a
4 * couple of non-obvious setup and teardown pieces which were implemented after
5 * days of debugging pain. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07006#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 <asm/paravirt.h>
15#include <asm/desc.h>
16#include <asm/pgtable.h>
17#include <asm/uaccess.h>
18#include <asm/poll.h>
19#include <asm/highmem.h>
20#include <asm/asm-offsets.h>
21#include <asm/i387.h>
22#include "lg.h"
23
24/* Found in switcher.S */
25extern char start_switcher_text[], end_switcher_text[], switch_to_guest[];
26extern unsigned long default_idt_entries[];
27
28/* Every guest maps the core switcher code. */
29#define SHARED_SWITCHER_PAGES \
30 DIV_ROUND_UP(end_switcher_text - start_switcher_text, PAGE_SIZE)
31/* Pages for switcher itself, then two pages per cpu */
32#define TOTAL_SWITCHER_PAGES (SHARED_SWITCHER_PAGES + 2 * NR_CPUS)
33
34/* We map at -4M for ease of mapping into the guest (one PTE page). */
35#define SWITCHER_ADDR 0xFFC00000
36
37static struct vm_struct *switcher_vma;
38static struct page **switcher_page;
39
40static int cpu_had_pge;
41static struct {
42 unsigned long offset;
43 unsigned short segment;
44} lguest_entry;
45
46/* This One Big lock protects all inter-guest data structures. */
47DEFINE_MUTEX(lguest_lock);
48static DEFINE_PER_CPU(struct lguest *, last_guest);
49
50/* FIXME: Make dynamic. */
51#define MAX_LGUEST_GUESTS 16
52struct lguest lguests[MAX_LGUEST_GUESTS];
53
54/* Offset from where switcher.S was compiled to where we've copied it */
55static unsigned long switcher_offset(void)
56{
57 return SWITCHER_ADDR - (unsigned long)start_switcher_text;
58}
59
60/* This cpu's struct lguest_pages. */
61static struct lguest_pages *lguest_pages(unsigned int cpu)
62{
63 return &(((struct lguest_pages *)
64 (SWITCHER_ADDR + SHARED_SWITCHER_PAGES*PAGE_SIZE))[cpu]);
65}
66
67static __init int map_switcher(void)
68{
69 int i, err;
70 struct page **pagep;
71
72 switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES,
73 GFP_KERNEL);
74 if (!switcher_page) {
75 err = -ENOMEM;
76 goto out;
77 }
78
79 for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
80 unsigned long addr = get_zeroed_page(GFP_KERNEL);
81 if (!addr) {
82 err = -ENOMEM;
83 goto free_some_pages;
84 }
85 switcher_page[i] = virt_to_page(addr);
86 }
87
88 switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE,
89 VM_ALLOC, SWITCHER_ADDR, VMALLOC_END);
90 if (!switcher_vma) {
91 err = -ENOMEM;
92 printk("lguest: could not map switcher pages high\n");
93 goto free_pages;
94 }
95
96 pagep = switcher_page;
97 err = map_vm_area(switcher_vma, PAGE_KERNEL, &pagep);
98 if (err) {
99 printk("lguest: map_vm_area failed: %i\n", err);
100 goto free_vma;
101 }
102 memcpy(switcher_vma->addr, start_switcher_text,
103 end_switcher_text - start_switcher_text);
104
105 /* Fix up IDT entries to point into copied text. */
106 for (i = 0; i < IDT_ENTRIES; i++)
107 default_idt_entries[i] += switcher_offset();
108
109 for_each_possible_cpu(i) {
110 struct lguest_pages *pages = lguest_pages(i);
111 struct lguest_ro_state *state = &pages->state;
112
113 /* These fields are static: rest done in copy_in_guest_info */
114 state->host_gdt_desc.size = GDT_SIZE-1;
115 state->host_gdt_desc.address = (long)get_cpu_gdt_table(i);
116 store_idt(&state->host_idt_desc);
117 state->guest_idt_desc.size = sizeof(state->guest_idt)-1;
118 state->guest_idt_desc.address = (long)&state->guest_idt;
119 state->guest_gdt_desc.size = sizeof(state->guest_gdt)-1;
120 state->guest_gdt_desc.address = (long)&state->guest_gdt;
121 state->guest_tss.esp0 = (long)(&pages->regs + 1);
122 state->guest_tss.ss0 = LGUEST_DS;
123 /* No I/O for you! */
124 state->guest_tss.io_bitmap_base = sizeof(state->guest_tss);
125 setup_default_gdt_entries(state);
126 setup_default_idt_entries(state, default_idt_entries);
127
128 /* Setup LGUEST segments on all cpus */
129 get_cpu_gdt_table(i)[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT;
130 get_cpu_gdt_table(i)[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT;
131 }
132
133 /* Initialize entry point into switcher. */
134 lguest_entry.offset = (long)switch_to_guest + switcher_offset();
135 lguest_entry.segment = LGUEST_CS;
136
137 printk(KERN_INFO "lguest: mapped switcher at %p\n",
138 switcher_vma->addr);
139 return 0;
140
141free_vma:
142 vunmap(switcher_vma->addr);
143free_pages:
144 i = TOTAL_SWITCHER_PAGES;
145free_some_pages:
146 for (--i; i >= 0; i--)
147 __free_pages(switcher_page[i], 0);
148 kfree(switcher_page);
149out:
150 return err;
151}
152
153static void unmap_switcher(void)
154{
155 unsigned int i;
156
157 vunmap(switcher_vma->addr);
158 for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
159 __free_pages(switcher_page[i], 0);
160}
161
162/* IN/OUT insns: enough to get us past boot-time probing. */
163static int emulate_insn(struct lguest *lg)
164{
165 u8 insn;
166 unsigned int insnlen = 0, in = 0, shift = 0;
167 unsigned long physaddr = guest_pa(lg, lg->regs->eip);
168
169 /* This only works for addresses in linear mapping... */
170 if (lg->regs->eip < lg->page_offset)
171 return 0;
172 lgread(lg, &insn, physaddr, 1);
173
174 /* Operand size prefix means it's actually for ax. */
175 if (insn == 0x66) {
176 shift = 16;
177 insnlen = 1;
178 lgread(lg, &insn, physaddr + insnlen, 1);
179 }
180
181 switch (insn & 0xFE) {
182 case 0xE4: /* in <next byte>,%al */
183 insnlen += 2;
184 in = 1;
185 break;
186 case 0xEC: /* in (%dx),%al */
187 insnlen += 1;
188 in = 1;
189 break;
190 case 0xE6: /* out %al,<next byte> */
191 insnlen += 2;
192 break;
193 case 0xEE: /* out %al,(%dx) */
194 insnlen += 1;
195 break;
196 default:
197 return 0;
198 }
199
200 if (in) {
201 /* Lower bit tells is whether it's a 16 or 32 bit access */
202 if (insn & 0x1)
203 lg->regs->eax = 0xFFFFFFFF;
204 else
205 lg->regs->eax |= (0xFFFF << shift);
206 }
207 lg->regs->eip += insnlen;
208 return 1;
209}
210
211int lguest_address_ok(const struct lguest *lg,
212 unsigned long addr, unsigned long len)
213{
214 return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
215}
216
217/* Just like get_user, but don't let guest access lguest binary. */
218u32 lgread_u32(struct lguest *lg, unsigned long addr)
219{
220 u32 val = 0;
221
222 /* Don't let them access lguest binary */
223 if (!lguest_address_ok(lg, addr, sizeof(val))
224 || get_user(val, (u32 __user *)addr) != 0)
225 kill_guest(lg, "bad read address %#lx", addr);
226 return val;
227}
228
229void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val)
230{
231 if (!lguest_address_ok(lg, addr, sizeof(val))
232 || put_user(val, (u32 __user *)addr) != 0)
233 kill_guest(lg, "bad write address %#lx", addr);
234}
235
236void lgread(struct lguest *lg, void *b, unsigned long addr, unsigned bytes)
237{
238 if (!lguest_address_ok(lg, addr, bytes)
239 || copy_from_user(b, (void __user *)addr, bytes) != 0) {
240 /* copy_from_user should do this, but as we rely on it... */
241 memset(b, 0, bytes);
242 kill_guest(lg, "bad read address %#lx len %u", addr, bytes);
243 }
244}
245
246void lgwrite(struct lguest *lg, unsigned long addr, const void *b,
247 unsigned bytes)
248{
249 if (!lguest_address_ok(lg, addr, bytes)
250 || copy_to_user((void __user *)addr, b, bytes) != 0)
251 kill_guest(lg, "bad write address %#lx len %u", addr, bytes);
252}
253
254static void set_ts(void)
255{
256 u32 cr0;
257
258 cr0 = read_cr0();
259 if (!(cr0 & 8))
260 write_cr0(cr0|8);
261}
262
263static void copy_in_guest_info(struct lguest *lg, struct lguest_pages *pages)
264{
265 if (__get_cpu_var(last_guest) != lg || lg->last_pages != pages) {
266 __get_cpu_var(last_guest) = lg;
267 lg->last_pages = pages;
268 lg->changed = CHANGED_ALL;
269 }
270
271 /* These are pretty cheap, so we do them unconditionally. */
272 pages->state.host_cr3 = __pa(current->mm->pgd);
273 map_switcher_in_guest(lg, pages);
274 pages->state.guest_tss.esp1 = lg->esp1;
275 pages->state.guest_tss.ss1 = lg->ss1;
276
277 /* Copy direct trap entries. */
278 if (lg->changed & CHANGED_IDT)
279 copy_traps(lg, pages->state.guest_idt, default_idt_entries);
280
281 /* Copy all GDT entries but the TSS. */
282 if (lg->changed & CHANGED_GDT)
283 copy_gdt(lg, pages->state.guest_gdt);
284 /* If only the TLS entries have changed, copy them. */
285 else if (lg->changed & CHANGED_GDT_TLS)
286 copy_gdt_tls(lg, pages->state.guest_gdt);
287
288 lg->changed = 0;
289}
290
291static void run_guest_once(struct lguest *lg, struct lguest_pages *pages)
292{
293 unsigned int clobber;
294
295 copy_in_guest_info(lg, pages);
296
297 /* Put eflags on stack, lcall does rest: suitable for iret return. */
298 asm volatile("pushf; lcall *lguest_entry"
299 : "=a"(clobber), "=b"(clobber)
300 : "0"(pages), "1"(__pa(lg->pgdirs[lg->pgdidx].pgdir))
301 : "memory", "%edx", "%ecx", "%edi", "%esi");
302}
303
304int run_guest(struct lguest *lg, unsigned long __user *user)
305{
306 while (!lg->dead) {
307 unsigned int cr2 = 0; /* Damn gcc */
308
309 /* Hypercalls first: we might have been out to userspace */
310 do_hypercalls(lg);
311 if (lg->dma_is_pending) {
312 if (put_user(lg->pending_dma, user) ||
313 put_user(lg->pending_key, user+1))
314 return -EFAULT;
315 return sizeof(unsigned long)*2;
316 }
317
318 if (signal_pending(current))
319 return -ERESTARTSYS;
320
321 /* If Waker set break_out, return to Launcher. */
322 if (lg->break_out)
323 return -EAGAIN;
324
325 maybe_do_interrupt(lg);
326
327 try_to_freeze();
328
329 if (lg->dead)
330 break;
331
332 if (lg->halted) {
333 set_current_state(TASK_INTERRUPTIBLE);
334 schedule();
335 continue;
336 }
337
338 local_irq_disable();
339
340 /* Even if *we* don't want FPU trap, guest might... */
341 if (lg->ts)
342 set_ts();
343
344 /* Don't let Guest do SYSENTER: we can't handle it. */
345 if (boot_cpu_has(X86_FEATURE_SEP))
346 wrmsr(MSR_IA32_SYSENTER_CS, 0, 0);
347
348 run_guest_once(lg, lguest_pages(raw_smp_processor_id()));
349
350 /* Save cr2 now if we page-faulted. */
351 if (lg->regs->trapnum == 14)
352 cr2 = read_cr2();
353 else if (lg->regs->trapnum == 7)
354 math_state_restore();
355
356 if (boot_cpu_has(X86_FEATURE_SEP))
357 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
358 local_irq_enable();
359
360 switch (lg->regs->trapnum) {
361 case 13: /* We've intercepted a GPF. */
362 if (lg->regs->errcode == 0) {
363 if (emulate_insn(lg))
364 continue;
365 }
366 break;
367 case 14: /* We've intercepted a page fault. */
368 if (demand_page(lg, cr2, lg->regs->errcode))
369 continue;
370
371 /* If lguest_data is NULL, this won't hurt. */
372 if (put_user(cr2, &lg->lguest_data->cr2))
373 kill_guest(lg, "Writing cr2");
374 break;
375 case 7: /* We've intercepted a Device Not Available fault. */
376 /* If they don't want to know, just absorb it. */
377 if (!lg->ts)
378 continue;
379 break;
380 case 32 ... 255: /* Real interrupt, fall thru */
381 cond_resched();
382 case LGUEST_TRAP_ENTRY: /* Handled at top of loop */
383 continue;
384 }
385
386 if (deliver_trap(lg, lg->regs->trapnum))
387 continue;
388
389 kill_guest(lg, "unhandled trap %li at %#lx (%#lx)",
390 lg->regs->trapnum, lg->regs->eip,
391 lg->regs->trapnum == 14 ? cr2 : lg->regs->errcode);
392 }
393 return -ENOENT;
394}
395
396int find_free_guest(void)
397{
398 unsigned int i;
399 for (i = 0; i < MAX_LGUEST_GUESTS; i++)
400 if (!lguests[i].tsk)
401 return i;
402 return -1;
403}
404
405static void adjust_pge(void *on)
406{
407 if (on)
408 write_cr4(read_cr4() | X86_CR4_PGE);
409 else
410 write_cr4(read_cr4() & ~X86_CR4_PGE);
411}
412
413static int __init init(void)
414{
415 int err;
416
417 if (paravirt_enabled()) {
418 printk("lguest is afraid of %s\n", paravirt_ops.name);
419 return -EPERM;
420 }
421
422 err = map_switcher();
423 if (err)
424 return err;
425
426 err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
427 if (err) {
428 unmap_switcher();
429 return err;
430 }
431 lguest_io_init();
432
433 err = lguest_device_init();
434 if (err) {
435 free_pagetables();
436 unmap_switcher();
437 return err;
438 }
439 lock_cpu_hotplug();
440 if (cpu_has_pge) { /* We have a broader idea of "global". */
441 cpu_had_pge = 1;
442 on_each_cpu(adjust_pge, (void *)0, 0, 1);
443 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
444 }
445 unlock_cpu_hotplug();
446 return 0;
447}
448
449static void __exit fini(void)
450{
451 lguest_device_remove();
452 free_pagetables();
453 unmap_switcher();
454 lock_cpu_hotplug();
455 if (cpu_had_pge) {
456 set_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
457 on_each_cpu(adjust_pge, (void *)1, 0, 1);
458 }
459 unlock_cpu_hotplug();
460}
461
462module_init(init);
463module_exit(fini);
464MODULE_LICENSE("GPL");
465MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");