xref: /openbmc/linux/arch/x86/kernel/stacktrace.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1250c2277SThomas Gleixner /*
2250c2277SThomas Gleixner  * Stack trace management functions
3250c2277SThomas Gleixner  *
48f47e163SIngo Molnar  *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5250c2277SThomas Gleixner  */
6250c2277SThomas Gleixner #include <linux/sched.h>
7b17b0153SIngo Molnar #include <linux/sched/debug.h>
868db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
9250c2277SThomas Gleixner #include <linux/stacktrace.h>
10186f4360SPaul Gortmaker #include <linux/export.h>
1102b67518STörök Edwin #include <linux/uaccess.h>
12250c2277SThomas Gleixner #include <asm/stacktrace.h>
1349a612c6SJosh Poimboeuf #include <asm/unwind.h>
14250c2277SThomas Gleixner 
arch_stack_walk(stack_trace_consume_fn consume_entry,void * cookie,struct task_struct * task,struct pt_regs * regs)153599fe12SThomas Gleixner void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
163599fe12SThomas Gleixner 		     struct task_struct *task, struct pt_regs *regs)
17018378c5SOleg Nesterov {
1849a612c6SJosh Poimboeuf 	struct unwind_state state;
1949a612c6SJosh Poimboeuf 	unsigned long addr;
2049a612c6SJosh Poimboeuf 
21264c03a2SMark Brown 	if (regs && !consume_entry(cookie, regs->ip))
223599fe12SThomas Gleixner 		return;
2349a612c6SJosh Poimboeuf 
2449a612c6SJosh Poimboeuf 	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
2549a612c6SJosh Poimboeuf 	     unwind_next_frame(&state)) {
2649a612c6SJosh Poimboeuf 		addr = unwind_get_return_address(&state);
27264c03a2SMark Brown 		if (!addr || !consume_entry(cookie, addr))
2849a612c6SJosh Poimboeuf 			break;
29018378c5SOleg Nesterov 	}
309745512cSArjan van de Ven }
319745512cSArjan van de Ven 
arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,void * cookie,struct task_struct * task)323599fe12SThomas Gleixner int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
333599fe12SThomas Gleixner 			     void *cookie, struct task_struct *task)
34af085d90SJosh Poimboeuf {
35af085d90SJosh Poimboeuf 	struct unwind_state state;
36af085d90SJosh Poimboeuf 	struct pt_regs *regs;
37af085d90SJosh Poimboeuf 	unsigned long addr;
38af085d90SJosh Poimboeuf 
39441ccc35SJiri Slaby 	for (unwind_start(&state, task, NULL, NULL);
40441ccc35SJiri Slaby 	     !unwind_done(&state) && !unwind_error(&state);
41af085d90SJosh Poimboeuf 	     unwind_next_frame(&state)) {
42af085d90SJosh Poimboeuf 
43a9cdbe72SJosh Poimboeuf 		regs = unwind_get_entry_regs(&state, NULL);
44af085d90SJosh Poimboeuf 		if (regs) {
45441ccc35SJiri Slaby 			/* Success path for user tasks */
46441ccc35SJiri Slaby 			if (user_mode(regs))
47c5c27a0aSThomas Gleixner 				return 0;
48441ccc35SJiri Slaby 
49af085d90SJosh Poimboeuf 			/*
50af085d90SJosh Poimboeuf 			 * Kernel mode registers on the stack indicate an
51af085d90SJosh Poimboeuf 			 * in-kernel interrupt or exception (e.g., preemption
52af085d90SJosh Poimboeuf 			 * or a page fault), which can make frame pointers
53af085d90SJosh Poimboeuf 			 * unreliable.
54af085d90SJosh Poimboeuf 			 */
550c414367SJiri Slaby 			if (IS_ENABLED(CONFIG_FRAME_POINTER))
56441ccc35SJiri Slaby 				return -EINVAL;
57af085d90SJosh Poimboeuf 		}
58af085d90SJosh Poimboeuf 
59af085d90SJosh Poimboeuf 		addr = unwind_get_return_address(&state);
60af085d90SJosh Poimboeuf 
61af085d90SJosh Poimboeuf 		/*
62af085d90SJosh Poimboeuf 		 * A NULL or invalid return address probably means there's some
63af085d90SJosh Poimboeuf 		 * generated code which __kernel_text_address() doesn't know
64af085d90SJosh Poimboeuf 		 * about.
65af085d90SJosh Poimboeuf 		 */
6617426923SJiri Slaby 		if (!addr)
67af085d90SJosh Poimboeuf 			return -EINVAL;
68af085d90SJosh Poimboeuf 
69264c03a2SMark Brown 		if (!consume_entry(cookie, addr))
70af085d90SJosh Poimboeuf 			return -EINVAL;
71af085d90SJosh Poimboeuf 	}
72af085d90SJosh Poimboeuf 
73af085d90SJosh Poimboeuf 	/* Check for stack corruption */
7417426923SJiri Slaby 	if (unwind_error(&state))
75af085d90SJosh Poimboeuf 		return -EINVAL;
76af085d90SJosh Poimboeuf 
77af085d90SJosh Poimboeuf 	return 0;
78af085d90SJosh Poimboeuf }
79af085d90SJosh Poimboeuf 
8002b67518STörök Edwin /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
8102b67518STörök Edwin 
82c9cf4dbbSFrederic Weisbecker struct stack_frame_user {
8302b67518STörök Edwin 	const void __user	*next_fp;
848d7c6a96STörök Edwin 	unsigned long		ret_addr;
8502b67518STörök Edwin };
8602b67518STörök Edwin 
87c9cf4dbbSFrederic Weisbecker static int
copy_stack_frame(const struct stack_frame_user __user * fp,struct stack_frame_user * frame)88c8e3dd86SAl Viro copy_stack_frame(const struct stack_frame_user __user *fp,
89c8e3dd86SAl Viro 		 struct stack_frame_user *frame)
9002b67518STörök Edwin {
9102b67518STörök Edwin 	int ret;
9202b67518STörök Edwin 
93*36903abeSArnd Bergmann 	if (!__access_ok(fp, sizeof(*frame)))
9402b67518STörök Edwin 		return 0;
9502b67518STörök Edwin 
9602b67518STörök Edwin 	ret = 1;
9702b67518STörök Edwin 	pagefault_disable();
98c8e3dd86SAl Viro 	if (__get_user(frame->next_fp, &fp->next_fp) ||
99c8e3dd86SAl Viro 	    __get_user(frame->ret_addr, &fp->ret_addr))
10002b67518STörök Edwin 		ret = 0;
10102b67518STörök Edwin 	pagefault_enable();
10202b67518STörök Edwin 
10302b67518STörök Edwin 	return ret;
10402b67518STörök Edwin }
10502b67518STörök Edwin 
arch_stack_walk_user(stack_trace_consume_fn consume_entry,void * cookie,const struct pt_regs * regs)1063599fe12SThomas Gleixner void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
1073599fe12SThomas Gleixner 			  const struct pt_regs *regs)
10802b67518STörök Edwin {
10902b67518STörök Edwin 	const void __user *fp = (const void __user *)regs->bp;
11002b67518STörök Edwin 
111264c03a2SMark Brown 	if (!consume_entry(cookie, regs->ip))
1123599fe12SThomas Gleixner 		return;
11302b67518STörök Edwin 
1143599fe12SThomas Gleixner 	while (1) {
115c9cf4dbbSFrederic Weisbecker 		struct stack_frame_user frame;
1168d7c6a96STörök Edwin 
11702b67518STörök Edwin 		frame.next_fp = NULL;
1188d7c6a96STörök Edwin 		frame.ret_addr = 0;
11902b67518STörök Edwin 		if (!copy_stack_frame(fp, &frame))
12002b67518STörök Edwin 			break;
12102b67518STörök Edwin 		if ((unsigned long)fp < regs->sp)
12202b67518STörök Edwin 			break;
123cbf5b73dSEiichi Tsukata 		if (!frame.ret_addr)
124cbf5b73dSEiichi Tsukata 			break;
125264c03a2SMark Brown 		if (!consume_entry(cookie, frame.ret_addr))
12602b67518STörök Edwin 			break;
12702b67518STörök Edwin 		fp = frame.next_fp;
12802b67518STörök Edwin 	}
12902b67518STörök Edwin }
1308d7c6a96STörök Edwin 
131