libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL
[linux-block.git] / tools / lib / bpf / bpf_tracing.h
CommitLineData
3ac4dbe3
AN
1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2#ifndef __BPF_TRACING_H__
3#define __BPF_TRACING_H__
4
5/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
6#if defined(__TARGET_ARCH_x86)
7 #define bpf_target_x86
8 #define bpf_target_defined
9#elif defined(__TARGET_ARCH_s390)
10 #define bpf_target_s390
11 #define bpf_target_defined
12#elif defined(__TARGET_ARCH_arm)
13 #define bpf_target_arm
14 #define bpf_target_defined
15#elif defined(__TARGET_ARCH_arm64)
16 #define bpf_target_arm64
17 #define bpf_target_defined
18#elif defined(__TARGET_ARCH_mips)
19 #define bpf_target_mips
20 #define bpf_target_defined
21#elif defined(__TARGET_ARCH_powerpc)
22 #define bpf_target_powerpc
23 #define bpf_target_defined
24#elif defined(__TARGET_ARCH_sparc)
25 #define bpf_target_sparc
26 #define bpf_target_defined
589fed47
BT
27#elif defined(__TARGET_ARCH_riscv)
28 #define bpf_target_riscv
29 #define bpf_target_defined
3ac4dbe3 30#else
3ac4dbe3
AN
31
32/* Fall back to what the compiler says */
3ac4dbe3
AN
33#if defined(__x86_64__)
34 #define bpf_target_x86
4a638d58 35 #define bpf_target_defined
3ac4dbe3
AN
36#elif defined(__s390__)
37 #define bpf_target_s390
4a638d58 38 #define bpf_target_defined
3ac4dbe3
AN
39#elif defined(__arm__)
40 #define bpf_target_arm
4a638d58 41 #define bpf_target_defined
3ac4dbe3
AN
42#elif defined(__aarch64__)
43 #define bpf_target_arm64
4a638d58 44 #define bpf_target_defined
3ac4dbe3
AN
45#elif defined(__mips__)
46 #define bpf_target_mips
4a638d58 47 #define bpf_target_defined
3ac4dbe3
AN
48#elif defined(__powerpc__)
49 #define bpf_target_powerpc
4a638d58 50 #define bpf_target_defined
3ac4dbe3
AN
51#elif defined(__sparc__)
52 #define bpf_target_sparc
4a638d58 53 #define bpf_target_defined
589fed47
BT
54#elif defined(__riscv) && __riscv_xlen == 64
55 #define bpf_target_riscv
56 #define bpf_target_defined
4a638d58
LB
57#endif /* no compiler target */
58
3ac4dbe3 59#endif
4a638d58
LB
60
61#ifndef __BPF_TARGET_MISSING
62#define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\""
3ac4dbe3
AN
63#endif
64
65#if defined(bpf_target_x86)
66
fd56e005 67#if defined(__KERNEL__) || defined(__VMLINUX_H__)
b8ebce86 68
3cc31d79
AN
69#define __PT_PARM1_REG di
70#define __PT_PARM2_REG si
71#define __PT_PARM3_REG dx
72#define __PT_PARM4_REG cx
73#define __PT_PARM5_REG r8
74#define __PT_RET_REG sp
75#define __PT_FP_REG bp
76#define __PT_RC_REG ax
77#define __PT_SP_REG sp
78#define __PT_IP_REG ip
60d16c5c
IL
79/* syscall uses r10 for PARM4 */
80#define PT_REGS_PARM4_SYSCALL(x) ((x)->r10)
81#define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(x, r10)
b8ebce86 82
3ac4dbe3 83#else
b8ebce86 84
3ac4dbe3 85#ifdef __i386__
b8ebce86 86
3cc31d79
AN
87#define __PT_PARM1_REG eax
88#define __PT_PARM2_REG edx
89#define __PT_PARM3_REG ecx
90/* i386 kernel is built with -mregparm=3 */
91#define __PT_PARM4_REG __unsupported__
92#define __PT_PARM5_REG __unsupported__
93#define __PT_RET_REG esp
94#define __PT_FP_REG ebp
95#define __PT_RC_REG eax
96#define __PT_SP_REG esp
97#define __PT_IP_REG eip
98
99#else /* __i386__ */
100
101#define __PT_PARM1_REG rdi
102#define __PT_PARM2_REG rsi
103#define __PT_PARM3_REG rdx
104#define __PT_PARM4_REG rcx
105#define __PT_PARM5_REG r8
106#define __PT_RET_REG rsp
107#define __PT_FP_REG rbp
108#define __PT_RC_REG rax
109#define __PT_SP_REG rsp
110#define __PT_IP_REG rip
60d16c5c
IL
111/* syscall uses r10 for PARM4 */
112#define PT_REGS_PARM4_SYSCALL(x) ((x)->r10)
113#define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(x, r10)
3cc31d79
AN
114
115#endif /* __i386__ */
116
117#endif /* __KERNEL__ || __VMLINUX_H__ */
3ac4dbe3
AN
118
119#elif defined(bpf_target_s390)
120
121/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
3cc31d79
AN
122#define __PT_REGS_CAST(x) ((const user_pt_regs *)(x))
123#define __PT_PARM1_REG gprs[2]
124#define __PT_PARM2_REG gprs[3]
125#define __PT_PARM3_REG gprs[4]
126#define __PT_PARM4_REG gprs[5]
127#define __PT_PARM5_REG gprs[6]
128#define __PT_RET_REG grps[14]
129#define __PT_FP_REG gprs[11] /* Works only with CONFIG_FRAME_POINTER */
130#define __PT_RC_REG gprs[2]
131#define __PT_SP_REG gprs[15]
132#define __PT_IP_REG psw.addr
b8ebce86 133
3ac4dbe3
AN
134#elif defined(bpf_target_arm)
135
3cc31d79
AN
136#define __PT_PARM1_REG uregs[0]
137#define __PT_PARM2_REG uregs[1]
138#define __PT_PARM3_REG uregs[2]
139#define __PT_PARM4_REG uregs[3]
140#define __PT_PARM5_REG uregs[4]
141#define __PT_RET_REG uregs[14]
142#define __PT_FP_REG uregs[11] /* Works only with CONFIG_FRAME_POINTER */
143#define __PT_RC_REG uregs[0]
144#define __PT_SP_REG uregs[13]
145#define __PT_IP_REG uregs[12]
b8ebce86 146
3ac4dbe3
AN
147#elif defined(bpf_target_arm64)
148
149/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
3cc31d79
AN
150#define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
151#define __PT_PARM1_REG regs[0]
152#define __PT_PARM2_REG regs[1]
153#define __PT_PARM3_REG regs[2]
154#define __PT_PARM4_REG regs[3]
155#define __PT_PARM5_REG regs[4]
156#define __PT_RET_REG regs[30]
157#define __PT_FP_REG regs[29] /* Works only with CONFIG_FRAME_POINTER */
158#define __PT_RC_REG regs[0]
159#define __PT_SP_REG sp
160#define __PT_IP_REG pc
b8ebce86 161
3ac4dbe3
AN
162#elif defined(bpf_target_mips)
163
3cc31d79
AN
164#define __PT_PARM1_REG regs[4]
165#define __PT_PARM2_REG regs[5]
166#define __PT_PARM3_REG regs[6]
167#define __PT_PARM4_REG regs[7]
168#define __PT_PARM5_REG regs[8]
169#define __PT_RET_REG regs[31]
170#define __PT_FP_REG regs[30] /* Works only with CONFIG_FRAME_POINTER */
171#define __PT_RC_REG regs[2]
172#define __PT_SP_REG regs[29]
173#define __PT_IP_REG cp0_epc
b8ebce86 174
3ac4dbe3
AN
175#elif defined(bpf_target_powerpc)
176
3cc31d79
AN
177#define __PT_PARM1_REG gpr[3]
178#define __PT_PARM2_REG gpr[4]
179#define __PT_PARM3_REG gpr[5]
180#define __PT_PARM4_REG gpr[6]
181#define __PT_PARM5_REG gpr[7]
182#define __PT_RET_REG regs[31]
183#define __PT_FP_REG __unsupported__
184#define __PT_RC_REG gpr[3]
185#define __PT_SP_REG sp
186#define __PT_IP_REG nip
f07f1503
IL
187/* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER. */
188#define PT_REGS_SYSCALL_REGS(ctx) ctx
b8ebce86 189
3ac4dbe3
AN
190#elif defined(bpf_target_sparc)
191
3cc31d79
AN
192#define __PT_PARM1_REG u_regs[UREG_I0]
193#define __PT_PARM2_REG u_regs[UREG_I1]
194#define __PT_PARM3_REG u_regs[UREG_I2]
195#define __PT_PARM4_REG u_regs[UREG_I3]
196#define __PT_PARM5_REG u_regs[UREG_I4]
197#define __PT_RET_REG u_regs[UREG_I7]
198#define __PT_FP_REG __unsupported__
199#define __PT_RC_REG u_regs[UREG_I0]
200#define __PT_SP_REG u_regs[UREG_FP]
3ac4dbe3
AN
201/* Should this also be a bpf_target check for the sparc case? */
202#if defined(__arch64__)
3cc31d79 203#define __PT_IP_REG tpc
3ac4dbe3 204#else
3cc31d79 205#define __PT_IP_REG pc
3ac4dbe3
AN
206#endif
207
589fed47
BT
208#elif defined(bpf_target_riscv)
209
3cc31d79
AN
210#define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
211#define __PT_PARM1_REG a0
212#define __PT_PARM2_REG a1
213#define __PT_PARM3_REG a2
214#define __PT_PARM4_REG a3
215#define __PT_PARM5_REG a4
216#define __PT_RET_REG ra
5c101153 217#define __PT_FP_REG s0
3cc31d79
AN
218#define __PT_RC_REG a5
219#define __PT_SP_REG sp
5c101153 220#define __PT_IP_REG pc
cf0b5b27
IL
221/* riscv does not select ARCH_HAS_SYSCALL_WRAPPER. */
222#define PT_REGS_SYSCALL_REGS(ctx) ctx
3cc31d79
AN
223
224#endif
225
226#if defined(bpf_target_defined)
227
589fed47 228struct pt_regs;
589fed47 229
3cc31d79
AN
230/* allow some architecutres to override `struct pt_regs` */
231#ifndef __PT_REGS_CAST
232#define __PT_REGS_CAST(x) (x)
3ac4dbe3
AN
233#endif
234
3cc31d79
AN
235#define PT_REGS_PARM1(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG)
236#define PT_REGS_PARM2(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG)
237#define PT_REGS_PARM3(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG)
238#define PT_REGS_PARM4(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG)
239#define PT_REGS_PARM5(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG)
240#define PT_REGS_RET(x) (__PT_REGS_CAST(x)->__PT_RET_REG)
241#define PT_REGS_FP(x) (__PT_REGS_CAST(x)->__PT_FP_REG)
242#define PT_REGS_RC(x) (__PT_REGS_CAST(x)->__PT_RC_REG)
243#define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG)
244#define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG)
245
246#define PT_REGS_PARM1_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG)
247#define PT_REGS_PARM2_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG)
248#define PT_REGS_PARM3_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG)
249#define PT_REGS_PARM4_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG)
250#define PT_REGS_PARM5_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG)
251#define PT_REGS_RET_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RET_REG)
252#define PT_REGS_FP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_FP_REG)
253#define PT_REGS_RC_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RC_REG)
254#define PT_REGS_SP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_SP_REG)
255#define PT_REGS_IP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_IP_REG)
256
3ac4dbe3 257#if defined(bpf_target_powerpc)
3cc31d79 258
3ac4dbe3
AN
259#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
260#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
3cc31d79 261
3ac4dbe3 262#elif defined(bpf_target_sparc)
3cc31d79 263
3ac4dbe3
AN
264#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
265#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
3cc31d79
AN
266
267#else
268
3ac4dbe3 269#define BPF_KPROBE_READ_RET_IP(ip, ctx) \
70785cfb 270 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
3ac4dbe3 271#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \
3cc31d79
AN
272 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
273
3ac4dbe3
AN
274#endif
275
60d16c5c 276#ifndef PT_REGS_PARM1_SYSCALL
d084df3b 277#define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1(x)
60d16c5c 278#endif
d084df3b
KT
279#define PT_REGS_PARM2_SYSCALL(x) PT_REGS_PARM2(x)
280#define PT_REGS_PARM3_SYSCALL(x) PT_REGS_PARM3(x)
60d16c5c 281#ifndef PT_REGS_PARM4_SYSCALL
d084df3b
KT
282#define PT_REGS_PARM4_SYSCALL(x) PT_REGS_PARM4(x)
283#endif
284#define PT_REGS_PARM5_SYSCALL(x) PT_REGS_PARM5(x)
285
60d16c5c 286#ifndef PT_REGS_PARM1_CORE_SYSCALL
d084df3b 287#define PT_REGS_PARM1_CORE_SYSCALL(x) PT_REGS_PARM1_CORE(x)
60d16c5c 288#endif
d084df3b
KT
289#define PT_REGS_PARM2_CORE_SYSCALL(x) PT_REGS_PARM2_CORE(x)
290#define PT_REGS_PARM3_CORE_SYSCALL(x) PT_REGS_PARM3_CORE(x)
60d16c5c 291#ifndef PT_REGS_PARM4_CORE_SYSCALL
d084df3b
KT
292#define PT_REGS_PARM4_CORE_SYSCALL(x) PT_REGS_PARM4_CORE(x)
293#endif
294#define PT_REGS_PARM5_CORE_SYSCALL(x) PT_REGS_PARM5_CORE(x)
295
3cc31d79 296#else /* defined(bpf_target_defined) */
4a638d58
LB
297
298#define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
299#define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
300#define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
301#define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
302#define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
303#define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
304#define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
305#define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
306#define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
307#define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
308
309#define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
310#define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
311#define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
312#define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
313#define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
314#define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
315#define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
316#define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
317#define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
318#define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
319
320#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
321#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
322
d084df3b
KT
323#define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
324#define PT_REGS_PARM2_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
325#define PT_REGS_PARM3_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
326#define PT_REGS_PARM4_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
327#define PT_REGS_PARM5_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
328
329#define PT_REGS_PARM1_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
330#define PT_REGS_PARM2_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
331#define PT_REGS_PARM3_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
332#define PT_REGS_PARM4_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
333#define PT_REGS_PARM5_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
334
3cc31d79 335#endif /* defined(bpf_target_defined) */
4a638d58 336
c5a1ffa0
IL
337/*
338 * When invoked from a syscall handler kprobe, returns a pointer to a
339 * struct pt_regs containing syscall arguments and suitable for passing to
340 * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
341 */
342#ifndef PT_REGS_SYSCALL_REGS
343/* By default, assume that the arch selects ARCH_HAS_SYSCALL_WRAPPER. */
344#define PT_REGS_SYSCALL_REGS(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
345#endif
346
d6a6a555 347#ifndef ___bpf_concat
df8ff353 348#define ___bpf_concat(a, b) a ## b
d6a6a555
FR
349#endif
350#ifndef ___bpf_apply
df8ff353 351#define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
d6a6a555
FR
352#endif
353#ifndef ___bpf_nth
df8ff353 354#define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
d6a6a555
FR
355#endif
356#ifndef ___bpf_narg
f60edf5b 357#define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
d6a6a555 358#endif
df8ff353 359
f60edf5b
AN
360#define ___bpf_ctx_cast0() ctx
361#define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0]
362#define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1]
363#define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2]
364#define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3]
365#define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4]
366#define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5]
367#define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6]
368#define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7]
369#define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8]
df8ff353
AN
370#define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
371#define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
372#define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
f60edf5b 373#define ___bpf_ctx_cast(args...) ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
df8ff353
AN
374
375/*
376 * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
377 * similar kinds of BPF programs, that accept input arguments as a single
378 * pointer to untyped u64 array, where each u64 can actually be a typed
379 * pointer or integer of different size. Instead of requring user to write
380 * manual casts and work with array elements by index, BPF_PROG macro
381 * allows user to declare a list of named and typed input arguments in the
382 * same syntax as for normal C function. All the casting is hidden and
383 * performed transparently, while user code can just assume working with
384 * function arguments of specified type and name.
385 *
386 * Original raw context argument is preserved as well as 'ctx' argument.
387 * This is useful when using BPF helpers that expect original context
388 * as one of the parameters (e.g., for bpf_perf_event_output()).
389 */
390#define BPF_PROG(name, args...) \
391name(unsigned long long *ctx); \
392static __attribute__((always_inline)) typeof(name(0)) \
393____##name(unsigned long long *ctx, ##args); \
394typeof(name(0)) name(unsigned long long *ctx) \
395{ \
396 _Pragma("GCC diagnostic push") \
397 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
398 return ____##name(___bpf_ctx_cast(args)); \
399 _Pragma("GCC diagnostic pop") \
400} \
401static __attribute__((always_inline)) typeof(name(0)) \
402____##name(unsigned long long *ctx, ##args)
403
404struct pt_regs;
405
f60edf5b
AN
406#define ___bpf_kprobe_args0() ctx
407#define ___bpf_kprobe_args1(x) ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx)
408#define ___bpf_kprobe_args2(x, args...) ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx)
409#define ___bpf_kprobe_args3(x, args...) ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx)
410#define ___bpf_kprobe_args4(x, args...) ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx)
411#define ___bpf_kprobe_args5(x, args...) ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx)
412#define ___bpf_kprobe_args(args...) ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args)
df8ff353
AN
413
414/*
415 * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for
416 * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific
417 * low-level way of getting kprobe input arguments from struct pt_regs, and
418 * provides a familiar typed and named function arguments syntax and
419 * semantics of accessing kprobe input paremeters.
420 *
421 * Original struct pt_regs* context is preserved as 'ctx' argument. This might
422 * be necessary when using BPF helpers like bpf_perf_event_output().
423 */
424#define BPF_KPROBE(name, args...) \
425name(struct pt_regs *ctx); \
426static __attribute__((always_inline)) typeof(name(0)) \
427____##name(struct pt_regs *ctx, ##args); \
428typeof(name(0)) name(struct pt_regs *ctx) \
429{ \
430 _Pragma("GCC diagnostic push") \
431 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
432 return ____##name(___bpf_kprobe_args(args)); \
433 _Pragma("GCC diagnostic pop") \
434} \
435static __attribute__((always_inline)) typeof(name(0)) \
436____##name(struct pt_regs *ctx, ##args)
437
f60edf5b
AN
438#define ___bpf_kretprobe_args0() ctx
439#define ___bpf_kretprobe_args1(x) ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx)
440#define ___bpf_kretprobe_args(args...) ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args)
df8ff353
AN
441
442/*
443 * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional
444 * return value (in addition to `struct pt_regs *ctx`), but no input
445 * arguments, because they will be clobbered by the time probed function
446 * returns.
447 */
448#define BPF_KRETPROBE(name, args...) \
449name(struct pt_regs *ctx); \
450static __attribute__((always_inline)) typeof(name(0)) \
451____##name(struct pt_regs *ctx, ##args); \
452typeof(name(0)) name(struct pt_regs *ctx) \
453{ \
454 _Pragma("GCC diagnostic push") \
455 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
456 return ____##name(___bpf_kretprobe_args(args)); \
457 _Pragma("GCC diagnostic pop") \
458} \
459static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
460
3ac4dbe3 461#endif