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 
7 void my_direct_func(struct vm_area_struct *vma,
8 			unsigned long address, unsigned int flags)
9 {
10 	trace_printk("handle mm fault vma=%p address=%lx flags=%x\n",
11 		     vma, address, flags);
12 }
13 
14 extern void my_tramp(void *);
15 
16 asm (
17 "	.pushsection    .text, \"ax\", @progbits\n"
18 "	.type		my_tramp, @function\n"
19 "   my_tramp:"
20 "	pushq %rbp\n"
21 "	movq %rsp, %rbp\n"
22 "	pushq %rdi\n"
23 "	pushq %rsi\n"
24 "	pushq %rdx\n"
25 "	call my_direct_func\n"
26 "	popq %rdx\n"
27 "	popq %rsi\n"
28 "	popq %rdi\n"
29 "	leave\n"
30 "	ret\n"
31 "	.size		my_tramp, .-my_tramp\n"
32 "	.popsection\n"
33 );
34 
35 
36 static int __init ftrace_direct_init(void)
37 {
38 	return register_ftrace_direct((unsigned long)handle_mm_fault,
39 				     (unsigned long)my_tramp);
40 }
41 
42 static void __exit ftrace_direct_exit(void)
43 {
44 	unregister_ftrace_direct((unsigned long)handle_mm_fault,
45 				 (unsigned long)my_tramp);
46 }
47 
48 module_init(ftrace_direct_init);
49 module_exit(ftrace_direct_exit);
50 
51 MODULE_AUTHOR("Steven Rostedt");
52 MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
53 MODULE_LICENSE("GPL");
54