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>
12 #include <asm/frame.h>
14 #ifdef CC_USING_FENTRY
15 # define function_hook __fentry__
16 EXPORT_SYMBOL(__fentry__)
18 # define function_hook mcount
22 #ifdef CONFIG_DYNAMIC_FTRACE
24 /* mcount uses a frame pointer even if CONFIG_FRAME_POINTER is not set */
25 #if !defined(CC_USING_FENTRY) || defined(CONFIG_FRAME_POINTER)
26 # define USING_FRAME_POINTER
29 #ifdef USING_FRAME_POINTER
30 # define MCOUNT_FRAME 1 /* using frame = true */
32 # define MCOUNT_FRAME 0 /* using frame = false */
41 #ifdef USING_FRAME_POINTER
42 # ifdef CC_USING_FENTRY
44 * Frame pointers are of ip followed by bp.
45 * Since fentry is an immediate jump, we are left with
46 * parent-ip, function-ip. We need to add a frame with
47 * parent-ip followed by ebp.
49 pushl 4(%esp) /* parent ip */
52 pushl 2*4(%esp) /* function ip */
54 /* For mcount, the function ip is directly above */
61 pushl $0 /* Pass NULL as regs pointer */
63 #ifdef USING_FRAME_POINTER
64 /* Load parent ebp into edx */
67 /* There's no frame pointer, load the appropriate stack addr instead */
71 movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
72 /* Get the parent ip */
73 movl 4(%edx), %edx /* edx has ebp */
75 movl function_trace_op, %ecx
76 subl $MCOUNT_INSN_SIZE, %eax
82 addl $4, %esp /* skip NULL pointer */
86 #ifdef USING_FRAME_POINTER
88 # ifdef CC_USING_FENTRY
89 addl $4,%esp /* skip function ip */
90 popl %ebp /* this is the orig bp */
91 addl $4, %esp /* skip parent ip */
95 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
96 .globl ftrace_graph_call
101 /* This is weak to keep gas from relaxing the jumps */
106 ENTRY(ftrace_regs_caller)
108 * i386 does not save SS and ESP when coming from kernel.
109 * Instead, to get sp, ®s->sp is used (see ptrace.h).
110 * Unfortunately, that means eflags must be at the same location
111 * as the current return ip is. We move the return ip into the
112 * regs->ip location, and move flags into the return ip location.
115 pushl 4(%esp) /* Save the return ip */
116 pushl $0 /* Load 0 into orig_ax */
123 /* Get flags and place them into the return ip slot */
137 movl 12*4(%esp), %eax /* Load ip (1st parameter) */
138 subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
139 #ifdef CC_USING_FENTRY
140 movl 15*4(%esp), %edx /* Load parent ip (2nd parameter) */
142 movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
144 movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
145 pushl %esp /* Save pt_regs as 4th parameter */
147 GLOBAL(ftrace_regs_call)
150 addl $4, %esp /* Skip pt_regs */
156 /* Move return ip back to its original location */
157 movl 12*4(%esp), %eax
158 movl %eax, 14*4(%esp)
172 /* use lea to not affect flags */
173 lea 3*4(%esp), %esp /* Skip orig_ax, ip and cs */
176 #else /* ! CONFIG_DYNAMIC_FTRACE */
179 cmpl $__PAGE_OFFSET, %esp
180 jb ftrace_stub /* Paging not enabled yet? */
182 cmpl $ftrace_stub, ftrace_trace_function
184 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
185 cmpl $ftrace_stub, ftrace_graph_return
186 jnz ftrace_graph_caller
188 cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
189 jnz ftrace_graph_caller
195 /* taken from glibc */
202 subl $MCOUNT_INSN_SIZE, %eax
204 movl ftrace_trace_function, %ecx
212 #endif /* CONFIG_DYNAMIC_FTRACE */
214 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
215 ENTRY(ftrace_graph_caller)
220 /* Even with frame pointers, fentry doesn't have one here */
221 #ifdef CC_USING_FENTRY
228 subl $MCOUNT_INSN_SIZE, %eax
229 call prepare_ftrace_return
234 END(ftrace_graph_caller)
236 .globl return_to_handler
240 #ifdef CC_USING_FENTRY
245 call ftrace_return_to_handler