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 if (!run->mmio.is_write) {
91 if (len > sizeof(unsigned long))
94 data = kvm_mmio_read_buf(run->mmio.data, len);
96 if (vcpu->arch.mmio_decode.sign_extend &&
97 len < sizeof(unsigned long)) {
98 mask = 1U << ((len * 8) - 1);
99 data = (data ^ mask) - mask;
102 trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
104 data = vcpu_data_host_to_guest(vcpu, data, len);
105 vcpu_set_reg(vcpu, vcpu->arch.mmio_decode.rt, data);
109 * The MMIO instruction is emulated and should not be re-executed
112 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
117 static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)
123 if (kvm_vcpu_dabt_iss1tw(vcpu)) {
124 /* page table accesses IO mem: tell guest to fix its TTBR */
125 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
129 access_size = kvm_vcpu_dabt_get_as(vcpu);
130 if (unlikely(access_size < 0))
133 *is_write = kvm_vcpu_dabt_iswrite(vcpu);
134 sign_extend = kvm_vcpu_dabt_issext(vcpu);
135 rt = kvm_vcpu_dabt_get_rd(vcpu);
138 vcpu->arch.mmio_decode.sign_extend = sign_extend;
139 vcpu->arch.mmio_decode.rt = rt;
144 int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
145 phys_addr_t fault_ipa)
155 * Prepare MMIO operation. First decode the syndrome data we get
156 * from the CPU. Then try if some in-kernel emulation feels
157 * responsible, otherwise let user space do its magic.
159 if (kvm_vcpu_dabt_isvalid(vcpu)) {
160 ret = decode_hsr(vcpu, &is_write, &len);
164 kvm_err("load/store instruction decoding not implemented\n");
168 rt = vcpu->arch.mmio_decode.rt;
171 data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
174 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
175 kvm_mmio_write_buf(data_buf, len, data);
177 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
180 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
183 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
187 /* Now prepare kvm_run for the potential return to userland. */
188 run->mmio.is_write = is_write;
189 run->mmio.phys_addr = fault_ipa;
193 /* We handled the access successfully in the kernel. */
195 memcpy(run->mmio.data, data_buf, len);
196 vcpu->stat.mmio_exit_kernel++;
197 kvm_handle_mmio_return(vcpu, run);
202 memcpy(run->mmio.data, data_buf, len);
203 vcpu->stat.mmio_exit_user++;
204 run->exit_reason = KVM_EXIT_MMIO;