xref: /linux/arch/x86/kernel/ftrace_64.S (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 *  Copyright (C) 2014  Steven Rostedt, Red Hat Inc
4 */
5
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>
13
14	.code64
15	.section .text, "ax"
16
17#ifdef CONFIG_FRAME_POINTER
18/* Save parent and function stack frames (rip and rbp) */
19#  define MCOUNT_FRAME_SIZE	(8+16*2)
20#else
21/* No need to save a stack frame */
22# define MCOUNT_FRAME_SIZE	0
23#endif /* CONFIG_FRAME_POINTER */
24
25/* Size of stack used to save mcount regs in save_mcount_regs */
26#define MCOUNT_REG_SIZE		(FRAME_SIZE + MCOUNT_FRAME_SIZE)
27
28/*
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.
34 *
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.
42 */
43
44/*
45 * @added: the amount of stack added before calling this
46 *
47 * After this is called, the following registers contain:
48 *
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
52 */
53.macro save_mcount_regs added=0
54
55#ifdef CONFIG_FRAME_POINTER
56	/* Save the original rbp */
57	pushq %rbp
58
59	/*
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.
65	 */
66
67	/* Save the parent pointer (skip orig rbp and our return address) */
68	pushq \added+8*2(%rsp)
69	pushq %rbp
70	movq %rsp, %rbp
71	/* Save the return address (now skip orig rbp, rbp and parent) */
72	pushq \added+8*3(%rsp)
73	pushq %rbp
74	movq %rsp, %rbp
75#endif /* CONFIG_FRAME_POINTER */
76
77	/*
78	 * We add enough stack to save all regs.
79	 */
80	subq $(FRAME_SIZE), %rsp
81	movq %rax, RAX(%rsp)
82	movq %rcx, RCX(%rsp)
83	movq %rdx, RDX(%rsp)
84	movq %rsi, RSI(%rsp)
85	movq %rdi, RDI(%rsp)
86	movq %r8, R8(%rsp)
87	movq %r9, R9(%rsp)
88	movq $0, ORIG_RAX(%rsp)
89	/*
90	 * Save the original RBP. Even though the mcount ABI does not
91	 * require this, it helps out callers.
92	 */
93#ifdef CONFIG_FRAME_POINTER
94	movq MCOUNT_REG_SIZE-8(%rsp), %rdx
95#else
96	movq %rbp, %rdx
97#endif
98	movq %rdx, RBP(%rsp)
99
100	/* Copy the parent address into %rsi (second parameter) */
101	movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
102
103	 /* Move RIP to its proper location */
104	movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
105	movq %rdi, RIP(%rsp)
106
107	/*
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.
111	 */
112	subq $MCOUNT_INSN_SIZE, %rdi
113	.endm
114
115.macro restore_mcount_regs save=0
116
117	/* ftrace_regs_caller or frame pointers require this */
118	movq RBP(%rsp), %rbp
119
120	movq R9(%rsp), %r9
121	movq R8(%rsp), %r8
122	movq RDI(%rsp), %rdi
123	movq RSI(%rsp), %rsi
124	movq RDX(%rsp), %rdx
125	movq RCX(%rsp), %rcx
126	movq RAX(%rsp), %rax
127
128	addq $MCOUNT_REG_SIZE-\save, %rsp
129
130	.endm
131
132#ifdef CONFIG_DYNAMIC_FTRACE
133
134SYM_FUNC_START(__fentry__)
135	RET
136SYM_FUNC_END(__fentry__)
137EXPORT_SYMBOL(__fentry__)
138
139SYM_FUNC_START(ftrace_caller)
140	/* save_mcount_regs fills in first two parameters */
141	save_mcount_regs
142
143	/* Stack - skipping return address of ftrace_caller */
144	leaq MCOUNT_REG_SIZE+8(%rsp), %rcx
145	movq %rcx, RSP(%rsp)
146
147SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
148	ANNOTATE_NOENDBR
149	/* Load the ftrace_ops into the 3rd parameter */
150	movq function_trace_op(%rip), %rdx
151
152	/* regs go into 4th parameter */
153	leaq (%rsp), %rcx
154
155	/* Only ops with REGS flag set should have CS register set */
156	movq $0, CS(%rsp)
157
158SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
159	ANNOTATE_NOENDBR
160	call ftrace_stub
161
162	/* Handlers can change the RIP */
163	movq RIP(%rsp), %rax
164	movq %rax, MCOUNT_REG_SIZE(%rsp)
165
166	restore_mcount_regs
167
168	/*
169	 * The code up to this label is copied into trampolines so
170	 * think twice before adding any new code or changing the
171	 * layout here.
172	 */
173SYM_INNER_LABEL(ftrace_caller_end, SYM_L_GLOBAL)
174	ANNOTATE_NOENDBR
175
176	jmp ftrace_epilogue
177SYM_FUNC_END(ftrace_caller);
178
179SYM_FUNC_START(ftrace_epilogue)
180/*
181 * This is weak to keep gas from relaxing the jumps.
182 */
183SYM_INNER_LABEL_ALIGN(ftrace_stub, SYM_L_WEAK)
184	UNWIND_HINT_FUNC
185	ENDBR
186	RET
187SYM_FUNC_END(ftrace_epilogue)
188
189SYM_FUNC_START(ftrace_regs_caller)
190	/* Save the current flags before any operations that can change them */
191	pushfq
192
193	/* added 8 bytes to save flags */
194	save_mcount_regs 8
195	/* save_mcount_regs fills in first two parameters */
196
197SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
198	ANNOTATE_NOENDBR
199	/* Load the ftrace_ops into the 3rd parameter */
200	movq function_trace_op(%rip), %rdx
201
202	/* Save the rest of pt_regs */
203	movq %r15, R15(%rsp)
204	movq %r14, R14(%rsp)
205	movq %r13, R13(%rsp)
206	movq %r12, R12(%rsp)
207	movq %r11, R11(%rsp)
208	movq %r10, R10(%rsp)
209	movq %rbx, RBX(%rsp)
210	/* Copy saved flags */
211	movq MCOUNT_REG_SIZE(%rsp), %rcx
212	movq %rcx, EFLAGS(%rsp)
213	/* Kernel segments */
214	movq $__KERNEL_DS, %rcx
215	movq %rcx, SS(%rsp)
216	movq $__KERNEL_CS, %rcx
217	movq %rcx, CS(%rsp)
218	/* Stack - skipping return address and flags */
219	leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
220	movq %rcx, RSP(%rsp)
221
222	ENCODE_FRAME_POINTER
223
224	/* regs go into 4th parameter */
225	leaq (%rsp), %rcx
226
227SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
228	ANNOTATE_NOENDBR
229	call ftrace_stub
230
231	/* Copy flags back to SS, to restore them */
232	movq EFLAGS(%rsp), %rax
233	movq %rax, MCOUNT_REG_SIZE(%rsp)
234
235	/* Handlers can change the RIP */
236	movq RIP(%rsp), %rax
237	movq %rax, MCOUNT_REG_SIZE+8(%rsp)
238
239	/* restore the rest of pt_regs */
240	movq R15(%rsp), %r15
241	movq R14(%rsp), %r14
242	movq R13(%rsp), %r13
243	movq R12(%rsp), %r12
244	movq R10(%rsp), %r10
245	movq RBX(%rsp), %rbx
246
247	movq ORIG_RAX(%rsp), %rax
248	movq %rax, MCOUNT_REG_SIZE-8(%rsp)
249
250	/*
251	 * If ORIG_RAX is anything but zero, make this a call to that.
252	 * See arch_ftrace_set_direct_caller().
253	 */
254	testq	%rax, %rax
255SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
256	ANNOTATE_NOENDBR
257	jnz	1f
258
259	restore_mcount_regs
260	/* Restore flags */
261	popfq
262
263	/*
264	 * As this jmp to ftrace_epilogue can be a short jump
265	 * it must not be copied into the trampoline.
266	 * The trampoline will add the code to jump
267	 * to the return.
268	 */
269SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
270	ANNOTATE_NOENDBR
271	jmp ftrace_epilogue
272
273	/* Swap the flags with orig_rax */
2741:	movq MCOUNT_REG_SIZE(%rsp), %rdi
275	movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
276	movq %rax, MCOUNT_REG_SIZE(%rsp)
277
278	restore_mcount_regs 8
279	/* Restore flags */
280	popfq
281	UNWIND_HINT_FUNC
282	jmp	ftrace_epilogue
283
284SYM_FUNC_END(ftrace_regs_caller)
285
286
287#else /* ! CONFIG_DYNAMIC_FTRACE */
288
289SYM_FUNC_START(__fentry__)
290	cmpq $ftrace_stub, ftrace_trace_function
291	jnz trace
292
293SYM_INNER_LABEL(ftrace_stub, SYM_L_GLOBAL)
294	ENDBR
295	RET
296
297trace:
298	/* save_mcount_regs fills in first two parameters */
299	save_mcount_regs
300
301	/*
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.
306	 */
307	movq ftrace_trace_function, %r8
308	CALL_NOSPEC r8
309	restore_mcount_regs
310
311	jmp ftrace_stub
312SYM_FUNC_END(__fentry__)
313EXPORT_SYMBOL(__fentry__)
314#endif /* CONFIG_DYNAMIC_FTRACE */
315
316#ifdef CONFIG_FUNCTION_GRAPH_TRACER
317SYM_FUNC_START(return_to_handler)
318	subq  $16, %rsp
319
320	/* Save the return values */
321	movq %rax, (%rsp)
322	movq %rdx, 8(%rsp)
323	movq %rbp, %rdi
324
325	call ftrace_return_to_handler
326
327	movq %rax, %rdi
328	movq 8(%rsp), %rdx
329	movq (%rsp), %rax
330
331	addq $16, %rsp
332	/*
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.
336	 */
337	ANNOTATE_INTRA_FUNCTION_CALL
338	call .Ldo_rop
339	int3
340.Ldo_rop:
341	mov %rdi, (%rsp)
342	UNWIND_HINT_FUNC
343	RET
344SYM_FUNC_END(return_to_handler)
345#endif
346