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 #if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
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 */
157 #include <asm/ppc_asm.h>
160 #define STACK_FRAME_SIZE 64
161 #define STACK_FRAME_ARG1 32
162 #define STACK_FRAME_ARG2 40
163 #define STACK_FRAME_ARG3 48
164 #define STACK_FRAME_ARG4 56
166 #define STACK_FRAME_SIZE 32
167 #define STACK_FRAME_ARG1 16
168 #define STACK_FRAME_ARG2 20
169 #define STACK_FRAME_ARG3 24
170 #define STACK_FRAME_ARG4 28
173 #if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
174 #define PPC64_TOC_SAVE_AND_UPDATE \
176 " bcl 20, 31, 1f\n" \
178 " ld 2, (99f - 1b)(12)\n"
179 #define PPC64_TOC_RESTORE \
182 " 99: .quad .TOC.@tocbase\n"
184 #define PPC64_TOC_SAVE_AND_UPDATE ""
185 #define PPC64_TOC_RESTORE ""
189 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
190 #define PPC_FTRACE_RESTORE_LR \
191 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
193 #define PPC_FTRACE_RET \
196 #define PPC_FTRACE_RESTORE_LR \
197 PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
199 #define PPC_FTRACE_RET \
205 " .pushsection .text, \"ax\", @progbits\n"
206 " .type my_tramp, @function\n"
209 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
210 PPC_STLU
" 1, -"__stringify(STACK_FRAME_MIN_SIZE
)"(1)\n"
212 PPC_STL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
213 PPC_STLU
" 1, -"__stringify(STACK_FRAME_SIZE
)"(1)\n"
214 PPC64_TOC_SAVE_AND_UPDATE
215 PPC_STL
" 3, "__stringify(STACK_FRAME_ARG1
)"(1)\n"
216 PPC_STL
" 4, "__stringify(STACK_FRAME_ARG2
)"(1)\n"
217 PPC_STL
" 5, "__stringify(STACK_FRAME_ARG3
)"(1)\n"
218 PPC_STL
" 6, "__stringify(STACK_FRAME_ARG4
)"(1)\n"
219 " bl my_direct_func\n"
220 PPC_LL
" 6, "__stringify(STACK_FRAME_ARG4
)"(1)\n"
221 PPC_LL
" 5, "__stringify(STACK_FRAME_ARG3
)"(1)\n"
222 PPC_LL
" 4, "__stringify(STACK_FRAME_ARG2
)"(1)\n"
223 PPC_LL
" 3, "__stringify(STACK_FRAME_ARG1
)"(1)\n"
225 " addi 1, 1, "__stringify(STACK_FRAME_SIZE
)"\n"
226 PPC_FTRACE_RESTORE_LR
227 " addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE
)"\n"
228 PPC_LL
" 0, "__stringify(PPC_LR_STKOFF
)"(1)\n"
231 " .size my_tramp, .-my_tramp\n"
235 #endif /* CONFIG_PPC */
237 static struct ftrace_ops direct
;
239 static int __init
ftrace_direct_init(void)
241 ftrace_set_filter_ip(&direct
, (unsigned long) handle_mm_fault
, 0, 0);
243 return register_ftrace_direct(&direct
, (unsigned long) my_tramp
);
246 static void __exit
ftrace_direct_exit(void)
248 unregister_ftrace_direct(&direct
, (unsigned long)my_tramp
, true);
251 module_init(ftrace_direct_init
);
252 module_exit(ftrace_direct_exit
);
254 MODULE_AUTHOR("Steven Rostedt");
255 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
256 MODULE_LICENSE("GPL");