1 #include <linux/sched.h>
2 #include <asm/ptrace.h>
3 #include <asm/bitops.h>
4 #include <asm/stacktrace.h>
5 #include <asm/unwind.h>
7 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
9 static void unwind_dump(struct unwind_state *state, unsigned long *sp)
11 static bool dumped_before = false;
12 bool prev_zero, zero = false;
20 printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
21 state->stack_info.type, state->stack_info.next_sp,
22 state->stack_mask, state->graph_idx);
24 for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
25 word = READ_ONCE_NOCHECK(*sp);
32 printk_deferred("%p: %016x ...\n", sp, 0);
36 printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
40 unsigned long unwind_get_return_address(struct unwind_state *state)
43 unsigned long *addr_p = unwind_get_return_address_ptr(state);
45 if (unwind_done(state))
48 if (state->regs && user_mode(state->regs))
51 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p,
54 return __kernel_text_address(addr) ? addr : 0;
56 EXPORT_SYMBOL_GPL(unwind_get_return_address);
58 static size_t regs_size(struct pt_regs *regs)
60 /* x86_32 regs from kernel mode are two words shorter: */
61 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
62 return sizeof(*regs) - 2*sizeof(long);
67 static bool is_last_task_frame(struct unwind_state *state)
69 unsigned long bp = (unsigned long)state->bp;
70 unsigned long regs = (unsigned long)task_pt_regs(state->task);
73 * We have to check for the last task frame at two different locations
74 * because gcc can occasionally decide to realign the stack pointer and
75 * change the offset of the stack frame by a word in the prologue of a
76 * function called by head/entry code.
78 return bp == regs - FRAME_HEADER_SIZE ||
79 bp == regs - FRAME_HEADER_SIZE - sizeof(long);
83 * This determines if the frame pointer actually contains an encoded pointer to
84 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
86 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
88 unsigned long regs = (unsigned long)bp;
93 return (struct pt_regs *)(regs & ~0x1);
96 static bool update_stack_state(struct unwind_state *state, void *addr,
99 struct stack_info *info = &state->stack_info;
100 enum stack_type orig_type = info->type;
103 * If addr isn't on the current stack, switch to the next one.
105 * We may have to traverse multiple stacks to deal with the possibility
106 * that 'info->next_sp' could point to an empty stack and 'addr' could
107 * be on a subsequent stack.
109 while (!on_stack(info, addr, len))
110 if (get_stack_info(info->next_sp, state->task, info,
114 if (!state->orig_sp || info->type != orig_type)
115 state->orig_sp = addr;
120 bool unwind_next_frame(struct unwind_state *state)
122 struct pt_regs *regs;
123 unsigned long *next_bp, *next_frame;
125 enum stack_type prev_type = state->stack_info.type;
127 if (unwind_done(state))
130 /* have we reached the end? */
131 if (state->regs && user_mode(state->regs))
134 if (is_last_task_frame(state)) {
135 regs = task_pt_regs(state->task);
138 * kthreads (other than the boot CPU's idle thread) have some
139 * partial regs at the end of their stack which were placed
140 * there by copy_thread_tls(). But the regs don't have any
141 * useful information, so we can skip them.
143 * This user_mode() check is slightly broader than a PF_KTHREAD
144 * check because it also catches the awkward situation where a
145 * newly forked kthread transitions into a user task by calling
146 * do_execve(), which eventually clears PF_KTHREAD.
148 if (!user_mode(regs))
152 * We're almost at the end, but not quite: there's still the
153 * syscall regs frame. Entry code doesn't encode the regs
154 * pointer for syscalls, so we have to set it manually.
161 /* get the next frame pointer */
163 next_bp = (unsigned long *)state->regs->bp;
165 next_bp = (unsigned long *)*state->bp;
167 /* is the next frame pointer an encoded pointer to pt_regs? */
168 regs = decode_frame_pointer(next_bp);
170 next_frame = (unsigned long *)regs;
171 next_len = sizeof(*regs);
173 next_frame = next_bp;
174 next_len = FRAME_HEADER_SIZE;
177 /* make sure the next frame's data is accessible */
178 if (!update_stack_state(state, next_frame, next_len)) {
180 * Don't warn on bad regs->bp. An interrupt in entry code
181 * might cause a false positive warning.
189 /* Make sure it only unwinds up and doesn't overlap the last frame: */
190 if (state->stack_info.type == prev_type) {
191 if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
194 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
198 /* move to the next frame */
211 printk_deferred_once(KERN_WARNING
212 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
213 state->regs, state->task->comm,
214 state->task->pid, next_frame);
215 unwind_dump(state, (unsigned long *)state->regs);
217 printk_deferred_once(KERN_WARNING
218 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
219 state->bp, state->task->comm,
220 state->task->pid, next_frame);
221 unwind_dump(state, state->bp);
224 state->stack_info.type = STACK_TYPE_UNKNOWN;
227 EXPORT_SYMBOL_GPL(unwind_next_frame);
229 void __unwind_start(struct unwind_state *state, struct task_struct *task,
230 struct pt_regs *regs, unsigned long *first_frame)
232 unsigned long *bp, *frame;
235 memset(state, 0, sizeof(*state));
238 /* don't even attempt to start from user mode regs */
239 if (regs && user_mode(regs)) {
240 state->stack_info.type = STACK_TYPE_UNKNOWN;
244 /* set up the starting stack frame */
245 bp = get_frame_pointer(task, regs);
246 regs = decode_frame_pointer(bp);
249 frame = (unsigned long *)regs;
254 len = FRAME_HEADER_SIZE;
257 /* initialize stack info and make sure the frame data is accessible */
258 get_stack_info(frame, state->task, &state->stack_info,
260 update_stack_state(state, frame, len);
263 * The caller can provide the address of the first frame directly
264 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
265 * to start unwinding at. Skip ahead until we reach it.
267 while (!unwind_done(state) &&
268 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
269 state->bp < first_frame))
270 unwind_next_frame(state);
272 EXPORT_SYMBOL_GPL(__unwind_start);