unwind_frame.c (bfca9acf1a5df0ff98fbf47e363adb48612bb7ec) unwind_frame.c (5ed8d8bb38c5dcd78de540182cedb0fb19399aab)
1#include <linux/sched.h>
2#include <linux/sched/task.h>
3#include <linux/sched/task_stack.h>
4#include <asm/ptrace.h>
5#include <asm/bitops.h>
6#include <asm/stacktrace.h>
7#include <asm/unwind.h>
8

--- 121 unchanged lines hidden (view full) ---

130 unsigned long regs = (unsigned long)bp;
131
132 if (!(regs & 0x1))
133 return NULL;
134
135 return (struct pt_regs *)(regs & ~0x1);
136}
137
1#include <linux/sched.h>
2#include <linux/sched/task.h>
3#include <linux/sched/task_stack.h>
4#include <asm/ptrace.h>
5#include <asm/bitops.h>
6#include <asm/stacktrace.h>
7#include <asm/unwind.h>
8

--- 121 unchanged lines hidden (view full) ---

130 unsigned long regs = (unsigned long)bp;
131
132 if (!(regs & 0x1))
133 return NULL;
134
135 return (struct pt_regs *)(regs & ~0x1);
136}
137
138static bool update_stack_state(struct unwind_state *state, void *addr,
139 size_t len)
138static bool update_stack_state(struct unwind_state *state,
139 unsigned long *next_bp)
140{
141 struct stack_info *info = &state->stack_info;
140{
141 struct stack_info *info = &state->stack_info;
142 enum stack_type orig_type = info->type;
142 enum stack_type prev_type = info->type;
143 struct pt_regs *regs;
144 unsigned long *frame, *prev_frame_end;
145 size_t len;
143
146
147 if (state->regs)
148 prev_frame_end = (void *)state->regs + regs_size(state->regs);
149 else
150 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
151
152 /* Is the next frame pointer an encoded pointer to pt_regs? */
153 regs = decode_frame_pointer(next_bp);
154 if (regs) {
155 frame = (unsigned long *)regs;
156 len = regs_size(regs);
157 } else {
158 frame = next_bp;
159 len = FRAME_HEADER_SIZE;
160 }
161
144 /*
162 /*
145 * If addr isn't on the current stack, switch to the next one.
163 * If the next bp isn't on the current stack, switch to the next one.
146 *
147 * We may have to traverse multiple stacks to deal with the possibility
164 *
165 * We may have to traverse multiple stacks to deal with the possibility
148 * that 'info->next_sp' could point to an empty stack and 'addr' could
149 * be on a subsequent stack.
166 * that info->next_sp could point to an empty stack and the next bp
167 * could be on a subsequent stack.
150 */
168 */
151 while (!on_stack(info, addr, len))
169 while (!on_stack(info, frame, len))
152 if (get_stack_info(info->next_sp, state->task, info,
153 &state->stack_mask))
154 return false;
155
170 if (get_stack_info(info->next_sp, state->task, info,
171 &state->stack_mask))
172 return false;
173
156 if (!state->orig_sp || info->type != orig_type)
157 state->orig_sp = addr;
174 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
175 if (state->orig_sp && state->stack_info.type == prev_type &&
176 frame < prev_frame_end)
177 return false;
158
178
179 /* Move state to the next frame: */
180 if (regs) {
181 state->regs = regs;
182 state->bp = NULL;
183 } else {
184 state->bp = next_bp;
185 state->regs = NULL;
186 }
187
188 /* Save the original stack pointer for unwind_dump(): */
189 if (!state->orig_sp || info->type != prev_type)
190 state->orig_sp = frame;
191
159 return true;
160}
161
162bool unwind_next_frame(struct unwind_state *state)
163{
164 struct pt_regs *regs;
192 return true;
193}
194
195bool unwind_next_frame(struct unwind_state *state)
196{
197 struct pt_regs *regs;
165 unsigned long *next_bp, *next_frame;
166 size_t next_len;
167 enum stack_type prev_type = state->stack_info.type;
198 unsigned long *next_bp;
168
169 if (unwind_done(state))
170 return false;
171
199
200 if (unwind_done(state))
201 return false;
202
172 /* have we reached the end? */
203 /* Have we reached the end? */
173 if (state->regs && user_mode(state->regs))
174 goto the_end;
175
176 if (is_last_task_frame(state)) {
177 regs = task_pt_regs(state->task);
178
179 /*
180 * kthreads (other than the boot CPU's idle thread) have some

--- 14 unchanged lines hidden (view full) ---

195 * syscall regs frame. Entry code doesn't encode the regs
196 * pointer for syscalls, so we have to set it manually.
197 */
198 state->regs = regs;
199 state->bp = NULL;
200 return true;
201 }
202
204 if (state->regs && user_mode(state->regs))
205 goto the_end;
206
207 if (is_last_task_frame(state)) {
208 regs = task_pt_regs(state->task);
209
210 /*
211 * kthreads (other than the boot CPU's idle thread) have some

--- 14 unchanged lines hidden (view full) ---

226 * syscall regs frame. Entry code doesn't encode the regs
227 * pointer for syscalls, so we have to set it manually.
228 */
229 state->regs = regs;
230 state->bp = NULL;
231 return true;
232 }
233
203 /* get the next frame pointer */
234 /* Get the next frame pointer: */
204 if (state->regs)
205 next_bp = (unsigned long *)state->regs->bp;
206 else
235 if (state->regs)
236 next_bp = (unsigned long *)state->regs->bp;
237 else
207 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
238 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
208
239
209 /* is the next frame pointer an encoded pointer to pt_regs? */
210 regs = decode_frame_pointer(next_bp);
211 if (regs) {
212 next_frame = (unsigned long *)regs;
213 next_len = sizeof(*regs);
214 } else {
215 next_frame = next_bp;
216 next_len = FRAME_HEADER_SIZE;
217 }
218
219 /* make sure the next frame's data is accessible */
220 if (!update_stack_state(state, next_frame, next_len)) {
240 /* Move to the next frame if it's safe: */
241 if (!update_stack_state(state, next_bp)) {
221 /*
222 * Don't warn on bad regs->bp. An interrupt in entry code
223 * might cause a false positive warning.
224 */
225 if (state->regs)
226 goto the_end;
227
228 goto bad_address;
229 }
230
242 /*
243 * Don't warn on bad regs->bp. An interrupt in entry code
244 * might cause a false positive warning.
245 */
246 if (state->regs)
247 goto the_end;
248
249 goto bad_address;
250 }
251
231 /* Make sure it only unwinds up and doesn't overlap the last frame: */
232 if (state->stack_info.type == prev_type) {
233 if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
234 goto bad_address;
235
236 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
237 goto bad_address;
238 }
239
240 /* move to the next frame */
241 if (regs) {
242 state->regs = regs;
243 state->bp = NULL;
244 } else {
245 state->bp = next_bp;
246 state->regs = NULL;
247 }
248
249 return true;
250
251bad_address:
252 /*
253 * When unwinding a non-current task, the task might actually be
254 * running on another CPU, in which case it could be modifying its
255 * stack while we're reading it. This is generally not a problem and
256 * can be ignored as long as the caller understands that unwinding
257 * another task will not always succeed.
258 */
259 if (state->task != current)
260 goto the_end;
261
262 if (state->regs) {
263 printk_deferred_once(KERN_WARNING
264 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
265 state->regs, state->task->comm,
252 return true;
253
254bad_address:
255 /*
256 * When unwinding a non-current task, the task might actually be
257 * running on another CPU, in which case it could be modifying its
258 * stack while we're reading it. This is generally not a problem and
259 * can be ignored as long as the caller understands that unwinding
260 * another task will not always succeed.
261 */
262 if (state->task != current)
263 goto the_end;
264
265 if (state->regs) {
266 printk_deferred_once(KERN_WARNING
267 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
268 state->regs, state->task->comm,
266 state->task->pid, next_frame);
269 state->task->pid, next_bp);
267 unwind_dump(state, (unsigned long *)state->regs);
268 } else {
269 printk_deferred_once(KERN_WARNING
270 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
271 state->bp, state->task->comm,
270 unwind_dump(state, (unsigned long *)state->regs);
271 } else {
272 printk_deferred_once(KERN_WARNING
273 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
274 state->bp, state->task->comm,
272 state->task->pid, next_frame);
275 state->task->pid, next_bp);
273 unwind_dump(state, state->bp);
274 }
275the_end:
276 state->stack_info.type = STACK_TYPE_UNKNOWN;
277 return false;
278}
279EXPORT_SYMBOL_GPL(unwind_next_frame);
280
281void __unwind_start(struct unwind_state *state, struct task_struct *task,
282 struct pt_regs *regs, unsigned long *first_frame)
283{
276 unwind_dump(state, state->bp);
277 }
278the_end:
279 state->stack_info.type = STACK_TYPE_UNKNOWN;
280 return false;
281}
282EXPORT_SYMBOL_GPL(unwind_next_frame);
283
284void __unwind_start(struct unwind_state *state, struct task_struct *task,
285 struct pt_regs *regs, unsigned long *first_frame)
286{
284 unsigned long *bp, *frame;
285 size_t len;
287 unsigned long *bp;
286
287 memset(state, 0, sizeof(*state));
288 state->task = task;
289
288
289 memset(state, 0, sizeof(*state));
290 state->task = task;
291
290 /* don't even attempt to start from user mode regs */
292 /* Don't even attempt to start from user mode regs: */
291 if (regs && user_mode(regs)) {
292 state->stack_info.type = STACK_TYPE_UNKNOWN;
293 return;
294 }
295
293 if (regs && user_mode(regs)) {
294 state->stack_info.type = STACK_TYPE_UNKNOWN;
295 return;
296 }
297
296 /* set up the starting stack frame */
297 bp = get_frame_pointer(task, regs);
298 bp = get_frame_pointer(task, regs);
298 regs = decode_frame_pointer(bp);
299 if (regs) {
300 state->regs = regs;
301 frame = (unsigned long *)regs;
302 len = sizeof(*regs);
303 } else {
304 state->bp = bp;
305 frame = bp;
306 len = FRAME_HEADER_SIZE;
307 }
308
299
309 /* initialize stack info and make sure the frame data is accessible */
310 get_stack_info(frame, state->task, &state->stack_info,
300 /* Initialize stack info and make sure the frame data is accessible: */
301 get_stack_info(bp, state->task, &state->stack_info,
311 &state->stack_mask);
302 &state->stack_mask);
312 update_stack_state(state, frame, len);
303 update_stack_state(state, bp);
313
314 /*
315 * The caller can provide the address of the first frame directly
316 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
317 * to start unwinding at. Skip ahead until we reach it.
318 */
319 while (!unwind_done(state) &&
320 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
321 state->bp < first_frame))
322 unwind_next_frame(state);
323}
324EXPORT_SYMBOL_GPL(__unwind_start);
304
305 /*
306 * The caller can provide the address of the first frame directly
307 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
308 * to start unwinding at. Skip ahead until we reach it.
309 */
310 while (!unwind_done(state) &&
311 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
312 state->bp < first_frame))
313 unwind_next_frame(state);
314}
315EXPORT_SYMBOL_GPL(__unwind_start);