Commit | Line | Data |
---|---|---|
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> | |
26 | #include <asm/page.h> | |
27 | #include <asm/pgtable.h> | |
d7e28ffe RR |
28 | #include "lg.h" |
29 | ||
b410e7b1 JS |
30 | /*H:120 This is the core hypercall routine: where the Guest gets what it wants. |
31 | * Or gets killed. Or, in the case of LHCALL_CRASH, both. */ | |
73044f05 | 32 | static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) |
d7e28ffe | 33 | { |
73044f05 GOC |
34 | struct lguest *lg = cpu->lg; |
35 | ||
b410e7b1 | 36 | switch (args->arg0) { |
d7e28ffe | 37 | case LHCALL_FLUSH_ASYNC: |
bff672e6 RR |
38 | /* This call does nothing, except by breaking out of the Guest |
39 | * it makes us process all the asynchronous hypercalls. */ | |
d7e28ffe RR |
40 | break; |
41 | case LHCALL_LGUEST_INIT: | |
bff672e6 RR |
42 | /* You can't get here unless you're already initialized. Don't |
43 | * do that. */ | |
d7e28ffe RR |
44 | kill_guest(lg, "already have lguest_data"); |
45 | break; | |
ec04b13f BR |
46 | case LHCALL_SHUTDOWN: { |
47 | /* Shutdown is such a trivial hypercall that we do it in four | |
bff672e6 | 48 | * lines right here. */ |
d7e28ffe | 49 | char msg[128]; |
bff672e6 RR |
50 | /* If the lgread fails, it will call kill_guest() itself; the |
51 | * kill_guest() with the message will be ignored. */ | |
2d37f94a | 52 | __lgread(lg, msg, args->arg1, sizeof(msg)); |
d7e28ffe RR |
53 | msg[sizeof(msg)-1] = '\0'; |
54 | kill_guest(lg, "CRASH: %s", msg); | |
ec04b13f BR |
55 | if (args->arg2 == LGUEST_SHUTDOWN_RESTART) |
56 | lg->dead = ERR_PTR(-ERESTART); | |
d7e28ffe RR |
57 | break; |
58 | } | |
59 | case LHCALL_FLUSH_TLB: | |
bff672e6 RR |
60 | /* FLUSH_TLB comes in two flavors, depending on the |
61 | * argument: */ | |
b410e7b1 | 62 | if (args->arg1) |
4665ac8e | 63 | guest_pagetable_clear_all(cpu); |
d7e28ffe | 64 | else |
1713608f | 65 | guest_pagetable_flush_user(cpu); |
d7e28ffe | 66 | break; |
bff672e6 RR |
67 | |
68 | /* All these calls simply pass the arguments through to the right | |
69 | * routines. */ | |
d7e28ffe | 70 | case LHCALL_NEW_PGTABLE: |
4665ac8e | 71 | guest_new_pagetable(cpu, args->arg1); |
d7e28ffe RR |
72 | break; |
73 | case LHCALL_SET_STACK: | |
4665ac8e | 74 | guest_set_stack(cpu, args->arg1, args->arg2, args->arg3); |
d7e28ffe RR |
75 | break; |
76 | case LHCALL_SET_PTE: | |
df29f43e | 77 | guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3)); |
d7e28ffe RR |
78 | break; |
79 | case LHCALL_SET_PMD: | |
b410e7b1 | 80 | guest_set_pmd(lg, args->arg1, args->arg2); |
d7e28ffe RR |
81 | break; |
82 | case LHCALL_SET_CLOCKEVENT: | |
ad8d8f3b | 83 | guest_set_clockevent(cpu, args->arg1); |
d7e28ffe RR |
84 | break; |
85 | case LHCALL_TS: | |
bff672e6 | 86 | /* This sets the TS flag, as we saw used in run_guest(). */ |
4665ac8e | 87 | cpu->ts = args->arg1; |
d7e28ffe RR |
88 | break; |
89 | case LHCALL_HALT: | |
bff672e6 | 90 | /* Similarly, this sets the halted flag for run_guest(). */ |
66686c2a | 91 | cpu->halted = 1; |
d7e28ffe | 92 | break; |
15045275 | 93 | case LHCALL_NOTIFY: |
5e232f4f | 94 | cpu->pending_notify = args->arg1; |
15045275 | 95 | break; |
d7e28ffe | 96 | default: |
e1e72965 | 97 | /* It should be an architecture-specific hypercall. */ |
73044f05 | 98 | if (lguest_arch_do_hcall(cpu, args)) |
b410e7b1 | 99 | kill_guest(lg, "Bad hypercall %li\n", args->arg0); |
d7e28ffe RR |
100 | } |
101 | } | |
b410e7b1 | 102 | /*:*/ |
d7e28ffe | 103 | |
b410e7b1 JS |
104 | /*H:124 Asynchronous hypercalls are easy: we just look in the array in the |
105 | * Guest's "struct lguest_data" to see if any new ones are marked "ready". | |
bff672e6 RR |
106 | * |
107 | * We are careful to do these in order: obviously we respect the order the | |
108 | * Guest put them in the ring, but we also promise the Guest that they will | |
109 | * happen before any normal hypercall (which is why we check this before | |
110 | * checking for a normal hcall). */ | |
73044f05 | 111 | static void do_async_hcalls(struct lg_cpu *cpu) |
d7e28ffe RR |
112 | { |
113 | unsigned int i; | |
114 | u8 st[LHCALL_RING_SIZE]; | |
73044f05 | 115 | struct lguest *lg = cpu->lg; |
d7e28ffe | 116 | |
bff672e6 | 117 | /* For simplicity, we copy the entire call status array in at once. */ |
d7e28ffe RR |
118 | if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) |
119 | return; | |
120 | ||
bff672e6 | 121 | /* We process "struct lguest_data"s hcalls[] ring once. */ |
d7e28ffe | 122 | for (i = 0; i < ARRAY_SIZE(st); i++) { |
b410e7b1 | 123 | struct hcall_args args; |
bff672e6 RR |
124 | /* We remember where we were up to from last time. This makes |
125 | * sure that the hypercalls are done in the order the Guest | |
126 | * places them in the ring. */ | |
73044f05 | 127 | unsigned int n = cpu->next_hcall; |
d7e28ffe | 128 | |
bff672e6 | 129 | /* 0xFF means there's no call here (yet). */ |
d7e28ffe RR |
130 | if (st[n] == 0xFF) |
131 | break; | |
132 | ||
bff672e6 RR |
133 | /* OK, we have hypercall. Increment the "next_hcall" cursor, |
134 | * and wrap back to 0 if we reach the end. */ | |
73044f05 GOC |
135 | if (++cpu->next_hcall == LHCALL_RING_SIZE) |
136 | cpu->next_hcall = 0; | |
d7e28ffe | 137 | |
b410e7b1 JS |
138 | /* Copy the hypercall arguments into a local copy of |
139 | * the hcall_args struct. */ | |
140 | if (copy_from_user(&args, &lg->lguest_data->hcalls[n], | |
141 | sizeof(struct hcall_args))) { | |
d7e28ffe RR |
142 | kill_guest(lg, "Fetching async hypercalls"); |
143 | break; | |
144 | } | |
145 | ||
bff672e6 | 146 | /* Do the hypercall, same as a normal one. */ |
73044f05 | 147 | do_hcall(cpu, &args); |
bff672e6 RR |
148 | |
149 | /* Mark the hypercall done. */ | |
d7e28ffe RR |
150 | if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { |
151 | kill_guest(lg, "Writing result for async hypercall"); | |
152 | break; | |
153 | } | |
154 | ||
15045275 RR |
155 | /* Stop doing hypercalls if they want to notify the Launcher: |
156 | * it needs to service this first. */ | |
5e232f4f | 157 | if (cpu->pending_notify) |
d7e28ffe RR |
158 | break; |
159 | } | |
160 | } | |
161 | ||
bff672e6 RR |
162 | /* Last of all, we look at what happens first of all. The very first time the |
163 | * Guest makes a hypercall, we end up here to set things up: */ | |
73044f05 | 164 | static void initialize(struct lg_cpu *cpu) |
d7e28ffe | 165 | { |
73044f05 | 166 | struct lguest *lg = cpu->lg; |
bff672e6 RR |
167 | /* You can't do anything until you're initialized. The Guest knows the |
168 | * rules, so we're unforgiving here. */ | |
73044f05 GOC |
169 | if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) { |
170 | kill_guest(lg, "hypercall %li before INIT", cpu->hcall->arg0); | |
d7e28ffe RR |
171 | return; |
172 | } | |
173 | ||
73044f05 | 174 | if (lguest_arch_init_hypercalls(cpu)) |
d7e28ffe | 175 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
3c6b5bfa | 176 | |
bff672e6 RR |
177 | /* The Guest tells us where we're not to deliver interrupts by putting |
178 | * the range of addresses into "struct lguest_data". */ | |
d7e28ffe | 179 | if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) |
47436aa4 | 180 | || get_user(lg->noirq_end, &lg->lguest_data->noirq_end)) |
d7e28ffe RR |
181 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
182 | ||
e1e72965 RR |
183 | /* We write the current time into the Guest's data page once so it can |
184 | * set its clock. */ | |
6c8dca5d RR |
185 | write_timestamp(lg); |
186 | ||
47436aa4 RR |
187 | /* page_tables.c will also do some setup. */ |
188 | page_table_guest_data_init(lg); | |
189 | ||
bff672e6 RR |
190 | /* This is the one case where the above accesses might have been the |
191 | * first write to a Guest page. This may have caused a copy-on-write | |
e1e72965 RR |
192 | * fault, but the old page might be (read-only) in the Guest |
193 | * pagetable. */ | |
4665ac8e | 194 | guest_pagetable_clear_all(cpu); |
d7e28ffe RR |
195 | } |
196 | ||
bff672e6 RR |
197 | /*H:100 |
198 | * Hypercalls | |
199 | * | |
200 | * Remember from the Guest, hypercalls come in two flavors: normal and | |
201 | * asynchronous. This file handles both of types. | |
202 | */ | |
73044f05 | 203 | void do_hypercalls(struct lg_cpu *cpu) |
d7e28ffe | 204 | { |
cc6d4fbc | 205 | /* Not initialized yet? This hypercall must do it. */ |
73044f05 | 206 | if (unlikely(!cpu->lg->lguest_data)) { |
cc6d4fbc | 207 | /* Set up the "struct lguest_data" */ |
73044f05 | 208 | initialize(cpu); |
cc6d4fbc | 209 | /* Hcall is done. */ |
73044f05 | 210 | cpu->hcall = NULL; |
d7e28ffe RR |
211 | return; |
212 | } | |
213 | ||
bff672e6 RR |
214 | /* The Guest has initialized. |
215 | * | |
216 | * Look in the hypercall ring for the async hypercalls: */ | |
73044f05 | 217 | do_async_hcalls(cpu); |
bff672e6 RR |
218 | |
219 | /* If we stopped reading the hypercall ring because the Guest did a | |
15045275 | 220 | * NOTIFY to the Launcher, we want to return now. Otherwise we do |
cc6d4fbc | 221 | * the hypercall. */ |
5e232f4f | 222 | if (!cpu->pending_notify) { |
73044f05 | 223 | do_hcall(cpu, cpu->hcall); |
cc6d4fbc RR |
224 | /* Tricky point: we reset the hcall pointer to mark the |
225 | * hypercall as "done". We use the hcall pointer rather than | |
226 | * the trap number to indicate a hypercall is pending. | |
227 | * Normally it doesn't matter: the Guest will run again and | |
228 | * update the trap number before we come back here. | |
229 | * | |
e1e72965 | 230 | * However, if we are signalled or the Guest sends I/O to the |
cc6d4fbc RR |
231 | * Launcher, the run_guest() loop will exit without running the |
232 | * Guest. When it comes back it would try to re-run the | |
233 | * hypercall. */ | |
73044f05 | 234 | cpu->hcall = NULL; |
d7e28ffe RR |
235 | } |
236 | } | |
6c8dca5d RR |
237 | |
238 | /* This routine supplies the Guest with time: it's used for wallclock time at | |
239 | * initial boot and as a rough time source if the TSC isn't available. */ | |
240 | void write_timestamp(struct lguest *lg) | |
241 | { | |
242 | struct timespec now; | |
243 | ktime_get_real_ts(&now); | |
891ff65f | 244 | if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec))) |
6c8dca5d RR |
245 | kill_guest(lg, "Writing timestamp"); |
246 | } |