1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_TRACING_H__
3 #define __BPF_TRACING_H__
5 #include <bpf/bpf_helpers.h>
7 /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
8 #if defined(__TARGET_ARCH_x86)
10 #define bpf_target_defined
11 #elif defined(__TARGET_ARCH_s390)
12 #define bpf_target_s390
13 #define bpf_target_defined
14 #elif defined(__TARGET_ARCH_arm)
15 #define bpf_target_arm
16 #define bpf_target_defined
17 #elif defined(__TARGET_ARCH_arm64)
18 #define bpf_target_arm64
19 #define bpf_target_defined
20 #elif defined(__TARGET_ARCH_mips)
21 #define bpf_target_mips
22 #define bpf_target_defined
23 #elif defined(__TARGET_ARCH_powerpc)
24 #define bpf_target_powerpc
25 #define bpf_target_defined
26 #elif defined(__TARGET_ARCH_sparc)
27 #define bpf_target_sparc
28 #define bpf_target_defined
29 #elif defined(__TARGET_ARCH_riscv)
30 #define bpf_target_riscv
31 #define bpf_target_defined
32 #elif defined(__TARGET_ARCH_arc)
33 #define bpf_target_arc
34 #define bpf_target_defined
35 #elif defined(__TARGET_ARCH_loongarch)
36 #define bpf_target_loongarch
37 #define bpf_target_defined
40 /* Fall back to what the compiler says */
41 #if defined(__x86_64__)
42 #define bpf_target_x86
43 #define bpf_target_defined
44 #elif defined(__s390__)
45 #define bpf_target_s390
46 #define bpf_target_defined
47 #elif defined(__arm__)
48 #define bpf_target_arm
49 #define bpf_target_defined
50 #elif defined(__aarch64__)
51 #define bpf_target_arm64
52 #define bpf_target_defined
53 #elif defined(__mips__)
54 #define bpf_target_mips
55 #define bpf_target_defined
56 #elif defined(__powerpc__)
57 #define bpf_target_powerpc
58 #define bpf_target_defined
59 #elif defined(__sparc__)
60 #define bpf_target_sparc
61 #define bpf_target_defined
62 #elif defined(__riscv) && __riscv_xlen == 64
63 #define bpf_target_riscv
64 #define bpf_target_defined
65 #elif defined(__arc__)
66 #define bpf_target_arc
67 #define bpf_target_defined
68 #elif defined(__loongarch__)
69 #define bpf_target_loongarch
70 #define bpf_target_defined
71 #endif /* no compiler target */
75 #ifndef __BPF_TARGET_MISSING
76 #define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\""
79 #if defined(bpf_target_x86)
82 * https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI
85 #if defined(__KERNEL__) || defined(__VMLINUX_H__)
87 #define __PT_PARM1_REG di
88 #define __PT_PARM2_REG si
89 #define __PT_PARM3_REG dx
90 #define __PT_PARM4_REG cx
91 #define __PT_PARM5_REG r8
92 #define __PT_PARM6_REG r9
93 #define __PT_RET_REG sp
94 #define __PT_FP_REG bp
95 #define __PT_RC_REG ax
96 #define __PT_SP_REG sp
97 #define __PT_IP_REG ip
98 /* syscall uses r10 for PARM4 */
99 #define PT_REGS_PARM4_SYSCALL(x) ((x)->r10)
100 #define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(x, r10)
106 /* i386 kernel is built with -mregparm=3 */
107 #define __PT_PARM1_REG eax
108 #define __PT_PARM2_REG edx
109 #define __PT_PARM3_REG ecx
110 #define __PT_RET_REG esp
111 #define __PT_FP_REG ebp
112 #define __PT_RC_REG eax
113 #define __PT_SP_REG esp
114 #define __PT_IP_REG eip
118 #define __PT_PARM1_REG rdi
119 #define __PT_PARM2_REG rsi
120 #define __PT_PARM3_REG rdx
121 #define __PT_PARM4_REG rcx
122 #define __PT_PARM5_REG r8
123 #define __PT_PARM6_REG r9
124 #define __PT_RET_REG rsp
125 #define __PT_FP_REG rbp
126 #define __PT_RC_REG rax
127 #define __PT_SP_REG rsp
128 #define __PT_IP_REG rip
129 /* syscall uses r10 for PARM4 */
130 #define PT_REGS_PARM4_SYSCALL(x) ((x)->r10)
131 #define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(x, r10)
133 #endif /* __i386__ */
135 #endif /* __KERNEL__ || __VMLINUX_H__ */
137 #elif defined(bpf_target_s390)
139 struct pt_regs___s390 {
140 unsigned long orig_gpr2;
143 /* s390 provides user_pt_regs instead of struct pt_regs to userspace */
144 #define __PT_REGS_CAST(x) ((const user_pt_regs *)(x))
145 #define __PT_PARM1_REG gprs[2]
146 #define __PT_PARM2_REG gprs[3]
147 #define __PT_PARM3_REG gprs[4]
148 #define __PT_PARM4_REG gprs[5]
149 #define __PT_PARM5_REG gprs[6]
150 #define __PT_RET_REG gprs[14]
151 #define __PT_FP_REG gprs[11] /* Works only with CONFIG_FRAME_POINTER */
152 #define __PT_RC_REG gprs[2]
153 #define __PT_SP_REG gprs[15]
154 #define __PT_IP_REG psw.addr
155 #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
156 #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___s390 *)(x), orig_gpr2)
158 #elif defined(bpf_target_arm)
161 * https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers
164 #define __PT_PARM1_REG uregs[0]
165 #define __PT_PARM2_REG uregs[1]
166 #define __PT_PARM3_REG uregs[2]
167 #define __PT_PARM4_REG uregs[3]
168 #define __PT_RET_REG uregs[14]
169 #define __PT_FP_REG uregs[11] /* Works only with CONFIG_FRAME_POINTER */
170 #define __PT_RC_REG uregs[0]
171 #define __PT_SP_REG uregs[13]
172 #define __PT_IP_REG uregs[12]
174 #elif defined(bpf_target_arm64)
177 * https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers
180 struct pt_regs___arm64 {
181 unsigned long orig_x0;
184 /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
185 #define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
186 #define __PT_PARM1_REG regs[0]
187 #define __PT_PARM2_REG regs[1]
188 #define __PT_PARM3_REG regs[2]
189 #define __PT_PARM4_REG regs[3]
190 #define __PT_PARM5_REG regs[4]
191 #define __PT_PARM6_REG regs[5]
192 #define __PT_PARM7_REG regs[6]
193 #define __PT_PARM8_REG regs[7]
194 #define __PT_RET_REG regs[30]
195 #define __PT_FP_REG regs[29] /* Works only with CONFIG_FRAME_POINTER */
196 #define __PT_RC_REG regs[0]
197 #define __PT_SP_REG sp
198 #define __PT_IP_REG pc
199 #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
200 #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___arm64 *)(x), orig_x0)
202 #elif defined(bpf_target_mips)
204 #define __PT_PARM1_REG regs[4]
205 #define __PT_PARM2_REG regs[5]
206 #define __PT_PARM3_REG regs[6]
207 #define __PT_PARM4_REG regs[7]
208 #define __PT_PARM5_REG regs[8]
209 #define __PT_RET_REG regs[31]
210 #define __PT_FP_REG regs[30] /* Works only with CONFIG_FRAME_POINTER */
211 #define __PT_RC_REG regs[2]
212 #define __PT_SP_REG regs[29]
213 #define __PT_IP_REG cp0_epc
215 #elif defined(bpf_target_powerpc)
217 #define __PT_PARM1_REG gpr[3]
218 #define __PT_PARM2_REG gpr[4]
219 #define __PT_PARM3_REG gpr[5]
220 #define __PT_PARM4_REG gpr[6]
221 #define __PT_PARM5_REG gpr[7]
222 #define __PT_RET_REG regs[31]
223 #define __PT_FP_REG __unsupported__
224 #define __PT_RC_REG gpr[3]
225 #define __PT_SP_REG sp
226 #define __PT_IP_REG nip
227 /* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER. */
228 #define PT_REGS_SYSCALL_REGS(ctx) ctx
230 #elif defined(bpf_target_sparc)
232 #define __PT_PARM1_REG u_regs[UREG_I0]
233 #define __PT_PARM2_REG u_regs[UREG_I1]
234 #define __PT_PARM3_REG u_regs[UREG_I2]
235 #define __PT_PARM4_REG u_regs[UREG_I3]
236 #define __PT_PARM5_REG u_regs[UREG_I4]
237 #define __PT_RET_REG u_regs[UREG_I7]
238 #define __PT_FP_REG __unsupported__
239 #define __PT_RC_REG u_regs[UREG_I0]
240 #define __PT_SP_REG u_regs[UREG_FP]
241 /* Should this also be a bpf_target check for the sparc case? */
242 #if defined(__arch64__)
243 #define __PT_IP_REG tpc
245 #define __PT_IP_REG pc
248 #elif defined(bpf_target_riscv)
250 #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
251 #define __PT_PARM1_REG a0
252 #define __PT_PARM2_REG a1
253 #define __PT_PARM3_REG a2
254 #define __PT_PARM4_REG a3
255 #define __PT_PARM5_REG a4
256 #define __PT_RET_REG ra
257 #define __PT_FP_REG s0
258 #define __PT_RC_REG a0
259 #define __PT_SP_REG sp
260 #define __PT_IP_REG pc
261 /* riscv does not select ARCH_HAS_SYSCALL_WRAPPER. */
262 #define PT_REGS_SYSCALL_REGS(ctx) ctx
264 #elif defined(bpf_target_arc)
266 /* arc provides struct user_pt_regs instead of struct pt_regs to userspace */
267 #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
268 #define __PT_PARM1_REG scratch.r0
269 #define __PT_PARM2_REG scratch.r1
270 #define __PT_PARM3_REG scratch.r2
271 #define __PT_PARM4_REG scratch.r3
272 #define __PT_PARM5_REG scratch.r4
273 #define __PT_RET_REG scratch.blink
274 #define __PT_FP_REG __unsupported__
275 #define __PT_RC_REG scratch.r0
276 #define __PT_SP_REG scratch.sp
277 #define __PT_IP_REG scratch.ret
278 /* arc does not select ARCH_HAS_SYSCALL_WRAPPER. */
279 #define PT_REGS_SYSCALL_REGS(ctx) ctx
281 #elif defined(bpf_target_loongarch)
283 /* https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html */
285 #define __PT_PARM1_REG regs[4]
286 #define __PT_PARM2_REG regs[5]
287 #define __PT_PARM3_REG regs[6]
288 #define __PT_PARM4_REG regs[7]
289 #define __PT_PARM5_REG regs[8]
290 #define __PT_RET_REG regs[1]
291 #define __PT_FP_REG regs[22]
292 #define __PT_RC_REG regs[4]
293 #define __PT_SP_REG regs[3]
294 #define __PT_IP_REG csr_era
295 /* loongarch does not select ARCH_HAS_SYSCALL_WRAPPER. */
296 #define PT_REGS_SYSCALL_REGS(ctx) ctx
300 #if defined(bpf_target_defined)
304 /* allow some architectures to override `struct pt_regs` */
305 #ifndef __PT_REGS_CAST
306 #define __PT_REGS_CAST(x) (x)
310 * Different architectures support different number of arguments passed
311 * through registers. i386 supports just 3, some arches support up to 8.
313 #ifndef __PT_PARM4_REG
314 #define __PT_PARM4_REG __unsupported__
316 #ifndef __PT_PARM5_REG
317 #define __PT_PARM5_REG __unsupported__
319 #ifndef __PT_PARM6_REG
320 #define __PT_PARM6_REG __unsupported__
322 #ifndef __PT_PARM7_REG
323 #define __PT_PARM7_REG __unsupported__
325 #ifndef __PT_PARM8_REG
326 #define __PT_PARM8_REG __unsupported__
329 #define PT_REGS_PARM1(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG)
330 #define PT_REGS_PARM2(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG)
331 #define PT_REGS_PARM3(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG)
332 #define PT_REGS_PARM4(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG)
333 #define PT_REGS_PARM5(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG)
334 #define PT_REGS_PARM6(x) (__PT_REGS_CAST(x)->__PT_PARM6_REG)
335 #define PT_REGS_PARM7(x) (__PT_REGS_CAST(x)->__PT_PARM7_REG)
336 #define PT_REGS_PARM8(x) (__PT_REGS_CAST(x)->__PT_PARM8_REG)
337 #define PT_REGS_RET(x) (__PT_REGS_CAST(x)->__PT_RET_REG)
338 #define PT_REGS_FP(x) (__PT_REGS_CAST(x)->__PT_FP_REG)
339 #define PT_REGS_RC(x) (__PT_REGS_CAST(x)->__PT_RC_REG)
340 #define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG)
341 #define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG)
343 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG)
344 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG)
345 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG)
346 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG)
347 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG)
348 #define PT_REGS_PARM6_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_REG)
349 #define PT_REGS_PARM7_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_REG)
350 #define PT_REGS_PARM8_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM8_REG)
351 #define PT_REGS_RET_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RET_REG)
352 #define PT_REGS_FP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_FP_REG)
353 #define PT_REGS_RC_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RC_REG)
354 #define PT_REGS_SP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_SP_REG)
355 #define PT_REGS_IP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_IP_REG)
357 #if defined(bpf_target_powerpc)
359 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
360 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
362 #elif defined(bpf_target_sparc)
364 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
365 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
369 #define BPF_KPROBE_READ_RET_IP(ip, ctx) \
370 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
371 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \
372 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
376 #ifndef PT_REGS_PARM1_SYSCALL
377 #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1(x)
379 #define PT_REGS_PARM2_SYSCALL(x) PT_REGS_PARM2(x)
380 #define PT_REGS_PARM3_SYSCALL(x) PT_REGS_PARM3(x)
381 #ifndef PT_REGS_PARM4_SYSCALL
382 #define PT_REGS_PARM4_SYSCALL(x) PT_REGS_PARM4(x)
384 #define PT_REGS_PARM5_SYSCALL(x) PT_REGS_PARM5(x)
386 #ifndef PT_REGS_PARM1_CORE_SYSCALL
387 #define PT_REGS_PARM1_CORE_SYSCALL(x) PT_REGS_PARM1_CORE(x)
389 #define PT_REGS_PARM2_CORE_SYSCALL(x) PT_REGS_PARM2_CORE(x)
390 #define PT_REGS_PARM3_CORE_SYSCALL(x) PT_REGS_PARM3_CORE(x)
391 #ifndef PT_REGS_PARM4_CORE_SYSCALL
392 #define PT_REGS_PARM4_CORE_SYSCALL(x) PT_REGS_PARM4_CORE(x)
394 #define PT_REGS_PARM5_CORE_SYSCALL(x) PT_REGS_PARM5_CORE(x)
396 #else /* defined(bpf_target_defined) */
398 #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
399 #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
400 #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
401 #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
402 #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
403 #define PT_REGS_PARM6(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
404 #define PT_REGS_PARM7(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
405 #define PT_REGS_PARM8(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
406 #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
407 #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
408 #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
409 #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
410 #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
412 #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
413 #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
414 #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
415 #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
416 #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
417 #define PT_REGS_PARM6_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
418 #define PT_REGS_PARM7_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
419 #define PT_REGS_PARM8_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
420 #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
421 #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
422 #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
423 #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
424 #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
426 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
427 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
429 #define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
430 #define PT_REGS_PARM2_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
431 #define PT_REGS_PARM3_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
432 #define PT_REGS_PARM4_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
433 #define PT_REGS_PARM5_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
435 #define PT_REGS_PARM1_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
436 #define PT_REGS_PARM2_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
437 #define PT_REGS_PARM3_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
438 #define PT_REGS_PARM4_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
439 #define PT_REGS_PARM5_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
441 #endif /* defined(bpf_target_defined) */
444 * When invoked from a syscall handler kprobe, returns a pointer to a
445 * struct pt_regs containing syscall arguments and suitable for passing to
446 * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
448 #ifndef PT_REGS_SYSCALL_REGS
449 /* By default, assume that the arch selects ARCH_HAS_SYSCALL_WRAPPER. */
450 #define PT_REGS_SYSCALL_REGS(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
453 #ifndef ___bpf_concat
454 #define ___bpf_concat(a, b) a ## b
457 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
460 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
463 #define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
466 #define ___bpf_ctx_cast0() ctx
467 #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0]
468 #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1]
469 #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2]
470 #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3]
471 #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4]
472 #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5]
473 #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6]
474 #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7]
475 #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8]
476 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
477 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
478 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
479 #define ___bpf_ctx_cast(args...) ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
482 * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
483 * similar kinds of BPF programs, that accept input arguments as a single
484 * pointer to untyped u64 array, where each u64 can actually be a typed
485 * pointer or integer of different size. Instead of requring user to write
486 * manual casts and work with array elements by index, BPF_PROG macro
487 * allows user to declare a list of named and typed input arguments in the
488 * same syntax as for normal C function. All the casting is hidden and
489 * performed transparently, while user code can just assume working with
490 * function arguments of specified type and name.
492 * Original raw context argument is preserved as well as 'ctx' argument.
493 * This is useful when using BPF helpers that expect original context
494 * as one of the parameters (e.g., for bpf_perf_event_output()).
496 #define BPF_PROG(name, args...) \
497 name(unsigned long long *ctx); \
498 static __always_inline typeof(name(0)) \
499 ____##name(unsigned long long *ctx, ##args); \
500 typeof(name(0)) name(unsigned long long *ctx) \
502 _Pragma("GCC diagnostic push") \
503 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
504 return ____##name(___bpf_ctx_cast(args)); \
505 _Pragma("GCC diagnostic pop") \
507 static __always_inline typeof(name(0)) \
508 ____##name(unsigned long long *ctx, ##args)
511 #define ___bpf_nth2(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, \
512 _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, N, ...) N
515 #define ___bpf_narg2(...) \
516 ___bpf_nth2(_, ##__VA_ARGS__, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, \
517 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0)
520 #define ___bpf_treg_cnt(t) \
521 __builtin_choose_expr(sizeof(t) == 1, 1, \
522 __builtin_choose_expr(sizeof(t) == 2, 1, \
523 __builtin_choose_expr(sizeof(t) == 4, 1, \
524 __builtin_choose_expr(sizeof(t) == 8, 1, \
525 __builtin_choose_expr(sizeof(t) == 16, 2, \
528 #define ___bpf_reg_cnt0() (0)
529 #define ___bpf_reg_cnt1(t, x) (___bpf_reg_cnt0() + ___bpf_treg_cnt(t))
530 #define ___bpf_reg_cnt2(t, x, args...) (___bpf_reg_cnt1(args) + ___bpf_treg_cnt(t))
531 #define ___bpf_reg_cnt3(t, x, args...) (___bpf_reg_cnt2(args) + ___bpf_treg_cnt(t))
532 #define ___bpf_reg_cnt4(t, x, args...) (___bpf_reg_cnt3(args) + ___bpf_treg_cnt(t))
533 #define ___bpf_reg_cnt5(t, x, args...) (___bpf_reg_cnt4(args) + ___bpf_treg_cnt(t))
534 #define ___bpf_reg_cnt6(t, x, args...) (___bpf_reg_cnt5(args) + ___bpf_treg_cnt(t))
535 #define ___bpf_reg_cnt7(t, x, args...) (___bpf_reg_cnt6(args) + ___bpf_treg_cnt(t))
536 #define ___bpf_reg_cnt8(t, x, args...) (___bpf_reg_cnt7(args) + ___bpf_treg_cnt(t))
537 #define ___bpf_reg_cnt9(t, x, args...) (___bpf_reg_cnt8(args) + ___bpf_treg_cnt(t))
538 #define ___bpf_reg_cnt10(t, x, args...) (___bpf_reg_cnt9(args) + ___bpf_treg_cnt(t))
539 #define ___bpf_reg_cnt11(t, x, args...) (___bpf_reg_cnt10(args) + ___bpf_treg_cnt(t))
540 #define ___bpf_reg_cnt12(t, x, args...) (___bpf_reg_cnt11(args) + ___bpf_treg_cnt(t))
541 #define ___bpf_reg_cnt(args...) ___bpf_apply(___bpf_reg_cnt, ___bpf_narg2(args))(args)
543 #define ___bpf_union_arg(t, x, n) \
544 __builtin_choose_expr(sizeof(t) == 1, ({ union { __u8 z[1]; t x; } ___t = { .z = {ctx[n]}}; ___t.x; }), \
545 __builtin_choose_expr(sizeof(t) == 2, ({ union { __u16 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \
546 __builtin_choose_expr(sizeof(t) == 4, ({ union { __u32 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \
547 __builtin_choose_expr(sizeof(t) == 8, ({ union { __u64 z[1]; t x; } ___t = {.z = {ctx[n]} }; ___t.x; }), \
548 __builtin_choose_expr(sizeof(t) == 16, ({ union { __u64 z[2]; t x; } ___t = {.z = {ctx[n], ctx[n + 1]} }; ___t.x; }), \
551 #define ___bpf_ctx_arg0(n, args...)
552 #define ___bpf_ctx_arg1(n, t, x) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt1(t, x))
553 #define ___bpf_ctx_arg2(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt2(t, x, args)) ___bpf_ctx_arg1(n, args)
554 #define ___bpf_ctx_arg3(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt3(t, x, args)) ___bpf_ctx_arg2(n, args)
555 #define ___bpf_ctx_arg4(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt4(t, x, args)) ___bpf_ctx_arg3(n, args)
556 #define ___bpf_ctx_arg5(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt5(t, x, args)) ___bpf_ctx_arg4(n, args)
557 #define ___bpf_ctx_arg6(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt6(t, x, args)) ___bpf_ctx_arg5(n, args)
558 #define ___bpf_ctx_arg7(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt7(t, x, args)) ___bpf_ctx_arg6(n, args)
559 #define ___bpf_ctx_arg8(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt8(t, x, args)) ___bpf_ctx_arg7(n, args)
560 #define ___bpf_ctx_arg9(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt9(t, x, args)) ___bpf_ctx_arg8(n, args)
561 #define ___bpf_ctx_arg10(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt10(t, x, args)) ___bpf_ctx_arg9(n, args)
562 #define ___bpf_ctx_arg11(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt11(t, x, args)) ___bpf_ctx_arg10(n, args)
563 #define ___bpf_ctx_arg12(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt12(t, x, args)) ___bpf_ctx_arg11(n, args)
564 #define ___bpf_ctx_arg(args...) ___bpf_apply(___bpf_ctx_arg, ___bpf_narg2(args))(___bpf_reg_cnt(args), args)
566 #define ___bpf_ctx_decl0()
567 #define ___bpf_ctx_decl1(t, x) , t x
568 #define ___bpf_ctx_decl2(t, x, args...) , t x ___bpf_ctx_decl1(args)
569 #define ___bpf_ctx_decl3(t, x, args...) , t x ___bpf_ctx_decl2(args)
570 #define ___bpf_ctx_decl4(t, x, args...) , t x ___bpf_ctx_decl3(args)
571 #define ___bpf_ctx_decl5(t, x, args...) , t x ___bpf_ctx_decl4(args)
572 #define ___bpf_ctx_decl6(t, x, args...) , t x ___bpf_ctx_decl5(args)
573 #define ___bpf_ctx_decl7(t, x, args...) , t x ___bpf_ctx_decl6(args)
574 #define ___bpf_ctx_decl8(t, x, args...) , t x ___bpf_ctx_decl7(args)
575 #define ___bpf_ctx_decl9(t, x, args...) , t x ___bpf_ctx_decl8(args)
576 #define ___bpf_ctx_decl10(t, x, args...) , t x ___bpf_ctx_decl9(args)
577 #define ___bpf_ctx_decl11(t, x, args...) , t x ___bpf_ctx_decl10(args)
578 #define ___bpf_ctx_decl12(t, x, args...) , t x ___bpf_ctx_decl11(args)
579 #define ___bpf_ctx_decl(args...) ___bpf_apply(___bpf_ctx_decl, ___bpf_narg2(args))(args)
582 * BPF_PROG2 is an enhanced version of BPF_PROG in order to handle struct
583 * arguments. Since each struct argument might take one or two u64 values
584 * in the trampoline stack, argument type size is needed to place proper number
585 * of u64 values for each argument. Therefore, BPF_PROG2 has different
586 * syntax from BPF_PROG. For example, for the following BPF_PROG syntax:
588 * int BPF_PROG(test2, int a, int b) { ... }
590 * the corresponding BPF_PROG2 syntax is:
592 * int BPF_PROG2(test2, int, a, int, b) { ... }
594 * where type and the corresponding argument name are separated by comma.
596 * Use BPF_PROG2 macro if one of the arguments might be a struct/union larger
599 * int BPF_PROG2(test_struct_arg, struct bpf_testmod_struct_arg_1, a, int, b,
600 * int, c, int, d, struct bpf_testmod_struct_arg_2, e, int, ret)
602 * // access a, b, c, d, e, and ret directly
606 #define BPF_PROG2(name, args...) \
607 name(unsigned long long *ctx); \
608 static __always_inline typeof(name(0)) \
609 ____##name(unsigned long long *ctx ___bpf_ctx_decl(args)); \
610 typeof(name(0)) name(unsigned long long *ctx) \
612 return ____##name(ctx ___bpf_ctx_arg(args)); \
614 static __always_inline typeof(name(0)) \
615 ____##name(unsigned long long *ctx ___bpf_ctx_decl(args))
619 #define ___bpf_kprobe_args0() ctx
620 #define ___bpf_kprobe_args1(x) ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx)
621 #define ___bpf_kprobe_args2(x, args...) ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx)
622 #define ___bpf_kprobe_args3(x, args...) ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx)
623 #define ___bpf_kprobe_args4(x, args...) ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx)
624 #define ___bpf_kprobe_args5(x, args...) ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx)
625 #define ___bpf_kprobe_args6(x, args...) ___bpf_kprobe_args5(args), (void *)PT_REGS_PARM6(ctx)
626 #define ___bpf_kprobe_args7(x, args...) ___bpf_kprobe_args6(args), (void *)PT_REGS_PARM7(ctx)
627 #define ___bpf_kprobe_args8(x, args...) ___bpf_kprobe_args7(args), (void *)PT_REGS_PARM8(ctx)
628 #define ___bpf_kprobe_args(args...) ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args)
631 * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for
632 * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific
633 * low-level way of getting kprobe input arguments from struct pt_regs, and
634 * provides a familiar typed and named function arguments syntax and
635 * semantics of accessing kprobe input paremeters.
637 * Original struct pt_regs* context is preserved as 'ctx' argument. This might
638 * be necessary when using BPF helpers like bpf_perf_event_output().
640 #define BPF_KPROBE(name, args...) \
641 name(struct pt_regs *ctx); \
642 static __always_inline typeof(name(0)) \
643 ____##name(struct pt_regs *ctx, ##args); \
644 typeof(name(0)) name(struct pt_regs *ctx) \
646 _Pragma("GCC diagnostic push") \
647 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
648 return ____##name(___bpf_kprobe_args(args)); \
649 _Pragma("GCC diagnostic pop") \
651 static __always_inline typeof(name(0)) \
652 ____##name(struct pt_regs *ctx, ##args)
654 #define ___bpf_kretprobe_args0() ctx
655 #define ___bpf_kretprobe_args1(x) ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx)
656 #define ___bpf_kretprobe_args(args...) ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args)
659 * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional
660 * return value (in addition to `struct pt_regs *ctx`), but no input
661 * arguments, because they will be clobbered by the time probed function
664 #define BPF_KRETPROBE(name, args...) \
665 name(struct pt_regs *ctx); \
666 static __always_inline typeof(name(0)) \
667 ____##name(struct pt_regs *ctx, ##args); \
668 typeof(name(0)) name(struct pt_regs *ctx) \
670 _Pragma("GCC diagnostic push") \
671 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
672 return ____##name(___bpf_kretprobe_args(args)); \
673 _Pragma("GCC diagnostic pop") \
675 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
677 /* If kernel has CONFIG_ARCH_HAS_SYSCALL_WRAPPER, read pt_regs directly */
678 #define ___bpf_syscall_args0() ctx
679 #define ___bpf_syscall_args1(x) ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_SYSCALL(regs)
680 #define ___bpf_syscall_args2(x, args...) ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_SYSCALL(regs)
681 #define ___bpf_syscall_args3(x, args...) ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_SYSCALL(regs)
682 #define ___bpf_syscall_args4(x, args...) ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_SYSCALL(regs)
683 #define ___bpf_syscall_args5(x, args...) ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_SYSCALL(regs)
684 #define ___bpf_syscall_args(args...) ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args)
686 /* If kernel doesn't have CONFIG_ARCH_HAS_SYSCALL_WRAPPER, we have to BPF_CORE_READ from pt_regs */
687 #define ___bpf_syswrap_args0() ctx
688 #define ___bpf_syswrap_args1(x) ___bpf_syswrap_args0(), (void *)PT_REGS_PARM1_CORE_SYSCALL(regs)
689 #define ___bpf_syswrap_args2(x, args...) ___bpf_syswrap_args1(args), (void *)PT_REGS_PARM2_CORE_SYSCALL(regs)
690 #define ___bpf_syswrap_args3(x, args...) ___bpf_syswrap_args2(args), (void *)PT_REGS_PARM3_CORE_SYSCALL(regs)
691 #define ___bpf_syswrap_args4(x, args...) ___bpf_syswrap_args3(args), (void *)PT_REGS_PARM4_CORE_SYSCALL(regs)
692 #define ___bpf_syswrap_args5(x, args...) ___bpf_syswrap_args4(args), (void *)PT_REGS_PARM5_CORE_SYSCALL(regs)
693 #define ___bpf_syswrap_args(args...) ___bpf_apply(___bpf_syswrap_args, ___bpf_narg(args))(args)
696 * BPF_KSYSCALL is a variant of BPF_KPROBE, which is intended for
697 * tracing syscall functions, like __x64_sys_close. It hides the underlying
698 * platform-specific low-level way of getting syscall input arguments from
699 * struct pt_regs, and provides a familiar typed and named function arguments
700 * syntax and semantics of accessing syscall input parameters.
702 * Original struct pt_regs * context is preserved as 'ctx' argument. This might
703 * be necessary when using BPF helpers like bpf_perf_event_output().
705 * At the moment BPF_KSYSCALL does not transparently handle all the calling
706 * convention quirks for the following syscalls:
708 * - mmap(): __ARCH_WANT_SYS_OLD_MMAP.
709 * - clone(): CONFIG_CLONE_BACKWARDS, CONFIG_CLONE_BACKWARDS2 and
710 * CONFIG_CLONE_BACKWARDS3.
711 * - socket-related syscalls: __ARCH_WANT_SYS_SOCKETCALL.
714 * This may or may not change in the future. User needs to take extra measures
715 * to handle such quirks explicitly, if necessary.
717 * This macro relies on BPF CO-RE support and virtual __kconfig externs.
719 #define BPF_KSYSCALL(name, args...) \
720 name(struct pt_regs *ctx); \
721 extern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig; \
722 static __always_inline typeof(name(0)) \
723 ____##name(struct pt_regs *ctx, ##args); \
724 typeof(name(0)) name(struct pt_regs *ctx) \
726 struct pt_regs *regs = LINUX_HAS_SYSCALL_WRAPPER \
727 ? (struct pt_regs *)PT_REGS_PARM1(ctx) \
729 _Pragma("GCC diagnostic push") \
730 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
731 if (LINUX_HAS_SYSCALL_WRAPPER) \
732 return ____##name(___bpf_syswrap_args(args)); \
734 return ____##name(___bpf_syscall_args(args)); \
735 _Pragma("GCC diagnostic pop") \
737 static __always_inline typeof(name(0)) \
738 ____##name(struct pt_regs *ctx, ##args)
740 #define BPF_KPROBE_SYSCALL BPF_KSYSCALL