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