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