4d04c63fda27c1248b026ed155d30716f2929c79
[linux-block.git] / tools / lib / bpf / bpf_tracing.h
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_TRACING_H__
3 #define __BPF_TRACING_H__
4
5 #include <bpf/bpf_helpers.h>
6
7 /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
8 #if defined(__TARGET_ARCH_x86)
9         #define bpf_target_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
38 #else
39
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 */
72
73 #endif
74
75 #ifndef __BPF_TARGET_MISSING
76 #define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\""
77 #endif
78
79 #if defined(bpf_target_x86)
80
81 /*
82  * https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI
83  */
84
85 #if defined(__KERNEL__) || defined(__VMLINUX_H__)
86
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 /*
94  * Syscall uses r10 for PARM4. See arch/x86/entry/entry_64.S:entry_SYSCALL_64
95  * comments in Linux sources. And refer to syscall(2) manpage.
96  */
97 #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
98 #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
99 #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
100 #define __PT_PARM4_SYSCALL_REG r10
101 #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
102 #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
103
104 #define __PT_RET_REG sp
105 #define __PT_FP_REG bp
106 #define __PT_RC_REG ax
107 #define __PT_SP_REG sp
108 #define __PT_IP_REG ip
109
110 #else
111
112 #ifdef __i386__
113
114 /* i386 kernel is built with -mregparm=3 */
115 #define __PT_PARM1_REG eax
116 #define __PT_PARM2_REG edx
117 #define __PT_PARM3_REG ecx
118 /* i386 syscall ABI is very different, refer to syscall(2) manpage */
119 #define __PT_PARM1_SYSCALL_REG ebx
120 #define __PT_PARM2_SYSCALL_REG ecx
121 #define __PT_PARM3_SYSCALL_REG edx
122 #define __PT_PARM4_SYSCALL_REG esi
123 #define __PT_PARM5_SYSCALL_REG edi
124 #define __PT_PARM6_SYSCALL_REG ebp
125
126 #define __PT_RET_REG esp
127 #define __PT_FP_REG ebp
128 #define __PT_RC_REG eax
129 #define __PT_SP_REG esp
130 #define __PT_IP_REG eip
131
132 #else /* __i386__ */
133
134 #define __PT_PARM1_REG rdi
135 #define __PT_PARM2_REG rsi
136 #define __PT_PARM3_REG rdx
137 #define __PT_PARM4_REG rcx
138 #define __PT_PARM5_REG r8
139 #define __PT_PARM6_REG r9
140
141 #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
142 #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
143 #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
144 #define __PT_PARM4_SYSCALL_REG r10
145 #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
146 #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
147
148 #define __PT_RET_REG rsp
149 #define __PT_FP_REG rbp
150 #define __PT_RC_REG rax
151 #define __PT_SP_REG rsp
152 #define __PT_IP_REG rip
153
154 #endif /* __i386__ */
155
156 #endif /* __KERNEL__ || __VMLINUX_H__ */
157
158 #elif defined(bpf_target_s390)
159
160 struct pt_regs___s390 {
161         unsigned long orig_gpr2;
162 };
163
164 /* s390 provides user_pt_regs instead of struct pt_regs to userspace */
165 #define __PT_REGS_CAST(x) ((const user_pt_regs *)(x))
166 #define __PT_PARM1_REG gprs[2]
167 #define __PT_PARM2_REG gprs[3]
168 #define __PT_PARM3_REG gprs[4]
169 #define __PT_PARM4_REG gprs[5]
170 #define __PT_PARM5_REG gprs[6]
171 #define __PT_RET_REG gprs[14]
172 #define __PT_FP_REG gprs[11]    /* Works only with CONFIG_FRAME_POINTER */
173 #define __PT_RC_REG gprs[2]
174 #define __PT_SP_REG gprs[15]
175 #define __PT_IP_REG psw.addr
176 #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
177 #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___s390 *)(x), orig_gpr2)
178
179 #elif defined(bpf_target_arm)
180
181 /*
182  * https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers
183  */
184
185 #define __PT_PARM1_REG uregs[0]
186 #define __PT_PARM2_REG uregs[1]
187 #define __PT_PARM3_REG uregs[2]
188 #define __PT_PARM4_REG uregs[3]
189 #define __PT_RET_REG uregs[14]
190 #define __PT_FP_REG uregs[11]   /* Works only with CONFIG_FRAME_POINTER */
191 #define __PT_RC_REG uregs[0]
192 #define __PT_SP_REG uregs[13]
193 #define __PT_IP_REG uregs[12]
194
195 #elif defined(bpf_target_arm64)
196
197 /*
198  * https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers
199  */
200
201 struct pt_regs___arm64 {
202         unsigned long orig_x0;
203 };
204
205 /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
206 #define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
207 #define __PT_PARM1_REG regs[0]
208 #define __PT_PARM2_REG regs[1]
209 #define __PT_PARM3_REG regs[2]
210 #define __PT_PARM4_REG regs[3]
211 #define __PT_PARM5_REG regs[4]
212 #define __PT_PARM6_REG regs[5]
213 #define __PT_PARM7_REG regs[6]
214 #define __PT_PARM8_REG regs[7]
215 #define __PT_RET_REG regs[30]
216 #define __PT_FP_REG regs[29]    /* Works only with CONFIG_FRAME_POINTER */
217 #define __PT_RC_REG regs[0]
218 #define __PT_SP_REG sp
219 #define __PT_IP_REG pc
220 #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
221 #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___arm64 *)(x), orig_x0)
222
223 #elif defined(bpf_target_mips)
224
225 /*
226  * N64 ABI is assumed right now.
227  * https://en.wikipedia.org/wiki/MIPS_architecture#Calling_conventions
228  */
229
230 #define __PT_PARM1_REG regs[4]
231 #define __PT_PARM2_REG regs[5]
232 #define __PT_PARM3_REG regs[6]
233 #define __PT_PARM4_REG regs[7]
234 #define __PT_PARM5_REG regs[8]
235 #define __PT_PARM6_REG regs[9]
236 #define __PT_PARM7_REG regs[10]
237 #define __PT_PARM8_REG regs[11]
238 #define __PT_RET_REG regs[31]
239 #define __PT_FP_REG regs[30]    /* Works only with CONFIG_FRAME_POINTER */
240 #define __PT_RC_REG regs[2]
241 #define __PT_SP_REG regs[29]
242 #define __PT_IP_REG cp0_epc
243
244 #elif defined(bpf_target_powerpc)
245
246 /*
247  * http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf (page 3-14,
248  * section "Function Calling Sequence")
249  */
250
251 #define __PT_PARM1_REG gpr[3]
252 #define __PT_PARM2_REG gpr[4]
253 #define __PT_PARM3_REG gpr[5]
254 #define __PT_PARM4_REG gpr[6]
255 #define __PT_PARM5_REG gpr[7]
256 #define __PT_PARM6_REG gpr[8]
257 #define __PT_PARM7_REG gpr[9]
258 #define __PT_PARM8_REG gpr[10]
259 #define __PT_RET_REG regs[31]
260 #define __PT_FP_REG __unsupported__
261 #define __PT_RC_REG gpr[3]
262 #define __PT_SP_REG sp
263 #define __PT_IP_REG nip
264 /* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER. */
265 #define PT_REGS_SYSCALL_REGS(ctx) ctx
266
267 #elif defined(bpf_target_sparc)
268
269 /*
270  * https://en.wikipedia.org/wiki/Calling_convention#SPARC
271  */
272
273 #define __PT_PARM1_REG u_regs[UREG_I0]
274 #define __PT_PARM2_REG u_regs[UREG_I1]
275 #define __PT_PARM3_REG u_regs[UREG_I2]
276 #define __PT_PARM4_REG u_regs[UREG_I3]
277 #define __PT_PARM5_REG u_regs[UREG_I4]
278 #define __PT_PARM6_REG u_regs[UREG_I5]
279 #define __PT_RET_REG u_regs[UREG_I7]
280 #define __PT_FP_REG __unsupported__
281 #define __PT_RC_REG u_regs[UREG_I0]
282 #define __PT_SP_REG u_regs[UREG_FP]
283 /* Should this also be a bpf_target check for the sparc case? */
284 #if defined(__arch64__)
285 #define __PT_IP_REG tpc
286 #else
287 #define __PT_IP_REG pc
288 #endif
289
290 #elif defined(bpf_target_riscv)
291
292 /*
293  * https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#risc-v-calling-conventions
294  */
295
296 #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
297 #define __PT_PARM1_REG a0
298 #define __PT_PARM2_REG a1
299 #define __PT_PARM3_REG a2
300 #define __PT_PARM4_REG a3
301 #define __PT_PARM5_REG a4
302 #define __PT_PARM6_REG a5
303 #define __PT_PARM7_REG a6
304 #define __PT_PARM8_REG a7
305 #define __PT_RET_REG ra
306 #define __PT_FP_REG s0
307 #define __PT_RC_REG a0
308 #define __PT_SP_REG sp
309 #define __PT_IP_REG pc
310 /* riscv does not select ARCH_HAS_SYSCALL_WRAPPER. */
311 #define PT_REGS_SYSCALL_REGS(ctx) ctx
312
313 #elif defined(bpf_target_arc)
314
315 /*
316  * Section "Function Calling Sequence" (page 24):
317  * https://raw.githubusercontent.com/wiki/foss-for-synopsys-dwc-arc-processors/toolchain/files/ARCv2_ABI.pdf
318  */
319
320 /* arc provides struct user_pt_regs instead of struct pt_regs to userspace */
321 #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
322 #define __PT_PARM1_REG scratch.r0
323 #define __PT_PARM2_REG scratch.r1
324 #define __PT_PARM3_REG scratch.r2
325 #define __PT_PARM4_REG scratch.r3
326 #define __PT_PARM5_REG scratch.r4
327 #define __PT_PARM6_REG scratch.r5
328 #define __PT_PARM7_REG scratch.r6
329 #define __PT_PARM8_REG scratch.r7
330 #define __PT_RET_REG scratch.blink
331 #define __PT_FP_REG scratch.fp
332 #define __PT_RC_REG scratch.r0
333 #define __PT_SP_REG scratch.sp
334 #define __PT_IP_REG scratch.ret
335 /* arc does not select ARCH_HAS_SYSCALL_WRAPPER. */
336 #define PT_REGS_SYSCALL_REGS(ctx) ctx
337
338 #elif defined(bpf_target_loongarch)
339
340 /*
341  * https://docs.kernel.org/loongarch/introduction.html
342  * https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html
343  */
344
345 #define __PT_PARM1_REG regs[4]
346 #define __PT_PARM2_REG regs[5]
347 #define __PT_PARM3_REG regs[6]
348 #define __PT_PARM4_REG regs[7]
349 #define __PT_PARM5_REG regs[8]
350 #define __PT_PARM6_REG regs[9]
351 #define __PT_PARM7_REG regs[10]
352 #define __PT_PARM8_REG regs[11]
353 #define __PT_RET_REG regs[1]
354 #define __PT_FP_REG regs[22]
355 #define __PT_RC_REG regs[4]
356 #define __PT_SP_REG regs[3]
357 #define __PT_IP_REG csr_era
358 /* loongarch does not select ARCH_HAS_SYSCALL_WRAPPER. */
359 #define PT_REGS_SYSCALL_REGS(ctx) ctx
360
361 #endif
362
363 #if defined(bpf_target_defined)
364
365 struct pt_regs;
366
367 /* allow some architectures to override `struct pt_regs` */
368 #ifndef __PT_REGS_CAST
369 #define __PT_REGS_CAST(x) (x)
370 #endif
371
372 /*
373  * Different architectures support different number of arguments passed
374  * through registers. i386 supports just 3, some arches support up to 8.
375  */
376 #ifndef __PT_PARM4_REG
377 #define __PT_PARM4_REG __unsupported__
378 #endif
379 #ifndef __PT_PARM5_REG
380 #define __PT_PARM5_REG __unsupported__
381 #endif
382 #ifndef __PT_PARM6_REG
383 #define __PT_PARM6_REG __unsupported__
384 #endif
385 #ifndef __PT_PARM7_REG
386 #define __PT_PARM7_REG __unsupported__
387 #endif
388 #ifndef __PT_PARM8_REG
389 #define __PT_PARM8_REG __unsupported__
390 #endif
391 /*
392  * Similarly, syscall-specific conventions might differ between function call
393  * conventions within each architecutre. All supported architectures pass
394  * either 6 or 7 syscall arguments in registers.
395  *
396  * See syscall(2) manpage for succinct table with information on each arch.
397  */
398 #ifndef __PT_PARM1_SYSCALL_REG
399 #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
400 #endif
401 #ifndef __PT_PARM2_SYSCALL_REG
402 #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
403 #endif
404 #ifndef __PT_PARM3_SYSCALL_REG
405 #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
406 #endif
407 #ifndef __PT_PARM4_SYSCALL_REG
408 #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
409 #endif
410 #ifndef __PT_PARM5_SYSCALL_REG
411 #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
412 #endif
413 #ifndef __PT_PARM6_SYSCALL_REG
414 #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
415 #endif
416 #ifndef __PT_PARM7_SYSCALL_REG
417 #define __PT_PARM7_SYSCALL_REG __unsupported__
418 #endif
419
420 #define PT_REGS_PARM1(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG)
421 #define PT_REGS_PARM2(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG)
422 #define PT_REGS_PARM3(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG)
423 #define PT_REGS_PARM4(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG)
424 #define PT_REGS_PARM5(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG)
425 #define PT_REGS_PARM6(x) (__PT_REGS_CAST(x)->__PT_PARM6_REG)
426 #define PT_REGS_PARM7(x) (__PT_REGS_CAST(x)->__PT_PARM7_REG)
427 #define PT_REGS_PARM8(x) (__PT_REGS_CAST(x)->__PT_PARM8_REG)
428 #define PT_REGS_RET(x) (__PT_REGS_CAST(x)->__PT_RET_REG)
429 #define PT_REGS_FP(x) (__PT_REGS_CAST(x)->__PT_FP_REG)
430 #define PT_REGS_RC(x) (__PT_REGS_CAST(x)->__PT_RC_REG)
431 #define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG)
432 #define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG)
433
434 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG)
435 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG)
436 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG)
437 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG)
438 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG)
439 #define PT_REGS_PARM6_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_REG)
440 #define PT_REGS_PARM7_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_REG)
441 #define PT_REGS_PARM8_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM8_REG)
442 #define PT_REGS_RET_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RET_REG)
443 #define PT_REGS_FP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_FP_REG)
444 #define PT_REGS_RC_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RC_REG)
445 #define PT_REGS_SP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_SP_REG)
446 #define PT_REGS_IP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_IP_REG)
447
448 #if defined(bpf_target_powerpc)
449
450 #define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({ (ip) = (ctx)->link; })
451 #define BPF_KRETPROBE_READ_RET_IP               BPF_KPROBE_READ_RET_IP
452
453 #elif defined(bpf_target_sparc)
454
455 #define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({ (ip) = PT_REGS_RET(ctx); })
456 #define BPF_KRETPROBE_READ_RET_IP               BPF_KPROBE_READ_RET_IP
457
458 #else
459
460 #define BPF_KPROBE_READ_RET_IP(ip, ctx)                                     \
461         ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
462 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx)                                  \
463         ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
464
465 #endif
466
467 #ifndef PT_REGS_PARM1_SYSCALL
468 #define PT_REGS_PARM1_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM1_SYSCALL_REG)
469 #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_SYSCALL_REG)
470 #endif
471 #ifndef PT_REGS_PARM2_SYSCALL
472 #define PT_REGS_PARM2_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM2_SYSCALL_REG)
473 #define PT_REGS_PARM2_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_SYSCALL_REG)
474 #endif
475 #ifndef PT_REGS_PARM3_SYSCALL
476 #define PT_REGS_PARM3_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM3_SYSCALL_REG)
477 #define PT_REGS_PARM3_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_SYSCALL_REG)
478 #endif
479 #ifndef PT_REGS_PARM4_SYSCALL
480 #define PT_REGS_PARM4_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM4_SYSCALL_REG)
481 #define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_SYSCALL_REG)
482 #endif
483 #ifndef PT_REGS_PARM5_SYSCALL
484 #define PT_REGS_PARM5_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM5_SYSCALL_REG)
485 #define PT_REGS_PARM5_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_SYSCALL_REG)
486 #endif
487 #ifndef PT_REGS_PARM6_SYSCALL
488 #define PT_REGS_PARM6_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM6_SYSCALL_REG)
489 #define PT_REGS_PARM6_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_SYSCALL_REG)
490 #endif
491 #ifndef PT_REGS_PARM7_SYSCALL
492 #define PT_REGS_PARM7_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM7_SYSCALL_REG)
493 #define PT_REGS_PARM7_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_SYSCALL_REG)
494 #endif
495
496 #else /* defined(bpf_target_defined) */
497
498 #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
499 #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
500 #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
501 #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
502 #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
503 #define PT_REGS_PARM6(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
504 #define PT_REGS_PARM7(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
505 #define PT_REGS_PARM8(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
506 #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
507 #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
508 #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
509 #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
510 #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
511
512 #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
513 #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
514 #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
515 #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
516 #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
517 #define PT_REGS_PARM6_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
518 #define PT_REGS_PARM7_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
519 #define PT_REGS_PARM8_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
520 #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
521 #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
522 #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
523 #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
524 #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
525
526 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
527 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
528
529 #define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
530 #define PT_REGS_PARM2_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
531 #define PT_REGS_PARM3_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
532 #define PT_REGS_PARM4_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
533 #define PT_REGS_PARM5_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
534 #define PT_REGS_PARM6_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
535 #define PT_REGS_PARM7_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
536
537 #define PT_REGS_PARM1_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
538 #define PT_REGS_PARM2_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
539 #define PT_REGS_PARM3_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
540 #define PT_REGS_PARM4_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
541 #define PT_REGS_PARM5_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
542 #define PT_REGS_PARM6_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
543 #define PT_REGS_PARM7_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
544
545 #endif /* defined(bpf_target_defined) */
546
547 /*
548  * When invoked from a syscall handler kprobe, returns a pointer to a
549  * struct pt_regs containing syscall arguments and suitable for passing to
550  * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
551  */
552 #ifndef PT_REGS_SYSCALL_REGS
553 /* By default, assume that the arch selects ARCH_HAS_SYSCALL_WRAPPER. */
554 #define PT_REGS_SYSCALL_REGS(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
555 #endif
556
557 #ifndef ___bpf_concat
558 #define ___bpf_concat(a, b) a ## b
559 #endif
560 #ifndef ___bpf_apply
561 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
562 #endif
563 #ifndef ___bpf_nth
564 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
565 #endif
566 #ifndef ___bpf_narg
567 #define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
568 #endif
569
570 #define ___bpf_ctx_cast0()            ctx
571 #define ___bpf_ctx_cast1(x)           ___bpf_ctx_cast0(), (void *)ctx[0]
572 #define ___bpf_ctx_cast2(x, args...)  ___bpf_ctx_cast1(args), (void *)ctx[1]
573 #define ___bpf_ctx_cast3(x, args...)  ___bpf_ctx_cast2(args), (void *)ctx[2]
574 #define ___bpf_ctx_cast4(x, args...)  ___bpf_ctx_cast3(args), (void *)ctx[3]
575 #define ___bpf_ctx_cast5(x, args...)  ___bpf_ctx_cast4(args), (void *)ctx[4]
576 #define ___bpf_ctx_cast6(x, args...)  ___bpf_ctx_cast5(args), (void *)ctx[5]
577 #define ___bpf_ctx_cast7(x, args...)  ___bpf_ctx_cast6(args), (void *)ctx[6]
578 #define ___bpf_ctx_cast8(x, args...)  ___bpf_ctx_cast7(args), (void *)ctx[7]
579 #define ___bpf_ctx_cast9(x, args...)  ___bpf_ctx_cast8(args), (void *)ctx[8]
580 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
581 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
582 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
583 #define ___bpf_ctx_cast(args...)      ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
584
585 /*
586  * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
587  * similar kinds of BPF programs, that accept input arguments as a single
588  * pointer to untyped u64 array, where each u64 can actually be a typed
589  * pointer or integer of different size. Instead of requring user to write
590  * manual casts and work with array elements by index, BPF_PROG macro
591  * allows user to declare a list of named and typed input arguments in the
592  * same syntax as for normal C function. All the casting is hidden and
593  * performed transparently, while user code can just assume working with
594  * function arguments of specified type and name.
595  *
596  * Original raw context argument is preserved as well as 'ctx' argument.
597  * This is useful when using BPF helpers that expect original context
598  * as one of the parameters (e.g., for bpf_perf_event_output()).
599  */
600 #define BPF_PROG(name, args...)                                             \
601 name(unsigned long long *ctx);                                              \
602 static __always_inline typeof(name(0))                                      \
603 ____##name(unsigned long long *ctx, ##args);                                \
604 typeof(name(0)) name(unsigned long long *ctx)                               \
605 {                                                                           \
606         _Pragma("GCC diagnostic push")                                      \
607         _Pragma("GCC diagnostic ignored \"-Wint-conversion\"")              \
608         return ____##name(___bpf_ctx_cast(args));                           \
609         _Pragma("GCC diagnostic pop")                                       \
610 }                                                                           \
611 static __always_inline typeof(name(0))                                      \
612 ____##name(unsigned long long *ctx, ##args)
613
614 #ifndef ___bpf_nth2
615 #define ___bpf_nth2(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13,  \
616                     _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, N, ...) N
617 #endif
618 #ifndef ___bpf_narg2
619 #define ___bpf_narg2(...)       \
620         ___bpf_nth2(_, ##__VA_ARGS__, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, \
621                     6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0)
622 #endif
623
624 #define ___bpf_treg_cnt(t) \
625         __builtin_choose_expr(sizeof(t) == 1, 1,        \
626         __builtin_choose_expr(sizeof(t) == 2, 1,        \
627         __builtin_choose_expr(sizeof(t) == 4, 1,        \
628         __builtin_choose_expr(sizeof(t) == 8, 1,        \
629         __builtin_choose_expr(sizeof(t) == 16, 2,       \
630                               (void)0)))))
631
632 #define ___bpf_reg_cnt0()               (0)
633 #define ___bpf_reg_cnt1(t, x)           (___bpf_reg_cnt0() + ___bpf_treg_cnt(t))
634 #define ___bpf_reg_cnt2(t, x, args...)  (___bpf_reg_cnt1(args) + ___bpf_treg_cnt(t))
635 #define ___bpf_reg_cnt3(t, x, args...)  (___bpf_reg_cnt2(args) + ___bpf_treg_cnt(t))
636 #define ___bpf_reg_cnt4(t, x, args...)  (___bpf_reg_cnt3(args) + ___bpf_treg_cnt(t))
637 #define ___bpf_reg_cnt5(t, x, args...)  (___bpf_reg_cnt4(args) + ___bpf_treg_cnt(t))
638 #define ___bpf_reg_cnt6(t, x, args...)  (___bpf_reg_cnt5(args) + ___bpf_treg_cnt(t))
639 #define ___bpf_reg_cnt7(t, x, args...)  (___bpf_reg_cnt6(args) + ___bpf_treg_cnt(t))
640 #define ___bpf_reg_cnt8(t, x, args...)  (___bpf_reg_cnt7(args) + ___bpf_treg_cnt(t))
641 #define ___bpf_reg_cnt9(t, x, args...)  (___bpf_reg_cnt8(args) + ___bpf_treg_cnt(t))
642 #define ___bpf_reg_cnt10(t, x, args...) (___bpf_reg_cnt9(args) + ___bpf_treg_cnt(t))
643 #define ___bpf_reg_cnt11(t, x, args...) (___bpf_reg_cnt10(args) + ___bpf_treg_cnt(t))
644 #define ___bpf_reg_cnt12(t, x, args...) (___bpf_reg_cnt11(args) + ___bpf_treg_cnt(t))
645 #define ___bpf_reg_cnt(args...)  ___bpf_apply(___bpf_reg_cnt, ___bpf_narg2(args))(args)
646
647 #define ___bpf_union_arg(t, x, n) \
648         __builtin_choose_expr(sizeof(t) == 1, ({ union { __u8 z[1]; t x; } ___t = { .z = {ctx[n]}}; ___t.x; }), \
649         __builtin_choose_expr(sizeof(t) == 2, ({ union { __u16 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \
650         __builtin_choose_expr(sizeof(t) == 4, ({ union { __u32 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \
651         __builtin_choose_expr(sizeof(t) == 8, ({ union { __u64 z[1]; t x; } ___t = {.z = {ctx[n]} }; ___t.x; }), \
652         __builtin_choose_expr(sizeof(t) == 16, ({ union { __u64 z[2]; t x; } ___t = {.z = {ctx[n], ctx[n + 1]} }; ___t.x; }), \
653                               (void)0)))))
654
655 #define ___bpf_ctx_arg0(n, args...)
656 #define ___bpf_ctx_arg1(n, t, x)                , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt1(t, x))
657 #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)
658 #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)
659 #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)
660 #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)
661 #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)
662 #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)
663 #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)
664 #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)
665 #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)
666 #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)
667 #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)
668 #define ___bpf_ctx_arg(args...) ___bpf_apply(___bpf_ctx_arg, ___bpf_narg2(args))(___bpf_reg_cnt(args), args)
669
670 #define ___bpf_ctx_decl0()
671 #define ___bpf_ctx_decl1(t, x)                  , t x
672 #define ___bpf_ctx_decl2(t, x, args...)         , t x ___bpf_ctx_decl1(args)
673 #define ___bpf_ctx_decl3(t, x, args...)         , t x ___bpf_ctx_decl2(args)
674 #define ___bpf_ctx_decl4(t, x, args...)         , t x ___bpf_ctx_decl3(args)
675 #define ___bpf_ctx_decl5(t, x, args...)         , t x ___bpf_ctx_decl4(args)
676 #define ___bpf_ctx_decl6(t, x, args...)         , t x ___bpf_ctx_decl5(args)
677 #define ___bpf_ctx_decl7(t, x, args...)         , t x ___bpf_ctx_decl6(args)
678 #define ___bpf_ctx_decl8(t, x, args...)         , t x ___bpf_ctx_decl7(args)
679 #define ___bpf_ctx_decl9(t, x, args...)         , t x ___bpf_ctx_decl8(args)
680 #define ___bpf_ctx_decl10(t, x, args...)        , t x ___bpf_ctx_decl9(args)
681 #define ___bpf_ctx_decl11(t, x, args...)        , t x ___bpf_ctx_decl10(args)
682 #define ___bpf_ctx_decl12(t, x, args...)        , t x ___bpf_ctx_decl11(args)
683 #define ___bpf_ctx_decl(args...)        ___bpf_apply(___bpf_ctx_decl, ___bpf_narg2(args))(args)
684
685 /*
686  * BPF_PROG2 is an enhanced version of BPF_PROG in order to handle struct
687  * arguments. Since each struct argument might take one or two u64 values
688  * in the trampoline stack, argument type size is needed to place proper number
689  * of u64 values for each argument. Therefore, BPF_PROG2 has different
690  * syntax from BPF_PROG. For example, for the following BPF_PROG syntax:
691  *
692  *   int BPF_PROG(test2, int a, int b) { ... }
693  *
694  * the corresponding BPF_PROG2 syntax is:
695  *
696  *   int BPF_PROG2(test2, int, a, int, b) { ... }
697  *
698  * where type and the corresponding argument name are separated by comma.
699  *
700  * Use BPF_PROG2 macro if one of the arguments might be a struct/union larger
701  * than 8 bytes:
702  *
703  *   int BPF_PROG2(test_struct_arg, struct bpf_testmod_struct_arg_1, a, int, b,
704  *                 int, c, int, d, struct bpf_testmod_struct_arg_2, e, int, ret)
705  *   {
706  *        // access a, b, c, d, e, and ret directly
707  *        ...
708  *   }
709  */
710 #define BPF_PROG2(name, args...)                                                \
711 name(unsigned long long *ctx);                                                  \
712 static __always_inline typeof(name(0))                                          \
713 ____##name(unsigned long long *ctx ___bpf_ctx_decl(args));                      \
714 typeof(name(0)) name(unsigned long long *ctx)                                   \
715 {                                                                               \
716         return ____##name(ctx ___bpf_ctx_arg(args));                            \
717 }                                                                               \
718 static __always_inline typeof(name(0))                                          \
719 ____##name(unsigned long long *ctx ___bpf_ctx_decl(args))
720
721 struct pt_regs;
722
723 #define ___bpf_kprobe_args0()           ctx
724 #define ___bpf_kprobe_args1(x)          ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx)
725 #define ___bpf_kprobe_args2(x, args...) ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx)
726 #define ___bpf_kprobe_args3(x, args...) ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx)
727 #define ___bpf_kprobe_args4(x, args...) ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx)
728 #define ___bpf_kprobe_args5(x, args...) ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx)
729 #define ___bpf_kprobe_args6(x, args...) ___bpf_kprobe_args5(args), (void *)PT_REGS_PARM6(ctx)
730 #define ___bpf_kprobe_args7(x, args...) ___bpf_kprobe_args6(args), (void *)PT_REGS_PARM7(ctx)
731 #define ___bpf_kprobe_args8(x, args...) ___bpf_kprobe_args7(args), (void *)PT_REGS_PARM8(ctx)
732 #define ___bpf_kprobe_args(args...)     ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args)
733
734 /*
735  * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for
736  * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific
737  * low-level way of getting kprobe input arguments from struct pt_regs, and
738  * provides a familiar typed and named function arguments syntax and
739  * semantics of accessing kprobe input paremeters.
740  *
741  * Original struct pt_regs* context is preserved as 'ctx' argument. This might
742  * be necessary when using BPF helpers like bpf_perf_event_output().
743  */
744 #define BPF_KPROBE(name, args...)                                           \
745 name(struct pt_regs *ctx);                                                  \
746 static __always_inline typeof(name(0))                                      \
747 ____##name(struct pt_regs *ctx, ##args);                                    \
748 typeof(name(0)) name(struct pt_regs *ctx)                                   \
749 {                                                                           \
750         _Pragma("GCC diagnostic push")                                      \
751         _Pragma("GCC diagnostic ignored \"-Wint-conversion\"")              \
752         return ____##name(___bpf_kprobe_args(args));                        \
753         _Pragma("GCC diagnostic pop")                                       \
754 }                                                                           \
755 static __always_inline typeof(name(0))                                      \
756 ____##name(struct pt_regs *ctx, ##args)
757
758 #define ___bpf_kretprobe_args0()       ctx
759 #define ___bpf_kretprobe_args1(x)      ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx)
760 #define ___bpf_kretprobe_args(args...) ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args)
761
762 /*
763  * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional
764  * return value (in addition to `struct pt_regs *ctx`), but no input
765  * arguments, because they will be clobbered by the time probed function
766  * returns.
767  */
768 #define BPF_KRETPROBE(name, args...)                                        \
769 name(struct pt_regs *ctx);                                                  \
770 static __always_inline typeof(name(0))                                      \
771 ____##name(struct pt_regs *ctx, ##args);                                    \
772 typeof(name(0)) name(struct pt_regs *ctx)                                   \
773 {                                                                           \
774         _Pragma("GCC diagnostic push")                                      \
775         _Pragma("GCC diagnostic ignored \"-Wint-conversion\"")              \
776         return ____##name(___bpf_kretprobe_args(args));                     \
777         _Pragma("GCC diagnostic pop")                                       \
778 }                                                                           \
779 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
780
781 /* If kernel has CONFIG_ARCH_HAS_SYSCALL_WRAPPER, read pt_regs directly */
782 #define ___bpf_syscall_args0()           ctx
783 #define ___bpf_syscall_args1(x)          ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_SYSCALL(regs)
784 #define ___bpf_syscall_args2(x, args...) ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_SYSCALL(regs)
785 #define ___bpf_syscall_args3(x, args...) ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_SYSCALL(regs)
786 #define ___bpf_syscall_args4(x, args...) ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_SYSCALL(regs)
787 #define ___bpf_syscall_args5(x, args...) ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_SYSCALL(regs)
788 #define ___bpf_syscall_args6(x, args...) ___bpf_syscall_args5(args), (void *)PT_REGS_PARM6_SYSCALL(regs)
789 #define ___bpf_syscall_args7(x, args...) ___bpf_syscall_args6(args), (void *)PT_REGS_PARM7_SYSCALL(regs)
790 #define ___bpf_syscall_args(args...)     ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args)
791
792 /* If kernel doesn't have CONFIG_ARCH_HAS_SYSCALL_WRAPPER, we have to BPF_CORE_READ from pt_regs */
793 #define ___bpf_syswrap_args0()           ctx
794 #define ___bpf_syswrap_args1(x)          ___bpf_syswrap_args0(), (void *)PT_REGS_PARM1_CORE_SYSCALL(regs)
795 #define ___bpf_syswrap_args2(x, args...) ___bpf_syswrap_args1(args), (void *)PT_REGS_PARM2_CORE_SYSCALL(regs)
796 #define ___bpf_syswrap_args3(x, args...) ___bpf_syswrap_args2(args), (void *)PT_REGS_PARM3_CORE_SYSCALL(regs)
797 #define ___bpf_syswrap_args4(x, args...) ___bpf_syswrap_args3(args), (void *)PT_REGS_PARM4_CORE_SYSCALL(regs)
798 #define ___bpf_syswrap_args5(x, args...) ___bpf_syswrap_args4(args), (void *)PT_REGS_PARM5_CORE_SYSCALL(regs)
799 #define ___bpf_syswrap_args6(x, args...) ___bpf_syswrap_args5(args), (void *)PT_REGS_PARM6_CORE_SYSCALL(regs)
800 #define ___bpf_syswrap_args7(x, args...) ___bpf_syswrap_args6(args), (void *)PT_REGS_PARM7_CORE_SYSCALL(regs)
801 #define ___bpf_syswrap_args(args...)     ___bpf_apply(___bpf_syswrap_args, ___bpf_narg(args))(args)
802
803 /*
804  * BPF_KSYSCALL is a variant of BPF_KPROBE, which is intended for
805  * tracing syscall functions, like __x64_sys_close. It hides the underlying
806  * platform-specific low-level way of getting syscall input arguments from
807  * struct pt_regs, and provides a familiar typed and named function arguments
808  * syntax and semantics of accessing syscall input parameters.
809  *
810  * Original struct pt_regs * context is preserved as 'ctx' argument. This might
811  * be necessary when using BPF helpers like bpf_perf_event_output().
812  *
813  * At the moment BPF_KSYSCALL does not transparently handle all the calling
814  * convention quirks for the following syscalls:
815  *
816  * - mmap(): __ARCH_WANT_SYS_OLD_MMAP.
817  * - clone(): CONFIG_CLONE_BACKWARDS, CONFIG_CLONE_BACKWARDS2 and
818  *            CONFIG_CLONE_BACKWARDS3.
819  * - socket-related syscalls: __ARCH_WANT_SYS_SOCKETCALL.
820  * - compat syscalls.
821  *
822  * This may or may not change in the future. User needs to take extra measures
823  * to handle such quirks explicitly, if necessary.
824  *
825  * This macro relies on BPF CO-RE support and virtual __kconfig externs.
826  */
827 #define BPF_KSYSCALL(name, args...)                                         \
828 name(struct pt_regs *ctx);                                                  \
829 extern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig;                           \
830 static __always_inline typeof(name(0))                                      \
831 ____##name(struct pt_regs *ctx, ##args);                                    \
832 typeof(name(0)) name(struct pt_regs *ctx)                                   \
833 {                                                                           \
834         struct pt_regs *regs = LINUX_HAS_SYSCALL_WRAPPER                    \
835                                ? (struct pt_regs *)PT_REGS_PARM1(ctx)       \
836                                : ctx;                                       \
837         _Pragma("GCC diagnostic push")                                      \
838         _Pragma("GCC diagnostic ignored \"-Wint-conversion\"")              \
839         if (LINUX_HAS_SYSCALL_WRAPPER)                                      \
840                 return ____##name(___bpf_syswrap_args(args));               \
841         else                                                                \
842                 return ____##name(___bpf_syscall_args(args));               \
843         _Pragma("GCC diagnostic pop")                                       \
844 }                                                                           \
845 static __always_inline typeof(name(0))                                      \
846 ____##name(struct pt_regs *ctx, ##args)
847
848 #define BPF_KPROBE_SYSCALL BPF_KSYSCALL
849
850 /* BPF_UPROBE and BPF_URETPROBE are identical to BPF_KPROBE and BPF_KRETPROBE,
851  * but are named way less confusingly for SEC("uprobe") and SEC("uretprobe")
852  * use cases.
853  */
854 #define BPF_UPROBE(name, args...)  BPF_KPROBE(name, ##args)
855 #define BPF_URETPROBE(name, args...)  BPF_KRETPROBE(name, ##args)
856
857 #endif