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