ftrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Code for replacing ftrace calls with jumps.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
  7. *
  8. * Added function graph tracer code, taken from x86 that was written
  9. * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
  10. *
  11. */
  12. #define pr_fmt(fmt) "ftrace-powerpc: " fmt
  13. #include <linux/spinlock.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/module.h>
  17. #include <linux/ftrace.h>
  18. #include <linux/percpu.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/code-patching.h>
  23. #include <asm/ftrace.h>
  24. #include <asm/syscall.h>
  25. #ifdef CONFIG_DYNAMIC_FTRACE
  26. static unsigned int
  27. ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
  28. {
  29. unsigned int op;
  30. addr = ppc_function_entry((void *)addr);
  31. /* if (link) set op to 'bl' else 'b' */
  32. op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
  33. return op;
  34. }
  35. static int
  36. ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
  37. {
  38. unsigned int replaced;
  39. /*
  40. * Note: Due to modules and __init, code can
  41. * disappear and change, we need to protect against faulting
  42. * as well as code changing. We do this by using the
  43. * probe_kernel_* functions.
  44. *
  45. * No real locking needed, this code is run through
  46. * kstop_machine, or before SMP starts.
  47. */
  48. /* read the text we want to modify */
  49. if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
  50. return -EFAULT;
  51. /* Make sure it is what we expect it to be */
  52. if (replaced != old) {
  53. pr_err("%p: replaced (%#x) != old (%#x)",
  54. (void *)ip, replaced, old);
  55. return -EINVAL;
  56. }
  57. /* replace the text with the new text */
  58. if (patch_instruction((unsigned int *)ip, new))
  59. return -EPERM;
  60. return 0;
  61. }
  62. /*
  63. * Helper functions that are the same for both PPC64 and PPC32.
  64. */
  65. static int test_24bit_addr(unsigned long ip, unsigned long addr)
  66. {
  67. addr = ppc_function_entry((void *)addr);
  68. /* use the create_branch to verify that this offset can be branched */
  69. return create_branch((unsigned int *)ip, addr, 0);
  70. }
  71. #ifdef CONFIG_MODULES
  72. static int is_bl_op(unsigned int op)
  73. {
  74. return (op & 0xfc000003) == 0x48000001;
  75. }
  76. static unsigned long find_bl_target(unsigned long ip, unsigned int op)
  77. {
  78. static int offset;
  79. offset = (op & 0x03fffffc);
  80. /* make it signed */
  81. if (offset & 0x02000000)
  82. offset |= 0xfe000000;
  83. return ip + (long)offset;
  84. }
  85. #ifdef CONFIG_PPC64
  86. static int
  87. __ftrace_make_nop(struct module *mod,
  88. struct dyn_ftrace *rec, unsigned long addr)
  89. {
  90. unsigned long entry, ptr, tramp;
  91. unsigned long ip = rec->ip;
  92. unsigned int op, pop;
  93. /* read where this goes */
  94. if (probe_kernel_read(&op, (void *)ip, sizeof(int))) {
  95. pr_err("Fetching opcode failed.\n");
  96. return -EFAULT;
  97. }
  98. /* Make sure that that this is still a 24bit jump */
  99. if (!is_bl_op(op)) {
  100. pr_err("Not expected bl: opcode is %x\n", op);
  101. return -EINVAL;
  102. }
  103. /* lets find where the pointer goes */
  104. tramp = find_bl_target(ip, op);
  105. pr_devel("ip:%lx jumps to %lx", ip, tramp);
  106. if (module_trampoline_target(mod, tramp, &ptr)) {
  107. pr_err("Failed to get trampoline target\n");
  108. return -EFAULT;
  109. }
  110. pr_devel("trampoline target %lx", ptr);
  111. entry = ppc_global_function_entry((void *)addr);
  112. /* This should match what was called */
  113. if (ptr != entry) {
  114. pr_err("addr %lx does not match expected %lx\n", ptr, entry);
  115. return -EINVAL;
  116. }
  117. #ifdef CC_USING_MPROFILE_KERNEL
  118. /* When using -mkernel_profile there is no load to jump over */
  119. pop = PPC_INST_NOP;
  120. if (probe_kernel_read(&op, (void *)(ip - 4), 4)) {
  121. pr_err("Fetching instruction at %lx failed.\n", ip - 4);
  122. return -EFAULT;
  123. }
  124. /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */
  125. if (op != PPC_INST_MFLR && op != PPC_INST_STD_LR) {
  126. pr_err("Unexpected instruction %08x around bl _mcount\n", op);
  127. return -EINVAL;
  128. }
  129. #else
  130. /*
  131. * Our original call site looks like:
  132. *
  133. * bl <tramp>
  134. * ld r2,XX(r1)
  135. *
  136. * Milton Miller pointed out that we can not simply nop the branch.
  137. * If a task was preempted when calling a trace function, the nops
  138. * will remove the way to restore the TOC in r2 and the r2 TOC will
  139. * get corrupted.
  140. *
  141. * Use a b +8 to jump over the load.
  142. */
  143. pop = PPC_INST_BRANCH | 8; /* b +8 */
  144. /*
  145. * Check what is in the next instruction. We can see ld r2,40(r1), but
  146. * on first pass after boot we will see mflr r0.
  147. */
  148. if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE)) {
  149. pr_err("Fetching op failed.\n");
  150. return -EFAULT;
  151. }
  152. if (op != PPC_INST_LD_TOC) {
  153. pr_err("Expected %08x found %08x\n", PPC_INST_LD_TOC, op);
  154. return -EINVAL;
  155. }
  156. #endif /* CC_USING_MPROFILE_KERNEL */
  157. if (patch_instruction((unsigned int *)ip, pop)) {
  158. pr_err("Patching NOP failed.\n");
  159. return -EPERM;
  160. }
  161. return 0;
  162. }
  163. #else /* !PPC64 */
  164. static int
  165. __ftrace_make_nop(struct module *mod,
  166. struct dyn_ftrace *rec, unsigned long addr)
  167. {
  168. unsigned int op;
  169. unsigned int jmp[4];
  170. unsigned long ip = rec->ip;
  171. unsigned long tramp;
  172. if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
  173. return -EFAULT;
  174. /* Make sure that that this is still a 24bit jump */
  175. if (!is_bl_op(op)) {
  176. pr_err("Not expected bl: opcode is %x\n", op);
  177. return -EINVAL;
  178. }
  179. /* lets find where the pointer goes */
  180. tramp = find_bl_target(ip, op);
  181. /*
  182. * On PPC32 the trampoline looks like:
  183. * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
  184. * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
  185. * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
  186. * 0x4e, 0x80, 0x04, 0x20 bctr
  187. */
  188. pr_devel("ip:%lx jumps to %lx", ip, tramp);
  189. /* Find where the trampoline jumps to */
  190. if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
  191. pr_err("Failed to read %lx\n", tramp);
  192. return -EFAULT;
  193. }
  194. pr_devel(" %08x %08x ", jmp[0], jmp[1]);
  195. /* verify that this is what we expect it to be */
  196. if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
  197. ((jmp[1] & 0xffff0000) != 0x398c0000) ||
  198. (jmp[2] != 0x7d8903a6) ||
  199. (jmp[3] != 0x4e800420)) {
  200. pr_err("Not a trampoline\n");
  201. return -EINVAL;
  202. }
  203. tramp = (jmp[1] & 0xffff) |
  204. ((jmp[0] & 0xffff) << 16);
  205. if (tramp & 0x8000)
  206. tramp -= 0x10000;
  207. pr_devel(" %lx ", tramp);
  208. if (tramp != addr) {
  209. pr_err("Trampoline location %08lx does not match addr\n",
  210. tramp);
  211. return -EINVAL;
  212. }
  213. op = PPC_INST_NOP;
  214. if (patch_instruction((unsigned int *)ip, op))
  215. return -EPERM;
  216. return 0;
  217. }
  218. #endif /* PPC64 */
  219. #endif /* CONFIG_MODULES */
  220. int ftrace_make_nop(struct module *mod,
  221. struct dyn_ftrace *rec, unsigned long addr)
  222. {
  223. unsigned long ip = rec->ip;
  224. unsigned int old, new;
  225. /*
  226. * If the calling address is more that 24 bits away,
  227. * then we had to use a trampoline to make the call.
  228. * Otherwise just update the call site.
  229. */
  230. if (test_24bit_addr(ip, addr)) {
  231. /* within range */
  232. old = ftrace_call_replace(ip, addr, 1);
  233. new = PPC_INST_NOP;
  234. return ftrace_modify_code(ip, old, new);
  235. }
  236. #ifdef CONFIG_MODULES
  237. /*
  238. * Out of range jumps are called from modules.
  239. * We should either already have a pointer to the module
  240. * or it has been passed in.
  241. */
  242. if (!rec->arch.mod) {
  243. if (!mod) {
  244. pr_err("No module loaded addr=%lx\n", addr);
  245. return -EFAULT;
  246. }
  247. rec->arch.mod = mod;
  248. } else if (mod) {
  249. if (mod != rec->arch.mod) {
  250. pr_err("Record mod %p not equal to passed in mod %p\n",
  251. rec->arch.mod, mod);
  252. return -EINVAL;
  253. }
  254. /* nothing to do if mod == rec->arch.mod */
  255. } else
  256. mod = rec->arch.mod;
  257. return __ftrace_make_nop(mod, rec, addr);
  258. #else
  259. /* We should not get here without modules */
  260. return -EINVAL;
  261. #endif /* CONFIG_MODULES */
  262. }
  263. #ifdef CONFIG_MODULES
  264. #ifdef CONFIG_PPC64
  265. /*
  266. * Examine the existing instructions for __ftrace_make_call.
  267. * They should effectively be a NOP, and follow formal constraints,
  268. * depending on the ABI. Return false if they don't.
  269. */
  270. #ifndef CC_USING_MPROFILE_KERNEL
  271. static int
  272. expected_nop_sequence(void *ip, unsigned int op0, unsigned int op1)
  273. {
  274. /*
  275. * We expect to see:
  276. *
  277. * b +8
  278. * ld r2,XX(r1)
  279. *
  280. * The load offset is different depending on the ABI. For simplicity
  281. * just mask it out when doing the compare.
  282. */
  283. if ((op0 != 0x48000008) || ((op1 & 0xffff0000) != 0xe8410000))
  284. return 0;
  285. return 1;
  286. }
  287. #else
  288. static int
  289. expected_nop_sequence(void *ip, unsigned int op0, unsigned int op1)
  290. {
  291. /* look for patched "NOP" on ppc64 with -mprofile-kernel */
  292. if (op0 != PPC_INST_NOP)
  293. return 0;
  294. return 1;
  295. }
  296. #endif
  297. static int
  298. __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  299. {
  300. unsigned int op[2];
  301. void *ip = (void *)rec->ip;
  302. /* read where this goes */
  303. if (probe_kernel_read(op, ip, sizeof(op)))
  304. return -EFAULT;
  305. if (!expected_nop_sequence(ip, op[0], op[1])) {
  306. pr_err("Unexpected call sequence at %p: %x %x\n",
  307. ip, op[0], op[1]);
  308. return -EINVAL;
  309. }
  310. /* If we never set up a trampoline to ftrace_caller, then bail */
  311. if (!rec->arch.mod->arch.tramp) {
  312. pr_err("No ftrace trampoline\n");
  313. return -EINVAL;
  314. }
  315. /* Ensure branch is within 24 bits */
  316. if (!create_branch(ip, rec->arch.mod->arch.tramp, BRANCH_SET_LINK)) {
  317. pr_err("Branch out of range\n");
  318. return -EINVAL;
  319. }
  320. if (patch_branch(ip, rec->arch.mod->arch.tramp, BRANCH_SET_LINK)) {
  321. pr_err("REL24 out of range!\n");
  322. return -EINVAL;
  323. }
  324. return 0;
  325. }
  326. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  327. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  328. unsigned long addr)
  329. {
  330. return ftrace_make_call(rec, addr);
  331. }
  332. #endif
  333. #else /* !CONFIG_PPC64: */
  334. static int
  335. __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  336. {
  337. unsigned int op;
  338. unsigned long ip = rec->ip;
  339. /* read where this goes */
  340. if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
  341. return -EFAULT;
  342. /* It should be pointing to a nop */
  343. if (op != PPC_INST_NOP) {
  344. pr_err("Expected NOP but have %x\n", op);
  345. return -EINVAL;
  346. }
  347. /* If we never set up a trampoline to ftrace_caller, then bail */
  348. if (!rec->arch.mod->arch.tramp) {
  349. pr_err("No ftrace trampoline\n");
  350. return -EINVAL;
  351. }
  352. /* create the branch to the trampoline */
  353. op = create_branch((unsigned int *)ip,
  354. rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
  355. if (!op) {
  356. pr_err("REL24 out of range!\n");
  357. return -EINVAL;
  358. }
  359. pr_devel("write to %lx\n", rec->ip);
  360. if (patch_instruction((unsigned int *)ip, op))
  361. return -EPERM;
  362. return 0;
  363. }
  364. #endif /* CONFIG_PPC64 */
  365. #endif /* CONFIG_MODULES */
  366. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  367. {
  368. unsigned long ip = rec->ip;
  369. unsigned int old, new;
  370. /*
  371. * If the calling address is more that 24 bits away,
  372. * then we had to use a trampoline to make the call.
  373. * Otherwise just update the call site.
  374. */
  375. if (test_24bit_addr(ip, addr)) {
  376. /* within range */
  377. old = PPC_INST_NOP;
  378. new = ftrace_call_replace(ip, addr, 1);
  379. return ftrace_modify_code(ip, old, new);
  380. }
  381. #ifdef CONFIG_MODULES
  382. /*
  383. * Out of range jumps are called from modules.
  384. * Being that we are converting from nop, it had better
  385. * already have a module defined.
  386. */
  387. if (!rec->arch.mod) {
  388. pr_err("No module loaded\n");
  389. return -EINVAL;
  390. }
  391. return __ftrace_make_call(rec, addr);
  392. #else
  393. /* We should not get here without modules */
  394. return -EINVAL;
  395. #endif /* CONFIG_MODULES */
  396. }
  397. int ftrace_update_ftrace_func(ftrace_func_t func)
  398. {
  399. unsigned long ip = (unsigned long)(&ftrace_call);
  400. unsigned int old, new;
  401. int ret;
  402. old = *(unsigned int *)&ftrace_call;
  403. new = ftrace_call_replace(ip, (unsigned long)func, 1);
  404. ret = ftrace_modify_code(ip, old, new);
  405. return ret;
  406. }
  407. static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  408. {
  409. unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
  410. int ret;
  411. ret = ftrace_update_record(rec, enable);
  412. switch (ret) {
  413. case FTRACE_UPDATE_IGNORE:
  414. return 0;
  415. case FTRACE_UPDATE_MAKE_CALL:
  416. return ftrace_make_call(rec, ftrace_addr);
  417. case FTRACE_UPDATE_MAKE_NOP:
  418. return ftrace_make_nop(NULL, rec, ftrace_addr);
  419. }
  420. return 0;
  421. }
  422. void ftrace_replace_code(int enable)
  423. {
  424. struct ftrace_rec_iter *iter;
  425. struct dyn_ftrace *rec;
  426. int ret;
  427. for (iter = ftrace_rec_iter_start(); iter;
  428. iter = ftrace_rec_iter_next(iter)) {
  429. rec = ftrace_rec_iter_record(iter);
  430. ret = __ftrace_replace_code(rec, enable);
  431. if (ret) {
  432. ftrace_bug(ret, rec);
  433. return;
  434. }
  435. }
  436. }
  437. /*
  438. * Use the default ftrace_modify_all_code, but without
  439. * stop_machine().
  440. */
  441. void arch_ftrace_update_code(int command)
  442. {
  443. ftrace_modify_all_code(command);
  444. }
  445. int __init ftrace_dyn_arch_init(void)
  446. {
  447. return 0;
  448. }
  449. #endif /* CONFIG_DYNAMIC_FTRACE */
  450. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  451. #ifdef CONFIG_DYNAMIC_FTRACE
  452. extern void ftrace_graph_call(void);
  453. extern void ftrace_graph_stub(void);
  454. int ftrace_enable_ftrace_graph_caller(void)
  455. {
  456. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  457. unsigned long addr = (unsigned long)(&ftrace_graph_caller);
  458. unsigned long stub = (unsigned long)(&ftrace_graph_stub);
  459. unsigned int old, new;
  460. old = ftrace_call_replace(ip, stub, 0);
  461. new = ftrace_call_replace(ip, addr, 0);
  462. return ftrace_modify_code(ip, old, new);
  463. }
  464. int ftrace_disable_ftrace_graph_caller(void)
  465. {
  466. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  467. unsigned long addr = (unsigned long)(&ftrace_graph_caller);
  468. unsigned long stub = (unsigned long)(&ftrace_graph_stub);
  469. unsigned int old, new;
  470. old = ftrace_call_replace(ip, addr, 0);
  471. new = ftrace_call_replace(ip, stub, 0);
  472. return ftrace_modify_code(ip, old, new);
  473. }
  474. #endif /* CONFIG_DYNAMIC_FTRACE */
  475. /*
  476. * Hook the return address and push it in the stack of return addrs
  477. * in current thread info. Return the address we want to divert to.
  478. */
  479. unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
  480. {
  481. struct ftrace_graph_ent trace;
  482. unsigned long return_hooker;
  483. if (unlikely(ftrace_graph_is_dead()))
  484. goto out;
  485. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  486. goto out;
  487. return_hooker = ppc_function_entry(return_to_handler);
  488. trace.func = ip;
  489. trace.depth = current->curr_ret_stack + 1;
  490. /* Only trace if the calling function expects to */
  491. if (!ftrace_graph_entry(&trace))
  492. goto out;
  493. if (ftrace_push_return_trace(parent, ip, &trace.depth, 0,
  494. NULL) == -EBUSY)
  495. goto out;
  496. parent = return_hooker;
  497. out:
  498. return parent;
  499. }
  500. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  501. #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
  502. unsigned long __init arch_syscall_addr(int nr)
  503. {
  504. return sys_call_table[nr*2];
  505. }
  506. #endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */
  507. #ifdef PPC64_ELF_ABI_v1
  508. char *arch_ftrace_match_adjust(char *str, const char *search)
  509. {
  510. if (str[0] == '.' && search[0] != '.')
  511. return str + 1;
  512. else
  513. return str;
  514. }
  515. #endif /* PPC64_ELF_ABI_v1 */