Merge tag 'lkmm.2023.04.07a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck...
[linux-block.git] / arch / x86 / kernel / ftrace_64.S
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
e18eead3 2/*
e18eead3
SR
3 * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
4 */
5
883bbbff 6#include <linux/cfi_types.h>
e18eead3 7#include <linux/linkage.h>
ee3e2469 8#include <asm/asm-offsets.h>
e18eead3
SR
9#include <asm/ptrace.h>
10#include <asm/ftrace.h>
784d5699 11#include <asm/export.h>
9351803b 12#include <asm/nospec-branch.h>
e2ac83d7 13#include <asm/unwind_hints.h>
ea1ed38d 14#include <asm/frame.h>
e18eead3
SR
15
16 .code64
b9f6976b 17 .section .text, "ax"
e18eead3 18
0687c36e 19#ifdef CONFIG_FRAME_POINTER
0687c36e
SRRH
20/* Save parent and function stack frames (rip and rbp) */
21# define MCOUNT_FRAME_SIZE (8+16*2)
0687c36e
SRRH
22#else
23/* No need to save a stack frame */
e2ac83d7 24# define MCOUNT_FRAME_SIZE 0
0687c36e
SRRH
25#endif /* CONFIG_FRAME_POINTER */
26
85f6f029 27/* Size of stack used to save mcount regs in save_mcount_regs */
dc2745b6 28#define MCOUNT_REG_SIZE (FRAME_SIZE + MCOUNT_FRAME_SIZE)
85f6f029 29
05df710e
SRRH
30/*
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.
36 *
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.
44 */
45
f1ab00af
SRRH
46/*
47 * @added: the amount of stack added before calling this
48 *
49 * After this is called, the following registers contain:
50 *
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
54 */
527aa75b 55.macro save_mcount_regs added=0
0687c36e 56
e2ac83d7
JP
57#ifdef CONFIG_FRAME_POINTER
58 /* Save the original rbp */
0687c36e
SRRH
59 pushq %rbp
60
0687c36e
SRRH
61 /*
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.
67 */
562e14f7 68
0687c36e
SRRH
69 /* Save the parent pointer (skip orig rbp and our return address) */
70 pushq \added+8*2(%rsp)
71 pushq %rbp
72 movq %rsp, %rbp
73 /* Save the return address (now skip orig rbp, rbp and parent) */
74 pushq \added+8*3(%rsp)
0687c36e
SRRH
75 pushq %rbp
76 movq %rsp, %rbp
77#endif /* CONFIG_FRAME_POINTER */
78
79 /*
80 * We add enough stack to save all regs.
81 */
dc2745b6 82 subq $(FRAME_SIZE), %rsp
4bcdf152
SRRH
83 movq %rax, RAX(%rsp)
84 movq %rcx, RCX(%rsp)
85 movq %rdx, RDX(%rsp)
86 movq %rsi, RSI(%rsp)
87 movq %rdi, RDI(%rsp)
88 movq %r8, R8(%rsp)
89 movq %r9, R9(%rsp)
562955fe 90 movq $0, ORIG_RAX(%rsp)
0687c36e
SRRH
91 /*
92 * Save the original RBP. Even though the mcount ABI does not
93 * require this, it helps out callers.
94 */
e2ac83d7 95#ifdef CONFIG_FRAME_POINTER
0687c36e 96 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
e2ac83d7
JP
97#else
98 movq %rbp, %rdx
99#endif
0687c36e
SRRH
100 movq %rdx, RBP(%rsp)
101
f1ab00af 102 /* Copy the parent address into %rsi (second parameter) */
f1ab00af 103 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
f1ab00af 104
4bcdf152 105 /* Move RIP to its proper location */
85f6f029 106 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
094dfc54 107 movq %rdi, RIP(%rsp)
f1ab00af
SRRH
108
109 /*
110 * Now %rdi (the first parameter) has the return address of
111 * where ftrace_call returns. But the callbacks expect the
6a06bdbf 112 * address of the call itself.
f1ab00af
SRRH
113 */
114 subq $MCOUNT_INSN_SIZE, %rdi
4bcdf152
SRRH
115 .endm
116
562955fe
SRV
117.macro restore_mcount_regs save=0
118
119 /* ftrace_regs_caller or frame pointers require this */
120 movq RBP(%rsp), %rbp
121
4bcdf152
SRRH
122 movq R9(%rsp), %r9
123 movq R8(%rsp), %r8
124 movq RDI(%rsp), %rdi
125 movq RSI(%rsp), %rsi
126 movq RDX(%rsp), %rdx
127 movq RCX(%rsp), %rcx
128 movq RAX(%rsp), %rax
0687c36e 129
562955fe 130 addq $MCOUNT_REG_SIZE-\save, %rsp
0687c36e 131
4bcdf152
SRRH
132 .endm
133
883bbbff 134SYM_TYPED_FUNC_START(ftrace_stub)
bd194611 135 CALL_DEPTH_ACCOUNT
883bbbff
PZ
136 RET
137SYM_FUNC_END(ftrace_stub)
138
aa69f814 139#ifdef CONFIG_FUNCTION_GRAPH_TRACER
883bbbff 140SYM_TYPED_FUNC_START(ftrace_stub_graph)
bd194611 141 CALL_DEPTH_ACCOUNT
883bbbff
PZ
142 RET
143SYM_FUNC_END(ftrace_stub_graph)
aa69f814 144#endif
883bbbff 145
76c2f13c
SRRH
146#ifdef CONFIG_DYNAMIC_FTRACE
147
0f42c1ad 148SYM_FUNC_START(__fentry__)
ee3e2469 149 CALL_DEPTH_ACCOUNT
f94909ce 150 RET
0f42c1ad
BP
151SYM_FUNC_END(__fentry__)
152EXPORT_SYMBOL(__fentry__)
76c2f13c 153
6dcc5627 154SYM_FUNC_START(ftrace_caller)
f1ab00af
SRRH
155 /* save_mcount_regs fills in first two parameters */
156 save_mcount_regs
157
ee3e2469
PZ
158 CALL_DEPTH_ACCOUNT
159
02a474ca
SRV
160 /* Stack - skipping return address of ftrace_caller */
161 leaq MCOUNT_REG_SIZE+8(%rsp), %rcx
162 movq %rcx, RSP(%rsp)
163
26ba4e57 164SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
3215de84 165 ANNOTATE_NOENDBR
f1ab00af
SRRH
166 /* Load the ftrace_ops into the 3rd parameter */
167 movq function_trace_op(%rip), %rdx
168
02a474ca
SRV
169 /* regs go into 4th parameter */
170 leaq (%rsp), %rcx
171
172 /* Only ops with REGS flag set should have CS register set */
173 movq $0, CS(%rsp)
e18eead3 174
ee3e2469
PZ
175 /* Account for the function call below */
176 CALL_DEPTH_ACCOUNT
177
26ba4e57 178SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
3215de84 179 ANNOTATE_NOENDBR
e18eead3
SR
180 call ftrace_stub
181
2860cd8a
SRV
182 /* Handlers can change the RIP */
183 movq RIP(%rsp), %rax
184 movq %rax, MCOUNT_REG_SIZE(%rsp)
185
05df710e 186 restore_mcount_regs
f3bea491
SRRH
187
188 /*
f1b92bb6
BP
189 * The code up to this label is copied into trampolines so
190 * think twice before adding any new code or changing the
191 * layout here.
f3bea491 192 */
0298739b 193SYM_INNER_LABEL(ftrace_caller_end, SYM_L_GLOBAL)
3215de84 194 ANNOTATE_NOENDBR
eac828ea 195 RET
0298739b 196SYM_FUNC_END(ftrace_caller);
7b6c7a87 197STACK_FRAME_NON_STANDARD_FP(ftrace_caller)
0298739b 198
6dcc5627 199SYM_FUNC_START(ftrace_regs_caller)
527aa75b 200 /* Save the current flags before any operations that can change them */
e18eead3
SR
201 pushfq
202
527aa75b 203 /* added 8 bytes to save flags */
f1ab00af
SRRH
204 save_mcount_regs 8
205 /* save_mcount_regs fills in first two parameters */
206
ee3e2469
PZ
207 CALL_DEPTH_ACCOUNT
208
26ba4e57 209SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
3215de84 210 ANNOTATE_NOENDBR
f1ab00af
SRRH
211 /* Load the ftrace_ops into the 3rd parameter */
212 movq function_trace_op(%rip), %rdx
e18eead3
SR
213
214 /* Save the rest of pt_regs */
215 movq %r15, R15(%rsp)
216 movq %r14, R14(%rsp)
217 movq %r13, R13(%rsp)
218 movq %r12, R12(%rsp)
219 movq %r11, R11(%rsp)
220 movq %r10, R10(%rsp)
e18eead3
SR
221 movq %rbx, RBX(%rsp)
222 /* Copy saved flags */
85f6f029 223 movq MCOUNT_REG_SIZE(%rsp), %rcx
e18eead3
SR
224 movq %rcx, EFLAGS(%rsp)
225 /* Kernel segments */
226 movq $__KERNEL_DS, %rcx
227 movq %rcx, SS(%rsp)
228 movq $__KERNEL_CS, %rcx
229 movq %rcx, CS(%rsp)
527aa75b 230 /* Stack - skipping return address and flags */
85f6f029 231 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
e18eead3
SR
232 movq %rcx, RSP(%rsp)
233
ea1ed38d
PZ
234 ENCODE_FRAME_POINTER
235
e18eead3
SR
236 /* regs go into 4th parameter */
237 leaq (%rsp), %rcx
238
ee3e2469
PZ
239 /* Account for the function call below */
240 CALL_DEPTH_ACCOUNT
241
26ba4e57 242SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
3215de84 243 ANNOTATE_NOENDBR
e18eead3
SR
244 call ftrace_stub
245
246 /* Copy flags back to SS, to restore them */
247 movq EFLAGS(%rsp), %rax
85f6f029 248 movq %rax, MCOUNT_REG_SIZE(%rsp)
e18eead3
SR
249
250 /* Handlers can change the RIP */
251 movq RIP(%rsp), %rax
85f6f029 252 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
e18eead3
SR
253
254 /* restore the rest of pt_regs */
255 movq R15(%rsp), %r15
256 movq R14(%rsp), %r14
257 movq R13(%rsp), %r13
258 movq R12(%rsp), %r12
259 movq R10(%rsp), %r10
e18eead3
SR
260 movq RBX(%rsp), %rbx
261
562955fe
SRV
262 movq ORIG_RAX(%rsp), %rax
263 movq %rax, MCOUNT_REG_SIZE-8(%rsp)
264
0298739b
PZ
265 /*
266 * If ORIG_RAX is anything but zero, make this a call to that.
267 * See arch_ftrace_set_direct_caller().
268 */
9f2dfd61 269 testq %rax, %rax
fe58acef 270SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
3215de84 271 ANNOTATE_NOENDBR
0b4f8ddc 272 jnz 1f
562955fe 273
0b4f8ddc 274 restore_mcount_regs
e18eead3
SR
275 /* Restore flags */
276 popfq
277
f3bea491 278 /*
eac828ea 279 * The trampoline will add the return.
f3bea491 280 */
5da7cd11 281SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
3215de84 282 ANNOTATE_NOENDBR
eac828ea 283 RET
fdc841b5 284
0b4f8ddc
SRV
285 /* Swap the flags with orig_rax */
2861: movq MCOUNT_REG_SIZE(%rsp), %rdi
287 movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
288 movq %rax, MCOUNT_REG_SIZE(%rsp)
289
290 restore_mcount_regs 8
291 /* Restore flags */
292 popfq
b735bd3e 293 UNWIND_HINT_FUNC
36b64f10
PZ
294
295 /*
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.
299 */
300 ANNOTATE_INTRA_FUNCTION_CALL
301 CALL .Ldo_rebalance
302 int3
303.Ldo_rebalance:
304 add $8, %rsp
ee3e2469
PZ
305 ALTERNATIVE __stringify(RET), \
306 __stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
307 X86_FEATURE_CALL_DEPTH
0b4f8ddc 308
6dcc5627 309SYM_FUNC_END(ftrace_regs_caller)
7b6c7a87 310STACK_FRAME_NON_STANDARD_FP(ftrace_regs_caller)
e18eead3
SR
311
312
313#else /* ! CONFIG_DYNAMIC_FTRACE */
314
0f42c1ad 315SYM_FUNC_START(__fentry__)
ee3e2469
PZ
316 CALL_DEPTH_ACCOUNT
317
e18eead3
SR
318 cmpq $ftrace_stub, ftrace_trace_function
319 jnz trace
f94909ce 320 RET
e18eead3
SR
321
322trace:
f1ab00af
SRRH
323 /* save_mcount_regs fills in first two parameters */
324 save_mcount_regs
e18eead3 325
112677d6
NK
326 /*
327 * When DYNAMIC_FTRACE is not defined, ARCH_SUPPORTS_FTRACE_OPS is not
328 * set (see include/asm/ftrace.h and include/linux/ftrace.h). Only the
329 * ip and parent ip are used and the list function is called when
330 * function tracing is enabled.
331 */
9351803b 332 movq ftrace_trace_function, %r8
34fdce69 333 CALL_NOSPEC r8
05df710e 334 restore_mcount_regs
e18eead3 335
0c0593b4 336 jmp ftrace_stub
0f42c1ad
BP
337SYM_FUNC_END(__fentry__)
338EXPORT_SYMBOL(__fentry__)
7b6c7a87
JP
339STACK_FRAME_NON_STANDARD_FP(__fentry__)
340
e18eead3 341#endif /* CONFIG_DYNAMIC_FTRACE */
e18eead3
SR
342
343#ifdef CONFIG_FUNCTION_GRAPH_TRACER
7b6c7a87
JP
344SYM_CODE_START(return_to_handler)
345 UNWIND_HINT_EMPTY
346 ANNOTATE_NOENDBR
e52fc2cf 347 subq $16, %rsp
e18eead3
SR
348
349 /* Save the return values */
350 movq %rax, (%rsp)
351 movq %rdx, 8(%rsp)
352 movq %rbp, %rdi
353
354 call ftrace_return_to_handler
355
356 movq %rax, %rdi
357 movq 8(%rsp), %rdx
358 movq (%rsp), %rax
e52fc2cf
PZ
359
360 addq $16, %rsp
361 /*
362 * Jump back to the old return address. This cannot be JMP_NOSPEC rdi
363 * since IBT would demand that contain ENDBR, which simply isn't so for
364 * return addresses. Use a retpoline here to keep the RSB balanced.
365 */
366 ANNOTATE_INTRA_FUNCTION_CALL
367 call .Ldo_rop
368 int3
369.Ldo_rop:
370 mov %rdi, (%rsp)
ee3e2469
PZ
371 ALTERNATIVE __stringify(RET), \
372 __stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
373 X86_FEATURE_CALL_DEPTH
7b6c7a87 374SYM_CODE_END(return_to_handler)
e18eead3 375#endif