xref: /linux-6.15/arch/arm64/kvm/hyp/include/hyp/switch.h (revision f883f34b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 - ARM Ltd
4  * Author: Marc Zyngier <[email protected]>
5  */
6 
7 #ifndef __ARM64_KVM_HYP_SWITCH_H__
8 #define __ARM64_KVM_HYP_SWITCH_H__
9 
10 #include <hyp/adjust_pc.h>
11 #include <hyp/fault.h>
12 
13 #include <linux/arm-smccc.h>
14 #include <linux/kvm_host.h>
15 #include <linux/types.h>
16 #include <linux/jump_label.h>
17 #include <uapi/linux/psci.h>
18 
19 #include <kvm/arm_psci.h>
20 
21 #include <asm/barrier.h>
22 #include <asm/cpufeature.h>
23 #include <asm/extable.h>
24 #include <asm/kprobes.h>
25 #include <asm/kvm_asm.h>
26 #include <asm/kvm_emulate.h>
27 #include <asm/kvm_hyp.h>
28 #include <asm/kvm_mmu.h>
29 #include <asm/kvm_nested.h>
30 #include <asm/fpsimd.h>
31 #include <asm/debug-monitors.h>
32 #include <asm/processor.h>
33 #include <asm/traps.h>
34 
35 struct kvm_exception_table_entry {
36 	int insn, fixup;
37 };
38 
39 extern struct kvm_exception_table_entry __start___kvm_ex_table;
40 extern struct kvm_exception_table_entry __stop___kvm_ex_table;
41 
42 /* Save the 32-bit only FPSIMD system register state */
43 static inline void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
44 {
45 	if (!vcpu_el1_is_32bit(vcpu))
46 		return;
47 
48 	__vcpu_sys_reg(vcpu, FPEXC32_EL2) = read_sysreg(fpexc32_el2);
49 }
50 
51 static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
52 {
53 	/*
54 	 * We are about to set CPTR_EL2.TFP to trap all floating point
55 	 * register accesses to EL2, however, the ARM ARM clearly states that
56 	 * traps are only taken to EL2 if the operation would not otherwise
57 	 * trap to EL1.  Therefore, always make sure that for 32-bit guests,
58 	 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
59 	 * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
60 	 * it will cause an exception.
61 	 */
62 	if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
63 		write_sysreg(1 << 30, fpexc32_el2);
64 		isb();
65 	}
66 }
67 
68 #define compute_clr_set(vcpu, reg, clr, set)				\
69 	do {								\
70 		u64 hfg;						\
71 		hfg = __vcpu_sys_reg(vcpu, reg) & ~__ ## reg ## _RES0;	\
72 		set |= hfg & __ ## reg ## _MASK; 			\
73 		clr |= ~hfg & __ ## reg ## _nMASK; 			\
74 	} while(0)
75 
76 #define reg_to_fgt_group_id(reg)					\
77 	({								\
78 		enum fgt_group_id id;					\
79 		switch(reg) {						\
80 		case HFGRTR_EL2:					\
81 		case HFGWTR_EL2:					\
82 			id = HFGxTR_GROUP;				\
83 			break;						\
84 		case HFGITR_EL2:					\
85 			id = HFGITR_GROUP;				\
86 			break;						\
87 		case HDFGRTR_EL2:					\
88 		case HDFGWTR_EL2:					\
89 			id = HDFGRTR_GROUP;				\
90 			break;						\
91 		case HAFGRTR_EL2:					\
92 			id = HAFGRTR_GROUP;				\
93 			break;						\
94 		default:						\
95 			BUILD_BUG_ON(1);				\
96 		}							\
97 									\
98 		id;							\
99 	})
100 
101 #define compute_undef_clr_set(vcpu, kvm, reg, clr, set)			\
102 	do {								\
103 		u64 hfg = kvm->arch.fgu[reg_to_fgt_group_id(reg)];	\
104 		set |= hfg & __ ## reg ## _MASK;			\
105 		clr |= hfg & __ ## reg ## _nMASK; 			\
106 	} while(0)
107 
108 #define update_fgt_traps_cs(hctxt, vcpu, kvm, reg, clr, set)		\
109 	do {								\
110 		u64 c = 0, s = 0;					\
111 									\
112 		ctxt_sys_reg(hctxt, reg) = read_sysreg_s(SYS_ ## reg);	\
113 		if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))		\
114 			compute_clr_set(vcpu, reg, c, s);		\
115 									\
116 		compute_undef_clr_set(vcpu, kvm, reg, c, s);		\
117 									\
118 		s |= set;						\
119 		c |= clr;						\
120 		if (c || s) {						\
121 			u64 val = __ ## reg ## _nMASK;			\
122 			val |= s;					\
123 			val &= ~c;					\
124 			write_sysreg_s(val, SYS_ ## reg);		\
125 		}							\
126 	} while(0)
127 
128 #define update_fgt_traps(hctxt, vcpu, kvm, reg)		\
129 	update_fgt_traps_cs(hctxt, vcpu, kvm, reg, 0, 0)
130 
131 /*
132  * Validate the fine grain trap masks.
133  * Check that the masks do not overlap and that all bits are accounted for.
134  */
135 #define CHECK_FGT_MASKS(reg)							\
136 	do {									\
137 		BUILD_BUG_ON((__ ## reg ## _MASK) & (__ ## reg ## _nMASK));	\
138 		BUILD_BUG_ON(~((__ ## reg ## _RES0) ^ (__ ## reg ## _MASK) ^	\
139 			       (__ ## reg ## _nMASK)));				\
140 	} while(0)
141 
142 static inline bool cpu_has_amu(void)
143 {
144        u64 pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);
145 
146        return cpuid_feature_extract_unsigned_field(pfr0,
147                ID_AA64PFR0_EL1_AMU_SHIFT);
148 }
149 
150 static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
151 {
152 	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
153 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
154 
155 	CHECK_FGT_MASKS(HFGRTR_EL2);
156 	CHECK_FGT_MASKS(HFGWTR_EL2);
157 	CHECK_FGT_MASKS(HFGITR_EL2);
158 	CHECK_FGT_MASKS(HDFGRTR_EL2);
159 	CHECK_FGT_MASKS(HDFGWTR_EL2);
160 	CHECK_FGT_MASKS(HAFGRTR_EL2);
161 	CHECK_FGT_MASKS(HCRX_EL2);
162 
163 	if (!cpus_have_final_cap(ARM64_HAS_FGT))
164 		return;
165 
166 	update_fgt_traps(hctxt, vcpu, kvm, HFGRTR_EL2);
167 	update_fgt_traps_cs(hctxt, vcpu, kvm, HFGWTR_EL2, 0,
168 			    cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) ?
169 			    HFGxTR_EL2_TCR_EL1_MASK : 0);
170 	update_fgt_traps(hctxt, vcpu, kvm, HFGITR_EL2);
171 	update_fgt_traps(hctxt, vcpu, kvm, HDFGRTR_EL2);
172 	update_fgt_traps(hctxt, vcpu, kvm, HDFGWTR_EL2);
173 
174 	if (cpu_has_amu())
175 		update_fgt_traps(hctxt, vcpu, kvm, HAFGRTR_EL2);
176 }
177 
178 #define __deactivate_fgt(htcxt, vcpu, kvm, reg)				\
179 	do {								\
180 		if ((vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) ||	\
181 		    kvm->arch.fgu[reg_to_fgt_group_id(reg)])		\
182 			write_sysreg_s(ctxt_sys_reg(hctxt, reg),	\
183 				       SYS_ ## reg);			\
184 	} while(0)
185 
186 static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
187 {
188 	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
189 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
190 
191 	if (!cpus_have_final_cap(ARM64_HAS_FGT))
192 		return;
193 
194 	__deactivate_fgt(hctxt, vcpu, kvm, HFGRTR_EL2);
195 	if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38))
196 		write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2);
197 	else
198 		__deactivate_fgt(hctxt, vcpu, kvm, HFGWTR_EL2);
199 	__deactivate_fgt(hctxt, vcpu, kvm, HFGITR_EL2);
200 	__deactivate_fgt(hctxt, vcpu, kvm, HDFGRTR_EL2);
201 	__deactivate_fgt(hctxt, vcpu, kvm, HDFGWTR_EL2);
202 
203 	if (cpu_has_amu())
204 		__deactivate_fgt(hctxt, vcpu, kvm, HAFGRTR_EL2);
205 }
206 
207 static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
208 {
209 	u64 r = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
210 
211 	if (!system_supports_mpam())
212 		return;
213 
214 	/* trap guest access to MPAMIDR_EL1 */
215 	if (system_supports_mpam_hcr()) {
216 		write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);
217 	} else {
218 		/* From v1.1 TIDR can trap MPAMIDR, set it unconditionally */
219 		r |= MPAM2_EL2_TIDR;
220 	}
221 
222 	write_sysreg_s(r, SYS_MPAM2_EL2);
223 }
224 
225 static inline void __deactivate_traps_mpam(void)
226 {
227 	if (!system_supports_mpam())
228 		return;
229 
230 	write_sysreg_s(0, SYS_MPAM2_EL2);
231 
232 	if (system_supports_mpam_hcr())
233 		write_sysreg_s(MPAMHCR_HOST_FLAGS, SYS_MPAMHCR_EL2);
234 }
235 
236 static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
237 {
238 	/* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
239 	write_sysreg(1 << 15, hstr_el2);
240 
241 	/*
242 	 * Make sure we trap PMU access from EL0 to EL2. Also sanitize
243 	 * PMSELR_EL0 to make sure it never contains the cycle
244 	 * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
245 	 * EL1 instead of being trapped to EL2.
246 	 */
247 	if (kvm_arm_support_pmu_v3()) {
248 		struct kvm_cpu_context *hctxt;
249 
250 		write_sysreg(0, pmselr_el0);
251 
252 		hctxt = host_data_ptr(host_ctxt);
253 		ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
254 		write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
255 		vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
256 	}
257 
258 	*host_data_ptr(host_debug_state.mdcr_el2) = read_sysreg(mdcr_el2);
259 	write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
260 
261 	if (cpus_have_final_cap(ARM64_HAS_HCX)) {
262 		u64 hcrx = vcpu->arch.hcrx_el2;
263 		if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
264 			u64 clr = 0, set = 0;
265 
266 			compute_clr_set(vcpu, HCRX_EL2, clr, set);
267 
268 			hcrx |= set;
269 			hcrx &= ~clr;
270 		}
271 
272 		write_sysreg_s(hcrx, SYS_HCRX_EL2);
273 	}
274 
275 	__activate_traps_hfgxtr(vcpu);
276 	__activate_traps_mpam(vcpu);
277 }
278 
279 static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
280 {
281 	write_sysreg(*host_data_ptr(host_debug_state.mdcr_el2), mdcr_el2);
282 
283 	write_sysreg(0, hstr_el2);
284 	if (kvm_arm_support_pmu_v3()) {
285 		struct kvm_cpu_context *hctxt;
286 
287 		hctxt = host_data_ptr(host_ctxt);
288 		write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
289 		vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
290 	}
291 
292 	if (cpus_have_final_cap(ARM64_HAS_HCX))
293 		write_sysreg_s(HCRX_HOST_FLAGS, SYS_HCRX_EL2);
294 
295 	__deactivate_traps_hfgxtr(vcpu);
296 	__deactivate_traps_mpam();
297 }
298 
299 static inline void ___activate_traps(struct kvm_vcpu *vcpu, u64 hcr)
300 {
301 	if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM))
302 		hcr |= HCR_TVM;
303 
304 	write_sysreg(hcr, hcr_el2);
305 
306 	if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
307 		write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
308 }
309 
310 static inline void ___deactivate_traps(struct kvm_vcpu *vcpu)
311 {
312 	/*
313 	 * If we pended a virtual abort, preserve it until it gets
314 	 * cleared. See D1.14.3 (Virtual Interrupts) for details, but
315 	 * the crucial bit is "On taking a vSError interrupt,
316 	 * HCR_EL2.VSE is cleared to 0."
317 	 */
318 	if (vcpu->arch.hcr_el2 & HCR_VSE) {
319 		vcpu->arch.hcr_el2 &= ~HCR_VSE;
320 		vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE;
321 	}
322 }
323 
324 static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)
325 {
326 	return __get_fault_info(vcpu->arch.fault.esr_el2, &vcpu->arch.fault);
327 }
328 
329 static bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code)
330 {
331 	*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
332 	arm64_mops_reset_regs(vcpu_gp_regs(vcpu), vcpu->arch.fault.esr_el2);
333 	write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
334 
335 	/*
336 	 * Finish potential single step before executing the prologue
337 	 * instruction.
338 	 */
339 	*vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
340 	write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
341 
342 	return true;
343 }
344 
345 static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
346 {
347 	/*
348 	 * The vCPU's saved SVE state layout always matches the max VL of the
349 	 * vCPU. Start off with the max VL so we can load the SVE state.
350 	 */
351 	sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2);
352 	__sve_restore_state(vcpu_sve_pffr(vcpu),
353 			    &vcpu->arch.ctxt.fp_regs.fpsr,
354 			    true);
355 
356 	/*
357 	 * The effective VL for a VM could differ from the max VL when running a
358 	 * nested guest, as the guest hypervisor could select a smaller VL. Slap
359 	 * that into hardware before wrapping up.
360 	 */
361 	if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))
362 		sve_cond_update_zcr_vq(__vcpu_sys_reg(vcpu, ZCR_EL2), SYS_ZCR_EL2);
363 
364 	write_sysreg_el1(__vcpu_sys_reg(vcpu, vcpu_sve_zcr_elx(vcpu)), SYS_ZCR);
365 }
366 
367 static inline void __hyp_sve_save_host(void)
368 {
369 	struct cpu_sve_state *sve_state = *host_data_ptr(sve_state);
370 
371 	sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR);
372 	write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
373 	__sve_save_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl),
374 			 &sve_state->fpsr,
375 			 true);
376 }
377 
378 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu);
379 
380 /*
381  * We trap the first access to the FP/SIMD to save the host context and
382  * restore the guest context lazily.
383  * If FP/SIMD is not implemented, handle the trap and inject an undefined
384  * instruction exception to the guest. Similarly for trapped SVE accesses.
385  */
386 static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
387 {
388 	bool sve_guest;
389 	u8 esr_ec;
390 
391 	if (!system_supports_fpsimd())
392 		return false;
393 
394 	sve_guest = vcpu_has_sve(vcpu);
395 	esr_ec = kvm_vcpu_trap_get_class(vcpu);
396 
397 	/* Only handle traps the vCPU can support here: */
398 	switch (esr_ec) {
399 	case ESR_ELx_EC_FP_ASIMD:
400 		/* Forward traps to the guest hypervisor as required */
401 		if (guest_hyp_fpsimd_traps_enabled(vcpu))
402 			return false;
403 		break;
404 	case ESR_ELx_EC_SYS64:
405 		if (WARN_ON_ONCE(!is_hyp_ctxt(vcpu)))
406 			return false;
407 		fallthrough;
408 	case ESR_ELx_EC_SVE:
409 		if (!sve_guest)
410 			return false;
411 		if (guest_hyp_sve_traps_enabled(vcpu))
412 			return false;
413 		break;
414 	default:
415 		return false;
416 	}
417 
418 	/* Valid trap.  Switch the context: */
419 
420 	/* First disable enough traps to allow us to update the registers */
421 	if (sve_guest || (is_protected_kvm_enabled() && system_supports_sve()))
422 		cpacr_clear_set(0, CPACR_EL1_FPEN | CPACR_EL1_ZEN);
423 	else
424 		cpacr_clear_set(0, CPACR_EL1_FPEN);
425 	isb();
426 
427 	/* Write out the host state if it's in the registers */
428 	if (host_owns_fp_regs())
429 		kvm_hyp_save_fpsimd_host(vcpu);
430 
431 	/* Restore the guest state */
432 	if (sve_guest)
433 		__hyp_sve_restore_guest(vcpu);
434 	else
435 		__fpsimd_restore_state(&vcpu->arch.ctxt.fp_regs);
436 
437 	if (kvm_has_fpmr(kern_hyp_va(vcpu->kvm)))
438 		write_sysreg_s(__vcpu_sys_reg(vcpu, FPMR), SYS_FPMR);
439 
440 	/* Skip restoring fpexc32 for AArch64 guests */
441 	if (!(read_sysreg(hcr_el2) & HCR_RW))
442 		write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
443 
444 	*host_data_ptr(fp_owner) = FP_STATE_GUEST_OWNED;
445 
446 	return true;
447 }
448 
449 static inline bool handle_tx2_tvm(struct kvm_vcpu *vcpu)
450 {
451 	u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
452 	int rt = kvm_vcpu_sys_get_rt(vcpu);
453 	u64 val = vcpu_get_reg(vcpu, rt);
454 
455 	/*
456 	 * The normal sysreg handling code expects to see the traps,
457 	 * let's not do anything here.
458 	 */
459 	if (vcpu->arch.hcr_el2 & HCR_TVM)
460 		return false;
461 
462 	switch (sysreg) {
463 	case SYS_SCTLR_EL1:
464 		write_sysreg_el1(val, SYS_SCTLR);
465 		break;
466 	case SYS_TTBR0_EL1:
467 		write_sysreg_el1(val, SYS_TTBR0);
468 		break;
469 	case SYS_TTBR1_EL1:
470 		write_sysreg_el1(val, SYS_TTBR1);
471 		break;
472 	case SYS_TCR_EL1:
473 		write_sysreg_el1(val, SYS_TCR);
474 		break;
475 	case SYS_ESR_EL1:
476 		write_sysreg_el1(val, SYS_ESR);
477 		break;
478 	case SYS_FAR_EL1:
479 		write_sysreg_el1(val, SYS_FAR);
480 		break;
481 	case SYS_AFSR0_EL1:
482 		write_sysreg_el1(val, SYS_AFSR0);
483 		break;
484 	case SYS_AFSR1_EL1:
485 		write_sysreg_el1(val, SYS_AFSR1);
486 		break;
487 	case SYS_MAIR_EL1:
488 		write_sysreg_el1(val, SYS_MAIR);
489 		break;
490 	case SYS_AMAIR_EL1:
491 		write_sysreg_el1(val, SYS_AMAIR);
492 		break;
493 	case SYS_CONTEXTIDR_EL1:
494 		write_sysreg_el1(val, SYS_CONTEXTIDR);
495 		break;
496 	default:
497 		return false;
498 	}
499 
500 	__kvm_skip_instr(vcpu);
501 	return true;
502 }
503 
504 static inline u64 compute_counter_value(struct arch_timer_context *ctxt)
505 {
506 	return arch_timer_read_cntpct_el0() - timer_get_offset(ctxt);
507 }
508 
509 static bool kvm_handle_cntxct(struct kvm_vcpu *vcpu)
510 {
511 	struct arch_timer_context *ctxt;
512 	u32 sysreg;
513 	u64 val;
514 
515 	/*
516 	 * We only get here for 64bit guests, 32bit guests will hit
517 	 * the long and winding road all the way to the standard
518 	 * handling. Yes, it sucks to be irrelevant.
519 	 *
520 	 * Also, we only deal with non-hypervisor context here (either
521 	 * an EL1 guest, or a non-HYP context of an EL2 guest).
522 	 */
523 	if (is_hyp_ctxt(vcpu))
524 		return false;
525 
526 	sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
527 
528 	switch (sysreg) {
529 	case SYS_CNTPCT_EL0:
530 	case SYS_CNTPCTSS_EL0:
531 		if (vcpu_has_nv(vcpu)) {
532 			/* Check for guest hypervisor trapping */
533 			val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2);
534 			if (!vcpu_el2_e2h_is_set(vcpu))
535 				val = (val & CNTHCTL_EL1PCTEN) << 10;
536 
537 			if (!(val & (CNTHCTL_EL1PCTEN << 10)))
538 				return false;
539 		}
540 
541 		ctxt = vcpu_ptimer(vcpu);
542 		break;
543 	case SYS_CNTVCT_EL0:
544 	case SYS_CNTVCTSS_EL0:
545 		if (vcpu_has_nv(vcpu)) {
546 			/* Check for guest hypervisor trapping */
547 			val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2);
548 
549 			if (val & CNTHCTL_EL1TVCT)
550 				return false;
551 		}
552 
553 		ctxt = vcpu_vtimer(vcpu);
554 		break;
555 	default:
556 		return false;
557 	}
558 
559 	val = compute_counter_value(ctxt);
560 
561 	vcpu_set_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu), val);
562 	__kvm_skip_instr(vcpu);
563 	return true;
564 }
565 
566 static bool handle_ampere1_tcr(struct kvm_vcpu *vcpu)
567 {
568 	u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
569 	int rt = kvm_vcpu_sys_get_rt(vcpu);
570 	u64 val = vcpu_get_reg(vcpu, rt);
571 
572 	if (sysreg != SYS_TCR_EL1)
573 		return false;
574 
575 	/*
576 	 * Affected parts do not advertise support for hardware Access Flag /
577 	 * Dirty state management in ID_AA64MMFR1_EL1.HAFDBS, but the underlying
578 	 * control bits are still functional. The architecture requires these be
579 	 * RES0 on systems that do not implement FEAT_HAFDBS.
580 	 *
581 	 * Uphold the requirements of the architecture by masking guest writes
582 	 * to TCR_EL1.{HA,HD} here.
583 	 */
584 	val &= ~(TCR_HD | TCR_HA);
585 	write_sysreg_el1(val, SYS_TCR);
586 	__kvm_skip_instr(vcpu);
587 	return true;
588 }
589 
590 static bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
591 {
592 	if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
593 	    handle_tx2_tvm(vcpu))
594 		return true;
595 
596 	if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) &&
597 	    handle_ampere1_tcr(vcpu))
598 		return true;
599 
600 	if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
601 	    __vgic_v3_perform_cpuif_access(vcpu) == 1)
602 		return true;
603 
604 	if (kvm_handle_cntxct(vcpu))
605 		return true;
606 
607 	return false;
608 }
609 
610 static bool kvm_hyp_handle_cp15_32(struct kvm_vcpu *vcpu, u64 *exit_code)
611 {
612 	if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
613 	    __vgic_v3_perform_cpuif_access(vcpu) == 1)
614 		return true;
615 
616 	return false;
617 }
618 
619 static bool kvm_hyp_handle_memory_fault(struct kvm_vcpu *vcpu, u64 *exit_code)
620 {
621 	if (!__populate_fault_info(vcpu))
622 		return true;
623 
624 	return false;
625 }
626 static bool kvm_hyp_handle_iabt_low(struct kvm_vcpu *vcpu, u64 *exit_code)
627 	__alias(kvm_hyp_handle_memory_fault);
628 static bool kvm_hyp_handle_watchpt_low(struct kvm_vcpu *vcpu, u64 *exit_code)
629 	__alias(kvm_hyp_handle_memory_fault);
630 
631 static bool kvm_hyp_handle_dabt_low(struct kvm_vcpu *vcpu, u64 *exit_code)
632 {
633 	if (kvm_hyp_handle_memory_fault(vcpu, exit_code))
634 		return true;
635 
636 	if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
637 		bool valid;
638 
639 		valid = kvm_vcpu_trap_is_translation_fault(vcpu) &&
640 			kvm_vcpu_dabt_isvalid(vcpu) &&
641 			!kvm_vcpu_abt_issea(vcpu) &&
642 			!kvm_vcpu_abt_iss1tw(vcpu);
643 
644 		if (valid) {
645 			int ret = __vgic_v2_perform_cpuif_access(vcpu);
646 
647 			if (ret == 1)
648 				return true;
649 
650 			/* Promote an illegal access to an SError.*/
651 			if (ret == -1)
652 				*exit_code = ARM_EXCEPTION_EL1_SERROR;
653 		}
654 	}
655 
656 	return false;
657 }
658 
659 typedef bool (*exit_handler_fn)(struct kvm_vcpu *, u64 *);
660 
661 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu);
662 
663 static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code);
664 
665 /*
666  * Allow the hypervisor to handle the exit with an exit handler if it has one.
667  *
668  * Returns true if the hypervisor handled the exit, and control should go back
669  * to the guest, or false if it hasn't.
670  */
671 static inline bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
672 {
673 	const exit_handler_fn *handlers = kvm_get_exit_handler_array(vcpu);
674 	exit_handler_fn fn;
675 
676 	fn = handlers[kvm_vcpu_trap_get_class(vcpu)];
677 
678 	if (fn)
679 		return fn(vcpu, exit_code);
680 
681 	return false;
682 }
683 
684 static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu, u64 *exit_code)
685 {
686 	/*
687 	 * Check for the conditions of Cortex-A510's #2077057. When these occur
688 	 * SPSR_EL2 can't be trusted, but isn't needed either as it is
689 	 * unchanged from the value in vcpu_gp_regs(vcpu)->pstate.
690 	 * Are we single-stepping the guest, and took a PAC exception from the
691 	 * active-not-pending state?
692 	 */
693 	if (cpus_have_final_cap(ARM64_WORKAROUND_2077057)		&&
694 	    vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP			&&
695 	    *vcpu_cpsr(vcpu) & DBG_SPSR_SS				&&
696 	    ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) == ESR_ELx_EC_PAC)
697 		write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
698 
699 	vcpu->arch.ctxt.regs.pstate = read_sysreg_el2(SYS_SPSR);
700 }
701 
702 /*
703  * Return true when we were able to fixup the guest exit and should return to
704  * the guest, false when we should restore the host state and return to the
705  * main run loop.
706  */
707 static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
708 {
709 	/*
710 	 * Save PSTATE early so that we can evaluate the vcpu mode
711 	 * early on.
712 	 */
713 	synchronize_vcpu_pstate(vcpu, exit_code);
714 
715 	/*
716 	 * Check whether we want to repaint the state one way or
717 	 * another.
718 	 */
719 	early_exit_filter(vcpu, exit_code);
720 
721 	if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
722 		vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR);
723 
724 	if (ARM_SERROR_PENDING(*exit_code) &&
725 	    ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ) {
726 		u8 esr_ec = kvm_vcpu_trap_get_class(vcpu);
727 
728 		/*
729 		 * HVC already have an adjusted PC, which we need to
730 		 * correct in order to return to after having injected
731 		 * the SError.
732 		 *
733 		 * SMC, on the other hand, is *trapped*, meaning its
734 		 * preferred return address is the SMC itself.
735 		 */
736 		if (esr_ec == ESR_ELx_EC_HVC32 || esr_ec == ESR_ELx_EC_HVC64)
737 			write_sysreg_el2(read_sysreg_el2(SYS_ELR) - 4, SYS_ELR);
738 	}
739 
740 	/*
741 	 * We're using the raw exception code in order to only process
742 	 * the trap if no SError is pending. We will come back to the
743 	 * same PC once the SError has been injected, and replay the
744 	 * trapping instruction.
745 	 */
746 	if (*exit_code != ARM_EXCEPTION_TRAP)
747 		goto exit;
748 
749 	/* Check if there's an exit handler and allow it to handle the exit. */
750 	if (kvm_hyp_handle_exit(vcpu, exit_code))
751 		goto guest;
752 exit:
753 	/* Return to the host kernel and handle the exit */
754 	return false;
755 
756 guest:
757 	/* Re-enter the guest */
758 	asm(ALTERNATIVE("nop", "dmb sy", ARM64_WORKAROUND_1508412));
759 	return true;
760 }
761 
762 static inline void __kvm_unexpected_el2_exception(void)
763 {
764 	extern char __guest_exit_restore_elr_and_panic[];
765 	unsigned long addr, fixup;
766 	struct kvm_exception_table_entry *entry, *end;
767 	unsigned long elr_el2 = read_sysreg(elr_el2);
768 
769 	entry = &__start___kvm_ex_table;
770 	end = &__stop___kvm_ex_table;
771 
772 	while (entry < end) {
773 		addr = (unsigned long)&entry->insn + entry->insn;
774 		fixup = (unsigned long)&entry->fixup + entry->fixup;
775 
776 		if (addr != elr_el2) {
777 			entry++;
778 			continue;
779 		}
780 
781 		write_sysreg(fixup, elr_el2);
782 		return;
783 	}
784 
785 	/* Trigger a panic after restoring the hyp context. */
786 	this_cpu_ptr(&kvm_hyp_ctxt)->sys_regs[ELR_EL2] = elr_el2;
787 	write_sysreg(__guest_exit_restore_elr_and_panic, elr_el2);
788 }
789 
790 #endif /* __ARM64_KVM_HYP_SWITCH_H__ */
791