ftrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. #include <linux/spinlock.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/module.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/percpu.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/code-patching.h>
  22. #include <asm/ftrace.h>
  23. #ifdef CONFIG_PPC32
  24. # define GET_ADDR(addr) addr
  25. #else
  26. /* PowerPC64's functions are data that points to the functions */
  27. # define GET_ADDR(addr) (*(unsigned long *)addr)
  28. #endif
  29. #ifdef CONFIG_DYNAMIC_FTRACE
  30. static unsigned int ftrace_nop_replace(void)
  31. {
  32. return PPC_INST_NOP;
  33. }
  34. static unsigned int
  35. ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
  36. {
  37. unsigned int op;
  38. addr = GET_ADDR(addr);
  39. /* if (link) set op to 'bl' else 'b' */
  40. op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
  41. return op;
  42. }
  43. #ifdef CONFIG_PPC64
  44. # define _ASM_ALIGN " .align 3 "
  45. # define _ASM_PTR " .llong "
  46. #else
  47. # define _ASM_ALIGN " .align 2 "
  48. # define _ASM_PTR " .long "
  49. #endif
  50. static int
  51. ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
  52. {
  53. unsigned int replaced;
  54. /*
  55. * Note: Due to modules and __init, code can
  56. * disappear and change, we need to protect against faulting
  57. * as well as code changing. We do this by using the
  58. * probe_kernel_* functions.
  59. *
  60. * No real locking needed, this code is run through
  61. * kstop_machine, or before SMP starts.
  62. */
  63. /* read the text we want to modify */
  64. if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
  65. return -EFAULT;
  66. /* Make sure it is what we expect it to be */
  67. if (replaced != old)
  68. return -EINVAL;
  69. /* replace the text with the new text */
  70. if (probe_kernel_write((void *)ip, &new, MCOUNT_INSN_SIZE))
  71. return -EPERM;
  72. flush_icache_range(ip, ip + 8);
  73. return 0;
  74. }
  75. /*
  76. * Helper functions that are the same for both PPC64 and PPC32.
  77. */
  78. static int test_24bit_addr(unsigned long ip, unsigned long addr)
  79. {
  80. /* use the create_branch to verify that this offset can be branched */
  81. return create_branch((unsigned int *)ip, addr, 0);
  82. }
  83. #ifdef CONFIG_MODULES
  84. static int is_bl_op(unsigned int op)
  85. {
  86. return (op & 0xfc000003) == 0x48000001;
  87. }
  88. static unsigned long find_bl_target(unsigned long ip, unsigned int op)
  89. {
  90. static int offset;
  91. offset = (op & 0x03fffffc);
  92. /* make it signed */
  93. if (offset & 0x02000000)
  94. offset |= 0xfe000000;
  95. return ip + (long)offset;
  96. }
  97. #ifdef CONFIG_PPC64
  98. static int
  99. __ftrace_make_nop(struct module *mod,
  100. struct dyn_ftrace *rec, unsigned long addr)
  101. {
  102. unsigned int op;
  103. unsigned int jmp[5];
  104. unsigned long ptr;
  105. unsigned long ip = rec->ip;
  106. unsigned long tramp;
  107. int offset;
  108. /* read where this goes */
  109. if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
  110. return -EFAULT;
  111. /* Make sure that that this is still a 24bit jump */
  112. if (!is_bl_op(op)) {
  113. printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
  114. return -EINVAL;
  115. }
  116. /* lets find where the pointer goes */
  117. tramp = find_bl_target(ip, op);
  118. /*
  119. * On PPC64 the trampoline looks like:
  120. * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
  121. * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
  122. * Where the bytes 2,3,6 and 7 make up the 32bit offset
  123. * to the TOC that holds the pointer.
  124. * to jump to.
  125. * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
  126. * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
  127. * The actually address is 32 bytes from the offset
  128. * into the TOC.
  129. * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
  130. */
  131. pr_devel("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
  132. /* Find where the trampoline jumps to */
  133. if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
  134. printk(KERN_ERR "Failed to read %lx\n", tramp);
  135. return -EFAULT;
  136. }
  137. pr_devel(" %08x %08x", jmp[0], jmp[1]);
  138. /* verify that this is what we expect it to be */
  139. if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
  140. ((jmp[1] & 0xffff0000) != 0x398c0000) ||
  141. (jmp[2] != 0xf8410028) ||
  142. (jmp[3] != 0xe96c0020) ||
  143. (jmp[4] != 0xe84c0028)) {
  144. printk(KERN_ERR "Not a trampoline\n");
  145. return -EINVAL;
  146. }
  147. /* The bottom half is signed extended */
  148. offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
  149. (int)((short)jmp[1]);
  150. pr_devel(" %x ", offset);
  151. /* get the address this jumps too */
  152. tramp = mod->arch.toc + offset + 32;
  153. pr_devel("toc: %lx", tramp);
  154. if (probe_kernel_read(jmp, (void *)tramp, 8)) {
  155. printk(KERN_ERR "Failed to read %lx\n", tramp);
  156. return -EFAULT;
  157. }
  158. pr_devel(" %08x %08x\n", jmp[0], jmp[1]);
  159. ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
  160. /* This should match what was called */
  161. if (ptr != GET_ADDR(addr)) {
  162. printk(KERN_ERR "addr does not match %lx\n", ptr);
  163. return -EINVAL;
  164. }
  165. /*
  166. * We want to nop the line, but the next line is
  167. * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
  168. * This needs to be turned to a nop too.
  169. */
  170. if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
  171. return -EFAULT;
  172. if (op != 0xe8410028) {
  173. printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
  174. return -EINVAL;
  175. }
  176. /*
  177. * Milton Miller pointed out that we can not blindly do nops.
  178. * If a task was preempted when calling a trace function,
  179. * the nops will remove the way to restore the TOC in r2
  180. * and the r2 TOC will get corrupted.
  181. */
  182. /*
  183. * Replace:
  184. * bl <tramp> <==== will be replaced with "b 1f"
  185. * ld r2,40(r1)
  186. * 1:
  187. */
  188. op = 0x48000008; /* b +8 */
  189. if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
  190. return -EPERM;
  191. flush_icache_range(ip, ip + 8);
  192. return 0;
  193. }
  194. #else /* !PPC64 */
  195. static int
  196. __ftrace_make_nop(struct module *mod,
  197. struct dyn_ftrace *rec, unsigned long addr)
  198. {
  199. unsigned int op;
  200. unsigned int jmp[4];
  201. unsigned long ip = rec->ip;
  202. unsigned long tramp;
  203. if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
  204. return -EFAULT;
  205. /* Make sure that that this is still a 24bit jump */
  206. if (!is_bl_op(op)) {
  207. printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
  208. return -EINVAL;
  209. }
  210. /* lets find where the pointer goes */
  211. tramp = find_bl_target(ip, op);
  212. /*
  213. * On PPC32 the trampoline looks like:
  214. * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
  215. * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
  216. * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
  217. * 0x4e, 0x80, 0x04, 0x20 bctr
  218. */
  219. pr_devel("ip:%lx jumps to %lx", ip, tramp);
  220. /* Find where the trampoline jumps to */
  221. if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
  222. printk(KERN_ERR "Failed to read %lx\n", tramp);
  223. return -EFAULT;
  224. }
  225. pr_devel(" %08x %08x ", jmp[0], jmp[1]);
  226. /* verify that this is what we expect it to be */
  227. if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
  228. ((jmp[1] & 0xffff0000) != 0x396b0000) ||
  229. (jmp[2] != 0x7d6903a6) ||
  230. (jmp[3] != 0x4e800420)) {
  231. printk(KERN_ERR "Not a trampoline\n");
  232. return -EINVAL;
  233. }
  234. tramp = (jmp[1] & 0xffff) |
  235. ((jmp[0] & 0xffff) << 16);
  236. if (tramp & 0x8000)
  237. tramp -= 0x10000;
  238. pr_devel(" %lx ", tramp);
  239. if (tramp != addr) {
  240. printk(KERN_ERR
  241. "Trampoline location %08lx does not match addr\n",
  242. tramp);
  243. return -EINVAL;
  244. }
  245. op = PPC_INST_NOP;
  246. if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
  247. return -EPERM;
  248. flush_icache_range(ip, ip + 8);
  249. return 0;
  250. }
  251. #endif /* PPC64 */
  252. #endif /* CONFIG_MODULES */
  253. int ftrace_make_nop(struct module *mod,
  254. struct dyn_ftrace *rec, unsigned long addr)
  255. {
  256. unsigned long ip = rec->ip;
  257. unsigned int old, new;
  258. /*
  259. * If the calling address is more that 24 bits away,
  260. * then we had to use a trampoline to make the call.
  261. * Otherwise just update the call site.
  262. */
  263. if (test_24bit_addr(ip, addr)) {
  264. /* within range */
  265. old = ftrace_call_replace(ip, addr, 1);
  266. new = ftrace_nop_replace();
  267. return ftrace_modify_code(ip, old, new);
  268. }
  269. #ifdef CONFIG_MODULES
  270. /*
  271. * Out of range jumps are called from modules.
  272. * We should either already have a pointer to the module
  273. * or it has been passed in.
  274. */
  275. if (!rec->arch.mod) {
  276. if (!mod) {
  277. printk(KERN_ERR "No module loaded addr=%lx\n",
  278. addr);
  279. return -EFAULT;
  280. }
  281. rec->arch.mod = mod;
  282. } else if (mod) {
  283. if (mod != rec->arch.mod) {
  284. printk(KERN_ERR
  285. "Record mod %p not equal to passed in mod %p\n",
  286. rec->arch.mod, mod);
  287. return -EINVAL;
  288. }
  289. /* nothing to do if mod == rec->arch.mod */
  290. } else
  291. mod = rec->arch.mod;
  292. return __ftrace_make_nop(mod, rec, addr);
  293. #else
  294. /* We should not get here without modules */
  295. return -EINVAL;
  296. #endif /* CONFIG_MODULES */
  297. }
  298. #ifdef CONFIG_MODULES
  299. #ifdef CONFIG_PPC64
  300. static int
  301. __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  302. {
  303. unsigned int op[2];
  304. unsigned long ip = rec->ip;
  305. /* read where this goes */
  306. if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
  307. return -EFAULT;
  308. /*
  309. * It should be pointing to two nops or
  310. * b +8; ld r2,40(r1)
  311. */
  312. if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
  313. ((op[0] != PPC_INST_NOP) || (op[1] != PPC_INST_NOP))) {
  314. printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
  315. return -EINVAL;
  316. }
  317. /* If we never set up a trampoline to ftrace_caller, then bail */
  318. if (!rec->arch.mod->arch.tramp) {
  319. printk(KERN_ERR "No ftrace trampoline\n");
  320. return -EINVAL;
  321. }
  322. /* create the branch to the trampoline */
  323. op[0] = create_branch((unsigned int *)ip,
  324. rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
  325. if (!op[0]) {
  326. printk(KERN_ERR "REL24 out of range!\n");
  327. return -EINVAL;
  328. }
  329. /* ld r2,40(r1) */
  330. op[1] = 0xe8410028;
  331. pr_devel("write to %lx\n", rec->ip);
  332. if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
  333. return -EPERM;
  334. flush_icache_range(ip, ip + 8);
  335. return 0;
  336. }
  337. #else
  338. static int
  339. __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  340. {
  341. unsigned int op;
  342. unsigned long ip = rec->ip;
  343. /* read where this goes */
  344. if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
  345. return -EFAULT;
  346. /* It should be pointing to a nop */
  347. if (op != PPC_INST_NOP) {
  348. printk(KERN_ERR "Expected NOP but have %x\n", op);
  349. return -EINVAL;
  350. }
  351. /* If we never set up a trampoline to ftrace_caller, then bail */
  352. if (!rec->arch.mod->arch.tramp) {
  353. printk(KERN_ERR "No ftrace trampoline\n");
  354. return -EINVAL;
  355. }
  356. /* create the branch to the trampoline */
  357. op = create_branch((unsigned int *)ip,
  358. rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
  359. if (!op) {
  360. printk(KERN_ERR "REL24 out of range!\n");
  361. return -EINVAL;
  362. }
  363. pr_devel("write to %lx\n", rec->ip);
  364. if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
  365. return -EPERM;
  366. flush_icache_range(ip, ip + 8);
  367. return 0;
  368. }
  369. #endif /* CONFIG_PPC64 */
  370. #endif /* CONFIG_MODULES */
  371. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  372. {
  373. unsigned long ip = rec->ip;
  374. unsigned int old, new;
  375. /*
  376. * If the calling address is more that 24 bits away,
  377. * then we had to use a trampoline to make the call.
  378. * Otherwise just update the call site.
  379. */
  380. if (test_24bit_addr(ip, addr)) {
  381. /* within range */
  382. old = ftrace_nop_replace();
  383. new = ftrace_call_replace(ip, addr, 1);
  384. return ftrace_modify_code(ip, old, new);
  385. }
  386. #ifdef CONFIG_MODULES
  387. /*
  388. * Out of range jumps are called from modules.
  389. * Being that we are converting from nop, it had better
  390. * already have a module defined.
  391. */
  392. if (!rec->arch.mod) {
  393. printk(KERN_ERR "No module loaded\n");
  394. return -EINVAL;
  395. }
  396. return __ftrace_make_call(rec, addr);
  397. #else
  398. /* We should not get here without modules */
  399. return -EINVAL;
  400. #endif /* CONFIG_MODULES */
  401. }
  402. int ftrace_update_ftrace_func(ftrace_func_t func)
  403. {
  404. unsigned long ip = (unsigned long)(&ftrace_call);
  405. unsigned int old, new;
  406. int ret;
  407. old = *(unsigned int *)&ftrace_call;
  408. new = ftrace_call_replace(ip, (unsigned long)func, 1);
  409. ret = ftrace_modify_code(ip, old, new);
  410. return ret;
  411. }
  412. int __init ftrace_dyn_arch_init(void *data)
  413. {
  414. /* caller expects data to be zero */
  415. unsigned long *p = data;
  416. *p = 0;
  417. return 0;
  418. }
  419. #endif /* CONFIG_DYNAMIC_FTRACE */
  420. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  421. #ifdef CONFIG_DYNAMIC_FTRACE
  422. extern void ftrace_graph_call(void);
  423. extern void ftrace_graph_stub(void);
  424. int ftrace_enable_ftrace_graph_caller(void)
  425. {
  426. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  427. unsigned long addr = (unsigned long)(&ftrace_graph_caller);
  428. unsigned long stub = (unsigned long)(&ftrace_graph_stub);
  429. unsigned int old, new;
  430. old = ftrace_call_replace(ip, stub, 0);
  431. new = ftrace_call_replace(ip, addr, 0);
  432. return ftrace_modify_code(ip, old, new);
  433. }
  434. int ftrace_disable_ftrace_graph_caller(void)
  435. {
  436. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  437. unsigned long addr = (unsigned long)(&ftrace_graph_caller);
  438. unsigned long stub = (unsigned long)(&ftrace_graph_stub);
  439. unsigned int old, new;
  440. old = ftrace_call_replace(ip, addr, 0);
  441. new = ftrace_call_replace(ip, stub, 0);
  442. return ftrace_modify_code(ip, old, new);
  443. }
  444. #endif /* CONFIG_DYNAMIC_FTRACE */
  445. #ifdef CONFIG_PPC64
  446. extern void mod_return_to_handler(void);
  447. #endif
  448. /*
  449. * Hook the return address and push it in the stack of return addrs
  450. * in current thread info.
  451. */
  452. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  453. {
  454. unsigned long old;
  455. int faulted;
  456. struct ftrace_graph_ent trace;
  457. unsigned long return_hooker = (unsigned long)&return_to_handler;
  458. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  459. return;
  460. #ifdef CONFIG_PPC64
  461. /* non core kernel code needs to save and restore the TOC */
  462. if (REGION_ID(self_addr) != KERNEL_REGION_ID)
  463. return_hooker = (unsigned long)&mod_return_to_handler;
  464. #endif
  465. return_hooker = GET_ADDR(return_hooker);
  466. /*
  467. * Protect against fault, even if it shouldn't
  468. * happen. This tool is too much intrusive to
  469. * ignore such a protection.
  470. */
  471. asm volatile(
  472. "1: " PPC_LL "%[old], 0(%[parent])\n"
  473. "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
  474. " li %[faulted], 0\n"
  475. "3:\n"
  476. ".section .fixup, \"ax\"\n"
  477. "4: li %[faulted], 1\n"
  478. " b 3b\n"
  479. ".previous\n"
  480. ".section __ex_table,\"a\"\n"
  481. PPC_LONG_ALIGN "\n"
  482. PPC_LONG "1b,4b\n"
  483. PPC_LONG "2b,4b\n"
  484. ".previous"
  485. : [old] "=&r" (old), [faulted] "=r" (faulted)
  486. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  487. : "memory"
  488. );
  489. if (unlikely(faulted)) {
  490. ftrace_graph_stop();
  491. WARN_ON(1);
  492. return;
  493. }
  494. if (ftrace_push_return_trace(old, self_addr, &trace.depth) == -EBUSY) {
  495. *parent = old;
  496. return;
  497. }
  498. trace.func = self_addr;
  499. /* Only trace if the calling function expects to */
  500. if (!ftrace_graph_entry(&trace)) {
  501. current->curr_ret_stack--;
  502. *parent = old;
  503. }
  504. }
  505. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */