lguest_device.c 13.5 KB
Newer Older
R
Rusty Russell 已提交
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
R
Rusty Russell 已提交
3 4 5 6
 * memory.
 *
 * We use the standard "virtio" device infrastructure, which provides us with a
 * console, a network and a block driver.  Each one expects some configuration
7
 * information and a "virtqueue" or two to send and receive data. :*/
R
Rusty Russell 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <linux/init.h>
#include <linux/bootmem.h>
#include <linux/lguest_launcher.h>
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/interrupt.h>
#include <linux/virtio_ring.h>
#include <linux/err.h>
#include <asm/io.h>
#include <asm/paravirt.h>
#include <asm/lguest_hcall.h>

/* The pointer to our (page) of device descriptions. */
static void *lguest_devices;

/* For Guests, device memory can be used as normal memory, so we cast away the
 * __iomem to quieten sparse. */
static inline void *lguest_map(unsigned long phys_addr, unsigned long pages)
{
27
	return (__force void *)ioremap_cache(phys_addr, PAGE_SIZE*pages);
R
Rusty Russell 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
}

static inline void lguest_unmap(void *addr)
{
	iounmap((__force void __iomem *)addr);
}

/*D:100 Each lguest device is just a virtio device plus a pointer to its entry
 * in the lguest_devices page. */
struct lguest_device {
	struct virtio_device vdev;

	/* The entry in the lguest_devices page for this device. */
	struct lguest_device_desc *desc;
};

/* Since the virtio infrastructure hands us a pointer to the virtio_device all
 * the time, it helps to have a curt macro to get a pointer to the struct
 * lguest_device it's enclosed in.  */
A
Alexey Dobriyan 已提交
47
#define to_lgdev(vd) container_of(vd, struct lguest_device, vdev)
R
Rusty Russell 已提交
48 49 50 51

/*D:130
 * Device configurations
 *
52
 * The configuration information for a device consists of one or more
53
 * virtqueues, a feature bitmap, and some configuration bytes.  The
R
Rusty Russell 已提交
54
 * configuration bytes don't really matter to us: the Launcher sets them up, and
55
 * the driver will look at them during setup.
R
Rusty Russell 已提交
56
 *
57 58 59 60 61 62
 * A convenient routine to return the device's virtqueue config array:
 * immediately after the descriptor. */
static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc)
{
	return (void *)(desc + 1);
}
R
Rusty Russell 已提交
63

64 65 66 67 68
/* The features come immediately after the virtqueues. */
static u8 *lg_features(const struct lguest_device_desc *desc)
{
	return (void *)(lg_vq(desc) + desc->num_vq);
}
R
Rusty Russell 已提交
69

70 71
/* The config space comes after the two feature bitmasks. */
static u8 *lg_config(const struct lguest_device_desc *desc)
R
Rusty Russell 已提交
72
{
73 74
	return lg_features(desc) + desc->feature_len * 2;
}
R
Rusty Russell 已提交
75

76 77 78 79 80 81 82 83 84
/* The total size of the config page used by this device (incl. desc) */
static unsigned desc_size(const struct lguest_device_desc *desc)
{
	return sizeof(*desc)
		+ desc->num_vq * sizeof(struct lguest_vqconfig)
		+ desc->feature_len * 2
		+ desc->config_len;
}

85 86
/* This gets the device's feature bits. */
static u32 lg_get_features(struct virtio_device *vdev)
87
{
88 89
	unsigned int i;
	u32 features = 0;
90
	struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
91 92 93 94 95 96 97 98 99 100
	u8 *in_features = lg_features(desc);

	/* We do this the slow but generic way. */
	for (i = 0; i < min(desc->feature_len * 8, 32); i++)
		if (in_features[i / 8] & (1 << (i % 8)))
			features |= (1 << i);

	return features;
}

101
static void lg_finalize_features(struct virtio_device *vdev)
102
{
103
	unsigned int i, bits;
104 105 106 107 108
	struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
	/* Second half of bitmap is features we accept. */
	u8 *out_features = lg_features(desc) + desc->feature_len;

	memset(out_features, 0, desc->feature_len);
109 110 111
	bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
	for (i = 0; i < bits; i++) {
		if (test_bit(i, vdev->features))
112 113
			out_features[i / 8] |= (1 << (i % 8));
	}
R
Rusty Russell 已提交
114 115 116
}

/* Once they've found a field, getting a copy of it is easy. */
117
static void lg_get(struct virtio_device *vdev, unsigned int offset,
R
Rusty Russell 已提交
118 119
		   void *buf, unsigned len)
{
120 121 122 123 124
	struct lguest_device_desc *desc = to_lgdev(vdev)->desc;

	/* Check they didn't ask for more than the length of the config! */
	BUG_ON(offset + len > desc->config_len);
	memcpy(buf, lg_config(desc) + offset, len);
R
Rusty Russell 已提交
125 126 127
}

/* Setting the contents is also trivial. */
128
static void lg_set(struct virtio_device *vdev, unsigned int offset,
R
Rusty Russell 已提交
129 130
		   const void *buf, unsigned len)
{
131 132 133 134 135
	struct lguest_device_desc *desc = to_lgdev(vdev)->desc;

	/* Check they didn't ask for more than the length of the config! */
	BUG_ON(offset + len > desc->config_len);
	memcpy(lg_config(desc) + offset, buf, len);
R
Rusty Russell 已提交
136 137 138 139 140 141 142 143 144
}

/* The operations to get and set the status word just access the status field
 * of the device descriptor. */
static u8 lg_get_status(struct virtio_device *vdev)
{
	return to_lgdev(vdev)->desc->status;
}

145 146 147 148 149 150 151 152 153 154 155
/* To notify on status updates, we (ab)use the NOTIFY hypercall, with the
 * descriptor address of the device.  A zero status means "reset". */
static void set_status(struct virtio_device *vdev, u8 status)
{
	unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices;

	/* We set the status. */
	to_lgdev(vdev)->desc->status = status;
	hcall(LHCALL_NOTIFY, (max_pfn<<PAGE_SHIFT) + offset, 0, 0);
}

R
Rusty Russell 已提交
156 157
static void lg_set_status(struct virtio_device *vdev, u8 status)
{
R
Rusty Russell 已提交
158
	BUG_ON(!status);
159
	set_status(vdev, status);
R
Rusty Russell 已提交
160 161
}

R
Rusty Russell 已提交
162 163
static void lg_reset(struct virtio_device *vdev)
{
164
	set_status(vdev, 0);
R
Rusty Russell 已提交
165 166
}

R
Rusty Russell 已提交
167 168 169 170 171 172
/*
 * Virtqueues
 *
 * The other piece of infrastructure virtio needs is a "virtqueue": a way of
 * the Guest device registering buffers for the other side to read from or
 * write into (ie. send and receive buffers).  Each device can have multiple
R
Rusty Russell 已提交
173 174
 * virtqueues: for example the console driver uses one queue for sending and
 * another for receiving.
R
Rusty Russell 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
 *
 * Fortunately for us, a very fast shared-memory-plus-descriptors virtqueue
 * already exists in virtio_ring.c.  We just need to connect it up.
 *
 * We start with the information we need to keep about each virtqueue.
 */

/*D:140 This is the information we remember about each virtqueue. */
struct lguest_vq_info
{
	/* A copy of the information contained in the device config. */
	struct lguest_vqconfig config;

	/* The address where we mapped the virtio ring, so we can unmap it. */
	void *pages;
};

/* When the virtio_ring code wants to prod the Host, it calls us here and we
193
 * make a hypercall.  We hand the physical address of the virtqueue so the Host
R
Rusty Russell 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
 * knows which virtqueue we're talking about. */
static void lg_notify(struct virtqueue *vq)
{
	/* We store our virtqueue information in the "priv" pointer of the
	 * virtqueue structure. */
	struct lguest_vq_info *lvq = vq->priv;

	hcall(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT, 0, 0);
}

/* This routine finds the first virtqueue described in the configuration of
 * this device and sets it up.
 *
 * This is kind of an ugly duckling.  It'd be nicer to have a standard
 * representation of a virtqueue in the configuration space, but it seems that
R
Rusty Russell 已提交
209
 * everyone wants to do it differently.  The KVM coders want the Guest to
R
Rusty Russell 已提交
210 211 212
 * allocate its own pages and tell the Host where they are, but for lguest it's
 * simpler for the Host to simply tell us where the pages are.
 *
213 214
 * So we provide drivers with a "find the Nth virtqueue and set it up"
 * function. */
R
Rusty Russell 已提交
215
static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
216
				    unsigned index,
217
				    void (*callback)(struct virtqueue *vq))
R
Rusty Russell 已提交
218
{
219
	struct lguest_device *ldev = to_lgdev(vdev);
R
Rusty Russell 已提交
220 221 222 223
	struct lguest_vq_info *lvq;
	struct virtqueue *vq;
	int err;

224 225
	/* We must have this many virtqueues. */
	if (index >= ldev->desc->num_vq)
R
Rusty Russell 已提交
226 227 228 229 230 231
		return ERR_PTR(-ENOENT);

	lvq = kmalloc(sizeof(*lvq), GFP_KERNEL);
	if (!lvq)
		return ERR_PTR(-ENOMEM);

232 233 234 235
	/* Make a copy of the "struct lguest_vqconfig" entry, which sits after
	 * the descriptor.  We need a copy because the config space might not
	 * be aligned correctly. */
	memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config));
R
Rusty Russell 已提交
236

237 238
	printk("Mapping virtqueue %i addr %lx\n", index,
	       (unsigned long)lvq->config.pfn << PAGE_SHIFT);
R
Rusty Russell 已提交
239 240
	/* Figure out how many pages the ring will take, and map that memory */
	lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,
241 242
				DIV_ROUND_UP(vring_size(lvq->config.num,
							PAGE_SIZE),
R
Rusty Russell 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
					     PAGE_SIZE));
	if (!lvq->pages) {
		err = -ENOMEM;
		goto free_lvq;
	}

	/* OK, tell virtio_ring.c to set up a virtqueue now we know its size
	 * and we've got a pointer to its pages. */
	vq = vring_new_virtqueue(lvq->config.num, vdev, lvq->pages,
				 lg_notify, callback);
	if (!vq) {
		err = -ENOMEM;
		goto unmap;
	}

	/* Tell the interrupt for this virtqueue to go to the virtio_ring
	 * interrupt handler. */
	/* FIXME: We used to have a flag for the Host to tell us we could use
	 * the interrupt as a source of randomness: it'd be nice to have that
	 * back.. */
	err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED,
			  vdev->dev.bus_id, vq);
	if (err)
		goto destroy_vring;

	/* Last of all we hook up our 'struct lguest_vq_info" to the
	 * virtqueue's priv pointer. */
	vq->priv = lvq;
	return vq;

destroy_vring:
	vring_del_virtqueue(vq);
unmap:
	lguest_unmap(lvq->pages);
free_lvq:
	kfree(lvq);
	return ERR_PTR(err);
}
/*:*/

/* Cleaning up a virtqueue is easy */
static void lg_del_vq(struct virtqueue *vq)
{
	struct lguest_vq_info *lvq = vq->priv;

288 289
	/* Release the interrupt */
	free_irq(lvq->config.irq, vq);
R
Rusty Russell 已提交
290 291 292 293 294 295 296 297 298 299
	/* Tell virtio_ring.c to free the virtqueue. */
	vring_del_virtqueue(vq);
	/* Unmap the pages containing the ring. */
	lguest_unmap(lvq->pages);
	/* Free our own queue information. */
	kfree(lvq);
}

/* The ops structure which hooks everything together. */
static struct virtio_config_ops lguest_config_ops = {
300
	.get_features = lg_get_features,
301
	.finalize_features = lg_finalize_features,
R
Rusty Russell 已提交
302 303 304 305
	.get = lg_get,
	.set = lg_set,
	.get_status = lg_get_status,
	.set_status = lg_set_status,
R
Rusty Russell 已提交
306
	.reset = lg_reset,
R
Rusty Russell 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	.find_vq = lg_find_vq,
	.del_vq = lg_del_vq,
};

/* The root device for the lguest virtio devices.  This makes them appear as
 * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */
static struct device lguest_root = {
	.parent = NULL,
	.bus_id = "lguest",
};

/*D:120 This is the core of the lguest bus: actually adding a new device.
 * It's a separate function because it's neater that way, and because an
 * earlier version of the code supported hotplug and unplug.  They were removed
 * early on because they were never used.
 *
 * As Andrew Tridgell says, "Untested code is buggy code".
 *
 * It's worth reading this carefully: we start with a pointer to the new device
326 327 328 329
 * descriptor in the "lguest_devices" page, and the offset into the device
 * descriptor page so we can uniquely identify it if things go badly wrong. */
static void add_lguest_device(struct lguest_device_desc *d,
			      unsigned int offset)
R
Rusty Russell 已提交
330 331 332
{
	struct lguest_device *ldev;

R
Rusty Russell 已提交
333 334
	/* Start with zeroed memory; Linux's device layer seems to count on
	 * it. */
R
Rusty Russell 已提交
335 336
	ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
	if (!ldev) {
337 338
		printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n",
		       offset, d->type);
R
Rusty Russell 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		return;
	}

	/* This devices' parent is the lguest/ dir. */
	ldev->vdev.dev.parent = &lguest_root;
	/* We have a unique device index thanks to the dev_index counter. */
	ldev->vdev.id.device = d->type;
	/* We have a simple set of routines for querying the device's
	 * configuration information and setting its status. */
	ldev->vdev.config = &lguest_config_ops;
	/* And we remember the device's descriptor for lguest_config_ops. */
	ldev->desc = d;

	/* register_virtio_device() sets up the generic fields for the struct
	 * virtio_device and calls device_register().  This makes the bus
	 * infrastructure look for a matching driver. */
	if (register_virtio_device(&ldev->vdev) != 0) {
356 357
		printk(KERN_ERR "Failed to register lguest dev %u type %u\n",
		       offset, d->type);
R
Rusty Russell 已提交
358 359 360 361 362 363 364 365 366 367 368 369
		kfree(ldev);
	}
}

/*D:110 scan_devices() simply iterates through the device page.  The type 0 is
 * reserved to mean "end of devices". */
static void scan_devices(void)
{
	unsigned int i;
	struct lguest_device_desc *d;

	/* We start at the page beginning, and skip over each entry. */
370
	for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
R
Rusty Russell 已提交
371 372 373 374 375 376
		d = lguest_devices + i;

		/* Once we hit a zero, stop. */
		if (d->type == 0)
			break;

377
		printk("Device at %i has size %u\n", i, desc_size(d));
378
		add_lguest_device(d, i);
R
Rusty Russell 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	}
}

/*D:105 Fairly early in boot, lguest_devices_init() is called to set up the
 * lguest device infrastructure.  We check that we are a Guest by checking
 * pv_info.name: there are other ways of checking, but this seems most
 * obvious to me.
 *
 * So we can access the "struct lguest_device_desc"s easily, we map that memory
 * and store the pointer in the global "lguest_devices".  Then we register a
 * root device from which all our devices will hang (this seems to be the
 * correct sysfs incantation).
 *
 * Finally we call scan_devices() which adds all the devices found in the
 * lguest_devices page. */
static int __init lguest_devices_init(void)
{
	if (strcmp(pv_info.name, "lguest") != 0)
		return 0;

	if (device_register(&lguest_root) != 0)
		panic("Could not register lguest root");

	/* Devices are in a single page above top of "normal" mem */
	lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);

	scan_devices();
	return 0;
}
/* We do this after core stuff, but before the drivers. */
postcore_initcall(lguest_devices_init);

/*D:150 At this point in the journey we used to now wade through the lguest
 * devices themselves: net, block and console.  Since they're all now virtio
 * devices rather than lguest-specific, I've decided to ignore them.  Mostly,
 * they're kind of boring.  But this does mean you'll never experience the
 * thrill of reading the forbidden love scene buried deep in the block driver.
 *
 * "make Launcher" beckons, where we answer questions like "Where do Guests
 * come from?", and "What do you do when someone asks for optimization?". */