xref: /onnv-gate/usr/src/uts/sparc/fpu/fpu_simulator.c (revision 1772:78cca3d2cc4b)
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
5*1772Sjl139090  * Common Development and Distribution License (the "License").
6*1772Sjl139090  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1772Sjl139090  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /* Main procedures for sparc FPU simulator. */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/fpu/fpu_simulator.h>
310Sstevel@tonic-gate #include <sys/fpu/globals.h>
320Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/signal.h>
350Sstevel@tonic-gate #include <sys/siginfo.h>
360Sstevel@tonic-gate #include <sys/thread.h>
370Sstevel@tonic-gate #include <sys/cpuvar.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/atomic.h>
400Sstevel@tonic-gate #include <sys/privregs.h>
410Sstevel@tonic-gate #include <sys/vis_simulator.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	FPUINFO_KSTAT(opcode)	{					\
440Sstevel@tonic-gate 	extern void __dtrace_probe___fpuinfo_##opcode(uint64_t *);	\
450Sstevel@tonic-gate 	uint64_t *stataddr = &fpuinfo.opcode.value.ui64;		\
460Sstevel@tonic-gate 	__dtrace_probe___fpuinfo_##opcode(stataddr);			\
470Sstevel@tonic-gate 	atomic_add_64(&fpuinfo.opcode.value.ui64, 1);			\
480Sstevel@tonic-gate }
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	FPUINFO_KSTAT_PREC(prec, kstat_s, kstat_d, kstat_q)		\
510Sstevel@tonic-gate 	if (prec < 2) {							\
520Sstevel@tonic-gate 		FPUINFO_KSTAT(kstat_s);					\
530Sstevel@tonic-gate 	} else if (prec == 2) {						\
540Sstevel@tonic-gate 		FPUINFO_KSTAT(kstat_d);					\
550Sstevel@tonic-gate 	} else {							\
560Sstevel@tonic-gate 		FPUINFO_KSTAT(kstat_q);					\
570Sstevel@tonic-gate 	}
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * FPU simulator global kstat data
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate struct fpuinfo_kstat fpuinfo = {
630Sstevel@tonic-gate 	{ "fpu_sim_fmovs",		KSTAT_DATA_UINT64},
640Sstevel@tonic-gate 	{ "fpu_sim_fmovd",		KSTAT_DATA_UINT64},
650Sstevel@tonic-gate 	{ "fpu_sim_fmovq",		KSTAT_DATA_UINT64},
660Sstevel@tonic-gate 	{ "fpu_sim_fnegs",		KSTAT_DATA_UINT64},
670Sstevel@tonic-gate 	{ "fpu_sim_fnegd",		KSTAT_DATA_UINT64},
680Sstevel@tonic-gate 	{ "fpu_sim_fnegq",		KSTAT_DATA_UINT64},
690Sstevel@tonic-gate 	{ "fpu_sim_fabss",		KSTAT_DATA_UINT64},
700Sstevel@tonic-gate 	{ "fpu_sim_fabsd",		KSTAT_DATA_UINT64},
710Sstevel@tonic-gate 	{ "fpu_sim_fabsq",		KSTAT_DATA_UINT64},
720Sstevel@tonic-gate 	{ "fpu_sim_fsqrts",		KSTAT_DATA_UINT64},
730Sstevel@tonic-gate 	{ "fpu_sim_fsqrtd",		KSTAT_DATA_UINT64},
740Sstevel@tonic-gate 	{ "fpu_sim_fsqrtq",		KSTAT_DATA_UINT64},
750Sstevel@tonic-gate 	{ "fpu_sim_fadds",		KSTAT_DATA_UINT64},
760Sstevel@tonic-gate 	{ "fpu_sim_faddd",		KSTAT_DATA_UINT64},
770Sstevel@tonic-gate 	{ "fpu_sim_faddq",		KSTAT_DATA_UINT64},
780Sstevel@tonic-gate 	{ "fpu_sim_fsubs",		KSTAT_DATA_UINT64},
790Sstevel@tonic-gate 	{ "fpu_sim_fsubd",		KSTAT_DATA_UINT64},
800Sstevel@tonic-gate 	{ "fpu_sim_fsubq",		KSTAT_DATA_UINT64},
810Sstevel@tonic-gate 	{ "fpu_sim_fmuls",		KSTAT_DATA_UINT64},
820Sstevel@tonic-gate 	{ "fpu_sim_fmuld",		KSTAT_DATA_UINT64},
830Sstevel@tonic-gate 	{ "fpu_sim_fmulq",		KSTAT_DATA_UINT64},
840Sstevel@tonic-gate 	{ "fpu_sim_fdivs",		KSTAT_DATA_UINT64},
850Sstevel@tonic-gate 	{ "fpu_sim_fdivd",		KSTAT_DATA_UINT64},
860Sstevel@tonic-gate 	{ "fpu_sim_fdivq",		KSTAT_DATA_UINT64},
870Sstevel@tonic-gate 	{ "fpu_sim_fcmps",		KSTAT_DATA_UINT64},
880Sstevel@tonic-gate 	{ "fpu_sim_fcmpd",		KSTAT_DATA_UINT64},
890Sstevel@tonic-gate 	{ "fpu_sim_fcmpq",		KSTAT_DATA_UINT64},
900Sstevel@tonic-gate 	{ "fpu_sim_fcmpes",		KSTAT_DATA_UINT64},
910Sstevel@tonic-gate 	{ "fpu_sim_fcmped",		KSTAT_DATA_UINT64},
920Sstevel@tonic-gate 	{ "fpu_sim_fcmpeq",		KSTAT_DATA_UINT64},
930Sstevel@tonic-gate 	{ "fpu_sim_fsmuld",		KSTAT_DATA_UINT64},
940Sstevel@tonic-gate 	{ "fpu_sim_fdmulx",		KSTAT_DATA_UINT64},
950Sstevel@tonic-gate 	{ "fpu_sim_fstox",		KSTAT_DATA_UINT64},
960Sstevel@tonic-gate 	{ "fpu_sim_fdtox",		KSTAT_DATA_UINT64},
970Sstevel@tonic-gate 	{ "fpu_sim_fqtox",		KSTAT_DATA_UINT64},
980Sstevel@tonic-gate 	{ "fpu_sim_fxtos",		KSTAT_DATA_UINT64},
990Sstevel@tonic-gate 	{ "fpu_sim_fxtod",		KSTAT_DATA_UINT64},
1000Sstevel@tonic-gate 	{ "fpu_sim_fxtoq",		KSTAT_DATA_UINT64},
1010Sstevel@tonic-gate 	{ "fpu_sim_fitos",		KSTAT_DATA_UINT64},
1020Sstevel@tonic-gate 	{ "fpu_sim_fitod",		KSTAT_DATA_UINT64},
1030Sstevel@tonic-gate 	{ "fpu_sim_fitoq",		KSTAT_DATA_UINT64},
1040Sstevel@tonic-gate 	{ "fpu_sim_fstoi",		KSTAT_DATA_UINT64},
1050Sstevel@tonic-gate 	{ "fpu_sim_fdtoi",		KSTAT_DATA_UINT64},
1060Sstevel@tonic-gate 	{ "fpu_sim_fqtoi",		KSTAT_DATA_UINT64},
1070Sstevel@tonic-gate 	{ "fpu_sim_fmovcc",		KSTAT_DATA_UINT64},
1080Sstevel@tonic-gate 	{ "fpu_sim_fmovr",		KSTAT_DATA_UINT64},
109*1772Sjl139090 	{ "fpu_sim_fmadds",		KSTAT_DATA_UINT64},
110*1772Sjl139090 	{ "fpu_sim_fmaddd",		KSTAT_DATA_UINT64},
111*1772Sjl139090 	{ "fpu_sim_fmsubs",		KSTAT_DATA_UINT64},
112*1772Sjl139090 	{ "fpu_sim_fmsubd",		KSTAT_DATA_UINT64},
113*1772Sjl139090 	{ "fpu_sim_fnmadds",		KSTAT_DATA_UINT64},
114*1772Sjl139090 	{ "fpu_sim_fnmaddd",		KSTAT_DATA_UINT64},
115*1772Sjl139090 	{ "fpu_sim_fnmsubs",		KSTAT_DATA_UINT64},
116*1772Sjl139090 	{ "fpu_sim_fnmsubd",		KSTAT_DATA_UINT64},
117*1772Sjl139090 	{ "fpu_sim_invalid",		KSTAT_DATA_UINT64},
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate struct visinfo_kstat visinfo = {
1210Sstevel@tonic-gate 	{ "vis_edge8",		KSTAT_DATA_UINT64},
1220Sstevel@tonic-gate 	{ "vis_edge8n",		KSTAT_DATA_UINT64},
1230Sstevel@tonic-gate 	{ "vis_edge8l",		KSTAT_DATA_UINT64},
1240Sstevel@tonic-gate 	{ "vis_edge8ln",	KSTAT_DATA_UINT64},
1250Sstevel@tonic-gate 	{ "vis_edge16",		KSTAT_DATA_UINT64},
1260Sstevel@tonic-gate 	{ "vis_edge16n",	KSTAT_DATA_UINT64},
1270Sstevel@tonic-gate 	{ "vis_edge16l",	KSTAT_DATA_UINT64},
1280Sstevel@tonic-gate 	{ "vis_edge16ln",	KSTAT_DATA_UINT64},
1290Sstevel@tonic-gate 	{ "vis_edge32",		KSTAT_DATA_UINT64},
1300Sstevel@tonic-gate 	{ "vis_edge32n",	KSTAT_DATA_UINT64},
1310Sstevel@tonic-gate 	{ "vis_edge32l",	KSTAT_DATA_UINT64},
1320Sstevel@tonic-gate 	{ "vis_edge32ln",	KSTAT_DATA_UINT64},
1330Sstevel@tonic-gate 	{ "vis_array8",		KSTAT_DATA_UINT64},
1340Sstevel@tonic-gate 	{ "vis_array16",	KSTAT_DATA_UINT64},
1350Sstevel@tonic-gate 	{ "vis_array32",	KSTAT_DATA_UINT64},
1360Sstevel@tonic-gate 	{ "vis_bmask",		KSTAT_DATA_UINT64},
1370Sstevel@tonic-gate 	{ "vis_fcmple16",	KSTAT_DATA_UINT64},
1380Sstevel@tonic-gate 	{ "vis_fcmpne16",	KSTAT_DATA_UINT64},
1390Sstevel@tonic-gate 	{ "vis_fcmpgt16",	KSTAT_DATA_UINT64},
1400Sstevel@tonic-gate 	{ "vis_fcmpeq16",	KSTAT_DATA_UINT64},
1410Sstevel@tonic-gate 	{ "vis_fcmple32",	KSTAT_DATA_UINT64},
1420Sstevel@tonic-gate 	{ "vis_fcmpne32",	KSTAT_DATA_UINT64},
1430Sstevel@tonic-gate 	{ "vis_fcmpgt32",	KSTAT_DATA_UINT64},
1440Sstevel@tonic-gate 	{ "vis_fcmpeq32",	KSTAT_DATA_UINT64},
1450Sstevel@tonic-gate 	{ "vis_fmul8x16",	KSTAT_DATA_UINT64},
1460Sstevel@tonic-gate 	{ "vis_fmul8x16au",	KSTAT_DATA_UINT64},
1470Sstevel@tonic-gate 	{ "vis_fmul8x16al",	KSTAT_DATA_UINT64},
1480Sstevel@tonic-gate 	{ "vis_fmul8sux16",	KSTAT_DATA_UINT64},
1490Sstevel@tonic-gate 	{ "vis_fmul8ulx16",	KSTAT_DATA_UINT64},
1500Sstevel@tonic-gate 	{ "vis_fmuld8sux16",	KSTAT_DATA_UINT64},
1510Sstevel@tonic-gate 	{ "vis_fmuld8ulx16",	KSTAT_DATA_UINT64},
1520Sstevel@tonic-gate 	{ "vis_fpack16",	KSTAT_DATA_UINT64},
1530Sstevel@tonic-gate 	{ "vis_fpack32",	KSTAT_DATA_UINT64},
1540Sstevel@tonic-gate 	{ "vis_fpackfix",	KSTAT_DATA_UINT64},
1550Sstevel@tonic-gate 	{ "vis_fexpand",	KSTAT_DATA_UINT64},
1560Sstevel@tonic-gate 	{ "vis_fpmerge",	KSTAT_DATA_UINT64},
1570Sstevel@tonic-gate 	{ "vis_pdist",		KSTAT_DATA_UINT64},
1580Sstevel@tonic-gate 	{ "vis_bshuffle",	KSTAT_DATA_UINT64},
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /* PUBLIC FUNCTIONS */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate int fp_notp = 1;	/* fp checking not a problem */
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /* ARGSUSED */
1670Sstevel@tonic-gate static enum ftt_type
1680Sstevel@tonic-gate _fp_fpu_simulator(
1690Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to fpu simulator data */
1700Sstevel@tonic-gate 	fp_inst_type	inst,	/* FPU instruction to simulate. */
1710Sstevel@tonic-gate 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write. */
1720Sstevel@tonic-gate 	uint64_t	gsr)	/* Image of GSR to read */
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	unpacked	us1, us2, ud;	/* Unpacked operands and result. */
1750Sstevel@tonic-gate 	uint32_t	nrs1, nrs2, nrd; /* Register number fields. */
1760Sstevel@tonic-gate 	uint32_t	usr, andexcep;
1770Sstevel@tonic-gate 	fsr_type	fsr;
1780Sstevel@tonic-gate 	enum fcc_type	cc;
1790Sstevel@tonic-gate 	uint32_t	nfcc;		/* fcc number field. */
1800Sstevel@tonic-gate 	uint64_t	lusr;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	nrs1 = inst.rs1;
1830Sstevel@tonic-gate 	nrs2 = inst.rs2;
1840Sstevel@tonic-gate 	nrd = inst.rd;
1850Sstevel@tonic-gate 	fsr = *pfsr;
1860Sstevel@tonic-gate 	pfpsd->fp_current_exceptions = 0;	/* Init current exceptions. */
1870Sstevel@tonic-gate 	pfpsd->fp_fsrtem    = fsr.tem;		/* Obtain fsr's tem */
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * Obtain rounding direction and precision
1900Sstevel@tonic-gate 	 */
1910Sstevel@tonic-gate 	pfpsd->fp_direction = GSR_IM(gsr) ? GSR_IRND(gsr) : fsr.rnd;
1920Sstevel@tonic-gate 	pfpsd->fp_precision = fsr.rnp;
193*1772Sjl139090 
194*1772Sjl139090 	if (inst.op3 == 0x37) { /* IMPDEP2B FMA-fused opcode */
195*1772Sjl139090 		fp_fma_inst_type *fma_inst;
196*1772Sjl139090 		uint32_t	nrs3;
197*1772Sjl139090 		unpacked	us3;
198*1772Sjl139090 		unpacked	ust;
199*1772Sjl139090 		fma_inst = (fp_fma_inst_type *) &inst;
200*1772Sjl139090 		nrs2 = fma_inst->rs2;
201*1772Sjl139090 		nrs3 = fma_inst->rs3;
202*1772Sjl139090 		switch (fma_inst->var) {
203*1772Sjl139090 		case fmadd:
204*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
205*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
206*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ust);
207*1772Sjl139090 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
208*1772Sjl139090 				_fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
209*1772Sjl139090 				_fp_add(pfpsd, &ust, &us3, &ud);
210*1772Sjl139090 				_fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
211*1772Sjl139090 			}
212*1772Sjl139090 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fmadds,
213*1772Sjl139090 				fpu_sim_fmaddd, fpu_sim_invalid);
214*1772Sjl139090 			break;
215*1772Sjl139090 		case fmsub:
216*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
217*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
218*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ust);
219*1772Sjl139090 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
220*1772Sjl139090 				_fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
221*1772Sjl139090 				_fp_sub(pfpsd, &ust, &us3, &ud);
222*1772Sjl139090 				_fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
223*1772Sjl139090 			}
224*1772Sjl139090 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fmsubs,
225*1772Sjl139090 				fpu_sim_fmsubd, fpu_sim_invalid);
226*1772Sjl139090 			break;
227*1772Sjl139090 		case fnmadd:
228*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
229*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
230*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ust);
231*1772Sjl139090 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
232*1772Sjl139090 				if (ust.fpclass == fp_quiet ||
233*1772Sjl139090 					ust.fpclass == fp_signaling) {
234*1772Sjl139090 				    _fp_pack(pfpsd, &ust, nrd, fma_inst->sz);
235*1772Sjl139090 				} else {
236*1772Sjl139090 				    _fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
237*1772Sjl139090 				    _fp_add(pfpsd, &ust, &us3, &ud);
238*1772Sjl139090 				    ud.sign ^= 1;
239*1772Sjl139090 				    _fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
240*1772Sjl139090 				}
241*1772Sjl139090 			}
242*1772Sjl139090 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fnmadds,
243*1772Sjl139090 				fpu_sim_fnmaddd, fpu_sim_invalid);
244*1772Sjl139090 			break;
245*1772Sjl139090 		case fnmsub:
246*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
247*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
248*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ust);
249*1772Sjl139090 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
250*1772Sjl139090 				if (ust.fpclass == fp_quiet ||
251*1772Sjl139090 					ust.fpclass == fp_signaling) {
252*1772Sjl139090 				    _fp_pack(pfpsd, &ust, nrd, fma_inst->sz);
253*1772Sjl139090 				} else {
254*1772Sjl139090 				    _fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
255*1772Sjl139090 				    _fp_sub(pfpsd, &ust, &us3, &ud);
256*1772Sjl139090 				    ud.sign ^= 1;
257*1772Sjl139090 				    _fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
258*1772Sjl139090 				}
259*1772Sjl139090 			}
260*1772Sjl139090 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fnmsubs,
261*1772Sjl139090 				fpu_sim_fnmsubd, fpu_sim_invalid);
2620Sstevel@tonic-gate 		}
263*1772Sjl139090 	} else {
264*1772Sjl139090 		nfcc = nrd & 0x3;
265*1772Sjl139090 		if (inst.op3 == 0x35) {		/* fpop2 */
266*1772Sjl139090 			fsr.cexc = 0;
267*1772Sjl139090 			*pfsr = fsr;
268*1772Sjl139090 			if ((inst.opcode & 0xf) == 0) {
269*1772Sjl139090 				if ((fp_notp) && (inst.prec == 0))
270*1772Sjl139090 					return (ftt_unimplemented);
271*1772Sjl139090 				FPUINFO_KSTAT(fpu_sim_fmovcc);
272*1772Sjl139090 				return (fmovcc(pfpsd, inst, pfsr)); /* fmovcc */
273*1772Sjl139090 			} else if ((inst.opcode & 0x7) == 1) {
274*1772Sjl139090 				if ((fp_notp) && (inst.prec == 0))
275*1772Sjl139090 					return (ftt_unimplemented);
276*1772Sjl139090 				FPUINFO_KSTAT(fpu_sim_fmovr);
277*1772Sjl139090 				return (fmovr(pfpsd, inst));	/* fmovr */
2780Sstevel@tonic-gate 			}
2790Sstevel@tonic-gate 		}
280*1772Sjl139090 		/* ibit not valid for fpop1 instructions */
281*1772Sjl139090 		if ((fp_notp) && (inst.ibit != 0))
282*1772Sjl139090 			return (ftt_unimplemented);
283*1772Sjl139090 		if ((fp_notp) && (inst.prec == 0)) { /* fxto[sdq], fito[sdq] */
284*1772Sjl139090 			if ((inst.opcode != flltos) &&
285*1772Sjl139090 			    (inst.opcode != flltod) &&
286*1772Sjl139090 			    (inst.opcode != flltox) &&
287*1772Sjl139090 			    (inst.opcode != fitos) &&
288*1772Sjl139090 			    (inst.opcode != fitod) &&
289*1772Sjl139090 			    (inst.opcode != fitox)) {
290*1772Sjl139090 				return (ftt_unimplemented);
2910Sstevel@tonic-gate 			}
2920Sstevel@tonic-gate 		}
293*1772Sjl139090 		switch (inst.opcode) {
294*1772Sjl139090 		case fmovs:		/* also covers fmovd, fmovq */
295*1772Sjl139090 			if (inst.prec < 2) {	/* fmovs */
296*1772Sjl139090 				_fp_unpack_word(pfpsd, &usr, nrs2);
297*1772Sjl139090 				_fp_pack_word(pfpsd, &usr, nrd);
298*1772Sjl139090 				FPUINFO_KSTAT(fpu_sim_fmovs);
299*1772Sjl139090 			} else {		/* fmovd */
300*1772Sjl139090 				_fp_unpack_extword(pfpsd, &lusr, nrs2);
301*1772Sjl139090 				_fp_pack_extword(pfpsd, &lusr, nrd);
302*1772Sjl139090 				if (inst.prec > 2) {		/* fmovq */
303*1772Sjl139090 				    _fp_unpack_extword(pfpsd, &lusr, nrs2+2);
304*1772Sjl139090 				    _fp_pack_extword(pfpsd, &lusr, nrd+2);
305*1772Sjl139090 				    FPUINFO_KSTAT(fpu_sim_fmovq);
306*1772Sjl139090 				} else {
307*1772Sjl139090 				    FPUINFO_KSTAT(fpu_sim_fmovd);
308*1772Sjl139090 				}
309*1772Sjl139090 			}
310*1772Sjl139090 			break;
311*1772Sjl139090 		case fabss:		/* also covers fabsd, fabsq */
312*1772Sjl139090 			if (inst.prec < 2) {	/* fabss */
313*1772Sjl139090 				_fp_unpack_word(pfpsd, &usr, nrs2);
314*1772Sjl139090 				usr &= 0x7fffffff;
315*1772Sjl139090 				_fp_pack_word(pfpsd, &usr, nrd);
316*1772Sjl139090 				FPUINFO_KSTAT(fpu_sim_fabss);
317*1772Sjl139090 			} else {		/* fabsd */
318*1772Sjl139090 				_fp_unpack_extword(pfpsd, &lusr, nrs2);
319*1772Sjl139090 				lusr &= 0x7fffffffffffffff;
320*1772Sjl139090 				_fp_pack_extword(pfpsd, &lusr, nrd);
321*1772Sjl139090 				if (inst.prec > 2) {		/* fabsq */
322*1772Sjl139090 				    _fp_unpack_extword(pfpsd, &lusr, nrs2+2);
323*1772Sjl139090 				    _fp_pack_extword(pfpsd, &lusr, nrd+2);
324*1772Sjl139090 				    FPUINFO_KSTAT(fpu_sim_fabsq);
325*1772Sjl139090 				} else {
326*1772Sjl139090 				    FPUINFO_KSTAT(fpu_sim_fabsd);
327*1772Sjl139090 				}
328*1772Sjl139090 			}
329*1772Sjl139090 			break;
330*1772Sjl139090 		case fnegs:		/* also covers fnegd, fnegq */
331*1772Sjl139090 			if (inst.prec < 2) {	/* fnegs */
332*1772Sjl139090 				_fp_unpack_word(pfpsd, &usr, nrs2);
333*1772Sjl139090 				usr ^= 0x80000000;
334*1772Sjl139090 				_fp_pack_word(pfpsd, &usr, nrd);
335*1772Sjl139090 				FPUINFO_KSTAT(fpu_sim_fnegs);
336*1772Sjl139090 			} else {		/* fnegd */
337*1772Sjl139090 				_fp_unpack_extword(pfpsd, &lusr, nrs2);
338*1772Sjl139090 				lusr ^= 0x8000000000000000;
339*1772Sjl139090 				_fp_pack_extword(pfpsd, &lusr, nrd);
340*1772Sjl139090 				if (inst.prec > 2) {		/* fnegq */
341*1772Sjl139090 				    _fp_unpack_extword(pfpsd, &lusr, nrs2+2);
342*1772Sjl139090 				    lusr ^= 0x0000000000000000;
343*1772Sjl139090 				    _fp_pack_extword(pfpsd, &lusr, nrd+2);
344*1772Sjl139090 				    FPUINFO_KSTAT(fpu_sim_fnegq);
345*1772Sjl139090 				} else {
346*1772Sjl139090 				    FPUINFO_KSTAT(fpu_sim_fnegd);
347*1772Sjl139090 				}
3480Sstevel@tonic-gate 			}
349*1772Sjl139090 			break;
350*1772Sjl139090 		case fadd:
351*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
352*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
353*1772Sjl139090 			_fp_add(pfpsd, &us1, &us2, &ud);
354*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
355*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fadds,
356*1772Sjl139090 			    fpu_sim_faddd, fpu_sim_faddq);
357*1772Sjl139090 			break;
358*1772Sjl139090 		case fsub:
359*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
360*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
361*1772Sjl139090 			_fp_sub(pfpsd, &us1, &us2, &ud);
362*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
363*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fsubs,
364*1772Sjl139090 			    fpu_sim_fsubd, fpu_sim_fsubq);
365*1772Sjl139090 			break;
366*1772Sjl139090 		case fmul:
367*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
368*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
369*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ud);
370*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
371*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fmuls,
372*1772Sjl139090 			    fpu_sim_fmuld, fpu_sim_fmulq);
373*1772Sjl139090 			break;
374*1772Sjl139090 		case fsmuld:
375*1772Sjl139090 			if ((fp_notp) && (inst.prec != 1))
376*1772Sjl139090 				return (ftt_unimplemented);
377*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
378*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
379*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ud);
380*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd,
381*1772Sjl139090 				(enum fp_op_type) ((int)inst.prec+1));
382*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fsmuld);
383*1772Sjl139090 			break;
384*1772Sjl139090 		case fdmulx:
385*1772Sjl139090 			if ((fp_notp) && (inst.prec != 2))
386*1772Sjl139090 				return (ftt_unimplemented);
387*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
388*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
389*1772Sjl139090 			_fp_mul(pfpsd, &us1, &us2, &ud);
390*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd,
391*1772Sjl139090 				(enum fp_op_type) ((int)inst.prec+1));
392*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fdmulx);
393*1772Sjl139090 			break;
394*1772Sjl139090 		case fdiv:
395*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
396*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
397*1772Sjl139090 			_fp_div(pfpsd, &us1, &us2, &ud);
398*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
399*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fdivs,
400*1772Sjl139090 			    fpu_sim_fdivd, fpu_sim_fdivq);
401*1772Sjl139090 			break;
402*1772Sjl139090 		case fcmp:
403*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
404*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
405*1772Sjl139090 			cc = _fp_compare(pfpsd, &us1, &us2, 0);
406*1772Sjl139090 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
407*1772Sjl139090 				switch (nfcc) {
408*1772Sjl139090 				case fcc_0:
409*1772Sjl139090 					fsr.fcc0 = cc;
410*1772Sjl139090 					break;
411*1772Sjl139090 				case fcc_1:
412*1772Sjl139090 					fsr.fcc1 = cc;
413*1772Sjl139090 					break;
414*1772Sjl139090 				case fcc_2:
415*1772Sjl139090 					fsr.fcc2 = cc;
416*1772Sjl139090 					break;
417*1772Sjl139090 				case fcc_3:
418*1772Sjl139090 					fsr.fcc3 = cc;
419*1772Sjl139090 					break;
420*1772Sjl139090 				}
421*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fcmps,
422*1772Sjl139090 			    fpu_sim_fcmpd, fpu_sim_fcmpq);
423*1772Sjl139090 			break;
424*1772Sjl139090 		case fcmpe:
425*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
426*1772Sjl139090 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
427*1772Sjl139090 			cc = _fp_compare(pfpsd, &us1, &us2, 1);
428*1772Sjl139090 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
429*1772Sjl139090 				switch (nfcc) {
430*1772Sjl139090 				case fcc_0:
431*1772Sjl139090 					fsr.fcc0 = cc;
432*1772Sjl139090 					break;
433*1772Sjl139090 				case fcc_1:
434*1772Sjl139090 					fsr.fcc1 = cc;
435*1772Sjl139090 					break;
436*1772Sjl139090 				case fcc_2:
437*1772Sjl139090 					fsr.fcc2 = cc;
438*1772Sjl139090 					break;
439*1772Sjl139090 				case fcc_3:
440*1772Sjl139090 					fsr.fcc3 = cc;
441*1772Sjl139090 					break;
442*1772Sjl139090 				}
443*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fcmpes,
444*1772Sjl139090 				fpu_sim_fcmped, fpu_sim_fcmpeq);
445*1772Sjl139090 			break;
446*1772Sjl139090 		case fsqrt:
447*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
448*1772Sjl139090 			_fp_sqrt(pfpsd, &us1, &ud);
449*1772Sjl139090 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
450*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fsqrts,
451*1772Sjl139090 			    fpu_sim_fsqrtd, fpu_sim_fsqrtq);
452*1772Sjl139090 			break;
453*1772Sjl139090 		case ftoi:
454*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
455*1772Sjl139090 			pfpsd->fp_direction = fp_tozero;
456*1772Sjl139090 			/* Force rounding toward zero. */
457*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_int32);
458*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fstoi,
459*1772Sjl139090 			    fpu_sim_fdtoi, fpu_sim_fqtoi);
460*1772Sjl139090 			break;
461*1772Sjl139090 		case ftoll:
462*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
463*1772Sjl139090 			pfpsd->fp_direction = fp_tozero;
464*1772Sjl139090 			/* Force rounding toward zero. */
465*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_int64);
466*1772Sjl139090 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fstox,
467*1772Sjl139090 			    fpu_sim_fdtox, fpu_sim_fqtox);
468*1772Sjl139090 			break;
469*1772Sjl139090 		case flltos:
470*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
471*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_single);
472*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fxtos);
473*1772Sjl139090 			break;
474*1772Sjl139090 		case flltod:
475*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
476*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_double);
477*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fxtod);
478*1772Sjl139090 			break;
479*1772Sjl139090 		case flltox:
480*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
481*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_extended);
482*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fxtoq);
483*1772Sjl139090 			break;
484*1772Sjl139090 		case fitos:
485*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
486*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_single);
487*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fitos);
488*1772Sjl139090 			break;
489*1772Sjl139090 		case fitod:
490*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
491*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_double);
492*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fitod);
493*1772Sjl139090 			break;
494*1772Sjl139090 		case fitox:
495*1772Sjl139090 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
496*1772Sjl139090 			_fp_pack(pfpsd, &us1, nrd, fp_op_extended);
497*1772Sjl139090 			FPUINFO_KSTAT(fpu_sim_fitoq);
498*1772Sjl139090 			break;
499*1772Sjl139090 		default:
500*1772Sjl139090 			return (ftt_unimplemented);
501*1772Sjl139090 		}
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 	fsr.cexc = pfpsd->fp_current_exceptions;
5040Sstevel@tonic-gate 	if (pfpsd->fp_current_exceptions) {	/* Exception(s) occurred. */
5050Sstevel@tonic-gate 		andexcep = pfpsd->fp_current_exceptions & fsr.tem;
5060Sstevel@tonic-gate 		if (andexcep != 0) {	/* Signal an IEEE SIGFPE here. */
5070Sstevel@tonic-gate 			if (andexcep & (1 << fp_invalid)) {
5080Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTINV;
5090Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_NV;
5100Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_overflow)) {
5110Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTOVF;
5120Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_OF;
5130Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_underflow)) {
5140Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTUND;
5150Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_UF;
5160Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_division)) {
5170Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTDIV;
5180Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_DZ;
5190Sstevel@tonic-gate 			} else if (andexcep & (1 << fp_inexact)) {
5200Sstevel@tonic-gate 				pfpsd->fp_trapcode = FPE_FLTRES;
5210Sstevel@tonic-gate 				fsr.cexc = FSR_CEXC_NX;
5220Sstevel@tonic-gate 			} else {
5230Sstevel@tonic-gate 				pfpsd->fp_trapcode = 0;
5240Sstevel@tonic-gate 			}
5250Sstevel@tonic-gate 			*pfsr = fsr;
5260Sstevel@tonic-gate 			return (ftt_ieee);
5270Sstevel@tonic-gate 		} else {	/* Just set accrued exception field. */
5280Sstevel@tonic-gate 			fsr.aexc |= pfpsd->fp_current_exceptions;
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 	*pfsr = fsr;
5320Sstevel@tonic-gate 	return (ftt_none);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
535*1772Sjl139090 
5360Sstevel@tonic-gate /*
5370Sstevel@tonic-gate  * fpu_vis_sim simulates fpu and vis instructions;
5380Sstevel@tonic-gate  * It can work with both real and pcb image registers.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate enum ftt_type
5410Sstevel@tonic-gate fpu_vis_sim(
5420Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
5430Sstevel@tonic-gate 	fp_inst_type	*pinst,	/* Address of FPU instruction to simulate */
5440Sstevel@tonic-gate 	struct regs	*pregs,	/* Pointer to PCB image of registers. */
5450Sstevel@tonic-gate 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write */
5460Sstevel@tonic-gate 	uint64_t	gsr,	/* Image of GSR to read */
5470Sstevel@tonic-gate 	uint32_t	inst)	/* The FPU instruction to simulate */
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	klwp_id_t lwp = ttolwp(curthread);
5500Sstevel@tonic-gate 	union {
5510Sstevel@tonic-gate 		uint32_t	i;
5520Sstevel@tonic-gate 		fp_inst_type	inst;
5530Sstevel@tonic-gate 	} fp;
5540Sstevel@tonic-gate 	kfpu_t *pfp = lwptofpu(lwp);
5550Sstevel@tonic-gate 	enum ftt_type ftt;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	fp.i = inst;
5580Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst;
5590Sstevel@tonic-gate 	if (fpu_exists) {
5600Sstevel@tonic-gate 		pfpsd->fp_current_read_freg = _fp_read_pfreg;
5610Sstevel@tonic-gate 		pfpsd->fp_current_write_freg = _fp_write_pfreg;
5620Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg = _fp_read_pdreg;
5630Sstevel@tonic-gate 		pfpsd->fp_current_write_dreg = _fp_write_pdreg;
5647Sdf157793 		pfpsd->fp_current_read_gsr = _fp_read_pgsr;
5657Sdf157793 		pfpsd->fp_current_write_gsr = _fp_write_pgsr;
5660Sstevel@tonic-gate 	} else {
5670Sstevel@tonic-gate 		pfpsd->fp_current_pfregs = pfp;
5680Sstevel@tonic-gate 		pfpsd->fp_current_read_freg = _fp_read_vfreg;
5690Sstevel@tonic-gate 		pfpsd->fp_current_write_freg = _fp_write_vfreg;
5700Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg = _fp_read_vdreg;
5710Sstevel@tonic-gate 		pfpsd->fp_current_write_dreg = _fp_write_vdreg;
5727Sdf157793 		pfpsd->fp_current_read_gsr = get_gsr;
5737Sdf157793 		pfpsd->fp_current_write_gsr = set_gsr;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
5770Sstevel@tonic-gate 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
5780Sstevel@tonic-gate 					pregs, (ulong_t *)pregs->r_sp, pfp);
5790Sstevel@tonic-gate 			return (ftt);
5800Sstevel@tonic-gate 	} else if ((fp.inst.hibits == 2) &&
581*1772Sjl139090 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35) ||
582*1772Sjl139090 	    (fp.inst.op3 == 0x37))) {
5830Sstevel@tonic-gate 		ftt =  _fp_fpu_simulator(pfpsd, fp.inst, pfsr, gsr);
5840Sstevel@tonic-gate 		if (ftt == ftt_none || ftt == ftt_ieee) {
5850Sstevel@tonic-gate 			pregs->r_pc = pregs->r_npc;
5860Sstevel@tonic-gate 			pregs->r_npc += 4;
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 		return (ftt);
5890Sstevel@tonic-gate 	} else {
5900Sstevel@tonic-gate 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs,
5910Sstevel@tonic-gate 		    (ulong_t *)pregs->r_sp, pfp);
5920Sstevel@tonic-gate 		return (ftt);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /*
5970Sstevel@tonic-gate  * fpu_simulator simulates FPU instructions only;
5980Sstevel@tonic-gate  * reads and writes FPU data registers directly.
5990Sstevel@tonic-gate  */
6000Sstevel@tonic-gate enum ftt_type
6010Sstevel@tonic-gate fpu_simulator(
6020Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
6030Sstevel@tonic-gate 	fp_inst_type	*pinst,	/* Address of FPU instruction to simulate */
6040Sstevel@tonic-gate 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write */
6050Sstevel@tonic-gate 	uint64_t	gsr,	/* Image of GSR to read */
6060Sstevel@tonic-gate 	uint32_t	inst)	/* The FPU instruction to simulate */
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	union {
6090Sstevel@tonic-gate 		uint32_t	i;
6100Sstevel@tonic-gate 		fp_inst_type	inst;
6110Sstevel@tonic-gate 	} fp;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	fp.i = inst;
6140Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst;
6150Sstevel@tonic-gate 	pfpsd->fp_current_read_freg = _fp_read_pfreg;
6160Sstevel@tonic-gate 	pfpsd->fp_current_write_freg = _fp_write_pfreg;
6170Sstevel@tonic-gate 	pfpsd->fp_current_read_dreg = _fp_read_pdreg;
6180Sstevel@tonic-gate 	pfpsd->fp_current_write_dreg = _fp_write_pdreg;
6197Sdf157793 	pfpsd->fp_current_read_gsr = _fp_read_pgsr;
6207Sdf157793 	pfpsd->fp_current_write_gsr = _fp_write_pgsr;
6210Sstevel@tonic-gate 	return (_fp_fpu_simulator(pfpsd, fp.inst, pfsr, gsr));
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate  * fp_emulator simulates FPU and CPU-FPU instructions; reads and writes FPU
6260Sstevel@tonic-gate  * data registers from image in pfpu.
6270Sstevel@tonic-gate  */
6280Sstevel@tonic-gate enum ftt_type
6290Sstevel@tonic-gate fp_emulator(
6300Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
6310Sstevel@tonic-gate 	fp_inst_type	*pinst,	/* Pointer to FPU instruction to simulate. */
6320Sstevel@tonic-gate 	struct regs	*pregs,	/* Pointer to PCB image of registers. */
6330Sstevel@tonic-gate 	void		*prw,	/* Pointer to locals and ins. */
6340Sstevel@tonic-gate 	kfpu_t		*pfpu)	/* Pointer to FPU register block. */
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	klwp_id_t lwp = ttolwp(curthread);
6370Sstevel@tonic-gate 	union {
6380Sstevel@tonic-gate 		uint32_t	i;
6390Sstevel@tonic-gate 		fp_inst_type	inst;
6400Sstevel@tonic-gate 	} fp;
6410Sstevel@tonic-gate 	enum ftt_type	ftt;
6420Sstevel@tonic-gate 	uint64_t gsr = get_gsr(pfpu);
6430Sstevel@tonic-gate 	kfpu_t *pfp = lwptofpu(lwp);
6440Sstevel@tonic-gate 	uint64_t	tfsr;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	tfsr = pfpu->fpu_fsr;
6470Sstevel@tonic-gate 	pfpsd->fp_current_pfregs = pfpu;
6480Sstevel@tonic-gate 	pfpsd->fp_current_read_freg = _fp_read_vfreg;
6490Sstevel@tonic-gate 	pfpsd->fp_current_write_freg = _fp_write_vfreg;
6500Sstevel@tonic-gate 	pfpsd->fp_current_read_dreg = _fp_read_vdreg;
6510Sstevel@tonic-gate 	pfpsd->fp_current_write_dreg = _fp_write_vdreg;
6527Sdf157793 	pfpsd->fp_current_read_gsr = get_gsr;
6537Sdf157793 	pfpsd->fp_current_write_gsr = set_gsr;
6540Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst; /* bad inst addr in case we trap */
6550Sstevel@tonic-gate 	ftt = _fp_read_inst((uint32_t *)pinst, &(fp.i), pfpsd);
6560Sstevel@tonic-gate 	if (ftt != ftt_none)
6570Sstevel@tonic-gate 		return (ftt);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	if ((fp.inst.hibits == 2) &&
660*1772Sjl139090 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35) ||
661*1772Sjl139090 	    (fp.inst.op3 == 0x37))) {
6620Sstevel@tonic-gate 		ftt = _fp_fpu_simulator(pfpsd, fp.inst, (fsr_type *)&tfsr, gsr);
6630Sstevel@tonic-gate 		/* Do not retry emulated instruction. */
6640Sstevel@tonic-gate 		pregs->r_pc = pregs->r_npc;
6650Sstevel@tonic-gate 		pregs->r_npc += 4;
6660Sstevel@tonic-gate 		pfpu->fpu_fsr = tfsr;
6670Sstevel@tonic-gate 		if (ftt != ftt_none) {
6680Sstevel@tonic-gate 			/*
6690Sstevel@tonic-gate 			 * Simulation generated an exception of some kind,
6700Sstevel@tonic-gate 			 * simulate the fp queue for a signal.
6710Sstevel@tonic-gate 			 */
6720Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_addr = (uint32_t *)pinst;
6730Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_instr = fp.i;
6740Sstevel@tonic-gate 			pfpu->fpu_qcnt = 1;
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 	} else if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
6770Sstevel@tonic-gate 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
6780Sstevel@tonic-gate 			    pregs, prw, pfp);
6790Sstevel@tonic-gate 	} else
6800Sstevel@tonic-gate 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs, prw, pfpu);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	if (ftt != ftt_none)
6830Sstevel@tonic-gate 		return (ftt);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * If we are single-stepping, don't emulate any more instructions.
6870Sstevel@tonic-gate 	 */
6880Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE)
6890Sstevel@tonic-gate 		return (ftt);
6900Sstevel@tonic-gate again:
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * now read next instruction and see if it can be emulated
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	pinst = (fp_inst_type *)pregs->r_pc;
6950Sstevel@tonic-gate 	pfpsd->fp_trapaddr = (caddr_t)pinst; /* bad inst addr in case we trap */
6960Sstevel@tonic-gate 	ftt = _fp_read_inst((uint32_t *)pinst, &(fp.i), pfpsd);
6970Sstevel@tonic-gate 	if (ftt != ftt_none)
6980Sstevel@tonic-gate 		return (ftt);
6990Sstevel@tonic-gate 	if ((fp.inst.hibits == 2) &&		/* fpops */
700*1772Sjl139090 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35) ||
701*1772Sjl139090 	    (fp.inst.op3 == 0x37))) {
7020Sstevel@tonic-gate 		ftt = _fp_fpu_simulator(pfpsd, fp.inst, (fsr_type *)&tfsr, gsr);
7030Sstevel@tonic-gate 		/* Do not retry emulated instruction. */
7040Sstevel@tonic-gate 		pfpu->fpu_fsr = tfsr;
7050Sstevel@tonic-gate 		pregs->r_pc = pregs->r_npc;
7060Sstevel@tonic-gate 		pregs->r_npc += 4;
7070Sstevel@tonic-gate 		if (ftt != ftt_none) {
7080Sstevel@tonic-gate 			/*
7090Sstevel@tonic-gate 			 * Simulation generated an exception of some kind,
7100Sstevel@tonic-gate 			 * simulate the fp queue for a signal.
7110Sstevel@tonic-gate 			 */
7120Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_addr = (uint32_t *)pinst;
7130Sstevel@tonic-gate 			pfpu->fpu_q->FQu.fpq.fpq_instr = fp.i;
7140Sstevel@tonic-gate 			pfpu->fpu_qcnt = 1;
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 	} else if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
7170Sstevel@tonic-gate 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
7180Sstevel@tonic-gate 						pregs, prw, pfp);
7190Sstevel@tonic-gate 	} else if (
7200Sstevel@tonic-gate 						/* rd %gsr */
7210Sstevel@tonic-gate 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x28) &&
7220Sstevel@tonic-gate 			(fp.inst.rs1 == 0x13)) ||
7230Sstevel@tonic-gate 						/* wr %gsr */
7240Sstevel@tonic-gate 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x30) &&
7250Sstevel@tonic-gate 			(fp.inst.rd == 0x13)) ||
7260Sstevel@tonic-gate 						/* movcc */
7270Sstevel@tonic-gate 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x2c) &&
7280Sstevel@tonic-gate 			(((fp.i>>18) & 0x1) == 0)) ||
7290Sstevel@tonic-gate 						/* fbpcc */
7300Sstevel@tonic-gate 	    ((fp.inst.hibits == 0) && (((fp.i>>22) & 0x7) == 5)) ||
7310Sstevel@tonic-gate 						/* fldst */
7320Sstevel@tonic-gate 	    ((fp.inst.hibits == 3) && ((fp.inst.op3 & 0x38) == 0x20)) ||
7330Sstevel@tonic-gate 						/* fbcc */
7340Sstevel@tonic-gate 	    ((fp.inst.hibits == 0) && (((fp.i>>22) & 0x7) == 6))) {
7350Sstevel@tonic-gate 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs, prw, pfpu);
7360Sstevel@tonic-gate 	} else
7370Sstevel@tonic-gate 		return (ftt);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (ftt != ftt_none)
7400Sstevel@tonic-gate 		return (ftt);
7410Sstevel@tonic-gate 	else
7420Sstevel@tonic-gate 		goto again;
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * FPU simulator global kstat data
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate struct fpustat_kstat fpustat = {
7490Sstevel@tonic-gate 	{ "fpu_ieee_traps",		KSTAT_DATA_UINT64 },
7500Sstevel@tonic-gate 	{ "fpu_unfinished_traps",	KSTAT_DATA_UINT64 },
7510Sstevel@tonic-gate 	{ "fpu_unimplemented",		KSTAT_DATA_UINT64 },
7520Sstevel@tonic-gate };
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate kstat_t *fpu_kstat = NULL;
7550Sstevel@tonic-gate kstat_t *fpuinfo_kstat = NULL;
7560Sstevel@tonic-gate kstat_t *visinfo_kstat = NULL;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate void
7590Sstevel@tonic-gate fp_kstat_init(void)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	const uint_t fpustat_ndata = sizeof (fpustat) / sizeof (kstat_named_t);
7620Sstevel@tonic-gate 	const uint_t fpuinfo_ndata = sizeof (fpuinfo) / sizeof (kstat_named_t);
7630Sstevel@tonic-gate 	const uint_t visinfo_ndata = sizeof (visinfo) /sizeof (kstat_named_t);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	ASSERT(fpu_kstat == NULL);
7660Sstevel@tonic-gate 	if ((fpu_kstat = kstat_create("unix", 0, "fpu_traps", "misc",
7670Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, fpustat_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
7680Sstevel@tonic-gate 		cmn_err(CE_WARN, "CPU%d: kstat_create for fpu_traps failed",
7690Sstevel@tonic-gate 		    CPU->cpu_id);
7700Sstevel@tonic-gate 	} else {
7710Sstevel@tonic-gate 		fpu_kstat->ks_data = (void *)&fpustat;
7720Sstevel@tonic-gate 		kstat_install(fpu_kstat);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	ASSERT(fpuinfo_kstat == NULL);
7760Sstevel@tonic-gate 	if ((fpuinfo_kstat = kstat_create("unix", 0, "fpu_info", "misc",
7770Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, fpuinfo_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
7780Sstevel@tonic-gate 		cmn_err(CE_WARN, "CPU%d: kstat_create for fpu_info failed",
7790Sstevel@tonic-gate 		    CPU->cpu_id);
7800Sstevel@tonic-gate 	} else {
7810Sstevel@tonic-gate 		fpuinfo_kstat->ks_data = (void *)&fpuinfo;
7820Sstevel@tonic-gate 		kstat_install(fpuinfo_kstat);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	ASSERT(visinfo_kstat == NULL);
7850Sstevel@tonic-gate 	if ((visinfo_kstat = kstat_create("unix", 0, "vis_info", "misc",
7860Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, visinfo_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
7870Sstevel@tonic-gate 		cmn_err(CE_WARN, "CPU%d: kstat_create for vis_info failed",
7880Sstevel@tonic-gate 		    CPU->cpu_id);
7890Sstevel@tonic-gate 	} else {
7900Sstevel@tonic-gate 		visinfo_kstat->ks_data = (void *)&visinfo;
7910Sstevel@tonic-gate 		kstat_install(visinfo_kstat);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate void
7960Sstevel@tonic-gate fp_kstat_update(enum ftt_type ftt)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	ASSERT((ftt == ftt_ieee) || (ftt == ftt_unfinished) ||
7990Sstevel@tonic-gate 	    (ftt == ftt_unimplemented));
8000Sstevel@tonic-gate 	if (ftt == ftt_ieee)
8010Sstevel@tonic-gate 		atomic_add_64(&fpustat.fpu_ieee_traps.value.ui64, 1);
8020Sstevel@tonic-gate 	else if (ftt == ftt_unfinished)
8030Sstevel@tonic-gate 		atomic_add_64(&fpustat.fpu_unfinished_traps.value.ui64, 1);
8040Sstevel@tonic-gate 	else if (ftt == ftt_unimplemented)
8050Sstevel@tonic-gate 		atomic_add_64(&fpustat.fpu_unimplemented_traps.value.ui64, 1);
8060Sstevel@tonic-gate }
807