xref: /linux/arch/arm64/kvm/hyp/vhe/switch.c (revision 1b30456150e57a79e300b82eb2efac40c25a162e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #include <hyp/switch.h>
8 
9 #include <linux/arm-smccc.h>
10 #include <linux/kvm_host.h>
11 #include <linux/types.h>
12 #include <linux/jump_label.h>
13 #include <linux/percpu.h>
14 #include <uapi/linux/psci.h>
15 
16 #include <kvm/arm_psci.h>
17 
18 #include <asm/barrier.h>
19 #include <asm/cpufeature.h>
20 #include <asm/kprobes.h>
21 #include <asm/kvm_asm.h>
22 #include <asm/kvm_emulate.h>
23 #include <asm/kvm_hyp.h>
24 #include <asm/kvm_mmu.h>
25 #include <asm/fpsimd.h>
26 #include <asm/debug-monitors.h>
27 #include <asm/processor.h>
28 #include <asm/thread_info.h>
29 #include <asm/vectors.h>
30 
31 /* VHE specific context */
32 DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
33 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
34 DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
35 
36 /*
37  * HCR_EL2 bits that the NV guest can freely change (no RES0/RES1
38  * semantics, irrespective of the configuration), but that cannot be
39  * applied to the actual HW as things would otherwise break badly.
40  *
41  * - TGE: we want the guest to use EL1, which is incompatible with
42  *   this bit being set
43  *
44  * - API/APK: they are already accounted for by vcpu_load(), and can
45  *   only take effect across a load/put cycle (such as ERET)
46  */
47 #define NV_HCR_GUEST_EXCLUDE	(HCR_TGE | HCR_API | HCR_APK)
48 
49 static u64 __compute_hcr(struct kvm_vcpu *vcpu)
50 {
51 	u64 hcr = vcpu->arch.hcr_el2;
52 
53 	if (!vcpu_has_nv(vcpu))
54 		return hcr;
55 
56 	if (is_hyp_ctxt(vcpu)) {
57 		hcr |= HCR_NV | HCR_NV2 | HCR_AT | HCR_TTLB;
58 
59 		if (!vcpu_el2_e2h_is_set(vcpu))
60 			hcr |= HCR_NV1;
61 
62 		write_sysreg_s(vcpu->arch.ctxt.vncr_array, SYS_VNCR_EL2);
63 	}
64 
65 	return hcr | (__vcpu_sys_reg(vcpu, HCR_EL2) & ~NV_HCR_GUEST_EXCLUDE);
66 }
67 
68 static void __activate_cptr_traps(struct kvm_vcpu *vcpu)
69 {
70 	u64 cptr;
71 
72 	/*
73 	 * With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to
74 	 * CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2,
75 	 * except for some missing controls, such as TAM.
76 	 * In this case, CPTR_EL2.TAM has the same position with or without
77 	 * VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM
78 	 * shift value for trapping the AMU accesses.
79 	 */
80 	u64 val = CPACR_EL1_TTA | CPTR_EL2_TAM;
81 
82 	if (guest_owns_fp_regs()) {
83 		val |= CPACR_EL1_FPEN;
84 		if (vcpu_has_sve(vcpu))
85 			val |= CPACR_EL1_ZEN;
86 	} else {
87 		__activate_traps_fpsimd32(vcpu);
88 	}
89 
90 	if (!vcpu_has_nv(vcpu))
91 		goto write;
92 
93 	/*
94 	 * The architecture is a bit crap (what a surprise): an EL2 guest
95 	 * writing to CPTR_EL2 via CPACR_EL1 can't set any of TCPAC or TTA,
96 	 * as they are RES0 in the guest's view. To work around it, trap the
97 	 * sucker using the very same bit it can't set...
98 	 */
99 	if (vcpu_el2_e2h_is_set(vcpu) && is_hyp_ctxt(vcpu))
100 		val |= CPTR_EL2_TCPAC;
101 
102 	/*
103 	 * Layer the guest hypervisor's trap configuration on top of our own if
104 	 * we're in a nested context.
105 	 */
106 	if (is_hyp_ctxt(vcpu))
107 		goto write;
108 
109 	cptr = vcpu_sanitised_cptr_el2(vcpu);
110 
111 	/*
112 	 * Pay attention, there's some interesting detail here.
113 	 *
114 	 * The CPTR_EL2.xEN fields are 2 bits wide, although there are only two
115 	 * meaningful trap states when HCR_EL2.TGE = 0 (running a nested guest):
116 	 *
117 	 *  - CPTR_EL2.xEN = x0, traps are enabled
118 	 *  - CPTR_EL2.xEN = x1, traps are disabled
119 	 *
120 	 * In other words, bit[0] determines if guest accesses trap or not. In
121 	 * the interest of simplicity, clear the entire field if the guest
122 	 * hypervisor has traps enabled to dispel any illusion of something more
123 	 * complicated taking place.
124 	 */
125 	if (!(SYS_FIELD_GET(CPACR_EL1, FPEN, cptr) & BIT(0)))
126 		val &= ~CPACR_EL1_FPEN;
127 	if (!(SYS_FIELD_GET(CPACR_EL1, ZEN, cptr) & BIT(0)))
128 		val &= ~CPACR_EL1_ZEN;
129 
130 	if (kvm_has_feat(vcpu->kvm, ID_AA64MMFR3_EL1, S2POE, IMP))
131 		val |= cptr & CPACR_EL1_E0POE;
132 
133 	val |= cptr & CPTR_EL2_TCPAC;
134 
135 write:
136 	write_sysreg(val, cpacr_el1);
137 }
138 
139 static void __activate_traps(struct kvm_vcpu *vcpu)
140 {
141 	u64 val;
142 
143 	___activate_traps(vcpu, __compute_hcr(vcpu));
144 
145 	if (has_cntpoff()) {
146 		struct timer_map map;
147 
148 		get_timer_map(vcpu, &map);
149 
150 		/*
151 		 * We're entrering the guest. Reload the correct
152 		 * values from memory now that TGE is clear.
153 		 */
154 		if (map.direct_ptimer == vcpu_ptimer(vcpu))
155 			val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
156 		if (map.direct_ptimer == vcpu_hptimer(vcpu))
157 			val = __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2);
158 
159 		if (map.direct_ptimer) {
160 			write_sysreg_el0(val, SYS_CNTP_CVAL);
161 			isb();
162 		}
163 	}
164 
165 	__activate_cptr_traps(vcpu);
166 
167 	write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1);
168 }
169 NOKPROBE_SYMBOL(__activate_traps);
170 
171 static void __deactivate_traps(struct kvm_vcpu *vcpu)
172 {
173 	const char *host_vectors = vectors;
174 
175 	___deactivate_traps(vcpu);
176 
177 	write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
178 
179 	if (has_cntpoff()) {
180 		struct timer_map map;
181 		u64 val, offset;
182 
183 		get_timer_map(vcpu, &map);
184 
185 		/*
186 		 * We're exiting the guest. Save the latest CVAL value
187 		 * to memory and apply the offset now that TGE is set.
188 		 */
189 		val = read_sysreg_el0(SYS_CNTP_CVAL);
190 		if (map.direct_ptimer == vcpu_ptimer(vcpu))
191 			__vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = val;
192 		if (map.direct_ptimer == vcpu_hptimer(vcpu))
193 			__vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2) = val;
194 
195 		offset = read_sysreg_s(SYS_CNTPOFF_EL2);
196 
197 		if (map.direct_ptimer && offset) {
198 			write_sysreg_el0(val + offset, SYS_CNTP_CVAL);
199 			isb();
200 		}
201 	}
202 
203 	/*
204 	 * ARM errata 1165522 and 1530923 require the actual execution of the
205 	 * above before we can switch to the EL2/EL0 translation regime used by
206 	 * the host.
207 	 */
208 	asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
209 
210 	kvm_reset_cptr_el2(vcpu);
211 
212 	if (!arm64_kernel_unmapped_at_el0())
213 		host_vectors = __this_cpu_read(this_cpu_vector);
214 	write_sysreg(host_vectors, vbar_el1);
215 }
216 NOKPROBE_SYMBOL(__deactivate_traps);
217 
218 /*
219  * Disable IRQs in __vcpu_{load,put}_{activate,deactivate}_traps() to
220  * prevent a race condition between context switching of PMUSERENR_EL0
221  * in __{activate,deactivate}_traps_common() and IPIs that attempts to
222  * update PMUSERENR_EL0. See also kvm_set_pmuserenr().
223  */
224 static void __vcpu_load_activate_traps(struct kvm_vcpu *vcpu)
225 {
226 	unsigned long flags;
227 
228 	local_irq_save(flags);
229 	__activate_traps_common(vcpu);
230 	local_irq_restore(flags);
231 }
232 
233 static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
234 {
235 	unsigned long flags;
236 
237 	local_irq_save(flags);
238 	__deactivate_traps_common(vcpu);
239 	local_irq_restore(flags);
240 }
241 
242 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
243 {
244 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
245 
246 	__vcpu_load_switch_sysregs(vcpu);
247 	__vcpu_load_activate_traps(vcpu);
248 	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
249 }
250 
251 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
252 {
253 	__vcpu_put_deactivate_traps(vcpu);
254 	__vcpu_put_switch_sysregs(vcpu);
255 
256 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = NULL;
257 }
258 
259 static u64 compute_emulated_cntx_ctl_el0(struct kvm_vcpu *vcpu,
260 					 enum vcpu_sysreg reg)
261 {
262 	unsigned long ctl;
263 	u64 cval, cnt;
264 	bool stat;
265 
266 	switch (reg) {
267 	case CNTP_CTL_EL0:
268 		cval = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
269 		ctl  = __vcpu_sys_reg(vcpu, CNTP_CTL_EL0);
270 		cnt  = compute_counter_value(vcpu_ptimer(vcpu));
271 		break;
272 	case CNTV_CTL_EL0:
273 		cval = __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
274 		ctl  = __vcpu_sys_reg(vcpu, CNTV_CTL_EL0);
275 		cnt  = compute_counter_value(vcpu_vtimer(vcpu));
276 		break;
277 	default:
278 		BUG();
279 	}
280 
281 	stat = cval <= cnt;
282 	__assign_bit(__ffs(ARCH_TIMER_CTRL_IT_STAT), &ctl, stat);
283 
284 	return ctl;
285 }
286 
287 static bool kvm_hyp_handle_timer(struct kvm_vcpu *vcpu, u64 *exit_code)
288 {
289 	u64 esr, val;
290 
291 	/*
292 	 * Having FEAT_ECV allows for a better quality of timer emulation.
293 	 * However, this comes at a huge cost in terms of traps. Try and
294 	 * satisfy the reads from guest's hypervisor context without
295 	 * returning to the kernel if we can.
296 	 */
297 	if (!is_hyp_ctxt(vcpu))
298 		return false;
299 
300 	esr = kvm_vcpu_get_esr(vcpu);
301 	if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) != ESR_ELx_SYS64_ISS_DIR_READ)
302 		return false;
303 
304 	switch (esr_sys64_to_sysreg(esr)) {
305 	case SYS_CNTP_CTL_EL02:
306 		val = compute_emulated_cntx_ctl_el0(vcpu, CNTP_CTL_EL0);
307 		break;
308 	case SYS_CNTP_CTL_EL0:
309 		if (vcpu_el2_e2h_is_set(vcpu))
310 			val = read_sysreg_el0(SYS_CNTP_CTL);
311 		else
312 			val = compute_emulated_cntx_ctl_el0(vcpu, CNTP_CTL_EL0);
313 		break;
314 	case SYS_CNTP_CVAL_EL02:
315 		val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
316 		break;
317 	case SYS_CNTP_CVAL_EL0:
318 		if (vcpu_el2_e2h_is_set(vcpu)) {
319 			val = read_sysreg_el0(SYS_CNTP_CVAL);
320 
321 			if (!has_cntpoff())
322 				val -= timer_get_offset(vcpu_hptimer(vcpu));
323 		} else {
324 			val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
325 		}
326 		break;
327 	case SYS_CNTPCT_EL0:
328 	case SYS_CNTPCTSS_EL0:
329 		val = compute_counter_value(vcpu_hptimer(vcpu));
330 		break;
331 	case SYS_CNTV_CTL_EL02:
332 		val = compute_emulated_cntx_ctl_el0(vcpu, CNTV_CTL_EL0);
333 		break;
334 	case SYS_CNTV_CTL_EL0:
335 		if (vcpu_el2_e2h_is_set(vcpu))
336 			val = read_sysreg_el0(SYS_CNTV_CTL);
337 		else
338 			val = compute_emulated_cntx_ctl_el0(vcpu, CNTV_CTL_EL0);
339 		break;
340 	case SYS_CNTV_CVAL_EL02:
341 		val = __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
342 		break;
343 	case SYS_CNTV_CVAL_EL0:
344 		if (vcpu_el2_e2h_is_set(vcpu))
345 			val = read_sysreg_el0(SYS_CNTV_CVAL);
346 		else
347 			val = __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
348 		break;
349 	case SYS_CNTVCT_EL0:
350 	case SYS_CNTVCTSS_EL0:
351 		val = compute_counter_value(vcpu_hvtimer(vcpu));
352 		break;
353 	default:
354 		return false;
355 	}
356 
357 	vcpu_set_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu), val);
358 	__kvm_skip_instr(vcpu);
359 
360 	return true;
361 }
362 
363 static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code)
364 {
365 	u64 esr = kvm_vcpu_get_esr(vcpu);
366 	u64 spsr, elr, mode;
367 
368 	/*
369 	 * Going through the whole put/load motions is a waste of time
370 	 * if this is a VHE guest hypervisor returning to its own
371 	 * userspace, or the hypervisor performing a local exception
372 	 * return. No need to save/restore registers, no need to
373 	 * switch S2 MMU. Just do the canonical ERET.
374 	 *
375 	 * Unless the trap has to be forwarded further down the line,
376 	 * of course...
377 	 */
378 	if ((__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_NV) ||
379 	    (__vcpu_sys_reg(vcpu, HFGITR_EL2) & HFGITR_EL2_ERET))
380 		return false;
381 
382 	spsr = read_sysreg_el1(SYS_SPSR);
383 	mode = spsr & (PSR_MODE_MASK | PSR_MODE32_BIT);
384 
385 	switch (mode) {
386 	case PSR_MODE_EL0t:
387 		if (!(vcpu_el2_e2h_is_set(vcpu) && vcpu_el2_tge_is_set(vcpu)))
388 			return false;
389 		break;
390 	case PSR_MODE_EL2t:
391 		mode = PSR_MODE_EL1t;
392 		break;
393 	case PSR_MODE_EL2h:
394 		mode = PSR_MODE_EL1h;
395 		break;
396 	default:
397 		return false;
398 	}
399 
400 	/* If ERETAx fails, take the slow path */
401 	if (esr_iss_is_eretax(esr)) {
402 		if (!(vcpu_has_ptrauth(vcpu) && kvm_auth_eretax(vcpu, &elr)))
403 			return false;
404 	} else {
405 		elr = read_sysreg_el1(SYS_ELR);
406 	}
407 
408 	spsr = (spsr & ~(PSR_MODE_MASK | PSR_MODE32_BIT)) | mode;
409 
410 	write_sysreg_el2(spsr, SYS_SPSR);
411 	write_sysreg_el2(elr, SYS_ELR);
412 
413 	return true;
414 }
415 
416 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
417 {
418 	__fpsimd_save_state(*host_data_ptr(fpsimd_state));
419 
420 	if (kvm_has_fpmr(vcpu->kvm))
421 		**host_data_ptr(fpmr_ptr) = read_sysreg_s(SYS_FPMR);
422 }
423 
424 static bool kvm_hyp_handle_tlbi_el2(struct kvm_vcpu *vcpu, u64 *exit_code)
425 {
426 	int ret = -EINVAL;
427 	u32 instr;
428 	u64 val;
429 
430 	/*
431 	 * Ideally, we would never trap on EL2 S1 TLB invalidations using
432 	 * the EL1 instructions when the guest's HCR_EL2.{E2H,TGE}=={1,1}.
433 	 * But "thanks" to FEAT_NV2, we don't trap writes to HCR_EL2,
434 	 * meaning that we can't track changes to the virtual TGE bit. So we
435 	 * have to leave HCR_EL2.TTLB set on the host. Oopsie...
436 	 *
437 	 * Try and handle these invalidation as quickly as possible, without
438 	 * fully exiting. Note that we don't need to consider any forwarding
439 	 * here, as having E2H+TGE set is the very definition of being
440 	 * InHost.
441 	 *
442 	 * For the lesser hypervisors out there that have failed to get on
443 	 * with the VHE program, we can also handle the nVHE style of EL2
444 	 * invalidation.
445 	 */
446 	if (!(is_hyp_ctxt(vcpu)))
447 		return false;
448 
449 	instr = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
450 	val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
451 
452 	if ((kvm_supported_tlbi_s1e1_op(vcpu, instr) &&
453 	     vcpu_el2_e2h_is_set(vcpu) && vcpu_el2_tge_is_set(vcpu)) ||
454 	    kvm_supported_tlbi_s1e2_op (vcpu, instr))
455 		ret = __kvm_tlbi_s1e2(NULL, val, instr);
456 
457 	if (ret)
458 		return false;
459 
460 	__kvm_skip_instr(vcpu);
461 
462 	return true;
463 }
464 
465 static bool kvm_hyp_handle_cpacr_el1(struct kvm_vcpu *vcpu, u64 *exit_code)
466 {
467 	u64 esr = kvm_vcpu_get_esr(vcpu);
468 	int rt;
469 
470 	if (!is_hyp_ctxt(vcpu) || esr_sys64_to_sysreg(esr) != SYS_CPACR_EL1)
471 		return false;
472 
473 	rt = kvm_vcpu_sys_get_rt(vcpu);
474 
475 	if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ) {
476 		vcpu_set_reg(vcpu, rt, __vcpu_sys_reg(vcpu, CPTR_EL2));
477 	} else {
478 		vcpu_write_sys_reg(vcpu, vcpu_get_reg(vcpu, rt), CPTR_EL2);
479 		__activate_cptr_traps(vcpu);
480 	}
481 
482 	__kvm_skip_instr(vcpu);
483 
484 	return true;
485 }
486 
487 static bool kvm_hyp_handle_zcr_el2(struct kvm_vcpu *vcpu, u64 *exit_code)
488 {
489 	u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
490 
491 	if (!vcpu_has_nv(vcpu))
492 		return false;
493 
494 	if (sysreg != SYS_ZCR_EL2)
495 		return false;
496 
497 	if (guest_owns_fp_regs())
498 		return false;
499 
500 	/*
501 	 * ZCR_EL2 traps are handled in the slow path, with the expectation
502 	 * that the guest's FP context has already been loaded onto the CPU.
503 	 *
504 	 * Load the guest's FP context and unconditionally forward to the
505 	 * slow path for handling (i.e. return false).
506 	 */
507 	kvm_hyp_handle_fpsimd(vcpu, exit_code);
508 	return false;
509 }
510 
511 static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu *vcpu, u64 *exit_code)
512 {
513 	if (kvm_hyp_handle_tlbi_el2(vcpu, exit_code))
514 		return true;
515 
516 	if (kvm_hyp_handle_timer(vcpu, exit_code))
517 		return true;
518 
519 	if (kvm_hyp_handle_cpacr_el1(vcpu, exit_code))
520 		return true;
521 
522 	if (kvm_hyp_handle_zcr_el2(vcpu, exit_code))
523 		return true;
524 
525 	return kvm_hyp_handle_sysreg(vcpu, exit_code);
526 }
527 
528 static const exit_handler_fn hyp_exit_handlers[] = {
529 	[0 ... ESR_ELx_EC_MAX]		= NULL,
530 	[ESR_ELx_EC_CP15_32]		= kvm_hyp_handle_cp15_32,
531 	[ESR_ELx_EC_SYS64]		= kvm_hyp_handle_sysreg_vhe,
532 	[ESR_ELx_EC_SVE]		= kvm_hyp_handle_fpsimd,
533 	[ESR_ELx_EC_FP_ASIMD]		= kvm_hyp_handle_fpsimd,
534 	[ESR_ELx_EC_IABT_LOW]		= kvm_hyp_handle_iabt_low,
535 	[ESR_ELx_EC_DABT_LOW]		= kvm_hyp_handle_dabt_low,
536 	[ESR_ELx_EC_WATCHPT_LOW]	= kvm_hyp_handle_watchpt_low,
537 	[ESR_ELx_EC_ERET]		= kvm_hyp_handle_eret,
538 	[ESR_ELx_EC_MOPS]		= kvm_hyp_handle_mops,
539 };
540 
541 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)
542 {
543 	return hyp_exit_handlers;
544 }
545 
546 static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code)
547 {
548 	/*
549 	 * If we were in HYP context on entry, adjust the PSTATE view
550 	 * so that the usual helpers work correctly.
551 	 */
552 	if (vcpu_has_nv(vcpu) && (read_sysreg(hcr_el2) & HCR_NV)) {
553 		u64 mode = *vcpu_cpsr(vcpu) & (PSR_MODE_MASK | PSR_MODE32_BIT);
554 
555 		switch (mode) {
556 		case PSR_MODE_EL1t:
557 			mode = PSR_MODE_EL2t;
558 			break;
559 		case PSR_MODE_EL1h:
560 			mode = PSR_MODE_EL2h;
561 			break;
562 		}
563 
564 		*vcpu_cpsr(vcpu) &= ~(PSR_MODE_MASK | PSR_MODE32_BIT);
565 		*vcpu_cpsr(vcpu) |= mode;
566 	}
567 }
568 
569 /* Switch to the guest for VHE systems running in EL2 */
570 static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
571 {
572 	struct kvm_cpu_context *host_ctxt;
573 	struct kvm_cpu_context *guest_ctxt;
574 	u64 exit_code;
575 
576 	host_ctxt = host_data_ptr(host_ctxt);
577 	guest_ctxt = &vcpu->arch.ctxt;
578 
579 	sysreg_save_host_state_vhe(host_ctxt);
580 
581 	/*
582 	 * Note that ARM erratum 1165522 requires us to configure both stage 1
583 	 * and stage 2 translation for the guest context before we clear
584 	 * HCR_EL2.TGE. The stage 1 and stage 2 guest context has already been
585 	 * loaded on the CPU in kvm_vcpu_load_vhe().
586 	 */
587 	__activate_traps(vcpu);
588 
589 	__kvm_adjust_pc(vcpu);
590 
591 	sysreg_restore_guest_state_vhe(guest_ctxt);
592 	__debug_switch_to_guest(vcpu);
593 
594 	do {
595 		/* Jump in the fire! */
596 		exit_code = __guest_enter(vcpu);
597 
598 		/* And we're baaack! */
599 	} while (fixup_guest_exit(vcpu, &exit_code));
600 
601 	sysreg_save_guest_state_vhe(guest_ctxt);
602 
603 	__deactivate_traps(vcpu);
604 
605 	sysreg_restore_host_state_vhe(host_ctxt);
606 
607 	if (guest_owns_fp_regs())
608 		__fpsimd_save_fpexc32(vcpu);
609 
610 	__debug_switch_to_host(vcpu);
611 
612 	return exit_code;
613 }
614 NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe);
615 
616 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
617 {
618 	int ret;
619 
620 	local_daif_mask();
621 
622 	/*
623 	 * Having IRQs masked via PMR when entering the guest means the GIC
624 	 * will not signal the CPU of interrupts of lower priority, and the
625 	 * only way to get out will be via guest exceptions.
626 	 * Naturally, we want to avoid this.
627 	 *
628 	 * local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a
629 	 * dsb to ensure the redistributor is forwards EL2 IRQs to the CPU.
630 	 */
631 	pmr_sync();
632 
633 	ret = __kvm_vcpu_run_vhe(vcpu);
634 
635 	/*
636 	 * local_daif_restore() takes care to properly restore PSTATE.DAIF
637 	 * and the GIC PMR if the host is using IRQ priorities.
638 	 */
639 	local_daif_restore(DAIF_PROCCTX_NOIRQ);
640 
641 	/*
642 	 * When we exit from the guest we change a number of CPU configuration
643 	 * parameters, such as traps.  We rely on the isb() in kvm_call_hyp*()
644 	 * to make sure these changes take effect before running the host or
645 	 * additional guests.
646 	 */
647 	return ret;
648 }
649 
650 static void __noreturn __hyp_call_panic(u64 spsr, u64 elr, u64 par)
651 {
652 	struct kvm_cpu_context *host_ctxt;
653 	struct kvm_vcpu *vcpu;
654 
655 	host_ctxt = host_data_ptr(host_ctxt);
656 	vcpu = host_ctxt->__hyp_running_vcpu;
657 
658 	__deactivate_traps(vcpu);
659 	sysreg_restore_host_state_vhe(host_ctxt);
660 
661 	panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n",
662 	      spsr, elr,
663 	      read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR),
664 	      read_sysreg(hpfar_el2), par, vcpu);
665 }
666 NOKPROBE_SYMBOL(__hyp_call_panic);
667 
668 void __noreturn hyp_panic(void)
669 {
670 	u64 spsr = read_sysreg_el2(SYS_SPSR);
671 	u64 elr = read_sysreg_el2(SYS_ELR);
672 	u64 par = read_sysreg_par();
673 
674 	__hyp_call_panic(spsr, elr, par);
675 }
676 
677 asmlinkage void kvm_unexpected_el2_exception(void)
678 {
679 	__kvm_unexpected_el2_exception();
680 }
681