1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kthread.h>
4 #include <linux/ftrace.h>
6 #include <asm/asm-offsets.h>
9 extern void my_direct_func1(unsigned long ip);
10 extern void my_direct_func2(unsigned long ip);
12 void my_direct_func1(unsigned long ip)
14 trace_printk("my direct func1 ip %lx\n", ip);
17 void my_direct_func2(unsigned long ip)
19 trace_printk("my direct func2 ip %lx\n", ip);
22 extern void my_tramp1(void *);
23 extern void my_tramp2(void *);
28 #include <asm/nospec-branch.h>
31 " .pushsection .text, \"ax\", @progbits\n"
32 " .type my_tramp1, @function\n"
40 " movq 8(%rbp), %rdi\n"
41 " call my_direct_func1\n"
45 " .size my_tramp1, .-my_tramp1\n"
47 " .type my_tramp2, @function\n"
55 " movq 8(%rbp), %rdi\n"
56 " call my_direct_func2\n"
60 " .size my_tramp2, .-my_tramp2\n"
64 #endif /* CONFIG_X86_64 */
69 " .pushsection .text, \"ax\", @progbits\n"
70 " .type my_tramp1, @function\n"
74 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
75 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
76 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
77 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
79 " brasl %r14,my_direct_func1\n"
80 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
81 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
82 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
85 " .size my_tramp1, .-my_tramp1\n"
87 " .type my_tramp2, @function\n"
91 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
92 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
93 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
94 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
96 " brasl %r14,my_direct_func2\n"
97 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
98 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
99 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
102 " .size my_tramp2, .-my_tramp2\n"
106 #endif /* CONFIG_S390 */
111 " .pushsection .text, \"ax\", @progbits\n"
112 " .type my_tramp1, @function\n"
113 " .globl my_tramp1\n"
115 " hint 34\n" // bti c
117 " stp x9, x30, [sp]\n"
118 " str x0, [sp, #16]\n"
120 " bl my_direct_func1\n"
121 " ldp x30, x9, [sp]\n"
122 " ldr x0, [sp, #16]\n"
125 " .size my_tramp1, .-my_tramp1\n"
127 " .type my_tramp2, @function\n"
128 " .globl my_tramp2\n"
130 " hint 34\n" // bti c
132 " stp x9, x30, [sp]\n"
133 " str x0, [sp, #16]\n"
135 " bl my_direct_func2\n"
136 " ldp x30, x9, [sp]\n"
137 " ldr x0, [sp, #16]\n"
140 " .size my_tramp2, .-my_tramp2\n"
144 #endif /* CONFIG_ARM64 */
146 #ifdef CONFIG_LOONGARCH
150 " .pushsection .text, \"ax\", @progbits\n"
151 " .type my_tramp1, @function\n"
152 " .globl my_tramp1\n"
154 " addi.d $sp, $sp, -32\n"
155 " st.d $a0, $sp, 0\n"
156 " st.d $t0, $sp, 8\n"
157 " st.d $ra, $sp, 16\n"
159 " bl my_direct_func1\n"
160 " ld.d $a0, $sp, 0\n"
161 " ld.d $t0, $sp, 8\n"
162 " ld.d $ra, $sp, 16\n"
163 " addi.d $sp, $sp, 32\n"
165 " .size my_tramp1, .-my_tramp1\n"
167 " .type my_tramp2, @function\n"
168 " .globl my_tramp2\n"
170 " addi.d $sp, $sp, -32\n"
171 " st.d $a0, $sp, 0\n"
172 " st.d $t0, $sp, 8\n"
173 " st.d $ra, $sp, 16\n"
175 " bl my_direct_func2\n"
176 " ld.d $a0, $sp, 0\n"
177 " ld.d $t0, $sp, 8\n"
178 " ld.d $ra, $sp, 16\n"
179 " addi.d $sp, $sp, 32\n"
181 " .size my_tramp2, .-my_tramp2\n"
185 #endif /* CONFIG_LOONGARCH */
187 static unsigned long my_tramp = (unsigned long)my_tramp1;
188 static unsigned long tramps[2] = {
189 (unsigned long)my_tramp1,
190 (unsigned long)my_tramp2,
193 static struct ftrace_ops direct;
195 static int simple_thread(void *arg)
200 while (!kthread_should_stop()) {
201 set_current_state(TASK_INTERRUPTIBLE);
202 schedule_timeout(2 * HZ);
207 ret = modify_ftrace_direct(&direct, tramps[t]);
209 my_tramp = tramps[t];
216 static struct task_struct *simple_tsk;
218 static int __init ftrace_direct_multi_init(void)
222 ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
223 ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
225 ret = register_ftrace_direct(&direct, my_tramp);
228 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
232 static void __exit ftrace_direct_multi_exit(void)
234 kthread_stop(simple_tsk);
235 unregister_ftrace_direct(&direct, my_tramp, true);
238 module_init(ftrace_direct_multi_init);
239 module_exit(ftrace_direct_multi_exit);
241 MODULE_AUTHOR("Jiri Olsa");
242 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
243 MODULE_LICENSE("GPL");