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>
13 #include <asm/asm-offsets.h>
15 # define function_hook __fentry__
16 EXPORT_SYMBOL(__fentry__)
18 #ifdef CONFIG_FRAME_POINTER
19 # define MCOUNT_FRAME 1 /* using frame = true */
21 # define MCOUNT_FRAME 0 /* using frame = false */
30 #ifdef CONFIG_FRAME_POINTER
32 * Frame pointers are of ip followed by bp.
33 * Since fentry is an immediate jump, we are left with
34 * parent-ip, function-ip. We need to add a frame with
35 * parent-ip followed by ebp.
37 pushl 4(%esp) /* parent ip */
40 pushl 2*4(%esp) /* function ip */
42 /* For mcount, the function ip is directly above */
49 pushl $0 /* Pass NULL as regs pointer */
51 #ifdef CONFIG_FRAME_POINTER
52 /* Load parent ebp into edx */
55 /* There's no frame pointer, load the appropriate stack addr instead */
59 movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
60 /* Get the parent ip */
61 movl 4(%edx), %edx /* edx has ebp */
63 movl function_trace_op, %ecx
64 subl $MCOUNT_INSN_SIZE, %eax
70 addl $4, %esp /* skip NULL pointer */
74 #ifdef CONFIG_FRAME_POINTER
76 addl $4,%esp /* skip function ip */
77 popl %ebp /* this is the orig bp */
78 addl $4, %esp /* skip parent ip */
81 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
82 .globl ftrace_graph_call
87 /* This is weak to keep gas from relaxing the jumps */
92 ENTRY(ftrace_regs_caller)
94 * We're here from an mcount/fentry CALL, and the stack frame looks like:
99 * The purpose of this function is to call out in an emulated INT3
100 * environment with a stack frame like:
109 * We do _NOT_ restore: ss, flags, cs, gs, fs, es, ds
111 subl $3*4, %esp # RET-IP + 3 gaps
113 pushl %esp # points at ss
114 addl $5*4, (%esp) # make it point at <previous context>
116 pushl $__KERNEL_CS # cs
117 pushl 7*4(%esp) # ip <- RET-IP
135 movl PT_EIP(%esp), %eax # 1st argument: IP
136 subl $MCOUNT_INSN_SIZE, %eax
137 movl 21*4(%esp), %edx # 2nd argument: parent ip
138 movl function_trace_op, %ecx # 3rd argument: ftrace_pos
139 pushl %esp # 4th argument: pt_regs
141 GLOBAL(ftrace_regs_call)
144 addl $4, %esp # skip 4th argument
146 /* place IP below the new SP */
147 movl PT_OLDESP(%esp), %eax
148 movl PT_EIP(%esp), %ecx
151 /* place EAX below that */
152 movl PT_EAX(%esp), %ecx
167 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
168 ENTRY(ftrace_graph_caller)
173 /* Even with frame pointers, fentry doesn't have one here */
176 subl $MCOUNT_INSN_SIZE, %eax
177 call prepare_ftrace_return
182 END(ftrace_graph_caller)
184 .globl return_to_handler
189 call ftrace_return_to_handler