xref: /onnv-gate/usr/src/uts/sparc/fpu/fpu_simulator.c (revision 7:420a1f4ae7e6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /* Main procedures for sparc FPU simulator. */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/fpu/fpu_simulator.h>
320Sstevel@tonic-gate #include <sys/fpu/globals.h>
330Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/siginfo.h>
370Sstevel@tonic-gate #include <sys/thread.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/atomic.h>
410Sstevel@tonic-gate #include <sys/privregs.h>
420Sstevel@tonic-gate #include <sys/vis_simulator.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	FPUINFO_KSTAT(opcode)	{					\
450Sstevel@tonic-gate 	extern void __dtrace_probe___fpuinfo_##opcode(uint64_t *);	\
460Sstevel@tonic-gate 	uint64_t *stataddr = &fpuinfo.opcode.value.ui64;		\
470Sstevel@tonic-gate 	__dtrace_probe___fpuinfo_##opcode(stataddr);			\
480Sstevel@tonic-gate 	atomic_add_64(&fpuinfo.opcode.value.ui64, 1);			\
490Sstevel@tonic-gate }
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	FPUINFO_KSTAT_PREC(prec, kstat_s, kstat_d, kstat_q)		\
520Sstevel@tonic-gate 	if (prec < 2) {							\
530Sstevel@tonic-gate 		FPUINFO_KSTAT(kstat_s);					\
540Sstevel@tonic-gate 	} else if (prec == 2) {						\
550Sstevel@tonic-gate 		FPUINFO_KSTAT(kstat_d);					\
560Sstevel@tonic-gate 	} else {							\
570Sstevel@tonic-gate 		FPUINFO_KSTAT(kstat_q);					\
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * FPU simulator global kstat data
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate struct fpuinfo_kstat fpuinfo = {
640Sstevel@tonic-gate 	{ "fpu_sim_fmovs",		KSTAT_DATA_UINT64},
650Sstevel@tonic-gate 	{ "fpu_sim_fmovd",		KSTAT_DATA_UINT64},
660Sstevel@tonic-gate 	{ "fpu_sim_fmovq",		KSTAT_DATA_UINT64},
670Sstevel@tonic-gate 	{ "fpu_sim_fnegs",		KSTAT_DATA_UINT64},
680Sstevel@tonic-gate 	{ "fpu_sim_fnegd",		KSTAT_DATA_UINT64},
690Sstevel@tonic-gate 	{ "fpu_sim_fnegq",		KSTAT_DATA_UINT64},
700Sstevel@tonic-gate 	{ "fpu_sim_fabss",		KSTAT_DATA_UINT64},
710Sstevel@tonic-gate 	{ "fpu_sim_fabsd",		KSTAT_DATA_UINT64},
720Sstevel@tonic-gate 	{ "fpu_sim_fabsq",		KSTAT_DATA_UINT64},
730Sstevel@tonic-gate 	{ "fpu_sim_fsqrts",		KSTAT_DATA_UINT64},
740Sstevel@tonic-gate 	{ "fpu_sim_fsqrtd",		KSTAT_DATA_UINT64},
750Sstevel@tonic-gate 	{ "fpu_sim_fsqrtq",		KSTAT_DATA_UINT64},
760Sstevel@tonic-gate 	{ "fpu_sim_fadds",		KSTAT_DATA_UINT64},
770Sstevel@tonic-gate 	{ "fpu_sim_faddd",		KSTAT_DATA_UINT64},
780Sstevel@tonic-gate 	{ "fpu_sim_faddq",		KSTAT_DATA_UINT64},
790Sstevel@tonic-gate 	{ "fpu_sim_fsubs",		KSTAT_DATA_UINT64},
800Sstevel@tonic-gate 	{ "fpu_sim_fsubd",		KSTAT_DATA_UINT64},
810Sstevel@tonic-gate 	{ "fpu_sim_fsubq",		KSTAT_DATA_UINT64},
820Sstevel@tonic-gate 	{ "fpu_sim_fmuls",		KSTAT_DATA_UINT64},
830Sstevel@tonic-gate 	{ "fpu_sim_fmuld",		KSTAT_DATA_UINT64},
840Sstevel@tonic-gate 	{ "fpu_sim_fmulq",		KSTAT_DATA_UINT64},
850Sstevel@tonic-gate 	{ "fpu_sim_fdivs",		KSTAT_DATA_UINT64},
860Sstevel@tonic-gate 	{ "fpu_sim_fdivd",		KSTAT_DATA_UINT64},
870Sstevel@tonic-gate 	{ "fpu_sim_fdivq",		KSTAT_DATA_UINT64},
880Sstevel@tonic-gate 	{ "fpu_sim_fcmps",		KSTAT_DATA_UINT64},
890Sstevel@tonic-gate 	{ "fpu_sim_fcmpd",		KSTAT_DATA_UINT64},
900Sstevel@tonic-gate 	{ "fpu_sim_fcmpq",		KSTAT_DATA_UINT64},
910Sstevel@tonic-gate 	{ "fpu_sim_fcmpes",		KSTAT_DATA_UINT64},
920Sstevel@tonic-gate 	{ "fpu_sim_fcmped",		KSTAT_DATA_UINT64},
930Sstevel@tonic-gate 	{ "fpu_sim_fcmpeq",		KSTAT_DATA_UINT64},
940Sstevel@tonic-gate 	{ "fpu_sim_fsmuld",		KSTAT_DATA_UINT64},
950Sstevel@tonic-gate 	{ "fpu_sim_fdmulx",		KSTAT_DATA_UINT64},
960Sstevel@tonic-gate 	{ "fpu_sim_fstox",		KSTAT_DATA_UINT64},
970Sstevel@tonic-gate 	{ "fpu_sim_fdtox",		KSTAT_DATA_UINT64},
980Sstevel@tonic-gate 	{ "fpu_sim_fqtox",		KSTAT_DATA_UINT64},
990Sstevel@tonic-gate 	{ "fpu_sim_fxtos",		KSTAT_DATA_UINT64},
1000Sstevel@tonic-gate 	{ "fpu_sim_fxtod",		KSTAT_DATA_UINT64},
1010Sstevel@tonic-gate 	{ "fpu_sim_fxtoq",		KSTAT_DATA_UINT64},
1020Sstevel@tonic-gate 	{ "fpu_sim_fitos",		KSTAT_DATA_UINT64},
1030Sstevel@tonic-gate 	{ "fpu_sim_fitod",		KSTAT_DATA_UINT64},
1040Sstevel@tonic-gate 	{ "fpu_sim_fitoq",		KSTAT_DATA_UINT64},
1050Sstevel@tonic-gate 	{ "fpu_sim_fstoi",		KSTAT_DATA_UINT64},
1060Sstevel@tonic-gate 	{ "fpu_sim_fdtoi",		KSTAT_DATA_UINT64},
1070Sstevel@tonic-gate 	{ "fpu_sim_fqtoi",		KSTAT_DATA_UINT64},
1080Sstevel@tonic-gate 	{ "fpu_sim_fmovcc",		KSTAT_DATA_UINT64},
1090Sstevel@tonic-gate 	{ "fpu_sim_fmovr",		KSTAT_DATA_UINT64},
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate struct visinfo_kstat visinfo = {
1130Sstevel@tonic-gate 	{ "vis_edge8",		KSTAT_DATA_UINT64},
1140Sstevel@tonic-gate 	{ "vis_edge8n",		KSTAT_DATA_UINT64},
1150Sstevel@tonic-gate 	{ "vis_edge8l",		KSTAT_DATA_UINT64},
1160Sstevel@tonic-gate 	{ "vis_edge8ln",	KSTAT_DATA_UINT64},
1170Sstevel@tonic-gate 	{ "vis_edge16",		KSTAT_DATA_UINT64},
1180Sstevel@tonic-gate 	{ "vis_edge16n",	KSTAT_DATA_UINT64},
1190Sstevel@tonic-gate 	{ "vis_edge16l",	KSTAT_DATA_UINT64},
1200Sstevel@tonic-gate 	{ "vis_edge16ln",	KSTAT_DATA_UINT64},
1210Sstevel@tonic-gate 	{ "vis_edge32",		KSTAT_DATA_UINT64},
1220Sstevel@tonic-gate 	{ "vis_edge32n",	KSTAT_DATA_UINT64},
1230Sstevel@tonic-gate 	{ "vis_edge32l",	KSTAT_DATA_UINT64},
1240Sstevel@tonic-gate 	{ "vis_edge32ln",	KSTAT_DATA_UINT64},
1250Sstevel@tonic-gate 	{ "vis_array8",		KSTAT_DATA_UINT64},
1260Sstevel@tonic-gate 	{ "vis_array16",	KSTAT_DATA_UINT64},
1270Sstevel@tonic-gate 	{ "vis_array32",	KSTAT_DATA_UINT64},
1280Sstevel@tonic-gate 	{ "vis_bmask",		KSTAT_DATA_UINT64},
1290Sstevel@tonic-gate 	{ "vis_fcmple16",	KSTAT_DATA_UINT64},
1300Sstevel@tonic-gate 	{ "vis_fcmpne16",	KSTAT_DATA_UINT64},
1310Sstevel@tonic-gate 	{ "vis_fcmpgt16",	KSTAT_DATA_UINT64},
1320Sstevel@tonic-gate 	{ "vis_fcmpeq16",	KSTAT_DATA_UINT64},
1330Sstevel@tonic-gate 	{ "vis_fcmple32",	KSTAT_DATA_UINT64},
1340Sstevel@tonic-gate 	{ "vis_fcmpne32",	KSTAT_DATA_UINT64},
1350Sstevel@tonic-gate 	{ "vis_fcmpgt32",	KSTAT_DATA_UINT64},
1360Sstevel@tonic-gate 	{ "vis_fcmpeq32",	KSTAT_DATA_UINT64},
1370Sstevel@tonic-gate 	{ "vis_fmul8x16",	KSTAT_DATA_UINT64},
1380Sstevel@tonic-gate 	{ "vis_fmul8x16au",	KSTAT_DATA_UINT64},
1390Sstevel@tonic-gate 	{ "vis_fmul8x16al",	KSTAT_DATA_UINT64},
1400Sstevel@tonic-gate 	{ "vis_fmul8sux16",	KSTAT_DATA_UINT64},
1410Sstevel@tonic-gate 	{ "vis_fmul8ulx16",	KSTAT_DATA_UINT64},
1420Sstevel@tonic-gate 	{ "vis_fmuld8sux16",	KSTAT_DATA_UINT64},
1430Sstevel@tonic-gate 	{ "vis_fmuld8ulx16",	KSTAT_DATA_UINT64},
1440Sstevel@tonic-gate 	{ "vis_fpack16",	KSTAT_DATA_UINT64},
1450Sstevel@tonic-gate 	{ "vis_fpack32",	KSTAT_DATA_UINT64},
1460Sstevel@tonic-gate 	{ "vis_fpackfix",	KSTAT_DATA_UINT64},
1470Sstevel@tonic-gate 	{ "vis_fexpand",	KSTAT_DATA_UINT64},
1480Sstevel@tonic-gate 	{ "vis_fpmerge",	KSTAT_DATA_UINT64},
1490Sstevel@tonic-gate 	{ "vis_pdist",		KSTAT_DATA_UINT64},
1500Sstevel@tonic-gate 	{ "vis_bshuffle",	KSTAT_DATA_UINT64},
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /* PUBLIC FUNCTIONS */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int fp_notp = 1;	/* fp checking not a problem */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /* ARGSUSED */
1590Sstevel@tonic-gate static enum ftt_type
1600Sstevel@tonic-gate _fp_fpu_simulator(
1610Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to fpu simulator data */
1620Sstevel@tonic-gate 	fp_inst_type	inst,	/* FPU instruction to simulate. */
1630Sstevel@tonic-gate 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write. */
1640Sstevel@tonic-gate 	uint64_t	gsr)	/* Image of GSR to read */
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	unpacked	us1, us2, ud;	/* Unpacked operands and result. */
1670Sstevel@tonic-gate 	uint32_t	nrs1, nrs2, nrd; /* Register number fields. */
1680Sstevel@tonic-gate 	uint32_t	usr, andexcep;
1690Sstevel@tonic-gate 	fsr_type	fsr;
1700Sstevel@tonic-gate 	enum fcc_type	cc;
1710Sstevel@tonic-gate 	uint32_t	nfcc;		/* fcc number field. */
1720Sstevel@tonic-gate 	uint64_t	lusr;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	nrs1 = inst.rs1;
1750Sstevel@tonic-gate 	nrs2 = inst.rs2;
1760Sstevel@tonic-gate 	nrd = inst.rd;
1770Sstevel@tonic-gate 	fsr = *pfsr;
1780Sstevel@tonic-gate 	pfpsd->fp_current_exceptions = 0;	/* Init current exceptions. */
1790Sstevel@tonic-gate 	pfpsd->fp_fsrtem    = fsr.tem;		/* Obtain fsr's tem */
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * Obtain rounding direction and precision
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	pfpsd->fp_direction = GSR_IM(gsr) ? GSR_IRND(gsr) : fsr.rnd;
1840Sstevel@tonic-gate 	pfpsd->fp_precision = fsr.rnp;
1850Sstevel@tonic-gate 	nfcc = nrd & 0x3;
1860Sstevel@tonic-gate 	if (inst.op3 == 0x35) {		/* fpop2 */
1870Sstevel@tonic-gate 		fsr.cexc = 0;
1880Sstevel@tonic-gate 		*pfsr = fsr;
1890Sstevel@tonic-gate 		if ((inst.opcode & 0xf) == 0) {
1900Sstevel@tonic-gate 			if ((fp_notp) && (inst.prec == 0))
1910Sstevel@tonic-gate 				return (ftt_unimplemented);
1920Sstevel@tonic-gate 			FPUINFO_KSTAT(fpu_sim_fmovcc);
1930Sstevel@tonic-gate 			return (fmovcc(pfpsd, inst, pfsr));	/* fmovcc */
1940Sstevel@tonic-gate 		} else if ((inst.opcode & 0x7) == 1) {
1950Sstevel@tonic-gate 			if ((fp_notp) && (inst.prec == 0))
1960Sstevel@tonic-gate 				return (ftt_unimplemented);
1970Sstevel@tonic-gate 			FPUINFO_KSTAT(fpu_sim_fmovr);
1980Sstevel@tonic-gate 			return (fmovr(pfpsd, inst));		/* fmovr */
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	/* ibit not valid for fpop1 instructions */
2020Sstevel@tonic-gate 	if ((fp_notp) && (inst.ibit != 0))
2030Sstevel@tonic-gate 		return (ftt_unimplemented);
2040Sstevel@tonic-gate 	if ((fp_notp) && (inst.prec == 0)) {	/* fxto[sdq], fito[sdq] */
2050Sstevel@tonic-gate 		if ((inst.opcode != flltos) &&
2060Sstevel@tonic-gate 		    (inst.opcode != flltod) &&
2070Sstevel@tonic-gate 		    (inst.opcode != flltox) &&
2080Sstevel@tonic-gate 		    (inst.opcode != fitos) &&
2090Sstevel@tonic-gate 		    (inst.opcode != fitod) &&
2100Sstevel@tonic-gate 		    (inst.opcode != fitox)) {
2110Sstevel@tonic-gate 			return (ftt_unimplemented);
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	switch (inst.opcode) {
2150Sstevel@tonic-gate 	case fmovs:		/* also covers fmovd, fmovq */
2160Sstevel@tonic-gate 		if (inst.prec < 2) {	/* fmovs */
2170Sstevel@tonic-gate 			_fp_unpack_word(pfpsd, &usr, nrs2);
2180Sstevel@tonic-gate 			_fp_pack_word(pfpsd, &usr, nrd);
2190Sstevel@tonic-gate 			FPUINFO_KSTAT(fpu_sim_fmovs);
2200Sstevel@tonic-gate 		} else {		/* fmovd */
2210Sstevel@tonic-gate 			_fp_unpack_extword(pfpsd, &lusr, nrs2);
2220Sstevel@tonic-gate 			_fp_pack_extword(pfpsd, &lusr, nrd);
2230Sstevel@tonic-gate 			if (inst.prec > 2) {		/* fmovq */
2240Sstevel@tonic-gate 				_fp_unpack_extword(pfpsd, &lusr, nrs2+2);
2250Sstevel@tonic-gate 				_fp_pack_extword(pfpsd, &lusr, nrd+2);
2260Sstevel@tonic-gate 				FPUINFO_KSTAT(fpu_sim_fmovq);
2270Sstevel@tonic-gate 			} else {
2280Sstevel@tonic-gate 				FPUINFO_KSTAT(fpu_sim_fmovd);
2290Sstevel@tonic-gate 			}
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 		break;
2320Sstevel@tonic-gate 	case fabss:		/* also covers fabsd, fabsq */
2330Sstevel@tonic-gate 		if (inst.prec < 2) {	/* fabss */
2340Sstevel@tonic-gate 			_fp_unpack_word(pfpsd, &usr, nrs2);
2350Sstevel@tonic-gate 			usr &= 0x7fffffff;
2360Sstevel@tonic-gate 			_fp_pack_word(pfpsd, &usr, nrd);
2370Sstevel@tonic-gate 			FPUINFO_KSTAT(fpu_sim_fabss);
2380Sstevel@tonic-gate 		} else {		/* fabsd */
2390Sstevel@tonic-gate 			_fp_unpack_extword(pfpsd, &lusr, nrs2);
2400Sstevel@tonic-gate 			lusr &= 0x7fffffffffffffff;
2410Sstevel@tonic-gate 			_fp_pack_extword(pfpsd, &lusr, nrd);
2420Sstevel@tonic-gate 			if (inst.prec > 2) {		/* fabsq */
2430Sstevel@tonic-gate 				_fp_unpack_extword(pfpsd, &lusr, nrs2+2);
2440Sstevel@tonic-gate 				_fp_pack_extword(pfpsd, &lusr, nrd+2);
2450Sstevel@tonic-gate 				FPUINFO_KSTAT(fpu_sim_fabsq);
2460Sstevel@tonic-gate 			} else {
2470Sstevel@tonic-gate 				FPUINFO_KSTAT(fpu_sim_fabsd);
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		break;
2510Sstevel@tonic-gate 	case fnegs:		/* also covers fnegd, fnegq */
2520Sstevel@tonic-gate 	if (inst.prec < 2) {	/* fnegs */
2530Sstevel@tonic-gate 			_fp_unpack_word(pfpsd, &usr, nrs2);
2540Sstevel@tonic-gate 			usr ^= 0x80000000;
2550Sstevel@tonic-gate 			_fp_pack_word(pfpsd, &usr, nrd);
2560Sstevel@tonic-gate 			FPUINFO_KSTAT(fpu_sim_fnegs);
2570Sstevel@tonic-gate 		} else {		/* fnegd */
2580Sstevel@tonic-gate 			_fp_unpack_extword(pfpsd, &lusr, nrs2);
2590Sstevel@tonic-gate 			lusr ^= 0x8000000000000000;
2600Sstevel@tonic-gate 			_fp_pack_extword(pfpsd, &lusr, nrd);
2610Sstevel@tonic-gate 			if (inst.prec > 2) {		/* fnegq */
2620Sstevel@tonic-gate 				_fp_unpack_extword(pfpsd, &lusr, nrs2+2);
2630Sstevel@tonic-gate 				lusr ^= 0x0000000000000000;
2640Sstevel@tonic-gate 				_fp_pack_extword(pfpsd, &lusr, nrd+2);
2650Sstevel@tonic-gate 				FPUINFO_KSTAT(fpu_sim_fnegq);
2660Sstevel@tonic-gate 			} else {
2670Sstevel@tonic-gate 				FPUINFO_KSTAT(fpu_sim_fnegd);
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 		break;
2710Sstevel@tonic-gate 	case fadd:
2720Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
2730Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
2740Sstevel@tonic-gate 		_fp_add(pfpsd, &us1, &us2, &ud);
2750Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, inst.prec);
2760Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fadds,
2770Sstevel@tonic-gate 		    fpu_sim_faddd, fpu_sim_faddq);
2780Sstevel@tonic-gate 		break;
2790Sstevel@tonic-gate 	case fsub:
2800Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
2810Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
2820Sstevel@tonic-gate 		_fp_sub(pfpsd, &us1, &us2, &ud);
2830Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, inst.prec);
2840Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fsubs,
2850Sstevel@tonic-gate 		    fpu_sim_fsubd, fpu_sim_fsubq);
2860Sstevel@tonic-gate 		break;
2870Sstevel@tonic-gate 	case fmul:
2880Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
2890Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
2900Sstevel@tonic-gate 		_fp_mul(pfpsd, &us1, &us2, &ud);
2910Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, inst.prec);
2920Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fmuls,
2930Sstevel@tonic-gate 		    fpu_sim_fmuld, fpu_sim_fmulq);
2940Sstevel@tonic-gate 		break;
2950Sstevel@tonic-gate 	case fsmuld:
2960Sstevel@tonic-gate 		if ((fp_notp) && (inst.prec != 1))
2970Sstevel@tonic-gate 			return (ftt_unimplemented);
2980Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
2990Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
3000Sstevel@tonic-gate 		_fp_mul(pfpsd, &us1, &us2, &ud);
3010Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, (enum fp_op_type) ((int)inst.prec+1));
3020Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fsmuld);
3030Sstevel@tonic-gate 		break;
3040Sstevel@tonic-gate 	case fdmulx:
3050Sstevel@tonic-gate 		if ((fp_notp) && (inst.prec != 2))
3060Sstevel@tonic-gate 			return (ftt_unimplemented);
3070Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
3080Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
3090Sstevel@tonic-gate 		_fp_mul(pfpsd, &us1, &us2, &ud);
3100Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, (enum fp_op_type) ((int)inst.prec+1));
3110Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fdmulx);
3120Sstevel@tonic-gate 		break;
3130Sstevel@tonic-gate 	case fdiv:
3140Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
3150Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
3160Sstevel@tonic-gate 		_fp_div(pfpsd, &us1, &us2, &ud);
3170Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, inst.prec);
3180Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fdivs,
3190Sstevel@tonic-gate 		    fpu_sim_fdivd, fpu_sim_fdivq);
3200Sstevel@tonic-gate 		break;
3210Sstevel@tonic-gate 	case fcmp:
3220Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
3230Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
3240Sstevel@tonic-gate 		cc = _fp_compare(pfpsd, &us1, &us2, 0);
3250Sstevel@tonic-gate 		if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
3260Sstevel@tonic-gate 			switch (nfcc) {
3270Sstevel@tonic-gate 			case fcc_0:
3280Sstevel@tonic-gate 				fsr.fcc0 = cc;
3290Sstevel@tonic-gate 				break;
3300Sstevel@tonic-gate 			case fcc_1:
3310Sstevel@tonic-gate 				fsr.fcc1 = cc;
3320Sstevel@tonic-gate 				break;
3330Sstevel@tonic-gate 			case fcc_2:
3340Sstevel@tonic-gate 				fsr.fcc2 = cc;
3350Sstevel@tonic-gate 				break;
3360Sstevel@tonic-gate 			case fcc_3:
3370Sstevel@tonic-gate 				fsr.fcc3 = cc;
3380Sstevel@tonic-gate 				break;
3390Sstevel@tonic-gate 			}
3400Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fcmps,
3410Sstevel@tonic-gate 		    fpu_sim_fcmpd, fpu_sim_fcmpq);
3420Sstevel@tonic-gate 		break;
3430Sstevel@tonic-gate 	case fcmpe:
3440Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
3450Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
3460Sstevel@tonic-gate 		cc = _fp_compare(pfpsd, &us1, &us2, 1);
3470Sstevel@tonic-gate 		if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
3480Sstevel@tonic-gate 			switch (nfcc) {
3490Sstevel@tonic-gate 			case fcc_0:
3500Sstevel@tonic-gate 				fsr.fcc0 = cc;
3510Sstevel@tonic-gate 				break;
3520Sstevel@tonic-gate 			case fcc_1:
3530Sstevel@tonic-gate 				fsr.fcc1 = cc;
3540Sstevel@tonic-gate 				break;
3550Sstevel@tonic-gate 			case fcc_2:
3560Sstevel@tonic-gate 				fsr.fcc2 = cc;
3570Sstevel@tonic-gate 				break;
3580Sstevel@tonic-gate 			case fcc_3:
3590Sstevel@tonic-gate 				fsr.fcc3 = cc;
3600Sstevel@tonic-gate 				break;
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fcmpes,
3630Sstevel@tonic-gate 			fpu_sim_fcmped, fpu_sim_fcmpeq);
3640Sstevel@tonic-gate 		break;
3650Sstevel@tonic-gate 	case fsqrt:
3660Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
3670Sstevel@tonic-gate 		_fp_sqrt(pfpsd, &us1, &ud);
3680Sstevel@tonic-gate 		_fp_pack(pfpsd, &ud, nrd, inst.prec);
3690Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fsqrts,
3700Sstevel@tonic-gate 		    fpu_sim_fsqrtd, fpu_sim_fsqrtq);
3710Sstevel@tonic-gate 		break;
3720Sstevel@tonic-gate 	case ftoi:
3730Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
3740Sstevel@tonic-gate 		pfpsd->fp_direction = fp_tozero;
3750Sstevel@tonic-gate 		/* Force rounding toward zero. */
3760Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_int32);
3770Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fstoi,
3780Sstevel@tonic-gate 		    fpu_sim_fdtoi, fpu_sim_fqtoi);
3790Sstevel@tonic-gate 		break;
3800Sstevel@tonic-gate 	case ftoll:
3810Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
3820Sstevel@tonic-gate 		pfpsd->fp_direction = fp_tozero;
3830Sstevel@tonic-gate 		/* Force rounding toward zero. */
3840Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_int64);
3850Sstevel@tonic-gate 		FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fstox,
3860Sstevel@tonic-gate 		    fpu_sim_fdtox, fpu_sim_fqtox);
3870Sstevel@tonic-gate 		break;
3880Sstevel@tonic-gate 	case flltos:
3890Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
3900Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_single);
3910Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fxtos);
3920Sstevel@tonic-gate 		break;
3930Sstevel@tonic-gate 	case flltod:
3940Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
3950Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_double);
3960Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fxtod);
3970Sstevel@tonic-gate 		break;
3980Sstevel@tonic-gate 	case flltox:
3990Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
4000Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_extended);
4010Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fxtoq);
4020Sstevel@tonic-gate 		break;
4030Sstevel@tonic-gate 	case fitos:
4040Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
4050Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_single);
4060Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fitos);
4070Sstevel@tonic-gate 		break;
4080Sstevel@tonic-gate 	case fitod:
4090Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
4100Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_double);
4110Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fitod);
4120Sstevel@tonic-gate 		break;
4130Sstevel@tonic-gate 	case fitox:
4140Sstevel@tonic-gate 		_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
4150Sstevel@tonic-gate 		_fp_pack(pfpsd, &us1, nrd, fp_op_extended);
4160Sstevel@tonic-gate 		FPUINFO_KSTAT(fpu_sim_fitoq);
4170Sstevel@tonic-gate 		break;
4180Sstevel@tonic-gate 	default:
4190Sstevel@tonic-gate 		return (ftt_unimplemented);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	fsr.cexc = pfpsd->fp_current_exceptions;
4220Sstevel@tonic-gate 	if (pfpsd->fp_current_exceptions) {	/* Exception(s) occurred. */
4230Sstevel@tonic-gate 		andexcep = pfpsd->fp_current_exceptions & fsr.tem;
4240Sstevel@tonic-gate 		if (andexcep != 0) {	/* Signal an IEEE SIGFPE here. */
4250Sstevel@tonic-gate 			if (andexcep & (1 << fp_invalid)) {
4260Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTINV;
4270Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_NV;
4280Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_overflow)) {
4290Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTOVF;
4300Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_OF;
4310Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_underflow)) {
4320Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTUND;
4330Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_UF;
4340Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_division)) {
4350Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTDIV;
4360Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_DZ;
4370Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_inexact)) {
4380Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTRES;
4390Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_NX;
4400Sstevel@tonic-gate 			} else {
4410Sstevel@tonic-gate 				pfpsd->fp_trapcode = 0;
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 			*pfsr = fsr;
4440Sstevel@tonic-gate 			return (ftt_ieee);
4450Sstevel@tonic-gate 		} else {	/* Just set accrued exception field. */
4460Sstevel@tonic-gate 			fsr.aexc |= pfpsd->fp_current_exceptions;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 	*pfsr = fsr;
4500Sstevel@tonic-gate 	return (ftt_none);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * fpu_vis_sim simulates fpu and vis instructions;
4550Sstevel@tonic-gate  * It can work with both real and pcb image registers.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate enum ftt_type
4580Sstevel@tonic-gate fpu_vis_sim(
4590Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
4600Sstevel@tonic-gate 	fp_inst_type	*pinst,	/* Address of FPU instruction to simulate */
4610Sstevel@tonic-gate 	struct regs	*pregs,	/* Pointer to PCB image of registers. */
4620Sstevel@tonic-gate 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write */
4630Sstevel@tonic-gate 	uint64_t	gsr,	/* Image of GSR to read */
4640Sstevel@tonic-gate 	uint32_t	inst)	/* The FPU instruction to simulate */
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	klwp_id_t lwp = ttolwp(curthread);
4670Sstevel@tonic-gate 	union {
4680Sstevel@tonic-gate 		uint32_t	i;
4690Sstevel@tonic-gate 		fp_inst_type	inst;
4700Sstevel@tonic-gate 	} fp;
4710Sstevel@tonic-gate 	kfpu_t *pfp = lwptofpu(lwp);
4720Sstevel@tonic-gate 	enum ftt_type ftt;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	fp.i = inst;
4750Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst;
4760Sstevel@tonic-gate 	if (fpu_exists) {
4770Sstevel@tonic-gate 		pfpsd->fp_current_read_freg = _fp_read_pfreg;
4780Sstevel@tonic-gate 		pfpsd->fp_current_write_freg = _fp_write_pfreg;
4790Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg = _fp_read_pdreg;
4800Sstevel@tonic-gate 		pfpsd->fp_current_write_dreg = _fp_write_pdreg;
481*7Sdf157793 		pfpsd->fp_current_read_gsr = _fp_read_pgsr;
482*7Sdf157793 		pfpsd->fp_current_write_gsr = _fp_write_pgsr;
4830Sstevel@tonic-gate 	} else {
4840Sstevel@tonic-gate 		pfpsd->fp_current_pfregs = pfp;
4850Sstevel@tonic-gate 		pfpsd->fp_current_read_freg = _fp_read_vfreg;
4860Sstevel@tonic-gate 		pfpsd->fp_current_write_freg = _fp_write_vfreg;
4870Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg = _fp_read_vdreg;
4880Sstevel@tonic-gate 		pfpsd->fp_current_write_dreg = _fp_write_vdreg;
489*7Sdf157793 		pfpsd->fp_current_read_gsr = get_gsr;
490*7Sdf157793 		pfpsd->fp_current_write_gsr = set_gsr;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
4940Sstevel@tonic-gate 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
4950Sstevel@tonic-gate 					pregs, (ulong_t *)pregs->r_sp, pfp);
4960Sstevel@tonic-gate 			return (ftt);
4970Sstevel@tonic-gate 	} else if ((fp.inst.hibits == 2) &&
4980Sstevel@tonic-gate 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35))) {
4990Sstevel@tonic-gate 		ftt =  _fp_fpu_simulator(pfpsd, fp.inst, pfsr, gsr);
5000Sstevel@tonic-gate 		if (ftt == ftt_none || ftt == ftt_ieee) {
5010Sstevel@tonic-gate 			pregs->r_pc = pregs->r_npc;
5020Sstevel@tonic-gate 			pregs->r_npc += 4;
5030Sstevel@tonic-gate 		}
5040Sstevel@tonic-gate 		return (ftt);
5050Sstevel@tonic-gate 	} else {
5060Sstevel@tonic-gate 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs,
5070Sstevel@tonic-gate 		    (ulong_t *)pregs->r_sp, pfp);
5080Sstevel@tonic-gate 		return (ftt);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * fpu_simulator simulates FPU instructions only;
5140Sstevel@tonic-gate  * reads and writes FPU data registers directly.
5150Sstevel@tonic-gate  */
5160Sstevel@tonic-gate enum ftt_type
5170Sstevel@tonic-gate fpu_simulator(
5180Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
5190Sstevel@tonic-gate 	fp_inst_type	*pinst,	/* Address of FPU instruction to simulate */
5200Sstevel@tonic-gate 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write */
5210Sstevel@tonic-gate 	uint64_t	gsr,	/* Image of GSR to read */
5220Sstevel@tonic-gate 	uint32_t	inst)	/* The FPU instruction to simulate */
5230Sstevel@tonic-gate {
5240Sstevel@tonic-gate 	union {
5250Sstevel@tonic-gate 		uint32_t	i;
5260Sstevel@tonic-gate 		fp_inst_type	inst;
5270Sstevel@tonic-gate 	} fp;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	fp.i = inst;
5300Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst;
5310Sstevel@tonic-gate 	pfpsd->fp_current_read_freg = _fp_read_pfreg;
5320Sstevel@tonic-gate 	pfpsd->fp_current_write_freg = _fp_write_pfreg;
5330Sstevel@tonic-gate 	pfpsd->fp_current_read_dreg = _fp_read_pdreg;
5340Sstevel@tonic-gate 	pfpsd->fp_current_write_dreg = _fp_write_pdreg;
535*7Sdf157793 	pfpsd->fp_current_read_gsr = _fp_read_pgsr;
536*7Sdf157793 	pfpsd->fp_current_write_gsr = _fp_write_pgsr;
5370Sstevel@tonic-gate 	return (_fp_fpu_simulator(pfpsd, fp.inst, pfsr, gsr));
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate  * fp_emulator simulates FPU and CPU-FPU instructions; reads and writes FPU
5420Sstevel@tonic-gate  * data registers from image in pfpu.
5430Sstevel@tonic-gate  */
5440Sstevel@tonic-gate enum ftt_type
5450Sstevel@tonic-gate fp_emulator(
5460Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
5470Sstevel@tonic-gate 	fp_inst_type	*pinst,	/* Pointer to FPU instruction to simulate. */
5480Sstevel@tonic-gate 	struct regs	*pregs,	/* Pointer to PCB image of registers. */
5490Sstevel@tonic-gate 	void		*prw,	/* Pointer to locals and ins. */
5500Sstevel@tonic-gate 	kfpu_t		*pfpu)	/* Pointer to FPU register block. */
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	klwp_id_t lwp = ttolwp(curthread);
5530Sstevel@tonic-gate 	union {
5540Sstevel@tonic-gate 		uint32_t	i;
5550Sstevel@tonic-gate 		fp_inst_type	inst;
5560Sstevel@tonic-gate 	} fp;
5570Sstevel@tonic-gate 	enum ftt_type	ftt;
5580Sstevel@tonic-gate 	uint64_t gsr = get_gsr(pfpu);
5590Sstevel@tonic-gate 	kfpu_t *pfp = lwptofpu(lwp);
5600Sstevel@tonic-gate 	uint64_t	tfsr;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	tfsr = pfpu->fpu_fsr;
5630Sstevel@tonic-gate 	pfpsd->fp_current_pfregs = pfpu;
5640Sstevel@tonic-gate 	pfpsd->fp_current_read_freg = _fp_read_vfreg;
5650Sstevel@tonic-gate 	pfpsd->fp_current_write_freg = _fp_write_vfreg;
5660Sstevel@tonic-gate 	pfpsd->fp_current_read_dreg = _fp_read_vdreg;
5670Sstevel@tonic-gate 	pfpsd->fp_current_write_dreg = _fp_write_vdreg;
568*7Sdf157793 	pfpsd->fp_current_read_gsr = get_gsr;
569*7Sdf157793 	pfpsd->fp_current_write_gsr = set_gsr;
5700Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst; /* bad inst addr in case we trap */
5710Sstevel@tonic-gate 	ftt = _fp_read_inst((uint32_t *)pinst, &(fp.i), pfpsd);
5720Sstevel@tonic-gate 	if (ftt != ftt_none)
5730Sstevel@tonic-gate 		return (ftt);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if ((fp.inst.hibits == 2) &&
5760Sstevel@tonic-gate 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35))) {
5770Sstevel@tonic-gate 		ftt = _fp_fpu_simulator(pfpsd, fp.inst, (fsr_type *)&tfsr, gsr);
5780Sstevel@tonic-gate 		/* Do not retry emulated instruction. */
5790Sstevel@tonic-gate 		pregs->r_pc = pregs->r_npc;
5800Sstevel@tonic-gate 		pregs->r_npc += 4;
5810Sstevel@tonic-gate 		pfpu->fpu_fsr = tfsr;
5820Sstevel@tonic-gate 		if (ftt != ftt_none) {
5830Sstevel@tonic-gate 			/*
5840Sstevel@tonic-gate 			 * Simulation generated an exception of some kind,
5850Sstevel@tonic-gate 			 * simulate the fp queue for a signal.
5860Sstevel@tonic-gate 			 */
5870Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_addr = (uint32_t *)pinst;
5880Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_instr = fp.i;
5890Sstevel@tonic-gate 			pfpu->fpu_qcnt = 1;
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 	} else if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
5920Sstevel@tonic-gate 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
5930Sstevel@tonic-gate 			    pregs, prw, pfp);
5940Sstevel@tonic-gate 	} else
5950Sstevel@tonic-gate 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs, prw, pfpu);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (ftt != ftt_none)
5980Sstevel@tonic-gate 		return (ftt);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * If we are single-stepping, don't emulate any more instructions.
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE)
6040Sstevel@tonic-gate 		return (ftt);
6050Sstevel@tonic-gate again:
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * now read next instruction and see if it can be emulated
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	pinst = (fp_inst_type *)pregs->r_pc;
6100Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst; /* bad inst addr in case we trap */
6110Sstevel@tonic-gate 	ftt = _fp_read_inst((uint32_t *)pinst, &(fp.i), pfpsd);
6120Sstevel@tonic-gate 	if (ftt != ftt_none)
6130Sstevel@tonic-gate 		return (ftt);
6140Sstevel@tonic-gate 	if ((fp.inst.hibits == 2) &&		/* fpops */
6150Sstevel@tonic-gate 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35))) {
6160Sstevel@tonic-gate 		ftt = _fp_fpu_simulator(pfpsd, fp.inst, (fsr_type *)&tfsr, gsr);
6170Sstevel@tonic-gate 		/* Do not retry emulated instruction. */
6180Sstevel@tonic-gate 		pfpu->fpu_fsr = tfsr;
6190Sstevel@tonic-gate 		pregs->r_pc = pregs->r_npc;
6200Sstevel@tonic-gate 		pregs->r_npc += 4;
6210Sstevel@tonic-gate 		if (ftt != ftt_none) {
6220Sstevel@tonic-gate 			/*
6230Sstevel@tonic-gate 			 * Simulation generated an exception of some kind,
6240Sstevel@tonic-gate 			 * simulate the fp queue for a signal.
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_addr = (uint32_t *)pinst;
6270Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_instr = fp.i;
6280Sstevel@tonic-gate 			pfpu->fpu_qcnt = 1;
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 	} else if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
6310Sstevel@tonic-gate 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
6320Sstevel@tonic-gate 						pregs, prw, pfp);
6330Sstevel@tonic-gate 	} else if (
6340Sstevel@tonic-gate 						/* rd %gsr */
6350Sstevel@tonic-gate 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x28) &&
6360Sstevel@tonic-gate 			(fp.inst.rs1 == 0x13)) ||
6370Sstevel@tonic-gate 						/* wr %gsr */
6380Sstevel@tonic-gate 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x30) &&
6390Sstevel@tonic-gate 			(fp.inst.rd == 0x13)) ||
6400Sstevel@tonic-gate 						/* movcc */
6410Sstevel@tonic-gate 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x2c) &&
6420Sstevel@tonic-gate 			(((fp.i>>18) & 0x1) == 0)) ||
6430Sstevel@tonic-gate 						/* fbpcc */
6440Sstevel@tonic-gate 	    ((fp.inst.hibits == 0) && (((fp.i>>22) & 0x7) == 5)) ||
6450Sstevel@tonic-gate 						/* fldst */
6460Sstevel@tonic-gate 	    ((fp.inst.hibits == 3) && ((fp.inst.op3 & 0x38) == 0x20)) ||
6470Sstevel@tonic-gate 						/* fbcc */
6480Sstevel@tonic-gate 	    ((fp.inst.hibits == 0) && (((fp.i>>22) & 0x7) == 6))) {
6490Sstevel@tonic-gate 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs, prw, pfpu);
6500Sstevel@tonic-gate 	} else
6510Sstevel@tonic-gate 		return (ftt);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (ftt != ftt_none)
6540Sstevel@tonic-gate 		return (ftt);
6550Sstevel@tonic-gate 	else
6560Sstevel@tonic-gate 		goto again;
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * FPU simulator global kstat data
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate struct fpustat_kstat fpustat = {
6630Sstevel@tonic-gate 	{ "fpu_ieee_traps",		KSTAT_DATA_UINT64 },
6640Sstevel@tonic-gate 	{ "fpu_unfinished_traps",	KSTAT_DATA_UINT64 },
6650Sstevel@tonic-gate 	{ "fpu_unimplemented",		KSTAT_DATA_UINT64 },
6660Sstevel@tonic-gate };
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate kstat_t *fpu_kstat = NULL;
6690Sstevel@tonic-gate kstat_t *fpuinfo_kstat = NULL;
6700Sstevel@tonic-gate kstat_t *visinfo_kstat = NULL;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate void
6730Sstevel@tonic-gate fp_kstat_init(void)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	const uint_t fpustat_ndata = sizeof (fpustat) / sizeof (kstat_named_t);
6760Sstevel@tonic-gate 	const uint_t fpuinfo_ndata = sizeof (fpuinfo) / sizeof (kstat_named_t);
6770Sstevel@tonic-gate 	const uint_t visinfo_ndata = sizeof (visinfo) /sizeof (kstat_named_t);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	ASSERT(fpu_kstat == NULL);
6800Sstevel@tonic-gate 	if ((fpu_kstat = kstat_create("unix", 0, "fpu_traps", "misc",
6810Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, fpustat_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
6820Sstevel@tonic-gate 		cmn_err(CE_WARN, "CPU%d: kstat_create for fpu_traps failed",
6830Sstevel@tonic-gate 		    CPU->cpu_id);
6840Sstevel@tonic-gate 	} else {
6850Sstevel@tonic-gate 		fpu_kstat->ks_data = (void *)&fpustat;
6860Sstevel@tonic-gate 		kstat_install(fpu_kstat);
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	ASSERT(fpuinfo_kstat == NULL);
6900Sstevel@tonic-gate 	if ((fpuinfo_kstat = kstat_create("unix", 0, "fpu_info", "misc",
6910Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, fpuinfo_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
6920Sstevel@tonic-gate 		cmn_err(CE_WARN, "CPU%d: kstat_create for fpu_info failed",
6930Sstevel@tonic-gate 		    CPU->cpu_id);
6940Sstevel@tonic-gate 	} else {
6950Sstevel@tonic-gate 		fpuinfo_kstat->ks_data = (void *)&fpuinfo;
6960Sstevel@tonic-gate 		kstat_install(fpuinfo_kstat);
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 	ASSERT(visinfo_kstat == NULL);
6990Sstevel@tonic-gate 	if ((visinfo_kstat = kstat_create("unix", 0, "vis_info", "misc",
7000Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, visinfo_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
7010Sstevel@tonic-gate 		cmn_err(CE_WARN, "CPU%d: kstat_create for vis_info failed",
7020Sstevel@tonic-gate 		    CPU->cpu_id);
7030Sstevel@tonic-gate 	} else {
7040Sstevel@tonic-gate 		visinfo_kstat->ks_data = (void *)&visinfo;
7050Sstevel@tonic-gate 		kstat_install(visinfo_kstat);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate void
7100Sstevel@tonic-gate fp_kstat_update(enum ftt_type ftt)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	ASSERT((ftt == ftt_ieee) || (ftt == ftt_unfinished) ||
7130Sstevel@tonic-gate 	    (ftt == ftt_unimplemented));
7140Sstevel@tonic-gate 	if (ftt == ftt_ieee)
7150Sstevel@tonic-gate 		atomic_add_64(&fpustat.fpu_ieee_traps.value.ui64, 1);
7160Sstevel@tonic-gate 	else if (ftt == ftt_unfinished)
7170Sstevel@tonic-gate 		atomic_add_64(&fpustat.fpu_unfinished_traps.value.ui64, 1);
7180Sstevel@tonic-gate 	else if (ftt == ftt_unimplemented)
7190Sstevel@tonic-gate 		atomic_add_64(&fpustat.fpu_unimplemented_traps.value.ui64, 1);
7200Sstevel@tonic-gate }
721