fpu.c revision 91174
1/*
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 *	This product includes software developed by the University of
12 *	California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 *    must display the following acknowledgement:
24 *	This product includes software developed by the University of
25 *	California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 *    may be used to endorse or promote products derived from this software
28 *    without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42/*-
43 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.  All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 *    notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 *    notice, this list of conditions and the following disclaimer in the
52 *    documentation and/or other materials provided with the distribution.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
61 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
63 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *
65 *	from: @(#)fpu.c	8.1 (Berkeley) 6/11/93
66 *	from: NetBSD: fpu.c,v 1.11 2000/12/06 01:47:50 mrg Exp
67 *
68 * $FreeBSD: head/lib/libc/sparc64/fpu/fpu.c 91174 2002-02-23 21:37:18Z tmm $
69 */
70
71#include <sys/param.h>
72
73#include "namespace.h"
74#include <errno.h>
75#include <unistd.h>
76#include <signal.h>
77#include <stdlib.h>
78#include "un-namespace.h"
79#include "libc_private.h"
80
81#include <machine/emul.h>
82#include <machine/fp.h>
83#include <machine/frame.h>
84#include <machine/fsr.h>
85#include <machine/instr.h>
86#include <machine/pcb.h>
87#include <machine/tstate.h>
88
89#include "../sys/__sparc_utrap_private.h"
90#include "fpu_emu.h"
91#include "fpu_extern.h"
92
93/*
94 * Translate current exceptions into `first' exception.  The
95 * bits go the wrong way for ffs() (0x10 is most important, etc).
96 * There are only 5, so do it the obvious way.
97 */
98#define	X1(x) x
99#define	X2(x) x,x
100#define	X4(x) x,x,x,x
101#define	X8(x) X4(x),X4(x)
102#define	X16(x) X8(x),X8(x)
103
104static char cx_to_trapx[] = {
105	X1(FSR_NX),
106	X2(FSR_DZ),
107	X4(FSR_UF),
108	X8(FSR_OF),
109	X16(FSR_NV)
110};
111
112#ifdef FPU_DEBUG
113#ifdef FPU_DEBUG_MASK
114int __fpe_debug = FPU_DEBUG_MASK;
115#else
116int __fpe_debug = 0;
117#endif
118#endif	/* FPU_DEBUG */
119
120static int __fpu_execute(struct utrapframe *, struct fpemu *, u_int32_t, u_long);
121static void utrap_write(char *);
122static void utrap_kill_self(int);
123
124/*
125 * System call wrappers usable in an utrap environment.
126 */
127static void
128utrap_write(char *str)
129{
130	int berrno;
131
132	berrno = errno;
133	__sys_write(STDERR_FILENO, str, strlen(str));
134	errno = berrno;
135}
136
137static void
138utrap_kill_self(sig)
139{
140	int berrno;
141
142	berrno = errno;
143	__sys_kill(__sys_getpid(), sig);
144	errno = berrno;
145}
146
147void
148__fpu_panic(char *msg)
149{
150
151	utrap_write(msg);
152	utrap_write("\n");
153	utrap_kill_self(SIGKILL);
154}
155
156/*
157 * Need to use an fpstate on the stack; we could switch, so we cannot safely
158 * modify the pcb one, it might get overwritten.
159 */
160void
161__fpu_exception(struct utrapframe *uf)
162{
163	struct fpemu fe;
164	u_long fsr, tstate;
165	u_int insn;
166	int rv;
167
168	fsr = uf->uf_fsr;
169
170	switch (FSR_GET_FTT(fsr)) {
171	case FSR_FTT_NONE:
172		utrap_write("lost FPU trap type\n");
173		return;
174	case FSR_FTT_IEEE:
175		goto fatal;
176	case FSR_FTT_SEQERR:
177		utrap_write("FPU sequence error\n");
178		goto fatal;
179	case FSR_FTT_HWERR:
180		utrap_write("FPU hardware error\n");
181		goto fatal;
182	case FSR_FTT_UNFIN:
183	case FSR_FTT_UNIMP:
184		break;
185	default:
186		utrap_write("unknown FPU error\n");
187		goto fatal;
188	}
189
190	fe.fe_fsr = fsr;
191	insn = *(u_int32_t *)uf->uf_pc;
192	if (IF_OP(insn) != IOP_MISC || (IF_F3_OP3(insn) != INS2_FPop1 &&
193	    IF_F3_OP3(insn) != INS2_FPop2))
194		__fpu_panic("bogus FP fault");
195	tstate = uf->uf_state;
196	rv = __fpu_execute(uf, &fe, insn, tstate);
197	if (rv != 0)
198		utrap_kill_self(rv);
199	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr));
200	return;
201fatal:
202	utrap_kill_self(SIGFPE);
203	return;
204}
205
206#ifdef FPU_DEBUG
207/*
208 * Dump a `fpn' structure.
209 */
210void
211__fpu_dumpfpn(struct fpn *fp)
212{
213	static char *class[] = {
214		"SNAN", "QNAN", "ZERO", "NUM", "INF"
215	};
216
217	printf("%s %c.%x %x %x %xE%d", class[fp->fp_class + 2],
218		fp->fp_sign ? '-' : ' ',
219		fp->fp_mant[0],	fp->fp_mant[1],
220		fp->fp_mant[2], fp->fp_mant[3],
221		fp->fp_exp);
222}
223#endif
224
225static u_long
226fetch_reg(struct utrapframe *uf, int reg)
227{
228	u_long offs;
229	struct frame *frm;
230
231	if (reg == IREG_G0)
232		return (0);
233	else if (reg < IREG_O0)	/* global */
234		return (uf->uf_global[reg]);
235	else if (reg < IREG_L0)	/* out */
236		return (uf->uf_out[reg - IREG_O0]);
237	else {			/* local, in */
238		/*
239		 * The in registers are immediately after the locals in
240		 * the frame.
241		 */
242		frm = (struct frame *)(uf->uf_out[6] + SPOFF);
243		return (frm->f_local[reg - IREG_L0]);
244	}
245	__fpu_panic("fetch_reg: bogus register");
246}
247
248static void
249__fpu_mov(struct fpemu *fe, int type, int rd, int rs1, int rs2)
250{
251	int i;
252
253	i = 1 << type;
254	__fpu_setreg(rd++, rs1);
255	while (--i)
256		__fpu_setreg(rd++, __fpu_getreg(++rs2));
257}
258
259static __inline void
260__fpu_ccmov(struct fpemu *fe, int type, int rd, int rs1, int rs2,
261    u_int32_t insn, int fcc)
262{
263
264	if (IF_F4_COND(insn) == fcc)
265		__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
266}
267
268static int
269__fpu_cmpck(struct fpemu *fe)
270{
271	int cx, fsr;
272
273	/*
274	 * The only possible exception here is NV; catch it
275	 * early and get out, as there is no result register.
276	 */
277	cx = fe->fe_cx;
278	fsr = fe->fe_fsr | (cx << FSR_CEXC_SHIFT);
279	if (cx != 0) {
280		if (fsr & (FSR_NV << FSR_TEM_SHIFT)) {
281			fe->fe_fsr = (fsr & ~FSR_FTT_MASK) |
282			    FSR_FTT(FSR_FTT_IEEE);
283			return (SIGFPE);
284		}
285		fsr |= FSR_NV << FSR_AEXC_SHIFT;
286	}
287	fe->fe_fsr = fsr;
288	return (0);
289}
290
291static int opmask[] = {0, 0, 1, 3};
292
293/*
294 * Helper for forming the below case statements. Build only the op3 and opf
295 * field of the instruction, these are the only that need to match.
296 */
297#define	FOP(op3, opf) \
298	((op3) << IF_F3_OP3_SHIFT | (opf) << IF_F3_OPF_SHIFT)
299
300/*
301 * Execute an FPU instruction (one that runs entirely in the FPU; not
302 * FBfcc or STF, for instance).  On return, fe->fe_fs->fs_fsr will be
303 * modified to reflect the setting the hardware would have left.
304 *
305 * Note that we do not catch all illegal opcodes, so you can, for instance,
306 * multiply two integers this way.
307 */
308static int
309__fpu_execute(struct utrapframe *uf, struct fpemu *fe, u_int32_t insn, u_long tstate)
310{
311	struct fpn *fp;
312	int opf, rs1, rs2, rd, type, mask, cx, cond;
313	u_long reg, fsr;
314	u_int space[4];
315
316	/*
317	 * `Decode' and execute instruction.  Start with no exceptions.
318	 * The type of any opf opcode is in the bottom two bits, so we
319	 * squish them out here.
320	 */
321	opf = insn & (IF_MASK(IF_F3_OP3_SHIFT, IF_F3_OP3_BITS) |
322	    IF_MASK(IF_F3_OPF_SHIFT + 2, IF_F3_OPF_BITS - 2));
323	type = IF_F3_OPF(insn) & 3;
324	mask = opmask[type];
325	rs1 = IF_F3_RS1(insn) & ~mask;
326	rs2 = IF_F3_RS2(insn) & ~mask;
327	rd = IF_F3_RD(insn) & ~mask;
328	cond = 0;
329#ifdef notdef
330	if ((rs1 | rs2 | rd) & mask)
331		return (SIGILL);
332#endif
333	fsr = fe->fe_fsr;
334	fe->fe_fsr &= ~FSR_CEXC_MASK;
335	fe->fe_cx = 0;
336	switch (opf) {
337	case FOP(INS2_FPop2, INSFP2_FMOV_CC(IFCC_FCC(0))):
338		__fpu_ccmov(fe, type, rd, __fpu_getreg(rs2), rs2, insn,
339		    FSR_GET_FCC0(fsr));
340		return (0);
341	case FOP(INS2_FPop2, INSFP2_FMOV_CC(IFCC_FCC(1))):
342		__fpu_ccmov(fe, type, rd, __fpu_getreg(rs2), rs2, insn,
343		    FSR_GET_FCC1(fsr));
344		return (0);
345	case FOP(INS2_FPop2, INSFP2_FMOV_CC(IFCC_FCC(2))):
346		__fpu_ccmov(fe, type, rd, __fpu_getreg(rs2), rs2, insn,
347		    FSR_GET_FCC2(fsr));
348		return (0);
349	case FOP(INS2_FPop2, INSFP2_FMOV_CC(IFCC_FCC(3))):
350		__fpu_ccmov(fe, type, rd, __fpu_getreg(rs2), rs2, insn,
351		    FSR_GET_FCC3(fsr));
352		return (0);
353	case FOP(INS2_FPop2, INSFP2_FMOV_CC(IFCC_ICC)):
354		__fpu_ccmov(fe, type, rd, __fpu_getreg(rs2), rs2, insn,
355		    (tstate & TSTATE_ICC_MASK) >> TSTATE_ICC_SHIFT);
356		return (0);
357	case FOP(INS2_FPop2, INSFP2_FMOV_CC(IFCC_XCC)):
358		__fpu_ccmov(fe, type, rd, __fpu_getreg(rs2), rs2, insn,
359		    (tstate & TSTATE_XCC_MASK) >> (TSTATE_XCC_SHIFT));
360		return (0);
361	case FOP(INS2_FPop2, INSFP2_FMOV_RC(IRCOND_Z)):
362		reg = fetch_reg(uf, IF_F4_RS1(insn));
363		if (reg == 0)
364			__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
365		return (0);
366	case FOP(INS2_FPop2, INSFP2_FMOV_RC(IRCOND_LEZ)):
367		reg = fetch_reg(uf, IF_F4_RS1(insn));
368		if (reg <= 0)
369			__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
370		return (0);
371	case FOP(INS2_FPop2, INSFP2_FMOV_RC(IRCOND_LZ)):
372		reg = fetch_reg(uf, IF_F4_RS1(insn));
373		if (reg < 0)
374			__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
375		return (0);
376	case FOP(INS2_FPop2, INSFP2_FMOV_RC(IRCOND_NZ)):
377		reg = fetch_reg(uf, IF_F4_RS1(insn));
378		if (reg != 0)
379			__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
380		return (0);
381	case FOP(INS2_FPop2, INSFP2_FMOV_RC(IRCOND_GZ)):
382		reg = fetch_reg(uf, IF_F4_RS1(insn));
383		if (reg > 0)
384			__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
385		return (0);
386	case FOP(INS2_FPop2, INSFP2_FMOV_RC(IRCOND_GEZ)):
387		reg = fetch_reg(uf, IF_F4_RS1(insn));
388		if (reg >= 0)
389			__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
390		return (0);
391	case FOP(INS2_FPop2, INSFP2_FCMP):
392		__fpu_explode(fe, &fe->fe_f1, type, rs1);
393		__fpu_explode(fe, &fe->fe_f2, type, rs2);
394		__fpu_compare(fe, 0);
395		return (__fpu_cmpck(fe));
396	case FOP(INS2_FPop2, INSFP2_FCMPE):
397		__fpu_explode(fe, &fe->fe_f1, type, rs1);
398		__fpu_explode(fe, &fe->fe_f2, type, rs2);
399		__fpu_compare(fe, 1);
400		return (__fpu_cmpck(fe));
401	case FOP(INS2_FPop1, INSFP1_FMOV):	/* these should all be pretty obvious */
402		__fpu_mov(fe, type, rd, __fpu_getreg(rs2), rs2);
403		return (0);
404	case FOP(INS2_FPop1, INSFP1_FNEG):
405		__fpu_mov(fe, type, rd, __fpu_getreg(rs2) ^ (1 << 31), rs2);
406		return (0);
407	case FOP(INS2_FPop1, INSFP1_FABS):
408		__fpu_mov(fe, type, rd, __fpu_getreg(rs2) & ~(1 << 31), rs2);
409		return (0);
410	case FOP(INS2_FPop1, INSFP1_FSQRT):
411		__fpu_explode(fe, &fe->fe_f1, type, rs2);
412		fp = __fpu_sqrt(fe);
413		break;
414	case FOP(INS2_FPop1, INSFP1_FADD):
415		__fpu_explode(fe, &fe->fe_f1, type, rs1);
416		__fpu_explode(fe, &fe->fe_f2, type, rs2);
417		fp = __fpu_add(fe);
418		break;
419	case FOP(INS2_FPop1, INSFP1_FSUB):
420		__fpu_explode(fe, &fe->fe_f1, type, rs1);
421		__fpu_explode(fe, &fe->fe_f2, type, rs2);
422		fp = __fpu_sub(fe);
423		break;
424	case FOP(INS2_FPop1, INSFP1_FMUL):
425		__fpu_explode(fe, &fe->fe_f1, type, rs1);
426		__fpu_explode(fe, &fe->fe_f2, type, rs2);
427		fp = __fpu_mul(fe);
428		break;
429	case FOP(INS2_FPop1, INSFP1_FDIV):
430		__fpu_explode(fe, &fe->fe_f1, type, rs1);
431		__fpu_explode(fe, &fe->fe_f2, type, rs2);
432		fp = __fpu_div(fe);
433		break;
434	case FOP(INS2_FPop1, INSFP1_FsMULd):
435	case FOP(INS2_FPop1, INSFP1_FdMULq):
436		if (type == FTYPE_EXT)
437			return (SIGILL);
438		__fpu_explode(fe, &fe->fe_f1, type, rs1);
439		__fpu_explode(fe, &fe->fe_f2, type, rs2);
440		type++;	/* single to double, or double to quad */
441		/*
442		 * Recalculate rd (the old type applied for the source regs
443		 * only, the target one has a different size).
444		 */
445		mask = opmask[type];
446		rd = IF_F3_RD(insn) & ~mask;
447		fp = __fpu_mul(fe);
448		break;
449	case FOP(INS2_FPop1, INSFP1_FxTOs):
450	case FOP(INS2_FPop1, INSFP1_FxTOd):
451	case FOP(INS2_FPop1, INSFP1_FxTOq):
452		type = FTYPE_LNG;
453		__fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
454		/* sneaky; depends on instruction encoding */
455		type = (IF_F3_OPF(insn) >> 2) & 3;
456		mask = opmask[type];
457		rd = IF_F3_RD(insn) & ~mask;
458		break;
459	case FOP(INS2_FPop1, INSFP1_FTOx):
460		__fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
461		type = FTYPE_LNG;
462		mask = 1;	/* needs 2 registers */
463		rd = IF_F3_RD(insn) & ~mask;
464		break;
465	case FOP(INS2_FPop1, INSFP1_FTOs):
466	case FOP(INS2_FPop1, INSFP1_FTOd):
467	case FOP(INS2_FPop1, INSFP1_FTOq):
468	case FOP(INS2_FPop1, INSFP1_FTOi):
469		__fpu_explode(fe, fp = &fe->fe_f1, type, rs2);
470		/* sneaky; depends on instruction encoding */
471		type = (IF_F3_OPF(insn) >> 2) & 3;
472		mask = opmask[type];
473		rd = IF_F3_RD(insn) & ~mask;
474		break;
475	default:
476		return (SIGILL);
477	}
478
479	/*
480	 * ALU operation is complete.  Collapse the result and then check
481	 * for exceptions.  If we got any, and they are enabled, do not
482	 * alter the destination register, just stop with an exception.
483	 * Otherwise set new current exceptions and accrue.
484	 */
485	__fpu_implode(fe, fp, type, space);
486	cx = fe->fe_cx;
487	if (cx != 0) {
488		mask = (fsr >> FSR_TEM_SHIFT) & FSR_TEM_MASK;
489		if (cx & mask) {
490			/* not accrued??? */
491			fsr = (fsr & ~FSR_FTT_MASK) |
492			    FSR_FTT(FSR_FTT_IEEE) |
493			    FSR_CEXC(cx_to_trapx[(cx & mask) - 1]);
494			return (SIGFPE);
495		}
496		fsr |= (cx << FSR_CEXC_SHIFT) | (cx << FSR_AEXC_SHIFT);
497	}
498	fe->fe_fsr = fsr;
499	__fpu_setreg(rd, space[0]);
500	if (type >= FTYPE_DBL || type == FTYPE_LNG) {
501		__fpu_setreg(rd + 1, space[1]);
502		if (type > FTYPE_DBL) {
503			__fpu_setreg(rd + 2, space[2]);
504			__fpu_setreg(rd + 3, space[3]);
505		}
506	}
507	return (0);	/* success */
508}
509