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