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(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 *);
29 " .pushsection .text, \"ax\", @progbits\n"
30 " .type my_tramp1, @function\n"
33 " addi sp,sp,-3*"SZREG"\n"
34 " "REG_S" a0,0*"SZREG"(sp)\n"
35 " "REG_S" t0,1*"SZREG"(sp)\n"
36 " "REG_S" ra,2*"SZREG"(sp)\n"
38 " call my_direct_func1\n"
39 " "REG_L" a0,0*"SZREG"(sp)\n"
40 " "REG_L" t0,1*"SZREG"(sp)\n"
41 " "REG_L" ra,2*"SZREG"(sp)\n"
42 " addi sp,sp,3*"SZREG"\n"
44 " .size my_tramp1, .-my_tramp1\n"
46 " .type my_tramp2, @function\n"
49 " addi sp,sp,-3*"SZREG"\n"
50 " "REG_S" a0,0*"SZREG"(sp)\n"
51 " "REG_S" t0,1*"SZREG"(sp)\n"
52 " "REG_S" ra,2*"SZREG"(sp)\n"
54 " call my_direct_func2\n"
55 " "REG_L" a0,0*"SZREG"(sp)\n"
56 " "REG_L" t0,1*"SZREG"(sp)\n"
57 " "REG_L" ra,2*"SZREG"(sp)\n"
58 " addi sp,sp,3*"SZREG"\n"
60 " .size my_tramp2, .-my_tramp2\n"
64 #endif /* CONFIG_RISCV */
69 #include <asm/nospec-branch.h>
72 " .pushsection .text, \"ax\", @progbits\n"
73 " .type my_tramp1, @function\n"
81 " movq 8(%rbp), %rdi\n"
82 " call my_direct_func1\n"
86 " .size my_tramp1, .-my_tramp1\n"
88 " .type my_tramp2, @function\n"
96 " movq 8(%rbp), %rdi\n"
97 " call my_direct_func2\n"
101 " .size my_tramp2, .-my_tramp2\n"
105 #endif /* CONFIG_X86_64 */
110 " .pushsection .text, \"ax\", @progbits\n"
111 " .type my_tramp1, @function\n"
112 " .globl my_tramp1\n"
115 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
116 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
117 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
118 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
120 " brasl %r14,my_direct_func1\n"
121 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
122 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
123 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
126 " .size my_tramp1, .-my_tramp1\n"
128 " .type my_tramp2, @function\n"
129 " .globl my_tramp2\n"
132 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
133 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
134 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
135 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
137 " brasl %r14,my_direct_func2\n"
138 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
139 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
140 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
143 " .size my_tramp2, .-my_tramp2\n"
147 #endif /* CONFIG_S390 */
152 " .pushsection .text, \"ax\", @progbits\n"
153 " .type my_tramp1, @function\n"
154 " .globl my_tramp1\n"
156 " hint 34\n" // bti c
158 " stp x9, x30, [sp]\n"
159 " str x0, [sp, #16]\n"
161 " bl my_direct_func1\n"
162 " ldp x30, x9, [sp]\n"
163 " ldr x0, [sp, #16]\n"
166 " .size my_tramp1, .-my_tramp1\n"
168 " .type my_tramp2, @function\n"
169 " .globl my_tramp2\n"
171 " hint 34\n" // bti c
173 " stp x9, x30, [sp]\n"
174 " str x0, [sp, #16]\n"
176 " bl my_direct_func2\n"
177 " ldp x30, x9, [sp]\n"
178 " ldr x0, [sp, #16]\n"
181 " .size my_tramp2, .-my_tramp2\n"
185 #endif /* CONFIG_ARM64 */
187 #ifdef CONFIG_LOONGARCH
191 " .pushsection .text, \"ax\", @progbits\n"
192 " .type my_tramp1, @function\n"
193 " .globl my_tramp1\n"
195 " addi.d $sp, $sp, -32\n"
196 " st.d $a0, $sp, 0\n"
197 " st.d $t0, $sp, 8\n"
198 " st.d $ra, $sp, 16\n"
200 " bl my_direct_func1\n"
201 " ld.d $a0, $sp, 0\n"
202 " ld.d $t0, $sp, 8\n"
203 " ld.d $ra, $sp, 16\n"
204 " addi.d $sp, $sp, 32\n"
206 " .size my_tramp1, .-my_tramp1\n"
208 " .type my_tramp2, @function\n"
209 " .globl my_tramp2\n"
211 " addi.d $sp, $sp, -32\n"
212 " st.d $a0, $sp, 0\n"
213 " st.d $t0, $sp, 8\n"
214 " st.d $ra, $sp, 16\n"
216 " bl my_direct_func2\n"
217 " ld.d $a0, $sp, 0\n"
218 " ld.d $t0, $sp, 8\n"
219 " ld.d $ra, $sp, 16\n"
220 " addi.d $sp, $sp, 32\n"
222 " .size my_tramp2, .-my_tramp2\n"
226 #endif /* CONFIG_LOONGARCH */
229 #include <asm/ppc_asm.h>
232 #define STACK_FRAME_SIZE 48
234 #define STACK_FRAME_SIZE 24
237 #if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
238 #define PPC64_TOC_SAVE_AND_UPDATE \
240 " bcl 20, 31, 1f\n" \
242 " ld 2, (99f - 1b)(12)\n"
243 #define PPC64_TOC_RESTORE \
246 " 99: .quad .TOC.@tocbase\n"
248 #define PPC64_TOC_SAVE_AND_UPDATE ""
249 #define PPC64_TOC_RESTORE ""
253 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
254 #define PPC_FTRACE_RESTORE_LR \
255 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
257 #define PPC_FTRACE_RET \
259 #define PPC_FTRACE_RECOVER_IP \
267 #define PPC_FTRACE_RESTORE_LR \
268 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
270 #define PPC_FTRACE_RET \
273 #define PPC_FTRACE_RECOVER_IP ""
277 " .pushsection .text, \"ax\", @progbits\n"
278 " .type my_tramp1, @function\n"
279 " .globl my_tramp1\n"
281 PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
282 PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
284 PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
285 PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
286 PPC64_TOC_SAVE_AND_UPDATE
287 PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
289 PPC_FTRACE_RECOVER_IP
290 " bl my_direct_func1\n"
291 PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
293 " addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
294 PPC_FTRACE_RESTORE_LR
295 " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
296 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
298 " .size my_tramp1, .-my_tramp1\n"
300 " .type my_tramp2, @function\n"
301 " .globl my_tramp2\n"
303 PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
304 PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
306 PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
307 PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
308 PPC64_TOC_SAVE_AND_UPDATE
309 PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
311 PPC_FTRACE_RECOVER_IP
312 " bl my_direct_func2\n"
313 PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
315 " addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
316 PPC_FTRACE_RESTORE_LR
317 " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
318 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
321 " .size my_tramp2, .-my_tramp2\n"
325 #endif /* CONFIG_PPC */
327 static unsigned long my_tramp = (unsigned long)my_tramp1;
328 static unsigned long tramps[2] = {
329 (unsigned long)my_tramp1,
330 (unsigned long)my_tramp2,
333 static struct ftrace_ops direct;
335 static int simple_thread(void *arg)
340 while (!kthread_should_stop()) {
341 set_current_state(TASK_INTERRUPTIBLE);
342 schedule_timeout(2 * HZ);
347 ret = modify_ftrace_direct(&direct, tramps[t]);
349 my_tramp = tramps[t];
356 static struct task_struct *simple_tsk;
358 static int __init ftrace_direct_multi_init(void)
362 ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
363 ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
365 ret = register_ftrace_direct(&direct, my_tramp);
368 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
372 static void __exit ftrace_direct_multi_exit(void)
374 kthread_stop(simple_tsk);
375 unregister_ftrace_direct(&direct, my_tramp, true);
378 module_init(ftrace_direct_multi_init);
379 module_exit(ftrace_direct_multi_exit);
381 MODULE_AUTHOR("Jiri Olsa");
382 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
383 MODULE_LICENSE("GPL");