ftrace.c 14 KB

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