| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [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> | 
| Steven Rostedt | 712406a | 2009-02-09 10:54:03 -0800 | [diff] [blame] | 13 | #include <linux/ftrace.h> | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 14 | #include <linux/kexec.h> | 
|  | 15 | #include <linux/bug.h> | 
|  | 16 | #include <linux/nmi.h> | 
|  | 17 | #include <linux/sysfs.h> | 
|  | 18 |  | 
|  | 19 | #include <asm/stacktrace.h> | 
|  | 20 |  | 
|  | 21 | #include "dumpstack.h" | 
|  | 22 |  | 
|  | 23 | int panic_on_unrecovered_nmi; | 
| Kurt Garloff | 5211a24 | 2009-06-24 14:32:11 -0700 | [diff] [blame] | 24 | int panic_on_io_nmi; | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 25 | unsigned int code_bytes = 64; | 
|  | 26 | int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; | 
|  | 27 | static int die_counter; | 
|  | 28 |  | 
|  | 29 | void printk_address(unsigned long address, int reliable) | 
|  | 30 | { | 
|  | 31 | printk(" [<%p>] %s%pS\n", (void *) address, | 
|  | 32 | reliable ? "" : "? ", (void *) address); | 
|  | 33 | } | 
|  | 34 |  | 
| Steven Rostedt | 7ee991f | 2008-12-02 23:50:04 -0500 | [diff] [blame] | 35 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | 
|  | 36 | static void | 
|  | 37 | print_ftrace_graph_addr(unsigned long addr, void *data, | 
|  | 38 | const struct stacktrace_ops *ops, | 
|  | 39 | struct thread_info *tinfo, int *graph) | 
|  | 40 | { | 
|  | 41 | struct task_struct *task = tinfo->task; | 
|  | 42 | unsigned long ret_addr; | 
|  | 43 | int index = task->curr_ret_stack; | 
|  | 44 |  | 
|  | 45 | if (addr != (unsigned long)return_to_handler) | 
|  | 46 | return; | 
|  | 47 |  | 
|  | 48 | if (!task->ret_stack || index < *graph) | 
|  | 49 | return; | 
|  | 50 |  | 
|  | 51 | index -= *graph; | 
|  | 52 | ret_addr = task->ret_stack[index].ret; | 
|  | 53 |  | 
|  | 54 | ops->address(data, ret_addr, 1); | 
|  | 55 |  | 
|  | 56 | (*graph)++; | 
|  | 57 | } | 
|  | 58 | #else | 
|  | 59 | static inline void | 
|  | 60 | print_ftrace_graph_addr(unsigned long addr, void *data, | 
|  | 61 | const struct stacktrace_ops *ops, | 
|  | 62 | struct thread_info *tinfo, int *graph) | 
|  | 63 | { } | 
|  | 64 | #endif | 
|  | 65 |  | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 66 | /* | 
|  | 67 | * x86-64 can have up to three kernel stacks: | 
|  | 68 | * process stack | 
|  | 69 | * interrupt stack | 
|  | 70 | * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack | 
|  | 71 | */ | 
|  | 72 |  | 
|  | 73 | static inline int valid_stack_ptr(struct thread_info *tinfo, | 
|  | 74 | void *p, unsigned int size, void *end) | 
|  | 75 | { | 
|  | 76 | void *t = tinfo; | 
|  | 77 | if (end) { | 
|  | 78 | if (p < end && p >= (end-THREAD_SIZE)) | 
|  | 79 | return 1; | 
|  | 80 | else | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 | return p > t && p < t + THREAD_SIZE - size; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | unsigned long | 
|  | 87 | print_context_stack(struct thread_info *tinfo, | 
|  | 88 | unsigned long *stack, unsigned long bp, | 
|  | 89 | const struct stacktrace_ops *ops, void *data, | 
| Steven Rostedt | 7ee991f | 2008-12-02 23:50:04 -0500 | [diff] [blame] | 90 | unsigned long *end, int *graph) | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 91 | { | 
|  | 92 | struct stack_frame *frame = (struct stack_frame *)bp; | 
|  | 93 |  | 
|  | 94 | while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) { | 
|  | 95 | unsigned long addr; | 
|  | 96 |  | 
|  | 97 | addr = *stack; | 
|  | 98 | if (__kernel_text_address(addr)) { | 
|  | 99 | if ((unsigned long) stack == bp + sizeof(long)) { | 
|  | 100 | ops->address(data, addr, 1); | 
|  | 101 | frame = frame->next_frame; | 
|  | 102 | bp = (unsigned long) frame; | 
|  | 103 | } else { | 
| Arjan van de Ven | 2c344e9 | 2009-02-07 12:23:37 -0800 | [diff] [blame] | 104 | ops->address(data, addr, 0); | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 105 | } | 
| Steven Rostedt | 7ee991f | 2008-12-02 23:50:04 -0500 | [diff] [blame] | 106 | print_ftrace_graph_addr(addr, data, ops, tinfo, graph); | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 107 | } | 
|  | 108 | stack++; | 
|  | 109 | } | 
|  | 110 | return bp; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 |  | 
|  | 114 | static void | 
|  | 115 | print_trace_warning_symbol(void *data, char *msg, unsigned long symbol) | 
|  | 116 | { | 
|  | 117 | printk(data); | 
|  | 118 | print_symbol(msg, symbol); | 
|  | 119 | printk("\n"); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | static void print_trace_warning(void *data, char *msg) | 
|  | 123 | { | 
|  | 124 | printk("%s%s\n", (char *)data, msg); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | static int print_trace_stack(void *data, char *name) | 
|  | 128 | { | 
|  | 129 | printk("%s <%s> ", (char *)data, name); | 
|  | 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 | touch_nmi_watchdog(); | 
|  | 139 | printk(data); | 
|  | 140 | printk_address(addr, reliable); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | static const struct stacktrace_ops print_trace_ops = { | 
|  | 144 | .warning = print_trace_warning, | 
|  | 145 | .warning_symbol = print_trace_warning_symbol, | 
|  | 146 | .stack = print_trace_stack, | 
|  | 147 | .address = print_trace_address, | 
|  | 148 | }; | 
|  | 149 |  | 
|  | 150 | void | 
|  | 151 | show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, | 
|  | 152 | unsigned long *stack, unsigned long bp, char *log_lvl) | 
|  | 153 | { | 
|  | 154 | printk("%sCall Trace:\n", log_lvl); | 
|  | 155 | dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | void show_trace(struct task_struct *task, struct pt_regs *regs, | 
|  | 159 | unsigned long *stack, unsigned long bp) | 
|  | 160 | { | 
|  | 161 | show_trace_log_lvl(task, regs, stack, bp, ""); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | void show_stack(struct task_struct *task, unsigned long *sp) | 
|  | 165 | { | 
|  | 166 | show_stack_log_lvl(task, NULL, sp, 0, ""); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | /* | 
|  | 170 | * The architecture-independent dump_stack generator | 
|  | 171 | */ | 
|  | 172 | void dump_stack(void) | 
|  | 173 | { | 
|  | 174 | unsigned long bp = 0; | 
|  | 175 | unsigned long stack; | 
|  | 176 |  | 
|  | 177 | #ifdef CONFIG_FRAME_POINTER | 
|  | 178 | if (!bp) | 
|  | 179 | get_bp(bp); | 
|  | 180 | #endif | 
|  | 181 |  | 
|  | 182 | printk("Pid: %d, comm: %.20s %s %s %.*s\n", | 
|  | 183 | current->pid, current->comm, print_tainted(), | 
|  | 184 | init_utsname()->release, | 
|  | 185 | (int)strcspn(init_utsname()->version, " "), | 
|  | 186 | init_utsname()->version); | 
|  | 187 | show_trace(NULL, NULL, &stack, bp); | 
|  | 188 | } | 
|  | 189 | EXPORT_SYMBOL(dump_stack); | 
|  | 190 |  | 
| Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 191 | static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 192 | static int die_owner = -1; | 
|  | 193 | static unsigned int die_nest_count; | 
|  | 194 |  | 
|  | 195 | unsigned __kprobes long oops_begin(void) | 
|  | 196 | { | 
|  | 197 | int cpu; | 
|  | 198 | unsigned long flags; | 
|  | 199 |  | 
| Markus Metzger | b181874 | 2009-01-19 10:31:01 +0100 | [diff] [blame] | 200 | /* notify the hw-branch tracer so it may disable tracing and | 
|  | 201 | add the last trace to the trace buffer - | 
|  | 202 | the earlier this happens, the more useful the trace. */ | 
|  | 203 | trace_hw_branch_oops(); | 
|  | 204 |  | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 205 | oops_enter(); | 
|  | 206 |  | 
|  | 207 | /* racy, but better than risking deadlock. */ | 
|  | 208 | raw_local_irq_save(flags); | 
|  | 209 | cpu = smp_processor_id(); | 
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 210 | if (!arch_spin_trylock(&die_lock)) { | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 211 | if (cpu == die_owner) | 
|  | 212 | /* nested oops. should stop eventually */; | 
|  | 213 | else | 
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 214 | arch_spin_lock(&die_lock); | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 215 | } | 
|  | 216 | die_nest_count++; | 
|  | 217 | die_owner = cpu; | 
|  | 218 | console_verbose(); | 
|  | 219 | bust_spinlocks(1); | 
|  | 220 | return flags; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr) | 
|  | 224 | { | 
|  | 225 | if (regs && kexec_should_crash(current)) | 
|  | 226 | crash_kexec(regs); | 
|  | 227 |  | 
|  | 228 | bust_spinlocks(0); | 
|  | 229 | die_owner = -1; | 
|  | 230 | add_taint(TAINT_DIE); | 
|  | 231 | die_nest_count--; | 
|  | 232 | if (!die_nest_count) | 
|  | 233 | /* Nest count reaches zero, release the lock. */ | 
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 234 | arch_spin_unlock(&die_lock); | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 235 | raw_local_irq_restore(flags); | 
|  | 236 | oops_exit(); | 
|  | 237 |  | 
|  | 238 | if (!signr) | 
|  | 239 | return; | 
|  | 240 | if (in_interrupt()) | 
|  | 241 | panic("Fatal exception in interrupt"); | 
|  | 242 | if (panic_on_oops) | 
|  | 243 | panic("Fatal exception"); | 
|  | 244 | do_exit(signr); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | int __kprobes __die(const char *str, struct pt_regs *regs, long err) | 
|  | 248 | { | 
|  | 249 | #ifdef CONFIG_X86_32 | 
|  | 250 | unsigned short ss; | 
|  | 251 | unsigned long sp; | 
|  | 252 | #endif | 
|  | 253 | printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter); | 
|  | 254 | #ifdef CONFIG_PREEMPT | 
|  | 255 | printk("PREEMPT "); | 
|  | 256 | #endif | 
|  | 257 | #ifdef CONFIG_SMP | 
|  | 258 | printk("SMP "); | 
|  | 259 | #endif | 
|  | 260 | #ifdef CONFIG_DEBUG_PAGEALLOC | 
|  | 261 | printk("DEBUG_PAGEALLOC"); | 
|  | 262 | #endif | 
|  | 263 | printk("\n"); | 
|  | 264 | sysfs_printk_last_file(); | 
|  | 265 | if (notify_die(DIE_OOPS, str, regs, err, | 
|  | 266 | current->thread.trap_no, SIGSEGV) == NOTIFY_STOP) | 
|  | 267 | return 1; | 
|  | 268 |  | 
|  | 269 | show_registers(regs); | 
|  | 270 | #ifdef CONFIG_X86_32 | 
| H. Peter Anvin | a343c75 | 2009-10-12 14:11:09 -0700 | [diff] [blame] | 271 | if (user_mode_vm(regs)) { | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 272 | sp = regs->sp; | 
|  | 273 | ss = regs->ss & 0xffff; | 
| H. Peter Anvin | a343c75 | 2009-10-12 14:11:09 -0700 | [diff] [blame] | 274 | } else { | 
|  | 275 | sp = kernel_stack_pointer(regs); | 
|  | 276 | savesegment(ss, ss); | 
| Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 277 | } | 
|  | 278 | printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip); | 
|  | 279 | print_symbol("%s", regs->ip); | 
|  | 280 | printk(" SS:ESP %04x:%08lx\n", ss, sp); | 
|  | 281 | #else | 
|  | 282 | /* Executive summary in case the oops scrolled away */ | 
|  | 283 | printk(KERN_ALERT "RIP "); | 
|  | 284 | printk_address(regs->ip, 1); | 
|  | 285 | printk(" RSP <%016lx>\n", regs->sp); | 
|  | 286 | #endif | 
|  | 287 | return 0; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | /* | 
|  | 291 | * This is gone through when something in the kernel has done something bad | 
|  | 292 | * and is about to be terminated: | 
|  | 293 | */ | 
|  | 294 | void die(const char *str, struct pt_regs *regs, long err) | 
|  | 295 | { | 
|  | 296 | unsigned long flags = oops_begin(); | 
|  | 297 | int sig = SIGSEGV; | 
|  | 298 |  | 
|  | 299 | if (!user_mode_vm(regs)) | 
|  | 300 | report_bug(regs->ip, regs); | 
|  | 301 |  | 
|  | 302 | if (__die(str, regs, err)) | 
|  | 303 | sig = 0; | 
|  | 304 | oops_end(flags, regs, sig); | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | void notrace __kprobes | 
|  | 308 | die_nmi(char *str, struct pt_regs *regs, int do_panic) | 
|  | 309 | { | 
|  | 310 | unsigned long flags; | 
|  | 311 |  | 
|  | 312 | if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP) | 
|  | 313 | return; | 
|  | 314 |  | 
|  | 315 | /* | 
|  | 316 | * We are in trouble anyway, lets at least try | 
|  | 317 | * to get a message out. | 
|  | 318 | */ | 
|  | 319 | flags = oops_begin(); | 
|  | 320 | printk(KERN_EMERG "%s", str); | 
|  | 321 | printk(" on CPU%d, ip %08lx, registers:\n", | 
|  | 322 | smp_processor_id(), regs->ip); | 
|  | 323 | show_registers(regs); | 
|  | 324 | oops_end(flags, regs, 0); | 
|  | 325 | if (do_panic || panic_on_oops) | 
|  | 326 | panic("Non maskable interrupt"); | 
|  | 327 | nmi_exit(); | 
|  | 328 | local_irq_enable(); | 
|  | 329 | do_exit(SIGBUS); | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | static int __init oops_setup(char *s) | 
|  | 333 | { | 
|  | 334 | if (!s) | 
|  | 335 | return -EINVAL; | 
|  | 336 | if (!strcmp(s, "panic")) | 
|  | 337 | panic_on_oops = 1; | 
|  | 338 | return 0; | 
|  | 339 | } | 
|  | 340 | early_param("oops", oops_setup); | 
|  | 341 |  | 
|  | 342 | static int __init kstack_setup(char *s) | 
|  | 343 | { | 
|  | 344 | if (!s) | 
|  | 345 | return -EINVAL; | 
|  | 346 | kstack_depth_to_print = simple_strtoul(s, NULL, 0); | 
|  | 347 | return 0; | 
|  | 348 | } | 
|  | 349 | early_param("kstack", kstack_setup); | 
|  | 350 |  | 
|  | 351 | static int __init code_bytes_setup(char *s) | 
|  | 352 | { | 
|  | 353 | code_bytes = simple_strtoul(s, NULL, 0); | 
|  | 354 | if (code_bytes > 8192) | 
|  | 355 | code_bytes = 8192; | 
|  | 356 |  | 
|  | 357 | return 1; | 
|  | 358 | } | 
|  | 359 | __setup("code_bytes=", code_bytes_setup); |