blob: c447807e2a45dd5911942a12674246d72468297e [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 Beulich359ad0d2006-12-07 02:14:13 +0100164 unsigned long sp = UNW_SP(info);
Jan Beulich176a2712006-06-26 13:57:41 +0200165
Jan Beulich359ad0d2006-12-07 02:14:13 +0100166 if (arch_unw_user_mode(info))
167 return -1;
Jan Beulich176a2712006-06-26 13:57:41 +0200168 while (unwind(info) == 0 && UNW_PC(info)) {
Ingo Molnarf0a5c312006-07-03 00:24:37 -0700169 n++;
Andi Kleen2b14a782006-09-26 10:52:34 +0200170 oad->ops->address(oad->data, UNW_PC(info));
Jan Beulich176a2712006-06-26 13:57:41 +0200171 if (arch_unw_user_mode(info))
172 break;
Jan Beulich359ad0d2006-12-07 02:14:13 +0100173 if ((sp & ~(PAGE_SIZE - 1)) == (UNW_SP(info) & ~(PAGE_SIZE - 1))
174 && sp > UNW_SP(info))
175 break;
176 sp = UNW_SP(info);
Jan Beulich176a2712006-06-26 13:57:41 +0200177 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200178 return n;
Jan Beulich176a2712006-06-26 13:57:41 +0200179}
180
Andi Kleenb615ebd2006-12-07 02:14:00 +0100181#define MSG(msg) ops->warning(data, msg)
182
Andi Kleen2b14a782006-09-26 10:52:34 +0200183void dump_trace(struct task_struct *task, struct pt_regs *regs,
184 unsigned long *stack,
185 struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Andi Kleena32cf392006-09-26 10:52:34 +0200187 unsigned long ebp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if (!task)
190 task = current;
191
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200192 if (call_trace >= 0) {
193 int unw_ret = 0;
194 struct unwind_frame_info info;
Andi Kleen2b14a782006-09-26 10:52:34 +0200195 struct ops_and_data oad = { .ops = ops, .data = data };
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200196
197 if (regs) {
198 if (unwind_init_frame_info(&info, task, regs) == 0)
Andi Kleen2b14a782006-09-26 10:52:34 +0200199 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200200 } else if (task == current)
Andi Kleenb615ebd2006-12-07 02:14:00 +0100201 unw_ret = unwind_init_running(&info, dump_trace_unwind,
202 &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200203 else {
204 if (unwind_init_blocked(&info, task) == 0)
Andi Kleen2b14a782006-09-26 10:52:34 +0200205 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulich176a2712006-06-26 13:57:41 +0200206 }
Jan Beulichea424052006-08-30 19:37:11 +0200207 if (unw_ret > 0) {
208 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
Andi Kleenb615ebd2006-12-07 02:14:00 +0100209 ops->warning_symbol(data,
Andi Kleendd315df2006-12-07 02:14:00 +0100210 "DWARF2 unwinder stuck at %s",
Jan Beulichea424052006-08-30 19:37:11 +0200211 UNW_PC(&info));
212 if (UNW_SP(&info) >= PAGE_OFFSET) {
Andi Kleendd315df2006-12-07 02:14:00 +0100213 MSG("Leftover inexact backtrace:");
Andi Kleen70583162006-07-29 21:42:52 +0200214 stack = (void *)UNW_SP(&info);
Andi Kleen950fee82006-09-26 10:52:34 +0200215 if (!stack)
216 return;
Andi Kleena32cf392006-09-26 10:52:34 +0200217 ebp = UNW_FP(&info);
Jan Beulichea424052006-08-30 19:37:11 +0200218 } else
Andi Kleendd315df2006-12-07 02:14:00 +0100219 MSG("Full inexact backtrace again:");
Jan Beulichea424052006-08-30 19:37:11 +0200220 } else if (call_trace >= 1)
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200221 return;
Andi Kleenc97d20a2006-07-28 14:44:57 +0200222 else
Andi Kleendd315df2006-12-07 02:14:00 +0100223 MSG("Full inexact backtrace again:");
Jan Beulichea424052006-08-30 19:37:11 +0200224 } else
Andi Kleendd315df2006-12-07 02:14:00 +0100225 MSG("Inexact backtrace:");
Andi Kleena32cf392006-09-26 10:52:34 +0200226 }
227 if (!stack) {
Andi Kleen2b14a782006-09-26 10:52:34 +0200228 unsigned long dummy;
229 stack = &dummy;
230 if (task && task != current)
231 stack = (unsigned long *)task->thread.esp;
Jan Beulich176a2712006-06-26 13:57:41 +0200232 }
233
Andi Kleena32cf392006-09-26 10:52:34 +0200234#ifdef CONFIG_FRAME_POINTER
235 if (!ebp) {
236 if (task == current) {
237 /* Grab ebp right from our regs */
238 asm ("movl %%ebp, %0" : "=r" (ebp) : );
239 } else {
240 /* ebp is the last reg pushed by switch_to */
241 ebp = *(unsigned long *) task->thread.esp;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Andi Kleena32cf392006-09-26 10:52:34 +0200244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 while (1) {
247 struct thread_info *context;
248 context = (struct thread_info *)
249 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
Andi Kleen2b14a782006-09-26 10:52:34 +0200250 ebp = print_context_stack(context, stack, ebp, ops, data);
251 /* Should be after the line below, but somewhere
252 in early boot context comes out corrupted and we
253 can't reference it -AK */
254 if (ops->stack(data, "IRQ") < 0)
255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 stack = (unsigned long*)context->previous_esp;
257 if (!stack)
258 break;
Dave Jonesa36df982006-12-07 02:14:12 +0100259 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261}
Andi Kleen2b14a782006-09-26 10:52:34 +0200262EXPORT_SYMBOL(dump_trace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Andi Kleen2b14a782006-09-26 10:52:34 +0200264static void
265print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
266{
267 printk(data);
268 print_symbol(msg, symbol);
269 printk("\n");
270}
271
272static void print_trace_warning(void *data, char *msg)
273{
274 printk("%s%s\n", (char *)data, msg);
275}
276
277static int print_trace_stack(void *data, char *name)
278{
279 return 0;
280}
281
282/*
283 * Print one address/symbol entries per line.
284 */
285static void print_trace_address(void *data, unsigned long addr)
286{
287 printk("%s [<%08lx>] ", (char *)data, addr);
288 print_symbol("%s\n", addr);
289}
290
291static struct stacktrace_ops print_trace_ops = {
292 .warning = print_trace_warning,
293 .warning_symbol = print_trace_warning_symbol,
294 .stack = print_trace_stack,
295 .address = print_trace_address,
296};
297
298static void
299show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
300 unsigned long * stack, char *log_lvl)
301{
302 dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
303 printk("%s =======================\n", log_lvl);
304}
305
306void show_trace(struct task_struct *task, struct pt_regs *regs,
307 unsigned long * stack)
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800308{
Jan Beulich176a2712006-06-26 13:57:41 +0200309 show_trace_log_lvl(task, regs, stack, "");
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800310}
311
Jan Beulich176a2712006-06-26 13:57:41 +0200312static void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
313 unsigned long *esp, char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 unsigned long *stack;
316 int i;
317
318 if (esp == NULL) {
319 if (task)
320 esp = (unsigned long*)task->thread.esp;
321 else
322 esp = (unsigned long *)&esp;
323 }
324
325 stack = esp;
326 for(i = 0; i < kstack_depth_to_print; i++) {
327 if (kstack_end(stack))
328 break;
Chuck Ebbert75874d52006-03-23 02:59:52 -0800329 if (i && ((i % 8) == 0))
330 printk("\n%s ", log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 printk("%08lx ", *stack++);
332 }
Chuck Ebbert75874d52006-03-23 02:59:52 -0800333 printk("\n%sCall Trace:\n", log_lvl);
Jan Beulich176a2712006-06-26 13:57:41 +0200334 show_trace_log_lvl(task, regs, esp, log_lvl);
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800335}
336
337void show_stack(struct task_struct *task, unsigned long *esp)
338{
Chuck Ebbert75874d52006-03-23 02:59:52 -0800339 printk(" ");
Jan Beulich176a2712006-06-26 13:57:41 +0200340 show_stack_log_lvl(task, NULL, esp, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * The architecture-independent dump_stack generator
345 */
346void dump_stack(void)
347{
348 unsigned long stack;
349
Jan Beulich176a2712006-06-26 13:57:41 +0200350 show_trace(current, NULL, &stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353EXPORT_SYMBOL(dump_stack);
354
355void show_registers(struct pt_regs *regs)
356{
357 int i;
358 int in_kernel = 1;
359 unsigned long esp;
360 unsigned short ss;
361
362 esp = (unsigned long) (&regs->esp);
Zachary Amsden0998e422005-09-03 15:56:43 -0700363 savesegment(ss, ss);
Jan Beulichdb753bd2006-03-23 02:59:46 -0800364 if (user_mode_vm(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 in_kernel = 0;
366 esp = regs->esp;
367 ss = regs->xss & 0xffff;
368 }
369 print_modules();
Dave Jonesf354b3a2006-09-26 10:52:41 +0200370 printk(KERN_EMERG "CPU: %d\n"
371 KERN_EMERG "EIP: %04x:[<%08lx>] %s VLI\n"
372 KERN_EMERG "EFLAGS: %08lx (%s %.*s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700374 print_tainted(), regs->eflags, init_utsname()->release,
375 (int)strcspn(init_utsname()->version, " "),
376 init_utsname()->version);
Dave Jones9c107802006-01-09 20:51:32 -0800377 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
378 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 regs->eax, regs->ebx, regs->ecx, regs->edx);
Dave Jones9c107802006-01-09 20:51:32 -0800380 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 regs->esi, regs->edi, regs->ebp, esp);
Dave Jones9c107802006-01-09 20:51:32 -0800382 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 regs->xds & 0xffff, regs->xes & 0xffff, ss);
Chuck Ebbert7e04a112006-06-23 02:04:31 -0700384 printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
385 TASK_COMM_LEN, current->comm, current->pid,
386 current_thread_info(), current, current->thread_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * When in-kernel, we also print out the stack and code at the
389 * time of the fault..
390 */
391 if (in_kernel) {
Andi Kleen11a41802006-12-07 02:14:06 +0100392 u8 *eip;
Chuck Ebbert99325322006-09-25 23:32:19 -0700393 int code_bytes = 64;
394 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Dave Jones9c107802006-01-09 20:51:32 -0800396 printk("\n" KERN_EMERG "Stack: ");
Jan Beulich176a2712006-06-26 13:57:41 +0200397 show_stack_log_lvl(NULL, regs, (unsigned long *)esp, KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Dave Jones9c107802006-01-09 20:51:32 -0800399 printk(KERN_EMERG "Code: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Andi Kleen11a41802006-12-07 02:14:06 +0100401 eip = (u8 *)regs->eip - 43;
402 if (eip < (u8 *)PAGE_OFFSET ||
403 probe_kernel_address(eip, c)) {
Chuck Ebbert99325322006-09-25 23:32:19 -0700404 /* try starting at EIP */
Andi Kleen11a41802006-12-07 02:14:06 +0100405 eip = (u8 *)regs->eip;
Chuck Ebbert99325322006-09-25 23:32:19 -0700406 code_bytes = 32;
407 }
408 for (i = 0; i < code_bytes; i++, eip++) {
Andi Kleen11a41802006-12-07 02:14:06 +0100409 if (eip < (u8 *)PAGE_OFFSET ||
410 probe_kernel_address(eip, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 printk(" Bad EIP value.");
412 break;
413 }
Andi Kleen11a41802006-12-07 02:14:06 +0100414 if (eip == (u8 *)regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 printk("<%02x> ", c);
416 else
417 printk("%02x ", c);
418 }
419 }
420 printk("\n");
421}
422
423static void handle_BUG(struct pt_regs *regs)
424{
Chuck Ebbertb7015332006-07-14 00:23:58 -0700425 unsigned long eip = regs->eip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (eip < PAGE_OFFSET)
Chuck Ebbertb7015332006-07-14 00:23:58 -0700429 return;
Andi Kleen11a41802006-12-07 02:14:06 +0100430 if (probe_kernel_address((unsigned short *)eip, ud2))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700431 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (ud2 != 0x0b0f)
Chuck Ebbertb7015332006-07-14 00:23:58 -0700433 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dave Jones9c107802006-01-09 20:51:32 -0800435 printk(KERN_EMERG "------------[ cut here ]------------\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Chuck Ebbertb7015332006-07-14 00:23:58 -0700437#ifdef CONFIG_DEBUG_BUGVERBOSE
438 do {
439 unsigned short line;
440 char *file;
441 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Andi Kleen11a41802006-12-07 02:14:06 +0100443 if (probe_kernel_address((unsigned short *)(eip + 2), line))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700444 break;
Andi Kleen11a41802006-12-07 02:14:06 +0100445 if (probe_kernel_address((char **)(eip + 4), file) ||
446 (unsigned long)file < PAGE_OFFSET ||
447 probe_kernel_address(file, c))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700448 file = "<bad filename>";
449
450 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
451 return;
452 } while (0);
453#endif
454 printk(KERN_EMERG "Kernel BUG at [verbose debug info unavailable]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700457/* This is gone through when something in the kernel
458 * has done something bad and is about to be terminated.
459*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460void die(const char * str, struct pt_regs * regs, long err)
461{
462 static struct {
463 spinlock_t lock;
464 u32 lock_owner;
465 int lock_owner_depth;
466 } die = {
467 .lock = SPIN_LOCK_UNLOCKED,
468 .lock_owner = -1,
469 .lock_owner_depth = 0
470 };
471 static int die_counter;
Jan Beuliche43d6742006-01-06 00:11:48 -0800472 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Andrew Mortondd287792006-03-23 03:00:57 -0800474 oops_enter();
475
Ingo Molnar39c715b2005-06-21 17:14:34 -0700476 if (die.lock_owner != raw_smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 console_verbose();
Jan Beuliche43d6742006-01-06 00:11:48 -0800478 spin_lock_irqsave(&die.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 die.lock_owner = smp_processor_id();
480 die.lock_owner_depth = 0;
481 bust_spinlocks(1);
482 }
Jan Beuliche43d6742006-01-06 00:11:48 -0800483 else
484 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (++die.lock_owner_depth < 3) {
487 int nl = 0;
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700488 unsigned long esp;
489 unsigned short ss;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 handle_BUG(regs);
Dave Jones9c107802006-01-09 20:51:32 -0800492 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#ifdef CONFIG_PREEMPT
Dave Jones9c107802006-01-09 20:51:32 -0800494 printk(KERN_EMERG "PREEMPT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 nl = 1;
496#endif
497#ifdef CONFIG_SMP
Dave Jones9c107802006-01-09 20:51:32 -0800498 if (!nl)
499 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 printk("SMP ");
501 nl = 1;
502#endif
503#ifdef CONFIG_DEBUG_PAGEALLOC
Dave Jones9c107802006-01-09 20:51:32 -0800504 if (!nl)
505 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 printk("DEBUG_PAGEALLOC");
507 nl = 1;
508#endif
509 if (nl)
510 printk("\n");
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800511 if (notify_die(DIE_OOPS, str, regs, err,
512 current->thread.trap_no, SIGSEGV) !=
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700513 NOTIFY_STOP) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800514 show_registers(regs);
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700515 /* Executive summary in case the oops scrolled away */
516 esp = (unsigned long) (&regs->esp);
517 savesegment(ss, ss);
518 if (user_mode(regs)) {
519 esp = regs->esp;
520 ss = regs->xss & 0xffff;
521 }
522 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
523 print_symbol("%s", regs->eip);
524 printk(" SS:ESP %04x:%08lx\n", ss, esp);
525 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800526 else
527 regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else
Dave Jones9c107802006-01-09 20:51:32 -0800529 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 bust_spinlocks(0);
532 die.lock_owner = -1;
Jan Beuliche43d6742006-01-06 00:11:48 -0800533 spin_unlock_irqrestore(&die.lock, flags);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700534
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800535 if (!regs)
536 return;
537
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700538 if (kexec_should_crash(current))
539 crash_kexec(regs);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (in_interrupt())
542 panic("Fatal exception in interrupt");
543
Hormscea6a4b2006-07-30 03:03:34 -0700544 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700545 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700546
Andrew Mortondd287792006-03-23 03:00:57 -0800547 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 do_exit(SIGSEGV);
549}
550
551static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
552{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700553 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 die(str, regs, err);
555}
556
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700557static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
558 struct pt_regs * regs, long error_code,
559 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700561 struct task_struct *tsk = current;
562 tsk->thread.error_code = error_code;
563 tsk->thread.trap_no = trapnr;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (regs->eflags & VM_MASK) {
566 if (vm86)
567 goto vm86_trap;
568 goto trap_signal;
569 }
570
Vincent Hanquez717b5942005-06-23 00:08:45 -0700571 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto kernel_trap;
573
574 trap_signal: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (info)
576 force_sig_info(signr, info, tsk);
577 else
578 force_sig(signr, tsk);
579 return;
580 }
581
582 kernel_trap: {
583 if (!fixup_exception(regs))
584 die(str, regs, error_code);
585 return;
586 }
587
588 vm86_trap: {
589 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
590 if (ret) goto trap_signal;
591 return;
592 }
593}
594
595#define DO_ERROR(trapnr, signr, str, name) \
596fastcall void do_##name(struct pt_regs * regs, long error_code) \
597{ \
598 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
599 == NOTIFY_STOP) \
600 return; \
601 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
602}
603
604#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
605fastcall void do_##name(struct pt_regs * regs, long error_code) \
606{ \
607 siginfo_t info; \
608 info.si_signo = signr; \
609 info.si_errno = 0; \
610 info.si_code = sicode; \
611 info.si_addr = (void __user *)siaddr; \
612 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
613 == NOTIFY_STOP) \
614 return; \
615 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
616}
617
618#define DO_VM86_ERROR(trapnr, signr, str, name) \
619fastcall 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; \
624 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
625}
626
627#define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
628fastcall void do_##name(struct pt_regs * regs, long error_code) \
629{ \
630 siginfo_t info; \
631 info.si_signo = signr; \
632 info.si_errno = 0; \
633 info.si_code = sicode; \
634 info.si_addr = (void __user *)siaddr; \
635 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
636 == NOTIFY_STOP) \
637 return; \
638 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
639}
640
641DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
642#ifndef CONFIG_KPROBES
643DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
644#endif
645DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
646DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert631b0342006-01-03 22:36:14 -0500647DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648DO_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(12, SIGBUS, "stack segment", stack_segment)
652DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvaldsa879cbb2005-04-29 09:38:44 -0700653DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700655fastcall void __kprobes do_general_protection(struct pt_regs * regs,
656 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int cpu = get_cpu();
659 struct tss_struct *tss = &per_cpu(init_tss, cpu);
660 struct thread_struct *thread = &current->thread;
661
662 /*
663 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
664 * invalid offset set (the LAZY one) and the faulting thread has
665 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
666 * and we set the offset field correctly. Then we let the CPU to
667 * restart the faulting instruction.
668 */
669 if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
670 thread->io_bitmap_ptr) {
671 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
672 thread->io_bitmap_max);
673 /*
674 * If the previously set map was extending to higher ports
675 * than the current one, pad extra space with 0xff (no access).
676 */
677 if (thread->io_bitmap_max < tss->io_bitmap_max)
678 memset((char *) tss->io_bitmap +
679 thread->io_bitmap_max, 0xff,
680 tss->io_bitmap_max - thread->io_bitmap_max);
681 tss->io_bitmap_max = thread->io_bitmap_max;
682 tss->io_bitmap_base = IO_BITMAP_OFFSET;
Bart Oldemand5cd4aa2005-10-30 14:59:29 -0800683 tss->io_bitmap_owner = thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 put_cpu();
685 return;
686 }
687 put_cpu();
688
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700689 current->thread.error_code = error_code;
690 current->thread.trap_no = 13;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (regs->eflags & VM_MASK)
693 goto gp_in_vm86;
694
Vincent Hanquez717b5942005-06-23 00:08:45 -0700695 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto gp_in_kernel;
697
698 current->thread.error_code = error_code;
699 current->thread.trap_no = 13;
700 force_sig(SIGSEGV, current);
701 return;
702
703gp_in_vm86:
704 local_irq_enable();
705 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
706 return;
707
708gp_in_kernel:
709 if (!fixup_exception(regs)) {
710 if (notify_die(DIE_GPF, "general protection fault", regs,
711 error_code, 13, SIGSEGV) == NOTIFY_STOP)
712 return;
713 die("general protection fault", regs, error_code);
714 }
715}
716
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200717static __kprobes void
718mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200720 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
721 "CPU %d.\n", reason, smp_processor_id());
Andi Kleen9c5f8be2006-12-07 02:14:03 +0100722 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200723 if (panic_on_unrecovered_nmi)
724 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Don Zickusc41c5cd2006-09-26 10:52:27 +0200726 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Clear and disable the memory parity error line. */
729 clear_mem_error(reason);
730}
731
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200732static __kprobes void
733io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 unsigned long i;
736
Dave Jones9c107802006-01-09 20:51:32 -0800737 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 show_registers(regs);
739
740 /* Re-enable the IOCK line, wait for a few seconds */
741 reason = (reason & 0xf) | 8;
742 outb(reason, 0x61);
743 i = 2000;
744 while (--i) udelay(1000);
745 reason &= ~8;
746 outb(reason, 0x61);
747}
748
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200749static __kprobes void
750unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752#ifdef CONFIG_MCA
753 /* Might actually be able to figure out what the guilty party
754 * is. */
755 if( MCA_bus ) {
756 mca_handle_nmi();
757 return;
758 }
759#endif
Don Zickusc41c5cd2006-09-26 10:52:27 +0200760 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
761 "CPU %d.\n", reason, smp_processor_id());
762 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200763 if (panic_on_unrecovered_nmi)
764 panic("NMI: Not continuing");
765
Don Zickusc41c5cd2006-09-26 10:52:27 +0200766 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769static DEFINE_SPINLOCK(nmi_print_lock);
770
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200771void __kprobes die_nmi(struct pt_regs *regs, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800773 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
George Anzinger748f2ed2005-09-03 15:56:48 -0700774 NOTIFY_STOP)
775 return;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 spin_lock(&nmi_print_lock);
778 /*
779 * We are in trouble anyway, lets at least try
780 * to get a message out.
781 */
782 bust_spinlocks(1);
Dave Jones9c107802006-01-09 20:51:32 -0800783 printk(KERN_EMERG "%s", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 printk(" on CPU%d, eip %08lx, registers:\n",
785 smp_processor_id(), regs->eip);
786 show_registers(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 console_silent();
788 spin_unlock(&nmi_print_lock);
789 bust_spinlocks(0);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700790
791 /* If we are in kernel we are probably nested up pretty bad
792 * and might aswell get out now while we still can.
793 */
Jan Beulichdb753bd2006-03-23 02:59:46 -0800794 if (!user_mode_vm(regs)) {
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700795 current->thread.trap_no = 2;
796 crash_kexec(regs);
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 do_exit(SIGSEGV);
800}
801
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200802static __kprobes void default_do_nmi(struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 unsigned char reason = 0;
805
806 /* Only the BSP gets external NMIs from the system. */
807 if (!smp_processor_id())
808 reason = get_nmi_reason();
809
810 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800811 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 == NOTIFY_STOP)
813 return;
814#ifdef CONFIG_X86_LOCAL_APIC
815 /*
816 * Ok, so this is none of the documented NMI sources,
817 * so it must be the NMI watchdog.
818 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200819 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200821 if (!do_nmi_callback(regs, smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822#endif
Don Zickus3adbbcc2006-09-26 10:52:26 +0200823 unknown_nmi_error(reason, regs);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
826 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800827 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return;
829 if (reason & 0x80)
830 mem_parity_error(reason, regs);
831 if (reason & 0x40)
832 io_check_error(reason, regs);
833 /*
834 * Reassert NMI in case it became active meanwhile
835 * as it's edge-triggered.
836 */
837 reassert_nmi();
838}
839
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200840fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 int cpu;
843
844 nmi_enter();
845
846 cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ++nmi_count(cpu);
849
Don Zickus3adbbcc2006-09-26 10:52:26 +0200850 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 nmi_exit();
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#ifdef CONFIG_KPROBES
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700856fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
859 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* This is an interrupt gate, because kprobes wants interrupts
862 disabled. Normal trap handlers don't. */
863 restore_interrupts(regs);
864 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866#endif
867
868/*
869 * Our handling of the processor debug registers is non-trivial.
870 * We do not clear them on entry and exit from the kernel. Therefore
871 * it is possible to get a watchpoint trap here from inside the kernel.
872 * However, the code in ./ptrace.c has ensured that the user can
873 * only set watchpoints on userspace addresses. Therefore the in-kernel
874 * watchpoint trap can only occur in code which is reading/writing
875 * from user space. Such code must not hold kernel locks (since it
876 * can equally take a page fault), therefore it is safe to call
877 * force_sig_info even though that claims and releases locks.
878 *
879 * Code in ./signal.c ensures that the debug control register
880 * is restored before we deliver any signal, and therefore that
881 * user code runs with the correct debug control register even though
882 * we clear it here.
883 *
884 * Being careful here means that we don't have to be as careful in a
885 * lot of more complicated places (task switching can be a bit lazy
886 * about restoring all the debug state, and ptrace doesn't have to
887 * find every occurrence of the TF bit that could be saved away even
888 * by user code)
889 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700890fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 unsigned int condition;
893 struct task_struct *tsk = current;
894
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700895 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
898 SIGTRAP) == NOTIFY_STOP)
899 return;
900 /* It's safe to allow irq's after DR6 has been saved */
901 if (regs->eflags & X86_EFLAGS_IF)
902 local_irq_enable();
903
904 /* Mask out spurious debug traps due to lazy DR7 setting */
905 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
906 if (!tsk->thread.debugreg[7])
907 goto clear_dr7;
908 }
909
910 if (regs->eflags & VM_MASK)
911 goto debug_vm86;
912
913 /* Save debug status register where ptrace can see it */
914 tsk->thread.debugreg[6] = condition;
915
916 /*
917 * Single-stepping through TF: make sure we ignore any events in
918 * kernel space (but re-enable TF when returning to user mode).
919 */
920 if (condition & DR_STEP) {
921 /*
922 * We already checked v86 mode above, so we can
923 * check for kernel mode by just checking the CPL
924 * of CS.
925 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700926 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto clear_TF_reenable;
928 }
929
930 /* Ok, finally something we can handle */
931 send_sigtrap(tsk, regs, error_code);
932
933 /* Disable additional traps. They'll be re-enabled when
934 * the signal is delivered.
935 */
936clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700937 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return;
939
940debug_vm86:
941 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
942 return;
943
944clear_TF_reenable:
945 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
946 regs->eflags &= ~TF_MASK;
947 return;
948}
949
950/*
951 * Note that we play around with the 'TS' bit in an attempt to get
952 * the correct behaviour even in the presence of the asynchronous
953 * IRQ13 behaviour
954 */
955void math_error(void __user *eip)
956{
957 struct task_struct * task;
958 siginfo_t info;
959 unsigned short cwd, swd;
960
961 /*
962 * Save the info for the exception handler and clear the error.
963 */
964 task = current;
965 save_init_fpu(task);
966 task->thread.trap_no = 16;
967 task->thread.error_code = 0;
968 info.si_signo = SIGFPE;
969 info.si_errno = 0;
970 info.si_code = __SI_FAULT;
971 info.si_addr = eip;
972 /*
973 * (~cwd & swd) will mask out exceptions that are not set to unmasked
974 * status. 0x3f is the exception bits in these regs, 0x200 is the
975 * C1 reg you need in case of a stack fault, 0x040 is the stack
976 * fault bit. We should only be taking one exception at a time,
977 * so if this combination doesn't produce any single exception,
978 * then we have a bad program that isn't syncronizing its FPU usage
979 * and it will suffer the consequences since we won't be able to
980 * fully reproduce the context of the exception
981 */
982 cwd = get_fpu_cwd(task);
983 swd = get_fpu_swd(task);
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400984 switch (swd & ~cwd & 0x3f) {
Chuck Ebbert33333372005-09-13 04:55:41 -0400985 case 0x000: /* No unmasked exception */
986 return;
987 default: /* Multiple exceptions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
989 case 0x001: /* Invalid Op */
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400990 /*
991 * swd & 0x240 == 0x040: Stack Underflow
992 * swd & 0x240 == 0x240: Stack Overflow
993 * User must clear the SF bit (0x40) if set
994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 info.si_code = FPE_FLTINV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997 case 0x002: /* Denormalize */
998 case 0x010: /* Underflow */
999 info.si_code = FPE_FLTUND;
1000 break;
1001 case 0x004: /* Zero Divide */
1002 info.si_code = FPE_FLTDIV;
1003 break;
1004 case 0x008: /* Overflow */
1005 info.si_code = FPE_FLTOVF;
1006 break;
1007 case 0x020: /* Precision */
1008 info.si_code = FPE_FLTRES;
1009 break;
1010 }
1011 force_sig_info(SIGFPE, &info, task);
1012}
1013
1014fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
1015{
1016 ignore_fpu_irq = 1;
1017 math_error((void __user *)regs->eip);
1018}
1019
1020static void simd_math_error(void __user *eip)
1021{
1022 struct task_struct * task;
1023 siginfo_t info;
1024 unsigned short mxcsr;
1025
1026 /*
1027 * Save the info for the exception handler and clear the error.
1028 */
1029 task = current;
1030 save_init_fpu(task);
1031 task->thread.trap_no = 19;
1032 task->thread.error_code = 0;
1033 info.si_signo = SIGFPE;
1034 info.si_errno = 0;
1035 info.si_code = __SI_FAULT;
1036 info.si_addr = eip;
1037 /*
1038 * The SIMD FPU exceptions are handled a little differently, as there
1039 * is only a single status/control register. Thus, to determine which
1040 * unmasked exception was caught we must mask the exception mask bits
1041 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1042 */
1043 mxcsr = get_fpu_mxcsr(task);
1044 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1045 case 0x000:
1046 default:
1047 break;
1048 case 0x001: /* Invalid Op */
1049 info.si_code = FPE_FLTINV;
1050 break;
1051 case 0x002: /* Denormalize */
1052 case 0x010: /* Underflow */
1053 info.si_code = FPE_FLTUND;
1054 break;
1055 case 0x004: /* Zero Divide */
1056 info.si_code = FPE_FLTDIV;
1057 break;
1058 case 0x008: /* Overflow */
1059 info.si_code = FPE_FLTOVF;
1060 break;
1061 case 0x020: /* Precision */
1062 info.si_code = FPE_FLTRES;
1063 break;
1064 }
1065 force_sig_info(SIGFPE, &info, task);
1066}
1067
1068fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
1069 long error_code)
1070{
1071 if (cpu_has_xmm) {
1072 /* Handle SIMD FPU exceptions on PIII+ processors. */
1073 ignore_fpu_irq = 1;
1074 simd_math_error((void __user *)regs->eip);
1075 } else {
1076 /*
1077 * Handle strange cache flush from user space exception
1078 * in all other cases. This is undocumented behaviour.
1079 */
1080 if (regs->eflags & VM_MASK) {
1081 handle_vm86_fault((struct kernel_vm86_regs *)regs,
1082 error_code);
1083 return;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 current->thread.trap_no = 19;
1086 current->thread.error_code = error_code;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -07001087 die_if_kernel("cache flush denied", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 force_sig(SIGSEGV, current);
1089 }
1090}
1091
1092fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
1093 long error_code)
1094{
1095#if 0
1096 /* No need to warn about this any longer. */
1097 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1098#endif
1099}
1100
Stas Sergeevbe44d2a2006-12-07 02:14:01 +01001101fastcall unsigned long patch_espfix_desc(unsigned long uesp,
1102 unsigned long kesp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int cpu = smp_processor_id();
Stas Sergeevbe44d2a2006-12-07 02:14:01 +01001105 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
1106 struct desc_struct *gdt = (struct desc_struct *)cpu_gdt_descr->address;
1107 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
1108 unsigned long new_kesp = kesp - base;
1109 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
1110 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
1111 /* Set up base for espfix segment */
1112 desc &= 0x00f0ff0000000000ULL;
1113 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
1114 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
1115 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
1116 (lim_pages & 0xffff);
1117 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
1118 return new_kesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121/*
1122 * 'math_state_restore()' saves the current math information in the
1123 * old math state array, and gets the new ones from the current task
1124 *
1125 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1126 * Don't touch unless you *really* know how it works.
1127 *
1128 * Must be called with kernel preemption disabled (in this case,
1129 * local interrupts are disabled at the call-site in entry.S).
1130 */
Chuck Ebbertacc20762006-12-07 02:14:01 +01001131asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct thread_info *thread = current_thread_info();
1134 struct task_struct *tsk = thread->task;
1135
1136 clts(); /* Allow maths ops (or we recurse) */
1137 if (!tsk_used_math(tsk))
1138 init_fpu(tsk);
1139 restore_fpu(tsk);
1140 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
Chuck Ebbertacc20762006-12-07 02:14:01 +01001141 tsk->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144#ifndef CONFIG_MATH_EMULATION
1145
1146asmlinkage void math_emulate(long arg)
1147{
Dave Jones9c107802006-01-09 20:51:32 -08001148 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1149 printk(KERN_EMERG "killing %s.\n",current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 force_sig(SIGFPE,current);
1151 schedule();
1152}
1153
1154#endif /* CONFIG_MATH_EMULATION */
1155
1156#ifdef CONFIG_X86_F00F_BUG
1157void __init trap_init_f00f_bug(void)
1158{
1159 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1160
1161 /*
1162 * Update the IDT descriptor and reload the IDT so that
1163 * it uses the read-only mapped virtual address.
1164 */
1165 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -07001166 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168#endif
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * This needs to use 'idt_table' rather than 'idt', and
1172 * thus use the _nonmapped_ version of the IDT, as the
1173 * Pentium F0 0F bugfix can have resulted in the mapped
1174 * IDT being write-protected.
1175 */
1176void set_intr_gate(unsigned int n, void *addr)
1177{
Rusty Russell522e93e2006-09-26 10:52:35 +02001178 _set_gate(n, DESCTYPE_INT, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181/*
1182 * This routine sets up an interrupt gate at directory privilege level 3.
1183 */
1184static inline void set_system_intr_gate(unsigned int n, void *addr)
1185{
Rusty Russell522e93e2006-09-26 10:52:35 +02001186 _set_gate(n, DESCTYPE_INT | DESCTYPE_DPL3, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static void __init set_trap_gate(unsigned int n, void *addr)
1190{
Rusty Russell522e93e2006-09-26 10:52:35 +02001191 _set_gate(n, DESCTYPE_TRAP, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194static void __init set_system_gate(unsigned int n, void *addr)
1195{
Rusty Russell522e93e2006-09-26 10:52:35 +02001196 _set_gate(n, DESCTYPE_TRAP | DESCTYPE_DPL3, addr, __KERNEL_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
1200{
Rusty Russell522e93e2006-09-26 10:52:35 +02001201 _set_gate(n, DESCTYPE_TASK, (void *)0, (gdt_entry<<3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204
1205void __init trap_init(void)
1206{
1207#ifdef CONFIG_EISA
1208 void __iomem *p = ioremap(0x0FFFD9, 4);
1209 if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
1210 EISA_bus = 1;
1211 }
1212 iounmap(p);
1213#endif
1214
1215#ifdef CONFIG_X86_LOCAL_APIC
1216 init_apic_mappings();
1217#endif
1218
1219 set_trap_gate(0,&divide_error);
1220 set_intr_gate(1,&debug);
1221 set_intr_gate(2,&nmi);
Jan Beulicheb05c322006-01-06 00:11:49 -08001222 set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 set_system_gate(4,&overflow);
Jan Beulicheb05c322006-01-06 00:11:49 -08001224 set_trap_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 set_trap_gate(6,&invalid_op);
1226 set_trap_gate(7,&device_not_available);
1227 set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1228 set_trap_gate(9,&coprocessor_segment_overrun);
1229 set_trap_gate(10,&invalid_TSS);
1230 set_trap_gate(11,&segment_not_present);
1231 set_trap_gate(12,&stack_segment);
1232 set_trap_gate(13,&general_protection);
1233 set_intr_gate(14,&page_fault);
1234 set_trap_gate(15,&spurious_interrupt_bug);
1235 set_trap_gate(16,&coprocessor_error);
1236 set_trap_gate(17,&alignment_check);
1237#ifdef CONFIG_X86_MCE
1238 set_trap_gate(18,&machine_check);
1239#endif
1240 set_trap_gate(19,&simd_coprocessor_error);
1241
Jan Beulichd43c6e82006-01-06 00:11:49 -08001242 if (cpu_has_fxsr) {
1243 /*
1244 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1245 * Generates a compile-time "error: zero width for bit-field" if
1246 * the alignment is wrong.
1247 */
1248 struct fxsrAlignAssert {
1249 int _:!(offsetof(struct task_struct,
1250 thread.i387.fxsave) & 15);
1251 };
1252
1253 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1254 set_in_cr4(X86_CR4_OSFXSR);
1255 printk("done.\n");
1256 }
1257 if (cpu_has_xmm) {
1258 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1259 "support... ");
1260 set_in_cr4(X86_CR4_OSXMMEXCPT);
1261 printk("done.\n");
1262 }
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 set_system_gate(SYSCALL_VECTOR,&system_call);
1265
1266 /*
1267 * Should be a barrier for any external CPU state.
1268 */
1269 cpu_init();
1270
1271 trap_init_hook();
1272}
1273
1274static int __init kstack_setup(char *s)
1275{
1276 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001277 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279__setup("kstack=", kstack_setup);
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001280
Jan Beulichea424052006-08-30 19:37:11 +02001281#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001282static int __init call_trace_setup(char *s)
1283{
1284 if (strcmp(s, "old") == 0)
1285 call_trace = -1;
1286 else if (strcmp(s, "both") == 0)
1287 call_trace = 0;
Andi Kleen70583162006-07-29 21:42:52 +02001288 else if (strcmp(s, "newfallback") == 0)
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001289 call_trace = 1;
Andi Kleen70583162006-07-29 21:42:52 +02001290 else if (strcmp(s, "new") == 2)
1291 call_trace = 2;
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001292 return 1;
1293}
1294__setup("call_trace=", call_trace_setup);
Jan Beulichea424052006-08-30 19:37:11 +02001295#endif