lguest_user.c 9.5 KB

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