ftrace-direct-too.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef CONFIG_ARM64
  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 $t0, $sp, 24\n"
  128. " ld.d $ra, $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. static struct ftrace_ops direct;
  136. static int __init ftrace_direct_init(void)
  137. {
  138. ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
  139. return register_ftrace_direct(&direct, (unsigned long) my_tramp);
  140. }
  141. static void __exit ftrace_direct_exit(void)
  142. {
  143. unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
  144. }
  145. module_init(ftrace_direct_init);
  146. module_exit(ftrace_direct_exit);
  147. MODULE_AUTHOR("Steven Rostedt");
  148. MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
  149. MODULE_LICENSE("GPL");