diff options
-rw-r--r-- | Documentation/lguest/lguest.c | 540 | ||||
-rw-r--r-- | arch/x86/include/asm/lguest.h | 3 | ||||
-rw-r--r-- | arch/x86/include/asm/lguest_hcall.h | 10 | ||||
-rw-r--r-- | arch/x86/lguest/boot.c | 428 | ||||
-rw-r--r-- | arch/x86/lguest/i386_head.S | 110 | ||||
-rw-r--r-- | drivers/lguest/core.c | 114 | ||||
-rw-r--r-- | drivers/lguest/hypercalls.c | 141 | ||||
-rw-r--r-- | drivers/lguest/interrupts_and_traps.c | 288 | ||||
-rw-r--r-- | drivers/lguest/lg.h | 23 | ||||
-rw-r--r-- | drivers/lguest/lguest_device.c | 150 | ||||
-rw-r--r-- | drivers/lguest/lguest_user.c | 137 | ||||
-rw-r--r-- | drivers/lguest/page_tables.c | 427 | ||||
-rw-r--r-- | drivers/lguest/segments.c | 106 | ||||
-rw-r--r-- | drivers/lguest/x86/core.c | 372 | ||||
-rw-r--r-- | drivers/lguest/x86/switcher_32.S | 18 | ||||
-rw-r--r-- | include/linux/lguest.h | 36 | ||||
-rw-r--r-- | include/linux/lguest_launcher.h | 18 |
17 files changed, 1906 insertions, 1015 deletions
diff --git a/Documentation/lguest/lguest.c b/Documentation/lguest/lguest.c index 45d7d6dcae7a..aa66a52b73e9 100644 --- a/Documentation/lguest/lguest.c +++ b/Documentation/lguest/lguest.c | |||
@@ -1,7 +1,9 @@ | |||
1 | /*P:100 This is the Launcher code, a simple program which lays out the | 1 | /*P:100 |
2 | * "physical" memory for the new Guest by mapping the kernel image and | 2 | * This is the Launcher code, a simple program which lays out the "physical" |
3 | * the virtual devices, then opens /dev/lguest to tell the kernel | 3 | * memory for the new Guest by mapping the kernel image and the virtual |
4 | * about the Guest and control it. :*/ | 4 | * devices, then opens /dev/lguest to tell the kernel about the Guest and |
5 | * control it. | ||
6 | :*/ | ||
5 | #define _LARGEFILE64_SOURCE | 7 | #define _LARGEFILE64_SOURCE |
6 | #define _GNU_SOURCE | 8 | #define _GNU_SOURCE |
7 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -46,13 +48,15 @@ | |||
46 | #include "linux/virtio_rng.h" | 48 | #include "linux/virtio_rng.h" |
47 | #include "linux/virtio_ring.h" | 49 | #include "linux/virtio_ring.h" |
48 | #include "asm/bootparam.h" | 50 | #include "asm/bootparam.h" |
49 | /*L:110 We can ignore the 39 include files we need for this program, but I do | 51 | /*L:110 |
50 | * want to draw attention to the use of kernel-style types. | 52 | * We can ignore the 39 include files we need for this program, but I do want |
53 | * to draw attention to the use of kernel-style types. | ||
51 | * | 54 | * |
52 | * As Linus said, "C is a Spartan language, and so should your naming be." I | 55 | * As Linus said, "C is a Spartan language, and so should your naming be." I |
53 | * like these abbreviations, so we define them here. Note that u64 is always | 56 | * like these abbreviations, so we define them here. Note that u64 is always |
54 | * unsigned long long, which works on all Linux systems: this means that we can | 57 | * unsigned long long, which works on all Linux systems: this means that we can |
55 | * use %llu in printf for any u64. */ | 58 | * use %llu in printf for any u64. |
59 | */ | ||
56 | typedef unsigned long long u64; | 60 | typedef unsigned long long u64; |
57 | typedef uint32_t u32; | 61 | typedef uint32_t u32; |
58 | typedef uint16_t u16; | 62 | typedef uint16_t u16; |
@@ -69,8 +73,10 @@ typedef uint8_t u8; | |||
69 | /* This will occupy 3 pages: it must be a power of 2. */ | 73 | /* This will occupy 3 pages: it must be a power of 2. */ |
70 | #define VIRTQUEUE_NUM 256 | 74 | #define VIRTQUEUE_NUM 256 |
71 | 75 | ||
72 | /*L:120 verbose is both a global flag and a macro. The C preprocessor allows | 76 | /*L:120 |
73 | * this, and although I wouldn't recommend it, it works quite nicely here. */ | 77 | * verbose is both a global flag and a macro. The C preprocessor allows |
78 | * this, and although I wouldn't recommend it, it works quite nicely here. | ||
79 | */ | ||
74 | static bool verbose; | 80 | static bool verbose; |
75 | #define verbose(args...) \ | 81 | #define verbose(args...) \ |
76 | do { if (verbose) printf(args); } while(0) | 82 | do { if (verbose) printf(args); } while(0) |
@@ -100,8 +106,7 @@ struct device_list | |||
100 | 106 | ||
101 | /* A single linked list of devices. */ | 107 | /* A single linked list of devices. */ |
102 | struct device *dev; | 108 | struct device *dev; |
103 | /* And a pointer to the last device for easy append and also for | 109 | /* And a pointer to the last device for easy append. */ |
104 | * configuration appending. */ | ||
105 | struct device *lastdev; | 110 | struct device *lastdev; |
106 | }; | 111 | }; |
107 | 112 | ||
@@ -168,20 +173,24 @@ static char **main_args; | |||
168 | /* The original tty settings to restore on exit. */ | 173 | /* The original tty settings to restore on exit. */ |
169 | static struct termios orig_term; | 174 | static struct termios orig_term; |
170 | 175 | ||
171 | /* We have to be careful with barriers: our devices are all run in separate | 176 | /* |
177 | * We have to be careful with barriers: our devices are all run in separate | ||
172 | * threads and so we need to make sure that changes visible to the Guest happen | 178 | * threads and so we need to make sure that changes visible to the Guest happen |
173 | * in precise order. */ | 179 | * in precise order. |
180 | */ | ||
174 | #define wmb() __asm__ __volatile__("" : : : "memory") | 181 | #define wmb() __asm__ __volatile__("" : : : "memory") |
175 | #define mb() __asm__ __volatile__("" : : : "memory") | 182 | #define mb() __asm__ __volatile__("" : : : "memory") |
176 | 183 | ||
177 | /* Convert an iovec element to the given type. | 184 | /* |
185 | * Convert an iovec element to the given type. | ||
178 | * | 186 | * |
179 | * This is a fairly ugly trick: we need to know the size of the type and | 187 | * This is a fairly ugly trick: we need to know the size of the type and |
180 | * alignment requirement to check the pointer is kosher. It's also nice to | 188 | * alignment requirement to check the pointer is kosher. It's also nice to |
181 | * have the name of the type in case we report failure. | 189 | * have the name of the type in case we report failure. |
182 | * | 190 | * |
183 | * Typing those three things all the time is cumbersome and error prone, so we | 191 | * Typing those three things all the time is cumbersome and error prone, so we |
184 | * have a macro which sets them all up and passes to the real function. */ | 192 | * have a macro which sets them all up and passes to the real function. |
193 | */ | ||
185 | #define convert(iov, type) \ | 194 | #define convert(iov, type) \ |
186 | ((type *)_convert((iov), sizeof(type), __alignof__(type), #type)) | 195 | ((type *)_convert((iov), sizeof(type), __alignof__(type), #type)) |
187 | 196 | ||
@@ -198,8 +207,10 @@ static void *_convert(struct iovec *iov, size_t size, size_t align, | |||
198 | /* Wrapper for the last available index. Makes it easier to change. */ | 207 | /* Wrapper for the last available index. Makes it easier to change. */ |
199 | #define lg_last_avail(vq) ((vq)->last_avail_idx) | 208 | #define lg_last_avail(vq) ((vq)->last_avail_idx) |
200 | 209 | ||
201 | /* The virtio configuration space is defined to be little-endian. x86 is | 210 | /* |
202 | * little-endian too, but it's nice to be explicit so we have these helpers. */ | 211 | * The virtio configuration space is defined to be little-endian. x86 is |
212 | * little-endian too, but it's nice to be explicit so we have these helpers. | ||
213 | */ | ||
203 | #define cpu_to_le16(v16) (v16) | 214 | #define cpu_to_le16(v16) (v16) |
204 | #define cpu_to_le32(v32) (v32) | 215 | #define cpu_to_le32(v32) (v32) |
205 | #define cpu_to_le64(v64) (v64) | 216 | #define cpu_to_le64(v64) (v64) |
@@ -241,11 +252,12 @@ static u8 *get_feature_bits(struct device *dev) | |||
241 | + dev->num_vq * sizeof(struct lguest_vqconfig); | 252 | + dev->num_vq * sizeof(struct lguest_vqconfig); |
242 | } | 253 | } |
243 | 254 | ||
244 | /*L:100 The Launcher code itself takes us out into userspace, that scary place | 255 | /*L:100 |
245 | * where pointers run wild and free! Unfortunately, like most userspace | 256 | * The Launcher code itself takes us out into userspace, that scary place where |
246 | * programs, it's quite boring (which is why everyone likes to hack on the | 257 | * pointers run wild and free! Unfortunately, like most userspace programs, |
247 | * kernel!). Perhaps if you make up an Lguest Drinking Game at this point, it | 258 | * it's quite boring (which is why everyone likes to hack on the kernel!). |
248 | * will get you through this section. Or, maybe not. | 259 | * Perhaps if you make up an Lguest Drinking Game at this point, it will get |
260 | * you through this section. Or, maybe not. | ||
249 | * | 261 | * |
250 | * The Launcher sets up a big chunk of memory to be the Guest's "physical" | 262 | * The Launcher sets up a big chunk of memory to be the Guest's "physical" |
251 | * memory and stores it in "guest_base". In other words, Guest physical == | 263 | * memory and stores it in "guest_base". In other words, Guest physical == |
@@ -253,7 +265,8 @@ static u8 *get_feature_bits(struct device *dev) | |||
253 | * | 265 | * |
254 | * This can be tough to get your head around, but usually it just means that we | 266 | * This can be tough to get your head around, but usually it just means that we |
255 | * use these trivial conversion functions when the Guest gives us it's | 267 | * use these trivial conversion functions when the Guest gives us it's |
256 | * "physical" addresses: */ | 268 | * "physical" addresses: |
269 | */ | ||
257 | static void *from_guest_phys(unsigned long addr) | 270 | static void *from_guest_phys(unsigned long addr) |
258 | { | 271 | { |
259 | return guest_base + addr; | 272 | return guest_base + addr; |
@@ -268,7 +281,8 @@ static unsigned long to_guest_phys(const void *addr) | |||
268 | * Loading the Kernel. | 281 | * Loading the Kernel. |
269 | * | 282 | * |
270 | * We start with couple of simple helper routines. open_or_die() avoids | 283 | * We start with couple of simple helper routines. open_or_die() avoids |
271 | * error-checking code cluttering the callers: */ | 284 | * error-checking code cluttering the callers: |
285 | */ | ||
272 | static int open_or_die(const char *name, int flags) | 286 | static int open_or_die(const char *name, int flags) |
273 | { | 287 | { |
274 | int fd = open(name, flags); | 288 | int fd = open(name, flags); |
@@ -283,8 +297,10 @@ static void *map_zeroed_pages(unsigned int num) | |||
283 | int fd = open_or_die("/dev/zero", O_RDONLY); | 297 | int fd = open_or_die("/dev/zero", O_RDONLY); |
284 | void *addr; | 298 | void *addr; |
285 | 299 | ||
286 | /* We use a private mapping (ie. if we write to the page, it will be | 300 | /* |
287 | * copied). */ | 301 | * We use a private mapping (ie. if we write to the page, it will be |
302 | * copied). | ||
303 | */ | ||
288 | addr = mmap(NULL, getpagesize() * num, | 304 | addr = mmap(NULL, getpagesize() * num, |
289 | PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, fd, 0); | 305 | PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, fd, 0); |
290 | if (addr == MAP_FAILED) | 306 | if (addr == MAP_FAILED) |
@@ -305,20 +321,24 @@ static void *get_pages(unsigned int num) | |||
305 | return addr; | 321 | return addr; |
306 | } | 322 | } |
307 | 323 | ||
308 | /* This routine is used to load the kernel or initrd. It tries mmap, but if | 324 | /* |
325 | * This routine is used to load the kernel or initrd. It tries mmap, but if | ||
309 | * that fails (Plan 9's kernel file isn't nicely aligned on page boundaries), | 326 | * that fails (Plan 9's kernel file isn't nicely aligned on page boundaries), |
310 | * it falls back to reading the memory in. */ | 327 | * it falls back to reading the memory in. |
328 | */ | ||
311 | static void map_at(int fd, void *addr, unsigned long offset, unsigned long len) | 329 | static void map_at(int fd, void *addr, unsigned long offset, unsigned long len) |
312 | { | 330 | { |
313 | ssize_t r; | 331 | ssize_t r; |
314 | 332 | ||
315 | /* We map writable even though for some segments are marked read-only. | 333 | /* |
334 | * We map writable even though for some segments are marked read-only. | ||
316 | * The kernel really wants to be writable: it patches its own | 335 | * The kernel really wants to be writable: it patches its own |
317 | * instructions. | 336 | * instructions. |
318 | * | 337 | * |
319 | * MAP_PRIVATE means that the page won't be copied until a write is | 338 | * MAP_PRIVATE means that the page won't be copied until a write is |
320 | * done to it. This allows us to share untouched memory between | 339 | * done to it. This allows us to share untouched memory between |
321 | * Guests. */ | 340 | * Guests. |
341 | */ | ||
322 | if (mmap(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC, | 342 | if (mmap(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC, |
323 | MAP_FIXED|MAP_PRIVATE, fd, offset) != MAP_FAILED) | 343 | MAP_FIXED|MAP_PRIVATE, fd, offset) != MAP_FAILED) |
324 | return; | 344 | return; |
@@ -329,7 +349,8 @@ static void map_at(int fd, void *addr, unsigned long offset, unsigned long len) | |||
329 | err(1, "Reading offset %lu len %lu gave %zi", offset, len, r); | 349 | err(1, "Reading offset %lu len %lu gave %zi", offset, len, r); |
330 | } | 350 | } |
331 | 351 | ||
332 | /* This routine takes an open vmlinux image, which is in ELF, and maps it into | 352 | /* |
353 | * This routine takes an open vmlinux image, which is in ELF, and maps it into | ||
333 | * the Guest memory. ELF = Embedded Linking Format, which is the format used | 354 | * the Guest memory. ELF = Embedded Linking Format, which is the format used |
334 | * by all modern binaries on Linux including the kernel. | 355 | * by all modern binaries on Linux including the kernel. |
335 | * | 356 | * |
@@ -337,23 +358,28 @@ static void map_at(int fd, void *addr, unsigned long offset, unsigned long len) | |||
337 | * address. We use the physical address; the Guest will map itself to the | 358 | * address. We use the physical address; the Guest will map itself to the |
338 | * virtual address. | 359 | * virtual address. |
339 | * | 360 | * |
340 | * We return the starting address. */ | 361 | * We return the starting address. |
362 | */ | ||
341 | static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr) | 363 | static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr) |
342 | { | 364 | { |
343 | Elf32_Phdr phdr[ehdr->e_phnum]; | 365 | Elf32_Phdr phdr[ehdr->e_phnum]; |
344 | unsigned int i; | 366 | unsigned int i; |
345 | 367 | ||
346 | /* Sanity checks on the main ELF header: an x86 executable with a | 368 | /* |
347 | * reasonable number of correctly-sized program headers. */ | 369 | * Sanity checks on the main ELF header: an x86 executable with a |
370 | * reasonable number of correctly-sized program headers. | ||
371 | */ | ||
348 | if (ehdr->e_type != ET_EXEC | 372 | if (ehdr->e_type != ET_EXEC |
349 | || ehdr->e_machine != EM_386 | 373 | || ehdr->e_machine != EM_386 |
350 | || ehdr->e_phentsize != sizeof(Elf32_Phdr) | 374 | || ehdr->e_phentsize != sizeof(Elf32_Phdr) |
351 | || ehdr->e_phnum < 1 || ehdr->e_phnum > 65536U/sizeof(Elf32_Phdr)) | 375 | || ehdr->e_phnum < 1 || ehdr->e_phnum > 65536U/sizeof(Elf32_Phdr)) |
352 | errx(1, "Malformed elf header"); | 376 | errx(1, "Malformed elf header"); |
353 | 377 | ||
354 | /* An ELF executable contains an ELF header and a number of "program" | 378 | /* |
379 | * An ELF executable contains an ELF header and a number of "program" | ||
355 | * headers which indicate which parts ("segments") of the program to | 380 | * headers which indicate which parts ("segments") of the program to |
356 | * load where. */ | 381 | * load where. |
382 | */ | ||
357 | 383 | ||
358 | /* We read in all the program headers at once: */ | 384 | /* We read in all the program headers at once: */ |
359 | if (lseek(elf_fd, ehdr->e_phoff, SEEK_SET) < 0) | 385 | if (lseek(elf_fd, ehdr->e_phoff, SEEK_SET) < 0) |
@@ -361,8 +387,10 @@ static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr) | |||
361 | if (read(elf_fd, phdr, sizeof(phdr)) != sizeof(phdr)) | 387 | if (read(elf_fd, phdr, sizeof(phdr)) != sizeof(phdr)) |
362 | err(1, "Reading program headers"); | 388 | err(1, "Reading program headers"); |
363 | 389 | ||
364 | /* Try all the headers: there are usually only three. A read-only one, | 390 | /* |
365 | * a read-write one, and a "note" section which we don't load. */ | 391 | * Try all the headers: there are usually only three. A read-only one, |
392 | * a read-write one, and a "note" section which we don't load. | ||
393 | */ | ||
366 | for (i = 0; i < ehdr->e_phnum; i++) { | 394 | for (i = 0; i < ehdr->e_phnum; i++) { |
367 | /* If this isn't a loadable segment, we ignore it */ | 395 | /* If this isn't a loadable segment, we ignore it */ |
368 | if (phdr[i].p_type != PT_LOAD) | 396 | if (phdr[i].p_type != PT_LOAD) |
@@ -380,13 +408,15 @@ static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr) | |||
380 | return ehdr->e_entry; | 408 | return ehdr->e_entry; |
381 | } | 409 | } |
382 | 410 | ||
383 | /*L:150 A bzImage, unlike an ELF file, is not meant to be loaded. You're | 411 | /*L:150 |
384 | * supposed to jump into it and it will unpack itself. We used to have to | 412 | * A bzImage, unlike an ELF file, is not meant to be loaded. You're supposed |
385 | * perform some hairy magic because the unpacking code scared me. | 413 | * to jump into it and it will unpack itself. We used to have to perform some |
414 | * hairy magic because the unpacking code scared me. | ||
386 | * | 415 | * |
387 | * Fortunately, Jeremy Fitzhardinge convinced me it wasn't that hard and wrote | 416 | * Fortunately, Jeremy Fitzhardinge convinced me it wasn't that hard and wrote |
388 | * a small patch to jump over the tricky bits in the Guest, so now we just read | 417 | * a small patch to jump over the tricky bits in the Guest, so now we just read |
389 | * the funky header so we know where in the file to load, and away we go! */ | 418 | * the funky header so we know where in the file to load, and away we go! |
419 | */ | ||
390 | static unsigned long load_bzimage(int fd) | 420 | static unsigned long load_bzimage(int fd) |
391 | { | 421 | { |
392 | struct boot_params boot; | 422 | struct boot_params boot; |
@@ -394,8 +424,10 @@ static unsigned long load_bzimage(int fd) | |||
394 | /* Modern bzImages get loaded at 1M. */ | 424 | /* Modern bzImages get loaded at 1M. */ |
395 | void *p = from_guest_phys(0x100000); | 425 | void *p = from_guest_phys(0x100000); |
396 | 426 | ||
397 | /* Go back to the start of the file and read the header. It should be | 427 | /* |
398 | * a Linux boot header (see Documentation/x86/i386/boot.txt) */ | 428 | * Go back to the start of the file and read the header. It should be |
429 | * a Linux boot header (see Documentation/x86/i386/boot.txt) | ||
430 | */ | ||
399 | lseek(fd, 0, SEEK_SET); | 431 | lseek(fd, 0, SEEK_SET); |
400 | read(fd, &boot, sizeof(boot)); | 432 | read(fd, &boot, sizeof(boot)); |
401 | 433 | ||
@@ -414,9 +446,11 @@ static unsigned long load_bzimage(int fd) | |||
414 | return boot.hdr.code32_start; | 446 | return boot.hdr.code32_start; |
415 | } | 447 | } |
416 | 448 | ||
417 | /*L:140 Loading the kernel is easy when it's a "vmlinux", but most kernels | 449 | /*L:140 |
450 | * Loading the kernel is easy when it's a "vmlinux", but most kernels | ||
418 | * come wrapped up in the self-decompressing "bzImage" format. With a little | 451 | * come wrapped up in the self-decompressing "bzImage" format. With a little |
419 | * work, we can load those, too. */ | 452 | * work, we can load those, too. |
453 | */ | ||
420 | static unsigned long load_kernel(int fd) | 454 | static unsigned long load_kernel(int fd) |
421 | { | 455 | { |
422 | Elf32_Ehdr hdr; | 456 | Elf32_Ehdr hdr; |
@@ -433,24 +467,28 @@ static unsigned long load_kernel(int fd) | |||
433 | return load_bzimage(fd); | 467 | return load_bzimage(fd); |
434 | } | 468 | } |
435 | 469 | ||
436 | /* This is a trivial little helper to align pages. Andi Kleen hated it because | 470 | /* |
471 | * This is a trivial little helper to align pages. Andi Kleen hated it because | ||
437 | * it calls getpagesize() twice: "it's dumb code." | 472 | * it calls getpagesize() twice: "it's dumb code." |
438 | * | 473 | * |
439 | * Kernel guys get really het up about optimization, even when it's not | 474 | * Kernel guys get really het up about optimization, even when it's not |
440 | * necessary. I leave this code as a reaction against that. */ | 475 | * necessary. I leave this code as a reaction against that. |
476 | */ | ||
441 | static inline unsigned long page_align(unsigned long addr) | 477 | static inline unsigned long page_align(unsigned long addr) |
442 | { | 478 | { |
443 | /* Add upwards and truncate downwards. */ | 479 | /* Add upwards and truncate downwards. */ |
444 | return ((addr + getpagesize()-1) & ~(getpagesize()-1)); | 480 | return ((addr + getpagesize()-1) & ~(getpagesize()-1)); |
445 | } | 481 | } |
446 | 482 | ||
447 | /*L:180 An "initial ram disk" is a disk image loaded into memory along with | 483 | /*L:180 |
448 | * the kernel which the kernel can use to boot from without needing any | 484 | * An "initial ram disk" is a disk image loaded into memory along with the |
449 | * drivers. Most distributions now use this as standard: the initrd contains | 485 | * kernel which the kernel can use to boot from without needing any drivers. |
450 | * the code to load the appropriate driver modules for the current machine. | 486 | * Most distributions now use this as standard: the initrd contains the code to |
487 | * load the appropriate driver modules for the current machine. | ||
451 | * | 488 | * |
452 | * Importantly, James Morris works for RedHat, and Fedora uses initrds for its | 489 | * Importantly, James Morris works for RedHat, and Fedora uses initrds for its |
453 | * kernels. He sent me this (and tells me when I break it). */ | 490 | * kernels. He sent me this (and tells me when I break it). |
491 | */ | ||
454 | static unsigned long load_initrd(const char *name, unsigned long mem) | 492 | static unsigned long load_initrd(const char *name, unsigned long mem) |
455 | { | 493 | { |
456 | int ifd; | 494 | int ifd; |
@@ -462,12 +500,16 @@ static unsigned long load_initrd(const char *name, unsigned long mem) | |||
462 | if (fstat(ifd, &st) < 0) | 500 | if (fstat(ifd, &st) < 0) |
463 | err(1, "fstat() on initrd '%s'", name); | 501 | err(1, "fstat() on initrd '%s'", name); |
464 | 502 | ||
465 | /* We map the initrd at the top of memory, but mmap wants it to be | 503 | /* |
466 | * page-aligned, so we round the size up for that. */ | 504 | * We map the initrd at the top of memory, but mmap wants it to be |
505 | * page-aligned, so we round the size up for that. | ||
506 | */ | ||
467 | len = page_align(st.st_size); | 507 | len = page_align(st.st_size); |
468 | map_at(ifd, from_guest_phys(mem - len), 0, st.st_size); | 508 | map_at(ifd, from_guest_phys(mem - len), 0, st.st_size); |
469 | /* Once a file is mapped, you can close the file descriptor. It's a | 509 | /* |
470 | * little odd, but quite useful. */ | 510 | * Once a file is mapped, you can close the file descriptor. It's a |
511 | * little odd, but quite useful. | ||
512 | */ | ||
471 | close(ifd); | 513 | close(ifd); |
472 | verbose("mapped initrd %s size=%lu @ %p\n", name, len, (void*)mem-len); | 514 | verbose("mapped initrd %s size=%lu @ %p\n", name, len, (void*)mem-len); |
473 | 515 | ||
@@ -476,8 +518,10 @@ static unsigned long load_initrd(const char *name, unsigned long mem) | |||
476 | } | 518 | } |
477 | /*:*/ | 519 | /*:*/ |
478 | 520 | ||
479 | /* Simple routine to roll all the commandline arguments together with spaces | 521 | /* |
480 | * between them. */ | 522 | * Simple routine to roll all the commandline arguments together with spaces |
523 | * between them. | ||
524 | */ | ||
481 | static void concat(char *dst, char *args[]) | 525 | static void concat(char *dst, char *args[]) |
482 | { | 526 | { |
483 | unsigned int i, len = 0; | 527 | unsigned int i, len = 0; |
@@ -494,10 +538,12 @@ static void concat(char *dst, char *args[]) | |||
494 | dst[len] = '\0'; | 538 | dst[len] = '\0'; |
495 | } | 539 | } |
496 | 540 | ||
497 | /*L:185 This is where we actually tell the kernel to initialize the Guest. We | 541 | /*L:185 |
542 | * This is where we actually tell the kernel to initialize the Guest. We | ||
498 | * saw the arguments it expects when we looked at initialize() in lguest_user.c: | 543 | * saw the arguments it expects when we looked at initialize() in lguest_user.c: |
499 | * the base of Guest "physical" memory, the top physical page to allow and the | 544 | * the base of Guest "physical" memory, the top physical page to allow and the |
500 | * entry point for the Guest. */ | 545 | * entry point for the Guest. |
546 | */ | ||
501 | static void tell_kernel(unsigned long start) | 547 | static void tell_kernel(unsigned long start) |
502 | { | 548 | { |
503 | unsigned long args[] = { LHREQ_INITIALIZE, | 549 | unsigned long args[] = { LHREQ_INITIALIZE, |
@@ -522,20 +568,26 @@ static void tell_kernel(unsigned long start) | |||
522 | static void *_check_pointer(unsigned long addr, unsigned int size, | 568 | static void *_check_pointer(unsigned long addr, unsigned int size, |
523 | unsigned int line) | 569 | unsigned int line) |
524 | { | 570 | { |
525 | /* We have to separately check addr and addr+size, because size could | 571 | /* |
526 | * be huge and addr + size might wrap around. */ | 572 | * We have to separately check addr and addr+size, because size could |
573 | * be huge and addr + size might wrap around. | ||
574 | */ | ||
527 | if (addr >= guest_limit || addr + size >= guest_limit) | 575 | if (addr >= guest_limit || addr + size >= guest_limit) |
528 | errx(1, "%s:%i: Invalid address %#lx", __FILE__, line, addr); | 576 | errx(1, "%s:%i: Invalid address %#lx", __FILE__, line, addr); |
529 | /* We return a pointer for the caller's convenience, now we know it's | 577 | /* |
530 | * safe to use. */ | 578 | * We return a pointer for the caller's convenience, now we know it's |
579 | * safe to use. | ||
580 | */ | ||
531 | return from_guest_phys(addr); | 581 | return from_guest_phys(addr); |
532 | } | 582 | } |
533 | /* A macro which transparently hands the line number to the real function. */ | 583 | /* A macro which transparently hands the line number to the real function. */ |
534 | #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__) | 584 | #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__) |
535 | 585 | ||
536 | /* Each buffer in the virtqueues is actually a chain of descriptors. This | 586 | /* |
587 | * Each buffer in the virtqueues is actually a chain of descriptors. This | ||
537 | * function returns the next descriptor in the chain, or vq->vring.num if we're | 588 | * function returns the next descriptor in the chain, or vq->vring.num if we're |
538 | * at the end. */ | 589 | * at the end. |
590 | */ | ||
539 | static unsigned next_desc(struct vring_desc *desc, | 591 | static unsigned next_desc(struct vring_desc *desc, |
540 | unsigned int i, unsigned int max) | 592 | unsigned int i, unsigned int max) |
541 | { | 593 | { |
@@ -576,12 +628,14 @@ static void trigger_irq(struct virtqueue *vq) | |||
576 | err(1, "Triggering irq %i", vq->config.irq); | 628 | err(1, "Triggering irq %i", vq->config.irq); |
577 | } | 629 | } |
578 | 630 | ||
579 | /* This looks in the virtqueue and for the first available buffer, and converts | 631 | /* |
632 | * This looks in the virtqueue and for the first available buffer, and converts | ||
580 | * it to an iovec for convenient access. Since descriptors consist of some | 633 | * it to an iovec for convenient access. Since descriptors consist of some |
581 | * number of output then some number of input descriptors, it's actually two | 634 | * number of output then some number of input descriptors, it's actually two |
582 | * iovecs, but we pack them into one and note how many of each there were. | 635 | * iovecs, but we pack them into one and note how many of each there were. |
583 | * | 636 | * |
584 | * This function returns the descriptor number found. */ | 637 | * This function returns the descriptor number found. |
638 | */ | ||
585 | static unsigned wait_for_vq_desc(struct virtqueue *vq, | 639 | static unsigned wait_for_vq_desc(struct virtqueue *vq, |
586 | struct iovec iov[], | 640 | struct iovec iov[], |
587 | unsigned int *out_num, unsigned int *in_num) | 641 | unsigned int *out_num, unsigned int *in_num) |
@@ -599,8 +653,10 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
599 | /* OK, now we need to know about added descriptors. */ | 653 | /* OK, now we need to know about added descriptors. */ |
600 | vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY; | 654 | vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY; |
601 | 655 | ||
602 | /* They could have slipped one in as we were doing that: make | 656 | /* |
603 | * sure it's written, then check again. */ | 657 | * They could have slipped one in as we were doing that: make |
658 | * sure it's written, then check again. | ||
659 | */ | ||
604 | mb(); | 660 | mb(); |
605 | if (last_avail != vq->vring.avail->idx) { | 661 | if (last_avail != vq->vring.avail->idx) { |
606 | vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY; | 662 | vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY; |
@@ -620,8 +676,10 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
620 | errx(1, "Guest moved used index from %u to %u", | 676 | errx(1, "Guest moved used index from %u to %u", |
621 | last_avail, vq->vring.avail->idx); | 677 | last_avail, vq->vring.avail->idx); |
622 | 678 | ||
623 | /* Grab the next descriptor number they're advertising, and increment | 679 | /* |
624 | * the index we've seen. */ | 680 | * Grab the next descriptor number they're advertising, and increment |
681 | * the index we've seen. | ||
682 | */ | ||
625 | head = vq->vring.avail->ring[last_avail % vq->vring.num]; | 683 | head = vq->vring.avail->ring[last_avail % vq->vring.num]; |
626 | lg_last_avail(vq)++; | 684 | lg_last_avail(vq)++; |
627 | 685 | ||
@@ -636,8 +694,10 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
636 | desc = vq->vring.desc; | 694 | desc = vq->vring.desc; |
637 | i = head; | 695 | i = head; |
638 | 696 | ||
639 | /* If this is an indirect entry, then this buffer contains a descriptor | 697 | /* |
640 | * table which we handle as if it's any normal descriptor chain. */ | 698 | * If this is an indirect entry, then this buffer contains a descriptor |
699 | * table which we handle as if it's any normal descriptor chain. | ||
700 | */ | ||
641 | if (desc[i].flags & VRING_DESC_F_INDIRECT) { | 701 | if (desc[i].flags & VRING_DESC_F_INDIRECT) { |
642 | if (desc[i].len % sizeof(struct vring_desc)) | 702 | if (desc[i].len % sizeof(struct vring_desc)) |
643 | errx(1, "Invalid size for indirect buffer table"); | 703 | errx(1, "Invalid size for indirect buffer table"); |
@@ -656,8 +716,10 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
656 | if (desc[i].flags & VRING_DESC_F_WRITE) | 716 | if (desc[i].flags & VRING_DESC_F_WRITE) |
657 | (*in_num)++; | 717 | (*in_num)++; |
658 | else { | 718 | else { |
659 | /* If it's an output descriptor, they're all supposed | 719 | /* |
660 | * to come before any input descriptors. */ | 720 | * If it's an output descriptor, they're all supposed |
721 | * to come before any input descriptors. | ||
722 | */ | ||
661 | if (*in_num) | 723 | if (*in_num) |
662 | errx(1, "Descriptor has out after in"); | 724 | errx(1, "Descriptor has out after in"); |
663 | (*out_num)++; | 725 | (*out_num)++; |
@@ -671,14 +733,18 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
671 | return head; | 733 | return head; |
672 | } | 734 | } |
673 | 735 | ||
674 | /* After we've used one of their buffers, we tell them about it. We'll then | 736 | /* |
675 | * want to send them an interrupt, using trigger_irq(). */ | 737 | * After we've used one of their buffers, we tell them about it. We'll then |
738 | * want to send them an interrupt, using trigger_irq(). | ||
739 | */ | ||
676 | static void add_used(struct virtqueue *vq, unsigned int head, int len) | 740 | static void add_used(struct virtqueue *vq, unsigned int head, int len) |
677 | { | 741 | { |
678 | struct vring_used_elem *used; | 742 | struct vring_used_elem *used; |
679 | 743 | ||
680 | /* The virtqueue contains a ring of used buffers. Get a pointer to the | 744 | /* |
681 | * next entry in that used ring. */ | 745 | * The virtqueue contains a ring of used buffers. Get a pointer to the |
746 | * next entry in that used ring. | ||
747 | */ | ||
682 | used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num]; | 748 | used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num]; |
683 | used->id = head; | 749 | used->id = head; |
684 | used->len = len; | 750 | used->len = len; |
@@ -698,7 +764,8 @@ static void add_used_and_trigger(struct virtqueue *vq, unsigned head, int len) | |||
698 | /* | 764 | /* |
699 | * The Console | 765 | * The Console |
700 | * | 766 | * |
701 | * We associate some data with the console for our exit hack. */ | 767 | * We associate some data with the console for our exit hack. |
768 | */ | ||
702 | struct console_abort | 769 | struct console_abort |
703 | { | 770 | { |
704 | /* How many times have they hit ^C? */ | 771 | /* How many times have they hit ^C? */ |
@@ -725,20 +792,24 @@ static void console_input(struct virtqueue *vq) | |||
725 | if (len <= 0) { | 792 | if (len <= 0) { |
726 | /* Ran out of input? */ | 793 | /* Ran out of input? */ |
727 | warnx("Failed to get console input, ignoring console."); | 794 | warnx("Failed to get console input, ignoring console."); |
728 | /* For simplicity, dying threads kill the whole Launcher. So | 795 | /* |
729 | * just nap here. */ | 796 | * For simplicity, dying threads kill the whole Launcher. So |
797 | * just nap here. | ||
798 | */ | ||
730 | for (;;) | 799 | for (;;) |
731 | pause(); | 800 | pause(); |
732 | } | 801 | } |
733 | 802 | ||
734 | add_used_and_trigger(vq, head, len); | 803 | add_used_and_trigger(vq, head, len); |
735 | 804 | ||
736 | /* Three ^C within one second? Exit. | 805 | /* |
806 | * Three ^C within one second? Exit. | ||
737 | * | 807 | * |
738 | * This is such a hack, but works surprisingly well. Each ^C has to | 808 | * This is such a hack, but works surprisingly well. Each ^C has to |
739 | * be in a buffer by itself, so they can't be too fast. But we check | 809 | * be in a buffer by itself, so they can't be too fast. But we check |
740 | * that we get three within about a second, so they can't be too | 810 | * that we get three within about a second, so they can't be too |
741 | * slow. */ | 811 | * slow. |
812 | */ | ||
742 | if (len != 1 || ((char *)iov[0].iov_base)[0] != 3) { | 813 | if (len != 1 || ((char *)iov[0].iov_base)[0] != 3) { |
743 | abort->count = 0; | 814 | abort->count = 0; |
744 | return; | 815 | return; |
@@ -809,8 +880,7 @@ static bool will_block(int fd) | |||
809 | return select(fd+1, &fdset, NULL, NULL, &zero) != 1; | 880 | return select(fd+1, &fdset, NULL, NULL, &zero) != 1; |
810 | } | 881 | } |
811 | 882 | ||
812 | /* This is where we handle packets coming in from the tun device to our | 883 | /* This handles packets coming in from the tun device to our Guest. */ |
813 | * Guest. */ | ||
814 | static void net_input(struct virtqueue *vq) | 884 | static void net_input(struct virtqueue *vq) |
815 | { | 885 | { |
816 | int len; | 886 | int len; |
@@ -842,8 +912,10 @@ static int do_thread(void *_vq) | |||
842 | return 0; | 912 | return 0; |
843 | } | 913 | } |
844 | 914 | ||
845 | /* When a child dies, we kill our entire process group with SIGTERM. This | 915 | /* |
846 | * also has the side effect that the shell restores the console for us! */ | 916 | * When a child dies, we kill our entire process group with SIGTERM. This |
917 | * also has the side effect that the shell restores the console for us! | ||
918 | */ | ||
847 | static void kill_launcher(int signal) | 919 | static void kill_launcher(int signal) |
848 | { | 920 | { |
849 | kill(0, SIGTERM); | 921 | kill(0, SIGTERM); |
@@ -880,9 +952,10 @@ static void reset_device(struct device *dev) | |||
880 | 952 | ||
881 | static void create_thread(struct virtqueue *vq) | 953 | static void create_thread(struct virtqueue *vq) |
882 | { | 954 | { |
883 | /* Create stack for thread and run it. Since stack grows | 955 | /* |
884 | * upwards, we point the stack pointer to the end of this | 956 | * Create stack for thread and run it. Since the stack grows upwards, |
885 | * region. */ | 957 | * we point the stack pointer to the end of this region. |
958 | */ | ||
886 | char *stack = malloc(32768); | 959 | char *stack = malloc(32768); |
887 | unsigned long args[] = { LHREQ_EVENTFD, | 960 | unsigned long args[] = { LHREQ_EVENTFD, |
888 | vq->config.pfn*getpagesize(), 0 }; | 961 | vq->config.pfn*getpagesize(), 0 }; |
@@ -981,8 +1054,11 @@ static void handle_output(unsigned long addr) | |||
981 | } | 1054 | } |
982 | } | 1055 | } |
983 | 1056 | ||
984 | /* Early console write is done using notify on a nul-terminated string | 1057 | /* |
985 | * in Guest memory. */ | 1058 | * Early console write is done using notify on a nul-terminated string |
1059 | * in Guest memory. It's also great for hacking debugging messages | ||
1060 | * into a Guest. | ||
1061 | */ | ||
986 | if (addr >= guest_limit) | 1062 | if (addr >= guest_limit) |
987 | errx(1, "Bad NOTIFY %#lx", addr); | 1063 | errx(1, "Bad NOTIFY %#lx", addr); |
988 | 1064 | ||
@@ -998,10 +1074,12 @@ static void handle_output(unsigned long addr) | |||
998 | * routines to allocate and manage them. | 1074 | * routines to allocate and manage them. |
999 | */ | 1075 | */ |
1000 | 1076 | ||
1001 | /* The layout of the device page is a "struct lguest_device_desc" followed by a | 1077 | /* |
1078 | * The layout of the device page is a "struct lguest_device_desc" followed by a | ||
1002 | * number of virtqueue descriptors, then two sets of feature bits, then an | 1079 | * number of virtqueue descriptors, then two sets of feature bits, then an |
1003 | * array of configuration bytes. This routine returns the configuration | 1080 | * array of configuration bytes. This routine returns the configuration |
1004 | * pointer. */ | 1081 | * pointer. |
1082 | */ | ||
1005 | static u8 *device_config(const struct device *dev) | 1083 | static u8 *device_config(const struct device *dev) |
1006 | { | 1084 | { |
1007 | return (void *)(dev->desc + 1) | 1085 | return (void *)(dev->desc + 1) |
@@ -1009,9 +1087,11 @@ static u8 *device_config(const struct device *dev) | |||
1009 | + dev->feature_len * 2; | 1087 | + dev->feature_len * 2; |
1010 | } | 1088 | } |
1011 | 1089 | ||
1012 | /* This routine allocates a new "struct lguest_device_desc" from descriptor | 1090 | /* |
1091 | * This routine allocates a new "struct lguest_device_desc" from descriptor | ||
1013 | * table page just above the Guest's normal memory. It returns a pointer to | 1092 | * table page just above the Guest's normal memory. It returns a pointer to |
1014 | * that descriptor. */ | 1093 | * that descriptor. |
1094 | */ | ||
1015 | static struct lguest_device_desc *new_dev_desc(u16 type) | 1095 | static struct lguest_device_desc *new_dev_desc(u16 type) |
1016 | { | 1096 | { |
1017 | struct lguest_device_desc d = { .type = type }; | 1097 | struct lguest_device_desc d = { .type = type }; |
@@ -1032,8 +1112,10 @@ static struct lguest_device_desc *new_dev_desc(u16 type) | |||
1032 | return memcpy(p, &d, sizeof(d)); | 1112 | return memcpy(p, &d, sizeof(d)); |
1033 | } | 1113 | } |
1034 | 1114 | ||
1035 | /* Each device descriptor is followed by the description of its virtqueues. We | 1115 | /* |
1036 | * specify how many descriptors the virtqueue is to have. */ | 1116 | * Each device descriptor is followed by the description of its virtqueues. We |
1117 | * specify how many descriptors the virtqueue is to have. | ||
1118 | */ | ||
1037 | static void add_virtqueue(struct device *dev, unsigned int num_descs, | 1119 | static void add_virtqueue(struct device *dev, unsigned int num_descs, |
1038 | void (*service)(struct virtqueue *)) | 1120 | void (*service)(struct virtqueue *)) |
1039 | { | 1121 | { |
@@ -1061,10 +1143,12 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs, | |||
1061 | /* Initialize the vring. */ | 1143 | /* Initialize the vring. */ |
1062 | vring_init(&vq->vring, num_descs, p, LGUEST_VRING_ALIGN); | 1144 | vring_init(&vq->vring, num_descs, p, LGUEST_VRING_ALIGN); |
1063 | 1145 | ||
1064 | /* Append virtqueue to this device's descriptor. We use | 1146 | /* |
1147 | * Append virtqueue to this device's descriptor. We use | ||
1065 | * device_config() to get the end of the device's current virtqueues; | 1148 | * device_config() to get the end of the device's current virtqueues; |
1066 | * we check that we haven't added any config or feature information | 1149 | * we check that we haven't added any config or feature information |
1067 | * yet, otherwise we'd be overwriting them. */ | 1150 | * yet, otherwise we'd be overwriting them. |
1151 | */ | ||
1068 | assert(dev->desc->config_len == 0 && dev->desc->feature_len == 0); | 1152 | assert(dev->desc->config_len == 0 && dev->desc->feature_len == 0); |
1069 | memcpy(device_config(dev), &vq->config, sizeof(vq->config)); | 1153 | memcpy(device_config(dev), &vq->config, sizeof(vq->config)); |
1070 | dev->num_vq++; | 1154 | dev->num_vq++; |
@@ -1072,14 +1156,18 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs, | |||
1072 | 1156 | ||
1073 | verbose("Virtqueue page %#lx\n", to_guest_phys(p)); | 1157 | verbose("Virtqueue page %#lx\n", to_guest_phys(p)); |
1074 | 1158 | ||
1075 | /* Add to tail of list, so dev->vq is first vq, dev->vq->next is | 1159 | /* |
1076 | * second. */ | 1160 | * Add to tail of list, so dev->vq is first vq, dev->vq->next is |
1161 | * second. | ||
1162 | */ | ||
1077 | for (i = &dev->vq; *i; i = &(*i)->next); | 1163 | for (i = &dev->vq; *i; i = &(*i)->next); |
1078 | *i = vq; | 1164 | *i = vq; |
1079 | } | 1165 | } |
1080 | 1166 | ||
1081 | /* The first half of the feature bitmask is for us to advertise features. The | 1167 | /* |
1082 | * second half is for the Guest to accept features. */ | 1168 | * The first half of the feature bitmask is for us to advertise features. The |
1169 | * second half is for the Guest to accept features. | ||
1170 | */ | ||
1083 | static void add_feature(struct device *dev, unsigned bit) | 1171 | static void add_feature(struct device *dev, unsigned bit) |
1084 | { | 1172 | { |
1085 | u8 *features = get_feature_bits(dev); | 1173 | u8 *features = get_feature_bits(dev); |
@@ -1093,9 +1181,11 @@ static void add_feature(struct device *dev, unsigned bit) | |||
1093 | features[bit / CHAR_BIT] |= (1 << (bit % CHAR_BIT)); | 1181 | features[bit / CHAR_BIT] |= (1 << (bit % CHAR_BIT)); |
1094 | } | 1182 | } |
1095 | 1183 | ||
1096 | /* This routine sets the configuration fields for an existing device's | 1184 | /* |
1185 | * This routine sets the configuration fields for an existing device's | ||
1097 | * descriptor. It only works for the last device, but that's OK because that's | 1186 | * descriptor. It only works for the last device, but that's OK because that's |
1098 | * how we use it. */ | 1187 | * how we use it. |
1188 | */ | ||
1099 | static void set_config(struct device *dev, unsigned len, const void *conf) | 1189 | static void set_config(struct device *dev, unsigned len, const void *conf) |
1100 | { | 1190 | { |
1101 | /* Check we haven't overflowed our single page. */ | 1191 | /* Check we haven't overflowed our single page. */ |
@@ -1110,10 +1200,12 @@ static void set_config(struct device *dev, unsigned len, const void *conf) | |||
1110 | assert(dev->desc->config_len == len); | 1200 | assert(dev->desc->config_len == len); |
1111 | } | 1201 | } |
1112 | 1202 | ||
1113 | /* This routine does all the creation and setup of a new device, including | 1203 | /* |
1204 | * This routine does all the creation and setup of a new device, including | ||
1114 | * calling new_dev_desc() to allocate the descriptor and device memory. | 1205 | * calling new_dev_desc() to allocate the descriptor and device memory. |
1115 | * | 1206 | * |
1116 | * See what I mean about userspace being boring? */ | 1207 | * See what I mean about userspace being boring? |
1208 | */ | ||
1117 | static struct device *new_device(const char *name, u16 type) | 1209 | static struct device *new_device(const char *name, u16 type) |
1118 | { | 1210 | { |
1119 | struct device *dev = malloc(sizeof(*dev)); | 1211 | struct device *dev = malloc(sizeof(*dev)); |
@@ -1126,10 +1218,12 @@ static struct device *new_device(const char *name, u16 type) | |||
1126 | dev->num_vq = 0; | 1218 | dev->num_vq = 0; |
1127 | dev->running = false; | 1219 | dev->running = false; |
1128 | 1220 | ||
1129 | /* Append to device list. Prepending to a single-linked list is | 1221 | /* |
1222 | * Append to device list. Prepending to a single-linked list is | ||
1130 | * easier, but the user expects the devices to be arranged on the bus | 1223 | * easier, but the user expects the devices to be arranged on the bus |
1131 | * in command-line order. The first network device on the command line | 1224 | * in command-line order. The first network device on the command line |
1132 | * is eth0, the first block device /dev/vda, etc. */ | 1225 | * is eth0, the first block device /dev/vda, etc. |
1226 | */ | ||
1133 | if (devices.lastdev) | 1227 | if (devices.lastdev) |
1134 | devices.lastdev->next = dev; | 1228 | devices.lastdev->next = dev; |
1135 | else | 1229 | else |
@@ -1139,8 +1233,10 @@ static struct device *new_device(const char *name, u16 type) | |||
1139 | return dev; | 1233 | return dev; |
1140 | } | 1234 | } |
1141 | 1235 | ||
1142 | /* Our first setup routine is the console. It's a fairly simple device, but | 1236 | /* |
1143 | * UNIX tty handling makes it uglier than it could be. */ | 1237 | * Our first setup routine is the console. It's a fairly simple device, but |
1238 | * UNIX tty handling makes it uglier than it could be. | ||
1239 | */ | ||
1144 | static void setup_console(void) | 1240 | static void setup_console(void) |
1145 | { | 1241 | { |
1146 | struct device *dev; | 1242 | struct device *dev; |
@@ -1148,8 +1244,10 @@ static void setup_console(void) | |||
1148 | /* If we can save the initial standard input settings... */ | 1244 | /* If we can save the initial standard input settings... */ |
1149 | if (tcgetattr(STDIN_FILENO, &orig_term) == 0) { | 1245 | if (tcgetattr(STDIN_FILENO, &orig_term) == 0) { |
1150 | struct termios term = orig_term; | 1246 | struct termios term = orig_term; |
1151 | /* Then we turn off echo, line buffering and ^C etc. We want a | 1247 | /* |
1152 | * raw input stream to the Guest. */ | 1248 | * Then we turn off echo, line buffering and ^C etc: We want a |
1249 | * raw input stream to the Guest. | ||
1250 | */ | ||
1153 | term.c_lflag &= ~(ISIG|ICANON|ECHO); | 1251 | term.c_lflag &= ~(ISIG|ICANON|ECHO); |
1154 | tcsetattr(STDIN_FILENO, TCSANOW, &term); | 1252 | tcsetattr(STDIN_FILENO, TCSANOW, &term); |
1155 | } | 1253 | } |
@@ -1160,10 +1258,12 @@ static void setup_console(void) | |||
1160 | dev->priv = malloc(sizeof(struct console_abort)); | 1258 | dev->priv = malloc(sizeof(struct console_abort)); |
1161 | ((struct console_abort *)dev->priv)->count = 0; | 1259 | ((struct console_abort *)dev->priv)->count = 0; |
1162 | 1260 | ||
1163 | /* The console needs two virtqueues: the input then the output. When | 1261 | /* |
1262 | * The console needs two virtqueues: the input then the output. When | ||
1164 | * they put something the input queue, we make sure we're listening to | 1263 | * they put something the input queue, we make sure we're listening to |
1165 | * stdin. When they put something in the output queue, we write it to | 1264 | * stdin. When they put something in the output queue, we write it to |
1166 | * stdout. */ | 1265 | * stdout. |
1266 | */ | ||
1167 | add_virtqueue(dev, VIRTQUEUE_NUM, console_input); | 1267 | add_virtqueue(dev, VIRTQUEUE_NUM, console_input); |
1168 | add_virtqueue(dev, VIRTQUEUE_NUM, console_output); | 1268 | add_virtqueue(dev, VIRTQUEUE_NUM, console_output); |
1169 | 1269 | ||
@@ -1171,7 +1271,8 @@ static void setup_console(void) | |||
1171 | } | 1271 | } |
1172 | /*:*/ | 1272 | /*:*/ |
1173 | 1273 | ||
1174 | /*M:010 Inter-guest networking is an interesting area. Simplest is to have a | 1274 | /*M:010 |
1275 | * Inter-guest networking is an interesting area. Simplest is to have a | ||
1175 | * --sharenet=<name> option which opens or creates a named pipe. This can be | 1276 | * --sharenet=<name> option which opens or creates a named pipe. This can be |
1176 | * used to send packets to another guest in a 1:1 manner. | 1277 | * used to send packets to another guest in a 1:1 manner. |
1177 | * | 1278 | * |
@@ -1185,7 +1286,8 @@ static void setup_console(void) | |||
1185 | * multiple inter-guest channels behind one interface, although it would | 1286 | * multiple inter-guest channels behind one interface, although it would |
1186 | * require some manner of hotplugging new virtio channels. | 1287 | * require some manner of hotplugging new virtio channels. |
1187 | * | 1288 | * |
1188 | * Finally, we could implement a virtio network switch in the kernel. :*/ | 1289 | * Finally, we could implement a virtio network switch in the kernel. |
1290 | :*/ | ||
1189 | 1291 | ||
1190 | static u32 str2ip(const char *ipaddr) | 1292 | static u32 str2ip(const char *ipaddr) |
1191 | { | 1293 | { |
@@ -1210,11 +1312,13 @@ static void str2mac(const char *macaddr, unsigned char mac[6]) | |||
1210 | mac[5] = m[5]; | 1312 | mac[5] = m[5]; |
1211 | } | 1313 | } |
1212 | 1314 | ||
1213 | /* This code is "adapted" from libbridge: it attaches the Host end of the | 1315 | /* |
1316 | * This code is "adapted" from libbridge: it attaches the Host end of the | ||
1214 | * network device to the bridge device specified by the command line. | 1317 | * network device to the bridge device specified by the command line. |
1215 | * | 1318 | * |
1216 | * This is yet another James Morris contribution (I'm an IP-level guy, so I | 1319 | * This is yet another James Morris contribution (I'm an IP-level guy, so I |
1217 | * dislike bridging), and I just try not to break it. */ | 1320 | * dislike bridging), and I just try not to break it. |
1321 | */ | ||
1218 | static void add_to_bridge(int fd, const char *if_name, const char *br_name) | 1322 | static void add_to_bridge(int fd, const char *if_name, const char *br_name) |
1219 | { | 1323 | { |
1220 | int ifidx; | 1324 | int ifidx; |
@@ -1234,9 +1338,11 @@ static void add_to_bridge(int fd, const char *if_name, const char *br_name) | |||
1234 | err(1, "can't add %s to bridge %s", if_name, br_name); | 1338 | err(1, "can't add %s to bridge %s", if_name, br_name); |
1235 | } | 1339 | } |
1236 | 1340 | ||
1237 | /* This sets up the Host end of the network device with an IP address, brings | 1341 | /* |
1342 | * This sets up the Host end of the network device with an IP address, brings | ||
1238 | * it up so packets will flow, the copies the MAC address into the hwaddr | 1343 | * it up so packets will flow, the copies the MAC address into the hwaddr |
1239 | * pointer. */ | 1344 | * pointer. |
1345 | */ | ||
1240 | static void configure_device(int fd, const char *tapif, u32 ipaddr) | 1346 | static void configure_device(int fd, const char *tapif, u32 ipaddr) |
1241 | { | 1347 | { |
1242 | struct ifreq ifr; | 1348 | struct ifreq ifr; |
@@ -1263,10 +1369,12 @@ static int get_tun_device(char tapif[IFNAMSIZ]) | |||
1263 | /* Start with this zeroed. Messy but sure. */ | 1369 | /* Start with this zeroed. Messy but sure. */ |
1264 | memset(&ifr, 0, sizeof(ifr)); | 1370 | memset(&ifr, 0, sizeof(ifr)); |
1265 | 1371 | ||
1266 | /* We open the /dev/net/tun device and tell it we want a tap device. A | 1372 | /* |
1373 | * We open the /dev/net/tun device and tell it we want a tap device. A | ||
1267 | * tap device is like a tun device, only somehow different. To tell | 1374 | * tap device is like a tun device, only somehow different. To tell |
1268 | * the truth, I completely blundered my way through this code, but it | 1375 | * the truth, I completely blundered my way through this code, but it |
1269 | * works now! */ | 1376 | * works now! |
1377 | */ | ||
1270 | netfd = open_or_die("/dev/net/tun", O_RDWR); | 1378 | netfd = open_or_die("/dev/net/tun", O_RDWR); |
1271 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; | 1379 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; |
1272 | strcpy(ifr.ifr_name, "tap%d"); | 1380 | strcpy(ifr.ifr_name, "tap%d"); |
@@ -1277,18 +1385,22 @@ static int get_tun_device(char tapif[IFNAMSIZ]) | |||
1277 | TUN_F_CSUM|TUN_F_TSO4|TUN_F_TSO6|TUN_F_TSO_ECN) != 0) | 1385 | TUN_F_CSUM|TUN_F_TSO4|TUN_F_TSO6|TUN_F_TSO_ECN) != 0) |
1278 | err(1, "Could not set features for tun device"); | 1386 | err(1, "Could not set features for tun device"); |
1279 | 1387 | ||
1280 | /* We don't need checksums calculated for packets coming in this | 1388 | /* |
1281 | * device: trust us! */ | 1389 | * We don't need checksums calculated for packets coming in this |
1390 | * device: trust us! | ||
1391 | */ | ||
1282 | ioctl(netfd, TUNSETNOCSUM, 1); | 1392 | ioctl(netfd, TUNSETNOCSUM, 1); |
1283 | 1393 | ||
1284 | memcpy(tapif, ifr.ifr_name, IFNAMSIZ); | 1394 | memcpy(tapif, ifr.ifr_name, IFNAMSIZ); |
1285 | return netfd; | 1395 | return netfd; |
1286 | } | 1396 | } |
1287 | 1397 | ||
1288 | /*L:195 Our network is a Host<->Guest network. This can either use bridging or | 1398 | /*L:195 |
1399 | * Our network is a Host<->Guest network. This can either use bridging or | ||
1289 | * routing, but the principle is the same: it uses the "tun" device to inject | 1400 | * routing, but the principle is the same: it uses the "tun" device to inject |
1290 | * packets into the Host as if they came in from a normal network card. We | 1401 | * packets into the Host as if they came in from a normal network card. We |
1291 | * just shunt packets between the Guest and the tun device. */ | 1402 | * just shunt packets between the Guest and the tun device. |
1403 | */ | ||
1292 | static void setup_tun_net(char *arg) | 1404 | static void setup_tun_net(char *arg) |
1293 | { | 1405 | { |
1294 | struct device *dev; | 1406 | struct device *dev; |
@@ -1305,13 +1417,14 @@ static void setup_tun_net(char *arg) | |||
1305 | dev = new_device("net", VIRTIO_ID_NET); | 1417 | dev = new_device("net", VIRTIO_ID_NET); |
1306 | dev->priv = net_info; | 1418 | dev->priv = net_info; |
1307 | 1419 | ||
1308 | /* Network devices need a receive and a send queue, just like | 1420 | /* Network devices need a recv and a send queue, just like console. */ |
1309 | * console. */ | ||
1310 | add_virtqueue(dev, VIRTQUEUE_NUM, net_input); | 1421 | add_virtqueue(dev, VIRTQUEUE_NUM, net_input); |
1311 | add_virtqueue(dev, VIRTQUEUE_NUM, net_output); | 1422 | add_virtqueue(dev, VIRTQUEUE_NUM, net_output); |
1312 | 1423 | ||
1313 | /* We need a socket to perform the magic network ioctls to bring up the | 1424 | /* |
1314 | * tap interface, connect to the bridge etc. Any socket will do! */ | 1425 | * We need a socket to perform the magic network ioctls to bring up the |
1426 | * tap interface, connect to the bridge etc. Any socket will do! | ||
1427 | */ | ||
1315 | ipfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); | 1428 | ipfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
1316 | if (ipfd < 0) | 1429 | if (ipfd < 0) |
1317 | err(1, "opening IP socket"); | 1430 | err(1, "opening IP socket"); |
@@ -1366,7 +1479,8 @@ static void setup_tun_net(char *arg) | |||
1366 | devices.device_num, tapif, arg); | 1479 | devices.device_num, tapif, arg); |
1367 | } | 1480 | } |
1368 | 1481 | ||
1369 | /* Our block (disk) device should be really simple: the Guest asks for a block | 1482 | /* |
1483 | * Our block (disk) device should be really simple: the Guest asks for a block | ||
1370 | * number and we read or write that position in the file. Unfortunately, that | 1484 | * number and we read or write that position in the file. Unfortunately, that |
1371 | * was amazingly slow: the Guest waits until the read is finished before | 1485 | * was amazingly slow: the Guest waits until the read is finished before |
1372 | * running anything else, even if it could have been doing useful work. | 1486 | * running anything else, even if it could have been doing useful work. |
@@ -1374,7 +1488,9 @@ static void setup_tun_net(char *arg) | |||
1374 | * We could use async I/O, except it's reputed to suck so hard that characters | 1488 | * We could use async I/O, except it's reputed to suck so hard that characters |
1375 | * actually go missing from your code when you try to use it. | 1489 | * actually go missing from your code when you try to use it. |
1376 | * | 1490 | * |
1377 | * So we farm the I/O out to thread, and communicate with it via a pipe. */ | 1491 | * So this was one reason why lguest now does all virtqueue servicing in |
1492 | * separate threads: it's more efficient and more like a real device. | ||
1493 | */ | ||
1378 | 1494 | ||
1379 | /* This hangs off device->priv. */ | 1495 | /* This hangs off device->priv. */ |
1380 | struct vblk_info | 1496 | struct vblk_info |
@@ -1412,9 +1528,11 @@ static void blk_request(struct virtqueue *vq) | |||
1412 | /* Get the next request. */ | 1528 | /* Get the next request. */ |
1413 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); | 1529 | head = wait_for_vq_desc(vq, iov, &out_num, &in_num); |
1414 | 1530 | ||
1415 | /* Every block request should contain at least one output buffer | 1531 | /* |
1532 | * Every block request should contain at least one output buffer | ||
1416 | * (detailing the location on disk and the type of request) and one | 1533 | * (detailing the location on disk and the type of request) and one |
1417 | * input buffer (to hold the result). */ | 1534 | * input buffer (to hold the result). |
1535 | */ | ||
1418 | if (out_num == 0 || in_num == 0) | 1536 | if (out_num == 0 || in_num == 0) |
1419 | errx(1, "Bad virtblk cmd %u out=%u in=%u", | 1537 | errx(1, "Bad virtblk cmd %u out=%u in=%u", |
1420 | head, out_num, in_num); | 1538 | head, out_num, in_num); |
@@ -1423,33 +1541,41 @@ static void blk_request(struct virtqueue *vq) | |||
1423 | in = convert(&iov[out_num+in_num-1], u8); | 1541 | in = convert(&iov[out_num+in_num-1], u8); |
1424 | off = out->sector * 512; | 1542 | off = out->sector * 512; |
1425 | 1543 | ||
1426 | /* The block device implements "barriers", where the Guest indicates | 1544 | /* |
1545 | * The block device implements "barriers", where the Guest indicates | ||
1427 | * that it wants all previous writes to occur before this write. We | 1546 | * that it wants all previous writes to occur before this write. We |
1428 | * don't have a way of asking our kernel to do a barrier, so we just | 1547 | * don't have a way of asking our kernel to do a barrier, so we just |
1429 | * synchronize all the data in the file. Pretty poor, no? */ | 1548 | * synchronize all the data in the file. Pretty poor, no? |
1549 | */ | ||
1430 | if (out->type & VIRTIO_BLK_T_BARRIER) | 1550 | if (out->type & VIRTIO_BLK_T_BARRIER) |
1431 | fdatasync(vblk->fd); | 1551 | fdatasync(vblk->fd); |
1432 | 1552 | ||
1433 | /* In general the virtio block driver is allowed to try SCSI commands. | 1553 | /* |
1434 | * It'd be nice if we supported eject, for example, but we don't. */ | 1554 | * In general the virtio block driver is allowed to try SCSI commands. |
1555 | * It'd be nice if we supported eject, for example, but we don't. | ||
1556 | */ | ||
1435 | if (out->type & VIRTIO_BLK_T_SCSI_CMD) { | 1557 | if (out->type & VIRTIO_BLK_T_SCSI_CMD) { |
1436 | fprintf(stderr, "Scsi commands unsupported\n"); | 1558 | fprintf(stderr, "Scsi commands unsupported\n"); |
1437 | *in = VIRTIO_BLK_S_UNSUPP; | 1559 | *in = VIRTIO_BLK_S_UNSUPP; |
1438 | wlen = sizeof(*in); | 1560 | wlen = sizeof(*in); |
1439 | } else if (out->type & VIRTIO_BLK_T_OUT) { | 1561 | } else if (out->type & VIRTIO_BLK_T_OUT) { |
1440 | /* Write */ | 1562 | /* |
1441 | 1563 | * Write | |
1442 | /* Move to the right location in the block file. This can fail | 1564 | * |
1443 | * if they try to write past end. */ | 1565 | * Move to the right location in the block file. This can fail |
1566 | * if they try to write past end. | ||
1567 | */ | ||
1444 | if (lseek64(vblk->fd, off, SEEK_SET) != off) | 1568 | if (lseek64(vblk->fd, off, SEEK_SET) != off) |
1445 | err(1, "Bad seek to sector %llu", out->sector); | 1569 | err(1, "Bad seek to sector %llu", out->sector); |
1446 | 1570 | ||
1447 | ret = writev(vblk->fd, iov+1, out_num-1); | 1571 | ret = writev(vblk->fd, iov+1, out_num-1); |
1448 | verbose("WRITE to sector %llu: %i\n", out->sector, ret); | 1572 | verbose("WRITE to sector %llu: %i\n", out->sector, ret); |
1449 | 1573 | ||
1450 | /* Grr... Now we know how long the descriptor they sent was, we | 1574 | /* |
1575 | * Grr... Now we know how long the descriptor they sent was, we | ||
1451 | * make sure they didn't try to write over the end of the block | 1576 | * make sure they didn't try to write over the end of the block |
1452 | * file (possibly extending it). */ | 1577 | * file (possibly extending it). |
1578 | */ | ||
1453 | if (ret > 0 && off + ret > vblk->len) { | 1579 | if (ret > 0 && off + ret > vblk->len) { |
1454 | /* Trim it back to the correct length */ | 1580 | /* Trim it back to the correct length */ |
1455 | ftruncate64(vblk->fd, vblk->len); | 1581 | ftruncate64(vblk->fd, vblk->len); |
@@ -1459,10 +1585,12 @@ static void blk_request(struct virtqueue *vq) | |||
1459 | wlen = sizeof(*in); | 1585 | wlen = sizeof(*in); |
1460 | *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); | 1586 | *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); |
1461 | } else { | 1587 | } else { |
1462 | /* Read */ | 1588 | /* |
1463 | 1589 | * Read | |
1464 | /* Move to the right location in the block file. This can fail | 1590 | * |
1465 | * if they try to read past end. */ | 1591 | * Move to the right location in the block file. This can fail |
1592 | * if they try to read past end. | ||
1593 | */ | ||
1466 | if (lseek64(vblk->fd, off, SEEK_SET) != off) | 1594 | if (lseek64(vblk->fd, off, SEEK_SET) != off) |
1467 | err(1, "Bad seek to sector %llu", out->sector); | 1595 | err(1, "Bad seek to sector %llu", out->sector); |
1468 | 1596 | ||
@@ -1477,10 +1605,12 @@ static void blk_request(struct virtqueue *vq) | |||
1477 | } | 1605 | } |
1478 | } | 1606 | } |
1479 | 1607 | ||
1480 | /* OK, so we noted that it was pretty poor to use an fdatasync as a | 1608 | /* |
1609 | * OK, so we noted that it was pretty poor to use an fdatasync as a | ||
1481 | * barrier. But Christoph Hellwig points out that we need a sync | 1610 | * barrier. But Christoph Hellwig points out that we need a sync |
1482 | * *afterwards* as well: "Barriers specify no reordering to the front | 1611 | * *afterwards* as well: "Barriers specify no reordering to the front |
1483 | * or the back." And Jens Axboe confirmed it, so here we are: */ | 1612 | * or the back." And Jens Axboe confirmed it, so here we are: |
1613 | */ | ||
1484 | if (out->type & VIRTIO_BLK_T_BARRIER) | 1614 | if (out->type & VIRTIO_BLK_T_BARRIER) |
1485 | fdatasync(vblk->fd); | 1615 | fdatasync(vblk->fd); |
1486 | 1616 | ||
@@ -1494,7 +1624,7 @@ static void setup_block_file(const char *filename) | |||
1494 | struct vblk_info *vblk; | 1624 | struct vblk_info *vblk; |
1495 | struct virtio_blk_config conf; | 1625 | struct virtio_blk_config conf; |
1496 | 1626 | ||
1497 | /* The device responds to return from I/O thread. */ | 1627 | /* Creat the device. */ |
1498 | dev = new_device("block", VIRTIO_ID_BLOCK); | 1628 | dev = new_device("block", VIRTIO_ID_BLOCK); |
1499 | 1629 | ||
1500 | /* The device has one virtqueue, where the Guest places requests. */ | 1630 | /* The device has one virtqueue, where the Guest places requests. */ |
@@ -1513,8 +1643,10 @@ static void setup_block_file(const char *filename) | |||
1513 | /* Tell Guest how many sectors this device has. */ | 1643 | /* Tell Guest how many sectors this device has. */ |
1514 | conf.capacity = cpu_to_le64(vblk->len / 512); | 1644 | conf.capacity = cpu_to_le64(vblk->len / 512); |
1515 | 1645 | ||
1516 | /* Tell Guest not to put in too many descriptors at once: two are used | 1646 | /* |
1517 | * for the in and out elements. */ | 1647 | * Tell Guest not to put in too many descriptors at once: two are used |
1648 | * for the in and out elements. | ||
1649 | */ | ||
1518 | add_feature(dev, VIRTIO_BLK_F_SEG_MAX); | 1650 | add_feature(dev, VIRTIO_BLK_F_SEG_MAX); |
1519 | conf.seg_max = cpu_to_le32(VIRTQUEUE_NUM - 2); | 1651 | conf.seg_max = cpu_to_le32(VIRTQUEUE_NUM - 2); |
1520 | 1652 | ||
@@ -1525,16 +1657,18 @@ static void setup_block_file(const char *filename) | |||
1525 | ++devices.device_num, le64_to_cpu(conf.capacity)); | 1657 | ++devices.device_num, le64_to_cpu(conf.capacity)); |
1526 | } | 1658 | } |
1527 | 1659 | ||
1528 | struct rng_info { | 1660 | /*L:211 |
1529 | int rfd; | 1661 | * Our random number generator device reads from /dev/random into the Guest's |
1530 | }; | ||
1531 | |||
1532 | /* Our random number generator device reads from /dev/random into the Guest's | ||
1533 | * input buffers. The usual case is that the Guest doesn't want random numbers | 1662 | * input buffers. The usual case is that the Guest doesn't want random numbers |
1534 | * and so has no buffers although /dev/random is still readable, whereas | 1663 | * and so has no buffers although /dev/random is still readable, whereas |
1535 | * console is the reverse. | 1664 | * console is the reverse. |
1536 | * | 1665 | * |
1537 | * The same logic applies, however. */ | 1666 | * The same logic applies, however. |
1667 | */ | ||
1668 | struct rng_info { | ||
1669 | int rfd; | ||
1670 | }; | ||
1671 | |||
1538 | static void rng_input(struct virtqueue *vq) | 1672 | static void rng_input(struct virtqueue *vq) |
1539 | { | 1673 | { |
1540 | int len; | 1674 | int len; |
@@ -1547,9 +1681,11 @@ static void rng_input(struct virtqueue *vq) | |||
1547 | if (out_num) | 1681 | if (out_num) |
1548 | errx(1, "Output buffers in rng?"); | 1682 | errx(1, "Output buffers in rng?"); |
1549 | 1683 | ||
1550 | /* This is why we convert to iovecs: the readv() call uses them, and so | 1684 | /* |
1685 | * This is why we convert to iovecs: the readv() call uses them, and so | ||
1551 | * it reads straight into the Guest's buffer. We loop to make sure we | 1686 | * it reads straight into the Guest's buffer. We loop to make sure we |
1552 | * fill it. */ | 1687 | * fill it. |
1688 | */ | ||
1553 | while (!iov_empty(iov, in_num)) { | 1689 | while (!iov_empty(iov, in_num)) { |
1554 | len = readv(rng_info->rfd, iov, in_num); | 1690 | len = readv(rng_info->rfd, iov, in_num); |
1555 | if (len <= 0) | 1691 | if (len <= 0) |
@@ -1562,15 +1698,18 @@ static void rng_input(struct virtqueue *vq) | |||
1562 | add_used(vq, head, totlen); | 1698 | add_used(vq, head, totlen); |
1563 | } | 1699 | } |
1564 | 1700 | ||
1565 | /* And this creates a "hardware" random number device for the Guest. */ | 1701 | /*L:199 |
1702 | * This creates a "hardware" random number device for the Guest. | ||
1703 | */ | ||
1566 | static void setup_rng(void) | 1704 | static void setup_rng(void) |
1567 | { | 1705 | { |
1568 | struct device *dev; | 1706 | struct device *dev; |
1569 | struct rng_info *rng_info = malloc(sizeof(*rng_info)); | 1707 | struct rng_info *rng_info = malloc(sizeof(*rng_info)); |
1570 | 1708 | ||
1709 | /* Our device's privat info simply contains the /dev/random fd. */ | ||
1571 | rng_info->rfd = open_or_die("/dev/random", O_RDONLY); | 1710 | rng_info->rfd = open_or_die("/dev/random", O_RDONLY); |
1572 | 1711 | ||
1573 | /* The device responds to return from I/O thread. */ | 1712 | /* Create the new device. */ |
1574 | dev = new_device("rng", VIRTIO_ID_RNG); | 1713 | dev = new_device("rng", VIRTIO_ID_RNG); |
1575 | dev->priv = rng_info; | 1714 | dev->priv = rng_info; |
1576 | 1715 | ||
@@ -1586,8 +1725,10 @@ static void __attribute__((noreturn)) restart_guest(void) | |||
1586 | { | 1725 | { |
1587 | unsigned int i; | 1726 | unsigned int i; |
1588 | 1727 | ||
1589 | /* Since we don't track all open fds, we simply close everything beyond | 1728 | /* |
1590 | * stderr. */ | 1729 | * Since we don't track all open fds, we simply close everything beyond |
1730 | * stderr. | ||
1731 | */ | ||
1591 | for (i = 3; i < FD_SETSIZE; i++) | 1732 | for (i = 3; i < FD_SETSIZE; i++) |
1592 | close(i); | 1733 | close(i); |
1593 | 1734 | ||
@@ -1598,8 +1739,10 @@ static void __attribute__((noreturn)) restart_guest(void) | |||
1598 | err(1, "Could not exec %s", main_args[0]); | 1739 | err(1, "Could not exec %s", main_args[0]); |
1599 | } | 1740 | } |
1600 | 1741 | ||
1601 | /*L:220 Finally we reach the core of the Launcher which runs the Guest, serves | 1742 | /*L:220 |
1602 | * its input and output, and finally, lays it to rest. */ | 1743 | * Finally we reach the core of the Launcher which runs the Guest, serves |
1744 | * its input and output, and finally, lays it to rest. | ||
1745 | */ | ||
1603 | static void __attribute__((noreturn)) run_guest(void) | 1746 | static void __attribute__((noreturn)) run_guest(void) |
1604 | { | 1747 | { |
1605 | for (;;) { | 1748 | for (;;) { |
@@ -1634,7 +1777,7 @@ static void __attribute__((noreturn)) run_guest(void) | |||
1634 | * | 1777 | * |
1635 | * Are you ready? Take a deep breath and join me in the core of the Host, in | 1778 | * Are you ready? Take a deep breath and join me in the core of the Host, in |
1636 | * "make Host". | 1779 | * "make Host". |
1637 | :*/ | 1780 | :*/ |
1638 | 1781 | ||
1639 | static struct option opts[] = { | 1782 | static struct option opts[] = { |
1640 | { "verbose", 0, NULL, 'v' }, | 1783 | { "verbose", 0, NULL, 'v' }, |
@@ -1655,8 +1798,7 @@ static void usage(void) | |||
1655 | /*L:105 The main routine is where the real work begins: */ | 1798 | /*L:105 The main routine is where the real work begins: */ |
1656 | int main(int argc, char *argv[]) | 1799 | int main(int argc, char *argv[]) |
1657 | { | 1800 | { |
1658 | /* Memory, top-level pagetable, code startpoint and size of the | 1801 | /* Memory, code startpoint and size of the (optional) initrd. */ |
1659 | * (optional) initrd. */ | ||
1660 | unsigned long mem = 0, start, initrd_size = 0; | 1802 | unsigned long mem = 0, start, initrd_size = 0; |
1661 | /* Two temporaries. */ | 1803 | /* Two temporaries. */ |
1662 | int i, c; | 1804 | int i, c; |
@@ -1668,24 +1810,30 @@ int main(int argc, char *argv[]) | |||
1668 | /* Save the args: we "reboot" by execing ourselves again. */ | 1810 | /* Save the args: we "reboot" by execing ourselves again. */ |
1669 | main_args = argv; | 1811 | main_args = argv; |
1670 | 1812 | ||
1671 | /* First we initialize the device list. We keep a pointer to the last | 1813 | /* |
1814 | * First we initialize the device list. We keep a pointer to the last | ||
1672 | * device, and the next interrupt number to use for devices (1: | 1815 | * device, and the next interrupt number to use for devices (1: |
1673 | * remember that 0 is used by the timer). */ | 1816 | * remember that 0 is used by the timer). |
1817 | */ | ||
1674 | devices.lastdev = NULL; | 1818 | devices.lastdev = NULL; |
1675 | devices.next_irq = 1; | 1819 | devices.next_irq = 1; |
1676 | 1820 | ||
1677 | cpu_id = 0; | 1821 | cpu_id = 0; |
1678 | /* We need to know how much memory so we can set up the device | 1822 | /* |
1823 | * We need to know how much memory so we can set up the device | ||
1679 | * descriptor and memory pages for the devices as we parse the command | 1824 | * descriptor and memory pages for the devices as we parse the command |
1680 | * line. So we quickly look through the arguments to find the amount | 1825 | * line. So we quickly look through the arguments to find the amount |
1681 | * of memory now. */ | 1826 | * of memory now. |
1827 | */ | ||
1682 | for (i = 1; i < argc; i++) { | 1828 | for (i = 1; i < argc; i++) { |
1683 | if (argv[i][0] != '-') { | 1829 | if (argv[i][0] != '-') { |
1684 | mem = atoi(argv[i]) * 1024 * 1024; | 1830 | mem = atoi(argv[i]) * 1024 * 1024; |
1685 | /* We start by mapping anonymous pages over all of | 1831 | /* |
1832 | * We start by mapping anonymous pages over all of | ||
1686 | * guest-physical memory range. This fills it with 0, | 1833 | * guest-physical memory range. This fills it with 0, |
1687 | * and ensures that the Guest won't be killed when it | 1834 | * and ensures that the Guest won't be killed when it |
1688 | * tries to access it. */ | 1835 | * tries to access it. |
1836 | */ | ||
1689 | guest_base = map_zeroed_pages(mem / getpagesize() | 1837 | guest_base = map_zeroed_pages(mem / getpagesize() |
1690 | + DEVICE_PAGES); | 1838 | + DEVICE_PAGES); |
1691 | guest_limit = mem; | 1839 | guest_limit = mem; |
@@ -1718,8 +1866,10 @@ int main(int argc, char *argv[]) | |||
1718 | usage(); | 1866 | usage(); |
1719 | } | 1867 | } |
1720 | } | 1868 | } |
1721 | /* After the other arguments we expect memory and kernel image name, | 1869 | /* |
1722 | * followed by command line arguments for the kernel. */ | 1870 | * After the other arguments we expect memory and kernel image name, |
1871 | * followed by command line arguments for the kernel. | ||
1872 | */ | ||
1723 | if (optind + 2 > argc) | 1873 | if (optind + 2 > argc) |
1724 | usage(); | 1874 | usage(); |
1725 | 1875 | ||
@@ -1737,20 +1887,26 @@ int main(int argc, char *argv[]) | |||
1737 | /* Map the initrd image if requested (at top of physical memory) */ | 1887 | /* Map the initrd image if requested (at top of physical memory) */ |
1738 | if (initrd_name) { | 1888 | if (initrd_name) { |
1739 | initrd_size = load_initrd(initrd_name, mem); | 1889 | initrd_size = load_initrd(initrd_name, mem); |
1740 | /* These are the location in the Linux boot header where the | 1890 | /* |
1741 | * start and size of the initrd are expected to be found. */ | 1891 | * These are the location in the Linux boot header where the |
1892 | * start and size of the initrd are expected to be found. | ||
1893 | */ | ||
1742 | boot->hdr.ramdisk_image = mem - initrd_size; | 1894 | boot->hdr.ramdisk_image = mem - initrd_size; |
1743 | boot->hdr.ramdisk_size = initrd_size; | 1895 | boot->hdr.ramdisk_size = initrd_size; |
1744 | /* The bootloader type 0xFF means "unknown"; that's OK. */ | 1896 | /* The bootloader type 0xFF means "unknown"; that's OK. */ |
1745 | boot->hdr.type_of_loader = 0xFF; | 1897 | boot->hdr.type_of_loader = 0xFF; |
1746 | } | 1898 | } |
1747 | 1899 | ||
1748 | /* The Linux boot header contains an "E820" memory map: ours is a | 1900 | /* |
1749 | * simple, single region. */ | 1901 | * The Linux boot header contains an "E820" memory map: ours is a |
1902 | * simple, single region. | ||
1903 | */ | ||
1750 | boot->e820_entries = 1; | 1904 | boot->e820_entries = 1; |
1751 | boot->e820_map[0] = ((struct e820entry) { 0, mem, E820_RAM }); | 1905 | boot->e820_map[0] = ((struct e820entry) { 0, mem, E820_RAM }); |
1752 | /* The boot header contains a command line pointer: we put the command | 1906 | /* |
1753 | * line after the boot header. */ | 1907 | * The boot header contains a command line pointer: we put the command |
1908 | * line after the boot header. | ||
1909 | */ | ||
1754 | boot->hdr.cmd_line_ptr = to_guest_phys(boot + 1); | 1910 | boot->hdr.cmd_line_ptr = to_guest_phys(boot + 1); |
1755 | /* We use a simple helper to copy the arguments separated by spaces. */ | 1911 | /* We use a simple helper to copy the arguments separated by spaces. */ |
1756 | concat((char *)(boot + 1), argv+optind+2); | 1912 | concat((char *)(boot + 1), argv+optind+2); |
@@ -1764,8 +1920,10 @@ int main(int argc, char *argv[]) | |||
1764 | /* Tell the entry path not to try to reload segment registers. */ | 1920 | /* Tell the entry path not to try to reload segment registers. */ |
1765 | boot->hdr.loadflags |= KEEP_SEGMENTS; | 1921 | boot->hdr.loadflags |= KEEP_SEGMENTS; |
1766 | 1922 | ||
1767 | /* We tell the kernel to initialize the Guest: this returns the open | 1923 | /* |
1768 | * /dev/lguest file descriptor. */ | 1924 | * We tell the kernel to initialize the Guest: this returns the open |
1925 | * /dev/lguest file descriptor. | ||
1926 | */ | ||
1769 | tell_kernel(start); | 1927 | tell_kernel(start); |
1770 | 1928 | ||
1771 | /* Ensure that we terminate if a child dies. */ | 1929 | /* Ensure that we terminate if a child dies. */ |
diff --git a/arch/x86/include/asm/lguest.h b/arch/x86/include/asm/lguest.h index 313389cd50d2..5136dad57cbb 100644 --- a/arch/x86/include/asm/lguest.h +++ b/arch/x86/include/asm/lguest.h | |||
@@ -17,8 +17,7 @@ | |||
17 | /* Pages for switcher itself, then two pages per cpu */ | 17 | /* Pages for switcher itself, then two pages per cpu */ |
18 | #define TOTAL_SWITCHER_PAGES (SHARED_SWITCHER_PAGES + 2 * nr_cpu_ids) | 18 | #define TOTAL_SWITCHER_PAGES (SHARED_SWITCHER_PAGES + 2 * nr_cpu_ids) |
19 | 19 | ||
20 | /* We map at -4M (-2M when PAE is activated) for ease of mapping | 20 | /* We map at -4M (-2M for PAE) for ease of mapping (one PTE page). */ |
21 | * into the guest (one PTE page). */ | ||
22 | #ifdef CONFIG_X86_PAE | 21 | #ifdef CONFIG_X86_PAE |
23 | #define SWITCHER_ADDR 0xFFE00000 | 22 | #define SWITCHER_ADDR 0xFFE00000 |
24 | #else | 23 | #else |
diff --git a/arch/x86/include/asm/lguest_hcall.h b/arch/x86/include/asm/lguest_hcall.h index 33600a66755f..cceb73e12e50 100644 --- a/arch/x86/include/asm/lguest_hcall.h +++ b/arch/x86/include/asm/lguest_hcall.h | |||
@@ -30,7 +30,8 @@ | |||
30 | #include <asm/hw_irq.h> | 30 | #include <asm/hw_irq.h> |
31 | #include <asm/kvm_para.h> | 31 | #include <asm/kvm_para.h> |
32 | 32 | ||
33 | /*G:030 But first, how does our Guest contact the Host to ask for privileged | 33 | /*G:030 |
34 | * But first, how does our Guest contact the Host to ask for privileged | ||
34 | * operations? There are two ways: the direct way is to make a "hypercall", | 35 | * operations? There are two ways: the direct way is to make a "hypercall", |
35 | * to make requests of the Host Itself. | 36 | * to make requests of the Host Itself. |
36 | * | 37 | * |
@@ -41,16 +42,15 @@ | |||
41 | * | 42 | * |
42 | * Grossly invalid calls result in Sudden Death at the hands of the vengeful | 43 | * Grossly invalid calls result in Sudden Death at the hands of the vengeful |
43 | * Host, rather than returning failure. This reflects Winston Churchill's | 44 | * Host, rather than returning failure. This reflects Winston Churchill's |
44 | * definition of a gentleman: "someone who is only rude intentionally". */ | 45 | * definition of a gentleman: "someone who is only rude intentionally". |
45 | /*:*/ | 46 | :*/ |
46 | 47 | ||
47 | /* Can't use our min() macro here: needs to be a constant */ | 48 | /* Can't use our min() macro here: needs to be a constant */ |
48 | #define LGUEST_IRQS (NR_IRQS < 32 ? NR_IRQS: 32) | 49 | #define LGUEST_IRQS (NR_IRQS < 32 ? NR_IRQS: 32) |
49 | 50 | ||
50 | #define LHCALL_RING_SIZE 64 | 51 | #define LHCALL_RING_SIZE 64 |
51 | struct hcall_args { | 52 | struct hcall_args { |
52 | /* These map directly onto eax, ebx, ecx, edx and esi | 53 | /* These map directly onto eax/ebx/ecx/edx/esi in struct lguest_regs */ |
53 | * in struct lguest_regs */ | ||
54 | unsigned long arg0, arg1, arg2, arg3, arg4; | 54 | unsigned long arg0, arg1, arg2, arg3, arg4; |
55 | }; | 55 | }; |
56 | 56 | ||
diff --git a/arch/x86/lguest/boot.c b/arch/x86/lguest/boot.c index f2bf1f73d468..025c04d18f2b 100644 --- a/arch/x86/lguest/boot.c +++ b/arch/x86/lguest/boot.c | |||
@@ -22,7 +22,8 @@ | |||
22 | * | 22 | * |
23 | * So how does the kernel know it's a Guest? We'll see that later, but let's | 23 | * So how does the kernel know it's a Guest? We'll see that later, but let's |
24 | * just say that we end up here where we replace the native functions various | 24 | * just say that we end up here where we replace the native functions various |
25 | * "paravirt" structures with our Guest versions, then boot like normal. :*/ | 25 | * "paravirt" structures with our Guest versions, then boot like normal. |
26 | :*/ | ||
26 | 27 | ||
27 | /* | 28 | /* |
28 | * Copyright (C) 2006, Rusty Russell <rusty@rustcorp.com.au> IBM Corporation. | 29 | * Copyright (C) 2006, Rusty Russell <rusty@rustcorp.com.au> IBM Corporation. |
@@ -74,7 +75,8 @@ | |||
74 | * | 75 | * |
75 | * The Guest in our tale is a simple creature: identical to the Host but | 76 | * The Guest in our tale is a simple creature: identical to the Host but |
76 | * behaving in simplified but equivalent ways. In particular, the Guest is the | 77 | * behaving in simplified but equivalent ways. In particular, the Guest is the |
77 | * same kernel as the Host (or at least, built from the same source code). :*/ | 78 | * same kernel as the Host (or at least, built from the same source code). |
79 | :*/ | ||
78 | 80 | ||
79 | struct lguest_data lguest_data = { | 81 | struct lguest_data lguest_data = { |
80 | .hcall_status = { [0 ... LHCALL_RING_SIZE-1] = 0xFF }, | 82 | .hcall_status = { [0 ... LHCALL_RING_SIZE-1] = 0xFF }, |
@@ -85,7 +87,8 @@ struct lguest_data lguest_data = { | |||
85 | .syscall_vec = SYSCALL_VECTOR, | 87 | .syscall_vec = SYSCALL_VECTOR, |
86 | }; | 88 | }; |
87 | 89 | ||
88 | /*G:037 async_hcall() is pretty simple: I'm quite proud of it really. We have a | 90 | /*G:037 |
91 | * async_hcall() is pretty simple: I'm quite proud of it really. We have a | ||
89 | * ring buffer of stored hypercalls which the Host will run though next time we | 92 | * ring buffer of stored hypercalls which the Host will run though next time we |
90 | * do a normal hypercall. Each entry in the ring has 5 slots for the hypercall | 93 | * do a normal hypercall. Each entry in the ring has 5 slots for the hypercall |
91 | * arguments, and a "hcall_status" word which is 0 if the call is ready to go, | 94 | * arguments, and a "hcall_status" word which is 0 if the call is ready to go, |
@@ -94,7 +97,8 @@ struct lguest_data lguest_data = { | |||
94 | * If we come around to a slot which hasn't been finished, then the table is | 97 | * If we come around to a slot which hasn't been finished, then the table is |
95 | * full and we just make the hypercall directly. This has the nice side | 98 | * full and we just make the hypercall directly. This has the nice side |
96 | * effect of causing the Host to run all the stored calls in the ring buffer | 99 | * effect of causing the Host to run all the stored calls in the ring buffer |
97 | * which empties it for next time! */ | 100 | * which empties it for next time! |
101 | */ | ||
98 | static void async_hcall(unsigned long call, unsigned long arg1, | 102 | static void async_hcall(unsigned long call, unsigned long arg1, |
99 | unsigned long arg2, unsigned long arg3, | 103 | unsigned long arg2, unsigned long arg3, |
100 | unsigned long arg4) | 104 | unsigned long arg4) |
@@ -103,9 +107,11 @@ static void async_hcall(unsigned long call, unsigned long arg1, | |||
103 | static unsigned int next_call; | 107 | static unsigned int next_call; |
104 | unsigned long flags; | 108 | unsigned long flags; |
105 | 109 | ||
106 | /* Disable interrupts if not already disabled: we don't want an | 110 | /* |
111 | * Disable interrupts if not already disabled: we don't want an | ||
107 | * interrupt handler making a hypercall while we're already doing | 112 | * interrupt handler making a hypercall while we're already doing |
108 | * one! */ | 113 | * one! |
114 | */ | ||
109 | local_irq_save(flags); | 115 | local_irq_save(flags); |
110 | if (lguest_data.hcall_status[next_call] != 0xFF) { | 116 | if (lguest_data.hcall_status[next_call] != 0xFF) { |
111 | /* Table full, so do normal hcall which will flush table. */ | 117 | /* Table full, so do normal hcall which will flush table. */ |
@@ -125,8 +131,9 @@ static void async_hcall(unsigned long call, unsigned long arg1, | |||
125 | local_irq_restore(flags); | 131 | local_irq_restore(flags); |
126 | } | 132 | } |
127 | 133 | ||
128 | /*G:035 Notice the lazy_hcall() above, rather than hcall(). This is our first | 134 | /*G:035 |
129 | * real optimization trick! | 135 | * Notice the lazy_hcall() above, rather than hcall(). This is our first real |
136 | * optimization trick! | ||
130 | * | 137 | * |
131 | * When lazy_mode is set, it means we're allowed to defer all hypercalls and do | 138 | * When lazy_mode is set, it means we're allowed to defer all hypercalls and do |
132 | * them as a batch when lazy_mode is eventually turned off. Because hypercalls | 139 | * them as a batch when lazy_mode is eventually turned off. Because hypercalls |
@@ -136,7 +143,8 @@ static void async_hcall(unsigned long call, unsigned long arg1, | |||
136 | * lguest_leave_lazy_mode(). | 143 | * lguest_leave_lazy_mode(). |
137 | * | 144 | * |
138 | * So, when we're in lazy mode, we call async_hcall() to store the call for | 145 | * So, when we're in lazy mode, we call async_hcall() to store the call for |
139 | * future processing: */ | 146 | * future processing: |
147 | */ | ||
140 | static void lazy_hcall1(unsigned long call, | 148 | static void lazy_hcall1(unsigned long call, |
141 | unsigned long arg1) | 149 | unsigned long arg1) |
142 | { | 150 | { |
@@ -208,9 +216,11 @@ static void lguest_end_context_switch(struct task_struct *next) | |||
208 | * check there before it tries to deliver an interrupt. | 216 | * check there before it tries to deliver an interrupt. |
209 | */ | 217 | */ |
210 | 218 | ||
211 | /* save_flags() is expected to return the processor state (ie. "flags"). The | 219 | /* |
220 | * save_flags() is expected to return the processor state (ie. "flags"). The | ||
212 | * flags word contains all kind of stuff, but in practice Linux only cares | 221 | * flags word contains all kind of stuff, but in practice Linux only cares |
213 | * about the interrupt flag. Our "save_flags()" just returns that. */ | 222 | * about the interrupt flag. Our "save_flags()" just returns that. |
223 | */ | ||
214 | static unsigned long save_fl(void) | 224 | static unsigned long save_fl(void) |
215 | { | 225 | { |
216 | return lguest_data.irq_enabled; | 226 | return lguest_data.irq_enabled; |
@@ -222,13 +232,15 @@ static void irq_disable(void) | |||
222 | lguest_data.irq_enabled = 0; | 232 | lguest_data.irq_enabled = 0; |
223 | } | 233 | } |
224 | 234 | ||
225 | /* Let's pause a moment. Remember how I said these are called so often? | 235 | /* |
236 | * Let's pause a moment. Remember how I said these are called so often? | ||
226 | * Jeremy Fitzhardinge optimized them so hard early in 2009 that he had to | 237 | * Jeremy Fitzhardinge optimized them so hard early in 2009 that he had to |
227 | * break some rules. In particular, these functions are assumed to save their | 238 | * break some rules. In particular, these functions are assumed to save their |
228 | * own registers if they need to: normal C functions assume they can trash the | 239 | * own registers if they need to: normal C functions assume they can trash the |
229 | * eax register. To use normal C functions, we use | 240 | * eax register. To use normal C functions, we use |
230 | * PV_CALLEE_SAVE_REGS_THUNK(), which pushes %eax onto the stack, calls the | 241 | * PV_CALLEE_SAVE_REGS_THUNK(), which pushes %eax onto the stack, calls the |
231 | * C function, then restores it. */ | 242 | * C function, then restores it. |
243 | */ | ||
232 | PV_CALLEE_SAVE_REGS_THUNK(save_fl); | 244 | PV_CALLEE_SAVE_REGS_THUNK(save_fl); |
233 | PV_CALLEE_SAVE_REGS_THUNK(irq_disable); | 245 | PV_CALLEE_SAVE_REGS_THUNK(irq_disable); |
234 | /*:*/ | 246 | /*:*/ |
@@ -237,18 +249,20 @@ PV_CALLEE_SAVE_REGS_THUNK(irq_disable); | |||
237 | extern void lg_irq_enable(void); | 249 | extern void lg_irq_enable(void); |
238 | extern void lg_restore_fl(unsigned long flags); | 250 | extern void lg_restore_fl(unsigned long flags); |
239 | 251 | ||
240 | /*M:003 Note that we don't check for outstanding interrupts when we re-enable | 252 | /*M:003 |
241 | * them (or when we unmask an interrupt). This seems to work for the moment, | 253 | * Note that we don't check for outstanding interrupts when we re-enable them |
242 | * since interrupts are rare and we'll just get the interrupt on the next timer | 254 | * (or when we unmask an interrupt). This seems to work for the moment, since |
243 | * tick, but now we can run with CONFIG_NO_HZ, we should revisit this. One way | 255 | * interrupts are rare and we'll just get the interrupt on the next timer tick, |
244 | * would be to put the "irq_enabled" field in a page by itself, and have the | 256 | * but now we can run with CONFIG_NO_HZ, we should revisit this. One way would |
245 | * Host write-protect it when an interrupt comes in when irqs are disabled. | 257 | * be to put the "irq_enabled" field in a page by itself, and have the Host |
246 | * There will then be a page fault as soon as interrupts are re-enabled. | 258 | * write-protect it when an interrupt comes in when irqs are disabled. There |
259 | * will then be a page fault as soon as interrupts are re-enabled. | ||
247 | * | 260 | * |
248 | * A better method is to implement soft interrupt disable generally for x86: | 261 | * A better method is to implement soft interrupt disable generally for x86: |
249 | * instead of disabling interrupts, we set a flag. If an interrupt does come | 262 | * instead of disabling interrupts, we set a flag. If an interrupt does come |
250 | * in, we then disable them for real. This is uncommon, so we could simply use | 263 | * in, we then disable them for real. This is uncommon, so we could simply use |
251 | * a hypercall for interrupt control and not worry about efficiency. :*/ | 264 | * a hypercall for interrupt control and not worry about efficiency. |
265 | :*/ | ||
252 | 266 | ||
253 | /*G:034 | 267 | /*G:034 |
254 | * The Interrupt Descriptor Table (IDT). | 268 | * The Interrupt Descriptor Table (IDT). |
@@ -261,10 +275,12 @@ extern void lg_restore_fl(unsigned long flags); | |||
261 | static void lguest_write_idt_entry(gate_desc *dt, | 275 | static void lguest_write_idt_entry(gate_desc *dt, |
262 | int entrynum, const gate_desc *g) | 276 | int entrynum, const gate_desc *g) |
263 | { | 277 | { |
264 | /* The gate_desc structure is 8 bytes long: we hand it to the Host in | 278 | /* |
279 | * The gate_desc structure is 8 bytes long: we hand it to the Host in | ||
265 | * two 32-bit chunks. The whole 32-bit kernel used to hand descriptors | 280 | * two 32-bit chunks. The whole 32-bit kernel used to hand descriptors |
266 | * around like this; typesafety wasn't a big concern in Linux's early | 281 | * around like this; typesafety wasn't a big concern in Linux's early |
267 | * years. */ | 282 | * years. |
283 | */ | ||
268 | u32 *desc = (u32 *)g; | 284 | u32 *desc = (u32 *)g; |
269 | /* Keep the local copy up to date. */ | 285 | /* Keep the local copy up to date. */ |
270 | native_write_idt_entry(dt, entrynum, g); | 286 | native_write_idt_entry(dt, entrynum, g); |
@@ -272,9 +288,11 @@ static void lguest_write_idt_entry(gate_desc *dt, | |||
272 | kvm_hypercall3(LHCALL_LOAD_IDT_ENTRY, entrynum, desc[0], desc[1]); | 288 | kvm_hypercall3(LHCALL_LOAD_IDT_ENTRY, entrynum, desc[0], desc[1]); |
273 | } | 289 | } |
274 | 290 | ||
275 | /* Changing to a different IDT is very rare: we keep the IDT up-to-date every | 291 | /* |
292 | * Changing to a different IDT is very rare: we keep the IDT up-to-date every | ||
276 | * time it is written, so we can simply loop through all entries and tell the | 293 | * time it is written, so we can simply loop through all entries and tell the |
277 | * Host about them. */ | 294 | * Host about them. |
295 | */ | ||
278 | static void lguest_load_idt(const struct desc_ptr *desc) | 296 | static void lguest_load_idt(const struct desc_ptr *desc) |
279 | { | 297 | { |
280 | unsigned int i; | 298 | unsigned int i; |
@@ -305,9 +323,11 @@ static void lguest_load_gdt(const struct desc_ptr *desc) | |||
305 | kvm_hypercall3(LHCALL_LOAD_GDT_ENTRY, i, gdt[i].a, gdt[i].b); | 323 | kvm_hypercall3(LHCALL_LOAD_GDT_ENTRY, i, gdt[i].a, gdt[i].b); |
306 | } | 324 | } |
307 | 325 | ||
308 | /* For a single GDT entry which changes, we do the lazy thing: alter our GDT, | 326 | /* |
327 | * For a single GDT entry which changes, we do the lazy thing: alter our GDT, | ||
309 | * then tell the Host to reload the entire thing. This operation is so rare | 328 | * then tell the Host to reload the entire thing. This operation is so rare |
310 | * that this naive implementation is reasonable. */ | 329 | * that this naive implementation is reasonable. |
330 | */ | ||
311 | static void lguest_write_gdt_entry(struct desc_struct *dt, int entrynum, | 331 | static void lguest_write_gdt_entry(struct desc_struct *dt, int entrynum, |
312 | const void *desc, int type) | 332 | const void *desc, int type) |
313 | { | 333 | { |
@@ -317,29 +337,36 @@ static void lguest_write_gdt_entry(struct desc_struct *dt, int entrynum, | |||
317 | dt[entrynum].a, dt[entrynum].b); | 337 | dt[entrynum].a, dt[entrynum].b); |
318 | } | 338 | } |
319 | 339 | ||
320 | /* OK, I lied. There are three "thread local storage" GDT entries which change | 340 | /* |
341 | * OK, I lied. There are three "thread local storage" GDT entries which change | ||
321 | * on every context switch (these three entries are how glibc implements | 342 | * on every context switch (these three entries are how glibc implements |
322 | * __thread variables). So we have a hypercall specifically for this case. */ | 343 | * __thread variables). So we have a hypercall specifically for this case. |
344 | */ | ||
323 | static void lguest_load_tls(struct thread_struct *t, unsigned int cpu) | 345 | static void lguest_load_tls(struct thread_struct *t, unsigned int cpu) |
324 | { | 346 | { |
325 | /* There's one problem which normal hardware doesn't have: the Host | 347 | /* |
348 | * There's one problem which normal hardware doesn't have: the Host | ||
326 | * can't handle us removing entries we're currently using. So we clear | 349 | * can't handle us removing entries we're currently using. So we clear |
327 | * the GS register here: if it's needed it'll be reloaded anyway. */ | 350 | * the GS register here: if it's needed it'll be reloaded anyway. |
351 | */ | ||
328 | lazy_load_gs(0); | 352 | lazy_load_gs(0); |
329 | lazy_hcall2(LHCALL_LOAD_TLS, __pa(&t->tls_array), cpu); | 353 | lazy_hcall2(LHCALL_LOAD_TLS, __pa(&t->tls_array), cpu); |
330 | } | 354 | } |
331 | 355 | ||
332 | /*G:038 That's enough excitement for now, back to ploughing through each of | 356 | /*G:038 |
333 | * the different pv_ops structures (we're about 1/3 of the way through). | 357 | * That's enough excitement for now, back to ploughing through each of the |
358 | * different pv_ops structures (we're about 1/3 of the way through). | ||
334 | * | 359 | * |
335 | * This is the Local Descriptor Table, another weird Intel thingy. Linux only | 360 | * This is the Local Descriptor Table, another weird Intel thingy. Linux only |
336 | * uses this for some strange applications like Wine. We don't do anything | 361 | * uses this for some strange applications like Wine. We don't do anything |
337 | * here, so they'll get an informative and friendly Segmentation Fault. */ | 362 | * here, so they'll get an informative and friendly Segmentation Fault. |
363 | */ | ||
338 | static void lguest_set_ldt(const void *addr, unsigned entries) | 364 | static void lguest_set_ldt(const void *addr, unsigned entries) |
339 | { | 365 | { |
340 | } | 366 | } |
341 | 367 | ||
342 | /* This loads a GDT entry into the "Task Register": that entry points to a | 368 | /* |
369 | * This loads a GDT entry into the "Task Register": that entry points to a | ||
343 | * structure called the Task State Segment. Some comments scattered though the | 370 | * structure called the Task State Segment. Some comments scattered though the |
344 | * kernel code indicate that this used for task switching in ages past, along | 371 | * kernel code indicate that this used for task switching in ages past, along |
345 | * with blood sacrifice and astrology. | 372 | * with blood sacrifice and astrology. |
@@ -347,19 +374,21 @@ static void lguest_set_ldt(const void *addr, unsigned entries) | |||
347 | * Now there's nothing interesting in here that we don't get told elsewhere. | 374 | * Now there's nothing interesting in here that we don't get told elsewhere. |
348 | * But the native version uses the "ltr" instruction, which makes the Host | 375 | * But the native version uses the "ltr" instruction, which makes the Host |
349 | * complain to the Guest about a Segmentation Fault and it'll oops. So we | 376 | * complain to the Guest about a Segmentation Fault and it'll oops. So we |
350 | * override the native version with a do-nothing version. */ | 377 | * override the native version with a do-nothing version. |
378 | */ | ||
351 | static void lguest_load_tr_desc(void) | 379 | static void lguest_load_tr_desc(void) |
352 | { | 380 | { |
353 | } | 381 | } |
354 | 382 | ||
355 | /* The "cpuid" instruction is a way of querying both the CPU identity | 383 | /* |
384 | * The "cpuid" instruction is a way of querying both the CPU identity | ||
356 | * (manufacturer, model, etc) and its features. It was introduced before the | 385 | * (manufacturer, model, etc) and its features. It was introduced before the |
357 | * Pentium in 1993 and keeps getting extended by both Intel, AMD and others. | 386 | * Pentium in 1993 and keeps getting extended by both Intel, AMD and others. |
358 | * As you might imagine, after a decade and a half this treatment, it is now a | 387 | * As you might imagine, after a decade and a half this treatment, it is now a |
359 | * giant ball of hair. Its entry in the current Intel manual runs to 28 pages. | 388 | * giant ball of hair. Its entry in the current Intel manual runs to 28 pages. |
360 | * | 389 | * |
361 | * This instruction even it has its own Wikipedia entry. The Wikipedia entry | 390 | * This instruction even it has its own Wikipedia entry. The Wikipedia entry |
362 | * has been translated into 4 languages. I am not making this up! | 391 | * has been translated into 5 languages. I am not making this up! |
363 | * | 392 | * |
364 | * We could get funky here and identify ourselves as "GenuineLguest", but | 393 | * We could get funky here and identify ourselves as "GenuineLguest", but |
365 | * instead we just use the real "cpuid" instruction. Then I pretty much turned | 394 | * instead we just use the real "cpuid" instruction. Then I pretty much turned |
@@ -371,7 +400,8 @@ static void lguest_load_tr_desc(void) | |||
371 | * Replacing the cpuid so we can turn features off is great for the kernel, but | 400 | * Replacing the cpuid so we can turn features off is great for the kernel, but |
372 | * anyone (including userspace) can just use the raw "cpuid" instruction and | 401 | * anyone (including userspace) can just use the raw "cpuid" instruction and |
373 | * the Host won't even notice since it isn't privileged. So we try not to get | 402 | * the Host won't even notice since it isn't privileged. So we try not to get |
374 | * too worked up about it. */ | 403 | * too worked up about it. |
404 | */ | ||
375 | static void lguest_cpuid(unsigned int *ax, unsigned int *bx, | 405 | static void lguest_cpuid(unsigned int *ax, unsigned int *bx, |
376 | unsigned int *cx, unsigned int *dx) | 406 | unsigned int *cx, unsigned int *dx) |
377 | { | 407 | { |
@@ -379,43 +409,63 @@ static void lguest_cpuid(unsigned int *ax, unsigned int *bx, | |||
379 | 409 | ||
380 | native_cpuid(ax, bx, cx, dx); | 410 | native_cpuid(ax, bx, cx, dx); |
381 | switch (function) { | 411 | switch (function) { |
382 | case 0: /* ID and highest CPUID. Futureproof a little by sticking to | 412 | /* |
383 | * older ones. */ | 413 | * CPUID 0 gives the highest legal CPUID number (and the ID string). |
414 | * We futureproof our code a little by sticking to known CPUID values. | ||
415 | */ | ||
416 | case 0: | ||
384 | if (*ax > 5) | 417 | if (*ax > 5) |
385 | *ax = 5; | 418 | *ax = 5; |
386 | break; | 419 | break; |
387 | case 1: /* Basic feature request. */ | 420 | |
388 | /* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */ | 421 | /* |
422 | * CPUID 1 is a basic feature request. | ||
423 | * | ||
424 | * CX: we only allow kernel to see SSE3, CMPXCHG16B and SSSE3 | ||
425 | * DX: SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, TSC, FPU and PAE. | ||
426 | */ | ||
427 | case 1: | ||
389 | *cx &= 0x00002201; | 428 | *cx &= 0x00002201; |
390 | /* SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, TSC, FPU, PAE. */ | ||
391 | *dx &= 0x07808151; | 429 | *dx &= 0x07808151; |
392 | /* The Host can do a nice optimization if it knows that the | 430 | /* |
431 | * The Host can do a nice optimization if it knows that the | ||
393 | * kernel mappings (addresses above 0xC0000000 or whatever | 432 | * kernel mappings (addresses above 0xC0000000 or whatever |
394 | * PAGE_OFFSET is set to) haven't changed. But Linux calls | 433 | * PAGE_OFFSET is set to) haven't changed. But Linux calls |
395 | * flush_tlb_user() for both user and kernel mappings unless | 434 | * flush_tlb_user() for both user and kernel mappings unless |
396 | * the Page Global Enable (PGE) feature bit is set. */ | 435 | * the Page Global Enable (PGE) feature bit is set. |
436 | */ | ||
397 | *dx |= 0x00002000; | 437 | *dx |= 0x00002000; |
398 | /* We also lie, and say we're family id 5. 6 or greater | 438 | /* |
439 | * We also lie, and say we're family id 5. 6 or greater | ||
399 | * leads to a rdmsr in early_init_intel which we can't handle. | 440 | * leads to a rdmsr in early_init_intel which we can't handle. |
400 | * Family ID is returned as bits 8-12 in ax. */ | 441 | * Family ID is returned as bits 8-12 in ax. |
442 | */ | ||
401 | *ax &= 0xFFFFF0FF; | 443 | *ax &= 0xFFFFF0FF; |
402 | *ax |= 0x00000500; | 444 | *ax |= 0x00000500; |
403 | break; | 445 | break; |
446 | /* | ||
447 | * 0x80000000 returns the highest Extended Function, so we futureproof | ||
448 | * like we do above by limiting it to known fields. | ||
449 | */ | ||
404 | case 0x80000000: | 450 | case 0x80000000: |
405 | /* Futureproof this a little: if they ask how much extended | ||
406 | * processor information there is, limit it to known fields. */ | ||
407 | if (*ax > 0x80000008) | 451 | if (*ax > 0x80000008) |
408 | *ax = 0x80000008; | 452 | *ax = 0x80000008; |
409 | break; | 453 | break; |
454 | |||
455 | /* | ||
456 | * PAE systems can mark pages as non-executable. Linux calls this the | ||
457 | * NX bit. Intel calls it XD (eXecute Disable), AMD EVP (Enhanced | ||
458 | * Virus Protection). We just switch turn if off here, since we don't | ||
459 | * support it. | ||
460 | */ | ||
410 | case 0x80000001: | 461 | case 0x80000001: |
411 | /* Here we should fix nx cap depending on host. */ | ||
412 | /* For this version of PAE, we just clear NX bit. */ | ||
413 | *dx &= ~(1 << 20); | 462 | *dx &= ~(1 << 20); |
414 | break; | 463 | break; |
415 | } | 464 | } |
416 | } | 465 | } |
417 | 466 | ||
418 | /* Intel has four control registers, imaginatively named cr0, cr2, cr3 and cr4. | 467 | /* |
468 | * Intel has four control registers, imaginatively named cr0, cr2, cr3 and cr4. | ||
419 | * I assume there's a cr1, but it hasn't bothered us yet, so we'll not bother | 469 | * I assume there's a cr1, but it hasn't bothered us yet, so we'll not bother |
420 | * it. The Host needs to know when the Guest wants to change them, so we have | 470 | * it. The Host needs to know when the Guest wants to change them, so we have |
421 | * a whole series of functions like read_cr0() and write_cr0(). | 471 | * a whole series of functions like read_cr0() and write_cr0(). |
@@ -430,7 +480,8 @@ static void lguest_cpuid(unsigned int *ax, unsigned int *bx, | |||
430 | * name like "FPUTRAP bit" be a little less cryptic? | 480 | * name like "FPUTRAP bit" be a little less cryptic? |
431 | * | 481 | * |
432 | * We store cr0 locally because the Host never changes it. The Guest sometimes | 482 | * We store cr0 locally because the Host never changes it. The Guest sometimes |
433 | * wants to read it and we'd prefer not to bother the Host unnecessarily. */ | 483 | * wants to read it and we'd prefer not to bother the Host unnecessarily. |
484 | */ | ||
434 | static unsigned long current_cr0; | 485 | static unsigned long current_cr0; |
435 | static void lguest_write_cr0(unsigned long val) | 486 | static void lguest_write_cr0(unsigned long val) |
436 | { | 487 | { |
@@ -443,18 +494,22 @@ static unsigned long lguest_read_cr0(void) | |||
443 | return current_cr0; | 494 | return current_cr0; |
444 | } | 495 | } |
445 | 496 | ||
446 | /* Intel provided a special instruction to clear the TS bit for people too cool | 497 | /* |
498 | * Intel provided a special instruction to clear the TS bit for people too cool | ||
447 | * to use write_cr0() to do it. This "clts" instruction is faster, because all | 499 | * to use write_cr0() to do it. This "clts" instruction is faster, because all |
448 | * the vowels have been optimized out. */ | 500 | * the vowels have been optimized out. |
501 | */ | ||
449 | static void lguest_clts(void) | 502 | static void lguest_clts(void) |
450 | { | 503 | { |
451 | lazy_hcall1(LHCALL_TS, 0); | 504 | lazy_hcall1(LHCALL_TS, 0); |
452 | current_cr0 &= ~X86_CR0_TS; | 505 | current_cr0 &= ~X86_CR0_TS; |
453 | } | 506 | } |
454 | 507 | ||
455 | /* cr2 is the virtual address of the last page fault, which the Guest only ever | 508 | /* |
509 | * cr2 is the virtual address of the last page fault, which the Guest only ever | ||
456 | * reads. The Host kindly writes this into our "struct lguest_data", so we | 510 | * reads. The Host kindly writes this into our "struct lguest_data", so we |
457 | * just read it out of there. */ | 511 | * just read it out of there. |
512 | */ | ||
458 | static unsigned long lguest_read_cr2(void) | 513 | static unsigned long lguest_read_cr2(void) |
459 | { | 514 | { |
460 | return lguest_data.cr2; | 515 | return lguest_data.cr2; |
@@ -463,10 +518,12 @@ static unsigned long lguest_read_cr2(void) | |||
463 | /* See lguest_set_pte() below. */ | 518 | /* See lguest_set_pte() below. */ |
464 | static bool cr3_changed = false; | 519 | static bool cr3_changed = false; |
465 | 520 | ||
466 | /* cr3 is the current toplevel pagetable page: the principle is the same as | 521 | /* |
522 | * cr3 is the current toplevel pagetable page: the principle is the same as | ||
467 | * cr0. Keep a local copy, and tell the Host when it changes. The only | 523 | * cr0. Keep a local copy, and tell the Host when it changes. The only |
468 | * difference is that our local copy is in lguest_data because the Host needs | 524 | * difference is that our local copy is in lguest_data because the Host needs |
469 | * to set it upon our initial hypercall. */ | 525 | * to set it upon our initial hypercall. |
526 | */ | ||
470 | static void lguest_write_cr3(unsigned long cr3) | 527 | static void lguest_write_cr3(unsigned long cr3) |
471 | { | 528 | { |
472 | lguest_data.pgdir = cr3; | 529 | lguest_data.pgdir = cr3; |
@@ -538,10 +595,12 @@ static void lguest_write_cr4(unsigned long val) | |||
538 | * the real page tables based on the Guests'. | 595 | * the real page tables based on the Guests'. |
539 | */ | 596 | */ |
540 | 597 | ||
541 | /* The Guest calls this to set a second-level entry (pte), ie. to map a page | 598 | /* |
599 | * The Guest calls this to set a second-level entry (pte), ie. to map a page | ||
542 | * into a process' address space. We set the entry then tell the Host the | 600 | * into a process' address space. We set the entry then tell the Host the |
543 | * toplevel and address this corresponds to. The Guest uses one pagetable per | 601 | * toplevel and address this corresponds to. The Guest uses one pagetable per |
544 | * process, so we need to tell the Host which one we're changing (mm->pgd). */ | 602 | * process, so we need to tell the Host which one we're changing (mm->pgd). |
603 | */ | ||
545 | static void lguest_pte_update(struct mm_struct *mm, unsigned long addr, | 604 | static void lguest_pte_update(struct mm_struct *mm, unsigned long addr, |
546 | pte_t *ptep) | 605 | pte_t *ptep) |
547 | { | 606 | { |
@@ -560,10 +619,13 @@ static void lguest_set_pte_at(struct mm_struct *mm, unsigned long addr, | |||
560 | lguest_pte_update(mm, addr, ptep); | 619 | lguest_pte_update(mm, addr, ptep); |
561 | } | 620 | } |
562 | 621 | ||
563 | /* The Guest calls lguest_set_pud to set a top-level entry and lguest_set_pmd | 622 | /* |
623 | * The Guest calls lguest_set_pud to set a top-level entry and lguest_set_pmd | ||
564 | * to set a middle-level entry when PAE is activated. | 624 | * to set a middle-level entry when PAE is activated. |
625 | * | ||
565 | * Again, we set the entry then tell the Host which page we changed, | 626 | * Again, we set the entry then tell the Host which page we changed, |
566 | * and the index of the entry we changed. */ | 627 | * and the index of the entry we changed. |
628 | */ | ||
567 | #ifdef CONFIG_X86_PAE | 629 | #ifdef CONFIG_X86_PAE |
568 | static void lguest_set_pud(pud_t *pudp, pud_t pudval) | 630 | static void lguest_set_pud(pud_t *pudp, pud_t pudval) |
569 | { | 631 | { |
@@ -582,8 +644,7 @@ static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval) | |||
582 | } | 644 | } |
583 | #else | 645 | #else |
584 | 646 | ||
585 | /* The Guest calls lguest_set_pmd to set a top-level entry when PAE is not | 647 | /* The Guest calls lguest_set_pmd to set a top-level entry when !PAE. */ |
586 | * activated. */ | ||
587 | static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval) | 648 | static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval) |
588 | { | 649 | { |
589 | native_set_pmd(pmdp, pmdval); | 650 | native_set_pmd(pmdp, pmdval); |
@@ -592,7 +653,8 @@ static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval) | |||
592 | } | 653 | } |
593 | #endif | 654 | #endif |
594 | 655 | ||
595 | /* There are a couple of legacy places where the kernel sets a PTE, but we | 656 | /* |
657 | * There are a couple of legacy places where the kernel sets a PTE, but we | ||
596 | * don't know the top level any more. This is useless for us, since we don't | 658 | * don't know the top level any more. This is useless for us, since we don't |
597 | * know which pagetable is changing or what address, so we just tell the Host | 659 | * know which pagetable is changing or what address, so we just tell the Host |
598 | * to forget all of them. Fortunately, this is very rare. | 660 | * to forget all of them. Fortunately, this is very rare. |
@@ -600,7 +662,8 @@ static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval) | |||
600 | * ... except in early boot when the kernel sets up the initial pagetables, | 662 | * ... except in early boot when the kernel sets up the initial pagetables, |
601 | * which makes booting astonishingly slow: 1.83 seconds! So we don't even tell | 663 | * which makes booting astonishingly slow: 1.83 seconds! So we don't even tell |
602 | * the Host anything changed until we've done the first page table switch, | 664 | * the Host anything changed until we've done the first page table switch, |
603 | * which brings boot back to 0.25 seconds. */ | 665 | * which brings boot back to 0.25 seconds. |
666 | */ | ||
604 | static void lguest_set_pte(pte_t *ptep, pte_t pteval) | 667 | static void lguest_set_pte(pte_t *ptep, pte_t pteval) |
605 | { | 668 | { |
606 | native_set_pte(ptep, pteval); | 669 | native_set_pte(ptep, pteval); |
@@ -628,7 +691,8 @@ void lguest_pmd_clear(pmd_t *pmdp) | |||
628 | } | 691 | } |
629 | #endif | 692 | #endif |
630 | 693 | ||
631 | /* Unfortunately for Lguest, the pv_mmu_ops for page tables were based on | 694 | /* |
695 | * Unfortunately for Lguest, the pv_mmu_ops for page tables were based on | ||
632 | * native page table operations. On native hardware you can set a new page | 696 | * native page table operations. On native hardware you can set a new page |
633 | * table entry whenever you want, but if you want to remove one you have to do | 697 | * table entry whenever you want, but if you want to remove one you have to do |
634 | * a TLB flush (a TLB is a little cache of page table entries kept by the CPU). | 698 | * a TLB flush (a TLB is a little cache of page table entries kept by the CPU). |
@@ -637,24 +701,29 @@ void lguest_pmd_clear(pmd_t *pmdp) | |||
637 | * called when a valid entry is written, not when it's removed (ie. marked not | 701 | * called when a valid entry is written, not when it's removed (ie. marked not |
638 | * present). Instead, this is where we come when the Guest wants to remove a | 702 | * present). Instead, this is where we come when the Guest wants to remove a |
639 | * page table entry: we tell the Host to set that entry to 0 (ie. the present | 703 | * page table entry: we tell the Host to set that entry to 0 (ie. the present |
640 | * bit is zero). */ | 704 | * bit is zero). |
705 | */ | ||
641 | static void lguest_flush_tlb_single(unsigned long addr) | 706 | static void lguest_flush_tlb_single(unsigned long addr) |
642 | { | 707 | { |
643 | /* Simply set it to zero: if it was not, it will fault back in. */ | 708 | /* Simply set it to zero: if it was not, it will fault back in. */ |
644 | lazy_hcall3(LHCALL_SET_PTE, lguest_data.pgdir, addr, 0); | 709 | lazy_hcall3(LHCALL_SET_PTE, lguest_data.pgdir, addr, 0); |
645 | } | 710 | } |
646 | 711 | ||
647 | /* This is what happens after the Guest has removed a large number of entries. | 712 | /* |
713 | * This is what happens after the Guest has removed a large number of entries. | ||
648 | * This tells the Host that any of the page table entries for userspace might | 714 | * This tells the Host that any of the page table entries for userspace might |
649 | * have changed, ie. virtual addresses below PAGE_OFFSET. */ | 715 | * have changed, ie. virtual addresses below PAGE_OFFSET. |
716 | */ | ||
650 | static void lguest_flush_tlb_user(void) | 717 | static void lguest_flush_tlb_user(void) |
651 | { | 718 | { |
652 | lazy_hcall1(LHCALL_FLUSH_TLB, 0); | 719 | lazy_hcall1(LHCALL_FLUSH_TLB, 0); |
653 | } | 720 | } |
654 | 721 | ||
655 | /* This is called when the kernel page tables have changed. That's not very | 722 | /* |
723 | * This is called when the kernel page tables have changed. That's not very | ||
656 | * common (unless the Guest is using highmem, which makes the Guest extremely | 724 | * common (unless the Guest is using highmem, which makes the Guest extremely |
657 | * slow), so it's worth separating this from the user flushing above. */ | 725 | * slow), so it's worth separating this from the user flushing above. |
726 | */ | ||
658 | static void lguest_flush_tlb_kernel(void) | 727 | static void lguest_flush_tlb_kernel(void) |
659 | { | 728 | { |
660 | lazy_hcall1(LHCALL_FLUSH_TLB, 1); | 729 | lazy_hcall1(LHCALL_FLUSH_TLB, 1); |
@@ -691,23 +760,27 @@ static struct irq_chip lguest_irq_controller = { | |||
691 | .unmask = enable_lguest_irq, | 760 | .unmask = enable_lguest_irq, |
692 | }; | 761 | }; |
693 | 762 | ||
694 | /* This sets up the Interrupt Descriptor Table (IDT) entry for each hardware | 763 | /* |
764 | * This sets up the Interrupt Descriptor Table (IDT) entry for each hardware | ||
695 | * interrupt (except 128, which is used for system calls), and then tells the | 765 | * interrupt (except 128, which is used for system calls), and then tells the |
696 | * Linux infrastructure that each interrupt is controlled by our level-based | 766 | * Linux infrastructure that each interrupt is controlled by our level-based |
697 | * lguest interrupt controller. */ | 767 | * lguest interrupt controller. |
768 | */ | ||
698 | static void __init lguest_init_IRQ(void) | 769 | static void __init lguest_init_IRQ(void) |
699 | { | 770 | { |
700 | unsigned int i; | 771 | unsigned int i; |
701 | 772 | ||
702 | for (i = FIRST_EXTERNAL_VECTOR; i < NR_VECTORS; i++) { | 773 | for (i = FIRST_EXTERNAL_VECTOR; i < NR_VECTORS; i++) { |
703 | /* Some systems map "vectors" to interrupts weirdly. Lguest has | 774 | /* Some systems map "vectors" to interrupts weirdly. Not us! */ |
704 | * a straightforward 1 to 1 mapping, so force that here. */ | ||
705 | __get_cpu_var(vector_irq)[i] = i - FIRST_EXTERNAL_VECTOR; | 775 | __get_cpu_var(vector_irq)[i] = i - FIRST_EXTERNAL_VECTOR; |
706 | if (i != SYSCALL_VECTOR) | 776 | if (i != SYSCALL_VECTOR) |
707 | set_intr_gate(i, interrupt[i - FIRST_EXTERNAL_VECTOR]); | 777 | set_intr_gate(i, interrupt[i - FIRST_EXTERNAL_VECTOR]); |
708 | } | 778 | } |
709 | /* This call is required to set up for 4k stacks, where we have | 779 | |
710 | * separate stacks for hard and soft interrupts. */ | 780 | /* |
781 | * This call is required to set up for 4k stacks, where we have | ||
782 | * separate stacks for hard and soft interrupts. | ||
783 | */ | ||
711 | irq_ctx_init(smp_processor_id()); | 784 | irq_ctx_init(smp_processor_id()); |
712 | } | 785 | } |
713 | 786 | ||
@@ -729,31 +802,39 @@ static unsigned long lguest_get_wallclock(void) | |||
729 | return lguest_data.time.tv_sec; | 802 | return lguest_data.time.tv_sec; |
730 | } | 803 | } |
731 | 804 | ||
732 | /* The TSC is an Intel thing called the Time Stamp Counter. The Host tells us | 805 | /* |
806 | * The TSC is an Intel thing called the Time Stamp Counter. The Host tells us | ||
733 | * what speed it runs at, or 0 if it's unusable as a reliable clock source. | 807 | * what speed it runs at, or 0 if it's unusable as a reliable clock source. |
734 | * This matches what we want here: if we return 0 from this function, the x86 | 808 | * This matches what we want here: if we return 0 from this function, the x86 |
735 | * TSC clock will give up and not register itself. */ | 809 | * TSC clock will give up and not register itself. |
810 | */ | ||
736 | static unsigned long lguest_tsc_khz(void) | 811 | static unsigned long lguest_tsc_khz(void) |
737 | { | 812 | { |
738 | return lguest_data.tsc_khz; | 813 | return lguest_data.tsc_khz; |
739 | } | 814 | } |
740 | 815 | ||
741 | /* If we can't use the TSC, the kernel falls back to our lower-priority | 816 | /* |
742 | * "lguest_clock", where we read the time value given to us by the Host. */ | 817 | * If we can't use the TSC, the kernel falls back to our lower-priority |
818 | * "lguest_clock", where we read the time value given to us by the Host. | ||
819 | */ | ||
743 | static cycle_t lguest_clock_read(struct clocksource *cs) | 820 | static cycle_t lguest_clock_read(struct clocksource *cs) |
744 | { | 821 | { |
745 | unsigned long sec, nsec; | 822 | unsigned long sec, nsec; |
746 | 823 | ||
747 | /* Since the time is in two parts (seconds and nanoseconds), we risk | 824 | /* |
825 | * Since the time is in two parts (seconds and nanoseconds), we risk | ||
748 | * reading it just as it's changing from 99 & 0.999999999 to 100 and 0, | 826 | * reading it just as it's changing from 99 & 0.999999999 to 100 and 0, |
749 | * and getting 99 and 0. As Linux tends to come apart under the stress | 827 | * and getting 99 and 0. As Linux tends to come apart under the stress |
750 | * of time travel, we must be careful: */ | 828 | * of time travel, we must be careful: |
829 | */ | ||
751 | do { | 830 | do { |
752 | /* First we read the seconds part. */ | 831 | /* First we read the seconds part. */ |
753 | sec = lguest_data.time.tv_sec; | 832 | sec = lguest_data.time.tv_sec; |
754 | /* This read memory barrier tells the compiler and the CPU that | 833 | /* |
834 | * This read memory barrier tells the compiler and the CPU that | ||
755 | * this can't be reordered: we have to complete the above | 835 | * this can't be reordered: we have to complete the above |
756 | * before going on. */ | 836 | * before going on. |
837 | */ | ||
757 | rmb(); | 838 | rmb(); |
758 | /* Now we read the nanoseconds part. */ | 839 | /* Now we read the nanoseconds part. */ |
759 | nsec = lguest_data.time.tv_nsec; | 840 | nsec = lguest_data.time.tv_nsec; |
@@ -777,9 +858,11 @@ static struct clocksource lguest_clock = { | |||
777 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, | 858 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
778 | }; | 859 | }; |
779 | 860 | ||
780 | /* We also need a "struct clock_event_device": Linux asks us to set it to go | 861 | /* |
862 | * We also need a "struct clock_event_device": Linux asks us to set it to go | ||
781 | * off some time in the future. Actually, James Morris figured all this out, I | 863 | * off some time in the future. Actually, James Morris figured all this out, I |
782 | * just applied the patch. */ | 864 | * just applied the patch. |
865 | */ | ||
783 | static int lguest_clockevent_set_next_event(unsigned long delta, | 866 | static int lguest_clockevent_set_next_event(unsigned long delta, |
784 | struct clock_event_device *evt) | 867 | struct clock_event_device *evt) |
785 | { | 868 | { |
@@ -829,8 +912,10 @@ static struct clock_event_device lguest_clockevent = { | |||
829 | .max_delta_ns = LG_CLOCK_MAX_DELTA, | 912 | .max_delta_ns = LG_CLOCK_MAX_DELTA, |
830 | }; | 913 | }; |
831 | 914 | ||
832 | /* This is the Guest timer interrupt handler (hardware interrupt 0). We just | 915 | /* |
833 | * call the clockevent infrastructure and it does whatever needs doing. */ | 916 | * This is the Guest timer interrupt handler (hardware interrupt 0). We just |
917 | * call the clockevent infrastructure and it does whatever needs doing. | ||
918 | */ | ||
834 | static void lguest_time_irq(unsigned int irq, struct irq_desc *desc) | 919 | static void lguest_time_irq(unsigned int irq, struct irq_desc *desc) |
835 | { | 920 | { |
836 | unsigned long flags; | 921 | unsigned long flags; |
@@ -841,10 +926,12 @@ static void lguest_time_irq(unsigned int irq, struct irq_desc *desc) | |||
841 | local_irq_restore(flags); | 926 | local_irq_restore(flags); |
842 | } | 927 | } |
843 | 928 | ||
844 | /* At some point in the boot process, we get asked to set up our timing | 929 | /* |
930 | * At some point in the boot process, we get asked to set up our timing | ||
845 | * infrastructure. The kernel doesn't expect timer interrupts before this, but | 931 | * infrastructure. The kernel doesn't expect timer interrupts before this, but |
846 | * we cleverly initialized the "blocked_interrupts" field of "struct | 932 | * we cleverly initialized the "blocked_interrupts" field of "struct |
847 | * lguest_data" so that timer interrupts were blocked until now. */ | 933 | * lguest_data" so that timer interrupts were blocked until now. |
934 | */ | ||
848 | static void lguest_time_init(void) | 935 | static void lguest_time_init(void) |
849 | { | 936 | { |
850 | /* Set up the timer interrupt (0) to go to our simple timer routine */ | 937 | /* Set up the timer interrupt (0) to go to our simple timer routine */ |
@@ -868,14 +955,16 @@ static void lguest_time_init(void) | |||
868 | * to work. They're pretty simple. | 955 | * to work. They're pretty simple. |
869 | */ | 956 | */ |
870 | 957 | ||
871 | /* The Guest needs to tell the Host what stack it expects traps to use. For | 958 | /* |
959 | * The Guest needs to tell the Host what stack it expects traps to use. For | ||
872 | * native hardware, this is part of the Task State Segment mentioned above in | 960 | * native hardware, this is part of the Task State Segment mentioned above in |
873 | * lguest_load_tr_desc(), but to help hypervisors there's this special call. | 961 | * lguest_load_tr_desc(), but to help hypervisors there's this special call. |
874 | * | 962 | * |
875 | * We tell the Host the segment we want to use (__KERNEL_DS is the kernel data | 963 | * We tell the Host the segment we want to use (__KERNEL_DS is the kernel data |
876 | * segment), the privilege level (we're privilege level 1, the Host is 0 and | 964 | * segment), the privilege level (we're privilege level 1, the Host is 0 and |
877 | * will not tolerate us trying to use that), the stack pointer, and the number | 965 | * will not tolerate us trying to use that), the stack pointer, and the number |
878 | * of pages in the stack. */ | 966 | * of pages in the stack. |
967 | */ | ||
879 | static void lguest_load_sp0(struct tss_struct *tss, | 968 | static void lguest_load_sp0(struct tss_struct *tss, |
880 | struct thread_struct *thread) | 969 | struct thread_struct *thread) |
881 | { | 970 | { |
@@ -889,7 +978,8 @@ static void lguest_set_debugreg(int regno, unsigned long value) | |||
889 | /* FIXME: Implement */ | 978 | /* FIXME: Implement */ |
890 | } | 979 | } |
891 | 980 | ||
892 | /* There are times when the kernel wants to make sure that no memory writes are | 981 | /* |
982 | * There are times when the kernel wants to make sure that no memory writes are | ||
893 | * caught in the cache (that they've all reached real hardware devices). This | 983 | * caught in the cache (that they've all reached real hardware devices). This |
894 | * doesn't matter for the Guest which has virtual hardware. | 984 | * doesn't matter for the Guest which has virtual hardware. |
895 | * | 985 | * |
@@ -903,11 +993,13 @@ static void lguest_wbinvd(void) | |||
903 | { | 993 | { |
904 | } | 994 | } |
905 | 995 | ||
906 | /* If the Guest expects to have an Advanced Programmable Interrupt Controller, | 996 | /* |
997 | * If the Guest expects to have an Advanced Programmable Interrupt Controller, | ||
907 | * we play dumb by ignoring writes and returning 0 for reads. So it's no | 998 | * we play dumb by ignoring writes and returning 0 for reads. So it's no |
908 | * longer Programmable nor Controlling anything, and I don't think 8 lines of | 999 | * longer Programmable nor Controlling anything, and I don't think 8 lines of |
909 | * code qualifies for Advanced. It will also never interrupt anything. It | 1000 | * code qualifies for Advanced. It will also never interrupt anything. It |
910 | * does, however, allow us to get through the Linux boot code. */ | 1001 | * does, however, allow us to get through the Linux boot code. |
1002 | */ | ||
911 | #ifdef CONFIG_X86_LOCAL_APIC | 1003 | #ifdef CONFIG_X86_LOCAL_APIC |
912 | static void lguest_apic_write(u32 reg, u32 v) | 1004 | static void lguest_apic_write(u32 reg, u32 v) |
913 | { | 1005 | { |
@@ -956,11 +1048,13 @@ static void lguest_safe_halt(void) | |||
956 | kvm_hypercall0(LHCALL_HALT); | 1048 | kvm_hypercall0(LHCALL_HALT); |
957 | } | 1049 | } |
958 | 1050 | ||
959 | /* The SHUTDOWN hypercall takes a string to describe what's happening, and | 1051 | /* |
1052 | * The SHUTDOWN hypercall takes a string to describe what's happening, and | ||
960 | * an argument which says whether this to restart (reboot) the Guest or not. | 1053 | * an argument which says whether this to restart (reboot) the Guest or not. |
961 | * | 1054 | * |
962 | * Note that the Host always prefers that the Guest speak in physical addresses | 1055 | * Note that the Host always prefers that the Guest speak in physical addresses |
963 | * rather than virtual addresses, so we use __pa() here. */ | 1056 | * rather than virtual addresses, so we use __pa() here. |
1057 | */ | ||
964 | static void lguest_power_off(void) | 1058 | static void lguest_power_off(void) |
965 | { | 1059 | { |
966 | kvm_hypercall2(LHCALL_SHUTDOWN, __pa("Power down"), | 1060 | kvm_hypercall2(LHCALL_SHUTDOWN, __pa("Power down"), |
@@ -991,8 +1085,10 @@ static __init char *lguest_memory_setup(void) | |||
991 | * nice to move it back to lguest_init. Patch welcome... */ | 1085 | * nice to move it back to lguest_init. Patch welcome... */ |
992 | atomic_notifier_chain_register(&panic_notifier_list, &paniced); | 1086 | atomic_notifier_chain_register(&panic_notifier_list, &paniced); |
993 | 1087 | ||
994 | /* The Linux bootloader header contains an "e820" memory map: the | 1088 | /* |
995 | * Launcher populated the first entry with our memory limit. */ | 1089 | *The Linux bootloader header contains an "e820" memory map: the |
1090 | * Launcher populated the first entry with our memory limit. | ||
1091 | */ | ||
996 | e820_add_region(boot_params.e820_map[0].addr, | 1092 | e820_add_region(boot_params.e820_map[0].addr, |
997 | boot_params.e820_map[0].size, | 1093 | boot_params.e820_map[0].size, |
998 | boot_params.e820_map[0].type); | 1094 | boot_params.e820_map[0].type); |
@@ -1001,16 +1097,17 @@ static __init char *lguest_memory_setup(void) | |||
1001 | return "LGUEST"; | 1097 | return "LGUEST"; |
1002 | } | 1098 | } |
1003 | 1099 | ||
1004 | /* We will eventually use the virtio console device to produce console output, | 1100 | /* |
1101 | * We will eventually use the virtio console device to produce console output, | ||
1005 | * but before that is set up we use LHCALL_NOTIFY on normal memory to produce | 1102 | * but before that is set up we use LHCALL_NOTIFY on normal memory to produce |
1006 | * console output. */ | 1103 | * console output. |
1104 | */ | ||
1007 | static __init int early_put_chars(u32 vtermno, const char *buf, int count) | 1105 | static __init int early_put_chars(u32 vtermno, const char *buf, int count) |
1008 | { | 1106 | { |
1009 | char scratch[17]; | 1107 | char scratch[17]; |
1010 | unsigned int len = count; | 1108 | unsigned int len = count; |
1011 | 1109 | ||
1012 | /* We use a nul-terminated string, so we have to make a copy. Icky, | 1110 | /* We use a nul-terminated string, so we make a copy. Icky, huh? */ |
1013 | * huh? */ | ||
1014 | if (len > sizeof(scratch) - 1) | 1111 | if (len > sizeof(scratch) - 1) |
1015 | len = sizeof(scratch) - 1; | 1112 | len = sizeof(scratch) - 1; |
1016 | scratch[len] = '\0'; | 1113 | scratch[len] = '\0'; |
@@ -1021,8 +1118,10 @@ static __init int early_put_chars(u32 vtermno, const char *buf, int count) | |||
1021 | return len; | 1118 | return len; |
1022 | } | 1119 | } |
1023 | 1120 | ||
1024 | /* Rebooting also tells the Host we're finished, but the RESTART flag tells the | 1121 | /* |
1025 | * Launcher to reboot us. */ | 1122 | * Rebooting also tells the Host we're finished, but the RESTART flag tells the |
1123 | * Launcher to reboot us. | ||
1124 | */ | ||
1026 | static void lguest_restart(char *reason) | 1125 | static void lguest_restart(char *reason) |
1027 | { | 1126 | { |
1028 | kvm_hypercall2(LHCALL_SHUTDOWN, __pa(reason), LGUEST_SHUTDOWN_RESTART); | 1127 | kvm_hypercall2(LHCALL_SHUTDOWN, __pa(reason), LGUEST_SHUTDOWN_RESTART); |
@@ -1049,7 +1148,8 @@ static void lguest_restart(char *reason) | |||
1049 | * fit comfortably. | 1148 | * fit comfortably. |
1050 | * | 1149 | * |
1051 | * First we need assembly templates of each of the patchable Guest operations, | 1150 | * First we need assembly templates of each of the patchable Guest operations, |
1052 | * and these are in i386_head.S. */ | 1151 | * and these are in i386_head.S. |
1152 | */ | ||
1053 | 1153 | ||
1054 | /*G:060 We construct a table from the assembler templates: */ | 1154 | /*G:060 We construct a table from the assembler templates: */ |
1055 | static const struct lguest_insns | 1155 | static const struct lguest_insns |
@@ -1060,9 +1160,11 @@ static const struct lguest_insns | |||
1060 | [PARAVIRT_PATCH(pv_irq_ops.save_fl)] = { lgstart_pushf, lgend_pushf }, | 1160 | [PARAVIRT_PATCH(pv_irq_ops.save_fl)] = { lgstart_pushf, lgend_pushf }, |
1061 | }; | 1161 | }; |
1062 | 1162 | ||
1063 | /* Now our patch routine is fairly simple (based on the native one in | 1163 | /* |
1164 | * Now our patch routine is fairly simple (based on the native one in | ||
1064 | * paravirt.c). If we have a replacement, we copy it in and return how much of | 1165 | * paravirt.c). If we have a replacement, we copy it in and return how much of |
1065 | * the available space we used. */ | 1166 | * the available space we used. |
1167 | */ | ||
1066 | static unsigned lguest_patch(u8 type, u16 clobber, void *ibuf, | 1168 | static unsigned lguest_patch(u8 type, u16 clobber, void *ibuf, |
1067 | unsigned long addr, unsigned len) | 1169 | unsigned long addr, unsigned len) |
1068 | { | 1170 | { |
@@ -1074,8 +1176,7 @@ static unsigned lguest_patch(u8 type, u16 clobber, void *ibuf, | |||
1074 | 1176 | ||
1075 | insn_len = lguest_insns[type].end - lguest_insns[type].start; | 1177 | insn_len = lguest_insns[type].end - lguest_insns[type].start; |
1076 | 1178 | ||
1077 | /* Similarly if we can't fit replacement (shouldn't happen, but let's | 1179 | /* Similarly if it can't fit (doesn't happen, but let's be thorough). */ |
1078 | * be thorough). */ | ||
1079 | if (len < insn_len) | 1180 | if (len < insn_len) |
1080 | return paravirt_patch_default(type, clobber, ibuf, addr, len); | 1181 | return paravirt_patch_default(type, clobber, ibuf, addr, len); |
1081 | 1182 | ||
@@ -1084,22 +1185,28 @@ static unsigned lguest_patch(u8 type, u16 clobber, void *ibuf, | |||
1084 | return insn_len; | 1185 | return insn_len; |
1085 | } | 1186 | } |
1086 | 1187 | ||
1087 | /*G:029 Once we get to lguest_init(), we know we're a Guest. The various | 1188 | /*G:029 |
1189 | * Once we get to lguest_init(), we know we're a Guest. The various | ||
1088 | * pv_ops structures in the kernel provide points for (almost) every routine we | 1190 | * pv_ops structures in the kernel provide points for (almost) every routine we |
1089 | * have to override to avoid privileged instructions. */ | 1191 | * have to override to avoid privileged instructions. |
1192 | */ | ||
1090 | __init void lguest_init(void) | 1193 | __init void lguest_init(void) |
1091 | { | 1194 | { |
1092 | /* We're under lguest, paravirt is enabled, and we're running at | 1195 | /* We're under lguest. */ |
1093 | * privilege level 1, not 0 as normal. */ | ||
1094 | pv_info.name = "lguest"; | 1196 | pv_info.name = "lguest"; |
1197 | /* Paravirt is enabled. */ | ||
1095 | pv_info.paravirt_enabled = 1; | 1198 | pv_info.paravirt_enabled = 1; |
1199 | /* We're running at privilege level 1, not 0 as normal. */ | ||
1096 | pv_info.kernel_rpl = 1; | 1200 | pv_info.kernel_rpl = 1; |
1201 | /* Everyone except Xen runs with this set. */ | ||
1097 | pv_info.shared_kernel_pmd = 1; | 1202 | pv_info.shared_kernel_pmd = 1; |
1098 | 1203 | ||
1099 | /* We set up all the lguest overrides for sensitive operations. These | 1204 | /* |
1100 | * are detailed with the operations themselves. */ | 1205 | * We set up all the lguest overrides for sensitive operations. These |
1206 | * are detailed with the operations themselves. | ||
1207 | */ | ||
1101 | 1208 | ||
1102 | /* interrupt-related operations */ | 1209 | /* Interrupt-related operations */ |
1103 | pv_irq_ops.init_IRQ = lguest_init_IRQ; | 1210 | pv_irq_ops.init_IRQ = lguest_init_IRQ; |
1104 | pv_irq_ops.save_fl = PV_CALLEE_SAVE(save_fl); | 1211 | pv_irq_ops.save_fl = PV_CALLEE_SAVE(save_fl); |
1105 | pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(lg_restore_fl); | 1212 | pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(lg_restore_fl); |
@@ -1107,11 +1214,11 @@ __init void lguest_init(void) | |||
1107 | pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(lg_irq_enable); | 1214 | pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(lg_irq_enable); |
1108 | pv_irq_ops.safe_halt = lguest_safe_halt; | 1215 | pv_irq_ops.safe_halt = lguest_safe_halt; |
1109 | 1216 | ||
1110 | /* init-time operations */ | 1217 | /* Setup operations */ |
1111 | pv_init_ops.memory_setup = lguest_memory_setup; | 1218 | pv_init_ops.memory_setup = lguest_memory_setup; |
1112 | pv_init_ops.patch = lguest_patch; | 1219 | pv_init_ops.patch = lguest_patch; |
1113 | 1220 | ||
1114 | /* Intercepts of various cpu instructions */ | 1221 | /* Intercepts of various CPU instructions */ |
1115 | pv_cpu_ops.load_gdt = lguest_load_gdt; | 1222 | pv_cpu_ops.load_gdt = lguest_load_gdt; |
1116 | pv_cpu_ops.cpuid = lguest_cpuid; | 1223 | pv_cpu_ops.cpuid = lguest_cpuid; |
1117 | pv_cpu_ops.load_idt = lguest_load_idt; | 1224 | pv_cpu_ops.load_idt = lguest_load_idt; |
@@ -1132,7 +1239,7 @@ __init void lguest_init(void) | |||
1132 | pv_cpu_ops.start_context_switch = paravirt_start_context_switch; | 1239 | pv_cpu_ops.start_context_switch = paravirt_start_context_switch; |
1133 | pv_cpu_ops.end_context_switch = lguest_end_context_switch; | 1240 | pv_cpu_ops.end_context_switch = lguest_end_context_switch; |
1134 | 1241 | ||
1135 | /* pagetable management */ | 1242 | /* Pagetable management */ |
1136 | pv_mmu_ops.write_cr3 = lguest_write_cr3; | 1243 | pv_mmu_ops.write_cr3 = lguest_write_cr3; |
1137 | pv_mmu_ops.flush_tlb_user = lguest_flush_tlb_user; | 1244 | pv_mmu_ops.flush_tlb_user = lguest_flush_tlb_user; |
1138 | pv_mmu_ops.flush_tlb_single = lguest_flush_tlb_single; | 1245 | pv_mmu_ops.flush_tlb_single = lguest_flush_tlb_single; |
@@ -1154,54 +1261,71 @@ __init void lguest_init(void) | |||
1154 | pv_mmu_ops.pte_update_defer = lguest_pte_update; | 1261 | pv_mmu_ops.pte_update_defer = lguest_pte_update; |
1155 | 1262 | ||
1156 | #ifdef CONFIG_X86_LOCAL_APIC | 1263 | #ifdef CONFIG_X86_LOCAL_APIC |
1157 | /* apic read/write intercepts */ | 1264 | /* APIC read/write intercepts */ |
1158 | set_lguest_basic_apic_ops(); | 1265 | set_lguest_basic_apic_ops(); |
1159 | #endif | 1266 | #endif |
1160 | 1267 | ||
1161 | /* time operations */ | 1268 | /* Time operations */ |
1162 | pv_time_ops.get_wallclock = lguest_get_wallclock; | 1269 | pv_time_ops.get_wallclock = lguest_get_wallclock; |
1163 | pv_time_ops.time_init = lguest_time_init; | 1270 | pv_time_ops.time_init = lguest_time_init; |
1164 | pv_time_ops.get_tsc_khz = lguest_tsc_khz; | 1271 | pv_time_ops.get_tsc_khz = lguest_tsc_khz; |
1165 | 1272 | ||
1166 | /* Now is a good time to look at the implementations of these functions | 1273 | /* |
1167 | * before returning to the rest of lguest_init(). */ | 1274 | * Now is a good time to look at the implementations of these functions |
1275 | * before returning to the rest of lguest_init(). | ||
1276 | */ | ||
1168 | 1277 | ||
1169 | /*G:070 Now we've seen all the paravirt_ops, we return to | 1278 | /*G:070 |
1279 | * Now we've seen all the paravirt_ops, we return to | ||
1170 | * lguest_init() where the rest of the fairly chaotic boot setup | 1280 | * lguest_init() where the rest of the fairly chaotic boot setup |
1171 | * occurs. */ | 1281 | * occurs. |
1282 | */ | ||
1172 | 1283 | ||
1173 | /* The stack protector is a weird thing where gcc places a canary | 1284 | /* |
1285 | * The stack protector is a weird thing where gcc places a canary | ||
1174 | * value on the stack and then checks it on return. This file is | 1286 | * value on the stack and then checks it on return. This file is |
1175 | * compiled with -fno-stack-protector it, so we got this far without | 1287 | * compiled with -fno-stack-protector it, so we got this far without |
1176 | * problems. The value of the canary is kept at offset 20 from the | 1288 | * problems. The value of the canary is kept at offset 20 from the |
1177 | * %gs register, so we need to set that up before calling C functions | 1289 | * %gs register, so we need to set that up before calling C functions |
1178 | * in other files. */ | 1290 | * in other files. |
1291 | */ | ||
1179 | setup_stack_canary_segment(0); | 1292 | setup_stack_canary_segment(0); |
1180 | /* We could just call load_stack_canary_segment(), but we might as | 1293 | |
1181 | * call switch_to_new_gdt() which loads the whole table and sets up | 1294 | /* |
1182 | * the per-cpu segment descriptor register %fs as well. */ | 1295 | * We could just call load_stack_canary_segment(), but we might as well |
1296 | * call switch_to_new_gdt() which loads the whole table and sets up the | ||
1297 | * per-cpu segment descriptor register %fs as well. | ||
1298 | */ | ||
1183 | switch_to_new_gdt(0); | 1299 | switch_to_new_gdt(0); |
1184 | 1300 | ||
1185 | /* As described in head_32.S, we map the first 128M of memory. */ | 1301 | /* As described in head_32.S, we map the first 128M of memory. */ |
1186 | max_pfn_mapped = (128*1024*1024) >> PAGE_SHIFT; | 1302 | max_pfn_mapped = (128*1024*1024) >> PAGE_SHIFT; |
1187 | 1303 | ||
1188 | /* The Host<->Guest Switcher lives at the top of our address space, and | 1304 | /* |
1305 | * The Host<->Guest Switcher lives at the top of our address space, and | ||
1189 | * the Host told us how big it is when we made LGUEST_INIT hypercall: | 1306 | * the Host told us how big it is when we made LGUEST_INIT hypercall: |
1190 | * it put the answer in lguest_data.reserve_mem */ | 1307 | * it put the answer in lguest_data.reserve_mem |
1308 | */ | ||
1191 | reserve_top_address(lguest_data.reserve_mem); | 1309 | reserve_top_address(lguest_data.reserve_mem); |
1192 | 1310 | ||
1193 | /* If we don't initialize the lock dependency checker now, it crashes | 1311 | /* |
1194 | * paravirt_disable_iospace. */ | 1312 | * If we don't initialize the lock dependency checker now, it crashes |
1313 | * paravirt_disable_iospace. | ||
1314 | */ | ||
1195 | lockdep_init(); | 1315 | lockdep_init(); |
1196 | 1316 | ||
1197 | /* The IDE code spends about 3 seconds probing for disks: if we reserve | 1317 | /* |
1318 | * The IDE code spends about 3 seconds probing for disks: if we reserve | ||
1198 | * all the I/O ports up front it can't get them and so doesn't probe. | 1319 | * all the I/O ports up front it can't get them and so doesn't probe. |
1199 | * Other device drivers are similar (but less severe). This cuts the | 1320 | * Other device drivers are similar (but less severe). This cuts the |
1200 | * kernel boot time on my machine from 4.1 seconds to 0.45 seconds. */ | 1321 | * kernel boot time on my machine from 4.1 seconds to 0.45 seconds. |
1322 | */ | ||
1201 | paravirt_disable_iospace(); | 1323 | paravirt_disable_iospace(); |
1202 | 1324 | ||
1203 | /* This is messy CPU setup stuff which the native boot code does before | 1325 | /* |
1204 | * start_kernel, so we have to do, too: */ | 1326 | * This is messy CPU setup stuff which the native boot code does before |
1327 | * start_kernel, so we have to do, too: | ||
1328 | */ | ||
1205 | cpu_detect(&new_cpu_data); | 1329 | cpu_detect(&new_cpu_data); |
1206 | /* head.S usually sets up the first capability word, so do it here. */ | 1330 | /* head.S usually sets up the first capability word, so do it here. */ |
1207 | new_cpu_data.x86_capability[0] = cpuid_edx(1); | 1331 | new_cpu_data.x86_capability[0] = cpuid_edx(1); |
@@ -1218,22 +1342,28 @@ __init void lguest_init(void) | |||
1218 | acpi_ht = 0; | 1342 | acpi_ht = 0; |
1219 | #endif | 1343 | #endif |
1220 | 1344 | ||
1221 | /* We set the preferred console to "hvc". This is the "hypervisor | 1345 | /* |
1346 | * We set the preferred console to "hvc". This is the "hypervisor | ||
1222 | * virtual console" driver written by the PowerPC people, which we also | 1347 | * virtual console" driver written by the PowerPC people, which we also |
1223 | * adapted for lguest's use. */ | 1348 | * adapted for lguest's use. |
1349 | */ | ||
1224 | add_preferred_console("hvc", 0, NULL); | 1350 | add_preferred_console("hvc", 0, NULL); |
1225 | 1351 | ||
1226 | /* Register our very early console. */ | 1352 | /* Register our very early console. */ |
1227 | virtio_cons_early_init(early_put_chars); | 1353 | virtio_cons_early_init(early_put_chars); |
1228 | 1354 | ||
1229 | /* Last of all, we set the power management poweroff hook to point to | 1355 | /* |
1356 | * Last of all, we set the power management poweroff hook to point to | ||
1230 | * the Guest routine to power off, and the reboot hook to our restart | 1357 | * the Guest routine to power off, and the reboot hook to our restart |
1231 | * routine. */ | 1358 | * routine. |
1359 | */ | ||
1232 | pm_power_off = lguest_power_off; | 1360 | pm_power_off = lguest_power_off; |
1233 | machine_ops.restart = lguest_restart; | 1361 | machine_ops.restart = lguest_restart; |
1234 | 1362 | ||
1235 | /* Now we're set up, call i386_start_kernel() in head32.c and we proceed | 1363 | /* |
1236 | * to boot as normal. It never returns. */ | 1364 | * Now we're set up, call i386_start_kernel() in head32.c and we proceed |
1365 | * to boot as normal. It never returns. | ||
1366 | */ | ||
1237 | i386_start_kernel(); | 1367 | i386_start_kernel(); |
1238 | } | 1368 | } |
1239 | /* | 1369 | /* |
diff --git a/arch/x86/lguest/i386_head.S b/arch/x86/lguest/i386_head.S index a9c8cfe61cd4..db6aa95eb054 100644 --- a/arch/x86/lguest/i386_head.S +++ b/arch/x86/lguest/i386_head.S | |||
@@ -5,7 +5,8 @@ | |||
5 | #include <asm/thread_info.h> | 5 | #include <asm/thread_info.h> |
6 | #include <asm/processor-flags.h> | 6 | #include <asm/processor-flags.h> |
7 | 7 | ||
8 | /*G:020 Our story starts with the kernel booting into startup_32 in | 8 | /*G:020 |
9 | * Our story starts with the kernel booting into startup_32 in | ||
9 | * arch/x86/kernel/head_32.S. It expects a boot header, which is created by | 10 | * arch/x86/kernel/head_32.S. It expects a boot header, which is created by |
10 | * the bootloader (the Launcher in our case). | 11 | * the bootloader (the Launcher in our case). |
11 | * | 12 | * |
@@ -21,11 +22,14 @@ | |||
21 | * data without remembering to subtract __PAGE_OFFSET! | 22 | * data without remembering to subtract __PAGE_OFFSET! |
22 | * | 23 | * |
23 | * The .section line puts this code in .init.text so it will be discarded after | 24 | * The .section line puts this code in .init.text so it will be discarded after |
24 | * boot. */ | 25 | * boot. |
26 | */ | ||
25 | .section .init.text, "ax", @progbits | 27 | .section .init.text, "ax", @progbits |
26 | ENTRY(lguest_entry) | 28 | ENTRY(lguest_entry) |
27 | /* We make the "initialization" hypercall now to tell the Host about | 29 | /* |
28 | * us, and also find out where it put our page tables. */ | 30 | * We make the "initialization" hypercall now to tell the Host about |
31 | * us, and also find out where it put our page tables. | ||
32 | */ | ||
29 | movl $LHCALL_LGUEST_INIT, %eax | 33 | movl $LHCALL_LGUEST_INIT, %eax |
30 | movl $lguest_data - __PAGE_OFFSET, %ebx | 34 | movl $lguest_data - __PAGE_OFFSET, %ebx |
31 | .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */ | 35 | .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */ |
@@ -33,13 +37,14 @@ ENTRY(lguest_entry) | |||
33 | /* Set up the initial stack so we can run C code. */ | 37 | /* Set up the initial stack so we can run C code. */ |
34 | movl $(init_thread_union+THREAD_SIZE),%esp | 38 | movl $(init_thread_union+THREAD_SIZE),%esp |
35 | 39 | ||
36 | /* Jumps are relative, and we're running __PAGE_OFFSET too low at the | 40 | /* Jumps are relative: we're running __PAGE_OFFSET too low. */ |
37 | * moment. */ | ||
38 | jmp lguest_init+__PAGE_OFFSET | 41 | jmp lguest_init+__PAGE_OFFSET |
39 | 42 | ||
40 | /*G:055 We create a macro which puts the assembler code between lgstart_ and | 43 | /*G:055 |
41 | * lgend_ markers. These templates are put in the .text section: they can't be | 44 | * We create a macro which puts the assembler code between lgstart_ and lgend_ |
42 | * discarded after boot as we may need to patch modules, too. */ | 45 | * markers. These templates are put in the .text section: they can't be |
46 | * discarded after boot as we may need to patch modules, too. | ||
47 | */ | ||
43 | .text | 48 | .text |
44 | #define LGUEST_PATCH(name, insns...) \ | 49 | #define LGUEST_PATCH(name, insns...) \ |
45 | lgstart_##name: insns; lgend_##name:; \ | 50 | lgstart_##name: insns; lgend_##name:; \ |
@@ -48,58 +53,74 @@ ENTRY(lguest_entry) | |||
48 | LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled) | 53 | LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled) |
49 | LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax) | 54 | LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax) |
50 | 55 | ||
51 | /*G:033 But using those wrappers is inefficient (we'll see why that doesn't | 56 | /*G:033 |
52 | * matter for save_fl and irq_disable later). If we write our routines | 57 | * But using those wrappers is inefficient (we'll see why that doesn't matter |
53 | * carefully in assembler, we can avoid clobbering any registers and avoid | 58 | * for save_fl and irq_disable later). If we write our routines carefully in |
54 | * jumping through the wrapper functions. | 59 | * assembler, we can avoid clobbering any registers and avoid jumping through |
60 | * the wrapper functions. | ||
55 | * | 61 | * |
56 | * I skipped over our first piece of assembler, but this one is worth studying | 62 | * I skipped over our first piece of assembler, but this one is worth studying |
57 | * in a bit more detail so I'll describe in easy stages. First, the routine | 63 | * in a bit more detail so I'll describe in easy stages. First, the routine to |
58 | * to enable interrupts: */ | 64 | * enable interrupts: |
65 | */ | ||
59 | ENTRY(lg_irq_enable) | 66 | ENTRY(lg_irq_enable) |
60 | /* The reverse of irq_disable, this sets lguest_data.irq_enabled to | 67 | /* |
61 | * X86_EFLAGS_IF (ie. "Interrupts enabled"). */ | 68 | * The reverse of irq_disable, this sets lguest_data.irq_enabled to |
69 | * X86_EFLAGS_IF (ie. "Interrupts enabled"). | ||
70 | */ | ||
62 | movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled | 71 | movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled |
63 | /* But now we need to check if the Host wants to know: there might have | 72 | /* |
73 | * But now we need to check if the Host wants to know: there might have | ||
64 | * been interrupts waiting to be delivered, in which case it will have | 74 | * been interrupts waiting to be delivered, in which case it will have |
65 | * set lguest_data.irq_pending to X86_EFLAGS_IF. If it's not zero, we | 75 | * set lguest_data.irq_pending to X86_EFLAGS_IF. If it's not zero, we |
66 | * jump to send_interrupts, otherwise we're done. */ | 76 | * jump to send_interrupts, otherwise we're done. |
77 | */ | ||
67 | testl $0, lguest_data+LGUEST_DATA_irq_pending | 78 | testl $0, lguest_data+LGUEST_DATA_irq_pending |
68 | jnz send_interrupts | 79 | jnz send_interrupts |
69 | /* One cool thing about x86 is that you can do many things without using | 80 | /* |
81 | * One cool thing about x86 is that you can do many things without using | ||
70 | * a register. In this case, the normal path hasn't needed to save or | 82 | * a register. In this case, the normal path hasn't needed to save or |
71 | * restore any registers at all! */ | 83 | * restore any registers at all! |
84 | */ | ||
72 | ret | 85 | ret |
73 | send_interrupts: | 86 | send_interrupts: |
74 | /* OK, now we need a register: eax is used for the hypercall number, | 87 | /* |
88 | * OK, now we need a register: eax is used for the hypercall number, | ||
75 | * which is LHCALL_SEND_INTERRUPTS. | 89 | * which is LHCALL_SEND_INTERRUPTS. |
76 | * | 90 | * |
77 | * We used not to bother with this pending detection at all, which was | 91 | * We used not to bother with this pending detection at all, which was |
78 | * much simpler. Sooner or later the Host would realize it had to | 92 | * much simpler. Sooner or later the Host would realize it had to |
79 | * send us an interrupt. But that turns out to make performance 7 | 93 | * send us an interrupt. But that turns out to make performance 7 |
80 | * times worse on a simple tcp benchmark. So now we do this the hard | 94 | * times worse on a simple tcp benchmark. So now we do this the hard |
81 | * way. */ | 95 | * way. |
96 | */ | ||
82 | pushl %eax | 97 | pushl %eax |
83 | movl $LHCALL_SEND_INTERRUPTS, %eax | 98 | movl $LHCALL_SEND_INTERRUPTS, %eax |
84 | /* This is a vmcall instruction (same thing that KVM uses). Older | 99 | /* |
100 | * This is a vmcall instruction (same thing that KVM uses). Older | ||
85 | * assembler versions might not know the "vmcall" instruction, so we | 101 | * assembler versions might not know the "vmcall" instruction, so we |
86 | * create one manually here. */ | 102 | * create one manually here. |
103 | */ | ||
87 | .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */ | 104 | .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */ |
88 | popl %eax | 105 | popl %eax |
89 | ret | 106 | ret |
90 | 107 | ||
91 | /* Finally, the "popf" or "restore flags" routine. The %eax register holds the | 108 | /* |
109 | * Finally, the "popf" or "restore flags" routine. The %eax register holds the | ||
92 | * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're | 110 | * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're |
93 | * enabling interrupts again, if it's 0 we're leaving them off. */ | 111 | * enabling interrupts again, if it's 0 we're leaving them off. |
112 | */ | ||
94 | ENTRY(lg_restore_fl) | 113 | ENTRY(lg_restore_fl) |
95 | /* This is just "lguest_data.irq_enabled = flags;" */ | 114 | /* This is just "lguest_data.irq_enabled = flags;" */ |
96 | movl %eax, lguest_data+LGUEST_DATA_irq_enabled | 115 | movl %eax, lguest_data+LGUEST_DATA_irq_enabled |
97 | /* Now, if the %eax value has enabled interrupts and | 116 | /* |
117 | * Now, if the %eax value has enabled interrupts and | ||
98 | * lguest_data.irq_pending is set, we want to tell the Host so it can | 118 | * lguest_data.irq_pending is set, we want to tell the Host so it can |
99 | * deliver any outstanding interrupts. Fortunately, both values will | 119 | * deliver any outstanding interrupts. Fortunately, both values will |
100 | * be X86_EFLAGS_IF (ie. 512) in that case, and the "testl" | 120 | * be X86_EFLAGS_IF (ie. 512) in that case, and the "testl" |
101 | * instruction will AND them together for us. If both are set, we | 121 | * instruction will AND them together for us. If both are set, we |
102 | * jump to send_interrupts. */ | 122 | * jump to send_interrupts. |
123 | */ | ||
103 | testl lguest_data+LGUEST_DATA_irq_pending, %eax | 124 | testl lguest_data+LGUEST_DATA_irq_pending, %eax |
104 | jnz send_interrupts | 125 | jnz send_interrupts |
105 | /* Again, the normal path has used no extra registers. Clever, huh? */ | 126 | /* Again, the normal path has used no extra registers. Clever, huh? */ |
@@ -109,22 +130,24 @@ ENTRY(lg_restore_fl) | |||
109 | .global lguest_noirq_start | 130 | .global lguest_noirq_start |
110 | .global lguest_noirq_end | 131 | .global lguest_noirq_end |
111 | 132 | ||
112 | /*M:004 When the Host reflects a trap or injects an interrupt into the Guest, | 133 | /*M:004 |
113 | * it sets the eflags interrupt bit on the stack based on | 134 | * When the Host reflects a trap or injects an interrupt into the Guest, it |
114 | * lguest_data.irq_enabled, so the Guest iret logic does the right thing when | 135 | * sets the eflags interrupt bit on the stack based on lguest_data.irq_enabled, |
115 | * restoring it. However, when the Host sets the Guest up for direct traps, | 136 | * so the Guest iret logic does the right thing when restoring it. However, |
116 | * such as system calls, the processor is the one to push eflags onto the | 137 | * when the Host sets the Guest up for direct traps, such as system calls, the |
117 | * stack, and the interrupt bit will be 1 (in reality, interrupts are always | 138 | * processor is the one to push eflags onto the stack, and the interrupt bit |
118 | * enabled in the Guest). | 139 | * will be 1 (in reality, interrupts are always enabled in the Guest). |
119 | * | 140 | * |
120 | * This turns out to be harmless: the only trap which should happen under Linux | 141 | * This turns out to be harmless: the only trap which should happen under Linux |
121 | * with interrupts disabled is Page Fault (due to our lazy mapping of vmalloc | 142 | * with interrupts disabled is Page Fault (due to our lazy mapping of vmalloc |
122 | * regions), which has to be reflected through the Host anyway. If another | 143 | * regions), which has to be reflected through the Host anyway. If another |
123 | * trap *does* go off when interrupts are disabled, the Guest will panic, and | 144 | * trap *does* go off when interrupts are disabled, the Guest will panic, and |
124 | * we'll never get to this iret! :*/ | 145 | * we'll never get to this iret! |
146 | :*/ | ||
125 | 147 | ||
126 | /*G:045 There is one final paravirt_op that the Guest implements, and glancing | 148 | /*G:045 |
127 | * at it you can see why I left it to last. It's *cool*! It's in *assembler*! | 149 | * There is one final paravirt_op that the Guest implements, and glancing at it |
150 | * you can see why I left it to last. It's *cool*! It's in *assembler*! | ||
128 | * | 151 | * |
129 | * The "iret" instruction is used to return from an interrupt or trap. The | 152 | * The "iret" instruction is used to return from an interrupt or trap. The |
130 | * stack looks like this: | 153 | * stack looks like this: |
@@ -148,15 +171,18 @@ ENTRY(lg_restore_fl) | |||
148 | * return to userspace or wherever. Our solution to this is to surround the | 171 | * return to userspace or wherever. Our solution to this is to surround the |
149 | * code with lguest_noirq_start: and lguest_noirq_end: labels. We tell the | 172 | * code with lguest_noirq_start: and lguest_noirq_end: labels. We tell the |
150 | * Host that it is *never* to interrupt us there, even if interrupts seem to be | 173 | * Host that it is *never* to interrupt us there, even if interrupts seem to be |
151 | * enabled. */ | 174 | * enabled. |
175 | */ | ||
152 | ENTRY(lguest_iret) | 176 | ENTRY(lguest_iret) |
153 | pushl %eax | 177 | pushl %eax |
154 | movl 12(%esp), %eax | 178 | movl 12(%esp), %eax |
155 | lguest_noirq_start: | 179 | lguest_noirq_start: |
156 | /* Note the %ss: segment prefix here. Normal data accesses use the | 180 | /* |
181 | * Note the %ss: segment prefix here. Normal data accesses use the | ||
157 | * "ds" segment, but that will have already been restored for whatever | 182 | * "ds" segment, but that will have already been restored for whatever |
158 | * we're returning to (such as userspace): we can't trust it. The %ss: | 183 | * we're returning to (such as userspace): we can't trust it. The %ss: |
159 | * prefix makes sure we use the stack segment, which is still valid. */ | 184 | * prefix makes sure we use the stack segment, which is still valid. |
185 | */ | ||
160 | movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled | 186 | movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled |
161 | popl %eax | 187 | popl %eax |
162 | iret | 188 | iret |
diff --git a/drivers/lguest/core.c b/drivers/lguest/core.c index a6974e9b8ebf..cd058bc903ff 100644 --- a/drivers/lguest/core.c +++ b/drivers/lguest/core.c | |||
@@ -1,6 +1,8 @@ | |||
1 | /*P:400 This contains run_guest() which actually calls into the Host<->Guest | 1 | /*P:400 |
2 | * This contains run_guest() which actually calls into the Host<->Guest | ||
2 | * Switcher and analyzes the return, such as determining if the Guest wants the | 3 | * Switcher and analyzes the return, such as determining if the Guest wants the |
3 | * Host to do something. This file also contains useful helper routines. :*/ | 4 | * Host to do something. This file also contains useful helper routines. |
5 | :*/ | ||
4 | #include <linux/module.h> | 6 | #include <linux/module.h> |
5 | #include <linux/stringify.h> | 7 | #include <linux/stringify.h> |
6 | #include <linux/stddef.h> | 8 | #include <linux/stddef.h> |
@@ -24,7 +26,8 @@ static struct page **switcher_page; | |||
24 | /* This One Big lock protects all inter-guest data structures. */ | 26 | /* This One Big lock protects all inter-guest data structures. */ |
25 | DEFINE_MUTEX(lguest_lock); | 27 | DEFINE_MUTEX(lguest_lock); |
26 | 28 | ||
27 | /*H:010 We need to set up the Switcher at a high virtual address. Remember the | 29 | /*H:010 |
30 | * We need to set up the Switcher at a high virtual address. Remember the | ||
28 | * Switcher is a few hundred bytes of assembler code which actually changes the | 31 | * Switcher is a few hundred bytes of assembler code which actually changes the |
29 | * CPU to run the Guest, and then changes back to the Host when a trap or | 32 | * CPU to run the Guest, and then changes back to the Host when a trap or |
30 | * interrupt happens. | 33 | * interrupt happens. |
@@ -33,7 +36,8 @@ DEFINE_MUTEX(lguest_lock); | |||
33 | * Host since it will be running as the switchover occurs. | 36 | * Host since it will be running as the switchover occurs. |
34 | * | 37 | * |
35 | * Trying to map memory at a particular address is an unusual thing to do, so | 38 | * Trying to map memory at a particular address is an unusual thing to do, so |
36 | * it's not a simple one-liner. */ | 39 | * it's not a simple one-liner. |
40 | */ | ||
37 | static __init int map_switcher(void) | 41 | static __init int map_switcher(void) |
38 | { | 42 | { |
39 | int i, err; | 43 | int i, err; |
@@ -47,8 +51,10 @@ static __init int map_switcher(void) | |||
47 | * easy. | 51 | * easy. |
48 | */ | 52 | */ |
49 | 53 | ||
50 | /* We allocate an array of struct page pointers. map_vm_area() wants | 54 | /* |
51 | * this, rather than just an array of pages. */ | 55 | * We allocate an array of struct page pointers. map_vm_area() wants |
56 | * this, rather than just an array of pages. | ||
57 | */ | ||
52 | switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES, | 58 | switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES, |
53 | GFP_KERNEL); | 59 | GFP_KERNEL); |
54 | if (!switcher_page) { | 60 | if (!switcher_page) { |
@@ -56,8 +62,10 @@ static __init int map_switcher(void) | |||
56 | goto out; | 62 | goto out; |
57 | } | 63 | } |
58 | 64 | ||
59 | /* Now we actually allocate the pages. The Guest will see these pages, | 65 | /* |
60 | * so we make sure they're zeroed. */ | 66 | * Now we actually allocate the pages. The Guest will see these pages, |
67 | * so we make sure they're zeroed. | ||
68 | */ | ||
61 | for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) { | 69 | for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) { |
62 | unsigned long addr = get_zeroed_page(GFP_KERNEL); | 70 | unsigned long addr = get_zeroed_page(GFP_KERNEL); |
63 | if (!addr) { | 71 | if (!addr) { |
@@ -67,19 +75,23 @@ static __init int map_switcher(void) | |||
67 | switcher_page[i] = virt_to_page(addr); | 75 | switcher_page[i] = virt_to_page(addr); |
68 | } | 76 | } |
69 | 77 | ||
70 | /* First we check that the Switcher won't overlap the fixmap area at | 78 | /* |
79 | * First we check that the Switcher won't overlap the fixmap area at | ||
71 | * the top of memory. It's currently nowhere near, but it could have | 80 | * the top of memory. It's currently nowhere near, but it could have |
72 | * very strange effects if it ever happened. */ | 81 | * very strange effects if it ever happened. |
82 | */ | ||
73 | if (SWITCHER_ADDR + (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE > FIXADDR_START){ | 83 | if (SWITCHER_ADDR + (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE > FIXADDR_START){ |
74 | err = -ENOMEM; | 84 | err = -ENOMEM; |
75 | printk("lguest: mapping switcher would thwack fixmap\n"); | 85 | printk("lguest: mapping switcher would thwack fixmap\n"); |
76 | goto free_pages; | 86 | goto free_pages; |
77 | } | 87 | } |
78 | 88 | ||
79 | /* Now we reserve the "virtual memory area" we want: 0xFFC00000 | 89 | /* |
90 | * Now we reserve the "virtual memory area" we want: 0xFFC00000 | ||
80 | * (SWITCHER_ADDR). We might not get it in theory, but in practice | 91 | * (SWITCHER_ADDR). We might not get it in theory, but in practice |
81 | * it's worked so far. The end address needs +1 because __get_vm_area | 92 | * it's worked so far. The end address needs +1 because __get_vm_area |
82 | * allocates an extra guard page, so we need space for that. */ | 93 | * allocates an extra guard page, so we need space for that. |
94 | */ | ||
83 | switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE, | 95 | switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE, |
84 | VM_ALLOC, SWITCHER_ADDR, SWITCHER_ADDR | 96 | VM_ALLOC, SWITCHER_ADDR, SWITCHER_ADDR |
85 | + (TOTAL_SWITCHER_PAGES+1) * PAGE_SIZE); | 97 | + (TOTAL_SWITCHER_PAGES+1) * PAGE_SIZE); |
@@ -89,11 +101,13 @@ static __init int map_switcher(void) | |||
89 | goto free_pages; | 101 | goto free_pages; |
90 | } | 102 | } |
91 | 103 | ||
92 | /* This code actually sets up the pages we've allocated to appear at | 104 | /* |
105 | * This code actually sets up the pages we've allocated to appear at | ||
93 | * SWITCHER_ADDR. map_vm_area() takes the vma we allocated above, the | 106 | * SWITCHER_ADDR. map_vm_area() takes the vma we allocated above, the |
94 | * kind of pages we're mapping (kernel pages), and a pointer to our | 107 | * kind of pages we're mapping (kernel pages), and a pointer to our |
95 | * array of struct pages. It increments that pointer, but we don't | 108 | * array of struct pages. It increments that pointer, but we don't |
96 | * care. */ | 109 | * care. |
110 | */ | ||
97 | pagep = switcher_page; | 111 | pagep = switcher_page; |
98 | err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, &pagep); | 112 | err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, &pagep); |
99 | if (err) { | 113 | if (err) { |
@@ -101,8 +115,10 @@ static __init int map_switcher(void) | |||
101 | goto free_vma; | 115 | goto free_vma; |
102 | } | 116 | } |
103 | 117 | ||
104 | /* Now the Switcher is mapped at the right address, we can't fail! | 118 | /* |
105 | * Copy in the compiled-in Switcher code (from <arch>_switcher.S). */ | 119 | * Now the Switcher is mapped at the right address, we can't fail! |
120 | * Copy in the compiled-in Switcher code (from <arch>_switcher.S). | ||
121 | */ | ||
106 | memcpy(switcher_vma->addr, start_switcher_text, | 122 | memcpy(switcher_vma->addr, start_switcher_text, |
107 | end_switcher_text - start_switcher_text); | 123 | end_switcher_text - start_switcher_text); |
108 | 124 | ||
@@ -124,8 +140,7 @@ out: | |||
124 | } | 140 | } |
125 | /*:*/ | 141 | /*:*/ |
126 | 142 | ||
127 | /* Cleaning up the mapping when the module is unloaded is almost... | 143 | /* Cleaning up the mapping when the module is unloaded is almost... too easy. */ |
128 | * too easy. */ | ||
129 | static void unmap_switcher(void) | 144 | static void unmap_switcher(void) |
130 | { | 145 | { |
131 | unsigned int i; | 146 | unsigned int i; |
@@ -151,16 +166,19 @@ static void unmap_switcher(void) | |||
151 | * But we can't trust the Guest: it might be trying to access the Launcher | 166 | * But we can't trust the Guest: it might be trying to access the Launcher |
152 | * code. We have to check that the range is below the pfn_limit the Launcher | 167 | * code. We have to check that the range is below the pfn_limit the Launcher |
153 | * gave us. We have to make sure that addr + len doesn't give us a false | 168 | * gave us. We have to make sure that addr + len doesn't give us a false |
154 | * positive by overflowing, too. */ | 169 | * positive by overflowing, too. |
170 | */ | ||
155 | bool lguest_address_ok(const struct lguest *lg, | 171 | bool lguest_address_ok(const struct lguest *lg, |
156 | unsigned long addr, unsigned long len) | 172 | unsigned long addr, unsigned long len) |
157 | { | 173 | { |
158 | return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr); | 174 | return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr); |
159 | } | 175 | } |
160 | 176 | ||
161 | /* This routine copies memory from the Guest. Here we can see how useful the | 177 | /* |
178 | * This routine copies memory from the Guest. Here we can see how useful the | ||
162 | * kill_lguest() routine we met in the Launcher can be: we return a random | 179 | * kill_lguest() routine we met in the Launcher can be: we return a random |
163 | * value (all zeroes) instead of needing to return an error. */ | 180 | * value (all zeroes) instead of needing to return an error. |
181 | */ | ||
164 | void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes) | 182 | void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes) |
165 | { | 183 | { |
166 | if (!lguest_address_ok(cpu->lg, addr, bytes) | 184 | if (!lguest_address_ok(cpu->lg, addr, bytes) |
@@ -181,9 +199,11 @@ void __lgwrite(struct lg_cpu *cpu, unsigned long addr, const void *b, | |||
181 | } | 199 | } |
182 | /*:*/ | 200 | /*:*/ |
183 | 201 | ||
184 | /*H:030 Let's jump straight to the the main loop which runs the Guest. | 202 | /*H:030 |
203 | * Let's jump straight to the the main loop which runs the Guest. | ||
185 | * Remember, this is called by the Launcher reading /dev/lguest, and we keep | 204 | * Remember, this is called by the Launcher reading /dev/lguest, and we keep |
186 | * going around and around until something interesting happens. */ | 205 | * going around and around until something interesting happens. |
206 | */ | ||
187 | int run_guest(struct lg_cpu *cpu, unsigned long __user *user) | 207 | int run_guest(struct lg_cpu *cpu, unsigned long __user *user) |
188 | { | 208 | { |
189 | /* We stop running once the Guest is dead. */ | 209 | /* We stop running once the Guest is dead. */ |
@@ -195,8 +215,10 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user) | |||
195 | if (cpu->hcall) | 215 | if (cpu->hcall) |
196 | do_hypercalls(cpu); | 216 | do_hypercalls(cpu); |
197 | 217 | ||
198 | /* It's possible the Guest did a NOTIFY hypercall to the | 218 | /* |
199 | * Launcher, in which case we return from the read() now. */ | 219 | * It's possible the Guest did a NOTIFY hypercall to the |
220 | * Launcher, in which case we return from the read() now. | ||
221 | */ | ||
200 | if (cpu->pending_notify) { | 222 | if (cpu->pending_notify) { |
201 | if (!send_notify_to_eventfd(cpu)) { | 223 | if (!send_notify_to_eventfd(cpu)) { |
202 | if (put_user(cpu->pending_notify, user)) | 224 | if (put_user(cpu->pending_notify, user)) |
@@ -209,29 +231,39 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user) | |||
209 | if (signal_pending(current)) | 231 | if (signal_pending(current)) |
210 | return -ERESTARTSYS; | 232 | return -ERESTARTSYS; |
211 | 233 | ||
212 | /* Check if there are any interrupts which can be delivered now: | 234 | /* |
235 | * Check if there are any interrupts which can be delivered now: | ||
213 | * if so, this sets up the hander to be executed when we next | 236 | * if so, this sets up the hander to be executed when we next |
214 | * run the Guest. */ | 237 | * run the Guest. |
238 | */ | ||
215 | irq = interrupt_pending(cpu, &more); | 239 | irq = interrupt_pending(cpu, &more); |
216 | if (irq < LGUEST_IRQS) | 240 | if (irq < LGUEST_IRQS) |
217 | try_deliver_interrupt(cpu, irq, more); | 241 | try_deliver_interrupt(cpu, irq, more); |
218 | 242 | ||
219 | /* All long-lived kernel loops need to check with this horrible | 243 | /* |
244 | * All long-lived kernel loops need to check with this horrible | ||
220 | * thing called the freezer. If the Host is trying to suspend, | 245 | * thing called the freezer. If the Host is trying to suspend, |
221 | * it stops us. */ | 246 | * it stops us. |
247 | */ | ||
222 | try_to_freeze(); | 248 | try_to_freeze(); |
223 | 249 | ||
224 | /* Just make absolutely sure the Guest is still alive. One of | 250 | /* |
225 | * those hypercalls could have been fatal, for example. */ | 251 | * Just make absolutely sure the Guest is still alive. One of |
252 | * those hypercalls could have been fatal, for example. | ||
253 | */ | ||
226 | if (cpu->lg->dead) | 254 | if (cpu->lg->dead) |
227 | break; | 255 | break; |
228 | 256 | ||
229 | /* If the Guest asked to be stopped, we sleep. The Guest's | 257 | /* |
230 | * clock timer will wake us. */ | 258 | * If the Guest asked to be stopped, we sleep. The Guest's |
259 | * clock timer will wake us. | ||
260 | */ | ||
231 | if (cpu->halted) { | 261 | if (cpu->halted) { |
232 | set_current_state(TASK_INTERRUPTIBLE); | 262 | set_current_state(TASK_INTERRUPTIBLE); |
233 | /* Just before we sleep, make sure no interrupt snuck in | 263 | /* |
234 | * which we should be doing. */ | 264 | * Just before we sleep, make sure no interrupt snuck in |
265 | * which we should be doing. | ||
266 | */ | ||
235 | if (interrupt_pending(cpu, &more) < LGUEST_IRQS) | 267 | if (interrupt_pending(cpu, &more) < LGUEST_IRQS) |
236 | set_current_state(TASK_RUNNING); | 268 | set_current_state(TASK_RUNNING); |
237 | else | 269 | else |
@@ -239,8 +271,10 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user) | |||
239 | continue; | 271 | continue; |
240 | } | 272 | } |
241 | 273 | ||
242 | /* OK, now we're ready to jump into the Guest. First we put up | 274 | /* |
243 | * the "Do Not Disturb" sign: */ | 275 | * OK, now we're ready to jump into the Guest. First we put up |
276 | * the "Do Not Disturb" sign: | ||
277 | */ | ||
244 | local_irq_disable(); | 278 | local_irq_disable(); |
245 | 279 | ||
246 | /* Actually run the Guest until something happens. */ | 280 | /* Actually run the Guest until something happens. */ |
@@ -327,8 +361,10 @@ static void __exit fini(void) | |||
327 | } | 361 | } |
328 | /*:*/ | 362 | /*:*/ |
329 | 363 | ||
330 | /* The Host side of lguest can be a module. This is a nice way for people to | 364 | /* |
331 | * play with it. */ | 365 | * The Host side of lguest can be a module. This is a nice way for people to |
366 | * play with it. | ||
367 | */ | ||
332 | module_init(init); | 368 | module_init(init); |
333 | module_exit(fini); | 369 | module_exit(fini); |
334 | MODULE_LICENSE("GPL"); | 370 | MODULE_LICENSE("GPL"); |
diff --git a/drivers/lguest/hypercalls.c b/drivers/lguest/hypercalls.c index c29ffa19cb74..787ab4bc09f0 100644 --- a/drivers/lguest/hypercalls.c +++ b/drivers/lguest/hypercalls.c | |||
@@ -1,8 +1,10 @@ | |||
1 | /*P:500 Just as userspace programs request kernel operations through a system | 1 | /*P:500 |
2 | * Just as userspace programs request kernel operations through a system | ||
2 | * call, the Guest requests Host operations through a "hypercall". You might | 3 | * call, the Guest requests Host operations through a "hypercall". You might |
3 | * notice this nomenclature doesn't really follow any logic, but the name has | 4 | * notice this nomenclature doesn't really follow any logic, but the name has |
4 | * been around for long enough that we're stuck with it. As you'd expect, this | 5 | * been around for long enough that we're stuck with it. As you'd expect, this |
5 | * code is basically a one big switch statement. :*/ | 6 | * code is basically a one big switch statement. |
7 | :*/ | ||
6 | 8 | ||
7 | /* Copyright (C) 2006 Rusty Russell IBM Corporation | 9 | /* Copyright (C) 2006 Rusty Russell IBM Corporation |
8 | 10 | ||
@@ -28,30 +30,41 @@ | |||
28 | #include <asm/pgtable.h> | 30 | #include <asm/pgtable.h> |
29 | #include "lg.h" | 31 | #include "lg.h" |
30 | 32 | ||
31 | /*H:120 This is the core hypercall routine: where the Guest gets what it wants. | 33 | /*H:120 |
32 | * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. */ | 34 | * This is the core hypercall routine: where the Guest gets what it wants. |
35 | * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. | ||
36 | */ | ||
33 | static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) | 37 | static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) |
34 | { | 38 | { |
35 | switch (args->arg0) { | 39 | switch (args->arg0) { |
36 | case LHCALL_FLUSH_ASYNC: | 40 | case LHCALL_FLUSH_ASYNC: |
37 | /* This call does nothing, except by breaking out of the Guest | 41 | /* |
38 | * it makes us process all the asynchronous hypercalls. */ | 42 | * This call does nothing, except by breaking out of the Guest |
43 | * it makes us process all the asynchronous hypercalls. | ||
44 | */ | ||
39 | break; | 45 | break; |
40 | case LHCALL_SEND_INTERRUPTS: | 46 | case LHCALL_SEND_INTERRUPTS: |
41 | /* This call does nothing too, but by breaking out of the Guest | 47 | /* |
42 | * it makes us process any pending interrupts. */ | 48 | * This call does nothing too, but by breaking out of the Guest |
49 | * it makes us process any pending interrupts. | ||
50 | */ | ||
43 | break; | 51 | break; |
44 | case LHCALL_LGUEST_INIT: | 52 | case LHCALL_LGUEST_INIT: |
45 | /* You can't get here unless you're already initialized. Don't | 53 | /* |
46 | * do that. */ | 54 | * You can't get here unless you're already initialized. Don't |
55 | * do that. | ||
56 | */ | ||
47 | kill_guest(cpu, "already have lguest_data"); | 57 | kill_guest(cpu, "already have lguest_data"); |
48 | break; | 58 | break; |
49 | case LHCALL_SHUTDOWN: { | 59 | case LHCALL_SHUTDOWN: { |
50 | /* Shutdown is such a trivial hypercall that we do it in four | ||
51 | * lines right here. */ | ||
52 | char msg[128]; | 60 | char msg[128]; |
53 | /* If the lgread fails, it will call kill_guest() itself; the | 61 | /* |
54 | * kill_guest() with the message will be ignored. */ | 62 | * Shutdown is such a trivial hypercall that we do it in four |
63 | * lines right here. | ||
64 | * | ||
65 | * If the lgread fails, it will call kill_guest() itself; the | ||
66 | * kill_guest() with the message will be ignored. | ||
67 | */ | ||
55 | __lgread(cpu, msg, args->arg1, sizeof(msg)); | 68 | __lgread(cpu, msg, args->arg1, sizeof(msg)); |
56 | msg[sizeof(msg)-1] = '\0'; | 69 | msg[sizeof(msg)-1] = '\0'; |
57 | kill_guest(cpu, "CRASH: %s", msg); | 70 | kill_guest(cpu, "CRASH: %s", msg); |
@@ -60,16 +73,17 @@ static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) | |||
60 | break; | 73 | break; |
61 | } | 74 | } |
62 | case LHCALL_FLUSH_TLB: | 75 | case LHCALL_FLUSH_TLB: |
63 | /* FLUSH_TLB comes in two flavors, depending on the | 76 | /* FLUSH_TLB comes in two flavors, depending on the argument: */ |
64 | * argument: */ | ||
65 | if (args->arg1) | 77 | if (args->arg1) |
66 | guest_pagetable_clear_all(cpu); | 78 | guest_pagetable_clear_all(cpu); |
67 | else | 79 | else |
68 | guest_pagetable_flush_user(cpu); | 80 | guest_pagetable_flush_user(cpu); |
69 | break; | 81 | break; |
70 | 82 | ||
71 | /* All these calls simply pass the arguments through to the right | 83 | /* |
72 | * routines. */ | 84 | * All these calls simply pass the arguments through to the right |
85 | * routines. | ||
86 | */ | ||
73 | case LHCALL_NEW_PGTABLE: | 87 | case LHCALL_NEW_PGTABLE: |
74 | guest_new_pagetable(cpu, args->arg1); | 88 | guest_new_pagetable(cpu, args->arg1); |
75 | break; | 89 | break; |
@@ -112,15 +126,16 @@ static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) | |||
112 | kill_guest(cpu, "Bad hypercall %li\n", args->arg0); | 126 | kill_guest(cpu, "Bad hypercall %li\n", args->arg0); |
113 | } | 127 | } |
114 | } | 128 | } |
115 | /*:*/ | ||
116 | 129 | ||
117 | /*H:124 Asynchronous hypercalls are easy: we just look in the array in the | 130 | /*H:124 |
131 | * Asynchronous hypercalls are easy: we just look in the array in the | ||
118 | * Guest's "struct lguest_data" to see if any new ones are marked "ready". | 132 | * Guest's "struct lguest_data" to see if any new ones are marked "ready". |
119 | * | 133 | * |
120 | * We are careful to do these in order: obviously we respect the order the | 134 | * We are careful to do these in order: obviously we respect the order the |
121 | * Guest put them in the ring, but we also promise the Guest that they will | 135 | * Guest put them in the ring, but we also promise the Guest that they will |
122 | * happen before any normal hypercall (which is why we check this before | 136 | * happen before any normal hypercall (which is why we check this before |
123 | * checking for a normal hcall). */ | 137 | * checking for a normal hcall). |
138 | */ | ||
124 | static void do_async_hcalls(struct lg_cpu *cpu) | 139 | static void do_async_hcalls(struct lg_cpu *cpu) |
125 | { | 140 | { |
126 | unsigned int i; | 141 | unsigned int i; |
@@ -133,22 +148,28 @@ static void do_async_hcalls(struct lg_cpu *cpu) | |||
133 | /* We process "struct lguest_data"s hcalls[] ring once. */ | 148 | /* We process "struct lguest_data"s hcalls[] ring once. */ |
134 | for (i = 0; i < ARRAY_SIZE(st); i++) { | 149 | for (i = 0; i < ARRAY_SIZE(st); i++) { |
135 | struct hcall_args args; | 150 | struct hcall_args args; |
136 | /* We remember where we were up to from last time. This makes | 151 | /* |
152 | * We remember where we were up to from last time. This makes | ||
137 | * sure that the hypercalls are done in the order the Guest | 153 | * sure that the hypercalls are done in the order the Guest |
138 | * places them in the ring. */ | 154 | * places them in the ring. |
155 | */ | ||
139 | unsigned int n = cpu->next_hcall; | 156 | unsigned int n = cpu->next_hcall; |
140 | 157 | ||
141 | /* 0xFF means there's no call here (yet). */ | 158 | /* 0xFF means there's no call here (yet). */ |
142 | if (st[n] == 0xFF) | 159 | if (st[n] == 0xFF) |
143 | break; | 160 | break; |
144 | 161 | ||
145 | /* OK, we have hypercall. Increment the "next_hcall" cursor, | 162 | /* |
146 | * and wrap back to 0 if we reach the end. */ | 163 | * OK, we have hypercall. Increment the "next_hcall" cursor, |
164 | * and wrap back to 0 if we reach the end. | ||
165 | */ | ||
147 | if (++cpu->next_hcall == LHCALL_RING_SIZE) | 166 | if (++cpu->next_hcall == LHCALL_RING_SIZE) |
148 | cpu->next_hcall = 0; | 167 | cpu->next_hcall = 0; |
149 | 168 | ||
150 | /* Copy the hypercall arguments into a local copy of | 169 | /* |
151 | * the hcall_args struct. */ | 170 | * Copy the hypercall arguments into a local copy of the |
171 | * hcall_args struct. | ||
172 | */ | ||
152 | if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n], | 173 | if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n], |
153 | sizeof(struct hcall_args))) { | 174 | sizeof(struct hcall_args))) { |
154 | kill_guest(cpu, "Fetching async hypercalls"); | 175 | kill_guest(cpu, "Fetching async hypercalls"); |
@@ -164,19 +185,25 @@ static void do_async_hcalls(struct lg_cpu *cpu) | |||
164 | break; | 185 | break; |
165 | } | 186 | } |
166 | 187 | ||
167 | /* Stop doing hypercalls if they want to notify the Launcher: | 188 | /* |
168 | * it needs to service this first. */ | 189 | * Stop doing hypercalls if they want to notify the Launcher: |
190 | * it needs to service this first. | ||
191 | */ | ||
169 | if (cpu->pending_notify) | 192 | if (cpu->pending_notify) |
170 | break; | 193 | break; |
171 | } | 194 | } |
172 | } | 195 | } |
173 | 196 | ||
174 | /* Last of all, we look at what happens first of all. The very first time the | 197 | /* |
175 | * Guest makes a hypercall, we end up here to set things up: */ | 198 | * Last of all, we look at what happens first of all. The very first time the |
199 | * Guest makes a hypercall, we end up here to set things up: | ||
200 | */ | ||
176 | static void initialize(struct lg_cpu *cpu) | 201 | static void initialize(struct lg_cpu *cpu) |
177 | { | 202 | { |
178 | /* You can't do anything until you're initialized. The Guest knows the | 203 | /* |
179 | * rules, so we're unforgiving here. */ | 204 | * You can't do anything until you're initialized. The Guest knows the |
205 | * rules, so we're unforgiving here. | ||
206 | */ | ||
180 | if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) { | 207 | if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) { |
181 | kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0); | 208 | kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0); |
182 | return; | 209 | return; |
@@ -185,32 +212,40 @@ static void initialize(struct lg_cpu *cpu) | |||
185 | if (lguest_arch_init_hypercalls(cpu)) | 212 | if (lguest_arch_init_hypercalls(cpu)) |
186 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); | 213 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); |
187 | 214 | ||
188 | /* The Guest tells us where we're not to deliver interrupts by putting | 215 | /* |
189 | * the range of addresses into "struct lguest_data". */ | 216 | * The Guest tells us where we're not to deliver interrupts by putting |
217 | * the range of addresses into "struct lguest_data". | ||
218 | */ | ||
190 | if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start) | 219 | if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start) |
191 | || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end)) | 220 | || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end)) |
192 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); | 221 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); |
193 | 222 | ||
194 | /* We write the current time into the Guest's data page once so it can | 223 | /* |
195 | * set its clock. */ | 224 | * We write the current time into the Guest's data page once so it can |
225 | * set its clock. | ||
226 | */ | ||
196 | write_timestamp(cpu); | 227 | write_timestamp(cpu); |
197 | 228 | ||
198 | /* page_tables.c will also do some setup. */ | 229 | /* page_tables.c will also do some setup. */ |
199 | page_table_guest_data_init(cpu); | 230 | page_table_guest_data_init(cpu); |
200 | 231 | ||
201 | /* This is the one case where the above accesses might have been the | 232 | /* |
233 | * This is the one case where the above accesses might have been the | ||
202 | * first write to a Guest page. This may have caused a copy-on-write | 234 | * first write to a Guest page. This may have caused a copy-on-write |
203 | * fault, but the old page might be (read-only) in the Guest | 235 | * fault, but the old page might be (read-only) in the Guest |
204 | * pagetable. */ | 236 | * pagetable. |
237 | */ | ||
205 | guest_pagetable_clear_all(cpu); | 238 | guest_pagetable_clear_all(cpu); |
206 | } | 239 | } |
207 | /*:*/ | 240 | /*:*/ |
208 | 241 | ||
209 | /*M:013 If a Guest reads from a page (so creates a mapping) that it has never | 242 | /*M:013 |
243 | * If a Guest reads from a page (so creates a mapping) that it has never | ||
210 | * written to, and then the Launcher writes to it (ie. the output of a virtual | 244 | * written to, and then the Launcher writes to it (ie. the output of a virtual |
211 | * device), the Guest will still see the old page. In practice, this never | 245 | * device), the Guest will still see the old page. In practice, this never |
212 | * happens: why would the Guest read a page which it has never written to? But | 246 | * happens: why would the Guest read a page which it has never written to? But |
213 | * a similar scenario might one day bite us, so it's worth mentioning. :*/ | 247 | * a similar scenario might one day bite us, so it's worth mentioning. |
248 | :*/ | ||
214 | 249 | ||
215 | /*H:100 | 250 | /*H:100 |
216 | * Hypercalls | 251 | * Hypercalls |
@@ -229,17 +264,22 @@ void do_hypercalls(struct lg_cpu *cpu) | |||
229 | return; | 264 | return; |
230 | } | 265 | } |
231 | 266 | ||
232 | /* The Guest has initialized. | 267 | /* |
268 | * The Guest has initialized. | ||
233 | * | 269 | * |
234 | * Look in the hypercall ring for the async hypercalls: */ | 270 | * Look in the hypercall ring for the async hypercalls: |
271 | */ | ||
235 | do_async_hcalls(cpu); | 272 | do_async_hcalls(cpu); |
236 | 273 | ||
237 | /* If we stopped reading the hypercall ring because the Guest did a | 274 | /* |
275 | * If we stopped reading the hypercall ring because the Guest did a | ||
238 | * NOTIFY to the Launcher, we want to return now. Otherwise we do | 276 | * NOTIFY to the Launcher, we want to return now. Otherwise we do |
239 | * the hypercall. */ | 277 | * the hypercall. |
278 | */ | ||
240 | if (!cpu->pending_notify) { | 279 | if (!cpu->pending_notify) { |
241 | do_hcall(cpu, cpu->hcall); | 280 | do_hcall(cpu, cpu->hcall); |
242 | /* Tricky point: we reset the hcall pointer to mark the | 281 | /* |
282 | * Tricky point: we reset the hcall pointer to mark the | ||
243 | * hypercall as "done". We use the hcall pointer rather than | 283 | * hypercall as "done". We use the hcall pointer rather than |
244 | * the trap number to indicate a hypercall is pending. | 284 | * the trap number to indicate a hypercall is pending. |
245 | * Normally it doesn't matter: the Guest will run again and | 285 | * Normally it doesn't matter: the Guest will run again and |
@@ -248,13 +288,16 @@ void do_hypercalls(struct lg_cpu *cpu) | |||
248 | * However, if we are signalled or the Guest sends I/O to the | 288 | * However, if we are signalled or the Guest sends I/O to the |
249 | * Launcher, the run_guest() loop will exit without running the | 289 | * Launcher, the run_guest() loop will exit without running the |
250 | * Guest. When it comes back it would try to re-run the | 290 | * Guest. When it comes back it would try to re-run the |
251 | * hypercall. Finding that bug sucked. */ | 291 | * hypercall. Finding that bug sucked. |
292 | */ | ||
252 | cpu->hcall = NULL; | 293 | cpu->hcall = NULL; |
253 | } | 294 | } |
254 | } | 295 | } |
255 | 296 | ||
256 | /* This routine supplies the Guest with time: it's used for wallclock time at | 297 | /* |
257 | * initial boot and as a rough time source if the TSC isn't available. */ | 298 | * This routine supplies the Guest with time: it's used for wallclock time at |
299 | * initial boot and as a rough time source if the TSC isn't available. | ||
300 | */ | ||
258 | void write_timestamp(struct lg_cpu *cpu) | 301 | void write_timestamp(struct lg_cpu *cpu) |
259 | { | 302 | { |
260 | struct timespec now; | 303 | struct timespec now; |
diff --git a/drivers/lguest/interrupts_and_traps.c b/drivers/lguest/interrupts_and_traps.c index 0e9067b0d507..18648180db02 100644 --- a/drivers/lguest/interrupts_and_traps.c +++ b/drivers/lguest/interrupts_and_traps.c | |||
@@ -1,4 +1,5 @@ | |||
1 | /*P:800 Interrupts (traps) are complicated enough to earn their own file. | 1 | /*P:800 |
2 | * Interrupts (traps) are complicated enough to earn their own file. | ||
2 | * There are three classes of interrupts: | 3 | * There are three classes of interrupts: |
3 | * | 4 | * |
4 | * 1) Real hardware interrupts which occur while we're running the Guest, | 5 | * 1) Real hardware interrupts which occur while we're running the Guest, |
@@ -10,7 +11,8 @@ | |||
10 | * just like real hardware would deliver them. Traps from the Guest can be set | 11 | * just like real hardware would deliver them. Traps from the Guest can be set |
11 | * up to go directly back into the Guest, but sometimes the Host wants to see | 12 | * up to go directly back into the Guest, but sometimes the Host wants to see |
12 | * them first, so we also have a way of "reflecting" them into the Guest as if | 13 | * them first, so we also have a way of "reflecting" them into the Guest as if |
13 | * they had been delivered to it directly. :*/ | 14 | * they had been delivered to it directly. |
15 | :*/ | ||
14 | #include <linux/uaccess.h> | 16 | #include <linux/uaccess.h> |
15 | #include <linux/interrupt.h> | 17 | #include <linux/interrupt.h> |
16 | #include <linux/module.h> | 18 | #include <linux/module.h> |
@@ -26,8 +28,10 @@ static unsigned long idt_address(u32 lo, u32 hi) | |||
26 | return (lo & 0x0000FFFF) | (hi & 0xFFFF0000); | 28 | return (lo & 0x0000FFFF) | (hi & 0xFFFF0000); |
27 | } | 29 | } |
28 | 30 | ||
29 | /* The "type" of the interrupt handler is a 4 bit field: we only support a | 31 | /* |
30 | * couple of types. */ | 32 | * The "type" of the interrupt handler is a 4 bit field: we only support a |
33 | * couple of types. | ||
34 | */ | ||
31 | static int idt_type(u32 lo, u32 hi) | 35 | static int idt_type(u32 lo, u32 hi) |
32 | { | 36 | { |
33 | return (hi >> 8) & 0xF; | 37 | return (hi >> 8) & 0xF; |
@@ -39,8 +43,10 @@ static bool idt_present(u32 lo, u32 hi) | |||
39 | return (hi & 0x8000); | 43 | return (hi & 0x8000); |
40 | } | 44 | } |
41 | 45 | ||
42 | /* We need a helper to "push" a value onto the Guest's stack, since that's a | 46 | /* |
43 | * big part of what delivering an interrupt does. */ | 47 | * We need a helper to "push" a value onto the Guest's stack, since that's a |
48 | * big part of what delivering an interrupt does. | ||
49 | */ | ||
44 | static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val) | 50 | static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val) |
45 | { | 51 | { |
46 | /* Stack grows upwards: move stack then write value. */ | 52 | /* Stack grows upwards: move stack then write value. */ |
@@ -48,7 +54,8 @@ static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val) | |||
48 | lgwrite(cpu, *gstack, u32, val); | 54 | lgwrite(cpu, *gstack, u32, val); |
49 | } | 55 | } |
50 | 56 | ||
51 | /*H:210 The set_guest_interrupt() routine actually delivers the interrupt or | 57 | /*H:210 |
58 | * The set_guest_interrupt() routine actually delivers the interrupt or | ||
52 | * trap. The mechanics of delivering traps and interrupts to the Guest are the | 59 | * trap. The mechanics of delivering traps and interrupts to the Guest are the |
53 | * same, except some traps have an "error code" which gets pushed onto the | 60 | * same, except some traps have an "error code" which gets pushed onto the |
54 | * stack as well: the caller tells us if this is one. | 61 | * stack as well: the caller tells us if this is one. |
@@ -59,7 +66,8 @@ static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val) | |||
59 | * | 66 | * |
60 | * We set up the stack just like the CPU does for a real interrupt, so it's | 67 | * We set up the stack just like the CPU does for a real interrupt, so it's |
61 | * identical for the Guest (and the standard "iret" instruction will undo | 68 | * identical for the Guest (and the standard "iret" instruction will undo |
62 | * it). */ | 69 | * it). |
70 | */ | ||
63 | static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, | 71 | static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, |
64 | bool has_err) | 72 | bool has_err) |
65 | { | 73 | { |
@@ -67,20 +75,26 @@ static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, | |||
67 | u32 eflags, ss, irq_enable; | 75 | u32 eflags, ss, irq_enable; |
68 | unsigned long virtstack; | 76 | unsigned long virtstack; |
69 | 77 | ||
70 | /* There are two cases for interrupts: one where the Guest is already | 78 | /* |
79 | * There are two cases for interrupts: one where the Guest is already | ||
71 | * in the kernel, and a more complex one where the Guest is in | 80 | * in the kernel, and a more complex one where the Guest is in |
72 | * userspace. We check the privilege level to find out. */ | 81 | * userspace. We check the privilege level to find out. |
82 | */ | ||
73 | if ((cpu->regs->ss&0x3) != GUEST_PL) { | 83 | if ((cpu->regs->ss&0x3) != GUEST_PL) { |
74 | /* The Guest told us their kernel stack with the SET_STACK | 84 | /* |
75 | * hypercall: both the virtual address and the segment */ | 85 | * The Guest told us their kernel stack with the SET_STACK |
86 | * hypercall: both the virtual address and the segment. | ||
87 | */ | ||
76 | virtstack = cpu->esp1; | 88 | virtstack = cpu->esp1; |
77 | ss = cpu->ss1; | 89 | ss = cpu->ss1; |
78 | 90 | ||
79 | origstack = gstack = guest_pa(cpu, virtstack); | 91 | origstack = gstack = guest_pa(cpu, virtstack); |
80 | /* We push the old stack segment and pointer onto the new | 92 | /* |
93 | * We push the old stack segment and pointer onto the new | ||
81 | * stack: when the Guest does an "iret" back from the interrupt | 94 | * stack: when the Guest does an "iret" back from the interrupt |
82 | * handler the CPU will notice they're dropping privilege | 95 | * handler the CPU will notice they're dropping privilege |
83 | * levels and expect these here. */ | 96 | * levels and expect these here. |
97 | */ | ||
84 | push_guest_stack(cpu, &gstack, cpu->regs->ss); | 98 | push_guest_stack(cpu, &gstack, cpu->regs->ss); |
85 | push_guest_stack(cpu, &gstack, cpu->regs->esp); | 99 | push_guest_stack(cpu, &gstack, cpu->regs->esp); |
86 | } else { | 100 | } else { |
@@ -91,18 +105,22 @@ static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, | |||
91 | origstack = gstack = guest_pa(cpu, virtstack); | 105 | origstack = gstack = guest_pa(cpu, virtstack); |
92 | } | 106 | } |
93 | 107 | ||
94 | /* Remember that we never let the Guest actually disable interrupts, so | 108 | /* |
109 | * Remember that we never let the Guest actually disable interrupts, so | ||
95 | * the "Interrupt Flag" bit is always set. We copy that bit from the | 110 | * the "Interrupt Flag" bit is always set. We copy that bit from the |
96 | * Guest's "irq_enabled" field into the eflags word: we saw the Guest | 111 | * Guest's "irq_enabled" field into the eflags word: we saw the Guest |
97 | * copy it back in "lguest_iret". */ | 112 | * copy it back in "lguest_iret". |
113 | */ | ||
98 | eflags = cpu->regs->eflags; | 114 | eflags = cpu->regs->eflags; |
99 | if (get_user(irq_enable, &cpu->lg->lguest_data->irq_enabled) == 0 | 115 | if (get_user(irq_enable, &cpu->lg->lguest_data->irq_enabled) == 0 |
100 | && !(irq_enable & X86_EFLAGS_IF)) | 116 | && !(irq_enable & X86_EFLAGS_IF)) |
101 | eflags &= ~X86_EFLAGS_IF; | 117 | eflags &= ~X86_EFLAGS_IF; |
102 | 118 | ||
103 | /* An interrupt is expected to push three things on the stack: the old | 119 | /* |
120 | * An interrupt is expected to push three things on the stack: the old | ||
104 | * "eflags" word, the old code segment, and the old instruction | 121 | * "eflags" word, the old code segment, and the old instruction |
105 | * pointer. */ | 122 | * pointer. |
123 | */ | ||
106 | push_guest_stack(cpu, &gstack, eflags); | 124 | push_guest_stack(cpu, &gstack, eflags); |
107 | push_guest_stack(cpu, &gstack, cpu->regs->cs); | 125 | push_guest_stack(cpu, &gstack, cpu->regs->cs); |
108 | push_guest_stack(cpu, &gstack, cpu->regs->eip); | 126 | push_guest_stack(cpu, &gstack, cpu->regs->eip); |
@@ -111,15 +129,19 @@ static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, | |||
111 | if (has_err) | 129 | if (has_err) |
112 | push_guest_stack(cpu, &gstack, cpu->regs->errcode); | 130 | push_guest_stack(cpu, &gstack, cpu->regs->errcode); |
113 | 131 | ||
114 | /* Now we've pushed all the old state, we change the stack, the code | 132 | /* |
115 | * segment and the address to execute. */ | 133 | * Now we've pushed all the old state, we change the stack, the code |
134 | * segment and the address to execute. | ||
135 | */ | ||
116 | cpu->regs->ss = ss; | 136 | cpu->regs->ss = ss; |
117 | cpu->regs->esp = virtstack + (gstack - origstack); | 137 | cpu->regs->esp = virtstack + (gstack - origstack); |
118 | cpu->regs->cs = (__KERNEL_CS|GUEST_PL); | 138 | cpu->regs->cs = (__KERNEL_CS|GUEST_PL); |
119 | cpu->regs->eip = idt_address(lo, hi); | 139 | cpu->regs->eip = idt_address(lo, hi); |
120 | 140 | ||
121 | /* There are two kinds of interrupt handlers: 0xE is an "interrupt | 141 | /* |
122 | * gate" which expects interrupts to be disabled on entry. */ | 142 | * There are two kinds of interrupt handlers: 0xE is an "interrupt |
143 | * gate" which expects interrupts to be disabled on entry. | ||
144 | */ | ||
123 | if (idt_type(lo, hi) == 0xE) | 145 | if (idt_type(lo, hi) == 0xE) |
124 | if (put_user(0, &cpu->lg->lguest_data->irq_enabled)) | 146 | if (put_user(0, &cpu->lg->lguest_data->irq_enabled)) |
125 | kill_guest(cpu, "Disabling interrupts"); | 147 | kill_guest(cpu, "Disabling interrupts"); |
@@ -130,7 +152,8 @@ static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, | |||
130 | * | 152 | * |
131 | * interrupt_pending() returns the first pending interrupt which isn't blocked | 153 | * interrupt_pending() returns the first pending interrupt which isn't blocked |
132 | * by the Guest. It is called before every entry to the Guest, and just before | 154 | * by the Guest. It is called before every entry to the Guest, and just before |
133 | * we go to sleep when the Guest has halted itself. */ | 155 | * we go to sleep when the Guest has halted itself. |
156 | */ | ||
134 | unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more) | 157 | unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more) |
135 | { | 158 | { |
136 | unsigned int irq; | 159 | unsigned int irq; |
@@ -140,8 +163,10 @@ unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more) | |||
140 | if (!cpu->lg->lguest_data) | 163 | if (!cpu->lg->lguest_data) |
141 | return LGUEST_IRQS; | 164 | return LGUEST_IRQS; |
142 | 165 | ||
143 | /* Take our "irqs_pending" array and remove any interrupts the Guest | 166 | /* |
144 | * wants blocked: the result ends up in "blk". */ | 167 | * Take our "irqs_pending" array and remove any interrupts the Guest |
168 | * wants blocked: the result ends up in "blk". | ||
169 | */ | ||
145 | if (copy_from_user(&blk, cpu->lg->lguest_data->blocked_interrupts, | 170 | if (copy_from_user(&blk, cpu->lg->lguest_data->blocked_interrupts, |
146 | sizeof(blk))) | 171 | sizeof(blk))) |
147 | return LGUEST_IRQS; | 172 | return LGUEST_IRQS; |
@@ -154,16 +179,20 @@ unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more) | |||
154 | return irq; | 179 | return irq; |
155 | } | 180 | } |
156 | 181 | ||
157 | /* This actually diverts the Guest to running an interrupt handler, once an | 182 | /* |
158 | * interrupt has been identified by interrupt_pending(). */ | 183 | * This actually diverts the Guest to running an interrupt handler, once an |
184 | * interrupt has been identified by interrupt_pending(). | ||
185 | */ | ||
159 | void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more) | 186 | void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more) |
160 | { | 187 | { |
161 | struct desc_struct *idt; | 188 | struct desc_struct *idt; |
162 | 189 | ||
163 | BUG_ON(irq >= LGUEST_IRQS); | 190 | BUG_ON(irq >= LGUEST_IRQS); |
164 | 191 | ||
165 | /* They may be in the middle of an iret, where they asked us never to | 192 | /* |
166 | * deliver interrupts. */ | 193 | * They may be in the middle of an iret, where they asked us never to |
194 | * deliver interrupts. | ||
195 | */ | ||
167 | if (cpu->regs->eip >= cpu->lg->noirq_start && | 196 | if (cpu->regs->eip >= cpu->lg->noirq_start && |
168 | (cpu->regs->eip < cpu->lg->noirq_end)) | 197 | (cpu->regs->eip < cpu->lg->noirq_end)) |
169 | return; | 198 | return; |
@@ -187,29 +216,37 @@ void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more) | |||
187 | } | 216 | } |
188 | } | 217 | } |
189 | 218 | ||
190 | /* Look at the IDT entry the Guest gave us for this interrupt. The | 219 | /* |
220 | * Look at the IDT entry the Guest gave us for this interrupt. The | ||
191 | * first 32 (FIRST_EXTERNAL_VECTOR) entries are for traps, so we skip | 221 | * first 32 (FIRST_EXTERNAL_VECTOR) entries are for traps, so we skip |
192 | * over them. */ | 222 | * over them. |
223 | */ | ||
193 | idt = &cpu->arch.idt[FIRST_EXTERNAL_VECTOR+irq]; | 224 | idt = &cpu->arch.idt[FIRST_EXTERNAL_VECTOR+irq]; |
194 | /* If they don't have a handler (yet?), we just ignore it */ | 225 | /* If they don't have a handler (yet?), we just ignore it */ |
195 | if (idt_present(idt->a, idt->b)) { | 226 | if (idt_present(idt->a, idt->b)) { |
196 | /* OK, mark it no longer pending and deliver it. */ | 227 | /* OK, mark it no longer pending and deliver it. */ |
197 | clear_bit(irq, cpu->irqs_pending); | 228 | clear_bit(irq, cpu->irqs_pending); |
198 | /* set_guest_interrupt() takes the interrupt descriptor and a | 229 | /* |
230 | * set_guest_interrupt() takes the interrupt descriptor and a | ||
199 | * flag to say whether this interrupt pushes an error code onto | 231 | * flag to say whether this interrupt pushes an error code onto |
200 | * the stack as well: virtual interrupts never do. */ | 232 | * the stack as well: virtual interrupts never do. |
233 | */ | ||
201 | set_guest_interrupt(cpu, idt->a, idt->b, false); | 234 | set_guest_interrupt(cpu, idt->a, idt->b, false); |
202 | } | 235 | } |
203 | 236 | ||
204 | /* Every time we deliver an interrupt, we update the timestamp in the | 237 | /* |
238 | * Every time we deliver an interrupt, we update the timestamp in the | ||
205 | * Guest's lguest_data struct. It would be better for the Guest if we | 239 | * Guest's lguest_data struct. It would be better for the Guest if we |
206 | * did this more often, but it can actually be quite slow: doing it | 240 | * did this more often, but it can actually be quite slow: doing it |
207 | * here is a compromise which means at least it gets updated every | 241 | * here is a compromise which means at least it gets updated every |
208 | * timer interrupt. */ | 242 | * timer interrupt. |
243 | */ | ||
209 | write_timestamp(cpu); | 244 | write_timestamp(cpu); |
210 | 245 | ||
211 | /* If there are no other interrupts we want to deliver, clear | 246 | /* |
212 | * the pending flag. */ | 247 | * If there are no other interrupts we want to deliver, clear |
248 | * the pending flag. | ||
249 | */ | ||
213 | if (!more) | 250 | if (!more) |
214 | put_user(0, &cpu->lg->lguest_data->irq_pending); | 251 | put_user(0, &cpu->lg->lguest_data->irq_pending); |
215 | } | 252 | } |
@@ -217,24 +254,29 @@ void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more) | |||
217 | /* And this is the routine when we want to set an interrupt for the Guest. */ | 254 | /* And this is the routine when we want to set an interrupt for the Guest. */ |
218 | void set_interrupt(struct lg_cpu *cpu, unsigned int irq) | 255 | void set_interrupt(struct lg_cpu *cpu, unsigned int irq) |
219 | { | 256 | { |
220 | /* Next time the Guest runs, the core code will see if it can deliver | 257 | /* |
221 | * this interrupt. */ | 258 | * Next time the Guest runs, the core code will see if it can deliver |
259 | * this interrupt. | ||
260 | */ | ||
222 | set_bit(irq, cpu->irqs_pending); | 261 | set_bit(irq, cpu->irqs_pending); |
223 | 262 | ||
224 | /* Make sure it sees it; it might be asleep (eg. halted), or | 263 | /* |
225 | * running the Guest right now, in which case kick_process() | 264 | * Make sure it sees it; it might be asleep (eg. halted), or running |
226 | * will knock it out. */ | 265 | * the Guest right now, in which case kick_process() will knock it out. |
266 | */ | ||
227 | if (!wake_up_process(cpu->tsk)) | 267 | if (!wake_up_process(cpu->tsk)) |
228 | kick_process(cpu->tsk); | 268 | kick_process(cpu->tsk); |
229 | } | 269 | } |
230 | /*:*/ | 270 | /*:*/ |
231 | 271 | ||
232 | /* Linux uses trap 128 for system calls. Plan9 uses 64, and Ron Minnich sent | 272 | /* |
273 | * Linux uses trap 128 for system calls. Plan9 uses 64, and Ron Minnich sent | ||
233 | * me a patch, so we support that too. It'd be a big step for lguest if half | 274 | * me a patch, so we support that too. It'd be a big step for lguest if half |
234 | * the Plan 9 user base were to start using it. | 275 | * the Plan 9 user base were to start using it. |
235 | * | 276 | * |
236 | * Actually now I think of it, it's possible that Ron *is* half the Plan 9 | 277 | * Actually now I think of it, it's possible that Ron *is* half the Plan 9 |
237 | * userbase. Oh well. */ | 278 | * userbase. Oh well. |
279 | */ | ||
238 | static bool could_be_syscall(unsigned int num) | 280 | static bool could_be_syscall(unsigned int num) |
239 | { | 281 | { |
240 | /* Normal Linux SYSCALL_VECTOR or reserved vector? */ | 282 | /* Normal Linux SYSCALL_VECTOR or reserved vector? */ |
@@ -274,9 +316,11 @@ void free_interrupts(void) | |||
274 | clear_bit(syscall_vector, used_vectors); | 316 | clear_bit(syscall_vector, used_vectors); |
275 | } | 317 | } |
276 | 318 | ||
277 | /*H:220 Now we've got the routines to deliver interrupts, delivering traps like | 319 | /*H:220 |
320 | * Now we've got the routines to deliver interrupts, delivering traps like | ||
278 | * page fault is easy. The only trick is that Intel decided that some traps | 321 | * page fault is easy. The only trick is that Intel decided that some traps |
279 | * should have error codes: */ | 322 | * should have error codes: |
323 | */ | ||
280 | static bool has_err(unsigned int trap) | 324 | static bool has_err(unsigned int trap) |
281 | { | 325 | { |
282 | return (trap == 8 || (trap >= 10 && trap <= 14) || trap == 17); | 326 | return (trap == 8 || (trap >= 10 && trap <= 14) || trap == 17); |
@@ -285,13 +329,17 @@ static bool has_err(unsigned int trap) | |||
285 | /* deliver_trap() returns true if it could deliver the trap. */ | 329 | /* deliver_trap() returns true if it could deliver the trap. */ |
286 | bool deliver_trap(struct lg_cpu *cpu, unsigned int num) | 330 | bool deliver_trap(struct lg_cpu *cpu, unsigned int num) |
287 | { | 331 | { |
288 | /* Trap numbers are always 8 bit, but we set an impossible trap number | 332 | /* |
289 | * for traps inside the Switcher, so check that here. */ | 333 | * Trap numbers are always 8 bit, but we set an impossible trap number |
334 | * for traps inside the Switcher, so check that here. | ||
335 | */ | ||
290 | if (num >= ARRAY_SIZE(cpu->arch.idt)) | 336 | if (num >= ARRAY_SIZE(cpu->arch.idt)) |
291 | return false; | 337 | return false; |
292 | 338 | ||
293 | /* Early on the Guest hasn't set the IDT entries (or maybe it put a | 339 | /* |
294 | * bogus one in): if we fail here, the Guest will be killed. */ | 340 | * Early on the Guest hasn't set the IDT entries (or maybe it put a |
341 | * bogus one in): if we fail here, the Guest will be killed. | ||
342 | */ | ||
295 | if (!idt_present(cpu->arch.idt[num].a, cpu->arch.idt[num].b)) | 343 | if (!idt_present(cpu->arch.idt[num].a, cpu->arch.idt[num].b)) |
296 | return false; | 344 | return false; |
297 | set_guest_interrupt(cpu, cpu->arch.idt[num].a, | 345 | set_guest_interrupt(cpu, cpu->arch.idt[num].a, |
@@ -299,7 +347,8 @@ bool deliver_trap(struct lg_cpu *cpu, unsigned int num) | |||
299 | return true; | 347 | return true; |
300 | } | 348 | } |
301 | 349 | ||
302 | /*H:250 Here's the hard part: returning to the Host every time a trap happens | 350 | /*H:250 |
351 | * Here's the hard part: returning to the Host every time a trap happens | ||
303 | * and then calling deliver_trap() and re-entering the Guest is slow. | 352 | * and then calling deliver_trap() and re-entering the Guest is slow. |
304 | * Particularly because Guest userspace system calls are traps (usually trap | 353 | * Particularly because Guest userspace system calls are traps (usually trap |
305 | * 128). | 354 | * 128). |
@@ -311,69 +360,87 @@ bool deliver_trap(struct lg_cpu *cpu, unsigned int num) | |||
311 | * the other hypervisors would beat it up at lunchtime. | 360 | * the other hypervisors would beat it up at lunchtime. |
312 | * | 361 | * |
313 | * This routine indicates if a particular trap number could be delivered | 362 | * This routine indicates if a particular trap number could be delivered |
314 | * directly. */ | 363 | * directly. |
364 | */ | ||
315 | static bool direct_trap(unsigned int num) | 365 | static bool direct_trap(unsigned int num) |
316 | { | 366 | { |
317 | /* Hardware interrupts don't go to the Guest at all (except system | 367 | /* |
318 | * call). */ | 368 | * Hardware interrupts don't go to the Guest at all (except system |
369 | * call). | ||
370 | */ | ||
319 | if (num >= FIRST_EXTERNAL_VECTOR && !could_be_syscall(num)) | 371 | if (num >= FIRST_EXTERNAL_VECTOR && !could_be_syscall(num)) |
320 | return false; | 372 | return false; |
321 | 373 | ||
322 | /* The Host needs to see page faults (for shadow paging and to save the | 374 | /* |
375 | * The Host needs to see page faults (for shadow paging and to save the | ||
323 | * fault address), general protection faults (in/out emulation) and | 376 | * fault address), general protection faults (in/out emulation) and |
324 | * device not available (TS handling), invalid opcode fault (kvm hcall), | 377 | * device not available (TS handling), invalid opcode fault (kvm hcall), |
325 | * and of course, the hypercall trap. */ | 378 | * and of course, the hypercall trap. |
379 | */ | ||
326 | return num != 14 && num != 13 && num != 7 && | 380 | return num != 14 && num != 13 && num != 7 && |
327 | num != 6 && num != LGUEST_TRAP_ENTRY; | 381 | num != 6 && num != LGUEST_TRAP_ENTRY; |
328 | } | 382 | } |
329 | /*:*/ | 383 | /*:*/ |
330 | 384 | ||
331 | /*M:005 The Guest has the ability to turn its interrupt gates into trap gates, | 385 | /*M:005 |
386 | * The Guest has the ability to turn its interrupt gates into trap gates, | ||
332 | * if it is careful. The Host will let trap gates can go directly to the | 387 | * if it is careful. The Host will let trap gates can go directly to the |
333 | * Guest, but the Guest needs the interrupts atomically disabled for an | 388 | * Guest, but the Guest needs the interrupts atomically disabled for an |
334 | * interrupt gate. It can do this by pointing the trap gate at instructions | 389 | * interrupt gate. It can do this by pointing the trap gate at instructions |
335 | * within noirq_start and noirq_end, where it can safely disable interrupts. */ | 390 | * within noirq_start and noirq_end, where it can safely disable interrupts. |
391 | */ | ||
336 | 392 | ||
337 | /*M:006 The Guests do not use the sysenter (fast system call) instruction, | 393 | /*M:006 |
394 | * The Guests do not use the sysenter (fast system call) instruction, | ||
338 | * because it's hardcoded to enter privilege level 0 and so can't go direct. | 395 | * because it's hardcoded to enter privilege level 0 and so can't go direct. |
339 | * It's about twice as fast as the older "int 0x80" system call, so it might | 396 | * It's about twice as fast as the older "int 0x80" system call, so it might |
340 | * still be worthwhile to handle it in the Switcher and lcall down to the | 397 | * still be worthwhile to handle it in the Switcher and lcall down to the |
341 | * Guest. The sysenter semantics are hairy tho: search for that keyword in | 398 | * Guest. The sysenter semantics are hairy tho: search for that keyword in |
342 | * entry.S :*/ | 399 | * entry.S |
400 | :*/ | ||
343 | 401 | ||
344 | /*H:260 When we make traps go directly into the Guest, we need to make sure | 402 | /*H:260 |
403 | * When we make traps go directly into the Guest, we need to make sure | ||
345 | * the kernel stack is valid (ie. mapped in the page tables). Otherwise, the | 404 | * the kernel stack is valid (ie. mapped in the page tables). Otherwise, the |
346 | * CPU trying to deliver the trap will fault while trying to push the interrupt | 405 | * CPU trying to deliver the trap will fault while trying to push the interrupt |
347 | * words on the stack: this is called a double fault, and it forces us to kill | 406 | * words on the stack: this is called a double fault, and it forces us to kill |
348 | * the Guest. | 407 | * the Guest. |
349 | * | 408 | * |
350 | * Which is deeply unfair, because (literally!) it wasn't the Guests' fault. */ | 409 | * Which is deeply unfair, because (literally!) it wasn't the Guests' fault. |
410 | */ | ||
351 | void pin_stack_pages(struct lg_cpu *cpu) | 411 | void pin_stack_pages(struct lg_cpu *cpu) |
352 | { | 412 | { |
353 | unsigned int i; | 413 | unsigned int i; |
354 | 414 | ||
355 | /* Depending on the CONFIG_4KSTACKS option, the Guest can have one or | 415 | /* |
356 | * two pages of stack space. */ | 416 | * Depending on the CONFIG_4KSTACKS option, the Guest can have one or |
417 | * two pages of stack space. | ||
418 | */ | ||
357 | for (i = 0; i < cpu->lg->stack_pages; i++) | 419 | for (i = 0; i < cpu->lg->stack_pages; i++) |
358 | /* The stack grows *upwards*, so the address we're given is the | 420 | /* |
421 | * The stack grows *upwards*, so the address we're given is the | ||
359 | * start of the page after the kernel stack. Subtract one to | 422 | * start of the page after the kernel stack. Subtract one to |
360 | * get back onto the first stack page, and keep subtracting to | 423 | * get back onto the first stack page, and keep subtracting to |
361 | * get to the rest of the stack pages. */ | 424 | * get to the rest of the stack pages. |
425 | */ | ||
362 | pin_page(cpu, cpu->esp1 - 1 - i * PAGE_SIZE); | 426 | pin_page(cpu, cpu->esp1 - 1 - i * PAGE_SIZE); |
363 | } | 427 | } |
364 | 428 | ||
365 | /* Direct traps also mean that we need to know whenever the Guest wants to use | 429 | /* |
430 | * Direct traps also mean that we need to know whenever the Guest wants to use | ||
366 | * a different kernel stack, so we can change the IDT entries to use that | 431 | * a different kernel stack, so we can change the IDT entries to use that |
367 | * stack. The IDT entries expect a virtual address, so unlike most addresses | 432 | * stack. The IDT entries expect a virtual address, so unlike most addresses |
368 | * the Guest gives us, the "esp" (stack pointer) value here is virtual, not | 433 | * the Guest gives us, the "esp" (stack pointer) value here is virtual, not |
369 | * physical. | 434 | * physical. |
370 | * | 435 | * |
371 | * In Linux each process has its own kernel stack, so this happens a lot: we | 436 | * In Linux each process has its own kernel stack, so this happens a lot: we |
372 | * change stacks on each context switch. */ | 437 | * change stacks on each context switch. |
438 | */ | ||
373 | void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages) | 439 | void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages) |
374 | { | 440 | { |
375 | /* You are not allowed have a stack segment with privilege level 0: bad | 441 | /* |
376 | * Guest! */ | 442 | * You're not allowed a stack segment with privilege level 0: bad Guest! |
443 | */ | ||
377 | if ((seg & 0x3) != GUEST_PL) | 444 | if ((seg & 0x3) != GUEST_PL) |
378 | kill_guest(cpu, "bad stack segment %i", seg); | 445 | kill_guest(cpu, "bad stack segment %i", seg); |
379 | /* We only expect one or two stack pages. */ | 446 | /* We only expect one or two stack pages. */ |
@@ -387,11 +454,15 @@ void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages) | |||
387 | pin_stack_pages(cpu); | 454 | pin_stack_pages(cpu); |
388 | } | 455 | } |
389 | 456 | ||
390 | /* All this reference to mapping stacks leads us neatly into the other complex | 457 | /* |
391 | * part of the Host: page table handling. */ | 458 | * All this reference to mapping stacks leads us neatly into the other complex |
459 | * part of the Host: page table handling. | ||
460 | */ | ||
392 | 461 | ||
393 | /*H:235 This is the routine which actually checks the Guest's IDT entry and | 462 | /*H:235 |
394 | * transfers it into the entry in "struct lguest": */ | 463 | * This is the routine which actually checks the Guest's IDT entry and |
464 | * transfers it into the entry in "struct lguest": | ||
465 | */ | ||
395 | static void set_trap(struct lg_cpu *cpu, struct desc_struct *trap, | 466 | static void set_trap(struct lg_cpu *cpu, struct desc_struct *trap, |
396 | unsigned int num, u32 lo, u32 hi) | 467 | unsigned int num, u32 lo, u32 hi) |
397 | { | 468 | { |
@@ -407,30 +478,38 @@ static void set_trap(struct lg_cpu *cpu, struct desc_struct *trap, | |||
407 | if (type != 0xE && type != 0xF) | 478 | if (type != 0xE && type != 0xF) |
408 | kill_guest(cpu, "bad IDT type %i", type); | 479 | kill_guest(cpu, "bad IDT type %i", type); |
409 | 480 | ||
410 | /* We only copy the handler address, present bit, privilege level and | 481 | /* |
482 | * We only copy the handler address, present bit, privilege level and | ||
411 | * type. The privilege level controls where the trap can be triggered | 483 | * type. The privilege level controls where the trap can be triggered |
412 | * manually with an "int" instruction. This is usually GUEST_PL, | 484 | * manually with an "int" instruction. This is usually GUEST_PL, |
413 | * except for system calls which userspace can use. */ | 485 | * except for system calls which userspace can use. |
486 | */ | ||
414 | trap->a = ((__KERNEL_CS|GUEST_PL)<<16) | (lo&0x0000FFFF); | 487 | trap->a = ((__KERNEL_CS|GUEST_PL)<<16) | (lo&0x0000FFFF); |
415 | trap->b = (hi&0xFFFFEF00); | 488 | trap->b = (hi&0xFFFFEF00); |
416 | } | 489 | } |
417 | 490 | ||
418 | /*H:230 While we're here, dealing with delivering traps and interrupts to the | 491 | /*H:230 |
492 | * While we're here, dealing with delivering traps and interrupts to the | ||
419 | * Guest, we might as well complete the picture: how the Guest tells us where | 493 | * Guest, we might as well complete the picture: how the Guest tells us where |
420 | * it wants them to go. This would be simple, except making traps fast | 494 | * it wants them to go. This would be simple, except making traps fast |
421 | * requires some tricks. | 495 | * requires some tricks. |
422 | * | 496 | * |
423 | * We saw the Guest setting Interrupt Descriptor Table (IDT) entries with the | 497 | * We saw the Guest setting Interrupt Descriptor Table (IDT) entries with the |
424 | * LHCALL_LOAD_IDT_ENTRY hypercall before: that comes here. */ | 498 | * LHCALL_LOAD_IDT_ENTRY hypercall before: that comes here. |
499 | */ | ||
425 | void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int num, u32 lo, u32 hi) | 500 | void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int num, u32 lo, u32 hi) |
426 | { | 501 | { |
427 | /* Guest never handles: NMI, doublefault, spurious interrupt or | 502 | /* |
428 | * hypercall. We ignore when it tries to set them. */ | 503 | * Guest never handles: NMI, doublefault, spurious interrupt or |
504 | * hypercall. We ignore when it tries to set them. | ||
505 | */ | ||
429 | if (num == 2 || num == 8 || num == 15 || num == LGUEST_TRAP_ENTRY) | 506 | if (num == 2 || num == 8 || num == 15 || num == LGUEST_TRAP_ENTRY) |
430 | return; | 507 | return; |
431 | 508 | ||
432 | /* Mark the IDT as changed: next time the Guest runs we'll know we have | 509 | /* |
433 | * to copy this again. */ | 510 | * Mark the IDT as changed: next time the Guest runs we'll know we have |
511 | * to copy this again. | ||
512 | */ | ||
434 | cpu->changed |= CHANGED_IDT; | 513 | cpu->changed |= CHANGED_IDT; |
435 | 514 | ||
436 | /* Check that the Guest doesn't try to step outside the bounds. */ | 515 | /* Check that the Guest doesn't try to step outside the bounds. */ |
@@ -440,9 +519,11 @@ void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int num, u32 lo, u32 hi) | |||
440 | set_trap(cpu, &cpu->arch.idt[num], num, lo, hi); | 519 | set_trap(cpu, &cpu->arch.idt[num], num, lo, hi); |
441 | } | 520 | } |
442 | 521 | ||
443 | /* The default entry for each interrupt points into the Switcher routines which | 522 | /* |
523 | * The default entry for each interrupt points into the Switcher routines which | ||
444 | * simply return to the Host. The run_guest() loop will then call | 524 | * simply return to the Host. The run_guest() loop will then call |
445 | * deliver_trap() to bounce it back into the Guest. */ | 525 | * deliver_trap() to bounce it back into the Guest. |
526 | */ | ||
446 | static void default_idt_entry(struct desc_struct *idt, | 527 | static void default_idt_entry(struct desc_struct *idt, |
447 | int trap, | 528 | int trap, |
448 | const unsigned long handler, | 529 | const unsigned long handler, |
@@ -451,13 +532,17 @@ static void default_idt_entry(struct desc_struct *idt, | |||
451 | /* A present interrupt gate. */ | 532 | /* A present interrupt gate. */ |
452 | u32 flags = 0x8e00; | 533 | u32 flags = 0x8e00; |
453 | 534 | ||
454 | /* Set the privilege level on the entry for the hypercall: this allows | 535 | /* |
455 | * the Guest to use the "int" instruction to trigger it. */ | 536 | * Set the privilege level on the entry for the hypercall: this allows |
537 | * the Guest to use the "int" instruction to trigger it. | ||
538 | */ | ||
456 | if (trap == LGUEST_TRAP_ENTRY) | 539 | if (trap == LGUEST_TRAP_ENTRY) |
457 | flags |= (GUEST_PL << 13); | 540 | flags |= (GUEST_PL << 13); |
458 | else if (base) | 541 | else if (base) |
459 | /* Copy priv. level from what Guest asked for. This allows | 542 | /* |
460 | * debug (int 3) traps from Guest userspace, for example. */ | 543 | * Copy privilege level from what Guest asked for. This allows |
544 | * debug (int 3) traps from Guest userspace, for example. | ||
545 | */ | ||
461 | flags |= (base->b & 0x6000); | 546 | flags |= (base->b & 0x6000); |
462 | 547 | ||
463 | /* Now pack it into the IDT entry in its weird format. */ | 548 | /* Now pack it into the IDT entry in its weird format. */ |
@@ -475,16 +560,20 @@ void setup_default_idt_entries(struct lguest_ro_state *state, | |||
475 | default_idt_entry(&state->guest_idt[i], i, def[i], NULL); | 560 | default_idt_entry(&state->guest_idt[i], i, def[i], NULL); |
476 | } | 561 | } |
477 | 562 | ||
478 | /*H:240 We don't use the IDT entries in the "struct lguest" directly, instead | 563 | /*H:240 |
564 | * We don't use the IDT entries in the "struct lguest" directly, instead | ||
479 | * we copy them into the IDT which we've set up for Guests on this CPU, just | 565 | * we copy them into the IDT which we've set up for Guests on this CPU, just |
480 | * before we run the Guest. This routine does that copy. */ | 566 | * before we run the Guest. This routine does that copy. |
567 | */ | ||
481 | void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt, | 568 | void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt, |
482 | const unsigned long *def) | 569 | const unsigned long *def) |
483 | { | 570 | { |
484 | unsigned int i; | 571 | unsigned int i; |
485 | 572 | ||
486 | /* We can simply copy the direct traps, otherwise we use the default | 573 | /* |
487 | * ones in the Switcher: they will return to the Host. */ | 574 | * We can simply copy the direct traps, otherwise we use the default |
575 | * ones in the Switcher: they will return to the Host. | ||
576 | */ | ||
488 | for (i = 0; i < ARRAY_SIZE(cpu->arch.idt); i++) { | 577 | for (i = 0; i < ARRAY_SIZE(cpu->arch.idt); i++) { |
489 | const struct desc_struct *gidt = &cpu->arch.idt[i]; | 578 | const struct desc_struct *gidt = &cpu->arch.idt[i]; |
490 | 579 | ||
@@ -492,14 +581,16 @@ void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt, | |||
492 | if (!direct_trap(i)) | 581 | if (!direct_trap(i)) |
493 | continue; | 582 | continue; |
494 | 583 | ||
495 | /* Only trap gates (type 15) can go direct to the Guest. | 584 | /* |
585 | * Only trap gates (type 15) can go direct to the Guest. | ||
496 | * Interrupt gates (type 14) disable interrupts as they are | 586 | * Interrupt gates (type 14) disable interrupts as they are |
497 | * entered, which we never let the Guest do. Not present | 587 | * entered, which we never let the Guest do. Not present |
498 | * entries (type 0x0) also can't go direct, of course. | 588 | * entries (type 0x0) also can't go direct, of course. |
499 | * | 589 | * |
500 | * If it can't go direct, we still need to copy the priv. level: | 590 | * If it can't go direct, we still need to copy the priv. level: |
501 | * they might want to give userspace access to a software | 591 | * they might want to give userspace access to a software |
502 | * interrupt. */ | 592 | * interrupt. |
593 | */ | ||
503 | if (idt_type(gidt->a, gidt->b) == 0xF) | 594 | if (idt_type(gidt->a, gidt->b) == 0xF) |
504 | idt[i] = *gidt; | 595 | idt[i] = *gidt; |
505 | else | 596 | else |
@@ -518,7 +609,8 @@ void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt, | |||
518 | * the next timer interrupt (in nanoseconds). We use the high-resolution timer | 609 | * the next timer interrupt (in nanoseconds). We use the high-resolution timer |
519 | * infrastructure to set a callback at that time. | 610 | * infrastructure to set a callback at that time. |
520 | * | 611 | * |
521 | * 0 means "turn off the clock". */ | 612 | * 0 means "turn off the clock". |
613 | */ | ||
522 | void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta) | 614 | void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta) |
523 | { | 615 | { |
524 | ktime_t expires; | 616 | ktime_t expires; |
@@ -529,9 +621,11 @@ void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta) | |||
529 | return; | 621 | return; |
530 | } | 622 | } |
531 | 623 | ||
532 | /* We use wallclock time here, so the Guest might not be running for | 624 | /* |
625 | * We use wallclock time here, so the Guest might not be running for | ||
533 | * all the time between now and the timer interrupt it asked for. This | 626 | * all the time between now and the timer interrupt it asked for. This |
534 | * is almost always the right thing to do. */ | 627 | * is almost always the right thing to do. |
628 | */ | ||
535 | expires = ktime_add_ns(ktime_get_real(), delta); | 629 | expires = ktime_add_ns(ktime_get_real(), delta); |
536 | hrtimer_start(&cpu->hrt, expires, HRTIMER_MODE_ABS); | 630 | hrtimer_start(&cpu->hrt, expires, HRTIMER_MODE_ABS); |
537 | } | 631 | } |
diff --git a/drivers/lguest/lg.h b/drivers/lguest/lg.h index 01c591923793..74c0db691b53 100644 --- a/drivers/lguest/lg.h +++ b/drivers/lguest/lg.h | |||
@@ -54,13 +54,13 @@ struct lg_cpu { | |||
54 | 54 | ||
55 | unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */ | 55 | unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */ |
56 | 56 | ||
57 | /* At end of a page shared mapped over lguest_pages in guest. */ | 57 | /* At end of a page shared mapped over lguest_pages in guest. */ |
58 | unsigned long regs_page; | 58 | unsigned long regs_page; |
59 | struct lguest_regs *regs; | 59 | struct lguest_regs *regs; |
60 | 60 | ||
61 | struct lguest_pages *last_pages; | 61 | struct lguest_pages *last_pages; |
62 | 62 | ||
63 | int cpu_pgd; /* which pgd this cpu is currently using */ | 63 | int cpu_pgd; /* Which pgd this cpu is currently using */ |
64 | 64 | ||
65 | /* If a hypercall was asked for, this points to the arguments. */ | 65 | /* If a hypercall was asked for, this points to the arguments. */ |
66 | struct hcall_args *hcall; | 66 | struct hcall_args *hcall; |
@@ -96,8 +96,11 @@ struct lguest | |||
96 | unsigned int nr_cpus; | 96 | unsigned int nr_cpus; |
97 | 97 | ||
98 | u32 pfn_limit; | 98 | u32 pfn_limit; |
99 | /* This provides the offset to the base of guest-physical | 99 | |
100 | * memory in the Launcher. */ | 100 | /* |
101 | * This provides the offset to the base of guest-physical memory in the | ||
102 | * Launcher. | ||
103 | */ | ||
101 | void __user *mem_base; | 104 | void __user *mem_base; |
102 | unsigned long kernel_address; | 105 | unsigned long kernel_address; |
103 | 106 | ||
@@ -122,11 +125,13 @@ bool lguest_address_ok(const struct lguest *lg, | |||
122 | void __lgread(struct lg_cpu *, void *, unsigned long, unsigned); | 125 | void __lgread(struct lg_cpu *, void *, unsigned long, unsigned); |
123 | void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); | 126 | void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); |
124 | 127 | ||
125 | /*H:035 Using memory-copy operations like that is usually inconvient, so we | 128 | /*H:035 |
129 | * Using memory-copy operations like that is usually inconvient, so we | ||
126 | * have the following helper macros which read and write a specific type (often | 130 | * have the following helper macros which read and write a specific type (often |
127 | * an unsigned long). | 131 | * an unsigned long). |
128 | * | 132 | * |
129 | * This reads into a variable of the given type then returns that. */ | 133 | * This reads into a variable of the given type then returns that. |
134 | */ | ||
130 | #define lgread(cpu, addr, type) \ | 135 | #define lgread(cpu, addr, type) \ |
131 | ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; }) | 136 | ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; }) |
132 | 137 | ||
@@ -140,9 +145,11 @@ void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); | |||
140 | 145 | ||
141 | int run_guest(struct lg_cpu *cpu, unsigned long __user *user); | 146 | int run_guest(struct lg_cpu *cpu, unsigned long __user *user); |
142 | 147 | ||
143 | /* Helper macros to obtain the first 12 or the last 20 bits, this is only the | 148 | /* |
149 | * Helper macros to obtain the first 12 or the last 20 bits, this is only the | ||
144 | * first step in the migration to the kernel types. pte_pfn is already defined | 150 | * first step in the migration to the kernel types. pte_pfn is already defined |
145 | * in the kernel. */ | 151 | * in the kernel. |
152 | */ | ||
146 | #define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK) | 153 | #define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK) |
147 | #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT) | 154 | #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT) |
148 | #define pmd_flags(x) (pmd_val(x) & ~PAGE_MASK) | 155 | #define pmd_flags(x) (pmd_val(x) & ~PAGE_MASK) |
diff --git a/drivers/lguest/lguest_device.c b/drivers/lguest/lguest_device.c index e082cdac88b4..cc000e79c3d1 100644 --- a/drivers/lguest/lguest_device.c +++ b/drivers/lguest/lguest_device.c | |||
@@ -1,10 +1,12 @@ | |||
1 | /*P:050 Lguest guests use a very simple method to describe devices. It's a | 1 | /*P:050 |
2 | * 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 | 3 | * series of device descriptors contained just above the top of normal Guest |
3 | * memory. | 4 | * memory. |
4 | * | 5 | * |
5 | * We use the standard "virtio" device infrastructure, which provides us with a | 6 | * We use the standard "virtio" device infrastructure, which provides us with a |
6 | * console, a network and a block driver. Each one expects some configuration | 7 | * console, a network and a block driver. Each one expects some configuration |
7 | * information and a "virtqueue" or two to send and receive data. :*/ | 8 | * information and a "virtqueue" or two to send and receive data. |
9 | :*/ | ||
8 | #include <linux/init.h> | 10 | #include <linux/init.h> |
9 | #include <linux/bootmem.h> | 11 | #include <linux/bootmem.h> |
10 | #include <linux/lguest_launcher.h> | 12 | #include <linux/lguest_launcher.h> |
@@ -20,8 +22,10 @@ | |||
20 | /* The pointer to our (page) of device descriptions. */ | 22 | /* The pointer to our (page) of device descriptions. */ |
21 | static void *lguest_devices; | 23 | static void *lguest_devices; |
22 | 24 | ||
23 | /* For Guests, device memory can be used as normal memory, so we cast away the | 25 | /* |
24 | * __iomem to quieten sparse. */ | 26 | * For Guests, device memory can be used as normal memory, so we cast away the |
27 | * __iomem to quieten sparse. | ||
28 | */ | ||
25 | static inline void *lguest_map(unsigned long phys_addr, unsigned long pages) | 29 | static inline void *lguest_map(unsigned long phys_addr, unsigned long pages) |
26 | { | 30 | { |
27 | return (__force void *)ioremap_cache(phys_addr, PAGE_SIZE*pages); | 31 | return (__force void *)ioremap_cache(phys_addr, PAGE_SIZE*pages); |
@@ -32,8 +36,10 @@ static inline void lguest_unmap(void *addr) | |||
32 | iounmap((__force void __iomem *)addr); | 36 | iounmap((__force void __iomem *)addr); |
33 | } | 37 | } |
34 | 38 | ||
35 | /*D:100 Each lguest device is just a virtio device plus a pointer to its entry | 39 | /*D:100 |
36 | * in the lguest_devices page. */ | 40 | * Each lguest device is just a virtio device plus a pointer to its entry |
41 | * in the lguest_devices page. | ||
42 | */ | ||
37 | struct lguest_device { | 43 | struct lguest_device { |
38 | struct virtio_device vdev; | 44 | struct virtio_device vdev; |
39 | 45 | ||
@@ -41,9 +47,11 @@ struct lguest_device { | |||
41 | struct lguest_device_desc *desc; | 47 | struct lguest_device_desc *desc; |
42 | }; | 48 | }; |
43 | 49 | ||
44 | /* Since the virtio infrastructure hands us a pointer to the virtio_device all | 50 | /* |
51 | * Since the virtio infrastructure hands us a pointer to the virtio_device all | ||
45 | * the time, it helps to have a curt macro to get a pointer to the struct | 52 | * the time, it helps to have a curt macro to get a pointer to the struct |
46 | * lguest_device it's enclosed in. */ | 53 | * lguest_device it's enclosed in. |
54 | */ | ||
47 | #define to_lgdev(vd) container_of(vd, struct lguest_device, vdev) | 55 | #define to_lgdev(vd) container_of(vd, struct lguest_device, vdev) |
48 | 56 | ||
49 | /*D:130 | 57 | /*D:130 |
@@ -55,7 +63,8 @@ struct lguest_device { | |||
55 | * the driver will look at them during setup. | 63 | * the driver will look at them during setup. |
56 | * | 64 | * |
57 | * A convenient routine to return the device's virtqueue config array: | 65 | * A convenient routine to return the device's virtqueue config array: |
58 | * immediately after the descriptor. */ | 66 | * immediately after the descriptor. |
67 | */ | ||
59 | static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc) | 68 | static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc) |
60 | { | 69 | { |
61 | return (void *)(desc + 1); | 70 | return (void *)(desc + 1); |
@@ -98,10 +107,12 @@ static u32 lg_get_features(struct virtio_device *vdev) | |||
98 | return features; | 107 | return features; |
99 | } | 108 | } |
100 | 109 | ||
101 | /* The virtio core takes the features the Host offers, and copies the | 110 | /* |
102 | * ones supported by the driver into the vdev->features array. Once | 111 | * The virtio core takes the features the Host offers, and copies the ones |
103 | * that's all sorted out, this routine is called so we can tell the | 112 | * supported by the driver into the vdev->features array. Once that's all |
104 | * Host which features we understand and accept. */ | 113 | * sorted out, this routine is called so we can tell the Host which features we |
114 | * understand and accept. | ||
115 | */ | ||
105 | static void lg_finalize_features(struct virtio_device *vdev) | 116 | static void lg_finalize_features(struct virtio_device *vdev) |
106 | { | 117 | { |
107 | unsigned int i, bits; | 118 | unsigned int i, bits; |
@@ -112,10 +123,11 @@ static void lg_finalize_features(struct virtio_device *vdev) | |||
112 | /* Give virtio_ring a chance to accept features. */ | 123 | /* Give virtio_ring a chance to accept features. */ |
113 | vring_transport_features(vdev); | 124 | vring_transport_features(vdev); |
114 | 125 | ||
115 | /* The vdev->feature array is a Linux bitmask: this isn't the | 126 | /* |
116 | * same as a the simple array of bits used by lguest devices | 127 | * The vdev->feature array is a Linux bitmask: this isn't the same as a |
117 | * for features. So we do this slow, manual conversion which is | 128 | * the simple array of bits used by lguest devices for features. So we |
118 | * completely general. */ | 129 | * do this slow, manual conversion which is completely general. |
130 | */ | ||
119 | memset(out_features, 0, desc->feature_len); | 131 | memset(out_features, 0, desc->feature_len); |
120 | bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8; | 132 | bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8; |
121 | for (i = 0; i < bits; i++) { | 133 | for (i = 0; i < bits; i++) { |
@@ -146,15 +158,19 @@ static void lg_set(struct virtio_device *vdev, unsigned int offset, | |||
146 | memcpy(lg_config(desc) + offset, buf, len); | 158 | memcpy(lg_config(desc) + offset, buf, len); |
147 | } | 159 | } |
148 | 160 | ||
149 | /* The operations to get and set the status word just access the status field | 161 | /* |
150 | * of the device descriptor. */ | 162 | * The operations to get and set the status word just access the status field |
163 | * of the device descriptor. | ||
164 | */ | ||
151 | static u8 lg_get_status(struct virtio_device *vdev) | 165 | static u8 lg_get_status(struct virtio_device *vdev) |
152 | { | 166 | { |
153 | return to_lgdev(vdev)->desc->status; | 167 | return to_lgdev(vdev)->desc->status; |
154 | } | 168 | } |
155 | 169 | ||
156 | /* To notify on status updates, we (ab)use the NOTIFY hypercall, with the | 170 | /* |
157 | * descriptor address of the device. A zero status means "reset". */ | 171 | * To notify on status updates, we (ab)use the NOTIFY hypercall, with the |
172 | * descriptor address of the device. A zero status means "reset". | ||
173 | */ | ||
158 | static void set_status(struct virtio_device *vdev, u8 status) | 174 | static void set_status(struct virtio_device *vdev, u8 status) |
159 | { | 175 | { |
160 | unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices; | 176 | unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices; |
@@ -200,13 +216,17 @@ struct lguest_vq_info | |||
200 | void *pages; | 216 | void *pages; |
201 | }; | 217 | }; |
202 | 218 | ||
203 | /* When the virtio_ring code wants to prod the Host, it calls us here and we | 219 | /* |
220 | * When the virtio_ring code wants to prod the Host, it calls us here and we | ||
204 | * make a hypercall. We hand the physical address of the virtqueue so the Host | 221 | * make a hypercall. We hand the physical address of the virtqueue so the Host |
205 | * knows which virtqueue we're talking about. */ | 222 | * knows which virtqueue we're talking about. |
223 | */ | ||
206 | static void lg_notify(struct virtqueue *vq) | 224 | static void lg_notify(struct virtqueue *vq) |
207 | { | 225 | { |
208 | /* We store our virtqueue information in the "priv" pointer of the | 226 | /* |
209 | * virtqueue structure. */ | 227 | * We store our virtqueue information in the "priv" pointer of the |
228 | * virtqueue structure. | ||
229 | */ | ||
210 | struct lguest_vq_info *lvq = vq->priv; | 230 | struct lguest_vq_info *lvq = vq->priv; |
211 | 231 | ||
212 | kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT); | 232 | kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT); |
@@ -215,7 +235,8 @@ static void lg_notify(struct virtqueue *vq) | |||
215 | /* An extern declaration inside a C file is bad form. Don't do it. */ | 235 | /* An extern declaration inside a C file is bad form. Don't do it. */ |
216 | extern void lguest_setup_irq(unsigned int irq); | 236 | extern void lguest_setup_irq(unsigned int irq); |
217 | 237 | ||
218 | /* This routine finds the first virtqueue described in the configuration of | 238 | /* |
239 | * This routine finds the first virtqueue described in the configuration of | ||
219 | * this device and sets it up. | 240 | * this device and sets it up. |
220 | * | 241 | * |
221 | * This is kind of an ugly duckling. It'd be nicer to have a standard | 242 | * This is kind of an ugly duckling. It'd be nicer to have a standard |
@@ -225,7 +246,8 @@ extern void lguest_setup_irq(unsigned int irq); | |||
225 | * simpler for the Host to simply tell us where the pages are. | 246 | * simpler for the Host to simply tell us where the pages are. |
226 | * | 247 | * |
227 | * So we provide drivers with a "find the Nth virtqueue and set it up" | 248 | * So we provide drivers with a "find the Nth virtqueue and set it up" |
228 | * function. */ | 249 | * function. |
250 | */ | ||
229 | static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | 251 | static struct virtqueue *lg_find_vq(struct virtio_device *vdev, |
230 | unsigned index, | 252 | unsigned index, |
231 | void (*callback)(struct virtqueue *vq), | 253 | void (*callback)(struct virtqueue *vq), |
@@ -244,9 +266,11 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | |||
244 | if (!lvq) | 266 | if (!lvq) |
245 | return ERR_PTR(-ENOMEM); | 267 | return ERR_PTR(-ENOMEM); |
246 | 268 | ||
247 | /* Make a copy of the "struct lguest_vqconfig" entry, which sits after | 269 | /* |
270 | * Make a copy of the "struct lguest_vqconfig" entry, which sits after | ||
248 | * the descriptor. We need a copy because the config space might not | 271 | * the descriptor. We need a copy because the config space might not |
249 | * be aligned correctly. */ | 272 | * be aligned correctly. |
273 | */ | ||
250 | memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config)); | 274 | memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config)); |
251 | 275 | ||
252 | printk("Mapping virtqueue %i addr %lx\n", index, | 276 | printk("Mapping virtqueue %i addr %lx\n", index, |
@@ -261,8 +285,10 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | |||
261 | goto free_lvq; | 285 | goto free_lvq; |
262 | } | 286 | } |
263 | 287 | ||
264 | /* OK, tell virtio_ring.c to set up a virtqueue now we know its size | 288 | /* |
265 | * and we've got a pointer to its pages. */ | 289 | * OK, tell virtio_ring.c to set up a virtqueue now we know its size |
290 | * and we've got a pointer to its pages. | ||
291 | */ | ||
266 | vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN, | 292 | vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN, |
267 | vdev, lvq->pages, lg_notify, callback, name); | 293 | vdev, lvq->pages, lg_notify, callback, name); |
268 | if (!vq) { | 294 | if (!vq) { |
@@ -273,18 +299,23 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | |||
273 | /* Make sure the interrupt is allocated. */ | 299 | /* Make sure the interrupt is allocated. */ |
274 | lguest_setup_irq(lvq->config.irq); | 300 | lguest_setup_irq(lvq->config.irq); |
275 | 301 | ||
276 | /* Tell the interrupt for this virtqueue to go to the virtio_ring | 302 | /* |
277 | * interrupt handler. */ | 303 | * Tell the interrupt for this virtqueue to go to the virtio_ring |
278 | /* FIXME: We used to have a flag for the Host to tell us we could use | 304 | * interrupt handler. |
305 | * | ||
306 | * FIXME: We used to have a flag for the Host to tell us we could use | ||
279 | * the interrupt as a source of randomness: it'd be nice to have that | 307 | * the interrupt as a source of randomness: it'd be nice to have that |
280 | * back.. */ | 308 | * back. |
309 | */ | ||
281 | err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED, | 310 | err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED, |
282 | dev_name(&vdev->dev), vq); | 311 | dev_name(&vdev->dev), vq); |
283 | if (err) | 312 | if (err) |
284 | goto destroy_vring; | 313 | goto destroy_vring; |
285 | 314 | ||
286 | /* Last of all we hook up our 'struct lguest_vq_info" to the | 315 | /* |
287 | * virtqueue's priv pointer. */ | 316 | * Last of all we hook up our 'struct lguest_vq_info" to the |
317 | * virtqueue's priv pointer. | ||
318 | */ | ||
288 | vq->priv = lvq; | 319 | vq->priv = lvq; |
289 | return vq; | 320 | return vq; |
290 | 321 | ||
@@ -358,11 +389,14 @@ static struct virtio_config_ops lguest_config_ops = { | |||
358 | .del_vqs = lg_del_vqs, | 389 | .del_vqs = lg_del_vqs, |
359 | }; | 390 | }; |
360 | 391 | ||
361 | /* The root device for the lguest virtio devices. This makes them appear as | 392 | /* |
362 | * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */ | 393 | * The root device for the lguest virtio devices. This makes them appear as |
394 | * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. | ||
395 | */ | ||
363 | static struct device *lguest_root; | 396 | static struct device *lguest_root; |
364 | 397 | ||
365 | /*D:120 This is the core of the lguest bus: actually adding a new device. | 398 | /*D:120 |
399 | * This is the core of the lguest bus: actually adding a new device. | ||
366 | * It's a separate function because it's neater that way, and because an | 400 | * It's a separate function because it's neater that way, and because an |
367 | * earlier version of the code supported hotplug and unplug. They were removed | 401 | * earlier version of the code supported hotplug and unplug. They were removed |
368 | * early on because they were never used. | 402 | * early on because they were never used. |
@@ -371,14 +405,14 @@ static struct device *lguest_root; | |||
371 | * | 405 | * |
372 | * It's worth reading this carefully: we start with a pointer to the new device | 406 | * It's worth reading this carefully: we start with a pointer to the new device |
373 | * descriptor in the "lguest_devices" page, and the offset into the device | 407 | * descriptor in the "lguest_devices" page, and the offset into the device |
374 | * descriptor page so we can uniquely identify it if things go badly wrong. */ | 408 | * descriptor page so we can uniquely identify it if things go badly wrong. |
409 | */ | ||
375 | static void add_lguest_device(struct lguest_device_desc *d, | 410 | static void add_lguest_device(struct lguest_device_desc *d, |
376 | unsigned int offset) | 411 | unsigned int offset) |
377 | { | 412 | { |
378 | struct lguest_device *ldev; | 413 | struct lguest_device *ldev; |
379 | 414 | ||
380 | /* Start with zeroed memory; Linux's device layer seems to count on | 415 | /* Start with zeroed memory; Linux's device layer counts on it. */ |
381 | * it. */ | ||
382 | ldev = kzalloc(sizeof(*ldev), GFP_KERNEL); | 416 | ldev = kzalloc(sizeof(*ldev), GFP_KERNEL); |
383 | if (!ldev) { | 417 | if (!ldev) { |
384 | printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n", | 418 | printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n", |
@@ -390,15 +424,19 @@ static void add_lguest_device(struct lguest_device_desc *d, | |||
390 | ldev->vdev.dev.parent = lguest_root; | 424 | ldev->vdev.dev.parent = lguest_root; |
391 | /* We have a unique device index thanks to the dev_index counter. */ | 425 | /* We have a unique device index thanks to the dev_index counter. */ |
392 | ldev->vdev.id.device = d->type; | 426 | ldev->vdev.id.device = d->type; |
393 | /* We have a simple set of routines for querying the device's | 427 | /* |
394 | * configuration information and setting its status. */ | 428 | * We have a simple set of routines for querying the device's |
429 | * configuration information and setting its status. | ||
430 | */ | ||
395 | ldev->vdev.config = &lguest_config_ops; | 431 | ldev->vdev.config = &lguest_config_ops; |
396 | /* And we remember the device's descriptor for lguest_config_ops. */ | 432 | /* And we remember the device's descriptor for lguest_config_ops. */ |
397 | ldev->desc = d; | 433 | ldev->desc = d; |
398 | 434 | ||
399 | /* register_virtio_device() sets up the generic fields for the struct | 435 | /* |
436 | * register_virtio_device() sets up the generic fields for the struct | ||
400 | * virtio_device and calls device_register(). This makes the bus | 437 | * virtio_device and calls device_register(). This makes the bus |
401 | * infrastructure look for a matching driver. */ | 438 | * infrastructure look for a matching driver. |
439 | */ | ||
402 | if (register_virtio_device(&ldev->vdev) != 0) { | 440 | if (register_virtio_device(&ldev->vdev) != 0) { |
403 | printk(KERN_ERR "Failed to register lguest dev %u type %u\n", | 441 | printk(KERN_ERR "Failed to register lguest dev %u type %u\n", |
404 | offset, d->type); | 442 | offset, d->type); |
@@ -406,8 +444,10 @@ static void add_lguest_device(struct lguest_device_desc *d, | |||
406 | } | 444 | } |
407 | } | 445 | } |
408 | 446 | ||
409 | /*D:110 scan_devices() simply iterates through the device page. The type 0 is | 447 | /*D:110 |
410 | * reserved to mean "end of devices". */ | 448 | * scan_devices() simply iterates through the device page. The type 0 is |
449 | * reserved to mean "end of devices". | ||
450 | */ | ||
411 | static void scan_devices(void) | 451 | static void scan_devices(void) |
412 | { | 452 | { |
413 | unsigned int i; | 453 | unsigned int i; |
@@ -426,7 +466,8 @@ static void scan_devices(void) | |||
426 | } | 466 | } |
427 | } | 467 | } |
428 | 468 | ||
429 | /*D:105 Fairly early in boot, lguest_devices_init() is called to set up the | 469 | /*D:105 |
470 | * Fairly early in boot, lguest_devices_init() is called to set up the | ||
430 | * lguest device infrastructure. We check that we are a Guest by checking | 471 | * lguest device infrastructure. We check that we are a Guest by checking |
431 | * pv_info.name: there are other ways of checking, but this seems most | 472 | * pv_info.name: there are other ways of checking, but this seems most |
432 | * obvious to me. | 473 | * obvious to me. |
@@ -437,7 +478,8 @@ static void scan_devices(void) | |||
437 | * correct sysfs incantation). | 478 | * correct sysfs incantation). |
438 | * | 479 | * |
439 | * Finally we call scan_devices() which adds all the devices found in the | 480 | * Finally we call scan_devices() which adds all the devices found in the |
440 | * lguest_devices page. */ | 481 | * lguest_devices page. |
482 | */ | ||
441 | static int __init lguest_devices_init(void) | 483 | static int __init lguest_devices_init(void) |
442 | { | 484 | { |
443 | if (strcmp(pv_info.name, "lguest") != 0) | 485 | if (strcmp(pv_info.name, "lguest") != 0) |
@@ -456,11 +498,13 @@ static int __init lguest_devices_init(void) | |||
456 | /* We do this after core stuff, but before the drivers. */ | 498 | /* We do this after core stuff, but before the drivers. */ |
457 | postcore_initcall(lguest_devices_init); | 499 | postcore_initcall(lguest_devices_init); |
458 | 500 | ||
459 | /*D:150 At this point in the journey we used to now wade through the lguest | 501 | /*D:150 |
502 | * At this point in the journey we used to now wade through the lguest | ||
460 | * devices themselves: net, block and console. Since they're all now virtio | 503 | * devices themselves: net, block and console. Since they're all now virtio |
461 | * devices rather than lguest-specific, I've decided to ignore them. Mostly, | 504 | * devices rather than lguest-specific, I've decided to ignore them. Mostly, |
462 | * they're kind of boring. But this does mean you'll never experience the | 505 | * they're kind of boring. But this does mean you'll never experience the |
463 | * thrill of reading the forbidden love scene buried deep in the block driver. | 506 | * thrill of reading the forbidden love scene buried deep in the block driver. |
464 | * | 507 | * |
465 | * "make Launcher" beckons, where we answer questions like "Where do Guests | 508 | * "make Launcher" beckons, where we answer questions like "Where do Guests |
466 | * come from?", and "What do you do when someone asks for optimization?". */ | 509 | * come from?", and "What do you do when someone asks for optimization?". |
510 | */ | ||
diff --git a/drivers/lguest/lguest_user.c b/drivers/lguest/lguest_user.c index 407722a8e0c4..7e92017103dc 100644 --- a/drivers/lguest/lguest_user.c +++ b/drivers/lguest/lguest_user.c | |||
@@ -1,8 +1,10 @@ | |||
1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher | 1 | /*P:200 |
2 | * This contains all the /dev/lguest code, whereby the userspace launcher | ||
2 | * controls and communicates with the Guest. For example, the first write will | 3 | * controls and communicates with the Guest. For example, the first write will |
3 | * tell us the Guest's memory layout, pagetable, entry point and kernel address | 4 | * tell us the Guest's memory layout, pagetable, entry point and kernel address |
4 | * offset. A read will run the Guest until something happens, such as a signal | 5 | * 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. :*/ | 6 | * or the Guest doing a NOTIFY out to the Launcher. |
7 | :*/ | ||
6 | #include <linux/uaccess.h> | 8 | #include <linux/uaccess.h> |
7 | #include <linux/miscdevice.h> | 9 | #include <linux/miscdevice.h> |
8 | #include <linux/fs.h> | 10 | #include <linux/fs.h> |
@@ -37,8 +39,10 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) | |||
37 | if (!addr) | 39 | if (!addr) |
38 | return -EINVAL; | 40 | return -EINVAL; |
39 | 41 | ||
40 | /* Replace the old array with the new one, carefully: others can | 42 | /* |
41 | * be accessing it at the same time */ | 43 | * Replace the old array with the new one, carefully: others can |
44 | * be accessing it at the same time. | ||
45 | */ | ||
42 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), | 46 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), |
43 | GFP_KERNEL); | 47 | GFP_KERNEL); |
44 | if (!new) | 48 | if (!new) |
@@ -61,8 +65,10 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) | |||
61 | /* Now put new one in place. */ | 65 | /* Now put new one in place. */ |
62 | rcu_assign_pointer(lg->eventfds, new); | 66 | rcu_assign_pointer(lg->eventfds, new); |
63 | 67 | ||
64 | /* We're not in a big hurry. Wait until noone's looking at old | 68 | /* |
65 | * version, then delete it. */ | 69 | * We're not in a big hurry. Wait until noone's looking at old |
70 | * version, then delete it. | ||
71 | */ | ||
66 | synchronize_rcu(); | 72 | synchronize_rcu(); |
67 | kfree(old); | 73 | kfree(old); |
68 | 74 | ||
@@ -87,8 +93,10 @@ static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) | |||
87 | return err; | 93 | return err; |
88 | } | 94 | } |
89 | 95 | ||
90 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt | 96 | /*L:050 |
91 | * number to /dev/lguest. */ | 97 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
98 | * number to /dev/lguest. | ||
99 | */ | ||
92 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) | 100 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
93 | { | 101 | { |
94 | unsigned long irq; | 102 | unsigned long irq; |
@@ -102,8 +110,10 @@ static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) | |||
102 | return 0; | 110 | return 0; |
103 | } | 111 | } |
104 | 112 | ||
105 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading | 113 | /*L:040 |
106 | * from /dev/lguest. */ | 114 | * Once our Guest is initialized, the Launcher makes it run by reading |
115 | * from /dev/lguest. | ||
116 | */ | ||
107 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | 117 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
108 | { | 118 | { |
109 | struct lguest *lg = file->private_data; | 119 | struct lguest *lg = file->private_data; |
@@ -139,8 +149,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
139 | return len; | 149 | return len; |
140 | } | 150 | } |
141 | 151 | ||
142 | /* If we returned from read() last time because the Guest sent I/O, | 152 | /* |
143 | * clear the flag. */ | 153 | * If we returned from read() last time because the Guest sent I/O, |
154 | * clear the flag. | ||
155 | */ | ||
144 | if (cpu->pending_notify) | 156 | if (cpu->pending_notify) |
145 | cpu->pending_notify = 0; | 157 | cpu->pending_notify = 0; |
146 | 158 | ||
@@ -148,8 +160,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
148 | return run_guest(cpu, (unsigned long __user *)user); | 160 | return run_guest(cpu, (unsigned long __user *)user); |
149 | } | 161 | } |
150 | 162 | ||
151 | /*L:025 This actually initializes a CPU. For the moment, a Guest is only | 163 | /*L:025 |
152 | * uniprocessor, so "id" is always 0. */ | 164 | * This actually initializes a CPU. For the moment, a Guest is only |
165 | * uniprocessor, so "id" is always 0. | ||
166 | */ | ||
153 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | 167 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
154 | { | 168 | { |
155 | /* We have a limited number the number of CPUs in the lguest struct. */ | 169 | /* We have a limited number the number of CPUs in the lguest struct. */ |
@@ -164,8 +178,10 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
164 | /* Each CPU has a timer it can set. */ | 178 | /* Each CPU has a timer it can set. */ |
165 | init_clockdev(cpu); | 179 | init_clockdev(cpu); |
166 | 180 | ||
167 | /* We need a complete page for the Guest registers: they are accessible | 181 | /* |
168 | * to the Guest and we can only grant it access to whole pages. */ | 182 | * We need a complete page for the Guest registers: they are accessible |
183 | * to the Guest and we can only grant it access to whole pages. | ||
184 | */ | ||
169 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); | 185 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
170 | if (!cpu->regs_page) | 186 | if (!cpu->regs_page) |
171 | return -ENOMEM; | 187 | return -ENOMEM; |
@@ -173,29 +189,38 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
173 | /* We actually put the registers at the bottom of the page. */ | 189 | /* We actually put the registers at the bottom of the page. */ |
174 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); | 190 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
175 | 191 | ||
176 | /* Now we initialize the Guest's registers, handing it the start | 192 | /* |
177 | * address. */ | 193 | * Now we initialize the Guest's registers, handing it the start |
194 | * address. | ||
195 | */ | ||
178 | lguest_arch_setup_regs(cpu, start_ip); | 196 | lguest_arch_setup_regs(cpu, start_ip); |
179 | 197 | ||
180 | /* We keep a pointer to the Launcher task (ie. current task) for when | 198 | /* |
181 | * other Guests want to wake this one (eg. console input). */ | 199 | * We keep a pointer to the Launcher task (ie. current task) for when |
200 | * other Guests want to wake this one (eg. console input). | ||
201 | */ | ||
182 | cpu->tsk = current; | 202 | cpu->tsk = current; |
183 | 203 | ||
184 | /* We need to keep a pointer to the Launcher's memory map, because if | 204 | /* |
205 | * We need to keep a pointer to the Launcher's memory map, because if | ||
185 | * the Launcher dies we need to clean it up. If we don't keep a | 206 | * the Launcher dies we need to clean it up. If we don't keep a |
186 | * reference, it is destroyed before close() is called. */ | 207 | * reference, it is destroyed before close() is called. |
208 | */ | ||
187 | cpu->mm = get_task_mm(cpu->tsk); | 209 | cpu->mm = get_task_mm(cpu->tsk); |
188 | 210 | ||
189 | /* We remember which CPU's pages this Guest used last, for optimization | 211 | /* |
190 | * when the same Guest runs on the same CPU twice. */ | 212 | * We remember which CPU's pages this Guest used last, for optimization |
213 | * when the same Guest runs on the same CPU twice. | ||
214 | */ | ||
191 | cpu->last_pages = NULL; | 215 | cpu->last_pages = NULL; |
192 | 216 | ||
193 | /* No error == success. */ | 217 | /* No error == success. */ |
194 | return 0; | 218 | return 0; |
195 | } | 219 | } |
196 | 220 | ||
197 | /*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit) | 221 | /*L:020 |
198 | * values (in addition to the LHREQ_INITIALIZE value). These are: | 222 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in |
223 | * addition to the LHREQ_INITIALIZE value). These are: | ||
199 | * | 224 | * |
200 | * base: The start of the Guest-physical memory inside the Launcher memory. | 225 | * base: The start of the Guest-physical memory inside the Launcher memory. |
201 | * | 226 | * |
@@ -207,14 +232,15 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
207 | */ | 232 | */ |
208 | static int initialize(struct file *file, const unsigned long __user *input) | 233 | static int initialize(struct file *file, const unsigned long __user *input) |
209 | { | 234 | { |
210 | /* "struct lguest" contains everything we (the Host) know about a | 235 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
211 | * Guest. */ | ||
212 | struct lguest *lg; | 236 | struct lguest *lg; |
213 | int err; | 237 | int err; |
214 | unsigned long args[3]; | 238 | unsigned long args[3]; |
215 | 239 | ||
216 | /* We grab the Big Lguest lock, which protects against multiple | 240 | /* |
217 | * simultaneous initializations. */ | 241 | * We grab the Big Lguest lock, which protects against multiple |
242 | * simultaneous initializations. | ||
243 | */ | ||
218 | mutex_lock(&lguest_lock); | 244 | mutex_lock(&lguest_lock); |
219 | /* You can't initialize twice! Close the device and start again... */ | 245 | /* You can't initialize twice! Close the device and start again... */ |
220 | if (file->private_data) { | 246 | if (file->private_data) { |
@@ -249,8 +275,10 @@ static int initialize(struct file *file, const unsigned long __user *input) | |||
249 | if (err) | 275 | if (err) |
250 | goto free_eventfds; | 276 | goto free_eventfds; |
251 | 277 | ||
252 | /* Initialize the Guest's shadow page tables, using the toplevel | 278 | /* |
253 | * address the Launcher gave us. This allocates memory, so can fail. */ | 279 | * Initialize the Guest's shadow page tables, using the toplevel |
280 | * address the Launcher gave us. This allocates memory, so can fail. | ||
281 | */ | ||
254 | err = init_guest_pagetable(lg); | 282 | err = init_guest_pagetable(lg); |
255 | if (err) | 283 | if (err) |
256 | goto free_regs; | 284 | goto free_regs; |
@@ -275,7 +303,8 @@ unlock: | |||
275 | return err; | 303 | return err; |
276 | } | 304 | } |
277 | 305 | ||
278 | /*L:010 The first operation the Launcher does must be a write. All writes | 306 | /*L:010 |
307 | * The first operation the Launcher does must be a write. All writes | ||
279 | * start with an unsigned long number: for the first write this must be | 308 | * start with an unsigned long number: for the first write this must be |
280 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use | 309 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
281 | * writes of other values to send interrupts. | 310 | * writes of other values to send interrupts. |
@@ -283,12 +312,15 @@ unlock: | |||
283 | * Note that we overload the "offset" in the /dev/lguest file to indicate what | 312 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
284 | * CPU number we're dealing with. Currently this is always 0, since we only | 313 | * CPU number we're dealing with. Currently this is always 0, since we only |
285 | * support uniprocessor Guests, but you can see the beginnings of SMP support | 314 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
286 | * here. */ | 315 | * here. |
316 | */ | ||
287 | static ssize_t write(struct file *file, const char __user *in, | 317 | static ssize_t write(struct file *file, const char __user *in, |
288 | size_t size, loff_t *off) | 318 | size_t size, loff_t *off) |
289 | { | 319 | { |
290 | /* Once the Guest is initialized, we hold the "struct lguest" in the | 320 | /* |
291 | * file private data. */ | 321 | * Once the Guest is initialized, we hold the "struct lguest" in the |
322 | * file private data. | ||
323 | */ | ||
292 | struct lguest *lg = file->private_data; | 324 | struct lguest *lg = file->private_data; |
293 | const unsigned long __user *input = (const unsigned long __user *)in; | 325 | const unsigned long __user *input = (const unsigned long __user *)in; |
294 | unsigned long req; | 326 | unsigned long req; |
@@ -323,13 +355,15 @@ static ssize_t write(struct file *file, const char __user *in, | |||
323 | } | 355 | } |
324 | } | 356 | } |
325 | 357 | ||
326 | /*L:060 The final piece of interface code is the close() routine. It reverses | 358 | /*L:060 |
359 | * The final piece of interface code is the close() routine. It reverses | ||
327 | * everything done in initialize(). This is usually called because the | 360 | * everything done in initialize(). This is usually called because the |
328 | * Launcher exited. | 361 | * Launcher exited. |
329 | * | 362 | * |
330 | * Note that the close routine returns 0 or a negative error number: it can't | 363 | * Note that the close routine returns 0 or a negative error number: it can't |
331 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for | 364 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
332 | * letting them do it. :*/ | 365 | * letting them do it. |
366 | :*/ | ||
333 | static int close(struct inode *inode, struct file *file) | 367 | static int close(struct inode *inode, struct file *file) |
334 | { | 368 | { |
335 | struct lguest *lg = file->private_data; | 369 | struct lguest *lg = file->private_data; |
@@ -339,8 +373,10 @@ static int close(struct inode *inode, struct file *file) | |||
339 | if (!lg) | 373 | if (!lg) |
340 | return 0; | 374 | return 0; |
341 | 375 | ||
342 | /* We need the big lock, to protect from inter-guest I/O and other | 376 | /* |
343 | * Launchers initializing guests. */ | 377 | * We need the big lock, to protect from inter-guest I/O and other |
378 | * Launchers initializing guests. | ||
379 | */ | ||
344 | mutex_lock(&lguest_lock); | 380 | mutex_lock(&lguest_lock); |
345 | 381 | ||
346 | /* Free up the shadow page tables for the Guest. */ | 382 | /* Free up the shadow page tables for the Guest. */ |
@@ -351,8 +387,10 @@ static int close(struct inode *inode, struct file *file) | |||
351 | hrtimer_cancel(&lg->cpus[i].hrt); | 387 | hrtimer_cancel(&lg->cpus[i].hrt); |
352 | /* We can free up the register page we allocated. */ | 388 | /* We can free up the register page we allocated. */ |
353 | free_page(lg->cpus[i].regs_page); | 389 | free_page(lg->cpus[i].regs_page); |
354 | /* Now all the memory cleanups are done, it's safe to release | 390 | /* |
355 | * the Launcher's memory management structure. */ | 391 | * Now all the memory cleanups are done, it's safe to release |
392 | * the Launcher's memory management structure. | ||
393 | */ | ||
356 | mmput(lg->cpus[i].mm); | 394 | mmput(lg->cpus[i].mm); |
357 | } | 395 | } |
358 | 396 | ||
@@ -361,8 +399,10 @@ static int close(struct inode *inode, struct file *file) | |||
361 | eventfd_ctx_put(lg->eventfds->map[i].event); | 399 | eventfd_ctx_put(lg->eventfds->map[i].event); |
362 | kfree(lg->eventfds); | 400 | kfree(lg->eventfds); |
363 | 401 | ||
364 | /* If lg->dead doesn't contain an error code it will be NULL or a | 402 | /* |
365 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ | 403 | * If lg->dead doesn't contain an error code it will be NULL or a |
404 | * kmalloc()ed string, either of which is ok to hand to kfree(). | ||
405 | */ | ||
366 | if (!IS_ERR(lg->dead)) | 406 | if (!IS_ERR(lg->dead)) |
367 | kfree(lg->dead); | 407 | kfree(lg->dead); |
368 | /* Free the memory allocated to the lguest_struct */ | 408 | /* Free the memory allocated to the lguest_struct */ |
@@ -386,7 +426,8 @@ static int close(struct inode *inode, struct file *file) | |||
386 | * | 426 | * |
387 | * We begin our understanding with the Host kernel interface which the Launcher | 427 | * We begin our understanding with the Host kernel interface which the Launcher |
388 | * uses: reading and writing a character device called /dev/lguest. All the | 428 | * uses: reading and writing a character device called /dev/lguest. All the |
389 | * work happens in the read(), write() and close() routines: */ | 429 | * work happens in the read(), write() and close() routines: |
430 | */ | ||
390 | static struct file_operations lguest_fops = { | 431 | static struct file_operations lguest_fops = { |
391 | .owner = THIS_MODULE, | 432 | .owner = THIS_MODULE, |
392 | .release = close, | 433 | .release = close, |
@@ -394,8 +435,10 @@ static struct file_operations lguest_fops = { | |||
394 | .read = read, | 435 | .read = read, |
395 | }; | 436 | }; |
396 | 437 | ||
397 | /* This is a textbook example of a "misc" character device. Populate a "struct | 438 | /* |
398 | * miscdevice" and register it with misc_register(). */ | 439 | * This is a textbook example of a "misc" character device. Populate a "struct |
440 | * miscdevice" and register it with misc_register(). | ||
441 | */ | ||
399 | static struct miscdevice lguest_dev = { | 442 | static struct miscdevice lguest_dev = { |
400 | .minor = MISC_DYNAMIC_MINOR, | 443 | .minor = MISC_DYNAMIC_MINOR, |
401 | .name = "lguest", | 444 | .name = "lguest", |
diff --git a/drivers/lguest/page_tables.c b/drivers/lguest/page_tables.c index a6fe1abda240..3da902e4b4cb 100644 --- a/drivers/lguest/page_tables.c +++ b/drivers/lguest/page_tables.c | |||
@@ -1,9 +1,11 @@ | |||
1 | /*P:700 The pagetable code, on the other hand, still shows the scars of | 1 | /*P:700 |
2 | * The pagetable code, on the other hand, still shows the scars of | ||
2 | * previous encounters. It's functional, and as neat as it can be in the | 3 | * previous encounters. It's functional, and as neat as it can be in the |
3 | * circumstances, but be wary, for these things are subtle and break easily. | 4 | * circumstances, but be wary, for these things are subtle and break easily. |
4 | * The Guest provides a virtual to physical mapping, but we can neither trust | 5 | * The Guest provides a virtual to physical mapping, but we can neither trust |
5 | * it nor use it: we verify and convert it here then point the CPU to the | 6 | * it nor use it: we verify and convert it here then point the CPU to the |
6 | * converted Guest pages when running the Guest. :*/ | 7 | * converted Guest pages when running the Guest. |
8 | :*/ | ||
7 | 9 | ||
8 | /* Copyright (C) Rusty Russell IBM Corporation 2006. | 10 | /* Copyright (C) Rusty Russell IBM Corporation 2006. |
9 | * GPL v2 and any later version */ | 11 | * GPL v2 and any later version */ |
@@ -17,10 +19,12 @@ | |||
17 | #include <asm/bootparam.h> | 19 | #include <asm/bootparam.h> |
18 | #include "lg.h" | 20 | #include "lg.h" |
19 | 21 | ||
20 | /*M:008 We hold reference to pages, which prevents them from being swapped. | 22 | /*M:008 |
23 | * We hold reference to pages, which prevents them from being swapped. | ||
21 | * It'd be nice to have a callback in the "struct mm_struct" when Linux wants | 24 | * It'd be nice to have a callback in the "struct mm_struct" when Linux wants |
22 | * to swap out. If we had this, and a shrinker callback to trim PTE pages, we | 25 | * to swap out. If we had this, and a shrinker callback to trim PTE pages, we |
23 | * could probably consider launching Guests as non-root. :*/ | 26 | * could probably consider launching Guests as non-root. |
27 | :*/ | ||
24 | 28 | ||
25 | /*H:300 | 29 | /*H:300 |
26 | * The Page Table Code | 30 | * The Page Table Code |
@@ -45,16 +49,19 @@ | |||
45 | * (v) Flushing (throwing away) page tables, | 49 | * (v) Flushing (throwing away) page tables, |
46 | * (vi) Mapping the Switcher when the Guest is about to run, | 50 | * (vi) Mapping the Switcher when the Guest is about to run, |
47 | * (vii) Setting up the page tables initially. | 51 | * (vii) Setting up the page tables initially. |
48 | :*/ | 52 | :*/ |
49 | 53 | ||
50 | 54 | /* | |
51 | /* 1024 entries in a page table page maps 1024 pages: 4MB. The Switcher is | 55 | * 1024 entries in a page table page maps 1024 pages: 4MB. The Switcher is |
52 | * conveniently placed at the top 4MB, so it uses a separate, complete PTE | 56 | * conveniently placed at the top 4MB, so it uses a separate, complete PTE |
53 | * page. */ | 57 | * page. |
58 | */ | ||
54 | #define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1) | 59 | #define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1) |
55 | 60 | ||
56 | /* For PAE we need the PMD index as well. We use the last 2MB, so we | 61 | /* |
57 | * will need the last pmd entry of the last pmd page. */ | 62 | * For PAE we need the PMD index as well. We use the last 2MB, so we |
63 | * will need the last pmd entry of the last pmd page. | ||
64 | */ | ||
58 | #ifdef CONFIG_X86_PAE | 65 | #ifdef CONFIG_X86_PAE |
59 | #define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1) | 66 | #define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1) |
60 | #define RESERVE_MEM 2U | 67 | #define RESERVE_MEM 2U |
@@ -64,13 +71,16 @@ | |||
64 | #define CHECK_GPGD_MASK _PAGE_TABLE | 71 | #define CHECK_GPGD_MASK _PAGE_TABLE |
65 | #endif | 72 | #endif |
66 | 73 | ||
67 | /* We actually need a separate PTE page for each CPU. Remember that after the | 74 | /* |
75 | * We actually need a separate PTE page for each CPU. Remember that after the | ||
68 | * Switcher code itself comes two pages for each CPU, and we don't want this | 76 | * Switcher code itself comes two pages for each CPU, and we don't want this |
69 | * CPU's guest to see the pages of any other CPU. */ | 77 | * CPU's guest to see the pages of any other CPU. |
78 | */ | ||
70 | static DEFINE_PER_CPU(pte_t *, switcher_pte_pages); | 79 | static DEFINE_PER_CPU(pte_t *, switcher_pte_pages); |
71 | #define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu) | 80 | #define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu) |
72 | 81 | ||
73 | /*H:320 The page table code is curly enough to need helper functions to keep it | 82 | /*H:320 |
83 | * The page table code is curly enough to need helper functions to keep it | ||
74 | * clear and clean. | 84 | * clear and clean. |
75 | * | 85 | * |
76 | * There are two functions which return pointers to the shadow (aka "real") | 86 | * There are two functions which return pointers to the shadow (aka "real") |
@@ -79,7 +89,8 @@ static DEFINE_PER_CPU(pte_t *, switcher_pte_pages); | |||
79 | * spgd_addr() takes the virtual address and returns a pointer to the top-level | 89 | * spgd_addr() takes the virtual address and returns a pointer to the top-level |
80 | * page directory entry (PGD) for that address. Since we keep track of several | 90 | * page directory entry (PGD) for that address. Since we keep track of several |
81 | * page tables, the "i" argument tells us which one we're interested in (it's | 91 | * page tables, the "i" argument tells us which one we're interested in (it's |
82 | * usually the current one). */ | 92 | * usually the current one). |
93 | */ | ||
83 | static pgd_t *spgd_addr(struct lg_cpu *cpu, u32 i, unsigned long vaddr) | 94 | static pgd_t *spgd_addr(struct lg_cpu *cpu, u32 i, unsigned long vaddr) |
84 | { | 95 | { |
85 | unsigned int index = pgd_index(vaddr); | 96 | unsigned int index = pgd_index(vaddr); |
@@ -96,9 +107,11 @@ static pgd_t *spgd_addr(struct lg_cpu *cpu, u32 i, unsigned long vaddr) | |||
96 | } | 107 | } |
97 | 108 | ||
98 | #ifdef CONFIG_X86_PAE | 109 | #ifdef CONFIG_X86_PAE |
99 | /* This routine then takes the PGD entry given above, which contains the | 110 | /* |
111 | * This routine then takes the PGD entry given above, which contains the | ||
100 | * address of the PMD page. It then returns a pointer to the PMD entry for the | 112 | * address of the PMD page. It then returns a pointer to the PMD entry for the |
101 | * given address. */ | 113 | * given address. |
114 | */ | ||
102 | static pmd_t *spmd_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) | 115 | static pmd_t *spmd_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) |
103 | { | 116 | { |
104 | unsigned int index = pmd_index(vaddr); | 117 | unsigned int index = pmd_index(vaddr); |
@@ -119,9 +132,11 @@ static pmd_t *spmd_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) | |||
119 | } | 132 | } |
120 | #endif | 133 | #endif |
121 | 134 | ||
122 | /* This routine then takes the page directory entry returned above, which | 135 | /* |
136 | * This routine then takes the page directory entry returned above, which | ||
123 | * contains the address of the page table entry (PTE) page. It then returns a | 137 | * contains the address of the page table entry (PTE) page. It then returns a |
124 | * pointer to the PTE entry for the given address. */ | 138 | * pointer to the PTE entry for the given address. |
139 | */ | ||
125 | static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) | 140 | static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) |
126 | { | 141 | { |
127 | #ifdef CONFIG_X86_PAE | 142 | #ifdef CONFIG_X86_PAE |
@@ -139,8 +154,10 @@ static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) | |||
139 | return &page[pte_index(vaddr)]; | 154 | return &page[pte_index(vaddr)]; |
140 | } | 155 | } |
141 | 156 | ||
142 | /* These two functions just like the above two, except they access the Guest | 157 | /* |
143 | * page tables. Hence they return a Guest address. */ | 158 | * These two functions just like the above two, except they access the Guest |
159 | * page tables. Hence they return a Guest address. | ||
160 | */ | ||
144 | static unsigned long gpgd_addr(struct lg_cpu *cpu, unsigned long vaddr) | 161 | static unsigned long gpgd_addr(struct lg_cpu *cpu, unsigned long vaddr) |
145 | { | 162 | { |
146 | unsigned int index = vaddr >> (PGDIR_SHIFT); | 163 | unsigned int index = vaddr >> (PGDIR_SHIFT); |
@@ -175,17 +192,21 @@ static unsigned long gpte_addr(struct lg_cpu *cpu, | |||
175 | #endif | 192 | #endif |
176 | /*:*/ | 193 | /*:*/ |
177 | 194 | ||
178 | /*M:014 get_pfn is slow: we could probably try to grab batches of pages here as | 195 | /*M:014 |
179 | * an optimization (ie. pre-faulting). :*/ | 196 | * get_pfn is slow: we could probably try to grab batches of pages here as |
197 | * an optimization (ie. pre-faulting). | ||
198 | :*/ | ||
180 | 199 | ||
181 | /*H:350 This routine takes a page number given by the Guest and converts it to | 200 | /*H:350 |
201 | * This routine takes a page number given by the Guest and converts it to | ||
182 | * an actual, physical page number. It can fail for several reasons: the | 202 | * an actual, physical page number. It can fail for several reasons: the |
183 | * virtual address might not be mapped by the Launcher, the write flag is set | 203 | * virtual address might not be mapped by the Launcher, the write flag is set |
184 | * and the page is read-only, or the write flag was set and the page was | 204 | * and the page is read-only, or the write flag was set and the page was |
185 | * shared so had to be copied, but we ran out of memory. | 205 | * shared so had to be copied, but we ran out of memory. |
186 | * | 206 | * |
187 | * This holds a reference to the page, so release_pte() is careful to put that | 207 | * This holds a reference to the page, so release_pte() is careful to put that |
188 | * back. */ | 208 | * back. |
209 | */ | ||
189 | static unsigned long get_pfn(unsigned long virtpfn, int write) | 210 | static unsigned long get_pfn(unsigned long virtpfn, int write) |
190 | { | 211 | { |
191 | struct page *page; | 212 | struct page *page; |
@@ -198,33 +219,41 @@ static unsigned long get_pfn(unsigned long virtpfn, int write) | |||
198 | return -1UL; | 219 | return -1UL; |
199 | } | 220 | } |
200 | 221 | ||
201 | /*H:340 Converting a Guest page table entry to a shadow (ie. real) page table | 222 | /*H:340 |
223 | * Converting a Guest page table entry to a shadow (ie. real) page table | ||
202 | * entry can be a little tricky. The flags are (almost) the same, but the | 224 | * entry can be a little tricky. The flags are (almost) the same, but the |
203 | * Guest PTE contains a virtual page number: the CPU needs the real page | 225 | * Guest PTE contains a virtual page number: the CPU needs the real page |
204 | * number. */ | 226 | * number. |
227 | */ | ||
205 | static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write) | 228 | static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write) |
206 | { | 229 | { |
207 | unsigned long pfn, base, flags; | 230 | unsigned long pfn, base, flags; |
208 | 231 | ||
209 | /* The Guest sets the global flag, because it thinks that it is using | 232 | /* |
233 | * The Guest sets the global flag, because it thinks that it is using | ||
210 | * PGE. We only told it to use PGE so it would tell us whether it was | 234 | * PGE. We only told it to use PGE so it would tell us whether it was |
211 | * flushing a kernel mapping or a userspace mapping. We don't actually | 235 | * flushing a kernel mapping or a userspace mapping. We don't actually |
212 | * use the global bit, so throw it away. */ | 236 | * use the global bit, so throw it away. |
237 | */ | ||
213 | flags = (pte_flags(gpte) & ~_PAGE_GLOBAL); | 238 | flags = (pte_flags(gpte) & ~_PAGE_GLOBAL); |
214 | 239 | ||
215 | /* The Guest's pages are offset inside the Launcher. */ | 240 | /* The Guest's pages are offset inside the Launcher. */ |
216 | base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE; | 241 | base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE; |
217 | 242 | ||
218 | /* We need a temporary "unsigned long" variable to hold the answer from | 243 | /* |
244 | * We need a temporary "unsigned long" variable to hold the answer from | ||
219 | * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't | 245 | * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't |
220 | * fit in spte.pfn. get_pfn() finds the real physical number of the | 246 | * fit in spte.pfn. get_pfn() finds the real physical number of the |
221 | * page, given the virtual number. */ | 247 | * page, given the virtual number. |
248 | */ | ||
222 | pfn = get_pfn(base + pte_pfn(gpte), write); | 249 | pfn = get_pfn(base + pte_pfn(gpte), write); |
223 | if (pfn == -1UL) { | 250 | if (pfn == -1UL) { |
224 | kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte)); | 251 | kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte)); |
225 | /* When we destroy the Guest, we'll go through the shadow page | 252 | /* |
253 | * When we destroy the Guest, we'll go through the shadow page | ||
226 | * tables and release_pte() them. Make sure we don't think | 254 | * tables and release_pte() them. Make sure we don't think |
227 | * this one is valid! */ | 255 | * this one is valid! |
256 | */ | ||
228 | flags = 0; | 257 | flags = 0; |
229 | } | 258 | } |
230 | /* Now we assemble our shadow PTE from the page number and flags. */ | 259 | /* Now we assemble our shadow PTE from the page number and flags. */ |
@@ -234,8 +263,10 @@ static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write) | |||
234 | /*H:460 And to complete the chain, release_pte() looks like this: */ | 263 | /*H:460 And to complete the chain, release_pte() looks like this: */ |
235 | static void release_pte(pte_t pte) | 264 | static void release_pte(pte_t pte) |
236 | { | 265 | { |
237 | /* Remember that get_user_pages_fast() took a reference to the page, in | 266 | /* |
238 | * get_pfn()? We have to put it back now. */ | 267 | * Remember that get_user_pages_fast() took a reference to the page, in |
268 | * get_pfn()? We have to put it back now. | ||
269 | */ | ||
239 | if (pte_flags(pte) & _PAGE_PRESENT) | 270 | if (pte_flags(pte) & _PAGE_PRESENT) |
240 | put_page(pte_page(pte)); | 271 | put_page(pte_page(pte)); |
241 | } | 272 | } |
@@ -273,7 +304,8 @@ static void check_gpmd(struct lg_cpu *cpu, pmd_t gpmd) | |||
273 | * and return to the Guest without it knowing. | 304 | * and return to the Guest without it knowing. |
274 | * | 305 | * |
275 | * If we fixed up the fault (ie. we mapped the address), this routine returns | 306 | * If we fixed up the fault (ie. we mapped the address), this routine returns |
276 | * true. Otherwise, it was a real fault and we need to tell the Guest. */ | 307 | * true. Otherwise, it was a real fault and we need to tell the Guest. |
308 | */ | ||
277 | bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | 309 | bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) |
278 | { | 310 | { |
279 | pgd_t gpgd; | 311 | pgd_t gpgd; |
@@ -298,22 +330,26 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
298 | if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) { | 330 | if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) { |
299 | /* No shadow entry: allocate a new shadow PTE page. */ | 331 | /* No shadow entry: allocate a new shadow PTE page. */ |
300 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); | 332 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); |
301 | /* This is not really the Guest's fault, but killing it is | 333 | /* |
302 | * simple for this corner case. */ | 334 | * This is not really the Guest's fault, but killing it is |
335 | * simple for this corner case. | ||
336 | */ | ||
303 | if (!ptepage) { | 337 | if (!ptepage) { |
304 | kill_guest(cpu, "out of memory allocating pte page"); | 338 | kill_guest(cpu, "out of memory allocating pte page"); |
305 | return false; | 339 | return false; |
306 | } | 340 | } |
307 | /* We check that the Guest pgd is OK. */ | 341 | /* We check that the Guest pgd is OK. */ |
308 | check_gpgd(cpu, gpgd); | 342 | check_gpgd(cpu, gpgd); |
309 | /* And we copy the flags to the shadow PGD entry. The page | 343 | /* |
310 | * number in the shadow PGD is the page we just allocated. */ | 344 | * And we copy the flags to the shadow PGD entry. The page |
345 | * number in the shadow PGD is the page we just allocated. | ||
346 | */ | ||
311 | set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd))); | 347 | set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd))); |
312 | } | 348 | } |
313 | 349 | ||
314 | #ifdef CONFIG_X86_PAE | 350 | #ifdef CONFIG_X86_PAE |
315 | gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t); | 351 | gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t); |
316 | /* middle level not present? We can't map it in. */ | 352 | /* Middle level not present? We can't map it in. */ |
317 | if (!(pmd_flags(gpmd) & _PAGE_PRESENT)) | 353 | if (!(pmd_flags(gpmd) & _PAGE_PRESENT)) |
318 | return false; | 354 | return false; |
319 | 355 | ||
@@ -324,8 +360,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
324 | /* No shadow entry: allocate a new shadow PTE page. */ | 360 | /* No shadow entry: allocate a new shadow PTE page. */ |
325 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); | 361 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); |
326 | 362 | ||
327 | /* This is not really the Guest's fault, but killing it is | 363 | /* |
328 | * simple for this corner case. */ | 364 | * This is not really the Guest's fault, but killing it is |
365 | * simple for this corner case. | ||
366 | */ | ||
329 | if (!ptepage) { | 367 | if (!ptepage) { |
330 | kill_guest(cpu, "out of memory allocating pte page"); | 368 | kill_guest(cpu, "out of memory allocating pte page"); |
331 | return false; | 369 | return false; |
@@ -334,17 +372,23 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
334 | /* We check that the Guest pmd is OK. */ | 372 | /* We check that the Guest pmd is OK. */ |
335 | check_gpmd(cpu, gpmd); | 373 | check_gpmd(cpu, gpmd); |
336 | 374 | ||
337 | /* And we copy the flags to the shadow PMD entry. The page | 375 | /* |
338 | * number in the shadow PMD is the page we just allocated. */ | 376 | * And we copy the flags to the shadow PMD entry. The page |
377 | * number in the shadow PMD is the page we just allocated. | ||
378 | */ | ||
339 | native_set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd))); | 379 | native_set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd))); |
340 | } | 380 | } |
341 | 381 | ||
342 | /* OK, now we look at the lower level in the Guest page table: keep its | 382 | /* |
343 | * address, because we might update it later. */ | 383 | * OK, now we look at the lower level in the Guest page table: keep its |
384 | * address, because we might update it later. | ||
385 | */ | ||
344 | gpte_ptr = gpte_addr(cpu, gpmd, vaddr); | 386 | gpte_ptr = gpte_addr(cpu, gpmd, vaddr); |
345 | #else | 387 | #else |
346 | /* OK, now we look at the lower level in the Guest page table: keep its | 388 | /* |
347 | * address, because we might update it later. */ | 389 | * OK, now we look at the lower level in the Guest page table: keep its |
390 | * address, because we might update it later. | ||
391 | */ | ||
348 | gpte_ptr = gpte_addr(cpu, gpgd, vaddr); | 392 | gpte_ptr = gpte_addr(cpu, gpgd, vaddr); |
349 | #endif | 393 | #endif |
350 | gpte = lgread(cpu, gpte_ptr, pte_t); | 394 | gpte = lgread(cpu, gpte_ptr, pte_t); |
@@ -353,8 +397,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
353 | if (!(pte_flags(gpte) & _PAGE_PRESENT)) | 397 | if (!(pte_flags(gpte) & _PAGE_PRESENT)) |
354 | return false; | 398 | return false; |
355 | 399 | ||
356 | /* Check they're not trying to write to a page the Guest wants | 400 | /* |
357 | * read-only (bit 2 of errcode == write). */ | 401 | * Check they're not trying to write to a page the Guest wants |
402 | * read-only (bit 2 of errcode == write). | ||
403 | */ | ||
358 | if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW)) | 404 | if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW)) |
359 | return false; | 405 | return false; |
360 | 406 | ||
@@ -362,8 +408,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
362 | if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER)) | 408 | if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER)) |
363 | return false; | 409 | return false; |
364 | 410 | ||
365 | /* Check that the Guest PTE flags are OK, and the page number is below | 411 | /* |
366 | * the pfn_limit (ie. not mapping the Launcher binary). */ | 412 | * Check that the Guest PTE flags are OK, and the page number is below |
413 | * the pfn_limit (ie. not mapping the Launcher binary). | ||
414 | */ | ||
367 | check_gpte(cpu, gpte); | 415 | check_gpte(cpu, gpte); |
368 | 416 | ||
369 | /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */ | 417 | /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */ |
@@ -373,29 +421,40 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
373 | 421 | ||
374 | /* Get the pointer to the shadow PTE entry we're going to set. */ | 422 | /* Get the pointer to the shadow PTE entry we're going to set. */ |
375 | spte = spte_addr(cpu, *spgd, vaddr); | 423 | spte = spte_addr(cpu, *spgd, vaddr); |
376 | /* If there was a valid shadow PTE entry here before, we release it. | 424 | |
377 | * This can happen with a write to a previously read-only entry. */ | 425 | /* |
426 | * If there was a valid shadow PTE entry here before, we release it. | ||
427 | * This can happen with a write to a previously read-only entry. | ||
428 | */ | ||
378 | release_pte(*spte); | 429 | release_pte(*spte); |
379 | 430 | ||
380 | /* If this is a write, we insist that the Guest page is writable (the | 431 | /* |
381 | * final arg to gpte_to_spte()). */ | 432 | * If this is a write, we insist that the Guest page is writable (the |
433 | * final arg to gpte_to_spte()). | ||
434 | */ | ||
382 | if (pte_dirty(gpte)) | 435 | if (pte_dirty(gpte)) |
383 | *spte = gpte_to_spte(cpu, gpte, 1); | 436 | *spte = gpte_to_spte(cpu, gpte, 1); |
384 | else | 437 | else |
385 | /* If this is a read, don't set the "writable" bit in the page | 438 | /* |
439 | * If this is a read, don't set the "writable" bit in the page | ||
386 | * table entry, even if the Guest says it's writable. That way | 440 | * table entry, even if the Guest says it's writable. That way |
387 | * we will come back here when a write does actually occur, so | 441 | * we will come back here when a write does actually occur, so |
388 | * we can update the Guest's _PAGE_DIRTY flag. */ | 442 | * we can update the Guest's _PAGE_DIRTY flag. |
443 | */ | ||
389 | native_set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0)); | 444 | native_set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0)); |
390 | 445 | ||
391 | /* Finally, we write the Guest PTE entry back: we've set the | 446 | /* |
392 | * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags. */ | 447 | * Finally, we write the Guest PTE entry back: we've set the |
448 | * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags. | ||
449 | */ | ||
393 | lgwrite(cpu, gpte_ptr, pte_t, gpte); | 450 | lgwrite(cpu, gpte_ptr, pte_t, gpte); |
394 | 451 | ||
395 | /* The fault is fixed, the page table is populated, the mapping | 452 | /* |
453 | * The fault is fixed, the page table is populated, the mapping | ||
396 | * manipulated, the result returned and the code complete. A small | 454 | * manipulated, the result returned and the code complete. A small |
397 | * delay and a trace of alliteration are the only indications the Guest | 455 | * delay and a trace of alliteration are the only indications the Guest |
398 | * has that a page fault occurred at all. */ | 456 | * has that a page fault occurred at all. |
457 | */ | ||
399 | return true; | 458 | return true; |
400 | } | 459 | } |
401 | 460 | ||
@@ -408,7 +467,8 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
408 | * mapped, so it's overkill. | 467 | * mapped, so it's overkill. |
409 | * | 468 | * |
410 | * This is a quick version which answers the question: is this virtual address | 469 | * This is a quick version which answers the question: is this virtual address |
411 | * mapped by the shadow page tables, and is it writable? */ | 470 | * mapped by the shadow page tables, and is it writable? |
471 | */ | ||
412 | static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) | 472 | static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) |
413 | { | 473 | { |
414 | pgd_t *spgd; | 474 | pgd_t *spgd; |
@@ -428,16 +488,20 @@ static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) | |||
428 | return false; | 488 | return false; |
429 | #endif | 489 | #endif |
430 | 490 | ||
431 | /* Check the flags on the pte entry itself: it must be present and | 491 | /* |
432 | * writable. */ | 492 | * Check the flags on the pte entry itself: it must be present and |
493 | * writable. | ||
494 | */ | ||
433 | flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr))); | 495 | flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr))); |
434 | 496 | ||
435 | return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW); | 497 | return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW); |
436 | } | 498 | } |
437 | 499 | ||
438 | /* So, when pin_stack_pages() asks us to pin a page, we check if it's already | 500 | /* |
501 | * So, when pin_stack_pages() asks us to pin a page, we check if it's already | ||
439 | * in the page tables, and if not, we call demand_page() with error code 2 | 502 | * in the page tables, and if not, we call demand_page() with error code 2 |
440 | * (meaning "write"). */ | 503 | * (meaning "write"). |
504 | */ | ||
441 | void pin_page(struct lg_cpu *cpu, unsigned long vaddr) | 505 | void pin_page(struct lg_cpu *cpu, unsigned long vaddr) |
442 | { | 506 | { |
443 | if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2)) | 507 | if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2)) |
@@ -485,9 +549,11 @@ static void release_pgd(pgd_t *spgd) | |||
485 | /* If the entry's not present, there's nothing to release. */ | 549 | /* If the entry's not present, there's nothing to release. */ |
486 | if (pgd_flags(*spgd) & _PAGE_PRESENT) { | 550 | if (pgd_flags(*spgd) & _PAGE_PRESENT) { |
487 | unsigned int i; | 551 | unsigned int i; |
488 | /* Converting the pfn to find the actual PTE page is easy: turn | 552 | /* |
553 | * Converting the pfn to find the actual PTE page is easy: turn | ||
489 | * the page number into a physical address, then convert to a | 554 | * the page number into a physical address, then convert to a |
490 | * virtual address (easy for kernel pages like this one). */ | 555 | * virtual address (easy for kernel pages like this one). |
556 | */ | ||
491 | pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); | 557 | pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); |
492 | /* For each entry in the page, we might need to release it. */ | 558 | /* For each entry in the page, we might need to release it. */ |
493 | for (i = 0; i < PTRS_PER_PTE; i++) | 559 | for (i = 0; i < PTRS_PER_PTE; i++) |
@@ -499,9 +565,12 @@ static void release_pgd(pgd_t *spgd) | |||
499 | } | 565 | } |
500 | } | 566 | } |
501 | #endif | 567 | #endif |
502 | /*H:445 We saw flush_user_mappings() twice: once from the flush_user_mappings() | 568 | |
569 | /*H:445 | ||
570 | * We saw flush_user_mappings() twice: once from the flush_user_mappings() | ||
503 | * hypercall and once in new_pgdir() when we re-used a top-level pgdir page. | 571 | * hypercall and once in new_pgdir() when we re-used a top-level pgdir page. |
504 | * It simply releases every PTE page from 0 up to the Guest's kernel address. */ | 572 | * It simply releases every PTE page from 0 up to the Guest's kernel address. |
573 | */ | ||
505 | static void flush_user_mappings(struct lguest *lg, int idx) | 574 | static void flush_user_mappings(struct lguest *lg, int idx) |
506 | { | 575 | { |
507 | unsigned int i; | 576 | unsigned int i; |
@@ -510,10 +579,12 @@ static void flush_user_mappings(struct lguest *lg, int idx) | |||
510 | release_pgd(lg->pgdirs[idx].pgdir + i); | 579 | release_pgd(lg->pgdirs[idx].pgdir + i); |
511 | } | 580 | } |
512 | 581 | ||
513 | /*H:440 (v) Flushing (throwing away) page tables, | 582 | /*H:440 |
583 | * (v) Flushing (throwing away) page tables, | ||
514 | * | 584 | * |
515 | * The Guest has a hypercall to throw away the page tables: it's used when a | 585 | * The Guest has a hypercall to throw away the page tables: it's used when a |
516 | * large number of mappings have been changed. */ | 586 | * large number of mappings have been changed. |
587 | */ | ||
517 | void guest_pagetable_flush_user(struct lg_cpu *cpu) | 588 | void guest_pagetable_flush_user(struct lg_cpu *cpu) |
518 | { | 589 | { |
519 | /* Drop the userspace part of the current page table. */ | 590 | /* Drop the userspace part of the current page table. */ |
@@ -551,9 +622,11 @@ unsigned long guest_pa(struct lg_cpu *cpu, unsigned long vaddr) | |||
551 | return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK); | 622 | return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK); |
552 | } | 623 | } |
553 | 624 | ||
554 | /* We keep several page tables. This is a simple routine to find the page | 625 | /* |
626 | * We keep several page tables. This is a simple routine to find the page | ||
555 | * table (if any) corresponding to this top-level address the Guest has given | 627 | * table (if any) corresponding to this top-level address the Guest has given |
556 | * us. */ | 628 | * us. |
629 | */ | ||
557 | static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) | 630 | static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) |
558 | { | 631 | { |
559 | unsigned int i; | 632 | unsigned int i; |
@@ -563,9 +636,11 @@ static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) | |||
563 | return i; | 636 | return i; |
564 | } | 637 | } |
565 | 638 | ||
566 | /*H:435 And this is us, creating the new page directory. If we really do | 639 | /*H:435 |
640 | * And this is us, creating the new page directory. If we really do | ||
567 | * allocate a new one (and so the kernel parts are not there), we set | 641 | * allocate a new one (and so the kernel parts are not there), we set |
568 | * blank_pgdir. */ | 642 | * blank_pgdir. |
643 | */ | ||
569 | static unsigned int new_pgdir(struct lg_cpu *cpu, | 644 | static unsigned int new_pgdir(struct lg_cpu *cpu, |
570 | unsigned long gpgdir, | 645 | unsigned long gpgdir, |
571 | int *blank_pgdir) | 646 | int *blank_pgdir) |
@@ -575,8 +650,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
575 | pmd_t *pmd_table; | 650 | pmd_t *pmd_table; |
576 | #endif | 651 | #endif |
577 | 652 | ||
578 | /* We pick one entry at random to throw out. Choosing the Least | 653 | /* |
579 | * Recently Used might be better, but this is easy. */ | 654 | * We pick one entry at random to throw out. Choosing the Least |
655 | * Recently Used might be better, but this is easy. | ||
656 | */ | ||
580 | next = random32() % ARRAY_SIZE(cpu->lg->pgdirs); | 657 | next = random32() % ARRAY_SIZE(cpu->lg->pgdirs); |
581 | /* If it's never been allocated at all before, try now. */ | 658 | /* If it's never been allocated at all before, try now. */ |
582 | if (!cpu->lg->pgdirs[next].pgdir) { | 659 | if (!cpu->lg->pgdirs[next].pgdir) { |
@@ -587,8 +664,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
587 | next = cpu->cpu_pgd; | 664 | next = cpu->cpu_pgd; |
588 | else { | 665 | else { |
589 | #ifdef CONFIG_X86_PAE | 666 | #ifdef CONFIG_X86_PAE |
590 | /* In PAE mode, allocate a pmd page and populate the | 667 | /* |
591 | * last pgd entry. */ | 668 | * In PAE mode, allocate a pmd page and populate the |
669 | * last pgd entry. | ||
670 | */ | ||
592 | pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL); | 671 | pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL); |
593 | if (!pmd_table) { | 672 | if (!pmd_table) { |
594 | free_page((long)cpu->lg->pgdirs[next].pgdir); | 673 | free_page((long)cpu->lg->pgdirs[next].pgdir); |
@@ -598,8 +677,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
598 | set_pgd(cpu->lg->pgdirs[next].pgdir + | 677 | set_pgd(cpu->lg->pgdirs[next].pgdir + |
599 | SWITCHER_PGD_INDEX, | 678 | SWITCHER_PGD_INDEX, |
600 | __pgd(__pa(pmd_table) | _PAGE_PRESENT)); | 679 | __pgd(__pa(pmd_table) | _PAGE_PRESENT)); |
601 | /* This is a blank page, so there are no kernel | 680 | /* |
602 | * mappings: caller must map the stack! */ | 681 | * This is a blank page, so there are no kernel |
682 | * mappings: caller must map the stack! | ||
683 | */ | ||
603 | *blank_pgdir = 1; | 684 | *blank_pgdir = 1; |
604 | } | 685 | } |
605 | #else | 686 | #else |
@@ -615,19 +696,23 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
615 | return next; | 696 | return next; |
616 | } | 697 | } |
617 | 698 | ||
618 | /*H:430 (iv) Switching page tables | 699 | /*H:430 |
700 | * (iv) Switching page tables | ||
619 | * | 701 | * |
620 | * Now we've seen all the page table setting and manipulation, let's see | 702 | * Now we've seen all the page table setting and manipulation, let's see |
621 | * what happens when the Guest changes page tables (ie. changes the top-level | 703 | * what happens when the Guest changes page tables (ie. changes the top-level |
622 | * pgdir). This occurs on almost every context switch. */ | 704 | * pgdir). This occurs on almost every context switch. |
705 | */ | ||
623 | void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) | 706 | void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) |
624 | { | 707 | { |
625 | int newpgdir, repin = 0; | 708 | int newpgdir, repin = 0; |
626 | 709 | ||
627 | /* Look to see if we have this one already. */ | 710 | /* Look to see if we have this one already. */ |
628 | newpgdir = find_pgdir(cpu->lg, pgtable); | 711 | newpgdir = find_pgdir(cpu->lg, pgtable); |
629 | /* If not, we allocate or mug an existing one: if it's a fresh one, | 712 | /* |
630 | * repin gets set to 1. */ | 713 | * If not, we allocate or mug an existing one: if it's a fresh one, |
714 | * repin gets set to 1. | ||
715 | */ | ||
631 | if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs)) | 716 | if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs)) |
632 | newpgdir = new_pgdir(cpu, pgtable, &repin); | 717 | newpgdir = new_pgdir(cpu, pgtable, &repin); |
633 | /* Change the current pgd index to the new one. */ | 718 | /* Change the current pgd index to the new one. */ |
@@ -637,9 +722,11 @@ void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) | |||
637 | pin_stack_pages(cpu); | 722 | pin_stack_pages(cpu); |
638 | } | 723 | } |
639 | 724 | ||
640 | /*H:470 Finally, a routine which throws away everything: all PGD entries in all | 725 | /*H:470 |
726 | * Finally, a routine which throws away everything: all PGD entries in all | ||
641 | * the shadow page tables, including the Guest's kernel mappings. This is used | 727 | * the shadow page tables, including the Guest's kernel mappings. This is used |
642 | * when we destroy the Guest. */ | 728 | * when we destroy the Guest. |
729 | */ | ||
643 | static void release_all_pagetables(struct lguest *lg) | 730 | static void release_all_pagetables(struct lguest *lg) |
644 | { | 731 | { |
645 | unsigned int i, j; | 732 | unsigned int i, j; |
@@ -656,8 +743,10 @@ static void release_all_pagetables(struct lguest *lg) | |||
656 | spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX; | 743 | spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX; |
657 | pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); | 744 | pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); |
658 | 745 | ||
659 | /* And release the pmd entries of that pmd page, | 746 | /* |
660 | * except for the switcher pmd. */ | 747 | * And release the pmd entries of that pmd page, |
748 | * except for the switcher pmd. | ||
749 | */ | ||
661 | for (k = 0; k < SWITCHER_PMD_INDEX; k++) | 750 | for (k = 0; k < SWITCHER_PMD_INDEX; k++) |
662 | release_pmd(&pmdpage[k]); | 751 | release_pmd(&pmdpage[k]); |
663 | #endif | 752 | #endif |
@@ -667,10 +756,12 @@ static void release_all_pagetables(struct lguest *lg) | |||
667 | } | 756 | } |
668 | } | 757 | } |
669 | 758 | ||
670 | /* We also throw away everything when a Guest tells us it's changed a kernel | 759 | /* |
760 | * We also throw away everything when a Guest tells us it's changed a kernel | ||
671 | * mapping. Since kernel mappings are in every page table, it's easiest to | 761 | * mapping. Since kernel mappings are in every page table, it's easiest to |
672 | * throw them all away. This traps the Guest in amber for a while as | 762 | * throw them all away. This traps the Guest in amber for a while as |
673 | * everything faults back in, but it's rare. */ | 763 | * everything faults back in, but it's rare. |
764 | */ | ||
674 | void guest_pagetable_clear_all(struct lg_cpu *cpu) | 765 | void guest_pagetable_clear_all(struct lg_cpu *cpu) |
675 | { | 766 | { |
676 | release_all_pagetables(cpu->lg); | 767 | release_all_pagetables(cpu->lg); |
@@ -678,15 +769,19 @@ void guest_pagetable_clear_all(struct lg_cpu *cpu) | |||
678 | pin_stack_pages(cpu); | 769 | pin_stack_pages(cpu); |
679 | } | 770 | } |
680 | /*:*/ | 771 | /*:*/ |
681 | /*M:009 Since we throw away all mappings when a kernel mapping changes, our | 772 | |
773 | /*M:009 | ||
774 | * Since we throw away all mappings when a kernel mapping changes, our | ||
682 | * performance sucks for guests using highmem. In fact, a guest with | 775 | * performance sucks for guests using highmem. In fact, a guest with |
683 | * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is | 776 | * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is |
684 | * usually slower than a Guest with less memory. | 777 | * usually slower than a Guest with less memory. |
685 | * | 778 | * |
686 | * This, of course, cannot be fixed. It would take some kind of... well, I | 779 | * This, of course, cannot be fixed. It would take some kind of... well, I |
687 | * don't know, but the term "puissant code-fu" comes to mind. :*/ | 780 | * don't know, but the term "puissant code-fu" comes to mind. |
781 | :*/ | ||
688 | 782 | ||
689 | /*H:420 This is the routine which actually sets the page table entry for then | 783 | /*H:420 |
784 | * This is the routine which actually sets the page table entry for then | ||
690 | * "idx"'th shadow page table. | 785 | * "idx"'th shadow page table. |
691 | * | 786 | * |
692 | * Normally, we can just throw out the old entry and replace it with 0: if they | 787 | * Normally, we can just throw out the old entry and replace it with 0: if they |
@@ -715,31 +810,36 @@ static void do_set_pte(struct lg_cpu *cpu, int idx, | |||
715 | spmd = spmd_addr(cpu, *spgd, vaddr); | 810 | spmd = spmd_addr(cpu, *spgd, vaddr); |
716 | if (pmd_flags(*spmd) & _PAGE_PRESENT) { | 811 | if (pmd_flags(*spmd) & _PAGE_PRESENT) { |
717 | #endif | 812 | #endif |
718 | /* Otherwise, we start by releasing | 813 | /* Otherwise, start by releasing the existing entry. */ |
719 | * the existing entry. */ | ||
720 | pte_t *spte = spte_addr(cpu, *spgd, vaddr); | 814 | pte_t *spte = spte_addr(cpu, *spgd, vaddr); |
721 | release_pte(*spte); | 815 | release_pte(*spte); |
722 | 816 | ||
723 | /* If they're setting this entry as dirty or accessed, | 817 | /* |
724 | * we might as well put that entry they've given us | 818 | * If they're setting this entry as dirty or accessed, |
725 | * in now. This shaves 10% off a | 819 | * we might as well put that entry they've given us in |
726 | * copy-on-write micro-benchmark. */ | 820 | * now. This shaves 10% off a copy-on-write |
821 | * micro-benchmark. | ||
822 | */ | ||
727 | if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) { | 823 | if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) { |
728 | check_gpte(cpu, gpte); | 824 | check_gpte(cpu, gpte); |
729 | native_set_pte(spte, | 825 | native_set_pte(spte, |
730 | gpte_to_spte(cpu, gpte, | 826 | gpte_to_spte(cpu, gpte, |
731 | pte_flags(gpte) & _PAGE_DIRTY)); | 827 | pte_flags(gpte) & _PAGE_DIRTY)); |
732 | } else | 828 | } else { |
733 | /* Otherwise kill it and we can demand_page() | 829 | /* |
734 | * it in later. */ | 830 | * Otherwise kill it and we can demand_page() |
831 | * it in later. | ||
832 | */ | ||
735 | native_set_pte(spte, __pte(0)); | 833 | native_set_pte(spte, __pte(0)); |
834 | } | ||
736 | #ifdef CONFIG_X86_PAE | 835 | #ifdef CONFIG_X86_PAE |
737 | } | 836 | } |
738 | #endif | 837 | #endif |
739 | } | 838 | } |
740 | } | 839 | } |
741 | 840 | ||
742 | /*H:410 Updating a PTE entry is a little trickier. | 841 | /*H:410 |
842 | * Updating a PTE entry is a little trickier. | ||
743 | * | 843 | * |
744 | * We keep track of several different page tables (the Guest uses one for each | 844 | * We keep track of several different page tables (the Guest uses one for each |
745 | * process, so it makes sense to cache at least a few). Each of these have | 845 | * process, so it makes sense to cache at least a few). Each of these have |
@@ -748,12 +848,15 @@ static void do_set_pte(struct lg_cpu *cpu, int idx, | |||
748 | * all the page tables, not just the current one. This is rare. | 848 | * all the page tables, not just the current one. This is rare. |
749 | * | 849 | * |
750 | * The benefit is that when we have to track a new page table, we can keep all | 850 | * The benefit is that when we have to track a new page table, we can keep all |
751 | * the kernel mappings. This speeds up context switch immensely. */ | 851 | * the kernel mappings. This speeds up context switch immensely. |
852 | */ | ||
752 | void guest_set_pte(struct lg_cpu *cpu, | 853 | void guest_set_pte(struct lg_cpu *cpu, |
753 | unsigned long gpgdir, unsigned long vaddr, pte_t gpte) | 854 | unsigned long gpgdir, unsigned long vaddr, pte_t gpte) |
754 | { | 855 | { |
755 | /* Kernel mappings must be changed on all top levels. Slow, but doesn't | 856 | /* |
756 | * happen often. */ | 857 | * Kernel mappings must be changed on all top levels. Slow, but doesn't |
858 | * happen often. | ||
859 | */ | ||
757 | if (vaddr >= cpu->lg->kernel_address) { | 860 | if (vaddr >= cpu->lg->kernel_address) { |
758 | unsigned int i; | 861 | unsigned int i; |
759 | for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++) | 862 | for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++) |
@@ -802,12 +905,14 @@ void guest_set_pmd(struct lguest *lg, unsigned long pmdp, u32 idx) | |||
802 | } | 905 | } |
803 | #endif | 906 | #endif |
804 | 907 | ||
805 | /* Once we know how much memory we have we can construct simple identity | 908 | /* |
806 | * (which set virtual == physical) and linear mappings | 909 | * Once we know how much memory we have we can construct simple identity (which |
807 | * which will get the Guest far enough into the boot to create its own. | 910 | * set virtual == physical) and linear mappings which will get the Guest far |
911 | * enough into the boot to create its own. | ||
808 | * | 912 | * |
809 | * We lay them out of the way, just below the initrd (which is why we need to | 913 | * We lay them out of the way, just below the initrd (which is why we need to |
810 | * know its size here). */ | 914 | * know its size here). |
915 | */ | ||
811 | static unsigned long setup_pagetables(struct lguest *lg, | 916 | static unsigned long setup_pagetables(struct lguest *lg, |
812 | unsigned long mem, | 917 | unsigned long mem, |
813 | unsigned long initrd_size) | 918 | unsigned long initrd_size) |
@@ -825,8 +930,10 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
825 | unsigned int phys_linear; | 930 | unsigned int phys_linear; |
826 | #endif | 931 | #endif |
827 | 932 | ||
828 | /* We have mapped_pages frames to map, so we need | 933 | /* |
829 | * linear_pages page tables to map them. */ | 934 | * We have mapped_pages frames to map, so we need linear_pages page |
935 | * tables to map them. | ||
936 | */ | ||
830 | mapped_pages = mem / PAGE_SIZE; | 937 | mapped_pages = mem / PAGE_SIZE; |
831 | linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE; | 938 | linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE; |
832 | 939 | ||
@@ -839,8 +946,10 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
839 | #ifdef CONFIG_X86_PAE | 946 | #ifdef CONFIG_X86_PAE |
840 | pmds = (void *)linear - PAGE_SIZE; | 947 | pmds = (void *)linear - PAGE_SIZE; |
841 | #endif | 948 | #endif |
842 | /* Linear mapping is easy: put every page's address into the | 949 | /* |
843 | * mapping in order. */ | 950 | * Linear mapping is easy: put every page's address into the |
951 | * mapping in order. | ||
952 | */ | ||
844 | for (i = 0; i < mapped_pages; i++) { | 953 | for (i = 0; i < mapped_pages; i++) { |
845 | pte_t pte; | 954 | pte_t pte; |
846 | pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER)); | 955 | pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER)); |
@@ -848,8 +957,10 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
848 | return -EFAULT; | 957 | return -EFAULT; |
849 | } | 958 | } |
850 | 959 | ||
851 | /* The top level points to the linear page table pages above. | 960 | /* |
852 | * We setup the identity and linear mappings here. */ | 961 | * The top level points to the linear page table pages above. |
962 | * We setup the identity and linear mappings here. | ||
963 | */ | ||
853 | #ifdef CONFIG_X86_PAE | 964 | #ifdef CONFIG_X86_PAE |
854 | for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD; | 965 | for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD; |
855 | i += PTRS_PER_PTE, j++) { | 966 | i += PTRS_PER_PTE, j++) { |
@@ -880,15 +991,19 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
880 | } | 991 | } |
881 | #endif | 992 | #endif |
882 | 993 | ||
883 | /* We return the top level (guest-physical) address: remember where | 994 | /* |
884 | * this is. */ | 995 | * We return the top level (guest-physical) address: remember where |
996 | * this is. | ||
997 | */ | ||
885 | return (unsigned long)pgdir - mem_base; | 998 | return (unsigned long)pgdir - mem_base; |
886 | } | 999 | } |
887 | 1000 | ||
888 | /*H:500 (vii) Setting up the page tables initially. | 1001 | /*H:500 |
1002 | * (vii) Setting up the page tables initially. | ||
889 | * | 1003 | * |
890 | * When a Guest is first created, the Launcher tells us where the toplevel of | 1004 | * When a Guest is first created, the Launcher tells us where the toplevel of |
891 | * its first page table is. We set some things up here: */ | 1005 | * its first page table is. We set some things up here: |
1006 | */ | ||
892 | int init_guest_pagetable(struct lguest *lg) | 1007 | int init_guest_pagetable(struct lguest *lg) |
893 | { | 1008 | { |
894 | u64 mem; | 1009 | u64 mem; |
@@ -898,14 +1013,18 @@ int init_guest_pagetable(struct lguest *lg) | |||
898 | pgd_t *pgd; | 1013 | pgd_t *pgd; |
899 | pmd_t *pmd_table; | 1014 | pmd_t *pmd_table; |
900 | #endif | 1015 | #endif |
901 | /* Get the Guest memory size and the ramdisk size from the boot header | 1016 | /* |
902 | * located at lg->mem_base (Guest address 0). */ | 1017 | * Get the Guest memory size and the ramdisk size from the boot header |
1018 | * located at lg->mem_base (Guest address 0). | ||
1019 | */ | ||
903 | if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem)) | 1020 | if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem)) |
904 | || get_user(initrd_size, &boot->hdr.ramdisk_size)) | 1021 | || get_user(initrd_size, &boot->hdr.ramdisk_size)) |
905 | return -EFAULT; | 1022 | return -EFAULT; |
906 | 1023 | ||
907 | /* We start on the first shadow page table, and give it a blank PGD | 1024 | /* |
908 | * page. */ | 1025 | * We start on the first shadow page table, and give it a blank PGD |
1026 | * page. | ||
1027 | */ | ||
909 | lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size); | 1028 | lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size); |
910 | if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir)) | 1029 | if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir)) |
911 | return lg->pgdirs[0].gpgdir; | 1030 | return lg->pgdirs[0].gpgdir; |
@@ -931,17 +1050,21 @@ void page_table_guest_data_init(struct lg_cpu *cpu) | |||
931 | /* We get the kernel address: above this is all kernel memory. */ | 1050 | /* We get the kernel address: above this is all kernel memory. */ |
932 | if (get_user(cpu->lg->kernel_address, | 1051 | if (get_user(cpu->lg->kernel_address, |
933 | &cpu->lg->lguest_data->kernel_address) | 1052 | &cpu->lg->lguest_data->kernel_address) |
934 | /* We tell the Guest that it can't use the top 2 or 4 MB | 1053 | /* |
935 | * of virtual addresses used by the Switcher. */ | 1054 | * We tell the Guest that it can't use the top 2 or 4 MB |
1055 | * of virtual addresses used by the Switcher. | ||
1056 | */ | ||
936 | || put_user(RESERVE_MEM * 1024 * 1024, | 1057 | || put_user(RESERVE_MEM * 1024 * 1024, |
937 | &cpu->lg->lguest_data->reserve_mem) | 1058 | &cpu->lg->lguest_data->reserve_mem) |
938 | || put_user(cpu->lg->pgdirs[0].gpgdir, | 1059 | || put_user(cpu->lg->pgdirs[0].gpgdir, |
939 | &cpu->lg->lguest_data->pgdir)) | 1060 | &cpu->lg->lguest_data->pgdir)) |
940 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); | 1061 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); |
941 | 1062 | ||
942 | /* In flush_user_mappings() we loop from 0 to | 1063 | /* |
1064 | * In flush_user_mappings() we loop from 0 to | ||
943 | * "pgd_index(lg->kernel_address)". This assumes it won't hit the | 1065 | * "pgd_index(lg->kernel_address)". This assumes it won't hit the |
944 | * Switcher mappings, so check that now. */ | 1066 | * Switcher mappings, so check that now. |
1067 | */ | ||
945 | #ifdef CONFIG_X86_PAE | 1068 | #ifdef CONFIG_X86_PAE |
946 | if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX && | 1069 | if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX && |
947 | pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX) | 1070 | pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX) |
@@ -964,12 +1087,14 @@ void free_guest_pagetable(struct lguest *lg) | |||
964 | free_page((long)lg->pgdirs[i].pgdir); | 1087 | free_page((long)lg->pgdirs[i].pgdir); |
965 | } | 1088 | } |
966 | 1089 | ||
967 | /*H:480 (vi) Mapping the Switcher when the Guest is about to run. | 1090 | /*H:480 |
1091 | * (vi) Mapping the Switcher when the Guest is about to run. | ||
968 | * | 1092 | * |
969 | * The Switcher and the two pages for this CPU need to be visible in the | 1093 | * The Switcher and the two pages for this CPU need to be visible in the |
970 | * Guest (and not the pages for other CPUs). We have the appropriate PTE pages | 1094 | * Guest (and not the pages for other CPUs). We have the appropriate PTE pages |
971 | * for each CPU already set up, we just need to hook them in now we know which | 1095 | * for each CPU already set up, we just need to hook them in now we know which |
972 | * Guest is about to run on this CPU. */ | 1096 | * Guest is about to run on this CPU. |
1097 | */ | ||
973 | void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) | 1098 | void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) |
974 | { | 1099 | { |
975 | pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages); | 1100 | pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages); |
@@ -990,20 +1115,24 @@ void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) | |||
990 | #else | 1115 | #else |
991 | pgd_t switcher_pgd; | 1116 | pgd_t switcher_pgd; |
992 | 1117 | ||
993 | /* Make the last PGD entry for this Guest point to the Switcher's PTE | 1118 | /* |
994 | * page for this CPU (with appropriate flags). */ | 1119 | * Make the last PGD entry for this Guest point to the Switcher's PTE |
1120 | * page for this CPU (with appropriate flags). | ||
1121 | */ | ||
995 | switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC); | 1122 | switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC); |
996 | 1123 | ||
997 | cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd; | 1124 | cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd; |
998 | 1125 | ||
999 | #endif | 1126 | #endif |
1000 | /* We also change the Switcher PTE page. When we're running the Guest, | 1127 | /* |
1128 | * We also change the Switcher PTE page. When we're running the Guest, | ||
1001 | * we want the Guest's "regs" page to appear where the first Switcher | 1129 | * we want the Guest's "regs" page to appear where the first Switcher |
1002 | * page for this CPU is. This is an optimization: when the Switcher | 1130 | * page for this CPU is. This is an optimization: when the Switcher |
1003 | * saves the Guest registers, it saves them into the first page of this | 1131 | * saves the Guest registers, it saves them into the first page of this |
1004 | * CPU's "struct lguest_pages": if we make sure the Guest's register | 1132 | * CPU's "struct lguest_pages": if we make sure the Guest's register |
1005 | * page is already mapped there, we don't have to copy them out | 1133 | * page is already mapped there, we don't have to copy them out |
1006 | * again. */ | 1134 | * again. |
1135 | */ | ||
1007 | pfn = __pa(cpu->regs_page) >> PAGE_SHIFT; | 1136 | pfn = __pa(cpu->regs_page) >> PAGE_SHIFT; |
1008 | native_set_pte(®s_pte, pfn_pte(pfn, PAGE_KERNEL)); | 1137 | native_set_pte(®s_pte, pfn_pte(pfn, PAGE_KERNEL)); |
1009 | native_set_pte(&switcher_pte_page[pte_index((unsigned long)pages)], | 1138 | native_set_pte(&switcher_pte_page[pte_index((unsigned long)pages)], |
@@ -1019,10 +1148,12 @@ static void free_switcher_pte_pages(void) | |||
1019 | free_page((long)switcher_pte_page(i)); | 1148 | free_page((long)switcher_pte_page(i)); |
1020 | } | 1149 | } |
1021 | 1150 | ||
1022 | /*H:520 Setting up the Switcher PTE page for given CPU is fairly easy, given | 1151 | /*H:520 |
1152 | * Setting up the Switcher PTE page for given CPU is fairly easy, given | ||
1023 | * the CPU number and the "struct page"s for the Switcher code itself. | 1153 | * the CPU number and the "struct page"s for the Switcher code itself. |
1024 | * | 1154 | * |
1025 | * Currently the Switcher is less than a page long, so "pages" is always 1. */ | 1155 | * Currently the Switcher is less than a page long, so "pages" is always 1. |
1156 | */ | ||
1026 | static __init void populate_switcher_pte_page(unsigned int cpu, | 1157 | static __init void populate_switcher_pte_page(unsigned int cpu, |
1027 | struct page *switcher_page[], | 1158 | struct page *switcher_page[], |
1028 | unsigned int pages) | 1159 | unsigned int pages) |
@@ -1043,13 +1174,16 @@ static __init void populate_switcher_pte_page(unsigned int cpu, | |||
1043 | native_set_pte(&pte[i], pfn_pte(page_to_pfn(switcher_page[i]), | 1174 | native_set_pte(&pte[i], pfn_pte(page_to_pfn(switcher_page[i]), |
1044 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW))); | 1175 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW))); |
1045 | 1176 | ||
1046 | /* The second page contains the "struct lguest_ro_state", and is | 1177 | /* |
1047 | * read-only. */ | 1178 | * The second page contains the "struct lguest_ro_state", and is |
1179 | * read-only. | ||
1180 | */ | ||
1048 | native_set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]), | 1181 | native_set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]), |
1049 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED))); | 1182 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED))); |
1050 | } | 1183 | } |
1051 | 1184 | ||
1052 | /* We've made it through the page table code. Perhaps our tired brains are | 1185 | /* |
1186 | * We've made it through the page table code. Perhaps our tired brains are | ||
1053 | * still processing the details, or perhaps we're simply glad it's over. | 1187 | * still processing the details, or perhaps we're simply glad it's over. |
1054 | * | 1188 | * |
1055 | * If nothing else, note that all this complexity in juggling shadow page tables | 1189 | * If nothing else, note that all this complexity in juggling shadow page tables |
@@ -1058,10 +1192,13 @@ static __init void populate_switcher_pte_page(unsigned int cpu, | |||
1058 | * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD | 1192 | * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD |
1059 | * have implemented shadow page table support directly into hardware. | 1193 | * have implemented shadow page table support directly into hardware. |
1060 | * | 1194 | * |
1061 | * There is just one file remaining in the Host. */ | 1195 | * There is just one file remaining in the Host. |
1196 | */ | ||
1062 | 1197 | ||
1063 | /*H:510 At boot or module load time, init_pagetables() allocates and populates | 1198 | /*H:510 |
1064 | * the Switcher PTE page for each CPU. */ | 1199 | * At boot or module load time, init_pagetables() allocates and populates |
1200 | * the Switcher PTE page for each CPU. | ||
1201 | */ | ||
1065 | __init int init_pagetables(struct page **switcher_page, unsigned int pages) | 1202 | __init int init_pagetables(struct page **switcher_page, unsigned int pages) |
1066 | { | 1203 | { |
1067 | unsigned int i; | 1204 | unsigned int i; |
diff --git a/drivers/lguest/segments.c b/drivers/lguest/segments.c index 482ed5a18750..951c57b0a7e0 100644 --- a/drivers/lguest/segments.c +++ b/drivers/lguest/segments.c | |||
@@ -1,4 +1,5 @@ | |||
1 | /*P:600 The x86 architecture has segments, which involve a table of descriptors | 1 | /*P:600 |
2 | * The x86 architecture has segments, which involve a table of descriptors | ||
2 | * which can be used to do funky things with virtual address interpretation. | 3 | * which can be used to do funky things with virtual address interpretation. |
3 | * We originally used to use segments so the Guest couldn't alter the | 4 | * We originally used to use segments so the Guest couldn't alter the |
4 | * Guest<->Host Switcher, and then we had to trim Guest segments, and restore | 5 | * Guest<->Host Switcher, and then we had to trim Guest segments, and restore |
@@ -8,7 +9,8 @@ | |||
8 | * | 9 | * |
9 | * In these modern times, the segment handling code consists of simple sanity | 10 | * In these modern times, the segment handling code consists of simple sanity |
10 | * checks, and the worst you'll experience reading this code is butterfly-rash | 11 | * checks, and the worst you'll experience reading this code is butterfly-rash |
11 | * from frolicking through its parklike serenity. :*/ | 12 | * from frolicking through its parklike serenity. |
13 | :*/ | ||
12 | #include "lg.h" | 14 | #include "lg.h" |
13 | 15 | ||
14 | /*H:600 | 16 | /*H:600 |
@@ -41,10 +43,12 @@ | |||
41 | * begin. | 43 | * begin. |
42 | */ | 44 | */ |
43 | 45 | ||
44 | /* There are several entries we don't let the Guest set. The TSS entry is the | 46 | /* |
47 | * There are several entries we don't let the Guest set. The TSS entry is the | ||
45 | * "Task State Segment" which controls all kinds of delicate things. The | 48 | * "Task State Segment" which controls all kinds of delicate things. The |
46 | * LGUEST_CS and LGUEST_DS entries are reserved for the Switcher, and the | 49 | * LGUEST_CS and LGUEST_DS entries are reserved for the Switcher, and the |
47 | * the Guest can't be trusted to deal with double faults. */ | 50 | * the Guest can't be trusted to deal with double faults. |
51 | */ | ||
48 | static bool ignored_gdt(unsigned int num) | 52 | static bool ignored_gdt(unsigned int num) |
49 | { | 53 | { |
50 | return (num == GDT_ENTRY_TSS | 54 | return (num == GDT_ENTRY_TSS |
@@ -53,42 +57,52 @@ static bool ignored_gdt(unsigned int num) | |||
53 | || num == GDT_ENTRY_DOUBLEFAULT_TSS); | 57 | || num == GDT_ENTRY_DOUBLEFAULT_TSS); |
54 | } | 58 | } |
55 | 59 | ||
56 | /*H:630 Once the Guest gave us new GDT entries, we fix them up a little. We | 60 | /*H:630 |
61 | * Once the Guest gave us new GDT entries, we fix them up a little. We | ||
57 | * don't care if they're invalid: the worst that can happen is a General | 62 | * don't care if they're invalid: the worst that can happen is a General |
58 | * Protection Fault in the Switcher when it restores a Guest segment register | 63 | * Protection Fault in the Switcher when it restores a Guest segment register |
59 | * which tries to use that entry. Then we kill the Guest for causing such a | 64 | * which tries to use that entry. Then we kill the Guest for causing such a |
60 | * mess: the message will be "unhandled trap 256". */ | 65 | * mess: the message will be "unhandled trap 256". |
66 | */ | ||
61 | static void fixup_gdt_table(struct lg_cpu *cpu, unsigned start, unsigned end) | 67 | static void fixup_gdt_table(struct lg_cpu *cpu, unsigned start, unsigned end) |
62 | { | 68 | { |
63 | unsigned int i; | 69 | unsigned int i; |
64 | 70 | ||
65 | for (i = start; i < end; i++) { | 71 | for (i = start; i < end; i++) { |
66 | /* We never copy these ones to real GDT, so we don't care what | 72 | /* |
67 | * they say */ | 73 | * We never copy these ones to real GDT, so we don't care what |
74 | * they say | ||
75 | */ | ||
68 | if (ignored_gdt(i)) | 76 | if (ignored_gdt(i)) |
69 | continue; | 77 | continue; |
70 | 78 | ||
71 | /* Segment descriptors contain a privilege level: the Guest is | 79 | /* |
80 | * Segment descriptors contain a privilege level: the Guest is | ||
72 | * sometimes careless and leaves this as 0, even though it's | 81 | * sometimes careless and leaves this as 0, even though it's |
73 | * running at privilege level 1. If so, we fix it here. */ | 82 | * running at privilege level 1. If so, we fix it here. |
83 | */ | ||
74 | if ((cpu->arch.gdt[i].b & 0x00006000) == 0) | 84 | if ((cpu->arch.gdt[i].b & 0x00006000) == 0) |
75 | cpu->arch.gdt[i].b |= (GUEST_PL << 13); | 85 | cpu->arch.gdt[i].b |= (GUEST_PL << 13); |
76 | 86 | ||
77 | /* Each descriptor has an "accessed" bit. If we don't set it | 87 | /* |
88 | * Each descriptor has an "accessed" bit. If we don't set it | ||
78 | * now, the CPU will try to set it when the Guest first loads | 89 | * now, the CPU will try to set it when the Guest first loads |
79 | * that entry into a segment register. But the GDT isn't | 90 | * that entry into a segment register. But the GDT isn't |
80 | * writable by the Guest, so bad things can happen. */ | 91 | * writable by the Guest, so bad things can happen. |
92 | */ | ||
81 | cpu->arch.gdt[i].b |= 0x00000100; | 93 | cpu->arch.gdt[i].b |= 0x00000100; |
82 | } | 94 | } |
83 | } | 95 | } |
84 | 96 | ||
85 | /*H:610 Like the IDT, we never simply use the GDT the Guest gives us. We keep | 97 | /*H:610 |
98 | * Like the IDT, we never simply use the GDT the Guest gives us. We keep | ||
86 | * a GDT for each CPU, and copy across the Guest's entries each time we want to | 99 | * a GDT for each CPU, and copy across the Guest's entries each time we want to |
87 | * run the Guest on that CPU. | 100 | * run the Guest on that CPU. |
88 | * | 101 | * |
89 | * This routine is called at boot or modprobe time for each CPU to set up the | 102 | * This routine is called at boot or modprobe time for each CPU to set up the |
90 | * constant GDT entries: the ones which are the same no matter what Guest we're | 103 | * constant GDT entries: the ones which are the same no matter what Guest we're |
91 | * running. */ | 104 | * running. |
105 | */ | ||
92 | void setup_default_gdt_entries(struct lguest_ro_state *state) | 106 | void setup_default_gdt_entries(struct lguest_ro_state *state) |
93 | { | 107 | { |
94 | struct desc_struct *gdt = state->guest_gdt; | 108 | struct desc_struct *gdt = state->guest_gdt; |
@@ -98,30 +112,37 @@ void setup_default_gdt_entries(struct lguest_ro_state *state) | |||
98 | gdt[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT; | 112 | gdt[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT; |
99 | gdt[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT; | 113 | gdt[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT; |
100 | 114 | ||
101 | /* The TSS segment refers to the TSS entry for this particular CPU. | 115 | /* |
116 | * The TSS segment refers to the TSS entry for this particular CPU. | ||
102 | * Forgive the magic flags: the 0x8900 means the entry is Present, it's | 117 | * Forgive the magic flags: the 0x8900 means the entry is Present, it's |
103 | * privilege level 0 Available 386 TSS system segment, and the 0x67 | 118 | * privilege level 0 Available 386 TSS system segment, and the 0x67 |
104 | * means Saturn is eclipsed by Mercury in the twelfth house. */ | 119 | * means Saturn is eclipsed by Mercury in the twelfth house. |
120 | */ | ||
105 | gdt[GDT_ENTRY_TSS].a = 0x00000067 | (tss << 16); | 121 | gdt[GDT_ENTRY_TSS].a = 0x00000067 | (tss << 16); |
106 | gdt[GDT_ENTRY_TSS].b = 0x00008900 | (tss & 0xFF000000) | 122 | gdt[GDT_ENTRY_TSS].b = 0x00008900 | (tss & 0xFF000000) |
107 | | ((tss >> 16) & 0x000000FF); | 123 | | ((tss >> 16) & 0x000000FF); |
108 | } | 124 | } |
109 | 125 | ||
110 | /* This routine sets up the initial Guest GDT for booting. All entries start | 126 | /* |
111 | * as 0 (unusable). */ | 127 | * This routine sets up the initial Guest GDT for booting. All entries start |
128 | * as 0 (unusable). | ||
129 | */ | ||
112 | void setup_guest_gdt(struct lg_cpu *cpu) | 130 | void setup_guest_gdt(struct lg_cpu *cpu) |
113 | { | 131 | { |
114 | /* Start with full 0-4G segments... */ | 132 | /* |
133 | * Start with full 0-4G segments...except the Guest is allowed to use | ||
134 | * them, so set the privilege level appropriately in the flags. | ||
135 | */ | ||
115 | cpu->arch.gdt[GDT_ENTRY_KERNEL_CS] = FULL_EXEC_SEGMENT; | 136 | cpu->arch.gdt[GDT_ENTRY_KERNEL_CS] = FULL_EXEC_SEGMENT; |
116 | cpu->arch.gdt[GDT_ENTRY_KERNEL_DS] = FULL_SEGMENT; | 137 | cpu->arch.gdt[GDT_ENTRY_KERNEL_DS] = FULL_SEGMENT; |
117 | /* ...except the Guest is allowed to use them, so set the privilege | ||
118 | * level appropriately in the flags. */ | ||
119 | cpu->arch.gdt[GDT_ENTRY_KERNEL_CS].b |= (GUEST_PL << 13); | 138 | cpu->arch.gdt[GDT_ENTRY_KERNEL_CS].b |= (GUEST_PL << 13); |
120 | cpu->arch.gdt[GDT_ENTRY_KERNEL_DS].b |= (GUEST_PL << 13); | 139 | cpu->arch.gdt[GDT_ENTRY_KERNEL_DS].b |= (GUEST_PL << 13); |
121 | } | 140 | } |
122 | 141 | ||
123 | /*H:650 An optimization of copy_gdt(), for just the three "thead-local storage" | 142 | /*H:650 |
124 | * entries. */ | 143 | * An optimization of copy_gdt(), for just the three "thead-local storage" |
144 | * entries. | ||
145 | */ | ||
125 | void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt) | 146 | void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt) |
126 | { | 147 | { |
127 | unsigned int i; | 148 | unsigned int i; |
@@ -130,26 +151,34 @@ void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt) | |||
130 | gdt[i] = cpu->arch.gdt[i]; | 151 | gdt[i] = cpu->arch.gdt[i]; |
131 | } | 152 | } |
132 | 153 | ||
133 | /*H:640 When the Guest is run on a different CPU, or the GDT entries have | 154 | /*H:640 |
134 | * changed, copy_gdt() is called to copy the Guest's GDT entries across to this | 155 | * When the Guest is run on a different CPU, or the GDT entries have changed, |
135 | * CPU's GDT. */ | 156 | * copy_gdt() is called to copy the Guest's GDT entries across to this CPU's |
157 | * GDT. | ||
158 | */ | ||
136 | void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt) | 159 | void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt) |
137 | { | 160 | { |
138 | unsigned int i; | 161 | unsigned int i; |
139 | 162 | ||
140 | /* The default entries from setup_default_gdt_entries() are not | 163 | /* |
141 | * replaced. See ignored_gdt() above. */ | 164 | * The default entries from setup_default_gdt_entries() are not |
165 | * replaced. See ignored_gdt() above. | ||
166 | */ | ||
142 | for (i = 0; i < GDT_ENTRIES; i++) | 167 | for (i = 0; i < GDT_ENTRIES; i++) |
143 | if (!ignored_gdt(i)) | 168 | if (!ignored_gdt(i)) |
144 | gdt[i] = cpu->arch.gdt[i]; | 169 | gdt[i] = cpu->arch.gdt[i]; |
145 | } | 170 | } |
146 | 171 | ||
147 | /*H:620 This is where the Guest asks us to load a new GDT entry | 172 | /*H:620 |
148 | * (LHCALL_LOAD_GDT_ENTRY). We tweak the entry and copy it in. */ | 173 | * This is where the Guest asks us to load a new GDT entry |
174 | * (LHCALL_LOAD_GDT_ENTRY). We tweak the entry and copy it in. | ||
175 | */ | ||
149 | void load_guest_gdt_entry(struct lg_cpu *cpu, u32 num, u32 lo, u32 hi) | 176 | void load_guest_gdt_entry(struct lg_cpu *cpu, u32 num, u32 lo, u32 hi) |
150 | { | 177 | { |
151 | /* We assume the Guest has the same number of GDT entries as the | 178 | /* |
152 | * Host, otherwise we'd have to dynamically allocate the Guest GDT. */ | 179 | * We assume the Guest has the same number of GDT entries as the |
180 | * Host, otherwise we'd have to dynamically allocate the Guest GDT. | ||
181 | */ | ||
153 | if (num >= ARRAY_SIZE(cpu->arch.gdt)) | 182 | if (num >= ARRAY_SIZE(cpu->arch.gdt)) |
154 | kill_guest(cpu, "too many gdt entries %i", num); | 183 | kill_guest(cpu, "too many gdt entries %i", num); |
155 | 184 | ||
@@ -157,15 +186,19 @@ void load_guest_gdt_entry(struct lg_cpu *cpu, u32 num, u32 lo, u32 hi) | |||
157 | cpu->arch.gdt[num].a = lo; | 186 | cpu->arch.gdt[num].a = lo; |
158 | cpu->arch.gdt[num].b = hi; | 187 | cpu->arch.gdt[num].b = hi; |
159 | fixup_gdt_table(cpu, num, num+1); | 188 | fixup_gdt_table(cpu, num, num+1); |
160 | /* Mark that the GDT changed so the core knows it has to copy it again, | 189 | /* |
161 | * even if the Guest is run on the same CPU. */ | 190 | * Mark that the GDT changed so the core knows it has to copy it again, |
191 | * even if the Guest is run on the same CPU. | ||
192 | */ | ||
162 | cpu->changed |= CHANGED_GDT; | 193 | cpu->changed |= CHANGED_GDT; |
163 | } | 194 | } |
164 | 195 | ||
165 | /* This is the fast-track version for just changing the three TLS entries. | 196 | /* |
197 | * This is the fast-track version for just changing the three TLS entries. | ||
166 | * Remember that this happens on every context switch, so it's worth | 198 | * Remember that this happens on every context switch, so it's worth |
167 | * optimizing. But wouldn't it be neater to have a single hypercall to cover | 199 | * optimizing. But wouldn't it be neater to have a single hypercall to cover |
168 | * both cases? */ | 200 | * both cases? |
201 | */ | ||
169 | void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls) | 202 | void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls) |
170 | { | 203 | { |
171 | struct desc_struct *tls = &cpu->arch.gdt[GDT_ENTRY_TLS_MIN]; | 204 | struct desc_struct *tls = &cpu->arch.gdt[GDT_ENTRY_TLS_MIN]; |
@@ -175,7 +208,6 @@ void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls) | |||
175 | /* Note that just the TLS entries have changed. */ | 208 | /* Note that just the TLS entries have changed. */ |
176 | cpu->changed |= CHANGED_GDT_TLS; | 209 | cpu->changed |= CHANGED_GDT_TLS; |
177 | } | 210 | } |
178 | /*:*/ | ||
179 | 211 | ||
180 | /*H:660 | 212 | /*H:660 |
181 | * With this, we have finished the Host. | 213 | * With this, we have finished the Host. |
diff --git a/drivers/lguest/x86/core.c b/drivers/lguest/x86/core.c index eaf722fe309a..96f7d88ec7f8 100644 --- a/drivers/lguest/x86/core.c +++ b/drivers/lguest/x86/core.c | |||
@@ -17,13 +17,15 @@ | |||
17 | * along with this program; if not, write to the Free Software | 17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | */ | 19 | */ |
20 | /*P:450 This file contains the x86-specific lguest code. It used to be all | 20 | /*P:450 |
21 | * This file contains the x86-specific lguest code. It used to be all | ||
21 | * mixed in with drivers/lguest/core.c but several foolhardy code slashers | 22 | * mixed in with drivers/lguest/core.c but several foolhardy code slashers |
22 | * wrestled most of the dependencies out to here in preparation for porting | 23 | * wrestled most of the dependencies out to here in preparation for porting |
23 | * lguest to other architectures (see what I mean by foolhardy?). | 24 | * lguest to other architectures (see what I mean by foolhardy?). |
24 | * | 25 | * |
25 | * This also contains a couple of non-obvious setup and teardown pieces which | 26 | * This also contains a couple of non-obvious setup and teardown pieces which |
26 | * were implemented after days of debugging pain. :*/ | 27 | * were implemented after days of debugging pain. |
28 | :*/ | ||
27 | #include <linux/kernel.h> | 29 | #include <linux/kernel.h> |
28 | #include <linux/start_kernel.h> | 30 | #include <linux/start_kernel.h> |
29 | #include <linux/string.h> | 31 | #include <linux/string.h> |
@@ -82,25 +84,33 @@ static DEFINE_PER_CPU(struct lg_cpu *, last_cpu); | |||
82 | */ | 84 | */ |
83 | static void copy_in_guest_info(struct lg_cpu *cpu, struct lguest_pages *pages) | 85 | static void copy_in_guest_info(struct lg_cpu *cpu, struct lguest_pages *pages) |
84 | { | 86 | { |
85 | /* Copying all this data can be quite expensive. We usually run the | 87 | /* |
88 | * Copying all this data can be quite expensive. We usually run the | ||
86 | * same Guest we ran last time (and that Guest hasn't run anywhere else | 89 | * same Guest we ran last time (and that Guest hasn't run anywhere else |
87 | * meanwhile). If that's not the case, we pretend everything in the | 90 | * meanwhile). If that's not the case, we pretend everything in the |
88 | * Guest has changed. */ | 91 | * Guest has changed. |
92 | */ | ||
89 | if (__get_cpu_var(last_cpu) != cpu || cpu->last_pages != pages) { | 93 | if (__get_cpu_var(last_cpu) != cpu || cpu->last_pages != pages) { |
90 | __get_cpu_var(last_cpu) = cpu; | 94 | __get_cpu_var(last_cpu) = cpu; |
91 | cpu->last_pages = pages; | 95 | cpu->last_pages = pages; |
92 | cpu->changed = CHANGED_ALL; | 96 | cpu->changed = CHANGED_ALL; |
93 | } | 97 | } |
94 | 98 | ||
95 | /* These copies are pretty cheap, so we do them unconditionally: */ | 99 | /* |
96 | /* Save the current Host top-level page directory. */ | 100 | * These copies are pretty cheap, so we do them unconditionally: */ |
101 | /* Save the current Host top-level page directory. | ||
102 | */ | ||
97 | pages->state.host_cr3 = __pa(current->mm->pgd); | 103 | pages->state.host_cr3 = __pa(current->mm->pgd); |
98 | /* Set up the Guest's page tables to see this CPU's pages (and no | 104 | /* |
99 | * other CPU's pages). */ | 105 | * Set up the Guest's page tables to see this CPU's pages (and no |
106 | * other CPU's pages). | ||
107 | */ | ||
100 | map_switcher_in_guest(cpu, pages); | 108 | map_switcher_in_guest(cpu, pages); |
101 | /* Set up the two "TSS" members which tell the CPU what stack to use | 109 | /* |
110 | * Set up the two "TSS" members which tell the CPU what stack to use | ||
102 | * for traps which do directly into the Guest (ie. traps at privilege | 111 | * for traps which do directly into the Guest (ie. traps at privilege |
103 | * level 1). */ | 112 | * level 1). |
113 | */ | ||
104 | pages->state.guest_tss.sp1 = cpu->esp1; | 114 | pages->state.guest_tss.sp1 = cpu->esp1; |
105 | pages->state.guest_tss.ss1 = cpu->ss1; | 115 | pages->state.guest_tss.ss1 = cpu->ss1; |
106 | 116 | ||
@@ -125,40 +135,53 @@ static void run_guest_once(struct lg_cpu *cpu, struct lguest_pages *pages) | |||
125 | /* This is a dummy value we need for GCC's sake. */ | 135 | /* This is a dummy value we need for GCC's sake. */ |
126 | unsigned int clobber; | 136 | unsigned int clobber; |
127 | 137 | ||
128 | /* Copy the guest-specific information into this CPU's "struct | 138 | /* |
129 | * lguest_pages". */ | 139 | * Copy the guest-specific information into this CPU's "struct |
140 | * lguest_pages". | ||
141 | */ | ||
130 | copy_in_guest_info(cpu, pages); | 142 | copy_in_guest_info(cpu, pages); |
131 | 143 | ||
132 | /* Set the trap number to 256 (impossible value). If we fault while | 144 | /* |
145 | * Set the trap number to 256 (impossible value). If we fault while | ||
133 | * switching to the Guest (bad segment registers or bug), this will | 146 | * switching to the Guest (bad segment registers or bug), this will |
134 | * cause us to abort the Guest. */ | 147 | * cause us to abort the Guest. |
148 | */ | ||
135 | cpu->regs->trapnum = 256; | 149 | cpu->regs->trapnum = 256; |
136 | 150 | ||
137 | /* Now: we push the "eflags" register on the stack, then do an "lcall". | 151 | /* |
152 | * Now: we push the "eflags" register on the stack, then do an "lcall". | ||
138 | * This is how we change from using the kernel code segment to using | 153 | * This is how we change from using the kernel code segment to using |
139 | * the dedicated lguest code segment, as well as jumping into the | 154 | * the dedicated lguest code segment, as well as jumping into the |
140 | * Switcher. | 155 | * Switcher. |
141 | * | 156 | * |
142 | * The lcall also pushes the old code segment (KERNEL_CS) onto the | 157 | * The lcall also pushes the old code segment (KERNEL_CS) onto the |
143 | * stack, then the address of this call. This stack layout happens to | 158 | * stack, then the address of this call. This stack layout happens to |
144 | * exactly match the stack layout created by an interrupt... */ | 159 | * exactly match the stack layout created by an interrupt... |
160 | */ | ||
145 | asm volatile("pushf; lcall *lguest_entry" | 161 | asm volatile("pushf; lcall *lguest_entry" |
146 | /* This is how we tell GCC that %eax ("a") and %ebx ("b") | 162 | /* |
147 | * are changed by this routine. The "=" means output. */ | 163 | * This is how we tell GCC that %eax ("a") and %ebx ("b") |
164 | * are changed by this routine. The "=" means output. | ||
165 | */ | ||
148 | : "=a"(clobber), "=b"(clobber) | 166 | : "=a"(clobber), "=b"(clobber) |
149 | /* %eax contains the pages pointer. ("0" refers to the | 167 | /* |
168 | * %eax contains the pages pointer. ("0" refers to the | ||
150 | * 0-th argument above, ie "a"). %ebx contains the | 169 | * 0-th argument above, ie "a"). %ebx contains the |
151 | * physical address of the Guest's top-level page | 170 | * physical address of the Guest's top-level page |
152 | * directory. */ | 171 | * directory. |
172 | */ | ||
153 | : "0"(pages), "1"(__pa(cpu->lg->pgdirs[cpu->cpu_pgd].pgdir)) | 173 | : "0"(pages), "1"(__pa(cpu->lg->pgdirs[cpu->cpu_pgd].pgdir)) |
154 | /* We tell gcc that all these registers could change, | 174 | /* |
175 | * We tell gcc that all these registers could change, | ||
155 | * which means we don't have to save and restore them in | 176 | * which means we don't have to save and restore them in |
156 | * the Switcher. */ | 177 | * the Switcher. |
178 | */ | ||
157 | : "memory", "%edx", "%ecx", "%edi", "%esi"); | 179 | : "memory", "%edx", "%ecx", "%edi", "%esi"); |
158 | } | 180 | } |
159 | /*:*/ | 181 | /*:*/ |
160 | 182 | ||
161 | /*M:002 There are hooks in the scheduler which we can register to tell when we | 183 | /*M:002 |
184 | * There are hooks in the scheduler which we can register to tell when we | ||
162 | * get kicked off the CPU (preempt_notifier_register()). This would allow us | 185 | * get kicked off the CPU (preempt_notifier_register()). This would allow us |
163 | * to lazily disable SYSENTER which would regain some performance, and should | 186 | * to lazily disable SYSENTER which would regain some performance, and should |
164 | * also simplify copy_in_guest_info(). Note that we'd still need to restore | 187 | * also simplify copy_in_guest_info(). Note that we'd still need to restore |
@@ -166,56 +189,72 @@ static void run_guest_once(struct lg_cpu *cpu, struct lguest_pages *pages) | |||
166 | * | 189 | * |
167 | * We could also try using this hooks for PGE, but that might be too expensive. | 190 | * We could also try using this hooks for PGE, but that might be too expensive. |
168 | * | 191 | * |
169 | * The hooks were designed for KVM, but we can also put them to good use. :*/ | 192 | * The hooks were designed for KVM, but we can also put them to good use. |
193 | :*/ | ||
170 | 194 | ||
171 | /*H:040 This is the i386-specific code to setup and run the Guest. Interrupts | 195 | /*H:040 |
172 | * are disabled: we own the CPU. */ | 196 | * This is the i386-specific code to setup and run the Guest. Interrupts |
197 | * are disabled: we own the CPU. | ||
198 | */ | ||
173 | void lguest_arch_run_guest(struct lg_cpu *cpu) | 199 | void lguest_arch_run_guest(struct lg_cpu *cpu) |
174 | { | 200 | { |
175 | /* Remember the awfully-named TS bit? If the Guest has asked to set it | 201 | /* |
202 | * Remember the awfully-named TS bit? If the Guest has asked to set it | ||
176 | * we set it now, so we can trap and pass that trap to the Guest if it | 203 | * we set it now, so we can trap and pass that trap to the Guest if it |
177 | * uses the FPU. */ | 204 | * uses the FPU. |
205 | */ | ||
178 | if (cpu->ts) | 206 | if (cpu->ts) |
179 | unlazy_fpu(current); | 207 | unlazy_fpu(current); |
180 | 208 | ||
181 | /* SYSENTER is an optimized way of doing system calls. We can't allow | 209 | /* |
210 | * SYSENTER is an optimized way of doing system calls. We can't allow | ||
182 | * it because it always jumps to privilege level 0. A normal Guest | 211 | * it because it always jumps to privilege level 0. A normal Guest |
183 | * won't try it because we don't advertise it in CPUID, but a malicious | 212 | * won't try it because we don't advertise it in CPUID, but a malicious |
184 | * Guest (or malicious Guest userspace program) could, so we tell the | 213 | * Guest (or malicious Guest userspace program) could, so we tell the |
185 | * CPU to disable it before running the Guest. */ | 214 | * CPU to disable it before running the Guest. |
215 | */ | ||
186 | if (boot_cpu_has(X86_FEATURE_SEP)) | 216 | if (boot_cpu_has(X86_FEATURE_SEP)) |
187 | wrmsr(MSR_IA32_SYSENTER_CS, 0, 0); | 217 | wrmsr(MSR_IA32_SYSENTER_CS, 0, 0); |
188 | 218 | ||
189 | /* Now we actually run the Guest. It will return when something | 219 | /* |
220 | * Now we actually run the Guest. It will return when something | ||
190 | * interesting happens, and we can examine its registers to see what it | 221 | * interesting happens, and we can examine its registers to see what it |
191 | * was doing. */ | 222 | * was doing. |
223 | */ | ||
192 | run_guest_once(cpu, lguest_pages(raw_smp_processor_id())); | 224 | run_guest_once(cpu, lguest_pages(raw_smp_processor_id())); |
193 | 225 | ||
194 | /* Note that the "regs" structure contains two extra entries which are | 226 | /* |
227 | * Note that the "regs" structure contains two extra entries which are | ||
195 | * not really registers: a trap number which says what interrupt or | 228 | * not really registers: a trap number which says what interrupt or |
196 | * trap made the switcher code come back, and an error code which some | 229 | * trap made the switcher code come back, and an error code which some |
197 | * traps set. */ | 230 | * traps set. |
231 | */ | ||
198 | 232 | ||
199 | /* Restore SYSENTER if it's supposed to be on. */ | 233 | /* Restore SYSENTER if it's supposed to be on. */ |
200 | if (boot_cpu_has(X86_FEATURE_SEP)) | 234 | if (boot_cpu_has(X86_FEATURE_SEP)) |
201 | wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0); | 235 | wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0); |
202 | 236 | ||
203 | /* If the Guest page faulted, then the cr2 register will tell us the | 237 | /* |
238 | * If the Guest page faulted, then the cr2 register will tell us the | ||
204 | * bad virtual address. We have to grab this now, because once we | 239 | * bad virtual address. We have to grab this now, because once we |
205 | * re-enable interrupts an interrupt could fault and thus overwrite | 240 | * re-enable interrupts an interrupt could fault and thus overwrite |
206 | * cr2, or we could even move off to a different CPU. */ | 241 | * cr2, or we could even move off to a different CPU. |
242 | */ | ||
207 | if (cpu->regs->trapnum == 14) | 243 | if (cpu->regs->trapnum == 14) |
208 | cpu->arch.last_pagefault = read_cr2(); | 244 | cpu->arch.last_pagefault = read_cr2(); |
209 | /* Similarly, if we took a trap because the Guest used the FPU, | 245 | /* |
246 | * Similarly, if we took a trap because the Guest used the FPU, | ||
210 | * we have to restore the FPU it expects to see. | 247 | * we have to restore the FPU it expects to see. |
211 | * math_state_restore() may sleep and we may even move off to | 248 | * math_state_restore() may sleep and we may even move off to |
212 | * a different CPU. So all the critical stuff should be done | 249 | * a different CPU. So all the critical stuff should be done |
213 | * before this. */ | 250 | * before this. |
251 | */ | ||
214 | else if (cpu->regs->trapnum == 7) | 252 | else if (cpu->regs->trapnum == 7) |
215 | math_state_restore(); | 253 | math_state_restore(); |
216 | } | 254 | } |
217 | 255 | ||
218 | /*H:130 Now we've examined the hypercall code; our Guest can make requests. | 256 | /*H:130 |
257 | * Now we've examined the hypercall code; our Guest can make requests. | ||
219 | * Our Guest is usually so well behaved; it never tries to do things it isn't | 258 | * Our Guest is usually so well behaved; it never tries to do things it isn't |
220 | * allowed to, and uses hypercalls instead. Unfortunately, Linux's paravirtual | 259 | * allowed to, and uses hypercalls instead. Unfortunately, Linux's paravirtual |
221 | * infrastructure isn't quite complete, because it doesn't contain replacements | 260 | * infrastructure isn't quite complete, because it doesn't contain replacements |
@@ -225,26 +264,33 @@ void lguest_arch_run_guest(struct lg_cpu *cpu) | |||
225 | * | 264 | * |
226 | * When the Guest uses one of these instructions, we get a trap (General | 265 | * When the Guest uses one of these instructions, we get a trap (General |
227 | * Protection Fault) and come here. We see if it's one of those troublesome | 266 | * Protection Fault) and come here. We see if it's one of those troublesome |
228 | * instructions and skip over it. We return true if we did. */ | 267 | * instructions and skip over it. We return true if we did. |
268 | */ | ||
229 | static int emulate_insn(struct lg_cpu *cpu) | 269 | static int emulate_insn(struct lg_cpu *cpu) |
230 | { | 270 | { |
231 | u8 insn; | 271 | u8 insn; |
232 | unsigned int insnlen = 0, in = 0, shift = 0; | 272 | unsigned int insnlen = 0, in = 0, shift = 0; |
233 | /* The eip contains the *virtual* address of the Guest's instruction: | 273 | /* |
234 | * guest_pa just subtracts the Guest's page_offset. */ | 274 | * The eip contains the *virtual* address of the Guest's instruction: |
275 | * guest_pa just subtracts the Guest's page_offset. | ||
276 | */ | ||
235 | unsigned long physaddr = guest_pa(cpu, cpu->regs->eip); | 277 | unsigned long physaddr = guest_pa(cpu, cpu->regs->eip); |
236 | 278 | ||
237 | /* This must be the Guest kernel trying to do something, not userspace! | 279 | /* |
280 | * This must be the Guest kernel trying to do something, not userspace! | ||
238 | * The bottom two bits of the CS segment register are the privilege | 281 | * The bottom two bits of the CS segment register are the privilege |
239 | * level. */ | 282 | * level. |
283 | */ | ||
240 | if ((cpu->regs->cs & 3) != GUEST_PL) | 284 | if ((cpu->regs->cs & 3) != GUEST_PL) |
241 | return 0; | 285 | return 0; |
242 | 286 | ||
243 | /* Decoding x86 instructions is icky. */ | 287 | /* Decoding x86 instructions is icky. */ |
244 | insn = lgread(cpu, physaddr, u8); | 288 | insn = lgread(cpu, physaddr, u8); |
245 | 289 | ||
246 | /* 0x66 is an "operand prefix". It means it's using the upper 16 bits | 290 | /* |
247 | of the eax register. */ | 291 | * 0x66 is an "operand prefix". It means it's using the upper 16 bits |
292 | * of the eax register. | ||
293 | */ | ||
248 | if (insn == 0x66) { | 294 | if (insn == 0x66) { |
249 | shift = 16; | 295 | shift = 16; |
250 | /* The instruction is 1 byte so far, read the next byte. */ | 296 | /* The instruction is 1 byte so far, read the next byte. */ |
@@ -252,8 +298,10 @@ static int emulate_insn(struct lg_cpu *cpu) | |||
252 | insn = lgread(cpu, physaddr + insnlen, u8); | 298 | insn = lgread(cpu, physaddr + insnlen, u8); |
253 | } | 299 | } |
254 | 300 | ||
255 | /* We can ignore the lower bit for the moment and decode the 4 opcodes | 301 | /* |
256 | * we need to emulate. */ | 302 | * We can ignore the lower bit for the moment and decode the 4 opcodes |
303 | * we need to emulate. | ||
304 | */ | ||
257 | switch (insn & 0xFE) { | 305 | switch (insn & 0xFE) { |
258 | case 0xE4: /* in <next byte>,%al */ | 306 | case 0xE4: /* in <next byte>,%al */ |
259 | insnlen += 2; | 307 | insnlen += 2; |
@@ -274,9 +322,11 @@ static int emulate_insn(struct lg_cpu *cpu) | |||
274 | return 0; | 322 | return 0; |
275 | } | 323 | } |
276 | 324 | ||
277 | /* If it was an "IN" instruction, they expect the result to be read | 325 | /* |
326 | * If it was an "IN" instruction, they expect the result to be read | ||
278 | * into %eax, so we change %eax. We always return all-ones, which | 327 | * into %eax, so we change %eax. We always return all-ones, which |
279 | * traditionally means "there's nothing there". */ | 328 | * traditionally means "there's nothing there". |
329 | */ | ||
280 | if (in) { | 330 | if (in) { |
281 | /* Lower bit tells is whether it's a 16 or 32 bit access */ | 331 | /* Lower bit tells is whether it's a 16 or 32 bit access */ |
282 | if (insn & 0x1) | 332 | if (insn & 0x1) |
@@ -290,7 +340,8 @@ static int emulate_insn(struct lg_cpu *cpu) | |||
290 | return 1; | 340 | return 1; |
291 | } | 341 | } |
292 | 342 | ||
293 | /* Our hypercalls mechanism used to be based on direct software interrupts. | 343 | /* |
344 | * Our hypercalls mechanism used to be based on direct software interrupts. | ||
294 | * After Anthony's "Refactor hypercall infrastructure" kvm patch, we decided to | 345 | * After Anthony's "Refactor hypercall infrastructure" kvm patch, we decided to |
295 | * change over to using kvm hypercalls. | 346 | * change over to using kvm hypercalls. |
296 | * | 347 | * |
@@ -318,16 +369,20 @@ static int emulate_insn(struct lg_cpu *cpu) | |||
318 | */ | 369 | */ |
319 | static void rewrite_hypercall(struct lg_cpu *cpu) | 370 | static void rewrite_hypercall(struct lg_cpu *cpu) |
320 | { | 371 | { |
321 | /* This are the opcodes we use to patch the Guest. The opcode for "int | 372 | /* |
373 | * This are the opcodes we use to patch the Guest. The opcode for "int | ||
322 | * $0x1f" is "0xcd 0x1f" but vmcall instruction is 3 bytes long, so we | 374 | * $0x1f" is "0xcd 0x1f" but vmcall instruction is 3 bytes long, so we |
323 | * complete the sequence with a NOP (0x90). */ | 375 | * complete the sequence with a NOP (0x90). |
376 | */ | ||
324 | u8 insn[3] = {0xcd, 0x1f, 0x90}; | 377 | u8 insn[3] = {0xcd, 0x1f, 0x90}; |
325 | 378 | ||
326 | __lgwrite(cpu, guest_pa(cpu, cpu->regs->eip), insn, sizeof(insn)); | 379 | __lgwrite(cpu, guest_pa(cpu, cpu->regs->eip), insn, sizeof(insn)); |
327 | /* The above write might have caused a copy of that page to be made | 380 | /* |
381 | * The above write might have caused a copy of that page to be made | ||
328 | * (if it was read-only). We need to make sure the Guest has | 382 | * (if it was read-only). We need to make sure the Guest has |
329 | * up-to-date pagetables. As this doesn't happen often, we can just | 383 | * up-to-date pagetables. As this doesn't happen often, we can just |
330 | * drop them all. */ | 384 | * drop them all. |
385 | */ | ||
331 | guest_pagetable_clear_all(cpu); | 386 | guest_pagetable_clear_all(cpu); |
332 | } | 387 | } |
333 | 388 | ||
@@ -335,9 +390,11 @@ static bool is_hypercall(struct lg_cpu *cpu) | |||
335 | { | 390 | { |
336 | u8 insn[3]; | 391 | u8 insn[3]; |
337 | 392 | ||
338 | /* This must be the Guest kernel trying to do something. | 393 | /* |
394 | * This must be the Guest kernel trying to do something. | ||
339 | * The bottom two bits of the CS segment register are the privilege | 395 | * The bottom two bits of the CS segment register are the privilege |
340 | * level. */ | 396 | * level. |
397 | */ | ||
341 | if ((cpu->regs->cs & 3) != GUEST_PL) | 398 | if ((cpu->regs->cs & 3) != GUEST_PL) |
342 | return false; | 399 | return false; |
343 | 400 | ||
@@ -351,86 +408,105 @@ void lguest_arch_handle_trap(struct lg_cpu *cpu) | |||
351 | { | 408 | { |
352 | switch (cpu->regs->trapnum) { | 409 | switch (cpu->regs->trapnum) { |
353 | case 13: /* We've intercepted a General Protection Fault. */ | 410 | case 13: /* We've intercepted a General Protection Fault. */ |
354 | /* Check if this was one of those annoying IN or OUT | 411 | /* |
412 | * Check if this was one of those annoying IN or OUT | ||
355 | * instructions which we need to emulate. If so, we just go | 413 | * instructions which we need to emulate. If so, we just go |
356 | * back into the Guest after we've done it. */ | 414 | * back into the Guest after we've done it. |
415 | */ | ||
357 | if (cpu->regs->errcode == 0) { | 416 | if (cpu->regs->errcode == 0) { |
358 | if (emulate_insn(cpu)) | 417 | if (emulate_insn(cpu)) |
359 | return; | 418 | return; |
360 | } | 419 | } |
361 | /* If KVM is active, the vmcall instruction triggers a | 420 | /* |
362 | * General Protection Fault. Normally it triggers an | 421 | * If KVM is active, the vmcall instruction triggers a General |
363 | * invalid opcode fault (6): */ | 422 | * Protection Fault. Normally it triggers an invalid opcode |
423 | * fault (6): | ||
424 | */ | ||
364 | case 6: | 425 | case 6: |
365 | /* We need to check if ring == GUEST_PL and | 426 | /* |
366 | * faulting instruction == vmcall. */ | 427 | * We need to check if ring == GUEST_PL and faulting |
428 | * instruction == vmcall. | ||
429 | */ | ||
367 | if (is_hypercall(cpu)) { | 430 | if (is_hypercall(cpu)) { |
368 | rewrite_hypercall(cpu); | 431 | rewrite_hypercall(cpu); |
369 | return; | 432 | return; |
370 | } | 433 | } |
371 | break; | 434 | break; |
372 | case 14: /* We've intercepted a Page Fault. */ | 435 | case 14: /* We've intercepted a Page Fault. */ |
373 | /* The Guest accessed a virtual address that wasn't mapped. | 436 | /* |
437 | * The Guest accessed a virtual address that wasn't mapped. | ||
374 | * This happens a lot: we don't actually set up most of the page | 438 | * This happens a lot: we don't actually set up most of the page |
375 | * tables for the Guest at all when we start: as it runs it asks | 439 | * tables for the Guest at all when we start: as it runs it asks |
376 | * for more and more, and we set them up as required. In this | 440 | * for more and more, and we set them up as required. In this |
377 | * case, we don't even tell the Guest that the fault happened. | 441 | * case, we don't even tell the Guest that the fault happened. |
378 | * | 442 | * |
379 | * The errcode tells whether this was a read or a write, and | 443 | * The errcode tells whether this was a read or a write, and |
380 | * whether kernel or userspace code. */ | 444 | * whether kernel or userspace code. |
445 | */ | ||
381 | if (demand_page(cpu, cpu->arch.last_pagefault, | 446 | if (demand_page(cpu, cpu->arch.last_pagefault, |
382 | cpu->regs->errcode)) | 447 | cpu->regs->errcode)) |
383 | return; | 448 | return; |
384 | 449 | ||
385 | /* OK, it's really not there (or not OK): the Guest needs to | 450 | /* |
451 | * OK, it's really not there (or not OK): the Guest needs to | ||
386 | * know. We write out the cr2 value so it knows where the | 452 | * know. We write out the cr2 value so it knows where the |
387 | * fault occurred. | 453 | * fault occurred. |
388 | * | 454 | * |
389 | * Note that if the Guest were really messed up, this could | 455 | * Note that if the Guest were really messed up, this could |
390 | * happen before it's done the LHCALL_LGUEST_INIT hypercall, so | 456 | * happen before it's done the LHCALL_LGUEST_INIT hypercall, so |
391 | * lg->lguest_data could be NULL */ | 457 | * lg->lguest_data could be NULL |
458 | */ | ||
392 | if (cpu->lg->lguest_data && | 459 | if (cpu->lg->lguest_data && |
393 | put_user(cpu->arch.last_pagefault, | 460 | put_user(cpu->arch.last_pagefault, |
394 | &cpu->lg->lguest_data->cr2)) | 461 | &cpu->lg->lguest_data->cr2)) |
395 | kill_guest(cpu, "Writing cr2"); | 462 | kill_guest(cpu, "Writing cr2"); |
396 | break; | 463 | break; |
397 | case 7: /* We've intercepted a Device Not Available fault. */ | 464 | case 7: /* We've intercepted a Device Not Available fault. */ |
398 | /* If the Guest doesn't want to know, we already restored the | 465 | /* |
399 | * Floating Point Unit, so we just continue without telling | 466 | * If the Guest doesn't want to know, we already restored the |
400 | * it. */ | 467 | * Floating Point Unit, so we just continue without telling it. |
468 | */ | ||
401 | if (!cpu->ts) | 469 | if (!cpu->ts) |
402 | return; | 470 | return; |
403 | break; | 471 | break; |
404 | case 32 ... 255: | 472 | case 32 ... 255: |
405 | /* These values mean a real interrupt occurred, in which case | 473 | /* |
474 | * These values mean a real interrupt occurred, in which case | ||
406 | * the Host handler has already been run. We just do a | 475 | * the Host handler has already been run. We just do a |
407 | * friendly check if another process should now be run, then | 476 | * friendly check if another process should now be run, then |
408 | * return to run the Guest again */ | 477 | * return to run the Guest again |
478 | */ | ||
409 | cond_resched(); | 479 | cond_resched(); |
410 | return; | 480 | return; |
411 | case LGUEST_TRAP_ENTRY: | 481 | case LGUEST_TRAP_ENTRY: |
412 | /* Our 'struct hcall_args' maps directly over our regs: we set | 482 | /* |
413 | * up the pointer now to indicate a hypercall is pending. */ | 483 | * Our 'struct hcall_args' maps directly over our regs: we set |
484 | * up the pointer now to indicate a hypercall is pending. | ||
485 | */ | ||
414 | cpu->hcall = (struct hcall_args *)cpu->regs; | 486 | cpu->hcall = (struct hcall_args *)cpu->regs; |
415 | return; | 487 | return; |
416 | } | 488 | } |
417 | 489 | ||
418 | /* We didn't handle the trap, so it needs to go to the Guest. */ | 490 | /* We didn't handle the trap, so it needs to go to the Guest. */ |
419 | if (!deliver_trap(cpu, cpu->regs->trapnum)) | 491 | if (!deliver_trap(cpu, cpu->regs->trapnum)) |
420 | /* If the Guest doesn't have a handler (either it hasn't | 492 | /* |
493 | * If the Guest doesn't have a handler (either it hasn't | ||
421 | * registered any yet, or it's one of the faults we don't let | 494 | * registered any yet, or it's one of the faults we don't let |
422 | * it handle), it dies with this cryptic error message. */ | 495 | * it handle), it dies with this cryptic error message. |
496 | */ | ||
423 | kill_guest(cpu, "unhandled trap %li at %#lx (%#lx)", | 497 | kill_guest(cpu, "unhandled trap %li at %#lx (%#lx)", |
424 | cpu->regs->trapnum, cpu->regs->eip, | 498 | cpu->regs->trapnum, cpu->regs->eip, |
425 | cpu->regs->trapnum == 14 ? cpu->arch.last_pagefault | 499 | cpu->regs->trapnum == 14 ? cpu->arch.last_pagefault |
426 | : cpu->regs->errcode); | 500 | : cpu->regs->errcode); |
427 | } | 501 | } |
428 | 502 | ||
429 | /* Now we can look at each of the routines this calls, in increasing order of | 503 | /* |
504 | * Now we can look at each of the routines this calls, in increasing order of | ||
430 | * complexity: do_hypercalls(), emulate_insn(), maybe_do_interrupt(), | 505 | * complexity: do_hypercalls(), emulate_insn(), maybe_do_interrupt(), |
431 | * deliver_trap() and demand_page(). After all those, we'll be ready to | 506 | * deliver_trap() and demand_page(). After all those, we'll be ready to |
432 | * examine the Switcher, and our philosophical understanding of the Host/Guest | 507 | * examine the Switcher, and our philosophical understanding of the Host/Guest |
433 | * duality will be complete. :*/ | 508 | * duality will be complete. |
509 | :*/ | ||
434 | static void adjust_pge(void *on) | 510 | static void adjust_pge(void *on) |
435 | { | 511 | { |
436 | if (on) | 512 | if (on) |
@@ -439,13 +515,16 @@ static void adjust_pge(void *on) | |||
439 | write_cr4(read_cr4() & ~X86_CR4_PGE); | 515 | write_cr4(read_cr4() & ~X86_CR4_PGE); |
440 | } | 516 | } |
441 | 517 | ||
442 | /*H:020 Now the Switcher is mapped and every thing else is ready, we need to do | 518 | /*H:020 |
443 | * some more i386-specific initialization. */ | 519 | * Now the Switcher is mapped and every thing else is ready, we need to do |
520 | * some more i386-specific initialization. | ||
521 | */ | ||
444 | void __init lguest_arch_host_init(void) | 522 | void __init lguest_arch_host_init(void) |
445 | { | 523 | { |
446 | int i; | 524 | int i; |
447 | 525 | ||
448 | /* Most of the i386/switcher.S doesn't care that it's been moved; on | 526 | /* |
527 | * Most of the i386/switcher.S doesn't care that it's been moved; on | ||
449 | * Intel, jumps are relative, and it doesn't access any references to | 528 | * Intel, jumps are relative, and it doesn't access any references to |
450 | * external code or data. | 529 | * external code or data. |
451 | * | 530 | * |
@@ -453,7 +532,8 @@ void __init lguest_arch_host_init(void) | |||
453 | * addresses are placed in a table (default_idt_entries), so we need to | 532 | * addresses are placed in a table (default_idt_entries), so we need to |
454 | * update the table with the new addresses. switcher_offset() is a | 533 | * update the table with the new addresses. switcher_offset() is a |
455 | * convenience function which returns the distance between the | 534 | * convenience function which returns the distance between the |
456 | * compiled-in switcher code and the high-mapped copy we just made. */ | 535 | * compiled-in switcher code and the high-mapped copy we just made. |
536 | */ | ||
457 | for (i = 0; i < IDT_ENTRIES; i++) | 537 | for (i = 0; i < IDT_ENTRIES; i++) |
458 | default_idt_entries[i] += switcher_offset(); | 538 | default_idt_entries[i] += switcher_offset(); |
459 | 539 | ||
@@ -468,63 +548,81 @@ void __init lguest_arch_host_init(void) | |||
468 | for_each_possible_cpu(i) { | 548 | for_each_possible_cpu(i) { |
469 | /* lguest_pages() returns this CPU's two pages. */ | 549 | /* lguest_pages() returns this CPU's two pages. */ |
470 | struct lguest_pages *pages = lguest_pages(i); | 550 | struct lguest_pages *pages = lguest_pages(i); |
471 | /* This is a convenience pointer to make the code fit one | 551 | /* This is a convenience pointer to make the code neater. */ |
472 | * statement to a line. */ | ||
473 | struct lguest_ro_state *state = &pages->state; | 552 | struct lguest_ro_state *state = &pages->state; |
474 | 553 | ||
475 | /* The Global Descriptor Table: the Host has a different one | 554 | /* |
555 | * The Global Descriptor Table: the Host has a different one | ||
476 | * for each CPU. We keep a descriptor for the GDT which says | 556 | * for each CPU. We keep a descriptor for the GDT which says |
477 | * where it is and how big it is (the size is actually the last | 557 | * where it is and how big it is (the size is actually the last |
478 | * byte, not the size, hence the "-1"). */ | 558 | * byte, not the size, hence the "-1"). |
559 | */ | ||
479 | state->host_gdt_desc.size = GDT_SIZE-1; | 560 | state->host_gdt_desc.size = GDT_SIZE-1; |
480 | state->host_gdt_desc.address = (long)get_cpu_gdt_table(i); | 561 | state->host_gdt_desc.address = (long)get_cpu_gdt_table(i); |
481 | 562 | ||
482 | /* All CPUs on the Host use the same Interrupt Descriptor | 563 | /* |
564 | * All CPUs on the Host use the same Interrupt Descriptor | ||
483 | * Table, so we just use store_idt(), which gets this CPU's IDT | 565 | * Table, so we just use store_idt(), which gets this CPU's IDT |
484 | * descriptor. */ | 566 | * descriptor. |
567 | */ | ||
485 | store_idt(&state->host_idt_desc); | 568 | store_idt(&state->host_idt_desc); |
486 | 569 | ||
487 | /* The descriptors for the Guest's GDT and IDT can be filled | 570 | /* |
571 | * The descriptors for the Guest's GDT and IDT can be filled | ||
488 | * out now, too. We copy the GDT & IDT into ->guest_gdt and | 572 | * out now, too. We copy the GDT & IDT into ->guest_gdt and |
489 | * ->guest_idt before actually running the Guest. */ | 573 | * ->guest_idt before actually running the Guest. |
574 | */ | ||
490 | state->guest_idt_desc.size = sizeof(state->guest_idt)-1; | 575 | state->guest_idt_desc.size = sizeof(state->guest_idt)-1; |
491 | state->guest_idt_desc.address = (long)&state->guest_idt; | 576 | state->guest_idt_desc.address = (long)&state->guest_idt; |
492 | state->guest_gdt_desc.size = sizeof(state->guest_gdt)-1; | 577 | state->guest_gdt_desc.size = sizeof(state->guest_gdt)-1; |
493 | state->guest_gdt_desc.address = (long)&state->guest_gdt; | 578 | state->guest_gdt_desc.address = (long)&state->guest_gdt; |
494 | 579 | ||
495 | /* We know where we want the stack to be when the Guest enters | 580 | /* |
581 | * We know where we want the stack to be when the Guest enters | ||
496 | * the Switcher: in pages->regs. The stack grows upwards, so | 582 | * the Switcher: in pages->regs. The stack grows upwards, so |
497 | * we start it at the end of that structure. */ | 583 | * we start it at the end of that structure. |
584 | */ | ||
498 | state->guest_tss.sp0 = (long)(&pages->regs + 1); | 585 | state->guest_tss.sp0 = (long)(&pages->regs + 1); |
499 | /* And this is the GDT entry to use for the stack: we keep a | 586 | /* |
500 | * couple of special LGUEST entries. */ | 587 | * And this is the GDT entry to use for the stack: we keep a |
588 | * couple of special LGUEST entries. | ||
589 | */ | ||
501 | state->guest_tss.ss0 = LGUEST_DS; | 590 | state->guest_tss.ss0 = LGUEST_DS; |
502 | 591 | ||
503 | /* x86 can have a finegrained bitmap which indicates what I/O | 592 | /* |
593 | * x86 can have a finegrained bitmap which indicates what I/O | ||
504 | * ports the process can use. We set it to the end of our | 594 | * ports the process can use. We set it to the end of our |
505 | * structure, meaning "none". */ | 595 | * structure, meaning "none". |
596 | */ | ||
506 | state->guest_tss.io_bitmap_base = sizeof(state->guest_tss); | 597 | state->guest_tss.io_bitmap_base = sizeof(state->guest_tss); |
507 | 598 | ||
508 | /* Some GDT entries are the same across all Guests, so we can | 599 | /* |
509 | * set them up now. */ | 600 | * Some GDT entries are the same across all Guests, so we can |
601 | * set them up now. | ||
602 | */ | ||
510 | setup_default_gdt_entries(state); | 603 | setup_default_gdt_entries(state); |
511 | /* Most IDT entries are the same for all Guests, too.*/ | 604 | /* Most IDT entries are the same for all Guests, too.*/ |
512 | setup_default_idt_entries(state, default_idt_entries); | 605 | setup_default_idt_entries(state, default_idt_entries); |
513 | 606 | ||
514 | /* The Host needs to be able to use the LGUEST segments on this | 607 | /* |
515 | * CPU, too, so put them in the Host GDT. */ | 608 | * The Host needs to be able to use the LGUEST segments on this |
609 | * CPU, too, so put them in the Host GDT. | ||
610 | */ | ||
516 | get_cpu_gdt_table(i)[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT; | 611 | get_cpu_gdt_table(i)[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT; |
517 | get_cpu_gdt_table(i)[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT; | 612 | get_cpu_gdt_table(i)[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT; |
518 | } | 613 | } |
519 | 614 | ||
520 | /* In the Switcher, we want the %cs segment register to use the | 615 | /* |
616 | * In the Switcher, we want the %cs segment register to use the | ||
521 | * LGUEST_CS GDT entry: we've put that in the Host and Guest GDTs, so | 617 | * LGUEST_CS GDT entry: we've put that in the Host and Guest GDTs, so |
522 | * it will be undisturbed when we switch. To change %cs and jump we | 618 | * it will be undisturbed when we switch. To change %cs and jump we |
523 | * need this structure to feed to Intel's "lcall" instruction. */ | 619 | * need this structure to feed to Intel's "lcall" instruction. |
620 | */ | ||
524 | lguest_entry.offset = (long)switch_to_guest + switcher_offset(); | 621 | lguest_entry.offset = (long)switch_to_guest + switcher_offset(); |
525 | lguest_entry.segment = LGUEST_CS; | 622 | lguest_entry.segment = LGUEST_CS; |
526 | 623 | ||
527 | /* Finally, we need to turn off "Page Global Enable". PGE is an | 624 | /* |
625 | * Finally, we need to turn off "Page Global Enable". PGE is an | ||
528 | * optimization where page table entries are specially marked to show | 626 | * optimization where page table entries are specially marked to show |
529 | * they never change. The Host kernel marks all the kernel pages this | 627 | * they never change. The Host kernel marks all the kernel pages this |
530 | * way because it's always present, even when userspace is running. | 628 | * way because it's always present, even when userspace is running. |
@@ -534,16 +632,21 @@ void __init lguest_arch_host_init(void) | |||
534 | * you'll get really weird bugs that you'll chase for two days. | 632 | * you'll get really weird bugs that you'll chase for two days. |
535 | * | 633 | * |
536 | * I used to turn PGE off every time we switched to the Guest and back | 634 | * I used to turn PGE off every time we switched to the Guest and back |
537 | * on when we return, but that slowed the Switcher down noticibly. */ | 635 | * on when we return, but that slowed the Switcher down noticibly. |
636 | */ | ||
538 | 637 | ||
539 | /* We don't need the complexity of CPUs coming and going while we're | 638 | /* |
540 | * doing this. */ | 639 | * We don't need the complexity of CPUs coming and going while we're |
640 | * doing this. | ||
641 | */ | ||
541 | get_online_cpus(); | 642 | get_online_cpus(); |
542 | if (cpu_has_pge) { /* We have a broader idea of "global". */ | 643 | if (cpu_has_pge) { /* We have a broader idea of "global". */ |
543 | /* Remember that this was originally set (for cleanup). */ | 644 | /* Remember that this was originally set (for cleanup). */ |
544 | cpu_had_pge = 1; | 645 | cpu_had_pge = 1; |
545 | /* adjust_pge is a helper function which sets or unsets the PGE | 646 | /* |
546 | * bit on its CPU, depending on the argument (0 == unset). */ | 647 | * adjust_pge is a helper function which sets or unsets the PGE |
648 | * bit on its CPU, depending on the argument (0 == unset). | ||
649 | */ | ||
547 | on_each_cpu(adjust_pge, (void *)0, 1); | 650 | on_each_cpu(adjust_pge, (void *)0, 1); |
548 | /* Turn off the feature in the global feature set. */ | 651 | /* Turn off the feature in the global feature set. */ |
549 | clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE); | 652 | clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE); |
@@ -590,26 +693,32 @@ int lguest_arch_init_hypercalls(struct lg_cpu *cpu) | |||
590 | { | 693 | { |
591 | u32 tsc_speed; | 694 | u32 tsc_speed; |
592 | 695 | ||
593 | /* The pointer to the Guest's "struct lguest_data" is the only argument. | 696 | /* |
594 | * We check that address now. */ | 697 | * The pointer to the Guest's "struct lguest_data" is the only argument. |
698 | * We check that address now. | ||
699 | */ | ||
595 | if (!lguest_address_ok(cpu->lg, cpu->hcall->arg1, | 700 | if (!lguest_address_ok(cpu->lg, cpu->hcall->arg1, |
596 | sizeof(*cpu->lg->lguest_data))) | 701 | sizeof(*cpu->lg->lguest_data))) |
597 | return -EFAULT; | 702 | return -EFAULT; |
598 | 703 | ||
599 | /* Having checked it, we simply set lg->lguest_data to point straight | 704 | /* |
705 | * Having checked it, we simply set lg->lguest_data to point straight | ||
600 | * into the Launcher's memory at the right place and then use | 706 | * into the Launcher's memory at the right place and then use |
601 | * copy_to_user/from_user from now on, instead of lgread/write. I put | 707 | * copy_to_user/from_user from now on, instead of lgread/write. I put |
602 | * this in to show that I'm not immune to writing stupid | 708 | * this in to show that I'm not immune to writing stupid |
603 | * optimizations. */ | 709 | * optimizations. |
710 | */ | ||
604 | cpu->lg->lguest_data = cpu->lg->mem_base + cpu->hcall->arg1; | 711 | cpu->lg->lguest_data = cpu->lg->mem_base + cpu->hcall->arg1; |
605 | 712 | ||
606 | /* We insist that the Time Stamp Counter exist and doesn't change with | 713 | /* |
714 | * We insist that the Time Stamp Counter exist and doesn't change with | ||
607 | * cpu frequency. Some devious chip manufacturers decided that TSC | 715 | * cpu frequency. Some devious chip manufacturers decided that TSC |
608 | * changes could be handled in software. I decided that time going | 716 | * changes could be handled in software. I decided that time going |
609 | * backwards might be good for benchmarks, but it's bad for users. | 717 | * backwards might be good for benchmarks, but it's bad for users. |
610 | * | 718 | * |
611 | * We also insist that the TSC be stable: the kernel detects unreliable | 719 | * We also insist that the TSC be stable: the kernel detects unreliable |
612 | * TSCs for its own purposes, and we use that here. */ | 720 | * TSCs for its own purposes, and we use that here. |
721 | */ | ||
613 | if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable()) | 722 | if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable()) |
614 | tsc_speed = tsc_khz; | 723 | tsc_speed = tsc_khz; |
615 | else | 724 | else |
@@ -625,38 +734,47 @@ int lguest_arch_init_hypercalls(struct lg_cpu *cpu) | |||
625 | } | 734 | } |
626 | /*:*/ | 735 | /*:*/ |
627 | 736 | ||
628 | /*L:030 lguest_arch_setup_regs() | 737 | /*L:030 |
738 | * lguest_arch_setup_regs() | ||
629 | * | 739 | * |
630 | * Most of the Guest's registers are left alone: we used get_zeroed_page() to | 740 | * Most of the Guest's registers are left alone: we used get_zeroed_page() to |
631 | * allocate the structure, so they will be 0. */ | 741 | * allocate the structure, so they will be 0. |
742 | */ | ||
632 | void lguest_arch_setup_regs(struct lg_cpu *cpu, unsigned long start) | 743 | void lguest_arch_setup_regs(struct lg_cpu *cpu, unsigned long start) |
633 | { | 744 | { |
634 | struct lguest_regs *regs = cpu->regs; | 745 | struct lguest_regs *regs = cpu->regs; |
635 | 746 | ||
636 | /* There are four "segment" registers which the Guest needs to boot: | 747 | /* |
748 | * There are four "segment" registers which the Guest needs to boot: | ||
637 | * The "code segment" register (cs) refers to the kernel code segment | 749 | * The "code segment" register (cs) refers to the kernel code segment |
638 | * __KERNEL_CS, and the "data", "extra" and "stack" segment registers | 750 | * __KERNEL_CS, and the "data", "extra" and "stack" segment registers |
639 | * refer to the kernel data segment __KERNEL_DS. | 751 | * refer to the kernel data segment __KERNEL_DS. |
640 | * | 752 | * |
641 | * The privilege level is packed into the lower bits. The Guest runs | 753 | * The privilege level is packed into the lower bits. The Guest runs |
642 | * at privilege level 1 (GUEST_PL).*/ | 754 | * at privilege level 1 (GUEST_PL). |
755 | */ | ||
643 | regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; | 756 | regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; |
644 | regs->cs = __KERNEL_CS|GUEST_PL; | 757 | regs->cs = __KERNEL_CS|GUEST_PL; |
645 | 758 | ||
646 | /* The "eflags" register contains miscellaneous flags. Bit 1 (0x002) | 759 | /* |
760 | * The "eflags" register contains miscellaneous flags. Bit 1 (0x002) | ||
647 | * is supposed to always be "1". Bit 9 (0x200) controls whether | 761 | * is supposed to always be "1". Bit 9 (0x200) controls whether |
648 | * interrupts are enabled. We always leave interrupts enabled while | 762 | * interrupts are enabled. We always leave interrupts enabled while |
649 | * running the Guest. */ | 763 | * running the Guest. |
764 | */ | ||
650 | regs->eflags = X86_EFLAGS_IF | 0x2; | 765 | regs->eflags = X86_EFLAGS_IF | 0x2; |
651 | 766 | ||
652 | /* The "Extended Instruction Pointer" register says where the Guest is | 767 | /* |
653 | * running. */ | 768 | * The "Extended Instruction Pointer" register says where the Guest is |
769 | * running. | ||
770 | */ | ||
654 | regs->eip = start; | 771 | regs->eip = start; |
655 | 772 | ||
656 | /* %esi points to our boot information, at physical address 0, so don't | 773 | /* |
657 | * touch it. */ | 774 | * %esi points to our boot information, at physical address 0, so don't |
775 | * touch it. | ||
776 | */ | ||
658 | 777 | ||
659 | /* There are a couple of GDT entries the Guest expects when first | 778 | /* There are a couple of GDT entries the Guest expects at boot. */ |
660 | * booting. */ | ||
661 | setup_guest_gdt(cpu); | 779 | setup_guest_gdt(cpu); |
662 | } | 780 | } |
diff --git a/drivers/lguest/x86/switcher_32.S b/drivers/lguest/x86/switcher_32.S index 3fc15318a80f..6dec09793836 100644 --- a/drivers/lguest/x86/switcher_32.S +++ b/drivers/lguest/x86/switcher_32.S | |||
@@ -1,12 +1,15 @@ | |||
1 | /*P:900 This is the Switcher: code which sits at 0xFFC00000 astride both the | 1 | /*P:900 |
2 | * This is the Switcher: code which sits at 0xFFC00000 astride both the | ||
2 | * Host and Guest to do the low-level Guest<->Host switch. It is as simple as | 3 | * Host and Guest to do the low-level Guest<->Host switch. It is as simple as |
3 | * it can be made, but it's naturally very specific to x86. | 4 | * it can be made, but it's naturally very specific to x86. |
4 | * | 5 | * |
5 | * You have now completed Preparation. If this has whet your appetite; if you | 6 | * You have now completed Preparation. If this has whet your appetite; if you |
6 | * are feeling invigorated and refreshed then the next, more challenging stage | 7 | * are feeling invigorated and refreshed then the next, more challenging stage |
7 | * can be found in "make Guest". :*/ | 8 | * can be found in "make Guest". |
9 | :*/ | ||
8 | 10 | ||
9 | /*M:012 Lguest is meant to be simple: my rule of thumb is that 1% more LOC must | 11 | /*M:012 |
12 | * Lguest is meant to be simple: my rule of thumb is that 1% more LOC must | ||
10 | * gain at least 1% more performance. Since neither LOC nor performance can be | 13 | * gain at least 1% more performance. Since neither LOC nor performance can be |
11 | * measured beforehand, it generally means implementing a feature then deciding | 14 | * measured beforehand, it generally means implementing a feature then deciding |
12 | * if it's worth it. And once it's implemented, who can say no? | 15 | * if it's worth it. And once it's implemented, who can say no? |
@@ -31,11 +34,14 @@ | |||
31 | * Host (which is actually really easy). | 34 | * Host (which is actually really easy). |
32 | * | 35 | * |
33 | * Two questions remain. Would the performance gain outweigh the complexity? | 36 | * Two questions remain. Would the performance gain outweigh the complexity? |
34 | * And who would write the verse documenting it? :*/ | 37 | * And who would write the verse documenting it? |
38 | :*/ | ||
35 | 39 | ||
36 | /*M:011 Lguest64 handles NMI. This gave me NMI envy (until I looked at their | 40 | /*M:011 |
41 | * Lguest64 handles NMI. This gave me NMI envy (until I looked at their | ||
37 | * code). It's worth doing though, since it would let us use oprofile in the | 42 | * code). It's worth doing though, since it would let us use oprofile in the |
38 | * Host when a Guest is running. :*/ | 43 | * Host when a Guest is running. |
44 | :*/ | ||
39 | 45 | ||
40 | /*S:100 | 46 | /*S:100 |
41 | * Welcome to the Switcher itself! | 47 | * Welcome to the Switcher itself! |
diff --git a/include/linux/lguest.h b/include/linux/lguest.h index dbf2479e808e..0a3a11afd64b 100644 --- a/include/linux/lguest.h +++ b/include/linux/lguest.h | |||
@@ -1,5 +1,7 @@ | |||
1 | /* Things the lguest guest needs to know. Note: like all lguest interfaces, | 1 | /* |
2 | * this is subject to wild and random change between versions. */ | 2 | * Things the lguest guest needs to know. Note: like all lguest interfaces, |
3 | * this is subject to wild and random change between versions. | ||
4 | */ | ||
3 | #ifndef _LINUX_LGUEST_H | 5 | #ifndef _LINUX_LGUEST_H |
4 | #define _LINUX_LGUEST_H | 6 | #define _LINUX_LGUEST_H |
5 | 7 | ||
@@ -11,32 +13,42 @@ | |||
11 | #define LG_CLOCK_MIN_DELTA 100UL | 13 | #define LG_CLOCK_MIN_DELTA 100UL |
12 | #define LG_CLOCK_MAX_DELTA ULONG_MAX | 14 | #define LG_CLOCK_MAX_DELTA ULONG_MAX |
13 | 15 | ||
14 | /*G:031 The second method of communicating with the Host is to via "struct | 16 | /*G:031 |
17 | * The second method of communicating with the Host is to via "struct | ||
15 | * lguest_data". Once the Guest's initialization hypercall tells the Host where | 18 | * lguest_data". Once the Guest's initialization hypercall tells the Host where |
16 | * this is, the Guest and Host both publish information in it. :*/ | 19 | * this is, the Guest and Host both publish information in it. |
20 | :*/ | ||
17 | struct lguest_data | 21 | struct lguest_data |
18 | { | 22 | { |
19 | /* 512 == enabled (same as eflags in normal hardware). The Guest | 23 | /* |
20 | * changes interrupts so often that a hypercall is too slow. */ | 24 | * 512 == enabled (same as eflags in normal hardware). The Guest |
25 | * changes interrupts so often that a hypercall is too slow. | ||
26 | */ | ||
21 | unsigned int irq_enabled; | 27 | unsigned int irq_enabled; |
22 | /* Fine-grained interrupt disabling by the Guest */ | 28 | /* Fine-grained interrupt disabling by the Guest */ |
23 | DECLARE_BITMAP(blocked_interrupts, LGUEST_IRQS); | 29 | DECLARE_BITMAP(blocked_interrupts, LGUEST_IRQS); |
24 | 30 | ||
25 | /* The Host writes the virtual address of the last page fault here, | 31 | /* |
32 | * The Host writes the virtual address of the last page fault here, | ||
26 | * which saves the Guest a hypercall. CR2 is the native register where | 33 | * which saves the Guest a hypercall. CR2 is the native register where |
27 | * this address would normally be found. */ | 34 | * this address would normally be found. |
35 | */ | ||
28 | unsigned long cr2; | 36 | unsigned long cr2; |
29 | 37 | ||
30 | /* Wallclock time set by the Host. */ | 38 | /* Wallclock time set by the Host. */ |
31 | struct timespec time; | 39 | struct timespec time; |
32 | 40 | ||
33 | /* Interrupt pending set by the Host. The Guest should do a hypercall | 41 | /* |
34 | * if it re-enables interrupts and sees this set (to X86_EFLAGS_IF). */ | 42 | * Interrupt pending set by the Host. The Guest should do a hypercall |
43 | * if it re-enables interrupts and sees this set (to X86_EFLAGS_IF). | ||
44 | */ | ||
35 | int irq_pending; | 45 | int irq_pending; |
36 | 46 | ||
37 | /* Async hypercall ring. Instead of directly making hypercalls, we can | 47 | /* |
48 | * Async hypercall ring. Instead of directly making hypercalls, we can | ||
38 | * place them in here for processing the next time the Host wants. | 49 | * place them in here for processing the next time the Host wants. |
39 | * This batching can be quite efficient. */ | 50 | * This batching can be quite efficient. |
51 | */ | ||
40 | 52 | ||
41 | /* 0xFF == done (set by Host), 0 == pending (set by Guest). */ | 53 | /* 0xFF == done (set by Host), 0 == pending (set by Guest). */ |
42 | u8 hcall_status[LHCALL_RING_SIZE]; | 54 | u8 hcall_status[LHCALL_RING_SIZE]; |
diff --git a/include/linux/lguest_launcher.h b/include/linux/lguest_launcher.h index bfefbdf7498a..495203ff221c 100644 --- a/include/linux/lguest_launcher.h +++ b/include/linux/lguest_launcher.h | |||
@@ -29,8 +29,10 @@ struct lguest_device_desc { | |||
29 | __u8 type; | 29 | __u8 type; |
30 | /* The number of virtqueues (first in config array) */ | 30 | /* The number of virtqueues (first in config array) */ |
31 | __u8 num_vq; | 31 | __u8 num_vq; |
32 | /* The number of bytes of feature bits. Multiply by 2: one for host | 32 | /* |
33 | * features and one for Guest acknowledgements. */ | 33 | * The number of bytes of feature bits. Multiply by 2: one for host |
34 | * features and one for Guest acknowledgements. | ||
35 | */ | ||
34 | __u8 feature_len; | 36 | __u8 feature_len; |
35 | /* The number of bytes of the config array after virtqueues. */ | 37 | /* The number of bytes of the config array after virtqueues. */ |
36 | __u8 config_len; | 38 | __u8 config_len; |
@@ -39,8 +41,10 @@ struct lguest_device_desc { | |||
39 | __u8 config[0]; | 41 | __u8 config[0]; |
40 | }; | 42 | }; |
41 | 43 | ||
42 | /*D:135 This is how we expect the device configuration field for a virtqueue | 44 | /*D:135 |
43 | * to be laid out in config space. */ | 45 | * This is how we expect the device configuration field for a virtqueue |
46 | * to be laid out in config space. | ||
47 | */ | ||
44 | struct lguest_vqconfig { | 48 | struct lguest_vqconfig { |
45 | /* The number of entries in the virtio_ring */ | 49 | /* The number of entries in the virtio_ring */ |
46 | __u16 num; | 50 | __u16 num; |
@@ -61,7 +65,9 @@ enum lguest_req | |||
61 | LHREQ_EVENTFD, /* + address, fd. */ | 65 | LHREQ_EVENTFD, /* + address, fd. */ |
62 | }; | 66 | }; |
63 | 67 | ||
64 | /* The alignment to use between consumer and producer parts of vring. | 68 | /* |
65 | * x86 pagesize for historical reasons. */ | 69 | * The alignment to use between consumer and producer parts of vring. |
70 | * x86 pagesize for historical reasons. | ||
71 | */ | ||
66 | #define LGUEST_VRING_ALIGN 4096 | 72 | #define LGUEST_VRING_ALIGN 4096 |
67 | #endif /* _LINUX_LGUEST_LAUNCHER */ | 73 | #endif /* _LINUX_LGUEST_LAUNCHER */ |