xref: /linux-6.15/arch/x86/kernel/dumpstack.c (revision 2c118f50)
1878719e8SNeil Horman /*
2878719e8SNeil Horman  *  Copyright (C) 1991, 1992  Linus Torvalds
3878719e8SNeil Horman  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4878719e8SNeil Horman  */
5878719e8SNeil Horman #include <linux/kallsyms.h>
6878719e8SNeil Horman #include <linux/kprobes.h>
7878719e8SNeil Horman #include <linux/uaccess.h>
8878719e8SNeil Horman #include <linux/utsname.h>
9878719e8SNeil Horman #include <linux/hardirq.h>
10878719e8SNeil Horman #include <linux/kdebug.h>
11878719e8SNeil Horman #include <linux/module.h>
12878719e8SNeil Horman #include <linux/ptrace.h>
13b17b0153SIngo Molnar #include <linux/sched/debug.h>
1468db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
15712406a6SSteven Rostedt #include <linux/ftrace.h>
16878719e8SNeil Horman #include <linux/kexec.h>
17878719e8SNeil Horman #include <linux/bug.h>
18878719e8SNeil Horman #include <linux/nmi.h>
19878719e8SNeil Horman #include <linux/sysfs.h>
20f12d11c5SJann Horn #include <linux/kasan.h>
21878719e8SNeil Horman 
2292a0f81dSThomas Gleixner #include <asm/cpu_entry_area.h>
23878719e8SNeil Horman #include <asm/stacktrace.h>
24e18bcccdSJosh Poimboeuf #include <asm/unwind.h>
25878719e8SNeil Horman 
26878719e8SNeil Horman int panic_on_unrecovered_nmi;
275211a242SKurt Garloff int panic_on_io_nmi;
28878719e8SNeil Horman static int die_counter;
29878719e8SNeil Horman 
30602bd705SBorislav Petkov static struct pt_regs exec_summary_regs;
31602bd705SBorislav Petkov 
in_task_stack(unsigned long * stack,struct task_struct * task,struct stack_info * info)326b27edd7SJoerg Roedel bool noinstr in_task_stack(unsigned long *stack, struct task_struct *task,
33cb76c939SJosh Poimboeuf 			   struct stack_info *info)
34cb76c939SJosh Poimboeuf {
35cb76c939SJosh Poimboeuf 	unsigned long *begin = task_stack_page(task);
36cb76c939SJosh Poimboeuf 	unsigned long *end   = task_stack_page(task) + THREAD_SIZE;
37cb76c939SJosh Poimboeuf 
38cb76c939SJosh Poimboeuf 	if (stack < begin || stack >= end)
39cb76c939SJosh Poimboeuf 		return false;
40cb76c939SJosh Poimboeuf 
41cb76c939SJosh Poimboeuf 	info->type	= STACK_TYPE_TASK;
42cb76c939SJosh Poimboeuf 	info->begin	= begin;
43cb76c939SJosh Poimboeuf 	info->end	= end;
44cb76c939SJosh Poimboeuf 	info->next_sp	= NULL;
45cb76c939SJosh Poimboeuf 
46cb76c939SJosh Poimboeuf 	return true;
47cb76c939SJosh Poimboeuf }
48cb76c939SJosh Poimboeuf 
496b27edd7SJoerg Roedel /* Called from get_stack_info_noinstr - so must be noinstr too */
in_entry_stack(unsigned long * stack,struct stack_info * info)506b27edd7SJoerg Roedel bool noinstr in_entry_stack(unsigned long *stack, struct stack_info *info)
5133a2f1a6SAndy Lutomirski {
524fe2d8b1SDave Hansen 	struct entry_stack *ss = cpu_entry_stack(smp_processor_id());
5333a2f1a6SAndy Lutomirski 
540f9a4810SAndy Lutomirski 	void *begin = ss;
550f9a4810SAndy Lutomirski 	void *end = ss + 1;
5633a2f1a6SAndy Lutomirski 
5733a2f1a6SAndy Lutomirski 	if ((void *)stack < begin || (void *)stack >= end)
5833a2f1a6SAndy Lutomirski 		return false;
5933a2f1a6SAndy Lutomirski 
604fe2d8b1SDave Hansen 	info->type	= STACK_TYPE_ENTRY;
6133a2f1a6SAndy Lutomirski 	info->begin	= begin;
6233a2f1a6SAndy Lutomirski 	info->end	= end;
6333a2f1a6SAndy Lutomirski 	info->next_sp	= NULL;
6433a2f1a6SAndy Lutomirski 
6533a2f1a6SAndy Lutomirski 	return true;
6633a2f1a6SAndy Lutomirski }
6733a2f1a6SAndy Lutomirski 
printk_stack_address(unsigned long address,int reliable,const char * log_lvl)681fc7f61cSAdrien Schildknecht static void printk_stack_address(unsigned long address, int reliable,
69d46b3df7SDmitry Safonov 				 const char *log_lvl)
70878719e8SNeil Horman {
71d438f5fdSJosh Poimboeuf 	touch_nmi_watchdog();
729ef8af2aSStephen Boyd 	printk("%s %s%pBb\n", log_lvl, reliable ? "" : "? ", (void *)address);
735f01c988SJiri Slaby }
745f01c988SJiri Slaby 
copy_code(struct pt_regs * regs,u8 * buf,unsigned long src,unsigned int nbytes)75d181d2daSThomas Gleixner static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src,
76d181d2daSThomas Gleixner 		     unsigned int nbytes)
77d181d2daSThomas Gleixner {
78d181d2daSThomas Gleixner 	if (!user_mode(regs))
79d181d2daSThomas Gleixner 		return copy_from_kernel_nofault(buf, (u8 *)src, nbytes);
80d181d2daSThomas Gleixner 
81860aaabaSThomas Gleixner 	/* The user space code from other tasks cannot be accessed. */
82860aaabaSThomas Gleixner 	if (regs != task_pt_regs(current))
83860aaabaSThomas Gleixner 		return -EPERM;
84d181d2daSThomas Gleixner 
85860aaabaSThomas Gleixner 	/*
86860aaabaSThomas Gleixner 	 * Even if named copy_from_user_nmi() this can be invoked from
87860aaabaSThomas Gleixner 	 * other contexts and will not try to resolve a pagefault, which is
88860aaabaSThomas Gleixner 	 * the correct thing to do here as this code can be called from any
89860aaabaSThomas Gleixner 	 * context.
90860aaabaSThomas Gleixner 	 */
91d181d2daSThomas Gleixner 	return copy_from_user_nmi(buf, (void __user *)src, nbytes);
92d181d2daSThomas Gleixner }
93d181d2daSThomas Gleixner 
944dba072cSBorislav Petkov /*
954dba072cSBorislav Petkov  * There are a couple of reasons for the 2/3rd prologue, courtesy of Linus:
964dba072cSBorislav Petkov  *
974dba072cSBorislav Petkov  * In case where we don't have the exact kernel image (which, if we did, we can
984dba072cSBorislav Petkov  * simply disassemble and navigate to the RIP), the purpose of the bigger
994dba072cSBorislav Petkov  * prologue is to have more context and to be able to correlate the code from
1004dba072cSBorislav Petkov  * the different toolchains better.
1014dba072cSBorislav Petkov  *
1024dba072cSBorislav Petkov  * In addition, it helps in recreating the register allocation of the failing
1034dba072cSBorislav Petkov  * kernel and thus make sense of the register dump.
1044dba072cSBorislav Petkov  *
1054dba072cSBorislav Petkov  * What is more, the additional complication of a variable length insn arch like
1064dba072cSBorislav Petkov  * x86 warrants having longer byte sequence before rIP so that the disassembler
1074dba072cSBorislav Petkov  * can "sync" up properly and find instruction boundaries when decoding the
1084dba072cSBorislav Petkov  * opcode bytes.
1094dba072cSBorislav Petkov  *
1104dba072cSBorislav Petkov  * Thus, the 2/3rds prologue and 64 byte OPCODE_BUFSIZE is just a random
1114dba072cSBorislav Petkov  * guesstimate in attempt to achieve all of the above.
1124dba072cSBorislav Petkov  */
show_opcodes(struct pt_regs * regs,const char * loglvl)113342db04aSJann Horn void show_opcodes(struct pt_regs *regs, const char *loglvl)
114f0a1d7c1SBorislav Petkov {
1158e974b3bSRasmus Villemoes #define PROLOGUE_SIZE 42
1168e974b3bSRasmus Villemoes #define EPILOGUE_SIZE 21
1178e974b3bSRasmus Villemoes #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE)
1189e4a90fdSBorislav Petkov 	u8 opcodes[OPCODE_BUFSIZE];
119342db04aSJann Horn 	unsigned long prologue = regs->ip - PROLOGUE_SIZE;
120f0a1d7c1SBorislav Petkov 
121860aaabaSThomas Gleixner 	switch (copy_code(regs, opcodes, prologue, sizeof(opcodes))) {
122860aaabaSThomas Gleixner 	case 0:
1238e974b3bSRasmus Villemoes 		printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %"
1248e974b3bSRasmus Villemoes 		       __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes,
1258e974b3bSRasmus Villemoes 		       opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1);
126860aaabaSThomas Gleixner 		break;
127860aaabaSThomas Gleixner 	case -EPERM:
128860aaabaSThomas Gleixner 		/* No access to the user space stack of other tasks. Ignore. */
129860aaabaSThomas Gleixner 		break;
130860aaabaSThomas Gleixner 	default:
1315258b80eSJiri Slaby 		printk("%sCode: Unable to access opcode bytes at 0x%lx.\n",
132860aaabaSThomas Gleixner 		       loglvl, prologue);
133860aaabaSThomas Gleixner 		break;
134f0a1d7c1SBorislav Petkov 	}
135f0a1d7c1SBorislav Petkov }
136f0a1d7c1SBorislav Petkov 
show_ip(struct pt_regs * regs,const char * loglvl)1377cccf072SBorislav Petkov void show_ip(struct pt_regs *regs, const char *loglvl)
1387cccf072SBorislav Petkov {
1397cccf072SBorislav Petkov #ifdef CONFIG_X86_32
1407cccf072SBorislav Petkov 	printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
1417cccf072SBorislav Petkov #else
1427cccf072SBorislav Petkov 	printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
1437cccf072SBorislav Petkov #endif
144342db04aSJann Horn 	show_opcodes(regs, loglvl);
1457cccf072SBorislav Petkov }
1467cccf072SBorislav Petkov 
show_iret_regs(struct pt_regs * regs,const char * log_lvl)147fd07f802SDmitry Safonov void show_iret_regs(struct pt_regs *regs, const char *log_lvl)
148b02fcf9bSJosh Poimboeuf {
149fd07f802SDmitry Safonov 	show_ip(regs, log_lvl);
150fd07f802SDmitry Safonov 	printk("%sRSP: %04x:%016lx EFLAGS: %08lx", log_lvl, (int)regs->ss,
151b02fcf9bSJosh Poimboeuf 		regs->sp, regs->flags);
152b02fcf9bSJosh Poimboeuf }
153b02fcf9bSJosh Poimboeuf 
show_regs_if_on_stack(struct stack_info * info,struct pt_regs * regs,bool partial,const char * log_lvl)154a9cdbe72SJosh Poimboeuf static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs,
155ef2ff0f5SDmitry Safonov 				  bool partial, const char *log_lvl)
156b02fcf9bSJosh Poimboeuf {
157a9cdbe72SJosh Poimboeuf 	/*
158a9cdbe72SJosh Poimboeuf 	 * These on_stack() checks aren't strictly necessary: the unwind code
159a9cdbe72SJosh Poimboeuf 	 * has already validated the 'regs' pointer.  The checks are done for
160a9cdbe72SJosh Poimboeuf 	 * ordering reasons: if the registers are on the next stack, we don't
161a9cdbe72SJosh Poimboeuf 	 * want to print them out yet.  Otherwise they'll be shown as part of
162a9cdbe72SJosh Poimboeuf 	 * the wrong stack.  Later, when show_trace_log_lvl() switches to the
163a9cdbe72SJosh Poimboeuf 	 * next stack, this function will be called again with the same regs so
164a9cdbe72SJosh Poimboeuf 	 * they can be printed in the right context.
165a9cdbe72SJosh Poimboeuf 	 */
166a9cdbe72SJosh Poimboeuf 	if (!partial && on_stack(info, regs, sizeof(*regs))) {
167ef2ff0f5SDmitry Safonov 		__show_regs(regs, SHOW_REGS_SHORT, log_lvl);
168a9cdbe72SJosh Poimboeuf 
169a9cdbe72SJosh Poimboeuf 	} else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
170b02fcf9bSJosh Poimboeuf 				       IRET_FRAME_SIZE)) {
171b02fcf9bSJosh Poimboeuf 		/*
172b02fcf9bSJosh Poimboeuf 		 * When an interrupt or exception occurs in entry code, the
173b02fcf9bSJosh Poimboeuf 		 * full pt_regs might not have been saved yet.  In that case
174b02fcf9bSJosh Poimboeuf 		 * just print the iret frame.
175b02fcf9bSJosh Poimboeuf 		 */
176ef2ff0f5SDmitry Safonov 		show_iret_regs(regs, log_lvl);
177b02fcf9bSJosh Poimboeuf 	}
178b02fcf9bSJosh Poimboeuf }
179b02fcf9bSJosh Poimboeuf 
18037ad4ee8SAlexander Potapenko /*
18137ad4ee8SAlexander Potapenko  * This function reads pointers from the stack and dereferences them. The
18237ad4ee8SAlexander Potapenko  * pointers may not have their KMSAN shadow set up properly, which may result
18337ad4ee8SAlexander Potapenko  * in false positive reports. Disable instrumentation to avoid those.
18437ad4ee8SAlexander Potapenko  */
18537ad4ee8SAlexander Potapenko __no_kmsan_checks
show_trace_log_lvl(struct task_struct * task,struct pt_regs * regs,unsigned long * stack,const char * log_lvl)18609a217c1SHui Su static void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
187d46b3df7SDmitry Safonov 			unsigned long *stack, const char *log_lvl)
188878719e8SNeil Horman {
189e18bcccdSJosh Poimboeuf 	struct unwind_state state;
190e18bcccdSJosh Poimboeuf 	struct stack_info stack_info = {0};
191e18bcccdSJosh Poimboeuf 	unsigned long visit_mask = 0;
192e18bcccdSJosh Poimboeuf 	int graph_idx = 0;
193ebfc1501SArnd Bergmann 	bool partial = false;
194e18bcccdSJosh Poimboeuf 
195e18bcccdSJosh Poimboeuf 	printk("%sCall Trace:\n", log_lvl);
196e18bcccdSJosh Poimboeuf 
197e18bcccdSJosh Poimboeuf 	unwind_start(&state, task, regs, stack);
198*2c118f50SJann Horn 	stack = stack ?: get_stack_pointer(task, regs);
1993ffdeb1aSJosh Poimboeuf 	regs = unwind_get_entry_regs(&state, &partial);
200878719e8SNeil Horman 
201878719e8SNeil Horman 	/*
202e18bcccdSJosh Poimboeuf 	 * Iterate through the stacks, starting with the current stack pointer.
203e18bcccdSJosh Poimboeuf 	 * Each stack has a pointer to the next one.
204e18bcccdSJosh Poimboeuf 	 *
205e18bcccdSJosh Poimboeuf 	 * x86-64 can have several stacks:
206e18bcccdSJosh Poimboeuf 	 * - task stack
207e18bcccdSJosh Poimboeuf 	 * - interrupt stack
208e18bcccdSJosh Poimboeuf 	 * - HW exception stacks (double fault, nmi, debug, mce)
2094fe2d8b1SDave Hansen 	 * - entry stack
210e18bcccdSJosh Poimboeuf 	 *
2116e60e583SAndy Lutomirski 	 * x86-32 can have up to four stacks:
212e18bcccdSJosh Poimboeuf 	 * - task stack
213e18bcccdSJosh Poimboeuf 	 * - softirq stack
214e18bcccdSJosh Poimboeuf 	 * - hardirq stack
2154fe2d8b1SDave Hansen 	 * - entry stack
216878719e8SNeil Horman 	 */
217*2c118f50SJann Horn 	for (; stack; stack = stack_info.next_sp) {
2183d02a9c4SJosh Poimboeuf 		const char *stack_name;
219e18bcccdSJosh Poimboeuf 
2202e4be0d0SVernon Lovejoy 		stack = PTR_ALIGN(stack, sizeof(long));
2212e4be0d0SVernon Lovejoy 
2226e60e583SAndy Lutomirski 		if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
223e18bcccdSJosh Poimboeuf 			/*
2246e60e583SAndy Lutomirski 			 * We weren't on a valid stack.  It's possible that
2256e60e583SAndy Lutomirski 			 * we overflowed a valid stack into a guard page.
2266e60e583SAndy Lutomirski 			 * See if the next page up is valid so that we can
2276e60e583SAndy Lutomirski 			 * generate some kind of backtrace if this happens.
228e18bcccdSJosh Poimboeuf 			 */
2296e60e583SAndy Lutomirski 			stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack);
230e18bcccdSJosh Poimboeuf 			if (get_stack_info(stack, task, &stack_info, &visit_mask))
231e18bcccdSJosh Poimboeuf 				break;
2326e60e583SAndy Lutomirski 		}
233e18bcccdSJosh Poimboeuf 
2343d02a9c4SJosh Poimboeuf 		stack_name = stack_type_name(stack_info.type);
2353d02a9c4SJosh Poimboeuf 		if (stack_name)
2363d02a9c4SJosh Poimboeuf 			printk("%s <%s>\n", log_lvl, stack_name);
237e18bcccdSJosh Poimboeuf 
238b02fcf9bSJosh Poimboeuf 		if (regs)
239ef2ff0f5SDmitry Safonov 			show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
240b0529becSJosh Poimboeuf 
241e18bcccdSJosh Poimboeuf 		/*
242e18bcccdSJosh Poimboeuf 		 * Scan the stack, printing any text addresses we find.  At the
243e18bcccdSJosh Poimboeuf 		 * same time, follow proper stack frames with the unwinder.
244e18bcccdSJosh Poimboeuf 		 *
245e18bcccdSJosh Poimboeuf 		 * Addresses found during the scan which are not reported by
246e18bcccdSJosh Poimboeuf 		 * the unwinder are considered to be additional clues which are
247e18bcccdSJosh Poimboeuf 		 * sometimes useful for debugging and are prefixed with '?'.
248e18bcccdSJosh Poimboeuf 		 * This also serves as a failsafe option in case the unwinder
249e18bcccdSJosh Poimboeuf 		 * goes off in the weeds.
250e18bcccdSJosh Poimboeuf 		 */
251e18bcccdSJosh Poimboeuf 		for (; stack < stack_info.end; stack++) {
252e18bcccdSJosh Poimboeuf 			unsigned long real_addr;
253e18bcccdSJosh Poimboeuf 			int reliable = 0;
25491e08ab0SJosh Poimboeuf 			unsigned long addr = READ_ONCE_NOCHECK(*stack);
255e18bcccdSJosh Poimboeuf 			unsigned long *ret_addr_p =
256e18bcccdSJosh Poimboeuf 				unwind_get_return_address_ptr(&state);
257e18bcccdSJosh Poimboeuf 
258e18bcccdSJosh Poimboeuf 			if (!__kernel_text_address(addr))
259e18bcccdSJosh Poimboeuf 				continue;
260e18bcccdSJosh Poimboeuf 
2613b3fa11bSJosh Poimboeuf 			/*
2623b3fa11bSJosh Poimboeuf 			 * Don't print regs->ip again if it was already printed
263a9cdbe72SJosh Poimboeuf 			 * by show_regs_if_on_stack().
2643b3fa11bSJosh Poimboeuf 			 */
265b0529becSJosh Poimboeuf 			if (regs && stack == &regs->ip)
266b0529becSJosh Poimboeuf 				goto next;
2673b3fa11bSJosh Poimboeuf 
268e18bcccdSJosh Poimboeuf 			if (stack == ret_addr_p)
269e18bcccdSJosh Poimboeuf 				reliable = 1;
270e18bcccdSJosh Poimboeuf 
271e18bcccdSJosh Poimboeuf 			/*
272e18bcccdSJosh Poimboeuf 			 * When function graph tracing is enabled for a
273e18bcccdSJosh Poimboeuf 			 * function, its return address on the stack is
274e18bcccdSJosh Poimboeuf 			 * replaced with the address of an ftrace handler
275e18bcccdSJosh Poimboeuf 			 * (return_to_handler).  In that case, before printing
276e18bcccdSJosh Poimboeuf 			 * the "real" address, we want to print the handler
277e18bcccdSJosh Poimboeuf 			 * address as an "unreliable" hint that function graph
278e18bcccdSJosh Poimboeuf 			 * tracing was involved.
279e18bcccdSJosh Poimboeuf 			 */
280e18bcccdSJosh Poimboeuf 			real_addr = ftrace_graph_ret_addr(task, &graph_idx,
281e18bcccdSJosh Poimboeuf 							  addr, stack);
282e18bcccdSJosh Poimboeuf 			if (real_addr != addr)
283e18bcccdSJosh Poimboeuf 				printk_stack_address(addr, 0, log_lvl);
284e18bcccdSJosh Poimboeuf 			printk_stack_address(real_addr, reliable, log_lvl);
285e18bcccdSJosh Poimboeuf 
286e18bcccdSJosh Poimboeuf 			if (!reliable)
287e18bcccdSJosh Poimboeuf 				continue;
288e18bcccdSJosh Poimboeuf 
289b0529becSJosh Poimboeuf next:
290e18bcccdSJosh Poimboeuf 			/*
291e18bcccdSJosh Poimboeuf 			 * Get the next frame from the unwinder.  No need to
292e18bcccdSJosh Poimboeuf 			 * check for an error: if anything goes wrong, the rest
293e18bcccdSJosh Poimboeuf 			 * of the addresses will just be printed as unreliable.
294e18bcccdSJosh Poimboeuf 			 */
295e18bcccdSJosh Poimboeuf 			unwind_next_frame(&state);
2963b3fa11bSJosh Poimboeuf 
2973b3fa11bSJosh Poimboeuf 			/* if the frame has entry regs, print them */
298a9cdbe72SJosh Poimboeuf 			regs = unwind_get_entry_regs(&state, &partial);
299b02fcf9bSJosh Poimboeuf 			if (regs)
300ef2ff0f5SDmitry Safonov 				show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
301878719e8SNeil Horman 		}
302878719e8SNeil Horman 
3033d02a9c4SJosh Poimboeuf 		if (stack_name)
3043d02a9c4SJosh Poimboeuf 			printk("%s </%s>\n", log_lvl, stack_name);
305e18bcccdSJosh Poimboeuf 	}
306878719e8SNeil Horman }
307878719e8SNeil Horman 
show_stack(struct task_struct * task,unsigned long * sp,const char * loglvl)3089cb8f069SDmitry Safonov void show_stack(struct task_struct *task, unsigned long *sp,
309a832ff02SDmitry Safonov 		       const char *loglvl)
310878719e8SNeil Horman {
31181539169SJosh Poimboeuf 	task = task ? : current;
31281539169SJosh Poimboeuf 
313a77f2a4eSTejun Heo 	/*
314a77f2a4eSTejun Heo 	 * Stack frames below this one aren't interesting.  Don't show them
315a77f2a4eSTejun Heo 	 * if we're printing for %current.
316a77f2a4eSTejun Heo 	 */
317e18bcccdSJosh Poimboeuf 	if (!sp && task == current)
3184b8afafbSJosh Poimboeuf 		sp = get_stack_pointer(current, NULL);
319a77f2a4eSTejun Heo 
320a832ff02SDmitry Safonov 	show_trace_log_lvl(task, NULL, sp, loglvl);
321a832ff02SDmitry Safonov }
322a832ff02SDmitry Safonov 
show_stack_regs(struct pt_regs * regs)32381c2949fSBorislav Petkov void show_stack_regs(struct pt_regs *regs)
32481c2949fSBorislav Petkov {
3250ee1dd9fSJosh Poimboeuf 	show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
32681c2949fSBorislav Petkov }
32781c2949fSBorislav Petkov 
328edc35bd7SThomas Gleixner static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
329878719e8SNeil Horman static int die_owner = -1;
330878719e8SNeil Horman static unsigned int die_nest_count;
331878719e8SNeil Horman 
oops_begin(void)3329326638cSMasami Hiramatsu unsigned long oops_begin(void)
333878719e8SNeil Horman {
334878719e8SNeil Horman 	int cpu;
335878719e8SNeil Horman 	unsigned long flags;
336878719e8SNeil Horman 
337878719e8SNeil Horman 	oops_enter();
338878719e8SNeil Horman 
339878719e8SNeil Horman 	/* racy, but better than risking deadlock. */
340878719e8SNeil Horman 	raw_local_irq_save(flags);
341878719e8SNeil Horman 	cpu = smp_processor_id();
3420199c4e6SThomas Gleixner 	if (!arch_spin_trylock(&die_lock)) {
343878719e8SNeil Horman 		if (cpu == die_owner)
344878719e8SNeil Horman 			/* nested oops. should stop eventually */;
345878719e8SNeil Horman 		else
3460199c4e6SThomas Gleixner 			arch_spin_lock(&die_lock);
347878719e8SNeil Horman 	}
348878719e8SNeil Horman 	die_nest_count++;
349878719e8SNeil Horman 	die_owner = cpu;
350878719e8SNeil Horman 	console_verbose();
351878719e8SNeil Horman 	bust_spinlocks(1);
352878719e8SNeil Horman 	return flags;
353878719e8SNeil Horman }
3549326638cSMasami Hiramatsu NOKPROBE_SYMBOL(oops_begin);
355878719e8SNeil Horman 
3560e25498fSEric W. Biederman void __noreturn rewind_stack_and_make_dead(int signr);
3572deb4be2SAndy Lutomirski 
oops_end(unsigned long flags,struct pt_regs * regs,int signr)3589326638cSMasami Hiramatsu void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
359878719e8SNeil Horman {
360878719e8SNeil Horman 	if (regs && kexec_should_crash(current))
361878719e8SNeil Horman 		crash_kexec(regs);
362878719e8SNeil Horman 
363878719e8SNeil Horman 	bust_spinlocks(0);
364878719e8SNeil Horman 	die_owner = -1;
365373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
366878719e8SNeil Horman 	die_nest_count--;
367878719e8SNeil Horman 	if (!die_nest_count)
368878719e8SNeil Horman 		/* Nest count reaches zero, release the lock. */
3690199c4e6SThomas Gleixner 		arch_spin_unlock(&die_lock);
370878719e8SNeil Horman 	raw_local_irq_restore(flags);
371878719e8SNeil Horman 	oops_exit();
372878719e8SNeil Horman 
373602bd705SBorislav Petkov 	/* Executive summary in case the oops scrolled away */
37444e21535SDmitry Safonov 	__show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT);
375602bd705SBorislav Petkov 
376878719e8SNeil Horman 	if (!signr)
377878719e8SNeil Horman 		return;
378878719e8SNeil Horman 	if (in_interrupt())
379878719e8SNeil Horman 		panic("Fatal exception in interrupt");
380878719e8SNeil Horman 	if (panic_on_oops)
381878719e8SNeil Horman 		panic("Fatal exception");
3822deb4be2SAndy Lutomirski 
3832deb4be2SAndy Lutomirski 	/*
3842deb4be2SAndy Lutomirski 	 * We're not going to return, but we might be on an IST stack or
3852deb4be2SAndy Lutomirski 	 * have very little stack space left.  Rewind the stack and kill
3862deb4be2SAndy Lutomirski 	 * the task.
387f12d11c5SJann Horn 	 * Before we rewind the stack, we have to tell KASAN that we're going to
388f12d11c5SJann Horn 	 * reuse the task stack and that existing poisons are invalid.
3892deb4be2SAndy Lutomirski 	 */
390f12d11c5SJann Horn 	kasan_unpoison_task_stack(current);
3910e25498fSEric W. Biederman 	rewind_stack_and_make_dead(signr);
392878719e8SNeil Horman }
3939326638cSMasami Hiramatsu NOKPROBE_SYMBOL(oops_end);
394878719e8SNeil Horman 
__die_header(const char * str,struct pt_regs * regs,long err)395aa49f204SJann Horn static void __die_header(const char *str, struct pt_regs *regs, long err)
396878719e8SNeil Horman {
397602bd705SBorislav Petkov 	/* Save the regs of the first oops for the executive summary later. */
398602bd705SBorislav Petkov 	if (!die_counter)
399602bd705SBorislav Petkov 		exec_summary_regs = *regs;
400602bd705SBorislav Petkov 
401b0f4c4b3SPrarit Bhargava 	printk(KERN_DEFAULT
40296389cf3SSebastian Andrzej Siewior 	       "Oops: %s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff,
40396389cf3SSebastian Andrzej Siewior 	       ++die_counter,
4048fad7ec5SRasmus Villemoes 	       IS_ENABLED(CONFIG_SMP)     ? " SMP"             : "",
4058fad7ec5SRasmus Villemoes 	       debug_pagealloc_enabled()  ? " DEBUG_PAGEALLOC" : "",
4065f26d76cSVlastimil Babka 	       IS_ENABLED(CONFIG_KASAN)   ? " KASAN"           : "",
407ea4654e0SBreno Leitao 	       IS_ENABLED(CONFIG_MITIGATION_PAGE_TABLE_ISOLATION) ?
4085f26d76cSVlastimil Babka 	       (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
409aa49f204SJann Horn }
410aa49f204SJann Horn NOKPROBE_SYMBOL(__die_header);
4118fad7ec5SRasmus Villemoes 
__die_body(const char * str,struct pt_regs * regs,long err)412aa49f204SJann Horn static int __die_body(const char *str, struct pt_regs *regs, long err)
413aa49f204SJann Horn {
414602bd705SBorislav Petkov 	show_regs(regs);
415602bd705SBorislav Petkov 	print_modules();
416602bd705SBorislav Petkov 
417878719e8SNeil Horman 	if (notify_die(DIE_OOPS, str, regs, err,
41851e7dc70SSrikar Dronamraju 			current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
419878719e8SNeil Horman 		return 1;
420878719e8SNeil Horman 
421878719e8SNeil Horman 	return 0;
422878719e8SNeil Horman }
423aa49f204SJann Horn NOKPROBE_SYMBOL(__die_body);
424aa49f204SJann Horn 
__die(const char * str,struct pt_regs * regs,long err)425aa49f204SJann Horn int __die(const char *str, struct pt_regs *regs, long err)
426aa49f204SJann Horn {
427aa49f204SJann Horn 	__die_header(str, regs, err);
428aa49f204SJann Horn 	return __die_body(str, regs, err);
429aa49f204SJann Horn }
4309326638cSMasami Hiramatsu NOKPROBE_SYMBOL(__die);
431878719e8SNeil Horman 
432878719e8SNeil Horman /*
433878719e8SNeil Horman  * This is gone through when something in the kernel has done something bad
434878719e8SNeil Horman  * and is about to be terminated:
435878719e8SNeil Horman  */
die(const char * str,struct pt_regs * regs,long err)436878719e8SNeil Horman void die(const char *str, struct pt_regs *regs, long err)
437878719e8SNeil Horman {
438878719e8SNeil Horman 	unsigned long flags = oops_begin();
439878719e8SNeil Horman 	int sig = SIGSEGV;
440878719e8SNeil Horman 
441878719e8SNeil Horman 	if (__die(str, regs, err))
442878719e8SNeil Horman 		sig = 0;
443878719e8SNeil Horman 	oops_end(flags, regs, sig);
444878719e8SNeil Horman }
445878719e8SNeil Horman 
die_addr(const char * str,struct pt_regs * regs,long err,long gp_addr)446aa49f204SJann Horn void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr)
447aa49f204SJann Horn {
448aa49f204SJann Horn 	unsigned long flags = oops_begin();
449aa49f204SJann Horn 	int sig = SIGSEGV;
450aa49f204SJann Horn 
451aa49f204SJann Horn 	__die_header(str, regs, err);
4522f004eeaSJann Horn 	if (gp_addr)
4532f004eeaSJann Horn 		kasan_non_canonical_hook(gp_addr);
454aa49f204SJann Horn 	if (__die_body(str, regs, err))
455aa49f204SJann Horn 		sig = 0;
456aa49f204SJann Horn 	oops_end(flags, regs, sig);
457aa49f204SJann Horn }
458aa49f204SJann Horn 
show_regs(struct pt_regs * regs)45916d1cb0bSBorislav Petkov void show_regs(struct pt_regs *regs)
46016d1cb0bSBorislav Petkov {
46144e21535SDmitry Safonov 	enum show_regs_mode print_kernel_regs;
46244e21535SDmitry Safonov 
46316d1cb0bSBorislav Petkov 	show_regs_print_info(KERN_DEFAULT);
46416d1cb0bSBorislav Petkov 
46544e21535SDmitry Safonov 	print_kernel_regs = user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL;
46644e21535SDmitry Safonov 	__show_regs(regs, print_kernel_regs, KERN_DEFAULT);
46716d1cb0bSBorislav Petkov 
46816d1cb0bSBorislav Petkov 	/*
4697cccf072SBorislav Petkov 	 * When in-kernel, we also print out the stack at the time of the fault..
47016d1cb0bSBorislav Petkov 	 */
4717cccf072SBorislav Petkov 	if (!user_mode(regs))
47216d1cb0bSBorislav Petkov 		show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
47316d1cb0bSBorislav Petkov }
474