ftrace-direct-modify.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/kthread.h>
  4. #include <linux/ftrace.h>
  5. #ifndef CONFIG_ARM64
  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. " .size my_tramp1, .-my_tramp1\n"
  67. ASM_RET
  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 $t0, $sp, 0\n"
  157. " ld.d $ra, $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 $t0, $sp, 0\n"
  169. " ld.d $ra, $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. static struct ftrace_ops direct;
  177. static unsigned long my_tramp = (unsigned long)my_tramp1;
  178. static unsigned long tramps[2] = {
  179. (unsigned long)my_tramp1,
  180. (unsigned long)my_tramp2,
  181. };
  182. static int simple_thread(void *arg)
  183. {
  184. static int t;
  185. int ret = 0;
  186. while (!kthread_should_stop()) {
  187. set_current_state(TASK_INTERRUPTIBLE);
  188. schedule_timeout(2 * HZ);
  189. if (ret)
  190. continue;
  191. t ^= 1;
  192. ret = modify_ftrace_direct(&direct, tramps[t]);
  193. if (!ret)
  194. my_tramp = tramps[t];
  195. WARN_ON_ONCE(ret);
  196. }
  197. return 0;
  198. }
  199. static struct task_struct *simple_tsk;
  200. static int __init ftrace_direct_init(void)
  201. {
  202. int ret;
  203. ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
  204. ret = register_ftrace_direct(&direct, my_tramp);
  205. if (!ret)
  206. simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
  207. return ret;
  208. }
  209. static void __exit ftrace_direct_exit(void)
  210. {
  211. kthread_stop(simple_tsk);
  212. unregister_ftrace_direct(&direct, my_tramp, true);
  213. }
  214. module_init(ftrace_direct_init);
  215. module_exit(ftrace_direct_exit);
  216. MODULE_AUTHOR("Steven Rostedt");
  217. MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
  218. MODULE_LICENSE("GPL");