ftrace-direct-multi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/mm.h> /* for handle_mm_fault() */
  4. #include <linux/ftrace.h>
  5. #include <linux/sched/stat.h>
  6. #if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
  7. #include <asm/asm-offsets.h>
  8. #endif
  9. extern void my_direct_func(unsigned long ip);
  10. void my_direct_func(unsigned long ip)
  11. {
  12. trace_printk("ip %lx\n", ip);
  13. }
  14. extern void my_tramp(void *);
  15. #ifdef CONFIG_RISCV
  16. #include <asm/asm.h>
  17. asm (
  18. " .pushsection .text, \"ax\", @progbits\n"
  19. " .type my_tramp, @function\n"
  20. " .globl my_tramp\n"
  21. " my_tramp:\n"
  22. " addi sp,sp,-3*"SZREG"\n"
  23. " "REG_S" a0,0*"SZREG"(sp)\n"
  24. " "REG_S" t0,1*"SZREG"(sp)\n"
  25. " "REG_S" ra,2*"SZREG"(sp)\n"
  26. " mv a0,t0\n"
  27. " call my_direct_func\n"
  28. " "REG_L" a0,0*"SZREG"(sp)\n"
  29. " "REG_L" t0,1*"SZREG"(sp)\n"
  30. " "REG_L" ra,2*"SZREG"(sp)\n"
  31. " addi sp,sp,3*"SZREG"\n"
  32. " jr t0\n"
  33. " .size my_tramp, .-my_tramp\n"
  34. " .popsection\n"
  35. );
  36. #endif /* CONFIG_RISCV */
  37. #ifdef CONFIG_X86_64
  38. #include <asm/ibt.h>
  39. #include <asm/nospec-branch.h>
  40. asm (
  41. " .pushsection .text, \"ax\", @progbits\n"
  42. " .type my_tramp, @function\n"
  43. " .globl my_tramp\n"
  44. " my_tramp:"
  45. ASM_ENDBR
  46. " pushq %rbp\n"
  47. " movq %rsp, %rbp\n"
  48. CALL_DEPTH_ACCOUNT
  49. " pushq %rdi\n"
  50. " movq 8(%rbp), %rdi\n"
  51. " call my_direct_func\n"
  52. " popq %rdi\n"
  53. " leave\n"
  54. ASM_RET
  55. " .size my_tramp, .-my_tramp\n"
  56. " .popsection\n"
  57. );
  58. #endif /* CONFIG_X86_64 */
  59. #ifdef CONFIG_S390
  60. asm (
  61. " .pushsection .text, \"ax\", @progbits\n"
  62. " .type my_tramp, @function\n"
  63. " .globl my_tramp\n"
  64. " my_tramp:"
  65. " lgr %r1,%r15\n"
  66. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  67. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  68. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  69. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  70. " lgr %r2,%r0\n"
  71. " brasl %r14,my_direct_func\n"
  72. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  73. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  74. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  75. " lgr %r1,%r0\n"
  76. " br %r1\n"
  77. " .size my_tramp, .-my_tramp\n"
  78. " .popsection\n"
  79. );
  80. #endif /* CONFIG_S390 */
  81. #ifdef CONFIG_ARM64
  82. asm (
  83. " .pushsection .text, \"ax\", @progbits\n"
  84. " .type my_tramp, @function\n"
  85. " .globl my_tramp\n"
  86. " my_tramp:"
  87. " hint 34\n" // bti c
  88. " sub sp, sp, #32\n"
  89. " stp x9, x30, [sp]\n"
  90. " str x0, [sp, #16]\n"
  91. " mov x0, x30\n"
  92. " bl my_direct_func\n"
  93. " ldp x30, x9, [sp]\n"
  94. " ldr x0, [sp, #16]\n"
  95. " add sp, sp, #32\n"
  96. " ret x9\n"
  97. " .size my_tramp, .-my_tramp\n"
  98. " .popsection\n"
  99. );
  100. #endif /* CONFIG_ARM64 */
  101. #ifdef CONFIG_LOONGARCH
  102. #include <asm/asm.h>
  103. asm (
  104. " .pushsection .text, \"ax\", @progbits\n"
  105. " .type my_tramp, @function\n"
  106. " .globl my_tramp\n"
  107. " my_tramp:\n"
  108. " addi.d $sp, $sp, -32\n"
  109. " st.d $a0, $sp, 0\n"
  110. " st.d $t0, $sp, 8\n"
  111. " st.d $ra, $sp, 16\n"
  112. " move $a0, $t0\n"
  113. " bl my_direct_func\n"
  114. " ld.d $a0, $sp, 0\n"
  115. " ld.d $ra, $sp, 8\n"
  116. " ld.d $t0, $sp, 16\n"
  117. " addi.d $sp, $sp, 32\n"
  118. " jr $t0\n"
  119. " .size my_tramp, .-my_tramp\n"
  120. " .popsection\n"
  121. );
  122. #endif /* CONFIG_LOONGARCH */
  123. #ifdef CONFIG_PPC
  124. #include <asm/ppc_asm.h>
  125. #ifdef CONFIG_PPC64
  126. #define STACK_FRAME_SIZE 48
  127. #else
  128. #define STACK_FRAME_SIZE 24
  129. #endif
  130. #if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
  131. #define PPC64_TOC_SAVE_AND_UPDATE \
  132. " std 2, 24(1)\n" \
  133. " bcl 20, 31, 1f\n" \
  134. " 1: mflr 12\n" \
  135. " ld 2, (99f - 1b)(12)\n"
  136. #define PPC64_TOC_RESTORE \
  137. " ld 2, 24(1)\n"
  138. #define PPC64_TOC \
  139. " 99: .quad .TOC.@tocbase\n"
  140. #else
  141. #define PPC64_TOC_SAVE_AND_UPDATE ""
  142. #define PPC64_TOC_RESTORE ""
  143. #define PPC64_TOC ""
  144. #endif
  145. #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
  146. #define PPC_FTRACE_RESTORE_LR \
  147. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
  148. " mtlr 0\n"
  149. #define PPC_FTRACE_RET \
  150. " blr\n"
  151. #define PPC_FTRACE_RECOVER_IP \
  152. " lwz 8, 4(3)\n" \
  153. " li 9, 6\n" \
  154. " slw 8, 8, 9\n" \
  155. " sraw 8, 8, 9\n" \
  156. " add 3, 3, 8\n" \
  157. " addi 3, 3, 4\n"
  158. #else
  159. #define PPC_FTRACE_RESTORE_LR \
  160. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
  161. " mtctr 0\n"
  162. #define PPC_FTRACE_RET \
  163. " mtlr 0\n" \
  164. " bctr\n"
  165. #define PPC_FTRACE_RECOVER_IP ""
  166. #endif
  167. asm (
  168. " .pushsection .text, \"ax\", @progbits\n"
  169. " .type my_tramp, @function\n"
  170. " .globl my_tramp\n"
  171. " my_tramp:\n"
  172. PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  173. PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
  174. " mflr 0\n"
  175. PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  176. PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
  177. PPC64_TOC_SAVE_AND_UPDATE
  178. PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
  179. " mr 3, 0\n"
  180. PPC_FTRACE_RECOVER_IP
  181. " bl my_direct_func\n"
  182. PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
  183. PPC64_TOC_RESTORE
  184. " addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
  185. PPC_FTRACE_RESTORE_LR
  186. " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
  187. PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
  188. PPC_FTRACE_RET
  189. PPC64_TOC
  190. " .size my_tramp, .-my_tramp\n"
  191. " .popsection\n"
  192. );
  193. #endif /* CONFIG_PPC */
  194. static struct ftrace_ops direct;
  195. static int __init ftrace_direct_multi_init(void)
  196. {
  197. ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
  198. ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
  199. return register_ftrace_direct(&direct, (unsigned long) my_tramp);
  200. }
  201. static void __exit ftrace_direct_multi_exit(void)
  202. {
  203. unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true);
  204. }
  205. module_init(ftrace_direct_multi_init);
  206. module_exit(ftrace_direct_multi_exit);
  207. MODULE_AUTHOR("Jiri Olsa");
  208. MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
  209. MODULE_LICENSE("GPL");