diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2007-07-26 13:41:04 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-26 14:35:17 -0400 |
commit | bff672e630a015d5b54c8bfb16160b7edc39a57c (patch) | |
tree | 3af06baacb76809234a3e71033d14b7ed769dbd8 /drivers/lguest/hypercalls.c | |
parent | dde797899ac17ebb812b7566044124d785e98dc7 (diff) |
lguest: documentation V: Host
Documentation: The Host
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/lguest/hypercalls.c')
-rw-r--r-- | drivers/lguest/hypercalls.c | 118 |
1 files changed, 108 insertions, 10 deletions
diff --git a/drivers/lguest/hypercalls.c b/drivers/lguest/hypercalls.c index fb546b046445..7a5299f9679d 100644 --- a/drivers/lguest/hypercalls.c +++ b/drivers/lguest/hypercalls.c | |||
@@ -28,37 +28,63 @@ | |||
28 | #include <irq_vectors.h> | 28 | #include <irq_vectors.h> |
29 | #include "lg.h" | 29 | #include "lg.h" |
30 | 30 | ||
31 | /*H:120 This is the core hypercall routine: where the Guest gets what it | ||
32 | * wants. Or gets killed. Or, in the case of LHCALL_CRASH, both. | ||
33 | * | ||
34 | * Remember from the Guest: %eax == which call to make, and the arguments are | ||
35 | * packed into %edx, %ebx and %ecx if needed. */ | ||
31 | static void do_hcall(struct lguest *lg, struct lguest_regs *regs) | 36 | static void do_hcall(struct lguest *lg, struct lguest_regs *regs) |
32 | { | 37 | { |
33 | switch (regs->eax) { | 38 | switch (regs->eax) { |
34 | case LHCALL_FLUSH_ASYNC: | 39 | case LHCALL_FLUSH_ASYNC: |
40 | /* This call does nothing, except by breaking out of the Guest | ||
41 | * it makes us process all the asynchronous hypercalls. */ | ||
35 | break; | 42 | break; |
36 | case LHCALL_LGUEST_INIT: | 43 | case LHCALL_LGUEST_INIT: |
44 | /* You can't get here unless you're already initialized. Don't | ||
45 | * do that. */ | ||
37 | kill_guest(lg, "already have lguest_data"); | 46 | kill_guest(lg, "already have lguest_data"); |
38 | break; | 47 | break; |
39 | case LHCALL_CRASH: { | 48 | case LHCALL_CRASH: { |
49 | /* Crash is such a trivial hypercall that we do it in four | ||
50 | * lines right here. */ | ||
40 | char msg[128]; | 51 | char msg[128]; |
52 | /* If the lgread fails, it will call kill_guest() itself; the | ||
53 | * kill_guest() with the message will be ignored. */ | ||
41 | lgread(lg, msg, regs->edx, sizeof(msg)); | 54 | lgread(lg, msg, regs->edx, sizeof(msg)); |
42 | msg[sizeof(msg)-1] = '\0'; | 55 | msg[sizeof(msg)-1] = '\0'; |
43 | kill_guest(lg, "CRASH: %s", msg); | 56 | kill_guest(lg, "CRASH: %s", msg); |
44 | break; | 57 | break; |
45 | } | 58 | } |
46 | case LHCALL_FLUSH_TLB: | 59 | case LHCALL_FLUSH_TLB: |
60 | /* FLUSH_TLB comes in two flavors, depending on the | ||
61 | * argument: */ | ||
47 | if (regs->edx) | 62 | if (regs->edx) |
48 | guest_pagetable_clear_all(lg); | 63 | guest_pagetable_clear_all(lg); |
49 | else | 64 | else |
50 | guest_pagetable_flush_user(lg); | 65 | guest_pagetable_flush_user(lg); |
51 | break; | 66 | break; |
52 | case LHCALL_GET_WALLCLOCK: { | 67 | case LHCALL_GET_WALLCLOCK: { |
68 | /* The Guest wants to know the real time in seconds since 1970, | ||
69 | * in good Unix tradition. */ | ||
53 | struct timespec ts; | 70 | struct timespec ts; |
54 | ktime_get_real_ts(&ts); | 71 | ktime_get_real_ts(&ts); |
55 | regs->eax = ts.tv_sec; | 72 | regs->eax = ts.tv_sec; |
56 | break; | 73 | break; |
57 | } | 74 | } |
58 | case LHCALL_BIND_DMA: | 75 | case LHCALL_BIND_DMA: |
76 | /* BIND_DMA really wants four arguments, but it's the only call | ||
77 | * which does. So the Guest packs the number of buffers and | ||
78 | * the interrupt number into the final argument, and we decode | ||
79 | * it here. This can legitimately fail, since we currently | ||
80 | * place a limit on the number of DMA pools a Guest can have. | ||
81 | * So we return true or false from this call. */ | ||
59 | regs->eax = bind_dma(lg, regs->edx, regs->ebx, | 82 | regs->eax = bind_dma(lg, regs->edx, regs->ebx, |
60 | regs->ecx >> 8, regs->ecx & 0xFF); | 83 | regs->ecx >> 8, regs->ecx & 0xFF); |
61 | break; | 84 | break; |
85 | |||
86 | /* All these calls simply pass the arguments through to the right | ||
87 | * routines. */ | ||
62 | case LHCALL_SEND_DMA: | 88 | case LHCALL_SEND_DMA: |
63 | send_dma(lg, regs->edx, regs->ebx); | 89 | send_dma(lg, regs->edx, regs->ebx); |
64 | break; | 90 | break; |
@@ -86,10 +112,13 @@ static void do_hcall(struct lguest *lg, struct lguest_regs *regs) | |||
86 | case LHCALL_SET_CLOCKEVENT: | 112 | case LHCALL_SET_CLOCKEVENT: |
87 | guest_set_clockevent(lg, regs->edx); | 113 | guest_set_clockevent(lg, regs->edx); |
88 | break; | 114 | break; |
115 | |||
89 | case LHCALL_TS: | 116 | case LHCALL_TS: |
117 | /* This sets the TS flag, as we saw used in run_guest(). */ | ||
90 | lg->ts = regs->edx; | 118 | lg->ts = regs->edx; |
91 | break; | 119 | break; |
92 | case LHCALL_HALT: | 120 | case LHCALL_HALT: |
121 | /* Similarly, this sets the halted flag for run_guest(). */ | ||
93 | lg->halted = 1; | 122 | lg->halted = 1; |
94 | break; | 123 | break; |
95 | default: | 124 | default: |
@@ -97,25 +126,42 @@ static void do_hcall(struct lguest *lg, struct lguest_regs *regs) | |||
97 | } | 126 | } |
98 | } | 127 | } |
99 | 128 | ||
100 | /* We always do queued calls before actual hypercall. */ | 129 | /* Asynchronous hypercalls are easy: we just look in the array in the Guest's |
130 | * "struct lguest_data" and see if there are any new ones marked "ready". | ||
131 | * | ||
132 | * We are careful to do these in order: obviously we respect the order the | ||
133 | * Guest put them in the ring, but we also promise the Guest that they will | ||
134 | * happen before any normal hypercall (which is why we check this before | ||
135 | * checking for a normal hcall). */ | ||
101 | static void do_async_hcalls(struct lguest *lg) | 136 | static void do_async_hcalls(struct lguest *lg) |
102 | { | 137 | { |
103 | unsigned int i; | 138 | unsigned int i; |
104 | u8 st[LHCALL_RING_SIZE]; | 139 | u8 st[LHCALL_RING_SIZE]; |
105 | 140 | ||
141 | /* For simplicity, we copy the entire call status array in at once. */ | ||
106 | if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) | 142 | if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) |
107 | return; | 143 | return; |
108 | 144 | ||
145 | |||
146 | /* We process "struct lguest_data"s hcalls[] ring once. */ | ||
109 | for (i = 0; i < ARRAY_SIZE(st); i++) { | 147 | for (i = 0; i < ARRAY_SIZE(st); i++) { |
110 | struct lguest_regs regs; | 148 | struct lguest_regs regs; |
149 | /* We remember where we were up to from last time. This makes | ||
150 | * sure that the hypercalls are done in the order the Guest | ||
151 | * places them in the ring. */ | ||
111 | unsigned int n = lg->next_hcall; | 152 | unsigned int n = lg->next_hcall; |
112 | 153 | ||
154 | /* 0xFF means there's no call here (yet). */ | ||
113 | if (st[n] == 0xFF) | 155 | if (st[n] == 0xFF) |
114 | break; | 156 | break; |
115 | 157 | ||
158 | /* OK, we have hypercall. Increment the "next_hcall" cursor, | ||
159 | * and wrap back to 0 if we reach the end. */ | ||
116 | if (++lg->next_hcall == LHCALL_RING_SIZE) | 160 | if (++lg->next_hcall == LHCALL_RING_SIZE) |
117 | lg->next_hcall = 0; | 161 | lg->next_hcall = 0; |
118 | 162 | ||
163 | /* We copy the hypercall arguments into a fake register | ||
164 | * structure. This makes life simple for do_hcall(). */ | ||
119 | if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax) | 165 | if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax) |
120 | || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx) | 166 | || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx) |
121 | || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx) | 167 | || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx) |
@@ -124,74 +170,126 @@ static void do_async_hcalls(struct lguest *lg) | |||
124 | break; | 170 | break; |
125 | } | 171 | } |
126 | 172 | ||
173 | /* Do the hypercall, same as a normal one. */ | ||
127 | do_hcall(lg, ®s); | 174 | do_hcall(lg, ®s); |
175 | |||
176 | /* Mark the hypercall done. */ | ||
128 | if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { | 177 | if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { |
129 | kill_guest(lg, "Writing result for async hypercall"); | 178 | kill_guest(lg, "Writing result for async hypercall"); |
130 | break; | 179 | break; |
131 | } | 180 | } |
132 | 181 | ||
182 | /* Stop doing hypercalls if we've just done a DMA to the | ||
183 | * Launcher: it needs to service this first. */ | ||
133 | if (lg->dma_is_pending) | 184 | if (lg->dma_is_pending) |
134 | break; | 185 | break; |
135 | } | 186 | } |
136 | } | 187 | } |
137 | 188 | ||
189 | /* Last of all, we look at what happens first of all. The very first time the | ||
190 | * Guest makes a hypercall, we end up here to set things up: */ | ||
138 | static void initialize(struct lguest *lg) | 191 | static void initialize(struct lguest *lg) |
139 | { | 192 | { |
140 | u32 tsc_speed; | 193 | u32 tsc_speed; |
141 | 194 | ||
195 | /* You can't do anything until you're initialized. The Guest knows the | ||
196 | * rules, so we're unforgiving here. */ | ||
142 | if (lg->regs->eax != LHCALL_LGUEST_INIT) { | 197 | if (lg->regs->eax != LHCALL_LGUEST_INIT) { |
143 | kill_guest(lg, "hypercall %li before LGUEST_INIT", | 198 | kill_guest(lg, "hypercall %li before LGUEST_INIT", |
144 | lg->regs->eax); | 199 | lg->regs->eax); |
145 | return; | 200 | return; |
146 | } | 201 | } |
147 | 202 | ||
148 | /* We only tell the guest to use the TSC if it's reliable. */ | 203 | /* We insist that the Time Stamp Counter exist and doesn't change with |
204 | * cpu frequency. Some devious chip manufacturers decided that TSC | ||
205 | * changes could be handled in software. I decided that time going | ||
206 | * backwards might be good for benchmarks, but it's bad for users. | ||
207 | * | ||
208 | * We also insist that the TSC be stable: the kernel detects unreliable | ||
209 | * TSCs for its own purposes, and we use that here. */ | ||
149 | if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable()) | 210 | if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable()) |
150 | tsc_speed = tsc_khz; | 211 | tsc_speed = tsc_khz; |
151 | else | 212 | else |
152 | tsc_speed = 0; | 213 | tsc_speed = 0; |
153 | 214 | ||
215 | /* The pointer to the Guest's "struct lguest_data" is the only | ||
216 | * argument. */ | ||
154 | lg->lguest_data = (struct lguest_data __user *)lg->regs->edx; | 217 | lg->lguest_data = (struct lguest_data __user *)lg->regs->edx; |
155 | /* We check here so we can simply copy_to_user/from_user */ | 218 | /* If we check the address they gave is OK now, we can simply |
219 | * copy_to_user/from_user from now on rather than using lgread/lgwrite. | ||
220 | * I put this in to show that I'm not immune to writing stupid | ||
221 | * optimizations. */ | ||
156 | if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) { | 222 | if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) { |
157 | kill_guest(lg, "bad guest page %p", lg->lguest_data); | 223 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
158 | return; | 224 | return; |
159 | } | 225 | } |
226 | /* The Guest tells us where we're not to deliver interrupts by putting | ||
227 | * the range of addresses into "struct lguest_data". */ | ||
160 | if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) | 228 | if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) |
161 | || get_user(lg->noirq_end, &lg->lguest_data->noirq_end) | 229 | || get_user(lg->noirq_end, &lg->lguest_data->noirq_end) |
162 | /* We reserve the top pgd entry. */ | 230 | /* We tell the Guest that it can't use the top 4MB of virtual |
231 | * addresses used by the Switcher. */ | ||
163 | || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem) | 232 | || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem) |
164 | || put_user(tsc_speed, &lg->lguest_data->tsc_khz) | 233 | || put_user(tsc_speed, &lg->lguest_data->tsc_khz) |
234 | /* We also give the Guest a unique id, as used in lguest_net.c. */ | ||
165 | || put_user(lg->guestid, &lg->lguest_data->guestid)) | 235 | || put_user(lg->guestid, &lg->lguest_data->guestid)) |
166 | kill_guest(lg, "bad guest page %p", lg->lguest_data); | 236 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
167 | 237 | ||
168 | /* This is the one case where the above accesses might have | 238 | /* This is the one case where the above accesses might have been the |
169 | * been the first write to a Guest page. This may have caused | 239 | * first write to a Guest page. This may have caused a copy-on-write |
170 | * a copy-on-write fault, but the Guest might be referring to | 240 | * fault, but the Guest might be referring to the old (read-only) |
171 | * the old (read-only) page. */ | 241 | * page. */ |
172 | guest_pagetable_clear_all(lg); | 242 | guest_pagetable_clear_all(lg); |
173 | } | 243 | } |
244 | /* Now we've examined the hypercall code; our Guest can make requests. There | ||
245 | * is one other way we can do things for the Guest, as we see in | ||
246 | * emulate_insn(). */ | ||
174 | 247 | ||
175 | /* Even if we go out to userspace and come back, we don't want to do | 248 | /*H:110 Tricky point: we mark the hypercall as "done" once we've done it. |
176 | * the hypercall again. */ | 249 | * Normally we don't need to do this: the Guest will run again and update the |
250 | * trap number before we come back around the run_guest() loop to | ||
251 | * do_hypercalls(). | ||
252 | * | ||
253 | * However, if we are signalled or the Guest sends DMA to the Launcher, that | ||
254 | * loop will exit without running the Guest. When it comes back it would try | ||
255 | * to re-run the hypercall. */ | ||
177 | static void clear_hcall(struct lguest *lg) | 256 | static void clear_hcall(struct lguest *lg) |
178 | { | 257 | { |
179 | lg->regs->trapnum = 255; | 258 | lg->regs->trapnum = 255; |
180 | } | 259 | } |
181 | 260 | ||
261 | /*H:100 | ||
262 | * Hypercalls | ||
263 | * | ||
264 | * Remember from the Guest, hypercalls come in two flavors: normal and | ||
265 | * asynchronous. This file handles both of types. | ||
266 | */ | ||
182 | void do_hypercalls(struct lguest *lg) | 267 | void do_hypercalls(struct lguest *lg) |
183 | { | 268 | { |
269 | /* Not initialized yet? */ | ||
184 | if (unlikely(!lg->lguest_data)) { | 270 | if (unlikely(!lg->lguest_data)) { |
271 | /* Did the Guest make a hypercall? We might have come back for | ||
272 | * some other reason (an interrupt, a different trap). */ | ||
185 | if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) { | 273 | if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) { |
274 | /* Set up the "struct lguest_data" */ | ||
186 | initialize(lg); | 275 | initialize(lg); |
276 | /* The hypercall is done. */ | ||
187 | clear_hcall(lg); | 277 | clear_hcall(lg); |
188 | } | 278 | } |
189 | return; | 279 | return; |
190 | } | 280 | } |
191 | 281 | ||
282 | /* The Guest has initialized. | ||
283 | * | ||
284 | * Look in the hypercall ring for the async hypercalls: */ | ||
192 | do_async_hcalls(lg); | 285 | do_async_hcalls(lg); |
286 | |||
287 | /* If we stopped reading the hypercall ring because the Guest did a | ||
288 | * SEND_DMA to the Launcher, we want to return now. Otherwise if the | ||
289 | * Guest asked us to do a hypercall, we do it. */ | ||
193 | if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) { | 290 | if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) { |
194 | do_hcall(lg, lg->regs); | 291 | do_hcall(lg, lg->regs); |
292 | /* The hypercall is done. */ | ||
195 | clear_hcall(lg); | 293 | clear_hcall(lg); |
196 | } | 294 | } |
197 | } | 295 | } |