blob: b8303ed950571c758784bf27cdd2007f84b66021 [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
228static inline unsigned long print_context_stack(struct thread_info *tinfo,
229 unsigned long *stack, unsigned long bp,
230 const struct stacktrace_ops *ops, void *data,
231 unsigned long *end)
232{
233 /*
234 * Print function call entries within a stack. 'cond' is the
235 * "end of stackframe" condition, that the 'stack++'
236 * iteration will eventually trigger.
237 */
238 while (valid_stack_ptr(tinfo, stack, 3, end)) {
239 unsigned long addr = *stack++;
240 /* Use unlocked access here because except for NMIs
241 we should be already protected against module unloads */
242 if (__kernel_text_address(addr)) {
243 /*
244 * If the address is either in the text segment of the
245 * kernel, or in the region which contains vmalloc'ed
246 * memory, it *may* be the address of a calling
247 * routine; if so, print it so that someone tracing
248 * down the cause of the crash will be able to figure
249 * out the call path that was taken.
250 */
251 ops->address(data, addr, 1);
252 }
253 }
254 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100255}
256
Andi Kleenb615ebd2006-12-07 02:14:00 +0100257void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100258 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200259 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Andrew Mortonda689332006-12-07 02:14:02 +0100261 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100262 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700263 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100264 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jan Beulichb538ed22006-06-26 13:57:32 +0200266 if (!tsk)
267 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100268 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200269
Andi Kleenc0b766f2006-09-26 10:52:34 +0200270 if (!stack) {
271 unsigned long dummy;
272 stack = &dummy;
273 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100274 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200275 }
276
Andi Kleen0a658002005-04-16 15:25:17 -0700277
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700278 /*
279 * Print function call entries in all stacks, starting at the
280 * current stack address. If the stacks consist of nested
281 * exceptions
282 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200283 for (;;) {
284 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700285 unsigned long *estack_end;
286 estack_end = in_exception_stack(cpu, (unsigned long)stack,
287 &used, &id);
288
289 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200290 if (ops->stack(data, id) < 0)
291 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100292
293 print_context_stack(tinfo, stack, 0, ops,
294 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200295 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700296 /*
297 * We link to the next stack via the
298 * second-to-last pointer (index -2 to end) in the
299 * exception stack:
300 */
Andi Kleen0a658002005-04-16 15:25:17 -0700301 stack = (unsigned long *) estack_end[-2];
302 continue;
303 }
304 if (irqstack_end) {
305 unsigned long *irqstack;
306 irqstack = irqstack_end -
307 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
308
309 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200310 if (ops->stack(data, "IRQ") < 0)
311 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100312 print_context_stack(tinfo, stack, 0, ops,
313 data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700314 /*
315 * We link to the next stack (which would be
316 * the process stack normally) the last
317 * pointer (index -1 to end) in the IRQ stack:
318 */
Andi Kleen0a658002005-04-16 15:25:17 -0700319 stack = (unsigned long *) (irqstack_end[-1]);
320 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200321 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700322 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 }
Andi Kleen0a658002005-04-16 15:25:17 -0700325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Andi Kleen0a658002005-04-16 15:25:17 -0700327
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700328 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200329 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700330 */
Arjan van de Vene4a94562008-01-30 13:33:07 +0100331 print_context_stack(tinfo, stack, 0, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100332 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200333}
334EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700335
Andi Kleenc0b766f2006-09-26 10:52:34 +0200336static void
337print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
338{
339 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 printk("\n");
341}
342
Andi Kleenc0b766f2006-09-26 10:52:34 +0200343static void print_trace_warning(void *data, char *msg)
344{
345 printk("%s\n", msg);
346}
347
348static int print_trace_stack(void *data, char *name)
349{
350 printk(" <%s> ", name);
351 return 0;
352}
353
Arjan van de Venbc850d62008-01-30 13:33:07 +0100354static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200355{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700356 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100357 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200358}
359
Jan Beulich9689ba82007-10-17 18:04:37 +0200360static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200361 .warning = print_trace_warning,
362 .warning_symbol = print_trace_warning_symbol,
363 .stack = print_trace_stack,
364 .address = print_trace_address,
365};
366
367void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100368show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
369 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200370{
371 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100372 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200373 printk("\n");
374}
375
376static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100377_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
378 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 unsigned long *stack;
381 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200382 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100383 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
384 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 // debugging aid: "show_stack(NULL, NULL);" prints the
387 // back trace for this cpu.
388
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100389 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100391 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100393 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100396 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 for(i=0; i < kstack_depth_to_print; i++) {
398 if (stack >= irqstack && stack <= irqstack_end) {
399 if (stack == irqstack_end) {
400 stack = (unsigned long *) (irqstack_end[-1]);
401 printk(" <EOI> ");
402 }
403 } else {
404 if (((long) stack & (THREAD_SIZE-1)) == 0)
405 break;
406 }
407 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700408 printk("\n");
409 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700410 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100412 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200413}
414
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100415void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200416{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100417 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/*
421 * The architecture-independent dump_stack generator
422 */
423void dump_stack(void)
424{
425 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100426 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100427
428 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
429 current->pid, current->comm, print_tainted(),
430 init_utsname()->release,
431 (int)strcspn(init_utsname()->version, " "),
432 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100433 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436EXPORT_SYMBOL(dump_stack);
437
438void show_registers(struct pt_regs *regs)
439{
440 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700441 int in_kernel = !user_mode(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100442 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200443 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100444 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100446 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 printk("CPU %d ", cpu);
448 __show_regs(regs);
449 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800450 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * When in-kernel, we also print out the stack and code at the
454 * time of the fault..
455 */
456 if (in_kernel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100458 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 printk("\nCode: ");
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100461 if (regs->ip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto bad;
463
Roberto Nibali2b692a82006-03-25 16:29:55 +0100464 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 unsigned char c;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100466 if (__get_user(c, &((unsigned char*)regs->ip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467bad:
468 printk(" Bad RIP value.");
469 break;
470 }
471 printk("%02x ", c);
472 }
473 }
474 printk("\n");
475}
476
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100477int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800478{
479 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100481 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800482 return 0;
483
484 return ud2 == 0x0b0f;
485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Andi Kleen39743c92007-10-19 20:35:03 +0200487static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200489static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100491unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700493 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200494 unsigned long flags;
495
Andrew Mortonabf0f102006-09-26 10:52:36 +0200496 oops_enter();
497
Jan Beulich12091402005-09-12 18:49:24 +0200498 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200499 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700500 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200501 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (cpu == die_owner)
503 /* nested oops. should stop eventually */;
504 else
Andi Kleen39743c92007-10-19 20:35:03 +0200505 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200507 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200508 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200510 bust_spinlocks(1);
511 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Jan Beulich22f59912008-01-30 13:31:23 +0100514void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200517 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200518 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200519 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200520 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200521 __raw_spin_unlock(&die_lock);
522 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100523 if (!regs) {
524 oops_exit();
525 return;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700528 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200529 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100530 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jan Beulich22f59912008-01-30 13:31:23 +0100533int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 static int die_counter;
536 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
537#ifdef CONFIG_PREEMPT
538 printk("PREEMPT ");
539#endif
540#ifdef CONFIG_SMP
541 printk("SMP ");
542#endif
543#ifdef CONFIG_DEBUG_PAGEALLOC
544 printk("DEBUG_PAGEALLOC");
545#endif
546 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100547 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
548 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700550 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Executive summary in case the oops scrolled away */
552 printk(KERN_ALERT "RIP ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100553 printk_address(regs->ip, regs->bp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100554 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200555 if (kexec_should_crash(current))
556 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560void die(const char * str, struct pt_regs * regs, long err)
561{
Jan Beulich12091402005-09-12 18:49:24 +0200562 unsigned long flags = oops_begin();
563
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800564 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100565 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800566
Jan Beulich22f59912008-01-30 13:31:23 +0100567 if (__die(str, regs, err))
568 regs = NULL;
569 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Andi Kleenfac58552006-09-26 10:52:27 +0200572void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Jan Beulich12091402005-09-12 18:49:24 +0200574 unsigned long flags = oops_begin();
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /*
577 * We are in trouble anyway, lets at least try
578 * to get a message out.
579 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200580 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200582 if (kexec_should_crash(current))
583 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200584 if (do_panic || panic_on_oops)
585 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100586 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200587 nmi_exit();
588 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100589 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700592static void __kprobes do_trap(int trapnr, int signr, char *str,
593 struct pt_regs * regs, long error_code,
594 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100596 struct task_struct *tsk = current;
597
Jan Beulich6e3f3612006-01-11 22:42:14 +0100598 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200599 /*
600 * We want error_code and trap_no set for userspace
601 * faults and kernelspace faults which result in
602 * die(), but not kernelspace faults which are fixed
603 * up. die() gives the process no chance to handle
604 * the signal and notice the kernel fault information,
605 * so that won't result in polluting the information
606 * about previously queued, but not yet delivered,
607 * faults. See also do_general_protection below.
608 */
609 tsk->thread.error_code = error_code;
610 tsk->thread.trap_no = trapnr;
611
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200612 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
613 printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 printk(KERN_INFO
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100615 "%s[%d] trap %s ip:%lx sp:%lx error:%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100617 regs->ip, regs->sp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (info)
620 force_sig_info(signr, info, tsk);
621 else
622 force_sig(signr, tsk);
623 return;
624 }
625
626
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100627 if (!fixup_exception(regs)) {
628 tsk->thread.error_code = error_code;
629 tsk->thread.trap_no = trapnr;
630 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635#define DO_ERROR(trapnr, signr, str, name) \
636asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
637{ \
638 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, NULL); \
643}
644
645#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
646asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
647{ \
648 siginfo_t info; \
649 info.si_signo = signr; \
650 info.si_errno = 0; \
651 info.si_code = sicode; \
652 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100653 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
655 == NOTIFY_STOP) \
656 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200657 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 do_trap(trapnr, signr, str, regs, error_code, &info); \
659}
660
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100661DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662DO_ERROR( 4, SIGSEGV, "overflow", overflow)
663DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100664DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
666DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
667DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
668DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
669DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
670DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200671
672/* Runs on IST stack */
673asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
674{
675 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
676 12, SIGBUS) == NOTIFY_STOP)
677 return;
678 preempt_conditional_sti(regs);
679 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
680 preempt_conditional_cli(regs);
681}
Jan Beulicheca37c12006-01-11 22:42:17 +0100682
683asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
684{
685 static const char str[] = "double fault";
686 struct task_struct *tsk = current;
687
688 /* Return not checked because double check cannot be ignored */
689 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
690
691 tsk->thread.error_code = error_code;
692 tsk->thread.trap_no = 8;
693
694 /* This is always a kernel trap and never fixable (and thus must
695 never return). */
696 for (;;)
697 die(str, regs, error_code);
698}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700700asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
701 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100703 struct task_struct *tsk = current;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 conditional_sti(regs);
706
Jan Beulich6e3f3612006-01-11 22:42:14 +0100707 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200708 tsk->thread.error_code = error_code;
709 tsk->thread.trap_no = 13;
710
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200711 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
712 printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 printk(KERN_INFO
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100714 "%s[%d] general protection ip:%lx sp:%lx error:%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100716 regs->ip, regs->sp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 force_sig(SIGSEGV, tsk);
719 return;
720 }
721
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100722 if (fixup_exception(regs))
723 return;
Andi Kleend1895182007-05-02 19:27:05 +0200724
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100725 tsk->thread.error_code = error_code;
726 tsk->thread.trap_no = 13;
727 if (notify_die(DIE_GPF, "general protection fault", regs,
728 error_code, 13, SIGSEGV) == NOTIFY_STOP)
729 return;
730 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100733static __kprobes void
734mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200736 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
737 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100738 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200739
Dave Jiangc0d12172007-07-19 01:49:46 -0700740#if defined(CONFIG_EDAC)
741 if(edac_handler_set()) {
742 edac_atomic_assert_error();
743 return;
744 }
745#endif
746
Don Zickus8da5add2006-09-26 10:52:27 +0200747 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200748 panic("NMI: Not continuing");
749
750 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* Clear and disable the memory parity error line. */
753 reason = (reason & 0xf) | 4;
754 outb(reason, 0x61);
755}
756
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100757static __kprobes void
758io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 printk("NMI: IOCK error (debug interrupt?)\n");
761 show_registers(regs);
762
763 /* Re-enable the IOCK line, wait for a few seconds */
764 reason = (reason & 0xf) | 8;
765 outb(reason, 0x61);
766 mdelay(2000);
767 reason &= ~8;
768 outb(reason, 0x61);
769}
770
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100771static __kprobes void
772unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200773{
774 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
775 reason);
776 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200777
778 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200779 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200780
Don Zickusc41c5cd2006-09-26 10:52:27 +0200781 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700784/* Runs on IST stack. This code must keep interrupts off all the time.
785 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100786asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700789 int cpu;
790
791 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700794 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 reason = get_nmi_reason();
796
797 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100798 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 == NOTIFY_STOP)
800 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * Ok, so this is none of the documented NMI sources,
803 * so it must be the NMI watchdog.
804 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200805 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200807 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200808 unknown_nmi_error(reason, regs);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return;
811 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100812 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return;
814
815 /* AK: following checks seem to be broken on modern chipsets. FIXME */
816
817 if (reason & 0x80)
818 mem_parity_error(reason, regs);
819 if (reason & 0x40)
820 io_check_error(reason, regs);
821}
822
Jan Beulichb556b352006-01-11 22:43:00 +0100823/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700824asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200826 trace_hardirqs_fixup();
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
829 return;
830 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200831 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200833 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700836/* Help handler running on IST stack to switch back to user stack
837 for scheduling or signal handling. The actual stack switch is done in
838 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100839asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700841 struct pt_regs *regs = eregs;
842 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100843 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700844 ;
845 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700846 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800847 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700848 /* Exception from kernel and interrupts are enabled. Move to
849 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100850 else if (eregs->flags & X86_EFLAGS_IF)
851 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700852 if (eregs != regs)
853 *regs = *eregs;
854 return regs;
855}
856
857/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700858asmlinkage void __kprobes do_debug(struct pt_regs * regs,
859 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned long condition;
862 struct task_struct *tsk = current;
863 siginfo_t info;
864
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100865 trace_hardirqs_fixup();
866
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700867 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Roland McGrath10faa812008-01-30 13:30:54 +0100869 /*
870 * The processor cleared BTF, so don't mark that we need it set.
871 */
872 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
873 tsk->thread.debugctlmsr = 0;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700876 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700877 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700878
John Blackwooda65d17c2006-02-12 14:34:58 -0800879 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* Mask out spurious debug traps due to lazy DR7 setting */
882 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
883 if (!tsk->thread.debugreg7) {
884 goto clear_dr7;
885 }
886 }
887
888 tsk->thread.debugreg6 = condition;
889
Roland McGrathe1f28772008-01-30 13:30:50 +0100890
891 /*
892 * Single-stepping through TF: make sure we ignore any events in
893 * kernel space (but re-enable TF when returning to user mode).
894 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700895 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700896 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
900 /* Ok, finally something we can handle */
901 tsk->thread.trap_no = 1;
902 tsk->thread.error_code = error_code;
903 info.si_signo = SIGTRAP;
904 info.si_errno = 0;
905 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100906 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100907 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700910 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800911 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700912 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914clear_TF_reenable:
915 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100916 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800917 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Jan Beulich6e3f3612006-01-11 22:42:14 +0100920static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100922 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100924
Jan Beulich6e3f3612006-01-11 22:42:14 +0100925 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700926 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100927 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
932/*
933 * Note that we play around with the 'TS' bit in an attempt to get
934 * the correct behaviour even in the presence of the asynchronous
935 * IRQ13 behaviour
936 */
937asmlinkage void do_coprocessor_error(struct pt_regs *regs)
938{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100939 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct task_struct * task;
941 siginfo_t info;
942 unsigned short cwd, swd;
943
944 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700945 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100946 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return;
948
949 /*
950 * Save the info for the exception handler and clear the error.
951 */
952 task = current;
953 save_init_fpu(task);
954 task->thread.trap_no = 16;
955 task->thread.error_code = 0;
956 info.si_signo = SIGFPE;
957 info.si_errno = 0;
958 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100959 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /*
961 * (~cwd & swd) will mask out exceptions that are not set to unmasked
962 * status. 0x3f is the exception bits in these regs, 0x200 is the
963 * C1 reg you need in case of a stack fault, 0x040 is the stack
964 * fault bit. We should only be taking one exception at a time,
965 * so if this combination doesn't produce any single exception,
966 * then we have a bad program that isn't synchronizing its FPU usage
967 * and it will suffer the consequences since we won't be able to
968 * fully reproduce the context of the exception
969 */
970 cwd = get_fpu_cwd(task);
971 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200972 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 case 0x000:
974 default:
975 break;
976 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200977 /*
978 * swd & 0x240 == 0x040: Stack Underflow
979 * swd & 0x240 == 0x240: Stack Overflow
980 * User must clear the SF bit (0x40) if set
981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 info.si_code = FPE_FLTINV;
983 break;
984 case 0x002: /* Denormalize */
985 case 0x010: /* Underflow */
986 info.si_code = FPE_FLTUND;
987 break;
988 case 0x004: /* Zero Divide */
989 info.si_code = FPE_FLTDIV;
990 break;
991 case 0x008: /* Overflow */
992 info.si_code = FPE_FLTOVF;
993 break;
994 case 0x020: /* Precision */
995 info.si_code = FPE_FLTRES;
996 break;
997 }
998 force_sig_info(SIGFPE, &info, task);
999}
1000
1001asmlinkage void bad_intr(void)
1002{
1003 printk("bad interrupt");
1004}
1005
1006asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1007{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001008 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct task_struct * task;
1010 siginfo_t info;
1011 unsigned short mxcsr;
1012
1013 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001014 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001015 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return;
1017
1018 /*
1019 * Save the info for the exception handler and clear the error.
1020 */
1021 task = current;
1022 save_init_fpu(task);
1023 task->thread.trap_no = 19;
1024 task->thread.error_code = 0;
1025 info.si_signo = SIGFPE;
1026 info.si_errno = 0;
1027 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001028 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /*
1030 * The SIMD FPU exceptions are handled a little differently, as there
1031 * is only a single status/control register. Thus, to determine which
1032 * unmasked exception was caught we must mask the exception mask bits
1033 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1034 */
1035 mxcsr = get_fpu_mxcsr(task);
1036 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1037 case 0x000:
1038 default:
1039 break;
1040 case 0x001: /* Invalid Op */
1041 info.si_code = FPE_FLTINV;
1042 break;
1043 case 0x002: /* Denormalize */
1044 case 0x010: /* Underflow */
1045 info.si_code = FPE_FLTUND;
1046 break;
1047 case 0x004: /* Zero Divide */
1048 info.si_code = FPE_FLTDIV;
1049 break;
1050 case 0x008: /* Overflow */
1051 info.si_code = FPE_FLTOVF;
1052 break;
1053 case 0x020: /* Precision */
1054 info.si_code = FPE_FLTRES;
1055 break;
1056 }
1057 force_sig_info(SIGFPE, &info, task);
1058}
1059
1060asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1061{
1062}
1063
1064asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1065{
1066}
1067
Jacob Shin89b831e2005-11-05 17:25:53 +01001068asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1069{
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
1073 * 'math_state_restore()' saves the current math information in the
1074 * old math state array, and gets the new ones from the current task
1075 *
1076 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1077 * Don't touch unless you *really* know how it works.
1078 */
1079asmlinkage void math_state_restore(void)
1080{
1081 struct task_struct *me = current;
1082 clts(); /* Allow maths ops (or we recurse) */
1083
1084 if (!used_math())
1085 init_fpu(me);
1086 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001087 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001088 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001090EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092void __init trap_init(void)
1093{
1094 set_intr_gate(0,&divide_error);
1095 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1096 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001097 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001098 set_system_gate(4,&overflow); /* int4 can be called from all */
1099 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 set_intr_gate(6,&invalid_op);
1101 set_intr_gate(7,&device_not_available);
1102 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1103 set_intr_gate(9,&coprocessor_segment_overrun);
1104 set_intr_gate(10,&invalid_TSS);
1105 set_intr_gate(11,&segment_not_present);
1106 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1107 set_intr_gate(13,&general_protection);
1108 set_intr_gate(14,&page_fault);
1109 set_intr_gate(15,&spurious_interrupt_bug);
1110 set_intr_gate(16,&coprocessor_error);
1111 set_intr_gate(17,&alignment_check);
1112#ifdef CONFIG_X86_MCE
1113 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1114#endif
1115 set_intr_gate(19,&simd_coprocessor_error);
1116
1117#ifdef CONFIG_IA32_EMULATION
1118 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1119#endif
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /*
1122 * Should be a barrier for any external CPU state.
1123 */
1124 cpu_init();
1125}
1126
1127
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001128static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001130 if (!s)
1131 return -EINVAL;
1132 if (!strcmp(s, "panic"))
1133 panic_on_oops = 1;
1134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001136early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138static int __init kstack_setup(char *s)
1139{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001140 if (!s)
1141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001145early_param("kstack", kstack_setup);