1 // SPDX-License-Identifier: GPL-2.0
2 #include "arm64-frame-pointer-unwind-support.h"
3 #include "callchain.h"
4 #include "event.h"
5 #include "unwind.h"
6 #include <string.h>
7
8 #define perf_event_arm_regs perf_event_arm64_regs
9 #include "../../arch/arm64/include/uapi/asm/perf_regs.h"
10 #undef perf_event_arm_regs
11
12 struct entries {
13 u64 stack[2];
14 size_t length;
15 };
16
17 #define SMPL_REG_MASK(b) (1ULL << (b))
18
get_leaf_frame_caller_enabled(struct perf_sample * sample)19 static bool get_leaf_frame_caller_enabled(struct perf_sample *sample)
20 {
21 struct regs_dump *regs;
22
23 if (callchain_param.record_mode != CALLCHAIN_FP)
24 return false;
25
26 regs = perf_sample__user_regs(sample);
27 return regs->regs && regs->mask & SMPL_REG_MASK(PERF_REG_ARM64_LR);
28 }
29
add_entry(struct unwind_entry * entry,void * arg)30 static int add_entry(struct unwind_entry *entry, void *arg)
31 {
32 struct entries *entries = arg;
33
34 entries->stack[entries->length++] = entry->ip;
35 return 0;
36 }
37
get_leaf_frame_caller_aarch64(struct perf_sample * sample,struct thread * thread,int usr_idx)38 u64 get_leaf_frame_caller_aarch64(struct perf_sample *sample, struct thread *thread, int usr_idx)
39 {
40 int ret;
41 struct entries entries = {};
42 struct regs_dump old_regs, *regs;
43
44 if (!get_leaf_frame_caller_enabled(sample))
45 return 0;
46
47 /*
48 * If PC and SP are not recorded, get the value of PC from the stack
49 * and set its mask. SP is not used when doing the unwinding but it
50 * still needs to be set to prevent failures.
51 */
52 regs = perf_sample__user_regs(sample);
53 memcpy(&old_regs, regs, sizeof(*regs));
54 if (!(regs->mask & SMPL_REG_MASK(PERF_REG_ARM64_PC))) {
55 regs->cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_PC);
56 regs->cache_regs[PERF_REG_ARM64_PC] = sample->callchain->ips[usr_idx+1];
57 }
58
59 if (!(regs->mask & SMPL_REG_MASK(PERF_REG_ARM64_SP))) {
60 regs->cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_SP);
61 regs->cache_regs[PERF_REG_ARM64_SP] = 0;
62 }
63
64 ret = unwind__get_entries(add_entry, &entries, thread, sample, 2, true);
65 memcpy(regs, &old_regs, sizeof(*regs));
66
67 if (ret || entries.length != 2)
68 return ret;
69
70 return callchain_param.order == ORDER_CALLER ? entries.stack[0] : entries.stack[1];
71 }
72