blob: e405fe1a8bf41931fb77b63e1a16ed5d821dc7b9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -04002/*
3 * Copyright (C) 2017 Steven Rostedt, VMware Inc.
4 */
5
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>
David Woodhouse93518032018-01-11 21:46:29 +000011#include <asm/nospec-branch.h>
Peter Zijlstraea1ed382019-05-07 23:25:50 +020012#include <asm/frame.h>
Peter Zijlstra3c88c692019-05-07 23:25:54 +020013#include <asm/asm-offsets.h>
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040014
Steven Rostedt (VMware)562e14f2019-05-09 15:32:05 -040015#ifdef CONFIG_FRAME_POINTER
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040016# define MCOUNT_FRAME 1 /* using frame = true */
17#else
18# define MCOUNT_FRAME 0 /* using frame = false */
19#endif
20
Borislav Petkov0f42c1a2019-10-21 17:18:23 +020021SYM_FUNC_START(__fentry__)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040022 ret
Borislav Petkov0f42c1a2019-10-21 17:18:23 +020023SYM_FUNC_END(__fentry__)
24EXPORT_SYMBOL(__fentry__)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040025
Jiri Slaby5e633062019-10-11 13:51:06 +020026SYM_CODE_START(ftrace_caller)
Steven Rostedt (VMware)e6928e52017-03-23 10:33:50 -040027
Steven Rostedt (VMware)562e14f2019-05-09 15:32:05 -040028#ifdef CONFIG_FRAME_POINTER
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040029 /*
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.
34 */
35 pushl 4(%esp) /* parent ip */
Steven Rostedt (VMware)e6928e52017-03-23 10:33:50 -040036 pushl %ebp
37 movl %esp, %ebp
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040038 pushl 2*4(%esp) /* function ip */
Steven Rostedt (VMware)562e14f2019-05-09 15:32:05 -040039
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040040 /* For mcount, the function ip is directly above */
41 pushl %ebp
42 movl %esp, %ebp
43#endif
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040044 pushl %eax
45 pushl %ecx
46 pushl %edx
47 pushl $0 /* Pass NULL as regs pointer */
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040048
Steven Rostedt (VMware)562e14f2019-05-09 15:32:05 -040049#ifdef CONFIG_FRAME_POINTER
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040050 /* Load parent ebp into edx */
Steven Rostedt (VMware)e6928e52017-03-23 10:33:50 -040051 movl 4*4(%esp), %edx
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040052#else
53 /* There's no frame pointer, load the appropriate stack addr instead */
54 lea 4*4(%esp), %edx
55#endif
56
57 movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
Steven Rostedt (VMware)e6928e52017-03-23 10:33:50 -040058 /* Get the parent ip */
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040059 movl 4(%edx), %edx /* edx has ebp */
60
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040061 movl function_trace_op, %ecx
62 subl $MCOUNT_INSN_SIZE, %eax
63
64.globl ftrace_call
65ftrace_call:
66 call ftrace_stub
67
68 addl $4, %esp /* skip NULL pointer */
69 popl %edx
70 popl %ecx
71 popl %eax
Steven Rostedt (VMware)562e14f2019-05-09 15:32:05 -040072#ifdef CONFIG_FRAME_POINTER
Steven Rostedt (VMware)e6928e52017-03-23 10:33:50 -040073 popl %ebp
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040074 addl $4,%esp /* skip function ip */
75 popl %ebp /* this is the orig bp */
76 addl $4, %esp /* skip parent ip */
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -040077#endif
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040078.Lftrace_ret:
79#ifdef CONFIG_FUNCTION_GRAPH_TRACER
80.globl ftrace_graph_call
81ftrace_graph_call:
82 jmp ftrace_stub
83#endif
84
85/* This is weak to keep gas from relaxing the jumps */
Jiri Slaby13fbe782019-10-11 13:51:08 +020086SYM_INNER_LABEL_ALIGN(ftrace_stub, SYM_L_WEAK)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040087 ret
Jiri Slaby5e633062019-10-11 13:51:06 +020088SYM_CODE_END(ftrace_caller)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040089
Jiri Slaby78762b02019-10-11 13:51:05 +020090SYM_CODE_START(ftrace_regs_caller)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -040091 /*
Peter Zijlstra3c88c692019-05-07 23:25:54 +020092 * We're here from an mcount/fentry CALL, and the stack frame looks like:
93 *
94 * <previous context>
95 * RET-IP
96 *
97 * The purpose of this function is to call out in an emulated INT3
98 * environment with a stack frame like:
99 *
100 * <previous context>
101 * gap / RET-IP
102 * gap
103 * gap
104 * gap
105 * pt_regs
106 *
107 * We do _NOT_ restore: ss, flags, cs, gs, fs, es, ds
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400108 */
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200109 subl $3*4, %esp # RET-IP + 3 gaps
110 pushl %ss # ss
111 pushl %esp # points at ss
112 addl $5*4, (%esp) # make it point at <previous context>
113 pushfl # flags
114 pushl $__KERNEL_CS # cs
115 pushl 7*4(%esp) # ip <- RET-IP
116 pushl $0 # orig_eax
117
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400118 pushl %gs
119 pushl %fs
120 pushl %es
121 pushl %ds
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200122
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400123 pushl %eax
124 pushl %ebp
125 pushl %edi
126 pushl %esi
127 pushl %edx
128 pushl %ecx
129 pushl %ebx
130
Peter Zijlstraea1ed382019-05-07 23:25:50 +0200131 ENCODE_FRAME_POINTER
132
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200133 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
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400138
Jiri Slaby26ba4e52019-10-11 13:50:57 +0200139SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400140 call ftrace_stub
141
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200142 addl $4, %esp # skip 4th argument
Steven Rostedt (VMware)ff04b442017-03-23 10:33:51 -0400143
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200144 /* place IP below the new SP */
145 movl PT_OLDESP(%esp), %eax
146 movl PT_EIP(%esp), %ecx
147 movl %ecx, -4(%eax)
Steven Rostedt (VMware)ff04b442017-03-23 10:33:51 -0400148
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200149 /* place EAX below that */
150 movl PT_EAX(%esp), %ecx
151 movl %ecx, -8(%eax)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400152
153 popl %ebx
154 popl %ecx
155 popl %edx
156 popl %esi
157 popl %edi
158 popl %ebp
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400159
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200160 lea -8(%eax), %esp
161 popl %eax
Steven Rostedt (VMware)ff04b442017-03-23 10:33:51 -0400162
163 jmp .Lftrace_ret
Jiri Slaby78762b02019-10-11 13:51:05 +0200164SYM_CODE_END(ftrace_regs_caller)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400165
166#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Jiri Slaby5e633062019-10-11 13:51:06 +0200167SYM_CODE_START(ftrace_graph_caller)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400168 pushl %eax
169 pushl %ecx
170 pushl %edx
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -0400171 movl 3*4(%esp), %eax
172 /* Even with frame pointers, fentry doesn't have one here */
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -0400173 lea 4*4(%esp), %edx
174 movl $0, %ecx
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400175 subl $MCOUNT_INSN_SIZE, %eax
176 call prepare_ftrace_return
177 popl %edx
178 popl %ecx
179 popl %eax
180 ret
Jiri Slaby5e633062019-10-11 13:51:06 +0200181SYM_CODE_END(ftrace_graph_caller)
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400182
183.globl return_to_handler
184return_to_handler:
185 pushl %eax
186 pushl %edx
Steven Rostedt (VMware)644e0e82017-03-23 10:33:52 -0400187 movl $0, %eax
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400188 call ftrace_return_to_handler
189 movl %eax, %ecx
190 popl %edx
191 popl %eax
Peter Zijlstra34fdce62020-04-22 17:16:40 +0200192 JMP_NOSPEC ecx
Steven Rostedt (VMware)3d82c592017-03-23 10:33:49 -0400193#endif