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)
10 * This disables KASAN checking when reading a value from another task's stack,
11 * since the other task could be running on another CPU and could have poisoned
12 * the stack in the meantime.
14 #define READ_ONCE_TASK_STACK(task, x) \
17 if (task == current) \
20 val = READ_ONCE_NOCHECK(x); \
24 unsigned long unwind_get_return_address(struct unwind_state
*state
)
27 unsigned long *addr_p
= unwind_get_return_address_ptr(state
);
29 if (unwind_done(state
))
32 addr
= READ_ONCE_TASK_STACK(state
->task
, *addr_p
);
33 addr
= ftrace_graph_ret_addr(state
->task
, &state
->graph_idx
, addr
,
36 return __kernel_text_address(addr
) ? addr
: 0;
38 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
40 static bool update_stack_state(struct unwind_state
*state
, void *addr
,
43 struct stack_info
*info
= &state
->stack_info
;
46 * If addr isn't on the current stack, switch to the next one.
48 * We may have to traverse multiple stacks to deal with the possibility
49 * that 'info->next_sp' could point to an empty stack and 'addr' could
50 * be on a subsequent stack.
52 while (!on_stack(info
, addr
, len
))
53 if (get_stack_info(info
->next_sp
, state
->task
, info
,
60 bool unwind_next_frame(struct unwind_state
*state
)
62 unsigned long *next_bp
;
64 if (unwind_done(state
))
67 next_bp
= (unsigned long *)READ_ONCE_TASK_STACK(state
->task
,*state
->bp
);
69 /* make sure the next frame's data is accessible */
70 if (!update_stack_state(state
, next_bp
, FRAME_HEADER_SIZE
))
73 /* move to the next frame */
77 EXPORT_SYMBOL_GPL(unwind_next_frame
);
79 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
80 struct pt_regs
*regs
, unsigned long *first_frame
)
82 memset(state
, 0, sizeof(*state
));
85 /* don't even attempt to start from user mode regs */
86 if (regs
&& user_mode(regs
)) {
87 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
91 /* set up the starting stack frame */
92 state
->bp
= get_frame_pointer(task
, regs
);
94 /* initialize stack info and make sure the frame data is accessible */
95 get_stack_info(state
->bp
, state
->task
, &state
->stack_info
,
97 update_stack_state(state
, state
->bp
, FRAME_HEADER_SIZE
);
100 * The caller can provide the address of the first frame directly
101 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
102 * to start unwinding at. Skip ahead until we reach it.
104 while (!unwind_done(state
) &&
105 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
106 state
->bp
< first_frame
))
107 unwind_next_frame(state
);
109 EXPORT_SYMBOL_GPL(__unwind_start
);