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