diff options
Diffstat (limited to 'drivers/lguest')
| -rw-r--r-- | drivers/lguest/core.c | 119 | ||||
| -rw-r--r-- | drivers/lguest/hypercalls.c | 145 | ||||
| -rw-r--r-- | drivers/lguest/interrupts_and_traps.c | 288 | ||||
| -rw-r--r-- | drivers/lguest/lg.h | 32 | ||||
| -rw-r--r-- | drivers/lguest/lguest_device.c | 160 | ||||
| -rw-r--r-- | drivers/lguest/lguest_user.c | 232 | ||||
| -rw-r--r-- | drivers/lguest/page_tables.c | 489 | ||||
| -rw-r--r-- | drivers/lguest/segments.c | 106 | ||||
| -rw-r--r-- | drivers/lguest/x86/core.c | 374 | ||||
| -rw-r--r-- | drivers/lguest/x86/switcher_32.S | 22 |
10 files changed, 1330 insertions, 637 deletions
diff --git a/drivers/lguest/core.c b/drivers/lguest/core.c index a6974e9b8ebf..1e2cb846b3c9 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,10 +215,17 @@ 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. | ||
| 221 | */ | ||
| 200 | if (cpu->pending_notify) { | 222 | if (cpu->pending_notify) { |
| 223 | /* | ||
| 224 | * Does it just needs to write to a registered | ||
| 225 | * eventfd (ie. the appropriate virtqueue thread)? | ||
| 226 | */ | ||
| 201 | if (!send_notify_to_eventfd(cpu)) { | 227 | if (!send_notify_to_eventfd(cpu)) { |
| 228 | /* OK, we tell the main Laucher. */ | ||
| 202 | if (put_user(cpu->pending_notify, user)) | 229 | if (put_user(cpu->pending_notify, user)) |
| 203 | return -EFAULT; | 230 | return -EFAULT; |
| 204 | return sizeof(cpu->pending_notify); | 231 | return sizeof(cpu->pending_notify); |
| @@ -209,29 +236,39 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user) | |||
| 209 | if (signal_pending(current)) | 236 | if (signal_pending(current)) |
| 210 | return -ERESTARTSYS; | 237 | return -ERESTARTSYS; |
| 211 | 238 | ||
| 212 | /* Check if there are any interrupts which can be delivered now: | 239 | /* |
| 240 | * 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 | 241 | * if so, this sets up the hander to be executed when we next |
| 214 | * run the Guest. */ | 242 | * run the Guest. |
| 243 | */ | ||
| 215 | irq = interrupt_pending(cpu, &more); | 244 | irq = interrupt_pending(cpu, &more); |
| 216 | if (irq < LGUEST_IRQS) | 245 | if (irq < LGUEST_IRQS) |
| 217 | try_deliver_interrupt(cpu, irq, more); | 246 | try_deliver_interrupt(cpu, irq, more); |
| 218 | 247 | ||
| 219 | /* All long-lived kernel loops need to check with this horrible | 248 | /* |
| 249 | * All long-lived kernel loops need to check with this horrible | ||
| 220 | * thing called the freezer. If the Host is trying to suspend, | 250 | * thing called the freezer. If the Host is trying to suspend, |
| 221 | * it stops us. */ | 251 | * it stops us. |
| 252 | */ | ||
| 222 | try_to_freeze(); | 253 | try_to_freeze(); |
| 223 | 254 | ||
| 224 | /* Just make absolutely sure the Guest is still alive. One of | 255 | /* |
| 225 | * those hypercalls could have been fatal, for example. */ | 256 | * Just make absolutely sure the Guest is still alive. One of |
| 257 | * those hypercalls could have been fatal, for example. | ||
| 258 | */ | ||
| 226 | if (cpu->lg->dead) | 259 | if (cpu->lg->dead) |
| 227 | break; | 260 | break; |
| 228 | 261 | ||
| 229 | /* If the Guest asked to be stopped, we sleep. The Guest's | 262 | /* |
| 230 | * clock timer will wake us. */ | 263 | * If the Guest asked to be stopped, we sleep. The Guest's |
| 264 | * clock timer will wake us. | ||
| 265 | */ | ||
| 231 | if (cpu->halted) { | 266 | if (cpu->halted) { |
| 232 | set_current_state(TASK_INTERRUPTIBLE); | 267 | set_current_state(TASK_INTERRUPTIBLE); |
| 233 | /* Just before we sleep, make sure no interrupt snuck in | 268 | /* |
| 234 | * which we should be doing. */ | 269 | * Just before we sleep, make sure no interrupt snuck in |
| 270 | * which we should be doing. | ||
| 271 | */ | ||
| 235 | if (interrupt_pending(cpu, &more) < LGUEST_IRQS) | 272 | if (interrupt_pending(cpu, &more) < LGUEST_IRQS) |
| 236 | set_current_state(TASK_RUNNING); | 273 | set_current_state(TASK_RUNNING); |
| 237 | else | 274 | else |
| @@ -239,8 +276,10 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user) | |||
| 239 | continue; | 276 | continue; |
| 240 | } | 277 | } |
| 241 | 278 | ||
| 242 | /* OK, now we're ready to jump into the Guest. First we put up | 279 | /* |
| 243 | * the "Do Not Disturb" sign: */ | 280 | * OK, now we're ready to jump into the Guest. First we put up |
| 281 | * the "Do Not Disturb" sign: | ||
| 282 | */ | ||
| 244 | local_irq_disable(); | 283 | local_irq_disable(); |
| 245 | 284 | ||
| 246 | /* Actually run the Guest until something happens. */ | 285 | /* Actually run the Guest until something happens. */ |
| @@ -327,8 +366,10 @@ static void __exit fini(void) | |||
| 327 | } | 366 | } |
| 328 | /*:*/ | 367 | /*:*/ |
| 329 | 368 | ||
| 330 | /* The Host side of lguest can be a module. This is a nice way for people to | 369 | /* |
| 331 | * play with it. */ | 370 | * The Host side of lguest can be a module. This is a nice way for people to |
| 371 | * play with it. | ||
| 372 | */ | ||
| 332 | module_init(init); | 373 | module_init(init); |
| 333 | module_exit(fini); | 374 | module_exit(fini); |
| 334 | MODULE_LICENSE("GPL"); | 375 | MODULE_LICENSE("GPL"); |
diff --git a/drivers/lguest/hypercalls.c b/drivers/lguest/hypercalls.c index c29ffa19cb74..83511eb0923d 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 five |
| 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,44 @@ 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 | * | ||
| 249 | * Note that if we used a shared anonymous mapping in the Launcher instead of | ||
| 250 | * mapping /dev/zero private, we wouldn't worry about cop-on-write. And we | ||
| 251 | * need that to switch the Launcher to processes (away from threads) anyway. | ||
| 252 | :*/ | ||
| 214 | 253 | ||
| 215 | /*H:100 | 254 | /*H:100 |
| 216 | * Hypercalls | 255 | * Hypercalls |
| @@ -229,17 +268,22 @@ void do_hypercalls(struct lg_cpu *cpu) | |||
| 229 | return; | 268 | return; |
| 230 | } | 269 | } |
| 231 | 270 | ||
| 232 | /* The Guest has initialized. | 271 | /* |
| 272 | * The Guest has initialized. | ||
| 233 | * | 273 | * |
| 234 | * Look in the hypercall ring for the async hypercalls: */ | 274 | * Look in the hypercall ring for the async hypercalls: |
| 275 | */ | ||
| 235 | do_async_hcalls(cpu); | 276 | do_async_hcalls(cpu); |
| 236 | 277 | ||
| 237 | /* If we stopped reading the hypercall ring because the Guest did a | 278 | /* |
| 279 | * 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 | 280 | * NOTIFY to the Launcher, we want to return now. Otherwise we do |
| 239 | * the hypercall. */ | 281 | * the hypercall. |
| 282 | */ | ||
| 240 | if (!cpu->pending_notify) { | 283 | if (!cpu->pending_notify) { |
| 241 | do_hcall(cpu, cpu->hcall); | 284 | do_hcall(cpu, cpu->hcall); |
| 242 | /* Tricky point: we reset the hcall pointer to mark the | 285 | /* |
| 286 | * Tricky point: we reset the hcall pointer to mark the | ||
| 243 | * hypercall as "done". We use the hcall pointer rather than | 287 | * hypercall as "done". We use the hcall pointer rather than |
| 244 | * the trap number to indicate a hypercall is pending. | 288 | * the trap number to indicate a hypercall is pending. |
| 245 | * Normally it doesn't matter: the Guest will run again and | 289 | * Normally it doesn't matter: the Guest will run again and |
| @@ -248,13 +292,16 @@ void do_hypercalls(struct lg_cpu *cpu) | |||
| 248 | * However, if we are signalled or the Guest sends I/O to the | 292 | * However, if we are signalled or the Guest sends I/O to the |
| 249 | * Launcher, the run_guest() loop will exit without running the | 293 | * Launcher, the run_guest() loop will exit without running the |
| 250 | * Guest. When it comes back it would try to re-run the | 294 | * Guest. When it comes back it would try to re-run the |
| 251 | * hypercall. Finding that bug sucked. */ | 295 | * hypercall. Finding that bug sucked. |
| 296 | */ | ||
| 252 | cpu->hcall = NULL; | 297 | cpu->hcall = NULL; |
| 253 | } | 298 | } |
| 254 | } | 299 | } |
| 255 | 300 | ||
| 256 | /* This routine supplies the Guest with time: it's used for wallclock time at | 301 | /* |
| 257 | * initial boot and as a rough time source if the TSC isn't available. */ | 302 | * This routine supplies the Guest with time: it's used for wallclock time at |
| 303 | * initial boot and as a rough time source if the TSC isn't available. | ||
| 304 | */ | ||
| 258 | void write_timestamp(struct lg_cpu *cpu) | 305 | void write_timestamp(struct lg_cpu *cpu) |
| 259 | { | 306 | { |
| 260 | struct timespec now; | 307 | 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..bc28745d05af 100644 --- a/drivers/lguest/lg.h +++ b/drivers/lguest/lg.h | |||
| @@ -16,15 +16,13 @@ | |||
| 16 | void free_pagetables(void); | 16 | void free_pagetables(void); |
| 17 | int init_pagetables(struct page **switcher_page, unsigned int pages); | 17 | int init_pagetables(struct page **switcher_page, unsigned int pages); |
| 18 | 18 | ||
| 19 | struct pgdir | 19 | struct pgdir { |
| 20 | { | ||
| 21 | unsigned long gpgdir; | 20 | unsigned long gpgdir; |
| 22 | pgd_t *pgdir; | 21 | pgd_t *pgdir; |
| 23 | }; | 22 | }; |
| 24 | 23 | ||
| 25 | /* We have two pages shared with guests, per cpu. */ | 24 | /* We have two pages shared with guests, per cpu. */ |
| 26 | struct lguest_pages | 25 | struct lguest_pages { |
| 27 | { | ||
| 28 | /* This is the stack page mapped rw in guest */ | 26 | /* This is the stack page mapped rw in guest */ |
| 29 | char spare[PAGE_SIZE - sizeof(struct lguest_regs)]; | 27 | char spare[PAGE_SIZE - sizeof(struct lguest_regs)]; |
| 30 | struct lguest_regs regs; | 28 | struct lguest_regs regs; |
| @@ -54,13 +52,13 @@ struct lg_cpu { | |||
| 54 | 52 | ||
| 55 | unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */ | 53 | unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */ |
| 56 | 54 | ||
| 57 | /* At end of a page shared mapped over lguest_pages in guest. */ | 55 | /* At end of a page shared mapped over lguest_pages in guest. */ |
| 58 | unsigned long regs_page; | 56 | unsigned long regs_page; |
| 59 | struct lguest_regs *regs; | 57 | struct lguest_regs *regs; |
| 60 | 58 | ||
| 61 | struct lguest_pages *last_pages; | 59 | struct lguest_pages *last_pages; |
| 62 | 60 | ||
| 63 | int cpu_pgd; /* which pgd this cpu is currently using */ | 61 | int cpu_pgd; /* Which pgd this cpu is currently using */ |
| 64 | 62 | ||
| 65 | /* If a hypercall was asked for, this points to the arguments. */ | 63 | /* If a hypercall was asked for, this points to the arguments. */ |
| 66 | struct hcall_args *hcall; | 64 | struct hcall_args *hcall; |
| @@ -89,15 +87,17 @@ struct lg_eventfd_map { | |||
| 89 | }; | 87 | }; |
| 90 | 88 | ||
| 91 | /* The private info the thread maintains about the guest. */ | 89 | /* The private info the thread maintains about the guest. */ |
| 92 | struct lguest | 90 | struct lguest { |
| 93 | { | ||
| 94 | struct lguest_data __user *lguest_data; | 91 | struct lguest_data __user *lguest_data; |
| 95 | struct lg_cpu cpus[NR_CPUS]; | 92 | struct lg_cpu cpus[NR_CPUS]; |
| 96 | unsigned int nr_cpus; | 93 | unsigned int nr_cpus; |
| 97 | 94 | ||
| 98 | u32 pfn_limit; | 95 | u32 pfn_limit; |
| 99 | /* This provides the offset to the base of guest-physical | 96 | |
| 100 | * memory in the Launcher. */ | 97 | /* |
| 98 | * This provides the offset to the base of guest-physical memory in the | ||
| 99 | * Launcher. | ||
| 100 | */ | ||
| 101 | void __user *mem_base; | 101 | void __user *mem_base; |
| 102 | unsigned long kernel_address; | 102 | unsigned long kernel_address; |
| 103 | 103 | ||
| @@ -122,11 +122,13 @@ bool lguest_address_ok(const struct lguest *lg, | |||
| 122 | void __lgread(struct lg_cpu *, void *, unsigned long, unsigned); | 122 | void __lgread(struct lg_cpu *, void *, unsigned long, unsigned); |
| 123 | void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); | 123 | void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); |
| 124 | 124 | ||
| 125 | /*H:035 Using memory-copy operations like that is usually inconvient, so we | 125 | /*H:035 |
| 126 | * 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 | 127 | * have the following helper macros which read and write a specific type (often |
| 127 | * an unsigned long). | 128 | * an unsigned long). |
| 128 | * | 129 | * |
| 129 | * This reads into a variable of the given type then returns that. */ | 130 | * This reads into a variable of the given type then returns that. |
| 131 | */ | ||
| 130 | #define lgread(cpu, addr, type) \ | 132 | #define lgread(cpu, addr, type) \ |
| 131 | ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; }) | 133 | ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; }) |
| 132 | 134 | ||
| @@ -140,9 +142,11 @@ void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); | |||
| 140 | 142 | ||
| 141 | int run_guest(struct lg_cpu *cpu, unsigned long __user *user); | 143 | int run_guest(struct lg_cpu *cpu, unsigned long __user *user); |
| 142 | 144 | ||
| 143 | /* Helper macros to obtain the first 12 or the last 20 bits, this is only the | 145 | /* |
| 146 | * 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 | 147 | * first step in the migration to the kernel types. pte_pfn is already defined |
| 145 | * in the kernel. */ | 148 | * in the kernel. |
| 149 | */ | ||
| 146 | #define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK) | 150 | #define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK) |
| 147 | #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT) | 151 | #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT) |
| 148 | #define pmd_flags(x) (pmd_val(x) & ~PAGE_MASK) | 152 | #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..b6200bc39b58 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; |
| @@ -191,8 +207,7 @@ static void lg_reset(struct virtio_device *vdev) | |||
| 191 | */ | 207 | */ |
| 192 | 208 | ||
| 193 | /*D:140 This is the information we remember about each virtqueue. */ | 209 | /*D:140 This is the information we remember about each virtqueue. */ |
| 194 | struct lguest_vq_info | 210 | struct lguest_vq_info { |
| 195 | { | ||
| 196 | /* A copy of the information contained in the device config. */ | 211 | /* A copy of the information contained in the device config. */ |
| 197 | struct lguest_vqconfig config; | 212 | struct lguest_vqconfig config; |
| 198 | 213 | ||
| @@ -200,13 +215,17 @@ struct lguest_vq_info | |||
| 200 | void *pages; | 215 | void *pages; |
| 201 | }; | 216 | }; |
| 202 | 217 | ||
| 203 | /* When the virtio_ring code wants to prod the Host, it calls us here and we | 218 | /* |
| 219 | * 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 | 220 | * make a hypercall. We hand the physical address of the virtqueue so the Host |
| 205 | * knows which virtqueue we're talking about. */ | 221 | * knows which virtqueue we're talking about. |
| 222 | */ | ||
| 206 | static void lg_notify(struct virtqueue *vq) | 223 | static void lg_notify(struct virtqueue *vq) |
| 207 | { | 224 | { |
| 208 | /* We store our virtqueue information in the "priv" pointer of the | 225 | /* |
| 209 | * virtqueue structure. */ | 226 | * We store our virtqueue information in the "priv" pointer of the |
| 227 | * virtqueue structure. | ||
| 228 | */ | ||
| 210 | struct lguest_vq_info *lvq = vq->priv; | 229 | struct lguest_vq_info *lvq = vq->priv; |
| 211 | 230 | ||
| 212 | kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT); | 231 | kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT); |
| @@ -215,7 +234,8 @@ static void lg_notify(struct virtqueue *vq) | |||
| 215 | /* An extern declaration inside a C file is bad form. Don't do it. */ | 234 | /* An extern declaration inside a C file is bad form. Don't do it. */ |
| 216 | extern void lguest_setup_irq(unsigned int irq); | 235 | extern void lguest_setup_irq(unsigned int irq); |
| 217 | 236 | ||
| 218 | /* This routine finds the first virtqueue described in the configuration of | 237 | /* |
| 238 | * This routine finds the Nth virtqueue described in the configuration of | ||
| 219 | * this device and sets it up. | 239 | * this device and sets it up. |
| 220 | * | 240 | * |
| 221 | * This is kind of an ugly duckling. It'd be nicer to have a standard | 241 | * This is kind of an ugly duckling. It'd be nicer to have a standard |
| @@ -223,9 +243,7 @@ extern void lguest_setup_irq(unsigned int irq); | |||
| 223 | * everyone wants to do it differently. The KVM coders want the Guest to | 243 | * everyone wants to do it differently. The KVM coders want the Guest to |
| 224 | * allocate its own pages and tell the Host where they are, but for lguest it's | 244 | * allocate its own pages and tell the Host where they are, but for lguest it's |
| 225 | * simpler for the Host to simply tell us where the pages are. | 245 | * simpler for the Host to simply tell us where the pages are. |
| 226 | * | 246 | */ |
| 227 | * So we provide drivers with a "find the Nth virtqueue and set it up" | ||
| 228 | * function. */ | ||
| 229 | static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | 247 | static struct virtqueue *lg_find_vq(struct virtio_device *vdev, |
| 230 | unsigned index, | 248 | unsigned index, |
| 231 | void (*callback)(struct virtqueue *vq), | 249 | void (*callback)(struct virtqueue *vq), |
| @@ -244,9 +262,11 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | |||
| 244 | if (!lvq) | 262 | if (!lvq) |
| 245 | return ERR_PTR(-ENOMEM); | 263 | return ERR_PTR(-ENOMEM); |
| 246 | 264 | ||
| 247 | /* Make a copy of the "struct lguest_vqconfig" entry, which sits after | 265 | /* |
| 266 | * 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 | 267 | * the descriptor. We need a copy because the config space might not |
| 249 | * be aligned correctly. */ | 268 | * be aligned correctly. |
| 269 | */ | ||
| 250 | memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config)); | 270 | memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config)); |
| 251 | 271 | ||
| 252 | printk("Mapping virtqueue %i addr %lx\n", index, | 272 | printk("Mapping virtqueue %i addr %lx\n", index, |
| @@ -261,8 +281,10 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | |||
| 261 | goto free_lvq; | 281 | goto free_lvq; |
| 262 | } | 282 | } |
| 263 | 283 | ||
| 264 | /* OK, tell virtio_ring.c to set up a virtqueue now we know its size | 284 | /* |
| 265 | * and we've got a pointer to its pages. */ | 285 | * OK, tell virtio_ring.c to set up a virtqueue now we know its size |
| 286 | * and we've got a pointer to its pages. | ||
| 287 | */ | ||
| 266 | vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN, | 288 | vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN, |
| 267 | vdev, lvq->pages, lg_notify, callback, name); | 289 | vdev, lvq->pages, lg_notify, callback, name); |
| 268 | if (!vq) { | 290 | if (!vq) { |
| @@ -273,18 +295,23 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev, | |||
| 273 | /* Make sure the interrupt is allocated. */ | 295 | /* Make sure the interrupt is allocated. */ |
| 274 | lguest_setup_irq(lvq->config.irq); | 296 | lguest_setup_irq(lvq->config.irq); |
| 275 | 297 | ||
| 276 | /* Tell the interrupt for this virtqueue to go to the virtio_ring | 298 | /* |
| 277 | * interrupt handler. */ | 299 | * 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 | 300 | * interrupt handler. |
| 301 | * | ||
| 302 | * 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 | 303 | * the interrupt as a source of randomness: it'd be nice to have that |
| 280 | * back.. */ | 304 | * back. |
| 305 | */ | ||
| 281 | err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED, | 306 | err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED, |
| 282 | dev_name(&vdev->dev), vq); | 307 | dev_name(&vdev->dev), vq); |
| 283 | if (err) | 308 | if (err) |
| 284 | goto destroy_vring; | 309 | goto destroy_vring; |
| 285 | 310 | ||
| 286 | /* Last of all we hook up our 'struct lguest_vq_info" to the | 311 | /* |
| 287 | * virtqueue's priv pointer. */ | 312 | * Last of all we hook up our 'struct lguest_vq_info" to the |
| 313 | * virtqueue's priv pointer. | ||
| 314 | */ | ||
| 288 | vq->priv = lvq; | 315 | vq->priv = lvq; |
| 289 | return vq; | 316 | return vq; |
| 290 | 317 | ||
| @@ -358,11 +385,14 @@ static struct virtio_config_ops lguest_config_ops = { | |||
| 358 | .del_vqs = lg_del_vqs, | 385 | .del_vqs = lg_del_vqs, |
| 359 | }; | 386 | }; |
| 360 | 387 | ||
| 361 | /* The root device for the lguest virtio devices. This makes them appear as | 388 | /* |
| 362 | * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */ | 389 | * The root device for the lguest virtio devices. This makes them appear as |
| 390 | * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. | ||
| 391 | */ | ||
| 363 | static struct device *lguest_root; | 392 | static struct device *lguest_root; |
| 364 | 393 | ||
| 365 | /*D:120 This is the core of the lguest bus: actually adding a new device. | 394 | /*D:120 |
| 395 | * 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 | 396 | * 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 | 397 | * earlier version of the code supported hotplug and unplug. They were removed |
| 368 | * early on because they were never used. | 398 | * early on because they were never used. |
| @@ -371,14 +401,14 @@ static struct device *lguest_root; | |||
| 371 | * | 401 | * |
| 372 | * It's worth reading this carefully: we start with a pointer to the new device | 402 | * 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 | 403 | * 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. */ | 404 | * descriptor page so we can uniquely identify it if things go badly wrong. |
| 405 | */ | ||
| 375 | static void add_lguest_device(struct lguest_device_desc *d, | 406 | static void add_lguest_device(struct lguest_device_desc *d, |
| 376 | unsigned int offset) | 407 | unsigned int offset) |
| 377 | { | 408 | { |
| 378 | struct lguest_device *ldev; | 409 | struct lguest_device *ldev; |
| 379 | 410 | ||
| 380 | /* Start with zeroed memory; Linux's device layer seems to count on | 411 | /* Start with zeroed memory; Linux's device layer counts on it. */ |
| 381 | * it. */ | ||
| 382 | ldev = kzalloc(sizeof(*ldev), GFP_KERNEL); | 412 | ldev = kzalloc(sizeof(*ldev), GFP_KERNEL); |
| 383 | if (!ldev) { | 413 | if (!ldev) { |
| 384 | printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n", | 414 | printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n", |
| @@ -388,17 +418,25 @@ static void add_lguest_device(struct lguest_device_desc *d, | |||
| 388 | 418 | ||
| 389 | /* This devices' parent is the lguest/ dir. */ | 419 | /* This devices' parent is the lguest/ dir. */ |
| 390 | ldev->vdev.dev.parent = lguest_root; | 420 | ldev->vdev.dev.parent = lguest_root; |
| 391 | /* We have a unique device index thanks to the dev_index counter. */ | 421 | /* |
| 422 | * The device type comes straight from the descriptor. There's also a | ||
| 423 | * device vendor field in the virtio_device struct, which we leave as | ||
| 424 | * 0. | ||
| 425 | */ | ||
| 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 9f9a2953b383..b4d3f7ca554f 100644 --- a/drivers/lguest/lguest_user.c +++ b/drivers/lguest/lguest_user.c | |||
| @@ -1,8 +1,9 @@ | |||
| 1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher | 1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher |
| 2 | * controls and communicates with the Guest. For example, the first write will | 2 | * 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 | 3 | * tell us the Guest's memory layout and entry point. A read will run the |
| 4 | * offset. A read will run the Guest until something happens, such as a signal | 4 | * Guest until something happens, such as a signal or the Guest doing a NOTIFY |
| 5 | * or the Guest doing a NOTIFY out to the Launcher. :*/ | 5 | * out to the Launcher. |
| 6 | :*/ | ||
| 6 | #include <linux/uaccess.h> | 7 | #include <linux/uaccess.h> |
| 7 | #include <linux/miscdevice.h> | 8 | #include <linux/miscdevice.h> |
| 8 | #include <linux/fs.h> | 9 | #include <linux/fs.h> |
| @@ -11,14 +12,41 @@ | |||
| 11 | #include <linux/file.h> | 12 | #include <linux/file.h> |
| 12 | #include "lg.h" | 13 | #include "lg.h" |
| 13 | 14 | ||
| 15 | /*L:056 | ||
| 16 | * Before we move on, let's jump ahead and look at what the kernel does when | ||
| 17 | * it needs to look up the eventfds. That will complete our picture of how we | ||
| 18 | * use RCU. | ||
| 19 | * | ||
| 20 | * The notification value is in cpu->pending_notify: we return true if it went | ||
| 21 | * to an eventfd. | ||
| 22 | */ | ||
| 14 | bool send_notify_to_eventfd(struct lg_cpu *cpu) | 23 | bool send_notify_to_eventfd(struct lg_cpu *cpu) |
| 15 | { | 24 | { |
| 16 | unsigned int i; | 25 | unsigned int i; |
| 17 | struct lg_eventfd_map *map; | 26 | struct lg_eventfd_map *map; |
| 18 | 27 | ||
| 19 | /* lg->eventfds is RCU-protected */ | 28 | /* |
| 29 | * This "rcu_read_lock()" helps track when someone is still looking at | ||
| 30 | * the (RCU-using) eventfds array. It's not actually a lock at all; | ||
| 31 | * indeed it's a noop in many configurations. (You didn't expect me to | ||
| 32 | * explain all the RCU secrets here, did you?) | ||
| 33 | */ | ||
| 20 | rcu_read_lock(); | 34 | rcu_read_lock(); |
| 35 | /* | ||
| 36 | * rcu_dereference is the counter-side of rcu_assign_pointer(); it | ||
| 37 | * makes sure we don't access the memory pointed to by | ||
| 38 | * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy, | ||
| 39 | * but Alpha allows this! Paul McKenney points out that a really | ||
| 40 | * aggressive compiler could have the same effect: | ||
| 41 | * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html | ||
| 42 | * | ||
| 43 | * So play safe, use rcu_dereference to get the rcu-protected pointer: | ||
| 44 | */ | ||
| 21 | map = rcu_dereference(cpu->lg->eventfds); | 45 | map = rcu_dereference(cpu->lg->eventfds); |
| 46 | /* | ||
| 47 | * Simple array search: even if they add an eventfd while we do this, | ||
| 48 | * we'll continue to use the old array and just won't see the new one. | ||
| 49 | */ | ||
| 22 | for (i = 0; i < map->num; i++) { | 50 | for (i = 0; i < map->num; i++) { |
| 23 | if (map->map[i].addr == cpu->pending_notify) { | 51 | if (map->map[i].addr == cpu->pending_notify) { |
| 24 | eventfd_signal(map->map[i].event, 1); | 52 | eventfd_signal(map->map[i].event, 1); |
| @@ -26,19 +54,50 @@ bool send_notify_to_eventfd(struct lg_cpu *cpu) | |||
| 26 | break; | 54 | break; |
| 27 | } | 55 | } |
| 28 | } | 56 | } |
| 57 | /* We're done with the rcu-protected variable cpu->lg->eventfds. */ | ||
| 29 | rcu_read_unlock(); | 58 | rcu_read_unlock(); |
| 59 | |||
| 60 | /* If we cleared the notification, it's because we found a match. */ | ||
| 30 | return cpu->pending_notify == 0; | 61 | return cpu->pending_notify == 0; |
| 31 | } | 62 | } |
| 32 | 63 | ||
| 64 | /*L:055 | ||
| 65 | * One of the more tricksy tricks in the Linux Kernel is a technique called | ||
| 66 | * Read Copy Update. Since one point of lguest is to teach lguest journeyers | ||
| 67 | * about kernel coding, I use it here. (In case you're curious, other purposes | ||
| 68 | * include learning about virtualization and instilling a deep appreciation for | ||
| 69 | * simplicity and puppies). | ||
| 70 | * | ||
| 71 | * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we | ||
| 72 | * add new eventfds without ever blocking readers from accessing the array. | ||
| 73 | * The current Launcher only does this during boot, so that never happens. But | ||
| 74 | * Read Copy Update is cool, and adding a lock risks damaging even more puppies | ||
| 75 | * than this code does. | ||
| 76 | * | ||
| 77 | * We allocate a brand new one-larger array, copy the old one and add our new | ||
| 78 | * element. Then we make the lg eventfd pointer point to the new array. | ||
| 79 | * That's the easy part: now we need to free the old one, but we need to make | ||
| 80 | * sure no slow CPU somewhere is still looking at it. That's what | ||
| 81 | * synchronize_rcu does for us: waits until every CPU has indicated that it has | ||
| 82 | * moved on to know it's no longer using the old one. | ||
| 83 | * | ||
| 84 | * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update. | ||
| 85 | */ | ||
| 33 | static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) | 86 | static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) |
| 34 | { | 87 | { |
| 35 | struct lg_eventfd_map *new, *old = lg->eventfds; | 88 | struct lg_eventfd_map *new, *old = lg->eventfds; |
| 36 | 89 | ||
| 90 | /* | ||
| 91 | * We don't allow notifications on value 0 anyway (pending_notify of | ||
| 92 | * 0 means "nothing pending"). | ||
| 93 | */ | ||
| 37 | if (!addr) | 94 | if (!addr) |
| 38 | return -EINVAL; | 95 | return -EINVAL; |
| 39 | 96 | ||
| 40 | /* Replace the old array with the new one, carefully: others can | 97 | /* |
| 41 | * be accessing it at the same time */ | 98 | * Replace the old array with the new one, carefully: others can |
| 99 | * be accessing it at the same time. | ||
| 100 | */ | ||
| 42 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), | 101 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), |
| 43 | GFP_KERNEL); | 102 | GFP_KERNEL); |
| 44 | if (!new) | 103 | if (!new) |
| @@ -52,22 +111,41 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) | |||
| 52 | new->map[new->num].addr = addr; | 111 | new->map[new->num].addr = addr; |
| 53 | new->map[new->num].event = eventfd_ctx_fdget(fd); | 112 | new->map[new->num].event = eventfd_ctx_fdget(fd); |
| 54 | if (IS_ERR(new->map[new->num].event)) { | 113 | if (IS_ERR(new->map[new->num].event)) { |
| 114 | int err = PTR_ERR(new->map[new->num].event); | ||
| 55 | kfree(new); | 115 | kfree(new); |
| 56 | return PTR_ERR(new->map[new->num].event); | 116 | return err; |
| 57 | } | 117 | } |
| 58 | new->num++; | 118 | new->num++; |
| 59 | 119 | ||
| 60 | /* Now put new one in place. */ | 120 | /* |
| 121 | * Now put new one in place: rcu_assign_pointer() is a fancy way of | ||
| 122 | * doing "lg->eventfds = new", but it uses memory barriers to make | ||
| 123 | * absolutely sure that the contents of "new" written above is nailed | ||
| 124 | * down before we actually do the assignment. | ||
| 125 | * | ||
| 126 | * We have to think about these kinds of things when we're operating on | ||
| 127 | * live data without locks. | ||
| 128 | */ | ||
| 61 | rcu_assign_pointer(lg->eventfds, new); | 129 | rcu_assign_pointer(lg->eventfds, new); |
| 62 | 130 | ||
| 63 | /* We're not in a big hurry. Wait until noone's looking at old | 131 | /* |
| 64 | * version, then delete it. */ | 132 | * We're not in a big hurry. Wait until noone's looking at old |
| 133 | * version, then free it. | ||
| 134 | */ | ||
| 65 | synchronize_rcu(); | 135 | synchronize_rcu(); |
| 66 | kfree(old); | 136 | kfree(old); |
| 67 | 137 | ||
| 68 | return 0; | 138 | return 0; |
| 69 | } | 139 | } |
| 70 | 140 | ||
| 141 | /*L:052 | ||
| 142 | * Receiving notifications from the Guest is usually done by attaching a | ||
| 143 | * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will | ||
| 144 | * become readable when the Guest does an LHCALL_NOTIFY with that value. | ||
| 145 | * | ||
| 146 | * This is really convenient for processing each virtqueue in a separate | ||
| 147 | * thread. | ||
| 148 | */ | ||
| 71 | static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) | 149 | static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) |
| 72 | { | 150 | { |
| 73 | unsigned long addr, fd; | 151 | unsigned long addr, fd; |
| @@ -79,15 +157,22 @@ static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) | |||
| 79 | if (get_user(fd, input) != 0) | 157 | if (get_user(fd, input) != 0) |
| 80 | return -EFAULT; | 158 | return -EFAULT; |
| 81 | 159 | ||
| 160 | /* | ||
| 161 | * Just make sure two callers don't add eventfds at once. We really | ||
| 162 | * only need to lock against callers adding to the same Guest, so using | ||
| 163 | * the Big Lguest Lock is overkill. But this is setup, not a fast path. | ||
| 164 | */ | ||
| 82 | mutex_lock(&lguest_lock); | 165 | mutex_lock(&lguest_lock); |
| 83 | err = add_eventfd(lg, addr, fd); | 166 | err = add_eventfd(lg, addr, fd); |
| 84 | mutex_unlock(&lguest_lock); | 167 | mutex_unlock(&lguest_lock); |
| 85 | 168 | ||
| 86 | return 0; | 169 | return err; |
| 87 | } | 170 | } |
| 88 | 171 | ||
| 89 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt | 172 | /*L:050 |
| 90 | * number to /dev/lguest. */ | 173 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
| 174 | * number to /dev/lguest. | ||
| 175 | */ | ||
| 91 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) | 176 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
| 92 | { | 177 | { |
| 93 | unsigned long irq; | 178 | unsigned long irq; |
| @@ -97,12 +182,18 @@ static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) | |||
| 97 | if (irq >= LGUEST_IRQS) | 182 | if (irq >= LGUEST_IRQS) |
| 98 | return -EINVAL; | 183 | return -EINVAL; |
| 99 | 184 | ||
| 185 | /* | ||
| 186 | * Next time the Guest runs, the core code will see if it can deliver | ||
| 187 | * this interrupt. | ||
| 188 | */ | ||
| 100 | set_interrupt(cpu, irq); | 189 | set_interrupt(cpu, irq); |
| 101 | return 0; | 190 | return 0; |
| 102 | } | 191 | } |
| 103 | 192 | ||
| 104 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading | 193 | /*L:040 |
| 105 | * from /dev/lguest. */ | 194 | * Once our Guest is initialized, the Launcher makes it run by reading |
| 195 | * from /dev/lguest. | ||
| 196 | */ | ||
| 106 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | 197 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
| 107 | { | 198 | { |
| 108 | struct lguest *lg = file->private_data; | 199 | struct lguest *lg = file->private_data; |
| @@ -138,8 +229,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
| 138 | return len; | 229 | return len; |
| 139 | } | 230 | } |
| 140 | 231 | ||
| 141 | /* If we returned from read() last time because the Guest sent I/O, | 232 | /* |
| 142 | * clear the flag. */ | 233 | * If we returned from read() last time because the Guest sent I/O, |
| 234 | * clear the flag. | ||
| 235 | */ | ||
| 143 | if (cpu->pending_notify) | 236 | if (cpu->pending_notify) |
| 144 | cpu->pending_notify = 0; | 237 | cpu->pending_notify = 0; |
| 145 | 238 | ||
| @@ -147,8 +240,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) | |||
| 147 | return run_guest(cpu, (unsigned long __user *)user); | 240 | return run_guest(cpu, (unsigned long __user *)user); |
| 148 | } | 241 | } |
| 149 | 242 | ||
| 150 | /*L:025 This actually initializes a CPU. For the moment, a Guest is only | 243 | /*L:025 |
| 151 | * uniprocessor, so "id" is always 0. */ | 244 | * This actually initializes a CPU. For the moment, a Guest is only |
| 245 | * uniprocessor, so "id" is always 0. | ||
| 246 | */ | ||
| 152 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | 247 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
| 153 | { | 248 | { |
| 154 | /* We have a limited number the number of CPUs in the lguest struct. */ | 249 | /* We have a limited number the number of CPUs in the lguest struct. */ |
| @@ -163,8 +258,10 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
| 163 | /* Each CPU has a timer it can set. */ | 258 | /* Each CPU has a timer it can set. */ |
| 164 | init_clockdev(cpu); | 259 | init_clockdev(cpu); |
| 165 | 260 | ||
| 166 | /* We need a complete page for the Guest registers: they are accessible | 261 | /* |
| 167 | * to the Guest and we can only grant it access to whole pages. */ | 262 | * We need a complete page for the Guest registers: they are accessible |
| 263 | * to the Guest and we can only grant it access to whole pages. | ||
| 264 | */ | ||
| 168 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); | 265 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
| 169 | if (!cpu->regs_page) | 266 | if (!cpu->regs_page) |
| 170 | return -ENOMEM; | 267 | return -ENOMEM; |
| @@ -172,29 +269,38 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
| 172 | /* We actually put the registers at the bottom of the page. */ | 269 | /* We actually put the registers at the bottom of the page. */ |
| 173 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); | 270 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
| 174 | 271 | ||
| 175 | /* Now we initialize the Guest's registers, handing it the start | 272 | /* |
| 176 | * address. */ | 273 | * Now we initialize the Guest's registers, handing it the start |
| 274 | * address. | ||
| 275 | */ | ||
| 177 | lguest_arch_setup_regs(cpu, start_ip); | 276 | lguest_arch_setup_regs(cpu, start_ip); |
| 178 | 277 | ||
| 179 | /* We keep a pointer to the Launcher task (ie. current task) for when | 278 | /* |
| 180 | * other Guests want to wake this one (eg. console input). */ | 279 | * We keep a pointer to the Launcher task (ie. current task) for when |
| 280 | * other Guests want to wake this one (eg. console input). | ||
| 281 | */ | ||
| 181 | cpu->tsk = current; | 282 | cpu->tsk = current; |
| 182 | 283 | ||
| 183 | /* We need to keep a pointer to the Launcher's memory map, because if | 284 | /* |
| 285 | * We need to keep a pointer to the Launcher's memory map, because if | ||
| 184 | * the Launcher dies we need to clean it up. If we don't keep a | 286 | * the Launcher dies we need to clean it up. If we don't keep a |
| 185 | * reference, it is destroyed before close() is called. */ | 287 | * reference, it is destroyed before close() is called. |
| 288 | */ | ||
| 186 | cpu->mm = get_task_mm(cpu->tsk); | 289 | cpu->mm = get_task_mm(cpu->tsk); |
| 187 | 290 | ||
| 188 | /* We remember which CPU's pages this Guest used last, for optimization | 291 | /* |
| 189 | * when the same Guest runs on the same CPU twice. */ | 292 | * We remember which CPU's pages this Guest used last, for optimization |
| 293 | * when the same Guest runs on the same CPU twice. | ||
| 294 | */ | ||
| 190 | cpu->last_pages = NULL; | 295 | cpu->last_pages = NULL; |
| 191 | 296 | ||
| 192 | /* No error == success. */ | 297 | /* No error == success. */ |
| 193 | return 0; | 298 | return 0; |
| 194 | } | 299 | } |
| 195 | 300 | ||
| 196 | /*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit) | 301 | /*L:020 |
| 197 | * values (in addition to the LHREQ_INITIALIZE value). These are: | 302 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in |
| 303 | * addition to the LHREQ_INITIALIZE value). These are: | ||
| 198 | * | 304 | * |
| 199 | * base: The start of the Guest-physical memory inside the Launcher memory. | 305 | * base: The start of the Guest-physical memory inside the Launcher memory. |
| 200 | * | 306 | * |
| @@ -206,14 +312,15 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) | |||
| 206 | */ | 312 | */ |
| 207 | static int initialize(struct file *file, const unsigned long __user *input) | 313 | static int initialize(struct file *file, const unsigned long __user *input) |
| 208 | { | 314 | { |
| 209 | /* "struct lguest" contains everything we (the Host) know about a | 315 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
| 210 | * Guest. */ | ||
| 211 | struct lguest *lg; | 316 | struct lguest *lg; |
| 212 | int err; | 317 | int err; |
| 213 | unsigned long args[3]; | 318 | unsigned long args[3]; |
| 214 | 319 | ||
| 215 | /* We grab the Big Lguest lock, which protects against multiple | 320 | /* |
| 216 | * simultaneous initializations. */ | 321 | * We grab the Big Lguest lock, which protects against multiple |
| 322 | * simultaneous initializations. | ||
| 323 | */ | ||
| 217 | mutex_lock(&lguest_lock); | 324 | mutex_lock(&lguest_lock); |
| 218 | /* You can't initialize twice! Close the device and start again... */ | 325 | /* You can't initialize twice! Close the device and start again... */ |
| 219 | if (file->private_data) { | 326 | if (file->private_data) { |
| @@ -248,8 +355,10 @@ static int initialize(struct file *file, const unsigned long __user *input) | |||
| 248 | if (err) | 355 | if (err) |
| 249 | goto free_eventfds; | 356 | goto free_eventfds; |
| 250 | 357 | ||
| 251 | /* Initialize the Guest's shadow page tables, using the toplevel | 358 | /* |
| 252 | * address the Launcher gave us. This allocates memory, so can fail. */ | 359 | * Initialize the Guest's shadow page tables, using the toplevel |
| 360 | * address the Launcher gave us. This allocates memory, so can fail. | ||
| 361 | */ | ||
| 253 | err = init_guest_pagetable(lg); | 362 | err = init_guest_pagetable(lg); |
| 254 | if (err) | 363 | if (err) |
| 255 | goto free_regs; | 364 | goto free_regs; |
| @@ -274,20 +383,24 @@ unlock: | |||
| 274 | return err; | 383 | return err; |
| 275 | } | 384 | } |
| 276 | 385 | ||
| 277 | /*L:010 The first operation the Launcher does must be a write. All writes | 386 | /*L:010 |
| 387 | * The first operation the Launcher does must be a write. All writes | ||
| 278 | * start with an unsigned long number: for the first write this must be | 388 | * start with an unsigned long number: for the first write this must be |
| 279 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use | 389 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
| 280 | * writes of other values to send interrupts. | 390 | * writes of other values to send interrupts or set up receipt of notifications. |
| 281 | * | 391 | * |
| 282 | * Note that we overload the "offset" in the /dev/lguest file to indicate what | 392 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
| 283 | * CPU number we're dealing with. Currently this is always 0, since we only | 393 | * CPU number we're dealing with. Currently this is always 0 since we only |
| 284 | * support uniprocessor Guests, but you can see the beginnings of SMP support | 394 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
| 285 | * here. */ | 395 | * here. |
| 396 | */ | ||
| 286 | static ssize_t write(struct file *file, const char __user *in, | 397 | static ssize_t write(struct file *file, const char __user *in, |
| 287 | size_t size, loff_t *off) | 398 | size_t size, loff_t *off) |
| 288 | { | 399 | { |
| 289 | /* Once the Guest is initialized, we hold the "struct lguest" in the | 400 | /* |
| 290 | * file private data. */ | 401 | * Once the Guest is initialized, we hold the "struct lguest" in the |
| 402 | * file private data. | ||
| 403 | */ | ||
| 291 | struct lguest *lg = file->private_data; | 404 | struct lguest *lg = file->private_data; |
| 292 | const unsigned long __user *input = (const unsigned long __user *)in; | 405 | const unsigned long __user *input = (const unsigned long __user *)in; |
| 293 | unsigned long req; | 406 | unsigned long req; |
| @@ -322,13 +435,15 @@ static ssize_t write(struct file *file, const char __user *in, | |||
| 322 | } | 435 | } |
| 323 | } | 436 | } |
| 324 | 437 | ||
| 325 | /*L:060 The final piece of interface code is the close() routine. It reverses | 438 | /*L:060 |
| 439 | * The final piece of interface code is the close() routine. It reverses | ||
| 326 | * everything done in initialize(). This is usually called because the | 440 | * everything done in initialize(). This is usually called because the |
| 327 | * Launcher exited. | 441 | * Launcher exited. |
| 328 | * | 442 | * |
| 329 | * Note that the close routine returns 0 or a negative error number: it can't | 443 | * Note that the close routine returns 0 or a negative error number: it can't |
| 330 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for | 444 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
| 331 | * letting them do it. :*/ | 445 | * letting them do it. |
| 446 | :*/ | ||
| 332 | static int close(struct inode *inode, struct file *file) | 447 | static int close(struct inode *inode, struct file *file) |
| 333 | { | 448 | { |
| 334 | struct lguest *lg = file->private_data; | 449 | struct lguest *lg = file->private_data; |
| @@ -338,8 +453,10 @@ static int close(struct inode *inode, struct file *file) | |||
| 338 | if (!lg) | 453 | if (!lg) |
| 339 | return 0; | 454 | return 0; |
| 340 | 455 | ||
| 341 | /* We need the big lock, to protect from inter-guest I/O and other | 456 | /* |
| 342 | * Launchers initializing guests. */ | 457 | * We need the big lock, to protect from inter-guest I/O and other |
| 458 | * Launchers initializing guests. | ||
| 459 | */ | ||
| 343 | mutex_lock(&lguest_lock); | 460 | mutex_lock(&lguest_lock); |
| 344 | 461 | ||
| 345 | /* Free up the shadow page tables for the Guest. */ | 462 | /* Free up the shadow page tables for the Guest. */ |
| @@ -350,8 +467,10 @@ static int close(struct inode *inode, struct file *file) | |||
| 350 | hrtimer_cancel(&lg->cpus[i].hrt); | 467 | hrtimer_cancel(&lg->cpus[i].hrt); |
| 351 | /* We can free up the register page we allocated. */ | 468 | /* We can free up the register page we allocated. */ |
| 352 | free_page(lg->cpus[i].regs_page); | 469 | free_page(lg->cpus[i].regs_page); |
| 353 | /* Now all the memory cleanups are done, it's safe to release | 470 | /* |
| 354 | * the Launcher's memory management structure. */ | 471 | * Now all the memory cleanups are done, it's safe to release |
| 472 | * the Launcher's memory management structure. | ||
| 473 | */ | ||
| 355 | mmput(lg->cpus[i].mm); | 474 | mmput(lg->cpus[i].mm); |
| 356 | } | 475 | } |
| 357 | 476 | ||
| @@ -360,8 +479,10 @@ static int close(struct inode *inode, struct file *file) | |||
| 360 | eventfd_ctx_put(lg->eventfds->map[i].event); | 479 | eventfd_ctx_put(lg->eventfds->map[i].event); |
| 361 | kfree(lg->eventfds); | 480 | kfree(lg->eventfds); |
| 362 | 481 | ||
| 363 | /* If lg->dead doesn't contain an error code it will be NULL or a | 482 | /* |
| 364 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ | 483 | * If lg->dead doesn't contain an error code it will be NULL or a |
| 484 | * kmalloc()ed string, either of which is ok to hand to kfree(). | ||
| 485 | */ | ||
| 365 | if (!IS_ERR(lg->dead)) | 486 | if (!IS_ERR(lg->dead)) |
| 366 | kfree(lg->dead); | 487 | kfree(lg->dead); |
| 367 | /* Free the memory allocated to the lguest_struct */ | 488 | /* Free the memory allocated to the lguest_struct */ |
| @@ -385,7 +506,8 @@ static int close(struct inode *inode, struct file *file) | |||
| 385 | * | 506 | * |
| 386 | * We begin our understanding with the Host kernel interface which the Launcher | 507 | * We begin our understanding with the Host kernel interface which the Launcher |
| 387 | * uses: reading and writing a character device called /dev/lguest. All the | 508 | * uses: reading and writing a character device called /dev/lguest. All the |
| 388 | * work happens in the read(), write() and close() routines: */ | 509 | * work happens in the read(), write() and close() routines: |
| 510 | */ | ||
| 389 | static struct file_operations lguest_fops = { | 511 | static struct file_operations lguest_fops = { |
| 390 | .owner = THIS_MODULE, | 512 | .owner = THIS_MODULE, |
| 391 | .release = close, | 513 | .release = close, |
| @@ -393,8 +515,10 @@ static struct file_operations lguest_fops = { | |||
| 393 | .read = read, | 515 | .read = read, |
| 394 | }; | 516 | }; |
| 395 | 517 | ||
| 396 | /* This is a textbook example of a "misc" character device. Populate a "struct | 518 | /* |
| 397 | * miscdevice" and register it with misc_register(). */ | 519 | * This is a textbook example of a "misc" character device. Populate a "struct |
| 520 | * miscdevice" and register it with misc_register(). | ||
| 521 | */ | ||
| 398 | static struct miscdevice lguest_dev = { | 522 | static struct miscdevice lguest_dev = { |
| 399 | .minor = MISC_DYNAMIC_MINOR, | 523 | .minor = MISC_DYNAMIC_MINOR, |
| 400 | .name = "lguest", | 524 | .name = "lguest", |
diff --git a/drivers/lguest/page_tables.c b/drivers/lguest/page_tables.c index a6fe1abda240..a8d0aee3bc0e 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,18 +19,20 @@ | |||
| 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 |
| 27 | * | 31 | * |
| 28 | * We use two-level page tables for the Guest. If you're not entirely | 32 | * We use two-level page tables for the Guest, or three-level with PAE. If |
| 29 | * comfortable with virtual addresses, physical addresses and page tables then | 33 | * you're not entirely comfortable with virtual addresses, physical addresses |
| 30 | * I recommend you review arch/x86/lguest/boot.c's "Page Table Handling" (with | 34 | * and page tables then I recommend you review arch/x86/lguest/boot.c's "Page |
| 31 | * diagrams!). | 35 | * Table Handling" (with diagrams!). |
| 32 | * | 36 | * |
| 33 | * The Guest keeps page tables, but we maintain the actual ones here: these are | 37 | * The Guest keeps page tables, but we maintain the actual ones here: these are |
| 34 | * called "shadow" page tables. Which is a very Guest-centric name: these are | 38 | * called "shadow" page tables. Which is a very Guest-centric name: these are |
| @@ -45,16 +49,18 @@ | |||
| 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 | * The Switcher uses the complete top PTE page. That's 1024 PTE entries (4MB) |
| 52 | * conveniently placed at the top 4MB, so it uses a separate, complete PTE | 56 | * or 512 PTE entries with PAE (2MB). |
| 53 | * page. */ | 57 | */ |
| 54 | #define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1) | 58 | #define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1) |
| 55 | 59 | ||
| 56 | /* For PAE we need the PMD index as well. We use the last 2MB, so we | 60 | /* |
| 57 | * will need the last pmd entry of the last pmd page. */ | 61 | * For PAE we need the PMD index as well. We use the last 2MB, so we |
| 62 | * will need the last pmd entry of the last pmd page. | ||
| 63 | */ | ||
| 58 | #ifdef CONFIG_X86_PAE | 64 | #ifdef CONFIG_X86_PAE |
| 59 | #define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1) | 65 | #define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1) |
| 60 | #define RESERVE_MEM 2U | 66 | #define RESERVE_MEM 2U |
| @@ -64,14 +70,18 @@ | |||
| 64 | #define CHECK_GPGD_MASK _PAGE_TABLE | 70 | #define CHECK_GPGD_MASK _PAGE_TABLE |
| 65 | #endif | 71 | #endif |
| 66 | 72 | ||
| 67 | /* We actually need a separate PTE page for each CPU. Remember that after the | 73 | /* |
| 74 | * 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 | 75 | * 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. */ | 76 | * CPU's guest to see the pages of any other CPU. |
| 77 | */ | ||
| 70 | static DEFINE_PER_CPU(pte_t *, switcher_pte_pages); | 78 | static DEFINE_PER_CPU(pte_t *, switcher_pte_pages); |
| 71 | #define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu) | 79 | #define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu) |
| 72 | 80 | ||
| 73 | /*H:320 The page table code is curly enough to need helper functions to keep it | 81 | /*H:320 |
| 74 | * clear and clean. | 82 | * The page table code is curly enough to need helper functions to keep it |
| 83 | * clear and clean. The kernel itself provides many of them; one advantage | ||
| 84 | * of insisting that the Guest and Host use the same CONFIG_PAE setting. | ||
| 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") |
| 77 | * page tables. | 87 | * page tables. |
| @@ -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 functions are 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); |
| @@ -148,6 +165,7 @@ static unsigned long gpgd_addr(struct lg_cpu *cpu, unsigned long vaddr) | |||
| 148 | } | 165 | } |
| 149 | 166 | ||
| 150 | #ifdef CONFIG_X86_PAE | 167 | #ifdef CONFIG_X86_PAE |
| 168 | /* Follow the PGD to the PMD. */ | ||
| 151 | static unsigned long gpmd_addr(pgd_t gpgd, unsigned long vaddr) | 169 | static unsigned long gpmd_addr(pgd_t gpgd, unsigned long vaddr) |
| 152 | { | 170 | { |
| 153 | unsigned long gpage = pgd_pfn(gpgd) << PAGE_SHIFT; | 171 | unsigned long gpage = pgd_pfn(gpgd) << PAGE_SHIFT; |
| @@ -155,6 +173,7 @@ static unsigned long gpmd_addr(pgd_t gpgd, unsigned long vaddr) | |||
| 155 | return gpage + pmd_index(vaddr) * sizeof(pmd_t); | 173 | return gpage + pmd_index(vaddr) * sizeof(pmd_t); |
| 156 | } | 174 | } |
| 157 | 175 | ||
| 176 | /* Follow the PMD to the PTE. */ | ||
| 158 | static unsigned long gpte_addr(struct lg_cpu *cpu, | 177 | static unsigned long gpte_addr(struct lg_cpu *cpu, |
| 159 | pmd_t gpmd, unsigned long vaddr) | 178 | pmd_t gpmd, unsigned long vaddr) |
| 160 | { | 179 | { |
| @@ -164,6 +183,7 @@ static unsigned long gpte_addr(struct lg_cpu *cpu, | |||
| 164 | return gpage + pte_index(vaddr) * sizeof(pte_t); | 183 | return gpage + pte_index(vaddr) * sizeof(pte_t); |
| 165 | } | 184 | } |
| 166 | #else | 185 | #else |
| 186 | /* Follow the PGD to the PTE (no mid-level for !PAE). */ | ||
| 167 | static unsigned long gpte_addr(struct lg_cpu *cpu, | 187 | static unsigned long gpte_addr(struct lg_cpu *cpu, |
| 168 | pgd_t gpgd, unsigned long vaddr) | 188 | pgd_t gpgd, unsigned long vaddr) |
| 169 | { | 189 | { |
| @@ -175,17 +195,21 @@ static unsigned long gpte_addr(struct lg_cpu *cpu, | |||
| 175 | #endif | 195 | #endif |
| 176 | /*:*/ | 196 | /*:*/ |
| 177 | 197 | ||
| 178 | /*M:014 get_pfn is slow: we could probably try to grab batches of pages here as | 198 | /*M:014 |
| 179 | * an optimization (ie. pre-faulting). :*/ | 199 | * get_pfn is slow: we could probably try to grab batches of pages here as |
| 200 | * an optimization (ie. pre-faulting). | ||
| 201 | :*/ | ||
| 180 | 202 | ||
| 181 | /*H:350 This routine takes a page number given by the Guest and converts it to | 203 | /*H:350 |
| 204 | * 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 | 205 | * 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 | 206 | * 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 | 207 | * 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. | 208 | * shared so had to be copied, but we ran out of memory. |
| 186 | * | 209 | * |
| 187 | * This holds a reference to the page, so release_pte() is careful to put that | 210 | * This holds a reference to the page, so release_pte() is careful to put that |
| 188 | * back. */ | 211 | * back. |
| 212 | */ | ||
| 189 | static unsigned long get_pfn(unsigned long virtpfn, int write) | 213 | static unsigned long get_pfn(unsigned long virtpfn, int write) |
| 190 | { | 214 | { |
| 191 | struct page *page; | 215 | struct page *page; |
| @@ -198,33 +222,41 @@ static unsigned long get_pfn(unsigned long virtpfn, int write) | |||
| 198 | return -1UL; | 222 | return -1UL; |
| 199 | } | 223 | } |
| 200 | 224 | ||
| 201 | /*H:340 Converting a Guest page table entry to a shadow (ie. real) page table | 225 | /*H:340 |
| 226 | * 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 | 227 | * 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 | 228 | * Guest PTE contains a virtual page number: the CPU needs the real page |
| 204 | * number. */ | 229 | * number. |
| 230 | */ | ||
| 205 | static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write) | 231 | static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write) |
| 206 | { | 232 | { |
| 207 | unsigned long pfn, base, flags; | 233 | unsigned long pfn, base, flags; |
| 208 | 234 | ||
| 209 | /* The Guest sets the global flag, because it thinks that it is using | 235 | /* |
| 236 | * 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 | 237 | * 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 | 238 | * flushing a kernel mapping or a userspace mapping. We don't actually |
| 212 | * use the global bit, so throw it away. */ | 239 | * use the global bit, so throw it away. |
| 240 | */ | ||
| 213 | flags = (pte_flags(gpte) & ~_PAGE_GLOBAL); | 241 | flags = (pte_flags(gpte) & ~_PAGE_GLOBAL); |
| 214 | 242 | ||
| 215 | /* The Guest's pages are offset inside the Launcher. */ | 243 | /* The Guest's pages are offset inside the Launcher. */ |
| 216 | base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE; | 244 | base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE; |
| 217 | 245 | ||
| 218 | /* We need a temporary "unsigned long" variable to hold the answer from | 246 | /* |
| 247 | * We need a temporary "unsigned long" variable to hold the answer from | ||
| 219 | * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't | 248 | * 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 | 249 | * fit in spte.pfn. get_pfn() finds the real physical number of the |
| 221 | * page, given the virtual number. */ | 250 | * page, given the virtual number. |
| 251 | */ | ||
| 222 | pfn = get_pfn(base + pte_pfn(gpte), write); | 252 | pfn = get_pfn(base + pte_pfn(gpte), write); |
| 223 | if (pfn == -1UL) { | 253 | if (pfn == -1UL) { |
| 224 | kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte)); | 254 | kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte)); |
| 225 | /* When we destroy the Guest, we'll go through the shadow page | 255 | /* |
| 256 | * When we destroy the Guest, we'll go through the shadow page | ||
| 226 | * tables and release_pte() them. Make sure we don't think | 257 | * tables and release_pte() them. Make sure we don't think |
| 227 | * this one is valid! */ | 258 | * this one is valid! |
| 259 | */ | ||
| 228 | flags = 0; | 260 | flags = 0; |
| 229 | } | 261 | } |
| 230 | /* Now we assemble our shadow PTE from the page number and flags. */ | 262 | /* Now we assemble our shadow PTE from the page number and flags. */ |
| @@ -234,8 +266,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: */ | 266 | /*H:460 And to complete the chain, release_pte() looks like this: */ |
| 235 | static void release_pte(pte_t pte) | 267 | static void release_pte(pte_t pte) |
| 236 | { | 268 | { |
| 237 | /* Remember that get_user_pages_fast() took a reference to the page, in | 269 | /* |
| 238 | * get_pfn()? We have to put it back now. */ | 270 | * Remember that get_user_pages_fast() took a reference to the page, in |
| 271 | * get_pfn()? We have to put it back now. | ||
| 272 | */ | ||
| 239 | if (pte_flags(pte) & _PAGE_PRESENT) | 273 | if (pte_flags(pte) & _PAGE_PRESENT) |
| 240 | put_page(pte_page(pte)); | 274 | put_page(pte_page(pte)); |
| 241 | } | 275 | } |
| @@ -273,7 +307,8 @@ static void check_gpmd(struct lg_cpu *cpu, pmd_t gpmd) | |||
| 273 | * and return to the Guest without it knowing. | 307 | * and return to the Guest without it knowing. |
| 274 | * | 308 | * |
| 275 | * If we fixed up the fault (ie. we mapped the address), this routine returns | 309 | * 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. */ | 310 | * true. Otherwise, it was a real fault and we need to tell the Guest. |
| 311 | */ | ||
| 277 | bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | 312 | bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) |
| 278 | { | 313 | { |
| 279 | pgd_t gpgd; | 314 | pgd_t gpgd; |
| @@ -282,6 +317,7 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 282 | pte_t gpte; | 317 | pte_t gpte; |
| 283 | pte_t *spte; | 318 | pte_t *spte; |
| 284 | 319 | ||
| 320 | /* Mid level for PAE. */ | ||
| 285 | #ifdef CONFIG_X86_PAE | 321 | #ifdef CONFIG_X86_PAE |
| 286 | pmd_t *spmd; | 322 | pmd_t *spmd; |
| 287 | pmd_t gpmd; | 323 | pmd_t gpmd; |
| @@ -298,22 +334,26 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 298 | if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) { | 334 | if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) { |
| 299 | /* No shadow entry: allocate a new shadow PTE page. */ | 335 | /* No shadow entry: allocate a new shadow PTE page. */ |
| 300 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); | 336 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); |
| 301 | /* This is not really the Guest's fault, but killing it is | 337 | /* |
| 302 | * simple for this corner case. */ | 338 | * This is not really the Guest's fault, but killing it is |
| 339 | * simple for this corner case. | ||
| 340 | */ | ||
| 303 | if (!ptepage) { | 341 | if (!ptepage) { |
| 304 | kill_guest(cpu, "out of memory allocating pte page"); | 342 | kill_guest(cpu, "out of memory allocating pte page"); |
| 305 | return false; | 343 | return false; |
| 306 | } | 344 | } |
| 307 | /* We check that the Guest pgd is OK. */ | 345 | /* We check that the Guest pgd is OK. */ |
| 308 | check_gpgd(cpu, gpgd); | 346 | check_gpgd(cpu, gpgd); |
| 309 | /* And we copy the flags to the shadow PGD entry. The page | 347 | /* |
| 310 | * number in the shadow PGD is the page we just allocated. */ | 348 | * And we copy the flags to the shadow PGD entry. The page |
| 349 | * number in the shadow PGD is the page we just allocated. | ||
| 350 | */ | ||
| 311 | set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd))); | 351 | set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd))); |
| 312 | } | 352 | } |
| 313 | 353 | ||
| 314 | #ifdef CONFIG_X86_PAE | 354 | #ifdef CONFIG_X86_PAE |
| 315 | gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t); | 355 | gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t); |
| 316 | /* middle level not present? We can't map it in. */ | 356 | /* Middle level not present? We can't map it in. */ |
| 317 | if (!(pmd_flags(gpmd) & _PAGE_PRESENT)) | 357 | if (!(pmd_flags(gpmd) & _PAGE_PRESENT)) |
| 318 | return false; | 358 | return false; |
| 319 | 359 | ||
| @@ -324,8 +364,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 324 | /* No shadow entry: allocate a new shadow PTE page. */ | 364 | /* No shadow entry: allocate a new shadow PTE page. */ |
| 325 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); | 365 | unsigned long ptepage = get_zeroed_page(GFP_KERNEL); |
| 326 | 366 | ||
| 327 | /* This is not really the Guest's fault, but killing it is | 367 | /* |
| 328 | * simple for this corner case. */ | 368 | * This is not really the Guest's fault, but killing it is |
| 369 | * simple for this corner case. | ||
| 370 | */ | ||
| 329 | if (!ptepage) { | 371 | if (!ptepage) { |
| 330 | kill_guest(cpu, "out of memory allocating pte page"); | 372 | kill_guest(cpu, "out of memory allocating pte page"); |
| 331 | return false; | 373 | return false; |
| @@ -334,27 +376,37 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 334 | /* We check that the Guest pmd is OK. */ | 376 | /* We check that the Guest pmd is OK. */ |
| 335 | check_gpmd(cpu, gpmd); | 377 | check_gpmd(cpu, gpmd); |
| 336 | 378 | ||
| 337 | /* And we copy the flags to the shadow PMD entry. The page | 379 | /* |
| 338 | * number in the shadow PMD is the page we just allocated. */ | 380 | * And we copy the flags to the shadow PMD entry. The page |
| 381 | * number in the shadow PMD is the page we just allocated. | ||
| 382 | */ | ||
| 339 | native_set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd))); | 383 | native_set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd))); |
| 340 | } | 384 | } |
| 341 | 385 | ||
| 342 | /* OK, now we look at the lower level in the Guest page table: keep its | 386 | /* |
| 343 | * address, because we might update it later. */ | 387 | * OK, now we look at the lower level in the Guest page table: keep its |
| 388 | * address, because we might update it later. | ||
| 389 | */ | ||
| 344 | gpte_ptr = gpte_addr(cpu, gpmd, vaddr); | 390 | gpte_ptr = gpte_addr(cpu, gpmd, vaddr); |
| 345 | #else | 391 | #else |
| 346 | /* OK, now we look at the lower level in the Guest page table: keep its | 392 | /* |
| 347 | * address, because we might update it later. */ | 393 | * OK, now we look at the lower level in the Guest page table: keep its |
| 394 | * address, because we might update it later. | ||
| 395 | */ | ||
| 348 | gpte_ptr = gpte_addr(cpu, gpgd, vaddr); | 396 | gpte_ptr = gpte_addr(cpu, gpgd, vaddr); |
| 349 | #endif | 397 | #endif |
| 398 | |||
| 399 | /* Read the actual PTE value. */ | ||
| 350 | gpte = lgread(cpu, gpte_ptr, pte_t); | 400 | gpte = lgread(cpu, gpte_ptr, pte_t); |
| 351 | 401 | ||
| 352 | /* If this page isn't in the Guest page tables, we can't page it in. */ | 402 | /* If this page isn't in the Guest page tables, we can't page it in. */ |
| 353 | if (!(pte_flags(gpte) & _PAGE_PRESENT)) | 403 | if (!(pte_flags(gpte) & _PAGE_PRESENT)) |
| 354 | return false; | 404 | return false; |
| 355 | 405 | ||
| 356 | /* Check they're not trying to write to a page the Guest wants | 406 | /* |
| 357 | * read-only (bit 2 of errcode == write). */ | 407 | * Check they're not trying to write to a page the Guest wants |
| 408 | * read-only (bit 2 of errcode == write). | ||
| 409 | */ | ||
| 358 | if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW)) | 410 | if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW)) |
| 359 | return false; | 411 | return false; |
| 360 | 412 | ||
| @@ -362,8 +414,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 362 | if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER)) | 414 | if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER)) |
| 363 | return false; | 415 | return false; |
| 364 | 416 | ||
| 365 | /* Check that the Guest PTE flags are OK, and the page number is below | 417 | /* |
| 366 | * the pfn_limit (ie. not mapping the Launcher binary). */ | 418 | * Check that the Guest PTE flags are OK, and the page number is below |
| 419 | * the pfn_limit (ie. not mapping the Launcher binary). | ||
| 420 | */ | ||
| 367 | check_gpte(cpu, gpte); | 421 | check_gpte(cpu, gpte); |
| 368 | 422 | ||
| 369 | /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */ | 423 | /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */ |
| @@ -373,29 +427,40 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 373 | 427 | ||
| 374 | /* Get the pointer to the shadow PTE entry we're going to set. */ | 428 | /* Get the pointer to the shadow PTE entry we're going to set. */ |
| 375 | spte = spte_addr(cpu, *spgd, vaddr); | 429 | spte = spte_addr(cpu, *spgd, vaddr); |
| 376 | /* If there was a valid shadow PTE entry here before, we release it. | 430 | |
| 377 | * This can happen with a write to a previously read-only entry. */ | 431 | /* |
| 432 | * If there was a valid shadow PTE entry here before, we release it. | ||
| 433 | * This can happen with a write to a previously read-only entry. | ||
| 434 | */ | ||
| 378 | release_pte(*spte); | 435 | release_pte(*spte); |
| 379 | 436 | ||
| 380 | /* If this is a write, we insist that the Guest page is writable (the | 437 | /* |
| 381 | * final arg to gpte_to_spte()). */ | 438 | * If this is a write, we insist that the Guest page is writable (the |
| 439 | * final arg to gpte_to_spte()). | ||
| 440 | */ | ||
| 382 | if (pte_dirty(gpte)) | 441 | if (pte_dirty(gpte)) |
| 383 | *spte = gpte_to_spte(cpu, gpte, 1); | 442 | *spte = gpte_to_spte(cpu, gpte, 1); |
| 384 | else | 443 | else |
| 385 | /* If this is a read, don't set the "writable" bit in the page | 444 | /* |
| 445 | * 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 | 446 | * 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 | 447 | * we will come back here when a write does actually occur, so |
| 388 | * we can update the Guest's _PAGE_DIRTY flag. */ | 448 | * we can update the Guest's _PAGE_DIRTY flag. |
| 449 | */ | ||
| 389 | native_set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0)); | 450 | native_set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0)); |
| 390 | 451 | ||
| 391 | /* Finally, we write the Guest PTE entry back: we've set the | 452 | /* |
| 392 | * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags. */ | 453 | * Finally, we write the Guest PTE entry back: we've set the |
| 454 | * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags. | ||
| 455 | */ | ||
| 393 | lgwrite(cpu, gpte_ptr, pte_t, gpte); | 456 | lgwrite(cpu, gpte_ptr, pte_t, gpte); |
| 394 | 457 | ||
| 395 | /* The fault is fixed, the page table is populated, the mapping | 458 | /* |
| 459 | * The fault is fixed, the page table is populated, the mapping | ||
| 396 | * manipulated, the result returned and the code complete. A small | 460 | * manipulated, the result returned and the code complete. A small |
| 397 | * delay and a trace of alliteration are the only indications the Guest | 461 | * delay and a trace of alliteration are the only indications the Guest |
| 398 | * has that a page fault occurred at all. */ | 462 | * has that a page fault occurred at all. |
| 463 | */ | ||
| 399 | return true; | 464 | return true; |
| 400 | } | 465 | } |
| 401 | 466 | ||
| @@ -408,7 +473,8 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) | |||
| 408 | * mapped, so it's overkill. | 473 | * mapped, so it's overkill. |
| 409 | * | 474 | * |
| 410 | * This is a quick version which answers the question: is this virtual address | 475 | * This is a quick version which answers the question: is this virtual address |
| 411 | * mapped by the shadow page tables, and is it writable? */ | 476 | * mapped by the shadow page tables, and is it writable? |
| 477 | */ | ||
| 412 | static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) | 478 | static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) |
| 413 | { | 479 | { |
| 414 | pgd_t *spgd; | 480 | pgd_t *spgd; |
| @@ -428,21 +494,26 @@ static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) | |||
| 428 | return false; | 494 | return false; |
| 429 | #endif | 495 | #endif |
| 430 | 496 | ||
| 431 | /* Check the flags on the pte entry itself: it must be present and | 497 | /* |
| 432 | * writable. */ | 498 | * Check the flags on the pte entry itself: it must be present and |
| 499 | * writable. | ||
| 500 | */ | ||
| 433 | flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr))); | 501 | flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr))); |
| 434 | 502 | ||
| 435 | return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW); | 503 | return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW); |
| 436 | } | 504 | } |
| 437 | 505 | ||
| 438 | /* So, when pin_stack_pages() asks us to pin a page, we check if it's already | 506 | /* |
| 507 | * 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 | 508 | * in the page tables, and if not, we call demand_page() with error code 2 |
| 440 | * (meaning "write"). */ | 509 | * (meaning "write"). |
| 510 | */ | ||
| 441 | void pin_page(struct lg_cpu *cpu, unsigned long vaddr) | 511 | void pin_page(struct lg_cpu *cpu, unsigned long vaddr) |
| 442 | { | 512 | { |
| 443 | if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2)) | 513 | if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2)) |
| 444 | kill_guest(cpu, "bad stack page %#lx", vaddr); | 514 | kill_guest(cpu, "bad stack page %#lx", vaddr); |
| 445 | } | 515 | } |
| 516 | /*:*/ | ||
| 446 | 517 | ||
| 447 | #ifdef CONFIG_X86_PAE | 518 | #ifdef CONFIG_X86_PAE |
| 448 | static void release_pmd(pmd_t *spmd) | 519 | static void release_pmd(pmd_t *spmd) |
| @@ -479,15 +550,21 @@ static void release_pgd(pgd_t *spgd) | |||
| 479 | } | 550 | } |
| 480 | 551 | ||
| 481 | #else /* !CONFIG_X86_PAE */ | 552 | #else /* !CONFIG_X86_PAE */ |
| 482 | /*H:450 If we chase down the release_pgd() code, it looks like this: */ | 553 | /*H:450 |
| 554 | * If we chase down the release_pgd() code, the non-PAE version looks like | ||
| 555 | * this. The PAE version is almost identical, but instead of calling | ||
| 556 | * release_pte it calls release_pmd(), which looks much like this. | ||
| 557 | */ | ||
| 483 | static void release_pgd(pgd_t *spgd) | 558 | static void release_pgd(pgd_t *spgd) |
| 484 | { | 559 | { |
| 485 | /* If the entry's not present, there's nothing to release. */ | 560 | /* If the entry's not present, there's nothing to release. */ |
| 486 | if (pgd_flags(*spgd) & _PAGE_PRESENT) { | 561 | if (pgd_flags(*spgd) & _PAGE_PRESENT) { |
| 487 | unsigned int i; | 562 | unsigned int i; |
| 488 | /* Converting the pfn to find the actual PTE page is easy: turn | 563 | /* |
| 564 | * Converting the pfn to find the actual PTE page is easy: turn | ||
| 489 | * the page number into a physical address, then convert to a | 565 | * the page number into a physical address, then convert to a |
| 490 | * virtual address (easy for kernel pages like this one). */ | 566 | * virtual address (easy for kernel pages like this one). |
| 567 | */ | ||
| 491 | pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); | 568 | pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); |
| 492 | /* For each entry in the page, we might need to release it. */ | 569 | /* For each entry in the page, we might need to release it. */ |
| 493 | for (i = 0; i < PTRS_PER_PTE; i++) | 570 | for (i = 0; i < PTRS_PER_PTE; i++) |
| @@ -499,9 +576,12 @@ static void release_pgd(pgd_t *spgd) | |||
| 499 | } | 576 | } |
| 500 | } | 577 | } |
| 501 | #endif | 578 | #endif |
| 502 | /*H:445 We saw flush_user_mappings() twice: once from the flush_user_mappings() | 579 | |
| 580 | /*H:445 | ||
| 581 | * 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. | 582 | * 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. */ | 583 | * It simply releases every PTE page from 0 up to the Guest's kernel address. |
| 584 | */ | ||
| 505 | static void flush_user_mappings(struct lguest *lg, int idx) | 585 | static void flush_user_mappings(struct lguest *lg, int idx) |
| 506 | { | 586 | { |
| 507 | unsigned int i; | 587 | unsigned int i; |
| @@ -510,10 +590,12 @@ static void flush_user_mappings(struct lguest *lg, int idx) | |||
| 510 | release_pgd(lg->pgdirs[idx].pgdir + i); | 590 | release_pgd(lg->pgdirs[idx].pgdir + i); |
| 511 | } | 591 | } |
| 512 | 592 | ||
| 513 | /*H:440 (v) Flushing (throwing away) page tables, | 593 | /*H:440 |
| 594 | * (v) Flushing (throwing away) page tables, | ||
| 514 | * | 595 | * |
| 515 | * The Guest has a hypercall to throw away the page tables: it's used when a | 596 | * The Guest has a hypercall to throw away the page tables: it's used when a |
| 516 | * large number of mappings have been changed. */ | 597 | * large number of mappings have been changed. |
| 598 | */ | ||
| 517 | void guest_pagetable_flush_user(struct lg_cpu *cpu) | 599 | void guest_pagetable_flush_user(struct lg_cpu *cpu) |
| 518 | { | 600 | { |
| 519 | /* Drop the userspace part of the current page table. */ | 601 | /* Drop the userspace part of the current page table. */ |
| @@ -551,9 +633,11 @@ unsigned long guest_pa(struct lg_cpu *cpu, unsigned long vaddr) | |||
| 551 | return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK); | 633 | return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK); |
| 552 | } | 634 | } |
| 553 | 635 | ||
| 554 | /* We keep several page tables. This is a simple routine to find the page | 636 | /* |
| 637 | * 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 | 638 | * table (if any) corresponding to this top-level address the Guest has given |
| 556 | * us. */ | 639 | * us. |
| 640 | */ | ||
| 557 | static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) | 641 | static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) |
| 558 | { | 642 | { |
| 559 | unsigned int i; | 643 | unsigned int i; |
| @@ -563,9 +647,11 @@ static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) | |||
| 563 | return i; | 647 | return i; |
| 564 | } | 648 | } |
| 565 | 649 | ||
| 566 | /*H:435 And this is us, creating the new page directory. If we really do | 650 | /*H:435 |
| 651 | * 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 | 652 | * allocate a new one (and so the kernel parts are not there), we set |
| 568 | * blank_pgdir. */ | 653 | * blank_pgdir. |
| 654 | */ | ||
| 569 | static unsigned int new_pgdir(struct lg_cpu *cpu, | 655 | static unsigned int new_pgdir(struct lg_cpu *cpu, |
| 570 | unsigned long gpgdir, | 656 | unsigned long gpgdir, |
| 571 | int *blank_pgdir) | 657 | int *blank_pgdir) |
| @@ -575,8 +661,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
| 575 | pmd_t *pmd_table; | 661 | pmd_t *pmd_table; |
| 576 | #endif | 662 | #endif |
| 577 | 663 | ||
| 578 | /* We pick one entry at random to throw out. Choosing the Least | 664 | /* |
| 579 | * Recently Used might be better, but this is easy. */ | 665 | * We pick one entry at random to throw out. Choosing the Least |
| 666 | * Recently Used might be better, but this is easy. | ||
| 667 | */ | ||
| 580 | next = random32() % ARRAY_SIZE(cpu->lg->pgdirs); | 668 | next = random32() % ARRAY_SIZE(cpu->lg->pgdirs); |
| 581 | /* If it's never been allocated at all before, try now. */ | 669 | /* If it's never been allocated at all before, try now. */ |
| 582 | if (!cpu->lg->pgdirs[next].pgdir) { | 670 | if (!cpu->lg->pgdirs[next].pgdir) { |
| @@ -587,8 +675,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
| 587 | next = cpu->cpu_pgd; | 675 | next = cpu->cpu_pgd; |
| 588 | else { | 676 | else { |
| 589 | #ifdef CONFIG_X86_PAE | 677 | #ifdef CONFIG_X86_PAE |
| 590 | /* In PAE mode, allocate a pmd page and populate the | 678 | /* |
| 591 | * last pgd entry. */ | 679 | * In PAE mode, allocate a pmd page and populate the |
| 680 | * last pgd entry. | ||
| 681 | */ | ||
| 592 | pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL); | 682 | pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL); |
| 593 | if (!pmd_table) { | 683 | if (!pmd_table) { |
| 594 | free_page((long)cpu->lg->pgdirs[next].pgdir); | 684 | free_page((long)cpu->lg->pgdirs[next].pgdir); |
| @@ -598,8 +688,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
| 598 | set_pgd(cpu->lg->pgdirs[next].pgdir + | 688 | set_pgd(cpu->lg->pgdirs[next].pgdir + |
| 599 | SWITCHER_PGD_INDEX, | 689 | SWITCHER_PGD_INDEX, |
| 600 | __pgd(__pa(pmd_table) | _PAGE_PRESENT)); | 690 | __pgd(__pa(pmd_table) | _PAGE_PRESENT)); |
| 601 | /* This is a blank page, so there are no kernel | 691 | /* |
| 602 | * mappings: caller must map the stack! */ | 692 | * This is a blank page, so there are no kernel |
| 693 | * mappings: caller must map the stack! | ||
| 694 | */ | ||
| 603 | *blank_pgdir = 1; | 695 | *blank_pgdir = 1; |
| 604 | } | 696 | } |
| 605 | #else | 697 | #else |
| @@ -615,19 +707,23 @@ static unsigned int new_pgdir(struct lg_cpu *cpu, | |||
| 615 | return next; | 707 | return next; |
| 616 | } | 708 | } |
| 617 | 709 | ||
| 618 | /*H:430 (iv) Switching page tables | 710 | /*H:430 |
| 711 | * (iv) Switching page tables | ||
| 619 | * | 712 | * |
| 620 | * Now we've seen all the page table setting and manipulation, let's see | 713 | * 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 | 714 | * what happens when the Guest changes page tables (ie. changes the top-level |
| 622 | * pgdir). This occurs on almost every context switch. */ | 715 | * pgdir). This occurs on almost every context switch. |
| 716 | */ | ||
| 623 | void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) | 717 | void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) |
| 624 | { | 718 | { |
| 625 | int newpgdir, repin = 0; | 719 | int newpgdir, repin = 0; |
| 626 | 720 | ||
| 627 | /* Look to see if we have this one already. */ | 721 | /* Look to see if we have this one already. */ |
| 628 | newpgdir = find_pgdir(cpu->lg, pgtable); | 722 | newpgdir = find_pgdir(cpu->lg, pgtable); |
| 629 | /* If not, we allocate or mug an existing one: if it's a fresh one, | 723 | /* |
| 630 | * repin gets set to 1. */ | 724 | * If not, we allocate or mug an existing one: if it's a fresh one, |
| 725 | * repin gets set to 1. | ||
| 726 | */ | ||
| 631 | if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs)) | 727 | if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs)) |
| 632 | newpgdir = new_pgdir(cpu, pgtable, &repin); | 728 | newpgdir = new_pgdir(cpu, pgtable, &repin); |
| 633 | /* Change the current pgd index to the new one. */ | 729 | /* Change the current pgd index to the new one. */ |
| @@ -637,9 +733,11 @@ void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) | |||
| 637 | pin_stack_pages(cpu); | 733 | pin_stack_pages(cpu); |
| 638 | } | 734 | } |
| 639 | 735 | ||
| 640 | /*H:470 Finally, a routine which throws away everything: all PGD entries in all | 736 | /*H:470 |
| 737 | * 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 | 738 | * the shadow page tables, including the Guest's kernel mappings. This is used |
| 642 | * when we destroy the Guest. */ | 739 | * when we destroy the Guest. |
| 740 | */ | ||
| 643 | static void release_all_pagetables(struct lguest *lg) | 741 | static void release_all_pagetables(struct lguest *lg) |
| 644 | { | 742 | { |
| 645 | unsigned int i, j; | 743 | unsigned int i, j; |
| @@ -656,8 +754,10 @@ static void release_all_pagetables(struct lguest *lg) | |||
| 656 | spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX; | 754 | spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX; |
| 657 | pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); | 755 | pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); |
| 658 | 756 | ||
| 659 | /* And release the pmd entries of that pmd page, | 757 | /* |
| 660 | * except for the switcher pmd. */ | 758 | * And release the pmd entries of that pmd page, |
| 759 | * except for the switcher pmd. | ||
| 760 | */ | ||
| 661 | for (k = 0; k < SWITCHER_PMD_INDEX; k++) | 761 | for (k = 0; k < SWITCHER_PMD_INDEX; k++) |
| 662 | release_pmd(&pmdpage[k]); | 762 | release_pmd(&pmdpage[k]); |
| 663 | #endif | 763 | #endif |
| @@ -667,10 +767,12 @@ static void release_all_pagetables(struct lguest *lg) | |||
| 667 | } | 767 | } |
| 668 | } | 768 | } |
| 669 | 769 | ||
| 670 | /* We also throw away everything when a Guest tells us it's changed a kernel | 770 | /* |
| 771 | * 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 | 772 | * 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 | 773 | * throw them all away. This traps the Guest in amber for a while as |
| 673 | * everything faults back in, but it's rare. */ | 774 | * everything faults back in, but it's rare. |
| 775 | */ | ||
| 674 | void guest_pagetable_clear_all(struct lg_cpu *cpu) | 776 | void guest_pagetable_clear_all(struct lg_cpu *cpu) |
| 675 | { | 777 | { |
| 676 | release_all_pagetables(cpu->lg); | 778 | release_all_pagetables(cpu->lg); |
| @@ -678,15 +780,19 @@ void guest_pagetable_clear_all(struct lg_cpu *cpu) | |||
| 678 | pin_stack_pages(cpu); | 780 | pin_stack_pages(cpu); |
| 679 | } | 781 | } |
| 680 | /*:*/ | 782 | /*:*/ |
| 681 | /*M:009 Since we throw away all mappings when a kernel mapping changes, our | 783 | |
| 784 | /*M:009 | ||
| 785 | * Since we throw away all mappings when a kernel mapping changes, our | ||
| 682 | * performance sucks for guests using highmem. In fact, a guest with | 786 | * 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 | 787 | * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is |
| 684 | * usually slower than a Guest with less memory. | 788 | * usually slower than a Guest with less memory. |
| 685 | * | 789 | * |
| 686 | * This, of course, cannot be fixed. It would take some kind of... well, I | 790 | * 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. :*/ | 791 | * don't know, but the term "puissant code-fu" comes to mind. |
| 792 | :*/ | ||
| 688 | 793 | ||
| 689 | /*H:420 This is the routine which actually sets the page table entry for then | 794 | /*H:420 |
| 795 | * This is the routine which actually sets the page table entry for then | ||
| 690 | * "idx"'th shadow page table. | 796 | * "idx"'th shadow page table. |
| 691 | * | 797 | * |
| 692 | * Normally, we can just throw out the old entry and replace it with 0: if they | 798 | * Normally, we can just throw out the old entry and replace it with 0: if they |
| @@ -715,31 +821,36 @@ static void do_set_pte(struct lg_cpu *cpu, int idx, | |||
| 715 | spmd = spmd_addr(cpu, *spgd, vaddr); | 821 | spmd = spmd_addr(cpu, *spgd, vaddr); |
| 716 | if (pmd_flags(*spmd) & _PAGE_PRESENT) { | 822 | if (pmd_flags(*spmd) & _PAGE_PRESENT) { |
| 717 | #endif | 823 | #endif |
| 718 | /* Otherwise, we start by releasing | 824 | /* Otherwise, start by releasing the existing entry. */ |
| 719 | * the existing entry. */ | ||
| 720 | pte_t *spte = spte_addr(cpu, *spgd, vaddr); | 825 | pte_t *spte = spte_addr(cpu, *spgd, vaddr); |
| 721 | release_pte(*spte); | 826 | release_pte(*spte); |
| 722 | 827 | ||
| 723 | /* If they're setting this entry as dirty or accessed, | 828 | /* |
| 724 | * we might as well put that entry they've given us | 829 | * If they're setting this entry as dirty or accessed, |
| 725 | * in now. This shaves 10% off a | 830 | * we might as well put that entry they've given us in |
| 726 | * copy-on-write micro-benchmark. */ | 831 | * now. This shaves 10% off a copy-on-write |
| 832 | * micro-benchmark. | ||
| 833 | */ | ||
| 727 | if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) { | 834 | if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) { |
| 728 | check_gpte(cpu, gpte); | 835 | check_gpte(cpu, gpte); |
| 729 | native_set_pte(spte, | 836 | native_set_pte(spte, |
| 730 | gpte_to_spte(cpu, gpte, | 837 | gpte_to_spte(cpu, gpte, |
| 731 | pte_flags(gpte) & _PAGE_DIRTY)); | 838 | pte_flags(gpte) & _PAGE_DIRTY)); |
| 732 | } else | 839 | } else { |
| 733 | /* Otherwise kill it and we can demand_page() | 840 | /* |
| 734 | * it in later. */ | 841 | * Otherwise kill it and we can demand_page() |
| 842 | * it in later. | ||
| 843 | */ | ||
| 735 | native_set_pte(spte, __pte(0)); | 844 | native_set_pte(spte, __pte(0)); |
| 845 | } | ||
| 736 | #ifdef CONFIG_X86_PAE | 846 | #ifdef CONFIG_X86_PAE |
| 737 | } | 847 | } |
| 738 | #endif | 848 | #endif |
| 739 | } | 849 | } |
| 740 | } | 850 | } |
| 741 | 851 | ||
| 742 | /*H:410 Updating a PTE entry is a little trickier. | 852 | /*H:410 |
| 853 | * Updating a PTE entry is a little trickier. | ||
| 743 | * | 854 | * |
| 744 | * We keep track of several different page tables (the Guest uses one for each | 855 | * 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 | 856 | * process, so it makes sense to cache at least a few). Each of these have |
| @@ -748,12 +859,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. | 859 | * all the page tables, not just the current one. This is rare. |
| 749 | * | 860 | * |
| 750 | * The benefit is that when we have to track a new page table, we can keep all | 861 | * 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. */ | 862 | * the kernel mappings. This speeds up context switch immensely. |
| 863 | */ | ||
| 752 | void guest_set_pte(struct lg_cpu *cpu, | 864 | void guest_set_pte(struct lg_cpu *cpu, |
| 753 | unsigned long gpgdir, unsigned long vaddr, pte_t gpte) | 865 | unsigned long gpgdir, unsigned long vaddr, pte_t gpte) |
| 754 | { | 866 | { |
| 755 | /* Kernel mappings must be changed on all top levels. Slow, but doesn't | 867 | /* |
| 756 | * happen often. */ | 868 | * Kernel mappings must be changed on all top levels. Slow, but doesn't |
| 869 | * happen often. | ||
| 870 | */ | ||
| 757 | if (vaddr >= cpu->lg->kernel_address) { | 871 | if (vaddr >= cpu->lg->kernel_address) { |
| 758 | unsigned int i; | 872 | unsigned int i; |
| 759 | for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++) | 873 | for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++) |
| @@ -795,19 +909,25 @@ void guest_set_pgd(struct lguest *lg, unsigned long gpgdir, u32 idx) | |||
| 795 | /* ... throw it away. */ | 909 | /* ... throw it away. */ |
| 796 | release_pgd(lg->pgdirs[pgdir].pgdir + idx); | 910 | release_pgd(lg->pgdirs[pgdir].pgdir + idx); |
| 797 | } | 911 | } |
| 912 | |||
| 798 | #ifdef CONFIG_X86_PAE | 913 | #ifdef CONFIG_X86_PAE |
| 914 | /* For setting a mid-level, we just throw everything away. It's easy. */ | ||
| 799 | void guest_set_pmd(struct lguest *lg, unsigned long pmdp, u32 idx) | 915 | void guest_set_pmd(struct lguest *lg, unsigned long pmdp, u32 idx) |
| 800 | { | 916 | { |
| 801 | guest_pagetable_clear_all(&lg->cpus[0]); | 917 | guest_pagetable_clear_all(&lg->cpus[0]); |
| 802 | } | 918 | } |
| 803 | #endif | 919 | #endif |
| 804 | 920 | ||
| 805 | /* Once we know how much memory we have we can construct simple identity | 921 | /*H:505 |
| 806 | * (which set virtual == physical) and linear mappings | 922 | * To get through boot, we construct simple identity page mappings (which |
| 807 | * which will get the Guest far enough into the boot to create its own. | 923 | * set virtual == physical) and linear mappings which will get the Guest far |
| 924 | * enough into the boot to create its own. The linear mapping means we | ||
| 925 | * simplify the Guest boot, but it makes assumptions about their PAGE_OFFSET, | ||
| 926 | * as you'll see. | ||
| 808 | * | 927 | * |
| 809 | * We lay them out of the way, just below the initrd (which is why we need to | 928 | * We lay them out of the way, just below the initrd (which is why we need to |
| 810 | * know its size here). */ | 929 | * know its size here). |
| 930 | */ | ||
| 811 | static unsigned long setup_pagetables(struct lguest *lg, | 931 | static unsigned long setup_pagetables(struct lguest *lg, |
| 812 | unsigned long mem, | 932 | unsigned long mem, |
| 813 | unsigned long initrd_size) | 933 | unsigned long initrd_size) |
| @@ -825,8 +945,10 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
| 825 | unsigned int phys_linear; | 945 | unsigned int phys_linear; |
| 826 | #endif | 946 | #endif |
| 827 | 947 | ||
| 828 | /* We have mapped_pages frames to map, so we need | 948 | /* |
| 829 | * linear_pages page tables to map them. */ | 949 | * We have mapped_pages frames to map, so we need linear_pages page |
| 950 | * tables to map them. | ||
| 951 | */ | ||
| 830 | mapped_pages = mem / PAGE_SIZE; | 952 | mapped_pages = mem / PAGE_SIZE; |
| 831 | linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE; | 953 | linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE; |
| 832 | 954 | ||
| @@ -837,10 +959,16 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
| 837 | linear = (void *)pgdir - linear_pages * PAGE_SIZE; | 959 | linear = (void *)pgdir - linear_pages * PAGE_SIZE; |
| 838 | 960 | ||
| 839 | #ifdef CONFIG_X86_PAE | 961 | #ifdef CONFIG_X86_PAE |
| 962 | /* | ||
| 963 | * And the single mid page goes below that. We only use one, but | ||
| 964 | * that's enough to map 1G, which definitely gets us through boot. | ||
| 965 | */ | ||
| 840 | pmds = (void *)linear - PAGE_SIZE; | 966 | pmds = (void *)linear - PAGE_SIZE; |
| 841 | #endif | 967 | #endif |
| 842 | /* Linear mapping is easy: put every page's address into the | 968 | /* |
| 843 | * mapping in order. */ | 969 | * Linear mapping is easy: put every page's address into the |
| 970 | * mapping in order. | ||
| 971 | */ | ||
| 844 | for (i = 0; i < mapped_pages; i++) { | 972 | for (i = 0; i < mapped_pages; i++) { |
| 845 | pte_t pte; | 973 | pte_t pte; |
| 846 | pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER)); | 974 | pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER)); |
| @@ -848,11 +976,14 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
| 848 | return -EFAULT; | 976 | return -EFAULT; |
| 849 | } | 977 | } |
| 850 | 978 | ||
| 851 | /* The top level points to the linear page table pages above. | ||
| 852 | * We setup the identity and linear mappings here. */ | ||
| 853 | #ifdef CONFIG_X86_PAE | 979 | #ifdef CONFIG_X86_PAE |
| 980 | /* | ||
| 981 | * Make the Guest PMD entries point to the corresponding place in the | ||
| 982 | * linear mapping (up to one page worth of PMD). | ||
| 983 | */ | ||
| 854 | for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD; | 984 | for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD; |
| 855 | i += PTRS_PER_PTE, j++) { | 985 | i += PTRS_PER_PTE, j++) { |
| 986 | /* FIXME: native_set_pmd is overkill here. */ | ||
| 856 | native_set_pmd(&pmd, __pmd(((unsigned long)(linear + i) | 987 | native_set_pmd(&pmd, __pmd(((unsigned long)(linear + i) |
| 857 | - mem_base) | _PAGE_PRESENT | _PAGE_RW | _PAGE_USER)); | 988 | - mem_base) | _PAGE_PRESENT | _PAGE_RW | _PAGE_USER)); |
| 858 | 989 | ||
| @@ -860,18 +991,36 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
| 860 | return -EFAULT; | 991 | return -EFAULT; |
| 861 | } | 992 | } |
| 862 | 993 | ||
| 994 | /* One PGD entry, pointing to that PMD page. */ | ||
| 863 | set_pgd(&pgd, __pgd(((u32)pmds - mem_base) | _PAGE_PRESENT)); | 995 | set_pgd(&pgd, __pgd(((u32)pmds - mem_base) | _PAGE_PRESENT)); |
| 996 | /* Copy it in as the first PGD entry (ie. addresses 0-1G). */ | ||
| 864 | if (copy_to_user(&pgdir[0], &pgd, sizeof(pgd)) != 0) | 997 | if (copy_to_user(&pgdir[0], &pgd, sizeof(pgd)) != 0) |
| 865 | return -EFAULT; | 998 | return -EFAULT; |
| 999 | /* | ||
| 1000 | * And the third PGD entry (ie. addresses 3G-4G). | ||
| 1001 | * | ||
| 1002 | * FIXME: This assumes that PAGE_OFFSET for the Guest is 0xC0000000. | ||
| 1003 | */ | ||
| 866 | if (copy_to_user(&pgdir[3], &pgd, sizeof(pgd)) != 0) | 1004 | if (copy_to_user(&pgdir[3], &pgd, sizeof(pgd)) != 0) |
| 867 | return -EFAULT; | 1005 | return -EFAULT; |
| 868 | #else | 1006 | #else |
| 1007 | /* | ||
| 1008 | * The top level points to the linear page table pages above. | ||
| 1009 | * We setup the identity and linear mappings here. | ||
| 1010 | */ | ||
| 869 | phys_linear = (unsigned long)linear - mem_base; | 1011 | phys_linear = (unsigned long)linear - mem_base; |
| 870 | for (i = 0; i < mapped_pages; i += PTRS_PER_PTE) { | 1012 | for (i = 0; i < mapped_pages; i += PTRS_PER_PTE) { |
| 871 | pgd_t pgd; | 1013 | pgd_t pgd; |
| 1014 | /* | ||
| 1015 | * Create a PGD entry which points to the right part of the | ||
| 1016 | * linear PTE pages. | ||
| 1017 | */ | ||
| 872 | pgd = __pgd((phys_linear + i * sizeof(pte_t)) | | 1018 | pgd = __pgd((phys_linear + i * sizeof(pte_t)) | |
| 873 | (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER)); | 1019 | (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER)); |
| 874 | 1020 | ||
| 1021 | /* | ||
| 1022 | * Copy it into the PGD page at 0 and PAGE_OFFSET. | ||
| 1023 | */ | ||
| 875 | if (copy_to_user(&pgdir[i / PTRS_PER_PTE], &pgd, sizeof(pgd)) | 1024 | if (copy_to_user(&pgdir[i / PTRS_PER_PTE], &pgd, sizeof(pgd)) |
| 876 | || copy_to_user(&pgdir[pgd_index(PAGE_OFFSET) | 1025 | || copy_to_user(&pgdir[pgd_index(PAGE_OFFSET) |
| 877 | + i / PTRS_PER_PTE], | 1026 | + i / PTRS_PER_PTE], |
| @@ -880,15 +1029,19 @@ static unsigned long setup_pagetables(struct lguest *lg, | |||
| 880 | } | 1029 | } |
| 881 | #endif | 1030 | #endif |
| 882 | 1031 | ||
| 883 | /* We return the top level (guest-physical) address: remember where | 1032 | /* |
| 884 | * this is. */ | 1033 | * We return the top level (guest-physical) address: we remember where |
| 1034 | * this is to write it into lguest_data when the Guest initializes. | ||
| 1035 | */ | ||
| 885 | return (unsigned long)pgdir - mem_base; | 1036 | return (unsigned long)pgdir - mem_base; |
| 886 | } | 1037 | } |
| 887 | 1038 | ||
| 888 | /*H:500 (vii) Setting up the page tables initially. | 1039 | /*H:500 |
| 1040 | * (vii) Setting up the page tables initially. | ||
| 889 | * | 1041 | * |
| 890 | * When a Guest is first created, the Launcher tells us where the toplevel of | 1042 | * 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: */ | 1043 | * its first page table is. We set some things up here: |
| 1044 | */ | ||
| 892 | int init_guest_pagetable(struct lguest *lg) | 1045 | int init_guest_pagetable(struct lguest *lg) |
| 893 | { | 1046 | { |
| 894 | u64 mem; | 1047 | u64 mem; |
| @@ -898,21 +1051,27 @@ int init_guest_pagetable(struct lguest *lg) | |||
| 898 | pgd_t *pgd; | 1051 | pgd_t *pgd; |
| 899 | pmd_t *pmd_table; | 1052 | pmd_t *pmd_table; |
| 900 | #endif | 1053 | #endif |
| 901 | /* Get the Guest memory size and the ramdisk size from the boot header | 1054 | /* |
| 902 | * located at lg->mem_base (Guest address 0). */ | 1055 | * Get the Guest memory size and the ramdisk size from the boot header |
| 1056 | * located at lg->mem_base (Guest address 0). | ||
| 1057 | */ | ||
| 903 | if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem)) | 1058 | if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem)) |
| 904 | || get_user(initrd_size, &boot->hdr.ramdisk_size)) | 1059 | || get_user(initrd_size, &boot->hdr.ramdisk_size)) |
| 905 | return -EFAULT; | 1060 | return -EFAULT; |
| 906 | 1061 | ||
| 907 | /* We start on the first shadow page table, and give it a blank PGD | 1062 | /* |
| 908 | * page. */ | 1063 | * We start on the first shadow page table, and give it a blank PGD |
| 1064 | * page. | ||
| 1065 | */ | ||
| 909 | lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size); | 1066 | lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size); |
| 910 | if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir)) | 1067 | if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir)) |
| 911 | return lg->pgdirs[0].gpgdir; | 1068 | return lg->pgdirs[0].gpgdir; |
| 912 | lg->pgdirs[0].pgdir = (pgd_t *)get_zeroed_page(GFP_KERNEL); | 1069 | lg->pgdirs[0].pgdir = (pgd_t *)get_zeroed_page(GFP_KERNEL); |
| 913 | if (!lg->pgdirs[0].pgdir) | 1070 | if (!lg->pgdirs[0].pgdir) |
| 914 | return -ENOMEM; | 1071 | return -ENOMEM; |
| 1072 | |||
| 915 | #ifdef CONFIG_X86_PAE | 1073 | #ifdef CONFIG_X86_PAE |
| 1074 | /* For PAE, we also create the initial mid-level. */ | ||
| 916 | pgd = lg->pgdirs[0].pgdir; | 1075 | pgd = lg->pgdirs[0].pgdir; |
| 917 | pmd_table = (pmd_t *) get_zeroed_page(GFP_KERNEL); | 1076 | pmd_table = (pmd_t *) get_zeroed_page(GFP_KERNEL); |
| 918 | if (!pmd_table) | 1077 | if (!pmd_table) |
| @@ -921,27 +1080,33 @@ int init_guest_pagetable(struct lguest *lg) | |||
| 921 | set_pgd(pgd + SWITCHER_PGD_INDEX, | 1080 | set_pgd(pgd + SWITCHER_PGD_INDEX, |
| 922 | __pgd(__pa(pmd_table) | _PAGE_PRESENT)); | 1081 | __pgd(__pa(pmd_table) | _PAGE_PRESENT)); |
| 923 | #endif | 1082 | #endif |
| 1083 | |||
| 1084 | /* This is the current page table. */ | ||
| 924 | lg->cpus[0].cpu_pgd = 0; | 1085 | lg->cpus[0].cpu_pgd = 0; |
| 925 | return 0; | 1086 | return 0; |
| 926 | } | 1087 | } |
| 927 | 1088 | ||
| 928 | /* When the Guest calls LHCALL_LGUEST_INIT we do more setup. */ | 1089 | /*H:508 When the Guest calls LHCALL_LGUEST_INIT we do more setup. */ |
| 929 | void page_table_guest_data_init(struct lg_cpu *cpu) | 1090 | void page_table_guest_data_init(struct lg_cpu *cpu) |
| 930 | { | 1091 | { |
| 931 | /* We get the kernel address: above this is all kernel memory. */ | 1092 | /* We get the kernel address: above this is all kernel memory. */ |
| 932 | if (get_user(cpu->lg->kernel_address, | 1093 | if (get_user(cpu->lg->kernel_address, |
| 933 | &cpu->lg->lguest_data->kernel_address) | 1094 | &cpu->lg->lguest_data->kernel_address) |
| 934 | /* We tell the Guest that it can't use the top 2 or 4 MB | 1095 | /* |
| 935 | * of virtual addresses used by the Switcher. */ | 1096 | * We tell the Guest that it can't use the top 2 or 4 MB |
| 1097 | * of virtual addresses used by the Switcher. | ||
| 1098 | */ | ||
| 936 | || put_user(RESERVE_MEM * 1024 * 1024, | 1099 | || put_user(RESERVE_MEM * 1024 * 1024, |
| 937 | &cpu->lg->lguest_data->reserve_mem) | 1100 | &cpu->lg->lguest_data->reserve_mem) |
| 938 | || put_user(cpu->lg->pgdirs[0].gpgdir, | 1101 | || put_user(cpu->lg->pgdirs[0].gpgdir, |
| 939 | &cpu->lg->lguest_data->pgdir)) | 1102 | &cpu->lg->lguest_data->pgdir)) |
| 940 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); | 1103 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); |
| 941 | 1104 | ||
| 942 | /* In flush_user_mappings() we loop from 0 to | 1105 | /* |
| 1106 | * In flush_user_mappings() we loop from 0 to | ||
| 943 | * "pgd_index(lg->kernel_address)". This assumes it won't hit the | 1107 | * "pgd_index(lg->kernel_address)". This assumes it won't hit the |
| 944 | * Switcher mappings, so check that now. */ | 1108 | * Switcher mappings, so check that now. |
| 1109 | */ | ||
| 945 | #ifdef CONFIG_X86_PAE | 1110 | #ifdef CONFIG_X86_PAE |
| 946 | if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX && | 1111 | if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX && |
| 947 | pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX) | 1112 | pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX) |
| @@ -964,12 +1129,14 @@ void free_guest_pagetable(struct lguest *lg) | |||
| 964 | free_page((long)lg->pgdirs[i].pgdir); | 1129 | free_page((long)lg->pgdirs[i].pgdir); |
| 965 | } | 1130 | } |
| 966 | 1131 | ||
| 967 | /*H:480 (vi) Mapping the Switcher when the Guest is about to run. | 1132 | /*H:480 |
| 1133 | * (vi) Mapping the Switcher when the Guest is about to run. | ||
| 968 | * | 1134 | * |
| 969 | * The Switcher and the two pages for this CPU need to be visible in the | 1135 | * 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 | 1136 | * 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 | 1137 | * 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. */ | 1138 | * Guest is about to run on this CPU. |
| 1139 | */ | ||
| 973 | void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) | 1140 | void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) |
| 974 | { | 1141 | { |
| 975 | pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages); | 1142 | pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages); |
| @@ -980,30 +1147,38 @@ void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) | |||
| 980 | pmd_t switcher_pmd; | 1147 | pmd_t switcher_pmd; |
| 981 | pmd_t *pmd_table; | 1148 | pmd_t *pmd_table; |
| 982 | 1149 | ||
| 1150 | /* FIXME: native_set_pmd is overkill here. */ | ||
| 983 | native_set_pmd(&switcher_pmd, pfn_pmd(__pa(switcher_pte_page) >> | 1151 | native_set_pmd(&switcher_pmd, pfn_pmd(__pa(switcher_pte_page) >> |
| 984 | PAGE_SHIFT, PAGE_KERNEL_EXEC)); | 1152 | PAGE_SHIFT, PAGE_KERNEL_EXEC)); |
| 985 | 1153 | ||
| 1154 | /* Figure out where the pmd page is, by reading the PGD, and converting | ||
| 1155 | * it to a virtual address. */ | ||
| 986 | pmd_table = __va(pgd_pfn(cpu->lg-> | 1156 | pmd_table = __va(pgd_pfn(cpu->lg-> |
| 987 | pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX]) | 1157 | pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX]) |
| 988 | << PAGE_SHIFT); | 1158 | << PAGE_SHIFT); |
| 1159 | /* Now write it into the shadow page table. */ | ||
| 989 | native_set_pmd(&pmd_table[SWITCHER_PMD_INDEX], switcher_pmd); | 1160 | native_set_pmd(&pmd_table[SWITCHER_PMD_INDEX], switcher_pmd); |
| 990 | #else | 1161 | #else |
| 991 | pgd_t switcher_pgd; | 1162 | pgd_t switcher_pgd; |
| 992 | 1163 | ||
| 993 | /* Make the last PGD entry for this Guest point to the Switcher's PTE | 1164 | /* |
| 994 | * page for this CPU (with appropriate flags). */ | 1165 | * Make the last PGD entry for this Guest point to the Switcher's PTE |
| 1166 | * page for this CPU (with appropriate flags). | ||
| 1167 | */ | ||
| 995 | switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC); | 1168 | switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC); |
| 996 | 1169 | ||
| 997 | cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd; | 1170 | cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd; |
| 998 | 1171 | ||
| 999 | #endif | 1172 | #endif |
| 1000 | /* We also change the Switcher PTE page. When we're running the Guest, | 1173 | /* |
| 1174 | * 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 | 1175 | * 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 | 1176 | * 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 | 1177 | * 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 | 1178 | * 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 | 1179 | * page is already mapped there, we don't have to copy them out |
| 1006 | * again. */ | 1180 | * again. |
| 1181 | */ | ||
| 1007 | pfn = __pa(cpu->regs_page) >> PAGE_SHIFT; | 1182 | pfn = __pa(cpu->regs_page) >> PAGE_SHIFT; |
| 1008 | native_set_pte(®s_pte, pfn_pte(pfn, PAGE_KERNEL)); | 1183 | native_set_pte(®s_pte, pfn_pte(pfn, PAGE_KERNEL)); |
| 1009 | native_set_pte(&switcher_pte_page[pte_index((unsigned long)pages)], | 1184 | native_set_pte(&switcher_pte_page[pte_index((unsigned long)pages)], |
| @@ -1019,10 +1194,12 @@ static void free_switcher_pte_pages(void) | |||
| 1019 | free_page((long)switcher_pte_page(i)); | 1194 | free_page((long)switcher_pte_page(i)); |
| 1020 | } | 1195 | } |
| 1021 | 1196 | ||
| 1022 | /*H:520 Setting up the Switcher PTE page for given CPU is fairly easy, given | 1197 | /*H:520 |
| 1198 | * 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. | 1199 | * the CPU number and the "struct page"s for the Switcher code itself. |
| 1024 | * | 1200 | * |
| 1025 | * Currently the Switcher is less than a page long, so "pages" is always 1. */ | 1201 | * Currently the Switcher is less than a page long, so "pages" is always 1. |
| 1202 | */ | ||
| 1026 | static __init void populate_switcher_pte_page(unsigned int cpu, | 1203 | static __init void populate_switcher_pte_page(unsigned int cpu, |
| 1027 | struct page *switcher_page[], | 1204 | struct page *switcher_page[], |
| 1028 | unsigned int pages) | 1205 | unsigned int pages) |
| @@ -1043,13 +1220,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]), | 1220 | native_set_pte(&pte[i], pfn_pte(page_to_pfn(switcher_page[i]), |
| 1044 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW))); | 1221 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW))); |
| 1045 | 1222 | ||
| 1046 | /* The second page contains the "struct lguest_ro_state", and is | 1223 | /* |
| 1047 | * read-only. */ | 1224 | * The second page contains the "struct lguest_ro_state", and is |
| 1225 | * read-only. | ||
| 1226 | */ | ||
| 1048 | native_set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]), | 1227 | native_set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]), |
| 1049 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED))); | 1228 | __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED))); |
| 1050 | } | 1229 | } |
| 1051 | 1230 | ||
| 1052 | /* We've made it through the page table code. Perhaps our tired brains are | 1231 | /* |
| 1232 | * 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. | 1233 | * still processing the details, or perhaps we're simply glad it's over. |
| 1054 | * | 1234 | * |
| 1055 | * If nothing else, note that all this complexity in juggling shadow page tables | 1235 | * If nothing else, note that all this complexity in juggling shadow page tables |
| @@ -1058,10 +1238,13 @@ static __init void populate_switcher_pte_page(unsigned int cpu, | |||
| 1058 | * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD | 1238 | * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD |
| 1059 | * have implemented shadow page table support directly into hardware. | 1239 | * have implemented shadow page table support directly into hardware. |
| 1060 | * | 1240 | * |
| 1061 | * There is just one file remaining in the Host. */ | 1241 | * There is just one file remaining in the Host. |
| 1242 | */ | ||
| 1062 | 1243 | ||
| 1063 | /*H:510 At boot or module load time, init_pagetables() allocates and populates | 1244 | /*H:510 |
| 1064 | * the Switcher PTE page for each CPU. */ | 1245 | * At boot or module load time, init_pagetables() allocates and populates |
| 1246 | * the Switcher PTE page for each CPU. | ||
| 1247 | */ | ||
| 1065 | __init int init_pagetables(struct page **switcher_page, unsigned int pages) | 1248 | __init int init_pagetables(struct page **switcher_page, unsigned int pages) |
| 1066 | { | 1249 | { |
| 1067 | unsigned int i; | 1250 | 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..6ae388849a3b 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,97 +135,126 @@ 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 |
| 165 | * things when we exit to Launcher userspace, but that's fairly easy. | 188 | * things when we exit to Launcher userspace, but that's fairly easy. |
| 166 | * | 189 | * |
| 167 | * We could also try using this hooks for PGE, but that might be too expensive. | 190 | * We could also try using these 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..40634b0db9f7 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 | * Host and Guest to do the low-level Guest<->Host switch. It is as simple as | 2 | * This is the Switcher: code which sits at 0xFFC00000 (or 0xFFE00000) astride |
| 3 | * it can be made, but it's naturally very specific to x86. | 3 | * both the Host and Guest to do the low-level Guest<->Host switch. It is as |
| 4 | * simple as 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! |
