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