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