mcount_64.S 8.0 KB

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