lguest_user.c 12 KB

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