ftrace: Add ftrace_graph_ret_addr() stack unwinding helpers
[linux-2.6-block.git] / arch / x86 / kernel / dumpstack.c
CommitLineData
878719e8
NH
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>
712406a6 13#include <linux/ftrace.h>
878719e8
NH
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
878719e8
NH
21
22int panic_on_unrecovered_nmi;
5211a242 23int panic_on_io_nmi;
878719e8
NH
24unsigned int code_bytes = 64;
25int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
26static int die_counter;
27
1fc7f61c
AS
28static void printk_stack_address(unsigned long address, int reliable,
29 void *data)
878719e8 30{
1fc7f61c
AS
31 printk("%s [<%p>] %s%pB\n",
32 (char *)data, (void *)address, reliable ? "" : "? ",
33 (void *)address);
878719e8
NH
34}
35
5f01c988
JS
36void printk_address(unsigned long address)
37{
38 pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
39}
40
7ee991fb
SR
41#ifdef CONFIG_FUNCTION_GRAPH_TRACER
42static void
43print_ftrace_graph_addr(unsigned long addr, void *data,
44 const struct stacktrace_ops *ops,
da01e18a 45 struct task_struct *task, int *graph)
7ee991fb 46{
7ee991fb 47 unsigned long ret_addr;
65c0ff40 48 int index;
7ee991fb
SR
49
50 if (addr != (unsigned long)return_to_handler)
51 return;
52
65c0ff40
HD
53 index = task->curr_ret_stack;
54
7ee991fb
SR
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
66static inline void
67print_ftrace_graph_addr(unsigned long addr, void *data,
68 const struct stacktrace_ops *ops,
da01e18a 69 struct task_struct *task, int *graph)
7ee991fb
SR
70{ }
71#endif
72
878719e8
NH
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
da01e18a 80static inline int valid_stack_ptr(struct task_struct *task,
878719e8
NH
81 void *p, unsigned int size, void *end)
82{
aca9c293 83 void *t = task_stack_page(task);
878719e8
NH
84 if (end) {
85 if (p < end && p >= (end-THREAD_SIZE))
86 return 1;
87 else
88 return 0;
89 }
9a2e9da3 90 return p >= t && p < t + THREAD_SIZE - size;
878719e8
NH
91}
92
93unsigned long
da01e18a 94print_context_stack(struct task_struct *task,
878719e8
NH
95 unsigned long *stack, unsigned long bp,
96 const struct stacktrace_ops *ops, void *data,
7ee991fb 97 unsigned long *end, int *graph)
878719e8
NH
98{
99 struct stack_frame *frame = (struct stack_frame *)bp;
100
9a2e9da3
AL
101 /*
102 * If we overflowed the stack into a guard page, jump back to the
103 * bottom of the usable stack.
104 */
105 if ((unsigned long)task_stack_page(task) - (unsigned long)stack <
106 PAGE_SIZE)
107 stack = (unsigned long *)task_stack_page(task);
108
da01e18a 109 while (valid_stack_ptr(task, stack, sizeof(*stack), end)) {
878719e8
NH
110 unsigned long addr;
111
112 addr = *stack;
113 if (__kernel_text_address(addr)) {
114 if ((unsigned long) stack == bp + sizeof(long)) {
115 ops->address(data, addr, 1);
116 frame = frame->next_frame;
117 bp = (unsigned long) frame;
118 } else {
2c344e9d 119 ops->address(data, addr, 0);
878719e8 120 }
da01e18a 121 print_ftrace_graph_addr(addr, data, ops, task, graph);
878719e8
NH
122 }
123 stack++;
124 }
125 return bp;
126}
06d65bda
FW
127EXPORT_SYMBOL_GPL(print_context_stack);
128
129unsigned long
da01e18a 130print_context_stack_bp(struct task_struct *task,
06d65bda
FW
131 unsigned long *stack, unsigned long bp,
132 const struct stacktrace_ops *ops, void *data,
133 unsigned long *end, int *graph)
134{
135 struct stack_frame *frame = (struct stack_frame *)bp;
136 unsigned long *ret_addr = &frame->return_address;
137
da01e18a 138 while (valid_stack_ptr(task, ret_addr, sizeof(*ret_addr), end)) {
06d65bda
FW
139 unsigned long addr = *ret_addr;
140
c2c5d45d
FW
141 if (!__kernel_text_address(addr))
142 break;
143
568b329a
AS
144 if (ops->address(data, addr, 1))
145 break;
c2c5d45d
FW
146 frame = frame->next_frame;
147 ret_addr = &frame->return_address;
da01e18a 148 print_ftrace_graph_addr(addr, data, ops, task, graph);
06d65bda 149 }
c2c5d45d 150
06d65bda
FW
151 return (unsigned long)frame;
152}
153EXPORT_SYMBOL_GPL(print_context_stack_bp);
878719e8 154
878719e8
NH
155static int print_trace_stack(void *data, char *name)
156{
157 printk("%s <%s> ", (char *)data, name);
158 return 0;
159}
160
161/*
162 * Print one address/symbol entries per line.
163 */
568b329a 164static int print_trace_address(void *data, unsigned long addr, int reliable)
878719e8
NH
165{
166 touch_nmi_watchdog();
1fc7f61c 167 printk_stack_address(addr, reliable, data);
568b329a 168 return 0;
878719e8
NH
169}
170
171static const struct stacktrace_ops print_trace_ops = {
06d65bda
FW
172 .stack = print_trace_stack,
173 .address = print_trace_address,
61c1917f 174 .walk_stack = print_context_stack,
878719e8
NH
175};
176
177void
178show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
e8e999cf 179 unsigned long *stack, unsigned long bp, char *log_lvl)
878719e8
NH
180{
181 printk("%sCall Trace:\n", log_lvl);
e8e999cf 182 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
878719e8
NH
183}
184
878719e8
NH
185void show_stack(struct task_struct *task, unsigned long *sp)
186{
a77f2a4e
TH
187 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, "");
878719e8
NH
200}
201
81c2949f
BP
202void show_stack_regs(struct pt_regs *regs)
203{
204 show_stack_log_lvl(current, regs, (unsigned long *)regs->sp, regs->bp, "");
205}
206
edc35bd7 207static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
878719e8
NH
208static int die_owner = -1;
209static unsigned int die_nest_count;
210
9326638c 211unsigned long oops_begin(void)
878719e8
NH
212{
213 int cpu;
214 unsigned long flags;
215
216 oops_enter();
217
218 /* racy, but better than risking deadlock. */
219 raw_local_irq_save(flags);
220 cpu = smp_processor_id();
0199c4e6 221 if (!arch_spin_trylock(&die_lock)) {
878719e8
NH
222 if (cpu == die_owner)
223 /* nested oops. should stop eventually */;
224 else
0199c4e6 225 arch_spin_lock(&die_lock);
878719e8
NH
226 }
227 die_nest_count++;
228 die_owner = cpu;
229 console_verbose();
230 bust_spinlocks(1);
231 return flags;
232}
81e88fdc 233EXPORT_SYMBOL_GPL(oops_begin);
9326638c 234NOKPROBE_SYMBOL(oops_begin);
878719e8 235
2deb4be2
AL
236void __noreturn rewind_stack_do_exit(int signr);
237
9326638c 238void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
878719e8
NH
239{
240 if (regs && kexec_should_crash(current))
241 crash_kexec(regs);
242
243 bust_spinlocks(0);
244 die_owner = -1;
373d4d09 245 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
878719e8
NH
246 die_nest_count--;
247 if (!die_nest_count)
248 /* Nest count reaches zero, release the lock. */
0199c4e6 249 arch_spin_unlock(&die_lock);
878719e8
NH
250 raw_local_irq_restore(flags);
251 oops_exit();
252
253 if (!signr)
254 return;
255 if (in_interrupt())
256 panic("Fatal exception in interrupt");
257 if (panic_on_oops)
258 panic("Fatal exception");
2deb4be2
AL
259
260 /*
261 * We're not going to return, but we might be on an IST stack or
262 * have very little stack space left. Rewind the stack and kill
263 * the task.
264 */
265 rewind_stack_do_exit(signr);
878719e8 266}
9326638c 267NOKPROBE_SYMBOL(oops_end);
878719e8 268
9326638c 269int __die(const char *str, struct pt_regs *regs, long err)
878719e8
NH
270{
271#ifdef CONFIG_X86_32
272 unsigned short ss;
273 unsigned long sp;
274#endif
b0f4c4b3 275 printk(KERN_DEFAULT
8fad7ec5
RV
276 "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
277 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
278 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
279 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
280 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
281
878719e8 282 if (notify_die(DIE_OOPS, str, regs, err,
51e7dc70 283 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
878719e8
NH
284 return 1;
285
0fa0e2f0 286 print_modules();
57da8b96 287 show_regs(regs);
878719e8 288#ifdef CONFIG_X86_32
f39b6f0e 289 if (user_mode(regs)) {
878719e8
NH
290 sp = regs->sp;
291 ss = regs->ss & 0xffff;
a343c75d
PA
292 } else {
293 sp = kernel_stack_pointer(regs);
294 savesegment(ss, ss);
878719e8
NH
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 ");
5f01c988 302 printk_address(regs->ip);
878719e8
NH
303 printk(" RSP <%016lx>\n", regs->sp);
304#endif
305 return 0;
306}
9326638c 307NOKPROBE_SYMBOL(__die);
878719e8
NH
308
309/*
310 * This is gone through when something in the kernel has done something bad
311 * and is about to be terminated:
312 */
313void die(const char *str, struct pt_regs *regs, long err)
314{
315 unsigned long flags = oops_begin();
316 int sig = SIGSEGV;
317
f39b6f0e 318 if (!user_mode(regs))
878719e8
NH
319 report_bug(regs->ip, regs);
320
321 if (__die(str, regs, err))
322 sig = 0;
323 oops_end(flags, regs, sig);
324}
325
878719e8
NH
326static int __init kstack_setup(char *s)
327{
363f7ce3
SK
328 ssize_t ret;
329 unsigned long val;
330
878719e8
NH
331 if (!s)
332 return -EINVAL;
363f7ce3
SK
333
334 ret = kstrtoul(s, 0, &val);
335 if (ret)
336 return ret;
337 kstack_depth_to_print = val;
878719e8
NH
338 return 0;
339}
340early_param("kstack", kstack_setup);
341
342static int __init code_bytes_setup(char *s)
343{
363f7ce3
SK
344 ssize_t ret;
345 unsigned long val;
346
347 if (!s)
348 return -EINVAL;
349
350 ret = kstrtoul(s, 0, &val);
351 if (ret)
352 return ret;
353
354 code_bytes = val;
878719e8
NH
355 if (code_bytes > 8192)
356 code_bytes = 8192;
357
358 return 1;
359}
360__setup("code_bytes=", code_bytes_setup);