driver core: Make Kconfig text for DEBUG_TEST_DRIVER_REMOVE stronger
[cascardo/linux.git] / arch / x86 / kernel / mcount_64.S
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>
10 #include <asm/export.h>
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
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
39 /* Size of stack used to save mcount regs in save_mcount_regs */
40 #define MCOUNT_REG_SIZE         (SS+8 + MCOUNT_FRAME_SIZE)
41
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
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  */
67 .macro save_mcount_regs added=0
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
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)
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
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
121          /* Move RIP to its proper location */
122         movq MCOUNT_REG_SIZE+\added(%rsp), %rdi
123         movq %rdi, RIP(%rsp)
124
125         /*
126          * Now %rdi (the first parameter) has the return address of
127          * where ftrace_call returns. But the callbacks expect the
128          * address of the call itself.
129          */
130         subq $MCOUNT_INSN_SIZE, %rdi
131         .endm
132
133 .macro restore_mcount_regs
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
141
142         /* ftrace_regs_caller can modify %rbp */
143         movq RBP(%rsp), %rbp
144
145         addq $MCOUNT_REG_SIZE, %rsp
146
147         .endm
148
149 #ifdef CONFIG_DYNAMIC_FTRACE
150
151 ENTRY(function_hook)
152         retq
153 END(function_hook)
154
155 ENTRY(ftrace_caller)
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
163         /* regs go into 4th parameter (but make it NULL) */
164         movq $0, %rcx
165
166 GLOBAL(ftrace_call)
167         call ftrace_stub
168
169         restore_mcount_regs
170
171         /*
172          * The copied trampoline must call ftrace_epilogue as it
173          * still may need to call the function graph tracer.
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 /* This is weak to keep gas from relaxing the jumps */
187 WEAK(ftrace_stub)
188         retq
189 END(ftrace_caller)
190
191 ENTRY(ftrace_regs_caller)
192         /* Save the current flags before any operations that can change them */
193         pushfq
194
195         /* added 8 bytes to save flags */
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
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)
210         movq %rbx, RBX(%rsp)
211         /* Copy saved flags */
212         movq MCOUNT_REG_SIZE(%rsp), %rcx
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)
219         /* Stack - skipping return address and flags */
220         leaq MCOUNT_REG_SIZE+8*2(%rsp), %rcx
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
231         movq %rax, MCOUNT_REG_SIZE(%rsp)
232
233         /* Handlers can change the RIP */
234         movq RIP(%rsp), %rax
235         movq %rax, MCOUNT_REG_SIZE+8(%rsp)
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
243         movq RBX(%rsp), %rbx
244
245         restore_mcount_regs
246
247         /* Restore flags */
248         popfq
249
250         /*
251          * As this jmp to ftrace_epilogue can be a short jump
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
258         jmp ftrace_epilogue
259
260 END(ftrace_regs_caller)
261
262
263 #else /* ! CONFIG_DYNAMIC_FTRACE */
264
265 ENTRY(function_hook)
266         cmpq $ftrace_stub, ftrace_trace_function
267         jnz trace
268
269 fgraph_trace:
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:
282         /* save_mcount_regs fills in first two parameters */
283         save_mcount_regs
284
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          */
291         call   *ftrace_trace_function
292
293         restore_mcount_regs
294
295         jmp fgraph_trace
296 END(function_hook)
297 #endif /* CONFIG_DYNAMIC_FTRACE */
298 EXPORT_SYMBOL(function_hook)
299 #endif /* CONFIG_FUNCTION_TRACER */
300
301 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
302 ENTRY(ftrace_graph_caller)
303         /* Saves rbp into %rdx and fills first parameter  */
304         save_mcount_regs
305
306 #ifdef CC_USING_FENTRY
307         leaq MCOUNT_REG_SIZE+8(%rsp), %rsi
308         movq $0, %rdx   /* No framepointers needed */
309 #else
310         /* Save address of the return address of traced function */
311         leaq 8(%rdx), %rsi
312         /* ftrace does sanity checks against frame pointers */
313         movq (%rdx), %rdx
314 #endif
315         call    prepare_ftrace_return
316
317         restore_mcount_regs
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