]>
Commit | Line | Data |
---|---|---|
7c7900f8 JP |
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> | |
6 | ||
7 | #define FRAME_HEADER_SIZE (sizeof(long) * 2) | |
8 | ||
9 | unsigned long unwind_get_return_address(struct unwind_state *state) | |
10 | { | |
11 | unsigned long addr; | |
12 | unsigned long *addr_p = unwind_get_return_address_ptr(state); | |
13 | ||
14 | if (unwind_done(state)) | |
15 | return 0; | |
16 | ||
946c1911 JP |
17 | if (state->regs && user_mode(state->regs)) |
18 | return 0; | |
19 | ||
7c7900f8 JP |
20 | addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p, |
21 | addr_p); | |
22 | ||
23 | return __kernel_text_address(addr) ? addr : 0; | |
24 | } | |
25 | EXPORT_SYMBOL_GPL(unwind_get_return_address); | |
26 | ||
acb4608a JP |
27 | static bool is_last_task_frame(struct unwind_state *state) |
28 | { | |
29 | unsigned long bp = (unsigned long)state->bp; | |
30 | unsigned long regs = (unsigned long)task_pt_regs(state->task); | |
31 | ||
32 | return bp == regs - FRAME_HEADER_SIZE; | |
33 | } | |
34 | ||
946c1911 JP |
35 | /* |
36 | * This determines if the frame pointer actually contains an encoded pointer to | |
37 | * pt_regs on the stack. See ENCODE_FRAME_POINTER. | |
38 | */ | |
39 | static struct pt_regs *decode_frame_pointer(unsigned long *bp) | |
40 | { | |
41 | unsigned long regs = (unsigned long)bp; | |
42 | ||
43 | if (!(regs & 0x1)) | |
44 | return NULL; | |
45 | ||
46 | return (struct pt_regs *)(regs & ~0x1); | |
47 | } | |
48 | ||
7c7900f8 JP |
49 | static bool update_stack_state(struct unwind_state *state, void *addr, |
50 | size_t len) | |
51 | { | |
52 | struct stack_info *info = &state->stack_info; | |
53 | ||
54 | /* | |
55 | * If addr isn't on the current stack, switch to the next one. | |
56 | * | |
57 | * We may have to traverse multiple stacks to deal with the possibility | |
58 | * that 'info->next_sp' could point to an empty stack and 'addr' could | |
59 | * be on a subsequent stack. | |
60 | */ | |
61 | while (!on_stack(info, addr, len)) | |
62 | if (get_stack_info(info->next_sp, state->task, info, | |
63 | &state->stack_mask)) | |
64 | return false; | |
65 | ||
66 | return true; | |
67 | } | |
68 | ||
69 | bool unwind_next_frame(struct unwind_state *state) | |
70 | { | |
946c1911 JP |
71 | struct pt_regs *regs; |
72 | unsigned long *next_bp, *next_frame; | |
73 | size_t next_len; | |
7c7900f8 JP |
74 | |
75 | if (unwind_done(state)) | |
76 | return false; | |
77 | ||
946c1911 JP |
78 | /* have we reached the end? */ |
79 | if (state->regs && user_mode(state->regs)) | |
80 | goto the_end; | |
81 | ||
acb4608a JP |
82 | if (is_last_task_frame(state)) { |
83 | regs = task_pt_regs(state->task); | |
84 | ||
85 | /* | |
86 | * kthreads (other than the boot CPU's idle thread) have some | |
87 | * partial regs at the end of their stack which were placed | |
88 | * there by copy_thread_tls(). But the regs don't have any | |
89 | * useful information, so we can skip them. | |
90 | * | |
91 | * This user_mode() check is slightly broader than a PF_KTHREAD | |
92 | * check because it also catches the awkward situation where a | |
93 | * newly forked kthread transitions into a user task by calling | |
94 | * do_execve(), which eventually clears PF_KTHREAD. | |
95 | */ | |
96 | if (!user_mode(regs)) | |
97 | goto the_end; | |
98 | ||
99 | /* | |
100 | * We're almost at the end, but not quite: there's still the | |
101 | * syscall regs frame. Entry code doesn't encode the regs | |
102 | * pointer for syscalls, so we have to set it manually. | |
103 | */ | |
104 | state->regs = regs; | |
105 | state->bp = NULL; | |
106 | return true; | |
107 | } | |
108 | ||
946c1911 JP |
109 | /* get the next frame pointer */ |
110 | if (state->regs) | |
111 | next_bp = (unsigned long *)state->regs->bp; | |
112 | else | |
113 | next_bp = (unsigned long *)*state->bp; | |
114 | ||
115 | /* is the next frame pointer an encoded pointer to pt_regs? */ | |
116 | regs = decode_frame_pointer(next_bp); | |
117 | if (regs) { | |
118 | next_frame = (unsigned long *)regs; | |
119 | next_len = sizeof(*regs); | |
120 | } else { | |
121 | next_frame = next_bp; | |
122 | next_len = FRAME_HEADER_SIZE; | |
123 | } | |
7c7900f8 JP |
124 | |
125 | /* make sure the next frame's data is accessible */ | |
946c1911 | 126 | if (!update_stack_state(state, next_frame, next_len)) |
7c7900f8 | 127 | return false; |
7c7900f8 | 128 | /* move to the next frame */ |
946c1911 JP |
129 | if (regs) { |
130 | state->regs = regs; | |
131 | state->bp = NULL; | |
132 | } else { | |
133 | state->bp = next_bp; | |
134 | state->regs = NULL; | |
135 | } | |
136 | ||
7c7900f8 | 137 | return true; |
946c1911 JP |
138 | |
139 | the_end: | |
140 | state->stack_info.type = STACK_TYPE_UNKNOWN; | |
141 | return false; | |
7c7900f8 JP |
142 | } |
143 | EXPORT_SYMBOL_GPL(unwind_next_frame); | |
144 | ||
145 | void __unwind_start(struct unwind_state *state, struct task_struct *task, | |
146 | struct pt_regs *regs, unsigned long *first_frame) | |
147 | { | |
946c1911 JP |
148 | unsigned long *bp, *frame; |
149 | size_t len; | |
150 | ||
7c7900f8 JP |
151 | memset(state, 0, sizeof(*state)); |
152 | state->task = task; | |
153 | ||
154 | /* don't even attempt to start from user mode regs */ | |
155 | if (regs && user_mode(regs)) { | |
156 | state->stack_info.type = STACK_TYPE_UNKNOWN; | |
157 | return; | |
158 | } | |
159 | ||
160 | /* set up the starting stack frame */ | |
946c1911 JP |
161 | bp = get_frame_pointer(task, regs); |
162 | regs = decode_frame_pointer(bp); | |
163 | if (regs) { | |
164 | state->regs = regs; | |
165 | frame = (unsigned long *)regs; | |
166 | len = sizeof(*regs); | |
167 | } else { | |
168 | state->bp = bp; | |
169 | frame = bp; | |
170 | len = FRAME_HEADER_SIZE; | |
171 | } | |
7c7900f8 JP |
172 | |
173 | /* initialize stack info and make sure the frame data is accessible */ | |
946c1911 | 174 | get_stack_info(frame, state->task, &state->stack_info, |
7c7900f8 | 175 | &state->stack_mask); |
946c1911 | 176 | update_stack_state(state, frame, len); |
7c7900f8 JP |
177 | |
178 | /* | |
179 | * The caller can provide the address of the first frame directly | |
180 | * (first_frame) or indirectly (regs->sp) to indicate which stack frame | |
181 | * to start unwinding at. Skip ahead until we reach it. | |
182 | */ | |
183 | while (!unwind_done(state) && | |
184 | (!on_stack(&state->stack_info, first_frame, sizeof(long)) || | |
185 | state->bp < first_frame)) | |
186 | unwind_next_frame(state); | |
187 | } | |
188 | EXPORT_SYMBOL_GPL(__unwind_start); |