1e6b673b7SDave Martin // SPDX-License-Identifier: GPL-2.0 2e6b673b7SDave Martin /* 3e6b673b7SDave Martin * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers 4e6b673b7SDave Martin * 5e6b673b7SDave Martin * Copyright 2018 Arm Limited 6e6b673b7SDave Martin * Author: Dave Martin <Dave.Martin@arm.com> 7e6b673b7SDave Martin */ 8b045e4d0SDave Martin #include <linux/irqflags.h> 9e6b673b7SDave Martin #include <linux/sched.h> 10e6b673b7SDave Martin #include <linux/kvm_host.h> 1104950674SDave Martin #include <asm/fpsimd.h> 12e6b673b7SDave Martin #include <asm/kvm_asm.h> 1383857371SMarc Zyngier #include <asm/kvm_hyp.h> 14e6b673b7SDave Martin #include <asm/kvm_mmu.h> 15b3eb56b6SDave Martin #include <asm/sysreg.h> 16e6b673b7SDave Martin 1752b28657SQuentin Perret void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu) 1852b28657SQuentin Perret { 1952b28657SQuentin Perret struct task_struct *p = vcpu->arch.parent_task; 2052b28657SQuentin Perret struct user_fpsimd_state *fpsimd; 2152b28657SQuentin Perret 2252b28657SQuentin Perret if (!is_protected_kvm_enabled() || !p) 2352b28657SQuentin Perret return; 2452b28657SQuentin Perret 2552b28657SQuentin Perret fpsimd = &p->thread.uw.fpsimd_state; 2652b28657SQuentin Perret kvm_unshare_hyp(fpsimd, fpsimd + 1); 2752b28657SQuentin Perret put_task_struct(p); 2852b28657SQuentin Perret } 2952b28657SQuentin Perret 30e6b673b7SDave Martin /* 31e6b673b7SDave Martin * Called on entry to KVM_RUN unless this vcpu previously ran at least 32e6b673b7SDave Martin * once and the most recent prior KVM_RUN for this vcpu was called from 33e6b673b7SDave Martin * the same task as current (highly likely). 34e6b673b7SDave Martin * 35e6b673b7SDave Martin * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu), 36e6b673b7SDave Martin * such that on entering hyp the relevant parts of current are already 37e6b673b7SDave Martin * mapped. 38e6b673b7SDave Martin */ 39e6b673b7SDave Martin int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) 40e6b673b7SDave Martin { 41e6b673b7SDave Martin int ret; 42e6b673b7SDave Martin 43e6b673b7SDave Martin struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state; 44e6b673b7SDave Martin 4552b28657SQuentin Perret kvm_vcpu_unshare_task_fp(vcpu); 4652b28657SQuentin Perret 47bee14bcaSMarc Zyngier /* Make sure the host task fpsimd state is visible to hyp: */ 483f868e14SQuentin Perret ret = kvm_share_hyp(fpsimd, fpsimd + 1); 4952b28657SQuentin Perret if (ret) 5052b28657SQuentin Perret return ret; 5152b28657SQuentin Perret 52e6b673b7SDave Martin vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd); 532d761dbfSMarc Zyngier 5452b28657SQuentin Perret /* 5552b28657SQuentin Perret * We need to keep current's task_struct pinned until its data has been 5652b28657SQuentin Perret * unshared with the hypervisor to make sure it is not re-used by the 5752b28657SQuentin Perret * kernel and donated to someone else while already shared -- see 5852b28657SQuentin Perret * kvm_vcpu_unshare_task_fp() for the matching put_task_struct(). 5952b28657SQuentin Perret */ 6052b28657SQuentin Perret if (is_protected_kvm_enabled()) { 6152b28657SQuentin Perret get_task_struct(current); 6252b28657SQuentin Perret vcpu->arch.parent_task = current; 6352b28657SQuentin Perret } 6452b28657SQuentin Perret 6552b28657SQuentin Perret return 0; 66e6b673b7SDave Martin } 67e6b673b7SDave Martin 68e6b673b7SDave Martin /* 69e6b673b7SDave Martin * Prepare vcpu for saving the host's FPSIMD state and loading the guest's. 70e6b673b7SDave Martin * The actual loading is done by the FPSIMD access trap taken to hyp. 71e6b673b7SDave Martin * 72e6b673b7SDave Martin * Here, we just set the correct metadata to indicate that the FPSIMD 73e6b673b7SDave Martin * state in the cpu regs (if any) belongs to current on the host. 74e6b673b7SDave Martin */ 75e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu) 76e6b673b7SDave Martin { 77e6b673b7SDave Martin BUG_ON(!current->mm); 78e6b673b7SDave Martin 79b4da9187SMarc Zyngier if (!system_supports_fpsimd()) 80b4da9187SMarc Zyngier return; 81b4da9187SMarc Zyngier 8293ae6b01SMark Brown fpsimd_kvm_prepare(); 8393ae6b01SMark Brown 844c181e3dSMark Brown /* 854c181e3dSMark Brown * We will check TIF_FOREIGN_FPSTATE just before entering the 864c181e3dSMark Brown * guest in kvm_arch_vcpu_ctxflush_fp() and override this to 874c181e3dSMark Brown * FP_STATE_FREE if the flag set. 884c181e3dSMark Brown */ 89f8077b0dSMarc Zyngier vcpu->arch.fp_state = FP_STATE_HOST_OWNED; 90b3eb56b6SDave Martin 910affa37fSMarc Zyngier vcpu_clear_flag(vcpu, HOST_SVE_ENABLED); 92b3eb56b6SDave Martin if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN) 930affa37fSMarc Zyngier vcpu_set_flag(vcpu, HOST_SVE_ENABLED); 94861262abSMark Brown 95861262abSMark Brown if (system_supports_sme()) { 960affa37fSMarc Zyngier vcpu_clear_flag(vcpu, HOST_SME_ENABLED); 97861262abSMark Brown if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN) 980affa37fSMarc Zyngier vcpu_set_flag(vcpu, HOST_SME_ENABLED); 99861262abSMark Brown 100*aaa2f14eSMark Brown /* 101*aaa2f14eSMark Brown * If PSTATE.SM is enabled then save any pending FP 102*aaa2f14eSMark Brown * state and disable PSTATE.SM. If we leave PSTATE.SM 103*aaa2f14eSMark Brown * enabled and the guest does not enable SME via 104*aaa2f14eSMark Brown * CPACR_EL1.SMEN then operations that should be valid 105*aaa2f14eSMark Brown * may generate SME traps from EL1 to EL1 which we 106*aaa2f14eSMark Brown * can't intercept and which would confuse the guest. 107*aaa2f14eSMark Brown * 108*aaa2f14eSMark Brown * Do the same for PSTATE.ZA in the case where there 109*aaa2f14eSMark Brown * is state in the registers which has not already 110*aaa2f14eSMark Brown * been saved, this is very unlikely to happen. 111*aaa2f14eSMark Brown */ 112f8077b0dSMarc Zyngier if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) { 113f8077b0dSMarc Zyngier vcpu->arch.fp_state = FP_STATE_FREE; 114861262abSMark Brown fpsimd_save_and_flush_cpu_state(); 115861262abSMark Brown } 116861262abSMark Brown } 117e6b673b7SDave Martin } 118e6b673b7SDave Martin 11923afc825SMark Brown /* 120e9ada6c2SMarc Zyngier * Called just before entering the guest once we are no longer preemptable 121e9ada6c2SMarc Zyngier * and interrupts are disabled. If we have managed to run anything using 122e9ada6c2SMarc Zyngier * FP while we were preemptible (such as off the back of an interrupt), 123e9ada6c2SMarc Zyngier * then neither the host nor the guest own the FP hardware (and it was the 124e9ada6c2SMarc Zyngier * responsibility of the code that used FP to save the existing state). 12523afc825SMark Brown */ 126af9a0e21SMarc Zyngier void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu) 127af9a0e21SMarc Zyngier { 128b4da9187SMarc Zyngier if (test_thread_flag(TIF_FOREIGN_FPSTATE)) 129f8077b0dSMarc Zyngier vcpu->arch.fp_state = FP_STATE_FREE; 130af9a0e21SMarc Zyngier } 131af9a0e21SMarc Zyngier 132e6b673b7SDave Martin /* 13323afc825SMark Brown * Called just after exiting the guest. If the guest FPSIMD state 13423afc825SMark Brown * was loaded, update the host's context tracking data mark the CPU 13523afc825SMark Brown * FPSIMD regs as dirty and belonging to vcpu so that they will be 13623afc825SMark Brown * written back if the kernel clobbers them due to kernel-mode NEON 13723afc825SMark Brown * before re-entry into the guest. 138e6b673b7SDave Martin */ 139e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu) 140e6b673b7SDave Martin { 1411192b93bSMark Brown struct cpu_fp_state fp_state; 142deeb8f9aSMark Brown 143e6b673b7SDave Martin WARN_ON_ONCE(!irqs_disabled()); 144e6b673b7SDave Martin 145f8077b0dSMarc Zyngier if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) { 146deeb8f9aSMark Brown 147b40c559bSMark Brown /* 148b40c559bSMark Brown * Currently we do not support SME guests so SVCR is 149b40c559bSMark Brown * always 0 and we just need a variable to point to. 150b40c559bSMark Brown */ 1511192b93bSMark Brown fp_state.st = &vcpu->arch.ctxt.fp_regs; 1521192b93bSMark Brown fp_state.sve_state = vcpu->arch.sve_state; 1531192b93bSMark Brown fp_state.sve_vl = vcpu->arch.sve_max_vl; 154ce514000SMark Brown fp_state.sme_state = NULL; 1551192b93bSMark Brown fp_state.svcr = &vcpu->arch.svcr; 1561192b93bSMark Brown fp_state.fp_type = &vcpu->arch.fp_type; 1571192b93bSMark Brown 1581192b93bSMark Brown if (vcpu_has_sve(vcpu)) 1591192b93bSMark Brown fp_state.to_save = FP_STATE_SVE; 1601192b93bSMark Brown else 1611192b93bSMark Brown fp_state.to_save = FP_STATE_FPSIMD; 1621192b93bSMark Brown 1631192b93bSMark Brown fpsimd_bind_state_to_cpu(&fp_state); 16404950674SDave Martin 165e6b673b7SDave Martin clear_thread_flag(TIF_FOREIGN_FPSTATE); 166e6b673b7SDave Martin } 167e6b673b7SDave Martin } 168e6b673b7SDave Martin 169e6b673b7SDave Martin /* 170e6b673b7SDave Martin * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the 171e6b673b7SDave Martin * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu 172e6b673b7SDave Martin * disappears and another task or vcpu appears that recycles the same 173e6b673b7SDave Martin * struct fpsimd_state. 174e6b673b7SDave Martin */ 175e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) 176e6b673b7SDave Martin { 177b045e4d0SDave Martin unsigned long flags; 178b045e4d0SDave Martin 179b045e4d0SDave Martin local_irq_save(flags); 180e6b673b7SDave Martin 181861262abSMark Brown /* 182861262abSMark Brown * If we have VHE then the Hyp code will reset CPACR_EL1 to 183861262abSMark Brown * CPACR_EL1_DEFAULT and we need to reenable SME. 184861262abSMark Brown */ 185861262abSMark Brown if (has_vhe() && system_supports_sme()) { 186861262abSMark Brown /* Also restore EL0 state seen on entry */ 1870affa37fSMarc Zyngier if (vcpu_get_flag(vcpu, HOST_SME_ENABLED)) 188861262abSMark Brown sysreg_clear_set(CPACR_EL1, 0, 189861262abSMark Brown CPACR_EL1_SMEN_EL0EN | 190861262abSMark Brown CPACR_EL1_SMEN_EL1EN); 191861262abSMark Brown else 192861262abSMark Brown sysreg_clear_set(CPACR_EL1, 193861262abSMark Brown CPACR_EL1_SMEN_EL0EN, 194861262abSMark Brown CPACR_EL1_SMEN_EL1EN); 19559d78a2eSNianyao Tang isb(); 196861262abSMark Brown } 197861262abSMark Brown 198f8077b0dSMarc Zyngier if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) { 1998383741aSMarc Zyngier if (vcpu_has_sve(vcpu)) { 20083857371SMarc Zyngier __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR); 201b145a843SMarc Zyngier 2028c8010d6SMarc Zyngier /* Restore the VL that was saved when bound to the CPU */ 2038c8010d6SMarc Zyngier if (!has_vhe()) 2048c8010d6SMarc Zyngier sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, 2058c8010d6SMarc Zyngier SYS_ZCR_EL1); 2068c8010d6SMarc Zyngier } 2078c8010d6SMarc Zyngier 208b145a843SMarc Zyngier fpsimd_save_and_flush_cpu_state(); 2098383741aSMarc Zyngier } else if (has_vhe() && system_supports_sve()) { 210b3eb56b6SDave Martin /* 211b3eb56b6SDave Martin * The FPSIMD/SVE state in the CPU has not been touched, and we 212b3eb56b6SDave Martin * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been 213b3eb56b6SDave Martin * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE 214b3eb56b6SDave Martin * for EL0. To avoid spurious traps, restore the trap state 215b3eb56b6SDave Martin * seen by kvm_arch_vcpu_load_fp(): 216b3eb56b6SDave Martin */ 2170affa37fSMarc Zyngier if (vcpu_get_flag(vcpu, HOST_SVE_ENABLED)) 218b3eb56b6SDave Martin sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN); 219b3eb56b6SDave Martin else 220b3eb56b6SDave Martin sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0); 221e6b673b7SDave Martin } 222e6b673b7SDave Martin 223b045e4d0SDave Martin local_irq_restore(flags); 224e6b673b7SDave Martin } 225