blob: 911ed28afff86d7ba4936871f2536868e3e3ba2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'entry.S'.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Randy Dunlap4b0ff1a2006-10-05 19:07:26 +020024#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070027#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070028#include <linux/kprobes.h>
Vivek Goyal8bcc5282006-04-18 12:35:13 +020029#include <linux/kexec.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020030#include <linux/unwind.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010031#include <linux/uaccess.h>
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -080032#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070033#include <linux/kdebug.h>
Arjan van de Ven57c351d2007-11-26 20:42:19 +010034#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Dave Jiangc0d12172007-07-19 01:49:46 -070036#if defined(CONFIG_EDAC)
37#include <linux/edac.h>
38#endif
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/atomic.h>
43#include <asm/debugreg.h>
44#include <asm/desc.h>
45#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020047#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/smp.h>
49#include <asm/pgalloc.h>
50#include <asm/pda.h>
51#include <asm/proto.h>
52#include <asm/nmi.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020053#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055asmlinkage 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Arjan van de Vena25bd942008-01-30 13:33:08 +010077static unsigned int code_bytes = 64;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static inline void conditional_sti(struct pt_regs *regs)
80{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010081 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 local_irq_enable();
83}
84
John Blackwooda65d17c2006-02-12 14:34:58 -080085static inline void preempt_conditional_sti(struct pt_regs *regs)
86{
87 preempt_disable();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010088 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080089 local_irq_enable();
90}
91
92static inline void preempt_conditional_cli(struct pt_regs *regs)
93{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010094 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080095 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +020096 /* Make sure to not schedule here because we could be running
97 on an exception stack. */
John Blackwooda65d17c2006-02-12 14:34:58 -080098 preempt_enable_no_resched();
99}
100
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100101int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#ifdef CONFIG_KALLSYMS
Arjan van de Venbc850d62008-01-30 13:33:07 +0100104void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700105{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long offset = 0, symsize;
107 const char *symname;
108 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700109 char *delim = ":";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 char namebuf[128];
Arjan van de Venbc850d62008-01-30 13:33:07 +0100111 char reliab[4] = "";;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ingo Molnar3ac94932006-07-03 00:24:36 -0700113 symname = kallsyms_lookup(address, &symsize, &offset,
114 &modname, namebuf);
115 if (!symname) {
116 printk(" [<%016lx>]\n", address);
117 return;
118 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100119 if (!reliable)
120 strcpy(reliab, "? ");
121
Ingo Molnar3ac94932006-07-03 00:24:36 -0700122 if (!modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 modname = delim = "";
Arjan van de Venbc850d62008-01-30 13:33:07 +0100124 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
125 address, reliab, delim, modname, delim, symname, offset, symsize);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#else
Arjan van de Venbc850d62008-01-30 13:33:07 +0100128void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700129{
130 printk(" [<%016lx>]\n", address);
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif
133
Andi Kleen0a658002005-04-16 15:25:17 -0700134static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200135 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700136{
Jan Beulichb556b352006-01-11 22:43:00 +0100137 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700138 [DEBUG_STACK - 1] = "#DB",
139 [NMI_STACK - 1] = "NMI",
140 [DOUBLEFAULT_STACK - 1] = "#DF",
141 [STACKFAULT_STACK - 1] = "#SS",
142 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100143#if DEBUG_STKSZ > EXCEPTION_STKSZ
144 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
145#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700146 };
147 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700149 /*
150 * Iterate over all exception stacks, and figure out whether
151 * 'stack' is in one of them:
152 */
Andi Kleen0a658002005-04-16 15:25:17 -0700153 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200154 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700155 /*
156 * Is 'stack' above this exception frame's end?
157 * If yes then skip to the next frame.
158 */
Andi Kleen0a658002005-04-16 15:25:17 -0700159 if (stack >= end)
160 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700161 /*
162 * Is 'stack' above this exception frame's start address?
163 * If yes then we found the right frame.
164 */
Andi Kleen0a658002005-04-16 15:25:17 -0700165 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700166 /*
167 * Make sure we only iterate through an exception
168 * stack once. If it comes up for the second time
169 * then there's something wrong going on - just
170 * break out and return NULL:
171 */
Andi Kleen0a658002005-04-16 15:25:17 -0700172 if (*usedp & (1U << k))
173 break;
174 *usedp |= 1U << k;
175 *idp = ids[k];
176 return (unsigned long *)end;
177 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700178 /*
179 * If this is a debug stack, and if it has a larger size than
180 * the usual exception stacks, then 'stack' might still
181 * be within the lower portion of the debug stack:
182 */
Jan Beulichb556b352006-01-11 22:43:00 +0100183#if DEBUG_STKSZ > EXCEPTION_STKSZ
184 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
185 unsigned j = N_EXCEPTION_STACKS - 1;
186
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700187 /*
188 * Black magic. A large debug stack is composed of
189 * multiple exception stack entries, which we
190 * iterate through now. Dont look:
191 */
Jan Beulichb556b352006-01-11 22:43:00 +0100192 do {
193 ++j;
194 end -= EXCEPTION_STKSZ;
195 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
196 } while (stack < end - EXCEPTION_STKSZ);
197 if (*usedp & (1U << j))
198 break;
199 *usedp |= 1U << j;
200 *idp = ids[j];
201 return (unsigned long *)end;
202 }
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Andi Kleenb615ebd2006-12-07 02:14:00 +0100208#define MSG(txt) ops->warning(data, txt)
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Simon Arlott676b1852007-10-20 01:25:36 +0200211 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * process stack
213 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700214 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
216
Arjan van de Vene4a94562008-01-30 13:33:07 +0100217static inline int valid_stack_ptr(struct thread_info *tinfo,
218 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100219{
220 void *t = (void *)tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100221 if (end) {
222 if (p < end && p >= (end-THREAD_SIZE))
223 return 1;
224 else
225 return 0;
226 }
227 return p > t && p < t + THREAD_SIZE - size;
228}
229
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100230/* The form of the top of the frame on the stack */
231struct stack_frame {
232 struct stack_frame *next_frame;
233 unsigned long return_address;
234};
235
236
Arjan van de Vene4a94562008-01-30 13:33:07 +0100237static inline unsigned long print_context_stack(struct thread_info *tinfo,
238 unsigned long *stack, unsigned long bp,
239 const struct stacktrace_ops *ops, void *data,
240 unsigned long *end)
241{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100242 struct stack_frame *frame = (struct stack_frame *)bp;
243
244 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
245 unsigned long addr;
246
247 addr = *stack;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100248 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100249 if ((unsigned long) stack == bp + 8) {
250 ops->address(data, addr, 1);
251 frame = frame->next_frame;
252 bp = (unsigned long) frame;
253 } else {
254 ops->address(data, addr, bp == 0);
255 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100256 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100257 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100258 }
259 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100260}
261
Andi Kleenb615ebd2006-12-07 02:14:00 +0100262void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100263 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200264 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Andrew Mortonda689332006-12-07 02:14:02 +0100266 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100267 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700268 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100269 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jan Beulichb538ed22006-06-26 13:57:32 +0200271 if (!tsk)
272 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100273 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200274
Andi Kleenc0b766f2006-09-26 10:52:34 +0200275 if (!stack) {
276 unsigned long dummy;
277 stack = &dummy;
278 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100279 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200280 }
281
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100282#ifdef CONFIG_FRAME_POINTER
283 if (!bp) {
284 if (tsk == current) {
285 /* Grab bp right from our regs */
286 asm("movq %%rbp, %0" : "=r" (bp):);
287 } else {
288 /* bp is the last reg pushed by switch_to */
289 bp = *(unsigned long *) tsk->thread.sp;
290 }
291 }
292#endif
293
294
Andi Kleen0a658002005-04-16 15:25:17 -0700295
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700296 /*
297 * Print function call entries in all stacks, starting at the
298 * current stack address. If the stacks consist of nested
299 * exceptions
300 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200301 for (;;) {
302 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700303 unsigned long *estack_end;
304 estack_end = in_exception_stack(cpu, (unsigned long)stack,
305 &used, &id);
306
307 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200308 if (ops->stack(data, id) < 0)
309 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100310
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100311 bp = print_context_stack(tinfo, stack, bp, ops,
312 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200313 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700314 /*
315 * We link to the next stack via the
316 * second-to-last pointer (index -2 to end) in the
317 * exception stack:
318 */
Andi Kleen0a658002005-04-16 15:25:17 -0700319 stack = (unsigned long *) estack_end[-2];
320 continue;
321 }
322 if (irqstack_end) {
323 unsigned long *irqstack;
324 irqstack = irqstack_end -
325 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
326
327 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200328 if (ops->stack(data, "IRQ") < 0)
329 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100330 bp = print_context_stack(tinfo, stack, bp,
331 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700332 /*
333 * We link to the next stack (which would be
334 * the process stack normally) the last
335 * pointer (index -1 to end) in the IRQ stack:
336 */
Andi Kleen0a658002005-04-16 15:25:17 -0700337 stack = (unsigned long *) (irqstack_end[-1]);
338 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200339 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
Andi Kleen0a658002005-04-16 15:25:17 -0700343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Andi Kleen0a658002005-04-16 15:25:17 -0700345
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700346 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200347 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700348 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100349 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100350 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200351}
352EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700353
Andi Kleenc0b766f2006-09-26 10:52:34 +0200354static void
355print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
356{
357 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk("\n");
359}
360
Andi Kleenc0b766f2006-09-26 10:52:34 +0200361static void print_trace_warning(void *data, char *msg)
362{
363 printk("%s\n", msg);
364}
365
366static int print_trace_stack(void *data, char *name)
367{
368 printk(" <%s> ", name);
369 return 0;
370}
371
Arjan van de Venbc850d62008-01-30 13:33:07 +0100372static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200373{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700374 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100375 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200376}
377
Jan Beulich9689ba82007-10-17 18:04:37 +0200378static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200379 .warning = print_trace_warning,
380 .warning_symbol = print_trace_warning_symbol,
381 .stack = print_trace_stack,
382 .address = print_trace_address,
383};
384
385void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100386show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
387 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200388{
389 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100390 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200391 printk("\n");
392}
393
394static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100395_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
396 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 unsigned long *stack;
399 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200400 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100401 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
402 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 // debugging aid: "show_stack(NULL, NULL);" prints the
405 // back trace for this cpu.
406
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100407 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100409 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100411 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100414 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 for(i=0; i < kstack_depth_to_print; i++) {
416 if (stack >= irqstack && stack <= irqstack_end) {
417 if (stack == irqstack_end) {
418 stack = (unsigned long *) (irqstack_end[-1]);
419 printk(" <EOI> ");
420 }
421 } else {
422 if (((long) stack & (THREAD_SIZE-1)) == 0)
423 break;
424 }
425 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700426 printk("\n");
427 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700428 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100430 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200431}
432
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100433void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200434{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100435 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438/*
439 * The architecture-independent dump_stack generator
440 */
441void dump_stack(void)
442{
443 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100444 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100445
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100446#ifdef CONFIG_FRAME_POINTER
447 if (!bp)
448 asm("movq %%rbp, %0" : "=r" (bp):);
449#endif
450
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100451 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
452 current->pid, current->comm, print_tainted(),
453 init_utsname()->release,
454 (int)strcspn(init_utsname()->version, " "),
455 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100456 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459EXPORT_SYMBOL(dump_stack);
460
461void show_registers(struct pt_regs *regs)
462{
463 int i;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100464 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200465 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100466 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100467 u8 *ip;
468 unsigned int code_prologue = code_bytes * 43 / 64;
469 unsigned int code_len = code_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100471 sp = regs->sp;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100472 ip = (u8 *) regs->ip - code_prologue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk("CPU %d ", cpu);
474 __show_regs(regs);
475 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800476 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * When in-kernel, we also print out the stack and code at the
480 * time of the fault..
481 */
Arjan van de Vena25bd942008-01-30 13:33:08 +0100482 if (!user_mode(regs)) {
483 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100485 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Arjan van de Vena25bd942008-01-30 13:33:08 +0100486 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Arjan van de Vena25bd942008-01-30 13:33:08 +0100488 printk(KERN_EMERG "Code: ");
489 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
490 /* try starting at RIP */
491 ip = (u8 *) regs->ip;
492 code_len = code_len - code_prologue + 1;
493 }
494 for (i = 0; i < code_len; i++, ip++) {
495 if (ip < (u8 *)PAGE_OFFSET ||
496 probe_kernel_address(ip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 printk(" Bad RIP value.");
498 break;
499 }
Arjan van de Vena25bd942008-01-30 13:33:08 +0100500 if (ip == (u8 *)regs->ip)
501 printk("<%02x> ", c);
502 else
503 printk("%02x ", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 }
506 printk("\n");
507}
508
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100509int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800510{
511 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100513 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800514 return 0;
515
516 return ud2 == 0x0b0f;
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Andi Kleen39743c92007-10-19 20:35:03 +0200519static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200521static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100523unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700525 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200526 unsigned long flags;
527
Andrew Mortonabf0f102006-09-26 10:52:36 +0200528 oops_enter();
529
Jan Beulich12091402005-09-12 18:49:24 +0200530 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200531 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700532 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200533 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (cpu == die_owner)
535 /* nested oops. should stop eventually */;
536 else
Andi Kleen39743c92007-10-19 20:35:03 +0200537 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200539 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200540 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200542 bust_spinlocks(1);
543 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Jan Beulich22f59912008-01-30 13:31:23 +0100546void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200549 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200550 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200551 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200552 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200553 __raw_spin_unlock(&die_lock);
554 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100555 if (!regs) {
556 oops_exit();
557 return;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700560 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200561 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100562 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jan Beulich22f59912008-01-30 13:31:23 +0100565int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 static int die_counter;
568 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
569#ifdef CONFIG_PREEMPT
570 printk("PREEMPT ");
571#endif
572#ifdef CONFIG_SMP
573 printk("SMP ");
574#endif
575#ifdef CONFIG_DEBUG_PAGEALLOC
576 printk("DEBUG_PAGEALLOC");
577#endif
578 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100579 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
580 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700582 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Executive summary in case the oops scrolled away */
584 printk(KERN_ALERT "RIP ");
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100585 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100586 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200587 if (kexec_should_crash(current))
588 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592void die(const char * str, struct pt_regs * regs, long err)
593{
Jan Beulich12091402005-09-12 18:49:24 +0200594 unsigned long flags = oops_begin();
595
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800596 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100597 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800598
Jan Beulich22f59912008-01-30 13:31:23 +0100599 if (__die(str, regs, err))
600 regs = NULL;
601 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Andi Kleenfac58552006-09-26 10:52:27 +0200604void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jan Beulich12091402005-09-12 18:49:24 +0200606 unsigned long flags = oops_begin();
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * We are in trouble anyway, lets at least try
610 * to get a message out.
611 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200612 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200614 if (kexec_should_crash(current))
615 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200616 if (do_panic || panic_on_oops)
617 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100618 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200619 nmi_exit();
620 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100621 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700624static void __kprobes do_trap(int trapnr, int signr, char *str,
625 struct pt_regs * regs, long error_code,
626 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100628 struct task_struct *tsk = current;
629
Jan Beulich6e3f3612006-01-11 22:42:14 +0100630 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200631 /*
632 * We want error_code and trap_no set for userspace
633 * faults and kernelspace faults which result in
634 * die(), but not kernelspace faults which are fixed
635 * up. die() gives the process no chance to handle
636 * the signal and notice the kernel fault information,
637 * so that won't result in polluting the information
638 * about previously queued, but not yet delivered,
639 * faults. See also do_general_protection below.
640 */
641 tsk->thread.error_code = error_code;
642 tsk->thread.trap_no = trapnr;
643
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200644 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
Andi Kleen03252912008-01-30 13:33:18 +0100645 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100647 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100649 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100650 print_vma_addr(" in ", regs->ip);
651 printk("\n");
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (info)
655 force_sig_info(signr, info, tsk);
656 else
657 force_sig(signr, tsk);
658 return;
659 }
660
661
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100662 if (!fixup_exception(regs)) {
663 tsk->thread.error_code = error_code;
664 tsk->thread.trap_no = trapnr;
665 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100667 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670#define DO_ERROR(trapnr, signr, str, name) \
671asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
672{ \
673 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
674 == NOTIFY_STOP) \
675 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200676 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 do_trap(trapnr, signr, str, regs, error_code, NULL); \
678}
679
680#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
681asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
682{ \
683 siginfo_t info; \
684 info.si_signo = signr; \
685 info.si_errno = 0; \
686 info.si_code = sicode; \
687 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100688 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
690 == NOTIFY_STOP) \
691 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200692 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 do_trap(trapnr, signr, str, regs, error_code, &info); \
694}
695
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100696DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697DO_ERROR( 4, SIGSEGV, "overflow", overflow)
698DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100699DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
701DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
702DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
703DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
704DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
705DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200706
707/* Runs on IST stack */
708asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
709{
710 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
711 12, SIGBUS) == NOTIFY_STOP)
712 return;
713 preempt_conditional_sti(regs);
714 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
715 preempt_conditional_cli(regs);
716}
Jan Beulicheca37c12006-01-11 22:42:17 +0100717
718asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
719{
720 static const char str[] = "double fault";
721 struct task_struct *tsk = current;
722
723 /* Return not checked because double check cannot be ignored */
724 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
725
726 tsk->thread.error_code = error_code;
727 tsk->thread.trap_no = 8;
728
729 /* This is always a kernel trap and never fixable (and thus must
730 never return). */
731 for (;;)
732 die(str, regs, error_code);
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700735asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
736 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100738 struct task_struct *tsk = current;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 conditional_sti(regs);
741
Jan Beulich6e3f3612006-01-11 22:42:14 +0100742 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200743 tsk->thread.error_code = error_code;
744 tsk->thread.trap_no = 13;
745
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200746 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
Andi Kleen03252912008-01-30 13:33:18 +0100747 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100749 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100751 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100752 print_vma_addr(" in ", regs->ip);
753 printk("\n");
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 force_sig(SIGSEGV, tsk);
757 return;
758 }
759
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100760 if (fixup_exception(regs))
761 return;
Andi Kleend1895182007-05-02 19:27:05 +0200762
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100763 tsk->thread.error_code = error_code;
764 tsk->thread.trap_no = 13;
765 if (notify_die(DIE_GPF, "general protection fault", regs,
766 error_code, 13, SIGSEGV) == NOTIFY_STOP)
767 return;
768 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100771static __kprobes void
772mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200774 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
775 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100776 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200777
Dave Jiangc0d12172007-07-19 01:49:46 -0700778#if defined(CONFIG_EDAC)
779 if(edac_handler_set()) {
780 edac_atomic_assert_error();
781 return;
782 }
783#endif
784
Don Zickus8da5add2006-09-26 10:52:27 +0200785 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200786 panic("NMI: Not continuing");
787
788 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Clear and disable the memory parity error line. */
791 reason = (reason & 0xf) | 4;
792 outb(reason, 0x61);
793}
794
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100795static __kprobes void
796io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 printk("NMI: IOCK error (debug interrupt?)\n");
799 show_registers(regs);
800
801 /* Re-enable the IOCK line, wait for a few seconds */
802 reason = (reason & 0xf) | 8;
803 outb(reason, 0x61);
804 mdelay(2000);
805 reason &= ~8;
806 outb(reason, 0x61);
807}
808
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100809static __kprobes void
810unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200811{
812 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
813 reason);
814 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200815
816 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200817 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200818
Don Zickusc41c5cd2006-09-26 10:52:27 +0200819 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700822/* Runs on IST stack. This code must keep interrupts off all the time.
823 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100824asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700827 int cpu;
828
829 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700832 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 reason = get_nmi_reason();
834
835 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100836 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 == NOTIFY_STOP)
838 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
840 * Ok, so this is none of the documented NMI sources,
841 * so it must be the NMI watchdog.
842 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200843 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200845 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200846 unknown_nmi_error(reason, regs);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return;
849 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100850 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return;
852
853 /* AK: following checks seem to be broken on modern chipsets. FIXME */
854
855 if (reason & 0x80)
856 mem_parity_error(reason, regs);
857 if (reason & 0x40)
858 io_check_error(reason, regs);
859}
860
Jan Beulichb556b352006-01-11 22:43:00 +0100861/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700862asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200864 trace_hardirqs_fixup();
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
867 return;
868 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200869 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200871 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700874/* Help handler running on IST stack to switch back to user stack
875 for scheduling or signal handling. The actual stack switch is done in
876 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100877asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700879 struct pt_regs *regs = eregs;
880 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100881 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700882 ;
883 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700884 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800885 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700886 /* Exception from kernel and interrupts are enabled. Move to
887 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100888 else if (eregs->flags & X86_EFLAGS_IF)
889 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700890 if (eregs != regs)
891 *regs = *eregs;
892 return regs;
893}
894
895/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700896asmlinkage void __kprobes do_debug(struct pt_regs * regs,
897 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700898{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long condition;
900 struct task_struct *tsk = current;
901 siginfo_t info;
902
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100903 trace_hardirqs_fixup();
904
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700905 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Roland McGrath10faa812008-01-30 13:30:54 +0100907 /*
908 * The processor cleared BTF, so don't mark that we need it set.
909 */
910 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
911 tsk->thread.debugctlmsr = 0;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700914 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700915 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700916
John Blackwooda65d17c2006-02-12 14:34:58 -0800917 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* Mask out spurious debug traps due to lazy DR7 setting */
920 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
921 if (!tsk->thread.debugreg7) {
922 goto clear_dr7;
923 }
924 }
925
926 tsk->thread.debugreg6 = condition;
927
Roland McGrathe1f28772008-01-30 13:30:50 +0100928
929 /*
930 * Single-stepping through TF: make sure we ignore any events in
931 * kernel space (but re-enable TF when returning to user mode).
932 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700933 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700934 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 /* Ok, finally something we can handle */
939 tsk->thread.trap_no = 1;
940 tsk->thread.error_code = error_code;
941 info.si_signo = SIGTRAP;
942 info.si_errno = 0;
943 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100944 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100945 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700948 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800949 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700950 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952clear_TF_reenable:
953 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100954 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800955 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Jan Beulich6e3f3612006-01-11 22:42:14 +0100958static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100960 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100962
Jan Beulich6e3f3612006-01-11 22:42:14 +0100963 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700964 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100965 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
970/*
971 * Note that we play around with the 'TS' bit in an attempt to get
972 * the correct behaviour even in the presence of the asynchronous
973 * IRQ13 behaviour
974 */
975asmlinkage void do_coprocessor_error(struct pt_regs *regs)
976{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100977 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct task_struct * task;
979 siginfo_t info;
980 unsigned short cwd, swd;
981
982 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700983 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100984 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return;
986
987 /*
988 * Save the info for the exception handler and clear the error.
989 */
990 task = current;
991 save_init_fpu(task);
992 task->thread.trap_no = 16;
993 task->thread.error_code = 0;
994 info.si_signo = SIGFPE;
995 info.si_errno = 0;
996 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100997 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
999 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1000 * status. 0x3f is the exception bits in these regs, 0x200 is the
1001 * C1 reg you need in case of a stack fault, 0x040 is the stack
1002 * fault bit. We should only be taking one exception at a time,
1003 * so if this combination doesn't produce any single exception,
1004 * then we have a bad program that isn't synchronizing its FPU usage
1005 * and it will suffer the consequences since we won't be able to
1006 * fully reproduce the context of the exception
1007 */
1008 cwd = get_fpu_cwd(task);
1009 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +02001010 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 case 0x000:
1012 default:
1013 break;
1014 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +02001015 /*
1016 * swd & 0x240 == 0x040: Stack Underflow
1017 * swd & 0x240 == 0x240: Stack Overflow
1018 * User must clear the SF bit (0x40) if set
1019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 info.si_code = FPE_FLTINV;
1021 break;
1022 case 0x002: /* Denormalize */
1023 case 0x010: /* Underflow */
1024 info.si_code = FPE_FLTUND;
1025 break;
1026 case 0x004: /* Zero Divide */
1027 info.si_code = FPE_FLTDIV;
1028 break;
1029 case 0x008: /* Overflow */
1030 info.si_code = FPE_FLTOVF;
1031 break;
1032 case 0x020: /* Precision */
1033 info.si_code = FPE_FLTRES;
1034 break;
1035 }
1036 force_sig_info(SIGFPE, &info, task);
1037}
1038
1039asmlinkage void bad_intr(void)
1040{
1041 printk("bad interrupt");
1042}
1043
1044asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1045{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001046 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct task_struct * task;
1048 siginfo_t info;
1049 unsigned short mxcsr;
1050
1051 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001052 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001053 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return;
1055
1056 /*
1057 * Save the info for the exception handler and clear the error.
1058 */
1059 task = current;
1060 save_init_fpu(task);
1061 task->thread.trap_no = 19;
1062 task->thread.error_code = 0;
1063 info.si_signo = SIGFPE;
1064 info.si_errno = 0;
1065 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001066 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
1068 * The SIMD FPU exceptions are handled a little differently, as there
1069 * is only a single status/control register. Thus, to determine which
1070 * unmasked exception was caught we must mask the exception mask bits
1071 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1072 */
1073 mxcsr = get_fpu_mxcsr(task);
1074 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1075 case 0x000:
1076 default:
1077 break;
1078 case 0x001: /* Invalid Op */
1079 info.si_code = FPE_FLTINV;
1080 break;
1081 case 0x002: /* Denormalize */
1082 case 0x010: /* Underflow */
1083 info.si_code = FPE_FLTUND;
1084 break;
1085 case 0x004: /* Zero Divide */
1086 info.si_code = FPE_FLTDIV;
1087 break;
1088 case 0x008: /* Overflow */
1089 info.si_code = FPE_FLTOVF;
1090 break;
1091 case 0x020: /* Precision */
1092 info.si_code = FPE_FLTRES;
1093 break;
1094 }
1095 force_sig_info(SIGFPE, &info, task);
1096}
1097
1098asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1099{
1100}
1101
1102asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1103{
1104}
1105
Jacob Shin89b831e2005-11-05 17:25:53 +01001106asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1107{
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/*
1111 * 'math_state_restore()' saves the current math information in the
1112 * old math state array, and gets the new ones from the current task
1113 *
1114 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1115 * Don't touch unless you *really* know how it works.
1116 */
1117asmlinkage void math_state_restore(void)
1118{
1119 struct task_struct *me = current;
1120 clts(); /* Allow maths ops (or we recurse) */
1121
1122 if (!used_math())
1123 init_fpu(me);
1124 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001125 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001126 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001128EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130void __init trap_init(void)
1131{
1132 set_intr_gate(0,&divide_error);
1133 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1134 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001135 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001136 set_system_gate(4,&overflow); /* int4 can be called from all */
1137 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 set_intr_gate(6,&invalid_op);
1139 set_intr_gate(7,&device_not_available);
1140 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1141 set_intr_gate(9,&coprocessor_segment_overrun);
1142 set_intr_gate(10,&invalid_TSS);
1143 set_intr_gate(11,&segment_not_present);
1144 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1145 set_intr_gate(13,&general_protection);
1146 set_intr_gate(14,&page_fault);
1147 set_intr_gate(15,&spurious_interrupt_bug);
1148 set_intr_gate(16,&coprocessor_error);
1149 set_intr_gate(17,&alignment_check);
1150#ifdef CONFIG_X86_MCE
1151 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1152#endif
1153 set_intr_gate(19,&simd_coprocessor_error);
1154
1155#ifdef CONFIG_IA32_EMULATION
1156 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1157#endif
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * Should be a barrier for any external CPU state.
1161 */
1162 cpu_init();
1163}
1164
1165
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001166static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001168 if (!s)
1169 return -EINVAL;
1170 if (!strcmp(s, "panic"))
1171 panic_on_oops = 1;
1172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001174early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176static int __init kstack_setup(char *s)
1177{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001178 if (!s)
1179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001183early_param("kstack", kstack_setup);
Arjan van de Vena25bd942008-01-30 13:33:08 +01001184
1185
1186static int __init code_bytes_setup(char *s)
1187{
1188 code_bytes = simple_strtoul(s, NULL, 0);
1189 if (code_bytes > 8192)
1190 code_bytes = 8192;
1191
1192 return 1;
1193}
1194__setup("code_bytes=", code_bytes_setup);