1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2017 Steven Rostedt, VMware Inc.
6 #include <linux/linkage.h>
7 #include <asm/page_types.h>
8 #include <asm/segment.h>
9 #include <asm/export.h>
10 #include <asm/ftrace.h>
11 #include <asm/nospec-branch.h>
13 #ifdef CC_USING_FENTRY
14 # define function_hook __fentry__
15 EXPORT_SYMBOL(__fentry__)
17 # define function_hook mcount
21 #ifdef CONFIG_DYNAMIC_FTRACE
23 /* mcount uses a frame pointer even if CONFIG_FRAME_POINTER is not set */
24 #if !defined(CC_USING_FENTRY) || defined(CONFIG_FRAME_POINTER)
25 # define USING_FRAME_POINTER
28 #ifdef USING_FRAME_POINTER
29 # define MCOUNT_FRAME 1 /* using frame = true */
31 # define MCOUNT_FRAME 0 /* using frame = false */
40 #ifdef USING_FRAME_POINTER
41 # ifdef CC_USING_FENTRY
43 * Frame pointers are of ip followed by bp.
44 * Since fentry is an immediate jump, we are left with
45 * parent-ip, function-ip. We need to add a frame with
46 * parent-ip followed by ebp.
48 pushl 4(%esp) /* parent ip */
51 pushl 2*4(%esp) /* function ip */
53 /* For mcount, the function ip is directly above */
60 pushl $0 /* Pass NULL as regs pointer */
62 #ifdef USING_FRAME_POINTER
63 /* Load parent ebp into edx */
66 /* There's no frame pointer, load the appropriate stack addr instead */
70 movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
71 /* Get the parent ip */
72 movl 4(%edx), %edx /* edx has ebp */
74 movl function_trace_op, %ecx
75 subl $MCOUNT_INSN_SIZE, %eax
81 addl $4, %esp /* skip NULL pointer */
85 #ifdef USING_FRAME_POINTER
87 # ifdef CC_USING_FENTRY
88 addl $4,%esp /* skip function ip */
89 popl %ebp /* this is the orig bp */
90 addl $4, %esp /* skip parent ip */
94 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
95 .globl ftrace_graph_call
100 /* This is weak to keep gas from relaxing the jumps */
105 ENTRY(ftrace_regs_caller)
107 * i386 does not save SS and ESP when coming from kernel.
108 * Instead, to get sp, ®s->sp is used (see ptrace.h).
109 * Unfortunately, that means eflags must be at the same location
110 * as the current return ip is. We move the return ip into the
111 * regs->ip location, and move flags into the return ip location.
114 pushl 4(%esp) /* Save the return ip */
115 pushl $0 /* Load 0 into orig_ax */
122 /* Get flags and place them into the return ip slot */
134 movl 12*4(%esp), %eax /* Load ip (1st parameter) */
135 subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
136 #ifdef CC_USING_FENTRY
137 movl 15*4(%esp), %edx /* Load parent ip (2nd parameter) */
139 movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
141 movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
142 pushl %esp /* Save pt_regs as 4th parameter */
144 GLOBAL(ftrace_regs_call)
147 addl $4, %esp /* Skip pt_regs */
153 /* Move return ip back to its original location */
154 movl 12*4(%esp), %eax
155 movl %eax, 14*4(%esp)
169 /* use lea to not affect flags */
170 lea 3*4(%esp), %esp /* Skip orig_ax, ip and cs */
173 #else /* ! CONFIG_DYNAMIC_FTRACE */
176 cmpl $__PAGE_OFFSET, %esp
177 jb ftrace_stub /* Paging not enabled yet? */
179 cmpl $ftrace_stub, ftrace_trace_function
181 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
182 cmpl $ftrace_stub, ftrace_graph_return
183 jnz ftrace_graph_caller
185 cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
186 jnz ftrace_graph_caller
192 /* taken from glibc */
199 subl $MCOUNT_INSN_SIZE, %eax
201 movl ftrace_trace_function, %ecx
209 #endif /* CONFIG_DYNAMIC_FTRACE */
211 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
212 ENTRY(ftrace_graph_caller)
217 /* Even with frame pointers, fentry doesn't have one here */
218 #ifdef CC_USING_FENTRY
225 subl $MCOUNT_INSN_SIZE, %eax
226 call prepare_ftrace_return
231 END(ftrace_graph_caller)
233 .globl return_to_handler
237 #ifdef CC_USING_FENTRY
242 call ftrace_return_to_handler