ftrace.c 14 KB

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