1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kthread.h>
4 #include <linux/ftrace.h>
5 #if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
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 */
203 #include <asm/ppc_asm.h>
206 #define STACK_FRAME_SIZE 48
208 #define STACK_FRAME_SIZE 24
211 #if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
212 #define PPC64_TOC_SAVE_AND_UPDATE \
214 " bcl 20, 31, 1f\n" \
216 " ld 2, (99f - 1b)(12)\n"
217 #define PPC64_TOC_RESTORE \
220 " 99: .quad .TOC.@tocbase\n"
222 #define PPC64_TOC_SAVE_AND_UPDATE ""
223 #define PPC64_TOC_RESTORE ""
227 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
228 #define PPC_FTRACE_RESTORE_LR \
229 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
231 #define PPC_FTRACE_RET \
234 #define PPC_FTRACE_RESTORE_LR \
235 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
237 #define PPC_FTRACE_RET \
243 " .pushsection .text, \"ax\", @progbits\n"
244 " .type my_tramp1, @function\n"
245 " .globl my_tramp1\n"
247 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
248 PPC_STLU
" 1, -"__stringify(STACK_FRAME_MIN_SIZE
)"(1)\n"
250 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
251 PPC_STLU
" 1, -"__stringify(STACK_FRAME_SIZE
)"(1)\n"
252 PPC64_TOC_SAVE_AND_UPDATE
253 " bl my_direct_func1\n"
255 " addi 1, 1, "__stringify(STACK_FRAME_SIZE
)"\n"
256 PPC_FTRACE_RESTORE_LR
257 " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE
)"\n"
258 PPC_LL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
260 " .size my_tramp1, .-my_tramp1\n"
262 " .type my_tramp2, @function\n"
263 " .globl my_tramp2\n"
265 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
266 PPC_STLU
" 1, -"__stringify(STACK_FRAME_MIN_SIZE
)"(1)\n"
268 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
269 PPC_STLU
" 1, -"__stringify(STACK_FRAME_SIZE
)"(1)\n"
270 PPC64_TOC_SAVE_AND_UPDATE
271 " bl my_direct_func2\n"
273 " addi 1, 1, "__stringify(STACK_FRAME_SIZE
)"\n"
274 PPC_FTRACE_RESTORE_LR
275 " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE
)"\n"
276 PPC_LL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
279 " .size my_tramp2, .-my_tramp2\n"
283 #endif /* CONFIG_PPC */
285 static struct ftrace_ops direct
;
287 static unsigned long my_tramp
= (unsigned long)my_tramp1
;
288 static unsigned long tramps
[2] = {
289 (unsigned long)my_tramp1
,
290 (unsigned long)my_tramp2
,
293 static int simple_thread(void *arg
)
298 while (!kthread_should_stop()) {
299 set_current_state(TASK_INTERRUPTIBLE
);
300 schedule_timeout(2 * HZ
);
305 ret
= modify_ftrace_direct(&direct
, tramps
[t
]);
307 my_tramp
= tramps
[t
];
314 static struct task_struct
*simple_tsk
;
316 static int __init
ftrace_direct_init(void)
320 ftrace_set_filter_ip(&direct
, (unsigned long) my_ip
, 0, 0);
321 ret
= register_ftrace_direct(&direct
, my_tramp
);
324 simple_tsk
= kthread_run(simple_thread
, NULL
, "event-sample-fn");
328 static void __exit
ftrace_direct_exit(void)
330 kthread_stop(simple_tsk
);
331 unregister_ftrace_direct(&direct
, my_tramp
, true);
334 module_init(ftrace_direct_init
);
335 module_exit(ftrace_direct_exit
);
337 MODULE_AUTHOR("Steven Rostedt");
338 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
339 MODULE_LICENSE("GPL");