patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / x86_64 / kernel / traps.c
1 /*
2  *  linux/arch/x86-64/traps.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
6  *
7  *  Pentium III FXSR, SSE support
8  *      Gareth Hughes <gareth@valinux.com>, May 2000
9  *
10  *  $Id: traps.c,v 1.36 2002/03/24 11:09:10 ak Exp $
11  */
12
13 /*
14  * 'Traps.c' handles hardware traps and faults after we have saved some
15  * state in 'entry.S'.
16  */
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/ptrace.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/io.h>
35 #include <asm/atomic.h>
36 #include <asm/debugreg.h>
37 #include <asm/desc.h>
38 #include <asm/i387.h>
39 #include <asm/kdebug.h>
40 #include <asm/processor.h>
41
42 #include <asm/smp.h>
43 #include <asm/pgalloc.h>
44 #include <asm/pda.h>
45 #include <asm/proto.h>
46
47 #include <linux/irq.h>
48
49 extern struct gate_struct idt_table[256]; 
50
51 asmlinkage void divide_error(void);
52 asmlinkage void debug(void);
53 asmlinkage void nmi(void);
54 asmlinkage void int3(void);
55 asmlinkage void overflow(void);
56 asmlinkage void bounds(void);
57 asmlinkage void invalid_op(void);
58 asmlinkage void device_not_available(void);
59 asmlinkage void double_fault(void);
60 asmlinkage void coprocessor_segment_overrun(void);
61 asmlinkage void invalid_TSS(void);
62 asmlinkage void segment_not_present(void);
63 asmlinkage void stack_segment(void);
64 asmlinkage void general_protection(void);
65 asmlinkage void page_fault(void);
66 asmlinkage void coprocessor_error(void);
67 asmlinkage void simd_coprocessor_error(void);
68 asmlinkage void reserved(void);
69 asmlinkage void alignment_check(void);
70 asmlinkage void machine_check(void);
71 asmlinkage void spurious_interrupt_bug(void);
72 asmlinkage void call_debug(void);
73
74 struct notifier_block *die_chain;
75
76 static inline void conditional_sti(struct pt_regs *regs)
77 {
78         if (regs->eflags & X86_EFLAGS_IF)
79                 local_irq_enable();
80 }
81
82 static int kstack_depth_to_print = 10;
83
84 #ifdef CONFIG_KALLSYMS
85 #include <linux/kallsyms.h> 
86 int printk_address(unsigned long address)
87
88         unsigned long offset = 0, symsize;
89         const char *symname;
90         char *modname;
91         char *delim = ":"; 
92         char namebuf[128];
93
94         symname = kallsyms_lookup(address, &symsize, &offset, &modname, namebuf); 
95         if (!symname) 
96                 return printk("[<%016lx>]", address);
97         if (!modname) 
98                 modname = delim = "";           
99         return printk("<%016lx>{%s%s%s%s%+ld}",
100                       address,delim,modname,delim,symname,offset); 
101
102 #else
103 int printk_address(unsigned long address)
104
105         return printk("[<%016lx>]", address);
106
107 #endif
108
109 unsigned long *in_exception_stack(int cpu, unsigned long stack) 
110
111         int k;
112         for (k = 0; k < N_EXCEPTION_STACKS; k++) {
113                 unsigned long end = init_tss[cpu].ist[k] + EXCEPTION_STKSZ; 
114
115                 if (stack >= init_tss[cpu].ist[k]  && stack <= end) 
116                         return (unsigned long *)end;
117         }
118         return 0;
119
120
121 /*
122  * x86-64 can have upto three kernel stacks: 
123  * process stack
124  * interrupt stack
125  * severe exception (double fault, nmi, stack fault) hardware stack
126  * Check and process them in order.
127  */
128
129 void show_trace(unsigned long *stack)
130 {
131         unsigned long addr;
132         unsigned long *irqstack, *irqstack_end, *estack_end;
133         const int cpu = safe_smp_processor_id();
134         int i;
135
136         printk("\nCall Trace:");
137         i = 0; 
138         
139         estack_end = in_exception_stack(cpu, (unsigned long)stack); 
140         if (estack_end) { 
141                 while (stack < estack_end) { 
142                         addr = *stack++; 
143                         if (kernel_text_address(addr)) {  
144                                 i += printk_address(addr);
145                                 i += printk(" "); 
146                                 if (i > 50) {
147                                         printk("\n"); 
148                                         i = 0;
149                                 }
150                         }
151                 }
152                 i += printk(" <EOE> "); 
153                 i += 7;
154                 stack = (unsigned long *) estack_end[-2]; 
155         }  
156
157         irqstack_end = (unsigned long *) (cpu_pda[cpu].irqstackptr);
158         irqstack = (unsigned long *) (cpu_pda[cpu].irqstackptr - IRQSTACKSIZE + 64);
159
160         if (stack >= irqstack && stack < irqstack_end) {
161                 printk("<IRQ> ");  
162                 while (stack < irqstack_end) {
163                         addr = *stack++;
164                         /*
165                          * If the address is either in the text segment of the
166                          * kernel, or in the region which contains vmalloc'ed
167                          * memory, it *may* be the address of a calling
168                          * routine; if so, print it so that someone tracing
169                          * down the cause of the crash will be able to figure
170                          * out the call path that was taken.
171                          */
172                          if (kernel_text_address(addr)) {  
173                                  i += printk_address(addr);
174                                  i += printk(" "); 
175                                  if (i > 50) { 
176                                         printk("\n       ");
177                                          i = 0;
178                                  } 
179                         }
180                 } 
181                 stack = (unsigned long *) (irqstack_end[-1]);
182                 printk(" <EOI> ");
183                 i += 7;
184         } 
185
186         while (((long) stack & (THREAD_SIZE-1)) != 0) {
187                 addr = *stack++;
188                 if (kernel_text_address(addr)) {         
189                         i += printk_address(addr);
190                         i += printk(" "); 
191                         if (i > 50) { 
192                                 printk("\n       ");
193                                          i = 0;
194                         } 
195                 }
196         }
197         printk("\n");
198 }
199
200 void show_stack(struct task_struct *tsk, unsigned long * rsp)
201 {
202         unsigned long *stack;
203         int i;
204         const int cpu = safe_smp_processor_id();
205         unsigned long *irqstack_end = (unsigned long *) (cpu_pda[cpu].irqstackptr);
206         unsigned long *irqstack = (unsigned long *) (cpu_pda[cpu].irqstackptr - IRQSTACKSIZE);    
207
208         // debugging aid: "show_stack(NULL, NULL);" prints the
209         // back trace for this cpu.
210
211         if (rsp == NULL) {
212                 if (tsk)
213                         rsp = (unsigned long *)tsk->thread.rsp;
214                 else
215                         rsp = (unsigned long *)&rsp;
216         }
217
218         stack = rsp;
219         for(i=0; i < kstack_depth_to_print; i++) {
220                 if (stack >= irqstack && stack <= irqstack_end) {
221                         if (stack == irqstack_end) {
222                                 stack = (unsigned long *) (irqstack_end[-1]);
223                                 printk(" <EOI> ");
224                         }
225                 } else {
226                 if (((long) stack & (THREAD_SIZE-1)) == 0)
227                         break;
228                 }
229                 if (i && ((i % 4) == 0))
230                         printk("\n       ");
231                 printk("%016lx ", *stack++);
232         }
233         show_trace((unsigned long *)rsp);
234 }
235
236 /*
237  * The architecture-independent dump_stack generator
238  */
239 void dump_stack(void)
240 {
241         unsigned long dummy;
242         show_trace(&dummy);
243 }
244
245 EXPORT_SYMBOL(dump_stack);
246
247 void show_registers(struct pt_regs *regs)
248 {
249         int i;
250         int in_kernel = (regs->cs & 3) == 0;
251         unsigned long rsp;
252         const int cpu = safe_smp_processor_id(); 
253         struct task_struct *cur = cpu_pda[cpu].pcurrent; 
254
255                 rsp = regs->rsp;
256
257         printk("CPU %d ", cpu);
258         __show_regs(regs);
259         printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
260                 cur->comm, cur->pid, cur->thread_info, cur);
261
262         /*
263          * When in-kernel, we also print out the stack and code at the
264          * time of the fault..
265          */
266         if (in_kernel) {
267
268                 printk("Stack: ");
269                 show_stack(NULL, (unsigned long*)rsp);
270
271                 printk("\nCode: ");
272                 if(regs->rip < PAGE_OFFSET)
273                         goto bad;
274
275                 for(i=0;i<20;i++)
276                 {
277                         unsigned char c;
278                         if(__get_user(c, &((unsigned char*)regs->rip)[i])) {
279 bad:
280                                 printk(" Bad RIP value.");
281                                 break;
282                         }
283                         printk("%02x ", c);
284                 }
285         }
286         printk("\n");
287 }       
288
289 void handle_BUG(struct pt_regs *regs)
290
291         struct bug_frame f;
292         char tmp;
293
294         if (regs->cs & 3)
295                 return; 
296         if (__copy_from_user(&f, (struct bug_frame *) regs->rip, 
297                              sizeof(struct bug_frame)))
298                 return; 
299         if ((unsigned long)f.filename < __PAGE_OFFSET || 
300             f.ud2[0] != 0x0f || f.ud2[1] != 0x0b) 
301                 return;
302         if (__get_user(tmp, f.filename))
303                 f.filename = "unmapped filename"; 
304         printk("----------- [cut here ] --------- [please bite here ] ---------\n");
305         printk(KERN_ALERT "Kernel BUG at %.50s:%d\n", f.filename, f.line);
306
307
308 void out_of_line_bug(void)
309
310         BUG(); 
311
312
313 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
314 static int die_owner = -1;
315
316 void oops_begin(void)
317 {
318         int cpu = safe_smp_processor_id(); 
319         /* racy, but better than risking deadlock. */ 
320         local_irq_disable();
321         if (!spin_trylock(&die_lock)) { 
322                 if (cpu == die_owner) 
323                         /* nested oops. should stop eventually */;
324                 else
325                         spin_lock(&die_lock); 
326         }
327         die_owner = cpu; 
328         console_verbose();
329         bust_spinlocks(1); 
330 }
331
332 void oops_end(void)
333
334         die_owner = -1;
335         bust_spinlocks(0); 
336         spin_unlock(&die_lock); 
337         local_irq_enable();     /* make sure back scroll still works */
338         if (panic_on_oops)
339                 panic("Oops"); 
340
341
342 void __die(const char * str, struct pt_regs * regs, long err)
343 {
344         static int die_counter;
345         printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
346 #ifdef CONFIG_PREEMPT
347         printk("PREEMPT ");
348 #endif
349 #ifdef CONFIG_SMP
350         printk("SMP ");
351 #endif
352 #ifdef CONFIG_DEBUG_PAGEALLOC
353         printk("DEBUG_PAGEALLOC");
354 #endif
355                 printk("\n");
356         notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
357         show_registers(regs);
358         /* Executive summary in case the oops scrolled away */
359         printk(KERN_ALERT "RIP ");
360         printk_address(regs->rip); 
361         printk(" RSP <%016lx>\n", regs->rsp); 
362 }
363
364 void die(const char * str, struct pt_regs * regs, long err)
365 {
366         oops_begin();
367         handle_BUG(regs);
368         __die(str, regs, err);
369         oops_end();
370         do_exit(SIGSEGV); 
371 }
372 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
373 {
374         if (!(regs->eflags & VM_MASK) && (regs->cs == __KERNEL_CS))
375                 die(str, regs, err);
376 }
377
378 static inline unsigned long get_cr2(void)
379 {
380         unsigned long address;
381
382         /* get the address */
383         __asm__("movq %%cr2,%0":"=r" (address));
384         return address;
385 }
386
387 static void do_trap(int trapnr, int signr, char *str, 
388                            struct pt_regs * regs, long error_code, siginfo_t *info)
389 {
390         conditional_sti(regs);
391
392 #ifdef CONFIG_CHECKING
393        { 
394                unsigned long gs; 
395                struct x8664_pda *pda = cpu_pda + safe_smp_processor_id(); 
396                rdmsrl(MSR_GS_BASE, gs); 
397                if (gs != (unsigned long)pda) { 
398                        wrmsrl(MSR_GS_BASE, pda); 
399                        printk("%s: wrong gs %lx expected %p rip %lx\n", str, gs, pda,
400                               regs->rip);
401                }
402        }
403 #endif
404
405         if ((regs->cs & 3)  != 0) { 
406                 struct task_struct *tsk = current;
407
408                 if (exception_trace && unhandled_signal(tsk, signr))
409                         printk(KERN_INFO
410                                "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
411                                tsk->comm, tsk->pid, str,
412                                regs->rip,regs->rsp,error_code); 
413
414                 tsk->thread.error_code = error_code;
415                 tsk->thread.trap_no = trapnr;
416                 if (info)
417                         force_sig_info(signr, info, tsk);
418                 else
419                         force_sig(signr, tsk);
420                 return;
421         }
422
423
424         /* kernel trap */ 
425         {            
426                 const struct exception_table_entry *fixup;
427                 fixup = search_exception_tables(regs->rip);
428                 if (fixup) {
429                         regs->rip = fixup->fixup;
430                 } else  
431                         die(str, regs, error_code);
432                 return;
433         }
434 }
435
436 #define DO_ERROR(trapnr, signr, str, name) \
437 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
438 { \
439         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) == NOTIFY_BAD) \
440                 return; \
441         do_trap(trapnr, signr, str, regs, error_code, NULL); \
442 }
443
444 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
445 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
446 { \
447         siginfo_t info; \
448         info.si_signo = signr; \
449         info.si_errno = 0; \
450         info.si_code = sicode; \
451         info.si_addr = (void *)siaddr; \
452         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) == NOTIFY_BAD) \
453                 return; \
454         do_trap(trapnr, signr, str, regs, error_code, &info); \
455 }
456
457 DO_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->rip)
458 DO_ERROR( 3, SIGTRAP, "int3", int3);
459 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
460 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
461 DO_ERROR_INFO( 6, SIGILL,  "invalid operand", invalid_op, ILL_ILLOPN, regs->rip)
462 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
463 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)
464 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
465 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)
466 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2())
467 DO_ERROR(18, SIGSEGV, "reserved", reserved)
468
469 #define DO_ERROR_STACK(trapnr, signr, str, name) \
470 asmlinkage void *do_##name(struct pt_regs * regs, long error_code) \
471 { \
472         struct pt_regs *pr = ((struct pt_regs *)(current->thread.rsp0))-1; \
473         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) == NOTIFY_BAD) \
474                 return regs; \
475         if (regs->cs & 3) { \
476                 memcpy(pr, regs, sizeof(struct pt_regs)); \
477                 regs = pr; \
478         } \
479         do_trap(trapnr, signr, str, regs, error_code, NULL); \
480         return regs;            \
481 }
482
483 DO_ERROR_STACK(12, SIGBUS,  "stack segment", stack_segment)
484 DO_ERROR_STACK( 8, SIGSEGV, "double fault", double_fault)
485
486 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
487 {
488         conditional_sti(regs);
489
490 #ifdef CONFIG_CHECKING
491        { 
492                unsigned long gs; 
493                struct x8664_pda *pda = cpu_pda + safe_smp_processor_id(); 
494                rdmsrl(MSR_GS_BASE, gs); 
495                if (gs != (unsigned long)pda) { 
496                        wrmsrl(MSR_GS_BASE, pda); 
497                        oops_in_progress++;
498                        printk("general protection handler: wrong gs %lx expected %p\n", gs, pda);
499                        oops_in_progress--;
500                }
501        }
502 #endif
503
504         if ((regs->cs & 3)!=0) { 
505                 struct task_struct *tsk = current;
506
507                 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
508                         printk(KERN_INFO
509                        "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
510                                tsk->comm, tsk->pid,
511                                regs->rip,regs->rsp,error_code); 
512
513                 tsk->thread.error_code = error_code;
514                 tsk->thread.trap_no = 13;
515                 force_sig(SIGSEGV, tsk);
516         return;
517         } 
518
519         /* kernel gp */
520         {
521                 const struct exception_table_entry *fixup;
522                 fixup = search_exception_tables(regs->rip);
523                 if (fixup) {
524                         regs->rip = fixup->fixup;
525                         return;
526                 }
527                 notify_die(DIE_GPF, "general protection fault", regs, error_code,
528                            13, SIGSEGV); 
529                 die("general protection fault", regs, error_code);
530         }
531 }
532
533 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
534 {
535         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
536         printk("You probably have a hardware problem with your RAM chips\n");
537
538         /* Clear and disable the memory parity error line. */
539         reason = (reason & 0xf) | 4;
540         outb(reason, 0x61);
541 }
542
543 static void io_check_error(unsigned char reason, struct pt_regs * regs)
544 {
545         printk("NMI: IOCK error (debug interrupt?)\n");
546         show_registers(regs);
547
548         /* Re-enable the IOCK line, wait for a few seconds */
549         reason = (reason & 0xf) | 8;
550         outb(reason, 0x61);
551         mdelay(2000);
552         reason &= ~8;
553         outb(reason, 0x61);
554 }
555
556 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
557 {       printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
558         printk("Dazed and confused, but trying to continue\n");
559         printk("Do you have a strange power saving mode enabled?\n");
560 }
561
562 asmlinkage void default_do_nmi(struct pt_regs * regs)
563 {
564         unsigned char reason = inb(0x61);
565
566         if (!(reason & 0xc0)) {
567                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT) == NOTIFY_BAD)
568                         return;
569 #ifdef CONFIG_X86_LOCAL_APIC
570                 /*
571                  * Ok, so this is none of the documented NMI sources,
572                  * so it must be the NMI watchdog.
573                  */
574                 if (nmi_watchdog > 0) {
575                         nmi_watchdog_tick(regs,reason);
576                         return;
577                 }
578 #endif
579                 unknown_nmi_error(reason, regs);
580                 return;
581         }
582         if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_BAD)
583                 return; 
584         if (reason & 0x80)
585                 mem_parity_error(reason, regs);
586         if (reason & 0x40)
587                 io_check_error(reason, regs);
588
589         /*
590          * Reassert NMI in case it became active meanwhile
591          * as it's edge-triggered.
592          */
593         outb(0x8f, 0x70);
594         inb(0x71);              /* dummy */
595         outb(0x0f, 0x70);
596         inb(0x71);              /* dummy */
597 }
598
599 /* runs on IST stack. */
600 asmlinkage void *do_debug(struct pt_regs * regs, unsigned long error_code)
601 {
602         struct pt_regs *pr;
603         unsigned long condition;
604         struct task_struct *tsk = current;
605         siginfo_t info;
606
607         pr = (struct pt_regs *)(current->thread.rsp0)-1;
608         if (regs->cs & 3) {
609                 memcpy(pr, regs, sizeof(struct pt_regs));
610                 regs = pr;
611         }       
612
613 #ifdef CONFIG_CHECKING
614        { 
615                /* RED-PEN interaction with debugger - could destroy gs */
616                unsigned long gs; 
617                struct x8664_pda *pda = cpu_pda + safe_smp_processor_id(); 
618                rdmsrl(MSR_GS_BASE, gs); 
619                if (gs != (unsigned long)pda) { 
620                        wrmsrl(MSR_GS_BASE, pda); 
621                        printk("debug handler: wrong gs %lx expected %p\n", gs, pda);
622                }
623        }
624 #endif
625
626         asm("movq %%db6,%0" : "=r" (condition));
627
628         conditional_sti(regs);
629
630         /* Mask out spurious debug traps due to lazy DR7 setting */
631         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
632                 if (!tsk->thread.debugreg7) { 
633                         goto clear_dr7;
634                 }
635         }
636
637         tsk->thread.debugreg6 = condition;
638
639         /* Mask out spurious TF errors due to lazy TF clearing */
640         if (condition & DR_STEP) {
641                 /*
642                  * The TF error should be masked out only if the current
643                  * process is not traced and if the TRAP flag has been set
644                  * previously by a tracing process (condition detected by
645                  * the PT_DTRACE flag); remember that the i386 TRAP flag
646                  * can be modified by the process itself in user mode,
647                  * allowing programs to debug themselves without the ptrace()
648                  * interface.
649                  */
650                 if ((regs->cs & 3) == 0)
651                        goto clear_TF_reenable;
652                 if ((tsk->ptrace & (PT_DTRACE|PT_PTRACED)) == PT_DTRACE)
653                         goto clear_TF;
654         }
655
656         /* Ok, finally something we can handle */
657         tsk->thread.trap_no = 1;
658         tsk->thread.error_code = error_code;
659         info.si_signo = SIGTRAP;
660         info.si_errno = 0;
661         info.si_code = TRAP_BRKPT;
662         if ((regs->cs & 3) == 0) 
663                 goto clear_dr7; 
664
665         info.si_addr = (void *)regs->rip;
666         force_sig_info(SIGTRAP, &info, tsk);    
667 clear_dr7:
668         asm volatile("movq %0,%%db7"::"r"(0UL));
669         notify_die(DIE_DEBUG, "debug", regs, condition, 1, SIGTRAP);
670         return regs;
671
672 clear_TF_reenable:
673         printk("clear_tf_reenable\n");
674         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
675
676 clear_TF:
677         /* RED-PEN could cause spurious errors */
678         if (notify_die(DIE_DEBUG, "debug2", regs, condition, 1, SIGTRAP) 
679             != NOTIFY_BAD)
680         regs->eflags &= ~TF_MASK;
681         return regs;    
682 }
683
684 /*
685  * Note that we play around with the 'TS' bit in an attempt to get
686  * the correct behaviour even in the presence of the asynchronous
687  * IRQ13 behaviour
688  */
689 void math_error(void *rip)
690 {
691         struct task_struct * task;
692         siginfo_t info;
693         unsigned short cwd, swd;
694         /*
695          * Save the info for the exception handler and clear the error.
696          */
697         task = current;
698         save_init_fpu(task);
699         task->thread.trap_no = 16;
700         task->thread.error_code = 0;
701         info.si_signo = SIGFPE;
702         info.si_errno = 0;
703         info.si_code = __SI_FAULT;
704         info.si_addr = rip;
705         /*
706          * (~cwd & swd) will mask out exceptions that are not set to unmasked
707          * status.  0x3f is the exception bits in these regs, 0x200 is the
708          * C1 reg you need in case of a stack fault, 0x040 is the stack
709          * fault bit.  We should only be taking one exception at a time,
710          * so if this combination doesn't produce any single exception,
711          * then we have a bad program that isn't synchronizing its FPU usage
712          * and it will suffer the consequences since we won't be able to
713          * fully reproduce the context of the exception
714          */
715         cwd = get_fpu_cwd(task);
716         swd = get_fpu_swd(task);
717         switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
718                 case 0x000:
719                 default:
720                         break;
721                 case 0x001: /* Invalid Op */
722                 case 0x041: /* Stack Fault */
723                 case 0x241: /* Stack Fault | Direction */
724                         info.si_code = FPE_FLTINV;
725                         break;
726                 case 0x002: /* Denormalize */
727                 case 0x010: /* Underflow */
728                         info.si_code = FPE_FLTUND;
729                         break;
730                 case 0x004: /* Zero Divide */
731                         info.si_code = FPE_FLTDIV;
732                         break;
733                 case 0x008: /* Overflow */
734                         info.si_code = FPE_FLTOVF;
735                         break;
736                 case 0x020: /* Precision */
737                         info.si_code = FPE_FLTRES;
738                         break;
739         }
740         force_sig_info(SIGFPE, &info, task);
741 }
742
743 asmlinkage void do_coprocessor_error(struct pt_regs * regs)
744 {
745         conditional_sti(regs);
746         math_error((void *)regs->rip);
747 }
748
749 asmlinkage void bad_intr(void)
750 {
751         printk("bad interrupt"); 
752 }
753
754 static inline void simd_math_error(void *rip)
755 {
756         struct task_struct * task;
757         siginfo_t info;
758         unsigned short mxcsr;
759
760         /*
761          * Save the info for the exception handler and clear the error.
762          */
763         task = current;
764         save_init_fpu(task);
765         task->thread.trap_no = 19;
766         task->thread.error_code = 0;
767         info.si_signo = SIGFPE;
768         info.si_errno = 0;
769         info.si_code = __SI_FAULT;
770         info.si_addr = rip;
771         /*
772          * The SIMD FPU exceptions are handled a little differently, as there
773          * is only a single status/control register.  Thus, to determine which
774          * unmasked exception was caught we must mask the exception mask bits
775          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
776          */
777         mxcsr = get_fpu_mxcsr(task);
778         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
779                 case 0x000:
780                 default:
781                         break;
782                 case 0x001: /* Invalid Op */
783                         info.si_code = FPE_FLTINV;
784                         break;
785                 case 0x002: /* Denormalize */
786                 case 0x010: /* Underflow */
787                         info.si_code = FPE_FLTUND;
788                         break;
789                 case 0x004: /* Zero Divide */
790                         info.si_code = FPE_FLTDIV;
791                         break;
792                 case 0x008: /* Overflow */
793                         info.si_code = FPE_FLTOVF;
794                         break;
795                 case 0x020: /* Precision */
796                         info.si_code = FPE_FLTRES;
797                         break;
798         }
799         force_sig_info(SIGFPE, &info, task);
800 }
801
802 asmlinkage void do_simd_coprocessor_error(struct pt_regs * regs)
803 {
804         conditional_sti(regs);
805                 simd_math_error((void *)regs->rip);
806 }
807
808 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
809 {
810 }
811
812 /*
813  *  'math_state_restore()' saves the current math information in the
814  * old math state array, and gets the new ones from the current task
815  *
816  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
817  * Don't touch unless you *really* know how it works.
818  */
819 asmlinkage void math_state_restore(void)
820 {
821         struct task_struct *me = current;
822         clts();                 /* Allow maths ops (or we recurse) */
823
824         if (!me->used_math)
825                 init_fpu(me);
826         restore_fpu_checking(&me->thread.i387.fxsave);
827         me->thread_info->status |= TS_USEDFPU;
828 }
829
830 void do_call_debug(struct pt_regs *regs) 
831
832         notify_die(DIE_CALL, "debug call", regs, 0, 255, SIGINT); 
833 }
834
835 void __init trap_init(void)
836 {
837         set_intr_gate(0,&divide_error);
838         set_intr_gate_ist(1,&debug,DEBUG_STACK);
839         set_intr_gate_ist(2,&nmi,NMI_STACK);
840         set_system_gate(3,&int3);       /* int3-5 can be called from all */
841         set_system_gate(4,&overflow);
842         set_system_gate(5,&bounds);
843         set_intr_gate(6,&invalid_op);
844         set_intr_gate(7,&device_not_available);
845         set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
846         set_intr_gate(9,&coprocessor_segment_overrun);
847         set_intr_gate(10,&invalid_TSS);
848         set_intr_gate(11,&segment_not_present);
849         set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
850         set_intr_gate(13,&general_protection);
851         set_intr_gate(14,&page_fault);
852         set_intr_gate(15,&spurious_interrupt_bug);
853         set_intr_gate(16,&coprocessor_error);
854         set_intr_gate(17,&alignment_check);
855         set_intr_gate_ist(18,&machine_check, MCE_STACK); 
856         set_intr_gate(19,&simd_coprocessor_error);
857
858 #ifdef CONFIG_IA32_EMULATION
859         set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
860 #endif
861        
862         set_intr_gate(KDB_VECTOR, call_debug);
863        
864         /*
865          * Should be a barrier for any external CPU state.
866          */
867         cpu_init();
868 }
869
870
871 /* Actual parsing is done early in setup.c. */
872 static int __init oops_dummy(char *s)
873
874         panic_on_oops = 1;
875         return -1; 
876
877 __setup("oops=", oops_dummy);