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 #ifndef __ARM_KVM_EMULATE_H__
8 #define __ARM_KVM_EMULATE_H__
9 
10 #include <linux/kvm_host.h>
11 #include <asm/kvm_asm.h>
12 #include <asm/kvm_mmio.h>
13 #include <asm/kvm_arm.h>
14 #include <asm/cputype.h>
15 
16 /* arm64 compatibility macros */
17 #define PSR_AA32_MODE_ABT	ABT_MODE
18 #define PSR_AA32_MODE_UND	UND_MODE
19 #define PSR_AA32_T_BIT		PSR_T_BIT
20 #define PSR_AA32_I_BIT		PSR_I_BIT
21 #define PSR_AA32_A_BIT		PSR_A_BIT
22 #define PSR_AA32_E_BIT		PSR_E_BIT
23 #define PSR_AA32_IT_MASK	PSR_IT_MASK
24 
25 unsigned long *vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num);
26 
vcpu_reg32(struct kvm_vcpu * vcpu,u8 reg_num)27 static inline unsigned long *vcpu_reg32(struct kvm_vcpu *vcpu, u8 reg_num)
28 {
29 	return vcpu_reg(vcpu, reg_num);
30 }
31 
32 unsigned long *__vcpu_spsr(struct kvm_vcpu *vcpu);
33 
vpcu_read_spsr(struct kvm_vcpu * vcpu)34 static inline unsigned long vpcu_read_spsr(struct kvm_vcpu *vcpu)
35 {
36 	return *__vcpu_spsr(vcpu);
37 }
38 
vcpu_write_spsr(struct kvm_vcpu * vcpu,unsigned long v)39 static inline void vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long v)
40 {
41 	*__vcpu_spsr(vcpu) = v;
42 }
43 
vcpu_get_reg(struct kvm_vcpu * vcpu,u8 reg_num)44 static inline unsigned long vcpu_get_reg(struct kvm_vcpu *vcpu,
45 					 u8 reg_num)
46 {
47 	return *vcpu_reg(vcpu, reg_num);
48 }
49 
vcpu_set_reg(struct kvm_vcpu * vcpu,u8 reg_num,unsigned long val)50 static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
51 				unsigned long val)
52 {
53 	*vcpu_reg(vcpu, reg_num) = val;
54 }
55 
56 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
57 void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
58 void kvm_inject_undef32(struct kvm_vcpu *vcpu);
59 void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr);
60 void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr);
61 void kvm_inject_vabt(struct kvm_vcpu *vcpu);
62 
kvm_inject_undefined(struct kvm_vcpu * vcpu)63 static inline void kvm_inject_undefined(struct kvm_vcpu *vcpu)
64 {
65 	kvm_inject_undef32(vcpu);
66 }
67 
kvm_inject_dabt(struct kvm_vcpu * vcpu,unsigned long addr)68 static inline void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
69 {
70 	kvm_inject_dabt32(vcpu, addr);
71 }
72 
kvm_inject_pabt(struct kvm_vcpu * vcpu,unsigned long addr)73 static inline void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
74 {
75 	kvm_inject_pabt32(vcpu, addr);
76 }
77 
kvm_condition_valid(const struct kvm_vcpu * vcpu)78 static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
79 {
80 	return kvm_condition_valid32(vcpu);
81 }
82 
kvm_skip_instr(struct kvm_vcpu * vcpu,bool is_wide_instr)83 static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
84 {
85 	kvm_skip_instr32(vcpu, is_wide_instr);
86 }
87 
vcpu_reset_hcr(struct kvm_vcpu * vcpu)88 static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
89 {
90 	vcpu->arch.hcr = HCR_GUEST_MASK;
91 }
92 
vcpu_hcr(const struct kvm_vcpu * vcpu)93 static inline unsigned long *vcpu_hcr(const struct kvm_vcpu *vcpu)
94 {
95 	return (unsigned long *)&vcpu->arch.hcr;
96 }
97 
vcpu_clear_wfe_traps(struct kvm_vcpu * vcpu)98 static inline void vcpu_clear_wfe_traps(struct kvm_vcpu *vcpu)
99 {
100 	vcpu->arch.hcr &= ~HCR_TWE;
101 }
102 
vcpu_set_wfe_traps(struct kvm_vcpu * vcpu)103 static inline void vcpu_set_wfe_traps(struct kvm_vcpu *vcpu)
104 {
105 	vcpu->arch.hcr |= HCR_TWE;
106 }
107 
vcpu_mode_is_32bit(const struct kvm_vcpu * vcpu)108 static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
109 {
110 	return true;
111 }
112 
vcpu_pc(struct kvm_vcpu * vcpu)113 static inline unsigned long *vcpu_pc(struct kvm_vcpu *vcpu)
114 {
115 	return &vcpu->arch.ctxt.gp_regs.usr_regs.ARM_pc;
116 }
117 
vcpu_cpsr(const struct kvm_vcpu * vcpu)118 static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
119 {
120 	return (unsigned long *)&vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr;
121 }
122 
vcpu_set_thumb(struct kvm_vcpu * vcpu)123 static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
124 {
125 	*vcpu_cpsr(vcpu) |= PSR_T_BIT;
126 }
127 
mode_has_spsr(struct kvm_vcpu * vcpu)128 static inline bool mode_has_spsr(struct kvm_vcpu *vcpu)
129 {
130 	unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
131 	return (cpsr_mode > USR_MODE && cpsr_mode < SYSTEM_MODE);
132 }
133 
vcpu_mode_priv(struct kvm_vcpu * vcpu)134 static inline bool vcpu_mode_priv(struct kvm_vcpu *vcpu)
135 {
136 	unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
137 	return cpsr_mode > USR_MODE;
138 }
139 
kvm_vcpu_get_hsr(const struct kvm_vcpu * vcpu)140 static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu)
141 {
142 	return vcpu->arch.fault.hsr;
143 }
144 
kvm_vcpu_get_condition(const struct kvm_vcpu * vcpu)145 static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
146 {
147 	u32 hsr = kvm_vcpu_get_hsr(vcpu);
148 
149 	if (hsr & HSR_CV)
150 		return (hsr & HSR_COND) >> HSR_COND_SHIFT;
151 
152 	return -1;
153 }
154 
kvm_vcpu_get_hfar(struct kvm_vcpu * vcpu)155 static inline unsigned long kvm_vcpu_get_hfar(struct kvm_vcpu *vcpu)
156 {
157 	return vcpu->arch.fault.hxfar;
158 }
159 
kvm_vcpu_get_fault_ipa(struct kvm_vcpu * vcpu)160 static inline phys_addr_t kvm_vcpu_get_fault_ipa(struct kvm_vcpu *vcpu)
161 {
162 	return ((phys_addr_t)vcpu->arch.fault.hpfar & HPFAR_MASK) << 8;
163 }
164 
kvm_vcpu_dabt_isvalid(struct kvm_vcpu * vcpu)165 static inline bool kvm_vcpu_dabt_isvalid(struct kvm_vcpu *vcpu)
166 {
167 	return kvm_vcpu_get_hsr(vcpu) & HSR_ISV;
168 }
169 
kvm_vcpu_dabt_iswrite(struct kvm_vcpu * vcpu)170 static inline bool kvm_vcpu_dabt_iswrite(struct kvm_vcpu *vcpu)
171 {
172 	return kvm_vcpu_get_hsr(vcpu) & HSR_WNR;
173 }
174 
kvm_vcpu_dabt_issext(struct kvm_vcpu * vcpu)175 static inline bool kvm_vcpu_dabt_issext(struct kvm_vcpu *vcpu)
176 {
177 	return kvm_vcpu_get_hsr(vcpu) & HSR_SSE;
178 }
179 
kvm_vcpu_dabt_get_rd(struct kvm_vcpu * vcpu)180 static inline int kvm_vcpu_dabt_get_rd(struct kvm_vcpu *vcpu)
181 {
182 	return (kvm_vcpu_get_hsr(vcpu) & HSR_SRT_MASK) >> HSR_SRT_SHIFT;
183 }
184 
kvm_vcpu_dabt_iss1tw(struct kvm_vcpu * vcpu)185 static inline bool kvm_vcpu_dabt_iss1tw(struct kvm_vcpu *vcpu)
186 {
187 	return kvm_vcpu_get_hsr(vcpu) & HSR_DABT_S1PTW;
188 }
189 
kvm_vcpu_dabt_is_cm(struct kvm_vcpu * vcpu)190 static inline bool kvm_vcpu_dabt_is_cm(struct kvm_vcpu *vcpu)
191 {
192 	return !!(kvm_vcpu_get_hsr(vcpu) & HSR_DABT_CM);
193 }
194 
195 /* Get Access Size from a data abort */
kvm_vcpu_dabt_get_as(struct kvm_vcpu * vcpu)196 static inline int kvm_vcpu_dabt_get_as(struct kvm_vcpu *vcpu)
197 {
198 	switch ((kvm_vcpu_get_hsr(vcpu) >> 22) & 0x3) {
199 	case 0:
200 		return 1;
201 	case 1:
202 		return 2;
203 	case 2:
204 		return 4;
205 	default:
206 		kvm_err("Hardware is weird: SAS 0b11 is reserved\n");
207 		return -EFAULT;
208 	}
209 }
210 
211 /* This one is not specific to Data Abort */
kvm_vcpu_trap_il_is32bit(struct kvm_vcpu * vcpu)212 static inline bool kvm_vcpu_trap_il_is32bit(struct kvm_vcpu *vcpu)
213 {
214 	return kvm_vcpu_get_hsr(vcpu) & HSR_IL;
215 }
216 
kvm_vcpu_trap_get_class(struct kvm_vcpu * vcpu)217 static inline u8 kvm_vcpu_trap_get_class(struct kvm_vcpu *vcpu)
218 {
219 	return kvm_vcpu_get_hsr(vcpu) >> HSR_EC_SHIFT;
220 }
221 
kvm_vcpu_trap_is_iabt(struct kvm_vcpu * vcpu)222 static inline bool kvm_vcpu_trap_is_iabt(struct kvm_vcpu *vcpu)
223 {
224 	return kvm_vcpu_trap_get_class(vcpu) == HSR_EC_IABT;
225 }
226 
kvm_vcpu_trap_get_fault(struct kvm_vcpu * vcpu)227 static inline u8 kvm_vcpu_trap_get_fault(struct kvm_vcpu *vcpu)
228 {
229 	return kvm_vcpu_get_hsr(vcpu) & HSR_FSC;
230 }
231 
kvm_vcpu_trap_get_fault_type(struct kvm_vcpu * vcpu)232 static inline u8 kvm_vcpu_trap_get_fault_type(struct kvm_vcpu *vcpu)
233 {
234 	return kvm_vcpu_get_hsr(vcpu) & HSR_FSC_TYPE;
235 }
236 
kvm_vcpu_dabt_isextabt(struct kvm_vcpu * vcpu)237 static inline bool kvm_vcpu_dabt_isextabt(struct kvm_vcpu *vcpu)
238 {
239 	switch (kvm_vcpu_trap_get_fault(vcpu)) {
240 	case FSC_SEA:
241 	case FSC_SEA_TTW0:
242 	case FSC_SEA_TTW1:
243 	case FSC_SEA_TTW2:
244 	case FSC_SEA_TTW3:
245 	case FSC_SECC:
246 	case FSC_SECC_TTW0:
247 	case FSC_SECC_TTW1:
248 	case FSC_SECC_TTW2:
249 	case FSC_SECC_TTW3:
250 		return true;
251 	default:
252 		return false;
253 	}
254 }
255 
kvm_is_write_fault(struct kvm_vcpu * vcpu)256 static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
257 {
258 	if (kvm_vcpu_trap_is_iabt(vcpu))
259 		return false;
260 
261 	return kvm_vcpu_dabt_iswrite(vcpu);
262 }
263 
kvm_vcpu_hvc_get_imm(struct kvm_vcpu * vcpu)264 static inline u32 kvm_vcpu_hvc_get_imm(struct kvm_vcpu *vcpu)
265 {
266 	return kvm_vcpu_get_hsr(vcpu) & HSR_HVC_IMM_MASK;
267 }
268 
kvm_vcpu_get_mpidr_aff(struct kvm_vcpu * vcpu)269 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
270 {
271 	return vcpu_cp15(vcpu, c0_MPIDR) & MPIDR_HWID_BITMASK;
272 }
273 
kvm_arm_get_vcpu_workaround_2_flag(struct kvm_vcpu * vcpu)274 static inline bool kvm_arm_get_vcpu_workaround_2_flag(struct kvm_vcpu *vcpu)
275 {
276 	return false;
277 }
278 
kvm_arm_set_vcpu_workaround_2_flag(struct kvm_vcpu * vcpu,bool flag)279 static inline void kvm_arm_set_vcpu_workaround_2_flag(struct kvm_vcpu *vcpu,
280 						      bool flag)
281 {
282 }
283 
kvm_vcpu_set_be(struct kvm_vcpu * vcpu)284 static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
285 {
286 	*vcpu_cpsr(vcpu) |= PSR_E_BIT;
287 }
288 
kvm_vcpu_is_be(struct kvm_vcpu * vcpu)289 static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
290 {
291 	return !!(*vcpu_cpsr(vcpu) & PSR_E_BIT);
292 }
293 
vcpu_data_guest_to_host(struct kvm_vcpu * vcpu,unsigned long data,unsigned int len)294 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
295 						    unsigned long data,
296 						    unsigned int len)
297 {
298 	if (kvm_vcpu_is_be(vcpu)) {
299 		switch (len) {
300 		case 1:
301 			return data & 0xff;
302 		case 2:
303 			return be16_to_cpu(data & 0xffff);
304 		default:
305 			return be32_to_cpu(data);
306 		}
307 	} else {
308 		switch (len) {
309 		case 1:
310 			return data & 0xff;
311 		case 2:
312 			return le16_to_cpu(data & 0xffff);
313 		default:
314 			return le32_to_cpu(data);
315 		}
316 	}
317 }
318 
vcpu_data_host_to_guest(struct kvm_vcpu * vcpu,unsigned long data,unsigned int len)319 static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
320 						    unsigned long data,
321 						    unsigned int len)
322 {
323 	if (kvm_vcpu_is_be(vcpu)) {
324 		switch (len) {
325 		case 1:
326 			return data & 0xff;
327 		case 2:
328 			return cpu_to_be16(data & 0xffff);
329 		default:
330 			return cpu_to_be32(data);
331 		}
332 	} else {
333 		switch (len) {
334 		case 1:
335 			return data & 0xff;
336 		case 2:
337 			return cpu_to_le16(data & 0xffff);
338 		default:
339 			return cpu_to_le32(data);
340 		}
341 	}
342 }
343 
vcpu_ptrauth_setup_lazy(struct kvm_vcpu * vcpu)344 static inline void vcpu_ptrauth_setup_lazy(struct kvm_vcpu *vcpu) {}
345 
346 #endif /* __ARM_KVM_EMULATE_H__ */
347