mmc_block: do not DMA to stack
[linux-2.6-block.git] / drivers / lguest / hypercalls.c
CommitLineData
f938d2c8
RR
1/*P:500 Just as userspace programs request kernel operations through a system
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
d7e28ffe
RR
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22*/
23#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/mm.h>
ca94f2bd 26#include <linux/ktime.h>
d7e28ffe
RR
27#include <asm/page.h>
28#include <asm/pgtable.h>
d7e28ffe
RR
29#include "lg.h"
30
b410e7b1 31/*H:120 This is the core hypercall routine: where the Guest gets what it wants.
a6bd8e13 32 * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. */
73044f05 33static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
d7e28ffe 34{
b410e7b1 35 switch (args->arg0) {
d7e28ffe 36 case LHCALL_FLUSH_ASYNC:
bff672e6
RR
37 /* This call does nothing, except by breaking out of the Guest
38 * it makes us process all the asynchronous hypercalls. */
d7e28ffe 39 break;
a32a8813
RR
40 case LHCALL_SEND_INTERRUPTS:
41 /* This call does nothing too, but by breaking out of the Guest
42 * it makes us process any pending interrupts. */
43 break;
d7e28ffe 44 case LHCALL_LGUEST_INIT:
bff672e6
RR
45 /* You can't get here unless you're already initialized. Don't
46 * do that. */
382ac6b3 47 kill_guest(cpu, "already have lguest_data");
d7e28ffe 48 break;
ec04b13f
BR
49 case LHCALL_SHUTDOWN: {
50 /* Shutdown is such a trivial hypercall that we do it in four
bff672e6 51 * lines right here. */
d7e28ffe 52 char msg[128];
bff672e6
RR
53 /* If the lgread fails, it will call kill_guest() itself; the
54 * kill_guest() with the message will be ignored. */
382ac6b3 55 __lgread(cpu, msg, args->arg1, sizeof(msg));
d7e28ffe 56 msg[sizeof(msg)-1] = '\0';
382ac6b3 57 kill_guest(cpu, "CRASH: %s", msg);
ec04b13f 58 if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
382ac6b3 59 cpu->lg->dead = ERR_PTR(-ERESTART);
d7e28ffe
RR
60 break;
61 }
62 case LHCALL_FLUSH_TLB:
bff672e6
RR
63 /* FLUSH_TLB comes in two flavors, depending on the
64 * argument: */
b410e7b1 65 if (args->arg1)
4665ac8e 66 guest_pagetable_clear_all(cpu);
d7e28ffe 67 else
1713608f 68 guest_pagetable_flush_user(cpu);
d7e28ffe 69 break;
bff672e6
RR
70
71 /* All these calls simply pass the arguments through to the right
72 * routines. */
d7e28ffe 73 case LHCALL_NEW_PGTABLE:
4665ac8e 74 guest_new_pagetable(cpu, args->arg1);
d7e28ffe
RR
75 break;
76 case LHCALL_SET_STACK:
4665ac8e 77 guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);
d7e28ffe
RR
78 break;
79 case LHCALL_SET_PTE:
acdd0b62
MZ
80#ifdef CONFIG_X86_PAE
81 guest_set_pte(cpu, args->arg1, args->arg2,
82 __pte(args->arg3 | (u64)args->arg4 << 32));
83#else
382ac6b3 84 guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));
acdd0b62 85#endif
d7e28ffe 86 break;
ebe0ba84
MZ
87 case LHCALL_SET_PGD:
88 guest_set_pgd(cpu->lg, args->arg1, args->arg2);
d7e28ffe 89 break;
acdd0b62
MZ
90#ifdef CONFIG_X86_PAE
91 case LHCALL_SET_PMD:
92 guest_set_pmd(cpu->lg, args->arg1, args->arg2);
93 break;
94#endif
d7e28ffe 95 case LHCALL_SET_CLOCKEVENT:
ad8d8f3b 96 guest_set_clockevent(cpu, args->arg1);
d7e28ffe
RR
97 break;
98 case LHCALL_TS:
bff672e6 99 /* This sets the TS flag, as we saw used in run_guest(). */
4665ac8e 100 cpu->ts = args->arg1;
d7e28ffe
RR
101 break;
102 case LHCALL_HALT:
bff672e6 103 /* Similarly, this sets the halted flag for run_guest(). */
66686c2a 104 cpu->halted = 1;
d7e28ffe 105 break;
15045275 106 case LHCALL_NOTIFY:
5e232f4f 107 cpu->pending_notify = args->arg1;
15045275 108 break;
d7e28ffe 109 default:
e1e72965 110 /* It should be an architecture-specific hypercall. */
73044f05 111 if (lguest_arch_do_hcall(cpu, args))
382ac6b3 112 kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
d7e28ffe
RR
113 }
114}
b410e7b1 115/*:*/
d7e28ffe 116
b410e7b1
JS
117/*H:124 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".
bff672e6
RR
119 *
120 * 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
122 * happen before any normal hypercall (which is why we check this before
123 * checking for a normal hcall). */
73044f05 124static void do_async_hcalls(struct lg_cpu *cpu)
d7e28ffe
RR
125{
126 unsigned int i;
127 u8 st[LHCALL_RING_SIZE];
128
bff672e6 129 /* For simplicity, we copy the entire call status array in at once. */
382ac6b3 130 if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
d7e28ffe
RR
131 return;
132
bff672e6 133 /* We process "struct lguest_data"s hcalls[] ring once. */
d7e28ffe 134 for (i = 0; i < ARRAY_SIZE(st); i++) {
b410e7b1 135 struct hcall_args args;
bff672e6
RR
136 /* We remember where we were up to from last time. This makes
137 * sure that the hypercalls are done in the order the Guest
138 * places them in the ring. */
73044f05 139 unsigned int n = cpu->next_hcall;
d7e28ffe 140
bff672e6 141 /* 0xFF means there's no call here (yet). */
d7e28ffe
RR
142 if (st[n] == 0xFF)
143 break;
144
bff672e6
RR
145 /* OK, we have hypercall. Increment the "next_hcall" cursor,
146 * and wrap back to 0 if we reach the end. */
73044f05
GOC
147 if (++cpu->next_hcall == LHCALL_RING_SIZE)
148 cpu->next_hcall = 0;
d7e28ffe 149
b410e7b1
JS
150 /* Copy the hypercall arguments into a local copy of
151 * the hcall_args struct. */
382ac6b3 152 if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
b410e7b1 153 sizeof(struct hcall_args))) {
382ac6b3 154 kill_guest(cpu, "Fetching async hypercalls");
d7e28ffe
RR
155 break;
156 }
157
bff672e6 158 /* Do the hypercall, same as a normal one. */
73044f05 159 do_hcall(cpu, &args);
bff672e6
RR
160
161 /* Mark the hypercall done. */
382ac6b3
GOC
162 if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
163 kill_guest(cpu, "Writing result for async hypercall");
d7e28ffe
RR
164 break;
165 }
166
15045275
RR
167 /* Stop doing hypercalls if they want to notify the Launcher:
168 * it needs to service this first. */
5e232f4f 169 if (cpu->pending_notify)
d7e28ffe
RR
170 break;
171 }
172}
173
bff672e6
RR
174/* Last of all, we look at what happens first of all. The very first time the
175 * Guest makes a hypercall, we end up here to set things up: */
73044f05 176static void initialize(struct lg_cpu *cpu)
d7e28ffe 177{
bff672e6
RR
178 /* You can't do anything until you're initialized. The Guest knows the
179 * rules, so we're unforgiving here. */
73044f05 180 if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
382ac6b3 181 kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
d7e28ffe
RR
182 return;
183 }
184
73044f05 185 if (lguest_arch_init_hypercalls(cpu))
382ac6b3 186 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
3c6b5bfa 187
bff672e6
RR
188 /* The Guest tells us where we're not to deliver interrupts by putting
189 * the range of addresses into "struct lguest_data". */
382ac6b3
GOC
190 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))
192 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
d7e28ffe 193
e1e72965
RR
194 /* We write the current time into the Guest's data page once so it can
195 * set its clock. */
382ac6b3 196 write_timestamp(cpu);
6c8dca5d 197
47436aa4 198 /* page_tables.c will also do some setup. */
382ac6b3 199 page_table_guest_data_init(cpu);
47436aa4 200
bff672e6
RR
201 /* 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
e1e72965
RR
203 * fault, but the old page might be (read-only) in the Guest
204 * pagetable. */
4665ac8e 205 guest_pagetable_clear_all(cpu);
d7e28ffe 206}
a6bd8e13
RR
207/*:*/
208
209/*M:013 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
211 * 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
213 * a similar scenario might one day bite us, so it's worth mentioning. :*/
d7e28ffe 214
bff672e6
RR
215/*H:100
216 * Hypercalls
217 *
218 * Remember from the Guest, hypercalls come in two flavors: normal and
219 * asynchronous. This file handles both of types.
220 */
73044f05 221void do_hypercalls(struct lg_cpu *cpu)
d7e28ffe 222{
cc6d4fbc 223 /* Not initialized yet? This hypercall must do it. */
73044f05 224 if (unlikely(!cpu->lg->lguest_data)) {
cc6d4fbc 225 /* Set up the "struct lguest_data" */
73044f05 226 initialize(cpu);
cc6d4fbc 227 /* Hcall is done. */
73044f05 228 cpu->hcall = NULL;
d7e28ffe
RR
229 return;
230 }
231
bff672e6
RR
232 /* The Guest has initialized.
233 *
234 * Look in the hypercall ring for the async hypercalls: */
73044f05 235 do_async_hcalls(cpu);
bff672e6
RR
236
237 /* If we stopped reading the hypercall ring because the Guest did a
15045275 238 * NOTIFY to the Launcher, we want to return now. Otherwise we do
cc6d4fbc 239 * the hypercall. */
5e232f4f 240 if (!cpu->pending_notify) {
73044f05 241 do_hcall(cpu, cpu->hcall);
cc6d4fbc
RR
242 /* Tricky point: we reset the hcall pointer to mark the
243 * hypercall as "done". We use the hcall pointer rather than
244 * the trap number to indicate a hypercall is pending.
245 * Normally it doesn't matter: the Guest will run again and
246 * update the trap number before we come back here.
247 *
e1e72965 248 * However, if we are signalled or the Guest sends I/O to the
cc6d4fbc
RR
249 * Launcher, the run_guest() loop will exit without running the
250 * Guest. When it comes back it would try to re-run the
a6bd8e13 251 * hypercall. Finding that bug sucked. */
73044f05 252 cpu->hcall = NULL;
d7e28ffe
RR
253 }
254}
6c8dca5d
RR
255
256/* This routine supplies the Guest with time: it's used for wallclock time at
257 * initial boot and as a rough time source if the TSC isn't available. */
382ac6b3 258void write_timestamp(struct lg_cpu *cpu)
6c8dca5d
RR
259{
260 struct timespec now;
261 ktime_get_real_ts(&now);
382ac6b3
GOC
262 if (copy_to_user(&cpu->lg->lguest_data->time,
263 &now, sizeof(struct timespec)))
264 kill_guest(cpu, "Writing timestamp");
6c8dca5d 265}