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