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;
108 #define GCC_REALIGN_WORDS 3
110 #define GCC_REALIGN_WORDS 1
113 static inline unsigned long *last_aligned_frame(struct unwind_state *state)
115 return last_frame(state) - GCC_REALIGN_WORDS;
118 static bool is_last_task_frame(struct unwind_state *state)
120 unsigned long *last_bp = last_frame(state);
121 unsigned long *aligned_bp = last_aligned_frame(state);
124 * We have to check for the last task frame at two different locations
125 * because gcc can occasionally decide to realign the stack pointer and
126 * change the offset of the stack frame in the prologue of a function
127 * called by head/entry code. Examples:
132 * and $0xfffffff8,%esp
137 * <x86_64_start_kernel>:
139 * and $0xfffffffffffffff0,%rsp
144 * Note that after aligning the stack, it pushes a duplicate copy of
145 * the return address before pushing the frame pointer.
147 return (state->bp == last_bp ||
148 (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1)));
152 * This determines if the frame pointer actually contains an encoded pointer to
153 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
155 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
157 unsigned long regs = (unsigned long)bp;
162 return (struct pt_regs *)(regs & ~0x1);
165 static bool update_stack_state(struct unwind_state *state,
166 unsigned long *next_bp)
168 struct stack_info *info = &state->stack_info;
169 enum stack_type prev_type = info->type;
170 struct pt_regs *regs;
171 unsigned long *frame, *prev_frame_end, *addr_p, addr;
175 prev_frame_end = (void *)state->regs + regs_size(state->regs);
177 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
179 /* Is the next frame pointer an encoded pointer to pt_regs? */
180 regs = decode_frame_pointer(next_bp);
182 frame = (unsigned long *)regs;
183 len = regs_size(regs);
184 state->got_irq = true;
187 len = FRAME_HEADER_SIZE;
191 * If the next bp isn't on the current stack, switch to the next one.
193 * We may have to traverse multiple stacks to deal with the possibility
194 * that info->next_sp could point to an empty stack and the next bp
195 * could be on a subsequent stack.
197 while (!on_stack(info, frame, len))
198 if (get_stack_info(info->next_sp, state->task, info,
202 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
203 if (state->orig_sp && state->stack_info.type == prev_type &&
204 frame < prev_frame_end)
207 /* Move state to the next frame: */
216 /* Save the return address: */
217 if (state->regs && user_mode(state->regs))
220 addr_p = unwind_get_return_address_ptr(state);
221 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
222 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
226 /* Save the original stack pointer for unwind_dump(): */
228 state->orig_sp = frame;
233 bool unwind_next_frame(struct unwind_state *state)
235 struct pt_regs *regs;
236 unsigned long *next_bp;
238 if (unwind_done(state))
241 /* Have we reached the end? */
242 if (state->regs && user_mode(state->regs))
245 if (is_last_task_frame(state)) {
246 regs = task_pt_regs(state->task);
249 * kthreads (other than the boot CPU's idle thread) have some
250 * partial regs at the end of their stack which were placed
251 * there by copy_thread_tls(). But the regs don't have any
252 * useful information, so we can skip them.
254 * This user_mode() check is slightly broader than a PF_KTHREAD
255 * check because it also catches the awkward situation where a
256 * newly forked kthread transitions into a user task by calling
257 * do_execve(), which eventually clears PF_KTHREAD.
259 if (!user_mode(regs))
263 * We're almost at the end, but not quite: there's still the
264 * syscall regs frame. Entry code doesn't encode the regs
265 * pointer for syscalls, so we have to set it manually.
273 /* Get the next frame pointer: */
275 next_bp = (unsigned long *)state->regs->bp;
277 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
279 /* Move to the next frame if it's safe: */
280 if (!update_stack_state(state, next_bp))
289 * When unwinding a non-current task, the task might actually be
290 * running on another CPU, in which case it could be modifying its
291 * stack while we're reading it. This is generally not a problem and
292 * can be ignored as long as the caller understands that unwinding
293 * another task will not always succeed.
295 if (state->task != current)
299 * Don't warn if the unwinder got lost due to an interrupt in entry
300 * code or in the C handler before the first frame pointer got set up:
302 if (state->got_irq && in_entry_code(state->ip))
305 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
306 state->regs->sp < (unsigned long)task_pt_regs(state->task))
310 printk_deferred_once(KERN_WARNING
311 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
312 state->regs, state->task->comm,
313 state->task->pid, next_bp);
316 printk_deferred_once(KERN_WARNING
317 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
318 state->bp, state->task->comm,
319 state->task->pid, next_bp);
323 state->stack_info.type = STACK_TYPE_UNKNOWN;
326 EXPORT_SYMBOL_GPL(unwind_next_frame);
328 void __unwind_start(struct unwind_state *state, struct task_struct *task,
329 struct pt_regs *regs, unsigned long *first_frame)
333 memset(state, 0, sizeof(*state));
335 state->got_irq = (regs);
337 /* Don't even attempt to start from user mode regs: */
338 if (regs && user_mode(regs)) {
339 state->stack_info.type = STACK_TYPE_UNKNOWN;
343 bp = get_frame_pointer(task, regs);
345 /* Initialize stack info and make sure the frame data is accessible: */
346 get_stack_info(bp, state->task, &state->stack_info,
348 update_stack_state(state, bp);
351 * The caller can provide the address of the first frame directly
352 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
353 * to start unwinding at. Skip ahead until we reach it.
355 while (!unwind_done(state) &&
356 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
357 state->bp < first_frame))
358 unwind_next_frame(state);
360 EXPORT_SYMBOL_GPL(__unwind_start);