1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/export.h>
17#include <linux/kallsyms.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/stacktrace.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/io.h>
24#include <asm/sections.h>
25#include <asm/exceptions.h>
26#include <asm/unwind.h>
27#include <asm/switch_to.h>
28
29struct stack_trace;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62static inline long get_frame_size(unsigned long instr)
63{
64 return abs((s16)(instr & 0xFFFF));
65}
66
67
68
69
70
71
72
73
74
75
76static unsigned long *find_frame_creation(unsigned long *pc)
77{
78 int i;
79
80
81
82
83
84
85 for (i = 0; i < 1000; i++, pc--) {
86 unsigned long instr;
87 s16 frame_size;
88
89 if (!kernel_text_address((unsigned long) pc))
90 return NULL;
91
92 instr = *pc;
93
94
95 if ((instr & 0xFFFF0000) != 0x30210000)
96 continue;
97
98 frame_size = get_frame_size(instr);
99 if ((frame_size < 8) || (frame_size & 3)) {
100 pr_debug(" Invalid frame size %d at 0x%p\n",
101 frame_size, pc);
102 return NULL;
103 }
104
105 pr_debug(" Found frame creation at 0x%p, size %d\n", pc,
106 frame_size);
107 return pc;
108 }
109
110 return NULL;
111}
112
113
114
115
116
117
118
119
120
121
122
123
124static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
125 unsigned long leaf_return,
126 unsigned long *pprev_fp,
127 unsigned long *pprev_pc)
128{
129 unsigned long *prologue = NULL;
130
131
132 if (pc != (unsigned long) &_switch_to)
133 prologue = find_frame_creation((unsigned long *)pc);
134
135 if (prologue) {
136 long frame_size = get_frame_size(*prologue);
137
138 *pprev_fp = fp + frame_size;
139 *pprev_pc = *(unsigned long *)fp;
140 } else {
141 if (!leaf_return)
142 return -EINVAL;
143 *pprev_pc = leaf_return;
144 *pprev_fp = fp;
145 }
146
147
148
149
150 return (!*pprev_pc || (*pprev_pc & 3)) ? -EINVAL : 0;
151}
152
153static void microblaze_unwind_inner(struct task_struct *task,
154 unsigned long pc, unsigned long fp,
155 unsigned long leaf_return,
156 struct stack_trace *trace);
157
158
159
160
161
162#ifdef CONFIG_MMU
163static inline void unwind_trap(struct task_struct *task, unsigned long pc,
164 unsigned long fp, struct stack_trace *trace)
165{
166
167}
168#else
169static inline void unwind_trap(struct task_struct *task, unsigned long pc,
170 unsigned long fp, struct stack_trace *trace)
171{
172 const struct pt_regs *regs = (const struct pt_regs *) fp;
173 microblaze_unwind_inner(task, regs->pc, regs->r1, regs->r15, trace);
174}
175#endif
176
177
178
179
180
181
182
183
184
185
186
187static void microblaze_unwind_inner(struct task_struct *task,
188 unsigned long pc, unsigned long fp,
189 unsigned long leaf_return,
190 struct stack_trace *trace)
191{
192 int ofs = 0;
193
194 pr_debug(" Unwinding with PC=%p, FP=%p\n", (void *)pc, (void *)fp);
195 if (!pc || !fp || (pc & 3) || (fp & 3)) {
196 pr_debug(" Invalid state for unwind, aborting\n");
197 return;
198 }
199 for (; pc != 0;) {
200 unsigned long next_fp, next_pc = 0;
201 unsigned long return_to = pc + 2 * sizeof(unsigned long);
202 const struct trap_handler_info *handler =
203 µblaze_trap_handlers;
204
205
206 if ((return_to >= (unsigned long)&_hw_exception_handler)
207 &&(return_to < (unsigned long)&ex_handler_unhandled)) {
208
209
210
211
212#ifndef CONFIG_MMU
213 const struct pt_regs *regs =
214 (const struct pt_regs *) fp;
215#endif
216 pr_info("HW EXCEPTION\n");
217#ifndef CONFIG_MMU
218 microblaze_unwind_inner(task, regs->r17 - 4,
219 fp + EX_HANDLER_STACK_SIZ,
220 regs->r15, trace);
221#endif
222 return;
223 }
224
225
226 for (; handler->start_addr; ++handler) {
227 if ((return_to >= handler->start_addr)
228 && (return_to <= handler->end_addr)) {
229 if (!trace)
230 pr_info("%s\n", handler->trap_name);
231 unwind_trap(task, pc, fp, trace);
232 return;
233 }
234 }
235 pc -= ofs;
236
237 if (trace) {
238#ifdef CONFIG_STACKTRACE
239 if (trace->skip > 0)
240 trace->skip--;
241 else
242 trace->entries[trace->nr_entries++] = pc;
243
244 if (trace->nr_entries >= trace->max_entries)
245 break;
246#endif
247 } else {
248
249 if (unlikely(pc == task_pt_regs(task)->pc)) {
250 pr_info("[<%p>] PID %lu [%s]\n",
251 (void *) pc,
252 (unsigned long) task->pid,
253 task->comm);
254 break;
255 } else
256 print_ip_sym(pc);
257 }
258
259
260 if (!kernel_text_address(pc))
261 break;
262
263 if (lookup_prev_stack_frame(fp, pc, leaf_return, &next_fp,
264 &next_pc) == 0) {
265 ofs = sizeof(unsigned long);
266 pc = next_pc & ~3;
267 fp = next_fp;
268 leaf_return = 0;
269 } else {
270 pr_debug(" Failed to find previous stack frame\n");
271 break;
272 }
273
274 pr_debug(" Next PC=%p, next FP=%p\n",
275 (void *)next_pc, (void *)next_fp);
276 }
277}
278
279
280
281
282
283
284
285void microblaze_unwind(struct task_struct *task, struct stack_trace *trace)
286{
287 if (task) {
288 if (task == current) {
289 const struct pt_regs *regs = task_pt_regs(task);
290 microblaze_unwind_inner(task, regs->pc, regs->r1,
291 regs->r15, trace);
292 } else {
293 struct thread_info *thread_info =
294 (struct thread_info *)(task->stack);
295 const struct cpu_context *cpu_context =
296 &thread_info->cpu_context;
297
298 microblaze_unwind_inner(task,
299 (unsigned long) &_switch_to,
300 cpu_context->r1,
301 cpu_context->r15, trace);
302 }
303 } else {
304 unsigned long pc, fp;
305
306 __asm__ __volatile__ ("or %0, r1, r0" : "=r" (fp));
307
308 __asm__ __volatile__ (
309 "brlid %0, 0f;"
310 "nop;"
311 "0:"
312 : "=r" (pc)
313 );
314
315
316 microblaze_unwind_inner(current, pc, fp, 0, trace);
317 }
318}
319
320