1 #include <linux/sched.h>
2 #include <linux/sched/task.h>
3 #include <linux/sched/task_stack.h>
4 #include <linux/interrupt.h>
5 #include <asm/sections.h>
6 #include <asm/ptrace.h>
7 #include <asm/bitops.h>
8 #include <asm/stacktrace.h>
9 #include <asm/unwind.h>
11 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
14 * This disables KASAN checking when reading a value from another task's stack,
15 * since the other task could be running on another CPU and could have poisoned
16 * the stack in the meantime.
18 #define READ_ONCE_TASK_STACK(task, x) \
21 if (task == current) \
24 val = READ_ONCE_NOCHECK(x); \
28 static void unwind_dump(struct unwind_state *state)
30 static bool dumped_before = false;
31 bool prev_zero, zero = false;
32 unsigned long word, *sp;
33 struct stack_info stack_info = {0};
34 unsigned long visit_mask = 0;
41 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
42 state->stack_info.type, state->stack_info.next_sp,
43 state->stack_mask, state->graph_idx);
45 for (sp = state->orig_sp; sp; sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
46 if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
49 for (; sp < stack_info.end; sp++) {
51 word = READ_ONCE_NOCHECK(*sp);
58 printk_deferred("%p: %0*x ...\n",
59 sp, BITS_PER_LONG/4, 0);
63 printk_deferred("%p: %0*lx (%pB)\n",
64 sp, BITS_PER_LONG/4, word, (void *)word);
69 unsigned long unwind_get_return_address(struct unwind_state *state)
71 if (unwind_done(state))
74 return __kernel_text_address(state->ip) ? state->ip : 0;
76 EXPORT_SYMBOL_GPL(unwind_get_return_address);
78 static size_t regs_size(struct pt_regs *regs)
80 /* x86_32 regs from kernel mode are two words shorter: */
81 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
82 return sizeof(*regs) - 2*sizeof(long);
87 static bool in_entry_code(unsigned long ip)
89 char *addr = (char *)ip;
91 if (addr >= __entry_text_start && addr < __entry_text_end)
94 #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
95 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
102 static inline unsigned long *last_frame(struct unwind_state *state)
104 return (unsigned long *)task_pt_regs(state->task) - 2;
107 static bool is_last_frame(struct unwind_state *state)
109 return state->bp == last_frame(state);
113 #define GCC_REALIGN_WORDS 3
115 #define GCC_REALIGN_WORDS 1
118 static inline unsigned long *last_aligned_frame(struct unwind_state *state)
120 return last_frame(state) - GCC_REALIGN_WORDS;
123 static bool is_last_aligned_frame(struct unwind_state *state)
125 unsigned long *last_bp = last_frame(state);
126 unsigned long *aligned_bp = last_aligned_frame(state);
129 * GCC can occasionally decide to realign the stack pointer and change
130 * the offset of the stack frame in the prologue of a function called
131 * by head/entry code. Examples:
136 * and $0xfffffff8,%esp
141 * <x86_64_start_kernel>:
143 * and $0xfffffffffffffff0,%rsp
148 * After aligning the stack, it pushes a duplicate copy of the return
149 * address before pushing the frame pointer.
151 return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
154 static bool is_last_ftrace_frame(struct unwind_state *state)
156 unsigned long *last_bp = last_frame(state);
157 unsigned long *last_ftrace_bp = last_bp - 3;
160 * When unwinding from an ftrace handler of a function called by entry
161 * code, the stack layout of the last frame is:
171 return (state->bp == last_ftrace_bp &&
172 *state->bp == *(state->bp + 2) &&
173 *(state->bp + 1) == *(state->bp + 4));
176 static bool is_last_task_frame(struct unwind_state *state)
178 return is_last_frame(state) || is_last_aligned_frame(state) ||
179 is_last_ftrace_frame(state);
183 * This determines if the frame pointer actually contains an encoded pointer to
184 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
186 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
188 unsigned long regs = (unsigned long)bp;
193 return (struct pt_regs *)(regs & ~0x1);
196 static bool update_stack_state(struct unwind_state *state,
197 unsigned long *next_bp)
199 struct stack_info *info = &state->stack_info;
200 enum stack_type prev_type = info->type;
201 struct pt_regs *regs;
202 unsigned long *frame, *prev_frame_end, *addr_p, addr;
206 prev_frame_end = (void *)state->regs + regs_size(state->regs);
208 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
210 /* Is the next frame pointer an encoded pointer to pt_regs? */
211 regs = decode_frame_pointer(next_bp);
213 frame = (unsigned long *)regs;
214 len = regs_size(regs);
215 state->got_irq = true;
218 len = FRAME_HEADER_SIZE;
222 * If the next bp isn't on the current stack, switch to the next one.
224 * We may have to traverse multiple stacks to deal with the possibility
225 * that info->next_sp could point to an empty stack and the next bp
226 * could be on a subsequent stack.
228 while (!on_stack(info, frame, len))
229 if (get_stack_info(info->next_sp, state->task, info,
233 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
234 if (state->orig_sp && state->stack_info.type == prev_type &&
235 frame < prev_frame_end)
238 /* Move state to the next frame: */
247 /* Save the return address: */
248 if (state->regs && user_mode(state->regs))
251 addr_p = unwind_get_return_address_ptr(state);
252 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
253 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
257 /* Save the original stack pointer for unwind_dump(): */
259 state->orig_sp = frame;
264 bool unwind_next_frame(struct unwind_state *state)
266 struct pt_regs *regs;
267 unsigned long *next_bp;
269 if (unwind_done(state))
272 /* Have we reached the end? */
273 if (state->regs && user_mode(state->regs))
276 if (is_last_task_frame(state)) {
277 regs = task_pt_regs(state->task);
280 * kthreads (other than the boot CPU's idle thread) have some
281 * partial regs at the end of their stack which were placed
282 * there by copy_thread_tls(). But the regs don't have any
283 * useful information, so we can skip them.
285 * This user_mode() check is slightly broader than a PF_KTHREAD
286 * check because it also catches the awkward situation where a
287 * newly forked kthread transitions into a user task by calling
288 * do_execve(), which eventually clears PF_KTHREAD.
290 if (!user_mode(regs))
294 * We're almost at the end, but not quite: there's still the
295 * syscall regs frame. Entry code doesn't encode the regs
296 * pointer for syscalls, so we have to set it manually.
304 /* Get the next frame pointer: */
306 next_bp = (unsigned long *)state->regs->bp;
308 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
310 /* Move to the next frame if it's safe: */
311 if (!update_stack_state(state, next_bp))
320 * When unwinding a non-current task, the task might actually be
321 * running on another CPU, in which case it could be modifying its
322 * stack while we're reading it. This is generally not a problem and
323 * can be ignored as long as the caller understands that unwinding
324 * another task will not always succeed.
326 if (state->task != current)
330 * Don't warn if the unwinder got lost due to an interrupt in entry
331 * code or in the C handler before the first frame pointer got set up:
333 if (state->got_irq && in_entry_code(state->ip))
336 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
337 state->regs->sp < (unsigned long)task_pt_regs(state->task))
341 printk_deferred_once(KERN_WARNING
342 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
343 state->regs, state->task->comm,
344 state->task->pid, next_bp);
347 printk_deferred_once(KERN_WARNING
348 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
349 state->bp, state->task->comm,
350 state->task->pid, next_bp);
354 state->stack_info.type = STACK_TYPE_UNKNOWN;
357 EXPORT_SYMBOL_GPL(unwind_next_frame);
359 void __unwind_start(struct unwind_state *state, struct task_struct *task,
360 struct pt_regs *regs, unsigned long *first_frame)
364 memset(state, 0, sizeof(*state));
366 state->got_irq = (regs);
368 /* Don't even attempt to start from user mode regs: */
369 if (regs && user_mode(regs)) {
370 state->stack_info.type = STACK_TYPE_UNKNOWN;
374 bp = get_frame_pointer(task, regs);
376 /* Initialize stack info and make sure the frame data is accessible: */
377 get_stack_info(bp, state->task, &state->stack_info,
379 update_stack_state(state, bp);
382 * The caller can provide the address of the first frame directly
383 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
384 * to start unwinding at. Skip ahead until we reach it.
386 while (!unwind_done(state) &&
387 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
388 state->bp < first_frame))
389 unwind_next_frame(state);
391 EXPORT_SYMBOL_GPL(__unwind_start);