blob: 05fad6fa804923eed6688e381adbe9c12c012f20 [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;
Balaji Raoec04b132007-12-28 14:26:24 +053044 case LHCALL_SHUTDOWN: {
45 /* Shutdown is such a trivial hypercall that we do it in four
Rusty Russellbff672e2007-07-26 10:41:04 -070046 * 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. */
Rusty Russell2d37f942007-10-22 11:24:24 +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);
Balaji Raoec04b132007-12-28 14:26:24 +053053 if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
54 lg->dead = ERR_PTR(-ERESTART);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070055 break;
56 }
57 case LHCALL_FLUSH_TLB:
Rusty Russellbff672e2007-07-26 10:41:04 -070058 /* FLUSH_TLB comes in two flavors, depending on the
59 * argument: */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100060 if (args->arg1)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070061 guest_pagetable_clear_all(lg);
62 else
63 guest_pagetable_flush_user(lg);
64 break;
Rusty Russellbff672e2007-07-26 10:41:04 -070065
66 /* All these calls simply pass the arguments through to the right
67 * routines. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070068 case LHCALL_NEW_PGTABLE:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100069 guest_new_pagetable(lg, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070070 break;
71 case LHCALL_SET_STACK:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100072 guest_set_stack(lg, args->arg1, args->arg2, args->arg3);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070073 break;
74 case LHCALL_SET_PTE:
Matias Zabaljaureguidf29f432007-10-22 11:03:33 +100075 guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070076 break;
77 case LHCALL_SET_PMD:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100078 guest_set_pmd(lg, args->arg1, args->arg2);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070079 break;
80 case LHCALL_SET_CLOCKEVENT:
Jes Sorensenb410e7b2007-10-22 11:03:31 +100081 guest_set_clockevent(lg, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070082 break;
83 case LHCALL_TS:
Rusty Russellbff672e2007-07-26 10:41:04 -070084 /* This sets the TS flag, as we saw used in run_guest(). */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100085 lg->ts = args->arg1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070086 break;
87 case LHCALL_HALT:
Rusty Russellbff672e2007-07-26 10:41:04 -070088 /* Similarly, this sets the halted flag for run_guest(). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070089 lg->halted = 1;
90 break;
Rusty Russell15045272007-10-22 11:24:10 +100091 case LHCALL_NOTIFY:
92 lg->pending_notify = args->arg1;
93 break;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070094 default:
Rusty Russelle1e72962007-10-25 15:02:50 +100095 /* It should be an architecture-specific hypercall. */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100096 if (lguest_arch_do_hcall(lg, args))
97 kill_guest(lg, "Bad hypercall %li\n", args->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070098 }
99}
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000100/*:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700101
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000102/*H:124 Asynchronous hypercalls are easy: we just look in the array in the
103 * Guest's "struct lguest_data" to see if any new ones are marked "ready".
Rusty Russellbff672e2007-07-26 10:41:04 -0700104 *
105 * We are careful to do these in order: obviously we respect the order the
106 * Guest put them in the ring, but we also promise the Guest that they will
107 * happen before any normal hypercall (which is why we check this before
108 * checking for a normal hcall). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700109static void do_async_hcalls(struct lguest *lg)
110{
111 unsigned int i;
112 u8 st[LHCALL_RING_SIZE];
113
Rusty Russellbff672e2007-07-26 10:41:04 -0700114 /* For simplicity, we copy the entire call status array in at once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700115 if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
116 return;
117
Rusty Russellbff672e2007-07-26 10:41:04 -0700118 /* We process "struct lguest_data"s hcalls[] ring once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700119 for (i = 0; i < ARRAY_SIZE(st); i++) {
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000120 struct hcall_args args;
Rusty Russellbff672e2007-07-26 10:41:04 -0700121 /* We remember where we were up to from last time. This makes
122 * sure that the hypercalls are done in the order the Guest
123 * places them in the ring. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700124 unsigned int n = lg->next_hcall;
125
Rusty Russellbff672e2007-07-26 10:41:04 -0700126 /* 0xFF means there's no call here (yet). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700127 if (st[n] == 0xFF)
128 break;
129
Rusty Russellbff672e2007-07-26 10:41:04 -0700130 /* OK, we have hypercall. Increment the "next_hcall" cursor,
131 * and wrap back to 0 if we reach the end. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700132 if (++lg->next_hcall == LHCALL_RING_SIZE)
133 lg->next_hcall = 0;
134
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000135 /* Copy the hypercall arguments into a local copy of
136 * the hcall_args struct. */
137 if (copy_from_user(&args, &lg->lguest_data->hcalls[n],
138 sizeof(struct hcall_args))) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700139 kill_guest(lg, "Fetching async hypercalls");
140 break;
141 }
142
Rusty Russellbff672e2007-07-26 10:41:04 -0700143 /* Do the hypercall, same as a normal one. */
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000144 do_hcall(lg, &args);
Rusty Russellbff672e2007-07-26 10:41:04 -0700145
146 /* Mark the hypercall done. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700147 if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
148 kill_guest(lg, "Writing result for async hypercall");
149 break;
150 }
151
Rusty Russell15045272007-10-22 11:24:10 +1000152 /* Stop doing hypercalls if they want to notify the Launcher:
153 * it needs to service this first. */
154 if (lg->pending_notify)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700155 break;
156 }
157}
158
Rusty Russellbff672e2007-07-26 10:41:04 -0700159/* Last of all, we look at what happens first of all. The very first time the
160 * Guest makes a hypercall, we end up here to set things up: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700161static void initialize(struct lguest *lg)
162{
Rusty Russellbff672e2007-07-26 10:41:04 -0700163 /* You can't do anything until you're initialized. The Guest knows the
164 * rules, so we're unforgiving here. */
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000165 if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) {
166 kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700167 return;
168 }
169
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000170 if (lguest_arch_init_hypercalls(lg))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700171 kill_guest(lg, "bad guest page %p", lg->lguest_data);
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000172
Rusty Russellbff672e2007-07-26 10:41:04 -0700173 /* The Guest tells us where we're not to deliver interrupts by putting
174 * the range of addresses into "struct lguest_data". */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700175 if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
Rusty Russell47436aa2007-10-22 11:03:36 +1000176 || get_user(lg->noirq_end, &lg->lguest_data->noirq_end))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700177 kill_guest(lg, "bad guest page %p", lg->lguest_data);
178
Rusty Russelle1e72962007-10-25 15:02:50 +1000179 /* We write the current time into the Guest's data page once so it can
180 * set its clock. */
Rusty Russell6c8dca52007-07-27 13:42:52 +1000181 write_timestamp(lg);
182
Rusty Russell47436aa2007-10-22 11:03:36 +1000183 /* page_tables.c will also do some setup. */
184 page_table_guest_data_init(lg);
185
Rusty Russellbff672e2007-07-26 10:41:04 -0700186 /* This is the one case where the above accesses might have been the
187 * first write to a Guest page. This may have caused a copy-on-write
Rusty Russelle1e72962007-10-25 15:02:50 +1000188 * fault, but the old page might be (read-only) in the Guest
189 * pagetable. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700190 guest_pagetable_clear_all(lg);
191}
192
Rusty Russellbff672e2007-07-26 10:41:04 -0700193/*H:100
194 * Hypercalls
195 *
196 * Remember from the Guest, hypercalls come in two flavors: normal and
197 * asynchronous. This file handles both of types.
198 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700199void do_hypercalls(struct lguest *lg)
200{
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000201 /* Not initialized yet? This hypercall must do it. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700202 if (unlikely(!lg->lguest_data)) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000203 /* Set up the "struct lguest_data" */
204 initialize(lg);
205 /* Hcall is done. */
206 lg->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700207 return;
208 }
209
Rusty Russellbff672e2007-07-26 10:41:04 -0700210 /* The Guest has initialized.
211 *
212 * Look in the hypercall ring for the async hypercalls: */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700213 do_async_hcalls(lg);
Rusty Russellbff672e2007-07-26 10:41:04 -0700214
215 /* If we stopped reading the hypercall ring because the Guest did a
Rusty Russell15045272007-10-22 11:24:10 +1000216 * NOTIFY to the Launcher, we want to return now. Otherwise we do
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000217 * the hypercall. */
Rusty Russell15045272007-10-22 11:24:10 +1000218 if (!lg->pending_notify) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000219 do_hcall(lg, lg->hcall);
220 /* Tricky point: we reset the hcall pointer to mark the
221 * hypercall as "done". We use the hcall pointer rather than
222 * the trap number to indicate a hypercall is pending.
223 * Normally it doesn't matter: the Guest will run again and
224 * update the trap number before we come back here.
225 *
Rusty Russelle1e72962007-10-25 15:02:50 +1000226 * However, if we are signalled or the Guest sends I/O to the
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000227 * Launcher, the run_guest() loop will exit without running the
228 * Guest. When it comes back it would try to re-run the
229 * hypercall. */
230 lg->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700231 }
232}
Rusty Russell6c8dca52007-07-27 13:42:52 +1000233
234/* This routine supplies the Guest with time: it's used for wallclock time at
235 * initial boot and as a rough time source if the TSC isn't available. */
236void write_timestamp(struct lguest *lg)
237{
238 struct timespec now;
239 ktime_get_real_ts(&now);
Jes Sorensen891ff652007-10-22 10:56:22 +1000240 if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
Rusty Russell6c8dca52007-07-27 13:42:52 +1000241 kill_guest(lg, "Writing timestamp");
242}