2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1992-1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 #include <mach/exception_types.h>
61 #include <mach/i386/thread_status.h>
62 #include <mach/i386/fp_reg.h>
63 #include <mach/branch_predicates.h>
65 #include <kern/mach_param.h>
66 #include <kern/processor.h>
67 #include <kern/thread.h>
68 #include <kern/zalloc.h>
69 #include <kern/misc_protos.h>
71 #include <kern/assert.h>
73 #include <libkern/OSAtomic.h>
75 #include <architecture/i386/pio.h>
76 #include <i386/cpuid.h>
78 #include <i386/proc_reg.h>
79 #include <i386/misc_protos.h>
80 #include <i386/thread.h>
81 #include <i386/trap.h>
83 int fp_kind
= FP_NO
; /* not inited */
84 zone_t ifps_zone
; /* zone for FPU save area */
86 #define ALIGNED(addr,size) (((uintptr_t)(addr)&((size)-1))==0)
90 extern void fpinit(void);
96 static void configure_mxcsr_capability_mask(struct x86_avx_thread_state
*fps
);
98 struct x86_avx_thread_state initial_fp_state
__attribute((aligned(64)));
101 /* Global MXCSR capability bitmask */
102 static unsigned int mxcsr_capability_mask
;
105 __asm__ volatile("fninit")
107 #define fnstcw(control) \
108 __asm__("fnstcw %0" : "=m" (*(unsigned short *)(control)))
110 #define fldcw(control) \
111 __asm__ volatile("fldcw %0" : : "m" (*(unsigned short *) &(control)) )
114 __asm__ volatile("fnclex")
116 #define fnsave(state) \
117 __asm__ volatile("fnsave %0" : "=m" (*state))
119 #define frstor(state) \
120 __asm__ volatile("frstor %0" : : "m" (state))
125 #define fxrstor(addr) __asm__ __volatile__("fxrstor %0" : : "m" (*(addr)))
126 #define fxsave(addr) __asm__ __volatile__("fxsave %0" : "=m" (*(addr)))
128 static uint32_t fp_register_state_size
= 0;
129 static uint32_t fpu_YMM_present
= FALSE
;
130 static uint32_t cpuid_reevaluated
= 0;
132 static void fpu_store_registers(void *, boolean_t
);
133 static void fpu_load_registers(void *);
135 extern void xsave64o(void);
136 extern void xrstor64o(void);
138 #define XMASK ((uint32_t) (XFEM_X87 | XFEM_SSE | XFEM_YMM))
140 static inline void xsetbv(uint32_t mask_hi
, uint32_t mask_lo
) {
141 __asm__
__volatile__("xsetbv" :: "a"(mask_lo
), "d"(mask_hi
), "c" (XCR0
));
144 static inline void xsave(struct x86_fx_thread_state
*a
) {
145 __asm__
__volatile__("xsave %0" :"=m" (*a
) : "a"(XMASK
), "d"(0));
148 static inline void xrstor(struct x86_fx_thread_state
*a
) {
149 __asm__
__volatile__("xrstor %0" :: "m" (*a
), "a"(XMASK
), "d"(0));
153 static inline unsigned short
156 unsigned short status
;
157 __asm__
volatile("fnstsw %0" : "=ma" (status
));
163 * Configure the initial FPU state presented to new threads.
164 * Determine the MXCSR capability mask, which allows us to mask off any
165 * potentially unsafe "reserved" bits before restoring the FPU context.
166 * *Not* per-cpu, assumes symmetry.
170 configure_mxcsr_capability_mask(struct x86_avx_thread_state
*fps
)
172 /* XSAVE requires a 64 byte aligned store */
173 assert(ALIGNED(fps
, 64));
174 /* Clear, to prepare for the diagnostic FXSAVE */
175 bzero(fps
, sizeof(*fps
));
178 fpu_store_registers(fps
, FALSE
);
180 mxcsr_capability_mask
= fps
->fx_MXCSR_MASK
;
182 /* Set default mask value if necessary */
183 if (mxcsr_capability_mask
== 0)
184 mxcsr_capability_mask
= 0xffbf;
186 /* Clear vector register store */
187 bzero(&fps
->fx_XMM_reg
[0][0], sizeof(fps
->fx_XMM_reg
));
188 bzero(&fps
->x_YMMH_reg
[0][0], sizeof(fps
->x_YMMH_reg
));
190 fps
->fp_valid
= TRUE
;
191 fps
->fp_save_layout
= fpu_YMM_present
? XSAVE32
: FXSAVE32
;
192 fpu_load_registers(fps
);
194 /* Poison values to trap unsafe usage */
195 fps
->fp_valid
= 0xFFFFFFFF;
196 fps
->fp_save_layout
= FP_UNUSED
;
198 /* Re-enable FPU/SSE DNA exceptions */
204 * Look for FPU and initialize it.
205 * Called on each CPU.
211 unsigned short status
;
212 unsigned short control
;
215 * Check for FPU by initializing it,
216 * then trying to read the correct bit patterns from
217 * the control and status registers.
219 set_cr0((get_cr0() & ~(CR0_EM
|CR0_TS
)) | CR0_NE
); /* allow use of FPU */
225 assert(((status
& 0xff) == 0) && ((control
& 0x103f) == 0x3f));
227 /* Advertise SSE support */
228 if (cpuid_features() & CPUID_FEATURE_FXSR
) {
230 set_cr4(get_cr4() | CR4_OSFXS
);
231 /* And allow SIMD exceptions if present */
232 if (cpuid_features() & CPUID_FEATURE_SSE
) {
233 set_cr4(get_cr4() | CR4_OSXMM
);
235 fp_register_state_size
= sizeof(struct x86_fx_thread_state
);
238 panic("fpu is not FP_FXSR");
240 /* Configure the XSAVE context mechanism if the processor supports
243 if (cpuid_features() & CPUID_FEATURE_XSAVE
) {
244 cpuid_xsave_leaf_t
*xsp
= &cpuid_info()->cpuid_xsave_leaf
[0];
245 if (xsp
->extended_state
[0] & (uint32_t)XFEM_YMM
) {
246 assert(xsp
->extended_state
[0] & (uint32_t) XFEM_SSE
);
247 /* XSAVE container size for all features */
248 fp_register_state_size
= sizeof(struct x86_avx_thread_state
);
249 fpu_YMM_present
= TRUE
;
250 set_cr4(get_cr4() | CR4_OSXSAVE
);
252 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
253 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated
))
255 /* Verify that now selected state can be accommodated */
256 assert(xsp
->extended_state
[1] == fp_register_state_size
);
260 fpu_YMM_present
= FALSE
;
265 * Trap wait instructions. Turn off FPU for now.
267 set_cr0(get_cr0() | CR0_TS
| CR0_MP
);
271 * Allocate and initialize FP state for current thread.
277 struct x86_fx_thread_state
*ifps
= zalloc(ifps_zone
);
280 if (!(ALIGNED(ifps
,64))) {
281 panic("fp_state_alloc: %p, %u, %p, %u", ifps
, (unsigned) ifps_zone
->elem_size
, (void *) ifps_zone
->free_elements
, (unsigned) ifps_zone
->alloc_size
);
284 bzero(ifps
, sizeof(*ifps
));
289 fp_state_free(void *ifps
)
291 zfree(ifps_zone
, ifps
);
300 static void fpu_load_registers(void *fstate
) {
301 struct x86_fx_thread_state
*ifps
= fstate
;
302 fp_save_layout_t layout
= ifps
->fp_save_layout
;
304 assert(layout
== FXSAVE32
|| layout
== FXSAVE64
|| layout
== XSAVE32
|| layout
== XSAVE64
);
305 assert(ALIGNED(ifps
, 64));
306 assert(ml_get_interrupts_enabled() == FALSE
);
309 if (layout
== XSAVE32
|| layout
== XSAVE64
) {
310 struct x86_avx_thread_state
*iavx
= fstate
;
312 /* Verify reserved bits in the XSAVE header*/
313 if (iavx
->_xh
.xsbv
& ~7)
314 panic("iavx->_xh.xsbv: 0x%llx", iavx
->_xh
.xsbv
);
315 for (i
= 0; i
< sizeof(iavx
->_xh
.xhrsvd
); i
++)
316 if (iavx
->_xh
.xhrsvd
[i
])
317 panic("Reserved bit set");
319 if (fpu_YMM_present
) {
320 if (layout
!= XSAVE32
&& layout
!= XSAVE64
)
321 panic("Inappropriate layout: %u\n", layout
);
325 if ((layout
== XSAVE64
) || (layout
== XSAVE32
))
331 static void fpu_store_registers(void *fstate
, boolean_t is64
) {
332 struct x86_fx_thread_state
*ifps
= fstate
;
333 assert(ALIGNED(ifps
, 64));
334 if (fpu_YMM_present
) {
336 ifps
->fp_save_layout
= is64
? XSAVE64
: XSAVE32
;
340 ifps
->fp_save_layout
= is64
? FXSAVE64
: FXSAVE32
;
345 * Initialize FP handling.
349 fpu_module_init(void)
351 if ((fp_register_state_size
!= sizeof(struct x86_fx_thread_state
)) &&
352 (fp_register_state_size
!= sizeof(struct x86_avx_thread_state
)))
353 panic("fpu_module_init: incorrect savearea size %u\n", fp_register_state_size
);
355 assert(fpu_YMM_present
!= 0xFFFFFFFF);
357 /* We explicitly choose an allocation size of 64
358 * to eliminate waste for the 832 byte sized
359 * AVX XSAVE register save area.
361 ifps_zone
= zinit(fp_register_state_size
,
362 thread_max
* fp_register_state_size
,
363 64 * fp_register_state_size
,
366 /* To maintain the required alignment, disable
367 * zone debugging for this zone as that appends
368 * 16 bytes to each element.
370 zone_change(ifps_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
371 /* Determine MXCSR reserved bits and configure initial FPU state*/
372 configure_mxcsr_capability_mask(&initial_fp_state
);
376 * Save thread`s FPU context.
379 fpu_save_context(thread_t thread
)
381 struct x86_fx_thread_state
*ifps
;
383 assert(ml_get_interrupts_enabled() == FALSE
);
384 ifps
= (thread
)->machine
.ifps
;
386 if (ifps
&& ((ifps
->fp_valid
!= FALSE
) && (ifps
->fp_valid
!= TRUE
))) {
387 panic("ifps->fp_valid: %u\n", ifps
->fp_valid
);
390 if (ifps
!= 0 && (ifps
->fp_valid
== FALSE
)) {
391 /* Clear CR0.TS in preparation for the FP context save. In
392 * theory, this shouldn't be necessary since a live FPU should
393 * indicate that TS is clear. However, various routines
394 * (such as sendsig & sigreturn) manipulate TS directly.
397 /* registers are in FPU - save to memory */
398 fpu_store_registers(ifps
, (thread_is_64bit(thread
) && is_saved_state64(thread
->machine
.iss
)));
399 ifps
->fp_valid
= TRUE
;
406 * Free a FPU save area.
407 * Called only when thread terminating - no locking necessary.
416 * Set the floating-point state for a thread based
417 * on the FXSave formatted data. This is basically
418 * the same as fpu_set_state except it uses the
419 * expanded data structure.
420 * If the thread is not the current thread, it is
421 * not running (held). Locking needed against
422 * concurrent fpu_set_state or fpu_get_state.
427 thread_state_t tstate
,
430 struct x86_fx_thread_state
*ifps
;
431 struct x86_fx_thread_state
*new_ifps
;
432 x86_float_state64_t
*state
;
434 size_t state_size
= sizeof(struct x86_fx_thread_state
);
435 boolean_t old_valid
, fresh_state
= FALSE
;
437 if (fp_kind
== FP_NO
)
440 if ((f
== x86_AVX_STATE32
|| f
== x86_AVX_STATE64
) &&
441 !ml_fpu_avx_enabled())
444 state
= (x86_float_state64_t
*)tstate
;
446 assert(thr_act
!= THREAD_NULL
);
447 pcb
= THREAD_TO_PCB(thr_act
);
451 * new FPU state is 'invalid'.
452 * Deallocate the fp state if it exists.
454 simple_lock(&pcb
->lock
);
459 simple_unlock(&pcb
->lock
);
466 * Valid incoming state. Allocate the fp state if there is none.
470 simple_lock(&pcb
->lock
);
475 simple_unlock(&pcb
->lock
);
476 new_ifps
= fp_state_alloc();
486 * now copy over the new data.
489 old_valid
= ifps
->fp_valid
;
491 #if DEBUG || DEVELOPMENT
492 if ((fresh_state
== FALSE
) && (old_valid
== FALSE
) && (thr_act
!= current_thread())) {
493 panic("fpu_set_fxstate inconsistency, thread: %p not stopped", thr_act
);
497 * Clear any reserved bits in the MXCSR to prevent a GPF
498 * when issuing an FXRSTOR.
501 state
->fpu_mxcsr
&= mxcsr_capability_mask
;
503 bcopy((char *)&state
->fpu_fcw
, (char *)ifps
, state_size
);
505 if (fpu_YMM_present
) {
506 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
507 uint32_t fpu_nyreg
= 0;
509 if (f
== x86_AVX_STATE32
)
511 else if (f
== x86_AVX_STATE64
)
515 x86_avx_state64_t
*ystate
= (x86_avx_state64_t
*) state
;
516 bcopy(&ystate
->__fpu_ymmh0
, &iavx
->x_YMMH_reg
[0][0], fpu_nyreg
* sizeof(_STRUCT_XMM_REG
));
519 iavx
->fp_save_layout
= thread_is_64bit(thr_act
) ? XSAVE64
: XSAVE32
;
520 /* Sanitize XSAVE header */
521 bzero(&iavx
->_xh
.xhrsvd
[0], sizeof(iavx
->_xh
.xhrsvd
));
523 iavx
->_xh
.xsbv
= (XFEM_YMM
| XFEM_SSE
| XFEM_X87
);
525 iavx
->_xh
.xsbv
= (XFEM_SSE
| XFEM_X87
);
527 ifps
->fp_save_layout
= thread_is_64bit(thr_act
) ? FXSAVE64
: FXSAVE32
;
529 ifps
->fp_valid
= old_valid
;
531 if (old_valid
== FALSE
) {
532 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
533 ifps
->fp_valid
= TRUE
;
534 /* If altering the current thread's state, disable FPU */
535 if (thr_act
== current_thread())
538 ml_set_interrupts_enabled(istate
);
541 simple_unlock(&pcb
->lock
);
544 fp_state_free(new_ifps
);
550 * Get the floating-point state for a thread.
551 * If the thread is not the current thread, it is
552 * not running (held). Locking needed against
553 * concurrent fpu_set_state or fpu_get_state.
558 thread_state_t tstate
,
561 struct x86_fx_thread_state
*ifps
;
562 x86_float_state64_t
*state
;
563 kern_return_t ret
= KERN_FAILURE
;
565 size_t state_size
= sizeof(struct x86_fx_thread_state
);
567 if (fp_kind
== FP_NO
)
570 if ((f
== x86_AVX_STATE32
|| f
== x86_AVX_STATE64
) &&
571 !ml_fpu_avx_enabled())
574 state
= (x86_float_state64_t
*)tstate
;
576 assert(thr_act
!= THREAD_NULL
);
577 pcb
= THREAD_TO_PCB(thr_act
);
579 simple_lock(&pcb
->lock
);
584 * No valid floating-point state.
587 bcopy((char *)&initial_fp_state
, (char *)&state
->fpu_fcw
,
590 simple_unlock(&pcb
->lock
);
595 * Make sure we`ve got the latest fp state info
596 * If the live fpu state belongs to our target
598 if (thr_act
== current_thread()) {
601 intr
= ml_set_interrupts_enabled(FALSE
);
607 (void)ml_set_interrupts_enabled(intr
);
609 if (ifps
->fp_valid
) {
610 bcopy((char *)ifps
, (char *)&state
->fpu_fcw
, state_size
);
611 if (fpu_YMM_present
) {
612 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
613 uint32_t fpu_nyreg
= 0;
615 if (f
== x86_AVX_STATE32
)
617 else if (f
== x86_AVX_STATE64
)
621 x86_avx_state64_t
*ystate
= (x86_avx_state64_t
*) state
;
622 bcopy(&iavx
->x_YMMH_reg
[0][0], &ystate
->__fpu_ymmh0
, fpu_nyreg
* sizeof(_STRUCT_XMM_REG
));
628 simple_unlock(&pcb
->lock
);
636 * the child thread is 'stopped' with the thread
637 * mutex held and is currently not known by anyone
638 * so no way for fpu state to get manipulated by an
639 * outside agency -> no need for pcb lock
647 struct x86_fx_thread_state
*new_ifps
= NULL
;
651 ppcb
= THREAD_TO_PCB(parent
);
653 if (ppcb
->ifps
== NULL
)
656 if (child
->machine
.ifps
)
657 panic("fpu_dup_fxstate: child's ifps non-null");
659 new_ifps
= fp_state_alloc();
661 simple_lock(&ppcb
->lock
);
663 if (ppcb
->ifps
!= NULL
) {
664 struct x86_fx_thread_state
*ifps
= ppcb
->ifps
;
666 * Make sure we`ve got the latest fp state info
668 if (current_thread() == parent
) {
669 intr
= ml_set_interrupts_enabled(FALSE
);
670 assert(current_thread() == parent
);
675 (void)ml_set_interrupts_enabled(intr
);
678 if (ifps
->fp_valid
) {
679 child
->machine
.ifps
= new_ifps
;
680 assert((fp_register_state_size
== sizeof(struct x86_fx_thread_state
)) ||
681 (fp_register_state_size
== sizeof(struct x86_avx_thread_state
)));
682 bcopy((char *)(ppcb
->ifps
),
683 (char *)(child
->machine
.ifps
), fp_register_state_size
);
685 /* Mark the new fp saved state as non-live. */
686 /* Temporarily disabled: radar 4647827
687 * new_ifps->fp_valid = TRUE;
691 * Clear any reserved bits in the MXCSR to prevent a GPF
692 * when issuing an FXRSTOR.
694 new_ifps
->fx_MXCSR
&= mxcsr_capability_mask
;
698 simple_unlock(&ppcb
->lock
);
700 if (new_ifps
!= NULL
)
701 fp_state_free(new_ifps
);
713 unsigned short control
;
718 control
&= ~(FPC_PC
|FPC_RC
); /* Clear precision & rounding control */
719 control
|= (FPC_PC_64
| /* Set precision */
720 FPC_RC_RN
| /* round-to-nearest */
721 FPC_ZE
| /* Suppress zero-divide */
722 FPC_OE
| /* and overflow */
723 FPC_UE
| /* underflow */
724 FPC_IE
| /* Allow NaNQs and +-INF */
725 FPC_DE
| /* Allow denorms as operands */
726 FPC_PE
); /* No trap for precision loss */
729 /* Initialize SSE/SSE2 */
730 __builtin_ia32_ldmxcsr(0x1f80);
734 * Coprocessor not present.
737 uint64_t x86_isr_fp_simd_use
;
745 struct x86_fx_thread_state
*ifps
= 0;
747 thr_act
= current_thread();
748 pcb
= THREAD_TO_PCB(thr_act
);
750 assert(fp_register_state_size
!= 0);
752 if (pcb
->ifps
== 0 && !get_interrupt_level()) {
753 ifps
= fp_state_alloc();
754 bcopy((char *)&initial_fp_state
, (char *)ifps
,
755 fp_register_state_size
);
756 if (!thread_is_64bit(thr_act
)) {
757 ifps
->fp_save_layout
= fpu_YMM_present
? XSAVE32
: FXSAVE32
;
760 ifps
->fp_save_layout
= fpu_YMM_present
? XSAVE64
: FXSAVE64
;
761 ifps
->fp_valid
= TRUE
;
763 intr
= ml_set_interrupts_enabled(FALSE
);
765 clear_ts(); /* Enable FPU use */
767 if (__improbable(get_interrupt_level())) {
768 /* Track number of #DNA traps at interrupt context,
769 * which is likely suboptimal. Racy, but good enough.
771 x86_isr_fp_simd_use
++;
773 * Save current FP/SIMD context if valid
774 * Initialize live FP/SIMD registers
781 if (pcb
->ifps
== 0) {
786 * Load this thread`s state into coprocessor live context.
790 (void)ml_set_interrupts_enabled(intr
);
797 * FPU overran end of segment.
798 * Re-initialize FPU. Floating point state is not valid.
804 thread_t thr_act
= current_thread();
806 struct x86_fx_thread_state
*ifps
;
809 intr
= ml_set_interrupts_enabled(FALSE
);
811 if (get_interrupt_level())
812 panic("FPU segment overrun exception at interrupt context\n");
813 if (current_task() == kernel_task
)
814 panic("FPU segment overrun exception in kernel thread context\n");
817 * This is a non-recoverable error.
818 * Invalidate the thread`s FPU state.
820 pcb
= THREAD_TO_PCB(thr_act
);
821 simple_lock(&pcb
->lock
);
824 simple_unlock(&pcb
->lock
);
827 * Re-initialize the FPU.
833 * And disable access.
837 (void)ml_set_interrupts_enabled(intr
);
840 zfree(ifps_zone
, ifps
);
845 i386_exception(EXC_BAD_ACCESS
, VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
850 * FPU error. Called by AST.
856 thread_t thr_act
= current_thread();
857 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
860 intr
= ml_set_interrupts_enabled(FALSE
);
862 if (get_interrupt_level())
863 panic("FPU error exception at interrupt context\n");
864 if (current_task() == kernel_task
)
865 panic("FPU error exception in kernel thread context\n");
868 * Save the FPU state and turn off the FPU.
872 (void)ml_set_interrupts_enabled(intr
);
875 * Raise FPU exception.
876 * Locking not needed on pcb->ifps,
877 * since thread is running.
879 i386_exception(EXC_ARITHMETIC
,
889 * Locking not needed:
890 * . if called from fpu_get_state, pcb already locked.
891 * . if called from fpnoextflt or fp_intr, we are single-cpu
892 * . otherwise, thread is running.
893 * N.B.: Must be called with interrupts disabled
900 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
901 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
904 if (ifps
!= 0 && !ifps
->fp_valid
) {
905 assert((get_cr0() & CR0_TS
) == 0);
906 /* registers are in FPU */
907 ifps
->fp_valid
= TRUE
;
908 fpu_store_registers(ifps
, thread_is_64bit(thr_act
));
913 * Restore FPU state from PCB.
915 * Locking not needed; always called on the current thread.
922 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
923 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
927 if (ifps
->fp_valid
!= FALSE
&& ifps
->fp_valid
!= TRUE
) {
928 panic("fp_load() invalid fp_valid: %u, fp_save_layout: %u\n",
929 ifps
->fp_valid
, ifps
->fp_save_layout
);
933 if (ifps
->fp_valid
== FALSE
) {
936 fpu_load_registers(ifps
);
938 ifps
->fp_valid
= FALSE
; /* in FPU */
942 * SSE arithmetic exception handling code.
943 * Basically the same as the x87 exception handler with a different subtype
949 thread_t thr_act
= current_thread();
950 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
953 intr
= ml_set_interrupts_enabled(FALSE
);
955 if (get_interrupt_level())
956 panic("SSE exception at interrupt context\n");
957 if (current_task() == kernel_task
)
958 panic("SSE exception in kernel thread context\n");
961 * Save the FPU state and turn off the FPU.
965 (void)ml_set_interrupts_enabled(intr
);
967 * Raise FPU exception.
968 * Locking not needed on pcb->ifps,
969 * since thread is running.
972 i386_exception(EXC_ARITHMETIC
,
979 fp_setvalid(boolean_t value
) {
980 thread_t thr_act
= current_thread();
981 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
984 ifps
->fp_valid
= value
;
987 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
989 ml_set_interrupts_enabled(istate
);
995 ml_fpu_avx_enabled(void) {
996 return (fpu_YMM_present
== TRUE
);