~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/sh/kernel/cpu/sh5/unwind.c

Version: ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * arch/sh/kernel/cpu/sh5/unwind.c
  3  *
  4  * Copyright (C) 2004  Paul Mundt
  5  * Copyright (C) 2004  Richard Curnow
  6  *
  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.
 10  */
 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>
 19 #include <asm/unwinder.h>
 20 #include <asm/stacktrace.h>
 21 
 22 static u8 regcache[63];
 23 
 24 /*
 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.
 29  *
 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).
 35  *
 36  * In other cases, such as the interrupt/exception path, we can poke around
 37  * the sp/fp.
 38  *
 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).
 44  *                                              -- PFM.
 45  */
 46 static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
 47                       unsigned long *pprev_fp, unsigned long *pprev_pc,
 48                       struct pt_regs *regs)
 49 {
 50         const char *sym;
 51         char namebuf[128];
 52         unsigned long offset;
 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;
 58 
 59         sym = kallsyms_lookup(pc, NULL, &offset, NULL, namebuf);
 60         if (!sym)
 61                 return -EINVAL;
 62 
 63         prologue = pc - offset;
 64         if (!prologue)
 65                 return -EINVAL;
 66 
 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) ||
 72             ((fp & 7) != 0)) {
 73                 return -EINVAL;
 74         }
 75 
 76         /*
 77          * Depth to walk, depth is completely arbitrary.
 78          */
 79         for (i = 0; i < 100; i++, prologue += sizeof(unsigned long)) {
 80                 unsigned long op;
 81                 u8 major, minor;
 82                 u8 src, dest, disp;
 83 
 84                 op = *(unsigned long *)prologue;
 85 
 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;
 91 
 92                 /*
 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:
 96                  *
 97                  *      addi/addi.l r15, -FRAME_SIZE, r15
 98                  *
 99                  * in the event that the frame size is bigger than this, it's
100                  * typically created using a movi/sub pair as follows:
101                  *
102                  *      movi    FRAME_SIZE, rX
103                  *      sub     r15, rX, r15
104                  */
105 
106                 switch (major) {
107                 case (0x00 >> 2):
108                         switch (minor) {
109                         case 0x8: /* add.l */
110                         case 0x9: /* add */
111                                 /* Look for r15, r63, r14 */
112                                 if (src == 15 && disp == 63 && dest == 14)
113                                         found_prologue_end = 1;
114 
115                                 break;
116                         case 0xa: /* sub.l */
117                         case 0xb: /* sub */
118                                 if (src != 15 || dest != 15)
119                                         continue;
120 
121                                 fp_displacement -= regcache[disp];
122                                 fp_prev = fp - fp_displacement;
123                                 break;
124                         }
125                         break;
126                 case (0xa8 >> 2): /* st.l */
127                         if (src != 15)
128                                 continue;
129 
130                         switch (dest) {
131                         case 14:
132                                 if (offset_r14 || fp_displacement == 0)
133                                         continue;
134 
135                                 offset_r14 = (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
136                                 offset_r14 *= sizeof(unsigned long);
137                                 offset_r14 += fp_displacement;
138                                 break;
139                         case 18:
140                                 if (offset_r18 || fp_displacement == 0)
141                                         continue;
142 
143                                 offset_r18 = (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
144                                 offset_r18 *= sizeof(unsigned long);
145                                 offset_r18 += fp_displacement;
146                                 break;
147                         }
148 
149                         break;
150                 case (0xcc >> 2): /* movi */
151                         if (dest >= 63) {
152                                 printk(KERN_NOTICE "%s: Invalid dest reg %d "
153                                        "specified in movi handler. Failed "
154                                        "opcode was 0x%lx: ", __func__,
155                                        dest, op);
156 
157                                 continue;
158                         }
159 
160                         /* Sign extend */
161                         regcache[dest] =
162                                 sign_extend64((((u64)op >> 10) & 0xffff), 9);
163                         break;
164                 case (0xd0 >> 2): /* addi */
165                 case (0xd4 >> 2): /* addi.l */
166                         /* Look for r15, -FRAME_SIZE, r15 */
167                         if (src != 15 || dest != 15)
168                                 continue;
169 
170                         /* Sign extended frame size.. */
171                         fp_displacement +=
172                                 (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
173                         fp_prev = fp - fp_displacement;
174                         break;
175                 }
176 
177                 if (found_prologue_end && offset_r14 && (offset_r18 || *pprev_pc) && fp_prev)
178                         break;
179         }
180 
181         if (offset_r14 == 0 || fp_prev == 0) {
182                 if (!offset_r14)
183                         pr_debug("Unable to find r14 offset\n");
184                 if (!fp_prev)
185                         pr_debug("Unable to find previous fp\n");
186 
187                 return -EINVAL;
188         }
189 
190         /* For innermost leaf function, there might not be a offset_r18 */
191         if (!*pprev_pc && (offset_r18 == 0))
192                 return -EINVAL;
193 
194         *pprev_fp = *(unsigned long *)(fp_prev + offset_r14);
195 
196         if (offset_r18)
197                 *pprev_pc = *(unsigned long *)(fp_prev + offset_r18);
198 
199         *pprev_pc &= ~1;
200 
201         return 0;
202 }
203 
204 /*
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
207  * anyway.
208  */
209 static struct pt_regs here_regs;
210 
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;
215 
216 static void sh64_unwind_inner(const struct stacktrace_ops *ops,
217                               void *data, struct pt_regs *regs);
218 
219 static inline void unwind_nested(const struct stacktrace_ops *ops, void *data,
220                                  unsigned long pc, unsigned long fp)
221 {
222         if ((fp >= __MEMORY_START) &&
223             ((fp & 7) == 0))
224                 sh64_unwind_inner(ops, data, (struct pt_regs *)fp);
225 }
226 
227 static void sh64_unwind_inner(const struct stacktrace_ops *ops,
228                               void *data, struct pt_regs *regs)
229 {
230         unsigned long pc, fp;
231         int ofs = 0;
232         int first_pass;
233 
234         pc = regs->pc & ~1;
235         fp = regs->regs[14];
236 
237         first_pass = 1;
238         for (;;) {
239                 int cond;
240                 unsigned long next_fp, next_pc;
241 
242                 if (pc == ((unsigned long)&syscall_ret & ~1)) {
243                         printk("SYSCALL\n");
244                         unwind_nested(ops, data, pc, fp);
245                         return;
246                 }
247 
248                 if (pc == ((unsigned long)&ret_from_syscall & ~1)) {
249                         printk("SYSCALL (PREEMPTED)\n");
250                         unwind_nested(ops, data, pc, fp);
251                         return;
252                 }
253 
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);
259                         return;
260                 }
261 
262                 if (pc == ((unsigned long)&ret_from_irq & ~1)) {
263                         printk("IRQ\n");
264                         unwind_nested(ops, data, pc, fp);
265                         return;
266                 }
267 
268                 cond = ((pc >= __MEMORY_START) && (fp >= __MEMORY_START) &&
269                         ((pc & 3) == 0) && ((fp & 7) == 0));
270 
271                 pc -= ofs;
272 
273                 ops->address(data, pc, 1);
274 
275                 if (first_pass) {
276                         /* If the innermost frame is a leaf function, it's
277                          * possible that r18 is never saved out to the stack.
278                          */
279                         next_pc = regs->regs[18];
280                 } else {
281                         next_pc = 0;
282                 }
283 
284                 if (lookup_prev_stack_frame(fp, pc, &next_fp, &next_pc, regs) == 0) {
285                         ofs = sizeof(unsigned long);
286                         pc = next_pc & ~1;
287                         fp = next_fp;
288                 } else {
289                         printk("Unable to lookup previous stack frame\n");
290                         break;
291                 }
292                 first_pass = 0;
293         }
294 
295         printk("\n");
296 }
297 
298 static void sh64_unwinder_dump(struct task_struct *task,
299                                struct pt_regs *regs,
300                                unsigned long *sp,
301                                const struct stacktrace_ops *ops,
302                                void *data)
303 {
304         if (!regs) {
305                 /*
306                  * Fetch current regs if we have no other saved state to back
307                  * trace from.
308                  */
309                 regs = &here_regs;
310 
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]));
314 
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]));
323 
324                 __asm__ __volatile__ (
325                         "pta 0f, tr0\n\t"
326                         "blink tr0, %0\n\t"
327                         "0: nop"
328                         : "=r" (regs->pc)
329                 );
330         }
331 
332         sh64_unwind_inner(ops, data, regs);
333 }
334 
335 static struct unwinder sh64_unwinder = {
336         .name   = "sh64-unwinder",
337         .dump   = sh64_unwinder_dump,
338         .rating = 150,
339 };
340 
341 static int __init sh64_unwinder_init(void)
342 {
343         return unwinder_register(&sh64_unwinder);
344 }
345 early_initcall(sh64_unwinder_init);
346 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp