ftrace-direct.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <asm/asm-offsets.h>
  6. extern void my_direct_func(struct task_struct *p);
  7. void my_direct_func(struct task_struct *p)
  8. {
  9. trace_printk("waking up %s-%d\n", p->comm, p->pid);
  10. }
  11. extern void my_tramp(void *);
  12. #ifdef CONFIG_X86_64
  13. #include <asm/ibt.h>
  14. asm (
  15. " .pushsection .text, \"ax\", @progbits\n"
  16. " .type my_tramp, @function\n"
  17. " .globl my_tramp\n"
  18. " my_tramp:"
  19. ASM_ENDBR
  20. " pushq %rbp\n"
  21. " movq %rsp, %rbp\n"
  22. " pushq %rdi\n"
  23. " call my_direct_func\n"
  24. " popq %rdi\n"
  25. " leave\n"
  26. ASM_RET
  27. " .size my_tramp, .-my_tramp\n"
  28. " .popsection\n"
  29. );
  30. #endif /* CONFIG_X86_64 */
  31. #ifdef CONFIG_S390
  32. asm (
  33. " .pushsection .text, \"ax\", @progbits\n"
  34. " .type my_tramp, @function\n"
  35. " .globl my_tramp\n"
  36. " my_tramp:"
  37. " lgr %r1,%r15\n"
  38. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  39. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  40. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  41. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  42. " brasl %r14,my_direct_func\n"
  43. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  44. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  45. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  46. " lgr %r1,%r0\n"
  47. " br %r1\n"
  48. " .size my_tramp, .-my_tramp\n"
  49. " .popsection\n"
  50. );
  51. #endif /* CONFIG_S390 */
  52. static int __init ftrace_direct_init(void)
  53. {
  54. return register_ftrace_direct((unsigned long)wake_up_process,
  55. (unsigned long)my_tramp);
  56. }
  57. static void __exit ftrace_direct_exit(void)
  58. {
  59. unregister_ftrace_direct((unsigned long)wake_up_process,
  60. (unsigned long)my_tramp);
  61. }
  62. module_init(ftrace_direct_init);
  63. module_exit(ftrace_direct_exit);
  64. MODULE_AUTHOR("Steven Rostedt");
  65. MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
  66. MODULE_LICENSE("GPL");