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