blob: 62c4d8f46ee9db3770216890e6d09b6f2512fb65 [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
Andi Kleenc547c772006-11-28 20:12:59 +0100215static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
216{
217 void *t = (void *)tinfo;
218 return p > t && p < t + THREAD_SIZE - 3;
219}
220
Andi Kleenb615ebd2006-12-07 02:14:00 +0100221void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100222 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200223 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Andrew Mortonda689332006-12-07 02:14:02 +0100225 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100226 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700227 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100228 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jan Beulichb538ed22006-06-26 13:57:32 +0200230 if (!tsk)
231 tsk = current;
232
Andi Kleenc0b766f2006-09-26 10:52:34 +0200233 if (!stack) {
234 unsigned long dummy;
235 stack = &dummy;
236 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100237 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200238 }
239
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700240 /*
241 * Print function call entries within a stack. 'cond' is the
242 * "end of stackframe" condition, that the 'stack++'
243 * iteration will eventually trigger.
244 */
Andi Kleen0a658002005-04-16 15:25:17 -0700245#define HANDLE_STACK(cond) \
246 do while (cond) { \
Jan Beulich1b2f6302006-01-11 22:46:45 +0100247 unsigned long addr = *stack++; \
Andi Kleen446f7132006-12-07 02:14:12 +0100248 /* Use unlocked access here because except for NMIs \
249 we should be already protected against module unloads */ \
250 if (__kernel_text_address(addr)) { \
Andi Kleen0a658002005-04-16 15:25:17 -0700251 /* \
252 * If the address is either in the text segment of the \
253 * kernel, or in the region which contains vmalloc'ed \
254 * memory, it *may* be the address of a calling \
255 * routine; if so, print it so that someone tracing \
256 * down the cause of the crash will be able to figure \
257 * out the call path that was taken. \
258 */ \
Arjan van de Venbc850d62008-01-30 13:33:07 +0100259 ops->address(data, addr, 1); \
Andi Kleen0a658002005-04-16 15:25:17 -0700260 } \
261 } while (0)
262
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700263 /*
264 * Print function call entries in all stacks, starting at the
265 * current stack address. If the stacks consist of nested
266 * exceptions
267 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200268 for (;;) {
269 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700270 unsigned long *estack_end;
271 estack_end = in_exception_stack(cpu, (unsigned long)stack,
272 &used, &id);
273
274 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200275 if (ops->stack(data, id) < 0)
276 break;
Andi Kleen0a658002005-04-16 15:25:17 -0700277 HANDLE_STACK (stack < estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200278 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700279 /*
280 * We link to the next stack via the
281 * second-to-last pointer (index -2 to end) in the
282 * exception stack:
283 */
Andi Kleen0a658002005-04-16 15:25:17 -0700284 stack = (unsigned long *) estack_end[-2];
285 continue;
286 }
287 if (irqstack_end) {
288 unsigned long *irqstack;
289 irqstack = irqstack_end -
290 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
291
292 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200293 if (ops->stack(data, "IRQ") < 0)
294 break;
Andi Kleen0a658002005-04-16 15:25:17 -0700295 HANDLE_STACK (stack < irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700296 /*
297 * We link to the next stack (which would be
298 * the process stack normally) the last
299 * pointer (index -1 to end) in the IRQ stack:
300 */
Andi Kleen0a658002005-04-16 15:25:17 -0700301 stack = (unsigned long *) (irqstack_end[-1]);
302 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200303 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700304 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 }
Andi Kleen0a658002005-04-16 15:25:17 -0700307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Andi Kleen0a658002005-04-16 15:25:17 -0700309
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700310 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200311 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700312 */
OGAWA Hirofumi7523c4d2007-01-04 01:21:28 +0900313 tinfo = task_thread_info(tsk);
Andi Kleenc547c772006-11-28 20:12:59 +0100314 HANDLE_STACK (valid_stack_ptr(tinfo, stack));
Andi Kleen0a658002005-04-16 15:25:17 -0700315#undef HANDLE_STACK
Andrew Mortonda689332006-12-07 02:14:02 +0100316 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200317}
318EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700319
Andi Kleenc0b766f2006-09-26 10:52:34 +0200320static void
321print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
322{
323 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 printk("\n");
325}
326
Andi Kleenc0b766f2006-09-26 10:52:34 +0200327static void print_trace_warning(void *data, char *msg)
328{
329 printk("%s\n", msg);
330}
331
332static int print_trace_stack(void *data, char *name)
333{
334 printk(" <%s> ", name);
335 return 0;
336}
337
Arjan van de Venbc850d62008-01-30 13:33:07 +0100338static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200339{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700340 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100341 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200342}
343
Jan Beulich9689ba82007-10-17 18:04:37 +0200344static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200345 .warning = print_trace_warning,
346 .warning_symbol = print_trace_warning_symbol,
347 .stack = print_trace_stack,
348 .address = print_trace_address,
349};
350
351void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100352show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
353 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200354{
355 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100356 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200357 printk("\n");
358}
359
360static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100361_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
362 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 unsigned long *stack;
365 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200366 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100367 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
368 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 // debugging aid: "show_stack(NULL, NULL);" prints the
371 // back trace for this cpu.
372
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100373 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100375 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100377 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100380 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 for(i=0; i < kstack_depth_to_print; i++) {
382 if (stack >= irqstack && stack <= irqstack_end) {
383 if (stack == irqstack_end) {
384 stack = (unsigned long *) (irqstack_end[-1]);
385 printk(" <EOI> ");
386 }
387 } else {
388 if (((long) stack & (THREAD_SIZE-1)) == 0)
389 break;
390 }
391 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700392 printk("\n");
393 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700394 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100396 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200397}
398
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100399void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200400{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100401 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * The architecture-independent dump_stack generator
406 */
407void dump_stack(void)
408{
409 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100410 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100411
412 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
413 current->pid, current->comm, print_tainted(),
414 init_utsname()->release,
415 (int)strcspn(init_utsname()->version, " "),
416 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100417 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420EXPORT_SYMBOL(dump_stack);
421
422void show_registers(struct pt_regs *regs)
423{
424 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700425 int in_kernel = !user_mode(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100426 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200427 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100428 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100430 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 printk("CPU %d ", cpu);
432 __show_regs(regs);
433 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800434 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * When in-kernel, we also print out the stack and code at the
438 * time of the fault..
439 */
440 if (in_kernel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100442 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 printk("\nCode: ");
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100445 if (regs->ip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto bad;
447
Roberto Nibali2b692a82006-03-25 16:29:55 +0100448 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 unsigned char c;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100450 if (__get_user(c, &((unsigned char*)regs->ip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451bad:
452 printk(" Bad RIP value.");
453 break;
454 }
455 printk("%02x ", c);
456 }
457 }
458 printk("\n");
459}
460
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100461int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800462{
463 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100465 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800466 return 0;
467
468 return ud2 == 0x0b0f;
469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Andi Kleen39743c92007-10-19 20:35:03 +0200471static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200473static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100475unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700477 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200478 unsigned long flags;
479
Andrew Mortonabf0f102006-09-26 10:52:36 +0200480 oops_enter();
481
Jan Beulich12091402005-09-12 18:49:24 +0200482 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200483 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700484 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200485 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (cpu == die_owner)
487 /* nested oops. should stop eventually */;
488 else
Andi Kleen39743c92007-10-19 20:35:03 +0200489 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200491 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200492 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200494 bust_spinlocks(1);
495 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Jan Beulich22f59912008-01-30 13:31:23 +0100498void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200501 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200502 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200503 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200504 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200505 __raw_spin_unlock(&die_lock);
506 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100507 if (!regs) {
508 oops_exit();
509 return;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700512 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200513 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100514 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jan Beulich22f59912008-01-30 13:31:23 +0100517int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 static int die_counter;
520 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
521#ifdef CONFIG_PREEMPT
522 printk("PREEMPT ");
523#endif
524#ifdef CONFIG_SMP
525 printk("SMP ");
526#endif
527#ifdef CONFIG_DEBUG_PAGEALLOC
528 printk("DEBUG_PAGEALLOC");
529#endif
530 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100531 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
532 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700534 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Executive summary in case the oops scrolled away */
536 printk(KERN_ALERT "RIP ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100537 printk_address(regs->ip, regs->bp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100538 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200539 if (kexec_should_crash(current))
540 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544void die(const char * str, struct pt_regs * regs, long err)
545{
Jan Beulich12091402005-09-12 18:49:24 +0200546 unsigned long flags = oops_begin();
547
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800548 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100549 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800550
Jan Beulich22f59912008-01-30 13:31:23 +0100551 if (__die(str, regs, err))
552 regs = NULL;
553 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Andi Kleenfac58552006-09-26 10:52:27 +0200556void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Jan Beulich12091402005-09-12 18:49:24 +0200558 unsigned long flags = oops_begin();
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
561 * We are in trouble anyway, lets at least try
562 * to get a message out.
563 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200564 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200566 if (kexec_should_crash(current))
567 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200568 if (do_panic || panic_on_oops)
569 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100570 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200571 nmi_exit();
572 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100573 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700576static void __kprobes do_trap(int trapnr, int signr, char *str,
577 struct pt_regs * regs, long error_code,
578 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100580 struct task_struct *tsk = current;
581
Jan Beulich6e3f3612006-01-11 22:42:14 +0100582 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200583 /*
584 * We want error_code and trap_no set for userspace
585 * faults and kernelspace faults which result in
586 * die(), but not kernelspace faults which are fixed
587 * up. die() gives the process no chance to handle
588 * the signal and notice the kernel fault information,
589 * so that won't result in polluting the information
590 * about previously queued, but not yet delivered,
591 * faults. See also do_general_protection below.
592 */
593 tsk->thread.error_code = error_code;
594 tsk->thread.trap_no = trapnr;
595
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200596 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
597 printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 printk(KERN_INFO
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100599 "%s[%d] trap %s ip:%lx sp:%lx error:%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100601 regs->ip, regs->sp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (info)
604 force_sig_info(signr, info, tsk);
605 else
606 force_sig(signr, tsk);
607 return;
608 }
609
610
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100611 if (!fixup_exception(regs)) {
612 tsk->thread.error_code = error_code;
613 tsk->thread.trap_no = trapnr;
614 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619#define DO_ERROR(trapnr, signr, str, name) \
620asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
621{ \
622 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
623 == NOTIFY_STOP) \
624 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200625 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 do_trap(trapnr, signr, str, regs, error_code, NULL); \
627}
628
629#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
630asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
631{ \
632 siginfo_t info; \
633 info.si_signo = signr; \
634 info.si_errno = 0; \
635 info.si_code = sicode; \
636 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100637 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
639 == NOTIFY_STOP) \
640 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200641 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 do_trap(trapnr, signr, str, regs, error_code, &info); \
643}
644
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100645DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646DO_ERROR( 4, SIGSEGV, "overflow", overflow)
647DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100648DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
650DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
651DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
652DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
653DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
654DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200655
656/* Runs on IST stack */
657asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
658{
659 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
660 12, SIGBUS) == NOTIFY_STOP)
661 return;
662 preempt_conditional_sti(regs);
663 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
664 preempt_conditional_cli(regs);
665}
Jan Beulicheca37c12006-01-11 22:42:17 +0100666
667asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
668{
669 static const char str[] = "double fault";
670 struct task_struct *tsk = current;
671
672 /* Return not checked because double check cannot be ignored */
673 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
674
675 tsk->thread.error_code = error_code;
676 tsk->thread.trap_no = 8;
677
678 /* This is always a kernel trap and never fixable (and thus must
679 never return). */
680 for (;;)
681 die(str, regs, error_code);
682}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700684asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
685 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100687 struct task_struct *tsk = current;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 conditional_sti(regs);
690
Jan Beulich6e3f3612006-01-11 22:42:14 +0100691 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200692 tsk->thread.error_code = error_code;
693 tsk->thread.trap_no = 13;
694
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200695 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
696 printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 printk(KERN_INFO
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100698 "%s[%d] general protection ip:%lx sp:%lx error:%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100700 regs->ip, regs->sp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 force_sig(SIGSEGV, tsk);
703 return;
704 }
705
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100706 if (fixup_exception(regs))
707 return;
Andi Kleend1895182007-05-02 19:27:05 +0200708
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100709 tsk->thread.error_code = error_code;
710 tsk->thread.trap_no = 13;
711 if (notify_die(DIE_GPF, "general protection fault", regs,
712 error_code, 13, SIGSEGV) == NOTIFY_STOP)
713 return;
714 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100717static __kprobes void
718mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200720 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
721 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100722 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200723
Dave Jiangc0d12172007-07-19 01:49:46 -0700724#if defined(CONFIG_EDAC)
725 if(edac_handler_set()) {
726 edac_atomic_assert_error();
727 return;
728 }
729#endif
730
Don Zickus8da5add2006-09-26 10:52:27 +0200731 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200732 panic("NMI: Not continuing");
733
734 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* Clear and disable the memory parity error line. */
737 reason = (reason & 0xf) | 4;
738 outb(reason, 0x61);
739}
740
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100741static __kprobes void
742io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 printk("NMI: IOCK error (debug interrupt?)\n");
745 show_registers(regs);
746
747 /* Re-enable the IOCK line, wait for a few seconds */
748 reason = (reason & 0xf) | 8;
749 outb(reason, 0x61);
750 mdelay(2000);
751 reason &= ~8;
752 outb(reason, 0x61);
753}
754
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100755static __kprobes void
756unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200757{
758 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
759 reason);
760 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200761
762 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200763 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200764
Don Zickusc41c5cd2006-09-26 10:52:27 +0200765 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700768/* Runs on IST stack. This code must keep interrupts off all the time.
769 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100770asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700773 int cpu;
774
775 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700778 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 reason = get_nmi_reason();
780
781 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100782 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 == NOTIFY_STOP)
784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
786 * Ok, so this is none of the documented NMI sources,
787 * so it must be the NMI watchdog.
788 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200789 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200791 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200792 unknown_nmi_error(reason, regs);
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
795 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100796 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return;
798
799 /* AK: following checks seem to be broken on modern chipsets. FIXME */
800
801 if (reason & 0x80)
802 mem_parity_error(reason, regs);
803 if (reason & 0x40)
804 io_check_error(reason, regs);
805}
806
Jan Beulichb556b352006-01-11 22:43:00 +0100807/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700808asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200810 trace_hardirqs_fixup();
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
813 return;
814 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200815 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200817 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700820/* Help handler running on IST stack to switch back to user stack
821 for scheduling or signal handling. The actual stack switch is done in
822 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100823asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700825 struct pt_regs *regs = eregs;
826 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100827 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700828 ;
829 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700830 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800831 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700832 /* Exception from kernel and interrupts are enabled. Move to
833 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100834 else if (eregs->flags & X86_EFLAGS_IF)
835 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700836 if (eregs != regs)
837 *regs = *eregs;
838 return regs;
839}
840
841/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700842asmlinkage void __kprobes do_debug(struct pt_regs * regs,
843 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned long condition;
846 struct task_struct *tsk = current;
847 siginfo_t info;
848
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100849 trace_hardirqs_fixup();
850
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700851 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Roland McGrath10faa812008-01-30 13:30:54 +0100853 /*
854 * The processor cleared BTF, so don't mark that we need it set.
855 */
856 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
857 tsk->thread.debugctlmsr = 0;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700860 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700861 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700862
John Blackwooda65d17c2006-02-12 14:34:58 -0800863 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* Mask out spurious debug traps due to lazy DR7 setting */
866 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
867 if (!tsk->thread.debugreg7) {
868 goto clear_dr7;
869 }
870 }
871
872 tsk->thread.debugreg6 = condition;
873
Roland McGrathe1f28772008-01-30 13:30:50 +0100874
875 /*
876 * Single-stepping through TF: make sure we ignore any events in
877 * kernel space (but re-enable TF when returning to user mode).
878 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700879 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700880 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 /* Ok, finally something we can handle */
885 tsk->thread.trap_no = 1;
886 tsk->thread.error_code = error_code;
887 info.si_signo = SIGTRAP;
888 info.si_errno = 0;
889 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100890 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100891 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700894 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800895 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700896 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898clear_TF_reenable:
899 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100900 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800901 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Jan Beulich6e3f3612006-01-11 22:42:14 +0100904static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100906 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100908
Jan Beulich6e3f3612006-01-11 22:42:14 +0100909 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700910 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100911 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914}
915
916/*
917 * Note that we play around with the 'TS' bit in an attempt to get
918 * the correct behaviour even in the presence of the asynchronous
919 * IRQ13 behaviour
920 */
921asmlinkage void do_coprocessor_error(struct pt_regs *regs)
922{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100923 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct task_struct * task;
925 siginfo_t info;
926 unsigned short cwd, swd;
927
928 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700929 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100930 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return;
932
933 /*
934 * Save the info for the exception handler and clear the error.
935 */
936 task = current;
937 save_init_fpu(task);
938 task->thread.trap_no = 16;
939 task->thread.error_code = 0;
940 info.si_signo = SIGFPE;
941 info.si_errno = 0;
942 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100943 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * (~cwd & swd) will mask out exceptions that are not set to unmasked
946 * status. 0x3f is the exception bits in these regs, 0x200 is the
947 * C1 reg you need in case of a stack fault, 0x040 is the stack
948 * fault bit. We should only be taking one exception at a time,
949 * so if this combination doesn't produce any single exception,
950 * then we have a bad program that isn't synchronizing its FPU usage
951 * and it will suffer the consequences since we won't be able to
952 * fully reproduce the context of the exception
953 */
954 cwd = get_fpu_cwd(task);
955 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200956 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 case 0x000:
958 default:
959 break;
960 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200961 /*
962 * swd & 0x240 == 0x040: Stack Underflow
963 * swd & 0x240 == 0x240: Stack Overflow
964 * User must clear the SF bit (0x40) if set
965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 info.si_code = FPE_FLTINV;
967 break;
968 case 0x002: /* Denormalize */
969 case 0x010: /* Underflow */
970 info.si_code = FPE_FLTUND;
971 break;
972 case 0x004: /* Zero Divide */
973 info.si_code = FPE_FLTDIV;
974 break;
975 case 0x008: /* Overflow */
976 info.si_code = FPE_FLTOVF;
977 break;
978 case 0x020: /* Precision */
979 info.si_code = FPE_FLTRES;
980 break;
981 }
982 force_sig_info(SIGFPE, &info, task);
983}
984
985asmlinkage void bad_intr(void)
986{
987 printk("bad interrupt");
988}
989
990asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
991{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100992 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct task_struct * task;
994 siginfo_t info;
995 unsigned short mxcsr;
996
997 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700998 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100999 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return;
1001
1002 /*
1003 * Save the info for the exception handler and clear the error.
1004 */
1005 task = current;
1006 save_init_fpu(task);
1007 task->thread.trap_no = 19;
1008 task->thread.error_code = 0;
1009 info.si_signo = SIGFPE;
1010 info.si_errno = 0;
1011 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001012 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * The SIMD FPU exceptions are handled a little differently, as there
1015 * is only a single status/control register. Thus, to determine which
1016 * unmasked exception was caught we must mask the exception mask bits
1017 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1018 */
1019 mxcsr = get_fpu_mxcsr(task);
1020 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1021 case 0x000:
1022 default:
1023 break;
1024 case 0x001: /* Invalid Op */
1025 info.si_code = FPE_FLTINV;
1026 break;
1027 case 0x002: /* Denormalize */
1028 case 0x010: /* Underflow */
1029 info.si_code = FPE_FLTUND;
1030 break;
1031 case 0x004: /* Zero Divide */
1032 info.si_code = FPE_FLTDIV;
1033 break;
1034 case 0x008: /* Overflow */
1035 info.si_code = FPE_FLTOVF;
1036 break;
1037 case 0x020: /* Precision */
1038 info.si_code = FPE_FLTRES;
1039 break;
1040 }
1041 force_sig_info(SIGFPE, &info, task);
1042}
1043
1044asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1045{
1046}
1047
1048asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1049{
1050}
1051
Jacob Shin89b831e2005-11-05 17:25:53 +01001052asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1053{
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * 'math_state_restore()' saves the current math information in the
1058 * old math state array, and gets the new ones from the current task
1059 *
1060 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1061 * Don't touch unless you *really* know how it works.
1062 */
1063asmlinkage void math_state_restore(void)
1064{
1065 struct task_struct *me = current;
1066 clts(); /* Allow maths ops (or we recurse) */
1067
1068 if (!used_math())
1069 init_fpu(me);
1070 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001071 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001072 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001074EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076void __init trap_init(void)
1077{
1078 set_intr_gate(0,&divide_error);
1079 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1080 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001081 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001082 set_system_gate(4,&overflow); /* int4 can be called from all */
1083 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 set_intr_gate(6,&invalid_op);
1085 set_intr_gate(7,&device_not_available);
1086 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1087 set_intr_gate(9,&coprocessor_segment_overrun);
1088 set_intr_gate(10,&invalid_TSS);
1089 set_intr_gate(11,&segment_not_present);
1090 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1091 set_intr_gate(13,&general_protection);
1092 set_intr_gate(14,&page_fault);
1093 set_intr_gate(15,&spurious_interrupt_bug);
1094 set_intr_gate(16,&coprocessor_error);
1095 set_intr_gate(17,&alignment_check);
1096#ifdef CONFIG_X86_MCE
1097 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1098#endif
1099 set_intr_gate(19,&simd_coprocessor_error);
1100
1101#ifdef CONFIG_IA32_EMULATION
1102 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1103#endif
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /*
1106 * Should be a barrier for any external CPU state.
1107 */
1108 cpu_init();
1109}
1110
1111
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001112static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001114 if (!s)
1115 return -EINVAL;
1116 if (!strcmp(s, "panic"))
1117 panic_on_oops = 1;
1118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001120early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122static int __init kstack_setup(char *s)
1123{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001124 if (!s)
1125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001129early_param("kstack", kstack_setup);