1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sh/kernel/cpu/sh5/unwind.c
5 * Copyright (C) 2004 Paul Mundt
6 * Copyright (C) 2004 Richard Curnow
8 #include <linux/kallsyms.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
13 #include <asm/ptrace.h>
14 #include <asm/processor.h>
16 #include <asm/unwinder.h>
17 #include <asm/stacktrace.h>
19 static u8 regcache
[63];
22 * Finding the previous stack frame isn't horribly straightforward as it is
23 * on some other platforms. In the sh64 case, we don't have "linked" stack
24 * frames, so we need to do a bit of work to determine the previous frame,
25 * and in turn, the previous r14/r18 pair.
27 * There are generally a few cases which determine where we can find out
28 * the r14/r18 values. In the general case, this can be determined by poking
29 * around the prologue of the symbol PC is in (note that we absolutely must
30 * have frame pointer support as well as the kernel symbol table mapped,
31 * otherwise we can't even get this far).
33 * In other cases, such as the interrupt/exception path, we can poke around
36 * Notably, this entire approach is somewhat error prone, and in the event
37 * that the previous frame cannot be determined, that's all we can do.
38 * Either way, this still leaves us with a more correct backtrace then what
39 * we would be able to come up with by walking the stack (which is garbage
40 * for anything beyond the first frame).
43 static int lookup_prev_stack_frame(unsigned long fp
, unsigned long pc
,
44 unsigned long *pprev_fp
, unsigned long *pprev_pc
,
50 unsigned long prologue
= 0;
51 unsigned long fp_displacement
= 0;
52 unsigned long fp_prev
= 0;
53 unsigned long offset_r14
= 0, offset_r18
= 0;
54 int i
, found_prologue_end
= 0;
56 sym
= kallsyms_lookup(pc
, NULL
, &offset
, NULL
, namebuf
);
60 prologue
= pc
- offset
;
64 /* Validate fp, to avoid risk of dereferencing a bad pointer later.
65 Assume 128Mb since that's the amount of RAM on a Cayman. Modify
66 when there is an SH-5 board with more. */
67 if ((fp
< (unsigned long) phys_to_virt(__MEMORY_START
)) ||
68 (fp
>= (unsigned long)(phys_to_virt(__MEMORY_START
)) + 128*1024*1024) ||
74 * Depth to walk, depth is completely arbitrary.
76 for (i
= 0; i
< 100; i
++, prologue
+= sizeof(unsigned long)) {
81 op
= *(unsigned long *)prologue
;
83 major
= (op
>> 26) & 0x3f;
84 src
= (op
>> 20) & 0x3f;
85 minor
= (op
>> 16) & 0xf;
86 disp
= (op
>> 10) & 0x3f;
87 dest
= (op
>> 4) & 0x3f;
90 * Stack frame creation happens in a number of ways.. in the
91 * general case when the stack frame is less than 511 bytes,
92 * it's generally created by an addi or addi.l:
94 * addi/addi.l r15, -FRAME_SIZE, r15
96 * in the event that the frame size is bigger than this, it's
97 * typically created using a movi/sub pair as follows:
106 case 0x8: /* add.l */
108 /* Look for r15, r63, r14 */
109 if (src
== 15 && disp
== 63 && dest
== 14)
110 found_prologue_end
= 1;
113 case 0xa: /* sub.l */
115 if (src
!= 15 || dest
!= 15)
118 fp_displacement
-= regcache
[disp
];
119 fp_prev
= fp
- fp_displacement
;
123 case (0xa8 >> 2): /* st.l */
129 if (offset_r14
|| fp_displacement
== 0)
132 offset_r14
= (u64
)(((((s64
)op
>> 10) & 0x3ff) << 54) >> 54);
133 offset_r14
*= sizeof(unsigned long);
134 offset_r14
+= fp_displacement
;
137 if (offset_r18
|| fp_displacement
== 0)
140 offset_r18
= (u64
)(((((s64
)op
>> 10) & 0x3ff) << 54) >> 54);
141 offset_r18
*= sizeof(unsigned long);
142 offset_r18
+= fp_displacement
;
147 case (0xcc >> 2): /* movi */
149 printk(KERN_NOTICE
"%s: Invalid dest reg %d "
150 "specified in movi handler. Failed "
151 "opcode was 0x%lx: ", __func__
,
159 sign_extend64((((u64
)op
>> 10) & 0xffff), 9);
161 case (0xd0 >> 2): /* addi */
162 case (0xd4 >> 2): /* addi.l */
163 /* Look for r15, -FRAME_SIZE, r15 */
164 if (src
!= 15 || dest
!= 15)
167 /* Sign extended frame size.. */
169 (u64
)(((((s64
)op
>> 10) & 0x3ff) << 54) >> 54);
170 fp_prev
= fp
- fp_displacement
;
174 if (found_prologue_end
&& offset_r14
&& (offset_r18
|| *pprev_pc
) && fp_prev
)
178 if (offset_r14
== 0 || fp_prev
== 0) {
180 pr_debug("Unable to find r14 offset\n");
182 pr_debug("Unable to find previous fp\n");
187 /* For innermost leaf function, there might not be a offset_r18 */
188 if (!*pprev_pc
&& (offset_r18
== 0))
191 *pprev_fp
= *(unsigned long *)(fp_prev
+ offset_r14
);
194 *pprev_pc
= *(unsigned long *)(fp_prev
+ offset_r18
);
202 * Don't put this on the stack since we'll want to call in to
203 * sh64_unwinder_dump() when we're close to underflowing the stack
206 static struct pt_regs here_regs
;
208 extern const char syscall_ret
;
209 extern const char ret_from_syscall
;
210 extern const char ret_from_exception
;
211 extern const char ret_from_irq
;
213 static void sh64_unwind_inner(const struct stacktrace_ops
*ops
,
214 void *data
, struct pt_regs
*regs
);
216 static inline void unwind_nested(const struct stacktrace_ops
*ops
, void *data
,
217 unsigned long pc
, unsigned long fp
)
219 if ((fp
>= __MEMORY_START
) &&
221 sh64_unwind_inner(ops
, data
, (struct pt_regs
*)fp
);
224 static void sh64_unwind_inner(const struct stacktrace_ops
*ops
,
225 void *data
, struct pt_regs
*regs
)
227 unsigned long pc
, fp
;
237 unsigned long next_fp
, next_pc
;
239 if (pc
== ((unsigned long)&syscall_ret
& ~1)) {
241 unwind_nested(ops
, data
, pc
, fp
);
245 if (pc
== ((unsigned long)&ret_from_syscall
& ~1)) {
246 printk("SYSCALL (PREEMPTED)\n");
247 unwind_nested(ops
, data
, pc
, fp
);
251 /* In this case, the PC is discovered by lookup_prev_stack_frame but
252 it has 4 taken off it to look like the 'caller' */
253 if (pc
== ((unsigned long)&ret_from_exception
& ~1)) {
254 printk("EXCEPTION\n");
255 unwind_nested(ops
, data
, pc
, fp
);
259 if (pc
== ((unsigned long)&ret_from_irq
& ~1)) {
261 unwind_nested(ops
, data
, pc
, fp
);
265 cond
= ((pc
>= __MEMORY_START
) && (fp
>= __MEMORY_START
) &&
266 ((pc
& 3) == 0) && ((fp
& 7) == 0));
270 ops
->address(data
, pc
, 1);
273 /* If the innermost frame is a leaf function, it's
274 * possible that r18 is never saved out to the stack.
276 next_pc
= regs
->regs
[18];
281 if (lookup_prev_stack_frame(fp
, pc
, &next_fp
, &next_pc
, regs
) == 0) {
282 ofs
= sizeof(unsigned long);
286 printk("Unable to lookup previous stack frame\n");
295 static void sh64_unwinder_dump(struct task_struct
*task
,
296 struct pt_regs
*regs
,
298 const struct stacktrace_ops
*ops
,
303 * Fetch current regs if we have no other saved state to back
308 __asm__
__volatile__ ("ori r14, 0, %0" : "=r" (regs
->regs
[14]));
309 __asm__
__volatile__ ("ori r15, 0, %0" : "=r" (regs
->regs
[15]));
310 __asm__
__volatile__ ("ori r18, 0, %0" : "=r" (regs
->regs
[18]));
312 __asm__
__volatile__ ("gettr tr0, %0" : "=r" (regs
->tregs
[0]));
313 __asm__
__volatile__ ("gettr tr1, %0" : "=r" (regs
->tregs
[1]));
314 __asm__
__volatile__ ("gettr tr2, %0" : "=r" (regs
->tregs
[2]));
315 __asm__
__volatile__ ("gettr tr3, %0" : "=r" (regs
->tregs
[3]));
316 __asm__
__volatile__ ("gettr tr4, %0" : "=r" (regs
->tregs
[4]));
317 __asm__
__volatile__ ("gettr tr5, %0" : "=r" (regs
->tregs
[5]));
318 __asm__
__volatile__ ("gettr tr6, %0" : "=r" (regs
->tregs
[6]));
319 __asm__
__volatile__ ("gettr tr7, %0" : "=r" (regs
->tregs
[7]));
321 __asm__
__volatile__ (
329 sh64_unwind_inner(ops
, data
, regs
);
332 static struct unwinder sh64_unwinder
= {
333 .name
= "sh64-unwinder",
334 .dump
= sh64_unwinder_dump
,
338 static int __init
sh64_unwinder_init(void)
340 return unwinder_register(&sh64_unwinder
);
342 early_initcall(sh64_unwinder_init
);