15b81b6b3SRodney W. Grimes /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 45b81b6b3SRodney W. Grimes * Copyright (c) 1990 William Jolitz. 55b81b6b3SRodney W. Grimes * Copyright (c) 1991 The Regents of the University of California. 65b81b6b3SRodney W. Grimes * All rights reserved. 75b81b6b3SRodney W. Grimes * 85b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 95b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 105b81b6b3SRodney W. Grimes * are met: 115b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 125b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 135b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 145b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 155b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 175b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 185b81b6b3SRodney W. Grimes * without specific prior written permission. 195b81b6b3SRodney W. Grimes * 205b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 215b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 225b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 235b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 245b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 255b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 265b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 275b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 285b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 295b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 305b81b6b3SRodney W. Grimes * SUCH DAMAGE. 315b81b6b3SRodney W. Grimes * 3221616ec3SPeter Wemm * from: @(#)npx.c 7.2 (Berkeley) 5/12/91 335b81b6b3SRodney W. Grimes */ 345b81b6b3SRodney W. Grimes 3556ae44c5SDavid E. O'Brien #include <sys/cdefs.h> 3656ae44c5SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3756ae44c5SDavid E. O'Brien 38f540b106SGarrett Wollman #include <sys/param.h> 39f540b106SGarrett Wollman #include <sys/systm.h> 406182fdbdSPeter Wemm #include <sys/bus.h> 413a34a5c3SPoul-Henning Kamp #include <sys/kernel.h> 42fb919e4dSMark Murray #include <sys/lock.h> 43cd59d49dSBruce Evans #include <sys/malloc.h> 446182fdbdSPeter Wemm #include <sys/module.h> 45c1ef8aacSJake Burkholder #include <sys/mutex.h> 46fb919e4dSMark Murray #include <sys/mutex.h> 47fb919e4dSMark Murray #include <sys/proc.h> 48fb919e4dSMark Murray #include <sys/sysctl.h> 496182fdbdSPeter Wemm #include <machine/bus.h> 506182fdbdSPeter Wemm #include <sys/rman.h> 51663f1485SBruce Evans #include <sys/signalvar.h> 522741efecSPeter Grehan #include <vm/uma.h> 532f86936aSGarrett Wollman 547f47cf2fSBruce Evans #include <machine/cputypes.h> 557f47cf2fSBruce Evans #include <machine/frame.h> 560d2a2989SPeter Wemm #include <machine/intr_machdep.h> 57c673fe98SBruce Evans #include <machine/md_var.h> 585400ed3bSPeter Wemm #include <machine/pcb.h> 597f47cf2fSBruce Evans #include <machine/psl.h> 606182fdbdSPeter Wemm #include <machine/resource.h> 61f540b106SGarrett Wollman #include <machine/specialreg.h> 627f47cf2fSBruce Evans #include <machine/segments.h> 6330abe507SJonathan Mini #include <machine/ucontext.h> 648b4fc8b1SKonstantin Belousov #include <x86/ifunc.h> 652f86936aSGarrett Wollman 665b81b6b3SRodney W. Grimes /* 67bf2f09eeSPeter Wemm * Floating point support. 685b81b6b3SRodney W. Grimes */ 695b81b6b3SRodney W. Grimes 70a5f50ef9SJoerg Wunsch #if defined(__GNUCLIKE_ASM) && !defined(lint) 715b81b6b3SRodney W. Grimes 7217275403SJung-uk Kim #define fldcw(cw) __asm __volatile("fldcw %0" : : "m" (cw)) 7330402401SJung-uk Kim #define fnclex() __asm __volatile("fnclex") 7430402401SJung-uk Kim #define fninit() __asm __volatile("fninit") 751d37f051SBruce Evans #define fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr))) 762e50fa36SJung-uk Kim #define fnstsw(addr) __asm __volatile("fnstsw %0" : "=am" (*(addr))) 7730402401SJung-uk Kim #define fxrstor(addr) __asm __volatile("fxrstor %0" : : "m" (*(addr))) 789d146ac5SPeter Wemm #define fxsave(addr) __asm __volatile("fxsave %0" : "=m" (*(addr))) 7907c86dcfSJung-uk Kim #define ldmxcsr(csr) __asm __volatile("ldmxcsr %0" : : "m" (csr)) 80a81f9fedSKonstantin Belousov #define stmxcsr(addr) __asm __volatile("stmxcsr %0" : : "m" (*(addr))) 815b81b6b3SRodney W. Grimes 8294818d19SKonstantin Belousov static __inline void 8394818d19SKonstantin Belousov xrstor(char *addr, uint64_t mask) 8494818d19SKonstantin Belousov { 8594818d19SKonstantin Belousov uint32_t low, hi; 8694818d19SKonstantin Belousov 8794818d19SKonstantin Belousov low = mask; 8894818d19SKonstantin Belousov hi = mask >> 32; 897574a595SJohn Baldwin __asm __volatile("xrstor %0" : : "m" (*addr), "a" (low), "d" (hi)); 9094818d19SKonstantin Belousov } 9194818d19SKonstantin Belousov 9294818d19SKonstantin Belousov static __inline void 9394818d19SKonstantin Belousov xsave(char *addr, uint64_t mask) 9494818d19SKonstantin Belousov { 9594818d19SKonstantin Belousov uint32_t low, hi; 9694818d19SKonstantin Belousov 9794818d19SKonstantin Belousov low = mask; 9894818d19SKonstantin Belousov hi = mask >> 32; 997574a595SJohn Baldwin __asm __volatile("xsave %0" : "=m" (*addr) : "a" (low), "d" (hi) : 1007574a595SJohn Baldwin "memory"); 10194818d19SKonstantin Belousov } 10294818d19SKonstantin Belousov 103cf4e1c46SPeter Wemm #else /* !(__GNUCLIKE_ASM && !lint) */ 1045b81b6b3SRodney W. Grimes 10517275403SJung-uk Kim void fldcw(u_short cw); 10689c9a483SAlfred Perlstein void fnclex(void); 10789c9a483SAlfred Perlstein void fninit(void); 10889c9a483SAlfred Perlstein void fnstcw(caddr_t addr); 10989c9a483SAlfred Perlstein void fnstsw(caddr_t addr); 11089c9a483SAlfred Perlstein void fxsave(caddr_t addr); 11189c9a483SAlfred Perlstein void fxrstor(caddr_t addr); 11207c86dcfSJung-uk Kim void ldmxcsr(u_int csr); 113a42fa0afSKonstantin Belousov void stmxcsr(u_int *csr); 11494818d19SKonstantin Belousov void xrstor(char *addr, uint64_t mask); 11594818d19SKonstantin Belousov void xsave(char *addr, uint64_t mask); 1165b81b6b3SRodney W. Grimes 117cf4e1c46SPeter Wemm #endif /* __GNUCLIKE_ASM && !lint */ 1185b81b6b3SRodney W. Grimes 119d706ec29SJohn Baldwin #define start_emulating() load_cr0(rcr0() | CR0_TS) 120d706ec29SJohn Baldwin #define stop_emulating() clts() 121d706ec29SJohn Baldwin 1228c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct savefpu) == 512); 1238c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct xstate_hdr) == 64); 1248c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct savefpu_ymm) == 832); 1258c6f8f3dSKonstantin Belousov 1268c6f8f3dSKonstantin Belousov /* 1278c6f8f3dSKonstantin Belousov * This requirement is to make it easier for asm code to calculate 1288c6f8f3dSKonstantin Belousov * offset of the fpu save area from the pcb address. FPU save area 129b74a2290SKonstantin Belousov * must be 64-byte aligned. 1308c6f8f3dSKonstantin Belousov */ 1318c6f8f3dSKonstantin Belousov CTASSERT(sizeof(struct pcb) % XSAVE_AREA_ALIGN == 0); 1325b81b6b3SRodney W. Grimes 133180e57e5SJohn Baldwin /* 134180e57e5SJohn Baldwin * Ensure the copy of XCR0 saved in a core is contained in the padding 135180e57e5SJohn Baldwin * area. 136180e57e5SJohn Baldwin */ 137180e57e5SJohn Baldwin CTASSERT(X86_XSTATE_XCR0_OFFSET >= offsetof(struct savefpu, sv_pad) && 138180e57e5SJohn Baldwin X86_XSTATE_XCR0_OFFSET + sizeof(uint64_t) <= sizeof(struct savefpu)); 139180e57e5SJohn Baldwin 1402652af56SColin Percival static void fpu_clean_state(void); 1412652af56SColin Percival 1420b7dc0a7SJohn Baldwin SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD, 143f0188618SHans Petter Selasky SYSCTL_NULL_INT_PTR, 1, "Floating point instructions executed in hardware"); 1443a34a5c3SPoul-Henning Kamp 145d1a07e31SKonstantin Belousov int lazy_fpu_switch = 0; 146d1a07e31SKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, lazy_fpu_switch, CTLFLAG_RWTUN | CTLFLAG_NOFETCH, 147d1a07e31SKonstantin Belousov &lazy_fpu_switch, 0, 148d1a07e31SKonstantin Belousov "Lazily load FPU context after context switch"); 149d1a07e31SKonstantin Belousov 1508c6f8f3dSKonstantin Belousov int use_xsave; /* non-static for cpu_switch.S */ 1518c6f8f3dSKonstantin Belousov uint64_t xsave_mask; /* the same */ 1522741efecSPeter Grehan static uma_zone_t fpu_save_area_zone; 1538c6f8f3dSKonstantin Belousov static struct savefpu *fpu_initialstate; 1548c6f8f3dSKonstantin Belousov 155333d0c60SKonstantin Belousov struct xsave_area_elm_descr { 156333d0c60SKonstantin Belousov u_int offset; 157333d0c60SKonstantin Belousov u_int size; 158333d0c60SKonstantin Belousov } *xsave_area_desc; 159333d0c60SKonstantin Belousov 1608b4fc8b1SKonstantin Belousov static void 1618b4fc8b1SKonstantin Belousov fpusave_xsave(void *addr) 1628c6f8f3dSKonstantin Belousov { 1638c6f8f3dSKonstantin Belousov 1648c6f8f3dSKonstantin Belousov xsave((char *)addr, xsave_mask); 1658b4fc8b1SKonstantin Belousov } 1668b4fc8b1SKonstantin Belousov 1678b4fc8b1SKonstantin Belousov static void 1688b4fc8b1SKonstantin Belousov fpurestore_xrstor(void *addr) 1698b4fc8b1SKonstantin Belousov { 1708b4fc8b1SKonstantin Belousov 1718b4fc8b1SKonstantin Belousov xrstor((char *)addr, xsave_mask); 1728b4fc8b1SKonstantin Belousov } 1738b4fc8b1SKonstantin Belousov 1748b4fc8b1SKonstantin Belousov static void 1758b4fc8b1SKonstantin Belousov fpusave_fxsave(void *addr) 1768b4fc8b1SKonstantin Belousov { 1778b4fc8b1SKonstantin Belousov 1788c6f8f3dSKonstantin Belousov fxsave((char *)addr); 1798c6f8f3dSKonstantin Belousov } 1808c6f8f3dSKonstantin Belousov 1818b4fc8b1SKonstantin Belousov static void 1828b4fc8b1SKonstantin Belousov fpurestore_fxrstor(void *addr) 1838b4fc8b1SKonstantin Belousov { 1848b4fc8b1SKonstantin Belousov 1858b4fc8b1SKonstantin Belousov fxrstor((char *)addr); 1868b4fc8b1SKonstantin Belousov } 1878b4fc8b1SKonstantin Belousov 1888b4fc8b1SKonstantin Belousov static void 1898b4fc8b1SKonstantin Belousov init_xsave(void) 1908c6f8f3dSKonstantin Belousov { 1918c6f8f3dSKonstantin Belousov 1928c6f8f3dSKonstantin Belousov if (use_xsave) 1938b4fc8b1SKonstantin Belousov return; 1948b4fc8b1SKonstantin Belousov if ((cpu_feature2 & CPUID2_XSAVE) == 0) 1958b4fc8b1SKonstantin Belousov return; 1968b4fc8b1SKonstantin Belousov use_xsave = 1; 1978b4fc8b1SKonstantin Belousov TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave); 1988b4fc8b1SKonstantin Belousov } 1998b4fc8b1SKonstantin Belousov 2008b4fc8b1SKonstantin Belousov DEFINE_IFUNC(, void, fpusave, (void *), static) 2018b4fc8b1SKonstantin Belousov { 2028b4fc8b1SKonstantin Belousov 2038b4fc8b1SKonstantin Belousov init_xsave(); 2048b4fc8b1SKonstantin Belousov return (use_xsave ? fpusave_xsave : fpusave_fxsave); 2058b4fc8b1SKonstantin Belousov } 2068b4fc8b1SKonstantin Belousov 2078b4fc8b1SKonstantin Belousov DEFINE_IFUNC(, void, fpurestore, (void *), static) 2088b4fc8b1SKonstantin Belousov { 2098b4fc8b1SKonstantin Belousov 2108b4fc8b1SKonstantin Belousov init_xsave(); 2118b4fc8b1SKonstantin Belousov return (use_xsave ? fpurestore_xrstor : fpurestore_fxrstor); 2128c6f8f3dSKonstantin Belousov } 2133902c3efSSteve Passe 2141d22d877SJung-uk Kim void 2151d22d877SJung-uk Kim fpususpend(void *addr) 2161d22d877SJung-uk Kim { 2171d22d877SJung-uk Kim u_long cr0; 2181d22d877SJung-uk Kim 2191d22d877SJung-uk Kim cr0 = rcr0(); 2201d22d877SJung-uk Kim stop_emulating(); 2211d22d877SJung-uk Kim fpusave(addr); 2221d22d877SJung-uk Kim load_cr0(cr0); 2231d22d877SJung-uk Kim } 2241d22d877SJung-uk Kim 225b1d735baSJohn Baldwin void 226b1d735baSJohn Baldwin fpuresume(void *addr) 227b1d735baSJohn Baldwin { 228b1d735baSJohn Baldwin u_long cr0; 229b1d735baSJohn Baldwin 230b1d735baSJohn Baldwin cr0 = rcr0(); 231b1d735baSJohn Baldwin stop_emulating(); 232b1d735baSJohn Baldwin fninit(); 233b1d735baSJohn Baldwin if (use_xsave) 234b1d735baSJohn Baldwin load_xcr(XCR0, xsave_mask); 235b1d735baSJohn Baldwin fpurestore(addr); 236b1d735baSJohn Baldwin load_cr0(cr0); 237b1d735baSJohn Baldwin } 238b1d735baSJohn Baldwin 2395b81b6b3SRodney W. Grimes /* 2408c6f8f3dSKonstantin Belousov * Enable XSAVE if supported and allowed by user. 2418c6f8f3dSKonstantin Belousov * Calculate the xsave_mask. 2428c6f8f3dSKonstantin Belousov */ 2438c6f8f3dSKonstantin Belousov static void 2448c6f8f3dSKonstantin Belousov fpuinit_bsp1(void) 2458c6f8f3dSKonstantin Belousov { 2468c6f8f3dSKonstantin Belousov u_int cp[4]; 2478c6f8f3dSKonstantin Belousov uint64_t xsave_mask_user; 2489cffc92cSKonstantin Belousov bool old_wp; 2498c6f8f3dSKonstantin Belousov 250d1a07e31SKonstantin Belousov TUNABLE_INT_FETCH("hw.lazy_fpu_switch", &lazy_fpu_switch); 2518c6f8f3dSKonstantin Belousov if (!use_xsave) 2528c6f8f3dSKonstantin Belousov return; 2538c6f8f3dSKonstantin Belousov cpuid_count(0xd, 0x0, cp); 2548c6f8f3dSKonstantin Belousov xsave_mask = XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE; 2558c6f8f3dSKonstantin Belousov if ((cp[0] & xsave_mask) != xsave_mask) 2568c6f8f3dSKonstantin Belousov panic("CPU0 does not support X87 or SSE: %x", cp[0]); 2578c6f8f3dSKonstantin Belousov xsave_mask = ((uint64_t)cp[3] << 32) | cp[0]; 2588c6f8f3dSKonstantin Belousov xsave_mask_user = xsave_mask; 2598c6f8f3dSKonstantin Belousov TUNABLE_ULONG_FETCH("hw.xsave_mask", &xsave_mask_user); 2608c6f8f3dSKonstantin Belousov xsave_mask_user |= XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE; 2618c6f8f3dSKonstantin Belousov xsave_mask &= xsave_mask_user; 2620eb7ae8dSJohn Baldwin if ((xsave_mask & XFEATURE_AVX512) != XFEATURE_AVX512) 2630eb7ae8dSJohn Baldwin xsave_mask &= ~XFEATURE_AVX512; 2640eb7ae8dSJohn Baldwin if ((xsave_mask & XFEATURE_MPX) != XFEATURE_MPX) 2650eb7ae8dSJohn Baldwin xsave_mask &= ~XFEATURE_MPX; 266333d0c60SKonstantin Belousov 267333d0c60SKonstantin Belousov cpuid_count(0xd, 0x1, cp); 268333d0c60SKonstantin Belousov if ((cp[0] & CPUID_EXTSTATE_XSAVEOPT) != 0) { 269333d0c60SKonstantin Belousov /* 270333d0c60SKonstantin Belousov * Patch the XSAVE instruction in the cpu_switch code 271333d0c60SKonstantin Belousov * to XSAVEOPT. We assume that XSAVE encoding used 272333d0c60SKonstantin Belousov * REX byte, and set the bit 4 of the r/m byte. 2739cffc92cSKonstantin Belousov * 2749cffc92cSKonstantin Belousov * It seems that some BIOSes give control to the OS 2759cffc92cSKonstantin Belousov * with CR0.WP already set, making the kernel text 2769cffc92cSKonstantin Belousov * read-only before cpu_startup(). 277333d0c60SKonstantin Belousov */ 2789cffc92cSKonstantin Belousov old_wp = disable_wp(); 279333d0c60SKonstantin Belousov ctx_switch_xsave[3] |= 0x10; 2809cffc92cSKonstantin Belousov restore_wp(old_wp); 281333d0c60SKonstantin Belousov } 2828c6f8f3dSKonstantin Belousov } 2838c6f8f3dSKonstantin Belousov 2848c6f8f3dSKonstantin Belousov /* 2858c6f8f3dSKonstantin Belousov * Calculate the fpu save area size. 2868c6f8f3dSKonstantin Belousov */ 2878c6f8f3dSKonstantin Belousov static void 2888c6f8f3dSKonstantin Belousov fpuinit_bsp2(void) 2898c6f8f3dSKonstantin Belousov { 2908c6f8f3dSKonstantin Belousov u_int cp[4]; 2918c6f8f3dSKonstantin Belousov 2928c6f8f3dSKonstantin Belousov if (use_xsave) { 2938c6f8f3dSKonstantin Belousov cpuid_count(0xd, 0x0, cp); 2948c6f8f3dSKonstantin Belousov cpu_max_ext_state_size = cp[1]; 2958c6f8f3dSKonstantin Belousov 2968c6f8f3dSKonstantin Belousov /* 2978c6f8f3dSKonstantin Belousov * Reload the cpu_feature2, since we enabled OSXSAVE. 2988c6f8f3dSKonstantin Belousov */ 2998c6f8f3dSKonstantin Belousov do_cpuid(1, cp); 3008c6f8f3dSKonstantin Belousov cpu_feature2 = cp[2]; 3018c6f8f3dSKonstantin Belousov } else 3028c6f8f3dSKonstantin Belousov cpu_max_ext_state_size = sizeof(struct savefpu); 3038c6f8f3dSKonstantin Belousov } 3048c6f8f3dSKonstantin Belousov 3058c6f8f3dSKonstantin Belousov /* 3068c6f8f3dSKonstantin Belousov * Initialize the floating point unit. 307da4113b3SPeter Wemm */ 308398dbb11SPeter Wemm void 3091c89210cSPeter Wemm fpuinit(void) 310da4113b3SPeter Wemm { 3110689bdccSJohn Baldwin register_t saveintr; 31296a7759eSPeter Wemm u_int mxcsr; 313398dbb11SPeter Wemm u_short control; 314da4113b3SPeter Wemm 3158c6f8f3dSKonstantin Belousov if (IS_BSP()) 3168c6f8f3dSKonstantin Belousov fpuinit_bsp1(); 3178c6f8f3dSKonstantin Belousov 3188c6f8f3dSKonstantin Belousov if (use_xsave) { 3198c6f8f3dSKonstantin Belousov load_cr4(rcr4() | CR4_XSAVE); 3207574a595SJohn Baldwin load_xcr(XCR0, xsave_mask); 3218c6f8f3dSKonstantin Belousov } 3228c6f8f3dSKonstantin Belousov 3238c6f8f3dSKonstantin Belousov /* 3248c6f8f3dSKonstantin Belousov * XCR0 shall be set up before CPU can report the save area size. 3258c6f8f3dSKonstantin Belousov */ 3268c6f8f3dSKonstantin Belousov if (IS_BSP()) 3278c6f8f3dSKonstantin Belousov fpuinit_bsp2(); 3288c6f8f3dSKonstantin Belousov 32999753495SKonstantin Belousov /* 33099753495SKonstantin Belousov * It is too early for critical_enter() to work on AP. 33199753495SKonstantin Belousov */ 3320689bdccSJohn Baldwin saveintr = intr_disable(); 3335b81b6b3SRodney W. Grimes stop_emulating(); 3345b81b6b3SRodney W. Grimes fninit(); 335398dbb11SPeter Wemm control = __INITIAL_FPUCW__; 33617275403SJung-uk Kim fldcw(control); 33796a7759eSPeter Wemm mxcsr = __INITIAL_MXCSR__; 33896a7759eSPeter Wemm ldmxcsr(mxcsr); 339a8346a98SJohn Baldwin start_emulating(); 3400689bdccSJohn Baldwin intr_restore(saveintr); 3415b81b6b3SRodney W. Grimes } 3425b81b6b3SRodney W. Grimes 3435b81b6b3SRodney W. Grimes /* 3448c6f8f3dSKonstantin Belousov * On the boot CPU we generate a clean state that is used to 3458c6f8f3dSKonstantin Belousov * initialize the floating point unit when it is first used by a 3468c6f8f3dSKonstantin Belousov * process. 3478c6f8f3dSKonstantin Belousov */ 3488c6f8f3dSKonstantin Belousov static void 3498c6f8f3dSKonstantin Belousov fpuinitstate(void *arg __unused) 3508c6f8f3dSKonstantin Belousov { 3518c6f8f3dSKonstantin Belousov register_t saveintr; 352333d0c60SKonstantin Belousov int cp[4], i, max_ext_n; 3538c6f8f3dSKonstantin Belousov 3548c6f8f3dSKonstantin Belousov fpu_initialstate = malloc(cpu_max_ext_state_size, M_DEVBUF, 3558c6f8f3dSKonstantin Belousov M_WAITOK | M_ZERO); 3568c6f8f3dSKonstantin Belousov saveintr = intr_disable(); 3578c6f8f3dSKonstantin Belousov stop_emulating(); 3588c6f8f3dSKonstantin Belousov 3598c6f8f3dSKonstantin Belousov fpusave(fpu_initialstate); 3608c6f8f3dSKonstantin Belousov if (fpu_initialstate->sv_env.en_mxcsr_mask) 3618c6f8f3dSKonstantin Belousov cpu_mxcsr_mask = fpu_initialstate->sv_env.en_mxcsr_mask; 3628c6f8f3dSKonstantin Belousov else 3638c6f8f3dSKonstantin Belousov cpu_mxcsr_mask = 0xFFBF; 3648c6f8f3dSKonstantin Belousov 3658c6f8f3dSKonstantin Belousov /* 366b57e6814SKonstantin Belousov * The fninit instruction does not modify XMM registers or x87 367b57e6814SKonstantin Belousov * registers (MM/ST). The fpusave call dumped the garbage 368b57e6814SKonstantin Belousov * contained in the registers after reset to the initial state 369b57e6814SKonstantin Belousov * saved. Clear XMM and x87 registers file image to make the 370b57e6814SKonstantin Belousov * startup program state and signal handler XMM/x87 register 371b57e6814SKonstantin Belousov * content predictable. 3728c6f8f3dSKonstantin Belousov */ 373b57e6814SKonstantin Belousov bzero(fpu_initialstate->sv_fp, sizeof(fpu_initialstate->sv_fp)); 374b57e6814SKonstantin Belousov bzero(fpu_initialstate->sv_xmm, sizeof(fpu_initialstate->sv_xmm)); 3758c6f8f3dSKonstantin Belousov 376333d0c60SKonstantin Belousov /* 377333d0c60SKonstantin Belousov * Create a table describing the layout of the CPU Extended 378333d0c60SKonstantin Belousov * Save Area. 379333d0c60SKonstantin Belousov */ 38014f52559SKonstantin Belousov if (use_xsave) { 381333d0c60SKonstantin Belousov max_ext_n = flsl(xsave_mask); 382333d0c60SKonstantin Belousov xsave_area_desc = malloc(max_ext_n * sizeof(struct 383333d0c60SKonstantin Belousov xsave_area_elm_descr), M_DEVBUF, M_WAITOK | M_ZERO); 384333d0c60SKonstantin Belousov /* x87 state */ 385333d0c60SKonstantin Belousov xsave_area_desc[0].offset = 0; 386333d0c60SKonstantin Belousov xsave_area_desc[0].size = 160; 387333d0c60SKonstantin Belousov /* XMM */ 388333d0c60SKonstantin Belousov xsave_area_desc[1].offset = 160; 389333d0c60SKonstantin Belousov xsave_area_desc[1].size = 288 - 160; 390333d0c60SKonstantin Belousov 391333d0c60SKonstantin Belousov for (i = 2; i < max_ext_n; i++) { 392333d0c60SKonstantin Belousov cpuid_count(0xd, i, cp); 393333d0c60SKonstantin Belousov xsave_area_desc[i].offset = cp[1]; 394333d0c60SKonstantin Belousov xsave_area_desc[i].size = cp[0]; 395333d0c60SKonstantin Belousov } 396333d0c60SKonstantin Belousov } 397333d0c60SKonstantin Belousov 3982741efecSPeter Grehan fpu_save_area_zone = uma_zcreate("FPU_save_area", 3992741efecSPeter Grehan cpu_max_ext_state_size, NULL, NULL, NULL, NULL, 4002741efecSPeter Grehan XSAVE_AREA_ALIGN - 1, 0); 4012741efecSPeter Grehan 4028c6f8f3dSKonstantin Belousov start_emulating(); 4038c6f8f3dSKonstantin Belousov intr_restore(saveintr); 4048c6f8f3dSKonstantin Belousov } 405ad456dd9SKyle Evans /* EFIRT needs this to be initialized before we can enter our EFI environment */ 406ad456dd9SKyle Evans SYSINIT(fpuinitstate, SI_SUB_DRIVERS, SI_ORDER_FIRST, fpuinitstate, NULL); 4078c6f8f3dSKonstantin Belousov 4088c6f8f3dSKonstantin Belousov /* 4095b81b6b3SRodney W. Grimes * Free coprocessor (if we have it). 4105b81b6b3SRodney W. Grimes */ 4115b81b6b3SRodney W. Grimes void 412bf2f09eeSPeter Wemm fpuexit(struct thread *td) 4135b81b6b3SRodney W. Grimes { 4145b81b6b3SRodney W. Grimes 41599753495SKonstantin Belousov critical_enter(); 4161c89210cSPeter Wemm if (curthread == PCPU_GET(fpcurthread)) { 4171c89210cSPeter Wemm stop_emulating(); 41883b22b05SKonstantin Belousov fpusave(curpcb->pcb_save); 4191c89210cSPeter Wemm start_emulating(); 4206dfc9e44SKonstantin Belousov PCPU_SET(fpcurthread, NULL); 4211c89210cSPeter Wemm } 42299753495SKonstantin Belousov critical_exit(); 4235b81b6b3SRodney W. Grimes } 4245b81b6b3SRodney W. Grimes 42530abe507SJonathan Mini int 426f132cd05SKonstantin Belousov fpuformat(void) 42730abe507SJonathan Mini { 42830abe507SJonathan Mini 42930abe507SJonathan Mini return (_MC_FPFMT_XMM); 43030abe507SJonathan Mini } 43130abe507SJonathan Mini 4325b81b6b3SRodney W. Grimes /* 433a7674320SMartin Cracauer * The following mechanism is used to ensure that the FPE_... value 434a7674320SMartin Cracauer * that is passed as a trapcode to the signal handler of the user 435a7674320SMartin Cracauer * process does not have more than one bit set. 436a7674320SMartin Cracauer * 437a7674320SMartin Cracauer * Multiple bits may be set if the user process modifies the control 438a7674320SMartin Cracauer * word while a status word bit is already set. While this is a sign 439a7674320SMartin Cracauer * of bad coding, we have no choise than to narrow them down to one 440a7674320SMartin Cracauer * bit, since we must not send a trapcode that is not exactly one of 441a7674320SMartin Cracauer * the FPE_ macros. 442a7674320SMartin Cracauer * 443a7674320SMartin Cracauer * The mechanism has a static table with 127 entries. Each combination 444a7674320SMartin Cracauer * of the 7 FPU status word exception bits directly translates to a 445a7674320SMartin Cracauer * position in this table, where a single FPE_... value is stored. 446a7674320SMartin Cracauer * This FPE_... value stored there is considered the "most important" 447a7674320SMartin Cracauer * of the exception bits and will be sent as the signal code. The 448a7674320SMartin Cracauer * precedence of the bits is based upon Intel Document "Numerical 449a7674320SMartin Cracauer * Applications", Chapter "Special Computational Situations". 450a7674320SMartin Cracauer * 451a7674320SMartin Cracauer * The macro to choose one of these values does these steps: 1) Throw 452a7674320SMartin Cracauer * away status word bits that cannot be masked. 2) Throw away the bits 453a7674320SMartin Cracauer * currently masked in the control word, assuming the user isn't 454a7674320SMartin Cracauer * interested in them anymore. 3) Reinsert status word bit 7 (stack 455a7674320SMartin Cracauer * fault) if it is set, which cannot be masked but must be presered. 456a7674320SMartin Cracauer * 4) Use the remaining bits to point into the trapcode table. 457a7674320SMartin Cracauer * 458a7674320SMartin Cracauer * The 6 maskable bits in order of their preference, as stated in the 459a7674320SMartin Cracauer * above referenced Intel manual: 460a7674320SMartin Cracauer * 1 Invalid operation (FP_X_INV) 461a7674320SMartin Cracauer * 1a Stack underflow 462a7674320SMartin Cracauer * 1b Stack overflow 463a7674320SMartin Cracauer * 1c Operand of unsupported format 464a7674320SMartin Cracauer * 1d SNaN operand. 465a7674320SMartin Cracauer * 2 QNaN operand (not an exception, irrelavant here) 466a7674320SMartin Cracauer * 3 Any other invalid-operation not mentioned above or zero divide 467a7674320SMartin Cracauer * (FP_X_INV, FP_X_DZ) 468a7674320SMartin Cracauer * 4 Denormal operand (FP_X_DNML) 469a7674320SMartin Cracauer * 5 Numeric over/underflow (FP_X_OFL, FP_X_UFL) 470784648c6SMartin Cracauer * 6 Inexact result (FP_X_IMP) 471784648c6SMartin Cracauer */ 472a7674320SMartin Cracauer static char fpetable[128] = { 473a7674320SMartin Cracauer 0, 474a7674320SMartin Cracauer FPE_FLTINV, /* 1 - INV */ 475a7674320SMartin Cracauer FPE_FLTUND, /* 2 - DNML */ 476a7674320SMartin Cracauer FPE_FLTINV, /* 3 - INV | DNML */ 477a7674320SMartin Cracauer FPE_FLTDIV, /* 4 - DZ */ 478a7674320SMartin Cracauer FPE_FLTINV, /* 5 - INV | DZ */ 479a7674320SMartin Cracauer FPE_FLTDIV, /* 6 - DNML | DZ */ 480a7674320SMartin Cracauer FPE_FLTINV, /* 7 - INV | DNML | DZ */ 481a7674320SMartin Cracauer FPE_FLTOVF, /* 8 - OFL */ 482a7674320SMartin Cracauer FPE_FLTINV, /* 9 - INV | OFL */ 483a7674320SMartin Cracauer FPE_FLTUND, /* A - DNML | OFL */ 484a7674320SMartin Cracauer FPE_FLTINV, /* B - INV | DNML | OFL */ 485a7674320SMartin Cracauer FPE_FLTDIV, /* C - DZ | OFL */ 486a7674320SMartin Cracauer FPE_FLTINV, /* D - INV | DZ | OFL */ 487a7674320SMartin Cracauer FPE_FLTDIV, /* E - DNML | DZ | OFL */ 488a7674320SMartin Cracauer FPE_FLTINV, /* F - INV | DNML | DZ | OFL */ 489a7674320SMartin Cracauer FPE_FLTUND, /* 10 - UFL */ 490a7674320SMartin Cracauer FPE_FLTINV, /* 11 - INV | UFL */ 491a7674320SMartin Cracauer FPE_FLTUND, /* 12 - DNML | UFL */ 492a7674320SMartin Cracauer FPE_FLTINV, /* 13 - INV | DNML | UFL */ 493a7674320SMartin Cracauer FPE_FLTDIV, /* 14 - DZ | UFL */ 494a7674320SMartin Cracauer FPE_FLTINV, /* 15 - INV | DZ | UFL */ 495a7674320SMartin Cracauer FPE_FLTDIV, /* 16 - DNML | DZ | UFL */ 496a7674320SMartin Cracauer FPE_FLTINV, /* 17 - INV | DNML | DZ | UFL */ 497a7674320SMartin Cracauer FPE_FLTOVF, /* 18 - OFL | UFL */ 498a7674320SMartin Cracauer FPE_FLTINV, /* 19 - INV | OFL | UFL */ 499a7674320SMartin Cracauer FPE_FLTUND, /* 1A - DNML | OFL | UFL */ 500a7674320SMartin Cracauer FPE_FLTINV, /* 1B - INV | DNML | OFL | UFL */ 501a7674320SMartin Cracauer FPE_FLTDIV, /* 1C - DZ | OFL | UFL */ 502a7674320SMartin Cracauer FPE_FLTINV, /* 1D - INV | DZ | OFL | UFL */ 503a7674320SMartin Cracauer FPE_FLTDIV, /* 1E - DNML | DZ | OFL | UFL */ 504a7674320SMartin Cracauer FPE_FLTINV, /* 1F - INV | DNML | DZ | OFL | UFL */ 505a7674320SMartin Cracauer FPE_FLTRES, /* 20 - IMP */ 506a7674320SMartin Cracauer FPE_FLTINV, /* 21 - INV | IMP */ 507a7674320SMartin Cracauer FPE_FLTUND, /* 22 - DNML | IMP */ 508a7674320SMartin Cracauer FPE_FLTINV, /* 23 - INV | DNML | IMP */ 509a7674320SMartin Cracauer FPE_FLTDIV, /* 24 - DZ | IMP */ 510a7674320SMartin Cracauer FPE_FLTINV, /* 25 - INV | DZ | IMP */ 511a7674320SMartin Cracauer FPE_FLTDIV, /* 26 - DNML | DZ | IMP */ 512a7674320SMartin Cracauer FPE_FLTINV, /* 27 - INV | DNML | DZ | IMP */ 513a7674320SMartin Cracauer FPE_FLTOVF, /* 28 - OFL | IMP */ 514a7674320SMartin Cracauer FPE_FLTINV, /* 29 - INV | OFL | IMP */ 515a7674320SMartin Cracauer FPE_FLTUND, /* 2A - DNML | OFL | IMP */ 516a7674320SMartin Cracauer FPE_FLTINV, /* 2B - INV | DNML | OFL | IMP */ 517a7674320SMartin Cracauer FPE_FLTDIV, /* 2C - DZ | OFL | IMP */ 518a7674320SMartin Cracauer FPE_FLTINV, /* 2D - INV | DZ | OFL | IMP */ 519a7674320SMartin Cracauer FPE_FLTDIV, /* 2E - DNML | DZ | OFL | IMP */ 520a7674320SMartin Cracauer FPE_FLTINV, /* 2F - INV | DNML | DZ | OFL | IMP */ 521a7674320SMartin Cracauer FPE_FLTUND, /* 30 - UFL | IMP */ 522a7674320SMartin Cracauer FPE_FLTINV, /* 31 - INV | UFL | IMP */ 523a7674320SMartin Cracauer FPE_FLTUND, /* 32 - DNML | UFL | IMP */ 524a7674320SMartin Cracauer FPE_FLTINV, /* 33 - INV | DNML | UFL | IMP */ 525a7674320SMartin Cracauer FPE_FLTDIV, /* 34 - DZ | UFL | IMP */ 526a7674320SMartin Cracauer FPE_FLTINV, /* 35 - INV | DZ | UFL | IMP */ 527a7674320SMartin Cracauer FPE_FLTDIV, /* 36 - DNML | DZ | UFL | IMP */ 528a7674320SMartin Cracauer FPE_FLTINV, /* 37 - INV | DNML | DZ | UFL | IMP */ 529a7674320SMartin Cracauer FPE_FLTOVF, /* 38 - OFL | UFL | IMP */ 530a7674320SMartin Cracauer FPE_FLTINV, /* 39 - INV | OFL | UFL | IMP */ 531a7674320SMartin Cracauer FPE_FLTUND, /* 3A - DNML | OFL | UFL | IMP */ 532a7674320SMartin Cracauer FPE_FLTINV, /* 3B - INV | DNML | OFL | UFL | IMP */ 533a7674320SMartin Cracauer FPE_FLTDIV, /* 3C - DZ | OFL | UFL | IMP */ 534a7674320SMartin Cracauer FPE_FLTINV, /* 3D - INV | DZ | OFL | UFL | IMP */ 535a7674320SMartin Cracauer FPE_FLTDIV, /* 3E - DNML | DZ | OFL | UFL | IMP */ 536a7674320SMartin Cracauer FPE_FLTINV, /* 3F - INV | DNML | DZ | OFL | UFL | IMP */ 537a7674320SMartin Cracauer FPE_FLTSUB, /* 40 - STK */ 538a7674320SMartin Cracauer FPE_FLTSUB, /* 41 - INV | STK */ 539a7674320SMartin Cracauer FPE_FLTUND, /* 42 - DNML | STK */ 540a7674320SMartin Cracauer FPE_FLTSUB, /* 43 - INV | DNML | STK */ 541a7674320SMartin Cracauer FPE_FLTDIV, /* 44 - DZ | STK */ 542a7674320SMartin Cracauer FPE_FLTSUB, /* 45 - INV | DZ | STK */ 543a7674320SMartin Cracauer FPE_FLTDIV, /* 46 - DNML | DZ | STK */ 544a7674320SMartin Cracauer FPE_FLTSUB, /* 47 - INV | DNML | DZ | STK */ 545a7674320SMartin Cracauer FPE_FLTOVF, /* 48 - OFL | STK */ 546a7674320SMartin Cracauer FPE_FLTSUB, /* 49 - INV | OFL | STK */ 547a7674320SMartin Cracauer FPE_FLTUND, /* 4A - DNML | OFL | STK */ 548a7674320SMartin Cracauer FPE_FLTSUB, /* 4B - INV | DNML | OFL | STK */ 549a7674320SMartin Cracauer FPE_FLTDIV, /* 4C - DZ | OFL | STK */ 550a7674320SMartin Cracauer FPE_FLTSUB, /* 4D - INV | DZ | OFL | STK */ 551a7674320SMartin Cracauer FPE_FLTDIV, /* 4E - DNML | DZ | OFL | STK */ 552a7674320SMartin Cracauer FPE_FLTSUB, /* 4F - INV | DNML | DZ | OFL | STK */ 553a7674320SMartin Cracauer FPE_FLTUND, /* 50 - UFL | STK */ 554a7674320SMartin Cracauer FPE_FLTSUB, /* 51 - INV | UFL | STK */ 555a7674320SMartin Cracauer FPE_FLTUND, /* 52 - DNML | UFL | STK */ 556a7674320SMartin Cracauer FPE_FLTSUB, /* 53 - INV | DNML | UFL | STK */ 557a7674320SMartin Cracauer FPE_FLTDIV, /* 54 - DZ | UFL | STK */ 558a7674320SMartin Cracauer FPE_FLTSUB, /* 55 - INV | DZ | UFL | STK */ 559a7674320SMartin Cracauer FPE_FLTDIV, /* 56 - DNML | DZ | UFL | STK */ 560a7674320SMartin Cracauer FPE_FLTSUB, /* 57 - INV | DNML | DZ | UFL | STK */ 561a7674320SMartin Cracauer FPE_FLTOVF, /* 58 - OFL | UFL | STK */ 562a7674320SMartin Cracauer FPE_FLTSUB, /* 59 - INV | OFL | UFL | STK */ 563a7674320SMartin Cracauer FPE_FLTUND, /* 5A - DNML | OFL | UFL | STK */ 564a7674320SMartin Cracauer FPE_FLTSUB, /* 5B - INV | DNML | OFL | UFL | STK */ 565a7674320SMartin Cracauer FPE_FLTDIV, /* 5C - DZ | OFL | UFL | STK */ 566a7674320SMartin Cracauer FPE_FLTSUB, /* 5D - INV | DZ | OFL | UFL | STK */ 567a7674320SMartin Cracauer FPE_FLTDIV, /* 5E - DNML | DZ | OFL | UFL | STK */ 568a7674320SMartin Cracauer FPE_FLTSUB, /* 5F - INV | DNML | DZ | OFL | UFL | STK */ 569a7674320SMartin Cracauer FPE_FLTRES, /* 60 - IMP | STK */ 570a7674320SMartin Cracauer FPE_FLTSUB, /* 61 - INV | IMP | STK */ 571a7674320SMartin Cracauer FPE_FLTUND, /* 62 - DNML | IMP | STK */ 572a7674320SMartin Cracauer FPE_FLTSUB, /* 63 - INV | DNML | IMP | STK */ 573a7674320SMartin Cracauer FPE_FLTDIV, /* 64 - DZ | IMP | STK */ 574a7674320SMartin Cracauer FPE_FLTSUB, /* 65 - INV | DZ | IMP | STK */ 575a7674320SMartin Cracauer FPE_FLTDIV, /* 66 - DNML | DZ | IMP | STK */ 576a7674320SMartin Cracauer FPE_FLTSUB, /* 67 - INV | DNML | DZ | IMP | STK */ 577a7674320SMartin Cracauer FPE_FLTOVF, /* 68 - OFL | IMP | STK */ 578a7674320SMartin Cracauer FPE_FLTSUB, /* 69 - INV | OFL | IMP | STK */ 579a7674320SMartin Cracauer FPE_FLTUND, /* 6A - DNML | OFL | IMP | STK */ 580a7674320SMartin Cracauer FPE_FLTSUB, /* 6B - INV | DNML | OFL | IMP | STK */ 581a7674320SMartin Cracauer FPE_FLTDIV, /* 6C - DZ | OFL | IMP | STK */ 582a7674320SMartin Cracauer FPE_FLTSUB, /* 6D - INV | DZ | OFL | IMP | STK */ 583a7674320SMartin Cracauer FPE_FLTDIV, /* 6E - DNML | DZ | OFL | IMP | STK */ 584a7674320SMartin Cracauer FPE_FLTSUB, /* 6F - INV | DNML | DZ | OFL | IMP | STK */ 585a7674320SMartin Cracauer FPE_FLTUND, /* 70 - UFL | IMP | STK */ 586a7674320SMartin Cracauer FPE_FLTSUB, /* 71 - INV | UFL | IMP | STK */ 587a7674320SMartin Cracauer FPE_FLTUND, /* 72 - DNML | UFL | IMP | STK */ 588a7674320SMartin Cracauer FPE_FLTSUB, /* 73 - INV | DNML | UFL | IMP | STK */ 589a7674320SMartin Cracauer FPE_FLTDIV, /* 74 - DZ | UFL | IMP | STK */ 590a7674320SMartin Cracauer FPE_FLTSUB, /* 75 - INV | DZ | UFL | IMP | STK */ 591a7674320SMartin Cracauer FPE_FLTDIV, /* 76 - DNML | DZ | UFL | IMP | STK */ 592a7674320SMartin Cracauer FPE_FLTSUB, /* 77 - INV | DNML | DZ | UFL | IMP | STK */ 593a7674320SMartin Cracauer FPE_FLTOVF, /* 78 - OFL | UFL | IMP | STK */ 594a7674320SMartin Cracauer FPE_FLTSUB, /* 79 - INV | OFL | UFL | IMP | STK */ 595a7674320SMartin Cracauer FPE_FLTUND, /* 7A - DNML | OFL | UFL | IMP | STK */ 596a7674320SMartin Cracauer FPE_FLTSUB, /* 7B - INV | DNML | OFL | UFL | IMP | STK */ 597a7674320SMartin Cracauer FPE_FLTDIV, /* 7C - DZ | OFL | UFL | IMP | STK */ 598a7674320SMartin Cracauer FPE_FLTSUB, /* 7D - INV | DZ | OFL | UFL | IMP | STK */ 599a7674320SMartin Cracauer FPE_FLTDIV, /* 7E - DNML | DZ | OFL | UFL | IMP | STK */ 600a7674320SMartin Cracauer FPE_FLTSUB, /* 7F - INV | DNML | DZ | OFL | UFL | IMP | STK */ 601a7674320SMartin Cracauer }; 602a7674320SMartin Cracauer 603a7674320SMartin Cracauer /* 604dfa8a512SKonstantin Belousov * Read the FP status and control words, then generate si_code value 605dfa8a512SKonstantin Belousov * for SIGFPE. The error code chosen will be one of the 606dfa8a512SKonstantin Belousov * FPE_... macros. It will be sent as the second argument to old 607dfa8a512SKonstantin Belousov * BSD-style signal handlers and as "siginfo_t->si_code" (second 608dfa8a512SKonstantin Belousov * argument) to SA_SIGINFO signal handlers. 6095b81b6b3SRodney W. Grimes * 610dfa8a512SKonstantin Belousov * Some time ago, we cleared the x87 exceptions with FNCLEX there. 611dfa8a512SKonstantin Belousov * Clearing exceptions was necessary mainly to avoid IRQ13 bugs. The 612dfa8a512SKonstantin Belousov * usermode code which understands the FPU hardware enough to enable 613dfa8a512SKonstantin Belousov * the exceptions, can also handle clearing the exception state in the 614dfa8a512SKonstantin Belousov * handler. The only consequence of not clearing the exception is the 615dfa8a512SKonstantin Belousov * rethrow of the SIGFPE on return from the signal handler and 616dfa8a512SKonstantin Belousov * reexecution of the corresponding instruction. 617bc84db62SKonstantin Belousov * 618dfa8a512SKonstantin Belousov * For XMM traps, the exceptions were never cleared. 6195b81b6b3SRodney W. Grimes */ 6201c1771cbSBruce Evans int 621bc84db62SKonstantin Belousov fputrap_x87(void) 6225b81b6b3SRodney W. Grimes { 623bc84db62SKonstantin Belousov struct savefpu *pcb_save; 6241c1771cbSBruce Evans u_short control, status; 6255b81b6b3SRodney W. Grimes 62699753495SKonstantin Belousov critical_enter(); 6275b81b6b3SRodney W. Grimes 6285b81b6b3SRodney W. Grimes /* 6291c1771cbSBruce Evans * Interrupt handling (for another interrupt) may have pushed the 6301c1771cbSBruce Evans * state to memory. Fetch the relevant parts of the state from 6311c1771cbSBruce Evans * wherever they are. 6325b81b6b3SRodney W. Grimes */ 6330bbc8826SJohn Baldwin if (PCPU_GET(fpcurthread) != curthread) { 63483b22b05SKonstantin Belousov pcb_save = curpcb->pcb_save; 635bc84db62SKonstantin Belousov control = pcb_save->sv_env.en_cw; 636bc84db62SKonstantin Belousov status = pcb_save->sv_env.en_sw; 6375b81b6b3SRodney W. Grimes } else { 6381c1771cbSBruce Evans fnstcw(&control); 6391c1771cbSBruce Evans fnstsw(&status); 6405b81b6b3SRodney W. Grimes } 6411c1771cbSBruce Evans 64299753495SKonstantin Belousov critical_exit(); 6431c1771cbSBruce Evans return (fpetable[status & ((~control & 0x3f) | 0x40)]); 6445b81b6b3SRodney W. Grimes } 6455b81b6b3SRodney W. Grimes 646bc84db62SKonstantin Belousov int 647bc84db62SKonstantin Belousov fputrap_sse(void) 648bc84db62SKonstantin Belousov { 649bc84db62SKonstantin Belousov u_int mxcsr; 650bc84db62SKonstantin Belousov 651bc84db62SKonstantin Belousov critical_enter(); 652bc84db62SKonstantin Belousov if (PCPU_GET(fpcurthread) != curthread) 65383b22b05SKonstantin Belousov mxcsr = curpcb->pcb_save->sv_env.en_mxcsr; 654bc84db62SKonstantin Belousov else 655bc84db62SKonstantin Belousov stmxcsr(&mxcsr); 656bc84db62SKonstantin Belousov critical_exit(); 657bc84db62SKonstantin Belousov return (fpetable[(mxcsr & (~mxcsr >> 7)) & 0x3f]); 658bc84db62SKonstantin Belousov } 659bc84db62SKonstantin Belousov 660d1a07e31SKonstantin Belousov static void 661d1a07e31SKonstantin Belousov restore_fpu_curthread(struct thread *td) 662d1a07e31SKonstantin Belousov { 663d1a07e31SKonstantin Belousov struct pcb *pcb; 664d1a07e31SKonstantin Belousov 665d1a07e31SKonstantin Belousov /* 666d1a07e31SKonstantin Belousov * Record new context early in case frstor causes a trap. 667d1a07e31SKonstantin Belousov */ 668d1a07e31SKonstantin Belousov PCPU_SET(fpcurthread, td); 669d1a07e31SKonstantin Belousov 670d1a07e31SKonstantin Belousov stop_emulating(); 671d1a07e31SKonstantin Belousov fpu_clean_state(); 672d1a07e31SKonstantin Belousov pcb = td->td_pcb; 673d1a07e31SKonstantin Belousov 674d1a07e31SKonstantin Belousov if ((pcb->pcb_flags & PCB_FPUINITDONE) == 0) { 675d1a07e31SKonstantin Belousov /* 676d1a07e31SKonstantin Belousov * This is the first time this thread has used the FPU or 677d1a07e31SKonstantin Belousov * the PCB doesn't contain a clean FPU state. Explicitly 678d1a07e31SKonstantin Belousov * load an initial state. 679d1a07e31SKonstantin Belousov * 680d1a07e31SKonstantin Belousov * We prefer to restore the state from the actual save 681d1a07e31SKonstantin Belousov * area in PCB instead of directly loading from 682d1a07e31SKonstantin Belousov * fpu_initialstate, to ignite the XSAVEOPT 683d1a07e31SKonstantin Belousov * tracking engine. 684d1a07e31SKonstantin Belousov */ 685d1a07e31SKonstantin Belousov bcopy(fpu_initialstate, pcb->pcb_save, 686d1a07e31SKonstantin Belousov cpu_max_ext_state_size); 687d1a07e31SKonstantin Belousov fpurestore(pcb->pcb_save); 688d1a07e31SKonstantin Belousov if (pcb->pcb_initial_fpucw != __INITIAL_FPUCW__) 689d1a07e31SKonstantin Belousov fldcw(pcb->pcb_initial_fpucw); 690d1a07e31SKonstantin Belousov if (PCB_USER_FPU(pcb)) 691d1a07e31SKonstantin Belousov set_pcb_flags(pcb, PCB_FPUINITDONE | 692d1a07e31SKonstantin Belousov PCB_USERFPUINITDONE); 693d1a07e31SKonstantin Belousov else 694d1a07e31SKonstantin Belousov set_pcb_flags(pcb, PCB_FPUINITDONE); 695d1a07e31SKonstantin Belousov } else 696d1a07e31SKonstantin Belousov fpurestore(pcb->pcb_save); 697d1a07e31SKonstantin Belousov } 698d1a07e31SKonstantin Belousov 6996dfc9e44SKonstantin Belousov /* 7006dfc9e44SKonstantin Belousov * Device Not Available (DNA, #NM) exception handler. 7016dfc9e44SKonstantin Belousov * 7026dfc9e44SKonstantin Belousov * It would be better to switch FP context here (if curthread != 7036dfc9e44SKonstantin Belousov * fpcurthread) and not necessarily for every context switch, but it 7046dfc9e44SKonstantin Belousov * is too hard to access foreign pcb's. 7056dfc9e44SKonstantin Belousov */ 706a8346a98SJohn Baldwin void 707a8346a98SJohn Baldwin fpudna(void) 7085b81b6b3SRodney W. Grimes { 709d1a07e31SKonstantin Belousov struct thread *td; 71005f6ee66SJake Burkholder 711d1a07e31SKonstantin Belousov td = curthread; 712060cd4d5SKonstantin Belousov /* 713060cd4d5SKonstantin Belousov * This handler is entered with interrupts enabled, so context 714060cd4d5SKonstantin Belousov * switches may occur before critical_enter() is executed. If 715060cd4d5SKonstantin Belousov * a context switch occurs, then when we regain control, our 716060cd4d5SKonstantin Belousov * state will have been completely restored. The CPU may 717060cd4d5SKonstantin Belousov * change underneath us, but the only part of our context that 718060cd4d5SKonstantin Belousov * lives in the CPU is CR0.TS and that will be "restored" by 719060cd4d5SKonstantin Belousov * setting it on the new CPU. 720060cd4d5SKonstantin Belousov */ 72199753495SKonstantin Belousov critical_enter(); 722060cd4d5SKonstantin Belousov 723cf1c4776SKonstantin Belousov KASSERT((curpcb->pcb_flags & PCB_FPUNOSAVE) == 0, 724cf1c4776SKonstantin Belousov ("fpudna while in fpu_kern_enter(FPU_KERN_NOCTX)")); 725d1a07e31SKonstantin Belousov if (PCPU_GET(fpcurthread) == td) { 726*fa7fad8aSKonstantin Belousov /* 727*fa7fad8aSKonstantin Belousov * Some virtual machines seems to set %cr0.TS at 728*fa7fad8aSKonstantin Belousov * arbitrary moments. Silently clear the TS bit 729*fa7fad8aSKonstantin Belousov * regardless of the eager/lazy FPU context switch 730*fa7fad8aSKonstantin Belousov * mode. 731*fa7fad8aSKonstantin Belousov */ 73230abe507SJonathan Mini stop_emulating(); 73399753495SKonstantin Belousov critical_exit(); 734a8346a98SJohn Baldwin return; 73530abe507SJonathan Mini } 7360bbc8826SJohn Baldwin if (PCPU_GET(fpcurthread) != NULL) { 7376dfc9e44SKonstantin Belousov panic("fpudna: fpcurthread = %p (%d), curthread = %p (%d)\n", 7386dfc9e44SKonstantin Belousov PCPU_GET(fpcurthread), PCPU_GET(fpcurthread)->td_tid, 739d1a07e31SKonstantin Belousov td, td->td_tid); 7405b81b6b3SRodney W. Grimes } 741d1a07e31SKonstantin Belousov restore_fpu_curthread(td); 74299753495SKonstantin Belousov critical_exit(); 7435b81b6b3SRodney W. Grimes } 7445b81b6b3SRodney W. Grimes 745d1a07e31SKonstantin Belousov void fpu_activate_sw(struct thread *td); /* Called from the context switch */ 746d1a07e31SKonstantin Belousov void 747d1a07e31SKonstantin Belousov fpu_activate_sw(struct thread *td) 748d1a07e31SKonstantin Belousov { 749d1a07e31SKonstantin Belousov 750d1a07e31SKonstantin Belousov if (lazy_fpu_switch || (td->td_pflags & TDP_KTHREAD) != 0 || 751d1a07e31SKonstantin Belousov !PCB_USER_FPU(td->td_pcb)) { 752d1a07e31SKonstantin Belousov PCPU_SET(fpcurthread, NULL); 753d1a07e31SKonstantin Belousov start_emulating(); 754d1a07e31SKonstantin Belousov } else if (PCPU_GET(fpcurthread) != td) { 755d1a07e31SKonstantin Belousov restore_fpu_curthread(td); 756d1a07e31SKonstantin Belousov } 757d1a07e31SKonstantin Belousov } 758d1a07e31SKonstantin Belousov 75930abe507SJonathan Mini void 760f132cd05SKonstantin Belousov fpudrop(void) 76130abe507SJonathan Mini { 76230abe507SJonathan Mini struct thread *td; 76330abe507SJonathan Mini 76430abe507SJonathan Mini td = PCPU_GET(fpcurthread); 76599753495SKonstantin Belousov KASSERT(td == curthread, ("fpudrop: fpcurthread != curthread")); 7664a23ecc7SKonstantin Belousov CRITICAL_ASSERT(td); 76730abe507SJonathan Mini PCPU_SET(fpcurthread, NULL); 768e6c006d9SJung-uk Kim clear_pcb_flags(td->td_pcb, PCB_FPUINITDONE); 76930abe507SJonathan Mini start_emulating(); 77030abe507SJonathan Mini } 77130abe507SJonathan Mini 77230abe507SJonathan Mini /* 7735c6eb037SKonstantin Belousov * Get the user state of the FPU into pcb->pcb_user_save without 7745c6eb037SKonstantin Belousov * dropping ownership (if possible). It returns the FPU ownership 7755c6eb037SKonstantin Belousov * status. 77630abe507SJonathan Mini */ 77730abe507SJonathan Mini int 7785c6eb037SKonstantin Belousov fpugetregs(struct thread *td) 7796cf9a08dSKonstantin Belousov { 7806cf9a08dSKonstantin Belousov struct pcb *pcb; 781333d0c60SKonstantin Belousov uint64_t *xstate_bv, bit; 782333d0c60SKonstantin Belousov char *sa; 78314f52559SKonstantin Belousov int max_ext_n, i, owned; 7846cf9a08dSKonstantin Belousov 7856cf9a08dSKonstantin Belousov pcb = td->td_pcb; 7866cf9a08dSKonstantin Belousov if ((pcb->pcb_flags & PCB_USERFPUINITDONE) == 0) { 7878c6f8f3dSKonstantin Belousov bcopy(fpu_initialstate, get_pcb_user_save_pcb(pcb), 7888c6f8f3dSKonstantin Belousov cpu_max_ext_state_size); 7898c6f8f3dSKonstantin Belousov get_pcb_user_save_pcb(pcb)->sv_env.en_cw = 7908c6f8f3dSKonstantin Belousov pcb->pcb_initial_fpucw; 7915c6eb037SKonstantin Belousov fpuuserinited(td); 7925c6eb037SKonstantin Belousov return (_MC_FPOWNED_PCB); 7936cf9a08dSKonstantin Belousov } 79499753495SKonstantin Belousov critical_enter(); 7956cf9a08dSKonstantin Belousov if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) { 7968c6f8f3dSKonstantin Belousov fpusave(get_pcb_user_save_pcb(pcb)); 79714f52559SKonstantin Belousov owned = _MC_FPOWNED_FPU; 7986cf9a08dSKonstantin Belousov } else { 79914f52559SKonstantin Belousov owned = _MC_FPOWNED_PCB; 80014f52559SKonstantin Belousov } 80199753495SKonstantin Belousov critical_exit(); 80214f52559SKonstantin Belousov if (use_xsave) { 803333d0c60SKonstantin Belousov /* 804333d0c60SKonstantin Belousov * Handle partially saved state. 805333d0c60SKonstantin Belousov */ 806333d0c60SKonstantin Belousov sa = (char *)get_pcb_user_save_pcb(pcb); 807333d0c60SKonstantin Belousov xstate_bv = (uint64_t *)(sa + sizeof(struct savefpu) + 808333d0c60SKonstantin Belousov offsetof(struct xstate_hdr, xstate_bv)); 809333d0c60SKonstantin Belousov max_ext_n = flsl(xsave_mask); 810333d0c60SKonstantin Belousov for (i = 0; i < max_ext_n; i++) { 811241b67bbSKonstantin Belousov bit = 1ULL << i; 812241b67bbSKonstantin Belousov if ((xsave_mask & bit) == 0 || (*xstate_bv & bit) != 0) 813333d0c60SKonstantin Belousov continue; 814333d0c60SKonstantin Belousov bcopy((char *)fpu_initialstate + 815333d0c60SKonstantin Belousov xsave_area_desc[i].offset, 816333d0c60SKonstantin Belousov sa + xsave_area_desc[i].offset, 817333d0c60SKonstantin Belousov xsave_area_desc[i].size); 818333d0c60SKonstantin Belousov *xstate_bv |= bit; 819333d0c60SKonstantin Belousov } 820333d0c60SKonstantin Belousov } 82114f52559SKonstantin Belousov return (owned); 8226cf9a08dSKonstantin Belousov } 8236cf9a08dSKonstantin Belousov 8245c6eb037SKonstantin Belousov void 8255c6eb037SKonstantin Belousov fpuuserinited(struct thread *td) 82630abe507SJonathan Mini { 8276cf9a08dSKonstantin Belousov struct pcb *pcb; 82830abe507SJonathan Mini 8296cf9a08dSKonstantin Belousov pcb = td->td_pcb; 8305c6eb037SKonstantin Belousov if (PCB_USER_FPU(pcb)) 831e6c006d9SJung-uk Kim set_pcb_flags(pcb, 832e6c006d9SJung-uk Kim PCB_FPUINITDONE | PCB_USERFPUINITDONE); 833e6c006d9SJung-uk Kim else 834e6c006d9SJung-uk Kim set_pcb_flags(pcb, PCB_FPUINITDONE); 83530abe507SJonathan Mini } 83630abe507SJonathan Mini 8378c6f8f3dSKonstantin Belousov int 8388c6f8f3dSKonstantin Belousov fpusetxstate(struct thread *td, char *xfpustate, size_t xfpustate_size) 8398c6f8f3dSKonstantin Belousov { 8408c6f8f3dSKonstantin Belousov struct xstate_hdr *hdr, *ehdr; 8418c6f8f3dSKonstantin Belousov size_t len, max_len; 8428c6f8f3dSKonstantin Belousov uint64_t bv; 8438c6f8f3dSKonstantin Belousov 8448c6f8f3dSKonstantin Belousov /* XXXKIB should we clear all extended state in xstate_bv instead ? */ 8458c6f8f3dSKonstantin Belousov if (xfpustate == NULL) 8468c6f8f3dSKonstantin Belousov return (0); 8478c6f8f3dSKonstantin Belousov if (!use_xsave) 8488c6f8f3dSKonstantin Belousov return (EOPNOTSUPP); 8498c6f8f3dSKonstantin Belousov 8508c6f8f3dSKonstantin Belousov len = xfpustate_size; 8518c6f8f3dSKonstantin Belousov if (len < sizeof(struct xstate_hdr)) 8528c6f8f3dSKonstantin Belousov return (EINVAL); 8538c6f8f3dSKonstantin Belousov max_len = cpu_max_ext_state_size - sizeof(struct savefpu); 8548c6f8f3dSKonstantin Belousov if (len > max_len) 8558c6f8f3dSKonstantin Belousov return (EINVAL); 8568c6f8f3dSKonstantin Belousov 8578c6f8f3dSKonstantin Belousov ehdr = (struct xstate_hdr *)xfpustate; 8588c6f8f3dSKonstantin Belousov bv = ehdr->xstate_bv; 8598c6f8f3dSKonstantin Belousov 8608c6f8f3dSKonstantin Belousov /* 8618c6f8f3dSKonstantin Belousov * Avoid #gp. 8628c6f8f3dSKonstantin Belousov */ 8638c6f8f3dSKonstantin Belousov if (bv & ~xsave_mask) 8648c6f8f3dSKonstantin Belousov return (EINVAL); 8658c6f8f3dSKonstantin Belousov 8668c6f8f3dSKonstantin Belousov hdr = (struct xstate_hdr *)(get_pcb_user_save_td(td) + 1); 8678c6f8f3dSKonstantin Belousov 8688c6f8f3dSKonstantin Belousov hdr->xstate_bv = bv; 8698c6f8f3dSKonstantin Belousov bcopy(xfpustate + sizeof(struct xstate_hdr), 8708c6f8f3dSKonstantin Belousov (char *)(hdr + 1), len - sizeof(struct xstate_hdr)); 8718c6f8f3dSKonstantin Belousov 8728c6f8f3dSKonstantin Belousov return (0); 8738c6f8f3dSKonstantin Belousov } 8748c6f8f3dSKonstantin Belousov 87530abe507SJonathan Mini /* 87630abe507SJonathan Mini * Set the state of the FPU. 87730abe507SJonathan Mini */ 8788c6f8f3dSKonstantin Belousov int 8798c6f8f3dSKonstantin Belousov fpusetregs(struct thread *td, struct savefpu *addr, char *xfpustate, 8808c6f8f3dSKonstantin Belousov size_t xfpustate_size) 8816cf9a08dSKonstantin Belousov { 8826cf9a08dSKonstantin Belousov struct pcb *pcb; 8838c6f8f3dSKonstantin Belousov int error; 8846cf9a08dSKonstantin Belousov 885aa788cc3SKonstantin Belousov addr->sv_env.en_mxcsr &= cpu_mxcsr_mask; 8866cf9a08dSKonstantin Belousov pcb = td->td_pcb; 88799753495SKonstantin Belousov critical_enter(); 8886cf9a08dSKonstantin Belousov if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) { 8898c6f8f3dSKonstantin Belousov error = fpusetxstate(td, xfpustate, xfpustate_size); 8908c6f8f3dSKonstantin Belousov if (error != 0) { 8918c6f8f3dSKonstantin Belousov critical_exit(); 8928c6f8f3dSKonstantin Belousov return (error); 8938c6f8f3dSKonstantin Belousov } 8948c6f8f3dSKonstantin Belousov bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr)); 8958c6f8f3dSKonstantin Belousov fpurestore(get_pcb_user_save_td(td)); 89699753495SKonstantin Belousov critical_exit(); 897e6c006d9SJung-uk Kim set_pcb_flags(pcb, PCB_FPUINITDONE | PCB_USERFPUINITDONE); 8986cf9a08dSKonstantin Belousov } else { 89999753495SKonstantin Belousov critical_exit(); 9008c6f8f3dSKonstantin Belousov error = fpusetxstate(td, xfpustate, xfpustate_size); 9018c6f8f3dSKonstantin Belousov if (error != 0) 9028c6f8f3dSKonstantin Belousov return (error); 9038c6f8f3dSKonstantin Belousov bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr)); 9045c6eb037SKonstantin Belousov fpuuserinited(td); 9056cf9a08dSKonstantin Belousov } 9068c6f8f3dSKonstantin Belousov return (0); 9076cf9a08dSKonstantin Belousov } 9086cf9a08dSKonstantin Belousov 9096182fdbdSPeter Wemm /* 9102652af56SColin Percival * On AuthenticAMD processors, the fxrstor instruction does not restore 9112652af56SColin Percival * the x87's stored last instruction pointer, last data pointer, and last 9122652af56SColin Percival * opcode values, except in the rare case in which the exception summary 9132652af56SColin Percival * (ES) bit in the x87 status word is set to 1. 9142652af56SColin Percival * 9152652af56SColin Percival * In order to avoid leaking this information across processes, we clean 9162652af56SColin Percival * these values by performing a dummy load before executing fxrstor(). 9172652af56SColin Percival */ 9182652af56SColin Percival static void 9192652af56SColin Percival fpu_clean_state(void) 9202652af56SColin Percival { 921b9dda9d6SJohn Baldwin static float dummy_variable = 0.0; 9222652af56SColin Percival u_short status; 9232652af56SColin Percival 9242652af56SColin Percival /* 9252652af56SColin Percival * Clear the ES bit in the x87 status word if it is currently 9262652af56SColin Percival * set, in order to avoid causing a fault in the upcoming load. 9272652af56SColin Percival */ 9282652af56SColin Percival fnstsw(&status); 9292652af56SColin Percival if (status & 0x80) 9302652af56SColin Percival fnclex(); 9312652af56SColin Percival 9322652af56SColin Percival /* 9332652af56SColin Percival * Load the dummy variable into the x87 stack. This mangles 9342652af56SColin Percival * the x87 stack, but we don't care since we're about to call 9352652af56SColin Percival * fxrstor() anyway. 9362652af56SColin Percival */ 93714965052SDimitry Andric __asm __volatile("ffree %%st(7); flds %0" : : "m" (dummy_variable)); 9382652af56SColin Percival } 9392652af56SColin Percival 9402652af56SColin Percival /* 941398dbb11SPeter Wemm * This really sucks. We want the acpi version only, but it requires 942398dbb11SPeter Wemm * the isa_if.h file in order to get the definitions. 9436182fdbdSPeter Wemm */ 944398dbb11SPeter Wemm #include "opt_isa.h" 945afa88623SPeter Wemm #ifdef DEV_ISA 946398dbb11SPeter Wemm #include <isa/isavar.h> 94754f1d0ceSGarrett Wollman /* 9485f063c7bSMike Smith * This sucks up the legacy ISA support assignments from PNPBIOS/ACPI. 94954f1d0ceSGarrett Wollman */ 950398dbb11SPeter Wemm static struct isa_pnp_id fpupnp_ids[] = { 95154f1d0ceSGarrett Wollman { 0x040cd041, "Legacy ISA coprocessor support" }, /* PNP0C04 */ 95254f1d0ceSGarrett Wollman { 0 } 95354f1d0ceSGarrett Wollman }; 95454f1d0ceSGarrett Wollman 95554f1d0ceSGarrett Wollman static int 956398dbb11SPeter Wemm fpupnp_probe(device_t dev) 95754f1d0ceSGarrett Wollman { 958bb9c06c1SMike Smith int result; 959bf2f09eeSPeter Wemm 960398dbb11SPeter Wemm result = ISA_PNP_PROBE(device_get_parent(dev), dev, fpupnp_ids); 961bf2f09eeSPeter Wemm if (result <= 0) 962bb9c06c1SMike Smith device_quiet(dev); 963bb9c06c1SMike Smith return (result); 96454f1d0ceSGarrett Wollman } 96554f1d0ceSGarrett Wollman 96654f1d0ceSGarrett Wollman static int 967398dbb11SPeter Wemm fpupnp_attach(device_t dev) 96854f1d0ceSGarrett Wollman { 969bf2f09eeSPeter Wemm 97054f1d0ceSGarrett Wollman return (0); 97154f1d0ceSGarrett Wollman } 97254f1d0ceSGarrett Wollman 973398dbb11SPeter Wemm static device_method_t fpupnp_methods[] = { 97454f1d0ceSGarrett Wollman /* Device interface */ 975398dbb11SPeter Wemm DEVMETHOD(device_probe, fpupnp_probe), 976398dbb11SPeter Wemm DEVMETHOD(device_attach, fpupnp_attach), 97754f1d0ceSGarrett Wollman DEVMETHOD(device_detach, bus_generic_detach), 97854f1d0ceSGarrett Wollman DEVMETHOD(device_shutdown, bus_generic_shutdown), 97954f1d0ceSGarrett Wollman DEVMETHOD(device_suspend, bus_generic_suspend), 98054f1d0ceSGarrett Wollman DEVMETHOD(device_resume, bus_generic_resume), 98154f1d0ceSGarrett Wollman 98254f1d0ceSGarrett Wollman { 0, 0 } 98354f1d0ceSGarrett Wollman }; 98454f1d0ceSGarrett Wollman 985398dbb11SPeter Wemm static driver_t fpupnp_driver = { 986398dbb11SPeter Wemm "fpupnp", 987398dbb11SPeter Wemm fpupnp_methods, 98854f1d0ceSGarrett Wollman 1, /* no softc */ 98954f1d0ceSGarrett Wollman }; 99054f1d0ceSGarrett Wollman 991398dbb11SPeter Wemm static devclass_t fpupnp_devclass; 99254f1d0ceSGarrett Wollman 993398dbb11SPeter Wemm DRIVER_MODULE(fpupnp, acpi, fpupnp_driver, fpupnp_devclass, 0, 0); 994d6b66397SWarner Losh ISA_PNP_INFO(fpupnp_ids); 995586079ccSBruce Evans #endif /* DEV_ISA */ 9966cf9a08dSKonstantin Belousov 9978c6f8f3dSKonstantin Belousov static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx", 9988c6f8f3dSKonstantin Belousov "Kernel contexts for FPU state"); 9998c6f8f3dSKonstantin Belousov 10008c6f8f3dSKonstantin Belousov #define FPU_KERN_CTX_FPUINITDONE 0x01 1001633034feSKonstantin Belousov #define FPU_KERN_CTX_DUMMY 0x02 /* avoided save for the kern thread */ 1002e808e13bSJohn-Mark Gurney #define FPU_KERN_CTX_INUSE 0x04 10038c6f8f3dSKonstantin Belousov 10048c6f8f3dSKonstantin Belousov struct fpu_kern_ctx { 10058c6f8f3dSKonstantin Belousov struct savefpu *prev; 10068c6f8f3dSKonstantin Belousov uint32_t flags; 10078c6f8f3dSKonstantin Belousov char hwstate1[]; 10088c6f8f3dSKonstantin Belousov }; 10098c6f8f3dSKonstantin Belousov 10108c6f8f3dSKonstantin Belousov struct fpu_kern_ctx * 10118c6f8f3dSKonstantin Belousov fpu_kern_alloc_ctx(u_int flags) 10128c6f8f3dSKonstantin Belousov { 10138c6f8f3dSKonstantin Belousov struct fpu_kern_ctx *res; 10148c6f8f3dSKonstantin Belousov size_t sz; 10158c6f8f3dSKonstantin Belousov 10168c6f8f3dSKonstantin Belousov sz = sizeof(struct fpu_kern_ctx) + XSAVE_AREA_ALIGN + 10178c6f8f3dSKonstantin Belousov cpu_max_ext_state_size; 10188c6f8f3dSKonstantin Belousov res = malloc(sz, M_FPUKERN_CTX, ((flags & FPU_KERN_NOWAIT) ? 10198c6f8f3dSKonstantin Belousov M_NOWAIT : M_WAITOK) | M_ZERO); 10208c6f8f3dSKonstantin Belousov return (res); 10218c6f8f3dSKonstantin Belousov } 10228c6f8f3dSKonstantin Belousov 10238c6f8f3dSKonstantin Belousov void 10248c6f8f3dSKonstantin Belousov fpu_kern_free_ctx(struct fpu_kern_ctx *ctx) 10258c6f8f3dSKonstantin Belousov { 10268c6f8f3dSKonstantin Belousov 1027e808e13bSJohn-Mark Gurney KASSERT((ctx->flags & FPU_KERN_CTX_INUSE) == 0, ("free'ing inuse ctx")); 10288c6f8f3dSKonstantin Belousov /* XXXKIB clear the memory ? */ 10298c6f8f3dSKonstantin Belousov free(ctx, M_FPUKERN_CTX); 10308c6f8f3dSKonstantin Belousov } 10318c6f8f3dSKonstantin Belousov 10328c6f8f3dSKonstantin Belousov static struct savefpu * 10338c6f8f3dSKonstantin Belousov fpu_kern_ctx_savefpu(struct fpu_kern_ctx *ctx) 10348c6f8f3dSKonstantin Belousov { 10358c6f8f3dSKonstantin Belousov vm_offset_t p; 10368c6f8f3dSKonstantin Belousov 10378c6f8f3dSKonstantin Belousov p = (vm_offset_t)&ctx->hwstate1; 10388c6f8f3dSKonstantin Belousov p = roundup2(p, XSAVE_AREA_ALIGN); 10398c6f8f3dSKonstantin Belousov return ((struct savefpu *)p); 10408c6f8f3dSKonstantin Belousov } 10418c6f8f3dSKonstantin Belousov 1042849ce31aSConrad Meyer void 10436cf9a08dSKonstantin Belousov fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags) 10446cf9a08dSKonstantin Belousov { 10456cf9a08dSKonstantin Belousov struct pcb *pcb; 10466cf9a08dSKonstantin Belousov 1047cf1c4776SKonstantin Belousov pcb = td->td_pcb; 1048cf1c4776SKonstantin Belousov KASSERT((flags & FPU_KERN_NOCTX) != 0 || ctx != NULL, 1049cf1c4776SKonstantin Belousov ("ctx is required when !FPU_KERN_NOCTX")); 1050cf1c4776SKonstantin Belousov KASSERT(ctx == NULL || (ctx->flags & FPU_KERN_CTX_INUSE) == 0, 1051cf1c4776SKonstantin Belousov ("using inuse ctx")); 1052cf1c4776SKonstantin Belousov KASSERT((pcb->pcb_flags & PCB_FPUNOSAVE) == 0, 1053cf1c4776SKonstantin Belousov ("recursive fpu_kern_enter while in PCB_FPUNOSAVE state")); 1054e808e13bSJohn-Mark Gurney 1055cf1c4776SKonstantin Belousov if ((flags & FPU_KERN_NOCTX) != 0) { 1056cf1c4776SKonstantin Belousov critical_enter(); 1057cf1c4776SKonstantin Belousov stop_emulating(); 1058cf1c4776SKonstantin Belousov if (curthread == PCPU_GET(fpcurthread)) { 1059cf1c4776SKonstantin Belousov fpusave(curpcb->pcb_save); 1060cf1c4776SKonstantin Belousov PCPU_SET(fpcurthread, NULL); 1061cf1c4776SKonstantin Belousov } else { 1062cf1c4776SKonstantin Belousov KASSERT(PCPU_GET(fpcurthread) == NULL, 1063cf1c4776SKonstantin Belousov ("invalid fpcurthread")); 1064cf1c4776SKonstantin Belousov } 1065cf1c4776SKonstantin Belousov 1066cf1c4776SKonstantin Belousov /* 1067cf1c4776SKonstantin Belousov * This breaks XSAVEOPT tracker, but 1068cf1c4776SKonstantin Belousov * PCB_FPUNOSAVE state is supposed to never need to 1069cf1c4776SKonstantin Belousov * save FPU context at all. 1070cf1c4776SKonstantin Belousov */ 1071cf1c4776SKonstantin Belousov fpurestore(fpu_initialstate); 1072cf1c4776SKonstantin Belousov set_pcb_flags(pcb, PCB_KERNFPU | PCB_FPUNOSAVE | 1073cf1c4776SKonstantin Belousov PCB_FPUINITDONE); 1074849ce31aSConrad Meyer return; 1075cf1c4776SKonstantin Belousov } 1076633034feSKonstantin Belousov if ((flags & FPU_KERN_KTHR) != 0 && is_fpu_kern_thread(0)) { 1077e808e13bSJohn-Mark Gurney ctx->flags = FPU_KERN_CTX_DUMMY | FPU_KERN_CTX_INUSE; 1078849ce31aSConrad Meyer return; 1079633034feSKonstantin Belousov } 10808c6f8f3dSKonstantin Belousov KASSERT(!PCB_USER_FPU(pcb) || pcb->pcb_save == 10818c6f8f3dSKonstantin Belousov get_pcb_user_save_pcb(pcb), ("mangled pcb_save")); 1082e808e13bSJohn-Mark Gurney ctx->flags = FPU_KERN_CTX_INUSE; 10836cf9a08dSKonstantin Belousov if ((pcb->pcb_flags & PCB_FPUINITDONE) != 0) 10846cf9a08dSKonstantin Belousov ctx->flags |= FPU_KERN_CTX_FPUINITDONE; 10856cf9a08dSKonstantin Belousov fpuexit(td); 10866cf9a08dSKonstantin Belousov ctx->prev = pcb->pcb_save; 10878c6f8f3dSKonstantin Belousov pcb->pcb_save = fpu_kern_ctx_savefpu(ctx); 1088e6c006d9SJung-uk Kim set_pcb_flags(pcb, PCB_KERNFPU); 1089e6c006d9SJung-uk Kim clear_pcb_flags(pcb, PCB_FPUINITDONE); 1090849ce31aSConrad Meyer return; 10916cf9a08dSKonstantin Belousov } 10926cf9a08dSKonstantin Belousov 10936cf9a08dSKonstantin Belousov int 10946cf9a08dSKonstantin Belousov fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx) 10956cf9a08dSKonstantin Belousov { 10966cf9a08dSKonstantin Belousov struct pcb *pcb; 10976cf9a08dSKonstantin Belousov 1098cf1c4776SKonstantin Belousov pcb = td->td_pcb; 1099cf1c4776SKonstantin Belousov 1100cf1c4776SKonstantin Belousov if ((pcb->pcb_flags & PCB_FPUNOSAVE) != 0) { 1101cf1c4776SKonstantin Belousov KASSERT(ctx == NULL, ("non-null ctx after FPU_KERN_NOCTX")); 1102cf1c4776SKonstantin Belousov KASSERT(PCPU_GET(fpcurthread) == NULL, 1103cf1c4776SKonstantin Belousov ("non-NULL fpcurthread for PCB_FPUNOSAVE")); 1104cf1c4776SKonstantin Belousov CRITICAL_ASSERT(td); 1105cf1c4776SKonstantin Belousov 1106cf1c4776SKonstantin Belousov clear_pcb_flags(pcb, PCB_FPUNOSAVE | PCB_FPUINITDONE); 1107cf1c4776SKonstantin Belousov start_emulating(); 1108cf1c4776SKonstantin Belousov critical_exit(); 1109cf1c4776SKonstantin Belousov } else { 1110e808e13bSJohn-Mark Gurney KASSERT((ctx->flags & FPU_KERN_CTX_INUSE) != 0, 1111e808e13bSJohn-Mark Gurney ("leaving not inuse ctx")); 1112e808e13bSJohn-Mark Gurney ctx->flags &= ~FPU_KERN_CTX_INUSE; 1113e808e13bSJohn-Mark Gurney 1114cf1c4776SKonstantin Belousov if (is_fpu_kern_thread(0) && 1115cf1c4776SKonstantin Belousov (ctx->flags & FPU_KERN_CTX_DUMMY) != 0) 1116633034feSKonstantin Belousov return (0); 1117cf1c4776SKonstantin Belousov KASSERT((ctx->flags & FPU_KERN_CTX_DUMMY) == 0, 1118cf1c4776SKonstantin Belousov ("dummy ctx")); 111999753495SKonstantin Belousov critical_enter(); 11206cf9a08dSKonstantin Belousov if (curthread == PCPU_GET(fpcurthread)) 11216cf9a08dSKonstantin Belousov fpudrop(); 112299753495SKonstantin Belousov critical_exit(); 11236cf9a08dSKonstantin Belousov pcb->pcb_save = ctx->prev; 1124cf1c4776SKonstantin Belousov } 1125cf1c4776SKonstantin Belousov 11268c6f8f3dSKonstantin Belousov if (pcb->pcb_save == get_pcb_user_save_pcb(pcb)) { 1127e6c006d9SJung-uk Kim if ((pcb->pcb_flags & PCB_USERFPUINITDONE) != 0) { 1128e6c006d9SJung-uk Kim set_pcb_flags(pcb, PCB_FPUINITDONE); 1129e6c006d9SJung-uk Kim clear_pcb_flags(pcb, PCB_KERNFPU); 1130e6c006d9SJung-uk Kim } else 1131e6c006d9SJung-uk Kim clear_pcb_flags(pcb, PCB_FPUINITDONE | PCB_KERNFPU); 11326cf9a08dSKonstantin Belousov } else { 11336cf9a08dSKonstantin Belousov if ((ctx->flags & FPU_KERN_CTX_FPUINITDONE) != 0) 1134e6c006d9SJung-uk Kim set_pcb_flags(pcb, PCB_FPUINITDONE); 11356cf9a08dSKonstantin Belousov else 1136e6c006d9SJung-uk Kim clear_pcb_flags(pcb, PCB_FPUINITDONE); 11376cf9a08dSKonstantin Belousov KASSERT(!PCB_USER_FPU(pcb), ("unpaired fpu_kern_leave")); 11386cf9a08dSKonstantin Belousov } 11396cf9a08dSKonstantin Belousov return (0); 11406cf9a08dSKonstantin Belousov } 11416cf9a08dSKonstantin Belousov 11426cf9a08dSKonstantin Belousov int 11436cf9a08dSKonstantin Belousov fpu_kern_thread(u_int flags) 11446cf9a08dSKonstantin Belousov { 11456cf9a08dSKonstantin Belousov 11466cf9a08dSKonstantin Belousov KASSERT((curthread->td_pflags & TDP_KTHREAD) != 0, 11476cf9a08dSKonstantin Belousov ("Only kthread may use fpu_kern_thread")); 11481965c139SKonstantin Belousov KASSERT(curpcb->pcb_save == get_pcb_user_save_pcb(curpcb), 11498c6f8f3dSKonstantin Belousov ("mangled pcb_save")); 11501965c139SKonstantin Belousov KASSERT(PCB_USER_FPU(curpcb), ("recursive call")); 11516cf9a08dSKonstantin Belousov 11521965c139SKonstantin Belousov set_pcb_flags(curpcb, PCB_KERNFPU); 11536cf9a08dSKonstantin Belousov return (0); 11546cf9a08dSKonstantin Belousov } 11556cf9a08dSKonstantin Belousov 11566cf9a08dSKonstantin Belousov int 11576cf9a08dSKonstantin Belousov is_fpu_kern_thread(u_int flags) 11586cf9a08dSKonstantin Belousov { 11596cf9a08dSKonstantin Belousov 11606cf9a08dSKonstantin Belousov if ((curthread->td_pflags & TDP_KTHREAD) == 0) 11616cf9a08dSKonstantin Belousov return (0); 116283b22b05SKonstantin Belousov return ((curpcb->pcb_flags & PCB_KERNFPU) != 0); 11636cf9a08dSKonstantin Belousov } 11642741efecSPeter Grehan 11652741efecSPeter Grehan /* 11662741efecSPeter Grehan * FPU save area alloc/free/init utility routines 11672741efecSPeter Grehan */ 11682741efecSPeter Grehan struct savefpu * 11692741efecSPeter Grehan fpu_save_area_alloc(void) 11702741efecSPeter Grehan { 11712741efecSPeter Grehan 11722741efecSPeter Grehan return (uma_zalloc(fpu_save_area_zone, 0)); 11732741efecSPeter Grehan } 11742741efecSPeter Grehan 11752741efecSPeter Grehan void 11762741efecSPeter Grehan fpu_save_area_free(struct savefpu *fsa) 11772741efecSPeter Grehan { 11782741efecSPeter Grehan 11792741efecSPeter Grehan uma_zfree(fpu_save_area_zone, fsa); 11802741efecSPeter Grehan } 11812741efecSPeter Grehan 11822741efecSPeter Grehan void 11832741efecSPeter Grehan fpu_save_area_reset(struct savefpu *fsa) 11842741efecSPeter Grehan { 11852741efecSPeter Grehan 11862741efecSPeter Grehan bcopy(fpu_initialstate, fsa, cpu_max_ext_state_size); 11872741efecSPeter Grehan } 1188