lguest_user.c 10 KB

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