Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[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         jmp syscall_exit
132
133 /*
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.
138  */
139
140         # userspace resumption stub bypassing syscall exit tracing
141         ALIGN
142 ret_from_exception:
143         preempt_stop
144 ret_from_intr:
145         GET_THREAD_INFO(%ebp)
146         movl EFLAGS(%esp), %eax         # mix EFLAGS and CS
147         movb CS(%esp), %al
148         testl $(VM_MASK | 3), %eax
149         jz resume_kernel
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?
157         jne work_pending
158         jmp restore_all
159
160 #ifdef CONFIG_PREEMPT
161 ENTRY(resume_kernel)
162         cli
163         cmpl $0,TI_preempt_count(%ebp)  # non-zero preempt_count ?
164         jnz restore_nocheck
165 need_resched:
166         movl TI_flags(%ebp), %ecx       # need_resched set ?
167         testb $_TIF_NEED_RESCHED, %cl
168         jz restore_all
169         testl $IF_MASK,EFLAGS(%esp)     # interrupts off (exception path) ?
170         jz restore_all
171         call preempt_schedule_irq
172         jmp need_resched
173 #endif
174
175 /* SYSENTER_RETURN points to after the "sysenter" instruction in
176    the vsyscall page.  See vsyscall-sysentry.S, which defines the symbol.  */
177
178         # sysenter call handler stub
179 ENTRY(sysenter_entry)
180         movl SYSENTER_stack_esp0(%esp),%esp
181 sysenter_past_esp:
182         sti
183         pushl $(__USER_DS)
184         pushl %ebp
185         pushfl
186         pushl $(__USER_CS)
187         /*
188          * Push current_thread_info()->sysenter_return to the stack.
189          * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
190          * pushed above; +8 corresponds to copy_thread's esp0 setting.
191          */
192         pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
193 /*
194  * Load the potential sixth argument from user stack.
195  * Careful about security.
196  */
197         cmpl $__PAGE_OFFSET-3,%ebp
198         jae syscall_fault
199 1:      movl (%ebp),%ebp
200 .section __ex_table,"a"
201         .align 4
202         .long 1b,syscall_fault
203 .previous
204
205         pushl %eax
206         SAVE_ALL
207         GET_THREAD_INFO(%ebp)
208
209         /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
210         testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
211         jnz syscall_trace_entry
212         cmpl $(nr_syscalls), %eax
213         jae syscall_badsys
214         call *sys_call_table(,%eax,4)
215         movl %eax,EAX(%esp)
216         cli
217         movl TI_flags(%ebp), %ecx
218         testw $_TIF_ALLWORK_MASK, %cx
219         jne syscall_exit_work
220 /* if something modifies registers it must also disable sysexit */
221         movl EIP(%esp), %edx
222         movl OLDESP(%esp), %ecx
223         xorl %ebp,%ebp
224         sti
225         sysexit
226
227
228         # system call handler stub
229 ENTRY(system_call)
230         pushl %eax                      # save orig_eax
231         SAVE_ALL
232         GET_THREAD_INFO(%ebp)
233         testl $TF_MASK,EFLAGS(%esp)
234         jz no_singlestep
235         orl $_TIF_SINGLESTEP,TI_flags(%ebp)
236 no_singlestep:
237                                         # system call tracing in operation / emulation
238         /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
239         testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
240         jnz syscall_trace_entry
241         cmpl $(nr_syscalls), %eax
242         jae syscall_badsys
243 syscall_call:
244         call *sys_call_table(,%eax,4)
245         movl %eax,EAX(%esp)             # store the return value
246 syscall_exit:
247         cli                             # make sure we don't miss an interrupt
248                                         # setting need_resched or sigpending
249                                         # between sampling and the iret
250         movl TI_flags(%ebp), %ecx
251         testw $_TIF_ALLWORK_MASK, %cx   # current->work
252         jne syscall_exit_work
253
254 restore_all:
255         movl EFLAGS(%esp), %eax         # mix EFLAGS, SS and CS
256         # Warning: OLDSS(%esp) contains the wrong/random values if we
257         # are returning to the kernel.
258         # See comments in process.c:copy_thread() for details.
259         movb OLDSS(%esp), %ah
260         movb CS(%esp), %al
261         andl $(VM_MASK | (4 << 8) | 3), %eax
262         cmpl $((4 << 8) | 3), %eax
263         je ldt_ss                       # returning to user-space with LDT SS
264 restore_nocheck:
265         RESTORE_REGS
266         addl $4, %esp
267 1:      iret
268 .section .fixup,"ax"
269 iret_exc:
270         sti
271         pushl $0                        # no error code
272         pushl $do_iret_error
273         jmp error_code
274 .previous
275 .section __ex_table,"a"
276         .align 4
277         .long 1b,iret_exc
278 .previous
279
280 ldt_ss:
281         larl OLDSS(%esp), %eax
282         jnz restore_nocheck
283         testl $0x00400000, %eax         # returning to 32bit stack?
284         jnz restore_nocheck             # allright, normal return
285         /* If returning to userspace with 16bit stack,
286          * try to fix the higher word of ESP, as the CPU
287          * won't restore it.
288          * This is an "official" bug of all the x86-compatible
289          * CPUs, which we can try to work around to make
290          * dosemu and wine happy. */
291         subl $8, %esp           # reserve space for switch16 pointer
292         cli
293         movl %esp, %eax
294         /* Set up the 16bit stack frame with switch32 pointer on top,
295          * and a switch16 pointer on top of the current frame. */
296         call setup_x86_bogus_stack
297         RESTORE_REGS
298         lss 20+4(%esp), %esp    # switch to 16bit stack
299 1:      iret
300 .section __ex_table,"a"
301         .align 4
302         .long 1b,iret_exc
303 .previous
304
305         # perform work that needs to be done immediately before resumption
306         ALIGN
307 work_pending:
308         testb $_TIF_NEED_RESCHED, %cl
309         jz work_notifysig
310 work_resched:
311         call schedule
312         cli                             # make sure we don't miss an interrupt
313                                         # setting need_resched or sigpending
314                                         # between sampling and the iret
315         movl TI_flags(%ebp), %ecx
316         andl $_TIF_WORK_MASK, %ecx      # is there any work to be done other
317                                         # than syscall tracing?
318         jz restore_all
319         testb $_TIF_NEED_RESCHED, %cl
320         jnz work_resched
321
322 work_notifysig:                         # deal with pending signals and
323                                         # notify-resume requests
324         testl $VM_MASK, EFLAGS(%esp)
325         movl %esp, %eax
326         jne work_notifysig_v86          # returning to kernel-space or
327                                         # vm86-space
328         xorl %edx, %edx
329         call do_notify_resume
330         jmp resume_userspace
331
332         ALIGN
333 work_notifysig_v86:
334 #ifdef CONFIG_VM86
335         pushl %ecx                      # save ti_flags for do_notify_resume
336         call save_v86_state             # %eax contains pt_regs pointer
337         popl %ecx
338         movl %eax, %esp
339         xorl %edx, %edx
340         call do_notify_resume
341         jmp resume_userspace
342 #endif
343
344         # perform syscall exit tracing
345         ALIGN
346 syscall_trace_entry:
347         movl $-ENOSYS,EAX(%esp)
348         movl %esp, %eax
349         xorl %edx,%edx
350         call do_syscall_trace
351         cmpl $0, %eax
352         jne resume_userspace            # ret != 0 -> running under PTRACE_SYSEMU,
353                                         # so must skip actual syscall
354         movl ORIG_EAX(%esp), %eax
355         cmpl $(nr_syscalls), %eax
356         jnae syscall_call
357         jmp syscall_exit
358
359         # perform syscall exit tracing
360         ALIGN
361 syscall_exit_work:
362         testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
363         jz work_pending
364         sti                             # could let do_syscall_trace() call
365                                         # schedule() instead
366         movl %esp, %eax
367         movl $1, %edx
368         call do_syscall_trace
369         jmp resume_userspace
370
371         ALIGN
372 syscall_fault:
373         pushl %eax                      # save orig_eax
374         SAVE_ALL
375         GET_THREAD_INFO(%ebp)
376         movl $-EFAULT,EAX(%esp)
377         jmp resume_userspace
378
379         ALIGN
380 syscall_badsys:
381         movl $-ENOSYS,EAX(%esp)
382         jmp resume_userspace
383
384 #define FIXUP_ESPFIX_STACK \
385         movl %esp, %eax; \
386         /* switch to 32bit stack using the pointer on top of 16bit stack */ \
387         lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
388         /* copy data from 16bit stack to 32bit stack */ \
389         call fixup_x86_bogus_stack; \
390         /* put ESP to the proper location */ \
391         movl %eax, %esp;
392 #define UNWIND_ESPFIX_STACK \
393         pushl %eax; \
394         movl %ss, %eax; \
395         /* see if on 16bit stack */ \
396         cmpw $__ESPFIX_SS, %ax; \
397         jne 28f; \
398         movl $__KERNEL_DS, %edx; \
399         movl %edx, %ds; \
400         movl %edx, %es; \
401         /* switch to 32bit stack */ \
402         FIXUP_ESPFIX_STACK \
403 28:     popl %eax;
404
405 /*
406  * Build the entry stubs and pointer table with
407  * some assembler magic.
408  */
409 .data
410 ENTRY(interrupt)
411 .text
412
413 vector=0
414 ENTRY(irq_entries_start)
415 .rept NR_IRQS
416         ALIGN
417 1:      pushl $~(vector)
418         jmp common_interrupt
419 .data
420         .long 1b
421 .text
422 vector=vector+1
423 .endr
424
425         ALIGN
426 common_interrupt:
427         SAVE_ALL
428         movl %esp,%eax
429         call do_IRQ
430         jmp ret_from_intr
431
432 #define BUILD_INTERRUPT(name, nr)       \
433 ENTRY(name)                             \
434         pushl $~(nr);                   \
435         SAVE_ALL                        \
436         movl %esp,%eax;                 \
437         call smp_/**/name;              \
438         jmp ret_from_intr;
439
440 /* The include is where all of the SMP etc. interrupts come from */
441 #include "entry_arch.h"
442
443 ENTRY(divide_error)
444         pushl $0                        # no error code
445         pushl $do_divide_error
446         ALIGN
447 error_code:
448         pushl %ds
449         pushl %eax
450         xorl %eax, %eax
451         pushl %ebp
452         pushl %edi
453         pushl %esi
454         pushl %edx
455         decl %eax                       # eax = -1
456         pushl %ecx
457         pushl %ebx
458         cld
459         pushl %es
460         UNWIND_ESPFIX_STACK
461         popl %ecx
462         movl ES(%esp), %edi             # get the function address
463         movl ORIG_EAX(%esp), %edx       # get the error code
464         movl %eax, ORIG_EAX(%esp)
465         movl %ecx, ES(%esp)
466         movl $(__USER_DS), %ecx
467         movl %ecx, %ds
468         movl %ecx, %es
469         movl %esp,%eax                  # pt_regs pointer
470         call *%edi
471         jmp ret_from_exception
472
473 ENTRY(coprocessor_error)
474         pushl $0
475         pushl $do_coprocessor_error
476         jmp error_code
477
478 ENTRY(simd_coprocessor_error)
479         pushl $0
480         pushl $do_simd_coprocessor_error
481         jmp error_code
482
483 ENTRY(device_not_available)
484         pushl $-1                       # mark this as an int
485         SAVE_ALL
486         movl %cr0, %eax
487         testl $0x4, %eax                # EM (math emulation bit)
488         jne device_not_available_emulate
489         preempt_stop
490         call math_state_restore
491         jmp ret_from_exception
492 device_not_available_emulate:
493         pushl $0                        # temporary storage for ORIG_EIP
494         call math_emulate
495         addl $4, %esp
496         jmp ret_from_exception
497
498 /*
499  * Debug traps and NMI can happen at the one SYSENTER instruction
500  * that sets up the real kernel stack. Check here, since we can't
501  * allow the wrong stack to be used.
502  *
503  * "SYSENTER_stack_esp0+12" is because the NMI/debug handler will have
504  * already pushed 3 words if it hits on the sysenter instruction:
505  * eflags, cs and eip.
506  *
507  * We just load the right stack, and push the three (known) values
508  * by hand onto the new stack - while updating the return eip past
509  * the instruction that would have done it for sysenter.
510  */
511 #define FIX_STACK(offset, ok, label)            \
512         cmpw $__KERNEL_CS,4(%esp);              \
513         jne ok;                                 \
514 label:                                          \
515         movl SYSENTER_stack_esp0+offset(%esp),%esp;     \
516         pushfl;                                 \
517         pushl $__KERNEL_CS;                     \
518         pushl $sysenter_past_esp
519
520 KPROBE_ENTRY(debug)
521         cmpl $sysenter_entry,(%esp)
522         jne debug_stack_correct
523         FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
524 debug_stack_correct:
525         pushl $-1                       # mark this as an int
526         SAVE_ALL
527         xorl %edx,%edx                  # error code 0
528         movl %esp,%eax                  # pt_regs pointer
529         call do_debug
530         jmp ret_from_exception
531         .previous .text
532 /*
533  * NMI is doubly nasty. It can happen _while_ we're handling
534  * a debug fault, and the debug fault hasn't yet been able to
535  * clear up the stack. So we first check whether we got  an
536  * NMI on the sysenter entry path, but after that we need to
537  * check whether we got an NMI on the debug path where the debug
538  * fault happened on the sysenter path.
539  */
540 ENTRY(nmi)
541         pushl %eax
542         movl %ss, %eax
543         cmpw $__ESPFIX_SS, %ax
544         popl %eax
545         je nmi_16bit_stack
546         cmpl $sysenter_entry,(%esp)
547         je nmi_stack_fixup
548         pushl %eax
549         movl %esp,%eax
550         /* Do not access memory above the end of our stack page,
551          * it might not exist.
552          */
553         andl $(THREAD_SIZE-1),%eax
554         cmpl $(THREAD_SIZE-20),%eax
555         popl %eax
556         jae nmi_stack_correct
557         cmpl $sysenter_entry,12(%esp)
558         je nmi_debug_stack_check
559 nmi_stack_correct:
560         pushl %eax
561         SAVE_ALL
562         xorl %edx,%edx          # zero error code
563         movl %esp,%eax          # pt_regs pointer
564         call do_nmi
565         jmp restore_all
566
567 nmi_stack_fixup:
568         FIX_STACK(12,nmi_stack_correct, 1)
569         jmp nmi_stack_correct
570 nmi_debug_stack_check:
571         cmpw $__KERNEL_CS,16(%esp)
572         jne nmi_stack_correct
573         cmpl $debug,(%esp)
574         jb nmi_stack_correct
575         cmpl $debug_esp_fix_insn,(%esp)
576         ja nmi_stack_correct
577         FIX_STACK(24,nmi_stack_correct, 1)
578         jmp nmi_stack_correct
579
580 nmi_16bit_stack:
581         /* create the pointer to lss back */
582         pushl %ss
583         pushl %esp
584         movzwl %sp, %esp
585         addw $4, (%esp)
586         /* copy the iret frame of 12 bytes */
587         .rept 3
588         pushl 16(%esp)
589         .endr
590         pushl %eax
591         SAVE_ALL
592         FIXUP_ESPFIX_STACK              # %eax == %esp
593         xorl %edx,%edx                  # zero error code
594         call do_nmi
595         RESTORE_REGS
596         lss 12+4(%esp), %esp            # back to 16bit stack
597 1:      iret
598 .section __ex_table,"a"
599         .align 4
600         .long 1b,iret_exc
601 .previous
602
603 KPROBE_ENTRY(int3)
604         pushl $-1                       # mark this as an int
605         SAVE_ALL
606         xorl %edx,%edx          # zero error code
607         movl %esp,%eax          # pt_regs pointer
608         call do_int3
609         jmp ret_from_exception
610         .previous .text
611
612 ENTRY(overflow)
613         pushl $0
614         pushl $do_overflow
615         jmp error_code
616
617 ENTRY(bounds)
618         pushl $0
619         pushl $do_bounds
620         jmp error_code
621
622 ENTRY(invalid_op)
623         pushl $0
624         pushl $do_invalid_op
625         jmp error_code
626
627 ENTRY(coprocessor_segment_overrun)
628         pushl $0
629         pushl $do_coprocessor_segment_overrun
630         jmp error_code
631
632 ENTRY(invalid_TSS)
633         pushl $do_invalid_TSS
634         jmp error_code
635
636 ENTRY(segment_not_present)
637         pushl $do_segment_not_present
638         jmp error_code
639
640 ENTRY(stack_segment)
641         pushl $do_stack_segment
642         jmp error_code
643
644 KPROBE_ENTRY(general_protection)
645         pushl $do_general_protection
646         jmp error_code
647         .previous .text
648
649 ENTRY(alignment_check)
650         pushl $do_alignment_check
651         jmp error_code
652
653 KPROBE_ENTRY(page_fault)
654         pushl $do_page_fault
655         jmp error_code
656         .previous .text
657
658 #ifdef CONFIG_X86_MCE
659 ENTRY(machine_check)
660         pushl $0
661         pushl machine_check_vector
662         jmp error_code
663 #endif
664
665 ENTRY(spurious_interrupt_bug)
666         pushl $0
667         pushl $do_spurious_interrupt_bug
668         jmp error_code
669
670 .section .rodata,"a"
671 #include "syscall_table.S"
672
673 syscall_table_size=(.-sys_call_table)