blob: f1205f92631df3b9289fe935b63d5a0d9667666e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Dave Peverley40a8b422008-12-16 09:35:40 +09002/*
3 * SH specific backtracing code for oprofile
4 *
5 * Copyright 2007 STMicroelectronics Ltd.
6 *
7 * Author: Dave Peverley <dpeverley@mpc-data.co.uk>
8 *
9 * Based on ARM oprofile backtrace code by Richard Purdie and in turn, i386
10 * oprofile backtrace code by John Levon, David Smith
Dave Peverley40a8b422008-12-16 09:35:40 +090011 */
12#include <linux/oprofile.h>
13#include <linux/sched.h>
14#include <linux/kallsyms.h>
15#include <linux/mm.h>
Matt Fleming0eff9f62009-08-11 22:43:20 +010016#include <asm/unwinder.h>
Dave Peverley40a8b422008-12-16 09:35:40 +090017#include <asm/ptrace.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
Dave Peverley40a8b422008-12-16 09:35:40 +090019#include <asm/sections.h>
Matt Fleming4e14dfc2009-08-07 16:11:19 +010020#include <asm/stacktrace.h>
21
Matt Fleming4e14dfc2009-08-07 16:11:19 +010022static int backtrace_stack(void *data, char *name)
23{
24 /* Yes, we want all stacks */
25 return 0;
26}
27
28static void backtrace_address(void *data, unsigned long addr, int reliable)
29{
30 unsigned int *depth = data;
31
32 if ((*depth)--)
33 oprofile_add_trace(addr);
34}
35
36static struct stacktrace_ops backtrace_ops = {
Matt Fleming4e14dfc2009-08-07 16:11:19 +010037 .stack = backtrace_stack,
38 .address = backtrace_address,
39};
Dave Peverley40a8b422008-12-16 09:35:40 +090040
41/* Limit to stop backtracing too far. */
42static int backtrace_limit = 20;
43
44static unsigned long *
45user_backtrace(unsigned long *stackaddr, struct pt_regs *regs)
46{
47 unsigned long buf_stack;
48
49 /* Also check accessibility of address */
Linus Torvalds96d4f262019-01-03 18:57:57 -080050 if (!access_ok(stackaddr, sizeof(unsigned long)))
Dave Peverley40a8b422008-12-16 09:35:40 +090051 return NULL;
52
53 if (__copy_from_user_inatomic(&buf_stack, stackaddr, sizeof(unsigned long)))
54 return NULL;
55
56 /* Quick paranoia check */
57 if (buf_stack & 3)
58 return NULL;
59
60 oprofile_add_trace(buf_stack);
61
62 stackaddr++;
63
64 return stackaddr;
65}
66
Dave Peverley40a8b422008-12-16 09:35:40 +090067void sh_backtrace(struct pt_regs * const regs, unsigned int depth)
68{
69 unsigned long *stackaddr;
70
71 /*
72 * Paranoia - clip max depth as we could get lost in the weeds.
73 */
74 if (depth > backtrace_limit)
75 depth = backtrace_limit;
76
Paul Mundtd1ba71f2010-10-27 16:58:22 +090077 stackaddr = (unsigned long *)kernel_stack_pointer(regs);
Dave Peverley40a8b422008-12-16 09:35:40 +090078 if (!user_mode(regs)) {
Matt Fleming4e14dfc2009-08-07 16:11:19 +010079 if (depth)
Matt Fleming0eff9f62009-08-11 22:43:20 +010080 unwind_stack(NULL, regs, stackaddr,
81 &backtrace_ops, &depth);
Dave Peverley40a8b422008-12-16 09:35:40 +090082 return;
83 }
84
85 while (depth-- && (stackaddr != NULL))
86 stackaddr = user_backtrace(stackaddr, regs);
87}