1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers 4 * 5 * Copyright 2018 Arm Limited 6 * Author: Dave Martin <Dave.Martin@arm.com> 7 */ 8 #include <linux/irqflags.h> 9 #include <linux/sched.h> 10 #include <linux/kvm_host.h> 11 #include <asm/fpsimd.h> 12 #include <asm/kvm_asm.h> 13 #include <asm/kvm_hyp.h> 14 #include <asm/kvm_mmu.h> 15 #include <asm/sysreg.h> 16 17 /* 18 * Prepare vcpu for saving the host's FPSIMD state and loading the guest's. 19 * The actual loading is done by the FPSIMD access trap taken to hyp. 20 * 21 * Here, we just set the correct metadata to indicate that the FPSIMD 22 * state in the cpu regs (if any) belongs to current on the host. 23 */ 24 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu) 25 { 26 BUG_ON(!current->mm); 27 28 if (!system_supports_fpsimd()) 29 return; 30 31 /* 32 * Avoid needless save/restore of the guest's common 33 * FPSIMD/SVE/SME regs during transitions between L1/L2. 34 * 35 * These transitions only happens in a non-preemptible context 36 * where the host regs have already been saved and unbound. The 37 * live registers are either free or owned by the guest. 38 */ 39 if (vcpu_get_flag(vcpu, IN_NESTED_ERET) || 40 vcpu_get_flag(vcpu, IN_NESTED_EXCEPTION)) { 41 WARN_ON_ONCE(host_owns_fp_regs()); 42 return; 43 } 44 45 /* 46 * Ensure that any host FPSIMD/SVE/SME state is saved and unbound such 47 * that the host kernel is responsible for restoring this state upon 48 * return to userspace, and the hyp code doesn't need to save anything. 49 * 50 * When the host may use SME, fpsimd_save_and_flush_cpu_state() ensures 51 * that PSTATE.{SM,ZA} == {0,0}. 52 */ 53 fpsimd_save_and_flush_cpu_state(); 54 *host_data_ptr(fp_owner) = FP_STATE_FREE; 55 56 WARN_ON_ONCE(system_supports_sme() && read_sysreg_s(SYS_SVCR)); 57 } 58 59 /* 60 * Called just before entering the guest once we are no longer preemptible 61 * and interrupts are disabled. If we have managed to run anything using 62 * FP while we were preemptible (such as off the back of an interrupt), 63 * then neither the host nor the guest own the FP hardware (and it was the 64 * responsibility of the code that used FP to save the existing state). 65 */ 66 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu) 67 { 68 if (test_thread_flag(TIF_FOREIGN_FPSTATE)) 69 *host_data_ptr(fp_owner) = FP_STATE_FREE; 70 } 71 72 /* 73 * Called just after exiting the guest. If the guest FPSIMD state 74 * was loaded, update the host's context tracking data mark the CPU 75 * FPSIMD regs as dirty and belonging to vcpu so that they will be 76 * written back if the kernel clobbers them due to kernel-mode NEON 77 * before re-entry into the guest. 78 */ 79 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu) 80 { 81 struct cpu_fp_state fp_state; 82 83 WARN_ON_ONCE(!irqs_disabled()); 84 85 if (guest_owns_fp_regs()) { 86 /* 87 * Currently we do not support SME guests so SVCR is 88 * always 0 and we just need a variable to point to. 89 */ 90 fp_state.st = &vcpu->arch.ctxt.fp_regs; 91 fp_state.sve_state = vcpu->arch.sve_state; 92 fp_state.sve_vl = vcpu->arch.sve_max_vl; 93 fp_state.sme_state = NULL; 94 fp_state.svcr = __ctxt_sys_reg(&vcpu->arch.ctxt, SVCR); 95 fp_state.fpmr = __ctxt_sys_reg(&vcpu->arch.ctxt, FPMR); 96 fp_state.fp_type = &vcpu->arch.fp_type; 97 98 if (vcpu_has_sve(vcpu)) 99 fp_state.to_save = FP_STATE_SVE; 100 else 101 fp_state.to_save = FP_STATE_FPSIMD; 102 103 fpsimd_bind_state_to_cpu(&fp_state); 104 105 clear_thread_flag(TIF_FOREIGN_FPSTATE); 106 } 107 } 108 109 /* 110 * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the 111 * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu 112 * disappears and another task or vcpu appears that recycles the same 113 * struct fpsimd_state. 114 */ 115 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) 116 { 117 unsigned long flags; 118 119 /* 120 * See comment in kvm_arch_vcpu_load_fp(). Note that we also rely on 121 * the guest's max VL to have been set by fpsimd_lazy_switch_to_host() 122 * so that any intervening kernel-mode SIMD (NEON or otherwise) 123 * operation sees the full guest state that needs saving. 124 */ 125 if (vcpu_get_flag(vcpu, IN_NESTED_ERET) || 126 vcpu_get_flag(vcpu, IN_NESTED_EXCEPTION)) { 127 WARN_ON_ONCE(host_owns_fp_regs()); 128 return; 129 } 130 131 local_irq_save(flags); 132 133 if (guest_owns_fp_regs()) { 134 /* 135 * Flush (save and invalidate) the fpsimd/sve state so that if 136 * the host tries to use fpsimd/sve, it's not using stale data 137 * from the guest. 138 * 139 * Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the 140 * context unconditionally, in both nVHE and VHE. This allows 141 * the kernel to restore the fpsimd/sve state, including ZCR_EL1 142 * when needed. 143 */ 144 fpsimd_save_and_flush_cpu_state(); 145 } 146 147 local_irq_restore(flags); 148 } 149