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