mcount.S 2.8 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 <linux/cfi_types.h>
  6. #include <linux/export.h>
  7. #include <asm/asm.h>
  8. #include <asm/csr.h>
  9. #include <asm/unistd.h>
  10. #include <asm/thread_info.h>
  11. #include <asm/asm-offsets.h>
  12. #include <asm/ftrace.h>
  13. .text
  14. .macro SAVE_ABI_STATE
  15. addi sp, sp, -16
  16. REG_S s0, 0*SZREG(sp)
  17. REG_S ra, 1*SZREG(sp)
  18. addi s0, sp, 16
  19. .endm
  20. /*
  21. * The call to ftrace_return_to_handler would overwrite the return
  22. * register if a0 was not saved.
  23. */
  24. .macro SAVE_RET_ABI_STATE
  25. addi sp, sp, -4*SZREG
  26. REG_S s0, 2*SZREG(sp)
  27. REG_S ra, 3*SZREG(sp)
  28. REG_S a0, 1*SZREG(sp)
  29. REG_S a1, 0*SZREG(sp)
  30. addi s0, sp, 4*SZREG
  31. .endm
  32. .macro RESTORE_ABI_STATE
  33. REG_L ra, 1*SZREG(sp)
  34. REG_L s0, 0*SZREG(sp)
  35. addi sp, sp, 16
  36. .endm
  37. .macro RESTORE_RET_ABI_STATE
  38. REG_L ra, 3*SZREG(sp)
  39. REG_L s0, 2*SZREG(sp)
  40. REG_L a0, 1*SZREG(sp)
  41. REG_L a1, 0*SZREG(sp)
  42. addi sp, sp, 4*SZREG
  43. .endm
  44. SYM_TYPED_FUNC_START(ftrace_stub)
  45. #ifdef CONFIG_DYNAMIC_FTRACE
  46. .global _mcount
  47. .set _mcount, ftrace_stub
  48. #endif
  49. ret
  50. SYM_FUNC_END(ftrace_stub)
  51. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  52. SYM_TYPED_FUNC_START(ftrace_stub_graph)
  53. ret
  54. SYM_FUNC_END(ftrace_stub_graph)
  55. SYM_FUNC_START(return_to_handler)
  56. /*
  57. * On implementing the frame point test, the ideal way is to compare the
  58. * s0 (frame pointer, if enabled) on entry and the sp (stack pointer) on return.
  59. * However, the psABI of variable-length-argument functions does not allow this.
  60. *
  61. * So alternatively we check the *old* frame pointer position, that is, the
  62. * value stored in -16(s0) on entry, and the s0 on return.
  63. */
  64. SAVE_RET_ABI_STATE
  65. mv a0, sp
  66. call ftrace_return_to_handler
  67. mv a2, a0
  68. RESTORE_RET_ABI_STATE
  69. jalr a2
  70. SYM_FUNC_END(return_to_handler)
  71. #endif
  72. #ifndef CONFIG_DYNAMIC_FTRACE
  73. SYM_FUNC_START(_mcount)
  74. la t4, ftrace_stub
  75. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  76. la t0, ftrace_graph_return
  77. REG_L t1, 0(t0)
  78. bne t1, t4, .Ldo_ftrace_graph_caller
  79. la t3, ftrace_graph_entry
  80. REG_L t2, 0(t3)
  81. la t6, ftrace_graph_entry_stub
  82. bne t2, t6, .Ldo_ftrace_graph_caller
  83. #endif
  84. la t3, ftrace_trace_function
  85. REG_L t5, 0(t3)
  86. bne t5, t4, .Ldo_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. .Ldo_ftrace_graph_caller:
  94. addi a0, s0, -SZREG
  95. mv a1, ra
  96. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  97. REG_L a2, -2*SZREG(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. .Ldo_trace:
  109. REG_L a1, -SZREG(s0)
  110. mv a0, ra
  111. SAVE_ABI_STATE
  112. jalr t5
  113. RESTORE_ABI_STATE
  114. ret
  115. SYM_FUNC_END(_mcount)
  116. #endif
  117. EXPORT_SYMBOL(_mcount)