Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 2 | /* |
| 3 | * arm64 callchain support |
| 4 | * |
| 5 | * Copyright (C) 2015 ARM Limited |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/perf_event.h> |
Madhavan T. Venkataraman | ed876d3 | 2021-11-29 14:28:44 +0000 | [diff] [blame] | 8 | #include <linux/stacktrace.h> |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
| 10 | |
Mark Rutland | ccc4381 | 2018-12-07 18:39:27 +0000 | [diff] [blame] | 11 | #include <asm/pointer_auth.h> |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 12 | |
| 13 | struct frame_tail { |
| 14 | struct frame_tail __user *fp; |
| 15 | unsigned long lr; |
| 16 | } __attribute__((packed)); |
| 17 | |
| 18 | /* |
| 19 | * Get the return address for a single stackframe and return a pointer to the |
| 20 | * next frame tail. |
| 21 | */ |
| 22 | static struct frame_tail __user * |
| 23 | user_backtrace(struct frame_tail __user *tail, |
Arnaldo Carvalho de Melo | cfbcf46 | 2016-04-28 12:30:53 -0300 | [diff] [blame] | 24 | struct perf_callchain_entry_ctx *entry) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 25 | { |
| 26 | struct frame_tail buftail; |
| 27 | unsigned long err; |
Mark Rutland | ccc4381 | 2018-12-07 18:39:27 +0000 | [diff] [blame] | 28 | unsigned long lr; |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 29 | |
| 30 | /* Also check accessibility of one struct frame_tail beyond */ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 31 | if (!access_ok(tail, sizeof(buftail))) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 32 | return NULL; |
| 33 | |
| 34 | pagefault_disable(); |
| 35 | err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail)); |
| 36 | pagefault_enable(); |
| 37 | |
| 38 | if (err) |
| 39 | return NULL; |
| 40 | |
Mark Rutland | ccc4381 | 2018-12-07 18:39:27 +0000 | [diff] [blame] | 41 | lr = ptrauth_strip_insn_pac(buftail.lr); |
| 42 | |
| 43 | perf_callchain_store(entry, lr); |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Frame pointers should strictly progress back up the stack |
| 47 | * (towards higher addresses). |
| 48 | */ |
| 49 | if (tail >= buftail.fp) |
| 50 | return NULL; |
| 51 | |
| 52 | return buftail.fp; |
| 53 | } |
| 54 | |
| 55 | #ifdef CONFIG_COMPAT |
| 56 | /* |
| 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 |
| 61 | * |
| 62 | * This code has been adapted from the ARM OProfile support. |
| 63 | */ |
| 64 | struct compat_frame_tail { |
| 65 | compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */ |
| 66 | u32 sp; |
| 67 | u32 lr; |
| 68 | } __attribute__((packed)); |
| 69 | |
| 70 | static struct compat_frame_tail __user * |
| 71 | compat_user_backtrace(struct compat_frame_tail __user *tail, |
Arnaldo Carvalho de Melo | cfbcf46 | 2016-04-28 12:30:53 -0300 | [diff] [blame] | 72 | struct perf_callchain_entry_ctx *entry) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 73 | { |
| 74 | struct compat_frame_tail buftail; |
| 75 | unsigned long err; |
| 76 | |
| 77 | /* Also check accessibility of one struct frame_tail beyond */ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 78 | if (!access_ok(tail, sizeof(buftail))) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 79 | return NULL; |
| 80 | |
| 81 | pagefault_disable(); |
| 82 | err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail)); |
| 83 | pagefault_enable(); |
| 84 | |
| 85 | if (err) |
| 86 | return NULL; |
| 87 | |
| 88 | perf_callchain_store(entry, buftail.lr); |
| 89 | |
| 90 | /* |
| 91 | * Frame pointers should strictly progress back up the stack |
| 92 | * (towards higher addresses). |
| 93 | */ |
| 94 | if (tail + 1 >= (struct compat_frame_tail __user *) |
| 95 | compat_ptr(buftail.fp)) |
| 96 | return NULL; |
| 97 | |
| 98 | return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1; |
| 99 | } |
| 100 | #endif /* CONFIG_COMPAT */ |
| 101 | |
Arnaldo Carvalho de Melo | cfbcf46 | 2016-04-28 12:30:53 -0300 | [diff] [blame] | 102 | void perf_callchain_user(struct perf_callchain_entry_ctx *entry, |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 103 | struct pt_regs *regs) |
| 104 | { |
Sean Christopherson | 1c34305 | 2021-11-11 02:07:28 +0000 | [diff] [blame] | 105 | if (perf_guest_state()) { |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 106 | /* We don't support guest os callchain now */ |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | perf_callchain_store(entry, regs->pc); |
| 111 | |
| 112 | if (!compat_user_mode(regs)) { |
| 113 | /* AARCH64 mode */ |
| 114 | struct frame_tail __user *tail; |
| 115 | |
| 116 | tail = (struct frame_tail __user *)regs->regs[29]; |
| 117 | |
Arnaldo Carvalho de Melo | 3b1fff0 | 2016-05-10 18:08:32 -0300 | [diff] [blame] | 118 | while (entry->nr < entry->max_stack && |
Peter Collingbourne | 33c222a | 2021-05-26 10:49:26 -0700 | [diff] [blame] | 119 | tail && !((unsigned long)tail & 0x7)) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 120 | tail = user_backtrace(tail, entry); |
| 121 | } else { |
| 122 | #ifdef CONFIG_COMPAT |
| 123 | /* AARCH32 compat mode */ |
| 124 | struct compat_frame_tail __user *tail; |
| 125 | |
| 126 | tail = (struct compat_frame_tail __user *)regs->compat_fp - 1; |
| 127 | |
Arnaldo Carvalho de Melo | 3b1fff0 | 2016-05-10 18:08:32 -0300 | [diff] [blame] | 128 | while ((entry->nr < entry->max_stack) && |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 129 | tail && !((unsigned long)tail & 0x3)) |
| 130 | tail = compat_user_backtrace(tail, entry); |
| 131 | #endif |
| 132 | } |
| 133 | } |
| 134 | |
Mark Brown | baa2cd4 | 2020-09-14 16:34:08 +0100 | [diff] [blame] | 135 | static bool callchain_trace(void *data, unsigned long pc) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 136 | { |
Arnaldo Carvalho de Melo | cfbcf46 | 2016-04-28 12:30:53 -0300 | [diff] [blame] | 137 | struct perf_callchain_entry_ctx *entry = data; |
Madhavan T. Venkataraman | ed876d3 | 2021-11-29 14:28:44 +0000 | [diff] [blame] | 138 | return perf_callchain_store(entry, pc) == 0; |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Arnaldo Carvalho de Melo | cfbcf46 | 2016-04-28 12:30:53 -0300 | [diff] [blame] | 141 | void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 142 | struct pt_regs *regs) |
| 143 | { |
Sean Christopherson | 1c34305 | 2021-11-11 02:07:28 +0000 | [diff] [blame] | 144 | if (perf_guest_state()) { |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 145 | /* We don't support guest os callchain now */ |
| 146 | return; |
| 147 | } |
| 148 | |
Madhavan T. Venkataraman | ed876d3 | 2021-11-29 14:28:44 +0000 | [diff] [blame] | 149 | arch_stack_walk(callchain_trace, entry, current, regs); |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
| 153 | { |
Sean Christopherson | 1c34305 | 2021-11-11 02:07:28 +0000 | [diff] [blame] | 154 | if (perf_guest_state()) |
| 155 | return perf_guest_get_ip(); |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 156 | |
| 157 | return instruction_pointer(regs); |
| 158 | } |
| 159 | |
| 160 | unsigned long perf_misc_flags(struct pt_regs *regs) |
| 161 | { |
Sean Christopherson | 1c34305 | 2021-11-11 02:07:28 +0000 | [diff] [blame] | 162 | unsigned int guest_state = perf_guest_state(); |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 163 | int misc = 0; |
| 164 | |
Like Xu | b9f5621 | 2021-11-11 02:07:27 +0000 | [diff] [blame] | 165 | if (guest_state) { |
| 166 | if (guest_state & PERF_GUEST_USER) |
Mark Rutland | 52da443e | 2015-07-06 12:23:54 +0100 | [diff] [blame] | 167 | misc |= PERF_RECORD_MISC_GUEST_USER; |
| 168 | else |
| 169 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; |
| 170 | } else { |
| 171 | if (user_mode(regs)) |
| 172 | misc |= PERF_RECORD_MISC_USER; |
| 173 | else |
| 174 | misc |= PERF_RECORD_MISC_KERNEL; |
| 175 | } |
| 176 | |
| 177 | return misc; |
| 178 | } |