blob: c29ffa19cb74410e5b37ddc74bf19d4a45c5d9e7 [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>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -020026#include <linux/ktime.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070027#include <asm/page.h>
28#include <asm/pgtable.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070029#include "lg.h"
30
Jes Sorensenb410e7b2007-10-22 11:03:31 +100031/*H:120 This is the core hypercall routine: where the Guest gets what it wants.
Rusty Russella6bd8e12008-03-28 11:05:53 -050032 * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -020033static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
Rusty Russelld7e28ff2007-07-19 01:49:23 -070034{
Jes Sorensenb410e7b2007-10-22 11:03:31 +100035 switch (args->arg0) {
Rusty Russelld7e28ff2007-07-19 01:49:23 -070036 case LHCALL_FLUSH_ASYNC:
Rusty Russellbff672e2007-07-26 10:41:04 -070037 /* This call does nothing, except by breaking out of the Guest
38 * it makes us process all the asynchronous hypercalls. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070039 break;
Rusty Russella32a88132009-06-12 22:27:02 -060040 case LHCALL_SEND_INTERRUPTS:
41 /* This call does nothing too, but by breaking out of the Guest
42 * it makes us process any pending interrupts. */
43 break;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070044 case LHCALL_LGUEST_INIT:
Rusty Russellbff672e2007-07-26 10:41:04 -070045 /* You can't get here unless you're already initialized. Don't
46 * do that. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020047 kill_guest(cpu, "already have lguest_data");
Rusty Russelld7e28ff2007-07-19 01:49:23 -070048 break;
Balaji Raoec04b132007-12-28 14:26:24 +053049 case LHCALL_SHUTDOWN: {
50 /* Shutdown is such a trivial hypercall that we do it in four
Rusty Russellbff672e2007-07-26 10:41:04 -070051 * lines right here. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070052 char msg[128];
Rusty Russellbff672e2007-07-26 10:41:04 -070053 /* If the lgread fails, it will call kill_guest() itself; the
54 * kill_guest() with the message will be ignored. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020055 __lgread(cpu, msg, args->arg1, sizeof(msg));
Rusty Russelld7e28ff2007-07-19 01:49:23 -070056 msg[sizeof(msg)-1] = '\0';
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020057 kill_guest(cpu, "CRASH: %s", msg);
Balaji Raoec04b132007-12-28 14:26:24 +053058 if (args->arg2 == LGUEST_SHUTDOWN_RESTART)
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020059 cpu->lg->dead = ERR_PTR(-ERESTART);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070060 break;
61 }
62 case LHCALL_FLUSH_TLB:
Rusty Russellbff672e2007-07-26 10:41:04 -070063 /* FLUSH_TLB comes in two flavors, depending on the
64 * argument: */
Jes Sorensenb410e7b2007-10-22 11:03:31 +100065 if (args->arg1)
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -020066 guest_pagetable_clear_all(cpu);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070067 else
Glauber de Oliveira Costa17136082008-01-07 11:05:37 -020068 guest_pagetable_flush_user(cpu);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070069 break;
Rusty Russellbff672e2007-07-26 10:41:04 -070070
71 /* All these calls simply pass the arguments through to the right
72 * routines. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070073 case LHCALL_NEW_PGTABLE:
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -020074 guest_new_pagetable(cpu, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070075 break;
76 case LHCALL_SET_STACK:
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -020077 guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070078 break;
79 case LHCALL_SET_PTE:
Matias Zabaljaureguiacdd0b62009-06-12 22:27:07 -060080#ifdef CONFIG_X86_PAE
81 guest_set_pte(cpu, args->arg1, args->arg2,
82 __pte(args->arg3 | (u64)args->arg4 << 32));
83#else
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -020084 guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));
Matias Zabaljaureguiacdd0b62009-06-12 22:27:07 -060085#endif
Rusty Russelld7e28ff2007-07-19 01:49:23 -070086 break;
Matias Zabaljaureguiebe0ba82009-05-30 15:48:08 -030087 case LHCALL_SET_PGD:
88 guest_set_pgd(cpu->lg, args->arg1, args->arg2);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070089 break;
Matias Zabaljaureguiacdd0b62009-06-12 22:27:07 -060090#ifdef CONFIG_X86_PAE
91 case LHCALL_SET_PMD:
92 guest_set_pmd(cpu->lg, args->arg1, args->arg2);
93 break;
94#endif
Rusty Russelld7e28ff2007-07-19 01:49:23 -070095 case LHCALL_SET_CLOCKEVENT:
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -020096 guest_set_clockevent(cpu, args->arg1);
Rusty Russelld7e28ff2007-07-19 01:49:23 -070097 break;
98 case LHCALL_TS:
Rusty Russellbff672e2007-07-26 10:41:04 -070099 /* This sets the TS flag, as we saw used in run_guest(). */
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -0200100 cpu->ts = args->arg1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700101 break;
102 case LHCALL_HALT:
Rusty Russellbff672e2007-07-26 10:41:04 -0700103 /* Similarly, this sets the halted flag for run_guest(). */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200104 cpu->halted = 1;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700105 break;
Rusty Russell15045272007-10-22 11:24:10 +1000106 case LHCALL_NOTIFY:
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200107 cpu->pending_notify = args->arg1;
Rusty Russell15045272007-10-22 11:24:10 +1000108 break;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700109 default:
Rusty Russelle1e72962007-10-25 15:02:50 +1000110 /* It should be an architecture-specific hypercall. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200111 if (lguest_arch_do_hcall(cpu, args))
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200112 kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700113 }
114}
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000115/*:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700116
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000117/*H:124 Asynchronous hypercalls are easy: we just look in the array in the
118 * Guest's "struct lguest_data" to see if any new ones are marked "ready".
Rusty Russellbff672e2007-07-26 10:41:04 -0700119 *
120 * We are careful to do these in order: obviously we respect the order the
121 * Guest put them in the ring, but we also promise the Guest that they will
122 * happen before any normal hypercall (which is why we check this before
123 * checking for a normal hcall). */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200124static void do_async_hcalls(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700125{
126 unsigned int i;
127 u8 st[LHCALL_RING_SIZE];
128
Rusty Russellbff672e2007-07-26 10:41:04 -0700129 /* For simplicity, we copy the entire call status array in at once. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200130 if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700131 return;
132
Rusty Russellbff672e2007-07-26 10:41:04 -0700133 /* We process "struct lguest_data"s hcalls[] ring once. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700134 for (i = 0; i < ARRAY_SIZE(st); i++) {
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000135 struct hcall_args args;
Rusty Russellbff672e2007-07-26 10:41:04 -0700136 /* We remember where we were up to from last time. This makes
137 * sure that the hypercalls are done in the order the Guest
138 * places them in the ring. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200139 unsigned int n = cpu->next_hcall;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700140
Rusty Russellbff672e2007-07-26 10:41:04 -0700141 /* 0xFF means there's no call here (yet). */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700142 if (st[n] == 0xFF)
143 break;
144
Rusty Russellbff672e2007-07-26 10:41:04 -0700145 /* OK, we have hypercall. Increment the "next_hcall" cursor,
146 * and wrap back to 0 if we reach the end. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200147 if (++cpu->next_hcall == LHCALL_RING_SIZE)
148 cpu->next_hcall = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700149
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000150 /* Copy the hypercall arguments into a local copy of
151 * the hcall_args struct. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200152 if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
Jes Sorensenb410e7b2007-10-22 11:03:31 +1000153 sizeof(struct hcall_args))) {
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200154 kill_guest(cpu, "Fetching async hypercalls");
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700155 break;
156 }
157
Rusty Russellbff672e2007-07-26 10:41:04 -0700158 /* Do the hypercall, same as a normal one. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200159 do_hcall(cpu, &args);
Rusty Russellbff672e2007-07-26 10:41:04 -0700160
161 /* Mark the hypercall done. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200162 if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
163 kill_guest(cpu, "Writing result for async hypercall");
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700164 break;
165 }
166
Rusty Russell15045272007-10-22 11:24:10 +1000167 /* Stop doing hypercalls if they want to notify the Launcher:
168 * it needs to service this first. */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200169 if (cpu->pending_notify)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700170 break;
171 }
172}
173
Rusty Russellbff672e2007-07-26 10:41:04 -0700174/* Last of all, we look at what happens first of all. The very first time the
175 * Guest makes a hypercall, we end up here to set things up: */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200176static void initialize(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700177{
Rusty Russellbff672e2007-07-26 10:41:04 -0700178 /* You can't do anything until you're initialized. The Guest knows the
179 * rules, so we're unforgiving here. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200180 if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200181 kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700182 return;
183 }
184
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200185 if (lguest_arch_init_hypercalls(cpu))
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200186 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000187
Rusty Russellbff672e2007-07-26 10:41:04 -0700188 /* The Guest tells us where we're not to deliver interrupts by putting
189 * the range of addresses into "struct lguest_data". */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200190 if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)
191 || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))
192 kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700193
Rusty Russelle1e72962007-10-25 15:02:50 +1000194 /* We write the current time into the Guest's data page once so it can
195 * set its clock. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200196 write_timestamp(cpu);
Rusty Russell6c8dca52007-07-27 13:42:52 +1000197
Rusty Russell47436aa2007-10-22 11:03:36 +1000198 /* page_tables.c will also do some setup. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200199 page_table_guest_data_init(cpu);
Rusty Russell47436aa2007-10-22 11:03:36 +1000200
Rusty Russellbff672e2007-07-26 10:41:04 -0700201 /* This is the one case where the above accesses might have been the
202 * first write to a Guest page. This may have caused a copy-on-write
Rusty Russelle1e72962007-10-25 15:02:50 +1000203 * fault, but the old page might be (read-only) in the Guest
204 * pagetable. */
Glauber de Oliveira Costa4665ac8e2008-01-07 11:05:35 -0200205 guest_pagetable_clear_all(cpu);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700206}
Rusty Russella6bd8e12008-03-28 11:05:53 -0500207/*:*/
208
209/*M:013 If a Guest reads from a page (so creates a mapping) that it has never
210 * written to, and then the Launcher writes to it (ie. the output of a virtual
211 * device), the Guest will still see the old page. In practice, this never
212 * happens: why would the Guest read a page which it has never written to? But
213 * a similar scenario might one day bite us, so it's worth mentioning. :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700214
Rusty Russellbff672e2007-07-26 10:41:04 -0700215/*H:100
216 * Hypercalls
217 *
218 * Remember from the Guest, hypercalls come in two flavors: normal and
219 * asynchronous. This file handles both of types.
220 */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200221void do_hypercalls(struct lg_cpu *cpu)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700222{
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000223 /* Not initialized yet? This hypercall must do it. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200224 if (unlikely(!cpu->lg->lguest_data)) {
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000225 /* Set up the "struct lguest_data" */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200226 initialize(cpu);
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000227 /* Hcall is done. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200228 cpu->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700229 return;
230 }
231
Rusty Russellbff672e2007-07-26 10:41:04 -0700232 /* The Guest has initialized.
233 *
234 * Look in the hypercall ring for the async hypercalls: */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200235 do_async_hcalls(cpu);
Rusty Russellbff672e2007-07-26 10:41:04 -0700236
237 /* If we stopped reading the hypercall ring because the Guest did a
Rusty Russell15045272007-10-22 11:24:10 +1000238 * NOTIFY to the Launcher, we want to return now. Otherwise we do
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000239 * the hypercall. */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200240 if (!cpu->pending_notify) {
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200241 do_hcall(cpu, cpu->hcall);
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000242 /* Tricky point: we reset the hcall pointer to mark the
243 * hypercall as "done". We use the hcall pointer rather than
244 * the trap number to indicate a hypercall is pending.
245 * Normally it doesn't matter: the Guest will run again and
246 * update the trap number before we come back here.
247 *
Rusty Russelle1e72962007-10-25 15:02:50 +1000248 * However, if we are signalled or the Guest sends I/O to the
Rusty Russellcc6d4fb2007-10-22 11:03:30 +1000249 * Launcher, the run_guest() loop will exit without running the
250 * Guest. When it comes back it would try to re-run the
Rusty Russella6bd8e12008-03-28 11:05:53 -0500251 * hypercall. Finding that bug sucked. */
Glauber de Oliveira Costa73044f02008-01-07 11:05:27 -0200252 cpu->hcall = NULL;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700253 }
254}
Rusty Russell6c8dca52007-07-27 13:42:52 +1000255
256/* This routine supplies the Guest with time: it's used for wallclock time at
257 * initial boot and as a rough time source if the TSC isn't available. */
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200258void write_timestamp(struct lg_cpu *cpu)
Rusty Russell6c8dca52007-07-27 13:42:52 +1000259{
260 struct timespec now;
261 ktime_get_real_ts(&now);
Glauber de Oliveira Costa382ac6b2008-01-17 19:19:42 -0200262 if (copy_to_user(&cpu->lg->lguest_data->time,
263 &now, sizeof(struct timespec)))
264 kill_guest(cpu, "Writing timestamp");
Rusty Russell6c8dca52007-07-27 13:42:52 +1000265}