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/kvm_host.h>
11 #include <linux/types.h>
12 #include <linux/jump_label.h>
13 #include <uapi/linux/psci.h>
14
15 #include <kvm/arm_psci.h>
16
17 #include <asm/barrier.h>
18 #include <asm/cpufeature.h>
19 #include <asm/kprobes.h>
20 #include <asm/kvm_asm.h>
21 #include <asm/kvm_emulate.h>
22 #include <asm/kvm_hyp.h>
23 #include <asm/kvm_hypevents.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/mem_protect.h>
30 #include <nvhe/pkvm.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 extern void __pkvm_unmask_serror(void);
39
40 #define update_pvm_fgt_traps(vcpu, reg) \
41 update_fgt_traps_cs(vcpu, reg, PVM_ ## reg ## _CLR, PVM_ ## reg ## _SET)
42
__activate_pvm_fine_grain_traps(struct kvm_vcpu * vcpu)43 static void __activate_pvm_fine_grain_traps(struct kvm_vcpu *vcpu)
44 {
45 if (cpus_have_final_cap(ARM64_HAS_HCX))
46 update_pvm_fgt_traps(vcpu, HCRX_EL2);
47
48 if (!cpus_have_final_cap(ARM64_HAS_FGT))
49 return;
50
51 update_pvm_fgt_traps(vcpu, HFGRTR_EL2);
52
53 /* Trap guest writes to TCR_EL1 to prevent it from enabling HA or HD. */
54 if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38)) {
55 update_fgt_traps_cs(vcpu, HFGWTR_EL2, PVM_HFGWTR_EL2_CLR,
56 PVM_HFGWTR_EL2_SET | HFGxTR_EL2_TCR_EL1_MASK);
57 } else {
58 update_pvm_fgt_traps(vcpu, HFGWTR_EL2);
59 }
60
61 update_pvm_fgt_traps(vcpu, HFGITR_EL2);
62 update_pvm_fgt_traps(vcpu, HDFGRTR_EL2);
63 update_pvm_fgt_traps(vcpu, HDFGWTR_EL2);
64
65 if (cpu_has_amu())
66 update_pvm_fgt_traps(vcpu, HAFGRTR_EL2);
67 }
68
__deactivate_pvm_traps_hfgxtr(struct kvm_vcpu * vcpu)69 static void __deactivate_pvm_traps_hfgxtr(struct kvm_vcpu *vcpu)
70 {
71 struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
72
73 if (!cpus_have_final_cap(ARM64_HAS_FGT))
74 return;
75
76 write_sysreg_s(ctxt_sys_reg(hctxt, HFGRTR_EL2), SYS_HFGRTR_EL2);
77 write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2);
78 write_sysreg_s(ctxt_sys_reg(hctxt, HFGITR_EL2), SYS_HFGITR_EL2);
79 write_sysreg_s(ctxt_sys_reg(hctxt, HDFGRTR_EL2), SYS_HDFGRTR_EL2);
80 write_sysreg_s(ctxt_sys_reg(hctxt, HDFGWTR_EL2), SYS_HDFGWTR_EL2);
81
82 if (cpu_has_amu())
83 write_sysreg_s(ctxt_sys_reg(hctxt, HAFGRTR_EL2), SYS_HAFGRTR_EL2);
84 }
85
__activate_traps(struct kvm_vcpu * vcpu)86 static void __activate_traps(struct kvm_vcpu *vcpu)
87 {
88 u64 val;
89
90 ___activate_traps(vcpu);
91 __activate_traps_common(vcpu);
92
93 if (unlikely(vcpu_is_protected(vcpu))) {
94 __activate_pvm_fine_grain_traps(vcpu);
95 } else {
96 __activate_traps_hcrx(vcpu);
97 __activate_traps_hfgxtr(vcpu);
98 }
99
100 val = vcpu->arch.cptr_el2;
101 val |= CPTR_EL2_TAM; /* Same bit irrespective of E2H */
102 val |= has_hvhe() ? CPACR_EL1_TTA : CPTR_EL2_TTA;
103 if (cpus_have_final_cap(ARM64_SME)) {
104 if (has_hvhe())
105 val &= ~(CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN);
106 else
107 val |= CPTR_EL2_TSM;
108 }
109
110 if (vcpu->arch.fp_state != FP_STATE_GUEST_OWNED) {
111 if (has_hvhe())
112 val &= ~(CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN |
113 CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN);
114 else
115 val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
116
117 __activate_traps_fpsimd32(vcpu);
118 }
119
120 kvm_write_cptr_el2(val);
121 write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
122
123 if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
124 struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
125
126 isb();
127 /*
128 * At this stage, and thanks to the above isb(), S2 is
129 * configured and enabled. We can now restore the guest's S1
130 * configuration: SCTLR, and only then TCR.
131 */
132 write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR);
133 isb();
134 write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1), SYS_TCR);
135 }
136 }
137
__deactivate_traps(struct kvm_vcpu * vcpu)138 static void __deactivate_traps(struct kvm_vcpu *vcpu)
139 {
140 extern char __kvm_hyp_host_vector[];
141
142 ___deactivate_traps(vcpu);
143
144 if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
145 u64 val;
146
147 /*
148 * Set the TCR and SCTLR registers in the exact opposite
149 * sequence as __activate_traps (first prevent walks,
150 * then force the MMU on). A generous sprinkling of isb()
151 * ensure that things happen in this exact order.
152 */
153 val = read_sysreg_el1(SYS_TCR);
154 write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR);
155 isb();
156 val = read_sysreg_el1(SYS_SCTLR);
157 write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR);
158 isb();
159 }
160
161 __deactivate_traps_common(vcpu);
162
163 if (unlikely(vcpu_is_protected(vcpu)))
164 __deactivate_pvm_traps_hfgxtr(vcpu);
165 else
166 __deactivate_traps_hfgxtr(vcpu);
167
168 write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
169
170 kvm_reset_cptr_el2(vcpu);
171 write_sysreg(__kvm_hyp_host_vector, vbar_el2);
172 }
173
__deactivate_fpsimd_traps(struct kvm_vcpu * vcpu)174 static void __deactivate_fpsimd_traps(struct kvm_vcpu *vcpu)
175 {
176 u64 reg;
177 bool trap_sve = vcpu_has_sve(vcpu) ||
178 (is_protected_kvm_enabled() && system_supports_sve());
179
180 if (has_hvhe()) {
181 reg = CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN;
182 if (trap_sve)
183 reg |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
184
185 sysreg_clear_set(cpacr_el1, 0, reg);
186 } else {
187 reg = CPTR_EL2_TFP;
188 if (trap_sve)
189 reg |= CPTR_EL2_TZ;
190
191 sysreg_clear_set(cptr_el2, reg, 0);
192 }
193 }
194
195 /* Save VGICv3 state on non-VHE systems */
__hyp_vgic_save_state(struct kvm_vcpu * vcpu)196 static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
197 {
198 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
199 __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
200 __vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
201 }
202 }
203
204 /* Restore VGICv3 state on non-VHE systems */
__hyp_vgic_restore_state(struct kvm_vcpu * vcpu)205 static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
206 {
207 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
208 __vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
209 __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
210 }
211 }
212
213 /*
214 * Disable host events, enable guest events
215 */
216 #ifdef CONFIG_HW_PERF_EVENTS
__pmu_switch_to_guest(struct kvm_vcpu * vcpu)217 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
218 {
219 struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
220
221 if (pmu->events_host)
222 write_sysreg(pmu->events_host, pmcntenclr_el0);
223
224 if (pmu->events_guest)
225 write_sysreg(pmu->events_guest, pmcntenset_el0);
226
227 return (pmu->events_host || pmu->events_guest);
228 }
229
230 /*
231 * Disable guest events, enable host events
232 */
__pmu_switch_to_host(struct kvm_vcpu * vcpu)233 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
234 {
235 struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
236
237 if (pmu->events_guest)
238 write_sysreg(pmu->events_guest, pmcntenclr_el0);
239
240 if (pmu->events_host)
241 write_sysreg(pmu->events_host, pmcntenset_el0);
242 }
243 #else
244 #define __pmu_switch_to_guest(v) ({ false; })
245 #define __pmu_switch_to_host(v) do {} while (0)
246 #endif
247
248 /*
249 * Handler for protected VM MSR, MRS or System instruction execution in AArch64.
250 *
251 * Returns true if the hypervisor has handled the exit, and control should go
252 * back to the guest, or false if it hasn't.
253 */
kvm_handle_pvm_sys64(struct kvm_vcpu * vcpu,u64 * exit_code)254 static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code)
255 {
256 /*
257 * Make sure we handle the exit for workarounds and ptrauth
258 * before the pKVM handling, as the latter could decide to
259 * UNDEF.
260 */
261 return (kvm_hyp_handle_sysreg(vcpu, exit_code) ||
262 kvm_handle_pvm_sysreg(vcpu, exit_code));
263 }
264
kvm_hyp_handle_fpsimd_host(struct kvm_vcpu * vcpu)265 static void kvm_hyp_handle_fpsimd_host(struct kvm_vcpu *vcpu)
266 {
267 /*
268 * Non-protected kvm relies on the host restoring its sve state.
269 * Protected kvm restores the host's sve state as not to reveal that
270 * fpsimd was used by a guest nor leak upper sve bits.
271 */
272 if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
273 struct kvm_host_sve_state *sve_state = get_host_sve_state(vcpu);
274 u64 zcr_el2 = sve_vq_from_vl(kvm_host_sve_max_vl) - 1;
275
276 sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR);
277 sve_cond_update_zcr_vq(zcr_el2, SYS_ZCR_EL2);
278 __sve_save_state(sve_state->sve_regs +
279 sve_ffr_offset(kvm_host_sve_max_vl),
280 &sve_state->fpsr);
281
282 /* Still trap SVE since it's handled by hyp in pKVM. */
283 if (!vcpu_has_sve(vcpu))
284 sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);
285 } else {
286 __fpsimd_save_state(get_host_fpsimd_state(vcpu));
287 }
288 }
289
290 static const exit_handler_fn hyp_exit_handlers[] = {
291 [0 ... ESR_ELx_EC_MAX] = NULL,
292 [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32,
293 [ESR_ELx_EC_HVC64] = kvm_hyp_handle_hvc64,
294 [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg,
295 [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd,
296 [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
297 [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
298 [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
299 [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
300 [ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
301 };
302
303 static const exit_handler_fn pvm_exit_handlers[] = {
304 [0 ... ESR_ELx_EC_MAX] = NULL,
305 [ESR_ELx_EC_HVC64] = kvm_handle_pvm_hvc64,
306 [ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64,
307 [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd,
308 [ESR_ELx_EC_SME] = kvm_handle_pvm_restricted,
309 [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
310 [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
311 [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
312 [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
313 [ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
314 };
315
kvm_get_exit_handler_array(struct kvm_vcpu * vcpu)316 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)
317 {
318 if (unlikely(vcpu_is_protected(vcpu)))
319 return pvm_exit_handlers;
320
321 return hyp_exit_handlers;
322 }
323
324 /*
325 * Some guests (e.g., protected VMs) are not be allowed to run in AArch32.
326 * The ARMv8 architecture does not give the hypervisor a mechanism to prevent a
327 * guest from dropping to AArch32 EL0 if implemented by the CPU. If the
328 * hypervisor spots a guest in such a state ensure it is handled, and don't
329 * trust the host to spot or fix it. The check below is based on the one in
330 * kvm_arch_vcpu_ioctl_run().
331 *
332 * Returns false if the guest ran in AArch32 when it shouldn't have, and
333 * thus should exit to the host, or true if a the guest run loop can continue.
334 */
early_exit_filter(struct kvm_vcpu * vcpu,u64 * exit_code)335 static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code)
336 {
337 if (unlikely(vcpu_is_protected(vcpu) && vcpu_mode_is_32bit(vcpu))) {
338 /*
339 * As we have caught the guest red-handed, decide that it isn't
340 * fit for purpose anymore by making the vcpu invalid. The VMM
341 * can try and fix it by re-initializing the vcpu with
342 * KVM_ARM_VCPU_INIT, however, this is likely not possible for
343 * protected VMs.
344 */
345 vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
346 *exit_code &= BIT(ARM_EXIT_WITH_SERROR_BIT);
347 *exit_code |= ARM_EXCEPTION_IL;
348 }
349 }
350
351 /* Switch to the guest for legacy non-VHE systems */
__kvm_vcpu_run(struct kvm_vcpu * vcpu)352 int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
353 {
354 struct kvm_cpu_context *host_ctxt;
355 struct kvm_cpu_context *guest_ctxt;
356 struct kvm_s2_mmu *mmu;
357 bool pmu_switch_needed;
358 u64 exit_code;
359
360 /*
361 * Having IRQs masked via PMR when entering the guest means the GIC
362 * will not signal the CPU of interrupts of lower priority, and the
363 * only way to get out will be via guest exceptions.
364 * Naturally, we want to avoid this.
365 */
366 if (system_uses_irq_prio_masking()) {
367 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
368 pmr_sync();
369 }
370
371 host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
372 host_ctxt->__hyp_running_vcpu = vcpu;
373 guest_ctxt = &vcpu->arch.ctxt;
374
375 pmu_switch_needed = __pmu_switch_to_guest(vcpu);
376
377 __sysreg_save_state_nvhe(host_ctxt);
378 /*
379 * We must flush and disable the SPE buffer for nVHE, as
380 * the translation regime(EL1&0) is going to be loaded with
381 * that of the guest. And we must do this before we change the
382 * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
383 * before we load guest Stage1.
384 */
385 __debug_save_host_buffers_nvhe(vcpu);
386
387 /*
388 * We're about to restore some new MMU state. Make sure
389 * ongoing page-table walks that have started before we
390 * trapped to EL2 have completed. This also synchronises the
391 * above disabling of SPE and TRBE.
392 *
393 * See DDI0487I.a D8.1.5 "Out-of-context translation regimes",
394 * rule R_LFHQG and subsequent information statements.
395 */
396 dsb(nsh);
397
398 __kvm_adjust_pc(vcpu);
399
400 /*
401 * We must restore the 32-bit state before the sysregs, thanks
402 * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
403 *
404 * Also, and in order to be able to deal with erratum #1319537 (A57)
405 * and #1319367 (A72), we must ensure that all VM-related sysreg are
406 * restored before we enable S2 translation.
407 */
408 __sysreg32_restore_state(vcpu);
409 __sysreg_restore_state_nvhe(guest_ctxt);
410
411 mmu = kern_hyp_va(vcpu->arch.hw_mmu);
412 __load_stage2(mmu, kern_hyp_va(mmu->arch));
413 __activate_traps(vcpu);
414
415 __hyp_vgic_restore_state(vcpu);
416 __timer_enable_traps(vcpu);
417
418 __debug_switch_to_guest(vcpu);
419
420 do {
421 trace_hyp_exit();
422
423 /* Jump in the fire! */
424 exit_code = __guest_enter(vcpu);
425
426 /* And we're baaack! */
427 trace_hyp_enter();
428 } while (fixup_guest_exit(vcpu, &exit_code));
429
430 __sysreg_save_state_nvhe(guest_ctxt);
431 __sysreg32_save_state(vcpu);
432 __timer_disable_traps(vcpu);
433 __hyp_vgic_save_state(vcpu);
434
435 /*
436 * Same thing as before the guest run: we're about to switch
437 * the MMU context, so let's make sure we don't have any
438 * ongoing EL1&0 translations.
439 */
440 dsb(nsh);
441
442 __deactivate_traps(vcpu);
443 __load_host_stage2();
444
445 __sysreg_restore_state_nvhe(host_ctxt);
446
447 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
448 __fpsimd_save_fpexc32(vcpu);
449
450 __debug_switch_to_host(vcpu);
451 /*
452 * This must come after restoring the host sysregs, since a non-VHE
453 * system may enable SPE here and make use of the TTBRs.
454 */
455 __debug_restore_host_buffers_nvhe(vcpu);
456
457 if (pmu_switch_needed)
458 __pmu_switch_to_host(vcpu);
459
460 /* Returning to host will clear PSR.I, remask PMR if needed */
461 if (system_uses_irq_prio_masking())
462 gic_write_pmr(GIC_PRIO_IRQOFF);
463
464 host_ctxt->__hyp_running_vcpu = NULL;
465
466 __pkvm_unmask_serror();
467
468 return exit_code;
469 }
470
471 static void (*hyp_panic_notifier)(struct user_pt_regs *regs);
__pkvm_register_hyp_panic_notifier(void (* cb)(struct user_pt_regs * regs))472 int __pkvm_register_hyp_panic_notifier(void (*cb)(struct user_pt_regs *regs))
473 {
474 return cmpxchg(&hyp_panic_notifier, NULL, cb) ? -EBUSY : 0;
475 }
476
hyp_panic(void)477 asmlinkage void __noreturn hyp_panic(void)
478 {
479 u64 spsr = read_sysreg_el2(SYS_SPSR);
480 u64 elr = read_sysreg_el2(SYS_ELR);
481 u64 par = read_sysreg_par();
482 struct kvm_cpu_context *host_ctxt;
483 struct kvm_vcpu *vcpu;
484
485 host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
486 vcpu = host_ctxt->__hyp_running_vcpu;
487
488 if (READ_ONCE(hyp_panic_notifier))
489 hyp_panic_notifier(&host_ctxt->regs);
490
491 if (vcpu) {
492 __timer_disable_traps(vcpu);
493 __deactivate_traps(vcpu);
494 __load_host_stage2();
495 __sysreg_restore_state_nvhe(host_ctxt);
496 }
497
498 /* Prepare to dump kvm nvhe hyp stacktrace */
499 kvm_nvhe_prepare_backtrace((unsigned long)__builtin_frame_address(0),
500 _THIS_IP_);
501
502 __hyp_do_panic(host_ctxt, spsr, elr, par);
503 unreachable();
504 }
505
hyp_panic_bad_stack(void)506 asmlinkage void __noreturn hyp_panic_bad_stack(void)
507 {
508 hyp_panic();
509 }
510
kvm_unexpected_el2_exception(void)511 asmlinkage void kvm_unexpected_el2_exception(void)
512 {
513 __kvm_unexpected_el2_exception();
514 }
515