]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2010 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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 | ||
1c79356b A |
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> | |
6d2010ae | 64 | #include <mach/branch_predicates.h> |
1c79356b A |
65 | |
66 | #include <kern/mach_param.h> | |
91447636 | 67 | #include <kern/processor.h> |
1c79356b A |
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 | ||
060df5ea A |
74 | #include <libkern/OSAtomic.h> |
75 | ||
0c530ab8 | 76 | #include <architecture/i386/pio.h> |
55e303ae | 77 | #include <i386/cpuid.h> |
b0d623f7 | 78 | #include <i386/fpu.h> |
0c530ab8 | 79 | #include <i386/proc_reg.h> |
b0d623f7 A |
80 | #include <i386/misc_protos.h> |
81 | #include <i386/thread.h> | |
82 | #include <i386/trap.h> | |
1c79356b | 83 | |
0c530ab8 | 84 | int fp_kind = FP_NO; /* not inited */ |
1c79356b A |
85 | zone_t ifps_zone; /* zone for FPU save area */ |
86 | ||
b0d623f7 | 87 | #define ALIGNED(addr,size) (((uintptr_t)(addr)&((size)-1))==0) |
1c79356b A |
88 | |
89 | /* Forward */ | |
90 | ||
91 | extern void fpinit(void); | |
92 | extern void fp_save( | |
91447636 | 93 | thread_t thr_act); |
1c79356b | 94 | extern void fp_load( |
91447636 | 95 | thread_t thr_act); |
1c79356b | 96 | |
060df5ea | 97 | static void configure_mxcsr_capability_mask(struct x86_avx_thread_state *fps); |
0c530ab8 | 98 | |
060df5ea | 99 | struct x86_avx_thread_state initial_fp_state __attribute((aligned(64))); |
0c530ab8 A |
100 | |
101 | ||
102 | /* Global MXCSR capability bitmask */ | |
103 | static unsigned int mxcsr_capability_mask; | |
104 | ||
060df5ea A |
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 | ||
0c530ab8 | 175 | /* |
060df5ea | 176 | * Configure the initial FPU state presented to new threads. |
0c530ab8 A |
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 | */ | |
060df5ea | 181 | |
0c530ab8 | 182 | static void |
060df5ea | 183 | configure_mxcsr_capability_mask(struct x86_avx_thread_state *fps) |
0c530ab8 | 184 | { |
060df5ea A |
185 | /* XSAVE requires a 64 byte aligned store */ |
186 | assert(ALIGNED(fps, 64)); | |
0c530ab8 | 187 | /* Clear, to prepare for the diagnostic FXSAVE */ |
060df5ea A |
188 | bzero(fps, sizeof(*fps)); |
189 | ||
190 | fpinit(); | |
191 | fpu_store_registers(fps, FALSE); | |
192 | ||
193 | mxcsr_capability_mask = fps->fx_MXCSR_MASK; | |
0c530ab8 A |
194 | |
195 | /* Set default mask value if necessary */ | |
196 | if (mxcsr_capability_mask == 0) | |
197 | mxcsr_capability_mask = 0xffbf; | |
198 | ||
060df5ea A |
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)); | |
0c530ab8 | 202 | |
060df5ea A |
203 | fps->fp_valid = TRUE; |
204 | fps->fp_save_layout = fpu_YMM_present ? XSAVE32: FXSAVE32; | |
205 | fpu_load_registers(fps); | |
0c530ab8 | 206 | |
060df5ea A |
207 | /* Poison values to trap unsafe usage */ |
208 | fps->fp_valid = 0xFFFFFFFF; | |
209 | fps->fp_save_layout = FP_UNUSED; | |
0c530ab8 | 210 | |
060df5ea A |
211 | /* Re-enable FPU/SSE DNA exceptions */ |
212 | set_ts(); | |
0c530ab8 A |
213 | } |
214 | ||
215 | ||
1c79356b A |
216 | /* |
217 | * Look for FPU and initialize it. | |
218 | * Called on each CPU. | |
219 | */ | |
220 | void | |
221 | init_fpu(void) | |
222 | { | |
060df5ea A |
223 | #if DEBUG |
224 | unsigned short status; | |
225 | unsigned short control; | |
226 | #endif | |
1c79356b A |
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 | */ | |
91447636 | 232 | set_cr0((get_cr0() & ~(CR0_EM|CR0_TS)) | CR0_NE); /* allow use of FPU */ |
1c79356b | 233 | fninit(); |
060df5ea | 234 | #if DEBUG |
1c79356b A |
235 | status = fnstsw(); |
236 | fnstcw(&control); | |
060df5ea A |
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); | |
1c79356b | 249 | |
060df5ea A |
250 | } else |
251 | panic("fpu is not FP_FXSR"); | |
55e303ae | 252 | |
060df5ea A |
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); | |
0c530ab8 | 291 | |
060df5ea A |
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); | |
1c79356b | 295 | } |
060df5ea A |
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); | |
1c79356b | 354 | else |
060df5ea A |
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; | |
1c79356b | 388 | } |
060df5ea A |
389 | else { |
390 | fxsave(ifps); | |
391 | ifps->fp_save_layout = is64 ? FXSAVE64 : FXSAVE32; | |
392 | } | |
393 | #endif | |
1c79356b A |
394 | } |
395 | ||
396 | /* | |
397 | * Initialize FP handling. | |
398 | */ | |
060df5ea | 399 | |
1c79356b A |
400 | void |
401 | fpu_module_init(void) | |
402 | { | |
060df5ea A |
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, | |
0c530ab8 | 416 | "x86 fpsave state"); |
060df5ea A |
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); | |
6d2010ae | 438 | ifps = (thread)->machine.ifps; |
060df5ea A |
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 */ | |
6d2010ae | 452 | fpu_store_registers(ifps, (thread_is_64bit(thread) && is_saved_state64(thread->machine.iss))); |
060df5ea A |
453 | ifps->fp_valid = TRUE; |
454 | } | |
455 | set_ts(); | |
1c79356b A |
456 | } |
457 | ||
060df5ea | 458 | |
1c79356b A |
459 | /* |
460 | * Free a FPU save area. | |
461 | * Called only when thread terminating - no locking necessary. | |
462 | */ | |
463 | void | |
060df5ea | 464 | fpu_free(void *fps) |
1c79356b | 465 | { |
0c530ab8 | 466 | fp_state_free(fps); |
1c79356b A |
467 | } |
468 | ||
55e303ae A |
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( | |
060df5ea A |
480 | thread_t thr_act, |
481 | thread_state_t tstate, | |
482 | thread_flavor_t f) | |
55e303ae | 483 | { |
060df5ea A |
484 | struct x86_fx_thread_state *ifps; |
485 | struct x86_fx_thread_state *new_ifps; | |
0c530ab8 A |
486 | x86_float_state64_t *state; |
487 | pcb_t pcb; | |
060df5ea A |
488 | size_t state_size = (((f == x86_AVX_STATE32) || (f == x86_AVX_STATE64)) && (fpu_YMM_present == TRUE)) ? sizeof(struct x86_avx_thread_state) : sizeof(struct x86_fx_thread_state); |
489 | boolean_t old_valid; | |
55e303ae | 490 | if (fp_kind == FP_NO) |
2d21ac55 | 491 | return KERN_FAILURE; |
0c530ab8 A |
492 | |
493 | state = (x86_float_state64_t *)tstate; | |
55e303ae | 494 | |
91447636 | 495 | assert(thr_act != THREAD_NULL); |
6d2010ae | 496 | pcb = THREAD_TO_PCB(thr_act); |
55e303ae | 497 | |
0c530ab8 | 498 | if (state == NULL) { |
2d21ac55 A |
499 | /* |
500 | * new FPU state is 'invalid'. | |
501 | * Deallocate the fp state if it exists. | |
502 | */ | |
503 | simple_lock(&pcb->lock); | |
0c530ab8 A |
504 | |
505 | ifps = pcb->ifps; | |
506 | pcb->ifps = 0; | |
4452a7af | 507 | |
2d21ac55 | 508 | simple_unlock(&pcb->lock); |
0c530ab8 A |
509 | |
510 | if (ifps != 0) | |
2d21ac55 | 511 | fp_state_free(ifps); |
0c530ab8 | 512 | } else { |
2d21ac55 A |
513 | /* |
514 | * Valid state. Allocate the fp state if there is none. | |
515 | */ | |
516 | new_ifps = 0; | |
55e303ae | 517 | Retry: |
2d21ac55 | 518 | simple_lock(&pcb->lock); |
0c530ab8 A |
519 | |
520 | ifps = pcb->ifps; | |
2d21ac55 A |
521 | if (ifps == 0) { |
522 | if (new_ifps == 0) { | |
523 | simple_unlock(&pcb->lock); | |
524 | new_ifps = fp_state_alloc(); | |
525 | goto Retry; | |
55e303ae | 526 | } |
2d21ac55 A |
527 | ifps = new_ifps; |
528 | new_ifps = 0; | |
060df5ea | 529 | pcb->ifps = ifps; |
2d21ac55 A |
530 | } |
531 | /* | |
532 | * now copy over the new data. | |
533 | */ | |
060df5ea | 534 | old_valid = ifps->fp_valid; |
4452a7af | 535 | |
060df5ea A |
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 | bcopy((char *)&state->fpu_fcw, (char *)ifps, state_size); | |
543 | ||
544 | if (fpu_YMM_present) { | |
545 | struct x86_avx_thread_state *iavx = (void *) ifps; | |
546 | iavx->fp_save_layout = thread_is_64bit(thr_act) ? XSAVE64 : XSAVE32; | |
547 | /* Sanitize XSAVE header */ | |
548 | bzero(&iavx->_xh.xhrsvd[0], sizeof(iavx->_xh.xhrsvd)); | |
549 | if (state_size == sizeof(struct x86_avx_thread_state)) | |
550 | iavx->_xh.xsbv = (XFEM_YMM | XFEM_SSE | XFEM_X87); | |
551 | else | |
552 | iavx->_xh.xsbv = (XFEM_SSE | XFEM_X87); | |
553 | } | |
554 | else | |
0c530ab8 | 555 | ifps->fp_save_layout = thread_is_64bit(thr_act) ? FXSAVE64 : FXSAVE32; |
060df5ea | 556 | ifps->fp_valid = old_valid; |
2d21ac55 | 557 | |
060df5ea A |
558 | if (old_valid == FALSE) { |
559 | boolean_t istate = ml_set_interrupts_enabled(FALSE); | |
560 | ifps->fp_valid = TRUE; | |
561 | set_ts(); | |
562 | ml_set_interrupts_enabled(istate); | |
563 | } | |
0c530ab8 A |
564 | /* |
565 | * Clear any reserved bits in the MXCSR to prevent a GPF | |
566 | * when issuing an FXRSTOR. | |
567 | */ | |
060df5ea | 568 | ifps->fx_MXCSR &= mxcsr_capability_mask; |
6601e61a | 569 | |
2d21ac55 | 570 | simple_unlock(&pcb->lock); |
0c530ab8 | 571 | |
2d21ac55 A |
572 | if (new_ifps != 0) |
573 | fp_state_free(new_ifps); | |
0c530ab8 | 574 | } |
55e303ae A |
575 | return KERN_SUCCESS; |
576 | } | |
577 | ||
578 | /* | |
579 | * Get the floating-point state for a thread. | |
580 | * If the thread is not the current thread, it is | |
581 | * not running (held). Locking needed against | |
582 | * concurrent fpu_set_state or fpu_get_state. | |
583 | */ | |
584 | kern_return_t | |
585 | fpu_get_fxstate( | |
060df5ea A |
586 | thread_t thr_act, |
587 | thread_state_t tstate, | |
588 | thread_flavor_t f) | |
55e303ae | 589 | { |
060df5ea | 590 | struct x86_fx_thread_state *ifps; |
0c530ab8 A |
591 | x86_float_state64_t *state; |
592 | kern_return_t ret = KERN_FAILURE; | |
593 | pcb_t pcb; | |
060df5ea | 594 | size_t state_size = (((f == x86_AVX_STATE32) || (f == x86_AVX_STATE64)) && (fpu_YMM_present == TRUE)) ? sizeof(struct x86_avx_thread_state) : sizeof(struct x86_fx_thread_state); |
55e303ae | 595 | |
0c530ab8 | 596 | if (fp_kind == FP_NO) |
2d21ac55 | 597 | return KERN_FAILURE; |
0c530ab8 A |
598 | |
599 | state = (x86_float_state64_t *)tstate; | |
55e303ae | 600 | |
91447636 | 601 | assert(thr_act != THREAD_NULL); |
6d2010ae | 602 | pcb = THREAD_TO_PCB(thr_act); |
55e303ae A |
603 | |
604 | simple_lock(&pcb->lock); | |
0c530ab8 A |
605 | |
606 | ifps = pcb->ifps; | |
55e303ae | 607 | if (ifps == 0) { |
2d21ac55 | 608 | /* |
0c530ab8 A |
609 | * No valid floating-point state. |
610 | */ | |
060df5ea A |
611 | |
612 | bcopy((char *)&initial_fp_state, (char *)&state->fpu_fcw, | |
613 | state_size); | |
0c530ab8 A |
614 | |
615 | simple_unlock(&pcb->lock); | |
6601e61a | 616 | |
0c530ab8 A |
617 | return KERN_SUCCESS; |
618 | } | |
619 | /* | |
620 | * Make sure we`ve got the latest fp state info | |
621 | * If the live fpu state belongs to our target | |
622 | */ | |
2d21ac55 A |
623 | if (thr_act == current_thread()) { |
624 | boolean_t intr; | |
8f6c56a5 | 625 | |
0c530ab8 | 626 | intr = ml_set_interrupts_enabled(FALSE); |
89b3af67 | 627 | |
0c530ab8 A |
628 | clear_ts(); |
629 | fp_save(thr_act); | |
630 | clear_fpu(); | |
6601e61a | 631 | |
0c530ab8 | 632 | (void)ml_set_interrupts_enabled(intr); |
6601e61a | 633 | } |
0c530ab8 | 634 | if (ifps->fp_valid) { |
060df5ea | 635 | bcopy((char *)ifps, (char *)&state->fpu_fcw, state_size); |
0c530ab8 | 636 | ret = KERN_SUCCESS; |
6601e61a | 637 | } |
0c530ab8 | 638 | simple_unlock(&pcb->lock); |
21362eb3 | 639 | |
0c530ab8 | 640 | return ret; |
6601e61a | 641 | } |
21362eb3 | 642 | |
0c530ab8 | 643 | |
2d21ac55 | 644 | |
6601e61a | 645 | /* |
0c530ab8 A |
646 | * the child thread is 'stopped' with the thread |
647 | * mutex held and is currently not known by anyone | |
648 | * so no way for fpu state to get manipulated by an | |
649 | * outside agency -> no need for pcb lock | |
6601e61a | 650 | */ |
0c530ab8 A |
651 | |
652 | void | |
653 | fpu_dup_fxstate( | |
654 | thread_t parent, | |
655 | thread_t child) | |
6601e61a | 656 | { |
060df5ea A |
657 | struct x86_fx_thread_state *new_ifps = NULL; |
658 | boolean_t intr; | |
0c530ab8 | 659 | pcb_t ppcb; |
21362eb3 | 660 | |
6d2010ae | 661 | ppcb = THREAD_TO_PCB(parent); |
21362eb3 | 662 | |
0c530ab8 A |
663 | if (ppcb->ifps == NULL) |
664 | return; | |
4452a7af | 665 | |
6d2010ae | 666 | if (child->machine.ifps) |
0c530ab8 | 667 | panic("fpu_dup_fxstate: child's ifps non-null"); |
4452a7af | 668 | |
0c530ab8 | 669 | new_ifps = fp_state_alloc(); |
5d5c5d0d | 670 | |
0c530ab8 | 671 | simple_lock(&ppcb->lock); |
6601e61a | 672 | |
0c530ab8 | 673 | if (ppcb->ifps != NULL) { |
060df5ea | 674 | struct x86_fx_thread_state *ifps = ppcb->ifps; |
0c530ab8 A |
675 | /* |
676 | * Make sure we`ve got the latest fp state info | |
677 | */ | |
678 | intr = ml_set_interrupts_enabled(FALSE); | |
060df5ea | 679 | assert(current_thread() == parent); |
0c530ab8 A |
680 | clear_ts(); |
681 | fp_save(parent); | |
682 | clear_fpu(); | |
6601e61a | 683 | |
0c530ab8 | 684 | (void)ml_set_interrupts_enabled(intr); |
6601e61a | 685 | |
060df5ea | 686 | if (ifps->fp_valid) { |
6d2010ae | 687 | child->machine.ifps = new_ifps; |
060df5ea A |
688 | assert((fp_register_state_size == sizeof(struct x86_fx_thread_state)) || |
689 | (fp_register_state_size == sizeof(struct x86_avx_thread_state))); | |
690 | bcopy((char *)(ppcb->ifps), | |
6d2010ae | 691 | (char *)(child->machine.ifps), fp_register_state_size); |
0c530ab8 | 692 | |
2d21ac55 A |
693 | /* Mark the new fp saved state as non-live. */ |
694 | /* Temporarily disabled: radar 4647827 | |
695 | * new_ifps->fp_valid = TRUE; | |
696 | */ | |
060df5ea | 697 | |
0c530ab8 A |
698 | /* |
699 | * Clear any reserved bits in the MXCSR to prevent a GPF | |
700 | * when issuing an FXRSTOR. | |
701 | */ | |
060df5ea | 702 | new_ifps->fx_MXCSR &= mxcsr_capability_mask; |
0c530ab8 A |
703 | new_ifps = NULL; |
704 | } | |
6601e61a | 705 | } |
0c530ab8 | 706 | simple_unlock(&ppcb->lock); |
89b3af67 | 707 | |
0c530ab8 A |
708 | if (new_ifps != NULL) |
709 | fp_state_free(new_ifps); | |
6601e61a | 710 | } |
4452a7af | 711 | |
0c530ab8 | 712 | |
1c79356b A |
713 | /* |
714 | * Initialize FPU. | |
715 | * | |
1c79356b | 716 | */ |
060df5ea | 717 | |
1c79356b A |
718 | void |
719 | fpinit(void) | |
720 | { | |
721 | unsigned short control; | |
722 | ||
1c79356b A |
723 | clear_ts(); |
724 | fninit(); | |
725 | fnstcw(&control); | |
726 | control &= ~(FPC_PC|FPC_RC); /* Clear precision & rounding control */ | |
0c530ab8 | 727 | control |= (FPC_PC_64 | /* Set precision */ |
1c79356b A |
728 | FPC_RC_RN | /* round-to-nearest */ |
729 | FPC_ZE | /* Suppress zero-divide */ | |
730 | FPC_OE | /* and overflow */ | |
731 | FPC_UE | /* underflow */ | |
732 | FPC_IE | /* Allow NaNQs and +-INF */ | |
733 | FPC_DE | /* Allow denorms as operands */ | |
734 | FPC_PE); /* No trap for precision loss */ | |
735 | fldcw(control); | |
0c530ab8 A |
736 | |
737 | /* Initialize SSE/SSE2 */ | |
060df5ea | 738 | __builtin_ia32_ldmxcsr(0x1f80); |
b0d623f7 | 739 | } |
1c79356b A |
740 | |
741 | /* | |
742 | * Coprocessor not present. | |
743 | */ | |
744 | ||
745 | void | |
746 | fpnoextflt(void) | |
747 | { | |
0c530ab8 | 748 | boolean_t intr; |
2d21ac55 A |
749 | thread_t thr_act; |
750 | pcb_t pcb; | |
060df5ea | 751 | struct x86_fx_thread_state *ifps = 0; |
2d21ac55 A |
752 | |
753 | thr_act = current_thread(); | |
6d2010ae | 754 | pcb = THREAD_TO_PCB(thr_act); |
2d21ac55 | 755 | |
060df5ea | 756 | assert(fp_register_state_size != 0); |
4452a7af | 757 | |
060df5ea A |
758 | if (pcb->ifps == 0 && !get_interrupt_level()) { |
759 | ifps = fp_state_alloc(); | |
760 | bcopy((char *)&initial_fp_state, (char *)ifps, | |
761 | fp_register_state_size); | |
762 | if (!thread_is_64bit(thr_act)) { | |
763 | ifps->fp_save_layout = fpu_YMM_present ? XSAVE32 : FXSAVE32; | |
764 | } | |
765 | else | |
766 | ifps->fp_save_layout = fpu_YMM_present ? XSAVE64 : FXSAVE64; | |
767 | ifps->fp_valid = TRUE; | |
768 | } | |
0c530ab8 A |
769 | intr = ml_set_interrupts_enabled(FALSE); |
770 | ||
771 | clear_ts(); /* Enable FPU use */ | |
772 | ||
6d2010ae | 773 | if (__improbable(get_interrupt_level())) { |
0c530ab8 A |
774 | /* |
775 | * Save current coprocessor context if valid | |
776 | * Initialize coprocessor live context | |
777 | */ | |
2d21ac55 | 778 | fp_save(thr_act); |
0c530ab8 A |
779 | fpinit(); |
780 | } else { | |
2d21ac55 A |
781 | if (pcb->ifps == 0) { |
782 | pcb->ifps = ifps; | |
783 | ifps = 0; | |
784 | } | |
0c530ab8 A |
785 | /* |
786 | * Load this thread`s state into coprocessor live context. | |
787 | */ | |
2d21ac55 | 788 | fp_load(thr_act); |
0c530ab8 | 789 | } |
0c530ab8 | 790 | (void)ml_set_interrupts_enabled(intr); |
2d21ac55 A |
791 | |
792 | if (ifps) | |
793 | fp_state_free(ifps); | |
1c79356b A |
794 | } |
795 | ||
796 | /* | |
797 | * FPU overran end of segment. | |
798 | * Re-initialize FPU. Floating point state is not valid. | |
799 | */ | |
800 | ||
801 | void | |
802 | fpextovrflt(void) | |
803 | { | |
0c530ab8 A |
804 | thread_t thr_act = current_thread(); |
805 | pcb_t pcb; | |
060df5ea | 806 | struct x86_fx_thread_state *ifps; |
0c530ab8 A |
807 | boolean_t intr; |
808 | ||
809 | intr = ml_set_interrupts_enabled(FALSE); | |
810 | ||
811 | if (get_interrupt_level()) | |
2d21ac55 | 812 | panic("FPU segment overrun exception at interrupt context\n"); |
0c530ab8 A |
813 | if (current_task() == kernel_task) |
814 | panic("FPU segment overrun exception in kernel thread context\n"); | |
1c79356b | 815 | |
1c79356b A |
816 | /* |
817 | * This is a non-recoverable error. | |
818 | * Invalidate the thread`s FPU state. | |
819 | */ | |
6d2010ae | 820 | pcb = THREAD_TO_PCB(thr_act); |
1c79356b | 821 | simple_lock(&pcb->lock); |
0c530ab8 A |
822 | ifps = pcb->ifps; |
823 | pcb->ifps = 0; | |
1c79356b A |
824 | simple_unlock(&pcb->lock); |
825 | ||
826 | /* | |
827 | * Re-initialize the FPU. | |
828 | */ | |
829 | clear_ts(); | |
830 | fninit(); | |
831 | ||
832 | /* | |
833 | * And disable access. | |
834 | */ | |
835 | clear_fpu(); | |
836 | ||
0c530ab8 A |
837 | (void)ml_set_interrupts_enabled(intr); |
838 | ||
1c79356b | 839 | if (ifps) |
91447636 | 840 | zfree(ifps_zone, ifps); |
1c79356b A |
841 | |
842 | /* | |
843 | * Raise exception. | |
844 | */ | |
845 | i386_exception(EXC_BAD_ACCESS, VM_PROT_READ|VM_PROT_EXECUTE, 0); | |
846 | /*NOTREACHED*/ | |
847 | } | |
848 | ||
849 | /* | |
850 | * FPU error. Called by AST. | |
851 | */ | |
852 | ||
853 | void | |
854 | fpexterrflt(void) | |
855 | { | |
0c530ab8 | 856 | thread_t thr_act = current_thread(); |
6d2010ae | 857 | struct x86_fx_thread_state *ifps = thr_act->machine.ifps; |
0c530ab8 A |
858 | boolean_t intr; |
859 | ||
860 | intr = ml_set_interrupts_enabled(FALSE); | |
861 | ||
862 | if (get_interrupt_level()) | |
863 | panic("FPU error exception at interrupt context\n"); | |
864 | if (current_task() == kernel_task) | |
865 | panic("FPU error exception in kernel thread context\n"); | |
1c79356b | 866 | |
1c79356b A |
867 | /* |
868 | * Save the FPU state and turn off the FPU. | |
869 | */ | |
870 | fp_save(thr_act); | |
1c79356b | 871 | |
0c530ab8 A |
872 | (void)ml_set_interrupts_enabled(intr); |
873 | ||
1c79356b A |
874 | /* |
875 | * Raise FPU exception. | |
0c530ab8 | 876 | * Locking not needed on pcb->ifps, |
1c79356b A |
877 | * since thread is running. |
878 | */ | |
879 | i386_exception(EXC_ARITHMETIC, | |
880 | EXC_I386_EXTERR, | |
060df5ea | 881 | ifps->fx_status); |
0c530ab8 | 882 | |
1c79356b A |
883 | /*NOTREACHED*/ |
884 | } | |
885 | ||
886 | /* | |
887 | * Save FPU state. | |
888 | * | |
889 | * Locking not needed: | |
890 | * . if called from fpu_get_state, pcb already locked. | |
891 | * . if called from fpnoextflt or fp_intr, we are single-cpu | |
892 | * . otherwise, thread is running. | |
0c530ab8 | 893 | * N.B.: Must be called with interrupts disabled |
1c79356b | 894 | */ |
0c530ab8 | 895 | |
1c79356b A |
896 | void |
897 | fp_save( | |
91447636 | 898 | thread_t thr_act) |
1c79356b | 899 | { |
6d2010ae | 900 | pcb_t pcb = THREAD_TO_PCB(thr_act); |
060df5ea | 901 | struct x86_fx_thread_state *ifps = pcb->ifps; |
0c530ab8 | 902 | |
060df5ea | 903 | assert(ifps != 0); |
1c79356b | 904 | if (ifps != 0 && !ifps->fp_valid) { |
0c530ab8 A |
905 | assert((get_cr0() & CR0_TS) == 0); |
906 | /* registers are in FPU */ | |
907 | ifps->fp_valid = TRUE; | |
060df5ea | 908 | fpu_store_registers(ifps, thread_is_64bit(thr_act)); |
1c79356b A |
909 | } |
910 | } | |
911 | ||
912 | /* | |
913 | * Restore FPU state from PCB. | |
914 | * | |
915 | * Locking not needed; always called on the current thread. | |
916 | */ | |
917 | ||
918 | void | |
919 | fp_load( | |
91447636 | 920 | thread_t thr_act) |
1c79356b | 921 | { |
6d2010ae | 922 | pcb_t pcb = THREAD_TO_PCB(thr_act); |
060df5ea | 923 | struct x86_fx_thread_state *ifps = pcb->ifps; |
0c530ab8 | 924 | |
060df5ea A |
925 | assert(ifps); |
926 | assert(ifps->fp_valid == FALSE || ifps->fp_valid == TRUE); | |
927 | ||
928 | if (ifps->fp_valid == FALSE) { | |
0c530ab8 | 929 | fpinit(); |
1c79356b | 930 | } else { |
060df5ea | 931 | fpu_load_registers(ifps); |
1c79356b A |
932 | } |
933 | ifps->fp_valid = FALSE; /* in FPU */ | |
934 | } | |
935 | ||
1c79356b | 936 | /* |
0c530ab8 A |
937 | * SSE arithmetic exception handling code. |
938 | * Basically the same as the x87 exception handler with a different subtype | |
1c79356b A |
939 | */ |
940 | ||
941 | void | |
0c530ab8 | 942 | fpSSEexterrflt(void) |
1c79356b | 943 | { |
0c530ab8 | 944 | thread_t thr_act = current_thread(); |
6d2010ae | 945 | struct x86_fx_thread_state *ifps = thr_act->machine.ifps; |
0c530ab8 | 946 | boolean_t intr; |
4452a7af | 947 | |
0c530ab8 A |
948 | intr = ml_set_interrupts_enabled(FALSE); |
949 | ||
950 | if (get_interrupt_level()) | |
951 | panic("SSE exception at interrupt context\n"); | |
952 | if (current_task() == kernel_task) | |
953 | panic("SSE exception in kernel thread context\n"); | |
1c79356b A |
954 | |
955 | /* | |
0c530ab8 | 956 | * Save the FPU state and turn off the FPU. |
1c79356b | 957 | */ |
1c79356b | 958 | fp_save(thr_act); |
1c79356b | 959 | |
0c530ab8 | 960 | (void)ml_set_interrupts_enabled(intr); |
1c79356b | 961 | /* |
0c530ab8 A |
962 | * Raise FPU exception. |
963 | * Locking not needed on pcb->ifps, | |
964 | * since thread is running. | |
1c79356b | 965 | */ |
0c530ab8 A |
966 | assert(ifps->fp_save_layout == FXSAVE32 || ifps->fp_save_layout == FXSAVE64); |
967 | i386_exception(EXC_ARITHMETIC, | |
968 | EXC_I386_SSEEXTERR, | |
060df5ea | 969 | ifps->fx_MXCSR); |
0c530ab8 A |
970 | /*NOTREACHED*/ |
971 | } | |
972 | ||
0c530ab8 A |
973 | void |
974 | fp_setvalid(boolean_t value) { | |
975 | thread_t thr_act = current_thread(); | |
6d2010ae | 976 | struct x86_fx_thread_state *ifps = thr_act->machine.ifps; |
0c530ab8 A |
977 | |
978 | if (ifps) { | |
979 | ifps->fp_valid = value; | |
980 | ||
060df5ea A |
981 | if (value == TRUE) { |
982 | boolean_t istate = ml_set_interrupts_enabled(FALSE); | |
0c530ab8 | 983 | clear_fpu(); |
060df5ea A |
984 | ml_set_interrupts_enabled(istate); |
985 | } | |
0c530ab8 | 986 | } |
1c79356b | 987 | } |
060df5ea | 988 | |
6d2010ae | 989 | __private_extern__ boolean_t |
060df5ea A |
990 | ml_fpu_avx_enabled(void) { |
991 | return (fpu_YMM_present == TRUE); | |
992 | } |