blob: 05dbbf40ccf502194cb0fa4031e1586e36711e7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86-64/traps.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
6 *
7 * Pentium III FXSR, SSE support
8 * Gareth Hughes <gareth@valinux.com>, May 2000
9 *
10 * $Id: traps.c,v 1.36 2002/03/24 11:09:10 ak Exp $
11 */
12
13/*
14 * 'Traps.c' handles hardware traps and faults after we have saved some
15 * state in 'entry.S'.
16 */
17#include <linux/config.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/string.h>
21#include <linux/errno.h>
22#include <linux/ptrace.h>
23#include <linux/timer.h>
24#include <linux/mm.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/spinlock.h>
28#include <linux/interrupt.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
akpm@osdl.org35faa712005-04-16 15:24:54 -070031#include <linux/nmi.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070032#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/system.h>
35#include <asm/uaccess.h>
36#include <asm/io.h>
37#include <asm/atomic.h>
38#include <asm/debugreg.h>
39#include <asm/desc.h>
40#include <asm/i387.h>
41#include <asm/kdebug.h>
42#include <asm/processor.h>
43
44#include <asm/smp.h>
45#include <asm/pgalloc.h>
46#include <asm/pda.h>
47#include <asm/proto.h>
48#include <asm/nmi.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050extern struct gate_struct idt_table[256];
51
52asmlinkage void divide_error(void);
53asmlinkage void debug(void);
54asmlinkage void nmi(void);
55asmlinkage void int3(void);
56asmlinkage void overflow(void);
57asmlinkage void bounds(void);
58asmlinkage void invalid_op(void);
59asmlinkage void device_not_available(void);
60asmlinkage void double_fault(void);
61asmlinkage void coprocessor_segment_overrun(void);
62asmlinkage void invalid_TSS(void);
63asmlinkage void segment_not_present(void);
64asmlinkage void stack_segment(void);
65asmlinkage void general_protection(void);
66asmlinkage void page_fault(void);
67asmlinkage void coprocessor_error(void);
68asmlinkage void simd_coprocessor_error(void);
69asmlinkage void reserved(void);
70asmlinkage void alignment_check(void);
71asmlinkage void machine_check(void);
72asmlinkage void spurious_interrupt_bug(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74struct notifier_block *die_chain;
75static DEFINE_SPINLOCK(die_notifier_lock);
76
77int register_die_notifier(struct notifier_block *nb)
78{
79 int err = 0;
80 unsigned long flags;
Jan Beulich8c914cb2006-03-25 16:29:40 +010081
82 vmalloc_sync_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 spin_lock_irqsave(&die_notifier_lock, flags);
84 err = notifier_chain_register(&die_chain, nb);
85 spin_unlock_irqrestore(&die_notifier_lock, flags);
86 return err;
87}
88
89static inline void conditional_sti(struct pt_regs *regs)
90{
91 if (regs->eflags & X86_EFLAGS_IF)
92 local_irq_enable();
93}
94
John Blackwooda65d17c2006-02-12 14:34:58 -080095static inline void preempt_conditional_sti(struct pt_regs *regs)
96{
97 preempt_disable();
98 if (regs->eflags & X86_EFLAGS_IF)
99 local_irq_enable();
100}
101
102static inline void preempt_conditional_cli(struct pt_regs *regs)
103{
104 if (regs->eflags & X86_EFLAGS_IF)
105 local_irq_disable();
106 preempt_enable_no_resched();
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int kstack_depth_to_print = 10;
110
111#ifdef CONFIG_KALLSYMS
112#include <linux/kallsyms.h>
113int printk_address(unsigned long address)
114{
115 unsigned long offset = 0, symsize;
116 const char *symname;
117 char *modname;
118 char *delim = ":";
119 char namebuf[128];
120
121 symname = kallsyms_lookup(address, &symsize, &offset, &modname, namebuf);
122 if (!symname)
123 return printk("[<%016lx>]", address);
124 if (!modname)
125 modname = delim = "";
126 return printk("<%016lx>{%s%s%s%s%+ld}",
Roberto Nibali2b692a82006-03-25 16:29:55 +0100127 address, delim, modname, delim, symname, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129#else
130int printk_address(unsigned long address)
131{
132 return printk("[<%016lx>]", address);
133}
134#endif
135
Andi Kleen0a658002005-04-16 15:25:17 -0700136static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
137 unsigned *usedp, const char **idp)
138{
Jan Beulichb556b352006-01-11 22:43:00 +0100139 static char ids[][8] = {
Andi Kleen0a658002005-04-16 15:25:17 -0700140 [DEBUG_STACK - 1] = "#DB",
141 [NMI_STACK - 1] = "NMI",
142 [DOUBLEFAULT_STACK - 1] = "#DF",
143 [STACKFAULT_STACK - 1] = "#SS",
144 [MCE_STACK - 1] = "#MC",
Jan Beulichb556b352006-01-11 22:43:00 +0100145#if DEBUG_STKSZ > EXCEPTION_STKSZ
146 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
147#endif
Andi Kleen0a658002005-04-16 15:25:17 -0700148 };
149 unsigned k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Andi Kleen0a658002005-04-16 15:25:17 -0700151 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
152 unsigned long end;
153
Jan Beulichb556b352006-01-11 22:43:00 +0100154 switch (k + 1) {
155#if DEBUG_STKSZ > EXCEPTION_STKSZ
156 case DEBUG_STACK:
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100157 end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ;
Jan Beulichb556b352006-01-11 22:43:00 +0100158 break;
159#endif
160 default:
161 end = per_cpu(init_tss, cpu).ist[k];
162 break;
163 }
Andi Kleen0a658002005-04-16 15:25:17 -0700164 if (stack >= end)
165 continue;
166 if (stack >= end - EXCEPTION_STKSZ) {
167 if (*usedp & (1U << k))
168 break;
169 *usedp |= 1U << k;
170 *idp = ids[k];
171 return (unsigned long *)end;
172 }
Jan Beulichb556b352006-01-11 22:43:00 +0100173#if DEBUG_STKSZ > EXCEPTION_STKSZ
174 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
175 unsigned j = N_EXCEPTION_STACKS - 1;
176
177 do {
178 ++j;
179 end -= EXCEPTION_STKSZ;
180 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
181 } while (stack < end - EXCEPTION_STKSZ);
182 if (*usedp & (1U << j))
183 break;
184 *usedp |= 1U << j;
185 *idp = ids[j];
186 return (unsigned long *)end;
187 }
188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 return NULL;
Andi Kleen0a658002005-04-16 15:25:17 -0700191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/*
194 * x86-64 can have upto three kernel stacks:
195 * process stack
196 * interrupt stack
Andi Kleen0a658002005-04-16 15:25:17 -0700197 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
199
200void show_trace(unsigned long *stack)
201{
Andi Kleen0a658002005-04-16 15:25:17 -0700202 const unsigned cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100203 unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int i;
Andi Kleen0a658002005-04-16 15:25:17 -0700205 unsigned used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 printk("\nCall Trace:");
Andi Kleen0a658002005-04-16 15:25:17 -0700208
209#define HANDLE_STACK(cond) \
210 do while (cond) { \
Jan Beulich1b2f6302006-01-11 22:46:45 +0100211 unsigned long addr = *stack++; \
Andi Kleen0a658002005-04-16 15:25:17 -0700212 if (kernel_text_address(addr)) { \
Jan Beulich1b2f6302006-01-11 22:46:45 +0100213 if (i > 50) { \
214 printk("\n "); \
215 i = 0; \
216 } \
217 else \
218 i += printk(" "); \
Andi Kleen0a658002005-04-16 15:25:17 -0700219 /* \
220 * If the address is either in the text segment of the \
221 * kernel, or in the region which contains vmalloc'ed \
222 * memory, it *may* be the address of a calling \
223 * routine; if so, print it so that someone tracing \
224 * down the cause of the crash will be able to figure \
225 * out the call path that was taken. \
226 */ \
227 i += printk_address(addr); \
Andi Kleen0a658002005-04-16 15:25:17 -0700228 } \
229 } while (0)
230
Jan Beulich1b2f6302006-01-11 22:46:45 +0100231 for(i = 11; ; ) {
Andi Kleen0a658002005-04-16 15:25:17 -0700232 const char *id;
233 unsigned long *estack_end;
234 estack_end = in_exception_stack(cpu, (unsigned long)stack,
235 &used, &id);
236
237 if (estack_end) {
Jan Beulich1b2f6302006-01-11 22:46:45 +0100238 i += printk(" <%s>", id);
Andi Kleen0a658002005-04-16 15:25:17 -0700239 HANDLE_STACK (stack < estack_end);
Jan Beulich1b2f6302006-01-11 22:46:45 +0100240 i += printk(" <EOE>");
Andi Kleen0a658002005-04-16 15:25:17 -0700241 stack = (unsigned long *) estack_end[-2];
242 continue;
243 }
244 if (irqstack_end) {
245 unsigned long *irqstack;
246 irqstack = irqstack_end -
247 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
248
249 if (stack >= irqstack && stack < irqstack_end) {
Jan Beulich1b2f6302006-01-11 22:46:45 +0100250 i += printk(" <IRQ>");
Andi Kleen0a658002005-04-16 15:25:17 -0700251 HANDLE_STACK (stack < irqstack_end);
252 stack = (unsigned long *) (irqstack_end[-1]);
253 irqstack_end = NULL;
Jan Beulich1b2f6302006-01-11 22:46:45 +0100254 i += printk(" <EOI>");
Andi Kleen0a658002005-04-16 15:25:17 -0700255 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 }
Andi Kleen0a658002005-04-16 15:25:17 -0700258 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Andi Kleen0a658002005-04-16 15:25:17 -0700260
261 HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
262#undef HANDLE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk("\n");
264}
265
266void show_stack(struct task_struct *tsk, unsigned long * rsp)
267{
268 unsigned long *stack;
269 int i;
270 const int cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100271 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
272 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 // debugging aid: "show_stack(NULL, NULL);" prints the
275 // back trace for this cpu.
276
277 if (rsp == NULL) {
278 if (tsk)
279 rsp = (unsigned long *)tsk->thread.rsp;
280 else
281 rsp = (unsigned long *)&rsp;
282 }
283
284 stack = rsp;
285 for(i=0; i < kstack_depth_to_print; i++) {
286 if (stack >= irqstack && stack <= irqstack_end) {
287 if (stack == irqstack_end) {
288 stack = (unsigned long *) (irqstack_end[-1]);
289 printk(" <EOI> ");
290 }
291 } else {
292 if (((long) stack & (THREAD_SIZE-1)) == 0)
293 break;
294 }
295 if (i && ((i % 4) == 0))
296 printk("\n ");
297 printk("%016lx ", *stack++);
akpm@osdl.org35faa712005-04-16 15:24:54 -0700298 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 show_trace((unsigned long *)rsp);
301}
302
303/*
304 * The architecture-independent dump_stack generator
305 */
306void dump_stack(void)
307{
308 unsigned long dummy;
309 show_trace(&dummy);
310}
311
312EXPORT_SYMBOL(dump_stack);
313
314void show_registers(struct pt_regs *regs)
315{
316 int i;
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700317 int in_kernel = !user_mode(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned long rsp;
319 const int cpu = safe_smp_processor_id();
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100320 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 rsp = regs->rsp;
323
324 printk("CPU %d ", cpu);
325 __show_regs(regs);
326 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
Al Viroe4f17c42006-01-12 01:05:38 -0800327 cur->comm, cur->pid, task_thread_info(cur), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /*
330 * When in-kernel, we also print out the stack and code at the
331 * time of the fault..
332 */
333 if (in_kernel) {
334
335 printk("Stack: ");
336 show_stack(NULL, (unsigned long*)rsp);
337
338 printk("\nCode: ");
Roberto Nibali2b692a82006-03-25 16:29:55 +0100339 if (regs->rip < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto bad;
341
Roberto Nibali2b692a82006-03-25 16:29:55 +0100342 for (i=0; i<20; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned char c;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100344 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345bad:
346 printk(" Bad RIP value.");
347 break;
348 }
349 printk("%02x ", c);
350 }
351 }
352 printk("\n");
353}
354
355void handle_BUG(struct pt_regs *regs)
356{
357 struct bug_frame f;
Jan Beulich5f1d1892006-01-11 22:46:48 +0100358 long len;
359 const char *prefix = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700361 if (user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return;
Stephen Hemminger77a75332006-01-11 22:46:30 +0100363 if (__copy_from_user(&f, (const void __user *) regs->rip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sizeof(struct bug_frame)))
365 return;
Jan Beulich049cdef2005-09-12 18:49:25 +0200366 if (f.filename >= 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
368 return;
Jan Beulich5f1d1892006-01-11 22:46:48 +0100369 len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1;
370 if (len < 0 || len >= PATH_MAX)
Jan Beulich049cdef2005-09-12 18:49:25 +0200371 f.filename = (int)(long)"unmapped filename";
Jan Beulich5f1d1892006-01-11 22:46:48 +0100372 else if (len > 50) {
373 f.filename += len - 50;
374 prefix = "...";
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
Jan Beulich5f1d1892006-01-11 22:46:48 +0100377 printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700380#ifdef CONFIG_BUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381void out_of_line_bug(void)
382{
383 BUG();
384}
Alexander Nyberg4f60fdf2005-05-25 12:31:28 -0700385#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387static DEFINE_SPINLOCK(die_lock);
388static int die_owner = -1;
389
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100390unsigned __kprobes long oops_begin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jan Beulich12091402005-09-12 18:49:24 +0200392 int cpu = safe_smp_processor_id();
393 unsigned long flags;
394
395 /* racy, but better than risking deadlock. */
396 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!spin_trylock(&die_lock)) {
398 if (cpu == die_owner)
399 /* nested oops. should stop eventually */;
400 else
Jan Beulich12091402005-09-12 18:49:24 +0200401 spin_lock(&die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Jan Beulich12091402005-09-12 18:49:24 +0200403 die_owner = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 console_verbose();
Jan Beulich12091402005-09-12 18:49:24 +0200405 bust_spinlocks(1);
406 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100409void __kprobes oops_end(unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 die_owner = -1;
Jan Beulich12091402005-09-12 18:49:24 +0200412 bust_spinlocks(0);
413 spin_unlock_irqrestore(&die_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (panic_on_oops)
Jan Beulich12091402005-09-12 18:49:24 +0200415 panic("Oops");
416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100418void __kprobes __die(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 static int die_counter;
421 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
422#ifdef CONFIG_PREEMPT
423 printk("PREEMPT ");
424#endif
425#ifdef CONFIG_SMP
426 printk("SMP ");
427#endif
428#ifdef CONFIG_DEBUG_PAGEALLOC
429 printk("DEBUG_PAGEALLOC");
430#endif
431 printk("\n");
Jan Beulich6e3f3612006-01-11 22:42:14 +0100432 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 show_registers(regs);
434 /* Executive summary in case the oops scrolled away */
435 printk(KERN_ALERT "RIP ");
436 printk_address(regs->rip);
437 printk(" RSP <%016lx>\n", regs->rsp);
438}
439
440void die(const char * str, struct pt_regs * regs, long err)
441{
Jan Beulich12091402005-09-12 18:49:24 +0200442 unsigned long flags = oops_begin();
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 handle_BUG(regs);
445 __die(str, regs, err);
Jan Beulich12091402005-09-12 18:49:24 +0200446 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 do_exit(SIGSEGV);
448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100450void __kprobes die_nmi(char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Jan Beulich12091402005-09-12 18:49:24 +0200452 unsigned long flags = oops_begin();
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * We are in trouble anyway, lets at least try
456 * to get a message out.
457 */
458 printk(str, safe_smp_processor_id());
459 show_registers(regs);
460 if (panic_on_timeout || panic_on_oops)
461 panic("nmi watchdog");
462 printk("console shuts up ...\n");
Jan Beulich12091402005-09-12 18:49:24 +0200463 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 do_exit(SIGSEGV);
465}
466
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700467static void __kprobes do_trap(int trapnr, int signr, char *str,
468 struct pt_regs * regs, long error_code,
469 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100471 struct task_struct *tsk = current;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 conditional_sti(regs);
474
Jan Beulich6e3f3612006-01-11 22:42:14 +0100475 tsk->thread.error_code = error_code;
476 tsk->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Jan Beulich6e3f3612006-01-11 22:42:14 +0100478 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (exception_trace && unhandled_signal(tsk, signr))
480 printk(KERN_INFO
481 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
482 tsk->comm, tsk->pid, str,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100483 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (info)
486 force_sig_info(signr, info, tsk);
487 else
488 force_sig(signr, tsk);
489 return;
490 }
491
492
493 /* kernel trap */
494 {
495 const struct exception_table_entry *fixup;
496 fixup = search_exception_tables(regs->rip);
Roberto Nibali2b692a82006-03-25 16:29:55 +0100497 if (fixup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 regs->rip = fixup->fixup;
Roberto Nibali2b692a82006-03-25 16:29:55 +0100499 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 die(str, regs, error_code);
501 return;
502 }
503}
504
505#define DO_ERROR(trapnr, signr, str, name) \
506asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
507{ \
508 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
509 == NOTIFY_STOP) \
510 return; \
511 do_trap(trapnr, signr, str, regs, error_code, NULL); \
512}
513
514#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
515asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
516{ \
517 siginfo_t info; \
518 info.si_signo = signr; \
519 info.si_errno = 0; \
520 info.si_code = sicode; \
521 info.si_addr = (void __user *)siaddr; \
522 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
523 == NOTIFY_STOP) \
524 return; \
525 do_trap(trapnr, signr, str, regs, error_code, &info); \
526}
527
528DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
529DO_ERROR( 4, SIGSEGV, "overflow", overflow)
530DO_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert100c0e32006-01-11 22:46:00 +0100531DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
533DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
534DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
535DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
536DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
537DO_ERROR(18, SIGSEGV, "reserved", reserved)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700538DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
Jan Beulicheca37c12006-01-11 22:42:17 +0100539
540asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
541{
542 static const char str[] = "double fault";
543 struct task_struct *tsk = current;
544
545 /* Return not checked because double check cannot be ignored */
546 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
547
548 tsk->thread.error_code = error_code;
549 tsk->thread.trap_no = 8;
550
551 /* This is always a kernel trap and never fixable (and thus must
552 never return). */
553 for (;;)
554 die(str, regs, error_code);
555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700557asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
558 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Jan Beulich6e3f3612006-01-11 22:42:14 +0100560 struct task_struct *tsk = current;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 conditional_sti(regs);
563
Jan Beulich6e3f3612006-01-11 22:42:14 +0100564 tsk->thread.error_code = error_code;
565 tsk->thread.trap_no = 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jan Beulich6e3f3612006-01-11 22:42:14 +0100567 if (user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
569 printk(KERN_INFO
570 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
571 tsk->comm, tsk->pid,
Roberto Nibali2b692a82006-03-25 16:29:55 +0100572 regs->rip, regs->rsp, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 force_sig(SIGSEGV, tsk);
575 return;
576 }
577
578 /* kernel gp */
579 {
580 const struct exception_table_entry *fixup;
581 fixup = search_exception_tables(regs->rip);
582 if (fixup) {
583 regs->rip = fixup->fixup;
584 return;
585 }
586 if (notify_die(DIE_GPF, "general protection fault", regs,
587 error_code, 13, SIGSEGV) == NOTIFY_STOP)
588 return;
589 die("general protection fault", regs, error_code);
590 }
591}
592
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100593static __kprobes void
594mem_parity_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
597 printk("You probably have a hardware problem with your RAM chips\n");
598
599 /* Clear and disable the memory parity error line. */
600 reason = (reason & 0xf) | 4;
601 outb(reason, 0x61);
602}
603
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100604static __kprobes void
605io_check_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 printk("NMI: IOCK error (debug interrupt?)\n");
608 show_registers(regs);
609
610 /* Re-enable the IOCK line, wait for a few seconds */
611 reason = (reason & 0xf) | 8;
612 outb(reason, 0x61);
613 mdelay(2000);
614 reason &= ~8;
615 outb(reason, 0x61);
616}
617
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100618static __kprobes void
619unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{ printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
621 printk("Dazed and confused, but trying to continue\n");
622 printk("Do you have a strange power saving mode enabled?\n");
623}
624
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700625/* Runs on IST stack. This code must keep interrupts off all the time.
626 Nested NMIs are prevented by the CPU. */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100627asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 unsigned char reason = 0;
Ashok Raj76e4f662005-06-25 14:55:00 -0700630 int cpu;
631
632 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* Only the BSP gets external NMIs from the system. */
Ashok Raj76e4f662005-06-25 14:55:00 -0700635 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 reason = get_nmi_reason();
637
638 if (!(reason & 0xc0)) {
Jan Beulich6e3f3612006-01-11 22:42:14 +0100639 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 == NOTIFY_STOP)
641 return;
642#ifdef CONFIG_X86_LOCAL_APIC
643 /*
644 * Ok, so this is none of the documented NMI sources,
645 * so it must be the NMI watchdog.
646 */
647 if (nmi_watchdog > 0) {
648 nmi_watchdog_tick(regs,reason);
649 return;
650 }
651#endif
652 unknown_nmi_error(reason, regs);
653 return;
654 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100655 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return;
657
658 /* AK: following checks seem to be broken on modern chipsets. FIXME */
659
660 if (reason & 0x80)
661 mem_parity_error(reason, regs);
662 if (reason & 0x40)
663 io_check_error(reason, regs);
664}
665
Jan Beulichb556b352006-01-11 22:43:00 +0100666/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700667asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
670 return;
671 }
672 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
673 return;
674}
675
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700676/* Help handler running on IST stack to switch back to user stack
677 for scheduling or signal handling. The actual stack switch is done in
678 entry.S */
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100679asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700681 struct pt_regs *regs = eregs;
682 /* Did already sync */
683 if (eregs == (struct pt_regs *)eregs->rsp)
684 ;
685 /* Exception from user space */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700686 else if (user_mode(eregs))
Al Virobb049232006-01-12 01:05:38 -0800687 regs = task_pt_regs(current);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700688 /* Exception from kernel and interrupts are enabled. Move to
689 kernel process stack. */
690 else if (eregs->eflags & X86_EFLAGS_IF)
691 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
692 if (eregs != regs)
693 *regs = *eregs;
694 return regs;
695}
696
697/* runs on IST stack. */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700698asmlinkage void __kprobes do_debug(struct pt_regs * regs,
699 unsigned long error_code)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700700{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned long condition;
702 struct task_struct *tsk = current;
703 siginfo_t info;
704
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700705 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Andi Kleendaeeafe2005-04-16 15:25:13 -0700708 SIGTRAP) == NOTIFY_STOP)
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700709 return;
Andi Kleendaeeafe2005-04-16 15:25:13 -0700710
John Blackwooda65d17c2006-02-12 14:34:58 -0800711 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* Mask out spurious debug traps due to lazy DR7 setting */
714 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
715 if (!tsk->thread.debugreg7) {
716 goto clear_dr7;
717 }
718 }
719
720 tsk->thread.debugreg6 = condition;
721
722 /* Mask out spurious TF errors due to lazy TF clearing */
Andi Kleendaeeafe2005-04-16 15:25:13 -0700723 if (condition & DR_STEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * The TF error should be masked out only if the current
726 * process is not traced and if the TRAP flag has been set
727 * previously by a tracing process (condition detected by
728 * the PT_DTRACE flag); remember that the i386 TRAP flag
729 * can be modified by the process itself in user mode,
730 * allowing programs to debug themselves without the ptrace()
731 * interface.
732 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700733 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto clear_TF_reenable;
Andi Kleenbe61bff2005-04-16 15:24:57 -0700735 /*
736 * Was the TF flag set by a debugger? If so, clear it now,
737 * so that register information is correct.
738 */
739 if (tsk->ptrace & PT_DTRACE) {
740 regs->eflags &= ~TF_MASK;
741 tsk->ptrace &= ~PT_DTRACE;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 /* Ok, finally something we can handle */
746 tsk->thread.trap_no = 1;
747 tsk->thread.error_code = error_code;
748 info.si_signo = SIGTRAP;
749 info.si_errno = 0;
750 info.si_code = TRAP_BRKPT;
John Blackwood01b8faa2006-01-11 22:44:15 +0100751 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
752 force_sig_info(SIGTRAP, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754clear_dr7:
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700755 set_debugreg(0UL, 7);
John Blackwooda65d17c2006-02-12 14:34:58 -0800756 preempt_conditional_cli(regs);
Andi Kleen6fefb0d2005-04-16 15:25:03 -0700757 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759clear_TF_reenable:
760 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 regs->eflags &= ~TF_MASK;
John Blackwooda65d17c2006-02-12 14:34:58 -0800762 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Jan Beulich6e3f3612006-01-11 22:42:14 +0100765static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 const struct exception_table_entry *fixup;
768 fixup = search_exception_tables(regs->rip);
769 if (fixup) {
770 regs->rip = fixup->fixup;
771 return 1;
772 }
Jan Beulich6e3f3612006-01-11 22:42:14 +0100773 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
Andi Kleen3a848f62005-04-16 15:25:06 -0700774 /* Illegal floating point operation in the kernel */
Jan Beulich6e3f3612006-01-11 22:42:14 +0100775 current->thread.trap_no = trapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 die(str, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778}
779
780/*
781 * Note that we play around with the 'TS' bit in an attempt to get
782 * the correct behaviour even in the presence of the asynchronous
783 * IRQ13 behaviour
784 */
785asmlinkage void do_coprocessor_error(struct pt_regs *regs)
786{
787 void __user *rip = (void __user *)(regs->rip);
788 struct task_struct * task;
789 siginfo_t info;
790 unsigned short cwd, swd;
791
792 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700793 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100794 kernel_math_error(regs, "kernel x87 math error", 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return;
796
797 /*
798 * Save the info for the exception handler and clear the error.
799 */
800 task = current;
801 save_init_fpu(task);
802 task->thread.trap_no = 16;
803 task->thread.error_code = 0;
804 info.si_signo = SIGFPE;
805 info.si_errno = 0;
806 info.si_code = __SI_FAULT;
807 info.si_addr = rip;
808 /*
809 * (~cwd & swd) will mask out exceptions that are not set to unmasked
810 * status. 0x3f is the exception bits in these regs, 0x200 is the
811 * C1 reg you need in case of a stack fault, 0x040 is the stack
812 * fault bit. We should only be taking one exception at a time,
813 * so if this combination doesn't produce any single exception,
814 * then we have a bad program that isn't synchronizing its FPU usage
815 * and it will suffer the consequences since we won't be able to
816 * fully reproduce the context of the exception
817 */
818 cwd = get_fpu_cwd(task);
819 swd = get_fpu_swd(task);
Chuck Ebbertff347b22005-09-12 18:49:25 +0200820 switch (swd & ~cwd & 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 case 0x000:
822 default:
823 break;
824 case 0x001: /* Invalid Op */
Chuck Ebbertff347b22005-09-12 18:49:25 +0200825 /*
826 * swd & 0x240 == 0x040: Stack Underflow
827 * swd & 0x240 == 0x240: Stack Overflow
828 * User must clear the SF bit (0x40) if set
829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 info.si_code = FPE_FLTINV;
831 break;
832 case 0x002: /* Denormalize */
833 case 0x010: /* Underflow */
834 info.si_code = FPE_FLTUND;
835 break;
836 case 0x004: /* Zero Divide */
837 info.si_code = FPE_FLTDIV;
838 break;
839 case 0x008: /* Overflow */
840 info.si_code = FPE_FLTOVF;
841 break;
842 case 0x020: /* Precision */
843 info.si_code = FPE_FLTRES;
844 break;
845 }
846 force_sig_info(SIGFPE, &info, task);
847}
848
849asmlinkage void bad_intr(void)
850{
851 printk("bad interrupt");
852}
853
854asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
855{
856 void __user *rip = (void __user *)(regs->rip);
857 struct task_struct * task;
858 siginfo_t info;
859 unsigned short mxcsr;
860
861 conditional_sti(regs);
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700862 if (!user_mode(regs) &&
Jan Beulich6e3f3612006-01-11 22:42:14 +0100863 kernel_math_error(regs, "kernel simd math error", 19))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return;
865
866 /*
867 * Save the info for the exception handler and clear the error.
868 */
869 task = current;
870 save_init_fpu(task);
871 task->thread.trap_no = 19;
872 task->thread.error_code = 0;
873 info.si_signo = SIGFPE;
874 info.si_errno = 0;
875 info.si_code = __SI_FAULT;
876 info.si_addr = rip;
877 /*
878 * The SIMD FPU exceptions are handled a little differently, as there
879 * is only a single status/control register. Thus, to determine which
880 * unmasked exception was caught we must mask the exception mask bits
881 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
882 */
883 mxcsr = get_fpu_mxcsr(task);
884 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
885 case 0x000:
886 default:
887 break;
888 case 0x001: /* Invalid Op */
889 info.si_code = FPE_FLTINV;
890 break;
891 case 0x002: /* Denormalize */
892 case 0x010: /* Underflow */
893 info.si_code = FPE_FLTUND;
894 break;
895 case 0x004: /* Zero Divide */
896 info.si_code = FPE_FLTDIV;
897 break;
898 case 0x008: /* Overflow */
899 info.si_code = FPE_FLTOVF;
900 break;
901 case 0x020: /* Precision */
902 info.si_code = FPE_FLTRES;
903 break;
904 }
905 force_sig_info(SIGFPE, &info, task);
906}
907
908asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
909{
910}
911
912asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
913{
914}
915
Jacob Shin89b831e2005-11-05 17:25:53 +0100916asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
917{
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * 'math_state_restore()' saves the current math information in the
922 * old math state array, and gets the new ones from the current task
923 *
924 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
925 * Don't touch unless you *really* know how it works.
926 */
927asmlinkage void math_state_restore(void)
928{
929 struct task_struct *me = current;
930 clts(); /* Allow maths ops (or we recurse) */
931
932 if (!used_math())
933 init_fpu(me);
934 restore_fpu_checking(&me->thread.i387.fxsave);
Al Viroe4f17c42006-01-12 01:05:38 -0800935 task_thread_info(me)->status |= TS_USEDFPU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938void __init trap_init(void)
939{
940 set_intr_gate(0,&divide_error);
941 set_intr_gate_ist(1,&debug,DEBUG_STACK);
942 set_intr_gate_ist(2,&nmi,NMI_STACK);
Jan Beulichb556b352006-01-11 22:43:00 +0100943 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
Jan Beulich0a521582006-01-11 22:42:08 +0100944 set_system_gate(4,&overflow); /* int4 can be called from all */
945 set_intr_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 set_intr_gate(6,&invalid_op);
947 set_intr_gate(7,&device_not_available);
948 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
949 set_intr_gate(9,&coprocessor_segment_overrun);
950 set_intr_gate(10,&invalid_TSS);
951 set_intr_gate(11,&segment_not_present);
952 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
953 set_intr_gate(13,&general_protection);
954 set_intr_gate(14,&page_fault);
955 set_intr_gate(15,&spurious_interrupt_bug);
956 set_intr_gate(16,&coprocessor_error);
957 set_intr_gate(17,&alignment_check);
958#ifdef CONFIG_X86_MCE
959 set_intr_gate_ist(18,&machine_check, MCE_STACK);
960#endif
961 set_intr_gate(19,&simd_coprocessor_error);
962
963#ifdef CONFIG_IA32_EMULATION
964 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
965#endif
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * Should be a barrier for any external CPU state.
969 */
970 cpu_init();
971}
972
973
974/* Actual parsing is done early in setup.c. */
975static int __init oops_dummy(char *s)
976{
977 panic_on_oops = 1;
978 return -1;
979}
980__setup("oops=", oops_dummy);
981
982static int __init kstack_setup(char *s)
983{
984 kstack_depth_to_print = simple_strtoul(s,NULL,0);
985 return 0;
986}
987__setup("kstack=", kstack_setup);
988