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