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;
211 int lguest_address_ok(const struct lguest *lg,
212 unsigned long addr, unsigned long len)
214 return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
217 /* Just like get_user, but don't let guest access lguest binary. */
218 u32 lgread_u32(struct lguest *lg, unsigned long addr)
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);
229 void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val)
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);
236 void lgread(struct lguest *lg, void *b, unsigned long addr, unsigned bytes)
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... */
242 kill_guest(lg, "bad read address %#lx len %u", addr, bytes);
246 void lgwrite(struct lguest *lg, unsigned long addr, const void *b,
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);
254 static void set_ts(void)
263 static void copy_in_guest_info(struct lguest *lg, struct lguest_pages *pages)
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;
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;
277 /* Copy direct trap entries. */
278 if (lg->changed & CHANGED_IDT)
279 copy_traps(lg, pages->state.guest_idt, default_idt_entries);
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);
291 static void run_guest_once(struct lguest *lg, struct lguest_pages *pages)
293 unsigned int clobber;
295 copy_in_guest_info(lg, pages);
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");
304 int run_guest(struct lguest *lg, unsigned long __user *user)
307 unsigned int cr2 = 0; /* Damn gcc */
309 /* Hypercalls first: we might have been out to userspace */
311 if (lg->dma_is_pending) {
312 if (put_user(lg->pending_dma, user) ||
313 put_user(lg->pending_key, user+1))
315 return sizeof(unsigned long)*2;
318 if (signal_pending(current))
321 /* If Waker set break_out, return to Launcher. */
325 maybe_do_interrupt(lg);
333 set_current_state(TASK_INTERRUPTIBLE);
340 /* Even if *we* don't want FPU trap, guest might... */
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);
348 run_guest_once(lg, lguest_pages(raw_smp_processor_id()));
350 /* Save cr2 now if we page-faulted. */
351 if (lg->regs->trapnum == 14)
353 else if (lg->regs->trapnum == 7)
354 math_state_restore();
356 if (boot_cpu_has(X86_FEATURE_SEP))
357 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
360 switch (lg->regs->trapnum) {
361 case 13: /* We've intercepted a GPF. */
362 if (lg->regs->errcode == 0) {
363 if (emulate_insn(lg))
367 case 14: /* We've intercepted a page fault. */
368 if (demand_page(lg, cr2, lg->regs->errcode))
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");
375 case 7: /* We've intercepted a Device Not Available fault. */
376 /* If they don't want to know, just absorb it. */
380 case 32 ... 255: /* Real interrupt, fall thru */
382 case LGUEST_TRAP_ENTRY: /* Handled at top of loop */
386 if (deliver_trap(lg, lg->regs->trapnum))
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);
396 int find_free_guest(void)
399 for (i = 0; i < MAX_LGUEST_GUESTS; i++)
405 static void adjust_pge(void *on)
408 write_cr4(read_cr4() | X86_CR4_PGE);
410 write_cr4(read_cr4() & ~X86_CR4_PGE);
413 static int __init init(void)
417 if (paravirt_enabled()) {
418 printk("lguest is afraid of %s\n", paravirt_ops.name);
422 err = map_switcher();
426 err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
433 err = lguest_device_init();
440 if (cpu_has_pge) { /* We have a broader idea of "global". */
442 on_each_cpu(adjust_pge, (void *)0, 0, 1);
443 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
445 unlock_cpu_hotplug();
449 static void __exit fini(void)
451 lguest_device_remove();
456 set_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
457 on_each_cpu(adjust_pge, (void *)1, 0, 1);
459 unlock_cpu_hotplug();
464 MODULE_LICENSE("GPL");
465 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");