xref: /freebsd/sys/amd64/amd64/fpu.c (revision f0188618f2abe08246731cf09b0b0a99609fd34c)
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>
502741efecSPeter Grehan #include <vm/uma.h>
512f86936aSGarrett Wollman 
527f47cf2fSBruce Evans #include <machine/cputypes.h>
537f47cf2fSBruce Evans #include <machine/frame.h>
540d2a2989SPeter Wemm #include <machine/intr_machdep.h>
55c673fe98SBruce Evans #include <machine/md_var.h>
565400ed3bSPeter Wemm #include <machine/pcb.h>
577f47cf2fSBruce Evans #include <machine/psl.h>
586182fdbdSPeter Wemm #include <machine/resource.h>
59f540b106SGarrett Wollman #include <machine/specialreg.h>
607f47cf2fSBruce Evans #include <machine/segments.h>
6130abe507SJonathan Mini #include <machine/ucontext.h>
622f86936aSGarrett Wollman 
635b81b6b3SRodney W. Grimes /*
64bf2f09eeSPeter Wemm  * Floating point support.
655b81b6b3SRodney W. Grimes  */
665b81b6b3SRodney W. Grimes 
67a5f50ef9SJoerg Wunsch #if defined(__GNUCLIKE_ASM) && !defined(lint)
685b81b6b3SRodney W. Grimes 
6917275403SJung-uk Kim #define	fldcw(cw)		__asm __volatile("fldcw %0" : : "m" (cw))
7030402401SJung-uk Kim #define	fnclex()		__asm __volatile("fnclex")
7130402401SJung-uk Kim #define	fninit()		__asm __volatile("fninit")
721d37f051SBruce Evans #define	fnstcw(addr)		__asm __volatile("fnstcw %0" : "=m" (*(addr)))
732e50fa36SJung-uk Kim #define	fnstsw(addr)		__asm __volatile("fnstsw %0" : "=am" (*(addr)))
7430402401SJung-uk Kim #define	fxrstor(addr)		__asm __volatile("fxrstor %0" : : "m" (*(addr)))
759d146ac5SPeter Wemm #define	fxsave(addr)		__asm __volatile("fxsave %0" : "=m" (*(addr)))
7607c86dcfSJung-uk Kim #define	ldmxcsr(csr)		__asm __volatile("ldmxcsr %0" : : "m" (csr))
77a81f9fedSKonstantin Belousov #define	stmxcsr(addr)		__asm __volatile("stmxcsr %0" : : "m" (*(addr)))
785b81b6b3SRodney W. Grimes 
7994818d19SKonstantin Belousov static __inline void
8094818d19SKonstantin Belousov xrstor(char *addr, uint64_t mask)
8194818d19SKonstantin Belousov {
8294818d19SKonstantin Belousov 	uint32_t low, hi;
8394818d19SKonstantin Belousov 
8494818d19SKonstantin Belousov 	low = mask;
8594818d19SKonstantin Belousov 	hi = mask >> 32;
867574a595SJohn Baldwin 	__asm __volatile("xrstor %0" : : "m" (*addr), "a" (low), "d" (hi));
8794818d19SKonstantin Belousov }
8894818d19SKonstantin Belousov 
8994818d19SKonstantin Belousov static __inline void
9094818d19SKonstantin Belousov xsave(char *addr, uint64_t mask)
9194818d19SKonstantin Belousov {
9294818d19SKonstantin Belousov 	uint32_t low, hi;
9394818d19SKonstantin Belousov 
9494818d19SKonstantin Belousov 	low = mask;
9594818d19SKonstantin Belousov 	hi = mask >> 32;
967574a595SJohn Baldwin 	__asm __volatile("xsave %0" : "=m" (*addr) : "a" (low), "d" (hi) :
977574a595SJohn Baldwin 	    "memory");
9894818d19SKonstantin Belousov }
9994818d19SKonstantin Belousov 
100cf4e1c46SPeter Wemm #else	/* !(__GNUCLIKE_ASM && !lint) */
1015b81b6b3SRodney W. Grimes 
10217275403SJung-uk Kim void	fldcw(u_short cw);
10389c9a483SAlfred Perlstein void	fnclex(void);
10489c9a483SAlfred Perlstein void	fninit(void);
10589c9a483SAlfred Perlstein void	fnstcw(caddr_t addr);
10689c9a483SAlfred Perlstein void	fnstsw(caddr_t addr);
10789c9a483SAlfred Perlstein void	fxsave(caddr_t addr);
10889c9a483SAlfred Perlstein void	fxrstor(caddr_t addr);
10907c86dcfSJung-uk Kim void	ldmxcsr(u_int csr);
110a42fa0afSKonstantin Belousov void	stmxcsr(u_int *csr);
11194818d19SKonstantin Belousov void	xrstor(char *addr, uint64_t mask);
11294818d19SKonstantin Belousov void	xsave(char *addr, uint64_t mask);
1135b81b6b3SRodney W. Grimes 
114cf4e1c46SPeter Wemm #endif	/* __GNUCLIKE_ASM && !lint */
1155b81b6b3SRodney W. Grimes 
116d706ec29SJohn Baldwin #define	start_emulating()	load_cr0(rcr0() | CR0_TS)
117d706ec29SJohn Baldwin #define	stop_emulating()	clts()
118d706ec29SJohn Baldwin 
1198c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct savefpu) == 512);
1208c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct xstate_hdr) == 64);
1218c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct savefpu_ymm) == 832);
1228c6f8f3dSKonstantin Belousov 
1238c6f8f3dSKonstantin Belousov /*
1248c6f8f3dSKonstantin Belousov  * This requirement is to make it easier for asm code to calculate
1258c6f8f3dSKonstantin Belousov  * offset of the fpu save area from the pcb address. FPU save area
126b74a2290SKonstantin Belousov  * must be 64-byte aligned.
1278c6f8f3dSKonstantin Belousov  */
1288c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct pcb) % XSAVE_AREA_ALIGN == 0);
1295b81b6b3SRodney W. Grimes 
1302652af56SColin Percival static	void	fpu_clean_state(void);
1312652af56SColin Percival 
1320b7dc0a7SJohn Baldwin SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
133*f0188618SHans Petter Selasky     SYSCTL_NULL_INT_PTR, 1, "Floating point instructions executed in hardware");
1343a34a5c3SPoul-Henning Kamp 
1358c6f8f3dSKonstantin Belousov int use_xsave;			/* non-static for cpu_switch.S */
1368c6f8f3dSKonstantin Belousov uint64_t xsave_mask;		/* the same */
1372741efecSPeter Grehan static	uma_zone_t fpu_save_area_zone;
1388c6f8f3dSKonstantin Belousov static	struct savefpu *fpu_initialstate;
1398c6f8f3dSKonstantin Belousov 
140333d0c60SKonstantin Belousov struct xsave_area_elm_descr {
141333d0c60SKonstantin Belousov 	u_int	offset;
142333d0c60SKonstantin Belousov 	u_int	size;
143333d0c60SKonstantin Belousov } *xsave_area_desc;
144333d0c60SKonstantin Belousov 
1458c6f8f3dSKonstantin Belousov void
1468c6f8f3dSKonstantin Belousov fpusave(void *addr)
1478c6f8f3dSKonstantin Belousov {
1488c6f8f3dSKonstantin Belousov 
1498c6f8f3dSKonstantin Belousov 	if (use_xsave)
1508c6f8f3dSKonstantin Belousov 		xsave((char *)addr, xsave_mask);
1518c6f8f3dSKonstantin Belousov 	else
1528c6f8f3dSKonstantin Belousov 		fxsave((char *)addr);
1538c6f8f3dSKonstantin Belousov }
1548c6f8f3dSKonstantin Belousov 
1552741efecSPeter Grehan void
1568c6f8f3dSKonstantin Belousov fpurestore(void *addr)
1578c6f8f3dSKonstantin Belousov {
1588c6f8f3dSKonstantin Belousov 
1598c6f8f3dSKonstantin Belousov 	if (use_xsave)
1608c6f8f3dSKonstantin Belousov 		xrstor((char *)addr, xsave_mask);
1618c6f8f3dSKonstantin Belousov 	else
1628c6f8f3dSKonstantin Belousov 		fxrstor((char *)addr);
1638c6f8f3dSKonstantin Belousov }
1643902c3efSSteve Passe 
1651d22d877SJung-uk Kim void
1661d22d877SJung-uk Kim fpususpend(void *addr)
1671d22d877SJung-uk Kim {
1681d22d877SJung-uk Kim 	u_long cr0;
1691d22d877SJung-uk Kim 
1701d22d877SJung-uk Kim 	cr0 = rcr0();
1711d22d877SJung-uk Kim 	stop_emulating();
1721d22d877SJung-uk Kim 	fpusave(addr);
1731d22d877SJung-uk Kim 	load_cr0(cr0);
1741d22d877SJung-uk Kim }
1751d22d877SJung-uk Kim 
176b1d735baSJohn Baldwin void
177b1d735baSJohn Baldwin fpuresume(void *addr)
178b1d735baSJohn Baldwin {
179b1d735baSJohn Baldwin 	u_long cr0;
180b1d735baSJohn Baldwin 
181b1d735baSJohn Baldwin 	cr0 = rcr0();
182b1d735baSJohn Baldwin 	stop_emulating();
183b1d735baSJohn Baldwin 	fninit();
184b1d735baSJohn Baldwin 	if (use_xsave)
185b1d735baSJohn Baldwin 		load_xcr(XCR0, xsave_mask);
186b1d735baSJohn Baldwin 	fpurestore(addr);
187b1d735baSJohn Baldwin 	load_cr0(cr0);
188b1d735baSJohn Baldwin }
189b1d735baSJohn Baldwin 
1905b81b6b3SRodney W. Grimes /*
1918c6f8f3dSKonstantin Belousov  * Enable XSAVE if supported and allowed by user.
1928c6f8f3dSKonstantin Belousov  * Calculate the xsave_mask.
1938c6f8f3dSKonstantin Belousov  */
1948c6f8f3dSKonstantin Belousov static void
1958c6f8f3dSKonstantin Belousov fpuinit_bsp1(void)
1968c6f8f3dSKonstantin Belousov {
1978c6f8f3dSKonstantin Belousov 	u_int cp[4];
1988c6f8f3dSKonstantin Belousov 	uint64_t xsave_mask_user;
1998c6f8f3dSKonstantin Belousov 
2008c6f8f3dSKonstantin Belousov 	if ((cpu_feature2 & CPUID2_XSAVE) != 0) {
2018c6f8f3dSKonstantin Belousov 		use_xsave = 1;
2028c6f8f3dSKonstantin Belousov 		TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave);
2038c6f8f3dSKonstantin Belousov 	}
2048c6f8f3dSKonstantin Belousov 	if (!use_xsave)
2058c6f8f3dSKonstantin Belousov 		return;
2068c6f8f3dSKonstantin Belousov 
2078c6f8f3dSKonstantin Belousov 	cpuid_count(0xd, 0x0, cp);
2088c6f8f3dSKonstantin Belousov 	xsave_mask = XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
2098c6f8f3dSKonstantin Belousov 	if ((cp[0] & xsave_mask) != xsave_mask)
2108c6f8f3dSKonstantin Belousov 		panic("CPU0 does not support X87 or SSE: %x", cp[0]);
2118c6f8f3dSKonstantin Belousov 	xsave_mask = ((uint64_t)cp[3] << 32) | cp[0];
2128c6f8f3dSKonstantin Belousov 	xsave_mask_user = xsave_mask;
2138c6f8f3dSKonstantin Belousov 	TUNABLE_ULONG_FETCH("hw.xsave_mask", &xsave_mask_user);
2148c6f8f3dSKonstantin Belousov 	xsave_mask_user |= XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
2158c6f8f3dSKonstantin Belousov 	xsave_mask &= xsave_mask_user;
2160eb7ae8dSJohn Baldwin 	if ((xsave_mask & XFEATURE_AVX512) != XFEATURE_AVX512)
2170eb7ae8dSJohn Baldwin 		xsave_mask &= ~XFEATURE_AVX512;
2180eb7ae8dSJohn Baldwin 	if ((xsave_mask & XFEATURE_MPX) != XFEATURE_MPX)
2190eb7ae8dSJohn Baldwin 		xsave_mask &= ~XFEATURE_MPX;
220333d0c60SKonstantin Belousov 
221333d0c60SKonstantin Belousov 	cpuid_count(0xd, 0x1, cp);
222333d0c60SKonstantin Belousov 	if ((cp[0] & CPUID_EXTSTATE_XSAVEOPT) != 0) {
223333d0c60SKonstantin Belousov 		/*
224333d0c60SKonstantin Belousov 		 * Patch the XSAVE instruction in the cpu_switch code
225333d0c60SKonstantin Belousov 		 * to XSAVEOPT.  We assume that XSAVE encoding used
226333d0c60SKonstantin Belousov 		 * REX byte, and set the bit 4 of the r/m byte.
227333d0c60SKonstantin Belousov 		 */
228333d0c60SKonstantin Belousov 		ctx_switch_xsave[3] |= 0x10;
229333d0c60SKonstantin Belousov 	}
2308c6f8f3dSKonstantin Belousov }
2318c6f8f3dSKonstantin Belousov 
2328c6f8f3dSKonstantin Belousov /*
2338c6f8f3dSKonstantin Belousov  * Calculate the fpu save area size.
2348c6f8f3dSKonstantin Belousov  */
2358c6f8f3dSKonstantin Belousov static void
2368c6f8f3dSKonstantin Belousov fpuinit_bsp2(void)
2378c6f8f3dSKonstantin Belousov {
2388c6f8f3dSKonstantin Belousov 	u_int cp[4];
2398c6f8f3dSKonstantin Belousov 
2408c6f8f3dSKonstantin Belousov 	if (use_xsave) {
2418c6f8f3dSKonstantin Belousov 		cpuid_count(0xd, 0x0, cp);
2428c6f8f3dSKonstantin Belousov 		cpu_max_ext_state_size = cp[1];
2438c6f8f3dSKonstantin Belousov 
2448c6f8f3dSKonstantin Belousov 		/*
2458c6f8f3dSKonstantin Belousov 		 * Reload the cpu_feature2, since we enabled OSXSAVE.
2468c6f8f3dSKonstantin Belousov 		 */
2478c6f8f3dSKonstantin Belousov 		do_cpuid(1, cp);
2488c6f8f3dSKonstantin Belousov 		cpu_feature2 = cp[2];
2498c6f8f3dSKonstantin Belousov 	} else
2508c6f8f3dSKonstantin Belousov 		cpu_max_ext_state_size = sizeof(struct savefpu);
2518c6f8f3dSKonstantin Belousov }
2528c6f8f3dSKonstantin Belousov 
2538c6f8f3dSKonstantin Belousov /*
2548c6f8f3dSKonstantin Belousov  * Initialize the floating point unit.
255da4113b3SPeter Wemm  */
256398dbb11SPeter Wemm void
2571c89210cSPeter Wemm fpuinit(void)
258da4113b3SPeter Wemm {
2590689bdccSJohn Baldwin 	register_t saveintr;
26096a7759eSPeter Wemm 	u_int mxcsr;
261398dbb11SPeter Wemm 	u_short control;
262da4113b3SPeter Wemm 
2638c6f8f3dSKonstantin Belousov 	if (IS_BSP())
2648c6f8f3dSKonstantin Belousov 		fpuinit_bsp1();
2658c6f8f3dSKonstantin Belousov 
2668c6f8f3dSKonstantin Belousov 	if (use_xsave) {
2678c6f8f3dSKonstantin Belousov 		load_cr4(rcr4() | CR4_XSAVE);
2687574a595SJohn Baldwin 		load_xcr(XCR0, xsave_mask);
2698c6f8f3dSKonstantin Belousov 	}
2708c6f8f3dSKonstantin Belousov 
2718c6f8f3dSKonstantin Belousov 	/*
2728c6f8f3dSKonstantin Belousov 	 * XCR0 shall be set up before CPU can report the save area size.
2738c6f8f3dSKonstantin Belousov 	 */
2748c6f8f3dSKonstantin Belousov 	if (IS_BSP())
2758c6f8f3dSKonstantin Belousov 		fpuinit_bsp2();
2768c6f8f3dSKonstantin Belousov 
27799753495SKonstantin Belousov 	/*
27899753495SKonstantin Belousov 	 * It is too early for critical_enter() to work on AP.
27999753495SKonstantin Belousov 	 */
2800689bdccSJohn Baldwin 	saveintr = intr_disable();
2815b81b6b3SRodney W. Grimes 	stop_emulating();
2825b81b6b3SRodney W. Grimes 	fninit();
283398dbb11SPeter Wemm 	control = __INITIAL_FPUCW__;
28417275403SJung-uk Kim 	fldcw(control);
28596a7759eSPeter Wemm 	mxcsr = __INITIAL_MXCSR__;
28696a7759eSPeter Wemm 	ldmxcsr(mxcsr);
287a8346a98SJohn Baldwin 	start_emulating();
2880689bdccSJohn Baldwin 	intr_restore(saveintr);
2895b81b6b3SRodney W. Grimes }
2905b81b6b3SRodney W. Grimes 
2915b81b6b3SRodney W. Grimes /*
2928c6f8f3dSKonstantin Belousov  * On the boot CPU we generate a clean state that is used to
2938c6f8f3dSKonstantin Belousov  * initialize the floating point unit when it is first used by a
2948c6f8f3dSKonstantin Belousov  * process.
2958c6f8f3dSKonstantin Belousov  */
2968c6f8f3dSKonstantin Belousov static void
2978c6f8f3dSKonstantin Belousov fpuinitstate(void *arg __unused)
2988c6f8f3dSKonstantin Belousov {
2998c6f8f3dSKonstantin Belousov 	register_t saveintr;
300333d0c60SKonstantin Belousov 	int cp[4], i, max_ext_n;
3018c6f8f3dSKonstantin Belousov 
3028c6f8f3dSKonstantin Belousov 	fpu_initialstate = malloc(cpu_max_ext_state_size, M_DEVBUF,
3038c6f8f3dSKonstantin Belousov 	    M_WAITOK | M_ZERO);
3048c6f8f3dSKonstantin Belousov 	saveintr = intr_disable();
3058c6f8f3dSKonstantin Belousov 	stop_emulating();
3068c6f8f3dSKonstantin Belousov 
3078c6f8f3dSKonstantin Belousov 	fpusave(fpu_initialstate);
3088c6f8f3dSKonstantin Belousov 	if (fpu_initialstate->sv_env.en_mxcsr_mask)
3098c6f8f3dSKonstantin Belousov 		cpu_mxcsr_mask = fpu_initialstate->sv_env.en_mxcsr_mask;
3108c6f8f3dSKonstantin Belousov 	else
3118c6f8f3dSKonstantin Belousov 		cpu_mxcsr_mask = 0xFFBF;
3128c6f8f3dSKonstantin Belousov 
3138c6f8f3dSKonstantin Belousov 	/*
3148c6f8f3dSKonstantin Belousov 	 * The fninit instruction does not modify XMM registers.  The
3158c6f8f3dSKonstantin Belousov 	 * fpusave call dumped the garbage contained in the registers
3168c6f8f3dSKonstantin Belousov 	 * after reset to the initial state saved.  Clear XMM
3178c6f8f3dSKonstantin Belousov 	 * registers file image to make the startup program state and
3188c6f8f3dSKonstantin Belousov 	 * signal handler XMM register content predictable.
3198c6f8f3dSKonstantin Belousov 	 */
3208c6f8f3dSKonstantin Belousov 	bzero(&fpu_initialstate->sv_xmm[0], sizeof(struct xmmacc));
3218c6f8f3dSKonstantin Belousov 
322333d0c60SKonstantin Belousov 	/*
323333d0c60SKonstantin Belousov 	 * Create a table describing the layout of the CPU Extended
324333d0c60SKonstantin Belousov 	 * Save Area.
325333d0c60SKonstantin Belousov 	 */
32614f52559SKonstantin Belousov 	if (use_xsave) {
327333d0c60SKonstantin Belousov 		max_ext_n = flsl(xsave_mask);
328333d0c60SKonstantin Belousov 		xsave_area_desc = malloc(max_ext_n * sizeof(struct
329333d0c60SKonstantin Belousov 		    xsave_area_elm_descr), M_DEVBUF, M_WAITOK | M_ZERO);
330333d0c60SKonstantin Belousov 		/* x87 state */
331333d0c60SKonstantin Belousov 		xsave_area_desc[0].offset = 0;
332333d0c60SKonstantin Belousov 		xsave_area_desc[0].size = 160;
333333d0c60SKonstantin Belousov 		/* XMM */
334333d0c60SKonstantin Belousov 		xsave_area_desc[1].offset = 160;
335333d0c60SKonstantin Belousov 		xsave_area_desc[1].size = 288 - 160;
336333d0c60SKonstantin Belousov 
337333d0c60SKonstantin Belousov 		for (i = 2; i < max_ext_n; i++) {
338333d0c60SKonstantin Belousov 			cpuid_count(0xd, i, cp);
339333d0c60SKonstantin Belousov 			xsave_area_desc[i].offset = cp[1];
340333d0c60SKonstantin Belousov 			xsave_area_desc[i].size = cp[0];
341333d0c60SKonstantin Belousov 		}
342333d0c60SKonstantin Belousov 	}
343333d0c60SKonstantin Belousov 
3442741efecSPeter Grehan 	fpu_save_area_zone = uma_zcreate("FPU_save_area",
3452741efecSPeter Grehan 	    cpu_max_ext_state_size, NULL, NULL, NULL, NULL,
3462741efecSPeter Grehan 	    XSAVE_AREA_ALIGN - 1, 0);
3472741efecSPeter Grehan 
3488c6f8f3dSKonstantin Belousov 	start_emulating();
3498c6f8f3dSKonstantin Belousov 	intr_restore(saveintr);
3508c6f8f3dSKonstantin Belousov }
3518c6f8f3dSKonstantin Belousov SYSINIT(fpuinitstate, SI_SUB_DRIVERS, SI_ORDER_ANY, fpuinitstate, NULL);
3528c6f8f3dSKonstantin Belousov 
3538c6f8f3dSKonstantin Belousov /*
3545b81b6b3SRodney W. Grimes  * Free coprocessor (if we have it).
3555b81b6b3SRodney W. Grimes  */
3565b81b6b3SRodney W. Grimes void
357bf2f09eeSPeter Wemm fpuexit(struct thread *td)
3585b81b6b3SRodney W. Grimes {
3595b81b6b3SRodney W. Grimes 
36099753495SKonstantin Belousov 	critical_enter();
3611c89210cSPeter Wemm 	if (curthread == PCPU_GET(fpcurthread)) {
3621c89210cSPeter Wemm 		stop_emulating();
36383b22b05SKonstantin Belousov 		fpusave(curpcb->pcb_save);
3641c89210cSPeter Wemm 		start_emulating();
3656dfc9e44SKonstantin Belousov 		PCPU_SET(fpcurthread, NULL);
3661c89210cSPeter Wemm 	}
36799753495SKonstantin Belousov 	critical_exit();
3685b81b6b3SRodney W. Grimes }
3695b81b6b3SRodney W. Grimes 
37030abe507SJonathan Mini int
371bf2f09eeSPeter Wemm fpuformat()
37230abe507SJonathan Mini {
37330abe507SJonathan Mini 
37430abe507SJonathan Mini 	return (_MC_FPFMT_XMM);
37530abe507SJonathan Mini }
37630abe507SJonathan Mini 
3775b81b6b3SRodney W. Grimes /*
378a7674320SMartin Cracauer  * The following mechanism is used to ensure that the FPE_... value
379a7674320SMartin Cracauer  * that is passed as a trapcode to the signal handler of the user
380a7674320SMartin Cracauer  * process does not have more than one bit set.
381a7674320SMartin Cracauer  *
382a7674320SMartin Cracauer  * Multiple bits may be set if the user process modifies the control
383a7674320SMartin Cracauer  * word while a status word bit is already set.  While this is a sign
384a7674320SMartin Cracauer  * of bad coding, we have no choise than to narrow them down to one
385a7674320SMartin Cracauer  * bit, since we must not send a trapcode that is not exactly one of
386a7674320SMartin Cracauer  * the FPE_ macros.
387a7674320SMartin Cracauer  *
388a7674320SMartin Cracauer  * The mechanism has a static table with 127 entries.  Each combination
389a7674320SMartin Cracauer  * of the 7 FPU status word exception bits directly translates to a
390a7674320SMartin Cracauer  * position in this table, where a single FPE_... value is stored.
391a7674320SMartin Cracauer  * This FPE_... value stored there is considered the "most important"
392a7674320SMartin Cracauer  * of the exception bits and will be sent as the signal code.  The
393a7674320SMartin Cracauer  * precedence of the bits is based upon Intel Document "Numerical
394a7674320SMartin Cracauer  * Applications", Chapter "Special Computational Situations".
395a7674320SMartin Cracauer  *
396a7674320SMartin Cracauer  * The macro to choose one of these values does these steps: 1) Throw
397a7674320SMartin Cracauer  * away status word bits that cannot be masked.  2) Throw away the bits
398a7674320SMartin Cracauer  * currently masked in the control word, assuming the user isn't
399a7674320SMartin Cracauer  * interested in them anymore.  3) Reinsert status word bit 7 (stack
400a7674320SMartin Cracauer  * fault) if it is set, which cannot be masked but must be presered.
401a7674320SMartin Cracauer  * 4) Use the remaining bits to point into the trapcode table.
402a7674320SMartin Cracauer  *
403a7674320SMartin Cracauer  * The 6 maskable bits in order of their preference, as stated in the
404a7674320SMartin Cracauer  * above referenced Intel manual:
405a7674320SMartin Cracauer  * 1  Invalid operation (FP_X_INV)
406a7674320SMartin Cracauer  * 1a   Stack underflow
407a7674320SMartin Cracauer  * 1b   Stack overflow
408a7674320SMartin Cracauer  * 1c   Operand of unsupported format
409a7674320SMartin Cracauer  * 1d   SNaN operand.
410a7674320SMartin Cracauer  * 2  QNaN operand (not an exception, irrelavant here)
411a7674320SMartin Cracauer  * 3  Any other invalid-operation not mentioned above or zero divide
412a7674320SMartin Cracauer  *      (FP_X_INV, FP_X_DZ)
413a7674320SMartin Cracauer  * 4  Denormal operand (FP_X_DNML)
414a7674320SMartin Cracauer  * 5  Numeric over/underflow (FP_X_OFL, FP_X_UFL)
415784648c6SMartin Cracauer  * 6  Inexact result (FP_X_IMP)
416784648c6SMartin Cracauer  */
417a7674320SMartin Cracauer static char fpetable[128] = {
418a7674320SMartin Cracauer 	0,
419a7674320SMartin Cracauer 	FPE_FLTINV,	/*  1 - INV */
420a7674320SMartin Cracauer 	FPE_FLTUND,	/*  2 - DNML */
421a7674320SMartin Cracauer 	FPE_FLTINV,	/*  3 - INV | DNML */
422a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  4 - DZ */
423a7674320SMartin Cracauer 	FPE_FLTINV,	/*  5 - INV | DZ */
424a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  6 - DNML | DZ */
425a7674320SMartin Cracauer 	FPE_FLTINV,	/*  7 - INV | DNML | DZ */
426a7674320SMartin Cracauer 	FPE_FLTOVF,	/*  8 - OFL */
427a7674320SMartin Cracauer 	FPE_FLTINV,	/*  9 - INV | OFL */
428a7674320SMartin Cracauer 	FPE_FLTUND,	/*  A - DNML | OFL */
429a7674320SMartin Cracauer 	FPE_FLTINV,	/*  B - INV | DNML | OFL */
430a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  C - DZ | OFL */
431a7674320SMartin Cracauer 	FPE_FLTINV,	/*  D - INV | DZ | OFL */
432a7674320SMartin Cracauer 	FPE_FLTDIV,	/*  E - DNML | DZ | OFL */
433a7674320SMartin Cracauer 	FPE_FLTINV,	/*  F - INV | DNML | DZ | OFL */
434a7674320SMartin Cracauer 	FPE_FLTUND,	/* 10 - UFL */
435a7674320SMartin Cracauer 	FPE_FLTINV,	/* 11 - INV | UFL */
436a7674320SMartin Cracauer 	FPE_FLTUND,	/* 12 - DNML | UFL */
437a7674320SMartin Cracauer 	FPE_FLTINV,	/* 13 - INV | DNML | UFL */
438a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 14 - DZ | UFL */
439a7674320SMartin Cracauer 	FPE_FLTINV,	/* 15 - INV | DZ | UFL */
440a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 16 - DNML | DZ | UFL */
441a7674320SMartin Cracauer 	FPE_FLTINV,	/* 17 - INV | DNML | DZ | UFL */
442a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 18 - OFL | UFL */
443a7674320SMartin Cracauer 	FPE_FLTINV,	/* 19 - INV | OFL | UFL */
444a7674320SMartin Cracauer 	FPE_FLTUND,	/* 1A - DNML | OFL | UFL */
445a7674320SMartin Cracauer 	FPE_FLTINV,	/* 1B - INV | DNML | OFL | UFL */
446a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 1C - DZ | OFL | UFL */
447a7674320SMartin Cracauer 	FPE_FLTINV,	/* 1D - INV | DZ | OFL | UFL */
448a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 1E - DNML | DZ | OFL | UFL */
449a7674320SMartin Cracauer 	FPE_FLTINV,	/* 1F - INV | DNML | DZ | OFL | UFL */
450a7674320SMartin Cracauer 	FPE_FLTRES,	/* 20 - IMP */
451a7674320SMartin Cracauer 	FPE_FLTINV,	/* 21 - INV | IMP */
452a7674320SMartin Cracauer 	FPE_FLTUND,	/* 22 - DNML | IMP */
453a7674320SMartin Cracauer 	FPE_FLTINV,	/* 23 - INV | DNML | IMP */
454a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 24 - DZ | IMP */
455a7674320SMartin Cracauer 	FPE_FLTINV,	/* 25 - INV | DZ | IMP */
456a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 26 - DNML | DZ | IMP */
457a7674320SMartin Cracauer 	FPE_FLTINV,	/* 27 - INV | DNML | DZ | IMP */
458a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 28 - OFL | IMP */
459a7674320SMartin Cracauer 	FPE_FLTINV,	/* 29 - INV | OFL | IMP */
460a7674320SMartin Cracauer 	FPE_FLTUND,	/* 2A - DNML | OFL | IMP */
461a7674320SMartin Cracauer 	FPE_FLTINV,	/* 2B - INV | DNML | OFL | IMP */
462a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 2C - DZ | OFL | IMP */
463a7674320SMartin Cracauer 	FPE_FLTINV,	/* 2D - INV | DZ | OFL | IMP */
464a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 2E - DNML | DZ | OFL | IMP */
465a7674320SMartin Cracauer 	FPE_FLTINV,	/* 2F - INV | DNML | DZ | OFL | IMP */
466a7674320SMartin Cracauer 	FPE_FLTUND,	/* 30 - UFL | IMP */
467a7674320SMartin Cracauer 	FPE_FLTINV,	/* 31 - INV | UFL | IMP */
468a7674320SMartin Cracauer 	FPE_FLTUND,	/* 32 - DNML | UFL | IMP */
469a7674320SMartin Cracauer 	FPE_FLTINV,	/* 33 - INV | DNML | UFL | IMP */
470a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 34 - DZ | UFL | IMP */
471a7674320SMartin Cracauer 	FPE_FLTINV,	/* 35 - INV | DZ | UFL | IMP */
472a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 36 - DNML | DZ | UFL | IMP */
473a7674320SMartin Cracauer 	FPE_FLTINV,	/* 37 - INV | DNML | DZ | UFL | IMP */
474a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 38 - OFL | UFL | IMP */
475a7674320SMartin Cracauer 	FPE_FLTINV,	/* 39 - INV | OFL | UFL | IMP */
476a7674320SMartin Cracauer 	FPE_FLTUND,	/* 3A - DNML | OFL | UFL | IMP */
477a7674320SMartin Cracauer 	FPE_FLTINV,	/* 3B - INV | DNML | OFL | UFL | IMP */
478a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 3C - DZ | OFL | UFL | IMP */
479a7674320SMartin Cracauer 	FPE_FLTINV,	/* 3D - INV | DZ | OFL | UFL | IMP */
480a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 3E - DNML | DZ | OFL | UFL | IMP */
481a7674320SMartin Cracauer 	FPE_FLTINV,	/* 3F - INV | DNML | DZ | OFL | UFL | IMP */
482a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 40 - STK */
483a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 41 - INV | STK */
484a7674320SMartin Cracauer 	FPE_FLTUND,	/* 42 - DNML | STK */
485a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 43 - INV | DNML | STK */
486a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 44 - DZ | STK */
487a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 45 - INV | DZ | STK */
488a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 46 - DNML | DZ | STK */
489a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 47 - INV | DNML | DZ | STK */
490a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 48 - OFL | STK */
491a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 49 - INV | OFL | STK */
492a7674320SMartin Cracauer 	FPE_FLTUND,	/* 4A - DNML | OFL | STK */
493a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 4B - INV | DNML | OFL | STK */
494a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 4C - DZ | OFL | STK */
495a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 4D - INV | DZ | OFL | STK */
496a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 4E - DNML | DZ | OFL | STK */
497a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 4F - INV | DNML | DZ | OFL | STK */
498a7674320SMartin Cracauer 	FPE_FLTUND,	/* 50 - UFL | STK */
499a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 51 - INV | UFL | STK */
500a7674320SMartin Cracauer 	FPE_FLTUND,	/* 52 - DNML | UFL | STK */
501a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 53 - INV | DNML | UFL | STK */
502a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 54 - DZ | UFL | STK */
503a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 55 - INV | DZ | UFL | STK */
504a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 56 - DNML | DZ | UFL | STK */
505a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 57 - INV | DNML | DZ | UFL | STK */
506a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 58 - OFL | UFL | STK */
507a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 59 - INV | OFL | UFL | STK */
508a7674320SMartin Cracauer 	FPE_FLTUND,	/* 5A - DNML | OFL | UFL | STK */
509a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 5B - INV | DNML | OFL | UFL | STK */
510a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 5C - DZ | OFL | UFL | STK */
511a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 5D - INV | DZ | OFL | UFL | STK */
512a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 5E - DNML | DZ | OFL | UFL | STK */
513a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 5F - INV | DNML | DZ | OFL | UFL | STK */
514a7674320SMartin Cracauer 	FPE_FLTRES,	/* 60 - IMP | STK */
515a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 61 - INV | IMP | STK */
516a7674320SMartin Cracauer 	FPE_FLTUND,	/* 62 - DNML | IMP | STK */
517a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 63 - INV | DNML | IMP | STK */
518a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 64 - DZ | IMP | STK */
519a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 65 - INV | DZ | IMP | STK */
520a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 66 - DNML | DZ | IMP | STK */
521a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 67 - INV | DNML | DZ | IMP | STK */
522a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 68 - OFL | IMP | STK */
523a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 69 - INV | OFL | IMP | STK */
524a7674320SMartin Cracauer 	FPE_FLTUND,	/* 6A - DNML | OFL | IMP | STK */
525a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 6B - INV | DNML | OFL | IMP | STK */
526a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 6C - DZ | OFL | IMP | STK */
527a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 6D - INV | DZ | OFL | IMP | STK */
528a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 6E - DNML | DZ | OFL | IMP | STK */
529a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 6F - INV | DNML | DZ | OFL | IMP | STK */
530a7674320SMartin Cracauer 	FPE_FLTUND,	/* 70 - UFL | IMP | STK */
531a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 71 - INV | UFL | IMP | STK */
532a7674320SMartin Cracauer 	FPE_FLTUND,	/* 72 - DNML | UFL | IMP | STK */
533a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 73 - INV | DNML | UFL | IMP | STK */
534a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 74 - DZ | UFL | IMP | STK */
535a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 75 - INV | DZ | UFL | IMP | STK */
536a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 76 - DNML | DZ | UFL | IMP | STK */
537a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 77 - INV | DNML | DZ | UFL | IMP | STK */
538a7674320SMartin Cracauer 	FPE_FLTOVF,	/* 78 - OFL | UFL | IMP | STK */
539a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 79 - INV | OFL | UFL | IMP | STK */
540a7674320SMartin Cracauer 	FPE_FLTUND,	/* 7A - DNML | OFL | UFL | IMP | STK */
541a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 7B - INV | DNML | OFL | UFL | IMP | STK */
542a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 7C - DZ | OFL | UFL | IMP | STK */
543a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 7D - INV | DZ | OFL | UFL | IMP | STK */
544a7674320SMartin Cracauer 	FPE_FLTDIV,	/* 7E - DNML | DZ | OFL | UFL | IMP | STK */
545a7674320SMartin Cracauer 	FPE_FLTSUB,	/* 7F - INV | DNML | DZ | OFL | UFL | IMP | STK */
546a7674320SMartin Cracauer };
547a7674320SMartin Cracauer 
548a7674320SMartin Cracauer /*
549dfa8a512SKonstantin Belousov  * Read the FP status and control words, then generate si_code value
550dfa8a512SKonstantin Belousov  * for SIGFPE.  The error code chosen will be one of the
551dfa8a512SKonstantin Belousov  * FPE_... macros.  It will be sent as the second argument to old
552dfa8a512SKonstantin Belousov  * BSD-style signal handlers and as "siginfo_t->si_code" (second
553dfa8a512SKonstantin Belousov  * argument) to SA_SIGINFO signal handlers.
5545b81b6b3SRodney W. Grimes  *
555dfa8a512SKonstantin Belousov  * Some time ago, we cleared the x87 exceptions with FNCLEX there.
556dfa8a512SKonstantin Belousov  * Clearing exceptions was necessary mainly to avoid IRQ13 bugs.  The
557dfa8a512SKonstantin Belousov  * usermode code which understands the FPU hardware enough to enable
558dfa8a512SKonstantin Belousov  * the exceptions, can also handle clearing the exception state in the
559dfa8a512SKonstantin Belousov  * handler.  The only consequence of not clearing the exception is the
560dfa8a512SKonstantin Belousov  * rethrow of the SIGFPE on return from the signal handler and
561dfa8a512SKonstantin Belousov  * reexecution of the corresponding instruction.
562bc84db62SKonstantin Belousov  *
563dfa8a512SKonstantin Belousov  * For XMM traps, the exceptions were never cleared.
5645b81b6b3SRodney W. Grimes  */
5651c1771cbSBruce Evans int
566bc84db62SKonstantin Belousov fputrap_x87(void)
5675b81b6b3SRodney W. Grimes {
568bc84db62SKonstantin Belousov 	struct savefpu *pcb_save;
5691c1771cbSBruce Evans 	u_short control, status;
5705b81b6b3SRodney W. Grimes 
57199753495SKonstantin Belousov 	critical_enter();
5725b81b6b3SRodney W. Grimes 
5735b81b6b3SRodney W. Grimes 	/*
5741c1771cbSBruce Evans 	 * Interrupt handling (for another interrupt) may have pushed the
5751c1771cbSBruce Evans 	 * state to memory.  Fetch the relevant parts of the state from
5761c1771cbSBruce Evans 	 * wherever they are.
5775b81b6b3SRodney W. Grimes 	 */
5780bbc8826SJohn Baldwin 	if (PCPU_GET(fpcurthread) != curthread) {
57983b22b05SKonstantin Belousov 		pcb_save = curpcb->pcb_save;
580bc84db62SKonstantin Belousov 		control = pcb_save->sv_env.en_cw;
581bc84db62SKonstantin Belousov 		status = pcb_save->sv_env.en_sw;
5825b81b6b3SRodney W. Grimes 	} else {
5831c1771cbSBruce Evans 		fnstcw(&control);
5841c1771cbSBruce Evans 		fnstsw(&status);
5855b81b6b3SRodney W. Grimes 	}
5861c1771cbSBruce Evans 
58799753495SKonstantin Belousov 	critical_exit();
5881c1771cbSBruce Evans 	return (fpetable[status & ((~control & 0x3f) | 0x40)]);
5895b81b6b3SRodney W. Grimes }
5905b81b6b3SRodney W. Grimes 
591bc84db62SKonstantin Belousov int
592bc84db62SKonstantin Belousov fputrap_sse(void)
593bc84db62SKonstantin Belousov {
594bc84db62SKonstantin Belousov 	u_int mxcsr;
595bc84db62SKonstantin Belousov 
596bc84db62SKonstantin Belousov 	critical_enter();
597bc84db62SKonstantin Belousov 	if (PCPU_GET(fpcurthread) != curthread)
59883b22b05SKonstantin Belousov 		mxcsr = curpcb->pcb_save->sv_env.en_mxcsr;
599bc84db62SKonstantin Belousov 	else
600bc84db62SKonstantin Belousov 		stmxcsr(&mxcsr);
601bc84db62SKonstantin Belousov 	critical_exit();
602bc84db62SKonstantin Belousov 	return (fpetable[(mxcsr & (~mxcsr >> 7)) & 0x3f]);
603bc84db62SKonstantin Belousov }
604bc84db62SKonstantin Belousov 
6056dfc9e44SKonstantin Belousov /*
6066dfc9e44SKonstantin Belousov  * Device Not Available (DNA, #NM) exception handler.
6076dfc9e44SKonstantin Belousov  *
6086dfc9e44SKonstantin Belousov  * It would be better to switch FP context here (if curthread !=
6096dfc9e44SKonstantin Belousov  * fpcurthread) and not necessarily for every context switch, but it
6106dfc9e44SKonstantin Belousov  * is too hard to access foreign pcb's.
6116dfc9e44SKonstantin Belousov  */
612a8346a98SJohn Baldwin void
613a8346a98SJohn Baldwin fpudna(void)
6145b81b6b3SRodney W. Grimes {
61505f6ee66SJake Burkholder 
616060cd4d5SKonstantin Belousov 	/*
617060cd4d5SKonstantin Belousov 	 * This handler is entered with interrupts enabled, so context
618060cd4d5SKonstantin Belousov 	 * switches may occur before critical_enter() is executed.  If
619060cd4d5SKonstantin Belousov 	 * a context switch occurs, then when we regain control, our
620060cd4d5SKonstantin Belousov 	 * state will have been completely restored.  The CPU may
621060cd4d5SKonstantin Belousov 	 * change underneath us, but the only part of our context that
622060cd4d5SKonstantin Belousov 	 * lives in the CPU is CR0.TS and that will be "restored" by
623060cd4d5SKonstantin Belousov 	 * setting it on the new CPU.
624060cd4d5SKonstantin Belousov 	 */
62599753495SKonstantin Belousov 	critical_enter();
626060cd4d5SKonstantin Belousov 
62730abe507SJonathan Mini 	if (PCPU_GET(fpcurthread) == curthread) {
628060cd4d5SKonstantin Belousov 		printf("fpudna: fpcurthread == curthread\n");
62930abe507SJonathan Mini 		stop_emulating();
63099753495SKonstantin Belousov 		critical_exit();
631a8346a98SJohn Baldwin 		return;
63230abe507SJonathan Mini 	}
6330bbc8826SJohn Baldwin 	if (PCPU_GET(fpcurthread) != NULL) {
6346dfc9e44SKonstantin Belousov 		panic("fpudna: fpcurthread = %p (%d), curthread = %p (%d)\n",
6356dfc9e44SKonstantin Belousov 		    PCPU_GET(fpcurthread), PCPU_GET(fpcurthread)->td_tid,
6366dfc9e44SKonstantin Belousov 		    curthread, curthread->td_tid);
6375b81b6b3SRodney W. Grimes 	}
6385b81b6b3SRodney W. Grimes 	stop_emulating();
6395b81b6b3SRodney W. Grimes 	/*
640bf2f09eeSPeter Wemm 	 * Record new context early in case frstor causes a trap.
6415b81b6b3SRodney W. Grimes 	 */
6420bbc8826SJohn Baldwin 	PCPU_SET(fpcurthread, curthread);
6439d146ac5SPeter Wemm 
6442652af56SColin Percival 	fpu_clean_state();
6452652af56SColin Percival 
6461965c139SKonstantin Belousov 	if ((curpcb->pcb_flags & PCB_FPUINITDONE) == 0) {
6475b81b6b3SRodney W. Grimes 		/*
64863de9515SJohn Baldwin 		 * This is the first time this thread has used the FPU or
64963de9515SJohn Baldwin 		 * the PCB doesn't contain a clean FPU state.  Explicitly
65063de9515SJohn Baldwin 		 * load an initial state.
651333d0c60SKonstantin Belousov 		 *
652333d0c60SKonstantin Belousov 		 * We prefer to restore the state from the actual save
653333d0c60SKonstantin Belousov 		 * area in PCB instead of directly loading from
654333d0c60SKonstantin Belousov 		 * fpu_initialstate, to ignite the XSAVEOPT
655333d0c60SKonstantin Belousov 		 * tracking engine.
6565b81b6b3SRodney W. Grimes 		 */
6571965c139SKonstantin Belousov 		bcopy(fpu_initialstate, curpcb->pcb_save, cpu_max_ext_state_size);
6581965c139SKonstantin Belousov 		fpurestore(curpcb->pcb_save);
6591965c139SKonstantin Belousov 		if (curpcb->pcb_initial_fpucw != __INITIAL_FPUCW__)
6601965c139SKonstantin Belousov 			fldcw(curpcb->pcb_initial_fpucw);
6611965c139SKonstantin Belousov 		if (PCB_USER_FPU(curpcb))
6621965c139SKonstantin Belousov 			set_pcb_flags(curpcb,
663e6c006d9SJung-uk Kim 			    PCB_FPUINITDONE | PCB_USERFPUINITDONE);
664e6c006d9SJung-uk Kim 		else
6651965c139SKonstantin Belousov 			set_pcb_flags(curpcb, PCB_FPUINITDONE);
6661c89210cSPeter Wemm 	} else
6671965c139SKonstantin Belousov 		fpurestore(curpcb->pcb_save);
66899753495SKonstantin Belousov 	critical_exit();
6695b81b6b3SRodney W. Grimes }
6705b81b6b3SRodney W. Grimes 
67130abe507SJonathan Mini void
672bf2f09eeSPeter Wemm fpudrop()
67330abe507SJonathan Mini {
67430abe507SJonathan Mini 	struct thread *td;
67530abe507SJonathan Mini 
67630abe507SJonathan Mini 	td = PCPU_GET(fpcurthread);
67799753495SKonstantin Belousov 	KASSERT(td == curthread, ("fpudrop: fpcurthread != curthread"));
6784a23ecc7SKonstantin Belousov 	CRITICAL_ASSERT(td);
67930abe507SJonathan Mini 	PCPU_SET(fpcurthread, NULL);
680e6c006d9SJung-uk Kim 	clear_pcb_flags(td->td_pcb, PCB_FPUINITDONE);
68130abe507SJonathan Mini 	start_emulating();
68230abe507SJonathan Mini }
68330abe507SJonathan Mini 
68430abe507SJonathan Mini /*
6855c6eb037SKonstantin Belousov  * Get the user state of the FPU into pcb->pcb_user_save without
6865c6eb037SKonstantin Belousov  * dropping ownership (if possible).  It returns the FPU ownership
6875c6eb037SKonstantin Belousov  * status.
68830abe507SJonathan Mini  */
68930abe507SJonathan Mini int
6905c6eb037SKonstantin Belousov fpugetregs(struct thread *td)
6916cf9a08dSKonstantin Belousov {
6926cf9a08dSKonstantin Belousov 	struct pcb *pcb;
693333d0c60SKonstantin Belousov 	uint64_t *xstate_bv, bit;
694333d0c60SKonstantin Belousov 	char *sa;
69514f52559SKonstantin Belousov 	int max_ext_n, i, owned;
6966cf9a08dSKonstantin Belousov 
6976cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
6986cf9a08dSKonstantin Belousov 	if ((pcb->pcb_flags & PCB_USERFPUINITDONE) == 0) {
6998c6f8f3dSKonstantin Belousov 		bcopy(fpu_initialstate, get_pcb_user_save_pcb(pcb),
7008c6f8f3dSKonstantin Belousov 		    cpu_max_ext_state_size);
7018c6f8f3dSKonstantin Belousov 		get_pcb_user_save_pcb(pcb)->sv_env.en_cw =
7028c6f8f3dSKonstantin Belousov 		    pcb->pcb_initial_fpucw;
7035c6eb037SKonstantin Belousov 		fpuuserinited(td);
7045c6eb037SKonstantin Belousov 		return (_MC_FPOWNED_PCB);
7056cf9a08dSKonstantin Belousov 	}
70699753495SKonstantin Belousov 	critical_enter();
7076cf9a08dSKonstantin Belousov 	if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) {
7088c6f8f3dSKonstantin Belousov 		fpusave(get_pcb_user_save_pcb(pcb));
70914f52559SKonstantin Belousov 		owned = _MC_FPOWNED_FPU;
7106cf9a08dSKonstantin Belousov 	} else {
71114f52559SKonstantin Belousov 		owned = _MC_FPOWNED_PCB;
71214f52559SKonstantin Belousov 	}
71399753495SKonstantin Belousov 	critical_exit();
71414f52559SKonstantin Belousov 	if (use_xsave) {
715333d0c60SKonstantin Belousov 		/*
716333d0c60SKonstantin Belousov 		 * Handle partially saved state.
717333d0c60SKonstantin Belousov 		 */
718333d0c60SKonstantin Belousov 		sa = (char *)get_pcb_user_save_pcb(pcb);
719333d0c60SKonstantin Belousov 		xstate_bv = (uint64_t *)(sa + sizeof(struct savefpu) +
720333d0c60SKonstantin Belousov 		    offsetof(struct xstate_hdr, xstate_bv));
721333d0c60SKonstantin Belousov 		max_ext_n = flsl(xsave_mask);
722333d0c60SKonstantin Belousov 		for (i = 0; i < max_ext_n; i++) {
723241b67bbSKonstantin Belousov 			bit = 1ULL << i;
724241b67bbSKonstantin Belousov 			if ((xsave_mask & bit) == 0 || (*xstate_bv & bit) != 0)
725333d0c60SKonstantin Belousov 				continue;
726333d0c60SKonstantin Belousov 			bcopy((char *)fpu_initialstate +
727333d0c60SKonstantin Belousov 			    xsave_area_desc[i].offset,
728333d0c60SKonstantin Belousov 			    sa + xsave_area_desc[i].offset,
729333d0c60SKonstantin Belousov 			    xsave_area_desc[i].size);
730333d0c60SKonstantin Belousov 			*xstate_bv |= bit;
731333d0c60SKonstantin Belousov 		}
732333d0c60SKonstantin Belousov 	}
73314f52559SKonstantin Belousov 	return (owned);
7346cf9a08dSKonstantin Belousov }
7356cf9a08dSKonstantin Belousov 
7365c6eb037SKonstantin Belousov void
7375c6eb037SKonstantin Belousov fpuuserinited(struct thread *td)
73830abe507SJonathan Mini {
7396cf9a08dSKonstantin Belousov 	struct pcb *pcb;
74030abe507SJonathan Mini 
7416cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
7425c6eb037SKonstantin Belousov 	if (PCB_USER_FPU(pcb))
743e6c006d9SJung-uk Kim 		set_pcb_flags(pcb,
744e6c006d9SJung-uk Kim 		    PCB_FPUINITDONE | PCB_USERFPUINITDONE);
745e6c006d9SJung-uk Kim 	else
746e6c006d9SJung-uk Kim 		set_pcb_flags(pcb, PCB_FPUINITDONE);
74730abe507SJonathan Mini }
74830abe507SJonathan Mini 
7498c6f8f3dSKonstantin Belousov int
7508c6f8f3dSKonstantin Belousov fpusetxstate(struct thread *td, char *xfpustate, size_t xfpustate_size)
7518c6f8f3dSKonstantin Belousov {
7528c6f8f3dSKonstantin Belousov 	struct xstate_hdr *hdr, *ehdr;
7538c6f8f3dSKonstantin Belousov 	size_t len, max_len;
7548c6f8f3dSKonstantin Belousov 	uint64_t bv;
7558c6f8f3dSKonstantin Belousov 
7568c6f8f3dSKonstantin Belousov 	/* XXXKIB should we clear all extended state in xstate_bv instead ? */
7578c6f8f3dSKonstantin Belousov 	if (xfpustate == NULL)
7588c6f8f3dSKonstantin Belousov 		return (0);
7598c6f8f3dSKonstantin Belousov 	if (!use_xsave)
7608c6f8f3dSKonstantin Belousov 		return (EOPNOTSUPP);
7618c6f8f3dSKonstantin Belousov 
7628c6f8f3dSKonstantin Belousov 	len = xfpustate_size;
7638c6f8f3dSKonstantin Belousov 	if (len < sizeof(struct xstate_hdr))
7648c6f8f3dSKonstantin Belousov 		return (EINVAL);
7658c6f8f3dSKonstantin Belousov 	max_len = cpu_max_ext_state_size - sizeof(struct savefpu);
7668c6f8f3dSKonstantin Belousov 	if (len > max_len)
7678c6f8f3dSKonstantin Belousov 		return (EINVAL);
7688c6f8f3dSKonstantin Belousov 
7698c6f8f3dSKonstantin Belousov 	ehdr = (struct xstate_hdr *)xfpustate;
7708c6f8f3dSKonstantin Belousov 	bv = ehdr->xstate_bv;
7718c6f8f3dSKonstantin Belousov 
7728c6f8f3dSKonstantin Belousov 	/*
7738c6f8f3dSKonstantin Belousov 	 * Avoid #gp.
7748c6f8f3dSKonstantin Belousov 	 */
7758c6f8f3dSKonstantin Belousov 	if (bv & ~xsave_mask)
7768c6f8f3dSKonstantin Belousov 		return (EINVAL);
7778c6f8f3dSKonstantin Belousov 
7788c6f8f3dSKonstantin Belousov 	hdr = (struct xstate_hdr *)(get_pcb_user_save_td(td) + 1);
7798c6f8f3dSKonstantin Belousov 
7808c6f8f3dSKonstantin Belousov 	hdr->xstate_bv = bv;
7818c6f8f3dSKonstantin Belousov 	bcopy(xfpustate + sizeof(struct xstate_hdr),
7828c6f8f3dSKonstantin Belousov 	    (char *)(hdr + 1), len - sizeof(struct xstate_hdr));
7838c6f8f3dSKonstantin Belousov 
7848c6f8f3dSKonstantin Belousov 	return (0);
7858c6f8f3dSKonstantin Belousov }
7868c6f8f3dSKonstantin Belousov 
78730abe507SJonathan Mini /*
78830abe507SJonathan Mini  * Set the state of the FPU.
78930abe507SJonathan Mini  */
7908c6f8f3dSKonstantin Belousov int
7918c6f8f3dSKonstantin Belousov fpusetregs(struct thread *td, struct savefpu *addr, char *xfpustate,
7928c6f8f3dSKonstantin Belousov     size_t xfpustate_size)
7936cf9a08dSKonstantin Belousov {
7946cf9a08dSKonstantin Belousov 	struct pcb *pcb;
7958c6f8f3dSKonstantin Belousov 	int error;
7966cf9a08dSKonstantin Belousov 
7976cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
79899753495SKonstantin Belousov 	critical_enter();
7996cf9a08dSKonstantin Belousov 	if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) {
8008c6f8f3dSKonstantin Belousov 		error = fpusetxstate(td, xfpustate, xfpustate_size);
8018c6f8f3dSKonstantin Belousov 		if (error != 0) {
8028c6f8f3dSKonstantin Belousov 			critical_exit();
8038c6f8f3dSKonstantin Belousov 			return (error);
8048c6f8f3dSKonstantin Belousov 		}
8058c6f8f3dSKonstantin Belousov 		bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr));
8068c6f8f3dSKonstantin Belousov 		fpurestore(get_pcb_user_save_td(td));
80799753495SKonstantin Belousov 		critical_exit();
808e6c006d9SJung-uk Kim 		set_pcb_flags(pcb, PCB_FPUINITDONE | PCB_USERFPUINITDONE);
8096cf9a08dSKonstantin Belousov 	} else {
81099753495SKonstantin Belousov 		critical_exit();
8118c6f8f3dSKonstantin Belousov 		error = fpusetxstate(td, xfpustate, xfpustate_size);
8128c6f8f3dSKonstantin Belousov 		if (error != 0)
8138c6f8f3dSKonstantin Belousov 			return (error);
8148c6f8f3dSKonstantin Belousov 		bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr));
8155c6eb037SKonstantin Belousov 		fpuuserinited(td);
8166cf9a08dSKonstantin Belousov 	}
8178c6f8f3dSKonstantin Belousov 	return (0);
8186cf9a08dSKonstantin Belousov }
8196cf9a08dSKonstantin Belousov 
8206182fdbdSPeter Wemm /*
8212652af56SColin Percival  * On AuthenticAMD processors, the fxrstor instruction does not restore
8222652af56SColin Percival  * the x87's stored last instruction pointer, last data pointer, and last
8232652af56SColin Percival  * opcode values, except in the rare case in which the exception summary
8242652af56SColin Percival  * (ES) bit in the x87 status word is set to 1.
8252652af56SColin Percival  *
8262652af56SColin Percival  * In order to avoid leaking this information across processes, we clean
8272652af56SColin Percival  * these values by performing a dummy load before executing fxrstor().
8282652af56SColin Percival  */
8292652af56SColin Percival static void
8302652af56SColin Percival fpu_clean_state(void)
8312652af56SColin Percival {
832b9dda9d6SJohn Baldwin 	static float dummy_variable = 0.0;
8332652af56SColin Percival 	u_short status;
8342652af56SColin Percival 
8352652af56SColin Percival 	/*
8362652af56SColin Percival 	 * Clear the ES bit in the x87 status word if it is currently
8372652af56SColin Percival 	 * set, in order to avoid causing a fault in the upcoming load.
8382652af56SColin Percival 	 */
8392652af56SColin Percival 	fnstsw(&status);
8402652af56SColin Percival 	if (status & 0x80)
8412652af56SColin Percival 		fnclex();
8422652af56SColin Percival 
8432652af56SColin Percival 	/*
8442652af56SColin Percival 	 * Load the dummy variable into the x87 stack.  This mangles
8452652af56SColin Percival 	 * the x87 stack, but we don't care since we're about to call
8462652af56SColin Percival 	 * fxrstor() anyway.
8472652af56SColin Percival 	 */
84814965052SDimitry Andric 	__asm __volatile("ffree %%st(7); flds %0" : : "m" (dummy_variable));
8492652af56SColin Percival }
8502652af56SColin Percival 
8512652af56SColin Percival /*
852398dbb11SPeter Wemm  * This really sucks.  We want the acpi version only, but it requires
853398dbb11SPeter Wemm  * the isa_if.h file in order to get the definitions.
8546182fdbdSPeter Wemm  */
855398dbb11SPeter Wemm #include "opt_isa.h"
856afa88623SPeter Wemm #ifdef DEV_ISA
857398dbb11SPeter Wemm #include <isa/isavar.h>
85854f1d0ceSGarrett Wollman /*
8595f063c7bSMike Smith  * This sucks up the legacy ISA support assignments from PNPBIOS/ACPI.
86054f1d0ceSGarrett Wollman  */
861398dbb11SPeter Wemm static struct isa_pnp_id fpupnp_ids[] = {
86254f1d0ceSGarrett Wollman 	{ 0x040cd041, "Legacy ISA coprocessor support" }, /* PNP0C04 */
86354f1d0ceSGarrett Wollman 	{ 0 }
86454f1d0ceSGarrett Wollman };
86554f1d0ceSGarrett Wollman 
86654f1d0ceSGarrett Wollman static int
867398dbb11SPeter Wemm fpupnp_probe(device_t dev)
86854f1d0ceSGarrett Wollman {
869bb9c06c1SMike Smith 	int result;
870bf2f09eeSPeter Wemm 
871398dbb11SPeter Wemm 	result = ISA_PNP_PROBE(device_get_parent(dev), dev, fpupnp_ids);
872bf2f09eeSPeter Wemm 	if (result <= 0)
873bb9c06c1SMike Smith 		device_quiet(dev);
874bb9c06c1SMike Smith 	return (result);
87554f1d0ceSGarrett Wollman }
87654f1d0ceSGarrett Wollman 
87754f1d0ceSGarrett Wollman static int
878398dbb11SPeter Wemm fpupnp_attach(device_t dev)
87954f1d0ceSGarrett Wollman {
880bf2f09eeSPeter Wemm 
88154f1d0ceSGarrett Wollman 	return (0);
88254f1d0ceSGarrett Wollman }
88354f1d0ceSGarrett Wollman 
884398dbb11SPeter Wemm static device_method_t fpupnp_methods[] = {
88554f1d0ceSGarrett Wollman 	/* Device interface */
886398dbb11SPeter Wemm 	DEVMETHOD(device_probe,		fpupnp_probe),
887398dbb11SPeter Wemm 	DEVMETHOD(device_attach,	fpupnp_attach),
88854f1d0ceSGarrett Wollman 	DEVMETHOD(device_detach,	bus_generic_detach),
88954f1d0ceSGarrett Wollman 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
89054f1d0ceSGarrett Wollman 	DEVMETHOD(device_suspend,	bus_generic_suspend),
89154f1d0ceSGarrett Wollman 	DEVMETHOD(device_resume,	bus_generic_resume),
89254f1d0ceSGarrett Wollman 
89354f1d0ceSGarrett Wollman 	{ 0, 0 }
89454f1d0ceSGarrett Wollman };
89554f1d0ceSGarrett Wollman 
896398dbb11SPeter Wemm static driver_t fpupnp_driver = {
897398dbb11SPeter Wemm 	"fpupnp",
898398dbb11SPeter Wemm 	fpupnp_methods,
89954f1d0ceSGarrett Wollman 	1,			/* no softc */
90054f1d0ceSGarrett Wollman };
90154f1d0ceSGarrett Wollman 
902398dbb11SPeter Wemm static devclass_t fpupnp_devclass;
90354f1d0ceSGarrett Wollman 
904398dbb11SPeter Wemm DRIVER_MODULE(fpupnp, acpi, fpupnp_driver, fpupnp_devclass, 0, 0);
905586079ccSBruce Evans #endif	/* DEV_ISA */
9066cf9a08dSKonstantin Belousov 
9078c6f8f3dSKonstantin Belousov static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx",
9088c6f8f3dSKonstantin Belousov     "Kernel contexts for FPU state");
9098c6f8f3dSKonstantin Belousov 
9108c6f8f3dSKonstantin Belousov #define	FPU_KERN_CTX_FPUINITDONE 0x01
911633034feSKonstantin Belousov #define	FPU_KERN_CTX_DUMMY	 0x02	/* avoided save for the kern thread */
9128c6f8f3dSKonstantin Belousov 
9138c6f8f3dSKonstantin Belousov struct fpu_kern_ctx {
9148c6f8f3dSKonstantin Belousov 	struct savefpu *prev;
9158c6f8f3dSKonstantin Belousov 	uint32_t flags;
9168c6f8f3dSKonstantin Belousov 	char hwstate1[];
9178c6f8f3dSKonstantin Belousov };
9188c6f8f3dSKonstantin Belousov 
9198c6f8f3dSKonstantin Belousov struct fpu_kern_ctx *
9208c6f8f3dSKonstantin Belousov fpu_kern_alloc_ctx(u_int flags)
9218c6f8f3dSKonstantin Belousov {
9228c6f8f3dSKonstantin Belousov 	struct fpu_kern_ctx *res;
9238c6f8f3dSKonstantin Belousov 	size_t sz;
9248c6f8f3dSKonstantin Belousov 
9258c6f8f3dSKonstantin Belousov 	sz = sizeof(struct fpu_kern_ctx) + XSAVE_AREA_ALIGN +
9268c6f8f3dSKonstantin Belousov 	    cpu_max_ext_state_size;
9278c6f8f3dSKonstantin Belousov 	res = malloc(sz, M_FPUKERN_CTX, ((flags & FPU_KERN_NOWAIT) ?
9288c6f8f3dSKonstantin Belousov 	    M_NOWAIT : M_WAITOK) | M_ZERO);
9298c6f8f3dSKonstantin Belousov 	return (res);
9308c6f8f3dSKonstantin Belousov }
9318c6f8f3dSKonstantin Belousov 
9328c6f8f3dSKonstantin Belousov void
9338c6f8f3dSKonstantin Belousov fpu_kern_free_ctx(struct fpu_kern_ctx *ctx)
9348c6f8f3dSKonstantin Belousov {
9358c6f8f3dSKonstantin Belousov 
9368c6f8f3dSKonstantin Belousov 	/* XXXKIB clear the memory ? */
9378c6f8f3dSKonstantin Belousov 	free(ctx, M_FPUKERN_CTX);
9388c6f8f3dSKonstantin Belousov }
9398c6f8f3dSKonstantin Belousov 
9408c6f8f3dSKonstantin Belousov static struct savefpu *
9418c6f8f3dSKonstantin Belousov fpu_kern_ctx_savefpu(struct fpu_kern_ctx *ctx)
9428c6f8f3dSKonstantin Belousov {
9438c6f8f3dSKonstantin Belousov 	vm_offset_t p;
9448c6f8f3dSKonstantin Belousov 
9458c6f8f3dSKonstantin Belousov 	p = (vm_offset_t)&ctx->hwstate1;
9468c6f8f3dSKonstantin Belousov 	p = roundup2(p, XSAVE_AREA_ALIGN);
9478c6f8f3dSKonstantin Belousov 	return ((struct savefpu *)p);
9488c6f8f3dSKonstantin Belousov }
9498c6f8f3dSKonstantin Belousov 
9506cf9a08dSKonstantin Belousov int
9516cf9a08dSKonstantin Belousov fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags)
9526cf9a08dSKonstantin Belousov {
9536cf9a08dSKonstantin Belousov 	struct pcb *pcb;
9546cf9a08dSKonstantin Belousov 
955633034feSKonstantin Belousov 	if ((flags & FPU_KERN_KTHR) != 0 && is_fpu_kern_thread(0)) {
956633034feSKonstantin Belousov 		ctx->flags = FPU_KERN_CTX_DUMMY;
957633034feSKonstantin Belousov 		return (0);
958633034feSKonstantin Belousov 	}
9596cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
9608c6f8f3dSKonstantin Belousov 	KASSERT(!PCB_USER_FPU(pcb) || pcb->pcb_save ==
9618c6f8f3dSKonstantin Belousov 	    get_pcb_user_save_pcb(pcb), ("mangled pcb_save"));
9626cf9a08dSKonstantin Belousov 	ctx->flags = 0;
9636cf9a08dSKonstantin Belousov 	if ((pcb->pcb_flags & PCB_FPUINITDONE) != 0)
9646cf9a08dSKonstantin Belousov 		ctx->flags |= FPU_KERN_CTX_FPUINITDONE;
9656cf9a08dSKonstantin Belousov 	fpuexit(td);
9666cf9a08dSKonstantin Belousov 	ctx->prev = pcb->pcb_save;
9678c6f8f3dSKonstantin Belousov 	pcb->pcb_save = fpu_kern_ctx_savefpu(ctx);
968e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_KERNFPU);
969e6c006d9SJung-uk Kim 	clear_pcb_flags(pcb, PCB_FPUINITDONE);
9706cf9a08dSKonstantin Belousov 	return (0);
9716cf9a08dSKonstantin Belousov }
9726cf9a08dSKonstantin Belousov 
9736cf9a08dSKonstantin Belousov int
9746cf9a08dSKonstantin Belousov fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx)
9756cf9a08dSKonstantin Belousov {
9766cf9a08dSKonstantin Belousov 	struct pcb *pcb;
9776cf9a08dSKonstantin Belousov 
978633034feSKonstantin Belousov 	if (is_fpu_kern_thread(0) && (ctx->flags & FPU_KERN_CTX_DUMMY) != 0)
979633034feSKonstantin Belousov 		return (0);
980633034feSKonstantin Belousov 	KASSERT((ctx->flags & FPU_KERN_CTX_DUMMY) == 0, ("dummy ctx"));
9816cf9a08dSKonstantin Belousov 	pcb = td->td_pcb;
98299753495SKonstantin Belousov 	critical_enter();
9836cf9a08dSKonstantin Belousov 	if (curthread == PCPU_GET(fpcurthread))
9846cf9a08dSKonstantin Belousov 		fpudrop();
98599753495SKonstantin Belousov 	critical_exit();
9866cf9a08dSKonstantin Belousov 	pcb->pcb_save = ctx->prev;
9878c6f8f3dSKonstantin Belousov 	if (pcb->pcb_save == get_pcb_user_save_pcb(pcb)) {
988e6c006d9SJung-uk Kim 		if ((pcb->pcb_flags & PCB_USERFPUINITDONE) != 0) {
989e6c006d9SJung-uk Kim 			set_pcb_flags(pcb, PCB_FPUINITDONE);
990e6c006d9SJung-uk Kim 			clear_pcb_flags(pcb, PCB_KERNFPU);
991e6c006d9SJung-uk Kim 		} else
992e6c006d9SJung-uk Kim 			clear_pcb_flags(pcb, PCB_FPUINITDONE | PCB_KERNFPU);
9936cf9a08dSKonstantin Belousov 	} else {
9946cf9a08dSKonstantin Belousov 		if ((ctx->flags & FPU_KERN_CTX_FPUINITDONE) != 0)
995e6c006d9SJung-uk Kim 			set_pcb_flags(pcb, PCB_FPUINITDONE);
9966cf9a08dSKonstantin Belousov 		else
997e6c006d9SJung-uk Kim 			clear_pcb_flags(pcb, PCB_FPUINITDONE);
9986cf9a08dSKonstantin Belousov 		KASSERT(!PCB_USER_FPU(pcb), ("unpaired fpu_kern_leave"));
9996cf9a08dSKonstantin Belousov 	}
10006cf9a08dSKonstantin Belousov 	return (0);
10016cf9a08dSKonstantin Belousov }
10026cf9a08dSKonstantin Belousov 
10036cf9a08dSKonstantin Belousov int
10046cf9a08dSKonstantin Belousov fpu_kern_thread(u_int flags)
10056cf9a08dSKonstantin Belousov {
10066cf9a08dSKonstantin Belousov 
10076cf9a08dSKonstantin Belousov 	KASSERT((curthread->td_pflags & TDP_KTHREAD) != 0,
10086cf9a08dSKonstantin Belousov 	    ("Only kthread may use fpu_kern_thread"));
10091965c139SKonstantin Belousov 	KASSERT(curpcb->pcb_save == get_pcb_user_save_pcb(curpcb),
10108c6f8f3dSKonstantin Belousov 	    ("mangled pcb_save"));
10111965c139SKonstantin Belousov 	KASSERT(PCB_USER_FPU(curpcb), ("recursive call"));
10126cf9a08dSKonstantin Belousov 
10131965c139SKonstantin Belousov 	set_pcb_flags(curpcb, PCB_KERNFPU);
10146cf9a08dSKonstantin Belousov 	return (0);
10156cf9a08dSKonstantin Belousov }
10166cf9a08dSKonstantin Belousov 
10176cf9a08dSKonstantin Belousov int
10186cf9a08dSKonstantin Belousov is_fpu_kern_thread(u_int flags)
10196cf9a08dSKonstantin Belousov {
10206cf9a08dSKonstantin Belousov 
10216cf9a08dSKonstantin Belousov 	if ((curthread->td_pflags & TDP_KTHREAD) == 0)
10226cf9a08dSKonstantin Belousov 		return (0);
102383b22b05SKonstantin Belousov 	return ((curpcb->pcb_flags & PCB_KERNFPU) != 0);
10246cf9a08dSKonstantin Belousov }
10252741efecSPeter Grehan 
10262741efecSPeter Grehan /*
10272741efecSPeter Grehan  * FPU save area alloc/free/init utility routines
10282741efecSPeter Grehan  */
10292741efecSPeter Grehan struct savefpu *
10302741efecSPeter Grehan fpu_save_area_alloc(void)
10312741efecSPeter Grehan {
10322741efecSPeter Grehan 
10332741efecSPeter Grehan 	return (uma_zalloc(fpu_save_area_zone, 0));
10342741efecSPeter Grehan }
10352741efecSPeter Grehan 
10362741efecSPeter Grehan void
10372741efecSPeter Grehan fpu_save_area_free(struct savefpu *fsa)
10382741efecSPeter Grehan {
10392741efecSPeter Grehan 
10402741efecSPeter Grehan 	uma_zfree(fpu_save_area_zone, fsa);
10412741efecSPeter Grehan }
10422741efecSPeter Grehan 
10432741efecSPeter Grehan void
10442741efecSPeter Grehan fpu_save_area_reset(struct savefpu *fsa)
10452741efecSPeter Grehan {
10462741efecSPeter Grehan 
10472741efecSPeter Grehan 	bcopy(fpu_initialstate, fsa, cpu_max_ext_state_size);
10482741efecSPeter Grehan }
1049