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)
79 /* These are replaces for paravirtualization */
80 #define DISABLE_INTERRUPTS cli
81 #define ENABLE_INTERRUPTS sti
82 #define ENABLE_INTERRUPTS_SYSEXIT sti; sysexit
83 #define INTERRUPT_RETURN iret
84 #define GET_CR0_INTO_EAX movl %cr0, %eax
87 #define preempt_stop DISABLE_INTERRUPTS; TRACE_IRQS_OFF
90 #define resume_kernel restore_nocheck
93 .macro TRACE_IRQS_IRET
94 #ifdef CONFIG_TRACE_IRQFLAGS
95 testl $IF_MASK,EFLAGS(%esp) # interrupts off?
103 #define resume_userspace_sig check_userspace
105 #define resume_userspace_sig resume_userspace
111 CFI_ADJUST_CFA_OFFSET 4;\
112 /*CFI_REL_OFFSET es, 0;*/\
114 CFI_ADJUST_CFA_OFFSET 4;\
115 /*CFI_REL_OFFSET ds, 0;*/\
117 CFI_ADJUST_CFA_OFFSET 4;\
118 CFI_REL_OFFSET eax, 0;\
120 CFI_ADJUST_CFA_OFFSET 4;\
121 CFI_REL_OFFSET ebp, 0;\
123 CFI_ADJUST_CFA_OFFSET 4;\
124 CFI_REL_OFFSET edi, 0;\
126 CFI_ADJUST_CFA_OFFSET 4;\
127 CFI_REL_OFFSET esi, 0;\
129 CFI_ADJUST_CFA_OFFSET 4;\
130 CFI_REL_OFFSET edx, 0;\
132 CFI_ADJUST_CFA_OFFSET 4;\
133 CFI_REL_OFFSET ecx, 0;\
135 CFI_ADJUST_CFA_OFFSET 4;\
136 CFI_REL_OFFSET ebx, 0;\
137 movl $(__USER_DS), %edx; \
141 #define RESTORE_INT_REGS \
143 CFI_ADJUST_CFA_OFFSET -4;\
146 CFI_ADJUST_CFA_OFFSET -4;\
149 CFI_ADJUST_CFA_OFFSET -4;\
152 CFI_ADJUST_CFA_OFFSET -4;\
155 CFI_ADJUST_CFA_OFFSET -4;\
158 CFI_ADJUST_CFA_OFFSET -4;\
161 CFI_ADJUST_CFA_OFFSET -4;\
164 #define RESTORE_REGS \
167 CFI_ADJUST_CFA_OFFSET -4;\
170 CFI_ADJUST_CFA_OFFSET -4;\
172 .section .fixup,"ax"; \
178 .section __ex_table,"a";\
184 #define RING0_INT_FRAME \
185 CFI_STARTPROC simple;\
187 CFI_DEF_CFA esp, 3*4;\
188 /*CFI_OFFSET cs, -2*4;*/\
191 #define RING0_EC_FRAME \
192 CFI_STARTPROC simple;\
194 CFI_DEF_CFA esp, 4*4;\
195 /*CFI_OFFSET cs, -2*4;*/\
198 #define RING0_PTREGS_FRAME \
199 CFI_STARTPROC simple;\
201 CFI_DEF_CFA esp, OLDESP-EBX;\
202 /*CFI_OFFSET cs, CS-OLDESP;*/\
203 CFI_OFFSET eip, EIP-OLDESP;\
204 /*CFI_OFFSET es, ES-OLDESP;*/\
205 /*CFI_OFFSET ds, DS-OLDESP;*/\
206 CFI_OFFSET eax, EAX-OLDESP;\
207 CFI_OFFSET ebp, EBP-OLDESP;\
208 CFI_OFFSET edi, EDI-OLDESP;\
209 CFI_OFFSET esi, ESI-OLDESP;\
210 CFI_OFFSET edx, EDX-OLDESP;\
211 CFI_OFFSET ecx, ECX-OLDESP;\
212 CFI_OFFSET ebx, EBX-OLDESP
217 CFI_ADJUST_CFA_OFFSET 4
219 GET_THREAD_INFO(%ebp)
221 CFI_ADJUST_CFA_OFFSET -4
222 pushl $0x0202 # Reset kernel eflags
223 CFI_ADJUST_CFA_OFFSET 4
225 CFI_ADJUST_CFA_OFFSET -4
230 * Return to user mode is not as complex as all this looks,
231 * but we want the default path for a system call return to
232 * go as quickly as possible which is why some of this is
233 * less clear than it otherwise should be.
236 # userspace resumption stub bypassing syscall exit tracing
242 GET_THREAD_INFO(%ebp)
244 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
246 andl $(VM_MASK | SEGMENT_RPL_MASK), %eax
248 jb resume_kernel # not returning to v8086 or userspace
249 ENTRY(resume_userspace)
250 DISABLE_INTERRUPTS # make sure we don't miss an interrupt
251 # setting need_resched or sigpending
252 # between sampling and the iret
253 movl TI_flags(%ebp), %ecx
254 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
255 # int/exception return?
259 #ifdef CONFIG_PREEMPT
262 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
265 movl TI_flags(%ebp), %ecx # need_resched set ?
266 testb $_TIF_NEED_RESCHED, %cl
268 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
270 call preempt_schedule_irq
275 /* SYSENTER_RETURN points to after the "sysenter" instruction in
276 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
278 # sysenter call handler stub
279 ENTRY(sysenter_entry)
283 CFI_REGISTER esp, ebp
284 movl TSS_sysenter_esp0(%esp),%esp
287 * No need to follow this irqs on/off section: the syscall
288 * disabled irqs and here we enable it straight after entry:
292 CFI_ADJUST_CFA_OFFSET 4
293 /*CFI_REL_OFFSET ss, 0*/
295 CFI_ADJUST_CFA_OFFSET 4
296 CFI_REL_OFFSET esp, 0
298 CFI_ADJUST_CFA_OFFSET 4
300 CFI_ADJUST_CFA_OFFSET 4
301 /*CFI_REL_OFFSET cs, 0*/
303 * Push current_thread_info()->sysenter_return to the stack.
304 * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
305 * pushed above; +8 corresponds to copy_thread's esp0 setting.
307 pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
308 CFI_ADJUST_CFA_OFFSET 4
309 CFI_REL_OFFSET eip, 0
312 * Load the potential sixth argument from user stack.
313 * Careful about security.
315 cmpl $__PAGE_OFFSET-3,%ebp
318 .section __ex_table,"a"
320 .long 1b,syscall_fault
324 CFI_ADJUST_CFA_OFFSET 4
326 GET_THREAD_INFO(%ebp)
328 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
329 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
330 jnz syscall_trace_entry
331 cmpl $(nr_syscalls), %eax
333 call *sys_call_table(,%eax,4)
337 movl TI_flags(%ebp), %ecx
338 testw $_TIF_ALLWORK_MASK, %cx
339 jne syscall_exit_work
340 /* if something modifies registers it must also disable sysexit */
342 movl OLDESP(%esp), %ecx
345 ENABLE_INTERRUPTS_SYSEXIT
349 # system call handler stub
351 RING0_INT_FRAME # can't unwind into user space anyway
352 pushl %eax # save orig_eax
353 CFI_ADJUST_CFA_OFFSET 4
355 GET_THREAD_INFO(%ebp)
356 testl $TF_MASK,EFLAGS(%esp)
358 orl $_TIF_SINGLESTEP,TI_flags(%ebp)
360 # system call tracing in operation / emulation
361 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
362 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
363 jnz syscall_trace_entry
364 cmpl $(nr_syscalls), %eax
367 call *sys_call_table(,%eax,4)
368 movl %eax,EAX(%esp) # store the return value
370 DISABLE_INTERRUPTS # make sure we don't miss an interrupt
371 # setting need_resched or sigpending
372 # between sampling and the iret
374 movl TI_flags(%ebp), %ecx
375 testw $_TIF_ALLWORK_MASK, %cx # current->work
376 jne syscall_exit_work
379 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
380 # Warning: OLDSS(%esp) contains the wrong/random values if we
381 # are returning to the kernel.
382 # See comments in process.c:copy_thread() for details.
383 movb OLDSS(%esp), %ah
385 andl $(VM_MASK | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
386 cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
388 je ldt_ss # returning to user-space with LDT SS
391 restore_nocheck_notrace:
394 CFI_ADJUST_CFA_OFFSET -4
400 pushl $0 # no error code
404 .section __ex_table,"a"
411 larl OLDSS(%esp), %eax
413 testl $0x00400000, %eax # returning to 32bit stack?
414 jnz restore_nocheck # allright, normal return
415 /* If returning to userspace with 16bit stack,
416 * try to fix the higher word of ESP, as the CPU
418 * This is an "official" bug of all the x86-compatible
419 * CPUs, which we can try to work around to make
420 * dosemu and wine happy. */
421 subl $8, %esp # reserve space for switch16 pointer
422 CFI_ADJUST_CFA_OFFSET 8
426 /* Set up the 16bit stack frame with switch32 pointer on top,
427 * and a switch16 pointer on top of the current frame. */
428 call setup_x86_bogus_stack
429 CFI_ADJUST_CFA_OFFSET -8 # frame has moved
432 lss 20+4(%esp), %esp # switch to 16bit stack
434 .section __ex_table,"a"
440 # perform work that needs to be done immediately before resumption
442 RING0_PTREGS_FRAME # can't unwind into user space anyway
444 testb $_TIF_NEED_RESCHED, %cl
448 DISABLE_INTERRUPTS # make sure we don't miss an interrupt
449 # setting need_resched or sigpending
450 # between sampling and the iret
452 movl TI_flags(%ebp), %ecx
453 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
454 # than syscall tracing?
456 testb $_TIF_NEED_RESCHED, %cl
459 work_notifysig: # deal with pending signals and
460 # notify-resume requests
461 testl $VM_MASK, EFLAGS(%esp)
463 jne work_notifysig_v86 # returning to kernel-space or
466 call do_notify_resume
467 jmp resume_userspace_sig
472 pushl %ecx # save ti_flags for do_notify_resume
473 CFI_ADJUST_CFA_OFFSET 4
474 call save_v86_state # %eax contains pt_regs pointer
476 CFI_ADJUST_CFA_OFFSET -4
479 call do_notify_resume
480 jmp resume_userspace_sig
483 # perform syscall exit tracing
486 movl $-ENOSYS,EAX(%esp)
489 call do_syscall_trace
491 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
492 # so must skip actual syscall
493 movl ORIG_EAX(%esp), %eax
494 cmpl $(nr_syscalls), %eax
498 # perform syscall exit tracing
501 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
504 ENABLE_INTERRUPTS # could let do_syscall_trace() call
508 call do_syscall_trace
512 RING0_INT_FRAME # can't unwind into user space anyway
514 pushl %eax # save orig_eax
515 CFI_ADJUST_CFA_OFFSET 4
517 GET_THREAD_INFO(%ebp)
518 movl $-EFAULT,EAX(%esp)
522 movl $-ENOSYS,EAX(%esp)
526 #define FIXUP_ESPFIX_STACK \
528 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
529 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
530 /* copy data from 16bit stack to 32bit stack */ \
531 call fixup_x86_bogus_stack; \
532 /* put ESP to the proper location */ \
534 #define UNWIND_ESPFIX_STACK \
536 CFI_ADJUST_CFA_OFFSET 4; \
538 /* see if on 16bit stack */ \
539 cmpw $__ESPFIX_SS, %ax; \
542 CFI_ADJUST_CFA_OFFSET -4; \
543 .section .fixup,"ax"; \
544 28: movl $__KERNEL_DS, %eax; \
547 /* switch to 32bit stack */ \
548 FIXUP_ESPFIX_STACK; \
553 * Build the entry stubs and pointer table with
554 * some assembler magic.
561 ENTRY(irq_entries_start)
566 CFI_ADJUST_CFA_OFFSET -4
569 CFI_ADJUST_CFA_OFFSET 4
578 * the CPU automatically disables interrupts when executing an IRQ vector,
579 * so IRQ-flags tracing has to follow that:
590 #define BUILD_INTERRUPT(name, nr) \
594 CFI_ADJUST_CFA_OFFSET 4; \
602 /* The include is where all of the SMP etc. interrupts come from */
603 #include "entry_arch.h"
605 KPROBE_ENTRY(page_fault)
608 CFI_ADJUST_CFA_OFFSET 4
612 CFI_ADJUST_CFA_OFFSET 4
613 /*CFI_REL_OFFSET ds, 0*/
615 CFI_ADJUST_CFA_OFFSET 4
616 CFI_REL_OFFSET eax, 0
619 CFI_ADJUST_CFA_OFFSET 4
620 CFI_REL_OFFSET ebp, 0
622 CFI_ADJUST_CFA_OFFSET 4
623 CFI_REL_OFFSET edi, 0
625 CFI_ADJUST_CFA_OFFSET 4
626 CFI_REL_OFFSET esi, 0
628 CFI_ADJUST_CFA_OFFSET 4
629 CFI_REL_OFFSET edx, 0
632 CFI_ADJUST_CFA_OFFSET 4
633 CFI_REL_OFFSET ecx, 0
635 CFI_ADJUST_CFA_OFFSET 4
636 CFI_REL_OFFSET ebx, 0
639 CFI_ADJUST_CFA_OFFSET 4
640 /*CFI_REL_OFFSET es, 0*/
643 CFI_ADJUST_CFA_OFFSET -4
644 /*CFI_REGISTER es, ecx*/
645 movl ES(%esp), %edi # get the function address
646 movl ORIG_EAX(%esp), %edx # get the error code
647 movl %eax, ORIG_EAX(%esp)
649 /*CFI_REL_OFFSET es, ES*/
650 movl $(__USER_DS), %ecx
653 movl %esp,%eax # pt_regs pointer
655 jmp ret_from_exception
657 KPROBE_END(page_fault)
659 ENTRY(coprocessor_error)
662 CFI_ADJUST_CFA_OFFSET 4
663 pushl $do_coprocessor_error
664 CFI_ADJUST_CFA_OFFSET 4
668 ENTRY(simd_coprocessor_error)
671 CFI_ADJUST_CFA_OFFSET 4
672 pushl $do_simd_coprocessor_error
673 CFI_ADJUST_CFA_OFFSET 4
677 ENTRY(device_not_available)
679 pushl $-1 # mark this as an int
680 CFI_ADJUST_CFA_OFFSET 4
683 testl $0x4, %eax # EM (math emulation bit)
684 jne device_not_available_emulate
686 call math_state_restore
687 jmp ret_from_exception
688 device_not_available_emulate:
689 pushl $0 # temporary storage for ORIG_EIP
690 CFI_ADJUST_CFA_OFFSET 4
693 CFI_ADJUST_CFA_OFFSET -4
694 jmp ret_from_exception
698 * Debug traps and NMI can happen at the one SYSENTER instruction
699 * that sets up the real kernel stack. Check here, since we can't
700 * allow the wrong stack to be used.
702 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
703 * already pushed 3 words if it hits on the sysenter instruction:
704 * eflags, cs and eip.
706 * We just load the right stack, and push the three (known) values
707 * by hand onto the new stack - while updating the return eip past
708 * the instruction that would have done it for sysenter.
710 #define FIX_STACK(offset, ok, label) \
711 cmpw $__KERNEL_CS,4(%esp); \
714 movl TSS_sysenter_esp0+offset(%esp),%esp; \
715 CFI_DEF_CFA esp, 0; \
718 CFI_ADJUST_CFA_OFFSET 4; \
719 pushl $__KERNEL_CS; \
720 CFI_ADJUST_CFA_OFFSET 4; \
721 pushl $sysenter_past_esp; \
722 CFI_ADJUST_CFA_OFFSET 4; \
723 CFI_REL_OFFSET eip, 0
727 cmpl $sysenter_entry,(%esp)
728 jne debug_stack_correct
729 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
731 pushl $-1 # mark this as an int
732 CFI_ADJUST_CFA_OFFSET 4
734 xorl %edx,%edx # error code 0
735 movl %esp,%eax # pt_regs pointer
737 jmp ret_from_exception
742 * NMI is doubly nasty. It can happen _while_ we're handling
743 * a debug fault, and the debug fault hasn't yet been able to
744 * clear up the stack. So we first check whether we got an
745 * NMI on the sysenter entry path, but after that we need to
746 * check whether we got an NMI on the debug path where the debug
747 * fault happened on the sysenter path.
752 CFI_ADJUST_CFA_OFFSET 4
754 cmpw $__ESPFIX_SS, %ax
756 CFI_ADJUST_CFA_OFFSET -4
758 cmpl $sysenter_entry,(%esp)
761 CFI_ADJUST_CFA_OFFSET 4
763 /* Do not access memory above the end of our stack page,
764 * it might not exist.
766 andl $(THREAD_SIZE-1),%eax
767 cmpl $(THREAD_SIZE-20),%eax
769 CFI_ADJUST_CFA_OFFSET -4
770 jae nmi_stack_correct
771 cmpl $sysenter_entry,12(%esp)
772 je nmi_debug_stack_check
774 /* We have a RING0_INT_FRAME here */
776 CFI_ADJUST_CFA_OFFSET 4
778 xorl %edx,%edx # zero error code
779 movl %esp,%eax # pt_regs pointer
781 jmp restore_nocheck_notrace
786 FIX_STACK(12,nmi_stack_correct, 1)
787 jmp nmi_stack_correct
789 nmi_debug_stack_check:
790 /* We have a RING0_INT_FRAME here */
791 cmpw $__KERNEL_CS,16(%esp)
792 jne nmi_stack_correct
795 cmpl $debug_esp_fix_insn,(%esp)
797 FIX_STACK(24,nmi_stack_correct, 1)
798 jmp nmi_stack_correct
801 /* We have a RING0_INT_FRAME here.
803 * create the pointer to lss back
806 CFI_ADJUST_CFA_OFFSET 4
808 CFI_ADJUST_CFA_OFFSET 4
811 /* copy the iret frame of 12 bytes */
814 CFI_ADJUST_CFA_OFFSET 4
817 CFI_ADJUST_CFA_OFFSET 4
819 FIXUP_ESPFIX_STACK # %eax == %esp
820 CFI_ADJUST_CFA_OFFSET -20 # the frame has now moved
821 xorl %edx,%edx # zero error code
824 lss 12+4(%esp), %esp # back to 16bit stack
827 .section __ex_table,"a"
835 pushl $-1 # mark this as an int
836 CFI_ADJUST_CFA_OFFSET 4
838 xorl %edx,%edx # zero error code
839 movl %esp,%eax # pt_regs pointer
841 jmp ret_from_exception
848 CFI_ADJUST_CFA_OFFSET 4
850 CFI_ADJUST_CFA_OFFSET 4
857 CFI_ADJUST_CFA_OFFSET 4
859 CFI_ADJUST_CFA_OFFSET 4
866 CFI_ADJUST_CFA_OFFSET 4
868 CFI_ADJUST_CFA_OFFSET 4
872 ENTRY(coprocessor_segment_overrun)
875 CFI_ADJUST_CFA_OFFSET 4
876 pushl $do_coprocessor_segment_overrun
877 CFI_ADJUST_CFA_OFFSET 4
883 pushl $do_invalid_TSS
884 CFI_ADJUST_CFA_OFFSET 4
888 ENTRY(segment_not_present)
890 pushl $do_segment_not_present
891 CFI_ADJUST_CFA_OFFSET 4
897 pushl $do_stack_segment
898 CFI_ADJUST_CFA_OFFSET 4
902 KPROBE_ENTRY(general_protection)
904 pushl $do_general_protection
905 CFI_ADJUST_CFA_OFFSET 4
908 KPROBE_END(general_protection)
910 ENTRY(alignment_check)
912 pushl $do_alignment_check
913 CFI_ADJUST_CFA_OFFSET 4
919 pushl $0 # no error code
920 CFI_ADJUST_CFA_OFFSET 4
921 pushl $do_divide_error
922 CFI_ADJUST_CFA_OFFSET 4
926 #ifdef CONFIG_X86_MCE
930 CFI_ADJUST_CFA_OFFSET 4
931 pushl machine_check_vector
932 CFI_ADJUST_CFA_OFFSET 4
937 ENTRY(spurious_interrupt_bug)
940 CFI_ADJUST_CFA_OFFSET 4
941 pushl $do_spurious_interrupt_bug
942 CFI_ADJUST_CFA_OFFSET 4
946 #ifdef CONFIG_STACK_UNWIND
947 ENTRY(arch_unwind_init_running)
960 movl $__USER_DS, DS(%edx)
961 movl $__USER_DS, ES(%edx)
962 movl %ebx, ORIG_EAX(%edx)
965 movl $__KERNEL_CS, CS(%edx)
966 movl %ebx, EFLAGS(%edx)
967 movl %eax, OLDESP(%edx)
971 movl $__KERNEL_DS, OLDSS(%edx)
974 ENDPROC(arch_unwind_init_running)
977 ENTRY(kernel_thread_helper)
978 pushl $0 # fake return address for unwinder
982 CFI_ADJUST_CFA_OFFSET 4
985 CFI_ADJUST_CFA_OFFSET 4
988 ENDPROC(kernel_thread_helper)
991 #include "syscall_table.S"
993 syscall_table_size=(.-sys_call_table)