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, unsigned long *sp)
30 static bool dumped_before = false;
31 bool prev_zero, zero = false;
39 printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
40 state->stack_info.type, state->stack_info.next_sp,
41 state->stack_mask, state->graph_idx);
43 for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
44 word = READ_ONCE_NOCHECK(*sp);
51 printk_deferred("%p: %0*x ...\n",
52 sp, BITS_PER_LONG/4, 0);
56 printk_deferred("%p: %0*lx (%pB)\n",
57 sp, BITS_PER_LONG/4, word, (void *)word);
61 unsigned long unwind_get_return_address(struct unwind_state *state)
63 if (unwind_done(state))
66 return __kernel_text_address(state->ip) ? state->ip : 0;
68 EXPORT_SYMBOL_GPL(unwind_get_return_address);
70 static size_t regs_size(struct pt_regs *regs)
72 /* x86_32 regs from kernel mode are two words shorter: */
73 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
74 return sizeof(*regs) - 2*sizeof(long);
79 static bool in_entry_code(unsigned long ip)
81 char *addr = (char *)ip;
83 if (addr >= __entry_text_start && addr < __entry_text_end)
86 #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
87 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
95 #define GCC_REALIGN_WORDS 3
97 #define GCC_REALIGN_WORDS 1
100 static bool is_last_task_frame(struct unwind_state *state)
102 unsigned long *last_bp = (unsigned long *)task_pt_regs(state->task) - 2;
103 unsigned long *aligned_bp = last_bp - GCC_REALIGN_WORDS;
106 * We have to check for the last task frame at two different locations
107 * because gcc can occasionally decide to realign the stack pointer and
108 * change the offset of the stack frame in the prologue of a function
109 * called by head/entry code. Examples:
114 * and $0xfffffff8,%esp
119 * <x86_64_start_kernel>:
121 * and $0xfffffffffffffff0,%rsp
126 * Note that after aligning the stack, it pushes a duplicate copy of
127 * the return address before pushing the frame pointer.
129 return (state->bp == last_bp ||
130 (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1)));
134 * This determines if the frame pointer actually contains an encoded pointer to
135 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
137 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
139 unsigned long regs = (unsigned long)bp;
144 return (struct pt_regs *)(regs & ~0x1);
147 static bool update_stack_state(struct unwind_state *state,
148 unsigned long *next_bp)
150 struct stack_info *info = &state->stack_info;
151 enum stack_type prev_type = info->type;
152 struct pt_regs *regs;
153 unsigned long *frame, *prev_frame_end, *addr_p, addr;
157 prev_frame_end = (void *)state->regs + regs_size(state->regs);
159 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
161 /* Is the next frame pointer an encoded pointer to pt_regs? */
162 regs = decode_frame_pointer(next_bp);
164 frame = (unsigned long *)regs;
165 len = regs_size(regs);
166 state->got_irq = true;
169 len = FRAME_HEADER_SIZE;
173 * If the next bp isn't on the current stack, switch to the next one.
175 * We may have to traverse multiple stacks to deal with the possibility
176 * that info->next_sp could point to an empty stack and the next bp
177 * could be on a subsequent stack.
179 while (!on_stack(info, frame, len))
180 if (get_stack_info(info->next_sp, state->task, info,
184 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
185 if (state->orig_sp && state->stack_info.type == prev_type &&
186 frame < prev_frame_end)
189 /* Move state to the next frame: */
198 /* Save the return address: */
199 if (state->regs && user_mode(state->regs))
202 addr_p = unwind_get_return_address_ptr(state);
203 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
204 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
208 /* Save the original stack pointer for unwind_dump(): */
209 if (!state->orig_sp || info->type != prev_type)
210 state->orig_sp = frame;
215 bool unwind_next_frame(struct unwind_state *state)
217 struct pt_regs *regs;
218 unsigned long *next_bp;
220 if (unwind_done(state))
223 /* Have we reached the end? */
224 if (state->regs && user_mode(state->regs))
227 if (is_last_task_frame(state)) {
228 regs = task_pt_regs(state->task);
231 * kthreads (other than the boot CPU's idle thread) have some
232 * partial regs at the end of their stack which were placed
233 * there by copy_thread_tls(). But the regs don't have any
234 * useful information, so we can skip them.
236 * This user_mode() check is slightly broader than a PF_KTHREAD
237 * check because it also catches the awkward situation where a
238 * newly forked kthread transitions into a user task by calling
239 * do_execve(), which eventually clears PF_KTHREAD.
241 if (!user_mode(regs))
245 * We're almost at the end, but not quite: there's still the
246 * syscall regs frame. Entry code doesn't encode the regs
247 * pointer for syscalls, so we have to set it manually.
255 /* Get the next frame pointer: */
257 next_bp = (unsigned long *)state->regs->bp;
259 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
261 /* Move to the next frame if it's safe: */
262 if (!update_stack_state(state, next_bp))
269 * When unwinding a non-current task, the task might actually be
270 * running on another CPU, in which case it could be modifying its
271 * stack while we're reading it. This is generally not a problem and
272 * can be ignored as long as the caller understands that unwinding
273 * another task will not always succeed.
275 if (state->task != current)
279 * Don't warn if the unwinder got lost due to an interrupt in entry
280 * code before the stack was set up:
282 if (state->got_irq && in_entry_code(state->ip))
286 printk_deferred_once(KERN_WARNING
287 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
288 state->regs, state->task->comm,
289 state->task->pid, next_bp);
290 unwind_dump(state, (unsigned long *)state->regs);
292 printk_deferred_once(KERN_WARNING
293 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
294 state->bp, state->task->comm,
295 state->task->pid, next_bp);
296 unwind_dump(state, state->bp);
299 state->stack_info.type = STACK_TYPE_UNKNOWN;
302 EXPORT_SYMBOL_GPL(unwind_next_frame);
304 void __unwind_start(struct unwind_state *state, struct task_struct *task,
305 struct pt_regs *regs, unsigned long *first_frame)
309 memset(state, 0, sizeof(*state));
311 state->got_irq = (regs);
313 /* Don't even attempt to start from user mode regs: */
314 if (regs && user_mode(regs)) {
315 state->stack_info.type = STACK_TYPE_UNKNOWN;
319 bp = get_frame_pointer(task, regs);
321 /* Initialize stack info and make sure the frame data is accessible: */
322 get_stack_info(bp, state->task, &state->stack_info,
324 update_stack_state(state, bp);
327 * The caller can provide the address of the first frame directly
328 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
329 * to start unwinding at. Skip ahead until we reach it.
331 while (!unwind_done(state) &&
332 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
333 state->bp < first_frame))
334 unwind_next_frame(state);
336 EXPORT_SYMBOL_GPL(__unwind_start);