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>
7 #include <asm/asm-offsets.h>
10 extern void my_direct_func(struct vm_area_struct *vma, unsigned long address,
11 unsigned int flags, struct pt_regs *regs);
13 void my_direct_func(struct vm_area_struct *vma, unsigned long address,
14 unsigned int flags, struct pt_regs *regs)
16 trace_printk("handle mm fault vma=%p address=%lx flags=%x regs=%p\n",
17 vma, address, flags, regs);
20 extern void my_tramp(void *);
26 " .pushsection .text, \"ax\", @progbits\n"
27 " .type my_tramp, @function\n"
30 " addi sp,sp,-5*"SZREG"\n"
31 " "REG_S" a0,0*"SZREG"(sp)\n"
32 " "REG_S" a1,1*"SZREG"(sp)\n"
33 " "REG_S" a2,2*"SZREG"(sp)\n"
34 " "REG_S" t0,3*"SZREG"(sp)\n"
35 " "REG_S" ra,4*"SZREG"(sp)\n"
36 " call my_direct_func\n"
37 " "REG_L" a0,0*"SZREG"(sp)\n"
38 " "REG_L" a1,1*"SZREG"(sp)\n"
39 " "REG_L" a2,2*"SZREG"(sp)\n"
40 " "REG_L" t0,3*"SZREG"(sp)\n"
41 " "REG_L" ra,4*"SZREG"(sp)\n"
42 " addi sp,sp,5*"SZREG"\n"
44 " .size my_tramp, .-my_tramp\n"
48 #endif /* CONFIG_RISCV */
53 #include <asm/nospec-branch.h>
56 " .pushsection .text, \"ax\", @progbits\n"
57 " .type my_tramp, @function\n"
68 " call my_direct_func\n"
75 " .size my_tramp, .-my_tramp\n"
79 #endif /* CONFIG_X86_64 */
84 " .pushsection .text, \"ax\", @progbits\n"
85 " .type my_tramp, @function\n"
89 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
90 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
91 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
92 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
93 " brasl %r14,my_direct_func\n"
94 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
95 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
96 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
99 " .size my_tramp, .-my_tramp\n"
103 #endif /* CONFIG_S390 */
108 " .pushsection .text, \"ax\", @progbits\n"
109 " .type my_tramp, @function\n"
112 " hint 34\n" // bti c
114 " stp x9, x30, [sp]\n"
115 " stp x0, x1, [sp, #16]\n"
116 " stp x2, x3, [sp, #32]\n"
117 " bl my_direct_func\n"
118 " ldp x30, x9, [sp]\n"
119 " ldp x0, x1, [sp, #16]\n"
120 " ldp x2, x3, [sp, #32]\n"
123 " .size my_tramp, .-my_tramp\n"
127 #endif /* CONFIG_ARM64 */
129 #ifdef CONFIG_LOONGARCH
132 " .pushsection .text, \"ax\", @progbits\n"
133 " .type my_tramp, @function\n"
136 " addi.d $sp, $sp, -48\n"
137 " st.d $a0, $sp, 0\n"
138 " st.d $a1, $sp, 8\n"
139 " st.d $a2, $sp, 16\n"
140 " st.d $t0, $sp, 24\n"
141 " st.d $ra, $sp, 32\n"
142 " bl my_direct_func\n"
143 " ld.d $a0, $sp, 0\n"
144 " ld.d $a1, $sp, 8\n"
145 " ld.d $a2, $sp, 16\n"
146 " ld.d $t0, $sp, 24\n"
147 " ld.d $ra, $sp, 32\n"
148 " addi.d $sp, $sp, 48\n"
150 " .size my_tramp, .-my_tramp\n"
154 #endif /* CONFIG_LOONGARCH */
156 static struct ftrace_ops direct;
158 static int __init ftrace_direct_init(void)
160 ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
162 return register_ftrace_direct(&direct, (unsigned long) my_tramp);
165 static void __exit ftrace_direct_exit(void)
167 unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
170 module_init(ftrace_direct_init);
171 module_exit(ftrace_direct_exit);
173 MODULE_AUTHOR("Steven Rostedt");
174 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
175 MODULE_LICENSE("GPL");