ftrace-direct.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/sched.h> /* for wake_up_process() */
  4. #include <linux/ftrace.h>
  5. #ifndef CONFIG_ARM64
  6. #include <asm/asm-offsets.h>
  7. #endif
  8. extern void my_direct_func(struct task_struct *p);
  9. void my_direct_func(struct task_struct *p)
  10. {
  11. trace_printk("waking up %s-%d\n", p->comm, p->pid);
  12. }
  13. extern void my_tramp(void *);
  14. #ifdef CONFIG_RISCV
  15. #include <asm/asm.h>
  16. asm (
  17. " .pushsection .text, \"ax\", @progbits\n"
  18. " .type my_tramp, @function\n"
  19. " .globl my_tramp\n"
  20. " my_tramp:\n"
  21. " addi sp,sp,-3*"SZREG"\n"
  22. " "REG_S" a0,0*"SZREG"(sp)\n"
  23. " "REG_S" t0,1*"SZREG"(sp)\n"
  24. " "REG_S" ra,2*"SZREG"(sp)\n"
  25. " call my_direct_func\n"
  26. " "REG_L" a0,0*"SZREG"(sp)\n"
  27. " "REG_L" t0,1*"SZREG"(sp)\n"
  28. " "REG_L" ra,2*"SZREG"(sp)\n"
  29. " addi sp,sp,3*"SZREG"\n"
  30. " jr t0\n"
  31. " .size my_tramp, .-my_tramp\n"
  32. " .popsection\n"
  33. );
  34. #endif /* CONFIG_RISCV */
  35. #ifdef CONFIG_X86_64
  36. #include <asm/ibt.h>
  37. #include <asm/nospec-branch.h>
  38. asm (
  39. " .pushsection .text, \"ax\", @progbits\n"
  40. " .type my_tramp, @function\n"
  41. " .globl my_tramp\n"
  42. " my_tramp:"
  43. ASM_ENDBR
  44. " pushq %rbp\n"
  45. " movq %rsp, %rbp\n"
  46. CALL_DEPTH_ACCOUNT
  47. " pushq %rdi\n"
  48. " call my_direct_func\n"
  49. " popq %rdi\n"
  50. " leave\n"
  51. ASM_RET
  52. " .size my_tramp, .-my_tramp\n"
  53. " .popsection\n"
  54. );
  55. #endif /* CONFIG_X86_64 */
  56. #ifdef CONFIG_S390
  57. asm (
  58. " .pushsection .text, \"ax\", @progbits\n"
  59. " .type my_tramp, @function\n"
  60. " .globl my_tramp\n"
  61. " my_tramp:"
  62. " lgr %r1,%r15\n"
  63. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  64. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  65. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  66. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  67. " brasl %r14,my_direct_func\n"
  68. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  69. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  70. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  71. " lgr %r1,%r0\n"
  72. " br %r1\n"
  73. " .size my_tramp, .-my_tramp\n"
  74. " .popsection\n"
  75. );
  76. #endif /* CONFIG_S390 */
  77. #ifdef CONFIG_ARM64
  78. asm (
  79. " .pushsection .text, \"ax\", @progbits\n"
  80. " .type my_tramp, @function\n"
  81. " .globl my_tramp\n"
  82. " my_tramp:"
  83. " hint 34\n" // bti c
  84. " sub sp, sp, #32\n"
  85. " stp x9, x30, [sp]\n"
  86. " str x0, [sp, #16]\n"
  87. " bl my_direct_func\n"
  88. " ldp x30, x9, [sp]\n"
  89. " ldr x0, [sp, #16]\n"
  90. " add sp, sp, #32\n"
  91. " ret x9\n"
  92. " .size my_tramp, .-my_tramp\n"
  93. " .popsection\n"
  94. );
  95. #endif /* CONFIG_ARM64 */
  96. #ifdef CONFIG_LOONGARCH
  97. asm (
  98. " .pushsection .text, \"ax\", @progbits\n"
  99. " .type my_tramp, @function\n"
  100. " .globl my_tramp\n"
  101. " my_tramp:\n"
  102. " addi.d $sp, $sp, -32\n"
  103. " st.d $a0, $sp, 0\n"
  104. " st.d $t0, $sp, 8\n"
  105. " st.d $ra, $sp, 16\n"
  106. " bl my_direct_func\n"
  107. " ld.d $a0, $sp, 0\n"
  108. " ld.d $t0, $sp, 8\n"
  109. " ld.d $ra, $sp, 16\n"
  110. " addi.d $sp, $sp, 32\n"
  111. " jr $t0\n"
  112. " .size my_tramp, .-my_tramp\n"
  113. " .popsection\n"
  114. );
  115. #endif /* CONFIG_LOONGARCH */
  116. static struct ftrace_ops direct;
  117. static int __init ftrace_direct_init(void)
  118. {
  119. ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
  120. return register_ftrace_direct(&direct, (unsigned long) my_tramp);
  121. }
  122. static void __exit ftrace_direct_exit(void)
  123. {
  124. unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
  125. }
  126. module_init(ftrace_direct_init);
  127. module_exit(ftrace_direct_exit);
  128. MODULE_AUTHOR("Steven Rostedt");
  129. MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
  130. MODULE_LICENSE("GPL");