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 *);
25 #include <asm/nospec-branch.h>
28 " .pushsection .text, \"ax\", @progbits\n"
29 " .type my_tramp, @function\n"
40 " call my_direct_func\n"
47 " .size my_tramp, .-my_tramp\n"
51 #endif /* CONFIG_X86_64 */
56 " .pushsection .text, \"ax\", @progbits\n"
57 " .type my_tramp, @function\n"
61 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
62 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
63 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
64 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
65 " brasl %r14,my_direct_func\n"
66 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
67 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
68 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
71 " .size my_tramp, .-my_tramp\n"
75 #endif /* CONFIG_S390 */
80 " .pushsection .text, \"ax\", @progbits\n"
81 " .type my_tramp, @function\n"
86 " stp x9, x30, [sp]\n"
87 " stp x0, x1, [sp, #16]\n"
88 " stp x2, x3, [sp, #32]\n"
89 " bl my_direct_func\n"
90 " ldp x30, x9, [sp]\n"
91 " ldp x0, x1, [sp, #16]\n"
92 " ldp x2, x3, [sp, #32]\n"
95 " .size my_tramp, .-my_tramp\n"
99 #endif /* CONFIG_ARM64 */
101 #ifdef CONFIG_LOONGARCH
104 " .pushsection .text, \"ax\", @progbits\n"
105 " .type my_tramp, @function\n"
108 " addi.d $sp, $sp, -48\n"
109 " st.d $a0, $sp, 0\n"
110 " st.d $a1, $sp, 8\n"
111 " st.d $a2, $sp, 16\n"
112 " st.d $t0, $sp, 24\n"
113 " st.d $ra, $sp, 32\n"
114 " bl my_direct_func\n"
115 " ld.d $a0, $sp, 0\n"
116 " ld.d $a1, $sp, 8\n"
117 " ld.d $a2, $sp, 16\n"
118 " ld.d $t0, $sp, 24\n"
119 " ld.d $ra, $sp, 32\n"
120 " addi.d $sp, $sp, 48\n"
122 " .size my_tramp, .-my_tramp\n"
126 #endif /* CONFIG_LOONGARCH */
128 static struct ftrace_ops direct;
130 static int __init ftrace_direct_init(void)
132 ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
134 return register_ftrace_direct(&direct, (unsigned long) my_tramp);
137 static void __exit ftrace_direct_exit(void)
139 unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
142 module_init(ftrace_direct_init);
143 module_exit(ftrace_direct_exit);
145 MODULE_AUTHOR("Steven Rostedt");
146 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
147 MODULE_LICENSE("GPL");