ftrace_64_mprofile.S 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 the possibly modified NIP */
  91. ld r15, _NIP(r1)
  92. #ifdef CONFIG_LIVEPATCH
  93. cmpd r14, r15 /* has NIP been altered? */
  94. #endif
  95. #if defined(CONFIG_LIVEPATCH) && defined(CONFIG_KPROBES_ON_FTRACE)
  96. /* NIP has not been altered, skip over further checks */
  97. beq 1f
  98. /* Check if there is an active jprobe on us */
  99. subi r3, r14, 4
  100. bl __is_active_jprobe
  101. nop
  102. /*
  103. * If r3 == 1, then this is a kprobe/jprobe.
  104. * else, this is livepatched function.
  105. *
  106. * The conditional branch for livepatch_handler below will use the
  107. * result of this comparison. For kprobe/jprobe, we just need to branch to
  108. * the new NIP, not call livepatch_handler. The branch below is bne, so we
  109. * want CR0[EQ] to be true if this is a kprobe/jprobe. Which means we want
  110. * CR0[EQ] = (r3 == 1).
  111. */
  112. cmpdi r3, 1
  113. 1:
  114. #endif
  115. /* Load CTR with the possibly modified NIP */
  116. mtctr r15
  117. /* Restore gprs */
  118. REST_GPR(0,r1)
  119. REST_10GPRS(2,r1)
  120. REST_10GPRS(12,r1)
  121. REST_10GPRS(22,r1)
  122. /* Restore possibly modified LR */
  123. ld r0, _LINK(r1)
  124. mtlr r0
  125. /* Restore callee's TOC */
  126. ld r2, 24(r1)
  127. /* Pop our stack frame */
  128. addi r1, r1, SWITCH_FRAME_SIZE
  129. #ifdef CONFIG_LIVEPATCH
  130. /*
  131. * Based on the cmpd or cmpdi above, if the NIP was altered and we're
  132. * not on a kprobe/jprobe, then handle livepatch.
  133. */
  134. bne- livepatch_handler
  135. #endif
  136. ftrace_caller_common:
  137. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  138. .globl ftrace_graph_call
  139. ftrace_graph_call:
  140. b ftrace_graph_stub
  141. _GLOBAL(ftrace_graph_stub)
  142. #endif
  143. bctr /* jump after _mcount site */
  144. _GLOBAL(ftrace_stub)
  145. blr
  146. ftrace_no_trace:
  147. mflr r3
  148. mtctr r3
  149. REST_GPR(3, r1)
  150. addi r1, r1, SWITCH_FRAME_SIZE
  151. mtlr r0
  152. bctr
  153. _GLOBAL(ftrace_caller)
  154. /* Save the original return address in A's stack frame */
  155. std r0, LRSAVE(r1)
  156. /* Create our stack frame + pt_regs */
  157. stdu r1, -SWITCH_FRAME_SIZE(r1)
  158. /* Save all gprs to pt_regs */
  159. SAVE_8GPRS(3, r1)
  160. lbz r3, PACA_FTRACE_ENABLED(r13)
  161. cmpdi r3, 0
  162. beq ftrace_no_trace
  163. /* Get the _mcount() call site out of LR */
  164. mflr r7
  165. std r7, _NIP(r1)
  166. /* Save callee's TOC in the ABI compliant location */
  167. std r2, 24(r1)
  168. ld r2, PACATOC(r13) /* get kernel TOC in r2 */
  169. addis r3, r2, function_trace_op@toc@ha
  170. addi r3, r3, function_trace_op@toc@l
  171. ld r5, 0(r3)
  172. /* Calculate ip from nip-4 into r3 for call below */
  173. subi r3, r7, MCOUNT_INSN_SIZE
  174. /* Put the original return address in r4 as parent_ip */
  175. mr r4, r0
  176. /* Set pt_regs to NULL */
  177. li r6, 0
  178. /* ftrace_call(r3, r4, r5, r6) */
  179. .globl ftrace_call
  180. ftrace_call:
  181. bl ftrace_stub
  182. nop
  183. ld r3, _NIP(r1)
  184. mtctr r3
  185. /* Restore gprs */
  186. REST_8GPRS(3,r1)
  187. /* Restore callee's TOC */
  188. ld r2, 24(r1)
  189. /* Pop our stack frame */
  190. addi r1, r1, SWITCH_FRAME_SIZE
  191. /* Reload original LR */
  192. ld r0, LRSAVE(r1)
  193. mtlr r0
  194. /* Handle function_graph or go back */
  195. b ftrace_caller_common
  196. #ifdef CONFIG_LIVEPATCH
  197. /*
  198. * This function runs in the mcount context, between two functions. As
  199. * such it can only clobber registers which are volatile and used in
  200. * function linkage.
  201. *
  202. * We get here when a function A, calls another function B, but B has
  203. * been live patched with a new function C.
  204. *
  205. * On entry:
  206. * - we have no stack frame and can not allocate one
  207. * - LR points back to the original caller (in A)
  208. * - CTR holds the new NIP in C
  209. * - r0, r11 & r12 are free
  210. */
  211. livepatch_handler:
  212. CURRENT_THREAD_INFO(r12, r1)
  213. /* Allocate 3 x 8 bytes */
  214. ld r11, TI_livepatch_sp(r12)
  215. addi r11, r11, 24
  216. std r11, TI_livepatch_sp(r12)
  217. /* Save toc & real LR on livepatch stack */
  218. std r2, -24(r11)
  219. mflr r12
  220. std r12, -16(r11)
  221. /* Store stack end marker */
  222. lis r12, STACK_END_MAGIC@h
  223. ori r12, r12, STACK_END_MAGIC@l
  224. std r12, -8(r11)
  225. /* Put ctr in r12 for global entry and branch there */
  226. mfctr r12
  227. bctrl
  228. /*
  229. * Now we are returning from the patched function to the original
  230. * caller A. We are free to use r11, r12 and we can use r2 until we
  231. * restore it.
  232. */
  233. CURRENT_THREAD_INFO(r12, r1)
  234. ld r11, TI_livepatch_sp(r12)
  235. /* Check stack marker hasn't been trashed */
  236. lis r2, STACK_END_MAGIC@h
  237. ori r2, r2, STACK_END_MAGIC@l
  238. ld r12, -8(r11)
  239. 1: tdne r12, r2
  240. EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
  241. /* Restore LR & toc from livepatch stack */
  242. ld r12, -16(r11)
  243. mtlr r12
  244. ld r2, -24(r11)
  245. /* Pop livepatch stack frame */
  246. CURRENT_THREAD_INFO(r12, r1)
  247. subi r11, r11, 24
  248. std r11, TI_livepatch_sp(r12)
  249. /* Return to original caller of live patched function */
  250. blr
  251. #endif /* CONFIG_LIVEPATCH */
  252. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  253. _GLOBAL(ftrace_graph_caller)
  254. stdu r1, -112(r1)
  255. /* with -mprofile-kernel, parameter regs are still alive at _mcount */
  256. std r10, 104(r1)
  257. std r9, 96(r1)
  258. std r8, 88(r1)
  259. std r7, 80(r1)
  260. std r6, 72(r1)
  261. std r5, 64(r1)
  262. std r4, 56(r1)
  263. std r3, 48(r1)
  264. /* Save callee's TOC in the ABI compliant location */
  265. std r2, 24(r1)
  266. ld r2, PACATOC(r13) /* get kernel TOC in r2 */
  267. mfctr r4 /* ftrace_caller has moved local addr here */
  268. std r4, 40(r1)
  269. mflr r3 /* ftrace_caller has restored LR from stack */
  270. subi r4, r4, MCOUNT_INSN_SIZE
  271. bl prepare_ftrace_return
  272. nop
  273. /*
  274. * prepare_ftrace_return gives us the address we divert to.
  275. * Change the LR to this.
  276. */
  277. mtlr r3
  278. ld r0, 40(r1)
  279. mtctr r0
  280. ld r10, 104(r1)
  281. ld r9, 96(r1)
  282. ld r8, 88(r1)
  283. ld r7, 80(r1)
  284. ld r6, 72(r1)
  285. ld r5, 64(r1)
  286. ld r4, 56(r1)
  287. ld r3, 48(r1)
  288. /* Restore callee's TOC */
  289. ld r2, 24(r1)
  290. addi r1, r1, 112
  291. mflr r0
  292. std r0, LRSAVE(r1)
  293. bctr
  294. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */