blob: f1a95d1059531e3cb0f5f920dc4bb1a04d7c573c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'entry.S'.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/ptrace.h>
18#include <linux/timer.h>
19#include <linux/mm.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
Randy Dunlap4b0ff1a2006-10-05 19:07:26 +020024#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070027#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070028#include <linux/kprobes.h>
Vivek Goyal8bcc5282006-04-18 12:35:13 +020029#include <linux/kexec.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020030#include <linux/unwind.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010031#include <linux/uaccess.h>
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -080032#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070033#include <linux/kdebug.h>
Arjan van de Ven57c351d2007-11-26 20:42:19 +010034#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Glauber de Oliveira Costae32ede12008-03-19 14:25:35 -030036#include <mach_traps.h>
37
Dave Jiangc0d12172007-07-19 01:49:46 -070038#if defined(CONFIG_EDAC)
39#include <linux/edac.h>
40#endif
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
44#include <asm/atomic.h>
45#include <asm/debugreg.h>
46#include <asm/desc.h>
47#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020049#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/smp.h>
51#include <asm/pgalloc.h>
52#include <asm/pda.h>
53#include <asm/proto.h>
54#include <asm/nmi.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020055#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057asmlinkage void divide_error(void);
58asmlinkage void debug(void);
59asmlinkage void nmi(void);
60asmlinkage void int3(void);
61asmlinkage void overflow(void);
62asmlinkage void bounds(void);
63asmlinkage void invalid_op(void);
64asmlinkage void device_not_available(void);
65asmlinkage void double_fault(void);
66asmlinkage void coprocessor_segment_overrun(void);
67asmlinkage void invalid_TSS(void);
68asmlinkage void segment_not_present(void);
69asmlinkage void stack_segment(void);
70asmlinkage void general_protection(void);
71asmlinkage void page_fault(void);
72asmlinkage void coprocessor_error(void);
73asmlinkage void simd_coprocessor_error(void);
74asmlinkage void reserved(void);
75asmlinkage void alignment_check(void);
76asmlinkage void machine_check(void);
77asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Arjan van de Vena25bd942008-01-30 13:33:08 +010079static unsigned int code_bytes = 64;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static inline void conditional_sti(struct pt_regs *regs)
82{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010083 if (regs->flags & X86_EFLAGS_IF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 local_irq_enable();
85}
86
John Blackwooda65d17c2006-02-12 14:34:58 -080087static inline void preempt_conditional_sti(struct pt_regs *regs)
88{
Ingo Molnare8bff742008-02-13 20:21:06 +010089 inc_preempt_count();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010090 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080091 local_irq_enable();
92}
93
94static inline void preempt_conditional_cli(struct pt_regs *regs)
95{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010096 if (regs->flags & X86_EFLAGS_IF)
John Blackwooda65d17c2006-02-12 14:34:58 -080097 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +020098 /* Make sure to not schedule here because we could be running
99 on an exception stack. */
Ingo Molnare8bff742008-02-13 20:21:06 +0100100 dec_preempt_count();
John Blackwooda65d17c2006-02-12 14:34:58 -0800101}
102
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100103int kstack_depth_to_print = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Arjan van de Venbc850d62008-01-30 13:33:07 +0100105void printk_address(unsigned long address, int reliable)
Ingo Molnar3ac94932006-07-03 00:24:36 -0700106{
Linus Torvalds7daf7052008-07-14 12:12:53 -0700107 printk(" [<%016lx>] %s%pS\n", address, reliable ? "": "? ", (void *) address);
Harvey Harrisona5ff6772008-01-30 13:33:25 +0100108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andi Kleen0a658002005-04-16 15:25:17 -0700110static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200111 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700112{
Jan Beulichb556b352006-01-11 22:43:00 +0100113 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700114 [DEBUG_STACK - 1] = "#DB",
115 [NMI_STACK - 1] = "NMI",
116 [DOUBLEFAULT_STACK - 1] = "#DF",
117 [STACKFAULT_STACK - 1] = "#SS",
118 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100119#if DEBUG_STKSZ > EXCEPTION_STKSZ
120 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
121#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700122 };
123 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700125 /*
126 * Iterate over all exception stacks, and figure out whether
127 * 'stack' is in one of them:
128 */
Andi Kleen0a658002005-04-16 15:25:17 -0700129 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200130 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700131 /*
132 * Is 'stack' above this exception frame's end?
133 * If yes then skip to the next frame.
134 */
Andi Kleen0a658002005-04-16 15:25:17 -0700135 if (stack >= end)
136 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700137 /*
138 * Is 'stack' above this exception frame's start address?
139 * If yes then we found the right frame.
140 */
Andi Kleen0a658002005-04-16 15:25:17 -0700141 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700142 /*
143 * Make sure we only iterate through an exception
144 * stack once. If it comes up for the second time
145 * then there's something wrong going on - just
146 * break out and return NULL:
147 */
Andi Kleen0a658002005-04-16 15:25:17 -0700148 if (*usedp & (1U << k))
149 break;
150 *usedp |= 1U << k;
151 *idp = ids[k];
152 return (unsigned long *)end;
153 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700154 /*
155 * If this is a debug stack, and if it has a larger size than
156 * the usual exception stacks, then 'stack' might still
157 * be within the lower portion of the debug stack:
158 */
Jan Beulichb556b352006-01-11 22:43:00 +0100159#if DEBUG_STKSZ > EXCEPTION_STKSZ
160 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
161 unsigned j = N_EXCEPTION_STACKS - 1;
162
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700163 /*
164 * Black magic. A large debug stack is composed of
165 * multiple exception stack entries, which we
166 * iterate through now. Dont look:
167 */
Jan Beulichb556b352006-01-11 22:43:00 +0100168 do {
169 ++j;
170 end -= EXCEPTION_STKSZ;
171 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
172 } while (stack < end - EXCEPTION_STKSZ);
173 if (*usedp & (1U << j))
174 break;
175 *usedp |= 1U << j;
176 *idp = ids[j];
177 return (unsigned long *)end;
178 }
179#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Andi Kleenb615ebd2006-12-07 02:14:00 +0100184#define MSG(txt) ops->warning(data, txt)
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
Simon Arlott676b1852007-10-20 01:25:36 +0200187 * x86-64 can have up to three kernel stacks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * process stack
189 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700190 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192
Arjan van de Vene4a94562008-01-30 13:33:07 +0100193static inline int valid_stack_ptr(struct thread_info *tinfo,
194 void *p, unsigned int size, void *end)
Andi Kleenc547c772006-11-28 20:12:59 +0100195{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100196 void *t = tinfo;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100197 if (end) {
198 if (p < end && p >= (end-THREAD_SIZE))
199 return 1;
200 else
201 return 0;
202 }
203 return p > t && p < t + THREAD_SIZE - size;
204}
205
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100206/* The form of the top of the frame on the stack */
207struct stack_frame {
208 struct stack_frame *next_frame;
209 unsigned long return_address;
210};
211
212
Arjan van de Vene4a94562008-01-30 13:33:07 +0100213static inline unsigned long print_context_stack(struct thread_info *tinfo,
214 unsigned long *stack, unsigned long bp,
215 const struct stacktrace_ops *ops, void *data,
216 unsigned long *end)
217{
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100218 struct stack_frame *frame = (struct stack_frame *)bp;
219
220 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
221 unsigned long addr;
222
223 addr = *stack;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100224 if (__kernel_text_address(addr)) {
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100225 if ((unsigned long) stack == bp + 8) {
226 ops->address(data, addr, 1);
227 frame = frame->next_frame;
228 bp = (unsigned long) frame;
229 } else {
230 ops->address(data, addr, bp == 0);
231 }
Arjan van de Vene4a94562008-01-30 13:33:07 +0100232 }
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100233 stack++;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100234 }
235 return bp;
Andi Kleenc547c772006-11-28 20:12:59 +0100236}
237
Andi Kleenb615ebd2006-12-07 02:14:00 +0100238void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
Arjan van de Venbc850d62008-01-30 13:33:07 +0100239 unsigned long *stack, unsigned long bp,
Jan Beulich9689ba82007-10-17 18:04:37 +0200240 const struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Andrew Mortonda689332006-12-07 02:14:02 +0100242 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100243 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700244 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100245 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jan Beulichb538ed22006-06-26 13:57:32 +0200247 if (!tsk)
248 tsk = current;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100249 tinfo = task_thread_info(tsk);
Jan Beulichb538ed22006-06-26 13:57:32 +0200250
Andi Kleenc0b766f2006-09-26 10:52:34 +0200251 if (!stack) {
252 unsigned long dummy;
253 stack = &dummy;
254 if (tsk && tsk != current)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100255 stack = (unsigned long *)tsk->thread.sp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200256 }
257
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100258#ifdef CONFIG_FRAME_POINTER
259 if (!bp) {
260 if (tsk == current) {
261 /* Grab bp right from our regs */
262 asm("movq %%rbp, %0" : "=r" (bp):);
263 } else {
264 /* bp is the last reg pushed by switch_to */
265 bp = *(unsigned long *) tsk->thread.sp;
266 }
267 }
268#endif
269
270
Andi Kleen0a658002005-04-16 15:25:17 -0700271
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700272 /*
273 * Print function call entries in all stacks, starting at the
274 * current stack address. If the stacks consist of nested
275 * exceptions
276 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200277 for (;;) {
278 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700279 unsigned long *estack_end;
280 estack_end = in_exception_stack(cpu, (unsigned long)stack,
281 &used, &id);
282
283 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200284 if (ops->stack(data, id) < 0)
285 break;
Arjan van de Vene4a94562008-01-30 13:33:07 +0100286
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100287 bp = print_context_stack(tinfo, stack, bp, ops,
288 data, estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200289 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700290 /*
291 * We link to the next stack via the
292 * second-to-last pointer (index -2 to end) in the
293 * exception stack:
294 */
Andi Kleen0a658002005-04-16 15:25:17 -0700295 stack = (unsigned long *) estack_end[-2];
296 continue;
297 }
298 if (irqstack_end) {
299 unsigned long *irqstack;
300 irqstack = irqstack_end -
301 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
302
303 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200304 if (ops->stack(data, "IRQ") < 0)
305 break;
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100306 bp = print_context_stack(tinfo, stack, bp,
307 ops, data, irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700308 /*
309 * We link to the next stack (which would be
310 * the process stack normally) the last
311 * pointer (index -1 to end) in the IRQ stack:
312 */
Andi Kleen0a658002005-04-16 15:25:17 -0700313 stack = (unsigned long *) (irqstack_end[-1]);
314 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200315 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700316 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 }
Andi Kleen0a658002005-04-16 15:25:17 -0700319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Andi Kleen0a658002005-04-16 15:25:17 -0700321
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700322 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200323 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700324 */
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100325 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
Andrew Mortonda689332006-12-07 02:14:02 +0100326 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200327}
328EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700329
Andi Kleenc0b766f2006-09-26 10:52:34 +0200330static void
331print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
332{
333 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 printk("\n");
335}
336
Andi Kleenc0b766f2006-09-26 10:52:34 +0200337static void print_trace_warning(void *data, char *msg)
338{
339 printk("%s\n", msg);
340}
341
342static int print_trace_stack(void *data, char *name)
343{
344 printk(" <%s> ", name);
345 return 0;
346}
347
Arjan van de Venbc850d62008-01-30 13:33:07 +0100348static void print_trace_address(void *data, unsigned long addr, int reliable)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200349{
Konrad Rzeszutek1c978b92007-07-17 04:03:56 -0700350 touch_nmi_watchdog();
Arjan van de Venbc850d62008-01-30 13:33:07 +0100351 printk_address(addr, reliable);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200352}
353
Jan Beulich9689ba82007-10-17 18:04:37 +0200354static const struct stacktrace_ops print_trace_ops = {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200355 .warning = print_trace_warning,
356 .warning_symbol = print_trace_warning_symbol,
357 .stack = print_trace_stack,
358 .address = print_trace_address,
359};
360
361void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100362show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
363 unsigned long bp)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200364{
365 printk("\nCall Trace:\n");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100366 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200367 printk("\n");
368}
369
370static void
Arjan van de Venbc850d62008-01-30 13:33:07 +0100371_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
372 unsigned long bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 unsigned long *stack;
375 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200376 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100377 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
378 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 // debugging aid: "show_stack(NULL, NULL);" prints the
381 // back trace for this cpu.
382
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100383 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (tsk)
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100385 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100387 sp = (unsigned long *)&sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100390 stack = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 for(i=0; i < kstack_depth_to_print; i++) {
392 if (stack >= irqstack && stack <= irqstack_end) {
393 if (stack == irqstack_end) {
394 stack = (unsigned long *) (irqstack_end[-1]);
395 printk(" <EOI> ");
396 }
397 } else {
398 if (((long) stack & (THREAD_SIZE-1)) == 0)
399 break;
400 }
401 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700402 printk("\n");
403 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700404 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Arjan van de Venbc850d62008-01-30 13:33:07 +0100406 show_trace(tsk, regs, sp, bp);
Jan Beulichb538ed22006-06-26 13:57:32 +0200407}
408
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100409void show_stack(struct task_struct *tsk, unsigned long * sp)
Jan Beulichb538ed22006-06-26 13:57:32 +0200410{
Arjan van de Venbc850d62008-01-30 13:33:07 +0100411 _show_stack(tsk, NULL, sp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/*
415 * The architecture-independent dump_stack generator
416 */
417void dump_stack(void)
418{
419 unsigned long dummy;
Arjan van de Venbc850d62008-01-30 13:33:07 +0100420 unsigned long bp = 0;
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100421
Arjan van de Ven80b51f32008-01-30 13:33:07 +0100422#ifdef CONFIG_FRAME_POINTER
423 if (!bp)
424 asm("movq %%rbp, %0" : "=r" (bp):);
425#endif
426
Arjan van de Ven57c351d2007-11-26 20:42:19 +0100427 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
428 current->pid, current->comm, print_tainted(),
429 init_utsname()->release,
430 (int)strcspn(init_utsname()->version, " "),
431 init_utsname()->version);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100432 show_trace(NULL, NULL, &dummy, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435EXPORT_SYMBOL(dump_stack);
436
437void show_registers(struct pt_regs *regs)
438{
439 int i;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100440 unsigned long sp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200441 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100442 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100443 u8 *ip;
444 unsigned int code_prologue = code_bytes * 43 / 64;
445 unsigned int code_len = code_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100447 sp = regs->sp;
Arjan van de Vena25bd942008-01-30 13:33:08 +0100448 ip = (u8 *) regs->ip - code_prologue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 printk("CPU %d ", cpu);
450 __show_regs(regs);
451 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800452 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /*
455 * When in-kernel, we also print out the stack and code at the
456 * time of the fault..
457 */
Arjan van de Vena25bd942008-01-30 13:33:08 +0100458 if (!user_mode(regs)) {
459 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 printk("Stack: ");
Arjan van de Venbc850d62008-01-30 13:33:07 +0100461 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
Arjan van de Vena25bd942008-01-30 13:33:08 +0100462 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Arjan van de Vena25bd942008-01-30 13:33:08 +0100464 printk(KERN_EMERG "Code: ");
465 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
466 /* try starting at RIP */
467 ip = (u8 *) regs->ip;
468 code_len = code_len - code_prologue + 1;
469 }
470 for (i = 0; i < code_len; i++, ip++) {
471 if (ip < (u8 *)PAGE_OFFSET ||
472 probe_kernel_address(ip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk(" Bad RIP value.");
474 break;
475 }
Arjan van de Vena25bd942008-01-30 13:33:08 +0100476 if (ip == (u8 *)regs->ip)
477 printk("<%02x> ", c);
478 else
479 printk("%02x ", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 }
482 printk("\n");
483}
484
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100485int is_valid_bugaddr(unsigned long ip)
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800486{
487 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100489 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800490 return 0;
491
492 return ud2 == 0x0b0f;
493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Andi Kleen39743c92007-10-19 20:35:03 +0200495static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200497static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100499unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Andrew Mortonb39b7032007-06-08 13:47:01 -0700501 int cpu;
Jan Beulich12091402005-09-12 18:49:24 +0200502 unsigned long flags;
503
Andrew Mortonabf0f102006-09-26 10:52:36 +0200504 oops_enter();
505
Jan Beulich12091402005-09-12 18:49:24 +0200506 /* racy, but better than risking deadlock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200507 raw_local_irq_save(flags);
Andrew Mortonb39b7032007-06-08 13:47:01 -0700508 cpu = smp_processor_id();
Andi Kleen39743c92007-10-19 20:35:03 +0200509 if (!__raw_spin_trylock(&die_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (cpu == die_owner)
511 /* nested oops. should stop eventually */;
512 else
Andi Kleen39743c92007-10-19 20:35:03 +0200513 __raw_spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200515 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200516 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200518 bust_spinlocks(1);
519 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Jan Beulich22f59912008-01-30 13:31:23 +0100522void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200525 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200526 die_nest_count--;
Andi Kleen39743c92007-10-19 20:35:03 +0200527 if (!die_nest_count)
Corey Minyardcdc60a42006-05-08 15:17:22 +0200528 /* Nest count reaches zero, release the lock. */
Andi Kleen39743c92007-10-19 20:35:03 +0200529 __raw_spin_unlock(&die_lock);
530 raw_local_irq_restore(flags);
Jan Beulich22f59912008-01-30 13:31:23 +0100531 if (!regs) {
532 oops_exit();
533 return;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700536 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200537 oops_exit();
Jan Beulich22f59912008-01-30 13:31:23 +0100538 do_exit(signr);
Jan Beulich12091402005-09-12 18:49:24 +0200539}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jan Beulich22f59912008-01-30 13:31:23 +0100541int __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 static int die_counter;
544 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
545#ifdef CONFIG_PREEMPT
546 printk("PREEMPT ");
547#endif
548#ifdef CONFIG_SMP
549 printk("SMP ");
550#endif
551#ifdef CONFIG_DEBUG_PAGEALLOC
552 printk("DEBUG_PAGEALLOC");
553#endif
554 printk("\n");
Jan Beulich22f59912008-01-30 13:31:23 +0100555 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
556 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700558 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Executive summary in case the oops scrolled away */
560 printk(KERN_ALERT "RIP ");
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100561 printk_address(regs->ip, 1);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100562 printk(" RSP <%016lx>\n", regs->sp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200563 if (kexec_should_crash(current))
564 crash_kexec(regs);
Jan Beulich22f59912008-01-30 13:31:23 +0100565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568void die(const char * str, struct pt_regs * regs, long err)
569{
Jan Beulich12091402005-09-12 18:49:24 +0200570 unsigned long flags = oops_begin();
571
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800572 if (!user_mode(regs))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100573 report_bug(regs->ip, regs);
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800574
Jan Beulich22f59912008-01-30 13:31:23 +0100575 if (__die(str, regs, err))
576 regs = NULL;
577 oops_end(flags, regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200580notrace __kprobes void
581die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Jason Wessel737a4602008-03-07 16:34:16 -0600583 unsigned long flags;
Jan Beulich12091402005-09-12 18:49:24 +0200584
Jason Wessel737a4602008-03-07 16:34:16 -0600585 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
586 NOTIFY_STOP)
587 return;
588
589 flags = oops_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
591 * We are in trouble anyway, lets at least try
592 * to get a message out.
593 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200594 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200596 if (kexec_should_crash(current))
597 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200598 if (do_panic || panic_on_oops)
599 panic("Non maskable interrupt");
Jan Beulich22f59912008-01-30 13:31:23 +0100600 oops_end(flags, NULL, SIGBUS);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200601 nmi_exit();
602 local_irq_enable();
Jan Beulich22f59912008-01-30 13:31:23 +0100603 do_exit(SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700606static void __kprobes do_trap(int trapnr, int signr, char *str,
607 struct pt_regs * regs, long error_code,
608 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100610 struct task_struct *tsk = current;
611
Jan Beulich6e3f3612006-01-11 22:42:14 +0100612 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200613 /*
614 * We want error_code and trap_no set for userspace
615 * faults and kernelspace faults which result in
616 * die(), but not kernelspace faults which are fixed
617 * up. die() gives the process no chance to handle
618 * the signal and notice the kernel fault information,
619 * so that won't result in polluting the information
620 * about previously queued, but not yet delivered,
621 * faults. See also do_general_protection below.
622 */
623 tsk->thread.error_code = error_code;
624 tsk->thread.trap_no = trapnr;
625
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200626 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
Andi Kleen03252912008-01-30 13:33:18 +0100627 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100629 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 tsk->comm, tsk->pid, str,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100631 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100632 print_vma_addr(" in ", regs->ip);
633 printk("\n");
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (info)
637 force_sig_info(signr, info, tsk);
638 else
639 force_sig(signr, tsk);
640 return;
641 }
642
643
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100644 if (!fixup_exception(regs)) {
645 tsk->thread.error_code = error_code;
646 tsk->thread.trap_no = trapnr;
647 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100649 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652#define DO_ERROR(trapnr, signr, str, name) \
653asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
654{ \
655 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
656 == NOTIFY_STOP) \
657 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200658 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 do_trap(trapnr, signr, str, regs, error_code, NULL); \
660}
661
662#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
663asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
664{ \
665 siginfo_t info; \
666 info.si_signo = signr; \
667 info.si_errno = 0; \
668 info.si_code = sicode; \
669 info.si_addr = (void __user *)siaddr; \
Peter Zijlstrafb1dac92008-01-16 09:51:59 +0100670 trace_hardirqs_fixup(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
672 == NOTIFY_STOP) \
673 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200674 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 do_trap(trapnr, signr, str, regs, error_code, &info); \
676}
677
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100678DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679DO_ERROR( 4, SIGSEGV, "overflow", overflow)
680DO_ERROR( 5, SIGSEGV, "bounds", bounds)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100681DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
683DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
684DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
685DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
686DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
687DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200688
689/* Runs on IST stack */
690asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
691{
692 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
693 12, SIGBUS) == NOTIFY_STOP)
694 return;
695 preempt_conditional_sti(regs);
696 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
697 preempt_conditional_cli(regs);
698}
Jan Beulicheca37c12006-01-11 22:42:17 +0100699
700asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
701{
702 static const char str[] = "double fault";
703 struct task_struct *tsk = current;
704
705 /* Return not checked because double check cannot be ignored */
706 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
707
708 tsk->thread.error_code = error_code;
709 tsk->thread.trap_no = 8;
710
711 /* This is always a kernel trap and never fixable (and thus must
712 never return). */
713 for (;;)
714 die(str, regs, error_code);
715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700717asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
718 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100720 struct task_struct *tsk = current;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 conditional_sti(regs);
723
Jan Beulich6e3f3612006-01-11 22:42:14 +0100724 if (user_mode(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200725 tsk->thread.error_code = error_code;
726 tsk->thread.trap_no = 13;
727
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200728 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
Andi Kleen03252912008-01-30 13:33:18 +0100729 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 printk(KERN_INFO
Andi Kleen03252912008-01-30 13:33:18 +0100731 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 tsk->comm, tsk->pid,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100733 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100734 print_vma_addr(" in ", regs->ip);
735 printk("\n");
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 force_sig(SIGSEGV, tsk);
739 return;
740 }
741
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100742 if (fixup_exception(regs))
743 return;
Andi Kleend1895182007-05-02 19:27:05 +0200744
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100745 tsk->thread.error_code = error_code;
746 tsk->thread.trap_no = 13;
747 if (notify_die(DIE_GPF, "general protection fault", regs,
748 error_code, 13, SIGSEGV) == NOTIFY_STOP)
749 return;
750 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200753static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100754mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200756 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
757 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100758 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200759
Dave Jiangc0d12172007-07-19 01:49:46 -0700760#if defined(CONFIG_EDAC)
761 if(edac_handler_set()) {
762 edac_atomic_assert_error();
763 return;
764 }
765#endif
766
Don Zickus8da5add2006-09-26 10:52:27 +0200767 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200768 panic("NMI: Not continuing");
769
770 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* Clear and disable the memory parity error line. */
773 reason = (reason & 0xf) | 4;
774 outb(reason, 0x61);
775}
776
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200777static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100778io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 printk("NMI: IOCK error (debug interrupt?)\n");
781 show_registers(regs);
782
783 /* Re-enable the IOCK line, wait for a few seconds */
784 reason = (reason & 0xf) | 8;
785 outb(reason, 0x61);
786 mdelay(2000);
787 reason &= ~8;
788 outb(reason, 0x61);
789}
790
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200791static notrace __kprobes void
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100792unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200793{
Jason Wesseld3597522008-02-15 14:55:53 -0600794 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
795 return;
Don Zickusc41c5cd2006-09-26 10:52:27 +0200796 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
797 reason);
798 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200799
800 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200801 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200802
Don Zickusc41c5cd2006-09-26 10:52:27 +0200803 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700806/* Runs on IST stack. This code must keep interrupts off all the time.
807 Nested NMIs are prevented by the CPU. */
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200808asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700811 int cpu;
812
813 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700816 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 reason = get_nmi_reason();
818
819 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100820 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 == NOTIFY_STOP)
822 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /*
824 * Ok, so this is none of the documented NMI sources,
825 * so it must be the NMI watchdog.
826 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200827 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200829 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200830 unknown_nmi_error(reason, regs);
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return;
833 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100834 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return;
836
837 /* AK: following checks seem to be broken on modern chipsets. FIXME */
838
839 if (reason & 0x80)
840 mem_parity_error(reason, regs);
841 if (reason & 0x40)
842 io_check_error(reason, regs);
843}
844
Jan Beulichb556b352006-01-11 22:43:00 +0100845/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700846asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200848 trace_hardirqs_fixup();
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
851 return;
852 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200853 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200855 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700858/* Help handler running on IST stack to switch back to user stack
859 for scheduling or signal handling. The actual stack switch is done in
860 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100861asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700863 struct pt_regs *regs = eregs;
864 /* Did already sync */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100865 if (eregs == (struct pt_regs *)eregs->sp)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700866 ;
867 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700868 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800869 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700870 /* Exception from kernel and interrupts are enabled. Move to
871 kernel process stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100872 else if (eregs->flags & X86_EFLAGS_IF)
873 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700874 if (eregs != regs)
875 *regs = *eregs;
876 return regs;
877}
878
879/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700880asmlinkage void __kprobes do_debug(struct pt_regs * regs,
881 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 unsigned long condition;
884 struct task_struct *tsk = current;
885 siginfo_t info;
886
Peter Zijlstra000f4a92007-11-26 20:42:19 +0100887 trace_hardirqs_fixup();
888
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700889 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Roland McGrath10faa812008-01-30 13:30:54 +0100891 /*
892 * The processor cleared BTF, so don't mark that we need it set.
893 */
894 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
895 tsk->thread.debugctlmsr = 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700898 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700899 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700900
John Blackwooda65d17c2006-02-12 14:34:58 -0800901 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* Mask out spurious debug traps due to lazy DR7 setting */
904 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
905 if (!tsk->thread.debugreg7) {
906 goto clear_dr7;
907 }
908 }
909
910 tsk->thread.debugreg6 = condition;
911
Roland McGrathe1f28772008-01-30 13:30:50 +0100912
913 /*
914 * Single-stepping through TF: make sure we ignore any events in
915 * kernel space (but re-enable TF when returning to user mode).
916 */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700917 if (condition & DR_STEP) {
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700918 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto clear_TF_reenable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
922 /* Ok, finally something we can handle */
923 tsk->thread.trap_no = 1;
924 tsk->thread.error_code = error_code;
925 info.si_signo = SIGTRAP;
926 info.si_errno = 0;
927 info.si_code = TRAP_BRKPT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100928 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
John Blackwood01b8faa2006-01-11 22:44:15 +0100929 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700932 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800933 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936clear_TF_reenable:
937 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100938 regs->flags &= ~X86_EFLAGS_TF;
John Blackwooda65d17c2006-02-12 14:34:58 -0800939 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Jan Beulich6e3f3612006-01-11 22:42:14 +0100942static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100944 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 1;
Harvey Harrisonb3a5acc2008-01-30 13:32:59 +0100946
Jan Beulich6e3f3612006-01-11 22:42:14 +0100947 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700948 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100949 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return 0;
952}
953
954/*
955 * Note that we play around with the 'TS' bit in an attempt to get
956 * the correct behaviour even in the presence of the asynchronous
957 * IRQ13 behaviour
958 */
959asmlinkage void do_coprocessor_error(struct pt_regs *regs)
960{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100961 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct task_struct * task;
963 siginfo_t info;
964 unsigned short cwd, swd;
965
966 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700967 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100968 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return;
970
971 /*
972 * Save the info for the exception handler and clear the error.
973 */
974 task = current;
975 save_init_fpu(task);
976 task->thread.trap_no = 16;
977 task->thread.error_code = 0;
978 info.si_signo = SIGFPE;
979 info.si_errno = 0;
980 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100981 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * (~cwd & swd) will mask out exceptions that are not set to unmasked
984 * status. 0x3f is the exception bits in these regs, 0x200 is the
985 * C1 reg you need in case of a stack fault, 0x040 is the stack
986 * fault bit. We should only be taking one exception at a time,
987 * so if this combination doesn't produce any single exception,
988 * then we have a bad program that isn't synchronizing its FPU usage
989 * and it will suffer the consequences since we won't be able to
990 * fully reproduce the context of the exception
991 */
992 cwd = get_fpu_cwd(task);
993 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200994 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 case 0x000:
996 default:
997 break;
998 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200999 /*
1000 * swd & 0x240 == 0x040: Stack Underflow
1001 * swd & 0x240 == 0x240: Stack Overflow
1002 * User must clear the SF bit (0x40) if set
1003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 info.si_code = FPE_FLTINV;
1005 break;
1006 case 0x002: /* Denormalize */
1007 case 0x010: /* Underflow */
1008 info.si_code = FPE_FLTUND;
1009 break;
1010 case 0x004: /* Zero Divide */
1011 info.si_code = FPE_FLTDIV;
1012 break;
1013 case 0x008: /* Overflow */
1014 info.si_code = FPE_FLTOVF;
1015 break;
1016 case 0x020: /* Precision */
1017 info.si_code = FPE_FLTRES;
1018 break;
1019 }
1020 force_sig_info(SIGFPE, &info, task);
1021}
1022
1023asmlinkage void bad_intr(void)
1024{
1025 printk("bad interrupt");
1026}
1027
1028asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1029{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001030 void __user *ip = (void __user *)(regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct task_struct * task;
1032 siginfo_t info;
1033 unsigned short mxcsr;
1034
1035 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001036 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001037 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
1039
1040 /*
1041 * Save the info for the exception handler and clear the error.
1042 */
1043 task = current;
1044 save_init_fpu(task);
1045 task->thread.trap_no = 19;
1046 task->thread.error_code = 0;
1047 info.si_signo = SIGFPE;
1048 info.si_errno = 0;
1049 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001050 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
1052 * The SIMD FPU exceptions are handled a little differently, as there
1053 * is only a single status/control register. Thus, to determine which
1054 * unmasked exception was caught we must mask the exception mask bits
1055 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1056 */
1057 mxcsr = get_fpu_mxcsr(task);
1058 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1059 case 0x000:
1060 default:
1061 break;
1062 case 0x001: /* Invalid Op */
1063 info.si_code = FPE_FLTINV;
1064 break;
1065 case 0x002: /* Denormalize */
1066 case 0x010: /* Underflow */
1067 info.si_code = FPE_FLTUND;
1068 break;
1069 case 0x004: /* Zero Divide */
1070 info.si_code = FPE_FLTDIV;
1071 break;
1072 case 0x008: /* Overflow */
1073 info.si_code = FPE_FLTOVF;
1074 break;
1075 case 0x020: /* Precision */
1076 info.si_code = FPE_FLTRES;
1077 break;
1078 }
1079 force_sig_info(SIGFPE, &info, task);
1080}
1081
1082asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1083{
1084}
1085
1086asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1087{
1088}
1089
Jacob Shin89b831e2005-11-05 17:25:53 +01001090asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1091{
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/*
1095 * 'math_state_restore()' saves the current math information in the
1096 * old math state array, and gets the new ones from the current task
1097 *
1098 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1099 * Don't touch unless you *really* know how it works.
1100 */
1101asmlinkage void math_state_restore(void)
1102{
1103 struct task_struct *me = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Suresh Siddhaaa283f42008-03-10 15:28:05 -07001105 if (!used_math()) {
1106 local_irq_enable();
1107 /*
1108 * does a slab alloc which can sleep
1109 */
1110 if (init_fpu(me)) {
1111 /*
1112 * ran out of memory!
1113 */
1114 do_group_exit(SIGKILL);
1115 return;
1116 }
1117 local_irq_disable();
1118 }
1119
1120 clts(); /* Allow maths ops (or we recurse) */
Suresh Siddha61c46282008-03-10 15:28:04 -07001121 restore_fpu_checking(&me->thread.xstate->fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001122 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001123 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
Glauber de Oliveira Costa21db5582008-01-30 13:31:10 +01001125EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127void __init trap_init(void)
1128{
1129 set_intr_gate(0,&divide_error);
1130 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1131 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001132 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001133 set_system_gate(4,&overflow); /* int4 can be called from all */
1134 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 set_intr_gate(6,&invalid_op);
1136 set_intr_gate(7,&device_not_available);
1137 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1138 set_intr_gate(9,&coprocessor_segment_overrun);
1139 set_intr_gate(10,&invalid_TSS);
1140 set_intr_gate(11,&segment_not_present);
1141 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1142 set_intr_gate(13,&general_protection);
1143 set_intr_gate(14,&page_fault);
1144 set_intr_gate(15,&spurious_interrupt_bug);
1145 set_intr_gate(16,&coprocessor_error);
1146 set_intr_gate(17,&alignment_check);
1147#ifdef CONFIG_X86_MCE
1148 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1149#endif
1150 set_intr_gate(19,&simd_coprocessor_error);
1151
1152#ifdef CONFIG_IA32_EMULATION
1153 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1154#endif
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
Suresh Siddha61c46282008-03-10 15:28:04 -07001157 * initialize the per thread extended state:
1158 */
1159 init_thread_xstate();
1160 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * Should be a barrier for any external CPU state.
1162 */
1163 cpu_init();
1164}
1165
1166
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001167static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001169 if (!s)
1170 return -EINVAL;
1171 if (!strcmp(s, "panic"))
1172 panic_on_oops = 1;
1173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001175early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177static int __init kstack_setup(char *s)
1178{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001179 if (!s)
1180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001184early_param("kstack", kstack_setup);
Arjan van de Vena25bd942008-01-30 13:33:08 +01001185
1186
1187static int __init code_bytes_setup(char *s)
1188{
1189 code_bytes = simple_strtoul(s, NULL, 0);
1190 if (code_bytes > 8192)
1191 code_bytes = 8192;
1192
1193 return 1;
1194}
1195__setup("code_bytes=", code_bytes_setup);