xref: /linux/samples/ftrace/ftrace-direct-modify.c (revision 249ebf3f65f8530beb2cbfb91bff1d83ba88d23c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kthread.h>
4 #include <linux/ftrace.h>
5 #ifndef CONFIG_ARM64
6 #include <asm/asm-offsets.h>
7 #endif
8 
9 extern void my_direct_func1(void);
10 extern void my_direct_func2(void);
11 
12 void my_direct_func1(void)
13 {
14 	trace_printk("my direct func1\n");
15 }
16 
17 void my_direct_func2(void)
18 {
19 	trace_printk("my direct func2\n");
20 }
21 
22 extern void my_tramp1(void *);
23 extern void my_tramp2(void *);
24 
25 static unsigned long my_ip = (unsigned long)schedule;
26 
27 #ifdef CONFIG_RISCV
28 #include <asm/asm.h>
29 
30 asm (
31 "	.pushsection    .text, \"ax\", @progbits\n"
32 "	.type		my_tramp1, @function\n"
33 "	.globl		my_tramp1\n"
34 "   my_tramp1:\n"
35 "	addi	sp,sp,-2*"SZREG"\n"
36 "	"REG_S"	t0,0*"SZREG"(sp)\n"
37 "	"REG_S"	ra,1*"SZREG"(sp)\n"
38 "	call	my_direct_func1\n"
39 "	"REG_L"	t0,0*"SZREG"(sp)\n"
40 "	"REG_L"	ra,1*"SZREG"(sp)\n"
41 "	addi	sp,sp,2*"SZREG"\n"
42 "	jr	t0\n"
43 "	.size		my_tramp1, .-my_tramp1\n"
44 "	.type		my_tramp2, @function\n"
45 "	.globl		my_tramp2\n"
46 
47 "   my_tramp2:\n"
48 "	addi	sp,sp,-2*"SZREG"\n"
49 "	"REG_S"	t0,0*"SZREG"(sp)\n"
50 "	"REG_S"	ra,1*"SZREG"(sp)\n"
51 "	call	my_direct_func2\n"
52 "	"REG_L"	t0,0*"SZREG"(sp)\n"
53 "	"REG_L"	ra,1*"SZREG"(sp)\n"
54 "	addi	sp,sp,2*"SZREG"\n"
55 "	jr	t0\n"
56 "	.size		my_tramp2, .-my_tramp2\n"
57 "	.popsection\n"
58 );
59 
60 #endif /* CONFIG_RISCV */
61 
62 #ifdef CONFIG_X86_64
63 
64 #include <asm/ibt.h>
65 #include <asm/nospec-branch.h>
66 
67 asm (
68 "	.pushsection    .text, \"ax\", @progbits\n"
69 "	.type		my_tramp1, @function\n"
70 "	.globl		my_tramp1\n"
71 "   my_tramp1:"
72 	ASM_ENDBR
73 "	pushq %rbp\n"
74 "	movq %rsp, %rbp\n"
75 	CALL_DEPTH_ACCOUNT
76 "	call my_direct_func1\n"
77 "	leave\n"
78 "	.size		my_tramp1, .-my_tramp1\n"
79 	ASM_RET
80 
81 "	.type		my_tramp2, @function\n"
82 "	.globl		my_tramp2\n"
83 "   my_tramp2:"
84 	ASM_ENDBR
85 "	pushq %rbp\n"
86 "	movq %rsp, %rbp\n"
87 	CALL_DEPTH_ACCOUNT
88 "	call my_direct_func2\n"
89 "	leave\n"
90 	ASM_RET
91 "	.size		my_tramp2, .-my_tramp2\n"
92 "	.popsection\n"
93 );
94 
95 #endif /* CONFIG_X86_64 */
96 
97 #ifdef CONFIG_S390
98 
99 asm (
100 "	.pushsection	.text, \"ax\", @progbits\n"
101 "	.type		my_tramp1, @function\n"
102 "	.globl		my_tramp1\n"
103 "   my_tramp1:"
104 "	lgr		%r1,%r15\n"
105 "	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
106 "	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
107 "	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
108 "	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
109 "	brasl		%r14,my_direct_func1\n"
110 "	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
111 "	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
112 "	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
113 "	lgr		%r1,%r0\n"
114 "	br		%r1\n"
115 "	.size		my_tramp1, .-my_tramp1\n"
116 "	.type		my_tramp2, @function\n"
117 "	.globl		my_tramp2\n"
118 "   my_tramp2:"
119 "	lgr		%r1,%r15\n"
120 "	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
121 "	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
122 "	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
123 "	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
124 "	brasl		%r14,my_direct_func2\n"
125 "	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
126 "	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
127 "	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
128 "	lgr		%r1,%r0\n"
129 "	br		%r1\n"
130 "	.size		my_tramp2, .-my_tramp2\n"
131 "	.popsection\n"
132 );
133 
134 #endif /* CONFIG_S390 */
135 
136 #ifdef CONFIG_ARM64
137 
138 asm (
139 "	.pushsection    .text, \"ax\", @progbits\n"
140 "	.type		my_tramp1, @function\n"
141 "	.globl		my_tramp1\n"
142 "   my_tramp1:"
143 "	hint	34\n" // bti	c
144 "	sub	sp, sp, #16\n"
145 "	stp	x9, x30, [sp]\n"
146 "	bl	my_direct_func1\n"
147 "	ldp	x30, x9, [sp]\n"
148 "	add	sp, sp, #16\n"
149 "	ret	x9\n"
150 "	.size		my_tramp1, .-my_tramp1\n"
151 
152 "	.type		my_tramp2, @function\n"
153 "	.globl		my_tramp2\n"
154 "   my_tramp2:"
155 "	hint	34\n" // bti	c
156 "	sub	sp, sp, #16\n"
157 "	stp	x9, x30, [sp]\n"
158 "	bl	my_direct_func2\n"
159 "	ldp	x30, x9, [sp]\n"
160 "	add	sp, sp, #16\n"
161 "	ret	x9\n"
162 "	.size		my_tramp2, .-my_tramp2\n"
163 "	.popsection\n"
164 );
165 
166 #endif /* CONFIG_ARM64 */
167 
168 #ifdef CONFIG_LOONGARCH
169 
170 asm (
171 "	.pushsection    .text, \"ax\", @progbits\n"
172 "	.type		my_tramp1, @function\n"
173 "	.globl		my_tramp1\n"
174 "   my_tramp1:\n"
175 "	addi.d	$sp, $sp, -16\n"
176 "	st.d	$t0, $sp, 0\n"
177 "	st.d	$ra, $sp, 8\n"
178 "	bl	my_direct_func1\n"
179 "	ld.d	$t0, $sp, 0\n"
180 "	ld.d	$ra, $sp, 8\n"
181 "	addi.d	$sp, $sp, 16\n"
182 "	jr	$t0\n"
183 "	.size		my_tramp1, .-my_tramp1\n"
184 
185 "	.type		my_tramp2, @function\n"
186 "	.globl		my_tramp2\n"
187 "   my_tramp2:\n"
188 "	addi.d	$sp, $sp, -16\n"
189 "	st.d	$t0, $sp, 0\n"
190 "	st.d	$ra, $sp, 8\n"
191 "	bl	my_direct_func2\n"
192 "	ld.d	$t0, $sp, 0\n"
193 "	ld.d	$ra, $sp, 8\n"
194 "	addi.d	$sp, $sp, 16\n"
195 "	jr	$t0\n"
196 "	.size		my_tramp2, .-my_tramp2\n"
197 "	.popsection\n"
198 );
199 
200 #endif /* CONFIG_LOONGARCH */
201 
202 static struct ftrace_ops direct;
203 
204 static unsigned long my_tramp = (unsigned long)my_tramp1;
205 static unsigned long tramps[2] = {
206 	(unsigned long)my_tramp1,
207 	(unsigned long)my_tramp2,
208 };
209 
210 static int simple_thread(void *arg)
211 {
212 	static int t;
213 	int ret = 0;
214 
215 	while (!kthread_should_stop()) {
216 		set_current_state(TASK_INTERRUPTIBLE);
217 		schedule_timeout(2 * HZ);
218 
219 		if (ret)
220 			continue;
221 		t ^= 1;
222 		ret = modify_ftrace_direct(&direct, tramps[t]);
223 		if (!ret)
224 			my_tramp = tramps[t];
225 		WARN_ON_ONCE(ret);
226 	}
227 
228 	return 0;
229 }
230 
231 static struct task_struct *simple_tsk;
232 
233 static int __init ftrace_direct_init(void)
234 {
235 	int ret;
236 
237 	ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
238 	ret = register_ftrace_direct(&direct, my_tramp);
239 
240 	if (!ret)
241 		simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
242 	return ret;
243 }
244 
245 static void __exit ftrace_direct_exit(void)
246 {
247 	kthread_stop(simple_tsk);
248 	unregister_ftrace_direct(&direct, my_tramp, true);
249 }
250 
251 module_init(ftrace_direct_init);
252 module_exit(ftrace_direct_exit);
253 
254 MODULE_AUTHOR("Steven Rostedt");
255 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
256 MODULE_LICENSE("GPL");
257