2 * Copyright (C) 2017 Steven Rostedt, VMware Inc.
5 #include <linux/linkage.h>
6 #include <asm/page_types.h>
7 #include <asm/segment.h>
8 #include <asm/export.h>
9 #include <asm/ftrace.h>
11 #ifdef CC_USING_FENTRY
12 # define function_hook __fentry__
13 EXPORT_SYMBOL(__fentry__)
15 # define function_hook mcount
19 #ifdef CONFIG_DYNAMIC_FTRACE
21 /* mcount uses a frame pointer even if CONFIG_FRAME_POINTER is not set */
22 #if !defined(CC_USING_FENTRY) || defined(CONFIG_FRAME_POINTER)
23 # define USING_FRAME_POINTER
26 #ifdef USING_FRAME_POINTER
27 # define MCOUNT_FRAME 1 /* using frame = true */
29 # define MCOUNT_FRAME 0 /* using frame = false */
38 #ifdef USING_FRAME_POINTER
39 # ifdef CC_USING_FENTRY
41 * Frame pointers are of ip followed by bp.
42 * Since fentry is an immediate jump, we are left with
43 * parent-ip, function-ip. We need to add a frame with
44 * parent-ip followed by ebp.
46 pushl 4(%esp) /* parent ip */
49 pushl 2*4(%esp) /* function ip */
51 /* For mcount, the function ip is directly above */
58 pushl $0 /* Pass NULL as regs pointer */
60 #ifdef USING_FRAME_POINTER
61 /* Load parent ebp into edx */
64 /* There's no frame pointer, load the appropriate stack addr instead */
68 movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
69 /* Get the parent ip */
70 movl 4(%edx), %edx /* edx has ebp */
72 movl function_trace_op, %ecx
73 subl $MCOUNT_INSN_SIZE, %eax
79 addl $4, %esp /* skip NULL pointer */
83 #ifdef USING_FRAME_POINTER
85 # ifdef CC_USING_FENTRY
86 addl $4,%esp /* skip function ip */
87 popl %ebp /* this is the orig bp */
88 addl $4, %esp /* skip parent ip */
92 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
93 .globl ftrace_graph_call
98 /* This is weak to keep gas from relaxing the jumps */
103 ENTRY(ftrace_regs_caller)
105 * i386 does not save SS and ESP when coming from kernel.
106 * Instead, to get sp, ®s->sp is used (see ptrace.h).
107 * Unfortunately, that means eflags must be at the same location
108 * as the current return ip is. We move the return ip into the
109 * regs->ip location, and move flags into the return ip location.
112 pushl 4(%esp) /* Save the return ip */
113 pushl $0 /* Load 0 into orig_ax */
120 /* Get flags and place them into the return ip slot */
132 movl 12*4(%esp), %eax /* Load ip (1st parameter) */
133 subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
134 #ifdef CC_USING_FENTRY
135 movl 15*4(%esp), %edx /* Load parent ip (2nd parameter) */
137 movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
139 movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
140 pushl %esp /* Save pt_regs as 4th parameter */
142 GLOBAL(ftrace_regs_call)
145 addl $4, %esp /* Skip pt_regs */
151 /* Move return ip back to its original location */
152 movl 12*4(%esp), %eax
153 movl %eax, 14*4(%esp)
167 /* use lea to not affect flags */
168 lea 3*4(%esp), %esp /* Skip orig_ax, ip and cs */
171 #else /* ! CONFIG_DYNAMIC_FTRACE */
174 cmpl $__PAGE_OFFSET, %esp
175 jb ftrace_stub /* Paging not enabled yet? */
177 cmpl $ftrace_stub, ftrace_trace_function
179 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
180 cmpl $ftrace_stub, ftrace_graph_return
181 jnz ftrace_graph_caller
183 cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
184 jnz ftrace_graph_caller
190 /* taken from glibc */
197 subl $MCOUNT_INSN_SIZE, %eax
199 call *ftrace_trace_function
206 #endif /* CONFIG_DYNAMIC_FTRACE */
208 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
209 ENTRY(ftrace_graph_caller)
214 /* Even with frame pointers, fentry doesn't have one here */
215 #ifdef CC_USING_FENTRY
222 subl $MCOUNT_INSN_SIZE, %eax
223 call prepare_ftrace_return
228 END(ftrace_graph_caller)
230 .globl return_to_handler
234 #ifdef CC_USING_FENTRY
239 call ftrace_return_to_handler