blob: 974b1c606d044c239bfa14ffbdf66f0fc982c4fb [file] [log] [blame]
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
19#include <linux/kvm_host.h>
20#include <asm/kvm_mmio.h>
21#include <asm/kvm_emulate.h>
22#include <trace/events/kvm.h>
23
24#include "trace.h"
25
Marc Zyngier6d89d2d2013-02-12 12:40:22 +000026static void mmio_write_buf(char *buf, unsigned int len, unsigned long data)
27{
28 void *datap = NULL;
29 union {
30 u8 byte;
31 u16 hword;
32 u32 word;
33 u64 dword;
34 } tmp;
35
36 switch (len) {
37 case 1:
38 tmp.byte = data;
39 datap = &tmp.byte;
40 break;
41 case 2:
42 tmp.hword = data;
43 datap = &tmp.hword;
44 break;
45 case 4:
46 tmp.word = data;
47 datap = &tmp.word;
48 break;
49 case 8:
50 tmp.dword = data;
51 datap = &tmp.dword;
52 break;
53 }
54
55 memcpy(buf, datap, len);
56}
57
58static unsigned long mmio_read_buf(char *buf, unsigned int len)
59{
60 unsigned long data = 0;
61 union {
62 u16 hword;
63 u32 word;
64 u64 dword;
65 } tmp;
66
67 switch (len) {
68 case 1:
69 data = buf[0];
70 break;
71 case 2:
72 memcpy(&tmp.hword, buf, len);
73 data = tmp.hword;
74 break;
75 case 4:
76 memcpy(&tmp.word, buf, len);
77 data = tmp.word;
78 break;
79 case 8:
80 memcpy(&tmp.dword, buf, len);
81 data = tmp.dword;
82 break;
83 }
84
85 return data;
86}
87
Christoffer Dall45e96ea2013-01-20 18:43:58 -050088/**
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
92 *
93 * This should only be called after returning from userspace for MMIO load
94 * emulation.
95 */
96int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
97{
Marc Zyngier6d89d2d2013-02-12 12:40:22 +000098 unsigned long data;
Christoffer Dall45e96ea2013-01-20 18:43:58 -050099 unsigned int len;
100 int mask;
101
102 if (!run->mmio.is_write) {
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500103 len = run->mmio.len;
Marc Zyngierf42798c2013-03-05 02:43:23 +0000104 if (len > sizeof(unsigned long))
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500105 return -EINVAL;
106
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000107 data = mmio_read_buf(run->mmio.data, len);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500108
Marc Zyngierf42798c2013-03-05 02:43:23 +0000109 if (vcpu->arch.mmio_decode.sign_extend &&
110 len < sizeof(unsigned long)) {
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500111 mask = 1U << ((len * 8) - 1);
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000112 data = (data ^ mask) - mask;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500113 }
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000114
115 trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
116 data);
117 data = vcpu_data_host_to_guest(vcpu, data, len);
118 *vcpu_reg(vcpu, vcpu->arch.mmio_decode.rt) = data;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500119 }
120
121 return 0;
122}
123
Andre Przywara950324a2015-03-28 01:13:13 +0000124static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500125{
Christoffer Dall2184a602013-07-29 20:46:04 -0700126 unsigned long rt;
Andre Przywara950324a2015-03-28 01:13:13 +0000127 int access_size;
128 bool sign_extend;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500129
Marc Zyngier78abfcd2012-09-18 11:36:16 +0100130 if (kvm_vcpu_dabt_isextabt(vcpu)) {
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500131 /* cache operation on I/O addr, tell guest unsupported */
Marc Zyngier7393b592012-09-17 19:27:09 +0100132 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500133 return 1;
134 }
135
Marc Zyngierb37670b2012-09-18 11:37:28 +0100136 if (kvm_vcpu_dabt_iss1tw(vcpu)) {
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500137 /* page table accesses IO mem: tell guest to fix its TTBR */
Marc Zyngier7393b592012-09-17 19:27:09 +0100138 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500139 return 1;
140 }
141
Andre Przywara950324a2015-03-28 01:13:13 +0000142 access_size = kvm_vcpu_dabt_get_as(vcpu);
143 if (unlikely(access_size < 0))
144 return access_size;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500145
Andre Przywara950324a2015-03-28 01:13:13 +0000146 *is_write = kvm_vcpu_dabt_iswrite(vcpu);
Marc Zyngier7c511b82012-09-18 11:23:02 +0100147 sign_extend = kvm_vcpu_dabt_issext(vcpu);
Marc Zyngierd0adf742012-09-18 11:28:57 +0100148 rt = kvm_vcpu_dabt_get_rd(vcpu);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500149
Andre Przywara950324a2015-03-28 01:13:13 +0000150 *len = access_size;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500151 vcpu->arch.mmio_decode.sign_extend = sign_extend;
152 vcpu->arch.mmio_decode.rt = rt;
153
154 /*
155 * The MMIO instruction is emulated and should not be re-executed
156 * in the guest.
157 */
Marc Zyngier23b415d2012-09-18 12:07:06 +0100158 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500159 return 0;
160}
161
162int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
163 phys_addr_t fault_ipa)
164{
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000165 unsigned long data;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500166 unsigned long rt;
167 int ret;
Andre Przywara950324a2015-03-28 01:13:13 +0000168 bool is_write;
169 int len;
170 u8 data_buf[8];
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500171
172 /*
Andre Przywara950324a2015-03-28 01:13:13 +0000173 * Prepare MMIO operation. First decode the syndrome data we get
174 * from the CPU. Then try if some in-kernel emulation feels
175 * responsible, otherwise let user space do its magic.
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500176 */
Marc Zyngier4a1df282012-09-18 11:06:23 +0100177 if (kvm_vcpu_dabt_isvalid(vcpu)) {
Andre Przywara950324a2015-03-28 01:13:13 +0000178 ret = decode_hsr(vcpu, &is_write, &len);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500179 if (ret)
180 return ret;
181 } else {
182 kvm_err("load/store instruction decoding not implemented\n");
183 return -ENOSYS;
184 }
185
186 rt = vcpu->arch.mmio_decode.rt;
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000187
Andre Przywara950324a2015-03-28 01:13:13 +0000188 if (is_write) {
189 data = vcpu_data_guest_to_host(vcpu, *vcpu_reg(vcpu, rt), len);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500190
Andre Przywara950324a2015-03-28 01:13:13 +0000191 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, data);
192 mmio_write_buf(data_buf, len, data);
193
194 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
195 data_buf);
Andre Przywara5100f9832014-11-06 12:11:45 +0000196 } else {
Andre Przywara950324a2015-03-28 01:13:13 +0000197 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
Andre Przywara5100f9832014-11-06 12:11:45 +0000198 fault_ipa, 0);
Andre Przywara950324a2015-03-28 01:13:13 +0000199
200 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
201 data_buf);
Andre Przywara5100f9832014-11-06 12:11:45 +0000202 }
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500203
Andre Przywara950324a2015-03-28 01:13:13 +0000204 /* Now prepare kvm_run for the potential return to userland. */
205 run->mmio.is_write = is_write;
206 run->mmio.phys_addr = fault_ipa;
207 run->mmio.len = len;
208 memcpy(run->mmio.data, data_buf, len);
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500209
Andre Przywara950324a2015-03-28 01:13:13 +0000210 if (!ret) {
211 /* We handled the access successfully in the kernel. */
212 kvm_handle_mmio_return(vcpu, run);
213 return 1;
214 }
215
216 run->exit_reason = KVM_EXIT_MMIO;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500217 return 0;
218}