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 #ifdef CC_USING_FENTRY
17 # define function_hook __fentry__
18 EXPORT_SYMBOL(__fentry__)
20 # define function_hook mcount
24 #ifdef CONFIG_FRAME_POINTER
25 # ifdef CC_USING_FENTRY
26 /* Save parent and function stack frames (rip and rbp) */
27 # define MCOUNT_FRAME_SIZE (8+16*2)
29 /* Save just function stack frame (rip and rbp) */
30 # define MCOUNT_FRAME_SIZE (8+16)
33 /* No need to save a stack frame */
34 # define MCOUNT_FRAME_SIZE 0
35 #endif /* CONFIG_FRAME_POINTER */
37 /* Size of stack used to save mcount regs in save_mcount_regs */
38 #define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
41 * gcc -pg option adds a call to 'mcount' in most functions.
42 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
43 * and is done before the function's stack frame is set up.
44 * They both require a set of regs to be saved before calling
45 * any C code and restored before returning back to the function.
47 * On boot up, all these calls are converted into nops. When tracing
48 * is enabled, the call can jump to either ftrace_caller or
49 * ftrace_regs_caller. Callbacks (tracing functions) that require
50 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
51 * it. For this reason, the size of the pt_regs structure will be
52 * allocated on the stack and the required mcount registers will
53 * be saved in the locations that pt_regs has them in.
57 * @added: the amount of stack added before calling this
59 * After this is called, the following registers contain:
61 * %rdi - holds the address that called the trampoline
62 * %rsi - holds the parent function (traced function's return address)
63 * %rdx - holds the original %rbp
65 .macro save_mcount_regs added=0
67 #ifdef CONFIG_FRAME_POINTER
68 /* Save the original rbp */
72 * Stack traces will stop at the ftrace trampoline if the frame pointer
73 * is not set up properly. If fentry is used, we need to save a frame
74 * pointer for the parent as well as the function traced, because the
75 * fentry is called before the stack frame is set up, where as mcount
76 * is called afterward.
78 #ifdef CC_USING_FENTRY
79 /* Save the parent pointer (skip orig rbp and our return address) */
80 pushq \added+8*2(%rsp)
83 /* Save the return address (now skip orig rbp, rbp and parent) */
84 pushq \added+8*3(%rsp)
86 /* Can't assume that rip is before this (unless added was zero) */
91 #endif /* CONFIG_FRAME_POINTER */
94 * We add enough stack to save all regs.
96 subq $(MCOUNT_REG_SIZE - MCOUNT_FRAME_SIZE), %rsp
105 * Save the original RBP. Even though the mcount ABI does not
106 * require this, it helps out callers.
108 #ifdef CONFIG_FRAME_POINTER
109 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
115 /* Copy the parent address into %rsi (second parameter) */
116 #ifdef CC_USING_FENTRY
117 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
119 /* %rdx contains original %rbp */
123 /* Move RIP to its proper location */
124 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
128 * Now %rdi (the first parameter) has the return address of
129 * where ftrace_call returns. But the callbacks expect the
130 * address of the call itself.
132 subq $MCOUNT_INSN_SIZE, %rdi
135 .macro restore_mcount_regs
144 /* ftrace_regs_caller can modify %rbp */
147 addq $MCOUNT_REG_SIZE, %rsp
151 #ifdef CONFIG_DYNAMIC_FTRACE
155 ENDPROC(function_hook)
158 /* save_mcount_regs fills in first two parameters */
161 GLOBAL(ftrace_caller_op_ptr)
162 /* Load the ftrace_ops into the 3rd parameter */
163 movq function_trace_op(%rip), %rdx
165 /* regs go into 4th parameter (but make it NULL) */
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)
186 * This is weak to keep gas from relaxing the jumps.
187 * It is also used to copy the retq for trampolines.
191 ENDPROC(ftrace_caller)
193 ENTRY(ftrace_regs_caller)
194 /* Save the current flags before any operations that can change them */
197 /* added 8 bytes to save flags */
199 /* save_mcount_regs fills in first two parameters */
201 GLOBAL(ftrace_regs_caller_op_ptr)
202 /* Load the ftrace_ops into the 3rd parameter */
203 movq function_trace_op(%rip), %rdx
205 /* Save the rest of pt_regs */
213 /* Copy saved flags */
214 movq MCOUNT_REG_SIZE(%rsp), %rcx
215 movq %rcx, EFLAGS(%rsp)
216 /* Kernel segments */
217 movq $__KERNEL_DS, %rcx
219 movq $__KERNEL_CS, %rcx
221 /* Stack - skipping return address and flags */
222 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
225 /* regs go into 4th parameter */
228 GLOBAL(ftrace_regs_call)
231 /* Copy flags back to SS, to restore them */
232 movq EFLAGS(%rsp), %rax
233 movq %rax, MCOUNT_REG_SIZE(%rsp)
235 /* Handlers can change the RIP */
237 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
239 /* restore the rest of pt_regs */
253 * As this jmp to ftrace_epilogue can be a short jump
254 * it must not be copied into the trampoline.
255 * The trampoline will add the code to jump
258 GLOBAL(ftrace_regs_caller_end)
262 ENDPROC(ftrace_regs_caller)
265 #else /* ! CONFIG_DYNAMIC_FTRACE */
268 cmpq $ftrace_stub, ftrace_trace_function
272 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
273 cmpq $ftrace_stub, ftrace_graph_return
274 jnz ftrace_graph_caller
276 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
277 jnz ftrace_graph_caller
284 /* save_mcount_regs fills in first two parameters */
288 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
289 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
290 * ip and parent ip are used and the list function is called when
291 * function tracing is enabled.
293 movq ftrace_trace_function, %r8
298 ENDPROC(function_hook)
299 #endif /* CONFIG_DYNAMIC_FTRACE */
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 ENDPROC(ftrace_graph_caller)
322 ENTRY(return_to_handler)
326 /* Save the return values */
331 call ftrace_return_to_handler
338 END(return_to_handler)