mcount.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2017 Andes Technology Corporation */
  3. #include <linux/init.h>
  4. #include <linux/linkage.h>
  5. #include <asm/asm.h>
  6. #include <asm/csr.h>
  7. #include <asm/unistd.h>
  8. #include <asm/thread_info.h>
  9. #include <asm/asm-offsets.h>
  10. #include <asm-generic/export.h>
  11. #include <asm/ftrace.h>
  12. .text
  13. .macro SAVE_ABI_STATE
  14. addi sp, sp, -16
  15. sd s0, 0(sp)
  16. sd ra, 8(sp)
  17. addi s0, sp, 16
  18. .endm
  19. /*
  20. * The call to ftrace_return_to_handler would overwrite the return
  21. * register if a0 was not saved.
  22. */
  23. .macro SAVE_RET_ABI_STATE
  24. addi sp, sp, -32
  25. sd s0, 16(sp)
  26. sd ra, 24(sp)
  27. sd a0, 8(sp)
  28. addi s0, sp, 32
  29. .endm
  30. .macro RESTORE_ABI_STATE
  31. ld ra, 8(sp)
  32. ld s0, 0(sp)
  33. addi sp, sp, 16
  34. .endm
  35. .macro RESTORE_RET_ABI_STATE
  36. ld ra, 24(sp)
  37. ld s0, 16(sp)
  38. ld a0, 8(sp)
  39. addi sp, sp, 32
  40. .endm
  41. ENTRY(ftrace_stub)
  42. #ifdef CONFIG_DYNAMIC_FTRACE
  43. .global _mcount
  44. .set _mcount, ftrace_stub
  45. #endif
  46. ret
  47. ENDPROC(ftrace_stub)
  48. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  49. ENTRY(return_to_handler)
  50. /*
  51. * On implementing the frame point test, the ideal way is to compare the
  52. * s0 (frame pointer, if enabled) on entry and the sp (stack pointer) on return.
  53. * However, the psABI of variable-length-argument functions does not allow this.
  54. *
  55. * So alternatively we check the *old* frame pointer position, that is, the
  56. * value stored in -16(s0) on entry, and the s0 on return.
  57. */
  58. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  59. mv t6, s0
  60. #endif
  61. SAVE_RET_ABI_STATE
  62. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  63. mv a0, t6
  64. #endif
  65. call ftrace_return_to_handler
  66. mv a1, a0
  67. RESTORE_RET_ABI_STATE
  68. jalr a1
  69. ENDPROC(return_to_handler)
  70. EXPORT_SYMBOL(return_to_handler)
  71. #endif
  72. #ifndef CONFIG_DYNAMIC_FTRACE
  73. ENTRY(_mcount)
  74. la t4, ftrace_stub
  75. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  76. la t0, ftrace_graph_return
  77. ld t1, 0(t0)
  78. bne t1, t4, do_ftrace_graph_caller
  79. la t3, ftrace_graph_entry
  80. ld t2, 0(t3)
  81. la t6, ftrace_graph_entry_stub
  82. bne t2, t6, do_ftrace_graph_caller
  83. #endif
  84. la t3, ftrace_trace_function
  85. ld t5, 0(t3)
  86. bne t5, t4, do_trace
  87. ret
  88. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  89. /*
  90. * A pseudo representation for the function graph tracer:
  91. * prepare_to_return(&ra_to_caller_of_caller, ra_to_caller)
  92. */
  93. do_ftrace_graph_caller:
  94. addi a0, s0, -8
  95. mv a1, ra
  96. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  97. ld a2, -16(s0)
  98. #endif
  99. SAVE_ABI_STATE
  100. call prepare_ftrace_return
  101. RESTORE_ABI_STATE
  102. ret
  103. #endif
  104. /*
  105. * A pseudo representation for the function tracer:
  106. * (*ftrace_trace_function)(ra_to_caller, ra_to_caller_of_caller)
  107. */
  108. do_trace:
  109. ld a1, -8(s0)
  110. mv a0, ra
  111. SAVE_ABI_STATE
  112. jalr t5
  113. RESTORE_ABI_STATE
  114. ret
  115. ENDPROC(_mcount)
  116. #endif
  117. EXPORT_SYMBOL(_mcount)