core.c 10.3 KB
Newer Older
R
Rusty Russell 已提交
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
R
Rusty Russell 已提交
4 5
 * Host to do something.  This file also contains useful helper routines.
:*/
R
Rusty Russell 已提交
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>
R
Rusty Russell 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <asm/paravirt.h>
#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/poll.h>
#include <asm/asm-offsets.h>
#include "lg.h"


static struct vm_struct *switcher_vma;
static struct page **switcher_page;

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

R
Rusty Russell 已提交
29 30
/*H:010
 * We need to set up the Switcher at a high virtual address.  Remember the
R
Rusty Russell 已提交
31 32 33 34 35 36 37 38
 * 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
R
Rusty Russell 已提交
39 40
 * it's not a simple one-liner.
 */
R
Rusty Russell 已提交
41 42 43 44 45
static __init int map_switcher(void)
{
	int i, err;
	struct page **pagep;

R
Rusty Russell 已提交
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.
	 */

R
Rusty Russell 已提交
54 55 56 57
	/*
	 * We allocate an array of struct page pointers.  map_vm_area() wants
	 * this, rather than just an array of pages.
	 */
R
Rusty Russell 已提交
58 59 60 61 62 63 64
	switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES,
				GFP_KERNEL);
	if (!switcher_page) {
		err = -ENOMEM;
		goto out;
	}

R
Rusty Russell 已提交
65 66 67 68
	/*
	 * Now we actually allocate the pages.  The Guest will see these pages,
	 * so we make sure they're zeroed.
	 */
R
Rusty Russell 已提交
69 70 71 72 73 74 75 76 77
	for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
		unsigned long addr = get_zeroed_page(GFP_KERNEL);
		if (!addr) {
			err = -ENOMEM;
			goto free_some_pages;
		}
		switcher_page[i] = virt_to_page(addr);
	}

R
Rusty Russell 已提交
78 79
	/*
	 * First we check that the Switcher won't overlap the fixmap area at
R
Rusty Russell 已提交
80
	 * the top of memory.  It's currently nowhere near, but it could have
R
Rusty Russell 已提交
81 82
	 * very strange effects if it ever happened.
	 */
R
Rusty Russell 已提交
83 84 85 86 87 88
	if (SWITCHER_ADDR + (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE > FIXADDR_START){
		err = -ENOMEM;
		printk("lguest: mapping switcher would thwack fixmap\n");
		goto free_pages;
	}

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

R
Rusty Russell 已提交
104 105
	/*
	 * This code actually sets up the pages we've allocated to appear at
R
Rusty Russell 已提交
106 107 108
	 * SWITCHER_ADDR.  map_vm_area() takes the vma we allocated above, the
	 * kind of pages we're mapping (kernel pages), and a pointer to our
	 * array of struct pages.  It increments that pointer, but we don't
R
Rusty Russell 已提交
109 110
	 * care.
	 */
R
Rusty Russell 已提交
111
	pagep = switcher_page;
112
	err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, &pagep);
R
Rusty Russell 已提交
113 114 115 116
	if (err) {
		printk("lguest: map_vm_area failed: %i\n", err);
		goto free_vma;
	}
R
Rusty Russell 已提交
117

R
Rusty Russell 已提交
118 119 120 121
	/*
	 * Now the Switcher is mapped at the right address, we can't fail!
	 * Copy in the compiled-in Switcher code (from <arch>_switcher.S).
	 */
R
Rusty Russell 已提交
122 123 124 125 126
	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);
R
Rusty Russell 已提交
127
	/* And we succeeded... */
R
Rusty Russell 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
	return 0;

free_vma:
	vunmap(switcher_vma->addr);
free_pages:
	i = TOTAL_SWITCHER_PAGES;
free_some_pages:
	for (--i; i >= 0; i--)
		__free_pages(switcher_page[i], 0);
	kfree(switcher_page);
out:
	return err;
}
R
Rusty Russell 已提交
141
/*:*/
R
Rusty Russell 已提交
142

R
Rusty Russell 已提交
143
/* Cleaning up the mapping when the module is unloaded is almost... too easy. */
R
Rusty Russell 已提交
144 145 146 147
static void unmap_switcher(void)
{
	unsigned int i;

R
Rusty Russell 已提交
148
	/* vunmap() undoes *both* map_vm_area() and __get_vm_area(). */
R
Rusty Russell 已提交
149
	vunmap(switcher_vma->addr);
R
Rusty Russell 已提交
150
	/* Now we just need to free the pages we copied the switcher into */
R
Rusty Russell 已提交
151 152
	for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
		__free_pages(switcher_page[i], 0);
153
	kfree(switcher_page);
R
Rusty Russell 已提交
154 155
}

R
Rusty Russell 已提交
156
/*H:032
157 158
 * Dealing With Guest Memory.
 *
R
Rusty Russell 已提交
159 160 161
 * Before we go too much further into the Host, we need to grok the routines
 * we use to deal with Guest memory.
 *
162
 * When the Guest gives us (what it thinks is) a physical address, we can use
163 164
 * the normal copy_from_user() & copy_to_user() on the corresponding place in
 * the memory region allocated by the Launcher.
165 166 167 168
 *
 * 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
R
Rusty Russell 已提交
169 170
 * positive by overflowing, too.
 */
171 172
bool lguest_address_ok(const struct lguest *lg,
		       unsigned long addr, unsigned long len)
R
Rusty Russell 已提交
173 174 175 176
{
	return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
}

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

192
/* This is the write (copy into Guest) version. */
193
void __lgwrite(struct lg_cpu *cpu, unsigned long addr, const void *b,
R
Rusty Russell 已提交
194
	       unsigned bytes)
R
Rusty Russell 已提交
195
{
196 197 198
	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);
R
Rusty Russell 已提交
199
}
R
Rusty Russell 已提交
200
/*:*/
R
Rusty Russell 已提交
201

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

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

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

R
Rusty Russell 已提交
235
		/* Check for signals */
R
Rusty Russell 已提交
236 237 238
		if (signal_pending(current))
			return -ERESTARTSYS;

R
Rusty Russell 已提交
239 240
		/*
		 * Check if there are any interrupts which can be delivered now:
241
		 * if so, this sets up the hander to be executed when we next
R
Rusty Russell 已提交
242 243
		 * run the Guest.
		 */
244
		irq = interrupt_pending(cpu, &more);
R
Rusty Russell 已提交
245
		if (irq < LGUEST_IRQS)
246
			try_deliver_interrupt(cpu, irq, more);
R
Rusty Russell 已提交
247

R
Rusty Russell 已提交
248 249
		/*
		 * All long-lived kernel loops need to check with this horrible
R
Rusty Russell 已提交
250
		 * thing called the freezer.  If the Host is trying to suspend,
R
Rusty Russell 已提交
251 252
		 * it stops us.
		 */
R
Rusty Russell 已提交
253 254
		try_to_freeze();

R
Rusty Russell 已提交
255 256 257 258
		/*
		 * Just make absolutely sure the Guest is still alive.  One of
		 * those hypercalls could have been fatal, for example.
		 */
259
		if (cpu->lg->dead)
R
Rusty Russell 已提交
260 261
			break;

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

R
Rusty Russell 已提交
279 280 281 282
		/*
		 * OK, now we're ready to jump into the Guest.  First we put up
		 * the "Do Not Disturb" sign:
		 */
R
Rusty Russell 已提交
283 284
		local_irq_disable();

285
		/* Actually run the Guest until something happens. */
286
		lguest_arch_run_guest(cpu);
R
Rusty Russell 已提交
287 288

		/* Now we're ready to be interrupted or moved to other CPUs */
R
Rusty Russell 已提交
289 290
		local_irq_enable();

291
		/* Now we deal with whatever happened to the Guest. */
292
		lguest_arch_handle_trap(cpu);
R
Rusty Russell 已提交
293
	}
294

295
	/* Special case: Guest is 'dead' but wants a reboot. */
296
	if (cpu->lg->dead == ERR_PTR(-ERESTART))
B
Balaji Rao 已提交
297
		return -ERESTART;
298

R
Rusty Russell 已提交
299
	/* The Guest is dead => "No such file or directory" */
R
Rusty Russell 已提交
300 301 302
	return -ENOENT;
}

R
Rusty Russell 已提交
303 304 305 306 307 308 309 310
/*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.
 */
R
Rusty Russell 已提交
311 312 313 314
static int __init init(void)
{
	int err;

R
Rusty Russell 已提交
315
	/* Lguest can't run under Xen, VMI or itself.  It does Tricky Stuff. */
R
Rusty Russell 已提交
316
	if (paravirt_enabled()) {
317
		printk("lguest is afraid of being a guest\n");
R
Rusty Russell 已提交
318 319 320
		return -EPERM;
	}

R
Rusty Russell 已提交
321
	/* First we put the Switcher up in very high virtual memory. */
R
Rusty Russell 已提交
322 323
	err = map_switcher();
	if (err)
324
		goto out;
R
Rusty Russell 已提交
325

R
Rusty Russell 已提交
326
	/* Now we set up the pagetable implementation for the Guests. */
R
Rusty Russell 已提交
327
	err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
328 329
	if (err)
		goto unmap;
R
Rusty Russell 已提交
330

331 332 333 334 335
	/* We might need to reserve an interrupt vector. */
	err = init_interrupts();
	if (err)
		goto free_pgtables;

R
Rusty Russell 已提交
336
	/* /dev/lguest needs to be registered. */
R
Rusty Russell 已提交
337
	err = lguest_device_init();
338 339
	if (err)
		goto free_interrupts;
R
Rusty Russell 已提交
340

341 342
	/* Finally we do some architecture-specific setup. */
	lguest_arch_host_init();
R
Rusty Russell 已提交
343 344

	/* All good! */
R
Rusty Russell 已提交
345
	return 0;
346 347 348 349 350 351 352 353 354

free_interrupts:
	free_interrupts();
free_pgtables:
	free_pagetables();
unmap:
	unmap_switcher();
out:
	return err;
R
Rusty Russell 已提交
355 356
}

R
Rusty Russell 已提交
357
/* Cleaning up is just the same code, backwards.  With a little French. */
R
Rusty Russell 已提交
358 359 360
static void __exit fini(void)
{
	lguest_device_remove();
361
	free_interrupts();
R
Rusty Russell 已提交
362 363
	free_pagetables();
	unmap_switcher();
R
Rusty Russell 已提交
364

365
	lguest_arch_host_fini();
R
Rusty Russell 已提交
366
}
367
/*:*/
R
Rusty Russell 已提交
368

R
Rusty Russell 已提交
369 370 371 372
/*
 * The Host side of lguest can be a module.  This is a nice way for people to
 * play with it.
 */
R
Rusty Russell 已提交
373 374 375 376
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");