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)
13 unsigned long unwind_get_return_address(struct unwind_state
*state
)
15 if (unwind_done(state
))
18 return __kernel_text_address(state
->ip
) ? state
->ip
: 0;
20 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
22 unsigned long *unwind_get_return_address_ptr(struct unwind_state
*state
)
24 if (unwind_done(state
))
27 return state
->regs
? &state
->regs
->ip
: state
->bp
+ 1;
30 static void unwind_dump(struct unwind_state
*state
)
32 static bool dumped_before
= false;
33 bool prev_zero
, zero
= false;
34 unsigned long word
, *sp
;
35 struct stack_info stack_info
= {0};
36 unsigned long visit_mask
= 0;
43 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
44 state
->stack_info
.type
, state
->stack_info
.next_sp
,
45 state
->stack_mask
, state
->graph_idx
);
47 for (sp
= state
->orig_sp
; sp
; sp
= PTR_ALIGN(stack_info
.next_sp
, sizeof(long))) {
48 if (get_stack_info(sp
, state
->task
, &stack_info
, &visit_mask
))
51 for (; sp
< stack_info
.end
; sp
++) {
53 word
= READ_ONCE_NOCHECK(*sp
);
60 printk_deferred("%p: %0*x ...\n",
61 sp
, BITS_PER_LONG
/4, 0);
65 printk_deferred("%p: %0*lx (%pB)\n",
66 sp
, BITS_PER_LONG
/4, word
, (void *)word
);
71 static size_t regs_size(struct pt_regs
*regs
)
73 /* x86_32 regs from kernel mode are two words shorter: */
74 if (IS_ENABLED(CONFIG_X86_32
) && !user_mode(regs
))
75 return sizeof(*regs
) - 2*sizeof(long);
80 static bool in_entry_code(unsigned long ip
)
82 char *addr
= (char *)ip
;
84 if (addr
>= __entry_text_start
&& addr
< __entry_text_end
)
87 if (addr
>= __irqentry_text_start
&& addr
< __irqentry_text_end
)
93 static inline unsigned long *last_frame(struct unwind_state
*state
)
95 return (unsigned long *)task_pt_regs(state
->task
) - 2;
98 static bool is_last_frame(struct unwind_state
*state
)
100 return state
->bp
== last_frame(state
);
104 #define GCC_REALIGN_WORDS 3
106 #define GCC_REALIGN_WORDS 1
109 static inline unsigned long *last_aligned_frame(struct unwind_state
*state
)
111 return last_frame(state
) - GCC_REALIGN_WORDS
;
114 static bool is_last_aligned_frame(struct unwind_state
*state
)
116 unsigned long *last_bp
= last_frame(state
);
117 unsigned long *aligned_bp
= last_aligned_frame(state
);
120 * GCC can occasionally decide to realign the stack pointer and change
121 * the offset of the stack frame in the prologue of a function called
122 * by head/entry code. Examples:
127 * and $0xfffffff8,%esp
132 * <x86_64_start_kernel>:
134 * and $0xfffffffffffffff0,%rsp
139 * After aligning the stack, it pushes a duplicate copy of the return
140 * address before pushing the frame pointer.
142 return (state
->bp
== aligned_bp
&& *(aligned_bp
+ 1) == *(last_bp
+ 1));
145 static bool is_last_ftrace_frame(struct unwind_state
*state
)
147 unsigned long *last_bp
= last_frame(state
);
148 unsigned long *last_ftrace_bp
= last_bp
- 3;
151 * When unwinding from an ftrace handler of a function called by entry
152 * code, the stack layout of the last frame is:
162 return (state
->bp
== last_ftrace_bp
&&
163 *state
->bp
== *(state
->bp
+ 2) &&
164 *(state
->bp
+ 1) == *(state
->bp
+ 4));
167 static bool is_last_task_frame(struct unwind_state
*state
)
169 return is_last_frame(state
) || is_last_aligned_frame(state
) ||
170 is_last_ftrace_frame(state
);
174 * This determines if the frame pointer actually contains an encoded pointer to
175 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
177 static struct pt_regs
*decode_frame_pointer(unsigned long *bp
)
179 unsigned long regs
= (unsigned long)bp
;
184 return (struct pt_regs
*)(regs
& ~0x1);
188 #define KERNEL_REGS_SIZE (sizeof(struct pt_regs) - 2*sizeof(long))
190 #define KERNEL_REGS_SIZE (sizeof(struct pt_regs))
193 static bool update_stack_state(struct unwind_state
*state
,
194 unsigned long *next_bp
)
196 struct stack_info
*info
= &state
->stack_info
;
197 enum stack_type prev_type
= info
->type
;
198 struct pt_regs
*regs
;
199 unsigned long *frame
, *prev_frame_end
, *addr_p
, addr
;
203 prev_frame_end
= (void *)state
->regs
+ regs_size(state
->regs
);
205 prev_frame_end
= (void *)state
->bp
+ FRAME_HEADER_SIZE
;
207 /* Is the next frame pointer an encoded pointer to pt_regs? */
208 regs
= decode_frame_pointer(next_bp
);
210 frame
= (unsigned long *)regs
;
211 len
= KERNEL_REGS_SIZE
;
212 state
->got_irq
= true;
215 len
= FRAME_HEADER_SIZE
;
219 * If the next bp isn't on the current stack, switch to the next one.
221 * We may have to traverse multiple stacks to deal with the possibility
222 * that info->next_sp could point to an empty stack and the next bp
223 * could be on a subsequent stack.
225 while (!on_stack(info
, frame
, len
))
226 if (get_stack_info(info
->next_sp
, state
->task
, info
,
230 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
231 if (state
->orig_sp
&& state
->stack_info
.type
== prev_type
&&
232 frame
< prev_frame_end
)
236 * On 32-bit with user mode regs, make sure the last two regs are safe
239 if (IS_ENABLED(CONFIG_X86_32
) && regs
&& user_mode(regs
) &&
240 !on_stack(info
, frame
, len
+ 2*sizeof(long)))
243 /* Move state to the next frame: */
252 /* Save the return address: */
253 if (state
->regs
&& user_mode(state
->regs
))
256 addr_p
= unwind_get_return_address_ptr(state
);
257 addr
= READ_ONCE_TASK_STACK(state
->task
, *addr_p
);
258 state
->ip
= ftrace_graph_ret_addr(state
->task
, &state
->graph_idx
,
262 /* Save the original stack pointer for unwind_dump(): */
264 state
->orig_sp
= frame
;
269 bool unwind_next_frame(struct unwind_state
*state
)
271 struct pt_regs
*regs
;
272 unsigned long *next_bp
;
274 if (unwind_done(state
))
277 /* Have we reached the end? */
278 if (state
->regs
&& user_mode(state
->regs
))
281 if (is_last_task_frame(state
)) {
282 regs
= task_pt_regs(state
->task
);
285 * kthreads (other than the boot CPU's idle thread) have some
286 * partial regs at the end of their stack which were placed
287 * there by copy_thread_tls(). But the regs don't have any
288 * useful information, so we can skip them.
290 * This user_mode() check is slightly broader than a PF_KTHREAD
291 * check because it also catches the awkward situation where a
292 * newly forked kthread transitions into a user task by calling
293 * do_execve(), which eventually clears PF_KTHREAD.
295 if (!user_mode(regs
))
299 * We're almost at the end, but not quite: there's still the
300 * syscall regs frame. Entry code doesn't encode the regs
301 * pointer for syscalls, so we have to set it manually.
309 /* Get the next frame pointer: */
311 next_bp
= (unsigned long *)state
->regs
->bp
;
313 next_bp
= (unsigned long *)READ_ONCE_TASK_STACK(state
->task
, *state
->bp
);
315 /* Move to the next frame if it's safe: */
316 if (!update_stack_state(state
, next_bp
))
325 * When unwinding a non-current task, the task might actually be
326 * running on another CPU, in which case it could be modifying its
327 * stack while we're reading it. This is generally not a problem and
328 * can be ignored as long as the caller understands that unwinding
329 * another task will not always succeed.
331 if (state
->task
!= current
)
335 * Don't warn if the unwinder got lost due to an interrupt in entry
336 * code or in the C handler before the first frame pointer got set up:
338 if (state
->got_irq
&& in_entry_code(state
->ip
))
341 state
->regs
->sp
>= (unsigned long)last_aligned_frame(state
) &&
342 state
->regs
->sp
< (unsigned long)task_pt_regs(state
->task
))
346 printk_deferred_once(KERN_WARNING
347 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
348 state
->regs
, state
->task
->comm
,
349 state
->task
->pid
, next_bp
);
352 printk_deferred_once(KERN_WARNING
353 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
354 state
->bp
, state
->task
->comm
,
355 state
->task
->pid
, next_bp
);
359 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
362 EXPORT_SYMBOL_GPL(unwind_next_frame
);
364 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
365 struct pt_regs
*regs
, unsigned long *first_frame
)
369 memset(state
, 0, sizeof(*state
));
371 state
->got_irq
= (regs
);
373 /* Don't even attempt to start from user mode regs: */
374 if (regs
&& user_mode(regs
)) {
375 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
379 bp
= get_frame_pointer(task
, regs
);
381 /* Initialize stack info and make sure the frame data is accessible: */
382 get_stack_info(bp
, state
->task
, &state
->stack_info
,
384 update_stack_state(state
, bp
);
387 * The caller can provide the address of the first frame directly
388 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
389 * to start unwinding at. Skip ahead until we reach it.
391 while (!unwind_done(state
) &&
392 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
393 state
->bp
< first_frame
))
394 unwind_next_frame(state
);
396 EXPORT_SYMBOL_GPL(__unwind_start
);