ftrace.c 15 KB

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