ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / traps.c
1 /*
2  *  linux/arch/i386/traps.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Pentium III FXSR, SSE support
7  *      Gareth Hughes <gareth@valinux.com>, May 2000
8  */
9
10 /*
11  * 'Traps.c' handles hardware traps and faults after we have saved some
12  * state in 'asm.s'.
13  */
14 #include <linux/config.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/timer.h>
20 #include <linux/mm.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/highmem.h>
26 #include <linux/kallsyms.h>
27 #include <linux/ptrace.h>
28 #include <linux/version.h>
29
30 #ifdef CONFIG_EISA
31 #include <linux/ioport.h>
32 #include <linux/eisa.h>
33 #endif
34
35 #ifdef CONFIG_MCA
36 #include <linux/mca.h>
37 #endif
38
39 #include <asm/processor.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43 #include <asm/atomic.h>
44 #include <asm/debugreg.h>
45 #include <asm/desc.h>
46 #include <asm/i387.h>
47 #include <asm/nmi.h>
48
49 #include <asm/smp.h>
50 #include <asm/pgalloc.h>
51 #include <asm/arch_hooks.h>
52
53 #include <linux/irq.h>
54 #include <linux/module.h>
55
56 #include "mach_traps.h"
57
58 asmlinkage int system_call(void);
59 asmlinkage void lcall7(void);
60 asmlinkage void lcall27(void);
61
62 struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
63                 { 0, 0 }, { 0, 0 } };
64
65 /* Do we ignore FPU interrupts ? */
66 char ignore_fpu_irq = 0;
67
68 /*
69  * The IDT has to be page-aligned to simplify the Pentium
70  * F0 0F bug workaround.. We have a special link segment
71  * for this.
72  */
73 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
74
75 asmlinkage void divide_error(void);
76 asmlinkage void debug(void);
77 asmlinkage void nmi(void);
78 asmlinkage void int3(void);
79 asmlinkage void overflow(void);
80 asmlinkage void bounds(void);
81 asmlinkage void invalid_op(void);
82 asmlinkage void device_not_available(void);
83 asmlinkage void coprocessor_segment_overrun(void);
84 asmlinkage void invalid_TSS(void);
85 asmlinkage void segment_not_present(void);
86 asmlinkage void stack_segment(void);
87 asmlinkage void general_protection(void);
88 asmlinkage void page_fault(void);
89 asmlinkage void coprocessor_error(void);
90 asmlinkage void simd_coprocessor_error(void);
91 asmlinkage void alignment_check(void);
92 asmlinkage void spurious_interrupt_bug(void);
93 asmlinkage void machine_check(void);
94
95 static int kstack_depth_to_print = 24;
96
97 void show_trace(struct task_struct *task, unsigned long * stack)
98 {
99         unsigned long addr;
100
101         if (!stack)
102                 stack = (unsigned long*)&stack;
103
104         printk("Call Trace:");
105 #ifdef CONFIG_KALLSYMS
106         printk("\n");
107 #endif
108         while (1) {
109                 struct thread_info *context;
110                 context = (struct thread_info*) ((unsigned long)stack & (~(THREAD_SIZE - 1)));
111                 while (!kstack_end(stack)) {
112                         addr = *stack++;
113                         if (kernel_text_address(addr)) {
114                                 printk(" [<%08lx>] ", addr);
115                                 print_symbol("%s\n", addr);
116                         }
117                 }
118                 stack = (unsigned long*)context->previous_esp;
119                 if (!stack)
120                         break;
121                 printk(" =======================\n");
122         }
123         printk("\n");
124 }
125
126 void show_stack(struct task_struct *task, unsigned long *esp)
127 {
128         unsigned long *stack;
129         int i;
130
131         if (esp == NULL) {
132                 if (task)
133                         esp = (unsigned long*)task->thread.esp;
134                 else
135                         esp = (unsigned long *)&esp;
136         }
137
138         stack = esp;
139         for(i = 0; i < kstack_depth_to_print; i++) {
140                 if (kstack_end(stack))
141                         break;
142                 if (i && ((i % 8) == 0))
143                         printk("\n       ");
144                 printk("%08lx ", *stack++);
145         }
146         printk("\n");
147         show_trace(task, esp);
148 }
149
150 /*
151  * The architecture-independent dump_stack generator
152  */
153 void dump_stack(void)
154 {
155         unsigned long stack;
156
157         show_trace(current, &stack);
158 }
159
160 EXPORT_SYMBOL(dump_stack);
161
162 void show_registers(struct pt_regs *regs)
163 {
164         int i;
165         int in_kernel = 1;
166         unsigned long esp;
167         unsigned short ss;
168
169         esp = (unsigned long) (&regs->esp);
170         ss = __KERNEL_DS;
171         if (regs->xcs & 3) {
172                 in_kernel = 0;
173                 esp = regs->esp;
174                 ss = regs->xss & 0xffff;
175         }
176         print_modules();
177         printk("CPU:    %d\nEIP:    %04x:[<%08lx>]    %s\nEFLAGS: %08lx"
178                         "   (%s) \n",
179                 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
180                 print_tainted(), regs->eflags, UTS_RELEASE);
181         print_symbol("EIP is at %s\n", regs->eip);
182         printk("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
183                 regs->eax, regs->ebx, regs->ecx, regs->edx);
184         printk("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
185                 regs->esi, regs->edi, regs->ebp, esp);
186         printk("ds: %04x   es: %04x   ss: %04x\n",
187                 regs->xds & 0xffff, regs->xes & 0xffff, ss);
188         printk("Process %s (pid: %d, threadinfo=%p task=%p)",
189                 current->comm, current->pid, current_thread_info(), current);
190         /*
191          * When in-kernel, we also print out the stack and code at the
192          * time of the fault..
193          */
194         if (in_kernel) {
195
196                 printk("\nStack: ");
197                 show_stack(NULL, (unsigned long*)esp);
198
199                 printk("Code: ");
200                 if(regs->eip < PAGE_OFFSET)
201                         goto bad;
202
203                 for(i=0;i<20;i++)
204                 {
205                         unsigned char c;
206                         if(__get_user(c, &((unsigned char*)regs->eip)[i])) {
207 bad:
208                                 printk(" Bad EIP value.");
209                                 break;
210                         }
211                         printk("%02x ", c);
212                 }
213         }
214         printk("\n");
215 }       
216
217 static void handle_BUG(struct pt_regs *regs)
218 {
219         unsigned short ud2;
220         unsigned short line;
221         char *file;
222         char c;
223         unsigned long eip;
224
225         if (regs->xcs & 3)
226                 goto no_bug;            /* Not in kernel */
227
228         eip = regs->eip;
229
230         if (eip < PAGE_OFFSET)
231                 goto no_bug;
232         if (__get_user(ud2, (unsigned short *)eip))
233                 goto no_bug;
234         if (ud2 != 0x0b0f)
235                 goto no_bug;
236         if (__get_user(line, (unsigned short *)(eip + 2)))
237                 goto bug;
238         if (__get_user(file, (char **)(eip + 4)) ||
239                 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
240                 file = "<bad filename>";
241
242         printk("------------[ cut here ]------------\n");
243         printk("kernel BUG at %s:%d!\n", file, line);
244
245 no_bug:
246         return;
247
248         /* Here we know it was a BUG but file-n-line is unavailable */
249 bug:
250         printk("Kernel BUG\n");
251 }
252
253 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
254
255 void die(const char * str, struct pt_regs * regs, long err)
256 {
257         static int die_counter;
258         int nl = 0;
259
260         console_verbose();
261         spin_lock_irq(&die_lock);
262         bust_spinlocks(1);
263         handle_BUG(regs);
264         printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
265 #ifdef CONFIG_PREEMPT
266         printk("PREEMPT ");
267         nl = 1;
268 #endif
269 #ifdef CONFIG_SMP
270         printk("SMP ");
271         nl = 1;
272 #endif
273 #ifdef CONFIG_DEBUG_PAGEALLOC
274         printk("DEBUG_PAGEALLOC");
275         nl = 1;
276 #endif
277         if (nl)
278                 printk("\n");
279         show_registers(regs);
280         bust_spinlocks(0);
281         spin_unlock_irq(&die_lock);
282         if (in_interrupt())
283                 panic("Fatal exception in interrupt");
284
285         if (panic_on_oops) {
286                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
287                 set_current_state(TASK_UNINTERRUPTIBLE);
288                 schedule_timeout(5 * HZ);
289                 panic("Fatal exception");
290         }
291         do_exit(SIGSEGV);
292 }
293
294 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
295 {
296         if (!(regs->eflags & VM_MASK) && !(3 & regs->xcs))
297                 die(str, regs, err);
298 }
299
300 static inline unsigned long get_cr2(void)
301 {
302         unsigned long address;
303
304         /* get the address */
305         __asm__("movl %%cr2,%0":"=r" (address));
306         return address;
307 }
308
309 static inline void do_trap(int trapnr, int signr, char *str, int vm86,
310                            struct pt_regs * regs, long error_code, siginfo_t *info)
311 {
312         if (regs->eflags & VM_MASK) {
313                 if (vm86)
314                         goto vm86_trap;
315                 goto trap_signal;
316         }
317
318         if (!(regs->xcs & 3))
319                 goto kernel_trap;
320
321         trap_signal: {
322                 struct task_struct *tsk = current;
323                 tsk->thread.error_code = error_code;
324                 tsk->thread.trap_no = trapnr;
325                 if (info)
326                         force_sig_info(signr, info, tsk);
327                 else
328                         force_sig(signr, tsk);
329                 return;
330         }
331
332         kernel_trap: {
333                 if (!fixup_exception(regs))
334                         die(str, regs, error_code);
335                 return;
336         }
337
338         vm86_trap: {
339                 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
340                 if (ret) goto trap_signal;
341                 return;
342         }
343 }
344
345 #define DO_ERROR(trapnr, signr, str, name) \
346 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
347 { \
348         do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
349 }
350
351 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
352 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
353 { \
354         siginfo_t info; \
355         info.si_signo = signr; \
356         info.si_errno = 0; \
357         info.si_code = sicode; \
358         info.si_addr = (void *)siaddr; \
359         do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
360 }
361
362 #define DO_VM86_ERROR(trapnr, signr, str, name) \
363 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
364 { \
365         do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
366 }
367
368 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
369 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
370 { \
371         siginfo_t info; \
372         info.si_signo = signr; \
373         info.si_errno = 0; \
374         info.si_code = sicode; \
375         info.si_addr = (void *)siaddr; \
376         do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
377 }
378
379 DO_VM86_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->eip)
380 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
381 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
382 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
383 DO_ERROR_INFO( 6, SIGILL,  "invalid operand", invalid_op, ILL_ILLOPN, regs->eip)
384 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)
385 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
386 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)
387 DO_ERROR(12, SIGBUS,  "stack segment", stack_segment)
388 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2())
389
390 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
391 {
392         if (regs->eflags & X86_EFLAGS_IF)
393                 local_irq_enable();
394  
395         if (regs->eflags & VM_MASK)
396                 goto gp_in_vm86;
397
398         if (!(regs->xcs & 3))
399                 goto gp_in_kernel;
400
401         current->thread.error_code = error_code;
402         current->thread.trap_no = 13;
403         force_sig(SIGSEGV, current);
404         return;
405
406 gp_in_vm86:
407         local_irq_enable();
408         handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
409         return;
410
411 gp_in_kernel:
412         if (!fixup_exception(regs))
413                 die("general protection fault", regs, error_code);
414 }
415
416 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
417 {
418         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
419         printk("You probably have a hardware problem with your RAM chips\n");
420
421         /* Clear and disable the memory parity error line. */
422         clear_mem_error(reason);
423 }
424
425 static void io_check_error(unsigned char reason, struct pt_regs * regs)
426 {
427         unsigned long i;
428
429         printk("NMI: IOCK error (debug interrupt?)\n");
430         show_registers(regs);
431
432         /* Re-enable the IOCK line, wait for a few seconds */
433         reason = (reason & 0xf) | 8;
434         outb(reason, 0x61);
435         i = 2000;
436         while (--i) udelay(1000);
437         reason &= ~8;
438         outb(reason, 0x61);
439 }
440
441 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
442 {
443 #ifdef CONFIG_MCA
444         /* Might actually be able to figure out what the guilty party
445         * is. */
446         if( MCA_bus ) {
447                 mca_handle_nmi();
448                 return;
449         }
450 #endif
451         printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
452                 reason, smp_processor_id());
453         printk("Dazed and confused, but trying to continue\n");
454         printk("Do you have a strange power saving mode enabled?\n");
455 }
456
457 static void default_do_nmi(struct pt_regs * regs)
458 {
459         unsigned char reason = get_nmi_reason();
460  
461         if (!(reason & 0xc0)) {
462 #ifdef CONFIG_X86_LOCAL_APIC
463                 /*
464                  * Ok, so this is none of the documented NMI sources,
465                  * so it must be the NMI watchdog.
466                  */
467                 if (nmi_watchdog) {
468                         nmi_watchdog_tick(regs);
469                         return;
470                 }
471 #endif
472                 unknown_nmi_error(reason, regs);
473                 return;
474         }
475         if (reason & 0x80)
476                 mem_parity_error(reason, regs);
477         if (reason & 0x40)
478                 io_check_error(reason, regs);
479         /*
480          * Reassert NMI in case it became active meanwhile
481          * as it's edge-triggered.
482          */
483         reassert_nmi();
484 }
485
486 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
487 {
488         return 0;
489 }
490  
491 static nmi_callback_t nmi_callback = dummy_nmi_callback;
492  
493 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
494 {
495         int cpu;
496
497         nmi_enter();
498
499         cpu = smp_processor_id();
500         ++nmi_count(cpu);
501
502         if (!nmi_callback(regs, cpu))
503                 default_do_nmi(regs);
504
505         nmi_exit();
506 }
507
508 void set_nmi_callback(nmi_callback_t callback)
509 {
510         nmi_callback = callback;
511 }
512
513 void unset_nmi_callback(void)
514 {
515         nmi_callback = dummy_nmi_callback;
516 }
517
518 /*
519  * Our handling of the processor debug registers is non-trivial.
520  * We do not clear them on entry and exit from the kernel. Therefore
521  * it is possible to get a watchpoint trap here from inside the kernel.
522  * However, the code in ./ptrace.c has ensured that the user can
523  * only set watchpoints on userspace addresses. Therefore the in-kernel
524  * watchpoint trap can only occur in code which is reading/writing
525  * from user space. Such code must not hold kernel locks (since it
526  * can equally take a page fault), therefore it is safe to call
527  * force_sig_info even though that claims and releases locks.
528  * 
529  * Code in ./signal.c ensures that the debug control register
530  * is restored before we deliver any signal, and therefore that
531  * user code runs with the correct debug control register even though
532  * we clear it here.
533  *
534  * Being careful here means that we don't have to be as careful in a
535  * lot of more complicated places (task switching can be a bit lazy
536  * about restoring all the debug state, and ptrace doesn't have to
537  * find every occurrence of the TF bit that could be saved away even
538  * by user code)
539  */
540 asmlinkage void do_debug(struct pt_regs * regs, long error_code)
541 {
542         unsigned int condition;
543         struct task_struct *tsk = current;
544         siginfo_t info;
545
546         __asm__ __volatile__("movl %%db6,%0" : "=r" (condition));
547
548         /* It's safe to allow irq's after DR6 has been saved */
549         if (regs->eflags & X86_EFLAGS_IF)
550                 local_irq_enable();
551
552         /* Mask out spurious debug traps due to lazy DR7 setting */
553         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
554                 if (!tsk->thread.debugreg[7])
555                         goto clear_dr7;
556         }
557
558         if (regs->eflags & VM_MASK)
559                 goto debug_vm86;
560
561         /* Save debug status register where ptrace can see it */
562         tsk->thread.debugreg[6] = condition;
563
564         /* Mask out spurious TF errors due to lazy TF clearing */
565         if (condition & DR_STEP) {
566                 /*
567                  * The TF error should be masked out only if the current
568                  * process is not traced and if the TRAP flag has been set
569                  * previously by a tracing process (condition detected by
570                  * the PT_DTRACE flag); remember that the i386 TRAP flag
571                  * can be modified by the process itself in user mode,
572                  * allowing programs to debug themselves without the ptrace()
573                  * interface.
574                  */
575                 if ((regs->xcs & 3) == 0)
576                         goto clear_TF_reenable;
577                 if ((tsk->ptrace & (PT_DTRACE|PT_PTRACED)) == PT_DTRACE)
578                         goto clear_TF;
579         }
580
581         /* Ok, finally something we can handle */
582         tsk->thread.trap_no = 1;
583         tsk->thread.error_code = error_code;
584         info.si_signo = SIGTRAP;
585         info.si_errno = 0;
586         info.si_code = TRAP_BRKPT;
587         
588         /* If this is a kernel mode trap, save the user PC on entry to 
589          * the kernel, that's what the debugger can make sense of.
590          */
591         info.si_addr = ((regs->xcs & 3) == 0) ? (void *)tsk->thread.eip : 
592                                                 (void *)regs->eip;
593         force_sig_info(SIGTRAP, &info, tsk);
594
595         /* Disable additional traps. They'll be re-enabled when
596          * the signal is delivered.
597          */
598 clear_dr7:
599         __asm__("movl %0,%%db7"
600                 : /* no output */
601                 : "r" (0));
602         return;
603
604 debug_vm86:
605         handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
606         return;
607
608 clear_TF_reenable:
609         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
610 clear_TF:
611         regs->eflags &= ~TF_MASK;
612         return;
613 }
614
615 /*
616  * Note that we play around with the 'TS' bit in an attempt to get
617  * the correct behaviour even in the presence of the asynchronous
618  * IRQ13 behaviour
619  */
620 void math_error(void *eip)
621 {
622         struct task_struct * task;
623         siginfo_t info;
624         unsigned short cwd, swd;
625
626         /*
627          * Save the info for the exception handler and clear the error.
628          */
629         task = current;
630         save_init_fpu(task);
631         task->thread.trap_no = 16;
632         task->thread.error_code = 0;
633         info.si_signo = SIGFPE;
634         info.si_errno = 0;
635         info.si_code = __SI_FAULT;
636         info.si_addr = eip;
637         /*
638          * (~cwd & swd) will mask out exceptions that are not set to unmasked
639          * status.  0x3f is the exception bits in these regs, 0x200 is the
640          * C1 reg you need in case of a stack fault, 0x040 is the stack
641          * fault bit.  We should only be taking one exception at a time,
642          * so if this combination doesn't produce any single exception,
643          * then we have a bad program that isn't syncronizing its FPU usage
644          * and it will suffer the consequences since we won't be able to
645          * fully reproduce the context of the exception
646          */
647         cwd = get_fpu_cwd(task);
648         swd = get_fpu_swd(task);
649         switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
650                 case 0x000:
651                 default:
652                         break;
653                 case 0x001: /* Invalid Op */
654                 case 0x041: /* Stack Fault */
655                 case 0x241: /* Stack Fault | Direction */
656                         info.si_code = FPE_FLTINV;
657                         /* Should we clear the SF or let user space do it ???? */
658                         break;
659                 case 0x002: /* Denormalize */
660                 case 0x010: /* Underflow */
661                         info.si_code = FPE_FLTUND;
662                         break;
663                 case 0x004: /* Zero Divide */
664                         info.si_code = FPE_FLTDIV;
665                         break;
666                 case 0x008: /* Overflow */
667                         info.si_code = FPE_FLTOVF;
668                         break;
669                 case 0x020: /* Precision */
670                         info.si_code = FPE_FLTRES;
671                         break;
672         }
673         force_sig_info(SIGFPE, &info, task);
674 }
675
676 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
677 {
678         ignore_fpu_irq = 1;
679         math_error((void *)regs->eip);
680 }
681
682 void simd_math_error(void *eip)
683 {
684         struct task_struct * task;
685         siginfo_t info;
686         unsigned short mxcsr;
687
688         /*
689          * Save the info for the exception handler and clear the error.
690          */
691         task = current;
692         save_init_fpu(task);
693         task->thread.trap_no = 19;
694         task->thread.error_code = 0;
695         info.si_signo = SIGFPE;
696         info.si_errno = 0;
697         info.si_code = __SI_FAULT;
698         info.si_addr = eip;
699         /*
700          * The SIMD FPU exceptions are handled a little differently, as there
701          * is only a single status/control register.  Thus, to determine which
702          * unmasked exception was caught we must mask the exception mask bits
703          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
704          */
705         mxcsr = get_fpu_mxcsr(task);
706         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
707                 case 0x000:
708                 default:
709                         break;
710                 case 0x001: /* Invalid Op */
711                         info.si_code = FPE_FLTINV;
712                         break;
713                 case 0x002: /* Denormalize */
714                 case 0x010: /* Underflow */
715                         info.si_code = FPE_FLTUND;
716                         break;
717                 case 0x004: /* Zero Divide */
718                         info.si_code = FPE_FLTDIV;
719                         break;
720                 case 0x008: /* Overflow */
721                         info.si_code = FPE_FLTOVF;
722                         break;
723                 case 0x020: /* Precision */
724                         info.si_code = FPE_FLTRES;
725                         break;
726         }
727         force_sig_info(SIGFPE, &info, task);
728 }
729
730 asmlinkage void do_simd_coprocessor_error(struct pt_regs * regs,
731                                           long error_code)
732 {
733         if (cpu_has_xmm) {
734                 /* Handle SIMD FPU exceptions on PIII+ processors. */
735                 ignore_fpu_irq = 1;
736                 simd_math_error((void *)regs->eip);
737         } else {
738                 /*
739                  * Handle strange cache flush from user space exception
740                  * in all other cases.  This is undocumented behaviour.
741                  */
742                 if (regs->eflags & VM_MASK) {
743                         handle_vm86_fault((struct kernel_vm86_regs *)regs,
744                                           error_code);
745                         return;
746                 }
747                 die_if_kernel("cache flush denied", regs, error_code);
748                 current->thread.trap_no = 19;
749                 current->thread.error_code = error_code;
750                 force_sig(SIGSEGV, current);
751         }
752 }
753
754 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs,
755                                           long error_code)
756 {
757 #if 0
758         /* No need to warn about this any longer. */
759         printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
760 #endif
761 }
762
763 /*
764  *  'math_state_restore()' saves the current math information in the
765  * old math state array, and gets the new ones from the current task
766  *
767  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
768  * Don't touch unless you *really* know how it works.
769  *
770  * Must be called with kernel preemption disabled (in this case,
771  * local interrupts are disabled at the call-site in entry.S).
772  */
773 asmlinkage void math_state_restore(struct pt_regs regs)
774 {
775         struct thread_info *thread = current_thread_info();
776         struct task_struct *tsk = thread->task;
777
778         clts();         /* Allow maths ops (or we recurse) */
779         if (!tsk->used_math)
780                 init_fpu(tsk);
781         restore_fpu(tsk);
782         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
783 }
784
785 #ifndef CONFIG_MATH_EMULATION
786
787 asmlinkage void math_emulate(long arg)
788 {
789         printk("math-emulation not enabled and no coprocessor found.\n");
790         printk("killing %s.\n",current->comm);
791         force_sig(SIGFPE,current);
792         schedule();
793 }
794
795 #endif /* CONFIG_MATH_EMULATION */
796
797 #ifdef CONFIG_X86_F00F_BUG
798 void __init trap_init_f00f_bug(void)
799 {
800         __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
801
802         /*
803          * Update the IDT descriptor and reload the IDT so that
804          * it uses the read-only mapped virtual address.
805          */
806         idt_descr.address = fix_to_virt(FIX_F00F_IDT);
807         __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
808 }
809 #endif
810
811 #define _set_gate(gate_addr,type,dpl,addr,seg) \
812 do { \
813   int __d0, __d1; \
814   __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
815         "movw %4,%%dx\n\t" \
816         "movl %%eax,%0\n\t" \
817         "movl %%edx,%1" \
818         :"=m" (*((long *) (gate_addr))), \
819          "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
820         :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
821          "3" ((char *) (addr)),"2" ((seg) << 16)); \
822 } while (0)
823
824
825 /*
826  * This needs to use 'idt_table' rather than 'idt', and
827  * thus use the _nonmapped_ version of the IDT, as the
828  * Pentium F0 0F bugfix can have resulted in the mapped
829  * IDT being write-protected.
830  */
831 void set_intr_gate(unsigned int n, void *addr)
832 {
833         _set_gate(idt_table+n,14,0,addr,__KERNEL_CS);
834 }
835
836 static void __init set_trap_gate(unsigned int n, void *addr)
837 {
838         _set_gate(idt_table+n,15,0,addr,__KERNEL_CS);
839 }
840
841 static void __init set_system_gate(unsigned int n, void *addr)
842 {
843         _set_gate(idt_table+n,15,3,addr,__KERNEL_CS);
844 }
845
846 static void __init set_call_gate(void *a, void *addr)
847 {
848         _set_gate(a,12,3,addr,__KERNEL_CS);
849 }
850
851 static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
852 {
853         _set_gate(idt_table+n,5,0,0,(gdt_entry<<3));
854 }
855
856
857 void __init trap_init(void)
858 {
859 #ifdef CONFIG_EISA
860         if (isa_readl(0x0FFFD9) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
861                 EISA_bus = 1;
862         }
863 #endif
864
865 #ifdef CONFIG_X86_LOCAL_APIC
866         init_apic_mappings();
867 #endif
868
869         set_trap_gate(0,&divide_error);
870         set_intr_gate(1,&debug);
871         set_intr_gate(2,&nmi);
872         set_system_gate(3,&int3);       /* int3-5 can be called from all */
873         set_system_gate(4,&overflow);
874         set_system_gate(5,&bounds);
875         set_trap_gate(6,&invalid_op);
876         set_trap_gate(7,&device_not_available);
877         set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
878         set_trap_gate(9,&coprocessor_segment_overrun);
879         set_trap_gate(10,&invalid_TSS);
880         set_trap_gate(11,&segment_not_present);
881         set_trap_gate(12,&stack_segment);
882         set_trap_gate(13,&general_protection);
883         set_intr_gate(14,&page_fault);
884         set_trap_gate(15,&spurious_interrupt_bug);
885         set_trap_gate(16,&coprocessor_error);
886         set_trap_gate(17,&alignment_check);
887 #ifdef CONFIG_X86_MCE
888         set_trap_gate(18,&machine_check);
889 #endif
890         set_trap_gate(19,&simd_coprocessor_error);
891
892         set_system_gate(SYSCALL_VECTOR,&system_call);
893
894         /*
895          * default LDT is a single-entry callgate to lcall7 for iBCS
896          * and a callgate to lcall27 for Solaris/x86 binaries
897          */
898         set_call_gate(&default_ldt[0],lcall7);
899         set_call_gate(&default_ldt[4],lcall27);
900
901         /*
902          * Should be a barrier for any external CPU state.
903          */
904         cpu_init();
905
906         trap_init_hook();
907 }