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