ftrace-direct-modify.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/kthread.h>
  4. #include <linux/ftrace.h>
  5. #if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
  6. #include <asm/asm-offsets.h>
  7. #endif
  8. extern void my_direct_func1(void);
  9. extern void my_direct_func2(void);
  10. void my_direct_func1(void)
  11. {
  12. trace_printk("my direct func1\n");
  13. }
  14. void my_direct_func2(void)
  15. {
  16. trace_printk("my direct func2\n");
  17. }
  18. extern void my_tramp1(void *);
  19. extern void my_tramp2(void *);
  20. static unsigned long my_ip = (unsigned long)schedule;
  21. #ifdef CONFIG_RISCV
  22. #include <asm/asm.h>
  23. asm (
  24. " .pushsection .text, \"ax\", @progbits\n"
  25. " .type my_tramp1, @function\n"
  26. " .globl my_tramp1\n"
  27. " my_tramp1:\n"
  28. " addi sp,sp,-2*"SZREG"\n"
  29. " "REG_S" t0,0*"SZREG"(sp)\n"
  30. " "REG_S" ra,1*"SZREG"(sp)\n"
  31. " call my_direct_func1\n"
  32. " "REG_L" t0,0*"SZREG"(sp)\n"
  33. " "REG_L" ra,1*"SZREG"(sp)\n"
  34. " addi sp,sp,2*"SZREG"\n"
  35. " jr t0\n"
  36. " .size my_tramp1, .-my_tramp1\n"
  37. " .type my_tramp2, @function\n"
  38. " .globl my_tramp2\n"
  39. " my_tramp2:\n"
  40. " addi sp,sp,-2*"SZREG"\n"
  41. " "REG_S" t0,0*"SZREG"(sp)\n"
  42. " "REG_S" ra,1*"SZREG"(sp)\n"
  43. " call my_direct_func2\n"
  44. " "REG_L" t0,0*"SZREG"(sp)\n"
  45. " "REG_L" ra,1*"SZREG"(sp)\n"
  46. " addi sp,sp,2*"SZREG"\n"
  47. " jr t0\n"
  48. " .size my_tramp2, .-my_tramp2\n"
  49. " .popsection\n"
  50. );
  51. #endif /* CONFIG_RISCV */
  52. #ifdef CONFIG_X86_64
  53. #include <asm/ibt.h>
  54. #include <asm/nospec-branch.h>
  55. asm (
  56. " .pushsection .text, \"ax\", @progbits\n"
  57. " .type my_tramp1, @function\n"
  58. " .globl my_tramp1\n"
  59. " my_tramp1:"
  60. ASM_ENDBR
  61. " pushq %rbp\n"
  62. " movq %rsp, %rbp\n"
  63. CALL_DEPTH_ACCOUNT
  64. " call my_direct_func1\n"
  65. " leave\n"
  66. ASM_RET
  67. " .size my_tramp1, .-my_tramp1\n"
  68. " .type my_tramp2, @function\n"
  69. " .globl my_tramp2\n"
  70. " my_tramp2:"
  71. ASM_ENDBR
  72. " pushq %rbp\n"
  73. " movq %rsp, %rbp\n"
  74. CALL_DEPTH_ACCOUNT
  75. " call my_direct_func2\n"
  76. " leave\n"
  77. ASM_RET
  78. " .size my_tramp2, .-my_tramp2\n"
  79. " .popsection\n"
  80. );
  81. #endif /* CONFIG_X86_64 */
  82. #ifdef CONFIG_S390
  83. asm (
  84. " .pushsection .text, \"ax\", @progbits\n"
  85. " .type my_tramp1, @function\n"
  86. " .globl my_tramp1\n"
  87. " my_tramp1:"
  88. " lgr %r1,%r15\n"
  89. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  90. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  91. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  92. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  93. " brasl %r14,my_direct_func1\n"
  94. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  95. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  96. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  97. " lgr %r1,%r0\n"
  98. " br %r1\n"
  99. " .size my_tramp1, .-my_tramp1\n"
  100. " .type my_tramp2, @function\n"
  101. " .globl my_tramp2\n"
  102. " my_tramp2:"
  103. " lgr %r1,%r15\n"
  104. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  105. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  106. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  107. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  108. " brasl %r14,my_direct_func2\n"
  109. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  110. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  111. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  112. " lgr %r1,%r0\n"
  113. " br %r1\n"
  114. " .size my_tramp2, .-my_tramp2\n"
  115. " .popsection\n"
  116. );
  117. #endif /* CONFIG_S390 */
  118. #ifdef CONFIG_ARM64
  119. asm (
  120. " .pushsection .text, \"ax\", @progbits\n"
  121. " .type my_tramp1, @function\n"
  122. " .globl my_tramp1\n"
  123. " my_tramp1:"
  124. " hint 34\n" // bti c
  125. " sub sp, sp, #16\n"
  126. " stp x9, x30, [sp]\n"
  127. " bl my_direct_func1\n"
  128. " ldp x30, x9, [sp]\n"
  129. " add sp, sp, #16\n"
  130. " ret x9\n"
  131. " .size my_tramp1, .-my_tramp1\n"
  132. " .type my_tramp2, @function\n"
  133. " .globl my_tramp2\n"
  134. " my_tramp2:"
  135. " hint 34\n" // bti c
  136. " sub sp, sp, #16\n"
  137. " stp x9, x30, [sp]\n"
  138. " bl my_direct_func2\n"
  139. " ldp x30, x9, [sp]\n"
  140. " add sp, sp, #16\n"
  141. " ret x9\n"
  142. " .size my_tramp2, .-my_tramp2\n"
  143. " .popsection\n"
  144. );
  145. #endif /* CONFIG_ARM64 */
  146. #ifdef CONFIG_LOONGARCH
  147. asm (
  148. " .pushsection .text, \"ax\", @progbits\n"
  149. " .type my_tramp1, @function\n"
  150. " .globl my_tramp1\n"
  151. " my_tramp1:\n"
  152. " addi.d $sp, $sp, -16\n"
  153. " st.d $t0, $sp, 0\n"
  154. " st.d $ra, $sp, 8\n"
  155. " bl my_direct_func1\n"
  156. " ld.d $ra, $sp, 0\n"
  157. " ld.d $t0, $sp, 8\n"
  158. " addi.d $sp, $sp, 16\n"
  159. " jr $t0\n"
  160. " .size my_tramp1, .-my_tramp1\n"
  161. " .type my_tramp2, @function\n"
  162. " .globl my_tramp2\n"
  163. " my_tramp2:\n"
  164. " addi.d $sp, $sp, -16\n"
  165. " st.d $t0, $sp, 0\n"
  166. " st.d $ra, $sp, 8\n"
  167. " bl my_direct_func2\n"
  168. " ld.d $ra, $sp, 0\n"
  169. " ld.d $t0, $sp, 8\n"
  170. " addi.d $sp, $sp, 16\n"
  171. " jr $t0\n"
  172. " .size my_tramp2, .-my_tramp2\n"
  173. " .popsection\n"
  174. );
  175. #endif /* CONFIG_LOONGARCH */
  176. #ifdef CONFIG_PPC
  177. #include <asm/ppc_asm.h>
  178. #ifdef CONFIG_PPC64
  179. #define STACK_FRAME_SIZE 48
  180. #else
  181. #define STACK_FRAME_SIZE 24
  182. #endif
  183. #if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
  184. #define PPC64_TOC_SAVE_AND_UPDATE \
  185. " std 2, 24(1)\n" \
  186. " bcl 20, 31, 1f\n" \
  187. " 1: mflr 12\n" \
  188. " ld 2, (99f - 1b)(12)\n"
  189. #define PPC64_TOC_RESTORE \
  190. " ld 2, 24(1)\n"
  191. #define PPC64_TOC \
  192. " 99: .quad .TOC.@tocbase\n"
  193. #else
  194. #define PPC64_TOC_SAVE_AND_UPDATE ""
  195. #define PPC64_TOC_RESTORE ""
  196. #define PPC64_TOC ""
  197. #endif
  198. #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
  199. #define PPC_FTRACE_RESTORE_LR \
  200. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
  201. " mtlr 0\n"
  202. #define PPC_FTRACE_RET \
  203. " blr\n"
  204. #else
  205. #define PPC_FTRACE_RESTORE_LR \
  206. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
  207. " mtctr 0\n"
  208. #define PPC_FTRACE_RET \
  209. " mtlr 0\n" \
  210. " bctr\n"
  211. #endif
  212. asm (
  213. " .pushsection .text, \"ax\", @progbits\n"
  214. " .type my_tramp1, @function\n"
  215. " .globl my_tramp1\n"
  216. " my_tramp1:\n"
  217. PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  218. PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
  219. " mflr 0\n"
  220. PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  221. PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
  222. PPC64_TOC_SAVE_AND_UPDATE
  223. " bl my_direct_func1\n"
  224. PPC64_TOC_RESTORE
  225. " addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
  226. PPC_FTRACE_RESTORE_LR
  227. " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
  228. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  229. PPC_FTRACE_RET
  230. " .size my_tramp1, .-my_tramp1\n"
  231. " .type my_tramp2, @function\n"
  232. " .globl my_tramp2\n"
  233. " my_tramp2:\n"
  234. PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  235. PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
  236. " mflr 0\n"
  237. PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  238. PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
  239. PPC64_TOC_SAVE_AND_UPDATE
  240. " bl my_direct_func2\n"
  241. PPC64_TOC_RESTORE
  242. " addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
  243. PPC_FTRACE_RESTORE_LR
  244. " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
  245. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  246. PPC_FTRACE_RET
  247. PPC64_TOC
  248. " .size my_tramp2, .-my_tramp2\n"
  249. " .popsection\n"
  250. );
  251. #endif /* CONFIG_PPC */
  252. static struct ftrace_ops direct;
  253. static unsigned long my_tramp = (unsigned long)my_tramp1;
  254. static unsigned long tramps[2] = {
  255. (unsigned long)my_tramp1,
  256. (unsigned long)my_tramp2,
  257. };
  258. static int simple_thread(void *arg)
  259. {
  260. static int t;
  261. int ret = 0;
  262. while (!kthread_should_stop()) {
  263. set_current_state(TASK_INTERRUPTIBLE);
  264. schedule_timeout(2 * HZ);
  265. if (ret)
  266. continue;
  267. t ^= 1;
  268. ret = modify_ftrace_direct(&direct, tramps[t]);
  269. if (!ret)
  270. my_tramp = tramps[t];
  271. WARN_ON_ONCE(ret);
  272. }
  273. return 0;
  274. }
  275. static struct task_struct *simple_tsk;
  276. static int __init ftrace_direct_init(void)
  277. {
  278. int ret;
  279. ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
  280. ret = register_ftrace_direct(&direct, my_tramp);
  281. if (!ret)
  282. simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
  283. return ret;
  284. }
  285. static void __exit ftrace_direct_exit(void)
  286. {
  287. kthread_stop(simple_tsk);
  288. unregister_ftrace_direct(&direct, my_tramp, true);
  289. }
  290. module_init(ftrace_direct_init);
  291. module_exit(ftrace_direct_exit);
  292. MODULE_AUTHOR("Steven Rostedt");
  293. MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
  294. MODULE_LICENSE("GPL");