| .. | .. |
|---|
| 27 | 27 | #include <asm/code-patching.h> |
|---|
| 28 | 28 | #include <asm/ftrace.h> |
|---|
| 29 | 29 | #include <asm/syscall.h> |
|---|
| 30 | +#include <asm/inst.h> |
|---|
| 30 | 31 | |
|---|
| 31 | 32 | |
|---|
| 32 | 33 | #ifdef CONFIG_DYNAMIC_FTRACE |
|---|
| 33 | | -static unsigned int |
|---|
| 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 | + |
|---|
| 44 | +static struct ppc_inst |
|---|
| 34 | 45 | ftrace_call_replace(unsigned long ip, unsigned long addr, int link) |
|---|
| 35 | 46 | { |
|---|
| 36 | | - unsigned int op; |
|---|
| 47 | + struct ppc_inst op; |
|---|
| 37 | 48 | |
|---|
| 38 | 49 | addr = ppc_function_entry((void *)addr); |
|---|
| 39 | 50 | |
|---|
| 40 | 51 | /* if (link) set op to 'bl' else 'b' */ |
|---|
| 41 | | - op = create_branch((unsigned int *)ip, addr, link ? 1 : 0); |
|---|
| 52 | + create_branch(&op, (struct ppc_inst *)ip, addr, link ? 1 : 0); |
|---|
| 42 | 53 | |
|---|
| 43 | 54 | return op; |
|---|
| 44 | 55 | } |
|---|
| 45 | 56 | |
|---|
| 46 | 57 | static int |
|---|
| 47 | | -ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new) |
|---|
| 58 | +ftrace_modify_code(unsigned long ip, struct ppc_inst old, struct ppc_inst new) |
|---|
| 48 | 59 | { |
|---|
| 49 | | - unsigned int replaced; |
|---|
| 60 | + struct ppc_inst replaced; |
|---|
| 50 | 61 | |
|---|
| 51 | 62 | /* |
|---|
| 52 | 63 | * Note: |
|---|
| .. | .. |
|---|
| 57 | 68 | */ |
|---|
| 58 | 69 | |
|---|
| 59 | 70 | /* read the text we want to modify */ |
|---|
| 60 | | - if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE)) |
|---|
| 71 | + if (probe_kernel_read_inst(&replaced, (void *)ip)) |
|---|
| 61 | 72 | return -EFAULT; |
|---|
| 62 | 73 | |
|---|
| 63 | 74 | /* Make sure it is what we expect it to be */ |
|---|
| 64 | | - if (replaced != old) { |
|---|
| 65 | | - pr_err("%p: replaced (%#x) != old (%#x)", |
|---|
| 66 | | - (void *)ip, replaced, old); |
|---|
| 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)); |
|---|
| 67 | 78 | return -EINVAL; |
|---|
| 68 | 79 | } |
|---|
| 69 | 80 | |
|---|
| 70 | 81 | /* replace the text with the new text */ |
|---|
| 71 | | - if (patch_instruction((unsigned int *)ip, new)) |
|---|
| 82 | + if (patch_instruction((struct ppc_inst *)ip, new)) |
|---|
| 72 | 83 | return -EPERM; |
|---|
| 73 | 84 | |
|---|
| 74 | 85 | return 0; |
|---|
| .. | .. |
|---|
| 79 | 90 | */ |
|---|
| 80 | 91 | static int test_24bit_addr(unsigned long ip, unsigned long addr) |
|---|
| 81 | 92 | { |
|---|
| 93 | + struct ppc_inst op; |
|---|
| 82 | 94 | addr = ppc_function_entry((void *)addr); |
|---|
| 83 | 95 | |
|---|
| 84 | 96 | /* use the create_branch to verify that this offset can be branched */ |
|---|
| 85 | | - return create_branch((unsigned int *)ip, addr, 0); |
|---|
| 97 | + return create_branch(&op, (struct ppc_inst *)ip, addr, 0) == 0; |
|---|
| 86 | 98 | } |
|---|
| 87 | 99 | |
|---|
| 88 | | -#ifdef CONFIG_MODULES |
|---|
| 89 | | - |
|---|
| 90 | | -static int is_bl_op(unsigned int op) |
|---|
| 100 | +static int is_bl_op(struct ppc_inst op) |
|---|
| 91 | 101 | { |
|---|
| 92 | | - return (op & 0xfc000003) == 0x48000001; |
|---|
| 102 | + return (ppc_inst_val(op) & 0xfc000003) == 0x48000001; |
|---|
| 93 | 103 | } |
|---|
| 94 | 104 | |
|---|
| 95 | | -static unsigned long find_bl_target(unsigned long ip, unsigned int op) |
|---|
| 105 | +static int is_b_op(struct ppc_inst op) |
|---|
| 96 | 106 | { |
|---|
| 97 | | - static int offset; |
|---|
| 107 | + return (ppc_inst_val(op) & 0xfc000003) == 0x48000000; |
|---|
| 108 | +} |
|---|
| 98 | 109 | |
|---|
| 99 | | - offset = (op & 0x03fffffc); |
|---|
| 110 | +static unsigned long find_bl_target(unsigned long ip, struct ppc_inst op) |
|---|
| 111 | +{ |
|---|
| 112 | + int offset; |
|---|
| 113 | + |
|---|
| 114 | + offset = (ppc_inst_val(op) & 0x03fffffc); |
|---|
| 100 | 115 | /* make it signed */ |
|---|
| 101 | 116 | if (offset & 0x02000000) |
|---|
| 102 | 117 | offset |= 0xfe000000; |
|---|
| .. | .. |
|---|
| 104 | 119 | return ip + (long)offset; |
|---|
| 105 | 120 | } |
|---|
| 106 | 121 | |
|---|
| 122 | +#ifdef CONFIG_MODULES |
|---|
| 107 | 123 | #ifdef CONFIG_PPC64 |
|---|
| 108 | 124 | static int |
|---|
| 109 | 125 | __ftrace_make_nop(struct module *mod, |
|---|
| .. | .. |
|---|
| 111 | 127 | { |
|---|
| 112 | 128 | unsigned long entry, ptr, tramp; |
|---|
| 113 | 129 | unsigned long ip = rec->ip; |
|---|
| 114 | | - unsigned int op, pop; |
|---|
| 130 | + struct ppc_inst op, pop; |
|---|
| 115 | 131 | |
|---|
| 116 | 132 | /* read where this goes */ |
|---|
| 117 | | - if (probe_kernel_read(&op, (void *)ip, sizeof(int))) { |
|---|
| 133 | + if (probe_kernel_read_inst(&op, (void *)ip)) { |
|---|
| 118 | 134 | pr_err("Fetching opcode failed.\n"); |
|---|
| 119 | 135 | return -EFAULT; |
|---|
| 120 | 136 | } |
|---|
| 121 | 137 | |
|---|
| 122 | 138 | /* Make sure that that this is still a 24bit jump */ |
|---|
| 123 | 139 | if (!is_bl_op(op)) { |
|---|
| 124 | | - pr_err("Not expected bl: opcode is %x\n", op); |
|---|
| 140 | + pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
|---|
| 125 | 141 | return -EINVAL; |
|---|
| 126 | 142 | } |
|---|
| 127 | 143 | |
|---|
| .. | .. |
|---|
| 146 | 162 | |
|---|
| 147 | 163 | #ifdef CONFIG_MPROFILE_KERNEL |
|---|
| 148 | 164 | /* When using -mkernel_profile there is no load to jump over */ |
|---|
| 149 | | - pop = PPC_INST_NOP; |
|---|
| 165 | + pop = ppc_inst(PPC_INST_NOP); |
|---|
| 150 | 166 | |
|---|
| 151 | | - if (probe_kernel_read(&op, (void *)(ip - 4), 4)) { |
|---|
| 167 | + if (probe_kernel_read_inst(&op, (void *)(ip - 4))) { |
|---|
| 152 | 168 | pr_err("Fetching instruction at %lx failed.\n", ip - 4); |
|---|
| 153 | 169 | return -EFAULT; |
|---|
| 154 | 170 | } |
|---|
| 155 | 171 | |
|---|
| 156 | 172 | /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */ |
|---|
| 157 | | - if (op != PPC_INST_MFLR && op != PPC_INST_STD_LR) { |
|---|
| 158 | | - pr_err("Unexpected instruction %08x around bl _mcount\n", op); |
|---|
| 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)); |
|---|
| 159 | 177 | return -EINVAL; |
|---|
| 160 | 178 | } |
|---|
| 161 | 179 | #else |
|---|
| .. | .. |
|---|
| 173 | 191 | * Use a b +8 to jump over the load. |
|---|
| 174 | 192 | */ |
|---|
| 175 | 193 | |
|---|
| 176 | | - pop = PPC_INST_BRANCH | 8; /* b +8 */ |
|---|
| 194 | + pop = ppc_inst(PPC_INST_BRANCH | 8); /* b +8 */ |
|---|
| 177 | 195 | |
|---|
| 178 | 196 | /* |
|---|
| 179 | 197 | * Check what is in the next instruction. We can see ld r2,40(r1), but |
|---|
| 180 | 198 | * on first pass after boot we will see mflr r0. |
|---|
| 181 | 199 | */ |
|---|
| 182 | | - if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE)) { |
|---|
| 200 | + if (probe_kernel_read_inst(&op, (void *)(ip + 4))) { |
|---|
| 183 | 201 | pr_err("Fetching op failed.\n"); |
|---|
| 184 | 202 | return -EFAULT; |
|---|
| 185 | 203 | } |
|---|
| 186 | 204 | |
|---|
| 187 | | - if (op != PPC_INST_LD_TOC) { |
|---|
| 188 | | - pr_err("Expected %08x found %08x\n", PPC_INST_LD_TOC, op); |
|---|
| 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)); |
|---|
| 189 | 207 | return -EINVAL; |
|---|
| 190 | 208 | } |
|---|
| 191 | 209 | #endif /* CONFIG_MPROFILE_KERNEL */ |
|---|
| 192 | 210 | |
|---|
| 193 | | - if (patch_instruction((unsigned int *)ip, pop)) { |
|---|
| 211 | + if (patch_instruction((struct ppc_inst *)ip, pop)) { |
|---|
| 194 | 212 | pr_err("Patching NOP failed.\n"); |
|---|
| 195 | 213 | return -EPERM; |
|---|
| 196 | 214 | } |
|---|
| .. | .. |
|---|
| 203 | 221 | __ftrace_make_nop(struct module *mod, |
|---|
| 204 | 222 | struct dyn_ftrace *rec, unsigned long addr) |
|---|
| 205 | 223 | { |
|---|
| 206 | | - unsigned int op; |
|---|
| 224 | + struct ppc_inst op; |
|---|
| 207 | 225 | unsigned int jmp[4]; |
|---|
| 208 | 226 | unsigned long ip = rec->ip; |
|---|
| 209 | 227 | unsigned long tramp; |
|---|
| 210 | 228 | |
|---|
| 211 | | - if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE)) |
|---|
| 229 | + if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE)) |
|---|
| 212 | 230 | return -EFAULT; |
|---|
| 213 | 231 | |
|---|
| 214 | 232 | /* Make sure that that this is still a 24bit jump */ |
|---|
| 215 | 233 | if (!is_bl_op(op)) { |
|---|
| 216 | | - pr_err("Not expected bl: opcode is %x\n", op); |
|---|
| 234 | + pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
|---|
| 217 | 235 | return -EINVAL; |
|---|
| 218 | 236 | } |
|---|
| 219 | 237 | |
|---|
| .. | .. |
|---|
| 231 | 249 | pr_devel("ip:%lx jumps to %lx", ip, tramp); |
|---|
| 232 | 250 | |
|---|
| 233 | 251 | /* Find where the trampoline jumps to */ |
|---|
| 234 | | - if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) { |
|---|
| 252 | + if (copy_from_kernel_nofault(jmp, (void *)tramp, sizeof(jmp))) { |
|---|
| 235 | 253 | pr_err("Failed to read %lx\n", tramp); |
|---|
| 236 | 254 | return -EFAULT; |
|---|
| 237 | 255 | } |
|---|
| .. | .. |
|---|
| 260 | 278 | return -EINVAL; |
|---|
| 261 | 279 | } |
|---|
| 262 | 280 | |
|---|
| 263 | | - op = PPC_INST_NOP; |
|---|
| 281 | + op = ppc_inst(PPC_INST_NOP); |
|---|
| 264 | 282 | |
|---|
| 265 | | - if (patch_instruction((unsigned int *)ip, op)) |
|---|
| 283 | + if (patch_instruction((struct ppc_inst *)ip, op)) |
|---|
| 266 | 284 | return -EPERM; |
|---|
| 267 | 285 | |
|---|
| 268 | 286 | return 0; |
|---|
| .. | .. |
|---|
| 270 | 288 | #endif /* PPC64 */ |
|---|
| 271 | 289 | #endif /* CONFIG_MODULES */ |
|---|
| 272 | 290 | |
|---|
| 291 | +static unsigned long find_ftrace_tramp(unsigned long ip) |
|---|
| 292 | +{ |
|---|
| 293 | + int i; |
|---|
| 294 | + struct ppc_inst instr; |
|---|
| 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; |
|---|
| 303 | + else if (create_branch(&instr, (void *)ip, |
|---|
| 304 | + ftrace_tramps[i], 0) == 0) |
|---|
| 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 | +{ |
|---|
| 331 | + int i; |
|---|
| 332 | + struct ppc_inst op; |
|---|
| 333 | + unsigned long ptr; |
|---|
| 334 | + struct ppc_inst instr; |
|---|
| 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++) |
|---|
| 339 | + if (ftrace_tramps[i] == tramp) |
|---|
| 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 */ |
|---|
| 350 | + if (probe_kernel_read_inst(&op, (void *)tramp)) { |
|---|
| 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 |
|---|
| 375 | + if (create_branch(&instr, (void *)tramp, ptr, 0)) { |
|---|
| 376 | + pr_debug("%ps is not reachable from existing mcount tramp\n", |
|---|
| 377 | + (void *)ptr); |
|---|
| 378 | + return -1; |
|---|
| 379 | + } |
|---|
| 380 | + |
|---|
| 381 | + if (patch_branch((struct ppc_inst *)tramp, ptr, 0)) { |
|---|
| 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; |
|---|
| 397 | + struct ppc_inst op; |
|---|
| 398 | + |
|---|
| 399 | + /* Read where this goes */ |
|---|
| 400 | + if (probe_kernel_read_inst(&op, (void *)ip)) { |
|---|
| 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)) { |
|---|
| 407 | + pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
|---|
| 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 | + |
|---|
| 425 | + if (patch_instruction((struct ppc_inst *)ip, ppc_inst(PPC_INST_NOP))) { |
|---|
| 426 | + pr_err("Patching NOP failed.\n"); |
|---|
| 427 | + return -EPERM; |
|---|
| 428 | + } |
|---|
| 429 | + |
|---|
| 430 | + return 0; |
|---|
| 431 | +} |
|---|
| 432 | + |
|---|
| 273 | 433 | int ftrace_make_nop(struct module *mod, |
|---|
| 274 | 434 | struct dyn_ftrace *rec, unsigned long addr) |
|---|
| 275 | 435 | { |
|---|
| 276 | 436 | unsigned long ip = rec->ip; |
|---|
| 277 | | - unsigned int old, new; |
|---|
| 437 | + struct ppc_inst old, new; |
|---|
| 278 | 438 | |
|---|
| 279 | 439 | /* |
|---|
| 280 | 440 | * If the calling address is more that 24 bits away, |
|---|
| .. | .. |
|---|
| 284 | 444 | if (test_24bit_addr(ip, addr)) { |
|---|
| 285 | 445 | /* within range */ |
|---|
| 286 | 446 | old = ftrace_call_replace(ip, addr, 1); |
|---|
| 287 | | - new = PPC_INST_NOP; |
|---|
| 447 | + new = ppc_inst(PPC_INST_NOP); |
|---|
| 288 | 448 | return ftrace_modify_code(ip, old, new); |
|---|
| 289 | | - } |
|---|
| 449 | + } else if (core_kernel_text(ip)) |
|---|
| 450 | + return __ftrace_make_nop_kernel(rec, addr); |
|---|
| 290 | 451 | |
|---|
| 291 | 452 | #ifdef CONFIG_MODULES |
|---|
| 292 | 453 | /* |
|---|
| .. | .. |
|---|
| 326 | 487 | */ |
|---|
| 327 | 488 | #ifndef CONFIG_MPROFILE_KERNEL |
|---|
| 328 | 489 | static int |
|---|
| 329 | | -expected_nop_sequence(void *ip, unsigned int op0, unsigned int op1) |
|---|
| 490 | +expected_nop_sequence(void *ip, struct ppc_inst op0, struct ppc_inst op1) |
|---|
| 330 | 491 | { |
|---|
| 331 | 492 | /* |
|---|
| 332 | 493 | * We expect to see: |
|---|
| .. | .. |
|---|
| 337 | 498 | * The load offset is different depending on the ABI. For simplicity |
|---|
| 338 | 499 | * just mask it out when doing the compare. |
|---|
| 339 | 500 | */ |
|---|
| 340 | | - if ((op0 != 0x48000008) || ((op1 & 0xffff0000) != 0xe8410000)) |
|---|
| 501 | + if (!ppc_inst_equal(op0, ppc_inst(0x48000008)) || |
|---|
| 502 | + (ppc_inst_val(op1) & 0xffff0000) != 0xe8410000) |
|---|
| 341 | 503 | return 0; |
|---|
| 342 | 504 | return 1; |
|---|
| 343 | 505 | } |
|---|
| 344 | 506 | #else |
|---|
| 345 | 507 | static int |
|---|
| 346 | | -expected_nop_sequence(void *ip, unsigned int op0, unsigned int op1) |
|---|
| 508 | +expected_nop_sequence(void *ip, struct ppc_inst op0, struct ppc_inst op1) |
|---|
| 347 | 509 | { |
|---|
| 348 | 510 | /* look for patched "NOP" on ppc64 with -mprofile-kernel */ |
|---|
| 349 | | - if (op0 != PPC_INST_NOP) |
|---|
| 511 | + if (!ppc_inst_equal(op0, ppc_inst(PPC_INST_NOP))) |
|---|
| 350 | 512 | return 0; |
|---|
| 351 | 513 | return 1; |
|---|
| 352 | 514 | } |
|---|
| .. | .. |
|---|
| 355 | 517 | static int |
|---|
| 356 | 518 | __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
|---|
| 357 | 519 | { |
|---|
| 358 | | - unsigned int op[2]; |
|---|
| 520 | + struct ppc_inst op[2]; |
|---|
| 521 | + struct ppc_inst instr; |
|---|
| 359 | 522 | void *ip = (void *)rec->ip; |
|---|
| 360 | 523 | unsigned long entry, ptr, tramp; |
|---|
| 361 | 524 | struct module *mod = rec->arch.mod; |
|---|
| 362 | 525 | |
|---|
| 363 | 526 | /* read where this goes */ |
|---|
| 364 | | - if (probe_kernel_read(op, ip, sizeof(op))) |
|---|
| 527 | + if (probe_kernel_read_inst(op, ip)) |
|---|
| 528 | + return -EFAULT; |
|---|
| 529 | + |
|---|
| 530 | + if (probe_kernel_read_inst(op + 1, ip + 4)) |
|---|
| 365 | 531 | return -EFAULT; |
|---|
| 366 | 532 | |
|---|
| 367 | 533 | if (!expected_nop_sequence(ip, op[0], op[1])) { |
|---|
| 368 | | - pr_err("Unexpected call sequence at %p: %x %x\n", |
|---|
| 369 | | - ip, op[0], op[1]); |
|---|
| 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])); |
|---|
| 370 | 536 | return -EINVAL; |
|---|
| 371 | 537 | } |
|---|
| 372 | 538 | |
|---|
| .. | .. |
|---|
| 402 | 568 | } |
|---|
| 403 | 569 | |
|---|
| 404 | 570 | /* Ensure branch is within 24 bits */ |
|---|
| 405 | | - if (!create_branch(ip, tramp, BRANCH_SET_LINK)) { |
|---|
| 571 | + if (create_branch(&instr, ip, tramp, BRANCH_SET_LINK)) { |
|---|
| 406 | 572 | pr_err("Branch out of range\n"); |
|---|
| 407 | 573 | return -EINVAL; |
|---|
| 408 | 574 | } |
|---|
| .. | .. |
|---|
| 419 | 585 | static int |
|---|
| 420 | 586 | __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
|---|
| 421 | 587 | { |
|---|
| 422 | | - unsigned int op; |
|---|
| 588 | + int err; |
|---|
| 589 | + struct ppc_inst op; |
|---|
| 423 | 590 | unsigned long ip = rec->ip; |
|---|
| 424 | 591 | |
|---|
| 425 | 592 | /* read where this goes */ |
|---|
| 426 | | - if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE)) |
|---|
| 593 | + if (probe_kernel_read_inst(&op, (void *)ip)) |
|---|
| 427 | 594 | return -EFAULT; |
|---|
| 428 | 595 | |
|---|
| 429 | 596 | /* It should be pointing to a nop */ |
|---|
| 430 | | - if (op != PPC_INST_NOP) { |
|---|
| 431 | | - pr_err("Expected NOP but have %x\n", op); |
|---|
| 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)); |
|---|
| 432 | 599 | return -EINVAL; |
|---|
| 433 | 600 | } |
|---|
| 434 | 601 | |
|---|
| .. | .. |
|---|
| 439 | 606 | } |
|---|
| 440 | 607 | |
|---|
| 441 | 608 | /* create the branch to the trampoline */ |
|---|
| 442 | | - op = create_branch((unsigned int *)ip, |
|---|
| 443 | | - rec->arch.mod->arch.tramp, BRANCH_SET_LINK); |
|---|
| 444 | | - if (!op) { |
|---|
| 609 | + err = create_branch(&op, (struct ppc_inst *)ip, |
|---|
| 610 | + rec->arch.mod->arch.tramp, BRANCH_SET_LINK); |
|---|
| 611 | + if (err) { |
|---|
| 445 | 612 | pr_err("REL24 out of range!\n"); |
|---|
| 446 | 613 | return -EINVAL; |
|---|
| 447 | 614 | } |
|---|
| 448 | 615 | |
|---|
| 449 | 616 | pr_devel("write to %lx\n", rec->ip); |
|---|
| 450 | 617 | |
|---|
| 451 | | - if (patch_instruction((unsigned int *)ip, op)) |
|---|
| 618 | + if (patch_instruction((struct ppc_inst *)ip, op)) |
|---|
| 452 | 619 | return -EPERM; |
|---|
| 453 | 620 | |
|---|
| 454 | 621 | return 0; |
|---|
| .. | .. |
|---|
| 456 | 623 | #endif /* CONFIG_PPC64 */ |
|---|
| 457 | 624 | #endif /* CONFIG_MODULES */ |
|---|
| 458 | 625 | |
|---|
| 626 | +static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr) |
|---|
| 627 | +{ |
|---|
| 628 | + struct ppc_inst op; |
|---|
| 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 */ |
|---|
| 649 | + if (probe_kernel_read_inst(&op, ip)) { |
|---|
| 650 | + pr_err("Unable to read ftrace location %p\n", ip); |
|---|
| 651 | + return -EFAULT; |
|---|
| 652 | + } |
|---|
| 653 | + |
|---|
| 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)); |
|---|
| 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 | + |
|---|
| 459 | 673 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
|---|
| 460 | 674 | { |
|---|
| 461 | 675 | unsigned long ip = rec->ip; |
|---|
| 462 | | - unsigned int old, new; |
|---|
| 676 | + struct ppc_inst old, new; |
|---|
| 463 | 677 | |
|---|
| 464 | 678 | /* |
|---|
| 465 | 679 | * If the calling address is more that 24 bits away, |
|---|
| .. | .. |
|---|
| 468 | 682 | */ |
|---|
| 469 | 683 | if (test_24bit_addr(ip, addr)) { |
|---|
| 470 | 684 | /* within range */ |
|---|
| 471 | | - old = PPC_INST_NOP; |
|---|
| 685 | + old = ppc_inst(PPC_INST_NOP); |
|---|
| 472 | 686 | new = ftrace_call_replace(ip, addr, 1); |
|---|
| 473 | 687 | return ftrace_modify_code(ip, old, new); |
|---|
| 474 | | - } |
|---|
| 688 | + } else if (core_kernel_text(ip)) |
|---|
| 689 | + return __ftrace_make_call_kernel(rec, addr); |
|---|
| 475 | 690 | |
|---|
| 476 | 691 | #ifdef CONFIG_MODULES |
|---|
| 477 | 692 | /* |
|---|
| .. | .. |
|---|
| 497 | 712 | __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, |
|---|
| 498 | 713 | unsigned long addr) |
|---|
| 499 | 714 | { |
|---|
| 500 | | - unsigned int op; |
|---|
| 715 | + struct ppc_inst op; |
|---|
| 501 | 716 | unsigned long ip = rec->ip; |
|---|
| 502 | 717 | unsigned long entry, ptr, tramp; |
|---|
| 503 | 718 | struct module *mod = rec->arch.mod; |
|---|
| .. | .. |
|---|
| 509 | 724 | } |
|---|
| 510 | 725 | |
|---|
| 511 | 726 | /* read where this goes */ |
|---|
| 512 | | - if (probe_kernel_read(&op, (void *)ip, sizeof(int))) { |
|---|
| 727 | + if (probe_kernel_read_inst(&op, (void *)ip)) { |
|---|
| 513 | 728 | pr_err("Fetching opcode failed.\n"); |
|---|
| 514 | 729 | return -EFAULT; |
|---|
| 515 | 730 | } |
|---|
| 516 | 731 | |
|---|
| 517 | 732 | /* Make sure that that this is still a 24bit jump */ |
|---|
| 518 | 733 | if (!is_bl_op(op)) { |
|---|
| 519 | | - pr_err("Not expected bl: opcode is %x\n", op); |
|---|
| 734 | + pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op)); |
|---|
| 520 | 735 | return -EINVAL; |
|---|
| 521 | 736 | } |
|---|
| 522 | 737 | |
|---|
| .. | .. |
|---|
| 545 | 760 | /* The new target may be within range */ |
|---|
| 546 | 761 | if (test_24bit_addr(ip, addr)) { |
|---|
| 547 | 762 | /* within range */ |
|---|
| 548 | | - if (patch_branch((unsigned int *)ip, addr, BRANCH_SET_LINK)) { |
|---|
| 763 | + if (patch_branch((struct ppc_inst *)ip, addr, BRANCH_SET_LINK)) { |
|---|
| 549 | 764 | pr_err("REL24 out of range!\n"); |
|---|
| 550 | 765 | return -EINVAL; |
|---|
| 551 | 766 | } |
|---|
| .. | .. |
|---|
| 573 | 788 | } |
|---|
| 574 | 789 | |
|---|
| 575 | 790 | /* Ensure branch is within 24 bits */ |
|---|
| 576 | | - if (!create_branch((unsigned int *)ip, tramp, BRANCH_SET_LINK)) { |
|---|
| 791 | + if (create_branch(&op, (struct ppc_inst *)ip, tramp, BRANCH_SET_LINK)) { |
|---|
| 577 | 792 | pr_err("Branch out of range\n"); |
|---|
| 578 | 793 | return -EINVAL; |
|---|
| 579 | 794 | } |
|---|
| 580 | 795 | |
|---|
| 581 | | - if (patch_branch((unsigned int *)ip, tramp, BRANCH_SET_LINK)) { |
|---|
| 796 | + if (patch_branch((struct ppc_inst *)ip, tramp, BRANCH_SET_LINK)) { |
|---|
| 582 | 797 | pr_err("REL24 out of range!\n"); |
|---|
| 583 | 798 | return -EINVAL; |
|---|
| 584 | 799 | } |
|---|
| .. | .. |
|---|
| 591 | 806 | unsigned long addr) |
|---|
| 592 | 807 | { |
|---|
| 593 | 808 | unsigned long ip = rec->ip; |
|---|
| 594 | | - unsigned int old, new; |
|---|
| 809 | + struct ppc_inst old, new; |
|---|
| 595 | 810 | |
|---|
| 596 | 811 | /* |
|---|
| 597 | 812 | * If the calling address is more that 24 bits away, |
|---|
| .. | .. |
|---|
| 603 | 818 | old = ftrace_call_replace(ip, old_addr, 1); |
|---|
| 604 | 819 | new = ftrace_call_replace(ip, addr, 1); |
|---|
| 605 | 820 | return ftrace_modify_code(ip, old, new); |
|---|
| 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; |
|---|
| 606 | 827 | } |
|---|
| 607 | 828 | |
|---|
| 608 | 829 | #ifdef CONFIG_MODULES |
|---|
| .. | .. |
|---|
| 625 | 846 | int ftrace_update_ftrace_func(ftrace_func_t func) |
|---|
| 626 | 847 | { |
|---|
| 627 | 848 | unsigned long ip = (unsigned long)(&ftrace_call); |
|---|
| 628 | | - unsigned int old, new; |
|---|
| 849 | + struct ppc_inst old, new; |
|---|
| 629 | 850 | int ret; |
|---|
| 630 | 851 | |
|---|
| 631 | | - old = *(unsigned int *)&ftrace_call; |
|---|
| 852 | + old = ppc_inst_read((struct ppc_inst *)&ftrace_call); |
|---|
| 632 | 853 | new = ftrace_call_replace(ip, (unsigned long)func, 1); |
|---|
| 633 | 854 | ret = ftrace_modify_code(ip, old, new); |
|---|
| 634 | 855 | |
|---|
| .. | .. |
|---|
| 636 | 857 | /* Also update the regs callback function */ |
|---|
| 637 | 858 | if (!ret) { |
|---|
| 638 | 859 | ip = (unsigned long)(&ftrace_regs_call); |
|---|
| 639 | | - old = *(unsigned int *)&ftrace_regs_call; |
|---|
| 860 | + old = ppc_inst_read((struct ppc_inst *)&ftrace_regs_call); |
|---|
| 640 | 861 | new = ftrace_call_replace(ip, (unsigned long)func, 1); |
|---|
| 641 | 862 | ret = ftrace_modify_code(ip, old, new); |
|---|
| 642 | 863 | } |
|---|
| .. | .. |
|---|
| 654 | 875 | ftrace_modify_all_code(command); |
|---|
| 655 | 876 | } |
|---|
| 656 | 877 | |
|---|
| 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 | + |
|---|
| 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 | + |
|---|
| 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 |
|---|
| 657 | 928 | int __init ftrace_dyn_arch_init(void) |
|---|
| 658 | 929 | { |
|---|
| 659 | 930 | return 0; |
|---|
| 660 | 931 | } |
|---|
| 932 | +#endif |
|---|
| 661 | 933 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
|---|
| 662 | 934 | |
|---|
| 663 | 935 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
|---|
| .. | .. |
|---|
| 670 | 942 | unsigned long ip = (unsigned long)(&ftrace_graph_call); |
|---|
| 671 | 943 | unsigned long addr = (unsigned long)(&ftrace_graph_caller); |
|---|
| 672 | 944 | unsigned long stub = (unsigned long)(&ftrace_graph_stub); |
|---|
| 673 | | - unsigned int old, new; |
|---|
| 945 | + struct ppc_inst old, new; |
|---|
| 674 | 946 | |
|---|
| 675 | 947 | old = ftrace_call_replace(ip, stub, 0); |
|---|
| 676 | 948 | new = ftrace_call_replace(ip, addr, 0); |
|---|
| .. | .. |
|---|
| 683 | 955 | unsigned long ip = (unsigned long)(&ftrace_graph_call); |
|---|
| 684 | 956 | unsigned long addr = (unsigned long)(&ftrace_graph_caller); |
|---|
| 685 | 957 | unsigned long stub = (unsigned long)(&ftrace_graph_stub); |
|---|
| 686 | | - unsigned int old, new; |
|---|
| 958 | + struct ppc_inst old, new; |
|---|
| 687 | 959 | |
|---|
| 688 | 960 | old = ftrace_call_replace(ip, addr, 0); |
|---|
| 689 | 961 | new = ftrace_call_replace(ip, stub, 0); |
|---|
| .. | .. |
|---|
| 695 | 967 | * Hook the return address and push it in the stack of return addrs |
|---|
| 696 | 968 | * in current thread info. Return the address we want to divert to. |
|---|
| 697 | 969 | */ |
|---|
| 698 | | -unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip) |
|---|
| 970 | +unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip, |
|---|
| 971 | + unsigned long sp) |
|---|
| 699 | 972 | { |
|---|
| 700 | 973 | unsigned long return_hooker; |
|---|
| 701 | 974 | |
|---|
| .. | .. |
|---|
| 707 | 980 | |
|---|
| 708 | 981 | return_hooker = ppc_function_entry(return_to_handler); |
|---|
| 709 | 982 | |
|---|
| 710 | | - if (!function_graph_enter(parent, ip, 0, NULL)) |
|---|
| 983 | + if (!function_graph_enter(parent, ip, 0, (unsigned long *)sp)) |
|---|
| 711 | 984 | parent = return_hooker; |
|---|
| 712 | 985 | out: |
|---|
| 713 | 986 | return parent; |
|---|
| 714 | 987 | } |
|---|
| 715 | 988 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
|---|
| 716 | | - |
|---|
| 717 | | -#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64) |
|---|
| 718 | | -unsigned long __init arch_syscall_addr(int nr) |
|---|
| 719 | | -{ |
|---|
| 720 | | - return sys_call_table[nr*2]; |
|---|
| 721 | | -} |
|---|
| 722 | | -#endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */ |
|---|
| 723 | 989 | |
|---|
| 724 | 990 | #ifdef PPC64_ELF_ABI_v1 |
|---|
| 725 | 991 | char *arch_ftrace_match_adjust(char *str, const char *search) |
|---|