ftrace_64.S 8.3 KB

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