1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
4 #include <linux/sched.h> /* for wake_up_process() */
5 #include <linux/ftrace.h>
6 #if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
7 #include <asm/asm-offsets.h>
10 extern void my_direct_func(struct task_struct
*p
);
12 void my_direct_func(struct task_struct
*p
)
14 trace_printk("waking up %s-%d\n", p
->comm
, p
->pid
);
17 extern void my_tramp(void *);
23 " .pushsection .text, \"ax\", @progbits\n"
24 " .type my_tramp, @function\n"
27 " addi sp,sp,-3*"SZREG
"\n"
28 " "REG_S
" a0,0*"SZREG
"(sp)\n"
29 " "REG_S
" t0,1*"SZREG
"(sp)\n"
30 " "REG_S
" ra,2*"SZREG
"(sp)\n"
31 " call my_direct_func\n"
32 " "REG_L
" a0,0*"SZREG
"(sp)\n"
33 " "REG_L
" t0,1*"SZREG
"(sp)\n"
34 " "REG_L
" ra,2*"SZREG
"(sp)\n"
35 " addi sp,sp,3*"SZREG
"\n"
37 " .size my_tramp, .-my_tramp\n"
41 #endif /* CONFIG_RISCV */
46 #include <asm/nospec-branch.h>
49 " .pushsection .text, \"ax\", @progbits\n"
50 " .type my_tramp, @function\n"
58 " call my_direct_func\n"
62 " .size my_tramp, .-my_tramp\n"
66 #endif /* CONFIG_X86_64 */
71 " .pushsection .text, \"ax\", @progbits\n"
72 " .type my_tramp, @function\n"
76 " stmg %r0,%r5,"__stringify(__SF_GPRS
)"(%r15)\n"
77 " stg %r14,"__stringify(__SF_GPRS
+8*8)"(%r15)\n"
78 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD
)"\n"
79 " stg %r1,"__stringify(__SF_BACKCHAIN
)"(%r15)\n"
80 " brasl %r14,my_direct_func\n"
81 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD
)"\n"
82 " lmg %r0,%r5,"__stringify(__SF_GPRS
)"(%r15)\n"
83 " lg %r14,"__stringify(__SF_GPRS
+8*8)"(%r15)\n"
86 " .size my_tramp, .-my_tramp\n"
90 #endif /* CONFIG_S390 */
95 " .pushsection .text, \"ax\", @progbits\n"
96 " .type my_tramp, @function\n"
101 " stp x9, x30, [sp]\n"
102 " str x0, [sp, #16]\n"
103 " bl my_direct_func\n"
104 " ldp x30, x9, [sp]\n"
105 " ldr x0, [sp, #16]\n"
108 " .size my_tramp, .-my_tramp\n"
112 #endif /* CONFIG_ARM64 */
114 #ifdef CONFIG_LOONGARCH
117 " .pushsection .text, \"ax\", @progbits\n"
118 " .type my_tramp, @function\n"
121 " addi.d $sp, $sp, -32\n"
122 " st.d $a0, $sp, 0\n"
123 " st.d $t0, $sp, 8\n"
124 " st.d $ra, $sp, 16\n"
125 " bl my_direct_func\n"
126 " ld.d $a0, $sp, 0\n"
127 " ld.d $t0, $sp, 8\n"
128 " ld.d $ra, $sp, 16\n"
129 " addi.d $sp, $sp, 32\n"
131 " .size my_tramp, .-my_tramp\n"
135 #endif /* CONFIG_LOONGARCH */
138 #include <asm/ppc_asm.h>
141 #define STACK_FRAME_SIZE 48
143 #define STACK_FRAME_SIZE 24
146 #if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
147 #define PPC64_TOC_SAVE_AND_UPDATE \
149 " bcl 20, 31, 1f\n" \
151 " ld 2, (99f - 1b)(12)\n"
152 #define PPC64_TOC_RESTORE \
155 " 99: .quad .TOC.@tocbase\n"
157 #define PPC64_TOC_SAVE_AND_UPDATE ""
158 #define PPC64_TOC_RESTORE ""
162 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
163 #define PPC_FTRACE_RESTORE_LR \
164 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
166 #define PPC_FTRACE_RET \
169 #define PPC_FTRACE_RESTORE_LR \
170 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
172 #define PPC_FTRACE_RET \
178 " .pushsection .text, \"ax\", @progbits\n"
179 " .type my_tramp, @function\n"
182 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
183 PPC_STLU
" 1, -"__stringify(STACK_FRAME_MIN_SIZE
)"(1)\n"
185 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
186 PPC_STLU
" 1, -"__stringify(STACK_FRAME_SIZE
)"(1)\n"
187 PPC64_TOC_SAVE_AND_UPDATE
188 PPC_STL
" 3, "__stringify(STACK_FRAME_MIN_SIZE
)"(1)\n"
189 " bl my_direct_func\n"
190 PPC_LL
" 3, "__stringify(STACK_FRAME_MIN_SIZE
)"(1)\n"
192 " addi 1, 1, "__stringify(STACK_FRAME_SIZE
)"\n"
193 PPC_FTRACE_RESTORE_LR
194 " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE
)"\n"
195 PPC_LL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
198 " .size my_tramp, .-my_tramp\n"
202 #endif /* CONFIG_PPC */
204 static struct ftrace_ops direct
;
206 static int __init
ftrace_direct_init(void)
208 ftrace_set_filter_ip(&direct
, (unsigned long) wake_up_process
, 0, 0);
210 return register_ftrace_direct(&direct
, (unsigned long) my_tramp
);
213 static void __exit
ftrace_direct_exit(void)
215 unregister_ftrace_direct(&direct
, (unsigned long)my_tramp
, true);
218 module_init(ftrace_direct_init
);
219 module_exit(ftrace_direct_exit
);
221 MODULE_AUTHOR("Steven Rostedt");
222 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
223 MODULE_LICENSE("GPL");