[PATCH] non lazy "sleazy" fpu implementation
[deliverable/linux.git] / arch / x86_64 / kernel / traps.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
9 */
10
11/*
12 * 'Traps.c' handles hardware traps and faults after we have saved some
13 * state in 'entry.S'.
14 */
1da177e4
LT
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>
35faa714 28#include <linux/nmi.h>
0f2fbdcb 29#include <linux/kprobes.h>
8bcc5280 30#include <linux/kexec.h>
b538ed27 31#include <linux/unwind.h>
1da177e4
LT
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>
b538ed27 42#include <asm/unwind.h>
1da177e4
LT
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>
c0b766f1 48#include <asm/stacktrace.h>
1da177e4 49
1da177e4
LT
50asmlinkage void divide_error(void);
51asmlinkage void debug(void);
52asmlinkage void nmi(void);
53asmlinkage void int3(void);
54asmlinkage void overflow(void);
55asmlinkage void bounds(void);
56asmlinkage void invalid_op(void);
57asmlinkage void device_not_available(void);
58asmlinkage void double_fault(void);
59asmlinkage void coprocessor_segment_overrun(void);
60asmlinkage void invalid_TSS(void);
61asmlinkage void segment_not_present(void);
62asmlinkage void stack_segment(void);
63asmlinkage void general_protection(void);
64asmlinkage void page_fault(void);
65asmlinkage void coprocessor_error(void);
66asmlinkage void simd_coprocessor_error(void);
67asmlinkage void reserved(void);
68asmlinkage void alignment_check(void);
69asmlinkage void machine_check(void);
70asmlinkage void spurious_interrupt_bug(void);
1da177e4 71
e041c683 72ATOMIC_NOTIFIER_HEAD(die_chain);
2ee60e17 73EXPORT_SYMBOL(die_chain);
1da177e4
LT
74
75int register_die_notifier(struct notifier_block *nb)
76{
8c914cb7 77 vmalloc_sync_all();
e041c683
AS
78 return atomic_notifier_chain_register(&die_chain, nb);
79}
1454aed9 80EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
e041c683
AS
81
82int unregister_die_notifier(struct notifier_block *nb)
83{
84 return atomic_notifier_chain_unregister(&die_chain, nb);
1da177e4 85}
1454aed9 86EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
1da177e4
LT
87
88static inline void conditional_sti(struct pt_regs *regs)
89{
90 if (regs->eflags & X86_EFLAGS_IF)
91 local_irq_enable();
92}
93
a65d17c9
JB
94static inline void preempt_conditional_sti(struct pt_regs *regs)
95{
96 preempt_disable();
97 if (regs->eflags & X86_EFLAGS_IF)
98 local_irq_enable();
99}
100
101static inline void preempt_conditional_cli(struct pt_regs *regs)
102{
103 if (regs->eflags & X86_EFLAGS_IF)
104 local_irq_disable();
40e59a61
AK
105 /* Make sure to not schedule here because we could be running
106 on an exception stack. */
a65d17c9
JB
107 preempt_enable_no_resched();
108}
109
cab093b9 110static int kstack_depth_to_print = 12;
ea424055 111#ifdef CONFIG_STACK_UNWIND
c33bd9aa 112static int call_trace = 1;
ea424055
JB
113#else
114#define call_trace (-1)
115#endif
1da177e4
LT
116
117#ifdef CONFIG_KALLSYMS
3ac94932
IM
118# include <linux/kallsyms.h>
119void printk_address(unsigned long address)
120{
1da177e4
LT
121 unsigned long offset = 0, symsize;
122 const char *symname;
123 char *modname;
3ac94932 124 char *delim = ":";
1da177e4
LT
125 char namebuf[128];
126
3ac94932
IM
127 symname = kallsyms_lookup(address, &symsize, &offset,
128 &modname, namebuf);
129 if (!symname) {
130 printk(" [<%016lx>]\n", address);
131 return;
132 }
133 if (!modname)
1da177e4 134 modname = delim = "";
3ac94932
IM
135 printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
136 address, delim, modname, delim, symname, offset, symsize);
137}
1da177e4 138#else
3ac94932
IM
139void printk_address(unsigned long address)
140{
141 printk(" [<%016lx>]\n", address);
142}
1da177e4
LT
143#endif
144
0a658002 145static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
c0b766f1 146 unsigned *usedp, char **idp)
0a658002 147{
b556b35e 148 static char ids[][8] = {
0a658002
AK
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",
b556b35e
JB
154#if DEBUG_STKSZ > EXCEPTION_STKSZ
155 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
156#endif
0a658002
AK
157 };
158 unsigned k;
1da177e4 159
c9ca1ba5
IM
160 /*
161 * Iterate over all exception stacks, and figure out whether
162 * 'stack' is in one of them:
163 */
0a658002
AK
164 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
165 unsigned long end;
166
c9ca1ba5
IM
167 /*
168 * set 'end' to the end of the exception stack.
169 */
b556b35e 170 switch (k + 1) {
c9ca1ba5
IM
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 */
b556b35e
JB
176#if DEBUG_STKSZ > EXCEPTION_STKSZ
177 case DEBUG_STACK:
df79efde 178 end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ;
b556b35e
JB
179 break;
180#endif
181 default:
01ebb77b 182 end = per_cpu(orig_ist, cpu).ist[k];
b556b35e
JB
183 break;
184 }
c9ca1ba5
IM
185 /*
186 * Is 'stack' above this exception frame's end?
187 * If yes then skip to the next frame.
188 */
0a658002
AK
189 if (stack >= end)
190 continue;
c9ca1ba5
IM
191 /*
192 * Is 'stack' above this exception frame's start address?
193 * If yes then we found the right frame.
194 */
0a658002 195 if (stack >= end - EXCEPTION_STKSZ) {
c9ca1ba5
IM
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 */
0a658002
AK
202 if (*usedp & (1U << k))
203 break;
204 *usedp |= 1U << k;
205 *idp = ids[k];
206 return (unsigned long *)end;
207 }
c9ca1ba5
IM
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 */
b556b35e
JB
213#if DEBUG_STKSZ > EXCEPTION_STKSZ
214 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
215 unsigned j = N_EXCEPTION_STACKS - 1;
216
c9ca1ba5
IM
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 */
b556b35e
JB
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
1da177e4
LT
234 }
235 return NULL;
0a658002 236}
1da177e4 237
c0b766f1
AK
238struct ops_and_data {
239 struct stacktrace_ops *ops;
240 void *data;
241};
242
243static int dump_trace_unwind(struct unwind_frame_info *info, void *context)
b538ed27 244{
c0b766f1 245 struct ops_and_data *oad = (struct ops_and_data *)context;
3ac94932 246 int n = 0;
b538ed27
JB
247
248 while (unwind(info) == 0 && UNW_PC(info)) {
3ac94932 249 n++;
c0b766f1 250 oad->ops->address(oad->data, UNW_PC(info));
b538ed27
JB
251 if (arch_unw_user_mode(info))
252 break;
253 }
c33bd9aa 254 return n;
b538ed27
JB
255}
256
1da177e4
LT
257/*
258 * x86-64 can have upto three kernel stacks:
259 * process stack
260 * interrupt stack
0a658002 261 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
1da177e4
LT
262 */
263
c0b766f1
AK
264void dump_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long * stack,
265 struct stacktrace_ops *ops, void *data)
1da177e4 266{
0a658002 267 const unsigned cpu = safe_smp_processor_id();
df79efde 268 unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
0a658002 269 unsigned used = 0;
1da177e4 270
b538ed27
JB
271 if (!tsk)
272 tsk = current;
273
c33bd9aa
JB
274 if (call_trace >= 0) {
275 int unw_ret = 0;
276 struct unwind_frame_info info;
c0b766f1 277 struct ops_and_data oad = { .ops = ops, .data = data };
c33bd9aa
JB
278
279 if (regs) {
280 if (unwind_init_frame_info(&info, tsk, regs) == 0)
c0b766f1 281 unw_ret = dump_trace_unwind(&info, &oad);
c33bd9aa 282 } else if (tsk == current)
c0b766f1 283 unw_ret = unwind_init_running(&info, dump_trace_unwind, &oad);
c33bd9aa
JB
284 else {
285 if (unwind_init_blocked(&info, tsk) == 0)
c0b766f1 286 unw_ret = dump_trace_unwind(&info, &oad);
b538ed27 287 }
ea424055
JB
288 if (unw_ret > 0) {
289 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
c0b766f1 290 ops->warning_symbol(data, "DWARF2 unwinder stuck at %s\n",
ea424055
JB
291 UNW_PC(&info));
292 if ((long)UNW_SP(&info) < 0) {
c0b766f1 293 ops->warning(data, "Leftover inexact backtrace:\n");
ea424055 294 stack = (unsigned long *)UNW_SP(&info);
be7a9170
AK
295 if (!stack)
296 return;
ea424055 297 } else
c0b766f1 298 ops->warning(data, "Full inexact backtrace again:\n");
ea424055 299 } else if (call_trace >= 1)
c33bd9aa 300 return;
b13761ec 301 else
c0b766f1 302 ops->warning(data, "Full inexact backtrace again:\n");
ea424055 303 } else
c0b766f1
AK
304 ops->warning(data, "Inexact backtrace:\n");
305 }
306 if (!stack) {
307 unsigned long dummy;
308 stack = &dummy;
309 if (tsk && tsk != current)
310 stack = (unsigned long *)tsk->thread.rsp;
b538ed27
JB
311 }
312
c9ca1ba5
IM
313 /*
314 * Print function call entries within a stack. 'cond' is the
315 * "end of stackframe" condition, that the 'stack++'
316 * iteration will eventually trigger.
317 */
0a658002
AK
318#define HANDLE_STACK(cond) \
319 do while (cond) { \
1b2f6304 320 unsigned long addr = *stack++; \
0a658002
AK
321 if (kernel_text_address(addr)) { \
322 /* \
323 * If the address is either in the text segment of the \
324 * kernel, or in the region which contains vmalloc'ed \
325 * memory, it *may* be the address of a calling \
326 * routine; if so, print it so that someone tracing \
327 * down the cause of the crash will be able to figure \
328 * out the call path that was taken. \
329 */ \
c0b766f1 330 ops->address(data, addr); \
0a658002
AK
331 } \
332 } while (0)
333
c9ca1ba5
IM
334 /*
335 * Print function call entries in all stacks, starting at the
336 * current stack address. If the stacks consist of nested
337 * exceptions
338 */
c0b766f1
AK
339 for (;;) {
340 char *id;
0a658002
AK
341 unsigned long *estack_end;
342 estack_end = in_exception_stack(cpu, (unsigned long)stack,
343 &used, &id);
344
345 if (estack_end) {
c0b766f1
AK
346 if (ops->stack(data, id) < 0)
347 break;
0a658002 348 HANDLE_STACK (stack < estack_end);
c0b766f1 349 ops->stack(data, "<EOE>");
c9ca1ba5
IM
350 /*
351 * We link to the next stack via the
352 * second-to-last pointer (index -2 to end) in the
353 * exception stack:
354 */
0a658002
AK
355 stack = (unsigned long *) estack_end[-2];
356 continue;
1da177e4 357 }
0a658002
AK
358 if (irqstack_end) {
359 unsigned long *irqstack;
360 irqstack = irqstack_end -
361 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
362
363 if (stack >= irqstack && stack < irqstack_end) {
c0b766f1
AK
364 if (ops->stack(data, "IRQ") < 0)
365 break;
0a658002 366 HANDLE_STACK (stack < irqstack_end);
c9ca1ba5
IM
367 /*
368 * We link to the next stack (which would be
369 * the process stack normally) the last
370 * pointer (index -1 to end) in the IRQ stack:
371 */
0a658002
AK
372 stack = (unsigned long *) (irqstack_end[-1]);
373 irqstack_end = NULL;
c0b766f1 374 ops->stack(data, "EOI");
0a658002 375 continue;
1da177e4 376 }
1da177e4 377 }
0a658002 378 break;
1da177e4 379 }
0a658002 380
c9ca1ba5 381 /*
c0b766f1 382 * This handles the process stack:
c9ca1ba5 383 */
0a658002
AK
384 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
385#undef HANDLE_STACK
c0b766f1
AK
386}
387EXPORT_SYMBOL(dump_trace);
388
389static void
390print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
391{
392 print_symbol(msg, symbol);
393 printk("\n");
394}
395
396static void print_trace_warning(void *data, char *msg)
397{
398 printk("%s\n", msg);
399}
400
401static int print_trace_stack(void *data, char *name)
402{
403 printk(" <%s> ", name);
404 return 0;
405}
3ac94932 406
c0b766f1
AK
407static void print_trace_address(void *data, unsigned long addr)
408{
409 printk_address(addr);
410}
411
412static struct stacktrace_ops print_trace_ops = {
413 .warning = print_trace_warning,
414 .warning_symbol = print_trace_warning_symbol,
415 .stack = print_trace_stack,
416 .address = print_trace_address,
417};
418
419void
420show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
421{
422 printk("\nCall Trace:\n");
423 dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
1da177e4
LT
424 printk("\n");
425}
426
c0b766f1
AK
427static void
428_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
1da177e4
LT
429{
430 unsigned long *stack;
431 int i;
432 const int cpu = safe_smp_processor_id();
df79efde
RT
433 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
434 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
1da177e4
LT
435
436 // debugging aid: "show_stack(NULL, NULL);" prints the
437 // back trace for this cpu.
438
439 if (rsp == NULL) {
440 if (tsk)
441 rsp = (unsigned long *)tsk->thread.rsp;
442 else
443 rsp = (unsigned long *)&rsp;
444 }
445
446 stack = rsp;
447 for(i=0; i < kstack_depth_to_print; i++) {
448 if (stack >= irqstack && stack <= irqstack_end) {
449 if (stack == irqstack_end) {
450 stack = (unsigned long *) (irqstack_end[-1]);
451 printk(" <EOI> ");
452 }
453 } else {
454 if (((long) stack & (THREAD_SIZE-1)) == 0)
455 break;
456 }
457 if (i && ((i % 4) == 0))
3ac94932
IM
458 printk("\n");
459 printk(" %016lx", *stack++);
35faa714 460 touch_nmi_watchdog();
1da177e4 461 }
b538ed27
JB
462 show_trace(tsk, regs, rsp);
463}
464
465void show_stack(struct task_struct *tsk, unsigned long * rsp)
466{
467 _show_stack(tsk, NULL, rsp);
1da177e4
LT
468}
469
470/*
471 * The architecture-independent dump_stack generator
472 */
473void dump_stack(void)
474{
475 unsigned long dummy;
b538ed27 476 show_trace(NULL, NULL, &dummy);
1da177e4
LT
477}
478
479EXPORT_SYMBOL(dump_stack);
480
481void show_registers(struct pt_regs *regs)
482{
483 int i;
76381fee 484 int in_kernel = !user_mode(regs);
1da177e4
LT
485 unsigned long rsp;
486 const int cpu = safe_smp_processor_id();
df79efde 487 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
1da177e4
LT
488
489 rsp = regs->rsp;
490
491 printk("CPU %d ", cpu);
492 __show_regs(regs);
493 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
e4f17c43 494 cur->comm, cur->pid, task_thread_info(cur), cur);
1da177e4
LT
495
496 /*
497 * When in-kernel, we also print out the stack and code at the
498 * time of the fault..
499 */
500 if (in_kernel) {
501
502 printk("Stack: ");
b538ed27 503 _show_stack(NULL, regs, (unsigned long*)rsp);
1da177e4
LT
504
505 printk("\nCode: ");
2b692a87 506 if (regs->rip < PAGE_OFFSET)
1da177e4
LT
507 goto bad;
508
2b692a87 509 for (i=0; i<20; i++) {
1da177e4 510 unsigned char c;
2b692a87 511 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
1da177e4
LT
512bad:
513 printk(" Bad RIP value.");
514 break;
515 }
516 printk("%02x ", c);
517 }
518 }
519 printk("\n");
520}
521
522void handle_BUG(struct pt_regs *regs)
523{
524 struct bug_frame f;
5f1d189f
JB
525 long len;
526 const char *prefix = "";
1da177e4 527
76381fee 528 if (user_mode(regs))
1da177e4 529 return;
77a75333 530 if (__copy_from_user(&f, (const void __user *) regs->rip,
1da177e4
LT
531 sizeof(struct bug_frame)))
532 return;
049cdefe 533 if (f.filename >= 0 ||
1da177e4
LT
534 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
535 return;
5f1d189f
JB
536 len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1;
537 if (len < 0 || len >= PATH_MAX)
049cdefe 538 f.filename = (int)(long)"unmapped filename";
5f1d189f
JB
539 else if (len > 50) {
540 f.filename += len - 50;
541 prefix = "...";
542 }
1da177e4 543 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
5f1d189f 544 printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line);
1da177e4
LT
545}
546
4f60fdf6 547#ifdef CONFIG_BUG
1da177e4
LT
548void out_of_line_bug(void)
549{
550 BUG();
551}
2ee60e17 552EXPORT_SYMBOL(out_of_line_bug);
4f60fdf6 553#endif
1da177e4
LT
554
555static DEFINE_SPINLOCK(die_lock);
556static int die_owner = -1;
cdc60a4c 557static unsigned int die_nest_count;
1da177e4 558
eddb6fb9 559unsigned __kprobes long oops_begin(void)
1da177e4 560{
1209140c
JB
561 int cpu = safe_smp_processor_id();
562 unsigned long flags;
563
564 /* racy, but better than risking deadlock. */
565 local_irq_save(flags);
1da177e4
LT
566 if (!spin_trylock(&die_lock)) {
567 if (cpu == die_owner)
568 /* nested oops. should stop eventually */;
569 else
1209140c 570 spin_lock(&die_lock);
1da177e4 571 }
cdc60a4c 572 die_nest_count++;
1209140c 573 die_owner = cpu;
1da177e4 574 console_verbose();
1209140c
JB
575 bust_spinlocks(1);
576 return flags;
1da177e4
LT
577}
578
eddb6fb9 579void __kprobes oops_end(unsigned long flags)
1da177e4
LT
580{
581 die_owner = -1;
1209140c 582 bust_spinlocks(0);
cdc60a4c
CM
583 die_nest_count--;
584 if (die_nest_count)
585 /* We still own the lock */
586 local_irq_restore(flags);
587 else
588 /* Nest count reaches zero, release the lock. */
589 spin_unlock_irqrestore(&die_lock, flags);
1da177e4 590 if (panic_on_oops)
012c437d 591 panic("Fatal exception");
1209140c 592}
1da177e4 593
eddb6fb9 594void __kprobes __die(const char * str, struct pt_regs * regs, long err)
1da177e4
LT
595{
596 static int die_counter;
597 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
598#ifdef CONFIG_PREEMPT
599 printk("PREEMPT ");
600#endif
601#ifdef CONFIG_SMP
602 printk("SMP ");
603#endif
604#ifdef CONFIG_DEBUG_PAGEALLOC
605 printk("DEBUG_PAGEALLOC");
606#endif
607 printk("\n");
6e3f3617 608 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
1da177e4
LT
609 show_registers(regs);
610 /* Executive summary in case the oops scrolled away */
611 printk(KERN_ALERT "RIP ");
612 printk_address(regs->rip);
613 printk(" RSP <%016lx>\n", regs->rsp);
8bcc5280
VG
614 if (kexec_should_crash(current))
615 crash_kexec(regs);
1da177e4
LT
616}
617
618void die(const char * str, struct pt_regs * regs, long err)
619{
1209140c
JB
620 unsigned long flags = oops_begin();
621
1da177e4
LT
622 handle_BUG(regs);
623 __die(str, regs, err);
1209140c 624 oops_end(flags);
1da177e4
LT
625 do_exit(SIGSEGV);
626}
1da177e4 627
fac58550 628void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
1da177e4 629{
1209140c
JB
630 unsigned long flags = oops_begin();
631
1da177e4
LT
632 /*
633 * We are in trouble anyway, lets at least try
634 * to get a message out.
635 */
636 printk(str, safe_smp_processor_id());
637 show_registers(regs);
8bcc5280
VG
638 if (kexec_should_crash(current))
639 crash_kexec(regs);
fac58550
AK
640 if (do_panic || panic_on_oops)
641 panic("Non maskable interrupt");
1209140c 642 oops_end(flags);
8b1ffe95
CM
643 nmi_exit();
644 local_irq_enable();
1da177e4
LT
645 do_exit(SIGSEGV);
646}
647
0f2fbdcb
PP
648static void __kprobes do_trap(int trapnr, int signr, char *str,
649 struct pt_regs * regs, long error_code,
650 siginfo_t *info)
1da177e4 651{
6e3f3617
JB
652 struct task_struct *tsk = current;
653
6e3f3617
JB
654 tsk->thread.error_code = error_code;
655 tsk->thread.trap_no = trapnr;
1da177e4 656
6e3f3617 657 if (user_mode(regs)) {
1da177e4
LT
658 if (exception_trace && unhandled_signal(tsk, signr))
659 printk(KERN_INFO
660 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
661 tsk->comm, tsk->pid, str,
2b692a87 662 regs->rip, regs->rsp, error_code);
1da177e4 663
1da177e4
LT
664 if (info)
665 force_sig_info(signr, info, tsk);
666 else
667 force_sig(signr, tsk);
668 return;
669 }
670
671
672 /* kernel trap */
673 {
674 const struct exception_table_entry *fixup;
675 fixup = search_exception_tables(regs->rip);
2b692a87 676 if (fixup)
1da177e4 677 regs->rip = fixup->fixup;
2b692a87 678 else
1da177e4
LT
679 die(str, regs, error_code);
680 return;
681 }
682}
683
684#define DO_ERROR(trapnr, signr, str, name) \
685asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
686{ \
687 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
688 == NOTIFY_STOP) \
689 return; \
40e59a61 690 conditional_sti(regs); \
1da177e4
LT
691 do_trap(trapnr, signr, str, regs, error_code, NULL); \
692}
693
694#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
695asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
696{ \
697 siginfo_t info; \
698 info.si_signo = signr; \
699 info.si_errno = 0; \
700 info.si_code = sicode; \
701 info.si_addr = (void __user *)siaddr; \
702 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
703 == NOTIFY_STOP) \
704 return; \
40e59a61 705 conditional_sti(regs); \
1da177e4
LT
706 do_trap(trapnr, signr, str, regs, error_code, &info); \
707}
708
709DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
710DO_ERROR( 4, SIGSEGV, "overflow", overflow)
711DO_ERROR( 5, SIGSEGV, "bounds", bounds)
100c0e36 712DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
1da177e4
LT
713DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
714DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
715DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
716DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
717DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
718DO_ERROR(18, SIGSEGV, "reserved", reserved)
40e59a61
AK
719
720/* Runs on IST stack */
721asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
722{
723 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
724 12, SIGBUS) == NOTIFY_STOP)
725 return;
726 preempt_conditional_sti(regs);
727 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
728 preempt_conditional_cli(regs);
729}
eca37c18
JB
730
731asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
732{
733 static const char str[] = "double fault";
734 struct task_struct *tsk = current;
735
736 /* Return not checked because double check cannot be ignored */
737 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
738
739 tsk->thread.error_code = error_code;
740 tsk->thread.trap_no = 8;
741
742 /* This is always a kernel trap and never fixable (and thus must
743 never return). */
744 for (;;)
745 die(str, regs, error_code);
746}
1da177e4 747
0f2fbdcb
PP
748asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
749 long error_code)
1da177e4 750{
6e3f3617
JB
751 struct task_struct *tsk = current;
752
1da177e4
LT
753 conditional_sti(regs);
754
6e3f3617
JB
755 tsk->thread.error_code = error_code;
756 tsk->thread.trap_no = 13;
1da177e4 757
6e3f3617 758 if (user_mode(regs)) {
1da177e4
LT
759 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
760 printk(KERN_INFO
761 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
762 tsk->comm, tsk->pid,
2b692a87 763 regs->rip, regs->rsp, error_code);
1da177e4 764
1da177e4
LT
765 force_sig(SIGSEGV, tsk);
766 return;
767 }
768
769 /* kernel gp */
770 {
771 const struct exception_table_entry *fixup;
772 fixup = search_exception_tables(regs->rip);
773 if (fixup) {
774 regs->rip = fixup->fixup;
775 return;
776 }
777 if (notify_die(DIE_GPF, "general protection fault", regs,
778 error_code, 13, SIGSEGV) == NOTIFY_STOP)
779 return;
780 die("general protection fault", regs, error_code);
781 }
782}
783
eddb6fb9
AK
784static __kprobes void
785mem_parity_error(unsigned char reason, struct pt_regs * regs)
1da177e4 786{
c41c5cd3
DZ
787 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
788 reason);
789 printk(KERN_EMERG "You probably have a hardware problem with your "
790 "RAM chips\n");
791
8da5adda 792 if (panic_on_unrecovered_nmi)
c41c5cd3
DZ
793 panic("NMI: Not continuing");
794
795 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
1da177e4
LT
796
797 /* Clear and disable the memory parity error line. */
798 reason = (reason & 0xf) | 4;
799 outb(reason, 0x61);
800}
801
eddb6fb9
AK
802static __kprobes void
803io_check_error(unsigned char reason, struct pt_regs * regs)
1da177e4
LT
804{
805 printk("NMI: IOCK error (debug interrupt?)\n");
806 show_registers(regs);
807
808 /* Re-enable the IOCK line, wait for a few seconds */
809 reason = (reason & 0xf) | 8;
810 outb(reason, 0x61);
811 mdelay(2000);
812 reason &= ~8;
813 outb(reason, 0x61);
814}
815
eddb6fb9
AK
816static __kprobes void
817unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
c41c5cd3
DZ
818{
819 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
820 reason);
821 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
8da5adda
DZ
822
823 if (panic_on_unrecovered_nmi)
c41c5cd3 824 panic("NMI: Not continuing");
8da5adda 825
c41c5cd3 826 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
1da177e4
LT
827}
828
6fefb0d1
AK
829/* Runs on IST stack. This code must keep interrupts off all the time.
830 Nested NMIs are prevented by the CPU. */
eddb6fb9 831asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
1da177e4
LT
832{
833 unsigned char reason = 0;
76e4f660
AR
834 int cpu;
835
836 cpu = smp_processor_id();
1da177e4
LT
837
838 /* Only the BSP gets external NMIs from the system. */
76e4f660 839 if (!cpu)
1da177e4
LT
840 reason = get_nmi_reason();
841
842 if (!(reason & 0xc0)) {
6e3f3617 843 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
1da177e4
LT
844 == NOTIFY_STOP)
845 return;
1da177e4
LT
846 /*
847 * Ok, so this is none of the documented NMI sources,
848 * so it must be the NMI watchdog.
849 */
3adbbcce 850 if (nmi_watchdog_tick(regs,reason))
1da177e4 851 return;
3adbbcce 852 if (!do_nmi_callback(regs,cpu))
3adbbcce
DZ
853 unknown_nmi_error(reason, regs);
854
1da177e4
LT
855 return;
856 }
6e3f3617 857 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
1da177e4
LT
858 return;
859
860 /* AK: following checks seem to be broken on modern chipsets. FIXME */
861
862 if (reason & 0x80)
863 mem_parity_error(reason, regs);
864 if (reason & 0x40)
865 io_check_error(reason, regs);
866}
867
b556b35e 868/* runs on IST stack. */
0f2fbdcb 869asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
1da177e4
LT
870{
871 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
872 return;
873 }
40e59a61 874 preempt_conditional_sti(regs);
1da177e4 875 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
40e59a61 876 preempt_conditional_cli(regs);
1da177e4
LT
877}
878
6fefb0d1
AK
879/* Help handler running on IST stack to switch back to user stack
880 for scheduling or signal handling. The actual stack switch is done in
881 entry.S */
eddb6fb9 882asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
6fefb0d1
AK
883{
884 struct pt_regs *regs = eregs;
885 /* Did already sync */
886 if (eregs == (struct pt_regs *)eregs->rsp)
887 ;
888 /* Exception from user space */
76381fee 889 else if (user_mode(eregs))
bb049232 890 regs = task_pt_regs(current);
6fefb0d1
AK
891 /* Exception from kernel and interrupts are enabled. Move to
892 kernel process stack. */
893 else if (eregs->eflags & X86_EFLAGS_IF)
894 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
895 if (eregs != regs)
896 *regs = *eregs;
897 return regs;
898}
899
1da177e4 900/* runs on IST stack. */
0f2fbdcb
PP
901asmlinkage void __kprobes do_debug(struct pt_regs * regs,
902 unsigned long error_code)
1da177e4 903{
1da177e4
LT
904 unsigned long condition;
905 struct task_struct *tsk = current;
906 siginfo_t info;
907
e9129e56 908 get_debugreg(condition, 6);
1da177e4
LT
909
910 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
daeeafec 911 SIGTRAP) == NOTIFY_STOP)
6fefb0d1 912 return;
daeeafec 913
a65d17c9 914 preempt_conditional_sti(regs);
1da177e4
LT
915
916 /* Mask out spurious debug traps due to lazy DR7 setting */
917 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
918 if (!tsk->thread.debugreg7) {
919 goto clear_dr7;
920 }
921 }
922
923 tsk->thread.debugreg6 = condition;
924
925 /* Mask out spurious TF errors due to lazy TF clearing */
daeeafec 926 if (condition & DR_STEP) {
1da177e4
LT
927 /*
928 * The TF error should be masked out only if the current
929 * process is not traced and if the TRAP flag has been set
930 * previously by a tracing process (condition detected by
931 * the PT_DTRACE flag); remember that the i386 TRAP flag
932 * can be modified by the process itself in user mode,
933 * allowing programs to debug themselves without the ptrace()
934 * interface.
935 */
76381fee 936 if (!user_mode(regs))
1da177e4 937 goto clear_TF_reenable;
be61bff7
AK
938 /*
939 * Was the TF flag set by a debugger? If so, clear it now,
940 * so that register information is correct.
941 */
942 if (tsk->ptrace & PT_DTRACE) {
943 regs->eflags &= ~TF_MASK;
944 tsk->ptrace &= ~PT_DTRACE;
945 }
1da177e4
LT
946 }
947
948 /* Ok, finally something we can handle */
949 tsk->thread.trap_no = 1;
950 tsk->thread.error_code = error_code;
951 info.si_signo = SIGTRAP;
952 info.si_errno = 0;
953 info.si_code = TRAP_BRKPT;
01b8faae
JB
954 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
955 force_sig_info(SIGTRAP, &info, tsk);
1da177e4 956
1da177e4 957clear_dr7:
e9129e56 958 set_debugreg(0UL, 7);
a65d17c9 959 preempt_conditional_cli(regs);
6fefb0d1 960 return;
1da177e4
LT
961
962clear_TF_reenable:
963 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
1da177e4 964 regs->eflags &= ~TF_MASK;
a65d17c9 965 preempt_conditional_cli(regs);
1da177e4
LT
966}
967
6e3f3617 968static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
1da177e4
LT
969{
970 const struct exception_table_entry *fixup;
971 fixup = search_exception_tables(regs->rip);
972 if (fixup) {
973 regs->rip = fixup->fixup;
974 return 1;
975 }
6e3f3617 976 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
3a848f63 977 /* Illegal floating point operation in the kernel */
6e3f3617 978 current->thread.trap_no = trapnr;
1da177e4 979 die(str, regs, 0);
1da177e4
LT
980 return 0;
981}
982
983/*
984 * Note that we play around with the 'TS' bit in an attempt to get
985 * the correct behaviour even in the presence of the asynchronous
986 * IRQ13 behaviour
987 */
988asmlinkage void do_coprocessor_error(struct pt_regs *regs)
989{
990 void __user *rip = (void __user *)(regs->rip);
991 struct task_struct * task;
992 siginfo_t info;
993 unsigned short cwd, swd;
994
995 conditional_sti(regs);
76381fee 996 if (!user_mode(regs) &&
6e3f3617 997 kernel_math_error(regs, "kernel x87 math error", 16))
1da177e4
LT
998 return;
999
1000 /*
1001 * Save the info for the exception handler and clear the error.
1002 */
1003 task = current;
1004 save_init_fpu(task);
1005 task->thread.trap_no = 16;
1006 task->thread.error_code = 0;
1007 info.si_signo = SIGFPE;
1008 info.si_errno = 0;
1009 info.si_code = __SI_FAULT;
1010 info.si_addr = rip;
1011 /*
1012 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1013 * status. 0x3f is the exception bits in these regs, 0x200 is the
1014 * C1 reg you need in case of a stack fault, 0x040 is the stack
1015 * fault bit. We should only be taking one exception at a time,
1016 * so if this combination doesn't produce any single exception,
1017 * then we have a bad program that isn't synchronizing its FPU usage
1018 * and it will suffer the consequences since we won't be able to
1019 * fully reproduce the context of the exception
1020 */
1021 cwd = get_fpu_cwd(task);
1022 swd = get_fpu_swd(task);
ff347b22 1023 switch (swd & ~cwd & 0x3f) {
1da177e4
LT
1024 case 0x000:
1025 default:
1026 break;
1027 case 0x001: /* Invalid Op */
ff347b22
CE
1028 /*
1029 * swd & 0x240 == 0x040: Stack Underflow
1030 * swd & 0x240 == 0x240: Stack Overflow
1031 * User must clear the SF bit (0x40) if set
1032 */
1da177e4
LT
1033 info.si_code = FPE_FLTINV;
1034 break;
1035 case 0x002: /* Denormalize */
1036 case 0x010: /* Underflow */
1037 info.si_code = FPE_FLTUND;
1038 break;
1039 case 0x004: /* Zero Divide */
1040 info.si_code = FPE_FLTDIV;
1041 break;
1042 case 0x008: /* Overflow */
1043 info.si_code = FPE_FLTOVF;
1044 break;
1045 case 0x020: /* Precision */
1046 info.si_code = FPE_FLTRES;
1047 break;
1048 }
1049 force_sig_info(SIGFPE, &info, task);
1050}
1051
1052asmlinkage void bad_intr(void)
1053{
1054 printk("bad interrupt");
1055}
1056
1057asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1058{
1059 void __user *rip = (void __user *)(regs->rip);
1060 struct task_struct * task;
1061 siginfo_t info;
1062 unsigned short mxcsr;
1063
1064 conditional_sti(regs);
76381fee 1065 if (!user_mode(regs) &&
6e3f3617 1066 kernel_math_error(regs, "kernel simd math error", 19))
1da177e4
LT
1067 return;
1068
1069 /*
1070 * Save the info for the exception handler and clear the error.
1071 */
1072 task = current;
1073 save_init_fpu(task);
1074 task->thread.trap_no = 19;
1075 task->thread.error_code = 0;
1076 info.si_signo = SIGFPE;
1077 info.si_errno = 0;
1078 info.si_code = __SI_FAULT;
1079 info.si_addr = rip;
1080 /*
1081 * The SIMD FPU exceptions are handled a little differently, as there
1082 * is only a single status/control register. Thus, to determine which
1083 * unmasked exception was caught we must mask the exception mask bits
1084 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1085 */
1086 mxcsr = get_fpu_mxcsr(task);
1087 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1088 case 0x000:
1089 default:
1090 break;
1091 case 0x001: /* Invalid Op */
1092 info.si_code = FPE_FLTINV;
1093 break;
1094 case 0x002: /* Denormalize */
1095 case 0x010: /* Underflow */
1096 info.si_code = FPE_FLTUND;
1097 break;
1098 case 0x004: /* Zero Divide */
1099 info.si_code = FPE_FLTDIV;
1100 break;
1101 case 0x008: /* Overflow */
1102 info.si_code = FPE_FLTOVF;
1103 break;
1104 case 0x020: /* Precision */
1105 info.si_code = FPE_FLTRES;
1106 break;
1107 }
1108 force_sig_info(SIGFPE, &info, task);
1109}
1110
1111asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1112{
1113}
1114
1115asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
89b831ef
JS
1116{
1117}
1118
1119asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1da177e4
LT
1120{
1121}
1122
1123/*
1124 * 'math_state_restore()' saves the current math information in the
1125 * old math state array, and gets the new ones from the current task
1126 *
1127 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1128 * Don't touch unless you *really* know how it works.
1129 */
1130asmlinkage void math_state_restore(void)
1131{
1132 struct task_struct *me = current;
1133 clts(); /* Allow maths ops (or we recurse) */
1134
1135 if (!used_math())
1136 init_fpu(me);
1137 restore_fpu_checking(&me->thread.i387.fxsave);
e4f17c43 1138 task_thread_info(me)->status |= TS_USEDFPU;
e07e23e1 1139 me->fpu_counter++;
1da177e4
LT
1140}
1141
1da177e4
LT
1142void __init trap_init(void)
1143{
1144 set_intr_gate(0,&divide_error);
1145 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1146 set_intr_gate_ist(2,&nmi,NMI_STACK);
b556b35e 1147 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
0a521588
JB
1148 set_system_gate(4,&overflow); /* int4 can be called from all */
1149 set_intr_gate(5,&bounds);
1da177e4
LT
1150 set_intr_gate(6,&invalid_op);
1151 set_intr_gate(7,&device_not_available);
1152 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1153 set_intr_gate(9,&coprocessor_segment_overrun);
1154 set_intr_gate(10,&invalid_TSS);
1155 set_intr_gate(11,&segment_not_present);
1156 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1157 set_intr_gate(13,&general_protection);
1158 set_intr_gate(14,&page_fault);
1159 set_intr_gate(15,&spurious_interrupt_bug);
1160 set_intr_gate(16,&coprocessor_error);
1161 set_intr_gate(17,&alignment_check);
1162#ifdef CONFIG_X86_MCE
1163 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1164#endif
1165 set_intr_gate(19,&simd_coprocessor_error);
1166
1167#ifdef CONFIG_IA32_EMULATION
1168 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1169#endif
1170
1da177e4
LT
1171 /*
1172 * Should be a barrier for any external CPU state.
1173 */
1174 cpu_init();
1175}
1176
1177
2c8c0e6b 1178static int __init oops_setup(char *s)
1da177e4 1179{
2c8c0e6b
AK
1180 if (!s)
1181 return -EINVAL;
1182 if (!strcmp(s, "panic"))
1183 panic_on_oops = 1;
1184 return 0;
1da177e4 1185}
2c8c0e6b 1186early_param("oops", oops_setup);
1da177e4
LT
1187
1188static int __init kstack_setup(char *s)
1189{
2c8c0e6b
AK
1190 if (!s)
1191 return -EINVAL;
1da177e4 1192 kstack_depth_to_print = simple_strtoul(s,NULL,0);
2c8c0e6b 1193 return 0;
1da177e4 1194}
2c8c0e6b 1195early_param("kstack", kstack_setup);
1da177e4 1196
ea424055 1197#ifdef CONFIG_STACK_UNWIND
c33bd9aa
JB
1198static int __init call_trace_setup(char *s)
1199{
2c8c0e6b
AK
1200 if (!s)
1201 return -EINVAL;
c33bd9aa
JB
1202 if (strcmp(s, "old") == 0)
1203 call_trace = -1;
1204 else if (strcmp(s, "both") == 0)
1205 call_trace = 0;
b13761ec 1206 else if (strcmp(s, "newfallback") == 0)
c33bd9aa 1207 call_trace = 1;
b13761ec
AK
1208 else if (strcmp(s, "new") == 0)
1209 call_trace = 2;
2c8c0e6b 1210 return 0;
c33bd9aa 1211}
2c8c0e6b 1212early_param("call_trace", call_trace_setup);
ea424055 1213#endif
This page took 0.209396 seconds and 5 git commands to generate.