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