xref: /openbmc/linux/arch/x86/kernel/stacktrace.c (revision f7c35abe)
1 /*
2  * Stack trace management functions
3  *
4  *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5  */
6 #include <linux/sched.h>
7 #include <linux/sched/debug.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/stacktrace.h>
10 #include <linux/export.h>
11 #include <linux/uaccess.h>
12 #include <asm/stacktrace.h>
13 #include <asm/unwind.h>
14 
15 static int save_stack_address(struct stack_trace *trace, unsigned long addr,
16 			      bool nosched)
17 {
18 	if (nosched && in_sched_functions(addr))
19 		return 0;
20 
21 	if (trace->skip > 0) {
22 		trace->skip--;
23 		return 0;
24 	}
25 
26 	if (trace->nr_entries >= trace->max_entries)
27 		return -1;
28 
29 	trace->entries[trace->nr_entries++] = addr;
30 	return 0;
31 }
32 
33 static void __save_stack_trace(struct stack_trace *trace,
34 			       struct task_struct *task, struct pt_regs *regs,
35 			       bool nosched)
36 {
37 	struct unwind_state state;
38 	unsigned long addr;
39 
40 	if (regs)
41 		save_stack_address(trace, regs->ip, nosched);
42 
43 	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
44 	     unwind_next_frame(&state)) {
45 		addr = unwind_get_return_address(&state);
46 		if (!addr || save_stack_address(trace, addr, nosched))
47 			break;
48 	}
49 
50 	if (trace->nr_entries < trace->max_entries)
51 		trace->entries[trace->nr_entries++] = ULONG_MAX;
52 }
53 
54 /*
55  * Save stack-backtrace addresses into a stack_trace buffer.
56  */
57 void save_stack_trace(struct stack_trace *trace)
58 {
59 	__save_stack_trace(trace, current, NULL, false);
60 }
61 EXPORT_SYMBOL_GPL(save_stack_trace);
62 
63 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
64 {
65 	__save_stack_trace(trace, current, regs, false);
66 }
67 
68 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
69 {
70 	if (!try_get_task_stack(tsk))
71 		return;
72 
73 	__save_stack_trace(trace, tsk, NULL, true);
74 
75 	put_task_stack(tsk);
76 }
77 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
78 
79 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
80 
81 struct stack_frame_user {
82 	const void __user	*next_fp;
83 	unsigned long		ret_addr;
84 };
85 
86 static int
87 copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
88 {
89 	int ret;
90 
91 	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
92 		return 0;
93 
94 	ret = 1;
95 	pagefault_disable();
96 	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
97 		ret = 0;
98 	pagefault_enable();
99 
100 	return ret;
101 }
102 
103 static inline void __save_stack_trace_user(struct stack_trace *trace)
104 {
105 	const struct pt_regs *regs = task_pt_regs(current);
106 	const void __user *fp = (const void __user *)regs->bp;
107 
108 	if (trace->nr_entries < trace->max_entries)
109 		trace->entries[trace->nr_entries++] = regs->ip;
110 
111 	while (trace->nr_entries < trace->max_entries) {
112 		struct stack_frame_user frame;
113 
114 		frame.next_fp = NULL;
115 		frame.ret_addr = 0;
116 		if (!copy_stack_frame(fp, &frame))
117 			break;
118 		if ((unsigned long)fp < regs->sp)
119 			break;
120 		if (frame.ret_addr) {
121 			trace->entries[trace->nr_entries++] =
122 				frame.ret_addr;
123 		}
124 		if (fp == frame.next_fp)
125 			break;
126 		fp = frame.next_fp;
127 	}
128 }
129 
130 void save_stack_trace_user(struct stack_trace *trace)
131 {
132 	/*
133 	 * Trace user stack if we are not a kernel thread
134 	 */
135 	if (current->mm) {
136 		__save_stack_trace_user(trace);
137 	}
138 	if (trace->nr_entries < trace->max_entries)
139 		trace->entries[trace->nr_entries++] = ULONG_MAX;
140 }
141 
142