2 * Copyright (c) 2000-2004 Apple Computer, 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.
58 #include <platforms.h>
60 #include <mach_kgdb.h>
62 #include <stat_time.h>
63 #include <mach_assert.h>
65 #include <sys/errno.h>
67 #include <i386/cpuid.h>
68 #include <i386/eflags.h>
69 #include <i386/proc_reg.h>
70 #include <i386/trap.h>
72 #include <mach/exception_types.h>
76 #define PREEMPT_DEBUG_LOG 0
80 * PTmap is recursive pagemap at top of virtual address space.
81 * Within PTmap, the page directory can be found (third indirection).
83 .globl _PTmap,_PTD,_PTDpde
84 .set _PTmap,(PTDPTDI << PDESHIFT)
85 .set _PTD,_PTmap + (PTDPTDI * NBPG)
86 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE)
89 * APTmap, APTD is the alternate recursive pagemap.
90 * It's used when modifying another process's page tables.
92 .globl _APTmap,_APTD,_APTDpde
93 .set _APTmap,(APTDPTDI << PDESHIFT)
94 .set _APTD,_APTmap + (APTDPTDI * NBPG)
95 .set _APTDpde,_PTD + (APTDPTDI * PDESIZE)
98 /* Under Mach-O, etext is a variable which contains
99 * the last text address
101 #define ETEXT_ADDR (EXT(etext))
103 /* Under ELF and other non-Mach-O formats, the address of
104 * etext represents the last text address
106 #define ETEXT_ADDR $ EXT(etext)
109 #define CX(addr,reg) addr(,reg,4)
119 #define RECOVERY_SECTION .section __VECTORS, __recover
120 #define RETRY_SECTION .section __VECTORS, __retries
122 #define RECOVERY_SECTION .text
123 #define RECOVERY_SECTION .text
126 #define RECOVER_TABLE_START \
128 .globl EXT(recover_table) ;\
129 LEXT(recover_table) ;\
132 #define RECOVER(addr) \
139 #define RECOVER_TABLE_END \
141 .globl EXT(recover_table_end) ;\
142 LEXT(recover_table_end) ;\
146 * Retry table for certain successful faults.
148 #define RETRY_TABLE_START \
150 .globl EXT(retry_table) ;\
154 #define RETRY(addr) \
161 #define RETRY_TABLE_END \
163 .globl EXT(retry_table_end) ;\
164 LEXT(retry_table_end) ;\
168 * Allocate recovery and retry tables.
182 movl %eax,TIMER_HIGHCHK(%ecx)
183 movl %edx,TIMER_LOW(%ecx)
184 movl %eax,TIMER_HIGH(%ecx)
189 0: movl TIMER_HIGH(%ecx),%edx
190 movl TIMER_LOW(%ecx),%eax
191 cmpl TIMER_HIGHCHK(%ecx),%edx
197 #define TIME_TRAP_UENTRY
198 #define TIME_TRAP_UEXIT
199 #define TIME_INT_ENTRY
200 #define TIME_INT_EXIT
208 * Low 32-bits of nanotime returned in %eax.
209 * Computed from tsc using conversion scale/shift from per-cpu data.
210 * Uses %ecx and %edx.
213 pushl %esi /* save %esi */ ;\
214 movl %gs:CPU_THIS,%esi /* per-cpu data ptr */ ;\
215 addl $(CPU_RTC_NANOTIME),%esi /* esi -> per-cpu nanotime*/ ;\
216 rdtsc /* edx:eax = tsc */ ;\
217 subl RTN_TSC(%esi),%eax /* eax = (tsc - base_tsc) */ ;\
218 mull RTN_SCALE(%esi) /* eax *= scale */ ;\
219 movl RTN_SHIFT(%esi),%ecx /* ecx = shift */ ;\
220 shrdl %cl,%edx,%eax /* edx:eax >> shift */ ;\
221 andb $32,%cl /* shift == 32? */ ;\
222 cmovnel %edx,%eax /* %eax = %edx if so */ ;\
223 addl RTN_NANOS(%esi),%eax /* add base ns */ ;\
227 * Add 32-bit ns delta in register dreg to timer pointed to by register treg.
229 #define TIMER_UPDATE(treg,dreg) \
230 addl TIMER_LOW(treg),dreg /* add delta low bits */ ;\
231 adcl $0,TIMER_HIGHCHK(treg) /* add carry check bits */ ;\
232 movl dreg,TIMER_LOW(treg) /* store updated low bit */ ;\
233 movl TIMER_HIGHCHK(treg),dreg /* copy high check bits */ ;\
234 movl dreg,TIMER_HIGH(treg) /* to high bita */
237 * Add time delta to old timer and start new.
239 #define TIMER_EVENT(old,new) \
240 pushl %eax /* must be invariant */ ;\
241 cli /* block interrupts */ ;\
242 NANOTIME32 /* eax low bits nanosecs */ ;\
243 movl %gs:CPU_PROCESSOR,%ecx /* get current processor */ ;\
244 movl CURRENT_TIMER(%ecx),%ecx /* get current timer */ ;\
245 movl %eax,%edx /* save timestamp in %edx */ ;\
246 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ;\
247 TIMER_UPDATE(%ecx,%eax) /* update timer struct */ ;\
248 addl $(new##_TIMER-old##_TIMER),%ecx /* point to new timer */ ;\
249 movl %edx,TIMER_TSTAMP(%ecx) /* set timestamp */ ;\
250 movl %gs:CPU_PROCESSOR,%edx /* get current processor */ ;\
251 movl %ecx,CURRENT_TIMER(%edx) /* set current timer */ ;\
252 sti /* interrupts on */ ;\
253 popl %eax /* must be invariant */
256 * Update time on user trap entry.
259 #define TIME_TRAP_UENTRY TIMER_EVENT(USER,SYSTEM)
262 * update time on user trap exit.
265 #define TIME_TRAP_UEXIT TIMER_EVENT(SYSTEM,USER)
268 * update time on interrupt entry.
269 * Uses %eax,%ecx,%edx.
271 #define TIME_INT_ENTRY \
272 NANOTIME32 /* eax low bits nanosecs */ ;\
273 movl %gs:CPU_PROCESSOR,%ecx /* get current processor */ ;\
274 movl CURRENT_TIMER(%ecx),%ecx /* get current timer */ ;\
275 movl %eax,%edx /* save timestamp in %edx */ ;\
276 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ;\
277 TIMER_UPDATE(%ecx,%eax) /* update timer struct */ ;\
278 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */ ;\
279 addl $(SYSTEM_TIMER),%ecx /* point to sys timer */ ;\
280 movl %edx,TIMER_TSTAMP(%ecx) /* set timestamp */
283 * update time on interrupt exit.
284 * Uses %eax, %ecx, %edx.
286 #define TIME_INT_EXIT \
287 NANOTIME32 /* eax low bits nanosecs */ ;\
288 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */ ;\
289 addl $(SYSTEM_TIMER),%ecx /* point to sys timer */ ;\
290 movl %eax,%edx /* save timestamp in %edx */ ;\
291 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ;\
292 TIMER_UPDATE(%ecx,%eax) /* update timer struct */ ;\
293 movl %gs:CPU_PROCESSOR,%ecx /* get current processor */ ;\
294 movl CURRENT_TIMER(%ecx),%ecx /* interrupted timer */ ;\
295 movl %edx,TIMER_TSTAMP(%ecx) /* set timestamp */
297 #endif /* STAT_TIME */
300 * Encapsulate the transfer of exception stack frames between a PCB
301 * and a thread stack. Since the whole point of these is to emulate
302 * a call or exception that changes privilege level, both macros
303 * assume that there is no user esp or ss stored in the source
304 * frame (because there was no change of privilege to generate them).
308 * Transfer a stack frame from a thread's user stack to its PCB.
309 * We assume the thread and stack addresses have been loaded into
310 * registers (our arguments).
312 * The macro overwrites edi, esi, ecx and whatever registers hold the
313 * thread and stack addresses (which can't be one of the above three).
314 * The thread address is overwritten with the address of its saved state
315 * (where the frame winds up).
317 * Must be called on kernel stack.
319 #define FRAME_STACK_TO_PCB(thread, stkp) ;\
320 movl ACT_PCB(thread),thread /* get act`s PCB */ ;\
321 leal PCB_ISS(thread),%edi /* point to PCB`s saved state */;\
322 movl %edi,thread /* save for later */ ;\
323 movl stkp,%esi /* point to start of frame */ ;\
324 movl $ R_UESP,%ecx ;\
325 sarl $2,%ecx /* word count for transfer */ ;\
326 cld /* we`re incrementing */ ;\
328 movsl /* transfer the frame */ ;\
329 addl $ R_UESP,stkp /* derive true "user" esp */ ;\
330 movl stkp,R_UESP(thread) /* store in PCB */ ;\
332 mov %ss,%cx /* get current ss */ ;\
333 movl %ecx,R_SS(thread) /* store in PCB */
336 * Transfer a stack frame from a thread's PCB to the stack pointed
337 * to by the PCB. We assume the thread address has been loaded into
338 * a register (our argument).
340 * The macro overwrites edi, esi, ecx and whatever register holds the
341 * thread address (which can't be one of the above three). The
342 * thread address is overwritten with the address of its saved state
343 * (where the frame winds up).
345 * Must be called on kernel stack.
347 #define FRAME_PCB_TO_STACK(thread) ;\
348 movl ACT_PCB(thread),%esi /* get act`s PCB */ ;\
349 leal PCB_ISS(%esi),%esi /* point to PCB`s saved state */;\
350 movl R_UESP(%esi),%edi /* point to end of dest frame */;\
351 movl ACT_MAP(thread),%ecx /* get act's map */ ;\
352 movl MAP_PMAP(%ecx),%ecx /* get map's pmap */ ;\
353 cmpl EXT(kernel_pmap), %ecx /* If kernel loaded task */ ;\
354 jz 1f /* use kernel data segment */ ;\
355 movl $ USER_DS,%ecx /* else use user data segment */;\
358 movl $ R_UESP,%ecx ;\
359 subl %ecx,%edi /* derive start of frame */ ;\
360 movl %edi,thread /* save for later */ ;\
361 sarl $2,%ecx /* word count for transfer */ ;\
362 cld /* we`re incrementing */ ;\
364 movsl /* transfer the frame */ ;\
365 mov %ss,%cx /* restore kernel segments */ ;\
373 * Traditional, not ANSI.
377 .globl label/**/count ;\
380 .globl label/**/limit ;\
384 addl $1,%ss:label/**/count ;\
385 cmpl $0,label/**/limit ;\
389 movl %ss:label/**/count,%eax ;\
390 cmpl %eax,%ss:label/**/limit ;\
403 * Last-ditch debug code to handle faults that might result
404 * from entering kernel (from collocated server) on an invalid
405 * stack. On collocated entry, there's no hardware-initiated
406 * stack switch, so a valid stack must be in place when an
407 * exception occurs, or we may double-fault.
409 * In case of a double-fault, our only recourse is to switch
410 * hardware "tasks", so that we avoid using the current stack.
412 * The idea here is just to get the processor into the debugger,
413 * post-haste. No attempt is made to fix up whatever error got
414 * us here, so presumably continuing from the debugger will
415 * simply land us here again -- at best.
419 * Note that the per-fault entry points are not currently
420 * functional. The only way to make them work would be to
421 * set up separate TSS's for each fault type, which doesn't
422 * currently seem worthwhile. (The offset part of a task
423 * gate is always ignored.) So all faults that task switch
424 * currently resume at db_task_start.
427 * Double fault (Murphy's point) - error code (0) on stack
429 Entry(db_task_dbl_fault)
431 movl $(T_DOUBLE_FAULT),%ebx
434 * Segment not present - error code on stack
436 Entry(db_task_seg_np)
438 movl $(T_SEGMENT_NOT_PRESENT),%ebx
441 * Stack fault - error code on (current) stack
443 Entry(db_task_stk_fault)
445 movl $(T_STACK_FAULT),%ebx
448 * General protection fault - error code on stack
450 Entry(db_task_gen_prot)
452 movl $(T_GENERAL_PROTECTION),%ebx
456 * The entry point where execution resumes after last-ditch debugger task
462 movl %edx,%esp /* allocate i386_saved_state on stack */
463 movl %eax,R_ERR(%esp)
464 movl %ebx,R_TRAPNO(%esp)
467 movl CX(EXT(mp_dbtss),%edx),%edx
468 movl TSS_LINK(%edx),%eax
469 pushl %eax /* pass along selector of previous TSS */
470 call EXT(db_tss_to_frame)
471 popl %eax /* get rid of TSS selector */
472 call EXT(db_trap_from_asm)
477 iret /* ha, ha, ha... */
478 #endif /* MACH_KDB */
481 * Trap/interrupt entry points.
483 * All traps must create the following save area on the PCB "stack":
492 * cr2 if page fault - otherwise unused
502 * user esp - if from user
503 * user ss - if from user
504 * es - if from V86 thread
505 * ds - if from V86 thread
506 * fs - if from V86 thread
507 * gs - if from V86 thread
512 * General protection or segment-not-present fault.
513 * Check for a GP/NP fault in the kernel_return
514 * sequence; if there, report it as a GP/NP fault on the user's instruction.
516 * esp-> 0: trap code (NP or GP)
517 * 4: segment number in error
521 * 20 old registers (trap is from kernel)
524 pushl $(T_GENERAL_PROTECTION) /* indicate fault type */
525 jmp trap_check_kernel_exit /* check for kernel exit sequence */
528 pushl $(T_SEGMENT_NOT_PRESENT)
529 /* indicate fault type */
531 trap_check_kernel_exit:
532 testl $(EFL_VM),16(%esp) /* is trap from V86 mode? */
533 jnz EXT(alltraps) /* isn`t kernel trap if so */
534 testl $3,12(%esp) /* is trap from kernel mode? */
535 jne EXT(alltraps) /* if so: */
536 /* check for the kernel exit sequence */
537 cmpl $ EXT(kret_iret),8(%esp) /* on IRET? */
539 cmpl $ EXT(kret_popl_ds),8(%esp) /* popping DS? */
541 cmpl $ EXT(kret_popl_es),8(%esp) /* popping ES? */
543 cmpl $ EXT(kret_popl_fs),8(%esp) /* popping FS? */
545 cmpl $ EXT(kret_popl_gs),8(%esp) /* popping GS? */
547 take_fault: /* if none of the above: */
548 jmp EXT(alltraps) /* treat as normal trap. */
551 * GP/NP fault on IRET: CS or SS is in error.
552 * All registers contain the user's values.
567 movl %eax,8(%esp) /* save eax (we don`t need saved eip) */
568 popl %eax /* get trap number */
569 movl %eax,12-4(%esp) /* put in user trap number */
570 popl %eax /* get error code */
571 movl %eax,16-8(%esp) /* put in user errcode */
572 popl %eax /* restore eax */
574 jmp EXT(alltraps) /* take fault */
577 * Fault restoring a segment register. The user's registers are still
578 * saved on the stack. The offending segment register has not been
582 popl %eax /* get trap number */
583 popl %edx /* get error code */
584 addl $12,%esp /* pop stack to user regs */
585 jmp push_es /* (DS on top of stack) */
587 popl %eax /* get trap number */
588 popl %edx /* get error code */
589 addl $12,%esp /* pop stack to user regs */
590 jmp push_fs /* (ES on top of stack) */
592 popl %eax /* get trap number */
593 popl %edx /* get error code */
594 addl $12,%esp /* pop stack to user regs */
595 jmp push_gs /* (FS on top of stack) */
597 popl %eax /* get trap number */
598 popl %edx /* get error code */
599 addl $12,%esp /* pop stack to user regs */
600 jmp push_segregs /* (GS on top of stack) */
603 pushl %es /* restore es, */
605 pushl %fs /* restore fs, */
607 pushl %gs /* restore gs. */
609 movl %eax,R_TRAPNO(%esp) /* set trap number */
610 movl %edx,R_ERR(%esp) /* set error code */
612 jmp trap_set_segs /* take trap */
615 * Debug trap. Check for single-stepping across system call into
616 * kernel. If this is the case, taking the debug trap has turned
617 * off single-stepping - save the flags register with the trace
621 testl $(EFL_VM),8(%esp) /* is trap from V86 mode? */
622 jnz 0f /* isn`t kernel trap if so */
623 testl $3,4(%esp) /* is trap from kernel mode? */
625 cmpl $syscall_entry,(%esp) /* system call entry? */
627 /* flags are sitting where syscall */
629 addl $8,%esp /* remove eip/cs */
630 jmp syscall_entry_2 /* continue system call entry */
632 1: cmpl $trap_unix_addr,(%esp)
637 0: pushl $0 /* otherwise: */
638 pushl $(T_DEBUG) /* handle as normal */
639 jmp EXT(alltraps) /* debug fault */
642 * Page fault traps save cr2.
645 pushl $(T_PAGE_FAULT) /* mark a page fault trap */
646 pusha /* save the general registers */
647 movl %cr2,%eax /* get the faulting address */
648 movl %eax,12(%esp) /* save in esp save slot */
649 jmp trap_push_segs /* continue fault */
652 * All 'exceptions' enter here with:
658 * old esp if trapped from user
659 * old ss if trapped from user
661 * NB: below use of CPU_NUMBER assumes that macro will use correct
662 * segment register for any kernel data accesses.
665 pusha /* save the general registers */
667 pushl %ds /* save the segment registers */
675 movl %eax,%es /* switch to kernel data seg */
676 cld /* clear direction flag */
677 testl $(EFL_VM),R_EFLAGS(%esp) /* in V86 mode? */
678 jnz trap_from_user /* user mode trap if so */
679 testb $3,R_CS(%esp) /* user mode trap? */
681 cmpl $0,%gs:CPU_ACTIVE_KLOADED
682 je trap_from_kernel /* if clear, truly in kernel */
684 cmpl ETEXT_ADDR,R_EIP(%esp) /* pc within kernel? */
689 * We didn't enter here "through" PCB (i.e., using ring 0 stack),
690 * so transfer the stack frame into the PCB explicitly, then
691 * start running on resulting "PCB stack". We have to set
692 * up a simulated "uesp" manually, since there's none in the
695 mov $ CPU_DATA_GS,%dx
698 movl %gs:CPU_ACTIVE_KLOADED,%ebx
699 movl %gs:CPU_KERNEL_STACK,%eax
701 FRAME_STACK_TO_PCB(%ebx,%eax)
706 mov $ CPU_DATA_GS,%ax
711 movl %gs:CPU_KERNEL_STACK,%ebx
712 xchgl %ebx,%esp /* switch to kernel stack */
713 /* user regs pointer already set */
715 pushl %ebx /* record register save area */
716 pushl %ebx /* pass register save area to trap */
717 call EXT(user_trap) /* call user trap routine */
718 movl 4(%esp),%esp /* switch back to PCB stack */
721 * Return from trap or system call, checking for ASTs.
725 LEXT(return_from_trap)
726 movl %gs:CPU_PENDING_AST,%edx
728 je EXT(return_to_user) /* if we need an AST: */
730 movl %gs:CPU_KERNEL_STACK,%esp
731 /* switch to kernel stack */
732 pushl $0 /* push preemption flag */
733 call EXT(i386_astintr) /* take the AST */
734 addl $4,%esp /* pop preemption flag */
735 popl %esp /* switch back to PCB stack (w/exc link) */
736 jmp EXT(return_from_trap) /* and check again (rare) */
737 /* ASTs after this point will */
741 * Arrange the checks needed for kernel-loaded (or kernel-loading)
742 * threads so that branch is taken in kernel-loaded case.
746 cmpl $0,%gs:CPU_ACTIVE_KLOADED
747 jnz EXT(return_xfer_stack)
748 movl %gs:CPU_ACTIVE_THREAD, %ebx /* get active thread */
752 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
753 je EXT(return_from_kernel)
755 #endif /* MACH_ASSERT */
759 * Return from kernel mode to interrupted thread.
762 LEXT(return_from_kernel)
764 popl %gs /* restore segment registers */
771 popa /* restore general registers */
772 addl $8,%esp /* discard trap number and error code */
775 iret /* return from interrupt */
778 LEXT(return_xfer_stack)
780 * If we're on PCB stack in a kernel-loaded task, we have
781 * to transfer saved state back to thread stack and swap
782 * stack pointers here, because the hardware's not going
786 movl %gs:CPU_KERNEL_STACK,%esp
787 movl %gs:CPU_ACTIVE_KLOADED,%eax
788 FRAME_PCB_TO_STACK(%eax)
791 jmp EXT(return_from_kernel)
794 * Hate to put this here, but setting up a separate swap_func for
795 * kernel-loaded threads no longer works, since thread executes
796 * "for a while" (i.e., until it reaches glue code) when first
797 * created, even if it's nominally suspended. Hence we can't
798 * transfer the PCB when the thread first resumes, because we
799 * haven't initialized it yet.
802 * Have to force transfer to new stack "manually". Use a string
803 * move to transfer all of our saved state to the stack pointed
804 * to by iss.uesp, then install a pointer to it as our current
807 LEXT(return_kernel_loading)
808 movl %gs:CPU_KERNEL_STACK,%esp
809 movl %gs:CPU_ACTIVE_THREAD, %ebx /* get active thread */
810 movl %ebx,%edx /* save for later */
811 FRAME_PCB_TO_STACK(%ebx)
812 movl %ebx,%esp /* start running on new stack */
813 movl $0,%gs:CPU_ACTIVE_KLOADED /* set cached indicator */
814 jmp EXT(return_from_kernel)
817 * Trap from kernel mode. No need to switch stacks or load segment registers.
820 #if MACH_KDB || MACH_KGDB
821 mov $ CPU_DATA_GS,%ax
823 movl %esp,%ebx /* save current stack */
825 cmpl EXT(int_stack_high),%esp /* on an interrupt stack? */
829 cmpl $0,EXT(kgdb_active) /* Unexpected trap in kgdb */
832 pushl %esp /* Already on kgdb stack */
836 jmp EXT(return_from_kernel)
837 0: /* should kgdb handle this exception? */
838 cmpl $(T_NO_FPU),R_TRAPNO(%esp) /* FPU disabled? */
840 cmpl $(T_PAGE_FAULT),R_TRAPNO(%esp) /* page fault? */
843 cli /* disable interrupts */
844 CPU_NUMBER(%edx) /* get CPU number */
845 movl CX(EXT(kgdb_stacks),%edx),%ebx
846 xchgl %ebx,%esp /* switch to kgdb stack */
847 pushl %ebx /* pass old sp as an arg */
848 call EXT(kgdb_from_kernel)
849 popl %esp /* switch back to kernel stack */
850 jmp EXT(return_from_kernel)
852 #endif /* MACH_KGDB */
855 cmpl $0,EXT(db_active) /* could trap be from ddb? */
857 CPU_NUMBER(%edx) /* see if this CPU is in ddb */
858 cmpl $0,CX(EXT(kdb_active),%edx)
861 call EXT(db_trap_from_asm)
863 jmp EXT(return_from_kernel)
867 * Dilemma: don't want to switch to kernel_stack if trap
868 * "belongs" to ddb; don't want to switch to db_stack if
869 * trap "belongs" to kernel. So have to duplicate here the
870 * set of trap types that kernel_trap() handles. Note that
871 * "unexpected" page faults will not be handled by kernel_trap().
872 * In this panic-worthy case, we fall into the debugger with
873 * kernel_stack containing the call chain that led to the
876 movl R_TRAPNO(%esp),%edx
877 cmpl $(T_PAGE_FAULT),%edx
879 cmpl $(T_NO_FPU),%edx
881 cmpl $(T_FPU_FAULT),%edx
883 cmpl $(T_FLOATING_POINT_ERROR),%edx
885 cmpl $(T_PREEMPT),%edx
888 #endif /* MACH_KDB */
890 cmpl %gs:CPU_KERNEL_STACK,%esp
891 /* if not already on kernel stack, */
892 ja 5f /* check some more */
893 cmpl %gs:CPU_ACTIVE_STACK,%esp
894 ja 6f /* on kernel stack: no switch */
896 movl %gs:CPU_KERNEL_STACK,%esp
898 pushl %ebx /* save old stack */
899 pushl %ebx /* pass as parameter */
900 call EXT(kernel_trap) /* to kernel trap routine */
901 addl $4,%esp /* pop parameter */
905 * If kernel_trap returns false, trap wasn't handled.
910 movl CX(EXT(db_stacks),%edx),%esp
911 pushl %ebx /* pass old stack as parameter */
912 call EXT(db_trap_from_asm)
913 #endif /* MACH_KDB */
915 cli /* disable interrupts */
916 CPU_NUMBER(%edx) /* get CPU number */
917 movl CX(EXT(kgdb_stacks),%edx),%esp
918 pushl %ebx /* pass old stack as parameter */
919 call EXT(kgdb_from_kernel)
920 #endif /* MACH_KGDB */
921 addl $4,%esp /* pop parameter */
925 * Likewise, if kdb_trap/kgdb_from_kernel returns false, trap
928 pushl %ebx /* pass old stack as parameter */
930 addl $4,%esp /* pop parameter */
932 movl %ebx,%esp /* get old stack (from callee-saves reg) */
933 #else /* MACH_KDB || MACH_KGDB */
934 pushl %esp /* pass parameter */
935 call EXT(kernel_trap) /* to kernel trap routine */
936 addl $4,%esp /* pop parameter */
937 #endif /* MACH_KDB || MACH_KGDB */
940 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
941 testl $ AST_URGENT,%eax /* any urgent preemption? */
942 je EXT(return_from_kernel) /* no, nothing to do */
943 cmpl $ T_PREEMPT,48(%esp) /* preempt request? */
944 jne EXT(return_from_kernel) /* no, nothing to do */
945 movl %gs:CPU_KERNEL_STACK,%eax
948 andl $(-KERNEL_STACK_SIZE),%ecx
949 testl %ecx,%ecx /* are we on the kernel stack? */
950 jne EXT(return_from_kernel) /* no, skip it */
952 #if PREEMPT_DEBUG_LOG
953 pushl 28(%esp) /* stack pointer */
954 pushl 24+4(%esp) /* frame pointer */
955 pushl 56+8(%esp) /* stack pointer */
957 call EXT(log_thread_action)
960 0: String "trap preempt eip"
962 #endif /* PREEMPT_DEBUG_LOG */
964 pushl $1 /* push preemption flag */
965 call EXT(i386_astintr) /* take the AST */
966 addl $4,%esp /* pop preemption flag */
969 jmp EXT(return_from_kernel)
972 * Called as a function, makes the current thread
973 * return from the kernel as if from an exception.
976 .globl EXT(thread_exception_return)
977 .globl EXT(thread_bootstrap_return)
978 LEXT(thread_exception_return)
979 LEXT(thread_bootstrap_return)
980 movl %esp,%ecx /* get kernel stack */
981 or $(KERNEL_STACK_SIZE-1),%ecx
982 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
983 jmp EXT(return_from_trap)
985 Entry(call_continuation)
986 movl S_ARG0,%eax /* get continuation */
987 movl S_ARG1,%edx /* continuation param */
988 movl S_ARG2,%ecx /* wait result */
989 movl %esp,%ebp /* get kernel stack */
990 or $(KERNEL_STACK_SIZE-1),%ebp
991 addl $(-3-IKS_SIZE),%ebp
992 movl %ebp,%esp /* pop the stack */
993 xorl %ebp,%ebp /* zero frame pointer */
996 call *%eax /* call continuation */
998 movl %gs:CPU_ACTIVE_THREAD,%eax
1000 call EXT(thread_terminate)
1003 #define LOG_INTERRUPT(info,msg) \
1007 call EXT(log_thread_action) ; \
1010 #define CHECK_INTERRUPT_TIME(n) \
1013 call EXT(check_thread_time) ; \
1017 #define LOG_INTERRUPT(info,msg)
1018 #define CHECK_INTERRUPT_TIME(n)
1023 String "interrupt start"
1025 String "interrupt end"
1029 * All interrupts enter here.
1030 * old %eax on stack; interrupt number in %eax.
1033 pushl %ecx /* save registers */
1035 cld /* clear direction flag */
1037 pushl %ds /* save segment registers */
1041 mov %ss,%dx /* switch to kernel segments */
1044 mov $ CPU_DATA_GS,%dx
1048 * test whether already on interrupt stack
1050 movl %gs:CPU_INT_STACK_TOP,%ecx
1053 leal -INTSTACK_SIZE(%ecx),%edx
1055 jb int_from_intstack
1057 movl %esp,%edx /* & i386_interrupt_state */
1058 xchgl %ecx,%esp /* switch to interrupt stack */
1060 pushl %ecx /* save pointer to old stack */
1061 pushl %edx /* pass &i386_interrupt_state to pe_incoming_interrupt */
1062 pushl %eax /* push trap number */
1064 TIME_INT_ENTRY /* do timing */
1067 incl %gs:CPU_PREEMPTION_LEVEL
1068 #endif /* MACH_RT */
1069 incl %gs:CPU_INTERRUPT_LEVEL
1071 call EXT(PE_incoming_interrupt) /* call generic interrupt routine */
1072 addl $8,%esp /* Pop trap number and eip */
1074 .globl EXT(return_to_iret)
1075 LEXT(return_to_iret) /* (label for kdb_kintr and hardclock) */
1077 decl %gs:CPU_INTERRUPT_LEVEL
1080 decl %gs:CPU_PREEMPTION_LEVEL
1081 #endif /* MACH_RT */
1083 TIME_INT_EXIT /* do timing */
1085 popl %esp /* switch back to old stack */
1087 movl %gs:CPU_PENDING_AST,%eax
1088 testl %eax,%eax /* any pending asts? */
1089 je 1f /* no, nothing to do */
1090 testl $(EFL_VM),I_EFL(%esp) /* if in V86 */
1091 jnz ast_from_interrupt /* take it */
1092 testb $3,I_CS(%esp) /* user mode, */
1093 jnz ast_from_interrupt /* take it */
1095 cmpl ETEXT_ADDR,I_EIP(%esp) /* if within kernel-loaded task, */
1096 jnb ast_from_interrupt /* take it */
1100 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption masked? */
1101 jne 1f /* yes, skip it */
1102 testl $ AST_URGENT,%eax /* any urgent requests? */
1103 je 1f /* no, skip it */
1104 cmpl $ EXT(locore_end),I_EIP(%esp) /* are we in locore code? */
1105 jb 1f /* yes, skip it */
1106 movl %gs:CPU_KERNEL_STACK,%eax
1109 andl $(-KERNEL_STACK_SIZE),%ecx
1110 testl %ecx,%ecx /* are we on the kernel stack? */
1111 jne 1f /* no, skip it */
1114 * Take an AST from kernel space. We don't need (and don't want)
1115 * to do as much as the case where the interrupt came from user
1118 #if PREEMPT_DEBUG_LOG
1123 call EXT(log_thread_action)
1126 0: String "intr preempt eip"
1128 #endif /* PREEMPT_DEBUG_LOG */
1131 pushl $1 /* push preemption flag */
1132 call EXT(i386_astintr) /* take the AST */
1133 addl $4,%esp /* pop preemption flag */
1134 #endif /* MACH_RT */
1139 pop %es /* restore segment regs */
1144 iret /* return to caller */
1148 incl %gs:CPU_PREEMPTION_LEVEL
1149 #endif /* MACH_RT */
1151 incl %gs:CPU_INTERRUPT_LEVEL
1153 movl %esp, %edx /* i386_interrupt_state */
1154 pushl %edx /* pass &i386_interrupt_state to PE_incoming_interrupt /*
1156 pushl %eax /* Push trap number */
1158 call EXT(PE_incoming_interrupt)
1159 addl $20,%esp /* pop i386_interrupt_state, gs,fs,es,ds */
1161 LEXT(return_to_iret_i) /* ( label for kdb_kintr) */
1163 addl $4,%esp /* pop trap number */
1165 decl %gs:CPU_INTERRUPT_LEVEL
1168 decl %gs:CPU_PREEMPTION_LEVEL
1169 #endif /* MACH_RT */
1171 pop %edx /* must have been on kernel segs */
1173 pop %eax /* no ASTs */
1177 * Take an AST from an interrupt.
1193 pop %es /* restore all registers ... */
1198 sti /* Reenable interrupts */
1199 pushl $0 /* zero code */
1200 pushl $0 /* zero trap number */
1201 pusha /* save general registers */
1202 push %ds /* save segment registers */
1206 mov %ss,%dx /* switch to kernel segments */
1209 mov $ CPU_DATA_GS,%dx
1213 * See if we interrupted a kernel-loaded thread executing
1217 testl $(EFL_VM),R_EFLAGS(%esp) /* in V86 mode? */
1218 jnz 0f /* user mode trap if so */
1220 jnz 0f /* user mode, back to normal */
1222 cmpl ETEXT_ADDR,R_EIP(%esp)
1223 jb 0f /* not kernel-loaded, back to normal */
1227 * Transfer the current stack frame by hand into the PCB.
1230 movl %gs:CPU_ACTIVE_KLOADED,%eax
1231 movl %gs:CPU_KERNEL_STACK,%ebx
1233 FRAME_STACK_TO_PCB(%eax,%ebx)
1240 movl %gs:CPU_KERNEL_STACK,%eax
1241 /* switch to kernel stack */
1245 pushl $0 /* push preemption flag */
1246 call EXT(i386_astintr) /* take the AST */
1247 addl $4,%esp /* pop preemption flag */
1248 popl %esp /* back to PCB stack */
1249 jmp EXT(return_from_trap) /* return */
1251 #if MACH_KDB || MACH_KGDB
1253 * kdb_kintr: enter kdb from keyboard interrupt.
1254 * Chase down the stack frames until we find one whose return
1255 * address is the interrupt handler. At that point, we have:
1257 * frame-> saved %ebp
1258 * return address in interrupt handler
1261 * return address == return_to_iret_i
1270 * frame-> saved %ebp
1271 * return address in interrupt handler
1274 * return address == return_to_iret
1275 * pointer to save area on old stack
1276 * [ saved %ebx, if accurate timing ]
1278 * old stack: saved %es
1287 * Call kdb, passing it that register save area.
1292 #endif /* MACH_KGDB */
1295 #endif /* MACH_KDB */
1296 movl %ebp,%eax /* save caller`s frame pointer */
1297 movl $ EXT(return_to_iret),%ecx /* interrupt return address 1 */
1298 movl $ EXT(return_to_iret_i),%edx /* interrupt return address 2 */
1300 0: cmpl 16(%eax),%ecx /* does this frame return to */
1301 /* interrupt handler (1)? */
1303 cmpl $kdb_from_iret,16(%eax)
1305 cmpl 16(%eax),%edx /* interrupt handler (2)? */
1307 cmpl $kdb_from_iret_i,16(%eax)
1309 movl (%eax),%eax /* try next frame */
1312 1: movl $kdb_from_iret,16(%eax) /* returns to kernel/user stack */
1315 2: movl $kdb_from_iret_i,16(%eax)
1316 /* returns to interrupt stack */
1320 * On return from keyboard interrupt, we will execute
1322 * if returning to an interrupt on the interrupt stack
1324 * if returning to an interrupt on the user or kernel stack
1327 /* save regs in known locations */
1328 pushl %ebx /* caller`s %ebx is in reg */
1336 pushl %esp /* pass regs */
1337 call EXT(kgdb_kentry) /* to kgdb */
1338 addl $4,%esp /* pop parameters */
1339 #endif /* MACH_KGDB */
1341 pushl %esp /* pass regs */
1342 call EXT(kdb_kentry) /* to kdb */
1343 addl $4,%esp /* pop parameters */
1344 #endif /* MACH_KDB */
1345 pop %gs /* restore registers */
1351 jmp EXT(return_to_iret) /* normal interrupt return */
1353 kdb_from_iret_i: /* on interrupt stack */
1354 pop %edx /* restore saved registers */
1357 pushl $0 /* zero error code */
1358 pushl $0 /* zero trap number */
1359 pusha /* save general registers */
1360 push %ds /* save segment registers */
1365 cli /* disable interrupts */
1366 CPU_NUMBER(%edx) /* get CPU number */
1367 movl CX(EXT(kgdb_stacks),%edx),%ebx
1368 xchgl %ebx,%esp /* switch to kgdb stack */
1369 pushl %ebx /* pass old sp as an arg */
1370 call EXT(kgdb_from_kernel)
1371 popl %esp /* switch back to interrupt stack */
1372 #endif /* MACH_KGDB */
1374 pushl %esp /* pass regs, */
1375 pushl $0 /* code, */
1376 pushl $-1 /* type to kdb */
1379 #endif /* MACH_KDB */
1380 pop %gs /* restore segment registers */
1384 popa /* restore general registers */
1388 #endif /* MACH_KDB || MACH_KGDB */
1392 * Mach RPC enters through a call gate, like a system call.
1396 pushf /* save flags as soon as possible */
1397 pushl %eax /* save system call number */
1398 pushl $0 /* clear trap number slot */
1400 pusha /* save the general registers */
1401 pushl %ds /* and the segment registers */
1406 mov %ss,%dx /* switch to kernel data segment */
1409 mov $ CPU_DATA_GS,%dx
1413 * Shuffle eflags,eip,cs into proper places
1416 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
1417 movl R_CS(%esp),%ecx /* eip is in CS slot */
1418 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
1419 movl %ecx,R_EIP(%esp) /* fix eip */
1420 movl %edx,R_CS(%esp) /* fix cs */
1421 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
1425 negl %eax /* get system call number */
1426 shll $4,%eax /* manual indexing */
1429 * Check here for mach_rpc from kernel-loaded task --
1430 * - Note that kernel-loaded task returns via real return.
1431 * We didn't enter here "through" PCB (i.e., using ring 0 stack),
1432 * so transfer the stack frame into the PCB explicitly, then
1433 * start running on resulting "PCB stack". We have to set
1434 * up a simulated "uesp" manually, since there's none in the
1437 cmpl $0,%gs:CPU_ACTIVE_KLOADED
1440 movl %gs:CPU_ACTIVE_KLOADED,%ebx
1441 movl %gs:CPU_KERNEL_STACK,%edx
1444 FRAME_STACK_TO_PCB(%ebx,%edx)
1450 movl %gs:CPU_KERNEL_STACK,%ebx
1451 /* get current kernel stack */
1452 xchgl %ebx,%esp /* switch stacks - %ebx points to */
1453 /* user registers. */
1458 * Register use on entry:
1459 * eax contains syscall number
1460 * ebx contains user regs pointer
1462 #undef RPC_TRAP_REGISTERS
1463 #ifdef RPC_TRAP_REGISTERS
1469 movl EXT(mach_trap_table)(%eax),%ecx
1470 /* get number of arguments */
1471 jecxz 2f /* skip argument copy if none */
1472 movl R_UESP(%ebx),%esi /* get user stack pointer */
1473 lea 4(%esi,%ecx,4),%esi /* skip user return address, */
1474 /* and point past last argument */
1475 movl %gs:CPU_ACTIVE_KLOADED,%edx
1476 /* point to current thread */
1477 orl %edx,%edx /* if ! kernel-loaded, check addr */
1479 mov %ds,%dx /* kernel data segment access */
1482 cmpl $(VM_MAX_ADDRESS),%esi /* in user space? */
1483 ja mach_call_addr /* address error if not */
1484 movl $ USER_DS,%edx /* user data segment access */
1487 movl %esp,%edx /* save kernel ESP for error recovery */
1491 RECOVER(mach_call_addr_push)
1492 pushl %fs:(%esi) /* push argument on stack */
1493 loop 1b /* loop for all arguments */
1497 * Register use on entry:
1498 * eax contains syscall number << 4
1499 * mach_call_munger is declared regparm(1), so the first arg is %eax
1503 call EXT(mach_call_munger)
1505 movl %esp,%ecx /* get kernel stack */
1506 or $(KERNEL_STACK_SIZE-1),%ecx
1507 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
1508 movl %eax,R_EAX(%esp) /* save return value */
1509 jmp EXT(return_from_trap) /* return to user */
1513 * Special system call entry for "int 0x80", which has the "eflags"
1514 * register saved at the right place already.
1515 * Fall back to the common syscall path after saving the registers.
1520 * old esp if trapped from user
1521 * old ss if trapped from user
1523 * XXX: for the moment, we don't check for int 0x80 from kernel mode.
1525 Entry(syscall_int80)
1526 pushl %eax /* save system call number */
1527 pushl $0 /* clear trap number slot */
1529 pusha /* save the general registers */
1530 pushl %ds /* and the segment registers */
1535 mov %ss,%dx /* switch to kernel data segment */
1538 mov $ CPU_DATA_GS,%dx
1544 * System call enters through a call gate. Flags are not saved -
1545 * we must shuffle stack to look like trap save area.
1552 * eax contains system call number.
1554 * NB: below use of CPU_NUMBER assumes that macro will use correct
1555 * correct segment register for any kernel data accesses.
1559 pushf /* save flags as soon as possible */
1561 pushl %eax /* save system call number */
1562 pushl $0 /* clear trap number slot */
1564 pusha /* save the general registers */
1565 pushl %ds /* and the segment registers */
1570 mov %ss,%dx /* switch to kernel data segment */
1573 mov $ CPU_DATA_GS,%dx
1577 * Shuffle eflags,eip,cs into proper places
1580 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
1581 movl R_CS(%esp),%ecx /* eip is in CS slot */
1582 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
1583 movl %ecx,R_EIP(%esp) /* fix eip */
1584 movl %edx,R_CS(%esp) /* fix cs */
1585 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
1589 * Check here for syscall from kernel-loaded task --
1590 * We didn't enter here "through" PCB (i.e., using ring 0 stack),
1591 * so transfer the stack frame into the PCB explicitly, then
1592 * start running on resulting "PCB stack". We have to set
1593 * up a simulated "uesp" manually, since there's none in the
1596 cmpl $0,%gs:CPU_ACTIVE_KLOADED
1599 movl %gs:CPU_ACTIVE_KLOADED,%ebx
1600 movl %gs:CPU_KERNEL_STACK,%edx
1602 FRAME_STACK_TO_PCB(%ebx,%edx)
1610 movl %gs:CPU_KERNEL_STACK,%ebx
1611 /* get current kernel stack */
1612 xchgl %ebx,%esp /* switch stacks - %ebx points to */
1613 /* user registers. */
1614 /* user regs pointer already set */
1617 * Native system call.
1618 * Register use on entry:
1619 * eax contains syscall number
1620 * ebx points to user regs
1623 negl %eax /* get system call number */
1624 jl mach_call_range /* out of range if it was positive */
1626 cmpl EXT(mach_trap_count),%eax /* check system call table bounds */
1627 jg mach_call_range /* error if out of range */
1628 shll $4,%eax /* manual indexing */
1630 movl EXT(mach_trap_table)+4(%eax),%edx
1632 cmpl $ EXT(kern_invalid),%edx /* if not "kern_invalid" */
1633 jne do_native_call /* go on with Mach syscall */
1634 shrl $4,%eax /* restore syscall number */
1635 jmp mach_call_range /* try it as a "server" syscall */
1638 * Register use on entry:
1639 * eax contains syscall number
1640 * ebx contains user regs pointer
1643 movl EXT(mach_trap_table)(%eax),%ecx
1644 /* get number of arguments */
1645 jecxz mach_call_call /* skip argument copy if none */
1646 movl R_UESP(%ebx),%esi /* get user stack pointer */
1647 lea 4(%esi,%ecx,4),%esi /* skip user return address, */
1648 /* and point past last argument */
1649 movl %gs:CPU_ACTIVE_KLOADED,%edx
1650 /* point to current thread */
1651 orl %edx,%edx /* if kernel-loaded, skip addr check */
1653 mov %ds,%dx /* kernel data segment access */
1656 cmpl $(VM_MAX_ADDRESS),%esi /* in user space? */
1657 ja mach_call_addr /* address error if not */
1658 movl $ USER_DS,%edx /* user data segment access */
1661 movl %esp,%edx /* save kernel ESP for error recovery */
1665 RECOVER(mach_call_addr_push)
1666 pushl %fs:(%esi) /* push argument on stack */
1667 loop 2b /* loop for all arguments */
1670 * Register use on entry:
1671 * eax contains syscall number
1672 * ebx contains user regs pointer
1678 #if ETAP_EVENT_MONITOR
1679 cmpl $0x200, %eax /* is this mach_msg? */
1680 jz make_syscall /* if yes, don't record event */
1682 pushal /* Otherwise: save registers */
1683 pushl %eax /* push syscall number on stack*/
1684 call EXT(etap_machcall_probe1) /* call event begin probe */
1685 add $4,%esp /* restore stack */
1686 popal /* restore registers */
1688 call *EXT(mach_trap_table)+4(%eax) /* call procedure */
1690 call EXT(etap_machcall_probe2) /* call event end probe */
1692 jmp skip_syscall /* syscall already made */
1693 #endif /* ETAP_EVENT_MONITOR */
1698 * mach_call_munger is declared regparm(1) so the first arg is %eax
1700 call EXT(mach_call_munger)
1704 movl %esp,%ecx /* get kernel stack */
1705 or $(KERNEL_STACK_SIZE-1),%ecx
1706 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
1707 movl %eax,R_EAX(%esp) /* save return value */
1708 jmp EXT(return_from_trap) /* return to user */
1711 * Address out of range. Change to page fault.
1712 * %esi holds failing address.
1713 * Register use on entry:
1714 * ebx contains user regs pointer
1716 mach_call_addr_push:
1717 movl %edx,%esp /* clean parameters from stack */
1719 movl %esi,R_CR2(%ebx) /* set fault address */
1720 movl $(T_PAGE_FAULT),R_TRAPNO(%ebx)
1721 /* set page-fault trap */
1722 movl $(T_PF_USER),R_ERR(%ebx)
1723 /* set error code - read user space */
1725 jmp EXT(take_trap) /* treat as a trap */
1728 * System call out of range. Treat as invalid-instruction trap.
1729 * (? general protection?)
1730 * Register use on entry:
1731 * eax contains syscall number
1736 push $1 /* code_cnt = 1 */
1737 push %edx /* exception_type_t (see i/f docky) */
1740 call EXT(exception_triage)
1743 .globl EXT(syscall_failed)
1744 LEXT(syscall_failed)
1745 movl %esp,%ecx /* get kernel stack */
1746 or $(KERNEL_STACK_SIZE-1),%ecx
1747 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
1748 movl %gs:CPU_KERNEL_STACK,%ebx
1749 /* get current kernel stack */
1750 xchgl %ebx,%esp /* switch stacks - %ebx points to */
1751 /* user registers. */
1752 /* user regs pointer already set */
1754 movl $(T_INVALID_OPCODE),R_TRAPNO(%ebx)
1755 /* set invalid-operation trap */
1756 movl $0,R_ERR(%ebx) /* clear error code */
1758 jmp EXT(take_trap) /* treat as a trap */
1767 * Copy from user address space.
1768 * arg0: user address
1769 * arg1: kernel address
1775 pushl %edi /* save registers */
1777 movl 8+S_ARG0,%esi /* get user start address */
1778 movl 8+S_ARG1,%edi /* get kernel destination address */
1779 movl 8+S_ARG2,%edx /* get count */
1781 lea 0(%esi,%edx),%eax /* get user end address + 1 */
1783 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get active thread */
1784 movl ACT_MAP(%ecx),%ecx /* get act->map */
1785 movl MAP_PMAP(%ecx),%ecx /* get map->pmap */
1786 cmpl EXT(kernel_pmap), %ecx
1788 movl $ USER_DS,%ecx /* user data segment access */
1792 jb copyin_fail /* fail if wrap-around */
1794 movl %edx,%ecx /* move by longwords first */
1797 RECOVER(copyin_fail)
1799 movsl /* move longwords */
1800 movl %edx,%ecx /* now move remaining bytes */
1803 RECOVER(copyin_fail)
1806 xorl %eax,%eax /* return 0 for success */
1808 mov %ss,%di /* restore kernel data segment */
1811 popl %edi /* restore registers */
1813 ret /* and return */
1816 movl $ EFAULT,%eax /* return error for failure */
1817 jmp copy_ret /* pop frame and return */
1820 * Copy string from user address space.
1821 * arg0: user address
1822 * arg1: kernel address
1823 * arg2: max byte count
1824 * arg3: actual byte count (OUT)
1828 pushl %edi /* save registers */
1830 movl 8+S_ARG0,%esi /* get user start address */
1831 movl 8+S_ARG1,%edi /* get kernel destination address */
1832 movl 8+S_ARG2,%edx /* get count */
1834 lea 0(%esi,%edx),%eax /* get user end address + 1 */
1836 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get active thread */
1837 movl ACT_MAP(%ecx),%ecx /* get act->map */
1838 movl MAP_PMAP(%ecx),%ecx /* get map->pmap */
1839 cmpl EXT(kernel_pmap), %ecx
1841 mov %ds,%cx /* kernel data segment access */
1844 movl $ USER_DS,%ecx /* user data segment access */
1852 RECOVER(copystr_fail) /* copy bytes... */
1855 testl %edi,%edi /* if kernel address is ... */
1856 jz 3f /* not NULL */
1857 movb %al,(%edi) /* copy the byte */
1861 je 5f /* Zero count.. error out */
1863 jne 2b /* .. a NUL found? */
1864 jmp 4f /* return zero (%eax) */
1866 movl $ ENAMETOOLONG,%eax /* String is too long.. */
1868 movl 8+S_ARG3,%edi /* get OUT len ptr */
1870 jz copystr_ret /* if null, just return */
1872 movl %esi,(%edi) /* else set OUT arg to xfer len */
1874 popl %edi /* restore registers */
1876 ret /* and return */
1879 movl $ EFAULT,%eax /* return error for failure */
1880 jmp copy_ret /* pop frame and return */
1883 * Copy to user address space.
1884 * arg0: kernel address
1885 * arg1: user address
1891 pushl %edi /* save registers */
1894 movl 12+S_ARG0,%esi /* get kernel start address */
1895 movl 12+S_ARG1,%edi /* get user start address */
1896 movl 12+S_ARG2,%edx /* get count */
1898 leal 0(%edi,%edx),%eax /* get user end address + 1 */
1900 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get active thread */
1901 movl ACT_MAP(%ecx),%ecx /* get act->map */
1902 movl MAP_PMAP(%ecx),%ecx /* get map->pmap */
1903 cmpl EXT(kernel_pmap), %ecx
1905 mov %ds,%cx /* else kernel data segment access */
1913 * Check whether user address space is writable
1914 * before writing to it - hardware is broken.
1916 * Skip check if "user" address is really in
1917 * kernel space (i.e., if it's in a kernel-loaded
1921 * esi/edi source/dest pointers for rep/mov
1922 * ecx counter for rep/mov
1923 * edx counts down from 3rd arg
1924 * eax count of bytes for each (partial) page copy
1925 * ebx shadows edi, used to adjust edx
1927 movl %edi,%ebx /* copy edi for syncing up */
1929 /* if restarting after a partial copy, put edx back in sync, */
1930 addl %ebx,%edx /* edx -= (edi - ebx); */
1932 movl %edi,%ebx /* ebx = edi; */
1935 * Copy only what fits on the current destination page.
1936 * Check for write-fault again on the next page.
1938 leal NBPG(%edi),%eax /* point to */
1939 andl $(-NBPG),%eax /* start of next page */
1940 subl %edi,%eax /* get number of bytes to that point */
1941 cmpl %edx,%eax /* bigger than count? */
1943 movl %edx,%eax /* use count */
1946 movl %eax,%ecx /* move by longwords first */
1949 RECOVER(copyout_fail)
1951 RETRY(copyout_retry)
1954 movl %eax,%ecx /* now move remaining bytes */
1957 RECOVER(copyout_fail)
1959 RETRY(copyout_retry)
1962 movl %edi,%ebx /* copy edi for syncing up */
1963 subl %eax,%edx /* and decrement count */
1964 jg copyout_retry /* restart on next page if not done */
1965 xorl %eax,%eax /* return 0 for success */
1967 mov %ss,%di /* restore kernel segment */
1971 popl %edi /* restore registers */
1973 ret /* and return */
1976 movl $ EFAULT,%eax /* return error for failure */
1977 jmp copyout_ret /* pop frame and return */
1994 pushl %eax /* get stack space */
2010 xor %eax,%eax /* clear high 16 bits of eax */
2011 fnstsw %ax /* read FP status */
2015 * Clear FPU exceptions
2022 * Clear task-switched flag.
2029 * Save complete FPU state. Save error for later.
2032 movl 4(%esp),%eax /* get save area pointer */
2033 fnsave (%eax) /* save complete state, including */
2038 * Restore FPU state.
2041 movl 4(%esp),%eax /* get save area pointer */
2042 frstor (%eax) /* restore complete state */
2052 * Don't set PDBR to a new value (hence invalidating the
2053 * "paging cache") if the new value matches the current one.
2055 movl %cr3,%edx /* get current cr3 value */
2057 je 0f /* if two are equal, don't set */
2058 movl %eax,%cr3 /* load it (and flush cache) */
2067 andl $(~0x7), %eax /* remove cpu number */
2074 movl %cr3,%eax /* flush tlb by reloading CR3 */
2075 movl %eax,%cr3 /* with itself */
2089 .byte 0x0f,0x20,0xe0 /* movl %cr4, %eax */
2097 .byte 0x0f,0x22,0xe0 /* movl %eax, %cr4 */
2116 * Read task register.
2124 * Set task register. Also clears busy bit of task descriptor.
2127 movl S_ARG0,%eax /* get task segment number */
2128 subl $8,%esp /* push space for SGDT */
2129 sgdt 2(%esp) /* store GDT limit and base (linear) */
2130 movl 4(%esp),%edx /* address GDT */
2131 movb $(K_TSS),5(%edx,%eax) /* fix access byte in task descriptor */
2132 ltr %ax /* load task register */
2133 addl $8,%esp /* clear stack */
2134 ret /* and return */
2137 * Set task-switched flag.
2140 movl %cr0,%eax /* get cr0 */
2141 orl $(CR0_TS),%eax /* or in TS bit */
2142 movl %eax,%cr0 /* set cr0 */
2146 * io register must not be used on slaves (no AT bus)
2148 #define ILL_ON_SLAVE
2156 #define PUSH_FRAME FRAME
2157 #define POP_FRAME EMARF
2159 #else /* MACH_ASSERT */
2167 #endif /* MACH_ASSERT */
2170 #if MACH_KDB || MACH_ASSERT
2173 * Following routines are also defined as macros in i386/pio.h
2174 * Compile then when MACH_KDB is configured so that they
2175 * can be invoked from the debugger.
2179 * void outb(unsigned char *io_port,
2180 * unsigned char byte)
2182 * Output a byte to an IO port.
2187 movl ARG0,%edx /* IO port address */
2188 movl ARG1,%eax /* data to output */
2189 outb %al,%dx /* send it out */
2194 * unsigned char inb(unsigned char *io_port)
2196 * Input a byte from an IO port.
2201 movl ARG0,%edx /* IO port address */
2202 xor %eax,%eax /* clear high bits of register */
2203 inb %dx,%al /* get the byte */
2208 * void outw(unsigned short *io_port,
2209 * unsigned short word)
2211 * Output a word to an IO port.
2216 movl ARG0,%edx /* IO port address */
2217 movl ARG1,%eax /* data to output */
2218 outw %ax,%dx /* send it out */
2223 * unsigned short inw(unsigned short *io_port)
2225 * Input a word from an IO port.
2230 movl ARG0,%edx /* IO port address */
2231 xor %eax,%eax /* clear high bits of register */
2232 inw %dx,%ax /* get the word */
2237 * void outl(unsigned int *io_port,
2238 * unsigned int byte)
2240 * Output an int to an IO port.
2245 movl ARG0,%edx /* IO port address*/
2246 movl ARG1,%eax /* data to output */
2247 outl %eax,%dx /* send it out */
2252 * unsigned int inl(unsigned int *io_port)
2254 * Input an int from an IO port.
2259 movl ARG0,%edx /* IO port address */
2260 inl %dx,%eax /* get the int */
2264 #endif /* MACH_KDB || MACH_ASSERT*/
2267 * void loutb(unsigned byte *io_port,
2268 * unsigned byte *data,
2269 * unsigned int count)
2271 * Output an array of bytes to an IO port.
2277 movl %esi,%eax /* save register */
2278 movl ARG0,%edx /* get io port number */
2279 movl ARG1,%esi /* get data address */
2280 movl ARG2,%ecx /* get count */
2284 movl %eax,%esi /* restore register */
2290 * void loutw(unsigned short *io_port,
2291 * unsigned short *data,
2292 * unsigned int count)
2294 * Output an array of shorts to an IO port.
2300 movl %esi,%eax /* save register */
2301 movl ARG0,%edx /* get io port number */
2302 movl ARG1,%esi /* get data address */
2303 movl ARG2,%ecx /* get count */
2307 movl %eax,%esi /* restore register */
2312 * void loutw(unsigned short io_port,
2313 * unsigned int *data,
2314 * unsigned int count)
2316 * Output an array of longs to an IO port.
2322 movl %esi,%eax /* save register */
2323 movl ARG0,%edx /* get io port number */
2324 movl ARG1,%esi /* get data address */
2325 movl ARG2,%ecx /* get count */
2329 movl %eax,%esi /* restore register */
2335 * void linb(unsigned char *io_port,
2336 * unsigned char *data,
2337 * unsigned int count)
2339 * Input an array of bytes from an IO port.
2345 movl %edi,%eax /* save register */
2346 movl ARG0,%edx /* get io port number */
2347 movl ARG1,%edi /* get data address */
2348 movl ARG2,%ecx /* get count */
2352 movl %eax,%edi /* restore register */
2358 * void linw(unsigned short *io_port,
2359 * unsigned short *data,
2360 * unsigned int count)
2362 * Input an array of shorts from an IO port.
2368 movl %edi,%eax /* save register */
2369 movl ARG0,%edx /* get io port number */
2370 movl ARG1,%edi /* get data address */
2371 movl ARG2,%ecx /* get count */
2375 movl %eax,%edi /* restore register */
2381 * void linl(unsigned short io_port,
2382 * unsigned int *data,
2383 * unsigned int count)
2385 * Input an array of longs from an IO port.
2391 movl %edi,%eax /* save register */
2392 movl ARG0,%edx /* get io port number */
2393 movl ARG1,%edi /* get data address */
2394 movl ARG2,%ecx /* get count */
2398 movl %eax,%edi /* restore register */
2404 * int inst_fetch(int eip, int cs);
2406 * Fetch instruction byte. Return -1 if invalid address.
2408 .globl EXT(inst_fetch)
2410 movl S_ARG1, %eax /* get segment */
2411 movw %ax,%fs /* into FS */
2412 movl S_ARG0, %eax /* get offset */
2414 RETRY(EXT(inst_fetch)) /* re-load FS on retry */
2416 RECOVER(EXT(inst_fetch_fault))
2417 movzbl %fs:(%eax),%eax /* load instruction byte */
2420 LEXT(inst_fetch_fault)
2421 movl $-1,%eax /* return -1 if error */
2427 * kdp_copy_kmem(char *src, char *dst, int count)
2429 * Similar to copyin except that both addresses are kernel addresses.
2432 ENTRY(kdp_copy_kmem)
2434 pushl %edi /* save registers */
2436 movl 8+S_ARG0,%esi /* get kernel start address */
2437 movl 8+S_ARG1,%edi /* get kernel destination address */
2439 movl 8+S_ARG2,%edx /* get count */
2441 lea 0(%esi,%edx),%eax /* get kernel end address + 1 */
2444 jb kdp_vm_read_fail /* fail if wrap-around */
2446 movl %edx,%ecx /* move by longwords first */
2449 RECOVER(kdp_vm_read_fail)
2451 movsl /* move longwords */
2452 movl %edx,%ecx /* now move remaining bytes */
2455 RECOVER(kdp_vm_read_fail)
2459 movl 8+S_ARG2,%edx /* get count */
2460 subl %ecx,%edx /* Return number of bytes transfered */
2463 popl %edi /* restore registers */
2465 ret /* and return */
2468 xorl %eax,%eax /* didn't copy a thing. */
2476 * int rdmsr_carefully(uint32_t msr, uint32_t *lo, uint32_t *hi)
2478 ENTRY(rdmsr_carefully)
2495 * Done with recovery and retry tables.
2508 /* dr<i>(address, type, len, persistence)
2512 movl %eax,EXT(dr_addr)
2518 movl %eax,EXT(dr_addr)+1*4
2524 movl %eax,EXT(dr_addr)+2*4
2531 movl %eax,EXT(dr_addr)+3*4
2540 movl %edx,EXT(dr_addr)+4*4
2541 andl dr_msk(,%ecx,2),%edx /* clear out new entry */
2542 movl %edx,EXT(dr_addr)+5*4
2561 movl %edx,EXT(dr_addr)+7*4
2609 lidt null_idtr /* disable the interrupt handler */
2610 xor %ecx,%ecx /* generate a divide by zero */
2611 div %ecx,%eax /* reboot now */
2612 ret /* this will "never" be executed */
2614 #endif /* SYMMETRY */
2618 * setbit(int bitno, int *s) - set bit in bit string
2621 movl S_ARG0, %ecx /* bit number */
2622 movl S_ARG1, %eax /* address */
2623 btsl %ecx, (%eax) /* set bit */
2627 * clrbit(int bitno, int *s) - clear bit in bit string
2630 movl S_ARG0, %ecx /* bit number */
2631 movl S_ARG1, %eax /* address */
2632 btrl %ecx, (%eax) /* clear bit */
2636 * ffsbit(int *s) - find first set bit in bit string
2639 movl S_ARG0, %ecx /* address */
2640 movl $0, %edx /* base offset */
2642 bsfl (%ecx), %eax /* check argument bits */
2643 jnz 1f /* found bit, return */
2644 addl $4, %ecx /* increment address */
2645 addl $32, %edx /* increment offset */
2646 jmp 0b /* try again */
2648 addl %edx, %eax /* return offset */
2652 * testbit(int nr, volatile void *array)
2654 * Test to see if the bit is set within the bit string
2658 movl S_ARG0,%eax /* Get the bit to test */
2659 movl S_ARG1,%ecx /* get the array string */
2671 movl 4(%ebp), %eax /* fetch pc of caller */
2674 ENTRY(tvals_to_etap)
2676 movl $1000000000, %ecx
2683 * etap_time_sub(etap_time_t stop, etap_time_t start)
2685 * 64bit subtract, returns stop - start
2687 ENTRY(etap_time_sub)
2688 movl S_ARG0, %eax /* stop.low */
2689 movl S_ARG1, %edx /* stop.hi */
2690 subl S_ARG2, %eax /* stop.lo - start.lo */
2691 sbbl S_ARG3, %edx /* stop.hi - start.hi */
2700 * jail: set the EIP to "jail" to block a kernel thread.
2701 * Useful to debug synchronization problems on MPs.
2708 * div_scale(unsigned int dividend,
2709 * unsigned int divisor,
2710 * unsigned int *scale)
2712 * This function returns (dividend << *scale) //divisor where *scale
2713 * is the largest possible value before overflow. This is used in
2714 * computation where precision must be achieved in order to avoid
2715 * floating point usage.
2719 * while (((dividend >> *scale) >= divisor))
2721 * *scale = 32 - *scale;
2722 * return ((dividend << *scale) / divisor);
2726 xorl %ecx, %ecx /* *scale = 0 */
2728 movl ARG0, %edx /* get dividend */
2730 cmpl ARG1, %edx /* if (divisor > dividend) */
2731 jle 1f /* goto 1f */
2732 addl $1, %ecx /* (*scale)++ */
2733 shrdl $1, %edx, %eax /* dividend >> 1 */
2734 shrl $1, %edx /* dividend >> 1 */
2735 jmp 0b /* goto 0b */
2737 divl ARG1 /* (dividend << (32 - *scale)) / divisor */
2738 movl ARG2, %edx /* get scale */
2739 movl $32, (%edx) /* *scale = 32 */
2740 subl %ecx, (%edx) /* *scale -= %ecx */
2746 * mul_scale(unsigned int multiplicand,
2747 * unsigned int multiplier,
2748 * unsigned int *scale)
2750 * This function returns ((multiplicand * multiplier) >> *scale) where
2751 * scale is the largest possible value before overflow. This is used in
2752 * computation where precision must be achieved in order to avoid
2753 * floating point usage.
2757 * while (overflow((multiplicand * multiplier) >> *scale))
2759 * return ((multiplicand * multiplier) >> *scale);
2763 xorl %ecx, %ecx /* *scale = 0 */
2764 movl ARG0, %eax /* get multiplicand */
2765 mull ARG1 /* multiplicand * multiplier */
2767 cmpl $0, %edx /* if (!overflow()) */
2769 addl $1, %ecx /* (*scale)++ */
2770 shrdl $1, %edx, %eax /* (multiplicand * multiplier) >> 1 */
2771 shrl $1, %edx /* (multiplicand * multiplier) >> 1 */
2774 movl ARG2, %edx /* get scale */
2775 movl %ecx, (%edx) /* set *scale */
2781 * BSD System call entry point..
2784 Entry(trap_unix_syscall)
2786 pushf /* save flags as soon as possible */
2788 pushl %eax /* save system call number */
2789 pushl $0 /* clear trap number slot */
2791 pusha /* save the general registers */
2792 pushl %ds /* and the segment registers */
2797 mov %ss,%dx /* switch to kernel data segment */
2800 mov $ CPU_DATA_GS,%dx
2804 * Shuffle eflags,eip,cs into proper places
2807 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
2808 movl R_CS(%esp),%ecx /* eip is in CS slot */
2809 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
2810 movl %ecx,R_EIP(%esp) /* fix eip */
2811 movl %edx,R_CS(%esp) /* fix cs */
2812 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
2816 negl %eax /* get system call number */
2817 shll $4,%eax /* manual indexing */
2819 movl %gs:CPU_KERNEL_STACK,%ebx
2820 /* get current kernel stack */
2821 xchgl %ebx,%esp /* switch stacks - %ebx points to */
2822 /* user registers. */
2825 * Register use on entry:
2826 * eax contains syscall number
2827 * ebx contains user regs pointer
2830 pushl %ebx /* Push the regs set onto stack */
2831 call EXT(unix_syscall)
2833 movl %esp,%ecx /* get kernel stack */
2834 or $(KERNEL_STACK_SIZE-1),%ecx
2835 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
2836 movl %eax,R_EAX(%esp) /* save return value */
2837 jmp EXT(return_from_trap) /* return to user */
2840 * Entry point for machdep system calls..
2843 Entry(trap_machdep_syscall)
2844 pushf /* save flags as soon as possible */
2845 pushl %eax /* save system call number */
2846 pushl $0 /* clear trap number slot */
2848 pusha /* save the general registers */
2849 pushl %ds /* and the segment registers */
2854 mov %ss,%dx /* switch to kernel data segment */
2857 mov $ CPU_DATA_GS,%dx
2861 * Shuffle eflags,eip,cs into proper places
2864 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
2865 movl R_CS(%esp),%ecx /* eip is in CS slot */
2866 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
2867 movl %ecx,R_EIP(%esp) /* fix eip */
2868 movl %edx,R_CS(%esp) /* fix cs */
2869 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
2873 negl %eax /* get system call number */
2874 shll $4,%eax /* manual indexing */
2876 movl %gs:CPU_KERNEL_STACK,%ebx
2877 /* get current kernel stack */
2878 xchgl %ebx,%esp /* switch stacks - %ebx points to */
2879 /* user registers. */
2882 * Register use on entry:
2883 * eax contains syscall number
2884 * ebx contains user regs pointer
2888 call EXT(machdep_syscall)
2890 movl %esp,%ecx /* get kernel stack */
2891 or $(KERNEL_STACK_SIZE-1),%ecx
2892 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
2893 movl %eax,R_EAX(%esp) /* save return value */
2894 jmp EXT(return_from_trap) /* return to user */
2896 Entry(trap_mach25_syscall)
2897 pushf /* save flags as soon as possible */
2898 pushl %eax /* save system call number */
2899 pushl $0 /* clear trap number slot */
2901 pusha /* save the general registers */
2902 pushl %ds /* and the segment registers */
2907 mov %ss,%dx /* switch to kernel data segment */
2910 mov $ CPU_DATA_GS,%dx
2914 * Shuffle eflags,eip,cs into proper places
2917 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
2918 movl R_CS(%esp),%ecx /* eip is in CS slot */
2919 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
2920 movl %ecx,R_EIP(%esp) /* fix eip */
2921 movl %edx,R_CS(%esp) /* fix cs */
2922 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
2926 negl %eax /* get system call number */
2927 shll $4,%eax /* manual indexing */
2929 movl %gs:CPU_KERNEL_STACK,%ebx
2930 /* get current kernel stack */
2931 xchgl %ebx,%esp /* switch stacks - %ebx points to */
2932 /* user registers. */
2935 * Register use on entry:
2936 * eax contains syscall number
2937 * ebx contains user regs pointer
2941 call EXT(mach25_syscall)
2943 movl %esp,%ecx /* get kernel stack */
2944 or $(KERNEL_STACK_SIZE-1),%ecx
2945 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
2946 movl %eax,R_EAX(%esp) /* save return value */
2947 jmp EXT(return_from_trap) /* return to user */