2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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>
74 #define _ARCH_I386_ASM_HELP_H_ /* Prevent inclusion of user header */
75 #include <mach/i386/syscall_sw.h>
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)
112 * The following macros make calls into C code.
113 * They dynamically align the stack to 16 bytes.
114 * Arguments are moved (not pushed) onto the correctly aligned stack.
115 * NOTE: EDI is destroyed in the process, and hence cannot
116 * be directly used as a parameter. Users of this macro must
117 * independently preserve EDI (a non-volatile) if the routine is
118 * intended to be called from C, for instance.
123 andl $0xFFFFFFF0, %esp ;\
127 #define CCALL1(fn, arg1) \
130 andl $0xFFFFFFF0, %esp ;\
131 movl arg1, 0(%esp) ;\
135 #define CCALL2(fn, arg1, arg2) \
138 andl $0xFFFFFFF0, %esp ;\
139 movl arg2, 4(%esp) ;\
140 movl arg1, 0(%esp) ;\
145 * CCALL5 is used for callee functions with 3 arguments but
146 * where arg2 (a3:a2) and arg3 (a5:a4) are 64-bit values.
148 #define CCALL5(fn, a1, a2, a3, a4, a5) \
151 andl $0xFFFFFFF0, %esp ;\
168 #define RECOVERY_SECTION .section __VECTORS, __recover
170 #define RECOVERY_SECTION .text
171 #define RECOVERY_SECTION .text
174 #define RECOVER_TABLE_START \
176 .globl EXT(recover_table) ;\
177 LEXT(recover_table) ;\
180 #define RECOVER(addr) \
187 #define RECOVER_TABLE_END \
189 .globl EXT(recover_table_end) ;\
190 LEXT(recover_table_end) ;\
194 * Allocate recovery and table.
206 movl %eax,TIMER_HIGHCHK(%ecx)
207 movl %edx,TIMER_LOW(%ecx)
208 movl %eax,TIMER_HIGH(%ecx)
213 0: movl TIMER_HIGH(%ecx),%edx
214 movl TIMER_LOW(%ecx),%eax
215 cmpl TIMER_HIGHCHK(%ecx),%edx
221 #define TIME_TRAP_UENTRY
222 #define TIME_TRAP_UEXIT
223 #define TIME_INT_ENTRY
224 #define TIME_INT_EXIT
232 * Nanotime returned in %edx:%eax.
233 * Computed from tsc based on the scale factor
234 * and an implicit 32 bit shift.
235 * This code must match what _rtc_nanotime_read does in
236 * i386/machine_routines_asm.s. Failure to do so can
237 * result in "weird" timing results.
239 * Uses %eax, %ebx, %ecx, %edx, %esi, %edi.
241 #define RNT_INFO _rtc_nanotime_info
243 lea RNT_INFO,%edi ; \
245 movl RNT_GENERATION(%edi),%esi /* being updated? */ ; \
247 jz 0b /* wait until done */ ; \
249 subl RNT_TSC_BASE(%edi),%eax ; \
250 sbbl RNT_TSC_BASE+4(%edi),%edx /* tsc - tsc_base */ ; \
251 movl RNT_SCALE(%edi),%ecx /* * scale factor */ ; \
259 addl RNT_NS_BASE(%edi),%eax /* + ns_base */ ; \
260 adcl RNT_NS_BASE+4(%edi),%edx ; \
261 cmpl RNT_GENERATION(%edi),%esi /* check for update */ ; \
262 jne 0b /* do it all again */
266 * Add 64-bit delta in register dreg : areg to timer pointed to by register treg.
268 #define TIMER_UPDATE(treg,dreg,areg) \
269 addl TIMER_LOW(treg),areg /* add low bits */ ; \
270 adcl dreg,TIMER_HIGH(treg) /* add carry high bits */ ; \
271 movl areg,TIMER_LOW(treg) /* store updated low bit */ ; \
272 movl TIMER_HIGH(treg),dreg /* copy high bits */ ; \
273 movl dreg,TIMER_HIGHCHK(treg) /* to high check */
276 * Add time delta to old timer and start new.
278 #define TIMER_EVENT(old,new) \
279 NANOTIME /* edx:eax nanosecs */ ; \
280 movl %eax,%esi /* save timestamp */ ; \
281 movl %edx,%edi /* save timestamp */ ; \
282 movl %gs:CPU_PROCESSOR,%ebx /* get current processor */ ; \
283 movl THREAD_TIMER(%ebx),%ecx /* get current timer */ ; \
284 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ; \
285 sbbl TIMER_TSTAMP+4(%ecx),%edx /* compute elapsed time */ ; \
286 TIMER_UPDATE(%ecx,%edx,%eax) /* update timer */ ; \
287 addl $(new##_TIMER-old##_TIMER),%ecx /* point to new timer */ ; \
288 movl %esi,TIMER_TSTAMP(%ecx) /* set timestamp */ ; \
289 movl %edi,TIMER_TSTAMP+4(%ecx) /* set timestamp */ ; \
290 movl %ecx,THREAD_TIMER(%ebx) /* set current timer */ ; \
291 movl %esi,%eax /* restore timestamp */ ; \
292 movl %edi,%edx /* restore timestamp */ ; \
293 movl CURRENT_STATE(%ebx),%ecx /* current state */ ; \
294 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ; \
295 sbbl TIMER_TSTAMP+4(%ecx),%edx /* compute elapsed time */ ; \
296 TIMER_UPDATE(%ecx,%edx,%eax) /* update timer */ ; \
297 addl $(new##_STATE-old##_STATE),%ecx /* point to new state */ ; \
298 movl %ecx,CURRENT_STATE(%ebx) /* set current state */ ; \
299 movl %esi,TIMER_TSTAMP(%ecx) /* set timestamp */ ; \
300 movl %edi,TIMER_TSTAMP+4(%ecx) /* set timestamp */
303 * Update time on user trap entry.
304 * Uses %eax,%ebx,%ecx,%edx,%esi,%edi.
306 #define TIME_TRAP_UENTRY TIMER_EVENT(USER,SYSTEM)
309 * update time on user trap exit.
310 * Uses %eax,%ebx,%ecx,%edx,%esi,%edi.
312 #define TIME_TRAP_UEXIT TIMER_EVENT(SYSTEM,USER)
315 * update time on interrupt entry.
316 * Uses %eax,%ebx,%ecx,%edx,%esi,%edi.
317 * Saves processor state info on stack.
319 #define TIME_INT_ENTRY \
320 NANOTIME /* edx:eax nanosecs */ ; \
321 movl %eax,%gs:CPU_INT_EVENT_TIME /* save in cpu data */ ; \
322 movl %edx,%gs:CPU_INT_EVENT_TIME+4 /* save in cpu data */ ; \
323 movl %eax,%esi /* save timestamp */ ; \
324 movl %edx,%edi /* save timestamp */ ; \
325 movl %gs:CPU_PROCESSOR,%ebx /* get current processor */ ; \
326 movl THREAD_TIMER(%ebx),%ecx /* get current timer */ ; \
327 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ; \
328 sbbl TIMER_TSTAMP+4(%ecx),%edx /* compute elapsed time */ ; \
329 TIMER_UPDATE(%ecx,%edx,%eax) /* update timer */ ; \
330 movl KERNEL_TIMER(%ebx),%ecx /* point to kernel timer */ ; \
331 movl %esi,TIMER_TSTAMP(%ecx) /* set timestamp */ ; \
332 movl %edi,TIMER_TSTAMP+4(%ecx) /* set timestamp */ ; \
333 movl %esi,%eax /* restore timestamp */ ; \
334 movl %edi,%edx /* restore timestamp */ ; \
335 movl CURRENT_STATE(%ebx),%ecx /* get current state */ ; \
336 pushl %ecx /* save state */ ; \
337 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ; \
338 sbbl TIMER_TSTAMP+4(%ecx),%edx /* compute elapsed time */ ; \
339 TIMER_UPDATE(%ecx,%edx,%eax) /* update timer */ ; \
340 leal IDLE_STATE(%ebx),%eax /* get idle state */ ; \
341 cmpl %eax,%ecx /* compare current state */ ; \
342 je 0f /* skip if equal */ ; \
343 leal SYSTEM_STATE(%ebx),%ecx /* get system state */ ; \
344 movl %ecx,CURRENT_STATE(%ebx) /* set current state */ ; \
345 0: movl %esi,TIMER_TSTAMP(%ecx) /* set timestamp */ ; \
346 movl %edi,TIMER_TSTAMP+4(%ecx) /* set timestamp */
349 * update time on interrupt exit.
350 * Uses %eax,%ebx,%ecx,%edx,%esi,%edi.
351 * Restores processor state info from stack.
353 #define TIME_INT_EXIT \
354 NANOTIME /* edx:eax nanosecs */ ; \
355 movl %eax,%gs:CPU_INT_EVENT_TIME /* save in cpu data */ ; \
356 movl %edx,%gs:CPU_INT_EVENT_TIME+4 /* save in cpu data */ ; \
357 movl %eax,%esi /* save timestamp */ ; \
358 movl %edx,%edi /* save timestamp */ ; \
359 movl %gs:CPU_PROCESSOR,%ebx /* get current processor */ ; \
360 movl KERNEL_TIMER(%ebx),%ecx /* point to kernel timer */ ; \
361 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ; \
362 sbbl TIMER_TSTAMP+4(%ecx),%edx /* compute elapsed time */ ; \
363 TIMER_UPDATE(%ecx,%edx,%eax) /* update timer */ ; \
364 movl THREAD_TIMER(%ebx),%ecx /* interrupted timer */ ; \
365 movl %esi,TIMER_TSTAMP(%ecx) /* set timestamp */ ; \
366 movl %edi,TIMER_TSTAMP+4(%ecx) /* set timestamp */ ; \
367 movl %esi,%eax /* restore timestamp */ ; \
368 movl %edi,%edx /* restore timestamp */ ; \
369 movl CURRENT_STATE(%ebx),%ecx /* get current state */ ; \
370 subl TIMER_TSTAMP(%ecx),%eax /* compute elapsed time */ ; \
371 sbbl TIMER_TSTAMP+4(%ecx),%edx /* compute elapsed time */ ; \
372 TIMER_UPDATE(%ecx,%edx,%eax) /* update timer */ ; \
373 popl %ecx /* restore state */ ; \
374 movl %ecx,CURRENT_STATE(%ebx) /* set current state */ ; \
375 movl %esi,TIMER_TSTAMP(%ecx) /* set timestamp */ ; \
376 movl %edi,TIMER_TSTAMP+4(%ecx) /* set timestamp */
378 #endif /* STAT_TIME */
385 * Traditional, not ANSI.
389 .globl label/**/count ;\
392 .globl label/**/limit ;\
396 addl $1,%ss:label/**/count ;\
397 cmpl $0,label/**/limit ;\
401 movl %ss:label/**/count,%eax ;\
402 cmpl %eax,%ss:label/**/limit ;\
415 * Last-ditch debug code to handle faults that might result
416 * from entering kernel (from collocated server) on an invalid
417 * stack. On collocated entry, there's no hardware-initiated
418 * stack switch, so a valid stack must be in place when an
419 * exception occurs, or we may double-fault.
421 * In case of a double-fault, our only recourse is to switch
422 * hardware "tasks", so that we avoid using the current stack.
424 * The idea here is just to get the processor into the debugger,
425 * post-haste. No attempt is made to fix up whatever error got
426 * us here, so presumably continuing from the debugger will
427 * simply land us here again -- at best.
431 * Note that the per-fault entry points are not currently
432 * functional. The only way to make them work would be to
433 * set up separate TSS's for each fault type, which doesn't
434 * currently seem worthwhile. (The offset part of a task
435 * gate is always ignored.) So all faults that task switch
436 * currently resume at db_task_start.
439 * Double fault (Murphy's point) - error code (0) on stack
441 Entry(db_task_dbl_fault)
443 movl $(T_DOUBLE_FAULT),%ebx
446 * Segment not present - error code on stack
448 Entry(db_task_seg_np)
450 movl $(T_SEGMENT_NOT_PRESENT),%ebx
453 * Stack fault - error code on (current) stack
455 Entry(db_task_stk_fault)
457 movl $(T_STACK_FAULT),%ebx
460 * General protection fault - error code on stack
462 Entry(db_task_gen_prot)
464 movl $(T_GENERAL_PROTECTION),%ebx
468 * The entry point where execution resumes after last-ditch debugger task
473 subl $(ISS32_SIZE),%edx
474 movl %edx,%esp /* allocate x86_saved_state on stack */
475 movl %eax,R_ERR(%esp)
476 movl %ebx,R_TRAPNO(%esp)
479 movl CX(EXT(master_dbtss),%edx),%edx
480 movl TSS_LINK(%edx),%eax
481 pushl %eax /* pass along selector of previous TSS */
482 call EXT(db_tss_to_frame)
483 popl %eax /* get rid of TSS selector */
484 call EXT(db_trap_from_asm)
489 iret /* ha, ha, ha... */
490 #endif /* MACH_KDB */
493 * Called as a function, makes the current thread
494 * return from the kernel as if from an exception.
497 .globl EXT(thread_exception_return)
498 .globl EXT(thread_bootstrap_return)
499 LEXT(thread_exception_return)
500 LEXT(thread_bootstrap_return)
502 movl %gs:CPU_KERNEL_STACK,%ecx
503 movl (%ecx),%esp /* switch back to PCB stack */
504 jmp EXT(return_from_trap)
506 Entry(call_continuation)
507 movl S_ARG0,%eax /* get continuation */
508 movl S_ARG1,%edx /* continuation param */
509 movl S_ARG2,%ecx /* wait result */
510 movl %gs:CPU_KERNEL_STACK,%esp /* pop the stack */
511 xorl %ebp,%ebp /* zero frame pointer */
512 subl $8,%esp /* align the stack */
515 call *%eax /* call continuation */
517 movl %gs:CPU_ACTIVE_THREAD,%eax
519 call EXT(thread_terminate)
523 /*******************************************************************************************************
525 * All 64 bit task 'exceptions' enter lo_alltraps:
526 * esp -> x86_saved_state_t
528 * The rest of the state is set up as:
529 * cr3 -> kernel directory
530 * esp -> low based stack
533 * ss/ds/es -> KERNEL_DS
535 * interrupts disabled
536 * direction flag cleared
539 movl R_CS(%esp),%eax /* assume 32-bit state */
540 cmpl $(SS_64),SS_FLAVOR(%esp)/* 64-bit? */
542 movl R64_CS(%esp),%eax /* 64-bit user mode */
549 movl %gs:CPU_ACTIVE_THREAD,%ecx
550 movl ACT_TASK(%ecx),%ebx
552 /* Check for active vtimers in the current task */
553 cmpl $0,TASK_VTIMERS(%ebx)
556 /* Set a pending AST */
557 orl $(AST_BSD),%gs:CPU_PENDING_AST
559 /* Set a thread AST (atomic) */
561 orl $(AST_BSD),ACT_AST(%ecx)
564 movl %gs:CPU_KERNEL_STACK,%ebx
565 xchgl %ebx,%esp /* switch to kernel stack */
568 CCALL1(user_trap, %ebx) /* call user trap routine */
569 cli /* hold off intrs - critical section */
570 popl %esp /* switch back to PCB stack */
573 * Return from trap or system call, checking for ASTs.
574 * On lowbase PCB stack with intrs disabled
576 LEXT(return_from_trap)
577 movl %gs:CPU_PENDING_AST,%eax
579 je EXT(return_to_user) /* branch if no AST */
581 movl %gs:CPU_KERNEL_STACK,%ebx
582 xchgl %ebx,%esp /* switch to kernel stack */
583 sti /* interrupts always enabled on return to user mode */
585 pushl %ebx /* save PCB stack */
586 xorl %ebp,%ebp /* Clear framepointer */
587 CCALL1(i386_astintr, $0) /* take the AST */
589 popl %esp /* switch back to PCB stack (w/exc link) */
590 jmp EXT(return_from_trap) /* and check again (rare) */
596 cmpl $0, %gs:CPU_IS64BIT
597 je EXT(lo_ret_to_user)
598 jmp EXT(lo64_ret_to_user)
603 * Trap from kernel mode. No need to switch stacks.
604 * Interrupts must be off here - we will set them to state at time of trap
605 * as soon as it's safe for us to do so and not recurse doing preemption
608 movl %esp, %eax /* saved state addr */
609 pushl R_EIP(%esp) /* Simulate a CALL from fault point */
610 pushl %ebp /* Extend framepointer chain */
612 CCALL1(kernel_trap, %eax) /* Call kernel trap handler */
617 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
618 testl $ AST_URGENT,%eax /* any urgent preemption? */
619 je ret_to_kernel /* no, nothing to do */
620 cmpl $ T_PREEMPT,R_TRAPNO(%esp)
621 je ret_to_kernel /* T_PREEMPT handled in kernel_trap() */
622 testl $ EFL_IF,R_EFLAGS(%esp) /* interrupts disabled? */
624 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
626 movl %gs:CPU_KERNEL_STACK,%eax
629 andl $(-KERNEL_STACK_SIZE),%ecx
630 testl %ecx,%ecx /* are we on the kernel stack? */
631 jne ret_to_kernel /* no, skip it */
633 CCALL1(i386_astintr, $1) /* take the AST */
636 cmpl $0, %gs:CPU_IS64BIT
637 je EXT(lo_ret_to_kernel)
638 jmp EXT(lo64_ret_to_kernel)
642 /*******************************************************************************************************
644 * All interrupts on all tasks enter here with:
645 * esp-> -> x86_saved_state_t
647 * cr3 -> kernel directory
648 * esp -> low based stack
651 * ss/ds/es -> KERNEL_DS
653 * interrupts disabled
654 * direction flag cleared
658 * test whether already on interrupt stack
660 movl %gs:CPU_INT_STACK_TOP,%ecx
663 leal -INTSTACK_SIZE(%ecx),%edx
667 xchgl %ecx,%esp /* switch to interrupt stack */
669 movl %cr0,%eax /* get cr0 */
670 orl $(CR0_TS),%eax /* or in TS bit */
671 movl %eax,%cr0 /* set cr0 */
673 subl $8, %esp /* for 16-byte stack alignment */
674 pushl %ecx /* save pointer to old stack */
675 movl %ecx,%gs:CPU_INT_STATE /* save intr state */
677 TIME_INT_ENTRY /* do timing */
679 movl %gs:CPU_ACTIVE_THREAD,%ecx
680 movl ACT_TASK(%ecx),%ebx
682 /* Check for active vtimers in the current task */
683 cmpl $0,TASK_VTIMERS(%ebx)
686 /* Set a pending AST */
687 orl $(AST_BSD),%gs:CPU_PENDING_AST
689 /* Set a thread AST (atomic) */
691 orl $(AST_BSD),ACT_AST(%ecx)
694 incl %gs:CPU_PREEMPTION_LEVEL
695 incl %gs:CPU_INTERRUPT_LEVEL
697 movl %gs:CPU_INT_STATE, %eax
698 CCALL1(PE_incoming_interrupt, %eax) /* call generic interrupt routine */
700 cli /* just in case we returned with intrs enabled */
702 movl %eax,%gs:CPU_INT_STATE /* clear intr state pointer */
704 decl %gs:CPU_INTERRUPT_LEVEL
705 decl %gs:CPU_PREEMPTION_LEVEL
707 TIME_INT_EXIT /* do timing */
709 movl %gs:CPU_ACTIVE_THREAD,%eax
710 movl ACT_PCB(%eax),%eax /* get act`s PCB */
711 movl PCB_FPS(%eax),%eax /* get pcb's ims.ifps */
712 cmpl $0,%eax /* Is there a context */
713 je 1f /* Branch if not */
714 movl FP_VALID(%eax),%eax /* Load fp_valid */
715 cmpl $0,%eax /* Check if valid */
716 jne 1f /* Branch if valid */
720 movl %cr0,%eax /* get cr0 */
721 orl $(CR0_TS),%eax /* or in TS bit */
722 movl %eax,%cr0 /* set cr0 */
724 popl %esp /* switch back to old stack */
726 /* Load interrupted code segment into %eax */
727 movl R_CS(%esp),%eax /* assume 32-bit state */
728 cmpl $(SS_64),SS_FLAVOR(%esp)/* 64-bit? */
730 movl R64_CS(%esp),%eax /* 64-bit user mode */
732 testb $3,%al /* user mode, */
733 jnz ast_from_interrupt_user /* go handle potential ASTs */
735 * we only want to handle preemption requests if
736 * the interrupt fell in the kernel context
737 * and preemption isn't disabled
739 movl %gs:CPU_PENDING_AST,%eax
740 testl $ AST_URGENT,%eax /* any urgent requests? */
741 je ret_to_kernel /* no, nothing to do */
743 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
744 jne ret_to_kernel /* yes, skip it */
746 movl %gs:CPU_KERNEL_STACK,%eax
749 andl $(-KERNEL_STACK_SIZE),%ecx
750 testl %ecx,%ecx /* are we on the kernel stack? */
751 jne ret_to_kernel /* no, skip it */
754 * Take an AST from kernel space. We don't need (and don't want)
755 * to do as much as the case where the interrupt came from user
758 CCALL1(i386_astintr, $1)
764 * nested int - simple path, can't preempt etc on way out
767 incl %gs:CPU_PREEMPTION_LEVEL
768 incl %gs:CPU_INTERRUPT_LEVEL
770 movl %esp, %edx /* x86_saved_state */
771 CCALL1(PE_incoming_interrupt, %edx)
773 decl %gs:CPU_INTERRUPT_LEVEL
774 decl %gs:CPU_PREEMPTION_LEVEL
779 * Take an AST from an interrupted user
781 ast_from_interrupt_user:
782 movl %gs:CPU_PENDING_AST,%eax
783 testl %eax,%eax /* pending ASTs? */
784 je EXT(ret_to_user) /* no, nothing to do */
788 jmp EXT(return_from_trap) /* return */
791 /*******************************************************************************************************
794 * System call entries via INTR_GATE or sysenter:
796 * esp -> x86_saved_state32_t
797 * cr3 -> kernel directory
798 * esp -> low based stack
801 * ss/ds/es -> KERNEL_DS
803 * interrupts disabled
804 * direction flag cleared
809 * We can be here either for a mach syscall or a unix syscall,
810 * as indicated by the sign of the code:
812 movl R_EAX(%esp),%eax
814 js EXT(lo_mach_scall) /* < 0 => mach */
820 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
821 movl ACT_TASK(%ecx),%ebx /* point to current task */
822 addl $1,TASK_SYSCALLS_UNIX(%ebx) /* increment call count */
824 /* Check for active vtimers in the current task */
825 cmpl $0,TASK_VTIMERS(%ebx)
828 /* Set a pending AST */
829 orl $(AST_BSD),%gs:CPU_PENDING_AST
831 /* Set a thread AST (atomic) */
833 orl $(AST_BSD),ACT_AST(%ecx)
836 movl %gs:CPU_KERNEL_STACK,%ebx
837 xchgl %ebx,%esp /* switch to kernel stack */
841 CCALL1(unix_syscall, %ebx)
843 * always returns through thread_exception_return
850 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
851 movl ACT_TASK(%ecx),%ebx /* point to current task */
852 addl $1,TASK_SYSCALLS_MACH(%ebx) /* increment call count */
854 /* Check for active vtimers in the current task */
855 cmpl $0,TASK_VTIMERS(%ebx)
858 /* Set a pending AST */
859 orl $(AST_BSD),%gs:CPU_PENDING_AST
861 /* Set a thread AST (atomic) */
863 orl $(AST_BSD),ACT_AST(%ecx)
866 movl %gs:CPU_KERNEL_STACK,%ebx
867 xchgl %ebx,%esp /* switch to kernel stack */
871 CCALL1(mach_call_munger, %ebx)
873 * always returns through thread_exception_return
880 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
881 movl ACT_TASK(%ecx),%ebx /* point to current task */
883 /* Check for active vtimers in the current task */
884 cmpl $0,TASK_VTIMERS(%ebx)
887 /* Set a pending AST */
888 orl $(AST_BSD),%gs:CPU_PENDING_AST
890 /* Set a thread AST (atomic) */
892 orl $(AST_BSD),ACT_AST(%ecx)
895 movl %gs:CPU_KERNEL_STACK,%ebx
896 xchgl %ebx,%esp /* switch to kernel stack */
900 CCALL1(machdep_syscall, %ebx)
902 * always returns through thread_exception_return
909 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
910 movl ACT_TASK(%ecx),%ebx /* point to current task */
912 /* Check for active vtimers in the current task */
913 cmpl $0,TASK_VTIMERS(%ebx)
916 /* Set a pending AST */
917 orl $(AST_BSD),%gs:CPU_PENDING_AST
919 /* Set a thread AST (atomic) */
921 orl $(AST_BSD),ACT_AST(%ecx)
924 movl %gs:CPU_KERNEL_STACK,%ebx // Get the address of the kernel stack
925 xchgl %ebx,%esp // Switch to it, saving the previous
927 CCALL1(diagCall, %ebx) // Call diagnostics
929 cmpl $0,%eax // What kind of return is this?
931 cli // Disable interruptions just in case they were enabled
932 popl %esp // Get back the original stack
933 jmp EXT(return_to_user) // Normal return, do not check asts...
935 CCALL5(i386_exception, $EXC_SYSCALL, $0x6000, $0, $1, $0)
936 // pass what would be the diag syscall
937 // error return - cause an exception
942 /*******************************************************************************************************
945 * System call entries via syscall only:
947 * esp -> x86_saved_state64_t
948 * cr3 -> kernel directory
949 * esp -> low based stack
952 * ss/ds/es -> KERNEL_DS
954 * interrupts disabled
955 * direction flag cleared
962 * We can be here either for a mach, unix machdep or diag syscall,
963 * as indicated by the syscall class:
965 movl R64_RAX(%esp), %eax /* syscall number/class */
967 andl $(SYSCALL_CLASS_MASK), %ebx /* syscall class */
968 cmpl $(SYSCALL_CLASS_MACH<<SYSCALL_CLASS_SHIFT), %ebx
969 je EXT(lo64_mach_scall)
970 cmpl $(SYSCALL_CLASS_UNIX<<SYSCALL_CLASS_SHIFT), %ebx
971 je EXT(lo64_unix_scall)
972 cmpl $(SYSCALL_CLASS_MDEP<<SYSCALL_CLASS_SHIFT), %ebx
973 je EXT(lo64_mdep_scall)
974 cmpl $(SYSCALL_CLASS_DIAG<<SYSCALL_CLASS_SHIFT), %ebx
975 je EXT(lo64_diag_scall)
977 movl %gs:CPU_KERNEL_STACK,%ebx
978 xchgl %ebx,%esp /* switch to kernel stack */
982 /* Syscall class unknown */
983 CCALL5(i386_exception, $(EXC_SYSCALL), %eax, $0, $1, $0)
987 Entry(lo64_unix_scall)
988 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
989 movl ACT_TASK(%ecx),%ebx /* point to current task */
990 addl $1,TASK_SYSCALLS_UNIX(%ebx) /* increment call count */
992 /* Check for active vtimers in the current task */
993 cmpl $0,TASK_VTIMERS(%ebx)
996 /* Set a pending AST */
997 orl $(AST_BSD),%gs:CPU_PENDING_AST
999 /* Set a thread AST (atomic) */
1001 orl $(AST_BSD),ACT_AST(%ecx)
1004 movl %gs:CPU_KERNEL_STACK,%ebx
1005 xchgl %ebx,%esp /* switch to kernel stack */
1009 CCALL1(unix_syscall64, %ebx)
1011 * always returns through thread_exception_return
1015 Entry(lo64_mach_scall)
1016 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
1017 movl ACT_TASK(%ecx),%ebx /* point to current task */
1018 addl $1,TASK_SYSCALLS_MACH(%ebx) /* increment call count */
1020 /* Check for active vtimers in the current task */
1021 cmpl $0,TASK_VTIMERS(%ebx)
1024 /* Set a pending AST */
1025 orl $(AST_BSD),%gs:CPU_PENDING_AST
1028 orl $(AST_BSD),ACT_AST(%ecx)
1031 movl %gs:CPU_KERNEL_STACK,%ebx
1032 xchgl %ebx,%esp /* switch to kernel stack */
1036 CCALL1(mach_call_munger64, %ebx)
1038 * always returns through thread_exception_return
1043 Entry(lo64_mdep_scall)
1044 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
1045 movl ACT_TASK(%ecx),%ebx /* point to current task */
1047 /* Check for active vtimers in the current task */
1048 cmpl $0,TASK_VTIMERS(%ebx)
1051 /* Set a pending AST */
1052 orl $(AST_BSD),%gs:CPU_PENDING_AST
1054 /* Set a thread AST (atomic) */
1056 orl $(AST_BSD),ACT_AST(%ecx)
1059 movl %gs:CPU_KERNEL_STACK,%ebx
1060 xchgl %ebx,%esp /* switch to kernel stack */
1064 CCALL1(machdep_syscall64, %ebx)
1066 * always returns through thread_exception_return
1070 Entry(lo64_diag_scall)
1071 movl %gs:CPU_ACTIVE_THREAD,%ecx /* get current thread */
1072 movl ACT_TASK(%ecx),%ebx /* point to current task */
1074 /* Check for active vtimers in the current task */
1075 cmpl $0,TASK_VTIMERS(%ebx)
1078 /* Set a pending AST */
1079 orl $(AST_BSD),%gs:CPU_PENDING_AST
1081 /* Set a thread AST (atomic) */
1083 orl $(AST_BSD),ACT_AST(%ecx)
1086 movl %gs:CPU_KERNEL_STACK,%ebx // Get the address of the kernel stack
1087 xchgl %ebx,%esp // Switch to it, saving the previous
1089 CCALL1(diagCall64, %ebx) // Call diagnostics
1091 cmpl $0,%eax // What kind of return is this?
1093 cli // Disable interruptions just in case they were enabled
1094 popl %esp // Get back the original stack
1095 jmp EXT(return_to_user) // Normal return, do not check asts...
1097 CCALL5(i386_exception, $EXC_SYSCALL, $0x6000, $0, $1, $0)
1098 // pass what would be the diag syscall
1099 // error return - cause an exception
1109 * Copy from user/kernel address space.
1110 * arg0: window offset or kernel address
1111 * arg1: kernel address
1114 Entry(copyinphys_user)
1115 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
1118 Entry(copyinphys_kern)
1119 movl $(PHYS_WINDOW_SEL),%ecx /* physical access through kernel window */
1124 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
1131 pushl %edi /* save registers */
1133 movl 8+S_ARG0,%esi /* get source - window offset or kernel address */
1134 movl 8+S_ARG1,%edi /* get destination - kernel address */
1135 movl 8+S_ARG2,%edx /* get count */
1138 movl %edx,%ecx /* move by longwords first */
1141 RECOVER(copyin_fail)
1143 movsl /* move longwords */
1144 movl %edx,%ecx /* now move remaining bytes */
1147 RECOVER(copyin_fail)
1150 xorl %eax,%eax /* return 0 for success */
1152 mov %ss,%cx /* restore kernel data and extended segments */
1156 popl %edi /* restore registers */
1158 ret /* and return */
1161 movl $(EFAULT),%eax /* return error for failure */
1162 jmp copyin_ret /* pop frame and return */
1167 * Copy string from user/kern address space.
1168 * arg0: window offset or kernel address
1169 * arg1: kernel address
1170 * arg2: max byte count
1171 * arg3: actual byte count (OUT)
1173 Entry(copyinstr_kern)
1175 jmp copyinstr_common
1177 Entry(copyinstr_user)
1178 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
1184 pushl %edi /* save registers */
1186 movl 8+S_ARG0,%esi /* get source - window offset or kernel address */
1187 movl 8+S_ARG1,%edi /* get destination - kernel address */
1188 movl 8+S_ARG2,%edx /* get count */
1190 xorl %eax,%eax /* set to 0 here so that the high 24 bits */
1191 /* are 0 for the cmpl against 0 */
1194 RECOVER(copystr_fail) /* copy bytes... */
1197 testl %edi,%edi /* if kernel address is ... */
1198 jz 3f /* not NULL */
1199 movb %al,(%edi) /* copy the byte */
1202 testl %eax,%eax /* did we just stuff the 0-byte? */
1203 jz 4f /* yes, return 0 status already in %eax */
1204 decl %edx /* decrement #bytes left in buffer */
1205 jnz 2b /* buffer not full so copy in another byte */
1206 movl $(ENAMETOOLONG),%eax /* buffer full but no 0-byte: ENAMETOOLONG */
1208 movl 8+S_ARG3,%edi /* get OUT len ptr */
1210 jz copystr_ret /* if null, just return */
1212 movl %esi,(%edi) /* else set OUT arg to xfer len */
1214 popl %edi /* restore registers */
1216 ret /* and return */
1219 movl $(EFAULT),%eax /* return error for failure */
1220 jmp copystr_ret /* pop frame and return */
1224 * Copy to user/kern address space.
1225 * arg0: kernel address
1226 * arg1: window offset or kernel address
1229 ENTRY(copyoutphys_user)
1230 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
1233 ENTRY(copyoutphys_kern)
1234 movl $(PHYS_WINDOW_SEL),%ecx /* physical access through kernel window */
1239 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
1246 pushl %edi /* save registers */
1248 movl 8+S_ARG0,%esi /* get source - kernel address */
1249 movl 8+S_ARG1,%edi /* get destination - window offset or kernel address */
1250 movl 8+S_ARG2,%edx /* get count */
1253 movl %edx,%ecx /* move by longwords first */
1256 RECOVER(copyout_fail)
1259 movl %edx,%ecx /* now move remaining bytes */
1262 RECOVER(copyout_fail)
1265 xorl %eax,%eax /* return 0 for success */
1267 mov %ss,%cx /* restore kernel segment */
1271 popl %edi /* restore registers */
1273 ret /* and return */
1276 movl $(EFAULT),%eax /* return error for failure */
1277 jmp copyout_ret /* pop frame and return */
1280 * io register must not be used on slaves (no AT bus)
1282 #define ILL_ON_SLAVE
1290 #define PUSH_FRAME FRAME
1291 #define POP_FRAME EMARF
1293 #else /* MACH_ASSERT */
1301 #endif /* MACH_ASSERT */
1304 #if MACH_KDB || MACH_ASSERT
1307 * Following routines are also defined as macros in i386/pio.h
1308 * Compile then when MACH_KDB is configured so that they
1309 * can be invoked from the debugger.
1313 * void outb(unsigned char *io_port,
1314 * unsigned char byte)
1316 * Output a byte to an IO port.
1321 movl ARG0,%edx /* IO port address */
1322 movl ARG1,%eax /* data to output */
1323 outb %al,%dx /* send it out */
1328 * unsigned char inb(unsigned char *io_port)
1330 * Input a byte from an IO port.
1335 movl ARG0,%edx /* IO port address */
1336 xor %eax,%eax /* clear high bits of register */
1337 inb %dx,%al /* get the byte */
1342 * void outw(unsigned short *io_port,
1343 * unsigned short word)
1345 * Output a word to an IO port.
1350 movl ARG0,%edx /* IO port address */
1351 movl ARG1,%eax /* data to output */
1352 outw %ax,%dx /* send it out */
1357 * unsigned short inw(unsigned short *io_port)
1359 * Input a word from an IO port.
1364 movl ARG0,%edx /* IO port address */
1365 xor %eax,%eax /* clear high bits of register */
1366 inw %dx,%ax /* get the word */
1371 * void outl(unsigned int *io_port,
1372 * unsigned int byte)
1374 * Output an int to an IO port.
1379 movl ARG0,%edx /* IO port address*/
1380 movl ARG1,%eax /* data to output */
1381 outl %eax,%dx /* send it out */
1386 * unsigned int inl(unsigned int *io_port)
1388 * Input an int from an IO port.
1393 movl ARG0,%edx /* IO port address */
1394 inl %dx,%eax /* get the int */
1398 #endif /* MACH_KDB || MACH_ASSERT*/
1401 * void loutb(unsigned byte *io_port,
1402 * unsigned byte *data,
1403 * unsigned int count)
1405 * Output an array of bytes to an IO port.
1411 movl %esi,%eax /* save register */
1412 movl ARG0,%edx /* get io port number */
1413 movl ARG1,%esi /* get data address */
1414 movl ARG2,%ecx /* get count */
1418 movl %eax,%esi /* restore register */
1424 * void loutw(unsigned short *io_port,
1425 * unsigned short *data,
1426 * unsigned int count)
1428 * Output an array of shorts to an IO port.
1434 movl %esi,%eax /* save register */
1435 movl ARG0,%edx /* get io port number */
1436 movl ARG1,%esi /* get data address */
1437 movl ARG2,%ecx /* get count */
1441 movl %eax,%esi /* restore register */
1446 * void loutw(unsigned short io_port,
1447 * unsigned int *data,
1448 * unsigned int count)
1450 * Output an array of longs to an IO port.
1456 movl %esi,%eax /* save register */
1457 movl ARG0,%edx /* get io port number */
1458 movl ARG1,%esi /* get data address */
1459 movl ARG2,%ecx /* get count */
1463 movl %eax,%esi /* restore register */
1469 * void linb(unsigned char *io_port,
1470 * unsigned char *data,
1471 * unsigned int count)
1473 * Input an array of bytes from an IO port.
1479 movl %edi,%eax /* save register */
1480 movl ARG0,%edx /* get io port number */
1481 movl ARG1,%edi /* get data address */
1482 movl ARG2,%ecx /* get count */
1486 movl %eax,%edi /* restore register */
1492 * void linw(unsigned short *io_port,
1493 * unsigned short *data,
1494 * unsigned int count)
1496 * Input an array of shorts from an IO port.
1502 movl %edi,%eax /* save register */
1503 movl ARG0,%edx /* get io port number */
1504 movl ARG1,%edi /* get data address */
1505 movl ARG2,%ecx /* get count */
1509 movl %eax,%edi /* restore register */
1515 * void linl(unsigned short io_port,
1516 * unsigned int *data,
1517 * unsigned int count)
1519 * Input an array of longs from an IO port.
1525 movl %edi,%eax /* save register */
1526 movl ARG0,%edx /* get io port number */
1527 movl ARG1,%edi /* get data address */
1528 movl ARG2,%ecx /* get count */
1532 movl %eax,%edi /* restore register */
1537 * int rdmsr_carefully(uint32_t msr, uint32_t *lo, uint32_t *hi)
1539 ENTRY(rdmsr_carefully)
1556 * Done with recovery table.
1596 lidt null_idtr /* disable the interrupt handler */
1597 xor %ecx,%ecx /* generate a divide by zero */
1598 div %ecx,%eax /* reboot now */
1599 ret /* this will "never" be executed */
1601 #endif /* SYMMETRY */
1605 * setbit(int bitno, int *s) - set bit in bit string
1608 movl S_ARG0, %ecx /* bit number */
1609 movl S_ARG1, %eax /* address */
1610 btsl %ecx, (%eax) /* set bit */
1614 * clrbit(int bitno, int *s) - clear bit in bit string
1617 movl S_ARG0, %ecx /* bit number */
1618 movl S_ARG1, %eax /* address */
1619 btrl %ecx, (%eax) /* clear bit */
1623 * ffsbit(int *s) - find first set bit in bit string
1626 movl S_ARG0, %ecx /* address */
1627 movl $0, %edx /* base offset */
1629 bsfl (%ecx), %eax /* check argument bits */
1630 jnz 1f /* found bit, return */
1631 addl $4, %ecx /* increment address */
1632 addl $32, %edx /* increment offset */
1633 jmp 0b /* try again */
1635 addl %edx, %eax /* return offset */
1639 * testbit(int nr, volatile void *array)
1641 * Test to see if the bit is set within the bit string
1645 movl S_ARG0,%eax /* Get the bit to test */
1646 movl S_ARG1,%ecx /* get the array string */
1659 * jail: set the EIP to "jail" to block a kernel thread.
1660 * Useful to debug synchronization problems on MPs.
1667 * div_scale(unsigned int dividend,
1668 * unsigned int divisor,
1669 * unsigned int *scale)
1671 * This function returns (dividend << *scale) //divisor where *scale
1672 * is the largest possible value before overflow. This is used in
1673 * computation where precision must be achieved in order to avoid
1674 * floating point usage.
1678 * while (((dividend >> *scale) >= divisor))
1680 * *scale = 32 - *scale;
1681 * return ((dividend << *scale) / divisor);
1685 xorl %ecx, %ecx /* *scale = 0 */
1687 movl ARG0, %edx /* get dividend */
1689 cmpl ARG1, %edx /* if (divisor > dividend) */
1690 jle 1f /* goto 1f */
1691 addl $1, %ecx /* (*scale)++ */
1692 shrdl $1, %edx, %eax /* dividend >> 1 */
1693 shrl $1, %edx /* dividend >> 1 */
1694 jmp 0b /* goto 0b */
1696 divl ARG1 /* (dividend << (32 - *scale)) / divisor */
1697 movl ARG2, %edx /* get scale */
1698 movl $32, (%edx) /* *scale = 32 */
1699 subl %ecx, (%edx) /* *scale -= %ecx */
1705 * mul_scale(unsigned int multiplicand,
1706 * unsigned int multiplier,
1707 * unsigned int *scale)
1709 * This function returns ((multiplicand * multiplier) >> *scale) where
1710 * scale is the largest possible value before overflow. This is used in
1711 * computation where precision must be achieved in order to avoid
1712 * floating point usage.
1716 * while (overflow((multiplicand * multiplier) >> *scale))
1718 * return ((multiplicand * multiplier) >> *scale);
1722 xorl %ecx, %ecx /* *scale = 0 */
1723 movl ARG0, %eax /* get multiplicand */
1724 mull ARG1 /* multiplicand * multiplier */
1726 cmpl $0, %edx /* if (!overflow()) */
1728 addl $1, %ecx /* (*scale)++ */
1729 shrdl $1, %edx, %eax /* (multiplicand * multiplier) >> 1 */
1730 shrl $1, %edx /* (multiplicand * multiplier) >> 1 */
1733 movl ARG2, %edx /* get scale */
1734 movl %ecx, (%edx) /* set *scale */
1741 * Double-fault exception handler task. The last gasp...
1743 Entry(df_task_start)
1744 CCALL1(panic_double_fault, $(T_DOUBLE_FAULT))
1749 * machine-check handler task. The last gasp...
1751 Entry(mc_task_start)
1752 CCALL1(panic_machine_check, $(T_MACHINE_CHECK))
1756 * Compatibility mode's last gasp...
1760 CCALL1(panic_double_fault64, %eax)
1765 CCALL1(panic_machine_check64, %eax)