Merge branch 'x86/mm' into x86/asm, to unify the two branches for simplicity
[deliverable/linux.git] / arch / x86 / kernel / dumpstack.c
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>
13 #include <linux/ftrace.h>
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
21
22 int panic_on_unrecovered_nmi;
23 int panic_on_io_nmi;
24 unsigned int code_bytes = 64;
25 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
26 static int die_counter;
27
28 static void printk_stack_address(unsigned long address, int reliable,
29 void *data)
30 {
31 printk("%s [<%p>] %s%pB\n",
32 (char *)data, (void *)address, reliable ? "" : "? ",
33 (void *)address);
34 }
35
36 void printk_address(unsigned long address)
37 {
38 pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
39 }
40
41 /*
42 * x86-64 can have up to three kernel stacks:
43 * process stack
44 * interrupt stack
45 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
46 */
47
48 static inline int valid_stack_ptr(struct task_struct *task,
49 void *p, unsigned int size, void *end)
50 {
51 void *t = task_stack_page(task);
52 if (end) {
53 if (p < end && p >= (end-THREAD_SIZE))
54 return 1;
55 else
56 return 0;
57 }
58 return p >= t && p < t + THREAD_SIZE - size;
59 }
60
61 unsigned long
62 print_context_stack(struct task_struct *task,
63 unsigned long *stack, unsigned long bp,
64 const struct stacktrace_ops *ops, void *data,
65 unsigned long *end, int *graph)
66 {
67 struct stack_frame *frame = (struct stack_frame *)bp;
68
69 /*
70 * If we overflowed the stack into a guard page, jump back to the
71 * bottom of the usable stack.
72 */
73 if ((unsigned long)task_stack_page(task) - (unsigned long)stack <
74 PAGE_SIZE)
75 stack = (unsigned long *)task_stack_page(task);
76
77 while (valid_stack_ptr(task, stack, sizeof(*stack), end)) {
78 unsigned long addr = *stack;
79
80 if (__kernel_text_address(addr)) {
81 unsigned long real_addr;
82 int reliable = 0;
83
84 if ((unsigned long) stack == bp + sizeof(long)) {
85 reliable = 1;
86 frame = frame->next_frame;
87 bp = (unsigned long) frame;
88 }
89
90 /*
91 * When function graph tracing is enabled for a
92 * function, its return address on the stack is
93 * replaced with the address of an ftrace handler
94 * (return_to_handler). In that case, before printing
95 * the "real" address, we want to print the handler
96 * address as an "unreliable" hint that function graph
97 * tracing was involved.
98 */
99 real_addr = ftrace_graph_ret_addr(task, graph, addr,
100 stack);
101 if (real_addr != addr)
102 ops->address(data, addr, 0);
103
104 ops->address(data, real_addr, reliable);
105 }
106 stack++;
107 }
108 return bp;
109 }
110 EXPORT_SYMBOL_GPL(print_context_stack);
111
112 unsigned long
113 print_context_stack_bp(struct task_struct *task,
114 unsigned long *stack, unsigned long bp,
115 const struct stacktrace_ops *ops, void *data,
116 unsigned long *end, int *graph)
117 {
118 struct stack_frame *frame = (struct stack_frame *)bp;
119 unsigned long *retp = &frame->return_address;
120
121 while (valid_stack_ptr(task, retp, sizeof(*retp), end)) {
122 unsigned long addr = *retp;
123 unsigned long real_addr;
124
125 if (!__kernel_text_address(addr))
126 break;
127
128 real_addr = ftrace_graph_ret_addr(task, graph, addr, retp);
129 if (ops->address(data, real_addr, 1))
130 break;
131
132 frame = frame->next_frame;
133 retp = &frame->return_address;
134 }
135
136 return (unsigned long)frame;
137 }
138 EXPORT_SYMBOL_GPL(print_context_stack_bp);
139
140 static int print_trace_stack(void *data, char *name)
141 {
142 printk("%s <%s> ", (char *)data, name);
143 return 0;
144 }
145
146 /*
147 * Print one address/symbol entries per line.
148 */
149 static int print_trace_address(void *data, unsigned long addr, int reliable)
150 {
151 touch_nmi_watchdog();
152 printk_stack_address(addr, reliable, data);
153 return 0;
154 }
155
156 static const struct stacktrace_ops print_trace_ops = {
157 .stack = print_trace_stack,
158 .address = print_trace_address,
159 .walk_stack = print_context_stack,
160 };
161
162 void
163 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
164 unsigned long *stack, unsigned long bp, char *log_lvl)
165 {
166 printk("%sCall Trace:\n", log_lvl);
167 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
168 }
169
170 void show_stack(struct task_struct *task, unsigned long *sp)
171 {
172 unsigned long bp = 0;
173 unsigned long stack;
174
175 /*
176 * Stack frames below this one aren't interesting. Don't show them
177 * if we're printing for %current.
178 */
179 if (!sp && (!task || task == current)) {
180 sp = &stack;
181 bp = stack_frame(current, NULL);
182 }
183
184 show_stack_log_lvl(task, NULL, sp, bp, "");
185 }
186
187 void show_stack_regs(struct pt_regs *regs)
188 {
189 show_stack_log_lvl(current, regs, (unsigned long *)regs->sp, regs->bp, "");
190 }
191
192 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
193 static int die_owner = -1;
194 static unsigned int die_nest_count;
195
196 unsigned long oops_begin(void)
197 {
198 int cpu;
199 unsigned long flags;
200
201 oops_enter();
202
203 /* racy, but better than risking deadlock. */
204 raw_local_irq_save(flags);
205 cpu = smp_processor_id();
206 if (!arch_spin_trylock(&die_lock)) {
207 if (cpu == die_owner)
208 /* nested oops. should stop eventually */;
209 else
210 arch_spin_lock(&die_lock);
211 }
212 die_nest_count++;
213 die_owner = cpu;
214 console_verbose();
215 bust_spinlocks(1);
216 return flags;
217 }
218 EXPORT_SYMBOL_GPL(oops_begin);
219 NOKPROBE_SYMBOL(oops_begin);
220
221 void __noreturn rewind_stack_do_exit(int signr);
222
223 void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
224 {
225 if (regs && kexec_should_crash(current))
226 crash_kexec(regs);
227
228 bust_spinlocks(0);
229 die_owner = -1;
230 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
231 die_nest_count--;
232 if (!die_nest_count)
233 /* Nest count reaches zero, release the lock. */
234 arch_spin_unlock(&die_lock);
235 raw_local_irq_restore(flags);
236 oops_exit();
237
238 if (!signr)
239 return;
240 if (in_interrupt())
241 panic("Fatal exception in interrupt");
242 if (panic_on_oops)
243 panic("Fatal exception");
244
245 /*
246 * We're not going to return, but we might be on an IST stack or
247 * have very little stack space left. Rewind the stack and kill
248 * the task.
249 */
250 rewind_stack_do_exit(signr);
251 }
252 NOKPROBE_SYMBOL(oops_end);
253
254 int __die(const char *str, struct pt_regs *regs, long err)
255 {
256 #ifdef CONFIG_X86_32
257 unsigned short ss;
258 unsigned long sp;
259 #endif
260 printk(KERN_DEFAULT
261 "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
262 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
263 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
264 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
265 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
266
267 if (notify_die(DIE_OOPS, str, regs, err,
268 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
269 return 1;
270
271 print_modules();
272 show_regs(regs);
273 #ifdef CONFIG_X86_32
274 if (user_mode(regs)) {
275 sp = regs->sp;
276 ss = regs->ss & 0xffff;
277 } else {
278 sp = kernel_stack_pointer(regs);
279 savesegment(ss, ss);
280 }
281 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
282 print_symbol("%s", regs->ip);
283 printk(" SS:ESP %04x:%08lx\n", ss, sp);
284 #else
285 /* Executive summary in case the oops scrolled away */
286 printk(KERN_ALERT "RIP ");
287 printk_address(regs->ip);
288 printk(" RSP <%016lx>\n", regs->sp);
289 #endif
290 return 0;
291 }
292 NOKPROBE_SYMBOL(__die);
293
294 /*
295 * This is gone through when something in the kernel has done something bad
296 * and is about to be terminated:
297 */
298 void die(const char *str, struct pt_regs *regs, long err)
299 {
300 unsigned long flags = oops_begin();
301 int sig = SIGSEGV;
302
303 if (!user_mode(regs))
304 report_bug(regs->ip, regs);
305
306 if (__die(str, regs, err))
307 sig = 0;
308 oops_end(flags, regs, sig);
309 }
310
311 static int __init kstack_setup(char *s)
312 {
313 ssize_t ret;
314 unsigned long val;
315
316 if (!s)
317 return -EINVAL;
318
319 ret = kstrtoul(s, 0, &val);
320 if (ret)
321 return ret;
322 kstack_depth_to_print = val;
323 return 0;
324 }
325 early_param("kstack", kstack_setup);
326
327 static int __init code_bytes_setup(char *s)
328 {
329 ssize_t ret;
330 unsigned long val;
331
332 if (!s)
333 return -EINVAL;
334
335 ret = kstrtoul(s, 0, &val);
336 if (ret)
337 return ret;
338
339 code_bytes = val;
340 if (code_bytes > 8192)
341 code_bytes = 8192;
342
343 return 1;
344 }
345 __setup("code_bytes=", code_bytes_setup);
This page took 0.050423 seconds and 6 git commands to generate.