Thomas Gleixner | d94d71c | 2019-05-29 07:12:40 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 4 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/kvm_host.h> |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 8 | #include <asm/kvm_emulate.h> |
| 9 | #include <trace/events/kvm.h> |
| 10 | |
| 11 | #include "trace.h" |
| 12 | |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 13 | 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] | 14 | { |
| 15 | void *datap = NULL; |
| 16 | union { |
| 17 | u8 byte; |
| 18 | u16 hword; |
| 19 | u32 word; |
| 20 | u64 dword; |
| 21 | } tmp; |
| 22 | |
| 23 | switch (len) { |
| 24 | case 1: |
| 25 | tmp.byte = data; |
| 26 | datap = &tmp.byte; |
| 27 | break; |
| 28 | case 2: |
| 29 | tmp.hword = data; |
| 30 | datap = &tmp.hword; |
| 31 | break; |
| 32 | case 4: |
| 33 | tmp.word = data; |
| 34 | datap = &tmp.word; |
| 35 | break; |
| 36 | case 8: |
| 37 | tmp.dword = data; |
| 38 | datap = &tmp.dword; |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | memcpy(buf, datap, len); |
| 43 | } |
| 44 | |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 45 | unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len) |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 46 | { |
| 47 | unsigned long data = 0; |
| 48 | union { |
| 49 | u16 hword; |
| 50 | u32 word; |
| 51 | u64 dword; |
| 52 | } tmp; |
| 53 | |
| 54 | switch (len) { |
| 55 | case 1: |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 56 | data = *(u8 *)buf; |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 57 | break; |
| 58 | case 2: |
| 59 | memcpy(&tmp.hword, buf, len); |
| 60 | data = tmp.hword; |
| 61 | break; |
| 62 | case 4: |
| 63 | memcpy(&tmp.word, buf, len); |
| 64 | data = tmp.word; |
| 65 | break; |
| 66 | case 8: |
| 67 | memcpy(&tmp.dword, buf, len); |
| 68 | data = tmp.dword; |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | return data; |
| 73 | } |
| 74 | |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 75 | /** |
| 76 | * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation |
Christoffer Dall | 83091db | 2016-03-29 14:29:28 +0200 | [diff] [blame] | 77 | * or in-kernel IO emulation |
| 78 | * |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 79 | * @vcpu: The VCPU pointer |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 80 | */ |
Tianjia Zhang | 74cc7e0 | 2020-06-23 21:14:15 +0800 | [diff] [blame] | 81 | int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 82 | { |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 83 | unsigned long data; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 84 | unsigned int len; |
| 85 | int mask; |
| 86 | |
Andrew Jones | 2113c5f | 2019-08-22 13:03:05 +0200 | [diff] [blame] | 87 | /* Detect an already handled MMIO return */ |
| 88 | if (unlikely(!vcpu->mmio_needed)) |
| 89 | return 0; |
| 90 | |
| 91 | vcpu->mmio_needed = 0; |
| 92 | |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 93 | if (!kvm_vcpu_dabt_iswrite(vcpu)) { |
Tianjia Zhang | 74cc7e0 | 2020-06-23 21:14:15 +0800 | [diff] [blame] | 94 | struct kvm_run *run = vcpu->run; |
| 95 | |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 96 | len = kvm_vcpu_dabt_get_as(vcpu); |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 97 | data = kvm_mmio_read_buf(run->mmio.data, len); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 98 | |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 99 | if (kvm_vcpu_dabt_issext(vcpu) && |
Marc Zyngier | f42798c | 2013-03-05 02:43:23 +0000 | [diff] [blame] | 100 | len < sizeof(unsigned long)) { |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 101 | mask = 1U << ((len * 8) - 1); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 102 | data = (data ^ mask) - mask; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 103 | } |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 104 | |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 105 | if (!kvm_vcpu_dabt_issf(vcpu)) |
Christoffer Dall | b6ae256 | 2019-12-12 20:50:55 +0100 | [diff] [blame] | 106 | data = data & 0xffffffff; |
| 107 | |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 108 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr, |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 109 | &data); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 110 | data = vcpu_data_host_to_guest(vcpu, data, len); |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 111 | vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Mark Rutland | 0d64073 | 2018-11-09 15:07:10 +0000 | [diff] [blame] | 114 | /* |
| 115 | * The MMIO instruction is emulated and should not be re-executed |
| 116 | * in the guest. |
| 117 | */ |
| 118 | kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); |
| 119 | |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Tianjia Zhang | 74cc7e0 | 2020-06-23 21:14:15 +0800 | [diff] [blame] | 123 | int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 124 | { |
Tianjia Zhang | 74cc7e0 | 2020-06-23 21:14:15 +0800 | [diff] [blame] | 125 | struct kvm_run *run = vcpu->run; |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 126 | unsigned long data; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 127 | unsigned long rt; |
| 128 | int ret; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 129 | bool is_write; |
| 130 | int len; |
| 131 | u8 data_buf[8]; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 132 | |
| 133 | /* |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 134 | * No valid syndrome? Ask userspace for help if it has |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 135 | * volunteered to do so, and bail out otherwise. |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 136 | */ |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 137 | if (!kvm_vcpu_dabt_isvalid(vcpu)) { |
Christoffer Dall | c726200 | 2019-10-11 13:07:05 +0200 | [diff] [blame] | 138 | if (vcpu->kvm->arch.return_nisv_io_abort_to_user) { |
| 139 | run->exit_reason = KVM_EXIT_ARM_NISV; |
| 140 | run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu); |
| 141 | run->arm_nisv.fault_ipa = fault_ipa; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | kvm_pr_unimpl("Data abort outside memslots with no valid syndrome info\n"); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 146 | return -ENOSYS; |
| 147 | } |
| 148 | |
Marc Zyngier | 0e20f5e | 2019-12-13 13:25:25 +0000 | [diff] [blame] | 149 | /* |
| 150 | * Prepare MMIO operation. First decode the syndrome data we get |
| 151 | * from the CPU. Then try if some in-kernel emulation feels |
| 152 | * responsible, otherwise let user space do its magic. |
| 153 | */ |
| 154 | is_write = kvm_vcpu_dabt_iswrite(vcpu); |
| 155 | len = kvm_vcpu_dabt_get_as(vcpu); |
| 156 | rt = kvm_vcpu_dabt_get_rd(vcpu); |
Marc Zyngier | 6d89d2d | 2013-02-12 12:40:22 +0000 | [diff] [blame] | 157 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 158 | if (is_write) { |
Pavel Fedin | bc45a51 | 2015-12-04 15:03:11 +0300 | [diff] [blame] | 159 | data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt), |
| 160 | len); |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 161 | |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 162 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data); |
Christoffer Dall | d5a5a0e | 2016-04-24 21:41:36 +0200 | [diff] [blame] | 163 | kvm_mmio_write_buf(data_buf, len, data); |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 164 | |
| 165 | ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len, |
| 166 | data_buf); |
Andre Przywara | 5100f983 | 2014-11-06 12:11:45 +0000 | [diff] [blame] | 167 | } else { |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 168 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len, |
Wanpeng Li | e39d200f | 2017-12-14 17:40:50 -0800 | [diff] [blame] | 169 | fault_ipa, NULL); |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 170 | |
| 171 | ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len, |
| 172 | data_buf); |
Andre Przywara | 5100f983 | 2014-11-06 12:11:45 +0000 | [diff] [blame] | 173 | } |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 174 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 175 | /* Now prepare kvm_run for the potential return to userland. */ |
| 176 | run->mmio.is_write = is_write; |
| 177 | run->mmio.phys_addr = fault_ipa; |
| 178 | run->mmio.len = len; |
Andrew Jones | 2113c5f | 2019-08-22 13:03:05 +0200 | [diff] [blame] | 179 | vcpu->mmio_needed = 1; |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 180 | |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 181 | if (!ret) { |
| 182 | /* We handled the access successfully in the kernel. */ |
Christoffer Dall | 83091db | 2016-03-29 14:29:28 +0200 | [diff] [blame] | 183 | if (!is_write) |
| 184 | memcpy(run->mmio.data, data_buf, len); |
Amit Tomar | b19e689 | 2015-11-26 10:09:43 +0000 | [diff] [blame] | 185 | vcpu->stat.mmio_exit_kernel++; |
Tianjia Zhang | 74cc7e0 | 2020-06-23 21:14:15 +0800 | [diff] [blame] | 186 | kvm_handle_mmio_return(vcpu); |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 187 | return 1; |
| 188 | } |
| 189 | |
Christoffer Dall | 83091db | 2016-03-29 14:29:28 +0200 | [diff] [blame] | 190 | if (is_write) |
| 191 | memcpy(run->mmio.data, data_buf, len); |
| 192 | vcpu->stat.mmio_exit_user++; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 193 | run->exit_reason = KVM_EXIT_MMIO; |
Christoffer Dall | 45e96ea | 2013-01-20 18:43:58 -0500 | [diff] [blame] | 194 | return 0; |
| 195 | } |