blob: 0d1ff4b407d4e1f9c445cf7516495824f85f3cfe [file] [log] [blame]
Neil Horman878719e2008-10-23 10:40:06 -04001/*
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 Rostedt712406a2009-02-09 10:54:03 -080013#include <linux/ftrace.h>
Neil Horman878719e2008-10-23 10:40:06 -040014#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 Horman878719e2008-10-23 10:40:06 -040021
22int panic_on_unrecovered_nmi;
Kurt Garloff5211a242009-06-24 14:32:11 -070023int panic_on_io_nmi;
Neil Horman878719e2008-10-23 10:40:06 -040024unsigned int code_bytes = 64;
25int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
26static int die_counter;
27
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +010028static void printk_stack_address(unsigned long address, int reliable,
29 void *data)
Neil Horman878719e2008-10-23 10:40:06 -040030{
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +010031 printk("%s [<%p>] %s%pB\n",
32 (char *)data, (void *)address, reliable ? "" : "? ",
33 (void *)address);
Neil Horman878719e2008-10-23 10:40:06 -040034}
35
Jiri Slaby5f01c982013-10-25 15:06:58 +020036void printk_address(unsigned long address)
37{
38 pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
39}
40
Steven Rostedt7ee991f2008-12-02 23:50:04 -050041#ifdef CONFIG_FUNCTION_GRAPH_TRACER
42static void
43print_ftrace_graph_addr(unsigned long addr, void *data,
44 const struct stacktrace_ops *ops,
45 struct thread_info *tinfo, int *graph)
46{
Hugh Dickins65c0ff42012-03-23 14:02:55 -070047 struct task_struct *task;
Steven Rostedt7ee991f2008-12-02 23:50:04 -050048 unsigned long ret_addr;
Hugh Dickins65c0ff42012-03-23 14:02:55 -070049 int index;
Steven Rostedt7ee991f2008-12-02 23:50:04 -050050
51 if (addr != (unsigned long)return_to_handler)
52 return;
53
Hugh Dickins65c0ff42012-03-23 14:02:55 -070054 task = tinfo->task;
55 index = task->curr_ret_stack;
56
Steven Rostedt7ee991f2008-12-02 23:50:04 -050057 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
68static inline void
69print_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 Horman878719e2008-10-23 10:40:06 -040075/*
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
82static 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
95unsigned long
96print_context_stack(struct thread_info *tinfo,
97 unsigned long *stack, unsigned long bp,
98 const struct stacktrace_ops *ops, void *data,
Steven Rostedt7ee991f2008-12-02 23:50:04 -050099 unsigned long *end, int *graph)
Neil Horman878719e2008-10-23 10:40:06 -0400100{
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 Ven2c344e92009-02-07 12:23:37 -0800113 ops->address(data, addr, 0);
Neil Horman878719e2008-10-23 10:40:06 -0400114 }
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500115 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
Neil Horman878719e2008-10-23 10:40:06 -0400116 }
117 stack++;
118 }
119 return bp;
120}
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100121EXPORT_SYMBOL_GPL(print_context_stack);
122
123unsigned long
124print_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 Weisbeckerc2c5d452009-12-31 03:52:25 +0100135 if (!__kernel_text_address(addr))
136 break;
137
Alexei Starovoitov568b3292016-02-17 19:58:57 -0800138 if (ops->address(data, addr, 1))
139 break;
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100140 frame = frame->next_frame;
141 ret_addr = &frame->return_address;
142 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100143 }
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100144
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100145 return (unsigned long)frame;
146}
147EXPORT_SYMBOL_GPL(print_context_stack_bp);
Neil Horman878719e2008-10-23 10:40:06 -0400148
Neil Horman878719e2008-10-23 10:40:06 -0400149static int print_trace_stack(void *data, char *name)
150{
151 printk("%s <%s> ", (char *)data, name);
152 return 0;
153}
154
155/*
156 * Print one address/symbol entries per line.
157 */
Alexei Starovoitov568b3292016-02-17 19:58:57 -0800158static int print_trace_address(void *data, unsigned long addr, int reliable)
Neil Horman878719e2008-10-23 10:40:06 -0400159{
160 touch_nmi_watchdog();
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +0100161 printk_stack_address(addr, reliable, data);
Alexei Starovoitov568b3292016-02-17 19:58:57 -0800162 return 0;
Neil Horman878719e2008-10-23 10:40:06 -0400163}
164
165static const struct stacktrace_ops print_trace_ops = {
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100166 .stack = print_trace_stack,
167 .address = print_trace_address,
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100168 .walk_stack = print_context_stack,
Neil Horman878719e2008-10-23 10:40:06 -0400169};
170
171void
172show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900173 unsigned long *stack, unsigned long bp, char *log_lvl)
Neil Horman878719e2008-10-23 10:40:06 -0400174{
175 printk("%sCall Trace:\n", log_lvl);
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900176 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
Neil Horman878719e2008-10-23 10:40:06 -0400177}
178
179void show_trace(struct task_struct *task, struct pt_regs *regs,
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900180 unsigned long *stack, unsigned long bp)
Neil Horman878719e2008-10-23 10:40:06 -0400181{
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900182 show_trace_log_lvl(task, regs, stack, bp, "");
Neil Horman878719e2008-10-23 10:40:06 -0400183}
184
185void show_stack(struct task_struct *task, unsigned long *sp)
186{
Tejun Heoa77f2a42013-04-30 15:27:09 -0700187 unsigned long bp = 0;
188 unsigned long stack;
189
190 /*
191 * Stack frames below this one aren't interesting. Don't show them
192 * if we're printing for %current.
193 */
194 if (!sp && (!task || task == current)) {
195 sp = &stack;
196 bp = stack_frame(current, NULL);
197 }
198
199 show_stack_log_lvl(task, NULL, sp, bp, "");
Neil Horman878719e2008-10-23 10:40:06 -0400200}
201
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100202static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Neil Horman878719e2008-10-23 10:40:06 -0400203static int die_owner = -1;
204static unsigned int die_nest_count;
205
Masami Hiramatsu93266382014-04-17 17:18:14 +0900206unsigned long oops_begin(void)
Neil Horman878719e2008-10-23 10:40:06 -0400207{
208 int cpu;
209 unsigned long flags;
210
Neil Horman878719e2008-10-23 10:40:06 -0400211 oops_enter();
212
213 /* racy, but better than risking deadlock. */
214 raw_local_irq_save(flags);
215 cpu = smp_processor_id();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100216 if (!arch_spin_trylock(&die_lock)) {
Neil Horman878719e2008-10-23 10:40:06 -0400217 if (cpu == die_owner)
218 /* nested oops. should stop eventually */;
219 else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100220 arch_spin_lock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400221 }
222 die_nest_count++;
223 die_owner = cpu;
224 console_verbose();
225 bust_spinlocks(1);
226 return flags;
227}
Huang Ying81e88fd2011-01-12 14:44:55 +0800228EXPORT_SYMBOL_GPL(oops_begin);
Masami Hiramatsu93266382014-04-17 17:18:14 +0900229NOKPROBE_SYMBOL(oops_begin);
Neil Horman878719e2008-10-23 10:40:06 -0400230
Masami Hiramatsu93266382014-04-17 17:18:14 +0900231void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Neil Horman878719e2008-10-23 10:40:06 -0400232{
233 if (regs && kexec_should_crash(current))
234 crash_kexec(regs);
235
236 bust_spinlocks(0);
237 die_owner = -1;
Rusty Russell373d4d02013-01-21 17:17:39 +1030238 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Neil Horman878719e2008-10-23 10:40:06 -0400239 die_nest_count--;
240 if (!die_nest_count)
241 /* Nest count reaches zero, release the lock. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100242 arch_spin_unlock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400243 raw_local_irq_restore(flags);
244 oops_exit();
245
246 if (!signr)
247 return;
248 if (in_interrupt())
249 panic("Fatal exception in interrupt");
250 if (panic_on_oops)
251 panic("Fatal exception");
252 do_exit(signr);
253}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900254NOKPROBE_SYMBOL(oops_end);
Neil Horman878719e2008-10-23 10:40:06 -0400255
Masami Hiramatsu93266382014-04-17 17:18:14 +0900256int __die(const char *str, struct pt_regs *regs, long err)
Neil Horman878719e2008-10-23 10:40:06 -0400257{
258#ifdef CONFIG_X86_32
259 unsigned short ss;
260 unsigned long sp;
261#endif
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500262 printk(KERN_DEFAULT
263 "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
Neil Horman878719e2008-10-23 10:40:06 -0400264#ifdef CONFIG_PREEMPT
265 printk("PREEMPT ");
266#endif
267#ifdef CONFIG_SMP
268 printk("SMP ");
269#endif
270#ifdef CONFIG_DEBUG_PAGEALLOC
Andrey Ryabininef7f0d62015-02-13 14:39:25 -0800271 printk("DEBUG_PAGEALLOC ");
272#endif
273#ifdef CONFIG_KASAN
274 printk("KASAN");
Neil Horman878719e2008-10-23 10:40:06 -0400275#endif
276 printk("\n");
Neil Horman878719e2008-10-23 10:40:06 -0400277 if (notify_die(DIE_OOPS, str, regs, err,
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530278 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
Neil Horman878719e2008-10-23 10:40:06 -0400279 return 1;
280
Jan Beulich0fa0e2f2012-06-18 11:40:04 +0100281 print_modules();
Jan Beulich57da8b92012-05-09 08:47:37 +0100282 show_regs(regs);
Neil Horman878719e2008-10-23 10:40:06 -0400283#ifdef CONFIG_X86_32
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700284 if (user_mode(regs)) {
Neil Horman878719e2008-10-23 10:40:06 -0400285 sp = regs->sp;
286 ss = regs->ss & 0xffff;
H. Peter Anvina343c752009-10-12 14:11:09 -0700287 } else {
288 sp = kernel_stack_pointer(regs);
289 savesegment(ss, ss);
Neil Horman878719e2008-10-23 10:40:06 -0400290 }
291 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
292 print_symbol("%s", regs->ip);
293 printk(" SS:ESP %04x:%08lx\n", ss, sp);
294#else
295 /* Executive summary in case the oops scrolled away */
296 printk(KERN_ALERT "RIP ");
Jiri Slaby5f01c982013-10-25 15:06:58 +0200297 printk_address(regs->ip);
Neil Horman878719e2008-10-23 10:40:06 -0400298 printk(" RSP <%016lx>\n", regs->sp);
299#endif
300 return 0;
301}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900302NOKPROBE_SYMBOL(__die);
Neil Horman878719e2008-10-23 10:40:06 -0400303
304/*
305 * This is gone through when something in the kernel has done something bad
306 * and is about to be terminated:
307 */
308void die(const char *str, struct pt_regs *regs, long err)
309{
310 unsigned long flags = oops_begin();
311 int sig = SIGSEGV;
312
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700313 if (!user_mode(regs))
Neil Horman878719e2008-10-23 10:40:06 -0400314 report_bug(regs->ip, regs);
315
316 if (__die(str, regs, err))
317 sig = 0;
318 oops_end(flags, regs, sig);
319}
320
Neil Horman878719e2008-10-23 10:40:06 -0400321static int __init kstack_setup(char *s)
322{
Shuah Khan363f7ce2012-05-06 11:58:04 -0600323 ssize_t ret;
324 unsigned long val;
325
Neil Horman878719e2008-10-23 10:40:06 -0400326 if (!s)
327 return -EINVAL;
Shuah Khan363f7ce2012-05-06 11:58:04 -0600328
329 ret = kstrtoul(s, 0, &val);
330 if (ret)
331 return ret;
332 kstack_depth_to_print = val;
Neil Horman878719e2008-10-23 10:40:06 -0400333 return 0;
334}
335early_param("kstack", kstack_setup);
336
337static int __init code_bytes_setup(char *s)
338{
Shuah Khan363f7ce2012-05-06 11:58:04 -0600339 ssize_t ret;
340 unsigned long val;
341
342 if (!s)
343 return -EINVAL;
344
345 ret = kstrtoul(s, 0, &val);
346 if (ret)
347 return ret;
348
349 code_bytes = val;
Neil Horman878719e2008-10-23 10:40:06 -0400350 if (code_bytes > 8192)
351 code_bytes = 8192;
352
353 return 1;
354}
355__setup("code_bytes=", code_bytes_setup);