ftrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. */
  9. #include <linux/spinlock.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/module.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/percpu.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/code-patching.h>
  19. #include <asm/ftrace.h>
  20. static unsigned int ftrace_nop = PPC_NOP_INSTR;
  21. #ifdef CONFIG_PPC32
  22. # define GET_ADDR(addr) addr
  23. #else
  24. /* PowerPC64's functions are data that points to the functions */
  25. # define GET_ADDR(addr) (*(unsigned long *)addr)
  26. #endif
  27. static unsigned int ftrace_calc_offset(long ip, long addr)
  28. {
  29. return (int)(addr - ip);
  30. }
  31. static unsigned char *ftrace_nop_replace(void)
  32. {
  33. return (char *)&ftrace_nop;
  34. }
  35. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  36. {
  37. static unsigned int op;
  38. /*
  39. * It would be nice to just use create_function_call, but that will
  40. * update the code itself. Here we need to just return the
  41. * instruction that is going to be modified, without modifying the
  42. * code.
  43. */
  44. addr = GET_ADDR(addr);
  45. /* Set to "bl addr" */
  46. op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
  47. /*
  48. * No locking needed, this must be called via kstop_machine
  49. * which in essence is like running on a uniprocessor machine.
  50. */
  51. return (unsigned char *)&op;
  52. }
  53. #ifdef CONFIG_PPC64
  54. # define _ASM_ALIGN " .align 3 "
  55. # define _ASM_PTR " .llong "
  56. #else
  57. # define _ASM_ALIGN " .align 2 "
  58. # define _ASM_PTR " .long "
  59. #endif
  60. static int
  61. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  62. unsigned char *new_code)
  63. {
  64. unsigned char replaced[MCOUNT_INSN_SIZE];
  65. /*
  66. * Note: Due to modules and __init, code can
  67. * disappear and change, we need to protect against faulting
  68. * as well as code changing. We do this by using the
  69. * probe_kernel_* functions.
  70. *
  71. * No real locking needed, this code is run through
  72. * kstop_machine, or before SMP starts.
  73. */
  74. /* read the text we want to modify */
  75. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  76. return -EFAULT;
  77. /* Make sure it is what we expect it to be */
  78. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  79. return -EINVAL;
  80. /* replace the text with the new text */
  81. if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
  82. return -EPERM;
  83. flush_icache_range(ip, ip + 8);
  84. return 0;
  85. }
  86. /*
  87. * Helper functions that are the same for both PPC64 and PPC32.
  88. */
  89. static int test_24bit_addr(unsigned long ip, unsigned long addr)
  90. {
  91. /* use the create_branch to verify that this offset can be branched */
  92. return create_branch((unsigned int *)ip, addr, 0);
  93. }
  94. static int is_bl_op(unsigned int op)
  95. {
  96. return (op & 0xfc000003) == 0x48000001;
  97. }
  98. static unsigned long find_bl_target(unsigned long ip, unsigned int op)
  99. {
  100. static int offset;
  101. offset = (op & 0x03fffffc);
  102. /* make it signed */
  103. if (offset & 0x02000000)
  104. offset |= 0xfe000000;
  105. return ip + (long)offset;
  106. }
  107. #ifdef CONFIG_PPC64
  108. static int
  109. __ftrace_make_nop(struct module *mod,
  110. struct dyn_ftrace *rec, unsigned long addr)
  111. {
  112. unsigned int op;
  113. unsigned int jmp[5];
  114. unsigned long ptr;
  115. unsigned long ip = rec->ip;
  116. unsigned long tramp;
  117. int offset;
  118. /* read where this goes */
  119. if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
  120. return -EFAULT;
  121. /* Make sure that that this is still a 24bit jump */
  122. if (!is_bl_op(op)) {
  123. printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
  124. return -EINVAL;
  125. }
  126. /* lets find where the pointer goes */
  127. tramp = find_bl_target(ip, op);
  128. /*
  129. * On PPC64 the trampoline looks like:
  130. * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
  131. * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
  132. * Where the bytes 2,3,6 and 7 make up the 32bit offset
  133. * to the TOC that holds the pointer.
  134. * to jump to.
  135. * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
  136. * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
  137. * The actually address is 32 bytes from the offset
  138. * into the TOC.
  139. * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
  140. */
  141. pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
  142. /* Find where the trampoline jumps to */
  143. if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
  144. printk(KERN_ERR "Failed to read %lx\n", tramp);
  145. return -EFAULT;
  146. }
  147. pr_debug(" %08x %08x", jmp[0], jmp[1]);
  148. /* verify that this is what we expect it to be */
  149. if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
  150. ((jmp[1] & 0xffff0000) != 0x398c0000) ||
  151. (jmp[2] != 0xf8410028) ||
  152. (jmp[3] != 0xe96c0020) ||
  153. (jmp[4] != 0xe84c0028)) {
  154. printk(KERN_ERR "Not a trampoline\n");
  155. return -EINVAL;
  156. }
  157. /* The bottom half is signed extended */
  158. offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
  159. (int)((short)jmp[1]);
  160. pr_debug(" %x ", offset);
  161. /* get the address this jumps too */
  162. tramp = mod->arch.toc + offset + 32;
  163. pr_debug("toc: %lx", tramp);
  164. if (probe_kernel_read(jmp, (void *)tramp, 8)) {
  165. printk(KERN_ERR "Failed to read %lx\n", tramp);
  166. return -EFAULT;
  167. }
  168. pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
  169. ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
  170. /* This should match what was called */
  171. if (ptr != GET_ADDR(addr)) {
  172. printk(KERN_ERR "addr does not match %lx\n", ptr);
  173. return -EINVAL;
  174. }
  175. /*
  176. * We want to nop the line, but the next line is
  177. * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
  178. * This needs to be turned to a nop too.
  179. */
  180. if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
  181. return -EFAULT;
  182. if (op != 0xe8410028) {
  183. printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
  184. return -EINVAL;
  185. }
  186. /*
  187. * Milton Miller pointed out that we can not blindly do nops.
  188. * If a task was preempted when calling a trace function,
  189. * the nops will remove the way to restore the TOC in r2
  190. * and the r2 TOC will get corrupted.
  191. */
  192. /*
  193. * Replace:
  194. * bl <tramp> <==== will be replaced with "b 1f"
  195. * ld r2,40(r1)
  196. * 1:
  197. */
  198. op = 0x48000008; /* b +8 */
  199. if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
  200. return -EPERM;
  201. flush_icache_range(ip, ip + 8);
  202. return 0;
  203. }
  204. #else /* !PPC64 */
  205. static int
  206. __ftrace_make_nop(struct module *mod,
  207. struct dyn_ftrace *rec, unsigned long addr)
  208. {
  209. unsigned int op;
  210. unsigned int jmp[4];
  211. unsigned long ip = rec->ip;
  212. unsigned long tramp;
  213. if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
  214. return -EFAULT;
  215. /* Make sure that that this is still a 24bit jump */
  216. if (!is_bl_op(op)) {
  217. printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
  218. return -EINVAL;
  219. }
  220. /* lets find where the pointer goes */
  221. tramp = find_bl_target(ip, op);
  222. /*
  223. * On PPC32 the trampoline looks like:
  224. * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
  225. * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
  226. * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
  227. * 0x4e, 0x80, 0x04, 0x20 bctr
  228. */
  229. pr_debug("ip:%lx jumps to %lx", ip, tramp);
  230. /* Find where the trampoline jumps to */
  231. if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
  232. printk(KERN_ERR "Failed to read %lx\n", tramp);
  233. return -EFAULT;
  234. }
  235. pr_debug(" %08x %08x ", jmp[0], jmp[1]);
  236. /* verify that this is what we expect it to be */
  237. if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
  238. ((jmp[1] & 0xffff0000) != 0x396b0000) ||
  239. (jmp[2] != 0x7d6903a6) ||
  240. (jmp[3] != 0x4e800420)) {
  241. printk(KERN_ERR "Not a trampoline\n");
  242. return -EINVAL;
  243. }
  244. tramp = (jmp[1] & 0xffff) |
  245. ((jmp[0] & 0xffff) << 16);
  246. if (tramp & 0x8000)
  247. tramp -= 0x10000;
  248. pr_debug(" %x ", tramp);
  249. if (tramp != addr) {
  250. printk(KERN_ERR
  251. "Trampoline location %08lx does not match addr\n",
  252. tramp);
  253. return -EINVAL;
  254. }
  255. op = PPC_NOP_INSTR;
  256. if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
  257. return -EPERM;
  258. flush_icache_range(ip, ip + 8);
  259. return 0;
  260. }
  261. #endif /* PPC64 */
  262. int ftrace_make_nop(struct module *mod,
  263. struct dyn_ftrace *rec, unsigned long addr)
  264. {
  265. unsigned char *old, *new;
  266. unsigned long ip = rec->ip;
  267. /*
  268. * If the calling address is more that 24 bits away,
  269. * then we had to use a trampoline to make the call.
  270. * Otherwise just update the call site.
  271. */
  272. if (test_24bit_addr(ip, addr)) {
  273. /* within range */
  274. old = ftrace_call_replace(ip, addr);
  275. new = ftrace_nop_replace();
  276. return ftrace_modify_code(ip, old, new);
  277. }
  278. /*
  279. * Out of range jumps are called from modules.
  280. * We should either already have a pointer to the module
  281. * or it has been passed in.
  282. */
  283. if (!rec->arch.mod) {
  284. if (!mod) {
  285. printk(KERN_ERR "No module loaded addr=%lx\n",
  286. addr);
  287. return -EFAULT;
  288. }
  289. rec->arch.mod = mod;
  290. } else if (mod) {
  291. if (mod != rec->arch.mod) {
  292. printk(KERN_ERR
  293. "Record mod %p not equal to passed in mod %p\n",
  294. rec->arch.mod, mod);
  295. return -EINVAL;
  296. }
  297. /* nothing to do if mod == rec->arch.mod */
  298. } else
  299. mod = rec->arch.mod;
  300. return __ftrace_make_nop(mod, rec, addr);
  301. }
  302. #ifdef CONFIG_PPC64
  303. static int
  304. __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  305. {
  306. unsigned int op[2];
  307. unsigned long ip = rec->ip;
  308. /* read where this goes */
  309. if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
  310. return -EFAULT;
  311. /*
  312. * It should be pointing to two nops or
  313. * b +8; ld r2,40(r1)
  314. */
  315. if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
  316. ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
  317. printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
  318. return -EINVAL;
  319. }
  320. /* If we never set up a trampoline to ftrace_caller, then bail */
  321. if (!rec->arch.mod->arch.tramp) {
  322. printk(KERN_ERR "No ftrace trampoline\n");
  323. return -EINVAL;
  324. }
  325. /* create the branch to the trampoline */
  326. op[0] = create_branch((unsigned int *)ip,
  327. rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
  328. if (!op[0]) {
  329. printk(KERN_ERR "REL24 out of range!\n");
  330. return -EINVAL;
  331. }
  332. /* ld r2,40(r1) */
  333. op[1] = 0xe8410028;
  334. pr_debug("write to %lx\n", rec->ip);
  335. if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
  336. return -EPERM;
  337. flush_icache_range(ip, ip + 8);
  338. return 0;
  339. }
  340. #else
  341. static int
  342. __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  343. {
  344. unsigned int op;
  345. unsigned long ip = rec->ip;
  346. /* read where this goes */
  347. if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
  348. return -EFAULT;
  349. /* It should be pointing to a nop */
  350. if (op != PPC_NOP_INSTR) {
  351. printk(KERN_ERR "Expected NOP but have %x\n", op);
  352. return -EINVAL;
  353. }
  354. /* If we never set up a trampoline to ftrace_caller, then bail */
  355. if (!rec->arch.mod->arch.tramp) {
  356. printk(KERN_ERR "No ftrace trampoline\n");
  357. return -EINVAL;
  358. }
  359. /* create the branch to the trampoline */
  360. op = create_branch((unsigned int *)ip,
  361. rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
  362. if (!op) {
  363. printk(KERN_ERR "REL24 out of range!\n");
  364. return -EINVAL;
  365. }
  366. pr_debug("write to %lx\n", rec->ip);
  367. if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
  368. return -EPERM;
  369. flush_icache_range(ip, ip + 8);
  370. return 0;
  371. }
  372. #endif /* CONFIG_PPC64 */
  373. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  374. {
  375. unsigned char *old, *new;
  376. unsigned long ip = rec->ip;
  377. /*
  378. * If the calling address is more that 24 bits away,
  379. * then we had to use a trampoline to make the call.
  380. * Otherwise just update the call site.
  381. */
  382. if (test_24bit_addr(ip, addr)) {
  383. /* within range */
  384. old = ftrace_nop_replace();
  385. new = ftrace_call_replace(ip, addr);
  386. return ftrace_modify_code(ip, old, new);
  387. }
  388. /*
  389. * Out of range jumps are called from modules.
  390. * Being that we are converting from nop, it had better
  391. * already have a module defined.
  392. */
  393. if (!rec->arch.mod) {
  394. printk(KERN_ERR "No module loaded\n");
  395. return -EINVAL;
  396. }
  397. return __ftrace_make_call(rec, addr);
  398. }
  399. int ftrace_update_ftrace_func(ftrace_func_t func)
  400. {
  401. unsigned long ip = (unsigned long)(&ftrace_call);
  402. unsigned char old[MCOUNT_INSN_SIZE], *new;
  403. int ret;
  404. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  405. new = ftrace_call_replace(ip, (unsigned long)func);
  406. ret = ftrace_modify_code(ip, old, new);
  407. return ret;
  408. }
  409. int __init ftrace_dyn_arch_init(void *data)
  410. {
  411. /* caller expects data to be zero */
  412. unsigned long *p = data;
  413. *p = 0;
  414. return 0;
  415. }