1 // SPDX-License-Identifier: GPL-2.0
3 * ARM callchain support
5 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
6 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
8 * This code is based on the ARM OProfile backtrace code.
10 #include <linux/perf_event.h>
11 #include <linux/uaccess.h>
13 #include <asm/stacktrace.h>
16 * The registers we're interested in are at the end of the variable
17 * length saved register structure. The fp points at the end of this
18 * structure so the address of this struct is:
19 * (struct frame_tail *)(xxx->fp)-1
21 * This code has been adapted from the ARM OProfile support.
24 struct frame_tail __user *fp;
27 } __attribute__((packed));
30 * Get the return address for a single stackframe and return a pointer to the
33 static struct frame_tail __user *
34 user_backtrace(struct frame_tail __user *tail,
35 struct perf_callchain_entry_ctx *entry)
37 struct frame_tail buftail;
40 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
44 err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
50 perf_callchain_store(entry, buftail.lr);
53 * Frame pointers should strictly progress back up the stack
54 * (towards higher addresses).
56 if (tail + 1 >= buftail.fp)
59 return buftail.fp - 1;
63 perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
65 struct frame_tail __user *tail;
67 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
68 /* We don't support guest os callchain now */
72 perf_callchain_store(entry, regs->ARM_pc);
77 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
79 while ((entry->nr < entry->max_stack) &&
80 tail && !((unsigned long)tail & 0x3))
81 tail = user_backtrace(tail, entry);
85 * Gets called by walk_stackframe() for every stackframe. This will be called
86 * whist unwinding the stackframe and is like a subroutine return so we use
90 callchain_trace(struct stackframe *fr,
93 struct perf_callchain_entry_ctx *entry = data;
94 perf_callchain_store(entry, fr->pc);
99 perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
101 struct stackframe fr;
103 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
104 /* We don't support guest os callchain now */
108 arm_get_current_stackframe(regs, &fr);
109 walk_stackframe(&fr, callchain_trace, entry);
112 unsigned long perf_instruction_pointer(struct pt_regs *regs)
114 if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
115 return perf_guest_cbs->get_guest_ip();
117 return instruction_pointer(regs);
120 unsigned long perf_misc_flags(struct pt_regs *regs)
124 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
125 if (perf_guest_cbs->is_user_mode())
126 misc |= PERF_RECORD_MISC_GUEST_USER;
128 misc |= PERF_RECORD_MISC_GUEST_KERNEL;
131 misc |= PERF_RECORD_MISC_USER;
133 misc |= PERF_RECORD_MISC_KERNEL;