lguest_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher
  2. * controls and communicates with the Guest. For example, the first write will
  3. * tell us the Guest's memory layout, pagetable, entry point and kernel address
  4. * offset. A read will run the Guest until something happens, such as a signal
  5. * or the Guest doing a DMA out to the Launcher. Writes are also used to get a
  6. * DMA buffer registered by the Guest and to send the Guest an interrupt. :*/
  7. #include <linux/uaccess.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/fs.h>
  10. #include "lg.h"
  11. /*L:030 setup_regs() doesn't really belong in this file, but it gives us an
  12. * early glimpse deeper into the Host so it's worth having here.
  13. *
  14. * Most of the Guest's registers are left alone: we used get_zeroed_page() to
  15. * allocate the structure, so they will be 0. */
  16. static void setup_regs(struct lguest_regs *regs, unsigned long start)
  17. {
  18. /* There are four "segment" registers which the Guest needs to boot:
  19. * The "code segment" register (cs) refers to the kernel code segment
  20. * __KERNEL_CS, and the "data", "extra" and "stack" segment registers
  21. * refer to the kernel data segment __KERNEL_DS.
  22. *
  23. * The privilege level is packed into the lower bits. The Guest runs
  24. * at privilege level 1 (GUEST_PL).*/
  25. regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL;
  26. regs->cs = __KERNEL_CS|GUEST_PL;
  27. /* The "eflags" register contains miscellaneous flags. Bit 1 (0x002)
  28. * is supposed to always be "1". Bit 9 (0x200) controls whether
  29. * interrupts are enabled. We always leave interrupts enabled while
  30. * running the Guest. */
  31. regs->eflags = 0x202;
  32. /* The "Extended Instruction Pointer" register says where the Guest is
  33. * running. */
  34. regs->eip = start;
  35. /* %esi points to our boot information, at physical address 0, so don't
  36. * touch it. */
  37. }
  38. /*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a
  39. * DMA buffer. This is done by writing LHREQ_GETDMA and the key to
  40. * /dev/lguest. */
  41. static long user_get_dma(struct lguest *lg, const u32 __user *input)
  42. {
  43. unsigned long key, udma, irq;
  44. /* Fetch the key they wrote to us. */
  45. if (get_user(key, input) != 0)
  46. return -EFAULT;
  47. /* Look for a free Guest DMA buffer bound to that key. */
  48. udma = get_dma_buffer(lg, key, &irq);
  49. if (!udma)
  50. return -ENOENT;
  51. /* We need to tell the Launcher what interrupt the Guest expects after
  52. * the buffer is filled. We stash it in udma->used_len. */
  53. lgwrite_u32(lg, udma + offsetof(struct lguest_dma, used_len), irq);
  54. /* The (guest-physical) address of the DMA buffer is returned from
  55. * the write(). */
  56. return udma;
  57. }
  58. /*L:315 To force the Guest to stop running and return to the Launcher, the
  59. * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The
  60. * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */
  61. static int break_guest_out(struct lguest *lg, const u32 __user *input)
  62. {
  63. unsigned long on;
  64. /* Fetch whether they're turning break on or off.. */
  65. if (get_user(on, input) != 0)
  66. return -EFAULT;
  67. if (on) {
  68. lg->break_out = 1;
  69. /* Pop it out (may be running on different CPU) */
  70. wake_up_process(lg->tsk);
  71. /* Wait for them to reset it */
  72. return wait_event_interruptible(lg->break_wq, !lg->break_out);
  73. } else {
  74. lg->break_out = 0;
  75. wake_up(&lg->break_wq);
  76. return 0;
  77. }
  78. }
  79. /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
  80. * number to /dev/lguest. */
  81. static int user_send_irq(struct lguest *lg, const u32 __user *input)
  82. {
  83. u32 irq;
  84. if (get_user(irq, input) != 0)
  85. return -EFAULT;
  86. if (irq >= LGUEST_IRQS)
  87. return -EINVAL;
  88. /* Next time the Guest runs, the core code will see if it can deliver
  89. * this interrupt. */
  90. set_bit(irq, lg->irqs_pending);
  91. return 0;
  92. }
  93. /*L:040 Once our Guest is initialized, the Launcher makes it run by reading
  94. * from /dev/lguest. */
  95. static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
  96. {
  97. struct lguest *lg = file->private_data;
  98. /* You must write LHREQ_INITIALIZE first! */
  99. if (!lg)
  100. return -EINVAL;
  101. /* If you're not the task which owns the guest, go away. */
  102. if (current != lg->tsk)
  103. return -EPERM;
  104. /* If the guest is already dead, we indicate why */
  105. if (lg->dead) {
  106. size_t len;
  107. /* lg->dead either contains an error code, or a string. */
  108. if (IS_ERR(lg->dead))
  109. return PTR_ERR(lg->dead);
  110. /* We can only return as much as the buffer they read with. */
  111. len = min(size, strlen(lg->dead)+1);
  112. if (copy_to_user(user, lg->dead, len) != 0)
  113. return -EFAULT;
  114. return len;
  115. }
  116. /* If we returned from read() last time because the Guest sent DMA,
  117. * clear the flag. */
  118. if (lg->dma_is_pending)
  119. lg->dma_is_pending = 0;
  120. /* Run the Guest until something interesting happens. */
  121. return run_guest(lg, (unsigned long __user *)user);
  122. }
  123. /*L:020 The initialization write supplies 5 32-bit values (in addition to the
  124. * 32-bit LHREQ_INITIALIZE value). These are:
  125. *
  126. * base: The start of the Guest-physical memory inside the Launcher memory.
  127. *
  128. * pfnlimit: The highest (Guest-physical) page number the Guest should be
  129. * allowed to access. The Launcher has to live in Guest memory, so it sets
  130. * this to ensure the Guest can't reach it.
  131. *
  132. * pgdir: The (Guest-physical) address of the top of the initial Guest
  133. * pagetables (which are set up by the Launcher).
  134. *
  135. * start: The first instruction to execute ("eip" in x86-speak).
  136. *
  137. * page_offset: The PAGE_OFFSET constant in the Guest kernel. We should
  138. * probably wean the code off this, but it's a very useful constant! Any
  139. * address above this is within the Guest kernel, and any kernel address can
  140. * quickly converted from physical to virtual by adding PAGE_OFFSET. It's
  141. * 0xC0000000 (3G) by default, but it's configurable at kernel build time.
  142. */
  143. static int initialize(struct file *file, const u32 __user *input)
  144. {
  145. /* "struct lguest" contains everything we (the Host) know about a
  146. * Guest. */
  147. struct lguest *lg;
  148. int err, i;
  149. u32 args[5];
  150. /* We grab the Big Lguest lock, which protects the global array
  151. * "lguests" and multiple simultaneous initializations. */
  152. mutex_lock(&lguest_lock);
  153. /* You can't initialize twice! Close the device and start again... */
  154. if (file->private_data) {
  155. err = -EBUSY;
  156. goto unlock;
  157. }
  158. if (copy_from_user(args, input, sizeof(args)) != 0) {
  159. err = -EFAULT;
  160. goto unlock;
  161. }
  162. /* Find an unused guest. */
  163. i = find_free_guest();
  164. if (i < 0) {
  165. err = -ENOSPC;
  166. goto unlock;
  167. }
  168. /* OK, we have an index into the "lguest" array: "lg" is a convenient
  169. * pointer. */
  170. lg = &lguests[i];
  171. /* Populate the easy fields of our "struct lguest" */
  172. lg->guestid = i;
  173. lg->mem_base = (void __user *)(long)args[0];
  174. lg->pfn_limit = args[1];
  175. lg->page_offset = args[4];
  176. /* We need a complete page for the Guest registers: they are accessible
  177. * to the Guest and we can only grant it access to whole pages. */
  178. lg->regs_page = get_zeroed_page(GFP_KERNEL);
  179. if (!lg->regs_page) {
  180. err = -ENOMEM;
  181. goto release_guest;
  182. }
  183. /* We actually put the registers at the bottom of the page. */
  184. lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
  185. /* Initialize the Guest's shadow page tables, using the toplevel
  186. * address the Launcher gave us. This allocates memory, so can
  187. * fail. */
  188. err = init_guest_pagetable(lg, args[2]);
  189. if (err)
  190. goto free_regs;
  191. /* Now we initialize the Guest's registers, handing it the start
  192. * address. */
  193. setup_regs(lg->regs, args[3]);
  194. /* There are a couple of GDT entries the Guest expects when first
  195. * booting. */
  196. setup_guest_gdt(lg);
  197. /* The timer for lguest's clock needs initialization. */
  198. init_clockdev(lg);
  199. /* We keep a pointer to the Launcher task (ie. current task) for when
  200. * other Guests want to wake this one (inter-Guest I/O). */
  201. lg->tsk = current;
  202. /* We need to keep a pointer to the Launcher's memory map, because if
  203. * the Launcher dies we need to clean it up. If we don't keep a
  204. * reference, it is destroyed before close() is called. */
  205. lg->mm = get_task_mm(lg->tsk);
  206. /* Initialize the queue for the waker to wait on */
  207. init_waitqueue_head(&lg->break_wq);
  208. /* We remember which CPU's pages this Guest used last, for optimization
  209. * when the same Guest runs on the same CPU twice. */
  210. lg->last_pages = NULL;
  211. /* We keep our "struct lguest" in the file's private_data. */
  212. file->private_data = lg;
  213. mutex_unlock(&lguest_lock);
  214. /* And because this is a write() call, we return the length used. */
  215. return sizeof(args);
  216. free_regs:
  217. free_page(lg->regs_page);
  218. release_guest:
  219. memset(lg, 0, sizeof(*lg));
  220. unlock:
  221. mutex_unlock(&lguest_lock);
  222. return err;
  223. }
  224. /*L:010 The first operation the Launcher does must be a write. All writes
  225. * start with a 32 bit number: for the first write this must be
  226. * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
  227. * writes of other values to get DMA buffers and send interrupts. */
  228. static ssize_t write(struct file *file, const char __user *input,
  229. size_t size, loff_t *off)
  230. {
  231. /* Once the guest is initialized, we hold the "struct lguest" in the
  232. * file private data. */
  233. struct lguest *lg = file->private_data;
  234. u32 req;
  235. if (get_user(req, input) != 0)
  236. return -EFAULT;
  237. input += sizeof(req);
  238. /* If you haven't initialized, you must do that first. */
  239. if (req != LHREQ_INITIALIZE && !lg)
  240. return -EINVAL;
  241. /* Once the Guest is dead, all you can do is read() why it died. */
  242. if (lg && lg->dead)
  243. return -ENOENT;
  244. /* If you're not the task which owns the Guest, you can only break */
  245. if (lg && current != lg->tsk && req != LHREQ_BREAK)
  246. return -EPERM;
  247. switch (req) {
  248. case LHREQ_INITIALIZE:
  249. return initialize(file, (const u32 __user *)input);
  250. case LHREQ_GETDMA:
  251. return user_get_dma(lg, (const u32 __user *)input);
  252. case LHREQ_IRQ:
  253. return user_send_irq(lg, (const u32 __user *)input);
  254. case LHREQ_BREAK:
  255. return break_guest_out(lg, (const u32 __user *)input);
  256. default:
  257. return -EINVAL;
  258. }
  259. }
  260. /*L:060 The final piece of interface code is the close() routine. It reverses
  261. * everything done in initialize(). This is usually called because the
  262. * Launcher exited.
  263. *
  264. * Note that the close routine returns 0 or a negative error number: it can't
  265. * really fail, but it can whine. I blame Sun for this wart, and K&R C for
  266. * letting them do it. :*/
  267. static int close(struct inode *inode, struct file *file)
  268. {
  269. struct lguest *lg = file->private_data;
  270. /* If we never successfully initialized, there's nothing to clean up */
  271. if (!lg)
  272. return 0;
  273. /* We need the big lock, to protect from inter-guest I/O and other
  274. * Launchers initializing guests. */
  275. mutex_lock(&lguest_lock);
  276. /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
  277. hrtimer_cancel(&lg->hrt);
  278. /* Free any DMA buffers the Guest had bound. */
  279. release_all_dma(lg);
  280. /* Free up the shadow page tables for the Guest. */
  281. free_guest_pagetable(lg);
  282. /* Now all the memory cleanups are done, it's safe to release the
  283. * Launcher's memory management structure. */
  284. mmput(lg->mm);
  285. /* If lg->dead doesn't contain an error code it will be NULL or a
  286. * kmalloc()ed string, either of which is ok to hand to kfree(). */
  287. if (!IS_ERR(lg->dead))
  288. kfree(lg->dead);
  289. /* We can free up the register page we allocated. */
  290. free_page(lg->regs_page);
  291. /* We clear the entire structure, which also marks it as free for the
  292. * next user. */
  293. memset(lg, 0, sizeof(*lg));
  294. /* Release lock and exit. */
  295. mutex_unlock(&lguest_lock);
  296. return 0;
  297. }
  298. /*L:000
  299. * Welcome to our journey through the Launcher!
  300. *
  301. * The Launcher is the Host userspace program which sets up, runs and services
  302. * the Guest. In fact, many comments in the Drivers which refer to "the Host"
  303. * doing things are inaccurate: the Launcher does all the device handling for
  304. * the Guest. The Guest can't tell what's done by the the Launcher and what by
  305. * the Host.
  306. *
  307. * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
  308. * shall see more of that later.
  309. *
  310. * We begin our understanding with the Host kernel interface which the Launcher
  311. * uses: reading and writing a character device called /dev/lguest. All the
  312. * work happens in the read(), write() and close() routines: */
  313. static struct file_operations lguest_fops = {
  314. .owner = THIS_MODULE,
  315. .release = close,
  316. .write = write,
  317. .read = read,
  318. };
  319. /* This is a textbook example of a "misc" character device. Populate a "struct
  320. * miscdevice" and register it with misc_register(). */
  321. static struct miscdevice lguest_dev = {
  322. .minor = MISC_DYNAMIC_MINOR,
  323. .name = "lguest",
  324. .fops = &lguest_fops,
  325. };
  326. int __init lguest_device_init(void)
  327. {
  328. return misc_register(&lguest_dev);
  329. }
  330. void __exit lguest_device_remove(void)
  331. {
  332. misc_deregister(&lguest_dev);
  333. }