blob: 304ca6b4a1caeceae1b42100b1675867983f11a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'entry.S'.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/ptrace.h>
18#include <linux/timer.h>
19#include <linux/mm.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
Randy Dunlap4b0ff1a2006-10-05 19:07:26 +020024#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070027#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070028#include <linux/kprobes.h>
Vivek Goyal8bcc5282006-04-18 12:35:13 +020029#include <linux/kexec.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020030#include <linux/unwind.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010031#include <linux/uaccess.h>
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -080032#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070033#include <linux/kdebug.h>
Arjan van de Ven57c351d2007-11-26 20:42:19 +010034#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Dave Jiangc0d12172007-07-19 01:49:46 -070036#if defined(CONFIG_EDAC)
37#include <linux/edac.h>
38#endif
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/atomic.h>
43#include <asm/debugreg.h>
44#include <asm/desc.h>
45#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020047#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/smp.h>
49#include <asm/pgalloc.h>
50#include <asm/pda.h>
51#include <asm/proto.h>
52#include <asm/nmi.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020053#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055asmlinkage void divide_error(void);
56asmlinkage void debug(void);
57asmlinkage void nmi(void);
58asmlinkage void int3(void);
59asmlinkage void overflow(void);
60asmlinkage void bounds(void);
61asmlinkage void invalid_op(void);
62asmlinkage void device_not_available(void);
63asmlinkage void double_fault(void);
64asmlinkage void coprocessor_segment_overrun(void);
65asmlinkage void invalid_TSS(void);
66asmlinkage void segment_not_present(void);
67asmlinkage void stack_segment(void);
68asmlinkage void general_protection(void);
69asmlinkage void page_fault(void);
70asmlinkage void coprocessor_error(void);
71asmlinkage void simd_coprocessor_error(void);
72asmlinkage void reserved(void);
73asmlinkage void alignment_check(void);
74asmlinkage void machine_check(void);
75asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static inline void conditional_sti(struct pt_regs *regs)
78{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010079 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 local_irq_enable();
81}
82
John Blackwooda65d17c2006-02-12 14:34:58 -080083static inline void preempt_conditional_sti(struct pt_regs *regs)
84{
85 preempt_disable();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010086 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080087 local_irq_enable();
88}
89
90static inline void preempt_conditional_cli(struct pt_regs *regs)
91{
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_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +020094 /* Make sure to not schedule here because we could be running
95 on an exception stack. */
John Blackwooda65d17c2006-02-12 14:34:58 -080096 preempt_enable_no_resched();
97}
98
Chuck Ebbert0741f4d2006-12-07 02:14:11 +010099int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#ifdef CONFIG_KALLSYMS
Arjan van de Venbc850d62008-01-30 13:33:07 +0100102void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long offset = 0, symsize;
105 const char *symname;
106 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700107 char *delim = ":";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 char namebuf[128];
Arjan van de Venbc850d62008-01-30 13:33:07 +0100109 char reliab[4] = "";;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ingo Molnar3ac94932006-07-03 00:24:36 -0700111 symname = kallsyms_lookup(address, &symsize, &offset,
112 &modname, namebuf);
113 if (!symname) {
114 printk(" [<%016lx>]\n", address);
115 return;
116 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100117 if (!reliable)
118 strcpy(reliab, "? ");
119
Ingo Molnar3ac94932006-07-03 00:24:36 -0700120 if (!modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 modname = delim = "";
Arjan van de Venbc850d62008-01-30 13:33:07 +0100122 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
123 address, reliab, delim, modname, delim, symname, offset, symsize);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#else
Arjan van de Venbc850d62008-01-30 13:33:07 +0100126void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700127{
128 printk(" [<%016lx>]\n", address);
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
131
Andi Kleen0a658002005-04-16 15:25:17 -0700132static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200133 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700134{
Jan Beulichb556b352006-01-11 22:43:00 +0100135 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700136 [DEBUG_STACK - 1] = "#DB",
137 [NMI_STACK - 1] = "NMI",
138 [DOUBLEFAULT_STACK - 1] = "#DF",
139 [STACKFAULT_STACK - 1] = "#SS",
140 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100141#if DEBUG_STKSZ > EXCEPTION_STKSZ
142 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
143#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700144 };
145 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700147 /*
148 * Iterate over all exception stacks, and figure out whether
149 * 'stack' is in one of them:
150 */
Andi Kleen0a658002005-04-16 15:25:17 -0700151 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200152 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700153 /*
154 * Is 'stack' above this exception frame's end?
155 * If yes then skip to the next frame.
156 */
Andi Kleen0a658002005-04-16 15:25:17 -0700157 if (stack >= end)
158 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700159 /*
160 * Is 'stack' above this exception frame's start address?
161 * If yes then we found the right frame.
162 */
Andi Kleen0a658002005-04-16 15:25:17 -0700163 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700164 /*
165 * Make sure we only iterate through an exception
166 * stack once. If it comes up for the second time
167 * then there's something wrong going on - just
168 * break out and return NULL:
169 */
Andi Kleen0a658002005-04-16 15:25:17 -0700170 if (*usedp & (1U << k))
171 break;
172 *usedp |= 1U << k;
173 *idp = ids[k];
174 return (unsigned long *)end;
175 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700176 /*
177 * If this is a debug stack, and if it has a larger size than
178 * the usual exception stacks, then 'stack' might still
179 * be within the lower portion of the debug stack:
180 */
Jan Beulichb556b352006-01-11 22:43:00 +0100181#if DEBUG_STKSZ > EXCEPTION_STKSZ
182 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
183 unsigned j = N_EXCEPTION_STACKS - 1;
184
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700185 /*
186 * Black magic. A large debug stack is composed of
187 * multiple exception stack entries, which we
188 * iterate through now. Dont look:
189 */
Jan Beulichb556b352006-01-11 22:43:00 +0100190 do {
191 ++j;
192 end -= EXCEPTION_STKSZ;
193 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
194 } while (stack < end - EXCEPTION_STKSZ);
195 if (*usedp & (1U << j))
196 break;
197 *usedp |= 1U << j;
198 *idp = ids[j];
199 return (unsigned long *)end;
200 }
201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Andi Kleenb615ebd2006-12-07 02:14:00 +0100206#define MSG(txt) ops->warning(data, txt)
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
Simon Arlott676b1852007-10-20 01:25:36 +0200209 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * process stack
211 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700212 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
214
Arjan van de Vene4a94562008-01-30 13:33:07 +0100215static inline int valid_stack_ptr(struct thread_info *tinfo,
216 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100217{
218 void *t = (void *)tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100219 if (end) {
220 if (p < end && p >= (end-THREAD_SIZE))
221 return 1;
222 else
223 return 0;
224 }
225 return p > t && p < t + THREAD_SIZE - size;
226}
227
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100228/* The form of the top of the frame on the stack */
229struct stack_frame {
230 struct stack_frame *next_frame;
231 unsigned long return_address;
232};
233
234
Arjan van de Vene4a94562008-01-30 13:33:07 +0100235static inline unsigned long print_context_stack(struct thread_info *tinfo,
236 unsigned long *stack, unsigned long bp,
237 const struct stacktrace_ops *ops, void *data,
238 unsigned long *end)
239{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100240 struct stack_frame *frame = (struct stack_frame *)bp;
241
242 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
243 unsigned long addr;
244
245 addr = *stack;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100246 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100247 if ((unsigned long) stack == bp + 8) {
248 ops->address(data, addr, 1);
249 frame = frame->next_frame;
250 bp = (unsigned long) frame;
251 } else {
252 ops->address(data, addr, bp == 0);
253 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100254 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100255 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100256 }
257 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100258}
259
Andi Kleenb615ebd2006-12-07 02:14:00 +0100260void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100261 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200262 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Andrew Mortonda689332006-12-07 02:14:02 +0100264 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100265 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700266 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100267 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jan Beulichb538ed22006-06-26 13:57:32 +0200269 if (!tsk)
270 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100271 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200272
Andi Kleenc0b766f2006-09-26 10:52:34 +0200273 if (!stack) {
274 unsigned long dummy;
275 stack = &dummy;
276 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100277 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200278 }
279
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100280#ifdef CONFIG_FRAME_POINTER
281 if (!bp) {
282 if (tsk == current) {
283 /* Grab bp right from our regs */
284 asm("movq %%rbp, %0" : "=r" (bp):);
285 } else {
286 /* bp is the last reg pushed by switch_to */
287 bp = *(unsigned long *) tsk->thread.sp;
288 }
289 }
290#endif
291
292
Andi Kleen0a658002005-04-16 15:25:17 -0700293
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700294 /*
295 * Print function call entries in all stacks, starting at the
296 * current stack address. If the stacks consist of nested
297 * exceptions
298 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200299 for (;;) {
300 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700301 unsigned long *estack_end;
302 estack_end = in_exception_stack(cpu, (unsigned long)stack,
303 &used, &id);
304
305 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200306 if (ops->stack(data, id) < 0)
307 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100308
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100309 bp = print_context_stack(tinfo, stack, bp, ops,
310 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200311 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700312 /*
313 * We link to the next stack via the
314 * second-to-last pointer (index -2 to end) in the
315 * exception stack:
316 */
Andi Kleen0a658002005-04-16 15:25:17 -0700317 stack = (unsigned long *) estack_end[-2];
318 continue;
319 }
320 if (irqstack_end) {
321 unsigned long *irqstack;
322 irqstack = irqstack_end -
323 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
324
325 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200326 if (ops->stack(data, "IRQ") < 0)
327 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100328 bp = print_context_stack(tinfo, stack, bp,
329 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700330 /*
331 * We link to the next stack (which would be
332 * the process stack normally) the last
333 * pointer (index -1 to end) in the IRQ stack:
334 */
Andi Kleen0a658002005-04-16 15:25:17 -0700335 stack = (unsigned long *) (irqstack_end[-1]);
336 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200337 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700338 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
Andi Kleen0a658002005-04-16 15:25:17 -0700341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Andi Kleen0a658002005-04-16 15:25:17 -0700343
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700344 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200345 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700346 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100347 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100348 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200349}
350EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700351
Andi Kleenc0b766f2006-09-26 10:52:34 +0200352static void
353print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
354{
355 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk("\n");
357}
358
Andi Kleenc0b766f2006-09-26 10:52:34 +0200359static void print_trace_warning(void *data, char *msg)
360{
361 printk("%s\n", msg);
362}
363
364static int print_trace_stack(void *data, char *name)
365{
366 printk(" <%s> ", name);
367 return 0;
368}
369
Arjan van de Venbc850d62008-01-30 13:33:07 +0100370static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200371{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700372 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100373 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200374}
375
Jan Beulich9689ba82007-10-17 18:04:37 +0200376static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200377 .warning = print_trace_warning,
378 .warning_symbol = print_trace_warning_symbol,
379 .stack = print_trace_stack,
380 .address = print_trace_address,
381};
382
383void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100384show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
385 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200386{
387 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100388 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200389 printk("\n");
390}
391
392static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100393_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
394 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 unsigned long *stack;
397 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200398 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100399 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
400 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 // debugging aid: "show_stack(NULL, NULL);" prints the
403 // back trace for this cpu.
404
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100405 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100407 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100409 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 for(i=0; i < kstack_depth_to_print; i++) {
414 if (stack >= irqstack && stack <= irqstack_end) {
415 if (stack == irqstack_end) {
416 stack = (unsigned long *) (irqstack_end[-1]);
417 printk(" <EOI> ");
418 }
419 } else {
420 if (((long) stack & (THREAD_SIZE-1)) == 0)
421 break;
422 }
423 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700424 printk("\n");
425 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700426 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100428 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200429}
430
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100431void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200432{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100433 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * The architecture-independent dump_stack generator
438 */
439void dump_stack(void)
440{
441 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100442 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100443
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100444#ifdef CONFIG_FRAME_POINTER
445 if (!bp)
446 asm("movq %%rbp, %0" : "=r" (bp):);
447#endif
448
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100449 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
450 current->pid, current->comm, print_tainted(),
451 init_utsname()->release,
452 (int)strcspn(init_utsname()->version, " "),
453 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100454 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457EXPORT_SYMBOL(dump_stack);
458
459void show_registers(struct pt_regs *regs)
460{
461 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700462 int in_kernel = !user_mode(regs);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100467 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 printk("CPU %d ", cpu);
469 __show_regs(regs);
470 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800471 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * When in-kernel, we also print out the stack and code at the
475 * time of the fault..
476 */
477 if (in_kernel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100479 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 printk("\nCode: ");
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100482 if (regs->ip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto bad;
484
Roberto Nibali2b692a82006-03-25 16:29:55 +0100485 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 unsigned char c;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100487 if (__get_user(c, &((unsigned char*)regs->ip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488bad:
489 printk(" Bad RIP value.");
490 break;
491 }
492 printk("%02x ", c);
493 }
494 }
495 printk("\n");
496}
497
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100498int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800499{
500 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100502 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800503 return 0;
504
505 return ud2 == 0x0b0f;
506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Andi Kleen39743c92007-10-19 20:35:03 +0200508static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200510static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100512unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700514 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200515 unsigned long flags;
516
Andrew Mortonabf0f102006-09-26 10:52:36 +0200517 oops_enter();
518
Jan Beulich12091402005-09-12 18:49:24 +0200519 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200520 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700521 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200522 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (cpu == die_owner)
524 /* nested oops. should stop eventually */;
525 else
Andi Kleen39743c92007-10-19 20:35:03 +0200526 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200528 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200529 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200531 bust_spinlocks(1);
532 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Jan Beulich22f59912008-01-30 13:31:23 +0100535void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200538 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200539 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200540 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200541 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200542 __raw_spin_unlock(&die_lock);
543 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100544 if (!regs) {
545 oops_exit();
546 return;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700549 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200550 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100551 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Jan Beulich22f59912008-01-30 13:31:23 +0100554int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 static int die_counter;
557 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
558#ifdef CONFIG_PREEMPT
559 printk("PREEMPT ");
560#endif
561#ifdef CONFIG_SMP
562 printk("SMP ");
563#endif
564#ifdef CONFIG_DEBUG_PAGEALLOC
565 printk("DEBUG_PAGEALLOC");
566#endif
567 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100568 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
569 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700571 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Executive summary in case the oops scrolled away */
573 printk(KERN_ALERT "RIP ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100574 printk_address(regs->ip, regs->bp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100575 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200576 if (kexec_should_crash(current))
577 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581void die(const char * str, struct pt_regs * regs, long err)
582{
Jan Beulich12091402005-09-12 18:49:24 +0200583 unsigned long flags = oops_begin();
584
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800585 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100586 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800587
Jan Beulich22f59912008-01-30 13:31:23 +0100588 if (__die(str, regs, err))
589 regs = NULL;
590 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Andi Kleenfac58552006-09-26 10:52:27 +0200593void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jan Beulich12091402005-09-12 18:49:24 +0200595 unsigned long flags = oops_begin();
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * We are in trouble anyway, lets at least try
599 * to get a message out.
600 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200601 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200603 if (kexec_should_crash(current))
604 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200605 if (do_panic || panic_on_oops)
606 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100607 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200608 nmi_exit();
609 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100610 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700613static void __kprobes do_trap(int trapnr, int signr, char *str,
614 struct pt_regs * regs, long error_code,
615 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100617 struct task_struct *tsk = current;
618
Jan Beulich6e3f3612006-01-11 22:42:14 +0100619 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200620 /*
621 * We want error_code and trap_no set for userspace
622 * faults and kernelspace faults which result in
623 * die(), but not kernelspace faults which are fixed
624 * up. die() gives the process no chance to handle
625 * the signal and notice the kernel fault information,
626 * so that won't result in polluting the information
627 * about previously queued, but not yet delivered,
628 * faults. See also do_general_protection below.
629 */
630 tsk->thread.error_code = error_code;
631 tsk->thread.trap_no = trapnr;
632
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200633 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
634 printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 printk(KERN_INFO
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100636 "%s[%d] trap %s ip:%lx sp:%lx error:%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100638 regs->ip, regs->sp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (info)
641 force_sig_info(signr, info, tsk);
642 else
643 force_sig(signr, tsk);
644 return;
645 }
646
647
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100648 if (!fixup_exception(regs)) {
649 tsk->thread.error_code = error_code;
650 tsk->thread.trap_no = trapnr;
651 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100653 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656#define DO_ERROR(trapnr, signr, str, name) \
657asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
658{ \
659 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
660 == NOTIFY_STOP) \
661 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200662 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 do_trap(trapnr, signr, str, regs, error_code, NULL); \
664}
665
666#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
667asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
668{ \
669 siginfo_t info; \
670 info.si_signo = signr; \
671 info.si_errno = 0; \
672 info.si_code = sicode; \
673 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100674 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
676 == NOTIFY_STOP) \
677 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200678 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 do_trap(trapnr, signr, str, regs, error_code, &info); \
680}
681
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100682DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683DO_ERROR( 4, SIGSEGV, "overflow", overflow)
684DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100685DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
687DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
688DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
689DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
690DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
691DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200692
693/* Runs on IST stack */
694asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
695{
696 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
697 12, SIGBUS) == NOTIFY_STOP)
698 return;
699 preempt_conditional_sti(regs);
700 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
701 preempt_conditional_cli(regs);
702}
Jan Beulicheca37c12006-01-11 22:42:17 +0100703
704asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
705{
706 static const char str[] = "double fault";
707 struct task_struct *tsk = current;
708
709 /* Return not checked because double check cannot be ignored */
710 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
711
712 tsk->thread.error_code = error_code;
713 tsk->thread.trap_no = 8;
714
715 /* This is always a kernel trap and never fixable (and thus must
716 never return). */
717 for (;;)
718 die(str, regs, error_code);
719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700721asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
722 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100724 struct task_struct *tsk = current;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 conditional_sti(regs);
727
Jan Beulich6e3f3612006-01-11 22:42:14 +0100728 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200729 tsk->thread.error_code = error_code;
730 tsk->thread.trap_no = 13;
731
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200732 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
733 printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 printk(KERN_INFO
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100735 "%s[%d] general protection ip:%lx sp:%lx error:%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100737 regs->ip, regs->sp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 force_sig(SIGSEGV, tsk);
740 return;
741 }
742
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100743 if (fixup_exception(regs))
744 return;
Andi Kleend1895182007-05-02 19:27:05 +0200745
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100746 tsk->thread.error_code = error_code;
747 tsk->thread.trap_no = 13;
748 if (notify_die(DIE_GPF, "general protection fault", regs,
749 error_code, 13, SIGSEGV) == NOTIFY_STOP)
750 return;
751 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100754static __kprobes void
755mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200757 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
758 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100759 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200760
Dave Jiangc0d12172007-07-19 01:49:46 -0700761#if defined(CONFIG_EDAC)
762 if(edac_handler_set()) {
763 edac_atomic_assert_error();
764 return;
765 }
766#endif
767
Don Zickus8da5add2006-09-26 10:52:27 +0200768 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200769 panic("NMI: Not continuing");
770
771 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* Clear and disable the memory parity error line. */
774 reason = (reason & 0xf) | 4;
775 outb(reason, 0x61);
776}
777
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100778static __kprobes void
779io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 printk("NMI: IOCK error (debug interrupt?)\n");
782 show_registers(regs);
783
784 /* Re-enable the IOCK line, wait for a few seconds */
785 reason = (reason & 0xf) | 8;
786 outb(reason, 0x61);
787 mdelay(2000);
788 reason &= ~8;
789 outb(reason, 0x61);
790}
791
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100792static __kprobes void
793unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200794{
795 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
796 reason);
797 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200798
799 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200800 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200801
Don Zickusc41c5cd2006-09-26 10:52:27 +0200802 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700805/* Runs on IST stack. This code must keep interrupts off all the time.
806 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100807asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700810 int cpu;
811
812 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700815 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 reason = get_nmi_reason();
817
818 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100819 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 == NOTIFY_STOP)
821 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * Ok, so this is none of the documented NMI sources,
824 * so it must be the NMI watchdog.
825 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200826 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200828 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200829 unknown_nmi_error(reason, regs);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return;
832 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100833 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return;
835
836 /* AK: following checks seem to be broken on modern chipsets. FIXME */
837
838 if (reason & 0x80)
839 mem_parity_error(reason, regs);
840 if (reason & 0x40)
841 io_check_error(reason, regs);
842}
843
Jan Beulichb556b352006-01-11 22:43:00 +0100844/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700845asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200847 trace_hardirqs_fixup();
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
850 return;
851 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200852 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200854 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700857/* Help handler running on IST stack to switch back to user stack
858 for scheduling or signal handling. The actual stack switch is done in
859 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100860asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700862 struct pt_regs *regs = eregs;
863 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100864 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700865 ;
866 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700867 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800868 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700869 /* Exception from kernel and interrupts are enabled. Move to
870 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100871 else if (eregs->flags & X86_EFLAGS_IF)
872 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700873 if (eregs != regs)
874 *regs = *eregs;
875 return regs;
876}
877
878/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700879asmlinkage void __kprobes do_debug(struct pt_regs * regs,
880 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unsigned long condition;
883 struct task_struct *tsk = current;
884 siginfo_t info;
885
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100886 trace_hardirqs_fixup();
887
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700888 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Roland McGrath10faa812008-01-30 13:30:54 +0100890 /*
891 * The processor cleared BTF, so don't mark that we need it set.
892 */
893 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
894 tsk->thread.debugctlmsr = 0;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700897 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700898 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700899
John Blackwooda65d17c2006-02-12 14:34:58 -0800900 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* Mask out spurious debug traps due to lazy DR7 setting */
903 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
904 if (!tsk->thread.debugreg7) {
905 goto clear_dr7;
906 }
907 }
908
909 tsk->thread.debugreg6 = condition;
910
Roland McGrathe1f28772008-01-30 13:30:50 +0100911
912 /*
913 * Single-stepping through TF: make sure we ignore any events in
914 * kernel space (but re-enable TF when returning to user mode).
915 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700916 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700917 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 /* Ok, finally something we can handle */
922 tsk->thread.trap_no = 1;
923 tsk->thread.error_code = error_code;
924 info.si_signo = SIGTRAP;
925 info.si_errno = 0;
926 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100927 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100928 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700931 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800932 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700933 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935clear_TF_reenable:
936 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100937 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800938 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Jan Beulich6e3f3612006-01-11 22:42:14 +0100941static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100943 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100945
Jan Beulich6e3f3612006-01-11 22:42:14 +0100946 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700947 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100948 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return 0;
951}
952
953/*
954 * Note that we play around with the 'TS' bit in an attempt to get
955 * the correct behaviour even in the presence of the asynchronous
956 * IRQ13 behaviour
957 */
958asmlinkage void do_coprocessor_error(struct pt_regs *regs)
959{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100960 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct task_struct * task;
962 siginfo_t info;
963 unsigned short cwd, swd;
964
965 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700966 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100967 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return;
969
970 /*
971 * Save the info for the exception handler and clear the error.
972 */
973 task = current;
974 save_init_fpu(task);
975 task->thread.trap_no = 16;
976 task->thread.error_code = 0;
977 info.si_signo = SIGFPE;
978 info.si_errno = 0;
979 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100980 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /*
982 * (~cwd & swd) will mask out exceptions that are not set to unmasked
983 * status. 0x3f is the exception bits in these regs, 0x200 is the
984 * C1 reg you need in case of a stack fault, 0x040 is the stack
985 * fault bit. We should only be taking one exception at a time,
986 * so if this combination doesn't produce any single exception,
987 * then we have a bad program that isn't synchronizing its FPU usage
988 * and it will suffer the consequences since we won't be able to
989 * fully reproduce the context of the exception
990 */
991 cwd = get_fpu_cwd(task);
992 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200993 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 case 0x000:
995 default:
996 break;
997 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200998 /*
999 * swd & 0x240 == 0x040: Stack Underflow
1000 * swd & 0x240 == 0x240: Stack Overflow
1001 * User must clear the SF bit (0x40) if set
1002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 info.si_code = FPE_FLTINV;
1004 break;
1005 case 0x002: /* Denormalize */
1006 case 0x010: /* Underflow */
1007 info.si_code = FPE_FLTUND;
1008 break;
1009 case 0x004: /* Zero Divide */
1010 info.si_code = FPE_FLTDIV;
1011 break;
1012 case 0x008: /* Overflow */
1013 info.si_code = FPE_FLTOVF;
1014 break;
1015 case 0x020: /* Precision */
1016 info.si_code = FPE_FLTRES;
1017 break;
1018 }
1019 force_sig_info(SIGFPE, &info, task);
1020}
1021
1022asmlinkage void bad_intr(void)
1023{
1024 printk("bad interrupt");
1025}
1026
1027asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1028{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001029 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct task_struct * task;
1031 siginfo_t info;
1032 unsigned short mxcsr;
1033
1034 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001035 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001036 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return;
1038
1039 /*
1040 * Save the info for the exception handler and clear the error.
1041 */
1042 task = current;
1043 save_init_fpu(task);
1044 task->thread.trap_no = 19;
1045 task->thread.error_code = 0;
1046 info.si_signo = SIGFPE;
1047 info.si_errno = 0;
1048 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001049 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * The SIMD FPU exceptions are handled a little differently, as there
1052 * is only a single status/control register. Thus, to determine which
1053 * unmasked exception was caught we must mask the exception mask bits
1054 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1055 */
1056 mxcsr = get_fpu_mxcsr(task);
1057 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1058 case 0x000:
1059 default:
1060 break;
1061 case 0x001: /* Invalid Op */
1062 info.si_code = FPE_FLTINV;
1063 break;
1064 case 0x002: /* Denormalize */
1065 case 0x010: /* Underflow */
1066 info.si_code = FPE_FLTUND;
1067 break;
1068 case 0x004: /* Zero Divide */
1069 info.si_code = FPE_FLTDIV;
1070 break;
1071 case 0x008: /* Overflow */
1072 info.si_code = FPE_FLTOVF;
1073 break;
1074 case 0x020: /* Precision */
1075 info.si_code = FPE_FLTRES;
1076 break;
1077 }
1078 force_sig_info(SIGFPE, &info, task);
1079}
1080
1081asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1082{
1083}
1084
1085asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1086{
1087}
1088
Jacob Shin89b831e2005-11-05 17:25:53 +01001089asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1090{
1091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093/*
1094 * 'math_state_restore()' saves the current math information in the
1095 * old math state array, and gets the new ones from the current task
1096 *
1097 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1098 * Don't touch unless you *really* know how it works.
1099 */
1100asmlinkage void math_state_restore(void)
1101{
1102 struct task_struct *me = current;
1103 clts(); /* Allow maths ops (or we recurse) */
1104
1105 if (!used_math())
1106 init_fpu(me);
1107 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001108 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001109 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001111EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113void __init trap_init(void)
1114{
1115 set_intr_gate(0,&divide_error);
1116 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1117 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001118 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001119 set_system_gate(4,&overflow); /* int4 can be called from all */
1120 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 set_intr_gate(6,&invalid_op);
1122 set_intr_gate(7,&device_not_available);
1123 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1124 set_intr_gate(9,&coprocessor_segment_overrun);
1125 set_intr_gate(10,&invalid_TSS);
1126 set_intr_gate(11,&segment_not_present);
1127 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1128 set_intr_gate(13,&general_protection);
1129 set_intr_gate(14,&page_fault);
1130 set_intr_gate(15,&spurious_interrupt_bug);
1131 set_intr_gate(16,&coprocessor_error);
1132 set_intr_gate(17,&alignment_check);
1133#ifdef CONFIG_X86_MCE
1134 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1135#endif
1136 set_intr_gate(19,&simd_coprocessor_error);
1137
1138#ifdef CONFIG_IA32_EMULATION
1139 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1140#endif
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * Should be a barrier for any external CPU state.
1144 */
1145 cpu_init();
1146}
1147
1148
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001149static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001151 if (!s)
1152 return -EINVAL;
1153 if (!strcmp(s, "panic"))
1154 panic_on_oops = 1;
1155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001157early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159static int __init kstack_setup(char *s)
1160{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001161 if (!s)
1162 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001166early_param("kstack", kstack_setup);