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