1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University 4 * Author: Christoffer Dall <c.dall@virtualopensystems.com> 5 */ 6 7 #include <linux/kvm_host.h> 8 #include <asm/kvm_emulate.h> 9 #include <trace/events/kvm.h> 10 11 #include "trace.h" 12 13 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data) 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 45 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len) 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: 56 data = *(u8 *)buf; 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 75 static bool kvm_pending_sync_exception(struct kvm_vcpu *vcpu) 76 { 77 if (!vcpu_get_flag(vcpu, PENDING_EXCEPTION)) 78 return false; 79 80 if (vcpu_el1_is_32bit(vcpu)) { 81 switch (vcpu_get_flag(vcpu, EXCEPT_MASK)) { 82 case unpack_vcpu_flag(EXCEPT_AA32_UND): 83 case unpack_vcpu_flag(EXCEPT_AA32_IABT): 84 case unpack_vcpu_flag(EXCEPT_AA32_DABT): 85 return true; 86 default: 87 return false; 88 } 89 } else { 90 switch (vcpu_get_flag(vcpu, EXCEPT_MASK)) { 91 case unpack_vcpu_flag(EXCEPT_AA64_EL1_SYNC): 92 case unpack_vcpu_flag(EXCEPT_AA64_EL2_SYNC): 93 return true; 94 default: 95 return false; 96 } 97 } 98 } 99 100 /** 101 * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation 102 * or in-kernel IO emulation 103 * 104 * @vcpu: The VCPU pointer 105 */ 106 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) 107 { 108 unsigned long data; 109 unsigned int len; 110 int mask; 111 112 /* 113 * Detect if the MMIO return was already handled or if userspace aborted 114 * the MMIO access. 115 */ 116 if (unlikely(!vcpu->mmio_needed || kvm_pending_sync_exception(vcpu))) 117 return 1; 118 119 vcpu->mmio_needed = 0; 120 121 if (!kvm_vcpu_dabt_iswrite(vcpu)) { 122 struct kvm_run *run = vcpu->run; 123 124 len = kvm_vcpu_dabt_get_as(vcpu); 125 data = kvm_mmio_read_buf(run->mmio.data, len); 126 127 if (kvm_vcpu_dabt_issext(vcpu) && 128 len < sizeof(unsigned long)) { 129 mask = 1U << ((len * 8) - 1); 130 data = (data ^ mask) - mask; 131 } 132 133 if (!kvm_vcpu_dabt_issf(vcpu)) 134 data = data & 0xffffffff; 135 136 trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr, 137 &data); 138 data = vcpu_data_host_to_guest(vcpu, data, len); 139 vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data); 140 } 141 142 /* 143 * The MMIO instruction is emulated and should not be re-executed 144 * in the guest. 145 */ 146 kvm_incr_pc(vcpu); 147 148 return 1; 149 } 150 151 int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) 152 { 153 struct kvm_run *run = vcpu->run; 154 unsigned long data; 155 unsigned long rt; 156 int ret; 157 bool is_write; 158 int len; 159 u8 data_buf[8]; 160 161 /* 162 * No valid syndrome? Ask userspace for help if it has 163 * volunteered to do so, and bail out otherwise. 164 * 165 * In the protected VM case, there isn't much userspace can do 166 * though, so directly deliver an exception to the guest. 167 */ 168 if (!kvm_vcpu_dabt_isvalid(vcpu)) { 169 trace_kvm_mmio_nisv(*vcpu_pc(vcpu), kvm_vcpu_get_esr(vcpu), 170 kvm_vcpu_get_hfar(vcpu), fault_ipa); 171 172 if (vcpu_is_protected(vcpu)) { 173 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu)); 174 return 1; 175 } 176 177 if (test_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, 178 &vcpu->kvm->arch.flags)) { 179 run->exit_reason = KVM_EXIT_ARM_NISV; 180 run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu); 181 run->arm_nisv.fault_ipa = fault_ipa; 182 return 0; 183 } 184 185 return -ENOSYS; 186 } 187 188 /* 189 * Prepare MMIO operation. First decode the syndrome data we get 190 * from the CPU. Then try if some in-kernel emulation feels 191 * responsible, otherwise let user space do its magic. 192 */ 193 is_write = kvm_vcpu_dabt_iswrite(vcpu); 194 len = kvm_vcpu_dabt_get_as(vcpu); 195 rt = kvm_vcpu_dabt_get_rd(vcpu); 196 197 if (is_write) { 198 data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt), 199 len); 200 201 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data); 202 kvm_mmio_write_buf(data_buf, len, data); 203 204 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len, 205 data_buf); 206 } else { 207 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len, 208 fault_ipa, NULL); 209 210 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len, 211 data_buf); 212 } 213 214 /* Now prepare kvm_run for the potential return to userland. */ 215 run->mmio.is_write = is_write; 216 run->mmio.phys_addr = fault_ipa; 217 run->mmio.len = len; 218 vcpu->mmio_needed = 1; 219 220 if (!ret) { 221 /* We handled the access successfully in the kernel. */ 222 if (!is_write) 223 memcpy(run->mmio.data, data_buf, len); 224 vcpu->stat.mmio_exit_kernel++; 225 kvm_handle_mmio_return(vcpu); 226 return 1; 227 } 228 229 if (is_write) 230 memcpy(run->mmio.data, data_buf, len); 231 vcpu->stat.mmio_exit_user++; 232 run->exit_reason = KVM_EXIT_MMIO; 233 return 0; 234 } 235