ftrace.c 15 KB

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