2 * SH specific backtracing code for oprofile
4 * Copyright 2007 STMicroelectronics Ltd.
6 * Author: Dave Peverley <dpeverley@mpc-data.co.uk>
8 * Based on ARM oprofile backtrace code by Richard Purdie and in turn, i386
9 * oprofile backtrace code by John Levon, David Smith
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/oprofile.h>
17 #include <linux/sched.h>
18 #include <linux/kallsyms.h>
20 #include <asm/ptrace.h>
21 #include <asm/uaccess.h>
22 #include <asm/sections.h>
24 /* Limit to stop backtracing too far. */
25 static int backtrace_limit = 20;
27 static unsigned long *
28 user_backtrace(unsigned long *stackaddr, struct pt_regs *regs)
30 unsigned long buf_stack;
32 /* Also check accessibility of address */
33 if (!access_ok(VERIFY_READ, stackaddr, sizeof(unsigned long)))
36 if (__copy_from_user_inatomic(&buf_stack, stackaddr, sizeof(unsigned long)))
39 /* Quick paranoia check */
43 oprofile_add_trace(buf_stack);
51 * | | /\ Higher addresses
53 * --------------- stack base (address of current_thread_info)
57 * --------------- saved regs->regs[15] value if valid
59 * --------------- struct pt_regs stored on stack (struct pt_regs *)
65 * | | \/ Lower addresses
67 * Thus, &pt_regs <-> stack base restricts the valid(ish) fp values
69 static int valid_kernel_stack(unsigned long *stackaddr, struct pt_regs *regs)
71 unsigned long stack = (unsigned long)regs;
72 unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
74 return ((unsigned long)stackaddr > stack) && ((unsigned long)stackaddr < stack_base);
77 static unsigned long *
78 kernel_backtrace(unsigned long *stackaddr, struct pt_regs *regs)
83 * If not a valid kernel address, keep going till we find one
84 * or the SP stops being a valid address.
88 oprofile_add_trace(addr);
89 } while (valid_kernel_stack(stackaddr, regs));
94 void sh_backtrace(struct pt_regs * const regs, unsigned int depth)
96 unsigned long *stackaddr;
99 * Paranoia - clip max depth as we could get lost in the weeds.
101 if (depth > backtrace_limit)
102 depth = backtrace_limit;
104 stackaddr = (unsigned long *)regs->regs[15];
105 if (!user_mode(regs)) {
106 while (depth-- && valid_kernel_stack(stackaddr, regs))
107 stackaddr = kernel_backtrace(stackaddr, regs);
112 while (depth-- && (stackaddr != NULL))
113 stackaddr = user_backtrace(stackaddr, regs);