xref: /linux/arch/arm64/kvm/mmio.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 
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 
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 
75 static bool kvm_pending_external_abort(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 		case unpack_vcpu_flag(EXCEPT_AA64_EL1_SERR):
94 		case unpack_vcpu_flag(EXCEPT_AA64_EL2_SERR):
95 			return true;
96 		default:
97 			return false;
98 		}
99 	}
100 }
101 
102 /**
103  * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
104  *			     or in-kernel IO emulation
105  *
106  * @vcpu: The VCPU pointer
107  */
108 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
109 {
110 	unsigned long data;
111 	unsigned int len;
112 	int mask;
113 
114 	/*
115 	 * Detect if the MMIO return was already handled or if userspace aborted
116 	 * the MMIO access.
117 	 */
118 	if (unlikely(!vcpu->mmio_needed || kvm_pending_external_abort(vcpu)))
119 		return 1;
120 
121 	vcpu->mmio_needed = 0;
122 
123 	if (!kvm_vcpu_dabt_iswrite(vcpu)) {
124 		struct kvm_run *run = vcpu->run;
125 
126 		len = kvm_vcpu_dabt_get_as(vcpu);
127 		data = kvm_mmio_read_buf(run->mmio.data, len);
128 
129 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
130 			       &data);
131 		data = vcpu_data_host_to_guest(vcpu, data, len);
132 
133 		if (kvm_vcpu_dabt_issext(vcpu) &&
134 		    len < sizeof(unsigned long)) {
135 			mask = 1U << ((len * 8) - 1);
136 			data = (data ^ mask) - mask;
137 		}
138 
139 		if (!kvm_vcpu_dabt_issf(vcpu))
140 			data = data & 0xffffffff;
141 
142 		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
143 	}
144 
145 	/*
146 	 * The MMIO instruction is emulated and should not be re-executed
147 	 * in the guest.
148 	 */
149 	kvm_incr_pc(vcpu);
150 
151 	return 1;
152 }
153 
154 int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
155 {
156 	struct kvm_run *run = vcpu->run;
157 	unsigned long data;
158 	unsigned long rt;
159 	int ret;
160 	bool is_write;
161 	int len;
162 	u8 data_buf[8];
163 	u64 esr;
164 
165 	esr = kvm_vcpu_get_esr(vcpu);
166 
167 	/*
168 	 * No valid syndrome? Ask userspace for help if it has
169 	 * volunteered to do so, and bail out otherwise.
170 	 *
171 	 * In the protected VM case, there isn't much userspace can do
172 	 * though, so directly deliver an exception to the guest.
173 	 */
174 	if (!kvm_vcpu_dabt_isvalid(vcpu)) {
175 		trace_kvm_mmio_nisv(*vcpu_pc(vcpu), esr,
176 				    kvm_vcpu_get_hfar(vcpu), fault_ipa);
177 
178 		if (vcpu_is_protected(vcpu))
179 			return kvm_inject_sea_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
180 
181 		if (test_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
182 			     &vcpu->kvm->arch.flags)) {
183 			run->exit_reason = KVM_EXIT_ARM_NISV;
184 			run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu);
185 			run->arm_nisv.fault_ipa = fault_ipa;
186 			return 0;
187 		}
188 
189 		return -ENOSYS;
190 	}
191 
192 	/*
193 	 * When (DFSC == 0b00xxxx || DFSC == 0b10101x) && DFSC != 0b0000xx
194 	 * ESR_EL2[12:11] describe the Load/Store Type. This allows us to
195 	 * punt the LD64B/ST64B/ST64BV/ST64BV0 instructions to userspace,
196 	 * which will have to provide a full emulation of these 4
197 	 * instructions.  No, we don't expect this do be fast.
198 	 *
199 	 * We rely on traps being set if the corresponding features are not
200 	 * enabled, so if we get here, userspace has promised us to handle
201 	 * it already.
202 	 */
203 	switch (kvm_vcpu_trap_get_fault(vcpu)) {
204 	case 0b000100 ... 0b001111:
205 	case 0b101010 ... 0b101011:
206 		if (FIELD_GET(GENMASK(12, 11), esr)) {
207 			run->exit_reason = KVM_EXIT_ARM_LDST64B;
208 			run->arm_nisv.esr_iss = esr & ~(u64)ESR_ELx_FSC;
209 			run->arm_nisv.fault_ipa = fault_ipa;
210 			return 0;
211 		}
212 	}
213 
214 	/*
215 	 * Prepare MMIO operation. First decode the syndrome data we get
216 	 * from the CPU. Then try if some in-kernel emulation feels
217 	 * responsible, otherwise let user space do its magic.
218 	 */
219 	is_write = kvm_vcpu_dabt_iswrite(vcpu);
220 	len = kvm_vcpu_dabt_get_as(vcpu);
221 	rt = kvm_vcpu_dabt_get_rd(vcpu);
222 
223 	if (is_write) {
224 		data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
225 					       len);
226 
227 		trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
228 		kvm_mmio_write_buf(data_buf, len, data);
229 
230 		ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
231 				       data_buf);
232 	} else {
233 		trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
234 			       fault_ipa, NULL);
235 
236 		ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
237 				      data_buf);
238 	}
239 
240 	/* Now prepare kvm_run for the potential return to userland. */
241 	run->mmio.is_write	= is_write;
242 	run->mmio.phys_addr	= fault_ipa;
243 	run->mmio.len		= len;
244 	vcpu->mmio_needed	= 1;
245 
246 	if (!ret) {
247 		/* We handled the access successfully in the kernel. */
248 		if (!is_write)
249 			memcpy(run->mmio.data, data_buf, len);
250 		vcpu->stat.mmio_exit_kernel++;
251 		kvm_handle_mmio_return(vcpu);
252 		return 1;
253 	}
254 
255 	if (is_write)
256 		memcpy(run->mmio.data, data_buf, len);
257 	vcpu->stat.mmio_exit_user++;
258 	run->exit_reason	= KVM_EXIT_MMIO;
259 	return 0;
260 }
261