entry-ftrace.S 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. */
  6. #include <asm/assembler.h>
  7. #include <asm/ftrace.h>
  8. #include <asm/unwind.h>
  9. #include "entry-header.S"
  10. /*
  11. * When compiling with -pg, gcc inserts a call to the mcount routine at the
  12. * start of every function. In mcount, apart from the function's address (in
  13. * lr), we need to get hold of the function's caller's address.
  14. *
  15. * Older GCCs (pre-4.4) inserted a call to a routine called mcount like this:
  16. *
  17. * bl mcount
  18. *
  19. * These versions have the limitation that in order for the mcount routine to
  20. * be able to determine the function's caller's address, an APCS-style frame
  21. * pointer (which is set up with something like the code below) is required.
  22. *
  23. * mov ip, sp
  24. * push {fp, ip, lr, pc}
  25. * sub fp, ip, #4
  26. *
  27. * With EABI, these frame pointers are not available unless -mapcs-frame is
  28. * specified, and if building as Thumb-2, not even then.
  29. *
  30. * Newer GCCs (4.4+) solve this problem by introducing a new version of mcount,
  31. * with call sites like:
  32. *
  33. * push {lr}
  34. * bl __gnu_mcount_nc
  35. *
  36. * With these compilers, frame pointers are not necessary.
  37. *
  38. * mcount can be thought of as a function called in the middle of a subroutine
  39. * call. As such, it needs to be transparent for both the caller and the
  40. * callee: the original lr needs to be restored when leaving mcount, and no
  41. * registers should be clobbered. (In the __gnu_mcount_nc implementation, we
  42. * clobber the ip register. This is OK because the ARM calling convention
  43. * allows it to be clobbered in subroutines and doesn't use it to hold
  44. * parameters.)
  45. *
  46. * When using dynamic ftrace, we patch out the mcount call by a "mov r0, r0"
  47. * for the mcount case, and a "pop {lr}" for the __gnu_mcount_nc case (see
  48. * arch/arm/kernel/ftrace.c).
  49. */
  50. #ifndef CONFIG_OLD_MCOUNT
  51. #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 4))
  52. #error Ftrace requires CONFIG_FRAME_POINTER=y with GCC older than 4.4.0.
  53. #endif
  54. #endif
  55. .macro mcount_adjust_addr rd, rn
  56. bic \rd, \rn, #1 @ clear the Thumb bit if present
  57. sub \rd, \rd, #MCOUNT_INSN_SIZE
  58. .endm
  59. .macro __mcount suffix
  60. mcount_enter
  61. ldr r0, =ftrace_trace_function
  62. ldr r2, [r0]
  63. adr r0, .Lftrace_stub
  64. cmp r0, r2
  65. bne 1f
  66. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  67. ldr r1, =ftrace_graph_return
  68. ldr r2, [r1]
  69. cmp r0, r2
  70. bne ftrace_graph_caller\suffix
  71. ldr r1, =ftrace_graph_entry
  72. ldr r2, [r1]
  73. ldr r0, =ftrace_graph_entry_stub
  74. cmp r0, r2
  75. bne ftrace_graph_caller\suffix
  76. #endif
  77. mcount_exit
  78. 1: mcount_get_lr r1 @ lr of instrumented func
  79. mcount_adjust_addr r0, lr @ instrumented function
  80. badr lr, 2f
  81. mov pc, r2
  82. 2: mcount_exit
  83. .endm
  84. .macro __ftrace_caller suffix
  85. mcount_enter
  86. mcount_get_lr r1 @ lr of instrumented func
  87. mcount_adjust_addr r0, lr @ instrumented function
  88. .globl ftrace_call\suffix
  89. ftrace_call\suffix:
  90. bl ftrace_stub
  91. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  92. .globl ftrace_graph_call\suffix
  93. ftrace_graph_call\suffix:
  94. mov r0, r0
  95. #endif
  96. mcount_exit
  97. .endm
  98. .macro __ftrace_graph_caller
  99. sub r0, fp, #4 @ &lr of instrumented routine (&parent)
  100. #ifdef CONFIG_DYNAMIC_FTRACE
  101. @ called from __ftrace_caller, saved in mcount_enter
  102. ldr r1, [sp, #16] @ instrumented routine (func)
  103. mcount_adjust_addr r1, r1
  104. #else
  105. @ called from __mcount, untouched in lr
  106. mcount_adjust_addr r1, lr @ instrumented routine (func)
  107. #endif
  108. mov r2, fp @ frame pointer
  109. bl prepare_ftrace_return
  110. mcount_exit
  111. .endm
  112. #ifdef CONFIG_OLD_MCOUNT
  113. /*
  114. * mcount
  115. */
  116. .macro mcount_enter
  117. stmdb sp!, {r0-r3, lr}
  118. .endm
  119. .macro mcount_get_lr reg
  120. ldr \reg, [fp, #-4]
  121. .endm
  122. .macro mcount_exit
  123. ldr lr, [fp, #-4]
  124. ldmia sp!, {r0-r3, pc}
  125. .endm
  126. ENTRY(mcount)
  127. #ifdef CONFIG_DYNAMIC_FTRACE
  128. stmdb sp!, {lr}
  129. ldr lr, [fp, #-4]
  130. ldmia sp!, {pc}
  131. #else
  132. __mcount _old
  133. #endif
  134. ENDPROC(mcount)
  135. #ifdef CONFIG_DYNAMIC_FTRACE
  136. ENTRY(ftrace_caller_old)
  137. __ftrace_caller _old
  138. ENDPROC(ftrace_caller_old)
  139. #endif
  140. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  141. ENTRY(ftrace_graph_caller_old)
  142. __ftrace_graph_caller
  143. ENDPROC(ftrace_graph_caller_old)
  144. #endif
  145. .purgem mcount_enter
  146. .purgem mcount_get_lr
  147. .purgem mcount_exit
  148. #endif
  149. /*
  150. * __gnu_mcount_nc
  151. */
  152. .macro mcount_enter
  153. /*
  154. * This pad compensates for the push {lr} at the call site. Note that we are
  155. * unable to unwind through a function which does not otherwise save its lr.
  156. */
  157. UNWIND(.pad #4)
  158. stmdb sp!, {r0-r3, lr}
  159. UNWIND(.save {r0-r3, lr})
  160. .endm
  161. .macro mcount_get_lr reg
  162. ldr \reg, [sp, #20]
  163. .endm
  164. .macro mcount_exit
  165. ldmia sp!, {r0-r3, ip, lr}
  166. ret ip
  167. .endm
  168. ENTRY(__gnu_mcount_nc)
  169. UNWIND(.fnstart)
  170. #ifdef CONFIG_DYNAMIC_FTRACE
  171. mov ip, lr
  172. ldmia sp!, {lr}
  173. ret ip
  174. #else
  175. __mcount
  176. #endif
  177. UNWIND(.fnend)
  178. ENDPROC(__gnu_mcount_nc)
  179. #ifdef CONFIG_DYNAMIC_FTRACE
  180. ENTRY(ftrace_caller)
  181. UNWIND(.fnstart)
  182. __ftrace_caller
  183. UNWIND(.fnend)
  184. ENDPROC(ftrace_caller)
  185. #endif
  186. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  187. ENTRY(ftrace_graph_caller)
  188. UNWIND(.fnstart)
  189. __ftrace_graph_caller
  190. UNWIND(.fnend)
  191. ENDPROC(ftrace_graph_caller)
  192. #endif
  193. .purgem mcount_enter
  194. .purgem mcount_get_lr
  195. .purgem mcount_exit
  196. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  197. .globl return_to_handler
  198. return_to_handler:
  199. stmdb sp!, {r0-r3}
  200. mov r0, fp @ frame pointer
  201. bl ftrace_return_to_handler
  202. mov lr, r0 @ r0 has real ret addr
  203. ldmia sp!, {r0-r3}
  204. ret lr
  205. #endif
  206. ENTRY(ftrace_stub)
  207. .Lftrace_stub:
  208. ret lr
  209. ENDPROC(ftrace_stub)