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 <linux/sched/stat.h>
7 
8 extern void my_direct_func(unsigned long ip);
9 
10 void my_direct_func(unsigned long ip)
11 {
12 	trace_printk("ip %lx\n", ip);
13 }
14 
15 extern void my_tramp(void *);
16 
17 asm (
18 "	.pushsection    .text, \"ax\", @progbits\n"
19 "	.type		my_tramp, @function\n"
20 "	.globl		my_tramp\n"
21 "   my_tramp:"
22 "	pushq %rbp\n"
23 "	movq %rsp, %rbp\n"
24 "	pushq %rdi\n"
25 "	movq 8(%rbp), %rdi\n"
26 "	call my_direct_func\n"
27 "	popq %rdi\n"
28 "	leave\n"
29 "	ret\n"
30 "	.size		my_tramp, .-my_tramp\n"
31 "	.popsection\n"
32 );
33 
34 static struct ftrace_ops direct;
35 
36 static int __init ftrace_direct_multi_init(void)
37 {
38 	ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
39 	ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
40 
41 	return register_ftrace_direct_multi(&direct, (unsigned long) my_tramp);
42 }
43 
44 static void __exit ftrace_direct_multi_exit(void)
45 {
46 	unregister_ftrace_direct_multi(&direct, (unsigned long) my_tramp);
47 }
48 
49 module_init(ftrace_direct_multi_init);
50 module_exit(ftrace_direct_multi_exit);
51 
52 MODULE_AUTHOR("Jiri Olsa");
53 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
54 MODULE_LICENSE("GPL");
55