1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #ifndef __ARM64_KVM_HYP_SWITCH_H__
8 #define __ARM64_KVM_HYP_SWITCH_H__
10 #include <hyp/adjust_pc.h>
12 #include <linux/arm-smccc.h>
13 #include <linux/kvm_host.h>
14 #include <linux/types.h>
15 #include <linux/jump_label.h>
16 #include <uapi/linux/psci.h>
18 #include <kvm/arm_psci.h>
20 #include <asm/barrier.h>
21 #include <asm/cpufeature.h>
22 #include <asm/extable.h>
23 #include <asm/kprobes.h>
24 #include <asm/kvm_asm.h>
25 #include <asm/kvm_emulate.h>
26 #include <asm/kvm_hyp.h>
27 #include <asm/kvm_mmu.h>
28 #include <asm/fpsimd.h>
29 #include <asm/debug-monitors.h>
30 #include <asm/processor.h>
31 #include <asm/thread_info.h>
33 extern struct exception_table_entry __start___kvm_ex_table;
34 extern struct exception_table_entry __stop___kvm_ex_table;
36 /* Check whether the FP regs were dirtied while in the host-side run loop: */
37 static inline bool update_fp_enabled(struct kvm_vcpu *vcpu)
40 * When the system doesn't support FP/SIMD, we cannot rely on
41 * the _TIF_FOREIGN_FPSTATE flag. However, we always inject an
42 * abort on the very first access to FP and thus we should never
43 * see KVM_ARM64_FP_ENABLED. For added safety, make sure we always
46 if (!system_supports_fpsimd() ||
47 vcpu->arch.host_thread_info->flags & _TIF_FOREIGN_FPSTATE)
48 vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
51 return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED);
54 /* Save the 32-bit only FPSIMD system register state */
55 static inline void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
57 if (!vcpu_el1_is_32bit(vcpu))
60 __vcpu_sys_reg(vcpu, FPEXC32_EL2) = read_sysreg(fpexc32_el2);
63 static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
66 * We are about to set CPTR_EL2.TFP to trap all floating point
67 * register accesses to EL2, however, the ARM ARM clearly states that
68 * traps are only taken to EL2 if the operation would not otherwise
69 * trap to EL1. Therefore, always make sure that for 32-bit guests,
70 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
71 * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
72 * it will cause an exception.
74 if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
75 write_sysreg(1 << 30, fpexc32_el2);
80 static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
82 /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
83 write_sysreg(1 << 15, hstr_el2);
86 * Make sure we trap PMU access from EL0 to EL2. Also sanitize
87 * PMSELR_EL0 to make sure it never contains the cycle
88 * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
89 * EL1 instead of being trapped to EL2.
91 if (kvm_arm_support_pmu_v3()) {
92 write_sysreg(0, pmselr_el0);
93 write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
96 vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
97 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
100 static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
102 write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
104 write_sysreg(0, hstr_el2);
105 if (kvm_arm_support_pmu_v3())
106 write_sysreg(0, pmuserenr_el0);
109 static inline void ___activate_traps(struct kvm_vcpu *vcpu)
111 u64 hcr = vcpu->arch.hcr_el2;
113 if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM))
116 write_sysreg(hcr, hcr_el2);
118 if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
119 write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
122 static inline void ___deactivate_traps(struct kvm_vcpu *vcpu)
125 * If we pended a virtual abort, preserve it until it gets
126 * cleared. See D1.14.3 (Virtual Interrupts) for details, but
127 * the crucial bit is "On taking a vSError interrupt,
128 * HCR_EL2.VSE is cleared to 0."
130 if (vcpu->arch.hcr_el2 & HCR_VSE) {
131 vcpu->arch.hcr_el2 &= ~HCR_VSE;
132 vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE;
136 static inline bool __translate_far_to_hpfar(u64 far, u64 *hpfar)
141 * Resolve the IPA the hard way using the guest VA.
143 * Stage-1 translation already validated the memory access
144 * rights. As such, we can use the EL1 translation regime, and
145 * don't have to distinguish between EL0 and EL1 access.
147 * We do need to save/restore PAR_EL1 though, as we haven't
148 * saved the guest context yet, and we may return early...
150 par = read_sysreg_par();
151 if (!__kvm_at("s1e1r", far))
152 tmp = read_sysreg_par();
154 tmp = SYS_PAR_EL1_F; /* back to the guest */
155 write_sysreg(par, par_el1);
157 if (unlikely(tmp & SYS_PAR_EL1_F))
158 return false; /* Translation failed, back to guest */
160 /* Convert PAR to HPFAR format */
161 *hpfar = PAR_TO_HPFAR(tmp);
165 static inline bool __get_fault_info(u64 esr, struct kvm_vcpu_fault_info *fault)
169 far = read_sysreg_el2(SYS_FAR);
172 * The HPFAR can be invalid if the stage 2 fault did not
173 * happen during a stage 1 page table walk (the ESR_EL2.S1PTW
174 * bit is clear) and one of the two following cases are true:
175 * 1. The fault was due to a permission fault
176 * 2. The processor carries errata 834220
178 * Therefore, for all non S1PTW faults where we either have a
179 * permission fault or the errata workaround is enabled, we
180 * resolve the IPA using the AT instruction.
182 if (!(esr & ESR_ELx_S1PTW) &&
183 (cpus_have_final_cap(ARM64_WORKAROUND_834220) ||
184 (esr & ESR_ELx_FSC_TYPE) == FSC_PERM)) {
185 if (!__translate_far_to_hpfar(far, &hpfar))
188 hpfar = read_sysreg(hpfar_el2);
191 fault->far_el2 = far;
192 fault->hpfar_el2 = hpfar;
196 static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)
201 esr = vcpu->arch.fault.esr_el2;
202 ec = ESR_ELx_EC(esr);
204 if (ec != ESR_ELx_EC_DABT_LOW && ec != ESR_ELx_EC_IABT_LOW)
207 return __get_fault_info(esr, &vcpu->arch.fault);
210 static inline void __hyp_sve_save_host(struct kvm_vcpu *vcpu)
212 struct thread_struct *thread;
214 thread = container_of(vcpu->arch.host_fpsimd_state, struct thread_struct,
217 __sve_save_state(sve_pffr(thread), &vcpu->arch.host_fpsimd_state->fpsr);
220 static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
222 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2);
223 __sve_restore_state(vcpu_sve_pffr(vcpu),
224 &vcpu->arch.ctxt.fp_regs.fpsr);
225 write_sysreg_el1(__vcpu_sys_reg(vcpu, ZCR_EL1), SYS_ZCR);
228 /* Check for an FPSIMD/SVE trap and handle as appropriate */
229 static inline bool __hyp_handle_fpsimd(struct kvm_vcpu *vcpu)
231 bool sve_guest, sve_host;
235 if (!system_supports_fpsimd())
238 if (system_supports_sve()) {
239 sve_guest = vcpu_has_sve(vcpu);
240 sve_host = vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE;
246 esr_ec = kvm_vcpu_trap_get_class(vcpu);
247 if (esr_ec != ESR_ELx_EC_FP_ASIMD &&
248 esr_ec != ESR_ELx_EC_SVE)
251 /* Don't handle SVE traps for non-SVE vcpus here: */
252 if (!sve_guest && esr_ec != ESR_ELx_EC_FP_ASIMD)
255 /* Valid trap. Switch the context: */
257 reg = CPACR_EL1_FPEN;
259 reg |= CPACR_EL1_ZEN;
261 sysreg_clear_set(cpacr_el1, 0, reg);
267 sysreg_clear_set(cptr_el2, reg, 0);
271 if (vcpu->arch.flags & KVM_ARM64_FP_HOST) {
273 __hyp_sve_save_host(vcpu);
275 __fpsimd_save_state(vcpu->arch.host_fpsimd_state);
277 vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
281 __hyp_sve_restore_guest(vcpu);
283 __fpsimd_restore_state(&vcpu->arch.ctxt.fp_regs);
285 /* Skip restoring fpexc32 for AArch64 guests */
286 if (!(read_sysreg(hcr_el2) & HCR_RW))
287 write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
289 vcpu->arch.flags |= KVM_ARM64_FP_ENABLED;
294 static inline bool handle_tx2_tvm(struct kvm_vcpu *vcpu)
296 u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
297 int rt = kvm_vcpu_sys_get_rt(vcpu);
298 u64 val = vcpu_get_reg(vcpu, rt);
301 * The normal sysreg handling code expects to see the traps,
302 * let's not do anything here.
304 if (vcpu->arch.hcr_el2 & HCR_TVM)
309 write_sysreg_el1(val, SYS_SCTLR);
312 write_sysreg_el1(val, SYS_TTBR0);
315 write_sysreg_el1(val, SYS_TTBR1);
318 write_sysreg_el1(val, SYS_TCR);
321 write_sysreg_el1(val, SYS_ESR);
324 write_sysreg_el1(val, SYS_FAR);
327 write_sysreg_el1(val, SYS_AFSR0);
330 write_sysreg_el1(val, SYS_AFSR1);
333 write_sysreg_el1(val, SYS_MAIR);
336 write_sysreg_el1(val, SYS_AMAIR);
338 case SYS_CONTEXTIDR_EL1:
339 write_sysreg_el1(val, SYS_CONTEXTIDR);
345 __kvm_skip_instr(vcpu);
349 static inline bool esr_is_ptrauth_trap(u32 esr)
351 u32 ec = ESR_ELx_EC(esr);
353 if (ec == ESR_ELx_EC_PAC)
356 if (ec != ESR_ELx_EC_SYS64)
359 switch (esr_sys64_to_sysreg(esr)) {
360 case SYS_APIAKEYLO_EL1:
361 case SYS_APIAKEYHI_EL1:
362 case SYS_APIBKEYLO_EL1:
363 case SYS_APIBKEYHI_EL1:
364 case SYS_APDAKEYLO_EL1:
365 case SYS_APDAKEYHI_EL1:
366 case SYS_APDBKEYLO_EL1:
367 case SYS_APDBKEYHI_EL1:
368 case SYS_APGAKEYLO_EL1:
369 case SYS_APGAKEYHI_EL1:
376 #define __ptrauth_save_key(ctxt, key) \
379 __val = read_sysreg_s(SYS_ ## key ## KEYLO_EL1); \
380 ctxt_sys_reg(ctxt, key ## KEYLO_EL1) = __val; \
381 __val = read_sysreg_s(SYS_ ## key ## KEYHI_EL1); \
382 ctxt_sys_reg(ctxt, key ## KEYHI_EL1) = __val; \
385 DECLARE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
387 static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)
389 struct kvm_cpu_context *ctxt;
392 if (!vcpu_has_ptrauth(vcpu) ||
393 !esr_is_ptrauth_trap(kvm_vcpu_get_esr(vcpu)))
396 ctxt = this_cpu_ptr(&kvm_hyp_ctxt);
397 __ptrauth_save_key(ctxt, APIA);
398 __ptrauth_save_key(ctxt, APIB);
399 __ptrauth_save_key(ctxt, APDA);
400 __ptrauth_save_key(ctxt, APDB);
401 __ptrauth_save_key(ctxt, APGA);
403 vcpu_ptrauth_enable(vcpu);
405 val = read_sysreg(hcr_el2);
406 val |= (HCR_API | HCR_APK);
407 write_sysreg(val, hcr_el2);
413 * Return true when we were able to fixup the guest exit and should return to
414 * the guest, false when we should restore the host state and return to the
417 static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
419 if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
420 vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR);
422 if (ARM_SERROR_PENDING(*exit_code)) {
423 u8 esr_ec = kvm_vcpu_trap_get_class(vcpu);
426 * HVC already have an adjusted PC, which we need to
427 * correct in order to return to after having injected
430 * SMC, on the other hand, is *trapped*, meaning its
431 * preferred return address is the SMC itself.
433 if (esr_ec == ESR_ELx_EC_HVC32 || esr_ec == ESR_ELx_EC_HVC64)
434 write_sysreg_el2(read_sysreg_el2(SYS_ELR) - 4, SYS_ELR);
438 * We're using the raw exception code in order to only process
439 * the trap if no SError is pending. We will come back to the
440 * same PC once the SError has been injected, and replay the
441 * trapping instruction.
443 if (*exit_code != ARM_EXCEPTION_TRAP)
446 if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
447 kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 &&
448 handle_tx2_tvm(vcpu))
452 * We trap the first access to the FP/SIMD to save the host context
453 * and restore the guest context lazily.
454 * If FP/SIMD is not implemented, handle the trap and inject an
455 * undefined instruction exception to the guest.
456 * Similarly for trapped SVE accesses.
458 if (__hyp_handle_fpsimd(vcpu))
461 if (__hyp_handle_ptrauth(vcpu))
464 if (!__populate_fault_info(vcpu))
467 if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
470 valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
471 kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
472 kvm_vcpu_dabt_isvalid(vcpu) &&
473 !kvm_vcpu_abt_issea(vcpu) &&
474 !kvm_vcpu_abt_iss1tw(vcpu);
477 int ret = __vgic_v2_perform_cpuif_access(vcpu);
482 /* Promote an illegal access to an SError.*/
484 *exit_code = ARM_EXCEPTION_EL1_SERROR;
490 if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
491 (kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
492 kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
493 int ret = __vgic_v3_perform_cpuif_access(vcpu);
500 /* Return to the host kernel and handle the exit */
504 /* Re-enter the guest */
505 asm(ALTERNATIVE("nop", "dmb sy", ARM64_WORKAROUND_1508412));
509 static inline void __kvm_unexpected_el2_exception(void)
511 extern char __guest_exit_panic[];
512 unsigned long addr, fixup;
513 struct exception_table_entry *entry, *end;
514 unsigned long elr_el2 = read_sysreg(elr_el2);
516 entry = &__start___kvm_ex_table;
517 end = &__stop___kvm_ex_table;
519 while (entry < end) {
520 addr = (unsigned long)&entry->insn + entry->insn;
521 fixup = (unsigned long)&entry->fixup + entry->fixup;
523 if (addr != elr_el2) {
528 write_sysreg(fixup, elr_el2);
532 /* Trigger a panic after restoring the hyp context. */
533 write_sysreg(__guest_exit_panic, elr_el2);
536 #endif /* __ARM64_KVM_HYP_SWITCH_H__ */