/*P:0501* Lguest guests use a very simple method to describe devices. It's a2* series of device descriptors contained just above the top of normal Guest3* memory.4*5* We use the standard "virtio" device infrastructure, which provides us with a6* console, a network and a block driver. Each one expects some configuration7* information and a "virtqueue" or two to send and receive data.8:*/9#include <linux/init.h>10#include <linux/bootmem.h>11#include <linux/lguest_launcher.h>12#include <linux/virtio.h>13#include <linux/virtio_config.h>14#include <linux/interrupt.h>15#include <linux/virtio_ring.h>16#include <linux/err.h>17#include <linux/slab.h>18#include <asm/io.h>19#include <asm/paravirt.h>20#include <asm/lguest_hcall.h>2122/* The pointer to our (page) of device descriptions. */23static void *lguest_devices;2425/*26* For Guests, device memory can be used as normal memory, so we cast away the27* __iomem to quieten sparse.28*/29static inline void *lguest_map(unsigned long phys_addr, unsigned long pages)30{31return (__force void *)ioremap_cache(phys_addr, PAGE_SIZE*pages);32}3334static inline void lguest_unmap(void *addr)35{36iounmap((__force void __iomem *)addr);37}3839/*D:10040* Each lguest device is just a virtio device plus a pointer to its entry41* in the lguest_devices page.42*/43struct lguest_device {44struct virtio_device vdev;4546/* The entry in the lguest_devices page for this device. */47struct lguest_device_desc *desc;48};4950/*51* Since the virtio infrastructure hands us a pointer to the virtio_device all52* the time, it helps to have a curt macro to get a pointer to the struct53* lguest_device it's enclosed in.54*/55#define to_lgdev(vd) container_of(vd, struct lguest_device, vdev)5657/*D:13058* Device configurations59*60* The configuration information for a device consists of one or more61* virtqueues, a feature bitmap, and some configuration bytes. The62* configuration bytes don't really matter to us: the Launcher sets them up, and63* the driver will look at them during setup.64*65* A convenient routine to return the device's virtqueue config array:66* immediately after the descriptor.67*/68static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc)69{70return (void *)(desc + 1);71}7273/* The features come immediately after the virtqueues. */74static u8 *lg_features(const struct lguest_device_desc *desc)75{76return (void *)(lg_vq(desc) + desc->num_vq);77}7879/* The config space comes after the two feature bitmasks. */80static u8 *lg_config(const struct lguest_device_desc *desc)81{82return lg_features(desc) + desc->feature_len * 2;83}8485/* The total size of the config page used by this device (incl. desc) */86static unsigned desc_size(const struct lguest_device_desc *desc)87{88return sizeof(*desc)89+ desc->num_vq * sizeof(struct lguest_vqconfig)90+ desc->feature_len * 291+ desc->config_len;92}9394/* This gets the device's feature bits. */95static u32 lg_get_features(struct virtio_device *vdev)96{97unsigned int i;98u32 features = 0;99struct lguest_device_desc *desc = to_lgdev(vdev)->desc;100u8 *in_features = lg_features(desc);101102/* We do this the slow but generic way. */103for (i = 0; i < min(desc->feature_len * 8, 32); i++)104if (in_features[i / 8] & (1 << (i % 8)))105features |= (1 << i);106107return features;108}109110/*111* The virtio core takes the features the Host offers, and copies the ones112* supported by the driver into the vdev->features array. Once that's all113* sorted out, this routine is called so we can tell the Host which features we114* understand and accept.115*/116static void lg_finalize_features(struct virtio_device *vdev)117{118unsigned int i, bits;119struct lguest_device_desc *desc = to_lgdev(vdev)->desc;120/* Second half of bitmap is features we accept. */121u8 *out_features = lg_features(desc) + desc->feature_len;122123/* Give virtio_ring a chance to accept features. */124vring_transport_features(vdev);125126/*127* The vdev->feature array is a Linux bitmask: this isn't the same as a128* the simple array of bits used by lguest devices for features. So we129* do this slow, manual conversion which is completely general.130*/131memset(out_features, 0, desc->feature_len);132bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;133for (i = 0; i < bits; i++) {134if (test_bit(i, vdev->features))135out_features[i / 8] |= (1 << (i % 8));136}137}138139/* Once they've found a field, getting a copy of it is easy. */140static void lg_get(struct virtio_device *vdev, unsigned int offset,141void *buf, unsigned len)142{143struct lguest_device_desc *desc = to_lgdev(vdev)->desc;144145/* Check they didn't ask for more than the length of the config! */146BUG_ON(offset + len > desc->config_len);147memcpy(buf, lg_config(desc) + offset, len);148}149150/* Setting the contents is also trivial. */151static void lg_set(struct virtio_device *vdev, unsigned int offset,152const void *buf, unsigned len)153{154struct lguest_device_desc *desc = to_lgdev(vdev)->desc;155156/* Check they didn't ask for more than the length of the config! */157BUG_ON(offset + len > desc->config_len);158memcpy(lg_config(desc) + offset, buf, len);159}160161/*162* The operations to get and set the status word just access the status field163* of the device descriptor.164*/165static u8 lg_get_status(struct virtio_device *vdev)166{167return to_lgdev(vdev)->desc->status;168}169170/*171* To notify on status updates, we (ab)use the NOTIFY hypercall, with the172* descriptor address of the device. A zero status means "reset".173*/174static void set_status(struct virtio_device *vdev, u8 status)175{176unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices;177178/* We set the status. */179to_lgdev(vdev)->desc->status = status;180hcall(LHCALL_NOTIFY, (max_pfn << PAGE_SHIFT) + offset, 0, 0, 0);181}182183static void lg_set_status(struct virtio_device *vdev, u8 status)184{185BUG_ON(!status);186set_status(vdev, status);187}188189static void lg_reset(struct virtio_device *vdev)190{191set_status(vdev, 0);192}193194/*195* Virtqueues196*197* The other piece of infrastructure virtio needs is a "virtqueue": a way of198* the Guest device registering buffers for the other side to read from or199* write into (ie. send and receive buffers). Each device can have multiple200* virtqueues: for example the console driver uses one queue for sending and201* another for receiving.202*203* Fortunately for us, a very fast shared-memory-plus-descriptors virtqueue204* already exists in virtio_ring.c. We just need to connect it up.205*206* We start with the information we need to keep about each virtqueue.207*/208209/*D:140 This is the information we remember about each virtqueue. */210struct lguest_vq_info {211/* A copy of the information contained in the device config. */212struct lguest_vqconfig config;213214/* The address where we mapped the virtio ring, so we can unmap it. */215void *pages;216};217218/*219* When the virtio_ring code wants to prod the Host, it calls us here and we220* make a hypercall. We hand the physical address of the virtqueue so the Host221* knows which virtqueue we're talking about.222*/223static void lg_notify(struct virtqueue *vq)224{225/*226* We store our virtqueue information in the "priv" pointer of the227* virtqueue structure.228*/229struct lguest_vq_info *lvq = vq->priv;230231hcall(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT, 0, 0, 0);232}233234/* An extern declaration inside a C file is bad form. Don't do it. */235extern void lguest_setup_irq(unsigned int irq);236237/*238* This routine finds the Nth virtqueue described in the configuration of239* this device and sets it up.240*241* This is kind of an ugly duckling. It'd be nicer to have a standard242* representation of a virtqueue in the configuration space, but it seems that243* everyone wants to do it differently. The KVM coders want the Guest to244* allocate its own pages and tell the Host where they are, but for lguest it's245* simpler for the Host to simply tell us where the pages are.246*/247static struct virtqueue *lg_find_vq(struct virtio_device *vdev,248unsigned index,249void (*callback)(struct virtqueue *vq),250const char *name)251{252struct lguest_device *ldev = to_lgdev(vdev);253struct lguest_vq_info *lvq;254struct virtqueue *vq;255int err;256257/* We must have this many virtqueues. */258if (index >= ldev->desc->num_vq)259return ERR_PTR(-ENOENT);260261lvq = kmalloc(sizeof(*lvq), GFP_KERNEL);262if (!lvq)263return ERR_PTR(-ENOMEM);264265/*266* Make a copy of the "struct lguest_vqconfig" entry, which sits after267* the descriptor. We need a copy because the config space might not268* be aligned correctly.269*/270memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config));271272printk("Mapping virtqueue %i addr %lx\n", index,273(unsigned long)lvq->config.pfn << PAGE_SHIFT);274/* Figure out how many pages the ring will take, and map that memory */275lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,276DIV_ROUND_UP(vring_size(lvq->config.num,277LGUEST_VRING_ALIGN),278PAGE_SIZE));279if (!lvq->pages) {280err = -ENOMEM;281goto free_lvq;282}283284/*285* OK, tell virtio_ring.c to set up a virtqueue now we know its size286* and we've got a pointer to its pages.287*/288vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN,289vdev, lvq->pages, lg_notify, callback, name);290if (!vq) {291err = -ENOMEM;292goto unmap;293}294295/* Make sure the interrupt is allocated. */296lguest_setup_irq(lvq->config.irq);297298/*299* Tell the interrupt for this virtqueue to go to the virtio_ring300* interrupt handler.301*302* FIXME: We used to have a flag for the Host to tell us we could use303* the interrupt as a source of randomness: it'd be nice to have that304* back.305*/306err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED,307dev_name(&vdev->dev), vq);308if (err)309goto destroy_vring;310311/*312* Last of all we hook up our 'struct lguest_vq_info" to the313* virtqueue's priv pointer.314*/315vq->priv = lvq;316return vq;317318destroy_vring:319vring_del_virtqueue(vq);320unmap:321lguest_unmap(lvq->pages);322free_lvq:323kfree(lvq);324return ERR_PTR(err);325}326/*:*/327328/* Cleaning up a virtqueue is easy */329static void lg_del_vq(struct virtqueue *vq)330{331struct lguest_vq_info *lvq = vq->priv;332333/* Release the interrupt */334free_irq(lvq->config.irq, vq);335/* Tell virtio_ring.c to free the virtqueue. */336vring_del_virtqueue(vq);337/* Unmap the pages containing the ring. */338lguest_unmap(lvq->pages);339/* Free our own queue information. */340kfree(lvq);341}342343static void lg_del_vqs(struct virtio_device *vdev)344{345struct virtqueue *vq, *n;346347list_for_each_entry_safe(vq, n, &vdev->vqs, list)348lg_del_vq(vq);349}350351static int lg_find_vqs(struct virtio_device *vdev, unsigned nvqs,352struct virtqueue *vqs[],353vq_callback_t *callbacks[],354const char *names[])355{356struct lguest_device *ldev = to_lgdev(vdev);357int i;358359/* We must have this many virtqueues. */360if (nvqs > ldev->desc->num_vq)361return -ENOENT;362363for (i = 0; i < nvqs; ++i) {364vqs[i] = lg_find_vq(vdev, i, callbacks[i], names[i]);365if (IS_ERR(vqs[i]))366goto error;367}368return 0;369370error:371lg_del_vqs(vdev);372return PTR_ERR(vqs[i]);373}374375/* The ops structure which hooks everything together. */376static struct virtio_config_ops lguest_config_ops = {377.get_features = lg_get_features,378.finalize_features = lg_finalize_features,379.get = lg_get,380.set = lg_set,381.get_status = lg_get_status,382.set_status = lg_set_status,383.reset = lg_reset,384.find_vqs = lg_find_vqs,385.del_vqs = lg_del_vqs,386};387388/*389* The root device for the lguest virtio devices. This makes them appear as390* /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2.391*/392static struct device *lguest_root;393394/*D:120395* This is the core of the lguest bus: actually adding a new device.396* It's a separate function because it's neater that way, and because an397* earlier version of the code supported hotplug and unplug. They were removed398* early on because they were never used.399*400* As Andrew Tridgell says, "Untested code is buggy code".401*402* It's worth reading this carefully: we start with a pointer to the new device403* descriptor in the "lguest_devices" page, and the offset into the device404* descriptor page so we can uniquely identify it if things go badly wrong.405*/406static void add_lguest_device(struct lguest_device_desc *d,407unsigned int offset)408{409struct lguest_device *ldev;410411/* Start with zeroed memory; Linux's device layer counts on it. */412ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);413if (!ldev) {414printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n",415offset, d->type);416return;417}418419/* This devices' parent is the lguest/ dir. */420ldev->vdev.dev.parent = lguest_root;421/*422* The device type comes straight from the descriptor. There's also a423* device vendor field in the virtio_device struct, which we leave as424* 0.425*/426ldev->vdev.id.device = d->type;427/*428* We have a simple set of routines for querying the device's429* configuration information and setting its status.430*/431ldev->vdev.config = &lguest_config_ops;432/* And we remember the device's descriptor for lguest_config_ops. */433ldev->desc = d;434435/*436* register_virtio_device() sets up the generic fields for the struct437* virtio_device and calls device_register(). This makes the bus438* infrastructure look for a matching driver.439*/440if (register_virtio_device(&ldev->vdev) != 0) {441printk(KERN_ERR "Failed to register lguest dev %u type %u\n",442offset, d->type);443kfree(ldev);444}445}446447/*D:110448* scan_devices() simply iterates through the device page. The type 0 is449* reserved to mean "end of devices".450*/451static void scan_devices(void)452{453unsigned int i;454struct lguest_device_desc *d;455456/* We start at the page beginning, and skip over each entry. */457for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {458d = lguest_devices + i;459460/* Once we hit a zero, stop. */461if (d->type == 0)462break;463464printk("Device at %i has size %u\n", i, desc_size(d));465add_lguest_device(d, i);466}467}468469/*D:105470* Fairly early in boot, lguest_devices_init() is called to set up the471* lguest device infrastructure. We check that we are a Guest by checking472* pv_info.name: there are other ways of checking, but this seems most473* obvious to me.474*475* So we can access the "struct lguest_device_desc"s easily, we map that memory476* and store the pointer in the global "lguest_devices". Then we register a477* root device from which all our devices will hang (this seems to be the478* correct sysfs incantation).479*480* Finally we call scan_devices() which adds all the devices found in the481* lguest_devices page.482*/483static int __init lguest_devices_init(void)484{485if (strcmp(pv_info.name, "lguest") != 0)486return 0;487488lguest_root = root_device_register("lguest");489if (IS_ERR(lguest_root))490panic("Could not register lguest root");491492/* Devices are in a single page above top of "normal" mem */493lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);494495scan_devices();496return 0;497}498/* We do this after core stuff, but before the drivers. */499postcore_initcall(lguest_devices_init);500501/*D:150502* At this point in the journey we used to now wade through the lguest503* devices themselves: net, block and console. Since they're all now virtio504* devices rather than lguest-specific, I've decided to ignore them. Mostly,505* they're kind of boring. But this does mean you'll never experience the506* thrill of reading the forbidden love scene buried deep in the block driver.507*508* "make Launcher" beckons, where we answer questions like "Where do Guests509* come from?", and "What do you do when someone asks for optimization?".510*/511512513