1
2
3
4
5
6
7#include <linux/linkage.h>
8#include <asm/ptrace.h>
9#include <asm/ftrace.h>
10
11
12 .code64
13 .section .entry.text, "ax"
14
15
16#ifdef CONFIG_FUNCTION_TRACER
17
18#ifdef CC_USING_FENTRY
19# define function_hook __fentry__
20#else
21# define function_hook mcount
22#endif
23
24
25#ifdef CONFIG_FRAME_POINTER
26# ifdef CC_USING_FENTRY
27
28# define MCOUNT_FRAME_SIZE (8+16*2)
29# else
30
31# define MCOUNT_FRAME_SIZE (8+16)
32# endif
33#else
34
35# define MCOUNT_FRAME_SIZE 8
36#endif
37
38
39#define MCOUNT_REG_SIZE (SS+8 + MCOUNT_FRAME_SIZE)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66.macro save_mcount_regs added=0
67
68
69 pushq %rbp
70
71#ifdef CONFIG_FRAME_POINTER
72
73
74
75
76
77
78
79#ifdef CC_USING_FENTRY
80
81 pushq \added+8*2(%rsp)
82 pushq %rbp
83 movq %rsp, %rbp
84
85 pushq \added+8*3(%rsp)
86#else
87
88 pushq \added+8(%rsp)
89#endif
90 pushq %rbp
91 movq %rsp, %rbp
92#endif
93
94
95
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
107
108
109 movq MCOUNT_REG_SIZE-8(%rsp), %rdx
110 movq %rdx, RBP(%rsp)
111
112
113#ifdef CC_USING_FENTRY
114 movq MCOUNT_REG_SIZE+8+\added(%rsp), %rsi
115#else
116
117 movq 8(%rdx), %rsi
118#endif
119
120
121 movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
122 movq %rdi, RIP(%rsp)
123
124
125
126
127
128
129 subq $MCOUNT_INSN_SIZE, %rdi
130 .endm
131
132.macro restore_mcount_regs
133 movq R9(%rsp), %r9
134 movq R8(%rsp), %r8
135 movq RDI(%rsp), %rdi
136 movq RSI(%rsp), %rsi
137 movq RDX(%rsp), %rdx
138 movq RCX(%rsp), %rcx
139 movq RAX(%rsp), %rax
140
141
142 movq RBP(%rsp), %rbp
143
144 addq $MCOUNT_REG_SIZE, %rsp
145
146 .endm
147
148#ifdef CONFIG_DYNAMIC_FTRACE
149
150ENTRY(function_hook)
151 retq
152END(function_hook)
153
154ENTRY(ftrace_caller)
155
156 save_mcount_regs
157
158GLOBAL(ftrace_caller_op_ptr)
159
160 movq function_trace_op(%rip), %rdx
161
162
163 movq $0, %rcx
164
165GLOBAL(ftrace_call)
166 call ftrace_stub
167
168 restore_mcount_regs
169
170
171
172
173
174GLOBAL(ftrace_caller_end)
175
176GLOBAL(ftrace_return)
177
178#ifdef CONFIG_FUNCTION_GRAPH_TRACER
179GLOBAL(ftrace_graph_call)
180 jmp ftrace_stub
181#endif
182
183GLOBAL(ftrace_stub)
184 retq
185END(ftrace_caller)
186
187ENTRY(ftrace_regs_caller)
188
189 pushfq
190
191
192 save_mcount_regs 8
193
194
195GLOBAL(ftrace_regs_caller_op_ptr)
196
197 movq function_trace_op(%rip), %rdx
198
199
200 movq %r15, R15(%rsp)
201 movq %r14, R14(%rsp)
202 movq %r13, R13(%rsp)
203 movq %r12, R12(%rsp)
204 movq %r11, R11(%rsp)
205 movq %r10, R10(%rsp)
206 movq %rbx, RBX(%rsp)
207
208 movq MCOUNT_REG_SIZE(%rsp), %rcx
209 movq %rcx, EFLAGS(%rsp)
210
211 movq $__KERNEL_DS, %rcx
212 movq %rcx, SS(%rsp)
213 movq $__KERNEL_CS, %rcx
214 movq %rcx, CS(%rsp)
215
216 leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
217 movq %rcx, RSP(%rsp)
218
219
220 leaq (%rsp), %rcx
221
222GLOBAL(ftrace_regs_call)
223 call ftrace_stub
224
225
226 movq EFLAGS(%rsp), %rax
227 movq %rax, MCOUNT_REG_SIZE(%rsp)
228
229
230 movq RIP(%rsp), %rax
231 movq %rax, MCOUNT_REG_SIZE+8(%rsp)
232
233
234 movq R15(%rsp), %r15
235 movq R14(%rsp), %r14
236 movq R13(%rsp), %r13
237 movq R12(%rsp), %r12
238 movq R10(%rsp), %r10
239 movq RBX(%rsp), %rbx
240
241 restore_mcount_regs
242
243
244 popfq
245
246
247
248
249
250
251
252GLOBAL(ftrace_regs_caller_end)
253
254 jmp ftrace_return
255
256END(ftrace_regs_caller)
257
258
259#else
260
261ENTRY(function_hook)
262 cmpq $ftrace_stub, ftrace_trace_function
263 jnz trace
264
265fgraph_trace:
266#ifdef CONFIG_FUNCTION_GRAPH_TRACER
267 cmpq $ftrace_stub, ftrace_graph_return
268 jnz ftrace_graph_caller
269
270 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
271 jnz ftrace_graph_caller
272#endif
273
274GLOBAL(ftrace_stub)
275 retq
276
277trace:
278
279 save_mcount_regs
280
281 call *ftrace_trace_function
282
283 restore_mcount_regs
284
285 jmp fgraph_trace
286END(function_hook)
287#endif
288#endif
289
290#ifdef CONFIG_FUNCTION_GRAPH_TRACER
291ENTRY(ftrace_graph_caller)
292
293 save_mcount_regs
294
295#ifdef CC_USING_FENTRY
296 leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
297 movq $0, %rdx
298#else
299
300 leaq 8(%rdx), %rsi
301
302 movq (%rdx), %rdx
303#endif
304 call prepare_ftrace_return
305
306 restore_mcount_regs
307
308 retq
309END(ftrace_graph_caller)
310
311GLOBAL(return_to_handler)
312 subq $24, %rsp
313
314
315 movq %rax, (%rsp)
316 movq %rdx, 8(%rsp)
317 movq %rbp, %rdi
318
319 call ftrace_return_to_handler
320
321 movq %rax, %rdi
322 movq 8(%rsp), %rdx
323 movq (%rsp), %rax
324 addq $24, %rsp
325 jmp *%rdi
326#endif
327