blob: dae10df60926008149dc8528eaa420852bff0342 [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>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070028#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070029#include <linux/kprobes.h>
Vivek Goyal8bcc5282006-04-18 12:35:13 +020030#include <linux/kexec.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020031#include <linux/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/system.h>
34#include <asm/uaccess.h>
35#include <asm/io.h>
36#include <asm/atomic.h>
37#include <asm/debugreg.h>
38#include <asm/desc.h>
39#include <asm/i387.h>
40#include <asm/kdebug.h>
41#include <asm/processor.h>
Jan Beulichb538ed22006-06-26 13:57:32 +020042#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/smp.h>
44#include <asm/pgalloc.h>
45#include <asm/pda.h>
46#include <asm/proto.h>
47#include <asm/nmi.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049asmlinkage void divide_error(void);
50asmlinkage void debug(void);
51asmlinkage void nmi(void);
52asmlinkage void int3(void);
53asmlinkage void overflow(void);
54asmlinkage void bounds(void);
55asmlinkage void invalid_op(void);
56asmlinkage void device_not_available(void);
57asmlinkage void double_fault(void);
58asmlinkage void coprocessor_segment_overrun(void);
59asmlinkage void invalid_TSS(void);
60asmlinkage void segment_not_present(void);
61asmlinkage void stack_segment(void);
62asmlinkage void general_protection(void);
63asmlinkage void page_fault(void);
64asmlinkage void coprocessor_error(void);
65asmlinkage void simd_coprocessor_error(void);
66asmlinkage void reserved(void);
67asmlinkage void alignment_check(void);
68asmlinkage void machine_check(void);
69asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Alan Sterne041c682006-03-27 01:16:30 -080071ATOMIC_NOTIFIER_HEAD(die_chain);
Andi Kleen2ee60e172006-06-26 13:59:44 +020072EXPORT_SYMBOL(die_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74int register_die_notifier(struct notifier_block *nb)
75{
Jan Beulich8c914cb2006-03-25 16:29:40 +010076 vmalloc_sync_all();
Alan Sterne041c682006-03-27 01:16:30 -080077 return atomic_notifier_chain_register(&die_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Arjan van de Ven1454aed2006-07-10 04:44:05 -070079EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
Alan Sterne041c682006-03-27 01:16:30 -080080
81int unregister_die_notifier(struct notifier_block *nb)
82{
83 return atomic_notifier_chain_unregister(&die_chain, nb);
84}
Arjan van de Ven1454aed2006-07-10 04:44:05 -070085EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static inline void conditional_sti(struct pt_regs *regs)
88{
89 if (regs->eflags & X86_EFLAGS_IF)
90 local_irq_enable();
91}
92
John Blackwooda65d17c2006-02-12 14:34:58 -080093static inline void preempt_conditional_sti(struct pt_regs *regs)
94{
95 preempt_disable();
96 if (regs->eflags & X86_EFLAGS_IF)
97 local_irq_enable();
98}
99
100static inline void preempt_conditional_cli(struct pt_regs *regs)
101{
102 if (regs->eflags & X86_EFLAGS_IF)
103 local_irq_disable();
Andi Kleen40e59a62006-05-15 18:19:47 +0200104 /* Make sure to not schedule here because we could be running
105 on an exception stack. */
John Blackwooda65d17c2006-02-12 14:34:58 -0800106 preempt_enable_no_resched();
107}
108
Jan Beulichcab093b2006-06-26 13:59:26 +0200109static int kstack_depth_to_print = 12;
Jan Beulichea424052006-08-30 19:37:11 +0200110#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200111static int call_trace = 1;
Jan Beulichea424052006-08-30 19:37:11 +0200112#else
113#define call_trace (-1)
114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#ifdef CONFIG_KALLSYMS
Ingo Molnar3ac94932006-07-03 00:24:36 -0700117# include <linux/kallsyms.h>
118void printk_address(unsigned long address)
119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned long offset = 0, symsize;
121 const char *symname;
122 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700123 char *delim = ":";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 char namebuf[128];
125
Ingo Molnar3ac94932006-07-03 00:24:36 -0700126 symname = kallsyms_lookup(address, &symsize, &offset,
127 &modname, namebuf);
128 if (!symname) {
129 printk(" [<%016lx>]\n", address);
130 return;
131 }
132 if (!modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 modname = delim = "";
Ingo Molnar3ac94932006-07-03 00:24:36 -0700134 printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
135 address, delim, modname, delim, symname, offset, symsize);
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700138void printk_address(unsigned long address)
139{
140 printk(" [<%016lx>]\n", address);
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#endif
143
Andi Kleen0a658002005-04-16 15:25:17 -0700144static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
145 unsigned *usedp, const char **idp)
146{
Jan Beulichb556b352006-01-11 22:43:00 +0100147 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700148 [DEBUG_STACK - 1] = "#DB",
149 [NMI_STACK - 1] = "NMI",
150 [DOUBLEFAULT_STACK - 1] = "#DF",
151 [STACKFAULT_STACK - 1] = "#SS",
152 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100153#if DEBUG_STKSZ > EXCEPTION_STKSZ
154 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
155#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700156 };
157 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700159 /*
160 * Iterate over all exception stacks, and figure out whether
161 * 'stack' is in one of them:
162 */
Andi Kleen0a658002005-04-16 15:25:17 -0700163 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
164 unsigned long end;
165
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700166 /*
167 * set 'end' to the end of the exception stack.
168 */
Jan Beulichb556b352006-01-11 22:43:00 +0100169 switch (k + 1) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700170 /*
171 * TODO: this block is not needed i think, because
172 * setup64.c:cpu_init() sets up t->ist[DEBUG_STACK]
173 * properly too.
174 */
Jan Beulichb556b352006-01-11 22:43:00 +0100175#if DEBUG_STKSZ > EXCEPTION_STKSZ
176 case DEBUG_STACK:
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100177 end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ;
Jan Beulichb556b352006-01-11 22:43:00 +0100178 break;
179#endif
180 default:
Keith Owens01ebb772006-08-30 19:37:19 +0200181 end = per_cpu(orig_ist, cpu).ist[k];
Jan Beulichb556b352006-01-11 22:43:00 +0100182 break;
183 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700184 /*
185 * Is 'stack' above this exception frame's end?
186 * If yes then skip to the next frame.
187 */
Andi Kleen0a658002005-04-16 15:25:17 -0700188 if (stack >= end)
189 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700190 /*
191 * Is 'stack' above this exception frame's start address?
192 * If yes then we found the right frame.
193 */
Andi Kleen0a658002005-04-16 15:25:17 -0700194 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700195 /*
196 * Make sure we only iterate through an exception
197 * stack once. If it comes up for the second time
198 * then there's something wrong going on - just
199 * break out and return NULL:
200 */
Andi Kleen0a658002005-04-16 15:25:17 -0700201 if (*usedp & (1U << k))
202 break;
203 *usedp |= 1U << k;
204 *idp = ids[k];
205 return (unsigned long *)end;
206 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700207 /*
208 * If this is a debug stack, and if it has a larger size than
209 * the usual exception stacks, then 'stack' might still
210 * be within the lower portion of the debug stack:
211 */
Jan Beulichb556b352006-01-11 22:43:00 +0100212#if DEBUG_STKSZ > EXCEPTION_STKSZ
213 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
214 unsigned j = N_EXCEPTION_STACKS - 1;
215
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700216 /*
217 * Black magic. A large debug stack is composed of
218 * multiple exception stack entries, which we
219 * iterate through now. Dont look:
220 */
Jan Beulichb556b352006-01-11 22:43:00 +0100221 do {
222 ++j;
223 end -= EXCEPTION_STKSZ;
224 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
225 } while (stack < end - EXCEPTION_STKSZ);
226 if (*usedp & (1U << j))
227 break;
228 *usedp |= 1U << j;
229 *idp = ids[j];
230 return (unsigned long *)end;
231 }
232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200237static int show_trace_unwind(struct unwind_frame_info *info, void *context)
Jan Beulichb538ed22006-06-26 13:57:32 +0200238{
Ingo Molnar3ac94932006-07-03 00:24:36 -0700239 int n = 0;
Jan Beulichb538ed22006-06-26 13:57:32 +0200240
241 while (unwind(info) == 0 && UNW_PC(info)) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700242 n++;
243 printk_address(UNW_PC(info));
Jan Beulichb538ed22006-06-26 13:57:32 +0200244 if (arch_unw_user_mode(info))
245 break;
246 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200247 return n;
Jan Beulichb538ed22006-06-26 13:57:32 +0200248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * x86-64 can have upto three kernel stacks:
252 * process stack
253 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700254 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256
Jan Beulichb538ed22006-06-26 13:57:32 +0200257void show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long * stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Andi Kleen0a658002005-04-16 15:25:17 -0700259 const unsigned cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100260 unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
Andi Kleen0a658002005-04-16 15:25:17 -0700261 unsigned used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Ingo Molnar3ac94932006-07-03 00:24:36 -0700263 printk("\nCall Trace:\n");
Andi Kleen0a658002005-04-16 15:25:17 -0700264
Jan Beulichb538ed22006-06-26 13:57:32 +0200265 if (!tsk)
266 tsk = current;
267
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200268 if (call_trace >= 0) {
269 int unw_ret = 0;
270 struct unwind_frame_info info;
271
272 if (regs) {
273 if (unwind_init_frame_info(&info, tsk, regs) == 0)
274 unw_ret = show_trace_unwind(&info, NULL);
275 } else if (tsk == current)
276 unw_ret = unwind_init_running(&info, show_trace_unwind, NULL);
277 else {
278 if (unwind_init_blocked(&info, tsk) == 0)
279 unw_ret = show_trace_unwind(&info, NULL);
Jan Beulichb538ed22006-06-26 13:57:32 +0200280 }
Jan Beulichea424052006-08-30 19:37:11 +0200281 if (unw_ret > 0) {
282 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
283 print_symbol("DWARF2 unwinder stuck at %s\n",
284 UNW_PC(&info));
285 if ((long)UNW_SP(&info) < 0) {
286 printk("Leftover inexact backtrace:\n");
287 stack = (unsigned long *)UNW_SP(&info);
288 } else
289 printk("Full inexact backtrace again:\n");
290 } else if (call_trace >= 1)
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200291 return;
Andi Kleenb13761e2006-07-28 14:44:51 +0200292 else
293 printk("Full inexact backtrace again:\n");
Jan Beulichea424052006-08-30 19:37:11 +0200294 } else
Andi Kleenb13761e2006-07-28 14:44:51 +0200295 printk("Inexact backtrace:\n");
Jan Beulichb538ed22006-06-26 13:57:32 +0200296 }
297
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700298 /*
299 * Print function call entries within a stack. 'cond' is the
300 * "end of stackframe" condition, that the 'stack++'
301 * iteration will eventually trigger.
302 */
Andi Kleen0a658002005-04-16 15:25:17 -0700303#define HANDLE_STACK(cond) \
304 do while (cond) { \
Jan Beulich1b2f6302006-01-11 22:46:45 +0100305 unsigned long addr = *stack++; \
Andi Kleen0a658002005-04-16 15:25:17 -0700306 if (kernel_text_address(addr)) { \
307 /* \
308 * If the address is either in the text segment of the \
309 * kernel, or in the region which contains vmalloc'ed \
310 * memory, it *may* be the address of a calling \
311 * routine; if so, print it so that someone tracing \
312 * down the cause of the crash will be able to figure \
313 * out the call path that was taken. \
314 */ \
Ingo Molnar3ac94932006-07-03 00:24:36 -0700315 printk_address(addr); \
Andi Kleen0a658002005-04-16 15:25:17 -0700316 } \
317 } while (0)
318
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700319 /*
320 * Print function call entries in all stacks, starting at the
321 * current stack address. If the stacks consist of nested
322 * exceptions
323 */
324 for ( ; ; ) {
Andi Kleen0a658002005-04-16 15:25:17 -0700325 const char *id;
326 unsigned long *estack_end;
327 estack_end = in_exception_stack(cpu, (unsigned long)stack,
328 &used, &id);
329
330 if (estack_end) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700331 printk(" <%s>", id);
Andi Kleen0a658002005-04-16 15:25:17 -0700332 HANDLE_STACK (stack < estack_end);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700333 printk(" <EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700334 /*
335 * We link to the next stack via the
336 * second-to-last pointer (index -2 to end) in the
337 * exception stack:
338 */
Andi Kleen0a658002005-04-16 15:25:17 -0700339 stack = (unsigned long *) estack_end[-2];
340 continue;
341 }
342 if (irqstack_end) {
343 unsigned long *irqstack;
344 irqstack = irqstack_end -
345 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
346
347 if (stack >= irqstack && stack < irqstack_end) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700348 printk(" <IRQ>");
Andi Kleen0a658002005-04-16 15:25:17 -0700349 HANDLE_STACK (stack < irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700350 /*
351 * We link to the next stack (which would be
352 * the process stack normally) the last
353 * pointer (index -1 to end) in the IRQ stack:
354 */
Andi Kleen0a658002005-04-16 15:25:17 -0700355 stack = (unsigned long *) (irqstack_end[-1]);
356 irqstack_end = NULL;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700357 printk(" <EOI>");
Andi Kleen0a658002005-04-16 15:25:17 -0700358 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
Andi Kleen0a658002005-04-16 15:25:17 -0700361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Andi Kleen0a658002005-04-16 15:25:17 -0700363
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700364 /*
365 * This prints the process stack:
366 */
Andi Kleen0a658002005-04-16 15:25:17 -0700367 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
368#undef HANDLE_STACK
Ingo Molnar3ac94932006-07-03 00:24:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 printk("\n");
371}
372
Jan Beulichb538ed22006-06-26 13:57:32 +0200373static void _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long * rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 unsigned long *stack;
376 int i;
377 const int cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100378 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
379 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 // debugging aid: "show_stack(NULL, NULL);" prints the
382 // back trace for this cpu.
383
384 if (rsp == NULL) {
385 if (tsk)
386 rsp = (unsigned long *)tsk->thread.rsp;
387 else
388 rsp = (unsigned long *)&rsp;
389 }
390
391 stack = rsp;
392 for(i=0; i < kstack_depth_to_print; i++) {
393 if (stack >= irqstack && stack <= irqstack_end) {
394 if (stack == irqstack_end) {
395 stack = (unsigned long *) (irqstack_end[-1]);
396 printk(" <EOI> ");
397 }
398 } else {
399 if (((long) stack & (THREAD_SIZE-1)) == 0)
400 break;
401 }
402 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700403 printk("\n");
404 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700405 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Jan Beulichb538ed22006-06-26 13:57:32 +0200407 show_trace(tsk, regs, rsp);
408}
409
410void show_stack(struct task_struct *tsk, unsigned long * rsp)
411{
412 _show_stack(tsk, NULL, rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415/*
416 * The architecture-independent dump_stack generator
417 */
418void dump_stack(void)
419{
420 unsigned long dummy;
Jan Beulichb538ed22006-06-26 13:57:32 +0200421 show_trace(NULL, NULL, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424EXPORT_SYMBOL(dump_stack);
425
426void show_registers(struct pt_regs *regs)
427{
428 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700429 int in_kernel = !user_mode(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long rsp;
431 const int cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100432 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 rsp = regs->rsp;
435
436 printk("CPU %d ", cpu);
437 __show_regs(regs);
438 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800439 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /*
442 * When in-kernel, we also print out the stack and code at the
443 * time of the fault..
444 */
445 if (in_kernel) {
446
447 printk("Stack: ");
Jan Beulichb538ed22006-06-26 13:57:32 +0200448 _show_stack(NULL, regs, (unsigned long*)rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 printk("\nCode: ");
Roberto Nibali2b692a82006-03-25 16:29:55 +0100451 if (regs->rip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto bad;
453
Roberto Nibali2b692a82006-03-25 16:29:55 +0100454 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned char c;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100456 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457bad:
458 printk(" Bad RIP value.");
459 break;
460 }
461 printk("%02x ", c);
462 }
463 }
464 printk("\n");
465}
466
467void handle_BUG(struct pt_regs *regs)
468{
469 struct bug_frame f;
Jan Beulich5f1d1892006-01-11 22:46:48 +0100470 long len;
471 const char *prefix = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700473 if (user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return;
Stephen Hemminger77a75332006-01-11 22:46:30 +0100475 if (__copy_from_user(&f, (const void __user *) regs->rip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sizeof(struct bug_frame)))
477 return;
Jan Beulich049cdef2005-09-12 18:49:25 +0200478 if (f.filename >= 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
480 return;
Jan Beulich5f1d1892006-01-11 22:46:48 +0100481 len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1;
482 if (len < 0 || len >= PATH_MAX)
Jan Beulich049cdef2005-09-12 18:49:25 +0200483 f.filename = (int)(long)"unmapped filename";
Jan Beulich5f1d1892006-01-11 22:46:48 +0100484 else if (len > 50) {
485 f.filename += len - 50;
486 prefix = "...";
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
Jan Beulich5f1d1892006-01-11 22:46:48 +0100489 printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700492#ifdef CONFIG_BUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493void out_of_line_bug(void)
494{
495 BUG();
496}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200497EXPORT_SYMBOL(out_of_line_bug);
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500static DEFINE_SPINLOCK(die_lock);
501static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200502static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100504unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jan Beulich12091402005-09-12 18:49:24 +0200506 int cpu = safe_smp_processor_id();
507 unsigned long flags;
508
509 /* racy, but better than risking deadlock. */
510 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!spin_trylock(&die_lock)) {
512 if (cpu == die_owner)
513 /* nested oops. should stop eventually */;
514 else
Jan Beulich12091402005-09-12 18:49:24 +0200515 spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200517 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200518 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200520 bust_spinlocks(1);
521 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100524void __kprobes oops_end(unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200527 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200528 die_nest_count--;
529 if (die_nest_count)
530 /* We still own the lock */
531 local_irq_restore(flags);
532 else
533 /* Nest count reaches zero, release the lock. */
534 spin_unlock_irqrestore(&die_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700536 panic("Fatal exception");
Jan Beulich12091402005-09-12 18:49:24 +0200537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100539void __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 static int die_counter;
542 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
543#ifdef CONFIG_PREEMPT
544 printk("PREEMPT ");
545#endif
546#ifdef CONFIG_SMP
547 printk("SMP ");
548#endif
549#ifdef CONFIG_DEBUG_PAGEALLOC
550 printk("DEBUG_PAGEALLOC");
551#endif
552 printk("\n");
Jan Beulich6e3f3612006-01-11 22:42:14 +0100553 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 show_registers(regs);
555 /* Executive summary in case the oops scrolled away */
556 printk(KERN_ALERT "RIP ");
557 printk_address(regs->rip);
558 printk(" RSP <%016lx>\n", regs->rsp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200559 if (kexec_should_crash(current))
560 crash_kexec(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563void die(const char * str, struct pt_regs * regs, long err)
564{
Jan Beulich12091402005-09-12 18:49:24 +0200565 unsigned long flags = oops_begin();
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 handle_BUG(regs);
568 __die(str, regs, err);
Jan Beulich12091402005-09-12 18:49:24 +0200569 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 do_exit(SIGSEGV);
571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100573void __kprobes die_nmi(char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jan Beulich12091402005-09-12 18:49:24 +0200575 unsigned long flags = oops_begin();
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * We are in trouble anyway, lets at least try
579 * to get a message out.
580 */
581 printk(str, safe_smp_processor_id());
582 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200583 if (kexec_should_crash(current))
584 crash_kexec(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (panic_on_timeout || panic_on_oops)
586 panic("nmi watchdog");
587 printk("console shuts up ...\n");
Jan Beulich12091402005-09-12 18:49:24 +0200588 oops_end(flags);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200589 nmi_exit();
590 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 do_exit(SIGSEGV);
592}
593
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700594static void __kprobes do_trap(int trapnr, int signr, char *str,
595 struct pt_regs * regs, long error_code,
596 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100598 struct task_struct *tsk = current;
599
Jan Beulich6e3f3612006-01-11 22:42:14 +0100600 tsk->thread.error_code = error_code;
601 tsk->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Jan Beulich6e3f3612006-01-11 22:42:14 +0100603 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (exception_trace && unhandled_signal(tsk, signr))
605 printk(KERN_INFO
606 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
607 tsk->comm, tsk->pid, str,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100608 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (info)
611 force_sig_info(signr, info, tsk);
612 else
613 force_sig(signr, tsk);
614 return;
615 }
616
617
618 /* kernel trap */
619 {
620 const struct exception_table_entry *fixup;
621 fixup = search_exception_tables(regs->rip);
Roberto Nibali2b692a82006-03-25 16:29:55 +0100622 if (fixup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 regs->rip = fixup->fixup;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100624 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 die(str, regs, error_code);
626 return;
627 }
628}
629
630#define DO_ERROR(trapnr, signr, str, name) \
631asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
632{ \
633 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
634 == NOTIFY_STOP) \
635 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200636 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 do_trap(trapnr, signr, str, regs, error_code, NULL); \
638}
639
640#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
641asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
642{ \
643 siginfo_t info; \
644 info.si_signo = signr; \
645 info.si_errno = 0; \
646 info.si_code = sicode; \
647 info.si_addr = (void __user *)siaddr; \
648 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
649 == NOTIFY_STOP) \
650 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200651 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 do_trap(trapnr, signr, str, regs, error_code, &info); \
653}
654
655DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
656DO_ERROR( 4, SIGSEGV, "overflow", overflow)
657DO_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert100c0e32006-01-11 22:46:00 +0100658DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
660DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
661DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
662DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
663DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
664DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200665
666/* Runs on IST stack */
667asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
668{
669 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
670 12, SIGBUS) == NOTIFY_STOP)
671 return;
672 preempt_conditional_sti(regs);
673 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
674 preempt_conditional_cli(regs);
675}
Jan Beulicheca37c12006-01-11 22:42:17 +0100676
677asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
678{
679 static const char str[] = "double fault";
680 struct task_struct *tsk = current;
681
682 /* Return not checked because double check cannot be ignored */
683 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
684
685 tsk->thread.error_code = error_code;
686 tsk->thread.trap_no = 8;
687
688 /* This is always a kernel trap and never fixable (and thus must
689 never return). */
690 for (;;)
691 die(str, regs, error_code);
692}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700694asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
695 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100697 struct task_struct *tsk = current;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 conditional_sti(regs);
700
Jan Beulich6e3f3612006-01-11 22:42:14 +0100701 tsk->thread.error_code = error_code;
702 tsk->thread.trap_no = 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jan Beulich6e3f3612006-01-11 22:42:14 +0100704 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
706 printk(KERN_INFO
707 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
708 tsk->comm, tsk->pid,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100709 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 force_sig(SIGSEGV, tsk);
712 return;
713 }
714
715 /* kernel gp */
716 {
717 const struct exception_table_entry *fixup;
718 fixup = search_exception_tables(regs->rip);
719 if (fixup) {
720 regs->rip = fixup->fixup;
721 return;
722 }
723 if (notify_die(DIE_GPF, "general protection fault", regs,
724 error_code, 13, SIGSEGV) == NOTIFY_STOP)
725 return;
726 die("general protection fault", regs, error_code);
727 }
728}
729
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100730static __kprobes void
731mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200733 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
734 reason);
735 printk(KERN_EMERG "You probably have a hardware problem with your "
736 "RAM chips\n");
737
Don Zickus8da5add2006-09-26 10:52:27 +0200738 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200739 panic("NMI: Not continuing");
740
741 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* Clear and disable the memory parity error line. */
744 reason = (reason & 0xf) | 4;
745 outb(reason, 0x61);
746}
747
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100748static __kprobes void
749io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 printk("NMI: IOCK error (debug interrupt?)\n");
752 show_registers(regs);
753
754 /* Re-enable the IOCK line, wait for a few seconds */
755 reason = (reason & 0xf) | 8;
756 outb(reason, 0x61);
757 mdelay(2000);
758 reason &= ~8;
759 outb(reason, 0x61);
760}
761
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100762static __kprobes void
763unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200764{
765 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
766 reason);
767 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200768
769 if (panic_on_unrecovered_nmi)
Don Zickusc41c5cd2006-09-26 10:52:27 +0200770 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200771
Don Zickusc41c5cd2006-09-26 10:52:27 +0200772 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700775/* Runs on IST stack. This code must keep interrupts off all the time.
776 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100777asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700780 int cpu;
781
782 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700785 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 reason = get_nmi_reason();
787
788 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100789 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 == NOTIFY_STOP)
791 return;
792#ifdef CONFIG_X86_LOCAL_APIC
793 /*
794 * Ok, so this is none of the documented NMI sources,
795 * so it must be the NMI watchdog.
796 */
Don Zickus3adbbcce2006-09-26 10:52:26 +0200797 if (nmi_watchdog_tick(regs,reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200799 if (!do_nmi_callback(regs,cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#endif
Don Zickus3adbbcce2006-09-26 10:52:26 +0200801 unknown_nmi_error(reason, regs);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100805 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
807
808 /* AK: following checks seem to be broken on modern chipsets. FIXME */
809
810 if (reason & 0x80)
811 mem_parity_error(reason, regs);
812 if (reason & 0x40)
813 io_check_error(reason, regs);
814}
815
Jan Beulichb556b352006-01-11 22:43:00 +0100816/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700817asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
820 return;
821 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200822 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200824 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700827/* Help handler running on IST stack to switch back to user stack
828 for scheduling or signal handling. The actual stack switch is done in
829 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100830asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700832 struct pt_regs *regs = eregs;
833 /* Did already sync */
834 if (eregs == (struct pt_regs *)eregs->rsp)
835 ;
836 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700837 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800838 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700839 /* Exception from kernel and interrupts are enabled. Move to
840 kernel process stack. */
841 else if (eregs->eflags & X86_EFLAGS_IF)
842 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
843 if (eregs != regs)
844 *regs = *eregs;
845 return regs;
846}
847
848/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700849asmlinkage void __kprobes do_debug(struct pt_regs * regs,
850 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long condition;
853 struct task_struct *tsk = current;
854 siginfo_t info;
855
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700856 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700859 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700860 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700861
John Blackwooda65d17c2006-02-12 14:34:58 -0800862 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* Mask out spurious debug traps due to lazy DR7 setting */
865 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
866 if (!tsk->thread.debugreg7) {
867 goto clear_dr7;
868 }
869 }
870
871 tsk->thread.debugreg6 = condition;
872
873 /* Mask out spurious TF errors due to lazy TF clearing */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700874 if (condition & DR_STEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
876 * The TF error should be masked out only if the current
877 * process is not traced and if the TRAP flag has been set
878 * previously by a tracing process (condition detected by
879 * the PT_DTRACE flag); remember that the i386 TRAP flag
880 * can be modified by the process itself in user mode,
881 * allowing programs to debug themselves without the ptrace()
882 * interface.
883 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700884 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto clear_TF_reenable;
Andi Kleenbe61bff2005-04-16 15:24:57 -0700886 /*
887 * Was the TF flag set by a debugger? If so, clear it now,
888 * so that register information is correct.
889 */
890 if (tsk->ptrace & PT_DTRACE) {
891 regs->eflags &= ~TF_MASK;
892 tsk->ptrace &= ~PT_DTRACE;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896 /* Ok, finally something we can handle */
897 tsk->thread.trap_no = 1;
898 tsk->thread.error_code = error_code;
899 info.si_signo = SIGTRAP;
900 info.si_errno = 0;
901 info.si_code = TRAP_BRKPT;
John Blackwood01b8faa2006-01-11 22:44:15 +0100902 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
903 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700906 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800907 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700908 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910clear_TF_reenable:
911 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 regs->eflags &= ~TF_MASK;
John Blackwooda65d17c2006-02-12 14:34:58 -0800913 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Jan Beulich6e3f3612006-01-11 22:42:14 +0100916static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 const struct exception_table_entry *fixup;
919 fixup = search_exception_tables(regs->rip);
920 if (fixup) {
921 regs->rip = fixup->fixup;
922 return 1;
923 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100924 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700925 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100926 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929}
930
931/*
932 * Note that we play around with the 'TS' bit in an attempt to get
933 * the correct behaviour even in the presence of the asynchronous
934 * IRQ13 behaviour
935 */
936asmlinkage void do_coprocessor_error(struct pt_regs *regs)
937{
938 void __user *rip = (void __user *)(regs->rip);
939 struct task_struct * task;
940 siginfo_t info;
941 unsigned short cwd, swd;
942
943 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700944 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100945 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return;
947
948 /*
949 * Save the info for the exception handler and clear the error.
950 */
951 task = current;
952 save_init_fpu(task);
953 task->thread.trap_no = 16;
954 task->thread.error_code = 0;
955 info.si_signo = SIGFPE;
956 info.si_errno = 0;
957 info.si_code = __SI_FAULT;
958 info.si_addr = rip;
959 /*
960 * (~cwd & swd) will mask out exceptions that are not set to unmasked
961 * status. 0x3f is the exception bits in these regs, 0x200 is the
962 * C1 reg you need in case of a stack fault, 0x040 is the stack
963 * fault bit. We should only be taking one exception at a time,
964 * so if this combination doesn't produce any single exception,
965 * then we have a bad program that isn't synchronizing its FPU usage
966 * and it will suffer the consequences since we won't be able to
967 * fully reproduce the context of the exception
968 */
969 cwd = get_fpu_cwd(task);
970 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200971 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 case 0x000:
973 default:
974 break;
975 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200976 /*
977 * swd & 0x240 == 0x040: Stack Underflow
978 * swd & 0x240 == 0x240: Stack Overflow
979 * User must clear the SF bit (0x40) if set
980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 info.si_code = FPE_FLTINV;
982 break;
983 case 0x002: /* Denormalize */
984 case 0x010: /* Underflow */
985 info.si_code = FPE_FLTUND;
986 break;
987 case 0x004: /* Zero Divide */
988 info.si_code = FPE_FLTDIV;
989 break;
990 case 0x008: /* Overflow */
991 info.si_code = FPE_FLTOVF;
992 break;
993 case 0x020: /* Precision */
994 info.si_code = FPE_FLTRES;
995 break;
996 }
997 force_sig_info(SIGFPE, &info, task);
998}
999
1000asmlinkage void bad_intr(void)
1001{
1002 printk("bad interrupt");
1003}
1004
1005asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1006{
1007 void __user *rip = (void __user *)(regs->rip);
1008 struct task_struct * task;
1009 siginfo_t info;
1010 unsigned short mxcsr;
1011
1012 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -07001013 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +01001014 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return;
1016
1017 /*
1018 * Save the info for the exception handler and clear the error.
1019 */
1020 task = current;
1021 save_init_fpu(task);
1022 task->thread.trap_no = 19;
1023 task->thread.error_code = 0;
1024 info.si_signo = SIGFPE;
1025 info.si_errno = 0;
1026 info.si_code = __SI_FAULT;
1027 info.si_addr = rip;
1028 /*
1029 * The SIMD FPU exceptions are handled a little differently, as there
1030 * is only a single status/control register. Thus, to determine which
1031 * unmasked exception was caught we must mask the exception mask bits
1032 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1033 */
1034 mxcsr = get_fpu_mxcsr(task);
1035 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1036 case 0x000:
1037 default:
1038 break;
1039 case 0x001: /* Invalid Op */
1040 info.si_code = FPE_FLTINV;
1041 break;
1042 case 0x002: /* Denormalize */
1043 case 0x010: /* Underflow */
1044 info.si_code = FPE_FLTUND;
1045 break;
1046 case 0x004: /* Zero Divide */
1047 info.si_code = FPE_FLTDIV;
1048 break;
1049 case 0x008: /* Overflow */
1050 info.si_code = FPE_FLTOVF;
1051 break;
1052 case 0x020: /* Precision */
1053 info.si_code = FPE_FLTRES;
1054 break;
1055 }
1056 force_sig_info(SIGFPE, &info, task);
1057}
1058
1059asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1060{
1061}
1062
1063asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1064{
1065}
1066
Jacob Shin89b831e2005-11-05 17:25:53 +01001067asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1068{
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * 'math_state_restore()' saves the current math information in the
1073 * old math state array, and gets the new ones from the current task
1074 *
1075 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1076 * Don't touch unless you *really* know how it works.
1077 */
1078asmlinkage void math_state_restore(void)
1079{
1080 struct task_struct *me = current;
1081 clts(); /* Allow maths ops (or we recurse) */
1082
1083 if (!used_math())
1084 init_fpu(me);
1085 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001086 task_thread_info(me)->status |= TS_USEDFPU;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089void __init trap_init(void)
1090{
1091 set_intr_gate(0,&divide_error);
1092 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1093 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001094 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001095 set_system_gate(4,&overflow); /* int4 can be called from all */
1096 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 set_intr_gate(6,&invalid_op);
1098 set_intr_gate(7,&device_not_available);
1099 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1100 set_intr_gate(9,&coprocessor_segment_overrun);
1101 set_intr_gate(10,&invalid_TSS);
1102 set_intr_gate(11,&segment_not_present);
1103 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1104 set_intr_gate(13,&general_protection);
1105 set_intr_gate(14,&page_fault);
1106 set_intr_gate(15,&spurious_interrupt_bug);
1107 set_intr_gate(16,&coprocessor_error);
1108 set_intr_gate(17,&alignment_check);
1109#ifdef CONFIG_X86_MCE
1110 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1111#endif
1112 set_intr_gate(19,&simd_coprocessor_error);
1113
1114#ifdef CONFIG_IA32_EMULATION
1115 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1116#endif
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * Should be a barrier for any external CPU state.
1120 */
1121 cpu_init();
1122}
1123
1124
1125/* Actual parsing is done early in setup.c. */
1126static int __init oops_dummy(char *s)
1127{
1128 panic_on_oops = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001129 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131__setup("oops=", oops_dummy);
1132
1133static int __init kstack_setup(char *s)
1134{
1135 kstack_depth_to_print = simple_strtoul(s,NULL,0);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001136 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138__setup("kstack=", kstack_setup);
1139
Jan Beulichea424052006-08-30 19:37:11 +02001140#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001141static int __init call_trace_setup(char *s)
1142{
1143 if (strcmp(s, "old") == 0)
1144 call_trace = -1;
1145 else if (strcmp(s, "both") == 0)
1146 call_trace = 0;
Andi Kleenb13761e2006-07-28 14:44:51 +02001147 else if (strcmp(s, "newfallback") == 0)
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001148 call_trace = 1;
Andi Kleenb13761e2006-07-28 14:44:51 +02001149 else if (strcmp(s, "new") == 0)
1150 call_trace = 2;
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001151 return 1;
1152}
1153__setup("call_trace=", call_trace_setup);
Jan Beulichea424052006-08-30 19:37:11 +02001154#endif