blob: 79d05c4820720f492578c8cb0f1b6ef1feb8f21d [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}
Alan Sterne041c682006-03-27 01:16:30 -080079EXPORT_SYMBOL(register_die_notifier);
80
81int unregister_die_notifier(struct notifier_block *nb)
82{
83 return atomic_notifier_chain_unregister(&die_chain, nb);
84}
85EXPORT_SYMBOL(unregister_die_notifier);
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 Beulichc33bd9a2006-06-26 13:57:47 +0200110static int call_trace = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#ifdef CONFIG_KALLSYMS
Ingo Molnar3ac94932006-07-03 00:24:36 -0700113# include <linux/kallsyms.h>
114void printk_address(unsigned long address)
115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned long offset = 0, symsize;
117 const char *symname;
118 char *modname;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700119 char *delim = ":";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 char namebuf[128];
121
Ingo Molnar3ac94932006-07-03 00:24:36 -0700122 symname = kallsyms_lookup(address, &symsize, &offset,
123 &modname, namebuf);
124 if (!symname) {
125 printk(" [<%016lx>]\n", address);
126 return;
127 }
128 if (!modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 modname = delim = "";
Ingo Molnar3ac94932006-07-03 00:24:36 -0700130 printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
131 address, delim, modname, delim, symname, offset, symsize);
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#else
Ingo Molnar3ac94932006-07-03 00:24:36 -0700134void printk_address(unsigned long address)
135{
136 printk(" [<%016lx>]\n", address);
137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif
139
Andi Kleen0a658002005-04-16 15:25:17 -0700140static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
141 unsigned *usedp, const char **idp)
142{
Jan Beulichb556b352006-01-11 22:43:00 +0100143 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700144 [DEBUG_STACK - 1] = "#DB",
145 [NMI_STACK - 1] = "NMI",
146 [DOUBLEFAULT_STACK - 1] = "#DF",
147 [STACKFAULT_STACK - 1] = "#SS",
148 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100149#if DEBUG_STKSZ > EXCEPTION_STKSZ
150 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
151#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700152 };
153 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700155 /*
156 * Iterate over all exception stacks, and figure out whether
157 * 'stack' is in one of them:
158 */
Andi Kleen0a658002005-04-16 15:25:17 -0700159 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
160 unsigned long end;
161
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700162 /*
163 * set 'end' to the end of the exception stack.
164 */
Jan Beulichb556b352006-01-11 22:43:00 +0100165 switch (k + 1) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700166 /*
167 * TODO: this block is not needed i think, because
168 * setup64.c:cpu_init() sets up t->ist[DEBUG_STACK]
169 * properly too.
170 */
Jan Beulichb556b352006-01-11 22:43:00 +0100171#if DEBUG_STKSZ > EXCEPTION_STKSZ
172 case DEBUG_STACK:
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100173 end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ;
Jan Beulichb556b352006-01-11 22:43:00 +0100174 break;
175#endif
176 default:
177 end = per_cpu(init_tss, cpu).ist[k];
178 break;
179 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700180 /*
181 * Is 'stack' above this exception frame's end?
182 * If yes then skip to the next frame.
183 */
Andi Kleen0a658002005-04-16 15:25:17 -0700184 if (stack >= end)
185 continue;
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700186 /*
187 * Is 'stack' above this exception frame's start address?
188 * If yes then we found the right frame.
189 */
Andi Kleen0a658002005-04-16 15:25:17 -0700190 if (stack >= end - EXCEPTION_STKSZ) {
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700191 /*
192 * Make sure we only iterate through an exception
193 * stack once. If it comes up for the second time
194 * then there's something wrong going on - just
195 * break out and return NULL:
196 */
Andi Kleen0a658002005-04-16 15:25:17 -0700197 if (*usedp & (1U << k))
198 break;
199 *usedp |= 1U << k;
200 *idp = ids[k];
201 return (unsigned long *)end;
202 }
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700203 /*
204 * If this is a debug stack, and if it has a larger size than
205 * the usual exception stacks, then 'stack' might still
206 * be within the lower portion of the debug stack:
207 */
Jan Beulichb556b352006-01-11 22:43:00 +0100208#if DEBUG_STKSZ > EXCEPTION_STKSZ
209 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
210 unsigned j = N_EXCEPTION_STACKS - 1;
211
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700212 /*
213 * Black magic. A large debug stack is composed of
214 * multiple exception stack entries, which we
215 * iterate through now. Dont look:
216 */
Jan Beulichb556b352006-01-11 22:43:00 +0100217 do {
218 ++j;
219 end -= EXCEPTION_STKSZ;
220 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
221 } while (stack < end - EXCEPTION_STKSZ);
222 if (*usedp & (1U << j))
223 break;
224 *usedp |= 1U << j;
225 *idp = ids[j];
226 return (unsigned long *)end;
227 }
228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200233static int show_trace_unwind(struct unwind_frame_info *info, void *context)
Jan Beulichb538ed22006-06-26 13:57:32 +0200234{
Ingo Molnar3ac94932006-07-03 00:24:36 -0700235 int n = 0;
Jan Beulichb538ed22006-06-26 13:57:32 +0200236
237 while (unwind(info) == 0 && UNW_PC(info)) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700238 n++;
239 printk_address(UNW_PC(info));
Jan Beulichb538ed22006-06-26 13:57:32 +0200240 if (arch_unw_user_mode(info))
241 break;
242 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200243 return n;
Jan Beulichb538ed22006-06-26 13:57:32 +0200244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
247 * x86-64 can have upto three kernel stacks:
248 * process stack
249 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700250 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252
Jan Beulichb538ed22006-06-26 13:57:32 +0200253void show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long * stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Andi Kleen0a658002005-04-16 15:25:17 -0700255 const unsigned cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100256 unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200257 int i = 11;
Andi Kleen0a658002005-04-16 15:25:17 -0700258 unsigned used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Ingo Molnar3ac94932006-07-03 00:24:36 -0700260 printk("\nCall Trace:\n");
Andi Kleen0a658002005-04-16 15:25:17 -0700261
Jan Beulichb538ed22006-06-26 13:57:32 +0200262 if (!tsk)
263 tsk = current;
264
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200265 if (call_trace >= 0) {
266 int unw_ret = 0;
267 struct unwind_frame_info info;
268
269 if (regs) {
270 if (unwind_init_frame_info(&info, tsk, regs) == 0)
271 unw_ret = show_trace_unwind(&info, NULL);
272 } else if (tsk == current)
273 unw_ret = unwind_init_running(&info, show_trace_unwind, NULL);
274 else {
275 if (unwind_init_blocked(&info, tsk) == 0)
276 unw_ret = show_trace_unwind(&info, NULL);
Jan Beulichb538ed22006-06-26 13:57:32 +0200277 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200278 if (unw_ret > 0) {
279 if (call_trace > 0)
280 return;
281 printk("Legacy call trace:");
282 i = 18;
Jan Beulichb538ed22006-06-26 13:57:32 +0200283 }
284 }
285
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700286 /*
287 * Print function call entries within a stack. 'cond' is the
288 * "end of stackframe" condition, that the 'stack++'
289 * iteration will eventually trigger.
290 */
Andi Kleen0a658002005-04-16 15:25:17 -0700291#define HANDLE_STACK(cond) \
292 do while (cond) { \
Jan Beulich1b2f6302006-01-11 22:46:45 +0100293 unsigned long addr = *stack++; \
Andi Kleen0a658002005-04-16 15:25:17 -0700294 if (kernel_text_address(addr)) { \
295 /* \
296 * If the address is either in the text segment of the \
297 * kernel, or in the region which contains vmalloc'ed \
298 * memory, it *may* be the address of a calling \
299 * routine; if so, print it so that someone tracing \
300 * down the cause of the crash will be able to figure \
301 * out the call path that was taken. \
302 */ \
Ingo Molnar3ac94932006-07-03 00:24:36 -0700303 printk_address(addr); \
Andi Kleen0a658002005-04-16 15:25:17 -0700304 } \
305 } while (0)
306
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700307 /*
308 * Print function call entries in all stacks, starting at the
309 * current stack address. If the stacks consist of nested
310 * exceptions
311 */
312 for ( ; ; ) {
Andi Kleen0a658002005-04-16 15:25:17 -0700313 const char *id;
314 unsigned long *estack_end;
315 estack_end = in_exception_stack(cpu, (unsigned long)stack,
316 &used, &id);
317
318 if (estack_end) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700319 printk(" <%s>", id);
Andi Kleen0a658002005-04-16 15:25:17 -0700320 HANDLE_STACK (stack < estack_end);
Ingo Molnar3ac94932006-07-03 00:24:36 -0700321 printk(" <EOE>");
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700322 /*
323 * We link to the next stack via the
324 * second-to-last pointer (index -2 to end) in the
325 * exception stack:
326 */
Andi Kleen0a658002005-04-16 15:25:17 -0700327 stack = (unsigned long *) estack_end[-2];
328 continue;
329 }
330 if (irqstack_end) {
331 unsigned long *irqstack;
332 irqstack = irqstack_end -
333 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
334
335 if (stack >= irqstack && stack < irqstack_end) {
Ingo Molnar3ac94932006-07-03 00:24:36 -0700336 printk(" <IRQ>");
Andi Kleen0a658002005-04-16 15:25:17 -0700337 HANDLE_STACK (stack < irqstack_end);
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700338 /*
339 * We link to the next stack (which would be
340 * the process stack normally) the last
341 * pointer (index -1 to end) in the IRQ stack:
342 */
Andi Kleen0a658002005-04-16 15:25:17 -0700343 stack = (unsigned long *) (irqstack_end[-1]);
344 irqstack_end = NULL;
Ingo Molnar3ac94932006-07-03 00:24:36 -0700345 printk(" <EOI>");
Andi Kleen0a658002005-04-16 15:25:17 -0700346 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
Andi Kleen0a658002005-04-16 15:25:17 -0700349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Andi Kleen0a658002005-04-16 15:25:17 -0700351
Ingo Molnarc9ca1ba2006-07-03 00:24:36 -0700352 /*
353 * This prints the process stack:
354 */
Andi Kleen0a658002005-04-16 15:25:17 -0700355 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
356#undef HANDLE_STACK
Ingo Molnar3ac94932006-07-03 00:24:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk("\n");
359}
360
Jan Beulichb538ed22006-06-26 13:57:32 +0200361static void _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long * rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned long *stack;
364 int i;
365 const int cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100366 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
367 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 // debugging aid: "show_stack(NULL, NULL);" prints the
370 // back trace for this cpu.
371
372 if (rsp == NULL) {
373 if (tsk)
374 rsp = (unsigned long *)tsk->thread.rsp;
375 else
376 rsp = (unsigned long *)&rsp;
377 }
378
379 stack = rsp;
380 for(i=0; i < kstack_depth_to_print; i++) {
381 if (stack >= irqstack && stack <= irqstack_end) {
382 if (stack == irqstack_end) {
383 stack = (unsigned long *) (irqstack_end[-1]);
384 printk(" <EOI> ");
385 }
386 } else {
387 if (((long) stack & (THREAD_SIZE-1)) == 0)
388 break;
389 }
390 if (i && ((i % 4) == 0))
Ingo Molnar3ac94932006-07-03 00:24:36 -0700391 printk("\n");
392 printk(" %016lx", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700393 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Jan Beulichb538ed22006-06-26 13:57:32 +0200395 show_trace(tsk, regs, rsp);
396}
397
398void show_stack(struct task_struct *tsk, unsigned long * rsp)
399{
400 _show_stack(tsk, NULL, rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403/*
404 * The architecture-independent dump_stack generator
405 */
406void dump_stack(void)
407{
408 unsigned long dummy;
Jan Beulichb538ed22006-06-26 13:57:32 +0200409 show_trace(NULL, NULL, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412EXPORT_SYMBOL(dump_stack);
413
414void show_registers(struct pt_regs *regs)
415{
416 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700417 int in_kernel = !user_mode(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unsigned long rsp;
419 const int cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100420 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 rsp = regs->rsp;
423
424 printk("CPU %d ", cpu);
425 __show_regs(regs);
426 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800427 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * When in-kernel, we also print out the stack and code at the
431 * time of the fault..
432 */
433 if (in_kernel) {
434
435 printk("Stack: ");
Jan Beulichb538ed22006-06-26 13:57:32 +0200436 _show_stack(NULL, regs, (unsigned long*)rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 printk("\nCode: ");
Roberto Nibali2b692a82006-03-25 16:29:55 +0100439 if (regs->rip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 goto bad;
441
Roberto Nibali2b692a82006-03-25 16:29:55 +0100442 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned char c;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100444 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445bad:
446 printk(" Bad RIP value.");
447 break;
448 }
449 printk("%02x ", c);
450 }
451 }
452 printk("\n");
453}
454
455void handle_BUG(struct pt_regs *regs)
456{
457 struct bug_frame f;
Jan Beulich5f1d1892006-01-11 22:46:48 +0100458 long len;
459 const char *prefix = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700461 if (user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return;
Stephen Hemminger77a75332006-01-11 22:46:30 +0100463 if (__copy_from_user(&f, (const void __user *) regs->rip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 sizeof(struct bug_frame)))
465 return;
Jan Beulich049cdef2005-09-12 18:49:25 +0200466 if (f.filename >= 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
468 return;
Jan Beulich5f1d1892006-01-11 22:46:48 +0100469 len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1;
470 if (len < 0 || len >= PATH_MAX)
Jan Beulich049cdef2005-09-12 18:49:25 +0200471 f.filename = (int)(long)"unmapped filename";
Jan Beulich5f1d1892006-01-11 22:46:48 +0100472 else if (len > 50) {
473 f.filename += len - 50;
474 prefix = "...";
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
Jan Beulich5f1d1892006-01-11 22:46:48 +0100477 printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700480#ifdef CONFIG_BUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481void out_of_line_bug(void)
482{
483 BUG();
484}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200485EXPORT_SYMBOL(out_of_line_bug);
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488static DEFINE_SPINLOCK(die_lock);
489static int die_owner = -1;
Corey Minyardcdc60a42006-05-08 15:17:22 +0200490static unsigned int die_nest_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100492unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jan Beulich12091402005-09-12 18:49:24 +0200494 int cpu = safe_smp_processor_id();
495 unsigned long flags;
496
497 /* racy, but better than risking deadlock. */
498 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!spin_trylock(&die_lock)) {
500 if (cpu == die_owner)
501 /* nested oops. should stop eventually */;
502 else
Jan Beulich12091402005-09-12 18:49:24 +0200503 spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Corey Minyardcdc60a42006-05-08 15:17:22 +0200505 die_nest_count++;
Jan Beulich12091402005-09-12 18:49:24 +0200506 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200508 bust_spinlocks(1);
509 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100512void __kprobes oops_end(unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200515 bust_spinlocks(0);
Corey Minyardcdc60a42006-05-08 15:17:22 +0200516 die_nest_count--;
517 if (die_nest_count)
518 /* We still own the lock */
519 local_irq_restore(flags);
520 else
521 /* Nest count reaches zero, release the lock. */
522 spin_unlock_irqrestore(&die_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (panic_on_oops)
Jan Beulich12091402005-09-12 18:49:24 +0200524 panic("Oops");
525}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100527void __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 static int die_counter;
530 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
531#ifdef CONFIG_PREEMPT
532 printk("PREEMPT ");
533#endif
534#ifdef CONFIG_SMP
535 printk("SMP ");
536#endif
537#ifdef CONFIG_DEBUG_PAGEALLOC
538 printk("DEBUG_PAGEALLOC");
539#endif
540 printk("\n");
Jan Beulich6e3f3612006-01-11 22:42:14 +0100541 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 show_registers(regs);
543 /* Executive summary in case the oops scrolled away */
544 printk(KERN_ALERT "RIP ");
545 printk_address(regs->rip);
546 printk(" RSP <%016lx>\n", regs->rsp);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200547 if (kexec_should_crash(current))
548 crash_kexec(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551void die(const char * str, struct pt_regs * regs, long err)
552{
Jan Beulich12091402005-09-12 18:49:24 +0200553 unsigned long flags = oops_begin();
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 handle_BUG(regs);
556 __die(str, regs, err);
Jan Beulich12091402005-09-12 18:49:24 +0200557 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 do_exit(SIGSEGV);
559}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100561void __kprobes die_nmi(char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Jan Beulich12091402005-09-12 18:49:24 +0200563 unsigned long flags = oops_begin();
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * We are in trouble anyway, lets at least try
567 * to get a message out.
568 */
569 printk(str, safe_smp_processor_id());
570 show_registers(regs);
Vivek Goyal8bcc5282006-04-18 12:35:13 +0200571 if (kexec_should_crash(current))
572 crash_kexec(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (panic_on_timeout || panic_on_oops)
574 panic("nmi watchdog");
575 printk("console shuts up ...\n");
Jan Beulich12091402005-09-12 18:49:24 +0200576 oops_end(flags);
Corey Minyard8b1ffe952006-05-08 15:17:25 +0200577 nmi_exit();
578 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 do_exit(SIGSEGV);
580}
581
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700582static void __kprobes do_trap(int trapnr, int signr, char *str,
583 struct pt_regs * regs, long error_code,
584 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100586 struct task_struct *tsk = current;
587
Jan Beulich6e3f3612006-01-11 22:42:14 +0100588 tsk->thread.error_code = error_code;
589 tsk->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Jan Beulich6e3f3612006-01-11 22:42:14 +0100591 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (exception_trace && unhandled_signal(tsk, signr))
593 printk(KERN_INFO
594 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
595 tsk->comm, tsk->pid, str,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100596 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (info)
599 force_sig_info(signr, info, tsk);
600 else
601 force_sig(signr, tsk);
602 return;
603 }
604
605
606 /* kernel trap */
607 {
608 const struct exception_table_entry *fixup;
609 fixup = search_exception_tables(regs->rip);
Roberto Nibali2b692a82006-03-25 16:29:55 +0100610 if (fixup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 regs->rip = fixup->fixup;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100612 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 die(str, regs, error_code);
614 return;
615 }
616}
617
618#define DO_ERROR(trapnr, signr, str, name) \
619asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
620{ \
621 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
622 == NOTIFY_STOP) \
623 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200624 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 do_trap(trapnr, signr, str, regs, error_code, NULL); \
626}
627
628#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
629asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
630{ \
631 siginfo_t info; \
632 info.si_signo = signr; \
633 info.si_errno = 0; \
634 info.si_code = sicode; \
635 info.si_addr = (void __user *)siaddr; \
636 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
637 == NOTIFY_STOP) \
638 return; \
Andi Kleen40e59a62006-05-15 18:19:47 +0200639 conditional_sti(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 do_trap(trapnr, signr, str, regs, error_code, &info); \
641}
642
643DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
644DO_ERROR( 4, SIGSEGV, "overflow", overflow)
645DO_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert100c0e32006-01-11 22:46:00 +0100646DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
648DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
649DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
650DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
651DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
652DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen40e59a62006-05-15 18:19:47 +0200653
654/* Runs on IST stack */
655asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
656{
657 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
658 12, SIGBUS) == NOTIFY_STOP)
659 return;
660 preempt_conditional_sti(regs);
661 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
662 preempt_conditional_cli(regs);
663}
Jan Beulicheca37c12006-01-11 22:42:17 +0100664
665asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
666{
667 static const char str[] = "double fault";
668 struct task_struct *tsk = current;
669
670 /* Return not checked because double check cannot be ignored */
671 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
672
673 tsk->thread.error_code = error_code;
674 tsk->thread.trap_no = 8;
675
676 /* This is always a kernel trap and never fixable (and thus must
677 never return). */
678 for (;;)
679 die(str, regs, error_code);
680}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700682asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
683 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100685 struct task_struct *tsk = current;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 conditional_sti(regs);
688
Jan Beulich6e3f3612006-01-11 22:42:14 +0100689 tsk->thread.error_code = error_code;
690 tsk->thread.trap_no = 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jan Beulich6e3f3612006-01-11 22:42:14 +0100692 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
694 printk(KERN_INFO
695 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
696 tsk->comm, tsk->pid,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100697 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 force_sig(SIGSEGV, tsk);
700 return;
701 }
702
703 /* kernel gp */
704 {
705 const struct exception_table_entry *fixup;
706 fixup = search_exception_tables(regs->rip);
707 if (fixup) {
708 regs->rip = fixup->fixup;
709 return;
710 }
711 if (notify_die(DIE_GPF, "general protection fault", regs,
712 error_code, 13, SIGSEGV) == NOTIFY_STOP)
713 return;
714 die("general protection fault", regs, error_code);
715 }
716}
717
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100718static __kprobes void
719mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
722 printk("You probably have a hardware problem with your RAM chips\n");
723
724 /* Clear and disable the memory parity error line. */
725 reason = (reason & 0xf) | 4;
726 outb(reason, 0x61);
727}
728
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100729static __kprobes void
730io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 printk("NMI: IOCK error (debug interrupt?)\n");
733 show_registers(regs);
734
735 /* Re-enable the IOCK line, wait for a few seconds */
736 reason = (reason & 0xf) | 8;
737 outb(reason, 0x61);
738 mdelay(2000);
739 reason &= ~8;
740 outb(reason, 0x61);
741}
742
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100743static __kprobes void
744unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{ printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
746 printk("Dazed and confused, but trying to continue\n");
747 printk("Do you have a strange power saving mode enabled?\n");
748}
749
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700750/* Runs on IST stack. This code must keep interrupts off all the time.
751 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100752asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700755 int cpu;
756
757 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700760 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 reason = get_nmi_reason();
762
763 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100764 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 == NOTIFY_STOP)
766 return;
767#ifdef CONFIG_X86_LOCAL_APIC
768 /*
769 * Ok, so this is none of the documented NMI sources,
770 * so it must be the NMI watchdog.
771 */
772 if (nmi_watchdog > 0) {
773 nmi_watchdog_tick(regs,reason);
774 return;
775 }
776#endif
777 unknown_nmi_error(reason, regs);
778 return;
779 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100780 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return;
782
783 /* AK: following checks seem to be broken on modern chipsets. FIXME */
784
785 if (reason & 0x80)
786 mem_parity_error(reason, regs);
787 if (reason & 0x40)
788 io_check_error(reason, regs);
789}
790
Jan Beulichb556b352006-01-11 22:43:00 +0100791/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700792asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
795 return;
796 }
Andi Kleen40e59a62006-05-15 18:19:47 +0200797 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Andi Kleen40e59a62006-05-15 18:19:47 +0200799 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700802/* Help handler running on IST stack to switch back to user stack
803 for scheduling or signal handling. The actual stack switch is done in
804 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100805asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700807 struct pt_regs *regs = eregs;
808 /* Did already sync */
809 if (eregs == (struct pt_regs *)eregs->rsp)
810 ;
811 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700812 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800813 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700814 /* Exception from kernel and interrupts are enabled. Move to
815 kernel process stack. */
816 else if (eregs->eflags & X86_EFLAGS_IF)
817 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
818 if (eregs != regs)
819 *regs = *eregs;
820 return regs;
821}
822
823/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700824asmlinkage void __kprobes do_debug(struct pt_regs * regs,
825 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 unsigned long condition;
828 struct task_struct *tsk = current;
829 siginfo_t info;
830
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700831 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700834 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700835 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700836
John Blackwooda65d17c2006-02-12 14:34:58 -0800837 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* Mask out spurious debug traps due to lazy DR7 setting */
840 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
841 if (!tsk->thread.debugreg7) {
842 goto clear_dr7;
843 }
844 }
845
846 tsk->thread.debugreg6 = condition;
847
848 /* Mask out spurious TF errors due to lazy TF clearing */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700849 if (condition & DR_STEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /*
851 * The TF error should be masked out only if the current
852 * process is not traced and if the TRAP flag has been set
853 * previously by a tracing process (condition detected by
854 * the PT_DTRACE flag); remember that the i386 TRAP flag
855 * can be modified by the process itself in user mode,
856 * allowing programs to debug themselves without the ptrace()
857 * interface.
858 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700859 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto clear_TF_reenable;
Andi Kleenbe61bff2005-04-16 15:24:57 -0700861 /*
862 * Was the TF flag set by a debugger? If so, clear it now,
863 * so that register information is correct.
864 */
865 if (tsk->ptrace & PT_DTRACE) {
866 regs->eflags &= ~TF_MASK;
867 tsk->ptrace &= ~PT_DTRACE;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 /* Ok, finally something we can handle */
872 tsk->thread.trap_no = 1;
873 tsk->thread.error_code = error_code;
874 info.si_signo = SIGTRAP;
875 info.si_errno = 0;
876 info.si_code = TRAP_BRKPT;
John Blackwood01b8faa2006-01-11 22:44:15 +0100877 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
878 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700881 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800882 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700883 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885clear_TF_reenable:
886 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 regs->eflags &= ~TF_MASK;
John Blackwooda65d17c2006-02-12 14:34:58 -0800888 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Jan Beulich6e3f3612006-01-11 22:42:14 +0100891static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 const struct exception_table_entry *fixup;
894 fixup = search_exception_tables(regs->rip);
895 if (fixup) {
896 regs->rip = fixup->fixup;
897 return 1;
898 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100899 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700900 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100901 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904}
905
906/*
907 * Note that we play around with the 'TS' bit in an attempt to get
908 * the correct behaviour even in the presence of the asynchronous
909 * IRQ13 behaviour
910 */
911asmlinkage void do_coprocessor_error(struct pt_regs *regs)
912{
913 void __user *rip = (void __user *)(regs->rip);
914 struct task_struct * task;
915 siginfo_t info;
916 unsigned short cwd, swd;
917
918 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700919 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100920 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return;
922
923 /*
924 * Save the info for the exception handler and clear the error.
925 */
926 task = current;
927 save_init_fpu(task);
928 task->thread.trap_no = 16;
929 task->thread.error_code = 0;
930 info.si_signo = SIGFPE;
931 info.si_errno = 0;
932 info.si_code = __SI_FAULT;
933 info.si_addr = rip;
934 /*
935 * (~cwd & swd) will mask out exceptions that are not set to unmasked
936 * status. 0x3f is the exception bits in these regs, 0x200 is the
937 * C1 reg you need in case of a stack fault, 0x040 is the stack
938 * fault bit. We should only be taking one exception at a time,
939 * so if this combination doesn't produce any single exception,
940 * then we have a bad program that isn't synchronizing its FPU usage
941 * and it will suffer the consequences since we won't be able to
942 * fully reproduce the context of the exception
943 */
944 cwd = get_fpu_cwd(task);
945 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200946 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case 0x000:
948 default:
949 break;
950 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200951 /*
952 * swd & 0x240 == 0x040: Stack Underflow
953 * swd & 0x240 == 0x240: Stack Overflow
954 * User must clear the SF bit (0x40) if set
955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 info.si_code = FPE_FLTINV;
957 break;
958 case 0x002: /* Denormalize */
959 case 0x010: /* Underflow */
960 info.si_code = FPE_FLTUND;
961 break;
962 case 0x004: /* Zero Divide */
963 info.si_code = FPE_FLTDIV;
964 break;
965 case 0x008: /* Overflow */
966 info.si_code = FPE_FLTOVF;
967 break;
968 case 0x020: /* Precision */
969 info.si_code = FPE_FLTRES;
970 break;
971 }
972 force_sig_info(SIGFPE, &info, task);
973}
974
975asmlinkage void bad_intr(void)
976{
977 printk("bad interrupt");
978}
979
980asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
981{
982 void __user *rip = (void __user *)(regs->rip);
983 struct task_struct * task;
984 siginfo_t info;
985 unsigned short mxcsr;
986
987 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700988 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100989 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return;
991
992 /*
993 * Save the info for the exception handler and clear the error.
994 */
995 task = current;
996 save_init_fpu(task);
997 task->thread.trap_no = 19;
998 task->thread.error_code = 0;
999 info.si_signo = SIGFPE;
1000 info.si_errno = 0;
1001 info.si_code = __SI_FAULT;
1002 info.si_addr = rip;
1003 /*
1004 * The SIMD FPU exceptions are handled a little differently, as there
1005 * is only a single status/control register. Thus, to determine which
1006 * unmasked exception was caught we must mask the exception mask bits
1007 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1008 */
1009 mxcsr = get_fpu_mxcsr(task);
1010 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1011 case 0x000:
1012 default:
1013 break;
1014 case 0x001: /* Invalid Op */
1015 info.si_code = FPE_FLTINV;
1016 break;
1017 case 0x002: /* Denormalize */
1018 case 0x010: /* Underflow */
1019 info.si_code = FPE_FLTUND;
1020 break;
1021 case 0x004: /* Zero Divide */
1022 info.si_code = FPE_FLTDIV;
1023 break;
1024 case 0x008: /* Overflow */
1025 info.si_code = FPE_FLTOVF;
1026 break;
1027 case 0x020: /* Precision */
1028 info.si_code = FPE_FLTRES;
1029 break;
1030 }
1031 force_sig_info(SIGFPE, &info, task);
1032}
1033
1034asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1035{
1036}
1037
1038asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1039{
1040}
1041
Jacob Shin89b831e2005-11-05 17:25:53 +01001042asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1043{
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
1047 * 'math_state_restore()' saves the current math information in the
1048 * old math state array, and gets the new ones from the current task
1049 *
1050 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1051 * Don't touch unless you *really* know how it works.
1052 */
1053asmlinkage void math_state_restore(void)
1054{
1055 struct task_struct *me = current;
1056 clts(); /* Allow maths ops (or we recurse) */
1057
1058 if (!used_math())
1059 init_fpu(me);
1060 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -08001061 task_thread_info(me)->status |= TS_USEDFPU;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064void __init trap_init(void)
1065{
1066 set_intr_gate(0,&divide_error);
1067 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1068 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +01001069 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +01001070 set_system_gate(4,&overflow); /* int4 can be called from all */
1071 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 set_intr_gate(6,&invalid_op);
1073 set_intr_gate(7,&device_not_available);
1074 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1075 set_intr_gate(9,&coprocessor_segment_overrun);
1076 set_intr_gate(10,&invalid_TSS);
1077 set_intr_gate(11,&segment_not_present);
1078 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1079 set_intr_gate(13,&general_protection);
1080 set_intr_gate(14,&page_fault);
1081 set_intr_gate(15,&spurious_interrupt_bug);
1082 set_intr_gate(16,&coprocessor_error);
1083 set_intr_gate(17,&alignment_check);
1084#ifdef CONFIG_X86_MCE
1085 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1086#endif
1087 set_intr_gate(19,&simd_coprocessor_error);
1088
1089#ifdef CONFIG_IA32_EMULATION
1090 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1091#endif
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Should be a barrier for any external CPU state.
1095 */
1096 cpu_init();
1097}
1098
1099
1100/* Actual parsing is done early in setup.c. */
1101static int __init oops_dummy(char *s)
1102{
1103 panic_on_oops = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106__setup("oops=", oops_dummy);
1107
1108static int __init kstack_setup(char *s)
1109{
1110 kstack_depth_to_print = simple_strtoul(s,NULL,0);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001111 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113__setup("kstack=", kstack_setup);
1114
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001115static int __init call_trace_setup(char *s)
1116{
1117 if (strcmp(s, "old") == 0)
1118 call_trace = -1;
1119 else if (strcmp(s, "both") == 0)
1120 call_trace = 0;
1121 else if (strcmp(s, "new") == 0)
1122 call_trace = 1;
1123 return 1;
1124}
1125__setup("call_trace=", call_trace_setup);