^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * arm64 callchain support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/pointer_auth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct frame_tail {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct frame_tail __user *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) unsigned long lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) } __attribute__((packed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Get the return address for a single stackframe and return a pointer to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * next frame tail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct frame_tail __user *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) user_backtrace(struct frame_tail __user *tail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct perf_callchain_entry_ctx *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct frame_tail buftail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Also check accessibility of one struct frame_tail beyond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!access_ok(tail, sizeof(buftail)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) pagefault_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pagefault_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) lr = ptrauth_strip_insn_pac(buftail.lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) perf_callchain_store(entry, lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Frame pointers should strictly progress back up the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * (towards higher addresses).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (tail >= buftail.fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return buftail.fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * The registers we're interested in are at the end of the variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * length saved register structure. The fp points at the end of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * structure so the address of this struct is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * (struct compat_frame_tail *)(xxx->fp)-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * This code has been adapted from the ARM OProfile support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct compat_frame_tail {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) u32 sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u32 lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) } __attribute__((packed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static struct compat_frame_tail __user *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) compat_user_backtrace(struct compat_frame_tail __user *tail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct perf_callchain_entry_ctx *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct compat_frame_tail buftail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned long err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Also check accessibility of one struct frame_tail beyond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (!access_ok(tail, sizeof(buftail)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pagefault_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pagefault_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) perf_callchain_store(entry, buftail.lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Frame pointers should strictly progress back up the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * (towards higher addresses).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (tail + 1 >= (struct compat_frame_tail __user *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) compat_ptr(buftail.fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #endif /* CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (guest_cbs && guest_cbs->is_in_guest()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* We don't support guest os callchain now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) perf_callchain_store(entry, regs->pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!compat_user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* AARCH64 mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct frame_tail __user *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) tail = (struct frame_tail __user *)regs->regs[29];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) while (entry->nr < entry->max_stack &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) tail && !((unsigned long)tail & 0xf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) tail = user_backtrace(tail, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* AARCH32 compat mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct compat_frame_tail __user *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) while ((entry->nr < entry->max_stack) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) tail && !((unsigned long)tail & 0x3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) tail = compat_user_backtrace(tail, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Gets called by walk_stackframe() for every stackframe. This will be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * whist unwinding the stackframe and is like a subroutine return so we use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * the PC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static bool callchain_trace(void *data, unsigned long pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct perf_callchain_entry_ctx *entry = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) perf_callchain_store(entry, pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct stackframe frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (guest_cbs && guest_cbs->is_in_guest()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* We don't support guest os callchain now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) start_backtrace(&frame, regs->regs[29], regs->pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) walk_stackframe(current, &frame, callchain_trace, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned long perf_instruction_pointer(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (guest_cbs && guest_cbs->is_in_guest())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return guest_cbs->get_guest_ip();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unsigned long perf_misc_flags(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int misc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (guest_cbs && guest_cbs->is_in_guest()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (guest_cbs->is_user_mode())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) misc |= PERF_RECORD_MISC_GUEST_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) misc |= PERF_RECORD_MISC_GUEST_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (user_mode(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) misc |= PERF_RECORD_MISC_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) misc |= PERF_RECORD_MISC_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }