blob: b54ccc07f37944dba409ac9a27761c5d5e0ce848 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86-64/traps.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
6 *
7 * Pentium III FXSR, SSE support
8 * Gareth Hughes <gareth@valinux.com>, May 2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
12 * 'Traps.c' handles hardware traps and faults after we have saved some
13 * state in 'entry.S'.
14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/string.h>
18#include <linux/errno.h>
19#include <linux/ptrace.h>
20#include <linux/timer.h>
21#include <linux/mm.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/spinlock.h>
25#include <linux/interrupt.h>
Randy Dunlap4b0ff1a2006-10-05 19:07:26 +020026#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070029#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070030#include <linux/kprobes.h>
Vivek Goyal8bcc5282006-04-18 12:35:13 +020031#include <linux/kexec.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020032#include <linux/unwind.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010033#include <linux/uaccess.h>
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -080034#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/io.h>
38#include <asm/atomic.h>
39#include <asm/debugreg.h>
40#include <asm/desc.h>
41#include <asm/i387.h>
42#include <asm/kdebug.h>
43#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020044#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/smp.h>
46#include <asm/pgalloc.h>
47#include <asm/pda.h>
48#include <asm/proto.h>
49#include <asm/nmi.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020050#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052asmlinkage void divide_error(void);
53asmlinkage void debug(void);
54asmlinkage void nmi(void);
55asmlinkage void int3(void);
56asmlinkage void overflow(void);
57asmlinkage void bounds(void);
58asmlinkage void invalid_op(void);
59asmlinkage void device_not_available(void);
60asmlinkage void double_fault(void);
61asmlinkage void coprocessor_segment_overrun(void);
62asmlinkage void invalid_TSS(void);
63asmlinkage void segment_not_present(void);
64asmlinkage void stack_segment(void);
65asmlinkage void general_protection(void);
66asmlinkage void page_fault(void);
67asmlinkage void coprocessor_error(void);
68asmlinkage void simd_coprocessor_error(void);
69asmlinkage void reserved(void);
70asmlinkage void alignment_check(void);
71asmlinkage void machine_check(void);
72asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alan Sterne041c682006-03-27 01:16:30 -080074ATOMIC_NOTIFIER_HEAD(die_chain);
Andi Kleen2ee60e172006-06-26 13:59:44 +020075EXPORT_SYMBOL(die_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77int register_die_notifier(struct notifier_block *nb)
78{
Jan Beulich8c914cb2006-03-25 16:29:40 +010079 vmalloc_sync_all();
Alan Sterne041c682006-03-27 01:16:30 -080080 return atomic_notifier_chain_register(&die_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Arjan van de Ven1454aed2006-07-10 04:44:05 -070082EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
Alan Sterne041c682006-03-27 01:16:30 -080083
84int unregister_die_notifier(struct notifier_block *nb)
85{
86 return atomic_notifier_chain_unregister(&die_chain, nb);
87}
Arjan van de Ven1454aed2006-07-10 04:44:05 -070088EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static inline void conditional_sti(struct pt_regs *regs)
91{
92 if (regs->eflags & X86_EFLAGS_IF)
93 local_irq_enable();
94}
95
John Blackwooda65d17c2006-02-12 14:34:58 -080096static inline void preempt_conditional_sti(struct pt_regs *regs)
97{
98 preempt_disable();
99 if (regs->eflags & X86_EFLAGS_IF)
100 local_irq_enable();
101}
102
103static inline void preempt_conditional_cli(struct pt_regs *regs)
104{
105 if (regs->eflags & X86_EFLAGS_IF)
106 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +0200107 /* Make sure to not schedule here because we could be running
108 on an exception stack. */
John Blackwooda65d17c2006-02-12 14:34:58 -0800109 preempt_enable_no_resched();
110}
111
Chuck Ebbert0741f4d2006-12-07 02:14:11 +0100112int kstack_depth_to_print = 12;
Jan Beulichea424052006-08-30 19:37:11 +0200113#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200114static int call_trace = 1;
Jan Beulichea424052006-08-30 19:37:11 +0200115#else
116#define call_trace (-1)
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119#ifdef CONFIG_KALLSYMS
Ingo Molnar3ac94932006-07-03 00:24:36 -0700120void printk_address(unsigned long address)
121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned long offset = 0, symsize;
123 const char *symname;
124 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700125 char *delim = ":";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char namebuf[128];
127
Ingo Molnar3ac94932006-07-03 00:24:36 -0700128 symname = kallsyms_lookup(address, &symsize, &offset,
129 &modname, namebuf);
130 if (!symname) {
131 printk(" [<%016lx>]\n", address);
132 return;
133 }
134 if (!modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 modname = delim = "";
Ingo Molnar3ac94932006-07-03 00:24:36 -0700136 printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
137 address, delim, modname, delim, symname, offset, symsize);
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700140void printk_address(unsigned long address)
141{
142 printk(" [<%016lx>]\n", address);
143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#endif
145
Andi Kleen0a658002005-04-16 15:25:17 -0700146static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200147 unsigned *usedp, char **idp)
Andi Kleen0a658002005-04-16 15:25:17 -0700148{
Jan Beulichb556b352006-01-11 22:43:00 +0100149 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700150 [DEBUG_STACK - 1] = "#DB",
151 [NMI_STACK - 1] = "NMI",
152 [DOUBLEFAULT_STACK - 1] = "#DF",
153 [STACKFAULT_STACK - 1] = "#SS",
154 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100155#if DEBUG_STKSZ > EXCEPTION_STKSZ
156 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
157#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700158 };
159 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700161 /*
162 * Iterate over all exception stacks, and figure out whether
163 * 'stack' is in one of them:
164 */
Andi Kleen0a658002005-04-16 15:25:17 -0700165 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
Keith Owensf5741642006-09-26 10:52:38 +0200166 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700167 /*
168 * Is 'stack' above this exception frame's end?
169 * If yes then skip to the next frame.
170 */
Andi Kleen0a658002005-04-16 15:25:17 -0700171 if (stack >= end)
172 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700173 /*
174 * Is 'stack' above this exception frame's start address?
175 * If yes then we found the right frame.
176 */
Andi Kleen0a658002005-04-16 15:25:17 -0700177 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700178 /*
179 * Make sure we only iterate through an exception
180 * stack once. If it comes up for the second time
181 * then there's something wrong going on - just
182 * break out and return NULL:
183 */
Andi Kleen0a658002005-04-16 15:25:17 -0700184 if (*usedp & (1U << k))
185 break;
186 *usedp |= 1U << k;
187 *idp = ids[k];
188 return (unsigned long *)end;
189 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700190 /*
191 * If this is a debug stack, and if it has a larger size than
192 * the usual exception stacks, then 'stack' might still
193 * be within the lower portion of the debug stack:
194 */
Jan Beulichb556b352006-01-11 22:43:00 +0100195#if DEBUG_STKSZ > EXCEPTION_STKSZ
196 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
197 unsigned j = N_EXCEPTION_STACKS - 1;
198
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700199 /*
200 * Black magic. A large debug stack is composed of
201 * multiple exception stack entries, which we
202 * iterate through now. Dont look:
203 */
Jan Beulichb556b352006-01-11 22:43:00 +0100204 do {
205 ++j;
206 end -= EXCEPTION_STKSZ;
207 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
208 } while (stack < end - EXCEPTION_STKSZ);
209 if (*usedp & (1U << j))
210 break;
211 *usedp |= 1U << j;
212 *idp = ids[j];
213 return (unsigned long *)end;
214 }
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Andi Kleenc0b766f2006-09-26 10:52:34 +0200220struct ops_and_data {
221 struct stacktrace_ops *ops;
222 void *data;
223};
224
225static int dump_trace_unwind(struct unwind_frame_info *info, void *context)
Jan Beulichb538ed22006-06-26 13:57:32 +0200226{
Andi Kleenc0b766f2006-09-26 10:52:34 +0200227 struct ops_and_data *oad = (struct ops_and_data *)context;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700228 int n = 0;
Jan Beulich359ad0d2006-12-07 02:14:13 +0100229 unsigned long sp = UNW_SP(info);
Jan Beulichb538ed22006-06-26 13:57:32 +0200230
Jan Beulich359ad0d2006-12-07 02:14:13 +0100231 if (arch_unw_user_mode(info))
232 return -1;
Jan Beulichb538ed22006-06-26 13:57:32 +0200233 while (unwind(info) == 0 && UNW_PC(info)) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700234 n++;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200235 oad->ops->address(oad->data, UNW_PC(info));
Jan Beulichb538ed22006-06-26 13:57:32 +0200236 if (arch_unw_user_mode(info))
237 break;
Jan Beulich359ad0d2006-12-07 02:14:13 +0100238 if ((sp & ~(PAGE_SIZE - 1)) == (UNW_SP(info) & ~(PAGE_SIZE - 1))
239 && sp > UNW_SP(info))
240 break;
241 sp = UNW_SP(info);
Jan Beulichb538ed22006-06-26 13:57:32 +0200242 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200243 return n;
Jan Beulichb538ed22006-06-26 13:57:32 +0200244}
245
Andi Kleenb615ebd2006-12-07 02:14:00 +0100246#define MSG(txt) ops->warning(data, txt)
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * x86-64 can have upto three kernel stacks:
250 * process stack
251 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700252 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254
Andi Kleenc547c772006-11-28 20:12:59 +0100255static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
256{
257 void *t = (void *)tinfo;
258 return p > t && p < t + THREAD_SIZE - 3;
259}
260
Andi Kleenb615ebd2006-12-07 02:14:00 +0100261void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
262 unsigned long *stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +0200263 struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Andrew Mortonda689332006-12-07 02:14:02 +0100265 const unsigned cpu = get_cpu();
Andi Kleenb615ebd2006-12-07 02:14:00 +0100266 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700267 unsigned used = 0;
Andi Kleenc547c772006-11-28 20:12:59 +0100268 struct thread_info *tinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Beulichb538ed22006-06-26 13:57:32 +0200270 if (!tsk)
271 tsk = current;
272
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200273 if (call_trace >= 0) {
274 int unw_ret = 0;
275 struct unwind_frame_info info;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200276 struct ops_and_data oad = { .ops = ops, .data = data };
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200277
278 if (regs) {
279 if (unwind_init_frame_info(&info, tsk, regs) == 0)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200280 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200281 } else if (tsk == current)
Andi Kleenb615ebd2006-12-07 02:14:00 +0100282 unw_ret = unwind_init_running(&info, dump_trace_unwind,
283 &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200284 else {
285 if (unwind_init_blocked(&info, tsk) == 0)
Andi Kleenc0b766f2006-09-26 10:52:34 +0200286 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulichb538ed22006-06-26 13:57:32 +0200287 }
Jan Beulichea424052006-08-30 19:37:11 +0200288 if (unw_ret > 0) {
289 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
Andi Kleenb615ebd2006-12-07 02:14:00 +0100290 ops->warning_symbol(data,
Andi Kleendd315df2006-12-07 02:14:00 +0100291 "DWARF2 unwinder stuck at %s",
Jan Beulichea424052006-08-30 19:37:11 +0200292 UNW_PC(&info));
293 if ((long)UNW_SP(&info) < 0) {
Andi Kleenb615ebd2006-12-07 02:14:00 +0100294 MSG("Leftover inexact backtrace:");
Jan Beulichea424052006-08-30 19:37:11 +0200295 stack = (unsigned long *)UNW_SP(&info);
Andi Kleenbe7a9172006-09-26 10:52:34 +0200296 if (!stack)
Andrew Mortonda689332006-12-07 02:14:02 +0100297 goto out;
Jan Beulichea424052006-08-30 19:37:11 +0200298 } else
Andi Kleendd315df2006-12-07 02:14:00 +0100299 MSG("Full inexact backtrace again:");
Jan Beulichea424052006-08-30 19:37:11 +0200300 } else if (call_trace >= 1)
Andrew Mortonda689332006-12-07 02:14:02 +0100301 goto out;
Andi Kleenb13761e2006-07-28 14:44:51 +0200302 else
Andi Kleendd315df2006-12-07 02:14:00 +0100303 MSG("Full inexact backtrace again:");
Jan Beulichea424052006-08-30 19:37:11 +0200304 } else
Andi Kleendd315df2006-12-07 02:14:00 +0100305 MSG("Inexact backtrace:");
Andi Kleenc0b766f2006-09-26 10:52:34 +0200306 }
307 if (!stack) {
308 unsigned long dummy;
309 stack = &dummy;
310 if (tsk && tsk != current)
311 stack = (unsigned long *)tsk->thread.rsp;
Jan Beulichb538ed22006-06-26 13:57:32 +0200312 }
313
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700314 /*
315 * Print function call entries within a stack. 'cond' is the
316 * "end of stackframe" condition, that the 'stack++'
317 * iteration will eventually trigger.
318 */
Andi Kleen0a658002005-04-16 15:25:17 -0700319#define HANDLE_STACK(cond) \
320 do while (cond) { \
Jan Beulich1b2f6302006-01-11 22:46:45 +0100321 unsigned long addr = *stack++; \
Andi Kleen446f7132006-12-07 02:14:12 +0100322 /* Use unlocked access here because except for NMIs \
323 we should be already protected against module unloads */ \
324 if (__kernel_text_address(addr)) { \
Andi Kleen0a658002005-04-16 15:25:17 -0700325 /* \
326 * If the address is either in the text segment of the \
327 * kernel, or in the region which contains vmalloc'ed \
328 * memory, it *may* be the address of a calling \
329 * routine; if so, print it so that someone tracing \
330 * down the cause of the crash will be able to figure \
331 * out the call path that was taken. \
332 */ \
Andi Kleenc0b766f2006-09-26 10:52:34 +0200333 ops->address(data, addr); \
Andi Kleen0a658002005-04-16 15:25:17 -0700334 } \
335 } while (0)
336
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700337 /*
338 * Print function call entries in all stacks, starting at the
339 * current stack address. If the stacks consist of nested
340 * exceptions
341 */
Andi Kleenc0b766f2006-09-26 10:52:34 +0200342 for (;;) {
343 char *id;
Andi Kleen0a658002005-04-16 15:25:17 -0700344 unsigned long *estack_end;
345 estack_end = in_exception_stack(cpu, (unsigned long)stack,
346 &used, &id);
347
348 if (estack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200349 if (ops->stack(data, id) < 0)
350 break;
Andi Kleen0a658002005-04-16 15:25:17 -0700351 HANDLE_STACK (stack < estack_end);
Andi Kleenc0b766f2006-09-26 10:52:34 +0200352 ops->stack(data, "<EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700353 /*
354 * We link to the next stack via the
355 * second-to-last pointer (index -2 to end) in the
356 * exception stack:
357 */
Andi Kleen0a658002005-04-16 15:25:17 -0700358 stack = (unsigned long *) estack_end[-2];
359 continue;
360 }
361 if (irqstack_end) {
362 unsigned long *irqstack;
363 irqstack = irqstack_end -
364 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
365
366 if (stack >= irqstack && stack < irqstack_end) {
Andi Kleenc0b766f2006-09-26 10:52:34 +0200367 if (ops->stack(data, "IRQ") < 0)
368 break;
Andi Kleen0a658002005-04-16 15:25:17 -0700369 HANDLE_STACK (stack < irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700370 /*
371 * We link to the next stack (which would be
372 * the process stack normally) the last
373 * pointer (index -1 to end) in the IRQ stack:
374 */
Andi Kleen0a658002005-04-16 15:25:17 -0700375 stack = (unsigned long *) (irqstack_end[-1]);
376 irqstack_end = NULL;
Andi Kleenc0b766f2006-09-26 10:52:34 +0200377 ops->stack(data, "EOI");
Andi Kleen0a658002005-04-16 15:25:17 -0700378 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 }
Andi Kleen0a658002005-04-16 15:25:17 -0700381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Andi Kleen0a658002005-04-16 15:25:17 -0700383
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700384 /*
Andi Kleenc0b766f2006-09-26 10:52:34 +0200385 * This handles the process stack:
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700386 */
Andi Kleenc547c772006-11-28 20:12:59 +0100387 tinfo = current_thread_info();
388 HANDLE_STACK (valid_stack_ptr(tinfo, stack));
Andi Kleen0a658002005-04-16 15:25:17 -0700389#undef HANDLE_STACK
Andrew Mortonda689332006-12-07 02:14:02 +0100390out:
391 put_cpu();
Andi Kleenc0b766f2006-09-26 10:52:34 +0200392}
393EXPORT_SYMBOL(dump_trace);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700394
Andi Kleenc0b766f2006-09-26 10:52:34 +0200395static void
396print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
397{
398 print_symbol(msg, symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 printk("\n");
400}
401
Andi Kleenc0b766f2006-09-26 10:52:34 +0200402static void print_trace_warning(void *data, char *msg)
403{
404 printk("%s\n", msg);
405}
406
407static int print_trace_stack(void *data, char *name)
408{
409 printk(" <%s> ", name);
410 return 0;
411}
412
413static void print_trace_address(void *data, unsigned long addr)
414{
415 printk_address(addr);
416}
417
418static struct stacktrace_ops print_trace_ops = {
419 .warning = print_trace_warning,
420 .warning_symbol = print_trace_warning_symbol,
421 .stack = print_trace_stack,
422 .address = print_trace_address,
423};
424
425void
426show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
427{
428 printk("\nCall Trace:\n");
429 dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
430 printk("\n");
431}
432
433static void
434_show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned long *stack;
437 int i;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200438 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100439 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
440 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 // debugging aid: "show_stack(NULL, NULL);" prints the
443 // back trace for this cpu.
444
445 if (rsp == NULL) {
446 if (tsk)
447 rsp = (unsigned long *)tsk->thread.rsp;
448 else
449 rsp = (unsigned long *)&rsp;
450 }
451
452 stack = rsp;
453 for(i=0; i < kstack_depth_to_print; i++) {
454 if (stack >= irqstack && stack <= irqstack_end) {
455 if (stack == irqstack_end) {
456 stack = (unsigned long *) (irqstack_end[-1]);
457 printk(" <EOI> ");
458 }
459 } else {
460 if (((long) stack & (THREAD_SIZE-1)) == 0)
461 break;
462 }
463 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700464 printk("\n");
465 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700466 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Jan Beulichb538ed22006-06-26 13:57:32 +0200468 show_trace(tsk, regs, rsp);
469}
470
471void show_stack(struct task_struct *tsk, unsigned long * rsp)
472{
473 _show_stack(tsk, NULL, rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
477 * The architecture-independent dump_stack generator
478 */
479void dump_stack(void)
480{
481 unsigned long dummy;
Jan Beulichb538ed22006-06-26 13:57:32 +0200482 show_trace(NULL, NULL, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485EXPORT_SYMBOL(dump_stack);
486
487void show_registers(struct pt_regs *regs)
488{
489 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700490 int in_kernel = !user_mode(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned long rsp;
Andi Kleen151f8cc2006-09-26 10:52:37 +0200492 const int cpu = smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100493 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 rsp = regs->rsp;
496
497 printk("CPU %d ", cpu);
498 __show_regs(regs);
499 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800500 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * When in-kernel, we also print out the stack and code at the
504 * time of the fault..
505 */
506 if (in_kernel) {
507
508 printk("Stack: ");
Jan Beulichb538ed22006-06-26 13:57:32 +0200509 _show_stack(NULL, regs, (unsigned long*)rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 printk("\nCode: ");
Roberto Nibali2b692a82006-03-25 16:29:55 +0100512 if (regs->rip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto bad;
514
Roberto Nibali2b692a82006-03-25 16:29:55 +0100515 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned char c;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100517 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518bad:
519 printk(" Bad RIP value.");
520 break;
521 }
522 printk("%02x ", c);
523 }
524 }
525 printk("\n");
526}
527
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800528int is_valid_bugaddr(unsigned long rip)
529{
530 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800532 if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
533 return 0;
534
535 return ud2 == 0x0b0f;
536}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700538#ifdef CONFIG_BUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539void out_of_line_bug(void)
540{
541 BUG();
542}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200543EXPORT_SYMBOL(out_of_line_bug);
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546static DEFINE_SPINLOCK(die_lock);
547static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200548static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100550unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Andi Kleen151f8cc2006-09-26 10:52:37 +0200552 int cpu = smp_processor_id();
Jan Beulich12091402005-09-12 18:49:24 +0200553 unsigned long flags;
554
Andrew Mortonabf0f102006-09-26 10:52:36 +0200555 oops_enter();
556
Jan Beulich12091402005-09-12 18:49:24 +0200557 /* racy, but better than risking deadlock. */
558 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!spin_trylock(&die_lock)) {
560 if (cpu == die_owner)
561 /* nested oops. should stop eventually */;
562 else
Jan Beulich12091402005-09-12 18:49:24 +0200563 spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200565 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200566 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200568 bust_spinlocks(1);
569 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100572void __kprobes oops_end(unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200575 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200576 die_nest_count--;
577 if (die_nest_count)
578 /* We still own the lock */
579 local_irq_restore(flags);
580 else
581 /* Nest count reaches zero, release the lock. */
582 spin_unlock_irqrestore(&die_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700584 panic("Fatal exception");
Andrew Mortonabf0f102006-09-26 10:52:36 +0200585 oops_exit();
Jan Beulich12091402005-09-12 18:49:24 +0200586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100588void __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 static int die_counter;
591 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
592#ifdef CONFIG_PREEMPT
593 printk("PREEMPT ");
594#endif
595#ifdef CONFIG_SMP
596 printk("SMP ");
597#endif
598#ifdef CONFIG_DEBUG_PAGEALLOC
599 printk("DEBUG_PAGEALLOC");
600#endif
601 printk("\n");
Jan Beulich6e3f3612006-01-11 22:42:14 +0100602 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 show_registers(regs);
604 /* Executive summary in case the oops scrolled away */
605 printk(KERN_ALERT "RIP ");
606 printk_address(regs->rip);
607 printk(" RSP <%016lx>\n", regs->rsp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200608 if (kexec_should_crash(current))
609 crash_kexec(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612void die(const char * str, struct pt_regs * regs, long err)
613{
Jan Beulich12091402005-09-12 18:49:24 +0200614 unsigned long flags = oops_begin();
615
Jeremy Fitzhardingec31a0bf2006-12-08 02:36:22 -0800616 if (!user_mode(regs))
617 report_bug(regs->rip);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 __die(str, regs, err);
Jan Beulich12091402005-09-12 18:49:24 +0200620 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 do_exit(SIGSEGV);
622}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Andi Kleenfac58552006-09-26 10:52:27 +0200624void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Jan Beulich12091402005-09-12 18:49:24 +0200626 unsigned long flags = oops_begin();
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * We are in trouble anyway, lets at least try
630 * to get a message out.
631 */
Andi Kleen151f8cc2006-09-26 10:52:37 +0200632 printk(str, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200634 if (kexec_should_crash(current))
635 crash_kexec(regs);
Andi Kleenfac58552006-09-26 10:52:27 +0200636 if (do_panic || panic_on_oops)
637 panic("Non maskable interrupt");
Jan Beulich12091402005-09-12 18:49:24 +0200638 oops_end(flags);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200639 nmi_exit();
640 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 do_exit(SIGSEGV);
642}
643
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700644static void __kprobes do_trap(int trapnr, int signr, char *str,
645 struct pt_regs * regs, long error_code,
646 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100648 struct task_struct *tsk = current;
649
Jan Beulich6e3f3612006-01-11 22:42:14 +0100650 tsk->thread.error_code = error_code;
651 tsk->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jan Beulich6e3f3612006-01-11 22:42:14 +0100653 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (exception_trace && unhandled_signal(tsk, signr))
655 printk(KERN_INFO
656 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
657 tsk->comm, tsk->pid, str,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100658 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (info)
661 force_sig_info(signr, info, tsk);
662 else
663 force_sig(signr, tsk);
664 return;
665 }
666
667
668 /* kernel trap */
669 {
670 const struct exception_table_entry *fixup;
671 fixup = search_exception_tables(regs->rip);
Roberto Nibali2b692a82006-03-25 16:29:55 +0100672 if (fixup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 regs->rip = fixup->fixup;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100674 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 die(str, regs, error_code);
676 return;
677 }
678}
679
680#define DO_ERROR(trapnr, signr, str, name) \
681asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
682{ \
683 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
684 == NOTIFY_STOP) \
685 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200686 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 do_trap(trapnr, signr, str, regs, error_code, NULL); \
688}
689
690#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
691asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
692{ \
693 siginfo_t info; \
694 info.si_signo = signr; \
695 info.si_errno = 0; \
696 info.si_code = sicode; \
697 info.si_addr = (void __user *)siaddr; \
698 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
699 == NOTIFY_STOP) \
700 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200701 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 do_trap(trapnr, signr, str, regs, error_code, &info); \
703}
704
705DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
706DO_ERROR( 4, SIGSEGV, "overflow", overflow)
707DO_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert100c0e32006-01-11 22:46:00 +0100708DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
710DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
711DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
712DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
713DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
714DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200715
716/* Runs on IST stack */
717asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
718{
719 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
720 12, SIGBUS) == NOTIFY_STOP)
721 return;
722 preempt_conditional_sti(regs);
723 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
724 preempt_conditional_cli(regs);
725}
Jan Beulicheca37c12006-01-11 22:42:17 +0100726
727asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
728{
729 static const char str[] = "double fault";
730 struct task_struct *tsk = current;
731
732 /* Return not checked because double check cannot be ignored */
733 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
734
735 tsk->thread.error_code = error_code;
736 tsk->thread.trap_no = 8;
737
738 /* This is always a kernel trap and never fixable (and thus must
739 never return). */
740 for (;;)
741 die(str, regs, error_code);
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700744asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
745 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100747 struct task_struct *tsk = current;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 conditional_sti(regs);
750
Jan Beulich6e3f3612006-01-11 22:42:14 +0100751 tsk->thread.error_code = error_code;
752 tsk->thread.trap_no = 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Jan Beulich6e3f3612006-01-11 22:42:14 +0100754 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
756 printk(KERN_INFO
757 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
758 tsk->comm, tsk->pid,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100759 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 force_sig(SIGSEGV, tsk);
762 return;
763 }
764
765 /* kernel gp */
766 {
767 const struct exception_table_entry *fixup;
768 fixup = search_exception_tables(regs->rip);
769 if (fixup) {
770 regs->rip = fixup->fixup;
771 return;
772 }
773 if (notify_die(DIE_GPF, "general protection fault", regs,
774 error_code, 13, SIGSEGV) == NOTIFY_STOP)
775 return;
776 die("general protection fault", regs, error_code);
777 }
778}
779
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100780static __kprobes void
781mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200783 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
784 reason);
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100785 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickusc41c5cd2006-09-26 10:52:27 +0200786
Don Zickus8da5add2006-09-26 10:52:27 +0200787 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200788 panic("NMI: Not continuing");
789
790 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* Clear and disable the memory parity error line. */
793 reason = (reason & 0xf) | 4;
794 outb(reason, 0x61);
795}
796
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100797static __kprobes void
798io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 printk("NMI: IOCK error (debug interrupt?)\n");
801 show_registers(regs);
802
803 /* Re-enable the IOCK line, wait for a few seconds */
804 reason = (reason & 0xf) | 8;
805 outb(reason, 0x61);
806 mdelay(2000);
807 reason &= ~8;
808 outb(reason, 0x61);
809}
810
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100811static __kprobes void
812unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200813{
814 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
815 reason);
816 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200817
818 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200819 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200820
Don Zickusc41c5cd2006-09-26 10:52:27 +0200821 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700824/* Runs on IST stack. This code must keep interrupts off all the time.
825 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100826asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700829 int cpu;
830
831 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700834 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 reason = get_nmi_reason();
836
837 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100838 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 == NOTIFY_STOP)
840 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * Ok, so this is none of the documented NMI sources,
843 * so it must be the NMI watchdog.
844 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200845 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200847 if (!do_nmi_callback(regs,cpu))
Don Zickus3adbbcce2006-09-26 10:52:26 +0200848 unknown_nmi_error(reason, regs);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return;
851 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100852 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return;
854
855 /* AK: following checks seem to be broken on modern chipsets. FIXME */
856
857 if (reason & 0x80)
858 mem_parity_error(reason, regs);
859 if (reason & 0x40)
860 io_check_error(reason, regs);
861}
862
Jan Beulichb556b352006-01-11 22:43:00 +0100863/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700864asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
867 return;
868 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200869 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200871 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700874/* Help handler running on IST stack to switch back to user stack
875 for scheduling or signal handling. The actual stack switch is done in
876 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100877asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700879 struct pt_regs *regs = eregs;
880 /* Did already sync */
881 if (eregs == (struct pt_regs *)eregs->rsp)
882 ;
883 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700884 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800885 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700886 /* Exception from kernel and interrupts are enabled. Move to
887 kernel process stack. */
888 else if (eregs->eflags & X86_EFLAGS_IF)
889 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
890 if (eregs != regs)
891 *regs = *eregs;
892 return regs;
893}
894
895/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700896asmlinkage void __kprobes do_debug(struct pt_regs * regs,
897 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700898{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long condition;
900 struct task_struct *tsk = current;
901 siginfo_t info;
902
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700903 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700906 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700907 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700908
John Blackwooda65d17c2006-02-12 14:34:58 -0800909 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /* Mask out spurious debug traps due to lazy DR7 setting */
912 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
913 if (!tsk->thread.debugreg7) {
914 goto clear_dr7;
915 }
916 }
917
918 tsk->thread.debugreg6 = condition;
919
920 /* Mask out spurious TF errors due to lazy TF clearing */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700921 if (condition & DR_STEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /*
923 * The TF error should be masked out only if the current
924 * process is not traced and if the TRAP flag has been set
925 * previously by a tracing process (condition detected by
926 * the PT_DTRACE flag); remember that the i386 TRAP flag
927 * can be modified by the process itself in user mode,
928 * allowing programs to debug themselves without the ptrace()
929 * interface.
930 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700931 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto clear_TF_reenable;
Andi Kleenbe61bff2005-04-16 15:24:57 -0700933 /*
934 * Was the TF flag set by a debugger? If so, clear it now,
935 * so that register information is correct.
936 */
937 if (tsk->ptrace & PT_DTRACE) {
938 regs->eflags &= ~TF_MASK;
939 tsk->ptrace &= ~PT_DTRACE;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 /* Ok, finally something we can handle */
944 tsk->thread.trap_no = 1;
945 tsk->thread.error_code = error_code;
946 info.si_signo = SIGTRAP;
947 info.si_errno = 0;
948 info.si_code = TRAP_BRKPT;
John Blackwood01b8faa2006-01-11 22:44:15 +0100949 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
950 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700953 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800954 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957clear_TF_reenable:
958 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 regs->eflags &= ~TF_MASK;
John Blackwooda65d17c2006-02-12 14:34:58 -0800960 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Jan Beulich6e3f3612006-01-11 22:42:14 +0100963static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 const struct exception_table_entry *fixup;
966 fixup = search_exception_tables(regs->rip);
967 if (fixup) {
968 regs->rip = fixup->fixup;
969 return 1;
970 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100971 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700972 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100973 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
978/*
979 * Note that we play around with the 'TS' bit in an attempt to get
980 * the correct behaviour even in the presence of the asynchronous
981 * IRQ13 behaviour
982 */
983asmlinkage void do_coprocessor_error(struct pt_regs *regs)
984{
985 void __user *rip = (void __user *)(regs->rip);
986 struct task_struct * task;
987 siginfo_t info;
988 unsigned short cwd, swd;
989
990 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700991 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100992 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return;
994
995 /*
996 * Save the info for the exception handler and clear the error.
997 */
998 task = current;
999 save_init_fpu(task);
1000 task->thread.trap_no = 16;
1001 task->thread.error_code = 0;
1002 info.si_signo = SIGFPE;
1003 info.si_errno = 0;
1004 info.si_code = __SI_FAULT;
1005 info.si_addr = rip;
1006 /*
1007 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1008 * status. 0x3f is the exception bits in these regs, 0x200 is the
1009 * C1 reg you need in case of a stack fault, 0x040 is the stack
1010 * fault bit. We should only be taking one exception at a time,
1011 * so if this combination doesn't produce any single exception,
1012 * then we have a bad program that isn't synchronizing its FPU usage
1013 * and it will suffer the consequences since we won't be able to
1014 * fully reproduce the context of the exception
1015 */
1016 cwd = get_fpu_cwd(task);
1017 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +02001018 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 case 0x000:
1020 default:
1021 break;
1022 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +02001023 /*
1024 * swd & 0x240 == 0x040: Stack Underflow
1025 * swd & 0x240 == 0x240: Stack Overflow
1026 * User must clear the SF bit (0x40) if set
1027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 info.si_code = FPE_FLTINV;
1029 break;
1030 case 0x002: /* Denormalize */
1031 case 0x010: /* Underflow */
1032 info.si_code = FPE_FLTUND;
1033 break;
1034 case 0x004: /* Zero Divide */
1035 info.si_code = FPE_FLTDIV;
1036 break;
1037 case 0x008: /* Overflow */
1038 info.si_code = FPE_FLTOVF;
1039 break;
1040 case 0x020: /* Precision */
1041 info.si_code = FPE_FLTRES;
1042 break;
1043 }
1044 force_sig_info(SIGFPE, &info, task);
1045}
1046
1047asmlinkage void bad_intr(void)
1048{
1049 printk("bad interrupt");
1050}
1051
1052asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1053{
1054 void __user *rip = (void __user *)(regs->rip);
1055 struct task_struct * task;
1056 siginfo_t info;
1057 unsigned short mxcsr;
1058
1059 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001060 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001061 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return;
1063
1064 /*
1065 * Save the info for the exception handler and clear the error.
1066 */
1067 task = current;
1068 save_init_fpu(task);
1069 task->thread.trap_no = 19;
1070 task->thread.error_code = 0;
1071 info.si_signo = SIGFPE;
1072 info.si_errno = 0;
1073 info.si_code = __SI_FAULT;
1074 info.si_addr = rip;
1075 /*
1076 * The SIMD FPU exceptions are handled a little differently, as there
1077 * is only a single status/control register. Thus, to determine which
1078 * unmasked exception was caught we must mask the exception mask bits
1079 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1080 */
1081 mxcsr = get_fpu_mxcsr(task);
1082 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1083 case 0x000:
1084 default:
1085 break;
1086 case 0x001: /* Invalid Op */
1087 info.si_code = FPE_FLTINV;
1088 break;
1089 case 0x002: /* Denormalize */
1090 case 0x010: /* Underflow */
1091 info.si_code = FPE_FLTUND;
1092 break;
1093 case 0x004: /* Zero Divide */
1094 info.si_code = FPE_FLTDIV;
1095 break;
1096 case 0x008: /* Overflow */
1097 info.si_code = FPE_FLTOVF;
1098 break;
1099 case 0x020: /* Precision */
1100 info.si_code = FPE_FLTRES;
1101 break;
1102 }
1103 force_sig_info(SIGFPE, &info, task);
1104}
1105
1106asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1107{
1108}
1109
1110asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1111{
1112}
1113
Jacob Shin89b831e2005-11-05 17:25:53 +01001114asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1115{
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * 'math_state_restore()' saves the current math information in the
1120 * old math state array, and gets the new ones from the current task
1121 *
1122 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1123 * Don't touch unless you *really* know how it works.
1124 */
1125asmlinkage void math_state_restore(void)
1126{
1127 struct task_struct *me = current;
1128 clts(); /* Allow maths ops (or we recurse) */
1129
1130 if (!used_math())
1131 init_fpu(me);
1132 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001133 task_thread_info(me)->status |= TS_USEDFPU;
Arjan van de Vene07e23e2006-09-26 10:52:36 +02001134 me->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137void __init trap_init(void)
1138{
1139 set_intr_gate(0,&divide_error);
1140 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1141 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001142 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001143 set_system_gate(4,&overflow); /* int4 can be called from all */
1144 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 set_intr_gate(6,&invalid_op);
1146 set_intr_gate(7,&device_not_available);
1147 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1148 set_intr_gate(9,&coprocessor_segment_overrun);
1149 set_intr_gate(10,&invalid_TSS);
1150 set_intr_gate(11,&segment_not_present);
1151 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1152 set_intr_gate(13,&general_protection);
1153 set_intr_gate(14,&page_fault);
1154 set_intr_gate(15,&spurious_interrupt_bug);
1155 set_intr_gate(16,&coprocessor_error);
1156 set_intr_gate(17,&alignment_check);
1157#ifdef CONFIG_X86_MCE
1158 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1159#endif
1160 set_intr_gate(19,&simd_coprocessor_error);
1161
1162#ifdef CONFIG_IA32_EMULATION
1163 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1164#endif
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /*
1167 * Should be a barrier for any external CPU state.
1168 */
1169 cpu_init();
1170}
1171
1172
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001173static int __init oops_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001175 if (!s)
1176 return -EINVAL;
1177 if (!strcmp(s, "panic"))
1178 panic_on_oops = 1;
1179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001181early_param("oops", oops_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183static int __init kstack_setup(char *s)
1184{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001185 if (!s)
1186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 kstack_depth_to_print = simple_strtoul(s,NULL,0);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001190early_param("kstack", kstack_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jan Beulichea424052006-08-30 19:37:11 +02001192#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001193static int __init call_trace_setup(char *s)
1194{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001195 if (!s)
1196 return -EINVAL;
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001197 if (strcmp(s, "old") == 0)
1198 call_trace = -1;
1199 else if (strcmp(s, "both") == 0)
1200 call_trace = 0;
Andi Kleenb13761e2006-07-28 14:44:51 +02001201 else if (strcmp(s, "newfallback") == 0)
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001202 call_trace = 1;
Andi Kleenb13761e2006-07-28 14:44:51 +02001203 else if (strcmp(s, "new") == 0)
1204 call_trace = 2;
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001205 return 0;
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001206}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001207early_param("call_trace", call_trace_setup);
Jan Beulichea424052006-08-30 19:37:11 +02001208#endif