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
212 pushl $0x0202 # Reset kernel eflags
213 CFI_ADJUST_CFA_OFFSET 4
215 CFI_ADJUST_CFA_OFFSET -4
220 * Return to user mode is not as complex as all this looks,
221 * but we want the default path for a system call return to
222 * go as quickly as possible which is why some of this is
223 * less clear than it otherwise should be.
226 # userspace resumption stub bypassing syscall exit tracing
232 GET_THREAD_INFO(%ebp)
234 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
236 testl $(VM_MASK | 3), %eax
238 ENTRY(resume_userspace)
239 cli # make sure we don't miss an interrupt
240 # setting need_resched or sigpending
241 # between sampling and the iret
242 movl TI_flags(%ebp), %ecx
243 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
244 # int/exception return?
248 #ifdef CONFIG_PREEMPT
251 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
254 movl TI_flags(%ebp), %ecx # need_resched set ?
255 testb $_TIF_NEED_RESCHED, %cl
257 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
259 call preempt_schedule_irq
264 /* SYSENTER_RETURN points to after the "sysenter" instruction in
265 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
267 # sysenter call handler stub
268 ENTRY(sysenter_entry)
271 CFI_REGISTER esp, ebp
272 movl TSS_sysenter_esp0(%esp),%esp
275 * No need to follow this irqs on/off section: the syscall
276 * disabled irqs and here we enable it straight after entry:
280 CFI_ADJUST_CFA_OFFSET 4
281 /*CFI_REL_OFFSET ss, 0*/
283 CFI_ADJUST_CFA_OFFSET 4
284 CFI_REL_OFFSET esp, 0
286 CFI_ADJUST_CFA_OFFSET 4
288 CFI_ADJUST_CFA_OFFSET 4
289 /*CFI_REL_OFFSET cs, 0*/
291 * Push current_thread_info()->sysenter_return to the stack.
292 * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
293 * pushed above; +8 corresponds to copy_thread's esp0 setting.
295 pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
296 CFI_ADJUST_CFA_OFFSET 4
297 CFI_REL_OFFSET eip, 0
300 * Load the potential sixth argument from user stack.
301 * Careful about security.
303 cmpl $__PAGE_OFFSET-3,%ebp
306 .section __ex_table,"a"
308 .long 1b,syscall_fault
312 CFI_ADJUST_CFA_OFFSET 4
314 GET_THREAD_INFO(%ebp)
316 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
317 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
318 jnz syscall_trace_entry
319 cmpl $(nr_syscalls), %eax
321 call *sys_call_table(,%eax,4)
325 movl TI_flags(%ebp), %ecx
326 testw $_TIF_ALLWORK_MASK, %cx
327 jne syscall_exit_work
328 /* if something modifies registers it must also disable sysexit */
330 movl OLDESP(%esp), %ecx
338 # system call handler stub
340 RING0_INT_FRAME # can't unwind into user space anyway
341 pushl %eax # save orig_eax
342 CFI_ADJUST_CFA_OFFSET 4
344 GET_THREAD_INFO(%ebp)
345 testl $TF_MASK,EFLAGS(%esp)
347 orl $_TIF_SINGLESTEP,TI_flags(%ebp)
349 # system call tracing in operation / emulation
350 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
351 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
352 jnz syscall_trace_entry
353 cmpl $(nr_syscalls), %eax
356 call *sys_call_table(,%eax,4)
357 movl %eax,EAX(%esp) # store the return value
359 cli # make sure we don't miss an interrupt
360 # setting need_resched or sigpending
361 # between sampling and the iret
363 movl TI_flags(%ebp), %ecx
364 testw $_TIF_ALLWORK_MASK, %cx # current->work
365 jne syscall_exit_work
368 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
369 # Warning: OLDSS(%esp) contains the wrong/random values if we
370 # are returning to the kernel.
371 # See comments in process.c:copy_thread() for details.
372 movb OLDSS(%esp), %ah
374 andl $(VM_MASK | (4 << 8) | 3), %eax
375 cmpl $((4 << 8) | 3), %eax
377 je ldt_ss # returning to user-space with LDT SS
380 restore_nocheck_notrace:
383 CFI_ADJUST_CFA_OFFSET -4
389 pushl $0 # no error code
393 .section __ex_table,"a"
400 larl OLDSS(%esp), %eax
402 testl $0x00400000, %eax # returning to 32bit stack?
403 jnz restore_nocheck # allright, normal return
404 /* If returning to userspace with 16bit stack,
405 * try to fix the higher word of ESP, as the CPU
407 * This is an "official" bug of all the x86-compatible
408 * CPUs, which we can try to work around to make
409 * dosemu and wine happy. */
410 subl $8, %esp # reserve space for switch16 pointer
411 CFI_ADJUST_CFA_OFFSET 8
415 /* Set up the 16bit stack frame with switch32 pointer on top,
416 * and a switch16 pointer on top of the current frame. */
417 call setup_x86_bogus_stack
418 CFI_ADJUST_CFA_OFFSET -8 # frame has moved
421 lss 20+4(%esp), %esp # switch to 16bit stack
423 .section __ex_table,"a"
429 # perform work that needs to be done immediately before resumption
431 RING0_PTREGS_FRAME # can't unwind into user space anyway
433 testb $_TIF_NEED_RESCHED, %cl
437 cli # make sure we don't miss an interrupt
438 # setting need_resched or sigpending
439 # between sampling and the iret
441 movl TI_flags(%ebp), %ecx
442 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
443 # than syscall tracing?
445 testb $_TIF_NEED_RESCHED, %cl
448 work_notifysig: # deal with pending signals and
449 # notify-resume requests
450 testl $VM_MASK, EFLAGS(%esp)
452 jne work_notifysig_v86 # returning to kernel-space or
455 call do_notify_resume
456 jmp resume_userspace_sig
461 pushl %ecx # save ti_flags for do_notify_resume
462 CFI_ADJUST_CFA_OFFSET 4
463 call save_v86_state # %eax contains pt_regs pointer
465 CFI_ADJUST_CFA_OFFSET -4
468 call do_notify_resume
469 jmp resume_userspace_sig
472 # perform syscall exit tracing
475 movl $-ENOSYS,EAX(%esp)
478 call do_syscall_trace
480 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
481 # so must skip actual syscall
482 movl ORIG_EAX(%esp), %eax
483 cmpl $(nr_syscalls), %eax
487 # perform syscall exit tracing
490 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
493 sti # could let do_syscall_trace() call
497 call do_syscall_trace
501 RING0_INT_FRAME # can't unwind into user space anyway
503 pushl %eax # save orig_eax
504 CFI_ADJUST_CFA_OFFSET 4
506 GET_THREAD_INFO(%ebp)
507 movl $-EFAULT,EAX(%esp)
511 movl $-ENOSYS,EAX(%esp)
515 #define FIXUP_ESPFIX_STACK \
517 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
518 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
519 /* copy data from 16bit stack to 32bit stack */ \
520 call fixup_x86_bogus_stack; \
521 /* put ESP to the proper location */ \
523 #define UNWIND_ESPFIX_STACK \
525 CFI_ADJUST_CFA_OFFSET 4; \
527 /* see if on 16bit stack */ \
528 cmpw $__ESPFIX_SS, %ax; \
531 CFI_ADJUST_CFA_OFFSET -4; \
532 .section .fixup,"ax"; \
533 28: movl $__KERNEL_DS, %eax; \
536 /* switch to 32bit stack */ \
537 FIXUP_ESPFIX_STACK; \
542 * Build the entry stubs and pointer table with
543 * some assembler magic.
550 ENTRY(irq_entries_start)
555 CFI_ADJUST_CFA_OFFSET -4
558 CFI_ADJUST_CFA_OFFSET 4
567 * the CPU automatically disables interrupts when executing an IRQ vector,
568 * so IRQ-flags tracing has to follow that:
579 #define BUILD_INTERRUPT(name, nr) \
583 CFI_ADJUST_CFA_OFFSET 4; \
591 /* The include is where all of the SMP etc. interrupts come from */
592 #include "entry_arch.h"
596 pushl $0 # no error code
597 CFI_ADJUST_CFA_OFFSET 4
598 pushl $do_divide_error
599 CFI_ADJUST_CFA_OFFSET 4
603 CFI_ADJUST_CFA_OFFSET 4
604 /*CFI_REL_OFFSET ds, 0*/
606 CFI_ADJUST_CFA_OFFSET 4
607 CFI_REL_OFFSET eax, 0
610 CFI_ADJUST_CFA_OFFSET 4
611 CFI_REL_OFFSET ebp, 0
613 CFI_ADJUST_CFA_OFFSET 4
614 CFI_REL_OFFSET edi, 0
616 CFI_ADJUST_CFA_OFFSET 4
617 CFI_REL_OFFSET esi, 0
619 CFI_ADJUST_CFA_OFFSET 4
620 CFI_REL_OFFSET edx, 0
623 CFI_ADJUST_CFA_OFFSET 4
624 CFI_REL_OFFSET ecx, 0
626 CFI_ADJUST_CFA_OFFSET 4
627 CFI_REL_OFFSET ebx, 0
630 CFI_ADJUST_CFA_OFFSET 4
631 /*CFI_REL_OFFSET es, 0*/
634 CFI_ADJUST_CFA_OFFSET -4
635 /*CFI_REGISTER es, ecx*/
636 movl ES(%esp), %edi # get the function address
637 movl ORIG_EAX(%esp), %edx # get the error code
638 movl %eax, ORIG_EAX(%esp)
640 /*CFI_REL_OFFSET es, ES*/
641 movl $(__USER_DS), %ecx
644 movl %esp,%eax # pt_regs pointer
646 jmp ret_from_exception
649 ENTRY(coprocessor_error)
652 CFI_ADJUST_CFA_OFFSET 4
653 pushl $do_coprocessor_error
654 CFI_ADJUST_CFA_OFFSET 4
658 ENTRY(simd_coprocessor_error)
661 CFI_ADJUST_CFA_OFFSET 4
662 pushl $do_simd_coprocessor_error
663 CFI_ADJUST_CFA_OFFSET 4
667 ENTRY(device_not_available)
669 pushl $-1 # mark this as an int
670 CFI_ADJUST_CFA_OFFSET 4
673 testl $0x4, %eax # EM (math emulation bit)
674 jne device_not_available_emulate
676 call math_state_restore
677 jmp ret_from_exception
678 device_not_available_emulate:
679 pushl $0 # temporary storage for ORIG_EIP
680 CFI_ADJUST_CFA_OFFSET 4
683 CFI_ADJUST_CFA_OFFSET -4
684 jmp ret_from_exception
688 * Debug traps and NMI can happen at the one SYSENTER instruction
689 * that sets up the real kernel stack. Check here, since we can't
690 * allow the wrong stack to be used.
692 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
693 * already pushed 3 words if it hits on the sysenter instruction:
694 * eflags, cs and eip.
696 * We just load the right stack, and push the three (known) values
697 * by hand onto the new stack - while updating the return eip past
698 * the instruction that would have done it for sysenter.
700 #define FIX_STACK(offset, ok, label) \
701 cmpw $__KERNEL_CS,4(%esp); \
704 movl TSS_sysenter_esp0+offset(%esp),%esp; \
706 pushl $__KERNEL_CS; \
707 pushl $sysenter_past_esp
711 cmpl $sysenter_entry,(%esp)
712 jne debug_stack_correct
713 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
715 pushl $-1 # mark this as an int
716 CFI_ADJUST_CFA_OFFSET 4
718 xorl %edx,%edx # error code 0
719 movl %esp,%eax # pt_regs pointer
721 jmp ret_from_exception
725 * NMI is doubly nasty. It can happen _while_ we're handling
726 * a debug fault, and the debug fault hasn't yet been able to
727 * clear up the stack. So we first check whether we got an
728 * NMI on the sysenter entry path, but after that we need to
729 * check whether we got an NMI on the debug path where the debug
730 * fault happened on the sysenter path.
735 CFI_ADJUST_CFA_OFFSET 4
737 cmpw $__ESPFIX_SS, %ax
739 CFI_ADJUST_CFA_OFFSET -4
741 cmpl $sysenter_entry,(%esp)
744 CFI_ADJUST_CFA_OFFSET 4
746 /* Do not access memory above the end of our stack page,
747 * it might not exist.
749 andl $(THREAD_SIZE-1),%eax
750 cmpl $(THREAD_SIZE-20),%eax
752 CFI_ADJUST_CFA_OFFSET -4
753 jae nmi_stack_correct
754 cmpl $sysenter_entry,12(%esp)
755 je nmi_debug_stack_check
758 CFI_ADJUST_CFA_OFFSET 4
760 xorl %edx,%edx # zero error code
761 movl %esp,%eax # pt_regs pointer
763 jmp restore_nocheck_notrace
767 FIX_STACK(12,nmi_stack_correct, 1)
768 jmp nmi_stack_correct
769 nmi_debug_stack_check:
770 cmpw $__KERNEL_CS,16(%esp)
771 jne nmi_stack_correct
774 cmpl $debug_esp_fix_insn,(%esp)
776 FIX_STACK(24,nmi_stack_correct, 1)
777 jmp nmi_stack_correct
781 /* create the pointer to lss back */
783 CFI_ADJUST_CFA_OFFSET 4
785 CFI_ADJUST_CFA_OFFSET 4
788 /* copy the iret frame of 12 bytes */
791 CFI_ADJUST_CFA_OFFSET 4
794 CFI_ADJUST_CFA_OFFSET 4
796 FIXUP_ESPFIX_STACK # %eax == %esp
797 CFI_ADJUST_CFA_OFFSET -20 # the frame has now moved
798 xorl %edx,%edx # zero error code
801 lss 12+4(%esp), %esp # back to 16bit stack
804 .section __ex_table,"a"
811 pushl $-1 # mark this as an int
812 CFI_ADJUST_CFA_OFFSET 4
814 xorl %edx,%edx # zero error code
815 movl %esp,%eax # pt_regs pointer
817 jmp ret_from_exception
824 CFI_ADJUST_CFA_OFFSET 4
826 CFI_ADJUST_CFA_OFFSET 4
833 CFI_ADJUST_CFA_OFFSET 4
835 CFI_ADJUST_CFA_OFFSET 4
842 CFI_ADJUST_CFA_OFFSET 4
844 CFI_ADJUST_CFA_OFFSET 4
848 ENTRY(coprocessor_segment_overrun)
851 CFI_ADJUST_CFA_OFFSET 4
852 pushl $do_coprocessor_segment_overrun
853 CFI_ADJUST_CFA_OFFSET 4
859 pushl $do_invalid_TSS
860 CFI_ADJUST_CFA_OFFSET 4
864 ENTRY(segment_not_present)
866 pushl $do_segment_not_present
867 CFI_ADJUST_CFA_OFFSET 4
873 pushl $do_stack_segment
874 CFI_ADJUST_CFA_OFFSET 4
878 KPROBE_ENTRY(general_protection)
880 pushl $do_general_protection
881 CFI_ADJUST_CFA_OFFSET 4
886 ENTRY(alignment_check)
888 pushl $do_alignment_check
889 CFI_ADJUST_CFA_OFFSET 4
893 KPROBE_ENTRY(page_fault)
896 CFI_ADJUST_CFA_OFFSET 4
901 #ifdef CONFIG_X86_MCE
905 CFI_ADJUST_CFA_OFFSET 4
906 pushl machine_check_vector
907 CFI_ADJUST_CFA_OFFSET 4
912 ENTRY(spurious_interrupt_bug)
915 CFI_ADJUST_CFA_OFFSET 4
916 pushl $do_spurious_interrupt_bug
917 CFI_ADJUST_CFA_OFFSET 4
921 #ifdef CONFIG_STACK_UNWIND
922 ENTRY(arch_unwind_init_running)
935 movl $__USER_DS, DS(%edx)
936 movl $__USER_DS, ES(%edx)
937 movl %ebx, ORIG_EAX(%edx)
940 movl $__KERNEL_CS, CS(%edx)
941 movl %ebx, EFLAGS(%edx)
942 movl %eax, OLDESP(%edx)
946 movl $__KERNEL_DS, OLDSS(%edx)
949 ENDPROC(arch_unwind_init_running)
953 #include "syscall_table.S"
955 syscall_table_size=(.-sys_call_table)