]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - arch/x86/kernel/traps_64.c
kgdb: fix NMI hangs
[mirror_ubuntu-kernels.git] / arch / x86 / kernel / traps_64.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
1da177e4
LT
7 */
8
9/*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'entry.S'.
12 */
1da177e4
LT
13#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/ptrace.h>
18#include <linux/timer.h>
19#include <linux/mm.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
4b0ff1a9 24#include <linux/kallsyms.h>
1da177e4
LT
25#include <linux/module.h>
26#include <linux/moduleparam.h>
35faa714 27#include <linux/nmi.h>
0f2fbdcb 28#include <linux/kprobes.h>
8bcc5280 29#include <linux/kexec.h>
b538ed27 30#include <linux/unwind.h>
ab2bf0c1 31#include <linux/uaccess.h>
c31a0bf3 32#include <linux/bug.h>
1eeb66a1 33#include <linux/kdebug.h>
57c351de 34#include <linux/utsname.h>
1da177e4 35
c0d12172
DJ
36#if defined(CONFIG_EDAC)
37#include <linux/edac.h>
38#endif
39
1da177e4 40#include <asm/system.h>
1da177e4
LT
41#include <asm/io.h>
42#include <asm/atomic.h>
43#include <asm/debugreg.h>
44#include <asm/desc.h>
45#include <asm/i387.h>
1da177e4 46#include <asm/processor.h>
b538ed27 47#include <asm/unwind.h>
1da177e4
LT
48#include <asm/smp.h>
49#include <asm/pgalloc.h>
50#include <asm/pda.h>
51#include <asm/proto.h>
52#include <asm/nmi.h>
c0b766f1 53#include <asm/stacktrace.h>
1da177e4 54
1da177e4
LT
55asmlinkage void divide_error(void);
56asmlinkage void debug(void);
57asmlinkage void nmi(void);
58asmlinkage void int3(void);
59asmlinkage void overflow(void);
60asmlinkage void bounds(void);
61asmlinkage void invalid_op(void);
62asmlinkage void device_not_available(void);
63asmlinkage void double_fault(void);
64asmlinkage void coprocessor_segment_overrun(void);
65asmlinkage void invalid_TSS(void);
66asmlinkage void segment_not_present(void);
67asmlinkage void stack_segment(void);
68asmlinkage void general_protection(void);
69asmlinkage void page_fault(void);
70asmlinkage void coprocessor_error(void);
71asmlinkage void simd_coprocessor_error(void);
72asmlinkage void reserved(void);
73asmlinkage void alignment_check(void);
74asmlinkage void machine_check(void);
75asmlinkage void spurious_interrupt_bug(void);
1da177e4 76
a25bd949
AV
77static unsigned int code_bytes = 64;
78
1da177e4
LT
79static inline void conditional_sti(struct pt_regs *regs)
80{
65ea5b03 81 if (regs->flags & X86_EFLAGS_IF)
1da177e4
LT
82 local_irq_enable();
83}
84
a65d17c9
JB
85static inline void preempt_conditional_sti(struct pt_regs *regs)
86{
e8bff74a 87 inc_preempt_count();
65ea5b03 88 if (regs->flags & X86_EFLAGS_IF)
a65d17c9
JB
89 local_irq_enable();
90}
91
92static inline void preempt_conditional_cli(struct pt_regs *regs)
93{
65ea5b03 94 if (regs->flags & X86_EFLAGS_IF)
a65d17c9 95 local_irq_disable();
40e59a61
AK
96 /* Make sure to not schedule here because we could be running
97 on an exception stack. */
e8bff74a 98 dec_preempt_count();
a65d17c9
JB
99}
100
0741f4d2 101int kstack_depth_to_print = 12;
1da177e4 102
bc850d6b 103void printk_address(unsigned long address, int reliable)
3ac94932 104{
a5ff677c 105#ifdef CONFIG_KALLSYMS
1da177e4
LT
106 unsigned long offset = 0, symsize;
107 const char *symname;
108 char *modname;
3ac94932 109 char *delim = ":";
85e2aeea 110 char namebuf[KSYM_NAME_LEN];
a5ff677c 111 char reliab[4] = "";
1da177e4 112
3ac94932
IM
113 symname = kallsyms_lookup(address, &symsize, &offset,
114 &modname, namebuf);
115 if (!symname) {
116 printk(" [<%016lx>]\n", address);
117 return;
118 }
bc850d6b
AV
119 if (!reliable)
120 strcpy(reliab, "? ");
121
3ac94932 122 if (!modname)
a5ff677c 123 modname = delim = "";
bc850d6b
AV
124 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
125 address, reliab, delim, modname, delim, symname, offset, symsize);
1da177e4 126#else
3ac94932 127 printk(" [<%016lx>]\n", address);
1da177e4 128#endif
a5ff677c 129}
1da177e4 130
0a658002 131static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
c0b766f1 132 unsigned *usedp, char **idp)
0a658002 133{
b556b35e 134 static char ids[][8] = {
0a658002
AK
135 [DEBUG_STACK - 1] = "#DB",
136 [NMI_STACK - 1] = "NMI",
137 [DOUBLEFAULT_STACK - 1] = "#DF",
138 [STACKFAULT_STACK - 1] = "#SS",
139 [MCE_STACK - 1] = "#MC",
b556b35e
JB
140#if DEBUG_STKSZ > EXCEPTION_STKSZ
141 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
142#endif
0a658002
AK
143 };
144 unsigned k;
1da177e4 145
c9ca1ba5
IM
146 /*
147 * Iterate over all exception stacks, and figure out whether
148 * 'stack' is in one of them:
149 */
0a658002 150 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
f5741644 151 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
c9ca1ba5
IM
152 /*
153 * Is 'stack' above this exception frame's end?
154 * If yes then skip to the next frame.
155 */
0a658002
AK
156 if (stack >= end)
157 continue;
c9ca1ba5
IM
158 /*
159 * Is 'stack' above this exception frame's start address?
160 * If yes then we found the right frame.
161 */
0a658002 162 if (stack >= end - EXCEPTION_STKSZ) {
c9ca1ba5
IM
163 /*
164 * Make sure we only iterate through an exception
165 * stack once. If it comes up for the second time
166 * then there's something wrong going on - just
167 * break out and return NULL:
168 */
0a658002
AK
169 if (*usedp & (1U << k))
170 break;
171 *usedp |= 1U << k;
172 *idp = ids[k];
173 return (unsigned long *)end;
174 }
c9ca1ba5
IM
175 /*
176 * If this is a debug stack, and if it has a larger size than
177 * the usual exception stacks, then 'stack' might still
178 * be within the lower portion of the debug stack:
179 */
b556b35e
JB
180#if DEBUG_STKSZ > EXCEPTION_STKSZ
181 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
182 unsigned j = N_EXCEPTION_STACKS - 1;
183
c9ca1ba5
IM
184 /*
185 * Black magic. A large debug stack is composed of
186 * multiple exception stack entries, which we
187 * iterate through now. Dont look:
188 */
b556b35e
JB
189 do {
190 ++j;
191 end -= EXCEPTION_STKSZ;
192 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
193 } while (stack < end - EXCEPTION_STKSZ);
194 if (*usedp & (1U << j))
195 break;
196 *usedp |= 1U << j;
197 *idp = ids[j];
198 return (unsigned long *)end;
199 }
200#endif
1da177e4
LT
201 }
202 return NULL;
0a658002 203}
1da177e4 204
b615ebda
AK
205#define MSG(txt) ops->warning(data, txt)
206
1da177e4 207/*
676b1855 208 * x86-64 can have up to three kernel stacks:
1da177e4
LT
209 * process stack
210 * interrupt stack
0a658002 211 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
1da177e4
LT
212 */
213
e4a94568
AV
214static inline int valid_stack_ptr(struct thread_info *tinfo,
215 void *p, unsigned int size, void *end)
c547c77e 216{
ade1af77 217 void *t = tinfo;
e4a94568
AV
218 if (end) {
219 if (p < end && p >= (end-THREAD_SIZE))
220 return 1;
221 else
222 return 0;
223 }
224 return p > t && p < t + THREAD_SIZE - size;
225}
226
80b51f31
AV
227/* The form of the top of the frame on the stack */
228struct stack_frame {
229 struct stack_frame *next_frame;
230 unsigned long return_address;
231};
232
233
e4a94568
AV
234static inline unsigned long print_context_stack(struct thread_info *tinfo,
235 unsigned long *stack, unsigned long bp,
236 const struct stacktrace_ops *ops, void *data,
237 unsigned long *end)
238{
80b51f31
AV
239 struct stack_frame *frame = (struct stack_frame *)bp;
240
241 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
242 unsigned long addr;
243
244 addr = *stack;
e4a94568 245 if (__kernel_text_address(addr)) {
80b51f31
AV
246 if ((unsigned long) stack == bp + 8) {
247 ops->address(data, addr, 1);
248 frame = frame->next_frame;
249 bp = (unsigned long) frame;
250 } else {
251 ops->address(data, addr, bp == 0);
252 }
e4a94568 253 }
80b51f31 254 stack++;
e4a94568
AV
255 }
256 return bp;
c547c77e
AK
257}
258
b615ebda 259void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
bc850d6b 260 unsigned long *stack, unsigned long bp,
9689ba8a 261 const struct stacktrace_ops *ops, void *data)
1da177e4 262{
da68933e 263 const unsigned cpu = get_cpu();
b615ebda 264 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
0a658002 265 unsigned used = 0;
c547c77e 266 struct thread_info *tinfo;
1da177e4 267
b538ed27
JB
268 if (!tsk)
269 tsk = current;
e4a94568 270 tinfo = task_thread_info(tsk);
b538ed27 271
c0b766f1
AK
272 if (!stack) {
273 unsigned long dummy;
274 stack = &dummy;
275 if (tsk && tsk != current)
faca6227 276 stack = (unsigned long *)tsk->thread.sp;
b538ed27
JB
277 }
278
80b51f31
AV
279#ifdef CONFIG_FRAME_POINTER
280 if (!bp) {
281 if (tsk == current) {
282 /* Grab bp right from our regs */
283 asm("movq %%rbp, %0" : "=r" (bp):);
284 } else {
285 /* bp is the last reg pushed by switch_to */
286 bp = *(unsigned long *) tsk->thread.sp;
287 }
288 }
289#endif
290
291
0a658002 292
c9ca1ba5
IM
293 /*
294 * Print function call entries in all stacks, starting at the
295 * current stack address. If the stacks consist of nested
296 * exceptions
297 */
c0b766f1
AK
298 for (;;) {
299 char *id;
0a658002
AK
300 unsigned long *estack_end;
301 estack_end = in_exception_stack(cpu, (unsigned long)stack,
302 &used, &id);
303
304 if (estack_end) {
c0b766f1
AK
305 if (ops->stack(data, id) < 0)
306 break;
e4a94568 307
80b51f31
AV
308 bp = print_context_stack(tinfo, stack, bp, ops,
309 data, estack_end);
c0b766f1 310 ops->stack(data, "<EOE>");
c9ca1ba5
IM
311 /*
312 * We link to the next stack via the
313 * second-to-last pointer (index -2 to end) in the
314 * exception stack:
315 */
0a658002
AK
316 stack = (unsigned long *) estack_end[-2];
317 continue;
1da177e4 318 }
0a658002
AK
319 if (irqstack_end) {
320 unsigned long *irqstack;
321 irqstack = irqstack_end -
322 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
323
324 if (stack >= irqstack && stack < irqstack_end) {
c0b766f1
AK
325 if (ops->stack(data, "IRQ") < 0)
326 break;
80b51f31
AV
327 bp = print_context_stack(tinfo, stack, bp,
328 ops, data, irqstack_end);
c9ca1ba5
IM
329 /*
330 * We link to the next stack (which would be
331 * the process stack normally) the last
332 * pointer (index -1 to end) in the IRQ stack:
333 */
0a658002
AK
334 stack = (unsigned long *) (irqstack_end[-1]);
335 irqstack_end = NULL;
c0b766f1 336 ops->stack(data, "EOI");
0a658002 337 continue;
1da177e4 338 }
1da177e4 339 }
0a658002 340 break;
1da177e4 341 }
0a658002 342
c9ca1ba5 343 /*
c0b766f1 344 * This handles the process stack:
c9ca1ba5 345 */
80b51f31 346 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
da68933e 347 put_cpu();
c0b766f1
AK
348}
349EXPORT_SYMBOL(dump_trace);
350
351static void
352print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
353{
354 print_symbol(msg, symbol);
355 printk("\n");
356}
357
358static void print_trace_warning(void *data, char *msg)
359{
360 printk("%s\n", msg);
361}
362
363static int print_trace_stack(void *data, char *name)
364{
365 printk(" <%s> ", name);
366 return 0;
367}
3ac94932 368
bc850d6b 369static void print_trace_address(void *data, unsigned long addr, int reliable)
c0b766f1 370{
1c978b93 371 touch_nmi_watchdog();
bc850d6b 372 printk_address(addr, reliable);
c0b766f1
AK
373}
374
9689ba8a 375static const struct stacktrace_ops print_trace_ops = {
c0b766f1
AK
376 .warning = print_trace_warning,
377 .warning_symbol = print_trace_warning_symbol,
378 .stack = print_trace_stack,
379 .address = print_trace_address,
380};
381
382void
bc850d6b
AV
383show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
384 unsigned long bp)
c0b766f1
AK
385{
386 printk("\nCall Trace:\n");
bc850d6b 387 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
1da177e4
LT
388 printk("\n");
389}
390
c0b766f1 391static void
bc850d6b
AV
392_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
393 unsigned long bp)
1da177e4
LT
394{
395 unsigned long *stack;
396 int i;
151f8cc1 397 const int cpu = smp_processor_id();
df79efde
RT
398 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
399 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
1da177e4
LT
400
401 // debugging aid: "show_stack(NULL, NULL);" prints the
402 // back trace for this cpu.
403
65ea5b03 404 if (sp == NULL) {
1da177e4 405 if (tsk)
faca6227 406 sp = (unsigned long *)tsk->thread.sp;
1da177e4 407 else
65ea5b03 408 sp = (unsigned long *)&sp;
1da177e4
LT
409 }
410
65ea5b03 411 stack = sp;
1da177e4
LT
412 for(i=0; i < kstack_depth_to_print; i++) {
413 if (stack >= irqstack && stack <= irqstack_end) {
414 if (stack == irqstack_end) {
415 stack = (unsigned long *) (irqstack_end[-1]);
416 printk(" <EOI> ");
417 }
418 } else {
419 if (((long) stack & (THREAD_SIZE-1)) == 0)
420 break;
421 }
422 if (i && ((i % 4) == 0))
3ac94932
IM
423 printk("\n");
424 printk(" %016lx", *stack++);
35faa714 425 touch_nmi_watchdog();
1da177e4 426 }
bc850d6b 427 show_trace(tsk, regs, sp, bp);
b538ed27
JB
428}
429
65ea5b03 430void show_stack(struct task_struct *tsk, unsigned long * sp)
b538ed27 431{
bc850d6b 432 _show_stack(tsk, NULL, sp, 0);
1da177e4
LT
433}
434
435/*
436 * The architecture-independent dump_stack generator
437 */
438void dump_stack(void)
439{
440 unsigned long dummy;
bc850d6b 441 unsigned long bp = 0;
57c351de 442
80b51f31
AV
443#ifdef CONFIG_FRAME_POINTER
444 if (!bp)
445 asm("movq %%rbp, %0" : "=r" (bp):);
446#endif
447
57c351de
AV
448 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
449 current->pid, current->comm, print_tainted(),
450 init_utsname()->release,
451 (int)strcspn(init_utsname()->version, " "),
452 init_utsname()->version);
bc850d6b 453 show_trace(NULL, NULL, &dummy, bp);
1da177e4
LT
454}
455
456EXPORT_SYMBOL(dump_stack);
457
458void show_registers(struct pt_regs *regs)
459{
460 int i;
65ea5b03 461 unsigned long sp;
151f8cc1 462 const int cpu = smp_processor_id();
df79efde 463 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
a25bd949
AV
464 u8 *ip;
465 unsigned int code_prologue = code_bytes * 43 / 64;
466 unsigned int code_len = code_bytes;
1da177e4 467
65ea5b03 468 sp = regs->sp;
a25bd949 469 ip = (u8 *) regs->ip - code_prologue;
1da177e4
LT
470 printk("CPU %d ", cpu);
471 __show_regs(regs);
472 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
e4f17c43 473 cur->comm, cur->pid, task_thread_info(cur), cur);
1da177e4
LT
474
475 /*
476 * When in-kernel, we also print out the stack and code at the
477 * time of the fault..
478 */
a25bd949
AV
479 if (!user_mode(regs)) {
480 unsigned char c;
1da177e4 481 printk("Stack: ");
bc850d6b 482 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
a25bd949 483 printk("\n");
1da177e4 484
a25bd949
AV
485 printk(KERN_EMERG "Code: ");
486 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
487 /* try starting at RIP */
488 ip = (u8 *) regs->ip;
489 code_len = code_len - code_prologue + 1;
490 }
491 for (i = 0; i < code_len; i++, ip++) {
492 if (ip < (u8 *)PAGE_OFFSET ||
493 probe_kernel_address(ip, c)) {
1da177e4
LT
494 printk(" Bad RIP value.");
495 break;
496 }
a25bd949
AV
497 if (ip == (u8 *)regs->ip)
498 printk("<%02x> ", c);
499 else
500 printk("%02x ", c);
1da177e4
LT
501 }
502 }
503 printk("\n");
504}
505
65ea5b03 506int is_valid_bugaddr(unsigned long ip)
c31a0bf3
JF
507{
508 unsigned short ud2;
509
65ea5b03 510 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
c31a0bf3
JF
511 return 0;
512
513 return ud2 == 0x0b0f;
514}
1da177e4 515
39743c9e 516static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
1da177e4 517static int die_owner = -1;
cdc60a4c 518static unsigned int die_nest_count;
1da177e4 519
eddb6fb9 520unsigned __kprobes long oops_begin(void)
1da177e4 521{
b39b7036 522 int cpu;
1209140c
JB
523 unsigned long flags;
524
abf0f109
AM
525 oops_enter();
526
1209140c 527 /* racy, but better than risking deadlock. */
39743c9e 528 raw_local_irq_save(flags);
b39b7036 529 cpu = smp_processor_id();
39743c9e 530 if (!__raw_spin_trylock(&die_lock)) {
1da177e4
LT
531 if (cpu == die_owner)
532 /* nested oops. should stop eventually */;
533 else
39743c9e 534 __raw_spin_lock(&die_lock);
1da177e4 535 }
cdc60a4c 536 die_nest_count++;
1209140c 537 die_owner = cpu;
1da177e4 538 console_verbose();
1209140c
JB
539 bust_spinlocks(1);
540 return flags;
1da177e4
LT
541}
542
22f5991c 543void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
1da177e4
LT
544{
545 die_owner = -1;
1209140c 546 bust_spinlocks(0);
cdc60a4c 547 die_nest_count--;
39743c9e 548 if (!die_nest_count)
cdc60a4c 549 /* Nest count reaches zero, release the lock. */
39743c9e
AK
550 __raw_spin_unlock(&die_lock);
551 raw_local_irq_restore(flags);
22f5991c
JB
552 if (!regs) {
553 oops_exit();
554 return;
555 }
1da177e4 556 if (panic_on_oops)
012c437d 557 panic("Fatal exception");
abf0f109 558 oops_exit();
22f5991c 559 do_exit(signr);
1209140c 560}
1da177e4 561
22f5991c 562int __kprobes __die(const char * str, struct pt_regs * regs, long err)
1da177e4
LT
563{
564 static int die_counter;
565 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
566#ifdef CONFIG_PREEMPT
567 printk("PREEMPT ");
568#endif
569#ifdef CONFIG_SMP
570 printk("SMP ");
571#endif
572#ifdef CONFIG_DEBUG_PAGEALLOC
573 printk("DEBUG_PAGEALLOC");
574#endif
575 printk("\n");
22f5991c
JB
576 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
577 return 1;
1da177e4 578 show_registers(regs);
bcdcd8e7 579 add_taint(TAINT_DIE);
1da177e4
LT
580 /* Executive summary in case the oops scrolled away */
581 printk(KERN_ALERT "RIP ");
aafbd7eb 582 printk_address(regs->ip, 1);
65ea5b03 583 printk(" RSP <%016lx>\n", regs->sp);
8bcc5280
VG
584 if (kexec_should_crash(current))
585 crash_kexec(regs);
22f5991c 586 return 0;
1da177e4
LT
587}
588
589void die(const char * str, struct pt_regs * regs, long err)
590{
1209140c
JB
591 unsigned long flags = oops_begin();
592
c31a0bf3 593 if (!user_mode(regs))
65ea5b03 594 report_bug(regs->ip, regs);
c31a0bf3 595
22f5991c
JB
596 if (__die(str, regs, err))
597 regs = NULL;
598 oops_end(flags, regs, SIGSEGV);
1da177e4 599}
1da177e4 600
fac58550 601void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
1da177e4 602{
1209140c
JB
603 unsigned long flags = oops_begin();
604
1da177e4
LT
605 /*
606 * We are in trouble anyway, lets at least try
607 * to get a message out.
608 */
151f8cc1 609 printk(str, smp_processor_id());
1da177e4 610 show_registers(regs);
8bcc5280
VG
611 if (kexec_should_crash(current))
612 crash_kexec(regs);
fac58550
AK
613 if (do_panic || panic_on_oops)
614 panic("Non maskable interrupt");
22f5991c 615 oops_end(flags, NULL, SIGBUS);
8b1ffe95
CM
616 nmi_exit();
617 local_irq_enable();
22f5991c 618 do_exit(SIGBUS);
1da177e4
LT
619}
620
0f2fbdcb
PP
621static void __kprobes do_trap(int trapnr, int signr, char *str,
622 struct pt_regs * regs, long error_code,
623 siginfo_t *info)
1da177e4 624{
6e3f3617
JB
625 struct task_struct *tsk = current;
626
6e3f3617 627 if (user_mode(regs)) {
d1895183
AK
628 /*
629 * We want error_code and trap_no set for userspace
630 * faults and kernelspace faults which result in
631 * die(), but not kernelspace faults which are fixed
632 * up. die() gives the process no chance to handle
633 * the signal and notice the kernel fault information,
634 * so that won't result in polluting the information
635 * about previously queued, but not yet delivered,
636 * faults. See also do_general_protection below.
637 */
638 tsk->thread.error_code = error_code;
639 tsk->thread.trap_no = trapnr;
640
abd4f750 641 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
03252919 642 printk_ratelimit()) {
1da177e4 643 printk(KERN_INFO
03252919 644 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
1da177e4 645 tsk->comm, tsk->pid, str,
65ea5b03 646 regs->ip, regs->sp, error_code);
03252919
AK
647 print_vma_addr(" in ", regs->ip);
648 printk("\n");
649 }
1da177e4 650
1da177e4
LT
651 if (info)
652 force_sig_info(signr, info, tsk);
653 else
654 force_sig(signr, tsk);
655 return;
656 }
657
658
b3a5acc1
HH
659 if (!fixup_exception(regs)) {
660 tsk->thread.error_code = error_code;
661 tsk->thread.trap_no = trapnr;
662 die(str, regs, error_code);
1da177e4 663 }
b3a5acc1 664 return;
1da177e4
LT
665}
666
667#define DO_ERROR(trapnr, signr, str, name) \
668asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
669{ \
670 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
671 == NOTIFY_STOP) \
672 return; \
40e59a61 673 conditional_sti(regs); \
1da177e4
LT
674 do_trap(trapnr, signr, str, regs, error_code, NULL); \
675}
676
677#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
678asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
679{ \
680 siginfo_t info; \
681 info.si_signo = signr; \
682 info.si_errno = 0; \
683 info.si_code = sicode; \
684 info.si_addr = (void __user *)siaddr; \
fb1dac90 685 trace_hardirqs_fixup(); \
1da177e4
LT
686 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
687 == NOTIFY_STOP) \
688 return; \
40e59a61 689 conditional_sti(regs); \
1da177e4
LT
690 do_trap(trapnr, signr, str, regs, error_code, &info); \
691}
692
65ea5b03 693DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
1da177e4
LT
694DO_ERROR( 4, SIGSEGV, "overflow", overflow)
695DO_ERROR( 5, SIGSEGV, "bounds", bounds)
65ea5b03 696DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
1da177e4
LT
697DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
698DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
699DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
700DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
701DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
702DO_ERROR(18, SIGSEGV, "reserved", reserved)
40e59a61
AK
703
704/* Runs on IST stack */
705asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
706{
707 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
708 12, SIGBUS) == NOTIFY_STOP)
709 return;
710 preempt_conditional_sti(regs);
711 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
712 preempt_conditional_cli(regs);
713}
eca37c18
JB
714
715asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
716{
717 static const char str[] = "double fault";
718 struct task_struct *tsk = current;
719
720 /* Return not checked because double check cannot be ignored */
721 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
722
723 tsk->thread.error_code = error_code;
724 tsk->thread.trap_no = 8;
725
726 /* This is always a kernel trap and never fixable (and thus must
727 never return). */
728 for (;;)
729 die(str, regs, error_code);
730}
1da177e4 731
0f2fbdcb
PP
732asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
733 long error_code)
1da177e4 734{
6e3f3617
JB
735 struct task_struct *tsk = current;
736
1da177e4
LT
737 conditional_sti(regs);
738
6e3f3617 739 if (user_mode(regs)) {
d1895183
AK
740 tsk->thread.error_code = error_code;
741 tsk->thread.trap_no = 13;
742
abd4f750 743 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
03252919 744 printk_ratelimit()) {
1da177e4 745 printk(KERN_INFO
03252919 746 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
1da177e4 747 tsk->comm, tsk->pid,
65ea5b03 748 regs->ip, regs->sp, error_code);
03252919
AK
749 print_vma_addr(" in ", regs->ip);
750 printk("\n");
751 }
1da177e4 752
1da177e4
LT
753 force_sig(SIGSEGV, tsk);
754 return;
755 }
756
b3a5acc1
HH
757 if (fixup_exception(regs))
758 return;
d1895183 759
b3a5acc1
HH
760 tsk->thread.error_code = error_code;
761 tsk->thread.trap_no = 13;
762 if (notify_die(DIE_GPF, "general protection fault", regs,
763 error_code, 13, SIGSEGV) == NOTIFY_STOP)
764 return;
765 die("general protection fault", regs, error_code);
1da177e4
LT
766}
767
eddb6fb9
AK
768static __kprobes void
769mem_parity_error(unsigned char reason, struct pt_regs * regs)
1da177e4 770{
c41c5cd3
DZ
771 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
772 reason);
9c5f8be4 773 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
c41c5cd3 774
c0d12172
DJ
775#if defined(CONFIG_EDAC)
776 if(edac_handler_set()) {
777 edac_atomic_assert_error();
778 return;
779 }
780#endif
781
8da5adda 782 if (panic_on_unrecovered_nmi)
c41c5cd3
DZ
783 panic("NMI: Not continuing");
784
785 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
1da177e4
LT
786
787 /* Clear and disable the memory parity error line. */
788 reason = (reason & 0xf) | 4;
789 outb(reason, 0x61);
790}
791
eddb6fb9
AK
792static __kprobes void
793io_check_error(unsigned char reason, struct pt_regs * regs)
1da177e4
LT
794{
795 printk("NMI: IOCK error (debug interrupt?)\n");
796 show_registers(regs);
797
798 /* Re-enable the IOCK line, wait for a few seconds */
799 reason = (reason & 0xf) | 8;
800 outb(reason, 0x61);
801 mdelay(2000);
802 reason &= ~8;
803 outb(reason, 0x61);
804}
805
eddb6fb9
AK
806static __kprobes void
807unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
c41c5cd3 808{
d3597524
JW
809 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
810 return;
c41c5cd3
DZ
811 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
812 reason);
813 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
8da5adda
DZ
814
815 if (panic_on_unrecovered_nmi)
c41c5cd3 816 panic("NMI: Not continuing");
8da5adda 817
c41c5cd3 818 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
1da177e4
LT
819}
820
6fefb0d1
AK
821/* Runs on IST stack. This code must keep interrupts off all the time.
822 Nested NMIs are prevented by the CPU. */
eddb6fb9 823asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
1da177e4
LT
824{
825 unsigned char reason = 0;
76e4f660
AR
826 int cpu;
827
828 cpu = smp_processor_id();
1da177e4
LT
829
830 /* Only the BSP gets external NMIs from the system. */
76e4f660 831 if (!cpu)
1da177e4
LT
832 reason = get_nmi_reason();
833
834 if (!(reason & 0xc0)) {
6e3f3617 835 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
1da177e4
LT
836 == NOTIFY_STOP)
837 return;
1da177e4
LT
838 /*
839 * Ok, so this is none of the documented NMI sources,
840 * so it must be the NMI watchdog.
841 */
3adbbcce 842 if (nmi_watchdog_tick(regs,reason))
1da177e4 843 return;
3adbbcce 844 if (!do_nmi_callback(regs,cpu))
3adbbcce
DZ
845 unknown_nmi_error(reason, regs);
846
1da177e4
LT
847 return;
848 }
6e3f3617 849 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
1da177e4
LT
850 return;
851
852 /* AK: following checks seem to be broken on modern chipsets. FIXME */
853
854 if (reason & 0x80)
855 mem_parity_error(reason, regs);
856 if (reason & 0x40)
857 io_check_error(reason, regs);
858}
859
b556b35e 860/* runs on IST stack. */
0f2fbdcb 861asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
1da177e4 862{
143a5d32
PZ
863 trace_hardirqs_fixup();
864
1da177e4
LT
865 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
866 return;
867 }
40e59a61 868 preempt_conditional_sti(regs);
1da177e4 869 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
40e59a61 870 preempt_conditional_cli(regs);
1da177e4
LT
871}
872
6fefb0d1
AK
873/* Help handler running on IST stack to switch back to user stack
874 for scheduling or signal handling. The actual stack switch is done in
875 entry.S */
eddb6fb9 876asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
6fefb0d1
AK
877{
878 struct pt_regs *regs = eregs;
879 /* Did already sync */
65ea5b03 880 if (eregs == (struct pt_regs *)eregs->sp)
6fefb0d1
AK
881 ;
882 /* Exception from user space */
76381fee 883 else if (user_mode(eregs))
bb049232 884 regs = task_pt_regs(current);
6fefb0d1
AK
885 /* Exception from kernel and interrupts are enabled. Move to
886 kernel process stack. */
65ea5b03
PA
887 else if (eregs->flags & X86_EFLAGS_IF)
888 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
6fefb0d1
AK
889 if (eregs != regs)
890 *regs = *eregs;
891 return regs;
892}
893
1da177e4 894/* runs on IST stack. */
0f2fbdcb
PP
895asmlinkage void __kprobes do_debug(struct pt_regs * regs,
896 unsigned long error_code)
1da177e4 897{
1da177e4
LT
898 unsigned long condition;
899 struct task_struct *tsk = current;
900 siginfo_t info;
901
000f4a9e
PZ
902 trace_hardirqs_fixup();
903
e9129e56 904 get_debugreg(condition, 6);
1da177e4 905
10faa81e
RM
906 /*
907 * The processor cleared BTF, so don't mark that we need it set.
908 */
909 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
910 tsk->thread.debugctlmsr = 0;
911
1da177e4 912 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
daeeafec 913 SIGTRAP) == NOTIFY_STOP)
6fefb0d1 914 return;
daeeafec 915
a65d17c9 916 preempt_conditional_sti(regs);
1da177e4
LT
917
918 /* Mask out spurious debug traps due to lazy DR7 setting */
919 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
920 if (!tsk->thread.debugreg7) {
921 goto clear_dr7;
922 }
923 }
924
925 tsk->thread.debugreg6 = condition;
926
e1f28773
RM
927
928 /*
929 * Single-stepping through TF: make sure we ignore any events in
930 * kernel space (but re-enable TF when returning to user mode).
931 */
daeeafec 932 if (condition & DR_STEP) {
76381fee 933 if (!user_mode(regs))
1da177e4 934 goto clear_TF_reenable;
1da177e4
LT
935 }
936
937 /* Ok, finally something we can handle */
938 tsk->thread.trap_no = 1;
939 tsk->thread.error_code = error_code;
940 info.si_signo = SIGTRAP;
941 info.si_errno = 0;
942 info.si_code = TRAP_BRKPT;
65ea5b03 943 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
01b8faae 944 force_sig_info(SIGTRAP, &info, tsk);
1da177e4 945
1da177e4 946clear_dr7:
e9129e56 947 set_debugreg(0UL, 7);
a65d17c9 948 preempt_conditional_cli(regs);
6fefb0d1 949 return;
1da177e4
LT
950
951clear_TF_reenable:
952 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
053de044 953 regs->flags &= ~X86_EFLAGS_TF;
a65d17c9 954 preempt_conditional_cli(regs);
1da177e4
LT
955}
956
6e3f3617 957static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
1da177e4 958{
b3a5acc1 959 if (fixup_exception(regs))
1da177e4 960 return 1;
b3a5acc1 961
6e3f3617 962 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
3a848f63 963 /* Illegal floating point operation in the kernel */
6e3f3617 964 current->thread.trap_no = trapnr;
1da177e4 965 die(str, regs, 0);
1da177e4
LT
966 return 0;
967}
968
969/*
970 * Note that we play around with the 'TS' bit in an attempt to get
971 * the correct behaviour even in the presence of the asynchronous
972 * IRQ13 behaviour
973 */
974asmlinkage void do_coprocessor_error(struct pt_regs *regs)
975{
65ea5b03 976 void __user *ip = (void __user *)(regs->ip);
1da177e4
LT
977 struct task_struct * task;
978 siginfo_t info;
979 unsigned short cwd, swd;
980
981 conditional_sti(regs);
76381fee 982 if (!user_mode(regs) &&
6e3f3617 983 kernel_math_error(regs, "kernel x87 math error", 16))
1da177e4
LT
984 return;
985
986 /*
987 * Save the info for the exception handler and clear the error.
988 */
989 task = current;
990 save_init_fpu(task);
991 task->thread.trap_no = 16;
992 task->thread.error_code = 0;
993 info.si_signo = SIGFPE;
994 info.si_errno = 0;
995 info.si_code = __SI_FAULT;
65ea5b03 996 info.si_addr = ip;
1da177e4
LT
997 /*
998 * (~cwd & swd) will mask out exceptions that are not set to unmasked
999 * status. 0x3f is the exception bits in these regs, 0x200 is the
1000 * C1 reg you need in case of a stack fault, 0x040 is the stack
1001 * fault bit. We should only be taking one exception at a time,
1002 * so if this combination doesn't produce any single exception,
1003 * then we have a bad program that isn't synchronizing its FPU usage
1004 * and it will suffer the consequences since we won't be able to
1005 * fully reproduce the context of the exception
1006 */
1007 cwd = get_fpu_cwd(task);
1008 swd = get_fpu_swd(task);
ff347b22 1009 switch (swd & ~cwd & 0x3f) {
1da177e4
LT
1010 case 0x000:
1011 default:
1012 break;
1013 case 0x001: /* Invalid Op */
ff347b22
CE
1014 /*
1015 * swd & 0x240 == 0x040: Stack Underflow
1016 * swd & 0x240 == 0x240: Stack Overflow
1017 * User must clear the SF bit (0x40) if set
1018 */
1da177e4
LT
1019 info.si_code = FPE_FLTINV;
1020 break;
1021 case 0x002: /* Denormalize */
1022 case 0x010: /* Underflow */
1023 info.si_code = FPE_FLTUND;
1024 break;
1025 case 0x004: /* Zero Divide */
1026 info.si_code = FPE_FLTDIV;
1027 break;
1028 case 0x008: /* Overflow */
1029 info.si_code = FPE_FLTOVF;
1030 break;
1031 case 0x020: /* Precision */
1032 info.si_code = FPE_FLTRES;
1033 break;
1034 }
1035 force_sig_info(SIGFPE, &info, task);
1036}
1037
1038asmlinkage void bad_intr(void)
1039{
1040 printk("bad interrupt");
1041}
1042
1043asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1044{
65ea5b03 1045 void __user *ip = (void __user *)(regs->ip);
1da177e4
LT
1046 struct task_struct * task;
1047 siginfo_t info;
1048 unsigned short mxcsr;
1049
1050 conditional_sti(regs);
76381fee 1051 if (!user_mode(regs) &&
6e3f3617 1052 kernel_math_error(regs, "kernel simd math error", 19))
1da177e4
LT
1053 return;
1054
1055 /*
1056 * Save the info for the exception handler and clear the error.
1057 */
1058 task = current;
1059 save_init_fpu(task);
1060 task->thread.trap_no = 19;
1061 task->thread.error_code = 0;
1062 info.si_signo = SIGFPE;
1063 info.si_errno = 0;
1064 info.si_code = __SI_FAULT;
65ea5b03 1065 info.si_addr = ip;
1da177e4
LT
1066 /*
1067 * The SIMD FPU exceptions are handled a little differently, as there
1068 * is only a single status/control register. Thus, to determine which
1069 * unmasked exception was caught we must mask the exception mask bits
1070 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1071 */
1072 mxcsr = get_fpu_mxcsr(task);
1073 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1074 case 0x000:
1075 default:
1076 break;
1077 case 0x001: /* Invalid Op */
1078 info.si_code = FPE_FLTINV;
1079 break;
1080 case 0x002: /* Denormalize */
1081 case 0x010: /* Underflow */
1082 info.si_code = FPE_FLTUND;
1083 break;
1084 case 0x004: /* Zero Divide */
1085 info.si_code = FPE_FLTDIV;
1086 break;
1087 case 0x008: /* Overflow */
1088 info.si_code = FPE_FLTOVF;
1089 break;
1090 case 0x020: /* Precision */
1091 info.si_code = FPE_FLTRES;
1092 break;
1093 }
1094 force_sig_info(SIGFPE, &info, task);
1095}
1096
1097asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1098{
1099}
1100
1101asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
89b831ef
JS
1102{
1103}
1104
1105asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1da177e4
LT
1106{
1107}
1108
1109/*
1110 * 'math_state_restore()' saves the current math information in the
1111 * old math state array, and gets the new ones from the current task
1112 *
1113 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1114 * Don't touch unless you *really* know how it works.
1115 */
1116asmlinkage void math_state_restore(void)
1117{
1118 struct task_struct *me = current;
1119 clts(); /* Allow maths ops (or we recurse) */
1120
1121 if (!used_math())
1122 init_fpu(me);
1123 restore_fpu_checking(&me->thread.i387.fxsave);
e4f17c43 1124 task_thread_info(me)->status |= TS_USEDFPU;
e07e23e1 1125 me->fpu_counter++;
1da177e4 1126}
21db5584 1127EXPORT_SYMBOL_GPL(math_state_restore);
1da177e4 1128
1da177e4
LT
1129void __init trap_init(void)
1130{
1131 set_intr_gate(0,&divide_error);
1132 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1133 set_intr_gate_ist(2,&nmi,NMI_STACK);
b556b35e 1134 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
0a521588
JB
1135 set_system_gate(4,&overflow); /* int4 can be called from all */
1136 set_intr_gate(5,&bounds);
1da177e4
LT
1137 set_intr_gate(6,&invalid_op);
1138 set_intr_gate(7,&device_not_available);
1139 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1140 set_intr_gate(9,&coprocessor_segment_overrun);
1141 set_intr_gate(10,&invalid_TSS);
1142 set_intr_gate(11,&segment_not_present);
1143 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1144 set_intr_gate(13,&general_protection);
1145 set_intr_gate(14,&page_fault);
1146 set_intr_gate(15,&spurious_interrupt_bug);
1147 set_intr_gate(16,&coprocessor_error);
1148 set_intr_gate(17,&alignment_check);
1149#ifdef CONFIG_X86_MCE
1150 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1151#endif
1152 set_intr_gate(19,&simd_coprocessor_error);
1153
1154#ifdef CONFIG_IA32_EMULATION
1155 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1156#endif
1157
1da177e4
LT
1158 /*
1159 * Should be a barrier for any external CPU state.
1160 */
1161 cpu_init();
1162}
1163
1164
2c8c0e6b 1165static int __init oops_setup(char *s)
1da177e4 1166{
2c8c0e6b
AK
1167 if (!s)
1168 return -EINVAL;
1169 if (!strcmp(s, "panic"))
1170 panic_on_oops = 1;
1171 return 0;
1da177e4 1172}
2c8c0e6b 1173early_param("oops", oops_setup);
1da177e4
LT
1174
1175static int __init kstack_setup(char *s)
1176{
2c8c0e6b
AK
1177 if (!s)
1178 return -EINVAL;
1da177e4 1179 kstack_depth_to_print = simple_strtoul(s,NULL,0);
2c8c0e6b 1180 return 0;
1da177e4 1181}
2c8c0e6b 1182early_param("kstack", kstack_setup);
a25bd949
AV
1183
1184
1185static int __init code_bytes_setup(char *s)
1186{
1187 code_bytes = simple_strtoul(s, NULL, 0);
1188 if (code_bytes > 8192)
1189 code_bytes = 8192;
1190
1191 return 1;
1192}
1193__setup("code_bytes=", code_bytes_setup);