xref: /openbmc/linux/arch/x86/kernel/ftrace_64.S (revision 7b73a9c8)
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 .entry.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		(SS+8 + 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 $(MCOUNT_REG_SIZE - MCOUNT_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	retq
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
143SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
144	/* Load the ftrace_ops into the 3rd parameter */
145	movq function_trace_op(%rip), %rdx
146
147	/* regs go into 4th parameter (but make it NULL) */
148	movq $0, %rcx
149
150SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
151	call ftrace_stub
152
153	restore_mcount_regs
154
155	/*
156	 * The code up to this label is copied into trampolines so
157	 * think twice before adding any new code or changing the
158	 * layout here.
159	 */
160SYM_INNER_LABEL(ftrace_epilogue, SYM_L_GLOBAL)
161
162#ifdef CONFIG_FUNCTION_GRAPH_TRACER
163SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
164	jmp ftrace_stub
165#endif
166
167/*
168 * This is weak to keep gas from relaxing the jumps.
169 * It is also used to copy the retq for trampolines.
170 */
171SYM_INNER_LABEL_ALIGN(ftrace_stub, SYM_L_WEAK)
172	retq
173SYM_FUNC_END(ftrace_caller)
174
175SYM_FUNC_START(ftrace_regs_caller)
176	/* Save the current flags before any operations that can change them */
177	pushfq
178
179	UNWIND_HINT_SAVE
180
181	/* added 8 bytes to save flags */
182	save_mcount_regs 8
183	/* save_mcount_regs fills in first two parameters */
184
185SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
186	/* Load the ftrace_ops into the 3rd parameter */
187	movq function_trace_op(%rip), %rdx
188
189	/* Save the rest of pt_regs */
190	movq %r15, R15(%rsp)
191	movq %r14, R14(%rsp)
192	movq %r13, R13(%rsp)
193	movq %r12, R12(%rsp)
194	movq %r11, R11(%rsp)
195	movq %r10, R10(%rsp)
196	movq %rbx, RBX(%rsp)
197	/* Copy saved flags */
198	movq MCOUNT_REG_SIZE(%rsp), %rcx
199	movq %rcx, EFLAGS(%rsp)
200	/* Kernel segments */
201	movq $__KERNEL_DS, %rcx
202	movq %rcx, SS(%rsp)
203	movq $__KERNEL_CS, %rcx
204	movq %rcx, CS(%rsp)
205	/* Stack - skipping return address and flags */
206	leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
207	movq %rcx, RSP(%rsp)
208
209	ENCODE_FRAME_POINTER
210
211	/* regs go into 4th parameter */
212	leaq (%rsp), %rcx
213
214SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
215	call ftrace_stub
216
217	/* Copy flags back to SS, to restore them */
218	movq EFLAGS(%rsp), %rax
219	movq %rax, MCOUNT_REG_SIZE(%rsp)
220
221	/* Handlers can change the RIP */
222	movq RIP(%rsp), %rax
223	movq %rax, MCOUNT_REG_SIZE+8(%rsp)
224
225	/* restore the rest of pt_regs */
226	movq R15(%rsp), %r15
227	movq R14(%rsp), %r14
228	movq R13(%rsp), %r13
229	movq R12(%rsp), %r12
230	movq R10(%rsp), %r10
231	movq RBX(%rsp), %rbx
232
233	movq ORIG_RAX(%rsp), %rax
234	movq %rax, MCOUNT_REG_SIZE-8(%rsp)
235
236	/* If ORIG_RAX is anything but zero, make this a call to that */
237	movq ORIG_RAX(%rsp), %rax
238	cmpq	$0, %rax
239	je	1f
240
241	/* Swap the flags with orig_rax */
242	movq MCOUNT_REG_SIZE(%rsp), %rdi
243	movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
244	movq %rax, MCOUNT_REG_SIZE(%rsp)
245
246	restore_mcount_regs 8
247
248	jmp	2f
249
2501:	restore_mcount_regs
251
252
2532:
254	/*
255	 * The stack layout is nondetermistic here, depending on which path was
256	 * taken.  This confuses objtool and ORC, rightfully so.  For now,
257	 * pretend the stack always looks like the non-direct case.
258	 */
259	UNWIND_HINT_RESTORE
260
261	/* Restore flags */
262	popfq
263
264	/*
265	 * As this jmp to ftrace_epilogue can be a short jump
266	 * it must not be copied into the trampoline.
267	 * The trampoline will add the code to jump
268	 * to the return.
269	 */
270SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
271
272	jmp ftrace_epilogue
273
274SYM_FUNC_END(ftrace_regs_caller)
275
276
277#else /* ! CONFIG_DYNAMIC_FTRACE */
278
279SYM_FUNC_START(__fentry__)
280	cmpq $ftrace_stub, ftrace_trace_function
281	jnz trace
282
283fgraph_trace:
284#ifdef CONFIG_FUNCTION_GRAPH_TRACER
285	cmpq $ftrace_stub, ftrace_graph_return
286	jnz ftrace_graph_caller
287
288	cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
289	jnz ftrace_graph_caller
290#endif
291
292SYM_INNER_LABEL(ftrace_stub, SYM_L_GLOBAL)
293	retq
294
295trace:
296	/* save_mcount_regs fills in first two parameters */
297	save_mcount_regs
298
299	/*
300	 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
301	 * set (see include/asm/ftrace.h and include/linux/ftrace.h).  Only the
302	 * ip and parent ip are used and the list function is called when
303	 * function tracing is enabled.
304	 */
305	movq ftrace_trace_function, %r8
306	CALL_NOSPEC %r8
307	restore_mcount_regs
308
309	jmp fgraph_trace
310SYM_FUNC_END(__fentry__)
311EXPORT_SYMBOL(__fentry__)
312#endif /* CONFIG_DYNAMIC_FTRACE */
313
314#ifdef CONFIG_FUNCTION_GRAPH_TRACER
315SYM_FUNC_START(ftrace_graph_caller)
316	/* Saves rbp into %rdx and fills first parameter  */
317	save_mcount_regs
318
319	leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
320	movq $0, %rdx	/* No framepointers needed */
321	call	prepare_ftrace_return
322
323	restore_mcount_regs
324
325	retq
326SYM_FUNC_END(ftrace_graph_caller)
327
328SYM_CODE_START(return_to_handler)
329	UNWIND_HINT_EMPTY
330	subq  $24, %rsp
331
332	/* Save the return values */
333	movq %rax, (%rsp)
334	movq %rdx, 8(%rsp)
335	movq %rbp, %rdi
336
337	call ftrace_return_to_handler
338
339	movq %rax, %rdi
340	movq 8(%rsp), %rdx
341	movq (%rsp), %rax
342	addq $24, %rsp
343	JMP_NOSPEC %rdi
344SYM_CODE_END(return_to_handler)
345#endif
346