2 * Copyright (c) 2000-2010 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) 1991,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.
60 #include <i386/eflags.h>
61 #include <i386/trap.h>
62 #include <i386/rtclock_asm.h>
63 #define _ARCH_I386_ASM_HELP_H_ /* Prevent inclusion of user header */
64 #include <mach/i386/syscall_sw.h>
65 #include <i386/postcode.h>
66 #include <i386/proc_reg.h>
67 #include <mach/exception_types.h>
70 * Low-memory handlers.
72 #define LO_ALLINTRS EXT(lo_allintrs32)
73 #define LO_ALLTRAPS EXT(lo_alltraps32)
74 #define LO_SYSENTER EXT(lo_sysenter32)
75 #define LO_UNIX_SCALL EXT(lo_unix_scall32)
76 #define LO_MACH_SCALL EXT(lo_mach_scall32)
77 #define LO_MDEP_SCALL EXT(lo_mdep_scall32)
79 #define HI_DATA(lo_addr) ( (EXT(lo_addr) - EXT(hi_remap_data)) + HIGH_IDT_BASE )
80 #define HI_TEXT(lo_text) ( (EXT(lo_text) - EXT(hi_remap_text)) + HIGH_MEM_BASE )
83 * Interrupt descriptor table and code vectors for it.
85 #define IDT_BASE_ENTRY(vec,seg,type) \
87 .long EXT(vec) - EXT(hi_remap_text) + HIGH_MEM_BASE ; \
93 #define IDT_BASE_ENTRY_INT(vec,seg,type) \
95 .long vec - EXT(hi_remap_text) + HIGH_MEM_BASE ; \
101 #define IDT_BASE_ENTRY_TG(vec,seg,type) \
109 #define IDT_ENTRY(vec,type) IDT_BASE_ENTRY(vec,KERNEL32_CS,type)
110 #define IDT_ENTRY_INT(vec,type) IDT_BASE_ENTRY_INT(vec,KERNEL32_CS,type)
113 * No error code. Clear error code and push trap number.
115 #define EXCEPTION(n,name) \
116 IDT_ENTRY(name,K_INTR_GATE);\
121 movl $(LO_ALLTRAPS),%ebx ;\
126 * Interrupt from user. Clear error code and push trap number.
128 #define EXCEP_USR(n,name) \
129 IDT_ENTRY(name,U_INTR_GATE);\
134 movl $(LO_ALLTRAPS),%ebx ;\
139 * Special interrupt code.
141 #define EXCEP_SPC(n,name) \
142 IDT_ENTRY(name,K_INTR_GATE)
145 * Special interrupt code from user.
147 #define EXCEP_SPC_USR(n,name) \
148 IDT_ENTRY(name,U_INTR_GATE)
152 * Extra-special interrupt code. Note that no offset may be
153 * specified in a task gate descriptor, so name is ignored.
156 /* Double-fault fatal handler */
157 #define DF_FATAL_TASK(n,name) \
158 IDT_BASE_ENTRY_TG(0,DF_TSS,K_TASK_GATE)
160 /* machine-check handler */
161 #define MC_FATAL_TASK(n,name) \
162 IDT_BASE_ENTRY_TG(0,MC_TSS,K_TASK_GATE)
165 * Error code has been pushed. Push trap number.
167 #define EXCEP_ERR(n,name) \
168 IDT_ENTRY(name,K_INTR_GATE) ;\
172 movl $(LO_ALLTRAPS),%ebx ;\
179 #define INTERRUPT(n) \
180 IDT_ENTRY_INT(L_ ## n,K_INTR_GATE) ;\
186 movl $(LO_ALLINTRS),%ebx ;\
198 EXCEPTION(0x00,t_zero_div)
199 EXCEP_SPC(0x01,hi_debug)
200 INTERRUPT(0x02) /* NMI */
201 EXCEP_USR(0x03,t_int3)
202 EXCEP_USR(0x04,t_into)
203 EXCEP_USR(0x05,t_bounds)
204 EXCEPTION(0x06,t_invop)
205 EXCEPTION(0x07,t_nofpu)
206 DF_FATAL_TASK(0x08,df_task_start)
207 EXCEPTION(0x09,a_fpu_over)
208 EXCEPTION(0x0a,a_inv_tss)
209 EXCEP_SPC(0x0b,hi_segnp)
210 EXCEP_ERR(0x0c,t_stack_fault)
211 EXCEP_SPC(0x0d,hi_gen_prot)
212 EXCEP_SPC(0x0e,hi_page_fault)
213 EXCEPTION(0x0f,t_trap_0f)
214 EXCEPTION(0x10,t_fpu_err)
215 EXCEPTION(0x11,t_trap_11)
216 MC_FATAL_TASK(0x12,mc_task_start)
217 EXCEPTION(0x13,t_sse_err)
218 EXCEPTION(0x14,t_trap_14)
219 EXCEPTION(0x15,t_trap_15)
220 EXCEPTION(0x16,t_trap_16)
221 EXCEPTION(0x17,t_trap_17)
222 EXCEPTION(0x18,t_trap_18)
223 EXCEPTION(0x19,t_trap_19)
224 EXCEPTION(0x1a,t_trap_1a)
225 EXCEPTION(0x1b,t_trap_1b)
226 EXCEPTION(0x1c,t_trap_1c)
227 EXCEPTION(0x1d,t_trap_1d)
228 EXCEPTION(0x1e,t_trap_1e)
229 EXCEPTION(0x1f,t_trap_1f)
331 EXCEP_USR(0x7f, t_dtrace_ret)
333 EXCEP_SPC_USR(0x80,hi_unix_scall)
334 EXCEP_SPC_USR(0x81,hi_mach_scall)
335 EXCEP_SPC_USR(0x82,hi_mdep_scall)
467 EXCEPTION(0xff,t_preempt)
479 * Trap/interrupt entry points.
481 * All traps must create the following save area on the PCB "stack":
490 * cr2 if page fault - otherwise unused
500 * user esp - if from user
501 * user ss - if from user
506 1: .long HI_TEXT(hi_ret_to_kernel)
510 1: .long HI_TEXT(hi_ret_to_user)
512 Entry(hi_ret_to_user)
514 movl %gs:CPU_ACTIVE_THREAD,%ecx
515 subl TH_PCB_ISS(%ecx),%ebx
516 movl $(WINDOWS_CLEAN),TH_COPYIO_STATE(%ecx)
518 movl TH_PCB_IDS(%ecx),%eax /* get debug state struct */
519 cmpl $0,%eax /* is there a debug state */
520 je 1f /* branch if not */
521 movl DS_DR0(%eax), %ecx /* Load the 32 bit debug registers */
523 movl DS_DR1(%eax), %ecx
525 movl DS_DR2(%eax), %ecx
527 movl DS_DR3(%eax), %ecx
529 movl DS_DR7(%eax), %eax
531 addl %gs:CPU_HI_ISS,%ebx /* rebase PCB save area to high addr */
532 movl %gs:CPU_TASK_CR3,%ecx
533 movl %ecx,%gs:CPU_ACTIVE_CR3
534 movl %ebx,%esp /* switch to hi based PCB stack */
535 movl %ecx,%cr3 /* switch to user's address space */
537 cmpl $0,%eax /* is dr7 set to something? */
538 je 2f /* branch if not */
539 movl %eax,%db7 /* Set dr7 */
542 Entry(hi_ret_to_kernel)
544 popl %eax /* ignore flavor of saved state */
546 popl %gs /* restore segment registers */
554 popa /* restore general registers */
555 addl $8,%esp /* discard trap number and error code */
557 cmpl $(SYSENTER_CS),4(%esp) /* test for fast entry/exit */
560 iret /* return from interrupt */
562 popl %edx /* user return eip */
563 popl %ecx /* pop and toss cs */
564 andl $(~EFL_IF),(%esp) /* clear intrs enabled, see sti below */
565 popf /* flags - carry denotes failure */
566 popl %ecx /* user return esp */
567 sti /* interrupts enabled after sysexit */
572 pushl %eax /* save system call number */
573 pushl $0 /* clear trap number slot */
574 pusha /* save the general registers */
575 movl $(LO_UNIX_SCALL),%ebx
580 pushl %eax /* save system call number */
581 pushl $0 /* clear trap number slot */
582 pusha /* save the general registers */
583 movl $(LO_MACH_SCALL),%ebx
588 pushl %eax /* save system call number */
589 pushl $0 /* clear trap number slot */
590 pusha /* save the general registers */
591 movl $(LO_MDEP_SCALL),%ebx
596 * sysenter entry point
597 * Requires user code to set up:
598 * edx: user instruction pointer (return address)
599 * ecx: user stack pointer
600 * on which is pushed stub ret addr and saved ebx
601 * Return to user-space is made using sysexit.
602 * Note: sysenter/sysexit cannot be used for calls returning a value in edx,
603 * or requiring ecx to be preserved.
606 movl (%esp), %esp /* switch from intr stack to pcb */
608 * Push values on to the PCB stack
609 * to cons up the saved state.
611 pushl $(USER_DS) /* ss */
612 pushl %ecx /* uesp */
615 * Clear, among others, the Nested Task (NT) flags bit;
616 * This is cleared by INT, but not by SYSENTER.
620 pushl $(SYSENTER_CS) /* cs */
623 pushl %eax /* err/eax - syscall code */
624 pushl $0 /* clear trap number slot */
625 pusha /* save the general registers */
626 orl $(EFL_IF),R32_EFLAGS-R32_EDI(%esp) /* (edi was last reg pushed) */
627 movl $(LO_SYSENTER),%ebx
633 pushl $(SS_32) /* 32-bit state flavor */
638 mov %eax,%es /* switch to kernel data seg */
639 mov $(CPU_DATA_GS),%eax
641 cld /* clear direction flag */
643 * Switch to kernel's address space if necessary
645 movl HI_DATA(lo_kernel_cr3),%ecx
650 movl %ecx,%gs:CPU_ACTIVE_CR3
652 testb $3,R32_CS(%esp)
654 movl %esp,%edx /* came from user mode */
656 subl %gs:CPU_HI_ISS,%edx
657 movl %gs:CPU_ACTIVE_THREAD,%ecx
658 addl TH_PCB_ISS(%ecx),%edx /* rebase the high stack to a low address */
660 cmpl $0, TH_PCB_IDS(%ecx) /* Is there a debug register state? */
662 movl $0, %ecx /* If so, reset DR7 (the control) */
665 movl R32_TRAPNO(%esp),%ecx // Get the interrupt vector
666 addl $1,%gs:hwIntCnt(,%ecx,4) // Bump the count
671 * Page fault traps save cr2.
674 pushl $(T_PAGE_FAULT) /* mark a page fault trap */
675 pusha /* save the general registers */
676 movl %cr2,%eax /* get the faulting address */
677 movl %eax,R32_CR2-R32_EDI(%esp)/* save in esp save slot */
679 movl $(LO_ALLTRAPS),%ebx
685 * Debug trap. Check for single-stepping across system call into
686 * kernel. If this is the case, taking the debug trap has turned
687 * off single-stepping - save the flags register with the trace
693 /* trap came from kernel mode */
694 cmpl $(HI_TEXT(hi_mach_scall)),(%esp)
696 addl $12,%esp /* remove eip/cs/eflags from debug_trap */
697 jmp EXT(hi_mach_scall) /* continue system call entry */
699 cmpl $(HI_TEXT(hi_mdep_scall)),(%esp)
701 addl $12,%esp /* remove eip/cs/eflags from debug_trap */
702 jmp EXT(hi_mdep_scall) /* continue system call entry */
704 cmpl $(HI_TEXT(hi_unix_scall)),(%esp)
706 addl $12,%esp /* remove eip/cs/eflags from debug_trap */
707 jmp EXT(hi_unix_scall) /* continue system call entry */
709 cmpl $(HI_TEXT(hi_sysenter)),(%esp)
712 * eip/cs/flags have been pushed on intr stack
713 * We have to switch to pcb stack and copy eflags.
714 * Note: setting the cs selector to SYSENTER_TF_CS
715 * will cause the return to user path to take the iret path so
716 * that eflags (containing the trap bit) is set atomically.
717 * In unix_syscall this is tested so that we'll rewind the pc
718 * to account for with sysenter or int entry.
720 addl $8,%esp /* remove eip/cs */
721 pushl %ecx /* save %ecx */
722 movl 8(%esp),%ecx /* top of intr stack -> pcb stack */
723 xchgl %ecx,%esp /* switch to pcb stack */
724 pushl $(USER_DS) /* ss */
725 pushl %ss:(%ecx) /* %ecx into uesp slot */
726 pushl %ss:4(%ecx) /* eflags */
727 movl %ss:(%ecx),%ecx /* restore %ecx */
728 pushl $(SYSENTER_TF_CS) /* cs - not SYSENTER_CS for iret path */
729 jmp hi_sysenter_2 /* continue sysenter entry */
732 pushl $(T_DEBUG) /* handle as user trap */
733 pusha /* save the general registers */
734 movl $(LO_ALLTRAPS),%ebx
740 * General protection or segment-not-present fault.
741 * Check for a GP/NP fault in the kernel_return
742 * sequence; if there, report it as a GP/NP fault on the user's instruction.
744 * esp-> 0: trap code (NP or GP)
745 * 4: segment number in error
749 * 20 old registers (trap is from kernel)
752 pushl $(T_GENERAL_PROTECTION) /* indicate fault type */
753 jmp trap_check_kernel_exit /* check for kernel exit sequence */
756 pushl $(T_SEGMENT_NOT_PRESENT)
757 /* indicate fault type */
758 trap_check_kernel_exit:
761 /* trap was from kernel mode, so */
762 /* check for the kernel exit sequence */
763 cmpl $(HI_TEXT(ret_iret)),8(%esp) /* on IRET? */
765 cmpl $(HI_TEXT(ret_popl_ds)),8(%esp) /* popping DS? */
767 cmpl $(HI_TEXT(ret_popl_es)),8(%esp) /* popping ES? */
769 cmpl $(HI_TEXT(ret_popl_fs)),8(%esp) /* popping FS? */
771 cmpl $(HI_TEXT(ret_popl_gs)),8(%esp) /* popping GS? */
774 pusha /* save the general registers */
775 movl $(LO_ALLTRAPS),%ebx
780 * GP/NP fault on IRET: CS or SS is in error.
781 * All registers contain the user's values.
796 movl %eax,8(%esp) /* save eax (we don`t need saved eip) */
797 popl %eax /* get trap number */
798 movl %eax,12-4(%esp) /* put in user trap number */
799 popl %eax /* get error code */
800 movl %eax,16-8(%esp) /* put in user errcode */
801 popl %eax /* restore eax */
802 /* now treat as fault from user */
803 pusha /* save the general registers */
804 movl $(LO_ALLTRAPS),%ebx
808 * Fault restoring a segment register. The user's registers are still
809 * saved on the stack. The offending segment register has not been
813 popl %eax /* get trap number */
814 popl %edx /* get error code */
815 addl $12,%esp /* pop stack to user regs */
816 jmp push_es /* (DS on top of stack) */
818 popl %eax /* get trap number */
819 popl %edx /* get error code */
820 addl $12,%esp /* pop stack to user regs */
821 jmp push_fs /* (ES on top of stack) */
823 popl %eax /* get trap number */
824 popl %edx /* get error code */
825 addl $12,%esp /* pop stack to user regs */
826 jmp push_gs /* (FS on top of stack) */
828 popl %eax /* get trap number */
829 popl %edx /* get error code */
830 addl $12,%esp /* pop stack to user regs */
831 jmp push_none /* (GS on top of stack) */
834 pushl %es /* restore es, */
836 pushl %fs /* restore fs, */
838 pushl %gs /* restore gs. */
840 pushl $(SS_32) /* 32-bit state flavor */
841 movl %eax,R32_TRAPNO(%esp) /* set trap number */
842 movl %edx,R32_ERR(%esp) /* set error code */
843 /* now treat as fault from user */
844 /* except that segment registers are */
846 movl $(LO_ALLTRAPS),%ebx
853 Entry(hi_remap_etext)
857 * All 32 bit task 'exceptions' enter lo_alltraps:
858 * esp -> x86_saved_state_t
860 * The rest of the state is set up as:
861 * cr3 -> kernel directory
862 * esp -> low based stack
865 * ss/ds/es -> KERNEL_DS
867 * interrupts disabled
868 * direction flag cleared
871 movl R32_CS(%esp),%eax /* assume 32-bit state */
872 cmpl $(SS_64),SS_FLAVOR(%esp)/* 64-bit? */
874 movl R64_CS(%esp),%eax /* 64-bit user mode */
881 movl %gs:CPU_ACTIVE_THREAD,%ecx
882 movl TH_TASK(%ecx),%ebx
884 /* Check for active vtimers in the current task */
885 TASK_VTIMER_CHECK(%ebx, %ecx)
887 movl %gs:CPU_KERNEL_STACK,%ebx
888 xchgl %ebx,%esp /* switch to kernel stack */
890 CCALL1(user_trap, %ebx) /* call user trap routine */
891 /* user_trap() unmasks interrupts */
892 cli /* hold off intrs - critical section */
893 xorl %ecx,%ecx /* don't check if we're in the PFZ */
896 * Return from trap or system call, checking for ASTs.
897 * On lowbase PCB stack with intrs disabled
899 Entry(return_from_trap32)
900 movl %gs:CPU_ACTIVE_THREAD, %esp
901 movl TH_PCB_ISS(%esp), %esp /* switch back to PCB stack */
902 movl %gs:CPU_PENDING_AST, %eax
904 je EXT(return_to_user) /* branch if no AST */
905 LEXT(return_from_trap_with_ast)
906 movl %gs:CPU_KERNEL_STACK, %ebx
907 xchgl %ebx, %esp /* switch to kernel stack */
909 testl %ecx, %ecx /* see if we need to check for an EIP in the PFZ */
910 je 2f /* no, go handle the AST */
911 cmpl $(SS_64), SS_FLAVOR(%ebx) /* are we a 64-bit task? */
913 /* no... 32-bit user mode */
914 movl R32_EIP(%ebx), %eax
915 pushl %ebx /* save PCB stack */
916 xorl %ebp, %ebp /* clear frame pointer */
917 CCALL1(commpage_is_in_pfz32, %eax)
918 popl %ebx /* retrieve pointer to PCB stack */
920 je 2f /* not in the PFZ... go service AST */
921 movl %eax, R32_EBX(%ebx) /* let the PFZ know we've pended an AST */
922 xchgl %ebx, %esp /* switch back to PCB stack */
923 jmp EXT(return_to_user)
924 1: /* 64-bit user mode */
925 movl R64_RIP(%ebx), %ecx
926 movl R64_RIP+4(%ebx), %eax
927 pushl %ebx /* save PCB stack */
928 xorl %ebp, %ebp /* clear frame pointer */
929 CCALL2(commpage_is_in_pfz64, %ecx, %eax)
930 popl %ebx /* retrieve pointer to PCB stack */
932 je 2f /* not in the PFZ... go service AST */
933 movl %eax, R64_RBX(%ebx) /* let the PFZ know we've pended an AST */
934 xchgl %ebx, %esp /* switch back to PCB stack */
935 jmp EXT(return_to_user)
937 sti /* interrupts always enabled on return to user mode */
938 xorl %ebp, %ebp /* Clear framepointer */
939 CCALL1(i386_astintr, $0) /* take the AST */
941 xorl %ecx, %ecx /* don't check if we're in the PFZ */
942 jmp EXT(return_from_trap32) /* and check again (rare) */
946 * Trap from kernel mode. No need to switch stacks.
947 * Interrupts must be off here - we will set them to state at time of trap
948 * as soon as it's safe for us to do so and not recurse doing preemption
951 movl %esp, %eax /* saved state addr */
952 pushl R32_EIP(%esp) /* Simulate a CALL from fault point */
953 pushl %ebp /* Extend framepointer chain */
955 CCALL1WITHSP(kernel_trap, %eax) /* Call kernel trap handler */
960 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
961 testl $ AST_URGENT,%eax /* any urgent preemption? */
962 je ret_to_kernel /* no, nothing to do */
963 cmpl $ T_PREEMPT,R32_TRAPNO(%esp)
964 je ret_to_kernel /* T_PREEMPT handled in kernel_trap() */
965 testl $ EFL_IF,R32_EFLAGS(%esp) /* interrupts disabled? */
967 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
969 movl %gs:CPU_KERNEL_STACK,%eax
972 and EXT(kernel_stack_mask),%ecx
973 testl %ecx,%ecx /* are we on the kernel stack? */
974 jne ret_to_kernel /* no, skip it */
976 CCALL1(i386_astintr, $1) /* take the AST */
982 * All interrupts on all tasks enter here with:
983 * esp-> -> x86_saved_state_t
985 * cr3 -> kernel directory
986 * esp -> low based stack
989 * ss/ds/es -> KERNEL_DS
991 * interrupts disabled
992 * direction flag cleared
996 * test whether already on interrupt stack
998 movl %gs:CPU_INT_STACK_TOP,%ecx
1001 leal -INTSTACK_SIZE(%ecx),%edx
1003 jb int_from_intstack
1005 xchgl %ecx,%esp /* switch to interrupt stack */
1007 movl %cr0,%eax /* get cr0 */
1008 orl $(CR0_TS),%eax /* or in TS bit */
1009 movl %eax,%cr0 /* set cr0 */
1011 subl $8, %esp /* for 16-byte stack alignment */
1012 pushl %ecx /* save pointer to old stack */
1013 movl %ecx,%gs:CPU_INT_STATE /* save intr state */
1015 TIME_INT_ENTRY /* do timing */
1017 movl %gs:CPU_ACTIVE_THREAD,%ecx
1018 movl TH_TASK(%ecx),%ebx
1020 /* Check for active vtimers in the current task */
1021 TASK_VTIMER_CHECK(%ebx, %ecx)
1023 incl %gs:CPU_PREEMPTION_LEVEL
1024 incl %gs:CPU_INTERRUPT_LEVEL
1026 movl %gs:CPU_INT_STATE, %eax
1027 CCALL1(interrupt, %eax) /* call generic interrupt routine */
1029 cli /* just in case we returned with intrs enabled */
1031 movl %eax,%gs:CPU_INT_STATE /* clear intr state pointer */
1033 decl %gs:CPU_INTERRUPT_LEVEL
1034 decl %gs:CPU_PREEMPTION_LEVEL
1036 TIME_INT_EXIT /* do timing */
1038 movl %gs:CPU_ACTIVE_THREAD,%eax
1039 movl TH_PCB_FPS(%eax),%eax /* get pcb's ifps */
1040 testl %eax, %eax /* Is there a context */
1041 je 1f /* Branch if not */
1042 cmpl $0, FP_VALID(%eax) /* Check fp_valid */
1043 jne 1f /* Branch if valid */
1047 movl %cr0,%eax /* get cr0 */
1048 orl $(CR0_TS),%eax /* or in TS bit */
1049 movl %eax,%cr0 /* set cr0 */
1051 popl %esp /* switch back to old stack */
1053 /* Load interrupted code segment into %eax */
1054 movl R32_CS(%esp),%eax /* assume 32-bit state */
1055 cmpl $(SS_64),SS_FLAVOR(%esp)/* 64-bit? */
1057 movl R64_CS(%esp),%eax /* 64-bit user mode */
1059 testb $3,%al /* user mode, */
1060 jnz ast_from_interrupt_user /* go handle potential ASTs */
1062 * we only want to handle preemption requests if
1063 * the interrupt fell in the kernel context
1064 * and preemption isn't disabled
1066 movl %gs:CPU_PENDING_AST,%eax
1067 testl $ AST_URGENT,%eax /* any urgent requests? */
1068 je ret_to_kernel /* no, nothing to do */
1070 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1071 jne ret_to_kernel /* yes, skip it */
1073 movl %gs:CPU_KERNEL_STACK,%eax
1076 and EXT(kernel_stack_mask),%ecx
1077 testl %ecx,%ecx /* are we on the kernel stack? */
1078 jne ret_to_kernel /* no, skip it */
1081 * Take an AST from kernel space. We don't need (and don't want)
1082 * to do as much as the case where the interrupt came from user
1085 CCALL1(i386_astintr, $1)
1091 * nested int - simple path, can't preempt etc on way out
1094 incl %gs:CPU_PREEMPTION_LEVEL
1095 incl %gs:CPU_INTERRUPT_LEVEL
1097 movl %esp, %edx /* x86_saved_state */
1098 CCALL1(interrupt, %edx)
1100 decl %gs:CPU_INTERRUPT_LEVEL
1101 decl %gs:CPU_PREEMPTION_LEVEL
1106 * Take an AST from an interrupted user
1108 ast_from_interrupt_user:
1109 movl %gs:CPU_PENDING_AST,%eax
1110 testl %eax,%eax /* pending ASTs? */
1111 je ret_to_user /* no, nothing to do */
1115 movl $1, %ecx /* check if we're in the PFZ */
1116 jmp EXT(return_from_trap_with_ast) /* return */
1121 * System call entries via INTR_GATE or sysenter:
1123 * esp -> x86_saved_state32_t
1124 * cr3 -> kernel directory
1125 * esp -> low based stack
1128 * ss/ds/es -> KERNEL_DS
1130 * interrupts disabled
1131 * direction flag cleared
1134 Entry(lo_sysenter32)
1136 * We can be here either for a mach syscall or a unix syscall,
1137 * as indicated by the sign of the code:
1139 movl R32_EAX(%esp),%eax
1141 js EXT(lo_mach_scall32) /* < 0 => mach */
1144 Entry(lo_unix_scall32)
1147 movl %gs:CPU_KERNEL_STACK,%edi
1148 xchgl %edi,%esp /* switch to kernel stack */
1149 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
1150 movl TH_TASK(%ecx),%ebx /* point to current task */
1151 incl TH_SYSCALLS_UNIX(%ecx) /* increment call count */
1153 /* Check for active vtimers in the current task */
1154 TASK_VTIMER_CHECK(%ebx, %ecx)
1158 CCALL1(unix_syscall, %edi)
1160 * always returns through thread_exception_return
1164 Entry(lo_mach_scall32)
1167 movl %gs:CPU_KERNEL_STACK,%edi
1168 xchgl %edi,%esp /* switch to kernel stack */
1169 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
1170 movl TH_TASK(%ecx),%ebx /* point to current task */
1171 incl TH_SYSCALLS_MACH(%ecx) /* increment call count */
1173 /* Check for active vtimers in the current task */
1174 TASK_VTIMER_CHECK(%ebx, %ecx)
1178 CCALL1(mach_call_munger, %edi)
1180 * always returns through thread_exception_return
1184 Entry(lo_mdep_scall32)
1187 movl %gs:CPU_KERNEL_STACK,%edi
1188 xchgl %edi,%esp /* switch to kernel stack */
1189 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
1190 movl TH_TASK(%ecx),%ebx /* point to current task */
1192 /* Check for active vtimers in the current task */
1193 TASK_VTIMER_CHECK(%ebx, %ecx)
1197 CCALL1(machdep_syscall, %edi)
1199 * always returns through thread_exception_return
1203 LEXT(return_to_user)
1209 * Double-fault exception handler task. The last gasp...
1211 Entry(df_task_start)
1212 CCALL1(panic_double_fault32, $(T_DOUBLE_FAULT))
1217 * machine-check handler task. The last gasp...
1219 Entry(mc_task_start)
1220 CCALL1(panic_machine_check32, $(T_MACHINE_CHECK))