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