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>
19 struct task_struct *p = vcpu->arch.parent_task;
20 struct user_fpsimd_state *fpsimd;
22 if (!is_protected_kvm_enabled() || !p)
25 fpsimd = &p->thread.uw.fpsimd_state;
43 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
52 vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
60 if (is_protected_kvm_enabled()) {
61 get_task_struct(current);
62 vcpu->arch.parent_task = current;
79 if (!system_supports_fpsimd())
89 vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
91 vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
92 if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
93 vcpu_set_flag(vcpu, HOST_SVE_ENABLED);
95 if (system_supports_sme()) {
96 vcpu_clear_flag(vcpu, HOST_SME_ENABLED);
97 if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
98 vcpu_set_flag(vcpu, HOST_SME_ENABLED);
112 if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
113 vcpu->arch.fp_state = FP_STATE_FREE;
114 fpsimd_save_and_flush_cpu_state();
128 if (test_thread_flag(TIF_FOREIGN_FPSTATE))
129 vcpu->arch.fp_state = FP_STATE_FREE;
141 struct cpu_fp_state fp_state;
143 WARN_ON_ONCE(!irqs_disabled());
145 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
151 fp_state.st = &vcpu->arch.ctxt.fp_regs;
152 fp_state.sve_state = vcpu->arch.sve_state;
153 fp_state.sve_vl = vcpu->arch.sve_max_vl;
154 fp_state.sme_state = NULL;
155 fp_state.svcr = &vcpu->arch.svcr;
156 fp_state.fp_type = &vcpu->arch.fp_type;
158 if (vcpu_has_sve(vcpu))
159 fp_state.to_save = FP_STATE_SVE;
161 fp_state.to_save = FP_STATE_FPSIMD;
163 fpsimd_bind_state_to_cpu(&fp_state);
165 clear_thread_flag(TIF_FOREIGN_FPSTATE);
179 local_irq_save(flags);
185 if (has_vhe() && system_supports_sme()) {
187 if (vcpu_get_flag(vcpu, HOST_SME_ENABLED))
188 sysreg_clear_set(CPACR_EL1, 0,
189 CPACR_EL1_SMEN_EL0EN |
190 CPACR_EL1_SMEN_EL1EN);
192 sysreg_clear_set(CPACR_EL1,
193 CPACR_EL1_SMEN_EL0EN,
194 CPACR_EL1_SMEN_EL1EN);
198 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
199 if (vcpu_has_sve(vcpu)) {
200 __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
204 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
208 fpsimd_save_and_flush_cpu_state();
209 }
else if (has_vhe() && system_supports_sve()) {
217 if (vcpu_get_flag(vcpu, HOST_SVE_ENABLED))
218 sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
220 sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
223 local_irq_restore(flags);
void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
int kvm_share_hyp(void *from, void *to)
void kvm_unshare_hyp(void *from, void *to)