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