Rusty Russell | f938d2c | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 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. :*/ |
| 6 | |
| 7 | /* Copyright (C) 2006 Rusty Russell IBM Corporation |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 8 | |
| 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 Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 28 | #include "lg.h" |
| 29 | |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 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) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 33 | { |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 34 | switch (args->arg0) { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 35 | case LHCALL_FLUSH_ASYNC: |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 36 | /* This call does nothing, except by breaking out of the Guest |
| 37 | * it makes us process all the asynchronous hypercalls. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 38 | break; |
| 39 | case LHCALL_LGUEST_INIT: |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 40 | /* You can't get here unless you're already initialized. Don't |
| 41 | * do that. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 42 | kill_guest(lg, "already have lguest_data"); |
| 43 | break; |
| 44 | case LHCALL_CRASH: { |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 45 | /* Crash is such a trivial hypercall that we do it in four |
| 46 | * lines right here. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 47 | char msg[128]; |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 48 | /* If the lgread fails, it will call kill_guest() itself; the |
| 49 | * kill_guest() with the message will be ignored. */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 50 | lgread(lg, msg, args->arg1, sizeof(msg)); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 51 | msg[sizeof(msg)-1] = '\0'; |
| 52 | kill_guest(lg, "CRASH: %s", msg); |
| 53 | break; |
| 54 | } |
| 55 | case LHCALL_FLUSH_TLB: |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 56 | /* FLUSH_TLB comes in two flavors, depending on the |
| 57 | * argument: */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 58 | if (args->arg1) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 59 | guest_pagetable_clear_all(lg); |
| 60 | else |
| 61 | guest_pagetable_flush_user(lg); |
| 62 | break; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 63 | case LHCALL_BIND_DMA: |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 64 | /* BIND_DMA really wants four arguments, but it's the only call |
| 65 | * which does. So the Guest packs the number of buffers and |
| 66 | * the interrupt number into the final argument, and we decode |
| 67 | * it here. This can legitimately fail, since we currently |
| 68 | * place a limit on the number of DMA pools a Guest can have. |
| 69 | * So we return true or false from this call. */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 70 | args->arg0 = bind_dma(lg, args->arg1, args->arg2, |
| 71 | args->arg3 >> 8, args->arg3 & 0xFF); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 72 | break; |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 73 | |
| 74 | /* All these calls simply pass the arguments through to the right |
| 75 | * routines. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 76 | case LHCALL_SEND_DMA: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 77 | send_dma(lg, args->arg1, args->arg2); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 78 | break; |
| 79 | case LHCALL_NEW_PGTABLE: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 80 | guest_new_pagetable(lg, args->arg1); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 81 | break; |
| 82 | case LHCALL_SET_STACK: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 83 | guest_set_stack(lg, args->arg1, args->arg2, args->arg3); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 84 | break; |
| 85 | case LHCALL_SET_PTE: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 86 | guest_set_pte(lg, args->arg1, args->arg2, mkgpte(args->arg3)); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 87 | break; |
| 88 | case LHCALL_SET_PMD: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 89 | guest_set_pmd(lg, args->arg1, args->arg2); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 90 | break; |
| 91 | case LHCALL_SET_CLOCKEVENT: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 92 | guest_set_clockevent(lg, args->arg1); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 93 | break; |
| 94 | case LHCALL_TS: |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 95 | /* This sets the TS flag, as we saw used in run_guest(). */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 96 | lg->ts = args->arg1; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 97 | break; |
| 98 | case LHCALL_HALT: |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 99 | /* Similarly, this sets the halted flag for run_guest(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 100 | lg->halted = 1; |
| 101 | break; |
| 102 | default: |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 103 | if (lguest_arch_do_hcall(lg, args)) |
| 104 | kill_guest(lg, "Bad hypercall %li\n", args->arg0); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 107 | /*:*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 108 | |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 109 | /*H:124 Asynchronous hypercalls are easy: we just look in the array in the |
| 110 | * Guest's "struct lguest_data" to see if any new ones are marked "ready". |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 111 | * |
| 112 | * We are careful to do these in order: obviously we respect the order the |
| 113 | * Guest put them in the ring, but we also promise the Guest that they will |
| 114 | * happen before any normal hypercall (which is why we check this before |
| 115 | * checking for a normal hcall). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 116 | static void do_async_hcalls(struct lguest *lg) |
| 117 | { |
| 118 | unsigned int i; |
| 119 | u8 st[LHCALL_RING_SIZE]; |
| 120 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 121 | /* For simplicity, we copy the entire call status array in at once. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 122 | if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) |
| 123 | return; |
| 124 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 125 | /* We process "struct lguest_data"s hcalls[] ring once. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 126 | for (i = 0; i < ARRAY_SIZE(st); i++) { |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 127 | struct hcall_args args; |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 128 | /* We remember where we were up to from last time. This makes |
| 129 | * sure that the hypercalls are done in the order the Guest |
| 130 | * places them in the ring. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 131 | unsigned int n = lg->next_hcall; |
| 132 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 133 | /* 0xFF means there's no call here (yet). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 134 | if (st[n] == 0xFF) |
| 135 | break; |
| 136 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 137 | /* OK, we have hypercall. Increment the "next_hcall" cursor, |
| 138 | * and wrap back to 0 if we reach the end. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 139 | if (++lg->next_hcall == LHCALL_RING_SIZE) |
| 140 | lg->next_hcall = 0; |
| 141 | |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 142 | /* Copy the hypercall arguments into a local copy of |
| 143 | * the hcall_args struct. */ |
| 144 | if (copy_from_user(&args, &lg->lguest_data->hcalls[n], |
| 145 | sizeof(struct hcall_args))) { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 146 | kill_guest(lg, "Fetching async hypercalls"); |
| 147 | break; |
| 148 | } |
| 149 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 150 | /* Do the hypercall, same as a normal one. */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 151 | do_hcall(lg, &args); |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 152 | |
| 153 | /* Mark the hypercall done. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 154 | if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { |
| 155 | kill_guest(lg, "Writing result for async hypercall"); |
| 156 | break; |
| 157 | } |
| 158 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 159 | /* Stop doing hypercalls if we've just done a DMA to the |
| 160 | * Launcher: it needs to service this first. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 161 | if (lg->dma_is_pending) |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 166 | /* Last of all, we look at what happens first of all. The very first time the |
| 167 | * Guest makes a hypercall, we end up here to set things up: */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 168 | static void initialize(struct lguest *lg) |
| 169 | { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 170 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 171 | /* You can't do anything until you're initialized. The Guest knows the |
| 172 | * rules, so we're unforgiving here. */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 173 | if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) { |
| 174 | kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 178 | if (lguest_arch_init_hypercalls(lg)) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 179 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 180 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 181 | /* The Guest tells us where we're not to deliver interrupts by putting |
| 182 | * the range of addresses into "struct lguest_data". */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 183 | if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) |
| 184 | || get_user(lg->noirq_end, &lg->lguest_data->noirq_end) |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 185 | /* We tell the Guest that it can't use the top 4MB of virtual |
| 186 | * addresses used by the Switcher. */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame^] | 187 | || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem)) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 188 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
| 189 | |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 190 | /* We write the current time into the Guest's data page once now. */ |
| 191 | write_timestamp(lg); |
| 192 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 193 | /* This is the one case where the above accesses might have been the |
| 194 | * first write to a Guest page. This may have caused a copy-on-write |
| 195 | * fault, but the Guest might be referring to the old (read-only) |
| 196 | * page. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 197 | guest_pagetable_clear_all(lg); |
| 198 | } |
| 199 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 200 | /*H:100 |
| 201 | * Hypercalls |
| 202 | * |
| 203 | * Remember from the Guest, hypercalls come in two flavors: normal and |
| 204 | * asynchronous. This file handles both of types. |
| 205 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 206 | void do_hypercalls(struct lguest *lg) |
| 207 | { |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 208 | /* Not initialized yet? This hypercall must do it. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 209 | if (unlikely(!lg->lguest_data)) { |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 210 | /* Set up the "struct lguest_data" */ |
| 211 | initialize(lg); |
| 212 | /* Hcall is done. */ |
| 213 | lg->hcall = NULL; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 217 | /* The Guest has initialized. |
| 218 | * |
| 219 | * Look in the hypercall ring for the async hypercalls: */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 220 | do_async_hcalls(lg); |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 221 | |
| 222 | /* If we stopped reading the hypercall ring because the Guest did a |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 223 | * SEND_DMA to the Launcher, we want to return now. Otherwise we do |
| 224 | * the hypercall. */ |
| 225 | if (!lg->dma_is_pending) { |
| 226 | do_hcall(lg, lg->hcall); |
| 227 | /* Tricky point: we reset the hcall pointer to mark the |
| 228 | * hypercall as "done". We use the hcall pointer rather than |
| 229 | * the trap number to indicate a hypercall is pending. |
| 230 | * Normally it doesn't matter: the Guest will run again and |
| 231 | * update the trap number before we come back here. |
| 232 | * |
| 233 | * However, if we are signalled or the Guest sends DMA to the |
| 234 | * Launcher, the run_guest() loop will exit without running the |
| 235 | * Guest. When it comes back it would try to re-run the |
| 236 | * hypercall. */ |
| 237 | lg->hcall = NULL; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 240 | |
| 241 | /* This routine supplies the Guest with time: it's used for wallclock time at |
| 242 | * initial boot and as a rough time source if the TSC isn't available. */ |
| 243 | void write_timestamp(struct lguest *lg) |
| 244 | { |
| 245 | struct timespec now; |
| 246 | ktime_get_real_ts(&now); |
Jes Sorensen | 891ff65 | 2007-10-22 10:56:22 +1000 | [diff] [blame] | 247 | if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec))) |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 248 | kill_guest(lg, "Writing timestamp"); |
| 249 | } |