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