lguest_user.c 12 KB

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