]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/fpu.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / osfmk / i386 / fpu.c
1 /*
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1992-1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #include <platforms.h>
60
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>
65
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>
71 #include <kern/spl.h>
72 #include <kern/assert.h>
73
74 #include <libkern/OSAtomic.h>
75
76 #include <architecture/i386/pio.h>
77 #include <i386/cpuid.h>
78 #include <i386/fpu.h>
79 #include <i386/proc_reg.h>
80 #include <i386/misc_protos.h>
81 #include <i386/thread.h>
82 #include <i386/trap.h>
83
84 int fp_kind = FP_NO; /* not inited */
85 zone_t ifps_zone; /* zone for FPU save area */
86
87 #define ALIGNED(addr,size) (((uintptr_t)(addr)&((size)-1))==0)
88
89 /* Forward */
90
91 extern void fpinit(void);
92 extern void fp_save(
93 thread_t thr_act);
94 extern void fp_load(
95 thread_t thr_act);
96
97 static void configure_mxcsr_capability_mask(struct x86_avx_thread_state *fps);
98
99 struct x86_avx_thread_state initial_fp_state __attribute((aligned(64)));
100
101
102 /* Global MXCSR capability bitmask */
103 static unsigned int mxcsr_capability_mask;
104
105 #define fninit() \
106 __asm__ volatile("fninit")
107
108 #define fnstcw(control) \
109 __asm__("fnstcw %0" : "=m" (*(unsigned short *)(control)))
110
111 #define fldcw(control) \
112 __asm__ volatile("fldcw %0" : : "m" (*(unsigned short *) &(control)) )
113
114 #define fnclex() \
115 __asm__ volatile("fnclex")
116
117 #define fnsave(state) \
118 __asm__ volatile("fnsave %0" : "=m" (*state))
119
120 #define frstor(state) \
121 __asm__ volatile("frstor %0" : : "m" (state))
122
123 #define fwait() \
124 __asm__("fwait");
125
126 #define fxrstor(addr) __asm__ __volatile__("fxrstor %0" : : "m" (*(addr)))
127 #define fxsave(addr) __asm__ __volatile__("fxsave %0" : "=m" (*(addr)))
128
129 static uint32_t fp_register_state_size = 0;
130 static uint32_t fpu_YMM_present = FALSE;
131 static uint32_t cpuid_reevaluated = 0;
132
133 static void fpu_store_registers(void *, boolean_t);
134 static void fpu_load_registers(void *);
135
136 extern void xsave64o(void);
137 extern void xrstor64o(void);
138
139 #define XMASK ((uint32_t) (XFEM_X87 | XFEM_SSE | XFEM_YMM))
140
141 /* DRK: TODO replace opcodes with mnemonics when assembler support available */
142
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));
145 }
146
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));
150 }
151
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));
155 }
156
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));
160 }
161
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));
165 }
166
167 static inline unsigned short
168 fnstsw(void)
169 {
170 unsigned short status;
171 __asm__ volatile("fnstsw %0" : "=ma" (status));
172 return(status);
173 }
174
175 /*
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.
180 */
181
182 static void
183 configure_mxcsr_capability_mask(struct x86_avx_thread_state *fps)
184 {
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));
189
190 fpinit();
191 fpu_store_registers(fps, FALSE);
192
193 mxcsr_capability_mask = fps->fx_MXCSR_MASK;
194
195 /* Set default mask value if necessary */
196 if (mxcsr_capability_mask == 0)
197 mxcsr_capability_mask = 0xffbf;
198
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));
202
203 fps->fp_valid = TRUE;
204 fps->fp_save_layout = fpu_YMM_present ? XSAVE32: FXSAVE32;
205 fpu_load_registers(fps);
206
207 /* Poison values to trap unsafe usage */
208 fps->fp_valid = 0xFFFFFFFF;
209 fps->fp_save_layout = FP_UNUSED;
210
211 /* Re-enable FPU/SSE DNA exceptions */
212 set_ts();
213 }
214
215
216 /*
217 * Look for FPU and initialize it.
218 * Called on each CPU.
219 */
220 void
221 init_fpu(void)
222 {
223 #if DEBUG
224 unsigned short status;
225 unsigned short control;
226 #endif
227 /*
228 * Check for FPU by initializing it,
229 * then trying to read the correct bit patterns from
230 * the control and status registers.
231 */
232 set_cr0((get_cr0() & ~(CR0_EM|CR0_TS)) | CR0_NE); /* allow use of FPU */
233 fninit();
234 #if DEBUG
235 status = fnstsw();
236 fnstcw(&control);
237
238 assert(((status & 0xff) == 0) && ((control & 0x103f) == 0x3f));
239 #endif
240 /* Advertise SSE support */
241 if (cpuid_features() & CPUID_FEATURE_FXSR) {
242 fp_kind = FP_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);
247 }
248 fp_register_state_size = sizeof(struct x86_fx_thread_state);
249
250 } else
251 panic("fpu is not FP_FXSR");
252
253 /* Configure the XSAVE context mechanism if the processor supports
254 * AVX/YMM registers
255 */
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);
265 xsetbv(0, XMASK);
266 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
267 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated))
268 cpuid_set_info();
269 /* DRK: consider verifying AVX offset with cpuid(d, ECX:2) */
270 }
271 }
272 else
273 fpu_YMM_present = FALSE;
274
275 fpinit();
276
277 /*
278 * Trap wait instructions. Turn off FPU for now.
279 */
280 set_cr0(get_cr0() | CR0_TS | CR0_MP);
281 }
282
283 /*
284 * Allocate and initialize FP state for current thread.
285 * Don't load state.
286 */
287 static void *
288 fp_state_alloc(void)
289 {
290 void *ifps = zalloc(ifps_zone);
291
292 #if DEBUG
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);
295 }
296 #endif
297 return ifps;
298 }
299
300 static inline void
301 fp_state_free(void *ifps)
302 {
303 zfree(ifps_zone, ifps);
304 }
305
306 void clear_fpu(void)
307 {
308 set_ts();
309 }
310
311
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;
315
316 assert(layout == FXSAVE32 || layout == FXSAVE64 || layout == XSAVE32 || layout == XSAVE64);
317 assert(ALIGNED(ifps, 64));
318 assert(ml_get_interrupts_enabled() == FALSE);
319
320 #if DEBUG
321 if (layout == XSAVE32 || layout == XSAVE64) {
322 struct x86_avx_thread_state *iavx = fstate;
323 unsigned i;
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");
330 }
331 if (fpu_YMM_present) {
332 if (layout != XSAVE32 && layout != XSAVE64)
333 panic("Inappropriate layout: %u\n", layout);
334 }
335 #endif /* DEBUG */
336
337 #if defined(__i386__)
338 if (layout == FXSAVE32) {
339 /* Restore the compatibility/legacy mode XMM+x87 state */
340 fxrstor(ifps);
341 }
342 else if (layout == FXSAVE64) {
343 fxrstor64(ifps);
344 }
345 else if (layout == XSAVE32) {
346 xrstor(ifps);
347 }
348 else if (layout == XSAVE64) {
349 xrstor64(ifps);
350 }
351 #elif defined(__x86_64__)
352 if ((layout == XSAVE64) || (layout == XSAVE32))
353 xrstor(ifps);
354 else
355 fxrstor(ifps);
356 #endif
357 }
358
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__)
363 if (!is64) {
364 if (fpu_YMM_present) {
365 xsave(ifps);
366 ifps->fp_save_layout = XSAVE32;
367 }
368 else {
369 /* save the compatibility/legacy mode XMM+x87 state */
370 fxsave(ifps);
371 ifps->fp_save_layout = FXSAVE32;
372 }
373 }
374 else {
375 if (fpu_YMM_present) {
376 xsave64(ifps);
377 ifps->fp_save_layout = XSAVE64;
378 }
379 else {
380 fxsave64(ifps);
381 ifps->fp_save_layout = FXSAVE64;
382 }
383 }
384 #elif defined(__x86_64__)
385 if (fpu_YMM_present) {
386 xsave(ifps);
387 ifps->fp_save_layout = is64 ? XSAVE64 : XSAVE32;
388 }
389 else {
390 fxsave(ifps);
391 ifps->fp_save_layout = is64 ? FXSAVE64 : FXSAVE32;
392 }
393 #endif
394 }
395
396 /*
397 * Initialize FP handling.
398 */
399
400 void
401 fpu_module_init(void)
402 {
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);
406
407 assert(fpu_YMM_present != 0xFFFFFFFF);
408
409 /* We explicitly choose an allocation size of 64
410 * to eliminate waste for the 832 byte sized
411 * AVX XSAVE register save area.
412 */
413 ifps_zone = zinit(fp_register_state_size,
414 thread_max * fp_register_state_size,
415 64 * fp_register_state_size,
416 "x86 fpsave state");
417
418 #if ZONE_DEBUG
419 /* To maintain the required alignment, disable
420 * zone debugging for this zone as that appends
421 * 16 bytes to each element.
422 */
423 zone_debug_disable(ifps_zone);
424 #endif
425 /* Determine MXCSR reserved bits and configure initial FPU state*/
426 configure_mxcsr_capability_mask(&initial_fp_state);
427 }
428
429 /*
430 * Save thread`s FPU context.
431 */
432 void
433 fpu_save_context(thread_t thread)
434 {
435 struct x86_fx_thread_state *ifps;
436
437 assert(ml_get_interrupts_enabled() == FALSE);
438 ifps = (thread)->machine.ifps;
439 #if DEBUG
440 if (ifps && ((ifps->fp_valid != FALSE) && (ifps->fp_valid != TRUE))) {
441 panic("ifps->fp_valid: %u\n", ifps->fp_valid);
442 }
443 #endif
444 if (ifps != 0 && (ifps->fp_valid == FALSE)) {
445 /* Clear CR0.TS in preparation for the FP context save. In
446 * theory, this shouldn't be necessary since a live FPU should
447 * indicate that TS is clear. However, various routines
448 * (such as sendsig & sigreturn) manipulate TS directly.
449 */
450 clear_ts();
451 /* registers are in FPU - save to memory */
452 fpu_store_registers(ifps, (thread_is_64bit(thread) && is_saved_state64(thread->machine.iss)));
453 ifps->fp_valid = TRUE;
454 }
455 set_ts();
456 }
457
458
459 /*
460 * Free a FPU save area.
461 * Called only when thread terminating - no locking necessary.
462 */
463 void
464 fpu_free(void *fps)
465 {
466 fp_state_free(fps);
467 }
468
469 /*
470 * Set the floating-point state for a thread based
471 * on the FXSave formatted data. This is basically
472 * the same as fpu_set_state except it uses the
473 * expanded data structure.
474 * If the thread is not the current thread, it is
475 * not running (held). Locking needed against
476 * concurrent fpu_set_state or fpu_get_state.
477 */
478 kern_return_t
479 fpu_set_fxstate(
480 thread_t thr_act,
481 thread_state_t tstate,
482 thread_flavor_t f)
483 {
484 struct x86_fx_thread_state *ifps;
485 struct x86_fx_thread_state *new_ifps;
486 x86_float_state64_t *state;
487 pcb_t pcb;
488 size_t state_size = sizeof(struct x86_fx_thread_state);
489 boolean_t old_valid;
490 if (fp_kind == FP_NO)
491 return KERN_FAILURE;
492
493 state = (x86_float_state64_t *)tstate;
494
495 assert(thr_act != THREAD_NULL);
496 pcb = THREAD_TO_PCB(thr_act);
497
498 if (state == NULL) {
499 /*
500 * new FPU state is 'invalid'.
501 * Deallocate the fp state if it exists.
502 */
503 simple_lock(&pcb->lock);
504
505 ifps = pcb->ifps;
506 pcb->ifps = 0;
507
508 simple_unlock(&pcb->lock);
509
510 if (ifps != 0)
511 fp_state_free(ifps);
512 } else {
513 /*
514 * Valid state. Allocate the fp state if there is none.
515 */
516 new_ifps = 0;
517 Retry:
518 simple_lock(&pcb->lock);
519
520 ifps = pcb->ifps;
521 if (ifps == 0) {
522 if (new_ifps == 0) {
523 simple_unlock(&pcb->lock);
524 new_ifps = fp_state_alloc();
525 goto Retry;
526 }
527 ifps = new_ifps;
528 new_ifps = 0;
529 pcb->ifps = ifps;
530 }
531 /*
532 * now copy over the new data.
533 */
534 old_valid = ifps->fp_valid;
535
536 #if DEBUG
537 if ((old_valid == FALSE) && (thr_act != current_thread())) {
538 panic("fpu_set_fxstate inconsistency, thread: %p not stopped", thr_act);
539 }
540 #endif
541 /*
542 * Clear any reserved bits in the MXCSR to prevent a GPF
543 * when issuing an FXRSTOR.
544 */
545
546 state->fpu_mxcsr &= mxcsr_capability_mask;
547
548 bcopy((char *)&state->fpu_fcw, (char *)ifps, state_size);
549
550 if (fpu_YMM_present) {
551 struct x86_avx_thread_state *iavx = (void *) ifps;
552 uint32_t fpu_nyreg = 0;
553
554 if (f == x86_AVX_STATE32)
555 fpu_nyreg = 8;
556 else if (f == x86_AVX_STATE64)
557 fpu_nyreg = 16;
558
559 if (fpu_nyreg) {
560 x86_avx_state64_t *ystate = (x86_avx_state64_t *) state;
561 bcopy(&ystate->__fpu_ymmh0, &iavx->x_YMMH_reg[0][0], fpu_nyreg * sizeof(_STRUCT_XMM_REG));
562 }
563
564 iavx->fp_save_layout = thread_is_64bit(thr_act) ? XSAVE64 : XSAVE32;
565 /* Sanitize XSAVE header */
566 bzero(&iavx->_xh.xhrsvd[0], sizeof(iavx->_xh.xhrsvd));
567 if (state_size == sizeof(struct x86_avx_thread_state))
568 iavx->_xh.xsbv = (XFEM_YMM | XFEM_SSE | XFEM_X87);
569 else
570 iavx->_xh.xsbv = (XFEM_SSE | XFEM_X87);
571 }
572 else
573 ifps->fp_save_layout = thread_is_64bit(thr_act) ? FXSAVE64 : FXSAVE32;
574 ifps->fp_valid = old_valid;
575
576 if (old_valid == FALSE) {
577 boolean_t istate = ml_set_interrupts_enabled(FALSE);
578 ifps->fp_valid = TRUE;
579 set_ts();
580 ml_set_interrupts_enabled(istate);
581 }
582
583 simple_unlock(&pcb->lock);
584
585 if (new_ifps != 0)
586 fp_state_free(new_ifps);
587 }
588 return KERN_SUCCESS;
589 }
590
591 /*
592 * Get the floating-point state for a thread.
593 * If the thread is not the current thread, it is
594 * not running (held). Locking needed against
595 * concurrent fpu_set_state or fpu_get_state.
596 */
597 kern_return_t
598 fpu_get_fxstate(
599 thread_t thr_act,
600 thread_state_t tstate,
601 thread_flavor_t f)
602 {
603 struct x86_fx_thread_state *ifps;
604 x86_float_state64_t *state;
605 kern_return_t ret = KERN_FAILURE;
606 pcb_t pcb;
607 size_t state_size = sizeof(struct x86_fx_thread_state);
608
609 if (fp_kind == FP_NO)
610 return KERN_FAILURE;
611
612 state = (x86_float_state64_t *)tstate;
613
614 assert(thr_act != THREAD_NULL);
615 pcb = THREAD_TO_PCB(thr_act);
616
617 simple_lock(&pcb->lock);
618
619 ifps = pcb->ifps;
620 if (ifps == 0) {
621 /*
622 * No valid floating-point state.
623 */
624
625 bcopy((char *)&initial_fp_state, (char *)&state->fpu_fcw,
626 state_size);
627
628 simple_unlock(&pcb->lock);
629
630 return KERN_SUCCESS;
631 }
632 /*
633 * Make sure we`ve got the latest fp state info
634 * If the live fpu state belongs to our target
635 */
636 if (thr_act == current_thread()) {
637 boolean_t intr;
638
639 intr = ml_set_interrupts_enabled(FALSE);
640
641 clear_ts();
642 fp_save(thr_act);
643 clear_fpu();
644
645 (void)ml_set_interrupts_enabled(intr);
646 }
647 if (ifps->fp_valid) {
648 bcopy((char *)ifps, (char *)&state->fpu_fcw, state_size);
649 if (fpu_YMM_present) {
650 struct x86_avx_thread_state *iavx = (void *) ifps;
651 uint32_t fpu_nyreg = 0;
652
653 if (f == x86_AVX_STATE32)
654 fpu_nyreg = 8;
655 else if (f == x86_AVX_STATE64)
656 fpu_nyreg = 16;
657
658 if (fpu_nyreg) {
659 x86_avx_state64_t *ystate = (x86_avx_state64_t *) state;
660 bcopy(&iavx->x_YMMH_reg[0][0], &ystate->__fpu_ymmh0, fpu_nyreg * sizeof(_STRUCT_XMM_REG));
661 }
662 }
663
664 ret = KERN_SUCCESS;
665 }
666 simple_unlock(&pcb->lock);
667
668 return ret;
669 }
670
671
672
673 /*
674 * the child thread is 'stopped' with the thread
675 * mutex held and is currently not known by anyone
676 * so no way for fpu state to get manipulated by an
677 * outside agency -> no need for pcb lock
678 */
679
680 void
681 fpu_dup_fxstate(
682 thread_t parent,
683 thread_t child)
684 {
685 struct x86_fx_thread_state *new_ifps = NULL;
686 boolean_t intr;
687 pcb_t ppcb;
688
689 ppcb = THREAD_TO_PCB(parent);
690
691 if (ppcb->ifps == NULL)
692 return;
693
694 if (child->machine.ifps)
695 panic("fpu_dup_fxstate: child's ifps non-null");
696
697 new_ifps = fp_state_alloc();
698
699 simple_lock(&ppcb->lock);
700
701 if (ppcb->ifps != NULL) {
702 struct x86_fx_thread_state *ifps = ppcb->ifps;
703 /*
704 * Make sure we`ve got the latest fp state info
705 */
706 intr = ml_set_interrupts_enabled(FALSE);
707 assert(current_thread() == parent);
708 clear_ts();
709 fp_save(parent);
710 clear_fpu();
711
712 (void)ml_set_interrupts_enabled(intr);
713
714 if (ifps->fp_valid) {
715 child->machine.ifps = new_ifps;
716 assert((fp_register_state_size == sizeof(struct x86_fx_thread_state)) ||
717 (fp_register_state_size == sizeof(struct x86_avx_thread_state)));
718 bcopy((char *)(ppcb->ifps),
719 (char *)(child->machine.ifps), fp_register_state_size);
720
721 /* Mark the new fp saved state as non-live. */
722 /* Temporarily disabled: radar 4647827
723 * new_ifps->fp_valid = TRUE;
724 */
725
726 /*
727 * Clear any reserved bits in the MXCSR to prevent a GPF
728 * when issuing an FXRSTOR.
729 */
730 new_ifps->fx_MXCSR &= mxcsr_capability_mask;
731 new_ifps = NULL;
732 }
733 }
734 simple_unlock(&ppcb->lock);
735
736 if (new_ifps != NULL)
737 fp_state_free(new_ifps);
738 }
739
740
741 /*
742 * Initialize FPU.
743 *
744 */
745
746 void
747 fpinit(void)
748 {
749 unsigned short control;
750
751 clear_ts();
752 fninit();
753 fnstcw(&control);
754 control &= ~(FPC_PC|FPC_RC); /* Clear precision & rounding control */
755 control |= (FPC_PC_64 | /* Set precision */
756 FPC_RC_RN | /* round-to-nearest */
757 FPC_ZE | /* Suppress zero-divide */
758 FPC_OE | /* and overflow */
759 FPC_UE | /* underflow */
760 FPC_IE | /* Allow NaNQs and +-INF */
761 FPC_DE | /* Allow denorms as operands */
762 FPC_PE); /* No trap for precision loss */
763 fldcw(control);
764
765 /* Initialize SSE/SSE2 */
766 __builtin_ia32_ldmxcsr(0x1f80);
767 }
768
769 /*
770 * Coprocessor not present.
771 */
772
773 void
774 fpnoextflt(void)
775 {
776 boolean_t intr;
777 thread_t thr_act;
778 pcb_t pcb;
779 struct x86_fx_thread_state *ifps = 0;
780
781 thr_act = current_thread();
782 pcb = THREAD_TO_PCB(thr_act);
783
784 assert(fp_register_state_size != 0);
785
786 if (pcb->ifps == 0 && !get_interrupt_level()) {
787 ifps = fp_state_alloc();
788 bcopy((char *)&initial_fp_state, (char *)ifps,
789 fp_register_state_size);
790 if (!thread_is_64bit(thr_act)) {
791 ifps->fp_save_layout = fpu_YMM_present ? XSAVE32 : FXSAVE32;
792 }
793 else
794 ifps->fp_save_layout = fpu_YMM_present ? XSAVE64 : FXSAVE64;
795 ifps->fp_valid = TRUE;
796 }
797 intr = ml_set_interrupts_enabled(FALSE);
798
799 clear_ts(); /* Enable FPU use */
800
801 if (__improbable(get_interrupt_level())) {
802 /*
803 * Save current coprocessor context if valid
804 * Initialize coprocessor live context
805 */
806 fp_save(thr_act);
807 fpinit();
808 } else {
809 if (pcb->ifps == 0) {
810 pcb->ifps = ifps;
811 ifps = 0;
812 }
813 /*
814 * Load this thread`s state into coprocessor live context.
815 */
816 fp_load(thr_act);
817 }
818 (void)ml_set_interrupts_enabled(intr);
819
820 if (ifps)
821 fp_state_free(ifps);
822 }
823
824 /*
825 * FPU overran end of segment.
826 * Re-initialize FPU. Floating point state is not valid.
827 */
828
829 void
830 fpextovrflt(void)
831 {
832 thread_t thr_act = current_thread();
833 pcb_t pcb;
834 struct x86_fx_thread_state *ifps;
835 boolean_t intr;
836
837 intr = ml_set_interrupts_enabled(FALSE);
838
839 if (get_interrupt_level())
840 panic("FPU segment overrun exception at interrupt context\n");
841 if (current_task() == kernel_task)
842 panic("FPU segment overrun exception in kernel thread context\n");
843
844 /*
845 * This is a non-recoverable error.
846 * Invalidate the thread`s FPU state.
847 */
848 pcb = THREAD_TO_PCB(thr_act);
849 simple_lock(&pcb->lock);
850 ifps = pcb->ifps;
851 pcb->ifps = 0;
852 simple_unlock(&pcb->lock);
853
854 /*
855 * Re-initialize the FPU.
856 */
857 clear_ts();
858 fninit();
859
860 /*
861 * And disable access.
862 */
863 clear_fpu();
864
865 (void)ml_set_interrupts_enabled(intr);
866
867 if (ifps)
868 zfree(ifps_zone, ifps);
869
870 /*
871 * Raise exception.
872 */
873 i386_exception(EXC_BAD_ACCESS, VM_PROT_READ|VM_PROT_EXECUTE, 0);
874 /*NOTREACHED*/
875 }
876
877 /*
878 * FPU error. Called by AST.
879 */
880
881 void
882 fpexterrflt(void)
883 {
884 thread_t thr_act = current_thread();
885 struct x86_fx_thread_state *ifps = thr_act->machine.ifps;
886 boolean_t intr;
887
888 intr = ml_set_interrupts_enabled(FALSE);
889
890 if (get_interrupt_level())
891 panic("FPU error exception at interrupt context\n");
892 if (current_task() == kernel_task)
893 panic("FPU error exception in kernel thread context\n");
894
895 /*
896 * Save the FPU state and turn off the FPU.
897 */
898 fp_save(thr_act);
899
900 (void)ml_set_interrupts_enabled(intr);
901
902 /*
903 * Raise FPU exception.
904 * Locking not needed on pcb->ifps,
905 * since thread is running.
906 */
907 i386_exception(EXC_ARITHMETIC,
908 EXC_I386_EXTERR,
909 ifps->fx_status);
910
911 /*NOTREACHED*/
912 }
913
914 /*
915 * Save FPU state.
916 *
917 * Locking not needed:
918 * . if called from fpu_get_state, pcb already locked.
919 * . if called from fpnoextflt or fp_intr, we are single-cpu
920 * . otherwise, thread is running.
921 * N.B.: Must be called with interrupts disabled
922 */
923
924 void
925 fp_save(
926 thread_t thr_act)
927 {
928 pcb_t pcb = THREAD_TO_PCB(thr_act);
929 struct x86_fx_thread_state *ifps = pcb->ifps;
930
931 assert(ifps != 0);
932 if (ifps != 0 && !ifps->fp_valid) {
933 assert((get_cr0() & CR0_TS) == 0);
934 /* registers are in FPU */
935 ifps->fp_valid = TRUE;
936 fpu_store_registers(ifps, thread_is_64bit(thr_act));
937 }
938 }
939
940 /*
941 * Restore FPU state from PCB.
942 *
943 * Locking not needed; always called on the current thread.
944 */
945
946 void
947 fp_load(
948 thread_t thr_act)
949 {
950 pcb_t pcb = THREAD_TO_PCB(thr_act);
951 struct x86_fx_thread_state *ifps = pcb->ifps;
952
953 assert(ifps);
954 assert(ifps->fp_valid == FALSE || ifps->fp_valid == TRUE);
955
956 if (ifps->fp_valid == FALSE) {
957 fpinit();
958 } else {
959 fpu_load_registers(ifps);
960 }
961 ifps->fp_valid = FALSE; /* in FPU */
962 }
963
964 /*
965 * SSE arithmetic exception handling code.
966 * Basically the same as the x87 exception handler with a different subtype
967 */
968
969 void
970 fpSSEexterrflt(void)
971 {
972 thread_t thr_act = current_thread();
973 struct x86_fx_thread_state *ifps = thr_act->machine.ifps;
974 boolean_t intr;
975
976 intr = ml_set_interrupts_enabled(FALSE);
977
978 if (get_interrupt_level())
979 panic("SSE exception at interrupt context\n");
980 if (current_task() == kernel_task)
981 panic("SSE exception in kernel thread context\n");
982
983 /*
984 * Save the FPU state and turn off the FPU.
985 */
986 fp_save(thr_act);
987
988 (void)ml_set_interrupts_enabled(intr);
989 /*
990 * Raise FPU exception.
991 * Locking not needed on pcb->ifps,
992 * since thread is running.
993 */
994 assert(ifps->fp_save_layout == FXSAVE32 || ifps->fp_save_layout == FXSAVE64);
995 i386_exception(EXC_ARITHMETIC,
996 EXC_I386_SSEEXTERR,
997 ifps->fx_MXCSR);
998 /*NOTREACHED*/
999 }
1000
1001 void
1002 fp_setvalid(boolean_t value) {
1003 thread_t thr_act = current_thread();
1004 struct x86_fx_thread_state *ifps = thr_act->machine.ifps;
1005
1006 if (ifps) {
1007 ifps->fp_valid = value;
1008
1009 if (value == TRUE) {
1010 boolean_t istate = ml_set_interrupts_enabled(FALSE);
1011 clear_fpu();
1012 ml_set_interrupts_enabled(istate);
1013 }
1014 }
1015 }
1016
1017 __private_extern__ boolean_t
1018 ml_fpu_avx_enabled(void) {
1019 return (fpu_YMM_present == TRUE);
1020 }