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>
17 #ifdef CONFIG_FRAME_POINTER
18 /* Save parent and function stack frames (rip and rbp) */
19 # define MCOUNT_FRAME_SIZE (8+16*2)
21 /* No need to save a stack frame */
22 # define MCOUNT_FRAME_SIZE 0
23 #endif /* CONFIG_FRAME_POINTER */
25 /* Size of stack used to save mcount regs in save_mcount_regs */
26 #define MCOUNT_REG_SIZE (FRAME_SIZE + MCOUNT_FRAME_SIZE)
29 * gcc -pg option adds a call to 'mcount' in most functions.
30 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
31 * and is done before the function's stack frame is set up.
32 * They both require a set of regs to be saved before calling
33 * any C code and restored before returning back to the function.
35 * On boot up, all these calls are converted into nops. When tracing
36 * is enabled, the call can jump to either ftrace_caller or
37 * ftrace_regs_caller. Callbacks (tracing functions) that require
38 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
39 * it. For this reason, the size of the pt_regs structure will be
40 * allocated on the stack and the required mcount registers will
41 * be saved in the locations that pt_regs has them in.
45 * @added: the amount of stack added before calling this
47 * After this is called, the following registers contain:
49 * %rdi - holds the address that called the trampoline
50 * %rsi - holds the parent function (traced function's return address)
51 * %rdx - holds the original %rbp
53 .macro save_mcount_regs added=0
55 #ifdef CONFIG_FRAME_POINTER
56 /* Save the original rbp */
60 * Stack traces will stop at the ftrace trampoline if the frame pointer
61 * is not set up properly. If fentry is used, we need to save a frame
62 * pointer for the parent as well as the function traced, because the
63 * fentry is called before the stack frame is set up, where as mcount
64 * is called afterward.
67 /* Save the parent pointer (skip orig rbp and our return address) */
68 pushq \added+8*2(%rsp)
71 /* Save the return address (now skip orig rbp, rbp and parent) */
72 pushq \added+8*3(%rsp)
75 #endif /* CONFIG_FRAME_POINTER */
78 * We add enough stack to save all regs.
80 subq $(FRAME_SIZE), %rsp
88 movq $0, ORIG_RAX(%rsp)
90 * Save the original RBP. Even though the mcount ABI does not
91 * require this, it helps out callers.
93 #ifdef CONFIG_FRAME_POINTER
94 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
100 /* Copy the parent address into %rsi (second parameter) */
101 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
103 /* Move RIP to its proper location */
104 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
108 * Now %rdi (the first parameter) has the return address of
109 * where ftrace_call returns. But the callbacks expect the
110 * address of the call itself.
112 subq $MCOUNT_INSN_SIZE, %rdi
115 .macro restore_mcount_regs save=0
117 /* ftrace_regs_caller or frame pointers require this */
128 addq $MCOUNT_REG_SIZE-\save, %rsp
132 #ifdef CONFIG_DYNAMIC_FTRACE
134 SYM_FUNC_START(__fentry__)
136 SYM_FUNC_END(__fentry__)
137 EXPORT_SYMBOL(__fentry__)
139 SYM_FUNC_START(ftrace_caller)
140 /* save_mcount_regs fills in first two parameters */
143 /* Stack - skipping return address of ftrace_caller */
144 leaq MCOUNT_REG_SIZE+8(%rsp), %rcx
147 SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
148 /* Load the ftrace_ops into the 3rd parameter */
149 movq function_trace_op(%rip), %rdx
151 /* regs go into 4th parameter */
154 /* Only ops with REGS flag set should have CS register set */
157 SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
160 /* Handlers can change the RIP */
162 movq %rax, MCOUNT_REG_SIZE(%rsp)
167 * The code up to this label is copied into trampolines so
168 * think twice before adding any new code or changing the
171 SYM_INNER_LABEL(ftrace_caller_end, SYM_L_GLOBAL)
174 SYM_FUNC_END(ftrace_caller);
176 SYM_FUNC_START(ftrace_epilogue)
177 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
178 SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
183 * This is weak to keep gas from relaxing the jumps.
184 * It is also used to copy the retq for trampolines.
186 SYM_INNER_LABEL_ALIGN(ftrace_stub, SYM_L_WEAK)
188 SYM_FUNC_END(ftrace_epilogue)
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)
199 /* Load the ftrace_ops into the 3rd parameter */
200 movq function_trace_op(%rip), %rdx
202 /* Save the rest of pt_regs */
210 /* Copy saved flags */
211 movq MCOUNT_REG_SIZE(%rsp), %rcx
212 movq %rcx, EFLAGS(%rsp)
213 /* Kernel segments */
214 movq $__KERNEL_DS, %rcx
216 movq $__KERNEL_CS, %rcx
218 /* Stack - skipping return address and flags */
219 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
224 /* regs go into 4th parameter */
227 SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
230 /* Copy flags back to SS, to restore them */
231 movq EFLAGS(%rsp), %rax
232 movq %rax, MCOUNT_REG_SIZE(%rsp)
234 /* Handlers can change the RIP */
236 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
238 /* restore the rest of pt_regs */
246 movq ORIG_RAX(%rsp), %rax
247 movq %rax, MCOUNT_REG_SIZE-8(%rsp)
250 * If ORIG_RAX is anything but zero, make this a call to that.
251 * See arch_ftrace_set_direct_caller().
253 movq ORIG_RAX(%rsp), %rax
255 SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
263 * As this jmp to ftrace_epilogue can be a short jump
264 * it must not be copied into the trampoline.
265 * The trampoline will add the code to jump
268 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
279 UNWIND_HINT_RET_OFFSET
282 SYM_FUNC_END(ftrace_regs_caller)
285 #else /* ! CONFIG_DYNAMIC_FTRACE */
287 SYM_FUNC_START(__fentry__)
288 cmpq $ftrace_stub, ftrace_trace_function
292 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
293 cmpq $ftrace_stub, ftrace_graph_return
294 jnz ftrace_graph_caller
296 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
297 jnz ftrace_graph_caller
300 SYM_INNER_LABEL(ftrace_stub, SYM_L_GLOBAL)
304 /* save_mcount_regs fills in first two parameters */
308 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
309 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
310 * ip and parent ip are used and the list function is called when
311 * function tracing is enabled.
313 movq ftrace_trace_function, %r8
318 SYM_FUNC_END(__fentry__)
319 EXPORT_SYMBOL(__fentry__)
320 #endif /* CONFIG_DYNAMIC_FTRACE */
322 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
323 SYM_FUNC_START(ftrace_graph_caller)
324 /* Saves rbp into %rdx and fills first parameter */
327 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
328 movq $0, %rdx /* No framepointers needed */
329 call prepare_ftrace_return
334 SYM_FUNC_END(ftrace_graph_caller)
336 SYM_CODE_START(return_to_handler)
340 /* Save the return values */
345 call ftrace_return_to_handler
352 SYM_CODE_END(return_to_handler)