lguest_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 memory size, pagetable, entry point and kernel address offset.
  4. * A read will run the Guest until a signal is pending (-EINTR), or the Guest
  5. * does a DMA out to the Launcher. Writes are also used to get a DMA buffer
  6. * 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 4 32-bit values (in addition to the
  124. * 32-bit LHREQ_INITIALIZE value). These are:
  125. *
  126. * pfnlimit: The highest (Guest-physical) page number the Guest should be
  127. * allowed to access. The Launcher has to live in Guest memory, so it sets
  128. * this to ensure the Guest can't reach it.
  129. *
  130. * pgdir: The (Guest-physical) address of the top of the initial Guest
  131. * pagetables (which are set up by the Launcher).
  132. *
  133. * start: The first instruction to execute ("eip" in x86-speak).
  134. *
  135. * page_offset: The PAGE_OFFSET constant in the Guest kernel. We should
  136. * probably wean the code off this, but it's a very useful constant! Any
  137. * address above this is within the Guest kernel, and any kernel address can
  138. * quickly converted from physical to virtual by adding PAGE_OFFSET. It's
  139. * 0xC0000000 (3G) by default, but it's configurable at kernel build time.
  140. */
  141. static int initialize(struct file *file, const u32 __user *input)
  142. {
  143. /* "struct lguest" contains everything we (the Host) know about a
  144. * Guest. */
  145. struct lguest *lg;
  146. int err, i;
  147. u32 args[4];
  148. /* We grab the Big Lguest lock, which protects the global array
  149. * "lguests" and multiple simultaneous initializations. */
  150. mutex_lock(&lguest_lock);
  151. /* You can't initialize twice! Close the device and start again... */
  152. if (file->private_data) {
  153. err = -EBUSY;
  154. goto unlock;
  155. }
  156. if (copy_from_user(args, input, sizeof(args)) != 0) {
  157. err = -EFAULT;
  158. goto unlock;
  159. }
  160. /* Find an unused guest. */
  161. i = find_free_guest();
  162. if (i < 0) {
  163. err = -ENOSPC;
  164. goto unlock;
  165. }
  166. /* OK, we have an index into the "lguest" array: "lg" is a convenient
  167. * pointer. */
  168. lg = &lguests[i];
  169. /* Populate the easy fields of our "struct lguest" */
  170. lg->guestid = i;
  171. lg->pfn_limit = args[0];
  172. lg->page_offset = args[3];
  173. /* We need a complete page for the Guest registers: they are accessible
  174. * to the Guest and we can only grant it access to whole pages. */
  175. lg->regs_page = get_zeroed_page(GFP_KERNEL);
  176. if (!lg->regs_page) {
  177. err = -ENOMEM;
  178. goto release_guest;
  179. }
  180. /* We actually put the registers at the bottom of the page. */
  181. lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
  182. /* Initialize the Guest's shadow page tables, using the toplevel
  183. * address the Launcher gave us. This allocates memory, so can
  184. * fail. */
  185. err = init_guest_pagetable(lg, args[1]);
  186. if (err)
  187. goto free_regs;
  188. /* Now we initialize the Guest's registers, handing it the start
  189. * address. */
  190. setup_regs(lg->regs, args[2]);
  191. /* There are a couple of GDT entries the Guest expects when first
  192. * booting. */
  193. setup_guest_gdt(lg);
  194. /* The timer for lguest's clock needs initialization. */
  195. init_clockdev(lg);
  196. /* We keep a pointer to the Launcher task (ie. current task) for when
  197. * other Guests want to wake this one (inter-Guest I/O). */
  198. lg->tsk = current;
  199. /* We need to keep a pointer to the Launcher's memory map, because if
  200. * the Launcher dies we need to clean it up. If we don't keep a
  201. * reference, it is destroyed before close() is called. */
  202. lg->mm = get_task_mm(lg->tsk);
  203. /* Initialize the queue for the waker to wait on */
  204. init_waitqueue_head(&lg->break_wq);
  205. /* We remember which CPU's pages this Guest used last, for optimization
  206. * when the same Guest runs on the same CPU twice. */
  207. lg->last_pages = NULL;
  208. /* We keep our "struct lguest" in the file's private_data. */
  209. file->private_data = lg;
  210. mutex_unlock(&lguest_lock);
  211. /* And because this is a write() call, we return the length used. */
  212. return sizeof(args);
  213. free_regs:
  214. free_page(lg->regs_page);
  215. release_guest:
  216. memset(lg, 0, sizeof(*lg));
  217. unlock:
  218. mutex_unlock(&lguest_lock);
  219. return err;
  220. }
  221. /*L:010 The first operation the Launcher does must be a write. All writes
  222. * start with a 32 bit number: for the first write this must be
  223. * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
  224. * writes of other values to get DMA buffers and send interrupts. */
  225. static ssize_t write(struct file *file, const char __user *input,
  226. size_t size, loff_t *off)
  227. {
  228. /* Once the guest is initialized, we hold the "struct lguest" in the
  229. * file private data. */
  230. struct lguest *lg = file->private_data;
  231. u32 req;
  232. if (get_user(req, input) != 0)
  233. return -EFAULT;
  234. input += sizeof(req);
  235. /* If you haven't initialized, you must do that first. */
  236. if (req != LHREQ_INITIALIZE && !lg)
  237. return -EINVAL;
  238. /* Once the Guest is dead, all you can do is read() why it died. */
  239. if (lg && lg->dead)
  240. return -ENOENT;
  241. /* If you're not the task which owns the Guest, you can only break */
  242. if (lg && current != lg->tsk && req != LHREQ_BREAK)
  243. return -EPERM;
  244. switch (req) {
  245. case LHREQ_INITIALIZE:
  246. return initialize(file, (const u32 __user *)input);
  247. case LHREQ_GETDMA:
  248. return user_get_dma(lg, (const u32 __user *)input);
  249. case LHREQ_IRQ:
  250. return user_send_irq(lg, (const u32 __user *)input);
  251. case LHREQ_BREAK:
  252. return break_guest_out(lg, (const u32 __user *)input);
  253. default:
  254. return -EINVAL;
  255. }
  256. }
  257. /*L:060 The final piece of interface code is the close() routine. It reverses
  258. * everything done in initialize(). This is usually called because the
  259. * Launcher exited.
  260. *
  261. * Note that the close routine returns 0 or a negative error number: it can't
  262. * really fail, but it can whine. I blame Sun for this wart, and K&R C for
  263. * letting them do it. :*/
  264. static int close(struct inode *inode, struct file *file)
  265. {
  266. struct lguest *lg = file->private_data;
  267. /* If we never successfully initialized, there's nothing to clean up */
  268. if (!lg)
  269. return 0;
  270. /* We need the big lock, to protect from inter-guest I/O and other
  271. * Launchers initializing guests. */
  272. mutex_lock(&lguest_lock);
  273. /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
  274. hrtimer_cancel(&lg->hrt);
  275. /* Free any DMA buffers the Guest had bound. */
  276. release_all_dma(lg);
  277. /* Free up the shadow page tables for the Guest. */
  278. free_guest_pagetable(lg);
  279. /* Now all the memory cleanups are done, it's safe to release the
  280. * Launcher's memory management structure. */
  281. mmput(lg->mm);
  282. /* If lg->dead doesn't contain an error code it will be NULL or a
  283. * kmalloc()ed string, either of which is ok to hand to kfree(). */
  284. if (!IS_ERR(lg->dead))
  285. kfree(lg->dead);
  286. /* We can free up the register page we allocated. */
  287. free_page(lg->regs_page);
  288. /* We clear the entire structure, which also marks it as free for the
  289. * next user. */
  290. memset(lg, 0, sizeof(*lg));
  291. /* Release lock and exit. */
  292. mutex_unlock(&lguest_lock);
  293. return 0;
  294. }
  295. /*L:000
  296. * Welcome to our journey through the Launcher!
  297. *
  298. * The Launcher is the Host userspace program which sets up, runs and services
  299. * the Guest. In fact, many comments in the Drivers which refer to "the Host"
  300. * doing things are inaccurate: the Launcher does all the device handling for
  301. * the Guest. The Guest can't tell what's done by the the Launcher and what by
  302. * the Host.
  303. *
  304. * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
  305. * shall see more of that later.
  306. *
  307. * We begin our understanding with the Host kernel interface which the Launcher
  308. * uses: reading and writing a character device called /dev/lguest. All the
  309. * work happens in the read(), write() and close() routines: */
  310. static struct file_operations lguest_fops = {
  311. .owner = THIS_MODULE,
  312. .release = close,
  313. .write = write,
  314. .read = read,
  315. };
  316. /* This is a textbook example of a "misc" character device. Populate a "struct
  317. * miscdevice" and register it with misc_register(). */
  318. static struct miscdevice lguest_dev = {
  319. .minor = MISC_DYNAMIC_MINOR,
  320. .name = "lguest",
  321. .fops = &lguest_fops,
  322. };
  323. int __init lguest_device_init(void)
  324. {
  325. return misc_register(&lguest_dev);
  326. }
  327. void __exit lguest_device_remove(void)
  328. {
  329. misc_deregister(&lguest_dev);
  330. }