a14218c9ec6cf208d78fb9e1f15c877c5a84d8df
[linux-2.6.git] / arch / i386 / kernel / entry.S
1 /*
2  *  linux/arch/i386/entry.S
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
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.
11  *
12  * NOTE: This code handles signal-recognition, which happens every time
13  * after a timer-interrupt and after each system call.
14  *
15  * I changed all the .align's to 4 (16 byte alignment), as that's faster
16  * on a 486.
17  *
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
23  *
24  *       0(%esp) - %ebx
25  *       4(%esp) - %ecx
26  *       8(%esp) - %edx
27  *       C(%esp) - %esi
28  *      10(%esp) - %edi
29  *      14(%esp) - %ebp
30  *      18(%esp) - %eax
31  *      1C(%esp) - %ds
32  *      20(%esp) - %es
33  *      24(%esp) - orig_eax
34  *      28(%esp) - %eip
35  *      2C(%esp) - %cs
36  *      30(%esp) - %eflags
37  *      34(%esp) - %oldesp
38  *      38(%esp) - %oldss
39  *
40  * "current" is in register %ebx during any slow entries.
41  */
42
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>
48 #include <asm/smp.h>
49 #include <asm/page.h>
50 #include <asm/desc.h>
51 #include "irq_vectors.h"
52
53 #define nr_syscalls ((syscall_table_size)/4)
54
55 EBX             = 0x00
56 ECX             = 0x04
57 EDX             = 0x08
58 ESI             = 0x0C
59 EDI             = 0x10
60 EBP             = 0x14
61 EAX             = 0x18
62 DS              = 0x1C
63 ES              = 0x20
64 ORIG_EAX        = 0x24
65 EIP             = 0x28
66 CS              = 0x2C
67 EFLAGS          = 0x30
68 OLDESP          = 0x34
69 OLDSS           = 0x38
70
71 CF_MASK         = 0x00000001
72 TF_MASK         = 0x00000100
73 IF_MASK         = 0x00000200
74 DF_MASK         = 0x00000400 
75 NT_MASK         = 0x00004000
76 VM_MASK         = 0x00020000
77
78 #ifdef CONFIG_PREEMPT
79 #define preempt_stop            cli
80 #else
81 #define preempt_stop
82 #define resume_kernel           restore_nocheck
83 #endif
84
85 #define SAVE_ALL \
86         cld; \
87         pushl %es; \
88         pushl %ds; \
89         pushl %eax; \
90         pushl %ebp; \
91         pushl %edi; \
92         pushl %esi; \
93         pushl %edx; \
94         pushl %ecx; \
95         pushl %ebx; \
96         movl $(__USER_DS), %edx; \
97         movl %edx, %ds; \
98         movl %edx, %es;
99
100 #define RESTORE_INT_REGS \
101         popl %ebx;      \
102         popl %ecx;      \
103         popl %edx;      \
104         popl %esi;      \
105         popl %edi;      \
106         popl %ebp;      \
107         popl %eax
108
109 #define RESTORE_REGS    \
110         RESTORE_INT_REGS; \
111 1:      popl %ds;       \
112 2:      popl %es;       \
113 .section .fixup,"ax";   \
114 3:      movl $0,(%esp); \
115         jmp 1b;         \
116 4:      movl $0,(%esp); \
117         jmp 2b;         \
118 .previous;              \
119 .section __ex_table,"a";\
120         .align 4;       \
121         .long 1b,3b;    \
122         .long 2b,4b;    \
123 .previous
124
125
126 ENTRY(ret_from_fork)
127         pushl %eax
128         call schedule_tail
129         GET_THREAD_INFO(%ebp)
130         popl %eax
131         pushl $0x0202                   # Reset kernel eflags
132         popfl
133         jmp syscall_exit
134
135 /*
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.
140  */
141
142         # userspace resumption stub bypassing syscall exit tracing
143         ALIGN
144 ret_from_exception:
145         preempt_stop
146 ret_from_intr:
147         GET_THREAD_INFO(%ebp)
148         movl EFLAGS(%esp), %eax         # mix EFLAGS and CS
149         movb CS(%esp), %al
150         testl $(VM_MASK | 3), %eax
151         jz resume_kernel
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?
159         jne work_pending
160         jmp restore_all
161
162 #ifdef CONFIG_PREEMPT
163 ENTRY(resume_kernel)
164         cli
165         cmpl $0,TI_preempt_count(%ebp)  # non-zero preempt_count ?
166         jnz restore_nocheck
167 need_resched:
168         movl TI_flags(%ebp), %ecx       # need_resched set ?
169         testb $_TIF_NEED_RESCHED, %cl
170         jz restore_all
171         testl $IF_MASK,EFLAGS(%esp)     # interrupts off (exception path) ?
172         jz restore_all
173         call preempt_schedule_irq
174         jmp need_resched
175 #endif
176
177 /* SYSENTER_RETURN points to after the "sysenter" instruction in
178    the vsyscall page.  See vsyscall-sysentry.S, which defines the symbol.  */
179
180         # sysenter call handler stub
181 ENTRY(sysenter_entry)
182         movl TSS_sysenter_esp0(%esp),%esp
183 sysenter_past_esp:
184         sti
185         pushl $(__USER_DS)
186         pushl %ebp
187         pushfl
188         pushl $(__USER_CS)
189         pushl $SYSENTER_RETURN
190
191 /*
192  * Load the potential sixth argument from user stack.
193  * Careful about security.
194  */
195         cmpl $__PAGE_OFFSET-3,%ebp
196         jae syscall_fault
197 1:      movl (%ebp),%ebp
198 .section __ex_table,"a"
199         .align 4
200         .long 1b,syscall_fault
201 .previous
202
203         pushl %eax
204         SAVE_ALL
205         GET_THREAD_INFO(%ebp)
206
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
211         jae syscall_badsys
212         call *sys_call_table(,%eax,4)
213         movl %eax,EAX(%esp)
214         cli
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 */
219         movl EIP(%esp), %edx
220         movl OLDESP(%esp), %ecx
221         xorl %ebp,%ebp
222         sti
223         sysexit
224
225
226         # system call handler stub
227 ENTRY(system_call)
228         pushl %eax                      # save orig_eax
229         SAVE_ALL
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
236         jae syscall_badsys
237 syscall_call:
238         call *sys_call_table(,%eax,4)
239         movl %eax,EAX(%esp)             # store the return value
240 syscall_exit:
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
247
248 restore_all:
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
254         movb CS(%esp), %al
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
258 restore_nocheck:
259         RESTORE_REGS
260         addl $4, %esp
261 1:      iret
262 .section .fixup,"ax"
263 iret_exc:
264         sti
265         pushl $0                        # no error code
266         pushl $do_iret_error
267         jmp error_code
268 .previous
269 .section __ex_table,"a"
270         .align 4
271         .long 1b,iret_exc
272 .previous
273
274 ldt_ss:
275         larl OLDSS(%esp), %eax
276         jnz restore_nocheck
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
281          * won't restore it.
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
286         cli
287         movl %esp, %eax
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
291         RESTORE_REGS
292         lss 20+4(%esp), %esp    # switch to 16bit stack
293 1:      iret
294 .section __ex_table,"a"
295         .align 4
296         .long 1b,iret_exc
297 .previous
298
299         # perform work that needs to be done immediately before resumption
300         ALIGN
301 work_pending:
302         testb $_TIF_NEED_RESCHED, %cl
303         jz work_notifysig
304 work_resched:
305         call schedule
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?
312         jz restore_all
313         testb $_TIF_NEED_RESCHED, %cl
314         jnz work_resched
315
316 work_notifysig:                         # deal with pending signals and
317                                         # notify-resume requests
318         testl $VM_MASK, EFLAGS(%esp)
319         movl %esp, %eax
320         jne work_notifysig_v86          # returning to kernel-space or
321                                         # vm86-space
322         xorl %edx, %edx
323         call do_notify_resume
324         jmp resume_userspace
325
326         ALIGN
327 work_notifysig_v86:
328 #ifdef CONFIG_VM86
329         pushl %ecx                      # save ti_flags for do_notify_resume
330         call save_v86_state             # %eax contains pt_regs pointer
331         popl %ecx
332         movl %eax, %esp
333         xorl %edx, %edx
334         call do_notify_resume
335         jmp resume_userspace
336 #endif
337
338         # perform syscall exit tracing
339         ALIGN
340 syscall_trace_entry:
341         movl $-ENOSYS,EAX(%esp)
342         movl %esp, %eax
343         xorl %edx,%edx
344         call do_syscall_trace
345         cmpl $0, %eax
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
350         jnae syscall_call
351         jmp syscall_exit
352
353         # perform syscall exit tracing
354         ALIGN
355 syscall_exit_work:
356         testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
357         jz work_pending
358         sti                             # could let do_syscall_trace() call
359                                         # schedule() instead
360         movl %esp, %eax
361         movl $1, %edx
362         call do_syscall_trace
363         jmp resume_userspace
364
365         ALIGN
366 syscall_fault:
367         pushl %eax                      # save orig_eax
368         SAVE_ALL
369         GET_THREAD_INFO(%ebp)
370         movl $-EFAULT,EAX(%esp)
371         jmp resume_userspace
372
373         ALIGN
374 syscall_badsys:
375         movl $-ENOSYS,EAX(%esp)
376         jmp resume_userspace
377
378 #define FIXUP_ESPFIX_STACK \
379         movl %esp, %eax; \
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 */ \
385         movl %eax, %esp;
386 #define UNWIND_ESPFIX_STACK \
387         pushl %eax; \
388         movl %ss, %eax; \
389         /* see if on 16bit stack */ \
390         cmpw $__ESPFIX_SS, %ax; \
391         jne 28f; \
392         movl $__KERNEL_DS, %edx; \
393         movl %edx, %ds; \
394         movl %edx, %es; \
395         /* switch to 32bit stack */ \
396         FIXUP_ESPFIX_STACK \
397 28:     popl %eax;
398
399 /*
400  * Build the entry stubs and pointer table with
401  * some assembler magic.
402  */
403 .data
404 ENTRY(interrupt)
405 .text
406
407 vector=0
408 ENTRY(irq_entries_start)
409 .rept NR_IRQS
410         ALIGN
411 1:      pushl $vector-256
412         jmp common_interrupt
413 .data
414         .long 1b
415 .text
416 vector=vector+1
417 .endr
418
419         ALIGN
420 common_interrupt:
421         SAVE_ALL
422         movl %esp,%eax
423         call do_IRQ
424         jmp ret_from_intr
425
426 #define BUILD_INTERRUPT(name, nr)       \
427 ENTRY(name)                             \
428         pushl $nr-256;                  \
429         SAVE_ALL                        \
430         movl %esp,%eax;                 \
431         call smp_/**/name;              \
432         jmp ret_from_intr;
433
434 /* The include is where all of the SMP etc. interrupts come from */
435 #include "entry_arch.h"
436
437 ENTRY(divide_error)
438         pushl $0                        # no error code
439         pushl $do_divide_error
440         ALIGN
441 error_code:
442         pushl %ds
443         pushl %eax
444         xorl %eax, %eax
445         pushl %ebp
446         pushl %edi
447         pushl %esi
448         pushl %edx
449         decl %eax                       # eax = -1
450         pushl %ecx
451         pushl %ebx
452         cld
453         pushl %es
454         UNWIND_ESPFIX_STACK
455         popl %ecx
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)
459         movl %ecx, ES(%esp)
460         movl $(__USER_DS), %ecx
461         movl %ecx, %ds
462         movl %ecx, %es
463         movl %esp,%eax                  # pt_regs pointer
464         call *%edi
465         jmp ret_from_exception
466
467 ENTRY(coprocessor_error)
468         pushl $0
469         pushl $do_coprocessor_error
470         jmp error_code
471
472 ENTRY(simd_coprocessor_error)
473         pushl $0
474         pushl $do_simd_coprocessor_error
475         jmp error_code
476
477 ENTRY(device_not_available)
478         pushl $-1                       # mark this as an int
479         SAVE_ALL
480         movl %cr0, %eax
481         testl $0x4, %eax                # EM (math emulation bit)
482         jne device_not_available_emulate
483         preempt_stop
484         call math_state_restore
485         jmp ret_from_exception
486 device_not_available_emulate:
487         pushl $0                        # temporary storage for ORIG_EIP
488         call math_emulate
489         addl $4, %esp
490         jmp ret_from_exception
491
492 /*
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.
496  *
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.
500  *
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.
504  */
505 #define FIX_STACK(offset, ok, label)            \
506         cmpw $__KERNEL_CS,4(%esp);              \
507         jne ok;                                 \
508 label:                                          \
509         movl TSS_sysenter_esp0+offset(%esp),%esp;       \
510         pushfl;                                 \
511         pushl $__KERNEL_CS;                     \
512         pushl $sysenter_past_esp
513
514 KPROBE_ENTRY(debug)
515         cmpl $sysenter_entry,(%esp)
516         jne debug_stack_correct
517         FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
518 debug_stack_correct:
519         pushl $-1                       # mark this as an int
520         SAVE_ALL
521         xorl %edx,%edx                  # error code 0
522         movl %esp,%eax                  # pt_regs pointer
523         call do_debug
524         jmp ret_from_exception
525         .previous .text
526 /*
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.
533  */
534 ENTRY(nmi)
535         pushl %eax
536         movl %ss, %eax
537         cmpw $__ESPFIX_SS, %ax
538         popl %eax
539         je nmi_16bit_stack
540         cmpl $sysenter_entry,(%esp)
541         je nmi_stack_fixup
542         pushl %eax
543         movl %esp,%eax
544         /* Do not access memory above the end of our stack page,
545          * it might not exist.
546          */
547         andl $(THREAD_SIZE-1),%eax
548         cmpl $(THREAD_SIZE-20),%eax
549         popl %eax
550         jae nmi_stack_correct
551         cmpl $sysenter_entry,12(%esp)
552         je nmi_debug_stack_check
553 nmi_stack_correct:
554         pushl %eax
555         SAVE_ALL
556         xorl %edx,%edx          # zero error code
557         movl %esp,%eax          # pt_regs pointer
558         call do_nmi
559         jmp restore_all
560
561 nmi_stack_fixup:
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
567         cmpl $debug,(%esp)
568         jb nmi_stack_correct
569         cmpl $debug_esp_fix_insn,(%esp)
570         ja nmi_stack_correct
571         FIX_STACK(24,nmi_stack_correct, 1)
572         jmp nmi_stack_correct
573
574 nmi_16bit_stack:
575         /* create the pointer to lss back */
576         pushl %ss
577         pushl %esp
578         movzwl %sp, %esp
579         addw $4, (%esp)
580         /* copy the iret frame of 12 bytes */
581         .rept 3
582         pushl 16(%esp)
583         .endr
584         pushl %eax
585         SAVE_ALL
586         FIXUP_ESPFIX_STACK              # %eax == %esp
587         xorl %edx,%edx                  # zero error code
588         call do_nmi
589         RESTORE_REGS
590         lss 12+4(%esp), %esp            # back to 16bit stack
591 1:      iret
592 .section __ex_table,"a"
593         .align 4
594         .long 1b,iret_exc
595 .previous
596
597 KPROBE_ENTRY(int3)
598         pushl $-1                       # mark this as an int
599         SAVE_ALL
600         xorl %edx,%edx          # zero error code
601         movl %esp,%eax          # pt_regs pointer
602         call do_int3
603         jmp ret_from_exception
604         .previous .text
605
606 ENTRY(overflow)
607         pushl $0
608         pushl $do_overflow
609         jmp error_code
610
611 ENTRY(bounds)
612         pushl $0
613         pushl $do_bounds
614         jmp error_code
615
616 ENTRY(invalid_op)
617         pushl $0
618         pushl $do_invalid_op
619         jmp error_code
620
621 ENTRY(coprocessor_segment_overrun)
622         pushl $0
623         pushl $do_coprocessor_segment_overrun
624         jmp error_code
625
626 ENTRY(invalid_TSS)
627         pushl $do_invalid_TSS
628         jmp error_code
629
630 ENTRY(segment_not_present)
631         pushl $do_segment_not_present
632         jmp error_code
633
634 ENTRY(stack_segment)
635         pushl $do_stack_segment
636         jmp error_code
637
638 KPROBE_ENTRY(general_protection)
639         pushl $do_general_protection
640         jmp error_code
641         .previous .text
642
643 ENTRY(alignment_check)
644         pushl $do_alignment_check
645         jmp error_code
646
647 KPROBE_ENTRY(page_fault)
648         pushl $do_page_fault
649         jmp error_code
650         .previous .text
651
652 #ifdef CONFIG_X86_MCE
653 ENTRY(machine_check)
654         pushl $0
655         pushl machine_check_vector
656         jmp error_code
657 #endif
658
659 ENTRY(spurious_interrupt_bug)
660         pushl $0
661         pushl $do_spurious_interrupt_bug
662         jmp error_code
663
664 .section .rodata,"a"
665 #include "syscall_table.S"
666
667 syscall_table_size=(.-sys_call_table)