lguest_user.c 10 KB

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