blob: adff76ea97c4732de4b7766272c8b6ad26ace478 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'entry.S'.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/ptrace.h>
18#include <linux/timer.h>
19#include <linux/mm.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
Randy Dunlap4b0ff1a2006-10-05 19:07:26 +020024#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070027#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070028#include <linux/kprobes.h>
Vivek Goyal8bcc5282006-04-18 12:35:13 +020029#include <linux/kexec.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020030#include <linux/unwind.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010031#include <linux/uaccess.h>
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -080032#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070033#include <linux/kdebug.h>
Arjan van de Ven57c351d2007-11-26 20:42:19 +010034#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Glauber de Oliveira Costae32ede12008-03-19 14:25:35 -030036#include <mach_traps.h>
37
Dave Jiangc0d12172007-07-19 01:49:46 -070038#if defined(CONFIG_EDAC)
39#include <linux/edac.h>
40#endif
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
44#include <asm/atomic.h>
45#include <asm/debugreg.h>
46#include <asm/desc.h>
47#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020049#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/smp.h>
51#include <asm/pgalloc.h>
52#include <asm/pda.h>
53#include <asm/proto.h>
54#include <asm/nmi.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020055#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057asmlinkage void divide_error(void);
58asmlinkage void debug(void);
59asmlinkage void nmi(void);
60asmlinkage void int3(void);
61asmlinkage void overflow(void);
62asmlinkage void bounds(void);
63asmlinkage void invalid_op(void);
64asmlinkage void device_not_available(void);
65asmlinkage void double_fault(void);
66asmlinkage void coprocessor_segment_overrun(void);
67asmlinkage void invalid_TSS(void);
68asmlinkage void segment_not_present(void);
69asmlinkage void stack_segment(void);
70asmlinkage void general_protection(void);
71asmlinkage void page_fault(void);
72asmlinkage void coprocessor_error(void);
73asmlinkage void simd_coprocessor_error(void);
74asmlinkage void reserved(void);
75asmlinkage void alignment_check(void);
76asmlinkage void machine_check(void);
77asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Arjan van de Vena25bd942008-01-30 13:33:08 +010079static unsigned int code_bytes = 64;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static inline void conditional_sti(struct pt_regs *regs)
82{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010083 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 local_irq_enable();
85}
86
John Blackwooda65d17c2006-02-12 14:34:58 -080087static inline void preempt_conditional_sti(struct pt_regs *regs)
88{
Ingo Molnare8bff742008-02-13 20:21:06 +010089 inc_preempt_count();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010090 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080091 local_irq_enable();
92}
93
94static inline void preempt_conditional_cli(struct pt_regs *regs)
95{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010096 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080097 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +020098 /* Make sure to not schedule here because we could be running
99 on an exception stack. */
Ingo Molnare8bff742008-02-13 20:21:06 +0100100 dec_preempt_count();
John Blackwooda65d17c2006-02-12 14:34:58 -0800101}
102
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100103int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Arjan van de Venbc850d62008-01-30 13:33:07 +0100105void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700106{
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100107#ifdef CONFIG_KALLSYMS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned long offset = 0, symsize;
109 const char *symname;
110 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700111 char *delim = ":";
Cyrill Gorcunov85e2aee2008-01-30 13:33:33 +0100112 char namebuf[KSYM_NAME_LEN];
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100113 char reliab[4] = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ingo Molnar3ac94932006-07-03 00:24:36 -0700115 symname = kallsyms_lookup(address, &symsize, &offset,
116 &modname, namebuf);
117 if (!symname) {
118 printk(" [<%016lx>]\n", address);
119 return;
120 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100121 if (!reliable)
122 strcpy(reliab, "? ");
123
Ingo Molnar3ac94932006-07-03 00:24:36 -0700124 if (!modname)
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100125 modname = delim = "";
Arjan van de Venbc850d62008-01-30 13:33:07 +0100126 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
127 address, reliab, delim, modname, delim, symname, offset, symsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700129 printk(" [<%016lx>]\n", address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Andi Kleen0a658002005-04-16 15:25:17 -0700133static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200134 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700135{
Jan Beulichb556b352006-01-11 22:43:00 +0100136 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700137 [DEBUG_STACK - 1] = "#DB",
138 [NMI_STACK - 1] = "NMI",
139 [DOUBLEFAULT_STACK - 1] = "#DF",
140 [STACKFAULT_STACK - 1] = "#SS",
141 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100142#if DEBUG_STKSZ > EXCEPTION_STKSZ
143 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
144#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700145 };
146 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700148 /*
149 * Iterate over all exception stacks, and figure out whether
150 * 'stack' is in one of them:
151 */
Andi Kleen0a658002005-04-16 15:25:17 -0700152 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200153 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700154 /*
155 * Is 'stack' above this exception frame's end?
156 * If yes then skip to the next frame.
157 */
Andi Kleen0a658002005-04-16 15:25:17 -0700158 if (stack >= end)
159 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700160 /*
161 * Is 'stack' above this exception frame's start address?
162 * If yes then we found the right frame.
163 */
Andi Kleen0a658002005-04-16 15:25:17 -0700164 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700165 /*
166 * Make sure we only iterate through an exception
167 * stack once. If it comes up for the second time
168 * then there's something wrong going on - just
169 * break out and return NULL:
170 */
Andi Kleen0a658002005-04-16 15:25:17 -0700171 if (*usedp & (1U << k))
172 break;
173 *usedp |= 1U << k;
174 *idp = ids[k];
175 return (unsigned long *)end;
176 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700177 /*
178 * If this is a debug stack, and if it has a larger size than
179 * the usual exception stacks, then 'stack' might still
180 * be within the lower portion of the debug stack:
181 */
Jan Beulichb556b352006-01-11 22:43:00 +0100182#if DEBUG_STKSZ > EXCEPTION_STKSZ
183 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
184 unsigned j = N_EXCEPTION_STACKS - 1;
185
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700186 /*
187 * Black magic. A large debug stack is composed of
188 * multiple exception stack entries, which we
189 * iterate through now. Dont look:
190 */
Jan Beulichb556b352006-01-11 22:43:00 +0100191 do {
192 ++j;
193 end -= EXCEPTION_STKSZ;
194 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
195 } while (stack < end - EXCEPTION_STKSZ);
196 if (*usedp & (1U << j))
197 break;
198 *usedp |= 1U << j;
199 *idp = ids[j];
200 return (unsigned long *)end;
201 }
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Andi Kleenb615ebd2006-12-07 02:14:00 +0100207#define MSG(txt) ops->warning(data, txt)
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Simon Arlott676b1852007-10-20 01:25:36 +0200210 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * process stack
212 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700213 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215
Arjan van de Vene4a94562008-01-30 13:33:07 +0100216static inline int valid_stack_ptr(struct thread_info *tinfo,
217 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100218{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100219 void *t = tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100220 if (end) {
221 if (p < end && p >= (end-THREAD_SIZE))
222 return 1;
223 else
224 return 0;
225 }
226 return p > t && p < t + THREAD_SIZE - size;
227}
228
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100229/* The form of the top of the frame on the stack */
230struct stack_frame {
231 struct stack_frame *next_frame;
232 unsigned long return_address;
233};
234
235
Arjan van de Vene4a94562008-01-30 13:33:07 +0100236static inline unsigned long print_context_stack(struct thread_info *tinfo,
237 unsigned long *stack, unsigned long bp,
238 const struct stacktrace_ops *ops, void *data,
239 unsigned long *end)
240{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100241 struct stack_frame *frame = (struct stack_frame *)bp;
242
243 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
244 unsigned long addr;
245
246 addr = *stack;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100247 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100248 if ((unsigned long) stack == bp + 8) {
249 ops->address(data, addr, 1);
250 frame = frame->next_frame;
251 bp = (unsigned long) frame;
252 } else {
253 ops->address(data, addr, bp == 0);
254 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100255 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100256 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100257 }
258 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100259}
260
Andi Kleenb615ebd2006-12-07 02:14:00 +0100261void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100262 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200263 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Andrew Mortonda689332006-12-07 02:14:02 +0100265 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100266 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700267 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100268 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Beulichb538ed22006-06-26 13:57:32 +0200270 if (!tsk)
271 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100272 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200273
Andi Kleenc0b766f2006-09-26 10:52:34 +0200274 if (!stack) {
275 unsigned long dummy;
276 stack = &dummy;
277 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100278 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200279 }
280
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100281#ifdef CONFIG_FRAME_POINTER
282 if (!bp) {
283 if (tsk == current) {
284 /* Grab bp right from our regs */
285 asm("movq %%rbp, %0" : "=r" (bp):);
286 } else {
287 /* bp is the last reg pushed by switch_to */
288 bp = *(unsigned long *) tsk->thread.sp;
289 }
290 }
291#endif
292
293
Andi Kleen0a658002005-04-16 15:25:17 -0700294
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700295 /*
296 * Print function call entries in all stacks, starting at the
297 * current stack address. If the stacks consist of nested
298 * exceptions
299 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200300 for (;;) {
301 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700302 unsigned long *estack_end;
303 estack_end = in_exception_stack(cpu, (unsigned long)stack,
304 &used, &id);
305
306 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200307 if (ops->stack(data, id) < 0)
308 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100309
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100310 bp = print_context_stack(tinfo, stack, bp, ops,
311 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200312 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700313 /*
314 * We link to the next stack via the
315 * second-to-last pointer (index -2 to end) in the
316 * exception stack:
317 */
Andi Kleen0a658002005-04-16 15:25:17 -0700318 stack = (unsigned long *) estack_end[-2];
319 continue;
320 }
321 if (irqstack_end) {
322 unsigned long *irqstack;
323 irqstack = irqstack_end -
324 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
325
326 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200327 if (ops->stack(data, "IRQ") < 0)
328 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100329 bp = print_context_stack(tinfo, stack, bp,
330 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700331 /*
332 * We link to the next stack (which would be
333 * the process stack normally) the last
334 * pointer (index -1 to end) in the IRQ stack:
335 */
Andi Kleen0a658002005-04-16 15:25:17 -0700336 stack = (unsigned long *) (irqstack_end[-1]);
337 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200338 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700339 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
Andi Kleen0a658002005-04-16 15:25:17 -0700342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Andi Kleen0a658002005-04-16 15:25:17 -0700344
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700345 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200346 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700347 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100348 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100349 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200350}
351EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700352
Andi Kleenc0b766f2006-09-26 10:52:34 +0200353static void
354print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
355{
356 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 printk("\n");
358}
359
Andi Kleenc0b766f2006-09-26 10:52:34 +0200360static void print_trace_warning(void *data, char *msg)
361{
362 printk("%s\n", msg);
363}
364
365static int print_trace_stack(void *data, char *name)
366{
367 printk(" <%s> ", name);
368 return 0;
369}
370
Arjan van de Venbc850d62008-01-30 13:33:07 +0100371static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200372{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700373 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100374 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200375}
376
Jan Beulich9689ba82007-10-17 18:04:37 +0200377static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200378 .warning = print_trace_warning,
379 .warning_symbol = print_trace_warning_symbol,
380 .stack = print_trace_stack,
381 .address = print_trace_address,
382};
383
384void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100385show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
386 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200387{
388 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100389 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200390 printk("\n");
391}
392
393static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100394_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
395 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 unsigned long *stack;
398 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200399 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100400 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
401 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 // debugging aid: "show_stack(NULL, NULL);" prints the
404 // back trace for this cpu.
405
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100406 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100408 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100410 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100413 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 for(i=0; i < kstack_depth_to_print; i++) {
415 if (stack >= irqstack && stack <= irqstack_end) {
416 if (stack == irqstack_end) {
417 stack = (unsigned long *) (irqstack_end[-1]);
418 printk(" <EOI> ");
419 }
420 } else {
421 if (((long) stack & (THREAD_SIZE-1)) == 0)
422 break;
423 }
424 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700425 printk("\n");
426 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700427 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100429 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200430}
431
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100432void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200433{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100434 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/*
438 * The architecture-independent dump_stack generator
439 */
440void dump_stack(void)
441{
442 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100443 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100444
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100445#ifdef CONFIG_FRAME_POINTER
446 if (!bp)
447 asm("movq %%rbp, %0" : "=r" (bp):);
448#endif
449
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100450 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
451 current->pid, current->comm, print_tainted(),
452 init_utsname()->release,
453 (int)strcspn(init_utsname()->version, " "),
454 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100455 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458EXPORT_SYMBOL(dump_stack);
459
460void show_registers(struct pt_regs *regs)
461{
462 int i;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100463 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200464 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100465 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100466 u8 *ip;
467 unsigned int code_prologue = code_bytes * 43 / 64;
468 unsigned int code_len = code_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100470 sp = regs->sp;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100471 ip = (u8 *) regs->ip - code_prologue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 printk("CPU %d ", cpu);
473 __show_regs(regs);
474 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800475 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * When in-kernel, we also print out the stack and code at the
479 * time of the fault..
480 */
Arjan van de Vena25bd942008-01-30 13:33:08 +0100481 if (!user_mode(regs)) {
482 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100484 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Arjan van de Vena25bd942008-01-30 13:33:08 +0100485 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Arjan van de Vena25bd942008-01-30 13:33:08 +0100487 printk(KERN_EMERG "Code: ");
488 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
489 /* try starting at RIP */
490 ip = (u8 *) regs->ip;
491 code_len = code_len - code_prologue + 1;
492 }
493 for (i = 0; i < code_len; i++, ip++) {
494 if (ip < (u8 *)PAGE_OFFSET ||
495 probe_kernel_address(ip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 printk(" Bad RIP value.");
497 break;
498 }
Arjan van de Vena25bd942008-01-30 13:33:08 +0100499 if (ip == (u8 *)regs->ip)
500 printk("<%02x> ", c);
501 else
502 printk("%02x ", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 printk("\n");
506}
507
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100508int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800509{
510 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100512 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800513 return 0;
514
515 return ud2 == 0x0b0f;
516}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Andi Kleen39743c92007-10-19 20:35:03 +0200518static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200520static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100522unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700524 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200525 unsigned long flags;
526
Andrew Mortonabf0f102006-09-26 10:52:36 +0200527 oops_enter();
528
Jan Beulich12091402005-09-12 18:49:24 +0200529 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200530 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700531 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200532 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (cpu == die_owner)
534 /* nested oops. should stop eventually */;
535 else
Andi Kleen39743c92007-10-19 20:35:03 +0200536 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200538 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200539 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200541 bust_spinlocks(1);
542 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Jan Beulich22f59912008-01-30 13:31:23 +0100545void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200548 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200549 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200550 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200551 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200552 __raw_spin_unlock(&die_lock);
553 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100554 if (!regs) {
555 oops_exit();
556 return;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700559 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200560 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100561 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jan Beulich22f59912008-01-30 13:31:23 +0100564int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 static int die_counter;
567 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
568#ifdef CONFIG_PREEMPT
569 printk("PREEMPT ");
570#endif
571#ifdef CONFIG_SMP
572 printk("SMP ");
573#endif
574#ifdef CONFIG_DEBUG_PAGEALLOC
575 printk("DEBUG_PAGEALLOC");
576#endif
577 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100578 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
579 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700581 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Executive summary in case the oops scrolled away */
583 printk(KERN_ALERT "RIP ");
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100584 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100585 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200586 if (kexec_should_crash(current))
587 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591void die(const char * str, struct pt_regs * regs, long err)
592{
Jan Beulich12091402005-09-12 18:49:24 +0200593 unsigned long flags = oops_begin();
594
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800595 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100596 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800597
Jan Beulich22f59912008-01-30 13:31:23 +0100598 if (__die(str, regs, err))
599 regs = NULL;
600 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200603notrace __kprobes void
604die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jason Wessel737a4602008-03-07 16:34:16 -0600606 unsigned long flags;
Jan Beulich12091402005-09-12 18:49:24 +0200607
Jason Wessel737a4602008-03-07 16:34:16 -0600608 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
609 NOTIFY_STOP)
610 return;
611
612 flags = oops_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /*
614 * We are in trouble anyway, lets at least try
615 * to get a message out.
616 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200617 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200619 if (kexec_should_crash(current))
620 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200621 if (do_panic || panic_on_oops)
622 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100623 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200624 nmi_exit();
625 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100626 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700629static void __kprobes do_trap(int trapnr, int signr, char *str,
630 struct pt_regs * regs, long error_code,
631 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100633 struct task_struct *tsk = current;
634
Jan Beulich6e3f3612006-01-11 22:42:14 +0100635 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200636 /*
637 * We want error_code and trap_no set for userspace
638 * faults and kernelspace faults which result in
639 * die(), but not kernelspace faults which are fixed
640 * up. die() gives the process no chance to handle
641 * the signal and notice the kernel fault information,
642 * so that won't result in polluting the information
643 * about previously queued, but not yet delivered,
644 * faults. See also do_general_protection below.
645 */
646 tsk->thread.error_code = error_code;
647 tsk->thread.trap_no = trapnr;
648
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200649 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
Andi Kleen03252912008-01-30 13:33:18 +0100650 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100652 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100654 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100655 print_vma_addr(" in ", regs->ip);
656 printk("\n");
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (info)
660 force_sig_info(signr, info, tsk);
661 else
662 force_sig(signr, tsk);
663 return;
664 }
665
666
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100667 if (!fixup_exception(regs)) {
668 tsk->thread.error_code = error_code;
669 tsk->thread.trap_no = trapnr;
670 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100672 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675#define DO_ERROR(trapnr, signr, str, name) \
676asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
677{ \
678 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
679 == NOTIFY_STOP) \
680 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200681 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 do_trap(trapnr, signr, str, regs, error_code, NULL); \
683}
684
685#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
686asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
687{ \
688 siginfo_t info; \
689 info.si_signo = signr; \
690 info.si_errno = 0; \
691 info.si_code = sicode; \
692 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100693 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
695 == NOTIFY_STOP) \
696 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200697 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 do_trap(trapnr, signr, str, regs, error_code, &info); \
699}
700
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100701DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702DO_ERROR( 4, SIGSEGV, "overflow", overflow)
703DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100704DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
706DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
707DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
708DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
709DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
710DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200711
712/* Runs on IST stack */
713asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
714{
715 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
716 12, SIGBUS) == NOTIFY_STOP)
717 return;
718 preempt_conditional_sti(regs);
719 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
720 preempt_conditional_cli(regs);
721}
Jan Beulicheca37c12006-01-11 22:42:17 +0100722
723asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
724{
725 static const char str[] = "double fault";
726 struct task_struct *tsk = current;
727
728 /* Return not checked because double check cannot be ignored */
729 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
730
731 tsk->thread.error_code = error_code;
732 tsk->thread.trap_no = 8;
733
734 /* This is always a kernel trap and never fixable (and thus must
735 never return). */
736 for (;;)
737 die(str, regs, error_code);
738}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700740asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
741 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100743 struct task_struct *tsk = current;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 conditional_sti(regs);
746
Jan Beulich6e3f3612006-01-11 22:42:14 +0100747 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200748 tsk->thread.error_code = error_code;
749 tsk->thread.trap_no = 13;
750
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200751 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
Andi Kleen03252912008-01-30 13:33:18 +0100752 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100754 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100756 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100757 print_vma_addr(" in ", regs->ip);
758 printk("\n");
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 force_sig(SIGSEGV, tsk);
762 return;
763 }
764
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100765 if (fixup_exception(regs))
766 return;
Andi Kleend1895182007-05-02 19:27:05 +0200767
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100768 tsk->thread.error_code = error_code;
769 tsk->thread.trap_no = 13;
770 if (notify_die(DIE_GPF, "general protection fault", regs,
771 error_code, 13, SIGSEGV) == NOTIFY_STOP)
772 return;
773 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200776static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100777mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200779 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
780 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100781 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200782
Dave Jiangc0d12172007-07-19 01:49:46 -0700783#if defined(CONFIG_EDAC)
784 if(edac_handler_set()) {
785 edac_atomic_assert_error();
786 return;
787 }
788#endif
789
Don Zickus8da5add2006-09-26 10:52:27 +0200790 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200791 panic("NMI: Not continuing");
792
793 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* Clear and disable the memory parity error line. */
796 reason = (reason & 0xf) | 4;
797 outb(reason, 0x61);
798}
799
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200800static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100801io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 printk("NMI: IOCK error (debug interrupt?)\n");
804 show_registers(regs);
805
806 /* Re-enable the IOCK line, wait for a few seconds */
807 reason = (reason & 0xf) | 8;
808 outb(reason, 0x61);
809 mdelay(2000);
810 reason &= ~8;
811 outb(reason, 0x61);
812}
813
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200814static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100815unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200816{
Jason Wesseld3597522008-02-15 14:55:53 -0600817 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
818 return;
Don Zickusc41c5cd2006-09-26 10:52:27 +0200819 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
820 reason);
821 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200822
823 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200824 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200825
Don Zickusc41c5cd2006-09-26 10:52:27 +0200826 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700829/* Runs on IST stack. This code must keep interrupts off all the time.
830 Nested NMIs are prevented by the CPU. */
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200831asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700834 int cpu;
835
836 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700839 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 reason = get_nmi_reason();
841
842 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100843 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 == NOTIFY_STOP)
845 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
847 * Ok, so this is none of the documented NMI sources,
848 * so it must be the NMI watchdog.
849 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200850 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200852 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200853 unknown_nmi_error(reason, regs);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return;
856 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100857 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return;
859
860 /* AK: following checks seem to be broken on modern chipsets. FIXME */
861
862 if (reason & 0x80)
863 mem_parity_error(reason, regs);
864 if (reason & 0x40)
865 io_check_error(reason, regs);
866}
867
Jan Beulichb556b352006-01-11 22:43:00 +0100868/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700869asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200871 trace_hardirqs_fixup();
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
874 return;
875 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200876 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200878 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700881/* Help handler running on IST stack to switch back to user stack
882 for scheduling or signal handling. The actual stack switch is done in
883 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100884asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700886 struct pt_regs *regs = eregs;
887 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100888 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700889 ;
890 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700891 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800892 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700893 /* Exception from kernel and interrupts are enabled. Move to
894 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100895 else if (eregs->flags & X86_EFLAGS_IF)
896 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700897 if (eregs != regs)
898 *regs = *eregs;
899 return regs;
900}
901
902/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700903asmlinkage void __kprobes do_debug(struct pt_regs * regs,
904 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700905{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 unsigned long condition;
907 struct task_struct *tsk = current;
908 siginfo_t info;
909
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100910 trace_hardirqs_fixup();
911
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700912 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Roland McGrath10faa812008-01-30 13:30:54 +0100914 /*
915 * The processor cleared BTF, so don't mark that we need it set.
916 */
917 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
918 tsk->thread.debugctlmsr = 0;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700921 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700922 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700923
John Blackwooda65d17c2006-02-12 14:34:58 -0800924 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* Mask out spurious debug traps due to lazy DR7 setting */
927 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
928 if (!tsk->thread.debugreg7) {
929 goto clear_dr7;
930 }
931 }
932
933 tsk->thread.debugreg6 = condition;
934
Roland McGrathe1f28772008-01-30 13:30:50 +0100935
936 /*
937 * Single-stepping through TF: make sure we ignore any events in
938 * kernel space (but re-enable TF when returning to user mode).
939 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700940 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700941 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
945 /* Ok, finally something we can handle */
946 tsk->thread.trap_no = 1;
947 tsk->thread.error_code = error_code;
948 info.si_signo = SIGTRAP;
949 info.si_errno = 0;
950 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100951 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100952 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700955 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800956 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959clear_TF_reenable:
960 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100961 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800962 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Jan Beulich6e3f3612006-01-11 22:42:14 +0100965static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100967 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100969
Jan Beulich6e3f3612006-01-11 22:42:14 +0100970 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700971 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100972 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
976
977/*
978 * Note that we play around with the 'TS' bit in an attempt to get
979 * the correct behaviour even in the presence of the asynchronous
980 * IRQ13 behaviour
981 */
982asmlinkage void do_coprocessor_error(struct pt_regs *regs)
983{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100984 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct task_struct * task;
986 siginfo_t info;
987 unsigned short cwd, swd;
988
989 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700990 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100991 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return;
993
994 /*
995 * Save the info for the exception handler and clear the error.
996 */
997 task = current;
998 save_init_fpu(task);
999 task->thread.trap_no = 16;
1000 task->thread.error_code = 0;
1001 info.si_signo = SIGFPE;
1002 info.si_errno = 0;
1003 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001004 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1007 * status. 0x3f is the exception bits in these regs, 0x200 is the
1008 * C1 reg you need in case of a stack fault, 0x040 is the stack
1009 * fault bit. We should only be taking one exception at a time,
1010 * so if this combination doesn't produce any single exception,
1011 * then we have a bad program that isn't synchronizing its FPU usage
1012 * and it will suffer the consequences since we won't be able to
1013 * fully reproduce the context of the exception
1014 */
1015 cwd = get_fpu_cwd(task);
1016 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +02001017 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 case 0x000:
1019 default:
1020 break;
1021 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +02001022 /*
1023 * swd & 0x240 == 0x040: Stack Underflow
1024 * swd & 0x240 == 0x240: Stack Overflow
1025 * User must clear the SF bit (0x40) if set
1026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 info.si_code = FPE_FLTINV;
1028 break;
1029 case 0x002: /* Denormalize */
1030 case 0x010: /* Underflow */
1031 info.si_code = FPE_FLTUND;
1032 break;
1033 case 0x004: /* Zero Divide */
1034 info.si_code = FPE_FLTDIV;
1035 break;
1036 case 0x008: /* Overflow */
1037 info.si_code = FPE_FLTOVF;
1038 break;
1039 case 0x020: /* Precision */
1040 info.si_code = FPE_FLTRES;
1041 break;
1042 }
1043 force_sig_info(SIGFPE, &info, task);
1044}
1045
1046asmlinkage void bad_intr(void)
1047{
1048 printk("bad interrupt");
1049}
1050
1051asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1052{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001053 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct task_struct * task;
1055 siginfo_t info;
1056 unsigned short mxcsr;
1057
1058 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001059 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001060 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return;
1062
1063 /*
1064 * Save the info for the exception handler and clear the error.
1065 */
1066 task = current;
1067 save_init_fpu(task);
1068 task->thread.trap_no = 19;
1069 task->thread.error_code = 0;
1070 info.si_signo = SIGFPE;
1071 info.si_errno = 0;
1072 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001073 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * The SIMD FPU exceptions are handled a little differently, as there
1076 * is only a single status/control register. Thus, to determine which
1077 * unmasked exception was caught we must mask the exception mask bits
1078 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1079 */
1080 mxcsr = get_fpu_mxcsr(task);
1081 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1082 case 0x000:
1083 default:
1084 break;
1085 case 0x001: /* Invalid Op */
1086 info.si_code = FPE_FLTINV;
1087 break;
1088 case 0x002: /* Denormalize */
1089 case 0x010: /* Underflow */
1090 info.si_code = FPE_FLTUND;
1091 break;
1092 case 0x004: /* Zero Divide */
1093 info.si_code = FPE_FLTDIV;
1094 break;
1095 case 0x008: /* Overflow */
1096 info.si_code = FPE_FLTOVF;
1097 break;
1098 case 0x020: /* Precision */
1099 info.si_code = FPE_FLTRES;
1100 break;
1101 }
1102 force_sig_info(SIGFPE, &info, task);
1103}
1104
1105asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1106{
1107}
1108
1109asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1110{
1111}
1112
Jacob Shin89b831e2005-11-05 17:25:53 +01001113asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1114{
1115}
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117/*
1118 * 'math_state_restore()' saves the current math information in the
1119 * old math state array, and gets the new ones from the current task
1120 *
1121 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1122 * Don't touch unless you *really* know how it works.
1123 */
1124asmlinkage void math_state_restore(void)
1125{
1126 struct task_struct *me = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Suresh Siddhaaa283f42008-03-10 15:28:05 -07001128 if (!used_math()) {
1129 local_irq_enable();
1130 /*
1131 * does a slab alloc which can sleep
1132 */
1133 if (init_fpu(me)) {
1134 /*
1135 * ran out of memory!
1136 */
1137 do_group_exit(SIGKILL);
1138 return;
1139 }
1140 local_irq_disable();
1141 }
1142
1143 clts(); /* Allow maths ops (or we recurse) */
Suresh Siddha61c46282008-03-10 15:28:04 -07001144 restore_fpu_checking(&me->thread.xstate->fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001145 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001146 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001148EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150void __init trap_init(void)
1151{
1152 set_intr_gate(0,&divide_error);
1153 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1154 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001155 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001156 set_system_gate(4,&overflow); /* int4 can be called from all */
1157 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 set_intr_gate(6,&invalid_op);
1159 set_intr_gate(7,&device_not_available);
1160 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1161 set_intr_gate(9,&coprocessor_segment_overrun);
1162 set_intr_gate(10,&invalid_TSS);
1163 set_intr_gate(11,&segment_not_present);
1164 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1165 set_intr_gate(13,&general_protection);
1166 set_intr_gate(14,&page_fault);
1167 set_intr_gate(15,&spurious_interrupt_bug);
1168 set_intr_gate(16,&coprocessor_error);
1169 set_intr_gate(17,&alignment_check);
1170#ifdef CONFIG_X86_MCE
1171 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1172#endif
1173 set_intr_gate(19,&simd_coprocessor_error);
1174
1175#ifdef CONFIG_IA32_EMULATION
1176 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1177#endif
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /*
Suresh Siddha61c46282008-03-10 15:28:04 -07001180 * initialize the per thread extended state:
1181 */
1182 init_thread_xstate();
1183 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * Should be a barrier for any external CPU state.
1185 */
1186 cpu_init();
1187}
1188
1189
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001190static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001192 if (!s)
1193 return -EINVAL;
1194 if (!strcmp(s, "panic"))
1195 panic_on_oops = 1;
1196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001198early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200static int __init kstack_setup(char *s)
1201{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001202 if (!s)
1203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001207early_param("kstack", kstack_setup);
Arjan van de Vena25bd942008-01-30 13:33:08 +01001208
1209
1210static int __init code_bytes_setup(char *s)
1211{
1212 code_bytes = simple_strtoul(s, NULL, 0);
1213 if (code_bytes > 8192)
1214 code_bytes = 8192;
1215
1216 return 1;
1217}
1218__setup("code_bytes=", code_bytes_setup);