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 | |
| 31 | static void do_hcall(struct lguest *lg, struct lguest_regs *regs) |
| 32 | { |
| 33 | switch (regs->eax) { |
| 34 | case LHCALL_FLUSH_ASYNC: |
| 35 | break; |
| 36 | case LHCALL_LGUEST_INIT: |
| 37 | kill_guest(lg, "already have lguest_data"); |
| 38 | break; |
| 39 | case LHCALL_CRASH: { |
| 40 | char msg[128]; |
| 41 | lgread(lg, msg, regs->edx, sizeof(msg)); |
| 42 | msg[sizeof(msg)-1] = '\0'; |
| 43 | kill_guest(lg, "CRASH: %s", msg); |
| 44 | break; |
| 45 | } |
| 46 | case LHCALL_FLUSH_TLB: |
| 47 | if (regs->edx) |
| 48 | guest_pagetable_clear_all(lg); |
| 49 | else |
| 50 | guest_pagetable_flush_user(lg); |
| 51 | break; |
| 52 | case LHCALL_GET_WALLCLOCK: { |
| 53 | struct timespec ts; |
| 54 | ktime_get_real_ts(&ts); |
| 55 | regs->eax = ts.tv_sec; |
| 56 | break; |
| 57 | } |
| 58 | case LHCALL_BIND_DMA: |
| 59 | regs->eax = bind_dma(lg, regs->edx, regs->ebx, |
| 60 | regs->ecx >> 8, regs->ecx & 0xFF); |
| 61 | break; |
| 62 | case LHCALL_SEND_DMA: |
| 63 | send_dma(lg, regs->edx, regs->ebx); |
| 64 | break; |
| 65 | case LHCALL_LOAD_GDT: |
| 66 | load_guest_gdt(lg, regs->edx, regs->ebx); |
| 67 | break; |
| 68 | case LHCALL_LOAD_IDT_ENTRY: |
| 69 | load_guest_idt_entry(lg, regs->edx, regs->ebx, regs->ecx); |
| 70 | break; |
| 71 | case LHCALL_NEW_PGTABLE: |
| 72 | guest_new_pagetable(lg, regs->edx); |
| 73 | break; |
| 74 | case LHCALL_SET_STACK: |
| 75 | guest_set_stack(lg, regs->edx, regs->ebx, regs->ecx); |
| 76 | break; |
| 77 | case LHCALL_SET_PTE: |
| 78 | guest_set_pte(lg, regs->edx, regs->ebx, mkgpte(regs->ecx)); |
| 79 | break; |
| 80 | case LHCALL_SET_PMD: |
| 81 | guest_set_pmd(lg, regs->edx, regs->ebx); |
| 82 | break; |
| 83 | case LHCALL_LOAD_TLS: |
| 84 | guest_load_tls(lg, regs->edx); |
| 85 | break; |
| 86 | case LHCALL_SET_CLOCKEVENT: |
| 87 | guest_set_clockevent(lg, regs->edx); |
| 88 | break; |
| 89 | case LHCALL_TS: |
| 90 | lg->ts = regs->edx; |
| 91 | break; |
| 92 | case LHCALL_HALT: |
| 93 | lg->halted = 1; |
| 94 | break; |
| 95 | default: |
| 96 | kill_guest(lg, "Bad hypercall %li\n", regs->eax); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* We always do queued calls before actual hypercall. */ |
| 101 | static void do_async_hcalls(struct lguest *lg) |
| 102 | { |
| 103 | unsigned int i; |
| 104 | u8 st[LHCALL_RING_SIZE]; |
| 105 | |
| 106 | if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) |
| 107 | return; |
| 108 | |
| 109 | for (i = 0; i < ARRAY_SIZE(st); i++) { |
| 110 | struct lguest_regs regs; |
| 111 | unsigned int n = lg->next_hcall; |
| 112 | |
| 113 | if (st[n] == 0xFF) |
| 114 | break; |
| 115 | |
| 116 | if (++lg->next_hcall == LHCALL_RING_SIZE) |
| 117 | lg->next_hcall = 0; |
| 118 | |
| 119 | if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax) |
| 120 | || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx) |
| 121 | || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx) |
| 122 | || get_user(regs.ebx, &lg->lguest_data->hcalls[n].ebx)) { |
| 123 | kill_guest(lg, "Fetching async hypercalls"); |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | do_hcall(lg, ®s); |
| 128 | if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { |
| 129 | kill_guest(lg, "Writing result for async hypercall"); |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (lg->dma_is_pending) |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void initialize(struct lguest *lg) |
| 139 | { |
| 140 | u32 tsc_speed; |
| 141 | |
| 142 | if (lg->regs->eax != LHCALL_LGUEST_INIT) { |
| 143 | kill_guest(lg, "hypercall %li before LGUEST_INIT", |
| 144 | lg->regs->eax); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | /* We only tell the guest to use the TSC if it's reliable. */ |
| 149 | if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable()) |
| 150 | tsc_speed = tsc_khz; |
| 151 | else |
| 152 | tsc_speed = 0; |
| 153 | |
| 154 | lg->lguest_data = (struct lguest_data __user *)lg->regs->edx; |
| 155 | /* We check here so we can simply copy_to_user/from_user */ |
| 156 | if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) { |
| 157 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
| 158 | return; |
| 159 | } |
| 160 | if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) |
| 161 | || get_user(lg->noirq_end, &lg->lguest_data->noirq_end) |
| 162 | /* We reserve the top pgd entry. */ |
| 163 | || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem) |
| 164 | || put_user(tsc_speed, &lg->lguest_data->tsc_khz) |
| 165 | || put_user(lg->guestid, &lg->lguest_data->guestid)) |
| 166 | kill_guest(lg, "bad guest page %p", lg->lguest_data); |
| 167 | |
| 168 | /* This is the one case where the above accesses might have |
| 169 | * been the first write to a Guest page. This may have caused |
| 170 | * a copy-on-write fault, but the Guest might be referring to |
| 171 | * the old (read-only) page. */ |
| 172 | guest_pagetable_clear_all(lg); |
| 173 | } |
| 174 | |
| 175 | /* Even if we go out to userspace and come back, we don't want to do |
| 176 | * the hypercall again. */ |
| 177 | static void clear_hcall(struct lguest *lg) |
| 178 | { |
| 179 | lg->regs->trapnum = 255; |
| 180 | } |
| 181 | |
| 182 | void do_hypercalls(struct lguest *lg) |
| 183 | { |
| 184 | if (unlikely(!lg->lguest_data)) { |
| 185 | if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) { |
| 186 | initialize(lg); |
| 187 | clear_hcall(lg); |
| 188 | } |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | do_async_hcalls(lg); |
| 193 | if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) { |
| 194 | do_hcall(lg, lg->regs); |
| 195 | clear_hcall(lg); |
| 196 | } |
| 197 | } |