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
10
11
12
13
14#define READ_ONCE_TASK_STACK(task, x) \
15({ \
16 unsigned long val; \
17 if (task == current) \
18 val = READ_ONCE(x); \
19 else \
20 val = READ_ONCE_NOCHECK(x); \
21 val; \
22})
23
24static void unwind_dump(struct unwind_state *state, unsigned long *sp)
25{
26 static bool dumped_before = false;
27 bool prev_zero, zero = false;
28 unsigned long word;
29
30 if (dumped_before)
31 return;
32
33 dumped_before = true;
34
35 printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
36 state->stack_info.type, state->stack_info.next_sp,
37 state->stack_mask, state->graph_idx);
38
39 for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
40 word = READ_ONCE_NOCHECK(*sp);
41
42 prev_zero = zero;
43 zero = word == 0;
44
45 if (zero) {
46 if (!prev_zero)
47 printk_deferred("%p: %016x ...\n", sp, 0);
48 continue;
49 }
50
51 printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
52 }
53}
54
55unsigned long unwind_get_return_address(struct unwind_state *state)
56{
57 unsigned long addr;
58 unsigned long *addr_p = unwind_get_return_address_ptr(state);
59
60 if (unwind_done(state))
61 return 0;
62
63 if (state->regs && user_mode(state->regs))
64 return 0;
65
66 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
67 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
68 addr_p);
69
70 return __kernel_text_address(addr) ? addr : 0;
71}
72EXPORT_SYMBOL_GPL(unwind_get_return_address);
73
74static size_t regs_size(struct pt_regs *regs)
75{
76
77 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
78 return sizeof(*regs) - 2*sizeof(long);
79
80 return sizeof(*regs);
81}
82
83static bool is_last_task_frame(struct unwind_state *state)
84{
85 unsigned long bp = (unsigned long)state->bp;
86 unsigned long regs = (unsigned long)task_pt_regs(state->task);
87
88
89
90
91
92
93
94 return bp == regs - FRAME_HEADER_SIZE ||
95 bp == regs - FRAME_HEADER_SIZE - sizeof(long);
96}
97
98
99
100
101
102static struct pt_regs *decode_frame_pointer(unsigned long *bp)
103{
104 unsigned long regs = (unsigned long)bp;
105
106 if (!(regs & 0x1))
107 return NULL;
108
109 return (struct pt_regs *)(regs & ~0x1);
110}
111
112static bool update_stack_state(struct unwind_state *state, void *addr,
113 size_t len)
114{
115 struct stack_info *info = &state->stack_info;
116 enum stack_type orig_type = info->type;
117
118
119
120
121
122
123
124
125 while (!on_stack(info, addr, len))
126 if (get_stack_info(info->next_sp, state->task, info,
127 &state->stack_mask))
128 return false;
129
130 if (!state->orig_sp || info->type != orig_type)
131 state->orig_sp = addr;
132
133 return true;
134}
135
136bool unwind_next_frame(struct unwind_state *state)
137{
138 struct pt_regs *regs;
139 unsigned long *next_bp, *next_frame;
140 size_t next_len;
141 enum stack_type prev_type = state->stack_info.type;
142
143 if (unwind_done(state))
144 return false;
145
146
147 if (state->regs && user_mode(state->regs))
148 goto the_end;
149
150 if (is_last_task_frame(state)) {
151 regs = task_pt_regs(state->task);
152
153
154
155
156
157
158
159
160
161
162
163
164 if (!user_mode(regs))
165 goto the_end;
166
167
168
169
170
171
172 state->regs = regs;
173 state->bp = NULL;
174 return true;
175 }
176
177
178 if (state->regs)
179 next_bp = (unsigned long *)state->regs->bp;
180 else
181 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
182
183
184 regs = decode_frame_pointer(next_bp);
185 if (regs) {
186 next_frame = (unsigned long *)regs;
187 next_len = sizeof(*regs);
188 } else {
189 next_frame = next_bp;
190 next_len = FRAME_HEADER_SIZE;
191 }
192
193
194 if (!update_stack_state(state, next_frame, next_len)) {
195
196
197
198
199 if (state->regs)
200 goto the_end;
201
202 goto bad_address;
203 }
204
205
206 if (state->stack_info.type == prev_type) {
207 if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
208 goto bad_address;
209
210 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
211 goto bad_address;
212 }
213
214
215 if (regs) {
216 state->regs = regs;
217 state->bp = NULL;
218 } else {
219 state->bp = next_bp;
220 state->regs = NULL;
221 }
222
223 return true;
224
225bad_address:
226
227
228
229
230
231
232
233 if (state->task != current)
234 goto the_end;
235
236 if (state->regs) {
237 printk_deferred_once(KERN_WARNING
238 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
239 state->regs, state->task->comm,
240 state->task->pid, next_frame);
241 unwind_dump(state, (unsigned long *)state->regs);
242 } else {
243 printk_deferred_once(KERN_WARNING
244 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
245 state->bp, state->task->comm,
246 state->task->pid, next_frame);
247 unwind_dump(state, state->bp);
248 }
249the_end:
250 state->stack_info.type = STACK_TYPE_UNKNOWN;
251 return false;
252}
253EXPORT_SYMBOL_GPL(unwind_next_frame);
254
255void __unwind_start(struct unwind_state *state, struct task_struct *task,
256 struct pt_regs *regs, unsigned long *first_frame)
257{
258 unsigned long *bp, *frame;
259 size_t len;
260
261 memset(state, 0, sizeof(*state));
262 state->task = task;
263
264
265 if (regs && user_mode(regs)) {
266 state->stack_info.type = STACK_TYPE_UNKNOWN;
267 return;
268 }
269
270
271 bp = get_frame_pointer(task, regs);
272 regs = decode_frame_pointer(bp);
273 if (regs) {
274 state->regs = regs;
275 frame = (unsigned long *)regs;
276 len = sizeof(*regs);
277 } else {
278 state->bp = bp;
279 frame = bp;
280 len = FRAME_HEADER_SIZE;
281 }
282
283
284 get_stack_info(frame, state->task, &state->stack_info,
285 &state->stack_mask);
286 update_stack_state(state, frame, len);
287
288
289
290
291
292
293 while (!unwind_done(state) &&
294 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
295 state->bp < first_frame))
296 unwind_next_frame(state);
297}
298EXPORT_SYMBOL_GPL(__unwind_start);
299