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