mtd: nand: omap: Fix comment in platform data using wrong Kconfig symbol
[linux/fpc-iii.git] / arch / x86 / kernel / unwind_frame.c
blob9b9fd4826e7ab4e8275877a0ebeba345d5e78e3f
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>
11 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
13 unsigned long unwind_get_return_address(struct unwind_state *state)
15 if (unwind_done(state))
16 return 0;
18 return __kernel_text_address(state->ip) ? state->ip : 0;
20 EXPORT_SYMBOL_GPL(unwind_get_return_address);
22 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
24 if (unwind_done(state))
25 return NULL;
27 return state->regs ? &state->regs->ip : state->bp + 1;
30 static void unwind_dump(struct unwind_state *state)
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;
38 if (dumped_before)
39 return;
41 dumped_before = true;
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);
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;
52 for (; sp < stack_info.end; sp++) {
54 word = READ_ONCE_NOCHECK(*sp);
56 prev_zero = zero;
57 zero = word == 0;
59 if (zero) {
60 if (!prev_zero)
61 printk_deferred("%p: %0*x ...\n",
62 sp, BITS_PER_LONG/4, 0);
63 continue;
66 printk_deferred("%p: %0*lx (%pB)\n",
67 sp, BITS_PER_LONG/4, word, (void *)word);
72 static size_t regs_size(struct pt_regs *regs)
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);
78 return sizeof(*regs);
81 static bool in_entry_code(unsigned long ip)
83 char *addr = (char *)ip;
85 if (addr >= __entry_text_start && addr < __entry_text_end)
86 return true;
88 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
89 return true;
91 return false;
94 static inline unsigned long *last_frame(struct unwind_state *state)
96 return (unsigned long *)task_pt_regs(state->task) - 2;
99 static bool is_last_frame(struct unwind_state *state)
101 return state->bp == last_frame(state);
104 #ifdef CONFIG_X86_32
105 #define GCC_REALIGN_WORDS 3
106 #else
107 #define GCC_REALIGN_WORDS 1
108 #endif
110 static inline unsigned long *last_aligned_frame(struct unwind_state *state)
112 return last_frame(state) - GCC_REALIGN_WORDS;
115 static bool is_last_aligned_frame(struct unwind_state *state)
117 unsigned long *last_bp = last_frame(state);
118 unsigned long *aligned_bp = last_aligned_frame(state);
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:
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
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
140 * After aligning the stack, it pushes a duplicate copy of the return
141 * address before pushing the frame pointer.
143 return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
146 static bool is_last_ftrace_frame(struct unwind_state *state)
148 unsigned long *last_bp = last_frame(state);
149 unsigned long *last_ftrace_bp = last_bp - 3;
152 * When unwinding from an ftrace handler of a function called by entry
153 * code, the stack layout of the last frame is:
155 * bp
156 * parent ret addr
157 * bp
158 * function ret addr
159 * parent ret addr
160 * pt_regs
161 * -----------------
163 return (state->bp == last_ftrace_bp &&
164 *state->bp == *(state->bp + 2) &&
165 *(state->bp + 1) == *(state->bp + 4));
168 static bool is_last_task_frame(struct unwind_state *state)
170 return is_last_frame(state) || is_last_aligned_frame(state) ||
171 is_last_ftrace_frame(state);
175 * This determines if the frame pointer actually contains an encoded pointer to
176 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
178 #ifdef CONFIG_X86_64
179 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
181 unsigned long regs = (unsigned long)bp;
183 if (!(regs & 0x1))
184 return NULL;
186 return (struct pt_regs *)(regs & ~0x1);
188 #else
189 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
191 unsigned long regs = (unsigned long)bp;
193 if (regs & 0x80000000)
194 return NULL;
196 return (struct pt_regs *)(regs | 0x80000000);
198 #endif
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
206 static bool update_stack_state(struct unwind_state *state,
207 unsigned long *next_bp)
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;
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;
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;
232 * If the next bp isn't on the current stack, switch to the next one.
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.
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;
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;
249 * On 32-bit with user mode regs, make sure the last two regs are safe
250 * to access:
252 if (IS_ENABLED(CONFIG_X86_32) && regs && user_mode(regs) &&
253 !on_stack(info, frame, len + 2*sizeof(long)))
254 return false;
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;
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);
275 /* Save the original stack pointer for unwind_dump(): */
276 if (!state->orig_sp)
277 state->orig_sp = frame;
279 return true;
282 bool unwind_next_frame(struct unwind_state *state)
284 struct pt_regs *regs;
285 unsigned long *next_bp;
287 if (unwind_done(state))
288 return false;
290 /* Have we reached the end? */
291 if (state->regs && user_mode(state->regs))
292 goto the_end;
294 if (is_last_task_frame(state)) {
295 regs = task_pt_regs(state->task);
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.
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.
308 if (!user_mode(regs))
309 goto the_end;
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.
316 state->regs = regs;
317 state->bp = NULL;
318 state->ip = 0;
319 return true;
322 /* Get the next frame pointer: */
323 if (state->next_bp) {
324 next_bp = state->next_bp;
325 state->next_bp = NULL;
326 } else if (state->regs) {
327 next_bp = (unsigned long *)state->regs->bp;
328 } else {
329 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
332 /* Move to the next frame if it's safe: */
333 if (!update_stack_state(state, next_bp))
334 goto bad_address;
336 return true;
338 bad_address:
339 state->error = true;
342 * When unwinding a non-current task, the task might actually be
343 * running on another CPU, in which case it could be modifying its
344 * stack while we're reading it. This is generally not a problem and
345 * can be ignored as long as the caller understands that unwinding
346 * another task will not always succeed.
348 if (state->task != current)
349 goto the_end;
352 * Don't warn if the unwinder got lost due to an interrupt in entry
353 * code or in the C handler before the first frame pointer got set up:
355 if (state->got_irq && in_entry_code(state->ip))
356 goto the_end;
357 if (state->regs &&
358 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
359 state->regs->sp < (unsigned long)task_pt_regs(state->task))
360 goto the_end;
363 * There are some known frame pointer issues on 32-bit. Disable
364 * unwinder warnings on 32-bit until it gets objtool support.
366 if (IS_ENABLED(CONFIG_X86_32))
367 goto the_end;
369 if (state->regs) {
370 printk_deferred_once(KERN_WARNING
371 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
372 state->regs, state->task->comm,
373 state->task->pid, next_bp);
374 unwind_dump(state);
375 } else {
376 printk_deferred_once(KERN_WARNING
377 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
378 state->bp, state->task->comm,
379 state->task->pid, next_bp);
380 unwind_dump(state);
382 the_end:
383 state->stack_info.type = STACK_TYPE_UNKNOWN;
384 return false;
386 EXPORT_SYMBOL_GPL(unwind_next_frame);
388 void __unwind_start(struct unwind_state *state, struct task_struct *task,
389 struct pt_regs *regs, unsigned long *first_frame)
391 unsigned long *bp;
393 memset(state, 0, sizeof(*state));
394 state->task = task;
395 state->got_irq = (regs);
397 /* Don't even attempt to start from user mode regs: */
398 if (regs && user_mode(regs)) {
399 state->stack_info.type = STACK_TYPE_UNKNOWN;
400 return;
403 bp = get_frame_pointer(task, regs);
406 * If we crash with IP==0, the last successfully executed instruction
407 * was probably an indirect function call with a NULL function pointer.
408 * That means that SP points into the middle of an incomplete frame:
409 * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
410 * would have written a frame pointer if we hadn't crashed.
411 * Pretend that the frame is complete and that BP points to it, but save
412 * the real BP so that we can use it when looking for the next frame.
414 if (regs && regs->ip == 0 &&
415 (unsigned long *)kernel_stack_pointer(regs) >= first_frame) {
416 state->next_bp = bp;
417 bp = ((unsigned long *)kernel_stack_pointer(regs)) - 1;
420 /* Initialize stack info and make sure the frame data is accessible: */
421 get_stack_info(bp, state->task, &state->stack_info,
422 &state->stack_mask);
423 update_stack_state(state, bp);
426 * The caller can provide the address of the first frame directly
427 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
428 * to start unwinding at. Skip ahead until we reach it.
430 while (!unwind_done(state) &&
431 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
432 (state->next_bp == NULL && state->bp < first_frame)))
433 unwind_next_frame(state);
435 EXPORT_SYMBOL_GPL(__unwind_start);