aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/lguest
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/lguest')
-rw-r--r--drivers/lguest/core.c114
-rw-r--r--drivers/lguest/hypercalls.c141
-rw-r--r--drivers/lguest/interrupts_and_traps.c288
-rw-r--r--drivers/lguest/lg.h23
-rw-r--r--drivers/lguest/lguest_device.c150
-rw-r--r--drivers/lguest/lguest_user.c137
-rw-r--r--drivers/lguest/page_tables.c427
-rw-r--r--drivers/lguest/segments.c106
-rw-r--r--drivers/lguest/x86/core.c372
-rw-r--r--drivers/lguest/x86/switcher_32.S18
10 files changed, 1168 insertions, 608 deletions
diff --git a/drivers/lguest/core.c b/drivers/lguest/core.c
index a6974e9b8ebf..cd058bc903ff 100644
--- a/drivers/lguest/core.c
+++ b/drivers/lguest/core.c
@@ -1,6 +1,8 @@
1/*P:400 This contains run_guest() which actually calls into the Host<->Guest 1/*P:400
2 * This contains run_guest() which actually calls into the Host<->Guest
2 * Switcher and analyzes the return, such as determining if the Guest wants the 3 * Switcher and analyzes the return, such as determining if the Guest wants the
3 * Host to do something. This file also contains useful helper routines. :*/ 4 * Host to do something. This file also contains useful helper routines.
5:*/
4#include <linux/module.h> 6#include <linux/module.h>
5#include <linux/stringify.h> 7#include <linux/stringify.h>
6#include <linux/stddef.h> 8#include <linux/stddef.h>
@@ -24,7 +26,8 @@ static struct page **switcher_page;
24/* This One Big lock protects all inter-guest data structures. */ 26/* This One Big lock protects all inter-guest data structures. */
25DEFINE_MUTEX(lguest_lock); 27DEFINE_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 */
37static __init int map_switcher(void) 41static __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. */
129static void unmap_switcher(void) 144static 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 */
155bool lguest_address_ok(const struct lguest *lg, 171bool 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 */
164void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes) 182void __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 */
187int run_guest(struct lg_cpu *cpu, unsigned long __user *user) 207int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
188{ 208{
189 /* We stop running once the Guest is dead. */ 209 /* We stop running once the Guest is dead. */
@@ -195,8 +215,10 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
195 if (cpu->hcall) 215 if (cpu->hcall)
196 do_hypercalls(cpu); 216 do_hypercalls(cpu);
197 217
198 /* It's possible the Guest did a NOTIFY hypercall to the 218 /*
199 * Launcher, in which case we return from the read() now. */ 219 * It's possible the Guest did a NOTIFY hypercall to the
220 * Launcher, in which case we return from the read() now.
221 */
200 if (cpu->pending_notify) { 222 if (cpu->pending_notify) {
201 if (!send_notify_to_eventfd(cpu)) { 223 if (!send_notify_to_eventfd(cpu)) {
202 if (put_user(cpu->pending_notify, user)) 224 if (put_user(cpu->pending_notify, user))
@@ -209,29 +231,39 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
209 if (signal_pending(current)) 231 if (signal_pending(current))
210 return -ERESTARTSYS; 232 return -ERESTARTSYS;
211 233
212 /* Check if there are any interrupts which can be delivered now: 234 /*
235 * Check if there are any interrupts which can be delivered now:
213 * if so, this sets up the hander to be executed when we next 236 * if so, this sets up the hander to be executed when we next
214 * run the Guest. */ 237 * run the Guest.
238 */
215 irq = interrupt_pending(cpu, &more); 239 irq = interrupt_pending(cpu, &more);
216 if (irq < LGUEST_IRQS) 240 if (irq < LGUEST_IRQS)
217 try_deliver_interrupt(cpu, irq, more); 241 try_deliver_interrupt(cpu, irq, more);
218 242
219 /* All long-lived kernel loops need to check with this horrible 243 /*
244 * All long-lived kernel loops need to check with this horrible
220 * thing called the freezer. If the Host is trying to suspend, 245 * thing called the freezer. If the Host is trying to suspend,
221 * it stops us. */ 246 * it stops us.
247 */
222 try_to_freeze(); 248 try_to_freeze();
223 249
224 /* Just make absolutely sure the Guest is still alive. One of 250 /*
225 * those hypercalls could have been fatal, for example. */ 251 * Just make absolutely sure the Guest is still alive. One of
252 * those hypercalls could have been fatal, for example.
253 */
226 if (cpu->lg->dead) 254 if (cpu->lg->dead)
227 break; 255 break;
228 256
229 /* If the Guest asked to be stopped, we sleep. The Guest's 257 /*
230 * clock timer will wake us. */ 258 * If the Guest asked to be stopped, we sleep. The Guest's
259 * clock timer will wake us.
260 */
231 if (cpu->halted) { 261 if (cpu->halted) {
232 set_current_state(TASK_INTERRUPTIBLE); 262 set_current_state(TASK_INTERRUPTIBLE);
233 /* Just before we sleep, make sure no interrupt snuck in 263 /*
234 * which we should be doing. */ 264 * Just before we sleep, make sure no interrupt snuck in
265 * which we should be doing.
266 */
235 if (interrupt_pending(cpu, &more) < LGUEST_IRQS) 267 if (interrupt_pending(cpu, &more) < LGUEST_IRQS)
236 set_current_state(TASK_RUNNING); 268 set_current_state(TASK_RUNNING);
237 else 269 else
@@ -239,8 +271,10 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
239 continue; 271 continue;
240 } 272 }
241 273
242 /* OK, now we're ready to jump into the Guest. First we put up 274 /*
243 * the "Do Not Disturb" sign: */ 275 * OK, now we're ready to jump into the Guest. First we put up
276 * the "Do Not Disturb" sign:
277 */
244 local_irq_disable(); 278 local_irq_disable();
245 279
246 /* Actually run the Guest until something happens. */ 280 /* Actually run the Guest until something happens. */
@@ -327,8 +361,10 @@ static void __exit fini(void)
327} 361}
328/*:*/ 362/*:*/
329 363
330/* The Host side of lguest can be a module. This is a nice way for people to 364/*
331 * play with it. */ 365 * The Host side of lguest can be a module. This is a nice way for people to
366 * play with it.
367 */
332module_init(init); 368module_init(init);
333module_exit(fini); 369module_exit(fini);
334MODULE_LICENSE("GPL"); 370MODULE_LICENSE("GPL");
diff --git a/drivers/lguest/hypercalls.c b/drivers/lguest/hypercalls.c
index c29ffa19cb74..787ab4bc09f0 100644
--- a/drivers/lguest/hypercalls.c
+++ b/drivers/lguest/hypercalls.c
@@ -1,8 +1,10 @@
1/*P:500 Just as userspace programs request kernel operations through a system 1/*P:500
2 * Just as userspace programs request kernel operations through a system
2 * call, the Guest requests Host operations through a "hypercall". You might 3 * call, the Guest requests Host operations through a "hypercall". You might
3 * notice this nomenclature doesn't really follow any logic, but the name has 4 * notice this nomenclature doesn't really follow any logic, but the name has
4 * been around for long enough that we're stuck with it. As you'd expect, this 5 * been around for long enough that we're stuck with it. As you'd expect, this
5 * code is basically a one big switch statement. :*/ 6 * code is basically a one big switch statement.
7:*/
6 8
7/* Copyright (C) 2006 Rusty Russell IBM Corporation 9/* Copyright (C) 2006 Rusty Russell IBM Corporation
8 10
@@ -28,30 +30,41 @@
28#include <asm/pgtable.h> 30#include <asm/pgtable.h>
29#include "lg.h" 31#include "lg.h"
30 32
31/*H:120 This is the core hypercall routine: where the Guest gets what it wants. 33/*H:120
32 * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. */ 34 * This is the core hypercall routine: where the Guest gets what it wants.
35 * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both.
36 */
33static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) 37static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
34{ 38{
35 switch (args->arg0) { 39 switch (args->arg0) {
36 case LHCALL_FLUSH_ASYNC: 40 case LHCALL_FLUSH_ASYNC:
37 /* This call does nothing, except by breaking out of the Guest 41 /*
38 * it makes us process all the asynchronous hypercalls. */ 42 * This call does nothing, except by breaking out of the Guest
43 * it makes us process all the asynchronous hypercalls.
44 */
39 break; 45 break;
40 case LHCALL_SEND_INTERRUPTS: 46 case LHCALL_SEND_INTERRUPTS:
41 /* This call does nothing too, but by breaking out of the Guest 47 /*
42 * it makes us process any pending interrupts. */ 48 * This call does nothing too, but by breaking out of the Guest
49 * it makes us process any pending interrupts.
50 */
43 break; 51 break;
44 case LHCALL_LGUEST_INIT: 52 case LHCALL_LGUEST_INIT:
45 /* You can't get here unless you're already initialized. Don't 53 /*
46 * do that. */ 54 * You can't get here unless you're already initialized. Don't
55 * do that.
56 */
47 kill_guest(cpu, "already have lguest_data"); 57 kill_guest(cpu, "already have lguest_data");
48 break; 58 break;
49 case LHCALL_SHUTDOWN: { 59 case LHCALL_SHUTDOWN: {
50 /* Shutdown is such a trivial hypercall that we do it in four
51 * lines right here. */
52 char msg[128]; 60 char msg[128];
53 /* If the lgread fails, it will call kill_guest() itself; the 61 /*
54 * kill_guest() with the message will be ignored. */ 62 * Shutdown is such a trivial hypercall that we do it in four
63 * lines right here.
64 *
65 * If the lgread fails, it will call kill_guest() itself; the
66 * kill_guest() with the message will be ignored.
67 */
55 __lgread(cpu, msg, args->arg1, sizeof(msg)); 68 __lgread(cpu, msg, args->arg1, sizeof(msg));
56 msg[sizeof(msg)-1] = '\0'; 69 msg[sizeof(msg)-1] = '\0';
57 kill_guest(cpu, "CRASH: %s", msg); 70 kill_guest(cpu, "CRASH: %s", msg);
@@ -60,16 +73,17 @@ static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
60 break; 73 break;
61 } 74 }
62 case LHCALL_FLUSH_TLB: 75 case LHCALL_FLUSH_TLB:
63 /* FLUSH_TLB comes in two flavors, depending on the 76 /* FLUSH_TLB comes in two flavors, depending on the argument: */
64 * argument: */
65 if (args->arg1) 77 if (args->arg1)
66 guest_pagetable_clear_all(cpu); 78 guest_pagetable_clear_all(cpu);
67 else 79 else
68 guest_pagetable_flush_user(cpu); 80 guest_pagetable_flush_user(cpu);
69 break; 81 break;
70 82
71 /* All these calls simply pass the arguments through to the right 83 /*
72 * routines. */ 84 * All these calls simply pass the arguments through to the right
85 * routines.
86 */
73 case LHCALL_NEW_PGTABLE: 87 case LHCALL_NEW_PGTABLE:
74 guest_new_pagetable(cpu, args->arg1); 88 guest_new_pagetable(cpu, args->arg1);
75 break; 89 break;
@@ -112,15 +126,16 @@ static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
112 kill_guest(cpu, "Bad hypercall %li\n", args->arg0); 126 kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
113 } 127 }
114} 128}
115/*:*/
116 129
117/*H:124 Asynchronous hypercalls are easy: we just look in the array in the 130/*H:124
131 * Asynchronous hypercalls are easy: we just look in the array in the
118 * Guest's "struct lguest_data" to see if any new ones are marked "ready". 132 * Guest's "struct lguest_data" to see if any new ones are marked "ready".
119 * 133 *
120 * We are careful to do these in order: obviously we respect the order the 134 * We are careful to do these in order: obviously we respect the order the
121 * Guest put them in the ring, but we also promise the Guest that they will 135 * Guest put them in the ring, but we also promise the Guest that they will
122 * happen before any normal hypercall (which is why we check this before 136 * happen before any normal hypercall (which is why we check this before
123 * checking for a normal hcall). */ 137 * checking for a normal hcall).
138 */
124static void do_async_hcalls(struct lg_cpu *cpu) 139static 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 */
176static void initialize(struct lg_cpu *cpu) 201static void initialize(struct lg_cpu *cpu)
177{ 202{
178 /* You can't do anything until you're initialized. The Guest knows the 203 /*
179 * rules, so we're unforgiving here. */ 204 * You can't do anything until you're initialized. The Guest knows the
205 * rules, so we're unforgiving here.
206 */
180 if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) { 207 if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
181 kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0); 208 kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
182 return; 209 return;
@@ -185,32 +212,40 @@ static void initialize(struct lg_cpu *cpu)
185 if (lguest_arch_init_hypercalls(cpu)) 212 if (lguest_arch_init_hypercalls(cpu))
186 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); 213 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
187 214
188 /* The Guest tells us where we're not to deliver interrupts by putting 215 /*
189 * the range of addresses into "struct lguest_data". */ 216 * The Guest tells us where we're not to deliver interrupts by putting
217 * the range of addresses into "struct lguest_data".
218 */
190 if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start) 219 if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)
191 || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end)) 220 || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))
192 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); 221 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
193 222
194 /* We write the current time into the Guest's data page once so it can 223 /*
195 * set its clock. */ 224 * We write the current time into the Guest's data page once so it can
225 * set its clock.
226 */
196 write_timestamp(cpu); 227 write_timestamp(cpu);
197 228
198 /* page_tables.c will also do some setup. */ 229 /* page_tables.c will also do some setup. */
199 page_table_guest_data_init(cpu); 230 page_table_guest_data_init(cpu);
200 231
201 /* This is the one case where the above accesses might have been the 232 /*
233 * This is the one case where the above accesses might have been the
202 * first write to a Guest page. This may have caused a copy-on-write 234 * first write to a Guest page. This may have caused a copy-on-write
203 * fault, but the old page might be (read-only) in the Guest 235 * fault, but the old page might be (read-only) in the Guest
204 * pagetable. */ 236 * pagetable.
237 */
205 guest_pagetable_clear_all(cpu); 238 guest_pagetable_clear_all(cpu);
206} 239}
207/*:*/ 240/*:*/
208 241
209/*M:013 If a Guest reads from a page (so creates a mapping) that it has never 242/*M:013
243 * If a Guest reads from a page (so creates a mapping) that it has never
210 * written to, and then the Launcher writes to it (ie. the output of a virtual 244 * written to, and then the Launcher writes to it (ie. the output of a virtual
211 * device), the Guest will still see the old page. In practice, this never 245 * device), the Guest will still see the old page. In practice, this never
212 * happens: why would the Guest read a page which it has never written to? But 246 * happens: why would the Guest read a page which it has never written to? But
213 * a similar scenario might one day bite us, so it's worth mentioning. :*/ 247 * a similar scenario might one day bite us, so it's worth mentioning.
248:*/
214 249
215/*H:100 250/*H:100
216 * Hypercalls 251 * Hypercalls
@@ -229,17 +264,22 @@ void do_hypercalls(struct lg_cpu *cpu)
229 return; 264 return;
230 } 265 }
231 266
232 /* The Guest has initialized. 267 /*
268 * The Guest has initialized.
233 * 269 *
234 * Look in the hypercall ring for the async hypercalls: */ 270 * Look in the hypercall ring for the async hypercalls:
271 */
235 do_async_hcalls(cpu); 272 do_async_hcalls(cpu);
236 273
237 /* If we stopped reading the hypercall ring because the Guest did a 274 /*
275 * If we stopped reading the hypercall ring because the Guest did a
238 * NOTIFY to the Launcher, we want to return now. Otherwise we do 276 * NOTIFY to the Launcher, we want to return now. Otherwise we do
239 * the hypercall. */ 277 * the hypercall.
278 */
240 if (!cpu->pending_notify) { 279 if (!cpu->pending_notify) {
241 do_hcall(cpu, cpu->hcall); 280 do_hcall(cpu, cpu->hcall);
242 /* Tricky point: we reset the hcall pointer to mark the 281 /*
282 * Tricky point: we reset the hcall pointer to mark the
243 * hypercall as "done". We use the hcall pointer rather than 283 * hypercall as "done". We use the hcall pointer rather than
244 * the trap number to indicate a hypercall is pending. 284 * the trap number to indicate a hypercall is pending.
245 * Normally it doesn't matter: the Guest will run again and 285 * Normally it doesn't matter: the Guest will run again and
@@ -248,13 +288,16 @@ void do_hypercalls(struct lg_cpu *cpu)
248 * However, if we are signalled or the Guest sends I/O to the 288 * However, if we are signalled or the Guest sends I/O to the
249 * Launcher, the run_guest() loop will exit without running the 289 * Launcher, the run_guest() loop will exit without running the
250 * Guest. When it comes back it would try to re-run the 290 * Guest. When it comes back it would try to re-run the
251 * hypercall. Finding that bug sucked. */ 291 * hypercall. Finding that bug sucked.
292 */
252 cpu->hcall = NULL; 293 cpu->hcall = NULL;
253 } 294 }
254} 295}
255 296
256/* This routine supplies the Guest with time: it's used for wallclock time at 297/*
257 * initial boot and as a rough time source if the TSC isn't available. */ 298 * This routine supplies the Guest with time: it's used for wallclock time at
299 * initial boot and as a rough time source if the TSC isn't available.
300 */
258void write_timestamp(struct lg_cpu *cpu) 301void write_timestamp(struct lg_cpu *cpu)
259{ 302{
260 struct timespec now; 303 struct timespec now;
diff --git a/drivers/lguest/interrupts_and_traps.c b/drivers/lguest/interrupts_and_traps.c
index 0e9067b0d507..18648180db02 100644
--- a/drivers/lguest/interrupts_and_traps.c
+++ b/drivers/lguest/interrupts_and_traps.c
@@ -1,4 +1,5 @@
1/*P:800 Interrupts (traps) are complicated enough to earn their own file. 1/*P:800
2 * Interrupts (traps) are complicated enough to earn their own file.
2 * There are three classes of interrupts: 3 * There are three classes of interrupts:
3 * 4 *
4 * 1) Real hardware interrupts which occur while we're running the Guest, 5 * 1) Real hardware interrupts which occur while we're running the Guest,
@@ -10,7 +11,8 @@
10 * just like real hardware would deliver them. Traps from the Guest can be set 11 * just like real hardware would deliver them. Traps from the Guest can be set
11 * up to go directly back into the Guest, but sometimes the Host wants to see 12 * up to go directly back into the Guest, but sometimes the Host wants to see
12 * them first, so we also have a way of "reflecting" them into the Guest as if 13 * them first, so we also have a way of "reflecting" them into the Guest as if
13 * they had been delivered to it directly. :*/ 14 * they had been delivered to it directly.
15:*/
14#include <linux/uaccess.h> 16#include <linux/uaccess.h>
15#include <linux/interrupt.h> 17#include <linux/interrupt.h>
16#include <linux/module.h> 18#include <linux/module.h>
@@ -26,8 +28,10 @@ static unsigned long idt_address(u32 lo, u32 hi)
26 return (lo & 0x0000FFFF) | (hi & 0xFFFF0000); 28 return (lo & 0x0000FFFF) | (hi & 0xFFFF0000);
27} 29}
28 30
29/* The "type" of the interrupt handler is a 4 bit field: we only support a 31/*
30 * couple of types. */ 32 * The "type" of the interrupt handler is a 4 bit field: we only support a
33 * couple of types.
34 */
31static int idt_type(u32 lo, u32 hi) 35static 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 */
44static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val) 50static 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 */
63static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi, 71static 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 */
134unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more) 157unsigned 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 */
159void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more) 186void 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. */
218void set_interrupt(struct lg_cpu *cpu, unsigned int irq) 255void 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 */
238static bool could_be_syscall(unsigned int num) 280static 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 */
280static bool has_err(unsigned int trap) 324static 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. */
286bool deliver_trap(struct lg_cpu *cpu, unsigned int num) 330bool 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 */
315static bool direct_trap(unsigned int num) 365static 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 */
351void pin_stack_pages(struct lg_cpu *cpu) 411void 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 */
373void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages) 439void 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 */
395static void set_trap(struct lg_cpu *cpu, struct desc_struct *trap, 466static 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 */
425void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int num, u32 lo, u32 hi) 500void 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 */
446static void default_idt_entry(struct desc_struct *idt, 527static 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 */
481void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt, 568void 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 */
522void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta) 614void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta)
523{ 615{
524 ktime_t expires; 616 ktime_t expires;
@@ -529,9 +621,11 @@ void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta)
529 return; 621 return;
530 } 622 }
531 623
532 /* We use wallclock time here, so the Guest might not be running for 624 /*
625 * We use wallclock time here, so the Guest might not be running for
533 * all the time between now and the timer interrupt it asked for. This 626 * all the time between now and the timer interrupt it asked for. This
534 * is almost always the right thing to do. */ 627 * is almost always the right thing to do.
628 */
535 expires = ktime_add_ns(ktime_get_real(), delta); 629 expires = ktime_add_ns(ktime_get_real(), delta);
536 hrtimer_start(&cpu->hrt, expires, HRTIMER_MODE_ABS); 630 hrtimer_start(&cpu->hrt, expires, HRTIMER_MODE_ABS);
537} 631}
diff --git a/drivers/lguest/lg.h b/drivers/lguest/lg.h
index 01c591923793..74c0db691b53 100644
--- a/drivers/lguest/lg.h
+++ b/drivers/lguest/lg.h
@@ -54,13 +54,13 @@ struct lg_cpu {
54 54
55 unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */ 55 unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */
56 56
57 /* At end of a page shared mapped over lguest_pages in guest. */ 57 /* At end of a page shared mapped over lguest_pages in guest. */
58 unsigned long regs_page; 58 unsigned long regs_page;
59 struct lguest_regs *regs; 59 struct lguest_regs *regs;
60 60
61 struct lguest_pages *last_pages; 61 struct lguest_pages *last_pages;
62 62
63 int cpu_pgd; /* which pgd this cpu is currently using */ 63 int cpu_pgd; /* Which pgd this cpu is currently using */
64 64
65 /* If a hypercall was asked for, this points to the arguments. */ 65 /* If a hypercall was asked for, this points to the arguments. */
66 struct hcall_args *hcall; 66 struct hcall_args *hcall;
@@ -96,8 +96,11 @@ struct lguest
96 unsigned int nr_cpus; 96 unsigned int nr_cpus;
97 97
98 u32 pfn_limit; 98 u32 pfn_limit;
99 /* This provides the offset to the base of guest-physical 99
100 * memory in the Launcher. */ 100 /*
101 * This provides the offset to the base of guest-physical memory in the
102 * Launcher.
103 */
101 void __user *mem_base; 104 void __user *mem_base;
102 unsigned long kernel_address; 105 unsigned long kernel_address;
103 106
@@ -122,11 +125,13 @@ bool lguest_address_ok(const struct lguest *lg,
122void __lgread(struct lg_cpu *, void *, unsigned long, unsigned); 125void __lgread(struct lg_cpu *, void *, unsigned long, unsigned);
123void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned); 126void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned);
124 127
125/*H:035 Using memory-copy operations like that is usually inconvient, so we 128/*H:035
129 * Using memory-copy operations like that is usually inconvient, so we
126 * have the following helper macros which read and write a specific type (often 130 * have the following helper macros which read and write a specific type (often
127 * an unsigned long). 131 * an unsigned long).
128 * 132 *
129 * This reads into a variable of the given type then returns that. */ 133 * This reads into a variable of the given type then returns that.
134 */
130#define lgread(cpu, addr, type) \ 135#define lgread(cpu, addr, type) \
131 ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; }) 136 ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; })
132 137
@@ -140,9 +145,11 @@ void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned);
140 145
141int run_guest(struct lg_cpu *cpu, unsigned long __user *user); 146int run_guest(struct lg_cpu *cpu, unsigned long __user *user);
142 147
143/* Helper macros to obtain the first 12 or the last 20 bits, this is only the 148/*
149 * Helper macros to obtain the first 12 or the last 20 bits, this is only the
144 * first step in the migration to the kernel types. pte_pfn is already defined 150 * first step in the migration to the kernel types. pte_pfn is already defined
145 * in the kernel. */ 151 * in the kernel.
152 */
146#define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK) 153#define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK)
147#define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT) 154#define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT)
148#define pmd_flags(x) (pmd_val(x) & ~PAGE_MASK) 155#define pmd_flags(x) (pmd_val(x) & ~PAGE_MASK)
diff --git a/drivers/lguest/lguest_device.c b/drivers/lguest/lguest_device.c
index e082cdac88b4..cc000e79c3d1 100644
--- a/drivers/lguest/lguest_device.c
+++ b/drivers/lguest/lguest_device.c
@@ -1,10 +1,12 @@
1/*P:050 Lguest guests use a very simple method to describe devices. It's a 1/*P:050
2 * Lguest guests use a very simple method to describe devices. It's a
2 * series of device descriptors contained just above the top of normal Guest 3 * series of device descriptors contained just above the top of normal Guest
3 * memory. 4 * memory.
4 * 5 *
5 * We use the standard "virtio" device infrastructure, which provides us with a 6 * We use the standard "virtio" device infrastructure, which provides us with a
6 * console, a network and a block driver. Each one expects some configuration 7 * console, a network and a block driver. Each one expects some configuration
7 * information and a "virtqueue" or two to send and receive data. :*/ 8 * information and a "virtqueue" or two to send and receive data.
9:*/
8#include <linux/init.h> 10#include <linux/init.h>
9#include <linux/bootmem.h> 11#include <linux/bootmem.h>
10#include <linux/lguest_launcher.h> 12#include <linux/lguest_launcher.h>
@@ -20,8 +22,10 @@
20/* The pointer to our (page) of device descriptions. */ 22/* The pointer to our (page) of device descriptions. */
21static void *lguest_devices; 23static 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 */
25static inline void *lguest_map(unsigned long phys_addr, unsigned long pages) 29static 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 */
37struct lguest_device { 43struct 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 */
59static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc) 68static 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 */
105static void lg_finalize_features(struct virtio_device *vdev) 116static 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 */
151static u8 lg_get_status(struct virtio_device *vdev) 165static 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 */
158static void set_status(struct virtio_device *vdev, u8 status) 174static void set_status(struct virtio_device *vdev, u8 status)
159{ 175{
160 unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices; 176 unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices;
@@ -200,13 +216,17 @@ struct lguest_vq_info
200 void *pages; 216 void *pages;
201}; 217};
202 218
203/* When the virtio_ring code wants to prod the Host, it calls us here and we 219/*
220 * When the virtio_ring code wants to prod the Host, it calls us here and we
204 * make a hypercall. We hand the physical address of the virtqueue so the Host 221 * make a hypercall. We hand the physical address of the virtqueue so the Host
205 * knows which virtqueue we're talking about. */ 222 * knows which virtqueue we're talking about.
223 */
206static void lg_notify(struct virtqueue *vq) 224static void lg_notify(struct virtqueue *vq)
207{ 225{
208 /* We store our virtqueue information in the "priv" pointer of the 226 /*
209 * virtqueue structure. */ 227 * We store our virtqueue information in the "priv" pointer of the
228 * virtqueue structure.
229 */
210 struct lguest_vq_info *lvq = vq->priv; 230 struct lguest_vq_info *lvq = vq->priv;
211 231
212 kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT); 232 kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT);
@@ -215,7 +235,8 @@ static void lg_notify(struct virtqueue *vq)
215/* An extern declaration inside a C file is bad form. Don't do it. */ 235/* An extern declaration inside a C file is bad form. Don't do it. */
216extern void lguest_setup_irq(unsigned int irq); 236extern void lguest_setup_irq(unsigned int irq);
217 237
218/* This routine finds the first virtqueue described in the configuration of 238/*
239 * This routine finds the first virtqueue described in the configuration of
219 * this device and sets it up. 240 * this device and sets it up.
220 * 241 *
221 * This is kind of an ugly duckling. It'd be nicer to have a standard 242 * This is kind of an ugly duckling. It'd be nicer to have a standard
@@ -225,7 +246,8 @@ extern void lguest_setup_irq(unsigned int irq);
225 * simpler for the Host to simply tell us where the pages are. 246 * simpler for the Host to simply tell us where the pages are.
226 * 247 *
227 * So we provide drivers with a "find the Nth virtqueue and set it up" 248 * So we provide drivers with a "find the Nth virtqueue and set it up"
228 * function. */ 249 * function.
250 */
229static struct virtqueue *lg_find_vq(struct virtio_device *vdev, 251static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
230 unsigned index, 252 unsigned index,
231 void (*callback)(struct virtqueue *vq), 253 void (*callback)(struct virtqueue *vq),
@@ -244,9 +266,11 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
244 if (!lvq) 266 if (!lvq)
245 return ERR_PTR(-ENOMEM); 267 return ERR_PTR(-ENOMEM);
246 268
247 /* Make a copy of the "struct lguest_vqconfig" entry, which sits after 269 /*
270 * Make a copy of the "struct lguest_vqconfig" entry, which sits after
248 * the descriptor. We need a copy because the config space might not 271 * the descriptor. We need a copy because the config space might not
249 * be aligned correctly. */ 272 * be aligned correctly.
273 */
250 memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config)); 274 memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config));
251 275
252 printk("Mapping virtqueue %i addr %lx\n", index, 276 printk("Mapping virtqueue %i addr %lx\n", index,
@@ -261,8 +285,10 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
261 goto free_lvq; 285 goto free_lvq;
262 } 286 }
263 287
264 /* OK, tell virtio_ring.c to set up a virtqueue now we know its size 288 /*
265 * and we've got a pointer to its pages. */ 289 * OK, tell virtio_ring.c to set up a virtqueue now we know its size
290 * and we've got a pointer to its pages.
291 */
266 vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN, 292 vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN,
267 vdev, lvq->pages, lg_notify, callback, name); 293 vdev, lvq->pages, lg_notify, callback, name);
268 if (!vq) { 294 if (!vq) {
@@ -273,18 +299,23 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
273 /* Make sure the interrupt is allocated. */ 299 /* Make sure the interrupt is allocated. */
274 lguest_setup_irq(lvq->config.irq); 300 lguest_setup_irq(lvq->config.irq);
275 301
276 /* Tell the interrupt for this virtqueue to go to the virtio_ring 302 /*
277 * interrupt handler. */ 303 * Tell the interrupt for this virtqueue to go to the virtio_ring
278 /* FIXME: We used to have a flag for the Host to tell us we could use 304 * interrupt handler.
305 *
306 * FIXME: We used to have a flag for the Host to tell us we could use
279 * the interrupt as a source of randomness: it'd be nice to have that 307 * the interrupt as a source of randomness: it'd be nice to have that
280 * back.. */ 308 * back.
309 */
281 err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED, 310 err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED,
282 dev_name(&vdev->dev), vq); 311 dev_name(&vdev->dev), vq);
283 if (err) 312 if (err)
284 goto destroy_vring; 313 goto destroy_vring;
285 314
286 /* Last of all we hook up our 'struct lguest_vq_info" to the 315 /*
287 * virtqueue's priv pointer. */ 316 * Last of all we hook up our 'struct lguest_vq_info" to the
317 * virtqueue's priv pointer.
318 */
288 vq->priv = lvq; 319 vq->priv = lvq;
289 return vq; 320 return vq;
290 321
@@ -358,11 +389,14 @@ static struct virtio_config_ops lguest_config_ops = {
358 .del_vqs = lg_del_vqs, 389 .del_vqs = lg_del_vqs,
359}; 390};
360 391
361/* The root device for the lguest virtio devices. This makes them appear as 392/*
362 * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */ 393 * The root device for the lguest virtio devices. This makes them appear as
394 * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2.
395 */
363static struct device *lguest_root; 396static struct device *lguest_root;
364 397
365/*D:120 This is the core of the lguest bus: actually adding a new device. 398/*D:120
399 * This is the core of the lguest bus: actually adding a new device.
366 * It's a separate function because it's neater that way, and because an 400 * It's a separate function because it's neater that way, and because an
367 * earlier version of the code supported hotplug and unplug. They were removed 401 * earlier version of the code supported hotplug and unplug. They were removed
368 * early on because they were never used. 402 * early on because they were never used.
@@ -371,14 +405,14 @@ static struct device *lguest_root;
371 * 405 *
372 * It's worth reading this carefully: we start with a pointer to the new device 406 * It's worth reading this carefully: we start with a pointer to the new device
373 * descriptor in the "lguest_devices" page, and the offset into the device 407 * descriptor in the "lguest_devices" page, and the offset into the device
374 * descriptor page so we can uniquely identify it if things go badly wrong. */ 408 * descriptor page so we can uniquely identify it if things go badly wrong.
409 */
375static void add_lguest_device(struct lguest_device_desc *d, 410static void add_lguest_device(struct lguest_device_desc *d,
376 unsigned int offset) 411 unsigned int offset)
377{ 412{
378 struct lguest_device *ldev; 413 struct lguest_device *ldev;
379 414
380 /* Start with zeroed memory; Linux's device layer seems to count on 415 /* Start with zeroed memory; Linux's device layer counts on it. */
381 * it. */
382 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL); 416 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
383 if (!ldev) { 417 if (!ldev) {
384 printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n", 418 printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n",
@@ -390,15 +424,19 @@ static void add_lguest_device(struct lguest_device_desc *d,
390 ldev->vdev.dev.parent = lguest_root; 424 ldev->vdev.dev.parent = lguest_root;
391 /* We have a unique device index thanks to the dev_index counter. */ 425 /* We have a unique device index thanks to the dev_index counter. */
392 ldev->vdev.id.device = d->type; 426 ldev->vdev.id.device = d->type;
393 /* We have a simple set of routines for querying the device's 427 /*
394 * configuration information and setting its status. */ 428 * We have a simple set of routines for querying the device's
429 * configuration information and setting its status.
430 */
395 ldev->vdev.config = &lguest_config_ops; 431 ldev->vdev.config = &lguest_config_ops;
396 /* And we remember the device's descriptor for lguest_config_ops. */ 432 /* And we remember the device's descriptor for lguest_config_ops. */
397 ldev->desc = d; 433 ldev->desc = d;
398 434
399 /* register_virtio_device() sets up the generic fields for the struct 435 /*
436 * register_virtio_device() sets up the generic fields for the struct
400 * virtio_device and calls device_register(). This makes the bus 437 * virtio_device and calls device_register(). This makes the bus
401 * infrastructure look for a matching driver. */ 438 * infrastructure look for a matching driver.
439 */
402 if (register_virtio_device(&ldev->vdev) != 0) { 440 if (register_virtio_device(&ldev->vdev) != 0) {
403 printk(KERN_ERR "Failed to register lguest dev %u type %u\n", 441 printk(KERN_ERR "Failed to register lguest dev %u type %u\n",
404 offset, d->type); 442 offset, d->type);
@@ -406,8 +444,10 @@ static void add_lguest_device(struct lguest_device_desc *d,
406 } 444 }
407} 445}
408 446
409/*D:110 scan_devices() simply iterates through the device page. The type 0 is 447/*D:110
410 * reserved to mean "end of devices". */ 448 * scan_devices() simply iterates through the device page. The type 0 is
449 * reserved to mean "end of devices".
450 */
411static void scan_devices(void) 451static 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 */
441static int __init lguest_devices_init(void) 483static 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. */
457postcore_initcall(lguest_devices_init); 499postcore_initcall(lguest_devices_init);
458 500
459/*D:150 At this point in the journey we used to now wade through the lguest 501/*D:150
502 * At this point in the journey we used to now wade through the lguest
460 * devices themselves: net, block and console. Since they're all now virtio 503 * devices themselves: net, block and console. Since they're all now virtio
461 * devices rather than lguest-specific, I've decided to ignore them. Mostly, 504 * devices rather than lguest-specific, I've decided to ignore them. Mostly,
462 * they're kind of boring. But this does mean you'll never experience the 505 * they're kind of boring. But this does mean you'll never experience the
463 * thrill of reading the forbidden love scene buried deep in the block driver. 506 * thrill of reading the forbidden love scene buried deep in the block driver.
464 * 507 *
465 * "make Launcher" beckons, where we answer questions like "Where do Guests 508 * "make Launcher" beckons, where we answer questions like "Where do Guests
466 * come from?", and "What do you do when someone asks for optimization?". */ 509 * come from?", and "What do you do when someone asks for optimization?".
510 */
diff --git a/drivers/lguest/lguest_user.c b/drivers/lguest/lguest_user.c
index 407722a8e0c4..7e92017103dc 100644
--- a/drivers/lguest/lguest_user.c
+++ b/drivers/lguest/lguest_user.c
@@ -1,8 +1,10 @@
1/*P:200 This contains all the /dev/lguest code, whereby the userspace launcher 1/*P:200
2 * This contains all the /dev/lguest code, whereby the userspace launcher
2 * controls and communicates with the Guest. For example, the first write will 3 * controls and communicates with the Guest. For example, the first write will
3 * tell us the Guest's memory layout, pagetable, entry point and kernel address 4 * tell us the Guest's memory layout, pagetable, entry point and kernel address
4 * offset. A read will run the Guest until something happens, such as a signal 5 * offset. A read will run the Guest until something happens, such as a signal
5 * or the Guest doing a NOTIFY out to the Launcher. :*/ 6 * or the Guest doing a NOTIFY out to the Launcher.
7:*/
6#include <linux/uaccess.h> 8#include <linux/uaccess.h>
7#include <linux/miscdevice.h> 9#include <linux/miscdevice.h>
8#include <linux/fs.h> 10#include <linux/fs.h>
@@ -37,8 +39,10 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
37 if (!addr) 39 if (!addr)
38 return -EINVAL; 40 return -EINVAL;
39 41
40 /* Replace the old array with the new one, carefully: others can 42 /*
41 * be accessing it at the same time */ 43 * Replace the old array with the new one, carefully: others can
44 * be accessing it at the same time.
45 */
42 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), 46 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
43 GFP_KERNEL); 47 GFP_KERNEL);
44 if (!new) 48 if (!new)
@@ -61,8 +65,10 @@ static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
61 /* Now put new one in place. */ 65 /* Now put new one in place. */
62 rcu_assign_pointer(lg->eventfds, new); 66 rcu_assign_pointer(lg->eventfds, new);
63 67
64 /* We're not in a big hurry. Wait until noone's looking at old 68 /*
65 * version, then delete it. */ 69 * We're not in a big hurry. Wait until noone's looking at old
70 * version, then delete it.
71 */
66 synchronize_rcu(); 72 synchronize_rcu();
67 kfree(old); 73 kfree(old);
68 74
@@ -87,8 +93,10 @@ static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
87 return err; 93 return err;
88} 94}
89 95
90/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt 96/*L:050
91 * number to /dev/lguest. */ 97 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
98 * number to /dev/lguest.
99 */
92static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) 100static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
93{ 101{
94 unsigned long irq; 102 unsigned long irq;
@@ -102,8 +110,10 @@ static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
102 return 0; 110 return 0;
103} 111}
104 112
105/*L:040 Once our Guest is initialized, the Launcher makes it run by reading 113/*L:040
106 * from /dev/lguest. */ 114 * Once our Guest is initialized, the Launcher makes it run by reading
115 * from /dev/lguest.
116 */
107static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) 117static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
108{ 118{
109 struct lguest *lg = file->private_data; 119 struct lguest *lg = file->private_data;
@@ -139,8 +149,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
139 return len; 149 return len;
140 } 150 }
141 151
142 /* If we returned from read() last time because the Guest sent I/O, 152 /*
143 * clear the flag. */ 153 * If we returned from read() last time because the Guest sent I/O,
154 * clear the flag.
155 */
144 if (cpu->pending_notify) 156 if (cpu->pending_notify)
145 cpu->pending_notify = 0; 157 cpu->pending_notify = 0;
146 158
@@ -148,8 +160,10 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
148 return run_guest(cpu, (unsigned long __user *)user); 160 return run_guest(cpu, (unsigned long __user *)user);
149} 161}
150 162
151/*L:025 This actually initializes a CPU. For the moment, a Guest is only 163/*L:025
152 * uniprocessor, so "id" is always 0. */ 164 * This actually initializes a CPU. For the moment, a Guest is only
165 * uniprocessor, so "id" is always 0.
166 */
153static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) 167static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
154{ 168{
155 /* We have a limited number the number of CPUs in the lguest struct. */ 169 /* We have a limited number the number of CPUs in the lguest struct. */
@@ -164,8 +178,10 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
164 /* Each CPU has a timer it can set. */ 178 /* Each CPU has a timer it can set. */
165 init_clockdev(cpu); 179 init_clockdev(cpu);
166 180
167 /* We need a complete page for the Guest registers: they are accessible 181 /*
168 * to the Guest and we can only grant it access to whole pages. */ 182 * We need a complete page for the Guest registers: they are accessible
183 * to the Guest and we can only grant it access to whole pages.
184 */
169 cpu->regs_page = get_zeroed_page(GFP_KERNEL); 185 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
170 if (!cpu->regs_page) 186 if (!cpu->regs_page)
171 return -ENOMEM; 187 return -ENOMEM;
@@ -173,29 +189,38 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
173 /* We actually put the registers at the bottom of the page. */ 189 /* We actually put the registers at the bottom of the page. */
174 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); 190 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
175 191
176 /* Now we initialize the Guest's registers, handing it the start 192 /*
177 * address. */ 193 * Now we initialize the Guest's registers, handing it the start
194 * address.
195 */
178 lguest_arch_setup_regs(cpu, start_ip); 196 lguest_arch_setup_regs(cpu, start_ip);
179 197
180 /* We keep a pointer to the Launcher task (ie. current task) for when 198 /*
181 * other Guests want to wake this one (eg. console input). */ 199 * We keep a pointer to the Launcher task (ie. current task) for when
200 * other Guests want to wake this one (eg. console input).
201 */
182 cpu->tsk = current; 202 cpu->tsk = current;
183 203
184 /* We need to keep a pointer to the Launcher's memory map, because if 204 /*
205 * We need to keep a pointer to the Launcher's memory map, because if
185 * the Launcher dies we need to clean it up. If we don't keep a 206 * the Launcher dies we need to clean it up. If we don't keep a
186 * reference, it is destroyed before close() is called. */ 207 * reference, it is destroyed before close() is called.
208 */
187 cpu->mm = get_task_mm(cpu->tsk); 209 cpu->mm = get_task_mm(cpu->tsk);
188 210
189 /* We remember which CPU's pages this Guest used last, for optimization 211 /*
190 * when the same Guest runs on the same CPU twice. */ 212 * We remember which CPU's pages this Guest used last, for optimization
213 * when the same Guest runs on the same CPU twice.
214 */
191 cpu->last_pages = NULL; 215 cpu->last_pages = NULL;
192 216
193 /* No error == success. */ 217 /* No error == success. */
194 return 0; 218 return 0;
195} 219}
196 220
197/*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit) 221/*L:020
198 * values (in addition to the LHREQ_INITIALIZE value). These are: 222 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
223 * addition to the LHREQ_INITIALIZE value). These are:
199 * 224 *
200 * base: The start of the Guest-physical memory inside the Launcher memory. 225 * base: The start of the Guest-physical memory inside the Launcher memory.
201 * 226 *
@@ -207,14 +232,15 @@ static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
207 */ 232 */
208static int initialize(struct file *file, const unsigned long __user *input) 233static int initialize(struct file *file, const unsigned long __user *input)
209{ 234{
210 /* "struct lguest" contains everything we (the Host) know about a 235 /* "struct lguest" contains all we (the Host) know about a Guest. */
211 * Guest. */
212 struct lguest *lg; 236 struct lguest *lg;
213 int err; 237 int err;
214 unsigned long args[3]; 238 unsigned long args[3];
215 239
216 /* We grab the Big Lguest lock, which protects against multiple 240 /*
217 * simultaneous initializations. */ 241 * We grab the Big Lguest lock, which protects against multiple
242 * simultaneous initializations.
243 */
218 mutex_lock(&lguest_lock); 244 mutex_lock(&lguest_lock);
219 /* You can't initialize twice! Close the device and start again... */ 245 /* You can't initialize twice! Close the device and start again... */
220 if (file->private_data) { 246 if (file->private_data) {
@@ -249,8 +275,10 @@ static int initialize(struct file *file, const unsigned long __user *input)
249 if (err) 275 if (err)
250 goto free_eventfds; 276 goto free_eventfds;
251 277
252 /* Initialize the Guest's shadow page tables, using the toplevel 278 /*
253 * address the Launcher gave us. This allocates memory, so can fail. */ 279 * Initialize the Guest's shadow page tables, using the toplevel
280 * address the Launcher gave us. This allocates memory, so can fail.
281 */
254 err = init_guest_pagetable(lg); 282 err = init_guest_pagetable(lg);
255 if (err) 283 if (err)
256 goto free_regs; 284 goto free_regs;
@@ -275,7 +303,8 @@ unlock:
275 return err; 303 return err;
276} 304}
277 305
278/*L:010 The first operation the Launcher does must be a write. All writes 306/*L:010
307 * The first operation the Launcher does must be a write. All writes
279 * start with an unsigned long number: for the first write this must be 308 * start with an unsigned long number: for the first write this must be
280 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use 309 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
281 * writes of other values to send interrupts. 310 * writes of other values to send interrupts.
@@ -283,12 +312,15 @@ unlock:
283 * Note that we overload the "offset" in the /dev/lguest file to indicate what 312 * Note that we overload the "offset" in the /dev/lguest file to indicate what
284 * CPU number we're dealing with. Currently this is always 0, since we only 313 * CPU number we're dealing with. Currently this is always 0, since we only
285 * support uniprocessor Guests, but you can see the beginnings of SMP support 314 * support uniprocessor Guests, but you can see the beginnings of SMP support
286 * here. */ 315 * here.
316 */
287static ssize_t write(struct file *file, const char __user *in, 317static ssize_t write(struct file *file, const char __user *in,
288 size_t size, loff_t *off) 318 size_t size, loff_t *off)
289{ 319{
290 /* Once the Guest is initialized, we hold the "struct lguest" in the 320 /*
291 * file private data. */ 321 * Once the Guest is initialized, we hold the "struct lguest" in the
322 * file private data.
323 */
292 struct lguest *lg = file->private_data; 324 struct lguest *lg = file->private_data;
293 const unsigned long __user *input = (const unsigned long __user *)in; 325 const unsigned long __user *input = (const unsigned long __user *)in;
294 unsigned long req; 326 unsigned long req;
@@ -323,13 +355,15 @@ static ssize_t write(struct file *file, const char __user *in,
323 } 355 }
324} 356}
325 357
326/*L:060 The final piece of interface code is the close() routine. It reverses 358/*L:060
359 * The final piece of interface code is the close() routine. It reverses
327 * everything done in initialize(). This is usually called because the 360 * everything done in initialize(). This is usually called because the
328 * Launcher exited. 361 * Launcher exited.
329 * 362 *
330 * Note that the close routine returns 0 or a negative error number: it can't 363 * Note that the close routine returns 0 or a negative error number: it can't
331 * really fail, but it can whine. I blame Sun for this wart, and K&R C for 364 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
332 * letting them do it. :*/ 365 * letting them do it.
366:*/
333static int close(struct inode *inode, struct file *file) 367static int close(struct inode *inode, struct file *file)
334{ 368{
335 struct lguest *lg = file->private_data; 369 struct lguest *lg = file->private_data;
@@ -339,8 +373,10 @@ static int close(struct inode *inode, struct file *file)
339 if (!lg) 373 if (!lg)
340 return 0; 374 return 0;
341 375
342 /* We need the big lock, to protect from inter-guest I/O and other 376 /*
343 * Launchers initializing guests. */ 377 * We need the big lock, to protect from inter-guest I/O and other
378 * Launchers initializing guests.
379 */
344 mutex_lock(&lguest_lock); 380 mutex_lock(&lguest_lock);
345 381
346 /* Free up the shadow page tables for the Guest. */ 382 /* Free up the shadow page tables for the Guest. */
@@ -351,8 +387,10 @@ static int close(struct inode *inode, struct file *file)
351 hrtimer_cancel(&lg->cpus[i].hrt); 387 hrtimer_cancel(&lg->cpus[i].hrt);
352 /* We can free up the register page we allocated. */ 388 /* We can free up the register page we allocated. */
353 free_page(lg->cpus[i].regs_page); 389 free_page(lg->cpus[i].regs_page);
354 /* Now all the memory cleanups are done, it's safe to release 390 /*
355 * the Launcher's memory management structure. */ 391 * Now all the memory cleanups are done, it's safe to release
392 * the Launcher's memory management structure.
393 */
356 mmput(lg->cpus[i].mm); 394 mmput(lg->cpus[i].mm);
357 } 395 }
358 396
@@ -361,8 +399,10 @@ static int close(struct inode *inode, struct file *file)
361 eventfd_ctx_put(lg->eventfds->map[i].event); 399 eventfd_ctx_put(lg->eventfds->map[i].event);
362 kfree(lg->eventfds); 400 kfree(lg->eventfds);
363 401
364 /* If lg->dead doesn't contain an error code it will be NULL or a 402 /*
365 * kmalloc()ed string, either of which is ok to hand to kfree(). */ 403 * If lg->dead doesn't contain an error code it will be NULL or a
404 * kmalloc()ed string, either of which is ok to hand to kfree().
405 */
366 if (!IS_ERR(lg->dead)) 406 if (!IS_ERR(lg->dead))
367 kfree(lg->dead); 407 kfree(lg->dead);
368 /* Free the memory allocated to the lguest_struct */ 408 /* Free the memory allocated to the lguest_struct */
@@ -386,7 +426,8 @@ static int close(struct inode *inode, struct file *file)
386 * 426 *
387 * We begin our understanding with the Host kernel interface which the Launcher 427 * We begin our understanding with the Host kernel interface which the Launcher
388 * uses: reading and writing a character device called /dev/lguest. All the 428 * uses: reading and writing a character device called /dev/lguest. All the
389 * work happens in the read(), write() and close() routines: */ 429 * work happens in the read(), write() and close() routines:
430 */
390static struct file_operations lguest_fops = { 431static struct file_operations lguest_fops = {
391 .owner = THIS_MODULE, 432 .owner = THIS_MODULE,
392 .release = close, 433 .release = close,
@@ -394,8 +435,10 @@ static struct file_operations lguest_fops = {
394 .read = read, 435 .read = read,
395}; 436};
396 437
397/* This is a textbook example of a "misc" character device. Populate a "struct 438/*
398 * miscdevice" and register it with misc_register(). */ 439 * This is a textbook example of a "misc" character device. Populate a "struct
440 * miscdevice" and register it with misc_register().
441 */
399static struct miscdevice lguest_dev = { 442static struct miscdevice lguest_dev = {
400 .minor = MISC_DYNAMIC_MINOR, 443 .minor = MISC_DYNAMIC_MINOR,
401 .name = "lguest", 444 .name = "lguest",
diff --git a/drivers/lguest/page_tables.c b/drivers/lguest/page_tables.c
index a6fe1abda240..3da902e4b4cb 100644
--- a/drivers/lguest/page_tables.c
+++ b/drivers/lguest/page_tables.c
@@ -1,9 +1,11 @@
1/*P:700 The pagetable code, on the other hand, still shows the scars of 1/*P:700
2 * The pagetable code, on the other hand, still shows the scars of
2 * previous encounters. It's functional, and as neat as it can be in the 3 * previous encounters. It's functional, and as neat as it can be in the
3 * circumstances, but be wary, for these things are subtle and break easily. 4 * circumstances, but be wary, for these things are subtle and break easily.
4 * The Guest provides a virtual to physical mapping, but we can neither trust 5 * The Guest provides a virtual to physical mapping, but we can neither trust
5 * it nor use it: we verify and convert it here then point the CPU to the 6 * it nor use it: we verify and convert it here then point the CPU to the
6 * converted Guest pages when running the Guest. :*/ 7 * converted Guest pages when running the Guest.
8:*/
7 9
8/* Copyright (C) Rusty Russell IBM Corporation 2006. 10/* Copyright (C) Rusty Russell IBM Corporation 2006.
9 * GPL v2 and any later version */ 11 * GPL v2 and any later version */
@@ -17,10 +19,12 @@
17#include <asm/bootparam.h> 19#include <asm/bootparam.h>
18#include "lg.h" 20#include "lg.h"
19 21
20/*M:008 We hold reference to pages, which prevents them from being swapped. 22/*M:008
23 * We hold reference to pages, which prevents them from being swapped.
21 * It'd be nice to have a callback in the "struct mm_struct" when Linux wants 24 * It'd be nice to have a callback in the "struct mm_struct" when Linux wants
22 * to swap out. If we had this, and a shrinker callback to trim PTE pages, we 25 * to swap out. If we had this, and a shrinker callback to trim PTE pages, we
23 * could probably consider launching Guests as non-root. :*/ 26 * could probably consider launching Guests as non-root.
27:*/
24 28
25/*H:300 29/*H:300
26 * The Page Table Code 30 * The Page Table Code
@@ -45,16 +49,19 @@
45 * (v) Flushing (throwing away) page tables, 49 * (v) Flushing (throwing away) page tables,
46 * (vi) Mapping the Switcher when the Guest is about to run, 50 * (vi) Mapping the Switcher when the Guest is about to run,
47 * (vii) Setting up the page tables initially. 51 * (vii) Setting up the page tables initially.
48 :*/ 52:*/
49 53
50 54/*
51/* 1024 entries in a page table page maps 1024 pages: 4MB. The Switcher is 55 * 1024 entries in a page table page maps 1024 pages: 4MB. The Switcher is
52 * conveniently placed at the top 4MB, so it uses a separate, complete PTE 56 * conveniently placed at the top 4MB, so it uses a separate, complete PTE
53 * page. */ 57 * page.
58 */
54#define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1) 59#define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1)
55 60
56/* For PAE we need the PMD index as well. We use the last 2MB, so we 61/*
57 * will need the last pmd entry of the last pmd page. */ 62 * For PAE we need the PMD index as well. We use the last 2MB, so we
63 * will need the last pmd entry of the last pmd page.
64 */
58#ifdef CONFIG_X86_PAE 65#ifdef CONFIG_X86_PAE
59#define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1) 66#define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1)
60#define RESERVE_MEM 2U 67#define RESERVE_MEM 2U
@@ -64,13 +71,16 @@
64#define CHECK_GPGD_MASK _PAGE_TABLE 71#define CHECK_GPGD_MASK _PAGE_TABLE
65#endif 72#endif
66 73
67/* We actually need a separate PTE page for each CPU. Remember that after the 74/*
75 * We actually need a separate PTE page for each CPU. Remember that after the
68 * Switcher code itself comes two pages for each CPU, and we don't want this 76 * Switcher code itself comes two pages for each CPU, and we don't want this
69 * CPU's guest to see the pages of any other CPU. */ 77 * CPU's guest to see the pages of any other CPU.
78 */
70static DEFINE_PER_CPU(pte_t *, switcher_pte_pages); 79static DEFINE_PER_CPU(pte_t *, switcher_pte_pages);
71#define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu) 80#define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu)
72 81
73/*H:320 The page table code is curly enough to need helper functions to keep it 82/*H:320
83 * The page table code is curly enough to need helper functions to keep it
74 * clear and clean. 84 * clear and clean.
75 * 85 *
76 * There are two functions which return pointers to the shadow (aka "real") 86 * There are two functions which return pointers to the shadow (aka "real")
@@ -79,7 +89,8 @@ static DEFINE_PER_CPU(pte_t *, switcher_pte_pages);
79 * spgd_addr() takes the virtual address and returns a pointer to the top-level 89 * spgd_addr() takes the virtual address and returns a pointer to the top-level
80 * page directory entry (PGD) for that address. Since we keep track of several 90 * page directory entry (PGD) for that address. Since we keep track of several
81 * page tables, the "i" argument tells us which one we're interested in (it's 91 * page tables, the "i" argument tells us which one we're interested in (it's
82 * usually the current one). */ 92 * usually the current one).
93 */
83static pgd_t *spgd_addr(struct lg_cpu *cpu, u32 i, unsigned long vaddr) 94static 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 */
102static pmd_t *spmd_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) 115static 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 */
125static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr) 140static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr)
126{ 141{
127#ifdef CONFIG_X86_PAE 142#ifdef CONFIG_X86_PAE
@@ -139,8 +154,10 @@ static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr)
139 return &page[pte_index(vaddr)]; 154 return &page[pte_index(vaddr)];
140} 155}
141 156
142/* These two functions just like the above two, except they access the Guest 157/*
143 * page tables. Hence they return a Guest address. */ 158 * These two functions just like the above two, except they access the Guest
159 * page tables. Hence they return a Guest address.
160 */
144static unsigned long gpgd_addr(struct lg_cpu *cpu, unsigned long vaddr) 161static unsigned long gpgd_addr(struct lg_cpu *cpu, unsigned long vaddr)
145{ 162{
146 unsigned int index = vaddr >> (PGDIR_SHIFT); 163 unsigned int index = vaddr >> (PGDIR_SHIFT);
@@ -175,17 +192,21 @@ static unsigned long gpte_addr(struct lg_cpu *cpu,
175#endif 192#endif
176/*:*/ 193/*:*/
177 194
178/*M:014 get_pfn is slow: we could probably try to grab batches of pages here as 195/*M:014
179 * an optimization (ie. pre-faulting). :*/ 196 * get_pfn is slow: we could probably try to grab batches of pages here as
197 * an optimization (ie. pre-faulting).
198:*/
180 199
181/*H:350 This routine takes a page number given by the Guest and converts it to 200/*H:350
201 * This routine takes a page number given by the Guest and converts it to
182 * an actual, physical page number. It can fail for several reasons: the 202 * an actual, physical page number. It can fail for several reasons: the
183 * virtual address might not be mapped by the Launcher, the write flag is set 203 * virtual address might not be mapped by the Launcher, the write flag is set
184 * and the page is read-only, or the write flag was set and the page was 204 * and the page is read-only, or the write flag was set and the page was
185 * shared so had to be copied, but we ran out of memory. 205 * shared so had to be copied, but we ran out of memory.
186 * 206 *
187 * This holds a reference to the page, so release_pte() is careful to put that 207 * This holds a reference to the page, so release_pte() is careful to put that
188 * back. */ 208 * back.
209 */
189static unsigned long get_pfn(unsigned long virtpfn, int write) 210static unsigned long get_pfn(unsigned long virtpfn, int write)
190{ 211{
191 struct page *page; 212 struct page *page;
@@ -198,33 +219,41 @@ static unsigned long get_pfn(unsigned long virtpfn, int write)
198 return -1UL; 219 return -1UL;
199} 220}
200 221
201/*H:340 Converting a Guest page table entry to a shadow (ie. real) page table 222/*H:340
223 * Converting a Guest page table entry to a shadow (ie. real) page table
202 * entry can be a little tricky. The flags are (almost) the same, but the 224 * entry can be a little tricky. The flags are (almost) the same, but the
203 * Guest PTE contains a virtual page number: the CPU needs the real page 225 * Guest PTE contains a virtual page number: the CPU needs the real page
204 * number. */ 226 * number.
227 */
205static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write) 228static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write)
206{ 229{
207 unsigned long pfn, base, flags; 230 unsigned long pfn, base, flags;
208 231
209 /* The Guest sets the global flag, because it thinks that it is using 232 /*
233 * The Guest sets the global flag, because it thinks that it is using
210 * PGE. We only told it to use PGE so it would tell us whether it was 234 * PGE. We only told it to use PGE so it would tell us whether it was
211 * flushing a kernel mapping or a userspace mapping. We don't actually 235 * flushing a kernel mapping or a userspace mapping. We don't actually
212 * use the global bit, so throw it away. */ 236 * use the global bit, so throw it away.
237 */
213 flags = (pte_flags(gpte) & ~_PAGE_GLOBAL); 238 flags = (pte_flags(gpte) & ~_PAGE_GLOBAL);
214 239
215 /* The Guest's pages are offset inside the Launcher. */ 240 /* The Guest's pages are offset inside the Launcher. */
216 base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE; 241 base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE;
217 242
218 /* We need a temporary "unsigned long" variable to hold the answer from 243 /*
244 * We need a temporary "unsigned long" variable to hold the answer from
219 * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't 245 * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't
220 * fit in spte.pfn. get_pfn() finds the real physical number of the 246 * fit in spte.pfn. get_pfn() finds the real physical number of the
221 * page, given the virtual number. */ 247 * page, given the virtual number.
248 */
222 pfn = get_pfn(base + pte_pfn(gpte), write); 249 pfn = get_pfn(base + pte_pfn(gpte), write);
223 if (pfn == -1UL) { 250 if (pfn == -1UL) {
224 kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte)); 251 kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte));
225 /* When we destroy the Guest, we'll go through the shadow page 252 /*
253 * When we destroy the Guest, we'll go through the shadow page
226 * tables and release_pte() them. Make sure we don't think 254 * tables and release_pte() them. Make sure we don't think
227 * this one is valid! */ 255 * this one is valid!
256 */
228 flags = 0; 257 flags = 0;
229 } 258 }
230 /* Now we assemble our shadow PTE from the page number and flags. */ 259 /* Now we assemble our shadow PTE from the page number and flags. */
@@ -234,8 +263,10 @@ static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write)
234/*H:460 And to complete the chain, release_pte() looks like this: */ 263/*H:460 And to complete the chain, release_pte() looks like this: */
235static void release_pte(pte_t pte) 264static void release_pte(pte_t pte)
236{ 265{
237 /* Remember that get_user_pages_fast() took a reference to the page, in 266 /*
238 * get_pfn()? We have to put it back now. */ 267 * Remember that get_user_pages_fast() took a reference to the page, in
268 * get_pfn()? We have to put it back now.
269 */
239 if (pte_flags(pte) & _PAGE_PRESENT) 270 if (pte_flags(pte) & _PAGE_PRESENT)
240 put_page(pte_page(pte)); 271 put_page(pte_page(pte));
241} 272}
@@ -273,7 +304,8 @@ static void check_gpmd(struct lg_cpu *cpu, pmd_t gpmd)
273 * and return to the Guest without it knowing. 304 * and return to the Guest without it knowing.
274 * 305 *
275 * If we fixed up the fault (ie. we mapped the address), this routine returns 306 * If we fixed up the fault (ie. we mapped the address), this routine returns
276 * true. Otherwise, it was a real fault and we need to tell the Guest. */ 307 * true. Otherwise, it was a real fault and we need to tell the Guest.
308 */
277bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode) 309bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
278{ 310{
279 pgd_t gpgd; 311 pgd_t gpgd;
@@ -298,22 +330,26 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
298 if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) { 330 if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) {
299 /* No shadow entry: allocate a new shadow PTE page. */ 331 /* No shadow entry: allocate a new shadow PTE page. */
300 unsigned long ptepage = get_zeroed_page(GFP_KERNEL); 332 unsigned long ptepage = get_zeroed_page(GFP_KERNEL);
301 /* This is not really the Guest's fault, but killing it is 333 /*
302 * simple for this corner case. */ 334 * This is not really the Guest's fault, but killing it is
335 * simple for this corner case.
336 */
303 if (!ptepage) { 337 if (!ptepage) {
304 kill_guest(cpu, "out of memory allocating pte page"); 338 kill_guest(cpu, "out of memory allocating pte page");
305 return false; 339 return false;
306 } 340 }
307 /* We check that the Guest pgd is OK. */ 341 /* We check that the Guest pgd is OK. */
308 check_gpgd(cpu, gpgd); 342 check_gpgd(cpu, gpgd);
309 /* And we copy the flags to the shadow PGD entry. The page 343 /*
310 * number in the shadow PGD is the page we just allocated. */ 344 * And we copy the flags to the shadow PGD entry. The page
345 * number in the shadow PGD is the page we just allocated.
346 */
311 set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd))); 347 set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd)));
312 } 348 }
313 349
314#ifdef CONFIG_X86_PAE 350#ifdef CONFIG_X86_PAE
315 gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t); 351 gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t);
316 /* middle level not present? We can't map it in. */ 352 /* Middle level not present? We can't map it in. */
317 if (!(pmd_flags(gpmd) & _PAGE_PRESENT)) 353 if (!(pmd_flags(gpmd) & _PAGE_PRESENT))
318 return false; 354 return false;
319 355
@@ -324,8 +360,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
324 /* No shadow entry: allocate a new shadow PTE page. */ 360 /* No shadow entry: allocate a new shadow PTE page. */
325 unsigned long ptepage = get_zeroed_page(GFP_KERNEL); 361 unsigned long ptepage = get_zeroed_page(GFP_KERNEL);
326 362
327 /* This is not really the Guest's fault, but killing it is 363 /*
328 * simple for this corner case. */ 364 * This is not really the Guest's fault, but killing it is
365 * simple for this corner case.
366 */
329 if (!ptepage) { 367 if (!ptepage) {
330 kill_guest(cpu, "out of memory allocating pte page"); 368 kill_guest(cpu, "out of memory allocating pte page");
331 return false; 369 return false;
@@ -334,17 +372,23 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
334 /* We check that the Guest pmd is OK. */ 372 /* We check that the Guest pmd is OK. */
335 check_gpmd(cpu, gpmd); 373 check_gpmd(cpu, gpmd);
336 374
337 /* And we copy the flags to the shadow PMD entry. The page 375 /*
338 * number in the shadow PMD is the page we just allocated. */ 376 * And we copy the flags to the shadow PMD entry. The page
377 * number in the shadow PMD is the page we just allocated.
378 */
339 native_set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd))); 379 native_set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd)));
340 } 380 }
341 381
342 /* OK, now we look at the lower level in the Guest page table: keep its 382 /*
343 * address, because we might update it later. */ 383 * OK, now we look at the lower level in the Guest page table: keep its
384 * address, because we might update it later.
385 */
344 gpte_ptr = gpte_addr(cpu, gpmd, vaddr); 386 gpte_ptr = gpte_addr(cpu, gpmd, vaddr);
345#else 387#else
346 /* OK, now we look at the lower level in the Guest page table: keep its 388 /*
347 * address, because we might update it later. */ 389 * OK, now we look at the lower level in the Guest page table: keep its
390 * address, because we might update it later.
391 */
348 gpte_ptr = gpte_addr(cpu, gpgd, vaddr); 392 gpte_ptr = gpte_addr(cpu, gpgd, vaddr);
349#endif 393#endif
350 gpte = lgread(cpu, gpte_ptr, pte_t); 394 gpte = lgread(cpu, gpte_ptr, pte_t);
@@ -353,8 +397,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
353 if (!(pte_flags(gpte) & _PAGE_PRESENT)) 397 if (!(pte_flags(gpte) & _PAGE_PRESENT))
354 return false; 398 return false;
355 399
356 /* Check they're not trying to write to a page the Guest wants 400 /*
357 * read-only (bit 2 of errcode == write). */ 401 * Check they're not trying to write to a page the Guest wants
402 * read-only (bit 2 of errcode == write).
403 */
358 if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW)) 404 if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW))
359 return false; 405 return false;
360 406
@@ -362,8 +408,10 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
362 if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER)) 408 if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER))
363 return false; 409 return false;
364 410
365 /* Check that the Guest PTE flags are OK, and the page number is below 411 /*
366 * the pfn_limit (ie. not mapping the Launcher binary). */ 412 * Check that the Guest PTE flags are OK, and the page number is below
413 * the pfn_limit (ie. not mapping the Launcher binary).
414 */
367 check_gpte(cpu, gpte); 415 check_gpte(cpu, gpte);
368 416
369 /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */ 417 /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */
@@ -373,29 +421,40 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
373 421
374 /* Get the pointer to the shadow PTE entry we're going to set. */ 422 /* Get the pointer to the shadow PTE entry we're going to set. */
375 spte = spte_addr(cpu, *spgd, vaddr); 423 spte = spte_addr(cpu, *spgd, vaddr);
376 /* If there was a valid shadow PTE entry here before, we release it. 424
377 * This can happen with a write to a previously read-only entry. */ 425 /*
426 * If there was a valid shadow PTE entry here before, we release it.
427 * This can happen with a write to a previously read-only entry.
428 */
378 release_pte(*spte); 429 release_pte(*spte);
379 430
380 /* If this is a write, we insist that the Guest page is writable (the 431 /*
381 * final arg to gpte_to_spte()). */ 432 * If this is a write, we insist that the Guest page is writable (the
433 * final arg to gpte_to_spte()).
434 */
382 if (pte_dirty(gpte)) 435 if (pte_dirty(gpte))
383 *spte = gpte_to_spte(cpu, gpte, 1); 436 *spte = gpte_to_spte(cpu, gpte, 1);
384 else 437 else
385 /* If this is a read, don't set the "writable" bit in the page 438 /*
439 * If this is a read, don't set the "writable" bit in the page
386 * table entry, even if the Guest says it's writable. That way 440 * table entry, even if the Guest says it's writable. That way
387 * we will come back here when a write does actually occur, so 441 * we will come back here when a write does actually occur, so
388 * we can update the Guest's _PAGE_DIRTY flag. */ 442 * we can update the Guest's _PAGE_DIRTY flag.
443 */
389 native_set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0)); 444 native_set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0));
390 445
391 /* Finally, we write the Guest PTE entry back: we've set the 446 /*
392 * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags. */ 447 * Finally, we write the Guest PTE entry back: we've set the
448 * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags.
449 */
393 lgwrite(cpu, gpte_ptr, pte_t, gpte); 450 lgwrite(cpu, gpte_ptr, pte_t, gpte);
394 451
395 /* The fault is fixed, the page table is populated, the mapping 452 /*
453 * The fault is fixed, the page table is populated, the mapping
396 * manipulated, the result returned and the code complete. A small 454 * manipulated, the result returned and the code complete. A small
397 * delay and a trace of alliteration are the only indications the Guest 455 * delay and a trace of alliteration are the only indications the Guest
398 * has that a page fault occurred at all. */ 456 * has that a page fault occurred at all.
457 */
399 return true; 458 return true;
400} 459}
401 460
@@ -408,7 +467,8 @@ bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
408 * mapped, so it's overkill. 467 * mapped, so it's overkill.
409 * 468 *
410 * This is a quick version which answers the question: is this virtual address 469 * This is a quick version which answers the question: is this virtual address
411 * mapped by the shadow page tables, and is it writable? */ 470 * mapped by the shadow page tables, and is it writable?
471 */
412static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr) 472static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr)
413{ 473{
414 pgd_t *spgd; 474 pgd_t *spgd;
@@ -428,16 +488,20 @@ static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr)
428 return false; 488 return false;
429#endif 489#endif
430 490
431 /* Check the flags on the pte entry itself: it must be present and 491 /*
432 * writable. */ 492 * Check the flags on the pte entry itself: it must be present and
493 * writable.
494 */
433 flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr))); 495 flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr)));
434 496
435 return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW); 497 return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW);
436} 498}
437 499
438/* So, when pin_stack_pages() asks us to pin a page, we check if it's already 500/*
501 * So, when pin_stack_pages() asks us to pin a page, we check if it's already
439 * in the page tables, and if not, we call demand_page() with error code 2 502 * in the page tables, and if not, we call demand_page() with error code 2
440 * (meaning "write"). */ 503 * (meaning "write").
504 */
441void pin_page(struct lg_cpu *cpu, unsigned long vaddr) 505void pin_page(struct lg_cpu *cpu, unsigned long vaddr)
442{ 506{
443 if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2)) 507 if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2))
@@ -485,9 +549,11 @@ static void release_pgd(pgd_t *spgd)
485 /* If the entry's not present, there's nothing to release. */ 549 /* If the entry's not present, there's nothing to release. */
486 if (pgd_flags(*spgd) & _PAGE_PRESENT) { 550 if (pgd_flags(*spgd) & _PAGE_PRESENT) {
487 unsigned int i; 551 unsigned int i;
488 /* Converting the pfn to find the actual PTE page is easy: turn 552 /*
553 * Converting the pfn to find the actual PTE page is easy: turn
489 * the page number into a physical address, then convert to a 554 * the page number into a physical address, then convert to a
490 * virtual address (easy for kernel pages like this one). */ 555 * virtual address (easy for kernel pages like this one).
556 */
491 pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); 557 pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT);
492 /* For each entry in the page, we might need to release it. */ 558 /* For each entry in the page, we might need to release it. */
493 for (i = 0; i < PTRS_PER_PTE; i++) 559 for (i = 0; i < PTRS_PER_PTE; i++)
@@ -499,9 +565,12 @@ static void release_pgd(pgd_t *spgd)
499 } 565 }
500} 566}
501#endif 567#endif
502/*H:445 We saw flush_user_mappings() twice: once from the flush_user_mappings() 568
569/*H:445
570 * We saw flush_user_mappings() twice: once from the flush_user_mappings()
503 * hypercall and once in new_pgdir() when we re-used a top-level pgdir page. 571 * hypercall and once in new_pgdir() when we re-used a top-level pgdir page.
504 * It simply releases every PTE page from 0 up to the Guest's kernel address. */ 572 * It simply releases every PTE page from 0 up to the Guest's kernel address.
573 */
505static void flush_user_mappings(struct lguest *lg, int idx) 574static void flush_user_mappings(struct lguest *lg, int idx)
506{ 575{
507 unsigned int i; 576 unsigned int i;
@@ -510,10 +579,12 @@ static void flush_user_mappings(struct lguest *lg, int idx)
510 release_pgd(lg->pgdirs[idx].pgdir + i); 579 release_pgd(lg->pgdirs[idx].pgdir + i);
511} 580}
512 581
513/*H:440 (v) Flushing (throwing away) page tables, 582/*H:440
583 * (v) Flushing (throwing away) page tables,
514 * 584 *
515 * The Guest has a hypercall to throw away the page tables: it's used when a 585 * The Guest has a hypercall to throw away the page tables: it's used when a
516 * large number of mappings have been changed. */ 586 * large number of mappings have been changed.
587 */
517void guest_pagetable_flush_user(struct lg_cpu *cpu) 588void guest_pagetable_flush_user(struct lg_cpu *cpu)
518{ 589{
519 /* Drop the userspace part of the current page table. */ 590 /* Drop the userspace part of the current page table. */
@@ -551,9 +622,11 @@ unsigned long guest_pa(struct lg_cpu *cpu, unsigned long vaddr)
551 return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK); 622 return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK);
552} 623}
553 624
554/* We keep several page tables. This is a simple routine to find the page 625/*
626 * We keep several page tables. This is a simple routine to find the page
555 * table (if any) corresponding to this top-level address the Guest has given 627 * table (if any) corresponding to this top-level address the Guest has given
556 * us. */ 628 * us.
629 */
557static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable) 630static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable)
558{ 631{
559 unsigned int i; 632 unsigned int i;
@@ -563,9 +636,11 @@ static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable)
563 return i; 636 return i;
564} 637}
565 638
566/*H:435 And this is us, creating the new page directory. If we really do 639/*H:435
640 * And this is us, creating the new page directory. If we really do
567 * allocate a new one (and so the kernel parts are not there), we set 641 * allocate a new one (and so the kernel parts are not there), we set
568 * blank_pgdir. */ 642 * blank_pgdir.
643 */
569static unsigned int new_pgdir(struct lg_cpu *cpu, 644static unsigned int new_pgdir(struct lg_cpu *cpu,
570 unsigned long gpgdir, 645 unsigned long gpgdir,
571 int *blank_pgdir) 646 int *blank_pgdir)
@@ -575,8 +650,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu,
575 pmd_t *pmd_table; 650 pmd_t *pmd_table;
576#endif 651#endif
577 652
578 /* We pick one entry at random to throw out. Choosing the Least 653 /*
579 * Recently Used might be better, but this is easy. */ 654 * We pick one entry at random to throw out. Choosing the Least
655 * Recently Used might be better, but this is easy.
656 */
580 next = random32() % ARRAY_SIZE(cpu->lg->pgdirs); 657 next = random32() % ARRAY_SIZE(cpu->lg->pgdirs);
581 /* If it's never been allocated at all before, try now. */ 658 /* If it's never been allocated at all before, try now. */
582 if (!cpu->lg->pgdirs[next].pgdir) { 659 if (!cpu->lg->pgdirs[next].pgdir) {
@@ -587,8 +664,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu,
587 next = cpu->cpu_pgd; 664 next = cpu->cpu_pgd;
588 else { 665 else {
589#ifdef CONFIG_X86_PAE 666#ifdef CONFIG_X86_PAE
590 /* In PAE mode, allocate a pmd page and populate the 667 /*
591 * last pgd entry. */ 668 * In PAE mode, allocate a pmd page and populate the
669 * last pgd entry.
670 */
592 pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL); 671 pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL);
593 if (!pmd_table) { 672 if (!pmd_table) {
594 free_page((long)cpu->lg->pgdirs[next].pgdir); 673 free_page((long)cpu->lg->pgdirs[next].pgdir);
@@ -598,8 +677,10 @@ static unsigned int new_pgdir(struct lg_cpu *cpu,
598 set_pgd(cpu->lg->pgdirs[next].pgdir + 677 set_pgd(cpu->lg->pgdirs[next].pgdir +
599 SWITCHER_PGD_INDEX, 678 SWITCHER_PGD_INDEX,
600 __pgd(__pa(pmd_table) | _PAGE_PRESENT)); 679 __pgd(__pa(pmd_table) | _PAGE_PRESENT));
601 /* This is a blank page, so there are no kernel 680 /*
602 * mappings: caller must map the stack! */ 681 * This is a blank page, so there are no kernel
682 * mappings: caller must map the stack!
683 */
603 *blank_pgdir = 1; 684 *blank_pgdir = 1;
604 } 685 }
605#else 686#else
@@ -615,19 +696,23 @@ static unsigned int new_pgdir(struct lg_cpu *cpu,
615 return next; 696 return next;
616} 697}
617 698
618/*H:430 (iv) Switching page tables 699/*H:430
700 * (iv) Switching page tables
619 * 701 *
620 * Now we've seen all the page table setting and manipulation, let's see 702 * Now we've seen all the page table setting and manipulation, let's see
621 * what happens when the Guest changes page tables (ie. changes the top-level 703 * what happens when the Guest changes page tables (ie. changes the top-level
622 * pgdir). This occurs on almost every context switch. */ 704 * pgdir). This occurs on almost every context switch.
705 */
623void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable) 706void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable)
624{ 707{
625 int newpgdir, repin = 0; 708 int newpgdir, repin = 0;
626 709
627 /* Look to see if we have this one already. */ 710 /* Look to see if we have this one already. */
628 newpgdir = find_pgdir(cpu->lg, pgtable); 711 newpgdir = find_pgdir(cpu->lg, pgtable);
629 /* If not, we allocate or mug an existing one: if it's a fresh one, 712 /*
630 * repin gets set to 1. */ 713 * If not, we allocate or mug an existing one: if it's a fresh one,
714 * repin gets set to 1.
715 */
631 if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs)) 716 if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs))
632 newpgdir = new_pgdir(cpu, pgtable, &repin); 717 newpgdir = new_pgdir(cpu, pgtable, &repin);
633 /* Change the current pgd index to the new one. */ 718 /* Change the current pgd index to the new one. */
@@ -637,9 +722,11 @@ void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable)
637 pin_stack_pages(cpu); 722 pin_stack_pages(cpu);
638} 723}
639 724
640/*H:470 Finally, a routine which throws away everything: all PGD entries in all 725/*H:470
726 * Finally, a routine which throws away everything: all PGD entries in all
641 * the shadow page tables, including the Guest's kernel mappings. This is used 727 * the shadow page tables, including the Guest's kernel mappings. This is used
642 * when we destroy the Guest. */ 728 * when we destroy the Guest.
729 */
643static void release_all_pagetables(struct lguest *lg) 730static void release_all_pagetables(struct lguest *lg)
644{ 731{
645 unsigned int i, j; 732 unsigned int i, j;
@@ -656,8 +743,10 @@ static void release_all_pagetables(struct lguest *lg)
656 spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX; 743 spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX;
657 pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT); 744 pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT);
658 745
659 /* And release the pmd entries of that pmd page, 746 /*
660 * except for the switcher pmd. */ 747 * And release the pmd entries of that pmd page,
748 * except for the switcher pmd.
749 */
661 for (k = 0; k < SWITCHER_PMD_INDEX; k++) 750 for (k = 0; k < SWITCHER_PMD_INDEX; k++)
662 release_pmd(&pmdpage[k]); 751 release_pmd(&pmdpage[k]);
663#endif 752#endif
@@ -667,10 +756,12 @@ static void release_all_pagetables(struct lguest *lg)
667 } 756 }
668} 757}
669 758
670/* We also throw away everything when a Guest tells us it's changed a kernel 759/*
760 * We also throw away everything when a Guest tells us it's changed a kernel
671 * mapping. Since kernel mappings are in every page table, it's easiest to 761 * mapping. Since kernel mappings are in every page table, it's easiest to
672 * throw them all away. This traps the Guest in amber for a while as 762 * throw them all away. This traps the Guest in amber for a while as
673 * everything faults back in, but it's rare. */ 763 * everything faults back in, but it's rare.
764 */
674void guest_pagetable_clear_all(struct lg_cpu *cpu) 765void guest_pagetable_clear_all(struct lg_cpu *cpu)
675{ 766{
676 release_all_pagetables(cpu->lg); 767 release_all_pagetables(cpu->lg);
@@ -678,15 +769,19 @@ void guest_pagetable_clear_all(struct lg_cpu *cpu)
678 pin_stack_pages(cpu); 769 pin_stack_pages(cpu);
679} 770}
680/*:*/ 771/*:*/
681/*M:009 Since we throw away all mappings when a kernel mapping changes, our 772
773/*M:009
774 * Since we throw away all mappings when a kernel mapping changes, our
682 * performance sucks for guests using highmem. In fact, a guest with 775 * performance sucks for guests using highmem. In fact, a guest with
683 * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is 776 * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is
684 * usually slower than a Guest with less memory. 777 * usually slower than a Guest with less memory.
685 * 778 *
686 * This, of course, cannot be fixed. It would take some kind of... well, I 779 * This, of course, cannot be fixed. It would take some kind of... well, I
687 * don't know, but the term "puissant code-fu" comes to mind. :*/ 780 * don't know, but the term "puissant code-fu" comes to mind.
781:*/
688 782
689/*H:420 This is the routine which actually sets the page table entry for then 783/*H:420
784 * This is the routine which actually sets the page table entry for then
690 * "idx"'th shadow page table. 785 * "idx"'th shadow page table.
691 * 786 *
692 * Normally, we can just throw out the old entry and replace it with 0: if they 787 * Normally, we can just throw out the old entry and replace it with 0: if they
@@ -715,31 +810,36 @@ static void do_set_pte(struct lg_cpu *cpu, int idx,
715 spmd = spmd_addr(cpu, *spgd, vaddr); 810 spmd = spmd_addr(cpu, *spgd, vaddr);
716 if (pmd_flags(*spmd) & _PAGE_PRESENT) { 811 if (pmd_flags(*spmd) & _PAGE_PRESENT) {
717#endif 812#endif
718 /* Otherwise, we start by releasing 813 /* Otherwise, start by releasing the existing entry. */
719 * the existing entry. */
720 pte_t *spte = spte_addr(cpu, *spgd, vaddr); 814 pte_t *spte = spte_addr(cpu, *spgd, vaddr);
721 release_pte(*spte); 815 release_pte(*spte);
722 816
723 /* If they're setting this entry as dirty or accessed, 817 /*
724 * we might as well put that entry they've given us 818 * If they're setting this entry as dirty or accessed,
725 * in now. This shaves 10% off a 819 * we might as well put that entry they've given us in
726 * copy-on-write micro-benchmark. */ 820 * now. This shaves 10% off a copy-on-write
821 * micro-benchmark.
822 */
727 if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) { 823 if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) {
728 check_gpte(cpu, gpte); 824 check_gpte(cpu, gpte);
729 native_set_pte(spte, 825 native_set_pte(spte,
730 gpte_to_spte(cpu, gpte, 826 gpte_to_spte(cpu, gpte,
731 pte_flags(gpte) & _PAGE_DIRTY)); 827 pte_flags(gpte) & _PAGE_DIRTY));
732 } else 828 } else {
733 /* Otherwise kill it and we can demand_page() 829 /*
734 * it in later. */ 830 * Otherwise kill it and we can demand_page()
831 * it in later.
832 */
735 native_set_pte(spte, __pte(0)); 833 native_set_pte(spte, __pte(0));
834 }
736#ifdef CONFIG_X86_PAE 835#ifdef CONFIG_X86_PAE
737 } 836 }
738#endif 837#endif
739 } 838 }
740} 839}
741 840
742/*H:410 Updating a PTE entry is a little trickier. 841/*H:410
842 * Updating a PTE entry is a little trickier.
743 * 843 *
744 * We keep track of several different page tables (the Guest uses one for each 844 * We keep track of several different page tables (the Guest uses one for each
745 * process, so it makes sense to cache at least a few). Each of these have 845 * process, so it makes sense to cache at least a few). Each of these have
@@ -748,12 +848,15 @@ static void do_set_pte(struct lg_cpu *cpu, int idx,
748 * all the page tables, not just the current one. This is rare. 848 * all the page tables, not just the current one. This is rare.
749 * 849 *
750 * The benefit is that when we have to track a new page table, we can keep all 850 * The benefit is that when we have to track a new page table, we can keep all
751 * the kernel mappings. This speeds up context switch immensely. */ 851 * the kernel mappings. This speeds up context switch immensely.
852 */
752void guest_set_pte(struct lg_cpu *cpu, 853void guest_set_pte(struct lg_cpu *cpu,
753 unsigned long gpgdir, unsigned long vaddr, pte_t gpte) 854 unsigned long gpgdir, unsigned long vaddr, pte_t gpte)
754{ 855{
755 /* Kernel mappings must be changed on all top levels. Slow, but doesn't 856 /*
756 * happen often. */ 857 * Kernel mappings must be changed on all top levels. Slow, but doesn't
858 * happen often.
859 */
757 if (vaddr >= cpu->lg->kernel_address) { 860 if (vaddr >= cpu->lg->kernel_address) {
758 unsigned int i; 861 unsigned int i;
759 for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++) 862 for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++)
@@ -802,12 +905,14 @@ void guest_set_pmd(struct lguest *lg, unsigned long pmdp, u32 idx)
802} 905}
803#endif 906#endif
804 907
805/* Once we know how much memory we have we can construct simple identity 908/*
806 * (which set virtual == physical) and linear mappings 909 * Once we know how much memory we have we can construct simple identity (which
807 * which will get the Guest far enough into the boot to create its own. 910 * set virtual == physical) and linear mappings which will get the Guest far
911 * enough into the boot to create its own.
808 * 912 *
809 * We lay them out of the way, just below the initrd (which is why we need to 913 * We lay them out of the way, just below the initrd (which is why we need to
810 * know its size here). */ 914 * know its size here).
915 */
811static unsigned long setup_pagetables(struct lguest *lg, 916static unsigned long setup_pagetables(struct lguest *lg,
812 unsigned long mem, 917 unsigned long mem,
813 unsigned long initrd_size) 918 unsigned long initrd_size)
@@ -825,8 +930,10 @@ static unsigned long setup_pagetables(struct lguest *lg,
825 unsigned int phys_linear; 930 unsigned int phys_linear;
826#endif 931#endif
827 932
828 /* We have mapped_pages frames to map, so we need 933 /*
829 * linear_pages page tables to map them. */ 934 * We have mapped_pages frames to map, so we need linear_pages page
935 * tables to map them.
936 */
830 mapped_pages = mem / PAGE_SIZE; 937 mapped_pages = mem / PAGE_SIZE;
831 linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE; 938 linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE;
832 939
@@ -839,8 +946,10 @@ static unsigned long setup_pagetables(struct lguest *lg,
839#ifdef CONFIG_X86_PAE 946#ifdef CONFIG_X86_PAE
840 pmds = (void *)linear - PAGE_SIZE; 947 pmds = (void *)linear - PAGE_SIZE;
841#endif 948#endif
842 /* Linear mapping is easy: put every page's address into the 949 /*
843 * mapping in order. */ 950 * Linear mapping is easy: put every page's address into the
951 * mapping in order.
952 */
844 for (i = 0; i < mapped_pages; i++) { 953 for (i = 0; i < mapped_pages; i++) {
845 pte_t pte; 954 pte_t pte;
846 pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER)); 955 pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER));
@@ -848,8 +957,10 @@ static unsigned long setup_pagetables(struct lguest *lg,
848 return -EFAULT; 957 return -EFAULT;
849 } 958 }
850 959
851 /* The top level points to the linear page table pages above. 960 /*
852 * We setup the identity and linear mappings here. */ 961 * The top level points to the linear page table pages above.
962 * We setup the identity and linear mappings here.
963 */
853#ifdef CONFIG_X86_PAE 964#ifdef CONFIG_X86_PAE
854 for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD; 965 for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD;
855 i += PTRS_PER_PTE, j++) { 966 i += PTRS_PER_PTE, j++) {
@@ -880,15 +991,19 @@ static unsigned long setup_pagetables(struct lguest *lg,
880 } 991 }
881#endif 992#endif
882 993
883 /* We return the top level (guest-physical) address: remember where 994 /*
884 * this is. */ 995 * We return the top level (guest-physical) address: remember where
996 * this is.
997 */
885 return (unsigned long)pgdir - mem_base; 998 return (unsigned long)pgdir - mem_base;
886} 999}
887 1000
888/*H:500 (vii) Setting up the page tables initially. 1001/*H:500
1002 * (vii) Setting up the page tables initially.
889 * 1003 *
890 * When a Guest is first created, the Launcher tells us where the toplevel of 1004 * When a Guest is first created, the Launcher tells us where the toplevel of
891 * its first page table is. We set some things up here: */ 1005 * its first page table is. We set some things up here:
1006 */
892int init_guest_pagetable(struct lguest *lg) 1007int init_guest_pagetable(struct lguest *lg)
893{ 1008{
894 u64 mem; 1009 u64 mem;
@@ -898,14 +1013,18 @@ int init_guest_pagetable(struct lguest *lg)
898 pgd_t *pgd; 1013 pgd_t *pgd;
899 pmd_t *pmd_table; 1014 pmd_t *pmd_table;
900#endif 1015#endif
901 /* Get the Guest memory size and the ramdisk size from the boot header 1016 /*
902 * located at lg->mem_base (Guest address 0). */ 1017 * Get the Guest memory size and the ramdisk size from the boot header
1018 * located at lg->mem_base (Guest address 0).
1019 */
903 if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem)) 1020 if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem))
904 || get_user(initrd_size, &boot->hdr.ramdisk_size)) 1021 || get_user(initrd_size, &boot->hdr.ramdisk_size))
905 return -EFAULT; 1022 return -EFAULT;
906 1023
907 /* We start on the first shadow page table, and give it a blank PGD 1024 /*
908 * page. */ 1025 * We start on the first shadow page table, and give it a blank PGD
1026 * page.
1027 */
909 lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size); 1028 lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size);
910 if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir)) 1029 if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir))
911 return lg->pgdirs[0].gpgdir; 1030 return lg->pgdirs[0].gpgdir;
@@ -931,17 +1050,21 @@ void page_table_guest_data_init(struct lg_cpu *cpu)
931 /* We get the kernel address: above this is all kernel memory. */ 1050 /* We get the kernel address: above this is all kernel memory. */
932 if (get_user(cpu->lg->kernel_address, 1051 if (get_user(cpu->lg->kernel_address,
933 &cpu->lg->lguest_data->kernel_address) 1052 &cpu->lg->lguest_data->kernel_address)
934 /* We tell the Guest that it can't use the top 2 or 4 MB 1053 /*
935 * of virtual addresses used by the Switcher. */ 1054 * We tell the Guest that it can't use the top 2 or 4 MB
1055 * of virtual addresses used by the Switcher.
1056 */
936 || put_user(RESERVE_MEM * 1024 * 1024, 1057 || put_user(RESERVE_MEM * 1024 * 1024,
937 &cpu->lg->lguest_data->reserve_mem) 1058 &cpu->lg->lguest_data->reserve_mem)
938 || put_user(cpu->lg->pgdirs[0].gpgdir, 1059 || put_user(cpu->lg->pgdirs[0].gpgdir,
939 &cpu->lg->lguest_data->pgdir)) 1060 &cpu->lg->lguest_data->pgdir))
940 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); 1061 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
941 1062
942 /* In flush_user_mappings() we loop from 0 to 1063 /*
1064 * In flush_user_mappings() we loop from 0 to
943 * "pgd_index(lg->kernel_address)". This assumes it won't hit the 1065 * "pgd_index(lg->kernel_address)". This assumes it won't hit the
944 * Switcher mappings, so check that now. */ 1066 * Switcher mappings, so check that now.
1067 */
945#ifdef CONFIG_X86_PAE 1068#ifdef CONFIG_X86_PAE
946 if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX && 1069 if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX &&
947 pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX) 1070 pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX)
@@ -964,12 +1087,14 @@ void free_guest_pagetable(struct lguest *lg)
964 free_page((long)lg->pgdirs[i].pgdir); 1087 free_page((long)lg->pgdirs[i].pgdir);
965} 1088}
966 1089
967/*H:480 (vi) Mapping the Switcher when the Guest is about to run. 1090/*H:480
1091 * (vi) Mapping the Switcher when the Guest is about to run.
968 * 1092 *
969 * The Switcher and the two pages for this CPU need to be visible in the 1093 * The Switcher and the two pages for this CPU need to be visible in the
970 * Guest (and not the pages for other CPUs). We have the appropriate PTE pages 1094 * Guest (and not the pages for other CPUs). We have the appropriate PTE pages
971 * for each CPU already set up, we just need to hook them in now we know which 1095 * for each CPU already set up, we just need to hook them in now we know which
972 * Guest is about to run on this CPU. */ 1096 * Guest is about to run on this CPU.
1097 */
973void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages) 1098void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages)
974{ 1099{
975 pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages); 1100 pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages);
@@ -990,20 +1115,24 @@ void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages)
990#else 1115#else
991 pgd_t switcher_pgd; 1116 pgd_t switcher_pgd;
992 1117
993 /* Make the last PGD entry for this Guest point to the Switcher's PTE 1118 /*
994 * page for this CPU (with appropriate flags). */ 1119 * Make the last PGD entry for this Guest point to the Switcher's PTE
1120 * page for this CPU (with appropriate flags).
1121 */
995 switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC); 1122 switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC);
996 1123
997 cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd; 1124 cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd;
998 1125
999#endif 1126#endif
1000 /* We also change the Switcher PTE page. When we're running the Guest, 1127 /*
1128 * We also change the Switcher PTE page. When we're running the Guest,
1001 * we want the Guest's "regs" page to appear where the first Switcher 1129 * we want the Guest's "regs" page to appear where the first Switcher
1002 * page for this CPU is. This is an optimization: when the Switcher 1130 * page for this CPU is. This is an optimization: when the Switcher
1003 * saves the Guest registers, it saves them into the first page of this 1131 * saves the Guest registers, it saves them into the first page of this
1004 * CPU's "struct lguest_pages": if we make sure the Guest's register 1132 * CPU's "struct lguest_pages": if we make sure the Guest's register
1005 * page is already mapped there, we don't have to copy them out 1133 * page is already mapped there, we don't have to copy them out
1006 * again. */ 1134 * again.
1135 */
1007 pfn = __pa(cpu->regs_page) >> PAGE_SHIFT; 1136 pfn = __pa(cpu->regs_page) >> PAGE_SHIFT;
1008 native_set_pte(&regs_pte, pfn_pte(pfn, PAGE_KERNEL)); 1137 native_set_pte(&regs_pte, pfn_pte(pfn, PAGE_KERNEL));
1009 native_set_pte(&switcher_pte_page[pte_index((unsigned long)pages)], 1138 native_set_pte(&switcher_pte_page[pte_index((unsigned long)pages)],
@@ -1019,10 +1148,12 @@ static void free_switcher_pte_pages(void)
1019 free_page((long)switcher_pte_page(i)); 1148 free_page((long)switcher_pte_page(i));
1020} 1149}
1021 1150
1022/*H:520 Setting up the Switcher PTE page for given CPU is fairly easy, given 1151/*H:520
1152 * Setting up the Switcher PTE page for given CPU is fairly easy, given
1023 * the CPU number and the "struct page"s for the Switcher code itself. 1153 * the CPU number and the "struct page"s for the Switcher code itself.
1024 * 1154 *
1025 * Currently the Switcher is less than a page long, so "pages" is always 1. */ 1155 * Currently the Switcher is less than a page long, so "pages" is always 1.
1156 */
1026static __init void populate_switcher_pte_page(unsigned int cpu, 1157static __init void populate_switcher_pte_page(unsigned int cpu,
1027 struct page *switcher_page[], 1158 struct page *switcher_page[],
1028 unsigned int pages) 1159 unsigned int pages)
@@ -1043,13 +1174,16 @@ static __init void populate_switcher_pte_page(unsigned int cpu,
1043 native_set_pte(&pte[i], pfn_pte(page_to_pfn(switcher_page[i]), 1174 native_set_pte(&pte[i], pfn_pte(page_to_pfn(switcher_page[i]),
1044 __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW))); 1175 __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW)));
1045 1176
1046 /* The second page contains the "struct lguest_ro_state", and is 1177 /*
1047 * read-only. */ 1178 * The second page contains the "struct lguest_ro_state", and is
1179 * read-only.
1180 */
1048 native_set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]), 1181 native_set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]),
1049 __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED))); 1182 __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED)));
1050} 1183}
1051 1184
1052/* We've made it through the page table code. Perhaps our tired brains are 1185/*
1186 * We've made it through the page table code. Perhaps our tired brains are
1053 * still processing the details, or perhaps we're simply glad it's over. 1187 * still processing the details, or perhaps we're simply glad it's over.
1054 * 1188 *
1055 * If nothing else, note that all this complexity in juggling shadow page tables 1189 * If nothing else, note that all this complexity in juggling shadow page tables
@@ -1058,10 +1192,13 @@ static __init void populate_switcher_pte_page(unsigned int cpu,
1058 * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD 1192 * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD
1059 * have implemented shadow page table support directly into hardware. 1193 * have implemented shadow page table support directly into hardware.
1060 * 1194 *
1061 * There is just one file remaining in the Host. */ 1195 * There is just one file remaining in the Host.
1196 */
1062 1197
1063/*H:510 At boot or module load time, init_pagetables() allocates and populates 1198/*H:510
1064 * the Switcher PTE page for each CPU. */ 1199 * At boot or module load time, init_pagetables() allocates and populates
1200 * the Switcher PTE page for each CPU.
1201 */
1065__init int init_pagetables(struct page **switcher_page, unsigned int pages) 1202__init int init_pagetables(struct page **switcher_page, unsigned int pages)
1066{ 1203{
1067 unsigned int i; 1204 unsigned int i;
diff --git a/drivers/lguest/segments.c b/drivers/lguest/segments.c
index 482ed5a18750..951c57b0a7e0 100644
--- a/drivers/lguest/segments.c
+++ b/drivers/lguest/segments.c
@@ -1,4 +1,5 @@
1/*P:600 The x86 architecture has segments, which involve a table of descriptors 1/*P:600
2 * The x86 architecture has segments, which involve a table of descriptors
2 * which can be used to do funky things with virtual address interpretation. 3 * which can be used to do funky things with virtual address interpretation.
3 * We originally used to use segments so the Guest couldn't alter the 4 * We originally used to use segments so the Guest couldn't alter the
4 * Guest<->Host Switcher, and then we had to trim Guest segments, and restore 5 * Guest<->Host Switcher, and then we had to trim Guest segments, and restore
@@ -8,7 +9,8 @@
8 * 9 *
9 * In these modern times, the segment handling code consists of simple sanity 10 * In these modern times, the segment handling code consists of simple sanity
10 * checks, and the worst you'll experience reading this code is butterfly-rash 11 * checks, and the worst you'll experience reading this code is butterfly-rash
11 * from frolicking through its parklike serenity. :*/ 12 * from frolicking through its parklike serenity.
13:*/
12#include "lg.h" 14#include "lg.h"
13 15
14/*H:600 16/*H:600
@@ -41,10 +43,12 @@
41 * begin. 43 * begin.
42 */ 44 */
43 45
44/* There are several entries we don't let the Guest set. The TSS entry is the 46/*
47 * There are several entries we don't let the Guest set. The TSS entry is the
45 * "Task State Segment" which controls all kinds of delicate things. The 48 * "Task State Segment" which controls all kinds of delicate things. The
46 * LGUEST_CS and LGUEST_DS entries are reserved for the Switcher, and the 49 * LGUEST_CS and LGUEST_DS entries are reserved for the Switcher, and the
47 * the Guest can't be trusted to deal with double faults. */ 50 * the Guest can't be trusted to deal with double faults.
51 */
48static bool ignored_gdt(unsigned int num) 52static 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 */
61static void fixup_gdt_table(struct lg_cpu *cpu, unsigned start, unsigned end) 67static 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 */
92void setup_default_gdt_entries(struct lguest_ro_state *state) 106void 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 */
112void setup_guest_gdt(struct lg_cpu *cpu) 130void 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 */
125void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt) 146void 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 */
136void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt) 159void 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 */
149void load_guest_gdt_entry(struct lg_cpu *cpu, u32 num, u32 lo, u32 hi) 176void 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 */
169void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls) 202void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls)
170{ 203{
171 struct desc_struct *tls = &cpu->arch.gdt[GDT_ENTRY_TLS_MIN]; 204 struct desc_struct *tls = &cpu->arch.gdt[GDT_ENTRY_TLS_MIN];
@@ -175,7 +208,6 @@ void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls)
175 /* Note that just the TLS entries have changed. */ 208 /* Note that just the TLS entries have changed. */
176 cpu->changed |= CHANGED_GDT_TLS; 209 cpu->changed |= CHANGED_GDT_TLS;
177} 210}
178/*:*/
179 211
180/*H:660 212/*H:660
181 * With this, we have finished the Host. 213 * With this, we have finished the Host.
diff --git a/drivers/lguest/x86/core.c b/drivers/lguest/x86/core.c
index eaf722fe309a..96f7d88ec7f8 100644
--- a/drivers/lguest/x86/core.c
+++ b/drivers/lguest/x86/core.c
@@ -17,13 +17,15 @@
17 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */ 19 */
20/*P:450 This file contains the x86-specific lguest code. It used to be all 20/*P:450
21 * This file contains the x86-specific lguest code. It used to be all
21 * mixed in with drivers/lguest/core.c but several foolhardy code slashers 22 * mixed in with drivers/lguest/core.c but several foolhardy code slashers
22 * wrestled most of the dependencies out to here in preparation for porting 23 * wrestled most of the dependencies out to here in preparation for porting
23 * lguest to other architectures (see what I mean by foolhardy?). 24 * lguest to other architectures (see what I mean by foolhardy?).
24 * 25 *
25 * This also contains a couple of non-obvious setup and teardown pieces which 26 * This also contains a couple of non-obvious setup and teardown pieces which
26 * were implemented after days of debugging pain. :*/ 27 * were implemented after days of debugging pain.
28:*/
27#include <linux/kernel.h> 29#include <linux/kernel.h>
28#include <linux/start_kernel.h> 30#include <linux/start_kernel.h>
29#include <linux/string.h> 31#include <linux/string.h>
@@ -82,25 +84,33 @@ static DEFINE_PER_CPU(struct lg_cpu *, last_cpu);
82 */ 84 */
83static void copy_in_guest_info(struct lg_cpu *cpu, struct lguest_pages *pages) 85static void copy_in_guest_info(struct lg_cpu *cpu, struct lguest_pages *pages)
84{ 86{
85 /* Copying all this data can be quite expensive. We usually run the 87 /*
88 * Copying all this data can be quite expensive. We usually run the
86 * same Guest we ran last time (and that Guest hasn't run anywhere else 89 * same Guest we ran last time (and that Guest hasn't run anywhere else
87 * meanwhile). If that's not the case, we pretend everything in the 90 * meanwhile). If that's not the case, we pretend everything in the
88 * Guest has changed. */ 91 * Guest has changed.
92 */
89 if (__get_cpu_var(last_cpu) != cpu || cpu->last_pages != pages) { 93 if (__get_cpu_var(last_cpu) != cpu || cpu->last_pages != pages) {
90 __get_cpu_var(last_cpu) = cpu; 94 __get_cpu_var(last_cpu) = cpu;
91 cpu->last_pages = pages; 95 cpu->last_pages = pages;
92 cpu->changed = CHANGED_ALL; 96 cpu->changed = CHANGED_ALL;
93 } 97 }
94 98
95 /* These copies are pretty cheap, so we do them unconditionally: */ 99 /*
96 /* Save the current Host top-level page directory. */ 100 * These copies are pretty cheap, so we do them unconditionally: */
101 /* Save the current Host top-level page directory.
102 */
97 pages->state.host_cr3 = __pa(current->mm->pgd); 103 pages->state.host_cr3 = __pa(current->mm->pgd);
98 /* Set up the Guest's page tables to see this CPU's pages (and no 104 /*
99 * other CPU's pages). */ 105 * Set up the Guest's page tables to see this CPU's pages (and no
106 * other CPU's pages).
107 */
100 map_switcher_in_guest(cpu, pages); 108 map_switcher_in_guest(cpu, pages);
101 /* Set up the two "TSS" members which tell the CPU what stack to use 109 /*
110 * Set up the two "TSS" members which tell the CPU what stack to use
102 * for traps which do directly into the Guest (ie. traps at privilege 111 * for traps which do directly into the Guest (ie. traps at privilege
103 * level 1). */ 112 * level 1).
113 */
104 pages->state.guest_tss.sp1 = cpu->esp1; 114 pages->state.guest_tss.sp1 = cpu->esp1;
105 pages->state.guest_tss.ss1 = cpu->ss1; 115 pages->state.guest_tss.ss1 = cpu->ss1;
106 116
@@ -125,40 +135,53 @@ static void run_guest_once(struct lg_cpu *cpu, struct lguest_pages *pages)
125 /* This is a dummy value we need for GCC's sake. */ 135 /* This is a dummy value we need for GCC's sake. */
126 unsigned int clobber; 136 unsigned int clobber;
127 137
128 /* Copy the guest-specific information into this CPU's "struct 138 /*
129 * lguest_pages". */ 139 * Copy the guest-specific information into this CPU's "struct
140 * lguest_pages".
141 */
130 copy_in_guest_info(cpu, pages); 142 copy_in_guest_info(cpu, pages);
131 143
132 /* Set the trap number to 256 (impossible value). If we fault while 144 /*
145 * Set the trap number to 256 (impossible value). If we fault while
133 * switching to the Guest (bad segment registers or bug), this will 146 * switching to the Guest (bad segment registers or bug), this will
134 * cause us to abort the Guest. */ 147 * cause us to abort the Guest.
148 */
135 cpu->regs->trapnum = 256; 149 cpu->regs->trapnum = 256;
136 150
137 /* Now: we push the "eflags" register on the stack, then do an "lcall". 151 /*
152 * Now: we push the "eflags" register on the stack, then do an "lcall".
138 * This is how we change from using the kernel code segment to using 153 * This is how we change from using the kernel code segment to using
139 * the dedicated lguest code segment, as well as jumping into the 154 * the dedicated lguest code segment, as well as jumping into the
140 * Switcher. 155 * Switcher.
141 * 156 *
142 * The lcall also pushes the old code segment (KERNEL_CS) onto the 157 * The lcall also pushes the old code segment (KERNEL_CS) onto the
143 * stack, then the address of this call. This stack layout happens to 158 * stack, then the address of this call. This stack layout happens to
144 * exactly match the stack layout created by an interrupt... */ 159 * exactly match the stack layout created by an interrupt...
160 */
145 asm volatile("pushf; lcall *lguest_entry" 161 asm volatile("pushf; lcall *lguest_entry"
146 /* This is how we tell GCC that %eax ("a") and %ebx ("b") 162 /*
147 * are changed by this routine. The "=" means output. */ 163 * This is how we tell GCC that %eax ("a") and %ebx ("b")
164 * are changed by this routine. The "=" means output.
165 */
148 : "=a"(clobber), "=b"(clobber) 166 : "=a"(clobber), "=b"(clobber)
149 /* %eax contains the pages pointer. ("0" refers to the 167 /*
168 * %eax contains the pages pointer. ("0" refers to the
150 * 0-th argument above, ie "a"). %ebx contains the 169 * 0-th argument above, ie "a"). %ebx contains the
151 * physical address of the Guest's top-level page 170 * physical address of the Guest's top-level page
152 * directory. */ 171 * directory.
172 */
153 : "0"(pages), "1"(__pa(cpu->lg->pgdirs[cpu->cpu_pgd].pgdir)) 173 : "0"(pages), "1"(__pa(cpu->lg->pgdirs[cpu->cpu_pgd].pgdir))
154 /* We tell gcc that all these registers could change, 174 /*
175 * We tell gcc that all these registers could change,
155 * which means we don't have to save and restore them in 176 * which means we don't have to save and restore them in
156 * the Switcher. */ 177 * the Switcher.
178 */
157 : "memory", "%edx", "%ecx", "%edi", "%esi"); 179 : "memory", "%edx", "%ecx", "%edi", "%esi");
158} 180}
159/*:*/ 181/*:*/
160 182
161/*M:002 There are hooks in the scheduler which we can register to tell when we 183/*M:002
184 * There are hooks in the scheduler which we can register to tell when we
162 * get kicked off the CPU (preempt_notifier_register()). This would allow us 185 * get kicked off the CPU (preempt_notifier_register()). This would allow us
163 * to lazily disable SYSENTER which would regain some performance, and should 186 * to lazily disable SYSENTER which would regain some performance, and should
164 * also simplify copy_in_guest_info(). Note that we'd still need to restore 187 * also simplify copy_in_guest_info(). Note that we'd still need to restore
@@ -166,56 +189,72 @@ static void run_guest_once(struct lg_cpu *cpu, struct lguest_pages *pages)
166 * 189 *
167 * We could also try using this hooks for PGE, but that might be too expensive. 190 * We could also try using this hooks for PGE, but that might be too expensive.
168 * 191 *
169 * The hooks were designed for KVM, but we can also put them to good use. :*/ 192 * The hooks were designed for KVM, but we can also put them to good use.
193:*/
170 194
171/*H:040 This is the i386-specific code to setup and run the Guest. Interrupts 195/*H:040
172 * are disabled: we own the CPU. */ 196 * This is the i386-specific code to setup and run the Guest. Interrupts
197 * are disabled: we own the CPU.
198 */
173void lguest_arch_run_guest(struct lg_cpu *cpu) 199void 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 */
229static int emulate_insn(struct lg_cpu *cpu) 269static 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 */
319static void rewrite_hypercall(struct lg_cpu *cpu) 370static 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:*/
434static void adjust_pge(void *on) 510static 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 */
444void __init lguest_arch_host_init(void) 522void __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 */
632void lguest_arch_setup_regs(struct lg_cpu *cpu, unsigned long start) 743void lguest_arch_setup_regs(struct lg_cpu *cpu, unsigned long start)
633{ 744{
634 struct lguest_regs *regs = cpu->regs; 745 struct lguest_regs *regs = cpu->regs;
635 746
636 /* There are four "segment" registers which the Guest needs to boot: 747 /*
748 * There are four "segment" registers which the Guest needs to boot:
637 * The "code segment" register (cs) refers to the kernel code segment 749 * The "code segment" register (cs) refers to the kernel code segment
638 * __KERNEL_CS, and the "data", "extra" and "stack" segment registers 750 * __KERNEL_CS, and the "data", "extra" and "stack" segment registers
639 * refer to the kernel data segment __KERNEL_DS. 751 * refer to the kernel data segment __KERNEL_DS.
640 * 752 *
641 * The privilege level is packed into the lower bits. The Guest runs 753 * The privilege level is packed into the lower bits. The Guest runs
642 * at privilege level 1 (GUEST_PL).*/ 754 * at privilege level 1 (GUEST_PL).
755 */
643 regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; 756 regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL;
644 regs->cs = __KERNEL_CS|GUEST_PL; 757 regs->cs = __KERNEL_CS|GUEST_PL;
645 758
646 /* The "eflags" register contains miscellaneous flags. Bit 1 (0x002) 759 /*
760 * The "eflags" register contains miscellaneous flags. Bit 1 (0x002)
647 * is supposed to always be "1". Bit 9 (0x200) controls whether 761 * is supposed to always be "1". Bit 9 (0x200) controls whether
648 * interrupts are enabled. We always leave interrupts enabled while 762 * interrupts are enabled. We always leave interrupts enabled while
649 * running the Guest. */ 763 * running the Guest.
764 */
650 regs->eflags = X86_EFLAGS_IF | 0x2; 765 regs->eflags = X86_EFLAGS_IF | 0x2;
651 766
652 /* The "Extended Instruction Pointer" register says where the Guest is 767 /*
653 * running. */ 768 * The "Extended Instruction Pointer" register says where the Guest is
769 * running.
770 */
654 regs->eip = start; 771 regs->eip = start;
655 772
656 /* %esi points to our boot information, at physical address 0, so don't 773 /*
657 * touch it. */ 774 * %esi points to our boot information, at physical address 0, so don't
775 * touch it.
776 */
658 777
659 /* There are a couple of GDT entries the Guest expects when first 778 /* There are a couple of GDT entries the Guest expects at boot. */
660 * booting. */
661 setup_guest_gdt(cpu); 779 setup_guest_gdt(cpu);
662} 780}
diff --git a/drivers/lguest/x86/switcher_32.S b/drivers/lguest/x86/switcher_32.S
index 3fc15318a80f..6dec09793836 100644
--- a/drivers/lguest/x86/switcher_32.S
+++ b/drivers/lguest/x86/switcher_32.S
@@ -1,12 +1,15 @@
1/*P:900 This is the Switcher: code which sits at 0xFFC00000 astride both the 1/*P:900
2 * This is the Switcher: code which sits at 0xFFC00000 astride both the
2 * Host and Guest to do the low-level Guest<->Host switch. It is as simple as 3 * Host and Guest to do the low-level Guest<->Host switch. It is as simple as
3 * it can be made, but it's naturally very specific to x86. 4 * it can be made, but it's naturally very specific to x86.
4 * 5 *
5 * You have now completed Preparation. If this has whet your appetite; if you 6 * You have now completed Preparation. If this has whet your appetite; if you
6 * are feeling invigorated and refreshed then the next, more challenging stage 7 * are feeling invigorated and refreshed then the next, more challenging stage
7 * can be found in "make Guest". :*/ 8 * can be found in "make Guest".
9 :*/
8 10
9/*M:012 Lguest is meant to be simple: my rule of thumb is that 1% more LOC must 11/*M:012
12 * Lguest is meant to be simple: my rule of thumb is that 1% more LOC must
10 * gain at least 1% more performance. Since neither LOC nor performance can be 13 * gain at least 1% more performance. Since neither LOC nor performance can be
11 * measured beforehand, it generally means implementing a feature then deciding 14 * measured beforehand, it generally means implementing a feature then deciding
12 * if it's worth it. And once it's implemented, who can say no? 15 * if it's worth it. And once it's implemented, who can say no?
@@ -31,11 +34,14 @@
31 * Host (which is actually really easy). 34 * Host (which is actually really easy).
32 * 35 *
33 * Two questions remain. Would the performance gain outweigh the complexity? 36 * Two questions remain. Would the performance gain outweigh the complexity?
34 * And who would write the verse documenting it? :*/ 37 * And who would write the verse documenting it?
38:*/
35 39
36/*M:011 Lguest64 handles NMI. This gave me NMI envy (until I looked at their 40/*M:011
41 * Lguest64 handles NMI. This gave me NMI envy (until I looked at their
37 * code). It's worth doing though, since it would let us use oprofile in the 42 * code). It's worth doing though, since it would let us use oprofile in the
38 * Host when a Guest is running. :*/ 43 * Host when a Guest is running.
44:*/
39 45
40/*S:100 46/*S:100
41 * Welcome to the Switcher itself! 47 * Welcome to the Switcher itself!