Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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.org | 35faa71 | 2005-04-16 15:24:54 -0700 | [diff] [blame] | 31 | #include <linux/nmi.h> |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 32 | #include <linux/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | extern struct gate_struct idt_table[256]; |
| 51 | |
| 52 | asmlinkage void divide_error(void); |
| 53 | asmlinkage void debug(void); |
| 54 | asmlinkage void nmi(void); |
| 55 | asmlinkage void int3(void); |
| 56 | asmlinkage void overflow(void); |
| 57 | asmlinkage void bounds(void); |
| 58 | asmlinkage void invalid_op(void); |
| 59 | asmlinkage void device_not_available(void); |
| 60 | asmlinkage void double_fault(void); |
| 61 | asmlinkage void coprocessor_segment_overrun(void); |
| 62 | asmlinkage void invalid_TSS(void); |
| 63 | asmlinkage void segment_not_present(void); |
| 64 | asmlinkage void stack_segment(void); |
| 65 | asmlinkage void general_protection(void); |
| 66 | asmlinkage void page_fault(void); |
| 67 | asmlinkage void coprocessor_error(void); |
| 68 | asmlinkage void simd_coprocessor_error(void); |
| 69 | asmlinkage void reserved(void); |
| 70 | asmlinkage void alignment_check(void); |
| 71 | asmlinkage void machine_check(void); |
| 72 | asmlinkage void spurious_interrupt_bug(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | struct notifier_block *die_chain; |
| 75 | static DEFINE_SPINLOCK(die_notifier_lock); |
| 76 | |
| 77 | int register_die_notifier(struct notifier_block *nb) |
| 78 | { |
| 79 | int err = 0; |
| 80 | unsigned long flags; |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame^] | 81 | |
| 82 | vmalloc_sync_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | 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 | |
| 89 | static inline void conditional_sti(struct pt_regs *regs) |
| 90 | { |
| 91 | if (regs->eflags & X86_EFLAGS_IF) |
| 92 | local_irq_enable(); |
| 93 | } |
| 94 | |
John Blackwood | a65d17c | 2006-02-12 14:34:58 -0800 | [diff] [blame] | 95 | static 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 | |
| 102 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | static int kstack_depth_to_print = 10; |
| 110 | |
| 111 | #ifdef CONFIG_KALLSYMS |
| 112 | #include <linux/kallsyms.h> |
| 113 | int 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}", |
| 127 | address,delim,modname,delim,symname,offset); |
| 128 | } |
| 129 | #else |
| 130 | int printk_address(unsigned long address) |
| 131 | { |
| 132 | return printk("[<%016lx>]", address); |
| 133 | } |
| 134 | #endif |
| 135 | |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 136 | static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack, |
| 137 | unsigned *usedp, const char **idp) |
| 138 | { |
Jan Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 139 | static char ids[][8] = { |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 140 | [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 Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 145 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
| 146 | [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]" |
| 147 | #endif |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 148 | }; |
| 149 | unsigned k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 151 | for (k = 0; k < N_EXCEPTION_STACKS; k++) { |
| 152 | unsigned long end; |
| 153 | |
Jan Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 154 | switch (k + 1) { |
| 155 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
| 156 | case DEBUG_STACK: |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 157 | end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ; |
Jan Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 158 | break; |
| 159 | #endif |
| 160 | default: |
| 161 | end = per_cpu(init_tss, cpu).ist[k]; |
| 162 | break; |
| 163 | } |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 164 | 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 Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 173 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | return NULL; |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * x86-64 can have upto three kernel stacks: |
| 195 | * process stack |
| 196 | * interrupt stack |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 197 | * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | */ |
| 199 | |
| 200 | void show_trace(unsigned long *stack) |
| 201 | { |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 202 | const unsigned cpu = safe_smp_processor_id(); |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 203 | unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | int i; |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 205 | unsigned used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | printk("\nCall Trace:"); |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 208 | |
| 209 | #define HANDLE_STACK(cond) \ |
| 210 | do while (cond) { \ |
Jan Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 211 | unsigned long addr = *stack++; \ |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 212 | if (kernel_text_address(addr)) { \ |
Jan Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 213 | if (i > 50) { \ |
| 214 | printk("\n "); \ |
| 215 | i = 0; \ |
| 216 | } \ |
| 217 | else \ |
| 218 | i += printk(" "); \ |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 219 | /* \ |
| 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 Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 228 | } \ |
| 229 | } while (0) |
| 230 | |
Jan Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 231 | for(i = 11; ; ) { |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 232 | 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 Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 238 | i += printk(" <%s>", id); |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 239 | HANDLE_STACK (stack < estack_end); |
Jan Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 240 | i += printk(" <EOE>"); |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 241 | 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 Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 250 | i += printk(" <IRQ>"); |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 251 | HANDLE_STACK (stack < irqstack_end); |
| 252 | stack = (unsigned long *) (irqstack_end[-1]); |
| 253 | irqstack_end = NULL; |
Jan Beulich | 1b2f630 | 2006-01-11 22:46:45 +0100 | [diff] [blame] | 254 | i += printk(" <EOI>"); |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 255 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 258 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
Andi Kleen | 0a65800 | 2005-04-16 15:25:17 -0700 | [diff] [blame] | 260 | |
| 261 | HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0); |
| 262 | #undef HANDLE_STACK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | printk("\n"); |
| 264 | } |
| 265 | |
| 266 | void 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 Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 271 | unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr); |
| 272 | unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 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.org | 35faa71 | 2005-04-16 15:24:54 -0700 | [diff] [blame] | 298 | touch_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | show_trace((unsigned long *)rsp); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * The architecture-independent dump_stack generator |
| 305 | */ |
| 306 | void dump_stack(void) |
| 307 | { |
| 308 | unsigned long dummy; |
| 309 | show_trace(&dummy); |
| 310 | } |
| 311 | |
| 312 | EXPORT_SYMBOL(dump_stack); |
| 313 | |
| 314 | void show_registers(struct pt_regs *regs) |
| 315 | { |
| 316 | int i; |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 317 | int in_kernel = !user_mode(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | unsigned long rsp; |
| 319 | const int cpu = safe_smp_processor_id(); |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 320 | struct task_struct *cur = cpu_pda(cpu)->pcurrent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 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 Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 327 | cur->comm, cur->pid, task_thread_info(cur), cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 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: "); |
| 339 | if(regs->rip < PAGE_OFFSET) |
| 340 | goto bad; |
| 341 | |
| 342 | for(i=0;i<20;i++) |
| 343 | { |
| 344 | unsigned char c; |
| 345 | if(__get_user(c, &((unsigned char*)regs->rip)[i])) { |
| 346 | bad: |
| 347 | printk(" Bad RIP value."); |
| 348 | break; |
| 349 | } |
| 350 | printk("%02x ", c); |
| 351 | } |
| 352 | } |
| 353 | printk("\n"); |
| 354 | } |
| 355 | |
| 356 | void handle_BUG(struct pt_regs *regs) |
| 357 | { |
| 358 | struct bug_frame f; |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 359 | long len; |
| 360 | const char *prefix = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 362 | if (user_mode(regs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return; |
Stephen Hemminger | 77a7533 | 2006-01-11 22:46:30 +0100 | [diff] [blame] | 364 | if (__copy_from_user(&f, (const void __user *) regs->rip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | sizeof(struct bug_frame))) |
| 366 | return; |
Jan Beulich | 049cdef | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 367 | if (f.filename >= 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | f.ud2[0] != 0x0f || f.ud2[1] != 0x0b) |
| 369 | return; |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 370 | len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1; |
| 371 | if (len < 0 || len >= PATH_MAX) |
Jan Beulich | 049cdef | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 372 | f.filename = (int)(long)"unmapped filename"; |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 373 | else if (len > 50) { |
| 374 | f.filename += len - 50; |
| 375 | prefix = "..."; |
| 376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | printk("----------- [cut here ] --------- [please bite here ] ---------\n"); |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 378 | printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Alexander Nyberg | 4f60fdf | 2005-05-25 12:31:28 -0700 | [diff] [blame] | 381 | #ifdef CONFIG_BUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | void out_of_line_bug(void) |
| 383 | { |
| 384 | BUG(); |
| 385 | } |
Alexander Nyberg | 4f60fdf | 2005-05-25 12:31:28 -0700 | [diff] [blame] | 386 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | static DEFINE_SPINLOCK(die_lock); |
| 389 | static int die_owner = -1; |
| 390 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 391 | unsigned __kprobes long oops_begin(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 393 | int cpu = safe_smp_processor_id(); |
| 394 | unsigned long flags; |
| 395 | |
| 396 | /* racy, but better than risking deadlock. */ |
| 397 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!spin_trylock(&die_lock)) { |
| 399 | if (cpu == die_owner) |
| 400 | /* nested oops. should stop eventually */; |
| 401 | else |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 402 | spin_lock(&die_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 404 | die_owner = cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | console_verbose(); |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 406 | bust_spinlocks(1); |
| 407 | return flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 410 | void __kprobes oops_end(unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | die_owner = -1; |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 413 | bust_spinlocks(0); |
| 414 | spin_unlock_irqrestore(&die_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (panic_on_oops) |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 416 | panic("Oops"); |
| 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 419 | void __kprobes __die(const char * str, struct pt_regs * regs, long err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | static int die_counter; |
| 422 | printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter); |
| 423 | #ifdef CONFIG_PREEMPT |
| 424 | printk("PREEMPT "); |
| 425 | #endif |
| 426 | #ifdef CONFIG_SMP |
| 427 | printk("SMP "); |
| 428 | #endif |
| 429 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 430 | printk("DEBUG_PAGEALLOC"); |
| 431 | #endif |
| 432 | printk("\n"); |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 433 | notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | show_registers(regs); |
| 435 | /* Executive summary in case the oops scrolled away */ |
| 436 | printk(KERN_ALERT "RIP "); |
| 437 | printk_address(regs->rip); |
| 438 | printk(" RSP <%016lx>\n", regs->rsp); |
| 439 | } |
| 440 | |
| 441 | void die(const char * str, struct pt_regs * regs, long err) |
| 442 | { |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 443 | unsigned long flags = oops_begin(); |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | handle_BUG(regs); |
| 446 | __die(str, regs, err); |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 447 | oops_end(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | do_exit(SIGSEGV); |
| 449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 451 | void __kprobes die_nmi(char *str, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 453 | unsigned long flags = oops_begin(); |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * We are in trouble anyway, lets at least try |
| 457 | * to get a message out. |
| 458 | */ |
| 459 | printk(str, safe_smp_processor_id()); |
| 460 | show_registers(regs); |
| 461 | if (panic_on_timeout || panic_on_oops) |
| 462 | panic("nmi watchdog"); |
| 463 | printk("console shuts up ...\n"); |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 464 | oops_end(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | do_exit(SIGSEGV); |
| 466 | } |
| 467 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 468 | static void __kprobes do_trap(int trapnr, int signr, char *str, |
| 469 | struct pt_regs * regs, long error_code, |
| 470 | siginfo_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 472 | struct task_struct *tsk = current; |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | conditional_sti(regs); |
| 475 | |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 476 | tsk->thread.error_code = error_code; |
| 477 | tsk->thread.trap_no = trapnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 479 | if (user_mode(regs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (exception_trace && unhandled_signal(tsk, signr)) |
| 481 | printk(KERN_INFO |
| 482 | "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n", |
| 483 | tsk->comm, tsk->pid, str, |
| 484 | regs->rip,regs->rsp,error_code); |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if (info) |
| 487 | force_sig_info(signr, info, tsk); |
| 488 | else |
| 489 | force_sig(signr, tsk); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /* kernel trap */ |
| 495 | { |
| 496 | const struct exception_table_entry *fixup; |
| 497 | fixup = search_exception_tables(regs->rip); |
| 498 | if (fixup) { |
| 499 | regs->rip = fixup->fixup; |
| 500 | } else |
| 501 | die(str, regs, error_code); |
| 502 | return; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | #define DO_ERROR(trapnr, signr, str, name) \ |
| 507 | asmlinkage void do_##name(struct pt_regs * regs, long error_code) \ |
| 508 | { \ |
| 509 | if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \ |
| 510 | == NOTIFY_STOP) \ |
| 511 | return; \ |
| 512 | do_trap(trapnr, signr, str, regs, error_code, NULL); \ |
| 513 | } |
| 514 | |
| 515 | #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \ |
| 516 | asmlinkage void do_##name(struct pt_regs * regs, long error_code) \ |
| 517 | { \ |
| 518 | siginfo_t info; \ |
| 519 | info.si_signo = signr; \ |
| 520 | info.si_errno = 0; \ |
| 521 | info.si_code = sicode; \ |
| 522 | info.si_addr = (void __user *)siaddr; \ |
| 523 | if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \ |
| 524 | == NOTIFY_STOP) \ |
| 525 | return; \ |
| 526 | do_trap(trapnr, signr, str, regs, error_code, &info); \ |
| 527 | } |
| 528 | |
| 529 | DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip) |
| 530 | DO_ERROR( 4, SIGSEGV, "overflow", overflow) |
| 531 | DO_ERROR( 5, SIGSEGV, "bounds", bounds) |
Chuck Ebbert | 100c0e3 | 2006-01-11 22:46:00 +0100 | [diff] [blame] | 532 | DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | DO_ERROR( 7, SIGSEGV, "device not available", device_not_available) |
| 534 | DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun) |
| 535 | DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS) |
| 536 | DO_ERROR(11, SIGBUS, "segment not present", segment_not_present) |
| 537 | DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0) |
| 538 | DO_ERROR(18, SIGSEGV, "reserved", reserved) |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 539 | DO_ERROR(12, SIGBUS, "stack segment", stack_segment) |
Jan Beulich | eca37c1 | 2006-01-11 22:42:17 +0100 | [diff] [blame] | 540 | |
| 541 | asmlinkage void do_double_fault(struct pt_regs * regs, long error_code) |
| 542 | { |
| 543 | static const char str[] = "double fault"; |
| 544 | struct task_struct *tsk = current; |
| 545 | |
| 546 | /* Return not checked because double check cannot be ignored */ |
| 547 | notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV); |
| 548 | |
| 549 | tsk->thread.error_code = error_code; |
| 550 | tsk->thread.trap_no = 8; |
| 551 | |
| 552 | /* This is always a kernel trap and never fixable (and thus must |
| 553 | never return). */ |
| 554 | for (;;) |
| 555 | die(str, regs, error_code); |
| 556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 558 | asmlinkage void __kprobes do_general_protection(struct pt_regs * regs, |
| 559 | long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 561 | struct task_struct *tsk = current; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | conditional_sti(regs); |
| 564 | |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 565 | tsk->thread.error_code = error_code; |
| 566 | tsk->thread.trap_no = 13; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 568 | if (user_mode(regs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (exception_trace && unhandled_signal(tsk, SIGSEGV)) |
| 570 | printk(KERN_INFO |
| 571 | "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n", |
| 572 | tsk->comm, tsk->pid, |
| 573 | regs->rip,regs->rsp,error_code); |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | force_sig(SIGSEGV, tsk); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | /* kernel gp */ |
| 580 | { |
| 581 | const struct exception_table_entry *fixup; |
| 582 | fixup = search_exception_tables(regs->rip); |
| 583 | if (fixup) { |
| 584 | regs->rip = fixup->fixup; |
| 585 | return; |
| 586 | } |
| 587 | if (notify_die(DIE_GPF, "general protection fault", regs, |
| 588 | error_code, 13, SIGSEGV) == NOTIFY_STOP) |
| 589 | return; |
| 590 | die("general protection fault", regs, error_code); |
| 591 | } |
| 592 | } |
| 593 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 594 | static __kprobes void |
| 595 | mem_parity_error(unsigned char reason, struct pt_regs * regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
| 597 | printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n"); |
| 598 | printk("You probably have a hardware problem with your RAM chips\n"); |
| 599 | |
| 600 | /* Clear and disable the memory parity error line. */ |
| 601 | reason = (reason & 0xf) | 4; |
| 602 | outb(reason, 0x61); |
| 603 | } |
| 604 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 605 | static __kprobes void |
| 606 | io_check_error(unsigned char reason, struct pt_regs * regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | printk("NMI: IOCK error (debug interrupt?)\n"); |
| 609 | show_registers(regs); |
| 610 | |
| 611 | /* Re-enable the IOCK line, wait for a few seconds */ |
| 612 | reason = (reason & 0xf) | 8; |
| 613 | outb(reason, 0x61); |
| 614 | mdelay(2000); |
| 615 | reason &= ~8; |
| 616 | outb(reason, 0x61); |
| 617 | } |
| 618 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 619 | static __kprobes void |
| 620 | unknown_nmi_error(unsigned char reason, struct pt_regs * regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { printk("Uhhuh. NMI received for unknown reason %02x.\n", reason); |
| 622 | printk("Dazed and confused, but trying to continue\n"); |
| 623 | printk("Do you have a strange power saving mode enabled?\n"); |
| 624 | } |
| 625 | |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 626 | /* Runs on IST stack. This code must keep interrupts off all the time. |
| 627 | Nested NMIs are prevented by the CPU. */ |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 628 | asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
| 630 | unsigned char reason = 0; |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 631 | int cpu; |
| 632 | |
| 633 | cpu = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | /* Only the BSP gets external NMIs from the system. */ |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 636 | if (!cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | reason = get_nmi_reason(); |
| 638 | |
| 639 | if (!(reason & 0xc0)) { |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 640 | if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | == NOTIFY_STOP) |
| 642 | return; |
| 643 | #ifdef CONFIG_X86_LOCAL_APIC |
| 644 | /* |
| 645 | * Ok, so this is none of the documented NMI sources, |
| 646 | * so it must be the NMI watchdog. |
| 647 | */ |
| 648 | if (nmi_watchdog > 0) { |
| 649 | nmi_watchdog_tick(regs,reason); |
| 650 | return; |
| 651 | } |
| 652 | #endif |
| 653 | unknown_nmi_error(reason, regs); |
| 654 | return; |
| 655 | } |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 656 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return; |
| 658 | |
| 659 | /* AK: following checks seem to be broken on modern chipsets. FIXME */ |
| 660 | |
| 661 | if (reason & 0x80) |
| 662 | mem_parity_error(reason, regs); |
| 663 | if (reason & 0x40) |
| 664 | io_check_error(reason, regs); |
| 665 | } |
| 666 | |
Jan Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 667 | /* runs on IST stack. */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 668 | asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
| 670 | if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) { |
| 671 | return; |
| 672 | } |
| 673 | do_trap(3, SIGTRAP, "int3", regs, error_code, NULL); |
| 674 | return; |
| 675 | } |
| 676 | |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 677 | /* Help handler running on IST stack to switch back to user stack |
| 678 | for scheduling or signal handling. The actual stack switch is done in |
| 679 | entry.S */ |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 680 | asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 682 | struct pt_regs *regs = eregs; |
| 683 | /* Did already sync */ |
| 684 | if (eregs == (struct pt_regs *)eregs->rsp) |
| 685 | ; |
| 686 | /* Exception from user space */ |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 687 | else if (user_mode(eregs)) |
Al Viro | bb04923 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 688 | regs = task_pt_regs(current); |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 689 | /* Exception from kernel and interrupts are enabled. Move to |
| 690 | kernel process stack. */ |
| 691 | else if (eregs->eflags & X86_EFLAGS_IF) |
| 692 | regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs)); |
| 693 | if (eregs != regs) |
| 694 | *regs = *eregs; |
| 695 | return regs; |
| 696 | } |
| 697 | |
| 698 | /* runs on IST stack. */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 699 | asmlinkage void __kprobes do_debug(struct pt_regs * regs, |
| 700 | unsigned long error_code) |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 701 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | unsigned long condition; |
| 703 | struct task_struct *tsk = current; |
| 704 | siginfo_t info; |
| 705 | |
Vincent Hanquez | e9129e5 | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 706 | get_debugreg(condition, 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
| 708 | if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code, |
Andi Kleen | daeeafe | 2005-04-16 15:25:13 -0700 | [diff] [blame] | 709 | SIGTRAP) == NOTIFY_STOP) |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 710 | return; |
Andi Kleen | daeeafe | 2005-04-16 15:25:13 -0700 | [diff] [blame] | 711 | |
John Blackwood | a65d17c | 2006-02-12 14:34:58 -0800 | [diff] [blame] | 712 | preempt_conditional_sti(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | /* Mask out spurious debug traps due to lazy DR7 setting */ |
| 715 | if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) { |
| 716 | if (!tsk->thread.debugreg7) { |
| 717 | goto clear_dr7; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | tsk->thread.debugreg6 = condition; |
| 722 | |
| 723 | /* Mask out spurious TF errors due to lazy TF clearing */ |
Andi Kleen | daeeafe | 2005-04-16 15:25:13 -0700 | [diff] [blame] | 724 | if (condition & DR_STEP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * The TF error should be masked out only if the current |
| 727 | * process is not traced and if the TRAP flag has been set |
| 728 | * previously by a tracing process (condition detected by |
| 729 | * the PT_DTRACE flag); remember that the i386 TRAP flag |
| 730 | * can be modified by the process itself in user mode, |
| 731 | * allowing programs to debug themselves without the ptrace() |
| 732 | * interface. |
| 733 | */ |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 734 | if (!user_mode(regs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | goto clear_TF_reenable; |
Andi Kleen | be61bff | 2005-04-16 15:24:57 -0700 | [diff] [blame] | 736 | /* |
| 737 | * Was the TF flag set by a debugger? If so, clear it now, |
| 738 | * so that register information is correct. |
| 739 | */ |
| 740 | if (tsk->ptrace & PT_DTRACE) { |
| 741 | regs->eflags &= ~TF_MASK; |
| 742 | tsk->ptrace &= ~PT_DTRACE; |
| 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Ok, finally something we can handle */ |
| 747 | tsk->thread.trap_no = 1; |
| 748 | tsk->thread.error_code = error_code; |
| 749 | info.si_signo = SIGTRAP; |
| 750 | info.si_errno = 0; |
| 751 | info.si_code = TRAP_BRKPT; |
John Blackwood | 01b8faa | 2006-01-11 22:44:15 +0100 | [diff] [blame] | 752 | info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL; |
| 753 | force_sig_info(SIGTRAP, &info, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | clear_dr7: |
Vincent Hanquez | e9129e5 | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 756 | set_debugreg(0UL, 7); |
John Blackwood | a65d17c | 2006-02-12 14:34:58 -0800 | [diff] [blame] | 757 | preempt_conditional_cli(regs); |
Andi Kleen | 6fefb0d | 2005-04-16 15:25:03 -0700 | [diff] [blame] | 758 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | clear_TF_reenable: |
| 761 | set_tsk_thread_flag(tsk, TIF_SINGLESTEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | regs->eflags &= ~TF_MASK; |
John Blackwood | a65d17c | 2006-02-12 14:34:58 -0800 | [diff] [blame] | 763 | preempt_conditional_cli(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 766 | static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
| 768 | const struct exception_table_entry *fixup; |
| 769 | fixup = search_exception_tables(regs->rip); |
| 770 | if (fixup) { |
| 771 | regs->rip = fixup->fixup; |
| 772 | return 1; |
| 773 | } |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 774 | notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE); |
Andi Kleen | 3a848f6 | 2005-04-16 15:25:06 -0700 | [diff] [blame] | 775 | /* Illegal floating point operation in the kernel */ |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 776 | current->thread.trap_no = trapnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | die(str, regs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Note that we play around with the 'TS' bit in an attempt to get |
| 783 | * the correct behaviour even in the presence of the asynchronous |
| 784 | * IRQ13 behaviour |
| 785 | */ |
| 786 | asmlinkage void do_coprocessor_error(struct pt_regs *regs) |
| 787 | { |
| 788 | void __user *rip = (void __user *)(regs->rip); |
| 789 | struct task_struct * task; |
| 790 | siginfo_t info; |
| 791 | unsigned short cwd, swd; |
| 792 | |
| 793 | conditional_sti(regs); |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 794 | if (!user_mode(regs) && |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 795 | kernel_math_error(regs, "kernel x87 math error", 16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | return; |
| 797 | |
| 798 | /* |
| 799 | * Save the info for the exception handler and clear the error. |
| 800 | */ |
| 801 | task = current; |
| 802 | save_init_fpu(task); |
| 803 | task->thread.trap_no = 16; |
| 804 | task->thread.error_code = 0; |
| 805 | info.si_signo = SIGFPE; |
| 806 | info.si_errno = 0; |
| 807 | info.si_code = __SI_FAULT; |
| 808 | info.si_addr = rip; |
| 809 | /* |
| 810 | * (~cwd & swd) will mask out exceptions that are not set to unmasked |
| 811 | * status. 0x3f is the exception bits in these regs, 0x200 is the |
| 812 | * C1 reg you need in case of a stack fault, 0x040 is the stack |
| 813 | * fault bit. We should only be taking one exception at a time, |
| 814 | * so if this combination doesn't produce any single exception, |
| 815 | * then we have a bad program that isn't synchronizing its FPU usage |
| 816 | * and it will suffer the consequences since we won't be able to |
| 817 | * fully reproduce the context of the exception |
| 818 | */ |
| 819 | cwd = get_fpu_cwd(task); |
| 820 | swd = get_fpu_swd(task); |
Chuck Ebbert | ff347b2 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 821 | switch (swd & ~cwd & 0x3f) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | case 0x000: |
| 823 | default: |
| 824 | break; |
| 825 | case 0x001: /* Invalid Op */ |
Chuck Ebbert | ff347b2 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 826 | /* |
| 827 | * swd & 0x240 == 0x040: Stack Underflow |
| 828 | * swd & 0x240 == 0x240: Stack Overflow |
| 829 | * User must clear the SF bit (0x40) if set |
| 830 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | info.si_code = FPE_FLTINV; |
| 832 | break; |
| 833 | case 0x002: /* Denormalize */ |
| 834 | case 0x010: /* Underflow */ |
| 835 | info.si_code = FPE_FLTUND; |
| 836 | break; |
| 837 | case 0x004: /* Zero Divide */ |
| 838 | info.si_code = FPE_FLTDIV; |
| 839 | break; |
| 840 | case 0x008: /* Overflow */ |
| 841 | info.si_code = FPE_FLTOVF; |
| 842 | break; |
| 843 | case 0x020: /* Precision */ |
| 844 | info.si_code = FPE_FLTRES; |
| 845 | break; |
| 846 | } |
| 847 | force_sig_info(SIGFPE, &info, task); |
| 848 | } |
| 849 | |
| 850 | asmlinkage void bad_intr(void) |
| 851 | { |
| 852 | printk("bad interrupt"); |
| 853 | } |
| 854 | |
| 855 | asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs) |
| 856 | { |
| 857 | void __user *rip = (void __user *)(regs->rip); |
| 858 | struct task_struct * task; |
| 859 | siginfo_t info; |
| 860 | unsigned short mxcsr; |
| 861 | |
| 862 | conditional_sti(regs); |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 863 | if (!user_mode(regs) && |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 864 | kernel_math_error(regs, "kernel simd math error", 19)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | return; |
| 866 | |
| 867 | /* |
| 868 | * Save the info for the exception handler and clear the error. |
| 869 | */ |
| 870 | task = current; |
| 871 | save_init_fpu(task); |
| 872 | task->thread.trap_no = 19; |
| 873 | task->thread.error_code = 0; |
| 874 | info.si_signo = SIGFPE; |
| 875 | info.si_errno = 0; |
| 876 | info.si_code = __SI_FAULT; |
| 877 | info.si_addr = rip; |
| 878 | /* |
| 879 | * The SIMD FPU exceptions are handled a little differently, as there |
| 880 | * is only a single status/control register. Thus, to determine which |
| 881 | * unmasked exception was caught we must mask the exception mask bits |
| 882 | * at 0x1f80, and then use these to mask the exception bits at 0x3f. |
| 883 | */ |
| 884 | mxcsr = get_fpu_mxcsr(task); |
| 885 | switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) { |
| 886 | case 0x000: |
| 887 | default: |
| 888 | break; |
| 889 | case 0x001: /* Invalid Op */ |
| 890 | info.si_code = FPE_FLTINV; |
| 891 | break; |
| 892 | case 0x002: /* Denormalize */ |
| 893 | case 0x010: /* Underflow */ |
| 894 | info.si_code = FPE_FLTUND; |
| 895 | break; |
| 896 | case 0x004: /* Zero Divide */ |
| 897 | info.si_code = FPE_FLTDIV; |
| 898 | break; |
| 899 | case 0x008: /* Overflow */ |
| 900 | info.si_code = FPE_FLTOVF; |
| 901 | break; |
| 902 | case 0x020: /* Precision */ |
| 903 | info.si_code = FPE_FLTRES; |
| 904 | break; |
| 905 | } |
| 906 | force_sig_info(SIGFPE, &info, task); |
| 907 | } |
| 908 | |
| 909 | asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs) |
| 910 | { |
| 911 | } |
| 912 | |
| 913 | asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void) |
| 914 | { |
| 915 | } |
| 916 | |
Jacob Shin | 89b831e | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 917 | asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void) |
| 918 | { |
| 919 | } |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | /* |
| 922 | * 'math_state_restore()' saves the current math information in the |
| 923 | * old math state array, and gets the new ones from the current task |
| 924 | * |
| 925 | * Careful.. There are problems with IBM-designed IRQ13 behaviour. |
| 926 | * Don't touch unless you *really* know how it works. |
| 927 | */ |
| 928 | asmlinkage void math_state_restore(void) |
| 929 | { |
| 930 | struct task_struct *me = current; |
| 931 | clts(); /* Allow maths ops (or we recurse) */ |
| 932 | |
| 933 | if (!used_math()) |
| 934 | init_fpu(me); |
| 935 | restore_fpu_checking(&me->thread.i387.fxsave); |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 936 | task_thread_info(me)->status |= TS_USEDFPU; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | void __init trap_init(void) |
| 940 | { |
| 941 | set_intr_gate(0,÷_error); |
| 942 | set_intr_gate_ist(1,&debug,DEBUG_STACK); |
| 943 | set_intr_gate_ist(2,&nmi,NMI_STACK); |
Jan Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 944 | set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */ |
Jan Beulich | 0a52158 | 2006-01-11 22:42:08 +0100 | [diff] [blame] | 945 | set_system_gate(4,&overflow); /* int4 can be called from all */ |
| 946 | set_intr_gate(5,&bounds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | set_intr_gate(6,&invalid_op); |
| 948 | set_intr_gate(7,&device_not_available); |
| 949 | set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK); |
| 950 | set_intr_gate(9,&coprocessor_segment_overrun); |
| 951 | set_intr_gate(10,&invalid_TSS); |
| 952 | set_intr_gate(11,&segment_not_present); |
| 953 | set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK); |
| 954 | set_intr_gate(13,&general_protection); |
| 955 | set_intr_gate(14,&page_fault); |
| 956 | set_intr_gate(15,&spurious_interrupt_bug); |
| 957 | set_intr_gate(16,&coprocessor_error); |
| 958 | set_intr_gate(17,&alignment_check); |
| 959 | #ifdef CONFIG_X86_MCE |
| 960 | set_intr_gate_ist(18,&machine_check, MCE_STACK); |
| 961 | #endif |
| 962 | set_intr_gate(19,&simd_coprocessor_error); |
| 963 | |
| 964 | #ifdef CONFIG_IA32_EMULATION |
| 965 | set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall); |
| 966 | #endif |
| 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | /* |
| 969 | * Should be a barrier for any external CPU state. |
| 970 | */ |
| 971 | cpu_init(); |
| 972 | } |
| 973 | |
| 974 | |
| 975 | /* Actual parsing is done early in setup.c. */ |
| 976 | static int __init oops_dummy(char *s) |
| 977 | { |
| 978 | panic_on_oops = 1; |
| 979 | return -1; |
| 980 | } |
| 981 | __setup("oops=", oops_dummy); |
| 982 | |
| 983 | static int __init kstack_setup(char *s) |
| 984 | { |
| 985 | kstack_depth_to_print = simple_strtoul(s,NULL,0); |
| 986 | return 0; |
| 987 | } |
| 988 | __setup("kstack=", kstack_setup); |
| 989 | |