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