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