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