1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
7 #include <linux/kvm_host.h>
8 #include <asm/kvm_mmio.h>
9 #include <asm/kvm_emulate.h>
10 #include <trace/events/kvm.h>
14 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data)
43 memcpy(buf, datap, len);
46 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len)
48 unsigned long data = 0;
60 memcpy(&tmp.hword, buf, len);
64 memcpy(&tmp.word, buf, len);
68 memcpy(&tmp.dword, buf, len);
77 * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
78 * or in-kernel IO emulation
80 * @vcpu: The VCPU pointer
81 * @run: The VCPU run struct containing the mmio data
83 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
89 /* Detect an already handled MMIO return */
90 if (unlikely(!vcpu->mmio_needed))
93 vcpu->mmio_needed = 0;
95 if (!run->mmio.is_write) {
97 if (len > sizeof(unsigned long))
100 data = kvm_mmio_read_buf(run->mmio.data, len);
102 if (vcpu->arch.mmio_decode.sign_extend &&
103 len < sizeof(unsigned long)) {
104 mask = 1U << ((len * 8) - 1);
105 data = (data ^ mask) - mask;
108 trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
110 data = vcpu_data_host_to_guest(vcpu, data, len);
111 vcpu_set_reg(vcpu, vcpu->arch.mmio_decode.rt, data);
115 * The MMIO instruction is emulated and should not be re-executed
118 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
123 static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)
129 if (kvm_vcpu_dabt_iss1tw(vcpu)) {
130 /* page table accesses IO mem: tell guest to fix its TTBR */
131 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
135 access_size = kvm_vcpu_dabt_get_as(vcpu);
136 if (unlikely(access_size < 0))
139 *is_write = kvm_vcpu_dabt_iswrite(vcpu);
140 sign_extend = kvm_vcpu_dabt_issext(vcpu);
141 rt = kvm_vcpu_dabt_get_rd(vcpu);
144 vcpu->arch.mmio_decode.sign_extend = sign_extend;
145 vcpu->arch.mmio_decode.rt = rt;
150 int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
151 phys_addr_t fault_ipa)
161 * Prepare MMIO operation. First decode the syndrome data we get
162 * from the CPU. Then try if some in-kernel emulation feels
163 * responsible, otherwise let user space do its magic.
165 if (kvm_vcpu_dabt_isvalid(vcpu)) {
166 ret = decode_hsr(vcpu, &is_write, &len);
170 if (vcpu->kvm->arch.return_nisv_io_abort_to_user) {
171 run->exit_reason = KVM_EXIT_ARM_NISV;
172 run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu);
173 run->arm_nisv.fault_ipa = fault_ipa;
177 kvm_pr_unimpl("Data abort outside memslots with no valid syndrome info\n");
181 rt = vcpu->arch.mmio_decode.rt;
184 data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
187 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
188 kvm_mmio_write_buf(data_buf, len, data);
190 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
193 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
196 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
200 /* Now prepare kvm_run for the potential return to userland. */
201 run->mmio.is_write = is_write;
202 run->mmio.phys_addr = fault_ipa;
204 vcpu->mmio_needed = 1;
207 /* We handled the access successfully in the kernel. */
209 memcpy(run->mmio.data, data_buf, len);
210 vcpu->stat.mmio_exit_kernel++;
211 kvm_handle_mmio_return(vcpu, run);
216 memcpy(run->mmio.data, data_buf, len);
217 vcpu->stat.mmio_exit_user++;
218 run->exit_reason = KVM_EXIT_MMIO;