mcount.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_NAME
  44. .set MCOUNT_NAME, 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. #endif
  71. #ifndef CONFIG_DYNAMIC_FTRACE
  72. ENTRY(MCOUNT_NAME)
  73. la t4, ftrace_stub
  74. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  75. la t0, ftrace_graph_return
  76. ld t1, 0(t0)
  77. bne t1, t4, do_ftrace_graph_caller
  78. la t3, ftrace_graph_entry
  79. ld t2, 0(t3)
  80. la t6, ftrace_graph_entry_stub
  81. bne t2, t6, do_ftrace_graph_caller
  82. #endif
  83. la t3, ftrace_trace_function
  84. ld t5, 0(t3)
  85. bne t5, t4, do_trace
  86. ret
  87. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  88. /*
  89. * A pseudo representation for the function graph tracer:
  90. * prepare_to_return(&ra_to_caller_of_caller, ra_to_caller)
  91. */
  92. do_ftrace_graph_caller:
  93. addi a0, s0, -8
  94. mv a1, ra
  95. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  96. ld a2, -16(s0)
  97. #endif
  98. SAVE_ABI_STATE
  99. call prepare_ftrace_return
  100. RESTORE_ABI_STATE
  101. ret
  102. #endif
  103. /*
  104. * A pseudo representation for the function tracer:
  105. * (*ftrace_trace_function)(ra_to_caller, ra_to_caller_of_caller)
  106. */
  107. do_trace:
  108. ld a1, -8(s0)
  109. mv a0, ra
  110. SAVE_ABI_STATE
  111. jalr t5
  112. RESTORE_ABI_STATE
  113. ret
  114. ENDPROC(MCOUNT_NAME)
  115. #endif
  116. EXPORT_SYMBOL(MCOUNT_NAME)