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