1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5 */
6
7 #include <linux/kvm_host.h>
8 #include <asm/kvm_emulate.h>
9 #include <trace/events/kvm.h>
10
11 #include "trace.h"
12
kvm_mmio_write_buf(void * buf,unsigned int len,unsigned long data)13 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data)
14 {
15 void *datap = NULL;
16 union {
17 u8 byte;
18 u16 hword;
19 u32 word;
20 u64 dword;
21 } tmp;
22
23 switch (len) {
24 case 1:
25 tmp.byte = data;
26 datap = &tmp.byte;
27 break;
28 case 2:
29 tmp.hword = data;
30 datap = &tmp.hword;
31 break;
32 case 4:
33 tmp.word = data;
34 datap = &tmp.word;
35 break;
36 case 8:
37 tmp.dword = data;
38 datap = &tmp.dword;
39 break;
40 }
41
42 memcpy(buf, datap, len);
43 }
44
kvm_mmio_read_buf(const void * buf,unsigned int len)45 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len)
46 {
47 unsigned long data = 0;
48 union {
49 u16 hword;
50 u32 word;
51 u64 dword;
52 } tmp;
53
54 switch (len) {
55 case 1:
56 data = *(u8 *)buf;
57 break;
58 case 2:
59 memcpy(&tmp.hword, buf, len);
60 data = tmp.hword;
61 break;
62 case 4:
63 memcpy(&tmp.word, buf, len);
64 data = tmp.word;
65 break;
66 case 8:
67 memcpy(&tmp.dword, buf, len);
68 data = tmp.dword;
69 break;
70 }
71
72 return data;
73 }
74
kvm_pending_sync_exception(struct kvm_vcpu * vcpu)75 static bool kvm_pending_sync_exception(struct kvm_vcpu *vcpu)
76 {
77 if (!vcpu_get_flag(vcpu, PENDING_EXCEPTION))
78 return false;
79
80 if (vcpu_el1_is_32bit(vcpu)) {
81 switch (vcpu_get_flag(vcpu, EXCEPT_MASK)) {
82 case unpack_vcpu_flag(EXCEPT_AA32_UND):
83 case unpack_vcpu_flag(EXCEPT_AA32_IABT):
84 case unpack_vcpu_flag(EXCEPT_AA32_DABT):
85 return true;
86 default:
87 return false;
88 }
89 } else {
90 switch (vcpu_get_flag(vcpu, EXCEPT_MASK)) {
91 case unpack_vcpu_flag(EXCEPT_AA64_EL1_SYNC):
92 case unpack_vcpu_flag(EXCEPT_AA64_EL2_SYNC):
93 return true;
94 default:
95 return false;
96 }
97 }
98 }
99
100 /**
101 * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
102 * or in-kernel IO emulation
103 *
104 * @vcpu: The VCPU pointer
105 */
kvm_handle_mmio_return(struct kvm_vcpu * vcpu)106 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
107 {
108 unsigned long data;
109 unsigned int len;
110 int mask;
111
112 /*
113 * Detect if the MMIO return was already handled or if userspace aborted
114 * the MMIO access.
115 */
116 if (unlikely(!vcpu->mmio_needed || kvm_pending_sync_exception(vcpu)))
117 return 1;
118
119 vcpu->mmio_needed = 0;
120
121 if (!kvm_vcpu_dabt_iswrite(vcpu)) {
122 struct kvm_run *run = vcpu->run;
123
124 len = kvm_vcpu_dabt_get_as(vcpu);
125 data = kvm_mmio_read_buf(run->mmio.data, len);
126
127 if (kvm_vcpu_dabt_issext(vcpu) &&
128 len < sizeof(unsigned long)) {
129 mask = 1U << ((len * 8) - 1);
130 data = (data ^ mask) - mask;
131 }
132
133 if (!kvm_vcpu_dabt_issf(vcpu))
134 data = data & 0xffffffff;
135
136 trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
137 &data);
138 data = vcpu_data_host_to_guest(vcpu, data, len);
139 vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
140 }
141
142 /*
143 * The MMIO instruction is emulated and should not be re-executed
144 * in the guest.
145 */
146 kvm_incr_pc(vcpu);
147
148 return 1;
149 }
150
io_mem_abort(struct kvm_vcpu * vcpu,phys_addr_t fault_ipa)151 int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
152 {
153 struct kvm_run *run = vcpu->run;
154 unsigned long data;
155 unsigned long rt;
156 int ret;
157 bool is_write;
158 int len;
159 u8 data_buf[8];
160
161 /*
162 * No valid syndrome? Ask userspace for help if it has
163 * volunteered to do so, and bail out otherwise.
164 */
165 if (!kvm_vcpu_dabt_isvalid(vcpu)) {
166 if (test_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
167 &vcpu->kvm->arch.flags)) {
168 run->exit_reason = KVM_EXIT_ARM_NISV;
169 run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu);
170 run->arm_nisv.fault_ipa = fault_ipa;
171 return 0;
172 }
173
174 kvm_pr_unimpl("Data abort outside memslots with no valid syndrome info\n");
175 return -ENOSYS;
176 }
177
178 /*
179 * Prepare MMIO operation. First decode the syndrome data we get
180 * from the CPU. Then try if some in-kernel emulation feels
181 * responsible, otherwise let user space do its magic.
182 */
183 is_write = kvm_vcpu_dabt_iswrite(vcpu);
184 len = kvm_vcpu_dabt_get_as(vcpu);
185 rt = kvm_vcpu_dabt_get_rd(vcpu);
186
187 if (is_write) {
188 data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
189 len);
190
191 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
192 kvm_mmio_write_buf(data_buf, len, data);
193
194 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
195 data_buf);
196 } else {
197 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
198 fault_ipa, NULL);
199
200 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
201 data_buf);
202 }
203
204 /* 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 vcpu->mmio_needed = 1;
209
210 if (!ret) {
211 /* We handled the access successfully in the kernel. */
212 if (!is_write)
213 memcpy(run->mmio.data, data_buf, len);
214 vcpu->stat.mmio_exit_kernel++;
215 kvm_handle_mmio_return(vcpu);
216 return 1;
217 }
218
219 if (is_write)
220 memcpy(run->mmio.data, data_buf, len);
221 vcpu->stat.mmio_exit_user++;
222 run->exit_reason = KVM_EXIT_MMIO;
223 return 0;
224 }
225