1 // SPDX-License-Identifier: GPL-2.0-only
3 * SH specific backtracing code for oprofile
5 * Copyright 2007 STMicroelectronics Ltd.
9 * Based on ARM oprofile backtrace code by Richard Purdie and in turn, i386
10 * oprofile backtrace code by John Levon, David Smith
12 #include <linux/oprofile.h>
13 #include <linux/sched.h>
14 #include <linux/kallsyms.h>
16 #include <asm/unwinder.h>
17 #include <asm/ptrace.h>
18 #include <linux/uaccess.h>
19 #include <asm/sections.h>
20 #include <asm/stacktrace.h>
22 static int backtrace_stack(void *data, char *name)
24 /* Yes, we want all stacks */
28 static void backtrace_address(void *data, unsigned long addr, int reliable)
30 unsigned int *depth = data;
33 oprofile_add_trace(addr);
36 static struct stacktrace_ops backtrace_ops = {
37 .stack = backtrace_stack,
38 .address = backtrace_address,
41 /* Limit to stop backtracing too far. */
42 static int backtrace_limit = 20;
44 static unsigned long *
45 user_backtrace(unsigned long *stackaddr, struct pt_regs *regs)
47 unsigned long buf_stack;
49 /* Also check accessibility of address */
50 if (!access_ok(stackaddr, sizeof(unsigned long)))
53 if (__copy_from_user_inatomic(&buf_stack, stackaddr, sizeof(unsigned long)))
56 /* Quick paranoia check */
60 oprofile_add_trace(buf_stack);
67 void sh_backtrace(struct pt_regs * const regs, unsigned int depth)
69 unsigned long *stackaddr;
72 * Paranoia - clip max depth as we could get lost in the weeds.
74 if (depth > backtrace_limit)
75 depth = backtrace_limit;
77 stackaddr = (unsigned long *)kernel_stack_pointer(regs);
78 if (!user_mode(regs)) {
80 unwind_stack(NULL, regs, stackaddr,
81 &backtrace_ops, &depth);
85 while (depth-- && (stackaddr != NULL))
86 stackaddr = user_backtrace(stackaddr, regs);