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_X86_64
28 
29 #include <asm/ibt.h>
30 #include <asm/nospec-branch.h>
31 
32 asm (
33 "	.pushsection    .text, \"ax\", @progbits\n"
34 "	.type		my_tramp1, @function\n"
35 "	.globl		my_tramp1\n"
36 "   my_tramp1:"
37 	ASM_ENDBR
38 "	pushq %rbp\n"
39 "	movq %rsp, %rbp\n"
40 	CALL_DEPTH_ACCOUNT
41 "	call my_direct_func1\n"
42 "	leave\n"
43 "	.size		my_tramp1, .-my_tramp1\n"
44 	ASM_RET
45 
46 "	.type		my_tramp2, @function\n"
47 "	.globl		my_tramp2\n"
48 "   my_tramp2:"
49 	ASM_ENDBR
50 "	pushq %rbp\n"
51 "	movq %rsp, %rbp\n"
52 	CALL_DEPTH_ACCOUNT
53 "	call my_direct_func2\n"
54 "	leave\n"
55 	ASM_RET
56 "	.size		my_tramp2, .-my_tramp2\n"
57 "	.popsection\n"
58 );
59 
60 #endif /* CONFIG_X86_64 */
61 
62 #ifdef CONFIG_S390
63 
64 asm (
65 "	.pushsection	.text, \"ax\", @progbits\n"
66 "	.type		my_tramp1, @function\n"
67 "	.globl		my_tramp1\n"
68 "   my_tramp1:"
69 "	lgr		%r1,%r15\n"
70 "	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
71 "	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
72 "	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
73 "	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
74 "	brasl		%r14,my_direct_func1\n"
75 "	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
76 "	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
77 "	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
78 "	lgr		%r1,%r0\n"
79 "	br		%r1\n"
80 "	.size		my_tramp1, .-my_tramp1\n"
81 "	.type		my_tramp2, @function\n"
82 "	.globl		my_tramp2\n"
83 "   my_tramp2:"
84 "	lgr		%r1,%r15\n"
85 "	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
86 "	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
87 "	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
88 "	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
89 "	brasl		%r14,my_direct_func2\n"
90 "	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
91 "	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
92 "	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
93 "	lgr		%r1,%r0\n"
94 "	br		%r1\n"
95 "	.size		my_tramp2, .-my_tramp2\n"
96 "	.popsection\n"
97 );
98 
99 #endif /* CONFIG_S390 */
100 
101 #ifdef CONFIG_ARM64
102 
103 asm (
104 "	.pushsection    .text, \"ax\", @progbits\n"
105 "	.type		my_tramp1, @function\n"
106 "	.globl		my_tramp1\n"
107 "   my_tramp1:"
108 "	bti	c\n"
109 "	sub	sp, sp, #16\n"
110 "	stp	x9, x30, [sp]\n"
111 "	bl	my_direct_func1\n"
112 "	ldp	x30, x9, [sp]\n"
113 "	add	sp, sp, #16\n"
114 "	ret	x9\n"
115 "	.size		my_tramp1, .-my_tramp1\n"
116 
117 "	.type		my_tramp2, @function\n"
118 "	.globl		my_tramp2\n"
119 "   my_tramp2:"
120 "	bti	c\n"
121 "	sub	sp, sp, #16\n"
122 "	stp	x9, x30, [sp]\n"
123 "	bl	my_direct_func2\n"
124 "	ldp	x30, x9, [sp]\n"
125 "	add	sp, sp, #16\n"
126 "	ret	x9\n"
127 "	.size		my_tramp2, .-my_tramp2\n"
128 "	.popsection\n"
129 );
130 
131 #endif /* CONFIG_ARM64 */
132 
133 #ifdef CONFIG_LOONGARCH
134 
135 asm (
136 "	.pushsection    .text, \"ax\", @progbits\n"
137 "	.type		my_tramp1, @function\n"
138 "	.globl		my_tramp1\n"
139 "   my_tramp1:\n"
140 "	addi.d	$sp, $sp, -16\n"
141 "	st.d	$t0, $sp, 0\n"
142 "	st.d	$ra, $sp, 8\n"
143 "	bl	my_direct_func1\n"
144 "	ld.d	$t0, $sp, 0\n"
145 "	ld.d	$ra, $sp, 8\n"
146 "	addi.d	$sp, $sp, 16\n"
147 "	jr	$t0\n"
148 "	.size		my_tramp1, .-my_tramp1\n"
149 
150 "	.type		my_tramp2, @function\n"
151 "	.globl		my_tramp2\n"
152 "   my_tramp2:\n"
153 "	addi.d	$sp, $sp, -16\n"
154 "	st.d	$t0, $sp, 0\n"
155 "	st.d	$ra, $sp, 8\n"
156 "	bl	my_direct_func2\n"
157 "	ld.d	$t0, $sp, 0\n"
158 "	ld.d	$ra, $sp, 8\n"
159 "	addi.d	$sp, $sp, 16\n"
160 "	jr	$t0\n"
161 "	.size		my_tramp2, .-my_tramp2\n"
162 "	.popsection\n"
163 );
164 
165 #endif /* CONFIG_LOONGARCH */
166 
167 static struct ftrace_ops direct;
168 
169 static unsigned long my_tramp = (unsigned long)my_tramp1;
170 static unsigned long tramps[2] = {
171 	(unsigned long)my_tramp1,
172 	(unsigned long)my_tramp2,
173 };
174 
175 static int simple_thread(void *arg)
176 {
177 	static int t;
178 	int ret = 0;
179 
180 	while (!kthread_should_stop()) {
181 		set_current_state(TASK_INTERRUPTIBLE);
182 		schedule_timeout(2 * HZ);
183 
184 		if (ret)
185 			continue;
186 		t ^= 1;
187 		ret = modify_ftrace_direct(&direct, tramps[t]);
188 		if (!ret)
189 			my_tramp = tramps[t];
190 		WARN_ON_ONCE(ret);
191 	}
192 
193 	return 0;
194 }
195 
196 static struct task_struct *simple_tsk;
197 
198 static int __init ftrace_direct_init(void)
199 {
200 	int ret;
201 
202 	ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
203 	ret = register_ftrace_direct(&direct, my_tramp);
204 
205 	if (!ret)
206 		simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
207 	return ret;
208 }
209 
210 static void __exit ftrace_direct_exit(void)
211 {
212 	kthread_stop(simple_tsk);
213 	unregister_ftrace_direct(&direct, my_tramp, true);
214 }
215 
216 module_init(ftrace_direct_init);
217 module_exit(ftrace_direct_exit);
218 
219 MODULE_AUTHOR("Steven Rostedt");
220 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
221 MODULE_LICENSE("GPL");
222