Home | History | Annotate | Line # | Download | only in fpe
fpu_emulate.c revision 1.48
      1 /*	$NetBSD: fpu_emulate.c,v 1.48 2024/12/28 12:23:51 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.48 2024/12/28 12:23:51 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 		/* real FTRAPcc raises T_TRAPVINST if the condition is met. */
    205 		if (sig == SIGFPE) {
    206 			ksi->ksi_trap = T_TRAPVINST;
    207 		}
    208 	} else {
    209 		/* type=4: fsave    (privileged) */
    210 		/* type=5: frestore (privileged) */
    211 		/* type=6: reserved */
    212 		/* type=7: reserved */
    213 		DPRINTF(("%s: bad opcode type: opcode=0x%x\n", __func__,
    214 		    insn.is_opcode));
    215 		sig = SIGILL;
    216 	}
    217 
    218 	DUMP_INSN(&insn);
    219 
    220 	/*
    221 	 * XXX it is not clear to me, if we should progress the PC always,
    222 	 * for SIGFPE || 0, or only for 0; however, without SIGFPE, we
    223 	 * don't pass the signalling regression  tests.	-is
    224 	 */
    225 	if ((sig == 0) || (sig == SIGFPE))
    226 		frame->f_pc += insn.is_advance;
    227 #if defined(DDB) && defined(DEBUG_FPE)
    228 	else {
    229 		printf("%s: sig=%d, opcode=%x, word1=%x\n", __func__,
    230 		    sig, insn.is_opcode, insn.is_word1);
    231 		kdb_trap(-1, (db_regs_t *)&frame);
    232 	}
    233 #endif
    234 #if 0 /* XXX something is wrong */
    235 	if (frame->f_format == 4) {
    236 		/* XXX Restore PC -- 68{EC,LC}040 only */
    237 		if (insn.is_nextpc)
    238 			frame->f_pc = insn.is_nextpc;
    239 	}
    240 #endif
    241 
    242 	DPRINTF(("%s: EXITING: w/FPSR=%08x, FPCR=%08x\n", __func__,
    243 	    fe.fe_fpsr, fe.fe_fpcr));
    244 
    245 	if (sig)
    246 		fpe_abort(frame, ksi, sig, 0);
    247 	return sig;
    248 }
    249 
    250 /* update accrued exception bits and see if there's an FP exception */
    251 int
    252 fpu_upd_excp(struct fpemu *fe)
    253 {
    254 	uint32_t fpsr;
    255 	uint32_t fpcr;
    256 
    257 	fpsr = fe->fe_fpsr;
    258 	fpcr = fe->fe_fpcr;
    259 	/*
    260 	 * update fpsr accrued exception bits; each insn doesn't have to
    261 	 * update this
    262 	 */
    263 	if (fpsr & (FPSR_BSUN | FPSR_SNAN | FPSR_OPERR)) {
    264 		fpsr |= FPSR_AIOP;
    265 	}
    266 	if (fpsr & FPSR_OVFL) {
    267 		fpsr |= FPSR_AOVFL;
    268 	}
    269 	if ((fpsr & FPSR_UNFL) && (fpsr & FPSR_INEX2)) {
    270 		fpsr |= FPSR_AUNFL;
    271 	}
    272 	if (fpsr & FPSR_DZ) {
    273 		fpsr |= FPSR_ADZ;
    274 	}
    275 	if (fpsr & (FPSR_INEX1 | FPSR_INEX2 | FPSR_OVFL)) {
    276 		fpsr |= FPSR_AINEX;
    277 	}
    278 
    279 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
    280 
    281 	return (fpsr & fpcr & FPSR_EXCP) ? SIGFPE : 0;
    282 }
    283 
    284 /* update fpsr according to fp (= result of an fp op) */
    285 uint32_t
    286 fpu_upd_fpsr(struct fpemu *fe, struct fpn *fp)
    287 {
    288 	uint32_t fpsr;
    289 
    290 	DPRINTF(("%s: previous fpsr=%08x\n", __func__, fe->fe_fpsr));
    291 	/* clear all condition code */
    292 	fpsr = fe->fe_fpsr & ~FPSR_CCB;
    293 
    294 	DPRINTF(("%s: result is a ", __func__));
    295 	if (fp->fp_sign) {
    296 		DPRINTF(("negative "));
    297 		fpsr |= FPSR_NEG;
    298 	} else {
    299 		DPRINTF(("positive "));
    300 	}
    301 
    302 	switch (fp->fp_class) {
    303 	case FPC_SNAN:
    304 		DPRINTF(("signaling NAN\n"));
    305 		fpsr |= (FPSR_NAN | FPSR_SNAN);
    306 		break;
    307 	case FPC_QNAN:
    308 		DPRINTF(("quiet NAN\n"));
    309 		fpsr |= FPSR_NAN;
    310 		break;
    311 	case FPC_ZERO:
    312 		DPRINTF(("Zero\n"));
    313 		fpsr |= FPSR_ZERO;
    314 		break;
    315 	case FPC_INF:
    316 		DPRINTF(("Inf\n"));
    317 		fpsr |= FPSR_INF;
    318 		break;
    319 	default:
    320 		DPRINTF(("Number\n"));
    321 		/* anything else is treated as if it is a number */
    322 		break;
    323 	}
    324 
    325 	fe->fe_fpsr = fe->fe_fpframe->fpf_fpsr = fpsr;
    326 
    327 	DPRINTF(("%s: new fpsr=%08x\n", __func__, fe->fe_fpframe->fpf_fpsr));
    328 
    329 	return fpsr;
    330 }
    331 
    332 static int
    333 fpu_emul_fmovmcr(struct fpemu *fe, struct instruction *insn)
    334 {
    335 	struct frame *frame = fe->fe_frame;
    336 	struct fpframe *fpf = fe->fe_fpframe;
    337 	int sig;
    338 	int reglist;
    339 	int regcount;
    340 	int fpu_to_mem;
    341 	uint32_t tmp[3];
    342 
    343 	/* move to/from control registers */
    344 	reglist = (insn->is_word1 & 0x1c00) >> 10;
    345 	/*
    346 	 * If reglist is 0b000, treat it as FPIAR.  This is not specification
    347 	 * but the behavior described in the 6888x user's manual.
    348 	 */
    349 	if (reglist == 0)
    350 		reglist = 1;
    351 
    352 	if (reglist == 7) {
    353 		regcount = 3;
    354 	} else if (reglist == 3 || reglist == 5 || reglist == 6) {
    355 		regcount = 2;
    356 	} else {
    357 		regcount = 1;
    358 	}
    359 	insn->is_datasize = regcount * 4;
    360 	sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
    361 	if (sig)
    362 		return sig;
    363 
    364 	/*
    365 	 * For data register, only single register can be transferred.
    366 	 * For addr register, only FPIAR can be transferred.
    367 	 */
    368 	if ((insn->is_ea.ea_flags & EA_DIRECT)) {
    369 		if (insn->is_ea.ea_regnum < 8) {
    370 			if (regcount != 1) {
    371 				return SIGILL;
    372 			}
    373 		} else {
    374 			if (reglist != 1) {
    375 				return SIGILL;
    376 			}
    377 		}
    378 	}
    379 
    380 	/* Bit 13 selects direction (FPU to/from Mem) */
    381 	fpu_to_mem = insn->is_word1 & 0x2000;
    382 	if (fpu_to_mem) {
    383 		uint32_t *s = &tmp[0];
    384 
    385 		if ((reglist & 4)) {
    386 			*s++ = fpf->fpf_fpcr;
    387 		}
    388 		if ((reglist & 2)) {
    389 			*s++ = fpf->fpf_fpsr;
    390 		}
    391 		if ((reglist & 1)) {
    392 			*s++ = fpf->fpf_fpiar;
    393 		}
    394 
    395 		sig = fpu_store_ea(frame, insn, &insn->is_ea, (char *)tmp);
    396 	} else {
    397 		const uint32_t *d = &tmp[0];
    398 
    399 		sig = fpu_load_ea(frame, insn, &insn->is_ea, (char *)tmp);
    400 		if (sig)
    401 			return sig;
    402 
    403 		if ((reglist & 4)) {
    404 			fpf->fpf_fpcr = *d++;
    405 			fpf->fpf_fpcr &= 0x0000fff0;
    406 		}
    407 		if ((reglist & 2)) {
    408 			fpf->fpf_fpsr = *d++;
    409 			fpf->fpf_fpsr &= 0x0ffffff8;
    410 		}
    411 		if ((reglist & 1)) {
    412 			fpf->fpf_fpiar = *d++;
    413 		}
    414 	}
    415 	return sig;
    416 }
    417 
    418 /*
    419  * type 0: fmovem
    420  * Separated out of fpu_emul_type0 for efficiency.
    421  * In this function, we know:
    422  *   (opcode & 0x01C0) == 0
    423  *   (word1 & 0x8000) == 0x8000
    424  *
    425  * No conversion or rounding is done by this instruction,
    426  * and the FPSR is not affected.
    427  */
    428 static int
    429 fpu_emul_fmovm(struct fpemu *fe, struct instruction *insn)
    430 {
    431 	struct frame *frame = fe->fe_frame;
    432 	struct fpframe *fpf = fe->fe_fpframe;
    433 	int word1, sig;
    434 	int reglist, regmask, regnum;
    435 	int fpu_to_mem, order;
    436 	/* int w1_post_incr; */
    437 	int *fpregs;
    438 
    439 	insn->is_datasize = 12;
    440 	word1 = insn->is_word1;
    441 
    442 	/* Bit 13 selects direction (FPU to/from Mem) */
    443 	fpu_to_mem = word1 & 0x2000;
    444 
    445 	/*
    446 	 * Bits 12,11 select register list mode:
    447 	 * 0,0: Static  reg list, pre-decr.
    448 	 * 0,1: Dynamic reg list, pre-decr.
    449 	 * 1,0: Static  reg list, post-incr.
    450 	 * 1,1: Dynamic reg list, post-incr
    451 	 */
    452 	/* w1_post_incr = word1 & 0x1000; */
    453 	if (word1 & 0x0800) {
    454 		/* dynamic reg list */
    455 		reglist = frame->f_regs[(word1 & 0x70) >> 4];
    456 	} else {
    457 		reglist = word1;
    458 	}
    459 	reglist &= 0xFF;
    460 
    461 	/* Get effective address. (modreg=opcode&077) */
    462 	sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
    463 	if (sig)
    464 		return sig;
    465 
    466 	/* Get address of soft coprocessor regs. */
    467 	fpregs = &fpf->fpf_regs[0];
    468 
    469 	if (insn->is_ea.ea_flags & EA_PREDECR) {
    470 		regnum = 7;
    471 		order = -1;
    472 	} else {
    473 		regnum = 0;
    474 		order = 1;
    475 	}
    476 
    477 	regmask = 0x80;
    478 	while ((0 <= regnum) && (regnum < 8)) {
    479 		if (regmask & reglist) {
    480 			if (fpu_to_mem) {
    481 				sig = fpu_store_ea(frame, insn, &insn->is_ea,
    482 				    (char *)&fpregs[regnum * 3]);
    483 				DPRINTF(("%s: FP%d (%08x,%08x,%08x) saved\n",
    484 				    __func__, regnum,
    485 				    fpregs[regnum * 3],
    486 				    fpregs[regnum * 3 + 1],
    487 				    fpregs[regnum * 3 + 2]));
    488 			} else {		/* mem to fpu */
    489 				sig = fpu_load_ea(frame, insn, &insn->is_ea,
    490 				    (char *)&fpregs[regnum * 3]);
    491 				DPRINTF(("%s: FP%d (%08x,%08x,%08x) loaded\n",
    492 				    __func__, regnum,
    493 				    fpregs[regnum * 3],
    494 				    fpregs[regnum * 3 + 1],
    495 				    fpregs[regnum * 3 + 2]));
    496 			}
    497 			if (sig)
    498 				break;
    499 		}
    500 		regnum += order;
    501 		regmask >>= 1;
    502 	}
    503 
    504 	return sig;
    505 }
    506 
    507 struct fpn *
    508 fpu_cmp(struct fpemu *fe)
    509 {
    510 	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
    511 
    512 	/* take care of special cases */
    513 	if (x->fp_class < 0 || y->fp_class < 0) {
    514 		/* if either of two is a SNAN, result is SNAN */
    515 		x->fp_class =
    516 		    (y->fp_class < x->fp_class) ? y->fp_class : x->fp_class;
    517 	} else if (x->fp_class == FPC_INF) {
    518 		if (y->fp_class == FPC_INF) {
    519 			/* both infinities */
    520 			if (x->fp_sign == y->fp_sign) {
    521 				/* return a signed zero */
    522 				x->fp_class = FPC_ZERO;
    523 			} else {
    524 				/* return a faked number w/x's sign */
    525 				x->fp_class = FPC_NUM;
    526 				x->fp_exp = 16383;
    527 				x->fp_mant[0] = FP_1;
    528 			}
    529 		} else {
    530 			/* y is a number */
    531 			/* return a forged number w/x's sign */
    532 			x->fp_class = FPC_NUM;
    533 			x->fp_exp = 16383;
    534 			x->fp_mant[0] = FP_1;
    535 		}
    536 	} else if (y->fp_class == FPC_INF) {
    537 		/* x is a Num but y is an Inf */
    538 		/* return a forged number w/y's sign inverted */
    539 		x->fp_class = FPC_NUM;
    540 		x->fp_sign = !y->fp_sign;
    541 		x->fp_exp = 16383;
    542 		x->fp_mant[0] = FP_1;
    543 	} else {
    544 		/*
    545 		 * x and y are both numbers or zeros,
    546 		 * or pair of a number and a zero
    547 		 */
    548 		y->fp_sign = !y->fp_sign;
    549 		x = fpu_add(fe);	/* (x - y) */
    550 		/*
    551 		 * FCMP does not set Inf bit in CC, so return a forged number
    552 		 * (value doesn't matter) if Inf is the result of fsub.
    553 		 */
    554 		if (x->fp_class == FPC_INF) {
    555 			x->fp_class = FPC_NUM;
    556 			x->fp_exp = 16383;
    557 			x->fp_mant[0] = FP_1;
    558 		}
    559 	}
    560 	return x;
    561 }
    562 
    563 /*
    564  * arithmetic operations
    565  */
    566 static int
    567 fpu_emul_arith(struct fpemu *fe, struct instruction *insn)
    568 {
    569 	struct frame *frame = fe->fe_frame;
    570 	uint32_t *fpregs = &(fe->fe_fpframe->fpf_regs[0]);
    571 	struct fpn *res;
    572 	int word1, sig = 0;
    573 	int regnum, format;
    574 	int discard_result = 0;
    575 	uint32_t buf[3];
    576 #ifdef DEBUG_FPE
    577 	int flags;
    578 	char regname;
    579 #endif
    580 
    581 	fe->fe_fpsr &= ~FPSR_EXCP;
    582 
    583 	DUMP_INSN(insn);
    584 
    585 	DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
    586 	    fe->fe_fpsr, fe->fe_fpcr));
    587 
    588 	word1 = insn->is_word1;
    589 	format = (word1 >> 10) & 7;
    590 	regnum = (word1 >> 7) & 7;
    591 
    592 	/* fetch a source operand : may not be used */
    593 	DPRINTF(("%s: dst/src FP%d=%08x,%08x,%08x\n", __func__,
    594 	    regnum, fpregs[regnum * 3], fpregs[regnum * 3 + 1],
    595 	    fpregs[regnum * 3 + 2]));
    596 
    597 	fpu_explode(fe, &fe->fe_f1, FTYPE_EXT, &fpregs[regnum * 3]);
    598 
    599 	DUMP_INSN(insn);
    600 
    601 	/* get the other operand which is always the source */
    602 	if ((word1 & 0x4000) == 0) {
    603 		DPRINTF(("%s: FP%d op FP%d => FP%d\n", __func__,
    604 		    format, regnum, regnum));
    605 		DPRINTF(("%s: src opr FP%d=%08x,%08x,%08x\n", __func__,
    606 		    format, fpregs[format * 3], fpregs[format * 3 + 1],
    607 		    fpregs[format * 3 + 2]));
    608 		fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
    609 	} else {
    610 		/* the operand is in memory */
    611 		if (format == FTYPE_DBL) {
    612 			insn->is_datasize = 8;
    613 		} else if (format == FTYPE_SNG || format == FTYPE_LNG) {
    614 			insn->is_datasize = 4;
    615 		} else if (format == FTYPE_WRD) {
    616 			insn->is_datasize = 2;
    617 		} else if (format == FTYPE_BYT) {
    618 			insn->is_datasize = 1;
    619 		} else if (format == FTYPE_EXT) {
    620 			insn->is_datasize = 12;
    621 		} else {
    622 			/* invalid or unsupported operand format */
    623 			sig = SIGFPE;
    624 			return sig;
    625 		}
    626 
    627 		/* Get effective address. (modreg=opcode&077) */
    628 		sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
    629 		if (sig) {
    630 			DPRINTF(("%s: error in fpu_decode_ea\n", __func__));
    631 			return sig;
    632 		}
    633 
    634 		DUMP_INSN(insn);
    635 
    636 #ifdef DEBUG_FPE
    637 		printf("%s: addr mode = ", __func__);
    638 		flags = insn->is_ea.ea_flags;
    639 		regname = (insn->is_ea.ea_regnum & 8) ? 'a' : 'd';
    640 
    641 		if (flags & EA_DIRECT) {
    642 			printf("%c%d\n", regname, insn->is_ea.ea_regnum & 7);
    643 		} else if (flags & EA_PC_REL) {
    644 			if (flags & EA_OFFSET) {
    645 				printf("pc@(%d)\n", insn->is_ea.ea_offset);
    646 			} else if (flags & EA_INDEXED) {
    647 				printf("pc@(...)\n");
    648 			}
    649 		} else if (flags & EA_PREDECR) {
    650 			printf("%c%d@-\n", regname, insn->is_ea.ea_regnum & 7);
    651 		} else if (flags & EA_POSTINCR) {
    652 			printf("%c%d@+\n", regname, insn->is_ea.ea_regnum & 7);
    653 		} else if (flags & EA_OFFSET) {
    654 			printf("%c%d@(%d)\n", regname,
    655 			    insn->is_ea.ea_regnum & 7,
    656 			    insn->is_ea.ea_offset);
    657 		} else if (flags & EA_INDEXED) {
    658 			printf("%c%d@(...)\n", regname,
    659 			    insn->is_ea.ea_regnum & 7);
    660 		} else if (flags & EA_ABS) {
    661 			printf("0x%08x\n", insn->is_ea.ea_absaddr);
    662 		} else if (flags & EA_IMMED) {
    663 			printf("#0x%08x,%08x,%08x\n",
    664 			    insn->is_ea.ea_immed[0],
    665 			    insn->is_ea.ea_immed[1],
    666 			    insn->is_ea.ea_immed[2]);
    667 		} else {
    668 			printf("%c%d@\n", regname, insn->is_ea.ea_regnum & 7);
    669 		}
    670 #endif /* DEBUG_FPE */
    671 
    672 		fpu_load_ea(frame, insn, &insn->is_ea, (char*)buf);
    673 		if (format == FTYPE_WRD) {
    674 			/* sign-extend */
    675 			buf[0] &= 0xffff;
    676 			if (buf[0] & 0x8000)
    677 				buf[0] |= 0xffff0000;
    678 			format = FTYPE_LNG;
    679 		} else if (format == FTYPE_BYT) {
    680 			/* sign-extend */
    681 			buf[0] &= 0xff;
    682 			if (buf[0] & 0x80)
    683 				buf[0] |= 0xffffff00;
    684 			format = FTYPE_LNG;
    685 		}
    686 		DPRINTF(("%s: src = %08x %08x %08x, siz = %d\n", __func__,
    687 		    buf[0], buf[1], buf[2], insn->is_datasize));
    688 		fpu_explode(fe, &fe->fe_f2, format, buf);
    689 	}
    690 
    691 	DUMP_INSN(insn);
    692 
    693 	/*
    694 	 * An arithmetic instruction emulate function has a prototype of
    695 	 * struct fpn *fpu_op(struct fpemu *);
    696 	 *
    697 	 * 1) If the instruction is monadic, then fpu_op() must use
    698 	 *    fe->fe_f2 as its operand, and return a pointer to the
    699 	 *    result.
    700 	 *
    701 	 * 2) If the instruction is diadic, then fpu_op() must use
    702 	 *    fe->fe_f1 and fe->fe_f2 as its two operands, and return a
    703 	 *    pointer to the result.
    704 	 *
    705 	 */
    706 	res = NULL;
    707 	switch (word1 & 0x7f) {
    708 	case 0x00:		/* fmove */
    709 		res = &fe->fe_f2;
    710 		break;
    711 
    712 	case 0x01:		/* fint */
    713 		res = fpu_int(fe);
    714 		break;
    715 
    716 	case 0x02:		/* fsinh */
    717 		res = fpu_sinh(fe);
    718 		break;
    719 
    720 	case 0x03:		/* fintrz */
    721 		res = fpu_intrz(fe);
    722 		break;
    723 
    724 	case 0x04:		/* fsqrt */
    725 		res = fpu_sqrt(fe);
    726 		break;
    727 
    728 	case 0x06:		/* flognp1 */
    729 		res = fpu_lognp1(fe);
    730 		break;
    731 
    732 	case 0x08:		/* fetoxm1 */
    733 		res = fpu_etoxm1(fe);
    734 		break;
    735 
    736 	case 0x09:		/* ftanh */
    737 		res = fpu_tanh(fe);
    738 		break;
    739 
    740 	case 0x0A:		/* fatan */
    741 		res = fpu_atan(fe);
    742 		break;
    743 
    744 	case 0x0C:		/* fasin */
    745 		res = fpu_asin(fe);
    746 		break;
    747 
    748 	case 0x0D:		/* fatanh */
    749 		res = fpu_atanh(fe);
    750 		break;
    751 
    752 	case 0x0E:		/* fsin */
    753 		res = fpu_sin(fe);
    754 		break;
    755 
    756 	case 0x0F:		/* ftan */
    757 		res = fpu_tan(fe);
    758 		break;
    759 
    760 	case 0x10:		/* fetox */
    761 		res = fpu_etox(fe);
    762 		break;
    763 
    764 	case 0x11:		/* ftwotox */
    765 		res = fpu_twotox(fe);
    766 		break;
    767 
    768 	case 0x12:		/* ftentox */
    769 		res = fpu_tentox(fe);
    770 		break;
    771 
    772 	case 0x14:		/* flogn */
    773 		res = fpu_logn(fe);
    774 		break;
    775 
    776 	case 0x15:		/* flog10 */
    777 		res = fpu_log10(fe);
    778 		break;
    779 
    780 	case 0x16:		/* flog2 */
    781 		res = fpu_log2(fe);
    782 		break;
    783 
    784 	case 0x18:		/* fabs */
    785 		fe->fe_f2.fp_sign = 0;
    786 		res = &fe->fe_f2;
    787 		break;
    788 
    789 	case 0x19:		/* fcosh */
    790 		res = fpu_cosh(fe);
    791 		break;
    792 
    793 	case 0x1A:		/* fneg */
    794 		fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign;
    795 		res = &fe->fe_f2;
    796 		break;
    797 
    798 	case 0x1C:		/* facos */
    799 		res = fpu_acos(fe);
    800 		break;
    801 
    802 	case 0x1D:		/* fcos */
    803 		res = fpu_cos(fe);
    804 		break;
    805 
    806 	case 0x1E:		/* fgetexp */
    807 		res = fpu_getexp(fe);
    808 		break;
    809 
    810 	case 0x1F:		/* fgetman */
    811 		res = fpu_getman(fe);
    812 		break;
    813 
    814 	case 0x20:		/* fdiv */
    815 	case 0x24:		/* fsgldiv: cheating - better than nothing */
    816 		res = fpu_div(fe);
    817 		break;
    818 
    819 	case 0x21:		/* fmod */
    820 		res = fpu_mod(fe);
    821 		break;
    822 
    823 	case 0x28:		/* fsub */
    824 		fe->fe_f2.fp_sign = !fe->fe_f2.fp_sign; /* f2 = -f2 */
    825 		/* FALLTHROUGH */
    826 	case 0x22:		/* fadd */
    827 		res = fpu_add(fe);
    828 		break;
    829 
    830 	case 0x23:		/* fmul */
    831 	case 0x27:		/* fsglmul: cheating - better than nothing */
    832 		res = fpu_mul(fe);
    833 		break;
    834 
    835 	case 0x25:		/* frem */
    836 		res = fpu_rem(fe);
    837 		break;
    838 
    839 	case 0x26:
    840 		/* fscale is handled by a separate function */
    841 		break;
    842 
    843 	case 0x30:
    844 	case 0x31:
    845 	case 0x32:
    846 	case 0x33:
    847 	case 0x34:
    848 	case 0x35:
    849 	case 0x36:
    850 	case 0x37:		/* fsincos */
    851 		res = fpu_sincos(fe, word1 & 7);
    852 		break;
    853 
    854 	case 0x38:		/* fcmp */
    855 		res = fpu_cmp(fe);
    856 		discard_result = 1;
    857 		break;
    858 
    859 	case 0x3A:		/* ftst */
    860 		res = &fe->fe_f2;
    861 		discard_result = 1;
    862 		break;
    863 
    864 	default:		/* possibly 040/060 instructions */
    865 		DPRINTF(("%s: bad opcode=0x%x, word1=0x%x\n", __func__,
    866 		    insn->is_opcode, insn->is_word1));
    867 		sig = SIGILL;
    868 	}
    869 
    870 	/* for sanity */
    871 	if (res == NULL)
    872 		sig = SIGILL;
    873 
    874 	if (sig == 0) {
    875 		if (!discard_result)
    876 			fpu_implode(fe, res, FTYPE_EXT, &fpregs[regnum * 3]);
    877 
    878 		/* update fpsr according to the result of operation */
    879 		fpu_upd_fpsr(fe, res);
    880 #ifdef DEBUG_FPE
    881 		if (!discard_result) {
    882 			printf("%s: %08x,%08x,%08x stored in FP%d\n", __func__,
    883 			    fpregs[regnum * 3],
    884 			    fpregs[regnum * 3 + 1],
    885 			    fpregs[regnum * 3 + 2],
    886 			    regnum);
    887 		} else {
    888 			static const char *class_name[] =
    889 			    { "SNAN", "QNAN", "ZERO", "NUM", "INF" };
    890 			printf("%s: result(%s,%c,%d,%08x,%08x,%08x) "
    891 			    "discarded\n", __func__,
    892 			    class_name[res->fp_class + 2],
    893 			    res->fp_sign ? '-' : '+', res->fp_exp,
    894 			    res->fp_mant[0], res->fp_mant[1],
    895 			    res->fp_mant[2]);
    896 		}
    897 #endif
    898 	} else {
    899 		DPRINTF(("%s: received signal %d\n", __func__, sig));
    900 	}
    901 
    902 	DPRINTF(("%s: FPSR = %08x, FPCR = %08x\n", __func__,
    903 	    fe->fe_fpsr, fe->fe_fpcr));
    904 
    905 	DUMP_INSN(insn);
    906 
    907 	return sig;
    908 }
    909 
    910 /*
    911  * test condition code according to the predicate in the opcode.
    912  * returns -1 when the predicate evaluates to true, 0 when false.
    913  * signal numbers are returned when an error is detected.
    914  */
    915 static int
    916 test_cc(struct fpemu *fe, int pred)
    917 {
    918 	int result, sig_bsun;
    919 	int fpsr;
    920 
    921 	fpsr = fe->fe_fpsr;
    922 	DPRINTF(("%s: fpsr=0x%08x\n", __func__, fpsr));
    923 	pred &= 0x3f;		/* lowest 6 bits */
    924 
    925 	DPRINTF(("%s: ", __func__));
    926 
    927 	if (pred >= 0x20) {
    928 		DPRINTF(("Illegal condition code\n"));
    929 		return SIGILL;
    930 	} else if (pred & 0x10) {
    931 		/* IEEE nonaware tests */
    932 		sig_bsun = 1;
    933 		pred &= 0x0f;		/* lower 4 bits */
    934 	} else {
    935 		/* IEEE aware tests */
    936 		DPRINTF(("IEEE "));
    937 		sig_bsun = 0;
    938 	}
    939 
    940 	/*
    941 	 *           condition   real 68882
    942 	 * mnemonic  in manual   condition
    943 	 * --------  ----------  ----------
    944 	 * 0000 F    0           <-         = ~NAN &  0 & ~Z | 0
    945 	 * 0001 EQ   Z           <-         = ~NAN &  0 |  Z | 0
    946 	 * 0010 OGT  ~(NAN|Z|N)  <-         = ~NAN & ~N & ~Z | 0
    947 	 * 0011 OGE  Z|~(NAN|N)  <-         = ~NAN & ~N |  Z | 0
    948 	 * 0100 OLT  N&~(NAN|Z)  <-         = ~NAN &  N & ~Z | 0
    949 	 * 0101 OLE  Z|(N&~NAN)  <-         = ~NAN &  N |  Z | 0
    950 	 * 0110 OGL  ~(NAN|Z)    <-         = ~NAN &  1 & ~Z | 0
    951 	 * 0111 OR   ~NAN        Z|~NAN     = ~NAN &  1 |  Z | 0
    952 	 *
    953 	 * 1000 UN   NAN         <-         =  1   &  0 & ~Z | NAN
    954 	 * 1001 UEQ  NAN|Z       <-         =  1   &  0 |  Z | NAN
    955 	 * 1010 UGT  NAN|~(N|Z)  <-         =  1   & ~N & ~Z | NAN
    956 	 * 1011 UGE  NAN|(Z|~N)  <-         =  1   & ~N |  Z | NAN
    957 	 * 1100 ULT  NAN|(N&~Z)  <-         =  1   &  N & ~Z | NAN
    958 	 * 1101 ULE  NAN|(Z|N)   <-         =  1   &  N |  Z | NAN
    959 	 * 1110 NE   ~Z          NAN|(~Z)   =  1   &  1 & ~Z | NAN
    960 	 * 1111 T    1           <-         =  1   &  1 |  Z | NAN
    961 	 */
    962 	if ((pred & 0x08) == 0) {
    963 		result = ((fpsr & FPSR_NAN) == 0);
    964 	} else {
    965 		result = 1;
    966 	}
    967 	switch (pred & 0x06) {
    968 	case 0x00:	/* AND 0 */
    969 		result &= 0;
    970 		break;
    971 	case 0x02:	/* AND ~N */
    972 		result &= ((fpsr & FPSR_NEG) == 0);
    973 		break;
    974 	case 0x04:	/* AND N */
    975 		result &= ((fpsr & FPSR_NEG) != 0);
    976 		break;
    977 	case 0x06:	/* AND 1 */
    978 		result &= 1;
    979 		break;
    980 	}
    981 	if ((pred & 0x01) == 0) {
    982 		result &= ((fpsr & FPSR_ZERO) == 0);
    983 	} else {
    984 		result |= ((fpsr & FPSR_ZERO) != 0);
    985 	}
    986 	if ((pred & 0x08) != 0) {
    987 		result |= ((fpsr & FPSR_NAN) != 0);
    988 	}
    989 
    990 	DPRINTF(("=> %s (%d)\n", result ? "true" : "false", result));
    991 	/* if it's an IEEE unaware test and NAN is set, BSUN is set */
    992 	if (sig_bsun && (fpsr & FPSR_NAN)) {
    993 		fpsr |= FPSR_BSUN;
    994 	}
    995 	/* if BSUN is set, IOP is set too */
    996 	if ((fpsr & FPSR_BSUN)) {
    997 		fpsr |= FPSR_AIOP;
    998 	}
    999 
   1000 	/* put fpsr back */
   1001 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
   1002 
   1003 	return -result;
   1004 }
   1005 
   1006 /*
   1007  * type 1: fdbcc, fscc, ftrapcc
   1008  * In this function, we know:
   1009  *   (opcode & 0x01C0) == 0x0040
   1010  * return SIGILL for an illegal instruction.
   1011  * return SIGFPE if FTRAPcc's condition is met.
   1012  */
   1013 static int
   1014 fpu_emul_type1(struct fpemu *fe, struct instruction *insn)
   1015 {
   1016 	struct frame *frame = fe->fe_frame;
   1017 	int advance, sig, branch, displ;
   1018 	unsigned short sval;
   1019 
   1020 	branch = test_cc(fe, insn->is_word1);
   1021 	if (branch > 0)
   1022 		return branch;
   1023 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
   1024 
   1025 	sig = 0;
   1026 	switch (insn->is_opcode & 070) {
   1027 	case 010:			/* fdbcc */
   1028 		if (branch) {
   1029 			/* advance */
   1030 			insn->is_advance = 6;
   1031 		} else {
   1032 			/* decrement Dn and if (Dn != -1) branch */
   1033 			uint16_t count = frame->f_regs[insn->is_opcode & 7];
   1034 
   1035 			if (count-- != 0) {
   1036 				if (ufetch_short((void *)(insn->is_pc +
   1037 							   insn->is_advance),
   1038 						  &sval)) {
   1039 					DPRINTF(("%s: fault reading "
   1040 					    "displacement\n", __func__));
   1041 					return SIGSEGV;
   1042 				}
   1043 				displ = sval;
   1044 				/* sign-extend the displacement */
   1045 				displ &= 0xffff;
   1046 				if (displ & 0x8000) {
   1047 					displ |= 0xffff0000;
   1048 				}
   1049 				insn->is_advance += displ;
   1050 #if 0				/* XXX */
   1051 				insn->is_nextpc = insn->is_pc +
   1052 				    insn->is_advance;
   1053 #endif
   1054 			} else {
   1055 				insn->is_advance = 6;
   1056 			}
   1057 			/* write it back */
   1058 			frame->f_regs[insn->is_opcode & 7] &= 0xffff0000;
   1059 			frame->f_regs[insn->is_opcode & 7] |= (uint32_t)count;
   1060 		}
   1061 		break;
   1062 
   1063 	case 070:			/* ftrapcc or fscc */
   1064 		advance = 4;
   1065 		if ((insn->is_opcode & 07) >= 2) {
   1066 			switch (insn->is_opcode & 07) {
   1067 			case 3:		/* long opr */
   1068 				advance += 2;
   1069 			case 2:		/* word opr */
   1070 				advance += 2;
   1071 			case 4:		/* no opr */
   1072 				break;
   1073 			default:
   1074 				return SIGILL;
   1075 				break;
   1076 			}
   1077 			insn->is_advance = advance;
   1078 
   1079 			if (branch) {
   1080 				/* trap */
   1081 				sig = SIGFPE;
   1082 			}
   1083 			break;
   1084 		}
   1085 
   1086 		/* FALLTHROUGH */
   1087 	default:			/* fscc */
   1088 		insn->is_datasize = 1;	/* always byte */
   1089 		sig = fpu_decode_ea(frame, insn, &insn->is_ea, insn->is_opcode);
   1090 		if (sig) {
   1091 			break;
   1092 		}
   1093 		/* set result */
   1094 		sig = fpu_store_ea(frame, insn, &insn->is_ea, (char *)&branch);
   1095 		break;
   1096 	}
   1097 	return sig;
   1098 }
   1099 
   1100 /*
   1101  * Type 2 or 3: fbcc (also fnop)
   1102  * In this function, we know:
   1103  *   (opcode & 0x0180) == 0x0080
   1104  */
   1105 static int
   1106 fpu_emul_brcc(struct fpemu *fe, struct instruction *insn)
   1107 {
   1108 	int displ, word2;
   1109 	int sig;
   1110 	unsigned short sval;
   1111 
   1112 	/*
   1113 	 * Get branch displacement.
   1114 	 */
   1115 	displ = insn->is_word1;
   1116 
   1117 	if (insn->is_opcode & 0x40) {
   1118 		if (ufetch_short((void *)(insn->is_pc + insn->is_advance),
   1119 				  &sval)) {
   1120 			DPRINTF(("%s: fault reading word2\n", __func__));
   1121 			return SIGSEGV;
   1122 		}
   1123 		word2 = sval;
   1124 		displ <<= 16;
   1125 		displ |= word2;
   1126 		insn->is_advance += 2;
   1127 	} else {
   1128 		/* displacement is word sized */
   1129 		if (displ & 0x8000)
   1130 			displ |= 0xFFFF0000;
   1131 	}
   1132 
   1133 	/* XXX: If CC, insn->is_pc += displ */
   1134 	sig = test_cc(fe, insn->is_opcode);
   1135 	fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr;
   1136 
   1137 	if (fe->fe_fpsr & fe->fe_fpcr & FPSR_EXCP) {
   1138 		return SIGFPE;		/* caught an exception */
   1139 	}
   1140 	if (sig == -1) {
   1141 		/*
   1142 		 * branch does take place; 2 is the offset to the 1st disp word
   1143 		 */
   1144 		insn->is_advance = displ + 2;
   1145 #if 0		/* XXX */
   1146 		insn->is_nextpc = insn->is_pc + insn->is_advance;
   1147 #endif
   1148 	} else if (sig)
   1149 		return SIGILL;		/* got a signal */
   1150 	DPRINTF(("%s: %s insn @ %x (%x+%x) (disp=%x)\n", __func__,
   1151 	    (sig == -1) ? "BRANCH to" : "NEXT",
   1152 	    insn->is_pc + insn->is_advance, insn->is_pc, insn->is_advance,
   1153 	    displ));
   1154 	return 0;
   1155 }
   1156