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(void);
10 extern void my_direct_func2(void);
12 void my_direct_func1(void)
14 trace_printk("my direct func1\n");
17 void my_direct_func2(void)
19 trace_printk("my direct func2\n");
22 extern void my_tramp1(void *);
23 extern void my_tramp2(void *);
25 static unsigned long my_ip = (unsigned long)schedule;
31 " .pushsection .text, \"ax\", @progbits\n"
32 " .type my_tramp1, @function\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"
43 " .size my_tramp1, .-my_tramp1\n"
44 " .type my_tramp2, @function\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"
56 " .size my_tramp2, .-my_tramp2\n"
60 #endif /* CONFIG_RISCV */
65 #include <asm/nospec-branch.h>
68 " .pushsection .text, \"ax\", @progbits\n"
69 " .type my_tramp1, @function\n"
76 " call my_direct_func1\n"
78 " .size my_tramp1, .-my_tramp1\n"
81 " .type my_tramp2, @function\n"
88 " call my_direct_func2\n"
91 " .size my_tramp2, .-my_tramp2\n"
95 #endif /* CONFIG_X86_64 */
100 " .pushsection .text, \"ax\", @progbits\n"
101 " .type my_tramp1, @function\n"
102 " .globl my_tramp1\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"
115 " .size my_tramp1, .-my_tramp1\n"
116 " .type my_tramp2, @function\n"
117 " .globl my_tramp2\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"
130 " .size my_tramp2, .-my_tramp2\n"
134 #endif /* CONFIG_S390 */
139 " .pushsection .text, \"ax\", @progbits\n"
140 " .type my_tramp1, @function\n"
141 " .globl my_tramp1\n"
143 " hint 34\n" // bti c
145 " stp x9, x30, [sp]\n"
146 " bl my_direct_func1\n"
147 " ldp x30, x9, [sp]\n"
150 " .size my_tramp1, .-my_tramp1\n"
152 " .type my_tramp2, @function\n"
153 " .globl my_tramp2\n"
155 " hint 34\n" // bti c
157 " stp x9, x30, [sp]\n"
158 " bl my_direct_func2\n"
159 " ldp x30, x9, [sp]\n"
162 " .size my_tramp2, .-my_tramp2\n"
166 #endif /* CONFIG_ARM64 */
168 #ifdef CONFIG_LOONGARCH
171 " .pushsection .text, \"ax\", @progbits\n"
172 " .type my_tramp1, @function\n"
173 " .globl 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"
183 " .size my_tramp1, .-my_tramp1\n"
185 " .type my_tramp2, @function\n"
186 " .globl 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"
196 " .size my_tramp2, .-my_tramp2\n"
200 #endif /* CONFIG_LOONGARCH */
202 static struct ftrace_ops direct;
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,
210 static int simple_thread(void *arg)
215 while (!kthread_should_stop()) {
216 set_current_state(TASK_INTERRUPTIBLE);
217 schedule_timeout(2 * HZ);
222 ret = modify_ftrace_direct(&direct, tramps[t]);
224 my_tramp = tramps[t];
231 static struct task_struct *simple_tsk;
233 static int __init ftrace_direct_init(void)
237 ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
238 ret = register_ftrace_direct(&direct, my_tramp);
241 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
245 static void __exit ftrace_direct_exit(void)
247 kthread_stop(simple_tsk);
248 unregister_ftrace_direct(&direct, my_tramp, true);
251 module_init(ftrace_direct_init);
252 module_exit(ftrace_direct_exit);
254 MODULE_AUTHOR("Steven Rostedt");
255 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
256 MODULE_LICENSE("GPL");