mcount_64.S 7.8 KB

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