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