1 // SPDX-License-Identifier: GPL-2.0
3 * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
5 * Copyright 2018 Arm Limited
6 * Author: Dave Martin <Dave.Martin@arm.com>
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>
17 void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
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;
26 kvm_unshare_hyp(fpsimd, fpsimd + 1);
31 * Called on entry to KVM_RUN unless this vcpu previously ran at least
32 * once and the most recent prior KVM_RUN for this vcpu was called from
33 * the same task as current (highly likely).
35 * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
36 * such that on entering hyp the relevant parts of current are already
39 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
43 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
45 kvm_vcpu_unshare_task_fp(vcpu);
47 /* Make sure the host task fpsimd state is visible to hyp: */
48 ret = kvm_share_hyp(fpsimd, fpsimd + 1);
52 vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
55 * We need to keep current's task_struct pinned until its data has been
56 * unshared with the hypervisor to make sure it is not re-used by the
57 * kernel and donated to someone else while already shared -- see
58 * kvm_vcpu_unshare_task_fp() for the matching put_task_struct().
60 if (is_protected_kvm_enabled()) {
61 get_task_struct(current);
62 vcpu->arch.parent_task = current;
69 * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
70 * The actual loading is done by the FPSIMD access trap taken to hyp.
72 * Here, we just set the correct metadata to indicate that the FPSIMD
73 * state in the cpu regs (if any) belongs to current on the host.
75 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
78 BUG_ON(test_thread_flag(TIF_SVE));
80 vcpu->arch.flags &= ~KVM_ARM64_FP_ENABLED;
81 vcpu->arch.flags |= KVM_ARM64_FP_HOST;
83 if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
84 vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
87 * We don't currently support SME guests but if we leave
88 * things in streaming mode then when the guest starts running
89 * FPSIMD or SVE code it may generate SME traps so as a
90 * special case if we are in streaming mode we force the host
91 * state to be saved now and exit streaming mode so that we
92 * don't have to handle any SME traps for valid guest
93 * operations. Do this for ZA as well for now for simplicity.
95 if (system_supports_sme()) {
96 if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
97 vcpu->arch.flags |= KVM_ARM64_HOST_SME_ENABLED;
99 if (read_sysreg_s(SYS_SVCR) &
100 (SVCR_SM_MASK | SVCR_ZA_MASK)) {
101 vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
102 fpsimd_save_and_flush_cpu_state();
108 * Called just before entering the guest once we are no longer
109 * preemptable. Syncs the host's TIF_FOREIGN_FPSTATE with the KVM
110 * mirror of the flag used by the hypervisor.
112 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
114 if (test_thread_flag(TIF_FOREIGN_FPSTATE))
115 vcpu->arch.flags |= KVM_ARM64_FP_FOREIGN_FPSTATE;
117 vcpu->arch.flags &= ~KVM_ARM64_FP_FOREIGN_FPSTATE;
121 * Called just after exiting the guest. If the guest FPSIMD state
122 * was loaded, update the host's context tracking data mark the CPU
123 * FPSIMD regs as dirty and belonging to vcpu so that they will be
124 * written back if the kernel clobbers them due to kernel-mode NEON
125 * before re-entry into the guest.
127 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
129 WARN_ON_ONCE(!irqs_disabled());
131 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
133 * Currently we do not support SME guests so SVCR is
134 * always 0 and we just need a variable to point to.
136 fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
137 vcpu->arch.sve_state,
138 vcpu->arch.sve_max_vl,
139 NULL, 0, &vcpu->arch.svcr);
141 clear_thread_flag(TIF_FOREIGN_FPSTATE);
142 update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
147 * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
148 * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
149 * disappears and another task or vcpu appears that recycles the same
150 * struct fpsimd_state.
152 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
156 local_irq_save(flags);
159 * If we have VHE then the Hyp code will reset CPACR_EL1 to
160 * CPACR_EL1_DEFAULT and we need to reenable SME.
162 if (has_vhe() && system_supports_sme()) {
163 /* Also restore EL0 state seen on entry */
164 if (vcpu->arch.flags & KVM_ARM64_HOST_SME_ENABLED)
165 sysreg_clear_set(CPACR_EL1, 0,
166 CPACR_EL1_SMEN_EL0EN |
167 CPACR_EL1_SMEN_EL1EN);
169 sysreg_clear_set(CPACR_EL1,
170 CPACR_EL1_SMEN_EL0EN,
171 CPACR_EL1_SMEN_EL1EN);
174 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
175 if (vcpu_has_sve(vcpu)) {
176 __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
178 /* Restore the VL that was saved when bound to the CPU */
180 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
184 fpsimd_save_and_flush_cpu_state();
185 } else if (has_vhe() && system_supports_sve()) {
187 * The FPSIMD/SVE state in the CPU has not been touched, and we
188 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
189 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
190 * for EL0. To avoid spurious traps, restore the trap state
191 * seen by kvm_arch_vcpu_load_fp():
193 if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
194 sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
196 sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
199 update_thread_flag(TIF_SVE, 0);
201 local_irq_restore(flags);