core.c 9.1 KB
Newer Older
1 2 3 4 5
/*P:400 This contains run_guest() which actually calls into the Host<->Guest
 * Switcher and analyzes the return, such as determining if the Guest wants the
 * Host to do something.  This file also contains useful helper routines, and a
 * couple of non-obvious setup and teardown pieces which were implemented after
 * days of debugging pain. :*/
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 31 32 33 34 35 36 37
/*H:010 We need to set up the Switcher at a high virtual address.  Remember the
 * 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
38
 * it's not a simple one-liner. */
R
Rusty Russell 已提交
39 40 41 42 43
static __init int map_switcher(void)
{
	int i, err;
	struct page **pagep;

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

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

R
Rusty Russell 已提交
61 62
	/* Now we actually allocate the pages.  The Guest will see these pages,
	 * so we make sure they're zeroed. */
R
Rusty Russell 已提交
63 64 65 66 67 68 69 70 71
	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 已提交
72 73 74
	/* Now we reserve the "virtual memory area" we want: 0xFFC00000
	 * (SWITCHER_ADDR).  We might not get it in theory, but in practice
	 * it's worked so far. */
R
Rusty Russell 已提交
75 76 77 78 79 80 81 82
	switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE,
				       VM_ALLOC, SWITCHER_ADDR, VMALLOC_END);
	if (!switcher_vma) {
		err = -ENOMEM;
		printk("lguest: could not map switcher pages high\n");
		goto free_pages;
	}

R
Rusty Russell 已提交
83 84 85 86 87
	/* This code actually sets up the pages we've allocated to appear at
	 * 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
	 * care. */
R
Rusty Russell 已提交
88 89 90 91 92 93
	pagep = switcher_page;
	err = map_vm_area(switcher_vma, PAGE_KERNEL, &pagep);
	if (err) {
		printk("lguest: map_vm_area failed: %i\n", err);
		goto free_vma;
	}
R
Rusty Russell 已提交
94

95 96
	/* 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 已提交
97 98 99 100 101
	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 已提交
102
	/* And we succeeded... */
R
Rusty Russell 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
	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 已提交
116
/*:*/
R
Rusty Russell 已提交
117

R
Rusty Russell 已提交
118 119
/* Cleaning up the mapping when the module is unloaded is almost...
 * too easy. */
R
Rusty Russell 已提交
120 121 122 123
static void unmap_switcher(void)
{
	unsigned int i;

R
Rusty Russell 已提交
124
	/* vunmap() undoes *both* map_vm_area() and __get_vm_area(). */
R
Rusty Russell 已提交
125
	vunmap(switcher_vma->addr);
R
Rusty Russell 已提交
126
	/* Now we just need to free the pages we copied the switcher into */
R
Rusty Russell 已提交
127 128 129 130
	for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
		__free_pages(switcher_page[i], 0);
}

131 132 133 134
/*L:305
 * Dealing With Guest Memory.
 *
 * When the Guest gives us (what it thinks is) a physical address, we can use
135 136
 * the normal copy_from_user() & copy_to_user() on the corresponding place in
 * the memory region allocated by the Launcher.
137 138 139 140 141
 *
 * 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
 * positive by overflowing, too. */
R
Rusty Russell 已提交
142 143 144 145 146 147
int lguest_address_ok(const struct lguest *lg,
		      unsigned long addr, unsigned long len)
{
	return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
}

R
Rusty Russell 已提交
148 149 150 151
/* This routine copies memory from the Guest.  Here we can see how useful the
 * kill_lguest() routine we met in the Launcher can be: we return a random
 * value (all zeroes) instead of needing to return an error. */
void __lgread(struct lguest *lg, void *b, unsigned long addr, unsigned bytes)
R
Rusty Russell 已提交
152 153
{
	if (!lguest_address_ok(lg, addr, bytes)
154
	    || copy_from_user(b, lg->mem_base + addr, bytes) != 0) {
R
Rusty Russell 已提交
155 156 157 158 159 160
		/* copy_from_user should do this, but as we rely on it... */
		memset(b, 0, bytes);
		kill_guest(lg, "bad read address %#lx len %u", addr, bytes);
	}
}

R
Rusty Russell 已提交
161 162 163
/* This is the write (copy into guest) version. */
void __lgwrite(struct lguest *lg, unsigned long addr, const void *b,
	       unsigned bytes)
R
Rusty Russell 已提交
164 165
{
	if (!lguest_address_ok(lg, addr, bytes)
166
	    || copy_to_user(lg->mem_base + addr, b, bytes) != 0)
R
Rusty Russell 已提交
167 168
		kill_guest(lg, "bad write address %#lx len %u", addr, bytes);
}
R
Rusty Russell 已提交
169
/*:*/
R
Rusty Russell 已提交
170

R
Rusty Russell 已提交
171 172 173
/*H:030 Let's jump straight to the the main loop which runs the Guest.
 * Remember, this is called by the Launcher reading /dev/lguest, and we keep
 * going around and around until something interesting happens. */
R
Rusty Russell 已提交
174 175
int run_guest(struct lguest *lg, unsigned long __user *user)
{
R
Rusty Russell 已提交
176
	/* We stop running once the Guest is dead. */
R
Rusty Russell 已提交
177
	while (!lg->dead) {
178 179 180 181
		/* First we run any hypercalls the Guest wants done. */
		if (lg->hcall)
			do_hypercalls(lg);

182
		/* It's possible the Guest did a NOTIFY hypercall to the
R
Rusty Russell 已提交
183
		 * Launcher, in which case we return from the read() now. */
184 185
		if (lg->pending_notify) {
			if (put_user(lg->pending_notify, user))
R
Rusty Russell 已提交
186
				return -EFAULT;
187
			return sizeof(lg->pending_notify);
R
Rusty Russell 已提交
188 189
		}

R
Rusty Russell 已提交
190
		/* Check for signals */
R
Rusty Russell 已提交
191 192 193 194 195 196 197
		if (signal_pending(current))
			return -ERESTARTSYS;

		/* If Waker set break_out, return to Launcher. */
		if (lg->break_out)
			return -EAGAIN;

R
Rusty Russell 已提交
198 199 200
		/* Check if there are any interrupts which can be delivered
		 * now: if so, this sets up the hander to be executed when we
		 * next run the Guest. */
R
Rusty Russell 已提交
201 202
		maybe_do_interrupt(lg);

R
Rusty Russell 已提交
203 204 205
		/* 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. */
R
Rusty Russell 已提交
206 207
		try_to_freeze();

R
Rusty Russell 已提交
208 209
		/* Just make absolutely sure the Guest is still alive.  One of
		 * those hypercalls could have been fatal, for example. */
R
Rusty Russell 已提交
210 211 212
		if (lg->dead)
			break;

R
Rusty Russell 已提交
213 214
		/* If the Guest asked to be stopped, we sleep.  The Guest's
		 * clock timer or LHCALL_BREAK from the Waker will wake us. */
R
Rusty Russell 已提交
215 216 217 218 219 220
		if (lg->halted) {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule();
			continue;
		}

R
Rusty Russell 已提交
221 222
		/* OK, now we're ready to jump into the Guest.  First we put up
		 * the "Do Not Disturb" sign: */
R
Rusty Russell 已提交
223 224
		local_irq_disable();

225 226
		/* Actually run the Guest until something happens. */
		lguest_arch_run_guest(lg);
R
Rusty Russell 已提交
227 228

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

231 232
		/* Now we deal with whatever happened to the Guest. */
		lguest_arch_handle_trap(lg);
R
Rusty Russell 已提交
233
	}
234

R
Rusty Russell 已提交
235
	/* The Guest is dead => "No such file or directory" */
R
Rusty Russell 已提交
236 237 238
	return -ENOENT;
}

R
Rusty Russell 已提交
239 240 241 242 243 244 245 246
/*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 已提交
247 248 249 250
static int __init init(void)
{
	int err;

R
Rusty Russell 已提交
251
	/* Lguest can't run under Xen, VMI or itself.  It does Tricky Stuff. */
R
Rusty Russell 已提交
252
	if (paravirt_enabled()) {
253
		printk("lguest is afraid of %s\n", pv_info.name);
R
Rusty Russell 已提交
254 255 256
		return -EPERM;
	}

R
Rusty Russell 已提交
257
	/* First we put the Switcher up in very high virtual memory. */
R
Rusty Russell 已提交
258 259
	err = map_switcher();
	if (err)
260
		goto out;
R
Rusty Russell 已提交
261

R
Rusty Russell 已提交
262
	/* Now we set up the pagetable implementation for the Guests. */
R
Rusty Russell 已提交
263
	err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
264 265
	if (err)
		goto unmap;
R
Rusty Russell 已提交
266

267 268 269 270 271
	/* We might need to reserve an interrupt vector. */
	err = init_interrupts();
	if (err)
		goto free_pgtables;

R
Rusty Russell 已提交
272
	/* /dev/lguest needs to be registered. */
R
Rusty Russell 已提交
273
	err = lguest_device_init();
274 275
	if (err)
		goto free_interrupts;
R
Rusty Russell 已提交
276

277 278
	/* Finally we do some architecture-specific setup. */
	lguest_arch_host_init();
R
Rusty Russell 已提交
279 280

	/* All good! */
R
Rusty Russell 已提交
281
	return 0;
282 283 284 285 286 287 288 289 290

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

R
Rusty Russell 已提交
293
/* Cleaning up is just the same code, backwards.  With a little French. */
R
Rusty Russell 已提交
294 295 296
static void __exit fini(void)
{
	lguest_device_remove();
297
	free_interrupts();
R
Rusty Russell 已提交
298 299
	free_pagetables();
	unmap_switcher();
R
Rusty Russell 已提交
300

301
	lguest_arch_host_fini();
R
Rusty Russell 已提交
302
}
303
/*:*/
R
Rusty Russell 已提交
304

R
Rusty Russell 已提交
305 306
/* The Host side of lguest can be a module.  This is a nice way for people to
 * play with it.  */
R
Rusty Russell 已提交
307 308 309 310
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");