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