xref: /freebsd/sys/amd64/amd64/fpu.c (revision 1965c139f1b3cd56a8dda1ce133ec0e9aebab7c4)
15b81b6b3SRodney W. Grimes /*-
25b81b6b3SRodney W. Grimes  * Copyright (c) 1990 William Jolitz.
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991 The Regents of the University of California.
45b81b6b3SRodney W. Grimes  * All rights reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
75b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
85b81b6b3SRodney W. Grimes  * are met:
95b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
105b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
115b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
125b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
135b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
145b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
155b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
165b81b6b3SRodney W. Grimes  *    without specific prior written permission.
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
195b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
215b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
225b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
235b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
245b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
255b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
265b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
275b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
285b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
295b81b6b3SRodney W. Grimes  *
3021616ec3SPeter Wemm  *	from: @(#)npx.c	7.2 (Berkeley) 5/12/91
315b81b6b3SRodney W. Grimes  */
325b81b6b3SRodney W. Grimes 
3356ae44c5SDavid E. O'Brien #include <sys/cdefs.h>
3456ae44c5SDavid E. O'Brien __FBSDID("$FreeBSD$");
3556ae44c5SDavid E. O'Brien 
36f540b106SGarrett Wollman #include <sys/param.h>
37f540b106SGarrett Wollman #include <sys/systm.h>
386182fdbdSPeter Wemm #include <sys/bus.h>
393a34a5c3SPoul-Henning Kamp #include <sys/kernel.h>
40fb919e4dSMark Murray #include <sys/lock.h>
41cd59d49dSBruce Evans #include <sys/malloc.h>
426182fdbdSPeter Wemm #include <sys/module.h>
43c1ef8aacSJake Burkholder #include <sys/mutex.h>
44fb919e4dSMark Murray #include <sys/mutex.h>
45fb919e4dSMark Murray #include <sys/proc.h>
46fb919e4dSMark Murray #include <sys/sysctl.h>
476182fdbdSPeter Wemm #include <machine/bus.h>
486182fdbdSPeter Wemm #include <sys/rman.h>
49663f1485SBruce Evans #include <sys/signalvar.h>
502f86936aSGarrett Wollman 
517f47cf2fSBruce Evans #include <machine/cputypes.h>
527f47cf2fSBruce Evans #include <machine/frame.h>
530d2a2989SPeter Wemm #include <machine/intr_machdep.h>
54c673fe98SBruce Evans #include <machine/md_var.h>
555400ed3bSPeter Wemm #include <machine/pcb.h>
567f47cf2fSBruce Evans #include <machine/psl.h>
576182fdbdSPeter Wemm #include <machine/resource.h>
58f540b106SGarrett Wollman #include <machine/specialreg.h>
597f47cf2fSBruce Evans #include <machine/segments.h>
6030abe507SJonathan Mini #include <machine/ucontext.h>
612f86936aSGarrett Wollman 
625b81b6b3SRodney W. Grimes /*
63bf2f09eeSPeter Wemm  * Floating point support.
645b81b6b3SRodney W. Grimes  */
655b81b6b3SRodney W. Grimes 
66a5f50ef9SJoerg Wunsch #if defined(__GNUCLIKE_ASM) && !defined(lint)
675b81b6b3SRodney W. Grimes 
6817275403SJung-uk Kim #define	fldcw(cw)		__asm __volatile("fldcw %0" : : "m" (cw))
6930402401SJung-uk Kim #define	fnclex()		__asm __volatile("fnclex")
7030402401SJung-uk Kim #define	fninit()		__asm __volatile("fninit")
711d37f051SBruce Evans #define	fnstcw(addr)		__asm __volatile("fnstcw %0" : "=m" (*(addr)))
722e50fa36SJung-uk Kim #define	fnstsw(addr)		__asm __volatile("fnstsw %0" : "=am" (*(addr)))
7330402401SJung-uk Kim #define	fxrstor(addr)		__asm __volatile("fxrstor %0" : : "m" (*(addr)))
749d146ac5SPeter Wemm #define	fxsave(addr)		__asm __volatile("fxsave %0" : "=m" (*(addr)))
7507c86dcfSJung-uk Kim #define	ldmxcsr(csr)		__asm __volatile("ldmxcsr %0" : : "m" (csr))
76a81f9fedSKonstantin Belousov #define	stmxcsr(addr)		__asm __volatile("stmxcsr %0" : : "m" (*(addr)))
775b81b6b3SRodney W. Grimes 
7894818d19SKonstantin Belousov static __inline void
7994818d19SKonstantin Belousov xrstor(char *addr, uint64_t mask)
8094818d19SKonstantin Belousov {
8194818d19SKonstantin Belousov 	uint32_t low, hi;
8294818d19SKonstantin Belousov 
8394818d19SKonstantin Belousov 	low = mask;
8494818d19SKonstantin Belousov 	hi = mask >> 32;
857574a595SJohn Baldwin 	__asm __volatile("xrstor %0" : : "m" (*addr), "a" (low), "d" (hi));
8694818d19SKonstantin Belousov }
8794818d19SKonstantin Belousov 
8894818d19SKonstantin Belousov static __inline void
8994818d19SKonstantin Belousov xsave(char *addr, uint64_t mask)
9094818d19SKonstantin Belousov {
9194818d19SKonstantin Belousov 	uint32_t low, hi;
9294818d19SKonstantin Belousov 
9394818d19SKonstantin Belousov 	low = mask;
9494818d19SKonstantin Belousov 	hi = mask >> 32;
957574a595SJohn Baldwin 	__asm __volatile("xsave %0" : "=m" (*addr) : "a" (low), "d" (hi) :
967574a595SJohn Baldwin 	    "memory");
9794818d19SKonstantin Belousov }
9894818d19SKonstantin Belousov 
99cf4e1c46SPeter Wemm #else	/* !(__GNUCLIKE_ASM && !lint) */
1005b81b6b3SRodney W. Grimes 
10117275403SJung-uk Kim void	fldcw(u_short cw);
10289c9a483SAlfred Perlstein void	fnclex(void);
10389c9a483SAlfred Perlstein void	fninit(void);
10489c9a483SAlfred Perlstein void	fnstcw(caddr_t addr);
10589c9a483SAlfred Perlstein void	fnstsw(caddr_t addr);
10689c9a483SAlfred Perlstein void	fxsave(caddr_t addr);
10789c9a483SAlfred Perlstein void	fxrstor(caddr_t addr);
10807c86dcfSJung-uk Kim void	ldmxcsr(u_int csr);
109a81f9fedSKonstantin Belousov void	stmxcsr(u_int csr);
11094818d19SKonstantin Belousov void	xrstor(char *addr, uint64_t mask);
11194818d19SKonstantin Belousov void	xsave(char *addr, uint64_t mask);
1125b81b6b3SRodney W. Grimes 
113cf4e1c46SPeter Wemm #endif	/* __GNUCLIKE_ASM && !lint */
1145b81b6b3SRodney W. Grimes 
115d706ec29SJohn Baldwin #define	start_emulating()	load_cr0(rcr0() | CR0_TS)
116d706ec29SJohn Baldwin #define	stop_emulating()	clts()
117d706ec29SJohn Baldwin 
1188c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct savefpu) == 512);
1198c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct xstate_hdr) == 64);
1208c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct savefpu_ymm) == 832);
1218c6f8f3dSKonstantin Belousov 
1228c6f8f3dSKonstantin Belousov /*
1238c6f8f3dSKonstantin Belousov  * This requirement is to make it easier for asm code to calculate
1248c6f8f3dSKonstantin Belousov  * offset of the fpu save area from the pcb address. FPU save area
125b74a2290SKonstantin Belousov  * must be 64-byte aligned.
1268c6f8f3dSKonstantin Belousov  */
1278c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct pcb) % XSAVE_AREA_ALIGN == 0);
1285b81b6b3SRodney W. Grimes 
1292652af56SColin Percival static	void	fpu_clean_state(void);
1302652af56SColin Percival 
1310b7dc0a7SJohn Baldwin SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
1320b7dc0a7SJohn Baldwin     NULL, 1, "Floating point instructions executed in hardware");
1333a34a5c3SPoul-Henning Kamp 
134333d0c60SKonstantin Belousov static int use_xsaveopt;
1358c6f8f3dSKonstantin Belousov int use_xsave;			/* non-static for cpu_switch.S */
1368c6f8f3dSKonstantin Belousov uint64_t xsave_mask;		/* the same */
1378c6f8f3dSKonstantin Belousov static	struct savefpu *fpu_initialstate;
1388c6f8f3dSKonstantin Belousov 
139333d0c60SKonstantin Belousov struct xsave_area_elm_descr {
140333d0c60SKonstantin Belousov 	u_int	offset;
141333d0c60SKonstantin Belousov 	u_int	size;
142333d0c60SKonstantin Belousov } *xsave_area_desc;
143333d0c60SKonstantin Belousov 
1448c6f8f3dSKonstantin Belousov void
1458c6f8f3dSKonstantin Belousov fpusave(void *addr)
1468c6f8f3dSKonstantin Belousov {
1478c6f8f3dSKonstantin Belousov 
1488c6f8f3dSKonstantin Belousov 	if (use_xsave)
1498c6f8f3dSKonstantin Belousov 		xsave((char *)addr, xsave_mask);
1508c6f8f3dSKonstantin Belousov 	else
1518c6f8f3dSKonstantin Belousov 		fxsave((char *)addr);
1528c6f8f3dSKonstantin Belousov }
1538c6f8f3dSKonstantin Belousov 
1548c6f8f3dSKonstantin Belousov static void
1558c6f8f3dSKonstantin Belousov fpurestore(void *addr)
1568c6f8f3dSKonstantin Belousov {
1578c6f8f3dSKonstantin Belousov 
1588c6f8f3dSKonstantin Belousov 	if (use_xsave)
1598c6f8f3dSKonstantin Belousov 		xrstor((char *)addr, xsave_mask);
1608c6f8f3dSKonstantin Belousov 	else
1618c6f8f3dSKonstantin Belousov 		fxrstor((char *)addr);
1628c6f8f3dSKonstantin Belousov }
1633902c3efSSteve Passe 
1645b81b6b3SRodney W. Grimes /*
1658c6f8f3dSKonstantin Belousov  * Enable XSAVE if supported and allowed by user.
1668c6f8f3dSKonstantin Belousov  * Calculate the xsave_mask.
1678c6f8f3dSKonstantin Belousov  */
1688c6f8f3dSKonstantin Belousov static void
1698c6f8f3dSKonstantin Belousov fpuinit_bsp1(void)
1708c6f8f3dSKonstantin Belousov {
1718c6f8f3dSKonstantin Belousov 	u_int cp[4];
1728c6f8f3dSKonstantin Belousov 	uint64_t xsave_mask_user;
1738c6f8f3dSKonstantin Belousov 
1748c6f8f3dSKonstantin Belousov 	if ((cpu_feature2 & CPUID2_XSAVE) != 0) {
1758c6f8f3dSKonstantin Belousov 		use_xsave = 1;
1768c6f8f3dSKonstantin Belousov 		TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave);
1778c6f8f3dSKonstantin Belousov 	}
1788c6f8f3dSKonstantin Belousov 	if (!use_xsave)
1798c6f8f3dSKonstantin Belousov 		return;
1808c6f8f3dSKonstantin Belousov 
1818c6f8f3dSKonstantin Belousov 	cpuid_count(0xd, 0x0, cp);
1828c6f8f3dSKonstantin Belousov 	xsave_mask = XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
1838c6f8f3dSKonstantin Belousov 	if ((cp[0] & xsave_mask) != xsave_mask)
1848c6f8f3dSKonstantin Belousov 		panic("CPU0 does not support X87 or SSE: %x", cp[0]);
1858c6f8f3dSKonstantin Belousov 	xsave_mask = ((uint64_t)cp[3] << 32) | cp[0];
1868c6f8f3dSKonstantin Belousov 	xsave_mask_user = xsave_mask;
1878c6f8f3dSKonstantin Belousov 	TUNABLE_ULONG_FETCH("hw.xsave_mask", &xsave_mask_user);
1888c6f8f3dSKonstantin Belousov 	xsave_mask_user |= XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
1898c6f8f3dSKonstantin Belousov 	xsave_mask &= xsave_mask_user;
190333d0c60SKonstantin Belousov 
191333d0c60SKonstantin Belousov 	cpuid_count(0xd, 0x1, cp);
192333d0c60SKonstantin Belousov 	if ((cp[0] & CPUID_EXTSTATE_XSAVEOPT) != 0) {
193333d0c60SKonstantin Belousov 		/*
194333d0c60SKonstantin Belousov 		 * Patch the XSAVE instruction in the cpu_switch code
195333d0c60SKonstantin Belousov 		 * to XSAVEOPT.  We assume that XSAVE encoding used
196333d0c60SKonstantin Belousov 		 * REX byte, and set the bit 4 of the r/m byte.
197333d0c60SKonstantin Belousov 		 */
198333d0c60SKonstantin Belousov 		ctx_switch_xsave[3] |= 0x10;
199333d0c60SKonstantin Belousov 		use_xsaveopt = 1;
200333d0c60SKonstantin Belousov 	}
2018c6f8f3dSKonstantin Belousov }
2028c6f8f3dSKonstantin Belousov 
2038c6f8f3dSKonstantin Belousov /*
2048c6f8f3dSKonstantin Belousov  * Calculate the fpu save area size.
2058c6f8f3dSKonstantin Belousov  */
2068c6f8f3dSKonstantin Belousov static void
2078c6f8f3dSKonstantin Belousov fpuinit_bsp2(void)
2088c6f8f3dSKonstantin Belousov {
2098c6f8f3dSKonstantin Belousov 	u_int cp[4];
2108c6f8f3dSKonstantin Belousov 
2118c6f8f3dSKonstantin Belousov 	if (use_xsave) {
2128c6f8f3dSKonstantin Belousov 		cpuid_count(0xd, 0x0, cp);
2138c6f8f3dSKonstantin Belousov 		cpu_max_ext_state_size = cp[1];
2148c6f8f3dSKonstantin Belousov 
2158c6f8f3dSKonstantin Belousov 		/*
2168c6f8f3dSKonstantin Belousov 		 * Reload the cpu_feature2, since we enabled OSXSAVE.
2178c6f8f3dSKonstantin Belousov 		 */
2188c6f8f3dSKonstantin Belousov 		do_cpuid(1, cp);
2198c6f8f3dSKonstantin Belousov 		cpu_feature2 = cp[2];
2208c6f8f3dSKonstantin Belousov 	} else
2218c6f8f3dSKonstantin Belousov 		cpu_max_ext_state_size = sizeof(struct savefpu);
2228c6f8f3dSKonstantin Belousov }
2238c6f8f3dSKonstantin Belousov 
2248c6f8f3dSKonstantin Belousov /*
2258c6f8f3dSKonstantin Belousov  * Initialize the floating point unit.
226da4113b3SPeter Wemm  */
227398dbb11SPeter Wemm void
2281c89210cSPeter Wemm fpuinit(void)
229da4113b3SPeter Wemm {
2300689bdccSJohn Baldwin 	register_t saveintr;
23196a7759eSPeter Wemm 	u_int mxcsr;
232398dbb11SPeter Wemm 	u_short control;
233da4113b3SPeter Wemm 
2348c6f8f3dSKonstantin Belousov 	if (IS_BSP())
2358c6f8f3dSKonstantin Belousov 		fpuinit_bsp1();
2368c6f8f3dSKonstantin Belousov 
2378c6f8f3dSKonstantin Belousov 	if (use_xsave) {
2388c6f8f3dSKonstantin Belousov 		load_cr4(rcr4() | CR4_XSAVE);
2397574a595SJohn Baldwin 		load_xcr(XCR0, xsave_mask);
2408c6f8f3dSKonstantin Belousov 	}
2418c6f8f3dSKonstantin Belousov 
2428c6f8f3dSKonstantin Belousov 	/*
2438c6f8f3dSKonstantin Belousov 	 * XCR0 shall be set up before CPU can report the save area size.
2448c6f8f3dSKonstantin Belousov 	 */
2458c6f8f3dSKonstantin Belousov 	if (IS_BSP())
2468c6f8f3dSKonstantin Belousov 		fpuinit_bsp2();
2478c6f8f3dSKonstantin Belousov 
24899753495SKonstantin Belousov 	/*
24999753495SKonstantin Belousov 	 * It is too early for critical_enter() to work on AP.
25099753495SKonstantin Belousov 	 */
2510689bdccSJohn Baldwin 	saveintr = intr_disable();
2525b81b6b3SRodney W. Grimes 	stop_emulating();
2535b81b6b3SRodney W. Grimes 	fninit();
254398dbb11SPeter Wemm 	control = __INITIAL_FPUCW__;
25517275403SJung-uk Kim 	fldcw(control);
25696a7759eSPeter Wemm 	mxcsr = __INITIAL_MXCSR__;
25796a7759eSPeter Wemm 	ldmxcsr(mxcsr);
258a8346a98SJohn Baldwin 	start_emulating();
2590689bdccSJohn Baldwin 	intr_restore(saveintr);
2605b81b6b3SRodney W. Grimes }
2615b81b6b3SRodney W. Grimes 
2625b81b6b3SRodney W. Grimes /*
2638c6f8f3dSKonstantin Belousov  * On the boot CPU we generate a clean state that is used to
2648c6f8f3dSKonstantin Belousov  * initialize the floating point unit when it is first used by a
2658c6f8f3dSKonstantin Belousov  * process.
2668c6f8f3dSKonstantin Belousov  */
2678c6f8f3dSKonstantin Belousov static void
2688c6f8f3dSKonstantin Belousov fpuinitstate(void *arg __unused)
2698c6f8f3dSKonstantin Belousov {
2708c6f8f3dSKonstantin Belousov 	register_t saveintr;
271333d0c60SKonstantin Belousov 	int cp[4], i, max_ext_n;
2728c6f8f3dSKonstantin Belousov 
2738c6f8f3dSKonstantin Belousov 	fpu_initialstate = malloc(cpu_max_ext_state_size, M_DEVBUF,
2748c6f8f3dSKonstantin Belousov 	    M_WAITOK | M_ZERO);
2758c6f8f3dSKonstantin Belousov 	saveintr = intr_disable();
2768c6f8f3dSKonstantin Belousov 	stop_emulating();
2778c6f8f3dSKonstantin Belousov 
2788c6f8f3dSKonstantin Belousov 	fpusave(fpu_initialstate);
2798c6f8f3dSKonstantin Belousov 	if (fpu_initialstate->sv_env.en_mxcsr_mask)
2808c6f8f3dSKonstantin Belousov 		cpu_mxcsr_mask = fpu_initialstate->sv_env.en_mxcsr_mask;
2818c6f8f3dSKonstantin Belousov 	else
2828c6f8f3dSKonstantin Belousov 		cpu_mxcsr_mask = 0xFFBF;
2838c6f8f3dSKonstantin Belousov 
2848c6f8f3dSKonstantin Belousov 	/*
2858c6f8f3dSKonstantin Belousov 	 * The fninit instruction does not modify XMM registers.  The
2868c6f8f3dSKonstantin Belousov 	 * fpusave call dumped the garbage contained in the registers
2878c6f8f3dSKonstantin Belousov 	 * after reset to the initial state saved.  Clear XMM
2888c6f8f3dSKonstantin Belousov 	 * registers file image to make the startup program state and
2898c6f8f3dSKonstantin Belousov 	 * signal handler XMM register content predictable.
2908c6f8f3dSKonstantin Belousov 	 */
2918c6f8f3dSKonstantin Belousov 	bzero(&fpu_initialstate->sv_xmm[0], sizeof(struct xmmacc));
2928c6f8f3dSKonstantin Belousov 
293333d0c60SKonstantin Belousov 	/*
294333d0c60SKonstantin Belousov 	 * Create a table describing the layout of the CPU Extended
295333d0c60SKonstantin Belousov 	 * Save Area.
296333d0c60SKonstantin Belousov 	 */
297333d0c60SKonstantin Belousov 	if (use_xsaveopt) {
298333d0c60SKonstantin Belousov 		max_ext_n = flsl(xsave_mask);
299333d0c60SKonstantin Belousov 		xsave_area_desc = malloc(max_ext_n * sizeof(struct
300333d0c60SKonstantin Belousov 		    xsave_area_elm_descr), M_DEVBUF, M_WAITOK | M_ZERO);
301333d0c60SKonstantin Belousov 		/* x87 state */
302333d0c60SKonstantin Belousov 		xsave_area_desc[0].offset = 0;
303333d0c60SKonstantin Belousov 		xsave_area_desc[0].size = 160;
304333d0c60SKonstantin Belousov 		/* XMM */
305333d0c60SKonstantin Belousov 		xsave_area_desc[1].offset = 160;
306333d0c60SKonstantin Belousov 		xsave_area_desc[1].size = 288 - 160;
307333d0c60SKonstantin Belousov 
308333d0c60SKonstantin Belousov 		for (i = 2; i < max_ext_n; i++) {
309333d0c60SKonstantin Belousov 			cpuid_count(0xd, i, cp);
310333d0c60SKonstantin Belousov 			xsave_area_desc[i].offset = cp[1];
311333d0c60SKonstantin Belousov 			xsave_area_desc[i].size = cp[0];
312333d0c60SKonstantin Belousov 		}
313333d0c60SKonstantin Belousov 	}
314333d0c60SKonstantin Belousov 
3158c6f8f3dSKonstantin Belousov 	start_emulating();
3168c6f8f3dSKonstantin Belousov 	intr_restore(saveintr);
3178c6f8f3dSKonstantin Belousov }
3188c6f8f3dSKonstantin Belousov SYSINIT(fpuinitstate, SI_SUB_DRIVERS, SI_ORDER_ANY, fpuinitstate, NULL);
3198c6f8f3dSKonstantin Belousov 
3208c6f8f3dSKonstantin Belousov /*
3215b81b6b3SRodney W. Grimes  * Free coprocessor (if we have it).
3225b81b6b3SRodney W. Grimes  */
3235b81b6b3SRodney W. Grimes void
324bf2f09eeSPeter Wemm fpuexit(struct thread *td)
3255b81b6b3SRodney W. Grimes {
3265b81b6b3SRodney W. Grimes 
32799753495SKonstantin Belousov 	critical_enter();
3281c89210cSPeter Wemm 	if (curthread == PCPU_GET(fpcurthread)) {
3291c89210cSPeter Wemm 		stop_emulating();
33083b22b05SKonstantin Belousov 		fpusave(curpcb->pcb_save);
3311c89210cSPeter Wemm 		start_emulating();
3321c89210cSPeter Wemm 		PCPU_SET(fpcurthread, 0);
3331c89210cSPeter Wemm 	}
33499753495SKonstantin Belousov 	critical_exit();
3355b81b6b3SRodney W. Grimes }
3365b81b6b3SRodney W. Grimes 
33730abe507SJonathan Mini int
338bf2f09eeSPeter Wemm fpuformat()
33930abe507SJonathan Mini {
34030abe507SJonathan Mini 
34130abe507SJonathan Mini 	return (_MC_FPFMT_XMM);
34230abe507SJonathan Mini }
34330abe507SJonathan Mini 
3445b81b6b3SRodney W. Grimes /*
345a7674320SMartin Cracauer  * The following mechanism is used to ensure that the FPE_... value
346a7674320SMartin Cracauer  * that is passed as a trapcode to the signal handler of the user
347a7674320SMartin Cracauer  * process does not have more than one bit set.
348a7674320SMartin Cracauer  *
349a7674320SMartin Cracauer  * Multiple bits may be set if the user process modifies the control
350a7674320SMartin Cracauer  * word while a status word bit is already set.  While this is a sign
351a7674320SMartin Cracauer  * of bad coding, we have no choise than to narrow them down to one
352a7674320SMartin Cracauer  * bit, since we must not send a trapcode that is not exactly one of
353a7674320SMartin Cracauer  * the FPE_ macros.
354a7674320SMartin Cracauer  *
355a7674320SMartin Cracauer  * The mechanism has a static table with 127 entries.  Each combination
356a7674320SMartin Cracauer  * of the 7 FPU status word exception bits directly translates to a
357a7674320SMartin Cracauer  * position in this table, where a single FPE_... value is stored.
358a7674320SMartin Cracauer  * This FPE_... value stored there is considered the "most important"
359a7674320SMartin Cracauer  * of the exception bits and will be sent as the signal code.  The
360a7674320SMartin Cracauer  * precedence of the bits is based upon Intel Document "Numerical
361a7674320SMartin Cracauer  * Applications", Chapter "Special Computational Situations".
362a7674320SMartin Cracauer  *
363a7674320SMartin Cracauer  * The macro to choose one of these values does these steps: 1) Throw
364a7674320SMartin Cracauer  * away status word bits that cannot be masked.  2) Throw away the bits
365a7674320SMartin Cracauer  * currently masked in the control word, assuming the user isn't
366a7674320SMartin Cracauer  * interested in them anymore.  3) Reinsert status word bit 7 (stack
367a7674320SMartin Cracauer  * fault) if it is set, which cannot be masked but must be presered.
368a7674320SMartin Cracauer  * 4) Use the remaining bits to point into the trapcode table.
369a7674320SMartin Cracauer  *
370a7674320SMartin Cracauer  * The 6 maskable bits in order of their preference, as stated in the
371a7674320SMartin Cracauer  * above referenced Intel manual:
372a7674320SMartin Cracauer  * 1  Invalid operation (FP_X_INV)
373a7674320SMartin Cracauer  * 1a   Stack underflow
374a7674320SMartin Cracauer  * 1b   Stack overflow
375a7674320SMartin Cracauer  * 1c   Operand of unsupported format
376a7674320SMartin Cracauer  * 1d   SNaN operand.
377a7674320SMartin Cracauer  * 2  QNaN operand (not an exception, irrelavant here)
378a7674320SMartin Cracauer  * 3  Any other invalid-operation not mentioned above or zero divide
379a7674320SMartin Cracauer  *      (FP_X_INV, FP_X_DZ)
380a7674320SMartin Cracauer  * 4  Denormal operand (FP_X_DNML)
381a7674320SMartin Cracauer  * 5  Numeric over/underflow (FP_X_OFL, FP_X_UFL)
382784648c6SMartin Cracauer  * 6  Inexact result (FP_X_IMP)
383784648c6SMartin Cracauer  */
384a7674320SMartin Cracauer static char fpetable[128] = {
385a7674320SMartin Cracauer 	0,
386a7674320SMartin Cracauer 	FPE_FLTINV,	/*  1 - INV */
387a7674320SMartin Cracauer 	FPE_FLTUND,	/*  2 - DNML */
388a7674320SMartin Cracauer 	FPE_FLTINV,	/*  3 - INV | DNML */
389a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  4 - DZ */
390a7674320SMartin Cracauer 	FPE_FLTINV,	/*  5 - INV | DZ */
391a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  6 - DNML | DZ */
392a7674320SMartin Cracauer 	FPE_FLTINV,	/*  7 - INV | DNML | DZ */
393a7674320SMartin Cracauer 	FPE_FLTOVF,	/*  8 - OFL */
394a7674320SMartin Cracauer 	FPE_FLTINV,	/*  9 - INV | OFL */
395a7674320SMartin Cracauer 	FPE_FLTUND,	/*  A - DNML | OFL */
396a7674320SMartin Cracauer 	FPE_FLTINV,	/*  B - INV | DNML | OFL */
397a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  C - DZ | OFL */
398a7674320SMartin Cracauer 	FPE_FLTINV,	/*  D - INV | DZ | OFL */
399a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  E - DNML | DZ | OFL */
400a7674320SMartin Cracauer 	FPE_FLTINV,	/*  F - INV | DNML | DZ | OFL */
401a7674320SMartin Cracauer 	FPE_FLTUND,	/* 10 - UFL */
402a7674320SMartin Cracauer 	FPE_FLTINV,	/* 11 - INV | UFL */
403a7674320SMartin Cracauer 	FPE_FLTUND,	/* 12 - DNML | UFL */
404a7674320SMartin Cracauer 	FPE_FLTINV,	/* 13 - INV | DNML | UFL */
405a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 14 - DZ | UFL */
406a7674320SMartin Cracauer 	FPE_FLTINV,	/* 15 - INV | DZ | UFL */
407a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 16 - DNML | DZ | UFL */
408a7674320SMartin Cracauer 	FPE_FLTINV,	/* 17 - INV | DNML | DZ | UFL */
409a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 18 - OFL | UFL */
410a7674320SMartin Cracauer 	FPE_FLTINV,	/* 19 - INV | OFL | UFL */
411a7674320SMartin Cracauer 	FPE_FLTUND,	/* 1A - DNML | OFL | UFL */
412a7674320SMartin Cracauer 	FPE_FLTINV,	/* 1B - INV | DNML | OFL | UFL */
413a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 1C - DZ | OFL | UFL */
414a7674320SMartin Cracauer 	FPE_FLTINV,	/* 1D - INV | DZ | OFL | UFL */
415a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 1E - DNML | DZ | OFL | UFL */
416a7674320SMartin Cracauer 	FPE_FLTINV,	/* 1F - INV | DNML | DZ | OFL | UFL */
417a7674320SMartin Cracauer 	FPE_FLTRES,	/* 20 - IMP */
418a7674320SMartin Cracauer 	FPE_FLTINV,	/* 21 - INV | IMP */
419a7674320SMartin Cracauer 	FPE_FLTUND,	/* 22 - DNML | IMP */
420a7674320SMartin Cracauer 	FPE_FLTINV,	/* 23 - INV | DNML | IMP */
421a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 24 - DZ | IMP */
422a7674320SMartin Cracauer 	FPE_FLTINV,	/* 25 - INV | DZ | IMP */
423a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 26 - DNML | DZ | IMP */
424a7674320SMartin Cracauer 	FPE_FLTINV,	/* 27 - INV | DNML | DZ | IMP */
425a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 28 - OFL | IMP */
426a7674320SMartin Cracauer 	FPE_FLTINV,	/* 29 - INV | OFL | IMP */
427a7674320SMartin Cracauer 	FPE_FLTUND,	/* 2A - DNML | OFL | IMP */
428a7674320SMartin Cracauer 	FPE_FLTINV,	/* 2B - INV | DNML | OFL | IMP */
429a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 2C - DZ | OFL | IMP */
430a7674320SMartin Cracauer 	FPE_FLTINV,	/* 2D - INV | DZ | OFL | IMP */
431a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 2E - DNML | DZ | OFL | IMP */
432a7674320SMartin Cracauer 	FPE_FLTINV,	/* 2F - INV | DNML | DZ | OFL | IMP */
433a7674320SMartin Cracauer 	FPE_FLTUND,	/* 30 - UFL | IMP */
434a7674320SMartin Cracauer 	FPE_FLTINV,	/* 31 - INV | UFL | IMP */
435a7674320SMartin Cracauer 	FPE_FLTUND,	/* 32 - DNML | UFL | IMP */
436a7674320SMartin Cracauer 	FPE_FLTINV,	/* 33 - INV | DNML | UFL | IMP */
437a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 34 - DZ | UFL | IMP */
438a7674320SMartin Cracauer 	FPE_FLTINV,	/* 35 - INV | DZ | UFL | IMP */
439a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 36 - DNML | DZ | UFL | IMP */
440a7674320SMartin Cracauer 	FPE_FLTINV,	/* 37 - INV | DNML | DZ | UFL | IMP */
441a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 38 - OFL | UFL | IMP */
442a7674320SMartin Cracauer 	FPE_FLTINV,	/* 39 - INV | OFL | UFL | IMP */
443a7674320SMartin Cracauer 	FPE_FLTUND,	/* 3A - DNML | OFL | UFL | IMP */
444a7674320SMartin Cracauer 	FPE_FLTINV,	/* 3B - INV | DNML | OFL | UFL | IMP */
445a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 3C - DZ | OFL | UFL | IMP */
446a7674320SMartin Cracauer 	FPE_FLTINV,	/* 3D - INV | DZ | OFL | UFL | IMP */
447a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 3E - DNML | DZ | OFL | UFL | IMP */
448a7674320SMartin Cracauer 	FPE_FLTINV,	/* 3F - INV | DNML | DZ | OFL | UFL | IMP */
449a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 40 - STK */
450a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 41 - INV | STK */
451a7674320SMartin Cracauer 	FPE_FLTUND,	/* 42 - DNML | STK */
452a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 43 - INV | DNML | STK */
453a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 44 - DZ | STK */
454a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 45 - INV | DZ | STK */
455a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 46 - DNML | DZ | STK */
456a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 47 - INV | DNML | DZ | STK */
457a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 48 - OFL | STK */
458a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 49 - INV | OFL | STK */
459a7674320SMartin Cracauer 	FPE_FLTUND,	/* 4A - DNML | OFL | STK */
460a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 4B - INV | DNML | OFL | STK */
461a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 4C - DZ | OFL | STK */
462a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 4D - INV | DZ | OFL | STK */
463a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 4E - DNML | DZ | OFL | STK */
464a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 4F - INV | DNML | DZ | OFL | STK */
465a7674320SMartin Cracauer 	FPE_FLTUND,	/* 50 - UFL | STK */
466a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 51 - INV | UFL | STK */
467a7674320SMartin Cracauer 	FPE_FLTUND,	/* 52 - DNML | UFL | STK */
468a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 53 - INV | DNML | UFL | STK */
469a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 54 - DZ | UFL | STK */
470a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 55 - INV | DZ | UFL | STK */
471a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 56 - DNML | DZ | UFL | STK */
472a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 57 - INV | DNML | DZ | UFL | STK */
473a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 58 - OFL | UFL | STK */
474a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 59 - INV | OFL | UFL | STK */
475a7674320SMartin Cracauer 	FPE_FLTUND,	/* 5A - DNML | OFL | UFL | STK */
476a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 5B - INV | DNML | OFL | UFL | STK */
477a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 5C - DZ | OFL | UFL | STK */
478a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 5D - INV | DZ | OFL | UFL | STK */
479a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 5E - DNML | DZ | OFL | UFL | STK */
480a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 5F - INV | DNML | DZ | OFL | UFL | STK */
481a7674320SMartin Cracauer 	FPE_FLTRES,	/* 60 - IMP | STK */
482a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 61 - INV | IMP | STK */
483a7674320SMartin Cracauer 	FPE_FLTUND,	/* 62 - DNML | IMP | STK */
484a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 63 - INV | DNML | IMP | STK */
485a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 64 - DZ | IMP | STK */
486a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 65 - INV | DZ | IMP | STK */
487a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 66 - DNML | DZ | IMP | STK */
488a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 67 - INV | DNML | DZ | IMP | STK */
489a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 68 - OFL | IMP | STK */
490a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 69 - INV | OFL | IMP | STK */
491a7674320SMartin Cracauer 	FPE_FLTUND,	/* 6A - DNML | OFL | IMP | STK */
492a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 6B - INV | DNML | OFL | IMP | STK */
493a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 6C - DZ | OFL | IMP | STK */
494a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 6D - INV | DZ | OFL | IMP | STK */
495a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 6E - DNML | DZ | OFL | IMP | STK */
496a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 6F - INV | DNML | DZ | OFL | IMP | STK */
497a7674320SMartin Cracauer 	FPE_FLTUND,	/* 70 - UFL | IMP | STK */
498a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 71 - INV | UFL | IMP | STK */
499a7674320SMartin Cracauer 	FPE_FLTUND,	/* 72 - DNML | UFL | IMP | STK */
500a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 73 - INV | DNML | UFL | IMP | STK */
501a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 74 - DZ | UFL | IMP | STK */
502a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 75 - INV | DZ | UFL | IMP | STK */
503a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 76 - DNML | DZ | UFL | IMP | STK */
504a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 77 - INV | DNML | DZ | UFL | IMP | STK */
505a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 78 - OFL | UFL | IMP | STK */
506a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 79 - INV | OFL | UFL | IMP | STK */
507a7674320SMartin Cracauer 	FPE_FLTUND,	/* 7A - DNML | OFL | UFL | IMP | STK */
508a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 7B - INV | DNML | OFL | UFL | IMP | STK */
509a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 7C - DZ | OFL | UFL | IMP | STK */
510a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 7D - INV | DZ | OFL | UFL | IMP | STK */
511a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 7E - DNML | DZ | OFL | UFL | IMP | STK */
512a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 7F - INV | DNML | DZ | OFL | UFL | IMP | STK */
513a7674320SMartin Cracauer };
514a7674320SMartin Cracauer 
515a7674320SMartin Cracauer /*
516dfa8a512SKonstantin Belousov  * Read the FP status and control words, then generate si_code value
517dfa8a512SKonstantin Belousov  * for SIGFPE.  The error code chosen will be one of the
518dfa8a512SKonstantin Belousov  * FPE_... macros. It will be sent as the second argument to old
519dfa8a512SKonstantin Belousov  * BSD-style signal handlers and as "siginfo_t->si_code" (second
520dfa8a512SKonstantin Belousov  * argument) to SA_SIGINFO signal handlers.
5215b81b6b3SRodney W. Grimes  *
522dfa8a512SKonstantin Belousov  * Some time ago, we cleared the x87 exceptions with FNCLEX there.
523dfa8a512SKonstantin Belousov  * Clearing exceptions was necessary mainly to avoid IRQ13 bugs.  The
524dfa8a512SKonstantin Belousov  * usermode code which understands the FPU hardware enough to enable
525dfa8a512SKonstantin Belousov  * the exceptions, can also handle clearing the exception state in the
526dfa8a512SKonstantin Belousov  * handler. The only consequence of not clearing the exception is the
527dfa8a512SKonstantin Belousov  * rethrow of the SIGFPE on return from the signal handler and
528dfa8a512SKonstantin Belousov  * reexecution of the corresponding instruction.
529bc84db62SKonstantin Belousov  *
530dfa8a512SKonstantin Belousov  * For XMM traps, the exceptions were never cleared.
5315b81b6b3SRodney W. Grimes  */
5321c1771cbSBruce Evans int
533bc84db62SKonstantin Belousov fputrap_x87(void)
5345b81b6b3SRodney W. Grimes {
535bc84db62SKonstantin Belousov 	struct savefpu *pcb_save;
5361c1771cbSBruce Evans 	u_short control, status;
5375b81b6b3SRodney W. Grimes 
53899753495SKonstantin Belousov 	critical_enter();
5395b81b6b3SRodney W. Grimes 
5405b81b6b3SRodney W. Grimes 	/*
5411c1771cbSBruce Evans 	 * Interrupt handling (for another interrupt) may have pushed the
5421c1771cbSBruce Evans 	 * state to memory.  Fetch the relevant parts of the state from
5431c1771cbSBruce Evans 	 * wherever they are.
5445b81b6b3SRodney W. Grimes 	 */
5450bbc8826SJohn Baldwin 	if (PCPU_GET(fpcurthread) != curthread) {
54683b22b05SKonstantin Belousov 		pcb_save = curpcb->pcb_save;
547bc84db62SKonstantin Belousov 		control = pcb_save->sv_env.en_cw;
548bc84db62SKonstantin Belousov 		status = pcb_save->sv_env.en_sw;
5495b81b6b3SRodney W. Grimes 	} else {
5501c1771cbSBruce Evans 		fnstcw(&control);
5511c1771cbSBruce Evans 		fnstsw(&status);
5525b81b6b3SRodney W. Grimes 	}
5531c1771cbSBruce Evans 
55499753495SKonstantin Belousov 	critical_exit();
5551c1771cbSBruce Evans 	return (fpetable[status & ((~control & 0x3f) | 0x40)]);
5565b81b6b3SRodney W. Grimes }
5575b81b6b3SRodney W. Grimes 
558bc84db62SKonstantin Belousov int
559bc84db62SKonstantin Belousov fputrap_sse(void)
560bc84db62SKonstantin Belousov {
561bc84db62SKonstantin Belousov 	u_int mxcsr;
562bc84db62SKonstantin Belousov 
563bc84db62SKonstantin Belousov 	critical_enter();
564bc84db62SKonstantin Belousov 	if (PCPU_GET(fpcurthread) != curthread)
56583b22b05SKonstantin Belousov 		mxcsr = curpcb->pcb_save->sv_env.en_mxcsr;
566bc84db62SKonstantin Belousov 	else
567bc84db62SKonstantin Belousov 		stmxcsr(&mxcsr);
568bc84db62SKonstantin Belousov 	critical_exit();
569bc84db62SKonstantin Belousov 	return (fpetable[(mxcsr & (~mxcsr >> 7)) & 0x3f]);
570bc84db62SKonstantin Belousov }
571bc84db62SKonstantin Belousov 
5725b81b6b3SRodney W. Grimes /*
5735b81b6b3SRodney W. Grimes  * Implement device not available (DNA) exception
5745b81b6b3SRodney W. Grimes  *
5750bbc8826SJohn Baldwin  * It would be better to switch FP context here (if curthread != fpcurthread)
57637e52b59SBruce Evans  * and not necessarily for every context switch, but it is too hard to
57737e52b59SBruce Evans  * access foreign pcb's.
5785b81b6b3SRodney W. Grimes  */
57930abe507SJonathan Mini 
58030abe507SJonathan Mini static int err_count = 0;
58130abe507SJonathan Mini 
582a8346a98SJohn Baldwin void
583a8346a98SJohn Baldwin fpudna(void)
5845b81b6b3SRodney W. Grimes {
58505f6ee66SJake Burkholder 
58699753495SKonstantin Belousov 	critical_enter();
58730abe507SJonathan Mini 	if (PCPU_GET(fpcurthread) == curthread) {
588bf2f09eeSPeter Wemm 		printf("fpudna: fpcurthread == curthread %d times\n",
58930abe507SJonathan Mini 		    ++err_count);
59030abe507SJonathan Mini 		stop_emulating();
59199753495SKonstantin Belousov 		critical_exit();
592a8346a98SJohn Baldwin 		return;
59330abe507SJonathan Mini 	}
5940bbc8826SJohn Baldwin 	if (PCPU_GET(fpcurthread) != NULL) {
595bf2f09eeSPeter Wemm 		printf("fpudna: fpcurthread = %p (%d), curthread = %p (%d)\n",
59630abe507SJonathan Mini 		       PCPU_GET(fpcurthread),
59730abe507SJonathan Mini 		       PCPU_GET(fpcurthread)->td_proc->p_pid,
59830abe507SJonathan Mini 		       curthread, curthread->td_proc->p_pid);
599bf2f09eeSPeter Wemm 		panic("fpudna");
6005b81b6b3SRodney W. Grimes 	}
6015b81b6b3SRodney W. Grimes 	stop_emulating();
6025b81b6b3SRodney W. Grimes 	/*
603bf2f09eeSPeter Wemm 	 * Record new context early in case frstor causes a trap.
6045b81b6b3SRodney W. Grimes 	 */
6050bbc8826SJohn Baldwin 	PCPU_SET(fpcurthread, curthread);
6069d146ac5SPeter Wemm 
6072652af56SColin Percival 	fpu_clean_state();
6082652af56SColin Percival 
609*1965c139SKonstantin Belousov 	if ((curpcb->pcb_flags & PCB_FPUINITDONE) == 0) {
6105b81b6b3SRodney W. Grimes 		/*
61163de9515SJohn Baldwin 		 * This is the first time this thread has used the FPU or
61263de9515SJohn Baldwin 		 * the PCB doesn't contain a clean FPU state.  Explicitly
61363de9515SJohn Baldwin 		 * load an initial state.
614333d0c60SKonstantin Belousov 		 *
615333d0c60SKonstantin Belousov 		 * We prefer to restore the state from the actual save
616333d0c60SKonstantin Belousov 		 * area in PCB instead of directly loading from
617333d0c60SKonstantin Belousov 		 * fpu_initialstate, to ignite the XSAVEOPT
618333d0c60SKonstantin Belousov 		 * tracking engine.
6195b81b6b3SRodney W. Grimes 		 */
620*1965c139SKonstantin Belousov 		bcopy(fpu_initialstate, curpcb->pcb_save, cpu_max_ext_state_size);
621*1965c139SKonstantin Belousov 		fpurestore(curpcb->pcb_save);
622*1965c139SKonstantin Belousov 		if (curpcb->pcb_initial_fpucw != __INITIAL_FPUCW__)
623*1965c139SKonstantin Belousov 			fldcw(curpcb->pcb_initial_fpucw);
624*1965c139SKonstantin Belousov 		if (PCB_USER_FPU(curpcb))
625*1965c139SKonstantin Belousov 			set_pcb_flags(curpcb,
626e6c006d9SJung-uk Kim 			    PCB_FPUINITDONE | PCB_USERFPUINITDONE);
627e6c006d9SJung-uk Kim 		else
628*1965c139SKonstantin Belousov 			set_pcb_flags(curpcb, PCB_FPUINITDONE);
6291c89210cSPeter Wemm 	} else
630*1965c139SKonstantin Belousov 		fpurestore(curpcb->pcb_save);
63199753495SKonstantin Belousov 	critical_exit();
6325b81b6b3SRodney W. Grimes }
6335b81b6b3SRodney W. Grimes 
63430abe507SJonathan Mini void
635bf2f09eeSPeter Wemm fpudrop()
63630abe507SJonathan Mini {
63730abe507SJonathan Mini 	struct thread *td;
63830abe507SJonathan Mini 
63930abe507SJonathan Mini 	td = PCPU_GET(fpcurthread);
64099753495SKonstantin Belousov 	KASSERT(td == curthread, ("fpudrop: fpcurthread != curthread"));
6414a23ecc7SKonstantin Belousov 	CRITICAL_ASSERT(td);
64230abe507SJonathan Mini 	PCPU_SET(fpcurthread, NULL);
643e6c006d9SJung-uk Kim 	clear_pcb_flags(td->td_pcb, PCB_FPUINITDONE);
64430abe507SJonathan Mini 	start_emulating();
64530abe507SJonathan Mini }
64630abe507SJonathan Mini 
64730abe507SJonathan Mini /*
6485c6eb037SKonstantin Belousov  * Get the user state of the FPU into pcb->pcb_user_save without
6495c6eb037SKonstantin Belousov  * dropping ownership (if possible).  It returns the FPU ownership
6505c6eb037SKonstantin Belousov  * status.
65130abe507SJonathan Mini  */
65230abe507SJonathan Mini int
6535c6eb037SKonstantin Belousov fpugetregs(struct thread *td)
6546cf9a08dSKonstantin Belousov {
6556cf9a08dSKonstantin Belousov 	struct pcb *pcb;
656333d0c60SKonstantin Belousov 	uint64_t *xstate_bv, bit;
657333d0c60SKonstantin Belousov 	char *sa;
658333d0c60SKonstantin Belousov 	int max_ext_n, i;
6596cf9a08dSKonstantin Belousov 
6606cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
6616cf9a08dSKonstantin Belousov 	if ((pcb->pcb_flags & PCB_USERFPUINITDONE) == 0) {
6628c6f8f3dSKonstantin Belousov 		bcopy(fpu_initialstate, get_pcb_user_save_pcb(pcb),
6638c6f8f3dSKonstantin Belousov 		    cpu_max_ext_state_size);
6648c6f8f3dSKonstantin Belousov 		get_pcb_user_save_pcb(pcb)->sv_env.en_cw =
6658c6f8f3dSKonstantin Belousov 		    pcb->pcb_initial_fpucw;
6665c6eb037SKonstantin Belousov 		fpuuserinited(td);
6675c6eb037SKonstantin Belousov 		return (_MC_FPOWNED_PCB);
6686cf9a08dSKonstantin Belousov 	}
66999753495SKonstantin Belousov 	critical_enter();
6706cf9a08dSKonstantin Belousov 	if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) {
6718c6f8f3dSKonstantin Belousov 		fpusave(get_pcb_user_save_pcb(pcb));
67299753495SKonstantin Belousov 		critical_exit();
6736cf9a08dSKonstantin Belousov 		return (_MC_FPOWNED_FPU);
6746cf9a08dSKonstantin Belousov 	} else {
67599753495SKonstantin Belousov 		critical_exit();
676333d0c60SKonstantin Belousov 		if (use_xsaveopt) {
677333d0c60SKonstantin Belousov 			/*
678333d0c60SKonstantin Belousov 			 * Handle partially saved state.
679333d0c60SKonstantin Belousov 			 */
680333d0c60SKonstantin Belousov 			sa = (char *)get_pcb_user_save_pcb(pcb);
681333d0c60SKonstantin Belousov 			xstate_bv = (uint64_t *)(sa + sizeof(struct savefpu) +
682333d0c60SKonstantin Belousov 			    offsetof(struct xstate_hdr, xstate_bv));
683333d0c60SKonstantin Belousov 			max_ext_n = flsl(xsave_mask);
684333d0c60SKonstantin Belousov 			for (i = 0; i < max_ext_n; i++) {
685333d0c60SKonstantin Belousov 				bit = 1 << i;
686333d0c60SKonstantin Belousov 				if ((*xstate_bv & bit) != 0)
687333d0c60SKonstantin Belousov 					continue;
688333d0c60SKonstantin Belousov 				bcopy((char *)fpu_initialstate +
689333d0c60SKonstantin Belousov 				    xsave_area_desc[i].offset,
690333d0c60SKonstantin Belousov 				    sa + xsave_area_desc[i].offset,
691333d0c60SKonstantin Belousov 				    xsave_area_desc[i].size);
692333d0c60SKonstantin Belousov 				*xstate_bv |= bit;
693333d0c60SKonstantin Belousov 			}
694333d0c60SKonstantin Belousov 		}
6956cf9a08dSKonstantin Belousov 		return (_MC_FPOWNED_PCB);
6966cf9a08dSKonstantin Belousov 	}
6976cf9a08dSKonstantin Belousov }
6986cf9a08dSKonstantin Belousov 
6995c6eb037SKonstantin Belousov void
7005c6eb037SKonstantin Belousov fpuuserinited(struct thread *td)
70130abe507SJonathan Mini {
7026cf9a08dSKonstantin Belousov 	struct pcb *pcb;
70330abe507SJonathan Mini 
7046cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
7055c6eb037SKonstantin Belousov 	if (PCB_USER_FPU(pcb))
706e6c006d9SJung-uk Kim 		set_pcb_flags(pcb,
707e6c006d9SJung-uk Kim 		    PCB_FPUINITDONE | PCB_USERFPUINITDONE);
708e6c006d9SJung-uk Kim 	else
709e6c006d9SJung-uk Kim 		set_pcb_flags(pcb, PCB_FPUINITDONE);
71030abe507SJonathan Mini }
71130abe507SJonathan Mini 
7128c6f8f3dSKonstantin Belousov int
7138c6f8f3dSKonstantin Belousov fpusetxstate(struct thread *td, char *xfpustate, size_t xfpustate_size)
7148c6f8f3dSKonstantin Belousov {
7158c6f8f3dSKonstantin Belousov 	struct xstate_hdr *hdr, *ehdr;
7168c6f8f3dSKonstantin Belousov 	size_t len, max_len;
7178c6f8f3dSKonstantin Belousov 	uint64_t bv;
7188c6f8f3dSKonstantin Belousov 
7198c6f8f3dSKonstantin Belousov 	/* XXXKIB should we clear all extended state in xstate_bv instead ? */
7208c6f8f3dSKonstantin Belousov 	if (xfpustate == NULL)
7218c6f8f3dSKonstantin Belousov 		return (0);
7228c6f8f3dSKonstantin Belousov 	if (!use_xsave)
7238c6f8f3dSKonstantin Belousov 		return (EOPNOTSUPP);
7248c6f8f3dSKonstantin Belousov 
7258c6f8f3dSKonstantin Belousov 	len = xfpustate_size;
7268c6f8f3dSKonstantin Belousov 	if (len < sizeof(struct xstate_hdr))
7278c6f8f3dSKonstantin Belousov 		return (EINVAL);
7288c6f8f3dSKonstantin Belousov 	max_len = cpu_max_ext_state_size - sizeof(struct savefpu);
7298c6f8f3dSKonstantin Belousov 	if (len > max_len)
7308c6f8f3dSKonstantin Belousov 		return (EINVAL);
7318c6f8f3dSKonstantin Belousov 
7328c6f8f3dSKonstantin Belousov 	ehdr = (struct xstate_hdr *)xfpustate;
7338c6f8f3dSKonstantin Belousov 	bv = ehdr->xstate_bv;
7348c6f8f3dSKonstantin Belousov 
7358c6f8f3dSKonstantin Belousov 	/*
7368c6f8f3dSKonstantin Belousov 	 * Avoid #gp.
7378c6f8f3dSKonstantin Belousov 	 */
7388c6f8f3dSKonstantin Belousov 	if (bv & ~xsave_mask)
7398c6f8f3dSKonstantin Belousov 		return (EINVAL);
7408c6f8f3dSKonstantin Belousov 	if ((bv & (XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE)) !=
7418c6f8f3dSKonstantin Belousov 	    (XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE))
7428c6f8f3dSKonstantin Belousov 		return (EINVAL);
7438c6f8f3dSKonstantin Belousov 
7448c6f8f3dSKonstantin Belousov 	hdr = (struct xstate_hdr *)(get_pcb_user_save_td(td) + 1);
7458c6f8f3dSKonstantin Belousov 
7468c6f8f3dSKonstantin Belousov 	hdr->xstate_bv = bv;
7478c6f8f3dSKonstantin Belousov 	bcopy(xfpustate + sizeof(struct xstate_hdr),
7488c6f8f3dSKonstantin Belousov 	    (char *)(hdr + 1), len - sizeof(struct xstate_hdr));
7498c6f8f3dSKonstantin Belousov 
7508c6f8f3dSKonstantin Belousov 	return (0);
7518c6f8f3dSKonstantin Belousov }
7528c6f8f3dSKonstantin Belousov 
75330abe507SJonathan Mini /*
75430abe507SJonathan Mini  * Set the state of the FPU.
75530abe507SJonathan Mini  */
7568c6f8f3dSKonstantin Belousov int
7578c6f8f3dSKonstantin Belousov fpusetregs(struct thread *td, struct savefpu *addr, char *xfpustate,
7588c6f8f3dSKonstantin Belousov     size_t xfpustate_size)
7596cf9a08dSKonstantin Belousov {
7606cf9a08dSKonstantin Belousov 	struct pcb *pcb;
7618c6f8f3dSKonstantin Belousov 	int error;
7626cf9a08dSKonstantin Belousov 
7636cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
76499753495SKonstantin Belousov 	critical_enter();
7656cf9a08dSKonstantin Belousov 	if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) {
7668c6f8f3dSKonstantin Belousov 		error = fpusetxstate(td, xfpustate, xfpustate_size);
7678c6f8f3dSKonstantin Belousov 		if (error != 0) {
7688c6f8f3dSKonstantin Belousov 			critical_exit();
7698c6f8f3dSKonstantin Belousov 			return (error);
7708c6f8f3dSKonstantin Belousov 		}
7718c6f8f3dSKonstantin Belousov 		bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr));
7728c6f8f3dSKonstantin Belousov 		fpurestore(get_pcb_user_save_td(td));
77399753495SKonstantin Belousov 		critical_exit();
774e6c006d9SJung-uk Kim 		set_pcb_flags(pcb, PCB_FPUINITDONE | PCB_USERFPUINITDONE);
7756cf9a08dSKonstantin Belousov 	} else {
77699753495SKonstantin Belousov 		critical_exit();
7778c6f8f3dSKonstantin Belousov 		error = fpusetxstate(td, xfpustate, xfpustate_size);
7788c6f8f3dSKonstantin Belousov 		if (error != 0)
7798c6f8f3dSKonstantin Belousov 			return (error);
7808c6f8f3dSKonstantin Belousov 		bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr));
7815c6eb037SKonstantin Belousov 		fpuuserinited(td);
7826cf9a08dSKonstantin Belousov 	}
7838c6f8f3dSKonstantin Belousov 	return (0);
7846cf9a08dSKonstantin Belousov }
7856cf9a08dSKonstantin Belousov 
7866182fdbdSPeter Wemm /*
7872652af56SColin Percival  * On AuthenticAMD processors, the fxrstor instruction does not restore
7882652af56SColin Percival  * the x87's stored last instruction pointer, last data pointer, and last
7892652af56SColin Percival  * opcode values, except in the rare case in which the exception summary
7902652af56SColin Percival  * (ES) bit in the x87 status word is set to 1.
7912652af56SColin Percival  *
7922652af56SColin Percival  * In order to avoid leaking this information across processes, we clean
7932652af56SColin Percival  * these values by performing a dummy load before executing fxrstor().
7942652af56SColin Percival  */
7952652af56SColin Percival static void
7962652af56SColin Percival fpu_clean_state(void)
7972652af56SColin Percival {
798b9dda9d6SJohn Baldwin 	static float dummy_variable = 0.0;
7992652af56SColin Percival 	u_short status;
8002652af56SColin Percival 
8012652af56SColin Percival 	/*
8022652af56SColin Percival 	 * Clear the ES bit in the x87 status word if it is currently
8032652af56SColin Percival 	 * set, in order to avoid causing a fault in the upcoming load.
8042652af56SColin Percival 	 */
8052652af56SColin Percival 	fnstsw(&status);
8062652af56SColin Percival 	if (status & 0x80)
8072652af56SColin Percival 		fnclex();
8082652af56SColin Percival 
8092652af56SColin Percival 	/*
8102652af56SColin Percival 	 * Load the dummy variable into the x87 stack.  This mangles
8112652af56SColin Percival 	 * the x87 stack, but we don't care since we're about to call
8122652af56SColin Percival 	 * fxrstor() anyway.
8132652af56SColin Percival 	 */
81414965052SDimitry Andric 	__asm __volatile("ffree %%st(7); flds %0" : : "m" (dummy_variable));
8152652af56SColin Percival }
8162652af56SColin Percival 
8172652af56SColin Percival /*
818398dbb11SPeter Wemm  * This really sucks.  We want the acpi version only, but it requires
819398dbb11SPeter Wemm  * the isa_if.h file in order to get the definitions.
8206182fdbdSPeter Wemm  */
821398dbb11SPeter Wemm #include "opt_isa.h"
822afa88623SPeter Wemm #ifdef DEV_ISA
823398dbb11SPeter Wemm #include <isa/isavar.h>
82454f1d0ceSGarrett Wollman /*
8255f063c7bSMike Smith  * This sucks up the legacy ISA support assignments from PNPBIOS/ACPI.
82654f1d0ceSGarrett Wollman  */
827398dbb11SPeter Wemm static struct isa_pnp_id fpupnp_ids[] = {
82854f1d0ceSGarrett Wollman 	{ 0x040cd041, "Legacy ISA coprocessor support" }, /* PNP0C04 */
82954f1d0ceSGarrett Wollman 	{ 0 }
83054f1d0ceSGarrett Wollman };
83154f1d0ceSGarrett Wollman 
83254f1d0ceSGarrett Wollman static int
833398dbb11SPeter Wemm fpupnp_probe(device_t dev)
83454f1d0ceSGarrett Wollman {
835bb9c06c1SMike Smith 	int result;
836bf2f09eeSPeter Wemm 
837398dbb11SPeter Wemm 	result = ISA_PNP_PROBE(device_get_parent(dev), dev, fpupnp_ids);
838bf2f09eeSPeter Wemm 	if (result <= 0)
839bb9c06c1SMike Smith 		device_quiet(dev);
840bb9c06c1SMike Smith 	return (result);
84154f1d0ceSGarrett Wollman }
84254f1d0ceSGarrett Wollman 
84354f1d0ceSGarrett Wollman static int
844398dbb11SPeter Wemm fpupnp_attach(device_t dev)
84554f1d0ceSGarrett Wollman {
846bf2f09eeSPeter Wemm 
84754f1d0ceSGarrett Wollman 	return (0);
84854f1d0ceSGarrett Wollman }
84954f1d0ceSGarrett Wollman 
850398dbb11SPeter Wemm static device_method_t fpupnp_methods[] = {
85154f1d0ceSGarrett Wollman 	/* Device interface */
852398dbb11SPeter Wemm 	DEVMETHOD(device_probe,		fpupnp_probe),
853398dbb11SPeter Wemm 	DEVMETHOD(device_attach,	fpupnp_attach),
85454f1d0ceSGarrett Wollman 	DEVMETHOD(device_detach,	bus_generic_detach),
85554f1d0ceSGarrett Wollman 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
85654f1d0ceSGarrett Wollman 	DEVMETHOD(device_suspend,	bus_generic_suspend),
85754f1d0ceSGarrett Wollman 	DEVMETHOD(device_resume,	bus_generic_resume),
85854f1d0ceSGarrett Wollman 
85954f1d0ceSGarrett Wollman 	{ 0, 0 }
86054f1d0ceSGarrett Wollman };
86154f1d0ceSGarrett Wollman 
862398dbb11SPeter Wemm static driver_t fpupnp_driver = {
863398dbb11SPeter Wemm 	"fpupnp",
864398dbb11SPeter Wemm 	fpupnp_methods,
86554f1d0ceSGarrett Wollman 	1,			/* no softc */
86654f1d0ceSGarrett Wollman };
86754f1d0ceSGarrett Wollman 
868398dbb11SPeter Wemm static devclass_t fpupnp_devclass;
86954f1d0ceSGarrett Wollman 
870398dbb11SPeter Wemm DRIVER_MODULE(fpupnp, acpi, fpupnp_driver, fpupnp_devclass, 0, 0);
871586079ccSBruce Evans #endif	/* DEV_ISA */
8726cf9a08dSKonstantin Belousov 
8738c6f8f3dSKonstantin Belousov static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx",
8748c6f8f3dSKonstantin Belousov     "Kernel contexts for FPU state");
8758c6f8f3dSKonstantin Belousov 
8768c6f8f3dSKonstantin Belousov #define	FPU_KERN_CTX_FPUINITDONE 0x01
8778c6f8f3dSKonstantin Belousov 
8788c6f8f3dSKonstantin Belousov struct fpu_kern_ctx {
8798c6f8f3dSKonstantin Belousov 	struct savefpu *prev;
8808c6f8f3dSKonstantin Belousov 	uint32_t flags;
8818c6f8f3dSKonstantin Belousov 	char hwstate1[];
8828c6f8f3dSKonstantin Belousov };
8838c6f8f3dSKonstantin Belousov 
8848c6f8f3dSKonstantin Belousov struct fpu_kern_ctx *
8858c6f8f3dSKonstantin Belousov fpu_kern_alloc_ctx(u_int flags)
8868c6f8f3dSKonstantin Belousov {
8878c6f8f3dSKonstantin Belousov 	struct fpu_kern_ctx *res;
8888c6f8f3dSKonstantin Belousov 	size_t sz;
8898c6f8f3dSKonstantin Belousov 
8908c6f8f3dSKonstantin Belousov 	sz = sizeof(struct fpu_kern_ctx) + XSAVE_AREA_ALIGN +
8918c6f8f3dSKonstantin Belousov 	    cpu_max_ext_state_size;
8928c6f8f3dSKonstantin Belousov 	res = malloc(sz, M_FPUKERN_CTX, ((flags & FPU_KERN_NOWAIT) ?
8938c6f8f3dSKonstantin Belousov 	    M_NOWAIT : M_WAITOK) | M_ZERO);
8948c6f8f3dSKonstantin Belousov 	return (res);
8958c6f8f3dSKonstantin Belousov }
8968c6f8f3dSKonstantin Belousov 
8978c6f8f3dSKonstantin Belousov void
8988c6f8f3dSKonstantin Belousov fpu_kern_free_ctx(struct fpu_kern_ctx *ctx)
8998c6f8f3dSKonstantin Belousov {
9008c6f8f3dSKonstantin Belousov 
9018c6f8f3dSKonstantin Belousov 	/* XXXKIB clear the memory ? */
9028c6f8f3dSKonstantin Belousov 	free(ctx, M_FPUKERN_CTX);
9038c6f8f3dSKonstantin Belousov }
9048c6f8f3dSKonstantin Belousov 
9058c6f8f3dSKonstantin Belousov static struct savefpu *
9068c6f8f3dSKonstantin Belousov fpu_kern_ctx_savefpu(struct fpu_kern_ctx *ctx)
9078c6f8f3dSKonstantin Belousov {
9088c6f8f3dSKonstantin Belousov 	vm_offset_t p;
9098c6f8f3dSKonstantin Belousov 
9108c6f8f3dSKonstantin Belousov 	p = (vm_offset_t)&ctx->hwstate1;
9118c6f8f3dSKonstantin Belousov 	p = roundup2(p, XSAVE_AREA_ALIGN);
9128c6f8f3dSKonstantin Belousov 	return ((struct savefpu *)p);
9138c6f8f3dSKonstantin Belousov }
9148c6f8f3dSKonstantin Belousov 
9156cf9a08dSKonstantin Belousov int
9166cf9a08dSKonstantin Belousov fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags)
9176cf9a08dSKonstantin Belousov {
9186cf9a08dSKonstantin Belousov 	struct pcb *pcb;
9196cf9a08dSKonstantin Belousov 
9206cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
9218c6f8f3dSKonstantin Belousov 	KASSERT(!PCB_USER_FPU(pcb) || pcb->pcb_save ==
9228c6f8f3dSKonstantin Belousov 	    get_pcb_user_save_pcb(pcb), ("mangled pcb_save"));
9236cf9a08dSKonstantin Belousov 	ctx->flags = 0;
9246cf9a08dSKonstantin Belousov 	if ((pcb->pcb_flags & PCB_FPUINITDONE) != 0)
9256cf9a08dSKonstantin Belousov 		ctx->flags |= FPU_KERN_CTX_FPUINITDONE;
9266cf9a08dSKonstantin Belousov 	fpuexit(td);
9276cf9a08dSKonstantin Belousov 	ctx->prev = pcb->pcb_save;
9288c6f8f3dSKonstantin Belousov 	pcb->pcb_save = fpu_kern_ctx_savefpu(ctx);
929e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_KERNFPU);
930e6c006d9SJung-uk Kim 	clear_pcb_flags(pcb, PCB_FPUINITDONE);
9316cf9a08dSKonstantin Belousov 	return (0);
9326cf9a08dSKonstantin Belousov }
9336cf9a08dSKonstantin Belousov 
9346cf9a08dSKonstantin Belousov int
9356cf9a08dSKonstantin Belousov fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx)
9366cf9a08dSKonstantin Belousov {
9376cf9a08dSKonstantin Belousov 	struct pcb *pcb;
9386cf9a08dSKonstantin Belousov 
9396cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
94099753495SKonstantin Belousov 	critical_enter();
9416cf9a08dSKonstantin Belousov 	if (curthread == PCPU_GET(fpcurthread))
9426cf9a08dSKonstantin Belousov 		fpudrop();
94399753495SKonstantin Belousov 	critical_exit();
9446cf9a08dSKonstantin Belousov 	pcb->pcb_save = ctx->prev;
9458c6f8f3dSKonstantin Belousov 	if (pcb->pcb_save == get_pcb_user_save_pcb(pcb)) {
946e6c006d9SJung-uk Kim 		if ((pcb->pcb_flags & PCB_USERFPUINITDONE) != 0) {
947e6c006d9SJung-uk Kim 			set_pcb_flags(pcb, PCB_FPUINITDONE);
948e6c006d9SJung-uk Kim 			clear_pcb_flags(pcb, PCB_KERNFPU);
949e6c006d9SJung-uk Kim 		} else
950e6c006d9SJung-uk Kim 			clear_pcb_flags(pcb, PCB_FPUINITDONE | PCB_KERNFPU);
9516cf9a08dSKonstantin Belousov 	} else {
9526cf9a08dSKonstantin Belousov 		if ((ctx->flags & FPU_KERN_CTX_FPUINITDONE) != 0)
953e6c006d9SJung-uk Kim 			set_pcb_flags(pcb, PCB_FPUINITDONE);
9546cf9a08dSKonstantin Belousov 		else
955e6c006d9SJung-uk Kim 			clear_pcb_flags(pcb, PCB_FPUINITDONE);
9566cf9a08dSKonstantin Belousov 		KASSERT(!PCB_USER_FPU(pcb), ("unpaired fpu_kern_leave"));
9576cf9a08dSKonstantin Belousov 	}
9586cf9a08dSKonstantin Belousov 	return (0);
9596cf9a08dSKonstantin Belousov }
9606cf9a08dSKonstantin Belousov 
9616cf9a08dSKonstantin Belousov int
9626cf9a08dSKonstantin Belousov fpu_kern_thread(u_int flags)
9636cf9a08dSKonstantin Belousov {
9646cf9a08dSKonstantin Belousov 
9656cf9a08dSKonstantin Belousov 	KASSERT((curthread->td_pflags & TDP_KTHREAD) != 0,
9666cf9a08dSKonstantin Belousov 	    ("Only kthread may use fpu_kern_thread"));
967*1965c139SKonstantin Belousov 	KASSERT(curpcb->pcb_save == get_pcb_user_save_pcb(curpcb),
9688c6f8f3dSKonstantin Belousov 	    ("mangled pcb_save"));
969*1965c139SKonstantin Belousov 	KASSERT(PCB_USER_FPU(curpcb), ("recursive call"));
9706cf9a08dSKonstantin Belousov 
971*1965c139SKonstantin Belousov 	set_pcb_flags(curpcb, PCB_KERNFPU);
9726cf9a08dSKonstantin Belousov 	return (0);
9736cf9a08dSKonstantin Belousov }
9746cf9a08dSKonstantin Belousov 
9756cf9a08dSKonstantin Belousov int
9766cf9a08dSKonstantin Belousov is_fpu_kern_thread(u_int flags)
9776cf9a08dSKonstantin Belousov {
9786cf9a08dSKonstantin Belousov 
9796cf9a08dSKonstantin Belousov 	if ((curthread->td_pflags & TDP_KTHREAD) == 0)
9806cf9a08dSKonstantin Belousov 		return (0);
98183b22b05SKonstantin Belousov 	return ((curpcb->pcb_flags & PCB_KERNFPU) != 0);
9826cf9a08dSKonstantin Belousov }
983