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