lguest_device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*P:050 Lguest guests use a very simple method to describe devices. It's a
  2. * series of device descriptors contained just above the top of normal
  3. * memory.
  4. *
  5. * We use the standard "virtio" device infrastructure, which provides us with a
  6. * console, a network and a block driver. Each one expects some configuration
  7. * information and a "virtqueue" mechanism to send and receive data. :*/
  8. #include <linux/init.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/lguest_launcher.h>
  11. #include <linux/virtio.h>
  12. #include <linux/virtio_config.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/virtio_ring.h>
  15. #include <linux/err.h>
  16. #include <asm/io.h>
  17. #include <asm/paravirt.h>
  18. #include <asm/lguest_hcall.h>
  19. /* The pointer to our (page) of device descriptions. */
  20. static void *lguest_devices;
  21. /* Unique numbering for lguest devices. */
  22. static unsigned int dev_index;
  23. /* For Guests, device memory can be used as normal memory, so we cast away the
  24. * __iomem to quieten sparse. */
  25. static inline void *lguest_map(unsigned long phys_addr, unsigned long pages)
  26. {
  27. return (__force void *)ioremap(phys_addr, PAGE_SIZE*pages);
  28. }
  29. static inline void lguest_unmap(void *addr)
  30. {
  31. iounmap((__force void __iomem *)addr);
  32. }
  33. /*D:100 Each lguest device is just a virtio device plus a pointer to its entry
  34. * in the lguest_devices page. */
  35. struct lguest_device {
  36. struct virtio_device vdev;
  37. /* The entry in the lguest_devices page for this device. */
  38. struct lguest_device_desc *desc;
  39. };
  40. /* Since the virtio infrastructure hands us a pointer to the virtio_device all
  41. * the time, it helps to have a curt macro to get a pointer to the struct
  42. * lguest_device it's enclosed in. */
  43. #define to_lgdev(vdev) container_of(vdev, struct lguest_device, vdev)
  44. /*D:130
  45. * Device configurations
  46. *
  47. * The configuration information for a device consists of a series of fields.
  48. * The device will look for these fields during setup.
  49. *
  50. * For us these fields come immediately after that device's descriptor in the
  51. * lguest_devices page.
  52. *
  53. * Each field starts with a "type" byte, a "length" byte, then that number of
  54. * bytes of configuration information. The device descriptor tells us the
  55. * total configuration length so we know when we've reached the last field. */
  56. /* type + length bytes */
  57. #define FHDR_LEN 2
  58. /* This finds the first field of a given type for a device's configuration. */
  59. static void *lg_find(struct virtio_device *vdev, u8 type, unsigned int *len)
  60. {
  61. struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
  62. int i;
  63. for (i = 0; i < desc->config_len; i += FHDR_LEN + desc->config[i+1]) {
  64. if (desc->config[i] == type) {
  65. /* Mark it used, so Host can know we looked at it, and
  66. * also so we won't find the same one twice. */
  67. desc->config[i] |= 0x80;
  68. /* Remember, the second byte is the length. */
  69. *len = desc->config[i+1];
  70. /* We return a pointer to the field header. */
  71. return desc->config + i;
  72. }
  73. }
  74. /* Not found: return NULL for failure. */
  75. return NULL;
  76. }
  77. /* Once they've found a field, getting a copy of it is easy. */
  78. static void lg_get(struct virtio_device *vdev, void *token,
  79. void *buf, unsigned len)
  80. {
  81. /* Check they didn't ask for more than the length of the field! */
  82. BUG_ON(len > ((u8 *)token)[1]);
  83. memcpy(buf, token + FHDR_LEN, len);
  84. }
  85. /* Setting the contents is also trivial. */
  86. static void lg_set(struct virtio_device *vdev, void *token,
  87. const void *buf, unsigned len)
  88. {
  89. BUG_ON(len > ((u8 *)token)[1]);
  90. memcpy(token + FHDR_LEN, buf, len);
  91. }
  92. /* The operations to get and set the status word just access the status field
  93. * of the device descriptor. */
  94. static u8 lg_get_status(struct virtio_device *vdev)
  95. {
  96. return to_lgdev(vdev)->desc->status;
  97. }
  98. static void lg_set_status(struct virtio_device *vdev, u8 status)
  99. {
  100. to_lgdev(vdev)->desc->status = status;
  101. }
  102. /*
  103. * Virtqueues
  104. *
  105. * The other piece of infrastructure virtio needs is a "virtqueue": a way of
  106. * the Guest device registering buffers for the other side to read from or
  107. * write into (ie. send and receive buffers). Each device can have multiple
  108. * virtqueues: for example the console has one queue for sending and one for
  109. * receiving.
  110. *
  111. * Fortunately for us, a very fast shared-memory-plus-descriptors virtqueue
  112. * already exists in virtio_ring.c. We just need to connect it up.
  113. *
  114. * We start with the information we need to keep about each virtqueue.
  115. */
  116. /*D:140 This is the information we remember about each virtqueue. */
  117. struct lguest_vq_info
  118. {
  119. /* A copy of the information contained in the device config. */
  120. struct lguest_vqconfig config;
  121. /* The address where we mapped the virtio ring, so we can unmap it. */
  122. void *pages;
  123. };
  124. /* When the virtio_ring code wants to prod the Host, it calls us here and we
  125. * make a hypercall. We hand the page number of the virtqueue so the Host
  126. * knows which virtqueue we're talking about. */
  127. static void lg_notify(struct virtqueue *vq)
  128. {
  129. /* We store our virtqueue information in the "priv" pointer of the
  130. * virtqueue structure. */
  131. struct lguest_vq_info *lvq = vq->priv;
  132. hcall(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT, 0, 0);
  133. }
  134. /* This routine finds the first virtqueue described in the configuration of
  135. * this device and sets it up.
  136. *
  137. * This is kind of an ugly duckling. It'd be nicer to have a standard
  138. * representation of a virtqueue in the configuration space, but it seems that
  139. * everyone wants to do it differently. The KVM guys want the Guest to
  140. * allocate its own pages and tell the Host where they are, but for lguest it's
  141. * simpler for the Host to simply tell us where the pages are.
  142. *
  143. * So we provide devices with a "find virtqueue and set it up" function. */
  144. static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
  145. bool (*callback)(struct virtqueue *vq))
  146. {
  147. struct lguest_vq_info *lvq;
  148. struct virtqueue *vq;
  149. unsigned int len;
  150. void *token;
  151. int err;
  152. /* Look for a field of the correct type to mark a virtqueue. Note that
  153. * if this succeeds, then the type will be changed so it won't be found
  154. * again, and future lg_find_vq() calls will find the next
  155. * virtqueue (if any). */
  156. token = vdev->config->find(vdev, VIRTIO_CONFIG_F_VIRTQUEUE, &len);
  157. if (!token)
  158. return ERR_PTR(-ENOENT);
  159. lvq = kmalloc(sizeof(*lvq), GFP_KERNEL);
  160. if (!lvq)
  161. return ERR_PTR(-ENOMEM);
  162. /* Note: we could use a configuration space inside here, just like we
  163. * do for the device. This would allow expansion in future, because
  164. * our configuration system is designed to be expansible. But this is
  165. * way easier. */
  166. if (len != sizeof(lvq->config)) {
  167. dev_err(&vdev->dev, "Unexpected virtio config len %u\n", len);
  168. err = -EIO;
  169. goto free_lvq;
  170. }
  171. /* Make a copy of the "struct lguest_vqconfig" field. We need a copy
  172. * because the config space might not be aligned correctly. */
  173. vdev->config->get(vdev, token, &lvq->config, sizeof(lvq->config));
  174. /* Figure out how many pages the ring will take, and map that memory */
  175. lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,
  176. DIV_ROUND_UP(vring_size(lvq->config.num),
  177. PAGE_SIZE));
  178. if (!lvq->pages) {
  179. err = -ENOMEM;
  180. goto free_lvq;
  181. }
  182. /* OK, tell virtio_ring.c to set up a virtqueue now we know its size
  183. * and we've got a pointer to its pages. */
  184. vq = vring_new_virtqueue(lvq->config.num, vdev, lvq->pages,
  185. lg_notify, callback);
  186. if (!vq) {
  187. err = -ENOMEM;
  188. goto unmap;
  189. }
  190. /* Tell the interrupt for this virtqueue to go to the virtio_ring
  191. * interrupt handler. */
  192. /* FIXME: We used to have a flag for the Host to tell us we could use
  193. * the interrupt as a source of randomness: it'd be nice to have that
  194. * back.. */
  195. err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED,
  196. vdev->dev.bus_id, vq);
  197. if (err)
  198. goto destroy_vring;
  199. /* Last of all we hook up our 'struct lguest_vq_info" to the
  200. * virtqueue's priv pointer. */
  201. vq->priv = lvq;
  202. return vq;
  203. destroy_vring:
  204. vring_del_virtqueue(vq);
  205. unmap:
  206. lguest_unmap(lvq->pages);
  207. free_lvq:
  208. kfree(lvq);
  209. return ERR_PTR(err);
  210. }
  211. /*:*/
  212. /* Cleaning up a virtqueue is easy */
  213. static void lg_del_vq(struct virtqueue *vq)
  214. {
  215. struct lguest_vq_info *lvq = vq->priv;
  216. /* Tell virtio_ring.c to free the virtqueue. */
  217. vring_del_virtqueue(vq);
  218. /* Unmap the pages containing the ring. */
  219. lguest_unmap(lvq->pages);
  220. /* Free our own queue information. */
  221. kfree(lvq);
  222. }
  223. /* The ops structure which hooks everything together. */
  224. static struct virtio_config_ops lguest_config_ops = {
  225. .find = lg_find,
  226. .get = lg_get,
  227. .set = lg_set,
  228. .get_status = lg_get_status,
  229. .set_status = lg_set_status,
  230. .find_vq = lg_find_vq,
  231. .del_vq = lg_del_vq,
  232. };
  233. /* The root device for the lguest virtio devices. This makes them appear as
  234. * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */
  235. static struct device lguest_root = {
  236. .parent = NULL,
  237. .bus_id = "lguest",
  238. };
  239. /*D:120 This is the core of the lguest bus: actually adding a new device.
  240. * It's a separate function because it's neater that way, and because an
  241. * earlier version of the code supported hotplug and unplug. They were removed
  242. * early on because they were never used.
  243. *
  244. * As Andrew Tridgell says, "Untested code is buggy code".
  245. *
  246. * It's worth reading this carefully: we start with a pointer to the new device
  247. * descriptor in the "lguest_devices" page. */
  248. static void add_lguest_device(struct lguest_device_desc *d)
  249. {
  250. struct lguest_device *ldev;
  251. ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
  252. if (!ldev) {
  253. printk(KERN_EMERG "Cannot allocate lguest dev %u\n",
  254. dev_index++);
  255. return;
  256. }
  257. /* This devices' parent is the lguest/ dir. */
  258. ldev->vdev.dev.parent = &lguest_root;
  259. /* We have a unique device index thanks to the dev_index counter. */
  260. ldev->vdev.index = dev_index++;
  261. /* The device type comes straight from the descriptor. There's also a
  262. * device vendor field in the virtio_device struct, which we leave as
  263. * 0. */
  264. ldev->vdev.id.device = d->type;
  265. /* We have a simple set of routines for querying the device's
  266. * configuration information and setting its status. */
  267. ldev->vdev.config = &lguest_config_ops;
  268. /* And we remember the device's descriptor for lguest_config_ops. */
  269. ldev->desc = d;
  270. /* register_virtio_device() sets up the generic fields for the struct
  271. * virtio_device and calls device_register(). This makes the bus
  272. * infrastructure look for a matching driver. */
  273. if (register_virtio_device(&ldev->vdev) != 0) {
  274. printk(KERN_ERR "Failed to register lguest device %u\n",
  275. ldev->vdev.index);
  276. kfree(ldev);
  277. }
  278. }
  279. /*D:110 scan_devices() simply iterates through the device page. The type 0 is
  280. * reserved to mean "end of devices". */
  281. static void scan_devices(void)
  282. {
  283. unsigned int i;
  284. struct lguest_device_desc *d;
  285. /* We start at the page beginning, and skip over each entry. */
  286. for (i = 0; i < PAGE_SIZE; i += sizeof(*d) + d->config_len) {
  287. d = lguest_devices + i;
  288. /* Once we hit a zero, stop. */
  289. if (d->type == 0)
  290. break;
  291. add_lguest_device(d);
  292. }
  293. }
  294. /*D:105 Fairly early in boot, lguest_devices_init() is called to set up the
  295. * lguest device infrastructure. We check that we are a Guest by checking
  296. * pv_info.name: there are other ways of checking, but this seems most
  297. * obvious to me.
  298. *
  299. * So we can access the "struct lguest_device_desc"s easily, we map that memory
  300. * and store the pointer in the global "lguest_devices". Then we register a
  301. * root device from which all our devices will hang (this seems to be the
  302. * correct sysfs incantation).
  303. *
  304. * Finally we call scan_devices() which adds all the devices found in the
  305. * lguest_devices page. */
  306. static int __init lguest_devices_init(void)
  307. {
  308. if (strcmp(pv_info.name, "lguest") != 0)
  309. return 0;
  310. if (device_register(&lguest_root) != 0)
  311. panic("Could not register lguest root");
  312. /* Devices are in a single page above top of "normal" mem */
  313. lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);
  314. scan_devices();
  315. return 0;
  316. }
  317. /* We do this after core stuff, but before the drivers. */
  318. postcore_initcall(lguest_devices_init);
  319. /*D:150 At this point in the journey we used to now wade through the lguest
  320. * devices themselves: net, block and console. Since they're all now virtio
  321. * devices rather than lguest-specific, I've decided to ignore them. Mostly,
  322. * they're kind of boring. But this does mean you'll never experience the
  323. * thrill of reading the forbidden love scene buried deep in the block driver.
  324. *
  325. * "make Launcher" beckons, where we answer questions like "Where do Guests
  326. * come from?", and "What do you do when someone asks for optimization?". */