2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1992-1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 #include <platforms.h>
63 #include <mach/exception_types.h>
64 #include <mach/i386/thread_status.h>
65 #include <mach/i386/fp_reg.h>
67 #include <kern/mach_param.h>
68 #include <kern/processor.h>
69 #include <kern/thread.h>
70 #include <kern/zalloc.h>
71 #include <kern/misc_protos.h>
73 #include <kern/assert.h>
75 #include <i386/thread.h>
77 #include <i386/trap.h>
79 #include <i386/cpuid.h>
80 #include <i386/misc_protos.h>
85 #define ASSERT_IPL(L) \
87 if (curr_ipl != L) { \
88 printf("IPL is %d, expected %d\n", curr_ipl, L); \
89 panic("fpu: wrong ipl"); \
96 int fp_kind
= FP_387
; /* 80387 present */
97 zone_t ifps_zone
; /* zone for FPU save area */
104 #define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
108 extern void fpinit(void);
115 * Look for FPU and initialize it.
116 * Called on each CPU.
121 unsigned short status
, control
;
124 * Check for FPU by initializing it,
125 * then trying to read the correct bit patterns from
126 * the control and status registers.
128 set_cr0((get_cr0() & ~(CR0_EM
|CR0_TS
)) | CR0_NE
); /* allow use of FPU */
134 if ((status
& 0xff) == 0 &&
135 (control
& 0x103f) == 0x3f)
137 fp_kind
= FP_387
; /* assume we have a 387 compatible instruction set */
138 /* Use FPU save/restore instructions if available */
139 if (cpuid_features() & CPUID_FEATURE_FXSR
) {
141 set_cr4(get_cr4() | CR4_FXS
);
142 printf("Enabling XMM register save/restore");
143 /* And allow SIMD instructions if present */
144 if (cpuid_features() & CPUID_FEATURE_SSE
) {
145 printf(" and SSE/SSE2");
146 set_cr4(get_cr4() | CR4_XMM
);
148 printf(" opcodes\n");
152 * Trap wait instructions. Turn off FPU for now.
154 set_cr0(get_cr0() | CR0_TS
| CR0_MP
);
162 set_cr0(get_cr0() | CR0_EM
);
167 * Initialize FP handling.
170 fpu_module_init(void)
172 ifps_zone
= zinit(sizeof(struct i386_fpsave_state
),
173 THREAD_MAX
* sizeof(struct i386_fpsave_state
),
174 THREAD_CHUNK
* sizeof(struct i386_fpsave_state
),
175 "i386 fpsave state");
179 * Free a FPU save area.
180 * Called only when thread terminating - no locking necessary.
184 struct i386_fpsave_state
*fps
;
187 zfree(ifps_zone
, fps
);
191 * Set the floating-point state for a thread based
192 * on the FXSave formatted data. This is basically
193 * the same as fpu_set_state except it uses the
194 * expanded data structure.
195 * If the thread is not the current thread, it is
196 * not running (held). Locking needed against
197 * concurrent fpu_set_state or fpu_get_state.
202 struct i386_float_state
*state
)
205 register struct i386_fpsave_state
*ifps
;
206 register struct i386_fpsave_state
*new_ifps
;
209 if (fp_kind
== FP_NO
)
212 if (state
->fpkind
!= FP_FXSR
) {
213 /* strange if this happens, but in case someone builds one of these manually... */
214 return fpu_set_state(thr_act
, state
);
217 assert(thr_act
!= THREAD_NULL
);
218 pcb
= thr_act
->machine
.pcb
;
220 if (state
->initialized
== 0) {
222 * new FPU state is 'invalid'.
223 * Deallocate the fp state if it exists.
225 simple_lock(&pcb
->lock
);
226 ifps
= pcb
->ims
.ifps
;
228 simple_unlock(&pcb
->lock
);
231 zfree(ifps_zone
, ifps
);
236 * Valid state. Allocate the fp state if there is none.
241 simple_lock(&pcb
->lock
);
242 ifps
= pcb
->ims
.ifps
;
245 simple_unlock(&pcb
->lock
);
246 new_ifps
= (struct i386_fpsave_state
*) zalloc(ifps_zone
);
247 assert(ALIGNED(new_ifps
,16));
252 bzero((char *)ifps
, sizeof *ifps
);
253 pcb
->ims
.ifps
= ifps
;
257 * now copy over the new data.
259 bcopy((char *)&state
->hw_state
[0], (char *)&ifps
->fx_save_state
, sizeof(struct i386_fx_save
));
260 ifps
->fp_save_flavor
= FP_FXSR
;
261 simple_unlock(&pcb
->lock
);
263 zfree(ifps_zone
, ifps
);
270 * Get the floating-point state for a thread.
271 * If the thread is not the current thread, it is
272 * not running (held). Locking needed against
273 * concurrent fpu_set_state or fpu_get_state.
278 register struct i386_float_state
*state
)
281 register struct i386_fpsave_state
*ifps
;
284 if (fp_kind
== FP_NO
) {
286 } else if (fp_kind
== FP_387
) {
287 return fpu_get_state(thr_act
, state
);
290 assert(thr_act
!= THREAD_NULL
);
291 pcb
= thr_act
->machine
.pcb
;
293 simple_lock(&pcb
->lock
);
294 ifps
= pcb
->ims
.ifps
;
297 * No valid floating-point state.
299 simple_unlock(&pcb
->lock
);
300 bzero((char *)state
, sizeof(struct i386_float_state
));
304 /* Make sure we`ve got the latest fp state info */
305 /* If the live fpu state belongs to our target */
306 if (thr_act
== current_thread())
313 state
->fpkind
= fp_kind
;
314 state
->exc_status
= 0;
315 state
->initialized
= ifps
->fp_valid
;
316 bcopy( (char *)&ifps
->fx_save_state
, (char *)&state
->hw_state
[0], sizeof(struct i386_fx_save
));
318 simple_unlock(&pcb
->lock
);
324 * Set the floating-point state for a thread.
325 * If the thread is not the current thread, it is
326 * not running (held). Locking needed against
327 * concurrent fpu_set_state or fpu_get_state.
332 struct i386_float_state
*state
)
335 register struct i386_fpsave_state
*ifps
;
336 register struct i386_fpsave_state
*new_ifps
;
339 if (fp_kind
== FP_NO
)
342 assert(thr_act
!= THREAD_NULL
);
343 pcb
= thr_act
->machine
.pcb
;
345 if (state
->initialized
== 0) {
347 * new FPU state is 'invalid'.
348 * Deallocate the fp state if it exists.
350 simple_lock(&pcb
->lock
);
351 ifps
= pcb
->ims
.ifps
;
353 simple_unlock(&pcb
->lock
);
356 zfree(ifps_zone
, ifps
);
361 * Valid state. Allocate the fp state if there is none.
363 register struct i386_fp_save
*user_fp_state
;
364 register struct i386_fp_regs
*user_fp_regs
;
366 user_fp_state
= (struct i386_fp_save
*) &state
->hw_state
[0];
367 user_fp_regs
= (struct i386_fp_regs
*)
368 &state
->hw_state
[sizeof(struct i386_fp_save
)];
372 simple_lock(&pcb
->lock
);
373 ifps
= pcb
->ims
.ifps
;
376 simple_unlock(&pcb
->lock
);
377 new_ifps
= (struct i386_fpsave_state
*) zalloc(ifps_zone
);
378 assert(ALIGNED(new_ifps
,16));
383 bzero((char *)ifps
, sizeof *ifps
); // zero ALL fields first
384 pcb
->ims
.ifps
= ifps
;
388 * Ensure that reserved parts of the environment are 0.
390 bzero((char *)&ifps
->fp_save_state
, sizeof(struct i386_fp_save
));
392 ifps
->fp_save_state
.fp_control
= user_fp_state
->fp_control
;
393 ifps
->fp_save_state
.fp_status
= user_fp_state
->fp_status
;
394 ifps
->fp_save_state
.fp_tag
= user_fp_state
->fp_tag
;
395 ifps
->fp_save_state
.fp_eip
= user_fp_state
->fp_eip
;
396 ifps
->fp_save_state
.fp_cs
= user_fp_state
->fp_cs
;
397 ifps
->fp_save_state
.fp_opcode
= user_fp_state
->fp_opcode
;
398 ifps
->fp_save_state
.fp_dp
= user_fp_state
->fp_dp
;
399 ifps
->fp_save_state
.fp_ds
= user_fp_state
->fp_ds
;
400 ifps
->fp_regs
= *user_fp_regs
;
401 ifps
->fp_save_flavor
= FP_387
;
402 simple_unlock(&pcb
->lock
);
404 zfree(ifps_zone
, ifps
);
411 * Get the floating-point state for a thread.
412 * If the thread is not the current thread, it is
413 * not running (held). Locking needed against
414 * concurrent fpu_set_state or fpu_get_state.
419 register struct i386_float_state
*state
)
422 register struct i386_fpsave_state
*ifps
;
425 if (fp_kind
== FP_NO
)
428 assert(thr_act
!= THREAD_NULL
);
429 pcb
= thr_act
->machine
.pcb
;
431 simple_lock(&pcb
->lock
);
432 ifps
= pcb
->ims
.ifps
;
435 * No valid floating-point state.
437 simple_unlock(&pcb
->lock
);
438 bzero((char *)state
, sizeof(struct i386_float_state
));
442 /* Make sure we`ve got the latest fp state info */
443 /* If the live fpu state belongs to our target */
444 if (thr_act
== current_thread())
451 state
->fpkind
= fp_kind
;
452 state
->exc_status
= 0;
455 register struct i386_fp_save
*user_fp_state
;
456 register struct i386_fp_regs
*user_fp_regs
;
458 state
->initialized
= ifps
->fp_valid
;
460 user_fp_state
= (struct i386_fp_save
*) &state
->hw_state
[0];
461 user_fp_regs
= (struct i386_fp_regs
*)
462 &state
->hw_state
[sizeof(struct i386_fp_save
)];
465 * Ensure that reserved parts of the environment are 0.
467 bzero((char *)user_fp_state
, sizeof(struct i386_fp_save
));
469 user_fp_state
->fp_control
= ifps
->fp_save_state
.fp_control
;
470 user_fp_state
->fp_status
= ifps
->fp_save_state
.fp_status
;
471 user_fp_state
->fp_tag
= ifps
->fp_save_state
.fp_tag
;
472 user_fp_state
->fp_eip
= ifps
->fp_save_state
.fp_eip
;
473 user_fp_state
->fp_cs
= ifps
->fp_save_state
.fp_cs
;
474 user_fp_state
->fp_opcode
= ifps
->fp_save_state
.fp_opcode
;
475 user_fp_state
->fp_dp
= ifps
->fp_save_state
.fp_dp
;
476 user_fp_state
->fp_ds
= ifps
->fp_save_state
.fp_ds
;
477 *user_fp_regs
= ifps
->fp_regs
;
479 simple_unlock(&pcb
->lock
);
487 * Raise exceptions for:
492 * Use 53-bit precision.
497 unsigned short control
;
503 control
&= ~(FPC_PC
|FPC_RC
); /* Clear precision & rounding control */
504 control
|= (FPC_PC_53
| /* Set precision */
505 FPC_RC_RN
| /* round-to-nearest */
506 FPC_ZE
| /* Suppress zero-divide */
507 FPC_OE
| /* and overflow */
508 FPC_UE
| /* underflow */
509 FPC_IE
| /* Allow NaNQs and +-INF */
510 FPC_DE
| /* Allow denorms as operands */
511 FPC_PE
); /* No trap for precision loss */
516 * Coprocessor not present.
529 * Load this thread`s state into the FPU.
531 fp_load(current_thread());
535 * FPU overran end of segment.
536 * Re-initialize FPU. Floating point state is not valid.
542 register thread_t thr_act
= current_thread();
544 register struct i386_fpsave_state
*ifps
;
547 * This is a non-recoverable error.
548 * Invalidate the thread`s FPU state.
550 pcb
= thr_act
->machine
.pcb
;
551 simple_lock(&pcb
->lock
);
552 ifps
= pcb
->ims
.ifps
;
554 simple_unlock(&pcb
->lock
);
557 * Re-initialize the FPU.
563 * And disable access.
568 zfree(ifps_zone
, ifps
);
573 i386_exception(EXC_BAD_ACCESS
, VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
578 * FPU error. Called by AST.
584 register thread_t thr_act
= current_thread();
588 * Save the FPU state and turn off the FPU.
593 * Raise FPU exception.
594 * Locking not needed on pcb->ims.ifps,
595 * since thread is running.
597 i386_exception(EXC_ARITHMETIC
,
599 thr_act
->machine
.pcb
->ims
.ifps
->fp_save_state
.fp_status
);
606 * Locking not needed:
607 * . if called from fpu_get_state, pcb already locked.
608 * . if called from fpnoextflt or fp_intr, we are single-cpu
609 * . otherwise, thread is running.
615 register pcb_t pcb
= thr_act
->machine
.pcb
;
616 register struct i386_fpsave_state
*ifps
= pcb
->ims
.ifps
;
617 if (ifps
!= 0 && !ifps
->fp_valid
) {
618 /* registers are in FPU */
619 ifps
->fp_valid
= TRUE
;
620 ifps
->fp_save_flavor
= FP_387
;
622 fxsave(&ifps
->fx_save_state
); // save the SSE2/Fp state in addition is enabled
623 ifps
->fp_save_flavor
= FP_FXSR
;
625 fnsave(&ifps
->fp_save_state
); // also update the old save area for now...
630 * Restore FPU state from PCB.
632 * Locking not needed; always called on the current thread.
639 register pcb_t pcb
= thr_act
->machine
.pcb
;
640 register struct i386_fpsave_state
*ifps
;
643 ifps
= pcb
->ims
.ifps
;
645 ifps
= (struct i386_fpsave_state
*) zalloc(ifps_zone
);
646 assert(ALIGNED(ifps
,16));
647 bzero((char *)ifps
, sizeof *ifps
);
648 pcb
->ims
.ifps
= ifps
;
652 * I'm not sure this is needed. Does the fpu regenerate the interrupt in
653 * frstor or not? Without this code we may miss some exceptions, with it
654 * we might send too many exceptions.
656 } else if (ifps
->fp_valid
== 2) {
657 /* delayed exception pending */
659 ifps
->fp_valid
= TRUE
;
662 * Raise FPU exception.
663 * Locking not needed on pcb->ims.ifps,
664 * since thread is running.
666 i386_exception(EXC_ARITHMETIC
,
668 thr_act
->machine
.pcb
->ims
.ifps
->fp_save_state
.fp_status
);
672 if (ifps
->fp_save_flavor
== FP_FXSR
) fxrstor(&ifps
->fx_save_state
);
673 else frstor(ifps
->fp_save_state
);
675 ifps
->fp_valid
= FALSE
; /* in FPU */
680 * Allocate and initialize FP state for current thread.
683 * Locking not needed; always called on the current thread.
688 pcb_t pcb
= current_thread()->machine
.pcb
;
689 struct i386_fpsave_state
*ifps
;
691 ifps
= (struct i386_fpsave_state
*)zalloc(ifps_zone
);
692 assert(ALIGNED(ifps
,16));
693 bzero((char *)ifps
, sizeof *ifps
);
694 pcb
->ims
.ifps
= ifps
;
696 ifps
->fp_valid
= TRUE
;
697 ifps
->fp_save_state
.fp_control
= (0x037f
698 & ~(FPC_IM
|FPC_ZM
|FPC_OM
|FPC_PC
))
699 | (FPC_PC_53
|FPC_IC_AFF
);
700 ifps
->fp_save_state
.fp_status
= 0;
701 ifps
->fp_save_state
.fp_tag
= 0xffff; /* all empty */
702 ifps
->fx_save_state
.fx_control
= ifps
->fp_save_state
.fp_control
;
703 ifps
->fx_save_state
.fx_status
= ifps
->fp_save_state
.fp_status
;
704 ifps
->fx_save_state
.fx_tag
= 0x00;
705 ifps
->fx_save_state
.fx_MXCSR
= 0x1f80;
712 * Flush the current act's state, if needed
713 * (used by thread_terminate_self to ensure fp faults
714 * aren't satisfied by overly general trap code in the
715 * context of the reaper thread)
718 fpflush(__unused thread_t thr_act
)
720 /* not needed on MP x86s; fp not lazily evaluated */
725 * Handle a coprocessor error interrupt on the AT386.
726 * This comes in on line 5 of the slave PIC at SPL1.
733 thread_t thr_act
= current_thread();
737 * Turn off the extended 'busy' line.
742 * Save the FPU context to the thread using it.
750 * Since we are running on the interrupt stack, we must
751 * signal the thread to take the exception when we return
752 * to user mode. Use an AST to do this.
754 * Don`t set the thread`s AST field. If the thread is
755 * descheduled before it takes the AST, it will notice
756 * the FPU error when it reloads its FPU state.
759 mp_disable_preemption();
761 mp_enable_preemption();