Home | History | Annotate | Line # | Download | only in fpu
fpu.c revision 1.21
      1  1.21        pk /*	$NetBSD: fpu.c,v 1.21 2003/10/05 21:13:23 pk Exp $ */
      2   1.2   deraadt 
      3   1.1   deraadt /*
      4   1.1   deraadt  * Copyright (c) 1992, 1993
      5   1.1   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1   deraadt  *
      7   1.1   deraadt  * This software was developed by the Computer Systems Engineering group
      8   1.1   deraadt  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1   deraadt  * contributed to Berkeley.
     10   1.1   deraadt  *
     11   1.1   deraadt  * All advertising materials mentioning features or use of this software
     12   1.1   deraadt  * must display the following acknowledgement:
     13   1.1   deraadt  *	This product includes software developed by the University of
     14   1.1   deraadt  *	California, Lawrence Berkeley Laboratory.
     15   1.1   deraadt  *
     16   1.1   deraadt  * Redistribution and use in source and binary forms, with or without
     17   1.1   deraadt  * modification, are permitted provided that the following conditions
     18   1.1   deraadt  * are met:
     19   1.1   deraadt  * 1. Redistributions of source code must retain the above copyright
     20   1.1   deraadt  *    notice, this list of conditions and the following disclaimer.
     21   1.1   deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.1   deraadt  *    notice, this list of conditions and the following disclaimer in the
     23   1.1   deraadt  *    documentation and/or other materials provided with the distribution.
     24  1.20       agc  * 3. Neither the name of the University nor the names of its contributors
     25   1.1   deraadt  *    may be used to endorse or promote products derived from this software
     26   1.1   deraadt  *    without specific prior written permission.
     27   1.1   deraadt  *
     28   1.1   deraadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1   deraadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1   deraadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1   deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1   deraadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1   deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1   deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1   deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1   deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1   deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1   deraadt  * SUCH DAMAGE.
     39   1.1   deraadt  *
     40   1.1   deraadt  *	@(#)fpu.c	8.1 (Berkeley) 6/11/93
     41   1.1   deraadt  */
     42  1.19     lukem 
     43  1.19     lukem #include <sys/cdefs.h>
     44  1.21        pk __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.21 2003/10/05 21:13:23 pk Exp $");
     45   1.1   deraadt 
     46   1.1   deraadt #include <sys/param.h>
     47   1.1   deraadt #include <sys/proc.h>
     48   1.1   deraadt #include <sys/signal.h>
     49   1.1   deraadt #include <sys/systm.h>
     50   1.1   deraadt #include <sys/syslog.h>
     51   1.3  christos #include <sys/signalvar.h>
     52   1.1   deraadt 
     53   1.1   deraadt #include <machine/instr.h>
     54   1.1   deraadt #include <machine/reg.h>
     55   1.1   deraadt 
     56   1.1   deraadt #include <sparc/fpu/fpu_emu.h>
     57   1.3  christos #include <sparc/fpu/fpu_extern.h>
     58   1.1   deraadt 
     59   1.8       eeh int fpe_debug = 0;
     60   1.8       eeh 
     61   1.8       eeh #ifdef DEBUG
     62   1.8       eeh /*
     63   1.8       eeh  * Dump a `fpn' structure.
     64   1.8       eeh  */
     65   1.8       eeh void
     66   1.8       eeh fpu_dumpfpn(struct fpn *fp)
     67   1.8       eeh {
     68   1.8       eeh 	static char *class[] = {
     69   1.8       eeh 		"SNAN", "QNAN", "ZERO", "NUM", "INF"
     70   1.8       eeh 	};
     71   1.8       eeh 
     72   1.8       eeh 	printf("%s %c.%x %x %x %xE%d", class[fp->fp_class + 2],
     73   1.8       eeh 		fp->fp_sign ? '-' : ' ',
     74   1.8       eeh 		fp->fp_mant[0],	fp->fp_mant[1],
     75   1.8       eeh 		fp->fp_mant[2], fp->fp_mant[3],
     76   1.8       eeh 		fp->fp_exp);
     77   1.8       eeh }
     78   1.8       eeh #endif
     79   1.8       eeh 
     80   1.1   deraadt /*
     81   1.1   deraadt  * fpu_execute returns the following error numbers (0 = no error):
     82   1.1   deraadt  */
     83   1.1   deraadt #define	FPE		1	/* take a floating point exception */
     84   1.1   deraadt #define	NOTFPU		2	/* not an FPU instruction */
     85   1.1   deraadt 
     86   1.1   deraadt /*
     87   1.1   deraadt  * Translate current exceptions into `first' exception.  The
     88   1.1   deraadt  * bits go the wrong way for ffs() (0x10 is most important, etc).
     89   1.1   deraadt  * There are only 5, so do it the obvious way.
     90   1.1   deraadt  */
     91   1.1   deraadt #define	X1(x) x
     92   1.1   deraadt #define	X2(x) x,x
     93   1.1   deraadt #define	X4(x) x,x,x,x
     94   1.1   deraadt #define	X8(x) X4(x),X4(x)
     95   1.1   deraadt #define	X16(x) X8(x),X8(x)
     96   1.1   deraadt 
     97   1.1   deraadt static char cx_to_trapx[] = {
     98   1.1   deraadt 	X1(FSR_NX),
     99   1.1   deraadt 	X2(FSR_DZ),
    100   1.1   deraadt 	X4(FSR_UF),
    101   1.1   deraadt 	X8(FSR_OF),
    102   1.1   deraadt 	X16(FSR_NV)
    103   1.1   deraadt };
    104  1.21        pk static u_char fpu_codes_native[] = {
    105  1.21        pk 	X1(FPE_FLTRES),
    106  1.21        pk 	X2(FPE_FLTDIV),
    107  1.21        pk 	X4(FPE_FLTUND),
    108  1.21        pk 	X8(FPE_FLTOVF),
    109  1.21        pk 	X16(FPE_FLTINV)
    110  1.21        pk };
    111  1.21        pk #if defined(COMPAT_SUNOS) || defined(SVR4_COMPAT)
    112  1.21        pk static u_char fpu_codes_compat[] = {
    113   1.1   deraadt 	X1(FPE_FLTINEX_TRAP),
    114   1.1   deraadt 	X2(FPE_FLTDIV_TRAP),
    115   1.1   deraadt 	X4(FPE_FLTUND_TRAP),
    116   1.1   deraadt 	X8(FPE_FLTOVF_TRAP),
    117   1.1   deraadt 	X16(FPE_FLTOPERR_TRAP)
    118   1.1   deraadt };
    119  1.21        pk #ifdef COMPAT_SUNOS
    120  1.21        pk extern struct emul emul_sunos;
    121  1.21        pk #endif
    122  1.21        pk #ifdef COMPAT_SVR4
    123  1.21        pk extern struct emul emul_svr4;
    124  1.21        pk #endif
    125  1.21        pk #endif /* {SUNOS,SVR4}_COMPAT */
    126   1.1   deraadt 
    127   1.1   deraadt /*
    128   1.1   deraadt  * The FPU gave us an exception.  Clean up the mess.  Note that the
    129   1.1   deraadt  * fp queue can only have FPops in it, never load/store FP registers
    130   1.1   deraadt  * nor FBfcc instructions.  Experiments with `crashme' prove that
    131   1.1   deraadt  * unknown FPops do enter the queue, however.
    132   1.1   deraadt  */
    133   1.3  christos void
    134  1.17   thorpej fpu_cleanup(l, fs)
    135  1.21        pk 	struct lwp *l;
    136   1.7       mrg #ifndef SUN4U
    137  1.21        pk 	struct fpstate *fs;
    138   1.7       mrg #else /* SUN4U */
    139  1.21        pk 	struct fpstate64 *fs;
    140   1.7       mrg #endif /* SUN4U */
    141   1.1   deraadt {
    142  1.21        pk 	int i, fsr = fs->fs_fsr, error;
    143  1.17   thorpej 	struct proc *p = l->l_proc;
    144   1.1   deraadt 	union instr instr;
    145   1.1   deraadt 	struct fpemu fe;
    146  1.21        pk 	u_char *fpu_codes;
    147  1.21        pk 	ksiginfo_t ksi;
    148  1.21        pk 
    149  1.21        pk 	fpu_codes = (0
    150  1.21        pk #ifdef COMPAT_SUNOS
    151  1.21        pk 			|| p->p_emul == &emul_sunos
    152  1.21        pk #endif
    153  1.21        pk #ifdef SVR4_COMPAT
    154  1.21        pk 			|| p->p_emul == &emul_svr4
    155  1.21        pk #endif
    156  1.21        pk 		) ? fpu_codes_compat : fpu_codes_native;
    157   1.1   deraadt 
    158   1.1   deraadt 	switch ((fsr >> FSR_FTT_SHIFT) & FSR_FTT_MASK) {
    159   1.1   deraadt 
    160   1.1   deraadt 	case FSR_TT_NONE:
    161   1.7       mrg 		panic("fpu_cleanup: No fault");	/* ??? */
    162   1.1   deraadt 		break;
    163   1.1   deraadt 
    164   1.1   deraadt 	case FSR_TT_IEEE:
    165  1.14       eeh 		DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_IEEE\n"));
    166   1.1   deraadt 		/* XXX missing trap address! */
    167   1.1   deraadt 		if ((i = fsr & FSR_CX) == 0)
    168   1.1   deraadt 			panic("fpu ieee trap, but no exception");
    169  1.21        pk 		ksi.ksi_signo = SIGFPE;
    170  1.21        pk 		ksi.ksi_code = fpu_codes[i - 1];
    171  1.17   thorpej 		KERNEL_PROC_LOCK(l);
    172  1.21        pk #ifdef __HAVE_SIGINFO
    173  1.21        pk 		trapsignal(l, &ksi);
    174  1.21        pk #else
    175  1.21        pk 		trapsignal(l, ksi.ksi_signo, ksi.ksi_code);
    176  1.21        pk #endif
    177  1.17   thorpej 		KERNEL_PROC_UNLOCK(l);
    178   1.1   deraadt 		break;		/* XXX should return, but queue remains */
    179   1.1   deraadt 
    180   1.1   deraadt 	case FSR_TT_UNFIN:
    181  1.14       eeh 		DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_UNFIN\n"));
    182   1.7       mrg #ifdef SUN4U
    183   1.7       mrg 		if (fs->fs_qsize == 0) {
    184   1.7       mrg 			printf("fpu_cleanup: unfinished fpop");
    185   1.7       mrg 			/* The book sez reexecute or emulate. */
    186   1.7       mrg 			return;
    187   1.7       mrg 		}
    188   1.7       mrg 		break;
    189   1.7       mrg 
    190   1.7       mrg #endif /* SUN4U */
    191   1.1   deraadt 	case FSR_TT_UNIMP:
    192  1.14       eeh 		DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_UNIMP\n"));
    193   1.1   deraadt 		if (fs->fs_qsize == 0)
    194   1.7       mrg 			panic("fpu_cleanup: unimplemented fpop");
    195   1.1   deraadt 		break;
    196   1.1   deraadt 
    197   1.1   deraadt 	case FSR_TT_SEQ:
    198   1.1   deraadt 		panic("fpu sequence error");
    199   1.1   deraadt 		/* NOTREACHED */
    200   1.1   deraadt 
    201   1.1   deraadt 	case FSR_TT_HWERR:
    202  1.14       eeh 		DPRINTF(FPE_INSN, ("fpu_cleanup: FSR_TT_HWERR\n"));
    203   1.1   deraadt 		log(LOG_ERR, "fpu hardware error (%s[%d])\n",
    204   1.1   deraadt 		    p->p_comm, p->p_pid);
    205   1.1   deraadt 		uprintf("%s[%d]: fpu hardware error\n", p->p_comm, p->p_pid);
    206  1.21        pk 		ksi.ksi_signo = SIGFPE;
    207  1.21        pk 		ksi.ksi_code = 0;
    208  1.17   thorpej 		KERNEL_PROC_LOCK(l);
    209  1.21        pk #ifdef __HAVE_SIGINFO
    210  1.21        pk 		trapsignal(l, &ksi);
    211  1.21        pk #else
    212  1.21        pk 		trapsignal(l, ksi.ksi_signo, ksi.ksi_code);
    213  1.21        pk #endif
    214  1.17   thorpej 		KERNEL_PROC_UNLOCK(l);
    215   1.1   deraadt 		goto out;
    216   1.1   deraadt 
    217   1.1   deraadt 	default:
    218   1.6      fair 		printf("fsr=0x%x\n", fsr);
    219   1.1   deraadt 		panic("fpu error");
    220   1.1   deraadt 	}
    221   1.1   deraadt 
    222   1.1   deraadt 	/* emulate the instructions left in the queue */
    223   1.1   deraadt 	fe.fe_fpstate = fs;
    224   1.1   deraadt 	for (i = 0; i < fs->fs_qsize; i++) {
    225   1.1   deraadt 		instr.i_int = fs->fs_queue[i].fq_instr;
    226   1.1   deraadt 		if (instr.i_any.i_op != IOP_reg ||
    227   1.1   deraadt 		    (instr.i_op3.i_op3 != IOP3_FPop1 &&
    228   1.1   deraadt 		     instr.i_op3.i_op3 != IOP3_FPop2))
    229   1.1   deraadt 			panic("bogus fpu queue");
    230   1.1   deraadt 		error = fpu_execute(&fe, instr);
    231  1.16        pk 		if (error == 0)
    232   1.1   deraadt 			continue;
    233   1.1   deraadt 
    234  1.17   thorpej 		KERNEL_PROC_LOCK(l);
    235  1.16        pk 		switch (error) {
    236   1.1   deraadt 		case FPE:
    237  1.21        pk 			ksi.ksi_signo = SIGFPE;
    238  1.21        pk 			ksi.ksi_code = fpu_codes[(fs->fs_fsr & FSR_CX) - 1];
    239  1.21        pk #ifdef __HAVE_SIGINFO
    240  1.21        pk 			trapsignal(l, &ksi);
    241  1.21        pk #else
    242  1.21        pk 			trapsignal(l, ksi.ksi_signo, ksi.ksi_code);
    243  1.21        pk #endif
    244   1.1   deraadt 			break;
    245   1.1   deraadt 
    246   1.1   deraadt 		case NOTFPU:
    247   1.7       mrg #ifdef SUN4U
    248   1.7       mrg #ifdef DEBUG
    249  1.11       mrg 			printf("fpu_cleanup: not an FPU error -- sending SIGILL\n");
    250   1.7       mrg #endif
    251   1.7       mrg #endif /* SUN4U */
    252  1.21        pk 			ksi.ksi_signo = SIGFPE;
    253  1.21        pk 			ksi.ksi_code = ILL_ILLOPC;
    254  1.21        pk #ifdef __HAVE_SIGINFO
    255  1.21        pk 			trapsignal(l, &ksi);
    256  1.21        pk #else
    257  1.21        pk 			trapsignal(l, ksi.ksi_signo, ksi.ksi_code);
    258  1.21        pk #endif
    259   1.1   deraadt 			break;
    260   1.1   deraadt 
    261   1.1   deraadt 		default:
    262   1.1   deraadt 			panic("fpu_cleanup 3");
    263   1.1   deraadt 			/* NOTREACHED */
    264   1.1   deraadt 		}
    265  1.17   thorpej 		KERNEL_PROC_UNLOCK(l);
    266   1.1   deraadt 		/* XXX should stop here, but queue remains */
    267   1.1   deraadt 	}
    268   1.1   deraadt out:
    269   1.1   deraadt 	fs->fs_qsize = 0;
    270   1.1   deraadt }
    271   1.1   deraadt 
    272   1.1   deraadt #ifdef notyet
    273   1.1   deraadt /*
    274   1.1   deraadt  * If we have no FPU at all (are there any machines like this out
    275   1.1   deraadt  * there!?) we have to emulate each instruction, and we need a pointer
    276   1.1   deraadt  * to the trapframe so that we can step over them and do FBfcc's.
    277   1.1   deraadt  * We know the `queue' is empty, though; we just want to emulate
    278   1.1   deraadt  * the instruction at tf->tf_pc.
    279   1.1   deraadt  */
    280  1.17   thorpej fpu_emulate(l, tf, fs)
    281  1.17   thorpej 	struct lwp *l;
    282  1.21        pk 	struct trapframe *tf;
    283   1.7       mrg #ifndef SUN4U
    284  1.21        pk 	struct fpstate *fs;
    285   1.7       mrg #else /* SUN4U */
    286  1.21        pk 	struct fpstate64 *fs;
    287   1.7       mrg #endif /* SUN4U */
    288   1.1   deraadt {
    289   1.1   deraadt 
    290   1.1   deraadt 	do {
    291   1.1   deraadt 		fetch instr from pc
    292   1.1   deraadt 		decode
    293   1.1   deraadt 		if (integer instr) {
    294   1.1   deraadt 			/*
    295   1.1   deraadt 			 * We do this here, rather than earlier, to avoid
    296   1.1   deraadt 			 * losing even more badly than usual.
    297   1.1   deraadt 			 */
    298  1.17   thorpej 			if (l->l_addr->u_pcb.pcb_uw) {
    299   1.1   deraadt 				write_user_windows();
    300  1.17   thorpej 				if (rwindow_save(l))
    301  1.17   thorpej 					sigexit(l, SIGILL);
    302   1.1   deraadt 			}
    303   1.1   deraadt 			if (loadstore) {
    304   1.1   deraadt 				do_it;
    305   1.1   deraadt 				pc = npc, npc += 4
    306   1.1   deraadt 			} else if (fbfcc) {
    307   1.1   deraadt 				do_annul_stuff;
    308   1.1   deraadt 			} else
    309   1.1   deraadt 				return;
    310   1.1   deraadt 		} else if (fpu instr) {
    311   1.1   deraadt 			fe.fe_fsr = fs->fs_fsr &= ~FSR_CX;
    312   1.1   deraadt 			error = fpu_execute(&fe, fs, instr);
    313   1.1   deraadt 			switch (error) {
    314   1.1   deraadt 				etc;
    315   1.1   deraadt 			}
    316   1.1   deraadt 		} else
    317   1.1   deraadt 			return;
    318   1.1   deraadt 		if (want to reschedule)
    319   1.1   deraadt 			return;
    320   1.1   deraadt 	} while (error == 0);
    321   1.1   deraadt }
    322   1.1   deraadt #endif
    323   1.1   deraadt 
    324   1.1   deraadt /*
    325   1.1   deraadt  * Execute an FPU instruction (one that runs entirely in the FPU; not
    326   1.1   deraadt  * FBfcc or STF, for instance).  On return, fe->fe_fs->fs_fsr will be
    327   1.1   deraadt  * modified to reflect the setting the hardware would have left.
    328   1.1   deraadt  *
    329   1.1   deraadt  * Note that we do not catch all illegal opcodes, so you can, for instance,
    330   1.1   deraadt  * multiply two integers this way.
    331   1.1   deraadt  */
    332   1.1   deraadt int
    333   1.1   deraadt fpu_execute(fe, instr)
    334  1.21        pk 	struct fpemu *fe;
    335   1.1   deraadt 	union instr instr;
    336   1.1   deraadt {
    337  1.21        pk 	struct fpn *fp;
    338   1.7       mrg #ifndef SUN4U
    339  1.21        pk 	int opf, rs1, rs2, rd, type, mask, fsr, cx;
    340  1.21        pk 	struct fpstate *fs;
    341   1.7       mrg #else /* SUN4U */
    342  1.21        pk 	int opf, rs1, rs2, rd, type, mask, fsr, cx, i, cond;
    343  1.21        pk 	struct fpstate64 *fs;
    344   1.7       mrg #endif /* SUN4U */
    345   1.1   deraadt 	u_int space[4];
    346  1.14       eeh 
    347   1.1   deraadt 	/*
    348   1.1   deraadt 	 * `Decode' and execute instruction.  Start with no exceptions.
    349   1.1   deraadt 	 * The type of any i_opf opcode is in the bottom two bits, so we
    350   1.1   deraadt 	 * squish them out here.
    351   1.1   deraadt 	 */
    352   1.1   deraadt 	opf = instr.i_opf.i_opf;
    353  1.12       eeh 	/*
    354  1.12       eeh 	 * The low two bits of the opf field for floating point insns usually
    355  1.12       eeh 	 * correspond to the operation width:
    356  1.12       eeh 	 *
    357  1.12       eeh 	 *	0:	Invalid
    358  1.12       eeh 	 *	1:	Single precision float
    359  1.12       eeh 	 *	2:	Double precision float
    360  1.12       eeh 	 *	3:	Quad precision float
    361  1.12       eeh 	 *
    362  1.12       eeh 	 * The exceptions are the integer to float conversion instructions.
    363  1.12       eeh 	 *
    364  1.12       eeh 	 * For double and quad precision, the low bit if the rs or rd field
    365  1.12       eeh 	 * is actually the high bit of the register number.
    366  1.12       eeh 	 */
    367  1.12       eeh 
    368   1.1   deraadt 	type = opf & 3;
    369  1.12       eeh 	mask = 0x3 >> (3 - type);
    370  1.12       eeh 
    371  1.12       eeh 	rs1 = instr.i_opf.i_rs1;
    372  1.12       eeh 	rs1 = (rs1 & ~mask) | ((rs1 & mask & 0x1) << 5);
    373  1.12       eeh 	rs2 = instr.i_opf.i_rs2;
    374  1.12       eeh 	rs2 = (rs2 & ~mask) | ((rs2 & mask & 0x1) << 5);
    375  1.12       eeh 	rd = instr.i_opf.i_rd;
    376  1.12       eeh 	rd = (rd & ~mask) | ((rd & mask & 0x1) << 5);
    377  1.12       eeh #ifdef DIAGNOSTIC
    378   1.1   deraadt 	if ((rs1 | rs2 | rd) & mask)
    379  1.13       eeh 		/* This may be an FPU insn but it is illegal. */
    380  1.13       eeh 		return (NOTFPU);
    381   1.1   deraadt #endif
    382   1.1   deraadt 	fs = fe->fe_fpstate;
    383   1.1   deraadt 	fe->fe_fsr = fs->fs_fsr & ~FSR_CX;
    384   1.1   deraadt 	fe->fe_cx = 0;
    385   1.7       mrg #ifdef SUN4U
    386   1.7       mrg 	/*
    387   1.7       mrg 	 * Check to see if we're dealing with a fancy cmove and handle
    388  1.12       eeh 	 * it first.
    389   1.7       mrg 	 */
    390   1.7       mrg 	if (instr.i_op3.i_op3 == IOP3_FPop2 && (opf&0xff0) != (FCMP&0xff0)) {
    391   1.7       mrg 		switch (opf >>= 2) {
    392   1.7       mrg 		case FMVFC0 >> 2:
    393   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVFC0\n"));
    394   1.7       mrg 			cond = (fs->fs_fsr>>FSR_FCC_SHIFT)&FSR_FCC_MASK;
    395   1.7       mrg 			if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
    396   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    397   1.7       mrg 			goto mov;
    398   1.7       mrg 		case FMVFC1 >> 2:
    399   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVFC1\n"));
    400   1.7       mrg 			cond = (fs->fs_fsr>>FSR_FCC1_SHIFT)&FSR_FCC_MASK;
    401   1.7       mrg 			if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
    402   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    403   1.7       mrg 			goto mov;
    404   1.7       mrg 		case FMVFC2 >> 2:
    405   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVFC2\n"));
    406   1.7       mrg 			cond = (fs->fs_fsr>>FSR_FCC2_SHIFT)&FSR_FCC_MASK;
    407   1.7       mrg 			if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
    408   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    409   1.7       mrg 			goto mov;
    410   1.7       mrg 		case FMVFC3 >> 2:
    411   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVFC3\n"));
    412   1.7       mrg 			cond = (fs->fs_fsr>>FSR_FCC3_SHIFT)&FSR_FCC_MASK;
    413   1.7       mrg 			if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
    414   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    415   1.7       mrg 			goto mov;
    416   1.7       mrg 		case FMVIC >> 2:
    417  1.17   thorpej 			/* Presume we're curlwp */
    418   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVIC\n"));
    419  1.17   thorpej 			cond = (curlwp->l_md.md_tf->tf_tstate>>TSTATE_CCR_SHIFT)&PSR_ICC;
    420   1.7       mrg 			if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
    421   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    422   1.7       mrg 			goto mov;
    423   1.7       mrg 		case FMVXC >> 2:
    424  1.17   thorpej 			/* Presume we're curlwp */
    425   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVXC\n"));
    426  1.17   thorpej 			cond = (curlwp->l_md.md_tf->tf_tstate>>(TSTATE_CCR_SHIFT+XCC_SHIFT))&PSR_ICC;
    427   1.7       mrg 			if (instr.i_fmovcc.i_cond != cond) return(0); /* success */
    428   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    429   1.7       mrg 			goto mov;
    430   1.7       mrg 		case FMVRZ >> 2:
    431  1.17   thorpej 			/* Presume we're curlwp */
    432   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVRZ\n"));
    433   1.7       mrg 			rs1 = instr.i_fmovr.i_rs1;
    434  1.17   thorpej 			if (rs1 != 0 && (int64_t)curlwp->l_md.md_tf->tf_global[rs1] != 0)
    435   1.7       mrg 				return (0); /* success */
    436   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    437   1.7       mrg 			goto mov;
    438   1.7       mrg 		case FMVRLEZ >> 2:
    439  1.17   thorpej 			/* Presume we're curlwp */
    440   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVRLEZ\n"));
    441   1.7       mrg 			rs1 = instr.i_fmovr.i_rs1;
    442  1.17   thorpej 			if (rs1 != 0 && (int64_t)curlwp->l_md.md_tf->tf_global[rs1] > 0)
    443   1.7       mrg 				return (0); /* success */
    444   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    445   1.7       mrg 			goto mov;
    446   1.7       mrg 		case FMVRLZ >> 2:
    447  1.17   thorpej 			/* Presume we're curlwp */
    448   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVRLZ\n"));
    449   1.7       mrg 			rs1 = instr.i_fmovr.i_rs1;
    450  1.17   thorpej 			if (rs1 == 0 || (int64_t)curlwp->l_md.md_tf->tf_global[rs1] >= 0)
    451   1.7       mrg 				return (0); /* success */
    452   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    453   1.7       mrg 			goto mov;
    454   1.7       mrg 		case FMVRNZ >> 2:
    455  1.17   thorpej 			/* Presume we're curlwp */
    456   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVRNZ\n"));
    457   1.7       mrg 			rs1 = instr.i_fmovr.i_rs1;
    458  1.17   thorpej 			if (rs1 == 0 || (int64_t)curlwp->l_md.md_tf->tf_global[rs1] == 0)
    459   1.7       mrg 				return (0); /* success */
    460   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    461   1.7       mrg 			goto mov;
    462   1.7       mrg 		case FMVRGZ >> 2:
    463  1.17   thorpej 			/* Presume we're curlwp */
    464   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVRGZ\n"));
    465   1.7       mrg 			rs1 = instr.i_fmovr.i_rs1;
    466  1.17   thorpej 			if (rs1 == 0 || (int64_t)curlwp->l_md.md_tf->tf_global[rs1] <= 0)
    467   1.7       mrg 				return (0); /* success */
    468   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    469   1.7       mrg 			goto mov;
    470   1.7       mrg 		case FMVRGEZ >> 2:
    471  1.17   thorpej 			/* Presume we're curlwp */
    472   1.8       eeh 			DPRINTF(FPE_INSN, ("fpu_execute: FMVRGEZ\n"));
    473   1.7       mrg 			rs1 = instr.i_fmovr.i_rs1;
    474  1.17   thorpej 			if (rs1 != 0 && (int64_t)curlwp->l_md.md_tf->tf_global[rs1] < 0)
    475   1.7       mrg 				return (0); /* success */
    476   1.7       mrg 			rs1 = fs->fs_regs[rs2];
    477   1.8       eeh 			goto mov;
    478   1.7       mrg 		default:
    479   1.8       eeh 			DPRINTF(FPE_INSN,
    480   1.8       eeh 				("fpu_execute: unknown v9 FP inst %x opf %x\n",
    481   1.8       eeh 					instr.i_int, opf));
    482   1.7       mrg 			return (NOTFPU);
    483   1.7       mrg 		}
    484   1.7       mrg 	}
    485   1.7       mrg #endif /* SUN4U */
    486   1.1   deraadt 	switch (opf >>= 2) {
    487   1.1   deraadt 
    488   1.1   deraadt 	default:
    489   1.8       eeh 		DPRINTF(FPE_INSN,
    490   1.8       eeh 			("fpu_execute: unknown basic FP inst %x opf %x\n",
    491   1.8       eeh 				instr.i_int, opf));
    492   1.1   deraadt 		return (NOTFPU);
    493   1.1   deraadt 
    494   1.1   deraadt 	case FMOV >> 2:		/* these should all be pretty obvious */
    495   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FMOV\n"));
    496   1.1   deraadt 		rs1 = fs->fs_regs[rs2];
    497   1.1   deraadt 		goto mov;
    498   1.1   deraadt 
    499   1.1   deraadt 	case FNEG >> 2:
    500   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FNEG\n"));
    501   1.1   deraadt 		rs1 = fs->fs_regs[rs2] ^ (1 << 31);
    502   1.1   deraadt 		goto mov;
    503   1.1   deraadt 
    504   1.1   deraadt 	case FABS >> 2:
    505   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FABS\n"));
    506   1.1   deraadt 		rs1 = fs->fs_regs[rs2] & ~(1 << 31);
    507   1.1   deraadt 	mov:
    508   1.7       mrg #ifndef SUN4U
    509   1.1   deraadt 		fs->fs_regs[rd] = rs1;
    510   1.7       mrg #else /* SUN4U */
    511  1.12       eeh 		i = 1<<(type-1);
    512   1.7       mrg 		fs->fs_regs[rd++] = rs1;
    513  1.12       eeh 		while (--i > 0)
    514   1.7       mrg 			fs->fs_regs[rd++] = fs->fs_regs[++rs2];
    515   1.7       mrg #endif /* SUN4U */
    516   1.1   deraadt 		fs->fs_fsr = fe->fe_fsr;
    517   1.1   deraadt 		return (0);	/* success */
    518   1.1   deraadt 
    519   1.1   deraadt 	case FSQRT >> 2:
    520   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FSQRT\n"));
    521   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs2);
    522   1.1   deraadt 		fp = fpu_sqrt(fe);
    523   1.1   deraadt 		break;
    524   1.1   deraadt 
    525   1.1   deraadt 	case FADD >> 2:
    526   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FADD\n"));
    527   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    528   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    529   1.1   deraadt 		fp = fpu_add(fe);
    530   1.1   deraadt 		break;
    531   1.1   deraadt 
    532   1.1   deraadt 	case FSUB >> 2:
    533   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FSUB\n"));
    534   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    535   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    536   1.1   deraadt 		fp = fpu_sub(fe);
    537   1.1   deraadt 		break;
    538   1.1   deraadt 
    539   1.1   deraadt 	case FMUL >> 2:
    540   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FMUL\n"));
    541   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    542   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    543   1.1   deraadt 		fp = fpu_mul(fe);
    544   1.1   deraadt 		break;
    545   1.1   deraadt 
    546   1.1   deraadt 	case FDIV >> 2:
    547   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FDIV\n"));
    548   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    549   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    550   1.1   deraadt 		fp = fpu_div(fe);
    551   1.1   deraadt 		break;
    552   1.1   deraadt 
    553   1.1   deraadt 	case FCMP >> 2:
    554   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FCMP\n"));
    555   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    556   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    557   1.1   deraadt 		fpu_compare(fe, 0);
    558   1.1   deraadt 		goto cmpdone;
    559   1.1   deraadt 
    560   1.1   deraadt 	case FCMPE >> 2:
    561   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FCMPE\n"));
    562   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    563   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    564   1.1   deraadt 		fpu_compare(fe, 1);
    565   1.1   deraadt 	cmpdone:
    566   1.1   deraadt 		/*
    567   1.1   deraadt 		 * The only possible exception here is NV; catch it
    568   1.1   deraadt 		 * early and get out, as there is no result register.
    569   1.1   deraadt 		 */
    570   1.1   deraadt 		cx = fe->fe_cx;
    571   1.1   deraadt 		fsr = fe->fe_fsr | (cx << FSR_CX_SHIFT);
    572   1.1   deraadt 		if (cx != 0) {
    573   1.1   deraadt 			if (fsr & (FSR_NV << FSR_TEM_SHIFT)) {
    574   1.1   deraadt 				fs->fs_fsr = (fsr & ~FSR_FTT) |
    575   1.1   deraadt 				    (FSR_TT_IEEE << FSR_FTT_SHIFT);
    576   1.1   deraadt 				return (FPE);
    577   1.1   deraadt 			}
    578   1.1   deraadt 			fsr |= FSR_NV << FSR_AX_SHIFT;
    579   1.1   deraadt 		}
    580   1.1   deraadt 		fs->fs_fsr = fsr;
    581   1.1   deraadt 		return (0);
    582   1.1   deraadt 
    583   1.1   deraadt 	case FSMULD >> 2:
    584   1.1   deraadt 	case FDMULX >> 2:
    585   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FSMULx\n"));
    586   1.1   deraadt 		if (type == FTYPE_EXT)
    587   1.1   deraadt 			return (NOTFPU);
    588   1.1   deraadt 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    589   1.1   deraadt 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    590   1.1   deraadt 		type++;	/* single to double, or double to quad */
    591   1.1   deraadt 		fp = fpu_mul(fe);
    592   1.1   deraadt 		break;
    593   1.1   deraadt 
    594   1.7       mrg #ifdef SUN4U
    595   1.7       mrg 	case FXTOS >> 2:
    596   1.7       mrg 	case FXTOD >> 2:
    597   1.7       mrg 	case FXTOQ >> 2:
    598   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FXTOx\n"));
    599   1.7       mrg 		type = FTYPE_LNG;
    600   1.7       mrg 		fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
    601   1.7       mrg 		type = opf & 3;	/* sneaky; depends on instruction encoding */
    602   1.7       mrg 		break;
    603   1.7       mrg 
    604   1.7       mrg 	case FTOX >> 2:
    605  1.14       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FTOX\n"));
    606   1.7       mrg 		fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
    607   1.7       mrg 		type = FTYPE_LNG;
    608  1.14       eeh 		/* Recalculate destination register */
    609  1.14       eeh 		rd = instr.i_opf.i_rd;
    610   1.8       eeh 		break;
    611  1.14       eeh 
    612   1.7       mrg #endif /* SUN4U */
    613  1.14       eeh 	case FTOI >> 2:
    614  1.14       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FTOI\n"));
    615  1.14       eeh 		fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
    616  1.14       eeh 		type = FTYPE_INT;
    617  1.14       eeh 		/* Recalculate destination register */
    618  1.14       eeh 		rd = instr.i_opf.i_rd;
    619  1.14       eeh 		break;
    620   1.7       mrg 
    621   1.1   deraadt 	case FTOS >> 2:
    622   1.1   deraadt 	case FTOD >> 2:
    623   1.7       mrg 	case FTOQ >> 2:
    624   1.8       eeh 		DPRINTF(FPE_INSN, ("fpu_execute: FTOx\n"));
    625   1.1   deraadt 		fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
    626  1.14       eeh 		/* Recalculate rd with correct type info. */
    627   1.1   deraadt 		type = opf & 3;	/* sneaky; depends on instruction encoding */
    628  1.14       eeh 		mask = 0x3 >> (3 - type);
    629  1.14       eeh 		rd = instr.i_opf.i_rd;
    630  1.14       eeh 		rd = (rd & ~mask) | ((rd & mask & 0x1) << 5);
    631   1.1   deraadt 		break;
    632   1.1   deraadt 	}
    633   1.1   deraadt 
    634   1.1   deraadt 	/*
    635   1.1   deraadt 	 * ALU operation is complete.  Collapse the result and then check
    636   1.1   deraadt 	 * for exceptions.  If we got any, and they are enabled, do not
    637   1.1   deraadt 	 * alter the destination register, just stop with an exception.
    638   1.1   deraadt 	 * Otherwise set new current exceptions and accrue.
    639   1.1   deraadt 	 */
    640   1.1   deraadt 	fpu_implode(fe, fp, type, space);
    641   1.1   deraadt 	cx = fe->fe_cx;
    642   1.1   deraadt 	fsr = fe->fe_fsr;
    643   1.1   deraadt 	if (cx != 0) {
    644   1.1   deraadt 		mask = (fsr >> FSR_TEM_SHIFT) & FSR_TEM_MASK;
    645   1.1   deraadt 		if (cx & mask) {
    646   1.1   deraadt 			/* not accrued??? */
    647   1.1   deraadt 			fs->fs_fsr = (fsr & ~FSR_FTT) |
    648   1.1   deraadt 			    (FSR_TT_IEEE << FSR_FTT_SHIFT) |
    649   1.1   deraadt 			    (cx_to_trapx[(cx & mask) - 1] << FSR_CX_SHIFT);
    650   1.1   deraadt 			return (FPE);
    651   1.1   deraadt 		}
    652   1.1   deraadt 		fsr |= (cx << FSR_CX_SHIFT) | (cx << FSR_AX_SHIFT);
    653   1.1   deraadt 	}
    654   1.1   deraadt 	fs->fs_fsr = fsr;
    655  1.14       eeh 	DPRINTF(FPE_REG, ("-> %c%d\n", (type == FTYPE_LNG) ? 'x' :
    656  1.14       eeh 		((type == FTYPE_INT) ? 'i' :
    657  1.14       eeh 			((type == FTYPE_SNG) ? 's' :
    658  1.14       eeh 				((type == FTYPE_DBL) ? 'd' :
    659  1.14       eeh 					((type == FTYPE_EXT) ? 'q' : '?')))),
    660  1.14       eeh 		rd));
    661   1.1   deraadt 	fs->fs_regs[rd] = space[0];
    662  1.10        pk 	if (type >= FTYPE_DBL || type == FTYPE_LNG) {
    663   1.1   deraadt 		fs->fs_regs[rd + 1] = space[1];
    664   1.1   deraadt 		if (type > FTYPE_DBL) {
    665   1.1   deraadt 			fs->fs_regs[rd + 2] = space[2];
    666   1.1   deraadt 			fs->fs_regs[rd + 3] = space[3];
    667   1.1   deraadt 		}
    668   1.1   deraadt 	}
    669   1.1   deraadt 	return (0);	/* success */
    670   1.1   deraadt }
    671