core.c 12 KB

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