1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
4 *
5 * Copyright 2018 Arm Limited
6 * Author: Dave Martin <Dave.Martin@arm.com>
7 */
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>
16
kvm_vcpu_unshare_task_fp(struct kvm_vcpu * vcpu)17 void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
18 {
19 struct task_struct *p = vcpu->arch.parent_task;
20 struct user_fpsimd_state *fpsimd;
21
22 if (!is_protected_kvm_enabled() || !p)
23 return;
24
25 fpsimd = &p->thread.uw.fpsimd_state;
26 kvm_unshare_hyp(fpsimd, fpsimd + 1);
27 put_task_struct(p);
28 }
29
30 /*
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).
34 *
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
37 * mapped.
38 */
kvm_arch_vcpu_run_map_fp(struct kvm_vcpu * vcpu)39 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
40 {
41 int ret;
42
43 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
44
45 kvm_vcpu_unshare_task_fp(vcpu);
46
47 /* Make sure the host task fpsimd state is visible to hyp: */
48 ret = kvm_share_hyp(fpsimd, fpsimd + 1);
49 if (ret)
50 return ret;
51
52 vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
53
54 /*
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().
59 */
60 if (is_protected_kvm_enabled()) {
61 get_task_struct(current);
62 vcpu->arch.parent_task = current;
63 }
64
65 return 0;
66 }
67
68 /*
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.
71 *
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.
74 */
kvm_arch_vcpu_load_fp(struct kvm_vcpu * vcpu)75 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
76 {
77 BUG_ON(!current->mm);
78
79 if (!system_supports_fpsimd())
80 return;
81
82 fpsimd_kvm_prepare();
83
84 vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
85
86 vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
87 if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
88 vcpu_set_flag(vcpu, HOST_SVE_ENABLED);
89
90 /*
91 * We don't currently support SME guests but if we leave
92 * things in streaming mode then when the guest starts running
93 * FPSIMD or SVE code it may generate SME traps so as a
94 * special case if we are in streaming mode we force the host
95 * state to be saved now and exit streaming mode so that we
96 * don't have to handle any SME traps for valid guest
97 * operations. Do this for ZA as well for now for simplicity.
98 */
99 if (system_supports_sme()) {
100 vcpu_clear_flag(vcpu, HOST_SME_ENABLED);
101 if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
102 vcpu_set_flag(vcpu, HOST_SME_ENABLED);
103
104 if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
105 vcpu->arch.fp_state = FP_STATE_FREE;
106 fpsimd_save_and_flush_cpu_state();
107 }
108 }
109 }
110
111 /*
112 * Called just before entering the guest once we are no longer preemptable
113 * and interrupts are disabled. If we have managed to run anything using
114 * FP while we were preemptible (such as off the back of an interrupt),
115 * then neither the host nor the guest own the FP hardware (and it was the
116 * responsibility of the code that used FP to save the existing state).
117 */
kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu * vcpu)118 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
119 {
120 if (test_thread_flag(TIF_FOREIGN_FPSTATE))
121 vcpu->arch.fp_state = FP_STATE_FREE;
122 }
123
124 /*
125 * Called just after exiting the guest. If the guest FPSIMD state
126 * was loaded, update the host's context tracking data mark the CPU
127 * FPSIMD regs as dirty and belonging to vcpu so that they will be
128 * written back if the kernel clobbers them due to kernel-mode NEON
129 * before re-entry into the guest.
130 */
kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu * vcpu)131 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
132 {
133 struct cpu_fp_state fp_state;
134
135 WARN_ON_ONCE(!irqs_disabled());
136
137 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
138
139 /*
140 * Currently we do not support SME guests so SVCR is
141 * always 0 and we just need a variable to point to.
142 */
143 fp_state.st = &vcpu->arch.ctxt.fp_regs;
144 fp_state.sve_state = vcpu->arch.sve_state;
145 fp_state.sve_vl = vcpu->arch.sve_max_vl;
146 fp_state.sme_state = NULL;
147 fp_state.svcr = &vcpu->arch.svcr;
148 fp_state.fp_type = &vcpu->arch.fp_type;
149
150 if (vcpu_has_sve(vcpu))
151 fp_state.to_save = FP_STATE_SVE;
152 else
153 fp_state.to_save = FP_STATE_FPSIMD;
154
155 fpsimd_bind_state_to_cpu(&fp_state);
156
157 clear_thread_flag(TIF_FOREIGN_FPSTATE);
158 }
159 }
160
161 /*
162 * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
163 * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
164 * disappears and another task or vcpu appears that recycles the same
165 * struct fpsimd_state.
166 */
kvm_arch_vcpu_put_fp(struct kvm_vcpu * vcpu)167 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
168 {
169 unsigned long flags;
170
171 local_irq_save(flags);
172
173 /*
174 * If we have VHE then the Hyp code will reset CPACR_EL1 to
175 * CPACR_EL1_DEFAULT and we need to reenable SME.
176 */
177 if (has_vhe() && system_supports_sme()) {
178 /* Also restore EL0 state seen on entry */
179 if (vcpu_get_flag(vcpu, HOST_SME_ENABLED))
180 sysreg_clear_set(CPACR_EL1, 0,
181 CPACR_EL1_SMEN_EL0EN |
182 CPACR_EL1_SMEN_EL1EN);
183 else
184 sysreg_clear_set(CPACR_EL1,
185 CPACR_EL1_SMEN_EL0EN,
186 CPACR_EL1_SMEN_EL1EN);
187 isb();
188 }
189
190 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
191 if (vcpu_has_sve(vcpu)) {
192 __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
193
194 /* Restore the VL that was saved when bound to the CPU */
195 if (!has_vhe())
196 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
197 SYS_ZCR_EL1);
198 }
199
200 fpsimd_save_and_flush_cpu_state();
201 } else if (has_vhe() && system_supports_sve()) {
202 /*
203 * The FPSIMD/SVE state in the CPU has not been touched, and we
204 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
205 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
206 * for EL0. To avoid spurious traps, restore the trap state
207 * seen by kvm_arch_vcpu_load_fp():
208 */
209 if (vcpu_get_flag(vcpu, HOST_SVE_ENABLED))
210 sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
211 else
212 sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
213 }
214
215 local_irq_restore(flags);
216 }
217