x86, fpu: split FPU state from task struct - v5
[linux-2.6-block.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
5deb45e3
SR
603notrace __kprobes void
604die_nmi(char *str, struct pt_regs *regs, int do_panic)
1da177e4 605{
737a460f 606 unsigned long flags;
1209140c 607
737a460f
JW
608 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
609 NOTIFY_STOP)
610 return;
1209140c 611
737a460f 612 flags = oops_begin();
1da177e4
LT
613 /*
614 * We are in trouble anyway, lets at least try
615 * to get a message out.
616 */
151f8cc1 617 printk(str, smp_processor_id());
1da177e4 618 show_registers(regs);
8bcc5280
VG
619 if (kexec_should_crash(current))
620 crash_kexec(regs);
fac58550
AK
621 if (do_panic || panic_on_oops)
622 panic("Non maskable interrupt");
22f5991c 623 oops_end(flags, NULL, SIGBUS);
8b1ffe95
CM
624 nmi_exit();
625 local_irq_enable();
22f5991c 626 do_exit(SIGBUS);
1da177e4
LT
627}
628
0f2fbdcb
PP
629static void __kprobes do_trap(int trapnr, int signr, char *str,
630 struct pt_regs * regs, long error_code,
631 siginfo_t *info)
1da177e4 632{
6e3f3617
JB
633 struct task_struct *tsk = current;
634
6e3f3617 635 if (user_mode(regs)) {
d1895183
AK
636 /*
637 * We want error_code and trap_no set for userspace
638 * faults and kernelspace faults which result in
639 * die(), but not kernelspace faults which are fixed
640 * up. die() gives the process no chance to handle
641 * the signal and notice the kernel fault information,
642 * so that won't result in polluting the information
643 * about previously queued, but not yet delivered,
644 * faults. See also do_general_protection below.
645 */
646 tsk->thread.error_code = error_code;
647 tsk->thread.trap_no = trapnr;
648
abd4f750 649 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
03252919 650 printk_ratelimit()) {
1da177e4 651 printk(KERN_INFO
03252919 652 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
1da177e4 653 tsk->comm, tsk->pid, str,
65ea5b03 654 regs->ip, regs->sp, error_code);
03252919
AK
655 print_vma_addr(" in ", regs->ip);
656 printk("\n");
657 }
1da177e4 658
1da177e4
LT
659 if (info)
660 force_sig_info(signr, info, tsk);
661 else
662 force_sig(signr, tsk);
663 return;
664 }
665
666
b3a5acc1
HH
667 if (!fixup_exception(regs)) {
668 tsk->thread.error_code = error_code;
669 tsk->thread.trap_no = trapnr;
670 die(str, regs, error_code);
1da177e4 671 }
b3a5acc1 672 return;
1da177e4
LT
673}
674
675#define DO_ERROR(trapnr, signr, str, name) \
676asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
677{ \
678 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
679 == NOTIFY_STOP) \
680 return; \
40e59a61 681 conditional_sti(regs); \
1da177e4
LT
682 do_trap(trapnr, signr, str, regs, error_code, NULL); \
683}
684
685#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
686asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
687{ \
688 siginfo_t info; \
689 info.si_signo = signr; \
690 info.si_errno = 0; \
691 info.si_code = sicode; \
692 info.si_addr = (void __user *)siaddr; \
fb1dac90 693 trace_hardirqs_fixup(); \
1da177e4
LT
694 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
695 == NOTIFY_STOP) \
696 return; \
40e59a61 697 conditional_sti(regs); \
1da177e4
LT
698 do_trap(trapnr, signr, str, regs, error_code, &info); \
699}
700
65ea5b03 701DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
1da177e4
LT
702DO_ERROR( 4, SIGSEGV, "overflow", overflow)
703DO_ERROR( 5, SIGSEGV, "bounds", bounds)
65ea5b03 704DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
1da177e4
LT
705DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
706DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
707DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
708DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
709DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
710DO_ERROR(18, SIGSEGV, "reserved", reserved)
40e59a61
AK
711
712/* Runs on IST stack */
713asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
714{
715 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
716 12, SIGBUS) == NOTIFY_STOP)
717 return;
718 preempt_conditional_sti(regs);
719 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
720 preempt_conditional_cli(regs);
721}
eca37c18
JB
722
723asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
724{
725 static const char str[] = "double fault";
726 struct task_struct *tsk = current;
727
728 /* Return not checked because double check cannot be ignored */
729 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
730
731 tsk->thread.error_code = error_code;
732 tsk->thread.trap_no = 8;
733
734 /* This is always a kernel trap and never fixable (and thus must
735 never return). */
736 for (;;)
737 die(str, regs, error_code);
738}
1da177e4 739
0f2fbdcb
PP
740asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
741 long error_code)
1da177e4 742{
6e3f3617
JB
743 struct task_struct *tsk = current;
744
1da177e4
LT
745 conditional_sti(regs);
746
6e3f3617 747 if (user_mode(regs)) {
d1895183
AK
748 tsk->thread.error_code = error_code;
749 tsk->thread.trap_no = 13;
750
abd4f750 751 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
03252919 752 printk_ratelimit()) {
1da177e4 753 printk(KERN_INFO
03252919 754 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
1da177e4 755 tsk->comm, tsk->pid,
65ea5b03 756 regs->ip, regs->sp, error_code);
03252919
AK
757 print_vma_addr(" in ", regs->ip);
758 printk("\n");
759 }
1da177e4 760
1da177e4
LT
761 force_sig(SIGSEGV, tsk);
762 return;
763 }
764
b3a5acc1
HH
765 if (fixup_exception(regs))
766 return;
d1895183 767
b3a5acc1
HH
768 tsk->thread.error_code = error_code;
769 tsk->thread.trap_no = 13;
770 if (notify_die(DIE_GPF, "general protection fault", regs,
771 error_code, 13, SIGSEGV) == NOTIFY_STOP)
772 return;
773 die("general protection fault", regs, error_code);
1da177e4
LT
774}
775
5deb45e3 776static notrace __kprobes void
eddb6fb9 777mem_parity_error(unsigned char reason, struct pt_regs * regs)
1da177e4 778{
c41c5cd3
DZ
779 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
780 reason);
9c5f8be4 781 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
c41c5cd3 782
c0d12172
DJ
783#if defined(CONFIG_EDAC)
784 if(edac_handler_set()) {
785 edac_atomic_assert_error();
786 return;
787 }
788#endif
789
8da5adda 790 if (panic_on_unrecovered_nmi)
c41c5cd3
DZ
791 panic("NMI: Not continuing");
792
793 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
1da177e4
LT
794
795 /* Clear and disable the memory parity error line. */
796 reason = (reason & 0xf) | 4;
797 outb(reason, 0x61);
798}
799
5deb45e3 800static notrace __kprobes void
eddb6fb9 801io_check_error(unsigned char reason, struct pt_regs * regs)
1da177e4
LT
802{
803 printk("NMI: IOCK error (debug interrupt?)\n");
804 show_registers(regs);
805
806 /* Re-enable the IOCK line, wait for a few seconds */
807 reason = (reason & 0xf) | 8;
808 outb(reason, 0x61);
809 mdelay(2000);
810 reason &= ~8;
811 outb(reason, 0x61);
812}
813
5deb45e3 814static notrace __kprobes void
eddb6fb9 815unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
c41c5cd3 816{
d3597524
JW
817 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
818 return;
c41c5cd3
DZ
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. */
5deb45e3 831asmlinkage notrace __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 870{
143a5d32
PZ
871 trace_hardirqs_fixup();
872
1da177e4
LT
873 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
874 return;
875 }
40e59a61 876 preempt_conditional_sti(regs);
1da177e4 877 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
40e59a61 878 preempt_conditional_cli(regs);
1da177e4
LT
879}
880
6fefb0d1
AK
881/* Help handler running on IST stack to switch back to user stack
882 for scheduling or signal handling. The actual stack switch is done in
883 entry.S */
eddb6fb9 884asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
6fefb0d1
AK
885{
886 struct pt_regs *regs = eregs;
887 /* Did already sync */
65ea5b03 888 if (eregs == (struct pt_regs *)eregs->sp)
6fefb0d1
AK
889 ;
890 /* Exception from user space */
76381fee 891 else if (user_mode(eregs))
bb049232 892 regs = task_pt_regs(current);
6fefb0d1
AK
893 /* Exception from kernel and interrupts are enabled. Move to
894 kernel process stack. */
65ea5b03
PA
895 else if (eregs->flags & X86_EFLAGS_IF)
896 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
6fefb0d1
AK
897 if (eregs != regs)
898 *regs = *eregs;
899 return regs;
900}
901
1da177e4 902/* runs on IST stack. */
0f2fbdcb
PP
903asmlinkage void __kprobes do_debug(struct pt_regs * regs,
904 unsigned long error_code)
1da177e4 905{
1da177e4
LT
906 unsigned long condition;
907 struct task_struct *tsk = current;
908 siginfo_t info;
909
000f4a9e
PZ
910 trace_hardirqs_fixup();
911
e9129e56 912 get_debugreg(condition, 6);
1da177e4 913
10faa81e
RM
914 /*
915 * The processor cleared BTF, so don't mark that we need it set.
916 */
917 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
918 tsk->thread.debugctlmsr = 0;
919
1da177e4 920 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
daeeafec 921 SIGTRAP) == NOTIFY_STOP)
6fefb0d1 922 return;
daeeafec 923
a65d17c9 924 preempt_conditional_sti(regs);
1da177e4
LT
925
926 /* Mask out spurious debug traps due to lazy DR7 setting */
927 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
928 if (!tsk->thread.debugreg7) {
929 goto clear_dr7;
930 }
931 }
932
933 tsk->thread.debugreg6 = condition;
934
e1f28773
RM
935
936 /*
937 * Single-stepping through TF: make sure we ignore any events in
938 * kernel space (but re-enable TF when returning to user mode).
939 */
daeeafec 940 if (condition & DR_STEP) {
76381fee 941 if (!user_mode(regs))
1da177e4 942 goto clear_TF_reenable;
1da177e4
LT
943 }
944
945 /* Ok, finally something we can handle */
946 tsk->thread.trap_no = 1;
947 tsk->thread.error_code = error_code;
948 info.si_signo = SIGTRAP;
949 info.si_errno = 0;
950 info.si_code = TRAP_BRKPT;
65ea5b03 951 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
01b8faae 952 force_sig_info(SIGTRAP, &info, tsk);
1da177e4 953
1da177e4 954clear_dr7:
e9129e56 955 set_debugreg(0UL, 7);
a65d17c9 956 preempt_conditional_cli(regs);
6fefb0d1 957 return;
1da177e4
LT
958
959clear_TF_reenable:
960 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
053de044 961 regs->flags &= ~X86_EFLAGS_TF;
a65d17c9 962 preempt_conditional_cli(regs);
1da177e4
LT
963}
964
6e3f3617 965static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
1da177e4 966{
b3a5acc1 967 if (fixup_exception(regs))
1da177e4 968 return 1;
b3a5acc1 969
6e3f3617 970 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
3a848f63 971 /* Illegal floating point operation in the kernel */
6e3f3617 972 current->thread.trap_no = trapnr;
1da177e4 973 die(str, regs, 0);
1da177e4
LT
974 return 0;
975}
976
977/*
978 * Note that we play around with the 'TS' bit in an attempt to get
979 * the correct behaviour even in the presence of the asynchronous
980 * IRQ13 behaviour
981 */
982asmlinkage void do_coprocessor_error(struct pt_regs *regs)
983{
65ea5b03 984 void __user *ip = (void __user *)(regs->ip);
1da177e4
LT
985 struct task_struct * task;
986 siginfo_t info;
987 unsigned short cwd, swd;
988
989 conditional_sti(regs);
76381fee 990 if (!user_mode(regs) &&
6e3f3617 991 kernel_math_error(regs, "kernel x87 math error", 16))
1da177e4
LT
992 return;
993
994 /*
995 * Save the info for the exception handler and clear the error.
996 */
997 task = current;
998 save_init_fpu(task);
999 task->thread.trap_no = 16;
1000 task->thread.error_code = 0;
1001 info.si_signo = SIGFPE;
1002 info.si_errno = 0;
1003 info.si_code = __SI_FAULT;
65ea5b03 1004 info.si_addr = ip;
1da177e4
LT
1005 /*
1006 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1007 * status. 0x3f is the exception bits in these regs, 0x200 is the
1008 * C1 reg you need in case of a stack fault, 0x040 is the stack
1009 * fault bit. We should only be taking one exception at a time,
1010 * so if this combination doesn't produce any single exception,
1011 * then we have a bad program that isn't synchronizing its FPU usage
1012 * and it will suffer the consequences since we won't be able to
1013 * fully reproduce the context of the exception
1014 */
1015 cwd = get_fpu_cwd(task);
1016 swd = get_fpu_swd(task);
ff347b22 1017 switch (swd & ~cwd & 0x3f) {
1da177e4
LT
1018 case 0x000:
1019 default:
1020 break;
1021 case 0x001: /* Invalid Op */
ff347b22
CE
1022 /*
1023 * swd & 0x240 == 0x040: Stack Underflow
1024 * swd & 0x240 == 0x240: Stack Overflow
1025 * User must clear the SF bit (0x40) if set
1026 */
1da177e4
LT
1027 info.si_code = FPE_FLTINV;
1028 break;
1029 case 0x002: /* Denormalize */
1030 case 0x010: /* Underflow */
1031 info.si_code = FPE_FLTUND;
1032 break;
1033 case 0x004: /* Zero Divide */
1034 info.si_code = FPE_FLTDIV;
1035 break;
1036 case 0x008: /* Overflow */
1037 info.si_code = FPE_FLTOVF;
1038 break;
1039 case 0x020: /* Precision */
1040 info.si_code = FPE_FLTRES;
1041 break;
1042 }
1043 force_sig_info(SIGFPE, &info, task);
1044}
1045
1046asmlinkage void bad_intr(void)
1047{
1048 printk("bad interrupt");
1049}
1050
1051asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1052{
65ea5b03 1053 void __user *ip = (void __user *)(regs->ip);
1da177e4
LT
1054 struct task_struct * task;
1055 siginfo_t info;
1056 unsigned short mxcsr;
1057
1058 conditional_sti(regs);
76381fee 1059 if (!user_mode(regs) &&
6e3f3617 1060 kernel_math_error(regs, "kernel simd math error", 19))
1da177e4
LT
1061 return;
1062
1063 /*
1064 * Save the info for the exception handler and clear the error.
1065 */
1066 task = current;
1067 save_init_fpu(task);
1068 task->thread.trap_no = 19;
1069 task->thread.error_code = 0;
1070 info.si_signo = SIGFPE;
1071 info.si_errno = 0;
1072 info.si_code = __SI_FAULT;
65ea5b03 1073 info.si_addr = ip;
1da177e4
LT
1074 /*
1075 * The SIMD FPU exceptions are handled a little differently, as there
1076 * is only a single status/control register. Thus, to determine which
1077 * unmasked exception was caught we must mask the exception mask bits
1078 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1079 */
1080 mxcsr = get_fpu_mxcsr(task);
1081 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1082 case 0x000:
1083 default:
1084 break;
1085 case 0x001: /* Invalid Op */
1086 info.si_code = FPE_FLTINV;
1087 break;
1088 case 0x002: /* Denormalize */
1089 case 0x010: /* Underflow */
1090 info.si_code = FPE_FLTUND;
1091 break;
1092 case 0x004: /* Zero Divide */
1093 info.si_code = FPE_FLTDIV;
1094 break;
1095 case 0x008: /* Overflow */
1096 info.si_code = FPE_FLTOVF;
1097 break;
1098 case 0x020: /* Precision */
1099 info.si_code = FPE_FLTRES;
1100 break;
1101 }
1102 force_sig_info(SIGFPE, &info, task);
1103}
1104
1105asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1106{
1107}
1108
1109asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
89b831ef
JS
1110{
1111}
1112
1113asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1da177e4
LT
1114{
1115}
1116
1117/*
1118 * 'math_state_restore()' saves the current math information in the
1119 * old math state array, and gets the new ones from the current task
1120 *
1121 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1122 * Don't touch unless you *really* know how it works.
1123 */
1124asmlinkage void math_state_restore(void)
1125{
1126 struct task_struct *me = current;
1127 clts(); /* Allow maths ops (or we recurse) */
1128
1129 if (!used_math())
1130 init_fpu(me);
61c4628b 1131 restore_fpu_checking(&me->thread.xstate->fxsave);
e4f17c43 1132 task_thread_info(me)->status |= TS_USEDFPU;
e07e23e1 1133 me->fpu_counter++;
1da177e4 1134}
21db5584 1135EXPORT_SYMBOL_GPL(math_state_restore);
1da177e4 1136
1da177e4
LT
1137void __init trap_init(void)
1138{
1139 set_intr_gate(0,&divide_error);
1140 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1141 set_intr_gate_ist(2,&nmi,NMI_STACK);
b556b35e 1142 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
0a521588
JB
1143 set_system_gate(4,&overflow); /* int4 can be called from all */
1144 set_intr_gate(5,&bounds);
1da177e4
LT
1145 set_intr_gate(6,&invalid_op);
1146 set_intr_gate(7,&device_not_available);
1147 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1148 set_intr_gate(9,&coprocessor_segment_overrun);
1149 set_intr_gate(10,&invalid_TSS);
1150 set_intr_gate(11,&segment_not_present);
1151 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1152 set_intr_gate(13,&general_protection);
1153 set_intr_gate(14,&page_fault);
1154 set_intr_gate(15,&spurious_interrupt_bug);
1155 set_intr_gate(16,&coprocessor_error);
1156 set_intr_gate(17,&alignment_check);
1157#ifdef CONFIG_X86_MCE
1158 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1159#endif
1160 set_intr_gate(19,&simd_coprocessor_error);
1161
1162#ifdef CONFIG_IA32_EMULATION
1163 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1164#endif
1165
61c4628b
SS
1166 /*
1167 * initialize the per thread extended state:
1168 */
1169 init_thread_xstate();
1da177e4
LT
1170 /*
1171 * Should be a barrier for any external CPU state.
1172 */
1173 cpu_init();
1174}
1175
1176
2c8c0e6b 1177static int __init oops_setup(char *s)
1da177e4 1178{
2c8c0e6b
AK
1179 if (!s)
1180 return -EINVAL;
1181 if (!strcmp(s, "panic"))
1182 panic_on_oops = 1;
1183 return 0;
1da177e4 1184}
2c8c0e6b 1185early_param("oops", oops_setup);
1da177e4
LT
1186
1187static int __init kstack_setup(char *s)
1188{
2c8c0e6b
AK
1189 if (!s)
1190 return -EINVAL;
1da177e4 1191 kstack_depth_to_print = simple_strtoul(s,NULL,0);
2c8c0e6b 1192 return 0;
1da177e4 1193}
2c8c0e6b 1194early_param("kstack", kstack_setup);
a25bd949
AV
1195
1196
1197static int __init code_bytes_setup(char *s)
1198{
1199 code_bytes = simple_strtoul(s, NULL, 0);
1200 if (code_bytes > 8192)
1201 code_bytes = 8192;
1202
1203 return 1;
1204}
1205__setup("code_bytes=", code_bytes_setup);