[PATCH] x86: fix stack trace facility level
[linux-2.6.22.y-op.git] / arch / i386 / kernel / traps.c
blobb85c9e88427dd5f9384cd9da2996191777345da9
1 /*
2 * linux/arch/i386/traps.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 */
11 * 'Traps.c' handles hardware traps and faults after we have saved some
12 * state in 'asm.s'.
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>
32 #ifdef CONFIG_EISA
33 #include <linux/ioport.h>
34 #include <linux/eisa.h>
35 #endif
37 #ifdef CONFIG_MCA
38 #include <linux/mca.h>
39 #endif
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>
51 #include <asm/smp.h>
52 #include <asm/arch_hooks.h>
53 #include <asm/kdebug.h>
55 #include <linux/module.h>
57 #include "mach_traps.h"
59 asmlinkage int system_call(void);
61 struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
62 { 0, 0 }, { 0, 0 } };
64 /* Do we ignore FPU interrupts ? */
65 char ignore_fpu_irq = 0;
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.
72 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
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);
94 static int kstack_depth_to_print = 24;
95 struct notifier_block *i386die_chain;
96 static DEFINE_SPINLOCK(die_notifier_lock);
98 int register_die_notifier(struct notifier_block *nb)
100 int err = 0;
101 unsigned long flags;
102 spin_lock_irqsave(&die_notifier_lock, flags);
103 err = notifier_chain_register(&i386die_chain, nb);
104 spin_unlock_irqrestore(&die_notifier_lock, flags);
105 return err;
107 EXPORT_SYMBOL(register_die_notifier);
109 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
111 return p > (void *)tinfo &&
112 p < (void *)tinfo + THREAD_SIZE - 3;
115 static void print_addr_and_symbol(unsigned long addr, char *log_lvl)
117 printk(log_lvl);
118 printk(" [<%08lx>] ", addr);
119 print_symbol("%s", addr);
120 printk("\n");
123 static inline unsigned long print_context_stack(struct thread_info *tinfo,
124 unsigned long *stack, unsigned long ebp,
125 char *log_lvl)
127 unsigned long addr;
129 #ifdef CONFIG_FRAME_POINTER
130 while (valid_stack_ptr(tinfo, (void *)ebp)) {
131 addr = *(unsigned long *)(ebp + 4);
132 print_addr_and_symbol(addr, log_lvl);
133 ebp = *(unsigned long *)ebp;
135 #else
136 while (valid_stack_ptr(tinfo, stack)) {
137 addr = *stack++;
138 if (__kernel_text_address(addr))
139 print_addr_and_symbol(addr, log_lvl);
141 #endif
142 return ebp;
145 static void show_trace_log_lvl(struct task_struct *task,
146 unsigned long *stack, char *log_lvl)
148 unsigned long ebp;
150 if (!task)
151 task = current;
153 if (task == current) {
154 /* Grab ebp right from our regs */
155 asm ("movl %%ebp, %0" : "=r" (ebp) : );
156 } else {
157 /* ebp is the last reg pushed by switch_to */
158 ebp = *(unsigned long *) task->thread.esp;
161 while (1) {
162 struct thread_info *context;
163 context = (struct thread_info *)
164 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
165 ebp = print_context_stack(context, stack, ebp, log_lvl);
166 stack = (unsigned long*)context->previous_esp;
167 if (!stack)
168 break;
169 printk(log_lvl);
170 printk(" =======================\n");
174 void show_trace(struct task_struct *task, unsigned long * stack)
176 show_trace_log_lvl(task, stack, "");
179 static void show_stack_log_lvl(struct task_struct *task, unsigned long *esp,
180 char *log_lvl)
182 unsigned long *stack;
183 int i;
185 if (esp == NULL) {
186 if (task)
187 esp = (unsigned long*)task->thread.esp;
188 else
189 esp = (unsigned long *)&esp;
192 stack = esp;
193 printk(log_lvl);
194 for(i = 0; i < kstack_depth_to_print; i++) {
195 if (kstack_end(stack))
196 break;
197 if (i && ((i % 8) == 0)) {
198 printk("\n");
199 printk(log_lvl);
200 printk(" ");
202 printk("%08lx ", *stack++);
204 printk("\n");
205 printk(log_lvl);
206 printk("Call Trace:\n");
207 show_trace_log_lvl(task, esp, log_lvl);
210 void show_stack(struct task_struct *task, unsigned long *esp)
212 show_stack_log_lvl(task, esp, "");
216 * The architecture-independent dump_stack generator
218 void dump_stack(void)
220 unsigned long stack;
222 show_trace(current, &stack);
225 EXPORT_SYMBOL(dump_stack);
227 void show_registers(struct pt_regs *regs)
229 int i;
230 int in_kernel = 1;
231 unsigned long esp;
232 unsigned short ss;
234 esp = (unsigned long) (&regs->esp);
235 savesegment(ss, ss);
236 if (user_mode(regs)) {
237 in_kernel = 0;
238 esp = regs->esp;
239 ss = regs->xss & 0xffff;
241 print_modules();
242 printk(KERN_EMERG "CPU: %d\nEIP: %04x:[<%08lx>] %s VLI\n"
243 "EFLAGS: %08lx (%s) \n",
244 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
245 print_tainted(), regs->eflags, system_utsname.release);
246 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
247 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
248 regs->eax, regs->ebx, regs->ecx, regs->edx);
249 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
250 regs->esi, regs->edi, regs->ebp, esp);
251 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
252 regs->xds & 0xffff, regs->xes & 0xffff, ss);
253 printk(KERN_EMERG "Process %s (pid: %d, threadinfo=%p task=%p)",
254 current->comm, current->pid, current_thread_info(), current);
256 * When in-kernel, we also print out the stack and code at the
257 * time of the fault..
259 if (in_kernel) {
260 u8 __user *eip;
262 printk("\n" KERN_EMERG "Stack: ");
263 show_stack_log_lvl(NULL, (unsigned long *)esp, KERN_EMERG);
265 printk(KERN_EMERG "Code: ");
267 eip = (u8 __user *)regs->eip - 43;
268 for (i = 0; i < 64; i++, eip++) {
269 unsigned char c;
271 if (eip < (u8 __user *)PAGE_OFFSET || __get_user(c, eip)) {
272 printk(" Bad EIP value.");
273 break;
275 if (eip == (u8 __user *)regs->eip)
276 printk("<%02x> ", c);
277 else
278 printk("%02x ", c);
281 printk("\n");
284 static void handle_BUG(struct pt_regs *regs)
286 unsigned short ud2;
287 unsigned short line;
288 char *file;
289 char c;
290 unsigned long eip;
292 eip = regs->eip;
294 if (eip < PAGE_OFFSET)
295 goto no_bug;
296 if (__get_user(ud2, (unsigned short __user *)eip))
297 goto no_bug;
298 if (ud2 != 0x0b0f)
299 goto no_bug;
300 if (__get_user(line, (unsigned short __user *)(eip + 2)))
301 goto bug;
302 if (__get_user(file, (char * __user *)(eip + 4)) ||
303 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
304 file = "<bad filename>";
306 printk(KERN_EMERG "------------[ cut here ]------------\n");
307 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
309 no_bug:
310 return;
312 /* Here we know it was a BUG but file-n-line is unavailable */
313 bug:
314 printk(KERN_EMERG "Kernel BUG\n");
317 /* This is gone through when something in the kernel
318 * has done something bad and is about to be terminated.
320 void die(const char * str, struct pt_regs * regs, long err)
322 static struct {
323 spinlock_t lock;
324 u32 lock_owner;
325 int lock_owner_depth;
326 } die = {
327 .lock = SPIN_LOCK_UNLOCKED,
328 .lock_owner = -1,
329 .lock_owner_depth = 0
331 static int die_counter;
332 unsigned long flags;
334 if (die.lock_owner != raw_smp_processor_id()) {
335 console_verbose();
336 spin_lock_irqsave(&die.lock, flags);
337 die.lock_owner = smp_processor_id();
338 die.lock_owner_depth = 0;
339 bust_spinlocks(1);
341 else
342 local_save_flags(flags);
344 if (++die.lock_owner_depth < 3) {
345 int nl = 0;
346 handle_BUG(regs);
347 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
348 #ifdef CONFIG_PREEMPT
349 printk(KERN_EMERG "PREEMPT ");
350 nl = 1;
351 #endif
352 #ifdef CONFIG_SMP
353 if (!nl)
354 printk(KERN_EMERG);
355 printk("SMP ");
356 nl = 1;
357 #endif
358 #ifdef CONFIG_DEBUG_PAGEALLOC
359 if (!nl)
360 printk(KERN_EMERG);
361 printk("DEBUG_PAGEALLOC");
362 nl = 1;
363 #endif
364 if (nl)
365 printk("\n");
366 notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
367 show_registers(regs);
368 } else
369 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
371 bust_spinlocks(0);
372 die.lock_owner = -1;
373 spin_unlock_irqrestore(&die.lock, flags);
375 if (kexec_should_crash(current))
376 crash_kexec(regs);
378 if (in_interrupt())
379 panic("Fatal exception in interrupt");
381 if (panic_on_oops) {
382 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
383 ssleep(5);
384 panic("Fatal exception");
386 do_exit(SIGSEGV);
389 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
391 if (!user_mode_vm(regs))
392 die(str, regs, err);
395 static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
396 struct pt_regs * regs, long error_code,
397 siginfo_t *info)
399 struct task_struct *tsk = current;
400 tsk->thread.error_code = error_code;
401 tsk->thread.trap_no = trapnr;
403 if (regs->eflags & VM_MASK) {
404 if (vm86)
405 goto vm86_trap;
406 goto trap_signal;
409 if (!user_mode(regs))
410 goto kernel_trap;
412 trap_signal: {
413 if (info)
414 force_sig_info(signr, info, tsk);
415 else
416 force_sig(signr, tsk);
417 return;
420 kernel_trap: {
421 if (!fixup_exception(regs))
422 die(str, regs, error_code);
423 return;
426 vm86_trap: {
427 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
428 if (ret) goto trap_signal;
429 return;
433 #define DO_ERROR(trapnr, signr, str, name) \
434 fastcall void do_##name(struct pt_regs * regs, long error_code) \
436 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
437 == NOTIFY_STOP) \
438 return; \
439 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
442 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
443 fastcall void do_##name(struct pt_regs * regs, long error_code) \
445 siginfo_t info; \
446 info.si_signo = signr; \
447 info.si_errno = 0; \
448 info.si_code = sicode; \
449 info.si_addr = (void __user *)siaddr; \
450 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
451 == NOTIFY_STOP) \
452 return; \
453 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
456 #define DO_VM86_ERROR(trapnr, signr, str, name) \
457 fastcall void do_##name(struct pt_regs * regs, long error_code) \
459 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
460 == NOTIFY_STOP) \
461 return; \
462 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
465 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
466 fastcall void do_##name(struct pt_regs * regs, long error_code) \
468 siginfo_t info; \
469 info.si_signo = signr; \
470 info.si_errno = 0; \
471 info.si_code = sicode; \
472 info.si_addr = (void __user *)siaddr; \
473 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
474 == NOTIFY_STOP) \
475 return; \
476 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
479 DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
480 #ifndef CONFIG_KPROBES
481 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
482 #endif
483 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
484 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
485 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
486 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
487 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
488 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
489 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
490 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
491 DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
493 fastcall void __kprobes do_general_protection(struct pt_regs * regs,
494 long error_code)
496 int cpu = get_cpu();
497 struct tss_struct *tss = &per_cpu(init_tss, cpu);
498 struct thread_struct *thread = &current->thread;
501 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
502 * invalid offset set (the LAZY one) and the faulting thread has
503 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
504 * and we set the offset field correctly. Then we let the CPU to
505 * restart the faulting instruction.
507 if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
508 thread->io_bitmap_ptr) {
509 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
510 thread->io_bitmap_max);
512 * If the previously set map was extending to higher ports
513 * than the current one, pad extra space with 0xff (no access).
515 if (thread->io_bitmap_max < tss->io_bitmap_max)
516 memset((char *) tss->io_bitmap +
517 thread->io_bitmap_max, 0xff,
518 tss->io_bitmap_max - thread->io_bitmap_max);
519 tss->io_bitmap_max = thread->io_bitmap_max;
520 tss->io_bitmap_base = IO_BITMAP_OFFSET;
521 tss->io_bitmap_owner = thread;
522 put_cpu();
523 return;
525 put_cpu();
527 current->thread.error_code = error_code;
528 current->thread.trap_no = 13;
530 if (regs->eflags & VM_MASK)
531 goto gp_in_vm86;
533 if (!user_mode(regs))
534 goto gp_in_kernel;
536 current->thread.error_code = error_code;
537 current->thread.trap_no = 13;
538 force_sig(SIGSEGV, current);
539 return;
541 gp_in_vm86:
542 local_irq_enable();
543 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
544 return;
546 gp_in_kernel:
547 if (!fixup_exception(regs)) {
548 if (notify_die(DIE_GPF, "general protection fault", regs,
549 error_code, 13, SIGSEGV) == NOTIFY_STOP)
550 return;
551 die("general protection fault", regs, error_code);
555 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
557 printk(KERN_EMERG "Uhhuh. NMI received. Dazed and confused, but trying "
558 "to continue\n");
559 printk(KERN_EMERG "You probably have a hardware problem with your RAM "
560 "chips\n");
562 /* Clear and disable the memory parity error line. */
563 clear_mem_error(reason);
566 static void io_check_error(unsigned char reason, struct pt_regs * regs)
568 unsigned long i;
570 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
571 show_registers(regs);
573 /* Re-enable the IOCK line, wait for a few seconds */
574 reason = (reason & 0xf) | 8;
575 outb(reason, 0x61);
576 i = 2000;
577 while (--i) udelay(1000);
578 reason &= ~8;
579 outb(reason, 0x61);
582 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
584 #ifdef CONFIG_MCA
585 /* Might actually be able to figure out what the guilty party
586 * is. */
587 if( MCA_bus ) {
588 mca_handle_nmi();
589 return;
591 #endif
592 printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
593 reason, smp_processor_id());
594 printk("Dazed and confused, but trying to continue\n");
595 printk("Do you have a strange power saving mode enabled?\n");
598 static DEFINE_SPINLOCK(nmi_print_lock);
600 void die_nmi (struct pt_regs *regs, const char *msg)
602 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 0, SIGINT) ==
603 NOTIFY_STOP)
604 return;
606 spin_lock(&nmi_print_lock);
608 * We are in trouble anyway, lets at least try
609 * to get a message out.
611 bust_spinlocks(1);
612 printk(KERN_EMERG "%s", msg);
613 printk(" on CPU%d, eip %08lx, registers:\n",
614 smp_processor_id(), regs->eip);
615 show_registers(regs);
616 printk(KERN_EMERG "console shuts up ...\n");
617 console_silent();
618 spin_unlock(&nmi_print_lock);
619 bust_spinlocks(0);
621 /* If we are in kernel we are probably nested up pretty bad
622 * and might aswell get out now while we still can.
624 if (!user_mode(regs)) {
625 current->thread.trap_no = 2;
626 crash_kexec(regs);
629 do_exit(SIGSEGV);
632 static void default_do_nmi(struct pt_regs * regs)
634 unsigned char reason = 0;
636 /* Only the BSP gets external NMIs from the system. */
637 if (!smp_processor_id())
638 reason = get_nmi_reason();
640 if (!(reason & 0xc0)) {
641 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT)
642 == NOTIFY_STOP)
643 return;
644 #ifdef CONFIG_X86_LOCAL_APIC
646 * Ok, so this is none of the documented NMI sources,
647 * so it must be the NMI watchdog.
649 if (nmi_watchdog) {
650 nmi_watchdog_tick(regs);
651 return;
653 #endif
654 unknown_nmi_error(reason, regs);
655 return;
657 if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_STOP)
658 return;
659 if (reason & 0x80)
660 mem_parity_error(reason, regs);
661 if (reason & 0x40)
662 io_check_error(reason, regs);
664 * Reassert NMI in case it became active meanwhile
665 * as it's edge-triggered.
667 reassert_nmi();
670 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
672 return 0;
675 static nmi_callback_t nmi_callback = dummy_nmi_callback;
677 fastcall void do_nmi(struct pt_regs * regs, long error_code)
679 int cpu;
681 nmi_enter();
683 cpu = smp_processor_id();
685 ++nmi_count(cpu);
687 if (!rcu_dereference(nmi_callback)(regs, cpu))
688 default_do_nmi(regs);
690 nmi_exit();
693 void set_nmi_callback(nmi_callback_t callback)
695 rcu_assign_pointer(nmi_callback, callback);
697 EXPORT_SYMBOL_GPL(set_nmi_callback);
699 void unset_nmi_callback(void)
701 nmi_callback = dummy_nmi_callback;
703 EXPORT_SYMBOL_GPL(unset_nmi_callback);
705 #ifdef CONFIG_KPROBES
706 fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
708 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
709 == NOTIFY_STOP)
710 return;
711 /* This is an interrupt gate, because kprobes wants interrupts
712 disabled. Normal trap handlers don't. */
713 restore_interrupts(regs);
714 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
716 #endif
719 * Our handling of the processor debug registers is non-trivial.
720 * We do not clear them on entry and exit from the kernel. Therefore
721 * it is possible to get a watchpoint trap here from inside the kernel.
722 * However, the code in ./ptrace.c has ensured that the user can
723 * only set watchpoints on userspace addresses. Therefore the in-kernel
724 * watchpoint trap can only occur in code which is reading/writing
725 * from user space. Such code must not hold kernel locks (since it
726 * can equally take a page fault), therefore it is safe to call
727 * force_sig_info even though that claims and releases locks.
729 * Code in ./signal.c ensures that the debug control register
730 * is restored before we deliver any signal, and therefore that
731 * user code runs with the correct debug control register even though
732 * we clear it here.
734 * Being careful here means that we don't have to be as careful in a
735 * lot of more complicated places (task switching can be a bit lazy
736 * about restoring all the debug state, and ptrace doesn't have to
737 * find every occurrence of the TF bit that could be saved away even
738 * by user code)
740 fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
742 unsigned int condition;
743 struct task_struct *tsk = current;
745 get_debugreg(condition, 6);
747 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
748 SIGTRAP) == NOTIFY_STOP)
749 return;
750 /* It's safe to allow irq's after DR6 has been saved */
751 if (regs->eflags & X86_EFLAGS_IF)
752 local_irq_enable();
754 /* Mask out spurious debug traps due to lazy DR7 setting */
755 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
756 if (!tsk->thread.debugreg[7])
757 goto clear_dr7;
760 if (regs->eflags & VM_MASK)
761 goto debug_vm86;
763 /* Save debug status register where ptrace can see it */
764 tsk->thread.debugreg[6] = condition;
767 * Single-stepping through TF: make sure we ignore any events in
768 * kernel space (but re-enable TF when returning to user mode).
770 if (condition & DR_STEP) {
772 * We already checked v86 mode above, so we can
773 * check for kernel mode by just checking the CPL
774 * of CS.
776 if (!user_mode(regs))
777 goto clear_TF_reenable;
780 /* Ok, finally something we can handle */
781 send_sigtrap(tsk, regs, error_code);
783 /* Disable additional traps. They'll be re-enabled when
784 * the signal is delivered.
786 clear_dr7:
787 set_debugreg(0, 7);
788 return;
790 debug_vm86:
791 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
792 return;
794 clear_TF_reenable:
795 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
796 regs->eflags &= ~TF_MASK;
797 return;
801 * Note that we play around with the 'TS' bit in an attempt to get
802 * the correct behaviour even in the presence of the asynchronous
803 * IRQ13 behaviour
805 void math_error(void __user *eip)
807 struct task_struct * task;
808 siginfo_t info;
809 unsigned short cwd, swd;
812 * Save the info for the exception handler and clear the error.
814 task = current;
815 save_init_fpu(task);
816 task->thread.trap_no = 16;
817 task->thread.error_code = 0;
818 info.si_signo = SIGFPE;
819 info.si_errno = 0;
820 info.si_code = __SI_FAULT;
821 info.si_addr = eip;
823 * (~cwd & swd) will mask out exceptions that are not set to unmasked
824 * status. 0x3f is the exception bits in these regs, 0x200 is the
825 * C1 reg you need in case of a stack fault, 0x040 is the stack
826 * fault bit. We should only be taking one exception at a time,
827 * so if this combination doesn't produce any single exception,
828 * then we have a bad program that isn't syncronizing its FPU usage
829 * and it will suffer the consequences since we won't be able to
830 * fully reproduce the context of the exception
832 cwd = get_fpu_cwd(task);
833 swd = get_fpu_swd(task);
834 switch (swd & ~cwd & 0x3f) {
835 case 0x000: /* No unmasked exception */
836 return;
837 default: /* Multiple exceptions */
838 break;
839 case 0x001: /* Invalid Op */
841 * swd & 0x240 == 0x040: Stack Underflow
842 * swd & 0x240 == 0x240: Stack Overflow
843 * User must clear the SF bit (0x40) if set
845 info.si_code = FPE_FLTINV;
846 break;
847 case 0x002: /* Denormalize */
848 case 0x010: /* Underflow */
849 info.si_code = FPE_FLTUND;
850 break;
851 case 0x004: /* Zero Divide */
852 info.si_code = FPE_FLTDIV;
853 break;
854 case 0x008: /* Overflow */
855 info.si_code = FPE_FLTOVF;
856 break;
857 case 0x020: /* Precision */
858 info.si_code = FPE_FLTRES;
859 break;
861 force_sig_info(SIGFPE, &info, task);
864 fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
866 ignore_fpu_irq = 1;
867 math_error((void __user *)regs->eip);
870 static void simd_math_error(void __user *eip)
872 struct task_struct * task;
873 siginfo_t info;
874 unsigned short mxcsr;
877 * Save the info for the exception handler and clear the error.
879 task = current;
880 save_init_fpu(task);
881 task->thread.trap_no = 19;
882 task->thread.error_code = 0;
883 info.si_signo = SIGFPE;
884 info.si_errno = 0;
885 info.si_code = __SI_FAULT;
886 info.si_addr = eip;
888 * The SIMD FPU exceptions are handled a little differently, as there
889 * is only a single status/control register. Thus, to determine which
890 * unmasked exception was caught we must mask the exception mask bits
891 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
893 mxcsr = get_fpu_mxcsr(task);
894 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
895 case 0x000:
896 default:
897 break;
898 case 0x001: /* Invalid Op */
899 info.si_code = FPE_FLTINV;
900 break;
901 case 0x002: /* Denormalize */
902 case 0x010: /* Underflow */
903 info.si_code = FPE_FLTUND;
904 break;
905 case 0x004: /* Zero Divide */
906 info.si_code = FPE_FLTDIV;
907 break;
908 case 0x008: /* Overflow */
909 info.si_code = FPE_FLTOVF;
910 break;
911 case 0x020: /* Precision */
912 info.si_code = FPE_FLTRES;
913 break;
915 force_sig_info(SIGFPE, &info, task);
918 fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
919 long error_code)
921 if (cpu_has_xmm) {
922 /* Handle SIMD FPU exceptions on PIII+ processors. */
923 ignore_fpu_irq = 1;
924 simd_math_error((void __user *)regs->eip);
925 } else {
927 * Handle strange cache flush from user space exception
928 * in all other cases. This is undocumented behaviour.
930 if (regs->eflags & VM_MASK) {
931 handle_vm86_fault((struct kernel_vm86_regs *)regs,
932 error_code);
933 return;
935 current->thread.trap_no = 19;
936 current->thread.error_code = error_code;
937 die_if_kernel("cache flush denied", regs, error_code);
938 force_sig(SIGSEGV, current);
942 fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
943 long error_code)
945 #if 0
946 /* No need to warn about this any longer. */
947 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
948 #endif
951 fastcall void setup_x86_bogus_stack(unsigned char * stk)
953 unsigned long *switch16_ptr, *switch32_ptr;
954 struct pt_regs *regs;
955 unsigned long stack_top, stack_bot;
956 unsigned short iret_frame16_off;
957 int cpu = smp_processor_id();
958 /* reserve the space on 32bit stack for the magic switch16 pointer */
959 memmove(stk, stk + 8, sizeof(struct pt_regs));
960 switch16_ptr = (unsigned long *)(stk + sizeof(struct pt_regs));
961 regs = (struct pt_regs *)stk;
962 /* now the switch32 on 16bit stack */
963 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
964 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
965 switch32_ptr = (unsigned long *)(stack_top - 8);
966 iret_frame16_off = CPU_16BIT_STACK_SIZE - 8 - 20;
967 /* copy iret frame on 16bit stack */
968 memcpy((void *)(stack_bot + iret_frame16_off), &regs->eip, 20);
969 /* fill in the switch pointers */
970 switch16_ptr[0] = (regs->esp & 0xffff0000) | iret_frame16_off;
971 switch16_ptr[1] = __ESPFIX_SS;
972 switch32_ptr[0] = (unsigned long)stk + sizeof(struct pt_regs) +
973 8 - CPU_16BIT_STACK_SIZE;
974 switch32_ptr[1] = __KERNEL_DS;
977 fastcall unsigned char * fixup_x86_bogus_stack(unsigned short sp)
979 unsigned long *switch32_ptr;
980 unsigned char *stack16, *stack32;
981 unsigned long stack_top, stack_bot;
982 int len;
983 int cpu = smp_processor_id();
984 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
985 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
986 switch32_ptr = (unsigned long *)(stack_top - 8);
987 /* copy the data from 16bit stack to 32bit stack */
988 len = CPU_16BIT_STACK_SIZE - 8 - sp;
989 stack16 = (unsigned char *)(stack_bot + sp);
990 stack32 = (unsigned char *)
991 (switch32_ptr[0] + CPU_16BIT_STACK_SIZE - 8 - len);
992 memcpy(stack32, stack16, len);
993 return stack32;
997 * 'math_state_restore()' saves the current math information in the
998 * old math state array, and gets the new ones from the current task
1000 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1001 * Don't touch unless you *really* know how it works.
1003 * Must be called with kernel preemption disabled (in this case,
1004 * local interrupts are disabled at the call-site in entry.S).
1006 asmlinkage void math_state_restore(struct pt_regs regs)
1008 struct thread_info *thread = current_thread_info();
1009 struct task_struct *tsk = thread->task;
1011 clts(); /* Allow maths ops (or we recurse) */
1012 if (!tsk_used_math(tsk))
1013 init_fpu(tsk);
1014 restore_fpu(tsk);
1015 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
1018 #ifndef CONFIG_MATH_EMULATION
1020 asmlinkage void math_emulate(long arg)
1022 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1023 printk(KERN_EMERG "killing %s.\n",current->comm);
1024 force_sig(SIGFPE,current);
1025 schedule();
1028 #endif /* CONFIG_MATH_EMULATION */
1030 #ifdef CONFIG_X86_F00F_BUG
1031 void __init trap_init_f00f_bug(void)
1033 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1036 * Update the IDT descriptor and reload the IDT so that
1037 * it uses the read-only mapped virtual address.
1039 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
1040 load_idt(&idt_descr);
1042 #endif
1044 #define _set_gate(gate_addr,type,dpl,addr,seg) \
1045 do { \
1046 int __d0, __d1; \
1047 __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
1048 "movw %4,%%dx\n\t" \
1049 "movl %%eax,%0\n\t" \
1050 "movl %%edx,%1" \
1051 :"=m" (*((long *) (gate_addr))), \
1052 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
1053 :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
1054 "3" ((char *) (addr)),"2" ((seg) << 16)); \
1055 } while (0)
1059 * This needs to use 'idt_table' rather than 'idt', and
1060 * thus use the _nonmapped_ version of the IDT, as the
1061 * Pentium F0 0F bugfix can have resulted in the mapped
1062 * IDT being write-protected.
1064 void set_intr_gate(unsigned int n, void *addr)
1066 _set_gate(idt_table+n,14,0,addr,__KERNEL_CS);
1070 * This routine sets up an interrupt gate at directory privilege level 3.
1072 static inline void set_system_intr_gate(unsigned int n, void *addr)
1074 _set_gate(idt_table+n, 14, 3, addr, __KERNEL_CS);
1077 static void __init set_trap_gate(unsigned int n, void *addr)
1079 _set_gate(idt_table+n,15,0,addr,__KERNEL_CS);
1082 static void __init set_system_gate(unsigned int n, void *addr)
1084 _set_gate(idt_table+n,15,3,addr,__KERNEL_CS);
1087 static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
1089 _set_gate(idt_table+n,5,0,0,(gdt_entry<<3));
1093 void __init trap_init(void)
1095 #ifdef CONFIG_EISA
1096 void __iomem *p = ioremap(0x0FFFD9, 4);
1097 if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
1098 EISA_bus = 1;
1100 iounmap(p);
1101 #endif
1103 #ifdef CONFIG_X86_LOCAL_APIC
1104 init_apic_mappings();
1105 #endif
1107 set_trap_gate(0,&divide_error);
1108 set_intr_gate(1,&debug);
1109 set_intr_gate(2,&nmi);
1110 set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
1111 set_system_gate(4,&overflow);
1112 set_trap_gate(5,&bounds);
1113 set_trap_gate(6,&invalid_op);
1114 set_trap_gate(7,&device_not_available);
1115 set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1116 set_trap_gate(9,&coprocessor_segment_overrun);
1117 set_trap_gate(10,&invalid_TSS);
1118 set_trap_gate(11,&segment_not_present);
1119 set_trap_gate(12,&stack_segment);
1120 set_trap_gate(13,&general_protection);
1121 set_intr_gate(14,&page_fault);
1122 set_trap_gate(15,&spurious_interrupt_bug);
1123 set_trap_gate(16,&coprocessor_error);
1124 set_trap_gate(17,&alignment_check);
1125 #ifdef CONFIG_X86_MCE
1126 set_trap_gate(18,&machine_check);
1127 #endif
1128 set_trap_gate(19,&simd_coprocessor_error);
1130 if (cpu_has_fxsr) {
1132 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1133 * Generates a compile-time "error: zero width for bit-field" if
1134 * the alignment is wrong.
1136 struct fxsrAlignAssert {
1137 int _:!(offsetof(struct task_struct,
1138 thread.i387.fxsave) & 15);
1141 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1142 set_in_cr4(X86_CR4_OSFXSR);
1143 printk("done.\n");
1145 if (cpu_has_xmm) {
1146 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1147 "support... ");
1148 set_in_cr4(X86_CR4_OSXMMEXCPT);
1149 printk("done.\n");
1152 set_system_gate(SYSCALL_VECTOR,&system_call);
1155 * Should be a barrier for any external CPU state.
1157 cpu_init();
1159 trap_init_hook();
1162 static int __init kstack_setup(char *s)
1164 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1165 return 0;
1167 __setup("kstack=", kstack_setup);