blob: 6f8c70ae380db22f928bd33c394515fb6df7522f [file] [log] [blame]
Rusty Russellf938d2c2007-07-26 10:41:02 -07001/*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
Rusty Russelld7e28ff2007-07-19 01:49:23 -07008
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>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070028#include "lg.h"
29
Jes Sorensenb410e7b2007-10-22 11:03:31 +100030/*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. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -020032static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070033{
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -020034 struct lguest *lg = cpu->lg;
35
Jes Sorensenb410e7b2007-10-22 11:03:31 +100036 switch (args->arg0) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -070037 case LHCALL_FLUSH_ASYNC:
Rusty Russellbff672e2007-07-26 10:41:04 -070038 /* This call does nothing, except by breaking out of the Guest
39 * it makes us process all the asynchronous hypercalls. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070040 break;
41 case LHCALL_LGUEST_INIT:
Rusty Russellbff672e2007-07-26 10:41:04 -070042 /* You can't get here unless you're already initialized. Don't
43 * do that. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070044 kill_guest(lg, "already have lguest_data");
45 break;
Balaji Raoec04b132007-12-28 14:26:24 +053046 case LHCALL_SHUTDOWN: {
47 /* Shutdown is such a trivial hypercall that we do it in four
Rusty Russellbff672e2007-07-26 10:41:04 -070048 * lines right here. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070049 char msg[128];
Rusty Russellbff672e2007-07-26 10:41:04 -070050 /* If the lgread fails, it will call kill_guest() itself; the
51 * kill_guest() with the message will be ignored. */
Rusty Russell2d37f942007-10-22 11:24:24 +100052 __lgread(lg, msg, args->arg1, sizeof(msg));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070053 msg[sizeof(msg)-1] = '\0';
54 kill_guest(lg, "CRASH: %s", msg);
Balaji Raoec04b132007-12-28 14:26:24 +053055 if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
56 lg->dead = ERR_PTR(-ERESTART);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070057 break;
58 }
59 case LHCALL_FLUSH_TLB:
Rusty Russellbff672e2007-07-26 10:41:04 -070060 /* FLUSH_TLB comes in two flavors, depending on the
61 * argument: */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100062 if (args->arg1)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070063 guest_pagetable_clear_all(lg);
64 else
65 guest_pagetable_flush_user(lg);
66 break;
Rusty Russellbff672e2007-07-26 10:41:04 -070067
68 /* All these calls simply pass the arguments through to the right
69 * routines. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070070 case LHCALL_NEW_PGTABLE:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100071 guest_new_pagetable(lg, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070072 break;
73 case LHCALL_SET_STACK:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100074 guest_set_stack(lg, args->arg1, args->arg2, args->arg3);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070075 break;
76 case LHCALL_SET_PTE:
Matias Zabaljaureguidf29f432007-10-22 11:03:33 +100077 guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070078 break;
79 case LHCALL_SET_PMD:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100080 guest_set_pmd(lg, args->arg1, args->arg2);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070081 break;
82 case LHCALL_SET_CLOCKEVENT:
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -020083 guest_set_clockevent(cpu, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070084 break;
85 case LHCALL_TS:
Rusty Russellbff672e2007-07-26 10:41:04 -070086 /* This sets the TS flag, as we saw used in run_guest(). */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100087 lg->ts = args->arg1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070088 break;
89 case LHCALL_HALT:
Rusty Russellbff672e2007-07-26 10:41:04 -070090 /* Similarly, this sets the halted flag for run_guest(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070091 lg->halted = 1;
92 break;
Rusty Russell15045272007-10-22 11:24:10 +100093 case LHCALL_NOTIFY:
94 lg->pending_notify = args->arg1;
95 break;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070096 default:
Rusty Russelle1e72962007-10-25 15:02:50 +100097 /* It should be an architecture-specific hypercall. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -020098 if (lguest_arch_do_hcall(cpu, args))
Jes Sorensenb410e7b2007-10-22 11:03:31 +100099 kill_guest(lg, "Bad hypercall %li\n", args->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700100 }
101}
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000102/*:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700103
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000104/*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".
Rusty Russellbff672e2007-07-26 10:41:04 -0700106 *
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). */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200111static void do_async_hcalls(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700112{
113 unsigned int i;
114 u8 st[LHCALL_RING_SIZE];
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200115 struct lguest *lg = cpu->lg;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700116
Rusty Russellbff672e2007-07-26 10:41:04 -0700117 /* For simplicity, we copy the entire call status array in at once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700118 if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
119 return;
120
Rusty Russellbff672e2007-07-26 10:41:04 -0700121 /* We process "struct lguest_data"s hcalls[] ring once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700122 for (i = 0; i < ARRAY_SIZE(st); i++) {
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000123 struct hcall_args args;
Rusty Russellbff672e2007-07-26 10:41:04 -0700124 /* 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. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200127 unsigned int n = cpu->next_hcall;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700128
Rusty Russellbff672e2007-07-26 10:41:04 -0700129 /* 0xFF means there's no call here (yet). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700130 if (st[n] == 0xFF)
131 break;
132
Rusty Russellbff672e2007-07-26 10:41:04 -0700133 /* OK, we have hypercall. Increment the "next_hcall" cursor,
134 * and wrap back to 0 if we reach the end. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200135 if (++cpu->next_hcall == LHCALL_RING_SIZE)
136 cpu->next_hcall = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700137
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000138 /* 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))) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700142 kill_guest(lg, "Fetching async hypercalls");
143 break;
144 }
145
Rusty Russellbff672e2007-07-26 10:41:04 -0700146 /* Do the hypercall, same as a normal one. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200147 do_hcall(cpu, &args);
Rusty Russellbff672e2007-07-26 10:41:04 -0700148
149 /* Mark the hypercall done. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700150 if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
151 kill_guest(lg, "Writing result for async hypercall");
152 break;
153 }
154
Rusty Russell15045272007-10-22 11:24:10 +1000155 /* Stop doing hypercalls if they want to notify the Launcher:
156 * it needs to service this first. */
157 if (lg->pending_notify)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700158 break;
159 }
160}
161
Rusty Russellbff672e2007-07-26 10:41:04 -0700162/* 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: */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200164static void initialize(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700165{
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200166 struct lguest *lg = cpu->lg;
Rusty Russellbff672e2007-07-26 10:41:04 -0700167 /* You can't do anything until you're initialized. The Guest knows the
168 * rules, so we're unforgiving here. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200169 if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
170 kill_guest(lg, "hypercall %li before INIT", cpu->hcall->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700171 return;
172 }
173
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200174 if (lguest_arch_init_hypercalls(cpu))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700175 kill_guest(lg, "bad guest page %p", lg->lguest_data);
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000176
Rusty Russellbff672e2007-07-26 10:41:04 -0700177 /* The Guest tells us where we're not to deliver interrupts by putting
178 * the range of addresses into "struct lguest_data". */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700179 if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
Rusty Russell47436aa2007-10-22 11:03:36 +1000180 || get_user(lg->noirq_end, &lg->lguest_data->noirq_end))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700181 kill_guest(lg, "bad guest page %p", lg->lguest_data);
182
Rusty Russelle1e72962007-10-25 15:02:50 +1000183 /* We write the current time into the Guest's data page once so it can
184 * set its clock. */
Rusty Russell6c8dca52007-07-27 13:42:52 +1000185 write_timestamp(lg);
186
Rusty Russell47436aa2007-10-22 11:03:36 +1000187 /* page_tables.c will also do some setup. */
188 page_table_guest_data_init(lg);
189
Rusty Russellbff672e2007-07-26 10:41:04 -0700190 /* 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
Rusty Russelle1e72962007-10-25 15:02:50 +1000192 * fault, but the old page might be (read-only) in the Guest
193 * pagetable. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700194 guest_pagetable_clear_all(lg);
195}
196
Rusty Russellbff672e2007-07-26 10:41:04 -0700197/*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 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200203void do_hypercalls(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700204{
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000205 /* Not initialized yet? This hypercall must do it. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200206 if (unlikely(!cpu->lg->lguest_data)) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000207 /* Set up the "struct lguest_data" */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200208 initialize(cpu);
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000209 /* Hcall is done. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200210 cpu->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700211 return;
212 }
213
Rusty Russellbff672e2007-07-26 10:41:04 -0700214 /* The Guest has initialized.
215 *
216 * Look in the hypercall ring for the async hypercalls: */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200217 do_async_hcalls(cpu);
Rusty Russellbff672e2007-07-26 10:41:04 -0700218
219 /* If we stopped reading the hypercall ring because the Guest did a
Rusty Russell15045272007-10-22 11:24:10 +1000220 * NOTIFY to the Launcher, we want to return now. Otherwise we do
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000221 * the hypercall. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200222 if (!cpu->lg->pending_notify) {
223 do_hcall(cpu, cpu->hcall);
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000224 /* 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 *
Rusty Russelle1e72962007-10-25 15:02:50 +1000230 * However, if we are signalled or the Guest sends I/O to the
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000231 * 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. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200234 cpu->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700235 }
236}
Rusty Russell6c8dca52007-07-27 13:42:52 +1000237
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. */
240void write_timestamp(struct lguest *lg)
241{
242 struct timespec now;
243 ktime_get_real_ts(&now);
Jes Sorensen891ff652007-10-22 10:56:22 +1000244 if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
Rusty Russell6c8dca52007-07-27 13:42:52 +1000245 kill_guest(lg, "Writing timestamp");
246}