vfp_init.c revision 1.70 1 1.70 riastrad /* $NetBSD: vfp_init.c,v 1.70 2020/07/27 20:51:29 riastradh Exp $ */
2 1.1 rearnsha
3 1.1 rearnsha /*
4 1.1 rearnsha * Copyright (c) 2008 ARM Ltd
5 1.1 rearnsha * All rights reserved.
6 1.1 rearnsha *
7 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
8 1.1 rearnsha * modification, are permitted provided that the following conditions
9 1.1 rearnsha * are met:
10 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
11 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
12 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
14 1.1 rearnsha * documentation and/or other materials provided with the distribution.
15 1.1 rearnsha * 3. The name of the company may not be used to endorse or promote
16 1.1 rearnsha * products derived from this software without specific prior written
17 1.1 rearnsha * permission.
18 1.1 rearnsha *
19 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR
20 1.1 rearnsha * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 rearnsha * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 rearnsha * ARE DISCLAIMED. IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY
23 1.1 rearnsha * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 rearnsha * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.1 rearnsha * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rearnsha * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1 rearnsha * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.1 rearnsha * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 1.1 rearnsha * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 rearnsha */
31 1.1 rearnsha
32 1.59 skrll #include "opt_cputypes.h"
33 1.59 skrll
34 1.58 skrll #include <sys/cdefs.h>
35 1.70 riastrad __KERNEL_RCSID(0, "$NetBSD: vfp_init.c,v 1.70 2020/07/27 20:51:29 riastradh Exp $");
36 1.58 skrll
37 1.1 rearnsha #include <sys/param.h>
38 1.1 rearnsha #include <sys/types.h>
39 1.1 rearnsha #include <sys/systm.h>
40 1.1 rearnsha #include <sys/device.h>
41 1.1 rearnsha #include <sys/proc.h>
42 1.4 matt #include <sys/cpu.h>
43 1.1 rearnsha
44 1.23 matt #include <arm/locore.h>
45 1.5 matt #include <arm/pcb.h>
46 1.1 rearnsha #include <arm/undefined.h>
47 1.1 rearnsha #include <arm/vfpreg.h>
48 1.8 matt #include <arm/mcontext.h>
49 1.65 riastrad #include <arm/fpu.h>
50 1.1 rearnsha
51 1.12 matt #include <uvm/uvm_extern.h> /* for pmap.h */
52 1.12 matt
53 1.69 riastrad #include <crypto/aes/aes_impl.h>
54 1.66 riastrad #include <crypto/aes/arch/arm/aes_neon.h>
55 1.70 riastrad #include <crypto/chacha/arch/arm/chacha_neon.h>
56 1.70 riastrad #include <crypto/chacha/chacha_impl.h>
57 1.66 riastrad
58 1.11 matt #ifdef FPU_VFP
59 1.11 matt
60 1.29 matt #ifdef CPU_CORTEX
61 1.56 christos #define SETFPU __asm(".fpu\tvfpv4")
62 1.29 matt #else
63 1.56 christos #define SETFPU __asm(".fpu\tvfp")
64 1.29 matt #endif
65 1.56 christos SETFPU;
66 1.29 matt
67 1.1 rearnsha /* FLDMD <X>, {d0-d15} */
68 1.11 matt static inline void
69 1.13 matt load_vfpregs_lo(const uint64_t *p)
70 1.10 matt {
71 1.56 christos SETFPU;
72 1.64 joerg __asm __volatile(".fpu vfp\n vldmia\t%0, {d0-d15}" :: "r" (p) : "memory");
73 1.10 matt }
74 1.10 matt
75 1.10 matt /* FSTMD <X>, {d0-d15} */
76 1.11 matt static inline void
77 1.10 matt save_vfpregs_lo(uint64_t *p)
78 1.10 matt {
79 1.56 christos SETFPU;
80 1.64 joerg __asm __volatile(".fpu vfp\n vstmia\t%0, {d0-d15}" :: "r" (p) : "memory");
81 1.10 matt }
82 1.10 matt
83 1.10 matt #ifdef CPU_CORTEX
84 1.10 matt /* FLDMD <X>, {d16-d31} */
85 1.11 matt static inline void
86 1.13 matt load_vfpregs_hi(const uint64_t *p)
87 1.10 matt {
88 1.56 christos SETFPU;
89 1.64 joerg __asm __volatile(".fpu neon-vfpv4\n vldmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
90 1.10 matt }
91 1.10 matt
92 1.10 matt /* FLDMD <X>, {d16-d31} */
93 1.11 matt static inline void
94 1.10 matt save_vfpregs_hi(uint64_t *p)
95 1.10 matt {
96 1.56 christos SETFPU;
97 1.64 joerg __asm __volatile(".fpu neon-vfpv4\nvstmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
98 1.10 matt }
99 1.10 matt #endif
100 1.1 rearnsha
101 1.13 matt static inline void
102 1.13 matt load_vfpregs(const struct vfpreg *fregs)
103 1.13 matt {
104 1.13 matt load_vfpregs_lo(fregs->vfp_regs);
105 1.13 matt #ifdef CPU_CORTEX
106 1.13 matt #ifdef CPU_ARM11
107 1.13 matt switch (curcpu()->ci_vfp_id) {
108 1.13 matt case FPU_VFP_CORTEXA5:
109 1.13 matt case FPU_VFP_CORTEXA7:
110 1.13 matt case FPU_VFP_CORTEXA8:
111 1.13 matt case FPU_VFP_CORTEXA9:
112 1.20 matt case FPU_VFP_CORTEXA15:
113 1.42 slp case FPU_VFP_CORTEXA15_QEMU:
114 1.50 skrll case FPU_VFP_CORTEXA53:
115 1.53 jmcneill case FPU_VFP_CORTEXA57:
116 1.13 matt #endif
117 1.13 matt load_vfpregs_hi(fregs->vfp_regs);
118 1.13 matt #ifdef CPU_ARM11
119 1.13 matt break;
120 1.13 matt }
121 1.13 matt #endif
122 1.13 matt #endif
123 1.13 matt }
124 1.13 matt
125 1.13 matt static inline void
126 1.13 matt save_vfpregs(struct vfpreg *fregs)
127 1.13 matt {
128 1.13 matt save_vfpregs_lo(fregs->vfp_regs);
129 1.13 matt #ifdef CPU_CORTEX
130 1.13 matt #ifdef CPU_ARM11
131 1.13 matt switch (curcpu()->ci_vfp_id) {
132 1.13 matt case FPU_VFP_CORTEXA5:
133 1.13 matt case FPU_VFP_CORTEXA7:
134 1.13 matt case FPU_VFP_CORTEXA8:
135 1.13 matt case FPU_VFP_CORTEXA9:
136 1.20 matt case FPU_VFP_CORTEXA15:
137 1.42 slp case FPU_VFP_CORTEXA15_QEMU:
138 1.50 skrll case FPU_VFP_CORTEXA53:
139 1.53 jmcneill case FPU_VFP_CORTEXA57:
140 1.13 matt #endif
141 1.13 matt save_vfpregs_hi(fregs->vfp_regs);
142 1.13 matt #ifdef CPU_ARM11
143 1.13 matt break;
144 1.13 matt }
145 1.13 matt #endif
146 1.13 matt #endif
147 1.13 matt }
148 1.13 matt
149 1.1 rearnsha /* The real handler for VFP bounces. */
150 1.1 rearnsha static int vfp_handler(u_int, u_int, trapframe_t *, int);
151 1.13 matt #ifdef CPU_CORTEX
152 1.13 matt static int neon_handler(u_int, u_int, trapframe_t *, int);
153 1.13 matt #endif
154 1.1 rearnsha
155 1.13 matt static void vfp_state_load(lwp_t *, u_int);
156 1.39 rmind static void vfp_state_save(lwp_t *);
157 1.39 rmind static void vfp_state_release(lwp_t *);
158 1.4 matt
159 1.4 matt const pcu_ops_t arm_vfp_ops = {
160 1.4 matt .pcu_id = PCU_FPU,
161 1.13 matt .pcu_state_save = vfp_state_save,
162 1.4 matt .pcu_state_load = vfp_state_load,
163 1.4 matt .pcu_state_release = vfp_state_release,
164 1.4 matt };
165 1.1 rearnsha
166 1.34 matt /* determine what bits can be changed */
167 1.34 matt uint32_t vfp_fpscr_changable = VFP_FPSCR_CSUM;
168 1.34 matt /* default to run fast */
169 1.34 matt uint32_t vfp_fpscr_default = (VFP_FPSCR_DN | VFP_FPSCR_FZ | VFP_FPSCR_RN);
170 1.34 matt
171 1.1 rearnsha /*
172 1.1 rearnsha * Used to test for a VFP. The following function is installed as a coproc10
173 1.1 rearnsha * handler on the undefined instruction vector and then we issue a VFP
174 1.1 rearnsha * instruction. If undefined_test is non zero then the VFP did not handle
175 1.1 rearnsha * the instruction so must be absent, or disabled.
176 1.1 rearnsha */
177 1.1 rearnsha
178 1.1 rearnsha static int undefined_test;
179 1.1 rearnsha
180 1.1 rearnsha static int
181 1.4 matt vfp_test(u_int address, u_int insn, trapframe_t *frame, int fault_code)
182 1.1 rearnsha {
183 1.1 rearnsha
184 1.1 rearnsha frame->tf_pc += INSN_SIZE;
185 1.1 rearnsha ++undefined_test;
186 1.4 matt return 0;
187 1.4 matt }
188 1.4 matt
189 1.35 matt #else
190 1.35 matt /* determine what bits can be changed */
191 1.35 matt uint32_t vfp_fpscr_changable = VFP_FPSCR_CSUM|VFP_FPSCR_ESUM|VFP_FPSCR_RMODE;
192 1.4 matt #endif /* FPU_VFP */
193 1.4 matt
194 1.4 matt static int
195 1.4 matt vfp_fpscr_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
196 1.4 matt {
197 1.4 matt struct lwp * const l = curlwp;
198 1.4 matt const u_int regno = (insn >> 12) & 0xf;
199 1.4 matt /*
200 1.4 matt * Only match move to/from the FPSCR register and we
201 1.4 matt * can't be using the SP,LR,PC as a source.
202 1.4 matt */
203 1.4 matt if ((insn & 0xffef0fff) != 0xeee10a10 || regno > 12)
204 1.4 matt return 1;
205 1.4 matt
206 1.4 matt struct pcb * const pcb = lwp_getpcb(l);
207 1.4 matt
208 1.4 matt #ifdef FPU_VFP
209 1.4 matt /*
210 1.4 matt * If FPU is valid somewhere, let's just reenable VFP and
211 1.4 matt * retry the instruction (only safe thing to do since the
212 1.4 matt * pcb has a stale copy).
213 1.4 matt */
214 1.4 matt if (pcb->pcb_vfp.vfp_fpexc & VFP_FPEXC_EN)
215 1.4 matt return 1;
216 1.4 matt
217 1.51 chs if (__predict_false(!vfp_used_p(l))) {
218 1.35 matt pcb->pcb_vfp.vfp_fpscr = vfp_fpscr_default;
219 1.4 matt }
220 1.26 matt #endif
221 1.4 matt
222 1.4 matt /*
223 1.30 skrll * We now know the pcb has the saved copy.
224 1.4 matt */
225 1.4 matt register_t * const regp = &frame->tf_r0 + regno;
226 1.4 matt if (insn & 0x00100000) {
227 1.4 matt *regp = pcb->pcb_vfp.vfp_fpscr;
228 1.4 matt } else {
229 1.34 matt pcb->pcb_vfp.vfp_fpscr &= ~vfp_fpscr_changable;
230 1.34 matt pcb->pcb_vfp.vfp_fpscr |= *regp & vfp_fpscr_changable;
231 1.4 matt }
232 1.4 matt
233 1.37 matt curcpu()->ci_vfp_evs[0].ev_count++;
234 1.61 skrll
235 1.4 matt frame->tf_pc += INSN_SIZE;
236 1.4 matt return 0;
237 1.1 rearnsha }
238 1.1 rearnsha
239 1.4 matt #ifndef FPU_VFP
240 1.4 matt /*
241 1.4 matt * If we don't want VFP support, we still need to handle emulating VFP FPSCR
242 1.4 matt * instructions.
243 1.4 matt */
244 1.4 matt void
245 1.37 matt vfp_attach(struct cpu_info *ci)
246 1.4 matt {
247 1.37 matt if (CPU_IS_PRIMARY(ci)) {
248 1.37 matt install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
249 1.37 matt }
250 1.37 matt evcnt_attach_dynamic(&ci->ci_vfp_evs[0], EVCNT_TYPE_TRAP, NULL,
251 1.37 matt ci->ci_cpuname, "vfp fpscr traps");
252 1.4 matt }
253 1.4 matt
254 1.4 matt #else
255 1.1 rearnsha void
256 1.37 matt vfp_attach(struct cpu_info *ci)
257 1.1 rearnsha {
258 1.4 matt const char *model = NULL;
259 1.1 rearnsha
260 1.37 matt if (CPU_ID_ARM11_P(ci->ci_arm_cpuid)
261 1.37 matt || CPU_ID_MV88SV58XX_P(ci->ci_arm_cpuid)
262 1.37 matt || CPU_ID_CORTEX_P(ci->ci_arm_cpuid)) {
263 1.37 matt #if 0
264 1.37 matt const uint32_t nsacr = armreg_nsacr_read();
265 1.37 matt const uint32_t nsacr_vfp = __BITS(VFP_COPROC,VFP_COPROC2);
266 1.37 matt if ((nsacr & nsacr_vfp) != nsacr_vfp) {
267 1.40 matt aprint_normal_dev(ci->ci_dev,
268 1.40 matt "VFP access denied (NSACR=%#x)\n", nsacr);
269 1.62 skrll if (CPU_IS_PRIMARY(ci))
270 1.62 skrll install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
271 1.37 matt ci->ci_vfp_id = 0;
272 1.37 matt evcnt_attach_dynamic(&ci->ci_vfp_evs[0],
273 1.37 matt EVCNT_TYPE_TRAP, NULL, ci->ci_cpuname,
274 1.37 matt "vfp fpscr traps");
275 1.37 matt return;
276 1.37 matt }
277 1.37 matt #endif
278 1.7 matt const uint32_t cpacr_vfp = CPACR_CPn(VFP_COPROC);
279 1.7 matt const uint32_t cpacr_vfp2 = CPACR_CPn(VFP_COPROC2);
280 1.1 rearnsha
281 1.7 matt /*
282 1.7 matt * We first need to enable access to the coprocessors.
283 1.7 matt */
284 1.7 matt uint32_t cpacr = armreg_cpacr_read();
285 1.7 matt cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp);
286 1.7 matt cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp2);
287 1.7 matt armreg_cpacr_write(cpacr);
288 1.1 rearnsha
289 1.48 jmcneill arm_isb();
290 1.48 jmcneill
291 1.7 matt /*
292 1.7 matt * If we could enable them, then they exist.
293 1.7 matt */
294 1.7 matt cpacr = armreg_cpacr_read();
295 1.40 matt bool vfp_p = __SHIFTOUT(cpacr, cpacr_vfp2) == CPACR_ALL
296 1.40 matt && __SHIFTOUT(cpacr, cpacr_vfp) == CPACR_ALL;
297 1.28 matt if (!vfp_p) {
298 1.40 matt aprint_normal_dev(ci->ci_dev,
299 1.40 matt "VFP access denied (CPACR=%#x)\n", cpacr);
300 1.62 skrll if (CPU_IS_PRIMARY(ci))
301 1.62 skrll install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
302 1.28 matt ci->ci_vfp_id = 0;
303 1.37 matt evcnt_attach_dynamic(&ci->ci_vfp_evs[0],
304 1.37 matt EVCNT_TYPE_TRAP, NULL, ci->ci_cpuname,
305 1.37 matt "vfp fpscr traps");
306 1.28 matt return;
307 1.28 matt }
308 1.6 matt }
309 1.6 matt
310 1.7 matt void *uh = install_coproc_handler(VFP_COPROC, vfp_test);
311 1.7 matt
312 1.7 matt undefined_test = 0;
313 1.7 matt
314 1.21 matt const uint32_t fpsid = armreg_fpsid_read();
315 1.1 rearnsha
316 1.1 rearnsha remove_coproc_handler(uh);
317 1.1 rearnsha
318 1.1 rearnsha if (undefined_test != 0) {
319 1.4 matt aprint_normal_dev(ci->ci_dev, "No VFP detected\n");
320 1.62 skrll if (CPU_IS_PRIMARY(ci))
321 1.62 skrll install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
322 1.4 matt ci->ci_vfp_id = 0;
323 1.1 rearnsha return;
324 1.1 rearnsha }
325 1.1 rearnsha
326 1.4 matt ci->ci_vfp_id = fpsid;
327 1.4 matt switch (fpsid & ~ VFP_FPSID_REV_MSK) {
328 1.4 matt case FPU_VFP10_ARM10E:
329 1.4 matt model = "VFP10 R1";
330 1.4 matt break;
331 1.4 matt case FPU_VFP11_ARM11:
332 1.4 matt model = "VFP11";
333 1.4 matt break;
334 1.36 matt case FPU_VFP_MV88SV58XX:
335 1.36 matt model = "VFP3";
336 1.36 matt break;
337 1.7 matt case FPU_VFP_CORTEXA5:
338 1.7 matt case FPU_VFP_CORTEXA7:
339 1.7 matt case FPU_VFP_CORTEXA8:
340 1.7 matt case FPU_VFP_CORTEXA9:
341 1.63 tnn case FPU_VFP_CORTEXA12:
342 1.20 matt case FPU_VFP_CORTEXA15:
343 1.42 slp case FPU_VFP_CORTEXA15_QEMU:
344 1.63 tnn case FPU_VFP_CORTEXA17:
345 1.50 skrll case FPU_VFP_CORTEXA53:
346 1.53 jmcneill case FPU_VFP_CORTEXA57:
347 1.37 matt if (armreg_cpacr_read() & CPACR_V7_ASEDIS) {
348 1.37 matt model = "VFP 4.0+";
349 1.37 matt } else {
350 1.37 matt model = "NEON MPE (VFP 3.0+)";
351 1.37 matt cpu_neon_present = 1;
352 1.37 matt }
353 1.6 matt break;
354 1.4 matt default:
355 1.36 matt aprint_normal_dev(ci->ci_dev, "unrecognized VFP version %#x\n",
356 1.4 matt fpsid);
357 1.62 skrll if (CPU_IS_PRIMARY(ci))
358 1.62 skrll install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
359 1.35 matt vfp_fpscr_changable = VFP_FPSCR_CSUM|VFP_FPSCR_ESUM
360 1.35 matt |VFP_FPSCR_RMODE;
361 1.35 matt vfp_fpscr_default = 0;
362 1.4 matt return;
363 1.4 matt }
364 1.1 rearnsha
365 1.17 matt cpu_fpu_present = 1;
366 1.21 matt cpu_media_and_vfp_features[0] = armreg_mvfr0_read();
367 1.21 matt cpu_media_and_vfp_features[1] = armreg_mvfr1_read();
368 1.1 rearnsha if (fpsid != 0) {
369 1.34 matt uint32_t f0 = armreg_mvfr0_read();
370 1.41 matt uint32_t f1 = armreg_mvfr1_read();
371 1.34 matt aprint_normal("vfp%d at %s: %s%s%s%s%s\n",
372 1.37 matt device_unit(ci->ci_dev),
373 1.37 matt device_xname(ci->ci_dev),
374 1.34 matt model,
375 1.34 matt ((f0 & ARM_MVFR0_ROUNDING_MASK) ? ", rounding" : ""),
376 1.34 matt ((f0 & ARM_MVFR0_EXCEPT_MASK) ? ", exceptions" : ""),
377 1.38 matt ((f1 & ARM_MVFR1_D_NAN_MASK) ? ", NaN propagation" : ""),
378 1.34 matt ((f1 & ARM_MVFR1_FTZ_MASK) ? ", denormals" : ""));
379 1.49 jmcneill aprint_debug("vfp%d: mvfr: [0]=%#x [1]=%#x\n",
380 1.37 matt device_unit(ci->ci_dev), f0, f1);
381 1.37 matt if (CPU_IS_PRIMARY(ci)) {
382 1.37 matt if (f0 & ARM_MVFR0_ROUNDING_MASK) {
383 1.37 matt vfp_fpscr_changable |= VFP_FPSCR_RMODE;
384 1.37 matt }
385 1.37 matt if (f1 & ARM_MVFR0_EXCEPT_MASK) {
386 1.37 matt vfp_fpscr_changable |= VFP_FPSCR_ESUM;
387 1.37 matt }
388 1.38 matt // If hardware supports propagation of NaNs, select it.
389 1.37 matt if (f1 & ARM_MVFR1_D_NAN_MASK) {
390 1.37 matt vfp_fpscr_default &= ~VFP_FPSCR_DN;
391 1.37 matt vfp_fpscr_changable |= VFP_FPSCR_DN;
392 1.37 matt }
393 1.37 matt // If hardware supports denormalized numbers, use it.
394 1.37 matt if (cpu_media_and_vfp_features[1] & ARM_MVFR1_FTZ_MASK) {
395 1.37 matt vfp_fpscr_default &= ~VFP_FPSCR_FZ;
396 1.37 matt vfp_fpscr_changable |= VFP_FPSCR_FZ;
397 1.37 matt }
398 1.37 matt }
399 1.37 matt }
400 1.37 matt evcnt_attach_dynamic(&ci->ci_vfp_evs[0], EVCNT_TYPE_MISC, NULL,
401 1.37 matt ci->ci_cpuname, "vfp coproc use");
402 1.37 matt evcnt_attach_dynamic(&ci->ci_vfp_evs[1], EVCNT_TYPE_MISC, NULL,
403 1.37 matt ci->ci_cpuname, "vfp coproc re-use");
404 1.37 matt evcnt_attach_dynamic(&ci->ci_vfp_evs[2], EVCNT_TYPE_TRAP, NULL,
405 1.37 matt ci->ci_cpuname, "vfp coproc fault");
406 1.62 skrll if (CPU_IS_PRIMARY(ci)) {
407 1.62 skrll install_coproc_handler(VFP_COPROC, vfp_handler);
408 1.62 skrll install_coproc_handler(VFP_COPROC2, vfp_handler);
409 1.13 matt #ifdef CPU_CORTEX
410 1.66 riastrad if (cpu_neon_present) {
411 1.66 riastrad install_coproc_handler(CORE_UNKNOWN_HANDLER,
412 1.66 riastrad neon_handler);
413 1.66 riastrad aes_md_init(&aes_neon_impl);
414 1.70 riastrad chacha_md_init(&chacha_neon_impl);
415 1.66 riastrad }
416 1.13 matt #endif
417 1.62 skrll }
418 1.1 rearnsha }
419 1.1 rearnsha
420 1.1 rearnsha /* The real handler for VFP bounces. */
421 1.4 matt static int
422 1.21 matt vfp_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
423 1.1 rearnsha {
424 1.4 matt struct cpu_info * const ci = curcpu();
425 1.1 rearnsha
426 1.1 rearnsha /* This shouldn't ever happen. */
427 1.1 rearnsha if (fault_code != FAULT_USER)
428 1.14 matt panic("VFP fault at %#x in non-user mode", frame->tf_pc);
429 1.1 rearnsha
430 1.27 matt if (ci->ci_vfp_id == 0) {
431 1.1 rearnsha /* No VFP detected, just fault. */
432 1.1 rearnsha return 1;
433 1.27 matt }
434 1.27 matt
435 1.47 matt /*
436 1.47 matt * If we already own the FPU and it's enabled (and no exception), raise
437 1.47 matt * SIGILL. If there is an exception, drop through to raise a SIGFPE.
438 1.47 matt */
439 1.46 matt if (curcpu()->ci_pcu_curlwp[PCU_FPU] == curlwp
440 1.47 matt && (armreg_fpexc_read() & (VFP_FPEXC_EX|VFP_FPEXC_EN)) == VFP_FPEXC_EN)
441 1.47 matt return 1;
442 1.44 matt
443 1.27 matt /*
444 1.27 matt * Make sure we own the FP.
445 1.27 matt */
446 1.27 matt pcu_load(&arm_vfp_ops);
447 1.1 rearnsha
448 1.21 matt uint32_t fpexc = armreg_fpexc_read();
449 1.21 matt if (fpexc & VFP_FPEXC_EX) {
450 1.21 matt ksiginfo_t ksi;
451 1.21 matt KASSERT(fpexc & VFP_FPEXC_EN);
452 1.21 matt
453 1.37 matt curcpu()->ci_vfp_evs[2].ev_count++;
454 1.21 matt
455 1.21 matt /*
456 1.21 matt * Need the clear the exception condition so any signal
457 1.33 skrll * and future use can proceed.
458 1.21 matt */
459 1.31 skrll armreg_fpexc_write(fpexc & ~(VFP_FPEXC_EX|VFP_FPEXC_FSUM));
460 1.21 matt
461 1.51 chs pcu_save(&arm_vfp_ops, curlwp);
462 1.33 skrll
463 1.33 skrll /*
464 1.33 skrll * XXX Need to emulate bounce instructions here to get correct
465 1.33 skrll * XXX exception codes, etc.
466 1.33 skrll */
467 1.21 matt KSI_INIT_TRAP(&ksi);
468 1.21 matt ksi.ksi_signo = SIGFPE;
469 1.21 matt if (fpexc & VFP_FPEXC_IXF)
470 1.21 matt ksi.ksi_code = FPE_FLTRES;
471 1.21 matt else if (fpexc & VFP_FPEXC_UFF)
472 1.21 matt ksi.ksi_code = FPE_FLTUND;
473 1.21 matt else if (fpexc & VFP_FPEXC_OFF)
474 1.21 matt ksi.ksi_code = FPE_FLTOVF;
475 1.21 matt else if (fpexc & VFP_FPEXC_DZF)
476 1.21 matt ksi.ksi_code = FPE_FLTDIV;
477 1.21 matt else if (fpexc & VFP_FPEXC_IOF)
478 1.21 matt ksi.ksi_code = FPE_FLTINV;
479 1.21 matt ksi.ksi_addr = (uint32_t *)address;
480 1.21 matt ksi.ksi_trap = 0;
481 1.21 matt trapsignal(curlwp, &ksi);
482 1.21 matt return 0;
483 1.21 matt }
484 1.21 matt
485 1.4 matt /* Need to restart the faulted instruction. */
486 1.4 matt // frame->tf_pc -= INSN_SIZE;
487 1.4 matt return 0;
488 1.4 matt }
489 1.1 rearnsha
490 1.13 matt #ifdef CPU_CORTEX
491 1.13 matt /* The real handler for NEON bounces. */
492 1.13 matt static int
493 1.21 matt neon_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
494 1.13 matt {
495 1.13 matt struct cpu_info * const ci = curcpu();
496 1.13 matt
497 1.13 matt if (ci->ci_vfp_id == 0)
498 1.13 matt /* No VFP detected, just fault. */
499 1.13 matt return 1;
500 1.13 matt
501 1.13 matt if ((insn & 0xfe000000) != 0xf2000000
502 1.13 matt && (insn & 0xfe000000) != 0xf4000000)
503 1.13 matt /* Not NEON instruction, just fault. */
504 1.13 matt return 1;
505 1.13 matt
506 1.13 matt /* This shouldn't ever happen. */
507 1.13 matt if (fault_code != FAULT_USER)
508 1.13 matt panic("NEON fault in non-user mode");
509 1.13 matt
510 1.45 matt /* if we already own the FPU and it's enabled, raise SIGILL */
511 1.45 matt if (curcpu()->ci_pcu_curlwp[PCU_FPU] == curlwp
512 1.45 matt && (armreg_fpexc_read() & VFP_FPEXC_EN) != 0)
513 1.47 matt return 1;
514 1.43 matt
515 1.13 matt pcu_load(&arm_vfp_ops);
516 1.13 matt
517 1.13 matt /* Need to restart the faulted instruction. */
518 1.13 matt // frame->tf_pc -= INSN_SIZE;
519 1.13 matt return 0;
520 1.13 matt }
521 1.13 matt #endif
522 1.13 matt
523 1.4 matt static void
524 1.13 matt vfp_state_load(lwp_t *l, u_int flags)
525 1.4 matt {
526 1.4 matt struct pcb * const pcb = lwp_getpcb(l);
527 1.4 matt struct vfpreg * const fregs = &pcb->pcb_vfp;
528 1.1 rearnsha
529 1.1 rearnsha /*
530 1.1 rearnsha * Instrument VFP usage -- if a process has not previously
531 1.1 rearnsha * used the VFP, mark it as having used VFP for the first time,
532 1.1 rearnsha * and count this event.
533 1.1 rearnsha *
534 1.1 rearnsha * If a process has used the VFP, count a "used VFP, and took
535 1.1 rearnsha * a trap to use it again" event.
536 1.1 rearnsha */
537 1.39 rmind if (__predict_false((flags & PCU_VALID) == 0)) {
538 1.37 matt curcpu()->ci_vfp_evs[0].ev_count++;
539 1.34 matt pcb->pcb_vfp.vfp_fpscr = vfp_fpscr_default;
540 1.4 matt } else {
541 1.37 matt curcpu()->ci_vfp_evs[1].ev_count++;
542 1.4 matt }
543 1.1 rearnsha
544 1.54 bouyer KASSERT((armreg_fpexc_read() & VFP_FPEXC_EN) == 0);
545 1.39 rmind /*
546 1.39 rmind * If the VFP is already enabled we must be bouncing an instruction.
547 1.39 rmind */
548 1.39 rmind if (flags & PCU_REENABLE) {
549 1.39 rmind uint32_t fpexc = armreg_fpexc_read();
550 1.39 rmind armreg_fpexc_write(fpexc | VFP_FPEXC_EN);
551 1.54 bouyer fregs->vfp_fpexc |= VFP_FPEXC_EN;
552 1.39 rmind return;
553 1.39 rmind }
554 1.54 bouyer KASSERT((fregs->vfp_fpexc & VFP_FPEXC_EN) == 0);
555 1.33 skrll
556 1.39 rmind /*
557 1.39 rmind * Load and Enable the VFP (so that we can write the registers).
558 1.39 rmind */
559 1.39 rmind fregs->vfp_fpexc |= VFP_FPEXC_EN;
560 1.39 rmind armreg_fpexc_write(fregs->vfp_fpexc);
561 1.54 bouyer KASSERT(curcpu()->ci_pcu_curlwp[PCU_FPU] == NULL);
562 1.54 bouyer KASSERT(l->l_pcu_cpu[PCU_FPU] == NULL);
563 1.13 matt
564 1.39 rmind load_vfpregs(fregs);
565 1.39 rmind armreg_fpscr_write(fregs->vfp_fpscr);
566 1.13 matt
567 1.39 rmind if (fregs->vfp_fpexc & VFP_FPEXC_EX) {
568 1.39 rmind /* Need to restore the exception handling state. */
569 1.52 chs armreg_fpinst_write(fregs->vfp_fpinst);
570 1.39 rmind if (fregs->vfp_fpexc & VFP_FPEXC_FP2V)
571 1.52 chs armreg_fpinst2_write(fregs->vfp_fpinst2);
572 1.1 rearnsha }
573 1.1 rearnsha }
574 1.1 rearnsha
575 1.1 rearnsha void
576 1.39 rmind vfp_state_save(lwp_t *l)
577 1.1 rearnsha {
578 1.4 matt struct pcb * const pcb = lwp_getpcb(l);
579 1.39 rmind struct vfpreg * const fregs = &pcb->pcb_vfp;
580 1.21 matt uint32_t fpexc = armreg_fpexc_read();
581 1.33 skrll
582 1.54 bouyer KASSERT(curcpu()->ci_pcu_curlwp[PCU_FPU] == l);
583 1.54 bouyer KASSERT(curcpu() == l->l_pcu_cpu[PCU_FPU]);
584 1.54 bouyer KASSERT(curlwp == l || curlwp->l_pcu_cpu[PCU_FPU] != curcpu());
585 1.33 skrll /*
586 1.33 skrll * Enable the VFP (so we can read the registers).
587 1.33 skrll * Make sure the exception bit is cleared so that we can
588 1.33 skrll * safely dump the registers.
589 1.33 skrll */
590 1.21 matt armreg_fpexc_write((fpexc | VFP_FPEXC_EN) & ~VFP_FPEXC_EX);
591 1.1 rearnsha
592 1.4 matt fregs->vfp_fpexc = fpexc;
593 1.4 matt if (fpexc & VFP_FPEXC_EX) {
594 1.4 matt /* Need to save the exception handling state */
595 1.21 matt fregs->vfp_fpinst = armreg_fpinst_read();
596 1.21 matt if (fpexc & VFP_FPEXC_FP2V)
597 1.21 matt fregs->vfp_fpinst2 = armreg_fpinst2_read();
598 1.1 rearnsha }
599 1.21 matt fregs->vfp_fpscr = armreg_fpscr_read();
600 1.13 matt save_vfpregs(fregs);
601 1.4 matt
602 1.1 rearnsha /* Disable the VFP. */
603 1.33 skrll armreg_fpexc_write(fpexc & ~VFP_FPEXC_EN);
604 1.1 rearnsha }
605 1.1 rearnsha
606 1.1 rearnsha void
607 1.39 rmind vfp_state_release(lwp_t *l)
608 1.1 rearnsha {
609 1.4 matt struct pcb * const pcb = lwp_getpcb(l);
610 1.1 rearnsha
611 1.39 rmind /*
612 1.39 rmind * Now mark the VFP as disabled (and our state
613 1.39 rmind * has been already saved or is being discarded).
614 1.39 rmind */
615 1.39 rmind pcb->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
616 1.1 rearnsha
617 1.1 rearnsha /*
618 1.4 matt * Turn off the FPU so the next time a VFP instruction is issued
619 1.4 matt * an exception happens. We don't know if this LWP's state was
620 1.4 matt * loaded but if we turned off the FPU for some other LWP, when
621 1.4 matt * pcu_load invokes vfp_state_load it will see that VFP_FPEXC_EN
622 1.13 matt * is still set so it just restore fpexc and return since its
623 1.4 matt * contents are still sitting in the VFP.
624 1.1 rearnsha */
625 1.21 matt armreg_fpexc_write(armreg_fpexc_read() & ~VFP_FPEXC_EN);
626 1.1 rearnsha }
627 1.1 rearnsha
628 1.1 rearnsha void
629 1.51 chs vfp_savecontext(lwp_t *l)
630 1.1 rearnsha {
631 1.51 chs pcu_save(&arm_vfp_ops, l);
632 1.1 rearnsha }
633 1.1 rearnsha
634 1.1 rearnsha void
635 1.51 chs vfp_discardcontext(lwp_t *l, bool used_p)
636 1.1 rearnsha {
637 1.51 chs pcu_discard(&arm_vfp_ops, l, used_p);
638 1.25 matt }
639 1.25 matt
640 1.25 matt bool
641 1.51 chs vfp_used_p(const lwp_t *l)
642 1.25 matt {
643 1.51 chs return pcu_valid_p(&arm_vfp_ops, l);
644 1.13 matt }
645 1.13 matt
646 1.13 matt void
647 1.8 matt vfp_getcontext(struct lwp *l, mcontext_t *mcp, int *flagsp)
648 1.8 matt {
649 1.51 chs if (vfp_used_p(l)) {
650 1.8 matt const struct pcb * const pcb = lwp_getpcb(l);
651 1.51 chs
652 1.51 chs pcu_save(&arm_vfp_ops, l);
653 1.8 matt mcp->__fpu.__vfpregs.__vfp_fpscr = pcb->pcb_vfp.vfp_fpscr;
654 1.8 matt memcpy(mcp->__fpu.__vfpregs.__vfp_fstmx, pcb->pcb_vfp.vfp_regs,
655 1.8 matt sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
656 1.10 matt *flagsp |= _UC_FPU|_UC_ARM_VFP;
657 1.8 matt }
658 1.8 matt }
659 1.8 matt
660 1.8 matt void
661 1.8 matt vfp_setcontext(struct lwp *l, const mcontext_t *mcp)
662 1.8 matt {
663 1.8 matt struct pcb * const pcb = lwp_getpcb(l);
664 1.51 chs
665 1.51 chs pcu_discard(&arm_vfp_ops, l, true);
666 1.8 matt pcb->pcb_vfp.vfp_fpscr = mcp->__fpu.__vfpregs.__vfp_fpscr;
667 1.8 matt memcpy(pcb->pcb_vfp.vfp_regs, mcp->__fpu.__vfpregs.__vfp_fstmx,
668 1.8 matt sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
669 1.8 matt }
670 1.8 matt
671 1.65 riastrad void
672 1.65 riastrad fpu_kern_enter(void)
673 1.65 riastrad {
674 1.65 riastrad struct cpu_info *ci;
675 1.65 riastrad uint32_t fpexc;
676 1.65 riastrad int s;
677 1.65 riastrad
678 1.65 riastrad /*
679 1.67 riastrad * Block interrupts up to IPL_VM. We must block preemption
680 1.67 riastrad * since -- if this is a user thread -- there is nowhere to
681 1.67 riastrad * save the kernel fpu state, and if we want this to be usable
682 1.67 riastrad * in interrupts, we can't let interrupts interfere with the
683 1.67 riastrad * fpu state in use since there's nowhere for them to save it.
684 1.65 riastrad */
685 1.67 riastrad s = splvm();
686 1.65 riastrad ci = curcpu();
687 1.67 riastrad KASSERTMSG(ci->ci_cpl <= IPL_VM, "cpl=%d", ci->ci_cpl);
688 1.65 riastrad KASSERT(ci->ci_kfpu_spl == -1);
689 1.65 riastrad ci->ci_kfpu_spl = s;
690 1.65 riastrad
691 1.68 riastrad /* Save any fpu state on the current CPU. */
692 1.68 riastrad pcu_save_all_on_cpu();
693 1.65 riastrad
694 1.65 riastrad /* Enable the fpu. */
695 1.65 riastrad fpexc = armreg_fpexc_read();
696 1.65 riastrad fpexc |= VFP_FPEXC_EN;
697 1.65 riastrad fpexc &= ~VFP_FPEXC_EX;
698 1.65 riastrad armreg_fpexc_write(fpexc);
699 1.65 riastrad }
700 1.65 riastrad
701 1.65 riastrad void
702 1.65 riastrad fpu_kern_leave(void)
703 1.65 riastrad {
704 1.65 riastrad static const struct vfpreg zero_vfpreg;
705 1.65 riastrad struct cpu_info *ci = curcpu();
706 1.65 riastrad int s;
707 1.65 riastrad uint32_t fpexc;
708 1.65 riastrad
709 1.67 riastrad KASSERT(ci->ci_cpl == IPL_VM);
710 1.65 riastrad KASSERT(ci->ci_kfpu_spl != -1);
711 1.65 riastrad
712 1.65 riastrad /*
713 1.65 riastrad * Zero the fpu registers; otherwise we might leak secrets
714 1.65 riastrad * through Spectre-class attacks to userland, even if there are
715 1.65 riastrad * no bugs in fpu state management.
716 1.65 riastrad */
717 1.65 riastrad load_vfpregs(&zero_vfpreg);
718 1.65 riastrad
719 1.65 riastrad /*
720 1.65 riastrad * Disable the fpu so that the kernel can't accidentally use
721 1.65 riastrad * it again.
722 1.65 riastrad */
723 1.65 riastrad fpexc = armreg_fpexc_read();
724 1.65 riastrad fpexc &= ~VFP_FPEXC_EN;
725 1.65 riastrad armreg_fpexc_write(fpexc);
726 1.65 riastrad
727 1.65 riastrad /* Restore interrupts. */
728 1.65 riastrad s = ci->ci_kfpu_spl;
729 1.65 riastrad ci->ci_kfpu_spl = -1;
730 1.65 riastrad splx(s);
731 1.65 riastrad }
732 1.65 riastrad
733 1.4 matt #endif /* FPU_VFP */
734