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/unwinder.h>
21 #include <asm/ptrace.h>
22 #include <asm/uaccess.h>
23 #include <asm/sections.h>
24 #include <asm/stacktrace.h>
26 static void backtrace_warning_symbol(void *data, char *msg,
32 static void backtrace_warning(void *data, char *msg)
37 static int backtrace_stack(void *data, char *name)
39 /* Yes, we want all stacks */
43 static void backtrace_address(void *data, unsigned long addr, int reliable)
45 unsigned int *depth = data;
48 oprofile_add_trace(addr);
51 static struct stacktrace_ops backtrace_ops = {
52 .warning = backtrace_warning,
53 .warning_symbol = backtrace_warning_symbol,
54 .stack = backtrace_stack,
55 .address = backtrace_address,
58 /* Limit to stop backtracing too far. */
59 static int backtrace_limit = 20;
61 static unsigned long *
62 user_backtrace(unsigned long *stackaddr, struct pt_regs *regs)
64 unsigned long buf_stack;
66 /* Also check accessibility of address */
67 if (!access_ok(VERIFY_READ, stackaddr, sizeof(unsigned long)))
70 if (__copy_from_user_inatomic(&buf_stack, stackaddr, sizeof(unsigned long)))
73 /* Quick paranoia check */
77 oprofile_add_trace(buf_stack);
85 * | | /\ Higher addresses
87 * --------------- stack base (address of current_thread_info)
91 * --------------- saved regs->regs[15] value if valid
93 * --------------- struct pt_regs stored on stack (struct pt_regs *)
99 * | | \/ Lower addresses
101 * Thus, &pt_regs <-> stack base restricts the valid(ish) fp values
103 static int valid_kernel_stack(unsigned long *stackaddr, struct pt_regs *regs)
105 unsigned long stack = (unsigned long)regs;
106 unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
108 return ((unsigned long)stackaddr > stack) && ((unsigned long)stackaddr < stack_base);
111 void sh_backtrace(struct pt_regs * const regs, unsigned int depth)
113 unsigned long *stackaddr;
116 * Paranoia - clip max depth as we could get lost in the weeds.
118 if (depth > backtrace_limit)
119 depth = backtrace_limit;
121 stackaddr = (unsigned long *)regs->regs[15];
122 if (!user_mode(regs)) {
124 unwind_stack(NULL, regs, stackaddr,
125 &backtrace_ops, &depth);
129 while (depth-- && (stackaddr != NULL))
130 stackaddr = user_backtrace(stackaddr, regs);