ftrace_64.S 8.0 KB

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