blob: 4ced4285163bd2b3d9f929593452384313e02ba9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#ifdef CONFIG_EISA
33#include <linux/ioport.h>
34#include <linux/eisa.h>
35#endif
36
37#ifdef CONFIG_MCA
38#include <linux/mca.h>
39#endif
40
41#include <asm/processor.h>
42#include <asm/system.h>
43#include <asm/uaccess.h>
44#include <asm/io.h>
45#include <asm/atomic.h>
46#include <asm/debugreg.h>
47#include <asm/desc.h>
48#include <asm/i387.h>
49#include <asm/nmi.h>
Jan Beulich176a2712006-06-26 13:57:41 +020050#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/smp.h>
52#include <asm/arch_hooks.h>
53#include <asm/kdebug.h>
Andi Kleen2b14a782006-09-26 10:52:34 +020054#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/module.h>
57
58#include "mach_traps.h"
59
60asmlinkage int system_call(void);
61
62struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
63 { 0, 0 }, { 0, 0 } };
64
65/* 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
95static int 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)) {
130 addr = *(unsigned long *)(ebp + 4);
Andi Kleen2b14a782006-09-26 10:52:34 +0200131 ops->address(data, addr);
Ingo Molnarb88d4f12006-06-23 02:04:20 -0700132 /*
133 * break out of recursive entries (such as
134 * end_of_stack_stop_unwind_function):
135 */
136 if (ebp == *(unsigned long *)ebp)
137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 ebp = *(unsigned long *)ebp;
139 }
140#else
141 while (valid_stack_ptr(tinfo, stack)) {
142 addr = *stack++;
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800143 if (__kernel_text_address(addr))
Andi Kleen2b14a782006-09-26 10:52:34 +0200144 ops->address(data, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146#endif
147 return ebp;
148}
149
Andi Kleen2b14a782006-09-26 10:52:34 +0200150struct ops_and_data {
151 struct stacktrace_ops *ops;
152 void *data;
153};
154
Ingo Molnarf0a5c312006-07-03 00:24:37 -0700155static asmlinkage int
Andi Kleen2b14a782006-09-26 10:52:34 +0200156dump_trace_unwind(struct unwind_frame_info *info, void *data)
Jan Beulich176a2712006-06-26 13:57:41 +0200157{
Andi Kleen2b14a782006-09-26 10:52:34 +0200158 struct ops_and_data *oad = (struct ops_and_data *)data;
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200159 int n = 0;
Jan Beulich176a2712006-06-26 13:57:41 +0200160
161 while (unwind(info) == 0 && UNW_PC(info)) {
Ingo Molnarf0a5c312006-07-03 00:24:37 -0700162 n++;
Andi Kleen2b14a782006-09-26 10:52:34 +0200163 oad->ops->address(oad->data, UNW_PC(info));
Jan Beulich176a2712006-06-26 13:57:41 +0200164 if (arch_unw_user_mode(info))
165 break;
166 }
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200167 return n;
Jan Beulich176a2712006-06-26 13:57:41 +0200168}
169
Andi Kleen2b14a782006-09-26 10:52:34 +0200170void dump_trace(struct task_struct *task, struct pt_regs *regs,
171 unsigned long *stack,
172 struct stacktrace_ops *ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long ebp;
175
176 if (!task)
177 task = current;
178
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200179 if (call_trace >= 0) {
180 int unw_ret = 0;
181 struct unwind_frame_info info;
Andi Kleen2b14a782006-09-26 10:52:34 +0200182 struct ops_and_data oad = { .ops = ops, .data = data };
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200183
184 if (regs) {
185 if (unwind_init_frame_info(&info, task, regs) == 0)
Andi Kleen2b14a782006-09-26 10:52:34 +0200186 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200187 } else if (task == current)
Andi Kleen2b14a782006-09-26 10:52:34 +0200188 unw_ret = unwind_init_running(&info, dump_trace_unwind, &oad);
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200189 else {
190 if (unwind_init_blocked(&info, task) == 0)
Andi Kleen2b14a782006-09-26 10:52:34 +0200191 unw_ret = dump_trace_unwind(&info, &oad);
Jan Beulich176a2712006-06-26 13:57:41 +0200192 }
Jan Beulichea424052006-08-30 19:37:11 +0200193 if (unw_ret > 0) {
194 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
Andi Kleen2b14a782006-09-26 10:52:34 +0200195 ops->warning_symbol(data, "DWARF2 unwinder stuck at %s\n",
Jan Beulichea424052006-08-30 19:37:11 +0200196 UNW_PC(&info));
197 if (UNW_SP(&info) >= PAGE_OFFSET) {
Andi Kleen2b14a782006-09-26 10:52:34 +0200198 ops->warning(data, "Leftover inexact backtrace:\n");
Andi Kleen70583162006-07-29 21:42:52 +0200199 stack = (void *)UNW_SP(&info);
Jan Beulichea424052006-08-30 19:37:11 +0200200 } else
Andi Kleen2b14a782006-09-26 10:52:34 +0200201 ops->warning(data, "Full inexact backtrace again:\n");
Jan Beulichea424052006-08-30 19:37:11 +0200202 } else if (call_trace >= 1)
Jan Beulichc33bd9a2006-06-26 13:57:47 +0200203 return;
Andi Kleenc97d20a2006-07-28 14:44:57 +0200204 else
Andi Kleen2b14a782006-09-26 10:52:34 +0200205 ops->warning(data, "Full inexact backtrace again:\n");
Jan Beulichea424052006-08-30 19:37:11 +0200206 } else
Andi Kleen2b14a782006-09-26 10:52:34 +0200207 ops->warning(data, "Inexact backtrace:\n");
208 } else if (!stack) {
209 unsigned long dummy;
210 stack = &dummy;
211 if (task && task != current)
212 stack = (unsigned long *)task->thread.esp;
Jan Beulich176a2712006-06-26 13:57:41 +0200213 }
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (task == current) {
216 /* Grab ebp right from our regs */
217 asm ("movl %%ebp, %0" : "=r" (ebp) : );
218 } else {
219 /* ebp is the last reg pushed by switch_to */
220 ebp = *(unsigned long *) task->thread.esp;
221 }
222
223 while (1) {
224 struct thread_info *context;
225 context = (struct thread_info *)
226 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
Andi Kleen2b14a782006-09-26 10:52:34 +0200227 ebp = print_context_stack(context, stack, ebp, ops, data);
228 /* Should be after the line below, but somewhere
229 in early boot context comes out corrupted and we
230 can't reference it -AK */
231 if (ops->stack(data, "IRQ") < 0)
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 stack = (unsigned long*)context->previous_esp;
234 if (!stack)
235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237}
Andi Kleen2b14a782006-09-26 10:52:34 +0200238EXPORT_SYMBOL(dump_trace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Andi Kleen2b14a782006-09-26 10:52:34 +0200240static void
241print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
242{
243 printk(data);
244 print_symbol(msg, symbol);
245 printk("\n");
246}
247
248static void print_trace_warning(void *data, char *msg)
249{
250 printk("%s%s\n", (char *)data, msg);
251}
252
253static int print_trace_stack(void *data, char *name)
254{
255 return 0;
256}
257
258/*
259 * Print one address/symbol entries per line.
260 */
261static void print_trace_address(void *data, unsigned long addr)
262{
263 printk("%s [<%08lx>] ", (char *)data, addr);
264 print_symbol("%s\n", addr);
265}
266
267static struct stacktrace_ops print_trace_ops = {
268 .warning = print_trace_warning,
269 .warning_symbol = print_trace_warning_symbol,
270 .stack = print_trace_stack,
271 .address = print_trace_address,
272};
273
274static void
275show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
276 unsigned long * stack, char *log_lvl)
277{
278 dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
279 printk("%s =======================\n", log_lvl);
280}
281
282void show_trace(struct task_struct *task, struct pt_regs *regs,
283 unsigned long * stack)
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800284{
Jan Beulich176a2712006-06-26 13:57:41 +0200285 show_trace_log_lvl(task, regs, stack, "");
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800286}
287
Jan Beulich176a2712006-06-26 13:57:41 +0200288static void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
289 unsigned long *esp, char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned long *stack;
292 int i;
293
294 if (esp == NULL) {
295 if (task)
296 esp = (unsigned long*)task->thread.esp;
297 else
298 esp = (unsigned long *)&esp;
299 }
300
301 stack = esp;
302 for(i = 0; i < kstack_depth_to_print; i++) {
303 if (kstack_end(stack))
304 break;
Chuck Ebbert75874d52006-03-23 02:59:52 -0800305 if (i && ((i % 8) == 0))
306 printk("\n%s ", log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 printk("%08lx ", *stack++);
308 }
Chuck Ebbert75874d52006-03-23 02:59:52 -0800309 printk("\n%sCall Trace:\n", log_lvl);
Jan Beulich176a2712006-06-26 13:57:41 +0200310 show_trace_log_lvl(task, regs, esp, log_lvl);
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800311}
312
313void show_stack(struct task_struct *task, unsigned long *esp)
314{
Chuck Ebbert75874d52006-03-23 02:59:52 -0800315 printk(" ");
Jan Beulich176a2712006-06-26 13:57:41 +0200316 show_stack_log_lvl(task, NULL, esp, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319/*
320 * The architecture-independent dump_stack generator
321 */
322void dump_stack(void)
323{
324 unsigned long stack;
325
Jan Beulich176a2712006-06-26 13:57:41 +0200326 show_trace(current, NULL, &stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329EXPORT_SYMBOL(dump_stack);
330
331void show_registers(struct pt_regs *regs)
332{
333 int i;
334 int in_kernel = 1;
335 unsigned long esp;
336 unsigned short ss;
337
338 esp = (unsigned long) (&regs->esp);
Zachary Amsden0998e422005-09-03 15:56:43 -0700339 savesegment(ss, ss);
Jan Beulichdb753bd2006-03-23 02:59:46 -0800340 if (user_mode_vm(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 in_kernel = 0;
342 esp = regs->esp;
343 ss = regs->xss & 0xffff;
344 }
345 print_modules();
Dave Jones9c107802006-01-09 20:51:32 -0800346 printk(KERN_EMERG "CPU: %d\nEIP: %04x:[<%08lx>] %s VLI\n"
Chuck Ebbertb53e8f62006-02-04 23:28:04 -0800347 "EFLAGS: %08lx (%s %.*s) \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
Chuck Ebbertb53e8f62006-02-04 23:28:04 -0800349 print_tainted(), regs->eflags, system_utsname.release,
350 (int)strcspn(system_utsname.version, " "),
351 system_utsname.version);
Dave Jones9c107802006-01-09 20:51:32 -0800352 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
353 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 regs->eax, regs->ebx, regs->ecx, regs->edx);
Dave Jones9c107802006-01-09 20:51:32 -0800355 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 regs->esi, regs->edi, regs->ebp, esp);
Dave Jones9c107802006-01-09 20:51:32 -0800357 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 regs->xds & 0xffff, regs->xes & 0xffff, ss);
Chuck Ebbert7e04a112006-06-23 02:04:31 -0700359 printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
360 TASK_COMM_LEN, current->comm, current->pid,
361 current_thread_info(), current, current->thread_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
363 * When in-kernel, we also print out the stack and code at the
364 * time of the fault..
365 */
366 if (in_kernel) {
Domen Puncer3f3ae342005-06-25 14:58:44 -0700367 u8 __user *eip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dave Jones9c107802006-01-09 20:51:32 -0800369 printk("\n" KERN_EMERG "Stack: ");
Jan Beulich176a2712006-06-26 13:57:41 +0200370 show_stack_log_lvl(NULL, regs, (unsigned long *)esp, KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Jones9c107802006-01-09 20:51:32 -0800372 printk(KERN_EMERG "Code: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Domen Puncer3f3ae342005-06-25 14:58:44 -0700374 eip = (u8 __user *)regs->eip - 43;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 for (i = 0; i < 64; i++, eip++) {
376 unsigned char c;
377
Domen Puncer3f3ae342005-06-25 14:58:44 -0700378 if (eip < (u8 __user *)PAGE_OFFSET || __get_user(c, eip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 printk(" Bad EIP value.");
380 break;
381 }
Domen Puncer3f3ae342005-06-25 14:58:44 -0700382 if (eip == (u8 __user *)regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 printk("<%02x> ", c);
384 else
385 printk("%02x ", c);
386 }
387 }
388 printk("\n");
389}
390
391static void handle_BUG(struct pt_regs *regs)
392{
Chuck Ebbertb7015332006-07-14 00:23:58 -0700393 unsigned long eip = regs->eip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned short ud2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (eip < PAGE_OFFSET)
Chuck Ebbertb7015332006-07-14 00:23:58 -0700397 return;
Domen Puncer3f3ae342005-06-25 14:58:44 -0700398 if (__get_user(ud2, (unsigned short __user *)eip))
Chuck Ebbertb7015332006-07-14 00:23:58 -0700399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (ud2 != 0x0b0f)
Chuck Ebbertb7015332006-07-14 00:23:58 -0700401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Dave Jones9c107802006-01-09 20:51:32 -0800403 printk(KERN_EMERG "------------[ cut here ]------------\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Chuck Ebbertb7015332006-07-14 00:23:58 -0700405#ifdef CONFIG_DEBUG_BUGVERBOSE
406 do {
407 unsigned short line;
408 char *file;
409 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Chuck Ebbertb7015332006-07-14 00:23:58 -0700411 if (__get_user(line, (unsigned short __user *)(eip + 2)))
412 break;
413 if (__get_user(file, (char * __user *)(eip + 4)) ||
414 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
415 file = "<bad filename>";
416
417 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
418 return;
419 } while (0);
420#endif
421 printk(KERN_EMERG "Kernel BUG at [verbose debug info unavailable]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700424/* This is gone through when something in the kernel
425 * has done something bad and is about to be terminated.
426*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427void die(const char * str, struct pt_regs * regs, long err)
428{
429 static struct {
430 spinlock_t lock;
431 u32 lock_owner;
432 int lock_owner_depth;
433 } die = {
434 .lock = SPIN_LOCK_UNLOCKED,
435 .lock_owner = -1,
436 .lock_owner_depth = 0
437 };
438 static int die_counter;
Jan Beuliche43d6742006-01-06 00:11:48 -0800439 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Andrew Mortondd287792006-03-23 03:00:57 -0800441 oops_enter();
442
Ingo Molnar39c715b2005-06-21 17:14:34 -0700443 if (die.lock_owner != raw_smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 console_verbose();
Jan Beuliche43d6742006-01-06 00:11:48 -0800445 spin_lock_irqsave(&die.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 die.lock_owner = smp_processor_id();
447 die.lock_owner_depth = 0;
448 bust_spinlocks(1);
449 }
Jan Beuliche43d6742006-01-06 00:11:48 -0800450 else
451 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (++die.lock_owner_depth < 3) {
454 int nl = 0;
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700455 unsigned long esp;
456 unsigned short ss;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 handle_BUG(regs);
Dave Jones9c107802006-01-09 20:51:32 -0800459 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#ifdef CONFIG_PREEMPT
Dave Jones9c107802006-01-09 20:51:32 -0800461 printk(KERN_EMERG "PREEMPT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 nl = 1;
463#endif
464#ifdef CONFIG_SMP
Dave Jones9c107802006-01-09 20:51:32 -0800465 if (!nl)
466 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 printk("SMP ");
468 nl = 1;
469#endif
470#ifdef CONFIG_DEBUG_PAGEALLOC
Dave Jones9c107802006-01-09 20:51:32 -0800471 if (!nl)
472 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk("DEBUG_PAGEALLOC");
474 nl = 1;
475#endif
476 if (nl)
477 printk("\n");
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800478 if (notify_die(DIE_OOPS, str, regs, err,
479 current->thread.trap_no, SIGSEGV) !=
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700480 NOTIFY_STOP) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800481 show_registers(regs);
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700482 /* Executive summary in case the oops scrolled away */
483 esp = (unsigned long) (&regs->esp);
484 savesegment(ss, ss);
485 if (user_mode(regs)) {
486 esp = regs->esp;
487 ss = regs->xss & 0xffff;
488 }
489 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
490 print_symbol("%s", regs->eip);
491 printk(" SS:ESP %04x:%08lx\n", ss, esp);
492 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800493 else
494 regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else
Dave Jones9c107802006-01-09 20:51:32 -0800496 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 bust_spinlocks(0);
499 die.lock_owner = -1;
Jan Beuliche43d6742006-01-06 00:11:48 -0800500 spin_unlock_irqrestore(&die.lock, flags);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700501
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800502 if (!regs)
503 return;
504
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700505 if (kexec_should_crash(current))
506 crash_kexec(regs);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (in_interrupt())
509 panic("Fatal exception in interrupt");
510
Hormscea6a4b2006-07-30 03:03:34 -0700511 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700512 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700513
Andrew Mortondd287792006-03-23 03:00:57 -0800514 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 do_exit(SIGSEGV);
516}
517
518static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
519{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700520 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 die(str, regs, err);
522}
523
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700524static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
525 struct pt_regs * regs, long error_code,
526 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700528 struct task_struct *tsk = current;
529 tsk->thread.error_code = error_code;
530 tsk->thread.trap_no = trapnr;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (regs->eflags & VM_MASK) {
533 if (vm86)
534 goto vm86_trap;
535 goto trap_signal;
536 }
537
Vincent Hanquez717b5942005-06-23 00:08:45 -0700538 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto kernel_trap;
540
541 trap_signal: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (info)
543 force_sig_info(signr, info, tsk);
544 else
545 force_sig(signr, tsk);
546 return;
547 }
548
549 kernel_trap: {
550 if (!fixup_exception(regs))
551 die(str, regs, error_code);
552 return;
553 }
554
555 vm86_trap: {
556 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
557 if (ret) goto trap_signal;
558 return;
559 }
560}
561
562#define DO_ERROR(trapnr, signr, str, name) \
563fastcall void do_##name(struct pt_regs * regs, long error_code) \
564{ \
565 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
566 == NOTIFY_STOP) \
567 return; \
568 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
569}
570
571#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
572fastcall void do_##name(struct pt_regs * regs, long error_code) \
573{ \
574 siginfo_t info; \
575 info.si_signo = signr; \
576 info.si_errno = 0; \
577 info.si_code = sicode; \
578 info.si_addr = (void __user *)siaddr; \
579 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
580 == NOTIFY_STOP) \
581 return; \
582 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
583}
584
585#define DO_VM86_ERROR(trapnr, signr, str, name) \
586fastcall void do_##name(struct pt_regs * regs, long error_code) \
587{ \
588 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
589 == NOTIFY_STOP) \
590 return; \
591 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
592}
593
594#define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
595fastcall void do_##name(struct pt_regs * regs, long error_code) \
596{ \
597 siginfo_t info; \
598 info.si_signo = signr; \
599 info.si_errno = 0; \
600 info.si_code = sicode; \
601 info.si_addr = (void __user *)siaddr; \
602 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
603 == NOTIFY_STOP) \
604 return; \
605 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
606}
607
608DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
609#ifndef CONFIG_KPROBES
610DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
611#endif
612DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
613DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert631b0342006-01-03 22:36:14 -0500614DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
616DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
617DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
618DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
619DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvaldsa879cbb2005-04-29 09:38:44 -0700620DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700622fastcall void __kprobes do_general_protection(struct pt_regs * regs,
623 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 int cpu = get_cpu();
626 struct tss_struct *tss = &per_cpu(init_tss, cpu);
627 struct thread_struct *thread = &current->thread;
628
629 /*
630 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
631 * invalid offset set (the LAZY one) and the faulting thread has
632 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
633 * and we set the offset field correctly. Then we let the CPU to
634 * restart the faulting instruction.
635 */
636 if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
637 thread->io_bitmap_ptr) {
638 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
639 thread->io_bitmap_max);
640 /*
641 * If the previously set map was extending to higher ports
642 * than the current one, pad extra space with 0xff (no access).
643 */
644 if (thread->io_bitmap_max < tss->io_bitmap_max)
645 memset((char *) tss->io_bitmap +
646 thread->io_bitmap_max, 0xff,
647 tss->io_bitmap_max - thread->io_bitmap_max);
648 tss->io_bitmap_max = thread->io_bitmap_max;
649 tss->io_bitmap_base = IO_BITMAP_OFFSET;
Bart Oldemand5cd4aa2005-10-30 14:59:29 -0800650 tss->io_bitmap_owner = thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 put_cpu();
652 return;
653 }
654 put_cpu();
655
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700656 current->thread.error_code = error_code;
657 current->thread.trap_no = 13;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (regs->eflags & VM_MASK)
660 goto gp_in_vm86;
661
Vincent Hanquez717b5942005-06-23 00:08:45 -0700662 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto gp_in_kernel;
664
665 current->thread.error_code = error_code;
666 current->thread.trap_no = 13;
667 force_sig(SIGSEGV, current);
668 return;
669
670gp_in_vm86:
671 local_irq_enable();
672 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
673 return;
674
675gp_in_kernel:
676 if (!fixup_exception(regs)) {
677 if (notify_die(DIE_GPF, "general protection fault", regs,
678 error_code, 13, SIGSEGV) == NOTIFY_STOP)
679 return;
680 die("general protection fault", regs, error_code);
681 }
682}
683
684static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
685{
Don Zickusc41c5cd2006-09-26 10:52:27 +0200686 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
687 "CPU %d.\n", reason, smp_processor_id());
Dave Jones9c107802006-01-09 20:51:32 -0800688 printk(KERN_EMERG "You probably have a hardware problem with your RAM "
689 "chips\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200690 if (panic_on_unrecovered_nmi)
691 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Don Zickusc41c5cd2006-09-26 10:52:27 +0200693 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Clear and disable the memory parity error line. */
696 clear_mem_error(reason);
697}
698
699static void io_check_error(unsigned char reason, struct pt_regs * regs)
700{
701 unsigned long i;
702
Dave Jones9c107802006-01-09 20:51:32 -0800703 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 show_registers(regs);
705
706 /* Re-enable the IOCK line, wait for a few seconds */
707 reason = (reason & 0xf) | 8;
708 outb(reason, 0x61);
709 i = 2000;
710 while (--i) udelay(1000);
711 reason &= ~8;
712 outb(reason, 0x61);
713}
714
715static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
716{
717#ifdef CONFIG_MCA
718 /* Might actually be able to figure out what the guilty party
719 * is. */
720 if( MCA_bus ) {
721 mca_handle_nmi();
722 return;
723 }
724#endif
Don Zickusc41c5cd2006-09-26 10:52:27 +0200725 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
726 "CPU %d.\n", reason, smp_processor_id());
727 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200728 if (panic_on_unrecovered_nmi)
729 panic("NMI: Not continuing");
730
Don Zickusc41c5cd2006-09-26 10:52:27 +0200731 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734static DEFINE_SPINLOCK(nmi_print_lock);
735
736void die_nmi (struct pt_regs *regs, const char *msg)
737{
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800738 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
George Anzinger748f2ed2005-09-03 15:56:48 -0700739 NOTIFY_STOP)
740 return;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 spin_lock(&nmi_print_lock);
743 /*
744 * We are in trouble anyway, lets at least try
745 * to get a message out.
746 */
747 bust_spinlocks(1);
Dave Jones9c107802006-01-09 20:51:32 -0800748 printk(KERN_EMERG "%s", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 printk(" on CPU%d, eip %08lx, registers:\n",
750 smp_processor_id(), regs->eip);
751 show_registers(regs);
Dave Jones9c107802006-01-09 20:51:32 -0800752 printk(KERN_EMERG "console shuts up ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 console_silent();
754 spin_unlock(&nmi_print_lock);
755 bust_spinlocks(0);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700756
757 /* If we are in kernel we are probably nested up pretty bad
758 * and might aswell get out now while we still can.
759 */
Jan Beulichdb753bd2006-03-23 02:59:46 -0800760 if (!user_mode_vm(regs)) {
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700761 current->thread.trap_no = 2;
762 crash_kexec(regs);
763 }
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 do_exit(SIGSEGV);
766}
767
768static void default_do_nmi(struct pt_regs * regs)
769{
770 unsigned char reason = 0;
771
772 /* Only the BSP gets external NMIs from the system. */
773 if (!smp_processor_id())
774 reason = get_nmi_reason();
775
776 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800777 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 == NOTIFY_STOP)
779 return;
780#ifdef CONFIG_X86_LOCAL_APIC
781 /*
782 * Ok, so this is none of the documented NMI sources,
783 * so it must be the NMI watchdog.
784 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200785 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return;
Don Zickus2fbe7b22006-09-26 10:52:27 +0200787 if (!do_nmi_callback(regs, smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#endif
Don Zickus3adbbcc2006-09-26 10:52:26 +0200789 unknown_nmi_error(reason, regs);
Don Zickus2fbe7b22006-09-26 10:52:27 +0200790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return;
792 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800793 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
795 if (reason & 0x80)
796 mem_parity_error(reason, regs);
797 if (reason & 0x40)
798 io_check_error(reason, regs);
799 /*
800 * Reassert NMI in case it became active meanwhile
801 * as it's edge-triggered.
802 */
803 reassert_nmi();
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806fastcall void do_nmi(struct pt_regs * regs, long error_code)
807{
808 int cpu;
809
810 nmi_enter();
811
812 cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ++nmi_count(cpu);
815
Don Zickus3adbbcc2006-09-26 10:52:26 +0200816 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 nmi_exit();
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821#ifdef CONFIG_KPROBES
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700822fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
825 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700826 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* This is an interrupt gate, because kprobes wants interrupts
828 disabled. Normal trap handlers don't. */
829 restore_interrupts(regs);
830 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832#endif
833
834/*
835 * Our handling of the processor debug registers is non-trivial.
836 * We do not clear them on entry and exit from the kernel. Therefore
837 * it is possible to get a watchpoint trap here from inside the kernel.
838 * However, the code in ./ptrace.c has ensured that the user can
839 * only set watchpoints on userspace addresses. Therefore the in-kernel
840 * watchpoint trap can only occur in code which is reading/writing
841 * from user space. Such code must not hold kernel locks (since it
842 * can equally take a page fault), therefore it is safe to call
843 * force_sig_info even though that claims and releases locks.
844 *
845 * Code in ./signal.c ensures that the debug control register
846 * is restored before we deliver any signal, and therefore that
847 * user code runs with the correct debug control register even though
848 * we clear it here.
849 *
850 * Being careful here means that we don't have to be as careful in a
851 * lot of more complicated places (task switching can be a bit lazy
852 * about restoring all the debug state, and ptrace doesn't have to
853 * find every occurrence of the TF bit that could be saved away even
854 * by user code)
855 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700856fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 unsigned int condition;
859 struct task_struct *tsk = current;
860
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700861 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
864 SIGTRAP) == NOTIFY_STOP)
865 return;
866 /* It's safe to allow irq's after DR6 has been saved */
867 if (regs->eflags & X86_EFLAGS_IF)
868 local_irq_enable();
869
870 /* Mask out spurious debug traps due to lazy DR7 setting */
871 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
872 if (!tsk->thread.debugreg[7])
873 goto clear_dr7;
874 }
875
876 if (regs->eflags & VM_MASK)
877 goto debug_vm86;
878
879 /* Save debug status register where ptrace can see it */
880 tsk->thread.debugreg[6] = condition;
881
882 /*
883 * Single-stepping through TF: make sure we ignore any events in
884 * kernel space (but re-enable TF when returning to user mode).
885 */
886 if (condition & DR_STEP) {
887 /*
888 * We already checked v86 mode above, so we can
889 * check for kernel mode by just checking the CPL
890 * of CS.
891 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700892 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto clear_TF_reenable;
894 }
895
896 /* Ok, finally something we can handle */
897 send_sigtrap(tsk, regs, error_code);
898
899 /* Disable additional traps. They'll be re-enabled when
900 * the signal is delivered.
901 */
902clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700903 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return;
905
906debug_vm86:
907 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
908 return;
909
910clear_TF_reenable:
911 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
912 regs->eflags &= ~TF_MASK;
913 return;
914}
915
916/*
917 * Note that we play around with the 'TS' bit in an attempt to get
918 * the correct behaviour even in the presence of the asynchronous
919 * IRQ13 behaviour
920 */
921void math_error(void __user *eip)
922{
923 struct task_struct * task;
924 siginfo_t info;
925 unsigned short cwd, swd;
926
927 /*
928 * Save the info for the exception handler and clear the error.
929 */
930 task = current;
931 save_init_fpu(task);
932 task->thread.trap_no = 16;
933 task->thread.error_code = 0;
934 info.si_signo = SIGFPE;
935 info.si_errno = 0;
936 info.si_code = __SI_FAULT;
937 info.si_addr = eip;
938 /*
939 * (~cwd & swd) will mask out exceptions that are not set to unmasked
940 * status. 0x3f is the exception bits in these regs, 0x200 is the
941 * C1 reg you need in case of a stack fault, 0x040 is the stack
942 * fault bit. We should only be taking one exception at a time,
943 * so if this combination doesn't produce any single exception,
944 * then we have a bad program that isn't syncronizing its FPU usage
945 * and it will suffer the consequences since we won't be able to
946 * fully reproduce the context of the exception
947 */
948 cwd = get_fpu_cwd(task);
949 swd = get_fpu_swd(task);
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400950 switch (swd & ~cwd & 0x3f) {
Chuck Ebbert33333372005-09-13 04:55:41 -0400951 case 0x000: /* No unmasked exception */
952 return;
953 default: /* Multiple exceptions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 break;
955 case 0x001: /* Invalid Op */
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400956 /*
957 * swd & 0x240 == 0x040: Stack Underflow
958 * swd & 0x240 == 0x240: Stack Overflow
959 * User must clear the SF bit (0x40) if set
960 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 info.si_code = FPE_FLTINV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963 case 0x002: /* Denormalize */
964 case 0x010: /* Underflow */
965 info.si_code = FPE_FLTUND;
966 break;
967 case 0x004: /* Zero Divide */
968 info.si_code = FPE_FLTDIV;
969 break;
970 case 0x008: /* Overflow */
971 info.si_code = FPE_FLTOVF;
972 break;
973 case 0x020: /* Precision */
974 info.si_code = FPE_FLTRES;
975 break;
976 }
977 force_sig_info(SIGFPE, &info, task);
978}
979
980fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
981{
982 ignore_fpu_irq = 1;
983 math_error((void __user *)regs->eip);
984}
985
986static void simd_math_error(void __user *eip)
987{
988 struct task_struct * task;
989 siginfo_t info;
990 unsigned short mxcsr;
991
992 /*
993 * Save the info for the exception handler and clear the error.
994 */
995 task = current;
996 save_init_fpu(task);
997 task->thread.trap_no = 19;
998 task->thread.error_code = 0;
999 info.si_signo = SIGFPE;
1000 info.si_errno = 0;
1001 info.si_code = __SI_FAULT;
1002 info.si_addr = eip;
1003 /*
1004 * The SIMD FPU exceptions are handled a little differently, as there
1005 * is only a single status/control register. Thus, to determine which
1006 * unmasked exception was caught we must mask the exception mask bits
1007 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1008 */
1009 mxcsr = get_fpu_mxcsr(task);
1010 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1011 case 0x000:
1012 default:
1013 break;
1014 case 0x001: /* Invalid Op */
1015 info.si_code = FPE_FLTINV;
1016 break;
1017 case 0x002: /* Denormalize */
1018 case 0x010: /* Underflow */
1019 info.si_code = FPE_FLTUND;
1020 break;
1021 case 0x004: /* Zero Divide */
1022 info.si_code = FPE_FLTDIV;
1023 break;
1024 case 0x008: /* Overflow */
1025 info.si_code = FPE_FLTOVF;
1026 break;
1027 case 0x020: /* Precision */
1028 info.si_code = FPE_FLTRES;
1029 break;
1030 }
1031 force_sig_info(SIGFPE, &info, task);
1032}
1033
1034fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
1035 long error_code)
1036{
1037 if (cpu_has_xmm) {
1038 /* Handle SIMD FPU exceptions on PIII+ processors. */
1039 ignore_fpu_irq = 1;
1040 simd_math_error((void __user *)regs->eip);
1041 } else {
1042 /*
1043 * Handle strange cache flush from user space exception
1044 * in all other cases. This is undocumented behaviour.
1045 */
1046 if (regs->eflags & VM_MASK) {
1047 handle_vm86_fault((struct kernel_vm86_regs *)regs,
1048 error_code);
1049 return;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 current->thread.trap_no = 19;
1052 current->thread.error_code = error_code;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -07001053 die_if_kernel("cache flush denied", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 force_sig(SIGSEGV, current);
1055 }
1056}
1057
1058fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
1059 long error_code)
1060{
1061#if 0
1062 /* No need to warn about this any longer. */
1063 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1064#endif
1065}
1066
1067fastcall void setup_x86_bogus_stack(unsigned char * stk)
1068{
1069 unsigned long *switch16_ptr, *switch32_ptr;
1070 struct pt_regs *regs;
1071 unsigned long stack_top, stack_bot;
1072 unsigned short iret_frame16_off;
1073 int cpu = smp_processor_id();
1074 /* reserve the space on 32bit stack for the magic switch16 pointer */
1075 memmove(stk, stk + 8, sizeof(struct pt_regs));
1076 switch16_ptr = (unsigned long *)(stk + sizeof(struct pt_regs));
1077 regs = (struct pt_regs *)stk;
1078 /* now the switch32 on 16bit stack */
1079 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
1080 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
1081 switch32_ptr = (unsigned long *)(stack_top - 8);
1082 iret_frame16_off = CPU_16BIT_STACK_SIZE - 8 - 20;
1083 /* copy iret frame on 16bit stack */
1084 memcpy((void *)(stack_bot + iret_frame16_off), &regs->eip, 20);
1085 /* fill in the switch pointers */
1086 switch16_ptr[0] = (regs->esp & 0xffff0000) | iret_frame16_off;
1087 switch16_ptr[1] = __ESPFIX_SS;
1088 switch32_ptr[0] = (unsigned long)stk + sizeof(struct pt_regs) +
1089 8 - CPU_16BIT_STACK_SIZE;
1090 switch32_ptr[1] = __KERNEL_DS;
1091}
1092
1093fastcall unsigned char * fixup_x86_bogus_stack(unsigned short sp)
1094{
1095 unsigned long *switch32_ptr;
1096 unsigned char *stack16, *stack32;
1097 unsigned long stack_top, stack_bot;
1098 int len;
1099 int cpu = smp_processor_id();
1100 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
1101 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
1102 switch32_ptr = (unsigned long *)(stack_top - 8);
1103 /* copy the data from 16bit stack to 32bit stack */
1104 len = CPU_16BIT_STACK_SIZE - 8 - sp;
1105 stack16 = (unsigned char *)(stack_bot + sp);
1106 stack32 = (unsigned char *)
1107 (switch32_ptr[0] + CPU_16BIT_STACK_SIZE - 8 - len);
1108 memcpy(stack32, stack16, len);
1109 return stack32;
1110}
1111
1112/*
1113 * 'math_state_restore()' saves the current math information in the
1114 * old math state array, and gets the new ones from the current task
1115 *
1116 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1117 * Don't touch unless you *really* know how it works.
1118 *
1119 * Must be called with kernel preemption disabled (in this case,
1120 * local interrupts are disabled at the call-site in entry.S).
1121 */
1122asmlinkage void math_state_restore(struct pt_regs regs)
1123{
1124 struct thread_info *thread = current_thread_info();
1125 struct task_struct *tsk = thread->task;
1126
1127 clts(); /* Allow maths ops (or we recurse) */
1128 if (!tsk_used_math(tsk))
1129 init_fpu(tsk);
1130 restore_fpu(tsk);
1131 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
1132}
1133
1134#ifndef CONFIG_MATH_EMULATION
1135
1136asmlinkage void math_emulate(long arg)
1137{
Dave Jones9c107802006-01-09 20:51:32 -08001138 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1139 printk(KERN_EMERG "killing %s.\n",current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 force_sig(SIGFPE,current);
1141 schedule();
1142}
1143
1144#endif /* CONFIG_MATH_EMULATION */
1145
1146#ifdef CONFIG_X86_F00F_BUG
1147void __init trap_init_f00f_bug(void)
1148{
1149 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1150
1151 /*
1152 * Update the IDT descriptor and reload the IDT so that
1153 * it uses the read-only mapped virtual address.
1154 */
1155 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -07001156 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158#endif
1159
1160#define _set_gate(gate_addr,type,dpl,addr,seg) \
1161do { \
1162 int __d0, __d1; \
1163 __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
1164 "movw %4,%%dx\n\t" \
1165 "movl %%eax,%0\n\t" \
1166 "movl %%edx,%1" \
1167 :"=m" (*((long *) (gate_addr))), \
1168 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
1169 :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
1170 "3" ((char *) (addr)),"2" ((seg) << 16)); \
1171} while (0)
1172
1173
1174/*
1175 * This needs to use 'idt_table' rather than 'idt', and
1176 * thus use the _nonmapped_ version of the IDT, as the
1177 * Pentium F0 0F bugfix can have resulted in the mapped
1178 * IDT being write-protected.
1179 */
1180void set_intr_gate(unsigned int n, void *addr)
1181{
1182 _set_gate(idt_table+n,14,0,addr,__KERNEL_CS);
1183}
1184
1185/*
1186 * This routine sets up an interrupt gate at directory privilege level 3.
1187 */
1188static inline void set_system_intr_gate(unsigned int n, void *addr)
1189{
1190 _set_gate(idt_table+n, 14, 3, addr, __KERNEL_CS);
1191}
1192
1193static void __init set_trap_gate(unsigned int n, void *addr)
1194{
1195 _set_gate(idt_table+n,15,0,addr,__KERNEL_CS);
1196}
1197
1198static void __init set_system_gate(unsigned int n, void *addr)
1199{
1200 _set_gate(idt_table+n,15,3,addr,__KERNEL_CS);
1201}
1202
1203static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
1204{
1205 _set_gate(idt_table+n,5,0,0,(gdt_entry<<3));
1206}
1207
1208
1209void __init trap_init(void)
1210{
1211#ifdef CONFIG_EISA
1212 void __iomem *p = ioremap(0x0FFFD9, 4);
1213 if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
1214 EISA_bus = 1;
1215 }
1216 iounmap(p);
1217#endif
1218
1219#ifdef CONFIG_X86_LOCAL_APIC
1220 init_apic_mappings();
1221#endif
1222
1223 set_trap_gate(0,&divide_error);
1224 set_intr_gate(1,&debug);
1225 set_intr_gate(2,&nmi);
Jan Beulicheb05c322006-01-06 00:11:49 -08001226 set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 set_system_gate(4,&overflow);
Jan Beulicheb05c322006-01-06 00:11:49 -08001228 set_trap_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 set_trap_gate(6,&invalid_op);
1230 set_trap_gate(7,&device_not_available);
1231 set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1232 set_trap_gate(9,&coprocessor_segment_overrun);
1233 set_trap_gate(10,&invalid_TSS);
1234 set_trap_gate(11,&segment_not_present);
1235 set_trap_gate(12,&stack_segment);
1236 set_trap_gate(13,&general_protection);
1237 set_intr_gate(14,&page_fault);
1238 set_trap_gate(15,&spurious_interrupt_bug);
1239 set_trap_gate(16,&coprocessor_error);
1240 set_trap_gate(17,&alignment_check);
1241#ifdef CONFIG_X86_MCE
1242 set_trap_gate(18,&machine_check);
1243#endif
1244 set_trap_gate(19,&simd_coprocessor_error);
1245
Jan Beulichd43c6e82006-01-06 00:11:49 -08001246 if (cpu_has_fxsr) {
1247 /*
1248 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1249 * Generates a compile-time "error: zero width for bit-field" if
1250 * the alignment is wrong.
1251 */
1252 struct fxsrAlignAssert {
1253 int _:!(offsetof(struct task_struct,
1254 thread.i387.fxsave) & 15);
1255 };
1256
1257 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1258 set_in_cr4(X86_CR4_OSFXSR);
1259 printk("done.\n");
1260 }
1261 if (cpu_has_xmm) {
1262 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1263 "support... ");
1264 set_in_cr4(X86_CR4_OSXMMEXCPT);
1265 printk("done.\n");
1266 }
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 set_system_gate(SYSCALL_VECTOR,&system_call);
1269
1270 /*
1271 * Should be a barrier for any external CPU state.
1272 */
1273 cpu_init();
1274
1275 trap_init_hook();
1276}
1277
1278static int __init kstack_setup(char *s)
1279{
1280 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001281 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283__setup("kstack=", kstack_setup);
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001284
Jan Beulichea424052006-08-30 19:37:11 +02001285#ifdef CONFIG_STACK_UNWIND
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001286static int __init call_trace_setup(char *s)
1287{
1288 if (strcmp(s, "old") == 0)
1289 call_trace = -1;
1290 else if (strcmp(s, "both") == 0)
1291 call_trace = 0;
Andi Kleen70583162006-07-29 21:42:52 +02001292 else if (strcmp(s, "newfallback") == 0)
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001293 call_trace = 1;
Andi Kleen70583162006-07-29 21:42:52 +02001294 else if (strcmp(s, "new") == 2)
1295 call_trace = 2;
Jan Beulichc33bd9a2006-06-26 13:57:47 +02001296 return 1;
1297}
1298__setup("call_trace=", call_trace_setup);
Jan Beulichea424052006-08-30 19:37:11 +02001299#endif