xref: /openbmc/linux/arch/arm64/kvm/mmio.c (revision 305c8388)
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 /**
76  * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
77  *			     or in-kernel IO emulation
78  *
79  * @vcpu: The VCPU pointer
80  * @run:  The VCPU run struct containing the mmio data
81  */
82 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
83 {
84 	unsigned long data;
85 	unsigned int len;
86 	int mask;
87 
88 	/* Detect an already handled MMIO return */
89 	if (unlikely(!vcpu->mmio_needed))
90 		return 0;
91 
92 	vcpu->mmio_needed = 0;
93 
94 	if (!kvm_vcpu_dabt_iswrite(vcpu)) {
95 		len = kvm_vcpu_dabt_get_as(vcpu);
96 		data = kvm_mmio_read_buf(run->mmio.data, len);
97 
98 		if (kvm_vcpu_dabt_issext(vcpu) &&
99 		    len < sizeof(unsigned long)) {
100 			mask = 1U << ((len * 8) - 1);
101 			data = (data ^ mask) - mask;
102 		}
103 
104 		if (!kvm_vcpu_dabt_issf(vcpu))
105 			data = data & 0xffffffff;
106 
107 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
108 			       &data);
109 		data = vcpu_data_host_to_guest(vcpu, data, len);
110 		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
111 	}
112 
113 	/*
114 	 * The MMIO instruction is emulated and should not be re-executed
115 	 * in the guest.
116 	 */
117 	kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
118 
119 	return 0;
120 }
121 
122 int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
123 		 phys_addr_t fault_ipa)
124 {
125 	unsigned long data;
126 	unsigned long rt;
127 	int ret;
128 	bool is_write;
129 	int len;
130 	u8 data_buf[8];
131 
132 	/*
133 	 * No valid syndrome? Ask userspace for help if it has
134 	 * volunteered to do so, and bail out otherwise.
135 	 */
136 	if (!kvm_vcpu_dabt_isvalid(vcpu)) {
137 		if (vcpu->kvm->arch.return_nisv_io_abort_to_user) {
138 			run->exit_reason = KVM_EXIT_ARM_NISV;
139 			run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu);
140 			run->arm_nisv.fault_ipa = fault_ipa;
141 			return 0;
142 		}
143 
144 		kvm_pr_unimpl("Data abort outside memslots with no valid syndrome info\n");
145 		return -ENOSYS;
146 	}
147 
148 	/* Page table accesses IO mem: tell guest to fix its TTBR */
149 	if (kvm_vcpu_dabt_iss1tw(vcpu)) {
150 		kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
151 		return 1;
152 	}
153 
154 	/*
155 	 * Prepare MMIO operation. First decode the syndrome data we get
156 	 * from the CPU. Then try if some in-kernel emulation feels
157 	 * responsible, otherwise let user space do its magic.
158 	 */
159 	is_write = kvm_vcpu_dabt_iswrite(vcpu);
160 	len = kvm_vcpu_dabt_get_as(vcpu);
161 	rt = kvm_vcpu_dabt_get_rd(vcpu);
162 
163 	if (is_write) {
164 		data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
165 					       len);
166 
167 		trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
168 		kvm_mmio_write_buf(data_buf, len, data);
169 
170 		ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
171 				       data_buf);
172 	} else {
173 		trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
174 			       fault_ipa, NULL);
175 
176 		ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
177 				      data_buf);
178 	}
179 
180 	/* Now prepare kvm_run for the potential return to userland. */
181 	run->mmio.is_write	= is_write;
182 	run->mmio.phys_addr	= fault_ipa;
183 	run->mmio.len		= len;
184 	vcpu->mmio_needed	= 1;
185 
186 	if (!ret) {
187 		/* We handled the access successfully in the kernel. */
188 		if (!is_write)
189 			memcpy(run->mmio.data, data_buf, len);
190 		vcpu->stat.mmio_exit_kernel++;
191 		kvm_handle_mmio_return(vcpu, run);
192 		return 1;
193 	}
194 
195 	if (is_write)
196 		memcpy(run->mmio.data, data_buf, len);
197 	vcpu->stat.mmio_exit_user++;
198 	run->exit_reason	= KVM_EXIT_MMIO;
199 	return 0;
200 }
201