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