Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/samples/ftrace/ftrace-direct-multi.c
26278 views
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
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
8
#include <asm/asm-offsets.h>
9
#endif
10
11
extern void my_direct_func(unsigned long ip);
12
13
void my_direct_func(unsigned long ip)
14
{
15
trace_printk("ip %lx\n", ip);
16
}
17
18
extern void my_tramp(void *);
19
20
#ifdef CONFIG_RISCV
21
#include <asm/asm.h>
22
23
asm (
24
" .pushsection .text, \"ax\", @progbits\n"
25
" .type my_tramp, @function\n"
26
" .globl my_tramp\n"
27
" my_tramp:\n"
28
" addi sp,sp,-3*"SZREG"\n"
29
" "REG_S" a0,0*"SZREG"(sp)\n"
30
" "REG_S" t0,1*"SZREG"(sp)\n"
31
" "REG_S" ra,2*"SZREG"(sp)\n"
32
" mv a0,t0\n"
33
" call my_direct_func\n"
34
" "REG_L" a0,0*"SZREG"(sp)\n"
35
" "REG_L" t0,1*"SZREG"(sp)\n"
36
" "REG_L" ra,2*"SZREG"(sp)\n"
37
" addi sp,sp,3*"SZREG"\n"
38
" jr t0\n"
39
" .size my_tramp, .-my_tramp\n"
40
" .popsection\n"
41
);
42
43
#endif /* CONFIG_RISCV */
44
45
#ifdef CONFIG_X86_64
46
47
#include <asm/ibt.h>
48
#include <asm/nospec-branch.h>
49
50
asm (
51
" .pushsection .text, \"ax\", @progbits\n"
52
" .type my_tramp, @function\n"
53
" .globl my_tramp\n"
54
" my_tramp:"
55
ASM_ENDBR
56
" pushq %rbp\n"
57
" movq %rsp, %rbp\n"
58
CALL_DEPTH_ACCOUNT
59
" pushq %rdi\n"
60
" movq 8(%rbp), %rdi\n"
61
" call my_direct_func\n"
62
" popq %rdi\n"
63
" leave\n"
64
ASM_RET
65
" .size my_tramp, .-my_tramp\n"
66
" .popsection\n"
67
);
68
69
#endif /* CONFIG_X86_64 */
70
71
#ifdef CONFIG_S390
72
73
asm (
74
" .pushsection .text, \"ax\", @progbits\n"
75
" .type my_tramp, @function\n"
76
" .globl my_tramp\n"
77
" my_tramp:"
78
" lgr %r1,%r15\n"
79
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
80
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
81
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
82
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
83
" lgr %r2,%r0\n"
84
" brasl %r14,my_direct_func\n"
85
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
86
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
87
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
88
" lgr %r1,%r0\n"
89
" br %r1\n"
90
" .size my_tramp, .-my_tramp\n"
91
" .popsection\n"
92
);
93
94
#endif /* CONFIG_S390 */
95
96
#ifdef CONFIG_ARM64
97
98
asm (
99
" .pushsection .text, \"ax\", @progbits\n"
100
" .type my_tramp, @function\n"
101
" .globl my_tramp\n"
102
" my_tramp:"
103
" hint 34\n" // bti c
104
" sub sp, sp, #32\n"
105
" stp x9, x30, [sp]\n"
106
" str x0, [sp, #16]\n"
107
" mov x0, x30\n"
108
" bl my_direct_func\n"
109
" ldp x30, x9, [sp]\n"
110
" ldr x0, [sp, #16]\n"
111
" add sp, sp, #32\n"
112
" ret x9\n"
113
" .size my_tramp, .-my_tramp\n"
114
" .popsection\n"
115
);
116
117
#endif /* CONFIG_ARM64 */
118
119
#ifdef CONFIG_LOONGARCH
120
121
#include <asm/asm.h>
122
asm (
123
" .pushsection .text, \"ax\", @progbits\n"
124
" .type my_tramp, @function\n"
125
" .globl my_tramp\n"
126
" my_tramp:\n"
127
" addi.d $sp, $sp, -32\n"
128
" st.d $a0, $sp, 0\n"
129
" st.d $t0, $sp, 8\n"
130
" st.d $ra, $sp, 16\n"
131
" move $a0, $t0\n"
132
" bl my_direct_func\n"
133
" ld.d $a0, $sp, 0\n"
134
" ld.d $t0, $sp, 8\n"
135
" ld.d $ra, $sp, 16\n"
136
" addi.d $sp, $sp, 32\n"
137
" jr $t0\n"
138
" .size my_tramp, .-my_tramp\n"
139
" .popsection\n"
140
);
141
142
#endif /* CONFIG_LOONGARCH */
143
144
#ifdef CONFIG_PPC
145
#include <asm/ppc_asm.h>
146
147
#ifdef CONFIG_PPC64
148
#define STACK_FRAME_SIZE 48
149
#else
150
#define STACK_FRAME_SIZE 24
151
#endif
152
153
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
154
#define PPC64_TOC_SAVE_AND_UPDATE \
155
" std 2, 24(1)\n" \
156
" bcl 20, 31, 1f\n" \
157
" 1: mflr 12\n" \
158
" ld 2, (99f - 1b)(12)\n"
159
#define PPC64_TOC_RESTORE \
160
" ld 2, 24(1)\n"
161
#define PPC64_TOC \
162
" 99: .quad .TOC.@tocbase\n"
163
#else
164
#define PPC64_TOC_SAVE_AND_UPDATE ""
165
#define PPC64_TOC_RESTORE ""
166
#define PPC64_TOC ""
167
#endif
168
169
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
170
#define PPC_FTRACE_RESTORE_LR \
171
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
172
" mtlr 0\n"
173
#define PPC_FTRACE_RET \
174
" blr\n"
175
#define PPC_FTRACE_RECOVER_IP \
176
" lwz 8, 4(3)\n" \
177
" li 9, 6\n" \
178
" slw 8, 8, 9\n" \
179
" sraw 8, 8, 9\n" \
180
" add 3, 3, 8\n" \
181
" addi 3, 3, 4\n"
182
#else
183
#define PPC_FTRACE_RESTORE_LR \
184
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
185
" mtctr 0\n"
186
#define PPC_FTRACE_RET \
187
" mtlr 0\n" \
188
" bctr\n"
189
#define PPC_FTRACE_RECOVER_IP ""
190
#endif
191
192
asm (
193
" .pushsection .text, \"ax\", @progbits\n"
194
" .type my_tramp, @function\n"
195
" .globl my_tramp\n"
196
" my_tramp:\n"
197
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
198
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
199
" mflr 0\n"
200
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
201
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
202
PPC64_TOC_SAVE_AND_UPDATE
203
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
204
" mr 3, 0\n"
205
PPC_FTRACE_RECOVER_IP
206
" bl my_direct_func\n"
207
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
208
PPC64_TOC_RESTORE
209
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
210
PPC_FTRACE_RESTORE_LR
211
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
212
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
213
PPC_FTRACE_RET
214
PPC64_TOC
215
" .size my_tramp, .-my_tramp\n"
216
" .popsection\n"
217
);
218
219
#endif /* CONFIG_PPC */
220
221
static struct ftrace_ops direct;
222
223
static int __init ftrace_direct_multi_init(void)
224
{
225
ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
226
ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
227
228
return register_ftrace_direct(&direct, (unsigned long) my_tramp);
229
}
230
231
static void __exit ftrace_direct_multi_exit(void)
232
{
233
unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true);
234
}
235
236
module_init(ftrace_direct_multi_init);
237
module_exit(ftrace_direct_multi_exit);
238
239
MODULE_AUTHOR("Jiri Olsa");
240
MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
241
MODULE_LICENSE("GPL");
242
243