xref: /linux/arch/arm64/include/asm/kvm_emulate.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012,2013 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  *
6  * Derived from arch/arm/include/kvm_emulate.h
7  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
8  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
9  */
10 
11 #ifndef __ARM64_KVM_EMULATE_H__
12 #define __ARM64_KVM_EMULATE_H__
13 
14 #include <linux/bitfield.h>
15 #include <linux/kvm_host.h>
16 
17 #include <asm/debug-monitors.h>
18 #include <asm/esr.h>
19 #include <asm/kvm_arm.h>
20 #include <asm/kvm_hyp.h>
21 #include <asm/kvm_nested.h>
22 #include <asm/ptrace.h>
23 #include <asm/cputype.h>
24 #include <asm/virt.h>
25 
26 #define CURRENT_EL_SP_EL0_VECTOR	0x0
27 #define CURRENT_EL_SP_ELx_VECTOR	0x200
28 #define LOWER_EL_AArch64_VECTOR		0x400
29 #define LOWER_EL_AArch32_VECTOR		0x600
30 
31 enum exception_type {
32 	except_type_sync	= 0,
33 	except_type_irq		= 0x80,
34 	except_type_fiq		= 0x100,
35 	except_type_serror	= 0x180,
36 };
37 
38 #define kvm_exception_type_names		\
39 	{ except_type_sync,	"SYNC"   },	\
40 	{ except_type_irq,	"IRQ"    },	\
41 	{ except_type_fiq,	"FIQ"    },	\
42 	{ except_type_serror,	"SERROR" }
43 
44 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
45 void kvm_skip_instr32(struct kvm_vcpu *vcpu);
46 
47 #if ARM64_S390_COMMON == 1
48 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
49 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
50 int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
51 int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
52 int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
53 void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
54 
55 static inline int kvm_inject_sea_dabt(struct kvm_vcpu *vcpu, u64 addr)
56 {
57 	return kvm_inject_sea(vcpu, false, addr);
58 }
59 
60 static inline int kvm_inject_sea_iabt(struct kvm_vcpu *vcpu, u64 addr)
61 {
62 	return kvm_inject_sea(vcpu, true, addr);
63 }
64 
65 static inline int kvm_inject_serror(struct kvm_vcpu *vcpu)
66 {
67 	/*
68 	 * ESR_ELx.ISV (later renamed to IDS) indicates whether or not
69 	 * ESR_ELx.ISS contains IMPLEMENTATION DEFINED syndrome information.
70 	 *
71 	 * Set the bit when injecting an SError w/o an ESR to indicate ISS
72 	 * does not follow the architected format.
73 	 */
74 	return kvm_inject_serror_esr(vcpu, ESR_ELx_ISV);
75 }
76 
77 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu);
78 
79 #endif /* ARM64_S390_COMMON == 1 */
80 
81 void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu);
82 int kvm_inject_nested_sync(struct kvm_vcpu *vcpu, u64 esr_el2);
83 int kvm_inject_nested_irq(struct kvm_vcpu *vcpu);
84 int kvm_inject_nested_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
85 int kvm_inject_nested_serror(struct kvm_vcpu *vcpu, u64 esr);
86 
87 static inline void kvm_inject_nested_sve_trap(struct kvm_vcpu *vcpu)
88 {
89 	u64 esr = FIELD_PREP(ESR_ELx_EC_MASK, ESR_ELx_EC_SVE) |
90 		  ESR_ELx_IL;
91 
92 	kvm_inject_nested_sync(vcpu, esr);
93 }
94 
95 #if defined(__KVM_VHE_HYPERVISOR__) || defined(__KVM_NVHE_HYPERVISOR__)
96 static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
97 {
98 	return !(vcpu->arch.hcr_el2 & HCR_EL2_RW);
99 }
100 #else
101 static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
102 {
103 	return vcpu_has_feature(vcpu, KVM_ARM_VCPU_EL1_32BIT);
104 }
105 #endif
106 
107 static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
108 {
109 	if (!vcpu_has_run_once(vcpu))
110 		vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
111 
112 	/*
113 	 * For non-FWB CPUs, we trap VM ops (HCR_EL2.TVM) until M+C
114 	 * get set in SCTLR_EL1 such that we can detect when the guest
115 	 * MMU gets turned on and do the necessary cache maintenance
116 	 * then.
117 	 */
118 	if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
119 		vcpu->arch.hcr_el2 |= HCR_EL2_TVM;
120 }
121 
122 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
123 {
124 	return (unsigned long *)&vcpu->arch.hcr_el2;
125 }
126 
127 static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
128 {
129 	return vcpu->arch.vsesr_el2;
130 }
131 
132 static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
133 {
134 	vcpu->arch.vsesr_el2 = vsesr;
135 }
136 
137 static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
138 {
139 	return (unsigned long *)&vcpu->arch.ctxt.regs.pc;
140 }
141 
142 static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
143 {
144 	return (unsigned long *)&vcpu->arch.ctxt.regs.pstate;
145 }
146 
147 static __always_inline unsigned long *vcpu_sp_el0(struct kvm_vcpu *vcpu)
148 {
149 	return (unsigned long *)&vcpu->arch.ctxt.regs.sp;
150 }
151 
152 static __always_inline u64 *vcpu_sp_el1(struct kvm_vcpu *vcpu)
153 {
154 	return __ctxt_sys_reg(&vcpu->arch.ctxt, SP_EL1);
155 }
156 
157 static __always_inline __u128 *vcpu_vreg(struct kvm_vcpu *vcpu, int n)
158 {
159 	return &vcpu->arch.ctxt.fp_regs.vregs[n];
160 }
161 
162 static __always_inline __u32 *vcpu_fpsr(struct kvm_vcpu *vcpu)
163 {
164 	return &vcpu->arch.ctxt.fp_regs.fpsr;
165 }
166 
167 static __always_inline __u32 *vcpu_fpcr(struct kvm_vcpu *vcpu)
168 {
169 	return &vcpu->arch.ctxt.fp_regs.fpcr;
170 }
171 
172 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
173 {
174 	return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
175 }
176 
177 static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
178 {
179 	if (vcpu_mode_is_32bit(vcpu))
180 		return kvm_condition_valid32(vcpu);
181 
182 	return true;
183 }
184 
185 static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
186 {
187 	*vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
188 }
189 
190 #if ARM64_S390_COMMON == 1
191 /*
192  * vcpu_get_reg and vcpu_set_reg should always be passed a register number
193  * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
194  * AArch32 with banked registers.
195  */
196 static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
197 					 u8 reg_num)
198 {
199 	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)[reg_num];
200 }
201 
202 static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
203 				unsigned long val)
204 {
205 	if (reg_num != 31)
206 		vcpu_gp_regs(vcpu)[reg_num] = val;
207 }
208 
209 #endif /* ARM64_S390_COMMON == 1 */
210 
211 static inline bool vcpu_is_el2_ctxt(const struct kvm_cpu_context *ctxt)
212 {
213 	switch (ctxt->regs.pstate & (PSR_MODE32_BIT | PSR_MODE_MASK)) {
214 	case PSR_MODE_EL2h:
215 	case PSR_MODE_EL2t:
216 		return true;
217 	default:
218 		return false;
219 	}
220 }
221 
222 static inline bool vcpu_is_el2(const struct kvm_vcpu *vcpu)
223 {
224 	return vcpu_is_el2_ctxt(&vcpu->arch.ctxt);
225 }
226 
227 static inline bool vcpu_el2_e2h_is_set(const struct kvm_vcpu *vcpu)
228 {
229 	return (!cpus_have_final_cap(ARM64_HAS_HCR_NV1) ||
230 		(__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_EL2_E2H));
231 }
232 
233 static inline bool vcpu_el2_tge_is_set(const struct kvm_vcpu *vcpu)
234 {
235 	return ctxt_sys_reg(&vcpu->arch.ctxt, HCR_EL2) & HCR_EL2_TGE;
236 }
237 
238 static inline bool vcpu_el2_amo_is_set(const struct kvm_vcpu *vcpu)
239 {
240 	/*
241 	 * DDI0487L.b Known Issue D22105
242 	 *
243 	 * When executing at EL2 and HCR_EL2.{E2H,TGE} = {1, 0} it is
244 	 * IMPLEMENTATION DEFINED whether the effective value of HCR_EL2.AMO
245 	 * is the value programmed or 1.
246 	 *
247 	 * Make the implementation choice of treating the effective value as 1 as
248 	 * we cannot subsequently catch changes to TGE or AMO that would
249 	 * otherwise lead to the SError becoming deliverable.
250 	 */
251 	if (vcpu_is_el2(vcpu) && vcpu_el2_e2h_is_set(vcpu) && !vcpu_el2_tge_is_set(vcpu))
252 		return true;
253 
254 	return ctxt_sys_reg(&vcpu->arch.ctxt, HCR_EL2) & HCR_EL2_AMO;
255 }
256 
257 static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
258 {
259 	bool e2h, tge;
260 	u64 hcr;
261 
262 	if (!vcpu_has_nv(vcpu))
263 		return false;
264 
265 	hcr = __vcpu_sys_reg(vcpu, HCR_EL2);
266 
267 	e2h = (hcr & HCR_EL2_E2H);
268 	tge = (hcr & HCR_EL2_TGE);
269 
270 	/*
271 	 * We are in a hypervisor context if the vcpu mode is EL2 or
272 	 * E2H and TGE bits are set. The latter means we are in the user space
273 	 * of the VHE kernel. ARMv8.1 ARM describes this as 'InHost'
274 	 *
275 	 * Note that the HCR_EL2.{E2H,TGE}={0,1} isn't really handled in the
276 	 * rest of the KVM code, and will result in a misbehaving guest.
277 	 */
278 	return vcpu_is_el2(vcpu) || (e2h && tge) || tge;
279 }
280 
281 static inline bool vcpu_is_host_el0(const struct kvm_vcpu *vcpu)
282 {
283 	return is_hyp_ctxt(vcpu) && !vcpu_is_el2(vcpu);
284 }
285 
286 static inline bool is_nested_ctxt(struct kvm_vcpu *vcpu)
287 {
288 	return vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu);
289 }
290 
291 static inline bool vserror_state_is_nested(struct kvm_vcpu *vcpu)
292 {
293 	if (!is_nested_ctxt(vcpu))
294 		return false;
295 
296 	return vcpu_el2_amo_is_set(vcpu) ||
297 	       (__vcpu_sys_reg(vcpu, HCRX_EL2) & HCRX_EL2_TMEA);
298 }
299 
300 static inline bool kvm_has_nv2(struct kvm *kvm)
301 {
302 	return (cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) &&
303 		kvm_has_feat(kvm, ID_AA64MMFR4_EL1, NV_frac, NV2_ONLY));
304 }
305 
306 static inline bool kvm_has_nv3(struct kvm *kvm)
307 {
308 	return (cpus_have_final_cap(ARM64_HAS_NV3) &&
309 		kvm_has_feat(kvm, ID_AA64MMFR4_EL1, NV_frac, NV3));
310 }
311 
312 static inline bool is_nested_nv3_ctxt(struct kvm_vcpu *vcpu)
313 {
314 	return (has_vhe() && kvm_has_nv3(vcpu->kvm) && is_nested_ctxt(vcpu) &&
315 		(__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_EL2_NV) &&
316 		(__vcpu_sys_reg(vcpu, HCRX_EL2) & HCRX_EL2_NVTGE));
317 }
318 
319 /*
320  * The layout of SPSR for an AArch32 state is different when observed from an
321  * AArch64 SPSR_ELx or an AArch32 SPSR_*. This function generates the AArch32
322  * view given an AArch64 view.
323  *
324  * In ARM DDI 0487E.a see:
325  *
326  * - The AArch64 view (SPSR_EL2) in section C5.2.18, page C5-426
327  * - The AArch32 view (SPSR_abt) in section G8.2.126, page G8-6256
328  * - The AArch32 view (SPSR_und) in section G8.2.132, page G8-6280
329  *
330  * Which show the following differences:
331  *
332  * | Bit | AA64 | AA32 | Notes                       |
333  * +-----+------+------+-----------------------------|
334  * | 24  | DIT  | J    | J is RES0 in ARMv8          |
335  * | 21  | SS   | DIT  | SS doesn't exist in AArch32 |
336  *
337  * ... and all other bits are (currently) common.
338  */
339 static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
340 {
341 	const unsigned long overlap = BIT(24) | BIT(21);
342 	unsigned long dit = !!(spsr & PSR_AA32_DIT_BIT);
343 
344 	spsr &= ~overlap;
345 
346 	spsr |= dit << 21;
347 
348 	return spsr;
349 }
350 
351 static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
352 {
353 	u32 mode;
354 
355 	if (vcpu_mode_is_32bit(vcpu)) {
356 		mode = *vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK;
357 		return mode > PSR_AA32_MODE_USR;
358 	}
359 
360 	mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
361 
362 	return mode != PSR_MODE_EL0t;
363 }
364 
365 static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
366 {
367 	return vcpu->arch.fault.esr_el2;
368 }
369 
370 static inline bool guest_hyp_wfx_traps_enabled(const struct kvm_vcpu *vcpu)
371 {
372 	u64 esr = kvm_vcpu_get_esr(vcpu);
373 	bool is_wfe = !!(esr & ESR_ELx_WFx_ISS_WFE);
374 	u64 hcr_el2 = __vcpu_sys_reg(vcpu, HCR_EL2);
375 
376 	if (!vcpu_has_nv(vcpu) || vcpu_is_el2(vcpu))
377 		return false;
378 
379 	return ((is_wfe && (hcr_el2 & HCR_EL2_TWE)) ||
380 		(!is_wfe && (hcr_el2 & HCR_EL2_TWI)));
381 }
382 
383 static __always_inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
384 {
385 	u64 esr = kvm_vcpu_get_esr(vcpu);
386 
387 	if (esr & ESR_ELx_CV)
388 		return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
389 
390 	return -1;
391 }
392 
393 static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
394 {
395 	return vcpu->arch.fault.far_el2;
396 }
397 
398 static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
399 {
400 	u64 hpfar = vcpu->arch.fault.hpfar_el2;
401 
402 	if (unlikely(!(hpfar & HPFAR_EL2_NS)))
403 		return INVALID_GPA;
404 
405 	return FIELD_GET(HPFAR_EL2_FIPA, hpfar) << 12;
406 }
407 
408 static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
409 {
410 	return vcpu->arch.fault.disr_el1;
411 }
412 
413 #if ARM64_S390_COMMON == 1
414 static __always_inline bool esr_abt_is_s1ptw(unsigned long esr)
415 {
416 	return esr & ESR_ELx_S1PTW;
417 }
418 
419 static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
420 {
421 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
422 }
423 
424 static __always_inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
425 {
426 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_ISV);
427 }
428 
429 static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
430 {
431 	return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC);
432 }
433 
434 static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
435 {
436 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SSE);
437 }
438 
439 static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
440 {
441 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SF);
442 }
443 
444 static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
445 {
446 	return (kvm_vcpu_get_esr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
447 }
448 
449 static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
450 {
451 	return esr_abt_is_s1ptw(kvm_vcpu_get_esr(vcpu));
452 }
453 
454 /* Always check for S1PTW *before* using this. */
455 static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
456 {
457 	return esr_dabt_is_write(kvm_vcpu_get_esr(vcpu));
458 }
459 
460 static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
461 {
462 	return 1 << ((kvm_vcpu_get_esr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
463 }
464 
465 /* This one is not specific to Data Abort */
466 static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
467 {
468 	return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_IL);
469 }
470 
471 static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
472 {
473 	return ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
474 }
475 
476 static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
477 {
478 	return esr_trap_is_iabt(kvm_vcpu_get_esr(vcpu));
479 }
480 
481 static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
482 {
483 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
484 }
485 
486 #endif /* ARM64_S390_COMMON == 1 */
487 
488 static __always_inline bool esr_abt_is_exec_fault(unsigned long esr)
489 {
490 	return esr_trap_is_iabt(esr) && !esr_abt_is_s1ptw(esr);
491 }
492 
493 static inline
494 bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
495 {
496 	return esr_fsc_is_permission_fault(kvm_vcpu_get_esr(vcpu));
497 }
498 
499 static inline
500 bool kvm_vcpu_trap_is_translation_fault(const struct kvm_vcpu *vcpu)
501 {
502 	return esr_fsc_is_translation_fault(kvm_vcpu_get_esr(vcpu));
503 }
504 
505 static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
506 {
507 	return esr_abt_is_sea(kvm_vcpu_get_esr(vcpu));
508 }
509 
510 static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
511 {
512 	u64 esr = kvm_vcpu_get_esr(vcpu);
513 	return ESR_ELx_SYS64_ISS_RT(esr);
514 }
515 
516 #if ARM64_S390_COMMON == 1
517 static inline bool esr_abt_is_write_fault(unsigned long esr)
518 {
519 	if (esr_abt_is_s1ptw(esr)) {
520 		/*
521 		 * Only a permission fault on a S1PTW should be
522 		 * considered as a write. Otherwise, page tables baked
523 		 * in a read-only memslot will result in an exception
524 		 * being delivered in the guest.
525 		 *
526 		 * The drawback is that we end-up faulting twice if the
527 		 * guest is using any of HW AF/DB: a translation fault
528 		 * to map the page containing the PT (read only at
529 		 * first), then a permission fault to allow the flags
530 		 * to be set.
531 		 */
532 		return esr_fsc_is_permission_fault(esr);
533 	}
534 
535 	if (esr_trap_is_iabt(esr))
536 		return false;
537 
538 	return esr_dabt_is_write(esr);
539 }
540 
541 static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
542 {
543 	return esr_abt_is_write_fault(kvm_vcpu_get_esr(vcpu));
544 }
545 
546 #endif /* ARM64_S390_COMMON == 1 */
547 
548 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
549 {
550 	return __vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
551 }
552 
553 /* In nVHE hyp code, registers are always in memory: use the raw accessors. */
554 #if defined(__KVM_NVHE_HYPERVISOR__)
555 #define vcpu_read_sys_reg(v, r)		__vcpu_sys_reg(v, r)
556 #define vcpu_write_sys_reg(v, x, r)	__vcpu_assign_sys_reg(v, r, x)
557 #endif
558 
559 static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
560 {
561 	if (vcpu_mode_is_32bit(vcpu)) {
562 		*vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT;
563 	} else {
564 		enum vcpu_sysreg r;
565 		u64 sctlr;
566 
567 		r = vcpu_has_nv(vcpu) ? SCTLR_EL2 : SCTLR_EL1;
568 
569 		sctlr = vcpu_read_sys_reg(vcpu, r);
570 		sctlr |= SCTLR_ELx_EE;
571 		vcpu_write_sys_reg(vcpu, sctlr, r);
572 	}
573 }
574 
575 static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
576 {
577 	enum vcpu_sysreg r;
578 	u64 bit;
579 
580 	if (vcpu_mode_is_32bit(vcpu))
581 		return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT);
582 
583 	r = is_hyp_ctxt(vcpu) ? SCTLR_EL2 : SCTLR_EL1;
584 	bit = vcpu_mode_priv(vcpu) ? SCTLR_ELx_EE : SCTLR_EL1_E0E;
585 
586 	return vcpu_read_sys_reg(vcpu, r) & bit;
587 }
588 
589 #if ARM64_S390_COMMON == 1
590 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
591 						    unsigned long data,
592 						    unsigned int len)
593 {
594 	if (kvm_vcpu_is_be(vcpu)) {
595 		switch (len) {
596 		case 1:
597 			return data & 0xff;
598 		case 2:
599 			return be16_to_cpu(data & 0xffff);
600 		case 4:
601 			return be32_to_cpu(data & 0xffffffff);
602 		default:
603 			return be64_to_cpu(data);
604 		}
605 	} else {
606 		switch (len) {
607 		case 1:
608 			return data & 0xff;
609 		case 2:
610 			return le16_to_cpu(data & 0xffff);
611 		case 4:
612 			return le32_to_cpu(data & 0xffffffff);
613 		default:
614 			return le64_to_cpu(data);
615 		}
616 	}
617 
618 	return data;		/* Leave LE untouched */
619 }
620 
621 static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
622 						    unsigned long data,
623 						    unsigned int len)
624 {
625 	if (kvm_vcpu_is_be(vcpu)) {
626 		switch (len) {
627 		case 1:
628 			return data & 0xff;
629 		case 2:
630 			return cpu_to_be16(data & 0xffff);
631 		case 4:
632 			return cpu_to_be32(data & 0xffffffff);
633 		default:
634 			return cpu_to_be64(data);
635 		}
636 	} else {
637 		switch (len) {
638 		case 1:
639 			return data & 0xff;
640 		case 2:
641 			return cpu_to_le16(data & 0xffff);
642 		case 4:
643 			return cpu_to_le32(data & 0xffffffff);
644 		default:
645 			return cpu_to_le64(data);
646 		}
647 	}
648 
649 	return data;		/* Leave LE untouched */
650 }
651 
652 static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
653 {
654 	WARN_ON(vcpu_get_flag(vcpu, PENDING_EXCEPTION));
655 	vcpu_set_flag(vcpu, INCREMENT_PC);
656 }
657 
658 #define kvm_pend_exception(v, e)					\
659 	do {								\
660 		WARN_ON(vcpu_get_flag((v), INCREMENT_PC));		\
661 		vcpu_set_flag((v), PENDING_EXCEPTION);			\
662 		vcpu_set_flag((v), e);					\
663 	} while (0)
664 
665 #endif /* ARM64_S390_COMMON == 1 */
666 
667 /*
668  * Returns a 'sanitised' view of CPTR_EL2, translating from nVHE to the VHE
669  * format if E2H isn't set.
670  */
671 static inline u64 vcpu_sanitised_cptr_el2(const struct kvm_vcpu *vcpu)
672 {
673 	u64 cptr = vcpu_read_sys_reg(vcpu, CPTR_EL2);
674 
675 	if (!vcpu_el2_e2h_is_set(vcpu))
676 		cptr = translate_cptr_el2_to_cpacr_el1(cptr);
677 
678 	return cptr;
679 }
680 
681 static inline bool ____cptr_xen_trap_enabled(const struct kvm_vcpu *vcpu,
682 					     unsigned int xen)
683 {
684 	switch (xen) {
685 	case 0b00:
686 	case 0b10:
687 		return true;
688 	case 0b01:
689 		return vcpu_el2_tge_is_set(vcpu) && !vcpu_is_el2(vcpu);
690 	case 0b11:
691 	default:
692 		return false;
693 	}
694 }
695 
696 #define __guest_hyp_cptr_xen_trap_enabled(vcpu, xen)				\
697 	(!vcpu_has_nv(vcpu) ? false :						\
698 	 ____cptr_xen_trap_enabled(vcpu,					\
699 				   SYS_FIELD_GET(CPACR_EL1, xen,		\
700 						 vcpu_sanitised_cptr_el2(vcpu))))
701 
702 static inline bool guest_hyp_fpsimd_traps_enabled(const struct kvm_vcpu *vcpu)
703 {
704 	return __guest_hyp_cptr_xen_trap_enabled(vcpu, FPEN);
705 }
706 
707 static inline bool guest_hyp_sve_traps_enabled(const struct kvm_vcpu *vcpu)
708 {
709 	return __guest_hyp_cptr_xen_trap_enabled(vcpu, ZEN);
710 }
711 
712 static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
713 {
714 	struct kvm *kvm = vcpu->kvm;
715 
716 	if (cpus_have_final_cap(ARM64_HAS_HCX)) {
717 		/*
718 		 * In general, all HCRX_EL2 bits are gated by a feature.
719 		 * The only reason we can set SMPME without checking any
720 		 * feature is that its effects are not directly observable
721 		 * from the guest.
722 		 */
723 		vcpu->arch.hcrx_el2 = HCRX_EL2_SMPME;
724 
725 		if (kvm_has_feat(kvm, ID_AA64ISAR2_EL1, MOPS, IMP))
726 			vcpu->arch.hcrx_el2 |= (HCRX_EL2_MSCEn | HCRX_EL2_MCE2);
727 
728 		if (kvm_has_tcr2(kvm))
729 			vcpu->arch.hcrx_el2 |= HCRX_EL2_TCR2En;
730 
731 		if (kvm_has_fpmr(kvm))
732 			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnFPM;
733 
734 		if (kvm_has_sctlr2(kvm))
735 			vcpu->arch.hcrx_el2 |= HCRX_EL2_SCTLR2En;
736 
737 		if (kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64))
738 			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnALS;
739 
740 		if (kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V))
741 			vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR;
742 
743 		/*
744 		 * NV3 is a host-specific extension, and we always use
745 		 * it when present and that the guest uses NV. It may
746 		 * be hidden from the guest though.
747 		 */
748 		if (cpus_have_final_cap(ARM64_HAS_NV3) &&
749 		    vcpu_has_nv(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
750 			vcpu->arch.hcrx_el2 |= HCRX_EL2_NVTGE;
751 
752 			/*
753 			 * If the guest is NV2-capable, then we need to see
754 			 * all the TLBIs, as configured in HCR_EL2.
755 			 * Otherwise, relax the TLBI traps to only TGE=0.
756 			 */
757 			if (!kvm_has_nv2(vcpu->kvm)) {
758 				vcpu->arch.hcrx_el2 |= (HCRX_EL2_NVnTTLB   |
759 							HCRX_EL2_NVnTTLBIS);
760 
761 				if (kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
762 					vcpu->arch.hcrx_el2 |= HCRX_EL2_NVnTTLBOS;
763 			}
764 		}
765 	}
766 }
767 
768 static inline void kvm_reset_fpsimd(struct kvm_vcpu *vcpu)
769 {
770 	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
771 }
772 
773 #if ARM64_S390_COMMON == 1
774 /* Reset a vcpu's core registers. */
775 static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
776 {
777 	u32 pstate;
778 
779 	if (vcpu_el1_is_32bit(vcpu))
780 		pstate = VCPU_RESET_PSTATE_SVC;
781 	else if (vcpu_has_nv(vcpu))
782 		pstate = VCPU_RESET_PSTATE_EL2;
783 	else
784 		pstate = VCPU_RESET_PSTATE_EL1;
785 
786 	/* Reset core registers */
787 	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
788 	*vcpu_pc(vcpu) = 0;
789 	*vcpu_sp_el0(vcpu) = 0;
790 	kvm_reset_fpsimd(vcpu);
791 	vcpu->arch.ctxt.spsr_abt = 0;
792 	vcpu->arch.ctxt.spsr_und = 0;
793 	vcpu->arch.ctxt.spsr_irq = 0;
794 	vcpu->arch.ctxt.spsr_fiq = 0;
795 	*vcpu_cpsr(vcpu) = pstate;
796 }
797 
798 #endif /* ARM64_S390_COMMON == 1 */
799 
800 /* PSCI reset handling for a vcpu. */
801 static inline void kvm_reset_vcpu_psci(struct kvm_vcpu *vcpu,
802 				       struct vcpu_reset_state *reset_state)
803 {
804 	unsigned long target_pc = reset_state->pc;
805 
806 	/* Gracefully handle Thumb2 entry point */
807 	if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
808 		target_pc &= ~1UL;
809 		vcpu_set_thumb(vcpu);
810 	}
811 
812 	/* Propagate caller endianness */
813 	if (reset_state->be)
814 		kvm_vcpu_set_be(vcpu);
815 
816 	*vcpu_pc(vcpu) = target_pc;
817 
818 	/*
819 	 * We may come from a state where either a PC update was
820 	 * pending (SMC call resulting in PC being increpented to
821 	 * skip the SMC) or a pending exception. Make sure we get
822 	 * rid of all that, as this cannot be valid out of reset.
823 	 *
824 	 * Note that clearing the exception mask also clears PC
825 	 * updates, but that's an implementation detail, and we
826 	 * really want to make it explicit.
827 	 */
828 	vcpu_clear_flag(vcpu, PENDING_EXCEPTION);
829 	vcpu_clear_flag(vcpu, EXCEPT_MASK);
830 	vcpu_clear_flag(vcpu, INCREMENT_PC);
831 	vcpu_set_reg(vcpu, 0, reset_state->r0);
832 }
833 
834 #endif /* __ARM64_KVM_EMULATE_H__ */
835