]> err.no Git - linux-2.6/blob - arch/x86/kernel/traps_64.c
x86: wipe get_nmi_reason out of nmi_64.h
[linux-2.6] / arch / x86 / kernel / traps_64.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  *
5  *  Pentium III FXSR, SSE support
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8
9 /*
10  * 'Traps.c' handles hardware traps and faults after we have saved some
11  * state in 'entry.S'.
12  */
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/timer.h>
19 #include <linux/mm.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/kallsyms.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/nmi.h>
28 #include <linux/kprobes.h>
29 #include <linux/kexec.h>
30 #include <linux/unwind.h>
31 #include <linux/uaccess.h>
32 #include <linux/bug.h>
33 #include <linux/kdebug.h>
34 #include <linux/utsname.h>
35
36 #include <mach_traps.h>
37
38 #if defined(CONFIG_EDAC)
39 #include <linux/edac.h>
40 #endif
41
42 #include <asm/system.h>
43 #include <asm/io.h>
44 #include <asm/atomic.h>
45 #include <asm/debugreg.h>
46 #include <asm/desc.h>
47 #include <asm/i387.h>
48 #include <asm/processor.h>
49 #include <asm/unwind.h>
50 #include <asm/smp.h>
51 #include <asm/pgalloc.h>
52 #include <asm/pda.h>
53 #include <asm/proto.h>
54 #include <asm/nmi.h>
55 #include <asm/stacktrace.h>
56
57 asmlinkage void divide_error(void);
58 asmlinkage void debug(void);
59 asmlinkage void nmi(void);
60 asmlinkage void int3(void);
61 asmlinkage void overflow(void);
62 asmlinkage void bounds(void);
63 asmlinkage void invalid_op(void);
64 asmlinkage void device_not_available(void);
65 asmlinkage void double_fault(void);
66 asmlinkage void coprocessor_segment_overrun(void);
67 asmlinkage void invalid_TSS(void);
68 asmlinkage void segment_not_present(void);
69 asmlinkage void stack_segment(void);
70 asmlinkage void general_protection(void);
71 asmlinkage void page_fault(void);
72 asmlinkage void coprocessor_error(void);
73 asmlinkage void simd_coprocessor_error(void);
74 asmlinkage void reserved(void);
75 asmlinkage void alignment_check(void);
76 asmlinkage void machine_check(void);
77 asmlinkage void spurious_interrupt_bug(void);
78
79 static unsigned int code_bytes = 64;
80
81 static inline void conditional_sti(struct pt_regs *regs)
82 {
83         if (regs->flags & X86_EFLAGS_IF)
84                 local_irq_enable();
85 }
86
87 static inline void preempt_conditional_sti(struct pt_regs *regs)
88 {
89         inc_preempt_count();
90         if (regs->flags & X86_EFLAGS_IF)
91                 local_irq_enable();
92 }
93
94 static inline void preempt_conditional_cli(struct pt_regs *regs)
95 {
96         if (regs->flags & X86_EFLAGS_IF)
97                 local_irq_disable();
98         /* Make sure to not schedule here because we could be running
99            on an exception stack. */
100         dec_preempt_count();
101 }
102
103 int kstack_depth_to_print = 12;
104
105 void printk_address(unsigned long address, int reliable)
106 {
107 #ifdef CONFIG_KALLSYMS
108         unsigned long offset = 0, symsize;
109         const char *symname;
110         char *modname;
111         char *delim = ":";
112         char namebuf[KSYM_NAME_LEN];
113         char reliab[4] = "";
114
115         symname = kallsyms_lookup(address, &symsize, &offset,
116                                         &modname, namebuf);
117         if (!symname) {
118                 printk(" [<%016lx>]\n", address);
119                 return;
120         }
121         if (!reliable)
122                 strcpy(reliab, "? ");
123
124         if (!modname)
125                 modname = delim = "";
126         printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
127                 address, reliab, delim, modname, delim, symname, offset, symsize);
128 #else
129         printk(" [<%016lx>]\n", address);
130 #endif
131 }
132
133 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
134                                         unsigned *usedp, char **idp)
135 {
136         static char ids[][8] = {
137                 [DEBUG_STACK - 1] = "#DB",
138                 [NMI_STACK - 1] = "NMI",
139                 [DOUBLEFAULT_STACK - 1] = "#DF",
140                 [STACKFAULT_STACK - 1] = "#SS",
141                 [MCE_STACK - 1] = "#MC",
142 #if DEBUG_STKSZ > EXCEPTION_STKSZ
143                 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
144 #endif
145         };
146         unsigned k;
147
148         /*
149          * Iterate over all exception stacks, and figure out whether
150          * 'stack' is in one of them:
151          */
152         for (k = 0; k < N_EXCEPTION_STACKS; k++) {
153                 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
154                 /*
155                  * Is 'stack' above this exception frame's end?
156                  * If yes then skip to the next frame.
157                  */
158                 if (stack >= end)
159                         continue;
160                 /*
161                  * Is 'stack' above this exception frame's start address?
162                  * If yes then we found the right frame.
163                  */
164                 if (stack >= end - EXCEPTION_STKSZ) {
165                         /*
166                          * Make sure we only iterate through an exception
167                          * stack once. If it comes up for the second time
168                          * then there's something wrong going on - just
169                          * break out and return NULL:
170                          */
171                         if (*usedp & (1U << k))
172                                 break;
173                         *usedp |= 1U << k;
174                         *idp = ids[k];
175                         return (unsigned long *)end;
176                 }
177                 /*
178                  * If this is a debug stack, and if it has a larger size than
179                  * the usual exception stacks, then 'stack' might still
180                  * be within the lower portion of the debug stack:
181                  */
182 #if DEBUG_STKSZ > EXCEPTION_STKSZ
183                 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
184                         unsigned j = N_EXCEPTION_STACKS - 1;
185
186                         /*
187                          * Black magic. A large debug stack is composed of
188                          * multiple exception stack entries, which we
189                          * iterate through now. Dont look:
190                          */
191                         do {
192                                 ++j;
193                                 end -= EXCEPTION_STKSZ;
194                                 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
195                         } while (stack < end - EXCEPTION_STKSZ);
196                         if (*usedp & (1U << j))
197                                 break;
198                         *usedp |= 1U << j;
199                         *idp = ids[j];
200                         return (unsigned long *)end;
201                 }
202 #endif
203         }
204         return NULL;
205 }
206
207 #define MSG(txt) ops->warning(data, txt)
208
209 /*
210  * x86-64 can have up to three kernel stacks: 
211  * process stack
212  * interrupt stack
213  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
214  */
215
216 static inline int valid_stack_ptr(struct thread_info *tinfo,
217                         void *p, unsigned int size, void *end)
218 {
219         void *t = tinfo;
220         if (end) {
221                 if (p < end && p >= (end-THREAD_SIZE))
222                         return 1;
223                 else
224                         return 0;
225         }
226         return p > t && p < t + THREAD_SIZE - size;
227 }
228
229 /* The form of the top of the frame on the stack */
230 struct stack_frame {
231         struct stack_frame *next_frame;
232         unsigned long return_address;
233 };
234
235
236 static inline unsigned long print_context_stack(struct thread_info *tinfo,
237                                 unsigned long *stack, unsigned long bp,
238                                 const struct stacktrace_ops *ops, void *data,
239                                 unsigned long *end)
240 {
241         struct stack_frame *frame = (struct stack_frame *)bp;
242
243         while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
244                 unsigned long addr;
245
246                 addr = *stack;
247                 if (__kernel_text_address(addr)) {
248                         if ((unsigned long) stack == bp + 8) {
249                                 ops->address(data, addr, 1);
250                                 frame = frame->next_frame;
251                                 bp = (unsigned long) frame;
252                         } else {
253                                 ops->address(data, addr, bp == 0);
254                         }
255                 }
256                 stack++;
257         }
258         return bp;
259 }
260
261 void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
262                 unsigned long *stack, unsigned long bp,
263                 const struct stacktrace_ops *ops, void *data)
264 {
265         const unsigned cpu = get_cpu();
266         unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
267         unsigned used = 0;
268         struct thread_info *tinfo;
269
270         if (!tsk)
271                 tsk = current;
272         tinfo = task_thread_info(tsk);
273
274         if (!stack) {
275                 unsigned long dummy;
276                 stack = &dummy;
277                 if (tsk && tsk != current)
278                         stack = (unsigned long *)tsk->thread.sp;
279         }
280
281 #ifdef CONFIG_FRAME_POINTER
282         if (!bp) {
283                 if (tsk == current) {
284                         /* Grab bp right from our regs */
285                         asm("movq %%rbp, %0" : "=r" (bp):);
286                 } else {
287                         /* bp is the last reg pushed by switch_to */
288                         bp = *(unsigned long *) tsk->thread.sp;
289                 }
290         }
291 #endif
292
293
294
295         /*
296          * Print function call entries in all stacks, starting at the
297          * current stack address. If the stacks consist of nested
298          * exceptions
299          */
300         for (;;) {
301                 char *id;
302                 unsigned long *estack_end;
303                 estack_end = in_exception_stack(cpu, (unsigned long)stack,
304                                                 &used, &id);
305
306                 if (estack_end) {
307                         if (ops->stack(data, id) < 0)
308                                 break;
309
310                         bp = print_context_stack(tinfo, stack, bp, ops,
311                                                         data, estack_end);
312                         ops->stack(data, "<EOE>");
313                         /*
314                          * We link to the next stack via the
315                          * second-to-last pointer (index -2 to end) in the
316                          * exception stack:
317                          */
318                         stack = (unsigned long *) estack_end[-2];
319                         continue;
320                 }
321                 if (irqstack_end) {
322                         unsigned long *irqstack;
323                         irqstack = irqstack_end -
324                                 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
325
326                         if (stack >= irqstack && stack < irqstack_end) {
327                                 if (ops->stack(data, "IRQ") < 0)
328                                         break;
329                                 bp = print_context_stack(tinfo, stack, bp,
330                                                 ops, data, irqstack_end);
331                                 /*
332                                  * We link to the next stack (which would be
333                                  * the process stack normally) the last
334                                  * pointer (index -1 to end) in the IRQ stack:
335                                  */
336                                 stack = (unsigned long *) (irqstack_end[-1]);
337                                 irqstack_end = NULL;
338                                 ops->stack(data, "EOI");
339                                 continue;
340                         }
341                 }
342                 break;
343         }
344
345         /*
346          * This handles the process stack:
347          */
348         bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
349         put_cpu();
350 }
351 EXPORT_SYMBOL(dump_trace);
352
353 static void
354 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
355 {
356         print_symbol(msg, symbol);
357         printk("\n");
358 }
359
360 static void print_trace_warning(void *data, char *msg)
361 {
362         printk("%s\n", msg);
363 }
364
365 static int print_trace_stack(void *data, char *name)
366 {
367         printk(" <%s> ", name);
368         return 0;
369 }
370
371 static void print_trace_address(void *data, unsigned long addr, int reliable)
372 {
373         touch_nmi_watchdog();
374         printk_address(addr, reliable);
375 }
376
377 static const struct stacktrace_ops print_trace_ops = {
378         .warning = print_trace_warning,
379         .warning_symbol = print_trace_warning_symbol,
380         .stack = print_trace_stack,
381         .address = print_trace_address,
382 };
383
384 void
385 show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
386                 unsigned long bp)
387 {
388         printk("\nCall Trace:\n");
389         dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
390         printk("\n");
391 }
392
393 static void
394 _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
395                                                         unsigned long bp)
396 {
397         unsigned long *stack;
398         int i;
399         const int cpu = smp_processor_id();
400         unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
401         unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
402
403         // debugging aid: "show_stack(NULL, NULL);" prints the
404         // back trace for this cpu.
405
406         if (sp == NULL) {
407                 if (tsk)
408                         sp = (unsigned long *)tsk->thread.sp;
409                 else
410                         sp = (unsigned long *)&sp;
411         }
412
413         stack = sp;
414         for(i=0; i < kstack_depth_to_print; i++) {
415                 if (stack >= irqstack && stack <= irqstack_end) {
416                         if (stack == irqstack_end) {
417                                 stack = (unsigned long *) (irqstack_end[-1]);
418                                 printk(" <EOI> ");
419                         }
420                 } else {
421                 if (((long) stack & (THREAD_SIZE-1)) == 0)
422                         break;
423                 }
424                 if (i && ((i % 4) == 0))
425                         printk("\n");
426                 printk(" %016lx", *stack++);
427                 touch_nmi_watchdog();
428         }
429         show_trace(tsk, regs, sp, bp);
430 }
431
432 void show_stack(struct task_struct *tsk, unsigned long * sp)
433 {
434         _show_stack(tsk, NULL, sp, 0);
435 }
436
437 /*
438  * The architecture-independent dump_stack generator
439  */
440 void dump_stack(void)
441 {
442         unsigned long dummy;
443         unsigned long bp = 0;
444
445 #ifdef CONFIG_FRAME_POINTER
446         if (!bp)
447                 asm("movq %%rbp, %0" : "=r" (bp):);
448 #endif
449
450         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
451                 current->pid, current->comm, print_tainted(),
452                 init_utsname()->release,
453                 (int)strcspn(init_utsname()->version, " "),
454                 init_utsname()->version);
455         show_trace(NULL, NULL, &dummy, bp);
456 }
457
458 EXPORT_SYMBOL(dump_stack);
459
460 void show_registers(struct pt_regs *regs)
461 {
462         int i;
463         unsigned long sp;
464         const int cpu = smp_processor_id();
465         struct task_struct *cur = cpu_pda(cpu)->pcurrent;
466         u8 *ip;
467         unsigned int code_prologue = code_bytes * 43 / 64;
468         unsigned int code_len = code_bytes;
469
470         sp = regs->sp;
471         ip = (u8 *) regs->ip - code_prologue;
472         printk("CPU %d ", cpu);
473         __show_regs(regs);
474         printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
475                 cur->comm, cur->pid, task_thread_info(cur), cur);
476
477         /*
478          * When in-kernel, we also print out the stack and code at the
479          * time of the fault..
480          */
481         if (!user_mode(regs)) {
482                 unsigned char c;
483                 printk("Stack: ");
484                 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
485                 printk("\n");
486
487                 printk(KERN_EMERG "Code: ");
488                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
489                         /* try starting at RIP */
490                         ip = (u8 *) regs->ip;
491                         code_len = code_len - code_prologue + 1;
492                 }
493                 for (i = 0; i < code_len; i++, ip++) {
494                         if (ip < (u8 *)PAGE_OFFSET ||
495                                         probe_kernel_address(ip, c)) {
496                                 printk(" Bad RIP value.");
497                                 break;
498                         }
499                         if (ip == (u8 *)regs->ip)
500                                 printk("<%02x> ", c);
501                         else
502                                 printk("%02x ", c);
503                 }
504         }
505         printk("\n");
506 }       
507
508 int is_valid_bugaddr(unsigned long ip)
509 {
510         unsigned short ud2;
511
512         if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
513                 return 0;
514
515         return ud2 == 0x0b0f;
516 }
517
518 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
519 static int die_owner = -1;
520 static unsigned int die_nest_count;
521
522 unsigned __kprobes long oops_begin(void)
523 {
524         int cpu;
525         unsigned long flags;
526
527         oops_enter();
528
529         /* racy, but better than risking deadlock. */
530         raw_local_irq_save(flags);
531         cpu = smp_processor_id();
532         if (!__raw_spin_trylock(&die_lock)) {
533                 if (cpu == die_owner) 
534                         /* nested oops. should stop eventually */;
535                 else
536                         __raw_spin_lock(&die_lock);
537         }
538         die_nest_count++;
539         die_owner = cpu;
540         console_verbose();
541         bust_spinlocks(1);
542         return flags;
543 }
544
545 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
546
547         die_owner = -1;
548         bust_spinlocks(0);
549         die_nest_count--;
550         if (!die_nest_count)
551                 /* Nest count reaches zero, release the lock. */
552                 __raw_spin_unlock(&die_lock);
553         raw_local_irq_restore(flags);
554         if (!regs) {
555                 oops_exit();
556                 return;
557         }
558         if (panic_on_oops)
559                 panic("Fatal exception");
560         oops_exit();
561         do_exit(signr);
562 }
563
564 int __kprobes __die(const char * str, struct pt_regs * regs, long err)
565 {
566         static int die_counter;
567         printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
568 #ifdef CONFIG_PREEMPT
569         printk("PREEMPT ");
570 #endif
571 #ifdef CONFIG_SMP
572         printk("SMP ");
573 #endif
574 #ifdef CONFIG_DEBUG_PAGEALLOC
575         printk("DEBUG_PAGEALLOC");
576 #endif
577         printk("\n");
578         if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
579                 return 1;
580         show_registers(regs);
581         add_taint(TAINT_DIE);
582         /* Executive summary in case the oops scrolled away */
583         printk(KERN_ALERT "RIP ");
584         printk_address(regs->ip, 1);
585         printk(" RSP <%016lx>\n", regs->sp);
586         if (kexec_should_crash(current))
587                 crash_kexec(regs);
588         return 0;
589 }
590
591 void die(const char * str, struct pt_regs * regs, long err)
592 {
593         unsigned long flags = oops_begin();
594
595         if (!user_mode(regs))
596                 report_bug(regs->ip, regs);
597
598         if (__die(str, regs, err))
599                 regs = NULL;
600         oops_end(flags, regs, SIGSEGV);
601 }
602
603 void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
604 {
605         unsigned long flags = oops_begin();
606
607         /*
608          * We are in trouble anyway, lets at least try
609          * to get a message out.
610          */
611         printk(str, smp_processor_id());
612         show_registers(regs);
613         if (kexec_should_crash(current))
614                 crash_kexec(regs);
615         if (do_panic || panic_on_oops)
616                 panic("Non maskable interrupt");
617         oops_end(flags, NULL, SIGBUS);
618         nmi_exit();
619         local_irq_enable();
620         do_exit(SIGBUS);
621 }
622
623 static void __kprobes do_trap(int trapnr, int signr, char *str,
624                               struct pt_regs * regs, long error_code,
625                               siginfo_t *info)
626 {
627         struct task_struct *tsk = current;
628
629         if (user_mode(regs)) {
630                 /*
631                  * We want error_code and trap_no set for userspace
632                  * faults and kernelspace faults which result in
633                  * die(), but not kernelspace faults which are fixed
634                  * up.  die() gives the process no chance to handle
635                  * the signal and notice the kernel fault information,
636                  * so that won't result in polluting the information
637                  * about previously queued, but not yet delivered,
638                  * faults.  See also do_general_protection below.
639                  */
640                 tsk->thread.error_code = error_code;
641                 tsk->thread.trap_no = trapnr;
642
643                 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
644                     printk_ratelimit()) {
645                         printk(KERN_INFO
646                                "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
647                                tsk->comm, tsk->pid, str,
648                                regs->ip, regs->sp, error_code);
649                         print_vma_addr(" in ", regs->ip);
650                         printk("\n");
651                 }
652
653                 if (info)
654                         force_sig_info(signr, info, tsk);
655                 else
656                         force_sig(signr, tsk);
657                 return;
658         }
659
660
661         if (!fixup_exception(regs)) {
662                 tsk->thread.error_code = error_code;
663                 tsk->thread.trap_no = trapnr;
664                 die(str, regs, error_code);
665         }
666         return;
667 }
668
669 #define DO_ERROR(trapnr, signr, str, name) \
670 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
671 { \
672         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
673                                                         == NOTIFY_STOP) \
674                 return; \
675         conditional_sti(regs);                                          \
676         do_trap(trapnr, signr, str, regs, error_code, NULL); \
677 }
678
679 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
680 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
681 { \
682         siginfo_t info; \
683         info.si_signo = signr; \
684         info.si_errno = 0; \
685         info.si_code = sicode; \
686         info.si_addr = (void __user *)siaddr; \
687         trace_hardirqs_fixup(); \
688         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
689                                                         == NOTIFY_STOP) \
690                 return; \
691         conditional_sti(regs);                                          \
692         do_trap(trapnr, signr, str, regs, error_code, &info); \
693 }
694
695 DO_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->ip)
696 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
697 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
698 DO_ERROR_INFO( 6, SIGILL,  "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
699 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
700 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)
701 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
702 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)
703 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
704 DO_ERROR(18, SIGSEGV, "reserved", reserved)
705
706 /* Runs on IST stack */
707 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
708 {
709         if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
710                         12, SIGBUS) == NOTIFY_STOP)
711                 return;
712         preempt_conditional_sti(regs);
713         do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
714         preempt_conditional_cli(regs);
715 }
716
717 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
718 {
719         static const char str[] = "double fault";
720         struct task_struct *tsk = current;
721
722         /* Return not checked because double check cannot be ignored */
723         notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
724
725         tsk->thread.error_code = error_code;
726         tsk->thread.trap_no = 8;
727
728         /* This is always a kernel trap and never fixable (and thus must
729            never return). */
730         for (;;)
731                 die(str, regs, error_code);
732 }
733
734 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
735                                                 long error_code)
736 {
737         struct task_struct *tsk = current;
738
739         conditional_sti(regs);
740
741         if (user_mode(regs)) {
742                 tsk->thread.error_code = error_code;
743                 tsk->thread.trap_no = 13;
744
745                 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
746                     printk_ratelimit()) {
747                         printk(KERN_INFO
748                        "%s[%d] general protection ip:%lx sp:%lx error:%lx",
749                                tsk->comm, tsk->pid,
750                                regs->ip, regs->sp, error_code);
751                         print_vma_addr(" in ", regs->ip);
752                         printk("\n");
753                 }
754
755                 force_sig(SIGSEGV, tsk);
756                 return;
757         } 
758
759         if (fixup_exception(regs))
760                 return;
761
762         tsk->thread.error_code = error_code;
763         tsk->thread.trap_no = 13;
764         if (notify_die(DIE_GPF, "general protection fault", regs,
765                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
766                 return;
767         die("general protection fault", regs, error_code);
768 }
769
770 static __kprobes void
771 mem_parity_error(unsigned char reason, struct pt_regs * regs)
772 {
773         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
774                 reason);
775         printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
776
777 #if defined(CONFIG_EDAC)
778         if(edac_handler_set()) {
779                 edac_atomic_assert_error();
780                 return;
781         }
782 #endif
783
784         if (panic_on_unrecovered_nmi)
785                 panic("NMI: Not continuing");
786
787         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
788
789         /* Clear and disable the memory parity error line. */
790         reason = (reason & 0xf) | 4;
791         outb(reason, 0x61);
792 }
793
794 static __kprobes void
795 io_check_error(unsigned char reason, struct pt_regs * regs)
796 {
797         printk("NMI: IOCK error (debug interrupt?)\n");
798         show_registers(regs);
799
800         /* Re-enable the IOCK line, wait for a few seconds */
801         reason = (reason & 0xf) | 8;
802         outb(reason, 0x61);
803         mdelay(2000);
804         reason &= ~8;
805         outb(reason, 0x61);
806 }
807
808 static __kprobes void
809 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
810 {
811         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
812                 reason);
813         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
814
815         if (panic_on_unrecovered_nmi)
816                 panic("NMI: Not continuing");
817
818         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
819 }
820
821 /* Runs on IST stack. This code must keep interrupts off all the time.
822    Nested NMIs are prevented by the CPU. */
823 asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
824 {
825         unsigned char reason = 0;
826         int cpu;
827
828         cpu = smp_processor_id();
829
830         /* Only the BSP gets external NMIs from the system.  */
831         if (!cpu)
832                 reason = get_nmi_reason();
833
834         if (!(reason & 0xc0)) {
835                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
836                                                                 == NOTIFY_STOP)
837                         return;
838                 /*
839                  * Ok, so this is none of the documented NMI sources,
840                  * so it must be the NMI watchdog.
841                  */
842                 if (nmi_watchdog_tick(regs,reason))
843                         return;
844                 if (!do_nmi_callback(regs,cpu))
845                         unknown_nmi_error(reason, regs);
846
847                 return;
848         }
849         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
850                 return; 
851
852         /* AK: following checks seem to be broken on modern chipsets. FIXME */
853
854         if (reason & 0x80)
855                 mem_parity_error(reason, regs);
856         if (reason & 0x40)
857                 io_check_error(reason, regs);
858 }
859
860 /* runs on IST stack. */
861 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
862 {
863         trace_hardirqs_fixup();
864
865         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
866                 return;
867         }
868         preempt_conditional_sti(regs);
869         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
870         preempt_conditional_cli(regs);
871 }
872
873 /* Help handler running on IST stack to switch back to user stack
874    for scheduling or signal handling. The actual stack switch is done in
875    entry.S */
876 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
877 {
878         struct pt_regs *regs = eregs;
879         /* Did already sync */
880         if (eregs == (struct pt_regs *)eregs->sp)
881                 ;
882         /* Exception from user space */
883         else if (user_mode(eregs))
884                 regs = task_pt_regs(current);
885         /* Exception from kernel and interrupts are enabled. Move to
886            kernel process stack. */
887         else if (eregs->flags & X86_EFLAGS_IF)
888                 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
889         if (eregs != regs)
890                 *regs = *eregs;
891         return regs;
892 }
893
894 /* runs on IST stack. */
895 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
896                                    unsigned long error_code)
897 {
898         unsigned long condition;
899         struct task_struct *tsk = current;
900         siginfo_t info;
901
902         trace_hardirqs_fixup();
903
904         get_debugreg(condition, 6);
905
906         /*
907          * The processor cleared BTF, so don't mark that we need it set.
908          */
909         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
910         tsk->thread.debugctlmsr = 0;
911
912         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
913                                                 SIGTRAP) == NOTIFY_STOP)
914                 return;
915
916         preempt_conditional_sti(regs);
917
918         /* Mask out spurious debug traps due to lazy DR7 setting */
919         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
920                 if (!tsk->thread.debugreg7) { 
921                         goto clear_dr7;
922                 }
923         }
924
925         tsk->thread.debugreg6 = condition;
926
927
928         /*
929          * Single-stepping through TF: make sure we ignore any events in
930          * kernel space (but re-enable TF when returning to user mode).
931          */
932         if (condition & DR_STEP) {
933                 if (!user_mode(regs))
934                        goto clear_TF_reenable;
935         }
936
937         /* Ok, finally something we can handle */
938         tsk->thread.trap_no = 1;
939         tsk->thread.error_code = error_code;
940         info.si_signo = SIGTRAP;
941         info.si_errno = 0;
942         info.si_code = TRAP_BRKPT;
943         info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
944         force_sig_info(SIGTRAP, &info, tsk);
945
946 clear_dr7:
947         set_debugreg(0UL, 7);
948         preempt_conditional_cli(regs);
949         return;
950
951 clear_TF_reenable:
952         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
953         regs->flags &= ~X86_EFLAGS_TF;
954         preempt_conditional_cli(regs);
955 }
956
957 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
958 {
959         if (fixup_exception(regs))
960                 return 1;
961
962         notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
963         /* Illegal floating point operation in the kernel */
964         current->thread.trap_no = trapnr;
965         die(str, regs, 0);
966         return 0;
967 }
968
969 /*
970  * Note that we play around with the 'TS' bit in an attempt to get
971  * the correct behaviour even in the presence of the asynchronous
972  * IRQ13 behaviour
973  */
974 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
975 {
976         void __user *ip = (void __user *)(regs->ip);
977         struct task_struct * task;
978         siginfo_t info;
979         unsigned short cwd, swd;
980
981         conditional_sti(regs);
982         if (!user_mode(regs) &&
983             kernel_math_error(regs, "kernel x87 math error", 16))
984                 return;
985
986         /*
987          * Save the info for the exception handler and clear the error.
988          */
989         task = current;
990         save_init_fpu(task);
991         task->thread.trap_no = 16;
992         task->thread.error_code = 0;
993         info.si_signo = SIGFPE;
994         info.si_errno = 0;
995         info.si_code = __SI_FAULT;
996         info.si_addr = ip;
997         /*
998          * (~cwd & swd) will mask out exceptions that are not set to unmasked
999          * status.  0x3f is the exception bits in these regs, 0x200 is the
1000          * C1 reg you need in case of a stack fault, 0x040 is the stack
1001          * fault bit.  We should only be taking one exception at a time,
1002          * so if this combination doesn't produce any single exception,
1003          * then we have a bad program that isn't synchronizing its FPU usage
1004          * and it will suffer the consequences since we won't be able to
1005          * fully reproduce the context of the exception
1006          */
1007         cwd = get_fpu_cwd(task);
1008         swd = get_fpu_swd(task);
1009         switch (swd & ~cwd & 0x3f) {
1010                 case 0x000:
1011                 default:
1012                         break;
1013                 case 0x001: /* Invalid Op */
1014                         /*
1015                          * swd & 0x240 == 0x040: Stack Underflow
1016                          * swd & 0x240 == 0x240: Stack Overflow
1017                          * User must clear the SF bit (0x40) if set
1018                          */
1019                         info.si_code = FPE_FLTINV;
1020                         break;
1021                 case 0x002: /* Denormalize */
1022                 case 0x010: /* Underflow */
1023                         info.si_code = FPE_FLTUND;
1024                         break;
1025                 case 0x004: /* Zero Divide */
1026                         info.si_code = FPE_FLTDIV;
1027                         break;
1028                 case 0x008: /* Overflow */
1029                         info.si_code = FPE_FLTOVF;
1030                         break;
1031                 case 0x020: /* Precision */
1032                         info.si_code = FPE_FLTRES;
1033                         break;
1034         }
1035         force_sig_info(SIGFPE, &info, task);
1036 }
1037
1038 asmlinkage void bad_intr(void)
1039 {
1040         printk("bad interrupt"); 
1041 }
1042
1043 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1044 {
1045         void __user *ip = (void __user *)(regs->ip);
1046         struct task_struct * task;
1047         siginfo_t info;
1048         unsigned short mxcsr;
1049
1050         conditional_sti(regs);
1051         if (!user_mode(regs) &&
1052                 kernel_math_error(regs, "kernel simd math error", 19))
1053                 return;
1054
1055         /*
1056          * Save the info for the exception handler and clear the error.
1057          */
1058         task = current;
1059         save_init_fpu(task);
1060         task->thread.trap_no = 19;
1061         task->thread.error_code = 0;
1062         info.si_signo = SIGFPE;
1063         info.si_errno = 0;
1064         info.si_code = __SI_FAULT;
1065         info.si_addr = ip;
1066         /*
1067          * The SIMD FPU exceptions are handled a little differently, as there
1068          * is only a single status/control register.  Thus, to determine which
1069          * unmasked exception was caught we must mask the exception mask bits
1070          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1071          */
1072         mxcsr = get_fpu_mxcsr(task);
1073         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1074                 case 0x000:
1075                 default:
1076                         break;
1077                 case 0x001: /* Invalid Op */
1078                         info.si_code = FPE_FLTINV;
1079                         break;
1080                 case 0x002: /* Denormalize */
1081                 case 0x010: /* Underflow */
1082                         info.si_code = FPE_FLTUND;
1083                         break;
1084                 case 0x004: /* Zero Divide */
1085                         info.si_code = FPE_FLTDIV;
1086                         break;
1087                 case 0x008: /* Overflow */
1088                         info.si_code = FPE_FLTOVF;
1089                         break;
1090                 case 0x020: /* Precision */
1091                         info.si_code = FPE_FLTRES;
1092                         break;
1093         }
1094         force_sig_info(SIGFPE, &info, task);
1095 }
1096
1097 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1098 {
1099 }
1100
1101 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1102 {
1103 }
1104
1105 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1106 {
1107 }
1108
1109 /*
1110  *  'math_state_restore()' saves the current math information in the
1111  * old math state array, and gets the new ones from the current task
1112  *
1113  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1114  * Don't touch unless you *really* know how it works.
1115  */
1116 asmlinkage void math_state_restore(void)
1117 {
1118         struct task_struct *me = current;
1119         clts();                 /* Allow maths ops (or we recurse) */
1120
1121         if (!used_math())
1122                 init_fpu(me);
1123         restore_fpu_checking(&me->thread.i387.fxsave);
1124         task_thread_info(me)->status |= TS_USEDFPU;
1125         me->fpu_counter++;
1126 }
1127 EXPORT_SYMBOL_GPL(math_state_restore);
1128
1129 void __init trap_init(void)
1130 {
1131         set_intr_gate(0,&divide_error);
1132         set_intr_gate_ist(1,&debug,DEBUG_STACK);
1133         set_intr_gate_ist(2,&nmi,NMI_STACK);
1134         set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
1135         set_system_gate(4,&overflow);   /* int4 can be called from all */
1136         set_intr_gate(5,&bounds);
1137         set_intr_gate(6,&invalid_op);
1138         set_intr_gate(7,&device_not_available);
1139         set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1140         set_intr_gate(9,&coprocessor_segment_overrun);
1141         set_intr_gate(10,&invalid_TSS);
1142         set_intr_gate(11,&segment_not_present);
1143         set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1144         set_intr_gate(13,&general_protection);
1145         set_intr_gate(14,&page_fault);
1146         set_intr_gate(15,&spurious_interrupt_bug);
1147         set_intr_gate(16,&coprocessor_error);
1148         set_intr_gate(17,&alignment_check);
1149 #ifdef CONFIG_X86_MCE
1150         set_intr_gate_ist(18,&machine_check, MCE_STACK); 
1151 #endif
1152         set_intr_gate(19,&simd_coprocessor_error);
1153
1154 #ifdef CONFIG_IA32_EMULATION
1155         set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1156 #endif
1157        
1158         /*
1159          * Should be a barrier for any external CPU state.
1160          */
1161         cpu_init();
1162 }
1163
1164
1165 static int __init oops_setup(char *s)
1166
1167         if (!s)
1168                 return -EINVAL;
1169         if (!strcmp(s, "panic"))
1170                 panic_on_oops = 1;
1171         return 0;
1172
1173 early_param("oops", oops_setup);
1174
1175 static int __init kstack_setup(char *s)
1176 {
1177         if (!s)
1178                 return -EINVAL;
1179         kstack_depth_to_print = simple_strtoul(s,NULL,0);
1180         return 0;
1181 }
1182 early_param("kstack", kstack_setup);
1183
1184
1185 static int __init code_bytes_setup(char *s)
1186 {
1187         code_bytes = simple_strtoul(s, NULL, 0);
1188         if (code_bytes > 8192)
1189                 code_bytes = 8192;
1190
1191         return 1;
1192 }
1193 __setup("code_bytes=", code_bytes_setup);