1 /*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. :*/
6 #include <linux/module.h>
7 #include <linux/stringify.h>
8 #include <linux/stddef.h>
11 #include <linux/vmalloc.h>
12 #include <linux/cpu.h>
13 #include <linux/freezer.h>
14 #include <asm/paravirt.h>
16 #include <asm/pgtable.h>
17 #include <asm/uaccess.h>
19 #include <asm/highmem.h>
20 #include <asm/asm-offsets.h>
24 /* Found in switcher.S */
25 extern char start_switcher_text[], end_switcher_text[], switch_to_guest[];
26 extern unsigned long default_idt_entries[];
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)
34 /* We map at -4M for ease of mapping into the guest (one PTE page). */
35 #define SWITCHER_ADDR 0xFFC00000
37 static struct vm_struct *switcher_vma;
38 static struct page **switcher_page;
40 static int cpu_had_pge;
43 unsigned short segment;
46 /* This One Big lock protects all inter-guest data structures. */
47 DEFINE_MUTEX(lguest_lock);
48 static DEFINE_PER_CPU(struct lguest *, last_guest);
50 /* FIXME: Make dynamic. */
51 #define MAX_LGUEST_GUESTS 16
52 struct lguest lguests[MAX_LGUEST_GUESTS];
54 /* Offset from where switcher.S was compiled to where we've copied it */
55 static unsigned long switcher_offset(void)
57 return SWITCHER_ADDR - (unsigned long)start_switcher_text;
60 /* This cpu's struct lguest_pages. */
61 static struct lguest_pages *lguest_pages(unsigned int cpu)
63 return &(((struct lguest_pages *)
64 (SWITCHER_ADDR + SHARED_SWITCHER_PAGES*PAGE_SIZE))[cpu]);
67 static __init int map_switcher(void)
72 switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES,
79 for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
80 unsigned long addr = get_zeroed_page(GFP_KERNEL);
85 switcher_page[i] = virt_to_page(addr);
88 switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE,
89 VM_ALLOC, SWITCHER_ADDR, VMALLOC_END);
92 printk("lguest: could not map switcher pages high\n");
96 pagep = switcher_page;
97 err = map_vm_area(switcher_vma, PAGE_KERNEL, &pagep);
99 printk("lguest: map_vm_area failed: %i\n", err);
102 memcpy(switcher_vma->addr, start_switcher_text,
103 end_switcher_text - start_switcher_text);
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();
109 for_each_possible_cpu(i) {
110 struct lguest_pages *pages = lguest_pages(i);
111 struct lguest_ro_state *state = &pages->state;
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);
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;
133 /* Initialize entry point into switcher. */
134 lguest_entry.offset = (long)switch_to_guest + switcher_offset();
135 lguest_entry.segment = LGUEST_CS;
137 printk(KERN_INFO "lguest: mapped switcher at %p\n",
142 vunmap(switcher_vma->addr);
144 i = TOTAL_SWITCHER_PAGES;
146 for (--i; i >= 0; i--)
147 __free_pages(switcher_page[i], 0);
148 kfree(switcher_page);
153 static void unmap_switcher(void)
157 vunmap(switcher_vma->addr);
158 for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
159 __free_pages(switcher_page[i], 0);
162 /* IN/OUT insns: enough to get us past boot-time probing. */
163 static int emulate_insn(struct lguest *lg)
166 unsigned int insnlen = 0, in = 0, shift = 0;
167 unsigned long physaddr = guest_pa(lg, lg->regs->eip);
169 /* This only works for addresses in linear mapping... */
170 if (lg->regs->eip < lg->page_offset)
172 lgread(lg, &insn, physaddr, 1);
174 /* Operand size prefix means it's actually for ax. */
178 lgread(lg, &insn, physaddr + insnlen, 1);
181 switch (insn & 0xFE) {
182 case 0xE4: /* in <next byte>,%al */
186 case 0xEC: /* in (%dx),%al */
190 case 0xE6: /* out %al,<next byte> */
193 case 0xEE: /* out %al,(%dx) */
201 /* Lower bit tells is whether it's a 16 or 32 bit access */
203 lg->regs->eax = 0xFFFFFFFF;
205 lg->regs->eax |= (0xFFFF << shift);
207 lg->regs->eip += insnlen;
212 * Dealing With Guest Memory.
214 * When the Guest gives us (what it thinks is) a physical address, we can use
215 * the normal copy_from_user() & copy_to_user() on that address: remember,
216 * Guest physical == Launcher virtual.
218 * But we can't trust the Guest: it might be trying to access the Launcher
219 * code. We have to check that the range is below the pfn_limit the Launcher
220 * gave us. We have to make sure that addr + len doesn't give us a false
221 * positive by overflowing, too. */
222 int lguest_address_ok(const struct lguest *lg,
223 unsigned long addr, unsigned long len)
225 return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
228 /* This is a convenient routine to get a 32-bit value from the Guest (a very
229 * common operation). Here we can see how useful the kill_lguest() routine we
230 * met in the Launcher can be: we return a random value (0) instead of needing
231 * to return an error. */
232 u32 lgread_u32(struct lguest *lg, unsigned long addr)
236 /* Don't let them access lguest binary. */
237 if (!lguest_address_ok(lg, addr, sizeof(val))
238 || get_user(val, (u32 __user *)addr) != 0)
239 kill_guest(lg, "bad read address %#lx", addr);
243 /* Same thing for writing a value. */
244 void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val)
246 if (!lguest_address_ok(lg, addr, sizeof(val))
247 || put_user(val, (u32 __user *)addr) != 0)
248 kill_guest(lg, "bad write address %#lx", addr);
251 /* This routine is more generic, and copies a range of Guest bytes into a
252 * buffer. If the copy_from_user() fails, we fill the buffer with zeroes, so
253 * the caller doesn't end up using uninitialized kernel memory. */
254 void lgread(struct lguest *lg, void *b, unsigned long addr, unsigned bytes)
256 if (!lguest_address_ok(lg, addr, bytes)
257 || copy_from_user(b, (void __user *)addr, bytes) != 0) {
258 /* copy_from_user should do this, but as we rely on it... */
260 kill_guest(lg, "bad read address %#lx len %u", addr, bytes);
264 /* Similarly, our generic routine to copy into a range of Guest bytes. */
265 void lgwrite(struct lguest *lg, unsigned long addr, const void *b,
268 if (!lguest_address_ok(lg, addr, bytes)
269 || copy_to_user((void __user *)addr, b, bytes) != 0)
270 kill_guest(lg, "bad write address %#lx len %u", addr, bytes);
272 /* (end of memory access helper routines) :*/
274 static void set_ts(void)
283 static void copy_in_guest_info(struct lguest *lg, struct lguest_pages *pages)
285 if (__get_cpu_var(last_guest) != lg || lg->last_pages != pages) {
286 __get_cpu_var(last_guest) = lg;
287 lg->last_pages = pages;
288 lg->changed = CHANGED_ALL;
291 /* These are pretty cheap, so we do them unconditionally. */
292 pages->state.host_cr3 = __pa(current->mm->pgd);
293 map_switcher_in_guest(lg, pages);
294 pages->state.guest_tss.esp1 = lg->esp1;
295 pages->state.guest_tss.ss1 = lg->ss1;
297 /* Copy direct trap entries. */
298 if (lg->changed & CHANGED_IDT)
299 copy_traps(lg, pages->state.guest_idt, default_idt_entries);
301 /* Copy all GDT entries but the TSS. */
302 if (lg->changed & CHANGED_GDT)
303 copy_gdt(lg, pages->state.guest_gdt);
304 /* If only the TLS entries have changed, copy them. */
305 else if (lg->changed & CHANGED_GDT_TLS)
306 copy_gdt_tls(lg, pages->state.guest_gdt);
311 static void run_guest_once(struct lguest *lg, struct lguest_pages *pages)
313 unsigned int clobber;
315 copy_in_guest_info(lg, pages);
317 /* Put eflags on stack, lcall does rest: suitable for iret return. */
318 asm volatile("pushf; lcall *lguest_entry"
319 : "=a"(clobber), "=b"(clobber)
320 : "0"(pages), "1"(__pa(lg->pgdirs[lg->pgdidx].pgdir))
321 : "memory", "%edx", "%ecx", "%edi", "%esi");
324 int run_guest(struct lguest *lg, unsigned long __user *user)
327 unsigned int cr2 = 0; /* Damn gcc */
329 /* Hypercalls first: we might have been out to userspace */
331 if (lg->dma_is_pending) {
332 if (put_user(lg->pending_dma, user) ||
333 put_user(lg->pending_key, user+1))
335 return sizeof(unsigned long)*2;
338 if (signal_pending(current))
341 /* If Waker set break_out, return to Launcher. */
345 maybe_do_interrupt(lg);
353 set_current_state(TASK_INTERRUPTIBLE);
360 /* Even if *we* don't want FPU trap, guest might... */
364 /* Don't let Guest do SYSENTER: we can't handle it. */
365 if (boot_cpu_has(X86_FEATURE_SEP))
366 wrmsr(MSR_IA32_SYSENTER_CS, 0, 0);
368 run_guest_once(lg, lguest_pages(raw_smp_processor_id()));
370 /* Save cr2 now if we page-faulted. */
371 if (lg->regs->trapnum == 14)
373 else if (lg->regs->trapnum == 7)
374 math_state_restore();
376 if (boot_cpu_has(X86_FEATURE_SEP))
377 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
380 switch (lg->regs->trapnum) {
381 case 13: /* We've intercepted a GPF. */
382 if (lg->regs->errcode == 0) {
383 if (emulate_insn(lg))
387 case 14: /* We've intercepted a page fault. */
388 if (demand_page(lg, cr2, lg->regs->errcode))
391 /* If lguest_data is NULL, this won't hurt. */
392 if (put_user(cr2, &lg->lguest_data->cr2))
393 kill_guest(lg, "Writing cr2");
395 case 7: /* We've intercepted a Device Not Available fault. */
396 /* If they don't want to know, just absorb it. */
400 case 32 ... 255: /* Real interrupt, fall thru */
402 case LGUEST_TRAP_ENTRY: /* Handled at top of loop */
406 if (deliver_trap(lg, lg->regs->trapnum))
409 kill_guest(lg, "unhandled trap %li at %#lx (%#lx)",
410 lg->regs->trapnum, lg->regs->eip,
411 lg->regs->trapnum == 14 ? cr2 : lg->regs->errcode);
416 int find_free_guest(void)
419 for (i = 0; i < MAX_LGUEST_GUESTS; i++)
425 static void adjust_pge(void *on)
428 write_cr4(read_cr4() | X86_CR4_PGE);
430 write_cr4(read_cr4() & ~X86_CR4_PGE);
433 static int __init init(void)
437 if (paravirt_enabled()) {
438 printk("lguest is afraid of %s\n", paravirt_ops.name);
442 err = map_switcher();
446 err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
453 err = lguest_device_init();
460 if (cpu_has_pge) { /* We have a broader idea of "global". */
462 on_each_cpu(adjust_pge, (void *)0, 0, 1);
463 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
465 unlock_cpu_hotplug();
469 static void __exit fini(void)
471 lguest_device_remove();
476 set_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
477 on_each_cpu(adjust_pge, (void *)1, 0, 1);
479 unlock_cpu_hotplug();
484 MODULE_LICENSE("GPL");
485 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");