x86: cpa: move clflush_cache_range()
[wrt350n-kernel.git] / drivers / lguest / hypercalls.c
blobb478affe8f91331733a288fa039fce50012e8409
1 /*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. :*/
7 /* Copyright (C) 2006 Rusty Russell IBM Corporation
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.
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.
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
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>
28 #include "lg.h"
30 /*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. */
32 static void do_hcall(struct lguest *lg, struct hcall_args *args)
34 switch (args->arg0) {
35 case LHCALL_FLUSH_ASYNC:
36 /* This call does nothing, except by breaking out of the Guest
37 * it makes us process all the asynchronous hypercalls. */
38 break;
39 case LHCALL_LGUEST_INIT:
40 /* You can't get here unless you're already initialized. Don't
41 * do that. */
42 kill_guest(lg, "already have lguest_data");
43 break;
44 case LHCALL_CRASH: {
45 /* Crash is such a trivial hypercall that we do it in four
46 * lines right here. */
47 char msg[128];
48 /* If the lgread fails, it will call kill_guest() itself; the
49 * kill_guest() with the message will be ignored. */
50 __lgread(lg, msg, args->arg1, sizeof(msg));
51 msg[sizeof(msg)-1] = '\0';
52 kill_guest(lg, "CRASH: %s", msg);
53 break;
55 case LHCALL_FLUSH_TLB:
56 /* FLUSH_TLB comes in two flavors, depending on the
57 * argument: */
58 if (args->arg1)
59 guest_pagetable_clear_all(lg);
60 else
61 guest_pagetable_flush_user(lg);
62 break;
64 /* All these calls simply pass the arguments through to the right
65 * routines. */
66 case LHCALL_NEW_PGTABLE:
67 guest_new_pagetable(lg, args->arg1);
68 break;
69 case LHCALL_SET_STACK:
70 guest_set_stack(lg, args->arg1, args->arg2, args->arg3);
71 break;
72 case LHCALL_SET_PTE:
73 guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3));
74 break;
75 case LHCALL_SET_PMD:
76 guest_set_pmd(lg, args->arg1, args->arg2);
77 break;
78 case LHCALL_SET_CLOCKEVENT:
79 guest_set_clockevent(lg, args->arg1);
80 break;
81 case LHCALL_TS:
82 /* This sets the TS flag, as we saw used in run_guest(). */
83 lg->ts = args->arg1;
84 break;
85 case LHCALL_HALT:
86 /* Similarly, this sets the halted flag for run_guest(). */
87 lg->halted = 1;
88 break;
89 case LHCALL_NOTIFY:
90 lg->pending_notify = args->arg1;
91 break;
92 default:
93 /* It should be an architecture-specific hypercall. */
94 if (lguest_arch_do_hcall(lg, args))
95 kill_guest(lg, "Bad hypercall %li\n", args->arg0);
98 /*:*/
100 /*H:124 Asynchronous hypercalls are easy: we just look in the array in the
101 * Guest's "struct lguest_data" to see if any new ones are marked "ready".
103 * We are careful to do these in order: obviously we respect the order the
104 * Guest put them in the ring, but we also promise the Guest that they will
105 * happen before any normal hypercall (which is why we check this before
106 * checking for a normal hcall). */
107 static void do_async_hcalls(struct lguest *lg)
109 unsigned int i;
110 u8 st[LHCALL_RING_SIZE];
112 /* For simplicity, we copy the entire call status array in at once. */
113 if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
114 return;
116 /* We process "struct lguest_data"s hcalls[] ring once. */
117 for (i = 0; i < ARRAY_SIZE(st); i++) {
118 struct hcall_args args;
119 /* We remember where we were up to from last time. This makes
120 * sure that the hypercalls are done in the order the Guest
121 * places them in the ring. */
122 unsigned int n = lg->next_hcall;
124 /* 0xFF means there's no call here (yet). */
125 if (st[n] == 0xFF)
126 break;
128 /* OK, we have hypercall. Increment the "next_hcall" cursor,
129 * and wrap back to 0 if we reach the end. */
130 if (++lg->next_hcall == LHCALL_RING_SIZE)
131 lg->next_hcall = 0;
133 /* Copy the hypercall arguments into a local copy of
134 * the hcall_args struct. */
135 if (copy_from_user(&args, &lg->lguest_data->hcalls[n],
136 sizeof(struct hcall_args))) {
137 kill_guest(lg, "Fetching async hypercalls");
138 break;
141 /* Do the hypercall, same as a normal one. */
142 do_hcall(lg, &args);
144 /* Mark the hypercall done. */
145 if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
146 kill_guest(lg, "Writing result for async hypercall");
147 break;
150 /* Stop doing hypercalls if they want to notify the Launcher:
151 * it needs to service this first. */
152 if (lg->pending_notify)
153 break;
157 /* Last of all, we look at what happens first of all. The very first time the
158 * Guest makes a hypercall, we end up here to set things up: */
159 static void initialize(struct lguest *lg)
161 /* You can't do anything until you're initialized. The Guest knows the
162 * rules, so we're unforgiving here. */
163 if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) {
164 kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0);
165 return;
168 if (lguest_arch_init_hypercalls(lg))
169 kill_guest(lg, "bad guest page %p", lg->lguest_data);
171 /* The Guest tells us where we're not to deliver interrupts by putting
172 * the range of addresses into "struct lguest_data". */
173 if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
174 || get_user(lg->noirq_end, &lg->lguest_data->noirq_end))
175 kill_guest(lg, "bad guest page %p", lg->lguest_data);
177 /* We write the current time into the Guest's data page once so it can
178 * set its clock. */
179 write_timestamp(lg);
181 /* page_tables.c will also do some setup. */
182 page_table_guest_data_init(lg);
184 /* This is the one case where the above accesses might have been the
185 * first write to a Guest page. This may have caused a copy-on-write
186 * fault, but the old page might be (read-only) in the Guest
187 * pagetable. */
188 guest_pagetable_clear_all(lg);
191 /*H:100
192 * Hypercalls
194 * Remember from the Guest, hypercalls come in two flavors: normal and
195 * asynchronous. This file handles both of types.
197 void do_hypercalls(struct lguest *lg)
199 /* Not initialized yet? This hypercall must do it. */
200 if (unlikely(!lg->lguest_data)) {
201 /* Set up the "struct lguest_data" */
202 initialize(lg);
203 /* Hcall is done. */
204 lg->hcall = NULL;
205 return;
208 /* The Guest has initialized.
210 * Look in the hypercall ring for the async hypercalls: */
211 do_async_hcalls(lg);
213 /* If we stopped reading the hypercall ring because the Guest did a
214 * NOTIFY to the Launcher, we want to return now. Otherwise we do
215 * the hypercall. */
216 if (!lg->pending_notify) {
217 do_hcall(lg, lg->hcall);
218 /* Tricky point: we reset the hcall pointer to mark the
219 * hypercall as "done". We use the hcall pointer rather than
220 * the trap number to indicate a hypercall is pending.
221 * Normally it doesn't matter: the Guest will run again and
222 * update the trap number before we come back here.
224 * However, if we are signalled or the Guest sends I/O to the
225 * Launcher, the run_guest() loop will exit without running the
226 * Guest. When it comes back it would try to re-run the
227 * hypercall. */
228 lg->hcall = NULL;
232 /* This routine supplies the Guest with time: it's used for wallclock time at
233 * initial boot and as a rough time source if the TSC isn't available. */
234 void write_timestamp(struct lguest *lg)
236 struct timespec now;
237 ktime_get_real_ts(&now);
238 if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec)))
239 kill_guest(lg, "Writing timestamp");