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
87 #define resume_userspace_sig check_userspace
89 #define resume_userspace_sig resume_userspace
95 CFI_ADJUST_CFA_OFFSET 4;\
96 /*CFI_REL_OFFSET es, 0;*/\
98 CFI_ADJUST_CFA_OFFSET 4;\
99 /*CFI_REL_OFFSET ds, 0;*/\
101 CFI_ADJUST_CFA_OFFSET 4;\
102 CFI_REL_OFFSET eax, 0;\
104 CFI_ADJUST_CFA_OFFSET 4;\
105 CFI_REL_OFFSET ebp, 0;\
107 CFI_ADJUST_CFA_OFFSET 4;\
108 CFI_REL_OFFSET edi, 0;\
110 CFI_ADJUST_CFA_OFFSET 4;\
111 CFI_REL_OFFSET esi, 0;\
113 CFI_ADJUST_CFA_OFFSET 4;\
114 CFI_REL_OFFSET edx, 0;\
116 CFI_ADJUST_CFA_OFFSET 4;\
117 CFI_REL_OFFSET ecx, 0;\
119 CFI_ADJUST_CFA_OFFSET 4;\
120 CFI_REL_OFFSET ebx, 0;\
121 movl $(__USER_DS), %edx; \
125 #define RESTORE_INT_REGS \
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 CFI_ADJUST_CFA_OFFSET -4;\
145 CFI_ADJUST_CFA_OFFSET -4;\
148 #define RESTORE_REGS \
151 CFI_ADJUST_CFA_OFFSET -4;\
154 CFI_ADJUST_CFA_OFFSET -4;\
156 .section .fixup,"ax"; \
162 .section __ex_table,"a";\
168 #define RING0_INT_FRAME \
169 CFI_STARTPROC simple;\
170 CFI_DEF_CFA esp, 3*4;\
171 /*CFI_OFFSET cs, -2*4;*/\
174 #define RING0_EC_FRAME \
175 CFI_STARTPROC simple;\
176 CFI_DEF_CFA esp, 4*4;\
177 /*CFI_OFFSET cs, -2*4;*/\
180 #define RING0_PTREGS_FRAME \
181 CFI_STARTPROC simple;\
182 CFI_DEF_CFA esp, OLDESP-EBX;\
183 /*CFI_OFFSET cs, CS-OLDESP;*/\
184 CFI_OFFSET eip, EIP-OLDESP;\
185 /*CFI_OFFSET es, ES-OLDESP;*/\
186 /*CFI_OFFSET ds, DS-OLDESP;*/\
187 CFI_OFFSET eax, EAX-OLDESP;\
188 CFI_OFFSET ebp, EBP-OLDESP;\
189 CFI_OFFSET edi, EDI-OLDESP;\
190 CFI_OFFSET esi, ESI-OLDESP;\
191 CFI_OFFSET edx, EDX-OLDESP;\
192 CFI_OFFSET ecx, ECX-OLDESP;\
193 CFI_OFFSET ebx, EBX-OLDESP
198 CFI_ADJUST_CFA_OFFSET -4
200 GET_THREAD_INFO(%ebp)
202 CFI_ADJUST_CFA_OFFSET -4
207 * Return to user mode is not as complex as all this looks,
208 * but we want the default path for a system call return to
209 * go as quickly as possible which is why some of this is
210 * less clear than it otherwise should be.
213 # userspace resumption stub bypassing syscall exit tracing
219 GET_THREAD_INFO(%ebp)
221 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
223 testl $(VM_MASK | 3), %eax
225 ENTRY(resume_userspace)
226 cli # make sure we don't miss an interrupt
227 # setting need_resched or sigpending
228 # between sampling and the iret
229 movl TI_flags(%ebp), %ecx
230 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
231 # int/exception return?
235 #ifdef CONFIG_PREEMPT
238 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
241 movl TI_flags(%ebp), %ecx # need_resched set ?
242 testb $_TIF_NEED_RESCHED, %cl
244 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
246 call preempt_schedule_irq
251 /* SYSENTER_RETURN points to after the "sysenter" instruction in
252 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
254 # sysenter call handler stub
255 ENTRY(sysenter_entry)
258 CFI_REGISTER esp, ebp
259 movl TSS_sysenter_esp0(%esp),%esp
263 CFI_ADJUST_CFA_OFFSET 4
264 /*CFI_REL_OFFSET ss, 0*/
266 CFI_ADJUST_CFA_OFFSET 4
267 CFI_REL_OFFSET esp, 0
269 CFI_ADJUST_CFA_OFFSET 4
271 CFI_ADJUST_CFA_OFFSET 4
272 /*CFI_REL_OFFSET cs, 0*/
274 * Push current_thread_info()->sysenter_return to the stack.
275 * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
276 * pushed above; +8 corresponds to copy_thread's esp0 setting.
278 pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
279 CFI_ADJUST_CFA_OFFSET 4
280 CFI_REL_OFFSET eip, 0
283 * Load the potential sixth argument from user stack.
284 * Careful about security.
286 cmpl $__PAGE_OFFSET-3,%ebp
289 .section __ex_table,"a"
291 .long 1b,syscall_fault
295 CFI_ADJUST_CFA_OFFSET 4
297 GET_THREAD_INFO(%ebp)
299 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
300 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
301 jnz syscall_trace_entry
302 cmpl $(nr_syscalls), %eax
304 call *sys_call_table(,%eax,4)
307 movl TI_flags(%ebp), %ecx
308 testw $_TIF_ALLWORK_MASK, %cx
309 jne syscall_exit_work
310 /* if something modifies registers it must also disable sysexit */
312 movl OLDESP(%esp), %ecx
319 # system call handler stub
321 RING0_INT_FRAME # can't unwind into user space anyway
322 pushl %eax # save orig_eax
323 CFI_ADJUST_CFA_OFFSET 4
325 GET_THREAD_INFO(%ebp)
326 testl $TF_MASK,EFLAGS(%esp)
328 orl $_TIF_SINGLESTEP,TI_flags(%ebp)
330 # system call tracing in operation / emulation
331 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
332 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
333 jnz syscall_trace_entry
334 cmpl $(nr_syscalls), %eax
337 call *sys_call_table(,%eax,4)
338 movl %eax,EAX(%esp) # store the return value
340 cli # make sure we don't miss an interrupt
341 # setting need_resched or sigpending
342 # between sampling and the iret
343 movl TI_flags(%ebp), %ecx
344 testw $_TIF_ALLWORK_MASK, %cx # current->work
345 jne syscall_exit_work
348 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
349 # Warning: OLDSS(%esp) contains the wrong/random values if we
350 # are returning to the kernel.
351 # See comments in process.c:copy_thread() for details.
352 movb OLDSS(%esp), %ah
354 andl $(VM_MASK | (4 << 8) | 3), %eax
355 cmpl $((4 << 8) | 3), %eax
357 je ldt_ss # returning to user-space with LDT SS
361 CFI_ADJUST_CFA_OFFSET -4
366 pushl $0 # no error code
370 .section __ex_table,"a"
377 larl OLDSS(%esp), %eax
379 testl $0x00400000, %eax # returning to 32bit stack?
380 jnz restore_nocheck # allright, normal return
381 /* If returning to userspace with 16bit stack,
382 * try to fix the higher word of ESP, as the CPU
384 * This is an "official" bug of all the x86-compatible
385 * CPUs, which we can try to work around to make
386 * dosemu and wine happy. */
387 subl $8, %esp # reserve space for switch16 pointer
388 CFI_ADJUST_CFA_OFFSET 8
391 /* Set up the 16bit stack frame with switch32 pointer on top,
392 * and a switch16 pointer on top of the current frame. */
393 call setup_x86_bogus_stack
394 CFI_ADJUST_CFA_OFFSET -8 # frame has moved
396 lss 20+4(%esp), %esp # switch to 16bit stack
398 .section __ex_table,"a"
404 # perform work that needs to be done immediately before resumption
406 RING0_PTREGS_FRAME # can't unwind into user space anyway
408 testb $_TIF_NEED_RESCHED, %cl
412 cli # make sure we don't miss an interrupt
413 # setting need_resched or sigpending
414 # between sampling and the iret
415 movl TI_flags(%ebp), %ecx
416 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
417 # than syscall tracing?
419 testb $_TIF_NEED_RESCHED, %cl
422 work_notifysig: # deal with pending signals and
423 # notify-resume requests
424 testl $VM_MASK, EFLAGS(%esp)
426 jne work_notifysig_v86 # returning to kernel-space or
429 call do_notify_resume
430 jmp resume_userspace_sig
435 pushl %ecx # save ti_flags for do_notify_resume
436 CFI_ADJUST_CFA_OFFSET 4
437 call save_v86_state # %eax contains pt_regs pointer
439 CFI_ADJUST_CFA_OFFSET -4
442 call do_notify_resume
443 jmp resume_userspace_sig
446 # perform syscall exit tracing
449 movl $-ENOSYS,EAX(%esp)
452 call do_syscall_trace
454 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
455 # so must skip actual syscall
456 movl ORIG_EAX(%esp), %eax
457 cmpl $(nr_syscalls), %eax
461 # perform syscall exit tracing
464 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
466 sti # could let do_syscall_trace() call
470 call do_syscall_trace
474 RING0_INT_FRAME # can't unwind into user space anyway
476 pushl %eax # save orig_eax
477 CFI_ADJUST_CFA_OFFSET 4
479 GET_THREAD_INFO(%ebp)
480 movl $-EFAULT,EAX(%esp)
484 movl $-ENOSYS,EAX(%esp)
488 #define FIXUP_ESPFIX_STACK \
490 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
491 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
492 /* copy data from 16bit stack to 32bit stack */ \
493 call fixup_x86_bogus_stack; \
494 /* put ESP to the proper location */ \
496 #define UNWIND_ESPFIX_STACK \
498 CFI_ADJUST_CFA_OFFSET 4; \
500 /* see if on 16bit stack */ \
501 cmpw $__ESPFIX_SS, %ax; \
504 CFI_ADJUST_CFA_OFFSET -4; \
505 .section .fixup,"ax"; \
506 28: movl $__KERNEL_DS, %eax; \
509 /* switch to 32bit stack */ \
510 FIXUP_ESPFIX_STACK; \
515 * Build the entry stubs and pointer table with
516 * some assembler magic.
523 ENTRY(irq_entries_start)
528 CFI_ADJUST_CFA_OFFSET -4
531 CFI_ADJUST_CFA_OFFSET 4
547 #define BUILD_INTERRUPT(name, nr) \
551 CFI_ADJUST_CFA_OFFSET 4; \
558 /* The include is where all of the SMP etc. interrupts come from */
559 #include "entry_arch.h"
563 pushl $0 # no error code
564 CFI_ADJUST_CFA_OFFSET 4
565 pushl $do_divide_error
566 CFI_ADJUST_CFA_OFFSET 4
570 CFI_ADJUST_CFA_OFFSET 4
571 /*CFI_REL_OFFSET ds, 0*/
573 CFI_ADJUST_CFA_OFFSET 4
574 CFI_REL_OFFSET eax, 0
577 CFI_ADJUST_CFA_OFFSET 4
578 CFI_REL_OFFSET ebp, 0
580 CFI_ADJUST_CFA_OFFSET 4
581 CFI_REL_OFFSET edi, 0
583 CFI_ADJUST_CFA_OFFSET 4
584 CFI_REL_OFFSET esi, 0
586 CFI_ADJUST_CFA_OFFSET 4
587 CFI_REL_OFFSET edx, 0
590 CFI_ADJUST_CFA_OFFSET 4
591 CFI_REL_OFFSET ecx, 0
593 CFI_ADJUST_CFA_OFFSET 4
594 CFI_REL_OFFSET ebx, 0
597 CFI_ADJUST_CFA_OFFSET 4
598 /*CFI_REL_OFFSET es, 0*/
601 CFI_ADJUST_CFA_OFFSET -4
602 /*CFI_REGISTER es, ecx*/
603 movl ES(%esp), %edi # get the function address
604 movl ORIG_EAX(%esp), %edx # get the error code
605 movl %eax, ORIG_EAX(%esp)
607 /*CFI_REL_OFFSET es, ES*/
608 movl $(__USER_DS), %ecx
611 movl %esp,%eax # pt_regs pointer
613 jmp ret_from_exception
616 ENTRY(coprocessor_error)
619 CFI_ADJUST_CFA_OFFSET 4
620 pushl $do_coprocessor_error
621 CFI_ADJUST_CFA_OFFSET 4
625 ENTRY(simd_coprocessor_error)
628 CFI_ADJUST_CFA_OFFSET 4
629 pushl $do_simd_coprocessor_error
630 CFI_ADJUST_CFA_OFFSET 4
634 ENTRY(device_not_available)
636 pushl $-1 # mark this as an int
637 CFI_ADJUST_CFA_OFFSET 4
640 testl $0x4, %eax # EM (math emulation bit)
641 jne device_not_available_emulate
643 call math_state_restore
644 jmp ret_from_exception
645 device_not_available_emulate:
646 pushl $0 # temporary storage for ORIG_EIP
647 CFI_ADJUST_CFA_OFFSET 4
650 CFI_ADJUST_CFA_OFFSET -4
651 jmp ret_from_exception
655 * Debug traps and NMI can happen at the one SYSENTER instruction
656 * that sets up the real kernel stack. Check here, since we can't
657 * allow the wrong stack to be used.
659 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
660 * already pushed 3 words if it hits on the sysenter instruction:
661 * eflags, cs and eip.
663 * We just load the right stack, and push the three (known) values
664 * by hand onto the new stack - while updating the return eip past
665 * the instruction that would have done it for sysenter.
667 #define FIX_STACK(offset, ok, label) \
668 cmpw $__KERNEL_CS,4(%esp); \
671 movl TSS_sysenter_esp0+offset(%esp),%esp; \
673 pushl $__KERNEL_CS; \
674 pushl $sysenter_past_esp
678 cmpl $sysenter_entry,(%esp)
679 jne debug_stack_correct
680 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
682 pushl $-1 # mark this as an int
683 CFI_ADJUST_CFA_OFFSET 4
685 xorl %edx,%edx # error code 0
686 movl %esp,%eax # pt_regs pointer
688 jmp ret_from_exception
692 * NMI is doubly nasty. It can happen _while_ we're handling
693 * a debug fault, and the debug fault hasn't yet been able to
694 * clear up the stack. So we first check whether we got an
695 * NMI on the sysenter entry path, but after that we need to
696 * check whether we got an NMI on the debug path where the debug
697 * fault happened on the sysenter path.
702 CFI_ADJUST_CFA_OFFSET 4
704 cmpw $__ESPFIX_SS, %ax
706 CFI_ADJUST_CFA_OFFSET -4
708 cmpl $sysenter_entry,(%esp)
711 CFI_ADJUST_CFA_OFFSET 4
713 /* Do not access memory above the end of our stack page,
714 * it might not exist.
716 andl $(THREAD_SIZE-1),%eax
717 cmpl $(THREAD_SIZE-20),%eax
719 CFI_ADJUST_CFA_OFFSET -4
720 jae nmi_stack_correct
721 cmpl $sysenter_entry,12(%esp)
722 je nmi_debug_stack_check
725 CFI_ADJUST_CFA_OFFSET 4
727 xorl %edx,%edx # zero error code
728 movl %esp,%eax # pt_regs pointer
734 FIX_STACK(12,nmi_stack_correct, 1)
735 jmp nmi_stack_correct
736 nmi_debug_stack_check:
737 cmpw $__KERNEL_CS,16(%esp)
738 jne nmi_stack_correct
741 cmpl $debug_esp_fix_insn,(%esp)
743 FIX_STACK(24,nmi_stack_correct, 1)
744 jmp nmi_stack_correct
748 /* create the pointer to lss back */
750 CFI_ADJUST_CFA_OFFSET 4
752 CFI_ADJUST_CFA_OFFSET 4
755 /* copy the iret frame of 12 bytes */
758 CFI_ADJUST_CFA_OFFSET 4
761 CFI_ADJUST_CFA_OFFSET 4
763 FIXUP_ESPFIX_STACK # %eax == %esp
764 CFI_ADJUST_CFA_OFFSET -20 # the frame has now moved
765 xorl %edx,%edx # zero error code
768 lss 12+4(%esp), %esp # back to 16bit stack
771 .section __ex_table,"a"
778 pushl $-1 # mark this as an int
779 CFI_ADJUST_CFA_OFFSET 4
781 xorl %edx,%edx # zero error code
782 movl %esp,%eax # pt_regs pointer
784 jmp ret_from_exception
791 CFI_ADJUST_CFA_OFFSET 4
793 CFI_ADJUST_CFA_OFFSET 4
800 CFI_ADJUST_CFA_OFFSET 4
802 CFI_ADJUST_CFA_OFFSET 4
809 CFI_ADJUST_CFA_OFFSET 4
811 CFI_ADJUST_CFA_OFFSET 4
815 ENTRY(coprocessor_segment_overrun)
818 CFI_ADJUST_CFA_OFFSET 4
819 pushl $do_coprocessor_segment_overrun
820 CFI_ADJUST_CFA_OFFSET 4
826 pushl $do_invalid_TSS
827 CFI_ADJUST_CFA_OFFSET 4
831 ENTRY(segment_not_present)
833 pushl $do_segment_not_present
834 CFI_ADJUST_CFA_OFFSET 4
840 pushl $do_stack_segment
841 CFI_ADJUST_CFA_OFFSET 4
845 KPROBE_ENTRY(general_protection)
847 pushl $do_general_protection
848 CFI_ADJUST_CFA_OFFSET 4
853 ENTRY(alignment_check)
855 pushl $do_alignment_check
856 CFI_ADJUST_CFA_OFFSET 4
860 KPROBE_ENTRY(page_fault)
863 CFI_ADJUST_CFA_OFFSET 4
868 #ifdef CONFIG_X86_MCE
872 CFI_ADJUST_CFA_OFFSET 4
873 pushl machine_check_vector
874 CFI_ADJUST_CFA_OFFSET 4
879 ENTRY(spurious_interrupt_bug)
882 CFI_ADJUST_CFA_OFFSET 4
883 pushl $do_spurious_interrupt_bug
884 CFI_ADJUST_CFA_OFFSET 4
888 #ifdef CONFIG_STACK_UNWIND
889 ENTRY(arch_unwind_init_running)
902 movl $__USER_DS, DS(%edx)
903 movl $__USER_DS, ES(%edx)
904 movl %ebx, ORIG_EAX(%edx)
907 movl $__KERNEL_CS, CS(%edx)
908 movl %ebx, EFLAGS(%edx)
909 movl %eax, OLDESP(%edx)
913 movl $__KERNEL_DS, OLDSS(%edx)
916 ENDPROC(arch_unwind_init_running)
920 #include "syscall_table.S"
922 syscall_table_size=(.-sys_call_table)