blob: 3a53788ba450dc320e77b914f86b76577cacee6b [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. */
32static void do_hcall(struct lguest *lg, struct hcall_args *args)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070033{
Jes Sorensenb410e7b2007-10-22 11:03:31 +100034 switch (args->arg0) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -070035 case LHCALL_FLUSH_ASYNC:
Rusty Russellbff672e2007-07-26 10:41:04 -070036 /* This call does nothing, except by breaking out of the Guest
37 * it makes us process all the asynchronous hypercalls. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070038 break;
39 case LHCALL_LGUEST_INIT:
Rusty Russellbff672e2007-07-26 10:41:04 -070040 /* You can't get here unless you're already initialized. Don't
41 * do that. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070042 kill_guest(lg, "already have lguest_data");
43 break;
44 case LHCALL_CRASH: {
Rusty Russellbff672e2007-07-26 10:41:04 -070045 /* Crash is such a trivial hypercall that we do it in four
46 * lines right here. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070047 char msg[128];
Rusty Russellbff672e2007-07-26 10:41:04 -070048 /* If the lgread fails, it will call kill_guest() itself; the
49 * kill_guest() with the message will be ignored. */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100050 lgread(lg, msg, args->arg1, sizeof(msg));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070051 msg[sizeof(msg)-1] = '\0';
52 kill_guest(lg, "CRASH: %s", msg);
53 break;
54 }
55 case LHCALL_FLUSH_TLB:
Rusty Russellbff672e2007-07-26 10:41:04 -070056 /* FLUSH_TLB comes in two flavors, depending on the
57 * argument: */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100058 if (args->arg1)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070059 guest_pagetable_clear_all(lg);
60 else
61 guest_pagetable_flush_user(lg);
62 break;
Rusty Russellbff672e2007-07-26 10:41:04 -070063
64 /* All these calls simply pass the arguments through to the right
65 * routines. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070066 case LHCALL_NEW_PGTABLE:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100067 guest_new_pagetable(lg, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070068 break;
69 case LHCALL_SET_STACK:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100070 guest_set_stack(lg, args->arg1, args->arg2, args->arg3);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070071 break;
72 case LHCALL_SET_PTE:
Matias Zabaljaureguidf29f432007-10-22 11:03:33 +100073 guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070074 break;
75 case LHCALL_SET_PMD:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100076 guest_set_pmd(lg, args->arg1, args->arg2);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070077 break;
78 case LHCALL_SET_CLOCKEVENT:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100079 guest_set_clockevent(lg, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070080 break;
81 case LHCALL_TS:
Rusty Russellbff672e2007-07-26 10:41:04 -070082 /* This sets the TS flag, as we saw used in run_guest(). */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100083 lg->ts = args->arg1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070084 break;
85 case LHCALL_HALT:
Rusty Russellbff672e2007-07-26 10:41:04 -070086 /* Similarly, this sets the halted flag for run_guest(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070087 lg->halted = 1;
88 break;
Rusty Russell15045272007-10-22 11:24:10 +100089 case LHCALL_NOTIFY:
90 lg->pending_notify = args->arg1;
91 break;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070092 default:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100093 if (lguest_arch_do_hcall(lg, args))
94 kill_guest(lg, "Bad hypercall %li\n", args->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070095 }
96}
Jes Sorensenb410e7b2007-10-22 11:03:31 +100097/*:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -070098
Jes Sorensenb410e7b2007-10-22 11:03:31 +100099/*H:124 Asynchronous hypercalls are easy: we just look in the array in the
100 * Guest's "struct lguest_data" to see if any new ones are marked "ready".
Rusty Russellbff672e2007-07-26 10:41:04 -0700101 *
102 * We are careful to do these in order: obviously we respect the order the
103 * Guest put them in the ring, but we also promise the Guest that they will
104 * happen before any normal hypercall (which is why we check this before
105 * checking for a normal hcall). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700106static void do_async_hcalls(struct lguest *lg)
107{
108 unsigned int i;
109 u8 st[LHCALL_RING_SIZE];
110
Rusty Russellbff672e2007-07-26 10:41:04 -0700111 /* For simplicity, we copy the entire call status array in at once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700112 if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
113 return;
114
Rusty Russellbff672e2007-07-26 10:41:04 -0700115 /* We process "struct lguest_data"s hcalls[] ring once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700116 for (i = 0; i < ARRAY_SIZE(st); i++) {
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000117 struct hcall_args args;
Rusty Russellbff672e2007-07-26 10:41:04 -0700118 /* We remember where we were up to from last time. This makes
119 * sure that the hypercalls are done in the order the Guest
120 * places them in the ring. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700121 unsigned int n = lg->next_hcall;
122
Rusty Russellbff672e2007-07-26 10:41:04 -0700123 /* 0xFF means there's no call here (yet). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700124 if (st[n] == 0xFF)
125 break;
126
Rusty Russellbff672e2007-07-26 10:41:04 -0700127 /* OK, we have hypercall. Increment the "next_hcall" cursor,
128 * and wrap back to 0 if we reach the end. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700129 if (++lg->next_hcall == LHCALL_RING_SIZE)
130 lg->next_hcall = 0;
131
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000132 /* Copy the hypercall arguments into a local copy of
133 * the hcall_args struct. */
134 if (copy_from_user(&args, &lg->lguest_data->hcalls[n],
135 sizeof(struct hcall_args))) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700136 kill_guest(lg, "Fetching async hypercalls");
137 break;
138 }
139
Rusty Russellbff672e2007-07-26 10:41:04 -0700140 /* Do the hypercall, same as a normal one. */
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000141 do_hcall(lg, &args);
Rusty Russellbff672e2007-07-26 10:41:04 -0700142
143 /* Mark the hypercall done. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700144 if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
145 kill_guest(lg, "Writing result for async hypercall");
146 break;
147 }
148
Rusty Russell15045272007-10-22 11:24:10 +1000149 /* Stop doing hypercalls if they want to notify the Launcher:
150 * it needs to service this first. */
151 if (lg->pending_notify)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700152 break;
153 }
154}
155
Rusty Russellbff672e2007-07-26 10:41:04 -0700156/* Last of all, we look at what happens first of all. The very first time the
157 * Guest makes a hypercall, we end up here to set things up: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700158static void initialize(struct lguest *lg)
159{
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700160
Rusty Russellbff672e2007-07-26 10:41:04 -0700161 /* You can't do anything until you're initialized. The Guest knows the
162 * rules, so we're unforgiving here. */
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000163 if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) {
164 kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700165 return;
166 }
167
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000168 if (lguest_arch_init_hypercalls(lg))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700169 kill_guest(lg, "bad guest page %p", lg->lguest_data);
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000170
Rusty Russellbff672e2007-07-26 10:41:04 -0700171 /* The Guest tells us where we're not to deliver interrupts by putting
172 * the range of addresses into "struct lguest_data". */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700173 if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
Rusty Russell47436aa2007-10-22 11:03:36 +1000174 || get_user(lg->noirq_end, &lg->lguest_data->noirq_end))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700175 kill_guest(lg, "bad guest page %p", lg->lguest_data);
176
Rusty Russell6c8dca52007-07-27 13:42:52 +1000177 /* We write the current time into the Guest's data page once now. */
178 write_timestamp(lg);
179
Rusty Russell47436aa2007-10-22 11:03:36 +1000180 /* page_tables.c will also do some setup. */
181 page_table_guest_data_init(lg);
182
Rusty Russellbff672e2007-07-26 10:41:04 -0700183 /* This is the one case where the above accesses might have been the
184 * first write to a Guest page. This may have caused a copy-on-write
185 * fault, but the Guest might be referring to the old (read-only)
186 * page. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700187 guest_pagetable_clear_all(lg);
188}
189
Rusty Russellbff672e2007-07-26 10:41:04 -0700190/*H:100
191 * Hypercalls
192 *
193 * Remember from the Guest, hypercalls come in two flavors: normal and
194 * asynchronous. This file handles both of types.
195 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700196void do_hypercalls(struct lguest *lg)
197{
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000198 /* Not initialized yet? This hypercall must do it. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700199 if (unlikely(!lg->lguest_data)) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000200 /* Set up the "struct lguest_data" */
201 initialize(lg);
202 /* Hcall is done. */
203 lg->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700204 return;
205 }
206
Rusty Russellbff672e2007-07-26 10:41:04 -0700207 /* The Guest has initialized.
208 *
209 * Look in the hypercall ring for the async hypercalls: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700210 do_async_hcalls(lg);
Rusty Russellbff672e2007-07-26 10:41:04 -0700211
212 /* If we stopped reading the hypercall ring because the Guest did a
Rusty Russell15045272007-10-22 11:24:10 +1000213 * NOTIFY to the Launcher, we want to return now. Otherwise we do
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000214 * the hypercall. */
Rusty Russell15045272007-10-22 11:24:10 +1000215 if (!lg->pending_notify) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000216 do_hcall(lg, lg->hcall);
217 /* Tricky point: we reset the hcall pointer to mark the
218 * hypercall as "done". We use the hcall pointer rather than
219 * the trap number to indicate a hypercall is pending.
220 * Normally it doesn't matter: the Guest will run again and
221 * update the trap number before we come back here.
222 *
223 * However, if we are signalled or the Guest sends DMA to the
224 * Launcher, the run_guest() loop will exit without running the
225 * Guest. When it comes back it would try to re-run the
226 * hypercall. */
227 lg->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700228 }
229}
Rusty Russell6c8dca52007-07-27 13:42:52 +1000230
231/* This routine supplies the Guest with time: it's used for wallclock time at
232 * initial boot and as a rough time source if the TSC isn't available. */
233void write_timestamp(struct lguest *lg)
234{
235 struct timespec now;
236 ktime_get_real_ts(&now);
Jes Sorensen891ff652007-10-22 10:56:22 +1000237 if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
Rusty Russell6c8dca52007-07-27 13:42:52 +1000238 kill_guest(lg, "Writing timestamp");
239}