1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 
4 #include <linux/mm.h> /* for handle_mm_fault() */
5 #include <linux/ftrace.h>
6 #include <asm/asm-offsets.h>
7 
8 extern void my_direct_func(struct vm_area_struct *vma,
9 			   unsigned long address, unsigned int flags);
10 
11 void my_direct_func(struct vm_area_struct *vma,
12 			unsigned long address, unsigned int flags)
13 {
14 	trace_printk("handle mm fault vma=%p address=%lx flags=%x\n",
15 		     vma, address, flags);
16 }
17 
18 extern void my_tramp(void *);
19 
20 #ifdef CONFIG_X86_64
21 
22 #include <asm/ibt.h>
23 
24 asm (
25 "	.pushsection    .text, \"ax\", @progbits\n"
26 "	.type		my_tramp, @function\n"
27 "	.globl		my_tramp\n"
28 "   my_tramp:"
29 	ASM_ENDBR
30 "	pushq %rbp\n"
31 "	movq %rsp, %rbp\n"
32 "	pushq %rdi\n"
33 "	pushq %rsi\n"
34 "	pushq %rdx\n"
35 "	call my_direct_func\n"
36 "	popq %rdx\n"
37 "	popq %rsi\n"
38 "	popq %rdi\n"
39 "	leave\n"
40 	ASM_RET
41 "	.size		my_tramp, .-my_tramp\n"
42 "	.popsection\n"
43 );
44 
45 #endif /* CONFIG_X86_64 */
46 
47 #ifdef CONFIG_S390
48 
49 asm (
50 "	.pushsection	.text, \"ax\", @progbits\n"
51 "	.type		my_tramp, @function\n"
52 "	.globl		my_tramp\n"
53 "   my_tramp:"
54 "	lgr		%r1,%r15\n"
55 "	stmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
56 "	stg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
57 "	aghi		%r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
58 "	stg		%r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
59 "	brasl		%r14,my_direct_func\n"
60 "	aghi		%r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
61 "	lmg		%r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
62 "	lg		%r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
63 "	lgr		%r1,%r0\n"
64 "	br		%r1\n"
65 "	.size		my_tramp, .-my_tramp\n"
66 "	.popsection\n"
67 );
68 
69 #endif /* CONFIG_S390 */
70 
71 static int __init ftrace_direct_init(void)
72 {
73 	return register_ftrace_direct((unsigned long)handle_mm_fault,
74 				     (unsigned long)my_tramp);
75 }
76 
77 static void __exit ftrace_direct_exit(void)
78 {
79 	unregister_ftrace_direct((unsigned long)handle_mm_fault,
80 				 (unsigned long)my_tramp);
81 }
82 
83 module_init(ftrace_direct_init);
84 module_exit(ftrace_direct_exit);
85 
86 MODULE_AUTHOR("Steven Rostedt");
87 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
88 MODULE_LICENSE("GPL");
89