1 // SPDX-License-Identifier: GPL-2.0-only
3 * SH specific backtracing code for oprofile
5 * Copyright 2007 STMicroelectronics Ltd.
7 * Author: Dave Peverley <dpeverley@mpc-data.co.uk>
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 void backtrace_address(void *data
, unsigned long addr
, int reliable
)
24 unsigned int *depth
= data
;
27 oprofile_add_trace(addr
);
30 static struct stacktrace_ops backtrace_ops
= {
31 .address
= backtrace_address
,
34 /* Limit to stop backtracing too far. */
35 static int backtrace_limit
= 20;
37 static unsigned long *
38 user_backtrace(unsigned long *stackaddr
, struct pt_regs
*regs
)
40 unsigned long buf_stack
;
42 /* Also check accessibility of address */
43 if (!access_ok(stackaddr
, sizeof(unsigned long)))
46 if (__copy_from_user_inatomic(&buf_stack
, stackaddr
, sizeof(unsigned long)))
49 /* Quick paranoia check */
53 oprofile_add_trace(buf_stack
);
60 void sh_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
62 unsigned long *stackaddr
;
65 * Paranoia - clip max depth as we could get lost in the weeds.
67 if (depth
> backtrace_limit
)
68 depth
= backtrace_limit
;
70 stackaddr
= (unsigned long *)kernel_stack_pointer(regs
);
71 if (!user_mode(regs
)) {
73 unwind_stack(NULL
, regs
, stackaddr
,
74 &backtrace_ops
, &depth
);
78 while (depth
-- && (stackaddr
!= NULL
))
79 stackaddr
= user_backtrace(stackaddr
, regs
);