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