ftrace_64.S 8.3 KB

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