1 // SPDX-License-Identifier: GPL-2.0-only
3 * arm64 callchain support
5 * Copyright (C) 2015 ARM Limited
7 #include <linux/perf_event.h>
8 #include <linux/stacktrace.h>
9 #include <linux/uaccess.h>
11 #include <asm/pointer_auth.h>
14 struct frame_tail __user *fp;
16 } __attribute__((packed));
19 * Get the return address for a single stackframe and return a pointer to the
22 static struct frame_tail __user *
23 user_backtrace(struct frame_tail __user *tail,
24 struct perf_callchain_entry_ctx *entry)
26 struct frame_tail buftail;
30 /* Also check accessibility of one struct frame_tail beyond */
31 if (!access_ok(tail, sizeof(buftail)))
35 err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
41 lr = ptrauth_strip_insn_pac(buftail.lr);
43 perf_callchain_store(entry, lr);
46 * Frame pointers should strictly progress back up the stack
47 * (towards higher addresses).
49 if (tail >= buftail.fp)
57 * The registers we're interested in are at the end of the variable
58 * length saved register structure. The fp points at the end of this
59 * structure so the address of this struct is:
60 * (struct compat_frame_tail *)(xxx->fp)-1
62 * This code has been adapted from the ARM OProfile support.
64 struct compat_frame_tail {
65 compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */
68 } __attribute__((packed));
70 static struct compat_frame_tail __user *
71 compat_user_backtrace(struct compat_frame_tail __user *tail,
72 struct perf_callchain_entry_ctx *entry)
74 struct compat_frame_tail buftail;
77 /* Also check accessibility of one struct frame_tail beyond */
78 if (!access_ok(tail, sizeof(buftail)))
82 err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
88 perf_callchain_store(entry, buftail.lr);
91 * Frame pointers should strictly progress back up the stack
92 * (towards higher addresses).
94 if (tail + 1 >= (struct compat_frame_tail __user *)
95 compat_ptr(buftail.fp))
98 return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
100 #endif /* CONFIG_COMPAT */
102 void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
103 struct pt_regs *regs)
105 if (perf_guest_state()) {
106 /* We don't support guest os callchain now */
110 perf_callchain_store(entry, regs->pc);
112 if (!compat_user_mode(regs)) {
114 struct frame_tail __user *tail;
116 tail = (struct frame_tail __user *)regs->regs[29];
118 while (entry->nr < entry->max_stack &&
119 tail && !((unsigned long)tail & 0x7))
120 tail = user_backtrace(tail, entry);
123 /* AARCH32 compat mode */
124 struct compat_frame_tail __user *tail;
126 tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
128 while ((entry->nr < entry->max_stack) &&
129 tail && !((unsigned long)tail & 0x3))
130 tail = compat_user_backtrace(tail, entry);
135 static bool callchain_trace(void *data, unsigned long pc)
137 struct perf_callchain_entry_ctx *entry = data;
138 return perf_callchain_store(entry, pc) == 0;
141 void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
142 struct pt_regs *regs)
144 if (perf_guest_state()) {
145 /* We don't support guest os callchain now */
149 arch_stack_walk(callchain_trace, entry, current, regs);
152 unsigned long perf_instruction_pointer(struct pt_regs *regs)
154 if (perf_guest_state())
155 return perf_guest_get_ip();
157 return instruction_pointer(regs);
160 unsigned long perf_misc_flags(struct pt_regs *regs)
162 unsigned int guest_state = perf_guest_state();
166 if (guest_state & PERF_GUEST_USER)
167 misc |= PERF_RECORD_MISC_GUEST_USER;
169 misc |= PERF_RECORD_MISC_GUEST_KERNEL;
172 misc |= PERF_RECORD_MISC_USER;
174 misc |= PERF_RECORD_MISC_KERNEL;