core.c 10.2 KB
Newer Older
Rusty Russell's avatar
Rusty Russell committed
1
2
/*P:400
 * This contains run_guest() which actually calls into the Host<->Guest
3
 * Switcher and analyzes the return, such as determining if the Guest wants the
Rusty Russell's avatar
Rusty Russell committed
4
5
 * Host to do something.  This file also contains useful helper routines.
:*/
Rusty Russell's avatar
Rusty Russell committed
6
7
8
9
10
11
12
13
#include <linux/module.h>
#include <linux/stringify.h>
#include <linux/stddef.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/cpu.h>
#include <linux/freezer.h>
14
#include <linux/highmem.h>
15
#include <linux/slab.h>
Rusty Russell's avatar
Rusty Russell committed
16
17
18
19
20
21
22
#include <asm/paravirt.h>
#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/poll.h>
#include <asm/asm-offsets.h>
#include "lg.h"

23
unsigned long switcher_addr;
24
struct page **lg_switcher_pages;
Rusty Russell's avatar
Rusty Russell committed
25
26
27
28
29
static struct vm_struct *switcher_vma;

/* This One Big lock protects all inter-guest data structures. */
DEFINE_MUTEX(lguest_lock);

Rusty Russell's avatar
Rusty Russell committed
30
31
/*H:010
 * We need to set up the Switcher at a high virtual address.  Remember the
Rusty Russell's avatar
Rusty Russell committed
32
33
34
35
36
37
38
39
 * Switcher is a few hundred bytes of assembler code which actually changes the
 * CPU to run the Guest, and then changes back to the Host when a trap or
 * interrupt happens.
 *
 * The Switcher code must be at the same virtual address in the Guest as the
 * Host since it will be running as the switchover occurs.
 *
 * Trying to map memory at a particular address is an unusual thing to do, so
Rusty Russell's avatar
Rusty Russell committed
40
41
 * it's not a simple one-liner.
 */
Rusty Russell's avatar
Rusty Russell committed
42
43
44
45
static __init int map_switcher(void)
{
	int i, err;

Rusty Russell's avatar
Rusty Russell committed
46
47
48
49
50
51
52
53
	/*
	 * Map the Switcher in to high memory.
	 *
	 * It turns out that if we choose the address 0xFFC00000 (4MB under the
	 * top virtual address), it makes setting up the page tables really
	 * easy.
	 */

54
55
56
57
58
59
60
	/* We assume Switcher text fits into a single page. */
	if (end_switcher_text - start_switcher_text > PAGE_SIZE) {
		printk(KERN_ERR "lguest: switcher text too large (%zu)\n",
		       end_switcher_text - start_switcher_text);
		return -EINVAL;
	}

Rusty Russell's avatar
Rusty Russell committed
61
62
63
64
	/*
	 * We allocate an array of struct page pointers.  map_vm_area() wants
	 * this, rather than just an array of pages.
	 */
65
66
67
68
	lg_switcher_pages = kmalloc(sizeof(lg_switcher_pages[0])
				    * TOTAL_SWITCHER_PAGES,
				    GFP_KERNEL);
	if (!lg_switcher_pages) {
Rusty Russell's avatar
Rusty Russell committed
69
70
71
72
		err = -ENOMEM;
		goto out;
	}

Rusty Russell's avatar
Rusty Russell committed
73
74
75
76
	/*
	 * Now we actually allocate the pages.  The Guest will see these pages,
	 * so we make sure they're zeroed.
	 */
Rusty Russell's avatar
Rusty Russell committed
77
	for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
78
79
		lg_switcher_pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
		if (!lg_switcher_pages[i]) {
Rusty Russell's avatar
Rusty Russell committed
80
81
82
83
84
			err = -ENOMEM;
			goto free_some_pages;
		}
	}

Rusty Russell's avatar
Rusty Russell committed
85
	/*
86
87
88
89
	 * We place the Switcher underneath the fixmap area, which is the
	 * highest virtual address we can get.  This is important, since we
	 * tell the Guest it can't access this memory, so we want its ceiling
	 * as high as possible.
Rusty Russell's avatar
Rusty Russell committed
90
	 */
91
	switcher_addr = FIXADDR_START - (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE;
92

Rusty Russell's avatar
Rusty Russell committed
93
	/*
94
95
96
97
	 * Now we reserve the "virtual memory area" we want.  We might
	 * not get it in theory, but in practice it's worked so far.
	 * The end address needs +1 because __get_vm_area allocates an
	 * extra guard page, so we need space for that.
Rusty Russell's avatar
Rusty Russell committed
98
	 */
Rusty Russell's avatar
Rusty Russell committed
99
	switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE,
100
				     VM_ALLOC, switcher_addr, switcher_addr
101
				     + (TOTAL_SWITCHER_PAGES+1) * PAGE_SIZE);
Rusty Russell's avatar
Rusty Russell committed
102
103
104
105
106
107
	if (!switcher_vma) {
		err = -ENOMEM;
		printk("lguest: could not map switcher pages high\n");
		goto free_pages;
	}

Rusty Russell's avatar
Rusty Russell committed
108
109
	/*
	 * This code actually sets up the pages we've allocated to appear at
110
	 * switcher_addr.  map_vm_area() takes the vma we allocated above, the
Rusty Russell's avatar
Rusty Russell committed
111
	 * kind of pages we're mapping (kernel pages), and a pointer to our
112
	 * array of struct pages.
Rusty Russell's avatar
Rusty Russell committed
113
	 */
114
	err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, lg_switcher_pages);
Rusty Russell's avatar
Rusty Russell committed
115
116
117
118
	if (err) {
		printk("lguest: map_vm_area failed: %i\n", err);
		goto free_vma;
	}
Rusty Russell's avatar
Rusty Russell committed
119

Rusty Russell's avatar
Rusty Russell committed
120
121
	/*
	 * Now the Switcher is mapped at the right address, we can't fail!
Rusty Russell's avatar
Rusty Russell committed
122
	 * Copy in the compiled-in Switcher code (from x86/switcher_32.S).
Rusty Russell's avatar
Rusty Russell committed
123
	 */
Rusty Russell's avatar
Rusty Russell committed
124
125
126
127
128
	memcpy(switcher_vma->addr, start_switcher_text,
	       end_switcher_text - start_switcher_text);

	printk(KERN_INFO "lguest: mapped switcher at %p\n",
	       switcher_vma->addr);
Rusty Russell's avatar
Rusty Russell committed
129
	/* And we succeeded... */
Rusty Russell's avatar
Rusty Russell committed
130
131
132
133
134
135
136
137
	return 0;

free_vma:
	vunmap(switcher_vma->addr);
free_pages:
	i = TOTAL_SWITCHER_PAGES;
free_some_pages:
	for (--i; i >= 0; i--)
138
139
		__free_pages(lg_switcher_pages[i], 0);
	kfree(lg_switcher_pages);
Rusty Russell's avatar
Rusty Russell committed
140
141
142
out:
	return err;
}
Rusty Russell's avatar
Rusty Russell committed
143
/*:*/
Rusty Russell's avatar
Rusty Russell committed
144

Rusty Russell's avatar
Rusty Russell committed
145
/* Cleaning up the mapping when the module is unloaded is almost... too easy. */
Rusty Russell's avatar
Rusty Russell committed
146
147
148
149
static void unmap_switcher(void)
{
	unsigned int i;

Rusty Russell's avatar
Rusty Russell committed
150
	/* vunmap() undoes *both* map_vm_area() and __get_vm_area(). */
Rusty Russell's avatar
Rusty Russell committed
151
	vunmap(switcher_vma->addr);
Rusty Russell's avatar
Rusty Russell committed
152
	/* Now we just need to free the pages we copied the switcher into */
Rusty Russell's avatar
Rusty Russell committed
153
	for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
154
155
		__free_pages(lg_switcher_pages[i], 0);
	kfree(lg_switcher_pages);
Rusty Russell's avatar
Rusty Russell committed
156
157
}

Rusty Russell's avatar
Rusty Russell committed
158
/*H:032
159
160
 * Dealing With Guest Memory.
 *
Rusty Russell's avatar
Rusty Russell committed
161
162
163
 * Before we go too much further into the Host, we need to grok the routines
 * we use to deal with Guest memory.
 *
164
 * When the Guest gives us (what it thinks is) a physical address, we can use
165
166
 * the normal copy_from_user() & copy_to_user() on the corresponding place in
 * the memory region allocated by the Launcher.
167
168
169
170
 *
 * But we can't trust the Guest: it might be trying to access the Launcher
 * code.  We have to check that the range is below the pfn_limit the Launcher
 * gave us.  We have to make sure that addr + len doesn't give us a false
Rusty Russell's avatar
Rusty Russell committed
171
172
 * positive by overflowing, too.
 */
173
174
bool lguest_address_ok(const struct lguest *lg,
		       unsigned long addr, unsigned long len)
Rusty Russell's avatar
Rusty Russell committed
175
176
177
178
{
	return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
}

Rusty Russell's avatar
Rusty Russell committed
179
180
/*
 * This routine copies memory from the Guest.  Here we can see how useful the
181
 * kill_lguest() routine we met in the Launcher can be: we return a random
Rusty Russell's avatar
Rusty Russell committed
182
183
 * value (all zeroes) instead of needing to return an error.
 */
184
void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes)
Rusty Russell's avatar
Rusty Russell committed
185
{
186
187
	if (!lguest_address_ok(cpu->lg, addr, bytes)
	    || copy_from_user(b, cpu->lg->mem_base + addr, bytes) != 0) {
Rusty Russell's avatar
Rusty Russell committed
188
189
		/* copy_from_user should do this, but as we rely on it... */
		memset(b, 0, bytes);
190
		kill_guest(cpu, "bad read address %#lx len %u", addr, bytes);
Rusty Russell's avatar
Rusty Russell committed
191
192
193
	}
}

194
/* This is the write (copy into Guest) version. */
195
void __lgwrite(struct lg_cpu *cpu, unsigned long addr, const void *b,
196
	       unsigned bytes)
Rusty Russell's avatar
Rusty Russell committed
197
{
198
199
200
	if (!lguest_address_ok(cpu->lg, addr, bytes)
	    || copy_to_user(cpu->lg->mem_base + addr, b, bytes) != 0)
		kill_guest(cpu, "bad write address %#lx len %u", addr, bytes);
Rusty Russell's avatar
Rusty Russell committed
201
}
202
/*:*/
Rusty Russell's avatar
Rusty Russell committed
203

Rusty Russell's avatar
Rusty Russell committed
204
205
/*H:030
 * Let's jump straight to the the main loop which runs the Guest.
Rusty Russell's avatar
Rusty Russell committed
206
 * Remember, this is called by the Launcher reading /dev/lguest, and we keep
Rusty Russell's avatar
Rusty Russell committed
207
208
 * going around and around until something interesting happens.
 */
209
int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
Rusty Russell's avatar
Rusty Russell committed
210
{
Rusty Russell's avatar
Rusty Russell committed
211
	/* We stop running once the Guest is dead. */
212
	while (!cpu->lg->dead) {
Rusty Russell's avatar
Rusty Russell committed
213
		unsigned int irq;
214
		bool more;
Rusty Russell's avatar
Rusty Russell committed
215

216
		/* First we run any hypercalls the Guest wants done. */
217
218
		if (cpu->hcall)
			do_hypercalls(cpu);
219

Rusty Russell's avatar
Rusty Russell committed
220
221
		/*
		 * It's possible the Guest did a NOTIFY hypercall to the
Rusty Russell's avatar
Rusty Russell committed
222
		 * Launcher.
Rusty Russell's avatar
Rusty Russell committed
223
		 */
224
		if (cpu->pending_notify) {
Rusty Russell's avatar
Rusty Russell committed
225
226
227
228
			/*
			 * Does it just needs to write to a registered
			 * eventfd (ie. the appropriate virtqueue thread)?
			 */
229
			if (!send_notify_to_eventfd(cpu)) {
Alex Russell's avatar
Alex Russell committed
230
				/* OK, we tell the main Launcher. */
231
232
233
234
				if (put_user(cpu->pending_notify, user))
					return -EFAULT;
				return sizeof(cpu->pending_notify);
			}
Rusty Russell's avatar
Rusty Russell committed
235
236
		}

237
238
239
240
241
242
243
		/*
		 * All long-lived kernel loops need to check with this horrible
		 * thing called the freezer.  If the Host is trying to suspend,
		 * it stops us.
		 */
		try_to_freeze();

Rusty Russell's avatar
Rusty Russell committed
244
		/* Check for signals */
Rusty Russell's avatar
Rusty Russell committed
245
246
247
		if (signal_pending(current))
			return -ERESTARTSYS;

Rusty Russell's avatar
Rusty Russell committed
248
249
		/*
		 * Check if there are any interrupts which can be delivered now:
250
		 * if so, this sets up the hander to be executed when we next
Rusty Russell's avatar
Rusty Russell committed
251
252
		 * run the Guest.
		 */
253
		irq = interrupt_pending(cpu, &more);
Rusty Russell's avatar
Rusty Russell committed
254
		if (irq < LGUEST_IRQS)
255
			try_deliver_interrupt(cpu, irq, more);
Rusty Russell's avatar
Rusty Russell committed
256

Rusty Russell's avatar
Rusty Russell committed
257
258
259
260
		/*
		 * Just make absolutely sure the Guest is still alive.  One of
		 * those hypercalls could have been fatal, for example.
		 */
261
		if (cpu->lg->dead)
Rusty Russell's avatar
Rusty Russell committed
262
263
			break;

Rusty Russell's avatar
Rusty Russell committed
264
265
266
267
		/*
		 * If the Guest asked to be stopped, we sleep.  The Guest's
		 * clock timer will wake us.
		 */
268
		if (cpu->halted) {
Rusty Russell's avatar
Rusty Russell committed
269
			set_current_state(TASK_INTERRUPTIBLE);
Rusty Russell's avatar
Rusty Russell committed
270
271
272
273
			/*
			 * Just before we sleep, make sure no interrupt snuck in
			 * which we should be doing.
			 */
274
			if (interrupt_pending(cpu, &more) < LGUEST_IRQS)
Rusty Russell's avatar
Rusty Russell committed
275
276
277
				set_current_state(TASK_RUNNING);
			else
				schedule();
Rusty Russell's avatar
Rusty Russell committed
278
279
280
			continue;
		}

Rusty Russell's avatar
Rusty Russell committed
281
282
283
284
		/*
		 * OK, now we're ready to jump into the Guest.  First we put up
		 * the "Do Not Disturb" sign:
		 */
Rusty Russell's avatar
Rusty Russell committed
285
286
		local_irq_disable();

287
		/* Actually run the Guest until something happens. */
288
		lguest_arch_run_guest(cpu);
Rusty Russell's avatar
Rusty Russell committed
289
290

		/* Now we're ready to be interrupted or moved to other CPUs */
Rusty Russell's avatar
Rusty Russell committed
291
292
		local_irq_enable();

293
		/* Now we deal with whatever happened to the Guest. */
294
		lguest_arch_handle_trap(cpu);
Rusty Russell's avatar
Rusty Russell committed
295
	}
296

297
	/* Special case: Guest is 'dead' but wants a reboot. */
298
	if (cpu->lg->dead == ERR_PTR(-ERESTART))
Balaji Rao's avatar
Balaji Rao committed
299
		return -ERESTART;
300

Rusty Russell's avatar
Rusty Russell committed
301
	/* The Guest is dead => "No such file or directory" */
Rusty Russell's avatar
Rusty Russell committed
302
303
304
	return -ENOENT;
}

Rusty Russell's avatar
Rusty Russell committed
305
306
307
308
309
310
311
312
/*H:000
 * Welcome to the Host!
 *
 * By this point your brain has been tickled by the Guest code and numbed by
 * the Launcher code; prepare for it to be stretched by the Host code.  This is
 * the heart.  Let's begin at the initialization routine for the Host's lg
 * module.
 */
Rusty Russell's avatar
Rusty Russell committed
313
314
315
316
static int __init init(void)
{
	int err;

Rusty Russell's avatar
Rusty Russell committed
317
	/* Lguest can't run under Xen, VMI or itself.  It does Tricky Stuff. */
318
	if (get_kernel_rpl() != 0) {
319
		printk("lguest is afraid of being a guest\n");
Rusty Russell's avatar
Rusty Russell committed
320
321
322
		return -EPERM;
	}

Rusty Russell's avatar
Rusty Russell committed
323
	/* First we put the Switcher up in very high virtual memory. */
Rusty Russell's avatar
Rusty Russell committed
324
325
	err = map_switcher();
	if (err)
326
		goto out;
Rusty Russell's avatar
Rusty Russell committed
327

328
329
330
	/* We might need to reserve an interrupt vector. */
	err = init_interrupts();
	if (err)
331
		goto unmap;
332

Rusty Russell's avatar
Rusty Russell committed
333
	/* /dev/lguest needs to be registered. */
Rusty Russell's avatar
Rusty Russell committed
334
	err = lguest_device_init();
335
336
	if (err)
		goto free_interrupts;
Rusty Russell's avatar
Rusty Russell committed
337

338
339
	/* Finally we do some architecture-specific setup. */
	lguest_arch_host_init();
Rusty Russell's avatar
Rusty Russell committed
340
341

	/* All good! */
Rusty Russell's avatar
Rusty Russell committed
342
	return 0;
343
344
345
346
347
348
349

free_interrupts:
	free_interrupts();
unmap:
	unmap_switcher();
out:
	return err;
Rusty Russell's avatar
Rusty Russell committed
350
351
}

Rusty Russell's avatar
Rusty Russell committed
352
/* Cleaning up is just the same code, backwards.  With a little French. */
Rusty Russell's avatar
Rusty Russell committed
353
354
355
static void __exit fini(void)
{
	lguest_device_remove();
356
	free_interrupts();
Rusty Russell's avatar
Rusty Russell committed
357
	unmap_switcher();
Rusty Russell's avatar
Rusty Russell committed
358

359
	lguest_arch_host_fini();
Rusty Russell's avatar
Rusty Russell committed
360
}
361
/*:*/
Rusty Russell's avatar
Rusty Russell committed
362

Rusty Russell's avatar
Rusty Russell committed
363
364
365
366
/*
 * The Host side of lguest can be a module.  This is a nice way for people to
 * play with it.
 */
Rusty Russell's avatar
Rusty Russell committed
367
368
369
370
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");