lguest_user.c 11.0 KB
Newer Older
1 2
/*P:200 This contains all the /dev/lguest code, whereby the userspace launcher
 * controls and communicates with the Guest.  For example, the first write will
3 4
 * tell us the Guest's memory layout, pagetable, entry point and kernel address
 * 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. :*/
R
Rusty Russell 已提交
6 7 8
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
9
#include <linux/sched.h>
R
Rusty Russell 已提交
10 11
#include "lg.h"

R
Rusty Russell 已提交
12 13 14 15 16
/*L:055 When something happens, the Waker process needs a way to stop the
 * kernel running the Guest and return to the Launcher.  So the Waker writes
 * LHREQ_BREAK and the value "1" to /dev/lguest to do this.  Once the Launcher
 * has done whatever needs attention, it writes LHREQ_BREAK and "0" to release
 * the Waker. */
17
static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
R
Rusty Russell 已提交
18 19 20
{
	unsigned long on;

R
Rusty Russell 已提交
21
	/* Fetch whether they're turning break on or off. */
R
Rusty Russell 已提交
22 23 24 25
	if (get_user(on, input) != 0)
		return -EFAULT;

	if (on) {
26
		cpu->break_out = 1;
R
Rusty Russell 已提交
27
		/* Pop it out of the Guest (may be running on different CPU) */
28
		wake_up_process(cpu->tsk);
R
Rusty Russell 已提交
29
		/* Wait for them to reset it */
30
		return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
R
Rusty Russell 已提交
31
	} else {
32 33
		cpu->break_out = 0;
		wake_up(&cpu->break_wq);
R
Rusty Russell 已提交
34 35 36 37
		return 0;
	}
}

38 39
/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
 * number to /dev/lguest. */
40
static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
R
Rusty Russell 已提交
41
{
42
	unsigned long irq;
R
Rusty Russell 已提交
43 44 45 46 47

	if (get_user(irq, input) != 0)
		return -EFAULT;
	if (irq >= LGUEST_IRQS)
		return -EINVAL;
48 49
	/* Next time the Guest runs, the core code will see if it can deliver
	 * this interrupt. */
50
	set_bit(irq, cpu->irqs_pending);
R
Rusty Russell 已提交
51 52 53
	return 0;
}

54 55
/*L:040 Once our Guest is initialized, the Launcher makes it run by reading
 * from /dev/lguest. */
R
Rusty Russell 已提交
56 57 58
static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
{
	struct lguest *lg = file->private_data;
59 60
	struct lg_cpu *cpu;
	unsigned int cpu_id = *o;
R
Rusty Russell 已提交
61

62
	/* You must write LHREQ_INITIALIZE first! */
R
Rusty Russell 已提交
63 64 65
	if (!lg)
		return -EINVAL;

66 67 68 69 70 71
	/* Watch out for arbitrary vcpu indexes! */
	if (cpu_id >= lg->nr_cpus)
		return -EINVAL;

	cpu = &lg->cpus[cpu_id];

R
Rusty Russell 已提交
72
	/* If you're not the task which owns the Guest, go away. */
73
	if (current != cpu->tsk)
R
Rusty Russell 已提交
74 75
		return -EPERM;

76
	/* If the Guest is already dead, we indicate why */
R
Rusty Russell 已提交
77 78 79
	if (lg->dead) {
		size_t len;

80
		/* lg->dead either contains an error code, or a string. */
R
Rusty Russell 已提交
81 82 83
		if (IS_ERR(lg->dead))
			return PTR_ERR(lg->dead);

84
		/* We can only return as much as the buffer they read with. */
R
Rusty Russell 已提交
85 86 87 88 89 90
		len = min(size, strlen(lg->dead)+1);
		if (copy_to_user(user, lg->dead, len) != 0)
			return -EFAULT;
		return len;
	}

91
	/* If we returned from read() last time because the Guest sent I/O,
92
	 * clear the flag. */
93 94
	if (cpu->pending_notify)
		cpu->pending_notify = 0;
R
Rusty Russell 已提交
95

96
	/* Run the Guest until something interesting happens. */
97
	return run_guest(cpu, (unsigned long __user *)user);
R
Rusty Russell 已提交
98 99
}

100 101
/*L:025 This actually initializes a CPU.  For the moment, a Guest is only
 * uniprocessor, so "id" is always 0. */
102 103
static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
{
104
	/* We have a limited number the number of CPUs in the lguest struct. */
105
	if (id >= ARRAY_SIZE(cpu->lg->cpus))
106 107
		return -EINVAL;

108
	/* Set up this CPU's id, and pointer back to the lguest struct. */
109 110 111
	cpu->id = id;
	cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
	cpu->lg->nr_cpus++;
112 113

	/* Each CPU has a timer it can set. */
114
	init_clockdev(cpu);
115

116 117 118 119 120 121 122 123 124 125 126 127 128
	/* We need a complete page for the Guest registers: they are accessible
	 * to the Guest and we can only grant it access to whole pages. */
	cpu->regs_page = get_zeroed_page(GFP_KERNEL);
	if (!cpu->regs_page)
		return -ENOMEM;

	/* We actually put the registers at the bottom of the page. */
	cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);

	/* Now we initialize the Guest's registers, handing it the start
	 * address. */
	lguest_arch_setup_regs(cpu, start_ip);

129
	/* Initialize the queue for the Waker to wait on */
130 131 132
	init_waitqueue_head(&cpu->break_wq);

	/* We keep a pointer to the Launcher task (ie. current task) for when
133
	 * other Guests want to wake this one (eg. console input). */
134 135 136 137 138 139 140
	cpu->tsk = current;

	/* We need to keep a pointer to the Launcher's memory map, because if
	 * the Launcher dies we need to clean it up.  If we don't keep a
	 * reference, it is destroyed before close() is called. */
	cpu->mm = get_task_mm(cpu->tsk);

141 142 143 144
	/* We remember which CPU's pages this Guest used last, for optimization
	 * when the same Guest runs on the same CPU twice. */
	cpu->last_pages = NULL;

145
	/* No error == success. */
146 147 148
	return 0;
}

149
/*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
150
 * values (in addition to the LHREQ_INITIALIZE value).  These are:
151
 *
152 153
 * base: The start of the Guest-physical memory inside the Launcher memory.
 *
154
 * pfnlimit: The highest (Guest-physical) page number the Guest should be
R
Rusty Russell 已提交
155 156
 * allowed to access.  The Guest memory lives inside the Launcher, so it sets
 * this to ensure the Guest can only reach its own memory.
157 158 159 160 161 162
 *
 * pgdir: The (Guest-physical) address of the top of the initial Guest
 * pagetables (which are set up by the Launcher).
 *
 * start: The first instruction to execute ("eip" in x86-speak).
 */
163
static int initialize(struct file *file, const unsigned long __user *input)
R
Rusty Russell 已提交
164
{
165 166
	/* "struct lguest" contains everything we (the Host) know about a
	 * Guest. */
R
Rusty Russell 已提交
167
	struct lguest *lg;
168
	int err;
169
	unsigned long args[4];
R
Rusty Russell 已提交
170

171 172
	/* We grab the Big Lguest lock, which protects against multiple
	 * simultaneous initializations. */
R
Rusty Russell 已提交
173
	mutex_lock(&lguest_lock);
174
	/* You can't initialize twice!  Close the device and start again... */
R
Rusty Russell 已提交
175 176 177 178 179 180 181 182 183 184
	if (file->private_data) {
		err = -EBUSY;
		goto unlock;
	}

	if (copy_from_user(args, input, sizeof(args)) != 0) {
		err = -EFAULT;
		goto unlock;
	}

185 186 187
	lg = kzalloc(sizeof(*lg), GFP_KERNEL);
	if (!lg) {
		err = -ENOMEM;
R
Rusty Russell 已提交
188 189
		goto unlock;
	}
190 191

	/* Populate the easy fields of our "struct lguest" */
192
	lg->mem_base = (void __user *)args[0];
193
	lg->pfn_limit = args[1];
194

195
	/* This is the first cpu (cpu 0) and it will start booting at args[3] */
196
	err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
197 198 199
	if (err)
		goto release_guest;

200
	/* Initialize the Guest's shadow page tables, using the toplevel
201
	 * address the Launcher gave us.  This allocates memory, so can fail. */
202
	err = init_guest_pagetable(lg, args[2]);
R
Rusty Russell 已提交
203 204 205
	if (err)
		goto free_regs;

206
	/* We keep our "struct lguest" in the file's private_data. */
R
Rusty Russell 已提交
207 208 209 210
	file->private_data = lg;

	mutex_unlock(&lguest_lock);

211
	/* And because this is a write() call, we return the length used. */
R
Rusty Russell 已提交
212 213 214
	return sizeof(args);

free_regs:
215 216
	/* FIXME: This should be in free_vcpu */
	free_page(lg->cpus[0].regs_page);
R
Rusty Russell 已提交
217
release_guest:
A
Adrian Bunk 已提交
218
	kfree(lg);
R
Rusty Russell 已提交
219 220 221 222 223
unlock:
	mutex_unlock(&lguest_lock);
	return err;
}

224
/*L:010 The first operation the Launcher does must be a write.  All writes
R
Rusty Russell 已提交
225
 * start with an unsigned long number: for the first write this must be
226
 * LHREQ_INITIALIZE to set up the Guest.  After that the Launcher can use
227 228 229 230 231 232
 * writes of other values to send interrupts.
 *
 * Note that we overload the "offset" in the /dev/lguest file to indicate what
 * CPU number we're dealing with.  Currently this is always 0, since we only
 * support uniprocessor Guests, but you can see the beginnings of SMP support
 * here. */
233
static ssize_t write(struct file *file, const char __user *in,
R
Rusty Russell 已提交
234 235
		     size_t size, loff_t *off)
{
236
	/* Once the Guest is initialized, we hold the "struct lguest" in the
237
	 * file private data. */
R
Rusty Russell 已提交
238
	struct lguest *lg = file->private_data;
239 240
	const unsigned long __user *input = (const unsigned long __user *)in;
	unsigned long req;
241
	struct lg_cpu *uninitialized_var(cpu);
242
	unsigned int cpu_id = *off;
R
Rusty Russell 已提交
243

244
	/* The first value tells us what this request is. */
R
Rusty Russell 已提交
245 246
	if (get_user(req, input) != 0)
		return -EFAULT;
247
	input++;
R
Rusty Russell 已提交
248

249
	/* If you haven't initialized, you must do that first. */
250 251 252 253
	if (req != LHREQ_INITIALIZE) {
		if (!lg || (cpu_id >= lg->nr_cpus))
			return -EINVAL;
		cpu = &lg->cpus[cpu_id];
254

255 256 257
		/* Once the Guest is dead, you can only read() why it died. */
		if (lg->dead)
			return -ENOENT;
R
Rusty Russell 已提交
258

259 260 261 262 263
		/* If you're not the task which owns the Guest, all you can do
		 * is break the Launcher out of running the Guest. */
		if (current != cpu->tsk && req != LHREQ_BREAK)
			return -EPERM;
	}
R
Rusty Russell 已提交
264 265 266

	switch (req) {
	case LHREQ_INITIALIZE:
267
		return initialize(file, input);
R
Rusty Russell 已提交
268
	case LHREQ_IRQ:
269
		return user_send_irq(cpu, input);
R
Rusty Russell 已提交
270
	case LHREQ_BREAK:
271
		return break_guest_out(cpu, input);
R
Rusty Russell 已提交
272 273 274 275 276
	default:
		return -EINVAL;
	}
}

277 278 279 280 281 282 283
/*L:060 The final piece of interface code is the close() routine.  It reverses
 * everything done in initialize().  This is usually called because the
 * Launcher exited.
 *
 * Note that the close routine returns 0 or a negative error number: it can't
 * really fail, but it can whine.  I blame Sun for this wart, and K&R C for
 * letting them do it. :*/
R
Rusty Russell 已提交
284 285 286
static int close(struct inode *inode, struct file *file)
{
	struct lguest *lg = file->private_data;
287
	unsigned int i;
R
Rusty Russell 已提交
288

289
	/* If we never successfully initialized, there's nothing to clean up */
R
Rusty Russell 已提交
290 291 292
	if (!lg)
		return 0;

293 294
	/* We need the big lock, to protect from inter-guest I/O and other
	 * Launchers initializing guests. */
R
Rusty Russell 已提交
295
	mutex_lock(&lguest_lock);
296 297 298 299

	/* Free up the shadow page tables for the Guest. */
	free_guest_pagetable(lg);

300
	for (i = 0; i < lg->nr_cpus; i++) {
301 302
		/* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
		hrtimer_cancel(&lg->cpus[i].hrt);
303 304
		/* We can free up the register page we allocated. */
		free_page(lg->cpus[i].regs_page);
305 306 307
		/* Now all the memory cleanups are done, it's safe to release
		 * the Launcher's memory management structure. */
		mmput(lg->cpus[i].mm);
308
	}
309 310
	/* If lg->dead doesn't contain an error code it will be NULL or a
	 * kmalloc()ed string, either of which is ok to hand to kfree(). */
R
Rusty Russell 已提交
311 312
	if (!IS_ERR(lg->dead))
		kfree(lg->dead);
313 314
	/* We clear the entire structure, which also marks it as free for the
	 * next user. */
R
Rusty Russell 已提交
315
	memset(lg, 0, sizeof(*lg));
316
	/* Release lock and exit. */
R
Rusty Russell 已提交
317
	mutex_unlock(&lguest_lock);
318

R
Rusty Russell 已提交
319 320 321
	return 0;
}

322 323 324 325 326 327
/*L:000
 * Welcome to our journey through the Launcher!
 *
 * The Launcher is the Host userspace program which sets up, runs and services
 * the Guest.  In fact, many comments in the Drivers which refer to "the Host"
 * doing things are inaccurate: the Launcher does all the device handling for
R
Rusty Russell 已提交
328
 * the Guest, but the Guest can't know that.
329 330 331 332 333 334 335
 *
 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
 * shall see more of that later.
 *
 * We begin our understanding with the Host kernel interface which the Launcher
 * uses: reading and writing a character device called /dev/lguest.  All the
 * work happens in the read(), write() and close() routines: */
R
Rusty Russell 已提交
336 337 338 339 340 341
static struct file_operations lguest_fops = {
	.owner	 = THIS_MODULE,
	.release = close,
	.write	 = write,
	.read	 = read,
};
342 343 344

/* This is a textbook example of a "misc" character device.  Populate a "struct
 * miscdevice" and register it with misc_register(). */
R
Rusty Russell 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
static struct miscdevice lguest_dev = {
	.minor	= MISC_DYNAMIC_MINOR,
	.name	= "lguest",
	.fops	= &lguest_fops,
};

int __init lguest_device_init(void)
{
	return misc_register(&lguest_dev);
}

void __exit lguest_device_remove(void)
{
	misc_deregister(&lguest_dev);
}