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