]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/kernel/ftrace_64.S
ftrace/x86: Tell objtool to ignore nondeterministic ftrace stack layout
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kernel / ftrace_64.S
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 # define function_hook __fentry__
18 EXPORT_SYMBOL(__fentry__)
19
20 #ifdef CONFIG_FRAME_POINTER
21 /* Save parent and function stack frames (rip and rbp) */
22 # define MCOUNT_FRAME_SIZE (8+16*2)
23 #else
24 /* No need to save a stack frame */
25 # define MCOUNT_FRAME_SIZE 0
26 #endif /* CONFIG_FRAME_POINTER */
27
28 /* Size of stack used to save mcount regs in save_mcount_regs */
29 #define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
30
31 /*
32 * gcc -pg option adds a call to 'mcount' in most functions.
33 * When -mfentry is used, the call is to 'fentry' and not 'mcount'
34 * and is done before the function's stack frame is set up.
35 * They both require a set of regs to be saved before calling
36 * any C code and restored before returning back to the function.
37 *
38 * On boot up, all these calls are converted into nops. When tracing
39 * is enabled, the call can jump to either ftrace_caller or
40 * ftrace_regs_caller. Callbacks (tracing functions) that require
41 * ftrace_regs_caller (like kprobes) need to have pt_regs passed to
42 * it. For this reason, the size of the pt_regs structure will be
43 * allocated on the stack and the required mcount registers will
44 * be saved in the locations that pt_regs has them in.
45 */
46
47 /*
48 * @added: the amount of stack added before calling this
49 *
50 * After this is called, the following registers contain:
51 *
52 * %rdi - holds the address that called the trampoline
53 * %rsi - holds the parent function (traced function's return address)
54 * %rdx - holds the original %rbp
55 */
56 .macro save_mcount_regs added=0
57
58 #ifdef CONFIG_FRAME_POINTER
59 /* Save the original rbp */
60 pushq %rbp
61
62 /*
63 * Stack traces will stop at the ftrace trampoline if the frame pointer
64 * is not set up properly. If fentry is used, we need to save a frame
65 * pointer for the parent as well as the function traced, because the
66 * fentry is called before the stack frame is set up, where as mcount
67 * is called afterward.
68 */
69
70 /* Save the parent pointer (skip orig rbp and our return address) */
71 pushq \added+8*2(%rsp)
72 pushq %rbp
73 movq %rsp, %rbp
74 /* Save the return address (now skip orig rbp, rbp and parent) */
75 pushq \added+8*3(%rsp)
76 pushq %rbp
77 movq %rsp, %rbp
78 #endif /* CONFIG_FRAME_POINTER */
79
80 /*
81 * We add enough stack to save all regs.
82 */
83 subq $(MCOUNT_REG_SIZE - MCOUNT_FRAME_SIZE), %rsp
84 movq %rax, RAX(%rsp)
85 movq %rcx, RCX(%rsp)
86 movq %rdx, RDX(%rsp)
87 movq %rsi, RSI(%rsp)
88 movq %rdi, RDI(%rsp)
89 movq %r8, R8(%rsp)
90 movq %r9, R9(%rsp)
91 movq $0, ORIG_RAX(%rsp)
92 /*
93 * Save the original RBP. Even though the mcount ABI does not
94 * require this, it helps out callers.
95 */
96 #ifdef CONFIG_FRAME_POINTER
97 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
98 #else
99 movq %rbp, %rdx
100 #endif
101 movq %rdx, RBP(%rsp)
102
103 /* Copy the parent address into %rsi (second parameter) */
104 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
105
106 /* Move RIP to its proper location */
107 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
108 movq %rdi, RIP(%rsp)
109
110 /*
111 * Now %rdi (the first parameter) has the return address of
112 * where ftrace_call returns. But the callbacks expect the
113 * address of the call itself.
114 */
115 subq $MCOUNT_INSN_SIZE, %rdi
116 .endm
117
118 .macro restore_mcount_regs save=0
119
120 /* ftrace_regs_caller or frame pointers require this */
121 movq RBP(%rsp), %rbp
122
123 movq R9(%rsp), %r9
124 movq R8(%rsp), %r8
125 movq RDI(%rsp), %rdi
126 movq RSI(%rsp), %rsi
127 movq RDX(%rsp), %rdx
128 movq RCX(%rsp), %rcx
129 movq RAX(%rsp), %rax
130
131 addq $MCOUNT_REG_SIZE-\save, %rsp
132
133 .endm
134
135 #ifdef CONFIG_DYNAMIC_FTRACE
136
137 ENTRY(function_hook)
138 retq
139 ENDPROC(function_hook)
140
141 ENTRY(ftrace_caller)
142 /* save_mcount_regs fills in first two parameters */
143 save_mcount_regs
144
145 GLOBAL(ftrace_caller_op_ptr)
146 /* Load the ftrace_ops into the 3rd parameter */
147 movq function_trace_op(%rip), %rdx
148
149 /* regs go into 4th parameter (but make it NULL) */
150 movq $0, %rcx
151
152 GLOBAL(ftrace_call)
153 call ftrace_stub
154
155 restore_mcount_regs
156
157 /*
158 * The code up to this label is copied into trampolines so
159 * think twice before adding any new code or changing the
160 * layout here.
161 */
162 GLOBAL(ftrace_epilogue)
163
164 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
165 GLOBAL(ftrace_graph_call)
166 jmp ftrace_stub
167 #endif
168
169 /*
170 * This is weak to keep gas from relaxing the jumps.
171 * It is also used to copy the retq for trampolines.
172 */
173 WEAK(ftrace_stub)
174 retq
175 ENDPROC(ftrace_caller)
176
177 ENTRY(ftrace_regs_caller)
178 /* Save the current flags before any operations that can change them */
179 pushfq
180
181 UNWIND_HINT_SAVE
182
183 /* added 8 bytes to save flags */
184 save_mcount_regs 8
185 /* save_mcount_regs fills in first two parameters */
186
187 GLOBAL(ftrace_regs_caller_op_ptr)
188 /* Load the ftrace_ops into the 3rd parameter */
189 movq function_trace_op(%rip), %rdx
190
191 /* Save the rest of pt_regs */
192 movq %r15, R15(%rsp)
193 movq %r14, R14(%rsp)
194 movq %r13, R13(%rsp)
195 movq %r12, R12(%rsp)
196 movq %r11, R11(%rsp)
197 movq %r10, R10(%rsp)
198 movq %rbx, RBX(%rsp)
199 /* Copy saved flags */
200 movq MCOUNT_REG_SIZE(%rsp), %rcx
201 movq %rcx, EFLAGS(%rsp)
202 /* Kernel segments */
203 movq $__KERNEL_DS, %rcx
204 movq %rcx, SS(%rsp)
205 movq $__KERNEL_CS, %rcx
206 movq %rcx, CS(%rsp)
207 /* Stack - skipping return address and flags */
208 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
209 movq %rcx, RSP(%rsp)
210
211 ENCODE_FRAME_POINTER
212
213 /* regs go into 4th parameter */
214 leaq (%rsp), %rcx
215
216 GLOBAL(ftrace_regs_call)
217 call ftrace_stub
218
219 /* Copy flags back to SS, to restore them */
220 movq EFLAGS(%rsp), %rax
221 movq %rax, MCOUNT_REG_SIZE(%rsp)
222
223 /* Handlers can change the RIP */
224 movq RIP(%rsp), %rax
225 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
226
227 /* restore the rest of pt_regs */
228 movq R15(%rsp), %r15
229 movq R14(%rsp), %r14
230 movq R13(%rsp), %r13
231 movq R12(%rsp), %r12
232 movq R10(%rsp), %r10
233 movq RBX(%rsp), %rbx
234
235 movq ORIG_RAX(%rsp), %rax
236 movq %rax, MCOUNT_REG_SIZE-8(%rsp)
237
238 /* If ORIG_RAX is anything but zero, make this a call to that */
239 movq ORIG_RAX(%rsp), %rax
240 cmpq $0, %rax
241 je 1f
242
243 /* Swap the flags with orig_rax */
244 movq MCOUNT_REG_SIZE(%rsp), %rdi
245 movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
246 movq %rax, MCOUNT_REG_SIZE(%rsp)
247
248 restore_mcount_regs 8
249
250 jmp 2f
251
252 1: restore_mcount_regs
253
254
255 2:
256 /*
257 * The stack layout is nondetermistic here, depending on which path was
258 * taken. This confuses objtool and ORC, rightfully so. For now,
259 * pretend the stack always looks like the non-direct case.
260 */
261 UNWIND_HINT_RESTORE
262
263 /* Restore flags */
264 popfq
265
266 /*
267 * As this jmp to ftrace_epilogue can be a short jump
268 * it must not be copied into the trampoline.
269 * The trampoline will add the code to jump
270 * to the return.
271 */
272 GLOBAL(ftrace_regs_caller_end)
273
274 jmp ftrace_epilogue
275
276 ENDPROC(ftrace_regs_caller)
277
278
279 #else /* ! CONFIG_DYNAMIC_FTRACE */
280
281 ENTRY(function_hook)
282 cmpq $ftrace_stub, ftrace_trace_function
283 jnz trace
284
285 fgraph_trace:
286 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
287 cmpq $ftrace_stub, ftrace_graph_return
288 jnz ftrace_graph_caller
289
290 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
291 jnz ftrace_graph_caller
292 #endif
293
294 GLOBAL(ftrace_stub)
295 retq
296
297 trace:
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 fgraph_trace
312 ENDPROC(function_hook)
313 #endif /* CONFIG_DYNAMIC_FTRACE */
314
315 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
316 ENTRY(ftrace_graph_caller)
317 /* Saves rbp into %rdx and fills first parameter */
318 save_mcount_regs
319
320 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
321 movq $0, %rdx /* No framepointers needed */
322 call prepare_ftrace_return
323
324 restore_mcount_regs
325
326 retq
327 ENDPROC(ftrace_graph_caller)
328
329 ENTRY(return_to_handler)
330 UNWIND_HINT_EMPTY
331 subq $24, %rsp
332
333 /* Save the return values */
334 movq %rax, (%rsp)
335 movq %rdx, 8(%rsp)
336 movq %rbp, %rdi
337
338 call ftrace_return_to_handler
339
340 movq %rax, %rdi
341 movq 8(%rsp), %rdx
342 movq (%rsp), %rax
343 addq $24, %rsp
344 JMP_NOSPEC %rdi
345 END(return_to_handler)
346 #endif