lguest_user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 3 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. * start: The first instruction to execute ("eip" in x86-speak).
  132. */
  133. static int initialize(struct file *file, const unsigned long __user *input)
  134. {
  135. /* "struct lguest" contains everything we (the Host) know about a
  136. * Guest. */
  137. struct lguest *lg;
  138. int err;
  139. unsigned long args[3];
  140. /* We grab the Big Lguest lock, which protects against multiple
  141. * simultaneous initializations. */
  142. mutex_lock(&lguest_lock);
  143. /* You can't initialize twice! Close the device and start again... */
  144. if (file->private_data) {
  145. err = -EBUSY;
  146. goto unlock;
  147. }
  148. if (copy_from_user(args, input, sizeof(args)) != 0) {
  149. err = -EFAULT;
  150. goto unlock;
  151. }
  152. lg = kzalloc(sizeof(*lg), GFP_KERNEL);
  153. if (!lg) {
  154. err = -ENOMEM;
  155. goto unlock;
  156. }
  157. /* Populate the easy fields of our "struct lguest" */
  158. lg->mem_base = (void __user *)args[0];
  159. lg->pfn_limit = args[1];
  160. /* This is the first cpu (cpu 0) and it will start booting at args[2] */
  161. err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
  162. if (err)
  163. goto release_guest;
  164. /* Initialize the Guest's shadow page tables, using the toplevel
  165. * address the Launcher gave us. This allocates memory, so can fail. */
  166. err = init_guest_pagetable(lg);
  167. if (err)
  168. goto free_regs;
  169. /* We keep our "struct lguest" in the file's private_data. */
  170. file->private_data = lg;
  171. mutex_unlock(&lguest_lock);
  172. /* And because this is a write() call, we return the length used. */
  173. return sizeof(args);
  174. free_regs:
  175. /* FIXME: This should be in free_vcpu */
  176. free_page(lg->cpus[0].regs_page);
  177. release_guest:
  178. kfree(lg);
  179. unlock:
  180. mutex_unlock(&lguest_lock);
  181. return err;
  182. }
  183. /*L:010 The first operation the Launcher does must be a write. All writes
  184. * start with an unsigned long number: for the first write this must be
  185. * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
  186. * writes of other values to send interrupts.
  187. *
  188. * Note that we overload the "offset" in the /dev/lguest file to indicate what
  189. * CPU number we're dealing with. Currently this is always 0, since we only
  190. * support uniprocessor Guests, but you can see the beginnings of SMP support
  191. * here. */
  192. static ssize_t write(struct file *file, const char __user *in,
  193. size_t size, loff_t *off)
  194. {
  195. /* Once the Guest is initialized, we hold the "struct lguest" in the
  196. * file private data. */
  197. struct lguest *lg = file->private_data;
  198. const unsigned long __user *input = (const unsigned long __user *)in;
  199. unsigned long req;
  200. struct lg_cpu *uninitialized_var(cpu);
  201. unsigned int cpu_id = *off;
  202. /* The first value tells us what this request is. */
  203. if (get_user(req, input) != 0)
  204. return -EFAULT;
  205. input++;
  206. /* If you haven't initialized, you must do that first. */
  207. if (req != LHREQ_INITIALIZE) {
  208. if (!lg || (cpu_id >= lg->nr_cpus))
  209. return -EINVAL;
  210. cpu = &lg->cpus[cpu_id];
  211. /* Once the Guest is dead, you can only read() why it died. */
  212. if (lg->dead)
  213. return -ENOENT;
  214. /* If you're not the task which owns the Guest, all you can do
  215. * is break the Launcher out of running the Guest. */
  216. if (current != cpu->tsk && req != LHREQ_BREAK)
  217. return -EPERM;
  218. }
  219. switch (req) {
  220. case LHREQ_INITIALIZE:
  221. return initialize(file, input);
  222. case LHREQ_IRQ:
  223. return user_send_irq(cpu, input);
  224. case LHREQ_BREAK:
  225. return break_guest_out(cpu, input);
  226. default:
  227. return -EINVAL;
  228. }
  229. }
  230. /*L:060 The final piece of interface code is the close() routine. It reverses
  231. * everything done in initialize(). This is usually called because the
  232. * Launcher exited.
  233. *
  234. * Note that the close routine returns 0 or a negative error number: it can't
  235. * really fail, but it can whine. I blame Sun for this wart, and K&R C for
  236. * letting them do it. :*/
  237. static int close(struct inode *inode, struct file *file)
  238. {
  239. struct lguest *lg = file->private_data;
  240. unsigned int i;
  241. /* If we never successfully initialized, there's nothing to clean up */
  242. if (!lg)
  243. return 0;
  244. /* We need the big lock, to protect from inter-guest I/O and other
  245. * Launchers initializing guests. */
  246. mutex_lock(&lguest_lock);
  247. /* Free up the shadow page tables for the Guest. */
  248. free_guest_pagetable(lg);
  249. for (i = 0; i < lg->nr_cpus; i++) {
  250. /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
  251. hrtimer_cancel(&lg->cpus[i].hrt);
  252. /* We can free up the register page we allocated. */
  253. free_page(lg->cpus[i].regs_page);
  254. /* Now all the memory cleanups are done, it's safe to release
  255. * the Launcher's memory management structure. */
  256. mmput(lg->cpus[i].mm);
  257. }
  258. /* If lg->dead doesn't contain an error code it will be NULL or a
  259. * kmalloc()ed string, either of which is ok to hand to kfree(). */
  260. if (!IS_ERR(lg->dead))
  261. kfree(lg->dead);
  262. /* We clear the entire structure, which also marks it as free for the
  263. * next user. */
  264. memset(lg, 0, sizeof(*lg));
  265. /* Release lock and exit. */
  266. mutex_unlock(&lguest_lock);
  267. return 0;
  268. }
  269. /*L:000
  270. * Welcome to our journey through the Launcher!
  271. *
  272. * The Launcher is the Host userspace program which sets up, runs and services
  273. * the Guest. In fact, many comments in the Drivers which refer to "the Host"
  274. * doing things are inaccurate: the Launcher does all the device handling for
  275. * the Guest, but the Guest can't know that.
  276. *
  277. * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
  278. * shall see more of that later.
  279. *
  280. * We begin our understanding with the Host kernel interface which the Launcher
  281. * uses: reading and writing a character device called /dev/lguest. All the
  282. * work happens in the read(), write() and close() routines: */
  283. static struct file_operations lguest_fops = {
  284. .owner = THIS_MODULE,
  285. .release = close,
  286. .write = write,
  287. .read = read,
  288. };
  289. /* This is a textbook example of a "misc" character device. Populate a "struct
  290. * miscdevice" and register it with misc_register(). */
  291. static struct miscdevice lguest_dev = {
  292. .minor = MISC_DYNAMIC_MINOR,
  293. .name = "lguest",
  294. .fops = &lguest_fops,
  295. };
  296. int __init lguest_device_init(void)
  297. {
  298. return misc_register(&lguest_dev);
  299. }
  300. void __exit lguest_device_remove(void)
  301. {
  302. misc_deregister(&lguest_dev);
  303. }