blob: e4a380797dc13005d466b4987f2be95229a76c93 [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
Cyrill Gorcunovc6425b92008-05-24 19:36:33 +040079int panic_on_unrecovered_nmi;
Arjan van de Vena25bd942008-01-30 13:33:08 +010080static unsigned int code_bytes = 64;
Cyrill Gorcunovc6425b92008-05-24 19:36:33 +040081static unsigned ignore_nmis;
Arjan van de Vena25bd942008-01-30 13:33:08 +010082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static inline void conditional_sti(struct pt_regs *regs)
84{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010085 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 local_irq_enable();
87}
88
John Blackwooda65d17c2006-02-12 14:34:58 -080089static inline void preempt_conditional_sti(struct pt_regs *regs)
90{
Ingo Molnare8bff742008-02-13 20:21:06 +010091 inc_preempt_count();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010092 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080093 local_irq_enable();
94}
95
96static inline void preempt_conditional_cli(struct pt_regs *regs)
97{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010098 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080099 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +0200100 /* Make sure to not schedule here because we could be running
101 on an exception stack. */
Ingo Molnare8bff742008-02-13 20:21:06 +0100102 dec_preempt_count();
John Blackwooda65d17c2006-02-12 14:34:58 -0800103}
104
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100105int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Arjan van de Venbc850d62008-01-30 13:33:07 +0100107void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700108{
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100109#ifdef CONFIG_KALLSYMS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned long offset = 0, symsize;
111 const char *symname;
112 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700113 char *delim = ":";
Cyrill Gorcunov85e2aee2008-01-30 13:33:33 +0100114 char namebuf[KSYM_NAME_LEN];
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100115 char reliab[4] = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Ingo Molnar3ac94932006-07-03 00:24:36 -0700117 symname = kallsyms_lookup(address, &symsize, &offset,
118 &modname, namebuf);
119 if (!symname) {
120 printk(" [<%016lx>]\n", address);
121 return;
122 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100123 if (!reliable)
124 strcpy(reliab, "? ");
125
Ingo Molnar3ac94932006-07-03 00:24:36 -0700126 if (!modname)
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100127 modname = delim = "";
Arjan van de Venbc850d62008-01-30 13:33:07 +0100128 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
129 address, reliab, delim, modname, delim, symname, offset, symsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700131 printk(" [<%016lx>]\n", address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Andi Kleen0a658002005-04-16 15:25:17 -0700135static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200136 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700137{
Jan Beulichb556b352006-01-11 22:43:00 +0100138 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700139 [DEBUG_STACK - 1] = "#DB",
140 [NMI_STACK - 1] = "NMI",
141 [DOUBLEFAULT_STACK - 1] = "#DF",
142 [STACKFAULT_STACK - 1] = "#SS",
143 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100144#if DEBUG_STKSZ > EXCEPTION_STKSZ
145 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
146#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700147 };
148 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700150 /*
151 * Iterate over all exception stacks, and figure out whether
152 * 'stack' is in one of them:
153 */
Andi Kleen0a658002005-04-16 15:25:17 -0700154 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200155 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700156 /*
157 * Is 'stack' above this exception frame's end?
158 * If yes then skip to the next frame.
159 */
Andi Kleen0a658002005-04-16 15:25:17 -0700160 if (stack >= end)
161 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700162 /*
163 * Is 'stack' above this exception frame's start address?
164 * If yes then we found the right frame.
165 */
Andi Kleen0a658002005-04-16 15:25:17 -0700166 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700167 /*
168 * Make sure we only iterate through an exception
169 * stack once. If it comes up for the second time
170 * then there's something wrong going on - just
171 * break out and return NULL:
172 */
Andi Kleen0a658002005-04-16 15:25:17 -0700173 if (*usedp & (1U << k))
174 break;
175 *usedp |= 1U << k;
176 *idp = ids[k];
177 return (unsigned long *)end;
178 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700179 /*
180 * If this is a debug stack, and if it has a larger size than
181 * the usual exception stacks, then 'stack' might still
182 * be within the lower portion of the debug stack:
183 */
Jan Beulichb556b352006-01-11 22:43:00 +0100184#if DEBUG_STKSZ > EXCEPTION_STKSZ
185 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
186 unsigned j = N_EXCEPTION_STACKS - 1;
187
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700188 /*
189 * Black magic. A large debug stack is composed of
190 * multiple exception stack entries, which we
191 * iterate through now. Dont look:
192 */
Jan Beulichb556b352006-01-11 22:43:00 +0100193 do {
194 ++j;
195 end -= EXCEPTION_STKSZ;
196 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
197 } while (stack < end - EXCEPTION_STKSZ);
198 if (*usedp & (1U << j))
199 break;
200 *usedp |= 1U << j;
201 *idp = ids[j];
202 return (unsigned long *)end;
203 }
204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Andi Kleenb615ebd2006-12-07 02:14:00 +0100209#define MSG(txt) ops->warning(data, txt)
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Simon Arlott676b1852007-10-20 01:25:36 +0200212 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * process stack
214 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700215 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
217
Arjan van de Vene4a94562008-01-30 13:33:07 +0100218static inline int valid_stack_ptr(struct thread_info *tinfo,
219 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100220{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100221 void *t = tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100222 if (end) {
223 if (p < end && p >= (end-THREAD_SIZE))
224 return 1;
225 else
226 return 0;
227 }
228 return p > t && p < t + THREAD_SIZE - size;
229}
230
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100231/* The form of the top of the frame on the stack */
232struct stack_frame {
233 struct stack_frame *next_frame;
234 unsigned long return_address;
235};
236
237
Arjan van de Vene4a94562008-01-30 13:33:07 +0100238static inline unsigned long print_context_stack(struct thread_info *tinfo,
239 unsigned long *stack, unsigned long bp,
240 const struct stacktrace_ops *ops, void *data,
241 unsigned long *end)
242{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100243 struct stack_frame *frame = (struct stack_frame *)bp;
244
245 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
246 unsigned long addr;
247
248 addr = *stack;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100249 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100250 if ((unsigned long) stack == bp + 8) {
251 ops->address(data, addr, 1);
252 frame = frame->next_frame;
253 bp = (unsigned long) frame;
254 } else {
255 ops->address(data, addr, bp == 0);
256 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100257 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100258 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100259 }
260 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100261}
262
Andi Kleenb615ebd2006-12-07 02:14:00 +0100263void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100264 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200265 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Andrew Mortonda689332006-12-07 02:14:02 +0100267 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100268 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700269 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100270 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jan Beulichb538ed22006-06-26 13:57:32 +0200272 if (!tsk)
273 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100274 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200275
Andi Kleenc0b766f2006-09-26 10:52:34 +0200276 if (!stack) {
277 unsigned long dummy;
278 stack = &dummy;
279 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100280 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200281 }
282
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100283#ifdef CONFIG_FRAME_POINTER
284 if (!bp) {
285 if (tsk == current) {
286 /* Grab bp right from our regs */
287 asm("movq %%rbp, %0" : "=r" (bp):);
288 } else {
289 /* bp is the last reg pushed by switch_to */
290 bp = *(unsigned long *) tsk->thread.sp;
291 }
292 }
293#endif
294
295
Andi Kleen0a658002005-04-16 15:25:17 -0700296
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700297 /*
298 * Print function call entries in all stacks, starting at the
299 * current stack address. If the stacks consist of nested
300 * exceptions
301 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200302 for (;;) {
303 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700304 unsigned long *estack_end;
305 estack_end = in_exception_stack(cpu, (unsigned long)stack,
306 &used, &id);
307
308 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200309 if (ops->stack(data, id) < 0)
310 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100311
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100312 bp = print_context_stack(tinfo, stack, bp, ops,
313 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200314 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700315 /*
316 * We link to the next stack via the
317 * second-to-last pointer (index -2 to end) in the
318 * exception stack:
319 */
Andi Kleen0a658002005-04-16 15:25:17 -0700320 stack = (unsigned long *) estack_end[-2];
321 continue;
322 }
323 if (irqstack_end) {
324 unsigned long *irqstack;
325 irqstack = irqstack_end -
326 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
327
328 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200329 if (ops->stack(data, "IRQ") < 0)
330 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100331 bp = print_context_stack(tinfo, stack, bp,
332 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700333 /*
334 * We link to the next stack (which would be
335 * the process stack normally) the last
336 * pointer (index -1 to end) in the IRQ stack:
337 */
Andi Kleen0a658002005-04-16 15:25:17 -0700338 stack = (unsigned long *) (irqstack_end[-1]);
339 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200340 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700341 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 }
Andi Kleen0a658002005-04-16 15:25:17 -0700344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Andi Kleen0a658002005-04-16 15:25:17 -0700346
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700347 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200348 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700349 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100350 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100351 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200352}
353EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700354
Andi Kleenc0b766f2006-09-26 10:52:34 +0200355static void
356print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
357{
358 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 printk("\n");
360}
361
Andi Kleenc0b766f2006-09-26 10:52:34 +0200362static void print_trace_warning(void *data, char *msg)
363{
364 printk("%s\n", msg);
365}
366
367static int print_trace_stack(void *data, char *name)
368{
369 printk(" <%s> ", name);
370 return 0;
371}
372
Arjan van de Venbc850d62008-01-30 13:33:07 +0100373static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200374{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700375 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100376 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200377}
378
Jan Beulich9689ba82007-10-17 18:04:37 +0200379static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200380 .warning = print_trace_warning,
381 .warning_symbol = print_trace_warning_symbol,
382 .stack = print_trace_stack,
383 .address = print_trace_address,
384};
385
386void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100387show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
388 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200389{
390 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100391 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200392 printk("\n");
393}
394
395static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100396_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
397 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 unsigned long *stack;
400 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200401 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100402 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
403 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 // debugging aid: "show_stack(NULL, NULL);" prints the
406 // back trace for this cpu.
407
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100408 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100410 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100415 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 for(i=0; i < kstack_depth_to_print; i++) {
417 if (stack >= irqstack && stack <= irqstack_end) {
418 if (stack == irqstack_end) {
419 stack = (unsigned long *) (irqstack_end[-1]);
420 printk(" <EOI> ");
421 }
422 } else {
423 if (((long) stack & (THREAD_SIZE-1)) == 0)
424 break;
425 }
426 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700427 printk("\n");
428 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700429 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100431 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200432}
433
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100434void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200435{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100436 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/*
440 * The architecture-independent dump_stack generator
441 */
442void dump_stack(void)
443{
444 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100445 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100446
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100447#ifdef CONFIG_FRAME_POINTER
448 if (!bp)
449 asm("movq %%rbp, %0" : "=r" (bp):);
450#endif
451
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100452 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
453 current->pid, current->comm, print_tainted(),
454 init_utsname()->release,
455 (int)strcspn(init_utsname()->version, " "),
456 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100457 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460EXPORT_SYMBOL(dump_stack);
461
462void show_registers(struct pt_regs *regs)
463{
464 int i;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100465 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200466 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100467 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100468 u8 *ip;
469 unsigned int code_prologue = code_bytes * 43 / 64;
470 unsigned int code_len = code_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100472 sp = regs->sp;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100473 ip = (u8 *) regs->ip - code_prologue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 printk("CPU %d ", cpu);
475 __show_regs(regs);
476 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800477 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /*
480 * When in-kernel, we also print out the stack and code at the
481 * time of the fault..
482 */
Arjan van de Vena25bd942008-01-30 13:33:08 +0100483 if (!user_mode(regs)) {
484 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100486 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Arjan van de Vena25bd942008-01-30 13:33:08 +0100487 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Arjan van de Vena25bd942008-01-30 13:33:08 +0100489 printk(KERN_EMERG "Code: ");
490 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
491 /* try starting at RIP */
492 ip = (u8 *) regs->ip;
493 code_len = code_len - code_prologue + 1;
494 }
495 for (i = 0; i < code_len; i++, ip++) {
496 if (ip < (u8 *)PAGE_OFFSET ||
497 probe_kernel_address(ip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 printk(" Bad RIP value.");
499 break;
500 }
Arjan van de Vena25bd942008-01-30 13:33:08 +0100501 if (ip == (u8 *)regs->ip)
502 printk("<%02x> ", c);
503 else
504 printk("%02x ", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 }
507 printk("\n");
508}
509
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100510int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800511{
512 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100514 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800515 return 0;
516
517 return ud2 == 0x0b0f;
518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Andi Kleen39743c92007-10-19 20:35:03 +0200520static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200522static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100524unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700526 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200527 unsigned long flags;
528
Andrew Mortonabf0f102006-09-26 10:52:36 +0200529 oops_enter();
530
Jan Beulich12091402005-09-12 18:49:24 +0200531 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200532 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700533 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200534 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (cpu == die_owner)
536 /* nested oops. should stop eventually */;
537 else
Andi Kleen39743c92007-10-19 20:35:03 +0200538 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200540 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200541 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200543 bust_spinlocks(1);
544 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Jan Beulich22f59912008-01-30 13:31:23 +0100547void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200550 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200551 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200552 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200553 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200554 __raw_spin_unlock(&die_lock);
555 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100556 if (!regs) {
557 oops_exit();
558 return;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700561 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200562 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100563 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200564}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jan Beulich22f59912008-01-30 13:31:23 +0100566int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 static int die_counter;
569 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
570#ifdef CONFIG_PREEMPT
571 printk("PREEMPT ");
572#endif
573#ifdef CONFIG_SMP
574 printk("SMP ");
575#endif
576#ifdef CONFIG_DEBUG_PAGEALLOC
577 printk("DEBUG_PAGEALLOC");
578#endif
579 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100580 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
581 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700583 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Executive summary in case the oops scrolled away */
585 printk(KERN_ALERT "RIP ");
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100586 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100587 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200588 if (kexec_should_crash(current))
589 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593void die(const char * str, struct pt_regs * regs, long err)
594{
Jan Beulich12091402005-09-12 18:49:24 +0200595 unsigned long flags = oops_begin();
596
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800597 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100598 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800599
Jan Beulich22f59912008-01-30 13:31:23 +0100600 if (__die(str, regs, err))
601 regs = NULL;
602 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200605notrace __kprobes void
606die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jason Wessel737a4602008-03-07 16:34:16 -0600608 unsigned long flags;
Jan Beulich12091402005-09-12 18:49:24 +0200609
Jason Wessel737a4602008-03-07 16:34:16 -0600610 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
611 NOTIFY_STOP)
612 return;
613
614 flags = oops_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * We are in trouble anyway, lets at least try
617 * to get a message out.
618 */
Cyrill Gorcunove56b3a12008-05-24 19:36:32 +0400619 printk(KERN_EMERG "%s", str);
620 printk(" on CPU%d, ip %08lx, registers:\n",
621 smp_processor_id(), regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200623 if (kexec_should_crash(current))
624 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200625 if (do_panic || panic_on_oops)
626 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100627 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200628 nmi_exit();
629 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100630 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700633static void __kprobes do_trap(int trapnr, int signr, char *str,
634 struct pt_regs * regs, long error_code,
635 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100637 struct task_struct *tsk = current;
638
Jan Beulich6e3f3612006-01-11 22:42:14 +0100639 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200640 /*
641 * We want error_code and trap_no set for userspace
642 * faults and kernelspace faults which result in
643 * die(), but not kernelspace faults which are fixed
644 * up. die() gives the process no chance to handle
645 * the signal and notice the kernel fault information,
646 * so that won't result in polluting the information
647 * about previously queued, but not yet delivered,
648 * faults. See also do_general_protection below.
649 */
650 tsk->thread.error_code = error_code;
651 tsk->thread.trap_no = trapnr;
652
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200653 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
Andi Kleen03252912008-01-30 13:33:18 +0100654 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100656 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100658 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100659 print_vma_addr(" in ", regs->ip);
660 printk("\n");
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (info)
664 force_sig_info(signr, info, tsk);
665 else
666 force_sig(signr, tsk);
667 return;
668 }
669
670
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100671 if (!fixup_exception(regs)) {
672 tsk->thread.error_code = error_code;
673 tsk->thread.trap_no = trapnr;
674 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100676 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679#define DO_ERROR(trapnr, signr, str, name) \
680asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
681{ \
682 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
683 == NOTIFY_STOP) \
684 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200685 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 do_trap(trapnr, signr, str, regs, error_code, NULL); \
687}
688
689#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
690asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
691{ \
692 siginfo_t info; \
693 info.si_signo = signr; \
694 info.si_errno = 0; \
695 info.si_code = sicode; \
696 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100697 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
699 == NOTIFY_STOP) \
700 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200701 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 do_trap(trapnr, signr, str, regs, error_code, &info); \
703}
704
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100705DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706DO_ERROR( 4, SIGSEGV, "overflow", overflow)
707DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100708DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
710DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
711DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
712DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
713DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
714DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200715
716/* Runs on IST stack */
717asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
718{
719 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
720 12, SIGBUS) == NOTIFY_STOP)
721 return;
722 preempt_conditional_sti(regs);
723 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
724 preempt_conditional_cli(regs);
725}
Jan Beulicheca37c12006-01-11 22:42:17 +0100726
727asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
728{
729 static const char str[] = "double fault";
730 struct task_struct *tsk = current;
731
732 /* Return not checked because double check cannot be ignored */
733 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
734
735 tsk->thread.error_code = error_code;
736 tsk->thread.trap_no = 8;
737
738 /* This is always a kernel trap and never fixable (and thus must
739 never return). */
740 for (;;)
741 die(str, regs, error_code);
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700744asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
745 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100747 struct task_struct *tsk = current;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 conditional_sti(regs);
750
Jan Beulich6e3f3612006-01-11 22:42:14 +0100751 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200752 tsk->thread.error_code = error_code;
753 tsk->thread.trap_no = 13;
754
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200755 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
Andi Kleen03252912008-01-30 13:33:18 +0100756 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100758 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100760 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100761 print_vma_addr(" in ", regs->ip);
762 printk("\n");
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 force_sig(SIGSEGV, tsk);
766 return;
767 }
768
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100769 if (fixup_exception(regs))
770 return;
Andi Kleend1895182007-05-02 19:27:05 +0200771
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100772 tsk->thread.error_code = error_code;
773 tsk->thread.trap_no = 13;
774 if (notify_die(DIE_GPF, "general protection fault", regs,
775 error_code, 13, SIGSEGV) == NOTIFY_STOP)
776 return;
777 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200780static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100781mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200783 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
784 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100785 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200786
Dave Jiangc0d12172007-07-19 01:49:46 -0700787#if defined(CONFIG_EDAC)
788 if(edac_handler_set()) {
789 edac_atomic_assert_error();
790 return;
791 }
792#endif
793
Don Zickus8da5add2006-09-26 10:52:27 +0200794 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200795 panic("NMI: Not continuing");
796
797 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* Clear and disable the memory parity error line. */
800 reason = (reason & 0xf) | 4;
801 outb(reason, 0x61);
802}
803
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200804static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100805io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 printk("NMI: IOCK error (debug interrupt?)\n");
808 show_registers(regs);
809
810 /* Re-enable the IOCK line, wait for a few seconds */
811 reason = (reason & 0xf) | 8;
812 outb(reason, 0x61);
813 mdelay(2000);
814 reason &= ~8;
815 outb(reason, 0x61);
816}
817
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200818static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100819unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200820{
Jason Wesseld3597522008-02-15 14:55:53 -0600821 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
822 return;
Don Zickusc41c5cd2006-09-26 10:52:27 +0200823 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
824 reason);
825 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200826
827 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200828 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200829
Don Zickusc41c5cd2006-09-26 10:52:27 +0200830 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700833/* Runs on IST stack. This code must keep interrupts off all the time.
834 Nested NMIs are prevented by the CPU. */
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200835asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700838 int cpu;
839
840 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700843 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 reason = get_nmi_reason();
845
846 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100847 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 == NOTIFY_STOP)
849 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /*
851 * Ok, so this is none of the documented NMI sources,
852 * so it must be the NMI watchdog.
853 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200854 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200856 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200857 unknown_nmi_error(reason, regs);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return;
860 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100861 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return;
863
864 /* AK: following checks seem to be broken on modern chipsets. FIXME */
865
866 if (reason & 0x80)
867 mem_parity_error(reason, regs);
868 if (reason & 0x40)
869 io_check_error(reason, regs);
870}
871
Cyrill Gorcunovc6425b92008-05-24 19:36:33 +0400872asmlinkage notrace __kprobes void
873do_nmi(struct pt_regs *regs, long error_code)
874{
875 nmi_enter();
876 add_pda(__nmi_count, 1);
877 if (!ignore_nmis)
878 default_do_nmi(regs);
879 nmi_exit();
880}
881
882void stop_nmi(void)
883{
884 acpi_nmi_disable();
885 ignore_nmis++;
886}
887
888void restart_nmi(void)
889{
890 ignore_nmis--;
891 acpi_nmi_enable();
892}
893
Jan Beulichb556b352006-01-11 22:43:00 +0100894/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700895asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200897 trace_hardirqs_fixup();
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
900 return;
901 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200902 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200904 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700907/* Help handler running on IST stack to switch back to user stack
908 for scheduling or signal handling. The actual stack switch is done in
909 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100910asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700912 struct pt_regs *regs = eregs;
913 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100914 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700915 ;
916 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700917 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800918 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700919 /* Exception from kernel and interrupts are enabled. Move to
920 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100921 else if (eregs->flags & X86_EFLAGS_IF)
922 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700923 if (eregs != regs)
924 *regs = *eregs;
925 return regs;
926}
927
928/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700929asmlinkage void __kprobes do_debug(struct pt_regs * regs,
930 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700931{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 unsigned long condition;
933 struct task_struct *tsk = current;
934 siginfo_t info;
935
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100936 trace_hardirqs_fixup();
937
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700938 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Roland McGrath10faa812008-01-30 13:30:54 +0100940 /*
941 * The processor cleared BTF, so don't mark that we need it set.
942 */
943 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
944 tsk->thread.debugctlmsr = 0;
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700947 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700948 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700949
John Blackwooda65d17c2006-02-12 14:34:58 -0800950 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* Mask out spurious debug traps due to lazy DR7 setting */
953 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
954 if (!tsk->thread.debugreg7) {
955 goto clear_dr7;
956 }
957 }
958
959 tsk->thread.debugreg6 = condition;
960
Roland McGrathe1f28772008-01-30 13:30:50 +0100961
962 /*
963 * Single-stepping through TF: make sure we ignore any events in
964 * kernel space (but re-enable TF when returning to user mode).
965 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700966 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700967 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
971 /* Ok, finally something we can handle */
972 tsk->thread.trap_no = 1;
973 tsk->thread.error_code = error_code;
974 info.si_signo = SIGTRAP;
975 info.si_errno = 0;
976 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100977 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100978 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700981 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800982 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700983 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985clear_TF_reenable:
986 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100987 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800988 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Jan Beulich6e3f3612006-01-11 22:42:14 +0100991static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100993 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100995
Jan Beulich6e3f3612006-01-11 22:42:14 +0100996 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700997 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100998 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 0;
1001}
1002
1003/*
1004 * Note that we play around with the 'TS' bit in an attempt to get
1005 * the correct behaviour even in the presence of the asynchronous
1006 * IRQ13 behaviour
1007 */
1008asmlinkage void do_coprocessor_error(struct pt_regs *regs)
1009{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001010 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct task_struct * task;
1012 siginfo_t info;
1013 unsigned short cwd, swd;
1014
1015 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001016 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001017 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return;
1019
1020 /*
1021 * Save the info for the exception handler and clear the error.
1022 */
1023 task = current;
1024 save_init_fpu(task);
1025 task->thread.trap_no = 16;
1026 task->thread.error_code = 0;
1027 info.si_signo = SIGFPE;
1028 info.si_errno = 0;
1029 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001030 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1033 * status. 0x3f is the exception bits in these regs, 0x200 is the
1034 * C1 reg you need in case of a stack fault, 0x040 is the stack
1035 * fault bit. We should only be taking one exception at a time,
1036 * so if this combination doesn't produce any single exception,
1037 * then we have a bad program that isn't synchronizing its FPU usage
1038 * and it will suffer the consequences since we won't be able to
1039 * fully reproduce the context of the exception
1040 */
1041 cwd = get_fpu_cwd(task);
1042 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +02001043 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 case 0x000:
1045 default:
1046 break;
1047 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +02001048 /*
1049 * swd & 0x240 == 0x040: Stack Underflow
1050 * swd & 0x240 == 0x240: Stack Overflow
1051 * User must clear the SF bit (0x40) if set
1052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 info.si_code = FPE_FLTINV;
1054 break;
1055 case 0x002: /* Denormalize */
1056 case 0x010: /* Underflow */
1057 info.si_code = FPE_FLTUND;
1058 break;
1059 case 0x004: /* Zero Divide */
1060 info.si_code = FPE_FLTDIV;
1061 break;
1062 case 0x008: /* Overflow */
1063 info.si_code = FPE_FLTOVF;
1064 break;
1065 case 0x020: /* Precision */
1066 info.si_code = FPE_FLTRES;
1067 break;
1068 }
1069 force_sig_info(SIGFPE, &info, task);
1070}
1071
1072asmlinkage void bad_intr(void)
1073{
1074 printk("bad interrupt");
1075}
1076
1077asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1078{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001079 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct task_struct * task;
1081 siginfo_t info;
1082 unsigned short mxcsr;
1083
1084 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001085 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001086 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return;
1088
1089 /*
1090 * Save the info for the exception handler and clear the error.
1091 */
1092 task = current;
1093 save_init_fpu(task);
1094 task->thread.trap_no = 19;
1095 task->thread.error_code = 0;
1096 info.si_signo = SIGFPE;
1097 info.si_errno = 0;
1098 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001099 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * The SIMD FPU exceptions are handled a little differently, as there
1102 * is only a single status/control register. Thus, to determine which
1103 * unmasked exception was caught we must mask the exception mask bits
1104 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1105 */
1106 mxcsr = get_fpu_mxcsr(task);
1107 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1108 case 0x000:
1109 default:
1110 break;
1111 case 0x001: /* Invalid Op */
1112 info.si_code = FPE_FLTINV;
1113 break;
1114 case 0x002: /* Denormalize */
1115 case 0x010: /* Underflow */
1116 info.si_code = FPE_FLTUND;
1117 break;
1118 case 0x004: /* Zero Divide */
1119 info.si_code = FPE_FLTDIV;
1120 break;
1121 case 0x008: /* Overflow */
1122 info.si_code = FPE_FLTOVF;
1123 break;
1124 case 0x020: /* Precision */
1125 info.si_code = FPE_FLTRES;
1126 break;
1127 }
1128 force_sig_info(SIGFPE, &info, task);
1129}
1130
1131asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1132{
1133}
1134
1135asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1136{
1137}
1138
Jacob Shin89b831e2005-11-05 17:25:53 +01001139asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1140{
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * 'math_state_restore()' saves the current math information in the
1145 * old math state array, and gets the new ones from the current task
1146 *
1147 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1148 * Don't touch unless you *really* know how it works.
1149 */
1150asmlinkage void math_state_restore(void)
1151{
1152 struct task_struct *me = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Suresh Siddhaaa283f42008-03-10 15:28:05 -07001154 if (!used_math()) {
1155 local_irq_enable();
1156 /*
1157 * does a slab alloc which can sleep
1158 */
1159 if (init_fpu(me)) {
1160 /*
1161 * ran out of memory!
1162 */
1163 do_group_exit(SIGKILL);
1164 return;
1165 }
1166 local_irq_disable();
1167 }
1168
1169 clts(); /* Allow maths ops (or we recurse) */
Suresh Siddha61c46282008-03-10 15:28:04 -07001170 restore_fpu_checking(&me->thread.xstate->fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001171 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001172 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001174EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176void __init trap_init(void)
1177{
1178 set_intr_gate(0,&divide_error);
1179 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1180 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001181 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001182 set_system_gate(4,&overflow); /* int4 can be called from all */
1183 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 set_intr_gate(6,&invalid_op);
1185 set_intr_gate(7,&device_not_available);
1186 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1187 set_intr_gate(9,&coprocessor_segment_overrun);
1188 set_intr_gate(10,&invalid_TSS);
1189 set_intr_gate(11,&segment_not_present);
1190 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1191 set_intr_gate(13,&general_protection);
1192 set_intr_gate(14,&page_fault);
1193 set_intr_gate(15,&spurious_interrupt_bug);
1194 set_intr_gate(16,&coprocessor_error);
1195 set_intr_gate(17,&alignment_check);
1196#ifdef CONFIG_X86_MCE
1197 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1198#endif
1199 set_intr_gate(19,&simd_coprocessor_error);
1200
1201#ifdef CONFIG_IA32_EMULATION
1202 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1203#endif
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /*
Suresh Siddha61c46282008-03-10 15:28:04 -07001206 * initialize the per thread extended state:
1207 */
1208 init_thread_xstate();
1209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * Should be a barrier for any external CPU state.
1211 */
1212 cpu_init();
1213}
1214
1215
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001216static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001218 if (!s)
1219 return -EINVAL;
1220 if (!strcmp(s, "panic"))
1221 panic_on_oops = 1;
1222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001224early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226static int __init kstack_setup(char *s)
1227{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001228 if (!s)
1229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001233early_param("kstack", kstack_setup);
Arjan van de Vena25bd942008-01-30 13:33:08 +01001234
1235
1236static int __init code_bytes_setup(char *s)
1237{
1238 code_bytes = simple_strtoul(s, NULL, 0);
1239 if (code_bytes > 8192)
1240 code_bytes = 8192;
1241
1242 return 1;
1243}
1244__setup("code_bytes=", code_bytes_setup);