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/config.h>
44 #include <linux/linkage.h>
45 #include <asm/thread_info.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
83 #define resume_kernel restore_nocheck
89 CFI_ADJUST_CFA_OFFSET 4;\
90 /*CFI_REL_OFFSET es, 0;*/\
92 CFI_ADJUST_CFA_OFFSET 4;\
93 /*CFI_REL_OFFSET ds, 0;*/\
95 CFI_ADJUST_CFA_OFFSET 4;\
96 CFI_REL_OFFSET eax, 0;\
98 CFI_ADJUST_CFA_OFFSET 4;\
99 CFI_REL_OFFSET ebp, 0;\
101 CFI_ADJUST_CFA_OFFSET 4;\
102 CFI_REL_OFFSET edi, 0;\
104 CFI_ADJUST_CFA_OFFSET 4;\
105 CFI_REL_OFFSET esi, 0;\
107 CFI_ADJUST_CFA_OFFSET 4;\
108 CFI_REL_OFFSET edx, 0;\
110 CFI_ADJUST_CFA_OFFSET 4;\
111 CFI_REL_OFFSET ecx, 0;\
113 CFI_ADJUST_CFA_OFFSET 4;\
114 CFI_REL_OFFSET ebx, 0;\
115 movl $(__USER_DS), %edx; \
119 #define RESTORE_INT_REGS \
121 CFI_ADJUST_CFA_OFFSET -4;\
124 CFI_ADJUST_CFA_OFFSET -4;\
127 CFI_ADJUST_CFA_OFFSET -4;\
130 CFI_ADJUST_CFA_OFFSET -4;\
133 CFI_ADJUST_CFA_OFFSET -4;\
136 CFI_ADJUST_CFA_OFFSET -4;\
139 CFI_ADJUST_CFA_OFFSET -4;\
142 #define RESTORE_REGS \
145 CFI_ADJUST_CFA_OFFSET -4;\
148 CFI_ADJUST_CFA_OFFSET -4;\
150 .section .fixup,"ax"; \
156 .section __ex_table,"a";\
162 #define RING0_INT_FRAME \
163 CFI_STARTPROC simple;\
164 CFI_DEF_CFA esp, 3*4;\
165 /*CFI_OFFSET cs, -2*4;*/\
168 #define RING0_EC_FRAME \
169 CFI_STARTPROC simple;\
170 CFI_DEF_CFA esp, 4*4;\
171 /*CFI_OFFSET cs, -2*4;*/\
174 #define RING0_PTREGS_FRAME \
175 CFI_STARTPROC simple;\
176 CFI_DEF_CFA esp, OLDESP-EBX;\
177 /*CFI_OFFSET cs, CS-OLDESP;*/\
178 CFI_OFFSET eip, EIP-OLDESP;\
179 /*CFI_OFFSET es, ES-OLDESP;*/\
180 /*CFI_OFFSET ds, DS-OLDESP;*/\
181 CFI_OFFSET eax, EAX-OLDESP;\
182 CFI_OFFSET ebp, EBP-OLDESP;\
183 CFI_OFFSET edi, EDI-OLDESP;\
184 CFI_OFFSET esi, ESI-OLDESP;\
185 CFI_OFFSET edx, EDX-OLDESP;\
186 CFI_OFFSET ecx, ECX-OLDESP;\
187 CFI_OFFSET ebx, EBX-OLDESP
192 CFI_ADJUST_CFA_OFFSET -4
194 GET_THREAD_INFO(%ebp)
196 CFI_ADJUST_CFA_OFFSET -4
201 * Return to user mode is not as complex as all this looks,
202 * but we want the default path for a system call return to
203 * go as quickly as possible which is why some of this is
204 * less clear than it otherwise should be.
207 # userspace resumption stub bypassing syscall exit tracing
213 GET_THREAD_INFO(%ebp)
214 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
216 testl $(VM_MASK | 3), %eax
218 ENTRY(resume_userspace)
219 cli # make sure we don't miss an interrupt
220 # setting need_resched or sigpending
221 # between sampling and the iret
222 movl TI_flags(%ebp), %ecx
223 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
224 # int/exception return?
228 #ifdef CONFIG_PREEMPT
231 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
234 movl TI_flags(%ebp), %ecx # need_resched set ?
235 testb $_TIF_NEED_RESCHED, %cl
237 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
239 call preempt_schedule_irq
244 /* SYSENTER_RETURN points to after the "sysenter" instruction in
245 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
247 # sysenter call handler stub
248 ENTRY(sysenter_entry)
251 CFI_REGISTER esp, ebp
252 movl TSS_sysenter_esp0(%esp),%esp
256 CFI_ADJUST_CFA_OFFSET 4
257 /*CFI_REL_OFFSET ss, 0*/
259 CFI_ADJUST_CFA_OFFSET 4
260 CFI_REL_OFFSET esp, 0
262 CFI_ADJUST_CFA_OFFSET 4
264 CFI_ADJUST_CFA_OFFSET 4
265 /*CFI_REL_OFFSET cs, 0*/
266 pushl $SYSENTER_RETURN
267 CFI_ADJUST_CFA_OFFSET 4
268 CFI_REL_OFFSET eip, 0
271 * Load the potential sixth argument from user stack.
272 * Careful about security.
274 cmpl $__PAGE_OFFSET-3,%ebp
277 .section __ex_table,"a"
279 .long 1b,syscall_fault
283 CFI_ADJUST_CFA_OFFSET 4
285 GET_THREAD_INFO(%ebp)
287 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
288 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
289 jnz syscall_trace_entry
290 cmpl $(nr_syscalls), %eax
292 call *sys_call_table(,%eax,4)
295 movl TI_flags(%ebp), %ecx
296 testw $_TIF_ALLWORK_MASK, %cx
297 jne syscall_exit_work
298 /* if something modifies registers it must also disable sysexit */
300 movl OLDESP(%esp), %ecx
307 # system call handler stub
309 RING0_INT_FRAME # can't unwind into user space anyway
310 pushl %eax # save orig_eax
311 CFI_ADJUST_CFA_OFFSET 4
313 GET_THREAD_INFO(%ebp)
314 testl $TF_MASK,EFLAGS(%esp)
316 orl $_TIF_SINGLESTEP,TI_flags(%ebp)
318 # system call tracing in operation / emulation
319 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
320 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
321 jnz syscall_trace_entry
322 cmpl $(nr_syscalls), %eax
325 call *sys_call_table(,%eax,4)
326 movl %eax,EAX(%esp) # store the return value
328 cli # make sure we don't miss an interrupt
329 # setting need_resched or sigpending
330 # between sampling and the iret
331 movl TI_flags(%ebp), %ecx
332 testw $_TIF_ALLWORK_MASK, %cx # current->work
333 jne syscall_exit_work
336 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
337 # Warning: OLDSS(%esp) contains the wrong/random values if we
338 # are returning to the kernel.
339 # See comments in process.c:copy_thread() for details.
340 movb OLDSS(%esp), %ah
342 andl $(VM_MASK | (4 << 8) | 3), %eax
343 cmpl $((4 << 8) | 3), %eax
345 je ldt_ss # returning to user-space with LDT SS
349 CFI_ADJUST_CFA_OFFSET -4
354 pushl $0 # no error code
358 .section __ex_table,"a"
365 larl OLDSS(%esp), %eax
367 testl $0x00400000, %eax # returning to 32bit stack?
368 jnz restore_nocheck # allright, normal return
369 /* If returning to userspace with 16bit stack,
370 * try to fix the higher word of ESP, as the CPU
372 * This is an "official" bug of all the x86-compatible
373 * CPUs, which we can try to work around to make
374 * dosemu and wine happy. */
375 subl $8, %esp # reserve space for switch16 pointer
376 CFI_ADJUST_CFA_OFFSET 8
379 /* Set up the 16bit stack frame with switch32 pointer on top,
380 * and a switch16 pointer on top of the current frame. */
381 call setup_x86_bogus_stack
382 CFI_ADJUST_CFA_OFFSET -8 # frame has moved
384 lss 20+4(%esp), %esp # switch to 16bit stack
386 .section __ex_table,"a"
392 # perform work that needs to be done immediately before resumption
394 RING0_PTREGS_FRAME # can't unwind into user space anyway
396 testb $_TIF_NEED_RESCHED, %cl
400 cli # make sure we don't miss an interrupt
401 # setting need_resched or sigpending
402 # between sampling and the iret
403 movl TI_flags(%ebp), %ecx
404 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
405 # than syscall tracing?
407 testb $_TIF_NEED_RESCHED, %cl
410 work_notifysig: # deal with pending signals and
411 # notify-resume requests
412 testl $VM_MASK, EFLAGS(%esp)
414 jne work_notifysig_v86 # returning to kernel-space or
417 call do_notify_resume
423 pushl %ecx # save ti_flags for do_notify_resume
424 CFI_ADJUST_CFA_OFFSET 4
425 call save_v86_state # %eax contains pt_regs pointer
427 CFI_ADJUST_CFA_OFFSET -4
430 call do_notify_resume
434 # perform syscall exit tracing
437 movl $-ENOSYS,EAX(%esp)
440 call do_syscall_trace
442 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
443 # so must skip actual syscall
444 movl ORIG_EAX(%esp), %eax
445 cmpl $(nr_syscalls), %eax
449 # perform syscall exit tracing
452 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
454 sti # could let do_syscall_trace() call
458 call do_syscall_trace
462 RING0_INT_FRAME # can't unwind into user space anyway
464 pushl %eax # save orig_eax
465 CFI_ADJUST_CFA_OFFSET 4
467 GET_THREAD_INFO(%ebp)
468 movl $-EFAULT,EAX(%esp)
472 movl $-ENOSYS,EAX(%esp)
476 #define FIXUP_ESPFIX_STACK \
478 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
479 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
480 /* copy data from 16bit stack to 32bit stack */ \
481 call fixup_x86_bogus_stack; \
482 /* put ESP to the proper location */ \
484 #define UNWIND_ESPFIX_STACK \
486 CFI_ADJUST_CFA_OFFSET 4; \
488 /* see if on 16bit stack */ \
489 cmpw $__ESPFIX_SS, %ax; \
492 CFI_ADJUST_CFA_OFFSET -4; \
493 .section .fixup,"ax"; \
494 28: movl $__KERNEL_DS, %eax; \
497 /* switch to 32bit stack */ \
498 FIXUP_ESPFIX_STACK; \
503 * Build the entry stubs and pointer table with
504 * some assembler magic.
511 ENTRY(irq_entries_start)
516 CFI_ADJUST_CFA_OFFSET -4
519 CFI_ADJUST_CFA_OFFSET 4
535 #define BUILD_INTERRUPT(name, nr) \
539 CFI_ADJUST_CFA_OFFSET 4; \
546 /* The include is where all of the SMP etc. interrupts come from */
547 #include "entry_arch.h"
551 pushl $0 # no error code
552 CFI_ADJUST_CFA_OFFSET 4
553 pushl $do_divide_error
554 CFI_ADJUST_CFA_OFFSET 4
558 CFI_ADJUST_CFA_OFFSET 4
559 /*CFI_REL_OFFSET ds, 0*/
561 CFI_ADJUST_CFA_OFFSET 4
562 CFI_REL_OFFSET eax, 0
565 CFI_ADJUST_CFA_OFFSET 4
566 CFI_REL_OFFSET ebp, 0
568 CFI_ADJUST_CFA_OFFSET 4
569 CFI_REL_OFFSET edi, 0
571 CFI_ADJUST_CFA_OFFSET 4
572 CFI_REL_OFFSET esi, 0
574 CFI_ADJUST_CFA_OFFSET 4
575 CFI_REL_OFFSET edx, 0
578 CFI_ADJUST_CFA_OFFSET 4
579 CFI_REL_OFFSET ecx, 0
581 CFI_ADJUST_CFA_OFFSET 4
582 CFI_REL_OFFSET ebx, 0
585 CFI_ADJUST_CFA_OFFSET 4
586 /*CFI_REL_OFFSET es, 0*/
589 CFI_ADJUST_CFA_OFFSET -4
590 /*CFI_REGISTER es, ecx*/
591 movl ES(%esp), %edi # get the function address
592 movl ORIG_EAX(%esp), %edx # get the error code
593 movl %eax, ORIG_EAX(%esp)
595 /*CFI_REL_OFFSET es, ES*/
596 movl $(__USER_DS), %ecx
599 movl %esp,%eax # pt_regs pointer
601 jmp ret_from_exception
604 ENTRY(coprocessor_error)
607 CFI_ADJUST_CFA_OFFSET 4
608 pushl $do_coprocessor_error
609 CFI_ADJUST_CFA_OFFSET 4
613 ENTRY(simd_coprocessor_error)
616 CFI_ADJUST_CFA_OFFSET 4
617 pushl $do_simd_coprocessor_error
618 CFI_ADJUST_CFA_OFFSET 4
622 ENTRY(device_not_available)
624 pushl $-1 # mark this as an int
625 CFI_ADJUST_CFA_OFFSET 4
628 testl $0x4, %eax # EM (math emulation bit)
629 jne device_not_available_emulate
631 call math_state_restore
632 jmp ret_from_exception
633 device_not_available_emulate:
634 pushl $0 # temporary storage for ORIG_EIP
635 CFI_ADJUST_CFA_OFFSET 4
638 CFI_ADJUST_CFA_OFFSET -4
639 jmp ret_from_exception
643 * Debug traps and NMI can happen at the one SYSENTER instruction
644 * that sets up the real kernel stack. Check here, since we can't
645 * allow the wrong stack to be used.
647 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
648 * already pushed 3 words if it hits on the sysenter instruction:
649 * eflags, cs and eip.
651 * We just load the right stack, and push the three (known) values
652 * by hand onto the new stack - while updating the return eip past
653 * the instruction that would have done it for sysenter.
655 #define FIX_STACK(offset, ok, label) \
656 cmpw $__KERNEL_CS,4(%esp); \
659 movl TSS_sysenter_esp0+offset(%esp),%esp; \
661 pushl $__KERNEL_CS; \
662 pushl $sysenter_past_esp
666 cmpl $sysenter_entry,(%esp)
667 jne debug_stack_correct
668 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
670 pushl $-1 # mark this as an int
671 CFI_ADJUST_CFA_OFFSET 4
673 xorl %edx,%edx # error code 0
674 movl %esp,%eax # pt_regs pointer
676 jmp ret_from_exception
680 * NMI is doubly nasty. It can happen _while_ we're handling
681 * a debug fault, and the debug fault hasn't yet been able to
682 * clear up the stack. So we first check whether we got an
683 * NMI on the sysenter entry path, but after that we need to
684 * check whether we got an NMI on the debug path where the debug
685 * fault happened on the sysenter path.
690 CFI_ADJUST_CFA_OFFSET 4
692 cmpw $__ESPFIX_SS, %ax
694 CFI_ADJUST_CFA_OFFSET -4
696 cmpl $sysenter_entry,(%esp)
699 CFI_ADJUST_CFA_OFFSET 4
701 /* Do not access memory above the end of our stack page,
702 * it might not exist.
704 andl $(THREAD_SIZE-1),%eax
705 cmpl $(THREAD_SIZE-20),%eax
707 CFI_ADJUST_CFA_OFFSET -4
708 jae nmi_stack_correct
709 cmpl $sysenter_entry,12(%esp)
710 je nmi_debug_stack_check
713 CFI_ADJUST_CFA_OFFSET 4
715 xorl %edx,%edx # zero error code
716 movl %esp,%eax # pt_regs pointer
722 FIX_STACK(12,nmi_stack_correct, 1)
723 jmp nmi_stack_correct
724 nmi_debug_stack_check:
725 cmpw $__KERNEL_CS,16(%esp)
726 jne nmi_stack_correct
729 cmpl $debug_esp_fix_insn,(%esp)
731 FIX_STACK(24,nmi_stack_correct, 1)
732 jmp nmi_stack_correct
736 /* create the pointer to lss back */
738 CFI_ADJUST_CFA_OFFSET 4
740 CFI_ADJUST_CFA_OFFSET 4
743 /* copy the iret frame of 12 bytes */
746 CFI_ADJUST_CFA_OFFSET 4
749 CFI_ADJUST_CFA_OFFSET 4
751 FIXUP_ESPFIX_STACK # %eax == %esp
752 CFI_ADJUST_CFA_OFFSET -20 # the frame has now moved
753 xorl %edx,%edx # zero error code
756 lss 12+4(%esp), %esp # back to 16bit stack
759 .section __ex_table,"a"
766 pushl $-1 # mark this as an int
767 CFI_ADJUST_CFA_OFFSET 4
769 xorl %edx,%edx # zero error code
770 movl %esp,%eax # pt_regs pointer
772 jmp ret_from_exception
779 CFI_ADJUST_CFA_OFFSET 4
781 CFI_ADJUST_CFA_OFFSET 4
788 CFI_ADJUST_CFA_OFFSET 4
790 CFI_ADJUST_CFA_OFFSET 4
797 CFI_ADJUST_CFA_OFFSET 4
799 CFI_ADJUST_CFA_OFFSET 4
803 ENTRY(coprocessor_segment_overrun)
806 CFI_ADJUST_CFA_OFFSET 4
807 pushl $do_coprocessor_segment_overrun
808 CFI_ADJUST_CFA_OFFSET 4
814 pushl $do_invalid_TSS
815 CFI_ADJUST_CFA_OFFSET 4
819 ENTRY(segment_not_present)
821 pushl $do_segment_not_present
822 CFI_ADJUST_CFA_OFFSET 4
828 pushl $do_stack_segment
829 CFI_ADJUST_CFA_OFFSET 4
833 KPROBE_ENTRY(general_protection)
835 pushl $do_general_protection
836 CFI_ADJUST_CFA_OFFSET 4
841 ENTRY(alignment_check)
843 pushl $do_alignment_check
844 CFI_ADJUST_CFA_OFFSET 4
848 KPROBE_ENTRY(page_fault)
851 CFI_ADJUST_CFA_OFFSET 4
856 #ifdef CONFIG_X86_MCE
860 CFI_ADJUST_CFA_OFFSET 4
861 pushl machine_check_vector
862 CFI_ADJUST_CFA_OFFSET 4
867 ENTRY(spurious_interrupt_bug)
870 CFI_ADJUST_CFA_OFFSET 4
871 pushl $do_spurious_interrupt_bug
872 CFI_ADJUST_CFA_OFFSET 4
876 #ifdef CONFIG_STACK_UNWIND
877 ENTRY(arch_unwind_init_running)
890 movl $__USER_DS, DS(%edx)
891 movl $__USER_DS, ES(%edx)
892 movl %ebx, ORIG_EAX(%edx)
895 movl $__KERNEL_CS, CS(%edx)
896 movl %ebx, EFLAGS(%edx)
897 movl %eax, OLDESP(%edx)
901 movl $__KERNEL_DS, OLDSS(%edx)
904 ENDPROC(arch_unwind_init_running)
908 #include "syscall_table.S"
910 syscall_table_size=(.-sys_call_table)