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>
9 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
12 * This disables KASAN checking when reading a value from another task's stack,
13 * since the other task could be running on another CPU and could have poisoned
14 * the stack in the meantime.
16 #define READ_ONCE_TASK_STACK(task, x) \
19 if (task == current) \
22 val = READ_ONCE_NOCHECK(x); \
26 static void unwind_dump(struct unwind_state *state, unsigned long *sp)
28 static bool dumped_before = false;
29 bool prev_zero, zero = false;
37 printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
38 state->stack_info.type, state->stack_info.next_sp,
39 state->stack_mask, state->graph_idx);
41 for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
42 word = READ_ONCE_NOCHECK(*sp);
49 printk_deferred("%p: %016x ...\n", sp, 0);
53 printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
57 unsigned long unwind_get_return_address(struct unwind_state *state)
60 unsigned long *addr_p = unwind_get_return_address_ptr(state);
62 if (unwind_done(state))
65 if (state->regs && user_mode(state->regs))
68 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
69 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
72 return __kernel_text_address(addr) ? addr : 0;
74 EXPORT_SYMBOL_GPL(unwind_get_return_address);
76 static size_t regs_size(struct pt_regs *regs)
78 /* x86_32 regs from kernel mode are two words shorter: */
79 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
80 return sizeof(*regs) - 2*sizeof(long);
86 #define GCC_REALIGN_WORDS 3
88 #define GCC_REALIGN_WORDS 1
91 static bool is_last_task_frame(struct unwind_state *state)
93 unsigned long *last_bp = (unsigned long *)task_pt_regs(state->task) - 2;
94 unsigned long *aligned_bp = last_bp - GCC_REALIGN_WORDS;
97 * We have to check for the last task frame at two different locations
98 * because gcc can occasionally decide to realign the stack pointer and
99 * change the offset of the stack frame in the prologue of a function
100 * called by head/entry code. Examples:
105 * and $0xfffffff8,%esp
110 * <x86_64_start_kernel>:
112 * and $0xfffffffffffffff0,%rsp
117 * Note that after aligning the stack, it pushes a duplicate copy of
118 * the return address before pushing the frame pointer.
120 return (state->bp == last_bp ||
121 (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1)));
125 * This determines if the frame pointer actually contains an encoded pointer to
126 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
128 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
130 unsigned long regs = (unsigned long)bp;
135 return (struct pt_regs *)(regs & ~0x1);
138 static bool update_stack_state(struct unwind_state *state, void *addr,
141 struct stack_info *info = &state->stack_info;
142 enum stack_type orig_type = info->type;
145 * If addr isn't on the current stack, switch to the next one.
147 * 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.
151 while (!on_stack(info, addr, len))
152 if (get_stack_info(info->next_sp, state->task, info,
156 if (!state->orig_sp || info->type != orig_type)
157 state->orig_sp = addr;
162 bool unwind_next_frame(struct unwind_state *state)
164 struct pt_regs *regs;
165 unsigned long *next_bp, *next_frame;
167 enum stack_type prev_type = state->stack_info.type;
169 if (unwind_done(state))
172 /* have we reached the end? */
173 if (state->regs && user_mode(state->regs))
176 if (is_last_task_frame(state)) {
177 regs = task_pt_regs(state->task);
180 * kthreads (other than the boot CPU's idle thread) have some
181 * partial regs at the end of their stack which were placed
182 * there by copy_thread_tls(). But the regs don't have any
183 * useful information, so we can skip them.
185 * This user_mode() check is slightly broader than a PF_KTHREAD
186 * check because it also catches the awkward situation where a
187 * newly forked kthread transitions into a user task by calling
188 * do_execve(), which eventually clears PF_KTHREAD.
190 if (!user_mode(regs))
194 * We're almost at the end, but not quite: there's still the
195 * syscall regs frame. Entry code doesn't encode the regs
196 * pointer for syscalls, so we have to set it manually.
203 /* get the next frame pointer */
205 next_bp = (unsigned long *)state->regs->bp;
207 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
209 /* is the next frame pointer an encoded pointer to pt_regs? */
210 regs = decode_frame_pointer(next_bp);
212 next_frame = (unsigned long *)regs;
213 next_len = sizeof(*regs);
215 next_frame = next_bp;
216 next_len = FRAME_HEADER_SIZE;
219 /* make sure the next frame's data is accessible */
220 if (!update_stack_state(state, next_frame, next_len)) {
222 * Don't warn on bad regs->bp. An interrupt in entry code
223 * might cause a false positive warning.
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))
236 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
240 /* move to the next frame */
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.
259 if (state->task != current)
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,
266 state->task->pid, next_frame);
267 unwind_dump(state, (unsigned long *)state->regs);
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,
272 state->task->pid, next_frame);
273 unwind_dump(state, state->bp);
276 state->stack_info.type = STACK_TYPE_UNKNOWN;
279 EXPORT_SYMBOL_GPL(unwind_next_frame);
281 void __unwind_start(struct unwind_state *state, struct task_struct *task,
282 struct pt_regs *regs, unsigned long *first_frame)
284 unsigned long *bp, *frame;
287 memset(state, 0, sizeof(*state));
290 /* don't even attempt to start from user mode regs */
291 if (regs && user_mode(regs)) {
292 state->stack_info.type = STACK_TYPE_UNKNOWN;
296 /* set up the starting stack frame */
297 bp = get_frame_pointer(task, regs);
298 regs = decode_frame_pointer(bp);
301 frame = (unsigned long *)regs;
306 len = FRAME_HEADER_SIZE;
309 /* initialize stack info and make sure the frame data is accessible */
310 get_stack_info(frame, state->task, &state->stack_info,
312 update_stack_state(state, frame, len);
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.
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);
324 EXPORT_SYMBOL_GPL(__unwind_start);