ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/x86_64/kernel/traps.c @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
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 *
10 * $Id: traps.c,v 1.36 2002/03/24 11:09:10 ak Exp $
11 */
13 /*
14 * 'Traps.c' handles hardware traps and faults after we have saved some
15 * state in 'entry.S'.
16 */
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/ptrace.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/nmi.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>
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 #include <linux/irq.h>
52 extern struct gate_struct idt_table[256];
54 asmlinkage void divide_error(void);
55 asmlinkage void debug(void);
56 asmlinkage void nmi(void);
57 asmlinkage void int3(void);
58 asmlinkage void overflow(void);
59 asmlinkage void bounds(void);
60 asmlinkage void invalid_op(void);
61 asmlinkage void device_not_available(void);
62 asmlinkage void double_fault(void);
63 asmlinkage void coprocessor_segment_overrun(void);
64 asmlinkage void invalid_TSS(void);
65 asmlinkage void segment_not_present(void);
66 asmlinkage void stack_segment(void);
67 asmlinkage void general_protection(void);
68 asmlinkage void page_fault(void);
69 asmlinkage void coprocessor_error(void);
70 asmlinkage void simd_coprocessor_error(void);
71 asmlinkage void reserved(void);
72 asmlinkage void alignment_check(void);
73 asmlinkage void machine_check(void);
74 asmlinkage void spurious_interrupt_bug(void);
75 asmlinkage void call_debug(void);
77 struct notifier_block *die_chain;
78 static DEFINE_SPINLOCK(die_notifier_lock);
80 int register_die_notifier(struct notifier_block *nb)
81 {
82 int err = 0;
83 unsigned long flags;
84 spin_lock_irqsave(&die_notifier_lock, flags);
85 err = notifier_chain_register(&die_chain, nb);
86 spin_unlock_irqrestore(&die_notifier_lock, flags);
87 return err;
88 }
90 static inline void conditional_sti(struct pt_regs *regs)
91 {
92 if (regs->eflags & X86_EFLAGS_IF)
93 local_irq_enable();
94 }
96 static int kstack_depth_to_print = 10;
98 #ifdef CONFIG_KALLSYMS
99 #include <linux/kallsyms.h>
100 int printk_address(unsigned long address)
101 {
102 unsigned long offset = 0, symsize;
103 const char *symname;
104 char *modname;
105 char *delim = ":";
106 char namebuf[128];
108 symname = kallsyms_lookup(address, &symsize, &offset, &modname, namebuf);
109 if (!symname)
110 return printk("[<%016lx>]", address);
111 if (!modname)
112 modname = delim = "";
113 return printk("<%016lx>{%s%s%s%s%+ld}",
114 address,delim,modname,delim,symname,offset);
115 }
116 #else
117 int printk_address(unsigned long address)
118 {
119 return printk("[<%016lx>]", address);
120 }
121 #endif
123 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
124 unsigned *usedp, const char **idp)
125 {
126 static const char ids[N_EXCEPTION_STACKS][8] = {
127 [DEBUG_STACK - 1] = "#DB",
128 [NMI_STACK - 1] = "NMI",
129 [DOUBLEFAULT_STACK - 1] = "#DF",
130 [STACKFAULT_STACK - 1] = "#SS",
131 [MCE_STACK - 1] = "#MC",
132 };
133 unsigned k;
135 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
136 unsigned long end;
138 end = per_cpu(init_tss, cpu).ist[k];
139 if (stack >= end)
140 continue;
141 if (stack >= end - EXCEPTION_STKSZ) {
142 if (*usedp & (1U << k))
143 break;
144 *usedp |= 1U << k;
145 *idp = ids[k];
146 return (unsigned long *)end;
147 }
148 }
149 return NULL;
150 }
152 /*
153 * x86-64 can have upto three kernel stacks:
154 * process stack
155 * interrupt stack
156 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
157 */
159 void show_trace(unsigned long *stack)
160 {
161 unsigned long addr;
162 const unsigned cpu = safe_smp_processor_id();
163 unsigned long *irqstack_end = (unsigned long *)cpu_pda[cpu].irqstackptr;
164 int i;
165 unsigned used = 0;
167 printk("\nCall Trace:");
169 #define HANDLE_STACK(cond) \
170 do while (cond) { \
171 addr = *stack++; \
172 if (kernel_text_address(addr)) { \
173 /* \
174 * If the address is either in the text segment of the \
175 * kernel, or in the region which contains vmalloc'ed \
176 * memory, it *may* be the address of a calling \
177 * routine; if so, print it so that someone tracing \
178 * down the cause of the crash will be able to figure \
179 * out the call path that was taken. \
180 */ \
181 i += printk_address(addr); \
182 if (i > 50) { \
183 printk("\n "); \
184 i = 0; \
185 } \
186 else \
187 i += printk(" "); \
188 } \
189 } while (0)
191 for(i = 0; ; ) {
192 const char *id;
193 unsigned long *estack_end;
194 estack_end = in_exception_stack(cpu, (unsigned long)stack,
195 &used, &id);
197 if (estack_end) {
198 i += printk(" <%s> ", id);
199 HANDLE_STACK (stack < estack_end);
200 i += printk(" <EOE> ");
201 stack = (unsigned long *) estack_end[-2];
202 continue;
203 }
204 if (irqstack_end) {
205 unsigned long *irqstack;
206 irqstack = irqstack_end -
207 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
209 if (stack >= irqstack && stack < irqstack_end) {
210 i += printk(" <IRQ> ");
211 HANDLE_STACK (stack < irqstack_end);
212 stack = (unsigned long *) (irqstack_end[-1]);
213 irqstack_end = NULL;
214 i += printk(" <EOI> ");
215 continue;
216 }
217 }
218 break;
219 }
221 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
222 #undef HANDLE_STACK
223 printk("\n");
224 }
226 void show_stack(struct task_struct *tsk, unsigned long * rsp)
227 {
228 unsigned long *stack;
229 int i;
230 const int cpu = safe_smp_processor_id();
231 unsigned long *irqstack_end = (unsigned long *) (cpu_pda[cpu].irqstackptr);
232 unsigned long *irqstack = (unsigned long *) (cpu_pda[cpu].irqstackptr - IRQSTACKSIZE);
234 // debugging aid: "show_stack(NULL, NULL);" prints the
235 // back trace for this cpu.
237 if (rsp == NULL) {
238 if (tsk)
239 rsp = (unsigned long *)tsk->thread.rsp;
240 else
241 rsp = (unsigned long *)&rsp;
242 }
244 stack = rsp;
245 for(i=0; i < kstack_depth_to_print; i++) {
246 if (stack >= irqstack && stack <= irqstack_end) {
247 if (stack == irqstack_end) {
248 stack = (unsigned long *) (irqstack_end[-1]);
249 printk(" <EOI> ");
250 }
251 } else {
252 if (((long) stack & (THREAD_SIZE-1)) == 0)
253 break;
254 }
255 if (i && ((i % 4) == 0))
256 printk("\n ");
257 printk("%016lx ", *stack++);
258 touch_nmi_watchdog();
259 }
260 show_trace((unsigned long *)rsp);
261 }
263 /*
264 * The architecture-independent dump_stack generator
265 */
266 void dump_stack(void)
267 {
268 unsigned long dummy;
269 show_trace(&dummy);
270 }
272 EXPORT_SYMBOL(dump_stack);
274 void show_registers(struct pt_regs *regs)
275 {
276 int i;
277 int in_kernel = (regs->cs & 3) == 0;
278 unsigned long rsp;
279 const int cpu = safe_smp_processor_id();
280 struct task_struct *cur = cpu_pda[cpu].pcurrent;
282 rsp = regs->rsp;
284 printk("CPU %d ", cpu);
285 __show_regs(regs);
286 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
287 cur->comm, cur->pid, cur->thread_info, cur);
289 /*
290 * When in-kernel, we also print out the stack and code at the
291 * time of the fault..
292 */
293 if (in_kernel) {
295 printk("Stack: ");
296 show_stack(NULL, (unsigned long*)rsp);
298 printk("\nCode: ");
299 if(regs->rip < PAGE_OFFSET)
300 goto bad;
302 for(i=0;i<20;i++)
303 {
304 unsigned char c;
305 if(__get_user(c, &((unsigned char*)regs->rip)[i])) {
306 bad:
307 printk(" Bad RIP value.");
308 break;
309 }
310 printk("%02x ", c);
311 }
312 }
313 printk("\n");
314 }
316 void handle_BUG(struct pt_regs *regs)
317 {
318 struct bug_frame f;
319 char tmp;
321 if (regs->cs & 3)
322 return;
323 if (__copy_from_user(&f, (struct bug_frame *) regs->rip,
324 sizeof(struct bug_frame)))
325 return;
326 if ((unsigned long)f.filename < __PAGE_OFFSET ||
327 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
328 return;
329 if (__get_user(tmp, f.filename))
330 f.filename = "unmapped filename";
331 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
332 printk(KERN_ALERT "Kernel BUG at %.50s:%d\n", f.filename, f.line);
333 }
335 #ifdef CONFIG_BUG
336 void out_of_line_bug(void)
337 {
338 BUG();
339 }
340 #endif
342 static DEFINE_SPINLOCK(die_lock);
343 static int die_owner = -1;
345 void oops_begin(void)
346 {
347 int cpu = safe_smp_processor_id();
348 /* racy, but better than risking deadlock. */
349 local_irq_disable();
350 if (!spin_trylock(&die_lock)) {
351 if (cpu == die_owner)
352 /* nested oops. should stop eventually */;
353 else
354 spin_lock(&die_lock);
355 }
356 die_owner = cpu;
357 console_verbose();
358 bust_spinlocks(1);
359 }
361 void oops_end(void)
362 {
363 die_owner = -1;
364 bust_spinlocks(0);
365 spin_unlock(&die_lock);
366 if (panic_on_oops)
367 panic("Oops");
368 }
370 void __die(const char * str, struct pt_regs * regs, long err)
371 {
372 static int die_counter;
373 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
374 #ifdef CONFIG_PREEMPT
375 printk("PREEMPT ");
376 #endif
377 #ifdef CONFIG_SMP
378 printk("SMP ");
379 #endif
380 #ifdef CONFIG_DEBUG_PAGEALLOC
381 printk("DEBUG_PAGEALLOC");
382 #endif
383 printk("\n");
384 notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
385 show_registers(regs);
386 /* Executive summary in case the oops scrolled away */
387 printk(KERN_ALERT "RIP ");
388 printk_address(regs->rip);
389 printk(" RSP <%016lx>\n", regs->rsp);
390 }
392 void die(const char * str, struct pt_regs * regs, long err)
393 {
394 oops_begin();
395 handle_BUG(regs);
396 __die(str, regs, err);
397 oops_end();
398 do_exit(SIGSEGV);
399 }
400 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
401 {
402 if (!(regs->eflags & VM_MASK) && (regs->cs == __KERNEL_CS))
403 die(str, regs, err);
404 }
406 #ifdef CONFIG_X86_LOCAL_APIC
407 void die_nmi(char *str, struct pt_regs *regs)
408 {
409 oops_begin();
410 /*
411 * We are in trouble anyway, lets at least try
412 * to get a message out.
413 */
414 printk(str, safe_smp_processor_id());
415 show_registers(regs);
416 if (panic_on_timeout || panic_on_oops)
417 panic("nmi watchdog");
418 printk("console shuts up ...\n");
419 oops_end();
420 do_exit(SIGSEGV);
421 }
422 #endif
424 static void do_trap(int trapnr, int signr, char *str,
425 struct pt_regs * regs, long error_code, siginfo_t *info)
426 {
427 conditional_sti(regs);
429 #ifdef CONFIG_CHECKING
430 {
431 unsigned long gs;
432 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
433 rdmsrl(MSR_GS_BASE, gs);
434 if (gs != (unsigned long)pda) {
435 wrmsrl(MSR_GS_BASE, pda);
436 printk("%s: wrong gs %lx expected %p rip %lx\n", str, gs, pda,
437 regs->rip);
438 }
439 }
440 #endif
442 if ((regs->cs & 3) != 0) {
443 struct task_struct *tsk = current;
445 if (exception_trace && unhandled_signal(tsk, signr))
446 printk(KERN_INFO
447 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
448 tsk->comm, tsk->pid, str,
449 regs->rip,regs->rsp,error_code);
451 tsk->thread.error_code = error_code;
452 tsk->thread.trap_no = trapnr;
453 if (info)
454 force_sig_info(signr, info, tsk);
455 else
456 force_sig(signr, tsk);
457 return;
458 }
461 /* kernel trap */
462 {
463 const struct exception_table_entry *fixup;
464 fixup = search_exception_tables(regs->rip);
465 if (fixup) {
466 regs->rip = fixup->fixup;
467 } else
468 die(str, regs, error_code);
469 return;
470 }
471 }
473 #define DO_ERROR(trapnr, signr, str, name) \
474 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
475 { \
476 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
477 == NOTIFY_STOP) \
478 return; \
479 do_trap(trapnr, signr, str, regs, error_code, NULL); \
480 }
482 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
483 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
484 { \
485 siginfo_t info; \
486 info.si_signo = signr; \
487 info.si_errno = 0; \
488 info.si_code = sicode; \
489 info.si_addr = (void __user *)siaddr; \
490 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
491 == NOTIFY_STOP) \
492 return; \
493 do_trap(trapnr, signr, str, regs, error_code, &info); \
494 }
496 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
497 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
498 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
499 DO_ERROR_INFO( 6, SIGILL, "invalid operand", invalid_op, ILL_ILLOPN, regs->rip)
500 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
501 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
502 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
503 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
504 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
505 DO_ERROR(18, SIGSEGV, "reserved", reserved)
506 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
507 DO_ERROR( 8, SIGSEGV, "double fault", double_fault)
509 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
510 {
511 conditional_sti(regs);
513 #ifdef CONFIG_CHECKING
514 {
515 unsigned long gs;
516 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
517 rdmsrl(MSR_GS_BASE, gs);
518 if (gs != (unsigned long)pda) {
519 wrmsrl(MSR_GS_BASE, pda);
520 oops_in_progress++;
521 printk("general protection handler: wrong gs %lx expected %p\n", gs, pda);
522 oops_in_progress--;
523 }
524 }
525 #endif
527 if ((regs->cs & 3)!=0) {
528 struct task_struct *tsk = current;
530 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
531 printk(KERN_INFO
532 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
533 tsk->comm, tsk->pid,
534 regs->rip,regs->rsp,error_code);
536 tsk->thread.error_code = error_code;
537 tsk->thread.trap_no = 13;
538 force_sig(SIGSEGV, tsk);
539 return;
540 }
542 /* kernel gp */
543 {
544 const struct exception_table_entry *fixup;
545 fixup = search_exception_tables(regs->rip);
546 if (fixup) {
547 regs->rip = fixup->fixup;
548 return;
549 }
550 if (notify_die(DIE_GPF, "general protection fault", regs,
551 error_code, 13, SIGSEGV) == NOTIFY_STOP)
552 return;
553 die("general protection fault", regs, error_code);
554 }
555 }
557 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
558 {
559 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
560 printk("You probably have a hardware problem with your RAM chips\n");
562 /* Clear and disable the memory parity error line. */
563 reason = (reason & 0xf) | 4;
564 outb(reason, 0x61);
565 }
567 static void io_check_error(unsigned char reason, struct pt_regs * regs)
568 {
569 printk("NMI: IOCK error (debug interrupt?)\n");
570 show_registers(regs);
572 /* Re-enable the IOCK line, wait for a few seconds */
573 reason = (reason & 0xf) | 8;
574 outb(reason, 0x61);
575 mdelay(2000);
576 reason &= ~8;
577 outb(reason, 0x61);
578 }
580 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
581 { printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
582 printk("Dazed and confused, but trying to continue\n");
583 printk("Do you have a strange power saving mode enabled?\n");
584 }
586 /* Runs on IST stack. This code must keep interrupts off all the time.
587 Nested NMIs are prevented by the CPU. */
588 asmlinkage void default_do_nmi(struct pt_regs *regs)
589 {
590 unsigned char reason = 0;
592 /* Only the BSP gets external NMIs from the system. */
593 if (!smp_processor_id())
594 reason = get_nmi_reason();
596 if (!(reason & 0xc0)) {
597 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT)
598 == NOTIFY_STOP)
599 return;
600 #ifdef CONFIG_X86_LOCAL_APIC
601 /*
602 * Ok, so this is none of the documented NMI sources,
603 * so it must be the NMI watchdog.
604 */
605 if (nmi_watchdog > 0) {
606 nmi_watchdog_tick(regs,reason);
607 return;
608 }
609 #endif
610 unknown_nmi_error(reason, regs);
611 return;
612 }
613 if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_STOP)
614 return;
616 /* AK: following checks seem to be broken on modern chipsets. FIXME */
618 if (reason & 0x80)
619 mem_parity_error(reason, regs);
620 if (reason & 0x40)
621 io_check_error(reason, regs);
622 }
624 asmlinkage void do_int3(struct pt_regs * regs, long error_code)
625 {
626 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
627 return;
628 }
629 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
630 return;
631 }
633 /* Help handler running on IST stack to switch back to user stack
634 for scheduling or signal handling. The actual stack switch is done in
635 entry.S */
636 asmlinkage struct pt_regs *sync_regs(struct pt_regs *eregs)
637 {
638 struct pt_regs *regs = eregs;
639 /* Did already sync */
640 if (eregs == (struct pt_regs *)eregs->rsp)
641 ;
642 /* Exception from user space */
643 else if (eregs->cs & 3)
644 regs = ((struct pt_regs *)current->thread.rsp0) - 1;
645 /* Exception from kernel and interrupts are enabled. Move to
646 kernel process stack. */
647 else if (eregs->eflags & X86_EFLAGS_IF)
648 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
649 if (eregs != regs)
650 *regs = *eregs;
651 return regs;
652 }
654 /* runs on IST stack. */
655 asmlinkage void do_debug(struct pt_regs * regs, unsigned long error_code)
656 {
657 unsigned long condition;
658 struct task_struct *tsk = current;
659 siginfo_t info;
661 #ifdef CONFIG_CHECKING
662 {
663 /* RED-PEN interaction with debugger - could destroy gs */
664 unsigned long gs;
665 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
666 rdmsrl(MSR_GS_BASE, gs);
667 if (gs != (unsigned long)pda) {
668 wrmsrl(MSR_GS_BASE, pda);
669 printk("debug handler: wrong gs %lx expected %p\n", gs, pda);
670 }
671 }
672 #endif
674 asm("movq %%db6,%0" : "=r" (condition));
676 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
677 SIGTRAP) == NOTIFY_STOP)
678 return;
680 conditional_sti(regs);
682 /* Mask out spurious debug traps due to lazy DR7 setting */
683 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
684 if (!tsk->thread.debugreg7) {
685 goto clear_dr7;
686 }
687 }
689 tsk->thread.debugreg6 = condition;
691 /* Mask out spurious TF errors due to lazy TF clearing */
692 if (condition & DR_STEP) {
693 /*
694 * The TF error should be masked out only if the current
695 * process is not traced and if the TRAP flag has been set
696 * previously by a tracing process (condition detected by
697 * the PT_DTRACE flag); remember that the i386 TRAP flag
698 * can be modified by the process itself in user mode,
699 * allowing programs to debug themselves without the ptrace()
700 * interface.
701 */
702 if ((regs->cs & 3) == 0)
703 goto clear_TF_reenable;
704 /*
705 * Was the TF flag set by a debugger? If so, clear it now,
706 * so that register information is correct.
707 */
708 if (tsk->ptrace & PT_DTRACE) {
709 regs->eflags &= ~TF_MASK;
710 tsk->ptrace &= ~PT_DTRACE;
711 }
712 }
714 /* Ok, finally something we can handle */
715 tsk->thread.trap_no = 1;
716 tsk->thread.error_code = error_code;
717 info.si_signo = SIGTRAP;
718 info.si_errno = 0;
719 info.si_code = TRAP_BRKPT;
720 if ((regs->cs & 3) == 0)
721 goto clear_dr7;
723 info.si_addr = (void __user *)regs->rip;
724 force_sig_info(SIGTRAP, &info, tsk);
725 clear_dr7:
726 asm volatile("movq %0,%%db7"::"r"(0UL));
727 return;
729 clear_TF_reenable:
730 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
731 regs->eflags &= ~TF_MASK;
732 }
734 static int kernel_math_error(struct pt_regs *regs, char *str)
735 {
736 const struct exception_table_entry *fixup;
737 fixup = search_exception_tables(regs->rip);
738 if (fixup) {
739 regs->rip = fixup->fixup;
740 return 1;
741 }
742 notify_die(DIE_GPF, str, regs, 0, 16, SIGFPE);
743 /* Illegal floating point operation in the kernel */
744 die(str, regs, 0);
745 return 0;
746 }
748 /*
749 * Note that we play around with the 'TS' bit in an attempt to get
750 * the correct behaviour even in the presence of the asynchronous
751 * IRQ13 behaviour
752 */
753 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
754 {
755 void __user *rip = (void __user *)(regs->rip);
756 struct task_struct * task;
757 siginfo_t info;
758 unsigned short cwd, swd;
760 conditional_sti(regs);
761 if ((regs->cs & 3) == 0 &&
762 kernel_math_error(regs, "kernel x87 math error"))
763 return;
765 /*
766 * Save the info for the exception handler and clear the error.
767 */
768 task = current;
769 save_init_fpu(task);
770 task->thread.trap_no = 16;
771 task->thread.error_code = 0;
772 info.si_signo = SIGFPE;
773 info.si_errno = 0;
774 info.si_code = __SI_FAULT;
775 info.si_addr = rip;
776 /*
777 * (~cwd & swd) will mask out exceptions that are not set to unmasked
778 * status. 0x3f is the exception bits in these regs, 0x200 is the
779 * C1 reg you need in case of a stack fault, 0x040 is the stack
780 * fault bit. We should only be taking one exception at a time,
781 * so if this combination doesn't produce any single exception,
782 * then we have a bad program that isn't synchronizing its FPU usage
783 * and it will suffer the consequences since we won't be able to
784 * fully reproduce the context of the exception
785 */
786 cwd = get_fpu_cwd(task);
787 swd = get_fpu_swd(task);
788 switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
789 case 0x000:
790 default:
791 break;
792 case 0x001: /* Invalid Op */
793 case 0x041: /* Stack Fault */
794 case 0x241: /* Stack Fault | Direction */
795 info.si_code = FPE_FLTINV;
796 break;
797 case 0x002: /* Denormalize */
798 case 0x010: /* Underflow */
799 info.si_code = FPE_FLTUND;
800 break;
801 case 0x004: /* Zero Divide */
802 info.si_code = FPE_FLTDIV;
803 break;
804 case 0x008: /* Overflow */
805 info.si_code = FPE_FLTOVF;
806 break;
807 case 0x020: /* Precision */
808 info.si_code = FPE_FLTRES;
809 break;
810 }
811 force_sig_info(SIGFPE, &info, task);
812 }
814 asmlinkage void bad_intr(void)
815 {
816 printk("bad interrupt");
817 }
819 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
820 {
821 void __user *rip = (void __user *)(regs->rip);
822 struct task_struct * task;
823 siginfo_t info;
824 unsigned short mxcsr;
826 conditional_sti(regs);
827 if ((regs->cs & 3) == 0 &&
828 kernel_math_error(regs, "kernel simd math error"))
829 return;
831 /*
832 * Save the info for the exception handler and clear the error.
833 */
834 task = current;
835 save_init_fpu(task);
836 task->thread.trap_no = 19;
837 task->thread.error_code = 0;
838 info.si_signo = SIGFPE;
839 info.si_errno = 0;
840 info.si_code = __SI_FAULT;
841 info.si_addr = rip;
842 /*
843 * The SIMD FPU exceptions are handled a little differently, as there
844 * is only a single status/control register. Thus, to determine which
845 * unmasked exception was caught we must mask the exception mask bits
846 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
847 */
848 mxcsr = get_fpu_mxcsr(task);
849 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
850 case 0x000:
851 default:
852 break;
853 case 0x001: /* Invalid Op */
854 info.si_code = FPE_FLTINV;
855 break;
856 case 0x002: /* Denormalize */
857 case 0x010: /* Underflow */
858 info.si_code = FPE_FLTUND;
859 break;
860 case 0x004: /* Zero Divide */
861 info.si_code = FPE_FLTDIV;
862 break;
863 case 0x008: /* Overflow */
864 info.si_code = FPE_FLTOVF;
865 break;
866 case 0x020: /* Precision */
867 info.si_code = FPE_FLTRES;
868 break;
869 }
870 force_sig_info(SIGFPE, &info, task);
871 }
873 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
874 {
875 }
877 #if 0
878 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
879 {
880 }
881 #endif
883 /*
884 * 'math_state_restore()' saves the current math information in the
885 * old math state array, and gets the new ones from the current task
886 *
887 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
888 * Don't touch unless you *really* know how it works.
889 */
890 asmlinkage void math_state_restore(void)
891 {
892 struct task_struct *me = current;
894 /* clts(); */ /* 'clts' is done for us by Xen during virtual trap. */
896 if (!used_math())
897 init_fpu(me);
898 restore_fpu_checking(&me->thread.i387.fxsave);
899 me->thread_info->status |= TS_USEDFPU;
900 }
902 void do_call_debug(struct pt_regs *regs)
903 {
904 notify_die(DIE_CALL, "debug call", regs, 0, 255, SIGINT);
905 }
908 static trap_info_t trap_table[] = {
909 { 0, 0, (__KERNEL_CS|0x3), (unsigned long)divide_error },
910 { 1, 0, (__KERNEL_CS|0x3), (unsigned long)debug },
911 { 3, 3, (__KERNEL_CS|0x3), (unsigned long)int3 },
912 { 4, 3, (__KERNEL_CS|0x3), (unsigned long)overflow },
913 { 5, 3, (__KERNEL_CS|0x3), (unsigned long)bounds },
914 { 6, 0, (__KERNEL_CS|0x3), (unsigned long)invalid_op },
915 { 7, 0, (__KERNEL_CS|0x3), (unsigned long)device_not_available },
916 { 9, 0, (__KERNEL_CS|0x3), (unsigned long)coprocessor_segment_overrun},
917 { 10, 0, (__KERNEL_CS|0x3), (unsigned long)invalid_TSS },
918 { 11, 0, (__KERNEL_CS|0x3), (unsigned long)segment_not_present },
919 { 12, 0, (__KERNEL_CS|0x3), (unsigned long)stack_segment },
920 { 13, 0, (__KERNEL_CS|0x3), (unsigned long)general_protection },
921 { 14, 0, (__KERNEL_CS|0x3), (unsigned long)page_fault },
922 { 15, 0, (__KERNEL_CS|0x3), (unsigned long)spurious_interrupt_bug },
923 { 16, 0, (__KERNEL_CS|0x3), (unsigned long)coprocessor_error },
924 { 17, 0, (__KERNEL_CS|0x3), (unsigned long)alignment_check },
925 #ifdef CONFIG_X86_MCE
926 { 18, 0, (__KERNEL_CS|0x3), (unsigned long)machine_check },
927 #endif
928 { 19, 0, (__KERNEL_CS|0x3), (unsigned long)simd_coprocessor_error },
929 { SYSCALL_VECTOR, 3, (__KERNEL_CS|0x3), (unsigned long)system_call },
930 #ifdef CONFIG_IA32_EMULATION
931 { IA32_SYSCALL_VECTOR, 3, (__KERNEL_CS|0x3), (unsigned long)ia32_syscall},
932 #endif
933 { 0, 0, 0, 0 }
934 };
936 void __init trap_init(void)
937 {
938 int ret;
940 ret = HYPERVISOR_set_trap_table(trap_table);
942 if (ret)
943 printk("HYPERVISOR_set_trap_table faild: error %d\n",
944 ret);
946 #ifdef CONFIG_IA32_EMULATION
947 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
948 #endif
950 /*
951 * Should be a barrier for any external CPU state.
952 */
953 cpu_init();
954 }
956 void smp_trap_init(trap_info_t *trap_ctxt)
957 {
958 trap_info_t *t = trap_table;
960 for (t = trap_table; t->address; t++) {
961 trap_ctxt[t->vector].flags = t->flags;
962 trap_ctxt[t->vector].cs = t->cs;
963 trap_ctxt[t->vector].address = t->address;
964 }
965 }
968 /* Actual parsing is done early in setup.c. */
969 static int __init oops_dummy(char *s)
970 {
971 panic_on_oops = 1;
972 return -1;
973 }
974 __setup("oops=", oops_dummy);
976 static int __init kstack_setup(char *s)
977 {
978 kstack_depth_to_print = simple_strtoul(s,NULL,0);
979 return 0;
980 }
981 __setup("kstack=", kstack_setup);