lguest_user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 sent I/O,
  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. /*L:025 This actually initializes a CPU. For the moment, a Guest is only
  85. * uniprocessor, so "id" is always 0. */
  86. static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
  87. {
  88. /* We have a limited number the number of CPUs in the lguest struct. */
  89. if (id >= ARRAY_SIZE(cpu->lg->cpus))
  90. return -EINVAL;
  91. /* Set up this CPU's id, and pointer back to the lguest struct. */
  92. cpu->id = id;
  93. cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
  94. cpu->lg->nr_cpus++;
  95. /* Each CPU has a timer it can set. */
  96. init_clockdev(cpu);
  97. /* We need a complete page for the Guest registers: they are accessible
  98. * to the Guest and we can only grant it access to whole pages. */
  99. cpu->regs_page = get_zeroed_page(GFP_KERNEL);
  100. if (!cpu->regs_page)
  101. return -ENOMEM;
  102. /* We actually put the registers at the bottom of the page. */
  103. cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
  104. /* Now we initialize the Guest's registers, handing it the start
  105. * address. */
  106. lguest_arch_setup_regs(cpu, start_ip);
  107. /* Initialize the queue for the Waker to wait on */
  108. init_waitqueue_head(&cpu->break_wq);
  109. /* We keep a pointer to the Launcher task (ie. current task) for when
  110. * other Guests want to wake this one (eg. console input). */
  111. cpu->tsk = current;
  112. /* We need to keep a pointer to the Launcher's memory map, because if
  113. * the Launcher dies we need to clean it up. If we don't keep a
  114. * reference, it is destroyed before close() is called. */
  115. cpu->mm = get_task_mm(cpu->tsk);
  116. /* We remember which CPU's pages this Guest used last, for optimization
  117. * when the same Guest runs on the same CPU twice. */
  118. cpu->last_pages = NULL;
  119. /* No error == success. */
  120. return 0;
  121. }
  122. /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
  123. * values (in addition to the LHREQ_INITIALIZE value). These are:
  124. *
  125. * base: The start of the Guest-physical memory inside the Launcher memory.
  126. *
  127. * pfnlimit: The highest (Guest-physical) page number the Guest should be
  128. * allowed to access. The Guest memory lives inside the Launcher, so it sets
  129. * this to ensure the Guest can only reach its own memory.
  130. *
  131. * pgdir: The (Guest-physical) address of the top of the initial Guest
  132. * pagetables (which are set up by the Launcher).
  133. *
  134. * start: The first instruction to execute ("eip" in x86-speak).
  135. */
  136. static int initialize(struct file *file, const unsigned long __user *input)
  137. {
  138. /* "struct lguest" contains everything we (the Host) know about a
  139. * Guest. */
  140. struct lguest *lg;
  141. int err;
  142. unsigned long args[4];
  143. /* We grab the Big Lguest lock, which protects against multiple
  144. * simultaneous initializations. */
  145. mutex_lock(&lguest_lock);
  146. /* You can't initialize twice! Close the device and start again... */
  147. if (file->private_data) {
  148. err = -EBUSY;
  149. goto unlock;
  150. }
  151. if (copy_from_user(args, input, sizeof(args)) != 0) {
  152. err = -EFAULT;
  153. goto unlock;
  154. }
  155. lg = kzalloc(sizeof(*lg), GFP_KERNEL);
  156. if (!lg) {
  157. err = -ENOMEM;
  158. goto unlock;
  159. }
  160. /* Populate the easy fields of our "struct lguest" */
  161. lg->mem_base = (void __user *)args[0];
  162. lg->pfn_limit = args[1];
  163. /* This is the first cpu (cpu 0) and it will start booting at args[3] */
  164. err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
  165. if (err)
  166. goto release_guest;
  167. /* Initialize the Guest's shadow page tables, using the toplevel
  168. * address the Launcher gave us. This allocates memory, so can fail. */
  169. err = init_guest_pagetable(lg, args[2]);
  170. if (err)
  171. goto free_regs;
  172. /* We keep our "struct lguest" in the file's private_data. */
  173. file->private_data = lg;
  174. mutex_unlock(&lguest_lock);
  175. /* And because this is a write() call, we return the length used. */
  176. return sizeof(args);
  177. free_regs:
  178. /* FIXME: This should be in free_vcpu */
  179. free_page(lg->cpus[0].regs_page);
  180. release_guest:
  181. kfree(lg);
  182. unlock:
  183. mutex_unlock(&lguest_lock);
  184. return err;
  185. }
  186. /*L:010 The first operation the Launcher does must be a write. All writes
  187. * start with an unsigned long number: for the first write this must be
  188. * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
  189. * writes of other values to send interrupts.
  190. *
  191. * Note that we overload the "offset" in the /dev/lguest file to indicate what
  192. * CPU number we're dealing with. Currently this is always 0, since we only
  193. * support uniprocessor Guests, but you can see the beginnings of SMP support
  194. * here. */
  195. static ssize_t write(struct file *file, const char __user *in,
  196. size_t size, loff_t *off)
  197. {
  198. /* Once the Guest is initialized, we hold the "struct lguest" in the
  199. * file private data. */
  200. struct lguest *lg = file->private_data;
  201. const unsigned long __user *input = (const unsigned long __user *)in;
  202. unsigned long req;
  203. struct lg_cpu *uninitialized_var(cpu);
  204. unsigned int cpu_id = *off;
  205. /* The first value tells us what this request is. */
  206. if (get_user(req, input) != 0)
  207. return -EFAULT;
  208. input++;
  209. /* If you haven't initialized, you must do that first. */
  210. if (req != LHREQ_INITIALIZE) {
  211. if (!lg || (cpu_id >= lg->nr_cpus))
  212. return -EINVAL;
  213. cpu = &lg->cpus[cpu_id];
  214. /* Once the Guest is dead, you can only read() why it died. */
  215. if (lg->dead)
  216. return -ENOENT;
  217. /* If you're not the task which owns the Guest, all you can do
  218. * is break the Launcher out of running the Guest. */
  219. if (current != cpu->tsk && req != LHREQ_BREAK)
  220. return -EPERM;
  221. }
  222. switch (req) {
  223. case LHREQ_INITIALIZE:
  224. return initialize(file, input);
  225. case LHREQ_IRQ:
  226. return user_send_irq(cpu, input);
  227. case LHREQ_BREAK:
  228. return break_guest_out(cpu, input);
  229. default:
  230. return -EINVAL;
  231. }
  232. }
  233. /*L:060 The final piece of interface code is the close() routine. It reverses
  234. * everything done in initialize(). This is usually called because the
  235. * Launcher exited.
  236. *
  237. * Note that the close routine returns 0 or a negative error number: it can't
  238. * really fail, but it can whine. I blame Sun for this wart, and K&R C for
  239. * letting them do it. :*/
  240. static int close(struct inode *inode, struct file *file)
  241. {
  242. struct lguest *lg = file->private_data;
  243. unsigned int i;
  244. /* If we never successfully initialized, there's nothing to clean up */
  245. if (!lg)
  246. return 0;
  247. /* We need the big lock, to protect from inter-guest I/O and other
  248. * Launchers initializing guests. */
  249. mutex_lock(&lguest_lock);
  250. /* Free up the shadow page tables for the Guest. */
  251. free_guest_pagetable(lg);
  252. for (i = 0; i < lg->nr_cpus; i++) {
  253. /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
  254. hrtimer_cancel(&lg->cpus[i].hrt);
  255. /* We can free up the register page we allocated. */
  256. free_page(lg->cpus[i].regs_page);
  257. /* Now all the memory cleanups are done, it's safe to release
  258. * the Launcher's memory management structure. */
  259. mmput(lg->cpus[i].mm);
  260. }
  261. /* If lg->dead doesn't contain an error code it will be NULL or a
  262. * kmalloc()ed string, either of which is ok to hand to kfree(). */
  263. if (!IS_ERR(lg->dead))
  264. kfree(lg->dead);
  265. /* We clear the entire structure, which also marks it as free for the
  266. * next user. */
  267. memset(lg, 0, sizeof(*lg));
  268. /* Release lock and exit. */
  269. mutex_unlock(&lguest_lock);
  270. return 0;
  271. }
  272. /*L:000
  273. * Welcome to our journey through the Launcher!
  274. *
  275. * The Launcher is the Host userspace program which sets up, runs and services
  276. * the Guest. In fact, many comments in the Drivers which refer to "the Host"
  277. * doing things are inaccurate: the Launcher does all the device handling for
  278. * the Guest, but the Guest can't know that.
  279. *
  280. * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
  281. * shall see more of that later.
  282. *
  283. * We begin our understanding with the Host kernel interface which the Launcher
  284. * uses: reading and writing a character device called /dev/lguest. All the
  285. * work happens in the read(), write() and close() routines: */
  286. static struct file_operations lguest_fops = {
  287. .owner = THIS_MODULE,
  288. .release = close,
  289. .write = write,
  290. .read = read,
  291. };
  292. /* This is a textbook example of a "misc" character device. Populate a "struct
  293. * miscdevice" and register it with misc_register(). */
  294. static struct miscdevice lguest_dev = {
  295. .minor = MISC_DYNAMIC_MINOR,
  296. .name = "lguest",
  297. .fops = &lguest_fops,
  298. };
  299. int __init lguest_device_init(void)
  300. {
  301. return misc_register(&lguest_dev);
  302. }
  303. void __exit lguest_device_remove(void)
  304. {
  305. misc_deregister(&lguest_dev);
  306. }