entry-ftrace.S 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm64/kernel/entry-ftrace.S
  4. *
  5. * Copyright (C) 2013 Linaro Limited
  6. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  7. */
  8. #include <linux/linkage.h>
  9. #include <linux/cfi_types.h>
  10. #include <asm/asm-offsets.h>
  11. #include <asm/assembler.h>
  12. #include <asm/ftrace.h>
  13. #include <asm/insn.h>
  14. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
  15. /*
  16. * Due to -fpatchable-function-entry=2, the compiler has placed two NOPs before
  17. * the regular function prologue. For an enabled callsite, ftrace_init_nop() and
  18. * ftrace_make_call() have patched those NOPs to:
  19. *
  20. * MOV X9, LR
  21. * BL ftrace_caller
  22. *
  23. * Each instrumented function follows the AAPCS, so here x0-x8 and x18-x30 are
  24. * live (x18 holds the Shadow Call Stack pointer), and x9-x17 are safe to
  25. * clobber.
  26. *
  27. * We save the callsite's context into a struct ftrace_regs before invoking any
  28. * ftrace callbacks. So that we can get a sensible backtrace, we create frame
  29. * records for the callsite and the ftrace entry assembly. This is not
  30. * sufficient for reliable stacktrace: until we create the callsite stack
  31. * record, its caller is missing from the LR and existing chain of frame
  32. * records.
  33. */
  34. SYM_CODE_START(ftrace_caller)
  35. bti c
  36. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
  37. /*
  38. * The literal pointer to the ops is at an 8-byte aligned boundary
  39. * which is either 12 or 16 bytes before the BL instruction in the call
  40. * site. See ftrace_call_adjust() for details.
  41. *
  42. * Therefore here the LR points at `literal + 16` or `literal + 20`,
  43. * and we can find the address of the literal in either case by
  44. * aligning to an 8-byte boundary and subtracting 16. We do the
  45. * alignment first as this allows us to fold the subtraction into the
  46. * LDR.
  47. */
  48. bic x11, x30, 0x7
  49. ldr x11, [x11, #-(4 * AARCH64_INSN_SIZE)] // op
  50. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  51. /*
  52. * If the op has a direct call, handle it immediately without
  53. * saving/restoring registers.
  54. */
  55. ldr x17, [x11, #FTRACE_OPS_DIRECT_CALL] // op->direct_call
  56. cbnz x17, ftrace_caller_direct
  57. #endif
  58. #endif
  59. /* Save original SP */
  60. mov x10, sp
  61. /* Make room for ftrace regs, plus two frame records */
  62. sub sp, sp, #(FREGS_SIZE + 32)
  63. /* Save function arguments */
  64. stp x0, x1, [sp, #FREGS_X0]
  65. stp x2, x3, [sp, #FREGS_X2]
  66. stp x4, x5, [sp, #FREGS_X4]
  67. stp x6, x7, [sp, #FREGS_X6]
  68. str x8, [sp, #FREGS_X8]
  69. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  70. str xzr, [sp, #FREGS_DIRECT_TRAMP]
  71. #endif
  72. /* Save the callsite's FP, LR, SP */
  73. str x29, [sp, #FREGS_FP]
  74. str x9, [sp, #FREGS_LR]
  75. str x10, [sp, #FREGS_SP]
  76. /* Save the PC after the ftrace callsite */
  77. str x30, [sp, #FREGS_PC]
  78. /* Create a frame record for the callsite above the ftrace regs */
  79. stp x29, x9, [sp, #FREGS_SIZE + 16]
  80. add x29, sp, #FREGS_SIZE + 16
  81. /* Create our frame record above the ftrace regs */
  82. stp x29, x30, [sp, #FREGS_SIZE]
  83. add x29, sp, #FREGS_SIZE
  84. /* Prepare arguments for the the tracer func */
  85. sub x0, x30, #AARCH64_INSN_SIZE // ip (callsite's BL insn)
  86. mov x1, x9 // parent_ip (callsite's LR)
  87. mov x3, sp // regs
  88. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
  89. mov x2, x11 // op
  90. ldr x4, [x2, #FTRACE_OPS_FUNC] // op->func
  91. blr x4 // op->func(ip, parent_ip, op, regs)
  92. #else
  93. ldr_l x2, function_trace_op // op
  94. SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
  95. bl ftrace_stub // func(ip, parent_ip, op, regs)
  96. #endif
  97. /*
  98. * At the callsite x0-x8 and x19-x30 were live. Any C code will have preserved
  99. * x19-x29 per the AAPCS, and we created frame records upon entry, so we need
  100. * to restore x0-x8, x29, and x30.
  101. */
  102. /* Restore function arguments */
  103. ldp x0, x1, [sp, #FREGS_X0]
  104. ldp x2, x3, [sp, #FREGS_X2]
  105. ldp x4, x5, [sp, #FREGS_X4]
  106. ldp x6, x7, [sp, #FREGS_X6]
  107. ldr x8, [sp, #FREGS_X8]
  108. /* Restore the callsite's FP */
  109. ldr x29, [sp, #FREGS_FP]
  110. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  111. ldr x17, [sp, #FREGS_DIRECT_TRAMP]
  112. cbnz x17, ftrace_caller_direct_late
  113. #endif
  114. /* Restore the callsite's LR and PC */
  115. ldr x30, [sp, #FREGS_LR]
  116. ldr x9, [sp, #FREGS_PC]
  117. /* Restore the callsite's SP */
  118. add sp, sp, #FREGS_SIZE + 32
  119. ret x9
  120. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  121. SYM_INNER_LABEL(ftrace_caller_direct_late, SYM_L_LOCAL)
  122. /*
  123. * Head to a direct trampoline in x17 after having run other tracers.
  124. * The ftrace_regs are live, and x0-x8 and FP have been restored. The
  125. * LR, PC, and SP have not been restored.
  126. */
  127. /*
  128. * Restore the callsite's LR and PC matching the trampoline calling
  129. * convention.
  130. */
  131. ldr x9, [sp, #FREGS_LR]
  132. ldr x30, [sp, #FREGS_PC]
  133. /* Restore the callsite's SP */
  134. add sp, sp, #FREGS_SIZE + 32
  135. SYM_INNER_LABEL(ftrace_caller_direct, SYM_L_LOCAL)
  136. /*
  137. * Head to a direct trampoline in x17.
  138. *
  139. * We use `BR X17` as this can safely land on a `BTI C` or `PACIASP` in
  140. * the trampoline, and will not unbalance any return stack.
  141. */
  142. br x17
  143. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  144. SYM_CODE_END(ftrace_caller)
  145. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  146. SYM_CODE_START(ftrace_stub_direct_tramp)
  147. bti c
  148. mov x10, x30
  149. mov x30, x9
  150. ret x10
  151. SYM_CODE_END(ftrace_stub_direct_tramp)
  152. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
  153. #else /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
  154. /*
  155. * Gcc with -pg will put the following code in the beginning of each function:
  156. * mov x0, x30
  157. * bl _mcount
  158. * [function's body ...]
  159. * "bl _mcount" may be replaced to "bl ftrace_caller" or NOP if dynamic
  160. * ftrace is enabled.
  161. *
  162. * Please note that x0 as an argument will not be used here because we can
  163. * get lr(x30) of instrumented function at any time by winding up call stack
  164. * as long as the kernel is compiled without -fomit-frame-pointer.
  165. * (or CONFIG_FRAME_POINTER, this is forced on arm64)
  166. *
  167. * stack layout after mcount_enter in _mcount():
  168. *
  169. * current sp/fp => 0:+-----+
  170. * in _mcount() | x29 | -> instrumented function's fp
  171. * +-----+
  172. * | x30 | -> _mcount()'s lr (= instrumented function's pc)
  173. * old sp => +16:+-----+
  174. * when instrumented | |
  175. * function calls | ... |
  176. * _mcount() | |
  177. * | |
  178. * instrumented => +xx:+-----+
  179. * function's fp | x29 | -> parent's fp
  180. * +-----+
  181. * | x30 | -> instrumented function's lr (= parent's pc)
  182. * +-----+
  183. * | ... |
  184. */
  185. .macro mcount_enter
  186. stp x29, x30, [sp, #-16]!
  187. mov x29, sp
  188. .endm
  189. .macro mcount_exit
  190. ldp x29, x30, [sp], #16
  191. ret
  192. .endm
  193. .macro mcount_adjust_addr rd, rn
  194. sub \rd, \rn, #AARCH64_INSN_SIZE
  195. .endm
  196. /* for instrumented function's parent */
  197. .macro mcount_get_parent_fp reg
  198. ldr \reg, [x29]
  199. ldr \reg, [\reg]
  200. .endm
  201. /* for instrumented function */
  202. .macro mcount_get_pc0 reg
  203. mcount_adjust_addr \reg, x30
  204. .endm
  205. .macro mcount_get_pc reg
  206. ldr \reg, [x29, #8]
  207. mcount_adjust_addr \reg, \reg
  208. .endm
  209. .macro mcount_get_lr reg
  210. ldr \reg, [x29]
  211. ldr \reg, [\reg, #8]
  212. .endm
  213. .macro mcount_get_lr_addr reg
  214. ldr \reg, [x29]
  215. add \reg, \reg, #8
  216. .endm
  217. /*
  218. * _mcount() is used to build the kernel with -pg option, but all the branch
  219. * instructions to _mcount() are replaced to NOP initially at kernel start up,
  220. * and later on, NOP to branch to ftrace_caller() when enabled or branch to
  221. * NOP when disabled per-function base.
  222. */
  223. SYM_FUNC_START(_mcount)
  224. ret
  225. SYM_FUNC_END(_mcount)
  226. EXPORT_SYMBOL(_mcount)
  227. NOKPROBE(_mcount)
  228. /*
  229. * void ftrace_caller(unsigned long return_address)
  230. * @return_address: return address to instrumented function
  231. *
  232. * This function is a counterpart of _mcount() in 'static' ftrace, and
  233. * makes calls to:
  234. * - tracer function to probe instrumented function's entry,
  235. * - ftrace_graph_caller to set up an exit hook
  236. */
  237. SYM_FUNC_START(ftrace_caller)
  238. mcount_enter
  239. mcount_get_pc0 x0 // function's pc
  240. mcount_get_lr x1 // function's lr
  241. SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL) // tracer(pc, lr);
  242. nop // This will be replaced with "bl xxx"
  243. // where xxx can be any kind of tracer.
  244. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  245. SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL) // ftrace_graph_caller();
  246. nop // If enabled, this will be replaced
  247. // "b ftrace_graph_caller"
  248. #endif
  249. mcount_exit
  250. SYM_FUNC_END(ftrace_caller)
  251. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  252. /*
  253. * void ftrace_graph_caller(void)
  254. *
  255. * Called from _mcount() or ftrace_caller() when function_graph tracer is
  256. * selected.
  257. * This function w/ prepare_ftrace_return() fakes link register's value on
  258. * the call stack in order to intercept instrumented function's return path
  259. * and run return_to_handler() later on its exit.
  260. */
  261. SYM_FUNC_START(ftrace_graph_caller)
  262. mcount_get_pc x0 // function's pc
  263. mcount_get_lr_addr x1 // pointer to function's saved lr
  264. mcount_get_parent_fp x2 // parent's fp
  265. bl prepare_ftrace_return // prepare_ftrace_return(pc, &lr, fp)
  266. mcount_exit
  267. SYM_FUNC_END(ftrace_graph_caller)
  268. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  269. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
  270. SYM_TYPED_FUNC_START(ftrace_stub)
  271. ret
  272. SYM_FUNC_END(ftrace_stub)
  273. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  274. SYM_TYPED_FUNC_START(ftrace_stub_graph)
  275. ret
  276. SYM_FUNC_END(ftrace_stub_graph)
  277. /*
  278. * void return_to_handler(void)
  279. *
  280. * Run ftrace_return_to_handler() before going back to parent.
  281. * @fp is checked against the value passed by ftrace_graph_caller().
  282. */
  283. SYM_CODE_START(return_to_handler)
  284. /* save return value regs */
  285. sub sp, sp, #FGRET_REGS_SIZE
  286. stp x0, x1, [sp, #FGRET_REGS_X0]
  287. stp x2, x3, [sp, #FGRET_REGS_X2]
  288. stp x4, x5, [sp, #FGRET_REGS_X4]
  289. stp x6, x7, [sp, #FGRET_REGS_X6]
  290. str x29, [sp, #FGRET_REGS_FP] // parent's fp
  291. mov x0, sp
  292. bl ftrace_return_to_handler // addr = ftrace_return_to_hander(regs);
  293. mov x30, x0 // restore the original return address
  294. /* restore return value regs */
  295. ldp x0, x1, [sp, #FGRET_REGS_X0]
  296. ldp x2, x3, [sp, #FGRET_REGS_X2]
  297. ldp x4, x5, [sp, #FGRET_REGS_X4]
  298. ldp x6, x7, [sp, #FGRET_REGS_X6]
  299. add sp, sp, #FGRET_REGS_SIZE
  300. ret
  301. SYM_CODE_END(return_to_handler)
  302. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */