blob: 65a37f52c56ef2c0760f2e3db9dfec9312a74d88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
akpm@osdl.org35faa712005-04-16 15:24:54 -070031#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
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
52extern struct gate_struct idt_table[256];
53
54asmlinkage void divide_error(void);
55asmlinkage void debug(void);
56asmlinkage void nmi(void);
57asmlinkage void int3(void);
58asmlinkage void overflow(void);
59asmlinkage void bounds(void);
60asmlinkage void invalid_op(void);
61asmlinkage void device_not_available(void);
62asmlinkage void double_fault(void);
63asmlinkage void coprocessor_segment_overrun(void);
64asmlinkage void invalid_TSS(void);
65asmlinkage void segment_not_present(void);
66asmlinkage void stack_segment(void);
67asmlinkage void general_protection(void);
68asmlinkage void page_fault(void);
69asmlinkage void coprocessor_error(void);
70asmlinkage void simd_coprocessor_error(void);
71asmlinkage void reserved(void);
72asmlinkage void alignment_check(void);
73asmlinkage void machine_check(void);
74asmlinkage void spurious_interrupt_bug(void);
75asmlinkage void call_debug(void);
76
77struct notifier_block *die_chain;
78static DEFINE_SPINLOCK(die_notifier_lock);
79
80int 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
90static inline void conditional_sti(struct pt_regs *regs)
91{
92 if (regs->eflags & X86_EFLAGS_IF)
93 local_irq_enable();
94}
95
96static int kstack_depth_to_print = 10;
97
98#ifdef CONFIG_KALLSYMS
99#include <linux/kallsyms.h>
100int 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
117int printk_address(unsigned long address)
118{
119 return printk("[<%016lx>]", address);
120}
121#endif
122
Andi Kleen0a658002005-04-16 15:25:17 -0700123static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Andi Kleen0a658002005-04-16 15:25:17 -0700135 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
136 unsigned long end;
137
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/*
153 * x86-64 can have upto three kernel stacks:
154 * process stack
155 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700156 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158
159void show_trace(unsigned long *stack)
160{
161 unsigned long addr;
Andi Kleen0a658002005-04-16 15:25:17 -0700162 const unsigned cpu = safe_smp_processor_id();
163 unsigned long *irqstack_end = (unsigned long *)cpu_pda[cpu].irqstackptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int i;
Andi Kleen0a658002005-04-16 15:25:17 -0700165 unsigned used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 printk("\nCall Trace:");
Andi Kleen0a658002005-04-16 15:25:17 -0700168
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)
190
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);
196
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);
208
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 }
Andi Kleen0a658002005-04-16 15:25:17 -0700218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Andi Kleen0a658002005-04-16 15:25:17 -0700220
221 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
222#undef HANDLE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 printk("\n");
224}
225
226void 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);
233
234 // debugging aid: "show_stack(NULL, NULL);" prints the
235 // back trace for this cpu.
236
237 if (rsp == NULL) {
238 if (tsk)
239 rsp = (unsigned long *)tsk->thread.rsp;
240 else
241 rsp = (unsigned long *)&rsp;
242 }
243
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++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700258 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 show_trace((unsigned long *)rsp);
261}
262
263/*
264 * The architecture-independent dump_stack generator
265 */
266void dump_stack(void)
267{
268 unsigned long dummy;
269 show_trace(&dummy);
270}
271
272EXPORT_SYMBOL(dump_stack);
273
274void 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;
281
282 rsp = regs->rsp;
283
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);
288
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) {
294
295 printk("Stack: ");
296 show_stack(NULL, (unsigned long*)rsp);
297
298 printk("\nCode: ");
299 if(regs->rip < PAGE_OFFSET)
300 goto bad;
301
302 for(i=0;i<20;i++)
303 {
304 unsigned char c;
305 if(__get_user(c, &((unsigned char*)regs->rip)[i])) {
306bad:
307 printk(" Bad RIP value.");
308 break;
309 }
310 printk("%02x ", c);
311 }
312 }
313 printk("\n");
314}
315
316void handle_BUG(struct pt_regs *regs)
317{
318 struct bug_frame f;
319 char tmp;
320
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}
334
335void out_of_line_bug(void)
336{
337 BUG();
338}
339
340static DEFINE_SPINLOCK(die_lock);
341static int die_owner = -1;
342
343void oops_begin(void)
344{
345 int cpu = safe_smp_processor_id();
346 /* racy, but better than risking deadlock. */
347 local_irq_disable();
348 if (!spin_trylock(&die_lock)) {
349 if (cpu == die_owner)
350 /* nested oops. should stop eventually */;
351 else
352 spin_lock(&die_lock);
353 }
354 die_owner = cpu;
355 console_verbose();
356 bust_spinlocks(1);
357}
358
359void oops_end(void)
360{
361 die_owner = -1;
362 bust_spinlocks(0);
363 spin_unlock(&die_lock);
364 if (panic_on_oops)
365 panic("Oops");
366}
367
368void __die(const char * str, struct pt_regs * regs, long err)
369{
370 static int die_counter;
371 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
372#ifdef CONFIG_PREEMPT
373 printk("PREEMPT ");
374#endif
375#ifdef CONFIG_SMP
376 printk("SMP ");
377#endif
378#ifdef CONFIG_DEBUG_PAGEALLOC
379 printk("DEBUG_PAGEALLOC");
380#endif
381 printk("\n");
382 notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
383 show_registers(regs);
384 /* Executive summary in case the oops scrolled away */
385 printk(KERN_ALERT "RIP ");
386 printk_address(regs->rip);
387 printk(" RSP <%016lx>\n", regs->rsp);
388}
389
390void die(const char * str, struct pt_regs * regs, long err)
391{
392 oops_begin();
393 handle_BUG(regs);
394 __die(str, regs, err);
395 oops_end();
396 do_exit(SIGSEGV);
397}
398static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
399{
400 if (!(regs->eflags & VM_MASK) && (regs->cs == __KERNEL_CS))
401 die(str, regs, err);
402}
403
404void die_nmi(char *str, struct pt_regs *regs)
405{
406 oops_begin();
407 /*
408 * We are in trouble anyway, lets at least try
409 * to get a message out.
410 */
411 printk(str, safe_smp_processor_id());
412 show_registers(regs);
413 if (panic_on_timeout || panic_on_oops)
414 panic("nmi watchdog");
415 printk("console shuts up ...\n");
416 oops_end();
417 do_exit(SIGSEGV);
418}
419
420static void do_trap(int trapnr, int signr, char *str,
421 struct pt_regs * regs, long error_code, siginfo_t *info)
422{
423 conditional_sti(regs);
424
425#ifdef CONFIG_CHECKING
426 {
427 unsigned long gs;
428 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
429 rdmsrl(MSR_GS_BASE, gs);
430 if (gs != (unsigned long)pda) {
431 wrmsrl(MSR_GS_BASE, pda);
432 printk("%s: wrong gs %lx expected %p rip %lx\n", str, gs, pda,
433 regs->rip);
434 }
435 }
436#endif
437
438 if ((regs->cs & 3) != 0) {
439 struct task_struct *tsk = current;
440
441 if (exception_trace && unhandled_signal(tsk, signr))
442 printk(KERN_INFO
443 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
444 tsk->comm, tsk->pid, str,
445 regs->rip,regs->rsp,error_code);
446
447 tsk->thread.error_code = error_code;
448 tsk->thread.trap_no = trapnr;
449 if (info)
450 force_sig_info(signr, info, tsk);
451 else
452 force_sig(signr, tsk);
453 return;
454 }
455
456
457 /* kernel trap */
458 {
459 const struct exception_table_entry *fixup;
460 fixup = search_exception_tables(regs->rip);
461 if (fixup) {
462 regs->rip = fixup->fixup;
463 } else
464 die(str, regs, error_code);
465 return;
466 }
467}
468
469#define DO_ERROR(trapnr, signr, str, name) \
470asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
471{ \
472 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
473 == NOTIFY_STOP) \
474 return; \
475 do_trap(trapnr, signr, str, regs, error_code, NULL); \
476}
477
478#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
479asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
480{ \
481 siginfo_t info; \
482 info.si_signo = signr; \
483 info.si_errno = 0; \
484 info.si_code = sicode; \
485 info.si_addr = (void __user *)siaddr; \
486 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
487 == NOTIFY_STOP) \
488 return; \
489 do_trap(trapnr, signr, str, regs, error_code, &info); \
490}
491
492DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
493DO_ERROR( 4, SIGSEGV, "overflow", overflow)
494DO_ERROR( 5, SIGSEGV, "bounds", bounds)
495DO_ERROR_INFO( 6, SIGILL, "invalid operand", invalid_op, ILL_ILLOPN, regs->rip)
496DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
497DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
498DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
499DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
500DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
501DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700502DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
503DO_ERROR( 8, SIGSEGV, "double fault", double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
506{
507 conditional_sti(regs);
508
509#ifdef CONFIG_CHECKING
510 {
511 unsigned long gs;
512 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
513 rdmsrl(MSR_GS_BASE, gs);
514 if (gs != (unsigned long)pda) {
515 wrmsrl(MSR_GS_BASE, pda);
516 oops_in_progress++;
517 printk("general protection handler: wrong gs %lx expected %p\n", gs, pda);
518 oops_in_progress--;
519 }
520 }
521#endif
522
523 if ((regs->cs & 3)!=0) {
524 struct task_struct *tsk = current;
525
526 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
527 printk(KERN_INFO
528 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
529 tsk->comm, tsk->pid,
530 regs->rip,regs->rsp,error_code);
531
532 tsk->thread.error_code = error_code;
533 tsk->thread.trap_no = 13;
534 force_sig(SIGSEGV, tsk);
535 return;
536 }
537
538 /* kernel gp */
539 {
540 const struct exception_table_entry *fixup;
541 fixup = search_exception_tables(regs->rip);
542 if (fixup) {
543 regs->rip = fixup->fixup;
544 return;
545 }
546 if (notify_die(DIE_GPF, "general protection fault", regs,
547 error_code, 13, SIGSEGV) == NOTIFY_STOP)
548 return;
549 die("general protection fault", regs, error_code);
550 }
551}
552
553static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
554{
555 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
556 printk("You probably have a hardware problem with your RAM chips\n");
557
558 /* Clear and disable the memory parity error line. */
559 reason = (reason & 0xf) | 4;
560 outb(reason, 0x61);
561}
562
563static void io_check_error(unsigned char reason, struct pt_regs * regs)
564{
565 printk("NMI: IOCK error (debug interrupt?)\n");
566 show_registers(regs);
567
568 /* Re-enable the IOCK line, wait for a few seconds */
569 reason = (reason & 0xf) | 8;
570 outb(reason, 0x61);
571 mdelay(2000);
572 reason &= ~8;
573 outb(reason, 0x61);
574}
575
576static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
577{ printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
578 printk("Dazed and confused, but trying to continue\n");
579 printk("Do you have a strange power saving mode enabled?\n");
580}
581
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700582/* Runs on IST stack. This code must keep interrupts off all the time.
583 Nested NMIs are prevented by the CPU. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584asmlinkage void default_do_nmi(struct pt_regs *regs)
585{
586 unsigned char reason = 0;
587
588 /* Only the BSP gets external NMIs from the system. */
589 if (!smp_processor_id())
590 reason = get_nmi_reason();
591
592 if (!(reason & 0xc0)) {
593 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT)
594 == NOTIFY_STOP)
595 return;
596#ifdef CONFIG_X86_LOCAL_APIC
597 /*
598 * Ok, so this is none of the documented NMI sources,
599 * so it must be the NMI watchdog.
600 */
601 if (nmi_watchdog > 0) {
602 nmi_watchdog_tick(regs,reason);
603 return;
604 }
605#endif
606 unknown_nmi_error(reason, regs);
607 return;
608 }
609 if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_STOP)
610 return;
611
612 /* AK: following checks seem to be broken on modern chipsets. FIXME */
613
614 if (reason & 0x80)
615 mem_parity_error(reason, regs);
616 if (reason & 0x40)
617 io_check_error(reason, regs);
618}
619
620asmlinkage void do_int3(struct pt_regs * regs, long error_code)
621{
622 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
623 return;
624 }
625 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
626 return;
627}
628
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700629/* Help handler running on IST stack to switch back to user stack
630 for scheduling or signal handling. The actual stack switch is done in
631 entry.S */
632asmlinkage struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700634 struct pt_regs *regs = eregs;
635 /* Did already sync */
636 if (eregs == (struct pt_regs *)eregs->rsp)
637 ;
638 /* Exception from user space */
639 else if (eregs->cs & 3)
640 regs = ((struct pt_regs *)current->thread.rsp0) - 1;
641 /* Exception from kernel and interrupts are enabled. Move to
642 kernel process stack. */
643 else if (eregs->eflags & X86_EFLAGS_IF)
644 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
645 if (eregs != regs)
646 *regs = *eregs;
647 return regs;
648}
649
650/* runs on IST stack. */
651asmlinkage void do_debug(struct pt_regs * regs, unsigned long error_code)
652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned long condition;
654 struct task_struct *tsk = current;
655 siginfo_t info;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#ifdef CONFIG_CHECKING
658 {
659 /* RED-PEN interaction with debugger - could destroy gs */
660 unsigned long gs;
661 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
662 rdmsrl(MSR_GS_BASE, gs);
663 if (gs != (unsigned long)pda) {
664 wrmsrl(MSR_GS_BASE, pda);
665 printk("debug handler: wrong gs %lx expected %p\n", gs, pda);
666 }
667 }
668#endif
669
670 asm("movq %%db6,%0" : "=r" (condition));
671
672 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700673 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700674 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 conditional_sti(regs);
677
678 /* Mask out spurious debug traps due to lazy DR7 setting */
679 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
680 if (!tsk->thread.debugreg7) {
681 goto clear_dr7;
682 }
683 }
684
685 tsk->thread.debugreg6 = condition;
686
687 /* Mask out spurious TF errors due to lazy TF clearing */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700688 if (condition & DR_STEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * The TF error should be masked out only if the current
691 * process is not traced and if the TRAP flag has been set
692 * previously by a tracing process (condition detected by
693 * the PT_DTRACE flag); remember that the i386 TRAP flag
694 * can be modified by the process itself in user mode,
695 * allowing programs to debug themselves without the ptrace()
696 * interface.
697 */
698 if ((regs->cs & 3) == 0)
699 goto clear_TF_reenable;
Andi Kleenbe61bff2005-04-16 15:24:57 -0700700 /*
701 * Was the TF flag set by a debugger? If so, clear it now,
702 * so that register information is correct.
703 */
704 if (tsk->ptrace & PT_DTRACE) {
705 regs->eflags &= ~TF_MASK;
706 tsk->ptrace &= ~PT_DTRACE;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 /* Ok, finally something we can handle */
711 tsk->thread.trap_no = 1;
712 tsk->thread.error_code = error_code;
713 info.si_signo = SIGTRAP;
714 info.si_errno = 0;
715 info.si_code = TRAP_BRKPT;
716 if ((regs->cs & 3) == 0)
717 goto clear_dr7;
718
719 info.si_addr = (void __user *)regs->rip;
720 force_sig_info(SIGTRAP, &info, tsk);
721clear_dr7:
722 asm volatile("movq %0,%%db7"::"r"(0UL));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725clear_TF_reenable:
726 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 regs->eflags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730static int kernel_math_error(struct pt_regs *regs, char *str)
731{
732 const struct exception_table_entry *fixup;
733 fixup = search_exception_tables(regs->rip);
734 if (fixup) {
735 regs->rip = fixup->fixup;
736 return 1;
737 }
738 notify_die(DIE_GPF, str, regs, 0, 16, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700739 /* Illegal floating point operation in the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return 0;
742}
743
744/*
745 * Note that we play around with the 'TS' bit in an attempt to get
746 * the correct behaviour even in the presence of the asynchronous
747 * IRQ13 behaviour
748 */
749asmlinkage void do_coprocessor_error(struct pt_regs *regs)
750{
751 void __user *rip = (void __user *)(regs->rip);
752 struct task_struct * task;
753 siginfo_t info;
754 unsigned short cwd, swd;
755
756 conditional_sti(regs);
757 if ((regs->cs & 3) == 0 &&
758 kernel_math_error(regs, "kernel x87 math error"))
759 return;
760
761 /*
762 * Save the info for the exception handler and clear the error.
763 */
764 task = current;
765 save_init_fpu(task);
766 task->thread.trap_no = 16;
767 task->thread.error_code = 0;
768 info.si_signo = SIGFPE;
769 info.si_errno = 0;
770 info.si_code = __SI_FAULT;
771 info.si_addr = rip;
772 /*
773 * (~cwd & swd) will mask out exceptions that are not set to unmasked
774 * status. 0x3f is the exception bits in these regs, 0x200 is the
775 * C1 reg you need in case of a stack fault, 0x040 is the stack
776 * fault bit. We should only be taking one exception at a time,
777 * so if this combination doesn't produce any single exception,
778 * then we have a bad program that isn't synchronizing its FPU usage
779 * and it will suffer the consequences since we won't be able to
780 * fully reproduce the context of the exception
781 */
782 cwd = get_fpu_cwd(task);
783 swd = get_fpu_swd(task);
784 switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
785 case 0x000:
786 default:
787 break;
788 case 0x001: /* Invalid Op */
789 case 0x041: /* Stack Fault */
790 case 0x241: /* Stack Fault | Direction */
791 info.si_code = FPE_FLTINV;
792 break;
793 case 0x002: /* Denormalize */
794 case 0x010: /* Underflow */
795 info.si_code = FPE_FLTUND;
796 break;
797 case 0x004: /* Zero Divide */
798 info.si_code = FPE_FLTDIV;
799 break;
800 case 0x008: /* Overflow */
801 info.si_code = FPE_FLTOVF;
802 break;
803 case 0x020: /* Precision */
804 info.si_code = FPE_FLTRES;
805 break;
806 }
807 force_sig_info(SIGFPE, &info, task);
808}
809
810asmlinkage void bad_intr(void)
811{
812 printk("bad interrupt");
813}
814
815asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
816{
817 void __user *rip = (void __user *)(regs->rip);
818 struct task_struct * task;
819 siginfo_t info;
820 unsigned short mxcsr;
821
822 conditional_sti(regs);
823 if ((regs->cs & 3) == 0 &&
Andi Kleen3a848f62005-04-16 15:25:06 -0700824 kernel_math_error(regs, "kernel simd math error"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
826
827 /*
828 * Save the info for the exception handler and clear the error.
829 */
830 task = current;
831 save_init_fpu(task);
832 task->thread.trap_no = 19;
833 task->thread.error_code = 0;
834 info.si_signo = SIGFPE;
835 info.si_errno = 0;
836 info.si_code = __SI_FAULT;
837 info.si_addr = rip;
838 /*
839 * The SIMD FPU exceptions are handled a little differently, as there
840 * is only a single status/control register. Thus, to determine which
841 * unmasked exception was caught we must mask the exception mask bits
842 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
843 */
844 mxcsr = get_fpu_mxcsr(task);
845 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
846 case 0x000:
847 default:
848 break;
849 case 0x001: /* Invalid Op */
850 info.si_code = FPE_FLTINV;
851 break;
852 case 0x002: /* Denormalize */
853 case 0x010: /* Underflow */
854 info.si_code = FPE_FLTUND;
855 break;
856 case 0x004: /* Zero Divide */
857 info.si_code = FPE_FLTDIV;
858 break;
859 case 0x008: /* Overflow */
860 info.si_code = FPE_FLTOVF;
861 break;
862 case 0x020: /* Precision */
863 info.si_code = FPE_FLTRES;
864 break;
865 }
866 force_sig_info(SIGFPE, &info, task);
867}
868
869asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
870{
871}
872
873asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
874{
875}
876
877/*
878 * 'math_state_restore()' saves the current math information in the
879 * old math state array, and gets the new ones from the current task
880 *
881 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
882 * Don't touch unless you *really* know how it works.
883 */
884asmlinkage void math_state_restore(void)
885{
886 struct task_struct *me = current;
887 clts(); /* Allow maths ops (or we recurse) */
888
889 if (!used_math())
890 init_fpu(me);
891 restore_fpu_checking(&me->thread.i387.fxsave);
892 me->thread_info->status |= TS_USEDFPU;
893}
894
895void do_call_debug(struct pt_regs *regs)
896{
897 notify_die(DIE_CALL, "debug call", regs, 0, 255, SIGINT);
898}
899
900void __init trap_init(void)
901{
902 set_intr_gate(0,&divide_error);
903 set_intr_gate_ist(1,&debug,DEBUG_STACK);
904 set_intr_gate_ist(2,&nmi,NMI_STACK);
905 set_system_gate(3,&int3);
906 set_system_gate(4,&overflow); /* int4-5 can be called from all */
907 set_system_gate(5,&bounds);
908 set_intr_gate(6,&invalid_op);
909 set_intr_gate(7,&device_not_available);
910 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
911 set_intr_gate(9,&coprocessor_segment_overrun);
912 set_intr_gate(10,&invalid_TSS);
913 set_intr_gate(11,&segment_not_present);
914 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
915 set_intr_gate(13,&general_protection);
916 set_intr_gate(14,&page_fault);
917 set_intr_gate(15,&spurious_interrupt_bug);
918 set_intr_gate(16,&coprocessor_error);
919 set_intr_gate(17,&alignment_check);
920#ifdef CONFIG_X86_MCE
921 set_intr_gate_ist(18,&machine_check, MCE_STACK);
922#endif
923 set_intr_gate(19,&simd_coprocessor_error);
924
925#ifdef CONFIG_IA32_EMULATION
926 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
927#endif
928
929 set_intr_gate(KDB_VECTOR, call_debug);
930
931 /*
932 * Should be a barrier for any external CPU state.
933 */
934 cpu_init();
935}
936
937
938/* Actual parsing is done early in setup.c. */
939static int __init oops_dummy(char *s)
940{
941 panic_on_oops = 1;
942 return -1;
943}
944__setup("oops=", oops_dummy);
945
946static int __init kstack_setup(char *s)
947{
948 kstack_depth_to_print = simple_strtoul(s,NULL,0);
949 return 0;
950}
951__setup("kstack=", kstack_setup);
952