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>
14 .section .entry.text, "ax"
16 # define function_hook __fentry__
17 EXPORT_SYMBOL(__fentry__)
19 #ifdef CONFIG_FRAME_POINTER
20 /* Save parent and function stack frames (rip and rbp) */
21 # define MCOUNT_FRAME_SIZE (8+16*2)
23 /* No need to save a stack frame */
24 # define MCOUNT_FRAME_SIZE 0
25 #endif /* CONFIG_FRAME_POINTER */
27 /* Size of stack used to save mcount regs in save_mcount_regs */
28 #define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
31 * gcc -pg option adds a call to 'mcount' in most functions.
32 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
33 * and is done before the function's stack frame is set up.
34 * They both require a set of regs to be saved before calling
35 * any C code and restored before returning back to the function.
37 * On boot up, all these calls are converted into nops. When tracing
38 * is enabled, the call can jump to either ftrace_caller or
39 * ftrace_regs_caller. Callbacks (tracing functions) that require
40 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
41 * it. For this reason, the size of the pt_regs structure will be
42 * allocated on the stack and the required mcount registers will
43 * be saved in the locations that pt_regs has them in.
47 * @added: the amount of stack added before calling this
49 * After this is called, the following registers contain:
51 * %rdi - holds the address that called the trampoline
52 * %rsi - holds the parent function (traced function's return address)
53 * %rdx - holds the original %rbp
55 .macro save_mcount_regs added=0
57 #ifdef CONFIG_FRAME_POINTER
58 /* Save the original rbp */
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.
69 /* Save the parent pointer (skip orig rbp and our return address) */
70 pushq \added+8*2(%rsp)
73 /* Save the return address (now skip orig rbp, rbp and parent) */
74 pushq \added+8*3(%rsp)
77 #endif /* CONFIG_FRAME_POINTER */
80 * We add enough stack to save all regs.
82 subq $(MCOUNT_REG_SIZE - MCOUNT_FRAME_SIZE), %rsp
91 * Save the original RBP. Even though the mcount ABI does not
92 * require this, it helps out callers.
94 #ifdef CONFIG_FRAME_POINTER
95 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
101 /* Copy the parent address into %rsi (second parameter) */
102 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
104 /* Move RIP to its proper location */
105 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
109 * Now %rdi (the first parameter) has the return address of
110 * where ftrace_call returns. But the callbacks expect the
111 * address of the call itself.
113 subq $MCOUNT_INSN_SIZE, %rdi
116 .macro restore_mcount_regs
125 /* ftrace_regs_caller can modify %rbp */
128 addq $MCOUNT_REG_SIZE, %rsp
132 #ifdef CONFIG_DYNAMIC_FTRACE
136 ENDPROC(function_hook)
139 /* save_mcount_regs fills in first two parameters */
142 GLOBAL(ftrace_caller_op_ptr)
143 /* Load the ftrace_ops into the 3rd parameter */
144 movq function_trace_op(%rip), %rdx
146 /* regs go into 4th parameter (but make it NULL) */
155 * The code up to this label is copied into trampolines so
156 * think twice before adding any new code or changing the
159 GLOBAL(ftrace_epilogue)
161 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
162 GLOBAL(ftrace_graph_call)
167 * This is weak to keep gas from relaxing the jumps.
168 * It is also used to copy the retq for trampolines.
172 ENDPROC(ftrace_caller)
174 ENTRY(ftrace_regs_caller)
175 /* Save the current flags before any operations that can change them */
178 /* added 8 bytes to save flags */
180 /* save_mcount_regs fills in first two parameters */
182 GLOBAL(ftrace_regs_caller_op_ptr)
183 /* Load the ftrace_ops into the 3rd parameter */
184 movq function_trace_op(%rip), %rdx
186 /* Save the rest of pt_regs */
194 /* Copy saved flags */
195 movq MCOUNT_REG_SIZE(%rsp), %rcx
196 movq %rcx, EFLAGS(%rsp)
197 /* Kernel segments */
198 movq $__KERNEL_DS, %rcx
200 movq $__KERNEL_CS, %rcx
202 /* Stack - skipping return address and flags */
203 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
206 /* regs go into 4th parameter */
209 GLOBAL(ftrace_regs_call)
212 /* Copy flags back to SS, to restore them */
213 movq EFLAGS(%rsp), %rax
214 movq %rax, MCOUNT_REG_SIZE(%rsp)
216 /* Handlers can change the RIP */
218 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
220 /* restore the rest of pt_regs */
234 * As this jmp to ftrace_epilogue can be a short jump
235 * it must not be copied into the trampoline.
236 * The trampoline will add the code to jump
239 GLOBAL(ftrace_regs_caller_end)
243 ENDPROC(ftrace_regs_caller)
246 #else /* ! CONFIG_DYNAMIC_FTRACE */
249 cmpq $ftrace_stub, ftrace_trace_function
253 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
254 cmpq $ftrace_stub, ftrace_graph_return
255 jnz ftrace_graph_caller
257 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
258 jnz ftrace_graph_caller
265 /* save_mcount_regs fills in first two parameters */
269 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
270 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
271 * ip and parent ip are used and the list function is called when
272 * function tracing is enabled.
274 movq ftrace_trace_function, %r8
279 ENDPROC(function_hook)
280 #endif /* CONFIG_DYNAMIC_FTRACE */
282 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
283 ENTRY(ftrace_graph_caller)
284 /* Saves rbp into %rdx and fills first parameter */
287 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
288 movq $0, %rdx /* No framepointers needed */
289 call prepare_ftrace_return
294 ENDPROC(ftrace_graph_caller)
296 ENTRY(return_to_handler)
300 /* Save the return values */
305 call ftrace_return_to_handler
312 END(return_to_handler)