blob: 4f0e17b90463a653db8f49122fc355151b600465 [file] [log] [blame]
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -05001#include <linux/sched.h>
2#include <linux/ftrace.h>
3#include <asm/ptrace.h>
4#include <asm/bitops.h>
5#include <asm/stacktrace.h>
6#include <asm/unwind.h>
7
Josh Poimboeufcfee9ed2016-10-06 00:28:40 -05008unsigned long unwind_get_return_address(struct unwind_state *state)
9{
Josh Poimboeuf55f856e2016-11-27 23:13:07 -060010 unsigned long addr;
Josh Poimboeufc2d75e02016-11-17 09:57:23 -060011
Josh Poimboeufcfee9ed2016-10-06 00:28:40 -050012 if (unwind_done(state))
13 return 0;
14
Josh Poimboeuf55f856e2016-11-27 23:13:07 -060015 addr = READ_ONCE_NOCHECK(*state->sp);
16
Josh Poimboeufcfee9ed2016-10-06 00:28:40 -050017 return ftrace_graph_ret_addr(state->task, &state->graph_idx,
Josh Poimboeufc2d75e02016-11-17 09:57:23 -060018 addr, state->sp);
Josh Poimboeufcfee9ed2016-10-06 00:28:40 -050019}
20EXPORT_SYMBOL_GPL(unwind_get_return_address);
21
Josh Poimboeufee9f8fc2017-07-24 18:36:57 -050022unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
23{
24 return NULL;
25}
26
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -050027bool unwind_next_frame(struct unwind_state *state)
28{
29 struct stack_info *info = &state->stack_info;
30
31 if (unwind_done(state))
32 return false;
33
34 do {
Josh Poimboeuf55f856e2016-11-27 23:13:07 -060035 for (state->sp++; state->sp < info->end; state->sp++) {
36 unsigned long addr = READ_ONCE_NOCHECK(*state->sp);
Josh Poimboeufc2d75e02016-11-17 09:57:23 -060037
Josh Poimboeufc2d75e02016-11-17 09:57:23 -060038 if (__kernel_text_address(addr))
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -050039 return true;
Josh Poimboeuf55f856e2016-11-27 23:13:07 -060040 }
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -050041
Josh Poimboeufe335bb52017-04-17 08:44:00 -050042 state->sp = PTR_ALIGN(info->next_sp, sizeof(long));
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -050043
44 } while (!get_stack_info(state->sp, state->task, info,
45 &state->stack_mask));
46
47 return false;
48}
49EXPORT_SYMBOL_GPL(unwind_next_frame);
50
51void __unwind_start(struct unwind_state *state, struct task_struct *task,
52 struct pt_regs *regs, unsigned long *first_frame)
53{
54 memset(state, 0, sizeof(*state));
55
56 state->task = task;
Josh Poimboeufe335bb52017-04-17 08:44:00 -050057 state->sp = PTR_ALIGN(first_frame, sizeof(long));
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -050058
59 get_stack_info(first_frame, state->task, &state->stack_info,
60 &state->stack_mask);
61
Josh Poimboeuf7fbe6ac2016-10-24 08:31:27 -050062 /*
63 * The caller can provide the address of the first frame directly
64 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
65 * to start unwinding at. Skip ahead until we reach it.
66 */
67 if (!unwind_done(state) &&
68 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
69 !__kernel_text_address(*first_frame)))
Josh Poimboeuf7c7900f2016-09-16 14:18:12 -050070 unwind_next_frame(state);
71}
72EXPORT_SYMBOL_GPL(__unwind_start);