1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
6 #include <linux/export.h>
7 #include <linux/cfi_types.h>
8 #include <linux/linkage.h>
9 #include <asm/asm-offsets.h>
10 #include <asm/ptrace.h>
11 #include <asm/ftrace.h>
12 #include <asm/nospec-branch.h>
13 #include <asm/unwind_hints.h>
14 #include <asm/frame.h>
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 (FRAME_SIZE + 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 $(FRAME_SIZE), %rsp
90 movq $0, ORIG_RAX(%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 save=0
119 /* ftrace_regs_caller or frame pointers require this */
130 addq $MCOUNT_REG_SIZE-\save, %rsp
134 SYM_TYPED_FUNC_START(ftrace_stub)
137 SYM_FUNC_END(ftrace_stub)
139 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
140 SYM_TYPED_FUNC_START(ftrace_stub_graph)
143 SYM_FUNC_END(ftrace_stub_graph)
146 #ifdef CONFIG_DYNAMIC_FTRACE
148 SYM_FUNC_START(__fentry__)
151 SYM_FUNC_END(__fentry__)
152 EXPORT_SYMBOL(__fentry__)
154 SYM_FUNC_START(ftrace_caller)
155 /* save_mcount_regs fills in first two parameters */
160 /* Stack - skipping return address of ftrace_caller */
161 leaq MCOUNT_REG_SIZE+8(%rsp), %rcx
164 SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
166 /* Load the ftrace_ops into the 3rd parameter */
167 movq function_trace_op(%rip), %rdx
169 /* regs go into 4th parameter */
172 /* Only ops with REGS flag set should have CS register set */
175 /* Account for the function call below */
178 SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
182 /* Handlers can change the RIP */
184 movq %rax, MCOUNT_REG_SIZE(%rsp)
189 * The code up to this label is copied into trampolines so
190 * think twice before adding any new code or changing the
193 SYM_INNER_LABEL(ftrace_caller_end, SYM_L_GLOBAL)
196 SYM_FUNC_END(ftrace_caller);
197 STACK_FRAME_NON_STANDARD_FP(ftrace_caller)
199 SYM_FUNC_START(ftrace_regs_caller)
200 /* Save the current flags before any operations that can change them */
203 /* added 8 bytes to save flags */
205 /* save_mcount_regs fills in first two parameters */
209 SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
211 /* Load the ftrace_ops into the 3rd parameter */
212 movq function_trace_op(%rip), %rdx
214 /* Save the rest of pt_regs */
222 /* Copy saved flags */
223 movq MCOUNT_REG_SIZE(%rsp), %rcx
224 movq %rcx, EFLAGS(%rsp)
225 /* Kernel segments */
226 movq $__KERNEL_DS, %rcx
228 movq $__KERNEL_CS, %rcx
230 /* Stack - skipping return address and flags */
231 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
236 /* regs go into 4th parameter */
239 /* Account for the function call below */
242 SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
246 /* Copy flags back to SS, to restore them */
247 movq EFLAGS(%rsp), %rax
248 movq %rax, MCOUNT_REG_SIZE(%rsp)
250 /* Handlers can change the RIP */
252 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
254 /* restore the rest of pt_regs */
262 movq ORIG_RAX(%rsp), %rax
263 movq %rax, MCOUNT_REG_SIZE-8(%rsp)
266 * If ORIG_RAX is anything but zero, make this a call to that.
267 * See arch_ftrace_set_direct_caller().
270 SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
279 * The trampoline will add the return.
281 SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
285 /* Swap the flags with orig_rax */
286 1: movq MCOUNT_REG_SIZE(%rsp), %rdi
287 movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
288 movq %rax, MCOUNT_REG_SIZE(%rsp)
290 restore_mcount_regs 8
296 * The above left an extra return value on the stack; effectively
297 * doing a tail-call without using a register. This PUSH;RET
298 * pattern unbalances the RSB, inject a pointless CALL to rebalance.
300 ANNOTATE_INTRA_FUNCTION_CALL
305 ALTERNATIVE __stringify(RET), \
306 __stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
307 X86_FEATURE_CALL_DEPTH
309 SYM_FUNC_END(ftrace_regs_caller)
310 STACK_FRAME_NON_STANDARD_FP(ftrace_regs_caller)
312 SYM_FUNC_START(ftrace_stub_direct_tramp)
315 SYM_FUNC_END(ftrace_stub_direct_tramp)
317 #else /* ! CONFIG_DYNAMIC_FTRACE */
319 SYM_FUNC_START(__fentry__)
322 cmpq $ftrace_stub, ftrace_trace_function
327 /* save_mcount_regs fills in first two parameters */
331 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
332 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
333 * ip and parent ip are used and the list function is called when
334 * function tracing is enabled.
336 movq ftrace_trace_function, %r8
341 SYM_FUNC_END(__fentry__)
342 EXPORT_SYMBOL(__fentry__)
343 STACK_FRAME_NON_STANDARD_FP(__fentry__)
345 #endif /* CONFIG_DYNAMIC_FTRACE */
347 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
348 SYM_CODE_START(return_to_handler)
349 UNWIND_HINT_UNDEFINED
353 /* Save the return values */
359 call ftrace_return_to_handler
367 * Jump back to the old return address. This cannot be JMP_NOSPEC rdi
368 * since IBT would demand that contain ENDBR, which simply isn't so for
369 * return addresses. Use a retpoline here to keep the RSB balanced.
371 ANNOTATE_INTRA_FUNCTION_CALL
376 ALTERNATIVE __stringify(RET), \
377 __stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
378 X86_FEATURE_CALL_DEPTH
379 SYM_CODE_END(return_to_handler)