xref: /openbmc/linux/arch/x86/kernel/unwind_frame.c (revision e4781421e883340b796da5a724bda7226817990b)
1 #include <linux/sched.h>
2 #include <asm/ptrace.h>
3 #include <asm/bitops.h>
4 #include <asm/stacktrace.h>
5 #include <asm/unwind.h>
6 
7 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
8 
9 static void unwind_dump(struct unwind_state *state, unsigned long *sp)
10 {
11 	static bool dumped_before = false;
12 	bool prev_zero, zero = false;
13 	unsigned long word;
14 
15 	if (dumped_before)
16 		return;
17 
18 	dumped_before = true;
19 
20 	printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
21 			state->stack_info.type, state->stack_info.next_sp,
22 			state->stack_mask, state->graph_idx);
23 
24 	for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
25 		word = READ_ONCE_NOCHECK(*sp);
26 
27 		prev_zero = zero;
28 		zero = word == 0;
29 
30 		if (zero) {
31 			if (!prev_zero)
32 				printk_deferred("%p: %016x ...\n", sp, 0);
33 			continue;
34 		}
35 
36 		printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
37 	}
38 }
39 
40 unsigned long unwind_get_return_address(struct unwind_state *state)
41 {
42 	unsigned long addr;
43 	unsigned long *addr_p = unwind_get_return_address_ptr(state);
44 
45 	if (unwind_done(state))
46 		return 0;
47 
48 	if (state->regs && user_mode(state->regs))
49 		return 0;
50 
51 	addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p,
52 				     addr_p);
53 
54 	return __kernel_text_address(addr) ? addr : 0;
55 }
56 EXPORT_SYMBOL_GPL(unwind_get_return_address);
57 
58 static size_t regs_size(struct pt_regs *regs)
59 {
60 	/* x86_32 regs from kernel mode are two words shorter: */
61 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
62 		return sizeof(*regs) - 2*sizeof(long);
63 
64 	return sizeof(*regs);
65 }
66 
67 static bool is_last_task_frame(struct unwind_state *state)
68 {
69 	unsigned long bp = (unsigned long)state->bp;
70 	unsigned long regs = (unsigned long)task_pt_regs(state->task);
71 
72 	/*
73 	 * We have to check for the last task frame at two different locations
74 	 * because gcc can occasionally decide to realign the stack pointer and
75 	 * change the offset of the stack frame by a word in the prologue of a
76 	 * function called by head/entry code.
77 	 */
78 	return bp == regs - FRAME_HEADER_SIZE ||
79 	       bp == regs - FRAME_HEADER_SIZE - sizeof(long);
80 }
81 
82 /*
83  * This determines if the frame pointer actually contains an encoded pointer to
84  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
85  */
86 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
87 {
88 	unsigned long regs = (unsigned long)bp;
89 
90 	if (!(regs & 0x1))
91 		return NULL;
92 
93 	return (struct pt_regs *)(regs & ~0x1);
94 }
95 
96 static bool update_stack_state(struct unwind_state *state, void *addr,
97 			       size_t len)
98 {
99 	struct stack_info *info = &state->stack_info;
100 	enum stack_type orig_type = info->type;
101 
102 	/*
103 	 * If addr isn't on the current stack, switch to the next one.
104 	 *
105 	 * We may have to traverse multiple stacks to deal with the possibility
106 	 * that 'info->next_sp' could point to an empty stack and 'addr' could
107 	 * be on a subsequent stack.
108 	 */
109 	while (!on_stack(info, addr, len))
110 		if (get_stack_info(info->next_sp, state->task, info,
111 				   &state->stack_mask))
112 			return false;
113 
114 	if (!state->orig_sp || info->type != orig_type)
115 		state->orig_sp = addr;
116 
117 	return true;
118 }
119 
120 bool unwind_next_frame(struct unwind_state *state)
121 {
122 	struct pt_regs *regs;
123 	unsigned long *next_bp, *next_frame;
124 	size_t next_len;
125 	enum stack_type prev_type = state->stack_info.type;
126 
127 	if (unwind_done(state))
128 		return false;
129 
130 	/* have we reached the end? */
131 	if (state->regs && user_mode(state->regs))
132 		goto the_end;
133 
134 	if (is_last_task_frame(state)) {
135 		regs = task_pt_regs(state->task);
136 
137 		/*
138 		 * kthreads (other than the boot CPU's idle thread) have some
139 		 * partial regs at the end of their stack which were placed
140 		 * there by copy_thread_tls().  But the regs don't have any
141 		 * useful information, so we can skip them.
142 		 *
143 		 * This user_mode() check is slightly broader than a PF_KTHREAD
144 		 * check because it also catches the awkward situation where a
145 		 * newly forked kthread transitions into a user task by calling
146 		 * do_execve(), which eventually clears PF_KTHREAD.
147 		 */
148 		if (!user_mode(regs))
149 			goto the_end;
150 
151 		/*
152 		 * We're almost at the end, but not quite: there's still the
153 		 * syscall regs frame.  Entry code doesn't encode the regs
154 		 * pointer for syscalls, so we have to set it manually.
155 		 */
156 		state->regs = regs;
157 		state->bp = NULL;
158 		return true;
159 	}
160 
161 	/* get the next frame pointer */
162 	if (state->regs)
163 		next_bp = (unsigned long *)state->regs->bp;
164 	else
165 		next_bp = (unsigned long *)*state->bp;
166 
167 	/* is the next frame pointer an encoded pointer to pt_regs? */
168 	regs = decode_frame_pointer(next_bp);
169 	if (regs) {
170 		next_frame = (unsigned long *)regs;
171 		next_len = sizeof(*regs);
172 	} else {
173 		next_frame = next_bp;
174 		next_len = FRAME_HEADER_SIZE;
175 	}
176 
177 	/* make sure the next frame's data is accessible */
178 	if (!update_stack_state(state, next_frame, next_len)) {
179 		/*
180 		 * Don't warn on bad regs->bp.  An interrupt in entry code
181 		 * might cause a false positive warning.
182 		 */
183 		if (state->regs)
184 			goto the_end;
185 
186 		goto bad_address;
187 	}
188 
189 	/* Make sure it only unwinds up and doesn't overlap the last frame: */
190 	if (state->stack_info.type == prev_type) {
191 		if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
192 			goto bad_address;
193 
194 		if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
195 			goto bad_address;
196 	}
197 
198 	/* move to the next frame */
199 	if (regs) {
200 		state->regs = regs;
201 		state->bp = NULL;
202 	} else {
203 		state->bp = next_bp;
204 		state->regs = NULL;
205 	}
206 
207 	return true;
208 
209 bad_address:
210 	if (state->regs) {
211 		printk_deferred_once(KERN_WARNING
212 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
213 			state->regs, state->task->comm,
214 			state->task->pid, next_frame);
215 		unwind_dump(state, (unsigned long *)state->regs);
216 	} else {
217 		printk_deferred_once(KERN_WARNING
218 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
219 			state->bp, state->task->comm,
220 			state->task->pid, next_frame);
221 		unwind_dump(state, state->bp);
222 	}
223 the_end:
224 	state->stack_info.type = STACK_TYPE_UNKNOWN;
225 	return false;
226 }
227 EXPORT_SYMBOL_GPL(unwind_next_frame);
228 
229 void __unwind_start(struct unwind_state *state, struct task_struct *task,
230 		    struct pt_regs *regs, unsigned long *first_frame)
231 {
232 	unsigned long *bp, *frame;
233 	size_t len;
234 
235 	memset(state, 0, sizeof(*state));
236 	state->task = task;
237 
238 	/* don't even attempt to start from user mode regs */
239 	if (regs && user_mode(regs)) {
240 		state->stack_info.type = STACK_TYPE_UNKNOWN;
241 		return;
242 	}
243 
244 	/* set up the starting stack frame */
245 	bp = get_frame_pointer(task, regs);
246 	regs = decode_frame_pointer(bp);
247 	if (regs) {
248 		state->regs = regs;
249 		frame = (unsigned long *)regs;
250 		len = sizeof(*regs);
251 	} else {
252 		state->bp = bp;
253 		frame = bp;
254 		len = FRAME_HEADER_SIZE;
255 	}
256 
257 	/* initialize stack info and make sure the frame data is accessible */
258 	get_stack_info(frame, state->task, &state->stack_info,
259 		       &state->stack_mask);
260 	update_stack_state(state, frame, len);
261 
262 	/*
263 	 * The caller can provide the address of the first frame directly
264 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
265 	 * to start unwinding at.  Skip ahead until we reach it.
266 	 */
267 	while (!unwind_done(state) &&
268 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
269 			state->bp < first_frame))
270 		unwind_next_frame(state);
271 }
272 EXPORT_SYMBOL_GPL(__unwind_start);
273