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
91 movq $0, ORIG_RAX(%rsp)
93 * Save the original RBP. Even though the mcount ABI does not
94 * require this, it helps out callers.
96 #ifdef CONFIG_FRAME_POINTER
97 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
103 /* Copy the parent address into %rsi (second parameter) */
104 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
106 /* Move RIP to its proper location */
107 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
111 * Now %rdi (the first parameter) has the return address of
112 * where ftrace_call returns. But the callbacks expect the
113 * address of the call itself.
115 subq $MCOUNT_INSN_SIZE, %rdi
118 .macro restore_mcount_regs save=0
120 /* ftrace_regs_caller or frame pointers require this */
131 addq $MCOUNT_REG_SIZE-\save, %rsp
135 #ifdef CONFIG_DYNAMIC_FTRACE
139 ENDPROC(function_hook)
142 /* save_mcount_regs fills in first two parameters */
145 GLOBAL(ftrace_caller_op_ptr)
146 /* Load the ftrace_ops into the 3rd parameter */
147 movq function_trace_op(%rip), %rdx
149 /* regs go into 4th parameter (but make it NULL) */
158 * The code up to this label is copied into trampolines so
159 * think twice before adding any new code or changing the
162 GLOBAL(ftrace_epilogue)
164 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
165 GLOBAL(ftrace_graph_call)
170 * This is weak to keep gas from relaxing the jumps.
171 * It is also used to copy the retq for trampolines.
175 ENDPROC(ftrace_caller)
177 ENTRY(ftrace_regs_caller)
178 /* Save the current flags before any operations that can change them */
183 /* added 8 bytes to save flags */
185 /* save_mcount_regs fills in first two parameters */
187 GLOBAL(ftrace_regs_caller_op_ptr)
188 /* Load the ftrace_ops into the 3rd parameter */
189 movq function_trace_op(%rip), %rdx
191 /* Save the rest of pt_regs */
199 /* Copy saved flags */
200 movq MCOUNT_REG_SIZE(%rsp), %rcx
201 movq %rcx, EFLAGS(%rsp)
202 /* Kernel segments */
203 movq $__KERNEL_DS, %rcx
205 movq $__KERNEL_CS, %rcx
207 /* Stack - skipping return address and flags */
208 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
213 /* regs go into 4th parameter */
216 GLOBAL(ftrace_regs_call)
219 /* Copy flags back to SS, to restore them */
220 movq EFLAGS(%rsp), %rax
221 movq %rax, MCOUNT_REG_SIZE(%rsp)
223 /* Handlers can change the RIP */
225 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
227 /* restore the rest of pt_regs */
235 movq ORIG_RAX(%rsp), %rax
236 movq %rax, MCOUNT_REG_SIZE-8(%rsp)
238 /* If ORIG_RAX is anything but zero, make this a call to that */
239 movq ORIG_RAX(%rsp), %rax
243 /* Swap the flags with orig_rax */
244 movq MCOUNT_REG_SIZE(%rsp), %rdi
245 movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
246 movq %rax, MCOUNT_REG_SIZE(%rsp)
248 restore_mcount_regs 8
252 1: restore_mcount_regs
257 * The stack layout is nondetermistic here, depending on which path was
258 * taken. This confuses objtool and ORC, rightfully so. For now,
259 * pretend the stack always looks like the non-direct case.
267 * As this jmp to ftrace_epilogue can be a short jump
268 * it must not be copied into the trampoline.
269 * The trampoline will add the code to jump
272 GLOBAL(ftrace_regs_caller_end)
276 ENDPROC(ftrace_regs_caller)
279 #else /* ! CONFIG_DYNAMIC_FTRACE */
282 cmpq $ftrace_stub, ftrace_trace_function
286 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
287 cmpq $ftrace_stub, ftrace_graph_return
288 jnz ftrace_graph_caller
290 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
291 jnz ftrace_graph_caller
298 /* save_mcount_regs fills in first two parameters */
302 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
303 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
304 * ip and parent ip are used and the list function is called when
305 * function tracing is enabled.
307 movq ftrace_trace_function, %r8
312 ENDPROC(function_hook)
313 #endif /* CONFIG_DYNAMIC_FTRACE */
315 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
316 ENTRY(ftrace_graph_caller)
317 /* Saves rbp into %rdx and fills first parameter */
320 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
321 movq $0, %rdx /* No framepointers needed */
322 call prepare_ftrace_return
327 ENDPROC(ftrace_graph_caller)
329 ENTRY(return_to_handler)
333 /* Save the return values */
338 call ftrace_return_to_handler
345 END(return_to_handler)