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