0001
0002
0003
0004
0005
0006
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
0032
0033
0034
0035
0036
0037
0038
0039 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
0040 {
0041 int ret;
0042
0043 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
0044
0045 kvm_vcpu_unshare_task_fp(vcpu);
0046
0047
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
0056
0057
0058
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
0070
0071
0072
0073
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
0091
0092
0093
0094
0095
0096
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
0112
0113
0114
0115
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
0125
0126
0127
0128
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
0137
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
0151
0152
0153
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
0163
0164
0165 if (has_vhe() && system_supports_sme()) {
0166
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
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
0191
0192
0193
0194
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 }