Home | History | Annotate | Line # | Download | only in fpu
fpu.c revision 1.6
      1 /*	$NetBSD: fpu.c,v 1.6 1997/07/29 10:09:51 fair Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)fpu.c	8.1 (Berkeley) 6/11/93
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/proc.h>
     49 #include <sys/signal.h>
     50 #include <sys/systm.h>
     51 #include <sys/syslog.h>
     52 #include <sys/signalvar.h>
     53 
     54 #include <machine/instr.h>
     55 #include <machine/reg.h>
     56 
     57 #include <sparc/fpu/fpu_emu.h>
     58 #include <sparc/fpu/fpu_extern.h>
     59 
     60 /*
     61  * fpu_execute returns the following error numbers (0 = no error):
     62  */
     63 #define	FPE		1	/* take a floating point exception */
     64 #define	NOTFPU		2	/* not an FPU instruction */
     65 
     66 /*
     67  * Translate current exceptions into `first' exception.  The
     68  * bits go the wrong way for ffs() (0x10 is most important, etc).
     69  * There are only 5, so do it the obvious way.
     70  */
     71 #define	X1(x) x
     72 #define	X2(x) x,x
     73 #define	X4(x) x,x,x,x
     74 #define	X8(x) X4(x),X4(x)
     75 #define	X16(x) X8(x),X8(x)
     76 
     77 static char cx_to_trapx[] = {
     78 	X1(FSR_NX),
     79 	X2(FSR_DZ),
     80 	X4(FSR_UF),
     81 	X8(FSR_OF),
     82 	X16(FSR_NV)
     83 };
     84 static u_char fpu_codes[] = {
     85 	X1(FPE_FLTINEX_TRAP),
     86 	X2(FPE_FLTDIV_TRAP),
     87 	X4(FPE_FLTUND_TRAP),
     88 	X8(FPE_FLTOVF_TRAP),
     89 	X16(FPE_FLTOPERR_TRAP)
     90 };
     91 
     92 /*
     93  * The FPU gave us an exception.  Clean up the mess.  Note that the
     94  * fp queue can only have FPops in it, never load/store FP registers
     95  * nor FBfcc instructions.  Experiments with `crashme' prove that
     96  * unknown FPops do enter the queue, however.
     97  */
     98 void
     99 fpu_cleanup(p, fs)
    100 	register struct proc *p;
    101 	register struct fpstate *fs;
    102 {
    103 	register int i, fsr = fs->fs_fsr, error;
    104 	union instr instr;
    105 	struct fpemu fe;
    106 
    107 	switch ((fsr >> FSR_FTT_SHIFT) & FSR_FTT_MASK) {
    108 
    109 	case FSR_TT_NONE:
    110 		panic("fpu_cleanup 1");	/* ??? */
    111 		break;
    112 
    113 	case FSR_TT_IEEE:
    114 		/* XXX missing trap address! */
    115 		if ((i = fsr & FSR_CX) == 0)
    116 			panic("fpu ieee trap, but no exception");
    117 		trapsignal(p, SIGFPE, fpu_codes[i - 1]);
    118 		break;		/* XXX should return, but queue remains */
    119 
    120 	case FSR_TT_UNFIN:
    121 	case FSR_TT_UNIMP:
    122 		if (fs->fs_qsize == 0)
    123 			panic("fpu_cleanup 2");
    124 		break;
    125 
    126 	case FSR_TT_SEQ:
    127 		panic("fpu sequence error");
    128 		/* NOTREACHED */
    129 
    130 	case FSR_TT_HWERR:
    131 		log(LOG_ERR, "fpu hardware error (%s[%d])\n",
    132 		    p->p_comm, p->p_pid);
    133 		uprintf("%s[%d]: fpu hardware error\n", p->p_comm, p->p_pid);
    134 		trapsignal(p, SIGFPE, -1);	/* ??? */
    135 		goto out;
    136 
    137 	default:
    138 		printf("fsr=0x%x\n", fsr);
    139 		panic("fpu error");
    140 	}
    141 
    142 	/* emulate the instructions left in the queue */
    143 	fe.fe_fpstate = fs;
    144 	for (i = 0; i < fs->fs_qsize; i++) {
    145 		instr.i_int = fs->fs_queue[i].fq_instr;
    146 		if (instr.i_any.i_op != IOP_reg ||
    147 		    (instr.i_op3.i_op3 != IOP3_FPop1 &&
    148 		     instr.i_op3.i_op3 != IOP3_FPop2))
    149 			panic("bogus fpu queue");
    150 		error = fpu_execute(&fe, instr);
    151 		switch (error) {
    152 
    153 		case 0:
    154 			continue;
    155 
    156 		case FPE:
    157 			trapsignal(p, SIGFPE,
    158 			    fpu_codes[(fs->fs_fsr & FSR_CX) - 1]);
    159 			break;
    160 
    161 		case NOTFPU:
    162 			trapsignal(p, SIGILL, 0);	/* ??? code?  */
    163 			break;
    164 
    165 		default:
    166 			panic("fpu_cleanup 3");
    167 			/* NOTREACHED */
    168 		}
    169 		/* XXX should stop here, but queue remains */
    170 	}
    171 out:
    172 	fs->fs_qsize = 0;
    173 }
    174 
    175 #ifdef notyet
    176 /*
    177  * If we have no FPU at all (are there any machines like this out
    178  * there!?) we have to emulate each instruction, and we need a pointer
    179  * to the trapframe so that we can step over them and do FBfcc's.
    180  * We know the `queue' is empty, though; we just want to emulate
    181  * the instruction at tf->tf_pc.
    182  */
    183 fpu_emulate(p, tf, fs)
    184 	struct proc *p;
    185 	register struct trapframe *tf;
    186 	register struct fpstate *fs;
    187 {
    188 
    189 	do {
    190 		fetch instr from pc
    191 		decode
    192 		if (integer instr) {
    193 			/*
    194 			 * We do this here, rather than earlier, to avoid
    195 			 * losing even more badly than usual.
    196 			 */
    197 			if (p->p_addr->u_pcb.pcb_uw) {
    198 				write_user_windows();
    199 				if (rwindow_save(p))
    200 					sigexit(p, SIGILL);
    201 			}
    202 			if (loadstore) {
    203 				do_it;
    204 				pc = npc, npc += 4
    205 			} else if (fbfcc) {
    206 				do_annul_stuff;
    207 			} else
    208 				return;
    209 		} else if (fpu instr) {
    210 			fe.fe_fsr = fs->fs_fsr &= ~FSR_CX;
    211 			error = fpu_execute(&fe, fs, instr);
    212 			switch (error) {
    213 				etc;
    214 			}
    215 		} else
    216 			return;
    217 		if (want to reschedule)
    218 			return;
    219 	} while (error == 0);
    220 }
    221 #endif
    222 
    223 /*
    224  * Execute an FPU instruction (one that runs entirely in the FPU; not
    225  * FBfcc or STF, for instance).  On return, fe->fe_fs->fs_fsr will be
    226  * modified to reflect the setting the hardware would have left.
    227  *
    228  * Note that we do not catch all illegal opcodes, so you can, for instance,
    229  * multiply two integers this way.
    230  */
    231 int
    232 fpu_execute(fe, instr)
    233 	register struct fpemu *fe;
    234 	union instr instr;
    235 {
    236 	register struct fpn *fp;
    237 	register int opf, rs1, rs2, rd, type, mask, fsr, cx;
    238 	register struct fpstate *fs;
    239 	u_int space[4];
    240 
    241 	/*
    242 	 * `Decode' and execute instruction.  Start with no exceptions.
    243 	 * The type of any i_opf opcode is in the bottom two bits, so we
    244 	 * squish them out here.
    245 	 */
    246 	opf = instr.i_opf.i_opf;
    247 	type = opf & 3;
    248 	mask = "\0\0\1\3"[type];
    249 	rs1 = instr.i_opf.i_rs1 & ~mask;
    250 	rs2 = instr.i_opf.i_rs2 & ~mask;
    251 	rd = instr.i_opf.i_rd & ~mask;
    252 #ifdef notdef
    253 	if ((rs1 | rs2 | rd) & mask)
    254 		return (BADREG);
    255 #endif
    256 	fs = fe->fe_fpstate;
    257 	fe->fe_fsr = fs->fs_fsr & ~FSR_CX;
    258 	fe->fe_cx = 0;
    259 	switch (opf >>= 2) {
    260 
    261 	default:
    262 		return (NOTFPU);
    263 
    264 	case FMOV >> 2:		/* these should all be pretty obvious */
    265 		rs1 = fs->fs_regs[rs2];
    266 		goto mov;
    267 
    268 	case FNEG >> 2:
    269 		rs1 = fs->fs_regs[rs2] ^ (1 << 31);
    270 		goto mov;
    271 
    272 	case FABS >> 2:
    273 		rs1 = fs->fs_regs[rs2] & ~(1 << 31);
    274 	mov:
    275 		fs->fs_regs[rd] = rs1;
    276 		fs->fs_fsr = fe->fe_fsr;
    277 		return (0);	/* success */
    278 
    279 	case FSQRT >> 2:
    280 		fpu_explode(fe, &fe->fe_f1, type, rs2);
    281 		fp = fpu_sqrt(fe);
    282 		break;
    283 
    284 	case FADD >> 2:
    285 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    286 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    287 		fp = fpu_add(fe);
    288 		break;
    289 
    290 	case FSUB >> 2:
    291 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    292 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    293 		fp = fpu_sub(fe);
    294 		break;
    295 
    296 	case FMUL >> 2:
    297 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    298 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    299 		fp = fpu_mul(fe);
    300 		break;
    301 
    302 	case FDIV >> 2:
    303 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    304 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    305 		fp = fpu_div(fe);
    306 		break;
    307 
    308 	case FCMP >> 2:
    309 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    310 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    311 		fpu_compare(fe, 0);
    312 		goto cmpdone;
    313 
    314 	case FCMPE >> 2:
    315 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    316 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    317 		fpu_compare(fe, 1);
    318 	cmpdone:
    319 		/*
    320 		 * The only possible exception here is NV; catch it
    321 		 * early and get out, as there is no result register.
    322 		 */
    323 		cx = fe->fe_cx;
    324 		fsr = fe->fe_fsr | (cx << FSR_CX_SHIFT);
    325 		if (cx != 0) {
    326 			if (fsr & (FSR_NV << FSR_TEM_SHIFT)) {
    327 				fs->fs_fsr = (fsr & ~FSR_FTT) |
    328 				    (FSR_TT_IEEE << FSR_FTT_SHIFT);
    329 				return (FPE);
    330 			}
    331 			fsr |= FSR_NV << FSR_AX_SHIFT;
    332 		}
    333 		fs->fs_fsr = fsr;
    334 		return (0);
    335 
    336 	case FSMULD >> 2:
    337 	case FDMULX >> 2:
    338 		if (type == FTYPE_EXT)
    339 			return (NOTFPU);
    340 		fpu_explode(fe, &fe->fe_f1, type, rs1);
    341 		fpu_explode(fe, &fe->fe_f2, type, rs2);
    342 		type++;	/* single to double, or double to quad */
    343 		fp = fpu_mul(fe);
    344 		break;
    345 
    346 	case FTOS >> 2:
    347 	case FTOD >> 2:
    348 	case FTOX >> 2:
    349 	case FTOI >> 2:
    350 		fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
    351 		type = opf & 3;	/* sneaky; depends on instruction encoding */
    352 		break;
    353 	}
    354 
    355 	/*
    356 	 * ALU operation is complete.  Collapse the result and then check
    357 	 * for exceptions.  If we got any, and they are enabled, do not
    358 	 * alter the destination register, just stop with an exception.
    359 	 * Otherwise set new current exceptions and accrue.
    360 	 */
    361 	fpu_implode(fe, fp, type, space);
    362 	cx = fe->fe_cx;
    363 	fsr = fe->fe_fsr;
    364 	if (cx != 0) {
    365 		mask = (fsr >> FSR_TEM_SHIFT) & FSR_TEM_MASK;
    366 		if (cx & mask) {
    367 			/* not accrued??? */
    368 			fs->fs_fsr = (fsr & ~FSR_FTT) |
    369 			    (FSR_TT_IEEE << FSR_FTT_SHIFT) |
    370 			    (cx_to_trapx[(cx & mask) - 1] << FSR_CX_SHIFT);
    371 			return (FPE);
    372 		}
    373 		fsr |= (cx << FSR_CX_SHIFT) | (cx << FSR_AX_SHIFT);
    374 	}
    375 	fs->fs_fsr = fsr;
    376 	fs->fs_regs[rd] = space[0];
    377 	if (type >= FTYPE_DBL) {
    378 		fs->fs_regs[rd + 1] = space[1];
    379 		if (type > FTYPE_DBL) {
    380 			fs->fs_regs[rd + 2] = space[2];
    381 			fs->fs_regs[rd + 3] = space[3];
    382 		}
    383 	}
    384 	return (0);	/* success */
    385 }
    386