core.c 11 KB

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