ftrace_64.S 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
  4. */
  5. #include <linux/export.h>
  6. #include <linux/cfi_types.h>
  7. #include <linux/linkage.h>
  8. #include <asm/asm-offsets.h>
  9. #include <asm/ptrace.h>
  10. #include <asm/ftrace.h>
  11. #include <asm/nospec-branch.h>
  12. #include <asm/unwind_hints.h>
  13. #include <asm/frame.h>
  14. .code64
  15. .section .text, "ax"
  16. #ifdef CONFIG_FRAME_POINTER
  17. /* Save parent and function stack frames (rip and rbp) */
  18. # define MCOUNT_FRAME_SIZE (8+16*2)
  19. #else
  20. /* No need to save a stack frame */
  21. # define MCOUNT_FRAME_SIZE 0
  22. #endif /* CONFIG_FRAME_POINTER */
  23. /* Size of stack used to save mcount regs in save_mcount_regs */
  24. #define MCOUNT_REG_SIZE (FRAME_SIZE + MCOUNT_FRAME_SIZE)
  25. /*
  26. * gcc -pg option adds a call to 'mcount' in most functions.
  27. * When -mfentry is used, the call is to 'fentry' and not 'mcount'
  28. * and is done before the function's stack frame is set up.
  29. * They both require a set of regs to be saved before calling
  30. * any C code and restored before returning back to the function.
  31. *
  32. * On boot up, all these calls are converted into nops. When tracing
  33. * is enabled, the call can jump to either ftrace_caller or
  34. * ftrace_regs_caller. Callbacks (tracing functions) that require
  35. * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
  36. * it. For this reason, the size of the pt_regs structure will be
  37. * allocated on the stack and the required mcount registers will
  38. * be saved in the locations that pt_regs has them in.
  39. */
  40. /*
  41. * @added: the amount of stack added before calling this
  42. *
  43. * After this is called, the following registers contain:
  44. *
  45. * %rdi - holds the address that called the trampoline
  46. * %rsi - holds the parent function (traced function's return address)
  47. * %rdx - holds the original %rbp
  48. */
  49. .macro save_mcount_regs added=0
  50. #ifdef CONFIG_FRAME_POINTER
  51. /* Save the original rbp */
  52. pushq %rbp
  53. /*
  54. * Stack traces will stop at the ftrace trampoline if the frame pointer
  55. * is not set up properly. If fentry is used, we need to save a frame
  56. * pointer for the parent as well as the function traced, because the
  57. * fentry is called before the stack frame is set up, where as mcount
  58. * is called afterward.
  59. */
  60. /* Save the parent pointer (skip orig rbp and our return address) */
  61. pushq \added+8*2(%rsp)
  62. pushq %rbp
  63. movq %rsp, %rbp
  64. /* Save the return address (now skip orig rbp, rbp and parent) */
  65. pushq \added+8*3(%rsp)
  66. pushq %rbp
  67. movq %rsp, %rbp
  68. #endif /* CONFIG_FRAME_POINTER */
  69. /*
  70. * We add enough stack to save all regs.
  71. */
  72. subq $(FRAME_SIZE), %rsp
  73. movq %rax, RAX(%rsp)
  74. movq %rcx, RCX(%rsp)
  75. movq %rdx, RDX(%rsp)
  76. movq %rsi, RSI(%rsp)
  77. movq %rdi, RDI(%rsp)
  78. movq %r8, R8(%rsp)
  79. movq %r9, R9(%rsp)
  80. movq $0, ORIG_RAX(%rsp)
  81. /*
  82. * Save the original RBP. Even though the mcount ABI does not
  83. * require this, it helps out callers.
  84. */
  85. #ifdef CONFIG_FRAME_POINTER
  86. movq MCOUNT_REG_SIZE-8(%rsp), %rdx
  87. #else
  88. movq %rbp, %rdx
  89. #endif
  90. movq %rdx, RBP(%rsp)
  91. /* Copy the parent address into %rsi (second parameter) */
  92. movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
  93. /* Move RIP to its proper location */
  94. movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
  95. movq %rdi, RIP(%rsp)
  96. /*
  97. * Now %rdi (the first parameter) has the return address of
  98. * where ftrace_call returns. But the callbacks expect the
  99. * address of the call itself.
  100. */
  101. subq $MCOUNT_INSN_SIZE, %rdi
  102. .endm
  103. .macro restore_mcount_regs save=0
  104. /* ftrace_regs_caller or frame pointers require this */
  105. movq RBP(%rsp), %rbp
  106. movq R9(%rsp), %r9
  107. movq R8(%rsp), %r8
  108. movq RDI(%rsp), %rdi
  109. movq RSI(%rsp), %rsi
  110. movq RDX(%rsp), %rdx
  111. movq RCX(%rsp), %rcx
  112. movq RAX(%rsp), %rax
  113. addq $MCOUNT_REG_SIZE-\save, %rsp
  114. .endm
  115. SYM_TYPED_FUNC_START(ftrace_stub)
  116. CALL_DEPTH_ACCOUNT
  117. RET
  118. SYM_FUNC_END(ftrace_stub)
  119. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  120. SYM_TYPED_FUNC_START(ftrace_stub_graph)
  121. CALL_DEPTH_ACCOUNT
  122. RET
  123. SYM_FUNC_END(ftrace_stub_graph)
  124. #endif
  125. #ifdef CONFIG_DYNAMIC_FTRACE
  126. SYM_FUNC_START(__fentry__)
  127. CALL_DEPTH_ACCOUNT
  128. RET
  129. SYM_FUNC_END(__fentry__)
  130. EXPORT_SYMBOL(__fentry__)
  131. SYM_FUNC_START(ftrace_caller)
  132. /* save_mcount_regs fills in first two parameters */
  133. save_mcount_regs
  134. CALL_DEPTH_ACCOUNT
  135. /* Stack - skipping return address of ftrace_caller */
  136. leaq MCOUNT_REG_SIZE+8(%rsp), %rcx
  137. movq %rcx, RSP(%rsp)
  138. SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
  139. ANNOTATE_NOENDBR
  140. /* Load the ftrace_ops into the 3rd parameter */
  141. movq function_trace_op(%rip), %rdx
  142. /* regs go into 4th parameter */
  143. leaq (%rsp), %rcx
  144. /* Only ops with REGS flag set should have CS register set */
  145. movq $0, CS(%rsp)
  146. /* Account for the function call below */
  147. CALL_DEPTH_ACCOUNT
  148. SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
  149. ANNOTATE_NOENDBR
  150. call ftrace_stub
  151. /* Handlers can change the RIP */
  152. movq RIP(%rsp), %rax
  153. movq %rax, MCOUNT_REG_SIZE(%rsp)
  154. restore_mcount_regs
  155. /*
  156. * The code up to this label is copied into trampolines so
  157. * think twice before adding any new code or changing the
  158. * layout here.
  159. */
  160. SYM_INNER_LABEL(ftrace_caller_end, SYM_L_GLOBAL)
  161. ANNOTATE_NOENDBR
  162. RET
  163. SYM_FUNC_END(ftrace_caller);
  164. STACK_FRAME_NON_STANDARD_FP(ftrace_caller)
  165. SYM_FUNC_START(ftrace_regs_caller)
  166. /* Save the current flags before any operations that can change them */
  167. pushfq
  168. /* added 8 bytes to save flags */
  169. save_mcount_regs 8
  170. /* save_mcount_regs fills in first two parameters */
  171. CALL_DEPTH_ACCOUNT
  172. SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
  173. ANNOTATE_NOENDBR
  174. /* Load the ftrace_ops into the 3rd parameter */
  175. movq function_trace_op(%rip), %rdx
  176. /* Save the rest of pt_regs */
  177. movq %r15, R15(%rsp)
  178. movq %r14, R14(%rsp)
  179. movq %r13, R13(%rsp)
  180. movq %r12, R12(%rsp)
  181. movq %r11, R11(%rsp)
  182. movq %r10, R10(%rsp)
  183. movq %rbx, RBX(%rsp)
  184. /* Copy saved flags */
  185. movq MCOUNT_REG_SIZE(%rsp), %rcx
  186. movq %rcx, EFLAGS(%rsp)
  187. /* Kernel segments */
  188. movq $__KERNEL_DS, %rcx
  189. movq %rcx, SS(%rsp)
  190. movq $__KERNEL_CS, %rcx
  191. movq %rcx, CS(%rsp)
  192. /* Stack - skipping return address and flags */
  193. leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
  194. movq %rcx, RSP(%rsp)
  195. ENCODE_FRAME_POINTER
  196. /* regs go into 4th parameter */
  197. leaq (%rsp), %rcx
  198. /* Account for the function call below */
  199. CALL_DEPTH_ACCOUNT
  200. SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
  201. ANNOTATE_NOENDBR
  202. call ftrace_stub
  203. /* Copy flags back to SS, to restore them */
  204. movq EFLAGS(%rsp), %rax
  205. movq %rax, MCOUNT_REG_SIZE(%rsp)
  206. /* Handlers can change the RIP */
  207. movq RIP(%rsp), %rax
  208. movq %rax, MCOUNT_REG_SIZE+8(%rsp)
  209. /* restore the rest of pt_regs */
  210. movq R15(%rsp), %r15
  211. movq R14(%rsp), %r14
  212. movq R13(%rsp), %r13
  213. movq R12(%rsp), %r12
  214. movq R10(%rsp), %r10
  215. movq RBX(%rsp), %rbx
  216. movq ORIG_RAX(%rsp), %rax
  217. movq %rax, MCOUNT_REG_SIZE-8(%rsp)
  218. /*
  219. * If ORIG_RAX is anything but zero, make this a call to that.
  220. * See arch_ftrace_set_direct_caller().
  221. */
  222. testq %rax, %rax
  223. SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
  224. ANNOTATE_NOENDBR
  225. jnz 1f
  226. restore_mcount_regs
  227. /* Restore flags */
  228. popfq
  229. /*
  230. * The trampoline will add the return.
  231. */
  232. SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
  233. ANNOTATE_NOENDBR
  234. RET
  235. /* Swap the flags with orig_rax */
  236. 1: movq MCOUNT_REG_SIZE(%rsp), %rdi
  237. movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
  238. movq %rax, MCOUNT_REG_SIZE(%rsp)
  239. restore_mcount_regs 8
  240. /* Restore flags */
  241. popfq
  242. UNWIND_HINT_FUNC
  243. /*
  244. * The above left an extra return value on the stack; effectively
  245. * doing a tail-call without using a register. This PUSH;RET
  246. * pattern unbalances the RSB, inject a pointless CALL to rebalance.
  247. */
  248. ANNOTATE_INTRA_FUNCTION_CALL
  249. CALL .Ldo_rebalance
  250. int3
  251. .Ldo_rebalance:
  252. add $8, %rsp
  253. ALTERNATIVE __stringify(RET), \
  254. __stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
  255. X86_FEATURE_CALL_DEPTH
  256. SYM_FUNC_END(ftrace_regs_caller)
  257. STACK_FRAME_NON_STANDARD_FP(ftrace_regs_caller)
  258. SYM_FUNC_START(ftrace_stub_direct_tramp)
  259. CALL_DEPTH_ACCOUNT
  260. RET
  261. SYM_FUNC_END(ftrace_stub_direct_tramp)
  262. #else /* ! CONFIG_DYNAMIC_FTRACE */
  263. SYM_FUNC_START(__fentry__)
  264. CALL_DEPTH_ACCOUNT
  265. cmpq $ftrace_stub, ftrace_trace_function
  266. jnz trace
  267. RET
  268. trace:
  269. /* save_mcount_regs fills in first two parameters */
  270. save_mcount_regs
  271. /*
  272. * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
  273. * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
  274. * ip and parent ip are used and the list function is called when
  275. * function tracing is enabled.
  276. */
  277. movq ftrace_trace_function, %r8
  278. CALL_NOSPEC r8
  279. restore_mcount_regs
  280. jmp ftrace_stub
  281. SYM_FUNC_END(__fentry__)
  282. EXPORT_SYMBOL(__fentry__)
  283. STACK_FRAME_NON_STANDARD_FP(__fentry__)
  284. #endif /* CONFIG_DYNAMIC_FTRACE */
  285. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  286. SYM_CODE_START(return_to_handler)
  287. UNWIND_HINT_UNDEFINED
  288. ANNOTATE_NOENDBR
  289. subq $24, %rsp
  290. /* Save the return values */
  291. movq %rax, (%rsp)
  292. movq %rdx, 8(%rsp)
  293. movq %rbp, 16(%rsp)
  294. movq %rsp, %rdi
  295. call ftrace_return_to_handler
  296. movq %rax, %rdi
  297. movq 8(%rsp), %rdx
  298. movq (%rsp), %rax
  299. addq $24, %rsp
  300. /*
  301. * Jump back to the old return address. This cannot be JMP_NOSPEC rdi
  302. * since IBT would demand that contain ENDBR, which simply isn't so for
  303. * return addresses. Use a retpoline here to keep the RSB balanced.
  304. */
  305. ANNOTATE_INTRA_FUNCTION_CALL
  306. call .Ldo_rop
  307. int3
  308. .Ldo_rop:
  309. mov %rdi, (%rsp)
  310. ALTERNATIVE __stringify(RET), \
  311. __stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
  312. X86_FEATURE_CALL_DEPTH
  313. SYM_CODE_END(return_to_handler)
  314. #endif