ftrace_64_mprofile.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Split from ftrace_64.S
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/magic.h>
  10. #include <asm/ppc_asm.h>
  11. #include <asm/asm-offsets.h>
  12. #include <asm/ftrace.h>
  13. #include <asm/ppc-opcode.h>
  14. #include <asm/export.h>
  15. #include <asm/thread_info.h>
  16. #include <asm/bug.h>
  17. #include <asm/ptrace.h>
  18. /*
  19. *
  20. * ftrace_caller()/ftrace_regs_caller() is the function that replaces _mcount()
  21. * when ftrace is active.
  22. *
  23. * We arrive here after a function A calls function B, and we are the trace
  24. * function for B. When we enter r1 points to A's stack frame, B has not yet
  25. * had a chance to allocate one yet.
  26. *
  27. * Additionally r2 may point either to the TOC for A, or B, depending on
  28. * whether B did a TOC setup sequence before calling us.
  29. *
  30. * On entry the LR points back to the _mcount() call site, and r0 holds the
  31. * saved LR as it was on entry to B, ie. the original return address at the
  32. * call site in A.
  33. *
  34. * Our job is to save the register state into a struct pt_regs (on the stack)
  35. * and then arrange for the ftrace function to be called.
  36. */
  37. _GLOBAL(ftrace_regs_caller)
  38. /* Save the original return address in A's stack frame */
  39. std r0,LRSAVE(r1)
  40. /* Create our stack frame + pt_regs */
  41. stdu r1,-SWITCH_FRAME_SIZE(r1)
  42. /* Save all gprs to pt_regs */
  43. SAVE_GPR(0, r1)
  44. SAVE_10GPRS(2, r1)
  45. /* Ok to continue? */
  46. lbz r3, PACA_FTRACE_ENABLED(r13)
  47. cmpdi r3, 0
  48. beq ftrace_no_trace
  49. SAVE_10GPRS(12, r1)
  50. SAVE_10GPRS(22, r1)
  51. /* Save previous stack pointer (r1) */
  52. addi r8, r1, SWITCH_FRAME_SIZE
  53. std r8, GPR1(r1)
  54. /* Load special regs for save below */
  55. mfmsr r8
  56. mfctr r9
  57. mfxer r10
  58. mfcr r11
  59. /* Get the _mcount() call site out of LR */
  60. mflr r7
  61. /* Save it as pt_regs->nip */
  62. std r7, _NIP(r1)
  63. /* Save the read LR in pt_regs->link */
  64. std r0, _LINK(r1)
  65. /* Save callee's TOC in the ABI compliant location */
  66. std r2, 24(r1)
  67. ld r2,PACATOC(r13) /* get kernel TOC in r2 */
  68. addis r3,r2,function_trace_op@toc@ha
  69. addi r3,r3,function_trace_op@toc@l
  70. ld r5,0(r3)
  71. #ifdef CONFIG_LIVEPATCH
  72. mr r14,r7 /* remember old NIP */
  73. #endif
  74. /* Calculate ip from nip-4 into r3 for call below */
  75. subi r3, r7, MCOUNT_INSN_SIZE
  76. /* Put the original return address in r4 as parent_ip */
  77. mr r4, r0
  78. /* Save special regs */
  79. std r8, _MSR(r1)
  80. std r9, _CTR(r1)
  81. std r10, _XER(r1)
  82. std r11, _CCR(r1)
  83. /* Load &pt_regs in r6 for call below */
  84. addi r6, r1 ,STACK_FRAME_OVERHEAD
  85. /* ftrace_call(r3, r4, r5, r6) */
  86. .globl ftrace_regs_call
  87. ftrace_regs_call:
  88. bl ftrace_stub
  89. nop
  90. /* Load ctr with the possibly modified NIP */
  91. ld r3, _NIP(r1)
  92. mtctr r3
  93. #ifdef CONFIG_LIVEPATCH
  94. cmpd r14, r3 /* has NIP been altered? */
  95. #endif
  96. /* Restore gprs */
  97. REST_GPR(0,r1)
  98. REST_10GPRS(2,r1)
  99. REST_10GPRS(12,r1)
  100. REST_10GPRS(22,r1)
  101. /* Restore possibly modified LR */
  102. ld r0, _LINK(r1)
  103. mtlr r0
  104. /* Restore callee's TOC */
  105. ld r2, 24(r1)
  106. /* Pop our stack frame */
  107. addi r1, r1, SWITCH_FRAME_SIZE
  108. #ifdef CONFIG_LIVEPATCH
  109. /* Based on the cmpd above, if the NIP was altered handle livepatch */
  110. bne- livepatch_handler
  111. #endif
  112. ftrace_caller_common:
  113. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  114. .globl ftrace_graph_call
  115. ftrace_graph_call:
  116. b ftrace_graph_stub
  117. _GLOBAL(ftrace_graph_stub)
  118. #endif
  119. bctr /* jump after _mcount site */
  120. _GLOBAL(ftrace_stub)
  121. blr
  122. ftrace_no_trace:
  123. mflr r3
  124. mtctr r3
  125. REST_GPR(3, r1)
  126. addi r1, r1, SWITCH_FRAME_SIZE
  127. mtlr r0
  128. bctr
  129. _GLOBAL(ftrace_caller)
  130. /* Save the original return address in A's stack frame */
  131. std r0, LRSAVE(r1)
  132. /* Create our stack frame + pt_regs */
  133. stdu r1, -SWITCH_FRAME_SIZE(r1)
  134. /* Save all gprs to pt_regs */
  135. SAVE_8GPRS(3, r1)
  136. lbz r3, PACA_FTRACE_ENABLED(r13)
  137. cmpdi r3, 0
  138. beq ftrace_no_trace
  139. /* Get the _mcount() call site out of LR */
  140. mflr r7
  141. std r7, _NIP(r1)
  142. /* Save callee's TOC in the ABI compliant location */
  143. std r2, 24(r1)
  144. ld r2, PACATOC(r13) /* get kernel TOC in r2 */
  145. addis r3, r2, function_trace_op@toc@ha
  146. addi r3, r3, function_trace_op@toc@l
  147. ld r5, 0(r3)
  148. /* Calculate ip from nip-4 into r3 for call below */
  149. subi r3, r7, MCOUNT_INSN_SIZE
  150. /* Put the original return address in r4 as parent_ip */
  151. mr r4, r0
  152. /* Set pt_regs to NULL */
  153. li r6, 0
  154. /* ftrace_call(r3, r4, r5, r6) */
  155. .globl ftrace_call
  156. ftrace_call:
  157. bl ftrace_stub
  158. nop
  159. ld r3, _NIP(r1)
  160. mtctr r3
  161. /* Restore gprs */
  162. REST_8GPRS(3,r1)
  163. /* Restore callee's TOC */
  164. ld r2, 24(r1)
  165. /* Pop our stack frame */
  166. addi r1, r1, SWITCH_FRAME_SIZE
  167. /* Reload original LR */
  168. ld r0, LRSAVE(r1)
  169. mtlr r0
  170. /* Handle function_graph or go back */
  171. b ftrace_caller_common
  172. #ifdef CONFIG_LIVEPATCH
  173. /*
  174. * This function runs in the mcount context, between two functions. As
  175. * such it can only clobber registers which are volatile and used in
  176. * function linkage.
  177. *
  178. * We get here when a function A, calls another function B, but B has
  179. * been live patched with a new function C.
  180. *
  181. * On entry:
  182. * - we have no stack frame and can not allocate one
  183. * - LR points back to the original caller (in A)
  184. * - CTR holds the new NIP in C
  185. * - r0, r11 & r12 are free
  186. */
  187. livepatch_handler:
  188. CURRENT_THREAD_INFO(r12, r1)
  189. /* Allocate 3 x 8 bytes */
  190. ld r11, TI_livepatch_sp(r12)
  191. addi r11, r11, 24
  192. std r11, TI_livepatch_sp(r12)
  193. /* Save toc & real LR on livepatch stack */
  194. std r2, -24(r11)
  195. mflr r12
  196. std r12, -16(r11)
  197. /* Store stack end marker */
  198. lis r12, STACK_END_MAGIC@h
  199. ori r12, r12, STACK_END_MAGIC@l
  200. std r12, -8(r11)
  201. /* Put ctr in r12 for global entry and branch there */
  202. mfctr r12
  203. bctrl
  204. /*
  205. * Now we are returning from the patched function to the original
  206. * caller A. We are free to use r11, r12 and we can use r2 until we
  207. * restore it.
  208. */
  209. CURRENT_THREAD_INFO(r12, r1)
  210. ld r11, TI_livepatch_sp(r12)
  211. /* Check stack marker hasn't been trashed */
  212. lis r2, STACK_END_MAGIC@h
  213. ori r2, r2, STACK_END_MAGIC@l
  214. ld r12, -8(r11)
  215. 1: tdne r12, r2
  216. EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
  217. /* Restore LR & toc from livepatch stack */
  218. ld r12, -16(r11)
  219. mtlr r12
  220. ld r2, -24(r11)
  221. /* Pop livepatch stack frame */
  222. CURRENT_THREAD_INFO(r12, r1)
  223. subi r11, r11, 24
  224. std r11, TI_livepatch_sp(r12)
  225. /* Return to original caller of live patched function */
  226. blr
  227. #endif /* CONFIG_LIVEPATCH */
  228. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  229. _GLOBAL(ftrace_graph_caller)
  230. stdu r1, -112(r1)
  231. /* with -mprofile-kernel, parameter regs are still alive at _mcount */
  232. std r10, 104(r1)
  233. std r9, 96(r1)
  234. std r8, 88(r1)
  235. std r7, 80(r1)
  236. std r6, 72(r1)
  237. std r5, 64(r1)
  238. std r4, 56(r1)
  239. std r3, 48(r1)
  240. /* Save callee's TOC in the ABI compliant location */
  241. std r2, 24(r1)
  242. ld r2, PACATOC(r13) /* get kernel TOC in r2 */
  243. mfctr r4 /* ftrace_caller has moved local addr here */
  244. std r4, 40(r1)
  245. mflr r3 /* ftrace_caller has restored LR from stack */
  246. subi r4, r4, MCOUNT_INSN_SIZE
  247. bl prepare_ftrace_return
  248. nop
  249. /*
  250. * prepare_ftrace_return gives us the address we divert to.
  251. * Change the LR to this.
  252. */
  253. mtlr r3
  254. ld r0, 40(r1)
  255. mtctr r0
  256. ld r10, 104(r1)
  257. ld r9, 96(r1)
  258. ld r8, 88(r1)
  259. ld r7, 80(r1)
  260. ld r6, 72(r1)
  261. ld r5, 64(r1)
  262. ld r4, 56(r1)
  263. ld r3, 48(r1)
  264. /* Restore callee's TOC */
  265. ld r2, 24(r1)
  266. addi r1, r1, 112
  267. mflr r0
  268. std r0, LRSAVE(r1)
  269. bctr
  270. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */