2 * linux/arch/x86_64/mcount_64.S
4 * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
7 #include <linux/linkage.h>
8 #include <asm/ptrace.h>
9 #include <asm/ftrace.h>
13 .section .entry.text, "ax"
16 #ifdef CONFIG_FUNCTION_TRACER
18 #ifdef CC_USING_FENTRY
19 # define function_hook __fentry__
21 # define function_hook mcount
24 /* All cases save the original rbp (8 bytes) */
25 #ifdef CONFIG_FRAME_POINTER
26 # ifdef CC_USING_FENTRY
27 /* Save parent and function stack frames (rip and rbp) */
28 # define MCOUNT_FRAME_SIZE (8+16*2)
30 /* Save just function stack frame (rip and rbp) */
31 # define MCOUNT_FRAME_SIZE (8+16)
34 /* No need to save a stack frame */
35 # define MCOUNT_FRAME_SIZE 8
36 #endif /* CONFIG_FRAME_POINTER */
38 /* Size of stack used to save mcount regs in save_mcount_regs */
39 #define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
42 * gcc -pg option adds a call to 'mcount' in most functions.
43 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
44 * and is done before the function's stack frame is set up.
45 * They both require a set of regs to be saved before calling
46 * any C code and restored before returning back to the function.
48 * On boot up, all these calls are converted into nops. When tracing
49 * is enabled, the call can jump to either ftrace_caller or
50 * ftrace_regs_caller. Callbacks (tracing functions) that require
51 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
52 * it. For this reason, the size of the pt_regs structure will be
53 * allocated on the stack and the required mcount registers will
54 * be saved in the locations that pt_regs has them in.
58 * @added: the amount of stack added before calling this
60 * After this is called, the following registers contain:
62 * %rdi - holds the address that called the trampoline
63 * %rsi - holds the parent function (traced function's return address)
64 * %rdx - holds the original %rbp
66 .macro save_mcount_regs added=0
68 /* Always save the original rbp */
71 #ifdef CONFIG_FRAME_POINTER
73 * Stack traces will stop at the ftrace trampoline if the frame pointer
74 * is not set up properly. If fentry is used, we need to save a frame
75 * pointer for the parent as well as the function traced, because the
76 * fentry is called before the stack frame is set up, where as mcount
77 * is called afterward.
79 #ifdef CC_USING_FENTRY
80 /* Save the parent pointer (skip orig rbp and our return address) */
81 pushq \added+8*2(%rsp)
84 /* Save the return address (now skip orig rbp, rbp and parent) */
85 pushq \added+8*3(%rsp)
87 /* Can't assume that rip is before this (unless added was zero) */
92 #endif /* CONFIG_FRAME_POINTER */
95 * We add enough stack to save all regs.
97 subq $(MCOUNT_REG_SIZE - MCOUNT_FRAME_SIZE), %rsp
106 * Save the original RBP. Even though the mcount ABI does not
107 * require this, it helps out callers.
109 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
112 /* Copy the parent address into %rsi (second parameter) */
113 #ifdef CC_USING_FENTRY
114 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
116 /* %rdx contains original %rbp */
120 /* Move RIP to its proper location */
121 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
125 * Now %rdi (the first parameter) has the return address of
126 * where ftrace_call returns. But the callbacks expect the
127 * address of the call itself.
129 subq $MCOUNT_INSN_SIZE, %rdi
132 .macro restore_mcount_regs
141 /* ftrace_regs_caller can modify %rbp */
144 addq $MCOUNT_REG_SIZE, %rsp
148 #ifdef CONFIG_DYNAMIC_FTRACE
155 /* save_mcount_regs fills in first two parameters */
158 GLOBAL(ftrace_caller_op_ptr)
159 /* Load the ftrace_ops into the 3rd parameter */
160 movq function_trace_op(%rip), %rdx
162 /* regs go into 4th parameter (but make it NULL) */
171 * The copied trampoline must call ftrace_epilogue as it
172 * still may need to call the function graph tracer.
174 * The code up to this label is copied into trampolines so
175 * think twice before adding any new code or changing the
178 GLOBAL(ftrace_epilogue)
180 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
181 GLOBAL(ftrace_graph_call)
185 /* This is weak to keep gas from relaxing the jumps */
190 ENTRY(ftrace_regs_caller)
191 /* Save the current flags before any operations that can change them */
194 /* added 8 bytes to save flags */
196 /* save_mcount_regs fills in first two parameters */
198 GLOBAL(ftrace_regs_caller_op_ptr)
199 /* Load the ftrace_ops into the 3rd parameter */
200 movq function_trace_op(%rip), %rdx
202 /* Save the rest of pt_regs */
210 /* Copy saved flags */
211 movq MCOUNT_REG_SIZE(%rsp), %rcx
212 movq %rcx, EFLAGS(%rsp)
213 /* Kernel segments */
214 movq $__KERNEL_DS, %rcx
216 movq $__KERNEL_CS, %rcx
218 /* Stack - skipping return address and flags */
219 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
222 /* regs go into 4th parameter */
225 GLOBAL(ftrace_regs_call)
228 /* Copy flags back to SS, to restore them */
229 movq EFLAGS(%rsp), %rax
230 movq %rax, MCOUNT_REG_SIZE(%rsp)
232 /* Handlers can change the RIP */
234 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
236 /* restore the rest of pt_regs */
250 * As this jmp to ftrace_epilogue can be a short jump
251 * it must not be copied into the trampoline.
252 * The trampoline will add the code to jump
255 GLOBAL(ftrace_regs_caller_end)
259 END(ftrace_regs_caller)
262 #else /* ! CONFIG_DYNAMIC_FTRACE */
265 cmpq $ftrace_stub, ftrace_trace_function
269 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
270 cmpq $ftrace_stub, ftrace_graph_return
271 jnz ftrace_graph_caller
273 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
274 jnz ftrace_graph_caller
281 /* save_mcount_regs fills in first two parameters */
285 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
286 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
287 * ip and parent ip are used and the list function is called when
288 * function tracing is enabled.
290 call *ftrace_trace_function
296 #endif /* CONFIG_DYNAMIC_FTRACE */
297 #endif /* CONFIG_FUNCTION_TRACER */
299 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
300 ENTRY(ftrace_graph_caller)
301 /* Saves rbp into %rdx and fills first parameter */
304 #ifdef CC_USING_FENTRY
305 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
306 movq $0, %rdx /* No framepointers needed */
308 /* Save address of the return address of traced function */
310 /* ftrace does sanity checks against frame pointers */
313 call prepare_ftrace_return
318 END(ftrace_graph_caller)
320 GLOBAL(return_to_handler)
323 /* Save the return values */
328 call ftrace_return_to_handler