2 * Copyright (c) 2000-2010 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.
59 #include <platforms.h>
61 #include <mach/exception_types.h>
62 #include <mach/i386/thread_status.h>
63 #include <mach/i386/fp_reg.h>
64 #include <mach/branch_predicates.h>
66 #include <kern/mach_param.h>
67 #include <kern/processor.h>
68 #include <kern/thread.h>
69 #include <kern/zalloc.h>
70 #include <kern/misc_protos.h>
72 #include <kern/assert.h>
74 #include <libkern/OSAtomic.h>
76 #include <architecture/i386/pio.h>
77 #include <i386/cpuid.h>
79 #include <i386/proc_reg.h>
80 #include <i386/misc_protos.h>
81 #include <i386/thread.h>
82 #include <i386/trap.h>
84 int fp_kind
= FP_NO
; /* not inited */
85 zone_t ifps_zone
; /* zone for FPU save area */
87 #define ALIGNED(addr,size) (((uintptr_t)(addr)&((size)-1))==0)
91 extern void fpinit(void);
97 static void configure_mxcsr_capability_mask(struct x86_avx_thread_state
*fps
);
99 struct x86_avx_thread_state initial_fp_state
__attribute((aligned(64)));
102 /* Global MXCSR capability bitmask */
103 static unsigned int mxcsr_capability_mask
;
106 __asm__ volatile("fninit")
108 #define fnstcw(control) \
109 __asm__("fnstcw %0" : "=m" (*(unsigned short *)(control)))
111 #define fldcw(control) \
112 __asm__ volatile("fldcw %0" : : "m" (*(unsigned short *) &(control)) )
115 __asm__ volatile("fnclex")
117 #define fnsave(state) \
118 __asm__ volatile("fnsave %0" : "=m" (*state))
120 #define frstor(state) \
121 __asm__ volatile("frstor %0" : : "m" (state))
126 #define fxrstor(addr) __asm__ __volatile__("fxrstor %0" : : "m" (*(addr)))
127 #define fxsave(addr) __asm__ __volatile__("fxsave %0" : "=m" (*(addr)))
129 static uint32_t fp_register_state_size
= 0;
130 static uint32_t fpu_YMM_present
= FALSE
;
131 static uint32_t cpuid_reevaluated
= 0;
133 static void fpu_store_registers(void *, boolean_t
);
134 static void fpu_load_registers(void *);
136 extern void xsave64o(void);
137 extern void xrstor64o(void);
139 #define XMASK ((uint32_t) (XFEM_X87 | XFEM_SSE | XFEM_YMM))
141 /* DRK: TODO replace opcodes with mnemonics when assembler support available */
143 static inline void xsetbv(uint32_t mask_hi
, uint32_t mask_lo
) {
144 __asm__
__volatile__(".short 0x010F\n\t.byte 0xD1" :: "a"(mask_lo
), "d"(mask_hi
), "c" (XCR0
));
147 static inline void xsave(void *a
) {
148 /* MOD 0x4, operand ECX 0x1 */
149 __asm__
__volatile__(".short 0xAE0F\n\t.byte 0x21" :: "a"(XMASK
), "d"(0), "c" (a
));
152 static inline void xrstor(void *a
) {
153 /* MOD 0x5, operand ECX 0x1 */
154 __asm__
__volatile__(".short 0xAE0F\n\t.byte 0x29" :: "a"(XMASK
), "d"(0), "c" (a
));
157 static inline void xsave64(void *a
) {
158 /* Out of line call that executes in 64-bit mode on K32 */
159 __asm__
__volatile__("call _xsave64o" :: "a"(XMASK
), "d"(0), "c" (a
));
162 static inline void xrstor64(void *a
) {
163 /* Out of line call that executes in 64-bit mode on K32 */
164 __asm__
__volatile__("call _xrstor64o" :: "a"(XMASK
), "d"(0), "c" (a
));
167 static inline unsigned short
170 unsigned short status
;
171 __asm__
volatile("fnstsw %0" : "=ma" (status
));
176 * Configure the initial FPU state presented to new threads.
177 * Determine the MXCSR capability mask, which allows us to mask off any
178 * potentially unsafe "reserved" bits before restoring the FPU context.
179 * *Not* per-cpu, assumes symmetry.
183 configure_mxcsr_capability_mask(struct x86_avx_thread_state
*fps
)
185 /* XSAVE requires a 64 byte aligned store */
186 assert(ALIGNED(fps
, 64));
187 /* Clear, to prepare for the diagnostic FXSAVE */
188 bzero(fps
, sizeof(*fps
));
191 fpu_store_registers(fps
, FALSE
);
193 mxcsr_capability_mask
= fps
->fx_MXCSR_MASK
;
195 /* Set default mask value if necessary */
196 if (mxcsr_capability_mask
== 0)
197 mxcsr_capability_mask
= 0xffbf;
199 /* Clear vector register store */
200 bzero(&fps
->fx_XMM_reg
[0][0], sizeof(fps
->fx_XMM_reg
));
201 bzero(&fps
->x_YMMH_reg
[0][0], sizeof(fps
->x_YMMH_reg
));
203 fps
->fp_valid
= TRUE
;
204 fps
->fp_save_layout
= fpu_YMM_present
? XSAVE32
: FXSAVE32
;
205 fpu_load_registers(fps
);
207 /* Poison values to trap unsafe usage */
208 fps
->fp_valid
= 0xFFFFFFFF;
209 fps
->fp_save_layout
= FP_UNUSED
;
211 /* Re-enable FPU/SSE DNA exceptions */
217 * Look for FPU and initialize it.
218 * Called on each CPU.
224 unsigned short status
;
225 unsigned short control
;
228 * Check for FPU by initializing it,
229 * then trying to read the correct bit patterns from
230 * the control and status registers.
232 set_cr0((get_cr0() & ~(CR0_EM
|CR0_TS
)) | CR0_NE
); /* allow use of FPU */
238 assert(((status
& 0xff) == 0) && ((control
& 0x103f) == 0x3f));
240 /* Advertise SSE support */
241 if (cpuid_features() & CPUID_FEATURE_FXSR
) {
243 set_cr4(get_cr4() | CR4_OSFXS
);
244 /* And allow SIMD exceptions if present */
245 if (cpuid_features() & CPUID_FEATURE_SSE
) {
246 set_cr4(get_cr4() | CR4_OSXMM
);
248 fp_register_state_size
= sizeof(struct x86_fx_thread_state
);
251 panic("fpu is not FP_FXSR");
253 /* Configure the XSAVE context mechanism if the processor supports
256 if (cpuid_features() & CPUID_FEATURE_XSAVE
) {
257 cpuid_xsave_leaf_t
*xsp
= &cpuid_info()->cpuid_xsave_leaf
;
258 if (xsp
->extended_state
[0] & (uint32_t)XFEM_YMM
) {
259 assert(xsp
->extended_state
[0] & (uint32_t) XFEM_SSE
);
260 /* XSAVE container size for all features */
261 assert(xsp
->extended_state
[2] == sizeof(struct x86_avx_thread_state
));
262 fp_register_state_size
= sizeof(struct x86_avx_thread_state
);
263 fpu_YMM_present
= TRUE
;
264 set_cr4(get_cr4() | CR4_OSXSAVE
);
266 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
267 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated
))
269 /* DRK: consider verifying AVX offset with cpuid(d, ECX:2) */
273 fpu_YMM_present
= FALSE
;
278 * Trap wait instructions. Turn off FPU for now.
280 set_cr0(get_cr0() | CR0_TS
| CR0_MP
);
284 * Allocate and initialize FP state for current thread.
290 void *ifps
= zalloc(ifps_zone
);
293 if (!(ALIGNED(ifps
,64))) {
294 panic("fp_state_alloc: %p, %u, %p, %u", ifps
, (unsigned) ifps_zone
->elem_size
, (void *) ifps_zone
->free_elements
, (unsigned) ifps_zone
->alloc_size
);
301 fp_state_free(void *ifps
)
303 zfree(ifps_zone
, ifps
);
312 static void fpu_load_registers(void *fstate
) {
313 struct x86_fx_thread_state
*ifps
= fstate
;
314 fp_save_layout_t layout
= ifps
->fp_save_layout
;
316 assert(layout
== FXSAVE32
|| layout
== FXSAVE64
|| layout
== XSAVE32
|| layout
== XSAVE64
);
317 assert(ALIGNED(ifps
, 64));
318 assert(ml_get_interrupts_enabled() == FALSE
);
321 if (layout
== XSAVE32
|| layout
== XSAVE64
) {
322 struct x86_avx_thread_state
*iavx
= fstate
;
324 /* Verify reserved bits in the XSAVE header*/
325 if (iavx
->_xh
.xsbv
& ~7)
326 panic("iavx->_xh.xsbv: 0x%llx", iavx
->_xh
.xsbv
);
327 for (i
= 0; i
< sizeof(iavx
->_xh
.xhrsvd
); i
++)
328 if (iavx
->_xh
.xhrsvd
[i
])
329 panic("Reserved bit set");
331 if (fpu_YMM_present
) {
332 if (layout
!= XSAVE32
&& layout
!= XSAVE64
)
333 panic("Inappropriate layout: %u\n", layout
);
337 #if defined(__i386__)
338 if (layout
== FXSAVE32
) {
339 /* Restore the compatibility/legacy mode XMM+x87 state */
342 else if (layout
== FXSAVE64
) {
345 else if (layout
== XSAVE32
) {
348 else if (layout
== XSAVE64
) {
351 #elif defined(__x86_64__)
352 if ((layout
== XSAVE64
) || (layout
== XSAVE32
))
359 static void fpu_store_registers(void *fstate
, boolean_t is64
) {
360 struct x86_fx_thread_state
*ifps
= fstate
;
361 assert(ALIGNED(ifps
, 64));
362 #if defined(__i386__)
364 if (fpu_YMM_present
) {
366 ifps
->fp_save_layout
= XSAVE32
;
369 /* save the compatibility/legacy mode XMM+x87 state */
371 ifps
->fp_save_layout
= FXSAVE32
;
375 if (fpu_YMM_present
) {
377 ifps
->fp_save_layout
= XSAVE64
;
381 ifps
->fp_save_layout
= FXSAVE64
;
384 #elif defined(__x86_64__)
385 if (fpu_YMM_present
) {
387 ifps
->fp_save_layout
= is64
? XSAVE64
: XSAVE32
;
391 ifps
->fp_save_layout
= is64
? FXSAVE64
: FXSAVE32
;
397 * Initialize FP handling.
401 fpu_module_init(void)
403 if ((fp_register_state_size
!= sizeof(struct x86_fx_thread_state
)) &&
404 (fp_register_state_size
!= sizeof(struct x86_avx_thread_state
)))
405 panic("fpu_module_init: incorrect savearea size %u\n", fp_register_state_size
);
407 assert(fpu_YMM_present
!= 0xFFFFFFFF);
409 /* We explicitly choose an allocation size of 64
410 * to eliminate waste for the 832 byte sized
411 * AVX XSAVE register save area.
413 ifps_zone
= zinit(fp_register_state_size
,
414 thread_max
* fp_register_state_size
,
415 64 * fp_register_state_size
,
418 /* To maintain the required alignment, disable
419 * zone debugging for this zone as that appends
420 * 16 bytes to each element.
422 zone_change(ifps_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
423 /* Determine MXCSR reserved bits and configure initial FPU state*/
424 configure_mxcsr_capability_mask(&initial_fp_state
);
428 * Save thread`s FPU context.
431 fpu_save_context(thread_t thread
)
433 struct x86_fx_thread_state
*ifps
;
435 assert(ml_get_interrupts_enabled() == FALSE
);
436 ifps
= (thread
)->machine
.ifps
;
438 if (ifps
&& ((ifps
->fp_valid
!= FALSE
) && (ifps
->fp_valid
!= TRUE
))) {
439 panic("ifps->fp_valid: %u\n", ifps
->fp_valid
);
442 if (ifps
!= 0 && (ifps
->fp_valid
== FALSE
)) {
443 /* Clear CR0.TS in preparation for the FP context save. In
444 * theory, this shouldn't be necessary since a live FPU should
445 * indicate that TS is clear. However, various routines
446 * (such as sendsig & sigreturn) manipulate TS directly.
449 /* registers are in FPU - save to memory */
450 fpu_store_registers(ifps
, (thread_is_64bit(thread
) && is_saved_state64(thread
->machine
.iss
)));
451 ifps
->fp_valid
= TRUE
;
458 * Free a FPU save area.
459 * Called only when thread terminating - no locking necessary.
468 * Set the floating-point state for a thread based
469 * on the FXSave formatted data. This is basically
470 * the same as fpu_set_state except it uses the
471 * expanded data structure.
472 * If the thread is not the current thread, it is
473 * not running (held). Locking needed against
474 * concurrent fpu_set_state or fpu_get_state.
479 thread_state_t tstate
,
482 struct x86_fx_thread_state
*ifps
;
483 struct x86_fx_thread_state
*new_ifps
;
484 x86_float_state64_t
*state
;
486 size_t state_size
= sizeof(struct x86_fx_thread_state
);
488 if (fp_kind
== FP_NO
)
491 state
= (x86_float_state64_t
*)tstate
;
493 assert(thr_act
!= THREAD_NULL
);
494 pcb
= THREAD_TO_PCB(thr_act
);
498 * new FPU state is 'invalid'.
499 * Deallocate the fp state if it exists.
501 simple_lock(&pcb
->lock
);
506 simple_unlock(&pcb
->lock
);
512 * Valid state. Allocate the fp state if there is none.
516 simple_lock(&pcb
->lock
);
521 simple_unlock(&pcb
->lock
);
522 new_ifps
= fp_state_alloc();
530 * now copy over the new data.
532 old_valid
= ifps
->fp_valid
;
535 if ((old_valid
== FALSE
) && (thr_act
!= current_thread())) {
536 panic("fpu_set_fxstate inconsistency, thread: %p not stopped", thr_act
);
540 * Clear any reserved bits in the MXCSR to prevent a GPF
541 * when issuing an FXRSTOR.
544 state
->fpu_mxcsr
&= mxcsr_capability_mask
;
546 bcopy((char *)&state
->fpu_fcw
, (char *)ifps
, state_size
);
548 if (fpu_YMM_present
) {
549 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
550 uint32_t fpu_nyreg
= 0;
552 if (f
== x86_AVX_STATE32
)
554 else if (f
== x86_AVX_STATE64
)
558 x86_avx_state64_t
*ystate
= (x86_avx_state64_t
*) state
;
559 bcopy(&ystate
->__fpu_ymmh0
, &iavx
->x_YMMH_reg
[0][0], fpu_nyreg
* sizeof(_STRUCT_XMM_REG
));
562 iavx
->fp_save_layout
= thread_is_64bit(thr_act
) ? XSAVE64
: XSAVE32
;
563 /* Sanitize XSAVE header */
564 bzero(&iavx
->_xh
.xhrsvd
[0], sizeof(iavx
->_xh
.xhrsvd
));
565 if (state_size
== sizeof(struct x86_avx_thread_state
))
566 iavx
->_xh
.xsbv
= (XFEM_YMM
| XFEM_SSE
| XFEM_X87
);
568 iavx
->_xh
.xsbv
= (XFEM_SSE
| XFEM_X87
);
571 ifps
->fp_save_layout
= thread_is_64bit(thr_act
) ? FXSAVE64
: FXSAVE32
;
572 ifps
->fp_valid
= old_valid
;
574 if (old_valid
== FALSE
) {
575 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
576 ifps
->fp_valid
= TRUE
;
578 ml_set_interrupts_enabled(istate
);
581 simple_unlock(&pcb
->lock
);
584 fp_state_free(new_ifps
);
590 * Get the floating-point state for a thread.
591 * If the thread is not the current thread, it is
592 * not running (held). Locking needed against
593 * concurrent fpu_set_state or fpu_get_state.
598 thread_state_t tstate
,
601 struct x86_fx_thread_state
*ifps
;
602 x86_float_state64_t
*state
;
603 kern_return_t ret
= KERN_FAILURE
;
605 size_t state_size
= sizeof(struct x86_fx_thread_state
);
607 if (fp_kind
== FP_NO
)
610 state
= (x86_float_state64_t
*)tstate
;
612 assert(thr_act
!= THREAD_NULL
);
613 pcb
= THREAD_TO_PCB(thr_act
);
615 simple_lock(&pcb
->lock
);
620 * No valid floating-point state.
623 bcopy((char *)&initial_fp_state
, (char *)&state
->fpu_fcw
,
626 simple_unlock(&pcb
->lock
);
631 * Make sure we`ve got the latest fp state info
632 * If the live fpu state belongs to our target
634 if (thr_act
== current_thread()) {
637 intr
= ml_set_interrupts_enabled(FALSE
);
643 (void)ml_set_interrupts_enabled(intr
);
645 if (ifps
->fp_valid
) {
646 bcopy((char *)ifps
, (char *)&state
->fpu_fcw
, state_size
);
647 if (fpu_YMM_present
) {
648 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
649 uint32_t fpu_nyreg
= 0;
651 if (f
== x86_AVX_STATE32
)
653 else if (f
== x86_AVX_STATE64
)
657 x86_avx_state64_t
*ystate
= (x86_avx_state64_t
*) state
;
658 bcopy(&iavx
->x_YMMH_reg
[0][0], &ystate
->__fpu_ymmh0
, fpu_nyreg
* sizeof(_STRUCT_XMM_REG
));
664 simple_unlock(&pcb
->lock
);
672 * the child thread is 'stopped' with the thread
673 * mutex held and is currently not known by anyone
674 * so no way for fpu state to get manipulated by an
675 * outside agency -> no need for pcb lock
683 struct x86_fx_thread_state
*new_ifps
= NULL
;
687 ppcb
= THREAD_TO_PCB(parent
);
689 if (ppcb
->ifps
== NULL
)
692 if (child
->machine
.ifps
)
693 panic("fpu_dup_fxstate: child's ifps non-null");
695 new_ifps
= fp_state_alloc();
697 simple_lock(&ppcb
->lock
);
699 if (ppcb
->ifps
!= NULL
) {
700 struct x86_fx_thread_state
*ifps
= ppcb
->ifps
;
702 * Make sure we`ve got the latest fp state info
704 intr
= ml_set_interrupts_enabled(FALSE
);
705 assert(current_thread() == parent
);
710 (void)ml_set_interrupts_enabled(intr
);
712 if (ifps
->fp_valid
) {
713 child
->machine
.ifps
= new_ifps
;
714 assert((fp_register_state_size
== sizeof(struct x86_fx_thread_state
)) ||
715 (fp_register_state_size
== sizeof(struct x86_avx_thread_state
)));
716 bcopy((char *)(ppcb
->ifps
),
717 (char *)(child
->machine
.ifps
), fp_register_state_size
);
719 /* Mark the new fp saved state as non-live. */
720 /* Temporarily disabled: radar 4647827
721 * new_ifps->fp_valid = TRUE;
725 * Clear any reserved bits in the MXCSR to prevent a GPF
726 * when issuing an FXRSTOR.
728 new_ifps
->fx_MXCSR
&= mxcsr_capability_mask
;
732 simple_unlock(&ppcb
->lock
);
734 if (new_ifps
!= NULL
)
735 fp_state_free(new_ifps
);
747 unsigned short control
;
752 control
&= ~(FPC_PC
|FPC_RC
); /* Clear precision & rounding control */
753 control
|= (FPC_PC_64
| /* Set precision */
754 FPC_RC_RN
| /* round-to-nearest */
755 FPC_ZE
| /* Suppress zero-divide */
756 FPC_OE
| /* and overflow */
757 FPC_UE
| /* underflow */
758 FPC_IE
| /* Allow NaNQs and +-INF */
759 FPC_DE
| /* Allow denorms as operands */
760 FPC_PE
); /* No trap for precision loss */
763 /* Initialize SSE/SSE2 */
764 __builtin_ia32_ldmxcsr(0x1f80);
768 * Coprocessor not present.
777 struct x86_fx_thread_state
*ifps
= 0;
779 thr_act
= current_thread();
780 pcb
= THREAD_TO_PCB(thr_act
);
782 assert(fp_register_state_size
!= 0);
784 if (pcb
->ifps
== 0 && !get_interrupt_level()) {
785 ifps
= fp_state_alloc();
786 bcopy((char *)&initial_fp_state
, (char *)ifps
,
787 fp_register_state_size
);
788 if (!thread_is_64bit(thr_act
)) {
789 ifps
->fp_save_layout
= fpu_YMM_present
? XSAVE32
: FXSAVE32
;
792 ifps
->fp_save_layout
= fpu_YMM_present
? XSAVE64
: FXSAVE64
;
793 ifps
->fp_valid
= TRUE
;
795 intr
= ml_set_interrupts_enabled(FALSE
);
797 clear_ts(); /* Enable FPU use */
799 if (__improbable(get_interrupt_level())) {
801 * Save current coprocessor context if valid
802 * Initialize coprocessor live context
807 if (pcb
->ifps
== 0) {
812 * Load this thread`s state into coprocessor live context.
816 (void)ml_set_interrupts_enabled(intr
);
823 * FPU overran end of segment.
824 * Re-initialize FPU. Floating point state is not valid.
830 thread_t thr_act
= current_thread();
832 struct x86_fx_thread_state
*ifps
;
835 intr
= ml_set_interrupts_enabled(FALSE
);
837 if (get_interrupt_level())
838 panic("FPU segment overrun exception at interrupt context\n");
839 if (current_task() == kernel_task
)
840 panic("FPU segment overrun exception in kernel thread context\n");
843 * This is a non-recoverable error.
844 * Invalidate the thread`s FPU state.
846 pcb
= THREAD_TO_PCB(thr_act
);
847 simple_lock(&pcb
->lock
);
850 simple_unlock(&pcb
->lock
);
853 * Re-initialize the FPU.
859 * And disable access.
863 (void)ml_set_interrupts_enabled(intr
);
866 zfree(ifps_zone
, ifps
);
871 i386_exception(EXC_BAD_ACCESS
, VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
876 * FPU error. Called by AST.
882 thread_t thr_act
= current_thread();
883 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
886 intr
= ml_set_interrupts_enabled(FALSE
);
888 if (get_interrupt_level())
889 panic("FPU error exception at interrupt context\n");
890 if (current_task() == kernel_task
)
891 panic("FPU error exception in kernel thread context\n");
894 * Save the FPU state and turn off the FPU.
898 (void)ml_set_interrupts_enabled(intr
);
901 * Raise FPU exception.
902 * Locking not needed on pcb->ifps,
903 * since thread is running.
905 i386_exception(EXC_ARITHMETIC
,
915 * Locking not needed:
916 * . if called from fpu_get_state, pcb already locked.
917 * . if called from fpnoextflt or fp_intr, we are single-cpu
918 * . otherwise, thread is running.
919 * N.B.: Must be called with interrupts disabled
926 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
927 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
930 if (ifps
!= 0 && !ifps
->fp_valid
) {
931 assert((get_cr0() & CR0_TS
) == 0);
932 /* registers are in FPU */
933 ifps
->fp_valid
= TRUE
;
934 fpu_store_registers(ifps
, thread_is_64bit(thr_act
));
939 * Restore FPU state from PCB.
941 * Locking not needed; always called on the current thread.
948 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
949 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
952 assert(ifps
->fp_valid
== FALSE
|| ifps
->fp_valid
== TRUE
);
954 if (ifps
->fp_valid
== FALSE
) {
957 fpu_load_registers(ifps
);
959 ifps
->fp_valid
= FALSE
; /* in FPU */
963 * SSE arithmetic exception handling code.
964 * Basically the same as the x87 exception handler with a different subtype
970 thread_t thr_act
= current_thread();
971 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
974 intr
= ml_set_interrupts_enabled(FALSE
);
976 if (get_interrupt_level())
977 panic("SSE exception at interrupt context\n");
978 if (current_task() == kernel_task
)
979 panic("SSE exception in kernel thread context\n");
982 * Save the FPU state and turn off the FPU.
986 (void)ml_set_interrupts_enabled(intr
);
988 * Raise FPU exception.
989 * Locking not needed on pcb->ifps,
990 * since thread is running.
992 assert(ifps
->fp_save_layout
== FXSAVE32
|| ifps
->fp_save_layout
== FXSAVE64
);
993 i386_exception(EXC_ARITHMETIC
,
1000 fp_setvalid(boolean_t value
) {
1001 thread_t thr_act
= current_thread();
1002 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1005 ifps
->fp_valid
= value
;
1007 if (value
== TRUE
) {
1008 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1010 ml_set_interrupts_enabled(istate
);
1016 ml_fpu_avx_enabled(void) {
1017 return (fpu_YMM_present
== TRUE
);