1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
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 #include <asm/unwind_hints.h>
12 #include <asm/frame.h>
15 .section .entry.text, "ax"
17 # define function_hook __fentry__
18 EXPORT_SYMBOL(__fentry__)
20 #ifdef CONFIG_FRAME_POINTER
21 /* Save parent and function stack frames (rip and rbp) */
22 # define MCOUNT_FRAME_SIZE (8+16*2)
24 /* No need to save a stack frame */
25 # define MCOUNT_FRAME_SIZE 0
26 #endif /* CONFIG_FRAME_POINTER */
28 /* Size of stack used to save mcount regs in save_mcount_regs */
29 #define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
32 * gcc -pg option adds a call to 'mcount' in most functions.
33 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
34 * and is done before the function's stack frame is set up.
35 * They both require a set of regs to be saved before calling
36 * any C code and restored before returning back to the function.
38 * On boot up, all these calls are converted into nops. When tracing
39 * is enabled, the call can jump to either ftrace_caller or
40 * ftrace_regs_caller. Callbacks (tracing functions) that require
41 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
42 * it. For this reason, the size of the pt_regs structure will be
43 * allocated on the stack and the required mcount registers will
44 * be saved in the locations that pt_regs has them in.
48 * @added: the amount of stack added before calling this
50 * After this is called, the following registers contain:
52 * %rdi - holds the address that called the trampoline
53 * %rsi - holds the parent function (traced function's return address)
54 * %rdx - holds the original %rbp
56 .macro save_mcount_regs added=0
58 #ifdef CONFIG_FRAME_POINTER
59 /* Save the original rbp */
63 * Stack traces will stop at the ftrace trampoline if the frame pointer
64 * is not set up properly. If fentry is used, we need to save a frame
65 * pointer for the parent as well as the function traced, because the
66 * fentry is called before the stack frame is set up, where as mcount
67 * is called afterward.
70 /* Save the parent pointer (skip orig rbp and our return address) */
71 pushq \added+8*2(%rsp)
74 /* Save the return address (now skip orig rbp, rbp and parent) */
75 pushq \added+8*3(%rsp)
78 #endif /* CONFIG_FRAME_POINTER */
81 * We add enough stack to save all regs.
83 subq $(MCOUNT_REG_SIZE - MCOUNT_FRAME_SIZE), %rsp
92 * Save the original RBP. Even though the mcount ABI does not
93 * require this, it helps out callers.
95 #ifdef CONFIG_FRAME_POINTER
96 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
102 /* Copy the parent address into %rsi (second parameter) */
103 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
105 /* Move RIP to its proper location */
106 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
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.
114 subq $MCOUNT_INSN_SIZE, %rdi
117 .macro restore_mcount_regs
126 /* ftrace_regs_caller can modify %rbp */
129 addq $MCOUNT_REG_SIZE, %rsp
133 #ifdef CONFIG_DYNAMIC_FTRACE
137 ENDPROC(function_hook)
140 /* save_mcount_regs fills in first two parameters */
143 GLOBAL(ftrace_caller_op_ptr)
144 /* Load the ftrace_ops into the 3rd parameter */
145 movq function_trace_op(%rip), %rdx
147 /* regs go into 4th parameter (but make it NULL) */
156 * The code up to this label is copied into trampolines so
157 * think twice before adding any new code or changing the
160 GLOBAL(ftrace_epilogue)
162 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
163 GLOBAL(ftrace_graph_call)
168 * This is weak to keep gas from relaxing the jumps.
169 * It is also used to copy the retq for trampolines.
173 ENDPROC(ftrace_caller)
175 ENTRY(ftrace_regs_caller)
176 /* Save the current flags before any operations that can change them */
179 /* added 8 bytes to save flags */
181 /* save_mcount_regs fills in first two parameters */
183 GLOBAL(ftrace_regs_caller_op_ptr)
184 /* Load the ftrace_ops into the 3rd parameter */
185 movq function_trace_op(%rip), %rdx
187 /* Save the rest of pt_regs */
195 /* Copy saved flags */
196 movq MCOUNT_REG_SIZE(%rsp), %rcx
197 movq %rcx, EFLAGS(%rsp)
198 /* Kernel segments */
199 movq $__KERNEL_DS, %rcx
201 movq $__KERNEL_CS, %rcx
203 /* Stack - skipping return address and flags */
204 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
209 /* regs go into 4th parameter */
212 GLOBAL(ftrace_regs_call)
215 /* Copy flags back to SS, to restore them */
216 movq EFLAGS(%rsp), %rax
217 movq %rax, MCOUNT_REG_SIZE(%rsp)
219 /* Handlers can change the RIP */
221 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
223 /* restore the rest of pt_regs */
237 * As this jmp to ftrace_epilogue can be a short jump
238 * it must not be copied into the trampoline.
239 * The trampoline will add the code to jump
242 GLOBAL(ftrace_regs_caller_end)
246 ENDPROC(ftrace_regs_caller)
249 #else /* ! CONFIG_DYNAMIC_FTRACE */
252 cmpq $ftrace_stub, ftrace_trace_function
256 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
257 cmpq $ftrace_stub, ftrace_graph_return
258 jnz ftrace_graph_caller
260 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
261 jnz ftrace_graph_caller
268 /* save_mcount_regs fills in first two parameters */
272 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
273 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
274 * ip and parent ip are used and the list function is called when
275 * function tracing is enabled.
277 movq ftrace_trace_function, %r8
282 ENDPROC(function_hook)
283 #endif /* CONFIG_DYNAMIC_FTRACE */
285 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
286 ENTRY(ftrace_graph_caller)
287 /* Saves rbp into %rdx and fills first parameter */
290 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
291 movq $0, %rdx /* No framepointers needed */
292 call prepare_ftrace_return
297 ENDPROC(ftrace_graph_caller)
299 ENTRY(return_to_handler)
303 /* Save the return values */
308 call ftrace_return_to_handler
315 END(return_to_handler)