Home | History | Annotate | Line # | Download | only in fpe
fpu_emulate.c revision 1.42
      1 /*	$NetBSD: fpu_emulate.c,v 1.42 2024/12/28 05:52:53 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross
      5  * some portion Copyright (c) 1995 Ken Nakata
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  * 4. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by Gordon Ross
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * mc68881 emulator
     36  * XXX - Just a start at it for now...
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: fpu_emulate.c,v 1.42 2024/12/28 05:52:53 isaki Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/types.h>
     44 #include <sys/signal.h>
     45 #include <sys/systm.h>
     46 #include <machine/frame.h>
     47 
     48 #if defined(DDB) && defined(DEBUG_FPE)
     49 # include <m68k/db_machdep.h>
     50 #endif
     51 
     52 #include "fpu_emulate.h"
     53 
     54 #define	fpe_abort(tfp, ksi, signo, code)			\
     55 	do {							\
     56 		(ksi)->ksi_signo = (signo);			\
     57 		(ksi)->ksi_code = (code);			\
     58 		(ksi)->ksi_addr = (void *)(frame)->f_pc;	\
     59 		return -1;					\
     60 	} while (/* CONSTCOND */ 0)
     61 
     62 static int fpu_emul_fmovmcr(struct fpemu *, struct instruction *);
     63 static int fpu_emul_fmovm(struct fpemu *, struct instruction *);
     64 static int fpu_emul_arith(struct fpemu *, struct instruction *);
     65 static int fpu_emul_type1(struct fpemu *, struct instruction *);
     66 static int fpu_emul_brcc(struct fpemu *, struct instruction *);
     67 static int test_cc(struct fpemu *, int);
     68 
     69 #ifdef DEBUG_FPE
     70 #define DUMP_INSN(insn)							\
     71 	printf("%s: insn={adv=%d,siz=%d,op=%04x,w1=%04x}\n",		\
     72 	    __func__,							\
     73 	    (insn)->is_advance, (insn)->is_datasize,			\
     74 	    (insn)->is_opcode, (insn)->is_word1)
     75 #define DPRINTF(x)	printf x
     76 #else
     77 #define DUMP_INSN(insn)	do {} while (/* CONSTCOND */ 0)
     78 #define DPRINTF(x)	do {} while (/* CONSTCOND */ 0)
     79 #endif
     80 
     81 /*
     82  * Emulate a floating-point instruction.
     83  * Return zero for success, else signal number.
     84  * (Typically: zero, SIGFPE, SIGILL, SIGSEGV)
     85  */
     86 int
     87 fpu_emulate(struct frame *frame, struct fpframe *fpf, ksiginfo_t *ksi)
     88 {
     89 	static struct instruction insn;
     90 	static struct fpemu fe;
     91 	int optype, sig;
     92 	unsigned short sval;
     93 
     94 	/* initialize insn.is_datasize to tell it is *not* initialized */
     95 	insn.is_datasize = -1;
     96 
     97 	fe.fe_frame = frame;
     98 	fe.fe_fpframe = fpf;
     99 	fe.fe_fpsr = fpf->fpf_fpsr;
    100 	fe.fe_fpcr = fpf->fpf_fpcr;
    101 
    102 	DPRINTF(("%s: ENTERING: FPSR=%08x, FPCR=%08x\n",
    103 	    __func__, fe.fe_fpsr, fe.fe_fpcr));
    104 
    105 	/* always set this (to avoid a warning) */
    106 	insn.is_pc = frame->f_pc;
    107 	insn.is_nextpc = 0;
    108 	if (frame->f_format == 4) {
    109 		/*
    110 		 * A format 4 is generated by the 68{EC,LC}040.  The PC is
    111 		 * already set to the instruction following the faulting
    112 		 * instruction.  We need to calculate that, anyway.  The
    113 		 * fslw is the PC of the faulted instruction, which is what
    114 		 * we expect to be in f_pc.
    115 		 *
    116 		 * XXX - This is a hack; it assumes we at least know the
    117 		 * sizes of all instructions we run across.
    118 		 * XXX TODO: This may not be true, so we might want to save
    119 		 * the PC in order to restore it later.
    120 		 */
    121 #if 0
    122 		insn.is_nextpc = frame->f_pc;
    123 #endif
    124 		insn.is_pc = frame->f_fmt4.f_fslw;
    125 		frame->f_pc = insn.is_pc;
    126 	}
    127 
    128 	if (ufetch_short((void *)(insn.is_pc), &sval)) {
    129 		DPRINTF(("%s: fault reading opcode\n", __func__));
    130 		fpe_abort(frame, ksi, SIGSEGV, SEGV_ACCERR);
    131 	}
    132 
    133 	if ((sval & 0xf000) != 0xf000) {
    134 		DPRINTF(("%s: not coproc. insn.: opcode=0x%x\n",
    135 		    __func__, sval));
    136 		fpe_abort(frame, ksi, SIGILL, ILL_ILLOPC);
    137 	}
    138 
    139 	if ((sval & 0x0E00) != 0x0200) {
    140 		DPRINTF(("%s: bad coproc. id: opcode=0x%x\n", __func__, sval));
    141 		fpe_abort(frame, ksi, SIGILL, ILL_ILLOPC);
    142 	}
    143 
    144 	insn.is_opcode = sval;
    145 	optype = (sval & 0x01C0);
    146 
    147 	if (ufetch_short((void *)(insn.is_pc + 2), &sval)) {
    148 		DPRINTF(("%s: fault reading word1\n", __func__));
    149 		fpe_abort(frame, ksi, SIGSEGV, SEGV_ACCERR);
    150 	}
    151 	insn.is_word1 = sval;
    152 	/* all FPU instructions are at least 4-byte long */
    153 	insn.is_advance = 4;
    154 
    155 	DUMP_INSN(&insn);
    156 
    157 	/*
    158 	 * Which family (or type) of opcode is it?
    159 	 * Tests ordered by likelihood (hopefully).
    160 	 * Certainly, type 0 is the most common.
    161 	 */
    162 	if (optype == 0x0000) {
    163 		/* type=0: generic */
    164 		if ((sval & 0x8000)) {
    165 			if ((sval & 0x4000)) {
    166 				DPRINTF(("%s: fmovm FPr\n", __func__));
    167 				sig = fpu_emul_fmovm(&fe, &insn);
    168 			} else {
    169 				DPRINTF(("%s: fmovm FPcr\n", __func__));
    170 				sig = fpu_emul_fmovmcr(&fe, &insn);
    171 			}
    172 		} else {
    173 			if ((sval & 0xe000) == 0x6000) {
    174 				/* fstore = fmove FPn,mem */
    175 				DPRINTF(("%s: fmove to mem\n", __func__));
    176 				sig = fpu_emul_fstore(&fe, &insn);
    177 			} else if ((sval & 0xfc00) == 0x5c00) {
    178 				/* fmovecr */
    179 				DPRINTF(("%s: fmovecr\n", __func__));
    180 				sig = fpu_emul_fmovecr(&fe, &insn);
    181 			} else if ((sval & 0xa07f) == 0x26) {
    182 				/* fscale */
    183 				DPRINTF(("%s: fscale\n", __func__));
    184 				sig = fpu_emul_fscale(&fe, &insn);
    185 			} else {
    186 				DPRINTF(("%s: other type0\n", __func__));
    187 				/* all other type0 insns are arithmetic */
    188 				sig = fpu_emul_arith(&fe, &insn);
    189 			}
    190 			if (sig == 0) {
    191 				DPRINTF(("%s: type 0 returned 0\n", __func__));
    192 				sig = fpu_upd_excp(&fe);
    193 			}
    194 		}
    195 	} else if (optype == 0x0080 || optype == 0x00C0) {
    196 		/* type=2 or 3: fbcc, short or long disp. */
    197 		DPRINTF(("%s: fbcc %s\n", __func__,
    198 		    (optype & 0x40) ? "long" : "short"));
    199 		sig = fpu_emul_brcc(&fe, &insn);
    200 	} else if (optype == 0x0040) {
    201 		/* type=1: fdbcc, fscc, ftrapcc */
    202 		DPRINTF(("%s: type1\n", __func__));
    203 		sig = fpu_emul_type1(&fe, &insn);
    204 	} else {
    205 		/* type=4: fsave    (privileged) */
    206 		/* type=5: frestore (privileged) */
    207 		/* type=6: reserved */
    208 		/* type=7: reserved */
    209 		DPRINTF(("%s: bad opcode type: opcode=0x%x\n", __func__,
    210 		    insn.is_opcode));
    211 		sig = SIGILL;
    212 	}
    213 
    214 	DUMP_INSN(&insn);
    215 
    216 	/*
    217 	 * XXX it is not clear to me, if we should progress the PC always,
    218 	 * for SIGFPE || 0, or only for 0; however, without SIGFPE, we
    219 	 * don't pass the signalling regression  tests.	-is
    220 	 */
    221 	if ((sig == 0) || (sig == SIGFPE))
    222 		frame->f_pc += insn.is_advance;
    223 #if defined(DDB) && defined(DEBUG_FPE)
    224 	else {
    225 		printf("%s: sig=%d, opcode=%x, word1=%x\n", __func__,
    226 		    sig, insn.is_opcode, insn.is_word1);
    227 		kdb_trap(-1, (db_regs_t *)&frame);
    228 	}
    229 #endif
    230 #if 0 /* XXX something is wrong */
    231 	if (frame->f_format == 4) {
    232 		/* XXX Restore PC -- 68{EC,LC}040 only */
    233 		if (insn.is_nextpc)
    234 			frame->f_pc = insn.is_nextpc;
    235 	}
    236 #endif
    237 
    238 	DPRINTF(("%s: EXITING: w/FPSR=%08x, FPCR=%08x\n", __func__,
    239 	    fe.fe_fpsr, fe.fe_fpcr));
    240 
    241 	if (sig)
    242 		fpe_abort(frame, ksi, sig, 0);
    243 	return sig;
    244 }
    245 
    246 /* update accrued exception bits and see if there's an FP exception */
    247 int
    248 fpu_upd_excp(struct fpemu *fe)
    249 {
    250 	uint32_t fpsr;
    251 	uint32_t fpcr;
    252 
    253 	fpsr = fe->fe_fpsr;
    254 	fpcr = fe->fe_fpcr;
    255 	/*
    256 	 * update fpsr accrued exception bits; each insn doesn't have to
    257 	 * update this
    258 	 */
    259 	if (fpsr & (FPSR_BSUN | FPSR_SNAN | FPSR_OPERR)) {
    260 		fpsr |= FPSR_AIOP;
    261 	}
    262 	if (fpsr & FPSR_OVFL) {
    263 		fpsr |= FPSR_AOVFL;
    264 	}
    265 	if ((fpsr & FPSR_UNFL) && (fpsr & FPSR_INEX2)) {
    266 		fpsr |= FPSR_AUNFL;
    267 	}
    268 	if (fpsr & FPSR_DZ) {
    269 		fpsr |= FPSR_ADZ;
    270 	}
    271 	if (fpsr & (FPSR_INEX1 | FPSR_INEX2 | FPSR_OVFL)) {
    272 		fpsr |= FPSR_AINEX;
    273 	}
    274 
    275 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
    276 
    277 	return (fpsr & fpcr & FPSR_EXCP) ? SIGFPE : 0;
    278 }
    279 
    280 /* update fpsr according to fp (= result of an fp op) */
    281 uint32_t
    282 fpu_upd_fpsr(struct fpemu *fe, struct fpn *fp)
    283 {
    284 	uint32_t fpsr;
    285 
    286 	DPRINTF(("%s: previous fpsr=%08x\n", __func__, fe->fe_fpsr));
    287 	/* clear all condition code */
    288 	fpsr = fe->fe_fpsr & ~FPSR_CCB;
    289 
    290 	DPRINTF(("%s: result is a ", __func__));
    291 	if (fp->fp_sign) {
    292 		DPRINTF(("negative "));
    293 		fpsr |= FPSR_NEG;
    294 	} else {
    295 		DPRINTF(("positive "));
    296 	}
    297 
    298 	switch (fp->fp_class) {
    299 	case FPC_SNAN:
    300 		DPRINTF(("signaling NAN\n"));
    301 		fpsr |= (FPSR_NAN | FPSR_SNAN);
    302 		break;
    303 	case FPC_QNAN:
    304 		DPRINTF(("quiet NAN\n"));
    305 		fpsr |= FPSR_NAN;
    306 		break;
    307 	case FPC_ZERO:
    308 		DPRINTF(("Zero\n"));
    309 		fpsr |= FPSR_ZERO;
    310 		break;
    311 	case FPC_INF:
    312 		DPRINTF(("Inf\n"));
    313 		fpsr |= FPSR_INF;
    314 		break;
    315 	default:
    316 		DPRINTF(("Number\n"));
    317 		/* anything else is treated as if it is a number */
    318 		break;
    319 	}
    320 
    321 	fe->fe_fpsr = fe->fe_fpframe->fpf_fpsr = fpsr;
    322 
    323 	DPRINTF(("%s: new fpsr=%08x\n", __func__, fe->fe_fpframe->fpf_fpsr));
    324 
    325 	return fpsr;
    326 }
    327 
    328 static int
    329 fpu_emul_fmovmcr(struct fpemu *fe, struct instruction *insn)
    330 {
    331 	struct frame *frame = fe->fe_frame;
    332 	struct fpframe *fpf = fe->fe_fpframe;
    333 	int sig;
    334 	int reglist;
    335 	int fpu_to_mem;
    336 
    337 	/* move to/from control registers */
    338 	reglist = (insn->is_word1 & 0x1c00) >> 10;
    339 	/* Bit 13 selects direction (FPU to/from Mem) */
    340 	fpu_to_mem = insn->is_word1 & 0x2000;
    341 
    342 	insn->is_datasize = 4;
    343 	insn->is_advance = 4;
    344 	sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
    345 	if (sig)
    346 		return sig;
    347 
    348 	if (reglist != 1 && reglist != 2 && reglist != 4 &&
    349 	    (insn->is_ea.ea_flags & EA_DIRECT)) {
    350 		/* attempted to copy more than one FPcr to CPU regs */
    351 		DPRINTF(("%s: tried to copy too many FPcr\n", __func__));
    352 		return SIGILL;
    353 	}
    354 
    355 	if (reglist & 4) {
    356 		/* fpcr */
    357 		if ((insn->is_ea.ea_flags & EA_DIRECT) &&
    358 		    insn->is_ea.ea_regnum >= 8 /* address reg */) {
    359 			/* attempted to copy FPCR to An */
    360 			DPRINTF(("%s: tried to copy FPCR from/to A%d\n",
    361 			    __func__, insn->is_ea.ea_regnum & 7));
    362 			return SIGILL;
    363 		}
    364 		if (fpu_to_mem) {
    365 			sig = fpu_store_ea(frame, insn, &insn->is_ea,
    366 			    (char *)&fpf->fpf_fpcr);
    367 		} else {
    368 			sig = fpu_load_ea(frame, insn, &insn->is_ea,
    369 			    (char *)&fpf->fpf_fpcr);
    370 		}
    371 	}
    372 	if (sig)
    373 		return sig;
    374 
    375 	if (reglist & 2) {
    376 		/* fpsr */
    377 		if ((insn->is_ea.ea_flags & EA_DIRECT) &&
    378 		    insn->is_ea.ea_regnum >= 8 /* address reg */) {
    379 			/* attempted to copy FPSR to An */
    380 			DPRINTF(("%s: tried to copy FPSR from/to A%d\n",
    381 			    __func__, insn->is_ea.ea_regnum & 7));
    382 			return SIGILL;
    383 		}
    384 		if (fpu_to_mem) {
    385 			sig = fpu_store_ea(frame, insn, &insn->is_ea,
    386 			    (char *)&fpf->fpf_fpsr);
    387 		} else {
    388 			sig = fpu_load_ea(frame, insn, &insn->is_ea,
    389 			    (char *)&fpf->fpf_fpsr);
    390 		}
    391 	}
    392 	if (sig)
    393 		return sig;
    394 
    395 	if (reglist & 1) {
    396 		/* fpiar - can be moved to/from An */
    397 		if (fpu_to_mem) {
    398 			sig = fpu_store_ea(frame, insn, &insn->is_ea,
    399 			    (char *)&fpf->fpf_fpiar);
    400 		} else {
    401 			sig = fpu_load_ea(frame, insn, &insn->is_ea,
    402 			    (char *)&fpf->fpf_fpiar);
    403 		}
    404 	}
    405 	return sig;
    406 }
    407 
    408 /*
    409  * type 0: fmovem
    410  * Separated out of fpu_emul_type0 for efficiency.
    411  * In this function, we know:
    412  *   (opcode & 0x01C0) == 0
    413  *   (word1 & 0x8000) == 0x8000
    414  *
    415  * No conversion or rounding is done by this instruction,
    416  * and the FPSR is not affected.
    417  */
    418 static int
    419 fpu_emul_fmovm(struct fpemu *fe, struct instruction *insn)
    420 {
    421 	struct frame *frame = fe->fe_frame;
    422 	struct fpframe *fpf = fe->fe_fpframe;
    423 	int word1, sig;
    424 	int reglist, regmask, regnum;
    425 	int fpu_to_mem, order;
    426 	/* int w1_post_incr; */
    427 	int *fpregs;
    428 
    429 	insn->is_advance = 4;
    430 	insn->is_datasize = 12;
    431 	word1 = insn->is_word1;
    432 
    433 	/* Bit 13 selects direction (FPU to/from Mem) */
    434 	fpu_to_mem = word1 & 0x2000;
    435 
    436 	/*
    437 	 * Bits 12,11 select register list mode:
    438 	 * 0,0: Static  reg list, pre-decr.
    439 	 * 0,1: Dynamic reg list, pre-decr.
    440 	 * 1,0: Static  reg list, post-incr.
    441 	 * 1,1: Dynamic reg list, post-incr
    442 	 */
    443 	/* w1_post_incr = word1 & 0x1000; */
    444 	if (word1 & 0x0800) {
    445 		/* dynamic reg list */
    446 		reglist = frame->f_regs[(word1 & 0x70) >> 4];
    447 	} else {
    448 		reglist = word1;
    449 	}
    450 	reglist &= 0xFF;
    451 
    452 	/* Get effective address. (modreg=opcode&077) */
    453 	sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
    454 	if (sig)
    455 		return sig;
    456 
    457 	/* Get address of soft coprocessor regs. */
    458 	fpregs = &fpf->fpf_regs[0];
    459 
    460 	if (insn->is_ea.ea_flags & EA_PREDECR) {
    461 		regnum = 7;
    462 		order = -1;
    463 	} else {
    464 		regnum = 0;
    465 		order = 1;
    466 	}
    467 
    468 	regmask = 0x80;
    469 	while ((0 <= regnum) && (regnum < 8)) {
    470 		if (regmask & reglist) {
    471 			if (fpu_to_mem) {
    472 				sig = fpu_store_ea(frame, insn, &insn->is_ea,
    473 				    (char *)&fpregs[regnum * 3]);
    474 				DPRINTF(("%s: FP%d (%08x,%08x,%08x) saved\n",
    475 				    __func__, regnum,
    476 				    fpregs[regnum * 3],
    477 				    fpregs[regnum * 3 + 1],
    478 				    fpregs[regnum * 3 + 2]));
    479 			} else {		/* mem to fpu */
    480 				sig = fpu_load_ea(frame, insn, &insn->is_ea,
    481 				    (char *)&fpregs[regnum * 3]);
    482 				DPRINTF(("%s: FP%d (%08x,%08x,%08x) loaded\n",
    483 				    __func__, regnum,
    484 				    fpregs[regnum * 3],
    485 				    fpregs[regnum * 3 + 1],
    486 				    fpregs[regnum * 3 + 2]));
    487 			}
    488 			if (sig)
    489 				break;
    490 		}
    491 		regnum += order;
    492 		regmask >>= 1;
    493 	}
    494 
    495 	return sig;
    496 }
    497 
    498 struct fpn *
    499 fpu_cmp(struct fpemu *fe)
    500 {
    501 	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
    502 
    503 	/* take care of special cases */
    504 	if (x->fp_class < 0 || y->fp_class < 0) {
    505 		/* if either of two is a SNAN, result is SNAN */
    506 		x->fp_class =
    507 		    (y->fp_class < x->fp_class) ? y->fp_class : x->fp_class;
    508 	} else if (x->fp_class == FPC_INF) {
    509 		if (y->fp_class == FPC_INF) {
    510 			/* both infinities */
    511 			if (x->fp_sign == y->fp_sign) {
    512 				/* return a signed zero */
    513 				x->fp_class = FPC_ZERO;
    514 			} else {
    515 				/* return a faked number w/x's sign */
    516 				x->fp_class = FPC_NUM;
    517 				x->fp_exp = 16383;
    518 				x->fp_mant[0] = FP_1;
    519 			}
    520 		} else {
    521 			/* y is a number */
    522 			/* return a forged number w/x's sign */
    523 			x->fp_class = FPC_NUM;
    524 			x->fp_exp = 16383;
    525 			x->fp_mant[0] = FP_1;
    526 		}
    527 	} else if (y->fp_class == FPC_INF) {
    528 		/* x is a Num but y is an Inf */
    529 		/* return a forged number w/y's sign inverted */
    530 		x->fp_class = FPC_NUM;
    531 		x->fp_sign = !y->fp_sign;
    532 		x->fp_exp = 16383;
    533 		x->fp_mant[0] = FP_1;
    534 	} else {
    535 		/*
    536 		 * x and y are both numbers or zeros,
    537 		 * or pair of a number and a zero
    538 		 */
    539 		y->fp_sign = !y->fp_sign;
    540 		x = fpu_add(fe);	/* (x - y) */
    541 		/*
    542 		 * FCMP does not set Inf bit in CC, so return a forged number
    543 		 * (value doesn't matter) if Inf is the result of fsub.
    544 		 */
    545 		if (x->fp_class == FPC_INF) {
    546 			x->fp_class = FPC_NUM;
    547 			x->fp_exp = 16383;
    548 			x->fp_mant[0] = FP_1;
    549 		}
    550 	}
    551 	return x;
    552 }
    553 
    554 /*
    555  * arithmetic operations
    556  */
    557 static int
    558 fpu_emul_arith(struct fpemu *fe, struct instruction *insn)
    559 {
    560 	struct frame *frame = fe->fe_frame;
    561 	uint32_t *fpregs = &(fe->fe_fpframe->fpf_regs[0]);
    562 	struct fpn *res;
    563 	int word1, sig = 0;
    564 	int regnum, format;
    565 	int discard_result = 0;
    566 	uint32_t buf[3];
    567 #ifdef DEBUG_FPE
    568 	int flags;
    569 	char regname;
    570 #endif
    571 
    572 	fe->fe_fpsr &= ~FPSR_EXCP;
    573 
    574 	DUMP_INSN(insn);
    575 
    576 	DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
    577 	    fe->fe_fpsr, fe->fe_fpcr));
    578 
    579 	word1 = insn->is_word1;
    580 	format = (word1 >> 10) & 7;
    581 	regnum = (word1 >> 7) & 7;
    582 
    583 	/* fetch a source operand : may not be used */
    584 	DPRINTF(("%s: dst/src FP%d=%08x,%08x,%08x\n", __func__,
    585 	    regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
    586 	    fpregs[regnum * 3 + 2]));
    587 
    588 	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
    589 
    590 	DUMP_INSN(insn);
    591 
    592 	/* get the other operand which is always the source */
    593 	if ((word1 & 0x4000) == 0) {
    594 		DPRINTF(("%s: FP%d op FP%d => FP%d\n", __func__,
    595 		    format, regnum, regnum));
    596 		DPRINTF(("%s: src opr FP%d=%08x,%08x,%08x\n", __func__,
    597 		    format, fpregs[format * 3], fpregs[format * 3 + 1],
    598 		    fpregs[format * 3 + 2]));
    599 		fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
    600 	} else {
    601 		/* the operand is in memory */
    602 		if (format == FTYPE_DBL) {
    603 			insn->is_datasize = 8;
    604 		} else if (format == FTYPE_SNG || format == FTYPE_LNG) {
    605 			insn->is_datasize = 4;
    606 		} else if (format == FTYPE_WRD) {
    607 			insn->is_datasize = 2;
    608 		} else if (format == FTYPE_BYT) {
    609 			insn->is_datasize = 1;
    610 		} else if (format == FTYPE_EXT) {
    611 			insn->is_datasize = 12;
    612 		} else {
    613 			/* invalid or unsupported operand format */
    614 			sig = SIGFPE;
    615 			return sig;
    616 		}
    617 
    618 		/* Get effective address. (modreg=opcode&077) */
    619 		sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
    620 		if (sig) {
    621 			DPRINTF(("%s: error in fpu_decode_ea\n", __func__));
    622 			return sig;
    623 		}
    624 
    625 		DUMP_INSN(insn);
    626 
    627 #ifdef DEBUG_FPE
    628 		printf("%s: addr mode = ", __func__);
    629 		flags = insn->is_ea.ea_flags;
    630 		regname = (insn->is_ea.ea_regnum & 8) ? 'a' : 'd';
    631 
    632 		if (flags & EA_DIRECT) {
    633 			printf("%c%d\n", regname, insn->is_ea.ea_regnum & 7);
    634 		} else if (flags & EA_PC_REL) {
    635 			if (flags & EA_OFFSET) {
    636 				printf("pc@(%d)\n", insn->is_ea.ea_offset);
    637 			} else if (flags & EA_INDEXED) {
    638 				printf("pc@(...)\n");
    639 			}
    640 		} else if (flags & EA_PREDECR) {
    641 			printf("%c%d@-\n", regname, insn->is_ea.ea_regnum & 7);
    642 		} else if (flags & EA_POSTINCR) {
    643 			printf("%c%d@+\n", regname, insn->is_ea.ea_regnum & 7);
    644 		} else if (flags & EA_OFFSET) {
    645 			printf("%c%d@(%d)\n", regname,
    646 			    insn->is_ea.ea_regnum & 7,
    647 			    insn->is_ea.ea_offset);
    648 		} else if (flags & EA_INDEXED) {
    649 			printf("%c%d@(...)\n", regname,
    650 			    insn->is_ea.ea_regnum & 7);
    651 		} else if (flags & EA_ABS) {
    652 			printf("0x%08x\n", insn->is_ea.ea_absaddr);
    653 		} else if (flags & EA_IMMED) {
    654 			printf("#0x%08x,%08x,%08x\n",
    655 			    insn->is_ea.ea_immed[0],
    656 			    insn->is_ea.ea_immed[1],
    657 			    insn->is_ea.ea_immed[2]);
    658 		} else {
    659 			printf("%c%d@\n", regname, insn->is_ea.ea_regnum & 7);
    660 		}
    661 #endif /* DEBUG_FPE */
    662 
    663 		fpu_load_ea(frame, insn, &insn->is_ea, (char*)buf);
    664 		if (format == FTYPE_WRD) {
    665 			/* sign-extend */
    666 			buf[0] &= 0xffff;
    667 			if (buf[0] & 0x8000)
    668 				buf[0] |= 0xffff0000;
    669 			format = FTYPE_LNG;
    670 		} else if (format == FTYPE_BYT) {
    671 			/* sign-extend */
    672 			buf[0] &= 0xff;
    673 			if (buf[0] & 0x80)
    674 				buf[0] |= 0xffffff00;
    675 			format = FTYPE_LNG;
    676 		}
    677 		DPRINTF(("%s: src = %08x %08x %08x, siz = %d\n", __func__,
    678 		    buf[0], buf[1], buf[2], insn->is_datasize));
    679 		fpu_explode(fe, &fe->fe_f2, format, buf);
    680 	}
    681 
    682 	DUMP_INSN(insn);
    683 
    684 	/*
    685 	 * An arithmetic instruction emulate function has a prototype of
    686 	 * struct fpn *fpu_op(struct fpemu *);
    687 	 *
    688 	 * 1) If the instruction is monadic, then fpu_op() must use
    689 	 *    fe->fe_f2 as its operand, and return a pointer to the
    690 	 *    result.
    691 	 *
    692 	 * 2) If the instruction is diadic, then fpu_op() must use
    693 	 *    fe->fe_f1 and fe->fe_f2 as its two operands, and return a
    694 	 *    pointer to the result.
    695 	 *
    696 	 */
    697 	res = NULL;
    698 	switch (word1 & 0x7f) {
    699 	case 0x00:		/* fmove */
    700 		res = &fe->fe_f2;
    701 		break;
    702 
    703 	case 0x01:		/* fint */
    704 		res = fpu_int(fe);
    705 		break;
    706 
    707 	case 0x02:		/* fsinh */
    708 		res = fpu_sinh(fe);
    709 		break;
    710 
    711 	case 0x03:		/* fintrz */
    712 		res = fpu_intrz(fe);
    713 		break;
    714 
    715 	case 0x04:		/* fsqrt */
    716 		res = fpu_sqrt(fe);
    717 		break;
    718 
    719 	case 0x06:		/* flognp1 */
    720 		res = fpu_lognp1(fe);
    721 		break;
    722 
    723 	case 0x08:		/* fetoxm1 */
    724 		res = fpu_etoxm1(fe);
    725 		break;
    726 
    727 	case 0x09:		/* ftanh */
    728 		res = fpu_tanh(fe);
    729 		break;
    730 
    731 	case 0x0A:		/* fatan */
    732 		res = fpu_atan(fe);
    733 		break;
    734 
    735 	case 0x0C:		/* fasin */
    736 		res = fpu_asin(fe);
    737 		break;
    738 
    739 	case 0x0D:		/* fatanh */
    740 		res = fpu_atanh(fe);
    741 		break;
    742 
    743 	case 0x0E:		/* fsin */
    744 		res = fpu_sin(fe);
    745 		break;
    746 
    747 	case 0x0F:		/* ftan */
    748 		res = fpu_tan(fe);
    749 		break;
    750 
    751 	case 0x10:		/* fetox */
    752 		res = fpu_etox(fe);
    753 		break;
    754 
    755 	case 0x11:		/* ftwotox */
    756 		res = fpu_twotox(fe);
    757 		break;
    758 
    759 	case 0x12:		/* ftentox */
    760 		res = fpu_tentox(fe);
    761 		break;
    762 
    763 	case 0x14:		/* flogn */
    764 		res = fpu_logn(fe);
    765 		break;
    766 
    767 	case 0x15:		/* flog10 */
    768 		res = fpu_log10(fe);
    769 		break;
    770 
    771 	case 0x16:		/* flog2 */
    772 		res = fpu_log2(fe);
    773 		break;
    774 
    775 	case 0x18:		/* fabs */
    776 		fe->fe_f2.fp_sign = 0;
    777 		res = &fe->fe_f2;
    778 		break;
    779 
    780 	case 0x19:		/* fcosh */
    781 		res = fpu_cosh(fe);
    782 		break;
    783 
    784 	case 0x1A:		/* fneg */
    785 		fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
    786 		res = &fe->fe_f2;
    787 		break;
    788 
    789 	case 0x1C:		/* facos */
    790 		res = fpu_acos(fe);
    791 		break;
    792 
    793 	case 0x1D:		/* fcos */
    794 		res = fpu_cos(fe);
    795 		break;
    796 
    797 	case 0x1E:		/* fgetexp */
    798 		res = fpu_getexp(fe);
    799 		break;
    800 
    801 	case 0x1F:		/* fgetman */
    802 		res = fpu_getman(fe);
    803 		break;
    804 
    805 	case 0x20:		/* fdiv */
    806 	case 0x24:		/* fsgldiv: cheating - better than nothing */
    807 		res = fpu_div(fe);
    808 		break;
    809 
    810 	case 0x21:		/* fmod */
    811 		res = fpu_mod(fe);
    812 		break;
    813 
    814 	case 0x28:		/* fsub */
    815 		fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
    816 		/* FALLTHROUGH */
    817 	case 0x22:		/* fadd */
    818 		res = fpu_add(fe);
    819 		break;
    820 
    821 	case 0x23:		/* fmul */
    822 	case 0x27:		/* fsglmul: cheating - better than nothing */
    823 		res = fpu_mul(fe);
    824 		break;
    825 
    826 	case 0x25:		/* frem */
    827 		res = fpu_rem(fe);
    828 		break;
    829 
    830 	case 0x26:
    831 		/* fscale is handled by a separate function */
    832 		break;
    833 
    834 	case 0x30:
    835 	case 0x31:
    836 	case 0x32:
    837 	case 0x33:
    838 	case 0x34:
    839 	case 0x35:
    840 	case 0x36:
    841 	case 0x37:		/* fsincos */
    842 		res = fpu_sincos(fe, word1 & 7);
    843 		break;
    844 
    845 	case 0x38:		/* fcmp */
    846 		res = fpu_cmp(fe);
    847 		discard_result = 1;
    848 		break;
    849 
    850 	case 0x3A:		/* ftst */
    851 		res = &fe->fe_f2;
    852 		discard_result = 1;
    853 		break;
    854 
    855 	default:		/* possibly 040/060 instructions */
    856 		DPRINTF(("%s: bad opcode=0x%x, word1=0x%x\n", __func__,
    857 		    insn->is_opcode, insn->is_word1));
    858 		sig = SIGILL;
    859 	}
    860 
    861 	/* for sanity */
    862 	if (res == NULL)
    863 		sig = SIGILL;
    864 
    865 	if (sig == 0) {
    866 		if (!discard_result)
    867 			fpu_implode(fe, res, FTYPE_EXT, &fpregs[regnum * 3]);
    868 
    869 		/* update fpsr according to the result of operation */
    870 		fpu_upd_fpsr(fe, res);
    871 #ifdef DEBUG_FPE
    872 		if (!discard_result) {
    873 			printf("%s: %08x,%08x,%08x stored in FP%d\n", __func__,
    874 			    fpregs[regnum * 3],
    875 			    fpregs[regnum * 3 + 1],
    876 			    fpregs[regnum * 3 + 2],
    877 			    regnum);
    878 		} else {
    879 			static const char *class_name[] =
    880 			    { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
    881 			printf("%s: result(%s,%c,%d,%08x,%08x,%08x) "
    882 			    "discarded\n", __func__,
    883 			    class_name[res->fp_class + 2],
    884 			    res->fp_sign ? '-' : '+', res->fp_exp,
    885 			    res->fp_mant[0], res->fp_mant[1],
    886 			    res->fp_mant[2]);
    887 		}
    888 #endif
    889 	} else {
    890 		DPRINTF(("%s: received signal %d\n", __func__, sig));
    891 	}
    892 
    893 	DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
    894 	    fe->fe_fpsr, fe->fe_fpcr));
    895 
    896 	DUMP_INSN(insn);
    897 
    898 	return sig;
    899 }
    900 
    901 /*
    902  * test condition code according to the predicate in the opcode.
    903  * returns -1 when the predicate evaluates to true, 0 when false.
    904  * signal numbers are returned when an error is detected.
    905  */
    906 static int
    907 test_cc(struct fpemu *fe, int pred)
    908 {
    909 	int result, sig_bsun, invert;
    910 	int fpsr;
    911 
    912 	fpsr = fe->fe_fpsr;
    913 	invert = 0;
    914 	fpsr &= ~FPSR_EXCP;		/* clear all exceptions */
    915 	DPRINTF(("%s: fpsr=0x%08x\n", __func__, fpsr));
    916 	pred &= 0x3f;		/* lowest 6 bits */
    917 
    918 	DPRINTF(("%s: ", __func__));
    919 
    920 	if (pred >= 0x20) {
    921 		DPRINTF(("Illegal condition code\n"));
    922 		return SIGILL;
    923 	} else if (pred & 0x10) {
    924 		/* IEEE nonaware tests */
    925 		sig_bsun = 1;
    926 		pred &= 0x0f;		/* lower 4 bits */
    927 	} else {
    928 		/* IEEE aware tests */
    929 		DPRINTF(("IEEE "));
    930 		sig_bsun = 0;
    931 	}
    932 
    933 	if (pred & 0x08) {
    934 		DPRINTF(("Not "));
    935 		/* predicate is "NOT ..." */
    936 		pred ^= 0xf;		/* invert */
    937 		invert = -1;
    938 	}
    939 	switch (pred) {
    940 	case 0:			/* (Signaling) False */
    941 		DPRINTF(("False"));
    942 		result = 0;
    943 		break;
    944 	case 1:			/* (Signaling) Equal */
    945 		DPRINTF(("Equal"));
    946 		result = -((fpsr & FPSR_ZERO) == FPSR_ZERO);
    947 		break;
    948 	case 2:			/* Greater Than */
    949 		DPRINTF(("GT"));
    950 		result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == 0);
    951 		break;
    952 	case 3:			/* Greater or Equal */
    953 		DPRINTF(("GE"));
    954 		result = -((fpsr & FPSR_ZERO) ||
    955 		    (fpsr & (FPSR_NAN|FPSR_NEG)) == 0);
    956 		break;
    957 	case 4:			/* Less Than */
    958 		DPRINTF(("LT"));
    959 		result = -((fpsr & (FPSR_NAN|FPSR_ZERO|FPSR_NEG)) == FPSR_NEG);
    960 		break;
    961 	case 5:			/* Less or Equal */
    962 		DPRINTF(("LE"));
    963 		result = -((fpsr & FPSR_ZERO) ||
    964 		    ((fpsr & (FPSR_NAN|FPSR_NEG)) == FPSR_NEG));
    965 		break;
    966 	case 6:			/* Greater or Less than */
    967 		DPRINTF(("GLT"));
    968 		result = -((fpsr & (FPSR_NAN|FPSR_ZERO)) == 0);
    969 		break;
    970 	case 7:			/* Greater, Less or Equal */
    971 		DPRINTF(("GLE"));
    972 		result = -((fpsr & FPSR_NAN) == 0);
    973 		break;
    974 	default:
    975 		/* invalid predicate */
    976 		DPRINTF(("Invalid predicate\n"));
    977 		return SIGILL;
    978 	}
    979 	/* if the predicate is "NOT ...", then invert the result */
    980 	result ^= invert;
    981 	DPRINTF(("=> %s (%d)\n", result ? "true" : "false", result));
    982 	/* if it's an IEEE unaware test and NAN is set, BSUN is set */
    983 	if (sig_bsun && (fpsr & FPSR_NAN)) {
    984 		fpsr |= FPSR_BSUN;
    985 	}
    986 
    987 	/* put fpsr back */
    988 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
    989 
    990 	return result;
    991 }
    992 
    993 /*
    994  * type 1: fdbcc, fscc, ftrapcc
    995  * In this function, we know:
    996  *   (opcode & 0x01C0) == 0x0040
    997  */
    998 static int
    999 fpu_emul_type1(struct fpemu *fe, struct instruction *insn)
   1000 {
   1001 	struct frame *frame = fe->fe_frame;
   1002 	int advance, sig, branch, displ;
   1003 	unsigned short sval;
   1004 
   1005 	branch = test_cc(fe, insn->is_word1);
   1006 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
   1007 
   1008 	insn->is_advance = 4;
   1009 	sig = 0;
   1010 
   1011 	switch (insn->is_opcode & 070) {
   1012 	case 010:			/* fdbcc */
   1013 		if (branch == -1) {
   1014 			/* advance */
   1015 			insn->is_advance = 6;
   1016 		} else if (!branch) {
   1017 			/* decrement Dn and if (Dn != -1) branch */
   1018 			uint16_t count = frame->f_regs[insn->is_opcode & 7];
   1019 
   1020 			if (count-- != 0) {
   1021 				if (ufetch_short((void *)(insn->is_pc +
   1022 							   insn->is_advance),
   1023 						  &sval)) {
   1024 					DPRINTF(("%s: fault reading "
   1025 					    "displacement\n", __func__));
   1026 					return SIGSEGV;
   1027 				}
   1028 				displ = sval;
   1029 				/* sign-extend the displacement */
   1030 				displ &= 0xffff;
   1031 				if (displ & 0x8000) {
   1032 					displ |= 0xffff0000;
   1033 				}
   1034 				insn->is_advance += displ;
   1035 #if 0				/* XXX */
   1036 				insn->is_nextpc = insn->is_pc +
   1037 				    insn->is_advance;
   1038 #endif
   1039 			} else {
   1040 				insn->is_advance = 6;
   1041 			}
   1042 			/* write it back */
   1043 			frame->f_regs[insn->is_opcode & 7] &= 0xffff0000;
   1044 			frame->f_regs[insn->is_opcode & 7] |= (uint32_t)count;
   1045 		} else {		/* got a signal */
   1046 			sig = SIGFPE;
   1047 		}
   1048 		break;
   1049 
   1050 	case 070:			/* ftrapcc or fscc */
   1051 		advance = 4;
   1052 		if ((insn->is_opcode & 07) >= 2) {
   1053 			switch (insn->is_opcode & 07) {
   1054 			case 3:		/* long opr */
   1055 				advance += 2;
   1056 			case 2:		/* word opr */
   1057 				advance += 2;
   1058 			case 4:		/* no opr */
   1059 				break;
   1060 			default:
   1061 				return SIGILL;
   1062 				break;
   1063 			}
   1064 
   1065 			if (branch == 0) {
   1066 				/* no trap */
   1067 				insn->is_advance = advance;
   1068 				sig = 0;
   1069 			} else {
   1070 				/* trap */
   1071 				sig = SIGFPE;
   1072 			}
   1073 			break;
   1074 		}
   1075 
   1076 		/* FALLTHROUGH */
   1077 	default:			/* fscc */
   1078 		insn->is_advance = 4;
   1079 		insn->is_datasize = 1;	/* always byte */
   1080 		sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
   1081 		if (sig) {
   1082 			break;
   1083 		}
   1084 		if (branch == -1 || branch == 0) {
   1085 			/* set result */
   1086 			sig = fpu_store_ea(frame, insn, &insn->is_ea,
   1087 			    (char *)&branch);
   1088 		} else {
   1089 			/* got an exception */
   1090 			sig = branch;
   1091 		}
   1092 		break;
   1093 	}
   1094 	return sig;
   1095 }
   1096 
   1097 /*
   1098  * Type 2 or 3: fbcc (also fnop)
   1099  * In this function, we know:
   1100  *   (opcode & 0x0180) == 0x0080
   1101  */
   1102 static int
   1103 fpu_emul_brcc(struct fpemu *fe, struct instruction *insn)
   1104 {
   1105 	int displ, word2;
   1106 	int sig;
   1107 	unsigned short sval;
   1108 
   1109 	/*
   1110 	 * Get branch displacement.
   1111 	 */
   1112 	insn->is_advance = 4;
   1113 	displ = insn->is_word1;
   1114 
   1115 	if (insn->is_opcode & 0x40) {
   1116 		if (ufetch_short((void *)(insn->is_pc + insn->is_advance),
   1117 				  &sval)) {
   1118 			DPRINTF(("%s: fault reading word2\n", __func__));
   1119 			return SIGSEGV;
   1120 		}
   1121 		word2 = sval;
   1122 		displ <<= 16;
   1123 		displ |= word2;
   1124 		insn->is_advance += 2;
   1125 	} else {
   1126 		/* displacement is word sized */
   1127 		if (displ & 0x8000)
   1128 			displ |= 0xFFFF0000;
   1129 	}
   1130 
   1131 	/* XXX: If CC, insn->is_pc += displ */
   1132 	sig = test_cc(fe, insn->is_opcode);
   1133 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
   1134 
   1135 	if (fe->fe_fpsr & fe->fe_fpcr & FPSR_EXCP) {
   1136 		return SIGFPE;		/* caught an exception */
   1137 	}
   1138 	if (sig == -1) {
   1139 		/*
   1140 		 * branch does take place; 2 is the offset to the 1st disp word
   1141 		 */
   1142 		insn->is_advance = displ + 2;
   1143 #if 0		/* XXX */
   1144 		insn->is_nextpc = insn->is_pc + insn->is_advance;
   1145 #endif
   1146 	} else if (sig)
   1147 		return SIGILL;		/* got a signal */
   1148 	DPRINTF(("%s: %s insn @ %x (%x+%x) (disp=%x)\n", __func__,
   1149 	    (sig == -1) ? "BRANCH to" : "NEXT",
   1150 	    insn->is_pc + insn->is_advance, insn->is_pc, insn->is_advance,
   1151 	    displ));
   1152 	return 0;
   1153 }
   1154