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/errno.h>
46 #include <asm/segment.h>
50 #include <asm/dwarf2.h>
51 #include "irq_vectors.h"
53 #define nr_syscalls ((syscall_table_size)/4)
79 #define preempt_stop cli
82 #define resume_kernel restore_nocheck
86 #define resume_userspace_sig check_userspace
88 #define resume_userspace_sig resume_userspace
94 CFI_ADJUST_CFA_OFFSET 4;\
95 /*CFI_REL_OFFSET es, 0;*/\
97 CFI_ADJUST_CFA_OFFSET 4;\
98 /*CFI_REL_OFFSET ds, 0;*/\
100 CFI_ADJUST_CFA_OFFSET 4;\
101 CFI_REL_OFFSET eax, 0;\
103 CFI_ADJUST_CFA_OFFSET 4;\
104 CFI_REL_OFFSET ebp, 0;\
106 CFI_ADJUST_CFA_OFFSET 4;\
107 CFI_REL_OFFSET edi, 0;\
109 CFI_ADJUST_CFA_OFFSET 4;\
110 CFI_REL_OFFSET esi, 0;\
112 CFI_ADJUST_CFA_OFFSET 4;\
113 CFI_REL_OFFSET edx, 0;\
115 CFI_ADJUST_CFA_OFFSET 4;\
116 CFI_REL_OFFSET ecx, 0;\
118 CFI_ADJUST_CFA_OFFSET 4;\
119 CFI_REL_OFFSET ebx, 0;\
120 movl $(__USER_DS), %edx; \
124 #define RESTORE_INT_REGS \
126 CFI_ADJUST_CFA_OFFSET -4;\
129 CFI_ADJUST_CFA_OFFSET -4;\
132 CFI_ADJUST_CFA_OFFSET -4;\
135 CFI_ADJUST_CFA_OFFSET -4;\
138 CFI_ADJUST_CFA_OFFSET -4;\
141 CFI_ADJUST_CFA_OFFSET -4;\
144 CFI_ADJUST_CFA_OFFSET -4;\
147 #define RESTORE_REGS \
150 CFI_ADJUST_CFA_OFFSET -4;\
153 CFI_ADJUST_CFA_OFFSET -4;\
155 .section .fixup,"ax"; \
161 .section __ex_table,"a";\
167 #define RING0_INT_FRAME \
168 CFI_STARTPROC simple;\
169 CFI_DEF_CFA esp, 3*4;\
170 /*CFI_OFFSET cs, -2*4;*/\
173 #define RING0_EC_FRAME \
174 CFI_STARTPROC simple;\
175 CFI_DEF_CFA esp, 4*4;\
176 /*CFI_OFFSET cs, -2*4;*/\
179 #define RING0_PTREGS_FRAME \
180 CFI_STARTPROC simple;\
181 CFI_DEF_CFA esp, OLDESP-EBX;\
182 /*CFI_OFFSET cs, CS-OLDESP;*/\
183 CFI_OFFSET eip, EIP-OLDESP;\
184 /*CFI_OFFSET es, ES-OLDESP;*/\
185 /*CFI_OFFSET ds, DS-OLDESP;*/\
186 CFI_OFFSET eax, EAX-OLDESP;\
187 CFI_OFFSET ebp, EBP-OLDESP;\
188 CFI_OFFSET edi, EDI-OLDESP;\
189 CFI_OFFSET esi, ESI-OLDESP;\
190 CFI_OFFSET edx, EDX-OLDESP;\
191 CFI_OFFSET ecx, ECX-OLDESP;\
192 CFI_OFFSET ebx, EBX-OLDESP
197 CFI_ADJUST_CFA_OFFSET -4
199 GET_THREAD_INFO(%ebp)
201 CFI_ADJUST_CFA_OFFSET -4
206 * Return to user mode is not as complex as all this looks,
207 * but we want the default path for a system call return to
208 * go as quickly as possible which is why some of this is
209 * less clear than it otherwise should be.
212 # userspace resumption stub bypassing syscall exit tracing
218 GET_THREAD_INFO(%ebp)
220 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
222 testl $(VM_MASK | 3), %eax
224 ENTRY(resume_userspace)
225 cli # make sure we don't miss an interrupt
226 # setting need_resched or sigpending
227 # between sampling and the iret
228 movl TI_flags(%ebp), %ecx
229 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
230 # int/exception return?
234 #ifdef CONFIG_PREEMPT
237 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
240 movl TI_flags(%ebp), %ecx # need_resched set ?
241 testb $_TIF_NEED_RESCHED, %cl
243 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
245 call preempt_schedule_irq
250 /* SYSENTER_RETURN points to after the "sysenter" instruction in
251 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
253 # sysenter call handler stub
254 ENTRY(sysenter_entry)
257 CFI_REGISTER esp, ebp
258 movl TSS_sysenter_esp0(%esp),%esp
262 CFI_ADJUST_CFA_OFFSET 4
263 /*CFI_REL_OFFSET ss, 0*/
265 CFI_ADJUST_CFA_OFFSET 4
266 CFI_REL_OFFSET esp, 0
268 CFI_ADJUST_CFA_OFFSET 4
270 CFI_ADJUST_CFA_OFFSET 4
271 /*CFI_REL_OFFSET cs, 0*/
273 * Push current_thread_info()->sysenter_return to the stack.
274 * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
275 * pushed above; +8 corresponds to copy_thread's esp0 setting.
277 pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
278 CFI_ADJUST_CFA_OFFSET 4
279 CFI_REL_OFFSET eip, 0
282 * Load the potential sixth argument from user stack.
283 * Careful about security.
285 cmpl $__PAGE_OFFSET-3,%ebp
288 .section __ex_table,"a"
290 .long 1b,syscall_fault
294 CFI_ADJUST_CFA_OFFSET 4
296 GET_THREAD_INFO(%ebp)
298 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
299 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
300 jnz syscall_trace_entry
301 cmpl $(nr_syscalls), %eax
303 call *sys_call_table(,%eax,4)
306 movl TI_flags(%ebp), %ecx
307 testw $_TIF_ALLWORK_MASK, %cx
308 jne syscall_exit_work
309 /* if something modifies registers it must also disable sysexit */
311 movl OLDESP(%esp), %ecx
318 # system call handler stub
320 RING0_INT_FRAME # can't unwind into user space anyway
321 pushl %eax # save orig_eax
322 CFI_ADJUST_CFA_OFFSET 4
324 GET_THREAD_INFO(%ebp)
325 testl $TF_MASK,EFLAGS(%esp)
327 orl $_TIF_SINGLESTEP,TI_flags(%ebp)
329 # system call tracing in operation / emulation
330 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
331 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
332 jnz syscall_trace_entry
333 cmpl $(nr_syscalls), %eax
336 call *sys_call_table(,%eax,4)
337 movl %eax,EAX(%esp) # store the return value
339 cli # make sure we don't miss an interrupt
340 # setting need_resched or sigpending
341 # between sampling and the iret
342 movl TI_flags(%ebp), %ecx
343 testw $_TIF_ALLWORK_MASK, %cx # current->work
344 jne syscall_exit_work
347 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
348 # Warning: OLDSS(%esp) contains the wrong/random values if we
349 # are returning to the kernel.
350 # See comments in process.c:copy_thread() for details.
351 movb OLDSS(%esp), %ah
353 andl $(VM_MASK | (4 << 8) | 3), %eax
354 cmpl $((4 << 8) | 3), %eax
356 je ldt_ss # returning to user-space with LDT SS
360 CFI_ADJUST_CFA_OFFSET -4
365 pushl $0 # no error code
369 .section __ex_table,"a"
376 larl OLDSS(%esp), %eax
378 testl $0x00400000, %eax # returning to 32bit stack?
379 jnz restore_nocheck # allright, normal return
380 /* If returning to userspace with 16bit stack,
381 * try to fix the higher word of ESP, as the CPU
383 * This is an "official" bug of all the x86-compatible
384 * CPUs, which we can try to work around to make
385 * dosemu and wine happy. */
386 subl $8, %esp # reserve space for switch16 pointer
387 CFI_ADJUST_CFA_OFFSET 8
390 /* Set up the 16bit stack frame with switch32 pointer on top,
391 * and a switch16 pointer on top of the current frame. */
392 call setup_x86_bogus_stack
393 CFI_ADJUST_CFA_OFFSET -8 # frame has moved
395 lss 20+4(%esp), %esp # switch to 16bit stack
397 .section __ex_table,"a"
403 # perform work that needs to be done immediately before resumption
405 RING0_PTREGS_FRAME # can't unwind into user space anyway
407 testb $_TIF_NEED_RESCHED, %cl
411 cli # make sure we don't miss an interrupt
412 # setting need_resched or sigpending
413 # between sampling and the iret
414 movl TI_flags(%ebp), %ecx
415 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
416 # than syscall tracing?
418 testb $_TIF_NEED_RESCHED, %cl
421 work_notifysig: # deal with pending signals and
422 # notify-resume requests
423 testl $VM_MASK, EFLAGS(%esp)
425 jne work_notifysig_v86 # returning to kernel-space or
428 call do_notify_resume
429 jmp resume_userspace_sig
434 pushl %ecx # save ti_flags for do_notify_resume
435 CFI_ADJUST_CFA_OFFSET 4
436 call save_v86_state # %eax contains pt_regs pointer
438 CFI_ADJUST_CFA_OFFSET -4
441 call do_notify_resume
442 jmp resume_userspace_sig
445 # perform syscall exit tracing
448 movl $-ENOSYS,EAX(%esp)
451 call do_syscall_trace
453 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
454 # so must skip actual syscall
455 movl ORIG_EAX(%esp), %eax
456 cmpl $(nr_syscalls), %eax
460 # perform syscall exit tracing
463 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
465 sti # could let do_syscall_trace() call
469 call do_syscall_trace
473 RING0_INT_FRAME # can't unwind into user space anyway
475 pushl %eax # save orig_eax
476 CFI_ADJUST_CFA_OFFSET 4
478 GET_THREAD_INFO(%ebp)
479 movl $-EFAULT,EAX(%esp)
483 movl $-ENOSYS,EAX(%esp)
487 #define FIXUP_ESPFIX_STACK \
489 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
490 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
491 /* copy data from 16bit stack to 32bit stack */ \
492 call fixup_x86_bogus_stack; \
493 /* put ESP to the proper location */ \
495 #define UNWIND_ESPFIX_STACK \
497 CFI_ADJUST_CFA_OFFSET 4; \
499 /* see if on 16bit stack */ \
500 cmpw $__ESPFIX_SS, %ax; \
503 CFI_ADJUST_CFA_OFFSET -4; \
504 .section .fixup,"ax"; \
505 28: movl $__KERNEL_DS, %eax; \
508 /* switch to 32bit stack */ \
509 FIXUP_ESPFIX_STACK; \
514 * Build the entry stubs and pointer table with
515 * some assembler magic.
522 ENTRY(irq_entries_start)
527 CFI_ADJUST_CFA_OFFSET -4
530 CFI_ADJUST_CFA_OFFSET 4
546 #define BUILD_INTERRUPT(name, nr) \
550 CFI_ADJUST_CFA_OFFSET 4; \
557 /* The include is where all of the SMP etc. interrupts come from */
558 #include "entry_arch.h"
562 pushl $0 # no error code
563 CFI_ADJUST_CFA_OFFSET 4
564 pushl $do_divide_error
565 CFI_ADJUST_CFA_OFFSET 4
569 CFI_ADJUST_CFA_OFFSET 4
570 /*CFI_REL_OFFSET ds, 0*/
572 CFI_ADJUST_CFA_OFFSET 4
573 CFI_REL_OFFSET eax, 0
576 CFI_ADJUST_CFA_OFFSET 4
577 CFI_REL_OFFSET ebp, 0
579 CFI_ADJUST_CFA_OFFSET 4
580 CFI_REL_OFFSET edi, 0
582 CFI_ADJUST_CFA_OFFSET 4
583 CFI_REL_OFFSET esi, 0
585 CFI_ADJUST_CFA_OFFSET 4
586 CFI_REL_OFFSET edx, 0
589 CFI_ADJUST_CFA_OFFSET 4
590 CFI_REL_OFFSET ecx, 0
592 CFI_ADJUST_CFA_OFFSET 4
593 CFI_REL_OFFSET ebx, 0
596 CFI_ADJUST_CFA_OFFSET 4
597 /*CFI_REL_OFFSET es, 0*/
600 CFI_ADJUST_CFA_OFFSET -4
601 /*CFI_REGISTER es, ecx*/
602 movl ES(%esp), %edi # get the function address
603 movl ORIG_EAX(%esp), %edx # get the error code
604 movl %eax, ORIG_EAX(%esp)
606 /*CFI_REL_OFFSET es, ES*/
607 movl $(__USER_DS), %ecx
610 movl %esp,%eax # pt_regs pointer
612 jmp ret_from_exception
615 ENTRY(coprocessor_error)
618 CFI_ADJUST_CFA_OFFSET 4
619 pushl $do_coprocessor_error
620 CFI_ADJUST_CFA_OFFSET 4
624 ENTRY(simd_coprocessor_error)
627 CFI_ADJUST_CFA_OFFSET 4
628 pushl $do_simd_coprocessor_error
629 CFI_ADJUST_CFA_OFFSET 4
633 ENTRY(device_not_available)
635 pushl $-1 # mark this as an int
636 CFI_ADJUST_CFA_OFFSET 4
639 testl $0x4, %eax # EM (math emulation bit)
640 jne device_not_available_emulate
642 call math_state_restore
643 jmp ret_from_exception
644 device_not_available_emulate:
645 pushl $0 # temporary storage for ORIG_EIP
646 CFI_ADJUST_CFA_OFFSET 4
649 CFI_ADJUST_CFA_OFFSET -4
650 jmp ret_from_exception
654 * Debug traps and NMI can happen at the one SYSENTER instruction
655 * that sets up the real kernel stack. Check here, since we can't
656 * allow the wrong stack to be used.
658 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
659 * already pushed 3 words if it hits on the sysenter instruction:
660 * eflags, cs and eip.
662 * We just load the right stack, and push the three (known) values
663 * by hand onto the new stack - while updating the return eip past
664 * the instruction that would have done it for sysenter.
666 #define FIX_STACK(offset, ok, label) \
667 cmpw $__KERNEL_CS,4(%esp); \
670 movl TSS_sysenter_esp0+offset(%esp),%esp; \
672 pushl $__KERNEL_CS; \
673 pushl $sysenter_past_esp
677 cmpl $sysenter_entry,(%esp)
678 jne debug_stack_correct
679 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
681 pushl $-1 # mark this as an int
682 CFI_ADJUST_CFA_OFFSET 4
684 xorl %edx,%edx # error code 0
685 movl %esp,%eax # pt_regs pointer
687 jmp ret_from_exception
691 * NMI is doubly nasty. It can happen _while_ we're handling
692 * a debug fault, and the debug fault hasn't yet been able to
693 * clear up the stack. So we first check whether we got an
694 * NMI on the sysenter entry path, but after that we need to
695 * check whether we got an NMI on the debug path where the debug
696 * fault happened on the sysenter path.
701 CFI_ADJUST_CFA_OFFSET 4
703 cmpw $__ESPFIX_SS, %ax
705 CFI_ADJUST_CFA_OFFSET -4
707 cmpl $sysenter_entry,(%esp)
710 CFI_ADJUST_CFA_OFFSET 4
712 /* Do not access memory above the end of our stack page,
713 * it might not exist.
715 andl $(THREAD_SIZE-1),%eax
716 cmpl $(THREAD_SIZE-20),%eax
718 CFI_ADJUST_CFA_OFFSET -4
719 jae nmi_stack_correct
720 cmpl $sysenter_entry,12(%esp)
721 je nmi_debug_stack_check
724 CFI_ADJUST_CFA_OFFSET 4
726 xorl %edx,%edx # zero error code
727 movl %esp,%eax # pt_regs pointer
733 FIX_STACK(12,nmi_stack_correct, 1)
734 jmp nmi_stack_correct
735 nmi_debug_stack_check:
736 cmpw $__KERNEL_CS,16(%esp)
737 jne nmi_stack_correct
740 cmpl $debug_esp_fix_insn,(%esp)
742 FIX_STACK(24,nmi_stack_correct, 1)
743 jmp nmi_stack_correct
747 /* create the pointer to lss back */
749 CFI_ADJUST_CFA_OFFSET 4
751 CFI_ADJUST_CFA_OFFSET 4
754 /* copy the iret frame of 12 bytes */
757 CFI_ADJUST_CFA_OFFSET 4
760 CFI_ADJUST_CFA_OFFSET 4
762 FIXUP_ESPFIX_STACK # %eax == %esp
763 CFI_ADJUST_CFA_OFFSET -20 # the frame has now moved
764 xorl %edx,%edx # zero error code
767 lss 12+4(%esp), %esp # back to 16bit stack
770 .section __ex_table,"a"
777 pushl $-1 # mark this as an int
778 CFI_ADJUST_CFA_OFFSET 4
780 xorl %edx,%edx # zero error code
781 movl %esp,%eax # pt_regs pointer
783 jmp ret_from_exception
790 CFI_ADJUST_CFA_OFFSET 4
792 CFI_ADJUST_CFA_OFFSET 4
799 CFI_ADJUST_CFA_OFFSET 4
801 CFI_ADJUST_CFA_OFFSET 4
808 CFI_ADJUST_CFA_OFFSET 4
810 CFI_ADJUST_CFA_OFFSET 4
814 ENTRY(coprocessor_segment_overrun)
817 CFI_ADJUST_CFA_OFFSET 4
818 pushl $do_coprocessor_segment_overrun
819 CFI_ADJUST_CFA_OFFSET 4
825 pushl $do_invalid_TSS
826 CFI_ADJUST_CFA_OFFSET 4
830 ENTRY(segment_not_present)
832 pushl $do_segment_not_present
833 CFI_ADJUST_CFA_OFFSET 4
839 pushl $do_stack_segment
840 CFI_ADJUST_CFA_OFFSET 4
844 KPROBE_ENTRY(general_protection)
846 pushl $do_general_protection
847 CFI_ADJUST_CFA_OFFSET 4
852 ENTRY(alignment_check)
854 pushl $do_alignment_check
855 CFI_ADJUST_CFA_OFFSET 4
859 KPROBE_ENTRY(page_fault)
862 CFI_ADJUST_CFA_OFFSET 4
867 #ifdef CONFIG_X86_MCE
871 CFI_ADJUST_CFA_OFFSET 4
872 pushl machine_check_vector
873 CFI_ADJUST_CFA_OFFSET 4
878 ENTRY(spurious_interrupt_bug)
881 CFI_ADJUST_CFA_OFFSET 4
882 pushl $do_spurious_interrupt_bug
883 CFI_ADJUST_CFA_OFFSET 4
887 #ifdef CONFIG_STACK_UNWIND
888 ENTRY(arch_unwind_init_running)
901 movl $__USER_DS, DS(%edx)
902 movl $__USER_DS, ES(%edx)
903 movl %ebx, ORIG_EAX(%edx)
906 movl $__KERNEL_CS, CS(%edx)
907 movl %ebx, EFLAGS(%edx)
908 movl %eax, OLDESP(%edx)
912 movl $__KERNEL_DS, OLDSS(%edx)
915 ENDPROC(arch_unwind_init_running)
919 #include "syscall_table.S"
921 syscall_table_size=(.-sys_call_table)