Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
0004  *
0005  * Copyright 2018 Arm Limited
0006  * Author: Dave Martin <Dave.Martin@arm.com>
0007  */
0008 #include <linux/irqflags.h>
0009 #include <linux/sched.h>
0010 #include <linux/kvm_host.h>
0011 #include <asm/fpsimd.h>
0012 #include <asm/kvm_asm.h>
0013 #include <asm/kvm_hyp.h>
0014 #include <asm/kvm_mmu.h>
0015 #include <asm/sysreg.h>
0016 
0017 void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
0018 {
0019     struct task_struct *p = vcpu->arch.parent_task;
0020     struct user_fpsimd_state *fpsimd;
0021 
0022     if (!is_protected_kvm_enabled() || !p)
0023         return;
0024 
0025     fpsimd = &p->thread.uw.fpsimd_state;
0026     kvm_unshare_hyp(fpsimd, fpsimd + 1);
0027     put_task_struct(p);
0028 }
0029 
0030 /*
0031  * Called on entry to KVM_RUN unless this vcpu previously ran at least
0032  * once and the most recent prior KVM_RUN for this vcpu was called from
0033  * the same task as current (highly likely).
0034  *
0035  * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
0036  * such that on entering hyp the relevant parts of current are already
0037  * mapped.
0038  */
0039 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
0040 {
0041     int ret;
0042 
0043     struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
0044 
0045     kvm_vcpu_unshare_task_fp(vcpu);
0046 
0047     /* Make sure the host task fpsimd state is visible to hyp: */
0048     ret = kvm_share_hyp(fpsimd, fpsimd + 1);
0049     if (ret)
0050         return ret;
0051 
0052     vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
0053 
0054     /*
0055      * We need to keep current's task_struct pinned until its data has been
0056      * unshared with the hypervisor to make sure it is not re-used by the
0057      * kernel and donated to someone else while already shared -- see
0058      * kvm_vcpu_unshare_task_fp() for the matching put_task_struct().
0059      */
0060     if (is_protected_kvm_enabled()) {
0061         get_task_struct(current);
0062         vcpu->arch.parent_task = current;
0063     }
0064 
0065     return 0;
0066 }
0067 
0068 /*
0069  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
0070  * The actual loading is done by the FPSIMD access trap taken to hyp.
0071  *
0072  * Here, we just set the correct metadata to indicate that the FPSIMD
0073  * state in the cpu regs (if any) belongs to current on the host.
0074  */
0075 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
0076 {
0077     BUG_ON(!current->mm);
0078     BUG_ON(test_thread_flag(TIF_SVE));
0079 
0080     if (!system_supports_fpsimd())
0081         return;
0082 
0083     vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
0084 
0085     vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
0086     if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
0087         vcpu_set_flag(vcpu, HOST_SVE_ENABLED);
0088 
0089     /*
0090      * We don't currently support SME guests but if we leave
0091      * things in streaming mode then when the guest starts running
0092      * FPSIMD or SVE code it may generate SME traps so as a
0093      * special case if we are in streaming mode we force the host
0094      * state to be saved now and exit streaming mode so that we
0095      * don't have to handle any SME traps for valid guest
0096      * operations. Do this for ZA as well for now for simplicity.
0097      */
0098     if (system_supports_sme()) {
0099         vcpu_clear_flag(vcpu, HOST_SME_ENABLED);
0100         if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
0101             vcpu_set_flag(vcpu, HOST_SME_ENABLED);
0102 
0103         if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
0104             vcpu->arch.fp_state = FP_STATE_FREE;
0105             fpsimd_save_and_flush_cpu_state();
0106         }
0107     }
0108 }
0109 
0110 /*
0111  * Called just before entering the guest once we are no longer preemptable
0112  * and interrupts are disabled. If we have managed to run anything using
0113  * FP while we were preemptible (such as off the back of an interrupt),
0114  * then neither the host nor the guest own the FP hardware (and it was the
0115  * responsibility of the code that used FP to save the existing state).
0116  */
0117 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
0118 {
0119     if (test_thread_flag(TIF_FOREIGN_FPSTATE))
0120         vcpu->arch.fp_state = FP_STATE_FREE;
0121 }
0122 
0123 /*
0124  * Called just after exiting the guest. If the guest FPSIMD state
0125  * was loaded, update the host's context tracking data mark the CPU
0126  * FPSIMD regs as dirty and belonging to vcpu so that they will be
0127  * written back if the kernel clobbers them due to kernel-mode NEON
0128  * before re-entry into the guest.
0129  */
0130 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
0131 {
0132     WARN_ON_ONCE(!irqs_disabled());
0133 
0134     if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
0135         /*
0136          * Currently we do not support SME guests so SVCR is
0137          * always 0 and we just need a variable to point to.
0138          */
0139         fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
0140                      vcpu->arch.sve_state,
0141                      vcpu->arch.sve_max_vl,
0142                      NULL, 0, &vcpu->arch.svcr);
0143 
0144         clear_thread_flag(TIF_FOREIGN_FPSTATE);
0145         update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
0146     }
0147 }
0148 
0149 /*
0150  * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
0151  * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
0152  * disappears and another task or vcpu appears that recycles the same
0153  * struct fpsimd_state.
0154  */
0155 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
0156 {
0157     unsigned long flags;
0158 
0159     local_irq_save(flags);
0160 
0161     /*
0162      * If we have VHE then the Hyp code will reset CPACR_EL1 to
0163      * CPACR_EL1_DEFAULT and we need to reenable SME.
0164      */
0165     if (has_vhe() && system_supports_sme()) {
0166         /* Also restore EL0 state seen on entry */
0167         if (vcpu_get_flag(vcpu, HOST_SME_ENABLED))
0168             sysreg_clear_set(CPACR_EL1, 0,
0169                      CPACR_EL1_SMEN_EL0EN |
0170                      CPACR_EL1_SMEN_EL1EN);
0171         else
0172             sysreg_clear_set(CPACR_EL1,
0173                      CPACR_EL1_SMEN_EL0EN,
0174                      CPACR_EL1_SMEN_EL1EN);
0175     }
0176 
0177     if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
0178         if (vcpu_has_sve(vcpu)) {
0179             __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
0180 
0181             /* Restore the VL that was saved when bound to the CPU */
0182             if (!has_vhe())
0183                 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
0184                                SYS_ZCR_EL1);
0185         }
0186 
0187         fpsimd_save_and_flush_cpu_state();
0188     } else if (has_vhe() && system_supports_sve()) {
0189         /*
0190          * The FPSIMD/SVE state in the CPU has not been touched, and we
0191          * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
0192          * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
0193          * for EL0.  To avoid spurious traps, restore the trap state
0194          * seen by kvm_arch_vcpu_load_fp():
0195          */
0196         if (vcpu_get_flag(vcpu, HOST_SVE_ENABLED))
0197             sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
0198         else
0199             sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
0200     }
0201 
0202     update_thread_flag(TIF_SVE, 0);
0203 
0204     local_irq_restore(flags);
0205 }