2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
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.
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.
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.
19 #include <linux/kvm_host.h>
20 #include <asm/kvm_mmio.h>
21 #include <asm/kvm_emulate.h>
22 #include <trace/events/kvm.h>
26 void kvm_mmio_write_buf(void *buf
, unsigned int len
, unsigned long data
)
55 memcpy(buf
, datap
, len
);
58 unsigned long kvm_mmio_read_buf(const void *buf
, unsigned int len
)
60 unsigned long data
= 0;
72 memcpy(&tmp
.hword
, buf
, len
);
76 memcpy(&tmp
.word
, buf
, len
);
80 memcpy(&tmp
.dword
, buf
, len
);
89 * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
90 * or in-kernel IO emulation
92 * @vcpu: The VCPU pointer
93 * @run: The VCPU run struct containing the mmio data
95 int kvm_handle_mmio_return(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
101 if (!run
->mmio
.is_write
) {
103 if (len
> sizeof(unsigned long))
106 data
= kvm_mmio_read_buf(run
->mmio
.data
, len
);
108 if (vcpu
->arch
.mmio_decode
.sign_extend
&&
109 len
< sizeof(unsigned long)) {
110 mask
= 1U << ((len
* 8) - 1);
111 data
= (data
^ mask
) - mask
;
114 trace_kvm_mmio(KVM_TRACE_MMIO_READ
, len
, run
->mmio
.phys_addr
,
116 data
= vcpu_data_host_to_guest(vcpu
, data
, len
);
117 vcpu_set_reg(vcpu
, vcpu
->arch
.mmio_decode
.rt
, data
);
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
;
148 * The MMIO instruction is emulated and should not be re-executed
151 kvm_skip_instr(vcpu
, kvm_vcpu_trap_il_is32bit(vcpu
));
155 int io_mem_abort(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
,
156 phys_addr_t fault_ipa
)
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.
170 if (kvm_vcpu_dabt_isvalid(vcpu
)) {
171 ret
= decode_hsr(vcpu
, &is_write
, &len
);
175 kvm_err("load/store instruction decoding not implemented\n");
179 rt
= vcpu
->arch
.mmio_decode
.rt
;
182 data
= vcpu_data_guest_to_host(vcpu
, vcpu_get_reg(vcpu
, rt
),
185 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE
, len
, fault_ipa
, data
);
186 kvm_mmio_write_buf(data_buf
, len
, data
);
188 ret
= kvm_io_bus_write(vcpu
, KVM_MMIO_BUS
, fault_ipa
, len
,
191 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED
, len
,
194 ret
= kvm_io_bus_read(vcpu
, KVM_MMIO_BUS
, fault_ipa
, len
,
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
;
204 /* We handled the access successfully in the kernel. */
206 memcpy(run
->mmio
.data
, data_buf
, len
);
207 vcpu
->stat
.mmio_exit_kernel
++;
208 kvm_handle_mmio_return(vcpu
, run
);
213 memcpy(run
->mmio
.data
, data_buf
, len
);
214 vcpu
->stat
.mmio_exit_user
++;
215 run
->exit_reason
= KVM_EXIT_MMIO
;