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