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 static void mmio_write_buf(char *buf
, unsigned int len
, unsigned long data
)
55 memcpy(buf
, datap
, len
);
58 static unsigned long mmio_read_buf(char *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 * @vcpu: The VCPU pointer
91 * @run: The VCPU run struct containing the mmio data
93 * This should only be called after returning from userspace for MMIO load
96 int kvm_handle_mmio_return(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
102 if (!run
->mmio
.is_write
) {
104 if (len
> sizeof(unsigned long))
107 data
= mmio_read_buf(run
->mmio
.data
, len
);
109 if (vcpu
->arch
.mmio_decode
.sign_extend
&&
110 len
< sizeof(unsigned long)) {
111 mask
= 1U << ((len
* 8) - 1);
112 data
= (data
^ mask
) - mask
;
115 trace_kvm_mmio(KVM_TRACE_MMIO_READ
, len
, run
->mmio
.phys_addr
,
117 data
= vcpu_data_host_to_guest(vcpu
, data
, len
);
118 *vcpu_reg(vcpu
, vcpu
->arch
.mmio_decode
.rt
) = data
;
124 static int decode_hsr(struct kvm_vcpu
*vcpu
, phys_addr_t fault_ipa
,
125 struct kvm_exit_mmio
*mmio
)
129 bool is_write
, sign_extend
;
131 if (kvm_vcpu_dabt_isextabt(vcpu
)) {
132 /* cache operation on I/O addr, tell guest unsupported */
133 kvm_inject_dabt(vcpu
, kvm_vcpu_get_hfar(vcpu
));
137 if (kvm_vcpu_dabt_iss1tw(vcpu
)) {
138 /* page table accesses IO mem: tell guest to fix its TTBR */
139 kvm_inject_dabt(vcpu
, kvm_vcpu_get_hfar(vcpu
));
143 len
= kvm_vcpu_dabt_get_as(vcpu
);
144 if (unlikely(len
< 0))
147 is_write
= kvm_vcpu_dabt_iswrite(vcpu
);
148 sign_extend
= kvm_vcpu_dabt_issext(vcpu
);
149 rt
= kvm_vcpu_dabt_get_rd(vcpu
);
151 mmio
->is_write
= is_write
;
152 mmio
->phys_addr
= fault_ipa
;
154 vcpu
->arch
.mmio_decode
.sign_extend
= sign_extend
;
155 vcpu
->arch
.mmio_decode
.rt
= rt
;
158 * The MMIO instruction is emulated and should not be re-executed
161 kvm_skip_instr(vcpu
, kvm_vcpu_trap_il_is32bit(vcpu
));
165 int io_mem_abort(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
,
166 phys_addr_t fault_ipa
)
168 struct kvm_exit_mmio mmio
;
174 * Prepare MMIO operation. First stash it in a private
175 * structure that we can use for in-kernel emulation. If the
176 * kernel can't handle it, copy it into run->mmio and let user
177 * space do its magic.
180 if (kvm_vcpu_dabt_isvalid(vcpu
)) {
181 ret
= decode_hsr(vcpu
, fault_ipa
, &mmio
);
185 kvm_err("load/store instruction decoding not implemented\n");
189 rt
= vcpu
->arch
.mmio_decode
.rt
;
190 data
= vcpu_data_guest_to_host(vcpu
, *vcpu_reg(vcpu
, rt
), mmio
.len
);
192 trace_kvm_mmio((mmio
.is_write
) ? KVM_TRACE_MMIO_WRITE
:
193 KVM_TRACE_MMIO_READ_UNSATISFIED
,
195 (mmio
.is_write
) ? data
: 0);
198 mmio_write_buf(mmio
.data
, mmio
.len
, data
);
200 if (vgic_handle_mmio(vcpu
, run
, &mmio
))
203 kvm_prepare_mmio(run
, &mmio
);