Home | History | Annotate | Line # | Download | only in fpu
fpu_emu.c revision 1.31
      1 /*	$NetBSD: fpu_emu.c,v 1.31 2020/07/15 09:22:26 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.31 2020/07/15 09:22:26 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_EXOP	(FPSR_EX_MSK&(~FPSR_EX))
    151 
    152 
    153 int fpe_debug = 0;
    154 
    155 #ifdef DDB
    156 extern vaddr_t opc_disasm(vaddr_t loc, int opcode);
    157 #endif
    158 
    159 #ifdef DEBUG
    160 /*
    161  * Dump a `fpn' structure.
    162  */
    163 void
    164 fpu_dumpfpn(struct fpn *fp)
    165 {
    166 	static const char *class[] = {
    167 		"SNAN", "QNAN", "ZERO", "NUM", "INF"
    168 	};
    169 
    170 	KASSERT(fp != NULL);
    171 
    172 	printf("%s %c.%x %x %x %xE%d\n", class[fp->fp_class + 2],
    173 		fp->fp_sign ? '-' : ' ',
    174 		fp->fp_mant[0],	fp->fp_mant[1],
    175 		fp->fp_mant[2], fp->fp_mant[3],
    176 		fp->fp_exp);
    177 }
    178 #endif
    179 
    180 /*
    181  * fpu_execute returns the following error numbers (0 = no error):
    182  */
    183 #define	FPE		1	/* take a floating point exception */
    184 #define	NOTFPU		2	/* not an FPU instruction */
    185 #define	FAULT		3
    186 
    187 
    188 /*
    189  * Emulate a floating-point instruction.
    190  * Return zero for success, else signal number.
    191  * (Typically: zero, SIGFPE, SIGILL, SIGSEGV)
    192  */
    193 bool
    194 fpu_emulate(struct trapframe *tf, struct fpreg *fpf, ksiginfo_t *ksi)
    195 {
    196 	struct pcb *pcb;
    197 	union instr insn;
    198 	struct fpemu fe;
    199 
    200 	KSI_INIT_TRAP(ksi);
    201 	ksi->ksi_signo = 0;
    202 	ksi->ksi_addr = (void *)tf->tf_srr0;
    203 
    204 	/* initialize insn.is_datasize to tell it is *not* initialized */
    205 	fe.fe_fpstate = fpf;
    206 	fe.fe_cx = 0;
    207 
    208 	/* always set this (to avoid a warning) */
    209 
    210 	if (copyin((void *) (tf->tf_srr0), &insn.i_int, sizeof (insn.i_int))) {
    211 #ifdef DEBUG
    212 		printf("fpu_emulate: fault reading opcode\n");
    213 #endif
    214 		ksi->ksi_signo = SIGSEGV;
    215 		ksi->ksi_trap = EXC_ISI;
    216 		ksi->ksi_code = SEGV_MAPERR;
    217 		return true;
    218 	}
    219 
    220 	DPRINTF(FPE_EX, ("fpu_emulate: emulating insn %x at %p\n",
    221 	    insn.i_int, (void *)tf->tf_srr0));
    222 
    223 	if ((insn.i_any.i_opcd == OPC_TWI) ||
    224 	    ((insn.i_any.i_opcd == OPC_integer_31) &&
    225 	    (insn.i_x.i_xo == OPC31_TW))) {
    226 		/* Check for the two trap insns. */
    227 		DPRINTF(FPE_EX, ("fpu_emulate: SIGTRAP\n"));
    228 		ksi->ksi_signo = SIGTRAP;
    229 		ksi->ksi_trap = EXC_PGM;
    230 		ksi->ksi_code = TRAP_BRKPT;
    231 		return true;
    232 	}
    233 	switch (fpu_execute(tf, &fe, &insn)) {
    234 	case 0:
    235 success:
    236 		DPRINTF(FPE_EX, ("fpu_emulate: success\n"));
    237 		tf->tf_srr0 += 4;
    238 		return true;
    239 
    240 	case FPE:
    241 		pcb = lwp_getpcb(curlwp);
    242 		if ((pcb->pcb_flags & PSL_FE_PREC) == 0)
    243 			goto success;
    244 		DPRINTF(FPE_EX, ("fpu_emulate: SIGFPE\n"));
    245 		ksi->ksi_signo = SIGFPE;
    246 		ksi->ksi_trap = EXC_PGM;
    247 		ksi->ksi_code = fpu_get_fault_code();
    248 		return true;
    249 
    250 	case FAULT:
    251 		DPRINTF(FPE_EX, ("fpu_emulate: SIGSEGV\n"));
    252 		ksi->ksi_signo = SIGSEGV;
    253 		ksi->ksi_trap = EXC_DSI;
    254 		ksi->ksi_code = SEGV_MAPERR;
    255 		ksi->ksi_addr = (void *)fe.fe_addr;
    256 		return true;
    257 
    258 	case NOTFPU:
    259 	default:
    260 		DPRINTF(FPE_EX, ("fpu_emulate: SIGILL\n"));
    261 #if defined(DDB) && defined(DEBUG)
    262 		if (fpe_debug & FPE_EX) {
    263 			printf("fpu_emulate:  illegal insn %x at %p:",
    264 			insn.i_int, (void *) (tf->tf_srr0));
    265 			opc_disasm((vaddr_t)(tf->tf_srr0), insn.i_int);
    266 		}
    267 #endif
    268 		return false;
    269 	}
    270 }
    271 
    272 /*
    273  * Execute an FPU instruction (one that runs entirely in the FPU; not
    274  * FBfcc or STF, for instance).  On return, fe->fe_fs->fs_fsr will be
    275  * modified to reflect the setting the hardware would have left.
    276  *
    277  * Note that we do not catch all illegal opcodes, so you can, for instance,
    278  * multiply two integers this way.
    279  */
    280 int
    281 fpu_execute(struct trapframe *tf, struct fpemu *fe, union instr *insn)
    282 {
    283 	struct fpn *fp;
    284 	union instr instr = *insn;
    285 	int *a;
    286 	vaddr_t addr;
    287 	int ra, rb, rc, rt, type, mask, fsr, cx, bf, setcr;
    288 	unsigned int cond;
    289 	struct fpreg *fs;
    290 
    291 	/* Setup work. */
    292 	fp = NULL;
    293 	fs = fe->fe_fpstate;
    294 	fe->fe_fpscr = ((int *)&fs->fpscr)[1];
    295 
    296 	/*
    297 	 * On PowerPC all floating point values are stored in registers
    298 	 * as doubles, even when used for single precision operations.
    299 	 */
    300 	type = FTYPE_DBL;
    301 	cond = instr.i_any.i_rc;
    302 	setcr = 0;
    303 	bf = 0;	/* XXX gcc */
    304 
    305 #if defined(DDB) && defined(DEBUG)
    306 	if (fpe_debug & FPE_EX) {
    307 		vaddr_t loc = tf->tf_srr0;
    308 
    309 		printf("Trying to emulate: %p ", (void *)loc);
    310 		opc_disasm(loc, instr.i_int);
    311 	}
    312 #endif
    313 
    314 	/*
    315 	 * `Decode' and execute instruction.
    316 	 */
    317 
    318 	if ((instr.i_any.i_opcd >= OPC_LFS && instr.i_any.i_opcd <= OPC_STFDU) ||
    319 	    instr.i_any.i_opcd == OPC_integer_31) {
    320 		/*
    321 		 * Handle load/store insns:
    322 		 *
    323 		 * Convert to/from single if needed, calculate addr,
    324 		 * and update index reg if needed.
    325 		 */
    326 		uint64_t buf;
    327 		size_t size = sizeof(float);
    328 		int store, update;
    329 
    330 		cond = 0; /* ld/st never set condition codes */
    331 
    332 
    333 		if (instr.i_any.i_opcd == OPC_integer_31) {
    334 			if (instr.i_x.i_xo == OPC31_STFIWX) {
    335 				FPU_EMU_EVCNT_INCR(stfiwx);
    336 
    337 				/* Store as integer */
    338 				ra = instr.i_x.i_ra;
    339 				rb = instr.i_x.i_rb;
    340 				DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n",
    341 					ra, tf->tf_fixreg[ra], rb, tf->tf_fixreg[rb]));
    342 
    343 				addr = tf->tf_fixreg[rb];
    344 				if (ra != 0)
    345 					addr += tf->tf_fixreg[ra];
    346 				rt = instr.i_x.i_rt;
    347 				a = (int *)&fs->fpreg[rt];
    348 				DPRINTF(FPE_INSN,
    349 					("fpu_execute: Store INT %x at %p\n",
    350 						a[1], (void *)addr));
    351 				if (copyout(&a[1], (void *)addr, sizeof(int))) {
    352 					fe->fe_addr = addr;
    353 					return (FAULT);
    354 				}
    355 				return (0);
    356 			}
    357 
    358 			if ((instr.i_x.i_xo & OPC31_FPMASK) != OPC31_FPOP)
    359 				/* Not an indexed FP load/store op */
    360 				return (NOTFPU);
    361 
    362 			store = (instr.i_x.i_xo & 0x80);
    363 			if (instr.i_x.i_xo & 0x40)
    364 				size = sizeof(double);
    365 			else
    366 				type = FTYPE_SNG;
    367 			update = (instr.i_x.i_xo & 0x20);
    368 
    369 			/* calculate EA of load/store */
    370 			ra = instr.i_x.i_ra;
    371 			rb = instr.i_x.i_rb;
    372 			DPRINTF(FPE_INSN, ("reg %d has %lx reg %d has %lx\n",
    373 				ra, tf->tf_fixreg[ra], rb, tf->tf_fixreg[rb]));
    374 			addr = tf->tf_fixreg[rb];
    375 			if (ra != 0)
    376 				addr += tf->tf_fixreg[ra];
    377 			rt = instr.i_x.i_rt;
    378 		} else {
    379 			store = instr.i_d.i_opcd & 0x4;
    380 			if (instr.i_d.i_opcd & 0x2)
    381 				size = sizeof(double);
    382 			else
    383 				type = FTYPE_SNG;
    384 			update = instr.i_d.i_opcd & 0x1;
    385 
    386 			/* calculate EA of load/store */
    387 			ra = instr.i_d.i_ra;
    388 			addr = instr.i_d.i_d;
    389 			DPRINTF(FPE_INSN, ("reg %d has %lx displ %lx\n",
    390 				ra, tf->tf_fixreg[ra], addr));
    391 			if (ra != 0)
    392 				addr += tf->tf_fixreg[ra];
    393 			rt = instr.i_d.i_rt;
    394 		}
    395 
    396 		if (update && ra == 0)
    397 			return (NOTFPU);
    398 
    399 		if (store) {
    400 			/* Store */
    401 			FPU_EMU_EVCNT_INCR(fpstore);
    402 			if (type != FTYPE_DBL) {
    403 				DPRINTF(FPE_INSN,
    404 					("fpu_execute: Store SNG at %p\n",
    405 						(void *)addr));
    406 				fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL, rt);
    407 				fpu_implode(fe, fp, type, (void *)&buf);
    408 				if (copyout(&buf, (void *)addr, size)) {
    409 					fe->fe_addr = addr;
    410 					return (FAULT);
    411 				}
    412 			} else {
    413 				DPRINTF(FPE_INSN,
    414 					("fpu_execute: Store DBL at %p\n",
    415 						(void *)addr));
    416 				if (copyout(&fs->fpreg[rt], (void *)addr, size)) {
    417 					fe->fe_addr = addr;
    418 					return (FAULT);
    419 				}
    420 			}
    421 		} else {
    422 			/* Load */
    423 			FPU_EMU_EVCNT_INCR(fpload);
    424 			DPRINTF(FPE_INSN, ("fpu_execute: Load from %p\n",
    425 				(void *)addr));
    426 			if (copyin((const void *)addr, &fs->fpreg[rt], size)) {
    427 				fe->fe_addr = addr;
    428 				return (FAULT);
    429 			}
    430 			if (type != FTYPE_DBL) {
    431 				fpu_explode(fe, fp = &fe->fe_f1, type, rt);
    432 				fpu_implode(fe, fp, FTYPE_DBL,
    433 					(u_int *)&fs->fpreg[rt]);
    434 			}
    435 		}
    436 		if (update)
    437 			tf->tf_fixreg[ra] = addr;
    438 		/* Complete. */
    439 		return (0);
    440 #ifdef notyet
    441 	} else if (instr.i_any.i_opcd == OPC_load_st_62) {
    442 		/* These are 64-bit extenstions */
    443 		return (NOTFPU);
    444 #endif
    445 	} else if (instr.i_any.i_opcd == OPC_sp_fp_59 ||
    446 		instr.i_any.i_opcd == OPC_dp_fp_63) {
    447 
    448 
    449 		if (instr.i_any.i_opcd == OPC_dp_fp_63 &&
    450 		    !(instr.i_a.i_xo & OPC63M_MASK)) {
    451 			/* Format X */
    452 			rt = instr.i_x.i_rt;
    453 			ra = instr.i_x.i_ra;
    454 			rb = instr.i_x.i_rb;
    455 
    456 
    457 			/* One of the special opcodes.... */
    458 			switch (instr.i_x.i_xo) {
    459 			case	OPC63_FCMPU:
    460 				FPU_EMU_EVCNT_INCR(fcmpu);
    461 				DPRINTF(FPE_INSN, ("fpu_execute: FCMPU\n"));
    462 				rt >>= 2;
    463 				fpu_explode(fe, &fe->fe_f1, type, ra);
    464 				fpu_explode(fe, &fe->fe_f2, type, rb);
    465 				fpu_compare(fe, 0);
    466 				/* Make sure we do the condition regs. */
    467 				cond = 0;
    468 				/* N.B.: i_rs is already left shifted by two. */
    469 				bf = instr.i_x.i_rs & 0xfc;
    470 				setcr = 1;
    471 				break;
    472 
    473 			case	OPC63_FRSP:
    474 				/*
    475 				 * Convert to single:
    476 				 *
    477 				 * PowerPC uses this to round a double
    478 				 * precision value to single precision,
    479 				 * but values in registers are always
    480 				 * stored in double precision format.
    481 				 */
    482 				FPU_EMU_EVCNT_INCR(frsp);
    483 				DPRINTF(FPE_INSN, ("fpu_execute: FRSP\n"));
    484 				fpu_explode(fe, fp = &fe->fe_f1, FTYPE_DBL, rb);
    485 				fpu_implode(fe, fp, FTYPE_SNG,
    486 					(u_int *)&fs->fpreg[rt]);
    487 				fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG, rt);
    488 				type = FTYPE_DBL;
    489 				break;
    490 			case	OPC63_FCTIW:
    491 			case	OPC63_FCTIWZ:
    492 				FPU_EMU_EVCNT_INCR(fctiw);
    493 				DPRINTF(FPE_INSN, ("fpu_execute: FCTIW\n"));
    494 				fpu_explode(fe, fp = &fe->fe_f1, type, rb);
    495 				type = FTYPE_INT;
    496 				break;
    497 			case	OPC63_FCMPO:
    498 				FPU_EMU_EVCNT_INCR(fcmpo);
    499 				DPRINTF(FPE_INSN, ("fpu_execute: FCMPO\n"));
    500 				rt >>= 2;
    501 				fpu_explode(fe, &fe->fe_f1, type, ra);
    502 				fpu_explode(fe, &fe->fe_f2, type, rb);
    503 				fpu_compare(fe, 1);
    504 				/* Make sure we do the condition regs. */
    505 				cond = 0;
    506 				/* N.B.: i_rs is already left shifted by two. */
    507 				bf = instr.i_x.i_rs & 0xfc;
    508 				setcr = 1;
    509 				break;
    510 			case	OPC63_MTFSB1:
    511 				FPU_EMU_EVCNT_INCR(mtfsb1);
    512 				DPRINTF(FPE_INSN, ("fpu_execute: MTFSB1\n"));
    513 				fe->fe_fpscr |=
    514 					(~(FPSCR_VX|FPSR_EX) & (1<<(31-rt)));
    515 				break;
    516 			case	OPC63_FNEG:
    517 				FPU_EMU_EVCNT_INCR(fnegabs);
    518 				DPRINTF(FPE_INSN, ("fpu_execute: FNEGABS\n"));
    519 				memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
    520 					sizeof(double));
    521 				a = (int *)&fs->fpreg[rt];
    522 				*a ^= (1 << 31);
    523 				break;
    524 			case	OPC63_MCRFS:
    525 				FPU_EMU_EVCNT_INCR(mcrfs);
    526 				DPRINTF(FPE_INSN, ("fpu_execute: MCRFS\n"));
    527 				cond = 0;
    528 				rt &= 0x1c;
    529 				ra &= 0x1c;
    530 				/* Extract the bits we want */
    531 				mask = (fe->fe_fpscr >> (28 - ra)) & 0xf;
    532 				/* Clear the bits we copied. */
    533 				fe->fe_cx =
    534 					(FPSR_EX_MSK | (0xf << (28 - ra)));
    535 				fe->fe_fpscr &= fe->fe_cx;
    536 				/* Now shove them in the right part of cr */
    537 				tf->tf_cr &= ~(0xf << (28 - rt));
    538 				tf->tf_cr |= (mask << (28 - rt));
    539 				break;
    540 			case	OPC63_MTFSB0:
    541 				FPU_EMU_EVCNT_INCR(mtfsb0);
    542 				DPRINTF(FPE_INSN, ("fpu_execute: MTFSB0\n"));
    543 				fe->fe_fpscr &=
    544 					((FPSCR_VX|FPSR_EX) & ~(1<<(31-rt)));
    545 				break;
    546 			case	OPC63_FMR:
    547 				FPU_EMU_EVCNT_INCR(fmr);
    548 				DPRINTF(FPE_INSN, ("fpu_execute: FMR\n"));
    549 				memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
    550 					sizeof(double));
    551 				break;
    552 			case	OPC63_MTFSFI:
    553 				FPU_EMU_EVCNT_INCR(mtfsfi);
    554 				DPRINTF(FPE_INSN, ("fpu_execute: MTFSFI\n"));
    555 				rb >>= 1;
    556 				rt &= 0x1c; /* Already left-shifted 4 */
    557 				fe->fe_cx = rb << (28 - rt);
    558 				mask = 0xf<<(28 - rt);
    559 				fe->fe_fpscr = (fe->fe_fpscr & ~mask) |
    560 					fe->fe_cx;
    561 /* XXX weird stuff about OX, FX, FEX, and VX should be handled */
    562 				break;
    563 			case	OPC63_FNABS:
    564 				FPU_EMU_EVCNT_INCR(fnabs);
    565 				DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
    566 				memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
    567 					sizeof(double));
    568 				a = (int *)&fs->fpreg[rt];
    569 				*a |= (1 << 31);
    570 				break;
    571 			case	OPC63_FABS:
    572 				FPU_EMU_EVCNT_INCR(fabs);
    573 				DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
    574 				memcpy(&fs->fpreg[rt], &fs->fpreg[rb],
    575 					sizeof(double));
    576 				a = (int *)&fs->fpreg[rt];
    577 				*a &= ~(1 << 31);
    578 				break;
    579 			case	OPC63_MFFS:
    580 				FPU_EMU_EVCNT_INCR(mffs);
    581 				DPRINTF(FPE_INSN, ("fpu_execute: MFFS\n"));
    582 				/* XXX FEX is not sticky */
    583 				fs->fpscr &= ~FPSCR_FEX;
    584 				memcpy(&fs->fpreg[rt], &fs->fpscr,
    585 					sizeof(fs->fpscr));
    586 				break;
    587 			case	OPC63_MTFSF:
    588 				FPU_EMU_EVCNT_INCR(mtfsf);
    589 				DPRINTF(FPE_INSN, ("fpu_execute: MTFSF\n"));
    590 				if ((rt = instr.i_xfl.i_flm) == -1)
    591 					mask = -1;
    592 				else {
    593 					mask = 0;
    594 					/* Convert 1 bit -> 4 bits */
    595 					for (ra = 0; ra < 8; ra ++)
    596 						if (rt & (1<<ra))
    597 							mask |= (0xf<<(4*ra));
    598 				}
    599 				a = (int *)&fs->fpreg[rb];
    600 				fe->fe_cx = mask & a[1];
    601 				fe->fe_fpscr = (fe->fe_fpscr&~mask) |
    602 					(fe->fe_cx);
    603 /* XXX weird stuff about OX, FX, FEX, and VX should be handled */
    604 				break;
    605 			case	OPC63_FCTID:
    606 			case	OPC63_FCTIDZ:
    607 				FPU_EMU_EVCNT_INCR(fctid);
    608 				DPRINTF(FPE_INSN, ("fpu_execute: FCTID\n"));
    609 				fpu_explode(fe, fp = &fe->fe_f1, type, rb);
    610 				type = FTYPE_LNG;
    611 				break;
    612 			case	OPC63_FCFID:
    613 				FPU_EMU_EVCNT_INCR(fcfid);
    614 				DPRINTF(FPE_INSN, ("fpu_execute: FCFID\n"));
    615 				type = FTYPE_LNG;
    616 				fpu_explode(fe, fp = &fe->fe_f1, type, rb);
    617 				type = FTYPE_DBL;
    618 				break;
    619 			default:
    620 				return (NOTFPU);
    621 				break;
    622 			}
    623 		} else {
    624 			/* Format A */
    625 			rt = instr.i_a.i_frt;
    626 			ra = instr.i_a.i_fra;
    627 			rb = instr.i_a.i_frb;
    628 			rc = instr.i_a.i_frc;
    629 
    630 			/*
    631 			 * All arithmetic operations work on registers, which
    632 			 * are stored as doubles.
    633 			 */
    634 			type = FTYPE_DBL;
    635 			switch ((unsigned int)instr.i_a.i_xo) {
    636 			case	OPC59_FDIVS:
    637 				FPU_EMU_EVCNT_INCR(fdiv);
    638 				DPRINTF(FPE_INSN, ("fpu_execute: FDIV\n"));
    639 				fpu_explode(fe, &fe->fe_f1, type, ra);
    640 				fpu_explode(fe, &fe->fe_f2, type, rb);
    641 				fp = fpu_div(fe);
    642 				break;
    643 			case	OPC59_FSUBS:
    644 				FPU_EMU_EVCNT_INCR(fsub);
    645 				DPRINTF(FPE_INSN, ("fpu_execute: FSUB\n"));
    646 				fpu_explode(fe, &fe->fe_f1, type, ra);
    647 				fpu_explode(fe, &fe->fe_f2, type, rb);
    648 				fp = fpu_sub(fe);
    649 				break;
    650 			case	OPC59_FADDS:
    651 				FPU_EMU_EVCNT_INCR(fadd);
    652 				DPRINTF(FPE_INSN, ("fpu_execute: FADD\n"));
    653 				fpu_explode(fe, &fe->fe_f1, type, ra);
    654 				fpu_explode(fe, &fe->fe_f2, type, rb);
    655 				fp = fpu_add(fe);
    656 				break;
    657 			case	OPC59_FSQRTS:
    658 				FPU_EMU_EVCNT_INCR(fsqrt);
    659 				DPRINTF(FPE_INSN, ("fpu_execute: FSQRT\n"));
    660 				fpu_explode(fe, &fe->fe_f1, type, rb);
    661 				fp = fpu_sqrt(fe);
    662 				break;
    663 			case	OPC63M_FSEL:
    664 				FPU_EMU_EVCNT_INCR(fsel);
    665 				DPRINTF(FPE_INSN, ("fpu_execute: FSEL\n"));
    666 				a = (int *)&fe->fe_fpstate->fpreg[ra];
    667 				if ((*a & 0x80000000) && (*a & 0x7fffffff))
    668 					/* fra < 0 */
    669 					rc = rb;
    670 				DPRINTF(FPE_INSN, ("f%d => f%d\n", rc, rt));
    671 				memcpy(&fs->fpreg[rt], &fs->fpreg[rc],
    672 					sizeof(double));
    673 				break;
    674 			case	OPC59_FRES:
    675 				FPU_EMU_EVCNT_INCR(fpres);
    676 				DPRINTF(FPE_INSN, ("fpu_execute: FPRES\n"));
    677 				fpu_explode(fe, &fe->fe_f1, type, rb);
    678 				fp = fpu_sqrt(fe);
    679 				/* now we've gotta overwrite the dest reg */
    680 				*((int *)&fe->fe_fpstate->fpreg[rt]) = 1;
    681 				fpu_explode(fe, &fe->fe_f1, FTYPE_INT, rt);
    682 				fpu_div(fe);
    683 				break;
    684 			case	OPC59_FMULS:
    685 				FPU_EMU_EVCNT_INCR(fmul);
    686 				DPRINTF(FPE_INSN, ("fpu_execute: FMUL\n"));
    687 				fpu_explode(fe, &fe->fe_f1, type, ra);
    688 				fpu_explode(fe, &fe->fe_f2, type, rc);
    689 				fp = fpu_mul(fe);
    690 				break;
    691 			case	OPC63M_FRSQRTE:
    692 				/* Reciprocal sqrt() estimate */
    693 				FPU_EMU_EVCNT_INCR(frsqrte);
    694 				DPRINTF(FPE_INSN, ("fpu_execute: FRSQRTE\n"));
    695 				fpu_explode(fe, &fe->fe_f1, type, rb);
    696 				fp = fpu_sqrt(fe);
    697 				fe->fe_f2 = *fp;
    698 				/* now we've gotta overwrite the dest reg */
    699 				*((int *)&fe->fe_fpstate->fpreg[rt]) = 1;
    700 				fpu_explode(fe, &fe->fe_f1, FTYPE_INT, rt);
    701 				fpu_div(fe);
    702 				break;
    703 			case	OPC59_FMSUBS:
    704 				FPU_EMU_EVCNT_INCR(fmulsub);
    705 				DPRINTF(FPE_INSN, ("fpu_execute: FMULSUB\n"));
    706 				fpu_explode(fe, &fe->fe_f1, type, ra);
    707 				fpu_explode(fe, &fe->fe_f2, type, rc);
    708 				fp = fpu_mul(fe);
    709 				fe->fe_f1 = *fp;
    710 				fpu_explode(fe, &fe->fe_f2, type, rb);
    711 				fp = fpu_sub(fe);
    712 				break;
    713 			case	OPC59_FMADDS:
    714 				FPU_EMU_EVCNT_INCR(fmuladd);
    715 				DPRINTF(FPE_INSN, ("fpu_execute: FMULADD\n"));
    716 				fpu_explode(fe, &fe->fe_f1, type, ra);
    717 				fpu_explode(fe, &fe->fe_f2, type, rc);
    718 				fp = fpu_mul(fe);
    719 				fe->fe_f1 = *fp;
    720 				fpu_explode(fe, &fe->fe_f2, type, rb);
    721 				fp = fpu_add(fe);
    722 				break;
    723 			case	OPC59_FNMSUBS:
    724 				FPU_EMU_EVCNT_INCR(fnmsub);
    725 				DPRINTF(FPE_INSN, ("fpu_execute: FNMSUB\n"));
    726 				fpu_explode(fe, &fe->fe_f1, type, ra);
    727 				fpu_explode(fe, &fe->fe_f2, type, rc);
    728 				fp = fpu_mul(fe);
    729 				fe->fe_f1 = *fp;
    730 				fpu_explode(fe, &fe->fe_f2, type, rb);
    731 				fp = fpu_sub(fe);
    732 				/* Negate */
    733 				fp->fp_sign ^= 1;
    734 				break;
    735 			case	OPC59_FNMADDS:
    736 				FPU_EMU_EVCNT_INCR(fnmadd);
    737 				DPRINTF(FPE_INSN, ("fpu_execute: FNMADD\n"));
    738 				fpu_explode(fe, &fe->fe_f1, type, ra);
    739 				fpu_explode(fe, &fe->fe_f2, type, rc);
    740 				fp = fpu_mul(fe);
    741 				fe->fe_f1 = *fp;
    742 				fpu_explode(fe, &fe->fe_f2, type, rb);
    743 				fp = fpu_add(fe);
    744 				/* Negate */
    745 				fp->fp_sign ^= 1;
    746 				break;
    747 			default:
    748 				return (NOTFPU);
    749 				break;
    750 			}
    751 
    752 			/* If the instruction was single precision, round */
    753 			if (!(instr.i_any.i_opcd & 0x4)) {
    754 				fpu_implode(fe, fp, FTYPE_SNG,
    755 					(u_int *)&fs->fpreg[rt]);
    756 				fpu_explode(fe, fp = &fe->fe_f1, FTYPE_SNG, rt);
    757 			}
    758 		}
    759 	} else {
    760 		return (NOTFPU);
    761 	}
    762 
    763 	/*
    764 	 * ALU operation is complete.  Collapse the result and then check
    765 	 * for exceptions.  If we got any, and they are enabled, do not
    766 	 * alter the destination register, just stop with an exception.
    767 	 * Otherwise set new current exceptions and accrue.
    768 	 */
    769 	if (fp)
    770 		fpu_implode(fe, fp, type, (u_int *)&fs->fpreg[rt]);
    771 	cx = fe->fe_cx;
    772 	fsr = fe->fe_fpscr;
    773 	if (cx != 0) {
    774 		fsr &= ~FPSCR_FX;
    775 		if ((cx^fsr)&FPSR_EX_MSK)
    776 			fsr |= FPSCR_FX;
    777 		mask = fsr & FPSR_EX;
    778 		mask <<= (25-3);
    779 		if (cx & mask)
    780 			fsr |= FPSCR_FEX;
    781 		if (cx & FPSCR_FPRF) {
    782 			/* Need to replace CC */
    783 			fsr &= ~FPSCR_FPRF;
    784 		}
    785 		if (cx & (FPSR_EXOP))
    786 			fsr |= FPSCR_VX;
    787 		fsr |= cx;
    788 		DPRINTF(FPE_INSN, ("fpu_execute: cx %x, fsr %x\n", cx, fsr));
    789 	}
    790 
    791 	if (cond) {
    792 		cond = fsr & 0xf0000000;
    793 		/* Isolate condition codes */
    794 		cond >>= 28;
    795 		/* Move fpu condition codes to cr[1] */
    796 		tf->tf_cr &= (0x0f000000);
    797 		tf->tf_cr |= (cond<<24);
    798 		DPRINTF(FPE_INSN, ("fpu_execute: cr[1] <= %x\n", cond));
    799 	}
    800 
    801 	if (setcr) {
    802 		cond = fsr & FPSCR_FPCC;
    803 		/* Isolate condition codes */
    804 		cond <<= 16;
    805 		/* Move fpu condition codes to cr[1] */
    806 		tf->tf_cr &= ~(0xf0000000>>bf);
    807 		tf->tf_cr |= (cond>>bf);
    808 		DPRINTF(FPE_INSN, ("fpu_execute: cr[%d] (cr=%x) <= %x\n", bf/4, tf->tf_cr, cond));
    809 	}
    810 
    811 	((int *)&fs->fpscr)[1] = fsr;
    812 	if (fsr & FPSCR_FEX)
    813 		return(FPE);
    814 	return (0);	/* success */
    815 }
    816