Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 1 | /*P:500 |
| 2 | * Just as userspace programs request kernel operations through a system |
Rusty Russell | f938d2c | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 3 | * call, the Guest requests Host operations through a "hypercall". You might |
| 4 | * notice this nomenclature doesn't really follow any logic, but the name has |
| 5 | * been around for long enough that we're stuck with it. As you'd expect, this |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 6 | * code is basically a one big switch statement. |
| 7 | :*/ |
Rusty Russell | f938d2c | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 8 | |
| 9 | /* Copyright (C) 2006 Rusty Russell IBM Corporation |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation; either version 2 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | #include <linux/uaccess.h> |
| 26 | #include <linux/syscalls.h> |
| 27 | #include <linux/mm.h> |
Glauber de Oliveira Costa | ca94f2b | 2008-01-18 23:59:07 -0200 | [diff] [blame] | 28 | #include <linux/ktime.h> |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 29 | #include <asm/page.h> |
| 30 | #include <asm/pgtable.h> |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 31 | #include "lg.h" |
| 32 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 33 | /*H:120 |
| 34 | * This is the core hypercall routine: where the Guest gets what it wants. |
| 35 | * Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both. |
| 36 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 37 | static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 38 | { |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 39 | switch (args->arg0) { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 40 | case LHCALL_FLUSH_ASYNC: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 41 | /* |
| 42 | * This call does nothing, except by breaking out of the Guest |
| 43 | * it makes us process all the asynchronous hypercalls. |
| 44 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 45 | break; |
Rusty Russell | a32a8813 | 2009-06-12 22:27:02 -0600 | [diff] [blame] | 46 | case LHCALL_SEND_INTERRUPTS: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 47 | /* |
| 48 | * This call does nothing too, but by breaking out of the Guest |
| 49 | * it makes us process any pending interrupts. |
| 50 | */ |
Rusty Russell | a32a8813 | 2009-06-12 22:27:02 -0600 | [diff] [blame] | 51 | break; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 52 | case LHCALL_LGUEST_INIT: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 53 | /* |
| 54 | * You can't get here unless you're already initialized. Don't |
| 55 | * do that. |
| 56 | */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 57 | kill_guest(cpu, "already have lguest_data"); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 58 | break; |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 59 | case LHCALL_SHUTDOWN: { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 60 | char msg[128]; |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 61 | /* |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 62 | * Shutdown is such a trivial hypercall that we do it in five |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 63 | * lines right here. |
| 64 | * |
| 65 | * If the lgread fails, it will call kill_guest() itself; the |
| 66 | * kill_guest() with the message will be ignored. |
| 67 | */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 68 | __lgread(cpu, msg, args->arg1, sizeof(msg)); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 69 | msg[sizeof(msg)-1] = '\0'; |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 70 | kill_guest(cpu, "CRASH: %s", msg); |
Balaji Rao | ec04b13 | 2007-12-28 14:26:24 +0530 | [diff] [blame] | 71 | if (args->arg2 == LGUEST_SHUTDOWN_RESTART) |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 72 | cpu->lg->dead = ERR_PTR(-ERESTART); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 73 | break; |
| 74 | } |
| 75 | case LHCALL_FLUSH_TLB: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 76 | /* FLUSH_TLB comes in two flavors, depending on the argument: */ |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 77 | if (args->arg1) |
Glauber de Oliveira Costa | 4665ac8 | 2008-01-07 11:05:35 -0200 | [diff] [blame] | 78 | guest_pagetable_clear_all(cpu); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 79 | else |
Glauber de Oliveira Costa | 1713608 | 2008-01-07 11:05:37 -0200 | [diff] [blame] | 80 | guest_pagetable_flush_user(cpu); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 81 | break; |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 82 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 83 | /* |
| 84 | * All these calls simply pass the arguments through to the right |
| 85 | * routines. |
| 86 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 87 | case LHCALL_NEW_PGTABLE: |
Glauber de Oliveira Costa | 4665ac8 | 2008-01-07 11:05:35 -0200 | [diff] [blame] | 88 | guest_new_pagetable(cpu, args->arg1); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 89 | break; |
| 90 | case LHCALL_SET_STACK: |
Glauber de Oliveira Costa | 4665ac8 | 2008-01-07 11:05:35 -0200 | [diff] [blame] | 91 | guest_set_stack(cpu, args->arg1, args->arg2, args->arg3); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 92 | break; |
| 93 | case LHCALL_SET_PTE: |
Matias Zabaljauregui | acdd0b6 | 2009-06-12 22:27:07 -0600 | [diff] [blame] | 94 | #ifdef CONFIG_X86_PAE |
| 95 | guest_set_pte(cpu, args->arg1, args->arg2, |
| 96 | __pte(args->arg3 | (u64)args->arg4 << 32)); |
| 97 | #else |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 98 | guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3)); |
Matias Zabaljauregui | acdd0b6 | 2009-06-12 22:27:07 -0600 | [diff] [blame] | 99 | #endif |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 100 | break; |
Matias Zabaljauregui | ebe0ba8 | 2009-05-30 15:48:08 -0300 | [diff] [blame] | 101 | case LHCALL_SET_PGD: |
| 102 | guest_set_pgd(cpu->lg, args->arg1, args->arg2); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 103 | break; |
Matias Zabaljauregui | acdd0b6 | 2009-06-12 22:27:07 -0600 | [diff] [blame] | 104 | #ifdef CONFIG_X86_PAE |
| 105 | case LHCALL_SET_PMD: |
| 106 | guest_set_pmd(cpu->lg, args->arg1, args->arg2); |
| 107 | break; |
| 108 | #endif |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 109 | case LHCALL_SET_CLOCKEVENT: |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 110 | guest_set_clockevent(cpu, args->arg1); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 111 | break; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 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(). */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 114 | cpu->halted = 1; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 115 | break; |
| 116 | default: |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 117 | /* It should be an architecture-specific hypercall. */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 118 | if (lguest_arch_do_hcall(cpu, args)) |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 119 | kill_guest(cpu, "Bad hypercall %li\n", args->arg0); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 123 | /*H:124 |
| 124 | * Asynchronous hypercalls are easy: we just look in the array in the |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 125 | * 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] | 126 | * |
| 127 | * We are careful to do these in order: obviously we respect the order the |
| 128 | * Guest put them in the ring, but we also promise the Guest that they will |
| 129 | * happen before any normal hypercall (which is why we check this before |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 130 | * checking for a normal hcall). |
| 131 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 132 | static void do_async_hcalls(struct lg_cpu *cpu) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 133 | { |
| 134 | unsigned int i; |
| 135 | u8 st[LHCALL_RING_SIZE]; |
| 136 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 137 | /* For simplicity, we copy the entire call status array in at once. */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 138 | if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st))) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 139 | return; |
| 140 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 141 | /* We process "struct lguest_data"s hcalls[] ring once. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 142 | for (i = 0; i < ARRAY_SIZE(st); i++) { |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 143 | struct hcall_args args; |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 144 | /* |
| 145 | * We remember where we were up to from last time. This makes |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 146 | * sure that the hypercalls are done in the order the Guest |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 147 | * places them in the ring. |
| 148 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 149 | unsigned int n = cpu->next_hcall; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 150 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 151 | /* 0xFF means there's no call here (yet). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 152 | if (st[n] == 0xFF) |
| 153 | break; |
| 154 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 155 | /* |
| 156 | * OK, we have hypercall. Increment the "next_hcall" cursor, |
| 157 | * and wrap back to 0 if we reach the end. |
| 158 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 159 | if (++cpu->next_hcall == LHCALL_RING_SIZE) |
| 160 | cpu->next_hcall = 0; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 161 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 162 | /* |
| 163 | * Copy the hypercall arguments into a local copy of the |
| 164 | * hcall_args struct. |
| 165 | */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 166 | if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n], |
Jes Sorensen | b410e7b | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 167 | sizeof(struct hcall_args))) { |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 168 | kill_guest(cpu, "Fetching async hypercalls"); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 172 | /* Do the hypercall, same as a normal one. */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 173 | do_hcall(cpu, &args); |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 174 | |
| 175 | /* Mark the hypercall done. */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 176 | if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) { |
| 177 | kill_guest(cpu, "Writing result for async hypercall"); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 181 | /* |
| 182 | * Stop doing hypercalls if they want to notify the Launcher: |
| 183 | * it needs to service this first. |
| 184 | */ |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 185 | if (cpu->pending.trap) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 190 | /* |
| 191 | * Last of all, we look at what happens first of all. The very first time the |
| 192 | * Guest makes a hypercall, we end up here to set things up: |
| 193 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 194 | static void initialize(struct lg_cpu *cpu) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 195 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 196 | /* |
| 197 | * You can't do anything until you're initialized. The Guest knows the |
| 198 | * rules, so we're unforgiving here. |
| 199 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 200 | if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) { |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 201 | kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 205 | if (lguest_arch_init_hypercalls(cpu)) |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 206 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 207 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 208 | /* |
| 209 | * The Guest tells us where we're not to deliver interrupts by putting |
Rusty Russell | 2f921b5 | 2015-03-24 11:51:39 +1030 | [diff] [blame] | 210 | * the instruction address into "struct lguest_data". |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 211 | */ |
Rusty Russell | 2f921b5 | 2015-03-24 11:51:39 +1030 | [diff] [blame] | 212 | if (get_user(cpu->lg->noirq_iret, &cpu->lg->lguest_data->noirq_iret)) |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 213 | kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 214 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 215 | /* |
| 216 | * We write the current time into the Guest's data page once so it can |
| 217 | * set its clock. |
| 218 | */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 219 | write_timestamp(cpu); |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 220 | |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 221 | /* page_tables.c will also do some setup. */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 222 | page_table_guest_data_init(cpu); |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 223 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 224 | /* |
| 225 | * This is the one case where the above accesses might have been the |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 226 | * first write to a Guest page. This may have caused a copy-on-write |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 227 | * fault, but the old page might be (read-only) in the Guest |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 228 | * pagetable. |
| 229 | */ |
Glauber de Oliveira Costa | 4665ac8 | 2008-01-07 11:05:35 -0200 | [diff] [blame] | 230 | guest_pagetable_clear_all(cpu); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 231 | } |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 232 | /*:*/ |
| 233 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 234 | /*M:013 |
| 235 | * If a Guest reads from a page (so creates a mapping) that it has never |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 236 | * written to, and then the Launcher writes to it (ie. the output of a virtual |
| 237 | * device), the Guest will still see the old page. In practice, this never |
| 238 | * happens: why would the Guest read a page which it has never written to? But |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 239 | * a similar scenario might one day bite us, so it's worth mentioning. |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 240 | * |
| 241 | * Note that if we used a shared anonymous mapping in the Launcher instead of |
| 242 | * mapping /dev/zero private, we wouldn't worry about cop-on-write. And we |
| 243 | * need that to switch the Launcher to processes (away from threads) anyway. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 244 | :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 245 | |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 246 | /*H:100 |
| 247 | * Hypercalls |
| 248 | * |
| 249 | * Remember from the Guest, hypercalls come in two flavors: normal and |
| 250 | * asynchronous. This file handles both of types. |
| 251 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 252 | void do_hypercalls(struct lg_cpu *cpu) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 253 | { |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 254 | /* Not initialized yet? This hypercall must do it. */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 255 | if (unlikely(!cpu->lg->lguest_data)) { |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 256 | /* Set up the "struct lguest_data" */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 257 | initialize(cpu); |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 258 | /* Hcall is done. */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 259 | cpu->hcall = NULL; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 263 | /* |
| 264 | * The Guest has initialized. |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 265 | * |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 266 | * Look in the hypercall ring for the async hypercalls: |
| 267 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 268 | do_async_hcalls(cpu); |
Rusty Russell | bff672e | 2007-07-26 10:41:04 -0700 | [diff] [blame] | 269 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 270 | /* |
| 271 | * If we stopped reading the hypercall ring because the Guest did a |
Rusty Russell | 1504527 | 2007-10-22 11:24:10 +1000 | [diff] [blame] | 272 | * NOTIFY to the Launcher, we want to return now. Otherwise we do |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 273 | * the hypercall. |
| 274 | */ |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 275 | if (!cpu->pending.trap) { |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 276 | do_hcall(cpu, cpu->hcall); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 277 | /* |
| 278 | * Tricky point: we reset the hcall pointer to mark the |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 279 | * hypercall as "done". We use the hcall pointer rather than |
| 280 | * the trap number to indicate a hypercall is pending. |
| 281 | * Normally it doesn't matter: the Guest will run again and |
| 282 | * update the trap number before we come back here. |
| 283 | * |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 284 | * However, if we are signalled or the Guest sends I/O to the |
Rusty Russell | cc6d4fb | 2007-10-22 11:03:30 +1000 | [diff] [blame] | 285 | * Launcher, the run_guest() loop will exit without running the |
| 286 | * Guest. When it comes back it would try to re-run the |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 287 | * hypercall. Finding that bug sucked. |
| 288 | */ |
Glauber de Oliveira Costa | 73044f0 | 2008-01-07 11:05:27 -0200 | [diff] [blame] | 289 | cpu->hcall = NULL; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 292 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 293 | /* |
| 294 | * This routine supplies the Guest with time: it's used for wallclock time at |
| 295 | * initial boot and as a rough time source if the TSC isn't available. |
| 296 | */ |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 297 | void write_timestamp(struct lg_cpu *cpu) |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 298 | { |
| 299 | struct timespec now; |
| 300 | ktime_get_real_ts(&now); |
Glauber de Oliveira Costa | 382ac6b | 2008-01-17 19:19:42 -0200 | [diff] [blame] | 301 | if (copy_to_user(&cpu->lg->lguest_data->time, |
| 302 | &now, sizeof(struct timespec))) |
| 303 | kill_guest(cpu, "Writing timestamp"); |
Rusty Russell | 6c8dca5 | 2007-07-27 13:42:52 +1000 | [diff] [blame] | 304 | } |