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