2 * Copyright (c) 2000-2018 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.
57 #include <mach/exception_types.h>
58 #include <mach/i386/thread_status.h>
59 #include <mach/i386/fp_reg.h>
61 #include <kern/mach_param.h>
62 #include <kern/processor.h>
63 #include <kern/thread.h>
64 #include <kern/zalloc.h>
65 #include <kern/misc_protos.h>
67 #include <kern/assert.h>
69 #include <libkern/OSAtomic.h>
71 #include <architecture/i386/pio.h>
72 #include <i386/cpuid.h>
74 #include <i386/proc_reg.h>
75 #include <i386/misc_protos.h>
76 #include <i386/thread.h>
77 #include <i386/trap.h>
79 xstate_t fpu_capability
= UNDEFINED
; /* extended state capability */
80 xstate_t fpu_default
= UNDEFINED
; /* default extended state */
82 #define ALIGNED(addr, size) (((uintptr_t)(addr)&((size)-1))==0)
86 extern void fpinit(void);
92 static void configure_mxcsr_capability_mask(x86_ext_thread_state_t
*fps
);
93 static xstate_t
thread_xstate(thread_t
);
95 x86_ext_thread_state_t initial_fp_state
__attribute((aligned(64)));
96 x86_ext_thread_state_t default_avx512_state
__attribute((aligned(64)));
97 x86_ext_thread_state_t default_avx_state
__attribute((aligned(64)));
98 x86_ext_thread_state_t default_fx_state
__attribute((aligned(64)));
100 /* Global MXCSR capability bitmask */
101 static unsigned int mxcsr_capability_mask
;
104 __asm__ volatile("fninit")
106 #define fnstcw(control) \
107 __asm__("fnstcw %0" : "=m" (*(unsigned short *)(control)))
109 #define fldcw(control) \
110 __asm__ volatile("fldcw %0" : : "m" (*(unsigned short *) &(control)) )
113 __asm__ volatile("fnclex")
115 #define fnsave(state) \
116 __asm__ volatile("fnsave %0" : "=m" (*state))
118 #define frstor(state) \
119 __asm__ volatile("frstor %0" : : "m" (state))
125 fxrstor(struct x86_fx_thread_state
*a
)
127 __asm__
__volatile__ ("fxrstor %0" :: "m" (*a
));
131 fxsave(struct x86_fx_thread_state
*a
)
133 __asm__
__volatile__ ("fxsave %0" : "=m" (*a
));
137 fxrstor64(struct x86_fx_thread_state
*a
)
139 __asm__
__volatile__ ("fxrstor64 %0" :: "m" (*a
));
143 fxsave64(struct x86_fx_thread_state
*a
)
145 __asm__
__volatile__ ("fxsave64 %0" : "=m" (*a
));
148 #if !defined(RC_HIDE_XNU_J137)
149 #define IS_VALID_XSTATE(x) ((x) == FP || (x) == AVX || (x) == AVX512)
151 #define IS_VALID_XSTATE(x) ((x) == FP || (x) == AVX)
154 zone_t ifps_zone
[] = {
157 #if !defined(RC_HIDE_XNU_J137)
161 static uint32_t fp_state_size
[] = {
162 [FP
] = sizeof(struct x86_fx_thread_state
),
163 [AVX
] = sizeof(struct x86_avx_thread_state
),
164 #if !defined(RC_HIDE_XNU_J137)
165 [AVX512
] = sizeof(struct x86_avx512_thread_state
)
169 static const char *xstate_name
[] = {
170 [UNDEFINED
] = "UNDEFINED",
173 #if !defined(RC_HIDE_XNU_J137)
178 #if !defined(RC_HIDE_XNU_J137)
179 #define fpu_ZMM_capable (fpu_capability == AVX512)
180 #define fpu_YMM_capable (fpu_capability == AVX || fpu_capability == AVX512)
182 * On-demand AVX512 support
183 * ------------------------
184 * On machines with AVX512 support, by default, threads are created with
185 * AVX512 masked off in XCR0 and an AVX-sized savearea is used. However, AVX512
186 * capabilities are advertised in the commpage and via sysctl. If a thread
187 * opts to use AVX512 instructions, the first will result in a #UD exception.
188 * Faulting AVX512 intructions are recognizable by their unique prefix.
189 * This exception results in the thread being promoted to use an AVX512-sized
190 * savearea and for the AVX512 bit masks being set in its XCR0. The faulting
191 * instruction is re-driven and the thread can proceed to perform AVX512
194 * In addition to AVX512 instructions causing promotion, the thread_set_state()
195 * primitive with an AVX512 state flavor result in promotion.
197 * AVX512 promotion of the first thread in a task causes the default xstate
198 * of the task to be promoted so that any subsequently created or subsequently
199 * DNA-faulted thread will have AVX512 xstate and it will not need to fault-in
202 * Two savearea zones are used: the default pool of AVX-sized (832 byte) areas
203 * and a second pool of larger AVX512-sized (2688 byte) areas.
205 * Note the initial state value is an AVX512 object but that the AVX initial
206 * value is a subset of it.
209 #define fpu_YMM_capable (fpu_capability == AVX)
211 static uint32_t cpuid_reevaluated
= 0;
213 static void fpu_store_registers(void *, boolean_t
);
214 static void fpu_load_registers(void *);
216 #if !defined(RC_HIDE_XNU_J137)
217 static const uint32_t xstate_xmask
[] = {
220 [AVX512
] = AVX512_XMASK
223 static const uint32_t xstate_xmask
[] = {
230 xsave(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
232 __asm__
__volatile__ ("xsave %0" :"=m" (*a
) : "a"(rfbm
), "d"(0));
236 xsave64(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
238 __asm__
__volatile__ ("xsave64 %0" :"=m" (*a
) : "a"(rfbm
), "d"(0));
242 xrstor(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
244 __asm__
__volatile__ ("xrstor %0" :: "m" (*a
), "a"(rfbm
), "d"(0));
248 xrstor64(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
250 __asm__
__volatile__ ("xrstor64 %0" :: "m" (*a
), "a"(rfbm
), "d"(0));
253 #if !defined(RC_HIDE_XNU_J137)
254 __unused
static inline void
257 __asm__
__volatile__ ("vzeroupper" ::);
260 static boolean_t
fpu_thread_promote_avx512(thread_t
); /* Forward */
263 * Define a wrapper for bcopy to defeat destination size checka.
264 * This is needed to treat repeated objects such as
265 * _STRUCT_XMM_REG fpu_ymmh0;
267 * _STRUCT_XMM_REG fpu_ymmh7;
268 * as an array and to copy like so:
269 * bcopy_nockch(src,&dst->fpu_ymmh0,8*sizeof(_STRUCT_XMM_REG));
270 * without the compiler throwing a __builtin__memmove_chk error.
273 bcopy_nochk(void *_src
, void *_dst
, size_t _len
)
275 bcopy(_src
, _dst
, _len
);
279 * Furthermore, make compile-time asserts that no padding creeps into structures
280 * for which we're doing this.
282 #define ASSERT_PACKED(t, m1, m2, n, mt) \
283 extern char assert_packed_ ## t ## _ ## m1 ## _ ## m2 \
284 [(offsetof(t,m2) - offsetof(t,m1) == (n - 1)*sizeof(mt)) ? 1 : -1]
286 ASSERT_PACKED(x86_avx_state32_t
, fpu_ymmh0
, fpu_ymmh7
, 8, _STRUCT_XMM_REG
);
288 ASSERT_PACKED(x86_avx_state64_t
, fpu_ymmh0
, fpu_ymmh15
, 16, _STRUCT_XMM_REG
);
290 ASSERT_PACKED(x86_avx512_state32_t
, fpu_k0
, fpu_k7
, 8, _STRUCT_OPMASK_REG
);
291 ASSERT_PACKED(x86_avx512_state32_t
, fpu_ymmh0
, fpu_ymmh7
, 8, _STRUCT_XMM_REG
);
292 ASSERT_PACKED(x86_avx512_state32_t
, fpu_zmmh0
, fpu_zmmh7
, 8, _STRUCT_YMM_REG
);
294 ASSERT_PACKED(x86_avx512_state64_t
, fpu_k0
, fpu_k7
, 8, _STRUCT_OPMASK_REG
);
295 ASSERT_PACKED(x86_avx512_state64_t
, fpu_ymmh0
, fpu_ymmh15
, 16, _STRUCT_XMM_REG
);
296 ASSERT_PACKED(x86_avx512_state64_t
, fpu_zmmh0
, fpu_zmmh15
, 16, _STRUCT_YMM_REG
);
297 ASSERT_PACKED(x86_avx512_state64_t
, fpu_zmm16
, fpu_zmm31
, 16, _STRUCT_ZMM_REG
);
299 #if defined(DEBUG_AVX512)
301 #define DBG(x...) kprintf("DBG: " x)
303 typedef struct { uint8_t byte
[8]; } opmask_t
;
304 typedef struct { uint8_t byte
[16]; } xmm_t
;
305 typedef struct { uint8_t byte
[32]; } ymm_t
;
306 typedef struct { uint8_t byte
[64]; } zmm_t
;
309 DBG_AVX512_STATE(struct x86_avx512_thread_state
*sp
)
312 xmm_t
*xmm
= (xmm_t
*) &sp
->fp
.fx_XMM_reg
;
313 xmm_t
*ymmh
= (xmm_t
*) &sp
->x_YMM_Hi128
;
314 ymm_t
*zmmh
= (ymm_t
*) &sp
->x_ZMM_Hi256
;
315 zmm_t
*zmm
= (zmm_t
*) &sp
->x_Hi16_ZMM
;
316 opmask_t
*k
= (opmask_t
*) &sp
->x_Opmask
;
318 kprintf("x_YMM_Hi128: %lu\n", offsetof(struct x86_avx512_thread_state
, x_YMM_Hi128
));
319 kprintf("x_Opmask: %lu\n", offsetof(struct x86_avx512_thread_state
, x_Opmask
));
320 kprintf("x_ZMM_Hi256: %lu\n", offsetof(struct x86_avx512_thread_state
, x_ZMM_Hi256
));
321 kprintf("x_Hi16_ZMM: %lu\n", offsetof(struct x86_avx512_thread_state
, x_Hi16_ZMM
));
323 kprintf("XCR0: 0x%016llx\n", xgetbv(XCR0
));
324 kprintf("XINUSE: 0x%016llx\n", xgetbv(1));
326 /* Print all ZMM registers */
327 for (i
= 0; i
< 16; i
++) {
328 kprintf("zmm%d:\t0x", i
);
329 for (j
= 0; j
< 16; j
++) {
330 kprintf("%02x", xmm
[i
].byte
[j
]);
332 for (j
= 0; j
< 16; j
++) {
333 kprintf("%02x", ymmh
[i
].byte
[j
]);
335 for (j
= 0; j
< 32; j
++) {
336 kprintf("%02x", zmmh
[i
].byte
[j
]);
340 for (i
= 0; i
< 16; i
++) {
341 kprintf("zmm%d:\t0x", 16 + i
);
342 for (j
= 0; j
< 64; j
++) {
343 kprintf("%02x", zmm
[i
].byte
[j
]);
347 for (i
= 0; i
< 8; i
++) {
348 kprintf("k%d:\t0x", i
);
349 for (j
= 0; j
< 8; j
++) {
350 kprintf("%02x", k
[i
].byte
[j
]);
355 kprintf("xstate_bv: 0x%016llx\n", sp
->_xh
.xstate_bv
);
356 kprintf("xcomp_bv: 0x%016llx\n", sp
->_xh
.xcomp_bv
);
361 DBG_AVX512_STATE(__unused
struct x86_avx512_thread_state
*sp
)
365 #endif /* DEBUG_AVX512 */
370 static inline unsigned short
373 unsigned short status
;
374 __asm__
volatile ("fnstsw %0" : "=ma" (status
));
380 * Configure the initial FPU state presented to new threads.
381 * Determine the MXCSR capability mask, which allows us to mask off any
382 * potentially unsafe "reserved" bits before restoring the FPU context.
383 * *Not* per-cpu, assumes symmetry.
387 configure_mxcsr_capability_mask(x86_ext_thread_state_t
*fps
)
389 /* XSAVE requires a 64 byte aligned store */
390 assert(ALIGNED(fps
, 64));
391 /* Clear, to prepare for the diagnostic FXSAVE */
392 bzero(fps
, sizeof(*fps
));
395 fpu_store_registers(fps
, FALSE
);
397 mxcsr_capability_mask
= fps
->fx
.fx_MXCSR_MASK
;
399 /* Set default mask value if necessary */
400 if (mxcsr_capability_mask
== 0) {
401 mxcsr_capability_mask
= 0xffbf;
404 /* Clear vector register store */
405 bzero(&fps
->fx
.fx_XMM_reg
[0][0], sizeof(fps
->fx
.fx_XMM_reg
));
406 bzero(fps
->avx
.x_YMM_Hi128
, sizeof(fps
->avx
.x_YMM_Hi128
));
407 #if !defined(RC_HIDE_XNU_J137)
408 if (fpu_ZMM_capable
) {
409 bzero(fps
->avx512
.x_ZMM_Hi256
, sizeof(fps
->avx512
.x_ZMM_Hi256
));
410 bzero(fps
->avx512
.x_Hi16_ZMM
, sizeof(fps
->avx512
.x_Hi16_ZMM
));
411 bzero(fps
->avx512
.x_Opmask
, sizeof(fps
->avx512
.x_Opmask
));
415 fps
->fx
.fp_valid
= TRUE
;
416 fps
->fx
.fp_save_layout
= fpu_YMM_capable
? XSAVE32
: FXSAVE32
;
417 fpu_load_registers(fps
);
419 if (fpu_ZMM_capable
) {
420 xsave64((struct x86_fx_thread_state
*)&default_avx512_state
, xstate_xmask
[AVX512
]);
422 if (fpu_YMM_capable
) {
423 xsave64((struct x86_fx_thread_state
*)&default_avx_state
, xstate_xmask
[AVX
]);
425 fxsave64((struct x86_fx_thread_state
*)&default_fx_state
);
428 /* Poison values to trap unsafe usage */
429 fps
->fx
.fp_valid
= 0xFFFFFFFF;
430 fps
->fx
.fp_save_layout
= FP_UNUSED
;
432 /* Re-enable FPU/SSE DNA exceptions */
436 int fpsimd_fault_popc
= 0;
438 * Look for FPU and initialize it.
439 * Called on each CPU.
445 unsigned short status
;
446 unsigned short control
;
449 * Check for FPU by initializing it,
450 * then trying to read the correct bit patterns from
451 * the control and status registers.
453 set_cr0((get_cr0() & ~(CR0_EM
| CR0_TS
)) | CR0_NE
); /* allow use of FPU */
459 assert(((status
& 0xff) == 0) && ((control
& 0x103f) == 0x3f));
461 /* Advertise SSE support */
462 if (cpuid_features() & CPUID_FEATURE_FXSR
) {
463 set_cr4(get_cr4() | CR4_OSFXS
);
464 /* And allow SIMD exceptions if present */
465 if (cpuid_features() & CPUID_FEATURE_SSE
) {
466 set_cr4(get_cr4() | CR4_OSXMM
);
469 panic("fpu is not FP_FXSR");
472 fpu_capability
= fpu_default
= FP
;
474 PE_parse_boot_argn("fpsimd_fault_popc", &fpsimd_fault_popc
, sizeof(fpsimd_fault_popc
));
476 #if !defined(RC_HIDE_XNU_J137)
477 static boolean_t is_avx512_enabled
= TRUE
;
478 if (cpu_number() == master_cpu
) {
479 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_AVX512F
) {
480 PE_parse_boot_argn("avx512", &is_avx512_enabled
, sizeof(boolean_t
));
481 kprintf("AVX512 supported %s\n",
482 is_avx512_enabled
? "and enabled" : "but disabled");
487 /* Configure the XSAVE context mechanism if the processor supports
490 if (cpuid_features() & CPUID_FEATURE_XSAVE
) {
491 cpuid_xsave_leaf_t
*xs0p
= &cpuid_info()->cpuid_xsave_leaf
[0];
492 #if !defined(RC_HIDE_XNU_J137)
493 if (is_avx512_enabled
&&
494 (xs0p
->extended_state
[eax
] & XFEM_ZMM
) == XFEM_ZMM
) {
495 assert(xs0p
->extended_state
[eax
] & XFEM_SSE
);
496 assert(xs0p
->extended_state
[eax
] & XFEM_YMM
);
497 fpu_capability
= AVX512
;
498 /* XSAVE container size for all features */
499 set_cr4(get_cr4() | CR4_OSXSAVE
);
500 xsetbv(0, AVX512_XMASK
);
501 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
502 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated
)) {
505 /* Verify that now selected state can be accommodated */
506 assert(xs0p
->extended_state
[ebx
] == fp_state_size
[AVX512
]);
508 * AVX set until AVX512 is used.
509 * See comment above about on-demand AVX512 support.
511 xsetbv(0, AVX_XMASK
);
515 if (xs0p
->extended_state
[eax
] & XFEM_YMM
) {
516 assert(xs0p
->extended_state
[eax
] & XFEM_SSE
);
517 fpu_capability
= AVX
;
519 /* XSAVE container size for all features */
520 set_cr4(get_cr4() | CR4_OSXSAVE
);
521 xsetbv(0, AVX_XMASK
);
522 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
523 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated
)) {
526 /* Verify that now selected state can be accommodated */
527 assert(xs0p
->extended_state
[ebx
] == fp_state_size
[AVX
]);
531 if (cpu_number() == master_cpu
) {
532 kprintf("fpu_state: %s, state_size: %d\n",
533 xstate_name
[fpu_capability
],
534 fp_state_size
[fpu_capability
]);
538 current_cpu_datap()->cpu_xstate
= fpu_default
;
541 * Trap wait instructions. Turn off FPU for now.
543 set_cr0(get_cr0() | CR0_TS
| CR0_MP
);
547 * Allocate and initialize FP state for specified xstate.
551 fp_state_alloc(xstate_t xs
)
553 struct x86_fx_thread_state
*ifps
;
555 assert(ifps_zone
[xs
] != NULL
);
556 ifps
= zalloc(ifps_zone
[xs
]);
559 if (!(ALIGNED(ifps
, 64))) {
560 panic("fp_state_alloc: %p, %u, %p, %u",
561 ifps
, (unsigned) ifps_zone
[xs
]->elem_size
,
562 (void *) ifps_zone
[xs
]->free_elements
,
563 (unsigned) ifps_zone
[xs
]->alloc_size
);
566 bzero(ifps
, fp_state_size
[xs
]);
572 fp_state_free(void *ifps
, xstate_t xs
)
574 assert(ifps_zone
[xs
] != NULL
);
575 zfree(ifps_zone
[xs
], ifps
);
586 fpu_load_registers(void *fstate
)
588 struct x86_fx_thread_state
*ifps
= fstate
;
589 fp_save_layout_t layout
= ifps
->fp_save_layout
;
591 assert(current_task() == NULL
|| \
592 (thread_is_64bit_addr(current_thread()) ? \
593 (layout
== FXSAVE64
|| layout
== XSAVE64
) : \
594 (layout
== FXSAVE32
|| layout
== XSAVE32
)));
595 assert(ALIGNED(ifps
, 64));
596 assert(ml_get_interrupts_enabled() == FALSE
);
599 if (layout
== XSAVE32
|| layout
== XSAVE64
) {
600 struct x86_avx_thread_state
*iavx
= fstate
;
602 /* Verify reserved bits in the XSAVE header*/
603 if (iavx
->_xh
.xstate_bv
& ~xstate_xmask
[current_xstate()]) {
604 panic("iavx->_xh.xstate_bv: 0x%llx", iavx
->_xh
.xstate_bv
);
606 for (i
= 0; i
< sizeof(iavx
->_xh
.xhrsvd
); i
++) {
607 if (iavx
->_xh
.xhrsvd
[i
]) {
608 panic("Reserved bit set");
612 if (fpu_YMM_capable
) {
613 if (layout
!= XSAVE32
&& layout
!= XSAVE64
) {
614 panic("Inappropriate layout: %u\n", layout
);
627 xrstor64(ifps
, xstate_xmask
[current_xstate()]);
630 xrstor(ifps
, xstate_xmask
[current_xstate()]);
633 panic("fpu_load_registers() bad layout: %d\n", layout
);
638 fpu_store_registers(void *fstate
, boolean_t is64
)
640 struct x86_fx_thread_state
*ifps
= fstate
;
641 assert(ALIGNED(ifps
, 64));
642 xstate_t xs
= current_xstate();
647 ifps
->fp_save_layout
= FXSAVE64
;
650 ifps
->fp_save_layout
= FXSAVE32
;
654 #if !defined(RC_HIDE_XNU_J137)
658 xsave64(ifps
, xstate_xmask
[xs
]);
659 ifps
->fp_save_layout
= XSAVE64
;
661 xsave(ifps
, xstate_xmask
[xs
]);
662 ifps
->fp_save_layout
= XSAVE32
;
666 panic("fpu_store_registers() bad xstate: %d\n", xs
);
671 * Initialize FP handling.
675 fpu_module_init(void)
677 if (!IS_VALID_XSTATE(fpu_default
)) {
678 panic("fpu_module_init: invalid extended state %u\n",
682 /* We explicitly choose an allocation size of 13 pages = 64 * 832
683 * to eliminate waste for the 832 byte sized
684 * AVX XSAVE register save area.
686 ifps_zone
[fpu_default
] = zinit(fp_state_size
[fpu_default
],
687 thread_max
* fp_state_size
[fpu_default
],
688 64 * fp_state_size
[fpu_default
],
691 /* To maintain the required alignment, disable
692 * zone debugging for this zone as that appends
693 * 16 bytes to each element.
695 zone_change(ifps_zone
[fpu_default
], Z_ALIGNMENT_REQUIRED
, TRUE
);
697 #if !defined(RC_HIDE_XNU_J137)
699 * If AVX512 is supported, create a separate savearea zone.
700 * with allocation size: 19 pages = 32 * 2668
702 if (fpu_capability
== AVX512
) {
703 ifps_zone
[AVX512
] = zinit(fp_state_size
[AVX512
],
704 thread_max
* fp_state_size
[AVX512
],
705 32 * fp_state_size
[AVX512
],
706 "x86 avx512 save state");
707 zone_change(ifps_zone
[AVX512
], Z_ALIGNMENT_REQUIRED
, TRUE
);
711 /* Determine MXCSR reserved bits and configure initial FPU state*/
712 configure_mxcsr_capability_mask(&initial_fp_state
);
716 * Context switch fpu state.
717 * Always save old thread`s FPU context but don't load new .. allow that to fault-in.
718 * Switch to the new task's xstate.
722 fpu_switch_context(thread_t old
, thread_t
new)
724 struct x86_fx_thread_state
*ifps
;
725 cpu_data_t
*cdp
= current_cpu_datap();
726 xstate_t new_xstate
= new ? thread_xstate(new) : fpu_default
;
728 assert(ml_get_interrupts_enabled() == FALSE
);
729 ifps
= (old
)->machine
.ifps
;
731 if (ifps
&& ((ifps
->fp_valid
!= FALSE
) && (ifps
->fp_valid
!= TRUE
))) {
732 panic("ifps->fp_valid: %u\n", ifps
->fp_valid
);
735 if (ifps
!= 0 && (ifps
->fp_valid
== FALSE
)) {
736 /* Clear CR0.TS in preparation for the FP context save. In
737 * theory, this shouldn't be necessary since a live FPU should
738 * indicate that TS is clear. However, various routines
739 * (such as sendsig & sigreturn) manipulate TS directly.
742 /* registers are in FPU - save to memory */
743 boolean_t is64
= (thread_is_64bit_addr(old
) &&
744 is_saved_state64(old
->machine
.iss
));
746 fpu_store_registers(ifps
, is64
);
747 ifps
->fp_valid
= TRUE
;
749 if (fpu_ZMM_capable
&& (cdp
->cpu_xstate
== AVX512
)) {
750 xrstor64((struct x86_fx_thread_state
*)&default_avx512_state
, xstate_xmask
[AVX512
]);
751 } else if (fpu_YMM_capable
) {
752 xrstor64((struct x86_fx_thread_state
*) &default_avx_state
, xstate_xmask
[AVX
]);
754 fxrstor64((struct x86_fx_thread_state
*)&default_fx_state
);
758 assertf(fpu_YMM_capable
? (xgetbv(XCR0
) == xstate_xmask
[cdp
->cpu_xstate
]) : TRUE
, "XCR0 mismatch: 0x%llx 0x%x 0x%x", xgetbv(XCR0
), cdp
->cpu_xstate
, xstate_xmask
[cdp
->cpu_xstate
]);
759 if (new_xstate
!= (xstate_t
) cdp
->cpu_xstate
) {
760 DBG("fpu_switch_context(%p,%p) new xstate: %s\n",
761 old
, new, xstate_name
[new_xstate
]);
762 xsetbv(0, xstate_xmask
[new_xstate
]);
763 cdp
->cpu_xstate
= new_xstate
;
770 * Free a FPU save area.
771 * Called only when thread terminating - no locking necessary.
774 fpu_free(thread_t thread
, void *fps
)
776 pcb_t pcb
= THREAD_TO_PCB(thread
);
778 fp_state_free(fps
, pcb
->xstate
);
779 pcb
->xstate
= UNDEFINED
;
783 * Set the floating-point state for a thread based
784 * on the FXSave formatted data. This is basically
785 * the same as fpu_set_state except it uses the
786 * expanded data structure.
787 * If the thread is not the current thread, it is
788 * not running (held). Locking needed against
789 * concurrent fpu_set_state or fpu_get_state.
794 thread_state_t tstate
,
797 struct x86_fx_thread_state
*ifps
;
798 struct x86_fx_thread_state
*new_ifps
;
799 x86_float_state64_t
*state
;
801 boolean_t old_valid
, fresh_state
= FALSE
;
803 if (fpu_capability
== UNDEFINED
) {
807 if ((f
== x86_AVX_STATE32
|| f
== x86_AVX_STATE64
) &&
808 fpu_capability
< AVX
) {
812 #if !defined(RC_HIDE_XNU_J137)
813 if ((f
== x86_AVX512_STATE32
|| f
== x86_AVX512_STATE64
) &&
814 thread_xstate(thr_act
) == AVX
) {
815 if (!fpu_thread_promote_avx512(thr_act
)) {
821 state
= (x86_float_state64_t
*)tstate
;
823 assert(thr_act
!= THREAD_NULL
);
824 pcb
= THREAD_TO_PCB(thr_act
);
828 * new FPU state is 'invalid'.
829 * Deallocate the fp state if it exists.
831 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
836 simple_unlock(&pcb
->lock
);
839 fp_state_free(ifps
, thread_xstate(thr_act
));
843 * Valid incoming state. Allocate the fp state if there is none.
847 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
852 simple_unlock(&pcb
->lock
);
853 new_ifps
= fp_state_alloc(thread_xstate(thr_act
));
859 pcb
->xstate
= thread_xstate(thr_act
);
864 * now copy over the new data.
867 old_valid
= ifps
->fp_valid
;
869 #if DEBUG || DEVELOPMENT
870 if ((fresh_state
== FALSE
) && (old_valid
== FALSE
) && (thr_act
!= current_thread())) {
871 panic("fpu_set_fxstate inconsistency, thread: %p not stopped", thr_act
);
875 * Clear any reserved bits in the MXCSR to prevent a GPF
876 * when issuing an FXRSTOR.
879 state
->fpu_mxcsr
&= mxcsr_capability_mask
;
881 bcopy((char *)&state
->fpu_fcw
, (char *)ifps
, fp_state_size
[FP
]);
883 switch (thread_xstate(thr_act
)) {
888 panic("fpu_set_fxstate() INVALID xstate: 0x%x", thread_xstate(thr_act
));
892 panic("fpu_set_fxstate() UNDEFINED xstate");
895 ifps
->fp_save_layout
= thread_is_64bit_addr(thr_act
) ? FXSAVE64
: FXSAVE32
;
898 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
899 x86_avx_state64_t
*xs
= (x86_avx_state64_t
*) state
;
901 iavx
->fp
.fp_save_layout
= thread_is_64bit_addr(thr_act
) ? XSAVE64
: XSAVE32
;
903 /* Sanitize XSAVE header */
904 bzero(&iavx
->_xh
.xhrsvd
[0], sizeof(iavx
->_xh
.xhrsvd
));
905 iavx
->_xh
.xstate_bv
= AVX_XMASK
;
906 iavx
->_xh
.xcomp_bv
= 0;
908 if (f
== x86_AVX_STATE32
) {
909 bcopy_nochk(&xs
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 8 * sizeof(_STRUCT_XMM_REG
));
910 } else if (f
== x86_AVX_STATE64
) {
911 bcopy_nochk(&xs
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 16 * sizeof(_STRUCT_XMM_REG
));
913 iavx
->_xh
.xstate_bv
= (XFEM_SSE
| XFEM_X87
);
917 #if !defined(RC_HIDE_XNU_J137)
919 struct x86_avx512_thread_state
*iavx
= (void *) ifps
;
922 x86_avx512_state32_t
*s32
;
923 x86_avx512_state64_t
*s64
;
924 } xs
= { .ts
= tstate
};
926 iavx
->fp
.fp_save_layout
= thread_is_64bit_addr(thr_act
) ? XSAVE64
: XSAVE32
;
928 /* Sanitize XSAVE header */
929 bzero(&iavx
->_xh
.xhrsvd
[0], sizeof(iavx
->_xh
.xhrsvd
));
930 iavx
->_xh
.xstate_bv
= AVX512_XMASK
;
931 iavx
->_xh
.xcomp_bv
= 0;
934 case x86_AVX512_STATE32
:
935 bcopy_nochk(&xs
.s32
->fpu_k0
, iavx
->x_Opmask
, 8 * sizeof(_STRUCT_OPMASK_REG
));
936 bcopy_nochk(&xs
.s32
->fpu_zmmh0
, iavx
->x_ZMM_Hi256
, 8 * sizeof(_STRUCT_YMM_REG
));
937 bcopy_nochk(&xs
.s32
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 8 * sizeof(_STRUCT_XMM_REG
));
938 DBG_AVX512_STATE(iavx
);
940 case x86_AVX_STATE32
:
941 bcopy_nochk(&xs
.s32
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 8 * sizeof(_STRUCT_XMM_REG
));
943 case x86_AVX512_STATE64
:
944 bcopy_nochk(&xs
.s64
->fpu_k0
, iavx
->x_Opmask
, 8 * sizeof(_STRUCT_OPMASK_REG
));
945 bcopy_nochk(&xs
.s64
->fpu_zmm16
, iavx
->x_Hi16_ZMM
, 16 * sizeof(_STRUCT_ZMM_REG
));
946 bcopy_nochk(&xs
.s64
->fpu_zmmh0
, iavx
->x_ZMM_Hi256
, 16 * sizeof(_STRUCT_YMM_REG
));
947 bcopy_nochk(&xs
.s64
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 16 * sizeof(_STRUCT_XMM_REG
));
948 DBG_AVX512_STATE(iavx
);
950 case x86_AVX_STATE64
:
951 bcopy_nochk(&xs
.s64
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 16 * sizeof(_STRUCT_XMM_REG
));
959 ifps
->fp_valid
= old_valid
;
961 if (old_valid
== FALSE
) {
962 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
963 ifps
->fp_valid
= TRUE
;
964 /* If altering the current thread's state, disable FPU */
965 if (thr_act
== current_thread()) {
969 ml_set_interrupts_enabled(istate
);
972 simple_unlock(&pcb
->lock
);
975 fp_state_free(new_ifps
, thread_xstate(thr_act
));
982 * Get the floating-point state for a thread.
983 * If the thread is not the current thread, it is
984 * not running (held). Locking needed against
985 * concurrent fpu_set_state or fpu_get_state.
990 thread_state_t tstate
,
993 struct x86_fx_thread_state
*ifps
;
994 x86_float_state64_t
*state
;
995 kern_return_t ret
= KERN_FAILURE
;
998 if (fpu_capability
== UNDEFINED
) {
1002 if ((f
== x86_AVX_STATE32
|| f
== x86_AVX_STATE64
) &&
1003 fpu_capability
< AVX
) {
1004 return KERN_FAILURE
;
1007 #if !defined(RC_HIDE_XNU_J137)
1008 if ((f
== x86_AVX512_STATE32
|| f
== x86_AVX512_STATE64
) &&
1009 thread_xstate(thr_act
) != AVX512
) {
1010 return KERN_FAILURE
;
1014 state
= (x86_float_state64_t
*)tstate
;
1016 assert(thr_act
!= THREAD_NULL
);
1017 pcb
= THREAD_TO_PCB(thr_act
);
1019 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1024 * No valid floating-point state.
1027 bcopy((char *)&initial_fp_state
, (char *)&state
->fpu_fcw
,
1030 simple_unlock(&pcb
->lock
);
1032 return KERN_SUCCESS
;
1035 * Make sure we`ve got the latest fp state info
1036 * If the live fpu state belongs to our target
1038 if (thr_act
== current_thread()) {
1041 intr
= ml_set_interrupts_enabled(FALSE
);
1047 (void)ml_set_interrupts_enabled(intr
);
1049 if (ifps
->fp_valid
) {
1050 bcopy((char *)ifps
, (char *)&state
->fpu_fcw
, fp_state_size
[FP
]);
1051 switch (thread_xstate(thr_act
)) {
1052 case UNDEFINED_FULL
:
1056 panic("fpu_get_fxstate() INVALID xstate: 0x%x", thread_xstate(thr_act
));
1060 panic("fpu_get_fxstate() UNDEFINED xstate");
1063 break; /* already done */
1065 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
1066 x86_avx_state64_t
*xs
= (x86_avx_state64_t
*) state
;
1067 if (f
== x86_AVX_STATE32
) {
1068 bcopy_nochk(iavx
->x_YMM_Hi128
, &xs
->fpu_ymmh0
, 8 * sizeof(_STRUCT_XMM_REG
));
1069 } else if (f
== x86_AVX_STATE64
) {
1070 bcopy_nochk(iavx
->x_YMM_Hi128
, &xs
->fpu_ymmh0
, 16 * sizeof(_STRUCT_XMM_REG
));
1074 #if !defined(RC_HIDE_XNU_J137)
1076 struct x86_avx512_thread_state
*iavx
= (void *) ifps
;
1079 x86_avx512_state32_t
*s32
;
1080 x86_avx512_state64_t
*s64
;
1081 } xs
= { .ts
= tstate
};
1083 case x86_AVX512_STATE32
:
1084 bcopy_nochk(iavx
->x_Opmask
, &xs
.s32
->fpu_k0
, 8 * sizeof(_STRUCT_OPMASK_REG
));
1085 bcopy_nochk(iavx
->x_ZMM_Hi256
, &xs
.s32
->fpu_zmmh0
, 8 * sizeof(_STRUCT_YMM_REG
));
1086 bcopy_nochk(iavx
->x_YMM_Hi128
, &xs
.s32
->fpu_ymmh0
, 8 * sizeof(_STRUCT_XMM_REG
));
1087 DBG_AVX512_STATE(iavx
);
1089 case x86_AVX_STATE32
:
1090 bcopy_nochk(iavx
->x_YMM_Hi128
, &xs
.s32
->fpu_ymmh0
, 8 * sizeof(_STRUCT_XMM_REG
));
1092 case x86_AVX512_STATE64
:
1093 bcopy_nochk(iavx
->x_Opmask
, &xs
.s64
->fpu_k0
, 8 * sizeof(_STRUCT_OPMASK_REG
));
1094 bcopy_nochk(iavx
->x_Hi16_ZMM
, &xs
.s64
->fpu_zmm16
, 16 * sizeof(_STRUCT_ZMM_REG
));
1095 bcopy_nochk(iavx
->x_ZMM_Hi256
, &xs
.s64
->fpu_zmmh0
, 16 * sizeof(_STRUCT_YMM_REG
));
1096 bcopy_nochk(iavx
->x_YMM_Hi128
, &xs
.s64
->fpu_ymmh0
, 16 * sizeof(_STRUCT_XMM_REG
));
1097 DBG_AVX512_STATE(iavx
);
1099 case x86_AVX_STATE64
:
1100 bcopy_nochk(iavx
->x_YMM_Hi128
, &xs
.s64
->fpu_ymmh0
, 16 * sizeof(_STRUCT_XMM_REG
));
1110 simple_unlock(&pcb
->lock
);
1118 * the child thread is 'stopped' with the thread
1119 * mutex held and is currently not known by anyone
1120 * so no way for fpu state to get manipulated by an
1121 * outside agency -> no need for pcb lock
1129 struct x86_fx_thread_state
*new_ifps
= NULL
;
1132 xstate_t xstate
= thread_xstate(parent
);
1134 ppcb
= THREAD_TO_PCB(parent
);
1136 if (ppcb
->ifps
== NULL
) {
1140 if (child
->machine
.ifps
) {
1141 panic("fpu_dup_fxstate: child's ifps non-null");
1144 new_ifps
= fp_state_alloc(xstate
);
1146 simple_lock(&ppcb
->lock
, LCK_GRP_NULL
);
1148 if (ppcb
->ifps
!= NULL
) {
1149 struct x86_fx_thread_state
*ifps
= ppcb
->ifps
;
1151 * Make sure we`ve got the latest fp state info
1153 if (current_thread() == parent
) {
1154 intr
= ml_set_interrupts_enabled(FALSE
);
1155 assert(current_thread() == parent
);
1160 (void)ml_set_interrupts_enabled(intr
);
1163 if (ifps
->fp_valid
) {
1164 child
->machine
.ifps
= new_ifps
;
1165 child
->machine
.xstate
= xstate
;
1166 bcopy((char *)(ppcb
->ifps
),
1167 (char *)(child
->machine
.ifps
),
1168 fp_state_size
[xstate
]);
1170 /* Mark the new fp saved state as non-live. */
1171 /* Temporarily disabled: radar 4647827
1172 * new_ifps->fp_valid = TRUE;
1176 * Clear any reserved bits in the MXCSR to prevent a GPF
1177 * when issuing an FXRSTOR.
1179 new_ifps
->fx_MXCSR
&= mxcsr_capability_mask
;
1183 simple_unlock(&ppcb
->lock
);
1185 if (new_ifps
!= NULL
) {
1186 fp_state_free(new_ifps
, xstate
);
1192 * FNINIT programs the x87 control word to 0x37f, which matches
1193 * the desired default for macOS.
1199 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1203 /* We skip this power-on-default verification sequence on
1204 * non-DEBUG, as dirtying the x87 control word may slow down
1205 * xsave/xrstor and affect energy use.
1207 unsigned short control
, control2
;
1210 control
&= ~(FPC_PC
| FPC_RC
); /* Clear precision & rounding control */
1211 control
|= (FPC_PC_64
| /* Set precision */
1212 FPC_RC_RN
| /* round-to-nearest */
1213 FPC_ZE
| /* Suppress zero-divide */
1214 FPC_OE
| /* and overflow */
1215 FPC_UE
| /* underflow */
1216 FPC_IE
| /* Allow NaNQs and +-INF */
1217 FPC_DE
| /* Allow denorms as operands */
1218 FPC_PE
); /* No trap for precision loss */
1219 assert(control
== control2
);
1222 /* Initialize SSE/SSE2 */
1223 __builtin_ia32_ldmxcsr(0x1f80);
1224 if (fpu_YMM_capable
) {
1229 ml_set_interrupts_enabled(istate
);
1233 * Coprocessor not present.
1236 uint64_t x86_isr_fp_simd_use
;
1244 struct x86_fx_thread_state
*ifps
= 0;
1245 xstate_t xstate
= current_xstate();
1247 thr_act
= current_thread();
1248 pcb
= THREAD_TO_PCB(thr_act
);
1250 if (pcb
->ifps
== 0 && !get_interrupt_level()) {
1251 ifps
= fp_state_alloc(xstate
);
1252 bcopy((char *)&initial_fp_state
, (char *)ifps
,
1253 fp_state_size
[xstate
]);
1254 if (!thread_is_64bit_addr(thr_act
)) {
1255 ifps
->fp_save_layout
= fpu_YMM_capable
? XSAVE32
: FXSAVE32
;
1257 ifps
->fp_save_layout
= fpu_YMM_capable
? XSAVE64
: FXSAVE64
;
1259 ifps
->fp_valid
= TRUE
;
1261 intr
= ml_set_interrupts_enabled(FALSE
);
1263 clear_ts(); /* Enable FPU use */
1265 if (__improbable(get_interrupt_level())) {
1266 /* Track number of #DNA traps at interrupt context,
1267 * which is likely suboptimal. Racy, but good enough.
1269 x86_isr_fp_simd_use
++;
1271 * Save current FP/SIMD context if valid
1272 * Initialize live FP/SIMD registers
1279 if (pcb
->ifps
== 0) {
1281 pcb
->xstate
= xstate
;
1285 * Load this thread`s state into coprocessor live context.
1289 (void)ml_set_interrupts_enabled(intr
);
1292 fp_state_free(ifps
, xstate
);
1297 * FPU overran end of segment.
1298 * Re-initialize FPU. Floating point state is not valid.
1304 thread_t thr_act
= current_thread();
1306 struct x86_fx_thread_state
*ifps
;
1308 xstate_t xstate
= current_xstate();
1310 intr
= ml_set_interrupts_enabled(FALSE
);
1312 if (get_interrupt_level()) {
1313 panic("FPU segment overrun exception at interrupt context\n");
1315 if (current_task() == kernel_task
) {
1316 panic("FPU segment overrun exception in kernel thread context\n");
1320 * This is a non-recoverable error.
1321 * Invalidate the thread`s FPU state.
1323 pcb
= THREAD_TO_PCB(thr_act
);
1324 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1327 simple_unlock(&pcb
->lock
);
1330 * Re-initialize the FPU.
1336 * And disable access.
1340 (void)ml_set_interrupts_enabled(intr
);
1343 fp_state_free(ifps
, xstate
);
1349 i386_exception(EXC_BAD_ACCESS
, VM_PROT_READ
| VM_PROT_EXECUTE
, 0);
1353 extern void fpxlog(int, uint32_t, uint32_t, uint32_t);
1356 * FPU error. Called by AST.
1362 thread_t thr_act
= current_thread();
1363 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1366 intr
= ml_set_interrupts_enabled(FALSE
);
1368 if (get_interrupt_level()) {
1369 panic("FPU error exception at interrupt context\n");
1371 if (current_task() == kernel_task
) {
1372 panic("FPU error exception in kernel thread context\n");
1376 * Save the FPU state and turn off the FPU.
1380 (void)ml_set_interrupts_enabled(intr
);
1382 const uint32_t mask
= ifps
->fx_control
&
1383 (FPC_IM
| FPC_DM
| FPC_ZM
| FPC_OM
| FPC_UE
| FPC_PE
);
1384 const uint32_t xcpt
= ~mask
& (ifps
->fx_status
&
1385 (FPS_IE
| FPS_DE
| FPS_ZE
| FPS_OE
| FPS_UE
| FPS_PE
));
1386 fpxlog(EXC_I386_EXTERR
, ifps
->fx_status
, ifps
->fx_control
, xcpt
);
1388 * Raise FPU exception.
1389 * Locking not needed on pcb->ifps,
1390 * since thread is running.
1392 i386_exception(EXC_ARITHMETIC
,
1402 * Locking not needed:
1403 * . if called from fpu_get_state, pcb already locked.
1404 * . if called from fpnoextflt or fp_intr, we are single-cpu
1405 * . otherwise, thread is running.
1406 * N.B.: Must be called with interrupts disabled
1413 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
1414 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
1417 if (ifps
!= 0 && !ifps
->fp_valid
) {
1418 assert((get_cr0() & CR0_TS
) == 0);
1419 /* registers are in FPU */
1420 ifps
->fp_valid
= TRUE
;
1421 fpu_store_registers(ifps
, thread_is_64bit_addr(thr_act
));
1426 * Restore FPU state from PCB.
1428 * Locking not needed; always called on the current thread.
1435 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
1436 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
1440 if (ifps
->fp_valid
!= FALSE
&& ifps
->fp_valid
!= TRUE
) {
1441 panic("fp_load() invalid fp_valid: %u, fp_save_layout: %u\n",
1442 ifps
->fp_valid
, ifps
->fp_save_layout
);
1446 if (ifps
->fp_valid
== FALSE
) {
1449 fpu_load_registers(ifps
);
1451 ifps
->fp_valid
= FALSE
; /* in FPU */
1455 * SSE arithmetic exception handling code.
1456 * Basically the same as the x87 exception handler with a different subtype
1460 fpSSEexterrflt(void)
1462 thread_t thr_act
= current_thread();
1463 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1466 intr
= ml_set_interrupts_enabled(FALSE
);
1468 if (get_interrupt_level()) {
1469 panic("SSE exception at interrupt context\n");
1471 if (current_task() == kernel_task
) {
1472 panic("SSE exception in kernel thread context\n");
1476 * Save the FPU state and turn off the FPU.
1480 (void)ml_set_interrupts_enabled(intr
);
1482 * Raise FPU exception.
1483 * Locking not needed on pcb->ifps,
1484 * since thread is running.
1486 const uint32_t mask
= (ifps
->fx_MXCSR
>> 7) &
1487 (FPC_IM
| FPC_DM
| FPC_ZM
| FPC_OM
| FPC_UE
| FPC_PE
);
1488 const uint32_t xcpt
= ~mask
& (ifps
->fx_MXCSR
&
1489 (FPS_IE
| FPS_DE
| FPS_ZE
| FPS_OE
| FPS_UE
| FPS_PE
));
1490 fpxlog(EXC_I386_SSEEXTERR
, ifps
->fx_MXCSR
, ifps
->fx_MXCSR
, xcpt
);
1492 i386_exception(EXC_ARITHMETIC
,
1499 #if !defined(RC_HIDE_XNU_J137)
1501 * If a thread is using an AVX-sized savearea:
1502 * - allocate a new AVX512-sized area,
1503 * - copy the 256-bit state into the 512-bit area,
1504 * - deallocate the smaller area
1507 fpu_savearea_promote_avx512(thread_t thread
)
1509 struct x86_avx_thread_state
*ifps
= NULL
;
1510 struct x86_avx512_thread_state
*ifps512
= NULL
;
1511 pcb_t pcb
= THREAD_TO_PCB(thread
);
1512 boolean_t do_avx512_alloc
= FALSE
;
1514 DBG("fpu_upgrade_savearea(%p)\n", thread
);
1516 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1520 pcb
->xstate
= AVX512
;
1521 simple_unlock(&pcb
->lock
);
1522 if (thread
!= current_thread()) {
1523 /* nothing to be done */
1531 if (pcb
->xstate
!= AVX512
) {
1532 do_avx512_alloc
= TRUE
;
1534 simple_unlock(&pcb
->lock
);
1536 if (do_avx512_alloc
== TRUE
) {
1537 ifps512
= fp_state_alloc(AVX512
);
1540 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1541 if (thread
== current_thread()) {
1544 intr
= ml_set_interrupts_enabled(FALSE
);
1550 xsetbv(0, AVX512_XMASK
);
1551 current_cpu_datap()->cpu_xstate
= AVX512
;
1552 (void)ml_set_interrupts_enabled(intr
);
1554 assert(ifps
->fp
.fp_valid
);
1556 /* Allocate an AVX512 savearea and copy AVX state into it */
1557 if (pcb
->xstate
!= AVX512
) {
1558 bcopy(ifps
, ifps512
, fp_state_size
[AVX
]);
1559 pcb
->ifps
= ifps512
;
1560 pcb
->xstate
= AVX512
;
1565 /* The PCB lock is redundant in some scenarios given the higher level
1566 * thread mutex, but its pre-emption disablement is relied upon here
1568 simple_unlock(&pcb
->lock
);
1571 fp_state_free(ifps
, AVX
);
1574 fp_state_free(ifps
, AVX512
);
1579 * Upgrade the calling thread to AVX512.
1582 fpu_thread_promote_avx512(thread_t thread
)
1584 task_t task
= current_task();
1586 if (thread
!= current_thread()) {
1589 if (!ml_fpu_avx512_enabled()) {
1593 fpu_savearea_promote_avx512(thread
);
1595 /* Racy but the task's xstate is only a hint */
1596 task
->xstate
= AVX512
;
1603 * Called from user_trap() when an invalid opcode fault is taken.
1604 * If the user is attempting an AVX512 instruction on a machine
1605 * that supports this, we switch the calling thread to use
1606 * a larger savearea, set its XCR0 bit mask to enable AVX512 and
1607 * return directly via thread_exception_return().
1608 * Otherwise simply return.
1610 #define MAX_X86_INSN_LENGTH (16)
1612 fpUDflt(user_addr_t rip
)
1614 uint8_t instruction_prefix
;
1615 boolean_t is_AVX512_instruction
= FALSE
;
1616 user_addr_t original_rip
= rip
;
1618 /* TODO: as an optimisation, copy up to the lesser of the
1619 * next page boundary or maximal prefix length in one pass
1620 * rather than issue multiple copyins
1622 if (copyin(rip
, (char *) &instruction_prefix
, 1)) {
1625 DBG("fpUDflt(0x%016llx) prefix: 0x%x\n",
1626 rip
, instruction_prefix
);
1627 /* TODO: determine more specifically which prefixes
1628 * are sane possibilities for AVX512 insns
1630 switch (instruction_prefix
) {
1631 case 0x2E: /* CS segment override */
1632 case 0x36: /* SS segment override */
1633 case 0x3E: /* DS segment override */
1634 case 0x26: /* ES segment override */
1635 case 0x64: /* FS segment override */
1636 case 0x65: /* GS segment override */
1637 case 0x66: /* Operand-size override */
1638 case 0x67: /* address-size override */
1639 /* Skip optional prefixes */
1641 if ((rip
- original_rip
) > MAX_X86_INSN_LENGTH
) {
1645 case 0x62: /* EVEX */
1646 case 0xC5: /* VEX 2-byte */
1647 case 0xC4: /* VEX 3-byte */
1648 is_AVX512_instruction
= TRUE
;
1653 } while (!is_AVX512_instruction
);
1655 /* Here if we detect attempted execution of an AVX512 instruction */
1658 * Fail if this machine doesn't support AVX512
1660 if (fpu_capability
!= AVX512
) {
1664 assert(xgetbv(XCR0
) == AVX_XMASK
);
1666 DBG("fpUDflt() switching xstate to AVX512\n");
1667 (void) fpu_thread_promote_avx512(current_thread());
1669 thread_exception_return();
1672 #endif /* !defined(RC_HIDE_XNU_J137) */
1675 fp_setvalid(boolean_t value
)
1677 thread_t thr_act
= current_thread();
1678 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1681 ifps
->fp_valid
= value
;
1683 if (value
== TRUE
) {
1684 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1686 ml_set_interrupts_enabled(istate
);
1692 ml_fpu_avx_enabled(void)
1694 return fpu_capability
>= AVX
;
1697 #if !defined(RC_HIDE_XNU_J137)
1699 ml_fpu_avx512_enabled(void)
1701 return fpu_capability
== AVX512
;
1706 task_xstate(task_t task
)
1708 if (task
== TASK_NULL
) {
1711 return task
->xstate
;
1716 thread_xstate(thread_t thread
)
1718 xstate_t xs
= THREAD_TO_PCB(thread
)->xstate
;
1719 if (xs
== UNDEFINED
) {
1720 return task_xstate(thread
->task
);
1727 current_xstate(void)
1729 return thread_xstate(current_thread());
1733 * Called when exec'ing between bitnesses.
1734 * If valid FPU state exists, adjust the layout.
1737 fpu_switch_addrmode(thread_t thread
, boolean_t is_64bit
)
1739 struct x86_fx_thread_state
*ifps
= thread
->machine
.ifps
;
1740 mp_disable_preemption();
1742 if (ifps
&& ifps
->fp_valid
) {
1743 if (thread_xstate(thread
) == FP
) {
1744 ifps
->fp_save_layout
= is_64bit
? FXSAVE64
: FXSAVE32
;
1746 ifps
->fp_save_layout
= is_64bit
? XSAVE64
: XSAVE32
;
1749 mp_enable_preemption();
1752 static inline uint32_t
1753 fpsimd_pop(uintptr_t ins
, int sz
)
1760 uint64_t *ins64
= (uint64_t *) ins
;
1761 uint64_t *ins642
= (uint64_t *) (ins
+ 8);
1762 rv1
= __builtin_popcountll(*ins64
);
1763 rv2
= __builtin_popcountll(*ins642
);
1770 uint32_t *ins32
= (uint32_t *) ins
;
1771 rv
+= __builtin_popcount(*ins32
);
1777 char *ins8
= (char *)ins
;
1778 rv
+= __builtin_popcount(*ins8
);
1786 thread_fpsimd_hash(thread_t ft
)
1788 if (fpsimd_fault_popc
== 0) {
1793 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1794 struct x86_fx_thread_state
*pifps
= THREAD_TO_PCB(ft
)->ifps
;
1797 if (pifps
->fp_valid
) {
1798 prv
= fpsimd_pop((uintptr_t) &pifps
->fx_XMM_reg
[0][0],
1799 sizeof(pifps
->fx_XMM_reg
));
1801 uintptr_t cr0
= get_cr0();
1804 prv
= fpsimd_pop((uintptr_t) &pifps
->fx_XMM_reg
[0][0],
1805 sizeof(pifps
->fx_XMM_reg
));
1806 pifps
->fp_valid
= FALSE
;
1812 ml_set_interrupts_enabled(istate
);