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 #ifdef CONFIG_LOONGARCH
70 
71 #include <asm/asm.h>
72 asm (
73 "	.pushsection	.text, \"ax\", @progbits\n"
74 "	.type		my_tramp, @function\n"
75 "	.globl		my_tramp\n"
76 "   my_tramp:\n"
77 "	addi.d	$sp, $sp, -32\n"
78 "	st.d	$a0, $sp, 0\n"
79 "	st.d	$t0, $sp, 8\n"
80 "	st.d	$ra, $sp, 16\n"
81 "	move	$a0, $t0\n"
82 "	bl	my_direct_func\n"
83 "	ld.d	$a0, $sp, 0\n"
84 "	ld.d	$t0, $sp, 8\n"
85 "	ld.d	$ra, $sp, 16\n"
86 "	addi.d	$sp, $sp, 32\n"
87 "	jr	$t0\n"
88 "	.size		my_tramp, .-my_tramp\n"
89 "	.popsection\n"
90 );
91 
92 #endif /* CONFIG_LOONGARCH */
93 
94 static struct ftrace_ops direct;
95 
96 static int __init ftrace_direct_multi_init(void)
97 {
98 	ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
99 	ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
100 
101 	return register_ftrace_direct(&direct, (unsigned long) my_tramp);
102 }
103 
104 static void __exit ftrace_direct_multi_exit(void)
105 {
106 	unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true);
107 }
108 
109 module_init(ftrace_direct_multi_init);
110 module_exit(ftrace_direct_multi_exit);
111 
112 MODULE_AUTHOR("Jiri Olsa");
113 MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
114 MODULE_LICENSE("GPL");
115