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