2 * VFPv2 or VFPv3 floating point unit
5 #include "../port/lib.h"
12 /* subarchitecture code in m->havefp */
18 /* fp control regs. most are read-only */
25 Fpinst= 9, /* optional, for exceptions */
32 Fpdex = 1 << 29, /* defined synch exception */
33 // Fp2v = 1 << 28, /* Fpinst2 reg is valid */
34 // Fpvv = 1 << 27, /* if Fpdex, vecitr is valid */
35 // Fptfv = 1 << 26, /* trapped fault is valid */
36 // Fpvecitr = MASK(3) << 8,
37 /* FSR bits appear here */
38 Fpmbc = Fpdex, /* bits exception handler must clear */
40 /* Fpscr bits; see u.h for more */
41 Stride = MASK(2) << 20,
45 /* trap exception enables (not allowed in vfp3) */
46 FPIDNRM = 1 << 15, /* input denormal */
47 Alltraps = FPIDNRM | FPINEX | FPUNFL | FPOVFL | FPZDIV | FPINVAL,
48 /* pending exceptions */
49 FPAIDNRM = 1 << 7, /* input denormal */
50 Allexc = FPAIDNRM | FPAINEX | FPAUNFL | FPAOVFL | FPAZDIV | FPAINVAL,
52 Allcc = MASK(4) << 28,
56 Cpaccnosimd = 1u << 31,
61 subarch(int impl, uint sa)
63 static char *armarchs[] = {
64 "VFPv1 (unsupported)",
66 "VFPv3+ with common VFP subarch v2",
67 "VFPv3+ with null subarch",
68 "VFPv3+ with common VFP subarch v3",
71 if (impl != 'A' || sa >= nelem(armarchs))
96 gotfp = 1 << CpFP | 1 << CpDFP;
97 cpwrsc(0, CpCONTROL, 0, CpCPaccess, MASK(28));
98 acc = cprdsc(0, CpCONTROL, 0, CpCPaccess);
99 if ((acc & (MASK(2) << (2*CpFP))) == 0) {
100 gotfp &= ~(1 << CpFP);
101 print("fpon: no single FP coprocessor\n");
103 if ((acc & (MASK(2) << (2*CpDFP))) == 0) {
104 gotfp &= ~(1 << CpDFP);
105 print("fpon: no double FP coprocessor\n");
108 print("fpon: no FP coprocessors\n");
112 m->fpon = 1; /* don't panic */
115 switch((sid >> 16) & MASK(7)){
122 default: /* VFPv3 or later */
124 m->fpnregs = (acc & Cpaccd16) ? 16 : 32;
128 print("fp: %d registers,%s simd\n", m->fpnregs,
129 (acc & Cpaccnosimd? " no": ""));
135 * these can be called to turn the fpu on or off for user procs,
136 * not just at system start up or shutdown.
151 if (!m->fpon && havefp()) {
152 /* enable fp. must be first operation on the FPUs. */
153 fpwr(Fpexc, Fpenabled);
165 /* clear pending exceptions; no traps in vfp3; all v7 ops are scalar */
166 m->fpscr = Dn | Fz | FPRNR | (FPINVAL | FPZDIV | FPOVFL) & ~Alltraps;
167 fpwr(Fpscr, m->fpscr);
174 print("fp: %s arch %s; rev %ld\n", implement(impl),
175 subarch(impl, (sid >> 16) & MASK(7)), sid & MASK(4));
194 fpwr(Fpscr, (fprd(Fpscr) & Allcc) | m->fpscr);
196 fpcfg(); /* 1st time on this fpu; configure it */
206 // scr = fprd(Fpscr);
207 // m->fpscr = scr & ~Allexc;
208 // fpwr(Fpscr, m->fpscr);
210 fpwr(Fpexc, fprd(Fpexc) & ~Fpmbc);
215 * Called when a note is about to be delivered to a
216 * user process, usually at the end of a system call.
217 * Note handlers are not allowed to use the FPU so
218 * the state is marked (after saving if necessary) and
219 * checked in the Device Not Available handler.
224 if(up->fpstate == FPactive){
226 up->fpstate = FPinactive;
228 up->fpstate |= FPillegal;
232 * Called from sysnoted() via the machine-dependent
234 * Clear the flag set above in fpunotify().
239 up->fpstate &= ~FPillegal;
243 * Called early in the non-interruptible path of
244 * sysrfork() via the machine-dependent syscall() routine.
245 * Save the state so that it can be easily copied
246 * to the child process later.
251 if(up->fpstate == FPactive){
253 up->fpstate = FPinactive;
258 * Called later in sysrfork() via the machine-dependent
259 * sysrforkchild() routine.
260 * Copy the parent FPU state to the child.
263 fpusysrforkchild(Proc *p, Ureg *, Proc *up)
265 /* don't penalize the child, it hasn't done FP in a note handler. */
266 p->fpstate = up->fpstate & ~FPillegal;
269 /* should only be called if p->fpstate == FPactive */
276 fps->control = fps->status = fprd(Fpscr);
278 for (n = 0; n < m->fpnregs; n++)
279 fpsavereg(n, (uvlong *)fps->regs[n]);
289 fpwr(Fpscr, p->fpsave.control);
290 m->fpscr = fprd(Fpscr) & ~Allcc;
292 for (n = 0; n < m->fpnregs; n++)
293 fprestreg(n, *(uvlong *)p->fpsave.regs[n]);
297 * Called from sched() and sleep() via the machine-dependent
298 * procsave() routine.
299 * About to go in to the scheduler.
300 * If the process wasn't using the FPU
301 * there's nothing to do.
306 if(p->fpstate == FPactive){
307 if(p->state == Moribund)
311 * Fpsave() stores without handling pending
312 * unmasked exeptions. Postnote() can't be called
313 * here as sleep() already has up->rlock, so
314 * the handling of pending exceptions is delayed
315 * until the process runs again and generates an
316 * emulation fault to activate the FPU.
320 p->fpstate = FPinactive;
325 * The process has been rescheduled and is about to run.
326 * Nothing to do here right now. If the process tries to use
327 * the FPU again it will cause a Device Not Available
328 * exception and the state will then be restored.
331 fpuprocrestore(Proc *)
337 * Called from sysexec() via sysprocsetup() to
338 * set the FPU for the new process.
341 fpusysprocsetup(Proc *p)
351 char *msg, note[ERRMAX];
353 status = up->fpsave.status;
356 * Some attention should probably be paid here to the
357 * exception masks and error summary.
359 if (status & FPAINEX)
361 else if (status & FPAOVFL)
363 else if (status & FPAUNFL)
365 else if (status & FPAZDIV)
366 msg = "divide by zero";
367 else if (status & FPAINVAL)
368 msg = "bad operation";
371 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=%#lux",
372 msg, up->fpsave.pc, status);
373 postnote(up, 1, note, NDebug);
381 error("illegal instruction: VFP opcode in emulated mode");
384 up->fpstate = FPactive;
388 * Before restoring the state, check for any pending
389 * exceptions. There's no way to restore the state without
390 * generating an unmasked exception.
391 * More attention should probably be paid here to the
392 * exception masks and error summary.
394 if(up->fpsave.status & (FPAINEX|FPAUNFL|FPAOVFL|FPAZDIV|FPAINVAL)){
399 up->fpstate = FPactive;
402 error("illegal instruction: bad vfp fpu opcode");
411 if (m->fppc == pc && m->fppid == up->pid) {
414 panic("fpuemu: cpu%d stuck at pid %ld %s pc %#p "
415 "instr %#8.8lux", m->machno, up->pid, up->text,
433 condok(int cc, int c)
438 case 1: /* Z clear */
442 case 3: /* C clear */
446 case 5: /* N clear */
450 case 7: /* V clear */
452 case 8: /* C set and Z clear */
453 return cc&C && (cc&Z) == 0;
454 case 9: /* C clear or Z set */
455 return (cc&C) == 0 || cc&Z;
456 case 10: /* N set and V set, or N clear and V clear */
457 return (~cc&(N|V))==0 || (cc&(N|V)) == 0;
458 case 11: /* N set and V clear, or N clear and V set */
459 return (cc&(N|V))==N || (cc&(N|V))==V;
460 case 12: /* Z clear, and either N set and V set or N clear and V clear */
461 return (cc&Z) == 0 && ((~cc&(N|V))==0 || (cc&(N|V))==0);
462 case 13: /* Z set, or N set and V clear or N clear and V set */
463 return (cc&Z) || (cc&(N|V))==N || (cc&(N|V))==V;
464 case 14: /* always */
466 case 15: /* never (reserved) */
469 return 0; /* not reached */
472 /* only called to deal with user-mode instruction faults */
480 postnote(up, 1, up->errstr, NDebug);
484 if(up->fpstate & FPillegal)
485 error("floating point in note handler");
490 if(!condok(ureg->psr, *(ulong*)pc >> 28))
491 iprint("fpuemu: conditional instr shouldn't have got here\n");
492 op = (*(ulong *)pc >> 24) & MASK(4);
493 cop = (*(ulong *)pc >> 8) & MASK(4);
495 fpstuck(pc); /* debugging; could move down 1 line */
496 if (ISFPAOP(cop, op)) { /* old arm 7500 fpa opcode? */
497 // iprint("fpuemu: fpa instr %#8.8lux at %#p\n", *(ulong *)pc, pc);
498 // error("illegal instruction: old arm 7500 fpa opcode");
504 nfp = fpiarm(ureg); /* advances pc past emulated instr(s) */
505 if (nfp > 1) /* could adjust this threshold */
506 m->fppc = m->fpcnt = 0;
509 } else if (ISVFPOP(cop, op)) { /* if vfp, fpu must be off */
510 mathemu(ureg); /* enable fpu & retry */