blob: 33292ac814f4d676dac432403398f2a7c325fa96 [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
Glauber de Oliveira Costae32ede12008-03-19 14:25:35 -030036#include <mach_traps.h>
37
Dave Jiangc0d12172007-07-19 01:49:46 -070038#if defined(CONFIG_EDAC)
39#include <linux/edac.h>
40#endif
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
44#include <asm/atomic.h>
45#include <asm/debugreg.h>
46#include <asm/desc.h>
47#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020049#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/smp.h>
51#include <asm/pgalloc.h>
52#include <asm/pda.h>
53#include <asm/proto.h>
54#include <asm/nmi.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020055#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057asmlinkage 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Arjan van de Vena25bd942008-01-30 13:33:08 +010079static unsigned int code_bytes = 64;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static inline void conditional_sti(struct pt_regs *regs)
82{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010083 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 local_irq_enable();
85}
86
John Blackwooda65d17c2006-02-12 14:34:58 -080087static inline void preempt_conditional_sti(struct pt_regs *regs)
88{
Ingo Molnare8bff742008-02-13 20:21:06 +010089 inc_preempt_count();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010090 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080091 local_irq_enable();
92}
93
94static inline void preempt_conditional_cli(struct pt_regs *regs)
95{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010096 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080097 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +020098 /* Make sure to not schedule here because we could be running
99 on an exception stack. */
Ingo Molnare8bff742008-02-13 20:21:06 +0100100 dec_preempt_count();
John Blackwooda65d17c2006-02-12 14:34:58 -0800101}
102
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100103int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Arjan van de Venbc850d62008-01-30 13:33:07 +0100105void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700106{
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100107#ifdef CONFIG_KALLSYMS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned long offset = 0, symsize;
109 const char *symname;
110 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700111 char *delim = ":";
Cyrill Gorcunov85e2aee2008-01-30 13:33:33 +0100112 char namebuf[KSYM_NAME_LEN];
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100113 char reliab[4] = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ingo Molnar3ac94932006-07-03 00:24:36 -0700115 symname = kallsyms_lookup(address, &symsize, &offset,
116 &modname, namebuf);
117 if (!symname) {
118 printk(" [<%016lx>]\n", address);
119 return;
120 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100121 if (!reliable)
122 strcpy(reliab, "? ");
123
Ingo Molnar3ac94932006-07-03 00:24:36 -0700124 if (!modname)
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100125 modname = delim = "";
Arjan van de Venbc850d62008-01-30 13:33:07 +0100126 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
127 address, reliab, delim, modname, delim, symname, offset, symsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700129 printk(" [<%016lx>]\n", address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Andi Kleen0a658002005-04-16 15:25:17 -0700133static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200134 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700135{
Jan Beulichb556b352006-01-11 22:43:00 +0100136 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700137 [DEBUG_STACK - 1] = "#DB",
138 [NMI_STACK - 1] = "NMI",
139 [DOUBLEFAULT_STACK - 1] = "#DF",
140 [STACKFAULT_STACK - 1] = "#SS",
141 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100142#if DEBUG_STKSZ > EXCEPTION_STKSZ
143 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
144#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700145 };
146 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700148 /*
149 * Iterate over all exception stacks, and figure out whether
150 * 'stack' is in one of them:
151 */
Andi Kleen0a658002005-04-16 15:25:17 -0700152 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200153 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700154 /*
155 * Is 'stack' above this exception frame's end?
156 * If yes then skip to the next frame.
157 */
Andi Kleen0a658002005-04-16 15:25:17 -0700158 if (stack >= end)
159 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700160 /*
161 * Is 'stack' above this exception frame's start address?
162 * If yes then we found the right frame.
163 */
Andi Kleen0a658002005-04-16 15:25:17 -0700164 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700165 /*
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 */
Andi Kleen0a658002005-04-16 15:25:17 -0700171 if (*usedp & (1U << k))
172 break;
173 *usedp |= 1U << k;
174 *idp = ids[k];
175 return (unsigned long *)end;
176 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700177 /*
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 */
Jan Beulichb556b352006-01-11 22:43:00 +0100182#if DEBUG_STKSZ > EXCEPTION_STKSZ
183 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
184 unsigned j = N_EXCEPTION_STACKS - 1;
185
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700186 /*
187 * Black magic. A large debug stack is composed of
188 * multiple exception stack entries, which we
189 * iterate through now. Dont look:
190 */
Jan Beulichb556b352006-01-11 22:43:00 +0100191 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Andi Kleenb615ebd2006-12-07 02:14:00 +0100207#define MSG(txt) ops->warning(data, txt)
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Simon Arlott676b1852007-10-20 01:25:36 +0200210 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * process stack
212 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700213 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215
Arjan van de Vene4a94562008-01-30 13:33:07 +0100216static inline int valid_stack_ptr(struct thread_info *tinfo,
217 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100218{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100219 void *t = tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100220 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
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100229/* 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
Arjan van de Vene4a94562008-01-30 13:33:07 +0100236static 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{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100241 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;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100247 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100248 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 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100255 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100256 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100257 }
258 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100259}
260
Andi Kleenb615ebd2006-12-07 02:14:00 +0100261void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100262 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200263 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Andrew Mortonda689332006-12-07 02:14:02 +0100265 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100266 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700267 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100268 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Beulichb538ed22006-06-26 13:57:32 +0200270 if (!tsk)
271 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100272 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200273
Andi Kleenc0b766f2006-09-26 10:52:34 +0200274 if (!stack) {
275 unsigned long dummy;
276 stack = &dummy;
277 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100278 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200279 }
280
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100281#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
Andi Kleen0a658002005-04-16 15:25:17 -0700294
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700295 /*
296 * Print function call entries in all stacks, starting at the
297 * current stack address. If the stacks consist of nested
298 * exceptions
299 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200300 for (;;) {
301 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700302 unsigned long *estack_end;
303 estack_end = in_exception_stack(cpu, (unsigned long)stack,
304 &used, &id);
305
306 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200307 if (ops->stack(data, id) < 0)
308 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100309
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100310 bp = print_context_stack(tinfo, stack, bp, ops,
311 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200312 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700313 /*
314 * We link to the next stack via the
315 * second-to-last pointer (index -2 to end) in the
316 * exception stack:
317 */
Andi Kleen0a658002005-04-16 15:25:17 -0700318 stack = (unsigned long *) estack_end[-2];
319 continue;
320 }
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) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200327 if (ops->stack(data, "IRQ") < 0)
328 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100329 bp = print_context_stack(tinfo, stack, bp,
330 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700331 /*
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 */
Andi Kleen0a658002005-04-16 15:25:17 -0700336 stack = (unsigned long *) (irqstack_end[-1]);
337 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200338 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700339 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
Andi Kleen0a658002005-04-16 15:25:17 -0700342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Andi Kleen0a658002005-04-16 15:25:17 -0700344
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700345 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200346 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700347 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100348 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100349 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200350}
351EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700352
Andi Kleenc0b766f2006-09-26 10:52:34 +0200353static void
354print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
355{
356 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 printk("\n");
358}
359
Andi Kleenc0b766f2006-09-26 10:52:34 +0200360static 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}
370
Arjan van de Venbc850d62008-01-30 13:33:07 +0100371static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200372{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700373 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100374 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200375}
376
Jan Beulich9689ba82007-10-17 18:04:37 +0200377static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200378 .warning = print_trace_warning,
379 .warning_symbol = print_trace_warning_symbol,
380 .stack = print_trace_stack,
381 .address = print_trace_address,
382};
383
384void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100385show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
386 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200387{
388 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100389 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200390 printk("\n");
391}
392
393static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100394_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
395 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 unsigned long *stack;
398 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200399 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100400 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
401 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 // debugging aid: "show_stack(NULL, NULL);" prints the
404 // back trace for this cpu.
405
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100406 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100408 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100410 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100413 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 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))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700425 printk("\n");
426 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700427 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100429 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200430}
431
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100432void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200433{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100434 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/*
438 * The architecture-independent dump_stack generator
439 */
440void dump_stack(void)
441{
442 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100443 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100444
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100445#ifdef CONFIG_FRAME_POINTER
446 if (!bp)
447 asm("movq %%rbp, %0" : "=r" (bp):);
448#endif
449
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100450 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);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100455 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458EXPORT_SYMBOL(dump_stack);
459
460void show_registers(struct pt_regs *regs)
461{
462 int i;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100463 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200464 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100465 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100466 u8 *ip;
467 unsigned int code_prologue = code_bytes * 43 / 64;
468 unsigned int code_len = code_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100470 sp = regs->sp;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100471 ip = (u8 *) regs->ip - code_prologue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 printk("CPU %d ", cpu);
473 __show_regs(regs);
474 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800475 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * When in-kernel, we also print out the stack and code at the
479 * time of the fault..
480 */
Arjan van de Vena25bd942008-01-30 13:33:08 +0100481 if (!user_mode(regs)) {
482 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100484 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Arjan van de Vena25bd942008-01-30 13:33:08 +0100485 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Arjan van de Vena25bd942008-01-30 13:33:08 +0100487 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 printk(" Bad RIP value.");
497 break;
498 }
Arjan van de Vena25bd942008-01-30 13:33:08 +0100499 if (ip == (u8 *)regs->ip)
500 printk("<%02x> ", c);
501 else
502 printk("%02x ", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 printk("\n");
506}
507
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100508int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800509{
510 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100512 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800513 return 0;
514
515 return ud2 == 0x0b0f;
516}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Andi Kleen39743c92007-10-19 20:35:03 +0200518static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200520static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100522unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700524 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200525 unsigned long flags;
526
Andrew Mortonabf0f102006-09-26 10:52:36 +0200527 oops_enter();
528
Jan Beulich12091402005-09-12 18:49:24 +0200529 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200530 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700531 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200532 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (cpu == die_owner)
534 /* nested oops. should stop eventually */;
535 else
Andi Kleen39743c92007-10-19 20:35:03 +0200536 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200538 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200539 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200541 bust_spinlocks(1);
542 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Jan Beulich22f59912008-01-30 13:31:23 +0100545void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200548 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200549 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200550 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200551 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200552 __raw_spin_unlock(&die_lock);
553 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100554 if (!regs) {
555 oops_exit();
556 return;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700559 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200560 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100561 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jan Beulich22f59912008-01-30 13:31:23 +0100564int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
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");
Jan Beulich22f59912008-01-30 13:31:23 +0100578 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
579 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700581 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Executive summary in case the oops scrolled away */
583 printk(KERN_ALERT "RIP ");
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100584 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100585 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200586 if (kexec_should_crash(current))
587 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591void die(const char * str, struct pt_regs * regs, long err)
592{
Jan Beulich12091402005-09-12 18:49:24 +0200593 unsigned long flags = oops_begin();
594
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800595 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100596 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800597
Jan Beulich22f59912008-01-30 13:31:23 +0100598 if (__die(str, regs, err))
599 regs = NULL;
600 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Andi Kleenfac58552006-09-26 10:52:27 +0200603void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jan Beulich12091402005-09-12 18:49:24 +0200605 unsigned long flags = oops_begin();
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /*
608 * We are in trouble anyway, lets at least try
609 * to get a message out.
610 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200611 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200613 if (kexec_should_crash(current))
614 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200615 if (do_panic || panic_on_oops)
616 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100617 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200618 nmi_exit();
619 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100620 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700623static void __kprobes do_trap(int trapnr, int signr, char *str,
624 struct pt_regs * regs, long error_code,
625 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100627 struct task_struct *tsk = current;
628
Jan Beulich6e3f3612006-01-11 22:42:14 +0100629 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200630 /*
631 * We want error_code and trap_no set for userspace
632 * faults and kernelspace faults which result in
633 * die(), but not kernelspace faults which are fixed
634 * up. die() gives the process no chance to handle
635 * the signal and notice the kernel fault information,
636 * so that won't result in polluting the information
637 * about previously queued, but not yet delivered,
638 * faults. See also do_general_protection below.
639 */
640 tsk->thread.error_code = error_code;
641 tsk->thread.trap_no = trapnr;
642
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200643 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
Andi Kleen03252912008-01-30 13:33:18 +0100644 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100646 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100648 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100649 print_vma_addr(" in ", regs->ip);
650 printk("\n");
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (info)
654 force_sig_info(signr, info, tsk);
655 else
656 force_sig(signr, tsk);
657 return;
658 }
659
660
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100661 if (!fixup_exception(regs)) {
662 tsk->thread.error_code = error_code;
663 tsk->thread.trap_no = trapnr;
664 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669#define DO_ERROR(trapnr, signr, str, name) \
670asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
671{ \
672 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
673 == NOTIFY_STOP) \
674 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200675 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 do_trap(trapnr, signr, str, regs, error_code, NULL); \
677}
678
679#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
680asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
681{ \
682 siginfo_t info; \
683 info.si_signo = signr; \
684 info.si_errno = 0; \
685 info.si_code = sicode; \
686 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100687 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
689 == NOTIFY_STOP) \
690 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200691 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 do_trap(trapnr, signr, str, regs, error_code, &info); \
693}
694
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100695DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696DO_ERROR( 4, SIGSEGV, "overflow", overflow)
697DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100698DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
700DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
701DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
702DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
703DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
704DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200705
706/* Runs on IST stack */
707asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
708{
709 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
710 12, SIGBUS) == NOTIFY_STOP)
711 return;
712 preempt_conditional_sti(regs);
713 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
714 preempt_conditional_cli(regs);
715}
Jan Beulicheca37c12006-01-11 22:42:17 +0100716
717asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
718{
719 static const char str[] = "double fault";
720 struct task_struct *tsk = current;
721
722 /* Return not checked because double check cannot be ignored */
723 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
724
725 tsk->thread.error_code = error_code;
726 tsk->thread.trap_no = 8;
727
728 /* This is always a kernel trap and never fixable (and thus must
729 never return). */
730 for (;;)
731 die(str, regs, error_code);
732}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700734asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
735 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100737 struct task_struct *tsk = current;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 conditional_sti(regs);
740
Jan Beulich6e3f3612006-01-11 22:42:14 +0100741 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200742 tsk->thread.error_code = error_code;
743 tsk->thread.trap_no = 13;
744
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200745 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
Andi Kleen03252912008-01-30 13:33:18 +0100746 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100748 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100750 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100751 print_vma_addr(" in ", regs->ip);
752 printk("\n");
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 force_sig(SIGSEGV, tsk);
756 return;
757 }
758
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100759 if (fixup_exception(regs))
760 return;
Andi Kleend1895182007-05-02 19:27:05 +0200761
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100762 tsk->thread.error_code = error_code;
763 tsk->thread.trap_no = 13;
764 if (notify_die(DIE_GPF, "general protection fault", regs,
765 error_code, 13, SIGSEGV) == NOTIFY_STOP)
766 return;
767 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100770static __kprobes void
771mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200773 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
774 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100775 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200776
Dave Jiangc0d12172007-07-19 01:49:46 -0700777#if defined(CONFIG_EDAC)
778 if(edac_handler_set()) {
779 edac_atomic_assert_error();
780 return;
781 }
782#endif
783
Don Zickus8da5add2006-09-26 10:52:27 +0200784 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200785 panic("NMI: Not continuing");
786
787 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* Clear and disable the memory parity error line. */
790 reason = (reason & 0xf) | 4;
791 outb(reason, 0x61);
792}
793
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100794static __kprobes void
795io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 printk("NMI: IOCK error (debug interrupt?)\n");
798 show_registers(regs);
799
800 /* Re-enable the IOCK line, wait for a few seconds */
801 reason = (reason & 0xf) | 8;
802 outb(reason, 0x61);
803 mdelay(2000);
804 reason &= ~8;
805 outb(reason, 0x61);
806}
807
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100808static __kprobes void
809unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200810{
811 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
812 reason);
813 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200814
815 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200816 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200817
Don Zickusc41c5cd2006-09-26 10:52:27 +0200818 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700821/* Runs on IST stack. This code must keep interrupts off all the time.
822 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100823asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700826 int cpu;
827
828 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700831 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 reason = get_nmi_reason();
833
834 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100835 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 == NOTIFY_STOP)
837 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * Ok, so this is none of the documented NMI sources,
840 * so it must be the NMI watchdog.
841 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200842 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200844 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200845 unknown_nmi_error(reason, regs);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return;
848 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100849 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return;
851
852 /* AK: following checks seem to be broken on modern chipsets. FIXME */
853
854 if (reason & 0x80)
855 mem_parity_error(reason, regs);
856 if (reason & 0x40)
857 io_check_error(reason, regs);
858}
859
Jan Beulichb556b352006-01-11 22:43:00 +0100860/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700861asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200863 trace_hardirqs_fixup();
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
866 return;
867 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200868 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200870 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700873/* Help handler running on IST stack to switch back to user stack
874 for scheduling or signal handling. The actual stack switch is done in
875 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100876asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700878 struct pt_regs *regs = eregs;
879 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100880 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700881 ;
882 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700883 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800884 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700885 /* Exception from kernel and interrupts are enabled. Move to
886 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100887 else if (eregs->flags & X86_EFLAGS_IF)
888 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700889 if (eregs != regs)
890 *regs = *eregs;
891 return regs;
892}
893
894/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700895asmlinkage void __kprobes do_debug(struct pt_regs * regs,
896 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700897{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 unsigned long condition;
899 struct task_struct *tsk = current;
900 siginfo_t info;
901
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100902 trace_hardirqs_fixup();
903
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700904 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Roland McGrath10faa812008-01-30 13:30:54 +0100906 /*
907 * The processor cleared BTF, so don't mark that we need it set.
908 */
909 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
910 tsk->thread.debugctlmsr = 0;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700913 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700914 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700915
John Blackwooda65d17c2006-02-12 14:34:58 -0800916 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* Mask out spurious debug traps due to lazy DR7 setting */
919 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
920 if (!tsk->thread.debugreg7) {
921 goto clear_dr7;
922 }
923 }
924
925 tsk->thread.debugreg6 = condition;
926
Roland McGrathe1f28772008-01-30 13:30:50 +0100927
928 /*
929 * Single-stepping through TF: make sure we ignore any events in
930 * kernel space (but re-enable TF when returning to user mode).
931 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700932 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700933 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 /* Ok, finally something we can handle */
938 tsk->thread.trap_no = 1;
939 tsk->thread.error_code = error_code;
940 info.si_signo = SIGTRAP;
941 info.si_errno = 0;
942 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100943 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100944 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700947 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800948 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951clear_TF_reenable:
952 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100953 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800954 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Jan Beulich6e3f3612006-01-11 22:42:14 +0100957static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100959 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100961
Jan Beulich6e3f3612006-01-11 22:42:14 +0100962 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700963 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100964 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967}
968
969/*
970 * Note that we play around with the 'TS' bit in an attempt to get
971 * the correct behaviour even in the presence of the asynchronous
972 * IRQ13 behaviour
973 */
974asmlinkage void do_coprocessor_error(struct pt_regs *regs)
975{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100976 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct task_struct * task;
978 siginfo_t info;
979 unsigned short cwd, swd;
980
981 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700982 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100983 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return;
985
986 /*
987 * Save the info for the exception handler and clear the error.
988 */
989 task = current;
990 save_init_fpu(task);
991 task->thread.trap_no = 16;
992 task->thread.error_code = 0;
993 info.si_signo = SIGFPE;
994 info.si_errno = 0;
995 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100996 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * (~cwd & swd) will mask out exceptions that are not set to unmasked
999 * status. 0x3f is the exception bits in these regs, 0x200 is the
1000 * C1 reg you need in case of a stack fault, 0x040 is the stack
1001 * fault bit. We should only be taking one exception at a time,
1002 * so if this combination doesn't produce any single exception,
1003 * then we have a bad program that isn't synchronizing its FPU usage
1004 * and it will suffer the consequences since we won't be able to
1005 * fully reproduce the context of the exception
1006 */
1007 cwd = get_fpu_cwd(task);
1008 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +02001009 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 case 0x000:
1011 default:
1012 break;
1013 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +02001014 /*
1015 * swd & 0x240 == 0x040: Stack Underflow
1016 * swd & 0x240 == 0x240: Stack Overflow
1017 * User must clear the SF bit (0x40) if set
1018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 info.si_code = FPE_FLTINV;
1020 break;
1021 case 0x002: /* Denormalize */
1022 case 0x010: /* Underflow */
1023 info.si_code = FPE_FLTUND;
1024 break;
1025 case 0x004: /* Zero Divide */
1026 info.si_code = FPE_FLTDIV;
1027 break;
1028 case 0x008: /* Overflow */
1029 info.si_code = FPE_FLTOVF;
1030 break;
1031 case 0x020: /* Precision */
1032 info.si_code = FPE_FLTRES;
1033 break;
1034 }
1035 force_sig_info(SIGFPE, &info, task);
1036}
1037
1038asmlinkage void bad_intr(void)
1039{
1040 printk("bad interrupt");
1041}
1042
1043asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1044{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001045 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct task_struct * task;
1047 siginfo_t info;
1048 unsigned short mxcsr;
1049
1050 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001051 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001052 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return;
1054
1055 /*
1056 * Save the info for the exception handler and clear the error.
1057 */
1058 task = current;
1059 save_init_fpu(task);
1060 task->thread.trap_no = 19;
1061 task->thread.error_code = 0;
1062 info.si_signo = SIGFPE;
1063 info.si_errno = 0;
1064 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001065 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * The SIMD FPU exceptions are handled a little differently, as there
1068 * is only a single status/control register. Thus, to determine which
1069 * unmasked exception was caught we must mask the exception mask bits
1070 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1071 */
1072 mxcsr = get_fpu_mxcsr(task);
1073 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1074 case 0x000:
1075 default:
1076 break;
1077 case 0x001: /* Invalid Op */
1078 info.si_code = FPE_FLTINV;
1079 break;
1080 case 0x002: /* Denormalize */
1081 case 0x010: /* Underflow */
1082 info.si_code = FPE_FLTUND;
1083 break;
1084 case 0x004: /* Zero Divide */
1085 info.si_code = FPE_FLTDIV;
1086 break;
1087 case 0x008: /* Overflow */
1088 info.si_code = FPE_FLTOVF;
1089 break;
1090 case 0x020: /* Precision */
1091 info.si_code = FPE_FLTRES;
1092 break;
1093 }
1094 force_sig_info(SIGFPE, &info, task);
1095}
1096
1097asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1098{
1099}
1100
1101asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1102{
1103}
1104
Jacob Shin89b831e2005-11-05 17:25:53 +01001105asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1106{
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/*
1110 * 'math_state_restore()' saves the current math information in the
1111 * old math state array, and gets the new ones from the current task
1112 *
1113 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1114 * Don't touch unless you *really* know how it works.
1115 */
1116asmlinkage void math_state_restore(void)
1117{
1118 struct task_struct *me = current;
1119 clts(); /* Allow maths ops (or we recurse) */
1120
1121 if (!used_math())
1122 init_fpu(me);
1123 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001124 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001125 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001127EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129void __init trap_init(void)
1130{
1131 set_intr_gate(0,&divide_error);
1132 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1133 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001134 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001135 set_system_gate(4,&overflow); /* int4 can be called from all */
1136 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 set_intr_gate(6,&invalid_op);
1138 set_intr_gate(7,&device_not_available);
1139 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1140 set_intr_gate(9,&coprocessor_segment_overrun);
1141 set_intr_gate(10,&invalid_TSS);
1142 set_intr_gate(11,&segment_not_present);
1143 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1144 set_intr_gate(13,&general_protection);
1145 set_intr_gate(14,&page_fault);
1146 set_intr_gate(15,&spurious_interrupt_bug);
1147 set_intr_gate(16,&coprocessor_error);
1148 set_intr_gate(17,&alignment_check);
1149#ifdef CONFIG_X86_MCE
1150 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1151#endif
1152 set_intr_gate(19,&simd_coprocessor_error);
1153
1154#ifdef CONFIG_IA32_EMULATION
1155 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1156#endif
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /*
1159 * Should be a barrier for any external CPU state.
1160 */
1161 cpu_init();
1162}
1163
1164
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001165static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001167 if (!s)
1168 return -EINVAL;
1169 if (!strcmp(s, "panic"))
1170 panic_on_oops = 1;
1171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001173early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175static int __init kstack_setup(char *s)
1176{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001177 if (!s)
1178 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001182early_param("kstack", kstack_setup);
Arjan van de Vena25bd942008-01-30 13:33:08 +01001183
1184
1185static int __init code_bytes_setup(char *s)
1186{
1187 code_bytes = simple_strtoul(s, NULL, 0);
1188 if (code_bytes > 8192)
1189 code_bytes = 8192;
1190
1191 return 1;
1192}
1193__setup("code_bytes=", code_bytes_setup);