xref: /onnv-gate/usr/src/uts/intel/ia32/os/fpu.c (revision 13134:8315ff49e22e)
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
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * 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*13134Skuriakose.kuruvilla@oracle.com  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T   */
270Sstevel@tonic-gate /*		All Rights Reserved				*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation		*/
300Sstevel@tonic-gate /*		All Rights Reserved				*/
310Sstevel@tonic-gate 
32*13134Skuriakose.kuruvilla@oracle.com /*
33*13134Skuriakose.kuruvilla@oracle.com  * Copyright (c) 2009, Intel Corporation.
34*13134Skuriakose.kuruvilla@oracle.com  * All rights reserved.
35*13134Skuriakose.kuruvilla@oracle.com  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/signal.h>
400Sstevel@tonic-gate #include <sys/regset.h>
410Sstevel@tonic-gate #include <sys/privregs.h>
420Sstevel@tonic-gate #include <sys/psw.h>
430Sstevel@tonic-gate #include <sys/trap.h>
440Sstevel@tonic-gate #include <sys/fault.h>
450Sstevel@tonic-gate #include <sys/systm.h>
460Sstevel@tonic-gate #include <sys/user.h>
470Sstevel@tonic-gate #include <sys/file.h>
480Sstevel@tonic-gate #include <sys/proc.h>
490Sstevel@tonic-gate #include <sys/pcb.h>
500Sstevel@tonic-gate #include <sys/lwp.h>
510Sstevel@tonic-gate #include <sys/cpuvar.h>
520Sstevel@tonic-gate #include <sys/thread.h>
530Sstevel@tonic-gate #include <sys/disp.h>
540Sstevel@tonic-gate #include <sys/fp.h>
550Sstevel@tonic-gate #include <sys/siginfo.h>
560Sstevel@tonic-gate #include <sys/archsystm.h>
570Sstevel@tonic-gate #include <sys/kmem.h>
580Sstevel@tonic-gate #include <sys/debug.h>
590Sstevel@tonic-gate #include <sys/x86_archext.h>
600Sstevel@tonic-gate #include <sys/sysmacros.h>
61*13134Skuriakose.kuruvilla@oracle.com #include <sys/cmn_err.h>
62*13134Skuriakose.kuruvilla@oracle.com 
63*13134Skuriakose.kuruvilla@oracle.com /* Legacy fxsave layout + xsave header + ymm */
64*13134Skuriakose.kuruvilla@oracle.com #define	AVX_XSAVE_SIZE		(512 + 64 + 256)
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*CSTYLED*/
670Sstevel@tonic-gate #pragma	align 16 (sse_initial)
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Initial kfpu state for SSE/SSE2 used by fpinit()
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate const struct fxsave_state sse_initial = {
730Sstevel@tonic-gate 	FPU_CW_INIT,	/* fx_fcw */
740Sstevel@tonic-gate 	0,		/* fx_fsw */
750Sstevel@tonic-gate 	0,		/* fx_fctw */
760Sstevel@tonic-gate 	0,		/* fx_fop */
770Sstevel@tonic-gate #if defined(__amd64)
780Sstevel@tonic-gate 	0,		/* fx_rip */
790Sstevel@tonic-gate 	0,		/* fx_rdp */
800Sstevel@tonic-gate #else
810Sstevel@tonic-gate 	0,		/* fx_eip */
820Sstevel@tonic-gate 	0,		/* fx_cs */
830Sstevel@tonic-gate 	0,		/* __fx_ign0 */
840Sstevel@tonic-gate 	0,		/* fx_dp */
850Sstevel@tonic-gate 	0,		/* fx_ds */
860Sstevel@tonic-gate 	0,		/* __fx_ign1 */
870Sstevel@tonic-gate #endif /* __amd64 */
880Sstevel@tonic-gate 	SSE_MXCSR_INIT	/* fx_mxcsr */
890Sstevel@tonic-gate 	/* rest of structure is zero */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
92*13134Skuriakose.kuruvilla@oracle.com /*CSTYLED*/
93*13134Skuriakose.kuruvilla@oracle.com #pragma	align 64 (avx_initial)
94*13134Skuriakose.kuruvilla@oracle.com 
95*13134Skuriakose.kuruvilla@oracle.com /*
96*13134Skuriakose.kuruvilla@oracle.com  * Initial kfpu state for AVX used by fpinit()
97*13134Skuriakose.kuruvilla@oracle.com  */
98*13134Skuriakose.kuruvilla@oracle.com const struct xsave_state avx_initial = {
99*13134Skuriakose.kuruvilla@oracle.com 	/*
100*13134Skuriakose.kuruvilla@oracle.com 	 * The definition below needs to be identical with sse_initial
101*13134Skuriakose.kuruvilla@oracle.com 	 * defined above.
102*13134Skuriakose.kuruvilla@oracle.com 	 */
103*13134Skuriakose.kuruvilla@oracle.com 	{
104*13134Skuriakose.kuruvilla@oracle.com 		FPU_CW_INIT,	/* fx_fcw */
105*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_fsw */
106*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_fctw */
107*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_fop */
108*13134Skuriakose.kuruvilla@oracle.com #if defined(__amd64)
109*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_rip */
110*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_rdp */
111*13134Skuriakose.kuruvilla@oracle.com #else
112*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_eip */
113*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_cs */
114*13134Skuriakose.kuruvilla@oracle.com 		0,		/* __fx_ign0 */
115*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_dp */
116*13134Skuriakose.kuruvilla@oracle.com 		0,		/* fx_ds */
117*13134Skuriakose.kuruvilla@oracle.com 		0,		/* __fx_ign1 */
118*13134Skuriakose.kuruvilla@oracle.com #endif /* __amd64 */
119*13134Skuriakose.kuruvilla@oracle.com 		SSE_MXCSR_INIT	/* fx_mxcsr */
120*13134Skuriakose.kuruvilla@oracle.com 		/* rest of structure is zero */
121*13134Skuriakose.kuruvilla@oracle.com 	},
122*13134Skuriakose.kuruvilla@oracle.com 	/*
123*13134Skuriakose.kuruvilla@oracle.com 	 * bit0 = 1 for XSTATE_BV to indicate that legacy fields are valid,
124*13134Skuriakose.kuruvilla@oracle.com 	 * and CPU should initialize XMM/YMM.
125*13134Skuriakose.kuruvilla@oracle.com 	 */
126*13134Skuriakose.kuruvilla@oracle.com 	1,
127*13134Skuriakose.kuruvilla@oracle.com 	{0, 0}	/* These 2 bytes must be zero */
128*13134Skuriakose.kuruvilla@oracle.com 	/* rest of structure is zero */
129*13134Skuriakose.kuruvilla@oracle.com };
130*13134Skuriakose.kuruvilla@oracle.com 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * mxcsr_mask value (possibly reset in fpu_probe); used to avoid
1330Sstevel@tonic-gate  * the #gp exception caused by setting unsupported bits in the
1340Sstevel@tonic-gate  * MXCSR register
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate uint32_t sse_mxcsr_mask = SSE_MXCSR_MASK_DEFAULT;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * Initial kfpu state for x87 used by fpinit()
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate const struct fnsave_state x87_initial = {
1420Sstevel@tonic-gate 	FPU_CW_INIT,	/* f_fcw */
1430Sstevel@tonic-gate 	0,		/* __f_ign0 */
1440Sstevel@tonic-gate 	0,		/* f_fsw */
1450Sstevel@tonic-gate 	0,		/* __f_ign1 */
1460Sstevel@tonic-gate 	0xffff,		/* f_ftw */
1470Sstevel@tonic-gate 	/* rest of structure is zero */
1480Sstevel@tonic-gate };
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #if defined(__amd64)
151*13134Skuriakose.kuruvilla@oracle.com /*
152*13134Skuriakose.kuruvilla@oracle.com  * This vector is patched to xsave_ctxt() if we discover we have an
153*13134Skuriakose.kuruvilla@oracle.com  * XSAVE-capable chip in fpu_probe.
154*13134Skuriakose.kuruvilla@oracle.com  */
155*13134Skuriakose.kuruvilla@oracle.com void (*fpsave_ctxt)(void *) = fpxsave_ctxt;
1560Sstevel@tonic-gate #elif defined(__i386)
1570Sstevel@tonic-gate /*
158*13134Skuriakose.kuruvilla@oracle.com  * This vector is patched to fpxsave_ctxt() if we discover we have an
159*13134Skuriakose.kuruvilla@oracle.com  * SSE-capable chip in fpu_probe(). It is patched to xsave_ctxt
160*13134Skuriakose.kuruvilla@oracle.com  * if we discover we have an XSAVE-capable chip in fpu_probe.
1610Sstevel@tonic-gate  */
1623446Smrj void (*fpsave_ctxt)(void *) = fpnsave_ctxt;
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static int fpe_sicode(uint_t);
1660Sstevel@tonic-gate static int fpe_simd_sicode(uint_t);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * Copy the state of parent lwp's floating point context into the new lwp.
1700Sstevel@tonic-gate  * Invoked for both fork() and lwp_create().
1710Sstevel@tonic-gate  *
1720Sstevel@tonic-gate  * Note that we inherit -only- the control state (e.g. exception masks,
1730Sstevel@tonic-gate  * rounding, precision control, etc.); the FPU registers are otherwise
1740Sstevel@tonic-gate  * reset to their initial state.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate static void
fp_new_lwp(kthread_id_t t,kthread_id_t ct)1770Sstevel@tonic-gate fp_new_lwp(kthread_id_t t, kthread_id_t ct)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	struct fpu_ctx *fp;		/* parent fpu context */
1800Sstevel@tonic-gate 	struct fpu_ctx *cfp;		/* new fpu context */
1810Sstevel@tonic-gate 	struct fxsave_state *fx, *cfx;
182*13134Skuriakose.kuruvilla@oracle.com #if defined(__i386)
183*13134Skuriakose.kuruvilla@oracle.com 	struct fnsave_state *fn, *cfn;
184*13134Skuriakose.kuruvilla@oracle.com #endif
185*13134Skuriakose.kuruvilla@oracle.com 	struct xsave_state *cxs;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	ASSERT(fp_kind != FP_NO);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	fp = &t->t_lwp->lwp_pcb.pcb_fpu;
1900Sstevel@tonic-gate 	cfp = &ct->t_lwp->lwp_pcb.pcb_fpu;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * If the parent FPU state is still in the FPU hw then save it;
1940Sstevel@tonic-gate 	 * conveniently, fp_save() already does this for us nicely.
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 	fp_save(fp);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	cfp->fpu_flags = FPU_EN | FPU_VALID;
1990Sstevel@tonic-gate 	cfp->fpu_regs.kfpu_status = 0;
2000Sstevel@tonic-gate 	cfp->fpu_regs.kfpu_xstatus = 0;
2010Sstevel@tonic-gate 
202*13134Skuriakose.kuruvilla@oracle.com 	switch (fp_save_mech) {
203*13134Skuriakose.kuruvilla@oracle.com #if defined(__i386)
204*13134Skuriakose.kuruvilla@oracle.com 	case FP_FNSAVE:
205*13134Skuriakose.kuruvilla@oracle.com 		fn = &fp->fpu_regs.kfpu_u.kfpu_fn;
206*13134Skuriakose.kuruvilla@oracle.com 		cfn = &cfp->fpu_regs.kfpu_u.kfpu_fn;
207*13134Skuriakose.kuruvilla@oracle.com 		bcopy(&x87_initial, cfn, sizeof (*cfn));
208*13134Skuriakose.kuruvilla@oracle.com 		cfn->f_fcw = fn->f_fcw;
209*13134Skuriakose.kuruvilla@oracle.com 		break;
210*13134Skuriakose.kuruvilla@oracle.com #endif
211*13134Skuriakose.kuruvilla@oracle.com 	case FP_FXSAVE:
2120Sstevel@tonic-gate 		fx = &fp->fpu_regs.kfpu_u.kfpu_fx;
2130Sstevel@tonic-gate 		cfx = &cfp->fpu_regs.kfpu_u.kfpu_fx;
2140Sstevel@tonic-gate 		bcopy(&sse_initial, cfx, sizeof (*cfx));
2150Sstevel@tonic-gate 		cfx->fx_mxcsr = fx->fx_mxcsr & ~SSE_MXCSR_EFLAGS;
2160Sstevel@tonic-gate 		cfx->fx_fcw = fx->fx_fcw;
217*13134Skuriakose.kuruvilla@oracle.com 		break;
218*13134Skuriakose.kuruvilla@oracle.com 
219*13134Skuriakose.kuruvilla@oracle.com 	case FP_XSAVE:
220*13134Skuriakose.kuruvilla@oracle.com 		cfp->fpu_xsave_mask = fp->fpu_xsave_mask;
221*13134Skuriakose.kuruvilla@oracle.com 
222*13134Skuriakose.kuruvilla@oracle.com 		fx = &fp->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave;
223*13134Skuriakose.kuruvilla@oracle.com 		cxs = &cfp->fpu_regs.kfpu_u.kfpu_xs;
224*13134Skuriakose.kuruvilla@oracle.com 		cfx = &cxs->xs_fxsave;
2250Sstevel@tonic-gate 
226*13134Skuriakose.kuruvilla@oracle.com 		bcopy(&avx_initial, cxs, sizeof (*cxs));
227*13134Skuriakose.kuruvilla@oracle.com 		cfx->fx_mxcsr = fx->fx_mxcsr & ~SSE_MXCSR_EFLAGS;
228*13134Skuriakose.kuruvilla@oracle.com 		cfx->fx_fcw = fx->fx_fcw;
229*13134Skuriakose.kuruvilla@oracle.com 		cxs->xs_xstate_bv |= (get_xcr(XFEATURE_ENABLED_MASK) &
230*13134Skuriakose.kuruvilla@oracle.com 		    XFEATURE_FP_ALL);
231*13134Skuriakose.kuruvilla@oracle.com 		break;
232*13134Skuriakose.kuruvilla@oracle.com 	default:
233*13134Skuriakose.kuruvilla@oracle.com 		panic("Invalid fp_save_mech");
234*13134Skuriakose.kuruvilla@oracle.com 		/*NOTREACHED*/
2350Sstevel@tonic-gate 	}
236*13134Skuriakose.kuruvilla@oracle.com 
2370Sstevel@tonic-gate 	installctx(ct, cfp,
2383446Smrj 	    fpsave_ctxt, NULL, fp_new_lwp, fp_new_lwp, NULL, fp_free);
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Now, when the new lwp starts running, it will take a trap
2410Sstevel@tonic-gate 	 * that will be handled inline in the trap table to cause
2420Sstevel@tonic-gate 	 * the appropriate f*rstor instruction to load the save area we
2430Sstevel@tonic-gate 	 * constructed above directly into the hardware.
2440Sstevel@tonic-gate 	 */
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Free any state associated with floating point context.
2490Sstevel@tonic-gate  * Fp_free can be called in three cases:
2500Sstevel@tonic-gate  * 1) from reaper -> thread_free -> ctxfree -> fp_free
2510Sstevel@tonic-gate  *	fp context belongs to a thread on deathrow
2520Sstevel@tonic-gate  *	nothing to do,  thread will never be resumed
2530Sstevel@tonic-gate  *	thread calling ctxfree is reaper
2540Sstevel@tonic-gate  *
2550Sstevel@tonic-gate  * 2) from exec -> ctxfree -> fp_free
2560Sstevel@tonic-gate  *	fp context belongs to the current thread
2570Sstevel@tonic-gate  *	must disable fpu, thread calling ctxfree is curthread
2580Sstevel@tonic-gate  *
2590Sstevel@tonic-gate  * 3) from restorecontext -> setfpregs -> fp_free
2600Sstevel@tonic-gate  *	we have a modified context in the memory (lwp->pcb_fpu)
2610Sstevel@tonic-gate  *	disable fpu and release the fp context for the CPU
2620Sstevel@tonic-gate  *
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate /*ARGSUSED*/
2650Sstevel@tonic-gate void
fp_free(struct fpu_ctx * fp,int isexec)2660Sstevel@tonic-gate fp_free(struct fpu_ctx *fp, int isexec)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	ASSERT(fp_kind != FP_NO);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (fp->fpu_flags & FPU_VALID)
2710Sstevel@tonic-gate 		return;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	kpreempt_disable();
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * We want to do fpsave rather than fpdisable so that we can
2760Sstevel@tonic-gate 	 * keep the fpu_flags as FPU_VALID tracking the CR0_TS bit
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	fp->fpu_flags |= FPU_VALID;
2790Sstevel@tonic-gate 	/* If for current thread disable FP to track FPU_VALID */
2800Sstevel@tonic-gate 	if (curthread->t_lwp && fp == &curthread->t_lwp->lwp_pcb.pcb_fpu) {
2810Sstevel@tonic-gate 		/* Clear errors if any to prevent frstor from complaining */
2820Sstevel@tonic-gate 		(void) fperr_reset();
283*13134Skuriakose.kuruvilla@oracle.com 		if (fp_kind & __FP_SSE)
2840Sstevel@tonic-gate 			(void) fpxerr_reset();
2850Sstevel@tonic-gate 		fpdisable();
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	kpreempt_enable();
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Store the floating point state and disable the floating point unit.
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate void
fp_save(struct fpu_ctx * fp)2940Sstevel@tonic-gate fp_save(struct fpu_ctx *fp)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	ASSERT(fp_kind != FP_NO);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	kpreempt_disable();
2990Sstevel@tonic-gate 	if (!fp || fp->fpu_flags & FPU_VALID) {
3000Sstevel@tonic-gate 		kpreempt_enable();
3010Sstevel@tonic-gate 		return;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	ASSERT(curthread->t_lwp && fp == &curthread->t_lwp->lwp_pcb.pcb_fpu);
3040Sstevel@tonic-gate 
305*13134Skuriakose.kuruvilla@oracle.com 	switch (fp_save_mech) {
306*13134Skuriakose.kuruvilla@oracle.com #if defined(__i386)
307*13134Skuriakose.kuruvilla@oracle.com 	case FP_FNSAVE:
308*13134Skuriakose.kuruvilla@oracle.com 		fpsave(&fp->fpu_regs.kfpu_u.kfpu_fn);
309*13134Skuriakose.kuruvilla@oracle.com 		break;
310*13134Skuriakose.kuruvilla@oracle.com #endif
311*13134Skuriakose.kuruvilla@oracle.com 	case FP_FXSAVE:
3120Sstevel@tonic-gate 		fpxsave(&fp->fpu_regs.kfpu_u.kfpu_fx);
3130Sstevel@tonic-gate 		break;
314*13134Skuriakose.kuruvilla@oracle.com 
315*13134Skuriakose.kuruvilla@oracle.com 	case FP_XSAVE:
316*13134Skuriakose.kuruvilla@oracle.com 		xsave(&fp->fpu_regs.kfpu_u.kfpu_xs, fp->fpu_xsave_mask);
3170Sstevel@tonic-gate 		break;
318*13134Skuriakose.kuruvilla@oracle.com 	default:
319*13134Skuriakose.kuruvilla@oracle.com 		panic("Invalid fp_save_mech");
320*13134Skuriakose.kuruvilla@oracle.com 		/*NOTREACHED*/
3210Sstevel@tonic-gate 	}
322*13134Skuriakose.kuruvilla@oracle.com 
3230Sstevel@tonic-gate 	fp->fpu_flags |= FPU_VALID;
3240Sstevel@tonic-gate 	kpreempt_enable();
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * Restore the FPU context for the thread:
3290Sstevel@tonic-gate  * The possibilities are:
3300Sstevel@tonic-gate  *	1. No active FPU context: Load the new context into the FPU hw
3310Sstevel@tonic-gate  *	   and enable the FPU.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate void
fp_restore(struct fpu_ctx * fp)3340Sstevel@tonic-gate fp_restore(struct fpu_ctx *fp)
3350Sstevel@tonic-gate {
336*13134Skuriakose.kuruvilla@oracle.com 	switch (fp_save_mech) {
337*13134Skuriakose.kuruvilla@oracle.com #if defined(__i386)
338*13134Skuriakose.kuruvilla@oracle.com 	case FP_FNSAVE:
339*13134Skuriakose.kuruvilla@oracle.com 		fprestore(&fp->fpu_regs.kfpu_u.kfpu_fn);
340*13134Skuriakose.kuruvilla@oracle.com 		break;
341*13134Skuriakose.kuruvilla@oracle.com #endif
342*13134Skuriakose.kuruvilla@oracle.com 	case FP_FXSAVE:
3430Sstevel@tonic-gate 		fpxrestore(&fp->fpu_regs.kfpu_u.kfpu_fx);
344*13134Skuriakose.kuruvilla@oracle.com 		break;
345*13134Skuriakose.kuruvilla@oracle.com 
346*13134Skuriakose.kuruvilla@oracle.com 	case FP_XSAVE:
347*13134Skuriakose.kuruvilla@oracle.com 		xrestore(&fp->fpu_regs.kfpu_u.kfpu_xs, fp->fpu_xsave_mask);
348*13134Skuriakose.kuruvilla@oracle.com 		break;
349*13134Skuriakose.kuruvilla@oracle.com 	default:
350*13134Skuriakose.kuruvilla@oracle.com 		panic("Invalid fp_save_mech");
351*13134Skuriakose.kuruvilla@oracle.com 		/*NOTREACHED*/
352*13134Skuriakose.kuruvilla@oracle.com 	}
353*13134Skuriakose.kuruvilla@oracle.com 
3540Sstevel@tonic-gate 	fp->fpu_flags &= ~FPU_VALID;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Seeds the initial state for the current thread.  The possibilities are:
3600Sstevel@tonic-gate  *      1. Another process has modified the FPU state before we have done any
3610Sstevel@tonic-gate  *         initialization: Load the FPU state from the LWP state.
3620Sstevel@tonic-gate  *      2. The FPU state has not been externally modified:  Load a clean state.
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate static void
fp_seed(void)3650Sstevel@tonic-gate fp_seed(void)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	struct fpu_ctx *fp = &ttolwp(curthread)->lwp_pcb.pcb_fpu;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	ASSERT(curthread->t_preempt >= 1);
3700Sstevel@tonic-gate 	ASSERT((fp->fpu_flags & FPU_EN) == 0);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * Always initialize a new context and initialize the hardware.
3740Sstevel@tonic-gate 	 */
375*13134Skuriakose.kuruvilla@oracle.com 	if (fp_save_mech == FP_XSAVE) {
376*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_xsave_mask = get_xcr(XFEATURE_ENABLED_MASK) &
377*13134Skuriakose.kuruvilla@oracle.com 		    XFEATURE_FP_ALL;
378*13134Skuriakose.kuruvilla@oracle.com 	}
379*13134Skuriakose.kuruvilla@oracle.com 
3800Sstevel@tonic-gate 	installctx(curthread, fp,
3813446Smrj 	    fpsave_ctxt, NULL, fp_new_lwp, fp_new_lwp, NULL, fp_free);
3820Sstevel@tonic-gate 	fpinit();
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/*
3850Sstevel@tonic-gate 	 * If FPU_VALID is set, it means someone has modified registers via
3860Sstevel@tonic-gate 	 * /proc.  In this case, restore the current lwp's state.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	if (fp->fpu_flags & FPU_VALID)
3890Sstevel@tonic-gate 		fp_restore(fp);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	ASSERT((fp->fpu_flags & FPU_VALID) == 0);
3920Sstevel@tonic-gate 	fp->fpu_flags = FPU_EN;
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * This routine is called from trap() when User thread takes No Extension
3970Sstevel@tonic-gate  * Fault. The possiblities are:
3980Sstevel@tonic-gate  *	1. User thread has executed a FP instruction for the first time.
3990Sstevel@tonic-gate  *	   Save current FPU context if any. Initialize FPU, setup FPU
4000Sstevel@tonic-gate  *	   context for the thread and enable FP hw.
4010Sstevel@tonic-gate  *	2. Thread's pcb has a valid FPU state: Restore the FPU state and
4020Sstevel@tonic-gate  *	   enable FP hw.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * Note that case #2 is inlined in the trap table.
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate int
fpnoextflt(struct regs * rp)4070Sstevel@tonic-gate fpnoextflt(struct regs *rp)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	struct fpu_ctx *fp = &ttolwp(curthread)->lwp_pcb.pcb_fpu;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate #if !defined(__lint)
4120Sstevel@tonic-gate 	ASSERT(sizeof (struct fxsave_state) == 512 &&
4130Sstevel@tonic-gate 	    sizeof (struct fnsave_state) == 108);
4140Sstevel@tonic-gate 	ASSERT((offsetof(struct fxsave_state, fx_xmm[0]) & 0xf) == 0);
415*13134Skuriakose.kuruvilla@oracle.com 
416*13134Skuriakose.kuruvilla@oracle.com 	ASSERT(sizeof (struct xsave_state) >= AVX_XSAVE_SIZE);
417*13134Skuriakose.kuruvilla@oracle.com 
4180Sstevel@tonic-gate #if defined(__i386)
4190Sstevel@tonic-gate 	ASSERT(sizeof (struct fpu) == sizeof (struct __old_fpu));
4200Sstevel@tonic-gate #endif	/* __i386 */
4210Sstevel@tonic-gate #endif	/* !__lint */
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * save area MUST be 16-byte aligned, else will page fault
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	ASSERT(((uintptr_t)(&fp->fpu_regs.kfpu_u.kfpu_fx) & 0xf) == 0);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	kpreempt_disable();
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Now we can enable the interrupts.
4310Sstevel@tonic-gate 	 * (NOTE: fp-no-coprocessor comes thru interrupt gate)
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	sti();
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (!fpu_exists) { /* check for FPU hw exists */
4360Sstevel@tonic-gate 		if (fp_kind == FP_NO) {
4370Sstevel@tonic-gate 			uint32_t inst;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 			/*
4400Sstevel@tonic-gate 			 * When the system has no floating point support,
4410Sstevel@tonic-gate 			 * i.e. no FP hardware and no emulator, skip the
4420Sstevel@tonic-gate 			 * two kinds of FP instruction that occur in
4430Sstevel@tonic-gate 			 * fpstart.  Allows processes that do no real FP
4440Sstevel@tonic-gate 			 * to run normally.
4450Sstevel@tonic-gate 			 */
4460Sstevel@tonic-gate 			if (fuword32((void *)rp->r_pc, &inst) != -1 &&
4470Sstevel@tonic-gate 			    ((inst & 0xFFFF) == 0x7dd9 ||
4480Sstevel@tonic-gate 			    (inst & 0xFFFF) == 0x6dd9)) {
4490Sstevel@tonic-gate 				rp->r_pc += 3;
4500Sstevel@tonic-gate 				kpreempt_enable();
4510Sstevel@tonic-gate 				return (0);
4520Sstevel@tonic-gate 			}
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		/*
4560Sstevel@tonic-gate 		 * If we have neither a processor extension nor
4570Sstevel@tonic-gate 		 * an emulator, kill the process OR panic the kernel.
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		kpreempt_enable();
4600Sstevel@tonic-gate 		return (1); /* error */
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4635084Sjohnlev #if !defined(__xpv)	/* XXPV	Is this ifdef needed now? */
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * A paranoid cross-check: for the SSE case, ensure that %cr4 is
4660Sstevel@tonic-gate 	 * configured to enable fully fledged (%xmm) fxsave/fxrestor on
4670Sstevel@tonic-gate 	 * this CPU.  For the non-SSE case, ensure that it isn't.
4680Sstevel@tonic-gate 	 */
469*13134Skuriakose.kuruvilla@oracle.com 	ASSERT(((fp_kind & __FP_SSE) &&
470*13134Skuriakose.kuruvilla@oracle.com 	    (getcr4() & CR4_OSFXSR) == CR4_OSFXSR) ||
471*13134Skuriakose.kuruvilla@oracle.com 	    (!(fp_kind & __FP_SSE) &&
4720Sstevel@tonic-gate 	    (getcr4() & (CR4_OSXMMEXCPT|CR4_OSFXSR)) == 0));
4735084Sjohnlev #endif
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (fp->fpu_flags & FPU_EN) {
4760Sstevel@tonic-gate 		/* case 2 */
4770Sstevel@tonic-gate 		fp_restore(fp);
4780Sstevel@tonic-gate 	} else {
4790Sstevel@tonic-gate 		/* case 1 */
4800Sstevel@tonic-gate 		fp_seed();
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	kpreempt_enable();
4830Sstevel@tonic-gate 	return (0);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate  * Handle a processor extension overrun fault
4890Sstevel@tonic-gate  * Returns non zero for error.
4903446Smrj  *
4913446Smrj  * XXX	Shouldn't this just be abolished given that we're not supporting
4923446Smrj  *	anything prior to Pentium?
4930Sstevel@tonic-gate  */
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /* ARGSUSED */
4960Sstevel@tonic-gate int
fpextovrflt(struct regs * rp)4970Sstevel@tonic-gate fpextovrflt(struct regs *rp)
4980Sstevel@tonic-gate {
4995084Sjohnlev #if !defined(__xpv)		/* XXPV	Do we need this ifdef either */
5000Sstevel@tonic-gate 	ulong_t cur_cr0;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	ASSERT(fp_kind != FP_NO);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	cur_cr0 = getcr0();
5050Sstevel@tonic-gate 	fpinit();		/* initialize the FPU hardware */
5060Sstevel@tonic-gate 	setcr0(cur_cr0);
5075084Sjohnlev #endif
5080Sstevel@tonic-gate 	sti();
5090Sstevel@tonic-gate 	return (1); 		/* error, send SIGSEGV signal to the thread */
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * Handle a processor extension error fault
5140Sstevel@tonic-gate  * Returns non zero for error.
5150Sstevel@tonic-gate  */
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*ARGSUSED*/
5180Sstevel@tonic-gate int
fpexterrflt(struct regs * rp)5190Sstevel@tonic-gate fpexterrflt(struct regs *rp)
5200Sstevel@tonic-gate {
5215849Ssethg 	uint32_t fpcw, fpsw;
5220Sstevel@tonic-gate 	fpu_ctx_t *fp = &ttolwp(curthread)->lwp_pcb.pcb_fpu;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	ASSERT(fp_kind != FP_NO);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/*
5270Sstevel@tonic-gate 	 * Now we can enable the interrupts.
5280Sstevel@tonic-gate 	 * (NOTE: x87 fp exceptions come thru interrupt gate)
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	sti();
5310Sstevel@tonic-gate 
5325849Ssethg 	if (!fpu_exists)
5335849Ssethg 		return (FPE_FLTINV);
5340Sstevel@tonic-gate 
5355849Ssethg 	/*
5365849Ssethg 	 * Do an unconditional save of the FP state.  If it's dirty (TS=0),
5375849Ssethg 	 * it'll be saved into the fpu context area passed in (that of the
5385849Ssethg 	 * current thread).  If it's not dirty (it may not be, due to
5395849Ssethg 	 * an intervening save due to a context switch between the sti(),
5405849Ssethg 	 * above and here, then it's safe to just use the stored values in
5415849Ssethg 	 * the context save area to determine the cause of the fault.
5425849Ssethg 	 */
5435849Ssethg 	fp_save(fp);
5445849Ssethg 
5455849Ssethg 	/* clear exception flags in saved state, as if by fnclex */
546*13134Skuriakose.kuruvilla@oracle.com 	switch (fp_save_mech) {
547*13134Skuriakose.kuruvilla@oracle.com #if defined(__i386)
548*13134Skuriakose.kuruvilla@oracle.com 	case FP_FNSAVE:
549*13134Skuriakose.kuruvilla@oracle.com 		fpsw = fp->fpu_regs.kfpu_u.kfpu_fn.f_fsw;
550*13134Skuriakose.kuruvilla@oracle.com 		fpcw = fp->fpu_regs.kfpu_u.kfpu_fn.f_fcw;
551*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_regs.kfpu_u.kfpu_fn.f_fsw &= ~FPS_SW_EFLAGS;
552*13134Skuriakose.kuruvilla@oracle.com 		break;
5530Sstevel@tonic-gate #endif
5545849Ssethg 
555*13134Skuriakose.kuruvilla@oracle.com 	case FP_FXSAVE:
556*13134Skuriakose.kuruvilla@oracle.com 		fpsw = fp->fpu_regs.kfpu_u.kfpu_fx.fx_fsw;
557*13134Skuriakose.kuruvilla@oracle.com 		fpcw = fp->fpu_regs.kfpu_u.kfpu_fx.fx_fcw;
558*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_regs.kfpu_u.kfpu_fx.fx_fsw &= ~FPS_SW_EFLAGS;
559*13134Skuriakose.kuruvilla@oracle.com 		break;
560*13134Skuriakose.kuruvilla@oracle.com 
561*13134Skuriakose.kuruvilla@oracle.com 	case FP_XSAVE:
562*13134Skuriakose.kuruvilla@oracle.com 		fpsw = fp->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave.fx_fsw;
563*13134Skuriakose.kuruvilla@oracle.com 		fpcw = fp->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave.fx_fcw;
564*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave.fx_fsw &= ~FPS_SW_EFLAGS;
565*13134Skuriakose.kuruvilla@oracle.com 		/*
566*13134Skuriakose.kuruvilla@oracle.com 		 * Always set LEGACY_FP as it may have been cleared by XSAVE
567*13134Skuriakose.kuruvilla@oracle.com 		 * instruction
568*13134Skuriakose.kuruvilla@oracle.com 		 */
569*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_regs.kfpu_u.kfpu_xs.xs_xstate_bv |= XFEATURE_LEGACY_FP;
570*13134Skuriakose.kuruvilla@oracle.com 		break;
571*13134Skuriakose.kuruvilla@oracle.com 	default:
572*13134Skuriakose.kuruvilla@oracle.com 		panic("Invalid fp_save_mech");
573*13134Skuriakose.kuruvilla@oracle.com 		/*NOTREACHED*/
574*13134Skuriakose.kuruvilla@oracle.com 	}
575*13134Skuriakose.kuruvilla@oracle.com 
5765849Ssethg 	fp->fpu_regs.kfpu_status = fpsw;
5775849Ssethg 
5785849Ssethg 	if ((fpsw & FPS_ES) == 0)
5795849Ssethg 		return (0);		/* No exception */
5805849Ssethg 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * "and" the exception flags with the complement of the mask
5830Sstevel@tonic-gate 	 * bits to determine which exception occurred
5840Sstevel@tonic-gate 	 */
5855849Ssethg 	return (fpe_sicode(fpsw & ~fpcw & 0x3f));
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate  * Handle an SSE/SSE2 precise exception.
5900Sstevel@tonic-gate  * Returns a non-zero sicode for error.
5910Sstevel@tonic-gate  */
5920Sstevel@tonic-gate /*ARGSUSED*/
5930Sstevel@tonic-gate int
fpsimderrflt(struct regs * rp)5940Sstevel@tonic-gate fpsimderrflt(struct regs *rp)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	uint32_t mxcsr, xmask;
5970Sstevel@tonic-gate 	fpu_ctx_t *fp = &ttolwp(curthread)->lwp_pcb.pcb_fpu;
5980Sstevel@tonic-gate 
599*13134Skuriakose.kuruvilla@oracle.com 	ASSERT(fp_kind & __FP_SSE);
6000Sstevel@tonic-gate 
6015849Ssethg 	/*
6025849Ssethg 	 * NOTE: Interrupts are disabled during execution of this
6035849Ssethg 	 * function.  They are enabled by the caller in trap.c.
6045849Ssethg 	 */
6055849Ssethg 
6065849Ssethg 	/*
6075849Ssethg 	 * The only way we could have gotten here if there is no FP unit
6085849Ssethg 	 * is via a user executing an INT $19 instruction, so there is
6095849Ssethg 	 * no fault in that case.
6105849Ssethg 	 */
6115849Ssethg 	if (!fpu_exists)
6125849Ssethg 		return (0);
6135849Ssethg 
6145849Ssethg 	/*
6155849Ssethg 	 * Do an unconditional save of the FP state.  If it's dirty (TS=0),
6165849Ssethg 	 * it'll be saved into the fpu context area passed in (that of the
6175849Ssethg 	 * current thread).  If it's not dirty, then it's safe to just use
6185849Ssethg 	 * the stored values in the context save area to determine the
6195849Ssethg 	 * cause of the fault.
6205849Ssethg 	 */
6215849Ssethg 	fp_save(fp); 		/* save the FPU state */
6225849Ssethg 
6235849Ssethg 	mxcsr = fp->fpu_regs.kfpu_u.kfpu_fx.fx_mxcsr;
6245849Ssethg 
6255849Ssethg 	fp->fpu_regs.kfpu_status = fp->fpu_regs.kfpu_u.kfpu_fx.fx_fsw;
6265849Ssethg 
6270Sstevel@tonic-gate 	fp->fpu_regs.kfpu_xstatus = mxcsr;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/*
6300Sstevel@tonic-gate 	 * compute the mask that determines which conditions can cause
6310Sstevel@tonic-gate 	 * a #xm exception, and use this to clean the status bits so that
6320Sstevel@tonic-gate 	 * we can identify the true cause of this one.
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	xmask = (mxcsr >> 7) & SSE_MXCSR_EFLAGS;
6350Sstevel@tonic-gate 	return (fpe_simd_sicode((mxcsr & SSE_MXCSR_EFLAGS) & ~xmask));
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate  * In the unlikely event that someone is relying on this subcode being
6400Sstevel@tonic-gate  * FPE_FLTILL for denormalize exceptions, it can always be patched back
6410Sstevel@tonic-gate  * again to restore old behaviour.
6420Sstevel@tonic-gate  */
6430Sstevel@tonic-gate int fpe_fltden = FPE_FLTDEN;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate  * Map from the FPU status word to the FP exception si_code.
6470Sstevel@tonic-gate  */
6480Sstevel@tonic-gate static int
fpe_sicode(uint_t sw)6490Sstevel@tonic-gate fpe_sicode(uint_t sw)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	if (sw & FPS_IE)
6520Sstevel@tonic-gate 		return (FPE_FLTINV);
6530Sstevel@tonic-gate 	if (sw & FPS_ZE)
6540Sstevel@tonic-gate 		return (FPE_FLTDIV);
6550Sstevel@tonic-gate 	if (sw & FPS_DE)
6560Sstevel@tonic-gate 		return (fpe_fltden);
6570Sstevel@tonic-gate 	if (sw & FPS_OE)
6580Sstevel@tonic-gate 		return (FPE_FLTOVF);
6590Sstevel@tonic-gate 	if (sw & FPS_UE)
6600Sstevel@tonic-gate 		return (FPE_FLTUND);
6610Sstevel@tonic-gate 	if (sw & FPS_PE)
6620Sstevel@tonic-gate 		return (FPE_FLTRES);
6630Sstevel@tonic-gate 	return (FPE_FLTINV);	/* default si_code for other exceptions */
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * Map from the SSE status word to the FP exception si_code.
6680Sstevel@tonic-gate  */
6690Sstevel@tonic-gate static int
fpe_simd_sicode(uint_t sw)6700Sstevel@tonic-gate fpe_simd_sicode(uint_t sw)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate 	if (sw & SSE_IE)
6730Sstevel@tonic-gate 		return (FPE_FLTINV);
6740Sstevel@tonic-gate 	if (sw & SSE_ZE)
6750Sstevel@tonic-gate 		return (FPE_FLTDIV);
6760Sstevel@tonic-gate 	if (sw & SSE_DE)
6770Sstevel@tonic-gate 		return (FPE_FLTDEN);
6780Sstevel@tonic-gate 	if (sw & SSE_OE)
6790Sstevel@tonic-gate 		return (FPE_FLTOVF);
6800Sstevel@tonic-gate 	if (sw & SSE_UE)
6810Sstevel@tonic-gate 		return (FPE_FLTUND);
6820Sstevel@tonic-gate 	if (sw & SSE_PE)
6830Sstevel@tonic-gate 		return (FPE_FLTRES);
6840Sstevel@tonic-gate 	return (FPE_FLTINV);	/* default si_code for other exceptions */
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate  * This routine is invoked as part of libc's __fpstart implementation
6890Sstevel@tonic-gate  * via sysi86(2).
6900Sstevel@tonic-gate  *
6910Sstevel@tonic-gate  * It may be called -before- any context has been assigned in which case
6920Sstevel@tonic-gate  * we try and avoid touching the hardware.  Or it may be invoked well
6930Sstevel@tonic-gate  * after the context has been assigned and fiddled with, in which case
6940Sstevel@tonic-gate  * just tweak it directly.
6950Sstevel@tonic-gate  */
6960Sstevel@tonic-gate void
fpsetcw(uint16_t fcw,uint32_t mxcsr)6970Sstevel@tonic-gate fpsetcw(uint16_t fcw, uint32_t mxcsr)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	struct fpu_ctx *fp = &curthread->t_lwp->lwp_pcb.pcb_fpu;
7000Sstevel@tonic-gate 	struct fxsave_state *fx;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (!fpu_exists || fp_kind == FP_NO)
7030Sstevel@tonic-gate 		return;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	if ((fp->fpu_flags & FPU_EN) == 0) {
7060Sstevel@tonic-gate 		if (fcw == FPU_CW_INIT && mxcsr == SSE_MXCSR_INIT) {
7070Sstevel@tonic-gate 			/*
7080Sstevel@tonic-gate 			 * Common case.  Floating point unit not yet
7090Sstevel@tonic-gate 			 * enabled, and kernel already intends to initialize
7100Sstevel@tonic-gate 			 * the hardware the way the caller wants.
7110Sstevel@tonic-gate 			 */
7120Sstevel@tonic-gate 			return;
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 		/*
7150Sstevel@tonic-gate 		 * Hmm.  Userland wants a different default.
7160Sstevel@tonic-gate 		 * Do a fake "first trap" to establish the context, then
7170Sstevel@tonic-gate 		 * handle as if we already had a context before we came in.
7180Sstevel@tonic-gate 		 */
7190Sstevel@tonic-gate 		kpreempt_disable();
7200Sstevel@tonic-gate 		fp_seed();
7210Sstevel@tonic-gate 		kpreempt_enable();
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/*
7250Sstevel@tonic-gate 	 * Ensure that the current hardware state is flushed back to the
7260Sstevel@tonic-gate 	 * pcb, then modify that copy.  Next use of the fp will
7270Sstevel@tonic-gate 	 * restore the context.
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 	fp_save(fp);
7300Sstevel@tonic-gate 
731*13134Skuriakose.kuruvilla@oracle.com 	switch (fp_save_mech) {
732*13134Skuriakose.kuruvilla@oracle.com #if defined(__i386)
733*13134Skuriakose.kuruvilla@oracle.com 	case FP_FNSAVE:
734*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_regs.kfpu_u.kfpu_fn.f_fcw = fcw;
735*13134Skuriakose.kuruvilla@oracle.com 		break;
736*13134Skuriakose.kuruvilla@oracle.com #endif
737*13134Skuriakose.kuruvilla@oracle.com 	case FP_FXSAVE:
7380Sstevel@tonic-gate 		fx = &fp->fpu_regs.kfpu_u.kfpu_fx;
7390Sstevel@tonic-gate 		fx->fx_fcw = fcw;
7400Sstevel@tonic-gate 		fx->fx_mxcsr = sse_mxcsr_mask & mxcsr;
7410Sstevel@tonic-gate 		break;
742*13134Skuriakose.kuruvilla@oracle.com 
743*13134Skuriakose.kuruvilla@oracle.com 	case FP_XSAVE:
744*13134Skuriakose.kuruvilla@oracle.com 		fx = &fp->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave;
745*13134Skuriakose.kuruvilla@oracle.com 		fx->fx_fcw = fcw;
746*13134Skuriakose.kuruvilla@oracle.com 		fx->fx_mxcsr = sse_mxcsr_mask & mxcsr;
747*13134Skuriakose.kuruvilla@oracle.com 		/*
748*13134Skuriakose.kuruvilla@oracle.com 		 * Always set LEGACY_FP as it may have been cleared by XSAVE
749*13134Skuriakose.kuruvilla@oracle.com 		 * instruction
750*13134Skuriakose.kuruvilla@oracle.com 		 */
751*13134Skuriakose.kuruvilla@oracle.com 		fp->fpu_regs.kfpu_u.kfpu_xs.xs_xstate_bv |= XFEATURE_LEGACY_FP;
7520Sstevel@tonic-gate 		break;
753*13134Skuriakose.kuruvilla@oracle.com 	default:
754*13134Skuriakose.kuruvilla@oracle.com 		panic("Invalid fp_save_mech");
755*13134Skuriakose.kuruvilla@oracle.com 		/*NOTREACHED*/
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate }
758