blob: ec6d3b2130c435556f2c61e9babe7de82df01d27 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074asmlinkage void alignment_check(void);
75asmlinkage void machine_check(void);
76asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Arjan van de Vena25bd942008-01-30 13:33:08 +010078static unsigned int code_bytes = 64;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static inline void conditional_sti(struct pt_regs *regs)
81{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010082 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 local_irq_enable();
84}
85
John Blackwooda65d17c2006-02-12 14:34:58 -080086static inline void preempt_conditional_sti(struct pt_regs *regs)
87{
Ingo Molnare8bff742008-02-13 20:21:06 +010088 inc_preempt_count();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010089 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080090 local_irq_enable();
91}
92
93static inline void preempt_conditional_cli(struct pt_regs *regs)
94{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010095 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080096 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +020097 /* Make sure to not schedule here because we could be running
98 on an exception stack. */
Ingo Molnare8bff742008-02-13 20:21:06 +010099 dec_preempt_count();
John Blackwooda65d17c2006-02-12 14:34:58 -0800100}
101
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100102int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
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{
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100106#ifdef CONFIG_KALLSYMS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned long offset = 0, symsize;
108 const char *symname;
109 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700110 char *delim = ":";
Cyrill Gorcunov85e2aee2008-01-30 13:33:33 +0100111 char namebuf[KSYM_NAME_LEN];
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100112 char reliab[4] = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Ingo Molnar3ac94932006-07-03 00:24:36 -0700114 symname = kallsyms_lookup(address, &symsize, &offset,
115 &modname, namebuf);
116 if (!symname) {
117 printk(" [<%016lx>]\n", address);
118 return;
119 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100120 if (!reliable)
121 strcpy(reliab, "? ");
122
Ingo Molnar3ac94932006-07-03 00:24:36 -0700123 if (!modname)
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100124 modname = delim = "";
Arjan van de Venbc850d62008-01-30 13:33:07 +0100125 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
126 address, reliab, delim, modname, delim, symname, offset, symsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700128 printk(" [<%016lx>]\n", address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Andi Kleen0a658002005-04-16 15:25:17 -0700132static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200133 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700134{
Jan Beulichb556b352006-01-11 22:43:00 +0100135 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700136 [DEBUG_STACK - 1] = "#DB",
137 [NMI_STACK - 1] = "NMI",
138 [DOUBLEFAULT_STACK - 1] = "#DF",
139 [STACKFAULT_STACK - 1] = "#SS",
140 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100141#if DEBUG_STKSZ > EXCEPTION_STKSZ
142 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
143#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700144 };
145 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700147 /*
148 * Iterate over all exception stacks, and figure out whether
149 * 'stack' is in one of them:
150 */
Andi Kleen0a658002005-04-16 15:25:17 -0700151 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200152 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700153 /*
154 * Is 'stack' above this exception frame's end?
155 * If yes then skip to the next frame.
156 */
Andi Kleen0a658002005-04-16 15:25:17 -0700157 if (stack >= end)
158 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700159 /*
160 * Is 'stack' above this exception frame's start address?
161 * If yes then we found the right frame.
162 */
Andi Kleen0a658002005-04-16 15:25:17 -0700163 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700164 /*
165 * Make sure we only iterate through an exception
166 * stack once. If it comes up for the second time
167 * then there's something wrong going on - just
168 * break out and return NULL:
169 */
Andi Kleen0a658002005-04-16 15:25:17 -0700170 if (*usedp & (1U << k))
171 break;
172 *usedp |= 1U << k;
173 *idp = ids[k];
174 return (unsigned long *)end;
175 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700176 /*
177 * If this is a debug stack, and if it has a larger size than
178 * the usual exception stacks, then 'stack' might still
179 * be within the lower portion of the debug stack:
180 */
Jan Beulichb556b352006-01-11 22:43:00 +0100181#if DEBUG_STKSZ > EXCEPTION_STKSZ
182 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
183 unsigned j = N_EXCEPTION_STACKS - 1;
184
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700185 /*
186 * Black magic. A large debug stack is composed of
187 * multiple exception stack entries, which we
188 * iterate through now. Dont look:
189 */
Jan Beulichb556b352006-01-11 22:43:00 +0100190 do {
191 ++j;
192 end -= EXCEPTION_STKSZ;
193 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
194 } while (stack < end - EXCEPTION_STKSZ);
195 if (*usedp & (1U << j))
196 break;
197 *usedp |= 1U << j;
198 *idp = ids[j];
199 return (unsigned long *)end;
200 }
201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Andi Kleenb615ebd2006-12-07 02:14:00 +0100206#define MSG(txt) ops->warning(data, txt)
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
Simon Arlott676b1852007-10-20 01:25:36 +0200209 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * process stack
211 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700212 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
214
Arjan van de Vene4a94562008-01-30 13:33:07 +0100215static inline int valid_stack_ptr(struct thread_info *tinfo,
216 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100217{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100218 void *t = tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100219 if (end) {
220 if (p < end && p >= (end-THREAD_SIZE))
221 return 1;
222 else
223 return 0;
224 }
225 return p > t && p < t + THREAD_SIZE - size;
226}
227
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100228/* The form of the top of the frame on the stack */
229struct stack_frame {
230 struct stack_frame *next_frame;
231 unsigned long return_address;
232};
233
234
Arjan van de Vene4a94562008-01-30 13:33:07 +0100235static inline unsigned long print_context_stack(struct thread_info *tinfo,
236 unsigned long *stack, unsigned long bp,
237 const struct stacktrace_ops *ops, void *data,
238 unsigned long *end)
239{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100240 struct stack_frame *frame = (struct stack_frame *)bp;
241
242 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
243 unsigned long addr;
244
245 addr = *stack;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100246 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100247 if ((unsigned long) stack == bp + 8) {
248 ops->address(data, addr, 1);
249 frame = frame->next_frame;
250 bp = (unsigned long) frame;
251 } else {
252 ops->address(data, addr, bp == 0);
253 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100254 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100255 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100256 }
257 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100258}
259
Andi Kleenb615ebd2006-12-07 02:14:00 +0100260void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100261 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200262 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Andrew Mortonda689332006-12-07 02:14:02 +0100264 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100265 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700266 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100267 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jan Beulichb538ed22006-06-26 13:57:32 +0200269 if (!tsk)
270 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100271 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200272
Andi Kleenc0b766f2006-09-26 10:52:34 +0200273 if (!stack) {
274 unsigned long dummy;
275 stack = &dummy;
276 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100277 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200278 }
279
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100280#ifdef CONFIG_FRAME_POINTER
281 if (!bp) {
282 if (tsk == current) {
283 /* Grab bp right from our regs */
284 asm("movq %%rbp, %0" : "=r" (bp):);
285 } else {
286 /* bp is the last reg pushed by switch_to */
287 bp = *(unsigned long *) tsk->thread.sp;
288 }
289 }
290#endif
291
292
Andi Kleen0a658002005-04-16 15:25:17 -0700293
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700294 /*
295 * Print function call entries in all stacks, starting at the
296 * current stack address. If the stacks consist of nested
297 * exceptions
298 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200299 for (;;) {
300 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700301 unsigned long *estack_end;
302 estack_end = in_exception_stack(cpu, (unsigned long)stack,
303 &used, &id);
304
305 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200306 if (ops->stack(data, id) < 0)
307 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100308
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100309 bp = print_context_stack(tinfo, stack, bp, ops,
310 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200311 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700312 /*
313 * We link to the next stack via the
314 * second-to-last pointer (index -2 to end) in the
315 * exception stack:
316 */
Andi Kleen0a658002005-04-16 15:25:17 -0700317 stack = (unsigned long *) estack_end[-2];
318 continue;
319 }
320 if (irqstack_end) {
321 unsigned long *irqstack;
322 irqstack = irqstack_end -
323 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
324
325 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200326 if (ops->stack(data, "IRQ") < 0)
327 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100328 bp = print_context_stack(tinfo, stack, bp,
329 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700330 /*
331 * We link to the next stack (which would be
332 * the process stack normally) the last
333 * pointer (index -1 to end) in the IRQ stack:
334 */
Andi Kleen0a658002005-04-16 15:25:17 -0700335 stack = (unsigned long *) (irqstack_end[-1]);
336 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200337 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700338 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
Andi Kleen0a658002005-04-16 15:25:17 -0700341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Andi Kleen0a658002005-04-16 15:25:17 -0700343
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700344 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200345 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700346 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100347 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100348 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200349}
350EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700351
Andi Kleenc0b766f2006-09-26 10:52:34 +0200352static void
353print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
354{
355 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk("\n");
357}
358
Andi Kleenc0b766f2006-09-26 10:52:34 +0200359static void print_trace_warning(void *data, char *msg)
360{
361 printk("%s\n", msg);
362}
363
364static int print_trace_stack(void *data, char *name)
365{
366 printk(" <%s> ", name);
367 return 0;
368}
369
Arjan van de Venbc850d62008-01-30 13:33:07 +0100370static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200371{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700372 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100373 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200374}
375
Jan Beulich9689ba82007-10-17 18:04:37 +0200376static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200377 .warning = print_trace_warning,
378 .warning_symbol = print_trace_warning_symbol,
379 .stack = print_trace_stack,
380 .address = print_trace_address,
381};
382
383void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100384show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
385 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200386{
387 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100388 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200389 printk("\n");
390}
391
392static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100393_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
394 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 unsigned long *stack;
397 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200398 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100399 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
400 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 // debugging aid: "show_stack(NULL, NULL);" prints the
403 // back trace for this cpu.
404
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100405 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100407 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100409 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 for(i=0; i < kstack_depth_to_print; i++) {
414 if (stack >= irqstack && stack <= irqstack_end) {
415 if (stack == irqstack_end) {
416 stack = (unsigned long *) (irqstack_end[-1]);
417 printk(" <EOI> ");
418 }
419 } else {
420 if (((long) stack & (THREAD_SIZE-1)) == 0)
421 break;
422 }
423 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700424 printk("\n");
425 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700426 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100428 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200429}
430
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100431void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200432{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100433 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * The architecture-independent dump_stack generator
438 */
439void dump_stack(void)
440{
441 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100442 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100443
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100444#ifdef CONFIG_FRAME_POINTER
445 if (!bp)
446 asm("movq %%rbp, %0" : "=r" (bp):);
447#endif
448
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100449 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
450 current->pid, current->comm, print_tainted(),
451 init_utsname()->release,
452 (int)strcspn(init_utsname()->version, " "),
453 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100454 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457EXPORT_SYMBOL(dump_stack);
458
459void show_registers(struct pt_regs *regs)
460{
461 int i;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100462 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200463 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100464 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100465 u8 *ip;
466 unsigned int code_prologue = code_bytes * 43 / 64;
467 unsigned int code_len = code_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100469 sp = regs->sp;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100470 ip = (u8 *) regs->ip - code_prologue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 printk("CPU %d ", cpu);
472 __show_regs(regs);
473 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800474 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /*
477 * When in-kernel, we also print out the stack and code at the
478 * time of the fault..
479 */
Arjan van de Vena25bd942008-01-30 13:33:08 +0100480 if (!user_mode(regs)) {
481 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100483 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Arjan van de Vena25bd942008-01-30 13:33:08 +0100484 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Arjan van de Vena25bd942008-01-30 13:33:08 +0100486 printk(KERN_EMERG "Code: ");
487 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
488 /* try starting at RIP */
489 ip = (u8 *) regs->ip;
490 code_len = code_len - code_prologue + 1;
491 }
492 for (i = 0; i < code_len; i++, ip++) {
493 if (ip < (u8 *)PAGE_OFFSET ||
494 probe_kernel_address(ip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 printk(" Bad RIP value.");
496 break;
497 }
Arjan van de Vena25bd942008-01-30 13:33:08 +0100498 if (ip == (u8 *)regs->ip)
499 printk("<%02x> ", c);
500 else
501 printk("%02x ", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504 printk("\n");
505}
506
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100507int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800508{
509 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100511 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800512 return 0;
513
514 return ud2 == 0x0b0f;
515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Andi Kleen39743c92007-10-19 20:35:03 +0200517static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200519static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100521unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700523 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200524 unsigned long flags;
525
Andrew Mortonabf0f102006-09-26 10:52:36 +0200526 oops_enter();
527
Jan Beulich12091402005-09-12 18:49:24 +0200528 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200529 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700530 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200531 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (cpu == die_owner)
533 /* nested oops. should stop eventually */;
534 else
Andi Kleen39743c92007-10-19 20:35:03 +0200535 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200537 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200538 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200540 bust_spinlocks(1);
541 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Jan Beulich22f59912008-01-30 13:31:23 +0100544void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200547 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200548 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200549 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200550 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200551 __raw_spin_unlock(&die_lock);
552 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100553 if (!regs) {
554 oops_exit();
555 return;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700558 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200559 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100560 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jan Beulich22f59912008-01-30 13:31:23 +0100563int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 static int die_counter;
566 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
567#ifdef CONFIG_PREEMPT
568 printk("PREEMPT ");
569#endif
570#ifdef CONFIG_SMP
571 printk("SMP ");
572#endif
573#ifdef CONFIG_DEBUG_PAGEALLOC
574 printk("DEBUG_PAGEALLOC");
575#endif
576 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100577 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
578 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700580 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Executive summary in case the oops scrolled away */
582 printk(KERN_ALERT "RIP ");
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100583 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100584 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200585 if (kexec_should_crash(current))
586 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590void die(const char * str, struct pt_regs * regs, long err)
591{
Jan Beulich12091402005-09-12 18:49:24 +0200592 unsigned long flags = oops_begin();
593
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800594 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100595 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800596
Jan Beulich22f59912008-01-30 13:31:23 +0100597 if (__die(str, regs, err))
598 regs = NULL;
599 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200602notrace __kprobes void
603die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jason Wessel737a4602008-03-07 16:34:16 -0600605 unsigned long flags;
Jan Beulich12091402005-09-12 18:49:24 +0200606
Jason Wessel737a4602008-03-07 16:34:16 -0600607 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
608 NOTIFY_STOP)
609 return;
610
611 flags = oops_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * We are in trouble anyway, lets at least try
614 * to get a message out.
615 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200616 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200618 if (kexec_should_crash(current))
619 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200620 if (do_panic || panic_on_oops)
621 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100622 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200623 nmi_exit();
624 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100625 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700628static void __kprobes do_trap(int trapnr, int signr, char *str,
629 struct pt_regs * regs, long error_code,
630 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100632 struct task_struct *tsk = current;
633
Jan Beulich6e3f3612006-01-11 22:42:14 +0100634 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200635 /*
636 * We want error_code and trap_no set for userspace
637 * faults and kernelspace faults which result in
638 * die(), but not kernelspace faults which are fixed
639 * up. die() gives the process no chance to handle
640 * the signal and notice the kernel fault information,
641 * so that won't result in polluting the information
642 * about previously queued, but not yet delivered,
643 * faults. See also do_general_protection below.
644 */
645 tsk->thread.error_code = error_code;
646 tsk->thread.trap_no = trapnr;
647
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200648 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
Andi Kleen03252912008-01-30 13:33:18 +0100649 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100651 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100653 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100654 print_vma_addr(" in ", regs->ip);
655 printk("\n");
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (info)
659 force_sig_info(signr, info, tsk);
660 else
661 force_sig(signr, tsk);
662 return;
663 }
664
665
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100666 if (!fixup_exception(regs)) {
667 tsk->thread.error_code = error_code;
668 tsk->thread.trap_no = trapnr;
669 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674#define DO_ERROR(trapnr, signr, str, name) \
675asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
676{ \
677 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
678 == NOTIFY_STOP) \
679 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200680 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 do_trap(trapnr, signr, str, regs, error_code, NULL); \
682}
683
684#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
685asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
686{ \
687 siginfo_t info; \
688 info.si_signo = signr; \
689 info.si_errno = 0; \
690 info.si_code = sicode; \
691 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100692 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
694 == NOTIFY_STOP) \
695 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200696 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 do_trap(trapnr, signr, str, regs, error_code, &info); \
698}
699
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100700DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701DO_ERROR( 4, SIGSEGV, "overflow", overflow)
702DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100703DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
705DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
706DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
707DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Andi Kleen40e59a62006-05-15 18:19:47 +0200708
709/* Runs on IST stack */
710asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
711{
712 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
713 12, SIGBUS) == NOTIFY_STOP)
714 return;
715 preempt_conditional_sti(regs);
716 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
717 preempt_conditional_cli(regs);
718}
Jan Beulicheca37c12006-01-11 22:42:17 +0100719
720asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
721{
722 static const char str[] = "double fault";
723 struct task_struct *tsk = current;
724
725 /* Return not checked because double check cannot be ignored */
726 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
727
728 tsk->thread.error_code = error_code;
729 tsk->thread.trap_no = 8;
730
731 /* This is always a kernel trap and never fixable (and thus must
732 never return). */
733 for (;;)
734 die(str, regs, error_code);
735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700737asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
738 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100740 struct task_struct *tsk = current;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 conditional_sti(regs);
743
Jan Beulich6e3f3612006-01-11 22:42:14 +0100744 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200745 tsk->thread.error_code = error_code;
746 tsk->thread.trap_no = 13;
747
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200748 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
Andi Kleen03252912008-01-30 13:33:18 +0100749 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100751 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100753 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100754 print_vma_addr(" in ", regs->ip);
755 printk("\n");
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 force_sig(SIGSEGV, tsk);
759 return;
760 }
761
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100762 if (fixup_exception(regs))
763 return;
Andi Kleend1895182007-05-02 19:27:05 +0200764
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100765 tsk->thread.error_code = error_code;
766 tsk->thread.trap_no = 13;
767 if (notify_die(DIE_GPF, "general protection fault", regs,
768 error_code, 13, SIGSEGV) == NOTIFY_STOP)
769 return;
770 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200773static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100774mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200776 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
777 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100778 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200779
Dave Jiangc0d12172007-07-19 01:49:46 -0700780#if defined(CONFIG_EDAC)
781 if(edac_handler_set()) {
782 edac_atomic_assert_error();
783 return;
784 }
785#endif
786
Don Zickus8da5add2006-09-26 10:52:27 +0200787 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200788 panic("NMI: Not continuing");
789
790 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* Clear and disable the memory parity error line. */
793 reason = (reason & 0xf) | 4;
794 outb(reason, 0x61);
795}
796
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200797static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100798io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 printk("NMI: IOCK error (debug interrupt?)\n");
801 show_registers(regs);
802
803 /* Re-enable the IOCK line, wait for a few seconds */
804 reason = (reason & 0xf) | 8;
805 outb(reason, 0x61);
806 mdelay(2000);
807 reason &= ~8;
808 outb(reason, 0x61);
809}
810
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200811static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100812unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200813{
Jason Wesseld3597522008-02-15 14:55:53 -0600814 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
815 return;
Don Zickusc41c5cd2006-09-26 10:52:27 +0200816 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
817 reason);
818 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200819
820 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200821 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200822
Don Zickusc41c5cd2006-09-26 10:52:27 +0200823 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700826/* Runs on IST stack. This code must keep interrupts off all the time.
827 Nested NMIs are prevented by the CPU. */
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200828asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700831 int cpu;
832
833 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700836 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 reason = get_nmi_reason();
838
839 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100840 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 == NOTIFY_STOP)
842 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
844 * Ok, so this is none of the documented NMI sources,
845 * so it must be the NMI watchdog.
846 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200847 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200849 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200850 unknown_nmi_error(reason, regs);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return;
853 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100854 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return;
856
857 /* AK: following checks seem to be broken on modern chipsets. FIXME */
858
859 if (reason & 0x80)
860 mem_parity_error(reason, regs);
861 if (reason & 0x40)
862 io_check_error(reason, regs);
863}
864
Jan Beulichb556b352006-01-11 22:43:00 +0100865/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700866asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200868 trace_hardirqs_fixup();
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
871 return;
872 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200873 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200875 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700878/* Help handler running on IST stack to switch back to user stack
879 for scheduling or signal handling. The actual stack switch is done in
880 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100881asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700883 struct pt_regs *regs = eregs;
884 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100885 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700886 ;
887 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700888 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800889 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700890 /* Exception from kernel and interrupts are enabled. Move to
891 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100892 else if (eregs->flags & X86_EFLAGS_IF)
893 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700894 if (eregs != regs)
895 *regs = *eregs;
896 return regs;
897}
898
899/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700900asmlinkage void __kprobes do_debug(struct pt_regs * regs,
901 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700902{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned long condition;
904 struct task_struct *tsk = current;
905 siginfo_t info;
906
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100907 trace_hardirqs_fixup();
908
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700909 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Roland McGrath10faa812008-01-30 13:30:54 +0100911 /*
912 * The processor cleared BTF, so don't mark that we need it set.
913 */
914 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
915 tsk->thread.debugctlmsr = 0;
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700918 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700919 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700920
John Blackwooda65d17c2006-02-12 14:34:58 -0800921 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Mask out spurious debug traps due to lazy DR7 setting */
924 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
925 if (!tsk->thread.debugreg7) {
926 goto clear_dr7;
927 }
928 }
929
930 tsk->thread.debugreg6 = condition;
931
Roland McGrathe1f28772008-01-30 13:30:50 +0100932
933 /*
934 * Single-stepping through TF: make sure we ignore any events in
935 * kernel space (but re-enable TF when returning to user mode).
936 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700937 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700938 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 /* Ok, finally something we can handle */
943 tsk->thread.trap_no = 1;
944 tsk->thread.error_code = error_code;
945 info.si_signo = SIGTRAP;
946 info.si_errno = 0;
947 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100948 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100949 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700952 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800953 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956clear_TF_reenable:
957 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100958 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800959 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Jan Beulich6e3f3612006-01-11 22:42:14 +0100962static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100964 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100966
Jan Beulich6e3f3612006-01-11 22:42:14 +0100967 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700968 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100969 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return 0;
972}
973
974/*
975 * Note that we play around with the 'TS' bit in an attempt to get
976 * the correct behaviour even in the presence of the asynchronous
977 * IRQ13 behaviour
978 */
979asmlinkage void do_coprocessor_error(struct pt_regs *regs)
980{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100981 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct task_struct * task;
983 siginfo_t info;
984 unsigned short cwd, swd;
985
986 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700987 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100988 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return;
990
991 /*
992 * Save the info for the exception handler and clear the error.
993 */
994 task = current;
995 save_init_fpu(task);
996 task->thread.trap_no = 16;
997 task->thread.error_code = 0;
998 info.si_signo = SIGFPE;
999 info.si_errno = 0;
1000 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001001 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /*
1003 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1004 * status. 0x3f is the exception bits in these regs, 0x200 is the
1005 * C1 reg you need in case of a stack fault, 0x040 is the stack
1006 * fault bit. We should only be taking one exception at a time,
1007 * so if this combination doesn't produce any single exception,
1008 * then we have a bad program that isn't synchronizing its FPU usage
1009 * and it will suffer the consequences since we won't be able to
1010 * fully reproduce the context of the exception
1011 */
1012 cwd = get_fpu_cwd(task);
1013 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +02001014 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 case 0x000:
1016 default:
1017 break;
1018 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +02001019 /*
1020 * swd & 0x240 == 0x040: Stack Underflow
1021 * swd & 0x240 == 0x240: Stack Overflow
1022 * User must clear the SF bit (0x40) if set
1023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 info.si_code = FPE_FLTINV;
1025 break;
1026 case 0x002: /* Denormalize */
1027 case 0x010: /* Underflow */
1028 info.si_code = FPE_FLTUND;
1029 break;
1030 case 0x004: /* Zero Divide */
1031 info.si_code = FPE_FLTDIV;
1032 break;
1033 case 0x008: /* Overflow */
1034 info.si_code = FPE_FLTOVF;
1035 break;
1036 case 0x020: /* Precision */
1037 info.si_code = FPE_FLTRES;
1038 break;
1039 }
1040 force_sig_info(SIGFPE, &info, task);
1041}
1042
1043asmlinkage void bad_intr(void)
1044{
1045 printk("bad interrupt");
1046}
1047
1048asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1049{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001050 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct task_struct * task;
1052 siginfo_t info;
1053 unsigned short mxcsr;
1054
1055 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001056 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001057 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return;
1059
1060 /*
1061 * Save the info for the exception handler and clear the error.
1062 */
1063 task = current;
1064 save_init_fpu(task);
1065 task->thread.trap_no = 19;
1066 task->thread.error_code = 0;
1067 info.si_signo = SIGFPE;
1068 info.si_errno = 0;
1069 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001070 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
1072 * The SIMD FPU exceptions are handled a little differently, as there
1073 * is only a single status/control register. Thus, to determine which
1074 * unmasked exception was caught we must mask the exception mask bits
1075 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1076 */
1077 mxcsr = get_fpu_mxcsr(task);
1078 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1079 case 0x000:
1080 default:
1081 break;
1082 case 0x001: /* Invalid Op */
1083 info.si_code = FPE_FLTINV;
1084 break;
1085 case 0x002: /* Denormalize */
1086 case 0x010: /* Underflow */
1087 info.si_code = FPE_FLTUND;
1088 break;
1089 case 0x004: /* Zero Divide */
1090 info.si_code = FPE_FLTDIV;
1091 break;
1092 case 0x008: /* Overflow */
1093 info.si_code = FPE_FLTOVF;
1094 break;
1095 case 0x020: /* Precision */
1096 info.si_code = FPE_FLTRES;
1097 break;
1098 }
1099 force_sig_info(SIGFPE, &info, task);
1100}
1101
1102asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1103{
1104}
1105
1106asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1107{
1108}
1109
Jacob Shin89b831e2005-11-05 17:25:53 +01001110asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1111{
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
1115 * 'math_state_restore()' saves the current math information in the
1116 * old math state array, and gets the new ones from the current task
1117 *
1118 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1119 * Don't touch unless you *really* know how it works.
1120 */
1121asmlinkage void math_state_restore(void)
1122{
1123 struct task_struct *me = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Suresh Siddhaaa283f42008-03-10 15:28:05 -07001125 if (!used_math()) {
1126 local_irq_enable();
1127 /*
1128 * does a slab alloc which can sleep
1129 */
1130 if (init_fpu(me)) {
1131 /*
1132 * ran out of memory!
1133 */
1134 do_group_exit(SIGKILL);
1135 return;
1136 }
1137 local_irq_disable();
1138 }
1139
1140 clts(); /* Allow maths ops (or we recurse) */
Suresh Siddha61c46282008-03-10 15:28:04 -07001141 restore_fpu_checking(&me->thread.xstate->fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001142 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001143 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001145EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147void __init trap_init(void)
1148{
1149 set_intr_gate(0,&divide_error);
1150 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1151 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001152 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001153 set_system_gate(4,&overflow); /* int4 can be called from all */
1154 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 set_intr_gate(6,&invalid_op);
1156 set_intr_gate(7,&device_not_available);
1157 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1158 set_intr_gate(9,&coprocessor_segment_overrun);
1159 set_intr_gate(10,&invalid_TSS);
1160 set_intr_gate(11,&segment_not_present);
1161 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1162 set_intr_gate(13,&general_protection);
1163 set_intr_gate(14,&page_fault);
1164 set_intr_gate(15,&spurious_interrupt_bug);
1165 set_intr_gate(16,&coprocessor_error);
1166 set_intr_gate(17,&alignment_check);
1167#ifdef CONFIG_X86_MCE
1168 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1169#endif
1170 set_intr_gate(19,&simd_coprocessor_error);
1171
1172#ifdef CONFIG_IA32_EMULATION
1173 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1174#endif
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
Suresh Siddha61c46282008-03-10 15:28:04 -07001177 * initialize the per thread extended state:
1178 */
1179 init_thread_xstate();
1180 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * Should be a barrier for any external CPU state.
1182 */
1183 cpu_init();
1184}
1185
1186
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001187static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001189 if (!s)
1190 return -EINVAL;
1191 if (!strcmp(s, "panic"))
1192 panic_on_oops = 1;
1193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001195early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197static int __init kstack_setup(char *s)
1198{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001199 if (!s)
1200 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001204early_param("kstack", kstack_setup);
Arjan van de Vena25bd942008-01-30 13:33:08 +01001205
1206
1207static int __init code_bytes_setup(char *s)
1208{
1209 code_bytes = simple_strtoul(s, NULL, 0);
1210 if (code_bytes > 8192)
1211 code_bytes = 8192;
1212
1213 return 1;
1214}
1215__setup("code_bytes=", code_bytes_setup);