entry-ftrace.S 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  85. .macro __ftrace_regs_caller
  86. sub sp, sp, #8 @ space for PC and CPSR OLD_R0,
  87. @ OLD_R0 will overwrite previous LR
  88. add ip, sp, #12 @ move in IP the value of SP as it was
  89. @ before the push {lr} of the mcount mechanism
  90. str lr, [sp, #0] @ store LR instead of PC
  91. ldr lr, [sp, #8] @ get previous LR
  92. str r0, [sp, #8] @ write r0 as OLD_R0 over previous LR
  93. stmdb sp!, {ip, lr}
  94. stmdb sp!, {r0-r11, lr}
  95. @ stack content at this point:
  96. @ 0 4 48 52 56 60 64 68 72
  97. @ R0 | R1 | ... | LR | SP + 4 | previous LR | LR | PSR | OLD_R0 |
  98. mov r3, sp @ struct pt_regs*
  99. ldr r2, =function_trace_op
  100. ldr r2, [r2] @ pointer to the current
  101. @ function tracing op
  102. ldr r1, [sp, #S_LR] @ lr of instrumented func
  103. ldr lr, [sp, #S_PC] @ get LR
  104. mcount_adjust_addr r0, lr @ instrumented function
  105. .globl ftrace_regs_call
  106. ftrace_regs_call:
  107. bl ftrace_stub
  108. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  109. .globl ftrace_graph_regs_call
  110. ftrace_graph_regs_call:
  111. mov r0, r0
  112. #endif
  113. @ pop saved regs
  114. ldmia sp!, {r0-r12} @ restore r0 through r12
  115. ldr ip, [sp, #8] @ restore PC
  116. ldr lr, [sp, #4] @ restore LR
  117. ldr sp, [sp, #0] @ restore SP
  118. mov pc, ip @ return
  119. .endm
  120. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  121. .macro __ftrace_graph_regs_caller
  122. sub r0, fp, #4 @ lr of instrumented routine (parent)
  123. @ called from __ftrace_regs_caller
  124. ldr r1, [sp, #S_PC] @ instrumented routine (func)
  125. mcount_adjust_addr r1, r1
  126. mov r2, fp @ frame pointer
  127. bl prepare_ftrace_return
  128. @ pop registers saved in ftrace_regs_caller
  129. ldmia sp!, {r0-r12} @ restore r0 through r12
  130. ldr ip, [sp, #8] @ restore PC
  131. ldr lr, [sp, #4] @ restore LR
  132. ldr sp, [sp, #0] @ restore SP
  133. mov pc, ip @ return
  134. .endm
  135. #endif
  136. #endif
  137. .macro __ftrace_caller suffix
  138. mcount_enter
  139. mcount_get_lr r1 @ lr of instrumented func
  140. mcount_adjust_addr r0, lr @ instrumented function
  141. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  142. ldr r2, =function_trace_op
  143. ldr r2, [r2] @ pointer to the current
  144. @ function tracing op
  145. mov r3, #0 @ regs is NULL
  146. #endif
  147. .globl ftrace_call\suffix
  148. ftrace_call\suffix:
  149. bl ftrace_stub
  150. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  151. .globl ftrace_graph_call\suffix
  152. ftrace_graph_call\suffix:
  153. mov r0, r0
  154. #endif
  155. mcount_exit
  156. .endm
  157. .macro __ftrace_graph_caller
  158. sub r0, fp, #4 @ &lr of instrumented routine (&parent)
  159. #ifdef CONFIG_DYNAMIC_FTRACE
  160. @ called from __ftrace_caller, saved in mcount_enter
  161. ldr r1, [sp, #16] @ instrumented routine (func)
  162. mcount_adjust_addr r1, r1
  163. #else
  164. @ called from __mcount, untouched in lr
  165. mcount_adjust_addr r1, lr @ instrumented routine (func)
  166. #endif
  167. mov r2, fp @ frame pointer
  168. bl prepare_ftrace_return
  169. mcount_exit
  170. .endm
  171. #ifdef CONFIG_OLD_MCOUNT
  172. /*
  173. * mcount
  174. */
  175. .macro mcount_enter
  176. stmdb sp!, {r0-r3, lr}
  177. .endm
  178. .macro mcount_get_lr reg
  179. ldr \reg, [fp, #-4]
  180. .endm
  181. .macro mcount_exit
  182. ldr lr, [fp, #-4]
  183. ldmia sp!, {r0-r3, pc}
  184. .endm
  185. ENTRY(mcount)
  186. #ifdef CONFIG_DYNAMIC_FTRACE
  187. stmdb sp!, {lr}
  188. ldr lr, [fp, #-4]
  189. ldmia sp!, {pc}
  190. #else
  191. __mcount _old
  192. #endif
  193. ENDPROC(mcount)
  194. #ifdef CONFIG_DYNAMIC_FTRACE
  195. ENTRY(ftrace_caller_old)
  196. __ftrace_caller _old
  197. ENDPROC(ftrace_caller_old)
  198. #endif
  199. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  200. ENTRY(ftrace_graph_caller_old)
  201. __ftrace_graph_caller
  202. ENDPROC(ftrace_graph_caller_old)
  203. #endif
  204. .purgem mcount_enter
  205. .purgem mcount_get_lr
  206. .purgem mcount_exit
  207. #endif
  208. /*
  209. * __gnu_mcount_nc
  210. */
  211. .macro mcount_enter
  212. /*
  213. * This pad compensates for the push {lr} at the call site. Note that we are
  214. * unable to unwind through a function which does not otherwise save its lr.
  215. */
  216. UNWIND(.pad #4)
  217. stmdb sp!, {r0-r3, lr}
  218. UNWIND(.save {r0-r3, lr})
  219. .endm
  220. .macro mcount_get_lr reg
  221. ldr \reg, [sp, #20]
  222. .endm
  223. .macro mcount_exit
  224. ldmia sp!, {r0-r3, ip, lr}
  225. ret ip
  226. .endm
  227. ENTRY(__gnu_mcount_nc)
  228. UNWIND(.fnstart)
  229. #ifdef CONFIG_DYNAMIC_FTRACE
  230. mov ip, lr
  231. ldmia sp!, {lr}
  232. ret ip
  233. #else
  234. __mcount
  235. #endif
  236. UNWIND(.fnend)
  237. ENDPROC(__gnu_mcount_nc)
  238. #ifdef CONFIG_DYNAMIC_FTRACE
  239. ENTRY(ftrace_caller)
  240. UNWIND(.fnstart)
  241. __ftrace_caller
  242. UNWIND(.fnend)
  243. ENDPROC(ftrace_caller)
  244. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  245. ENTRY(ftrace_regs_caller)
  246. UNWIND(.fnstart)
  247. __ftrace_regs_caller
  248. UNWIND(.fnend)
  249. ENDPROC(ftrace_regs_caller)
  250. #endif
  251. #endif
  252. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  253. ENTRY(ftrace_graph_caller)
  254. UNWIND(.fnstart)
  255. __ftrace_graph_caller
  256. UNWIND(.fnend)
  257. ENDPROC(ftrace_graph_caller)
  258. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  259. ENTRY(ftrace_graph_regs_caller)
  260. UNWIND(.fnstart)
  261. __ftrace_graph_regs_caller
  262. UNWIND(.fnend)
  263. ENDPROC(ftrace_graph_regs_caller)
  264. #endif
  265. #endif
  266. .purgem mcount_enter
  267. .purgem mcount_get_lr
  268. .purgem mcount_exit
  269. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  270. .globl return_to_handler
  271. return_to_handler:
  272. stmdb sp!, {r0-r3}
  273. mov r0, fp @ frame pointer
  274. bl ftrace_return_to_handler
  275. mov lr, r0 @ r0 has real ret addr
  276. ldmia sp!, {r0-r3}
  277. ret lr
  278. #endif
  279. ENTRY(ftrace_stub)
  280. .Lftrace_stub:
  281. ret lr
  282. ENDPROC(ftrace_stub)