ftrace.c 12 KB

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