2 * linux/arch/i386/entry.S
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * entry.S contains the system-call and fault low-level handling routines.
9 * This also contains the timer-interrupt handler, as well as all interrupts
10 * and faults that can result in a task-switch.
12 * NOTE: This code handles signal-recognition, which happens every time
13 * after a timer-interrupt and after each system call.
15 * I changed all the .align's to 4 (16 byte alignment), as that's faster
18 * Stack layout in 'ret_from_system_call':
19 * ptrace needs to have all regs on the stack.
20 * if the order here is changed, it needs to be
21 * updated in fork.c:copy_process, signal.c:do_signal,
22 * ptrace.c and ptrace.h
40 * "current" is in register %ebx during any slow entries.
43 #include <linux/linkage.h>
44 #include <asm/thread_info.h>
45 #include <asm/irqflags.h>
46 #include <asm/errno.h>
47 #include <asm/segment.h>
51 #include <asm/dwarf2.h>
52 #include "irq_vectors.h"
54 #define nr_syscalls ((syscall_table_size)/4)
80 #define preempt_stop cli; TRACE_IRQS_OFF
83 #define resume_kernel restore_nocheck
86 .macro TRACE_IRQS_IRET
87 #ifdef CONFIG_TRACE_IRQFLAGS
88 testl $IF_MASK,EFLAGS(%esp) # interrupts off?
96 #define resume_userspace_sig check_userspace
98 #define resume_userspace_sig resume_userspace
104 CFI_ADJUST_CFA_OFFSET 4;\
105 /*CFI_REL_OFFSET es, 0;*/\
107 CFI_ADJUST_CFA_OFFSET 4;\
108 /*CFI_REL_OFFSET ds, 0;*/\
110 CFI_ADJUST_CFA_OFFSET 4;\
111 CFI_REL_OFFSET eax, 0;\
113 CFI_ADJUST_CFA_OFFSET 4;\
114 CFI_REL_OFFSET ebp, 0;\
116 CFI_ADJUST_CFA_OFFSET 4;\
117 CFI_REL_OFFSET edi, 0;\
119 CFI_ADJUST_CFA_OFFSET 4;\
120 CFI_REL_OFFSET esi, 0;\
122 CFI_ADJUST_CFA_OFFSET 4;\
123 CFI_REL_OFFSET edx, 0;\
125 CFI_ADJUST_CFA_OFFSET 4;\
126 CFI_REL_OFFSET ecx, 0;\
128 CFI_ADJUST_CFA_OFFSET 4;\
129 CFI_REL_OFFSET ebx, 0;\
130 movl $(__USER_DS), %edx; \
134 #define RESTORE_INT_REGS \
136 CFI_ADJUST_CFA_OFFSET -4;\
139 CFI_ADJUST_CFA_OFFSET -4;\
142 CFI_ADJUST_CFA_OFFSET -4;\
145 CFI_ADJUST_CFA_OFFSET -4;\
148 CFI_ADJUST_CFA_OFFSET -4;\
151 CFI_ADJUST_CFA_OFFSET -4;\
154 CFI_ADJUST_CFA_OFFSET -4;\
157 #define RESTORE_REGS \
160 CFI_ADJUST_CFA_OFFSET -4;\
163 CFI_ADJUST_CFA_OFFSET -4;\
165 .section .fixup,"ax"; \
171 .section __ex_table,"a";\
177 #define RING0_INT_FRAME \
178 CFI_STARTPROC simple;\
179 CFI_DEF_CFA esp, 3*4;\
180 /*CFI_OFFSET cs, -2*4;*/\
183 #define RING0_EC_FRAME \
184 CFI_STARTPROC simple;\
185 CFI_DEF_CFA esp, 4*4;\
186 /*CFI_OFFSET cs, -2*4;*/\
189 #define RING0_PTREGS_FRAME \
190 CFI_STARTPROC simple;\
191 CFI_DEF_CFA esp, OLDESP-EBX;\
192 /*CFI_OFFSET cs, CS-OLDESP;*/\
193 CFI_OFFSET eip, EIP-OLDESP;\
194 /*CFI_OFFSET es, ES-OLDESP;*/\
195 /*CFI_OFFSET ds, DS-OLDESP;*/\
196 CFI_OFFSET eax, EAX-OLDESP;\
197 CFI_OFFSET ebp, EBP-OLDESP;\
198 CFI_OFFSET edi, EDI-OLDESP;\
199 CFI_OFFSET esi, ESI-OLDESP;\
200 CFI_OFFSET edx, EDX-OLDESP;\
201 CFI_OFFSET ecx, ECX-OLDESP;\
202 CFI_OFFSET ebx, EBX-OLDESP
207 CFI_ADJUST_CFA_OFFSET 4
209 GET_THREAD_INFO(%ebp)
211 CFI_ADJUST_CFA_OFFSET -4
216 * Return to user mode is not as complex as all this looks,
217 * but we want the default path for a system call return to
218 * go as quickly as possible which is why some of this is
219 * less clear than it otherwise should be.
222 # userspace resumption stub bypassing syscall exit tracing
228 GET_THREAD_INFO(%ebp)
230 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
232 testl $(VM_MASK | 3), %eax
234 ENTRY(resume_userspace)
235 cli # make sure we don't miss an interrupt
236 # setting need_resched or sigpending
237 # between sampling and the iret
238 movl TI_flags(%ebp), %ecx
239 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
240 # int/exception return?
244 #ifdef CONFIG_PREEMPT
247 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
250 movl TI_flags(%ebp), %ecx # need_resched set ?
251 testb $_TIF_NEED_RESCHED, %cl
253 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
255 call preempt_schedule_irq
260 /* SYSENTER_RETURN points to after the "sysenter" instruction in
261 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
263 # sysenter call handler stub
264 ENTRY(sysenter_entry)
267 CFI_REGISTER esp, ebp
268 movl TSS_sysenter_esp0(%esp),%esp
271 * No need to follow this irqs on/off section: the syscall
272 * disabled irqs and here we enable it straight after entry:
276 CFI_ADJUST_CFA_OFFSET 4
277 /*CFI_REL_OFFSET ss, 0*/
279 CFI_ADJUST_CFA_OFFSET 4
280 CFI_REL_OFFSET esp, 0
282 CFI_ADJUST_CFA_OFFSET 4
284 CFI_ADJUST_CFA_OFFSET 4
285 /*CFI_REL_OFFSET cs, 0*/
287 * Push current_thread_info()->sysenter_return to the stack.
288 * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
289 * pushed above; +8 corresponds to copy_thread's esp0 setting.
291 pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
292 CFI_ADJUST_CFA_OFFSET 4
293 CFI_REL_OFFSET eip, 0
296 * Load the potential sixth argument from user stack.
297 * Careful about security.
299 cmpl $__PAGE_OFFSET-3,%ebp
302 .section __ex_table,"a"
304 .long 1b,syscall_fault
308 CFI_ADJUST_CFA_OFFSET 4
310 GET_THREAD_INFO(%ebp)
312 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
313 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
314 jnz syscall_trace_entry
315 cmpl $(nr_syscalls), %eax
317 call *sys_call_table(,%eax,4)
321 movl TI_flags(%ebp), %ecx
322 testw $_TIF_ALLWORK_MASK, %cx
323 jne syscall_exit_work
324 /* if something modifies registers it must also disable sysexit */
326 movl OLDESP(%esp), %ecx
334 # system call handler stub
336 RING0_INT_FRAME # can't unwind into user space anyway
337 pushl %eax # save orig_eax
338 CFI_ADJUST_CFA_OFFSET 4
340 GET_THREAD_INFO(%ebp)
341 testl $TF_MASK,EFLAGS(%esp)
343 orl $_TIF_SINGLESTEP,TI_flags(%ebp)
345 # system call tracing in operation / emulation
346 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
347 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
348 jnz syscall_trace_entry
349 cmpl $(nr_syscalls), %eax
352 call *sys_call_table(,%eax,4)
353 movl %eax,EAX(%esp) # store the return value
355 cli # make sure we don't miss an interrupt
356 # setting need_resched or sigpending
357 # between sampling and the iret
359 movl TI_flags(%ebp), %ecx
360 testw $_TIF_ALLWORK_MASK, %cx # current->work
361 jne syscall_exit_work
364 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
365 # Warning: OLDSS(%esp) contains the wrong/random values if we
366 # are returning to the kernel.
367 # See comments in process.c:copy_thread() for details.
368 movb OLDSS(%esp), %ah
370 andl $(VM_MASK | (4 << 8) | 3), %eax
371 cmpl $((4 << 8) | 3), %eax
373 je ldt_ss # returning to user-space with LDT SS
376 restore_nocheck_notrace:
379 CFI_ADJUST_CFA_OFFSET -4
385 pushl $0 # no error code
389 .section __ex_table,"a"
396 larl OLDSS(%esp), %eax
398 testl $0x00400000, %eax # returning to 32bit stack?
399 jnz restore_nocheck # allright, normal return
400 /* If returning to userspace with 16bit stack,
401 * try to fix the higher word of ESP, as the CPU
403 * This is an "official" bug of all the x86-compatible
404 * CPUs, which we can try to work around to make
405 * dosemu and wine happy. */
406 subl $8, %esp # reserve space for switch16 pointer
407 CFI_ADJUST_CFA_OFFSET 8
411 /* Set up the 16bit stack frame with switch32 pointer on top,
412 * and a switch16 pointer on top of the current frame. */
413 call setup_x86_bogus_stack
414 CFI_ADJUST_CFA_OFFSET -8 # frame has moved
417 lss 20+4(%esp), %esp # switch to 16bit stack
419 .section __ex_table,"a"
425 # perform work that needs to be done immediately before resumption
427 RING0_PTREGS_FRAME # can't unwind into user space anyway
429 testb $_TIF_NEED_RESCHED, %cl
433 cli # make sure we don't miss an interrupt
434 # setting need_resched or sigpending
435 # between sampling and the iret
437 movl TI_flags(%ebp), %ecx
438 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
439 # than syscall tracing?
441 testb $_TIF_NEED_RESCHED, %cl
444 work_notifysig: # deal with pending signals and
445 # notify-resume requests
446 testl $VM_MASK, EFLAGS(%esp)
448 jne work_notifysig_v86 # returning to kernel-space or
451 call do_notify_resume
452 jmp resume_userspace_sig
457 pushl %ecx # save ti_flags for do_notify_resume
458 CFI_ADJUST_CFA_OFFSET 4
459 call save_v86_state # %eax contains pt_regs pointer
461 CFI_ADJUST_CFA_OFFSET -4
464 call do_notify_resume
465 jmp resume_userspace_sig
468 # perform syscall exit tracing
471 movl $-ENOSYS,EAX(%esp)
474 call do_syscall_trace
476 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
477 # so must skip actual syscall
478 movl ORIG_EAX(%esp), %eax
479 cmpl $(nr_syscalls), %eax
483 # perform syscall exit tracing
486 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
489 sti # could let do_syscall_trace() call
493 call do_syscall_trace
497 RING0_INT_FRAME # can't unwind into user space anyway
499 pushl %eax # save orig_eax
500 CFI_ADJUST_CFA_OFFSET 4
502 GET_THREAD_INFO(%ebp)
503 movl $-EFAULT,EAX(%esp)
507 movl $-ENOSYS,EAX(%esp)
511 #define FIXUP_ESPFIX_STACK \
513 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
514 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
515 /* copy data from 16bit stack to 32bit stack */ \
516 call fixup_x86_bogus_stack; \
517 /* put ESP to the proper location */ \
519 #define UNWIND_ESPFIX_STACK \
521 CFI_ADJUST_CFA_OFFSET 4; \
523 /* see if on 16bit stack */ \
524 cmpw $__ESPFIX_SS, %ax; \
527 CFI_ADJUST_CFA_OFFSET -4; \
528 .section .fixup,"ax"; \
529 28: movl $__KERNEL_DS, %eax; \
532 /* switch to 32bit stack */ \
533 FIXUP_ESPFIX_STACK; \
538 * Build the entry stubs and pointer table with
539 * some assembler magic.
546 ENTRY(irq_entries_start)
551 CFI_ADJUST_CFA_OFFSET -4
554 CFI_ADJUST_CFA_OFFSET 4
563 * the CPU automatically disables interrupts when executing an IRQ vector,
564 * so IRQ-flags tracing has to follow that:
575 #define BUILD_INTERRUPT(name, nr) \
579 CFI_ADJUST_CFA_OFFSET 4; \
587 /* The include is where all of the SMP etc. interrupts come from */
588 #include "entry_arch.h"
592 pushl $0 # no error code
593 CFI_ADJUST_CFA_OFFSET 4
594 pushl $do_divide_error
595 CFI_ADJUST_CFA_OFFSET 4
599 CFI_ADJUST_CFA_OFFSET 4
600 /*CFI_REL_OFFSET ds, 0*/
602 CFI_ADJUST_CFA_OFFSET 4
603 CFI_REL_OFFSET eax, 0
606 CFI_ADJUST_CFA_OFFSET 4
607 CFI_REL_OFFSET ebp, 0
609 CFI_ADJUST_CFA_OFFSET 4
610 CFI_REL_OFFSET edi, 0
612 CFI_ADJUST_CFA_OFFSET 4
613 CFI_REL_OFFSET esi, 0
615 CFI_ADJUST_CFA_OFFSET 4
616 CFI_REL_OFFSET edx, 0
619 CFI_ADJUST_CFA_OFFSET 4
620 CFI_REL_OFFSET ecx, 0
622 CFI_ADJUST_CFA_OFFSET 4
623 CFI_REL_OFFSET ebx, 0
626 CFI_ADJUST_CFA_OFFSET 4
627 /*CFI_REL_OFFSET es, 0*/
630 CFI_ADJUST_CFA_OFFSET -4
631 /*CFI_REGISTER es, ecx*/
632 movl ES(%esp), %edi # get the function address
633 movl ORIG_EAX(%esp), %edx # get the error code
634 movl %eax, ORIG_EAX(%esp)
636 /*CFI_REL_OFFSET es, ES*/
637 movl $(__USER_DS), %ecx
640 movl %esp,%eax # pt_regs pointer
642 jmp ret_from_exception
645 ENTRY(coprocessor_error)
648 CFI_ADJUST_CFA_OFFSET 4
649 pushl $do_coprocessor_error
650 CFI_ADJUST_CFA_OFFSET 4
654 ENTRY(simd_coprocessor_error)
657 CFI_ADJUST_CFA_OFFSET 4
658 pushl $do_simd_coprocessor_error
659 CFI_ADJUST_CFA_OFFSET 4
663 ENTRY(device_not_available)
665 pushl $-1 # mark this as an int
666 CFI_ADJUST_CFA_OFFSET 4
669 testl $0x4, %eax # EM (math emulation bit)
670 jne device_not_available_emulate
672 call math_state_restore
673 jmp ret_from_exception
674 device_not_available_emulate:
675 pushl $0 # temporary storage for ORIG_EIP
676 CFI_ADJUST_CFA_OFFSET 4
679 CFI_ADJUST_CFA_OFFSET -4
680 jmp ret_from_exception
684 * Debug traps and NMI can happen at the one SYSENTER instruction
685 * that sets up the real kernel stack. Check here, since we can't
686 * allow the wrong stack to be used.
688 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
689 * already pushed 3 words if it hits on the sysenter instruction:
690 * eflags, cs and eip.
692 * We just load the right stack, and push the three (known) values
693 * by hand onto the new stack - while updating the return eip past
694 * the instruction that would have done it for sysenter.
696 #define FIX_STACK(offset, ok, label) \
697 cmpw $__KERNEL_CS,4(%esp); \
700 movl TSS_sysenter_esp0+offset(%esp),%esp; \
702 pushl $__KERNEL_CS; \
703 pushl $sysenter_past_esp
707 cmpl $sysenter_entry,(%esp)
708 jne debug_stack_correct
709 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
711 pushl $-1 # mark this as an int
712 CFI_ADJUST_CFA_OFFSET 4
714 xorl %edx,%edx # error code 0
715 movl %esp,%eax # pt_regs pointer
717 jmp ret_from_exception
721 * NMI is doubly nasty. It can happen _while_ we're handling
722 * a debug fault, and the debug fault hasn't yet been able to
723 * clear up the stack. So we first check whether we got an
724 * NMI on the sysenter entry path, but after that we need to
725 * check whether we got an NMI on the debug path where the debug
726 * fault happened on the sysenter path.
731 CFI_ADJUST_CFA_OFFSET 4
733 cmpw $__ESPFIX_SS, %ax
735 CFI_ADJUST_CFA_OFFSET -4
737 cmpl $sysenter_entry,(%esp)
740 CFI_ADJUST_CFA_OFFSET 4
742 /* Do not access memory above the end of our stack page,
743 * it might not exist.
745 andl $(THREAD_SIZE-1),%eax
746 cmpl $(THREAD_SIZE-20),%eax
748 CFI_ADJUST_CFA_OFFSET -4
749 jae nmi_stack_correct
750 cmpl $sysenter_entry,12(%esp)
751 je nmi_debug_stack_check
754 CFI_ADJUST_CFA_OFFSET 4
756 xorl %edx,%edx # zero error code
757 movl %esp,%eax # pt_regs pointer
759 jmp restore_nocheck_notrace
763 FIX_STACK(12,nmi_stack_correct, 1)
764 jmp nmi_stack_correct
765 nmi_debug_stack_check:
766 cmpw $__KERNEL_CS,16(%esp)
767 jne nmi_stack_correct
770 cmpl $debug_esp_fix_insn,(%esp)
772 FIX_STACK(24,nmi_stack_correct, 1)
773 jmp nmi_stack_correct
777 /* create the pointer to lss back */
779 CFI_ADJUST_CFA_OFFSET 4
781 CFI_ADJUST_CFA_OFFSET 4
784 /* copy the iret frame of 12 bytes */
787 CFI_ADJUST_CFA_OFFSET 4
790 CFI_ADJUST_CFA_OFFSET 4
792 FIXUP_ESPFIX_STACK # %eax == %esp
793 CFI_ADJUST_CFA_OFFSET -20 # the frame has now moved
794 xorl %edx,%edx # zero error code
797 lss 12+4(%esp), %esp # back to 16bit stack
800 .section __ex_table,"a"
807 pushl $-1 # mark this as an int
808 CFI_ADJUST_CFA_OFFSET 4
810 xorl %edx,%edx # zero error code
811 movl %esp,%eax # pt_regs pointer
813 jmp ret_from_exception
820 CFI_ADJUST_CFA_OFFSET 4
822 CFI_ADJUST_CFA_OFFSET 4
829 CFI_ADJUST_CFA_OFFSET 4
831 CFI_ADJUST_CFA_OFFSET 4
838 CFI_ADJUST_CFA_OFFSET 4
840 CFI_ADJUST_CFA_OFFSET 4
844 ENTRY(coprocessor_segment_overrun)
847 CFI_ADJUST_CFA_OFFSET 4
848 pushl $do_coprocessor_segment_overrun
849 CFI_ADJUST_CFA_OFFSET 4
855 pushl $do_invalid_TSS
856 CFI_ADJUST_CFA_OFFSET 4
860 ENTRY(segment_not_present)
862 pushl $do_segment_not_present
863 CFI_ADJUST_CFA_OFFSET 4
869 pushl $do_stack_segment
870 CFI_ADJUST_CFA_OFFSET 4
874 KPROBE_ENTRY(general_protection)
876 pushl $do_general_protection
877 CFI_ADJUST_CFA_OFFSET 4
882 ENTRY(alignment_check)
884 pushl $do_alignment_check
885 CFI_ADJUST_CFA_OFFSET 4
889 KPROBE_ENTRY(page_fault)
892 CFI_ADJUST_CFA_OFFSET 4
897 #ifdef CONFIG_X86_MCE
901 CFI_ADJUST_CFA_OFFSET 4
902 pushl machine_check_vector
903 CFI_ADJUST_CFA_OFFSET 4
908 ENTRY(spurious_interrupt_bug)
911 CFI_ADJUST_CFA_OFFSET 4
912 pushl $do_spurious_interrupt_bug
913 CFI_ADJUST_CFA_OFFSET 4
917 #ifdef CONFIG_STACK_UNWIND
918 ENTRY(arch_unwind_init_running)
931 movl $__USER_DS, DS(%edx)
932 movl $__USER_DS, ES(%edx)
933 movl %ebx, ORIG_EAX(%edx)
936 movl $__KERNEL_CS, CS(%edx)
937 movl %ebx, EFLAGS(%edx)
938 movl %eax, OLDESP(%edx)
942 movl $__KERNEL_DS, OLDSS(%edx)
945 ENDPROC(arch_unwind_init_running)
949 #include "syscall_table.S"
951 syscall_table_size=(.-sys_call_table)