2 * Copyright (c) 2000-2019 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 #define IS_VALID_XSTATE(x) ((x) == FP || (x) == AVX || (x) == AVX512)
150 zone_t ifps_zone
[] = {
155 static uint32_t fp_state_size
[] = {
156 [FP
] = sizeof(struct x86_fx_thread_state
),
157 [AVX
] = sizeof(struct x86_avx_thread_state
),
158 [AVX512
] = sizeof(struct x86_avx512_thread_state
)
161 static const char *xstate_name
[] = {
162 [UNDEFINED
] = "UNDEFINED",
168 #define fpu_ZMM_capable (fpu_capability == AVX512)
169 #define fpu_YMM_capable (fpu_capability == AVX || fpu_capability == AVX512)
171 * On-demand AVX512 support
172 * ------------------------
173 * On machines with AVX512 support, by default, threads are created with
174 * AVX512 masked off in XCR0 and an AVX-sized savearea is used. However, AVX512
175 * capabilities are advertised in the commpage and via sysctl. If a thread
176 * opts to use AVX512 instructions, the first will result in a #UD exception.
177 * Faulting AVX512 intructions are recognizable by their unique prefix.
178 * This exception results in the thread being promoted to use an AVX512-sized
179 * savearea and for the AVX512 bit masks being set in its XCR0. The faulting
180 * instruction is re-driven and the thread can proceed to perform AVX512
183 * In addition to AVX512 instructions causing promotion, the thread_set_state()
184 * primitive with an AVX512 state flavor result in promotion.
186 * AVX512 promotion of the first thread in a task causes the default xstate
187 * of the task to be promoted so that any subsequently created or subsequently
188 * DNA-faulted thread will have AVX512 xstate and it will not need to fault-in
191 * Two savearea zones are used: the default pool of AVX-sized (832 byte) areas
192 * and a second pool of larger AVX512-sized (2688 byte) areas.
194 * Note the initial state value is an AVX512 object but that the AVX initial
195 * value is a subset of it.
197 static uint32_t cpuid_reevaluated
= 0;
199 static void fpu_store_registers(void *, boolean_t
);
200 static void fpu_load_registers(void *);
202 static const uint32_t xstate_xmask
[] = {
205 [AVX512
] = AVX512_XMASK
209 xsave(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
211 __asm__
__volatile__ ("xsave %0" :"=m" (*a
) : "a"(rfbm
), "d"(0));
215 xsave64(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
217 __asm__
__volatile__ ("xsave64 %0" :"=m" (*a
) : "a"(rfbm
), "d"(0));
221 xrstor(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
223 __asm__
__volatile__ ("xrstor %0" :: "m" (*a
), "a"(rfbm
), "d"(0));
227 xrstor64(struct x86_fx_thread_state
*a
, uint32_t rfbm
)
229 __asm__
__volatile__ ("xrstor64 %0" :: "m" (*a
), "a"(rfbm
), "d"(0));
232 __unused
static inline void
235 __asm__
__volatile__ ("vzeroupper" ::);
238 static boolean_t
fpu_thread_promote_avx512(thread_t
); /* Forward */
242 * Furthermore, make compile-time asserts that no padding creeps into structures
243 * for which we're doing this.
245 #define ASSERT_PACKED(t, m1, m2, n, mt) \
246 extern char assert_packed_ ## t ## _ ## m1 ## _ ## m2 \
247 [(offsetof(t,m2) - offsetof(t,m1) == (n - 1)*sizeof(mt)) ? 1 : -1]
249 ASSERT_PACKED(x86_avx_state32_t
, fpu_ymmh0
, fpu_ymmh7
, 8, _STRUCT_XMM_REG
);
251 ASSERT_PACKED(x86_avx_state64_t
, fpu_ymmh0
, fpu_ymmh15
, 16, _STRUCT_XMM_REG
);
253 ASSERT_PACKED(x86_avx512_state32_t
, fpu_k0
, fpu_k7
, 8, _STRUCT_OPMASK_REG
);
254 ASSERT_PACKED(x86_avx512_state32_t
, fpu_ymmh0
, fpu_ymmh7
, 8, _STRUCT_XMM_REG
);
255 ASSERT_PACKED(x86_avx512_state32_t
, fpu_zmmh0
, fpu_zmmh7
, 8, _STRUCT_YMM_REG
);
257 ASSERT_PACKED(x86_avx512_state64_t
, fpu_k0
, fpu_k7
, 8, _STRUCT_OPMASK_REG
);
258 ASSERT_PACKED(x86_avx512_state64_t
, fpu_ymmh0
, fpu_ymmh15
, 16, _STRUCT_XMM_REG
);
259 ASSERT_PACKED(x86_avx512_state64_t
, fpu_zmmh0
, fpu_zmmh15
, 16, _STRUCT_YMM_REG
);
260 ASSERT_PACKED(x86_avx512_state64_t
, fpu_zmm16
, fpu_zmm31
, 16, _STRUCT_ZMM_REG
);
262 #if defined(DEBUG_AVX512)
264 #define DBG(x...) kprintf("DBG: " x)
266 typedef struct { uint8_t byte
[8]; } opmask_t
;
267 typedef struct { uint8_t byte
[16]; } xmm_t
;
268 typedef struct { uint8_t byte
[32]; } ymm_t
;
269 typedef struct { uint8_t byte
[64]; } zmm_t
;
272 DBG_AVX512_STATE(struct x86_avx512_thread_state
*sp
)
275 xmm_t
*xmm
= (xmm_t
*) &sp
->fp
.fx_XMM_reg
;
276 xmm_t
*ymmh
= (xmm_t
*) &sp
->x_YMM_Hi128
;
277 ymm_t
*zmmh
= (ymm_t
*) &sp
->x_ZMM_Hi256
;
278 zmm_t
*zmm
= (zmm_t
*) &sp
->x_Hi16_ZMM
;
279 opmask_t
*k
= (opmask_t
*) &sp
->x_Opmask
;
281 kprintf("x_YMM_Hi128: %lu\n", offsetof(struct x86_avx512_thread_state
, x_YMM_Hi128
));
282 kprintf("x_Opmask: %lu\n", offsetof(struct x86_avx512_thread_state
, x_Opmask
));
283 kprintf("x_ZMM_Hi256: %lu\n", offsetof(struct x86_avx512_thread_state
, x_ZMM_Hi256
));
284 kprintf("x_Hi16_ZMM: %lu\n", offsetof(struct x86_avx512_thread_state
, x_Hi16_ZMM
));
286 kprintf("XCR0: 0x%016llx\n", xgetbv(XCR0
));
287 kprintf("XINUSE: 0x%016llx\n", xgetbv(1));
289 /* Print all ZMM registers */
290 for (i
= 0; i
< 16; i
++) {
291 kprintf("zmm%d:\t0x", i
);
292 for (j
= 0; j
< 16; j
++) {
293 kprintf("%02x", xmm
[i
].byte
[j
]);
295 for (j
= 0; j
< 16; j
++) {
296 kprintf("%02x", ymmh
[i
].byte
[j
]);
298 for (j
= 0; j
< 32; j
++) {
299 kprintf("%02x", zmmh
[i
].byte
[j
]);
303 for (i
= 0; i
< 16; i
++) {
304 kprintf("zmm%d:\t0x", 16 + i
);
305 for (j
= 0; j
< 64; j
++) {
306 kprintf("%02x", zmm
[i
].byte
[j
]);
310 for (i
= 0; i
< 8; i
++) {
311 kprintf("k%d:\t0x", i
);
312 for (j
= 0; j
< 8; j
++) {
313 kprintf("%02x", k
[i
].byte
[j
]);
318 kprintf("xstate_bv: 0x%016llx\n", sp
->_xh
.xstate_bv
);
319 kprintf("xcomp_bv: 0x%016llx\n", sp
->_xh
.xcomp_bv
);
324 DBG_AVX512_STATE(__unused
struct x86_avx512_thread_state
*sp
)
328 #endif /* DEBUG_AVX512 */
331 static inline unsigned short
334 unsigned short status
;
335 __asm__
volatile ("fnstsw %0" : "=ma" (status
));
341 * Configure the initial FPU state presented to new threads.
342 * Determine the MXCSR capability mask, which allows us to mask off any
343 * potentially unsafe "reserved" bits before restoring the FPU context.
344 * *Not* per-cpu, assumes symmetry.
348 configure_mxcsr_capability_mask(x86_ext_thread_state_t
*fps
)
350 /* XSAVE requires a 64 byte aligned store */
351 assert(ALIGNED(fps
, 64));
352 /* Clear, to prepare for the diagnostic FXSAVE */
353 bzero(fps
, sizeof(*fps
));
356 fpu_store_registers(fps
, FALSE
);
358 mxcsr_capability_mask
= fps
->fx
.fx_MXCSR_MASK
;
360 /* Set default mask value if necessary */
361 if (mxcsr_capability_mask
== 0) {
362 mxcsr_capability_mask
= 0xffbf;
365 /* Clear vector register store */
366 bzero(&fps
->fx
.fx_XMM_reg
[0][0], sizeof(fps
->fx
.fx_XMM_reg
));
367 bzero(fps
->avx
.x_YMM_Hi128
, sizeof(fps
->avx
.x_YMM_Hi128
));
368 if (fpu_ZMM_capable
) {
369 bzero(fps
->avx512
.x_ZMM_Hi256
, sizeof(fps
->avx512
.x_ZMM_Hi256
));
370 bzero(fps
->avx512
.x_Hi16_ZMM
, sizeof(fps
->avx512
.x_Hi16_ZMM
));
371 bzero(fps
->avx512
.x_Opmask
, sizeof(fps
->avx512
.x_Opmask
));
374 fps
->fx
.fp_valid
= TRUE
;
375 fps
->fx
.fp_save_layout
= fpu_YMM_capable
? XSAVE32
: FXSAVE32
;
376 fpu_load_registers(fps
);
378 if (fpu_ZMM_capable
) {
379 xsave64((struct x86_fx_thread_state
*)&default_avx512_state
, xstate_xmask
[AVX512
]);
381 if (fpu_YMM_capable
) {
382 xsave64((struct x86_fx_thread_state
*)&default_avx_state
, xstate_xmask
[AVX
]);
384 fxsave64((struct x86_fx_thread_state
*)&default_fx_state
);
387 /* Poison values to trap unsafe usage */
388 fps
->fx
.fp_valid
= 0xFFFFFFFF;
389 fps
->fx
.fp_save_layout
= FP_UNUSED
;
391 /* Re-enable FPU/SSE DNA exceptions */
395 int fpsimd_fault_popc
= 0;
397 * Look for FPU and initialize it.
398 * Called on each CPU.
404 unsigned short status
;
405 unsigned short control
;
408 * Check for FPU by initializing it,
409 * then trying to read the correct bit patterns from
410 * the control and status registers.
412 set_cr0((get_cr0() & ~(CR0_EM
| CR0_TS
)) | CR0_NE
); /* allow use of FPU */
418 assert(((status
& 0xff) == 0) && ((control
& 0x103f) == 0x3f));
420 /* Advertise SSE support */
421 if (cpuid_features() & CPUID_FEATURE_FXSR
) {
422 set_cr4(get_cr4() | CR4_OSFXS
);
423 /* And allow SIMD exceptions if present */
424 if (cpuid_features() & CPUID_FEATURE_SSE
) {
425 set_cr4(get_cr4() | CR4_OSXMM
);
428 panic("fpu is not FP_FXSR");
431 fpu_capability
= fpu_default
= FP
;
433 PE_parse_boot_argn("fpsimd_fault_popc", &fpsimd_fault_popc
, sizeof(fpsimd_fault_popc
));
435 static boolean_t is_avx512_enabled
= TRUE
;
436 if (cpu_number() == master_cpu
) {
437 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_AVX512F
) {
438 PE_parse_boot_argn("avx512", &is_avx512_enabled
, sizeof(boolean_t
));
439 kprintf("AVX512 supported %s\n",
440 is_avx512_enabled
? "and enabled" : "but disabled");
444 /* Configure the XSAVE context mechanism if the processor supports
447 if (cpuid_features() & CPUID_FEATURE_XSAVE
) {
448 cpuid_xsave_leaf_t
*xs0p
= &cpuid_info()->cpuid_xsave_leaf
[0];
449 if (is_avx512_enabled
&&
450 (xs0p
->extended_state
[eax
] & XFEM_ZMM
) == XFEM_ZMM
) {
451 assert(xs0p
->extended_state
[eax
] & XFEM_SSE
);
452 assert(xs0p
->extended_state
[eax
] & XFEM_YMM
);
453 fpu_capability
= AVX512
;
454 /* XSAVE container size for all features */
455 set_cr4(get_cr4() | CR4_OSXSAVE
);
456 xsetbv(0, AVX512_XMASK
);
457 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
458 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated
)) {
461 /* Verify that now selected state can be accommodated */
462 assert(xs0p
->extended_state
[ebx
] == fp_state_size
[AVX512
]);
464 * AVX set until AVX512 is used.
465 * See comment above about on-demand AVX512 support.
467 xsetbv(0, AVX_XMASK
);
469 } else if (xs0p
->extended_state
[eax
] & XFEM_YMM
) {
470 assert(xs0p
->extended_state
[eax
] & XFEM_SSE
);
471 fpu_capability
= AVX
;
473 /* XSAVE container size for all features */
474 set_cr4(get_cr4() | CR4_OSXSAVE
);
475 xsetbv(0, AVX_XMASK
);
476 /* Re-evaluate CPUID, once, to reflect OSXSAVE */
477 if (OSCompareAndSwap(0, 1, &cpuid_reevaluated
)) {
480 /* Verify that now selected state can be accommodated */
481 assert(xs0p
->extended_state
[ebx
] == fp_state_size
[AVX
]);
485 if (cpu_number() == master_cpu
) {
486 kprintf("fpu_state: %s, state_size: %d\n",
487 xstate_name
[fpu_capability
],
488 fp_state_size
[fpu_capability
]);
492 current_cpu_datap()->cpu_xstate
= fpu_default
;
495 * Trap wait instructions. Turn off FPU for now.
497 set_cr0(get_cr0() | CR0_TS
| CR0_MP
);
501 * Allocate and initialize FP state for specified xstate.
505 fp_state_alloc(xstate_t xs
)
507 struct x86_fx_thread_state
*ifps
;
509 assert(ifps_zone
[xs
] != NULL
);
510 ifps
= zalloc(ifps_zone
[xs
]);
513 if (!(ALIGNED(ifps
, 64))) {
514 panic("fp_state_alloc: %p, %u, %p, %u",
515 ifps
, (unsigned) ifps_zone
[xs
]->elem_size
,
516 (void *) ifps_zone
[xs
]->free_elements
,
517 (unsigned) ifps_zone
[xs
]->alloc_size
);
520 bzero(ifps
, fp_state_size
[xs
]);
526 fp_state_free(void *ifps
, xstate_t xs
)
528 assert(ifps_zone
[xs
] != NULL
);
529 zfree(ifps_zone
[xs
], ifps
);
540 fpu_load_registers(void *fstate
)
542 struct x86_fx_thread_state
*ifps
= fstate
;
543 fp_save_layout_t layout
= ifps
->fp_save_layout
;
545 assert(current_task() == NULL
|| \
546 (thread_is_64bit_addr(current_thread()) ? \
547 (layout
== FXSAVE64
|| layout
== XSAVE64
) : \
548 (layout
== FXSAVE32
|| layout
== XSAVE32
)));
549 assert(ALIGNED(ifps
, 64));
550 assert(ml_get_interrupts_enabled() == FALSE
);
553 if (layout
== XSAVE32
|| layout
== XSAVE64
) {
554 struct x86_avx_thread_state
*iavx
= fstate
;
556 /* Verify reserved bits in the XSAVE header*/
557 if (iavx
->_xh
.xstate_bv
& ~xstate_xmask
[current_xstate()]) {
558 panic("iavx->_xh.xstate_bv: 0x%llx", iavx
->_xh
.xstate_bv
);
560 for (i
= 0; i
< sizeof(iavx
->_xh
.xhrsvd
); i
++) {
561 if (iavx
->_xh
.xhrsvd
[i
]) {
562 panic("Reserved bit set");
566 if (fpu_YMM_capable
) {
567 if (layout
!= XSAVE32
&& layout
!= XSAVE64
) {
568 panic("Inappropriate layout: %u\n", layout
);
581 xrstor64(ifps
, xstate_xmask
[current_xstate()]);
584 xrstor(ifps
, xstate_xmask
[current_xstate()]);
587 panic("fpu_load_registers() bad layout: %d\n", layout
);
592 fpu_store_registers(void *fstate
, boolean_t is64
)
594 struct x86_fx_thread_state
*ifps
= fstate
;
595 assert(ALIGNED(ifps
, 64));
596 xstate_t xs
= current_xstate();
601 ifps
->fp_save_layout
= FXSAVE64
;
604 ifps
->fp_save_layout
= FXSAVE32
;
610 xsave64(ifps
, xstate_xmask
[xs
]);
611 ifps
->fp_save_layout
= XSAVE64
;
613 xsave(ifps
, xstate_xmask
[xs
]);
614 ifps
->fp_save_layout
= XSAVE32
;
618 panic("fpu_store_registers() bad xstate: %d\n", xs
);
623 * Initialize FP handling.
627 fpu_module_init(void)
629 if (!IS_VALID_XSTATE(fpu_default
)) {
630 panic("fpu_module_init: invalid extended state %u\n",
634 /* We explicitly choose an allocation size of 13 pages = 64 * 832
635 * to eliminate waste for the 832 byte sized
636 * AVX XSAVE register save area.
638 ifps_zone
[fpu_default
] = zinit(fp_state_size
[fpu_default
],
639 thread_max
* fp_state_size
[fpu_default
],
640 64 * fp_state_size
[fpu_default
],
643 /* To maintain the required alignment, disable
644 * zone debugging for this zone as that appends
645 * 16 bytes to each element.
647 zone_change(ifps_zone
[fpu_default
], Z_ALIGNMENT_REQUIRED
, TRUE
);
650 * If AVX512 is supported, create a separate savearea zone.
651 * with allocation size: 19 pages = 32 * 2668
653 if (fpu_capability
== AVX512
) {
654 ifps_zone
[AVX512
] = zinit(fp_state_size
[AVX512
],
655 thread_max
* fp_state_size
[AVX512
],
656 32 * fp_state_size
[AVX512
],
657 "x86 avx512 save state");
658 zone_change(ifps_zone
[AVX512
], Z_ALIGNMENT_REQUIRED
, TRUE
);
661 /* Determine MXCSR reserved bits and configure initial FPU state*/
662 configure_mxcsr_capability_mask(&initial_fp_state
);
666 * Context switch fpu state.
667 * Always save old thread`s FPU context but don't load new .. allow that to fault-in.
668 * Switch to the new task's xstate.
672 fpu_switch_context(thread_t old
, thread_t
new)
674 struct x86_fx_thread_state
*ifps
;
675 cpu_data_t
*cdp
= current_cpu_datap();
676 xstate_t new_xstate
= new ? thread_xstate(new) : fpu_default
;
678 assert(ml_get_interrupts_enabled() == FALSE
);
679 ifps
= (old
)->machine
.ifps
;
681 if (ifps
&& ((ifps
->fp_valid
!= FALSE
) && (ifps
->fp_valid
!= TRUE
))) {
682 panic("ifps->fp_valid: %u\n", ifps
->fp_valid
);
685 if (ifps
!= 0 && (ifps
->fp_valid
== FALSE
)) {
686 /* Clear CR0.TS in preparation for the FP context save. In
687 * theory, this shouldn't be necessary since a live FPU should
688 * indicate that TS is clear. However, various routines
689 * (such as sendsig & sigreturn) manipulate TS directly.
692 /* registers are in FPU - save to memory */
693 boolean_t is64
= (thread_is_64bit_addr(old
) &&
694 is_saved_state64(old
->machine
.iss
));
696 fpu_store_registers(ifps
, is64
);
697 ifps
->fp_valid
= TRUE
;
699 if (fpu_ZMM_capable
&& (cdp
->cpu_xstate
== AVX512
)) {
700 xrstor64((struct x86_fx_thread_state
*)&default_avx512_state
, xstate_xmask
[AVX512
]);
701 } else if (fpu_YMM_capable
) {
702 xrstor64((struct x86_fx_thread_state
*) &default_avx_state
, xstate_xmask
[AVX
]);
704 fxrstor64((struct x86_fx_thread_state
*)&default_fx_state
);
708 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
]);
709 if (new_xstate
!= (xstate_t
) cdp
->cpu_xstate
) {
710 DBG("fpu_switch_context(%p,%p) new xstate: %s\n",
711 old
, new, xstate_name
[new_xstate
]);
712 xsetbv(0, xstate_xmask
[new_xstate
]);
713 cdp
->cpu_xstate
= new_xstate
;
720 * Free a FPU save area.
721 * Called only when thread terminating - no locking necessary.
724 fpu_free(thread_t thread
, void *fps
)
726 pcb_t pcb
= THREAD_TO_PCB(thread
);
728 fp_state_free(fps
, pcb
->xstate
);
729 pcb
->xstate
= UNDEFINED
;
733 * Set the floating-point state for a thread based
734 * on the FXSave formatted data. This is basically
735 * the same as fpu_set_state except it uses the
736 * expanded data structure.
737 * If the thread is not the current thread, it is
738 * not running (held). Locking needed against
739 * concurrent fpu_set_state or fpu_get_state.
744 thread_state_t tstate
,
747 struct x86_fx_thread_state
*ifps
;
748 struct x86_fx_thread_state
*new_ifps
;
749 x86_float_state64_t
*state
;
751 boolean_t old_valid
, fresh_state
= FALSE
;
754 if (fpu_capability
== UNDEFINED
) {
758 if ((f
== x86_AVX_STATE32
|| f
== x86_AVX_STATE64
) &&
759 fpu_capability
< AVX
) {
763 assert(thr_act
!= THREAD_NULL
);
765 thr_xstate
= thread_xstate(thr_act
);
767 if ((f
== x86_AVX512_STATE32
|| f
== x86_AVX512_STATE64
) &&
769 if (!fpu_thread_promote_avx512(thr_act
)) {
772 /* Reload thr_xstate after successful promotion */
773 thr_xstate
= thread_xstate(thr_act
);
777 state
= (x86_float_state64_t
*)tstate
;
779 pcb
= THREAD_TO_PCB(thr_act
);
783 * new FPU state is 'invalid'.
784 * Deallocate the fp state if it exists.
786 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
791 simple_unlock(&pcb
->lock
);
794 fp_state_free(ifps
, thr_xstate
);
798 * Valid incoming state. Allocate the fp state if there is none.
802 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
807 simple_unlock(&pcb
->lock
);
808 new_ifps
= fp_state_alloc(thr_xstate
);
814 pcb
->xstate
= thr_xstate
;
819 * now copy over the new data.
822 old_valid
= ifps
->fp_valid
;
824 #if DEBUG || DEVELOPMENT
825 if ((fresh_state
== FALSE
) && (old_valid
== FALSE
) && (thr_act
!= current_thread())) {
826 panic("fpu_set_fxstate inconsistency, thread: %p not stopped", thr_act
);
830 * Clear any reserved bits in the MXCSR to prevent a GPF
831 * when issuing an FXRSTOR.
834 state
->fpu_mxcsr
&= mxcsr_capability_mask
;
836 __nochk_bcopy((char *)&state
->fpu_fcw
, (char *)ifps
, fp_state_size
[FP
]);
838 switch (thr_xstate
) {
843 panic("fpu_set_fxstate() INVALID xstate: 0x%x", thr_xstate
);
847 panic("fpu_set_fxstate() UNDEFINED xstate");
850 ifps
->fp_save_layout
= thread_is_64bit_addr(thr_act
) ? FXSAVE64
: FXSAVE32
;
853 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
854 x86_avx_state64_t
*xs
= (x86_avx_state64_t
*) state
;
856 iavx
->fp
.fp_save_layout
= thread_is_64bit_addr(thr_act
) ? XSAVE64
: XSAVE32
;
858 /* Sanitize XSAVE header */
859 bzero(&iavx
->_xh
.xhrsvd
[0], sizeof(iavx
->_xh
.xhrsvd
));
860 iavx
->_xh
.xstate_bv
= AVX_XMASK
;
861 iavx
->_xh
.xcomp_bv
= 0;
863 if (f
== x86_AVX_STATE32
) {
864 __nochk_bcopy(&xs
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 8 * sizeof(_STRUCT_XMM_REG
));
865 } else if (f
== x86_AVX_STATE64
) {
866 __nochk_bcopy(&xs
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 16 * sizeof(_STRUCT_XMM_REG
));
868 iavx
->_xh
.xstate_bv
= (XFEM_SSE
| XFEM_X87
);
873 struct x86_avx512_thread_state
*iavx
= (void *) ifps
;
876 x86_avx512_state32_t
*s32
;
877 x86_avx512_state64_t
*s64
;
878 } xs
= { .ts
= tstate
};
880 iavx
->fp
.fp_save_layout
= thread_is_64bit_addr(thr_act
) ? XSAVE64
: XSAVE32
;
882 /* Sanitize XSAVE header */
883 bzero(&iavx
->_xh
.xhrsvd
[0], sizeof(iavx
->_xh
.xhrsvd
));
884 iavx
->_xh
.xstate_bv
= AVX512_XMASK
;
885 iavx
->_xh
.xcomp_bv
= 0;
888 case x86_AVX512_STATE32
:
889 __nochk_bcopy(&xs
.s32
->fpu_k0
, iavx
->x_Opmask
, 8 * sizeof(_STRUCT_OPMASK_REG
));
890 __nochk_bcopy(&xs
.s32
->fpu_zmmh0
, iavx
->x_ZMM_Hi256
, 8 * sizeof(_STRUCT_YMM_REG
));
891 __nochk_bcopy(&xs
.s32
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 8 * sizeof(_STRUCT_XMM_REG
));
892 DBG_AVX512_STATE(iavx
);
894 case x86_AVX_STATE32
:
895 __nochk_bcopy(&xs
.s32
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 8 * sizeof(_STRUCT_XMM_REG
));
897 case x86_AVX512_STATE64
:
898 __nochk_bcopy(&xs
.s64
->fpu_k0
, iavx
->x_Opmask
, 8 * sizeof(_STRUCT_OPMASK_REG
));
899 __nochk_bcopy(&xs
.s64
->fpu_zmm16
, iavx
->x_Hi16_ZMM
, 16 * sizeof(_STRUCT_ZMM_REG
));
900 __nochk_bcopy(&xs
.s64
->fpu_zmmh0
, iavx
->x_ZMM_Hi256
, 16 * sizeof(_STRUCT_YMM_REG
));
901 __nochk_bcopy(&xs
.s64
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 16 * sizeof(_STRUCT_XMM_REG
));
902 DBG_AVX512_STATE(iavx
);
904 case x86_AVX_STATE64
:
905 __nochk_bcopy(&xs
.s64
->fpu_ymmh0
, iavx
->x_YMM_Hi128
, 16 * sizeof(_STRUCT_XMM_REG
));
912 ifps
->fp_valid
= old_valid
;
914 if (old_valid
== FALSE
) {
915 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
916 ifps
->fp_valid
= TRUE
;
917 /* If altering the current thread's state, disable FPU */
918 if (thr_act
== current_thread()) {
922 ml_set_interrupts_enabled(istate
);
925 simple_unlock(&pcb
->lock
);
928 fp_state_free(new_ifps
, thr_xstate
);
935 * Get the floating-point state for a thread.
936 * If the thread is not the current thread, it is
937 * not running (held). Locking needed against
938 * concurrent fpu_set_state or fpu_get_state.
943 thread_state_t tstate
,
946 struct x86_fx_thread_state
*ifps
;
947 x86_float_state64_t
*state
;
948 kern_return_t ret
= KERN_FAILURE
;
950 xstate_t thr_xstate
= thread_xstate(thr_act
);
952 if (fpu_capability
== UNDEFINED
) {
956 if ((f
== x86_AVX_STATE32
|| f
== x86_AVX_STATE64
) &&
957 fpu_capability
< AVX
) {
961 if ((f
== x86_AVX512_STATE32
|| f
== x86_AVX512_STATE64
) &&
962 thr_xstate
!= AVX512
) {
966 state
= (x86_float_state64_t
*)tstate
;
968 assert(thr_act
!= THREAD_NULL
);
969 pcb
= THREAD_TO_PCB(thr_act
);
971 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
976 * No valid floating-point state.
979 __nochk_bcopy((char *)&initial_fp_state
, (char *)&state
->fpu_fcw
,
982 simple_unlock(&pcb
->lock
);
987 * Make sure we`ve got the latest fp state info
988 * If the live fpu state belongs to our target
990 if (thr_act
== current_thread()) {
993 intr
= ml_set_interrupts_enabled(FALSE
);
999 (void)ml_set_interrupts_enabled(intr
);
1001 if (ifps
->fp_valid
) {
1002 __nochk_bcopy((char *)ifps
, (char *)&state
->fpu_fcw
, fp_state_size
[FP
]);
1003 switch (thr_xstate
) {
1004 case UNDEFINED_FULL
:
1008 panic("fpu_get_fxstate() INVALID xstate: 0x%x", thr_xstate
);
1012 panic("fpu_get_fxstate() UNDEFINED xstate");
1015 break; /* already done */
1017 struct x86_avx_thread_state
*iavx
= (void *) ifps
;
1018 x86_avx_state64_t
*xs
= (x86_avx_state64_t
*) state
;
1019 if (f
== x86_AVX_STATE32
) {
1020 __nochk_bcopy(iavx
->x_YMM_Hi128
, &xs
->fpu_ymmh0
, 8 * sizeof(_STRUCT_XMM_REG
));
1021 } else if (f
== x86_AVX_STATE64
) {
1022 __nochk_bcopy(iavx
->x_YMM_Hi128
, &xs
->fpu_ymmh0
, 16 * sizeof(_STRUCT_XMM_REG
));
1027 struct x86_avx512_thread_state
*iavx
= (void *) ifps
;
1030 x86_avx512_state32_t
*s32
;
1031 x86_avx512_state64_t
*s64
;
1032 } xs
= { .ts
= tstate
};
1034 case x86_AVX512_STATE32
:
1035 __nochk_bcopy(iavx
->x_Opmask
, &xs
.s32
->fpu_k0
, 8 * sizeof(_STRUCT_OPMASK_REG
));
1036 __nochk_bcopy(iavx
->x_ZMM_Hi256
, &xs
.s32
->fpu_zmmh0
, 8 * sizeof(_STRUCT_YMM_REG
));
1037 __nochk_bcopy(iavx
->x_YMM_Hi128
, &xs
.s32
->fpu_ymmh0
, 8 * sizeof(_STRUCT_XMM_REG
));
1038 DBG_AVX512_STATE(iavx
);
1040 case x86_AVX_STATE32
:
1041 __nochk_bcopy(iavx
->x_YMM_Hi128
, &xs
.s32
->fpu_ymmh0
, 8 * sizeof(_STRUCT_XMM_REG
));
1043 case x86_AVX512_STATE64
:
1044 __nochk_bcopy(iavx
->x_Opmask
, &xs
.s64
->fpu_k0
, 8 * sizeof(_STRUCT_OPMASK_REG
));
1045 __nochk_bcopy(iavx
->x_Hi16_ZMM
, &xs
.s64
->fpu_zmm16
, 16 * sizeof(_STRUCT_ZMM_REG
));
1046 __nochk_bcopy(iavx
->x_ZMM_Hi256
, &xs
.s64
->fpu_zmmh0
, 16 * sizeof(_STRUCT_YMM_REG
));
1047 __nochk_bcopy(iavx
->x_YMM_Hi128
, &xs
.s64
->fpu_ymmh0
, 16 * sizeof(_STRUCT_XMM_REG
));
1048 DBG_AVX512_STATE(iavx
);
1050 case x86_AVX_STATE64
:
1051 __nochk_bcopy(iavx
->x_YMM_Hi128
, &xs
.s64
->fpu_ymmh0
, 16 * sizeof(_STRUCT_XMM_REG
));
1060 simple_unlock(&pcb
->lock
);
1068 * the child thread is 'stopped' with the thread
1069 * mutex held and is currently not known by anyone
1070 * so no way for fpu state to get manipulated by an
1071 * outside agency -> no need for pcb lock
1079 struct x86_fx_thread_state
*new_ifps
= NULL
;
1082 xstate_t xstate
= thread_xstate(parent
);
1084 ppcb
= THREAD_TO_PCB(parent
);
1086 if (ppcb
->ifps
== NULL
) {
1090 if (child
->machine
.ifps
) {
1091 panic("fpu_dup_fxstate: child's ifps non-null");
1094 new_ifps
= fp_state_alloc(xstate
);
1096 simple_lock(&ppcb
->lock
, LCK_GRP_NULL
);
1098 if (ppcb
->ifps
!= NULL
) {
1099 struct x86_fx_thread_state
*ifps
= ppcb
->ifps
;
1101 * Make sure we`ve got the latest fp state info
1103 if (current_thread() == parent
) {
1104 intr
= ml_set_interrupts_enabled(FALSE
);
1105 assert(current_thread() == parent
);
1110 (void)ml_set_interrupts_enabled(intr
);
1113 if (ifps
->fp_valid
) {
1114 child
->machine
.ifps
= new_ifps
;
1115 child
->machine
.xstate
= xstate
;
1116 __nochk_bcopy((char *)(ppcb
->ifps
),
1117 (char *)(child
->machine
.ifps
),
1118 fp_state_size
[xstate
]);
1120 /* Mark the new fp saved state as non-live. */
1121 /* Temporarily disabled: radar 4647827
1122 * new_ifps->fp_valid = TRUE;
1126 * Clear any reserved bits in the MXCSR to prevent a GPF
1127 * when issuing an FXRSTOR.
1129 new_ifps
->fx_MXCSR
&= mxcsr_capability_mask
;
1133 simple_unlock(&ppcb
->lock
);
1135 if (new_ifps
!= NULL
) {
1136 fp_state_free(new_ifps
, xstate
);
1142 * FNINIT programs the x87 control word to 0x37f, which matches
1143 * the desired default for macOS.
1149 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1153 /* We skip this power-on-default verification sequence on
1154 * non-DEBUG, as dirtying the x87 control word may slow down
1155 * xsave/xrstor and affect energy use.
1157 unsigned short control
, control2
;
1160 control
&= ~(FPC_PC
| FPC_RC
); /* Clear precision & rounding control */
1161 control
|= (FPC_PC_64
| /* Set precision */
1162 FPC_RC_RN
| /* round-to-nearest */
1163 FPC_ZE
| /* Suppress zero-divide */
1164 FPC_OE
| /* and overflow */
1165 FPC_UE
| /* underflow */
1166 FPC_IE
| /* Allow NaNQs and +-INF */
1167 FPC_DE
| /* Allow denorms as operands */
1168 FPC_PE
); /* No trap for precision loss */
1169 assert(control
== control2
);
1172 /* Initialize SSE/SSE2 */
1173 __builtin_ia32_ldmxcsr(0x1f80);
1174 if (fpu_YMM_capable
) {
1179 ml_set_interrupts_enabled(istate
);
1183 * Coprocessor not present.
1186 uint64_t x86_isr_fp_simd_use
;
1194 struct x86_fx_thread_state
*ifps
= 0;
1195 xstate_t xstate
= current_xstate();
1197 thr_act
= current_thread();
1198 pcb
= THREAD_TO_PCB(thr_act
);
1200 if (pcb
->ifps
== 0 && !get_interrupt_level()) {
1201 ifps
= fp_state_alloc(xstate
);
1202 __nochk_bcopy((char *)&initial_fp_state
, (char *)ifps
,
1203 fp_state_size
[xstate
]);
1204 if (!thread_is_64bit_addr(thr_act
)) {
1205 ifps
->fp_save_layout
= fpu_YMM_capable
? XSAVE32
: FXSAVE32
;
1207 ifps
->fp_save_layout
= fpu_YMM_capable
? XSAVE64
: FXSAVE64
;
1209 ifps
->fp_valid
= TRUE
;
1211 intr
= ml_set_interrupts_enabled(FALSE
);
1213 clear_ts(); /* Enable FPU use */
1215 if (__improbable(get_interrupt_level())) {
1216 /* Track number of #DNA traps at interrupt context,
1217 * which is likely suboptimal. Racy, but good enough.
1219 x86_isr_fp_simd_use
++;
1221 * Save current FP/SIMD context if valid
1222 * Initialize live FP/SIMD registers
1229 if (pcb
->ifps
== 0) {
1231 pcb
->xstate
= xstate
;
1235 * Load this thread`s state into coprocessor live context.
1239 (void)ml_set_interrupts_enabled(intr
);
1242 fp_state_free(ifps
, xstate
);
1247 * FPU overran end of segment.
1248 * Re-initialize FPU. Floating point state is not valid.
1254 thread_t thr_act
= current_thread();
1256 struct x86_fx_thread_state
*ifps
;
1258 xstate_t xstate
= current_xstate();
1260 intr
= ml_set_interrupts_enabled(FALSE
);
1262 if (get_interrupt_level()) {
1263 panic("FPU segment overrun exception at interrupt context\n");
1265 if (current_task() == kernel_task
) {
1266 panic("FPU segment overrun exception in kernel thread context\n");
1270 * This is a non-recoverable error.
1271 * Invalidate the thread`s FPU state.
1273 pcb
= THREAD_TO_PCB(thr_act
);
1274 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1277 simple_unlock(&pcb
->lock
);
1280 * Re-initialize the FPU.
1286 * And disable access.
1290 (void)ml_set_interrupts_enabled(intr
);
1293 fp_state_free(ifps
, xstate
);
1297 extern void fpxlog(int, uint32_t, uint32_t, uint32_t);
1300 * FPU error. Called by AST.
1306 thread_t thr_act
= current_thread();
1307 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1310 intr
= ml_set_interrupts_enabled(FALSE
);
1312 if (get_interrupt_level()) {
1313 panic("FPU error exception at interrupt context\n");
1315 if (current_task() == kernel_task
) {
1316 panic("FPU error exception in kernel thread context\n");
1320 * Save the FPU state and turn off the FPU.
1324 (void)ml_set_interrupts_enabled(intr
);
1326 const uint32_t mask
= ifps
->fx_control
&
1327 (FPC_IM
| FPC_DM
| FPC_ZM
| FPC_OM
| FPC_UE
| FPC_PE
);
1328 const uint32_t xcpt
= ~mask
& (ifps
->fx_status
&
1329 (FPS_IE
| FPS_DE
| FPS_ZE
| FPS_OE
| FPS_UE
| FPS_PE
));
1330 fpxlog(EXC_I386_EXTERR
, ifps
->fx_status
, ifps
->fx_control
, xcpt
);
1336 * Locking not needed:
1337 * . if called from fpu_get_state, pcb already locked.
1338 * . if called from fpnoextflt or fp_intr, we are single-cpu
1339 * . otherwise, thread is running.
1340 * N.B.: Must be called with interrupts disabled
1347 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
1348 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
1351 if (ifps
!= 0 && !ifps
->fp_valid
) {
1352 assert((get_cr0() & CR0_TS
) == 0);
1353 /* registers are in FPU */
1354 ifps
->fp_valid
= TRUE
;
1355 fpu_store_registers(ifps
, thread_is_64bit_addr(thr_act
));
1360 * Restore FPU state from PCB.
1362 * Locking not needed; always called on the current thread.
1369 pcb_t pcb
= THREAD_TO_PCB(thr_act
);
1370 struct x86_fx_thread_state
*ifps
= pcb
->ifps
;
1374 if (ifps
->fp_valid
!= FALSE
&& ifps
->fp_valid
!= TRUE
) {
1375 panic("fp_load() invalid fp_valid: %u, fp_save_layout: %u\n",
1376 ifps
->fp_valid
, ifps
->fp_save_layout
);
1380 if (ifps
->fp_valid
== FALSE
) {
1383 fpu_load_registers(ifps
);
1385 ifps
->fp_valid
= FALSE
; /* in FPU */
1389 * SSE arithmetic exception handling code.
1390 * Basically the same as the x87 exception handler with a different subtype
1394 fpSSEexterrflt(void)
1396 thread_t thr_act
= current_thread();
1397 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1400 intr
= ml_set_interrupts_enabled(FALSE
);
1402 if (get_interrupt_level()) {
1403 panic("SSE exception at interrupt context\n");
1405 if (current_task() == kernel_task
) {
1406 panic("SSE exception in kernel thread context\n");
1410 * Save the FPU state and turn off the FPU.
1414 (void)ml_set_interrupts_enabled(intr
);
1416 * Raise FPU exception.
1417 * Locking not needed on pcb->ifps,
1418 * since thread is running.
1420 const uint32_t mask
= (ifps
->fx_MXCSR
>> 7) &
1421 (FPC_IM
| FPC_DM
| FPC_ZM
| FPC_OM
| FPC_UE
| FPC_PE
);
1422 const uint32_t xcpt
= ~mask
& (ifps
->fx_MXCSR
&
1423 (FPS_IE
| FPS_DE
| FPS_ZE
| FPS_OE
| FPS_UE
| FPS_PE
));
1424 fpxlog(EXC_I386_SSEEXTERR
, ifps
->fx_MXCSR
, ifps
->fx_MXCSR
, xcpt
);
1429 * If a thread is using an AVX-sized savearea:
1430 * - allocate a new AVX512-sized area,
1431 * - copy the 256-bit state into the 512-bit area,
1432 * - deallocate the smaller area
1433 * ASSUMES: thread is the current thread.
1436 fpu_savearea_promote_avx512(thread_t thread
)
1438 struct x86_avx_thread_state
*ifps
= NULL
;
1439 struct x86_avx512_thread_state
*ifps512
= NULL
;
1440 pcb_t pcb
= THREAD_TO_PCB(thread
);
1441 boolean_t do_avx512_alloc
= FALSE
;
1444 assert(thread
== current_thread());
1446 DBG("fpu_savearea_promote_avx512(%p)\n", thread
);
1448 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1452 pcb
->xstate
= AVX512
;
1453 simple_unlock(&pcb
->lock
);
1455 * Now that the PCB xstate has been promoted, set XCR0 so
1456 * that we don't re-trip #UD on the next AVX-512 instruction.
1458 * Since this branch is taken when the first FP instruction
1459 * attempted by this thread is an AVX-512 instruction, we
1460 * call fpnoextflt() to allocate an appropriately-sized
1461 * AVX-512 save-area, thereby avoiding the overhead of another
1462 * fault that would be triggered immediately on return.
1464 intr
= ml_set_interrupts_enabled(FALSE
);
1465 xsetbv(0, AVX512_XMASK
);
1466 current_cpu_datap()->cpu_xstate
= AVX512
;
1467 (void)ml_set_interrupts_enabled(intr
);
1473 if (pcb
->xstate
!= AVX512
) {
1474 do_avx512_alloc
= TRUE
;
1477 simple_unlock(&pcb
->lock
);
1479 if (do_avx512_alloc
== TRUE
) {
1480 ifps512
= fp_state_alloc(AVX512
);
1483 simple_lock(&pcb
->lock
, LCK_GRP_NULL
);
1485 intr
= ml_set_interrupts_enabled(FALSE
);
1491 xsetbv(0, AVX512_XMASK
);
1492 current_cpu_datap()->cpu_xstate
= AVX512
;
1493 (void)ml_set_interrupts_enabled(intr
);
1495 assert(ifps
->fp
.fp_valid
);
1497 /* Allocate an AVX512 savearea and copy AVX state into it */
1498 if (pcb
->xstate
!= AVX512
) {
1499 __nochk_bcopy(ifps
, ifps512
, fp_state_size
[AVX
]);
1500 pcb
->ifps
= ifps512
;
1501 pcb
->xstate
= AVX512
;
1506 /* The PCB lock is redundant in some scenarios given the higher level
1507 * thread mutex, but its pre-emption disablement is relied upon here
1509 simple_unlock(&pcb
->lock
);
1512 fp_state_free(ifps
, AVX
);
1515 fp_state_free(ifps
, AVX512
);
1520 * Upgrade the calling thread to AVX512.
1523 fpu_thread_promote_avx512(thread_t thread
)
1525 task_t task
= current_task();
1527 if (thread
!= current_thread()) {
1530 if (!ml_fpu_avx512_enabled()) {
1534 fpu_savearea_promote_avx512(thread
);
1536 /* Racy but the task's xstate is only a hint */
1537 task
->xstate
= AVX512
;
1544 * Called from user_trap() when an invalid opcode fault is taken.
1545 * If the user is attempting an AVX512 instruction on a machine
1546 * that supports this, we switch the calling thread to use
1547 * a larger savearea, set its XCR0 bit mask to enable AVX512 and
1548 * return to user_trap() with a 0 return value.
1549 * Otherwise, simply return a nonzero value.
1552 #define MAX_X86_INSN_LENGTH (15)
1554 fpUDflt(user_addr_t rip
)
1556 uint8_t instruction_prefix
;
1557 boolean_t is_AVX512_instruction
= FALSE
;
1558 user_addr_t original_rip
= rip
;
1560 /* TODO: as an optimisation, copy up to the lesser of the
1561 * next page boundary or maximal prefix length in one pass
1562 * rather than issue multiple copyins
1564 if (copyin(rip
, (char *) &instruction_prefix
, 1)) {
1567 DBG("fpUDflt(0x%016llx) prefix: 0x%x\n",
1568 rip
, instruction_prefix
);
1569 /* TODO: determine more specifically which prefixes
1570 * are sane possibilities for AVX512 insns
1572 switch (instruction_prefix
) {
1573 case 0x2E: /* CS segment override */
1574 case 0x36: /* SS segment override */
1575 case 0x3E: /* DS segment override */
1576 case 0x26: /* ES segment override */
1577 case 0x64: /* FS segment override */
1578 case 0x65: /* GS segment override */
1579 case 0x66: /* Operand-size override */
1580 case 0x67: /* address-size override */
1581 /* Skip optional prefixes */
1583 if ((rip
- original_rip
) > MAX_X86_INSN_LENGTH
) {
1587 case 0x62: /* EVEX */
1588 case 0xC5: /* VEX 2-byte */
1589 case 0xC4: /* VEX 3-byte */
1590 is_AVX512_instruction
= TRUE
;
1595 } while (!is_AVX512_instruction
);
1597 /* Here if we detect attempted execution of an AVX512 instruction */
1600 * Fail if this machine doesn't support AVX512
1602 if (fpu_capability
!= AVX512
) {
1606 assert(xgetbv(XCR0
) == AVX_XMASK
);
1608 DBG("fpUDflt() switching xstate to AVX512\n");
1609 (void) fpu_thread_promote_avx512(current_thread());
1615 fp_setvalid(boolean_t value
)
1617 thread_t thr_act
= current_thread();
1618 struct x86_fx_thread_state
*ifps
= thr_act
->machine
.ifps
;
1621 ifps
->fp_valid
= value
;
1623 if (value
== TRUE
) {
1624 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1626 ml_set_interrupts_enabled(istate
);
1632 ml_fpu_avx_enabled(void)
1634 return fpu_capability
>= AVX
;
1638 ml_fpu_avx512_enabled(void)
1640 return fpu_capability
== AVX512
;
1644 task_xstate(task_t task
)
1646 if (task
== TASK_NULL
) {
1649 return task
->xstate
;
1654 thread_xstate(thread_t thread
)
1656 xstate_t xs
= THREAD_TO_PCB(thread
)->xstate
;
1657 if (xs
== UNDEFINED
) {
1658 return task_xstate(thread
->task
);
1665 current_xstate(void)
1667 return thread_xstate(current_thread());
1671 * Called when exec'ing between bitnesses.
1672 * If valid FPU state exists, adjust the layout.
1675 fpu_switch_addrmode(thread_t thread
, boolean_t is_64bit
)
1677 struct x86_fx_thread_state
*ifps
= thread
->machine
.ifps
;
1678 mp_disable_preemption();
1680 if (ifps
&& ifps
->fp_valid
) {
1681 if (thread_xstate(thread
) == FP
) {
1682 ifps
->fp_save_layout
= is_64bit
? FXSAVE64
: FXSAVE32
;
1684 ifps
->fp_save_layout
= is_64bit
? XSAVE64
: XSAVE32
;
1687 mp_enable_preemption();
1690 static inline uint32_t
1691 fpsimd_pop(uintptr_t ins
, int sz
)
1698 uint64_t *ins64
= (uint64_t *) ins
;
1699 uint64_t *ins642
= (uint64_t *) (ins
+ 8);
1700 rv1
= __builtin_popcountll(*ins64
);
1701 rv2
= __builtin_popcountll(*ins642
);
1708 uint32_t *ins32
= (uint32_t *) ins
;
1709 rv
+= __builtin_popcount(*ins32
);
1715 char *ins8
= (char *)ins
;
1716 rv
+= __builtin_popcount(*ins8
);
1724 thread_fpsimd_hash(thread_t ft
)
1726 if (fpsimd_fault_popc
== 0) {
1731 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1732 struct x86_fx_thread_state
*pifps
= THREAD_TO_PCB(ft
)->ifps
;
1735 if (pifps
->fp_valid
) {
1736 prv
= fpsimd_pop((uintptr_t) &pifps
->fx_XMM_reg
[0][0],
1737 sizeof(pifps
->fx_XMM_reg
));
1739 uintptr_t cr0
= get_cr0();
1742 prv
= fpsimd_pop((uintptr_t) &pifps
->fx_XMM_reg
[0][0],
1743 sizeof(pifps
->fx_XMM_reg
));
1744 pifps
->fp_valid
= FALSE
;
1750 ml_set_interrupts_enabled(istate
);