1 #include <linux/sched.h>
2 #include <linux/sched/task.h>
3 #include <asm/ptrace.h>
4 #include <asm/bitops.h>
5 #include <asm/stacktrace.h>
6 #include <asm/unwind.h>
8 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
11 * This disables KASAN checking when reading a value from another task's stack,
12 * since the other task could be running on another CPU and could have poisoned
13 * the stack in the meantime.
15 #define READ_ONCE_TASK_STACK(task, x) \
18 if (task == current) \
21 val = READ_ONCE_NOCHECK(x); \
25 static void unwind_dump(struct unwind_state
*state
, unsigned long *sp
)
27 static bool dumped_before
= false;
28 bool prev_zero
, zero
= false;
36 printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
37 state
->stack_info
.type
, state
->stack_info
.next_sp
,
38 state
->stack_mask
, state
->graph_idx
);
40 for (sp
= state
->orig_sp
; sp
< state
->stack_info
.end
; sp
++) {
41 word
= READ_ONCE_NOCHECK(*sp
);
48 printk_deferred("%p: %016x ...\n", sp
, 0);
52 printk_deferred("%p: %016lx (%pB)\n", sp
, word
, (void *)word
);
56 unsigned long unwind_get_return_address(struct unwind_state
*state
)
59 unsigned long *addr_p
= unwind_get_return_address_ptr(state
);
61 if (unwind_done(state
))
64 if (state
->regs
&& user_mode(state
->regs
))
67 addr
= READ_ONCE_TASK_STACK(state
->task
, *addr_p
);
68 addr
= ftrace_graph_ret_addr(state
->task
, &state
->graph_idx
, addr
,
71 return __kernel_text_address(addr
) ? addr
: 0;
73 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
75 static size_t regs_size(struct pt_regs
*regs
)
77 /* x86_32 regs from kernel mode are two words shorter: */
78 if (IS_ENABLED(CONFIG_X86_32
) && !user_mode(regs
))
79 return sizeof(*regs
) - 2*sizeof(long);
84 static bool is_last_task_frame(struct unwind_state
*state
)
86 unsigned long bp
= (unsigned long)state
->bp
;
87 unsigned long regs
= (unsigned long)task_pt_regs(state
->task
);
90 * We have to check for the last task frame at two different locations
91 * because gcc can occasionally decide to realign the stack pointer and
92 * change the offset of the stack frame by a word in the prologue of a
93 * function called by head/entry code.
95 return bp
== regs
- FRAME_HEADER_SIZE
||
96 bp
== regs
- FRAME_HEADER_SIZE
- sizeof(long);
100 * This determines if the frame pointer actually contains an encoded pointer to
101 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
103 static struct pt_regs
*decode_frame_pointer(unsigned long *bp
)
105 unsigned long regs
= (unsigned long)bp
;
110 return (struct pt_regs
*)(regs
& ~0x1);
113 static bool update_stack_state(struct unwind_state
*state
, void *addr
,
116 struct stack_info
*info
= &state
->stack_info
;
117 enum stack_type orig_type
= info
->type
;
120 * If addr isn't on the current stack, switch to the next one.
122 * We may have to traverse multiple stacks to deal with the possibility
123 * that 'info->next_sp' could point to an empty stack and 'addr' could
124 * be on a subsequent stack.
126 while (!on_stack(info
, addr
, len
))
127 if (get_stack_info(info
->next_sp
, state
->task
, info
,
131 if (!state
->orig_sp
|| info
->type
!= orig_type
)
132 state
->orig_sp
= addr
;
137 bool unwind_next_frame(struct unwind_state
*state
)
139 struct pt_regs
*regs
;
140 unsigned long *next_bp
, *next_frame
;
142 enum stack_type prev_type
= state
->stack_info
.type
;
144 if (unwind_done(state
))
147 /* have we reached the end? */
148 if (state
->regs
&& user_mode(state
->regs
))
151 if (is_last_task_frame(state
)) {
152 regs
= task_pt_regs(state
->task
);
155 * kthreads (other than the boot CPU's idle thread) have some
156 * partial regs at the end of their stack which were placed
157 * there by copy_thread_tls(). But the regs don't have any
158 * useful information, so we can skip them.
160 * This user_mode() check is slightly broader than a PF_KTHREAD
161 * check because it also catches the awkward situation where a
162 * newly forked kthread transitions into a user task by calling
163 * do_execve(), which eventually clears PF_KTHREAD.
165 if (!user_mode(regs
))
169 * We're almost at the end, but not quite: there's still the
170 * syscall regs frame. Entry code doesn't encode the regs
171 * pointer for syscalls, so we have to set it manually.
178 /* get the next frame pointer */
180 next_bp
= (unsigned long *)state
->regs
->bp
;
182 next_bp
= (unsigned long *)READ_ONCE_TASK_STACK(state
->task
,*state
->bp
);
184 /* is the next frame pointer an encoded pointer to pt_regs? */
185 regs
= decode_frame_pointer(next_bp
);
187 next_frame
= (unsigned long *)regs
;
188 next_len
= sizeof(*regs
);
190 next_frame
= next_bp
;
191 next_len
= FRAME_HEADER_SIZE
;
194 /* make sure the next frame's data is accessible */
195 if (!update_stack_state(state
, next_frame
, next_len
)) {
197 * Don't warn on bad regs->bp. An interrupt in entry code
198 * might cause a false positive warning.
206 /* Make sure it only unwinds up and doesn't overlap the last frame: */
207 if (state
->stack_info
.type
== prev_type
) {
208 if (state
->regs
&& (void *)next_frame
< (void *)state
->regs
+ regs_size(state
->regs
))
211 if (state
->bp
&& (void *)next_frame
< (void *)state
->bp
+ FRAME_HEADER_SIZE
)
215 /* move to the next frame */
228 * When unwinding a non-current task, the task might actually be
229 * running on another CPU, in which case it could be modifying its
230 * stack while we're reading it. This is generally not a problem and
231 * can be ignored as long as the caller understands that unwinding
232 * another task will not always succeed.
234 if (state
->task
!= current
)
238 printk_deferred_once(KERN_WARNING
239 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
240 state
->regs
, state
->task
->comm
,
241 state
->task
->pid
, next_frame
);
242 unwind_dump(state
, (unsigned long *)state
->regs
);
244 printk_deferred_once(KERN_WARNING
245 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
246 state
->bp
, state
->task
->comm
,
247 state
->task
->pid
, next_frame
);
248 unwind_dump(state
, state
->bp
);
251 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
254 EXPORT_SYMBOL_GPL(unwind_next_frame
);
256 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
257 struct pt_regs
*regs
, unsigned long *first_frame
)
259 unsigned long *bp
, *frame
;
262 memset(state
, 0, sizeof(*state
));
265 /* don't even attempt to start from user mode regs */
266 if (regs
&& user_mode(regs
)) {
267 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
271 /* set up the starting stack frame */
272 bp
= get_frame_pointer(task
, regs
);
273 regs
= decode_frame_pointer(bp
);
276 frame
= (unsigned long *)regs
;
281 len
= FRAME_HEADER_SIZE
;
284 /* initialize stack info and make sure the frame data is accessible */
285 get_stack_info(frame
, state
->task
, &state
->stack_info
,
287 update_stack_state(state
, frame
, len
);
290 * The caller can provide the address of the first frame directly
291 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
292 * to start unwinding at. Skip ahead until we reach it.
294 while (!unwind_done(state
) &&
295 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
296 state
->bp
< first_frame
))
297 unwind_next_frame(state
);
299 EXPORT_SYMBOL_GPL(__unwind_start
);