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>
10 #include <asm/export.h>
14 .section .entry.text, "ax"
17 #ifdef CONFIG_FUNCTION_TRACER
19 #ifdef CC_USING_FENTRY
20 # define function_hook __fentry__
22 # define function_hook mcount
25 /* All cases save the original rbp (8 bytes) */
26 #ifdef CONFIG_FRAME_POINTER
27 # ifdef CC_USING_FENTRY
28 /* Save parent and function stack frames (rip and rbp) */
29 # define MCOUNT_FRAME_SIZE (8+16*2)
31 /* Save just function stack frame (rip and rbp) */
32 # define MCOUNT_FRAME_SIZE (8+16)
35 /* No need to save a stack frame */
36 # define MCOUNT_FRAME_SIZE 8
37 #endif /* CONFIG_FRAME_POINTER */
39 /* Size of stack used to save mcount regs in save_mcount_regs */
40 #define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
43 * gcc -pg option adds a call to 'mcount' in most functions.
44 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
45 * and is done before the function's stack frame is set up.
46 * They both require a set of regs to be saved before calling
47 * any C code and restored before returning back to the function.
49 * On boot up, all these calls are converted into nops. When tracing
50 * is enabled, the call can jump to either ftrace_caller or
51 * ftrace_regs_caller. Callbacks (tracing functions) that require
52 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
53 * it. For this reason, the size of the pt_regs structure will be
54 * allocated on the stack and the required mcount registers will
55 * be saved in the locations that pt_regs has them in.
59 * @added: the amount of stack added before calling this
61 * After this is called, the following registers contain:
63 * %rdi - holds the address that called the trampoline
64 * %rsi - holds the parent function (traced function's return address)
65 * %rdx - holds the original %rbp
67 .macro save_mcount_regs added=0
69 /* Always save the original rbp */
72 #ifdef CONFIG_FRAME_POINTER
74 * Stack traces will stop at the ftrace trampoline if the frame pointer
75 * is not set up properly. If fentry is used, we need to save a frame
76 * pointer for the parent as well as the function traced, because the
77 * fentry is called before the stack frame is set up, where as mcount
78 * is called afterward.
80 #ifdef CC_USING_FENTRY
81 /* Save the parent pointer (skip orig rbp and our return address) */
82 pushq \added+8*2(%rsp)
85 /* Save the return address (now skip orig rbp, rbp and parent) */
86 pushq \added+8*3(%rsp)
88 /* Can't assume that rip is before this (unless added was zero) */
93 #endif /* CONFIG_FRAME_POINTER */
96 * We add enough stack to save all regs.
98 subq $(MCOUNT_REG_SIZE - MCOUNT_FRAME_SIZE), %rsp
107 * Save the original RBP. Even though the mcount ABI does not
108 * require this, it helps out callers.
110 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
113 /* Copy the parent address into %rsi (second parameter) */
114 #ifdef CC_USING_FENTRY
115 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
117 /* %rdx contains original %rbp */
121 /* Move RIP to its proper location */
122 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
126 * Now %rdi (the first parameter) has the return address of
127 * where ftrace_call returns. But the callbacks expect the
128 * address of the call itself.
130 subq $MCOUNT_INSN_SIZE, %rdi
133 .macro restore_mcount_regs
142 /* ftrace_regs_caller can modify %rbp */
145 addq $MCOUNT_REG_SIZE, %rsp
149 #ifdef CONFIG_DYNAMIC_FTRACE
156 /* save_mcount_regs fills in first two parameters */
159 GLOBAL(ftrace_caller_op_ptr)
160 /* Load the ftrace_ops into the 3rd parameter */
161 movq function_trace_op(%rip), %rdx
163 /* regs go into 4th parameter (but make it NULL) */
172 * The copied trampoline must call ftrace_epilogue as it
173 * still may need to call the function graph tracer.
175 * The code up to this label is copied into trampolines so
176 * think twice before adding any new code or changing the
179 GLOBAL(ftrace_epilogue)
181 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
182 GLOBAL(ftrace_graph_call)
186 /* This is weak to keep gas from relaxing the jumps */
191 ENTRY(ftrace_regs_caller)
192 /* Save the current flags before any operations that can change them */
195 /* added 8 bytes to save flags */
197 /* save_mcount_regs fills in first two parameters */
199 GLOBAL(ftrace_regs_caller_op_ptr)
200 /* Load the ftrace_ops into the 3rd parameter */
201 movq function_trace_op(%rip), %rdx
203 /* Save the rest of pt_regs */
211 /* Copy saved flags */
212 movq MCOUNT_REG_SIZE(%rsp), %rcx
213 movq %rcx, EFLAGS(%rsp)
214 /* Kernel segments */
215 movq $__KERNEL_DS, %rcx
217 movq $__KERNEL_CS, %rcx
219 /* Stack - skipping return address and flags */
220 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
223 /* regs go into 4th parameter */
226 GLOBAL(ftrace_regs_call)
229 /* Copy flags back to SS, to restore them */
230 movq EFLAGS(%rsp), %rax
231 movq %rax, MCOUNT_REG_SIZE(%rsp)
233 /* Handlers can change the RIP */
235 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
237 /* restore the rest of pt_regs */
251 * As this jmp to ftrace_epilogue can be a short jump
252 * it must not be copied into the trampoline.
253 * The trampoline will add the code to jump
256 GLOBAL(ftrace_regs_caller_end)
260 END(ftrace_regs_caller)
263 #else /* ! CONFIG_DYNAMIC_FTRACE */
266 cmpq $ftrace_stub, ftrace_trace_function
270 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
271 cmpq $ftrace_stub, ftrace_graph_return
272 jnz ftrace_graph_caller
274 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
275 jnz ftrace_graph_caller
282 /* save_mcount_regs fills in first two parameters */
286 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
287 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
288 * ip and parent ip are used and the list function is called when
289 * function tracing is enabled.
291 call *ftrace_trace_function
297 #endif /* CONFIG_DYNAMIC_FTRACE */
298 EXPORT_SYMBOL(function_hook)
299 #endif /* CONFIG_FUNCTION_TRACER */
301 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
302 ENTRY(ftrace_graph_caller)
303 /* Saves rbp into %rdx and fills first parameter */
306 #ifdef CC_USING_FENTRY
307 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
308 movq $0, %rdx /* No framepointers needed */
310 /* Save address of the return address of traced function */
312 /* ftrace does sanity checks against frame pointers */
315 call prepare_ftrace_return
320 END(ftrace_graph_caller)
322 GLOBAL(return_to_handler)
325 /* Save the return values */
330 call ftrace_return_to_handler