Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / sh / kernel / cpu / sh5 / unwind.c
blob11d03665fcb82b692c0849809a40bcbfff65b11a
1 /*
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
9 * for more details.
11 #include <linux/kallsyms.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <asm/page.h>
16 #include <asm/ptrace.h>
17 #include <asm/processor.h>
18 #include <asm/io.h>
20 static u8 regcache[63];
23 * Finding the previous stack frame isn't horribly straightforward as it is
24 * on some other platforms. In the sh64 case, we don't have "linked" stack
25 * frames, so we need to do a bit of work to determine the previous frame,
26 * and in turn, the previous r14/r18 pair.
28 * There are generally a few cases which determine where we can find out
29 * the r14/r18 values. In the general case, this can be determined by poking
30 * around the prologue of the symbol PC is in (note that we absolutely must
31 * have frame pointer support as well as the kernel symbol table mapped,
32 * otherwise we can't even get this far).
34 * In other cases, such as the interrupt/exception path, we can poke around
35 * the sp/fp.
37 * Notably, this entire approach is somewhat error prone, and in the event
38 * that the previous frame cannot be determined, that's all we can do.
39 * Either way, this still leaves us with a more correct backtrace then what
40 * we would be able to come up with by walking the stack (which is garbage
41 * for anything beyond the first frame).
42 * -- PFM.
44 static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
45 unsigned long *pprev_fp, unsigned long *pprev_pc,
46 struct pt_regs *regs)
48 const char *sym;
49 char namebuf[128];
50 unsigned long offset;
51 unsigned long prologue = 0;
52 unsigned long fp_displacement = 0;
53 unsigned long fp_prev = 0;
54 unsigned long offset_r14 = 0, offset_r18 = 0;
55 int i, found_prologue_end = 0;
57 sym = kallsyms_lookup(pc, NULL, &offset, NULL, namebuf);
58 if (!sym)
59 return -EINVAL;
61 prologue = pc - offset;
62 if (!prologue)
63 return -EINVAL;
65 /* Validate fp, to avoid risk of dereferencing a bad pointer later.
66 Assume 128Mb since that's the amount of RAM on a Cayman. Modify
67 when there is an SH-5 board with more. */
68 if ((fp < (unsigned long) phys_to_virt(__MEMORY_START)) ||
69 (fp >= (unsigned long)(phys_to_virt(__MEMORY_START)) + 128*1024*1024) ||
70 ((fp & 7) != 0)) {
71 return -EINVAL;
75 * Depth to walk, depth is completely arbitrary.
77 for (i = 0; i < 100; i++, prologue += sizeof(unsigned long)) {
78 unsigned long op;
79 u8 major, minor;
80 u8 src, dest, disp;
82 op = *(unsigned long *)prologue;
84 major = (op >> 26) & 0x3f;
85 src = (op >> 20) & 0x3f;
86 minor = (op >> 16) & 0xf;
87 disp = (op >> 10) & 0x3f;
88 dest = (op >> 4) & 0x3f;
91 * Stack frame creation happens in a number of ways.. in the
92 * general case when the stack frame is less than 511 bytes,
93 * it's generally created by an addi or addi.l:
95 * addi/addi.l r15, -FRAME_SIZE, r15
97 * in the event that the frame size is bigger than this, it's
98 * typically created using a movi/sub pair as follows:
100 * movi FRAME_SIZE, rX
101 * sub r15, rX, r15
104 switch (major) {
105 case (0x00 >> 2):
106 switch (minor) {
107 case 0x8: /* add.l */
108 case 0x9: /* add */
109 /* Look for r15, r63, r14 */
110 if (src == 15 && disp == 63 && dest == 14)
111 found_prologue_end = 1;
113 break;
114 case 0xa: /* sub.l */
115 case 0xb: /* sub */
116 if (src != 15 || dest != 15)
117 continue;
119 fp_displacement -= regcache[disp];
120 fp_prev = fp - fp_displacement;
121 break;
123 break;
124 case (0xa8 >> 2): /* st.l */
125 if (src != 15)
126 continue;
128 switch (dest) {
129 case 14:
130 if (offset_r14 || fp_displacement == 0)
131 continue;
133 offset_r14 = (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
134 offset_r14 *= sizeof(unsigned long);
135 offset_r14 += fp_displacement;
136 break;
137 case 18:
138 if (offset_r18 || fp_displacement == 0)
139 continue;
141 offset_r18 = (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
142 offset_r18 *= sizeof(unsigned long);
143 offset_r18 += fp_displacement;
144 break;
147 break;
148 case (0xcc >> 2): /* movi */
149 if (dest >= 63) {
150 printk(KERN_NOTICE "%s: Invalid dest reg %d "
151 "specified in movi handler. Failed "
152 <<<<<<< HEAD:arch/sh/kernel/cpu/sh5/unwind.c
153 "opcode was 0x%lx: ", __FUNCTION__,
154 =======
155 "opcode was 0x%lx: ", __func__,
156 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/sh/kernel/cpu/sh5/unwind.c
157 dest, op);
159 continue;
162 /* Sign extend */
163 regcache[dest] =
164 ((((s64)(u64)op >> 10) & 0xffff) << 54) >> 54;
165 break;
166 case (0xd0 >> 2): /* addi */
167 case (0xd4 >> 2): /* addi.l */
168 /* Look for r15, -FRAME_SIZE, r15 */
169 if (src != 15 || dest != 15)
170 continue;
172 /* Sign extended frame size.. */
173 fp_displacement +=
174 (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
175 fp_prev = fp - fp_displacement;
176 break;
179 if (found_prologue_end && offset_r14 && (offset_r18 || *pprev_pc) && fp_prev)
180 break;
183 if (offset_r14 == 0 || fp_prev == 0) {
184 if (!offset_r14)
185 pr_debug("Unable to find r14 offset\n");
186 if (!fp_prev)
187 pr_debug("Unable to find previous fp\n");
189 return -EINVAL;
192 /* For innermost leaf function, there might not be a offset_r18 */
193 if (!*pprev_pc && (offset_r18 == 0))
194 return -EINVAL;
196 *pprev_fp = *(unsigned long *)(fp_prev + offset_r14);
198 if (offset_r18)
199 *pprev_pc = *(unsigned long *)(fp_prev + offset_r18);
201 *pprev_pc &= ~1;
203 return 0;
206 /* Don't put this on the stack since we'll want to call sh64_unwind
207 * when we're close to underflowing the stack anyway. */
208 static struct pt_regs here_regs;
210 extern const char syscall_ret;
211 extern const char ret_from_syscall;
212 extern const char ret_from_exception;
213 extern const char ret_from_irq;
215 static void sh64_unwind_inner(struct pt_regs *regs);
217 static void unwind_nested (unsigned long pc, unsigned long fp)
219 if ((fp >= __MEMORY_START) &&
220 ((fp & 7) == 0)) {
221 sh64_unwind_inner((struct pt_regs *) fp);
225 static void sh64_unwind_inner(struct pt_regs *regs)
227 unsigned long pc, fp;
228 int ofs = 0;
229 int first_pass;
231 pc = regs->pc & ~1;
232 fp = regs->regs[14];
234 first_pass = 1;
235 for (;;) {
236 int cond;
237 unsigned long next_fp, next_pc;
239 if (pc == ((unsigned long) &syscall_ret & ~1)) {
240 printk("SYSCALL\n");
241 unwind_nested(pc,fp);
242 return;
245 if (pc == ((unsigned long) &ret_from_syscall & ~1)) {
246 printk("SYSCALL (PREEMPTED)\n");
247 unwind_nested(pc,fp);
248 return;
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(pc,fp);
256 return;
259 if (pc == ((unsigned long) &ret_from_irq & ~1)) {
260 printk("IRQ\n");
261 unwind_nested(pc,fp);
262 return;
265 cond = ((pc >= __MEMORY_START) && (fp >= __MEMORY_START) &&
266 ((pc & 3) == 0) && ((fp & 7) == 0));
268 pc -= ofs;
270 printk("[<%08lx>] ", pc);
271 print_symbol("%s\n", pc);
273 if (first_pass) {
274 /* If the innermost frame is a leaf function, it's
275 * possible that r18 is never saved out to the stack.
277 next_pc = regs->regs[18];
278 } else {
279 next_pc = 0;
282 if (lookup_prev_stack_frame(fp, pc, &next_fp, &next_pc, regs) == 0) {
283 ofs = sizeof(unsigned long);
284 pc = next_pc & ~1;
285 fp = next_fp;
286 } else {
287 printk("Unable to lookup previous stack frame\n");
288 break;
290 first_pass = 0;
293 printk("\n");
297 void sh64_unwind(struct pt_regs *regs)
299 if (!regs) {
301 * Fetch current regs if we have no other saved state to back
302 * trace from.
304 regs = &here_regs;
306 __asm__ __volatile__ ("ori r14, 0, %0" : "=r" (regs->regs[14]));
307 __asm__ __volatile__ ("ori r15, 0, %0" : "=r" (regs->regs[15]));
308 __asm__ __volatile__ ("ori r18, 0, %0" : "=r" (regs->regs[18]));
310 __asm__ __volatile__ ("gettr tr0, %0" : "=r" (regs->tregs[0]));
311 __asm__ __volatile__ ("gettr tr1, %0" : "=r" (regs->tregs[1]));
312 __asm__ __volatile__ ("gettr tr2, %0" : "=r" (regs->tregs[2]));
313 __asm__ __volatile__ ("gettr tr3, %0" : "=r" (regs->tregs[3]));
314 __asm__ __volatile__ ("gettr tr4, %0" : "=r" (regs->tregs[4]));
315 __asm__ __volatile__ ("gettr tr5, %0" : "=r" (regs->tregs[5]));
316 __asm__ __volatile__ ("gettr tr6, %0" : "=r" (regs->tregs[6]));
317 __asm__ __volatile__ ("gettr tr7, %0" : "=r" (regs->tregs[7]));
319 __asm__ __volatile__ (
320 "pta 0f, tr0\n\t"
321 "blink tr0, %0\n\t"
322 "0: nop"
323 : "=r" (regs->pc)
327 printk("\nCall Trace:\n");
328 sh64_unwind_inner(regs);