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