ftrace-direct-too.c 6.2 KB

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