lguest_user.c 11 KB

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