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