lguest_user.c 12 KB

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