Linux 4.9.243
[linux/fpc-iii.git] / arch / x86 / kernel / unwind_frame.c
blob9b8b3cb2e934dcf81de9f96e3e29a0cb328dbeb9
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>
7 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
9 /*
10 * This disables KASAN checking when reading a value from another task's stack,
11 * since the other task could be running on another CPU and could have poisoned
12 * the stack in the meantime.
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; \
24 unsigned long unwind_get_return_address(struct unwind_state *state)
26 unsigned long addr;
27 unsigned long *addr_p = unwind_get_return_address_ptr(state);
29 if (unwind_done(state))
30 return 0;
32 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
33 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
34 addr_p);
36 return __kernel_text_address(addr) ? addr : 0;
38 EXPORT_SYMBOL_GPL(unwind_get_return_address);
40 static bool update_stack_state(struct unwind_state *state, void *addr,
41 size_t len)
43 struct stack_info *info = &state->stack_info;
46 * If addr isn't on the current stack, switch to the next one.
48 * We may have to traverse multiple stacks to deal with the possibility
49 * that 'info->next_sp' could point to an empty stack and 'addr' could
50 * be on a subsequent stack.
52 while (!on_stack(info, addr, len))
53 if (get_stack_info(info->next_sp, state->task, info,
54 &state->stack_mask))
55 return false;
57 return true;
60 bool unwind_next_frame(struct unwind_state *state)
62 unsigned long *next_bp;
64 if (unwind_done(state))
65 return false;
67 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
69 /* make sure the next frame's data is accessible */
70 if (!update_stack_state(state, next_bp, FRAME_HEADER_SIZE))
71 return false;
73 /* move to the next frame */
74 state->bp = next_bp;
75 return true;
77 EXPORT_SYMBOL_GPL(unwind_next_frame);
79 void __unwind_start(struct unwind_state *state, struct task_struct *task,
80 struct pt_regs *regs, unsigned long *first_frame)
82 memset(state, 0, sizeof(*state));
83 state->task = task;
85 /* don't even attempt to start from user mode regs */
86 if (regs && user_mode(regs)) {
87 state->stack_info.type = STACK_TYPE_UNKNOWN;
88 return;
91 /* set up the starting stack frame */
92 state->bp = get_frame_pointer(task, regs);
94 /* initialize stack info and make sure the frame data is accessible */
95 get_stack_info(state->bp, state->task, &state->stack_info,
96 &state->stack_mask);
97 update_stack_state(state, state->bp, FRAME_HEADER_SIZE);
100 * The caller can provide the address of the first frame directly
101 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
102 * to start unwinding at. Skip ahead until we reach it.
104 while (!unwind_done(state) &&
105 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
106 state->bp < first_frame))
107 unwind_next_frame(state);
109 EXPORT_SYMBOL_GPL(__unwind_start);