GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / x86 / kernel / unwind_frame.c
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  * 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.
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
24 unsigned long unwind_get_return_address(struct unwind_state *state)
25 {
26         unsigned long addr;
27         unsigned long *addr_p = unwind_get_return_address_ptr(state);
28
29         if (unwind_done(state))
30                 return 0;
31
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);
35
36         return __kernel_text_address(addr) ? addr : 0;
37 }
38 EXPORT_SYMBOL_GPL(unwind_get_return_address);
39
40 static bool update_stack_state(struct unwind_state *state, void *addr,
41                                size_t len)
42 {
43         struct stack_info *info = &state->stack_info;
44
45         /*
46          * If addr isn't on the current stack, switch to the next one.
47          *
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.
51          */
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;
56
57         return true;
58 }
59
60 bool unwind_next_frame(struct unwind_state *state)
61 {
62         unsigned long *next_bp;
63
64         if (unwind_done(state))
65                 return false;
66
67         next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
68
69         /* make sure the next frame's data is accessible */
70         if (!update_stack_state(state, next_bp, FRAME_HEADER_SIZE))
71                 return false;
72
73         /* move to the next frame */
74         state->bp = next_bp;
75         return true;
76 }
77 EXPORT_SYMBOL_GPL(unwind_next_frame);
78
79 void __unwind_start(struct unwind_state *state, struct task_struct *task,
80                     struct pt_regs *regs, unsigned long *first_frame)
81 {
82         memset(state, 0, sizeof(*state));
83         state->task = task;
84
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;
89         }
90
91         /* set up the starting stack frame */
92         state->bp = get_frame_pointer(task, regs);
93
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);
98
99         /*
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.
103          */
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);
108 }
109 EXPORT_SYMBOL_GPL(__unwind_start);