This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / i386 / kernel / traps-xen.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 #include <linux/kexec.h>
31
32 #ifdef CONFIG_EISA
33 #include <linux/ioport.h>
34 #include <linux/eisa.h>
35 #endif
36
37 #ifdef CONFIG_MCA
38 #include <linux/mca.h>
39 #endif
40
41 #include <asm/processor.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/io.h>
45 #include <asm/atomic.h>
46 #include <asm/debugreg.h>
47 #include <asm/desc.h>
48 #include <asm/i387.h>
49 #include <asm/nmi.h>
50
51 #include <asm/smp.h>
52 #include <asm/arch_hooks.h>
53 #include <asm/kdebug.h>
54
55 #include <linux/module.h>
56
57 #include "mach_traps.h"
58
59 asmlinkage int system_call(void);
60
61 struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
62                 { 0, 0 }, { 0, 0 } };
63
64 /* Do we ignore FPU interrupts ? */
65 char ignore_fpu_irq = 0;
66
67 #ifndef CONFIG_X86_NO_IDT
68 /*
69  * The IDT has to be page-aligned to simplify the Pentium
70  * F0 0F bug workaround.. We have a special link segment
71  * for this.
72  */
73 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
74 #endif
75
76 asmlinkage void divide_error(void);
77 asmlinkage void debug(void);
78 asmlinkage void nmi(void);
79 asmlinkage void int3(void);
80 asmlinkage void overflow(void);
81 asmlinkage void bounds(void);
82 asmlinkage void invalid_op(void);
83 asmlinkage void device_not_available(void);
84 asmlinkage void coprocessor_segment_overrun(void);
85 asmlinkage void invalid_TSS(void);
86 asmlinkage void segment_not_present(void);
87 asmlinkage void stack_segment(void);
88 asmlinkage void general_protection(void);
89 asmlinkage void page_fault(void);
90 asmlinkage void coprocessor_error(void);
91 asmlinkage void simd_coprocessor_error(void);
92 asmlinkage void alignment_check(void);
93 #ifndef CONFIG_XEN
94 asmlinkage void spurious_interrupt_bug(void);
95 #else
96 asmlinkage void fixup_4gb_segment(void);
97 #endif
98 asmlinkage void machine_check(void);
99
100 static int kstack_depth_to_print = 24;
101 ATOMIC_NOTIFIER_HEAD(i386die_chain);
102
103 int register_die_notifier(struct notifier_block *nb)
104 {
105         vmalloc_sync_all();
106         return atomic_notifier_chain_register(&i386die_chain, nb);
107 }
108 EXPORT_SYMBOL(register_die_notifier);
109
110 int unregister_die_notifier(struct notifier_block *nb)
111 {
112         return atomic_notifier_chain_unregister(&i386die_chain, nb);
113 }
114 EXPORT_SYMBOL(unregister_die_notifier);
115
116 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
117 {
118         return  p > (void *)tinfo &&
119                 p < (void *)tinfo + THREAD_SIZE - 3;
120 }
121
122 /*
123  * Print CONFIG_STACK_BACKTRACE_COLS address/symbol entries per line.
124  */
125 static inline int print_addr_and_symbol(unsigned long addr, char *log_lvl,
126                                         int printed)
127 {
128         if (!printed)
129                 printk(log_lvl);
130
131 #if CONFIG_STACK_BACKTRACE_COLS == 1
132         printk(" [<%08lx>] ", addr);
133 #else
134         printk(" <%08lx> ", addr);
135 #endif
136         print_symbol("%s", addr);
137
138         printed = (printed + 1) % CONFIG_STACK_BACKTRACE_COLS;
139         if (printed)
140                 printk(" ");
141         else
142                 printk("\n");
143
144         return printed;
145 }
146
147 static inline unsigned long print_context_stack(struct thread_info *tinfo,
148                                 unsigned long *stack, unsigned long ebp,
149                                 char *log_lvl)
150 {
151         unsigned long addr;
152         int printed = 0; /* nr of entries already printed on current line */
153
154 #ifdef  CONFIG_FRAME_POINTER
155         while (valid_stack_ptr(tinfo, (void *)ebp)) {
156                 addr = *(unsigned long *)(ebp + 4);
157                 printed = print_addr_and_symbol(addr, log_lvl, printed);
158                 ebp = *(unsigned long *)ebp;
159         }
160 #else
161         while (valid_stack_ptr(tinfo, stack)) {
162                 addr = *stack++;
163                 if (__kernel_text_address(addr))
164                         printed = print_addr_and_symbol(addr, log_lvl, printed);
165         }
166 #endif
167         if (printed)
168                 printk("\n");
169
170         return ebp;
171 }
172
173 static void show_trace_log_lvl(struct task_struct *task,
174                                unsigned long *stack, char *log_lvl)
175 {
176         unsigned long ebp;
177
178         if (!task)
179                 task = current;
180
181         if (task == current) {
182                 /* Grab ebp right from our regs */
183                 asm ("movl %%ebp, %0" : "=r" (ebp) : );
184         } else {
185                 /* ebp is the last reg pushed by switch_to */
186                 ebp = *(unsigned long *) task->thread.esp;
187         }
188
189         while (1) {
190                 struct thread_info *context;
191                 context = (struct thread_info *)
192                         ((unsigned long)stack & (~(THREAD_SIZE - 1)));
193                 ebp = print_context_stack(context, stack, ebp, log_lvl);
194                 stack = (unsigned long*)context->previous_esp;
195                 if (!stack)
196                         break;
197                 printk("%s =======================\n", log_lvl);
198         }
199 }
200
201 void show_trace(struct task_struct *task, unsigned long * stack)
202 {
203         show_trace_log_lvl(task, stack, "");
204 }
205
206 static void show_stack_log_lvl(struct task_struct *task, unsigned long *esp,
207                                char *log_lvl)
208 {
209         unsigned long *stack;
210         int i;
211
212         if (esp == NULL) {
213                 if (task)
214                         esp = (unsigned long*)task->thread.esp;
215                 else
216                         esp = (unsigned long *)&esp;
217         }
218
219         stack = esp;
220         printk(log_lvl);
221         for(i = 0; i < kstack_depth_to_print; i++) {
222                 if (kstack_end(stack))
223                         break;
224                 if (i && ((i % 8) == 0))
225                         printk("\n%s       ", log_lvl);
226                 printk("%08lx ", *stack++);
227         }
228         printk("\n%sCall Trace:\n", log_lvl);
229         show_trace_log_lvl(task, esp, log_lvl);
230 }
231
232 void show_stack(struct task_struct *task, unsigned long *esp)
233 {
234         printk("       ");
235         show_stack_log_lvl(task, esp, "");
236 }
237
238 /*
239  * The architecture-independent dump_stack generator
240  */
241 void dump_stack(void)
242 {
243         unsigned long stack;
244
245         show_trace(current, &stack);
246 }
247
248 EXPORT_SYMBOL(dump_stack);
249
250 void show_registers(struct pt_regs *regs)
251 {
252         int i;
253         int in_kernel = 1;
254         unsigned long esp;
255         unsigned short ss;
256
257         esp = (unsigned long) (&regs->esp);
258         savesegment(ss, ss);
259         if (user_mode_vm(regs)) {
260                 in_kernel = 0;
261                 esp = regs->esp;
262                 ss = regs->xss & 0xffff;
263         }
264         print_modules();
265         printk(KERN_EMERG "CPU:    %d\nEIP:    %04x:[<%08lx>]    %s VLI\n"
266                         "EFLAGS: %08lx   (%s %.*s) \n",
267                 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
268                 print_tainted(), regs->eflags, system_utsname.release,
269                 (int)strcspn(system_utsname.version, " "),
270                 system_utsname.version);
271         print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
272         printk(KERN_EMERG "eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
273                 regs->eax, regs->ebx, regs->ecx, regs->edx);
274         printk(KERN_EMERG "esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
275                 regs->esi, regs->edi, regs->ebp, esp);
276         printk(KERN_EMERG "ds: %04x   es: %04x   ss: %04x\n",
277                 regs->xds & 0xffff, regs->xes & 0xffff, ss);
278         printk(KERN_EMERG "Process %s (pid: %d, threadinfo=%p task=%p)",
279                 current->comm, current->pid, current_thread_info(), current);
280         /*
281          * When in-kernel, we also print out the stack and code at the
282          * time of the fault..
283          */
284         if (in_kernel) {
285                 u8 __user *eip;
286
287                 printk("\n" KERN_EMERG "Stack: ");
288                 show_stack_log_lvl(NULL, (unsigned long *)esp, KERN_EMERG);
289
290                 printk(KERN_EMERG "Code: ");
291
292                 eip = (u8 __user *)regs->eip - 43;
293                 for (i = 0; i < 64; i++, eip++) {
294                         unsigned char c;
295
296                         if (eip < (u8 __user *)PAGE_OFFSET || __get_user(c, eip)) {
297                                 printk(" Bad EIP value.");
298                                 break;
299                         }
300                         if (eip == (u8 __user *)regs->eip)
301                                 printk("<%02x> ", c);
302                         else
303                                 printk("%02x ", c);
304                 }
305         }
306         printk("\n");
307 }       
308
309 static void handle_BUG(struct pt_regs *regs)
310 {
311         unsigned short ud2;
312         unsigned short line;
313         char *file;
314         char c;
315         unsigned long eip;
316
317         eip = regs->eip;
318
319         if (eip < PAGE_OFFSET)
320                 goto no_bug;
321         if (__get_user(ud2, (unsigned short __user *)eip))
322                 goto no_bug;
323         if (ud2 != 0x0b0f)
324                 goto no_bug;
325         if (__get_user(line, (unsigned short __user *)(eip + 2)))
326                 goto bug;
327         if (__get_user(file, (char * __user *)(eip + 4)) ||
328                 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
329                 file = "<bad filename>";
330
331         printk(KERN_EMERG "------------[ cut here ]------------\n");
332         printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
333
334 no_bug:
335         return;
336
337         /* Here we know it was a BUG but file-n-line is unavailable */
338 bug:
339         printk(KERN_EMERG "Kernel BUG\n");
340 }
341
342 /* This is gone through when something in the kernel
343  * has done something bad and is about to be terminated.
344 */
345 void die(const char * str, struct pt_regs * regs, long err)
346 {
347         static struct {
348                 spinlock_t lock;
349                 u32 lock_owner;
350                 int lock_owner_depth;
351         } die = {
352                 .lock =                 SPIN_LOCK_UNLOCKED,
353                 .lock_owner =           -1,
354                 .lock_owner_depth =     0
355         };
356         static int die_counter;
357         unsigned long flags;
358
359         oops_enter();
360
361         if (die.lock_owner != raw_smp_processor_id()) {
362                 console_verbose();
363                 spin_lock_irqsave(&die.lock, flags);
364                 die.lock_owner = smp_processor_id();
365                 die.lock_owner_depth = 0;
366                 bust_spinlocks(1);
367         }
368         else
369                 local_save_flags(flags);
370
371         if (++die.lock_owner_depth < 3) {
372                 int nl = 0;
373                 unsigned long esp;
374                 unsigned short ss;
375
376                 handle_BUG(regs);
377                 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
378 #ifdef CONFIG_PREEMPT
379                 printk(KERN_EMERG "PREEMPT ");
380                 nl = 1;
381 #endif
382 #ifdef CONFIG_SMP
383                 if (!nl)
384                         printk(KERN_EMERG);
385                 printk("SMP ");
386                 nl = 1;
387 #endif
388 #ifdef CONFIG_DEBUG_PAGEALLOC
389                 if (!nl)
390                         printk(KERN_EMERG);
391                 printk("DEBUG_PAGEALLOC");
392                 nl = 1;
393 #endif
394                 if (nl)
395                         printk("\n");
396                 if (notify_die(DIE_OOPS, str, regs, err,
397                                         current->thread.trap_no, SIGSEGV) !=
398                                 NOTIFY_STOP) {
399                         show_registers(regs);
400                         /* Executive summary in case the oops scrolled away */
401                         esp = (unsigned long) (&regs->esp);
402                         savesegment(ss, ss);
403                         if (user_mode(regs)) {
404                                 esp = regs->esp;
405                                 ss = regs->xss & 0xffff;
406                         }
407                         printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
408                         print_symbol("%s", regs->eip);
409                         printk(" SS:ESP %04x:%08lx\n", ss, esp);
410                 }
411                 else
412                         regs = NULL;
413         } else
414                 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
415
416         bust_spinlocks(0);
417         die.lock_owner = -1;
418         spin_unlock_irqrestore(&die.lock, flags);
419
420         if (!regs)
421                 return;
422
423         if (kexec_should_crash(current))
424                 crash_kexec(regs);
425
426         if (in_interrupt())
427                 panic("Fatal exception in interrupt");
428
429         if (panic_on_oops) {
430                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
431                 ssleep(5);
432                 panic("Fatal exception");
433         }
434         oops_exit();
435         do_exit(SIGSEGV);
436 }
437
438 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
439 {
440         if (!user_mode_vm(regs))
441                 die(str, regs, err);
442 }
443
444 static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
445                               struct pt_regs * regs, long error_code,
446                               siginfo_t *info)
447 {
448         struct task_struct *tsk = current;
449         tsk->thread.error_code = error_code;
450         tsk->thread.trap_no = trapnr;
451
452         if (regs->eflags & VM_MASK) {
453                 if (vm86)
454                         goto vm86_trap;
455                 goto trap_signal;
456         }
457
458         if (!user_mode(regs))
459                 goto kernel_trap;
460
461         trap_signal: {
462                 if (info)
463                         force_sig_info(signr, info, tsk);
464                 else
465                         force_sig(signr, tsk);
466                 return;
467         }
468
469         kernel_trap: {
470                 if (!fixup_exception(regs))
471                         die(str, regs, error_code);
472                 return;
473         }
474
475         vm86_trap: {
476                 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
477                 if (ret) goto trap_signal;
478                 return;
479         }
480 }
481
482 #define DO_ERROR(trapnr, signr, str, name) \
483 fastcall void do_##name(struct pt_regs * regs, long error_code) \
484 { \
485         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
486                                                 == NOTIFY_STOP) \
487                 return; \
488         do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
489 }
490
491 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
492 fastcall void do_##name(struct pt_regs * regs, long error_code) \
493 { \
494         siginfo_t info; \
495         info.si_signo = signr; \
496         info.si_errno = 0; \
497         info.si_code = sicode; \
498         info.si_addr = (void __user *)siaddr; \
499         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
500                                                 == NOTIFY_STOP) \
501                 return; \
502         do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
503 }
504
505 #define DO_VM86_ERROR(trapnr, signr, str, name) \
506 fastcall void do_##name(struct pt_regs * regs, long error_code) \
507 { \
508         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
509                                                 == NOTIFY_STOP) \
510                 return; \
511         do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
512 }
513
514 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
515 fastcall void do_##name(struct pt_regs * regs, long error_code) \
516 { \
517         siginfo_t info; \
518         info.si_signo = signr; \
519         info.si_errno = 0; \
520         info.si_code = sicode; \
521         info.si_addr = (void __user *)siaddr; \
522         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
523                                                 == NOTIFY_STOP) \
524                 return; \
525         do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
526 }
527
528 DO_VM86_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->eip)
529 #ifndef CONFIG_KPROBES
530 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
531 #endif
532 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
533 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
534 DO_ERROR_INFO( 6, SIGILL,  "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
535 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)
536 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
537 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)
538 DO_ERROR(12, SIGBUS,  "stack segment", stack_segment)
539 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
540
541
542 /*
543  * lazy-check for CS validity on exec-shield binaries:
544  *
545  * the original non-exec stack patch was written by
546  * Solar Designer <solar at openwall.com>. Thanks!
547  */
548 static int
549 check_lazy_exec_limit(int cpu, struct pt_regs *regs, long error_code)
550 {
551         struct desc_struct *desc1, *desc2;
552         struct vm_area_struct *vma;
553         unsigned long limit;
554
555         if (current->mm == NULL)
556                 return 0;
557
558         limit = -1UL;
559         if (current->mm->context.exec_limit != -1UL) {
560                 limit = PAGE_SIZE;
561                 spin_lock(&current->mm->page_table_lock);
562                 for (vma = current->mm->mmap; vma; vma = vma->vm_next)
563                         if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
564                                 limit = vma->vm_end;
565                 spin_unlock(&current->mm->page_table_lock);
566                 if (limit >= TASK_SIZE)
567                         limit = -1UL;
568                 current->mm->context.exec_limit = limit;
569         }
570         set_user_cs(&current->mm->context.user_cs, limit);
571
572         desc1 = &current->mm->context.user_cs;
573         desc2 = &get_cpu_gdt_table(cpu)[GDT_ENTRY_DEFAULT_USER_CS];
574
575         if (desc1->a != desc2->a || desc1->b != desc2->b) {
576                 /*
577                  * The CS was not in sync - reload it and retry the
578                  * instruction. If the instruction still faults then
579                  * we won't hit this branch next time around.
580                  */
581                 if (print_fatal_signals >= 2) {
582                         printk("#GPF fixup (%ld[seg:%lx]) at %08lx, CPU#%d.\n", error_code, error_code/8, regs->eip, smp_processor_id());
583                         printk(" exec_limit: %08lx, user_cs: %08lx/%08lx, CPU_cs: %08lx/%08lx.\n", current->mm->context.exec_limit, desc1->a, desc1->b, desc2->a, desc2->b);
584                 }
585                 load_user_cs_desc(cpu, current->mm);
586                 return 1;
587         }
588
589         return 0;
590 }
591
592 /*
593  * The fixup code for errors in iret jumps to here (iret_exc).  It loses
594  * the original trap number and error code.  The bogus trap 32 and error
595  * code 0 are what the vanilla kernel delivers via:
596  * DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
597  *
598  * In case of a general protection fault in the iret instruction, we
599  * need to check for a lazy CS update for exec-shield.
600  */
601 fastcall void do_iret_error(struct pt_regs *regs, long error_code)
602 {
603         int ok = check_lazy_exec_limit(get_cpu(), regs, error_code);
604         put_cpu();
605         if (!ok && notify_die(DIE_TRAP, "iret exception", regs,
606                               error_code, 32, SIGSEGV) != NOTIFY_STOP) {
607                 siginfo_t info;
608                 info.si_signo = SIGSEGV;
609                 info.si_errno = 0;
610                 info.si_code = ILL_BADSTK;
611                 info.si_addr = 0;
612                 do_trap(32, SIGSEGV, "iret exception", 0, regs, error_code,
613                         &info);
614         }
615 }
616
617 fastcall void __kprobes do_general_protection(struct pt_regs * regs,
618                                               long error_code)
619 {
620         int cpu = get_cpu();
621         int ok;
622
623         ok = check_lazy_exec_limit(cpu, regs, error_code);
624         put_cpu();
625
626         if (ok)
627                 return;
628
629         if (print_fatal_signals) {
630                 printk("#GPF(%ld[seg:%lx]) at %08lx, CPU#%d.\n", error_code, error_code/8, regs->eip, smp_processor_id());
631                 printk(" exec_limit: %08lx, user_cs: %08lx/%08lx.\n", current->mm->context.exec_limit, current->mm->context.user_cs.a, current->mm->context.user_cs.b);
632         }
633
634         if (regs->eflags & VM_MASK)
635                 goto gp_in_vm86;
636
637         if (!user_mode(regs))
638                 goto gp_in_kernel;
639
640         current->thread.error_code = error_code;
641         current->thread.trap_no = 13;
642         force_sig(SIGSEGV, current);
643         return;
644
645 gp_in_vm86:
646         local_irq_enable();
647         handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
648         return;
649
650 gp_in_kernel:
651         if (!fixup_exception(regs)) {
652                 if (notify_die(DIE_GPF, "general protection fault", regs,
653                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
654                         return;
655                 die("general protection fault", regs, error_code);
656         }
657 }
658
659 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
660 {
661         printk(KERN_EMERG "Uhhuh. NMI received. Dazed and confused, but trying "
662                         "to continue\n");
663         printk(KERN_EMERG "You probably have a hardware problem with your RAM "
664                         "chips\n");
665
666         /* Clear and disable the memory parity error line. */
667         clear_mem_error(reason);
668 }
669
670 static void io_check_error(unsigned char reason, struct pt_regs * regs)
671 {
672         printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
673         show_registers(regs);
674
675         /* Re-enable the IOCK line, wait for a few seconds */
676         clear_io_check_error(reason);
677 }
678
679 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
680 {
681 #ifdef CONFIG_MCA
682         /* Might actually be able to figure out what the guilty party
683         * is. */
684         if( MCA_bus ) {
685                 mca_handle_nmi();
686                 return;
687         }
688 #endif
689         printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
690                 reason, smp_processor_id());
691         printk("Dazed and confused, but trying to continue\n");
692         printk("Do you have a strange power saving mode enabled?\n");
693 }
694
695 static DEFINE_SPINLOCK(nmi_print_lock);
696
697 void die_nmi (struct pt_regs *regs, const char *msg)
698 {
699         if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
700             NOTIFY_STOP)
701                 return;
702
703         spin_lock(&nmi_print_lock);
704         /*
705         * We are in trouble anyway, lets at least try
706         * to get a message out.
707         */
708         bust_spinlocks(1);
709         printk(KERN_EMERG "%s", msg);
710         printk(" on CPU%d, eip %08lx, registers:\n",
711                 smp_processor_id(), regs->eip);
712         show_registers(regs);
713         printk(KERN_EMERG "console shuts up ...\n");
714         console_silent();
715         spin_unlock(&nmi_print_lock);
716         bust_spinlocks(0);
717
718         /* If we are in kernel we are probably nested up pretty bad
719          * and might aswell get out now while we still can.
720         */
721         if (!user_mode_vm(regs)) {
722                 current->thread.trap_no = 2;
723                 crash_kexec(regs);
724         }
725
726         do_exit(SIGSEGV);
727 }
728
729 static void default_do_nmi(struct pt_regs * regs)
730 {
731         unsigned char reason = 0;
732
733         /* Only the BSP gets external NMIs from the system.  */
734         if (!smp_processor_id())
735                 reason = get_nmi_reason();
736  
737         if (!(reason & 0xc0)) {
738                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
739                                                         == NOTIFY_STOP)
740                         return;
741 #ifdef CONFIG_X86_LOCAL_APIC
742                 /*
743                  * Ok, so this is none of the documented NMI sources,
744                  * so it must be the NMI watchdog.
745                  */
746                 if (nmi_watchdog) {
747                         nmi_watchdog_tick(regs);
748                         return;
749                 }
750 #endif
751                 unknown_nmi_error(reason, regs);
752                 return;
753         }
754         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
755                 return;
756         if (reason & 0x80)
757                 mem_parity_error(reason, regs);
758         if (reason & 0x40)
759                 io_check_error(reason, regs);
760         /*
761          * Reassert NMI in case it became active meanwhile
762          * as it's edge-triggered.
763          */
764         reassert_nmi();
765 }
766
767 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
768 {
769         return 0;
770 }
771  
772 static nmi_callback_t nmi_callback = dummy_nmi_callback;
773  
774 fastcall void do_nmi(struct pt_regs * regs, long error_code)
775 {
776         int cpu;
777
778         nmi_enter();
779
780         cpu = smp_processor_id();
781
782         ++nmi_count(cpu);
783
784         if (!rcu_dereference(nmi_callback)(regs, cpu))
785                 default_do_nmi(regs);
786
787         nmi_exit();
788 }
789
790 void set_nmi_callback(nmi_callback_t callback)
791 {
792         vmalloc_sync_all();
793         rcu_assign_pointer(nmi_callback, callback);
794 }
795 EXPORT_SYMBOL_GPL(set_nmi_callback);
796
797 void unset_nmi_callback(void)
798 {
799         nmi_callback = dummy_nmi_callback;
800 }
801 EXPORT_SYMBOL_GPL(unset_nmi_callback);
802
803 #ifdef CONFIG_KPROBES
804 fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
805 {
806         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
807                         == NOTIFY_STOP)
808                 return;
809         /* This is an interrupt gate, because kprobes wants interrupts
810         disabled.  Normal trap handlers don't. */
811         restore_interrupts(regs);
812         do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
813 }
814 #endif
815
816 /*
817  * Our handling of the processor debug registers is non-trivial.
818  * We do not clear them on entry and exit from the kernel. Therefore
819  * it is possible to get a watchpoint trap here from inside the kernel.
820  * However, the code in ./ptrace.c has ensured that the user can
821  * only set watchpoints on userspace addresses. Therefore the in-kernel
822  * watchpoint trap can only occur in code which is reading/writing
823  * from user space. Such code must not hold kernel locks (since it
824  * can equally take a page fault), therefore it is safe to call
825  * force_sig_info even though that claims and releases locks.
826  * 
827  * Code in ./signal.c ensures that the debug control register
828  * is restored before we deliver any signal, and therefore that
829  * user code runs with the correct debug control register even though
830  * we clear it here.
831  *
832  * Being careful here means that we don't have to be as careful in a
833  * lot of more complicated places (task switching can be a bit lazy
834  * about restoring all the debug state, and ptrace doesn't have to
835  * find every occurrence of the TF bit that could be saved away even
836  * by user code)
837  */
838 fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
839 {
840         unsigned int condition;
841         struct task_struct *tsk = current;
842
843         get_debugreg(condition, 6);
844
845         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
846                                         SIGTRAP) == NOTIFY_STOP)
847                 return;
848         /* It's safe to allow irq's after DR6 has been saved */
849         if (regs->eflags & X86_EFLAGS_IF)
850                 local_irq_enable();
851
852         /* Mask out spurious debug traps due to lazy DR7 setting */
853         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
854                 if (!tsk->thread.debugreg[7])
855                         goto clear_dr7;
856         }
857
858         if (regs->eflags & VM_MASK)
859                 goto debug_vm86;
860
861         /* Save debug status register where ptrace can see it */
862         tsk->thread.debugreg[6] = condition;
863
864         /*
865          * Single-stepping through TF: make sure we ignore any events in
866          * kernel space (but re-enable TF when returning to user mode).
867          */
868         if (condition & DR_STEP) {
869                 /*
870                  * We already checked v86 mode above, so we can
871                  * check for kernel mode by just checking the CPL
872                  * of CS.
873                  */
874                 if (!user_mode(regs))
875                         goto clear_TF_reenable;
876         }
877
878         /* Ok, finally something we can handle */
879         send_sigtrap(tsk, regs, error_code);
880
881         /* Disable additional traps. They'll be re-enabled when
882          * the signal is delivered.
883          */
884 clear_dr7:
885         set_debugreg(0, 7);
886         return;
887
888 debug_vm86:
889         handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
890         return;
891
892 clear_TF_reenable:
893         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
894         regs->eflags &= ~TF_MASK;
895         return;
896 }
897
898 /*
899  * Note that we play around with the 'TS' bit in an attempt to get
900  * the correct behaviour even in the presence of the asynchronous
901  * IRQ13 behaviour
902  */
903 void math_error(void __user *eip)
904 {
905         struct task_struct * task;
906         siginfo_t info;
907         unsigned short cwd, swd;
908
909         /*
910          * Save the info for the exception handler and clear the error.
911          */
912         task = current;
913         save_init_fpu(task);
914         task->thread.trap_no = 16;
915         task->thread.error_code = 0;
916         info.si_signo = SIGFPE;
917         info.si_errno = 0;
918         info.si_code = __SI_FAULT;
919         info.si_addr = eip;
920         /*
921          * (~cwd & swd) will mask out exceptions that are not set to unmasked
922          * status.  0x3f is the exception bits in these regs, 0x200 is the
923          * C1 reg you need in case of a stack fault, 0x040 is the stack
924          * fault bit.  We should only be taking one exception at a time,
925          * so if this combination doesn't produce any single exception,
926          * then we have a bad program that isn't syncronizing its FPU usage
927          * and it will suffer the consequences since we won't be able to
928          * fully reproduce the context of the exception
929          */
930         cwd = get_fpu_cwd(task);
931         swd = get_fpu_swd(task);
932         switch (swd & ~cwd & 0x3f) {
933                 case 0x000: /* No unmasked exception */
934                         return;
935                 default:    /* Multiple exceptions */
936                         break;
937                 case 0x001: /* Invalid Op */
938                         /*
939                          * swd & 0x240 == 0x040: Stack Underflow
940                          * swd & 0x240 == 0x240: Stack Overflow
941                          * User must clear the SF bit (0x40) if set
942                          */
943                         info.si_code = FPE_FLTINV;
944                         break;
945                 case 0x002: /* Denormalize */
946                 case 0x010: /* Underflow */
947                         info.si_code = FPE_FLTUND;
948                         break;
949                 case 0x004: /* Zero Divide */
950                         info.si_code = FPE_FLTDIV;
951                         break;
952                 case 0x008: /* Overflow */
953                         info.si_code = FPE_FLTOVF;
954                         break;
955                 case 0x020: /* Precision */
956                         info.si_code = FPE_FLTRES;
957                         break;
958         }
959         force_sig_info(SIGFPE, &info, task);
960 }
961
962 fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
963 {
964         ignore_fpu_irq = 1;
965         math_error((void __user *)regs->eip);
966 }
967
968 static void simd_math_error(void __user *eip)
969 {
970         struct task_struct * task;
971         siginfo_t info;
972         unsigned short mxcsr;
973
974         /*
975          * Save the info for the exception handler and clear the error.
976          */
977         task = current;
978         save_init_fpu(task);
979         task->thread.trap_no = 19;
980         task->thread.error_code = 0;
981         info.si_signo = SIGFPE;
982         info.si_errno = 0;
983         info.si_code = __SI_FAULT;
984         info.si_addr = eip;
985         /*
986          * The SIMD FPU exceptions are handled a little differently, as there
987          * is only a single status/control register.  Thus, to determine which
988          * unmasked exception was caught we must mask the exception mask bits
989          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
990          */
991         mxcsr = get_fpu_mxcsr(task);
992         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
993                 case 0x000:
994                 default:
995                         break;
996                 case 0x001: /* Invalid Op */
997                         info.si_code = FPE_FLTINV;
998                         break;
999                 case 0x002: /* Denormalize */
1000                 case 0x010: /* Underflow */
1001                         info.si_code = FPE_FLTUND;
1002                         break;
1003                 case 0x004: /* Zero Divide */
1004                         info.si_code = FPE_FLTDIV;
1005                         break;
1006                 case 0x008: /* Overflow */
1007                         info.si_code = FPE_FLTOVF;
1008                         break;
1009                 case 0x020: /* Precision */
1010                         info.si_code = FPE_FLTRES;
1011                         break;
1012         }
1013         force_sig_info(SIGFPE, &info, task);
1014 }
1015
1016 fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
1017                                           long error_code)
1018 {
1019         if (cpu_has_xmm) {
1020                 /* Handle SIMD FPU exceptions on PIII+ processors. */
1021                 ignore_fpu_irq = 1;
1022                 simd_math_error((void __user *)regs->eip);
1023         } else {
1024                 /*
1025                  * Handle strange cache flush from user space exception
1026                  * in all other cases.  This is undocumented behaviour.
1027                  */
1028                 if (regs->eflags & VM_MASK) {
1029                         handle_vm86_fault((struct kernel_vm86_regs *)regs,
1030                                           error_code);
1031                         return;
1032                 }
1033                 current->thread.trap_no = 19;
1034                 current->thread.error_code = error_code;
1035                 die_if_kernel("cache flush denied", regs, error_code);
1036                 force_sig(SIGSEGV, current);
1037         }
1038 }
1039
1040 #ifndef CONFIG_XEN
1041 fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
1042                                           long error_code)
1043 {
1044 #if 0
1045         /* No need to warn about this any longer. */
1046         printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1047 #endif
1048 }
1049
1050 fastcall void setup_x86_bogus_stack(unsigned char * stk)
1051 {
1052         unsigned long *switch16_ptr, *switch32_ptr;
1053         struct pt_regs *regs;
1054         unsigned long stack_top, stack_bot;
1055         unsigned short iret_frame16_off;
1056         int cpu = smp_processor_id();
1057         /* reserve the space on 32bit stack for the magic switch16 pointer */
1058         memmove(stk, stk + 8, sizeof(struct pt_regs));
1059         switch16_ptr = (unsigned long *)(stk + sizeof(struct pt_regs));
1060         regs = (struct pt_regs *)stk;
1061         /* now the switch32 on 16bit stack */
1062         stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
1063         stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
1064         switch32_ptr = (unsigned long *)(stack_top - 8);
1065         iret_frame16_off = CPU_16BIT_STACK_SIZE - 8 - 20;
1066         /* copy iret frame on 16bit stack */
1067         memcpy((void *)(stack_bot + iret_frame16_off), &regs->eip, 20);
1068         /* fill in the switch pointers */
1069         switch16_ptr[0] = (regs->esp & 0xffff0000) | iret_frame16_off;
1070         switch16_ptr[1] = __ESPFIX_SS;
1071         switch32_ptr[0] = (unsigned long)stk + sizeof(struct pt_regs) +
1072                 8 - CPU_16BIT_STACK_SIZE;
1073         switch32_ptr[1] = __KERNEL_DS;
1074 }
1075
1076 fastcall unsigned char * fixup_x86_bogus_stack(unsigned short sp)
1077 {
1078         unsigned long *switch32_ptr;
1079         unsigned char *stack16, *stack32;
1080         unsigned long stack_top, stack_bot;
1081         int len;
1082         int cpu = smp_processor_id();
1083         stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
1084         stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
1085         switch32_ptr = (unsigned long *)(stack_top - 8);
1086         /* copy the data from 16bit stack to 32bit stack */
1087         len = CPU_16BIT_STACK_SIZE - 8 - sp;
1088         stack16 = (unsigned char *)(stack_bot + sp);
1089         stack32 = (unsigned char *)
1090                 (switch32_ptr[0] + CPU_16BIT_STACK_SIZE - 8 - len);
1091         memcpy(stack32, stack16, len);
1092         return stack32;
1093 }
1094 #endif
1095
1096 /*
1097  *  'math_state_restore()' saves the current math information in the
1098  * old math state array, and gets the new ones from the current task
1099  *
1100  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1101  * Don't touch unless you *really* know how it works.
1102  *
1103  * Must be called with kernel preemption disabled (in this case,
1104  * local interrupts are disabled at the call-site in entry.S).
1105  */
1106 asmlinkage void math_state_restore(struct pt_regs regs)
1107 {
1108         struct thread_info *thread = current_thread_info();
1109         struct task_struct *tsk = thread->task;
1110
1111         /* NB. 'clts' is done for us by Xen during virtual trap. */
1112         if (!tsk_used_math(tsk))
1113                 init_fpu(tsk);
1114         restore_fpu(tsk);
1115         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
1116 }
1117
1118 #ifndef CONFIG_MATH_EMULATION
1119
1120 asmlinkage void math_emulate(long arg)
1121 {
1122         printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1123         printk(KERN_EMERG "killing %s.\n",current->comm);
1124         force_sig(SIGFPE,current);
1125         schedule();
1126 }
1127
1128 #endif /* CONFIG_MATH_EMULATION */
1129
1130 #ifdef CONFIG_X86_F00F_BUG
1131 void __init trap_init_f00f_bug(void)
1132 {
1133         __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1134
1135         /*
1136          * Update the IDT descriptor and reload the IDT so that
1137          * it uses the read-only mapped virtual address.
1138          */
1139         idt_descr.address = fix_to_virt(FIX_F00F_IDT);
1140         load_idt(&idt_descr);
1141 }
1142 #endif
1143
1144
1145 /*
1146  * NB. All these are "trap gates" (i.e. events_mask isn't set) except
1147  * for those that specify <dpl>|4 in the second field.
1148  */
1149 static trap_info_t trap_table[] = {
1150         {  0, 0, __KERNEL_CS, (unsigned long)divide_error               },
1151         {  1, 0|4, __KERNEL_CS, (unsigned long)debug                    },
1152         {  3, 3|4, __KERNEL_CS, (unsigned long)int3                     },
1153         {  4, 3, __KERNEL_CS, (unsigned long)overflow                   },
1154         {  5, 0, __KERNEL_CS, (unsigned long)bounds                     },
1155         {  6, 0, __KERNEL_CS, (unsigned long)invalid_op                 },
1156         {  7, 0|4, __KERNEL_CS, (unsigned long)device_not_available     },
1157         {  9, 0, __KERNEL_CS, (unsigned long)coprocessor_segment_overrun },
1158         { 10, 0, __KERNEL_CS, (unsigned long)invalid_TSS                },
1159         { 11, 0, __KERNEL_CS, (unsigned long)segment_not_present        },
1160         { 12, 0, __KERNEL_CS, (unsigned long)stack_segment              },
1161         { 13, 0, __KERNEL_CS, (unsigned long)general_protection         },
1162         { 14, 0|4, __KERNEL_CS, (unsigned long)page_fault               },
1163         { 15, 0, __KERNEL_CS, (unsigned long)fixup_4gb_segment          },
1164         { 16, 0, __KERNEL_CS, (unsigned long)coprocessor_error          },
1165         { 17, 0, __KERNEL_CS, (unsigned long)alignment_check            },
1166 #ifdef CONFIG_X86_MCE
1167         { 18, 0, __KERNEL_CS, (unsigned long)machine_check              },
1168 #endif
1169         { 19, 0, __KERNEL_CS, (unsigned long)simd_coprocessor_error     },
1170         { SYSCALL_VECTOR,  3, __KERNEL_CS, (unsigned long)system_call   },
1171         {  0, 0,           0, 0                                         }
1172 };
1173
1174 void __init trap_init(void)
1175 {
1176         HYPERVISOR_set_trap_table(trap_table);
1177
1178         if (cpu_has_fxsr) {
1179                 /*
1180                  * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1181                  * Generates a compile-time "error: zero width for bit-field" if
1182                  * the alignment is wrong.
1183                  */
1184                 struct fxsrAlignAssert {
1185                         int _:!(offsetof(struct task_struct,
1186                                         thread.i387.fxsave) & 15);
1187                 };
1188
1189                 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1190                 set_in_cr4(X86_CR4_OSFXSR);
1191                 printk("done.\n");
1192         }
1193         if (cpu_has_xmm) {
1194                 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1195                                 "support... ");
1196                 set_in_cr4(X86_CR4_OSXMMEXCPT);
1197                 printk("done.\n");
1198         }
1199
1200         /*
1201          * Should be a barrier for any external CPU state.
1202          */
1203         cpu_init();
1204 }
1205
1206 void smp_trap_init(trap_info_t *trap_ctxt)
1207 {
1208         trap_info_t *t = trap_table;
1209
1210         for (t = trap_table; t->address; t++) {
1211                 trap_ctxt[t->vector].flags = t->flags;
1212                 trap_ctxt[t->vector].cs = t->cs;
1213                 trap_ctxt[t->vector].address = t->address;
1214         }
1215 }
1216
1217 static int __init kstack_setup(char *s)
1218 {
1219         kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1220         return 1;
1221 }
1222 __setup("kstack=", kstack_setup);