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 #ifdef CONFIG_FRAME_POINTER
16 # define MCOUNT_FRAME 1 /* using frame = true */
18 # define MCOUNT_FRAME 0 /* using frame = false */
21 SYM_FUNC_START(__fentry__)
23 SYM_FUNC_END(__fentry__)
24 EXPORT_SYMBOL(__fentry__)
26 SYM_CODE_START(ftrace_caller)
28 #ifdef CONFIG_FRAME_POINTER
30 * Frame pointers are of ip followed by bp.
31 * Since fentry is an immediate jump, we are left with
32 * parent-ip, function-ip. We need to add a frame with
33 * parent-ip followed by ebp.
35 pushl 4(%esp) /* parent ip */
38 pushl 2*4(%esp) /* function ip */
40 /* For mcount, the function ip is directly above */
47 pushl $0 /* Pass NULL as regs pointer */
49 #ifdef CONFIG_FRAME_POINTER
50 /* Load parent ebp into edx */
53 /* There's no frame pointer, load the appropriate stack addr instead */
57 movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
58 /* Get the parent ip */
59 movl 4(%edx), %edx /* edx has ebp */
61 movl function_trace_op, %ecx
62 subl $MCOUNT_INSN_SIZE, %eax
68 addl $4, %esp /* skip NULL pointer */
72 #ifdef CONFIG_FRAME_POINTER
74 addl $4,%esp /* skip function ip */
75 popl %ebp /* this is the orig bp */
76 addl $4, %esp /* skip parent ip */
79 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
80 .globl ftrace_graph_call
85 /* This is weak to keep gas from relaxing the jumps */
86 SYM_INNER_LABEL_ALIGN(ftrace_stub, SYM_L_WEAK)
88 SYM_CODE_END(ftrace_caller)
90 SYM_CODE_START(ftrace_regs_caller)
92 * We're here from an mcount/fentry CALL, and the stack frame looks like:
97 * The purpose of this function is to call out in an emulated INT3
98 * environment with a stack frame like:
107 * We do _NOT_ restore: ss, flags, cs, gs, fs, es, ds
109 subl $3*4, %esp # RET-IP + 3 gaps
111 pushl %esp # points at ss
112 addl $5*4, (%esp) # make it point at <previous context>
114 pushl $__KERNEL_CS # cs
115 pushl 7*4(%esp) # ip <- RET-IP
133 movl PT_EIP(%esp), %eax # 1st argument: IP
134 subl $MCOUNT_INSN_SIZE, %eax
135 movl 21*4(%esp), %edx # 2nd argument: parent ip
136 movl function_trace_op, %ecx # 3rd argument: ftrace_pos
137 pushl %esp # 4th argument: pt_regs
139 SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
142 addl $4, %esp # skip 4th argument
144 /* place IP below the new SP */
145 movl PT_OLDESP(%esp), %eax
146 movl PT_EIP(%esp), %ecx
149 /* place EAX below that */
150 movl PT_EAX(%esp), %ecx
164 SYM_CODE_END(ftrace_regs_caller)
166 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
167 SYM_CODE_START(ftrace_graph_caller)
172 /* Even with frame pointers, fentry doesn't have one here */
175 subl $MCOUNT_INSN_SIZE, %eax
176 call prepare_ftrace_return
181 SYM_CODE_END(ftrace_graph_caller)
183 .globl return_to_handler
188 call ftrace_return_to_handler