1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
6 #include <linux/linkage.h>
7 #include <linux/cfi_types.h>
8 #include <asm/ptrace.h>
9 #include <asm/ftrace.h>
10 #include <asm/export.h>
11 #include <asm/nospec-branch.h>
12 #include <asm/unwind_hints.h>
13 #include <asm/frame.h>
18 #ifdef CONFIG_FRAME_POINTER
19 /* Save parent and function stack frames (rip and rbp) */
20 # define MCOUNT_FRAME_SIZE (8+16*2)
22 /* No need to save a stack frame */
23 # define MCOUNT_FRAME_SIZE 0
24 #endif /* CONFIG_FRAME_POINTER */
26 /* Size of stack used to save mcount regs in save_mcount_regs */
27 #define MCOUNT_REG_SIZE (FRAME_SIZE + MCOUNT_FRAME_SIZE)
30 * gcc -pg option adds a call to 'mcount' in most functions.
31 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
32 * and is done before the function's stack frame is set up.
33 * They both require a set of regs to be saved before calling
34 * any C code and restored before returning back to the function.
36 * On boot up, all these calls are converted into nops. When tracing
37 * is enabled, the call can jump to either ftrace_caller or
38 * ftrace_regs_caller. Callbacks (tracing functions) that require
39 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
40 * it. For this reason, the size of the pt_regs structure will be
41 * allocated on the stack and the required mcount registers will
42 * be saved in the locations that pt_regs has them in.
46 * @added: the amount of stack added before calling this
48 * After this is called, the following registers contain:
50 * %rdi - holds the address that called the trampoline
51 * %rsi - holds the parent function (traced function's return address)
52 * %rdx - holds the original %rbp
54 .macro save_mcount_regs added=0
56 #ifdef CONFIG_FRAME_POINTER
57 /* Save the original rbp */
61 * Stack traces will stop at the ftrace trampoline if the frame pointer
62 * is not set up properly. If fentry is used, we need to save a frame
63 * pointer for the parent as well as the function traced, because the
64 * fentry is called before the stack frame is set up, where as mcount
65 * is called afterward.
68 /* Save the parent pointer (skip orig rbp and our return address) */
69 pushq \added+8*2(%rsp)
72 /* Save the return address (now skip orig rbp, rbp and parent) */
73 pushq \added+8*3(%rsp)
76 #endif /* CONFIG_FRAME_POINTER */
79 * We add enough stack to save all regs.
81 subq $(FRAME_SIZE), %rsp
89 movq $0, ORIG_RAX(%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 save=0
118 /* ftrace_regs_caller or frame pointers require this */
129 addq $MCOUNT_REG_SIZE-\save, %rsp
133 SYM_TYPED_FUNC_START(ftrace_stub)
135 SYM_FUNC_END(ftrace_stub)
137 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
138 SYM_TYPED_FUNC_START(ftrace_stub_graph)
140 SYM_FUNC_END(ftrace_stub_graph)
143 #ifdef CONFIG_DYNAMIC_FTRACE
145 SYM_FUNC_START(__fentry__)
147 SYM_FUNC_END(__fentry__)
148 EXPORT_SYMBOL(__fentry__)
150 SYM_FUNC_START(ftrace_caller)
151 /* save_mcount_regs fills in first two parameters */
154 /* Stack - skipping return address of ftrace_caller */
155 leaq MCOUNT_REG_SIZE+8(%rsp), %rcx
158 SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
160 /* Load the ftrace_ops into the 3rd parameter */
161 movq function_trace_op(%rip), %rdx
163 /* regs go into 4th parameter */
166 /* Only ops with REGS flag set should have CS register set */
169 SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
173 /* Handlers can change the RIP */
175 movq %rax, MCOUNT_REG_SIZE(%rsp)
180 * The code up to this label is copied into trampolines so
181 * think twice before adding any new code or changing the
184 SYM_INNER_LABEL(ftrace_caller_end, SYM_L_GLOBAL)
187 SYM_FUNC_END(ftrace_caller);
188 STACK_FRAME_NON_STANDARD_FP(ftrace_caller)
190 SYM_FUNC_START(ftrace_regs_caller)
191 /* Save the current flags before any operations that can change them */
194 /* added 8 bytes to save flags */
196 /* save_mcount_regs fills in first two parameters */
198 SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
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
225 /* regs go into 4th parameter */
228 SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
232 /* Copy flags back to SS, to restore them */
233 movq EFLAGS(%rsp), %rax
234 movq %rax, MCOUNT_REG_SIZE(%rsp)
236 /* Handlers can change the RIP */
238 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
240 /* restore the rest of pt_regs */
248 movq ORIG_RAX(%rsp), %rax
249 movq %rax, MCOUNT_REG_SIZE-8(%rsp)
252 * If ORIG_RAX is anything but zero, make this a call to that.
253 * See arch_ftrace_set_direct_caller().
256 SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
265 * The trampoline will add the return.
267 SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
271 /* Swap the flags with orig_rax */
272 1: movq MCOUNT_REG_SIZE(%rsp), %rdi
273 movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
274 movq %rax, MCOUNT_REG_SIZE(%rsp)
276 restore_mcount_regs 8
282 SYM_FUNC_END(ftrace_regs_caller)
283 STACK_FRAME_NON_STANDARD_FP(ftrace_regs_caller)
286 #else /* ! CONFIG_DYNAMIC_FTRACE */
288 SYM_FUNC_START(__fentry__)
289 cmpq $ftrace_stub, ftrace_trace_function
294 /* save_mcount_regs fills in first two parameters */
298 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
299 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
300 * ip and parent ip are used and the list function is called when
301 * function tracing is enabled.
303 movq ftrace_trace_function, %r8
308 SYM_FUNC_END(__fentry__)
309 EXPORT_SYMBOL(__fentry__)
310 STACK_FRAME_NON_STANDARD_FP(__fentry__)
312 #endif /* CONFIG_DYNAMIC_FTRACE */
314 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
315 SYM_CODE_START(return_to_handler)
320 /* Save the return values */
325 call ftrace_return_to_handler
333 * Jump back to the old return address. This cannot be JMP_NOSPEC rdi
334 * since IBT would demand that contain ENDBR, which simply isn't so for
335 * return addresses. Use a retpoline here to keep the RSB balanced.
337 ANNOTATE_INTRA_FUNCTION_CALL
343 SYM_CODE_END(return_to_handler)