Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 3 | * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs |
| 4 | */ |
| 5 | #include <linux/kallsyms.h> |
| 6 | #include <linux/kprobes.h> |
| 7 | #include <linux/uaccess.h> |
| 8 | #include <linux/utsname.h> |
| 9 | #include <linux/hardirq.h> |
| 10 | #include <linux/kdebug.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/ptrace.h> |
| 13 | #include <linux/kexec.h> |
| 14 | #include <linux/bug.h> |
| 15 | #include <linux/nmi.h> |
| 16 | |
| 17 | #include <asm/stacktrace.h> |
| 18 | |
| 19 | int panic_on_unrecovered_nmi; |
| 20 | int kstack_depth_to_print = 24; |
| 21 | static unsigned int code_bytes = 64; |
| 22 | static int die_counter; |
| 23 | |
| 24 | void printk_address(unsigned long address, int reliable) |
| 25 | { |
Alexander van Heukelum | 1618279 | 2008-10-04 23:12:41 +0200 | [diff] [blame^] | 26 | printk(" [<%p>] %s%pS\n", (void *) address, |
| 27 | reliable ? "" : "? ", (void *) address); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static inline int valid_stack_ptr(struct thread_info *tinfo, |
| 31 | void *p, unsigned int size) |
| 32 | { |
| 33 | void *t = tinfo; |
| 34 | return p > t && p <= t + THREAD_SIZE - size; |
| 35 | } |
| 36 | |
| 37 | /* The form of the top of the frame on the stack */ |
| 38 | struct stack_frame { |
| 39 | struct stack_frame *next_frame; |
| 40 | unsigned long return_address; |
| 41 | }; |
| 42 | |
| 43 | static inline unsigned long |
| 44 | print_context_stack(struct thread_info *tinfo, |
| 45 | unsigned long *stack, unsigned long bp, |
| 46 | const struct stacktrace_ops *ops, void *data) |
| 47 | { |
| 48 | struct stack_frame *frame = (struct stack_frame *)bp; |
| 49 | |
| 50 | while (valid_stack_ptr(tinfo, stack, sizeof(*stack))) { |
| 51 | unsigned long addr; |
| 52 | |
| 53 | addr = *stack; |
| 54 | if (__kernel_text_address(addr)) { |
| 55 | if ((unsigned long) stack == bp + 4) { |
| 56 | ops->address(data, addr, 1); |
| 57 | frame = frame->next_frame; |
| 58 | bp = (unsigned long) frame; |
| 59 | } else { |
| 60 | ops->address(data, addr, bp == 0); |
| 61 | } |
| 62 | } |
| 63 | stack++; |
| 64 | } |
| 65 | return bp; |
| 66 | } |
| 67 | |
| 68 | void dump_trace(struct task_struct *task, struct pt_regs *regs, |
| 69 | unsigned long *stack, unsigned long bp, |
| 70 | const struct stacktrace_ops *ops, void *data) |
| 71 | { |
| 72 | if (!task) |
| 73 | task = current; |
| 74 | |
| 75 | if (!stack) { |
| 76 | unsigned long dummy; |
| 77 | stack = &dummy; |
| 78 | if (task != current) |
| 79 | stack = (unsigned long *)task->thread.sp; |
| 80 | } |
| 81 | |
| 82 | #ifdef CONFIG_FRAME_POINTER |
| 83 | if (!bp) { |
| 84 | if (task == current) { |
| 85 | /* Grab bp right from our regs */ |
| 86 | asm("movl %%ebp, %0" : "=r" (bp) :); |
| 87 | } else { |
| 88 | /* bp is the last reg pushed by switch_to */ |
| 89 | bp = *(unsigned long *) task->thread.sp; |
| 90 | } |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | for (;;) { |
| 95 | struct thread_info *context; |
| 96 | |
| 97 | context = (struct thread_info *) |
| 98 | ((unsigned long)stack & (~(THREAD_SIZE - 1))); |
| 99 | bp = print_context_stack(context, stack, bp, ops, data); |
| 100 | /* |
| 101 | * Should be after the line below, but somewhere |
| 102 | * in early boot context comes out corrupted and we |
| 103 | * can't reference it: |
| 104 | */ |
| 105 | if (ops->stack(data, "IRQ") < 0) |
| 106 | break; |
| 107 | stack = (unsigned long *)context->previous_esp; |
| 108 | if (!stack) |
| 109 | break; |
| 110 | touch_nmi_watchdog(); |
| 111 | } |
| 112 | } |
| 113 | EXPORT_SYMBOL(dump_trace); |
| 114 | |
| 115 | static void |
| 116 | print_trace_warning_symbol(void *data, char *msg, unsigned long symbol) |
| 117 | { |
| 118 | printk(data); |
| 119 | print_symbol(msg, symbol); |
| 120 | printk("\n"); |
| 121 | } |
| 122 | |
| 123 | static void print_trace_warning(void *data, char *msg) |
| 124 | { |
| 125 | printk("%s%s\n", (char *)data, msg); |
| 126 | } |
| 127 | |
| 128 | static int print_trace_stack(void *data, char *name) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Print one address/symbol entries per line. |
| 135 | */ |
| 136 | static void print_trace_address(void *data, unsigned long addr, int reliable) |
| 137 | { |
| 138 | printk("%s [<%08lx>] ", (char *)data, addr); |
| 139 | if (!reliable) |
| 140 | printk("? "); |
| 141 | print_symbol("%s\n", addr); |
| 142 | touch_nmi_watchdog(); |
| 143 | } |
| 144 | |
| 145 | static const struct stacktrace_ops print_trace_ops = { |
| 146 | .warning = print_trace_warning, |
| 147 | .warning_symbol = print_trace_warning_symbol, |
| 148 | .stack = print_trace_stack, |
| 149 | .address = print_trace_address, |
| 150 | }; |
| 151 | |
| 152 | static void |
| 153 | show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, |
| 154 | unsigned long *stack, unsigned long bp, char *log_lvl) |
| 155 | { |
| 156 | dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); |
| 157 | printk("%s =======================\n", log_lvl); |
| 158 | } |
| 159 | |
| 160 | void show_trace(struct task_struct *task, struct pt_regs *regs, |
| 161 | unsigned long *stack, unsigned long bp) |
| 162 | { |
| 163 | show_trace_log_lvl(task, regs, stack, bp, ""); |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, |
| 168 | unsigned long *sp, unsigned long bp, char *log_lvl) |
| 169 | { |
| 170 | unsigned long *stack; |
| 171 | int i; |
| 172 | |
| 173 | if (sp == NULL) { |
| 174 | if (task) |
| 175 | sp = (unsigned long *)task->thread.sp; |
| 176 | else |
| 177 | sp = (unsigned long *)&sp; |
| 178 | } |
| 179 | |
| 180 | stack = sp; |
| 181 | for (i = 0; i < kstack_depth_to_print; i++) { |
| 182 | if (kstack_end(stack)) |
| 183 | break; |
| 184 | if (i && ((i % 8) == 0)) |
| 185 | printk("\n%s ", log_lvl); |
| 186 | printk("%08lx ", *stack++); |
| 187 | } |
| 188 | printk("\n%sCall Trace:\n", log_lvl); |
| 189 | |
| 190 | show_trace_log_lvl(task, regs, sp, bp, log_lvl); |
| 191 | } |
| 192 | |
| 193 | void show_stack(struct task_struct *task, unsigned long *sp) |
| 194 | { |
| 195 | printk(" "); |
| 196 | show_stack_log_lvl(task, NULL, sp, 0, ""); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * The architecture-independent dump_stack generator |
| 201 | */ |
| 202 | void dump_stack(void) |
| 203 | { |
| 204 | unsigned long bp = 0; |
| 205 | unsigned long stack; |
| 206 | |
| 207 | #ifdef CONFIG_FRAME_POINTER |
| 208 | if (!bp) |
| 209 | asm("movl %%ebp, %0" : "=r" (bp):); |
| 210 | #endif |
| 211 | |
| 212 | printk("Pid: %d, comm: %.20s %s %s %.*s\n", |
| 213 | current->pid, current->comm, print_tainted(), |
| 214 | init_utsname()->release, |
| 215 | (int)strcspn(init_utsname()->version, " "), |
| 216 | init_utsname()->version); |
| 217 | |
| 218 | show_trace(current, NULL, &stack, bp); |
| 219 | } |
| 220 | |
| 221 | EXPORT_SYMBOL(dump_stack); |
| 222 | |
| 223 | void show_registers(struct pt_regs *regs) |
| 224 | { |
| 225 | int i; |
| 226 | |
| 227 | print_modules(); |
| 228 | __show_regs(regs, 0); |
| 229 | |
| 230 | printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)", |
| 231 | TASK_COMM_LEN, current->comm, task_pid_nr(current), |
| 232 | current_thread_info(), current, task_thread_info(current)); |
| 233 | /* |
| 234 | * When in-kernel, we also print out the stack and code at the |
| 235 | * time of the fault.. |
| 236 | */ |
| 237 | if (!user_mode_vm(regs)) { |
| 238 | unsigned int code_prologue = code_bytes * 43 / 64; |
| 239 | unsigned int code_len = code_bytes; |
| 240 | unsigned char c; |
| 241 | u8 *ip; |
| 242 | |
| 243 | printk("\n" KERN_EMERG "Stack: "); |
| 244 | show_stack_log_lvl(NULL, regs, ®s->sp, 0, KERN_EMERG); |
| 245 | |
| 246 | printk(KERN_EMERG "Code: "); |
| 247 | |
| 248 | ip = (u8 *)regs->ip - code_prologue; |
| 249 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { |
| 250 | /* try starting at EIP */ |
| 251 | ip = (u8 *)regs->ip; |
| 252 | code_len = code_len - code_prologue + 1; |
| 253 | } |
| 254 | for (i = 0; i < code_len; i++, ip++) { |
| 255 | if (ip < (u8 *)PAGE_OFFSET || |
| 256 | probe_kernel_address(ip, c)) { |
| 257 | printk(" Bad EIP value."); |
| 258 | break; |
| 259 | } |
| 260 | if (ip == (u8 *)regs->ip) |
| 261 | printk("<%02x> ", c); |
| 262 | else |
| 263 | printk("%02x ", c); |
| 264 | } |
| 265 | } |
| 266 | printk("\n"); |
| 267 | } |
| 268 | |
| 269 | int is_valid_bugaddr(unsigned long ip) |
| 270 | { |
| 271 | unsigned short ud2; |
| 272 | |
| 273 | if (ip < PAGE_OFFSET) |
| 274 | return 0; |
| 275 | if (probe_kernel_address((unsigned short *)ip, ud2)) |
| 276 | return 0; |
| 277 | |
| 278 | return ud2 == 0x0b0f; |
| 279 | } |
| 280 | |
| 281 | static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED; |
| 282 | static int die_owner = -1; |
| 283 | static unsigned int die_nest_count; |
| 284 | |
| 285 | unsigned __kprobes long oops_begin(void) |
| 286 | { |
| 287 | unsigned long flags; |
| 288 | |
| 289 | oops_enter(); |
| 290 | |
| 291 | if (die_owner != raw_smp_processor_id()) { |
| 292 | console_verbose(); |
| 293 | raw_local_irq_save(flags); |
| 294 | __raw_spin_lock(&die_lock); |
| 295 | die_owner = smp_processor_id(); |
| 296 | die_nest_count = 0; |
| 297 | bust_spinlocks(1); |
| 298 | } else { |
| 299 | raw_local_irq_save(flags); |
| 300 | } |
| 301 | die_nest_count++; |
| 302 | return flags; |
| 303 | } |
| 304 | |
| 305 | void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr) |
| 306 | { |
| 307 | bust_spinlocks(0); |
| 308 | die_owner = -1; |
| 309 | add_taint(TAINT_DIE); |
| 310 | __raw_spin_unlock(&die_lock); |
| 311 | raw_local_irq_restore(flags); |
| 312 | |
| 313 | if (!regs) |
| 314 | return; |
| 315 | |
| 316 | if (kexec_should_crash(current)) |
| 317 | crash_kexec(regs); |
| 318 | |
| 319 | if (in_interrupt()) |
| 320 | panic("Fatal exception in interrupt"); |
| 321 | |
| 322 | if (panic_on_oops) |
| 323 | panic("Fatal exception"); |
| 324 | |
| 325 | oops_exit(); |
| 326 | do_exit(signr); |
| 327 | } |
| 328 | |
| 329 | int __kprobes __die(const char *str, struct pt_regs *regs, long err) |
| 330 | { |
| 331 | unsigned short ss; |
| 332 | unsigned long sp; |
| 333 | |
| 334 | printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter); |
| 335 | #ifdef CONFIG_PREEMPT |
| 336 | printk("PREEMPT "); |
| 337 | #endif |
| 338 | #ifdef CONFIG_SMP |
| 339 | printk("SMP "); |
| 340 | #endif |
| 341 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 342 | printk("DEBUG_PAGEALLOC"); |
| 343 | #endif |
| 344 | printk("\n"); |
| 345 | if (notify_die(DIE_OOPS, str, regs, err, |
| 346 | current->thread.trap_no, SIGSEGV) == NOTIFY_STOP) |
| 347 | return 1; |
| 348 | |
| 349 | show_registers(regs); |
| 350 | /* Executive summary in case the oops scrolled away */ |
| 351 | sp = (unsigned long) (®s->sp); |
| 352 | savesegment(ss, ss); |
| 353 | if (user_mode(regs)) { |
| 354 | sp = regs->sp; |
| 355 | ss = regs->ss & 0xffff; |
| 356 | } |
| 357 | printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip); |
| 358 | print_symbol("%s", regs->ip); |
| 359 | printk(" SS:ESP %04x:%08lx\n", ss, sp); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * This is gone through when something in the kernel has done something bad |
| 365 | * and is about to be terminated: |
| 366 | */ |
| 367 | void die(const char *str, struct pt_regs *regs, long err) |
| 368 | { |
| 369 | unsigned long flags = oops_begin(); |
| 370 | |
| 371 | if (die_nest_count < 3) { |
| 372 | report_bug(regs->ip, regs); |
| 373 | |
| 374 | if (__die(str, regs, err)) |
| 375 | regs = NULL; |
| 376 | } else { |
| 377 | printk(KERN_EMERG "Recursive die() failure, output suppressed\n"); |
| 378 | } |
| 379 | |
| 380 | oops_end(flags, regs, SIGSEGV); |
| 381 | } |
| 382 | |
Alexander van Heukelum | dd6e4eb | 2008-10-04 23:12:40 +0200 | [diff] [blame] | 383 | static DEFINE_SPINLOCK(nmi_print_lock); |
| 384 | |
| 385 | void notrace __kprobes |
| 386 | die_nmi(char *str, struct pt_regs *regs, int do_panic) |
| 387 | { |
| 388 | if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP) |
| 389 | return; |
| 390 | |
| 391 | spin_lock(&nmi_print_lock); |
| 392 | /* |
| 393 | * We are in trouble anyway, lets at least try |
| 394 | * to get a message out: |
| 395 | */ |
| 396 | bust_spinlocks(1); |
| 397 | printk(KERN_EMERG "%s", str); |
| 398 | printk(" on CPU%d, ip %08lx, registers:\n", |
| 399 | smp_processor_id(), regs->ip); |
| 400 | show_registers(regs); |
| 401 | if (do_panic) |
| 402 | panic("Non maskable interrupt"); |
| 403 | console_silent(); |
| 404 | spin_unlock(&nmi_print_lock); |
| 405 | bust_spinlocks(0); |
| 406 | |
| 407 | /* |
| 408 | * If we are in kernel we are probably nested up pretty bad |
| 409 | * and might aswell get out now while we still can: |
| 410 | */ |
| 411 | if (!user_mode_vm(regs)) { |
| 412 | current->thread.trap_no = 2; |
| 413 | crash_kexec(regs); |
| 414 | } |
| 415 | |
| 416 | do_exit(SIGSEGV); |
| 417 | } |
| 418 | |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 419 | static int __init kstack_setup(char *s) |
| 420 | { |
| 421 | kstack_depth_to_print = simple_strtoul(s, NULL, 0); |
| 422 | |
| 423 | return 1; |
| 424 | } |
| 425 | __setup("kstack=", kstack_setup); |
| 426 | |
| 427 | static int __init code_bytes_setup(char *s) |
| 428 | { |
| 429 | code_bytes = simple_strtoul(s, NULL, 0); |
| 430 | if (code_bytes > 8192) |
| 431 | code_bytes = 8192; |
| 432 | |
| 433 | return 1; |
| 434 | } |
| 435 | __setup("code_bytes=", code_bytes_setup); |