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