]> Git Repo - J-linux.git/blob - samples/ftrace/ftrace-direct-multi-modify.c
Merge tag 'amd-drm-next-6.5-2023-06-09' of https://gitlab.freedesktop.org/agd5f/linux...
[J-linux.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 #include <asm/nospec-branch.h>
27
28 asm (
29 "       .pushsection    .text, \"ax\", @progbits\n"
30 "       .type           my_tramp1, @function\n"
31 "       .globl          my_tramp1\n"
32 "   my_tramp1:"
33         ASM_ENDBR
34 "       pushq %rbp\n"
35 "       movq %rsp, %rbp\n"
36         CALL_DEPTH_ACCOUNT
37 "       pushq %rdi\n"
38 "       movq 8(%rbp), %rdi\n"
39 "       call my_direct_func1\n"
40 "       popq %rdi\n"
41 "       leave\n"
42         ASM_RET
43 "       .size           my_tramp1, .-my_tramp1\n"
44
45 "       .type           my_tramp2, @function\n"
46 "       .globl          my_tramp2\n"
47 "   my_tramp2:"
48         ASM_ENDBR
49 "       pushq %rbp\n"
50 "       movq %rsp, %rbp\n"
51         CALL_DEPTH_ACCOUNT
52 "       pushq %rdi\n"
53 "       movq 8(%rbp), %rdi\n"
54 "       call my_direct_func2\n"
55 "       popq %rdi\n"
56 "       leave\n"
57         ASM_RET
58 "       .size           my_tramp2, .-my_tramp2\n"
59 "       .popsection\n"
60 );
61
62 #endif /* CONFIG_X86_64 */
63
64 #ifdef CONFIG_S390
65
66 asm (
67 "       .pushsection    .text, \"ax\", @progbits\n"
68 "       .type           my_tramp1, @function\n"
69 "       .globl          my_tramp1\n"
70 "   my_tramp1:"
71 "       lgr             %r1,%r15\n"
72 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
73 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
74 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
75 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
76 "       lgr             %r2,%r0\n"
77 "       brasl           %r14,my_direct_func1\n"
78 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
79 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
80 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
81 "       lgr             %r1,%r0\n"
82 "       br              %r1\n"
83 "       .size           my_tramp1, .-my_tramp1\n"
84 "\n"
85 "       .type           my_tramp2, @function\n"
86 "       .globl          my_tramp2\n"
87 "   my_tramp2:"
88 "       lgr             %r1,%r15\n"
89 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
90 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
91 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
92 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
93 "       lgr             %r2,%r0\n"
94 "       brasl           %r14,my_direct_func2\n"
95 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
96 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
97 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
98 "       lgr             %r1,%r0\n"
99 "       br              %r1\n"
100 "       .size           my_tramp2, .-my_tramp2\n"
101 "       .popsection\n"
102 );
103
104 #endif /* CONFIG_S390 */
105
106 #ifdef CONFIG_LOONGARCH
107 #include <asm/asm.h>
108
109 asm (
110 "       .pushsection    .text, \"ax\", @progbits\n"
111 "       .type           my_tramp1, @function\n"
112 "       .globl          my_tramp1\n"
113 "   my_tramp1:\n"
114 "       addi.d  $sp, $sp, -32\n"
115 "       st.d    $a0, $sp, 0\n"
116 "       st.d    $t0, $sp, 8\n"
117 "       st.d    $ra, $sp, 16\n"
118 "       move    $a0, $t0\n"
119 "       bl      my_direct_func1\n"
120 "       ld.d    $a0, $sp, 0\n"
121 "       ld.d    $t0, $sp, 8\n"
122 "       ld.d    $ra, $sp, 16\n"
123 "       addi.d  $sp, $sp, 32\n"
124 "       jr      $t0\n"
125 "       .size           my_tramp1, .-my_tramp1\n"
126
127 "       .type           my_tramp2, @function\n"
128 "       .globl          my_tramp2\n"
129 "   my_tramp2:\n"
130 "       addi.d  $sp, $sp, -32\n"
131 "       st.d    $a0, $sp, 0\n"
132 "       st.d    $t0, $sp, 8\n"
133 "       st.d    $ra, $sp, 16\n"
134 "       move    $a0, $t0\n"
135 "       bl      my_direct_func2\n"
136 "       ld.d    $a0, $sp, 0\n"
137 "       ld.d    $t0, $sp, 8\n"
138 "       ld.d    $ra, $sp, 16\n"
139 "       addi.d  $sp, $sp, 32\n"
140 "       jr      $t0\n"
141 "       .size           my_tramp2, .-my_tramp2\n"
142 "       .popsection\n"
143 );
144
145 #endif /* CONFIG_LOONGARCH */
146
147 static unsigned long my_tramp = (unsigned long)my_tramp1;
148 static unsigned long tramps[2] = {
149         (unsigned long)my_tramp1,
150         (unsigned long)my_tramp2,
151 };
152
153 static struct ftrace_ops direct;
154
155 static int simple_thread(void *arg)
156 {
157         static int t;
158         int ret = 0;
159
160         while (!kthread_should_stop()) {
161                 set_current_state(TASK_INTERRUPTIBLE);
162                 schedule_timeout(2 * HZ);
163
164                 if (ret)
165                         continue;
166                 t ^= 1;
167                 ret = modify_ftrace_direct(&direct, tramps[t]);
168                 if (!ret)
169                         my_tramp = tramps[t];
170                 WARN_ON_ONCE(ret);
171         }
172
173         return 0;
174 }
175
176 static struct task_struct *simple_tsk;
177
178 static int __init ftrace_direct_multi_init(void)
179 {
180         int ret;
181
182         ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
183         ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
184
185         ret = register_ftrace_direct(&direct, my_tramp);
186
187         if (!ret)
188                 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
189         return ret;
190 }
191
192 static void __exit ftrace_direct_multi_exit(void)
193 {
194         kthread_stop(simple_tsk);
195         unregister_ftrace_direct(&direct, my_tramp, true);
196 }
197
198 module_init(ftrace_direct_multi_init);
199 module_exit(ftrace_direct_multi_exit);
200
201 MODULE_AUTHOR("Jiri Olsa");
202 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
203 MODULE_LICENSE("GPL");
This page took 0.041745 seconds and 4 git commands to generate.