blob: 86d8476be4fe9694b4c0ca9c55a4706eca5b54b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/i386/traps.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 */
9
10/*
11 * 'Traps.c' handles hardware traps and faults after we have saved some
12 * state in 'asm.s'.
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/string.h>
17#include <linux/errno.h>
18#include <linux/timer.h>
19#include <linux/mm.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/highmem.h>
25#include <linux/kallsyms.h>
26#include <linux/ptrace.h>
27#include <linux/utsname.h>
28#include <linux/kprobes.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070029#include <linux/kexec.h>
Jan Beulich176a2712006-06-26 13:57:41 +020030#include <linux/unwind.h>
Andrew Morton1e2af922006-09-27 01:51:15 -070031#include <linux/uaccess.h>
Dave Jonesa36df982006-12-07 02:14:12 +010032#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#ifdef CONFIG_EISA
35#include <linux/ioport.h>
36#include <linux/eisa.h>
37#endif
38
39#ifdef CONFIG_MCA
40#include <linux/mca.h>
41#endif
42
43#include <asm/processor.h>
44#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/io.h>
46#include <asm/atomic.h>
47#include <asm/debugreg.h>
48#include <asm/desc.h>
49#include <asm/i387.h>
50#include <asm/nmi.h>
Jan Beulich176a2712006-06-26 13:57:41 +020051#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/smp.h>
53#include <asm/arch_hooks.h>
54#include <asm/kdebug.h>
Andi Kleen2b14a782006-09-26 10:52:34 +020055#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/module.h>
58
59#include "mach_traps.h"
60
Andi Kleen29cbc782006-09-30 01:47:55 +020061int panic_on_unrecovered_nmi;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063asmlinkage int system_call(void);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Do we ignore FPU interrupts ? */
66char ignore_fpu_irq = 0;
67
68/*
69 * The IDT has to be page-aligned to simplify the Pentium
70 * F0 0F bug workaround.. We have a special link segment
71 * for this.
72 */
73struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
74
75asmlinkage void divide_error(void);
76asmlinkage void debug(void);
77asmlinkage void nmi(void);
78asmlinkage void int3(void);
79asmlinkage void overflow(void);
80asmlinkage void bounds(void);
81asmlinkage void invalid_op(void);
82asmlinkage void device_not_available(void);
83asmlinkage void coprocessor_segment_overrun(void);
84asmlinkage void invalid_TSS(void);
85asmlinkage void segment_not_present(void);
86asmlinkage void stack_segment(void);
87asmlinkage void general_protection(void);
88asmlinkage void page_fault(void);
89asmlinkage void coprocessor_error(void);
90asmlinkage void simd_coprocessor_error(void);
91asmlinkage void alignment_check(void);
92asmlinkage void spurious_interrupt_bug(void);
93asmlinkage void machine_check(void);
94
Chuck Ebbert0741f4d2006-12-07 02:14:11 +010095int kstack_depth_to_print = 24;
Jan Beulichea424052006-08-30 19:37:11 +020096#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +020097static int call_trace = 1;
Jan Beulichea424052006-08-30 19:37:11 +020098#else
99#define call_trace (-1)
100#endif
Alan Sterne041c682006-03-27 01:16:30 -0800101ATOMIC_NOTIFIER_HEAD(i386die_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103int register_die_notifier(struct notifier_block *nb)
104{
Jan Beulich101f12a2006-03-23 02:59:45 -0800105 vmalloc_sync_all();
Alan Sterne041c682006-03-27 01:16:30 -0800106 return atomic_notifier_chain_register(&i386die_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
Arjan van de Ven1454aed2006-07-10 04:44:05 -0700108EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alan Sterne041c682006-03-27 01:16:30 -0800110int unregister_die_notifier(struct notifier_block *nb)
111{
112 return atomic_notifier_chain_unregister(&i386die_chain, nb);
113}
Arjan van de Ven1454aed2006-07-10 04:44:05 -0700114EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
Alan Sterne041c682006-03-27 01:16:30 -0800115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
117{
118 return p > (void *)tinfo &&
119 p < (void *)tinfo + THREAD_SIZE - 3;
120}
121
122static inline unsigned long print_context_stack(struct thread_info *tinfo,
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800123 unsigned long *stack, unsigned long ebp,
Andi Kleen2b14a782006-09-26 10:52:34 +0200124 struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 unsigned long addr;
127
128#ifdef CONFIG_FRAME_POINTER
129 while (valid_stack_ptr(tinfo, (void *)ebp)) {
Linus Torvalds808dbbb2006-11-17 11:14:56 -0800130 unsigned long new_ebp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 addr = *(unsigned long *)(ebp + 4);
Andi Kleen2b14a782006-09-26 10:52:34 +0200132 ops->address(data, addr);
Ingo Molnarb88d4f12006-06-23 02:04:20 -0700133 /*
134 * break out of recursive entries (such as
Linus Torvalds808dbbb2006-11-17 11:14:56 -0800135 * end_of_stack_stop_unwind_function). Also,
136 * we can never allow a frame pointer to
137 * move downwards!
Ingo Molnarb88d4f12006-06-23 02:04:20 -0700138 */
Linus Torvalds808dbbb2006-11-17 11:14:56 -0800139 new_ebp = *(unsigned long *)ebp;
140 if (new_ebp <= ebp)
Ingo Molnarb88d4f12006-06-23 02:04:20 -0700141 break;
Linus Torvalds808dbbb2006-11-17 11:14:56 -0800142 ebp = new_ebp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144#else
145 while (valid_stack_ptr(tinfo, stack)) {
146 addr = *stack++;
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800147 if (__kernel_text_address(addr))
Andi Kleen2b14a782006-09-26 10:52:34 +0200148 ops->address(data, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150#endif
151 return ebp;
152}
153
Andi Kleen2b14a782006-09-26 10:52:34 +0200154struct ops_and_data {
155 struct stacktrace_ops *ops;
156 void *data;
157};
158
Ingo Molnarf0a5c312006-07-03 00:24:37 -0700159static asmlinkage int
Andi Kleen2b14a782006-09-26 10:52:34 +0200160dump_trace_unwind(struct unwind_frame_info *info, void *data)
Jan Beulich176a2712006-06-26 13:57:41 +0200161{
Andi Kleen2b14a782006-09-26 10:52:34 +0200162 struct ops_and_data *oad = (struct ops_and_data *)data;
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200163 int n = 0;
Jan Beulich176a2712006-06-26 13:57:41 +0200164
165 while (unwind(info) == 0 && UNW_PC(info)) {
Ingo Molnarf0a5c312006-07-03 00:24:37 -0700166 n++;
Andi Kleen2b14a782006-09-26 10:52:34 +0200167 oad->ops->address(oad->data, UNW_PC(info));
Jan Beulich176a2712006-06-26 13:57:41 +0200168 if (arch_unw_user_mode(info))
169 break;
170 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200171 return n;
Jan Beulich176a2712006-06-26 13:57:41 +0200172}
173
Andi Kleenb615ebd2006-12-07 02:14:00 +0100174#define MSG(msg) ops->warning(data, msg)
175
Andi Kleen2b14a782006-09-26 10:52:34 +0200176void dump_trace(struct task_struct *task, struct pt_regs *regs,
177 unsigned long *stack,
178 struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Andi Kleena32cf392006-09-26 10:52:34 +0200180 unsigned long ebp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (!task)
183 task = current;
184
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200185 if (call_trace >= 0) {
186 int unw_ret = 0;
187 struct unwind_frame_info info;
Andi Kleen2b14a782006-09-26 10:52:34 +0200188 struct ops_and_data oad = { .ops = ops, .data = data };
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200189
190 if (regs) {
191 if (unwind_init_frame_info(&info, task, regs) == 0)
Andi Kleen2b14a782006-09-26 10:52:34 +0200192 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200193 } else if (task == current)
Andi Kleenb615ebd2006-12-07 02:14:00 +0100194 unw_ret = unwind_init_running(&info, dump_trace_unwind,
195 &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200196 else {
197 if (unwind_init_blocked(&info, task) == 0)
Andi Kleen2b14a782006-09-26 10:52:34 +0200198 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulich176a2712006-06-26 13:57:41 +0200199 }
Jan Beulichea424052006-08-30 19:37:11 +0200200 if (unw_ret > 0) {
201 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
Andi Kleenb615ebd2006-12-07 02:14:00 +0100202 ops->warning_symbol(data,
Andi Kleendd315df2006-12-07 02:14:00 +0100203 "DWARF2 unwinder stuck at %s",
Jan Beulichea424052006-08-30 19:37:11 +0200204 UNW_PC(&info));
205 if (UNW_SP(&info) >= PAGE_OFFSET) {
Andi Kleendd315df2006-12-07 02:14:00 +0100206 MSG("Leftover inexact backtrace:");
Andi Kleen70583162006-07-29 21:42:52 +0200207 stack = (void *)UNW_SP(&info);
Andi Kleen950fee82006-09-26 10:52:34 +0200208 if (!stack)
209 return;
Andi Kleena32cf392006-09-26 10:52:34 +0200210 ebp = UNW_FP(&info);
Jan Beulichea424052006-08-30 19:37:11 +0200211 } else
Andi Kleendd315df2006-12-07 02:14:00 +0100212 MSG("Full inexact backtrace again:");
Jan Beulichea424052006-08-30 19:37:11 +0200213 } else if (call_trace >= 1)
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200214 return;
Andi Kleenc97d20a2006-07-28 14:44:57 +0200215 else
Andi Kleendd315df2006-12-07 02:14:00 +0100216 MSG("Full inexact backtrace again:");
Jan Beulichea424052006-08-30 19:37:11 +0200217 } else
Andi Kleendd315df2006-12-07 02:14:00 +0100218 MSG("Inexact backtrace:");
Andi Kleena32cf392006-09-26 10:52:34 +0200219 }
220 if (!stack) {
Andi Kleen2b14a782006-09-26 10:52:34 +0200221 unsigned long dummy;
222 stack = &dummy;
223 if (task && task != current)
224 stack = (unsigned long *)task->thread.esp;
Jan Beulich176a2712006-06-26 13:57:41 +0200225 }
226
Andi Kleena32cf392006-09-26 10:52:34 +0200227#ifdef CONFIG_FRAME_POINTER
228 if (!ebp) {
229 if (task == current) {
230 /* Grab ebp right from our regs */
231 asm ("movl %%ebp, %0" : "=r" (ebp) : );
232 } else {
233 /* ebp is the last reg pushed by switch_to */
234 ebp = *(unsigned long *) task->thread.esp;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Andi Kleena32cf392006-09-26 10:52:34 +0200237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 while (1) {
240 struct thread_info *context;
241 context = (struct thread_info *)
242 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
Andi Kleen2b14a782006-09-26 10:52:34 +0200243 ebp = print_context_stack(context, stack, ebp, ops, data);
244 /* Should be after the line below, but somewhere
245 in early boot context comes out corrupted and we
246 can't reference it -AK */
247 if (ops->stack(data, "IRQ") < 0)
248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 stack = (unsigned long*)context->previous_esp;
250 if (!stack)
251 break;
Dave Jonesa36df982006-12-07 02:14:12 +0100252 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
Andi Kleen2b14a782006-09-26 10:52:34 +0200255EXPORT_SYMBOL(dump_trace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Andi Kleen2b14a782006-09-26 10:52:34 +0200257static void
258print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
259{
260 printk(data);
261 print_symbol(msg, symbol);
262 printk("\n");
263}
264
265static void print_trace_warning(void *data, char *msg)
266{
267 printk("%s%s\n", (char *)data, msg);
268}
269
270static int print_trace_stack(void *data, char *name)
271{
272 return 0;
273}
274
275/*
276 * Print one address/symbol entries per line.
277 */
278static void print_trace_address(void *data, unsigned long addr)
279{
280 printk("%s [<%08lx>] ", (char *)data, addr);
281 print_symbol("%s\n", addr);
282}
283
284static struct stacktrace_ops print_trace_ops = {
285 .warning = print_trace_warning,
286 .warning_symbol = print_trace_warning_symbol,
287 .stack = print_trace_stack,
288 .address = print_trace_address,
289};
290
291static void
292show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
293 unsigned long * stack, char *log_lvl)
294{
295 dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
296 printk("%s =======================\n", log_lvl);
297}
298
299void show_trace(struct task_struct *task, struct pt_regs *regs,
300 unsigned long * stack)
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800301{
Jan Beulich176a2712006-06-26 13:57:41 +0200302 show_trace_log_lvl(task, regs, stack, "");
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800303}
304
Jan Beulich176a2712006-06-26 13:57:41 +0200305static void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
306 unsigned long *esp, char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 unsigned long *stack;
309 int i;
310
311 if (esp == NULL) {
312 if (task)
313 esp = (unsigned long*)task->thread.esp;
314 else
315 esp = (unsigned long *)&esp;
316 }
317
318 stack = esp;
319 for(i = 0; i < kstack_depth_to_print; i++) {
320 if (kstack_end(stack))
321 break;
Chuck Ebbert75874d52006-03-23 02:59:52 -0800322 if (i && ((i % 8) == 0))
323 printk("\n%s ", log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 printk("%08lx ", *stack++);
325 }
Chuck Ebbert75874d52006-03-23 02:59:52 -0800326 printk("\n%sCall Trace:\n", log_lvl);
Jan Beulich176a2712006-06-26 13:57:41 +0200327 show_trace_log_lvl(task, regs, esp, log_lvl);
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800328}
329
330void show_stack(struct task_struct *task, unsigned long *esp)
331{
Chuck Ebbert75874d52006-03-23 02:59:52 -0800332 printk(" ");
Jan Beulich176a2712006-06-26 13:57:41 +0200333 show_stack_log_lvl(task, NULL, esp, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336/*
337 * The architecture-independent dump_stack generator
338 */
339void dump_stack(void)
340{
341 unsigned long stack;
342
Jan Beulich176a2712006-06-26 13:57:41 +0200343 show_trace(current, NULL, &stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346EXPORT_SYMBOL(dump_stack);
347
348void show_registers(struct pt_regs *regs)
349{
350 int i;
351 int in_kernel = 1;
352 unsigned long esp;
353 unsigned short ss;
354
355 esp = (unsigned long) (&regs->esp);
Zachary Amsden0998e422005-09-03 15:56:43 -0700356 savesegment(ss, ss);
Jan Beulichdb753bd2006-03-23 02:59:46 -0800357 if (user_mode_vm(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 in_kernel = 0;
359 esp = regs->esp;
360 ss = regs->xss & 0xffff;
361 }
362 print_modules();
Dave Jonesf354b3a2006-09-26 10:52:41 +0200363 printk(KERN_EMERG "CPU: %d\n"
364 KERN_EMERG "EIP: %04x:[<%08lx>] %s VLI\n"
365 KERN_EMERG "EFLAGS: %08lx (%s %.*s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700367 print_tainted(), regs->eflags, init_utsname()->release,
368 (int)strcspn(init_utsname()->version, " "),
369 init_utsname()->version);
Dave Jones9c107802006-01-09 20:51:32 -0800370 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
371 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 regs->eax, regs->ebx, regs->ecx, regs->edx);
Dave Jones9c107802006-01-09 20:51:32 -0800373 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 regs->esi, regs->edi, regs->ebp, esp);
Dave Jones9c107802006-01-09 20:51:32 -0800375 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 regs->xds & 0xffff, regs->xes & 0xffff, ss);
Chuck Ebbert7e04a112006-06-23 02:04:31 -0700377 printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
378 TASK_COMM_LEN, current->comm, current->pid,
379 current_thread_info(), current, current->thread_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * When in-kernel, we also print out the stack and code at the
382 * time of the fault..
383 */
384 if (in_kernel) {
Andi Kleen11a41802006-12-07 02:14:06 +0100385 u8 *eip;
Chuck Ebbert99325322006-09-25 23:32:19 -0700386 int code_bytes = 64;
387 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Dave Jones9c107802006-01-09 20:51:32 -0800389 printk("\n" KERN_EMERG "Stack: ");
Jan Beulich176a2712006-06-26 13:57:41 +0200390 show_stack_log_lvl(NULL, regs, (unsigned long *)esp, KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Dave Jones9c107802006-01-09 20:51:32 -0800392 printk(KERN_EMERG "Code: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Andi Kleen11a41802006-12-07 02:14:06 +0100394 eip = (u8 *)regs->eip - 43;
395 if (eip < (u8 *)PAGE_OFFSET ||
396 probe_kernel_address(eip, c)) {
Chuck Ebbert99325322006-09-25 23:32:19 -0700397 /* try starting at EIP */
Andi Kleen11a41802006-12-07 02:14:06 +0100398 eip = (u8 *)regs->eip;
Chuck Ebbert99325322006-09-25 23:32:19 -0700399 code_bytes = 32;
400 }
401 for (i = 0; i < code_bytes; i++, eip++) {
Andi Kleen11a41802006-12-07 02:14:06 +0100402 if (eip < (u8 *)PAGE_OFFSET ||
403 probe_kernel_address(eip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 printk(" Bad EIP value.");
405 break;
406 }
Andi Kleen11a41802006-12-07 02:14:06 +0100407 if (eip == (u8 *)regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 printk("<%02x> ", c);
409 else
410 printk("%02x ", c);
411 }
412 }
413 printk("\n");
414}
415
416static void handle_BUG(struct pt_regs *regs)
417{
Chuck Ebbertb7015332006-07-14 00:23:58 -0700418 unsigned long eip = regs->eip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (eip < PAGE_OFFSET)
Chuck Ebbertb7015332006-07-14 00:23:58 -0700422 return;
Andi Kleen11a41802006-12-07 02:14:06 +0100423 if (probe_kernel_address((unsigned short *)eip, ud2))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700424 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (ud2 != 0x0b0f)
Chuck Ebbertb7015332006-07-14 00:23:58 -0700426 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dave Jones9c107802006-01-09 20:51:32 -0800428 printk(KERN_EMERG "------------[ cut here ]------------\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Chuck Ebbertb7015332006-07-14 00:23:58 -0700430#ifdef CONFIG_DEBUG_BUGVERBOSE
431 do {
432 unsigned short line;
433 char *file;
434 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Andi Kleen11a41802006-12-07 02:14:06 +0100436 if (probe_kernel_address((unsigned short *)(eip + 2), line))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700437 break;
Andi Kleen11a41802006-12-07 02:14:06 +0100438 if (probe_kernel_address((char **)(eip + 4), file) ||
439 (unsigned long)file < PAGE_OFFSET ||
440 probe_kernel_address(file, c))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700441 file = "<bad filename>";
442
443 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
444 return;
445 } while (0);
446#endif
447 printk(KERN_EMERG "Kernel BUG at [verbose debug info unavailable]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700450/* This is gone through when something in the kernel
451 * has done something bad and is about to be terminated.
452*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453void die(const char * str, struct pt_regs * regs, long err)
454{
455 static struct {
456 spinlock_t lock;
457 u32 lock_owner;
458 int lock_owner_depth;
459 } die = {
460 .lock = SPIN_LOCK_UNLOCKED,
461 .lock_owner = -1,
462 .lock_owner_depth = 0
463 };
464 static int die_counter;
Jan Beuliche43d6742006-01-06 00:11:48 -0800465 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Andrew Mortondd287792006-03-23 03:00:57 -0800467 oops_enter();
468
Ingo Molnar39c715b2005-06-21 17:14:34 -0700469 if (die.lock_owner != raw_smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 console_verbose();
Jan Beuliche43d6742006-01-06 00:11:48 -0800471 spin_lock_irqsave(&die.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 die.lock_owner = smp_processor_id();
473 die.lock_owner_depth = 0;
474 bust_spinlocks(1);
475 }
Jan Beuliche43d6742006-01-06 00:11:48 -0800476 else
477 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (++die.lock_owner_depth < 3) {
480 int nl = 0;
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700481 unsigned long esp;
482 unsigned short ss;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 handle_BUG(regs);
Dave Jones9c107802006-01-09 20:51:32 -0800485 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#ifdef CONFIG_PREEMPT
Dave Jones9c107802006-01-09 20:51:32 -0800487 printk(KERN_EMERG "PREEMPT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 nl = 1;
489#endif
490#ifdef CONFIG_SMP
Dave Jones9c107802006-01-09 20:51:32 -0800491 if (!nl)
492 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 printk("SMP ");
494 nl = 1;
495#endif
496#ifdef CONFIG_DEBUG_PAGEALLOC
Dave Jones9c107802006-01-09 20:51:32 -0800497 if (!nl)
498 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 printk("DEBUG_PAGEALLOC");
500 nl = 1;
501#endif
502 if (nl)
503 printk("\n");
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800504 if (notify_die(DIE_OOPS, str, regs, err,
505 current->thread.trap_no, SIGSEGV) !=
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700506 NOTIFY_STOP) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800507 show_registers(regs);
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700508 /* Executive summary in case the oops scrolled away */
509 esp = (unsigned long) (&regs->esp);
510 savesegment(ss, ss);
511 if (user_mode(regs)) {
512 esp = regs->esp;
513 ss = regs->xss & 0xffff;
514 }
515 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
516 print_symbol("%s", regs->eip);
517 printk(" SS:ESP %04x:%08lx\n", ss, esp);
518 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800519 else
520 regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else
Dave Jones9c107802006-01-09 20:51:32 -0800522 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 bust_spinlocks(0);
525 die.lock_owner = -1;
Jan Beuliche43d6742006-01-06 00:11:48 -0800526 spin_unlock_irqrestore(&die.lock, flags);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700527
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800528 if (!regs)
529 return;
530
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700531 if (kexec_should_crash(current))
532 crash_kexec(regs);
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (in_interrupt())
535 panic("Fatal exception in interrupt");
536
Hormscea6a4b2006-07-30 03:03:34 -0700537 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700538 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700539
Andrew Mortondd287792006-03-23 03:00:57 -0800540 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 do_exit(SIGSEGV);
542}
543
544static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
545{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700546 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 die(str, regs, err);
548}
549
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700550static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
551 struct pt_regs * regs, long error_code,
552 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700554 struct task_struct *tsk = current;
555 tsk->thread.error_code = error_code;
556 tsk->thread.trap_no = trapnr;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (regs->eflags & VM_MASK) {
559 if (vm86)
560 goto vm86_trap;
561 goto trap_signal;
562 }
563
Vincent Hanquez717b5942005-06-23 00:08:45 -0700564 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto kernel_trap;
566
567 trap_signal: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (info)
569 force_sig_info(signr, info, tsk);
570 else
571 force_sig(signr, tsk);
572 return;
573 }
574
575 kernel_trap: {
576 if (!fixup_exception(regs))
577 die(str, regs, error_code);
578 return;
579 }
580
581 vm86_trap: {
582 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
583 if (ret) goto trap_signal;
584 return;
585 }
586}
587
588#define DO_ERROR(trapnr, signr, str, name) \
589fastcall void do_##name(struct pt_regs * regs, long error_code) \
590{ \
591 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
592 == NOTIFY_STOP) \
593 return; \
594 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
595}
596
597#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
598fastcall void do_##name(struct pt_regs * regs, long error_code) \
599{ \
600 siginfo_t info; \
601 info.si_signo = signr; \
602 info.si_errno = 0; \
603 info.si_code = sicode; \
604 info.si_addr = (void __user *)siaddr; \
605 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
606 == NOTIFY_STOP) \
607 return; \
608 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
609}
610
611#define DO_VM86_ERROR(trapnr, signr, str, name) \
612fastcall void do_##name(struct pt_regs * regs, long error_code) \
613{ \
614 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
615 == NOTIFY_STOP) \
616 return; \
617 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
618}
619
620#define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
621fastcall void do_##name(struct pt_regs * regs, long error_code) \
622{ \
623 siginfo_t info; \
624 info.si_signo = signr; \
625 info.si_errno = 0; \
626 info.si_code = sicode; \
627 info.si_addr = (void __user *)siaddr; \
628 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
629 == NOTIFY_STOP) \
630 return; \
631 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
632}
633
634DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
635#ifndef CONFIG_KPROBES
636DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
637#endif
638DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
639DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert631b0342006-01-03 22:36:14 -0500640DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
642DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
643DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
644DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
645DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvaldsa879cbb2005-04-29 09:38:44 -0700646DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700648fastcall void __kprobes do_general_protection(struct pt_regs * regs,
649 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 int cpu = get_cpu();
652 struct tss_struct *tss = &per_cpu(init_tss, cpu);
653 struct thread_struct *thread = &current->thread;
654
655 /*
656 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
657 * invalid offset set (the LAZY one) and the faulting thread has
658 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
659 * and we set the offset field correctly. Then we let the CPU to
660 * restart the faulting instruction.
661 */
662 if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
663 thread->io_bitmap_ptr) {
664 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
665 thread->io_bitmap_max);
666 /*
667 * If the previously set map was extending to higher ports
668 * than the current one, pad extra space with 0xff (no access).
669 */
670 if (thread->io_bitmap_max < tss->io_bitmap_max)
671 memset((char *) tss->io_bitmap +
672 thread->io_bitmap_max, 0xff,
673 tss->io_bitmap_max - thread->io_bitmap_max);
674 tss->io_bitmap_max = thread->io_bitmap_max;
675 tss->io_bitmap_base = IO_BITMAP_OFFSET;
Bart Oldemand5cd4aa2005-10-30 14:59:29 -0800676 tss->io_bitmap_owner = thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 put_cpu();
678 return;
679 }
680 put_cpu();
681
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700682 current->thread.error_code = error_code;
683 current->thread.trap_no = 13;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (regs->eflags & VM_MASK)
686 goto gp_in_vm86;
687
Vincent Hanquez717b5942005-06-23 00:08:45 -0700688 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto gp_in_kernel;
690
691 current->thread.error_code = error_code;
692 current->thread.trap_no = 13;
693 force_sig(SIGSEGV, current);
694 return;
695
696gp_in_vm86:
697 local_irq_enable();
698 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
699 return;
700
701gp_in_kernel:
702 if (!fixup_exception(regs)) {
703 if (notify_die(DIE_GPF, "general protection fault", regs,
704 error_code, 13, SIGSEGV) == NOTIFY_STOP)
705 return;
706 die("general protection fault", regs, error_code);
707 }
708}
709
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200710static __kprobes void
711mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200713 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
714 "CPU %d.\n", reason, smp_processor_id());
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100715 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200716 if (panic_on_unrecovered_nmi)
717 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Don Zickusc41c5cd2006-09-26 10:52:27 +0200719 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /* Clear and disable the memory parity error line. */
722 clear_mem_error(reason);
723}
724
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200725static __kprobes void
726io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 unsigned long i;
729
Dave Jones9c107802006-01-09 20:51:32 -0800730 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 show_registers(regs);
732
733 /* Re-enable the IOCK line, wait for a few seconds */
734 reason = (reason & 0xf) | 8;
735 outb(reason, 0x61);
736 i = 2000;
737 while (--i) udelay(1000);
738 reason &= ~8;
739 outb(reason, 0x61);
740}
741
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200742static __kprobes void
743unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745#ifdef CONFIG_MCA
746 /* Might actually be able to figure out what the guilty party
747 * is. */
748 if( MCA_bus ) {
749 mca_handle_nmi();
750 return;
751 }
752#endif
Don Zickusc41c5cd2006-09-26 10:52:27 +0200753 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
754 "CPU %d.\n", reason, smp_processor_id());
755 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200756 if (panic_on_unrecovered_nmi)
757 panic("NMI: Not continuing");
758
Don Zickusc41c5cd2006-09-26 10:52:27 +0200759 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static DEFINE_SPINLOCK(nmi_print_lock);
763
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200764void __kprobes die_nmi(struct pt_regs *regs, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800766 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
George Anzinger748f2ed2005-09-03 15:56:48 -0700767 NOTIFY_STOP)
768 return;
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 spin_lock(&nmi_print_lock);
771 /*
772 * We are in trouble anyway, lets at least try
773 * to get a message out.
774 */
775 bust_spinlocks(1);
Dave Jones9c107802006-01-09 20:51:32 -0800776 printk(KERN_EMERG "%s", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 printk(" on CPU%d, eip %08lx, registers:\n",
778 smp_processor_id(), regs->eip);
779 show_registers(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 console_silent();
781 spin_unlock(&nmi_print_lock);
782 bust_spinlocks(0);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700783
784 /* If we are in kernel we are probably nested up pretty bad
785 * and might aswell get out now while we still can.
786 */
Jan Beulichdb753bd2006-03-23 02:59:46 -0800787 if (!user_mode_vm(regs)) {
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700788 current->thread.trap_no = 2;
789 crash_kexec(regs);
790 }
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 do_exit(SIGSEGV);
793}
794
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200795static __kprobes void default_do_nmi(struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 unsigned char reason = 0;
798
799 /* Only the BSP gets external NMIs from the system. */
800 if (!smp_processor_id())
801 reason = get_nmi_reason();
802
803 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800804 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 == NOTIFY_STOP)
806 return;
807#ifdef CONFIG_X86_LOCAL_APIC
808 /*
809 * Ok, so this is none of the documented NMI sources,
810 * so it must be the NMI watchdog.
811 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200812 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200814 if (!do_nmi_callback(regs, smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#endif
Don Zickus3adbbcc2006-09-26 10:52:26 +0200816 unknown_nmi_error(reason, regs);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return;
819 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800820 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return;
822 if (reason & 0x80)
823 mem_parity_error(reason, regs);
824 if (reason & 0x40)
825 io_check_error(reason, regs);
826 /*
827 * Reassert NMI in case it became active meanwhile
828 * as it's edge-triggered.
829 */
830 reassert_nmi();
831}
832
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200833fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int cpu;
836
837 nmi_enter();
838
839 cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ++nmi_count(cpu);
842
Don Zickus3adbbcc2006-09-26 10:52:26 +0200843 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 nmi_exit();
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#ifdef CONFIG_KPROBES
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700849fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
852 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700853 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* This is an interrupt gate, because kprobes wants interrupts
855 disabled. Normal trap handlers don't. */
856 restore_interrupts(regs);
857 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859#endif
860
861/*
862 * Our handling of the processor debug registers is non-trivial.
863 * We do not clear them on entry and exit from the kernel. Therefore
864 * it is possible to get a watchpoint trap here from inside the kernel.
865 * However, the code in ./ptrace.c has ensured that the user can
866 * only set watchpoints on userspace addresses. Therefore the in-kernel
867 * watchpoint trap can only occur in code which is reading/writing
868 * from user space. Such code must not hold kernel locks (since it
869 * can equally take a page fault), therefore it is safe to call
870 * force_sig_info even though that claims and releases locks.
871 *
872 * Code in ./signal.c ensures that the debug control register
873 * is restored before we deliver any signal, and therefore that
874 * user code runs with the correct debug control register even though
875 * we clear it here.
876 *
877 * Being careful here means that we don't have to be as careful in a
878 * lot of more complicated places (task switching can be a bit lazy
879 * about restoring all the debug state, and ptrace doesn't have to
880 * find every occurrence of the TF bit that could be saved away even
881 * by user code)
882 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700883fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 unsigned int condition;
886 struct task_struct *tsk = current;
887
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700888 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
891 SIGTRAP) == NOTIFY_STOP)
892 return;
893 /* It's safe to allow irq's after DR6 has been saved */
894 if (regs->eflags & X86_EFLAGS_IF)
895 local_irq_enable();
896
897 /* Mask out spurious debug traps due to lazy DR7 setting */
898 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
899 if (!tsk->thread.debugreg[7])
900 goto clear_dr7;
901 }
902
903 if (regs->eflags & VM_MASK)
904 goto debug_vm86;
905
906 /* Save debug status register where ptrace can see it */
907 tsk->thread.debugreg[6] = condition;
908
909 /*
910 * Single-stepping through TF: make sure we ignore any events in
911 * kernel space (but re-enable TF when returning to user mode).
912 */
913 if (condition & DR_STEP) {
914 /*
915 * We already checked v86 mode above, so we can
916 * check for kernel mode by just checking the CPL
917 * of CS.
918 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700919 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto clear_TF_reenable;
921 }
922
923 /* Ok, finally something we can handle */
924 send_sigtrap(tsk, regs, error_code);
925
926 /* Disable additional traps. They'll be re-enabled when
927 * the signal is delivered.
928 */
929clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700930 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return;
932
933debug_vm86:
934 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
935 return;
936
937clear_TF_reenable:
938 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
939 regs->eflags &= ~TF_MASK;
940 return;
941}
942
943/*
944 * Note that we play around with the 'TS' bit in an attempt to get
945 * the correct behaviour even in the presence of the asynchronous
946 * IRQ13 behaviour
947 */
948void math_error(void __user *eip)
949{
950 struct task_struct * task;
951 siginfo_t info;
952 unsigned short cwd, swd;
953
954 /*
955 * Save the info for the exception handler and clear the error.
956 */
957 task = current;
958 save_init_fpu(task);
959 task->thread.trap_no = 16;
960 task->thread.error_code = 0;
961 info.si_signo = SIGFPE;
962 info.si_errno = 0;
963 info.si_code = __SI_FAULT;
964 info.si_addr = eip;
965 /*
966 * (~cwd & swd) will mask out exceptions that are not set to unmasked
967 * status. 0x3f is the exception bits in these regs, 0x200 is the
968 * C1 reg you need in case of a stack fault, 0x040 is the stack
969 * fault bit. We should only be taking one exception at a time,
970 * so if this combination doesn't produce any single exception,
971 * then we have a bad program that isn't syncronizing its FPU usage
972 * and it will suffer the consequences since we won't be able to
973 * fully reproduce the context of the exception
974 */
975 cwd = get_fpu_cwd(task);
976 swd = get_fpu_swd(task);
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400977 switch (swd & ~cwd & 0x3f) {
Chuck Ebbert33333372005-09-13 04:55:41 -0400978 case 0x000: /* No unmasked exception */
979 return;
980 default: /* Multiple exceptions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 case 0x001: /* Invalid Op */
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400983 /*
984 * swd & 0x240 == 0x040: Stack Underflow
985 * swd & 0x240 == 0x240: Stack Overflow
986 * User must clear the SF bit (0x40) if set
987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 info.si_code = FPE_FLTINV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
990 case 0x002: /* Denormalize */
991 case 0x010: /* Underflow */
992 info.si_code = FPE_FLTUND;
993 break;
994 case 0x004: /* Zero Divide */
995 info.si_code = FPE_FLTDIV;
996 break;
997 case 0x008: /* Overflow */
998 info.si_code = FPE_FLTOVF;
999 break;
1000 case 0x020: /* Precision */
1001 info.si_code = FPE_FLTRES;
1002 break;
1003 }
1004 force_sig_info(SIGFPE, &info, task);
1005}
1006
1007fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
1008{
1009 ignore_fpu_irq = 1;
1010 math_error((void __user *)regs->eip);
1011}
1012
1013static void simd_math_error(void __user *eip)
1014{
1015 struct task_struct * task;
1016 siginfo_t info;
1017 unsigned short mxcsr;
1018
1019 /*
1020 * Save the info for the exception handler and clear the error.
1021 */
1022 task = current;
1023 save_init_fpu(task);
1024 task->thread.trap_no = 19;
1025 task->thread.error_code = 0;
1026 info.si_signo = SIGFPE;
1027 info.si_errno = 0;
1028 info.si_code = __SI_FAULT;
1029 info.si_addr = eip;
1030 /*
1031 * The SIMD FPU exceptions are handled a little differently, as there
1032 * is only a single status/control register. Thus, to determine which
1033 * unmasked exception was caught we must mask the exception mask bits
1034 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1035 */
1036 mxcsr = get_fpu_mxcsr(task);
1037 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1038 case 0x000:
1039 default:
1040 break;
1041 case 0x001: /* Invalid Op */
1042 info.si_code = FPE_FLTINV;
1043 break;
1044 case 0x002: /* Denormalize */
1045 case 0x010: /* Underflow */
1046 info.si_code = FPE_FLTUND;
1047 break;
1048 case 0x004: /* Zero Divide */
1049 info.si_code = FPE_FLTDIV;
1050 break;
1051 case 0x008: /* Overflow */
1052 info.si_code = FPE_FLTOVF;
1053 break;
1054 case 0x020: /* Precision */
1055 info.si_code = FPE_FLTRES;
1056 break;
1057 }
1058 force_sig_info(SIGFPE, &info, task);
1059}
1060
1061fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
1062 long error_code)
1063{
1064 if (cpu_has_xmm) {
1065 /* Handle SIMD FPU exceptions on PIII+ processors. */
1066 ignore_fpu_irq = 1;
1067 simd_math_error((void __user *)regs->eip);
1068 } else {
1069 /*
1070 * Handle strange cache flush from user space exception
1071 * in all other cases. This is undocumented behaviour.
1072 */
1073 if (regs->eflags & VM_MASK) {
1074 handle_vm86_fault((struct kernel_vm86_regs *)regs,
1075 error_code);
1076 return;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 current->thread.trap_no = 19;
1079 current->thread.error_code = error_code;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -07001080 die_if_kernel("cache flush denied", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 force_sig(SIGSEGV, current);
1082 }
1083}
1084
1085fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
1086 long error_code)
1087{
1088#if 0
1089 /* No need to warn about this any longer. */
1090 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1091#endif
1092}
1093
Stas Sergeevbe44d2a2006-12-07 02:14:01 +01001094fastcall unsigned long patch_espfix_desc(unsigned long uesp,
1095 unsigned long kesp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int cpu = smp_processor_id();
Stas Sergeevbe44d2a2006-12-07 02:14:01 +01001098 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
1099 struct desc_struct *gdt = (struct desc_struct *)cpu_gdt_descr->address;
1100 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
1101 unsigned long new_kesp = kesp - base;
1102 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
1103 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
1104 /* Set up base for espfix segment */
1105 desc &= 0x00f0ff0000000000ULL;
1106 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
1107 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
1108 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
1109 (lim_pages & 0xffff);
1110 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
1111 return new_kesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114/*
1115 * 'math_state_restore()' saves the current math information in the
1116 * old math state array, and gets the new ones from the current task
1117 *
1118 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1119 * Don't touch unless you *really* know how it works.
1120 *
1121 * Must be called with kernel preemption disabled (in this case,
1122 * local interrupts are disabled at the call-site in entry.S).
1123 */
Chuck Ebbertacc20762006-12-07 02:14:01 +01001124asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct thread_info *thread = current_thread_info();
1127 struct task_struct *tsk = thread->task;
1128
1129 clts(); /* Allow maths ops (or we recurse) */
1130 if (!tsk_used_math(tsk))
1131 init_fpu(tsk);
1132 restore_fpu(tsk);
1133 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
Chuck Ebbertacc20762006-12-07 02:14:01 +01001134 tsk->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137#ifndef CONFIG_MATH_EMULATION
1138
1139asmlinkage void math_emulate(long arg)
1140{
Dave Jones9c107802006-01-09 20:51:32 -08001141 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1142 printk(KERN_EMERG "killing %s.\n",current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 force_sig(SIGFPE,current);
1144 schedule();
1145}
1146
1147#endif /* CONFIG_MATH_EMULATION */
1148
1149#ifdef CONFIG_X86_F00F_BUG
1150void __init trap_init_f00f_bug(void)
1151{
1152 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1153
1154 /*
1155 * Update the IDT descriptor and reload the IDT so that
1156 * it uses the read-only mapped virtual address.
1157 */
1158 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -07001159 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161#endif
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * This needs to use 'idt_table' rather than 'idt', and
1165 * thus use the _nonmapped_ version of the IDT, as the
1166 * Pentium F0 0F bugfix can have resulted in the mapped
1167 * IDT being write-protected.
1168 */
1169void set_intr_gate(unsigned int n, void *addr)
1170{
Rusty Russell522e93e2006-09-26 10:52:35 +02001171 _set_gate(n, DESCTYPE_INT, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174/*
1175 * This routine sets up an interrupt gate at directory privilege level 3.
1176 */
1177static inline void set_system_intr_gate(unsigned int n, void *addr)
1178{
Rusty Russell522e93e2006-09-26 10:52:35 +02001179 _set_gate(n, DESCTYPE_INT | DESCTYPE_DPL3, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182static void __init set_trap_gate(unsigned int n, void *addr)
1183{
Rusty Russell522e93e2006-09-26 10:52:35 +02001184 _set_gate(n, DESCTYPE_TRAP, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static void __init set_system_gate(unsigned int n, void *addr)
1188{
Rusty Russell522e93e2006-09-26 10:52:35 +02001189 _set_gate(n, DESCTYPE_TRAP | DESCTYPE_DPL3, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
1193{
Rusty Russell522e93e2006-09-26 10:52:35 +02001194 _set_gate(n, DESCTYPE_TASK, (void *)0, (gdt_entry<<3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197
1198void __init trap_init(void)
1199{
1200#ifdef CONFIG_EISA
1201 void __iomem *p = ioremap(0x0FFFD9, 4);
1202 if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
1203 EISA_bus = 1;
1204 }
1205 iounmap(p);
1206#endif
1207
1208#ifdef CONFIG_X86_LOCAL_APIC
1209 init_apic_mappings();
1210#endif
1211
1212 set_trap_gate(0,&divide_error);
1213 set_intr_gate(1,&debug);
1214 set_intr_gate(2,&nmi);
Jan Beulicheb05c322006-01-06 00:11:49 -08001215 set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 set_system_gate(4,&overflow);
Jan Beulicheb05c322006-01-06 00:11:49 -08001217 set_trap_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 set_trap_gate(6,&invalid_op);
1219 set_trap_gate(7,&device_not_available);
1220 set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1221 set_trap_gate(9,&coprocessor_segment_overrun);
1222 set_trap_gate(10,&invalid_TSS);
1223 set_trap_gate(11,&segment_not_present);
1224 set_trap_gate(12,&stack_segment);
1225 set_trap_gate(13,&general_protection);
1226 set_intr_gate(14,&page_fault);
1227 set_trap_gate(15,&spurious_interrupt_bug);
1228 set_trap_gate(16,&coprocessor_error);
1229 set_trap_gate(17,&alignment_check);
1230#ifdef CONFIG_X86_MCE
1231 set_trap_gate(18,&machine_check);
1232#endif
1233 set_trap_gate(19,&simd_coprocessor_error);
1234
Jan Beulichd43c6e82006-01-06 00:11:49 -08001235 if (cpu_has_fxsr) {
1236 /*
1237 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1238 * Generates a compile-time "error: zero width for bit-field" if
1239 * the alignment is wrong.
1240 */
1241 struct fxsrAlignAssert {
1242 int _:!(offsetof(struct task_struct,
1243 thread.i387.fxsave) & 15);
1244 };
1245
1246 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1247 set_in_cr4(X86_CR4_OSFXSR);
1248 printk("done.\n");
1249 }
1250 if (cpu_has_xmm) {
1251 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1252 "support... ");
1253 set_in_cr4(X86_CR4_OSXMMEXCPT);
1254 printk("done.\n");
1255 }
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 set_system_gate(SYSCALL_VECTOR,&system_call);
1258
1259 /*
1260 * Should be a barrier for any external CPU state.
1261 */
1262 cpu_init();
1263
1264 trap_init_hook();
1265}
1266
1267static int __init kstack_setup(char *s)
1268{
1269 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001270 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272__setup("kstack=", kstack_setup);
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001273
Jan Beulichea424052006-08-30 19:37:11 +02001274#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001275static int __init call_trace_setup(char *s)
1276{
1277 if (strcmp(s, "old") == 0)
1278 call_trace = -1;
1279 else if (strcmp(s, "both") == 0)
1280 call_trace = 0;
Andi Kleen70583162006-07-29 21:42:52 +02001281 else if (strcmp(s, "newfallback") == 0)
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001282 call_trace = 1;
Andi Kleen70583162006-07-29 21:42:52 +02001283 else if (strcmp(s, "new") == 2)
1284 call_trace = 2;
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001285 return 1;
1286}
1287__setup("call_trace=", call_trace_setup);
Jan Beulichea424052006-08-30 19:37:11 +02001288#endif