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