This commit was generated by cvs2svn to compensate for changes in r925,
[linux-2.6.git] / arch / xen / i386 / kernel / traps.c
1 /*
2  *  linux/arch/i386/traps.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Pentium III FXSR, SSE support
7  *      Gareth Hughes <gareth@valinux.com>, May 2000
8  */
9
10 /*
11  * 'Traps.c' handles hardware traps and faults after we have saved some
12  * state in 'asm.s'.
13  */
14 #include <linux/config.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/timer.h>
20 #include <linux/mm.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/highmem.h>
26 #include <linux/kallsyms.h>
27 #include <linux/ptrace.h>
28 #include <linux/utsname.h>
29 #include <linux/kprobes.h>
30
31 #ifdef CONFIG_EISA
32 #include <linux/ioport.h>
33 #include <linux/eisa.h>
34 #endif
35
36 #ifdef CONFIG_MCA
37 #include <linux/mca.h>
38 #endif
39
40 #include <asm/processor.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <asm/io.h>
44 #include <asm/atomic.h>
45 #include <asm/debugreg.h>
46 #include <asm/desc.h>
47 #include <asm/i387.h>
48 #include <asm/nmi.h>
49
50 #include <asm/smp.h>
51 #include <asm/arch_hooks.h>
52 #include <asm/kdebug.h>
53
54 #include <linux/irq.h>
55 #include <linux/module.h>
56
57 #include "mach_traps.h"
58
59 asmlinkage int system_call(void);
60
61 /* Do we ignore FPU interrupts ? */
62 char ignore_fpu_irq = 0;
63
64 /*
65  * The IDT has to be page-aligned to simplify the Pentium
66  * F0 0F bug workaround.. We have a special link segment
67  * for this.
68  */
69 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
70
71 asmlinkage void divide_error(void);
72 asmlinkage void debug(void);
73 asmlinkage void nmi(void);
74 asmlinkage void int3(void);
75 asmlinkage void overflow(void);
76 asmlinkage void bounds(void);
77 asmlinkage void invalid_op(void);
78 asmlinkage void device_not_available(void);
79 asmlinkage void coprocessor_segment_overrun(void);
80 asmlinkage void invalid_TSS(void);
81 asmlinkage void segment_not_present(void);
82 asmlinkage void stack_segment(void);
83 asmlinkage void general_protection(void);
84 asmlinkage void page_fault(void);
85 asmlinkage void coprocessor_error(void);
86 asmlinkage void simd_coprocessor_error(void);
87 asmlinkage void alignment_check(void);
88 asmlinkage void fixup_4gb_segment(void);
89 asmlinkage void machine_check(void);
90
91 static int kstack_depth_to_print = 24;
92 struct notifier_block *i386die_chain;
93 static DEFINE_SPINLOCK(die_notifier_lock);
94
95 int register_die_notifier(struct notifier_block *nb)
96 {
97         int err = 0;
98         unsigned long flags;
99         spin_lock_irqsave(&die_notifier_lock, flags);
100         err = notifier_chain_register(&i386die_chain, nb);
101         spin_unlock_irqrestore(&die_notifier_lock, flags);
102         return err;
103 }
104
105 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
106 {
107         return  p > (void *)tinfo &&
108                 p < (void *)tinfo + THREAD_SIZE - 3;
109 }
110
111 static inline unsigned long print_context_stack(struct thread_info *tinfo,
112                                 unsigned long *stack, unsigned long ebp)
113 {
114         unsigned long addr;
115
116 #ifdef  CONFIG_FRAME_POINTER
117         while (valid_stack_ptr(tinfo, (void *)ebp)) {
118                 addr = *(unsigned long *)(ebp + 4);
119                 printk(" [<%08lx>] ", addr);
120                 print_symbol("%s", addr);
121                 printk("\n");
122                 ebp = *(unsigned long *)ebp;
123         }
124 #else
125         while (valid_stack_ptr(tinfo, stack)) {
126                 addr = *stack++;
127                 if (__kernel_text_address(addr)) {
128                         printk(" [<%08lx>]", addr);
129                         print_symbol(" %s", addr);
130                         printk("\n");
131                 }
132         }
133 #endif
134         return ebp;
135 }
136
137 void show_trace(struct task_struct *task, unsigned long * stack)
138 {
139         unsigned long ebp;
140
141         if (!task)
142                 task = current;
143
144         if (task == current) {
145                 /* Grab ebp right from our regs */
146                 asm ("movl %%ebp, %0" : "=r" (ebp) : );
147         } else {
148                 /* ebp is the last reg pushed by switch_to */
149                 ebp = *(unsigned long *) task->thread.esp;
150         }
151
152         while (1) {
153                 struct thread_info *context;
154                 context = (struct thread_info *)
155                         ((unsigned long)stack & (~(THREAD_SIZE - 1)));
156                 ebp = print_context_stack(context, stack, ebp);
157                 stack = (unsigned long*)context->previous_esp;
158                 if (!stack)
159                         break;
160                 printk(" =======================\n");
161         }
162 }
163
164 void show_stack(struct task_struct *task, unsigned long *esp)
165 {
166         unsigned long *stack;
167         int i;
168
169         if (esp == NULL) {
170                 if (task)
171                         esp = (unsigned long*)task->thread.esp;
172                 else
173                         esp = (unsigned long *)&esp;
174         }
175
176         stack = esp;
177         for(i = 0; i < kstack_depth_to_print; i++) {
178                 if (kstack_end(stack))
179                         break;
180                 if (i && ((i % 8) == 0))
181                         printk("\n       ");
182                 printk("%08lx ", *stack++);
183         }
184         printk("\nCall Trace:\n");
185         show_trace(task, esp);
186 }
187
188 /*
189  * The architecture-independent dump_stack generator
190  */
191 void dump_stack(void)
192 {
193         unsigned long stack;
194
195         show_trace(current, &stack);
196 }
197
198 EXPORT_SYMBOL(dump_stack);
199
200 void show_registers(struct pt_regs *regs)
201 {
202         int i;
203         int in_kernel = 1;
204         unsigned long esp;
205         unsigned short ss;
206
207         esp = (unsigned long) (&regs->esp);
208         ss = __KERNEL_DS;
209         if (regs->xcs & 2) {
210                 in_kernel = 0;
211                 esp = regs->esp;
212                 ss = regs->xss & 0xffff;
213         }
214         print_modules();
215         printk("CPU:    %d\nEIP:    %04x:[<%08lx>]    %s VLI\nEFLAGS: %08lx"
216                         "   (%s) \n",
217                 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
218                 print_tainted(), regs->eflags, system_utsname.release);
219         print_symbol("EIP is at %s\n", regs->eip);
220         printk("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
221                 regs->eax, regs->ebx, regs->ecx, regs->edx);
222         printk("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
223                 regs->esi, regs->edi, regs->ebp, esp);
224         printk("ds: %04x   es: %04x   ss: %04x\n",
225                 regs->xds & 0xffff, regs->xes & 0xffff, ss);
226         printk("Process %s (pid: %d, threadinfo=%p task=%p)",
227                 current->comm, current->pid, current_thread_info(), current);
228         /*
229          * When in-kernel, we also print out the stack and code at the
230          * time of the fault..
231          */
232         if (in_kernel) {
233                 u8 *eip;
234
235                 printk("\nStack: ");
236                 show_stack(NULL, (unsigned long*)esp);
237
238                 printk("Code: ");
239
240                 eip = (u8 *)regs->eip - 43;
241                 for (i = 0; i < 64; i++, eip++) {
242                         unsigned char c;
243
244                         if (eip < (u8 *)PAGE_OFFSET || __get_user(c, eip)) {
245                                 printk(" Bad EIP value.");
246                                 break;
247                         }
248                         if (eip == (u8 *)regs->eip)
249                                 printk("<%02x> ", c);
250                         else
251                                 printk("%02x ", c);
252                 }
253         }
254         printk("\n");
255 }       
256
257 static void handle_BUG(struct pt_regs *regs)
258 {
259         unsigned short ud2;
260         unsigned short line;
261         char *file;
262         char c;
263         unsigned long eip;
264
265         if (regs->xcs & 2)
266                 goto no_bug;            /* Not in kernel */
267
268         eip = regs->eip;
269
270         if (eip < PAGE_OFFSET)
271                 goto no_bug;
272         if (__get_user(ud2, (unsigned short *)eip))
273                 goto no_bug;
274         if (ud2 != 0x0b0f)
275                 goto no_bug;
276         if (__get_user(line, (unsigned short *)(eip + 2)))
277                 goto bug;
278         if (__get_user(file, (char **)(eip + 4)) ||
279                 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
280                 file = "<bad filename>";
281
282         printk("------------[ cut here ]------------\n");
283         printk(KERN_ALERT "kernel BUG at %s:%d!\n", file, line);
284
285 no_bug:
286         return;
287
288         /* Here we know it was a BUG but file-n-line is unavailable */
289 bug:
290         printk("Kernel BUG\n");
291 }
292
293 void die(const char * str, struct pt_regs * regs, long err)
294 {
295         static struct {
296                 spinlock_t lock;
297                 u32 lock_owner;
298                 int lock_owner_depth;
299         } die = {
300                 .lock =                 SPIN_LOCK_UNLOCKED,
301                 .lock_owner =           -1,
302                 .lock_owner_depth =     0
303         };
304         static int die_counter;
305
306         if (die.lock_owner != _smp_processor_id()) {
307                 console_verbose();
308                 spin_lock_irq(&die.lock);
309                 die.lock_owner = smp_processor_id();
310                 die.lock_owner_depth = 0;
311                 bust_spinlocks(1);
312         }
313
314         if (++die.lock_owner_depth < 3) {
315                 int nl = 0;
316                 handle_BUG(regs);
317                 printk(KERN_ALERT "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
318 #ifdef CONFIG_PREEMPT
319                 printk("PREEMPT ");
320                 nl = 1;
321 #endif
322 #ifdef CONFIG_SMP
323                 printk("SMP ");
324                 nl = 1;
325 #endif
326 #ifdef CONFIG_DEBUG_PAGEALLOC
327                 printk("DEBUG_PAGEALLOC");
328                 nl = 1;
329 #endif
330                 if (nl)
331                         printk("\n");
332         notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
333                 show_registers(regs);
334         } else
335                 printk(KERN_ERR "Recursive die() failure, output suppressed\n");
336
337         bust_spinlocks(0);
338         die.lock_owner = -1;
339         spin_unlock_irq(&die.lock);
340         if (in_interrupt())
341                 panic("Fatal exception in interrupt");
342
343         if (panic_on_oops) {
344                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
345                 set_current_state(TASK_UNINTERRUPTIBLE);
346                 schedule_timeout(5 * HZ);
347                 panic("Fatal exception");
348         }
349         do_exit(SIGSEGV);
350 }
351
352 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
353 {
354         if (!(regs->eflags & VM_MASK) && !(2 & regs->xcs))
355                 die(str, regs, err);
356 }
357
358 static void do_trap(int trapnr, int signr, char *str, int vm86,
359                            struct pt_regs * regs, long error_code, siginfo_t *info)
360 {
361         if (regs->eflags & VM_MASK) {
362                 if (vm86)
363                         goto vm86_trap;
364                 goto trap_signal;
365         }
366
367         if (!(regs->xcs & 2))
368                 goto kernel_trap;
369
370         trap_signal: {
371                 struct task_struct *tsk = current;
372                 tsk->thread.error_code = error_code;
373                 tsk->thread.trap_no = trapnr;
374                 if (info)
375                         force_sig_info(signr, info, tsk);
376                 else
377                         force_sig(signr, tsk);
378                 return;
379         }
380
381         kernel_trap: {
382                 if (!fixup_exception(regs))
383                         die(str, regs, error_code);
384                 return;
385         }
386
387         vm86_trap: {
388                 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
389                 if (ret) goto trap_signal;
390                 return;
391         }
392 }
393
394 #define DO_ERROR(trapnr, signr, str, name) \
395 fastcall void do_##name(struct pt_regs * regs, long error_code) \
396 { \
397         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
398                                                 == NOTIFY_STOP) \
399                 return; \
400         do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
401 }
402
403 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
404 fastcall void do_##name(struct pt_regs * regs, long error_code) \
405 { \
406         siginfo_t info; \
407         info.si_signo = signr; \
408         info.si_errno = 0; \
409         info.si_code = sicode; \
410         info.si_addr = (void __user *)siaddr; \
411         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
412                                                 == NOTIFY_STOP) \
413                 return; \
414         do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
415 }
416
417 #define DO_VM86_ERROR(trapnr, signr, str, name) \
418 fastcall void do_##name(struct pt_regs * regs, long error_code) \
419 { \
420         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
421                                                 == NOTIFY_STOP) \
422                 return; \
423         do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
424 }
425
426 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
427 fastcall void do_##name(struct pt_regs * regs, long error_code) \
428 { \
429         siginfo_t info; \
430         info.si_signo = signr; \
431         info.si_errno = 0; \
432         info.si_code = sicode; \
433         info.si_addr = (void __user *)siaddr; \
434         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
435                                                 == NOTIFY_STOP) \
436                 return; \
437         do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
438 }
439
440 DO_VM86_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->eip)
441 #ifndef CONFIG_KPROBES
442 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
443 #endif
444 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
445 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
446 DO_ERROR_INFO( 6, SIGILL,  "invalid operand", invalid_op, ILL_ILLOPN, regs->eip)
447 DO_VM86_ERROR( 7, SIGSEGV, "device not available", device_not_available)
448 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)
449 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
450 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)
451 DO_ERROR(12, SIGBUS,  "stack segment", stack_segment)
452 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
453 #ifdef CONFIG_X86_MCE
454 DO_ERROR(18, SIGBUS, "machine check", machine_check)
455 #endif
456
457 /*
458  * the original non-exec stack patch was written by
459  * Solar Designer <solar at openwall.com>. Thanks!
460  */
461 fastcall void do_general_protection(struct pt_regs * regs, long error_code)
462 {
463         /*
464          * If we trapped on an LDT access then ensure that the default_ldt is
465          * loaded, if nothing else. We load default_ldt lazily because LDT
466          * switching costs time and many applications don't need it.
467          */
468         if (unlikely((error_code & 6) == 4)) {
469                 unsigned long ldt;
470                 __asm__ __volatile__ ("sldt %0" : "=r" (ldt));
471                 if (ldt == 0) {
472                         xen_set_ldt((unsigned long)&default_ldt[0], 5);
473                         return;
474                 }
475         }
476
477         if (regs->eflags & VM_MASK)
478                 goto gp_in_vm86;
479
480         if (!(regs->xcs & 2))
481                 goto gp_in_kernel;
482
483         /*
484          * lazy-check for CS validity on exec-shield binaries:
485          */
486         if (current->mm) {
487                 int cpu = smp_processor_id();
488                 struct desc_struct *desc1, *desc2;
489                 struct vm_area_struct *vma;
490                 unsigned long limit = 0;
491
492                 spin_lock(&current->mm->page_table_lock);
493                 for (vma = current->mm->mmap; vma; vma = vma->vm_next)
494                         if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
495                                 limit = vma->vm_end;
496                 spin_unlock(&current->mm->page_table_lock);
497
498                 current->mm->context.exec_limit = limit;
499                 set_user_cs(&current->mm->context.user_cs, limit);
500
501                 desc1 = &current->mm->context.user_cs;
502                 desc2 = &get_cpu_gdt_table(cpu)[GDT_ENTRY_DEFAULT_USER_CS];
503                 
504                 /*
505                  * The CS was not in sync - reload it and retry the
506                  * instruction. If the instruction still faults then
507                  * we wont hit this branch next time around.
508                  */
509                 if (desc1->a != desc2->a || desc1->b != desc2->b) {
510                         load_user_cs_desc(cpu, current->mm);
511                         return;
512                 }
513         }
514
515         current->thread.error_code = error_code;
516         current->thread.trap_no = 13;
517         force_sig(SIGSEGV, current);
518         return;
519
520 gp_in_vm86:
521         local_irq_enable();
522         handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
523         return;
524
525 gp_in_kernel:
526         if (!fixup_exception(regs)) {
527                 if (notify_die(DIE_GPF, "general protection fault", regs,
528                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
529                         return;
530                 die("general protection fault", regs, error_code);
531         }
532 }
533
534 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
535 {
536         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
537         printk("You probably have a hardware problem with your RAM chips\n");
538
539         /* Clear and disable the memory parity error line. */
540         clear_mem_error(reason);
541 }
542
543 static void io_check_error(unsigned char reason, struct pt_regs * regs)
544 {
545         unsigned long i;
546
547         printk("NMI: IOCK error (debug interrupt?)\n");
548         show_registers(regs);
549
550         /* Re-enable the IOCK line, wait for a few seconds */
551         reason = (reason & 0xf) | 8;
552         outb(reason, 0x61);
553         i = 2000;
554         while (--i) udelay(1000);
555         reason &= ~8;
556         outb(reason, 0x61);
557 }
558
559 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
560 {
561 #ifdef CONFIG_MCA
562         /* Might actually be able to figure out what the guilty party
563         * is. */
564         if( MCA_bus ) {
565                 mca_handle_nmi();
566                 return;
567         }
568 #endif
569         printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
570                 reason, smp_processor_id());
571         printk("Dazed and confused, but trying to continue\n");
572         printk("Do you have a strange power saving mode enabled?\n");
573 }
574
575 static DEFINE_SPINLOCK(nmi_print_lock);
576
577 void die_nmi (struct pt_regs *regs, const char *msg)
578 {
579         spin_lock(&nmi_print_lock);
580         /*
581         * We are in trouble anyway, lets at least try
582         * to get a message out.
583         */
584         bust_spinlocks(1);
585         printk(msg);
586         printk(" on CPU%d, eip %08lx, registers:\n",
587                 smp_processor_id(), regs->eip);
588         show_registers(regs);
589         printk("console shuts up ...\n");
590         console_silent();
591         spin_unlock(&nmi_print_lock);
592         bust_spinlocks(0);
593         do_exit(SIGSEGV);
594 }
595
596 static void default_do_nmi(struct pt_regs * regs)
597 {
598         unsigned char reason = 0;
599
600         /* Only the BSP gets external NMIs from the system.  */
601         if (!smp_processor_id())
602                 reason = get_nmi_reason();
603  
604         if (!(reason & 0xc0)) {
605                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT)
606                                                         == NOTIFY_STOP)
607                         return;
608 #ifdef CONFIG_X86_LOCAL_APIC
609                 /*
610                  * Ok, so this is none of the documented NMI sources,
611                  * so it must be the NMI watchdog.
612                  */
613                 if (nmi_watchdog) {
614                         nmi_watchdog_tick(regs);
615                         return;
616                 }
617 #endif
618                 unknown_nmi_error(reason, regs);
619                 return;
620         }
621         if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_STOP)
622                 return;
623         if (reason & 0x80)
624                 mem_parity_error(reason, regs);
625         if (reason & 0x40)
626                 io_check_error(reason, regs);
627         /*
628          * Reassert NMI in case it became active meanwhile
629          * as it's edge-triggered.
630          */
631         reassert_nmi();
632 }
633
634 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
635 {
636         return 0;
637 }
638  
639 static nmi_callback_t nmi_callback = dummy_nmi_callback;
640  
641 fastcall void do_nmi(struct pt_regs * regs, long error_code)
642 {
643         int cpu;
644
645         nmi_enter();
646
647         cpu = smp_processor_id();
648         ++nmi_count(cpu);
649
650         if (!nmi_callback(regs, cpu))
651                 default_do_nmi(regs);
652
653         nmi_exit();
654 }
655
656 void set_nmi_callback(nmi_callback_t callback)
657 {
658         nmi_callback = callback;
659 }
660
661 void unset_nmi_callback(void)
662 {
663         nmi_callback = dummy_nmi_callback;
664 }
665
666 #ifdef CONFIG_KPROBES
667 fastcall int do_int3(struct pt_regs *regs, long error_code)
668 {
669         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
670                         == NOTIFY_STOP)
671                 return 1;
672         /* This is an interrupt gate, because kprobes wants interrupts
673         disabled.  Normal trap handlers don't. */
674         restore_interrupts(regs);
675         do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
676         return 0;
677 }
678 #endif
679
680 /*
681  * Our handling of the processor debug registers is non-trivial.
682  * We do not clear them on entry and exit from the kernel. Therefore
683  * it is possible to get a watchpoint trap here from inside the kernel.
684  * However, the code in ./ptrace.c has ensured that the user can
685  * only set watchpoints on userspace addresses. Therefore the in-kernel
686  * watchpoint trap can only occur in code which is reading/writing
687  * from user space. Such code must not hold kernel locks (since it
688  * can equally take a page fault), therefore it is safe to call
689  * force_sig_info even though that claims and releases locks.
690  * 
691  * Code in ./signal.c ensures that the debug control register
692  * is restored before we deliver any signal, and therefore that
693  * user code runs with the correct debug control register even though
694  * we clear it here.
695  *
696  * Being careful here means that we don't have to be as careful in a
697  * lot of more complicated places (task switching can be a bit lazy
698  * about restoring all the debug state, and ptrace doesn't have to
699  * find every occurrence of the TF bit that could be saved away even
700  * by user code)
701  */
702 fastcall void do_debug(struct pt_regs * regs, long error_code)
703 {
704         unsigned int condition;
705         struct task_struct *tsk = current;
706
707         condition = HYPERVISOR_get_debugreg(6);
708
709         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
710                                         SIGTRAP) == NOTIFY_STOP)
711                 return;
712 #if 0
713         /* It's safe to allow irq's after DR6 has been saved */
714         if (regs->eflags & X86_EFLAGS_IF)
715                 local_irq_enable();
716 #endif
717
718         /* Mask out spurious debug traps due to lazy DR7 setting */
719         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
720                 if (!tsk->thread.debugreg[7])
721                         goto clear_dr7;
722         }
723
724         if (regs->eflags & VM_MASK)
725                 goto debug_vm86;
726
727         /* Save debug status register where ptrace can see it */
728         tsk->thread.debugreg[6] = condition;
729
730         /*
731          * Single-stepping through TF: make sure we ignore any events in
732          * kernel space (but re-enable TF when returning to user mode).
733          * And if the event was due to a debugger (PT_DTRACE), clear the
734          * TF flag so that register information is correct.
735          */
736         if (condition & DR_STEP) {
737                 /*
738                  * We already checked v86 mode above, so we can
739                  * check for kernel mode by just checking the CPL
740                  * of CS.
741                  */
742                 if ((regs->xcs & 2) == 0)
743                         goto clear_TF_reenable;
744
745                 if (likely(tsk->ptrace & PT_DTRACE)) {
746                         tsk->ptrace &= ~PT_DTRACE;
747                         regs->eflags &= ~TF_MASK;
748                 }
749         }
750
751         /* Ok, finally something we can handle */
752         send_sigtrap(tsk, regs, error_code);
753
754         /* Disable additional traps. They'll be re-enabled when
755          * the signal is delivered.
756          */
757 clear_dr7:
758         HYPERVISOR_set_debugreg(7, 0);
759         return;
760
761 debug_vm86:
762         handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
763         return;
764
765 clear_TF_reenable:
766         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
767         regs->eflags &= ~TF_MASK;
768         return;
769 }
770
771 /*
772  * Note that we play around with the 'TS' bit in an attempt to get
773  * the correct behaviour even in the presence of the asynchronous
774  * IRQ13 behaviour
775  */
776 void math_error(void __user *eip)
777 {
778         struct task_struct * task;
779         siginfo_t info;
780         unsigned short cwd, swd;
781
782         /*
783          * Save the info for the exception handler and clear the error.
784          */
785         task = current;
786         save_init_fpu(task);
787         task->thread.trap_no = 16;
788         task->thread.error_code = 0;
789         info.si_signo = SIGFPE;
790         info.si_errno = 0;
791         info.si_code = __SI_FAULT;
792         info.si_addr = eip;
793         /*
794          * (~cwd & swd) will mask out exceptions that are not set to unmasked
795          * status.  0x3f is the exception bits in these regs, 0x200 is the
796          * C1 reg you need in case of a stack fault, 0x040 is the stack
797          * fault bit.  We should only be taking one exception at a time,
798          * so if this combination doesn't produce any single exception,
799          * then we have a bad program that isn't syncronizing its FPU usage
800          * and it will suffer the consequences since we won't be able to
801          * fully reproduce the context of the exception
802          */
803         cwd = get_fpu_cwd(task);
804         swd = get_fpu_swd(task);
805         switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
806                 case 0x000:
807                 default:
808                         break;
809                 case 0x001: /* Invalid Op */
810                 case 0x041: /* Stack Fault */
811                 case 0x241: /* Stack Fault | Direction */
812                         info.si_code = FPE_FLTINV;
813                         /* Should we clear the SF or let user space do it ???? */
814                         break;
815                 case 0x002: /* Denormalize */
816                 case 0x010: /* Underflow */
817                         info.si_code = FPE_FLTUND;
818                         break;
819                 case 0x004: /* Zero Divide */
820                         info.si_code = FPE_FLTDIV;
821                         break;
822                 case 0x008: /* Overflow */
823                         info.si_code = FPE_FLTOVF;
824                         break;
825                 case 0x020: /* Precision */
826                         info.si_code = FPE_FLTRES;
827                         break;
828         }
829         force_sig_info(SIGFPE, &info, task);
830 }
831
832 fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
833 {
834         ignore_fpu_irq = 1;
835         math_error((void __user *)regs->eip);
836 }
837
838 void simd_math_error(void __user *eip)
839 {
840         struct task_struct * task;
841         siginfo_t info;
842         unsigned short mxcsr;
843
844         /*
845          * Save the info for the exception handler and clear the error.
846          */
847         task = current;
848         save_init_fpu(task);
849         task->thread.trap_no = 19;
850         task->thread.error_code = 0;
851         info.si_signo = SIGFPE;
852         info.si_errno = 0;
853         info.si_code = __SI_FAULT;
854         info.si_addr = eip;
855         /*
856          * The SIMD FPU exceptions are handled a little differently, as there
857          * is only a single status/control register.  Thus, to determine which
858          * unmasked exception was caught we must mask the exception mask bits
859          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
860          */
861         mxcsr = get_fpu_mxcsr(task);
862         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
863                 case 0x000:
864                 default:
865                         break;
866                 case 0x001: /* Invalid Op */
867                         info.si_code = FPE_FLTINV;
868                         break;
869                 case 0x002: /* Denormalize */
870                 case 0x010: /* Underflow */
871                         info.si_code = FPE_FLTUND;
872                         break;
873                 case 0x004: /* Zero Divide */
874                         info.si_code = FPE_FLTDIV;
875                         break;
876                 case 0x008: /* Overflow */
877                         info.si_code = FPE_FLTOVF;
878                         break;
879                 case 0x020: /* Precision */
880                         info.si_code = FPE_FLTRES;
881                         break;
882         }
883         force_sig_info(SIGFPE, &info, task);
884 }
885
886 fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
887                                           long error_code)
888 {
889         if (cpu_has_xmm) {
890                 /* Handle SIMD FPU exceptions on PIII+ processors. */
891                 ignore_fpu_irq = 1;
892                 simd_math_error((void __user *)regs->eip);
893         } else {
894                 /*
895                  * Handle strange cache flush from user space exception
896                  * in all other cases.  This is undocumented behaviour.
897                  */
898                 if (regs->eflags & VM_MASK) {
899                         handle_vm86_fault((struct kernel_vm86_regs *)regs,
900                                           error_code);
901                         return;
902                 }
903                 die_if_kernel("cache flush denied", regs, error_code);
904                 current->thread.trap_no = 19;
905                 current->thread.error_code = error_code;
906                 force_sig(SIGSEGV, current);
907         }
908 }
909
910 /*
911  *  'math_state_restore()' saves the current math information in the
912  * old math state array, and gets the new ones from the current task
913  *
914  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
915  * Don't touch unless you *really* know how it works.
916  *
917  * Must be called with kernel preemption disabled (in this case,
918  * local interrupts are disabled at the call-site in entry.S).
919  */
920 asmlinkage void math_state_restore(struct pt_regs regs)
921 {
922         struct thread_info *thread = current_thread_info();
923         struct task_struct *tsk = thread->task;
924
925         /* NB. 'clts' is done for us by Xen during virtual trap. */
926         if (!tsk_used_math(tsk))
927                 init_fpu(tsk);
928         restore_fpu(tsk);
929         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
930 }
931
932 #ifndef CONFIG_MATH_EMULATION
933
934 asmlinkage void math_emulate(long arg)
935 {
936         printk("math-emulation not enabled and no coprocessor found.\n");
937         printk("killing %s.\n",current->comm);
938         force_sig(SIGFPE,current);
939         schedule();
940 }
941
942 #endif /* CONFIG_MATH_EMULATION */
943
944 #ifdef CONFIG_X86_F00F_BUG
945 void __init trap_init_f00f_bug(void)
946 {
947         __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
948
949         /*
950          * Update the IDT descriptor and reload the IDT so that
951          * it uses the read-only mapped virtual address.
952          */
953         idt_descr.address = fix_to_virt(FIX_F00F_IDT);
954         __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
955 }
956 #endif
957
958
959 /* NB. All these are "trap gates" (i.e. events_mask isn't cleared). */
960 static trap_info_t trap_table[] = {
961         {  0, 0, __KERNEL_CS, (unsigned long)divide_error               },
962         {  1, 0, __KERNEL_CS, (unsigned long)debug                      },
963         {  3, 3, __KERNEL_CS, (unsigned long)int3                       },
964         {  4, 3, __KERNEL_CS, (unsigned long)overflow                   },
965         {  5, 3, __KERNEL_CS, (unsigned long)bounds                     },
966         {  6, 0, __KERNEL_CS, (unsigned long)invalid_op                 },
967         {  7, 0, __KERNEL_CS, (unsigned long)device_not_available       },
968         {  9, 0, __KERNEL_CS, (unsigned long)coprocessor_segment_overrun },
969         { 10, 0, __KERNEL_CS, (unsigned long)invalid_TSS                },
970         { 11, 0, __KERNEL_CS, (unsigned long)segment_not_present        },
971         { 12, 0, __KERNEL_CS, (unsigned long)stack_segment              },
972         { 13, 0, __KERNEL_CS, (unsigned long)general_protection         },
973         { 14, 0, __KERNEL_CS, (unsigned long)page_fault                 },
974         { 15, 0, __KERNEL_CS, (unsigned long)fixup_4gb_segment          },
975         { 16, 0, __KERNEL_CS, (unsigned long)coprocessor_error          },
976         { 17, 0, __KERNEL_CS, (unsigned long)alignment_check            },
977 #ifdef CONFIG_X86_MCE
978         { 18, 0, __KERNEL_CS, (unsigned long)machine_check              },
979 #endif
980         { 19, 0, __KERNEL_CS, (unsigned long)simd_coprocessor_error     },
981         { SYSCALL_VECTOR,  3, __KERNEL_CS, (unsigned long)system_call   },
982         {  0, 0,           0, 0                                         }
983 };
984
985 void __init trap_init(void)
986 {
987         HYPERVISOR_set_trap_table(trap_table);
988         HYPERVISOR_set_fast_trap(SYSCALL_VECTOR);
989
990         /*
991          * default LDT is a single-entry callgate to lcall7 for iBCS
992          * and a callgate to lcall27 for Solaris/x86 binaries
993          */
994         make_lowmem_page_readonly(&default_ldt[0]);
995
996         /*
997          * Should be a barrier for any external CPU state.
998          */
999         cpu_init();
1000 }
1001
1002 int smp_trap_init(trap_info_t *trap_ctxt)
1003 {
1004         trap_info_t *t = trap_table;
1005
1006         for (t = trap_table; t->address; t++) {
1007                 trap_ctxt[t->vector].flags = t->flags;
1008                 trap_ctxt[t->vector].cs = t->cs;
1009                 trap_ctxt[t->vector].address = t->address;
1010         }
1011         return SYSCALL_VECTOR;
1012 }