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