x86/dumpstack: When OOPSing, rewind the stack before do_exit()
[deliverable/linux.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
185void show_trace(struct task_struct *task, struct pt_regs *regs,
e8e999cf 186 unsigned long *stack, unsigned long bp)
878719e8 187{
e8e999cf 188 show_trace_log_lvl(task, regs, stack, bp, "");
878719e8
NH
189}
190
191void show_stack(struct task_struct *task, unsigned long *sp)
192{
a77f2a4e
TH
193 unsigned long bp = 0;
194 unsigned long stack;
195
196 /*
197 * Stack frames below this one aren't interesting. Don't show them
198 * if we're printing for %current.
199 */
200 if (!sp && (!task || task == current)) {
201 sp = &stack;
202 bp = stack_frame(current, NULL);
203 }
204
205 show_stack_log_lvl(task, NULL, sp, bp, "");
878719e8
NH
206}
207
edc35bd7 208static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
878719e8
NH
209static int die_owner = -1;
210static unsigned int die_nest_count;
211
9326638c 212unsigned long oops_begin(void)
878719e8
NH
213{
214 int cpu;
215 unsigned long flags;
216
217 oops_enter();
218
219 /* racy, but better than risking deadlock. */
220 raw_local_irq_save(flags);
221 cpu = smp_processor_id();
0199c4e6 222 if (!arch_spin_trylock(&die_lock)) {
878719e8
NH
223 if (cpu == die_owner)
224 /* nested oops. should stop eventually */;
225 else
0199c4e6 226 arch_spin_lock(&die_lock);
878719e8
NH
227 }
228 die_nest_count++;
229 die_owner = cpu;
230 console_verbose();
231 bust_spinlocks(1);
232 return flags;
233}
81e88fdc 234EXPORT_SYMBOL_GPL(oops_begin);
9326638c 235NOKPROBE_SYMBOL(oops_begin);
878719e8 236
2deb4be2
AL
237void __noreturn rewind_stack_do_exit(int signr);
238
9326638c 239void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
878719e8
NH
240{
241 if (regs && kexec_should_crash(current))
242 crash_kexec(regs);
243
244 bust_spinlocks(0);
245 die_owner = -1;
373d4d09 246 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
878719e8
NH
247 die_nest_count--;
248 if (!die_nest_count)
249 /* Nest count reaches zero, release the lock. */
0199c4e6 250 arch_spin_unlock(&die_lock);
878719e8
NH
251 raw_local_irq_restore(flags);
252 oops_exit();
253
254 if (!signr)
255 return;
256 if (in_interrupt())
257 panic("Fatal exception in interrupt");
258 if (panic_on_oops)
259 panic("Fatal exception");
2deb4be2
AL
260
261 /*
262 * We're not going to return, but we might be on an IST stack or
263 * have very little stack space left. Rewind the stack and kill
264 * the task.
265 */
266 rewind_stack_do_exit(signr);
878719e8 267}
9326638c 268NOKPROBE_SYMBOL(oops_end);
878719e8 269
9326638c 270int __die(const char *str, struct pt_regs *regs, long err)
878719e8
NH
271{
272#ifdef CONFIG_X86_32
273 unsigned short ss;
274 unsigned long sp;
275#endif
b0f4c4b3 276 printk(KERN_DEFAULT
8fad7ec5
RV
277 "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
278 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
279 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
280 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
281 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
282
878719e8 283 if (notify_die(DIE_OOPS, str, regs, err,
51e7dc70 284 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
878719e8
NH
285 return 1;
286
0fa0e2f0 287 print_modules();
57da8b96 288 show_regs(regs);
878719e8 289#ifdef CONFIG_X86_32
f39b6f0e 290 if (user_mode(regs)) {
878719e8
NH
291 sp = regs->sp;
292 ss = regs->ss & 0xffff;
a343c75d
PA
293 } else {
294 sp = kernel_stack_pointer(regs);
295 savesegment(ss, ss);
878719e8
NH
296 }
297 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
298 print_symbol("%s", regs->ip);
299 printk(" SS:ESP %04x:%08lx\n", ss, sp);
300#else
301 /* Executive summary in case the oops scrolled away */
302 printk(KERN_ALERT "RIP ");
5f01c988 303 printk_address(regs->ip);
878719e8
NH
304 printk(" RSP <%016lx>\n", regs->sp);
305#endif
306 return 0;
307}
9326638c 308NOKPROBE_SYMBOL(__die);
878719e8
NH
309
310/*
311 * This is gone through when something in the kernel has done something bad
312 * and is about to be terminated:
313 */
314void die(const char *str, struct pt_regs *regs, long err)
315{
316 unsigned long flags = oops_begin();
317 int sig = SIGSEGV;
318
f39b6f0e 319 if (!user_mode(regs))
878719e8
NH
320 report_bug(regs->ip, regs);
321
322 if (__die(str, regs, err))
323 sig = 0;
324 oops_end(flags, regs, sig);
325}
326
878719e8
NH
327static int __init kstack_setup(char *s)
328{
363f7ce3
SK
329 ssize_t ret;
330 unsigned long val;
331
878719e8
NH
332 if (!s)
333 return -EINVAL;
363f7ce3
SK
334
335 ret = kstrtoul(s, 0, &val);
336 if (ret)
337 return ret;
338 kstack_depth_to_print = val;
878719e8
NH
339 return 0;
340}
341early_param("kstack", kstack_setup);
342
343static int __init code_bytes_setup(char *s)
344{
363f7ce3
SK
345 ssize_t ret;
346 unsigned long val;
347
348 if (!s)
349 return -EINVAL;
350
351 ret = kstrtoul(s, 0, &val);
352 if (ret)
353 return ret;
354
355 code_bytes = val;
878719e8
NH
356 if (code_bytes > 8192)
357 code_bytes = 8192;
358
359 return 1;
360}
361__setup("code_bytes=", code_bytes_setup);
This page took 0.415163 seconds and 5 git commands to generate.