blob: 6d6f46837eeab1f7cf6982071447f290cd21b7ae [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,
Josh Poimboeufd438f5f2016-08-24 11:50:16 -050029 char *log_lvl)
Neil Horman878719e2008-10-23 10:40:06 -040030{
Josh Poimboeufd438f5f2016-08-24 11:50:16 -050031 touch_nmi_watchdog();
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +010032 printk("%s [<%p>] %s%pB\n",
Josh Poimboeufd438f5f2016-08-24 11:50:16 -050033 log_lvl, (void *)address, reliable ? "" : "? ",
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +010034 (void *)address);
Neil Horman878719e2008-10-23 10:40:06 -040035}
36
Jiri Slaby5f01c982013-10-25 15:06:58 +020037void printk_address(unsigned long address)
38{
39 pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
40}
41
Neil Horman878719e2008-10-23 10:40:06 -040042/*
43 * x86-64 can have up to three kernel stacks:
44 * process stack
45 * interrupt stack
46 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
47 */
48
Linus Torvaldsda01e182016-06-23 12:20:01 -070049static inline int valid_stack_ptr(struct task_struct *task,
Neil Horman878719e2008-10-23 10:40:06 -040050 void *p, unsigned int size, void *end)
51{
Linus Torvaldsaca9c292016-06-24 16:55:53 -070052 void *t = task_stack_page(task);
Neil Horman878719e2008-10-23 10:40:06 -040053 if (end) {
54 if (p < end && p >= (end-THREAD_SIZE))
55 return 1;
56 else
57 return 0;
58 }
Andy Lutomirski9a2e9da2016-07-14 13:22:52 -070059 return p >= t && p < t + THREAD_SIZE - size;
Neil Horman878719e2008-10-23 10:40:06 -040060}
61
62unsigned long
Linus Torvaldsda01e182016-06-23 12:20:01 -070063print_context_stack(struct task_struct *task,
Neil Horman878719e2008-10-23 10:40:06 -040064 unsigned long *stack, unsigned long bp,
65 const struct stacktrace_ops *ops, void *data,
Steven Rostedt7ee991f2008-12-02 23:50:04 -050066 unsigned long *end, int *graph)
Neil Horman878719e2008-10-23 10:40:06 -040067{
68 struct stack_frame *frame = (struct stack_frame *)bp;
69
Andy Lutomirski9a2e9da2016-07-14 13:22:52 -070070 /*
71 * If we overflowed the stack into a guard page, jump back to the
72 * bottom of the usable stack.
73 */
74 if ((unsigned long)task_stack_page(task) - (unsigned long)stack <
75 PAGE_SIZE)
76 stack = (unsigned long *)task_stack_page(task);
77
Linus Torvaldsda01e182016-06-23 12:20:01 -070078 while (valid_stack_ptr(task, stack, sizeof(*stack), end)) {
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -050079 unsigned long addr = *stack;
Neil Horman878719e2008-10-23 10:40:06 -040080
Neil Horman878719e2008-10-23 10:40:06 -040081 if (__kernel_text_address(addr)) {
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -050082 unsigned long real_addr;
83 int reliable = 0;
84
Neil Horman878719e2008-10-23 10:40:06 -040085 if ((unsigned long) stack == bp + sizeof(long)) {
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -050086 reliable = 1;
Neil Horman878719e2008-10-23 10:40:06 -040087 frame = frame->next_frame;
88 bp = (unsigned long) frame;
Neil Horman878719e2008-10-23 10:40:06 -040089 }
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -050090
Josh Poimboeuf6f727b82016-08-19 06:53:01 -050091 /*
92 * When function graph tracing is enabled for a
93 * function, its return address on the stack is
94 * replaced with the address of an ftrace handler
95 * (return_to_handler). In that case, before printing
96 * the "real" address, we want to print the handler
97 * address as an "unreliable" hint that function graph
98 * tracing was involved.
99 */
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -0500100 real_addr = ftrace_graph_ret_addr(task, graph, addr,
101 stack);
102 if (real_addr != addr)
Josh Poimboeuf6f727b82016-08-19 06:53:01 -0500103 ops->address(data, addr, 0);
104
105 ops->address(data, real_addr, reliable);
Neil Horman878719e2008-10-23 10:40:06 -0400106 }
107 stack++;
108 }
109 return bp;
110}
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100111EXPORT_SYMBOL_GPL(print_context_stack);
112
113unsigned long
Linus Torvaldsda01e182016-06-23 12:20:01 -0700114print_context_stack_bp(struct task_struct *task,
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100115 unsigned long *stack, unsigned long bp,
116 const struct stacktrace_ops *ops, void *data,
117 unsigned long *end, int *graph)
118{
119 struct stack_frame *frame = (struct stack_frame *)bp;
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -0500120 unsigned long *retp = &frame->return_address;
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100121
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -0500122 while (valid_stack_ptr(task, retp, sizeof(*retp), end)) {
123 unsigned long addr = *retp;
124 unsigned long real_addr;
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100125
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100126 if (!__kernel_text_address(addr))
127 break;
128
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -0500129 real_addr = ftrace_graph_ret_addr(task, graph, addr, retp);
Josh Poimboeuf6f727b82016-08-19 06:53:01 -0500130 if (ops->address(data, real_addr, 1))
131 break;
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -0500132
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100133 frame = frame->next_frame;
Josh Poimboeuf408fe5d2016-08-19 06:52:59 -0500134 retp = &frame->return_address;
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100135 }
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100136
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100137 return (unsigned long)frame;
138}
139EXPORT_SYMBOL_GPL(print_context_stack_bp);
Neil Horman878719e2008-10-23 10:40:06 -0400140
Neil Horman878719e2008-10-23 10:40:06 -0400141static int print_trace_stack(void *data, char *name)
142{
143 printk("%s <%s> ", (char *)data, name);
144 return 0;
145}
146
147/*
148 * Print one address/symbol entries per line.
149 */
Alexei Starovoitov568b3292016-02-17 19:58:57 -0800150static int print_trace_address(void *data, unsigned long addr, int reliable)
Neil Horman878719e2008-10-23 10:40:06 -0400151{
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +0100152 printk_stack_address(addr, reliable, data);
Alexei Starovoitov568b3292016-02-17 19:58:57 -0800153 return 0;
Neil Horman878719e2008-10-23 10:40:06 -0400154}
155
156static const struct stacktrace_ops print_trace_ops = {
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100157 .stack = print_trace_stack,
158 .address = print_trace_address,
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100159 .walk_stack = print_context_stack,
Neil Horman878719e2008-10-23 10:40:06 -0400160};
161
162void
163show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900164 unsigned long *stack, unsigned long bp, char *log_lvl)
Neil Horman878719e2008-10-23 10:40:06 -0400165{
166 printk("%sCall Trace:\n", log_lvl);
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900167 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
Neil Horman878719e2008-10-23 10:40:06 -0400168}
169
Neil Horman878719e2008-10-23 10:40:06 -0400170void show_stack(struct task_struct *task, unsigned long *sp)
171{
Tejun Heoa77f2a42013-04-30 15:27:09 -0700172 unsigned long bp = 0;
Tejun Heoa77f2a42013-04-30 15:27:09 -0700173
174 /*
175 * Stack frames below this one aren't interesting. Don't show them
176 * if we're printing for %current.
177 */
178 if (!sp && (!task || task == current)) {
Josh Poimboeuf4b8afaf2016-08-24 11:50:17 -0500179 sp = get_stack_pointer(current, NULL);
180 bp = (unsigned long)get_frame_pointer(current, NULL);
Tejun Heoa77f2a42013-04-30 15:27:09 -0700181 }
182
183 show_stack_log_lvl(task, NULL, sp, bp, "");
Neil Horman878719e2008-10-23 10:40:06 -0400184}
185
Borislav Petkov81c29492016-07-05 00:31:27 +0200186void show_stack_regs(struct pt_regs *regs)
187{
188 show_stack_log_lvl(current, regs, (unsigned long *)regs->sp, regs->bp, "");
189}
190
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100191static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Neil Horman878719e2008-10-23 10:40:06 -0400192static int die_owner = -1;
193static unsigned int die_nest_count;
194
Masami Hiramatsu93266382014-04-17 17:18:14 +0900195unsigned long oops_begin(void)
Neil Horman878719e2008-10-23 10:40:06 -0400196{
197 int cpu;
198 unsigned long flags;
199
Neil Horman878719e2008-10-23 10:40:06 -0400200 oops_enter();
201
202 /* racy, but better than risking deadlock. */
203 raw_local_irq_save(flags);
204 cpu = smp_processor_id();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100205 if (!arch_spin_trylock(&die_lock)) {
Neil Horman878719e2008-10-23 10:40:06 -0400206 if (cpu == die_owner)
207 /* nested oops. should stop eventually */;
208 else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100209 arch_spin_lock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400210 }
211 die_nest_count++;
212 die_owner = cpu;
213 console_verbose();
214 bust_spinlocks(1);
215 return flags;
216}
Huang Ying81e88fd2011-01-12 14:44:55 +0800217EXPORT_SYMBOL_GPL(oops_begin);
Masami Hiramatsu93266382014-04-17 17:18:14 +0900218NOKPROBE_SYMBOL(oops_begin);
Neil Horman878719e2008-10-23 10:40:06 -0400219
Andy Lutomirski2deb4be2016-07-14 13:22:55 -0700220void __noreturn rewind_stack_do_exit(int signr);
221
Masami Hiramatsu93266382014-04-17 17:18:14 +0900222void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Neil Horman878719e2008-10-23 10:40:06 -0400223{
224 if (regs && kexec_should_crash(current))
225 crash_kexec(regs);
226
227 bust_spinlocks(0);
228 die_owner = -1;
Rusty Russell373d4d02013-01-21 17:17:39 +1030229 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Neil Horman878719e2008-10-23 10:40:06 -0400230 die_nest_count--;
231 if (!die_nest_count)
232 /* Nest count reaches zero, release the lock. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100233 arch_spin_unlock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400234 raw_local_irq_restore(flags);
235 oops_exit();
236
237 if (!signr)
238 return;
239 if (in_interrupt())
240 panic("Fatal exception in interrupt");
241 if (panic_on_oops)
242 panic("Fatal exception");
Andy Lutomirski2deb4be2016-07-14 13:22:55 -0700243
244 /*
245 * We're not going to return, but we might be on an IST stack or
246 * have very little stack space left. Rewind the stack and kill
247 * the task.
248 */
249 rewind_stack_do_exit(signr);
Neil Horman878719e2008-10-23 10:40:06 -0400250}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900251NOKPROBE_SYMBOL(oops_end);
Neil Horman878719e2008-10-23 10:40:06 -0400252
Masami Hiramatsu93266382014-04-17 17:18:14 +0900253int __die(const char *str, struct pt_regs *regs, long err)
Neil Horman878719e2008-10-23 10:40:06 -0400254{
255#ifdef CONFIG_X86_32
256 unsigned short ss;
257 unsigned long sp;
258#endif
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500259 printk(KERN_DEFAULT
Rasmus Villemoes8fad7ec2016-03-26 21:40:16 +0100260 "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
261 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
262 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
263 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
264 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
265
Neil Horman878719e2008-10-23 10:40:06 -0400266 if (notify_die(DIE_OOPS, str, regs, err,
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530267 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
Neil Horman878719e2008-10-23 10:40:06 -0400268 return 1;
269
Jan Beulich0fa0e2f2012-06-18 11:40:04 +0100270 print_modules();
Jan Beulich57da8b92012-05-09 08:47:37 +0100271 show_regs(regs);
Neil Horman878719e2008-10-23 10:40:06 -0400272#ifdef CONFIG_X86_32
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700273 if (user_mode(regs)) {
Neil Horman878719e2008-10-23 10:40:06 -0400274 sp = regs->sp;
275 ss = regs->ss & 0xffff;
H. Peter Anvina343c752009-10-12 14:11:09 -0700276 } else {
277 sp = kernel_stack_pointer(regs);
278 savesegment(ss, ss);
Neil Horman878719e2008-10-23 10:40:06 -0400279 }
280 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
281 print_symbol("%s", regs->ip);
282 printk(" SS:ESP %04x:%08lx\n", ss, sp);
283#else
284 /* Executive summary in case the oops scrolled away */
285 printk(KERN_ALERT "RIP ");
Jiri Slaby5f01c982013-10-25 15:06:58 +0200286 printk_address(regs->ip);
Neil Horman878719e2008-10-23 10:40:06 -0400287 printk(" RSP <%016lx>\n", regs->sp);
288#endif
289 return 0;
290}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900291NOKPROBE_SYMBOL(__die);
Neil Horman878719e2008-10-23 10:40:06 -0400292
293/*
294 * This is gone through when something in the kernel has done something bad
295 * and is about to be terminated:
296 */
297void die(const char *str, struct pt_regs *regs, long err)
298{
299 unsigned long flags = oops_begin();
300 int sig = SIGSEGV;
301
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700302 if (!user_mode(regs))
Neil Horman878719e2008-10-23 10:40:06 -0400303 report_bug(regs->ip, regs);
304
305 if (__die(str, regs, err))
306 sig = 0;
307 oops_end(flags, regs, sig);
308}
309
Neil Horman878719e2008-10-23 10:40:06 -0400310static int __init kstack_setup(char *s)
311{
Shuah Khan363f7ce2012-05-06 11:58:04 -0600312 ssize_t ret;
313 unsigned long val;
314
Neil Horman878719e2008-10-23 10:40:06 -0400315 if (!s)
316 return -EINVAL;
Shuah Khan363f7ce2012-05-06 11:58:04 -0600317
318 ret = kstrtoul(s, 0, &val);
319 if (ret)
320 return ret;
321 kstack_depth_to_print = val;
Neil Horman878719e2008-10-23 10:40:06 -0400322 return 0;
323}
324early_param("kstack", kstack_setup);
325
326static int __init code_bytes_setup(char *s)
327{
Shuah Khan363f7ce2012-05-06 11:58:04 -0600328 ssize_t ret;
329 unsigned long val;
330
331 if (!s)
332 return -EINVAL;
333
334 ret = kstrtoul(s, 0, &val);
335 if (ret)
336 return ret;
337
338 code_bytes = val;
Neil Horman878719e2008-10-23 10:40:06 -0400339 if (code_bytes > 8192)
340 code_bytes = 8192;
341
342 return 1;
343}
344__setup("code_bytes=", code_bytes_setup);