Merge tag 'v6.6-p2' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[platform/kernel/linux-rpi.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 #ifndef CONFIG_ARM64
6 #include <asm/asm-offsets.h>
7 #endif
8
9 extern void my_direct_func1(unsigned long ip);
10 extern void my_direct_func2(unsigned long ip);
11
12 void my_direct_func1(unsigned long ip)
13 {
14         trace_printk("my direct func1 ip %lx\n", ip);
15 }
16
17 void my_direct_func2(unsigned long ip)
18 {
19         trace_printk("my direct func2 ip %lx\n", ip);
20 }
21
22 extern void my_tramp1(void *);
23 extern void my_tramp2(void *);
24
25 #ifdef CONFIG_X86_64
26
27 #include <asm/ibt.h>
28 #include <asm/nospec-branch.h>
29
30 asm (
31 "       .pushsection    .text, \"ax\", @progbits\n"
32 "       .type           my_tramp1, @function\n"
33 "       .globl          my_tramp1\n"
34 "   my_tramp1:"
35         ASM_ENDBR
36 "       pushq %rbp\n"
37 "       movq %rsp, %rbp\n"
38         CALL_DEPTH_ACCOUNT
39 "       pushq %rdi\n"
40 "       movq 8(%rbp), %rdi\n"
41 "       call my_direct_func1\n"
42 "       popq %rdi\n"
43 "       leave\n"
44         ASM_RET
45 "       .size           my_tramp1, .-my_tramp1\n"
46
47 "       .type           my_tramp2, @function\n"
48 "       .globl          my_tramp2\n"
49 "   my_tramp2:"
50         ASM_ENDBR
51 "       pushq %rbp\n"
52 "       movq %rsp, %rbp\n"
53         CALL_DEPTH_ACCOUNT
54 "       pushq %rdi\n"
55 "       movq 8(%rbp), %rdi\n"
56 "       call my_direct_func2\n"
57 "       popq %rdi\n"
58 "       leave\n"
59         ASM_RET
60 "       .size           my_tramp2, .-my_tramp2\n"
61 "       .popsection\n"
62 );
63
64 #endif /* CONFIG_X86_64 */
65
66 #ifdef CONFIG_S390
67
68 asm (
69 "       .pushsection    .text, \"ax\", @progbits\n"
70 "       .type           my_tramp1, @function\n"
71 "       .globl          my_tramp1\n"
72 "   my_tramp1:"
73 "       lgr             %r1,%r15\n"
74 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
75 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
76 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
77 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
78 "       lgr             %r2,%r0\n"
79 "       brasl           %r14,my_direct_func1\n"
80 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
81 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
82 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
83 "       lgr             %r1,%r0\n"
84 "       br              %r1\n"
85 "       .size           my_tramp1, .-my_tramp1\n"
86 "\n"
87 "       .type           my_tramp2, @function\n"
88 "       .globl          my_tramp2\n"
89 "   my_tramp2:"
90 "       lgr             %r1,%r15\n"
91 "       stmg            %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
92 "       stg             %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
93 "       aghi            %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
94 "       stg             %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
95 "       lgr             %r2,%r0\n"
96 "       brasl           %r14,my_direct_func2\n"
97 "       aghi            %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
98 "       lmg             %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
99 "       lg              %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
100 "       lgr             %r1,%r0\n"
101 "       br              %r1\n"
102 "       .size           my_tramp2, .-my_tramp2\n"
103 "       .popsection\n"
104 );
105
106 #endif /* CONFIG_S390 */
107
108 #ifdef CONFIG_ARM64
109
110 asm (
111 "       .pushsection    .text, \"ax\", @progbits\n"
112 "       .type           my_tramp1, @function\n"
113 "       .globl          my_tramp1\n"
114 "   my_tramp1:"
115 "       hint    34\n" // bti    c
116 "       sub     sp, sp, #32\n"
117 "       stp     x9, x30, [sp]\n"
118 "       str     x0, [sp, #16]\n"
119 "       mov     x0, x30\n"
120 "       bl      my_direct_func1\n"
121 "       ldp     x30, x9, [sp]\n"
122 "       ldr     x0, [sp, #16]\n"
123 "       add     sp, sp, #32\n"
124 "       ret     x9\n"
125 "       .size           my_tramp1, .-my_tramp1\n"
126
127 "       .type           my_tramp2, @function\n"
128 "       .globl          my_tramp2\n"
129 "   my_tramp2:"
130 "       hint    34\n" // bti    c
131 "       sub     sp, sp, #32\n"
132 "       stp     x9, x30, [sp]\n"
133 "       str     x0, [sp, #16]\n"
134 "       mov     x0, x30\n"
135 "       bl      my_direct_func2\n"
136 "       ldp     x30, x9, [sp]\n"
137 "       ldr     x0, [sp, #16]\n"
138 "       add     sp, sp, #32\n"
139 "       ret     x9\n"
140 "       .size           my_tramp2, .-my_tramp2\n"
141 "       .popsection\n"
142 );
143
144 #endif /* CONFIG_ARM64 */
145
146 #ifdef CONFIG_LOONGARCH
147 #include <asm/asm.h>
148
149 asm (
150 "       .pushsection    .text, \"ax\", @progbits\n"
151 "       .type           my_tramp1, @function\n"
152 "       .globl          my_tramp1\n"
153 "   my_tramp1:\n"
154 "       addi.d  $sp, $sp, -32\n"
155 "       st.d    $a0, $sp, 0\n"
156 "       st.d    $t0, $sp, 8\n"
157 "       st.d    $ra, $sp, 16\n"
158 "       move    $a0, $t0\n"
159 "       bl      my_direct_func1\n"
160 "       ld.d    $a0, $sp, 0\n"
161 "       ld.d    $t0, $sp, 8\n"
162 "       ld.d    $ra, $sp, 16\n"
163 "       addi.d  $sp, $sp, 32\n"
164 "       jr      $t0\n"
165 "       .size           my_tramp1, .-my_tramp1\n"
166
167 "       .type           my_tramp2, @function\n"
168 "       .globl          my_tramp2\n"
169 "   my_tramp2:\n"
170 "       addi.d  $sp, $sp, -32\n"
171 "       st.d    $a0, $sp, 0\n"
172 "       st.d    $t0, $sp, 8\n"
173 "       st.d    $ra, $sp, 16\n"
174 "       move    $a0, $t0\n"
175 "       bl      my_direct_func2\n"
176 "       ld.d    $a0, $sp, 0\n"
177 "       ld.d    $t0, $sp, 8\n"
178 "       ld.d    $ra, $sp, 16\n"
179 "       addi.d  $sp, $sp, 32\n"
180 "       jr      $t0\n"
181 "       .size           my_tramp2, .-my_tramp2\n"
182 "       .popsection\n"
183 );
184
185 #endif /* CONFIG_LOONGARCH */
186
187 static unsigned long my_tramp = (unsigned long)my_tramp1;
188 static unsigned long tramps[2] = {
189         (unsigned long)my_tramp1,
190         (unsigned long)my_tramp2,
191 };
192
193 static struct ftrace_ops direct;
194
195 static int simple_thread(void *arg)
196 {
197         static int t;
198         int ret = 0;
199
200         while (!kthread_should_stop()) {
201                 set_current_state(TASK_INTERRUPTIBLE);
202                 schedule_timeout(2 * HZ);
203
204                 if (ret)
205                         continue;
206                 t ^= 1;
207                 ret = modify_ftrace_direct(&direct, tramps[t]);
208                 if (!ret)
209                         my_tramp = tramps[t];
210                 WARN_ON_ONCE(ret);
211         }
212
213         return 0;
214 }
215
216 static struct task_struct *simple_tsk;
217
218 static int __init ftrace_direct_multi_init(void)
219 {
220         int ret;
221
222         ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
223         ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
224
225         ret = register_ftrace_direct(&direct, my_tramp);
226
227         if (!ret)
228                 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
229         return ret;
230 }
231
232 static void __exit ftrace_direct_multi_exit(void)
233 {
234         kthread_stop(simple_tsk);
235         unregister_ftrace_direct(&direct, my_tramp, true);
236 }
237
238 module_init(ftrace_direct_multi_init);
239 module_exit(ftrace_direct_multi_exit);
240
241 MODULE_AUTHOR("Jiri Olsa");
242 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
243 MODULE_LICENSE("GPL");