GNU Linux-libre 5.19-rc6-gnu
[releases.git] / samples / ftrace / ftrace-direct-multi-modify.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kthread.h>
4 #include <linux/ftrace.h>
5 #include <asm/asm-offsets.h>
6
7 extern void my_direct_func1(unsigned long ip);
8 extern void my_direct_func2(unsigned long ip);
9
10 void my_direct_func1(unsigned long ip)
11 {
12         trace_printk("my direct func1 ip %lx\n", ip);
13 }
14
15 void my_direct_func2(unsigned long ip)
16 {
17         trace_printk("my direct func2 ip %lx\n", ip);
18 }
19
20 extern void my_tramp1(void *);
21 extern void my_tramp2(void *);
22
23 #ifdef CONFIG_X86_64
24
25 #include <asm/ibt.h>
26
27 asm (
28 "       .pushsection    .text, \"ax\", @progbits\n"
29 "       .type           my_tramp1, @function\n"
30 "       .globl          my_tramp1\n"
31 "   my_tramp1:"
32         ASM_ENDBR
33 "       pushq %rbp\n"
34 "       movq %rsp, %rbp\n"
35 "       pushq %rdi\n"
36 "       movq 8(%rbp), %rdi\n"
37 "       call my_direct_func1\n"
38 "       popq %rdi\n"
39 "       leave\n"
40         ASM_RET
41 "       .size           my_tramp1, .-my_tramp1\n"
42
43 "       .type           my_tramp2, @function\n"
44 "       .globl          my_tramp2\n"
45 "   my_tramp2:"
46         ASM_ENDBR
47 "       pushq %rbp\n"
48 "       movq %rsp, %rbp\n"
49 "       pushq %rdi\n"
50 "       movq 8(%rbp), %rdi\n"
51 "       call my_direct_func2\n"
52 "       popq %rdi\n"
53 "       leave\n"
54         ASM_RET
55 "       .size           my_tramp2, .-my_tramp2\n"
56 "       .popsection\n"
57 );
58
59 #endif /* CONFIG_X86_64 */
60
61 #ifdef CONFIG_S390
62
63 asm (
64 "       .pushsection    .text, \"ax\", @progbits\n"
65 "       .type           my_tramp1, @function\n"
66 "       .globl          my_tramp1\n"
67 "   my_tramp1:"
68 "       lgr             %r1,%r15\n"
69 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
70 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
71 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
72 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
73 "       lgr             %r2,%r0\n"
74 "       brasl           %r14,my_direct_func1\n"
75 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
76 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
77 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
78 "       lgr             %r1,%r0\n"
79 "       br              %r1\n"
80 "       .size           my_tramp1, .-my_tramp1\n"
81 "\n"
82 "       .type           my_tramp2, @function\n"
83 "       .globl          my_tramp2\n"
84 "   my_tramp2:"
85 "       lgr             %r1,%r15\n"
86 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
87 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
88 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
89 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
90 "       lgr             %r2,%r0\n"
91 "       brasl           %r14,my_direct_func2\n"
92 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
93 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
94 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
95 "       lgr             %r1,%r0\n"
96 "       br              %r1\n"
97 "       .size           my_tramp2, .-my_tramp2\n"
98 "       .popsection\n"
99 );
100
101 #endif /* CONFIG_S390 */
102
103 static unsigned long my_tramp = (unsigned long)my_tramp1;
104 static unsigned long tramps[2] = {
105         (unsigned long)my_tramp1,
106         (unsigned long)my_tramp2,
107 };
108
109 static struct ftrace_ops direct;
110
111 static int simple_thread(void *arg)
112 {
113         static int t;
114         int ret = 0;
115
116         while (!kthread_should_stop()) {
117                 set_current_state(TASK_INTERRUPTIBLE);
118                 schedule_timeout(2 * HZ);
119
120                 if (ret)
121                         continue;
122                 t ^= 1;
123                 ret = modify_ftrace_direct_multi(&direct, tramps[t]);
124                 if (!ret)
125                         my_tramp = tramps[t];
126                 WARN_ON_ONCE(ret);
127         }
128
129         return 0;
130 }
131
132 static struct task_struct *simple_tsk;
133
134 static int __init ftrace_direct_multi_init(void)
135 {
136         int ret;
137
138         ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
139         ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
140
141         ret = register_ftrace_direct_multi(&direct, my_tramp);
142
143         if (!ret)
144                 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
145         return ret;
146 }
147
148 static void __exit ftrace_direct_multi_exit(void)
149 {
150         kthread_stop(simple_tsk);
151         unregister_ftrace_direct_multi(&direct, my_tramp);
152 }
153
154 module_init(ftrace_direct_multi_init);
155 module_exit(ftrace_direct_multi_exit);
156
157 MODULE_AUTHOR("Jiri Olsa");
158 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct_multi()");
159 MODULE_LICENSE("GPL");