blob: ee344030fd0ae11ce2a644c479f871d970b69c86 [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010013#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010014#include <linux/sched/task_stack.h>
Steven Rostedt712406a2009-02-09 10:54:03 -080015#include <linux/ftrace.h>
Neil Horman878719e2008-10-23 10:40:06 -040016#include <linux/kexec.h>
17#include <linux/bug.h>
18#include <linux/nmi.h>
19#include <linux/sysfs.h>
20
Thomas Gleixner92a0f812017-12-20 18:51:31 +010021#include <asm/cpu_entry_area.h>
Neil Horman878719e2008-10-23 10:40:06 -040022#include <asm/stacktrace.h>
Josh Poimboeufe18bccc2016-09-16 14:18:16 -050023#include <asm/unwind.h>
Neil Horman878719e2008-10-23 10:40:06 -040024
Borislav Petkov5d12f042018-04-17 18:11:16 +020025#define OPCODE_BUFSIZE 64
26
Neil Horman878719e2008-10-23 10:40:06 -040027int panic_on_unrecovered_nmi;
Kurt Garloff5211a242009-06-24 14:32:11 -070028int panic_on_io_nmi;
Neil Horman878719e2008-10-23 10:40:06 -040029static int die_counter;
30
Borislav Petkov602bd702018-04-17 18:11:23 +020031static struct pt_regs exec_summary_regs;
32
Josh Poimboeufcb76c932016-09-14 21:07:42 -050033bool in_task_stack(unsigned long *stack, struct task_struct *task,
34 struct stack_info *info)
35{
36 unsigned long *begin = task_stack_page(task);
37 unsigned long *end = task_stack_page(task) + THREAD_SIZE;
38
39 if (stack < begin || stack >= end)
40 return false;
41
42 info->type = STACK_TYPE_TASK;
43 info->begin = begin;
44 info->end = end;
45 info->next_sp = NULL;
46
47 return true;
48}
49
Dave Hansen4fe2d8b2017-12-04 17:25:07 -080050bool in_entry_stack(unsigned long *stack, struct stack_info *info)
Andy Lutomirski33a2f1a2017-12-04 15:07:13 +010051{
Dave Hansen4fe2d8b2017-12-04 17:25:07 -080052 struct entry_stack *ss = cpu_entry_stack(smp_processor_id());
Andy Lutomirski33a2f1a2017-12-04 15:07:13 +010053
Andy Lutomirski0f9a4812017-12-04 15:07:28 +010054 void *begin = ss;
55 void *end = ss + 1;
Andy Lutomirski33a2f1a2017-12-04 15:07:13 +010056
57 if ((void *)stack < begin || (void *)stack >= end)
58 return false;
59
Dave Hansen4fe2d8b2017-12-04 17:25:07 -080060 info->type = STACK_TYPE_ENTRY;
Andy Lutomirski33a2f1a2017-12-04 15:07:13 +010061 info->begin = begin;
62 info->end = end;
63 info->next_sp = NULL;
64
65 return true;
66}
67
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +010068static void printk_stack_address(unsigned long address, int reliable,
Josh Poimboeufd438f5f2016-08-24 11:50:16 -050069 char *log_lvl)
Neil Horman878719e2008-10-23 10:40:06 -040070{
Josh Poimboeufd438f5f2016-08-24 11:50:16 -050071 touch_nmi_watchdog();
Josh Poimboeufbb5e5ce2016-10-25 09:51:12 -050072 printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address);
Jiri Slaby5f01c982013-10-25 15:06:58 +020073}
74
Borislav Petkove8b6f982018-04-17 18:11:20 +020075void show_opcodes(u8 *rip, const char *loglvl)
Borislav Petkovf0a1d7c2018-04-17 18:11:18 +020076{
Borislav Petkov9e4a90f2018-04-17 18:11:19 +020077 unsigned int code_prologue = OPCODE_BUFSIZE * 2 / 3;
78 u8 opcodes[OPCODE_BUFSIZE];
Borislav Petkovf0a1d7c2018-04-17 18:11:18 +020079 u8 *ip;
80 int i;
81
Borislav Petkove8b6f982018-04-17 18:11:20 +020082 printk("%sCode: ", loglvl);
Borislav Petkovf0a1d7c2018-04-17 18:11:18 +020083
84 ip = (u8 *)rip - code_prologue;
Borislav Petkov9e4a90f2018-04-17 18:11:19 +020085 if (probe_kernel_read(opcodes, ip, OPCODE_BUFSIZE)) {
86 pr_cont("Bad RIP value.\n");
87 return;
Borislav Petkovf0a1d7c2018-04-17 18:11:18 +020088 }
Borislav Petkov9e4a90f2018-04-17 18:11:19 +020089
90 for (i = 0; i < OPCODE_BUFSIZE; i++, ip++) {
91 if (ip == rip)
92 pr_cont("<%02x> ", opcodes[i]);
Borislav Petkovf0a1d7c2018-04-17 18:11:18 +020093 else
Borislav Petkov9e4a90f2018-04-17 18:11:19 +020094 pr_cont("%02x ", opcodes[i]);
Borislav Petkovf0a1d7c2018-04-17 18:11:18 +020095 }
96 pr_cont("\n");
97}
98
Borislav Petkov7cccf072018-04-17 18:11:22 +020099void show_ip(struct pt_regs *regs, const char *loglvl)
100{
101#ifdef CONFIG_X86_32
102 printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
103#else
104 printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
105#endif
106 show_opcodes((u8 *)regs->ip, loglvl);
107}
108
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100109void show_iret_regs(struct pt_regs *regs)
110{
Borislav Petkov7cccf072018-04-17 18:11:22 +0200111 show_ip(regs, KERN_DEFAULT);
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100112 printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss,
113 regs->sp, regs->flags);
114}
115
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600116static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs,
117 bool partial)
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100118{
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600119 /*
120 * These on_stack() checks aren't strictly necessary: the unwind code
121 * has already validated the 'regs' pointer. The checks are done for
122 * ordering reasons: if the registers are on the next stack, we don't
123 * want to print them out yet. Otherwise they'll be shown as part of
124 * the wrong stack. Later, when show_trace_log_lvl() switches to the
125 * next stack, this function will be called again with the same regs so
126 * they can be printed in the right context.
127 */
128 if (!partial && on_stack(info, regs, sizeof(*regs))) {
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100129 __show_regs(regs, 0);
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600130
131 } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
132 IRET_FRAME_SIZE)) {
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100133 /*
134 * When an interrupt or exception occurs in entry code, the
135 * full pt_regs might not have been saved yet. In that case
136 * just print the iret frame.
137 */
138 show_iret_regs(regs);
139 }
140}
141
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500142void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
143 unsigned long *stack, char *log_lvl)
Neil Horman878719e2008-10-23 10:40:06 -0400144{
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500145 struct unwind_state state;
146 struct stack_info stack_info = {0};
147 unsigned long visit_mask = 0;
148 int graph_idx = 0;
Arnd Bergmannebfc1502018-02-02 15:56:17 +0100149 bool partial = false;
Neil Horman878719e2008-10-23 10:40:06 -0400150
Neil Horman878719e2008-10-23 10:40:06 -0400151 printk("%sCall Trace:\n", log_lvl);
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500152
153 unwind_start(&state, task, regs, stack);
Josh Poimboeuff4474c92016-11-17 00:04:58 -0600154 stack = stack ? : get_stack_pointer(task, regs);
Josh Poimboeuf3ffdeb12017-12-31 10:18:07 -0600155 regs = unwind_get_entry_regs(&state, &partial);
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500156
157 /*
158 * Iterate through the stacks, starting with the current stack pointer.
159 * Each stack has a pointer to the next one.
160 *
161 * x86-64 can have several stacks:
162 * - task stack
163 * - interrupt stack
164 * - HW exception stacks (double fault, nmi, debug, mce)
Dave Hansen4fe2d8b2017-12-04 17:25:07 -0800165 * - entry stack
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500166 *
Andy Lutomirski6e60e582017-12-04 15:07:18 +0100167 * x86-32 can have up to four stacks:
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500168 * - task stack
169 * - softirq stack
170 * - hardirq stack
Dave Hansen4fe2d8b2017-12-04 17:25:07 -0800171 * - entry stack
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500172 */
Josh Poimboeuf3ffdeb12017-12-31 10:18:07 -0600173 for ( ; stack; stack = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
Josh Poimboeuf3d02a9c2016-11-18 11:46:23 -0600174 const char *stack_name;
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500175
Andy Lutomirski6e60e582017-12-04 15:07:18 +0100176 if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
177 /*
178 * We weren't on a valid stack. It's possible that
179 * we overflowed a valid stack into a guard page.
180 * See if the next page up is valid so that we can
181 * generate some kind of backtrace if this happens.
182 */
183 stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack);
184 if (get_stack_info(stack, task, &stack_info, &visit_mask))
185 break;
186 }
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500187
Josh Poimboeuf3d02a9c2016-11-18 11:46:23 -0600188 stack_name = stack_type_name(stack_info.type);
189 if (stack_name)
190 printk("%s <%s>\n", log_lvl, stack_name);
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500191
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100192 if (regs)
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600193 show_regs_if_on_stack(&stack_info, regs, partial);
Josh Poimboeufb0529be2017-07-11 10:33:40 -0500194
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500195 /*
196 * Scan the stack, printing any text addresses we find. At the
197 * same time, follow proper stack frames with the unwinder.
198 *
199 * Addresses found during the scan which are not reported by
200 * the unwinder are considered to be additional clues which are
201 * sometimes useful for debugging and are prefixed with '?'.
202 * This also serves as a failsafe option in case the unwinder
203 * goes off in the weeds.
204 */
205 for (; stack < stack_info.end; stack++) {
206 unsigned long real_addr;
207 int reliable = 0;
Josh Poimboeuf91e08ab2016-11-17 09:57:24 -0600208 unsigned long addr = READ_ONCE_NOCHECK(*stack);
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500209 unsigned long *ret_addr_p =
210 unwind_get_return_address_ptr(&state);
211
212 if (!__kernel_text_address(addr))
213 continue;
214
Josh Poimboeuf3b3fa112016-10-20 11:34:43 -0500215 /*
216 * Don't print regs->ip again if it was already printed
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600217 * by show_regs_if_on_stack().
Josh Poimboeuf3b3fa112016-10-20 11:34:43 -0500218 */
Josh Poimboeufb0529be2017-07-11 10:33:40 -0500219 if (regs && stack == &regs->ip)
220 goto next;
Josh Poimboeuf3b3fa112016-10-20 11:34:43 -0500221
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500222 if (stack == ret_addr_p)
223 reliable = 1;
224
225 /*
226 * When function graph tracing is enabled for a
227 * function, its return address on the stack is
228 * replaced with the address of an ftrace handler
229 * (return_to_handler). In that case, before printing
230 * the "real" address, we want to print the handler
231 * address as an "unreliable" hint that function graph
232 * tracing was involved.
233 */
234 real_addr = ftrace_graph_ret_addr(task, &graph_idx,
235 addr, stack);
236 if (real_addr != addr)
237 printk_stack_address(addr, 0, log_lvl);
238 printk_stack_address(real_addr, reliable, log_lvl);
239
240 if (!reliable)
241 continue;
242
Josh Poimboeufb0529be2017-07-11 10:33:40 -0500243next:
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500244 /*
245 * Get the next frame from the unwinder. No need to
246 * check for an error: if anything goes wrong, the rest
247 * of the addresses will just be printed as unreliable.
248 */
249 unwind_next_frame(&state);
Josh Poimboeuf3b3fa112016-10-20 11:34:43 -0500250
251 /* if the frame has entry regs, print them */
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600252 regs = unwind_get_entry_regs(&state, &partial);
Josh Poimboeufb02fcf92017-12-04 15:07:09 +0100253 if (regs)
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -0600254 show_regs_if_on_stack(&stack_info, regs, partial);
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500255 }
256
Josh Poimboeuf3d02a9c2016-11-18 11:46:23 -0600257 if (stack_name)
258 printk("%s </%s>\n", log_lvl, stack_name);
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500259 }
Neil Horman878719e2008-10-23 10:40:06 -0400260}
261
Neil Horman878719e2008-10-23 10:40:06 -0400262void show_stack(struct task_struct *task, unsigned long *sp)
263{
Josh Poimboeuf81539162016-09-16 08:05:20 -0500264 task = task ? : current;
265
Tejun Heoa77f2a42013-04-30 15:27:09 -0700266 /*
267 * Stack frames below this one aren't interesting. Don't show them
268 * if we're printing for %current.
269 */
Josh Poimboeufe18bccc2016-09-16 14:18:16 -0500270 if (!sp && task == current)
Josh Poimboeuf4b8afaf2016-08-24 11:50:17 -0500271 sp = get_stack_pointer(current, NULL);
Tejun Heoa77f2a42013-04-30 15:27:09 -0700272
Josh Poimboeuf0ee1dd92016-10-25 09:51:13 -0500273 show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);
Neil Horman878719e2008-10-23 10:40:06 -0400274}
275
Borislav Petkov81c29492016-07-05 00:31:27 +0200276void show_stack_regs(struct pt_regs *regs)
277{
Josh Poimboeuf0ee1dd92016-10-25 09:51:13 -0500278 show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
Borislav Petkov81c29492016-07-05 00:31:27 +0200279}
280
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100281static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Neil Horman878719e2008-10-23 10:40:06 -0400282static int die_owner = -1;
283static unsigned int die_nest_count;
284
Masami Hiramatsu93266382014-04-17 17:18:14 +0900285unsigned long oops_begin(void)
Neil Horman878719e2008-10-23 10:40:06 -0400286{
287 int cpu;
288 unsigned long flags;
289
Neil Horman878719e2008-10-23 10:40:06 -0400290 oops_enter();
291
292 /* racy, but better than risking deadlock. */
293 raw_local_irq_save(flags);
294 cpu = smp_processor_id();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100295 if (!arch_spin_trylock(&die_lock)) {
Neil Horman878719e2008-10-23 10:40:06 -0400296 if (cpu == die_owner)
297 /* nested oops. should stop eventually */;
298 else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100299 arch_spin_lock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400300 }
301 die_nest_count++;
302 die_owner = cpu;
303 console_verbose();
304 bust_spinlocks(1);
305 return flags;
306}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900307NOKPROBE_SYMBOL(oops_begin);
Neil Horman878719e2008-10-23 10:40:06 -0400308
Andy Lutomirski2deb4be2016-07-14 13:22:55 -0700309void __noreturn rewind_stack_do_exit(int signr);
310
Masami Hiramatsu93266382014-04-17 17:18:14 +0900311void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
Neil Horman878719e2008-10-23 10:40:06 -0400312{
313 if (regs && kexec_should_crash(current))
314 crash_kexec(regs);
315
316 bust_spinlocks(0);
317 die_owner = -1;
Rusty Russell373d4d02013-01-21 17:17:39 +1030318 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Neil Horman878719e2008-10-23 10:40:06 -0400319 die_nest_count--;
320 if (!die_nest_count)
321 /* Nest count reaches zero, release the lock. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100322 arch_spin_unlock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400323 raw_local_irq_restore(flags);
324 oops_exit();
325
Borislav Petkov602bd702018-04-17 18:11:23 +0200326 /* Executive summary in case the oops scrolled away */
327 __show_regs(&exec_summary_regs, true);
328
Neil Horman878719e2008-10-23 10:40:06 -0400329 if (!signr)
330 return;
331 if (in_interrupt())
332 panic("Fatal exception in interrupt");
333 if (panic_on_oops)
334 panic("Fatal exception");
Andy Lutomirski2deb4be2016-07-14 13:22:55 -0700335
336 /*
337 * We're not going to return, but we might be on an IST stack or
338 * have very little stack space left. Rewind the stack and kill
339 * the task.
340 */
341 rewind_stack_do_exit(signr);
Neil Horman878719e2008-10-23 10:40:06 -0400342}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900343NOKPROBE_SYMBOL(oops_end);
Neil Horman878719e2008-10-23 10:40:06 -0400344
Masami Hiramatsu93266382014-04-17 17:18:14 +0900345int __die(const char *str, struct pt_regs *regs, long err)
Neil Horman878719e2008-10-23 10:40:06 -0400346{
Borislav Petkov602bd702018-04-17 18:11:23 +0200347 /* Save the regs of the first oops for the executive summary later. */
348 if (!die_counter)
349 exec_summary_regs = *regs;
350
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500351 printk(KERN_DEFAULT
Vlastimil Babka5f26d762017-12-19 22:33:46 +0100352 "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
Rasmus Villemoes8fad7ec2016-03-26 21:40:16 +0100353 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
354 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
355 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
Vlastimil Babka5f26d762017-12-19 22:33:46 +0100356 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "",
357 IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
358 (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
Rasmus Villemoes8fad7ec2016-03-26 21:40:16 +0100359
Borislav Petkov602bd702018-04-17 18:11:23 +0200360 show_regs(regs);
361 print_modules();
362
Neil Horman878719e2008-10-23 10:40:06 -0400363 if (notify_die(DIE_OOPS, str, regs, err,
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530364 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
Neil Horman878719e2008-10-23 10:40:06 -0400365 return 1;
366
Neil Horman878719e2008-10-23 10:40:06 -0400367 return 0;
368}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900369NOKPROBE_SYMBOL(__die);
Neil Horman878719e2008-10-23 10:40:06 -0400370
371/*
372 * This is gone through when something in the kernel has done something bad
373 * and is about to be terminated:
374 */
375void die(const char *str, struct pt_regs *regs, long err)
376{
377 unsigned long flags = oops_begin();
378 int sig = SIGSEGV;
379
Neil Horman878719e2008-10-23 10:40:06 -0400380 if (__die(str, regs, err))
381 sig = 0;
382 oops_end(flags, regs, sig);
383}
384
Borislav Petkov16d1cb02018-03-06 10:49:14 +0100385void show_regs(struct pt_regs *regs)
386{
387 bool all = true;
Borislav Petkov16d1cb02018-03-06 10:49:14 +0100388
389 show_regs_print_info(KERN_DEFAULT);
390
391 if (IS_ENABLED(CONFIG_X86_32))
392 all = !user_mode(regs);
393
394 __show_regs(regs, all);
395
396 /*
Borislav Petkov7cccf072018-04-17 18:11:22 +0200397 * When in-kernel, we also print out the stack at the time of the fault..
Borislav Petkov16d1cb02018-03-06 10:49:14 +0100398 */
Borislav Petkov7cccf072018-04-17 18:11:22 +0200399 if (!user_mode(regs))
Borislav Petkov16d1cb02018-03-06 10:49:14 +0100400 show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
Borislav Petkov16d1cb02018-03-06 10:49:14 +0100401}