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)
131 pushl $0x0202 # Reset kernel eflags
136 * Return to user mode is not as complex as all this looks,
137 * but we want the default path for a system call return to
138 * go as quickly as possible which is why some of this is
139 * less clear than it otherwise should be.
142 # userspace resumption stub bypassing syscall exit tracing
147 GET_THREAD_INFO(%ebp)
148 movl EFLAGS(%esp), %eax # mix EFLAGS and CS
150 testl $(VM_MASK | 3), %eax
152 ENTRY(resume_userspace)
153 cli # make sure we don't miss an interrupt
154 # setting need_resched or sigpending
155 # between sampling and the iret
156 movl TI_flags(%ebp), %ecx
157 andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
158 # int/exception return?
162 #ifdef CONFIG_PREEMPT
165 cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
168 movl TI_flags(%ebp), %ecx # need_resched set ?
169 testb $_TIF_NEED_RESCHED, %cl
171 testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
173 call preempt_schedule_irq
177 /* SYSENTER_RETURN points to after the "sysenter" instruction in
178 the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
180 # sysenter call handler stub
181 ENTRY(sysenter_entry)
182 movl TSS_sysenter_esp0(%esp),%esp
189 pushl $SYSENTER_RETURN
192 * Load the potential sixth argument from user stack.
193 * Careful about security.
195 cmpl $__PAGE_OFFSET-3,%ebp
198 .section __ex_table,"a"
200 .long 1b,syscall_fault
205 GET_THREAD_INFO(%ebp)
207 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
208 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
209 jnz syscall_trace_entry
210 cmpl $(nr_syscalls), %eax
212 call *sys_call_table(,%eax,4)
215 movl TI_flags(%ebp), %ecx
216 testw $_TIF_ALLWORK_MASK, %cx
217 jne syscall_exit_work
218 /* if something modifies registers it must also disable sysexit */
220 movl OLDESP(%esp), %ecx
226 # system call handler stub
228 pushl %eax # save orig_eax
230 GET_THREAD_INFO(%ebp)
231 # system call tracing in operation / emulation
232 /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
233 testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
234 jnz syscall_trace_entry
235 cmpl $(nr_syscalls), %eax
238 call *sys_call_table(,%eax,4)
239 movl %eax,EAX(%esp) # store the return value
241 cli # make sure we don't miss an interrupt
242 # setting need_resched or sigpending
243 # between sampling and the iret
244 movl TI_flags(%ebp), %ecx
245 testw $_TIF_ALLWORK_MASK, %cx # current->work
246 jne syscall_exit_work
249 movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
250 # Warning: OLDSS(%esp) contains the wrong/random values if we
251 # are returning to the kernel.
252 # See comments in process.c:copy_thread() for details.
253 movb OLDSS(%esp), %ah
255 andl $(VM_MASK | (4 << 8) | 3), %eax
256 cmpl $((4 << 8) | 3), %eax
257 je ldt_ss # returning to user-space with LDT SS
265 pushl $0 # no error code
269 .section __ex_table,"a"
275 larl OLDSS(%esp), %eax
277 testl $0x00400000, %eax # returning to 32bit stack?
278 jnz restore_nocheck # allright, normal return
279 /* If returning to userspace with 16bit stack,
280 * try to fix the higher word of ESP, as the CPU
282 * This is an "official" bug of all the x86-compatible
283 * CPUs, which we can try to work around to make
284 * dosemu and wine happy. */
285 subl $8, %esp # reserve space for switch16 pointer
288 /* Set up the 16bit stack frame with switch32 pointer on top,
289 * and a switch16 pointer on top of the current frame. */
290 call setup_x86_bogus_stack
292 lss 20+4(%esp), %esp # switch to 16bit stack
294 .section __ex_table,"a"
299 # perform work that needs to be done immediately before resumption
302 testb $_TIF_NEED_RESCHED, %cl
306 cli # make sure we don't miss an interrupt
307 # setting need_resched or sigpending
308 # between sampling and the iret
309 movl TI_flags(%ebp), %ecx
310 andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
311 # than syscall tracing?
313 testb $_TIF_NEED_RESCHED, %cl
316 work_notifysig: # deal with pending signals and
317 # notify-resume requests
318 testl $VM_MASK, EFLAGS(%esp)
320 jne work_notifysig_v86 # returning to kernel-space or
323 call do_notify_resume
329 pushl %ecx # save ti_flags for do_notify_resume
330 call save_v86_state # %eax contains pt_regs pointer
334 call do_notify_resume
338 # perform syscall exit tracing
341 movl $-ENOSYS,EAX(%esp)
344 call do_syscall_trace
346 jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
347 # so must skip actual syscall
348 movl ORIG_EAX(%esp), %eax
349 cmpl $(nr_syscalls), %eax
353 # perform syscall exit tracing
356 testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
358 sti # could let do_syscall_trace() call
362 call do_syscall_trace
367 pushl %eax # save orig_eax
369 GET_THREAD_INFO(%ebp)
370 movl $-EFAULT,EAX(%esp)
375 movl $-ENOSYS,EAX(%esp)
378 #define FIXUP_ESPFIX_STACK \
380 /* switch to 32bit stack using the pointer on top of 16bit stack */ \
381 lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
382 /* copy data from 16bit stack to 32bit stack */ \
383 call fixup_x86_bogus_stack; \
384 /* put ESP to the proper location */ \
386 #define UNWIND_ESPFIX_STACK \
389 /* see if on 16bit stack */ \
390 cmpw $__ESPFIX_SS, %ax; \
392 movl $__KERNEL_DS, %edx; \
395 /* switch to 32bit stack */ \
400 * Build the entry stubs and pointer table with
401 * some assembler magic.
408 ENTRY(irq_entries_start)
426 #define BUILD_INTERRUPT(name, nr) \
434 /* The include is where all of the SMP etc. interrupts come from */
435 #include "entry_arch.h"
438 pushl $0 # no error code
439 pushl $do_divide_error
456 movl ES(%esp), %edi # get the function address
457 movl ORIG_EAX(%esp), %edx # get the error code
458 movl %eax, ORIG_EAX(%esp)
460 movl $(__USER_DS), %ecx
463 movl %esp,%eax # pt_regs pointer
465 jmp ret_from_exception
467 ENTRY(coprocessor_error)
469 pushl $do_coprocessor_error
472 ENTRY(simd_coprocessor_error)
474 pushl $do_simd_coprocessor_error
477 ENTRY(device_not_available)
478 pushl $-1 # mark this as an int
481 testl $0x4, %eax # EM (math emulation bit)
482 jne device_not_available_emulate
484 call math_state_restore
485 jmp ret_from_exception
486 device_not_available_emulate:
487 pushl $0 # temporary storage for ORIG_EIP
490 jmp ret_from_exception
493 * Debug traps and NMI can happen at the one SYSENTER instruction
494 * that sets up the real kernel stack. Check here, since we can't
495 * allow the wrong stack to be used.
497 * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
498 * already pushed 3 words if it hits on the sysenter instruction:
499 * eflags, cs and eip.
501 * We just load the right stack, and push the three (known) values
502 * by hand onto the new stack - while updating the return eip past
503 * the instruction that would have done it for sysenter.
505 #define FIX_STACK(offset, ok, label) \
506 cmpw $__KERNEL_CS,4(%esp); \
509 movl TSS_sysenter_esp0+offset(%esp),%esp; \
511 pushl $__KERNEL_CS; \
512 pushl $sysenter_past_esp
515 cmpl $sysenter_entry,(%esp)
516 jne debug_stack_correct
517 FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
519 pushl $-1 # mark this as an int
521 xorl %edx,%edx # error code 0
522 movl %esp,%eax # pt_regs pointer
524 jmp ret_from_exception
527 * NMI is doubly nasty. It can happen _while_ we're handling
528 * a debug fault, and the debug fault hasn't yet been able to
529 * clear up the stack. So we first check whether we got an
530 * NMI on the sysenter entry path, but after that we need to
531 * check whether we got an NMI on the debug path where the debug
532 * fault happened on the sysenter path.
537 cmpw $__ESPFIX_SS, %ax
540 cmpl $sysenter_entry,(%esp)
544 /* Do not access memory above the end of our stack page,
545 * it might not exist.
547 andl $(THREAD_SIZE-1),%eax
548 cmpl $(THREAD_SIZE-20),%eax
550 jae nmi_stack_correct
551 cmpl $sysenter_entry,12(%esp)
552 je nmi_debug_stack_check
556 xorl %edx,%edx # zero error code
557 movl %esp,%eax # pt_regs pointer
562 FIX_STACK(12,nmi_stack_correct, 1)
563 jmp nmi_stack_correct
564 nmi_debug_stack_check:
565 cmpw $__KERNEL_CS,16(%esp)
566 jne nmi_stack_correct
569 cmpl $debug_esp_fix_insn,(%esp)
571 FIX_STACK(24,nmi_stack_correct, 1)
572 jmp nmi_stack_correct
575 /* create the pointer to lss back */
580 /* copy the iret frame of 12 bytes */
586 FIXUP_ESPFIX_STACK # %eax == %esp
587 xorl %edx,%edx # zero error code
590 lss 12+4(%esp), %esp # back to 16bit stack
592 .section __ex_table,"a"
598 pushl $-1 # mark this as an int
600 xorl %edx,%edx # zero error code
601 movl %esp,%eax # pt_regs pointer
603 jmp ret_from_exception
621 ENTRY(coprocessor_segment_overrun)
623 pushl $do_coprocessor_segment_overrun
627 pushl $do_invalid_TSS
630 ENTRY(segment_not_present)
631 pushl $do_segment_not_present
635 pushl $do_stack_segment
638 KPROBE_ENTRY(general_protection)
639 pushl $do_general_protection
643 ENTRY(alignment_check)
644 pushl $do_alignment_check
647 KPROBE_ENTRY(page_fault)
652 #ifdef CONFIG_X86_MCE
655 pushl machine_check_vector
659 ENTRY(spurious_interrupt_bug)
661 pushl $do_spurious_interrupt_bug
665 #include "syscall_table.S"
667 syscall_table_size=(.-sys_call_table)