1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kthread.h>
4 #include <linux/ftrace.h>
5 #include <asm/asm-offsets.h>
6 
7 extern void my_direct_func1(unsigned long ip);
8 extern void my_direct_func2(unsigned long ip);
9 
my_direct_func1(unsigned long ip)10 void my_direct_func1(unsigned long ip)
11 {
12 	trace_printk("my direct func1 ip %lx\n", ip);
13 }
14 
my_direct_func2(unsigned long ip)15 void my_direct_func2(unsigned long ip)
16 {
17 	trace_printk("my direct func2 ip %lx\n", ip);
18 }
19 
20 extern void my_tramp1(void *);
21 extern void my_tramp2(void *);
22 
23 #ifdef CONFIG_X86_64
24 
25 #include <asm/ibt.h>
26 #include <asm/nospec-branch.h>
27 
28 asm (
29 "	.pushsection    .text, \"ax\", @progbits\n"
30 "	.type		my_tramp1, @function\n"
31 "	.globl		my_tramp1\n"
32 "   my_tramp1:"
33 	ASM_ENDBR
34 "	pushq %rbp\n"
35 "	movq %rsp, %rbp\n"
36 	CALL_DEPTH_ACCOUNT
37 "	pushq %rdi\n"
38 "	movq 8(%rbp), %rdi\n"
39 "	call my_direct_func1\n"
40 "	popq %rdi\n"
41 "	leave\n"
42 	ASM_RET
43 "	.size		my_tramp1, .-my_tramp1\n"
44 
45 "	.type		my_tramp2, @function\n"
46 "	.globl		my_tramp2\n"
47 "   my_tramp2:"
48 	ASM_ENDBR
49 "	pushq %rbp\n"
50 "	movq %rsp, %rbp\n"
51 	CALL_DEPTH_ACCOUNT
52 "	pushq %rdi\n"
53 "	movq 8(%rbp), %rdi\n"
54 "	call my_direct_func2\n"
55 "	popq %rdi\n"
56 "	leave\n"
57 	ASM_RET
58 "	.size		my_tramp2, .-my_tramp2\n"
59 "	.popsection\n"
60 );
61 
62 #endif /* CONFIG_X86_64 */
63 
64 #ifdef CONFIG_S390
65 
66 asm (
67 "       .pushsection    .text, \"ax\", @progbits\n"
68 "       .type           my_tramp1, @function\n"
69 "       .globl          my_tramp1\n"
70 "   my_tramp1:"
71 "       lgr             %r1,%r15\n"
72 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
73 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
74 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
75 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
76 "       lgr             %r2,%r0\n"
77 "       brasl           %r14,my_direct_func1\n"
78 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
79 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
80 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
81 "       lgr             %r1,%r0\n"
82 "       br              %r1\n"
83 "       .size           my_tramp1, .-my_tramp1\n"
84 "\n"
85 "       .type           my_tramp2, @function\n"
86 "       .globl          my_tramp2\n"
87 "   my_tramp2:"
88 "       lgr             %r1,%r15\n"
89 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
90 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
91 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
92 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
93 "       lgr             %r2,%r0\n"
94 "       brasl           %r14,my_direct_func2\n"
95 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
96 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
97 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
98 "       lgr             %r1,%r0\n"
99 "       br              %r1\n"
100 "       .size           my_tramp2, .-my_tramp2\n"
101 "       .popsection\n"
102 );
103 
104 #endif /* CONFIG_S390 */
105 
106 static unsigned long my_tramp = (unsigned long)my_tramp1;
107 static unsigned long tramps[2] = {
108 	(unsigned long)my_tramp1,
109 	(unsigned long)my_tramp2,
110 };
111 
112 static struct ftrace_ops direct;
113 
simple_thread(void * arg)114 static int simple_thread(void *arg)
115 {
116 	static int t;
117 	int ret = 0;
118 
119 	while (!kthread_should_stop()) {
120 		set_current_state(TASK_INTERRUPTIBLE);
121 		schedule_timeout(2 * HZ);
122 
123 		if (ret)
124 			continue;
125 		t ^= 1;
126 		ret = modify_ftrace_direct_multi(&direct, tramps[t]);
127 		if (!ret)
128 			my_tramp = tramps[t];
129 		WARN_ON_ONCE(ret);
130 	}
131 
132 	return 0;
133 }
134 
135 static struct task_struct *simple_tsk;
136 
ftrace_direct_multi_init(void)137 static int __init ftrace_direct_multi_init(void)
138 {
139 	int ret;
140 
141 	ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
142 	ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
143 
144 	ret = register_ftrace_direct_multi(&direct, my_tramp);
145 
146 	if (!ret)
147 		simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
148 	return ret;
149 }
150 
ftrace_direct_multi_exit(void)151 static void __exit ftrace_direct_multi_exit(void)
152 {
153 	kthread_stop(simple_tsk);
154 	unregister_ftrace_direct_multi(&direct, my_tramp);
155 	ftrace_free_filter(&direct);
156 }
157 
158 module_init(ftrace_direct_multi_init);
159 module_exit(ftrace_direct_multi_exit);
160 
161 MODULE_AUTHOR("Jiri Olsa");
162 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct_multi()");
163 MODULE_LICENSE("GPL");
164