xref: /openbmc/linux/arch/arm64/kvm/hyp/nvhe/switch.c (revision 840d9a813c8eaa5c55d86525e374a97ca5023b53)
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 #include <hyp/sysreg-sr.h>
9 
10 #include <linux/arm-smccc.h>
11 #include <linux/kvm_host.h>
12 #include <linux/types.h>
13 #include <linux/jump_label.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 
29 #include <nvhe/fixed_config.h>
30 #include <nvhe/mem_protect.h>
31 
32 /* Non-VHE specific context */
33 DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
34 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
35 DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
36 
37 extern void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc);
38 
__activate_cptr_traps(struct kvm_vcpu * vcpu)39 static void __activate_cptr_traps(struct kvm_vcpu *vcpu)
40 {
41 	u64 val = CPTR_EL2_TAM;	/* Same bit irrespective of E2H */
42 
43 	if (!guest_owns_fp_regs(vcpu))
44 		__activate_traps_fpsimd32(vcpu);
45 
46 	if (has_hvhe()) {
47 		val |= CPACR_ELx_TTA;
48 
49 		if (guest_owns_fp_regs(vcpu)) {
50 			val |= CPACR_ELx_FPEN;
51 			if (vcpu_has_sve(vcpu))
52 				val |= CPACR_ELx_ZEN;
53 		}
54 
55 		write_sysreg(val, cpacr_el1);
56 	} else {
57 		val |= CPTR_EL2_TTA | CPTR_NVHE_EL2_RES1;
58 
59 		/*
60 		 * Always trap SME since it's not supported in KVM.
61 		 * TSM is RES1 if SME isn't implemented.
62 		 */
63 		val |= CPTR_EL2_TSM;
64 
65 		if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs(vcpu))
66 			val |= CPTR_EL2_TZ;
67 
68 		if (!guest_owns_fp_regs(vcpu))
69 			val |= CPTR_EL2_TFP;
70 
71 		write_sysreg(val, cptr_el2);
72 	}
73 }
74 
__deactivate_cptr_traps(struct kvm_vcpu * vcpu)75 static void __deactivate_cptr_traps(struct kvm_vcpu *vcpu)
76 {
77 	if (has_hvhe()) {
78 		u64 val = CPACR_ELx_FPEN;
79 
80 		if (cpus_have_final_cap(ARM64_SVE))
81 			val |= CPACR_ELx_ZEN;
82 		if (cpus_have_final_cap(ARM64_SME))
83 			val |= CPACR_ELx_SMEN;
84 
85 		write_sysreg(val, cpacr_el1);
86 	} else {
87 		u64 val = CPTR_NVHE_EL2_RES1;
88 
89 		if (!cpus_have_final_cap(ARM64_SVE))
90 			val |= CPTR_EL2_TZ;
91 		if (!cpus_have_final_cap(ARM64_SME))
92 			val |= CPTR_EL2_TSM;
93 
94 		write_sysreg(val, cptr_el2);
95 	}
96 }
97 
__activate_traps(struct kvm_vcpu * vcpu)98 static void __activate_traps(struct kvm_vcpu *vcpu)
99 {
100 	___activate_traps(vcpu);
101 	__activate_traps_common(vcpu);
102 	__activate_cptr_traps(vcpu);
103 
104 	write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
105 
106 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
107 		struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
108 
109 		isb();
110 		/*
111 		 * At this stage, and thanks to the above isb(), S2 is
112 		 * configured and enabled. We can now restore the guest's S1
113 		 * configuration: SCTLR, and only then TCR.
114 		 */
115 		write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1),	SYS_SCTLR);
116 		isb();
117 		write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1),	SYS_TCR);
118 	}
119 }
120 
__deactivate_traps(struct kvm_vcpu * vcpu)121 static void __deactivate_traps(struct kvm_vcpu *vcpu)
122 {
123 	extern char __kvm_hyp_host_vector[];
124 
125 	___deactivate_traps(vcpu);
126 
127 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
128 		u64 val;
129 
130 		/*
131 		 * Set the TCR and SCTLR registers in the exact opposite
132 		 * sequence as __activate_traps (first prevent walks,
133 		 * then force the MMU on). A generous sprinkling of isb()
134 		 * ensure that things happen in this exact order.
135 		 */
136 		val = read_sysreg_el1(SYS_TCR);
137 		write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR);
138 		isb();
139 		val = read_sysreg_el1(SYS_SCTLR);
140 		write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR);
141 		isb();
142 	}
143 
144 	__deactivate_traps_common(vcpu);
145 
146 	write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
147 
148 	__deactivate_cptr_traps(vcpu);
149 	write_sysreg(__kvm_hyp_host_vector, vbar_el2);
150 }
151 
152 /* Save VGICv3 state on non-VHE systems */
__hyp_vgic_save_state(struct kvm_vcpu * vcpu)153 static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
154 {
155 	if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
156 		__vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
157 		__vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
158 	}
159 }
160 
161 /* Restore VGICv3 state on non-VHE systems */
__hyp_vgic_restore_state(struct kvm_vcpu * vcpu)162 static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
163 {
164 	if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
165 		__vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
166 		__vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
167 	}
168 }
169 
170 /*
171  * Disable host events, enable guest events
172  */
173 #ifdef CONFIG_HW_PERF_EVENTS
__pmu_switch_to_guest(struct kvm_vcpu * vcpu)174 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
175 {
176 	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
177 
178 	if (pmu->events_host)
179 		write_sysreg(pmu->events_host, pmcntenclr_el0);
180 
181 	if (pmu->events_guest)
182 		write_sysreg(pmu->events_guest, pmcntenset_el0);
183 
184 	return (pmu->events_host || pmu->events_guest);
185 }
186 
187 /*
188  * Disable guest events, enable host events
189  */
__pmu_switch_to_host(struct kvm_vcpu * vcpu)190 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
191 {
192 	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
193 
194 	if (pmu->events_guest)
195 		write_sysreg(pmu->events_guest, pmcntenclr_el0);
196 
197 	if (pmu->events_host)
198 		write_sysreg(pmu->events_host, pmcntenset_el0);
199 }
200 #else
201 #define __pmu_switch_to_guest(v)	({ false; })
202 #define __pmu_switch_to_host(v)		do {} while (0)
203 #endif
204 
205 /*
206  * Handler for protected VM MSR, MRS or System instruction execution in AArch64.
207  *
208  * Returns true if the hypervisor has handled the exit, and control should go
209  * back to the guest, or false if it hasn't.
210  */
kvm_handle_pvm_sys64(struct kvm_vcpu * vcpu,u64 * exit_code)211 static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code)
212 {
213 	/*
214 	 * Make sure we handle the exit for workarounds and ptrauth
215 	 * before the pKVM handling, as the latter could decide to
216 	 * UNDEF.
217 	 */
218 	return (kvm_hyp_handle_sysreg(vcpu, exit_code) ||
219 		kvm_handle_pvm_sysreg(vcpu, exit_code));
220 }
221 
222 static const exit_handler_fn hyp_exit_handlers[] = {
223 	[0 ... ESR_ELx_EC_MAX]		= NULL,
224 	[ESR_ELx_EC_CP15_32]		= kvm_hyp_handle_cp15_32,
225 	[ESR_ELx_EC_SYS64]		= kvm_hyp_handle_sysreg,
226 	[ESR_ELx_EC_SVE]		= kvm_hyp_handle_fpsimd,
227 	[ESR_ELx_EC_FP_ASIMD]		= kvm_hyp_handle_fpsimd,
228 	[ESR_ELx_EC_IABT_LOW]		= kvm_hyp_handle_iabt_low,
229 	[ESR_ELx_EC_DABT_LOW]		= kvm_hyp_handle_dabt_low,
230 	[ESR_ELx_EC_WATCHPT_LOW]	= kvm_hyp_handle_watchpt_low,
231 	[ESR_ELx_EC_PAC]		= kvm_hyp_handle_ptrauth,
232 };
233 
234 static const exit_handler_fn pvm_exit_handlers[] = {
235 	[0 ... ESR_ELx_EC_MAX]		= NULL,
236 	[ESR_ELx_EC_SYS64]		= kvm_handle_pvm_sys64,
237 	[ESR_ELx_EC_SVE]		= kvm_handle_pvm_restricted,
238 	[ESR_ELx_EC_FP_ASIMD]		= kvm_hyp_handle_fpsimd,
239 	[ESR_ELx_EC_IABT_LOW]		= kvm_hyp_handle_iabt_low,
240 	[ESR_ELx_EC_DABT_LOW]		= kvm_hyp_handle_dabt_low,
241 	[ESR_ELx_EC_WATCHPT_LOW]	= kvm_hyp_handle_watchpt_low,
242 	[ESR_ELx_EC_PAC]		= kvm_hyp_handle_ptrauth,
243 };
244 
kvm_get_exit_handler_array(struct kvm_vcpu * vcpu)245 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)
246 {
247 	if (unlikely(kvm_vm_is_protected(kern_hyp_va(vcpu->kvm))))
248 		return pvm_exit_handlers;
249 
250 	return hyp_exit_handlers;
251 }
252 
fixup_guest_exit(struct kvm_vcpu * vcpu,u64 * exit_code)253 static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
254 {
255 	const exit_handler_fn *handlers = kvm_get_exit_handler_array(vcpu);
256 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
257 
258 	synchronize_vcpu_pstate(vcpu, exit_code);
259 
260 	/*
261 	 * Some guests (e.g., protected VMs) are not be allowed to run in
262 	 * AArch32.  The ARMv8 architecture does not give the hypervisor a
263 	 * mechanism to prevent a guest from dropping to AArch32 EL0 if
264 	 * implemented by the CPU. If the hypervisor spots a guest in such a
265 	 * state ensure it is handled, and don't trust the host to spot or fix
266 	 * it.  The check below is based on the one in
267 	 * kvm_arch_vcpu_ioctl_run().
268 	 */
269 	if (kvm_vm_is_protected(kvm) && vcpu_mode_is_32bit(vcpu)) {
270 		/*
271 		 * As we have caught the guest red-handed, decide that it isn't
272 		 * fit for purpose anymore by making the vcpu invalid. The VMM
273 		 * can try and fix it by re-initializing the vcpu with
274 		 * KVM_ARM_VCPU_INIT, however, this is likely not possible for
275 		 * protected VMs.
276 		 */
277 		vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
278 		*exit_code &= BIT(ARM_EXIT_WITH_SERROR_BIT);
279 		*exit_code |= ARM_EXCEPTION_IL;
280 	}
281 
282 	return __fixup_guest_exit(vcpu, exit_code, handlers);
283 }
284 
285 /* Switch to the guest for legacy non-VHE systems */
__kvm_vcpu_run(struct kvm_vcpu * vcpu)286 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
287 {
288 	struct kvm_cpu_context *host_ctxt;
289 	struct kvm_cpu_context *guest_ctxt;
290 	struct kvm_s2_mmu *mmu;
291 	bool pmu_switch_needed;
292 	u64 exit_code;
293 
294 	/*
295 	 * Having IRQs masked via PMR when entering the guest means the GIC
296 	 * will not signal the CPU of interrupts of lower priority, and the
297 	 * only way to get out will be via guest exceptions.
298 	 * Naturally, we want to avoid this.
299 	 */
300 	if (system_uses_irq_prio_masking()) {
301 		gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
302 		pmr_sync();
303 	}
304 
305 	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
306 	host_ctxt->__hyp_running_vcpu = vcpu;
307 	guest_ctxt = &vcpu->arch.ctxt;
308 
309 	pmu_switch_needed = __pmu_switch_to_guest(vcpu);
310 
311 	__sysreg_save_state_nvhe(host_ctxt);
312 	/*
313 	 * We must flush and disable the SPE buffer for nVHE, as
314 	 * the translation regime(EL1&0) is going to be loaded with
315 	 * that of the guest. And we must do this before we change the
316 	 * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
317 	 * before we load guest Stage1.
318 	 */
319 	__debug_save_host_buffers_nvhe(vcpu);
320 
321 	/*
322 	 * We're about to restore some new MMU state. Make sure
323 	 * ongoing page-table walks that have started before we
324 	 * trapped to EL2 have completed. This also synchronises the
325 	 * above disabling of SPE and TRBE.
326 	 *
327 	 * See DDI0487I.a D8.1.5 "Out-of-context translation regimes",
328 	 * rule R_LFHQG and subsequent information statements.
329 	 */
330 	dsb(nsh);
331 
332 	__kvm_adjust_pc(vcpu);
333 
334 	/*
335 	 * We must restore the 32-bit state before the sysregs, thanks
336 	 * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
337 	 *
338 	 * Also, and in order to be able to deal with erratum #1319537 (A57)
339 	 * and #1319367 (A72), we must ensure that all VM-related sysreg are
340 	 * restored before we enable S2 translation.
341 	 */
342 	__sysreg32_restore_state(vcpu);
343 	__sysreg_restore_state_nvhe(guest_ctxt);
344 
345 	mmu = kern_hyp_va(vcpu->arch.hw_mmu);
346 	__load_stage2(mmu, kern_hyp_va(mmu->arch));
347 	__activate_traps(vcpu);
348 
349 	__hyp_vgic_restore_state(vcpu);
350 	__timer_enable_traps(vcpu);
351 
352 	__debug_switch_to_guest(vcpu);
353 
354 	do {
355 		/* Jump in the fire! */
356 		exit_code = __guest_enter(vcpu);
357 
358 		/* And we're baaack! */
359 	} while (fixup_guest_exit(vcpu, &exit_code));
360 
361 	__sysreg_save_state_nvhe(guest_ctxt);
362 	__sysreg32_save_state(vcpu);
363 	__timer_disable_traps(vcpu);
364 	__hyp_vgic_save_state(vcpu);
365 
366 	/*
367 	 * Same thing as before the guest run: we're about to switch
368 	 * the MMU context, so let's make sure we don't have any
369 	 * ongoing EL1&0 translations.
370 	 */
371 	dsb(nsh);
372 
373 	__deactivate_traps(vcpu);
374 	__load_host_stage2();
375 
376 	__sysreg_restore_state_nvhe(host_ctxt);
377 
378 	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
379 		__fpsimd_save_fpexc32(vcpu);
380 
381 	__debug_switch_to_host(vcpu);
382 	/*
383 	 * This must come after restoring the host sysregs, since a non-VHE
384 	 * system may enable SPE here and make use of the TTBRs.
385 	 */
386 	__debug_restore_host_buffers_nvhe(vcpu);
387 
388 	if (pmu_switch_needed)
389 		__pmu_switch_to_host(vcpu);
390 
391 	/* Returning to host will clear PSR.I, remask PMR if needed */
392 	if (system_uses_irq_prio_masking())
393 		gic_write_pmr(GIC_PRIO_IRQOFF);
394 
395 	host_ctxt->__hyp_running_vcpu = NULL;
396 
397 	return exit_code;
398 }
399 
hyp_panic(void)400 asmlinkage void __noreturn hyp_panic(void)
401 {
402 	u64 spsr = read_sysreg_el2(SYS_SPSR);
403 	u64 elr = read_sysreg_el2(SYS_ELR);
404 	u64 par = read_sysreg_par();
405 	struct kvm_cpu_context *host_ctxt;
406 	struct kvm_vcpu *vcpu;
407 
408 	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
409 	vcpu = host_ctxt->__hyp_running_vcpu;
410 
411 	if (vcpu) {
412 		__timer_disable_traps(vcpu);
413 		__deactivate_traps(vcpu);
414 		__load_host_stage2();
415 		__sysreg_restore_state_nvhe(host_ctxt);
416 	}
417 
418 	/* Prepare to dump kvm nvhe hyp stacktrace */
419 	kvm_nvhe_prepare_backtrace((unsigned long)__builtin_frame_address(0),
420 				   _THIS_IP_);
421 
422 	__hyp_do_panic(host_ctxt, spsr, elr, par);
423 	unreachable();
424 }
425 
hyp_panic_bad_stack(void)426 asmlinkage void __noreturn hyp_panic_bad_stack(void)
427 {
428 	hyp_panic();
429 }
430 
kvm_unexpected_el2_exception(void)431 asmlinkage void kvm_unexpected_el2_exception(void)
432 {
433 	__kvm_unexpected_el2_exception();
434 }
435