2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
52 #include <platforms.h>
54 #include <mach_kgdb.h>
56 #include <stat_time.h>
57 #include <mach_assert.h>
59 #include <sys/errno.h>
61 #include <i386/cpuid.h>
62 #include <i386/eflags.h>
63 #include <i386/proc_reg.h>
64 #include <i386/trap.h>
66 #include <mach/exception_types.h>
70 #define PREEMPT_DEBUG_LOG 0
74 * PTmap is recursive pagemap at top of virtual address space.
75 * Within PTmap, the page directory can be found (third indirection).
77 .globl _PTmap,_PTD,_PTDpde
78 .set _PTmap,(PTDPTDI << PDESHIFT)
79 .set _PTD,_PTmap + (PTDPTDI * NBPG)
80 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE)
83 * APTmap, APTD is the alternate recursive pagemap.
84 * It's used when modifying another process's page tables.
86 .globl _APTmap,_APTD,_APTDpde
87 .set _APTmap,(APTDPTDI << PDESHIFT)
88 .set _APTD,_APTmap + (APTDPTDI * NBPG)
89 .set _APTDpde,_PTD + (APTDPTDI * PDESIZE)
92 /* Under Mach-O, etext is a variable which contains
93 * the last text address
95 #define ETEXT_ADDR (EXT(etext))
97 /* Under ELF and other non-Mach-O formats, the address of
98 * etext represents the last text address
100 #define ETEXT_ADDR $ EXT(etext)
103 #define CX(addr,reg) addr(,reg,4)
113 #define RECOVERY_SECTION .section __VECTORS, __recover
114 #define RETRY_SECTION .section __VECTORS, __retries
116 #define RECOVERY_SECTION .text
117 #define RECOVERY_SECTION .text
120 #define RECOVER_TABLE_START \
122 .globl EXT(recover_table) ;\
123 LEXT(recover_table) ;\
126 #define RECOVER(addr) \
133 #define RECOVER_TABLE_END \
135 .globl EXT(recover_table_end) ;\
136 LEXT(recover_table_end) ;\
140 * Retry table for certain successful faults.
142 #define RETRY_TABLE_START \
144 .globl EXT(retry_table) ;\
148 #define RETRY(addr) \
155 #define RETRY_TABLE_END \
157 .globl EXT(retry_table_end) ;\
158 LEXT(retry_table_end) ;\
162 * Allocate recovery and retry tables.
176 movl %eax,TIMER_HIGHCHK(%ecx)
177 movl %edx,TIMER_LOW(%ecx)
178 movl %eax,TIMER_HIGH(%ecx)
183 0: movl TIMER_HIGH(%ecx),%edx
184 movl TIMER_LOW(%ecx),%eax
185 cmpl TIMER_HIGHCHK(%ecx),%edx
191 #define TIME_TRAP_UENTRY
192 #define TIME_TRAP_UEXIT
193 #define TIME_INT_ENTRY
194 #define TIME_INT_EXIT
202 * Low 32-bits of nanotime returned in %eax.
203 * Computed from tsc using conversion scale/shift from per-cpu data.
204 * Uses %ecx and %edx.
207 pushl %esi /* save %esi */ ;\
208 movl %gs:CPU_THIS,%esi /* per-cpu data ptr */ ;\
209 addl $(CPU_RTC_NANOTIME),%esi /* esi -> per-cpu nanotime*/ ;\
210 rdtsc /* edx:eax = tsc */ ;\
211 subl RTN_TSC(%esi),%eax /* eax = (tsc - base_tsc) */ ;\
212 mull RTN_SCALE(%esi) /* eax *= scale */ ;\
213 movl RTN_SHIFT(%esi),%ecx /* ecx = shift */ ;\
214 shrdl %cl,%edx,%eax /* edx:eax >> shift */ ;\
215 andb $32,%cl /* shift == 32? */ ;\
216 cmovnel %edx,%eax /* %eax = %edx if so */ ;\
217 addl RTN_NANOS(%esi),%eax /* add base ns */ ;\
221 * Add 32-bit ns delta in register dreg to timer pointed to by register treg.
223 #define TIMER_UPDATE(treg,dreg) \
224 addl TIMER_LOW(treg),dreg /* add delta low bits */ ;\
225 adcl $0,TIMER_HIGHCHK(treg) /* add carry check bits */ ;\
226 movl dreg,TIMER_LOW(treg) /* store updated low bit */ ;\
227 movl TIMER_HIGHCHK(treg),dreg /* copy high check bits */ ;\
228 movl dreg,TIMER_HIGH(treg) /* to high bita */
231 * Add time delta to old timer and start new.
233 #define TIMER_EVENT(old,new) \
234 pushl %eax /* must be invariant */ ;\
235 cli /* block interrupts */ ;\
236 NANOTIME32 /* eax low bits nanosecs */ ;\
237 movl %gs:CPU_PROCESSOR,%ecx /* get current processor */ ;\
238 movl CURRENT_TIMER(%ecx),%ecx /* get current timer */ ;\
239 movl %eax,%edx /* save timestamp in %edx */ ;\
240 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ;\
241 TIMER_UPDATE(%ecx,%eax) /* update timer struct */ ;\
242 addl $(new##_TIMER-old##_TIMER),%ecx /* point to new timer */ ;\
243 movl %edx,TIMER_TSTAMP(%ecx) /* set timestamp */ ;\
244 movl %gs:CPU_PROCESSOR,%edx /* get current processor */ ;\
245 movl %ecx,CURRENT_TIMER(%edx) /* set current timer */ ;\
246 sti /* interrupts on */ ;\
247 popl %eax /* must be invariant */
250 * Update time on user trap entry.
253 #define TIME_TRAP_UENTRY TIMER_EVENT(USER,SYSTEM)
256 * update time on user trap exit.
259 #define TIME_TRAP_UEXIT TIMER_EVENT(SYSTEM,USER)
262 * update time on interrupt entry.
263 * Uses %eax,%ecx,%edx.
265 #define TIME_INT_ENTRY \
266 NANOTIME32 /* eax low bits nanosecs */ ;\
267 movl %gs:CPU_PROCESSOR,%ecx /* get current processor */ ;\
268 movl CURRENT_TIMER(%ecx),%ecx /* get current timer */ ;\
269 movl %eax,%edx /* save timestamp in %edx */ ;\
270 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ;\
271 TIMER_UPDATE(%ecx,%eax) /* update timer struct */ ;\
272 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */ ;\
273 addl $(SYSTEM_TIMER),%ecx /* point to sys timer */ ;\
274 movl %edx,TIMER_TSTAMP(%ecx) /* set timestamp */
277 * update time on interrupt exit.
278 * Uses %eax, %ecx, %edx.
280 #define TIME_INT_EXIT \
281 NANOTIME32 /* eax low bits nanosecs */ ;\
282 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */ ;\
283 addl $(SYSTEM_TIMER),%ecx /* point to sys timer */ ;\
284 movl %eax,%edx /* save timestamp in %edx */ ;\
285 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ;\
286 TIMER_UPDATE(%ecx,%eax) /* update timer struct */ ;\
287 movl %gs:CPU_PROCESSOR,%ecx /* get current processor */ ;\
288 movl CURRENT_TIMER(%ecx),%ecx /* interrupted timer */ ;\
289 movl %edx,TIMER_TSTAMP(%ecx) /* set timestamp */
291 #endif /* STAT_TIME */
294 * Encapsulate the transfer of exception stack frames between a PCB
295 * and a thread stack. Since the whole point of these is to emulate
296 * a call or exception that changes privilege level, both macros
297 * assume that there is no user esp or ss stored in the source
298 * frame (because there was no change of privilege to generate them).
302 * Transfer a stack frame from a thread's user stack to its PCB.
303 * We assume the thread and stack addresses have been loaded into
304 * registers (our arguments).
306 * The macro overwrites edi, esi, ecx and whatever registers hold the
307 * thread and stack addresses (which can't be one of the above three).
308 * The thread address is overwritten with the address of its saved state
309 * (where the frame winds up).
311 * Must be called on kernel stack.
313 #define FRAME_STACK_TO_PCB(thread, stkp) ;\
314 movl ACT_PCB(thread),thread /* get act`s PCB */ ;\
315 leal PCB_ISS(thread),%edi /* point to PCB`s saved state */;\
316 movl %edi,thread /* save for later */ ;\
317 movl stkp,%esi /* point to start of frame */ ;\
318 movl $ R_UESP,%ecx ;\
319 sarl $2,%ecx /* word count for transfer */ ;\
320 cld /* we`re incrementing */ ;\
322 movsl /* transfer the frame */ ;\
323 addl $ R_UESP,stkp /* derive true "user" esp */ ;\
324 movl stkp,R_UESP(thread) /* store in PCB */ ;\
326 mov %ss,%cx /* get current ss */ ;\
327 movl %ecx,R_SS(thread) /* store in PCB */
330 * Transfer a stack frame from a thread's PCB to the stack pointed
331 * to by the PCB. We assume the thread address has been loaded into
332 * a register (our argument).
334 * The macro overwrites edi, esi, ecx and whatever register holds the
335 * thread address (which can't be one of the above three). The
336 * thread address is overwritten with the address of its saved state
337 * (where the frame winds up).
339 * Must be called on kernel stack.
341 #define FRAME_PCB_TO_STACK(thread) ;\
342 movl ACT_PCB(thread),%esi /* get act`s PCB */ ;\
343 leal PCB_ISS(%esi),%esi /* point to PCB`s saved state */;\
344 movl R_UESP(%esi),%edi /* point to end of dest frame */;\
345 movl ACT_MAP(thread),%ecx /* get act's map */ ;\
346 movl MAP_PMAP(%ecx),%ecx /* get map's pmap */ ;\
347 cmpl EXT(kernel_pmap), %ecx /* If kernel loaded task */ ;\
348 jz 1f /* use kernel data segment */ ;\
349 movl $ USER_DS,%ecx /* else use user data segment */;\
352 movl $ R_UESP,%ecx ;\
353 subl %ecx,%edi /* derive start of frame */ ;\
354 movl %edi,thread /* save for later */ ;\
355 sarl $2,%ecx /* word count for transfer */ ;\
356 cld /* we`re incrementing */ ;\
358 movsl /* transfer the frame */ ;\
359 mov %ss,%cx /* restore kernel segments */ ;\
367 * Traditional, not ANSI.
371 .globl label/**/count ;\
374 .globl label/**/limit ;\
378 addl $1,%ss:label/**/count ;\
379 cmpl $0,label/**/limit ;\
383 movl %ss:label/**/count,%eax ;\
384 cmpl %eax,%ss:label/**/limit ;\
397 * Last-ditch debug code to handle faults that might result
398 * from entering kernel (from collocated server) on an invalid
399 * stack. On collocated entry, there's no hardware-initiated
400 * stack switch, so a valid stack must be in place when an
401 * exception occurs, or we may double-fault.
403 * In case of a double-fault, our only recourse is to switch
404 * hardware "tasks", so that we avoid using the current stack.
406 * The idea here is just to get the processor into the debugger,
407 * post-haste. No attempt is made to fix up whatever error got
408 * us here, so presumably continuing from the debugger will
409 * simply land us here again -- at best.
413 * Note that the per-fault entry points are not currently
414 * functional. The only way to make them work would be to
415 * set up separate TSS's for each fault type, which doesn't
416 * currently seem worthwhile. (The offset part of a task
417 * gate is always ignored.) So all faults that task switch
418 * currently resume at db_task_start.
421 * Double fault (Murphy's point) - error code (0) on stack
423 Entry(db_task_dbl_fault)
425 movl $(T_DOUBLE_FAULT),%ebx
428 * Segment not present - error code on stack
430 Entry(db_task_seg_np)
432 movl $(T_SEGMENT_NOT_PRESENT),%ebx
435 * Stack fault - error code on (current) stack
437 Entry(db_task_stk_fault)
439 movl $(T_STACK_FAULT),%ebx
442 * General protection fault - error code on stack
444 Entry(db_task_gen_prot)
446 movl $(T_GENERAL_PROTECTION),%ebx
450 * The entry point where execution resumes after last-ditch debugger task
456 movl %edx,%esp /* allocate i386_saved_state on stack */
457 movl %eax,R_ERR(%esp)
458 movl %ebx,R_TRAPNO(%esp)
461 movl CX(EXT(mp_dbtss),%edx),%edx
462 movl TSS_LINK(%edx),%eax
463 pushl %eax /* pass along selector of previous TSS */
464 call EXT(db_tss_to_frame)
465 popl %eax /* get rid of TSS selector */
466 call EXT(db_trap_from_asm)
471 iret /* ha, ha, ha... */
472 #endif /* MACH_KDB */
475 * Trap/interrupt entry points.
477 * All traps must create the following save area on the PCB "stack":
486 * cr2 if page fault - otherwise unused
496 * user esp - if from user
497 * user ss - if from user
498 * es - if from V86 thread
499 * ds - if from V86 thread
500 * fs - if from V86 thread
501 * gs - if from V86 thread
506 * General protection or segment-not-present fault.
507 * Check for a GP/NP fault in the kernel_return
508 * sequence; if there, report it as a GP/NP fault on the user's instruction.
510 * esp-> 0: trap code (NP or GP)
511 * 4: segment number in error
515 * 20 old registers (trap is from kernel)
518 pushl $(T_GENERAL_PROTECTION) /* indicate fault type */
519 jmp trap_check_kernel_exit /* check for kernel exit sequence */
522 pushl $(T_SEGMENT_NOT_PRESENT)
523 /* indicate fault type */
525 trap_check_kernel_exit:
526 testl $(EFL_VM),16(%esp) /* is trap from V86 mode? */
527 jnz EXT(alltraps) /* isn`t kernel trap if so */
528 testl $3,12(%esp) /* is trap from kernel mode? */
529 jne EXT(alltraps) /* if so: */
530 /* check for the kernel exit sequence */
531 cmpl $ EXT(kret_iret),8(%esp) /* on IRET? */
533 cmpl $ EXT(kret_popl_ds),8(%esp) /* popping DS? */
535 cmpl $ EXT(kret_popl_es),8(%esp) /* popping ES? */
537 cmpl $ EXT(kret_popl_fs),8(%esp) /* popping FS? */
539 cmpl $ EXT(kret_popl_gs),8(%esp) /* popping GS? */
541 take_fault: /* if none of the above: */
542 jmp EXT(alltraps) /* treat as normal trap. */
545 * GP/NP fault on IRET: CS or SS is in error.
546 * All registers contain the user's values.
561 movl %eax,8(%esp) /* save eax (we don`t need saved eip) */
562 popl %eax /* get trap number */
563 movl %eax,12-4(%esp) /* put in user trap number */
564 popl %eax /* get error code */
565 movl %eax,16-8(%esp) /* put in user errcode */
566 popl %eax /* restore eax */
568 jmp EXT(alltraps) /* take fault */
571 * Fault restoring a segment register. The user's registers are still
572 * saved on the stack. The offending segment register has not been
576 popl %eax /* get trap number */
577 popl %edx /* get error code */
578 addl $12,%esp /* pop stack to user regs */
579 jmp push_es /* (DS on top of stack) */
581 popl %eax /* get trap number */
582 popl %edx /* get error code */
583 addl $12,%esp /* pop stack to user regs */
584 jmp push_fs /* (ES on top of stack) */
586 popl %eax /* get trap number */
587 popl %edx /* get error code */
588 addl $12,%esp /* pop stack to user regs */
589 jmp push_gs /* (FS on top of stack) */
591 popl %eax /* get trap number */
592 popl %edx /* get error code */
593 addl $12,%esp /* pop stack to user regs */
594 jmp push_segregs /* (GS on top of stack) */
597 pushl %es /* restore es, */
599 pushl %fs /* restore fs, */
601 pushl %gs /* restore gs. */
603 movl %eax,R_TRAPNO(%esp) /* set trap number */
604 movl %edx,R_ERR(%esp) /* set error code */
606 jmp trap_set_segs /* take trap */
609 * Debug trap. Check for single-stepping across system call into
610 * kernel. If this is the case, taking the debug trap has turned
611 * off single-stepping - save the flags register with the trace
615 testl $(EFL_VM),8(%esp) /* is trap from V86 mode? */
616 jnz 0f /* isn`t kernel trap if so */
617 testl $3,4(%esp) /* is trap from kernel mode? */
619 cmpl $syscall_entry,(%esp) /* system call entry? */
621 /* flags are sitting where syscall */
623 addl $8,%esp /* remove eip/cs */
624 jmp syscall_entry_2 /* continue system call entry */
626 1: cmpl $trap_unix_addr,(%esp)
631 0: pushl $0 /* otherwise: */
632 pushl $(T_DEBUG) /* handle as normal */
633 jmp EXT(alltraps) /* debug fault */
636 * Page fault traps save cr2.
639 pushl $(T_PAGE_FAULT) /* mark a page fault trap */
640 pusha /* save the general registers */
641 movl %cr2,%eax /* get the faulting address */
642 movl %eax,12(%esp) /* save in esp save slot */
643 jmp trap_push_segs /* continue fault */
646 * All 'exceptions' enter here with:
652 * old esp if trapped from user
653 * old ss if trapped from user
655 * NB: below use of CPU_NUMBER assumes that macro will use correct
656 * segment register for any kernel data accesses.
659 pusha /* save the general registers */
661 pushl %ds /* save the segment registers */
669 movl %eax,%es /* switch to kernel data seg */
670 cld /* clear direction flag */
671 testl $(EFL_VM),R_EFLAGS(%esp) /* in V86 mode? */
672 jnz trap_from_user /* user mode trap if so */
673 testb $3,R_CS(%esp) /* user mode trap? */
675 cmpl $0,%gs:CPU_ACTIVE_KLOADED
676 je trap_from_kernel /* if clear, truly in kernel */
678 cmpl ETEXT_ADDR,R_EIP(%esp) /* pc within kernel? */
683 * We didn't enter here "through" PCB (i.e., using ring 0 stack),
684 * so transfer the stack frame into the PCB explicitly, then
685 * start running on resulting "PCB stack". We have to set
686 * up a simulated "uesp" manually, since there's none in the
689 mov $ CPU_DATA_GS,%dx
692 movl %gs:CPU_ACTIVE_KLOADED,%ebx
693 movl %gs:CPU_KERNEL_STACK,%eax
695 FRAME_STACK_TO_PCB(%ebx,%eax)
700 mov $ CPU_DATA_GS,%ax
705 movl %gs:CPU_KERNEL_STACK,%ebx
706 xchgl %ebx,%esp /* switch to kernel stack */
707 /* user regs pointer already set */
709 pushl %ebx /* record register save area */
710 pushl %ebx /* pass register save area to trap */
711 call EXT(user_trap) /* call user trap routine */
712 movl 4(%esp),%esp /* switch back to PCB stack */
715 * Return from trap or system call, checking for ASTs.
719 LEXT(return_from_trap)
720 movl %gs:CPU_PENDING_AST,%edx
722 je EXT(return_to_user) /* if we need an AST: */
724 movl %gs:CPU_KERNEL_STACK,%esp
725 /* switch to kernel stack */
726 pushl $0 /* push preemption flag */
727 call EXT(i386_astintr) /* take the AST */
728 addl $4,%esp /* pop preemption flag */
729 popl %esp /* switch back to PCB stack (w/exc link) */
730 jmp EXT(return_from_trap) /* and check again (rare) */
731 /* ASTs after this point will */
735 * Arrange the checks needed for kernel-loaded (or kernel-loading)
736 * threads so that branch is taken in kernel-loaded case.
740 cmpl $0,%gs:CPU_ACTIVE_KLOADED
741 jnz EXT(return_xfer_stack)
742 movl %gs:CPU_ACTIVE_THREAD, %ebx /* get active thread */
746 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
747 je EXT(return_from_kernel)
749 #endif /* MACH_ASSERT */
753 * Return from kernel mode to interrupted thread.
756 LEXT(return_from_kernel)
758 popl %gs /* restore segment registers */
765 popa /* restore general registers */
766 addl $8,%esp /* discard trap number and error code */
769 iret /* return from interrupt */
772 LEXT(return_xfer_stack)
774 * If we're on PCB stack in a kernel-loaded task, we have
775 * to transfer saved state back to thread stack and swap
776 * stack pointers here, because the hardware's not going
780 movl %gs:CPU_KERNEL_STACK,%esp
781 movl %gs:CPU_ACTIVE_KLOADED,%eax
782 FRAME_PCB_TO_STACK(%eax)
785 jmp EXT(return_from_kernel)
788 * Hate to put this here, but setting up a separate swap_func for
789 * kernel-loaded threads no longer works, since thread executes
790 * "for a while" (i.e., until it reaches glue code) when first
791 * created, even if it's nominally suspended. Hence we can't
792 * transfer the PCB when the thread first resumes, because we
793 * haven't initialized it yet.
796 * Have to force transfer to new stack "manually". Use a string
797 * move to transfer all of our saved state to the stack pointed
798 * to by iss.uesp, then install a pointer to it as our current
801 LEXT(return_kernel_loading)
802 movl %gs:CPU_KERNEL_STACK,%esp
803 movl %gs:CPU_ACTIVE_THREAD, %ebx /* get active thread */
804 movl %ebx,%edx /* save for later */
805 FRAME_PCB_TO_STACK(%ebx)
806 movl %ebx,%esp /* start running on new stack */
807 movl $0,%gs:CPU_ACTIVE_KLOADED /* set cached indicator */
808 jmp EXT(return_from_kernel)
811 * Trap from kernel mode. No need to switch stacks or load segment registers.
814 #if MACH_KDB || MACH_KGDB
815 mov $ CPU_DATA_GS,%ax
817 movl %esp,%ebx /* save current stack */
819 cmpl EXT(int_stack_high),%esp /* on an interrupt stack? */
823 cmpl $0,EXT(kgdb_active) /* Unexpected trap in kgdb */
826 pushl %esp /* Already on kgdb stack */
830 jmp EXT(return_from_kernel)
831 0: /* should kgdb handle this exception? */
832 cmpl $(T_NO_FPU),R_TRAPNO(%esp) /* FPU disabled? */
834 cmpl $(T_PAGE_FAULT),R_TRAPNO(%esp) /* page fault? */
837 cli /* disable interrupts */
838 CPU_NUMBER(%edx) /* get CPU number */
839 movl CX(EXT(kgdb_stacks),%edx),%ebx
840 xchgl %ebx,%esp /* switch to kgdb stack */
841 pushl %ebx /* pass old sp as an arg */
842 call EXT(kgdb_from_kernel)
843 popl %esp /* switch back to kernel stack */
844 jmp EXT(return_from_kernel)
846 #endif /* MACH_KGDB */
849 cmpl $0,EXT(db_active) /* could trap be from ddb? */
851 CPU_NUMBER(%edx) /* see if this CPU is in ddb */
852 cmpl $0,CX(EXT(kdb_active),%edx)
855 call EXT(db_trap_from_asm)
857 jmp EXT(return_from_kernel)
861 * Dilemma: don't want to switch to kernel_stack if trap
862 * "belongs" to ddb; don't want to switch to db_stack if
863 * trap "belongs" to kernel. So have to duplicate here the
864 * set of trap types that kernel_trap() handles. Note that
865 * "unexpected" page faults will not be handled by kernel_trap().
866 * In this panic-worthy case, we fall into the debugger with
867 * kernel_stack containing the call chain that led to the
870 movl R_TRAPNO(%esp),%edx
871 cmpl $(T_PAGE_FAULT),%edx
873 cmpl $(T_NO_FPU),%edx
875 cmpl $(T_FPU_FAULT),%edx
877 cmpl $(T_FLOATING_POINT_ERROR),%edx
879 cmpl $(T_PREEMPT),%edx
882 #endif /* MACH_KDB */
884 cmpl %gs:CPU_KERNEL_STACK,%esp
885 /* if not already on kernel stack, */
886 ja 5f /* check some more */
887 cmpl %gs:CPU_ACTIVE_STACK,%esp
888 ja 6f /* on kernel stack: no switch */
890 movl %gs:CPU_KERNEL_STACK,%esp
892 pushl %ebx /* save old stack */
893 pushl %ebx /* pass as parameter */
894 call EXT(kernel_trap) /* to kernel trap routine */
895 addl $4,%esp /* pop parameter */
899 * If kernel_trap returns false, trap wasn't handled.
904 movl CX(EXT(db_stacks),%edx),%esp
905 pushl %ebx /* pass old stack as parameter */
906 call EXT(db_trap_from_asm)
907 #endif /* MACH_KDB */
909 cli /* disable interrupts */
910 CPU_NUMBER(%edx) /* get CPU number */
911 movl CX(EXT(kgdb_stacks),%edx),%esp
912 pushl %ebx /* pass old stack as parameter */
913 call EXT(kgdb_from_kernel)
914 #endif /* MACH_KGDB */
915 addl $4,%esp /* pop parameter */
919 * Likewise, if kdb_trap/kgdb_from_kernel returns false, trap
922 pushl %ebx /* pass old stack as parameter */
924 addl $4,%esp /* pop parameter */
926 movl %ebx,%esp /* get old stack (from callee-saves reg) */
927 #else /* MACH_KDB || MACH_KGDB */
928 pushl %esp /* pass parameter */
929 call EXT(kernel_trap) /* to kernel trap routine */
930 addl $4,%esp /* pop parameter */
931 #endif /* MACH_KDB || MACH_KGDB */
934 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
935 testl $ AST_URGENT,%eax /* any urgent preemption? */
936 je EXT(return_from_kernel) /* no, nothing to do */
937 cmpl $ T_PREEMPT,48(%esp) /* preempt request? */
938 jne EXT(return_from_kernel) /* no, nothing to do */
939 movl %gs:CPU_KERNEL_STACK,%eax
942 andl $(-KERNEL_STACK_SIZE),%ecx
943 testl %ecx,%ecx /* are we on the kernel stack? */
944 jne EXT(return_from_kernel) /* no, skip it */
946 #if PREEMPT_DEBUG_LOG
947 pushl 28(%esp) /* stack pointer */
948 pushl 24+4(%esp) /* frame pointer */
949 pushl 56+8(%esp) /* stack pointer */
951 call EXT(log_thread_action)
954 0: String "trap preempt eip"
956 #endif /* PREEMPT_DEBUG_LOG */
958 pushl $1 /* push preemption flag */
959 call EXT(i386_astintr) /* take the AST */
960 addl $4,%esp /* pop preemption flag */
963 jmp EXT(return_from_kernel)
966 * Called as a function, makes the current thread
967 * return from the kernel as if from an exception.
970 .globl EXT(thread_exception_return)
971 .globl EXT(thread_bootstrap_return)
972 LEXT(thread_exception_return)
973 LEXT(thread_bootstrap_return)
974 movl %esp,%ecx /* get kernel stack */
975 or $(KERNEL_STACK_SIZE-1),%ecx
976 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
977 jmp EXT(return_from_trap)
979 Entry(call_continuation)
980 movl S_ARG0,%eax /* get continuation */
981 movl S_ARG1,%edx /* continuation param */
982 movl S_ARG2,%ecx /* wait result */
983 movl %esp,%ebp /* get kernel stack */
984 or $(KERNEL_STACK_SIZE-1),%ebp
985 addl $(-3-IKS_SIZE),%ebp
986 movl %ebp,%esp /* pop the stack */
987 xorl %ebp,%ebp /* zero frame pointer */
990 call *%eax /* call continuation */
992 movl %gs:CPU_ACTIVE_THREAD,%eax
994 call EXT(thread_terminate)
997 #define LOG_INTERRUPT(info,msg) \
1001 call EXT(log_thread_action) ; \
1004 #define CHECK_INTERRUPT_TIME(n) \
1007 call EXT(check_thread_time) ; \
1011 #define LOG_INTERRUPT(info,msg)
1012 #define CHECK_INTERRUPT_TIME(n)
1017 String "interrupt start"
1019 String "interrupt end"
1023 * All interrupts enter here.
1024 * old %eax on stack; interrupt number in %eax.
1027 pushl %ecx /* save registers */
1029 cld /* clear direction flag */
1031 pushl %ds /* save segment registers */
1035 mov %ss,%dx /* switch to kernel segments */
1038 mov $ CPU_DATA_GS,%dx
1042 * test whether already on interrupt stack
1044 movl %gs:CPU_INT_STACK_TOP,%ecx
1047 leal -INTSTACK_SIZE(%ecx),%edx
1049 jb int_from_intstack
1051 movl %esp,%edx /* & i386_interrupt_state */
1052 xchgl %ecx,%esp /* switch to interrupt stack */
1054 pushl %ecx /* save pointer to old stack */
1055 pushl %edx /* pass &i386_interrupt_state to pe_incoming_interrupt */
1056 pushl %eax /* push trap number */
1058 TIME_INT_ENTRY /* do timing */
1061 incl %gs:CPU_PREEMPTION_LEVEL
1062 #endif /* MACH_RT */
1063 incl %gs:CPU_INTERRUPT_LEVEL
1065 call EXT(PE_incoming_interrupt) /* call generic interrupt routine */
1066 addl $8,%esp /* Pop trap number and eip */
1068 .globl EXT(return_to_iret)
1069 LEXT(return_to_iret) /* (label for kdb_kintr and hardclock) */
1071 decl %gs:CPU_INTERRUPT_LEVEL
1074 decl %gs:CPU_PREEMPTION_LEVEL
1075 #endif /* MACH_RT */
1077 TIME_INT_EXIT /* do timing */
1079 popl %esp /* switch back to old stack */
1081 movl %gs:CPU_PENDING_AST,%eax
1082 testl %eax,%eax /* any pending asts? */
1083 je 1f /* no, nothing to do */
1084 testl $(EFL_VM),I_EFL(%esp) /* if in V86 */
1085 jnz ast_from_interrupt /* take it */
1086 testb $3,I_CS(%esp) /* user mode, */
1087 jnz ast_from_interrupt /* take it */
1089 cmpl ETEXT_ADDR,I_EIP(%esp) /* if within kernel-loaded task, */
1090 jnb ast_from_interrupt /* take it */
1094 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption masked? */
1095 jne 1f /* yes, skip it */
1096 testl $ AST_URGENT,%eax /* any urgent requests? */
1097 je 1f /* no, skip it */
1098 cmpl $ EXT(locore_end),I_EIP(%esp) /* are we in locore code? */
1099 jb 1f /* yes, skip it */
1100 movl %gs:CPU_KERNEL_STACK,%eax
1103 andl $(-KERNEL_STACK_SIZE),%ecx
1104 testl %ecx,%ecx /* are we on the kernel stack? */
1105 jne 1f /* no, skip it */
1108 * Take an AST from kernel space. We don't need (and don't want)
1109 * to do as much as the case where the interrupt came from user
1112 #if PREEMPT_DEBUG_LOG
1117 call EXT(log_thread_action)
1120 0: String "intr preempt eip"
1122 #endif /* PREEMPT_DEBUG_LOG */
1125 pushl $1 /* push preemption flag */
1126 call EXT(i386_astintr) /* take the AST */
1127 addl $4,%esp /* pop preemption flag */
1128 #endif /* MACH_RT */
1133 pop %es /* restore segment regs */
1138 iret /* return to caller */
1142 incl %gs:CPU_PREEMPTION_LEVEL
1143 #endif /* MACH_RT */
1145 incl %gs:CPU_INTERRUPT_LEVEL
1147 movl %esp, %edx /* i386_interrupt_state */
1148 pushl %edx /* pass &i386_interrupt_state to PE_incoming_interrupt /*
1150 pushl %eax /* Push trap number */
1152 call EXT(PE_incoming_interrupt)
1153 addl $20,%esp /* pop i386_interrupt_state, gs,fs,es,ds */
1155 LEXT(return_to_iret_i) /* ( label for kdb_kintr) */
1157 addl $4,%esp /* pop trap number */
1159 decl %gs:CPU_INTERRUPT_LEVEL
1162 decl %gs:CPU_PREEMPTION_LEVEL
1163 #endif /* MACH_RT */
1165 pop %edx /* must have been on kernel segs */
1167 pop %eax /* no ASTs */
1171 * Take an AST from an interrupt.
1187 pop %es /* restore all registers ... */
1192 sti /* Reenable interrupts */
1193 pushl $0 /* zero code */
1194 pushl $0 /* zero trap number */
1195 pusha /* save general registers */
1196 push %ds /* save segment registers */
1200 mov %ss,%dx /* switch to kernel segments */
1203 mov $ CPU_DATA_GS,%dx
1207 * See if we interrupted a kernel-loaded thread executing
1211 testl $(EFL_VM),R_EFLAGS(%esp) /* in V86 mode? */
1212 jnz 0f /* user mode trap if so */
1214 jnz 0f /* user mode, back to normal */
1216 cmpl ETEXT_ADDR,R_EIP(%esp)
1217 jb 0f /* not kernel-loaded, back to normal */
1221 * Transfer the current stack frame by hand into the PCB.
1224 movl %gs:CPU_ACTIVE_KLOADED,%eax
1225 movl %gs:CPU_KERNEL_STACK,%ebx
1227 FRAME_STACK_TO_PCB(%eax,%ebx)
1234 movl %gs:CPU_KERNEL_STACK,%eax
1235 /* switch to kernel stack */
1239 pushl $0 /* push preemption flag */
1240 call EXT(i386_astintr) /* take the AST */
1241 addl $4,%esp /* pop preemption flag */
1242 popl %esp /* back to PCB stack */
1243 jmp EXT(return_from_trap) /* return */
1245 #if MACH_KDB || MACH_KGDB
1247 * kdb_kintr: enter kdb from keyboard interrupt.
1248 * Chase down the stack frames until we find one whose return
1249 * address is the interrupt handler. At that point, we have:
1251 * frame-> saved %ebp
1252 * return address in interrupt handler
1255 * return address == return_to_iret_i
1264 * frame-> saved %ebp
1265 * return address in interrupt handler
1268 * return address == return_to_iret
1269 * pointer to save area on old stack
1270 * [ saved %ebx, if accurate timing ]
1272 * old stack: saved %es
1281 * Call kdb, passing it that register save area.
1286 #endif /* MACH_KGDB */
1289 #endif /* MACH_KDB */
1290 movl %ebp,%eax /* save caller`s frame pointer */
1291 movl $ EXT(return_to_iret),%ecx /* interrupt return address 1 */
1292 movl $ EXT(return_to_iret_i),%edx /* interrupt return address 2 */
1294 0: cmpl 16(%eax),%ecx /* does this frame return to */
1295 /* interrupt handler (1)? */
1297 cmpl $kdb_from_iret,16(%eax)
1299 cmpl 16(%eax),%edx /* interrupt handler (2)? */
1301 cmpl $kdb_from_iret_i,16(%eax)
1303 movl (%eax),%eax /* try next frame */
1306 1: movl $kdb_from_iret,16(%eax) /* returns to kernel/user stack */
1309 2: movl $kdb_from_iret_i,16(%eax)
1310 /* returns to interrupt stack */
1314 * On return from keyboard interrupt, we will execute
1316 * if returning to an interrupt on the interrupt stack
1318 * if returning to an interrupt on the user or kernel stack
1321 /* save regs in known locations */
1322 pushl %ebx /* caller`s %ebx is in reg */
1330 pushl %esp /* pass regs */
1331 call EXT(kgdb_kentry) /* to kgdb */
1332 addl $4,%esp /* pop parameters */
1333 #endif /* MACH_KGDB */
1335 pushl %esp /* pass regs */
1336 call EXT(kdb_kentry) /* to kdb */
1337 addl $4,%esp /* pop parameters */
1338 #endif /* MACH_KDB */
1339 pop %gs /* restore registers */
1345 jmp EXT(return_to_iret) /* normal interrupt return */
1347 kdb_from_iret_i: /* on interrupt stack */
1348 pop %edx /* restore saved registers */
1351 pushl $0 /* zero error code */
1352 pushl $0 /* zero trap number */
1353 pusha /* save general registers */
1354 push %ds /* save segment registers */
1359 cli /* disable interrupts */
1360 CPU_NUMBER(%edx) /* get CPU number */
1361 movl CX(EXT(kgdb_stacks),%edx),%ebx
1362 xchgl %ebx,%esp /* switch to kgdb stack */
1363 pushl %ebx /* pass old sp as an arg */
1364 call EXT(kgdb_from_kernel)
1365 popl %esp /* switch back to interrupt stack */
1366 #endif /* MACH_KGDB */
1368 pushl %esp /* pass regs, */
1369 pushl $0 /* code, */
1370 pushl $-1 /* type to kdb */
1373 #endif /* MACH_KDB */
1374 pop %gs /* restore segment registers */
1378 popa /* restore general registers */
1382 #endif /* MACH_KDB || MACH_KGDB */
1386 * Mach RPC enters through a call gate, like a system call.
1390 pushf /* save flags as soon as possible */
1391 pushl %eax /* save system call number */
1392 pushl $0 /* clear trap number slot */
1394 pusha /* save the general registers */
1395 pushl %ds /* and the segment registers */
1400 mov %ss,%dx /* switch to kernel data segment */
1403 mov $ CPU_DATA_GS,%dx
1407 * Shuffle eflags,eip,cs into proper places
1410 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
1411 movl R_CS(%esp),%ecx /* eip is in CS slot */
1412 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
1413 movl %ecx,R_EIP(%esp) /* fix eip */
1414 movl %edx,R_CS(%esp) /* fix cs */
1415 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
1419 negl %eax /* get system call number */
1420 shll $4,%eax /* manual indexing */
1423 * Check here for mach_rpc from kernel-loaded task --
1424 * - Note that kernel-loaded task returns via real return.
1425 * We didn't enter here "through" PCB (i.e., using ring 0 stack),
1426 * so transfer the stack frame into the PCB explicitly, then
1427 * start running on resulting "PCB stack". We have to set
1428 * up a simulated "uesp" manually, since there's none in the
1431 cmpl $0,%gs:CPU_ACTIVE_KLOADED
1434 movl %gs:CPU_ACTIVE_KLOADED,%ebx
1435 movl %gs:CPU_KERNEL_STACK,%edx
1438 FRAME_STACK_TO_PCB(%ebx,%edx)
1444 movl %gs:CPU_KERNEL_STACK,%ebx
1445 /* get current kernel stack */
1446 xchgl %ebx,%esp /* switch stacks - %ebx points to */
1447 /* user registers. */
1452 * Register use on entry:
1453 * eax contains syscall number
1454 * ebx contains user regs pointer
1456 #undef RPC_TRAP_REGISTERS
1457 #ifdef RPC_TRAP_REGISTERS
1463 movl EXT(mach_trap_table)(%eax),%ecx
1464 /* get number of arguments */
1465 jecxz 2f /* skip argument copy if none */
1466 movl R_UESP(%ebx),%esi /* get user stack pointer */
1467 lea 4(%esi,%ecx,4),%esi /* skip user return address, */
1468 /* and point past last argument */
1469 movl %gs:CPU_ACTIVE_KLOADED,%edx
1470 /* point to current thread */
1471 orl %edx,%edx /* if ! kernel-loaded, check addr */
1473 mov %ds,%dx /* kernel data segment access */
1476 cmpl $(VM_MAX_ADDRESS),%esi /* in user space? */
1477 ja mach_call_addr /* address error if not */
1478 movl $ USER_DS,%edx /* user data segment access */
1481 movl %esp,%edx /* save kernel ESP for error recovery */
1485 RECOVER(mach_call_addr_push)
1486 pushl %fs:(%esi) /* push argument on stack */
1487 loop 1b /* loop for all arguments */
1491 * Register use on entry:
1492 * eax contains syscall number << 4
1493 * mach_call_munger is declared regparm(1), so the first arg is %eax
1497 call EXT(mach_call_munger)
1499 movl %esp,%ecx /* get kernel stack */
1500 or $(KERNEL_STACK_SIZE-1),%ecx
1501 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
1502 movl %eax,R_EAX(%esp) /* save return value */
1503 jmp EXT(return_from_trap) /* return to user */
1507 * Special system call entry for "int 0x80", which has the "eflags"
1508 * register saved at the right place already.
1509 * Fall back to the common syscall path after saving the registers.
1514 * old esp if trapped from user
1515 * old ss if trapped from user
1517 * XXX: for the moment, we don't check for int 0x80 from kernel mode.
1519 Entry(syscall_int80)
1520 pushl %eax /* save system call number */
1521 pushl $0 /* clear trap number slot */
1523 pusha /* save the general registers */
1524 pushl %ds /* and the segment registers */
1529 mov %ss,%dx /* switch to kernel data segment */
1532 mov $ CPU_DATA_GS,%dx
1538 * System call enters through a call gate. Flags are not saved -
1539 * we must shuffle stack to look like trap save area.
1546 * eax contains system call number.
1548 * NB: below use of CPU_NUMBER assumes that macro will use correct
1549 * correct segment register for any kernel data accesses.
1553 pushf /* save flags as soon as possible */
1555 pushl %eax /* save system call number */
1556 pushl $0 /* clear trap number slot */
1558 pusha /* save the general registers */
1559 pushl %ds /* and the segment registers */
1564 mov %ss,%dx /* switch to kernel data segment */
1567 mov $ CPU_DATA_GS,%dx
1571 * Shuffle eflags,eip,cs into proper places
1574 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
1575 movl R_CS(%esp),%ecx /* eip is in CS slot */
1576 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
1577 movl %ecx,R_EIP(%esp) /* fix eip */
1578 movl %edx,R_CS(%esp) /* fix cs */
1579 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
1583 * Check here for syscall from kernel-loaded task --
1584 * We didn't enter here "through" PCB (i.e., using ring 0 stack),
1585 * so transfer the stack frame into the PCB explicitly, then
1586 * start running on resulting "PCB stack". We have to set
1587 * up a simulated "uesp" manually, since there's none in the
1590 cmpl $0,%gs:CPU_ACTIVE_KLOADED
1593 movl %gs:CPU_ACTIVE_KLOADED,%ebx
1594 movl %gs:CPU_KERNEL_STACK,%edx
1596 FRAME_STACK_TO_PCB(%ebx,%edx)
1604 movl %gs:CPU_KERNEL_STACK,%ebx
1605 /* get current kernel stack */
1606 xchgl %ebx,%esp /* switch stacks - %ebx points to */
1607 /* user registers. */
1608 /* user regs pointer already set */
1611 * Native system call.
1612 * Register use on entry:
1613 * eax contains syscall number
1614 * ebx points to user regs
1617 negl %eax /* get system call number */
1618 jl mach_call_range /* out of range if it was positive */
1620 cmpl EXT(mach_trap_count),%eax /* check system call table bounds */
1621 jg mach_call_range /* error if out of range */
1622 shll $4,%eax /* manual indexing */
1624 movl EXT(mach_trap_table)+4(%eax),%edx
1626 cmpl $ EXT(kern_invalid),%edx /* if not "kern_invalid" */
1627 jne do_native_call /* go on with Mach syscall */
1628 shrl $4,%eax /* restore syscall number */
1629 jmp mach_call_range /* try it as a "server" syscall */
1632 * Register use on entry:
1633 * eax contains syscall number
1634 * ebx contains user regs pointer
1637 movl EXT(mach_trap_table)(%eax),%ecx
1638 /* get number of arguments */
1639 jecxz mach_call_call /* skip argument copy if none */
1640 movl R_UESP(%ebx),%esi /* get user stack pointer */
1641 lea 4(%esi,%ecx,4),%esi /* skip user return address, */
1642 /* and point past last argument */
1643 movl %gs:CPU_ACTIVE_KLOADED,%edx
1644 /* point to current thread */
1645 orl %edx,%edx /* if kernel-loaded, skip addr check */
1647 mov %ds,%dx /* kernel data segment access */
1650 cmpl $(VM_MAX_ADDRESS),%esi /* in user space? */
1651 ja mach_call_addr /* address error if not */
1652 movl $ USER_DS,%edx /* user data segment access */
1655 movl %esp,%edx /* save kernel ESP for error recovery */
1659 RECOVER(mach_call_addr_push)
1660 pushl %fs:(%esi) /* push argument on stack */
1661 loop 2b /* loop for all arguments */
1664 * Register use on entry:
1665 * eax contains syscall number
1666 * ebx contains user regs pointer
1672 #if ETAP_EVENT_MONITOR
1673 cmpl $0x200, %eax /* is this mach_msg? */
1674 jz make_syscall /* if yes, don't record event */
1676 pushal /* Otherwise: save registers */
1677 pushl %eax /* push syscall number on stack*/
1678 call EXT(etap_machcall_probe1) /* call event begin probe */
1679 add $4,%esp /* restore stack */
1680 popal /* restore registers */
1682 call *EXT(mach_trap_table)+4(%eax) /* call procedure */
1684 call EXT(etap_machcall_probe2) /* call event end probe */
1686 jmp skip_syscall /* syscall already made */
1687 #endif /* ETAP_EVENT_MONITOR */
1692 * mach_call_munger is declared regparm(1) so the first arg is %eax
1694 call EXT(mach_call_munger)
1698 movl %esp,%ecx /* get kernel stack */
1699 or $(KERNEL_STACK_SIZE-1),%ecx
1700 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
1701 movl %eax,R_EAX(%esp) /* save return value */
1702 jmp EXT(return_from_trap) /* return to user */
1705 * Address out of range. Change to page fault.
1706 * %esi holds failing address.
1707 * Register use on entry:
1708 * ebx contains user regs pointer
1710 mach_call_addr_push:
1711 movl %edx,%esp /* clean parameters from stack */
1713 movl %esi,R_CR2(%ebx) /* set fault address */
1714 movl $(T_PAGE_FAULT),R_TRAPNO(%ebx)
1715 /* set page-fault trap */
1716 movl $(T_PF_USER),R_ERR(%ebx)
1717 /* set error code - read user space */
1719 jmp EXT(take_trap) /* treat as a trap */
1722 * System call out of range. Treat as invalid-instruction trap.
1723 * (? general protection?)
1724 * Register use on entry:
1725 * eax contains syscall number
1730 push $1 /* code_cnt = 1 */
1731 push %edx /* exception_type_t (see i/f docky) */
1734 call EXT(exception_triage)
1737 .globl EXT(syscall_failed)
1738 LEXT(syscall_failed)
1739 movl %esp,%ecx /* get kernel stack */
1740 or $(KERNEL_STACK_SIZE-1),%ecx
1741 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
1742 movl %gs:CPU_KERNEL_STACK,%ebx
1743 /* get current kernel stack */
1744 xchgl %ebx,%esp /* switch stacks - %ebx points to */
1745 /* user registers. */
1746 /* user regs pointer already set */
1748 movl $(T_INVALID_OPCODE),R_TRAPNO(%ebx)
1749 /* set invalid-operation trap */
1750 movl $0,R_ERR(%ebx) /* clear error code */
1752 jmp EXT(take_trap) /* treat as a trap */
1761 * Copy from user address space.
1762 * arg0: user address
1763 * arg1: kernel address
1769 pushl %edi /* save registers */
1771 movl 8+S_ARG0,%esi /* get user start address */
1772 movl 8+S_ARG1,%edi /* get kernel destination address */
1773 movl 8+S_ARG2,%edx /* get count */
1775 lea 0(%esi,%edx),%eax /* get user end address + 1 */
1777 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get active thread */
1778 movl ACT_MAP(%ecx),%ecx /* get act->map */
1779 movl MAP_PMAP(%ecx),%ecx /* get map->pmap */
1780 cmpl EXT(kernel_pmap), %ecx
1782 movl $ USER_DS,%ecx /* user data segment access */
1786 jb copyin_fail /* fail if wrap-around */
1788 movl %edx,%ecx /* move by longwords first */
1791 RECOVER(copyin_fail)
1793 movsl /* move longwords */
1794 movl %edx,%ecx /* now move remaining bytes */
1797 RECOVER(copyin_fail)
1800 xorl %eax,%eax /* return 0 for success */
1802 mov %ss,%di /* restore kernel data segment */
1805 popl %edi /* restore registers */
1807 ret /* and return */
1810 movl $ EFAULT,%eax /* return error for failure */
1811 jmp copy_ret /* pop frame and return */
1814 * Copy string from user address space.
1815 * arg0: user address
1816 * arg1: kernel address
1817 * arg2: max byte count
1818 * arg3: actual byte count (OUT)
1822 pushl %edi /* save registers */
1824 movl 8+S_ARG0,%esi /* get user start address */
1825 movl 8+S_ARG1,%edi /* get kernel destination address */
1826 movl 8+S_ARG2,%edx /* get count */
1828 lea 0(%esi,%edx),%eax /* get user end address + 1 */
1830 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get active thread */
1831 movl ACT_MAP(%ecx),%ecx /* get act->map */
1832 movl MAP_PMAP(%ecx),%ecx /* get map->pmap */
1833 cmpl EXT(kernel_pmap), %ecx
1835 mov %ds,%cx /* kernel data segment access */
1838 movl $ USER_DS,%ecx /* user data segment access */
1846 RECOVER(copystr_fail) /* copy bytes... */
1849 testl %edi,%edi /* if kernel address is ... */
1850 jz 3f /* not NULL */
1851 movb %al,(%edi) /* copy the byte */
1855 je 5f /* Zero count.. error out */
1857 jne 2b /* .. a NUL found? */
1858 jmp 4f /* return zero (%eax) */
1860 movl $ ENAMETOOLONG,%eax /* String is too long.. */
1862 movl 8+S_ARG3,%edi /* get OUT len ptr */
1864 jz copystr_ret /* if null, just return */
1866 movl %esi,(%edi) /* else set OUT arg to xfer len */
1868 popl %edi /* restore registers */
1870 ret /* and return */
1873 movl $ EFAULT,%eax /* return error for failure */
1874 jmp copy_ret /* pop frame and return */
1877 * Copy to user address space.
1878 * arg0: kernel address
1879 * arg1: user address
1885 pushl %edi /* save registers */
1888 movl 12+S_ARG0,%esi /* get kernel start address */
1889 movl 12+S_ARG1,%edi /* get user start address */
1890 movl 12+S_ARG2,%edx /* get count */
1892 leal 0(%edi,%edx),%eax /* get user end address + 1 */
1894 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get active thread */
1895 movl ACT_MAP(%ecx),%ecx /* get act->map */
1896 movl MAP_PMAP(%ecx),%ecx /* get map->pmap */
1897 cmpl EXT(kernel_pmap), %ecx
1899 mov %ds,%cx /* else kernel data segment access */
1907 * Check whether user address space is writable
1908 * before writing to it - hardware is broken.
1910 * Skip check if "user" address is really in
1911 * kernel space (i.e., if it's in a kernel-loaded
1915 * esi/edi source/dest pointers for rep/mov
1916 * ecx counter for rep/mov
1917 * edx counts down from 3rd arg
1918 * eax count of bytes for each (partial) page copy
1919 * ebx shadows edi, used to adjust edx
1921 movl %edi,%ebx /* copy edi for syncing up */
1923 /* if restarting after a partial copy, put edx back in sync, */
1924 addl %ebx,%edx /* edx -= (edi - ebx); */
1926 movl %edi,%ebx /* ebx = edi; */
1929 * Copy only what fits on the current destination page.
1930 * Check for write-fault again on the next page.
1932 leal NBPG(%edi),%eax /* point to */
1933 andl $(-NBPG),%eax /* start of next page */
1934 subl %edi,%eax /* get number of bytes to that point */
1935 cmpl %edx,%eax /* bigger than count? */
1937 movl %edx,%eax /* use count */
1940 movl %eax,%ecx /* move by longwords first */
1943 RECOVER(copyout_fail)
1945 RETRY(copyout_retry)
1948 movl %eax,%ecx /* now move remaining bytes */
1951 RECOVER(copyout_fail)
1953 RETRY(copyout_retry)
1956 movl %edi,%ebx /* copy edi for syncing up */
1957 subl %eax,%edx /* and decrement count */
1958 jg copyout_retry /* restart on next page if not done */
1959 xorl %eax,%eax /* return 0 for success */
1961 mov %ss,%di /* restore kernel segment */
1965 popl %edi /* restore registers */
1967 ret /* and return */
1970 movl $ EFAULT,%eax /* return error for failure */
1971 jmp copyout_ret /* pop frame and return */
1988 pushl %eax /* get stack space */
2004 xor %eax,%eax /* clear high 16 bits of eax */
2005 fnstsw %ax /* read FP status */
2009 * Clear FPU exceptions
2016 * Clear task-switched flag.
2023 * Save complete FPU state. Save error for later.
2026 movl 4(%esp),%eax /* get save area pointer */
2027 fnsave (%eax) /* save complete state, including */
2032 * Restore FPU state.
2035 movl 4(%esp),%eax /* get save area pointer */
2036 frstor (%eax) /* restore complete state */
2046 * Don't set PDBR to a new value (hence invalidating the
2047 * "paging cache") if the new value matches the current one.
2049 movl %cr3,%edx /* get current cr3 value */
2051 je 0f /* if two are equal, don't set */
2052 movl %eax,%cr3 /* load it (and flush cache) */
2061 andl $(~0x7), %eax /* remove cpu number */
2068 movl %cr3,%eax /* flush tlb by reloading CR3 */
2069 movl %eax,%cr3 /* with itself */
2083 .byte 0x0f,0x20,0xe0 /* movl %cr4, %eax */
2091 .byte 0x0f,0x22,0xe0 /* movl %eax, %cr4 */
2110 * Read task register.
2118 * Set task register. Also clears busy bit of task descriptor.
2121 movl S_ARG0,%eax /* get task segment number */
2122 subl $8,%esp /* push space for SGDT */
2123 sgdt 2(%esp) /* store GDT limit and base (linear) */
2124 movl 4(%esp),%edx /* address GDT */
2125 movb $(K_TSS),5(%edx,%eax) /* fix access byte in task descriptor */
2126 ltr %ax /* load task register */
2127 addl $8,%esp /* clear stack */
2128 ret /* and return */
2131 * Set task-switched flag.
2134 movl %cr0,%eax /* get cr0 */
2135 orl $(CR0_TS),%eax /* or in TS bit */
2136 movl %eax,%cr0 /* set cr0 */
2140 * io register must not be used on slaves (no AT bus)
2142 #define ILL_ON_SLAVE
2150 #define PUSH_FRAME FRAME
2151 #define POP_FRAME EMARF
2153 #else /* MACH_ASSERT */
2161 #endif /* MACH_ASSERT */
2164 #if MACH_KDB || MACH_ASSERT
2167 * Following routines are also defined as macros in i386/pio.h
2168 * Compile then when MACH_KDB is configured so that they
2169 * can be invoked from the debugger.
2173 * void outb(unsigned char *io_port,
2174 * unsigned char byte)
2176 * Output a byte to an IO port.
2181 movl ARG0,%edx /* IO port address */
2182 movl ARG1,%eax /* data to output */
2183 outb %al,%dx /* send it out */
2188 * unsigned char inb(unsigned char *io_port)
2190 * Input a byte from an IO port.
2195 movl ARG0,%edx /* IO port address */
2196 xor %eax,%eax /* clear high bits of register */
2197 inb %dx,%al /* get the byte */
2202 * void outw(unsigned short *io_port,
2203 * unsigned short word)
2205 * Output a word to an IO port.
2210 movl ARG0,%edx /* IO port address */
2211 movl ARG1,%eax /* data to output */
2212 outw %ax,%dx /* send it out */
2217 * unsigned short inw(unsigned short *io_port)
2219 * Input a word from an IO port.
2224 movl ARG0,%edx /* IO port address */
2225 xor %eax,%eax /* clear high bits of register */
2226 inw %dx,%ax /* get the word */
2231 * void outl(unsigned int *io_port,
2232 * unsigned int byte)
2234 * Output an int to an IO port.
2239 movl ARG0,%edx /* IO port address*/
2240 movl ARG1,%eax /* data to output */
2241 outl %eax,%dx /* send it out */
2246 * unsigned int inl(unsigned int *io_port)
2248 * Input an int from an IO port.
2253 movl ARG0,%edx /* IO port address */
2254 inl %dx,%eax /* get the int */
2258 #endif /* MACH_KDB || MACH_ASSERT*/
2261 * void loutb(unsigned byte *io_port,
2262 * unsigned byte *data,
2263 * unsigned int count)
2265 * Output an array of bytes to an IO port.
2271 movl %esi,%eax /* save register */
2272 movl ARG0,%edx /* get io port number */
2273 movl ARG1,%esi /* get data address */
2274 movl ARG2,%ecx /* get count */
2278 movl %eax,%esi /* restore register */
2284 * void loutw(unsigned short *io_port,
2285 * unsigned short *data,
2286 * unsigned int count)
2288 * Output an array of shorts to an IO port.
2294 movl %esi,%eax /* save register */
2295 movl ARG0,%edx /* get io port number */
2296 movl ARG1,%esi /* get data address */
2297 movl ARG2,%ecx /* get count */
2301 movl %eax,%esi /* restore register */
2306 * void loutw(unsigned short io_port,
2307 * unsigned int *data,
2308 * unsigned int count)
2310 * Output an array of longs to an IO port.
2316 movl %esi,%eax /* save register */
2317 movl ARG0,%edx /* get io port number */
2318 movl ARG1,%esi /* get data address */
2319 movl ARG2,%ecx /* get count */
2323 movl %eax,%esi /* restore register */
2329 * void linb(unsigned char *io_port,
2330 * unsigned char *data,
2331 * unsigned int count)
2333 * Input an array of bytes from an IO port.
2339 movl %edi,%eax /* save register */
2340 movl ARG0,%edx /* get io port number */
2341 movl ARG1,%edi /* get data address */
2342 movl ARG2,%ecx /* get count */
2346 movl %eax,%edi /* restore register */
2352 * void linw(unsigned short *io_port,
2353 * unsigned short *data,
2354 * unsigned int count)
2356 * Input an array of shorts from an IO port.
2362 movl %edi,%eax /* save register */
2363 movl ARG0,%edx /* get io port number */
2364 movl ARG1,%edi /* get data address */
2365 movl ARG2,%ecx /* get count */
2369 movl %eax,%edi /* restore register */
2375 * void linl(unsigned short io_port,
2376 * unsigned int *data,
2377 * unsigned int count)
2379 * Input an array of longs from an IO port.
2385 movl %edi,%eax /* save register */
2386 movl ARG0,%edx /* get io port number */
2387 movl ARG1,%edi /* get data address */
2388 movl ARG2,%ecx /* get count */
2392 movl %eax,%edi /* restore register */
2398 * int inst_fetch(int eip, int cs);
2400 * Fetch instruction byte. Return -1 if invalid address.
2402 .globl EXT(inst_fetch)
2404 movl S_ARG1, %eax /* get segment */
2405 movw %ax,%fs /* into FS */
2406 movl S_ARG0, %eax /* get offset */
2408 RETRY(EXT(inst_fetch)) /* re-load FS on retry */
2410 RECOVER(EXT(inst_fetch_fault))
2411 movzbl %fs:(%eax),%eax /* load instruction byte */
2414 LEXT(inst_fetch_fault)
2415 movl $-1,%eax /* return -1 if error */
2421 * kdp_copy_kmem(char *src, char *dst, int count)
2423 * Similar to copyin except that both addresses are kernel addresses.
2426 ENTRY(kdp_copy_kmem)
2428 pushl %edi /* save registers */
2430 movl 8+S_ARG0,%esi /* get kernel start address */
2431 movl 8+S_ARG1,%edi /* get kernel destination address */
2433 movl 8+S_ARG2,%edx /* get count */
2435 lea 0(%esi,%edx),%eax /* get kernel end address + 1 */
2438 jb kdp_vm_read_fail /* fail if wrap-around */
2440 movl %edx,%ecx /* move by longwords first */
2443 RECOVER(kdp_vm_read_fail)
2445 movsl /* move longwords */
2446 movl %edx,%ecx /* now move remaining bytes */
2449 RECOVER(kdp_vm_read_fail)
2453 movl 8+S_ARG2,%edx /* get count */
2454 subl %ecx,%edx /* Return number of bytes transfered */
2457 popl %edi /* restore registers */
2459 ret /* and return */
2462 xorl %eax,%eax /* didn't copy a thing. */
2470 * int rdmsr_carefully(uint32_t msr, uint32_t *lo, uint32_t *hi)
2472 ENTRY(rdmsr_carefully)
2489 * Done with recovery and retry tables.
2502 /* dr<i>(address, type, len, persistence)
2506 movl %eax,EXT(dr_addr)
2512 movl %eax,EXT(dr_addr)+1*4
2518 movl %eax,EXT(dr_addr)+2*4
2525 movl %eax,EXT(dr_addr)+3*4
2534 movl %edx,EXT(dr_addr)+4*4
2535 andl dr_msk(,%ecx,2),%edx /* clear out new entry */
2536 movl %edx,EXT(dr_addr)+5*4
2555 movl %edx,EXT(dr_addr)+7*4
2603 lidt null_idtr /* disable the interrupt handler */
2604 xor %ecx,%ecx /* generate a divide by zero */
2605 div %ecx,%eax /* reboot now */
2606 ret /* this will "never" be executed */
2608 #endif /* SYMMETRY */
2612 * setbit(int bitno, int *s) - set bit in bit string
2615 movl S_ARG0, %ecx /* bit number */
2616 movl S_ARG1, %eax /* address */
2617 btsl %ecx, (%eax) /* set bit */
2621 * clrbit(int bitno, int *s) - clear bit in bit string
2624 movl S_ARG0, %ecx /* bit number */
2625 movl S_ARG1, %eax /* address */
2626 btrl %ecx, (%eax) /* clear bit */
2630 * ffsbit(int *s) - find first set bit in bit string
2633 movl S_ARG0, %ecx /* address */
2634 movl $0, %edx /* base offset */
2636 bsfl (%ecx), %eax /* check argument bits */
2637 jnz 1f /* found bit, return */
2638 addl $4, %ecx /* increment address */
2639 addl $32, %edx /* increment offset */
2640 jmp 0b /* try again */
2642 addl %edx, %eax /* return offset */
2646 * testbit(int nr, volatile void *array)
2648 * Test to see if the bit is set within the bit string
2652 movl S_ARG0,%eax /* Get the bit to test */
2653 movl S_ARG1,%ecx /* get the array string */
2665 movl 4(%ebp), %eax /* fetch pc of caller */
2668 ENTRY(tvals_to_etap)
2670 movl $1000000000, %ecx
2677 * etap_time_sub(etap_time_t stop, etap_time_t start)
2679 * 64bit subtract, returns stop - start
2681 ENTRY(etap_time_sub)
2682 movl S_ARG0, %eax /* stop.low */
2683 movl S_ARG1, %edx /* stop.hi */
2684 subl S_ARG2, %eax /* stop.lo - start.lo */
2685 sbbl S_ARG3, %edx /* stop.hi - start.hi */
2694 * jail: set the EIP to "jail" to block a kernel thread.
2695 * Useful to debug synchronization problems on MPs.
2702 * div_scale(unsigned int dividend,
2703 * unsigned int divisor,
2704 * unsigned int *scale)
2706 * This function returns (dividend << *scale) //divisor where *scale
2707 * is the largest possible value before overflow. This is used in
2708 * computation where precision must be achieved in order to avoid
2709 * floating point usage.
2713 * while (((dividend >> *scale) >= divisor))
2715 * *scale = 32 - *scale;
2716 * return ((dividend << *scale) / divisor);
2720 xorl %ecx, %ecx /* *scale = 0 */
2722 movl ARG0, %edx /* get dividend */
2724 cmpl ARG1, %edx /* if (divisor > dividend) */
2725 jle 1f /* goto 1f */
2726 addl $1, %ecx /* (*scale)++ */
2727 shrdl $1, %edx, %eax /* dividend >> 1 */
2728 shrl $1, %edx /* dividend >> 1 */
2729 jmp 0b /* goto 0b */
2731 divl ARG1 /* (dividend << (32 - *scale)) / divisor */
2732 movl ARG2, %edx /* get scale */
2733 movl $32, (%edx) /* *scale = 32 */
2734 subl %ecx, (%edx) /* *scale -= %ecx */
2740 * mul_scale(unsigned int multiplicand,
2741 * unsigned int multiplier,
2742 * unsigned int *scale)
2744 * This function returns ((multiplicand * multiplier) >> *scale) where
2745 * scale is the largest possible value before overflow. This is used in
2746 * computation where precision must be achieved in order to avoid
2747 * floating point usage.
2751 * while (overflow((multiplicand * multiplier) >> *scale))
2753 * return ((multiplicand * multiplier) >> *scale);
2757 xorl %ecx, %ecx /* *scale = 0 */
2758 movl ARG0, %eax /* get multiplicand */
2759 mull ARG1 /* multiplicand * multiplier */
2761 cmpl $0, %edx /* if (!overflow()) */
2763 addl $1, %ecx /* (*scale)++ */
2764 shrdl $1, %edx, %eax /* (multiplicand * multiplier) >> 1 */
2765 shrl $1, %edx /* (multiplicand * multiplier) >> 1 */
2768 movl ARG2, %edx /* get scale */
2769 movl %ecx, (%edx) /* set *scale */
2775 * BSD System call entry point..
2778 Entry(trap_unix_syscall)
2780 pushf /* save flags as soon as possible */
2782 pushl %eax /* save system call number */
2783 pushl $0 /* clear trap number slot */
2785 pusha /* save the general registers */
2786 pushl %ds /* and the segment registers */
2791 mov %ss,%dx /* switch to kernel data segment */
2794 mov $ CPU_DATA_GS,%dx
2798 * Shuffle eflags,eip,cs into proper places
2801 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
2802 movl R_CS(%esp),%ecx /* eip is in CS slot */
2803 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
2804 movl %ecx,R_EIP(%esp) /* fix eip */
2805 movl %edx,R_CS(%esp) /* fix cs */
2806 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
2810 negl %eax /* get system call number */
2811 shll $4,%eax /* manual indexing */
2813 movl %gs:CPU_KERNEL_STACK,%ebx
2814 /* get current kernel stack */
2815 xchgl %ebx,%esp /* switch stacks - %ebx points to */
2816 /* user registers. */
2819 * Register use on entry:
2820 * eax contains syscall number
2821 * ebx contains user regs pointer
2824 pushl %ebx /* Push the regs set onto stack */
2825 call EXT(unix_syscall)
2827 movl %esp,%ecx /* get kernel stack */
2828 or $(KERNEL_STACK_SIZE-1),%ecx
2829 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
2830 movl %eax,R_EAX(%esp) /* save return value */
2831 jmp EXT(return_from_trap) /* return to user */
2834 * Entry point for machdep system calls..
2837 Entry(trap_machdep_syscall)
2838 pushf /* save flags as soon as possible */
2839 pushl %eax /* save system call number */
2840 pushl $0 /* clear trap number slot */
2842 pusha /* save the general registers */
2843 pushl %ds /* and the segment registers */
2848 mov %ss,%dx /* switch to kernel data segment */
2851 mov $ CPU_DATA_GS,%dx
2855 * Shuffle eflags,eip,cs into proper places
2858 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
2859 movl R_CS(%esp),%ecx /* eip is in CS slot */
2860 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
2861 movl %ecx,R_EIP(%esp) /* fix eip */
2862 movl %edx,R_CS(%esp) /* fix cs */
2863 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
2867 negl %eax /* get system call number */
2868 shll $4,%eax /* manual indexing */
2870 movl %gs:CPU_KERNEL_STACK,%ebx
2871 /* get current kernel stack */
2872 xchgl %ebx,%esp /* switch stacks - %ebx points to */
2873 /* user registers. */
2876 * Register use on entry:
2877 * eax contains syscall number
2878 * ebx contains user regs pointer
2882 call EXT(machdep_syscall)
2884 movl %esp,%ecx /* get kernel stack */
2885 or $(KERNEL_STACK_SIZE-1),%ecx
2886 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
2887 movl %eax,R_EAX(%esp) /* save return value */
2888 jmp EXT(return_from_trap) /* return to user */
2890 Entry(trap_mach25_syscall)
2891 pushf /* save flags as soon as possible */
2892 pushl %eax /* save system call number */
2893 pushl $0 /* clear trap number slot */
2895 pusha /* save the general registers */
2896 pushl %ds /* and the segment registers */
2901 mov %ss,%dx /* switch to kernel data segment */
2904 mov $ CPU_DATA_GS,%dx
2908 * Shuffle eflags,eip,cs into proper places
2911 movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
2912 movl R_CS(%esp),%ecx /* eip is in CS slot */
2913 movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
2914 movl %ecx,R_EIP(%esp) /* fix eip */
2915 movl %edx,R_CS(%esp) /* fix cs */
2916 movl %ebx,R_EFLAGS(%esp) /* fix eflags */
2920 negl %eax /* get system call number */
2921 shll $4,%eax /* manual indexing */
2923 movl %gs:CPU_KERNEL_STACK,%ebx
2924 /* get current kernel stack */
2925 xchgl %ebx,%esp /* switch stacks - %ebx points to */
2926 /* user registers. */
2929 * Register use on entry:
2930 * eax contains syscall number
2931 * ebx contains user regs pointer
2935 call EXT(mach25_syscall)
2937 movl %esp,%ecx /* get kernel stack */
2938 or $(KERNEL_STACK_SIZE-1),%ecx
2939 movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
2940 movl %eax,R_EAX(%esp) /* save return value */
2941 jmp EXT(return_from_trap) /* return to user */