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 "irq_vectors.h"
53 #define nr_syscalls ((syscall_table_size)/4)
79 #define preempt_stop cli
82 #define resume_kernel restore_nocheck
96 movl $(__USER_DS), %edx; \
100 #define RESTORE_INT_REGS \
109 #define RESTORE_REGS \
113 .section .fixup,"ax"; \
119 .section __ex_table,"a";\
129 GET_THREAD_INFO(%ebp)
134 * Return to user mode is not as complex as all this looks,
135 * but we want the default path for a system call return to
136 * go as quickly as possible which is why some of this is
137 * less clear than it otherwise should be.
140 # userspace resumption stub bypassing syscall exit tracing
145 GET_THREAD_INFO(%ebp)
146 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
148 testl $(VM_MASK | 3), %eax
150 ENTRY(resume_userspace)
151 cli # make sure we don't miss an interrupt
152 # setting need_resched or sigpending
153 # between sampling and the iret
154 movl TI_flags(%ebp), %ecx
155 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
156 # int/exception return?
160 #ifdef CONFIG_PREEMPT
163 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
166 movl TI_flags(%ebp), %ecx # need_resched set ?
167 testb $_TIF_NEED_RESCHED, %cl
169 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
171 call preempt_schedule_irq
175 /* SYSENTER_RETURN points to after the "sysenter" instruction in
176 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
178 # sysenter call handler stub
179 ENTRY(sysenter_entry)
180 movl TSS_sysenter_esp0(%esp),%esp
187 pushl $SYSENTER_RETURN
190 * Load the potential sixth argument from user stack.
191 * Careful about security.
193 cmpl $__PAGE_OFFSET-3,%ebp
196 .section __ex_table,"a"
198 .long 1b,syscall_fault
203 GET_THREAD_INFO(%ebp)
205 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
206 testw $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP),TI_flags(%ebp)
207 jnz syscall_trace_entry
208 cmpl $(nr_syscalls), %eax
210 call *sys_call_table(,%eax,4)
213 movl TI_flags(%ebp), %ecx
214 testw $_TIF_ALLWORK_MASK, %cx
215 jne syscall_exit_work
216 /* if something modifies registers it must also disable sysexit */
218 movl OLDESP(%esp), %ecx
224 # system call handler stub
226 pushl %eax # save orig_eax
228 GET_THREAD_INFO(%ebp)
229 # system call tracing in operation
230 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
231 testw $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP),TI_flags(%ebp)
232 jnz syscall_trace_entry
233 cmpl $(nr_syscalls), %eax
236 call *sys_call_table(,%eax,4)
237 movl %eax,EAX(%esp) # store the return value
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 testw $_TIF_ALLWORK_MASK, %cx # current->work
244 jne syscall_exit_work
247 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
248 movb OLDSS(%esp), %ah
250 andl $(VM_MASK | (4 << 8) | 3), %eax
251 cmpl $((4 << 8) | 3), %eax
252 je ldt_ss # returning to user-space with LDT SS
260 movl $__USER_DS, %edx
266 .section __ex_table,"a"
272 larl OLDSS(%esp), %eax
274 testl $0x00400000, %eax # returning to 32bit stack?
275 jnz restore_nocheck # allright, normal return
276 /* If returning to userspace with 16bit stack,
277 * try to fix the higher word of ESP, as the CPU
279 * This is an "official" bug of all the x86-compatible
280 * CPUs, which we can try to work around to make
281 * dosemu and wine happy. */
282 subl $8, %esp # reserve space for switch16 pointer
285 /* Set up the 16bit stack frame with switch32 pointer on top,
286 * and a switch16 pointer on top of the current frame. */
287 call setup_x86_bogus_stack
289 lss 20+4(%esp), %esp # switch to 16bit stack
291 .section __ex_table,"a"
296 # perform work that needs to be done immediately before resumption
299 testb $_TIF_NEED_RESCHED, %cl
303 cli # make sure we don't miss an interrupt
304 # setting need_resched or sigpending
305 # between sampling and the iret
306 movl TI_flags(%ebp), %ecx
307 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
308 # than syscall tracing?
310 testb $_TIF_NEED_RESCHED, %cl
313 work_notifysig: # deal with pending signals and
314 # notify-resume requests
315 testl $VM_MASK, EFLAGS(%esp)
317 jne work_notifysig_v86 # returning to kernel-space or
320 call do_notify_resume
325 pushl %ecx # save ti_flags for do_notify_resume
326 call save_v86_state # %eax contains pt_regs pointer
330 call do_notify_resume
333 # perform syscall exit tracing
336 movl $-ENOSYS,EAX(%esp)
339 call do_syscall_trace
340 movl ORIG_EAX(%esp), %eax
341 cmpl $(nr_syscalls), %eax
345 # perform syscall exit tracing
348 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
350 sti # could let do_syscall_trace() call
354 call do_syscall_trace
359 pushl %eax # save orig_eax
361 GET_THREAD_INFO(%ebp)
362 movl $-EFAULT,EAX(%esp)
367 movl $-ENOSYS,EAX(%esp)
370 #define FIXUP_ESPFIX_STACK \
372 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
373 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
374 /* copy data from 16bit stack to 32bit stack */ \
375 call fixup_x86_bogus_stack; \
376 /* put ESP to the proper location */ \
378 #define UNWIND_ESPFIX_STACK \
381 /* see if on 16bit stack */ \
382 cmpw $__ESPFIX_SS, %ax; \
384 movl $__KERNEL_DS, %edx; \
387 /* switch to 32bit stack */ \
392 * Build the entry stubs and pointer table with
393 * some assembler magic.
400 ENTRY(irq_entries_start)
418 #define BUILD_INTERRUPT(name, nr) \
426 /* The include is where all of the SMP etc. interrupts come from */
427 #include "entry_arch.h"
430 pushl $0 # no error code
431 pushl $do_divide_error
448 movl ES(%esp), %edi # get the function address
449 movl ORIG_EAX(%esp), %edx # get the error code
450 movl %eax, ORIG_EAX(%esp)
452 movl $(__USER_DS), %ecx
455 movl %esp,%eax # pt_regs pointer
457 jmp ret_from_exception
459 ENTRY(coprocessor_error)
461 pushl $do_coprocessor_error
464 ENTRY(simd_coprocessor_error)
466 pushl $do_simd_coprocessor_error
469 ENTRY(device_not_available)
470 pushl $-1 # mark this as an int
473 testl $0x4, %eax # EM (math emulation bit)
474 jne device_not_available_emulate
476 call math_state_restore
477 jmp ret_from_exception
478 device_not_available_emulate:
479 pushl $0 # temporary storage for ORIG_EIP
482 jmp ret_from_exception
485 * Debug traps and NMI can happen at the one SYSENTER instruction
486 * that sets up the real kernel stack. Check here, since we can't
487 * allow the wrong stack to be used.
489 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
490 * already pushed 3 words if it hits on the sysenter instruction:
491 * eflags, cs and eip.
493 * We just load the right stack, and push the three (known) values
494 * by hand onto the new stack - while updating the return eip past
495 * the instruction that would have done it for sysenter.
497 #define FIX_STACK(offset, ok, label) \
498 cmpw $__KERNEL_CS,4(%esp); \
501 movl TSS_sysenter_esp0+offset(%esp),%esp; \
503 pushl $__KERNEL_CS; \
504 pushl $sysenter_past_esp
507 cmpl $sysenter_entry,(%esp)
508 jne debug_stack_correct
509 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
511 pushl $-1 # mark this as an int
513 xorl %edx,%edx # error code 0
514 movl %esp,%eax # pt_regs pointer
518 jmp ret_from_exception
521 * NMI is doubly nasty. It can happen _while_ we're handling
522 * a debug fault, and the debug fault hasn't yet been able to
523 * clear up the stack. So we first check whether we got an
524 * NMI on the sysenter entry path, but after that we need to
525 * check whether we got an NMI on the debug path where the debug
526 * fault happened on the sysenter path.
531 cmpw $__ESPFIX_SS, %ax
534 cmpl $sysenter_entry,(%esp)
538 /* Do not access memory above the end of our stack page,
539 * it might not exist.
541 andl $(THREAD_SIZE-1),%eax
542 cmpl $(THREAD_SIZE-20),%eax
544 jae nmi_stack_correct
545 cmpl $sysenter_entry,12(%esp)
546 je nmi_debug_stack_check
550 xorl %edx,%edx # zero error code
551 movl %esp,%eax # pt_regs pointer
556 FIX_STACK(12,nmi_stack_correct, 1)
557 jmp nmi_stack_correct
558 nmi_debug_stack_check:
559 cmpw $__KERNEL_CS,16(%esp)
560 jne nmi_stack_correct
561 cmpl $debug - 1,(%esp)
562 jle nmi_stack_correct
563 cmpl $debug_esp_fix_insn,(%esp)
564 jle nmi_debug_stack_fixup
565 nmi_debug_stack_fixup:
566 FIX_STACK(24,nmi_stack_correct, 1)
567 jmp nmi_stack_correct
570 /* create the pointer to lss back */
575 /* copy the iret frame of 12 bytes */
581 FIXUP_ESPFIX_STACK # %eax == %esp
582 xorl %edx,%edx # zero error code
585 lss 12+4(%esp), %esp # back to 16bit stack
587 .section __ex_table,"a"
593 pushl $-1 # mark this as an int
595 xorl %edx,%edx # zero error code
596 movl %esp,%eax # pt_regs pointer
600 jmp ret_from_exception
617 ENTRY(coprocessor_segment_overrun)
619 pushl $do_coprocessor_segment_overrun
623 pushl $do_invalid_TSS
626 ENTRY(segment_not_present)
627 pushl $do_segment_not_present
631 pushl $do_stack_segment
634 ENTRY(general_protection)
635 pushl $do_general_protection
638 ENTRY(alignment_check)
639 pushl $do_alignment_check
646 #ifdef CONFIG_X86_MCE
649 pushl machine_check_vector
653 ENTRY(spurious_interrupt_bug)
655 pushl $do_spurious_interrupt_bug
659 ENTRY(sys_call_table)
660 .long sys_restart_syscall /* 0 - old "setup()" system call, used for restarting */
665 .long sys_open /* 5 */
670 .long sys_unlink /* 10 */
675 .long sys_chmod /* 15 */
677 .long sys_ni_syscall /* old break syscall holder */
680 .long sys_getpid /* 20 */
685 .long sys_stime /* 25 */
690 .long sys_utime /* 30 */
691 .long sys_ni_syscall /* old stty syscall holder */
692 .long sys_ni_syscall /* old gtty syscall holder */
695 .long sys_ni_syscall /* 35 - old ftime syscall holder */
700 .long sys_rmdir /* 40 */
704 .long sys_ni_syscall /* old prof syscall holder */
705 .long sys_brk /* 45 */
710 .long sys_getegid16 /* 50 */
712 .long sys_umount /* recycled never used phys() */
713 .long sys_ni_syscall /* old lock syscall holder */
715 .long sys_fcntl /* 55 */
716 .long sys_ni_syscall /* old mpx syscall holder */
718 .long sys_ni_syscall /* old ulimit syscall holder */
720 .long sys_umask /* 60 */
725 .long sys_getpgrp /* 65 */
730 .long sys_setreuid16 /* 70 */
734 .long sys_sethostname
735 .long sys_setrlimit /* 75 */
736 .long sys_old_getrlimit
738 .long sys_gettimeofday
739 .long sys_settimeofday
740 .long sys_getgroups16 /* 80 */
741 .long sys_setgroups16
745 .long sys_readlink /* 85 */
750 .long old_mmap /* 90 */
755 .long sys_fchown16 /* 95 */
756 .long sys_getpriority
757 .long sys_setpriority
758 .long sys_ni_syscall /* old profil syscall holder */
760 .long sys_fstatfs /* 100 */
765 .long sys_getitimer /* 105 */
770 .long sys_iopl /* 110 */
772 .long sys_ni_syscall /* old "idle" system call */
775 .long sys_swapoff /* 115 */
780 .long sys_clone /* 120 */
781 .long sys_setdomainname
785 .long sys_mprotect /* 125 */
786 .long sys_sigprocmask
787 .long sys_ni_syscall /* old "create_module" */
788 .long sys_init_module
789 .long sys_delete_module
790 .long sys_ni_syscall /* 130: old "get_kernel_syms" */
795 .long sys_sysfs /* 135 */
796 .long sys_personality
797 .long sys_ni_syscall /* reserved for afs_syscall */
800 .long sys_llseek /* 140 */
805 .long sys_readv /* 145 */
810 .long sys_mlock /* 150 */
814 .long sys_sched_setparam
815 .long sys_sched_getparam /* 155 */
816 .long sys_sched_setscheduler
817 .long sys_sched_getscheduler
818 .long sys_sched_yield
819 .long sys_sched_get_priority_max
820 .long sys_sched_get_priority_min /* 160 */
821 .long sys_sched_rr_get_interval
824 .long sys_setresuid16
825 .long sys_getresuid16 /* 165 */
827 .long sys_ni_syscall /* Old sys_query_module */
830 .long sys_setresgid16 /* 170 */
831 .long sys_getresgid16
833 .long sys_rt_sigreturn
834 .long sys_rt_sigaction
835 .long sys_rt_sigprocmask /* 175 */
836 .long sys_rt_sigpending
837 .long sys_rt_sigtimedwait
838 .long sys_rt_sigqueueinfo
839 .long sys_rt_sigsuspend
840 .long sys_pread64 /* 180 */
845 .long sys_capset /* 185 */
846 .long sys_sigaltstack
848 .long sys_ni_syscall /* reserved for streams1 */
849 .long sys_ni_syscall /* reserved for streams2 */
850 .long sys_vfork /* 190 */
854 .long sys_ftruncate64
855 .long sys_stat64 /* 195 */
860 .long sys_getgid /* 200 */
865 .long sys_getgroups /* 205 */
870 .long sys_setresgid /* 210 */
875 .long sys_setfsuid /* 215 */
880 .long sys_getdents64 /* 220 */
882 .long sys_ni_syscall /* reserved for TUX */
885 .long sys_readahead /* 225 */
890 .long sys_lgetxattr /* 230 */
895 .long sys_removexattr /* 235 */
896 .long sys_lremovexattr
897 .long sys_fremovexattr
900 .long sys_futex /* 240 */
901 .long sys_sched_setaffinity
902 .long sys_sched_getaffinity
903 .long sys_set_thread_area
904 .long sys_get_thread_area
905 .long sys_io_setup /* 245 */
907 .long sys_io_getevents
910 .long sys_fadvise64 /* 250 */
913 .long sys_lookup_dcookie
914 .long sys_epoll_create
915 .long sys_epoll_ctl /* 255 */
917 .long sys_remap_file_pages
918 .long sys_set_tid_address
919 .long sys_timer_create
920 .long sys_timer_settime /* 260 */
921 .long sys_timer_gettime
922 .long sys_timer_getoverrun
923 .long sys_timer_delete
924 .long sys_clock_settime
925 .long sys_clock_gettime /* 265 */
926 .long sys_clock_getres
927 .long sys_clock_nanosleep
930 .long sys_tgkill /* 270 */
932 .long sys_fadvise64_64
933 .long sys_ni_syscall /* sys_vserver */
935 .long sys_get_mempolicy
936 .long sys_set_mempolicy
939 .long sys_mq_timedsend
940 .long sys_mq_timedreceive /* 280 */
942 .long sys_mq_getsetattr
943 .long sys_ni_syscall /* reserved for kexec */
945 .long sys_ni_syscall /* 285 */ /* available */
947 .long sys_request_key
950 syscall_table_size=(.-sys_call_table)