Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/sched.h>
3#include <linux/sched/task.h>
4#include <linux/sched/task_stack.h>
5#include <linux/interrupt.h>
6#include <asm/sections.h>
7#include <asm/ptrace.h>
8#include <asm/bitops.h>
9#include <asm/stacktrace.h>
10#include <asm/unwind.h>
11
12#define FRAME_HEADER_SIZE (sizeof(long) * 2)
13
14unsigned long unwind_get_return_address(struct unwind_state *state)
15{
16 if (unwind_done(state))
17 return 0;
18
19 return __kernel_text_address(state->ip) ? state->ip : 0;
20}
21EXPORT_SYMBOL_GPL(unwind_get_return_address);
22
23unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
24{
25 if (unwind_done(state))
26 return NULL;
27
28 return state->regs ? &state->regs->ip : state->bp + 1;
29}
30
31static void unwind_dump(struct unwind_state *state)
32{
33 static bool dumped_before = false;
34 bool prev_zero, zero = false;
35 unsigned long word, *sp;
36 struct stack_info stack_info = {0};
37 unsigned long visit_mask = 0;
38
39 if (dumped_before)
40 return;
41
42 dumped_before = true;
43
44 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
45 state->stack_info.type, state->stack_info.next_sp,
46 state->stack_mask, state->graph_idx);
47
48 for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
49 sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
50 if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
51 break;
52
53 for (; sp < stack_info.end; sp++) {
54
55 word = READ_ONCE_NOCHECK(*sp);
56
57 prev_zero = zero;
58 zero = word == 0;
59
60 if (zero) {
61 if (!prev_zero)
62 printk_deferred("%p: %0*x ...\n",
63 sp, BITS_PER_LONG/4, 0);
64 continue;
65 }
66
67 printk_deferred("%p: %0*lx (%pB)\n",
68 sp, BITS_PER_LONG/4, word, (void *)word);
69 }
70 }
71}
72
73static bool in_entry_code(unsigned long ip)
74{
75 char *addr = (char *)ip;
76
77 return addr >= __entry_text_start && addr < __entry_text_end;
78}
79
80static inline unsigned long *last_frame(struct unwind_state *state)
81{
82 return (unsigned long *)task_pt_regs(state->task) - 2;
83}
84
85static bool is_last_frame(struct unwind_state *state)
86{
87 return state->bp == last_frame(state);
88}
89
90#ifdef CONFIG_X86_32
91#define GCC_REALIGN_WORDS 3
92#else
93#define GCC_REALIGN_WORDS 1
94#endif
95
96static inline unsigned long *last_aligned_frame(struct unwind_state *state)
97{
98 return last_frame(state) - GCC_REALIGN_WORDS;
99}
100
101static bool is_last_aligned_frame(struct unwind_state *state)
102{
103 unsigned long *last_bp = last_frame(state);
104 unsigned long *aligned_bp = last_aligned_frame(state);
105
106 /*
107 * GCC can occasionally decide to realign the stack pointer and change
108 * the offset of the stack frame in the prologue of a function called
109 * by head/entry code. Examples:
110 *
111 * <start_secondary>:
112 * push %edi
113 * lea 0x8(%esp),%edi
114 * and $0xfffffff8,%esp
115 * pushl -0x4(%edi)
116 * push %ebp
117 * mov %esp,%ebp
118 *
119 * <x86_64_start_kernel>:
120 * lea 0x8(%rsp),%r10
121 * and $0xfffffffffffffff0,%rsp
122 * pushq -0x8(%r10)
123 * push %rbp
124 * mov %rsp,%rbp
125 *
126 * After aligning the stack, it pushes a duplicate copy of the return
127 * address before pushing the frame pointer.
128 */
129 return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
130}
131
132static bool is_last_ftrace_frame(struct unwind_state *state)
133{
134 unsigned long *last_bp = last_frame(state);
135 unsigned long *last_ftrace_bp = last_bp - 3;
136
137 /*
138 * When unwinding from an ftrace handler of a function called by entry
139 * code, the stack layout of the last frame is:
140 *
141 * bp
142 * parent ret addr
143 * bp
144 * function ret addr
145 * parent ret addr
146 * pt_regs
147 * -----------------
148 */
149 return (state->bp == last_ftrace_bp &&
150 *state->bp == *(state->bp + 2) &&
151 *(state->bp + 1) == *(state->bp + 4));
152}
153
154static bool is_last_task_frame(struct unwind_state *state)
155{
156 return is_last_frame(state) || is_last_aligned_frame(state) ||
157 is_last_ftrace_frame(state);
158}
159
160/*
161 * This determines if the frame pointer actually contains an encoded pointer to
162 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
163 */
164#ifdef CONFIG_X86_64
165static struct pt_regs *decode_frame_pointer(unsigned long *bp)
166{
167 unsigned long regs = (unsigned long)bp;
168
169 if (!(regs & 0x1))
170 return NULL;
171
172 return (struct pt_regs *)(regs & ~0x1);
173}
174#else
175static struct pt_regs *decode_frame_pointer(unsigned long *bp)
176{
177 unsigned long regs = (unsigned long)bp;
178
179 if (regs & 0x80000000)
180 return NULL;
181
182 return (struct pt_regs *)(regs | 0x80000000);
183}
184#endif
185
186/*
187 * While walking the stack, KMSAN may stomp on stale locals from other
188 * functions that were marked as uninitialized upon function exit, and
189 * now hold the call frame information for the current function (e.g. the frame
190 * pointer). Because KMSAN does not specifically mark call frames as
191 * initialized, false positive reports are possible. To prevent such reports,
192 * we mark the functions scanning the stack (here and below) with
193 * __no_kmsan_checks.
194 */
195__no_kmsan_checks
196static bool update_stack_state(struct unwind_state *state,
197 unsigned long *next_bp)
198{
199 struct stack_info *info = &state->stack_info;
200 enum stack_type prev_type = info->type;
201 struct pt_regs *regs;
202 unsigned long *frame, *prev_frame_end, *addr_p, addr;
203 size_t len;
204
205 if (state->regs)
206 prev_frame_end = (void *)state->regs + sizeof(*state->regs);
207 else
208 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
209
210 /* Is the next frame pointer an encoded pointer to pt_regs? */
211 regs = decode_frame_pointer(next_bp);
212 if (regs) {
213 frame = (unsigned long *)regs;
214 len = sizeof(*regs);
215 state->got_irq = true;
216 } else {
217 frame = next_bp;
218 len = FRAME_HEADER_SIZE;
219 }
220
221 /*
222 * If the next bp isn't on the current stack, switch to the next one.
223 *
224 * We may have to traverse multiple stacks to deal with the possibility
225 * that info->next_sp could point to an empty stack and the next bp
226 * could be on a subsequent stack.
227 */
228 while (!on_stack(info, frame, len))
229 if (get_stack_info(info->next_sp, state->task, info,
230 &state->stack_mask))
231 return false;
232
233 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
234 if (state->orig_sp && state->stack_info.type == prev_type &&
235 frame < prev_frame_end)
236 return false;
237
238 /* Move state to the next frame: */
239 if (regs) {
240 state->regs = regs;
241 state->bp = NULL;
242 } else {
243 state->bp = next_bp;
244 state->regs = NULL;
245 }
246
247 /* Save the return address: */
248 if (state->regs && user_mode(state->regs))
249 state->ip = 0;
250 else {
251 addr_p = unwind_get_return_address_ptr(state);
252 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
253 state->ip = unwind_recover_ret_addr(state, addr, addr_p);
254 }
255
256 /* Save the original stack pointer for unwind_dump(): */
257 if (!state->orig_sp)
258 state->orig_sp = frame;
259
260 return true;
261}
262
263__no_kmsan_checks
264bool unwind_next_frame(struct unwind_state *state)
265{
266 struct pt_regs *regs;
267 unsigned long *next_bp;
268
269 if (unwind_done(state))
270 return false;
271
272 /* Have we reached the end? */
273 if (state->regs && user_mode(state->regs))
274 goto the_end;
275
276 if (is_last_task_frame(state)) {
277 regs = task_pt_regs(state->task);
278
279 /*
280 * kthreads (other than the boot CPU's idle thread) have some
281 * partial regs at the end of their stack which were placed
282 * there by copy_thread(). But the regs don't have any
283 * useful information, so we can skip them.
284 *
285 * This user_mode() check is slightly broader than a PF_KTHREAD
286 * check because it also catches the awkward situation where a
287 * newly forked kthread transitions into a user task by calling
288 * kernel_execve(), which eventually clears PF_KTHREAD.
289 */
290 if (!user_mode(regs))
291 goto the_end;
292
293 /*
294 * We're almost at the end, but not quite: there's still the
295 * syscall regs frame. Entry code doesn't encode the regs
296 * pointer for syscalls, so we have to set it manually.
297 */
298 state->regs = regs;
299 state->bp = NULL;
300 state->ip = 0;
301 return true;
302 }
303
304 /* Get the next frame pointer: */
305 if (state->next_bp) {
306 next_bp = state->next_bp;
307 state->next_bp = NULL;
308 } else if (state->regs) {
309 next_bp = (unsigned long *)state->regs->bp;
310 } else {
311 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
312 }
313
314 /* Move to the next frame if it's safe: */
315 if (!update_stack_state(state, next_bp))
316 goto bad_address;
317
318 return true;
319
320bad_address:
321 state->error = true;
322
323 /*
324 * When unwinding a non-current task, the task might actually be
325 * running on another CPU, in which case it could be modifying its
326 * stack while we're reading it. This is generally not a problem and
327 * can be ignored as long as the caller understands that unwinding
328 * another task will not always succeed.
329 */
330 if (state->task != current)
331 goto the_end;
332
333 /*
334 * Don't warn if the unwinder got lost due to an interrupt in entry
335 * code or in the C handler before the first frame pointer got set up:
336 */
337 if (state->got_irq && in_entry_code(state->ip))
338 goto the_end;
339 if (state->regs &&
340 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
341 state->regs->sp < (unsigned long)task_pt_regs(state->task))
342 goto the_end;
343
344 /*
345 * There are some known frame pointer issues on 32-bit. Disable
346 * unwinder warnings on 32-bit until it gets objtool support.
347 */
348 if (IS_ENABLED(CONFIG_X86_32))
349 goto the_end;
350
351 if (state->task != current)
352 goto the_end;
353
354 if (state->regs) {
355 printk_deferred_once(KERN_WARNING
356 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
357 state->regs, state->task->comm,
358 state->task->pid, next_bp);
359 unwind_dump(state);
360 } else {
361 printk_deferred_once(KERN_WARNING
362 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
363 state->bp, state->task->comm,
364 state->task->pid, next_bp);
365 unwind_dump(state);
366 }
367the_end:
368 state->stack_info.type = STACK_TYPE_UNKNOWN;
369 return false;
370}
371EXPORT_SYMBOL_GPL(unwind_next_frame);
372
373void __unwind_start(struct unwind_state *state, struct task_struct *task,
374 struct pt_regs *regs, unsigned long *first_frame)
375{
376 unsigned long *bp;
377
378 memset(state, 0, sizeof(*state));
379 state->task = task;
380 state->got_irq = (regs);
381
382 /* Don't even attempt to start from user mode regs: */
383 if (regs && user_mode(regs)) {
384 state->stack_info.type = STACK_TYPE_UNKNOWN;
385 return;
386 }
387
388 bp = get_frame_pointer(task, regs);
389
390 /*
391 * If we crash with IP==0, the last successfully executed instruction
392 * was probably an indirect function call with a NULL function pointer.
393 * That means that SP points into the middle of an incomplete frame:
394 * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
395 * would have written a frame pointer if we hadn't crashed.
396 * Pretend that the frame is complete and that BP points to it, but save
397 * the real BP so that we can use it when looking for the next frame.
398 */
399 if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
400 state->next_bp = bp;
401 bp = ((unsigned long *)regs->sp) - 1;
402 }
403
404 /* Initialize stack info and make sure the frame data is accessible: */
405 get_stack_info(bp, state->task, &state->stack_info,
406 &state->stack_mask);
407 update_stack_state(state, bp);
408
409 /*
410 * The caller can provide the address of the first frame directly
411 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
412 * to start unwinding at. Skip ahead until we reach it.
413 */
414 while (!unwind_done(state) &&
415 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
416 (state->next_bp == NULL && state->bp < first_frame)))
417 unwind_next_frame(state);
418}
419EXPORT_SYMBOL_GPL(__unwind_start);
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>
10
11#define FRAME_HEADER_SIZE (sizeof(long) * 2)
12
13unsigned long unwind_get_return_address(struct unwind_state *state)
14{
15 if (unwind_done(state))
16 return 0;
17
18 return __kernel_text_address(state->ip) ? state->ip : 0;
19}
20EXPORT_SYMBOL_GPL(unwind_get_return_address);
21
22unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
23{
24 if (unwind_done(state))
25 return NULL;
26
27 return state->regs ? &state->regs->ip : state->bp + 1;
28}
29
30static void unwind_dump(struct unwind_state *state)
31{
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;
37
38 if (dumped_before)
39 return;
40
41 dumped_before = true;
42
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);
46
47 for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
48 sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
49 if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
50 break;
51
52 for (; sp < stack_info.end; sp++) {
53
54 word = READ_ONCE_NOCHECK(*sp);
55
56 prev_zero = zero;
57 zero = word == 0;
58
59 if (zero) {
60 if (!prev_zero)
61 printk_deferred("%p: %0*x ...\n",
62 sp, BITS_PER_LONG/4, 0);
63 continue;
64 }
65
66 printk_deferred("%p: %0*lx (%pB)\n",
67 sp, BITS_PER_LONG/4, word, (void *)word);
68 }
69 }
70}
71
72static size_t regs_size(struct pt_regs *regs)
73{
74 /* x86_32 regs from kernel mode are two words shorter: */
75 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
76 return sizeof(*regs) - 2*sizeof(long);
77
78 return sizeof(*regs);
79}
80
81static bool in_entry_code(unsigned long ip)
82{
83 char *addr = (char *)ip;
84
85 if (addr >= __entry_text_start && addr < __entry_text_end)
86 return true;
87
88 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
89 return true;
90
91 return false;
92}
93
94static inline unsigned long *last_frame(struct unwind_state *state)
95{
96 return (unsigned long *)task_pt_regs(state->task) - 2;
97}
98
99static bool is_last_frame(struct unwind_state *state)
100{
101 return state->bp == last_frame(state);
102}
103
104#ifdef CONFIG_X86_32
105#define GCC_REALIGN_WORDS 3
106#else
107#define GCC_REALIGN_WORDS 1
108#endif
109
110static inline unsigned long *last_aligned_frame(struct unwind_state *state)
111{
112 return last_frame(state) - GCC_REALIGN_WORDS;
113}
114
115static bool is_last_aligned_frame(struct unwind_state *state)
116{
117 unsigned long *last_bp = last_frame(state);
118 unsigned long *aligned_bp = last_aligned_frame(state);
119
120 /*
121 * GCC can occasionally decide to realign the stack pointer and change
122 * the offset of the stack frame in the prologue of a function called
123 * by head/entry code. Examples:
124 *
125 * <start_secondary>:
126 * push %edi
127 * lea 0x8(%esp),%edi
128 * and $0xfffffff8,%esp
129 * pushl -0x4(%edi)
130 * push %ebp
131 * mov %esp,%ebp
132 *
133 * <x86_64_start_kernel>:
134 * lea 0x8(%rsp),%r10
135 * and $0xfffffffffffffff0,%rsp
136 * pushq -0x8(%r10)
137 * push %rbp
138 * mov %rsp,%rbp
139 *
140 * After aligning the stack, it pushes a duplicate copy of the return
141 * address before pushing the frame pointer.
142 */
143 return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
144}
145
146static bool is_last_ftrace_frame(struct unwind_state *state)
147{
148 unsigned long *last_bp = last_frame(state);
149 unsigned long *last_ftrace_bp = last_bp - 3;
150
151 /*
152 * When unwinding from an ftrace handler of a function called by entry
153 * code, the stack layout of the last frame is:
154 *
155 * bp
156 * parent ret addr
157 * bp
158 * function ret addr
159 * parent ret addr
160 * pt_regs
161 * -----------------
162 */
163 return (state->bp == last_ftrace_bp &&
164 *state->bp == *(state->bp + 2) &&
165 *(state->bp + 1) == *(state->bp + 4));
166}
167
168static bool is_last_task_frame(struct unwind_state *state)
169{
170 return is_last_frame(state) || is_last_aligned_frame(state) ||
171 is_last_ftrace_frame(state);
172}
173
174/*
175 * This determines if the frame pointer actually contains an encoded pointer to
176 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
177 */
178#ifdef CONFIG_X86_64
179static struct pt_regs *decode_frame_pointer(unsigned long *bp)
180{
181 unsigned long regs = (unsigned long)bp;
182
183 if (!(regs & 0x1))
184 return NULL;
185
186 return (struct pt_regs *)(regs & ~0x1);
187}
188#else
189static struct pt_regs *decode_frame_pointer(unsigned long *bp)
190{
191 unsigned long regs = (unsigned long)bp;
192
193 if (regs & 0x80000000)
194 return NULL;
195
196 return (struct pt_regs *)(regs | 0x80000000);
197}
198#endif
199
200#ifdef CONFIG_X86_32
201#define KERNEL_REGS_SIZE (sizeof(struct pt_regs) - 2*sizeof(long))
202#else
203#define KERNEL_REGS_SIZE (sizeof(struct pt_regs))
204#endif
205
206static bool update_stack_state(struct unwind_state *state,
207 unsigned long *next_bp)
208{
209 struct stack_info *info = &state->stack_info;
210 enum stack_type prev_type = info->type;
211 struct pt_regs *regs;
212 unsigned long *frame, *prev_frame_end, *addr_p, addr;
213 size_t len;
214
215 if (state->regs)
216 prev_frame_end = (void *)state->regs + regs_size(state->regs);
217 else
218 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
219
220 /* Is the next frame pointer an encoded pointer to pt_regs? */
221 regs = decode_frame_pointer(next_bp);
222 if (regs) {
223 frame = (unsigned long *)regs;
224 len = KERNEL_REGS_SIZE;
225 state->got_irq = true;
226 } else {
227 frame = next_bp;
228 len = FRAME_HEADER_SIZE;
229 }
230
231 /*
232 * If the next bp isn't on the current stack, switch to the next one.
233 *
234 * We may have to traverse multiple stacks to deal with the possibility
235 * that info->next_sp could point to an empty stack and the next bp
236 * could be on a subsequent stack.
237 */
238 while (!on_stack(info, frame, len))
239 if (get_stack_info(info->next_sp, state->task, info,
240 &state->stack_mask))
241 return false;
242
243 /* Make sure it only unwinds up and doesn't overlap the prev frame: */
244 if (state->orig_sp && state->stack_info.type == prev_type &&
245 frame < prev_frame_end)
246 return false;
247
248 /*
249 * On 32-bit with user mode regs, make sure the last two regs are safe
250 * to access:
251 */
252 if (IS_ENABLED(CONFIG_X86_32) && regs && user_mode(regs) &&
253 !on_stack(info, frame, len + 2*sizeof(long)))
254 return false;
255
256 /* Move state to the next frame: */
257 if (regs) {
258 state->regs = regs;
259 state->bp = NULL;
260 } else {
261 state->bp = next_bp;
262 state->regs = NULL;
263 }
264
265 /* Save the return address: */
266 if (state->regs && user_mode(state->regs))
267 state->ip = 0;
268 else {
269 addr_p = unwind_get_return_address_ptr(state);
270 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
271 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
272 addr, addr_p);
273 }
274
275 /* Save the original stack pointer for unwind_dump(): */
276 if (!state->orig_sp)
277 state->orig_sp = frame;
278
279 return true;
280}
281
282bool unwind_next_frame(struct unwind_state *state)
283{
284 struct pt_regs *regs;
285 unsigned long *next_bp;
286
287 if (unwind_done(state))
288 return false;
289
290 /* Have we reached the end? */
291 if (state->regs && user_mode(state->regs))
292 goto the_end;
293
294 if (is_last_task_frame(state)) {
295 regs = task_pt_regs(state->task);
296
297 /*
298 * kthreads (other than the boot CPU's idle thread) have some
299 * partial regs at the end of their stack which were placed
300 * there by copy_thread_tls(). But the regs don't have any
301 * useful information, so we can skip them.
302 *
303 * This user_mode() check is slightly broader than a PF_KTHREAD
304 * check because it also catches the awkward situation where a
305 * newly forked kthread transitions into a user task by calling
306 * do_execve(), which eventually clears PF_KTHREAD.
307 */
308 if (!user_mode(regs))
309 goto the_end;
310
311 /*
312 * We're almost at the end, but not quite: there's still the
313 * syscall regs frame. Entry code doesn't encode the regs
314 * pointer for syscalls, so we have to set it manually.
315 */
316 state->regs = regs;
317 state->bp = NULL;
318 state->ip = 0;
319 return true;
320 }
321
322 /* Get the next frame pointer: */
323 if (state->regs)
324 next_bp = (unsigned long *)state->regs->bp;
325 else
326 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
327
328 /* Move to the next frame if it's safe: */
329 if (!update_stack_state(state, next_bp))
330 goto bad_address;
331
332 return true;
333
334bad_address:
335 state->error = true;
336
337 /*
338 * When unwinding a non-current task, the task might actually be
339 * running on another CPU, in which case it could be modifying its
340 * stack while we're reading it. This is generally not a problem and
341 * can be ignored as long as the caller understands that unwinding
342 * another task will not always succeed.
343 */
344 if (state->task != current)
345 goto the_end;
346
347 /*
348 * Don't warn if the unwinder got lost due to an interrupt in entry
349 * code or in the C handler before the first frame pointer got set up:
350 */
351 if (state->got_irq && in_entry_code(state->ip))
352 goto the_end;
353 if (state->regs &&
354 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
355 state->regs->sp < (unsigned long)task_pt_regs(state->task))
356 goto the_end;
357
358 /*
359 * There are some known frame pointer issues on 32-bit. Disable
360 * unwinder warnings on 32-bit until it gets objtool support.
361 */
362 if (IS_ENABLED(CONFIG_X86_32))
363 goto the_end;
364
365 if (state->regs) {
366 printk_deferred_once(KERN_WARNING
367 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
368 state->regs, state->task->comm,
369 state->task->pid, next_bp);
370 unwind_dump(state);
371 } else {
372 printk_deferred_once(KERN_WARNING
373 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
374 state->bp, state->task->comm,
375 state->task->pid, next_bp);
376 unwind_dump(state);
377 }
378the_end:
379 state->stack_info.type = STACK_TYPE_UNKNOWN;
380 return false;
381}
382EXPORT_SYMBOL_GPL(unwind_next_frame);
383
384void __unwind_start(struct unwind_state *state, struct task_struct *task,
385 struct pt_regs *regs, unsigned long *first_frame)
386{
387 unsigned long *bp;
388
389 memset(state, 0, sizeof(*state));
390 state->task = task;
391 state->got_irq = (regs);
392
393 /* Don't even attempt to start from user mode regs: */
394 if (regs && user_mode(regs)) {
395 state->stack_info.type = STACK_TYPE_UNKNOWN;
396 return;
397 }
398
399 bp = get_frame_pointer(task, regs);
400
401 /* Initialize stack info and make sure the frame data is accessible: */
402 get_stack_info(bp, state->task, &state->stack_info,
403 &state->stack_mask);
404 update_stack_state(state, bp);
405
406 /*
407 * The caller can provide the address of the first frame directly
408 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
409 * to start unwinding at. Skip ahead until we reach it.
410 */
411 while (!unwind_done(state) &&
412 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
413 state->bp < first_frame))
414 unwind_next_frame(state);
415}
416EXPORT_SYMBOL_GPL(__unwind_start);