blob: 19a32280731db99e05661d3ae5c46b6a8f168dd2 [file] [log] [blame]
Rusty Russell2e04ef72009-07-30 16:03:45 -06001/*P:500
2 * Just as userspace programs request kernel operations through a system
Rusty Russellf938d2c2007-07-26 10:41:02 -07003 * call, the Guest requests Host operations through a "hypercall". You might
4 * notice this nomenclature doesn't really follow any logic, but the name has
5 * been around for long enough that we're stuck with it. As you'd expect, this
Rusty Russell2e04ef72009-07-30 16:03:45 -06006 * code is basically a one big switch statement.
7:*/
Rusty Russellf938d2c2007-07-26 10:41:02 -07008
9/* Copyright (C) 2006 Rusty Russell IBM Corporation
Rusty Russelld7e28ff2007-07-19 01:49:23 -070010
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25#include <linux/uaccess.h>
26#include <linux/syscalls.h>
27#include <linux/mm.h>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -020028#include <linux/ktime.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070029#include <asm/page.h>
30#include <asm/pgtable.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070031#include "lg.h"
32
Rusty Russell2e04ef72009-07-30 16:03:45 -060033/*H:120
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 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -020037static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070038{
Jes Sorensenb410e7b2007-10-22 11:03:31 +100039 switch (args->arg0) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -070040 case LHCALL_FLUSH_ASYNC:
Rusty Russell2e04ef72009-07-30 16:03:45 -060041 /*
42 * This call does nothing, except by breaking out of the Guest
43 * it makes us process all the asynchronous hypercalls.
44 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070045 break;
Rusty Russella32a88132009-06-12 22:27:02 -060046 case LHCALL_SEND_INTERRUPTS:
Rusty Russell2e04ef72009-07-30 16:03:45 -060047 /*
48 * This call does nothing too, but by breaking out of the Guest
49 * it makes us process any pending interrupts.
50 */
Rusty Russella32a88132009-06-12 22:27:02 -060051 break;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070052 case LHCALL_LGUEST_INIT:
Rusty Russell2e04ef72009-07-30 16:03:45 -060053 /*
54 * You can't get here unless you're already initialized. Don't
55 * do that.
56 */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020057 kill_guest(cpu, "already have lguest_data");
Rusty Russelld7e28ff2007-07-19 01:49:23 -070058 break;
Balaji Raoec04b132007-12-28 14:26:24 +053059 case LHCALL_SHUTDOWN: {
Rusty Russelld7e28ff2007-07-19 01:49:23 -070060 char msg[128];
Rusty Russell2e04ef72009-07-30 16:03:45 -060061 /*
Rusty Russella91d74a2009-07-30 16:03:45 -060062 * Shutdown is such a trivial hypercall that we do it in five
Rusty Russell2e04ef72009-07-30 16:03:45 -060063 * 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 */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020068 __lgread(cpu, msg, args->arg1, sizeof(msg));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070069 msg[sizeof(msg)-1] = '\0';
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020070 kill_guest(cpu, "CRASH: %s", msg);
Balaji Raoec04b132007-12-28 14:26:24 +053071 if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020072 cpu->lg->dead = ERR_PTR(-ERESTART);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070073 break;
74 }
75 case LHCALL_FLUSH_TLB:
Rusty Russell2e04ef72009-07-30 16:03:45 -060076 /* FLUSH_TLB comes in two flavors, depending on the argument: */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100077 if (args->arg1)
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -020078 guest_pagetable_clear_all(cpu);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070079 else
Glauber de Oliveira Costa17136082008-01-07 11:05:37 -020080 guest_pagetable_flush_user(cpu);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070081 break;
Rusty Russellbff672e62007-07-26 10:41:04 -070082
Rusty Russell2e04ef72009-07-30 16:03:45 -060083 /*
84 * All these calls simply pass the arguments through to the right
85 * routines.
86 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070087 case LHCALL_NEW_PGTABLE:
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -020088 guest_new_pagetable(cpu, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070089 break;
90 case LHCALL_SET_STACK:
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -020091 guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070092 break;
93 case LHCALL_SET_PTE:
Matias Zabaljaureguiacdd0b62009-06-12 22:27:07 -060094#ifdef CONFIG_X86_PAE
95 guest_set_pte(cpu, args->arg1, args->arg2,
96 __pte(args->arg3 | (u64)args->arg4 << 32));
97#else
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020098 guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));
Matias Zabaljaureguiacdd0b62009-06-12 22:27:07 -060099#endif
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700100 break;
Matias Zabaljaureguiebe0ba82009-05-30 15:48:08 -0300101 case LHCALL_SET_PGD:
102 guest_set_pgd(cpu->lg, args->arg1, args->arg2);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700103 break;
Matias Zabaljaureguiacdd0b62009-06-12 22:27:07 -0600104#ifdef CONFIG_X86_PAE
105 case LHCALL_SET_PMD:
106 guest_set_pmd(cpu->lg, args->arg1, args->arg2);
107 break;
108#endif
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700109 case LHCALL_SET_CLOCKEVENT:
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200110 guest_set_clockevent(cpu, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700111 break;
112 case LHCALL_TS:
Rusty Russellbff672e62007-07-26 10:41:04 -0700113 /* This sets the TS flag, as we saw used in run_guest(). */
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -0200114 cpu->ts = args->arg1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700115 break;
116 case LHCALL_HALT:
Rusty Russellbff672e62007-07-26 10:41:04 -0700117 /* Similarly, this sets the halted flag for run_guest(). */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200118 cpu->halted = 1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700119 break;
120 default:
Rusty Russelle1e72962007-10-25 15:02:50 +1000121 /* It should be an architecture-specific hypercall. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200122 if (lguest_arch_do_hcall(cpu, args))
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200123 kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700124 }
125}
126
Rusty Russell2e04ef72009-07-30 16:03:45 -0600127/*H:124
128 * Asynchronous hypercalls are easy: we just look in the array in the
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000129 * Guest's "struct lguest_data" to see if any new ones are marked "ready".
Rusty Russellbff672e62007-07-26 10:41:04 -0700130 *
131 * We are careful to do these in order: obviously we respect the order the
132 * Guest put them in the ring, but we also promise the Guest that they will
133 * happen before any normal hypercall (which is why we check this before
Rusty Russell2e04ef72009-07-30 16:03:45 -0600134 * checking for a normal hcall).
135 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200136static void do_async_hcalls(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700137{
138 unsigned int i;
139 u8 st[LHCALL_RING_SIZE];
140
Rusty Russellbff672e62007-07-26 10:41:04 -0700141 /* For simplicity, we copy the entire call status array in at once. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200142 if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700143 return;
144
Rusty Russellbff672e62007-07-26 10:41:04 -0700145 /* We process "struct lguest_data"s hcalls[] ring once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700146 for (i = 0; i < ARRAY_SIZE(st); i++) {
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000147 struct hcall_args args;
Rusty Russell2e04ef72009-07-30 16:03:45 -0600148 /*
149 * We remember where we were up to from last time. This makes
Rusty Russellbff672e62007-07-26 10:41:04 -0700150 * sure that the hypercalls are done in the order the Guest
Rusty Russell2e04ef72009-07-30 16:03:45 -0600151 * places them in the ring.
152 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200153 unsigned int n = cpu->next_hcall;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700154
Rusty Russellbff672e62007-07-26 10:41:04 -0700155 /* 0xFF means there's no call here (yet). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700156 if (st[n] == 0xFF)
157 break;
158
Rusty Russell2e04ef72009-07-30 16:03:45 -0600159 /*
160 * OK, we have hypercall. Increment the "next_hcall" cursor,
161 * and wrap back to 0 if we reach the end.
162 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200163 if (++cpu->next_hcall == LHCALL_RING_SIZE)
164 cpu->next_hcall = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700165
Rusty Russell2e04ef72009-07-30 16:03:45 -0600166 /*
167 * Copy the hypercall arguments into a local copy of the
168 * hcall_args struct.
169 */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200170 if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000171 sizeof(struct hcall_args))) {
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200172 kill_guest(cpu, "Fetching async hypercalls");
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700173 break;
174 }
175
Rusty Russellbff672e62007-07-26 10:41:04 -0700176 /* Do the hypercall, same as a normal one. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200177 do_hcall(cpu, &args);
Rusty Russellbff672e62007-07-26 10:41:04 -0700178
179 /* Mark the hypercall done. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200180 if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
181 kill_guest(cpu, "Writing result for async hypercall");
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700182 break;
183 }
184
Rusty Russell2e04ef72009-07-30 16:03:45 -0600185 /*
186 * Stop doing hypercalls if they want to notify the Launcher:
187 * it needs to service this first.
188 */
Rusty Russell69a09dc2015-02-11 15:15:09 +1030189 if (cpu->pending.trap)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700190 break;
191 }
192}
193
Rusty Russell2e04ef72009-07-30 16:03:45 -0600194/*
195 * Last of all, we look at what happens first of all. The very first time the
196 * Guest makes a hypercall, we end up here to set things up:
197 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200198static void initialize(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700199{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600200 /*
201 * You can't do anything until you're initialized. The Guest knows the
202 * rules, so we're unforgiving here.
203 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200204 if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200205 kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700206 return;
207 }
208
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200209 if (lguest_arch_init_hypercalls(cpu))
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200210 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000211
Rusty Russell2e04ef72009-07-30 16:03:45 -0600212 /*
213 * The Guest tells us where we're not to deliver interrupts by putting
Rusty Russell2f921b52015-03-24 11:51:39 +1030214 * the instruction address into "struct lguest_data".
Rusty Russell2e04ef72009-07-30 16:03:45 -0600215 */
Rusty Russell2f921b52015-03-24 11:51:39 +1030216 if (get_user(cpu->lg->noirq_iret, &cpu->lg->lguest_data->noirq_iret))
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200217 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700218
Rusty Russell2e04ef72009-07-30 16:03:45 -0600219 /*
220 * We write the current time into the Guest's data page once so it can
221 * set its clock.
222 */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200223 write_timestamp(cpu);
Rusty Russell6c8dca52007-07-27 13:42:52 +1000224
Rusty Russell47436aa2007-10-22 11:03:36 +1000225 /* page_tables.c will also do some setup. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200226 page_table_guest_data_init(cpu);
Rusty Russell47436aa2007-10-22 11:03:36 +1000227
Rusty Russell2e04ef72009-07-30 16:03:45 -0600228 /*
229 * This is the one case where the above accesses might have been the
Rusty Russellbff672e62007-07-26 10:41:04 -0700230 * first write to a Guest page. This may have caused a copy-on-write
Rusty Russelle1e72962007-10-25 15:02:50 +1000231 * fault, but the old page might be (read-only) in the Guest
Rusty Russell2e04ef72009-07-30 16:03:45 -0600232 * pagetable.
233 */
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -0200234 guest_pagetable_clear_all(cpu);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700235}
Rusty Russella6bd8e12008-03-28 11:05:53 -0500236/*:*/
237
Rusty Russell2e04ef72009-07-30 16:03:45 -0600238/*M:013
239 * If a Guest reads from a page (so creates a mapping) that it has never
Rusty Russella6bd8e12008-03-28 11:05:53 -0500240 * written to, and then the Launcher writes to it (ie. the output of a virtual
241 * device), the Guest will still see the old page. In practice, this never
242 * happens: why would the Guest read a page which it has never written to? But
Rusty Russell2e04ef72009-07-30 16:03:45 -0600243 * a similar scenario might one day bite us, so it's worth mentioning.
Rusty Russella91d74a2009-07-30 16:03:45 -0600244 *
245 * Note that if we used a shared anonymous mapping in the Launcher instead of
246 * mapping /dev/zero private, we wouldn't worry about cop-on-write. And we
247 * need that to switch the Launcher to processes (away from threads) anyway.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600248:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700249
Rusty Russellbff672e62007-07-26 10:41:04 -0700250/*H:100
251 * Hypercalls
252 *
253 * Remember from the Guest, hypercalls come in two flavors: normal and
254 * asynchronous. This file handles both of types.
255 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200256void do_hypercalls(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700257{
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000258 /* Not initialized yet? This hypercall must do it. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200259 if (unlikely(!cpu->lg->lguest_data)) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000260 /* Set up the "struct lguest_data" */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200261 initialize(cpu);
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000262 /* Hcall is done. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200263 cpu->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700264 return;
265 }
266
Rusty Russell2e04ef72009-07-30 16:03:45 -0600267 /*
268 * The Guest has initialized.
Rusty Russellbff672e62007-07-26 10:41:04 -0700269 *
Rusty Russell2e04ef72009-07-30 16:03:45 -0600270 * Look in the hypercall ring for the async hypercalls:
271 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200272 do_async_hcalls(cpu);
Rusty Russellbff672e62007-07-26 10:41:04 -0700273
Rusty Russell2e04ef72009-07-30 16:03:45 -0600274 /*
275 * If we stopped reading the hypercall ring because the Guest did a
Rusty Russell15045272007-10-22 11:24:10 +1000276 * NOTIFY to the Launcher, we want to return now. Otherwise we do
Rusty Russell2e04ef72009-07-30 16:03:45 -0600277 * the hypercall.
278 */
Rusty Russell69a09dc2015-02-11 15:15:09 +1030279 if (!cpu->pending.trap) {
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200280 do_hcall(cpu, cpu->hcall);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600281 /*
282 * Tricky point: we reset the hcall pointer to mark the
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000283 * hypercall as "done". We use the hcall pointer rather than
284 * the trap number to indicate a hypercall is pending.
285 * Normally it doesn't matter: the Guest will run again and
286 * update the trap number before we come back here.
287 *
Rusty Russelle1e72962007-10-25 15:02:50 +1000288 * However, if we are signalled or the Guest sends I/O to the
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000289 * Launcher, the run_guest() loop will exit without running the
290 * Guest. When it comes back it would try to re-run the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600291 * hypercall. Finding that bug sucked.
292 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200293 cpu->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700294 }
295}
Rusty Russell6c8dca52007-07-27 13:42:52 +1000296
Rusty Russell2e04ef72009-07-30 16:03:45 -0600297/*
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 */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200301void write_timestamp(struct lg_cpu *cpu)
Rusty Russell6c8dca52007-07-27 13:42:52 +1000302{
303 struct timespec now;
304 ktime_get_real_ts(&now);
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200305 if (copy_to_user(&cpu->lg->lguest_data->time,
306 &now, sizeof(struct timespec)))
307 kill_guest(cpu, "Writing timestamp");
Rusty Russell6c8dca52007-07-27 13:42:52 +1000308}