1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/ptrace.h>
28#include <linux/export.h>
29#include <linux/stacktrace.h>
30#include <linux/kallsyms.h>
31#include <linux/sched/debug.h>
32
33#include <asm/arcregs.h>
34#include <asm/unwind.h>
35#include <asm/switch_to.h>
36
37
38
39
40
41
42#ifdef CONFIG_ARC_DW2_UNWIND
43
44static void seed_unwind_frame_info(struct task_struct *tsk,
45 struct pt_regs *regs,
46 struct unwind_frame_info *frame_info)
47{
48
49
50
51
52 if (tsk == NULL && regs == NULL) {
53 unsigned long fp, sp, blink, ret;
54 frame_info->task = current;
55
56 __asm__ __volatile__(
57 "mov %0,r27\n\t"
58 "mov %1,r28\n\t"
59 "mov %2,r31\n\t"
60 "mov %3,r63\n\t"
61 : "=r"(fp), "=r"(sp), "=r"(blink), "=r"(ret)
62 );
63
64 frame_info->regs.r27 = fp;
65 frame_info->regs.r28 = sp;
66 frame_info->regs.r31 = blink;
67 frame_info->regs.r63 = ret;
68 frame_info->call_frame = 0;
69 } else if (regs == NULL) {
70
71
72
73
74
75
76 frame_info->task = tsk;
77
78 frame_info->regs.r27 = TSK_K_FP(tsk);
79 frame_info->regs.r28 = TSK_K_ESP(tsk);
80 frame_info->regs.r31 = TSK_K_BLINK(tsk);
81 frame_info->regs.r63 = (unsigned int)__switch_to;
82
83
84
85
86
87
88
89
90
91
92 frame_info->regs.r27 = 0;
93 frame_info->regs.r28 += 60;
94 frame_info->call_frame = 0;
95
96 } else {
97
98
99
100
101 frame_info->task = tsk;
102
103 frame_info->regs.r27 = regs->fp;
104 frame_info->regs.r28 = regs->sp;
105 frame_info->regs.r31 = regs->blink;
106 frame_info->regs.r63 = regs->ret;
107 frame_info->call_frame = 0;
108 }
109}
110
111#endif
112
113notrace noinline unsigned int
114arc_unwind_core(struct task_struct *tsk, struct pt_regs *regs,
115 int (*consumer_fn) (unsigned int, void *), void *arg)
116{
117#ifdef CONFIG_ARC_DW2_UNWIND
118 int ret = 0;
119 unsigned int address;
120 struct unwind_frame_info frame_info;
121
122 seed_unwind_frame_info(tsk, regs, &frame_info);
123
124 while (1) {
125 address = UNW_PC(&frame_info);
126
127 if (!address || !__kernel_text_address(address))
128 break;
129
130 if (consumer_fn(address, arg) == -1)
131 break;
132
133 ret = arc_unwind(&frame_info);
134 if (ret)
135 break;
136
137 frame_info.regs.r63 = frame_info.regs.r31;
138 }
139
140 return address;
141#else
142
143
144
145
146
147 pr_warn_once("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
148 return 0;
149
150#endif
151}
152
153
154
155
156
157
158
159
160
161
162
163
164static int __print_sym(unsigned int address, void *unused)
165{
166 printk(" %pS\n", (void *)address);
167 return 0;
168}
169
170#ifdef CONFIG_STACKTRACE
171
172
173
174
175static int __collect_all(unsigned int address, void *arg)
176{
177 struct stack_trace *trace = arg;
178
179 if (trace->skip > 0)
180 trace->skip--;
181 else
182 trace->entries[trace->nr_entries++] = address;
183
184 if (trace->nr_entries >= trace->max_entries)
185 return -1;
186
187 return 0;
188}
189
190static int __collect_all_but_sched(unsigned int address, void *arg)
191{
192 struct stack_trace *trace = arg;
193
194 if (in_sched_functions(address))
195 return 0;
196
197 if (trace->skip > 0)
198 trace->skip--;
199 else
200 trace->entries[trace->nr_entries++] = address;
201
202 if (trace->nr_entries >= trace->max_entries)
203 return -1;
204
205 return 0;
206}
207
208#endif
209
210static int __get_first_nonsched(unsigned int address, void *unused)
211{
212 if (in_sched_functions(address))
213 return 0;
214
215 return -1;
216}
217
218
219
220
221
222
223noinline void show_stacktrace(struct task_struct *tsk, struct pt_regs *regs)
224{
225 pr_info("\nStack Trace:\n");
226 arc_unwind_core(tsk, regs, __print_sym, NULL);
227}
228EXPORT_SYMBOL(show_stacktrace);
229
230
231void show_stack(struct task_struct *tsk, unsigned long *sp)
232{
233 show_stacktrace(tsk, NULL);
234}
235
236
237
238
239
240unsigned int get_wchan(struct task_struct *tsk)
241{
242 return arc_unwind_core(tsk, NULL, __get_first_nonsched, NULL);
243}
244
245#ifdef CONFIG_STACKTRACE
246
247
248
249
250
251void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
252{
253
254 arc_unwind_core(tsk, NULL, __collect_all_but_sched, trace);
255}
256
257void save_stack_trace(struct stack_trace *trace)
258{
259
260 arc_unwind_core(NULL, NULL, __collect_all, trace);
261}
262EXPORT_SYMBOL_GPL(save_stack_trace);
263#endif
264