Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Code for replacing ftrace calls with jumps. |
| 4 | * |
| 5 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 6 | * |
| 7 | * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box. |
| 8 | * |
| 9 | * Added function graph tracer code, taken from x86 that was written |
| 10 | * by Frederic Weisbecker, and ported to PPC by Steven Rostedt. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "ftrace-powerpc: " fmt |
| 15 | |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/hardirq.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/ftrace.h> |
| 21 | #include <linux/percpu.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/list.h> |
| 24 | |
| 25 | #include <asm/asm-prototypes.h> |
| 26 | #include <asm/cacheflush.h> |
| 27 | #include <asm/code-patching.h> |
| 28 | #include <asm/ftrace.h> |
| 29 | #include <asm/syscall.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 30 | #include <asm/inst.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | #ifdef CONFIG_DYNAMIC_FTRACE |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * We generally only have a single long_branch tramp and at most 2 or 3 plt |
| 37 | * tramps generated. But, we don't use the plt tramps currently. We also allot |
| 38 | * 2 tramps after .text and .init.text. So, we only end up with around 3 usable |
| 39 | * tramps in total. Set aside 8 just to be sure. |
| 40 | */ |
| 41 | #define NUM_FTRACE_TRAMPS 8 |
| 42 | static unsigned long ftrace_tramps[NUM_FTRACE_TRAMPS]; |
| 43 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 44 | static struct ppc_inst |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | ftrace_call_replace(unsigned long ip, unsigned long addr, int link) |
| 46 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 47 | struct ppc_inst op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | |
| 49 | addr = ppc_function_entry((void *)addr); |
| 50 | |
| 51 | /* if (link) set op to 'bl' else 'b' */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 52 | create_branch(&op, (struct ppc_inst *)ip, addr, link ? 1 : 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | |
| 54 | return op; |
| 55 | } |
| 56 | |
| 57 | static int |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 58 | ftrace_modify_code(unsigned long ip, struct ppc_inst old, struct ppc_inst new) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 59 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 60 | struct ppc_inst replaced; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Note: |
| 64 | * We are paranoid about modifying text, as if a bug was to happen, it |
| 65 | * could cause us to read or write to someplace that could cause harm. |
| 66 | * Carefully read and modify the code with probe_kernel_*(), and make |
| 67 | * sure what we read is what we expected it to be before modifying it. |
| 68 | */ |
| 69 | |
| 70 | /* read the text we want to modify */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 71 | if (probe_kernel_read_inst(&replaced, (void *)ip)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 72 | return -EFAULT; |
| 73 | |
| 74 | /* Make sure it is what we expect it to be */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 75 | if (!ppc_inst_equal(replaced, old)) { |
| 76 | pr_err("%p: replaced (%s) != old (%s)", |
| 77 | (void *)ip, ppc_inst_as_str(replaced), ppc_inst_as_str(old)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | /* replace the text with the new text */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 82 | if (patch_instruction((struct ppc_inst *)ip, new)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 83 | return -EPERM; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Helper functions that are the same for both PPC64 and PPC32. |
| 90 | */ |
| 91 | static int test_24bit_addr(unsigned long ip, unsigned long addr) |
| 92 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 93 | struct ppc_inst op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | addr = ppc_function_entry((void *)addr); |
| 95 | |
| 96 | /* use the create_branch to verify that this offset can be branched */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 97 | return create_branch(&op, (struct ppc_inst *)ip, addr, 0) == 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 100 | static int is_bl_op(struct ppc_inst op) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 101 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 102 | return (ppc_inst_val(op) & 0xfc000003) == 0x48000001; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 105 | static int is_b_op(struct ppc_inst op) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 106 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 107 | return (ppc_inst_val(op) & 0xfc000003) == 0x48000000; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 110 | static unsigned long find_bl_target(unsigned long ip, struct ppc_inst op) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 111 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 112 | int offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 113 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 114 | offset = (ppc_inst_val(op) & 0x03fffffc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 115 | /* make it signed */ |
| 116 | if (offset & 0x02000000) |
| 117 | offset |= 0xfe000000; |
| 118 | |
| 119 | return ip + (long)offset; |
| 120 | } |
| 121 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 122 | #ifdef CONFIG_MODULES |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 123 | #ifdef CONFIG_PPC64 |
| 124 | static int |
| 125 | __ftrace_make_nop(struct module *mod, |
| 126 | struct dyn_ftrace *rec, unsigned long addr) |
| 127 | { |
| 128 | unsigned long entry, ptr, tramp; |
| 129 | unsigned long ip = rec->ip; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 130 | struct ppc_inst op, pop; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 131 | |
| 132 | /* read where this goes */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 133 | if (probe_kernel_read_inst(&op, (void *)ip)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | pr_err("Fetching opcode failed.\n"); |
| 135 | return -EFAULT; |
| 136 | } |
| 137 | |
| 138 | /* Make sure that that this is still a 24bit jump */ |
| 139 | if (!is_bl_op(op)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 140 | pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | /* lets find where the pointer goes */ |
| 145 | tramp = find_bl_target(ip, op); |
| 146 | |
| 147 | pr_devel("ip:%lx jumps to %lx", ip, tramp); |
| 148 | |
| 149 | if (module_trampoline_target(mod, tramp, &ptr)) { |
| 150 | pr_err("Failed to get trampoline target\n"); |
| 151 | return -EFAULT; |
| 152 | } |
| 153 | |
| 154 | pr_devel("trampoline target %lx", ptr); |
| 155 | |
| 156 | entry = ppc_global_function_entry((void *)addr); |
| 157 | /* This should match what was called */ |
| 158 | if (ptr != entry) { |
| 159 | pr_err("addr %lx does not match expected %lx\n", ptr, entry); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | #ifdef CONFIG_MPROFILE_KERNEL |
| 164 | /* When using -mkernel_profile there is no load to jump over */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 165 | pop = ppc_inst(PPC_INST_NOP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 166 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 167 | if (probe_kernel_read_inst(&op, (void *)(ip - 4))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 168 | pr_err("Fetching instruction at %lx failed.\n", ip - 4); |
| 169 | return -EFAULT; |
| 170 | } |
| 171 | |
| 172 | /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 173 | if (!ppc_inst_equal(op, ppc_inst(PPC_INST_MFLR)) && |
| 174 | !ppc_inst_equal(op, ppc_inst(PPC_INST_STD_LR))) { |
| 175 | pr_err("Unexpected instruction %s around bl _mcount\n", |
| 176 | ppc_inst_as_str(op)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | } |
| 179 | #else |
| 180 | /* |
| 181 | * Our original call site looks like: |
| 182 | * |
| 183 | * bl <tramp> |
| 184 | * ld r2,XX(r1) |
| 185 | * |
| 186 | * Milton Miller pointed out that we can not simply nop the branch. |
| 187 | * If a task was preempted when calling a trace function, the nops |
| 188 | * will remove the way to restore the TOC in r2 and the r2 TOC will |
| 189 | * get corrupted. |
| 190 | * |
| 191 | * Use a b +8 to jump over the load. |
| 192 | */ |
| 193 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 194 | pop = ppc_inst(PPC_INST_BRANCH | 8); /* b +8 */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Check what is in the next instruction. We can see ld r2,40(r1), but |
| 198 | * on first pass after boot we will see mflr r0. |
| 199 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 200 | if (probe_kernel_read_inst(&op, (void *)(ip + 4))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 201 | pr_err("Fetching op failed.\n"); |
| 202 | return -EFAULT; |
| 203 | } |
| 204 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 205 | if (!ppc_inst_equal(op, ppc_inst(PPC_INST_LD_TOC))) { |
| 206 | pr_err("Expected %08x found %s\n", PPC_INST_LD_TOC, ppc_inst_as_str(op)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 207 | return -EINVAL; |
| 208 | } |
| 209 | #endif /* CONFIG_MPROFILE_KERNEL */ |
| 210 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 211 | if (patch_instruction((struct ppc_inst *)ip, pop)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 212 | pr_err("Patching NOP failed.\n"); |
| 213 | return -EPERM; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | #else /* !PPC64 */ |
| 220 | static int |
| 221 | __ftrace_make_nop(struct module *mod, |
| 222 | struct dyn_ftrace *rec, unsigned long addr) |
| 223 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 224 | struct ppc_inst op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 225 | unsigned int jmp[4]; |
| 226 | unsigned long ip = rec->ip; |
| 227 | unsigned long tramp; |
| 228 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 229 | if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 230 | return -EFAULT; |
| 231 | |
| 232 | /* Make sure that that this is still a 24bit jump */ |
| 233 | if (!is_bl_op(op)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 234 | pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | /* lets find where the pointer goes */ |
| 239 | tramp = find_bl_target(ip, op); |
| 240 | |
| 241 | /* |
| 242 | * On PPC32 the trampoline looks like: |
| 243 | * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha |
| 244 | * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l |
| 245 | * 0x7d, 0x89, 0x03, 0xa6 mtctr r12 |
| 246 | * 0x4e, 0x80, 0x04, 0x20 bctr |
| 247 | */ |
| 248 | |
| 249 | pr_devel("ip:%lx jumps to %lx", ip, tramp); |
| 250 | |
| 251 | /* Find where the trampoline jumps to */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 252 | if (copy_from_kernel_nofault(jmp, (void *)tramp, sizeof(jmp))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 253 | pr_err("Failed to read %lx\n", tramp); |
| 254 | return -EFAULT; |
| 255 | } |
| 256 | |
| 257 | pr_devel(" %08x %08x ", jmp[0], jmp[1]); |
| 258 | |
| 259 | /* verify that this is what we expect it to be */ |
| 260 | if (((jmp[0] & 0xffff0000) != 0x3d800000) || |
| 261 | ((jmp[1] & 0xffff0000) != 0x398c0000) || |
| 262 | (jmp[2] != 0x7d8903a6) || |
| 263 | (jmp[3] != 0x4e800420)) { |
| 264 | pr_err("Not a trampoline\n"); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | tramp = (jmp[1] & 0xffff) | |
| 269 | ((jmp[0] & 0xffff) << 16); |
| 270 | if (tramp & 0x8000) |
| 271 | tramp -= 0x10000; |
| 272 | |
| 273 | pr_devel(" %lx ", tramp); |
| 274 | |
| 275 | if (tramp != addr) { |
| 276 | pr_err("Trampoline location %08lx does not match addr\n", |
| 277 | tramp); |
| 278 | return -EINVAL; |
| 279 | } |
| 280 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 281 | op = ppc_inst(PPC_INST_NOP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 282 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 283 | if (patch_instruction((struct ppc_inst *)ip, op)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 284 | return -EPERM; |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | #endif /* PPC64 */ |
| 289 | #endif /* CONFIG_MODULES */ |
| 290 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 291 | static unsigned long find_ftrace_tramp(unsigned long ip) |
| 292 | { |
| 293 | int i; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 294 | struct ppc_inst instr; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * We have the compiler generated long_branch tramps at the end |
| 298 | * and we prefer those |
| 299 | */ |
| 300 | for (i = NUM_FTRACE_TRAMPS - 1; i >= 0; i--) |
| 301 | if (!ftrace_tramps[i]) |
| 302 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 303 | else if (create_branch(&instr, (void *)ip, |
| 304 | ftrace_tramps[i], 0) == 0) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 305 | return ftrace_tramps[i]; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int add_ftrace_tramp(unsigned long tramp) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for (i = 0; i < NUM_FTRACE_TRAMPS; i++) |
| 315 | if (!ftrace_tramps[i]) { |
| 316 | ftrace_tramps[i] = tramp; |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * If this is a compiler generated long_branch trampoline (essentially, a |
| 325 | * trampoline that has a branch to _mcount()), we re-write the branch to |
| 326 | * instead go to ftrace_[regs_]caller() and note down the location of this |
| 327 | * trampoline. |
| 328 | */ |
| 329 | static int setup_mcount_compiler_tramp(unsigned long tramp) |
| 330 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 331 | int i; |
| 332 | struct ppc_inst op; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 333 | unsigned long ptr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 334 | struct ppc_inst instr; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 335 | static unsigned long ftrace_plt_tramps[NUM_FTRACE_TRAMPS]; |
| 336 | |
| 337 | /* Is this a known long jump tramp? */ |
| 338 | for (i = 0; i < NUM_FTRACE_TRAMPS; i++) |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 339 | if (ftrace_tramps[i] == tramp) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | |
| 342 | /* Is this a known plt tramp? */ |
| 343 | for (i = 0; i < NUM_FTRACE_TRAMPS; i++) |
| 344 | if (!ftrace_plt_tramps[i]) |
| 345 | break; |
| 346 | else if (ftrace_plt_tramps[i] == tramp) |
| 347 | return -1; |
| 348 | |
| 349 | /* New trampoline -- read where this goes */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 350 | if (probe_kernel_read_inst(&op, (void *)tramp)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 351 | pr_debug("Fetching opcode failed.\n"); |
| 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | /* Is this a 24 bit branch? */ |
| 356 | if (!is_b_op(op)) { |
| 357 | pr_debug("Trampoline is not a long branch tramp.\n"); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | /* lets find where the pointer goes */ |
| 362 | ptr = find_bl_target(tramp, op); |
| 363 | |
| 364 | if (ptr != ppc_global_function_entry((void *)_mcount)) { |
| 365 | pr_debug("Trampoline target %p is not _mcount\n", (void *)ptr); |
| 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | /* Let's re-write the tramp to go to ftrace_[regs_]caller */ |
| 370 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 371 | ptr = ppc_global_function_entry((void *)ftrace_regs_caller); |
| 372 | #else |
| 373 | ptr = ppc_global_function_entry((void *)ftrace_caller); |
| 374 | #endif |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 375 | if (create_branch(&instr, (void *)tramp, ptr, 0)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 376 | pr_debug("%ps is not reachable from existing mcount tramp\n", |
| 377 | (void *)ptr); |
| 378 | return -1; |
| 379 | } |
| 380 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 381 | if (patch_branch((struct ppc_inst *)tramp, ptr, 0)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 382 | pr_debug("REL24 out of range!\n"); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | if (add_ftrace_tramp(tramp)) { |
| 387 | pr_debug("No tramp locations left\n"); |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr) |
| 395 | { |
| 396 | unsigned long tramp, ip = rec->ip; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 397 | struct ppc_inst op; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 398 | |
| 399 | /* Read where this goes */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 400 | if (probe_kernel_read_inst(&op, (void *)ip)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 401 | pr_err("Fetching opcode failed.\n"); |
| 402 | return -EFAULT; |
| 403 | } |
| 404 | |
| 405 | /* Make sure that that this is still a 24bit jump */ |
| 406 | if (!is_bl_op(op)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 407 | pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | /* Let's find where the pointer goes */ |
| 412 | tramp = find_bl_target(ip, op); |
| 413 | |
| 414 | pr_devel("ip:%lx jumps to %lx", ip, tramp); |
| 415 | |
| 416 | if (setup_mcount_compiler_tramp(tramp)) { |
| 417 | /* Are other trampolines reachable? */ |
| 418 | if (!find_ftrace_tramp(ip)) { |
| 419 | pr_err("No ftrace trampolines reachable from %ps\n", |
| 420 | (void *)ip); |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | } |
| 424 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 425 | if (patch_instruction((struct ppc_inst *)ip, ppc_inst(PPC_INST_NOP))) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 426 | pr_err("Patching NOP failed.\n"); |
| 427 | return -EPERM; |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 433 | int ftrace_make_nop(struct module *mod, |
| 434 | struct dyn_ftrace *rec, unsigned long addr) |
| 435 | { |
| 436 | unsigned long ip = rec->ip; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 437 | struct ppc_inst old, new; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 438 | |
| 439 | /* |
| 440 | * If the calling address is more that 24 bits away, |
| 441 | * then we had to use a trampoline to make the call. |
| 442 | * Otherwise just update the call site. |
| 443 | */ |
| 444 | if (test_24bit_addr(ip, addr)) { |
| 445 | /* within range */ |
| 446 | old = ftrace_call_replace(ip, addr, 1); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 447 | new = ppc_inst(PPC_INST_NOP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 448 | return ftrace_modify_code(ip, old, new); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 449 | } else if (core_kernel_text(ip)) |
| 450 | return __ftrace_make_nop_kernel(rec, addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 451 | |
| 452 | #ifdef CONFIG_MODULES |
| 453 | /* |
| 454 | * Out of range jumps are called from modules. |
| 455 | * We should either already have a pointer to the module |
| 456 | * or it has been passed in. |
| 457 | */ |
| 458 | if (!rec->arch.mod) { |
| 459 | if (!mod) { |
| 460 | pr_err("No module loaded addr=%lx\n", addr); |
| 461 | return -EFAULT; |
| 462 | } |
| 463 | rec->arch.mod = mod; |
| 464 | } else if (mod) { |
| 465 | if (mod != rec->arch.mod) { |
| 466 | pr_err("Record mod %p not equal to passed in mod %p\n", |
| 467 | rec->arch.mod, mod); |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | /* nothing to do if mod == rec->arch.mod */ |
| 471 | } else |
| 472 | mod = rec->arch.mod; |
| 473 | |
| 474 | return __ftrace_make_nop(mod, rec, addr); |
| 475 | #else |
| 476 | /* We should not get here without modules */ |
| 477 | return -EINVAL; |
| 478 | #endif /* CONFIG_MODULES */ |
| 479 | } |
| 480 | |
| 481 | #ifdef CONFIG_MODULES |
| 482 | #ifdef CONFIG_PPC64 |
| 483 | /* |
| 484 | * Examine the existing instructions for __ftrace_make_call. |
| 485 | * They should effectively be a NOP, and follow formal constraints, |
| 486 | * depending on the ABI. Return false if they don't. |
| 487 | */ |
| 488 | #ifndef CONFIG_MPROFILE_KERNEL |
| 489 | static int |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 490 | expected_nop_sequence(void *ip, struct ppc_inst op0, struct ppc_inst op1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 491 | { |
| 492 | /* |
| 493 | * We expect to see: |
| 494 | * |
| 495 | * b +8 |
| 496 | * ld r2,XX(r1) |
| 497 | * |
| 498 | * The load offset is different depending on the ABI. For simplicity |
| 499 | * just mask it out when doing the compare. |
| 500 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 501 | if (!ppc_inst_equal(op0, ppc_inst(0x48000008)) || |
| 502 | (ppc_inst_val(op1) & 0xffff0000) != 0xe8410000) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 503 | return 0; |
| 504 | return 1; |
| 505 | } |
| 506 | #else |
| 507 | static int |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 508 | expected_nop_sequence(void *ip, struct ppc_inst op0, struct ppc_inst op1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 509 | { |
| 510 | /* look for patched "NOP" on ppc64 with -mprofile-kernel */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 511 | if (!ppc_inst_equal(op0, ppc_inst(PPC_INST_NOP))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | return 1; |
| 514 | } |
| 515 | #endif |
| 516 | |
| 517 | static int |
| 518 | __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 519 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 520 | struct ppc_inst op[2]; |
| 521 | struct ppc_inst instr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 522 | void *ip = (void *)rec->ip; |
| 523 | unsigned long entry, ptr, tramp; |
| 524 | struct module *mod = rec->arch.mod; |
| 525 | |
| 526 | /* read where this goes */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 527 | if (probe_kernel_read_inst(op, ip)) |
| 528 | return -EFAULT; |
| 529 | |
| 530 | if (probe_kernel_read_inst(op + 1, ip + 4)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 531 | return -EFAULT; |
| 532 | |
| 533 | if (!expected_nop_sequence(ip, op[0], op[1])) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 534 | pr_err("Unexpected call sequence at %p: %s %s\n", |
| 535 | ip, ppc_inst_as_str(op[0]), ppc_inst_as_str(op[1])); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | /* If we never set up ftrace trampoline(s), then bail */ |
| 540 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 541 | if (!mod->arch.tramp || !mod->arch.tramp_regs) { |
| 542 | #else |
| 543 | if (!mod->arch.tramp) { |
| 544 | #endif |
| 545 | pr_err("No ftrace trampoline\n"); |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | |
| 549 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 550 | if (rec->flags & FTRACE_FL_REGS) |
| 551 | tramp = mod->arch.tramp_regs; |
| 552 | else |
| 553 | #endif |
| 554 | tramp = mod->arch.tramp; |
| 555 | |
| 556 | if (module_trampoline_target(mod, tramp, &ptr)) { |
| 557 | pr_err("Failed to get trampoline target\n"); |
| 558 | return -EFAULT; |
| 559 | } |
| 560 | |
| 561 | pr_devel("trampoline target %lx", ptr); |
| 562 | |
| 563 | entry = ppc_global_function_entry((void *)addr); |
| 564 | /* This should match what was called */ |
| 565 | if (ptr != entry) { |
| 566 | pr_err("addr %lx does not match expected %lx\n", ptr, entry); |
| 567 | return -EINVAL; |
| 568 | } |
| 569 | |
| 570 | /* Ensure branch is within 24 bits */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 571 | if (create_branch(&instr, ip, tramp, BRANCH_SET_LINK)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 572 | pr_err("Branch out of range\n"); |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | |
| 576 | if (patch_branch(ip, tramp, BRANCH_SET_LINK)) { |
| 577 | pr_err("REL24 out of range!\n"); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | #else /* !CONFIG_PPC64: */ |
| 585 | static int |
| 586 | __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 587 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 588 | int err; |
| 589 | struct ppc_inst op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 590 | unsigned long ip = rec->ip; |
| 591 | |
| 592 | /* read where this goes */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 593 | if (probe_kernel_read_inst(&op, (void *)ip)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 594 | return -EFAULT; |
| 595 | |
| 596 | /* It should be pointing to a nop */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 597 | if (!ppc_inst_equal(op, ppc_inst(PPC_INST_NOP))) { |
| 598 | pr_err("Expected NOP but have %s\n", ppc_inst_as_str(op)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 599 | return -EINVAL; |
| 600 | } |
| 601 | |
| 602 | /* If we never set up a trampoline to ftrace_caller, then bail */ |
| 603 | if (!rec->arch.mod->arch.tramp) { |
| 604 | pr_err("No ftrace trampoline\n"); |
| 605 | return -EINVAL; |
| 606 | } |
| 607 | |
| 608 | /* create the branch to the trampoline */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 609 | err = create_branch(&op, (struct ppc_inst *)ip, |
| 610 | rec->arch.mod->arch.tramp, BRANCH_SET_LINK); |
| 611 | if (err) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 612 | pr_err("REL24 out of range!\n"); |
| 613 | return -EINVAL; |
| 614 | } |
| 615 | |
| 616 | pr_devel("write to %lx\n", rec->ip); |
| 617 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 618 | if (patch_instruction((struct ppc_inst *)ip, op)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 619 | return -EPERM; |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | #endif /* CONFIG_PPC64 */ |
| 624 | #endif /* CONFIG_MODULES */ |
| 625 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 626 | static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr) |
| 627 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 628 | struct ppc_inst op; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 629 | void *ip = (void *)rec->ip; |
| 630 | unsigned long tramp, entry, ptr; |
| 631 | |
| 632 | /* Make sure we're being asked to patch branch to a known ftrace addr */ |
| 633 | entry = ppc_global_function_entry((void *)ftrace_caller); |
| 634 | ptr = ppc_global_function_entry((void *)addr); |
| 635 | |
| 636 | if (ptr != entry) { |
| 637 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 638 | entry = ppc_global_function_entry((void *)ftrace_regs_caller); |
| 639 | if (ptr != entry) { |
| 640 | #endif |
| 641 | pr_err("Unknown ftrace addr to patch: %ps\n", (void *)ptr); |
| 642 | return -EINVAL; |
| 643 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 644 | } |
| 645 | #endif |
| 646 | } |
| 647 | |
| 648 | /* Make sure we have a nop */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 649 | if (probe_kernel_read_inst(&op, ip)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 650 | pr_err("Unable to read ftrace location %p\n", ip); |
| 651 | return -EFAULT; |
| 652 | } |
| 653 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 654 | if (!ppc_inst_equal(op, ppc_inst(PPC_INST_NOP))) { |
| 655 | pr_err("Unexpected call sequence at %p: %s\n", ip, ppc_inst_as_str(op)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 656 | return -EINVAL; |
| 657 | } |
| 658 | |
| 659 | tramp = find_ftrace_tramp((unsigned long)ip); |
| 660 | if (!tramp) { |
| 661 | pr_err("No ftrace trampolines reachable from %ps\n", ip); |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | |
| 665 | if (patch_branch(ip, tramp, BRANCH_SET_LINK)) { |
| 666 | pr_err("Error patching branch to ftrace tramp!\n"); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 673 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 674 | { |
| 675 | unsigned long ip = rec->ip; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 676 | struct ppc_inst old, new; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | * If the calling address is more that 24 bits away, |
| 680 | * then we had to use a trampoline to make the call. |
| 681 | * Otherwise just update the call site. |
| 682 | */ |
| 683 | if (test_24bit_addr(ip, addr)) { |
| 684 | /* within range */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 685 | old = ppc_inst(PPC_INST_NOP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 686 | new = ftrace_call_replace(ip, addr, 1); |
| 687 | return ftrace_modify_code(ip, old, new); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 688 | } else if (core_kernel_text(ip)) |
| 689 | return __ftrace_make_call_kernel(rec, addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 690 | |
| 691 | #ifdef CONFIG_MODULES |
| 692 | /* |
| 693 | * Out of range jumps are called from modules. |
| 694 | * Being that we are converting from nop, it had better |
| 695 | * already have a module defined. |
| 696 | */ |
| 697 | if (!rec->arch.mod) { |
| 698 | pr_err("No module loaded\n"); |
| 699 | return -EINVAL; |
| 700 | } |
| 701 | |
| 702 | return __ftrace_make_call(rec, addr); |
| 703 | #else |
| 704 | /* We should not get here without modules */ |
| 705 | return -EINVAL; |
| 706 | #endif /* CONFIG_MODULES */ |
| 707 | } |
| 708 | |
| 709 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 710 | #ifdef CONFIG_MODULES |
| 711 | static int |
| 712 | __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, |
| 713 | unsigned long addr) |
| 714 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 715 | struct ppc_inst op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 716 | unsigned long ip = rec->ip; |
| 717 | unsigned long entry, ptr, tramp; |
| 718 | struct module *mod = rec->arch.mod; |
| 719 | |
| 720 | /* If we never set up ftrace trampolines, then bail */ |
| 721 | if (!mod->arch.tramp || !mod->arch.tramp_regs) { |
| 722 | pr_err("No ftrace trampoline\n"); |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | |
| 726 | /* read where this goes */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 727 | if (probe_kernel_read_inst(&op, (void *)ip)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 728 | pr_err("Fetching opcode failed.\n"); |
| 729 | return -EFAULT; |
| 730 | } |
| 731 | |
| 732 | /* Make sure that that this is still a 24bit jump */ |
| 733 | if (!is_bl_op(op)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 734 | pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | } |
| 737 | |
| 738 | /* lets find where the pointer goes */ |
| 739 | tramp = find_bl_target(ip, op); |
| 740 | entry = ppc_global_function_entry((void *)old_addr); |
| 741 | |
| 742 | pr_devel("ip:%lx jumps to %lx", ip, tramp); |
| 743 | |
| 744 | if (tramp != entry) { |
| 745 | /* old_addr is not within range, so we must have used a trampoline */ |
| 746 | if (module_trampoline_target(mod, tramp, &ptr)) { |
| 747 | pr_err("Failed to get trampoline target\n"); |
| 748 | return -EFAULT; |
| 749 | } |
| 750 | |
| 751 | pr_devel("trampoline target %lx", ptr); |
| 752 | |
| 753 | /* This should match what was called */ |
| 754 | if (ptr != entry) { |
| 755 | pr_err("addr %lx does not match expected %lx\n", ptr, entry); |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* The new target may be within range */ |
| 761 | if (test_24bit_addr(ip, addr)) { |
| 762 | /* within range */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 763 | if (patch_branch((struct ppc_inst *)ip, addr, BRANCH_SET_LINK)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 764 | pr_err("REL24 out of range!\n"); |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | if (rec->flags & FTRACE_FL_REGS) |
| 772 | tramp = mod->arch.tramp_regs; |
| 773 | else |
| 774 | tramp = mod->arch.tramp; |
| 775 | |
| 776 | if (module_trampoline_target(mod, tramp, &ptr)) { |
| 777 | pr_err("Failed to get trampoline target\n"); |
| 778 | return -EFAULT; |
| 779 | } |
| 780 | |
| 781 | pr_devel("trampoline target %lx", ptr); |
| 782 | |
| 783 | entry = ppc_global_function_entry((void *)addr); |
| 784 | /* This should match what was called */ |
| 785 | if (ptr != entry) { |
| 786 | pr_err("addr %lx does not match expected %lx\n", ptr, entry); |
| 787 | return -EINVAL; |
| 788 | } |
| 789 | |
| 790 | /* Ensure branch is within 24 bits */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 791 | if (create_branch(&op, (struct ppc_inst *)ip, tramp, BRANCH_SET_LINK)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 792 | pr_err("Branch out of range\n"); |
| 793 | return -EINVAL; |
| 794 | } |
| 795 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 796 | if (patch_branch((struct ppc_inst *)ip, tramp, BRANCH_SET_LINK)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 797 | pr_err("REL24 out of range!\n"); |
| 798 | return -EINVAL; |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | #endif |
| 804 | |
| 805 | int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, |
| 806 | unsigned long addr) |
| 807 | { |
| 808 | unsigned long ip = rec->ip; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 809 | struct ppc_inst old, new; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 810 | |
| 811 | /* |
| 812 | * If the calling address is more that 24 bits away, |
| 813 | * then we had to use a trampoline to make the call. |
| 814 | * Otherwise just update the call site. |
| 815 | */ |
| 816 | if (test_24bit_addr(ip, addr) && test_24bit_addr(ip, old_addr)) { |
| 817 | /* within range */ |
| 818 | old = ftrace_call_replace(ip, old_addr, 1); |
| 819 | new = ftrace_call_replace(ip, addr, 1); |
| 820 | return ftrace_modify_code(ip, old, new); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 821 | } else if (core_kernel_text(ip)) { |
| 822 | /* |
| 823 | * We always patch out of range locations to go to the regs |
| 824 | * variant, so there is nothing to do here |
| 825 | */ |
| 826 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | #ifdef CONFIG_MODULES |
| 830 | /* |
| 831 | * Out of range jumps are called from modules. |
| 832 | */ |
| 833 | if (!rec->arch.mod) { |
| 834 | pr_err("No module loaded\n"); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | |
| 838 | return __ftrace_modify_call(rec, old_addr, addr); |
| 839 | #else |
| 840 | /* We should not get here without modules */ |
| 841 | return -EINVAL; |
| 842 | #endif /* CONFIG_MODULES */ |
| 843 | } |
| 844 | #endif |
| 845 | |
| 846 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 847 | { |
| 848 | unsigned long ip = (unsigned long)(&ftrace_call); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 849 | struct ppc_inst old, new; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 850 | int ret; |
| 851 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 852 | old = ppc_inst_read((struct ppc_inst *)&ftrace_call); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 853 | new = ftrace_call_replace(ip, (unsigned long)func, 1); |
| 854 | ret = ftrace_modify_code(ip, old, new); |
| 855 | |
| 856 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 857 | /* Also update the regs callback function */ |
| 858 | if (!ret) { |
| 859 | ip = (unsigned long)(&ftrace_regs_call); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 860 | old = ppc_inst_read((struct ppc_inst *)&ftrace_regs_call); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 861 | new = ftrace_call_replace(ip, (unsigned long)func, 1); |
| 862 | ret = ftrace_modify_code(ip, old, new); |
| 863 | } |
| 864 | #endif |
| 865 | |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Use the default ftrace_modify_all_code, but without |
| 871 | * stop_machine(). |
| 872 | */ |
| 873 | void arch_ftrace_update_code(int command) |
| 874 | { |
| 875 | ftrace_modify_all_code(command); |
| 876 | } |
| 877 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 878 | #ifdef CONFIG_PPC64 |
| 879 | #define PACATOC offsetof(struct paca_struct, kernel_toc) |
| 880 | |
| 881 | extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[]; |
| 882 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 883 | void ftrace_free_init_tramp(void) |
| 884 | { |
| 885 | int i; |
| 886 | |
| 887 | for (i = 0; i < NUM_FTRACE_TRAMPS && ftrace_tramps[i]; i++) |
| 888 | if (ftrace_tramps[i] == (unsigned long)ftrace_tramp_init) { |
| 889 | ftrace_tramps[i] = 0; |
| 890 | return; |
| 891 | } |
| 892 | } |
| 893 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 894 | int __init ftrace_dyn_arch_init(void) |
| 895 | { |
| 896 | int i; |
| 897 | unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init }; |
| 898 | u32 stub_insns[] = { |
| 899 | 0xe98d0000 | PACATOC, /* ld r12,PACATOC(r13) */ |
| 900 | 0x3d8c0000, /* addis r12,r12,<high> */ |
| 901 | 0x398c0000, /* addi r12,r12,<low> */ |
| 902 | 0x7d8903a6, /* mtctr r12 */ |
| 903 | 0x4e800420, /* bctr */ |
| 904 | }; |
| 905 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 906 | unsigned long addr = ppc_global_function_entry((void *)ftrace_regs_caller); |
| 907 | #else |
| 908 | unsigned long addr = ppc_global_function_entry((void *)ftrace_caller); |
| 909 | #endif |
| 910 | long reladdr = addr - kernel_toc_addr(); |
| 911 | |
| 912 | if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { |
| 913 | pr_err("Address of %ps out of range of kernel_toc.\n", |
| 914 | (void *)addr); |
| 915 | return -1; |
| 916 | } |
| 917 | |
| 918 | for (i = 0; i < 2; i++) { |
| 919 | memcpy(tramp[i], stub_insns, sizeof(stub_insns)); |
| 920 | tramp[i][1] |= PPC_HA(reladdr); |
| 921 | tramp[i][2] |= PPC_LO(reladdr); |
| 922 | add_ftrace_tramp((unsigned long)tramp[i]); |
| 923 | } |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | #else |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 928 | int __init ftrace_dyn_arch_init(void) |
| 929 | { |
| 930 | return 0; |
| 931 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 932 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 933 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 934 | |
| 935 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 936 | |
| 937 | extern void ftrace_graph_call(void); |
| 938 | extern void ftrace_graph_stub(void); |
| 939 | |
| 940 | int ftrace_enable_ftrace_graph_caller(void) |
| 941 | { |
| 942 | unsigned long ip = (unsigned long)(&ftrace_graph_call); |
| 943 | unsigned long addr = (unsigned long)(&ftrace_graph_caller); |
| 944 | unsigned long stub = (unsigned long)(&ftrace_graph_stub); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 945 | struct ppc_inst old, new; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 946 | |
| 947 | old = ftrace_call_replace(ip, stub, 0); |
| 948 | new = ftrace_call_replace(ip, addr, 0); |
| 949 | |
| 950 | return ftrace_modify_code(ip, old, new); |
| 951 | } |
| 952 | |
| 953 | int ftrace_disable_ftrace_graph_caller(void) |
| 954 | { |
| 955 | unsigned long ip = (unsigned long)(&ftrace_graph_call); |
| 956 | unsigned long addr = (unsigned long)(&ftrace_graph_caller); |
| 957 | unsigned long stub = (unsigned long)(&ftrace_graph_stub); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 958 | struct ppc_inst old, new; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 959 | |
| 960 | old = ftrace_call_replace(ip, addr, 0); |
| 961 | new = ftrace_call_replace(ip, stub, 0); |
| 962 | |
| 963 | return ftrace_modify_code(ip, old, new); |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Hook the return address and push it in the stack of return addrs |
| 968 | * in current thread info. Return the address we want to divert to. |
| 969 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 970 | unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip, |
| 971 | unsigned long sp) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 972 | { |
| 973 | unsigned long return_hooker; |
| 974 | |
| 975 | if (unlikely(ftrace_graph_is_dead())) |
| 976 | goto out; |
| 977 | |
| 978 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 979 | goto out; |
| 980 | |
| 981 | return_hooker = ppc_function_entry(return_to_handler); |
| 982 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 983 | if (!function_graph_enter(parent, ip, 0, (unsigned long *)sp)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 984 | parent = return_hooker; |
| 985 | out: |
| 986 | return parent; |
| 987 | } |
| 988 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 989 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 990 | #ifdef PPC64_ELF_ABI_v1 |
| 991 | char *arch_ftrace_match_adjust(char *str, const char *search) |
| 992 | { |
| 993 | if (str[0] == '.' && search[0] != '.') |
| 994 | return str + 1; |
| 995 | else |
| 996 | return str; |
| 997 | } |
| 998 | #endif /* PPC64_ELF_ABI_v1 */ |