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 unsigned long unwind_get_return_address(struct unwind_state *state)
12 unsigned long *addr_p = unwind_get_return_address_ptr(state);
14 if (unwind_done(state))
17 if (state->regs && user_mode(state->regs))
20 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p,
23 if (!__kernel_text_address(addr)) {
24 printk_deferred_once(KERN_WARNING
25 "WARNING: unrecognized kernel stack return address %p at %p in %s:%d\n",
26 (void *)addr, addr_p, state->task->comm,
33 EXPORT_SYMBOL_GPL(unwind_get_return_address);
35 static size_t regs_size(struct pt_regs *regs)
37 /* x86_32 regs from kernel mode are two words shorter: */
38 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
39 return sizeof(*regs) - 2*sizeof(long);
44 static bool is_last_task_frame(struct unwind_state *state)
46 unsigned long bp = (unsigned long)state->bp;
47 unsigned long regs = (unsigned long)task_pt_regs(state->task);
49 return bp == regs - FRAME_HEADER_SIZE;
53 * This determines if the frame pointer actually contains an encoded pointer to
54 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
56 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
58 unsigned long regs = (unsigned long)bp;
63 return (struct pt_regs *)(regs & ~0x1);
66 static bool update_stack_state(struct unwind_state *state, void *addr,
69 struct stack_info *info = &state->stack_info;
72 * If addr isn't on the current stack, switch to the next one.
74 * We may have to traverse multiple stacks to deal with the possibility
75 * that 'info->next_sp' could point to an empty stack and 'addr' could
76 * be on a subsequent stack.
78 while (!on_stack(info, addr, len))
79 if (get_stack_info(info->next_sp, state->task, info,
86 bool unwind_next_frame(struct unwind_state *state)
89 unsigned long *next_bp, *next_frame;
91 enum stack_type prev_type = state->stack_info.type;
93 if (unwind_done(state))
96 /* have we reached the end? */
97 if (state->regs && user_mode(state->regs))
100 if (is_last_task_frame(state)) {
101 regs = task_pt_regs(state->task);
104 * kthreads (other than the boot CPU's idle thread) have some
105 * partial regs at the end of their stack which were placed
106 * there by copy_thread_tls(). But the regs don't have any
107 * useful information, so we can skip them.
109 * This user_mode() check is slightly broader than a PF_KTHREAD
110 * check because it also catches the awkward situation where a
111 * newly forked kthread transitions into a user task by calling
112 * do_execve(), which eventually clears PF_KTHREAD.
114 if (!user_mode(regs))
118 * We're almost at the end, but not quite: there's still the
119 * syscall regs frame. Entry code doesn't encode the regs
120 * pointer for syscalls, so we have to set it manually.
127 /* get the next frame pointer */
129 next_bp = (unsigned long *)state->regs->bp;
131 next_bp = (unsigned long *)*state->bp;
133 /* is the next frame pointer an encoded pointer to pt_regs? */
134 regs = decode_frame_pointer(next_bp);
136 next_frame = (unsigned long *)regs;
137 next_len = sizeof(*regs);
139 next_frame = next_bp;
140 next_len = FRAME_HEADER_SIZE;
143 /* make sure the next frame's data is accessible */
144 if (!update_stack_state(state, next_frame, next_len)) {
146 * Don't warn on bad regs->bp. An interrupt in entry code
147 * might cause a false positive warning.
155 /* Make sure it only unwinds up and doesn't overlap the last frame: */
156 if (state->stack_info.type == prev_type) {
157 if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
160 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
164 /* move to the next frame */
177 printk_deferred_once(KERN_WARNING
178 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
179 state->regs, state->task->comm,
180 state->task->pid, next_frame);
182 printk_deferred_once(KERN_WARNING
183 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
184 state->bp, state->task->comm,
185 state->task->pid, next_frame);
188 state->stack_info.type = STACK_TYPE_UNKNOWN;
191 EXPORT_SYMBOL_GPL(unwind_next_frame);
193 void __unwind_start(struct unwind_state *state, struct task_struct *task,
194 struct pt_regs *regs, unsigned long *first_frame)
196 unsigned long *bp, *frame;
199 memset(state, 0, sizeof(*state));
202 /* don't even attempt to start from user mode regs */
203 if (regs && user_mode(regs)) {
204 state->stack_info.type = STACK_TYPE_UNKNOWN;
208 /* set up the starting stack frame */
209 bp = get_frame_pointer(task, regs);
210 regs = decode_frame_pointer(bp);
213 frame = (unsigned long *)regs;
218 len = FRAME_HEADER_SIZE;
221 /* initialize stack info and make sure the frame data is accessible */
222 get_stack_info(frame, state->task, &state->stack_info,
224 update_stack_state(state, frame, len);
227 * The caller can provide the address of the first frame directly
228 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
229 * to start unwinding at. Skip ahead until we reach it.
231 while (!unwind_done(state) &&
232 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
233 state->bp < first_frame))
234 unwind_next_frame(state);
236 EXPORT_SYMBOL_GPL(__unwind_start);