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