1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
4 #include <linux/mm.h> /* for handle_mm_fault() */
5 #include <linux/ftrace.h>
6 #include <linux/sched/stat.h>
8 #include <asm/asm-offsets.h>
11 extern void my_direct_func(unsigned long ip);
13 void my_direct_func(unsigned long ip)
15 trace_printk("ip %lx\n", ip);
18 extern void my_tramp(void *);
24 " .pushsection .text, \"ax\", @progbits\n"
25 " .type my_tramp, @function\n"
28 " addi sp,sp,-3*"SZREG"\n"
29 " "REG_S" a0,0*"SZREG"(sp)\n"
30 " "REG_S" t0,1*"SZREG"(sp)\n"
31 " "REG_S" ra,2*"SZREG"(sp)\n"
33 " call my_direct_func\n"
34 " "REG_L" a0,0*"SZREG"(sp)\n"
35 " "REG_L" t0,1*"SZREG"(sp)\n"
36 " "REG_L" ra,2*"SZREG"(sp)\n"
37 " addi sp,sp,3*"SZREG"\n"
39 " .size my_tramp, .-my_tramp\n"
43 #endif /* CONFIG_RISCV */
48 #include <asm/nospec-branch.h>
51 " .pushsection .text, \"ax\", @progbits\n"
52 " .type my_tramp, @function\n"
60 " movq 8(%rbp), %rdi\n"
61 " call my_direct_func\n"
65 " .size my_tramp, .-my_tramp\n"
69 #endif /* CONFIG_X86_64 */
74 " .pushsection .text, \"ax\", @progbits\n"
75 " .type my_tramp, @function\n"
79 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
80 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
81 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
82 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
84 " brasl %r14,my_direct_func\n"
85 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
86 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
87 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
90 " .size my_tramp, .-my_tramp\n"
94 #endif /* CONFIG_S390 */
99 " .pushsection .text, \"ax\", @progbits\n"
100 " .type my_tramp, @function\n"
103 " hint 34\n" // bti c
105 " stp x9, x30, [sp]\n"
106 " str x0, [sp, #16]\n"
108 " bl my_direct_func\n"
109 " ldp x30, x9, [sp]\n"
110 " ldr x0, [sp, #16]\n"
113 " .size my_tramp, .-my_tramp\n"
117 #endif /* CONFIG_ARM64 */
119 #ifdef CONFIG_LOONGARCH
123 " .pushsection .text, \"ax\", @progbits\n"
124 " .type my_tramp, @function\n"
127 " addi.d $sp, $sp, -32\n"
128 " st.d $a0, $sp, 0\n"
129 " st.d $t0, $sp, 8\n"
130 " st.d $ra, $sp, 16\n"
132 " bl my_direct_func\n"
133 " ld.d $a0, $sp, 0\n"
134 " ld.d $t0, $sp, 8\n"
135 " ld.d $ra, $sp, 16\n"
136 " addi.d $sp, $sp, 32\n"
138 " .size my_tramp, .-my_tramp\n"
142 #endif /* CONFIG_LOONGARCH */
144 static struct ftrace_ops direct;
146 static int __init ftrace_direct_multi_init(void)
148 ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
149 ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
151 return register_ftrace_direct(&direct, (unsigned long) my_tramp);
154 static void __exit ftrace_direct_multi_exit(void)
156 unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true);
159 module_init(ftrace_direct_multi_init);
160 module_exit(ftrace_direct_multi_exit);
162 MODULE_AUTHOR("Jiri Olsa");
163 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
164 MODULE_LICENSE("GPL");