2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990 William Jolitz.
5 * Copyright (c) 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)npx.c 7.2 (Berkeley) 5/12/91
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/domainset.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/sysent.h>
51 #include <machine/bus.h>
53 #include <sys/signalvar.h>
56 #include <machine/cputypes.h>
57 #include <machine/frame.h>
58 #include <machine/intr_machdep.h>
59 #include <machine/md_var.h>
60 #include <machine/pcb.h>
61 #include <machine/psl.h>
62 #include <machine/resource.h>
63 #include <machine/specialreg.h>
64 #include <machine/segments.h>
65 #include <machine/ucontext.h>
66 #include <x86/ifunc.h>
69 * Floating point support.
72 #define fldcw(cw) __asm __volatile("fldcw %0" : : "m" (cw))
73 #define fnclex() __asm __volatile("fnclex")
74 #define fninit() __asm __volatile("fninit")
75 #define fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr)))
76 #define fnstsw(addr) __asm __volatile("fnstsw %0" : "=am" (*(addr)))
77 #define fxrstor(addr) __asm __volatile("fxrstor %0" : : "m" (*(addr)))
78 #define fxsave(addr) __asm __volatile("fxsave %0" : "=m" (*(addr)))
79 #define ldmxcsr(csr) __asm __volatile("ldmxcsr %0" : : "m" (csr))
80 #define stmxcsr(addr) __asm __volatile("stmxcsr %0" : "=m" (*(addr)))
83 xrstor32(char *addr, uint64_t mask)
89 __asm __volatile("xrstor %0" : : "m" (*addr), "a" (low), "d" (hi));
93 xrstor64(char *addr, uint64_t mask)
99 __asm __volatile("xrstor64 %0" : : "m" (*addr), "a" (low), "d" (hi));
103 xsave32(char *addr, uint64_t mask)
109 __asm __volatile("xsave %0" : "=m" (*addr) : "a" (low), "d" (hi) :
114 xsave64(char *addr, uint64_t mask)
120 __asm __volatile("xsave64 %0" : "=m" (*addr) : "a" (low), "d" (hi) :
125 xsaveopt32(char *addr, uint64_t mask)
131 __asm __volatile("xsaveopt %0" : "=m" (*addr) : "a" (low), "d" (hi) :
136 xsaveopt64(char *addr, uint64_t mask)
142 __asm __volatile("xsaveopt64 %0" : "=m" (*addr) : "a" (low), "d" (hi) :
146 #define start_emulating() load_cr0(rcr0() | CR0_TS)
147 #define stop_emulating() clts()
149 CTASSERT(sizeof(struct savefpu) == 512);
150 CTASSERT(sizeof(struct xstate_hdr) == 64);
151 CTASSERT(sizeof(struct savefpu_ymm) == 832);
154 * This requirement is to make it easier for asm code to calculate
155 * offset of the fpu save area from the pcb address. FPU save area
156 * must be 64-byte aligned.
158 CTASSERT(sizeof(struct pcb) % XSAVE_AREA_ALIGN == 0);
161 * Ensure the copy of XCR0 saved in a core is contained in the padding
164 CTASSERT(X86_XSTATE_XCR0_OFFSET >= offsetof(struct savefpu, sv_pad) &&
165 X86_XSTATE_XCR0_OFFSET + sizeof(uint64_t) <= sizeof(struct savefpu));
167 static void fpu_clean_state(void);
169 SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
170 SYSCTL_NULL_INT_PTR, 1, "Floating point instructions executed in hardware");
172 int use_xsave; /* non-static for cpu_switch.S */
173 uint64_t xsave_mask; /* the same */
174 static uma_zone_t fpu_save_area_zone;
175 static struct savefpu *fpu_initialstate;
177 static struct xsave_area_elm_descr {
183 fpusave_xsaveopt64(void *addr)
185 xsaveopt64((char *)addr, xsave_mask);
189 fpusave_xsaveopt3264(void *addr)
191 if (SV_CURPROC_FLAG(SV_ILP32))
192 xsaveopt32((char *)addr, xsave_mask);
194 xsaveopt64((char *)addr, xsave_mask);
198 fpusave_xsave64(void *addr)
200 xsave64((char *)addr, xsave_mask);
204 fpusave_xsave3264(void *addr)
206 if (SV_CURPROC_FLAG(SV_ILP32))
207 xsave32((char *)addr, xsave_mask);
209 xsave64((char *)addr, xsave_mask);
213 fpurestore_xrstor64(void *addr)
215 xrstor64((char *)addr, xsave_mask);
219 fpurestore_xrstor3264(void *addr)
221 if (SV_CURPROC_FLAG(SV_ILP32))
222 xrstor32((char *)addr, xsave_mask);
224 xrstor64((char *)addr, xsave_mask);
228 fpusave_fxsave(void *addr)
231 fxsave((char *)addr);
235 fpurestore_fxrstor(void *addr)
238 fxrstor((char *)addr);
247 if ((cpu_feature2 & CPUID2_XSAVE) == 0)
250 TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave);
253 DEFINE_IFUNC(, void, fpusave, (void *))
258 return (fpusave_fxsave);
259 if ((cpu_stdext_feature & CPUID_EXTSTATE_XSAVEOPT) != 0) {
260 return ((cpu_stdext_feature & CPUID_STDEXT_NFPUSG) != 0 ?
261 fpusave_xsaveopt64 : fpusave_xsaveopt3264);
263 return ((cpu_stdext_feature & CPUID_STDEXT_NFPUSG) != 0 ?
264 fpusave_xsave64 : fpusave_xsave3264);
267 DEFINE_IFUNC(, void, fpurestore, (void *))
272 return (fpurestore_fxrstor);
273 return ((cpu_stdext_feature & CPUID_STDEXT_NFPUSG) != 0 ?
274 fpurestore_xrstor64 : fpurestore_xrstor3264);
278 fpususpend(void *addr)
289 fpuresume(void *addr)
297 load_xcr(XCR0, xsave_mask);
303 * Enable XSAVE if supported and allowed by user.
304 * Calculate the xsave_mask.
310 uint64_t xsave_mask_user;
315 cpuid_count(0xd, 0x0, cp);
316 xsave_mask = XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
317 if ((cp[0] & xsave_mask) != xsave_mask)
318 panic("CPU0 does not support X87 or SSE: %x", cp[0]);
319 xsave_mask = ((uint64_t)cp[3] << 32) | cp[0];
320 xsave_mask_user = xsave_mask;
321 TUNABLE_ULONG_FETCH("hw.xsave_mask", &xsave_mask_user);
322 xsave_mask_user |= XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
323 xsave_mask &= xsave_mask_user;
324 if ((xsave_mask & XFEATURE_AVX512) != XFEATURE_AVX512)
325 xsave_mask &= ~XFEATURE_AVX512;
326 if ((xsave_mask & XFEATURE_MPX) != XFEATURE_MPX)
327 xsave_mask &= ~XFEATURE_MPX;
329 cpuid_count(0xd, 0x1, cp);
330 if ((cp[0] & CPUID_EXTSTATE_XSAVEOPT) != 0) {
332 * Patch the XSAVE instruction in the cpu_switch code
333 * to XSAVEOPT. We assume that XSAVE encoding used
334 * REX byte, and set the bit 4 of the r/m byte.
336 * It seems that some BIOSes give control to the OS
337 * with CR0.WP already set, making the kernel text
338 * read-only before cpu_startup().
340 old_wp = disable_wp();
341 ctx_switch_xsave32[3] |= 0x10;
342 ctx_switch_xsave[3] |= 0x10;
348 * Calculate the fpu save area size.
356 cpuid_count(0xd, 0x0, cp);
357 cpu_max_ext_state_size = cp[1];
360 * Reload the cpu_feature2, since we enabled OSXSAVE.
363 cpu_feature2 = cp[2];
365 cpu_max_ext_state_size = sizeof(struct savefpu);
369 * Initialize the floating point unit.
386 * Revert enablement of PKRU if user disabled its
387 * saving on context switches by clearing the bit in
388 * the xsave mask. Also redundantly clear the bit in
389 * cpu_stdext_feature2 to prevent pmap from ever
390 * trying to set the page table bits.
392 if ((cpu_stdext_feature2 & CPUID_STDEXT2_PKU) != 0 &&
393 (xsave_mask & XFEATURE_ENABLED_PKRU) == 0) {
395 cpu_stdext_feature2 &= ~CPUID_STDEXT2_PKU;
398 load_cr4(cr4 | CR4_XSAVE);
399 load_xcr(XCR0, xsave_mask);
403 * XCR0 shall be set up before CPU can report the save area size.
409 * It is too early for critical_enter() to work on AP.
411 saveintr = intr_disable();
414 control = __INITIAL_FPUCW__;
416 mxcsr = __INITIAL_MXCSR__;
419 intr_restore(saveintr);
423 * On the boot CPU we generate a clean state that is used to
424 * initialize the floating point unit when it is first used by a
428 fpuinitstate(void *arg __unused)
432 int cp[4], i, max_ext_n;
434 /* Do potentially blocking operations before disabling interrupts. */
435 fpu_save_area_zone = uma_zcreate("FPU_save_area",
436 cpu_max_ext_state_size, NULL, NULL, NULL, NULL,
437 XSAVE_AREA_ALIGN - 1, 0);
438 fpu_initialstate = uma_zalloc(fpu_save_area_zone, M_WAITOK | M_ZERO);
440 max_ext_n = flsl(xsave_mask);
441 xsave_area_desc = malloc(max_ext_n * sizeof(struct
442 xsave_area_elm_descr), M_DEVBUF, M_WAITOK | M_ZERO);
445 cpu_thread_alloc(&thread0);
447 saveintr = intr_disable();
450 fpusave_fxsave(fpu_initialstate);
451 if (fpu_initialstate->sv_env.en_mxcsr_mask)
452 cpu_mxcsr_mask = fpu_initialstate->sv_env.en_mxcsr_mask;
454 cpu_mxcsr_mask = 0xFFBF;
457 * The fninit instruction does not modify XMM registers or x87
458 * registers (MM/ST). The fpusave call dumped the garbage
459 * contained in the registers after reset to the initial state
460 * saved. Clear XMM and x87 registers file image to make the
461 * startup program state and signal handler XMM/x87 register
462 * content predictable.
464 bzero(fpu_initialstate->sv_fp, sizeof(fpu_initialstate->sv_fp));
465 bzero(fpu_initialstate->sv_xmm, sizeof(fpu_initialstate->sv_xmm));
468 * Create a table describing the layout of the CPU Extended
469 * Save Area. See Intel SDM rev. 075 Vol. 1 13.4.1 "Legacy
470 * Region of an XSAVE Area" for the source of offsets/sizes.
473 xstate_bv = (uint64_t *)((char *)(fpu_initialstate + 1) +
474 offsetof(struct xstate_hdr, xstate_bv));
475 *xstate_bv = XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE;
478 xsave_area_desc[0].offset = 0;
479 xsave_area_desc[0].size = 160;
481 xsave_area_desc[1].offset = 160;
482 xsave_area_desc[1].size = 416 - 160;
484 for (i = 2; i < max_ext_n; i++) {
485 cpuid_count(0xd, i, cp);
486 xsave_area_desc[i].offset = cp[1];
487 xsave_area_desc[i].size = cp[0];
492 intr_restore(saveintr);
494 /* EFIRT needs this to be initialized before we can enter our EFI environment */
495 SYSINIT(fpuinitstate, SI_SUB_CPU, SI_ORDER_ANY, fpuinitstate, NULL);
498 * Free coprocessor (if we have it).
501 fpuexit(struct thread *td)
505 if (curthread == PCPU_GET(fpcurthread)) {
507 fpusave(curpcb->pcb_save);
509 PCPU_SET(fpcurthread, NULL);
518 return (_MC_FPFMT_XMM);
522 * The following mechanism is used to ensure that the FPE_... value
523 * that is passed as a trapcode to the signal handler of the user
524 * process does not have more than one bit set.
526 * Multiple bits may be set if the user process modifies the control
527 * word while a status word bit is already set. While this is a sign
528 * of bad coding, we have no choice than to narrow them down to one
529 * bit, since we must not send a trapcode that is not exactly one of
532 * The mechanism has a static table with 127 entries. Each combination
533 * of the 7 FPU status word exception bits directly translates to a
534 * position in this table, where a single FPE_... value is stored.
535 * This FPE_... value stored there is considered the "most important"
536 * of the exception bits and will be sent as the signal code. The
537 * precedence of the bits is based upon Intel Document "Numerical
538 * Applications", Chapter "Special Computational Situations".
540 * The macro to choose one of these values does these steps: 1) Throw
541 * away status word bits that cannot be masked. 2) Throw away the bits
542 * currently masked in the control word, assuming the user isn't
543 * interested in them anymore. 3) Reinsert status word bit 7 (stack
544 * fault) if it is set, which cannot be masked but must be presered.
545 * 4) Use the remaining bits to point into the trapcode table.
547 * The 6 maskable bits in order of their preference, as stated in the
548 * above referenced Intel manual:
549 * 1 Invalid operation (FP_X_INV)
552 * 1c Operand of unsupported format
554 * 2 QNaN operand (not an exception, irrelavant here)
555 * 3 Any other invalid-operation not mentioned above or zero divide
556 * (FP_X_INV, FP_X_DZ)
557 * 4 Denormal operand (FP_X_DNML)
558 * 5 Numeric over/underflow (FP_X_OFL, FP_X_UFL)
559 * 6 Inexact result (FP_X_IMP)
561 static char fpetable[128] = {
563 FPE_FLTINV, /* 1 - INV */
564 FPE_FLTUND, /* 2 - DNML */
565 FPE_FLTINV, /* 3 - INV | DNML */
566 FPE_FLTDIV, /* 4 - DZ */
567 FPE_FLTINV, /* 5 - INV | DZ */
568 FPE_FLTDIV, /* 6 - DNML | DZ */
569 FPE_FLTINV, /* 7 - INV | DNML | DZ */
570 FPE_FLTOVF, /* 8 - OFL */
571 FPE_FLTINV, /* 9 - INV | OFL */
572 FPE_FLTUND, /* A - DNML | OFL */
573 FPE_FLTINV, /* B - INV | DNML | OFL */
574 FPE_FLTDIV, /* C - DZ | OFL */
575 FPE_FLTINV, /* D - INV | DZ | OFL */
576 FPE_FLTDIV, /* E - DNML | DZ | OFL */
577 FPE_FLTINV, /* F - INV | DNML | DZ | OFL */
578 FPE_FLTUND, /* 10 - UFL */
579 FPE_FLTINV, /* 11 - INV | UFL */
580 FPE_FLTUND, /* 12 - DNML | UFL */
581 FPE_FLTINV, /* 13 - INV | DNML | UFL */
582 FPE_FLTDIV, /* 14 - DZ | UFL */
583 FPE_FLTINV, /* 15 - INV | DZ | UFL */
584 FPE_FLTDIV, /* 16 - DNML | DZ | UFL */
585 FPE_FLTINV, /* 17 - INV | DNML | DZ | UFL */
586 FPE_FLTOVF, /* 18 - OFL | UFL */
587 FPE_FLTINV, /* 19 - INV | OFL | UFL */
588 FPE_FLTUND, /* 1A - DNML | OFL | UFL */
589 FPE_FLTINV, /* 1B - INV | DNML | OFL | UFL */
590 FPE_FLTDIV, /* 1C - DZ | OFL | UFL */
591 FPE_FLTINV, /* 1D - INV | DZ | OFL | UFL */
592 FPE_FLTDIV, /* 1E - DNML | DZ | OFL | UFL */
593 FPE_FLTINV, /* 1F - INV | DNML | DZ | OFL | UFL */
594 FPE_FLTRES, /* 20 - IMP */
595 FPE_FLTINV, /* 21 - INV | IMP */
596 FPE_FLTUND, /* 22 - DNML | IMP */
597 FPE_FLTINV, /* 23 - INV | DNML | IMP */
598 FPE_FLTDIV, /* 24 - DZ | IMP */
599 FPE_FLTINV, /* 25 - INV | DZ | IMP */
600 FPE_FLTDIV, /* 26 - DNML | DZ | IMP */
601 FPE_FLTINV, /* 27 - INV | DNML | DZ | IMP */
602 FPE_FLTOVF, /* 28 - OFL | IMP */
603 FPE_FLTINV, /* 29 - INV | OFL | IMP */
604 FPE_FLTUND, /* 2A - DNML | OFL | IMP */
605 FPE_FLTINV, /* 2B - INV | DNML | OFL | IMP */
606 FPE_FLTDIV, /* 2C - DZ | OFL | IMP */
607 FPE_FLTINV, /* 2D - INV | DZ | OFL | IMP */
608 FPE_FLTDIV, /* 2E - DNML | DZ | OFL | IMP */
609 FPE_FLTINV, /* 2F - INV | DNML | DZ | OFL | IMP */
610 FPE_FLTUND, /* 30 - UFL | IMP */
611 FPE_FLTINV, /* 31 - INV | UFL | IMP */
612 FPE_FLTUND, /* 32 - DNML | UFL | IMP */
613 FPE_FLTINV, /* 33 - INV | DNML | UFL | IMP */
614 FPE_FLTDIV, /* 34 - DZ | UFL | IMP */
615 FPE_FLTINV, /* 35 - INV | DZ | UFL | IMP */
616 FPE_FLTDIV, /* 36 - DNML | DZ | UFL | IMP */
617 FPE_FLTINV, /* 37 - INV | DNML | DZ | UFL | IMP */
618 FPE_FLTOVF, /* 38 - OFL | UFL | IMP */
619 FPE_FLTINV, /* 39 - INV | OFL | UFL | IMP */
620 FPE_FLTUND, /* 3A - DNML | OFL | UFL | IMP */
621 FPE_FLTINV, /* 3B - INV | DNML | OFL | UFL | IMP */
622 FPE_FLTDIV, /* 3C - DZ | OFL | UFL | IMP */
623 FPE_FLTINV, /* 3D - INV | DZ | OFL | UFL | IMP */
624 FPE_FLTDIV, /* 3E - DNML | DZ | OFL | UFL | IMP */
625 FPE_FLTINV, /* 3F - INV | DNML | DZ | OFL | UFL | IMP */
626 FPE_FLTSUB, /* 40 - STK */
627 FPE_FLTSUB, /* 41 - INV | STK */
628 FPE_FLTUND, /* 42 - DNML | STK */
629 FPE_FLTSUB, /* 43 - INV | DNML | STK */
630 FPE_FLTDIV, /* 44 - DZ | STK */
631 FPE_FLTSUB, /* 45 - INV | DZ | STK */
632 FPE_FLTDIV, /* 46 - DNML | DZ | STK */
633 FPE_FLTSUB, /* 47 - INV | DNML | DZ | STK */
634 FPE_FLTOVF, /* 48 - OFL | STK */
635 FPE_FLTSUB, /* 49 - INV | OFL | STK */
636 FPE_FLTUND, /* 4A - DNML | OFL | STK */
637 FPE_FLTSUB, /* 4B - INV | DNML | OFL | STK */
638 FPE_FLTDIV, /* 4C - DZ | OFL | STK */
639 FPE_FLTSUB, /* 4D - INV | DZ | OFL | STK */
640 FPE_FLTDIV, /* 4E - DNML | DZ | OFL | STK */
641 FPE_FLTSUB, /* 4F - INV | DNML | DZ | OFL | STK */
642 FPE_FLTUND, /* 50 - UFL | STK */
643 FPE_FLTSUB, /* 51 - INV | UFL | STK */
644 FPE_FLTUND, /* 52 - DNML | UFL | STK */
645 FPE_FLTSUB, /* 53 - INV | DNML | UFL | STK */
646 FPE_FLTDIV, /* 54 - DZ | UFL | STK */
647 FPE_FLTSUB, /* 55 - INV | DZ | UFL | STK */
648 FPE_FLTDIV, /* 56 - DNML | DZ | UFL | STK */
649 FPE_FLTSUB, /* 57 - INV | DNML | DZ | UFL | STK */
650 FPE_FLTOVF, /* 58 - OFL | UFL | STK */
651 FPE_FLTSUB, /* 59 - INV | OFL | UFL | STK */
652 FPE_FLTUND, /* 5A - DNML | OFL | UFL | STK */
653 FPE_FLTSUB, /* 5B - INV | DNML | OFL | UFL | STK */
654 FPE_FLTDIV, /* 5C - DZ | OFL | UFL | STK */
655 FPE_FLTSUB, /* 5D - INV | DZ | OFL | UFL | STK */
656 FPE_FLTDIV, /* 5E - DNML | DZ | OFL | UFL | STK */
657 FPE_FLTSUB, /* 5F - INV | DNML | DZ | OFL | UFL | STK */
658 FPE_FLTRES, /* 60 - IMP | STK */
659 FPE_FLTSUB, /* 61 - INV | IMP | STK */
660 FPE_FLTUND, /* 62 - DNML | IMP | STK */
661 FPE_FLTSUB, /* 63 - INV | DNML | IMP | STK */
662 FPE_FLTDIV, /* 64 - DZ | IMP | STK */
663 FPE_FLTSUB, /* 65 - INV | DZ | IMP | STK */
664 FPE_FLTDIV, /* 66 - DNML | DZ | IMP | STK */
665 FPE_FLTSUB, /* 67 - INV | DNML | DZ | IMP | STK */
666 FPE_FLTOVF, /* 68 - OFL | IMP | STK */
667 FPE_FLTSUB, /* 69 - INV | OFL | IMP | STK */
668 FPE_FLTUND, /* 6A - DNML | OFL | IMP | STK */
669 FPE_FLTSUB, /* 6B - INV | DNML | OFL | IMP | STK */
670 FPE_FLTDIV, /* 6C - DZ | OFL | IMP | STK */
671 FPE_FLTSUB, /* 6D - INV | DZ | OFL | IMP | STK */
672 FPE_FLTDIV, /* 6E - DNML | DZ | OFL | IMP | STK */
673 FPE_FLTSUB, /* 6F - INV | DNML | DZ | OFL | IMP | STK */
674 FPE_FLTUND, /* 70 - UFL | IMP | STK */
675 FPE_FLTSUB, /* 71 - INV | UFL | IMP | STK */
676 FPE_FLTUND, /* 72 - DNML | UFL | IMP | STK */
677 FPE_FLTSUB, /* 73 - INV | DNML | UFL | IMP | STK */
678 FPE_FLTDIV, /* 74 - DZ | UFL | IMP | STK */
679 FPE_FLTSUB, /* 75 - INV | DZ | UFL | IMP | STK */
680 FPE_FLTDIV, /* 76 - DNML | DZ | UFL | IMP | STK */
681 FPE_FLTSUB, /* 77 - INV | DNML | DZ | UFL | IMP | STK */
682 FPE_FLTOVF, /* 78 - OFL | UFL | IMP | STK */
683 FPE_FLTSUB, /* 79 - INV | OFL | UFL | IMP | STK */
684 FPE_FLTUND, /* 7A - DNML | OFL | UFL | IMP | STK */
685 FPE_FLTSUB, /* 7B - INV | DNML | OFL | UFL | IMP | STK */
686 FPE_FLTDIV, /* 7C - DZ | OFL | UFL | IMP | STK */
687 FPE_FLTSUB, /* 7D - INV | DZ | OFL | UFL | IMP | STK */
688 FPE_FLTDIV, /* 7E - DNML | DZ | OFL | UFL | IMP | STK */
689 FPE_FLTSUB, /* 7F - INV | DNML | DZ | OFL | UFL | IMP | STK */
693 * Read the FP status and control words, then generate si_code value
694 * for SIGFPE. The error code chosen will be one of the
695 * FPE_... macros. It will be sent as the second argument to old
696 * BSD-style signal handlers and as "siginfo_t->si_code" (second
697 * argument) to SA_SIGINFO signal handlers.
699 * Some time ago, we cleared the x87 exceptions with FNCLEX there.
700 * Clearing exceptions was necessary mainly to avoid IRQ13 bugs. The
701 * usermode code which understands the FPU hardware enough to enable
702 * the exceptions, can also handle clearing the exception state in the
703 * handler. The only consequence of not clearing the exception is the
704 * rethrow of the SIGFPE on return from the signal handler and
705 * reexecution of the corresponding instruction.
707 * For XMM traps, the exceptions were never cleared.
712 struct savefpu *pcb_save;
713 u_short control, status;
718 * Interrupt handling (for another interrupt) may have pushed the
719 * state to memory. Fetch the relevant parts of the state from
722 if (PCPU_GET(fpcurthread) != curthread) {
723 pcb_save = curpcb->pcb_save;
724 control = pcb_save->sv_env.en_cw;
725 status = pcb_save->sv_env.en_sw;
732 return (fpetable[status & ((~control & 0x3f) | 0x40)]);
741 if (PCPU_GET(fpcurthread) != curthread)
742 mxcsr = curpcb->pcb_save->sv_env.en_mxcsr;
746 return (fpetable[(mxcsr & (~mxcsr >> 7)) & 0x3f]);
750 restore_fpu_curthread(struct thread *td)
755 * Record new context early in case frstor causes a trap.
757 PCPU_SET(fpcurthread, td);
763 if ((pcb->pcb_flags & PCB_FPUINITDONE) == 0) {
765 * This is the first time this thread has used the FPU or
766 * the PCB doesn't contain a clean FPU state. Explicitly
767 * load an initial state.
769 * We prefer to restore the state from the actual save
770 * area in PCB instead of directly loading from
771 * fpu_initialstate, to ignite the XSAVEOPT
774 bcopy(fpu_initialstate, pcb->pcb_save,
775 cpu_max_ext_state_size);
776 fpurestore(pcb->pcb_save);
777 if (pcb->pcb_initial_fpucw != __INITIAL_FPUCW__)
778 fldcw(pcb->pcb_initial_fpucw);
779 if (PCB_USER_FPU(pcb))
780 set_pcb_flags(pcb, PCB_FPUINITDONE |
781 PCB_USERFPUINITDONE);
783 set_pcb_flags(pcb, PCB_FPUINITDONE);
785 fpurestore(pcb->pcb_save);
789 * Device Not Available (DNA, #NM) exception handler.
791 * It would be better to switch FP context here (if curthread !=
792 * fpcurthread) and not necessarily for every context switch, but it
793 * is too hard to access foreign pcb's.
802 * This handler is entered with interrupts enabled, so context
803 * switches may occur before critical_enter() is executed. If
804 * a context switch occurs, then when we regain control, our
805 * state will have been completely restored. The CPU may
806 * change underneath us, but the only part of our context that
807 * lives in the CPU is CR0.TS and that will be "restored" by
808 * setting it on the new CPU.
812 KASSERT((curpcb->pcb_flags & PCB_FPUNOSAVE) == 0,
813 ("fpudna while in fpu_kern_enter(FPU_KERN_NOCTX)"));
814 if (__predict_false(PCPU_GET(fpcurthread) == td)) {
816 * Some virtual machines seems to set %cr0.TS at
817 * arbitrary moments. Silently clear the TS bit
818 * regardless of the eager/lazy FPU context switch
823 if (__predict_false(PCPU_GET(fpcurthread) != NULL)) {
825 "fpudna: fpcurthread = %p (%d), curthread = %p (%d)\n",
826 PCPU_GET(fpcurthread),
827 PCPU_GET(fpcurthread)->td_tid, td, td->td_tid);
829 restore_fpu_curthread(td);
834 void fpu_activate_sw(struct thread *td); /* Called from the context switch */
836 fpu_activate_sw(struct thread *td)
839 if ((td->td_pflags & TDP_KTHREAD) != 0 || !PCB_USER_FPU(td->td_pcb)) {
840 PCPU_SET(fpcurthread, NULL);
842 } else if (PCPU_GET(fpcurthread) != td) {
843 restore_fpu_curthread(td);
852 td = PCPU_GET(fpcurthread);
853 KASSERT(td == curthread, ("fpudrop: fpcurthread != curthread"));
855 PCPU_SET(fpcurthread, NULL);
856 clear_pcb_flags(td->td_pcb, PCB_FPUINITDONE);
861 * Get the user state of the FPU into pcb->pcb_user_save without
862 * dropping ownership (if possible). It returns the FPU ownership
866 fpugetregs(struct thread *td)
869 uint64_t *xstate_bv, bit;
871 int max_ext_n, i, owned;
875 if ((pcb->pcb_flags & PCB_USERFPUINITDONE) == 0) {
876 bcopy(fpu_initialstate, get_pcb_user_save_pcb(pcb),
877 cpu_max_ext_state_size);
878 get_pcb_user_save_pcb(pcb)->sv_env.en_cw =
879 pcb->pcb_initial_fpucw;
882 return (_MC_FPOWNED_PCB);
884 if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) {
885 fpusave(get_pcb_user_save_pcb(pcb));
886 owned = _MC_FPOWNED_FPU;
888 owned = _MC_FPOWNED_PCB;
892 * Handle partially saved state.
894 sa = (char *)get_pcb_user_save_pcb(pcb);
895 xstate_bv = (uint64_t *)(sa + sizeof(struct savefpu) +
896 offsetof(struct xstate_hdr, xstate_bv));
897 max_ext_n = flsl(xsave_mask);
898 for (i = 0; i < max_ext_n; i++) {
900 if ((xsave_mask & bit) == 0 || (*xstate_bv & bit) != 0)
902 bcopy((char *)fpu_initialstate +
903 xsave_area_desc[i].offset,
904 sa + xsave_area_desc[i].offset,
905 xsave_area_desc[i].size);
914 fpuuserinited(struct thread *td)
920 if (PCB_USER_FPU(pcb))
922 PCB_FPUINITDONE | PCB_USERFPUINITDONE);
924 set_pcb_flags(pcb, PCB_FPUINITDONE);
928 fpusetxstate(struct thread *td, char *xfpustate, size_t xfpustate_size)
930 struct xstate_hdr *hdr, *ehdr;
934 /* XXXKIB should we clear all extended state in xstate_bv instead ? */
935 if (xfpustate == NULL)
940 len = xfpustate_size;
941 if (len < sizeof(struct xstate_hdr))
943 max_len = cpu_max_ext_state_size - sizeof(struct savefpu);
947 ehdr = (struct xstate_hdr *)xfpustate;
948 bv = ehdr->xstate_bv;
953 if (bv & ~xsave_mask)
956 hdr = (struct xstate_hdr *)(get_pcb_user_save_td(td) + 1);
959 bcopy(xfpustate + sizeof(struct xstate_hdr),
960 (char *)(hdr + 1), len - sizeof(struct xstate_hdr));
966 * Set the state of the FPU.
969 fpusetregs(struct thread *td, struct savefpu *addr, char *xfpustate,
970 size_t xfpustate_size)
975 addr->sv_env.en_mxcsr &= cpu_mxcsr_mask;
979 if (td == PCPU_GET(fpcurthread) && PCB_USER_FPU(pcb)) {
980 error = fpusetxstate(td, xfpustate, xfpustate_size);
982 bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr));
983 fpurestore(get_pcb_user_save_td(td));
984 set_pcb_flags(pcb, PCB_FPUINITDONE |
985 PCB_USERFPUINITDONE);
988 error = fpusetxstate(td, xfpustate, xfpustate_size);
990 bcopy(addr, get_pcb_user_save_td(td), sizeof(*addr));
999 * On AuthenticAMD processors, the fxrstor instruction does not restore
1000 * the x87's stored last instruction pointer, last data pointer, and last
1001 * opcode values, except in the rare case in which the exception summary
1002 * (ES) bit in the x87 status word is set to 1.
1004 * In order to avoid leaking this information across processes, we clean
1005 * these values by performing a dummy load before executing fxrstor().
1008 fpu_clean_state(void)
1010 static float dummy_variable = 0.0;
1014 * Clear the ES bit in the x87 status word if it is currently
1015 * set, in order to avoid causing a fault in the upcoming load.
1022 * Load the dummy variable into the x87 stack. This mangles
1023 * the x87 stack, but we don't care since we're about to call
1026 __asm __volatile("ffree %%st(7); flds %0" : : "m" (dummy_variable));
1030 * This really sucks. We want the acpi version only, but it requires
1031 * the isa_if.h file in order to get the definitions.
1033 #include "opt_isa.h"
1035 #include <isa/isavar.h>
1037 * This sucks up the legacy ISA support assignments from PNPBIOS/ACPI.
1039 static struct isa_pnp_id fpupnp_ids[] = {
1040 { 0x040cd041, "Legacy ISA coprocessor support" }, /* PNP0C04 */
1045 fpupnp_probe(device_t dev)
1049 result = ISA_PNP_PROBE(device_get_parent(dev), dev, fpupnp_ids);
1056 fpupnp_attach(device_t dev)
1062 static device_method_t fpupnp_methods[] = {
1063 /* Device interface */
1064 DEVMETHOD(device_probe, fpupnp_probe),
1065 DEVMETHOD(device_attach, fpupnp_attach),
1066 DEVMETHOD(device_detach, bus_generic_detach),
1067 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1068 DEVMETHOD(device_suspend, bus_generic_suspend),
1069 DEVMETHOD(device_resume, bus_generic_resume),
1073 static driver_t fpupnp_driver = {
1079 DRIVER_MODULE(fpupnp, acpi, fpupnp_driver, 0, 0);
1080 ISA_PNP_INFO(fpupnp_ids);
1081 #endif /* DEV_ISA */
1083 static MALLOC_DEFINE(M_FPUKERN_CTX, "fpukern_ctx",
1084 "Kernel contexts for FPU state");
1086 #define FPU_KERN_CTX_FPUINITDONE 0x01
1087 #define FPU_KERN_CTX_DUMMY 0x02 /* avoided save for the kern thread */
1088 #define FPU_KERN_CTX_INUSE 0x04
1090 struct fpu_kern_ctx {
1091 struct savefpu *prev;
1096 static inline size_t __pure2
1097 fpu_kern_alloc_sz(u_int max_est)
1099 return (sizeof(struct fpu_kern_ctx) + XSAVE_AREA_ALIGN + max_est);
1102 static inline int __pure2
1103 fpu_kern_malloc_flags(u_int fpflags)
1105 return (((fpflags & FPU_KERN_NOWAIT) ? M_NOWAIT : M_WAITOK) | M_ZERO);
1108 struct fpu_kern_ctx *
1109 fpu_kern_alloc_ctx_domain(int domain, u_int flags)
1111 return (malloc_domainset(fpu_kern_alloc_sz(cpu_max_ext_state_size),
1112 M_FPUKERN_CTX, DOMAINSET_PREF(domain),
1113 fpu_kern_malloc_flags(flags)));
1116 struct fpu_kern_ctx *
1117 fpu_kern_alloc_ctx(u_int flags)
1119 return (malloc(fpu_kern_alloc_sz(cpu_max_ext_state_size),
1120 M_FPUKERN_CTX, fpu_kern_malloc_flags(flags)));
1124 fpu_kern_free_ctx(struct fpu_kern_ctx *ctx)
1127 KASSERT((ctx->flags & FPU_KERN_CTX_INUSE) == 0, ("free'ing inuse ctx"));
1128 /* XXXKIB clear the memory ? */
1129 free(ctx, M_FPUKERN_CTX);
1132 static struct savefpu *
1133 fpu_kern_ctx_savefpu(struct fpu_kern_ctx *ctx)
1137 p = (vm_offset_t)&ctx->hwstate1;
1138 p = roundup2(p, XSAVE_AREA_ALIGN);
1139 return ((struct savefpu *)p);
1143 fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx, u_int flags)
1148 KASSERT((flags & FPU_KERN_NOCTX) != 0 || ctx != NULL,
1149 ("ctx is required when !FPU_KERN_NOCTX"));
1150 KASSERT(ctx == NULL || (ctx->flags & FPU_KERN_CTX_INUSE) == 0,
1151 ("using inuse ctx"));
1152 KASSERT((pcb->pcb_flags & PCB_FPUNOSAVE) == 0,
1153 ("recursive fpu_kern_enter while in PCB_FPUNOSAVE state"));
1155 if ((flags & FPU_KERN_NOCTX) != 0) {
1158 if (curthread == PCPU_GET(fpcurthread)) {
1159 fpusave(curpcb->pcb_save);
1160 PCPU_SET(fpcurthread, NULL);
1162 KASSERT(PCPU_GET(fpcurthread) == NULL,
1163 ("invalid fpcurthread"));
1167 * This breaks XSAVEOPT tracker, but
1168 * PCB_FPUNOSAVE state is supposed to never need to
1169 * save FPU context at all.
1171 fpurestore(fpu_initialstate);
1172 set_pcb_flags(pcb, PCB_KERNFPU | PCB_FPUNOSAVE |
1176 if ((flags & FPU_KERN_KTHR) != 0 && is_fpu_kern_thread(0)) {
1177 ctx->flags = FPU_KERN_CTX_DUMMY | FPU_KERN_CTX_INUSE;
1181 KASSERT(!PCB_USER_FPU(pcb) || pcb->pcb_save ==
1182 get_pcb_user_save_pcb(pcb), ("mangled pcb_save"));
1183 ctx->flags = FPU_KERN_CTX_INUSE;
1184 if ((pcb->pcb_flags & PCB_FPUINITDONE) != 0)
1185 ctx->flags |= FPU_KERN_CTX_FPUINITDONE;
1187 ctx->prev = pcb->pcb_save;
1188 pcb->pcb_save = fpu_kern_ctx_savefpu(ctx);
1189 set_pcb_flags(pcb, PCB_KERNFPU);
1190 clear_pcb_flags(pcb, PCB_FPUINITDONE);
1195 fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx)
1201 if ((pcb->pcb_flags & PCB_FPUNOSAVE) != 0) {
1202 KASSERT(ctx == NULL, ("non-null ctx after FPU_KERN_NOCTX"));
1203 KASSERT(PCPU_GET(fpcurthread) == NULL,
1204 ("non-NULL fpcurthread for PCB_FPUNOSAVE"));
1205 CRITICAL_ASSERT(td);
1207 clear_pcb_flags(pcb, PCB_FPUNOSAVE | PCB_FPUINITDONE);
1210 KASSERT((ctx->flags & FPU_KERN_CTX_INUSE) != 0,
1211 ("leaving not inuse ctx"));
1212 ctx->flags &= ~FPU_KERN_CTX_INUSE;
1214 if (is_fpu_kern_thread(0) &&
1215 (ctx->flags & FPU_KERN_CTX_DUMMY) != 0)
1217 KASSERT((ctx->flags & FPU_KERN_CTX_DUMMY) == 0,
1220 if (curthread == PCPU_GET(fpcurthread))
1222 pcb->pcb_save = ctx->prev;
1225 if (pcb->pcb_save == get_pcb_user_save_pcb(pcb)) {
1226 if ((pcb->pcb_flags & PCB_USERFPUINITDONE) != 0) {
1227 set_pcb_flags(pcb, PCB_FPUINITDONE);
1228 if ((pcb->pcb_flags & PCB_KERNFPU_THR) == 0)
1229 clear_pcb_flags(pcb, PCB_KERNFPU);
1230 } else if ((pcb->pcb_flags & PCB_KERNFPU_THR) == 0)
1231 clear_pcb_flags(pcb, PCB_FPUINITDONE | PCB_KERNFPU);
1233 if ((ctx->flags & FPU_KERN_CTX_FPUINITDONE) != 0)
1234 set_pcb_flags(pcb, PCB_FPUINITDONE);
1236 clear_pcb_flags(pcb, PCB_FPUINITDONE);
1237 KASSERT(!PCB_USER_FPU(pcb), ("unpaired fpu_kern_leave"));
1244 fpu_kern_thread(u_int flags)
1247 KASSERT((curthread->td_pflags & TDP_KTHREAD) != 0,
1248 ("Only kthread may use fpu_kern_thread"));
1249 KASSERT(curpcb->pcb_save == get_pcb_user_save_pcb(curpcb),
1250 ("mangled pcb_save"));
1251 KASSERT(PCB_USER_FPU(curpcb), ("recursive call"));
1253 set_pcb_flags(curpcb, PCB_KERNFPU | PCB_KERNFPU_THR);
1258 is_fpu_kern_thread(u_int flags)
1261 if ((curthread->td_pflags & TDP_KTHREAD) == 0)
1263 return ((curpcb->pcb_flags & PCB_KERNFPU_THR) != 0);
1267 * FPU save area alloc/free/init utility routines
1270 fpu_save_area_alloc(void)
1273 return (uma_zalloc(fpu_save_area_zone, M_WAITOK));
1277 fpu_save_area_free(struct savefpu *fsa)
1280 uma_zfree(fpu_save_area_zone, fsa);
1284 fpu_save_area_reset(struct savefpu *fsa)
1287 bcopy(fpu_initialstate, fsa, cpu_max_ext_state_size);