xref: /titanic_52/usr/src/uts/sparc/fpu/fpu_simulator.c (revision 342440ec94087b8c751c580ab9ed6c693d31d418)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /* Main procedures for sparc FPU simulator. */
29 
30 #include <sys/fpu/fpu_simulator.h>
31 #include <sys/fpu/globals.h>
32 #include <sys/fpu/fpusystm.h>
33 #include <sys/proc.h>
34 #include <sys/signal.h>
35 #include <sys/siginfo.h>
36 #include <sys/thread.h>
37 #include <sys/cpuvar.h>
38 #include <sys/cmn_err.h>
39 #include <sys/atomic.h>
40 #include <sys/privregs.h>
41 #include <sys/vis_simulator.h>
42 
43 #define	FPUINFO_KSTAT(opcode)	{					\
44 	extern void __dtrace_probe___fpuinfo_##opcode(uint64_t *);	\
45 	uint64_t *stataddr = &fpuinfo.opcode.value.ui64;		\
46 	__dtrace_probe___fpuinfo_##opcode(stataddr);			\
47 	atomic_add_64(&fpuinfo.opcode.value.ui64, 1);			\
48 }
49 
50 #define	FPUINFO_KSTAT_PREC(prec, kstat_s, kstat_d, kstat_q)		\
51 	if (prec < 2) {							\
52 		FPUINFO_KSTAT(kstat_s);					\
53 	} else if (prec == 2) {						\
54 		FPUINFO_KSTAT(kstat_d);					\
55 	} else {							\
56 		FPUINFO_KSTAT(kstat_q);					\
57 	}
58 
59 /*
60  * FPU simulator global kstat data
61  */
62 struct fpuinfo_kstat fpuinfo = {
63 	{ "fpu_sim_fmovs",		KSTAT_DATA_UINT64},
64 	{ "fpu_sim_fmovd",		KSTAT_DATA_UINT64},
65 	{ "fpu_sim_fmovq",		KSTAT_DATA_UINT64},
66 	{ "fpu_sim_fnegs",		KSTAT_DATA_UINT64},
67 	{ "fpu_sim_fnegd",		KSTAT_DATA_UINT64},
68 	{ "fpu_sim_fnegq",		KSTAT_DATA_UINT64},
69 	{ "fpu_sim_fabss",		KSTAT_DATA_UINT64},
70 	{ "fpu_sim_fabsd",		KSTAT_DATA_UINT64},
71 	{ "fpu_sim_fabsq",		KSTAT_DATA_UINT64},
72 	{ "fpu_sim_fsqrts",		KSTAT_DATA_UINT64},
73 	{ "fpu_sim_fsqrtd",		KSTAT_DATA_UINT64},
74 	{ "fpu_sim_fsqrtq",		KSTAT_DATA_UINT64},
75 	{ "fpu_sim_fadds",		KSTAT_DATA_UINT64},
76 	{ "fpu_sim_faddd",		KSTAT_DATA_UINT64},
77 	{ "fpu_sim_faddq",		KSTAT_DATA_UINT64},
78 	{ "fpu_sim_fsubs",		KSTAT_DATA_UINT64},
79 	{ "fpu_sim_fsubd",		KSTAT_DATA_UINT64},
80 	{ "fpu_sim_fsubq",		KSTAT_DATA_UINT64},
81 	{ "fpu_sim_fmuls",		KSTAT_DATA_UINT64},
82 	{ "fpu_sim_fmuld",		KSTAT_DATA_UINT64},
83 	{ "fpu_sim_fmulq",		KSTAT_DATA_UINT64},
84 	{ "fpu_sim_fdivs",		KSTAT_DATA_UINT64},
85 	{ "fpu_sim_fdivd",		KSTAT_DATA_UINT64},
86 	{ "fpu_sim_fdivq",		KSTAT_DATA_UINT64},
87 	{ "fpu_sim_fcmps",		KSTAT_DATA_UINT64},
88 	{ "fpu_sim_fcmpd",		KSTAT_DATA_UINT64},
89 	{ "fpu_sim_fcmpq",		KSTAT_DATA_UINT64},
90 	{ "fpu_sim_fcmpes",		KSTAT_DATA_UINT64},
91 	{ "fpu_sim_fcmped",		KSTAT_DATA_UINT64},
92 	{ "fpu_sim_fcmpeq",		KSTAT_DATA_UINT64},
93 	{ "fpu_sim_fsmuld",		KSTAT_DATA_UINT64},
94 	{ "fpu_sim_fdmulx",		KSTAT_DATA_UINT64},
95 	{ "fpu_sim_fstox",		KSTAT_DATA_UINT64},
96 	{ "fpu_sim_fdtox",		KSTAT_DATA_UINT64},
97 	{ "fpu_sim_fqtox",		KSTAT_DATA_UINT64},
98 	{ "fpu_sim_fxtos",		KSTAT_DATA_UINT64},
99 	{ "fpu_sim_fxtod",		KSTAT_DATA_UINT64},
100 	{ "fpu_sim_fxtoq",		KSTAT_DATA_UINT64},
101 	{ "fpu_sim_fitos",		KSTAT_DATA_UINT64},
102 	{ "fpu_sim_fitod",		KSTAT_DATA_UINT64},
103 	{ "fpu_sim_fitoq",		KSTAT_DATA_UINT64},
104 	{ "fpu_sim_fstoi",		KSTAT_DATA_UINT64},
105 	{ "fpu_sim_fdtoi",		KSTAT_DATA_UINT64},
106 	{ "fpu_sim_fqtoi",		KSTAT_DATA_UINT64},
107 	{ "fpu_sim_fmovcc",		KSTAT_DATA_UINT64},
108 	{ "fpu_sim_fmovr",		KSTAT_DATA_UINT64},
109 	{ "fpu_sim_fmadds",		KSTAT_DATA_UINT64},
110 	{ "fpu_sim_fmaddd",		KSTAT_DATA_UINT64},
111 	{ "fpu_sim_fmsubs",		KSTAT_DATA_UINT64},
112 	{ "fpu_sim_fmsubd",		KSTAT_DATA_UINT64},
113 	{ "fpu_sim_fnmadds",		KSTAT_DATA_UINT64},
114 	{ "fpu_sim_fnmaddd",		KSTAT_DATA_UINT64},
115 	{ "fpu_sim_fnmsubs",		KSTAT_DATA_UINT64},
116 	{ "fpu_sim_fnmsubd",		KSTAT_DATA_UINT64},
117 	{ "fpu_sim_invalid",		KSTAT_DATA_UINT64},
118 };
119 
120 struct visinfo_kstat visinfo = {
121 	{ "vis_edge8",		KSTAT_DATA_UINT64},
122 	{ "vis_edge8n",		KSTAT_DATA_UINT64},
123 	{ "vis_edge8l",		KSTAT_DATA_UINT64},
124 	{ "vis_edge8ln",	KSTAT_DATA_UINT64},
125 	{ "vis_edge16",		KSTAT_DATA_UINT64},
126 	{ "vis_edge16n",	KSTAT_DATA_UINT64},
127 	{ "vis_edge16l",	KSTAT_DATA_UINT64},
128 	{ "vis_edge16ln",	KSTAT_DATA_UINT64},
129 	{ "vis_edge32",		KSTAT_DATA_UINT64},
130 	{ "vis_edge32n",	KSTAT_DATA_UINT64},
131 	{ "vis_edge32l",	KSTAT_DATA_UINT64},
132 	{ "vis_edge32ln",	KSTAT_DATA_UINT64},
133 	{ "vis_array8",		KSTAT_DATA_UINT64},
134 	{ "vis_array16",	KSTAT_DATA_UINT64},
135 	{ "vis_array32",	KSTAT_DATA_UINT64},
136 	{ "vis_bmask",		KSTAT_DATA_UINT64},
137 	{ "vis_fcmple16",	KSTAT_DATA_UINT64},
138 	{ "vis_fcmpne16",	KSTAT_DATA_UINT64},
139 	{ "vis_fcmpgt16",	KSTAT_DATA_UINT64},
140 	{ "vis_fcmpeq16",	KSTAT_DATA_UINT64},
141 	{ "vis_fcmple32",	KSTAT_DATA_UINT64},
142 	{ "vis_fcmpne32",	KSTAT_DATA_UINT64},
143 	{ "vis_fcmpgt32",	KSTAT_DATA_UINT64},
144 	{ "vis_fcmpeq32",	KSTAT_DATA_UINT64},
145 	{ "vis_fmul8x16",	KSTAT_DATA_UINT64},
146 	{ "vis_fmul8x16au",	KSTAT_DATA_UINT64},
147 	{ "vis_fmul8x16al",	KSTAT_DATA_UINT64},
148 	{ "vis_fmul8sux16",	KSTAT_DATA_UINT64},
149 	{ "vis_fmul8ulx16",	KSTAT_DATA_UINT64},
150 	{ "vis_fmuld8sux16",	KSTAT_DATA_UINT64},
151 	{ "vis_fmuld8ulx16",	KSTAT_DATA_UINT64},
152 	{ "vis_fpack16",	KSTAT_DATA_UINT64},
153 	{ "vis_fpack32",	KSTAT_DATA_UINT64},
154 	{ "vis_fpackfix",	KSTAT_DATA_UINT64},
155 	{ "vis_fexpand",	KSTAT_DATA_UINT64},
156 	{ "vis_fpmerge",	KSTAT_DATA_UINT64},
157 	{ "vis_pdist",		KSTAT_DATA_UINT64},
158 	{ "vis_bshuffle",	KSTAT_DATA_UINT64},
159 
160 };
161 
162 /* PUBLIC FUNCTIONS */
163 
164 int fp_notp = 1;	/* fp checking not a problem */
165 
166 /* ARGSUSED */
167 static enum ftt_type
168 _fp_fpu_simulator(
169 	fp_simd_type	*pfpsd,	/* Pointer to fpu simulator data */
170 	fp_inst_type	inst,	/* FPU instruction to simulate. */
171 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write. */
172 	uint64_t	gsr)	/* Image of GSR to read */
173 {
174 	unpacked	us1, us2, ud;	/* Unpacked operands and result. */
175 	uint32_t	nrs1, nrs2, nrd; /* Register number fields. */
176 	uint32_t	usr, andexcep;
177 	fsr_type	fsr;
178 	enum fcc_type	cc;
179 	uint32_t	nfcc;		/* fcc number field. */
180 	uint64_t	lusr;
181 
182 	nrs1 = inst.rs1;
183 	nrs2 = inst.rs2;
184 	nrd = inst.rd;
185 	fsr = *pfsr;
186 	pfpsd->fp_current_exceptions = 0;	/* Init current exceptions. */
187 	pfpsd->fp_fsrtem    = fsr.tem;		/* Obtain fsr's tem */
188 	/*
189 	 * Obtain rounding direction and precision
190 	 */
191 	pfpsd->fp_direction = GSR_IM(gsr) ? GSR_IRND(gsr) : fsr.rnd;
192 	pfpsd->fp_precision = fsr.rnp;
193 
194 	if (inst.op3 == 0x37) { /* IMPDEP2B FMA-fused opcode */
195 		fp_fma_inst_type *fma_inst;
196 		uint32_t	nrs3;
197 		unpacked	us3;
198 		unpacked	ust;
199 		fma_inst = (fp_fma_inst_type *) &inst;
200 		nrs2 = fma_inst->rs2;
201 		nrs3 = fma_inst->rs3;
202 		switch (fma_inst->var) {
203 		case fmadd:
204 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
205 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
206 			_fp_mul(pfpsd, &us1, &us2, &ust);
207 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
208 				_fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
209 				_fp_add(pfpsd, &ust, &us3, &ud);
210 				_fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
211 			}
212 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fmadds,
213 				fpu_sim_fmaddd, fpu_sim_invalid);
214 			break;
215 		case fmsub:
216 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
217 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
218 			_fp_mul(pfpsd, &us1, &us2, &ust);
219 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
220 				_fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
221 				_fp_sub(pfpsd, &ust, &us3, &ud);
222 				_fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
223 			}
224 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fmsubs,
225 				fpu_sim_fmsubd, fpu_sim_invalid);
226 			break;
227 		case fnmadd:
228 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
229 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
230 			_fp_mul(pfpsd, &us1, &us2, &ust);
231 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
232 				_fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
233 				if (ust.fpclass != fp_quiet &&
234 				    ust.fpclass != fp_signaling)
235 					ust.sign ^= 1;
236 				_fp_sub(pfpsd, &ust, &us3, &ud);
237 				_fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
238 			}
239 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fnmadds,
240 				fpu_sim_fnmaddd, fpu_sim_invalid);
241 			break;
242 		case fnmsub:
243 			_fp_unpack(pfpsd, &us1, nrs1, fma_inst->sz);
244 			_fp_unpack(pfpsd, &us2, nrs2, fma_inst->sz);
245 			_fp_mul(pfpsd, &us1, &us2, &ust);
246 			if ((pfpsd->fp_current_exceptions & fsr.tem) == 0) {
247 				_fp_unpack(pfpsd, &us3, nrs3, fma_inst->sz);
248 				if (ust.fpclass != fp_quiet &&
249 				    ust.fpclass != fp_signaling)
250 					ust.sign ^= 1;
251 				_fp_add(pfpsd, &ust, &us3, &ud);
252 				_fp_pack(pfpsd, &ud, nrd, fma_inst->sz);
253 			}
254 			FPUINFO_KSTAT_PREC(fma_inst->sz, fpu_sim_fnmsubs,
255 				fpu_sim_fnmsubd, fpu_sim_invalid);
256 		}
257 	} else {
258 		nfcc = nrd & 0x3;
259 		if (inst.op3 == 0x35) {		/* fpop2 */
260 			fsr.cexc = 0;
261 			*pfsr = fsr;
262 			if ((inst.opcode & 0xf) == 0) {
263 				if ((fp_notp) && (inst.prec == 0))
264 					return (ftt_unimplemented);
265 				FPUINFO_KSTAT(fpu_sim_fmovcc);
266 				return (fmovcc(pfpsd, inst, pfsr)); /* fmovcc */
267 			} else if ((inst.opcode & 0x7) == 1) {
268 				if ((fp_notp) && (inst.prec == 0))
269 					return (ftt_unimplemented);
270 				FPUINFO_KSTAT(fpu_sim_fmovr);
271 				return (fmovr(pfpsd, inst));	/* fmovr */
272 			}
273 		}
274 		/* ibit not valid for fpop1 instructions */
275 		if ((fp_notp) && (inst.ibit != 0))
276 			return (ftt_unimplemented);
277 		if ((fp_notp) && (inst.prec == 0)) { /* fxto[sdq], fito[sdq] */
278 			if ((inst.opcode != flltos) &&
279 			    (inst.opcode != flltod) &&
280 			    (inst.opcode != flltox) &&
281 			    (inst.opcode != fitos) &&
282 			    (inst.opcode != fitod) &&
283 			    (inst.opcode != fitox)) {
284 				return (ftt_unimplemented);
285 			}
286 		}
287 		switch (inst.opcode) {
288 		case fmovs:		/* also covers fmovd, fmovq */
289 			if (inst.prec < 2) {	/* fmovs */
290 				_fp_unpack_word(pfpsd, &usr, nrs2);
291 				_fp_pack_word(pfpsd, &usr, nrd);
292 				FPUINFO_KSTAT(fpu_sim_fmovs);
293 			} else {		/* fmovd */
294 				_fp_unpack_extword(pfpsd, &lusr, nrs2);
295 				_fp_pack_extword(pfpsd, &lusr, nrd);
296 				if (inst.prec > 2) {		/* fmovq */
297 				    _fp_unpack_extword(pfpsd, &lusr, nrs2+2);
298 				    _fp_pack_extword(pfpsd, &lusr, nrd+2);
299 				    FPUINFO_KSTAT(fpu_sim_fmovq);
300 				} else {
301 				    FPUINFO_KSTAT(fpu_sim_fmovd);
302 				}
303 			}
304 			break;
305 		case fabss:		/* also covers fabsd, fabsq */
306 			if (inst.prec < 2) {	/* fabss */
307 				_fp_unpack_word(pfpsd, &usr, nrs2);
308 				usr &= 0x7fffffff;
309 				_fp_pack_word(pfpsd, &usr, nrd);
310 				FPUINFO_KSTAT(fpu_sim_fabss);
311 			} else {		/* fabsd */
312 				_fp_unpack_extword(pfpsd, &lusr, nrs2);
313 				lusr &= 0x7fffffffffffffff;
314 				_fp_pack_extword(pfpsd, &lusr, nrd);
315 				if (inst.prec > 2) {		/* fabsq */
316 				    _fp_unpack_extword(pfpsd, &lusr, nrs2+2);
317 				    _fp_pack_extword(pfpsd, &lusr, nrd+2);
318 				    FPUINFO_KSTAT(fpu_sim_fabsq);
319 				} else {
320 				    FPUINFO_KSTAT(fpu_sim_fabsd);
321 				}
322 			}
323 			break;
324 		case fnegs:		/* also covers fnegd, fnegq */
325 			if (inst.prec < 2) {	/* fnegs */
326 				_fp_unpack_word(pfpsd, &usr, nrs2);
327 				usr ^= 0x80000000;
328 				_fp_pack_word(pfpsd, &usr, nrd);
329 				FPUINFO_KSTAT(fpu_sim_fnegs);
330 			} else {		/* fnegd */
331 				_fp_unpack_extword(pfpsd, &lusr, nrs2);
332 				lusr ^= 0x8000000000000000;
333 				_fp_pack_extword(pfpsd, &lusr, nrd);
334 				if (inst.prec > 2) {		/* fnegq */
335 				    _fp_unpack_extword(pfpsd, &lusr, nrs2+2);
336 				    lusr ^= 0x0000000000000000;
337 				    _fp_pack_extword(pfpsd, &lusr, nrd+2);
338 				    FPUINFO_KSTAT(fpu_sim_fnegq);
339 				} else {
340 				    FPUINFO_KSTAT(fpu_sim_fnegd);
341 				}
342 			}
343 			break;
344 		case fadd:
345 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
346 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
347 			_fp_add(pfpsd, &us1, &us2, &ud);
348 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
349 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fadds,
350 			    fpu_sim_faddd, fpu_sim_faddq);
351 			break;
352 		case fsub:
353 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
354 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
355 			_fp_sub(pfpsd, &us1, &us2, &ud);
356 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
357 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fsubs,
358 			    fpu_sim_fsubd, fpu_sim_fsubq);
359 			break;
360 		case fmul:
361 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
362 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
363 			_fp_mul(pfpsd, &us1, &us2, &ud);
364 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
365 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fmuls,
366 			    fpu_sim_fmuld, fpu_sim_fmulq);
367 			break;
368 		case fsmuld:
369 			if ((fp_notp) && (inst.prec != 1))
370 				return (ftt_unimplemented);
371 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
372 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
373 			_fp_mul(pfpsd, &us1, &us2, &ud);
374 			_fp_pack(pfpsd, &ud, nrd,
375 				(enum fp_op_type) ((int)inst.prec+1));
376 			FPUINFO_KSTAT(fpu_sim_fsmuld);
377 			break;
378 		case fdmulx:
379 			if ((fp_notp) && (inst.prec != 2))
380 				return (ftt_unimplemented);
381 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
382 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
383 			_fp_mul(pfpsd, &us1, &us2, &ud);
384 			_fp_pack(pfpsd, &ud, nrd,
385 				(enum fp_op_type) ((int)inst.prec+1));
386 			FPUINFO_KSTAT(fpu_sim_fdmulx);
387 			break;
388 		case fdiv:
389 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
390 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
391 			_fp_div(pfpsd, &us1, &us2, &ud);
392 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
393 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fdivs,
394 			    fpu_sim_fdivd, fpu_sim_fdivq);
395 			break;
396 		case fcmp:
397 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
398 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
399 			cc = _fp_compare(pfpsd, &us1, &us2, 0);
400 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
401 				switch (nfcc) {
402 				case fcc_0:
403 					fsr.fcc0 = cc;
404 					break;
405 				case fcc_1:
406 					fsr.fcc1 = cc;
407 					break;
408 				case fcc_2:
409 					fsr.fcc2 = cc;
410 					break;
411 				case fcc_3:
412 					fsr.fcc3 = cc;
413 					break;
414 				}
415 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fcmps,
416 			    fpu_sim_fcmpd, fpu_sim_fcmpq);
417 			break;
418 		case fcmpe:
419 			_fp_unpack(pfpsd, &us1, nrs1, inst.prec);
420 			_fp_unpack(pfpsd, &us2, nrs2, inst.prec);
421 			cc = _fp_compare(pfpsd, &us1, &us2, 1);
422 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
423 				switch (nfcc) {
424 				case fcc_0:
425 					fsr.fcc0 = cc;
426 					break;
427 				case fcc_1:
428 					fsr.fcc1 = cc;
429 					break;
430 				case fcc_2:
431 					fsr.fcc2 = cc;
432 					break;
433 				case fcc_3:
434 					fsr.fcc3 = cc;
435 					break;
436 				}
437 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fcmpes,
438 				fpu_sim_fcmped, fpu_sim_fcmpeq);
439 			break;
440 		case fsqrt:
441 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
442 			_fp_sqrt(pfpsd, &us1, &ud);
443 			_fp_pack(pfpsd, &ud, nrd, inst.prec);
444 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fsqrts,
445 			    fpu_sim_fsqrtd, fpu_sim_fsqrtq);
446 			break;
447 		case ftoi:
448 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
449 			pfpsd->fp_direction = fp_tozero;
450 			/* Force rounding toward zero. */
451 			_fp_pack(pfpsd, &us1, nrd, fp_op_int32);
452 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fstoi,
453 			    fpu_sim_fdtoi, fpu_sim_fqtoi);
454 			break;
455 		case ftoll:
456 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
457 			pfpsd->fp_direction = fp_tozero;
458 			/* Force rounding toward zero. */
459 			_fp_pack(pfpsd, &us1, nrd, fp_op_int64);
460 			FPUINFO_KSTAT_PREC(inst.prec, fpu_sim_fstox,
461 			    fpu_sim_fdtox, fpu_sim_fqtox);
462 			break;
463 		case flltos:
464 			_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
465 			_fp_pack(pfpsd, &us1, nrd, fp_op_single);
466 			FPUINFO_KSTAT(fpu_sim_fxtos);
467 			break;
468 		case flltod:
469 			_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
470 			_fp_pack(pfpsd, &us1, nrd, fp_op_double);
471 			FPUINFO_KSTAT(fpu_sim_fxtod);
472 			break;
473 		case flltox:
474 			_fp_unpack(pfpsd, &us1, nrs2, fp_op_int64);
475 			_fp_pack(pfpsd, &us1, nrd, fp_op_extended);
476 			FPUINFO_KSTAT(fpu_sim_fxtoq);
477 			break;
478 		case fitos:
479 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
480 			_fp_pack(pfpsd, &us1, nrd, fp_op_single);
481 			FPUINFO_KSTAT(fpu_sim_fitos);
482 			break;
483 		case fitod:
484 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
485 			_fp_pack(pfpsd, &us1, nrd, fp_op_double);
486 			FPUINFO_KSTAT(fpu_sim_fitod);
487 			break;
488 		case fitox:
489 			_fp_unpack(pfpsd, &us1, nrs2, inst.prec);
490 			_fp_pack(pfpsd, &us1, nrd, fp_op_extended);
491 			FPUINFO_KSTAT(fpu_sim_fitoq);
492 			break;
493 		default:
494 			return (ftt_unimplemented);
495 		}
496 	}
497 	fsr.cexc = pfpsd->fp_current_exceptions;
498 	if (pfpsd->fp_current_exceptions) {	/* Exception(s) occurred. */
499 		andexcep = pfpsd->fp_current_exceptions & fsr.tem;
500 		if (andexcep != 0) {	/* Signal an IEEE SIGFPE here. */
501 			if (andexcep & (1 << fp_invalid)) {
502 				pfpsd->fp_trapcode = FPE_FLTINV;
503 				fsr.cexc = FSR_CEXC_NV;
504 			} else if (andexcep & (1 << fp_overflow)) {
505 				pfpsd->fp_trapcode = FPE_FLTOVF;
506 				fsr.cexc = FSR_CEXC_OF;
507 			} else if (andexcep & (1 << fp_underflow)) {
508 				pfpsd->fp_trapcode = FPE_FLTUND;
509 				fsr.cexc = FSR_CEXC_UF;
510 			} else if (andexcep & (1 << fp_division)) {
511 				pfpsd->fp_trapcode = FPE_FLTDIV;
512 				fsr.cexc = FSR_CEXC_DZ;
513 			} else if (andexcep & (1 << fp_inexact)) {
514 				pfpsd->fp_trapcode = FPE_FLTRES;
515 				fsr.cexc = FSR_CEXC_NX;
516 			} else {
517 				pfpsd->fp_trapcode = 0;
518 			}
519 			*pfsr = fsr;
520 			return (ftt_ieee);
521 		} else {	/* Just set accrued exception field. */
522 			fsr.aexc |= pfpsd->fp_current_exceptions;
523 		}
524 	}
525 	*pfsr = fsr;
526 	return (ftt_none);
527 }
528 
529 
530 /*
531  * fpu_vis_sim simulates fpu and vis instructions;
532  * It can work with both real and pcb image registers.
533  */
534 enum ftt_type
535 fpu_vis_sim(
536 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
537 	fp_inst_type	*pinst,	/* Address of FPU instruction to simulate */
538 	struct regs	*pregs,	/* Pointer to PCB image of registers. */
539 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write */
540 	uint64_t	gsr,	/* Image of GSR to read */
541 	uint32_t	inst)	/* The FPU instruction to simulate */
542 {
543 	klwp_id_t lwp = ttolwp(curthread);
544 	union {
545 		uint32_t	i;
546 		fp_inst_type	inst;
547 	} fp;
548 	kfpu_t *pfp = lwptofpu(lwp);
549 	enum ftt_type ftt;
550 
551 	fp.i = inst;
552 	pfpsd->fp_trapaddr = (caddr_t)pinst;
553 	if (fpu_exists) {
554 		pfpsd->fp_current_read_freg = _fp_read_pfreg;
555 		pfpsd->fp_current_write_freg = _fp_write_pfreg;
556 		pfpsd->fp_current_read_dreg = _fp_read_pdreg;
557 		pfpsd->fp_current_write_dreg = _fp_write_pdreg;
558 		pfpsd->fp_current_read_gsr = _fp_read_pgsr;
559 		pfpsd->fp_current_write_gsr = _fp_write_pgsr;
560 	} else {
561 		pfpsd->fp_current_pfregs = pfp;
562 		pfpsd->fp_current_read_freg = _fp_read_vfreg;
563 		pfpsd->fp_current_write_freg = _fp_write_vfreg;
564 		pfpsd->fp_current_read_dreg = _fp_read_vdreg;
565 		pfpsd->fp_current_write_dreg = _fp_write_vdreg;
566 		pfpsd->fp_current_read_gsr = get_gsr;
567 		pfpsd->fp_current_write_gsr = set_gsr;
568 	}
569 
570 	if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
571 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
572 					pregs, (ulong_t *)pregs->r_sp, pfp);
573 			return (ftt);
574 	} else if ((fp.inst.hibits == 2) &&
575 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35) ||
576 	    (fp.inst.op3 == 0x37))) {
577 		ftt =  _fp_fpu_simulator(pfpsd, fp.inst, pfsr, gsr);
578 		if (ftt == ftt_none || ftt == ftt_ieee) {
579 			pregs->r_pc = pregs->r_npc;
580 			pregs->r_npc += 4;
581 		}
582 		return (ftt);
583 	} else {
584 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs,
585 		    (ulong_t *)pregs->r_sp, pfp);
586 		return (ftt);
587 	}
588 }
589 
590 /*
591  * fpu_simulator simulates FPU instructions only;
592  * reads and writes FPU data registers directly.
593  */
594 enum ftt_type
595 fpu_simulator(
596 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
597 	fp_inst_type	*pinst,	/* Address of FPU instruction to simulate */
598 	fsr_type	*pfsr,	/* Pointer to image of FSR to read and write */
599 	uint64_t	gsr,	/* Image of GSR to read */
600 	uint32_t	inst)	/* The FPU instruction to simulate */
601 {
602 	union {
603 		uint32_t	i;
604 		fp_inst_type	inst;
605 	} fp;
606 
607 	fp.i = inst;
608 	pfpsd->fp_trapaddr = (caddr_t)pinst;
609 	pfpsd->fp_current_read_freg = _fp_read_pfreg;
610 	pfpsd->fp_current_write_freg = _fp_write_pfreg;
611 	pfpsd->fp_current_read_dreg = _fp_read_pdreg;
612 	pfpsd->fp_current_write_dreg = _fp_write_pdreg;
613 	pfpsd->fp_current_read_gsr = _fp_read_pgsr;
614 	pfpsd->fp_current_write_gsr = _fp_write_pgsr;
615 	return (_fp_fpu_simulator(pfpsd, fp.inst, pfsr, gsr));
616 }
617 
618 /*
619  * fp_emulator simulates FPU and CPU-FPU instructions; reads and writes FPU
620  * data registers from image in pfpu.
621  */
622 enum ftt_type
623 fp_emulator(
624 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
625 	fp_inst_type	*pinst,	/* Pointer to FPU instruction to simulate. */
626 	struct regs	*pregs,	/* Pointer to PCB image of registers. */
627 	void		*prw,	/* Pointer to locals and ins. */
628 	kfpu_t		*pfpu)	/* Pointer to FPU register block. */
629 {
630 	klwp_id_t lwp = ttolwp(curthread);
631 	union {
632 		uint32_t	i;
633 		fp_inst_type	inst;
634 	} fp;
635 	enum ftt_type	ftt;
636 	uint64_t gsr = get_gsr(pfpu);
637 	kfpu_t *pfp = lwptofpu(lwp);
638 	uint64_t	tfsr;
639 
640 	tfsr = pfpu->fpu_fsr;
641 	pfpsd->fp_current_pfregs = pfpu;
642 	pfpsd->fp_current_read_freg = _fp_read_vfreg;
643 	pfpsd->fp_current_write_freg = _fp_write_vfreg;
644 	pfpsd->fp_current_read_dreg = _fp_read_vdreg;
645 	pfpsd->fp_current_write_dreg = _fp_write_vdreg;
646 	pfpsd->fp_current_read_gsr = get_gsr;
647 	pfpsd->fp_current_write_gsr = set_gsr;
648 	pfpsd->fp_trapaddr = (caddr_t)pinst; /* bad inst addr in case we trap */
649 	ftt = _fp_read_inst((uint32_t *)pinst, &(fp.i), pfpsd);
650 	if (ftt != ftt_none)
651 		return (ftt);
652 
653 	if ((fp.inst.hibits == 2) &&
654 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35) ||
655 	    (fp.inst.op3 == 0x37))) {
656 		ftt = _fp_fpu_simulator(pfpsd, fp.inst, (fsr_type *)&tfsr, gsr);
657 		/* Do not retry emulated instruction. */
658 		pregs->r_pc = pregs->r_npc;
659 		pregs->r_npc += 4;
660 		pfpu->fpu_fsr = tfsr;
661 		if (ftt != ftt_none) {
662 			/*
663 			 * Simulation generated an exception of some kind,
664 			 * simulate the fp queue for a signal.
665 			 */
666 			pfpu->fpu_q->FQu.fpq.fpq_addr = (uint32_t *)pinst;
667 			pfpu->fpu_q->FQu.fpq.fpq_instr = fp.i;
668 			pfpu->fpu_qcnt = 1;
669 		}
670 	} else if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
671 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
672 			    pregs, prw, pfp);
673 	} else
674 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs, prw, pfpu);
675 
676 	if (ftt != ftt_none)
677 		return (ftt);
678 
679 	/*
680 	 * If we are single-stepping, don't emulate any more instructions.
681 	 */
682 	if (lwp->lwp_pcb.pcb_step != STEP_NONE)
683 		return (ftt);
684 again:
685 	/*
686 	 * now read next instruction and see if it can be emulated
687 	 */
688 	pinst = (fp_inst_type *)pregs->r_pc;
689 	pfpsd->fp_trapaddr = (caddr_t)pinst; /* bad inst addr in case we trap */
690 	ftt = _fp_read_inst((uint32_t *)pinst, &(fp.i), pfpsd);
691 	if (ftt != ftt_none)
692 		return (ftt);
693 	if ((fp.inst.hibits == 2) &&		/* fpops */
694 	    ((fp.inst.op3 == 0x34) || (fp.inst.op3 == 0x35) ||
695 	    (fp.inst.op3 == 0x37))) {
696 		ftt = _fp_fpu_simulator(pfpsd, fp.inst, (fsr_type *)&tfsr, gsr);
697 		/* Do not retry emulated instruction. */
698 		pfpu->fpu_fsr = tfsr;
699 		pregs->r_pc = pregs->r_npc;
700 		pregs->r_npc += 4;
701 		if (ftt != ftt_none) {
702 			/*
703 			 * Simulation generated an exception of some kind,
704 			 * simulate the fp queue for a signal.
705 			 */
706 			pfpu->fpu_q->FQu.fpq.fpq_addr = (uint32_t *)pinst;
707 			pfpu->fpu_q->FQu.fpq.fpq_instr = fp.i;
708 			pfpu->fpu_qcnt = 1;
709 		}
710 	} else if ((fp.inst.hibits == 2) && (fp.inst.op3 == 0x36)) {
711 			ftt = vis_fpu_simulator(pfpsd, fp.inst,
712 						pregs, prw, pfp);
713 	} else if (
714 						/* rd %gsr */
715 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x28) &&
716 			(fp.inst.rs1 == 0x13)) ||
717 						/* wr %gsr */
718 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x30) &&
719 			(fp.inst.rd == 0x13)) ||
720 						/* movcc */
721 	    ((fp.inst.hibits == 2) && ((fp.inst.op3 & 0x3f) == 0x2c) &&
722 			(((fp.i>>18) & 0x1) == 0)) ||
723 						/* fbpcc */
724 	    ((fp.inst.hibits == 0) && (((fp.i>>22) & 0x7) == 5)) ||
725 						/* fldst */
726 	    ((fp.inst.hibits == 3) && ((fp.inst.op3 & 0x38) == 0x20)) ||
727 						/* fbcc */
728 	    ((fp.inst.hibits == 0) && (((fp.i>>22) & 0x7) == 6))) {
729 		ftt = _fp_iu_simulator(pfpsd, fp.inst, pregs, prw, pfpu);
730 	} else
731 		return (ftt);
732 
733 	if (ftt != ftt_none)
734 		return (ftt);
735 	else
736 		goto again;
737 }
738 
739 /*
740  * FPU simulator global kstat data
741  */
742 struct fpustat_kstat fpustat = {
743 	{ "fpu_ieee_traps",		KSTAT_DATA_UINT64 },
744 	{ "fpu_unfinished_traps",	KSTAT_DATA_UINT64 },
745 	{ "fpu_unimplemented",		KSTAT_DATA_UINT64 },
746 };
747 
748 kstat_t *fpu_kstat = NULL;
749 kstat_t *fpuinfo_kstat = NULL;
750 kstat_t *visinfo_kstat = NULL;
751 
752 void
753 fp_kstat_init(void)
754 {
755 	const uint_t fpustat_ndata = sizeof (fpustat) / sizeof (kstat_named_t);
756 	const uint_t fpuinfo_ndata = sizeof (fpuinfo) / sizeof (kstat_named_t);
757 	const uint_t visinfo_ndata = sizeof (visinfo) /sizeof (kstat_named_t);
758 
759 	ASSERT(fpu_kstat == NULL);
760 	if ((fpu_kstat = kstat_create("unix", 0, "fpu_traps", "misc",
761 	    KSTAT_TYPE_NAMED, fpustat_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
762 		cmn_err(CE_WARN, "CPU%d: kstat_create for fpu_traps failed",
763 		    CPU->cpu_id);
764 	} else {
765 		fpu_kstat->ks_data = (void *)&fpustat;
766 		kstat_install(fpu_kstat);
767 	}
768 
769 	ASSERT(fpuinfo_kstat == NULL);
770 	if ((fpuinfo_kstat = kstat_create("unix", 0, "fpu_info", "misc",
771 	    KSTAT_TYPE_NAMED, fpuinfo_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
772 		cmn_err(CE_WARN, "CPU%d: kstat_create for fpu_info failed",
773 		    CPU->cpu_id);
774 	} else {
775 		fpuinfo_kstat->ks_data = (void *)&fpuinfo;
776 		kstat_install(fpuinfo_kstat);
777 	}
778 	ASSERT(visinfo_kstat == NULL);
779 	if ((visinfo_kstat = kstat_create("unix", 0, "vis_info", "misc",
780 	    KSTAT_TYPE_NAMED, visinfo_ndata, KSTAT_FLAG_VIRTUAL)) == NULL) {
781 		cmn_err(CE_WARN, "CPU%d: kstat_create for vis_info failed",
782 		    CPU->cpu_id);
783 	} else {
784 		visinfo_kstat->ks_data = (void *)&visinfo;
785 		kstat_install(visinfo_kstat);
786 	}
787 }
788 
789 void
790 fp_kstat_update(enum ftt_type ftt)
791 {
792 	ASSERT((ftt == ftt_ieee) || (ftt == ftt_unfinished) ||
793 	    (ftt == ftt_unimplemented));
794 	if (ftt == ftt_ieee)
795 		atomic_add_64(&fpustat.fpu_ieee_traps.value.ui64, 1);
796 	else if (ftt == ftt_unfinished)
797 		atomic_add_64(&fpustat.fpu_unfinished_traps.value.ui64, 1);
798 	else if (ftt == ftt_unimplemented)
799 		atomic_add_64(&fpustat.fpu_unimplemented_traps.value.ui64, 1);
800 }
801