entry-ftrace.S 5.4 KB

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