1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/export.h>
9#include <linux/ftrace.h>
10#include <linux/kprobes.h>
11#include <linux/sched.h>
12#include <linux/sched/debug.h>
13#include <linux/sched/task_stack.h>
14#include <linux/stacktrace.h>
15
16#include <asm/irq.h>
17#include <asm/pointer_auth.h>
18#include <asm/stack_pointer.h>
19#include <asm/stacktrace.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42struct unwind_state {
43 unsigned long fp;
44 unsigned long pc;
45 DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
46 unsigned long prev_fp;
47 enum stack_type prev_type;
48#ifdef CONFIG_KRETPROBES
49 struct llist_node *kr_cur;
50#endif
51};
52
53static notrace void unwind_init(struct unwind_state *state, unsigned long fp,
54 unsigned long pc)
55{
56 state->fp = fp;
57 state->pc = pc;
58#ifdef CONFIG_KRETPROBES
59 state->kr_cur = NULL;
60#endif
61
62
63
64
65
66
67
68
69
70
71 bitmap_zero(state->stacks_done, __NR_STACK_TYPES);
72 state->prev_fp = 0;
73 state->prev_type = STACK_TYPE_UNKNOWN;
74}
75NOKPROBE_SYMBOL(unwind_init);
76
77
78
79
80
81
82
83
84static int notrace unwind_next(struct task_struct *tsk,
85 struct unwind_state *state)
86{
87 unsigned long fp = state->fp;
88 struct stack_info info;
89
90
91 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
92 return -ENOENT;
93
94 if (fp & 0x7)
95 return -EINVAL;
96
97 if (!on_accessible_stack(tsk, fp, 16, &info))
98 return -EINVAL;
99
100 if (test_bit(info.type, state->stacks_done))
101 return -EINVAL;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 if (info.type == state->prev_type) {
117 if (fp <= state->prev_fp)
118 return -EINVAL;
119 } else {
120 set_bit(state->prev_type, state->stacks_done);
121 }
122
123
124
125
126
127 state->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
128 state->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
129 state->prev_fp = fp;
130 state->prev_type = info.type;
131
132 state->pc = ptrauth_strip_insn_pac(state->pc);
133
134#ifdef CONFIG_FUNCTION_GRAPH_TRACER
135 if (tsk->ret_stack &&
136 (state->pc == (unsigned long)return_to_handler)) {
137 unsigned long orig_pc;
138
139
140
141
142
143
144 orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc,
145 (void *)state->fp);
146 if (WARN_ON_ONCE(state->pc == orig_pc))
147 return -EINVAL;
148 state->pc = orig_pc;
149 }
150#endif
151#ifdef CONFIG_KRETPROBES
152 if (is_kretprobe_trampoline(state->pc))
153 state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur);
154#endif
155
156 return 0;
157}
158NOKPROBE_SYMBOL(unwind_next);
159
160static void notrace unwind(struct task_struct *tsk,
161 struct unwind_state *state,
162 stack_trace_consume_fn consume_entry, void *cookie)
163{
164 while (1) {
165 int ret;
166
167 if (!consume_entry(cookie, state->pc))
168 break;
169 ret = unwind_next(tsk, state);
170 if (ret < 0)
171 break;
172 }
173}
174NOKPROBE_SYMBOL(unwind);
175
176static bool dump_backtrace_entry(void *arg, unsigned long where)
177{
178 char *loglvl = arg;
179 printk("%s %pSb\n", loglvl, (void *)where);
180 return true;
181}
182
183void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
184 const char *loglvl)
185{
186 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
187
188 if (regs && user_mode(regs))
189 return;
190
191 if (!tsk)
192 tsk = current;
193
194 if (!try_get_task_stack(tsk))
195 return;
196
197 printk("%sCall trace:\n", loglvl);
198 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
199
200 put_task_stack(tsk);
201}
202
203void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
204{
205 dump_backtrace(NULL, tsk, loglvl);
206 barrier();
207}
208
209noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
210 void *cookie, struct task_struct *task,
211 struct pt_regs *regs)
212{
213 struct unwind_state state;
214
215 if (regs)
216 unwind_init(&state, regs->regs[29], regs->pc);
217 else if (task == current)
218 unwind_init(&state,
219 (unsigned long)__builtin_frame_address(1),
220 (unsigned long)__builtin_return_address(0));
221 else
222 unwind_init(&state, thread_saved_fp(task),
223 thread_saved_pc(task));
224
225 unwind(task, &state, consume_entry, cookie);
226}
227