fpu_emu.c revision 1.46 1 /* $NetBSD: fpu_emu.c,v 1.46 2022/09/02 12:47:10 rin Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (c) 1992, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * This software was developed by the Computer Systems Engineering group
43 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 * contributed to Berkeley.
45 *
46 * All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Lawrence Berkeley Laboratory.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)fpu.c 8.1 (Berkeley) 6/11/93
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: fpu_emu.c,v 1.46 2022/09/02 12:47:10 rin Exp $");
80
81 #ifdef _KERNEL_OPT
82 #include "opt_ddb.h"
83 #endif
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/evcnt.h>
88 #include <sys/proc.h>
89 #include <sys/siginfo.h>
90 #include <sys/signal.h>
91 #include <sys/signalvar.h>
92 #include <sys/syslog.h>
93
94 #include <powerpc/instr.h>
95 #include <powerpc/psl.h>
96
97 #include <machine/fpu.h>
98 #include <machine/reg.h>
99 #include <machine/trap.h>
100
101 #include <powerpc/fpu/fpu_emu.h>
102 #include <powerpc/fpu/fpu_extern.h>
103
104 #define FPU_EMU_EVCNT_DECL(name) \
105 static struct evcnt fpu_emu_ev_##name = \
106 EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "fpemu", #name); \
107 EVCNT_ATTACH_STATIC(fpu_emu_ev_##name)
108
109 #define FPU_EMU_EVCNT_INCR(name) \
110 fpu_emu_ev_##name.ev_count++
111
112 FPU_EMU_EVCNT_DECL(stfiwx);
113 FPU_EMU_EVCNT_DECL(fpstore);
114 FPU_EMU_EVCNT_DECL(fpload);
115 FPU_EMU_EVCNT_DECL(fcmpu);
116 FPU_EMU_EVCNT_DECL(frsp);
117 FPU_EMU_EVCNT_DECL(fctiw);
118 FPU_EMU_EVCNT_DECL(fcmpo);
119 FPU_EMU_EVCNT_DECL(mtfsb1);
120 FPU_EMU_EVCNT_DECL(fnegabs);
121 FPU_EMU_EVCNT_DECL(mcrfs);
122 FPU_EMU_EVCNT_DECL(mtfsb0);
123 FPU_EMU_EVCNT_DECL(fmr);
124 FPU_EMU_EVCNT_DECL(mtfsfi);
125 FPU_EMU_EVCNT_DECL(fnabs);
126 FPU_EMU_EVCNT_DECL(fabs);
127 FPU_EMU_EVCNT_DECL(mffs);
128 FPU_EMU_EVCNT_DECL(mtfsf);
129 FPU_EMU_EVCNT_DECL(fctid);
130 FPU_EMU_EVCNT_DECL(fcfid);
131 FPU_EMU_EVCNT_DECL(fdiv);
132 FPU_EMU_EVCNT_DECL(fsub);
133 FPU_EMU_EVCNT_DECL(fadd);
134 FPU_EMU_EVCNT_DECL(fsqrt);
135 FPU_EMU_EVCNT_DECL(fsel);
136 FPU_EMU_EVCNT_DECL(fpres);
137 FPU_EMU_EVCNT_DECL(fmul);
138 FPU_EMU_EVCNT_DECL(frsqrte);
139 FPU_EMU_EVCNT_DECL(fmulsub);
140 FPU_EMU_EVCNT_DECL(fmuladd);
141 FPU_EMU_EVCNT_DECL(fnmsub);
142 FPU_EMU_EVCNT_DECL(fnmadd);
143
144 /* FPSR exception masks */
145 #define FPSR_EX_MSK (FPSCR_VX|FPSCR_OX|FPSCR_UX|FPSCR_ZX| \
146 FPSCR_XX|FPSCR_VXSNAN|FPSCR_VXISI|FPSCR_VXIDI| \
147 FPSCR_VXZDZ|FPSCR_VXIMZ|FPSCR_VXVC|FPSCR_VXSOFT|\
148 FPSCR_VXSQRT|FPSCR_VXCVI)
149 #define FPSR_EX (FPSCR_VE|FPSCR_OE|FPSCR_UE|FPSCR_ZE|FPSCR_XE)
150 #define FPSR_INV (FPSCR_VXSNAN|FPSCR_VXISI|FPSCR_VXIDI| \
151 FPSCR_VXZDZ|FPSCR_VXIMZ|FPSCR_VXVC|FPSCR_VXSOFT|\
152 FPSCR_VXSQRT|FPSCR_VXCVI)
153 #define MCRFS_MASK \
154 ( \
155 FPSCR_FX | FPSCR_OX | \
156 FPSCR_UX | FPSCR_ZX | FPSCR_XX | FPSCR_VXSNAN | \
157 FPSCR_VXISI | FPSCR_VXIDI | FPSCR_VXZDZ | FPSCR_VXIMZ | \
158 FPSCR_VXVC | \
159 FPSCR_VXSOFT | FPSCR_VXSQRT | FPSCR_VXCVI \
160 )
161
162 #define FR(reg) (fs->fpreg[reg])
163
164 int fpe_debug = 0;
165
166 #ifdef DDB
167 extern vaddr_t opc_disasm(vaddr_t loc, int opcode);
168 #endif
169
170 #ifdef DEBUG
171 /*
172 * Dump a `fpn' structure.
173 */
174 void
175 fpu_dumpfpn(struct fpn *fp)
176 {
177 static const char *class[] = {
178 "SNAN", "QNAN", "ZERO", "NUM", "INF"
179 };
180
181 KASSERT(fp != NULL);
182
183 printf("%s %c.%x %x %x %xE%d\n", class[fp->fp_class + 2],
184 fp->fp_sign ? '-' : ' ',
185 fp->fp_mant[0], fp->fp_mant[1],
186 fp->fp_mant[2], fp->fp_mant[3],
187 fp->fp_exp);
188 }
189 #endif
190
191 /*
192 * fpu_execute returns the following error numbers (0 = no error):
193 */
194 #define FPE 1 /* take a floating point exception */
195 #define NOTFPU 2 /* not an FPU instruction */
196 #define FAULT 3
197
198
199 /*
200 * Emulate a floating-point instruction.
201 * Return true if insn is consumed anyway.
202 * Otherwise, the caller must take care of it.
203 */
204 bool
205 fpu_emulate(struct trapframe *tf, struct fpreg *fpf, ksiginfo_t *ksi)
206 {
207 struct pcb *pcb;
208 union instr insn;
209 struct fpemu fe;
210
211 KSI_INIT_TRAP(ksi);
212 ksi->ksi_signo = 0;
213 ksi->ksi_addr = (void *)tf->tf_srr0;
214
215 /* initialize insn.is_datasize to tell it is *not* initialized */
216 fe.fe_fpstate = fpf;
217 fe.fe_cx = 0;
218
219 /* always set this (to avoid a warning) */
220
221 if (copyin((void *) (tf->tf_srr0), &insn.i_int, sizeof (insn.i_int))) {
222 #ifdef DEBUG
223 printf("fpu_emulate: fault reading opcode\n");
224 #endif
225 ksi->ksi_signo = SIGSEGV;
226 ksi->ksi_trap = EXC_ISI;
227 ksi->ksi_code = SEGV_MAPERR;
228 return true;
229 }
230
231 DPRINTF(FPE_EX, ("fpu_emulate: emulating insn %x at %p\n",
232 insn.i_int, (void *)tf->tf_srr0));
233
234 if ((insn.i_any.i_opcd == OPC_TWI) ||
235 ((insn.i_any.i_opcd == OPC_integer_31) &&
236 (insn.i_x.i_xo == OPC31_TW))) {
237 /* Check for the two trap insns. */
238 DPRINTF(FPE_EX, ("fpu_emulate: SIGTRAP\n"));
239 ksi->ksi_signo = SIGTRAP;
240 ksi->ksi_trap = EXC_PGM;
241 ksi->ksi_code = TRAP_BRKPT;
242 return true;
243 }
244 switch (fpu_execute(tf, &fe, &insn)) {
245 case 0:
246 success:
247 DPRINTF(FPE_EX, ("fpu_emulate: success\n"));
248 tf->tf_srr0 += 4;
249 return true;
250
251 case FPE:
252 pcb = lwp_getpcb(curlwp);
253 if ((pcb->pcb_flags & PSL_FE_PREC) == 0)
254 goto success;
255 DPRINTF(FPE_EX, ("fpu_emulate: SIGFPE\n"));
256 ksi->ksi_signo = SIGFPE;
257 ksi->ksi_trap = EXC_PGM;
258 ksi->ksi_code = fpu_get_fault_code();
259 return true;
260
261 case FAULT:
262 DPRINTF(FPE_EX, ("fpu_emulate: SIGSEGV\n"));
263 ksi->ksi_signo = SIGSEGV;
264 ksi->ksi_trap = EXC_DSI;
265 ksi->ksi_code = SEGV_MAPERR;
266 ksi->ksi_addr = (void *)fe.fe_addr;
267 return true;
268
269 case NOTFPU:
270 default:
271 DPRINTF(FPE_EX, ("fpu_emulate: SIGILL\n"));
272 #if defined(DDB) && defined(DEBUG)
273 if (fpe_debug & FPE_EX) {
274 printf("fpu_emulate: illegal insn %x at %p:",
275 insn.i_int, (void *) (tf->tf_srr0));
276 opc_disasm((vaddr_t)(tf->tf_srr0), insn.i_int);
277 }
278 #endif
279 return false;
280 }
281 }
282
283 /*
284 * Execute an FPU instruction (one that runs entirely in the FPU; not
285 * FBfcc or STF, for instance). On return, fe->fe_fs->fs_fsr will be
286 * modified to reflect the setting the hardware would have left.
287 *
288 * Note that we do not catch all illegal opcodes, so you can, for instance,
289 * multiply two integers this way.
290 */
291 int
292 fpu_execute(struct trapframe *tf, struct fpemu *fe, union instr *insn)
293 {
294 struct fpn *fp;
295 union instr instr = *insn;
296 int *a;
297 vaddr_t addr;
298 int ra, rb, rc, rt, type, mask, fsr, cx, bf, setcr;
299 unsigned int bits, cond;
300 struct fpreg *fs;
301 int i, mtfsb1 = 0;
302
303 /* Setup work. */
304 fp = NULL;
305 fs = fe->fe_fpstate;
306 fe->fe_fpscr = ((int *)&fs->fpscr)[1];
307
308 /*
309 * On PowerPC all floating point values are stored in registers
310 * as doubles, even when used for single precision operations.
311 */
312 type = FTYPE_DBL;
313 cond = instr.i_any.i_rc;
314 setcr = 0;
315 bf = 0; /* XXX gcc */
316
317 #if defined(DDB) && defined(DEBUG)
318 if (fpe_debug & FPE_EX) {
319 vaddr_t loc = tf->tf_srr0;
320
321 printf("Trying to emulate: %p ", (void *)loc);
322 opc_disasm(loc, instr.i_int);
323 }
324 #endif
325
326 /*
327 * `Decode' and execute instruction.
328 */
329
330 if ((instr.i_any.i_opcd >= OPC_LFS && instr.i_any.i_opcd <= OPC_STFDU) ||
331 instr.i_any.i_opcd == OPC_integer_31) {
332 /*
333 * Handle load/store insns:
334 *
335 * Convert to/from single if needed, calculate addr,
336 * and update index reg if needed.
337 */
338 uint64_t buf;
339 size_t size = sizeof(float);
340 int store, update;
341
342 cond = 0; /* ld/st never set condition codes */
343
344
345 if (instr.i_any.i_opcd == OPC_integer_31) {
346 if (instr.i_x.i_xo == OPC31_STFIWX) {
347 FPU_EMU_EVCNT_INCR(stfiwx);
348
349 /* Store as integer */
350 ra = instr.i_x.i_ra;
351 rb = instr.i_x.i_rb;
352 DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n",
353 ra, tf->tf_fixreg[ra], rb, tf->tf_fixreg[rb]));
354
355 addr = tf->tf_fixreg[rb];
356 if (ra != 0)
357 addr += tf->tf_fixreg[ra];
358 rt = instr.i_x.i_rt;
359 a = (int *)&fs->fpreg[rt];
360 DPRINTF(FPE_INSN,
361 ("fpu_execute: Store INT %x at %p\n",
362 a[1], (void *)addr));
363 if (copyout(&a[1], (void *)addr, sizeof(int))) {
364 fe->fe_addr = addr;
365 return (FAULT);
366 }
367 return (0);
368 }
369
370 if ((instr.i_x.i_xo & OPC31_FPMASK) != OPC31_FPOP)
371 /* Not an indexed FP load/store op */
372 return (NOTFPU);
373
374 store = (instr.i_x.i_xo & 0x80);
375 if (instr.i_x.i_xo & 0x40)
376 size = sizeof(double);
377 else
378 type = FTYPE_SNG;
379 update = (instr.i_x.i_xo & 0x20);
380
381 /* calculate EA of load/store */
382 ra = instr.i_x.i_ra;
383 rb = instr.i_x.i_rb;
384 DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n",
385 ra, tf->tf_fixreg[ra], rb, tf->tf_fixreg[rb]));
386 addr = tf->tf_fixreg[rb];
387 if (ra != 0)
388 addr += tf->tf_fixreg[ra];
389 rt = instr.i_x.i_rt;
390 } else {
391 store = instr.i_d.i_opcd & 0x4;
392 if (instr.i_d.i_opcd & 0x2)
393 size = sizeof(double);
394 else
395 type = FTYPE_SNG;
396 update = instr.i_d.i_opcd & 0x1;
397
398 /* calculate EA of load/store */
399 ra = instr.i_d.i_ra;
400 addr = instr.i_d.i_d;
401 DPRINTF(FPE_INSN, ("reg %d has %lx displ %lx\n",
402 ra, tf->tf_fixreg[ra], addr));
403 if (ra != 0)
404 addr += tf->tf_fixreg[ra];
405 rt = instr.i_d.i_rt;
406 }
407
408 if (update && ra == 0)
409 return (NOTFPU);
410
411 if (store) {
412 /* Store */
413 FPU_EMU_EVCNT_INCR(fpstore);
414 if (type != FTYPE_DBL) {
415 DPRINTF(FPE_INSN,
416 ("fpu_execute: Store SNG at %p\n",
417 (void *)addr));
418 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL,
419 FR(rt));
420 fpu_implode(fe, fp, type, &buf);
421 if (copyout(&buf, (void *)addr, size)) {
422 fe->fe_addr = addr;
423 return (FAULT);
424 }
425 } else {
426 DPRINTF(FPE_INSN,
427 ("fpu_execute: Store DBL at %p\n",
428 (void *)addr));
429 if (copyout(&fs->fpreg[rt], (void *)addr, size)) {
430 fe->fe_addr = addr;
431 return (FAULT);
432 }
433 }
434 } else {
435 /* Load */
436 FPU_EMU_EVCNT_INCR(fpload);
437 DPRINTF(FPE_INSN, ("fpu_execute: Load from %p\n",
438 (void *)addr));
439 if (copyin((const void *)addr, &fs->fpreg[rt], size)) {
440 fe->fe_addr = addr;
441 return (FAULT);
442 }
443 if (type != FTYPE_DBL) {
444 fpu_explode(fe, fp = &fe->fe_f1, type, FR(rt));
445 fpu_implode(fe, fp, FTYPE_DBL, &FR(rt));
446 }
447 }
448 if (update)
449 tf->tf_fixreg[ra] = addr;
450 /* Complete. */
451 return (0);
452 #ifdef notyet
453 } else if (instr.i_any.i_opcd == OPC_load_st_62) {
454 /* These are 64-bit extenstions */
455 return (NOTFPU);
456 #endif
457 } else if (instr.i_any.i_opcd == OPC_sp_fp_59 ||
458 instr.i_any.i_opcd == OPC_dp_fp_63) {
459
460
461 if (instr.i_any.i_opcd == OPC_dp_fp_63 &&
462 !(instr.i_a.i_xo & OPC63M_MASK)) {
463 /* Format X */
464 rt = instr.i_x.i_rt;
465 ra = instr.i_x.i_ra;
466 rb = instr.i_x.i_rb;
467
468
469 /* One of the special opcodes.... */
470 switch (instr.i_x.i_xo) {
471 case OPC63_FCMPU:
472 FPU_EMU_EVCNT_INCR(fcmpu);
473 DPRINTF(FPE_INSN, ("fpu_execute: FCMPU\n"));
474 rt >>= 2;
475 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
476 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
477 fpu_compare(fe, 0);
478 /* Make sure we do the condition regs. */
479 cond = 0;
480 /* N.B.: i_rs is already left shifted by two. */
481 bf = instr.i_x.i_rs & 0xfc;
482 setcr = 1;
483 break;
484
485 case OPC63_FRSP:
486 /*
487 * Convert to single:
488 *
489 * PowerPC uses this to round a double
490 * precision value to single precision,
491 * but values in registers are always
492 * stored in double precision format.
493 */
494 FPU_EMU_EVCNT_INCR(frsp);
495 DPRINTF(FPE_INSN, ("fpu_execute: FRSP\n"));
496 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL,
497 FR(rb));
498 fpu_implode(fe, fp, FTYPE_SNG, &FR(rt));
499 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG,
500 FR(rt));
501 type = FTYPE_DBL | FTYPE_FPRF;
502 break;
503 case OPC63_FCTIW:
504 case OPC63_FCTIWZ:
505 FPU_EMU_EVCNT_INCR(fctiw);
506 DPRINTF(FPE_INSN, ("fpu_execute: FCTIW\n"));
507 fpu_explode(fe, fp = &fe->fe_f1, type, FR(rb));
508 type = FTYPE_INT;
509 if (instr.i_x.i_xo == OPC63_FCTIWZ)
510 type |= FTYPE_RD_RZ;
511 break;
512 case OPC63_FCMPO:
513 FPU_EMU_EVCNT_INCR(fcmpo);
514 DPRINTF(FPE_INSN, ("fpu_execute: FCMPO\n"));
515 rt >>= 2;
516 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
517 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
518 fpu_compare(fe, 1);
519 /* Make sure we do the condition regs. */
520 cond = 0;
521 /* N.B.: i_rs is already left shifted by two. */
522 bf = instr.i_x.i_rs & 0xfc;
523 setcr = 1;
524 break;
525 case OPC63_MTFSB1:
526 FPU_EMU_EVCNT_INCR(mtfsb1);
527 DPRINTF(FPE_INSN, ("fpu_execute: MTFSB1\n"));
528 mtfsb1 = 1;
529 fe->fe_cx = (1 << (31 - rt)) &
530 ~(FPSCR_FEX | FPSCR_VX);
531 break;
532 case OPC63_FNEG:
533 FPU_EMU_EVCNT_INCR(fnegabs);
534 DPRINTF(FPE_INSN, ("fpu_execute: FNEGABS\n"));
535 memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
536 sizeof(double));
537 a = (int *)&fs->fpreg[rt];
538 *a ^= (1 << 31);
539 break;
540 case OPC63_MCRFS:
541 FPU_EMU_EVCNT_INCR(mcrfs);
542 DPRINTF(FPE_INSN, ("fpu_execute: MCRFS\n"));
543 cond = 0;
544 rt &= 0x1c;
545 ra &= 0x1c;
546 /* Extract the bits we want */
547 bits = (fe->fe_fpscr >> (28 - ra)) & 0xf;
548 /* Clear the bits we copied. */
549 mask = (0xf << (28 - ra)) & MCRFS_MASK;
550 fe->fe_fpscr &= ~mask;
551 /* Now shove them in the right part of cr */
552 tf->tf_cr &= ~(0xf << (28 - rt));
553 tf->tf_cr |= bits << (28 - rt);
554 break;
555 case OPC63_MTFSB0:
556 FPU_EMU_EVCNT_INCR(mtfsb0);
557 DPRINTF(FPE_INSN, ("fpu_execute: MTFSB0\n"));
558 fe->fe_fpscr &= ~(1 << (31 - rt)) |
559 (FPSCR_FEX | FPSCR_VX);
560 break;
561 case OPC63_FMR:
562 FPU_EMU_EVCNT_INCR(fmr);
563 DPRINTF(FPE_INSN, ("fpu_execute: FMR\n"));
564 memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
565 sizeof(double));
566 break;
567 case OPC63_MTFSFI:
568 FPU_EMU_EVCNT_INCR(mtfsfi);
569 DPRINTF(FPE_INSN, ("fpu_execute: MTFSFI\n"));
570 rb >>= 1;
571 rt &= 0x1c; /* Already left-shifted 4 */
572 bits = rb << (28 - rt);
573 mask = 0xf << (28 - rt);
574 fe->fe_fpscr = (fe->fe_fpscr & ~mask) | bits;
575 break;
576 case OPC63_FNABS:
577 FPU_EMU_EVCNT_INCR(fnabs);
578 DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
579 memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
580 sizeof(double));
581 a = (int *)&fs->fpreg[rt];
582 *a |= (1 << 31);
583 break;
584 case OPC63_FABS:
585 FPU_EMU_EVCNT_INCR(fabs);
586 DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
587 memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
588 sizeof(double));
589 a = (int *)&fs->fpreg[rt];
590 *a &= ~(1 << 31);
591 break;
592 case OPC63_MFFS:
593 FPU_EMU_EVCNT_INCR(mffs);
594 DPRINTF(FPE_INSN, ("fpu_execute: MFFS\n"));
595 memcpy(&fs->fpreg[rt], &fs->fpscr,
596 sizeof(fs->fpscr));
597 break;
598 case OPC63_MTFSF:
599 FPU_EMU_EVCNT_INCR(mtfsf);
600 DPRINTF(FPE_INSN, ("fpu_execute: MTFSF\n"));
601 if ((rt = instr.i_xfl.i_flm) == -1) {
602 mask = -1;
603 } else {
604 mask = 0;
605 /* Convert 1 bit -> 4 bits */
606 for (i = 0; i < 8; i++)
607 if (rt & (1 << i))
608 mask |=
609 (0xf << (4 * i));
610 }
611 a = (int *)&fs->fpreg[rb];
612 bits = a[1] & mask;
613 fe->fe_fpscr = (fe->fe_fpscr & ~mask) | bits;
614 break;
615 case OPC63_FCTID:
616 case OPC63_FCTIDZ:
617 FPU_EMU_EVCNT_INCR(fctid);
618 DPRINTF(FPE_INSN, ("fpu_execute: FCTID\n"));
619 fpu_explode(fe, fp = &fe->fe_f1, type, FR(rb));
620 type = FTYPE_LNG;
621 if (instr.i_x.i_xo == OPC63_FCTIDZ)
622 type |= FTYPE_RD_RZ;
623 break;
624 case OPC63_FCFID:
625 FPU_EMU_EVCNT_INCR(fcfid);
626 DPRINTF(FPE_INSN, ("fpu_execute: FCFID\n"));
627 type = FTYPE_LNG;
628 fpu_explode(fe, fp = &fe->fe_f1, type, FR(rb));
629 type = FTYPE_DBL | FTYPE_FPRF;
630 break;
631 default:
632 return (NOTFPU);
633 break;
634 }
635 } else {
636 /* Format A */
637 rt = instr.i_a.i_frt;
638 ra = instr.i_a.i_fra;
639 rb = instr.i_a.i_frb;
640 rc = instr.i_a.i_frc;
641
642 /*
643 * All arithmetic operations work on registers, which
644 * are stored as doubles.
645 */
646 type = FTYPE_DBL;
647 switch ((unsigned int)instr.i_a.i_xo) {
648 case OPC59_FDIVS:
649 FPU_EMU_EVCNT_INCR(fdiv);
650 DPRINTF(FPE_INSN, ("fpu_execute: FDIV\n"));
651 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
652 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
653 fp = fpu_div(fe);
654 break;
655 case OPC59_FSUBS:
656 FPU_EMU_EVCNT_INCR(fsub);
657 DPRINTF(FPE_INSN, ("fpu_execute: FSUB\n"));
658 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
659 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
660 fp = fpu_sub(fe);
661 break;
662 case OPC59_FADDS:
663 FPU_EMU_EVCNT_INCR(fadd);
664 DPRINTF(FPE_INSN, ("fpu_execute: FADD\n"));
665 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
666 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
667 fp = fpu_add(fe);
668 break;
669 case OPC59_FSQRTS:
670 FPU_EMU_EVCNT_INCR(fsqrt);
671 DPRINTF(FPE_INSN, ("fpu_execute: FSQRT\n"));
672 fpu_explode(fe, &fe->fe_f1, type, FR(rb));
673 fp = fpu_sqrt(fe);
674 break;
675 case OPC63M_FSEL:
676 FPU_EMU_EVCNT_INCR(fsel);
677 DPRINTF(FPE_INSN, ("fpu_execute: FSEL\n"));
678 a = (int *)&fe->fe_fpstate->fpreg[ra];
679 if ((( a[0] & 0x80000000) &&
680 ((a[0] & 0x7fffffff) | a[1])) ||
681 (( a[0] & 0x7ff00000) &&
682 ((a[0] & 0x000fffff) | a[1]))) {
683 /* negative/NaN or NaN */
684 rc = rb;
685 }
686 DPRINTF(FPE_INSN, ("f%d => f%d\n", rc, rt));
687 memcpy(&fs->fpreg[rt], &fs->fpreg[rc],
688 sizeof(double));
689 break;
690 case OPC59_FRES:
691 FPU_EMU_EVCNT_INCR(fpres);
692 DPRINTF(FPE_INSN, ("fpu_execute: FPRES\n"));
693 fpu_explode(fe, &fe->fe_f1, FTYPE_INT, 1);
694 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
695 fp = fpu_div(fe);
696 break;
697 case OPC59_FMULS:
698 FPU_EMU_EVCNT_INCR(fmul);
699 DPRINTF(FPE_INSN, ("fpu_execute: FMUL\n"));
700 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
701 fpu_explode(fe, &fe->fe_f2, type, FR(rc));
702 fp = fpu_mul(fe);
703 break;
704 case OPC63M_FRSQRTE:
705 /* Reciprocal sqrt() estimate */
706 FPU_EMU_EVCNT_INCR(frsqrte);
707 DPRINTF(FPE_INSN, ("fpu_execute: FRSQRTE\n"));
708 fpu_explode(fe, &fe->fe_f1, type, FR(rb));
709 fp = fpu_sqrt(fe);
710 fe->fe_f2 = *fp;
711 fpu_explode(fe, &fe->fe_f1, FTYPE_INT, 1);
712 fp = fpu_div(fe);
713 break;
714 case OPC59_FMSUBS:
715 FPU_EMU_EVCNT_INCR(fmulsub);
716 DPRINTF(FPE_INSN, ("fpu_execute: FMULSUB\n"));
717 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
718 fpu_explode(fe, &fe->fe_f2, type, FR(rc));
719 fp = fpu_mul(fe);
720 fe->fe_f1 = *fp;
721 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
722 fp = fpu_sub(fe);
723 break;
724 case OPC59_FMADDS:
725 FPU_EMU_EVCNT_INCR(fmuladd);
726 DPRINTF(FPE_INSN, ("fpu_execute: FMULADD\n"));
727 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
728 fpu_explode(fe, &fe->fe_f2, type, FR(rc));
729 fp = fpu_mul(fe);
730 fe->fe_f1 = *fp;
731 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
732 fp = fpu_add(fe);
733 break;
734 case OPC59_FNMSUBS:
735 FPU_EMU_EVCNT_INCR(fnmsub);
736 DPRINTF(FPE_INSN, ("fpu_execute: FNMSUB\n"));
737 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
738 fpu_explode(fe, &fe->fe_f2, type, FR(rc));
739 fp = fpu_mul(fe);
740 fe->fe_f1 = *fp;
741 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
742 fp = fpu_sub(fe);
743 /* Negate */
744 fp->fp_sign ^= 1;
745 break;
746 case OPC59_FNMADDS:
747 FPU_EMU_EVCNT_INCR(fnmadd);
748 DPRINTF(FPE_INSN, ("fpu_execute: FNMADD\n"));
749 fpu_explode(fe, &fe->fe_f1, type, FR(ra));
750 fpu_explode(fe, &fe->fe_f2, type, FR(rc));
751 fp = fpu_mul(fe);
752 fe->fe_f1 = *fp;
753 fpu_explode(fe, &fe->fe_f2, type, FR(rb));
754 fp = fpu_add(fe);
755 /* Negate */
756 fp->fp_sign ^= 1;
757 break;
758 default:
759 return (NOTFPU);
760 break;
761 }
762
763 /* If the instruction was single precision, round */
764 if (!(instr.i_any.i_opcd & 0x4)) {
765 fpu_implode(fe, fp, FTYPE_SNG | FTYPE_FPRF,
766 &FR(rt));
767 fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG,
768 FR(rt));
769 } else
770 type |= FTYPE_FPRF;
771 }
772 } else {
773 return (NOTFPU);
774 }
775
776 /*
777 * ALU operation is complete. Collapse the result and then check
778 * for exceptions. If we got any, and they are enabled, do not
779 * alter the destination register, just stop with an exception.
780 * Otherwise set new current exceptions and accrue.
781 */
782 if (fp)
783 fpu_implode(fe, fp, type, &FR(rt));
784 cx = fe->fe_cx;
785 fsr = fe->fe_fpscr & ~(FPSCR_FEX|FPSCR_VX);
786 if (cx != 0) {
787 if (mtfsb1 == 0 && (cx & FPSCR_FPRF) != 0) {
788 /* Need to replace CC */
789 fsr &= ~FPSCR_FPRF;
790 }
791 fsr |= cx;
792 DPRINTF(FPE_INSN, ("fpu_execute: cx %x, fsr %x\n", cx, fsr));
793 }
794 if (fsr & FPSR_INV)
795 fsr |= FPSCR_VX;
796 mask = (fsr & FPSR_EX) << (25 - 3);
797 if (fsr & mask)
798 fsr |= FPSCR_FEX;
799 if ((fsr ^ fe->fe_fpscr) & FPSR_EX_MSK)
800 fsr |= FPSCR_FX;
801
802 if (cond) {
803 cond = fsr & 0xf0000000;
804 /* Isolate condition codes */
805 cond >>= 28;
806 /* Move fpu condition codes to cr[1] */
807 tf->tf_cr &= ~(0x0f000000);
808 tf->tf_cr |= (cond<<24);
809 DPRINTF(FPE_INSN, ("fpu_execute: cr[1] <= %x\n", cond));
810 }
811
812 if (setcr) {
813 cond = fsr & FPSCR_FPCC;
814 /* Isolate condition codes */
815 cond <<= 16;
816 /* Move fpu condition codes to cr[bf/4] */
817 tf->tf_cr &= ~(0xf0000000>>bf);
818 tf->tf_cr |= (cond>>bf);
819 DPRINTF(FPE_INSN, ("fpu_execute: cr[%d] (cr=%x) <= %x\n", bf/4, tf->tf_cr, cond));
820 }
821
822 ((int *)&fs->fpscr)[1] = fsr;
823 if (fsr & FPSCR_FEX)
824 return(FPE);
825 return (0); /* success */
826 }
827