1228925Sgonzo// SPDX-License-Identifier: GPL-2.0-only
2228925Sgonzo#include <linux/module.h>
3228925Sgonzo#include <linux/kthread.h>
4228925Sgonzo#include <linux/ftrace.h>
5228925Sgonzo#ifndef CONFIG_ARM64
6228925Sgonzo#include <asm/asm-offsets.h>
7228925Sgonzo#endif
8228925Sgonzo
9228925Sgonzoextern void my_direct_func1(void);
10228925Sgonzoextern void my_direct_func2(void);
11228925Sgonzo
12228925Sgonzovoid my_direct_func1(void)
13228925Sgonzo{
14228925Sgonzo	trace_printk("my direct func1\n");
15228925Sgonzo}
16228925Sgonzo
17228925Sgonzovoid my_direct_func2(void)
18228925Sgonzo{
19228925Sgonzo	trace_printk("my direct func2\n");
20228925Sgonzo}
21228925Sgonzo
22228925Sgonzoextern void my_tramp1(void *);
23228925Sgonzoextern void my_tramp2(void *);
24228925Sgonzo
25228925Sgonzostatic unsigned long my_ip = (unsigned long)schedule;
26228925Sgonzo
27228925Sgonzo#ifdef CONFIG_RISCV
28228925Sgonzo#include <asm/asm.h>
29228925Sgonzo
30228925Sgonzoasm (
31228925Sgonzo"	.pushsection    .text, \"ax\", @progbits\n"
32228925Sgonzo"	.type		my_tramp1, @function\n"
33228925Sgonzo"	.globl		my_tramp1\n"
34228925Sgonzo"   my_tramp1:\n"
35228925Sgonzo"	addi	sp,sp,-2*"SZREG"\n"
36228925Sgonzo"	"REG_S"	t0,0*"SZREG"(sp)\n"
37228925Sgonzo"	"REG_S"	ra,1*"SZREG"(sp)\n"
38228925Sgonzo"	call	my_direct_func1\n"
39228925Sgonzo"	"REG_L"	t0,0*"SZREG"(sp)\n"
40228925Sgonzo"	"REG_L"	ra,1*"SZREG"(sp)\n"
41228925Sgonzo"	addi	sp,sp,2*"SZREG"\n"
42228925Sgonzo"	jr	t0\n"
43228925Sgonzo"	.size		my_tramp1, .-my_tramp1\n"
44228925Sgonzo"	.type		my_tramp2, @function\n"
45228925Sgonzo"	.globl		my_tramp2\n"
46228925Sgonzo
47228925Sgonzo"   my_tramp2:\n"
48228925Sgonzo"	addi	sp,sp,-2*"SZREG"\n"
49228925Sgonzo"	"REG_S"	t0,0*"SZREG"(sp)\n"
50228925Sgonzo"	"REG_S"	ra,1*"SZREG"(sp)\n"
51232812Sjmallett"	call	my_direct_func2\n"
52228925Sgonzo"	"REG_L"	t0,0*"SZREG"(sp)\n"
53228925Sgonzo"	"REG_L"	ra,1*"SZREG"(sp)\n"
54277996Sloos"	addi	sp,sp,2*"SZREG"\n"
55228925Sgonzo"	jr	t0\n"
56228925Sgonzo"	.size		my_tramp2, .-my_tramp2\n"
57228925Sgonzo"	.popsection\n"
58228925Sgonzo);
59228925Sgonzo
60228925Sgonzo#endif /* CONFIG_RISCV */
61228925Sgonzo
62228925Sgonzo#ifdef CONFIG_X86_64
63228925Sgonzo
64228925Sgonzo#include <asm/ibt.h>
65228925Sgonzo#include <asm/nospec-branch.h>
66228925Sgonzo
67228925Sgonzoasm (
68228925Sgonzo"	.pushsection    .text, \"ax\", @progbits\n"
69228925Sgonzo"	.type		my_tramp1, @function\n"
70228925Sgonzo"	.globl		my_tramp1\n"
71228925Sgonzo"   my_tramp1:"
72228925Sgonzo	ASM_ENDBR
73228925Sgonzo"	pushq %rbp\n"
74228925Sgonzo"	movq %rsp, %rbp\n"
75228925Sgonzo	CALL_DEPTH_ACCOUNT
76228925Sgonzo"	call my_direct_func1\n"
77228925Sgonzo"	leave\n"
78228925Sgonzo"	.size		my_tramp1, .-my_tramp1\n"
79228925Sgonzo	ASM_RET
80228925Sgonzo
81228925Sgonzo"	.type		my_tramp2, @function\n"
82228925Sgonzo"	.globl		my_tramp2\n"
83228925Sgonzo"   my_tramp2:"
84228925Sgonzo	ASM_ENDBR
85228925Sgonzo"	pushq %rbp\n"
86228925Sgonzo"	movq %rsp, %rbp\n"
87228925Sgonzo	CALL_DEPTH_ACCOUNT
88228925Sgonzo"	call my_direct_func2\n"
89228925Sgonzo"	leave\n"
90228925Sgonzo	ASM_RET
91228925Sgonzo"	.size		my_tramp2, .-my_tramp2\n"
92228925Sgonzo"	.popsection\n"
93228925Sgonzo);
94277996Sloos
95228925Sgonzo#endif /* CONFIG_X86_64 */
96228925Sgonzo
97228925Sgonzo#ifdef CONFIG_S390
98228925Sgonzo
99228925Sgonzoasm (
100228925Sgonzo"	.pushsection	.text, \"ax\", @progbits\n"
101228925Sgonzo"	.type		my_tramp1, @function\n"
102228925Sgonzo"	.globl		my_tramp1\n"
103228925Sgonzo"   my_tramp1:"
104228925Sgonzo"	lgr		%r1,%r15\n"
105228925Sgonzo"	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
106228925Sgonzo"	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
107228925Sgonzo"	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
108228925Sgonzo"	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
109228925Sgonzo"	brasl		%r14,my_direct_func1\n"
110228925Sgonzo"	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
111228925Sgonzo"	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
112228925Sgonzo"	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
113228925Sgonzo"	lgr		%r1,%r0\n"
114228925Sgonzo"	br		%r1\n"
115228925Sgonzo"	.size		my_tramp1, .-my_tramp1\n"
116228925Sgonzo"	.type		my_tramp2, @function\n"
117228925Sgonzo"	.globl		my_tramp2\n"
118228925Sgonzo"   my_tramp2:"
119228925Sgonzo"	lgr		%r1,%r15\n"
120228925Sgonzo"	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
121228925Sgonzo"	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
122228925Sgonzo"	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
123228925Sgonzo"	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
124228925Sgonzo"	brasl		%r14,my_direct_func2\n"
125228925Sgonzo"	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
126228925Sgonzo"	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
127228925Sgonzo"	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
128228925Sgonzo"	lgr		%r1,%r0\n"
129228925Sgonzo"	br		%r1\n"
130228925Sgonzo"	.size		my_tramp2, .-my_tramp2\n"
131228925Sgonzo"	.popsection\n"
132228925Sgonzo);
133228925Sgonzo
134228925Sgonzo#endif /* CONFIG_S390 */
135228925Sgonzo
136228925Sgonzo#ifdef CONFIG_ARM64
137228925Sgonzo
138228925Sgonzoasm (
139277996Sloos"	.pushsection    .text, \"ax\", @progbits\n"
140277996Sloos"	.type		my_tramp1, @function\n"
141277996Sloos"	.globl		my_tramp1\n"
142277996Sloos"   my_tramp1:"
143277996Sloos"	hint	34\n" // bti	c
144277996Sloos"	sub	sp, sp, #16\n"
145277996Sloos"	stp	x9, x30, [sp]\n"
146277996Sloos"	bl	my_direct_func1\n"
147277996Sloos"	ldp	x30, x9, [sp]\n"
148277996Sloos"	add	sp, sp, #16\n"
149228925Sgonzo"	ret	x9\n"
150228925Sgonzo"	.size		my_tramp1, .-my_tramp1\n"
151228925Sgonzo
152228925Sgonzo"	.type		my_tramp2, @function\n"
153228925Sgonzo"	.globl		my_tramp2\n"
154228925Sgonzo"   my_tramp2:"
155228925Sgonzo"	hint	34\n" // bti	c
156228925Sgonzo"	sub	sp, sp, #16\n"
157228925Sgonzo"	stp	x9, x30, [sp]\n"
158228925Sgonzo"	bl	my_direct_func2\n"
159228925Sgonzo"	ldp	x30, x9, [sp]\n"
160228925Sgonzo"	add	sp, sp, #16\n"
161228925Sgonzo"	ret	x9\n"
162228925Sgonzo"	.size		my_tramp2, .-my_tramp2\n"
163228925Sgonzo"	.popsection\n"
164228925Sgonzo);
165228925Sgonzo
166228925Sgonzo#endif /* CONFIG_ARM64 */
167228925Sgonzo
168228925Sgonzo#ifdef CONFIG_LOONGARCH
169228925Sgonzo
170228925Sgonzoasm (
171228925Sgonzo"	.pushsection    .text, \"ax\", @progbits\n"
172228925Sgonzo"	.type		my_tramp1, @function\n"
173228925Sgonzo"	.globl		my_tramp1\n"
174228925Sgonzo"   my_tramp1:\n"
175228925Sgonzo"	addi.d	$sp, $sp, -16\n"
176228925Sgonzo"	st.d	$t0, $sp, 0\n"
177228925Sgonzo"	st.d	$ra, $sp, 8\n"
178228925Sgonzo"	bl	my_direct_func1\n"
179228925Sgonzo"	ld.d	$t0, $sp, 0\n"
180228925Sgonzo"	ld.d	$ra, $sp, 8\n"
181228925Sgonzo"	addi.d	$sp, $sp, 16\n"
182228925Sgonzo"	jr	$t0\n"
183228925Sgonzo"	.size		my_tramp1, .-my_tramp1\n"
184228925Sgonzo
185228925Sgonzo"	.type		my_tramp2, @function\n"
186228925Sgonzo"	.globl		my_tramp2\n"
187228925Sgonzo"   my_tramp2:\n"
188228925Sgonzo"	addi.d	$sp, $sp, -16\n"
189228925Sgonzo"	st.d	$t0, $sp, 0\n"
190228925Sgonzo"	st.d	$ra, $sp, 8\n"
191228925Sgonzo"	bl	my_direct_func2\n"
192228925Sgonzo"	ld.d	$t0, $sp, 0\n"
193228925Sgonzo"	ld.d	$ra, $sp, 8\n"
194228925Sgonzo"	addi.d	$sp, $sp, 16\n"
195228925Sgonzo"	jr	$t0\n"
196228925Sgonzo"	.size		my_tramp2, .-my_tramp2\n"
197228925Sgonzo"	.popsection\n"
198228925Sgonzo);
199228925Sgonzo
200228925Sgonzo#endif /* CONFIG_LOONGARCH */
201228925Sgonzo
202228925Sgonzostatic struct ftrace_ops direct;
203228925Sgonzo
204228925Sgonzostatic unsigned long my_tramp = (unsigned long)my_tramp1;
205228925Sgonzostatic unsigned long tramps[2] = {
206228925Sgonzo	(unsigned long)my_tramp1,
207228925Sgonzo	(unsigned long)my_tramp2,
208228925Sgonzo};
209228925Sgonzo
210228925Sgonzostatic int simple_thread(void *arg)
211228925Sgonzo{
212228925Sgonzo	static int t;
213228925Sgonzo	int ret = 0;
214228925Sgonzo
215228925Sgonzo	while (!kthread_should_stop()) {
216228925Sgonzo		set_current_state(TASK_INTERRUPTIBLE);
217228925Sgonzo		schedule_timeout(2 * HZ);
218228925Sgonzo
219228925Sgonzo		if (ret)
220228925Sgonzo			continue;
221228925Sgonzo		t ^= 1;
222228925Sgonzo		ret = modify_ftrace_direct(&direct, tramps[t]);
223228925Sgonzo		if (!ret)
224228925Sgonzo			my_tramp = tramps[t];
225228925Sgonzo		WARN_ON_ONCE(ret);
226228925Sgonzo	}
227228925Sgonzo
228228925Sgonzo	return 0;
229228925Sgonzo}
230228925Sgonzo
231228925Sgonzostatic struct task_struct *simple_tsk;
232228925Sgonzo
233228925Sgonzostatic int __init ftrace_direct_init(void)
234274670Sloos{
235228925Sgonzo	int ret;
236228925Sgonzo
237228925Sgonzo	ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
238228925Sgonzo	ret = register_ftrace_direct(&direct, my_tramp);
239228925Sgonzo
240228925Sgonzo	if (!ret)
241228925Sgonzo		simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
242228925Sgonzo	return ret;
243228925Sgonzo}
244228925Sgonzo
245228925Sgonzostatic void __exit ftrace_direct_exit(void)
246228925Sgonzo{
247228925Sgonzo	kthread_stop(simple_tsk);
248228925Sgonzo	unregister_ftrace_direct(&direct, my_tramp, true);
249228925Sgonzo}
250228925Sgonzo
251228925Sgonzomodule_init(ftrace_direct_init);
252228925Sgonzomodule_exit(ftrace_direct_exit);
253228925Sgonzo
254228925SgonzoMODULE_AUTHOR("Steven Rostedt");
255228925SgonzoMODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
256228925SgonzoMODULE_LICENSE("GPL");
257228925Sgonzo