lguest_device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 Guest
  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" or two 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(vd) container_of(vd, struct lguest_device, vdev)
  44. /*D:130
  45. * Device configurations
  46. *
  47. * The configuration information for a device consists of one or more
  48. * virtqueues, a feature bitmap, and some configuration bytes. The
  49. * configuration bytes don't really matter to us: the Launcher sets them up, and
  50. * the driver will look at them during setup.
  51. *
  52. * A convenient routine to return the device's virtqueue config array:
  53. * immediately after the descriptor. */
  54. static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc)
  55. {
  56. return (void *)(desc + 1);
  57. }
  58. /* The features come immediately after the virtqueues. */
  59. static u8 *lg_features(const struct lguest_device_desc *desc)
  60. {
  61. return (void *)(lg_vq(desc) + desc->num_vq);
  62. }
  63. /* The config space comes after the two feature bitmasks. */
  64. static u8 *lg_config(const struct lguest_device_desc *desc)
  65. {
  66. return lg_features(desc) + desc->feature_len * 2;
  67. }
  68. /* The total size of the config page used by this device (incl. desc) */
  69. static unsigned desc_size(const struct lguest_device_desc *desc)
  70. {
  71. return sizeof(*desc)
  72. + desc->num_vq * sizeof(struct lguest_vqconfig)
  73. + desc->feature_len * 2
  74. + desc->config_len;
  75. }
  76. /* This tests (and acknowleges) a feature bit. */
  77. static bool lg_feature(struct virtio_device *vdev, unsigned fbit)
  78. {
  79. struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
  80. u8 *features;
  81. /* Obviously if they ask for a feature off the end of our feature
  82. * bitmap, it's not set. */
  83. if (fbit / 8 > desc->feature_len)
  84. return false;
  85. /* The feature bitmap comes after the virtqueues. */
  86. features = lg_features(desc);
  87. if (!(features[fbit / 8] & (1 << (fbit % 8))))
  88. return false;
  89. /* We set the matching bit in the other half of the bitmap to tell the
  90. * Host we want to use this feature. We don't use this yet, but we
  91. * could in future. */
  92. features[desc->feature_len + fbit / 8] |= (1 << (fbit % 8));
  93. return true;
  94. }
  95. /* Once they've found a field, getting a copy of it is easy. */
  96. static void lg_get(struct virtio_device *vdev, unsigned int offset,
  97. void *buf, unsigned len)
  98. {
  99. struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
  100. /* Check they didn't ask for more than the length of the config! */
  101. BUG_ON(offset + len > desc->config_len);
  102. memcpy(buf, lg_config(desc) + offset, len);
  103. }
  104. /* Setting the contents is also trivial. */
  105. static void lg_set(struct virtio_device *vdev, unsigned int offset,
  106. const void *buf, unsigned len)
  107. {
  108. struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
  109. /* Check they didn't ask for more than the length of the config! */
  110. BUG_ON(offset + len > desc->config_len);
  111. memcpy(lg_config(desc) + offset, buf, len);
  112. }
  113. /* The operations to get and set the status word just access the status field
  114. * of the device descriptor. */
  115. static u8 lg_get_status(struct virtio_device *vdev)
  116. {
  117. return to_lgdev(vdev)->desc->status;
  118. }
  119. static void lg_set_status(struct virtio_device *vdev, u8 status)
  120. {
  121. BUG_ON(!status);
  122. to_lgdev(vdev)->desc->status = status;
  123. }
  124. /* To reset the device, we (ab)use the NOTIFY hypercall, with the descriptor
  125. * address of the device. The Host will zero the status and all the
  126. * features. */
  127. static void lg_reset(struct virtio_device *vdev)
  128. {
  129. unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices;
  130. hcall(LHCALL_NOTIFY, (max_pfn<<PAGE_SHIFT) + offset, 0, 0);
  131. }
  132. /*
  133. * Virtqueues
  134. *
  135. * The other piece of infrastructure virtio needs is a "virtqueue": a way of
  136. * the Guest device registering buffers for the other side to read from or
  137. * write into (ie. send and receive buffers). Each device can have multiple
  138. * virtqueues: for example the console driver uses one queue for sending and
  139. * another for receiving.
  140. *
  141. * Fortunately for us, a very fast shared-memory-plus-descriptors virtqueue
  142. * already exists in virtio_ring.c. We just need to connect it up.
  143. *
  144. * We start with the information we need to keep about each virtqueue.
  145. */
  146. /*D:140 This is the information we remember about each virtqueue. */
  147. struct lguest_vq_info
  148. {
  149. /* A copy of the information contained in the device config. */
  150. struct lguest_vqconfig config;
  151. /* The address where we mapped the virtio ring, so we can unmap it. */
  152. void *pages;
  153. };
  154. /* When the virtio_ring code wants to prod the Host, it calls us here and we
  155. * make a hypercall. We hand the physical address of the virtqueue so the Host
  156. * knows which virtqueue we're talking about. */
  157. static void lg_notify(struct virtqueue *vq)
  158. {
  159. /* We store our virtqueue information in the "priv" pointer of the
  160. * virtqueue structure. */
  161. struct lguest_vq_info *lvq = vq->priv;
  162. hcall(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT, 0, 0);
  163. }
  164. /* This routine finds the first virtqueue described in the configuration of
  165. * this device and sets it up.
  166. *
  167. * This is kind of an ugly duckling. It'd be nicer to have a standard
  168. * representation of a virtqueue in the configuration space, but it seems that
  169. * everyone wants to do it differently. The KVM coders want the Guest to
  170. * allocate its own pages and tell the Host where they are, but for lguest it's
  171. * simpler for the Host to simply tell us where the pages are.
  172. *
  173. * So we provide drivers with a "find the Nth virtqueue and set it up"
  174. * function. */
  175. static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
  176. unsigned index,
  177. void (*callback)(struct virtqueue *vq))
  178. {
  179. struct lguest_device *ldev = to_lgdev(vdev);
  180. struct lguest_vq_info *lvq;
  181. struct virtqueue *vq;
  182. int err;
  183. /* We must have this many virtqueues. */
  184. if (index >= ldev->desc->num_vq)
  185. return ERR_PTR(-ENOENT);
  186. lvq = kmalloc(sizeof(*lvq), GFP_KERNEL);
  187. if (!lvq)
  188. return ERR_PTR(-ENOMEM);
  189. /* Make a copy of the "struct lguest_vqconfig" entry, which sits after
  190. * the descriptor. We need a copy because the config space might not
  191. * be aligned correctly. */
  192. memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config));
  193. printk("Mapping virtqueue %i addr %lx\n", index,
  194. (unsigned long)lvq->config.pfn << PAGE_SHIFT);
  195. /* Figure out how many pages the ring will take, and map that memory */
  196. lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,
  197. DIV_ROUND_UP(vring_size(lvq->config.num,
  198. PAGE_SIZE),
  199. PAGE_SIZE));
  200. if (!lvq->pages) {
  201. err = -ENOMEM;
  202. goto free_lvq;
  203. }
  204. /* OK, tell virtio_ring.c to set up a virtqueue now we know its size
  205. * and we've got a pointer to its pages. */
  206. vq = vring_new_virtqueue(lvq->config.num, vdev, lvq->pages,
  207. lg_notify, callback);
  208. if (!vq) {
  209. err = -ENOMEM;
  210. goto unmap;
  211. }
  212. /* Tell the interrupt for this virtqueue to go to the virtio_ring
  213. * interrupt handler. */
  214. /* FIXME: We used to have a flag for the Host to tell us we could use
  215. * the interrupt as a source of randomness: it'd be nice to have that
  216. * back.. */
  217. err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED,
  218. vdev->dev.bus_id, vq);
  219. if (err)
  220. goto destroy_vring;
  221. /* Last of all we hook up our 'struct lguest_vq_info" to the
  222. * virtqueue's priv pointer. */
  223. vq->priv = lvq;
  224. return vq;
  225. destroy_vring:
  226. vring_del_virtqueue(vq);
  227. unmap:
  228. lguest_unmap(lvq->pages);
  229. free_lvq:
  230. kfree(lvq);
  231. return ERR_PTR(err);
  232. }
  233. /*:*/
  234. /* Cleaning up a virtqueue is easy */
  235. static void lg_del_vq(struct virtqueue *vq)
  236. {
  237. struct lguest_vq_info *lvq = vq->priv;
  238. /* Release the interrupt */
  239. free_irq(lvq->config.irq, vq);
  240. /* Tell virtio_ring.c to free the virtqueue. */
  241. vring_del_virtqueue(vq);
  242. /* Unmap the pages containing the ring. */
  243. lguest_unmap(lvq->pages);
  244. /* Free our own queue information. */
  245. kfree(lvq);
  246. }
  247. /* The ops structure which hooks everything together. */
  248. static struct virtio_config_ops lguest_config_ops = {
  249. .feature = lg_feature,
  250. .get = lg_get,
  251. .set = lg_set,
  252. .get_status = lg_get_status,
  253. .set_status = lg_set_status,
  254. .reset = lg_reset,
  255. .find_vq = lg_find_vq,
  256. .del_vq = lg_del_vq,
  257. };
  258. /* The root device for the lguest virtio devices. This makes them appear as
  259. * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */
  260. static struct device lguest_root = {
  261. .parent = NULL,
  262. .bus_id = "lguest",
  263. };
  264. /*D:120 This is the core of the lguest bus: actually adding a new device.
  265. * It's a separate function because it's neater that way, and because an
  266. * earlier version of the code supported hotplug and unplug. They were removed
  267. * early on because they were never used.
  268. *
  269. * As Andrew Tridgell says, "Untested code is buggy code".
  270. *
  271. * It's worth reading this carefully: we start with a pointer to the new device
  272. * descriptor in the "lguest_devices" page. */
  273. static void add_lguest_device(struct lguest_device_desc *d)
  274. {
  275. struct lguest_device *ldev;
  276. /* Start with zeroed memory; Linux's device layer seems to count on
  277. * it. */
  278. ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
  279. if (!ldev) {
  280. printk(KERN_EMERG "Cannot allocate lguest dev %u\n",
  281. dev_index++);
  282. return;
  283. }
  284. /* This devices' parent is the lguest/ dir. */
  285. ldev->vdev.dev.parent = &lguest_root;
  286. /* We have a unique device index thanks to the dev_index counter. */
  287. ldev->vdev.index = dev_index++;
  288. /* The device type comes straight from the descriptor. There's also a
  289. * device vendor field in the virtio_device struct, which we leave as
  290. * 0. */
  291. ldev->vdev.id.device = d->type;
  292. /* We have a simple set of routines for querying the device's
  293. * configuration information and setting its status. */
  294. ldev->vdev.config = &lguest_config_ops;
  295. /* And we remember the device's descriptor for lguest_config_ops. */
  296. ldev->desc = d;
  297. /* register_virtio_device() sets up the generic fields for the struct
  298. * virtio_device and calls device_register(). This makes the bus
  299. * infrastructure look for a matching driver. */
  300. if (register_virtio_device(&ldev->vdev) != 0) {
  301. printk(KERN_ERR "Failed to register lguest device %u\n",
  302. ldev->vdev.index);
  303. kfree(ldev);
  304. }
  305. }
  306. /*D:110 scan_devices() simply iterates through the device page. The type 0 is
  307. * reserved to mean "end of devices". */
  308. static void scan_devices(void)
  309. {
  310. unsigned int i;
  311. struct lguest_device_desc *d;
  312. /* We start at the page beginning, and skip over each entry. */
  313. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  314. d = lguest_devices + i;
  315. /* Once we hit a zero, stop. */
  316. if (d->type == 0)
  317. break;
  318. printk("Device at %i has size %u\n", i, desc_size(d));
  319. add_lguest_device(d);
  320. }
  321. }
  322. /*D:105 Fairly early in boot, lguest_devices_init() is called to set up the
  323. * lguest device infrastructure. We check that we are a Guest by checking
  324. * pv_info.name: there are other ways of checking, but this seems most
  325. * obvious to me.
  326. *
  327. * So we can access the "struct lguest_device_desc"s easily, we map that memory
  328. * and store the pointer in the global "lguest_devices". Then we register a
  329. * root device from which all our devices will hang (this seems to be the
  330. * correct sysfs incantation).
  331. *
  332. * Finally we call scan_devices() which adds all the devices found in the
  333. * lguest_devices page. */
  334. static int __init lguest_devices_init(void)
  335. {
  336. if (strcmp(pv_info.name, "lguest") != 0)
  337. return 0;
  338. if (device_register(&lguest_root) != 0)
  339. panic("Could not register lguest root");
  340. /* Devices are in a single page above top of "normal" mem */
  341. lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);
  342. scan_devices();
  343. return 0;
  344. }
  345. /* We do this after core stuff, but before the drivers. */
  346. postcore_initcall(lguest_devices_init);
  347. /*D:150 At this point in the journey we used to now wade through the lguest
  348. * devices themselves: net, block and console. Since they're all now virtio
  349. * devices rather than lguest-specific, I've decided to ignore them. Mostly,
  350. * they're kind of boring. But this does mean you'll never experience the
  351. * thrill of reading the forbidden love scene buried deep in the block driver.
  352. *
  353. * "make Launcher" beckons, where we answer questions like "Where do Guests
  354. * come from?", and "What do you do when someone asks for optimization?". */