2 * arch/sh/kernel/cpu/sh5/unwind.c
4 * Copyright (C) 2004 Paul Mundt
5 * Copyright (C) 2004 Richard Curnow
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/kallsyms.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
16 #include <asm/ptrace.h>
17 #include <asm/processor.h>
19 #include <asm/unwinder.h>
20 #include <asm/stacktrace.h>
22 static u8 regcache
[63];
25 * Finding the previous stack frame isn't horribly straightforward as it is
26 * on some other platforms. In the sh64 case, we don't have "linked" stack
27 * frames, so we need to do a bit of work to determine the previous frame,
28 * and in turn, the previous r14/r18 pair.
30 * There are generally a few cases which determine where we can find out
31 * the r14/r18 values. In the general case, this can be determined by poking
32 * around the prologue of the symbol PC is in (note that we absolutely must
33 * have frame pointer support as well as the kernel symbol table mapped,
34 * otherwise we can't even get this far).
36 * In other cases, such as the interrupt/exception path, we can poke around
39 * Notably, this entire approach is somewhat error prone, and in the event
40 * that the previous frame cannot be determined, that's all we can do.
41 * Either way, this still leaves us with a more correct backtrace then what
42 * we would be able to come up with by walking the stack (which is garbage
43 * for anything beyond the first frame).
46 static int lookup_prev_stack_frame(unsigned long fp
, unsigned long pc
,
47 unsigned long *pprev_fp
, unsigned long *pprev_pc
,
53 unsigned long prologue
= 0;
54 unsigned long fp_displacement
= 0;
55 unsigned long fp_prev
= 0;
56 unsigned long offset_r14
= 0, offset_r18
= 0;
57 int i
, found_prologue_end
= 0;
59 sym
= kallsyms_lookup(pc
, NULL
, &offset
, NULL
, namebuf
);
63 prologue
= pc
- offset
;
67 /* Validate fp, to avoid risk of dereferencing a bad pointer later.
68 Assume 128Mb since that's the amount of RAM on a Cayman. Modify
69 when there is an SH-5 board with more. */
70 if ((fp
< (unsigned long) phys_to_virt(__MEMORY_START
)) ||
71 (fp
>= (unsigned long)(phys_to_virt(__MEMORY_START
)) + 128*1024*1024) ||
77 * Depth to walk, depth is completely arbitrary.
79 for (i
= 0; i
< 100; i
++, prologue
+= sizeof(unsigned long)) {
84 op
= *(unsigned long *)prologue
;
86 major
= (op
>> 26) & 0x3f;
87 src
= (op
>> 20) & 0x3f;
88 minor
= (op
>> 16) & 0xf;
89 disp
= (op
>> 10) & 0x3f;
90 dest
= (op
>> 4) & 0x3f;
93 * Stack frame creation happens in a number of ways.. in the
94 * general case when the stack frame is less than 511 bytes,
95 * it's generally created by an addi or addi.l:
97 * addi/addi.l r15, -FRAME_SIZE, r15
99 * in the event that the frame size is bigger than this, it's
100 * typically created using a movi/sub pair as follows:
102 * movi FRAME_SIZE, rX
109 case 0x8: /* add.l */
111 /* Look for r15, r63, r14 */
112 if (src
== 15 && disp
== 63 && dest
== 14)
113 found_prologue_end
= 1;
116 case 0xa: /* sub.l */
118 if (src
!= 15 || dest
!= 15)
121 fp_displacement
-= regcache
[disp
];
122 fp_prev
= fp
- fp_displacement
;
126 case (0xa8 >> 2): /* st.l */
132 if (offset_r14
|| fp_displacement
== 0)
135 offset_r14
= (u64
)(((((s64
)op
>> 10) & 0x3ff) << 54) >> 54);
136 offset_r14
*= sizeof(unsigned long);
137 offset_r14
+= fp_displacement
;
140 if (offset_r18
|| fp_displacement
== 0)
143 offset_r18
= (u64
)(((((s64
)op
>> 10) & 0x3ff) << 54) >> 54);
144 offset_r18
*= sizeof(unsigned long);
145 offset_r18
+= fp_displacement
;
150 case (0xcc >> 2): /* movi */
152 printk(KERN_NOTICE
"%s: Invalid dest reg %d "
153 "specified in movi handler. Failed "
154 "opcode was 0x%lx: ", __func__
,
162 ((((s64
)(u64
)op
>> 10) & 0xffff) << 54) >> 54;
164 case (0xd0 >> 2): /* addi */
165 case (0xd4 >> 2): /* addi.l */
166 /* Look for r15, -FRAME_SIZE, r15 */
167 if (src
!= 15 || dest
!= 15)
170 /* Sign extended frame size.. */
172 (u64
)(((((s64
)op
>> 10) & 0x3ff) << 54) >> 54);
173 fp_prev
= fp
- fp_displacement
;
177 if (found_prologue_end
&& offset_r14
&& (offset_r18
|| *pprev_pc
) && fp_prev
)
181 if (offset_r14
== 0 || fp_prev
== 0) {
183 pr_debug("Unable to find r14 offset\n");
185 pr_debug("Unable to find previous fp\n");
190 /* For innermost leaf function, there might not be a offset_r18 */
191 if (!*pprev_pc
&& (offset_r18
== 0))
194 *pprev_fp
= *(unsigned long *)(fp_prev
+ offset_r14
);
197 *pprev_pc
= *(unsigned long *)(fp_prev
+ offset_r18
);
205 * Don't put this on the stack since we'll want to call in to
206 * sh64_unwinder_dump() when we're close to underflowing the stack
209 static struct pt_regs here_regs
;
211 extern const char syscall_ret
;
212 extern const char ret_from_syscall
;
213 extern const char ret_from_exception
;
214 extern const char ret_from_irq
;
216 static void sh64_unwind_inner(const struct stacktrace_ops
*ops
,
217 void *data
, struct pt_regs
*regs
);
219 static inline void unwind_nested(const struct stacktrace_ops
*ops
, void *data
,
220 unsigned long pc
, unsigned long fp
)
222 if ((fp
>= __MEMORY_START
) &&
224 sh64_unwind_inner(ops
, data
, (struct pt_regs
*)fp
);
227 static void sh64_unwind_inner(const struct stacktrace_ops
*ops
,
228 void *data
, struct pt_regs
*regs
)
230 unsigned long pc
, fp
;
240 unsigned long next_fp
, next_pc
;
242 if (pc
== ((unsigned long)&syscall_ret
& ~1)) {
244 unwind_nested(ops
, data
, pc
, fp
);
248 if (pc
== ((unsigned long)&ret_from_syscall
& ~1)) {
249 printk("SYSCALL (PREEMPTED)\n");
250 unwind_nested(ops
, data
, pc
, fp
);
254 /* In this case, the PC is discovered by lookup_prev_stack_frame but
255 it has 4 taken off it to look like the 'caller' */
256 if (pc
== ((unsigned long)&ret_from_exception
& ~1)) {
257 printk("EXCEPTION\n");
258 unwind_nested(ops
, data
, pc
, fp
);
262 if (pc
== ((unsigned long)&ret_from_irq
& ~1)) {
264 unwind_nested(ops
, data
, pc
, fp
);
268 cond
= ((pc
>= __MEMORY_START
) && (fp
>= __MEMORY_START
) &&
269 ((pc
& 3) == 0) && ((fp
& 7) == 0));
273 ops
->address(data
, pc
, 1);
276 /* If the innermost frame is a leaf function, it's
277 * possible that r18 is never saved out to the stack.
279 next_pc
= regs
->regs
[18];
284 if (lookup_prev_stack_frame(fp
, pc
, &next_fp
, &next_pc
, regs
) == 0) {
285 ofs
= sizeof(unsigned long);
289 printk("Unable to lookup previous stack frame\n");
298 static void sh64_unwinder_dump(struct task_struct
*task
,
299 struct pt_regs
*regs
,
301 const struct stacktrace_ops
*ops
,
306 * Fetch current regs if we have no other saved state to back
311 __asm__
__volatile__ ("ori r14, 0, %0" : "=r" (regs
->regs
[14]));
312 __asm__
__volatile__ ("ori r15, 0, %0" : "=r" (regs
->regs
[15]));
313 __asm__
__volatile__ ("ori r18, 0, %0" : "=r" (regs
->regs
[18]));
315 __asm__
__volatile__ ("gettr tr0, %0" : "=r" (regs
->tregs
[0]));
316 __asm__
__volatile__ ("gettr tr1, %0" : "=r" (regs
->tregs
[1]));
317 __asm__
__volatile__ ("gettr tr2, %0" : "=r" (regs
->tregs
[2]));
318 __asm__
__volatile__ ("gettr tr3, %0" : "=r" (regs
->tregs
[3]));
319 __asm__
__volatile__ ("gettr tr4, %0" : "=r" (regs
->tregs
[4]));
320 __asm__
__volatile__ ("gettr tr5, %0" : "=r" (regs
->tregs
[5]));
321 __asm__
__volatile__ ("gettr tr6, %0" : "=r" (regs
->tregs
[6]));
322 __asm__
__volatile__ ("gettr tr7, %0" : "=r" (regs
->tregs
[7]));
324 __asm__
__volatile__ (
332 sh64_unwind_inner(ops
, data
, regs
);
335 static struct unwinder sh64_unwinder
= {
336 .name
= "sh64-unwinder",
337 .dump
= sh64_unwinder_dump
,
341 static int __init
sh64_unwinder_init(void)
343 return unwinder_register(&sh64_unwinder
);
345 early_initcall(sh64_unwinder_init
);