ftrace-direct-multi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef CONFIG_ARM64
  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 $t0, $sp, 8\n"
  116. " ld.d $ra, $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. static struct ftrace_ops direct;
  124. static int __init ftrace_direct_multi_init(void)
  125. {
  126. ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
  127. ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
  128. return register_ftrace_direct(&direct, (unsigned long) my_tramp);
  129. }
  130. static void __exit ftrace_direct_multi_exit(void)
  131. {
  132. unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true);
  133. }
  134. module_init(ftrace_direct_multi_init);
  135. module_exit(ftrace_direct_multi_exit);
  136. MODULE_AUTHOR("Jiri Olsa");
  137. MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
  138. MODULE_LICENSE("GPL");