ftrace.c 14 KB

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