Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 3 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kvm_host.h> |
| 20 | #include <asm/kvm_mmio.h> |
| 21 | #include <asm/kvm_emulate.h> |
| 22 | #include <trace/events/kvm.h> |
| 23 | |
| 24 | #include "trace.h" |
| 25 | |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 26 | void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data) |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 27 | { |
| 28 | void *datap = NULL; |
| 29 | union { |
| 30 | u8 byte; |
| 31 | u16 hword; |
| 32 | u32 word; |
| 33 | u64 dword; |
| 34 | } tmp; |
| 35 | |
| 36 | switch (len) { |
| 37 | case 1: |
| 38 | tmp.byte = data; |
| 39 | datap = &tmp.byte; |
| 40 | break; |
| 41 | case 2: |
| 42 | tmp.hword = data; |
| 43 | datap = &tmp.hword; |
| 44 | break; |
| 45 | case 4: |
| 46 | tmp.word = data; |
| 47 | datap = &tmp.word; |
| 48 | break; |
| 49 | case 8: |
| 50 | tmp.dword = data; |
| 51 | datap = &tmp.dword; |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | memcpy(buf, datap, len); |
| 56 | } |
| 57 | |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 58 | unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len) |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned long data = 0; |
| 61 | union { |
| 62 | u16 hword; |
| 63 | u32 word; |
| 64 | u64 dword; |
| 65 | } tmp; |
| 66 | |
| 67 | switch (len) { |
| 68 | case 1: |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 69 | data = *(u8 *)buf; |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 70 | break; |
| 71 | case 2: |
| 72 | memcpy(&tmp.hword, buf, len); |
| 73 | data = tmp.hword; |
| 74 | break; |
| 75 | case 4: |
| 76 | memcpy(&tmp.word, buf, len); |
| 77 | data = tmp.word; |
| 78 | break; |
| 79 | case 8: |
| 80 | memcpy(&tmp.dword, buf, len); |
| 81 | data = tmp.dword; |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | return data; |
| 86 | } |
| 87 | |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 88 | /** |
| 89 | * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation |
Christoffer Dall | 83091db | 2016-03-29 14:29:28 +0200 | [diff] [blame] | 90 | * or in-kernel IO emulation |
| 91 | * |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 92 | * @vcpu: The VCPU pointer |
| 93 | * @run: The VCPU run struct containing the mmio data |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 94 | */ |
| 95 | int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 96 | { |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 97 | unsigned long data; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 98 | unsigned int len; |
| 99 | int mask; |
| 100 | |
| 101 | if (!run->mmio.is_write) { |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 102 | len = run->mmio.len; |
Marc Zyngier | f42798c | 2013-03-05 02:43:23 +0000 | [diff] [blame] | 103 | if (len > sizeof(unsigned long)) |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 106 | data = kvm_mmio_read_buf(run->mmio.data, len); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 107 | |
Marc Zyngier | f42798c | 2013-03-05 02:43:23 +0000 | [diff] [blame] | 108 | if (vcpu->arch.mmio_decode.sign_extend && |
| 109 | len < sizeof(unsigned long)) { |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 110 | mask = 1U << ((len * 8) - 1); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 111 | data = (data ^ mask) - mask; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 112 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 113 | |
| 114 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr, |
| 115 | data); |
| 116 | data = vcpu_data_host_to_guest(vcpu, data, len); |
Pavel Fedin | bc45a51 | 2015-12-04 15:03:11 +0300 | [diff] [blame] | 117 | vcpu_set_reg(vcpu, vcpu->arch.mmio_decode.rt, data); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 123 | static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len) |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 124 | { |
Christoffer Dall | 2184a60 | 2013-07-29 20:46:04 -0700 | [diff] [blame] | 125 | unsigned long rt; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 126 | int access_size; |
| 127 | bool sign_extend; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 128 | |
Marc Zyngier | b37670b | 2012-09-18 11:37:28 +0100 | [diff] [blame] | 129 | if (kvm_vcpu_dabt_iss1tw(vcpu)) { |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 130 | /* page table accesses IO mem: tell guest to fix its TTBR */ |
Marc Zyngier | 7393b59 | 2012-09-17 19:27:09 +0100 | [diff] [blame] | 131 | kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu)); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 135 | access_size = kvm_vcpu_dabt_get_as(vcpu); |
| 136 | if (unlikely(access_size < 0)) |
| 137 | return access_size; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 138 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 139 | *is_write = kvm_vcpu_dabt_iswrite(vcpu); |
Marc Zyngier | 7c511b8 | 2012-09-18 11:23:02 +0100 | [diff] [blame] | 140 | sign_extend = kvm_vcpu_dabt_issext(vcpu); |
Marc Zyngier | d0adf74 | 2012-09-18 11:28:57 +0100 | [diff] [blame] | 141 | rt = kvm_vcpu_dabt_get_rd(vcpu); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 142 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 143 | *len = access_size; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 144 | vcpu->arch.mmio_decode.sign_extend = sign_extend; |
| 145 | vcpu->arch.mmio_decode.rt = rt; |
| 146 | |
| 147 | /* |
| 148 | * The MMIO instruction is emulated and should not be re-executed |
| 149 | * in the guest. |
| 150 | */ |
Marc Zyngier | 23b415d | 2012-09-18 12:07:06 +0100 | [diff] [blame] | 151 | kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, |
| 156 | phys_addr_t fault_ipa) |
| 157 | { |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 158 | unsigned long data; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 159 | unsigned long rt; |
| 160 | int ret; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 161 | bool is_write; |
| 162 | int len; |
| 163 | u8 data_buf[8]; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 164 | |
| 165 | /* |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 166 | * Prepare MMIO operation. First decode the syndrome data we get |
| 167 | * from the CPU. Then try if some in-kernel emulation feels |
| 168 | * responsible, otherwise let user space do its magic. |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 169 | */ |
Marc Zyngier | 4a1df28 | 2012-09-18 11:06:23 +0100 | [diff] [blame] | 170 | if (kvm_vcpu_dabt_isvalid(vcpu)) { |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 171 | ret = decode_hsr(vcpu, &is_write, &len); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 172 | if (ret) |
| 173 | return ret; |
| 174 | } else { |
| 175 | kvm_err("load/store instruction decoding not implemented\n"); |
| 176 | return -ENOSYS; |
| 177 | } |
| 178 | |
| 179 | rt = vcpu->arch.mmio_decode.rt; |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 180 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 181 | if (is_write) { |
Pavel Fedin | bc45a51 | 2015-12-04 15:03:11 +0300 | [diff] [blame] | 182 | data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt), |
| 183 | len); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 184 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 185 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, data); |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 186 | kvm_mmio_write_buf(data_buf, len, data); |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 187 | |
| 188 | ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len, |
| 189 | data_buf); |
Andre Przywara | 5100f983 | 2014-11-06 12:11:45 +0000 | [diff] [blame] | 190 | } else { |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 191 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len, |
Andre Przywara | 5100f983 | 2014-11-06 12:11:45 +0000 | [diff] [blame] | 192 | fault_ipa, 0); |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 193 | |
| 194 | ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len, |
| 195 | data_buf); |
Andre Przywara | 5100f983 | 2014-11-06 12:11:45 +0000 | [diff] [blame] | 196 | } |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 197 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 198 | /* Now prepare kvm_run for the potential return to userland. */ |
| 199 | run->mmio.is_write = is_write; |
| 200 | run->mmio.phys_addr = fault_ipa; |
| 201 | run->mmio.len = len; |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 202 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 203 | if (!ret) { |
| 204 | /* We handled the access successfully in the kernel. */ |
Christoffer Dall | 83091db | 2016-03-29 14:29:28 +0200 | [diff] [blame] | 205 | if (!is_write) |
| 206 | memcpy(run->mmio.data, data_buf, len); |
Amit Tomar | b19e689 | 2015-11-26 10:09:43 +0000 | [diff] [blame] | 207 | vcpu->stat.mmio_exit_kernel++; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 208 | kvm_handle_mmio_return(vcpu, run); |
| 209 | return 1; |
| 210 | } |
| 211 | |
Christoffer Dall | 83091db | 2016-03-29 14:29:28 +0200 | [diff] [blame] | 212 | if (is_write) |
| 213 | memcpy(run->mmio.data, data_buf, len); |
| 214 | vcpu->stat.mmio_exit_user++; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 215 | run->exit_reason = KVM_EXIT_MMIO; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 216 | return 0; |
| 217 | } |