ia64/xen-unstable

view linux-2.6-xen-sparse/arch/x86_64/kernel/traps-xen.c @ 13647:057c4c2991fa

32-on-64: Fix an one-off error in compat_addr_ok().
Signed-off-by: Jan Beulich <jbeulich@novell.com>
author kaf24@localhost.localdomain
date Fri Jan 26 13:36:52 2007 +0000 (2007-01-26)
parents 3adf00179a63
children
line source
1 /*
2 * linux/arch/x86-64/traps.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
6 *
7 * Pentium III FXSR, SSE support
8 * Gareth Hughes <gareth@valinux.com>, May 2000
9 */
11 /*
12 * 'Traps.c' handles hardware traps and faults after we have saved some
13 * state in 'entry.S'.
14 */
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/timer.h>
21 #include <linux/mm.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/nmi.h>
29 #include <linux/kprobes.h>
30 #include <linux/kexec.h>
31 #include <linux/unwind.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35 #include <asm/io.h>
36 #include <asm/atomic.h>
37 #include <asm/debugreg.h>
38 #include <asm/desc.h>
39 #include <asm/i387.h>
40 #include <asm/kdebug.h>
41 #include <asm/processor.h>
42 #include <asm/unwind.h>
43 #include <asm/smp.h>
44 #include <asm/pgalloc.h>
45 #include <asm/pda.h>
46 #include <asm/proto.h>
47 #include <asm/nmi.h>
49 asmlinkage void divide_error(void);
50 asmlinkage void debug(void);
51 asmlinkage void nmi(void);
52 asmlinkage void int3(void);
53 asmlinkage void overflow(void);
54 asmlinkage void bounds(void);
55 asmlinkage void invalid_op(void);
56 asmlinkage void device_not_available(void);
57 asmlinkage void double_fault(void);
58 asmlinkage void coprocessor_segment_overrun(void);
59 asmlinkage void invalid_TSS(void);
60 asmlinkage void segment_not_present(void);
61 asmlinkage void stack_segment(void);
62 asmlinkage void general_protection(void);
63 asmlinkage void page_fault(void);
64 asmlinkage void coprocessor_error(void);
65 asmlinkage void simd_coprocessor_error(void);
66 asmlinkage void reserved(void);
67 asmlinkage void alignment_check(void);
68 asmlinkage void machine_check(void);
69 asmlinkage void spurious_interrupt_bug(void);
71 ATOMIC_NOTIFIER_HEAD(die_chain);
72 EXPORT_SYMBOL(die_chain);
74 int register_die_notifier(struct notifier_block *nb)
75 {
76 vmalloc_sync_all();
77 return atomic_notifier_chain_register(&die_chain, nb);
78 }
79 EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
81 int unregister_die_notifier(struct notifier_block *nb)
82 {
83 return atomic_notifier_chain_unregister(&die_chain, nb);
84 }
85 EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
87 static inline void conditional_sti(struct pt_regs *regs)
88 {
89 if (regs->eflags & X86_EFLAGS_IF)
90 local_irq_enable();
91 }
93 static inline void preempt_conditional_sti(struct pt_regs *regs)
94 {
95 preempt_disable();
96 if (regs->eflags & X86_EFLAGS_IF)
97 local_irq_enable();
98 }
100 static inline void preempt_conditional_cli(struct pt_regs *regs)
101 {
102 if (regs->eflags & X86_EFLAGS_IF)
103 local_irq_disable();
104 /* Make sure to not schedule here because we could be running
105 on an exception stack. */
106 preempt_enable_no_resched();
107 }
109 static int kstack_depth_to_print = 12;
110 #ifdef CONFIG_STACK_UNWIND
111 static int call_trace = 1;
112 #else
113 #define call_trace (-1)
114 #endif
116 #ifdef CONFIG_KALLSYMS
117 # include <linux/kallsyms.h>
118 void printk_address(unsigned long address)
119 {
120 unsigned long offset = 0, symsize;
121 const char *symname;
122 char *modname;
123 char *delim = ":";
124 char namebuf[128];
126 symname = kallsyms_lookup(address, &symsize, &offset,
127 &modname, namebuf);
128 if (!symname) {
129 printk(" [<%016lx>]\n", address);
130 return;
131 }
132 if (!modname)
133 modname = delim = "";
134 printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
135 address, delim, modname, delim, symname, offset, symsize);
136 }
137 #else
138 void printk_address(unsigned long address)
139 {
140 printk(" [<%016lx>]\n", address);
141 }
142 #endif
144 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
145 unsigned *usedp, const char **idp)
146 {
147 #ifndef CONFIG_X86_NO_TSS
148 static char ids[][8] = {
149 [DEBUG_STACK - 1] = "#DB",
150 [NMI_STACK - 1] = "NMI",
151 [DOUBLEFAULT_STACK - 1] = "#DF",
152 [STACKFAULT_STACK - 1] = "#SS",
153 [MCE_STACK - 1] = "#MC",
154 #if DEBUG_STKSZ > EXCEPTION_STKSZ
155 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
156 #endif
157 };
158 unsigned k;
160 /*
161 * Iterate over all exception stacks, and figure out whether
162 * 'stack' is in one of them:
163 */
164 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
165 unsigned long end;
167 /*
168 * set 'end' to the end of the exception stack.
169 */
170 switch (k + 1) {
171 /*
172 * TODO: this block is not needed i think, because
173 * setup64.c:cpu_init() sets up t->ist[DEBUG_STACK]
174 * properly too.
175 */
176 #if DEBUG_STKSZ > EXCEPTION_STKSZ
177 case DEBUG_STACK:
178 end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ;
179 break;
180 #endif
181 default:
182 end = per_cpu(orig_ist, cpu).ist[k];
183 break;
184 }
185 /*
186 * Is 'stack' above this exception frame's end?
187 * If yes then skip to the next frame.
188 */
189 if (stack >= end)
190 continue;
191 /*
192 * Is 'stack' above this exception frame's start address?
193 * If yes then we found the right frame.
194 */
195 if (stack >= end - EXCEPTION_STKSZ) {
196 /*
197 * Make sure we only iterate through an exception
198 * stack once. If it comes up for the second time
199 * then there's something wrong going on - just
200 * break out and return NULL:
201 */
202 if (*usedp & (1U << k))
203 break;
204 *usedp |= 1U << k;
205 *idp = ids[k];
206 return (unsigned long *)end;
207 }
208 /*
209 * If this is a debug stack, and if it has a larger size than
210 * the usual exception stacks, then 'stack' might still
211 * be within the lower portion of the debug stack:
212 */
213 #if DEBUG_STKSZ > EXCEPTION_STKSZ
214 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
215 unsigned j = N_EXCEPTION_STACKS - 1;
217 /*
218 * Black magic. A large debug stack is composed of
219 * multiple exception stack entries, which we
220 * iterate through now. Dont look:
221 */
222 do {
223 ++j;
224 end -= EXCEPTION_STKSZ;
225 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
226 } while (stack < end - EXCEPTION_STKSZ);
227 if (*usedp & (1U << j))
228 break;
229 *usedp |= 1U << j;
230 *idp = ids[j];
231 return (unsigned long *)end;
232 }
233 #endif
234 }
235 #endif
236 return NULL;
237 }
239 static int show_trace_unwind(struct unwind_frame_info *info, void *context)
240 {
241 int n = 0;
243 while (unwind(info) == 0 && UNW_PC(info)) {
244 n++;
245 printk_address(UNW_PC(info));
246 if (arch_unw_user_mode(info))
247 break;
248 }
249 return n;
250 }
252 /*
253 * x86-64 can have upto three kernel stacks:
254 * process stack
255 * interrupt stack
256 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
257 */
259 void show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long * stack)
260 {
261 const unsigned cpu = safe_smp_processor_id();
262 unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
263 unsigned used = 0;
265 printk("\nCall Trace:\n");
267 if (!tsk)
268 tsk = current;
270 if (call_trace >= 0) {
271 int unw_ret = 0;
272 struct unwind_frame_info info;
274 if (regs) {
275 if (unwind_init_frame_info(&info, tsk, regs) == 0)
276 unw_ret = show_trace_unwind(&info, NULL);
277 } else if (tsk == current)
278 unw_ret = unwind_init_running(&info, show_trace_unwind, NULL);
279 else {
280 if (unwind_init_blocked(&info, tsk) == 0)
281 unw_ret = show_trace_unwind(&info, NULL);
282 }
283 if (unw_ret > 0) {
284 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
285 print_symbol("DWARF2 unwinder stuck at %s\n",
286 UNW_PC(&info));
287 if ((long)UNW_SP(&info) < 0) {
288 printk("Leftover inexact backtrace:\n");
289 stack = (unsigned long *)UNW_SP(&info);
290 } else
291 printk("Full inexact backtrace again:\n");
292 } else if (call_trace >= 1)
293 return;
294 else
295 printk("Full inexact backtrace again:\n");
296 } else
297 printk("Inexact backtrace:\n");
298 }
300 /*
301 * Print function call entries within a stack. 'cond' is the
302 * "end of stackframe" condition, that the 'stack++'
303 * iteration will eventually trigger.
304 */
305 #define HANDLE_STACK(cond) \
306 do while (cond) { \
307 unsigned long addr = *stack++; \
308 if (kernel_text_address(addr)) { \
309 /* \
310 * If the address is either in the text segment of the \
311 * kernel, or in the region which contains vmalloc'ed \
312 * memory, it *may* be the address of a calling \
313 * routine; if so, print it so that someone tracing \
314 * down the cause of the crash will be able to figure \
315 * out the call path that was taken. \
316 */ \
317 printk_address(addr); \
318 } \
319 } while (0)
321 /*
322 * Print function call entries in all stacks, starting at the
323 * current stack address. If the stacks consist of nested
324 * exceptions
325 */
326 for ( ; ; ) {
327 const char *id;
328 unsigned long *estack_end;
329 estack_end = in_exception_stack(cpu, (unsigned long)stack,
330 &used, &id);
332 if (estack_end) {
333 printk(" <%s>", id);
334 HANDLE_STACK (stack < estack_end);
335 printk(" <EOE>");
336 /*
337 * We link to the next stack via the
338 * second-to-last pointer (index -2 to end) in the
339 * exception stack:
340 */
341 stack = (unsigned long *) estack_end[-2];
342 continue;
343 }
344 if (irqstack_end) {
345 unsigned long *irqstack;
346 irqstack = irqstack_end -
347 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
349 if (stack >= irqstack && stack < irqstack_end) {
350 printk(" <IRQ>");
351 HANDLE_STACK (stack < irqstack_end);
352 /*
353 * We link to the next stack (which would be
354 * the process stack normally) the last
355 * pointer (index -1 to end) in the IRQ stack:
356 */
357 stack = (unsigned long *) (irqstack_end[-1]);
358 irqstack_end = NULL;
359 printk(" <EOI>");
360 continue;
361 }
362 }
363 break;
364 }
366 /*
367 * This prints the process stack:
368 */
369 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
370 #undef HANDLE_STACK
372 printk("\n");
373 }
375 static void _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long * rsp)
376 {
377 unsigned long *stack;
378 int i;
379 const int cpu = safe_smp_processor_id();
380 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
381 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
383 // debugging aid: "show_stack(NULL, NULL);" prints the
384 // back trace for this cpu.
386 if (rsp == NULL) {
387 if (tsk)
388 rsp = (unsigned long *)tsk->thread.rsp;
389 else
390 rsp = (unsigned long *)&rsp;
391 }
393 stack = rsp;
394 for(i=0; i < kstack_depth_to_print; i++) {
395 if (stack >= irqstack && stack <= irqstack_end) {
396 if (stack == irqstack_end) {
397 stack = (unsigned long *) (irqstack_end[-1]);
398 printk(" <EOI> ");
399 }
400 } else {
401 if (((long) stack & (THREAD_SIZE-1)) == 0)
402 break;
403 }
404 if (i && ((i % 4) == 0))
405 printk("\n");
406 printk(" %016lx", *stack++);
407 touch_nmi_watchdog();
408 }
409 show_trace(tsk, regs, rsp);
410 }
412 void show_stack(struct task_struct *tsk, unsigned long * rsp)
413 {
414 _show_stack(tsk, NULL, rsp);
415 }
417 /*
418 * The architecture-independent dump_stack generator
419 */
420 void dump_stack(void)
421 {
422 unsigned long dummy;
423 show_trace(NULL, NULL, &dummy);
424 }
426 EXPORT_SYMBOL(dump_stack);
428 void show_registers(struct pt_regs *regs)
429 {
430 int i;
431 int in_kernel = !user_mode(regs);
432 unsigned long rsp;
433 const int cpu = safe_smp_processor_id();
434 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
436 rsp = regs->rsp;
438 printk("CPU %d ", cpu);
439 __show_regs(regs);
440 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
441 cur->comm, cur->pid, task_thread_info(cur), cur);
443 /*
444 * When in-kernel, we also print out the stack and code at the
445 * time of the fault..
446 */
447 if (in_kernel) {
449 printk("Stack: ");
450 _show_stack(NULL, regs, (unsigned long*)rsp);
452 printk("\nCode: ");
453 if (regs->rip < PAGE_OFFSET)
454 goto bad;
456 for (i=0; i<20; i++) {
457 unsigned char c;
458 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
459 bad:
460 printk(" Bad RIP value.");
461 break;
462 }
463 printk("%02x ", c);
464 }
465 }
466 printk("\n");
467 }
469 void handle_BUG(struct pt_regs *regs)
470 {
471 struct bug_frame f;
472 long len;
473 const char *prefix = "";
475 if (user_mode(regs))
476 return;
477 if (__copy_from_user(&f, (const void __user *) regs->rip,
478 sizeof(struct bug_frame)))
479 return;
480 if (f.filename >= 0 ||
481 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
482 return;
483 len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1;
484 if (len < 0 || len >= PATH_MAX)
485 f.filename = (int)(long)"unmapped filename";
486 else if (len > 50) {
487 f.filename += len - 50;
488 prefix = "...";
489 }
490 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
491 printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line);
492 }
494 #ifdef CONFIG_BUG
495 void out_of_line_bug(void)
496 {
497 BUG();
498 }
499 EXPORT_SYMBOL(out_of_line_bug);
500 #endif
502 static DEFINE_SPINLOCK(die_lock);
503 static int die_owner = -1;
504 static unsigned int die_nest_count;
506 unsigned __kprobes long oops_begin(void)
507 {
508 int cpu = safe_smp_processor_id();
509 unsigned long flags;
511 /* racy, but better than risking deadlock. */
512 local_irq_save(flags);
513 if (!spin_trylock(&die_lock)) {
514 if (cpu == die_owner)
515 /* nested oops. should stop eventually */;
516 else
517 spin_lock(&die_lock);
518 }
519 die_nest_count++;
520 die_owner = cpu;
521 console_verbose();
522 bust_spinlocks(1);
523 return flags;
524 }
526 void __kprobes oops_end(unsigned long flags)
527 {
528 die_owner = -1;
529 bust_spinlocks(0);
530 die_nest_count--;
531 if (die_nest_count)
532 /* We still own the lock */
533 local_irq_restore(flags);
534 else
535 /* Nest count reaches zero, release the lock. */
536 spin_unlock_irqrestore(&die_lock, flags);
537 if (panic_on_oops)
538 panic("Fatal exception");
539 }
541 void __kprobes __die(const char * str, struct pt_regs * regs, long err)
542 {
543 static int die_counter;
544 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
545 #ifdef CONFIG_PREEMPT
546 printk("PREEMPT ");
547 #endif
548 #ifdef CONFIG_SMP
549 printk("SMP ");
550 #endif
551 #ifdef CONFIG_DEBUG_PAGEALLOC
552 printk("DEBUG_PAGEALLOC");
553 #endif
554 printk("\n");
555 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
556 show_registers(regs);
557 /* Executive summary in case the oops scrolled away */
558 printk(KERN_ALERT "RIP ");
559 printk_address(regs->rip);
560 printk(" RSP <%016lx>\n", regs->rsp);
561 if (kexec_should_crash(current))
562 crash_kexec(regs);
563 }
565 void die(const char * str, struct pt_regs * regs, long err)
566 {
567 unsigned long flags = oops_begin();
569 handle_BUG(regs);
570 __die(str, regs, err);
571 oops_end(flags);
572 do_exit(SIGSEGV);
573 }
575 #ifdef CONFIG_X86_LOCAL_APIC
576 void __kprobes die_nmi(char *str, struct pt_regs *regs)
577 {
578 unsigned long flags = oops_begin();
580 /*
581 * We are in trouble anyway, lets at least try
582 * to get a message out.
583 */
584 printk(str, safe_smp_processor_id());
585 show_registers(regs);
586 if (kexec_should_crash(current))
587 crash_kexec(regs);
588 if (panic_on_timeout || panic_on_oops)
589 panic("nmi watchdog");
590 printk("console shuts up ...\n");
591 oops_end(flags);
592 nmi_exit();
593 local_irq_enable();
594 do_exit(SIGSEGV);
595 }
596 #endif
598 static void __kprobes do_trap(int trapnr, int signr, char *str,
599 struct pt_regs * regs, long error_code,
600 siginfo_t *info)
601 {
602 struct task_struct *tsk = current;
604 tsk->thread.error_code = error_code;
605 tsk->thread.trap_no = trapnr;
607 if (user_mode(regs)) {
608 if (exception_trace && unhandled_signal(tsk, signr))
609 printk(KERN_INFO
610 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
611 tsk->comm, tsk->pid, str,
612 regs->rip, regs->rsp, error_code);
614 if (info)
615 force_sig_info(signr, info, tsk);
616 else
617 force_sig(signr, tsk);
618 return;
619 }
622 /* kernel trap */
623 {
624 const struct exception_table_entry *fixup;
625 fixup = search_exception_tables(regs->rip);
626 if (fixup)
627 regs->rip = fixup->fixup;
628 else
629 die(str, regs, error_code);
630 return;
631 }
632 }
634 #define DO_ERROR(trapnr, signr, str, name) \
635 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
636 { \
637 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
638 == NOTIFY_STOP) \
639 return; \
640 conditional_sti(regs); \
641 do_trap(trapnr, signr, str, regs, error_code, NULL); \
642 }
644 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
645 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
646 { \
647 siginfo_t info; \
648 info.si_signo = signr; \
649 info.si_errno = 0; \
650 info.si_code = sicode; \
651 info.si_addr = (void __user *)siaddr; \
652 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
653 == NOTIFY_STOP) \
654 return; \
655 conditional_sti(regs); \
656 do_trap(trapnr, signr, str, regs, error_code, &info); \
657 }
659 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
660 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
661 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
662 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
663 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
664 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
665 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
666 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
667 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
668 DO_ERROR(18, SIGSEGV, "reserved", reserved)
670 /* Runs on IST stack */
671 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
672 {
673 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
674 12, SIGBUS) == NOTIFY_STOP)
675 return;
676 preempt_conditional_sti(regs);
677 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
678 preempt_conditional_cli(regs);
679 }
681 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
682 {
683 static const char str[] = "double fault";
684 struct task_struct *tsk = current;
686 /* Return not checked because double check cannot be ignored */
687 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
689 tsk->thread.error_code = error_code;
690 tsk->thread.trap_no = 8;
692 /* This is always a kernel trap and never fixable (and thus must
693 never return). */
694 for (;;)
695 die(str, regs, error_code);
696 }
698 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
699 long error_code)
700 {
701 struct task_struct *tsk = current;
703 conditional_sti(regs);
705 tsk->thread.error_code = error_code;
706 tsk->thread.trap_no = 13;
708 if (user_mode(regs)) {
709 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
710 printk(KERN_INFO
711 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
712 tsk->comm, tsk->pid,
713 regs->rip, regs->rsp, error_code);
715 force_sig(SIGSEGV, tsk);
716 return;
717 }
719 /* kernel gp */
720 {
721 const struct exception_table_entry *fixup;
722 fixup = search_exception_tables(regs->rip);
723 if (fixup) {
724 regs->rip = fixup->fixup;
725 return;
726 }
727 if (notify_die(DIE_GPF, "general protection fault", regs,
728 error_code, 13, SIGSEGV) == NOTIFY_STOP)
729 return;
730 die("general protection fault", regs, error_code);
731 }
732 }
734 static __kprobes void
735 mem_parity_error(unsigned char reason, struct pt_regs * regs)
736 {
737 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
738 printk("You probably have a hardware problem with your RAM chips\n");
740 #if 0 /* XEN */
741 /* Clear and disable the memory parity error line. */
742 reason = (reason & 0xf) | 4;
743 outb(reason, 0x61);
744 #endif /* XEN */
745 }
747 static __kprobes void
748 io_check_error(unsigned char reason, struct pt_regs * regs)
749 {
750 printk("NMI: IOCK error (debug interrupt?)\n");
751 show_registers(regs);
753 #if 0 /* XEN */
754 /* Re-enable the IOCK line, wait for a few seconds */
755 reason = (reason & 0xf) | 8;
756 outb(reason, 0x61);
757 mdelay(2000);
758 reason &= ~8;
759 outb(reason, 0x61);
760 #endif /* XEN */
761 }
763 static __kprobes void
764 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
765 { printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
766 printk("Dazed and confused, but trying to continue\n");
767 printk("Do you have a strange power saving mode enabled?\n");
768 }
770 /* Runs on IST stack. This code must keep interrupts off all the time.
771 Nested NMIs are prevented by the CPU. */
772 asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
773 {
774 unsigned char reason = 0;
775 int cpu;
777 cpu = smp_processor_id();
779 /* Only the BSP gets external NMIs from the system. */
780 if (!cpu)
781 reason = get_nmi_reason();
783 if (!(reason & 0xc0)) {
784 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
785 == NOTIFY_STOP)
786 return;
787 #ifdef CONFIG_X86_LOCAL_APIC
788 /*
789 * Ok, so this is none of the documented NMI sources,
790 * so it must be the NMI watchdog.
791 */
792 if (nmi_watchdog > 0) {
793 nmi_watchdog_tick(regs,reason);
794 return;
795 }
796 #endif
797 unknown_nmi_error(reason, regs);
798 return;
799 }
800 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
801 return;
803 /* AK: following checks seem to be broken on modern chipsets. FIXME */
805 if (reason & 0x80)
806 mem_parity_error(reason, regs);
807 if (reason & 0x40)
808 io_check_error(reason, regs);
809 }
811 /* runs on IST stack. */
812 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
813 {
814 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
815 return;
816 }
817 preempt_conditional_sti(regs);
818 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
819 preempt_conditional_cli(regs);
820 }
822 /* Help handler running on IST stack to switch back to user stack
823 for scheduling or signal handling. The actual stack switch is done in
824 entry.S */
825 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
826 {
827 struct pt_regs *regs = eregs;
828 /* Did already sync */
829 if (eregs == (struct pt_regs *)eregs->rsp)
830 ;
831 /* Exception from user space */
832 else if (user_mode(eregs))
833 regs = task_pt_regs(current);
834 /* Exception from kernel and interrupts are enabled. Move to
835 kernel process stack. */
836 else if (eregs->eflags & X86_EFLAGS_IF)
837 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
838 if (eregs != regs)
839 *regs = *eregs;
840 return regs;
841 }
843 /* runs on IST stack. */
844 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
845 unsigned long error_code)
846 {
847 unsigned long condition;
848 struct task_struct *tsk = current;
849 siginfo_t info;
851 get_debugreg(condition, 6);
853 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
854 SIGTRAP) == NOTIFY_STOP)
855 return;
857 preempt_conditional_sti(regs);
859 /* Mask out spurious debug traps due to lazy DR7 setting */
860 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
861 if (!tsk->thread.debugreg7) {
862 goto clear_dr7;
863 }
864 }
866 tsk->thread.debugreg6 = condition;
868 /* Mask out spurious TF errors due to lazy TF clearing */
869 if (condition & DR_STEP) {
870 /*
871 * The TF error should be masked out only if the current
872 * process is not traced and if the TRAP flag has been set
873 * previously by a tracing process (condition detected by
874 * the PT_DTRACE flag); remember that the i386 TRAP flag
875 * can be modified by the process itself in user mode,
876 * allowing programs to debug themselves without the ptrace()
877 * interface.
878 */
879 if (!user_mode(regs))
880 goto clear_TF_reenable;
881 /*
882 * Was the TF flag set by a debugger? If so, clear it now,
883 * so that register information is correct.
884 */
885 if (tsk->ptrace & PT_DTRACE) {
886 regs->eflags &= ~TF_MASK;
887 tsk->ptrace &= ~PT_DTRACE;
888 }
889 }
891 /* Ok, finally something we can handle */
892 tsk->thread.trap_no = 1;
893 tsk->thread.error_code = error_code;
894 info.si_signo = SIGTRAP;
895 info.si_errno = 0;
896 info.si_code = TRAP_BRKPT;
897 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
898 force_sig_info(SIGTRAP, &info, tsk);
900 clear_dr7:
901 set_debugreg(0UL, 7);
902 preempt_conditional_cli(regs);
903 return;
905 clear_TF_reenable:
906 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
907 regs->eflags &= ~TF_MASK;
908 preempt_conditional_cli(regs);
909 }
911 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
912 {
913 const struct exception_table_entry *fixup;
914 fixup = search_exception_tables(regs->rip);
915 if (fixup) {
916 regs->rip = fixup->fixup;
917 return 1;
918 }
919 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
920 /* Illegal floating point operation in the kernel */
921 current->thread.trap_no = trapnr;
922 die(str, regs, 0);
923 return 0;
924 }
926 /*
927 * Note that we play around with the 'TS' bit in an attempt to get
928 * the correct behaviour even in the presence of the asynchronous
929 * IRQ13 behaviour
930 */
931 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
932 {
933 void __user *rip = (void __user *)(regs->rip);
934 struct task_struct * task;
935 siginfo_t info;
936 unsigned short cwd, swd;
938 conditional_sti(regs);
939 if (!user_mode(regs) &&
940 kernel_math_error(regs, "kernel x87 math error", 16))
941 return;
943 /*
944 * Save the info for the exception handler and clear the error.
945 */
946 task = current;
947 save_init_fpu(task);
948 task->thread.trap_no = 16;
949 task->thread.error_code = 0;
950 info.si_signo = SIGFPE;
951 info.si_errno = 0;
952 info.si_code = __SI_FAULT;
953 info.si_addr = rip;
954 /*
955 * (~cwd & swd) will mask out exceptions that are not set to unmasked
956 * status. 0x3f is the exception bits in these regs, 0x200 is the
957 * C1 reg you need in case of a stack fault, 0x040 is the stack
958 * fault bit. We should only be taking one exception at a time,
959 * so if this combination doesn't produce any single exception,
960 * then we have a bad program that isn't synchronizing its FPU usage
961 * and it will suffer the consequences since we won't be able to
962 * fully reproduce the context of the exception
963 */
964 cwd = get_fpu_cwd(task);
965 swd = get_fpu_swd(task);
966 switch (swd & ~cwd & 0x3f) {
967 case 0x000:
968 default:
969 break;
970 case 0x001: /* Invalid Op */
971 /*
972 * swd & 0x240 == 0x040: Stack Underflow
973 * swd & 0x240 == 0x240: Stack Overflow
974 * User must clear the SF bit (0x40) if set
975 */
976 info.si_code = FPE_FLTINV;
977 break;
978 case 0x002: /* Denormalize */
979 case 0x010: /* Underflow */
980 info.si_code = FPE_FLTUND;
981 break;
982 case 0x004: /* Zero Divide */
983 info.si_code = FPE_FLTDIV;
984 break;
985 case 0x008: /* Overflow */
986 info.si_code = FPE_FLTOVF;
987 break;
988 case 0x020: /* Precision */
989 info.si_code = FPE_FLTRES;
990 break;
991 }
992 force_sig_info(SIGFPE, &info, task);
993 }
995 asmlinkage void bad_intr(void)
996 {
997 printk("bad interrupt");
998 }
1000 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1002 void __user *rip = (void __user *)(regs->rip);
1003 struct task_struct * task;
1004 siginfo_t info;
1005 unsigned short mxcsr;
1007 conditional_sti(regs);
1008 if (!user_mode(regs) &&
1009 kernel_math_error(regs, "kernel simd math error", 19))
1010 return;
1012 /*
1013 * Save the info for the exception handler and clear the error.
1014 */
1015 task = current;
1016 save_init_fpu(task);
1017 task->thread.trap_no = 19;
1018 task->thread.error_code = 0;
1019 info.si_signo = SIGFPE;
1020 info.si_errno = 0;
1021 info.si_code = __SI_FAULT;
1022 info.si_addr = rip;
1023 /*
1024 * The SIMD FPU exceptions are handled a little differently, as there
1025 * is only a single status/control register. Thus, to determine which
1026 * unmasked exception was caught we must mask the exception mask bits
1027 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1028 */
1029 mxcsr = get_fpu_mxcsr(task);
1030 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1031 case 0x000:
1032 default:
1033 break;
1034 case 0x001: /* Invalid Op */
1035 info.si_code = FPE_FLTINV;
1036 break;
1037 case 0x002: /* Denormalize */
1038 case 0x010: /* Underflow */
1039 info.si_code = FPE_FLTUND;
1040 break;
1041 case 0x004: /* Zero Divide */
1042 info.si_code = FPE_FLTDIV;
1043 break;
1044 case 0x008: /* Overflow */
1045 info.si_code = FPE_FLTOVF;
1046 break;
1047 case 0x020: /* Precision */
1048 info.si_code = FPE_FLTRES;
1049 break;
1051 force_sig_info(SIGFPE, &info, task);
1054 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1058 #if 0
1059 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1062 #endif
1064 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1068 /*
1069 * 'math_state_restore()' saves the current math information in the
1070 * old math state array, and gets the new ones from the current task
1072 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1073 * Don't touch unless you *really* know how it works.
1074 */
1075 asmlinkage void math_state_restore(void)
1077 struct task_struct *me = current;
1078 /* clts(); */ /* 'clts' is done for us by Xen during virtual trap. */
1080 if (!used_math())
1081 init_fpu(me);
1082 restore_fpu_checking(&me->thread.i387.fxsave);
1083 task_thread_info(me)->status |= TS_USEDFPU;
1087 /*
1088 * NB. All these are "interrupt gates" (i.e. events_mask is set) because we
1089 * specify <dpl>|4 in the second field.
1090 */
1091 static trap_info_t trap_table[] = {
1092 { 0, 0|4, __KERNEL_CS, (unsigned long)divide_error },
1093 { 1, 0|4, __KERNEL_CS, (unsigned long)debug },
1094 { 3, 3|4, __KERNEL_CS, (unsigned long)int3 },
1095 { 4, 3|4, __KERNEL_CS, (unsigned long)overflow },
1096 { 5, 0|4, __KERNEL_CS, (unsigned long)bounds },
1097 { 6, 0|4, __KERNEL_CS, (unsigned long)invalid_op },
1098 { 7, 0|4, __KERNEL_CS, (unsigned long)device_not_available },
1099 { 9, 0|4, __KERNEL_CS, (unsigned long)coprocessor_segment_overrun},
1100 { 10, 0|4, __KERNEL_CS, (unsigned long)invalid_TSS },
1101 { 11, 0|4, __KERNEL_CS, (unsigned long)segment_not_present },
1102 { 12, 0|4, __KERNEL_CS, (unsigned long)stack_segment },
1103 { 13, 0|4, __KERNEL_CS, (unsigned long)general_protection },
1104 { 14, 0|4, __KERNEL_CS, (unsigned long)page_fault },
1105 { 15, 0|4, __KERNEL_CS, (unsigned long)spurious_interrupt_bug },
1106 { 16, 0|4, __KERNEL_CS, (unsigned long)coprocessor_error },
1107 { 17, 0|4, __KERNEL_CS, (unsigned long)alignment_check },
1108 #ifdef CONFIG_X86_MCE
1109 { 18, 0|4, __KERNEL_CS, (unsigned long)machine_check },
1110 #endif
1111 { 19, 0|4, __KERNEL_CS, (unsigned long)simd_coprocessor_error },
1112 #ifdef CONFIG_IA32_EMULATION
1113 { IA32_SYSCALL_VECTOR, 3|4, __KERNEL_CS, (unsigned long)ia32_syscall},
1114 #endif
1115 { 0, 0, 0, 0 }
1116 };
1118 void __init trap_init(void)
1120 int ret;
1122 ret = HYPERVISOR_set_trap_table(trap_table);
1124 if (ret)
1125 printk("HYPERVISOR_set_trap_table faild: error %d\n",
1126 ret);
1128 /*
1129 * Should be a barrier for any external CPU state.
1130 */
1131 cpu_init();
1134 void smp_trap_init(trap_info_t *trap_ctxt)
1136 trap_info_t *t = trap_table;
1138 for (t = trap_table; t->address; t++) {
1139 trap_ctxt[t->vector].flags = t->flags;
1140 trap_ctxt[t->vector].cs = t->cs;
1141 trap_ctxt[t->vector].address = t->address;
1146 /* Actual parsing is done early in setup.c. */
1147 static int __init oops_dummy(char *s)
1149 panic_on_oops = 1;
1150 return 1;
1152 __setup("oops=", oops_dummy);
1154 static int __init kstack_setup(char *s)
1156 kstack_depth_to_print = simple_strtoul(s,NULL,0);
1157 return 1;
1159 __setup("kstack=", kstack_setup);
1161 #ifdef CONFIG_STACK_UNWIND
1162 static int __init call_trace_setup(char *s)
1164 if (strcmp(s, "old") == 0)
1165 call_trace = -1;
1166 else if (strcmp(s, "both") == 0)
1167 call_trace = 0;
1168 else if (strcmp(s, "newfallback") == 0)
1169 call_trace = 1;
1170 else if (strcmp(s, "new") == 0)
1171 call_trace = 2;
1172 return 1;
1174 __setup("call_trace=", call_trace_setup);
1175 #endif