162306a36Sopenharmony_ci/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
262306a36Sopenharmony_ci#ifndef __BPF_TRACING_H__
362306a36Sopenharmony_ci#define __BPF_TRACING_H__
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include "bpf_helpers.h"
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
862306a36Sopenharmony_ci#if defined(__TARGET_ARCH_x86)
962306a36Sopenharmony_ci	#define bpf_target_x86
1062306a36Sopenharmony_ci	#define bpf_target_defined
1162306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_s390)
1262306a36Sopenharmony_ci	#define bpf_target_s390
1362306a36Sopenharmony_ci	#define bpf_target_defined
1462306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_arm)
1562306a36Sopenharmony_ci	#define bpf_target_arm
1662306a36Sopenharmony_ci	#define bpf_target_defined
1762306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_arm64)
1862306a36Sopenharmony_ci	#define bpf_target_arm64
1962306a36Sopenharmony_ci	#define bpf_target_defined
2062306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_mips)
2162306a36Sopenharmony_ci	#define bpf_target_mips
2262306a36Sopenharmony_ci	#define bpf_target_defined
2362306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_powerpc)
2462306a36Sopenharmony_ci	#define bpf_target_powerpc
2562306a36Sopenharmony_ci	#define bpf_target_defined
2662306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_sparc)
2762306a36Sopenharmony_ci	#define bpf_target_sparc
2862306a36Sopenharmony_ci	#define bpf_target_defined
2962306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_riscv)
3062306a36Sopenharmony_ci	#define bpf_target_riscv
3162306a36Sopenharmony_ci	#define bpf_target_defined
3262306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_arc)
3362306a36Sopenharmony_ci	#define bpf_target_arc
3462306a36Sopenharmony_ci	#define bpf_target_defined
3562306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_loongarch)
3662306a36Sopenharmony_ci	#define bpf_target_loongarch
3762306a36Sopenharmony_ci	#define bpf_target_defined
3862306a36Sopenharmony_ci#else
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* Fall back to what the compiler says */
4162306a36Sopenharmony_ci#if defined(__x86_64__)
4262306a36Sopenharmony_ci	#define bpf_target_x86
4362306a36Sopenharmony_ci	#define bpf_target_defined
4462306a36Sopenharmony_ci#elif defined(__s390__)
4562306a36Sopenharmony_ci	#define bpf_target_s390
4662306a36Sopenharmony_ci	#define bpf_target_defined
4762306a36Sopenharmony_ci#elif defined(__arm__)
4862306a36Sopenharmony_ci	#define bpf_target_arm
4962306a36Sopenharmony_ci	#define bpf_target_defined
5062306a36Sopenharmony_ci#elif defined(__aarch64__)
5162306a36Sopenharmony_ci	#define bpf_target_arm64
5262306a36Sopenharmony_ci	#define bpf_target_defined
5362306a36Sopenharmony_ci#elif defined(__mips__)
5462306a36Sopenharmony_ci	#define bpf_target_mips
5562306a36Sopenharmony_ci	#define bpf_target_defined
5662306a36Sopenharmony_ci#elif defined(__powerpc__)
5762306a36Sopenharmony_ci	#define bpf_target_powerpc
5862306a36Sopenharmony_ci	#define bpf_target_defined
5962306a36Sopenharmony_ci#elif defined(__sparc__)
6062306a36Sopenharmony_ci	#define bpf_target_sparc
6162306a36Sopenharmony_ci	#define bpf_target_defined
6262306a36Sopenharmony_ci#elif defined(__riscv) && __riscv_xlen == 64
6362306a36Sopenharmony_ci	#define bpf_target_riscv
6462306a36Sopenharmony_ci	#define bpf_target_defined
6562306a36Sopenharmony_ci#elif defined(__arc__)
6662306a36Sopenharmony_ci	#define bpf_target_arc
6762306a36Sopenharmony_ci	#define bpf_target_defined
6862306a36Sopenharmony_ci#elif defined(__loongarch__)
6962306a36Sopenharmony_ci	#define bpf_target_loongarch
7062306a36Sopenharmony_ci	#define bpf_target_defined
7162306a36Sopenharmony_ci#endif /* no compiler target */
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#endif
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#ifndef __BPF_TARGET_MISSING
7662306a36Sopenharmony_ci#define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\""
7762306a36Sopenharmony_ci#endif
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#if defined(bpf_target_x86)
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/*
8262306a36Sopenharmony_ci * https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#if defined(__KERNEL__) || defined(__VMLINUX_H__)
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define __PT_PARM1_REG di
8862306a36Sopenharmony_ci#define __PT_PARM2_REG si
8962306a36Sopenharmony_ci#define __PT_PARM3_REG dx
9062306a36Sopenharmony_ci#define __PT_PARM4_REG cx
9162306a36Sopenharmony_ci#define __PT_PARM5_REG r8
9262306a36Sopenharmony_ci#define __PT_PARM6_REG r9
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci * Syscall uses r10 for PARM4. See arch/x86/entry/entry_64.S:entry_SYSCALL_64
9562306a36Sopenharmony_ci * comments in Linux sources. And refer to syscall(2) manpage.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
9862306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
9962306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
10062306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG r10
10162306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
10262306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define __PT_RET_REG sp
10562306a36Sopenharmony_ci#define __PT_FP_REG bp
10662306a36Sopenharmony_ci#define __PT_RC_REG ax
10762306a36Sopenharmony_ci#define __PT_SP_REG sp
10862306a36Sopenharmony_ci#define __PT_IP_REG ip
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#else
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#ifdef __i386__
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/* i386 kernel is built with -mregparm=3 */
11562306a36Sopenharmony_ci#define __PT_PARM1_REG eax
11662306a36Sopenharmony_ci#define __PT_PARM2_REG edx
11762306a36Sopenharmony_ci#define __PT_PARM3_REG ecx
11862306a36Sopenharmony_ci/* i386 syscall ABI is very different, refer to syscall(2) manpage */
11962306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG ebx
12062306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG ecx
12162306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG edx
12262306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG esi
12362306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG edi
12462306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG ebp
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci#define __PT_RET_REG esp
12762306a36Sopenharmony_ci#define __PT_FP_REG ebp
12862306a36Sopenharmony_ci#define __PT_RC_REG eax
12962306a36Sopenharmony_ci#define __PT_SP_REG esp
13062306a36Sopenharmony_ci#define __PT_IP_REG eip
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci#else /* __i386__ */
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#define __PT_PARM1_REG rdi
13562306a36Sopenharmony_ci#define __PT_PARM2_REG rsi
13662306a36Sopenharmony_ci#define __PT_PARM3_REG rdx
13762306a36Sopenharmony_ci#define __PT_PARM4_REG rcx
13862306a36Sopenharmony_ci#define __PT_PARM5_REG r8
13962306a36Sopenharmony_ci#define __PT_PARM6_REG r9
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
14262306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
14362306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
14462306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG r10
14562306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
14662306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define __PT_RET_REG rsp
14962306a36Sopenharmony_ci#define __PT_FP_REG rbp
15062306a36Sopenharmony_ci#define __PT_RC_REG rax
15162306a36Sopenharmony_ci#define __PT_SP_REG rsp
15262306a36Sopenharmony_ci#define __PT_IP_REG rip
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci#endif /* __i386__ */
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci#endif /* __KERNEL__ || __VMLINUX_H__ */
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci#elif defined(bpf_target_s390)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/*
16162306a36Sopenharmony_ci * https://github.com/IBM/s390x-abi/releases/download/v1.6/lzsabi_s390x.pdf
16262306a36Sopenharmony_ci */
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistruct pt_regs___s390 {
16562306a36Sopenharmony_ci	unsigned long orig_gpr2;
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
16962306a36Sopenharmony_ci#define __PT_REGS_CAST(x) ((const user_pt_regs *)(x))
17062306a36Sopenharmony_ci#define __PT_PARM1_REG gprs[2]
17162306a36Sopenharmony_ci#define __PT_PARM2_REG gprs[3]
17262306a36Sopenharmony_ci#define __PT_PARM3_REG gprs[4]
17362306a36Sopenharmony_ci#define __PT_PARM4_REG gprs[5]
17462306a36Sopenharmony_ci#define __PT_PARM5_REG gprs[6]
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG orig_gpr2
17762306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
17862306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
17962306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
18062306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
18162306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG gprs[7]
18262306a36Sopenharmony_ci#define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
18362306a36Sopenharmony_ci#define PT_REGS_PARM1_CORE_SYSCALL(x) \
18462306a36Sopenharmony_ci	BPF_CORE_READ((const struct pt_regs___s390 *)(x), __PT_PARM1_SYSCALL_REG)
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci#define __PT_RET_REG gprs[14]
18762306a36Sopenharmony_ci#define __PT_FP_REG gprs[11]	/* Works only with CONFIG_FRAME_POINTER */
18862306a36Sopenharmony_ci#define __PT_RC_REG gprs[2]
18962306a36Sopenharmony_ci#define __PT_SP_REG gprs[15]
19062306a36Sopenharmony_ci#define __PT_IP_REG psw.addr
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#elif defined(bpf_target_arm)
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci/*
19562306a36Sopenharmony_ci * https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers
19662306a36Sopenharmony_ci */
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci#define __PT_PARM1_REG uregs[0]
19962306a36Sopenharmony_ci#define __PT_PARM2_REG uregs[1]
20062306a36Sopenharmony_ci#define __PT_PARM3_REG uregs[2]
20162306a36Sopenharmony_ci#define __PT_PARM4_REG uregs[3]
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
20462306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
20562306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
20662306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
20762306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG uregs[4]
20862306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG uregs[5]
20962306a36Sopenharmony_ci#define __PT_PARM7_SYSCALL_REG uregs[6]
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci#define __PT_RET_REG uregs[14]
21262306a36Sopenharmony_ci#define __PT_FP_REG uregs[11]	/* Works only with CONFIG_FRAME_POINTER */
21362306a36Sopenharmony_ci#define __PT_RC_REG uregs[0]
21462306a36Sopenharmony_ci#define __PT_SP_REG uregs[13]
21562306a36Sopenharmony_ci#define __PT_IP_REG uregs[12]
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci#elif defined(bpf_target_arm64)
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/*
22062306a36Sopenharmony_ci * https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers
22162306a36Sopenharmony_ci */
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistruct pt_regs___arm64 {
22462306a36Sopenharmony_ci	unsigned long orig_x0;
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
22862306a36Sopenharmony_ci#define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
22962306a36Sopenharmony_ci#define __PT_PARM1_REG regs[0]
23062306a36Sopenharmony_ci#define __PT_PARM2_REG regs[1]
23162306a36Sopenharmony_ci#define __PT_PARM3_REG regs[2]
23262306a36Sopenharmony_ci#define __PT_PARM4_REG regs[3]
23362306a36Sopenharmony_ci#define __PT_PARM5_REG regs[4]
23462306a36Sopenharmony_ci#define __PT_PARM6_REG regs[5]
23562306a36Sopenharmony_ci#define __PT_PARM7_REG regs[6]
23662306a36Sopenharmony_ci#define __PT_PARM8_REG regs[7]
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG orig_x0
23962306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
24062306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
24162306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
24262306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
24362306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
24462306a36Sopenharmony_ci#define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
24562306a36Sopenharmony_ci#define PT_REGS_PARM1_CORE_SYSCALL(x) \
24662306a36Sopenharmony_ci	BPF_CORE_READ((const struct pt_regs___arm64 *)(x), __PT_PARM1_SYSCALL_REG)
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci#define __PT_RET_REG regs[30]
24962306a36Sopenharmony_ci#define __PT_FP_REG regs[29]	/* Works only with CONFIG_FRAME_POINTER */
25062306a36Sopenharmony_ci#define __PT_RC_REG regs[0]
25162306a36Sopenharmony_ci#define __PT_SP_REG sp
25262306a36Sopenharmony_ci#define __PT_IP_REG pc
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci#elif defined(bpf_target_mips)
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci/*
25762306a36Sopenharmony_ci * N64 ABI is assumed right now.
25862306a36Sopenharmony_ci * https://en.wikipedia.org/wiki/MIPS_architecture#Calling_conventions
25962306a36Sopenharmony_ci */
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci#define __PT_PARM1_REG regs[4]
26262306a36Sopenharmony_ci#define __PT_PARM2_REG regs[5]
26362306a36Sopenharmony_ci#define __PT_PARM3_REG regs[6]
26462306a36Sopenharmony_ci#define __PT_PARM4_REG regs[7]
26562306a36Sopenharmony_ci#define __PT_PARM5_REG regs[8]
26662306a36Sopenharmony_ci#define __PT_PARM6_REG regs[9]
26762306a36Sopenharmony_ci#define __PT_PARM7_REG regs[10]
26862306a36Sopenharmony_ci#define __PT_PARM8_REG regs[11]
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
27162306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
27262306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
27362306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
27462306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG /* only N32/N64 */
27562306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG /* only N32/N64 */
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci#define __PT_RET_REG regs[31]
27862306a36Sopenharmony_ci#define __PT_FP_REG regs[30]	/* Works only with CONFIG_FRAME_POINTER */
27962306a36Sopenharmony_ci#define __PT_RC_REG regs[2]
28062306a36Sopenharmony_ci#define __PT_SP_REG regs[29]
28162306a36Sopenharmony_ci#define __PT_IP_REG cp0_epc
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci#elif defined(bpf_target_powerpc)
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci/*
28662306a36Sopenharmony_ci * http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf (page 3-14,
28762306a36Sopenharmony_ci * section "Function Calling Sequence")
28862306a36Sopenharmony_ci */
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci#define __PT_PARM1_REG gpr[3]
29162306a36Sopenharmony_ci#define __PT_PARM2_REG gpr[4]
29262306a36Sopenharmony_ci#define __PT_PARM3_REG gpr[5]
29362306a36Sopenharmony_ci#define __PT_PARM4_REG gpr[6]
29462306a36Sopenharmony_ci#define __PT_PARM5_REG gpr[7]
29562306a36Sopenharmony_ci#define __PT_PARM6_REG gpr[8]
29662306a36Sopenharmony_ci#define __PT_PARM7_REG gpr[9]
29762306a36Sopenharmony_ci#define __PT_PARM8_REG gpr[10]
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci/* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER. */
30062306a36Sopenharmony_ci#define PT_REGS_SYSCALL_REGS(ctx) ctx
30162306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG orig_gpr3
30262306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
30362306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
30462306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
30562306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
30662306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
30762306a36Sopenharmony_ci#if !defined(__arch64__)
30862306a36Sopenharmony_ci#define __PT_PARM7_SYSCALL_REG __PT_PARM7_REG /* only powerpc (not powerpc64) */
30962306a36Sopenharmony_ci#endif
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci#define __PT_RET_REG regs[31]
31262306a36Sopenharmony_ci#define __PT_FP_REG __unsupported__
31362306a36Sopenharmony_ci#define __PT_RC_REG gpr[3]
31462306a36Sopenharmony_ci#define __PT_SP_REG sp
31562306a36Sopenharmony_ci#define __PT_IP_REG nip
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci#elif defined(bpf_target_sparc)
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci/*
32062306a36Sopenharmony_ci * https://en.wikipedia.org/wiki/Calling_convention#SPARC
32162306a36Sopenharmony_ci */
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci#define __PT_PARM1_REG u_regs[UREG_I0]
32462306a36Sopenharmony_ci#define __PT_PARM2_REG u_regs[UREG_I1]
32562306a36Sopenharmony_ci#define __PT_PARM3_REG u_regs[UREG_I2]
32662306a36Sopenharmony_ci#define __PT_PARM4_REG u_regs[UREG_I3]
32762306a36Sopenharmony_ci#define __PT_PARM5_REG u_regs[UREG_I4]
32862306a36Sopenharmony_ci#define __PT_PARM6_REG u_regs[UREG_I5]
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
33162306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
33262306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
33362306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
33462306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
33562306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci#define __PT_RET_REG u_regs[UREG_I7]
33862306a36Sopenharmony_ci#define __PT_FP_REG __unsupported__
33962306a36Sopenharmony_ci#define __PT_RC_REG u_regs[UREG_I0]
34062306a36Sopenharmony_ci#define __PT_SP_REG u_regs[UREG_FP]
34162306a36Sopenharmony_ci/* Should this also be a bpf_target check for the sparc case? */
34262306a36Sopenharmony_ci#if defined(__arch64__)
34362306a36Sopenharmony_ci#define __PT_IP_REG tpc
34462306a36Sopenharmony_ci#else
34562306a36Sopenharmony_ci#define __PT_IP_REG pc
34662306a36Sopenharmony_ci#endif
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci#elif defined(bpf_target_riscv)
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci/*
35162306a36Sopenharmony_ci * https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#risc-v-calling-conventions
35262306a36Sopenharmony_ci */
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci/* riscv provides struct user_regs_struct instead of struct pt_regs to userspace */
35562306a36Sopenharmony_ci#define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
35662306a36Sopenharmony_ci#define __PT_PARM1_REG a0
35762306a36Sopenharmony_ci#define __PT_PARM2_REG a1
35862306a36Sopenharmony_ci#define __PT_PARM3_REG a2
35962306a36Sopenharmony_ci#define __PT_PARM4_REG a3
36062306a36Sopenharmony_ci#define __PT_PARM5_REG a4
36162306a36Sopenharmony_ci#define __PT_PARM6_REG a5
36262306a36Sopenharmony_ci#define __PT_PARM7_REG a6
36362306a36Sopenharmony_ci#define __PT_PARM8_REG a7
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
36662306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
36762306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
36862306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
36962306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
37062306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci#define __PT_RET_REG ra
37362306a36Sopenharmony_ci#define __PT_FP_REG s0
37462306a36Sopenharmony_ci#define __PT_RC_REG a0
37562306a36Sopenharmony_ci#define __PT_SP_REG sp
37662306a36Sopenharmony_ci#define __PT_IP_REG pc
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci#elif defined(bpf_target_arc)
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci/*
38162306a36Sopenharmony_ci * Section "Function Calling Sequence" (page 24):
38262306a36Sopenharmony_ci * https://raw.githubusercontent.com/wiki/foss-for-synopsys-dwc-arc-processors/toolchain/files/ARCv2_ABI.pdf
38362306a36Sopenharmony_ci */
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci/* arc provides struct user_regs_struct instead of struct pt_regs to userspace */
38662306a36Sopenharmony_ci#define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x))
38762306a36Sopenharmony_ci#define __PT_PARM1_REG scratch.r0
38862306a36Sopenharmony_ci#define __PT_PARM2_REG scratch.r1
38962306a36Sopenharmony_ci#define __PT_PARM3_REG scratch.r2
39062306a36Sopenharmony_ci#define __PT_PARM4_REG scratch.r3
39162306a36Sopenharmony_ci#define __PT_PARM5_REG scratch.r4
39262306a36Sopenharmony_ci#define __PT_PARM6_REG scratch.r5
39362306a36Sopenharmony_ci#define __PT_PARM7_REG scratch.r6
39462306a36Sopenharmony_ci#define __PT_PARM8_REG scratch.r7
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/* arc does not select ARCH_HAS_SYSCALL_WRAPPER. */
39762306a36Sopenharmony_ci#define PT_REGS_SYSCALL_REGS(ctx) ctx
39862306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
39962306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
40062306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
40162306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
40262306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
40362306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci#define __PT_RET_REG scratch.blink
40662306a36Sopenharmony_ci#define __PT_FP_REG scratch.fp
40762306a36Sopenharmony_ci#define __PT_RC_REG scratch.r0
40862306a36Sopenharmony_ci#define __PT_SP_REG scratch.sp
40962306a36Sopenharmony_ci#define __PT_IP_REG scratch.ret
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci#elif defined(bpf_target_loongarch)
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci/*
41462306a36Sopenharmony_ci * https://docs.kernel.org/loongarch/introduction.html
41562306a36Sopenharmony_ci * https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html
41662306a36Sopenharmony_ci */
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci/* loongarch provides struct user_pt_regs instead of struct pt_regs to userspace */
41962306a36Sopenharmony_ci#define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
42062306a36Sopenharmony_ci#define __PT_PARM1_REG regs[4]
42162306a36Sopenharmony_ci#define __PT_PARM2_REG regs[5]
42262306a36Sopenharmony_ci#define __PT_PARM3_REG regs[6]
42362306a36Sopenharmony_ci#define __PT_PARM4_REG regs[7]
42462306a36Sopenharmony_ci#define __PT_PARM5_REG regs[8]
42562306a36Sopenharmony_ci#define __PT_PARM6_REG regs[9]
42662306a36Sopenharmony_ci#define __PT_PARM7_REG regs[10]
42762306a36Sopenharmony_ci#define __PT_PARM8_REG regs[11]
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci/* loongarch does not select ARCH_HAS_SYSCALL_WRAPPER. */
43062306a36Sopenharmony_ci#define PT_REGS_SYSCALL_REGS(ctx) ctx
43162306a36Sopenharmony_ci#define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG
43262306a36Sopenharmony_ci#define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG
43362306a36Sopenharmony_ci#define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG
43462306a36Sopenharmony_ci#define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG
43562306a36Sopenharmony_ci#define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG
43662306a36Sopenharmony_ci#define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci#define __PT_RET_REG regs[1]
43962306a36Sopenharmony_ci#define __PT_FP_REG regs[22]
44062306a36Sopenharmony_ci#define __PT_RC_REG regs[4]
44162306a36Sopenharmony_ci#define __PT_SP_REG regs[3]
44262306a36Sopenharmony_ci#define __PT_IP_REG csr_era
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci#endif
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci#if defined(bpf_target_defined)
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_cistruct pt_regs;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci/* allow some architectures to override `struct pt_regs` */
45162306a36Sopenharmony_ci#ifndef __PT_REGS_CAST
45262306a36Sopenharmony_ci#define __PT_REGS_CAST(x) (x)
45362306a36Sopenharmony_ci#endif
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci/*
45662306a36Sopenharmony_ci * Different architectures support different number of arguments passed
45762306a36Sopenharmony_ci * through registers. i386 supports just 3, some arches support up to 8.
45862306a36Sopenharmony_ci */
45962306a36Sopenharmony_ci#ifndef __PT_PARM4_REG
46062306a36Sopenharmony_ci#define __PT_PARM4_REG __unsupported__
46162306a36Sopenharmony_ci#endif
46262306a36Sopenharmony_ci#ifndef __PT_PARM5_REG
46362306a36Sopenharmony_ci#define __PT_PARM5_REG __unsupported__
46462306a36Sopenharmony_ci#endif
46562306a36Sopenharmony_ci#ifndef __PT_PARM6_REG
46662306a36Sopenharmony_ci#define __PT_PARM6_REG __unsupported__
46762306a36Sopenharmony_ci#endif
46862306a36Sopenharmony_ci#ifndef __PT_PARM7_REG
46962306a36Sopenharmony_ci#define __PT_PARM7_REG __unsupported__
47062306a36Sopenharmony_ci#endif
47162306a36Sopenharmony_ci#ifndef __PT_PARM8_REG
47262306a36Sopenharmony_ci#define __PT_PARM8_REG __unsupported__
47362306a36Sopenharmony_ci#endif
47462306a36Sopenharmony_ci/*
47562306a36Sopenharmony_ci * Similarly, syscall-specific conventions might differ between function call
47662306a36Sopenharmony_ci * conventions within each architecutre. All supported architectures pass
47762306a36Sopenharmony_ci * either 6 or 7 syscall arguments in registers.
47862306a36Sopenharmony_ci *
47962306a36Sopenharmony_ci * See syscall(2) manpage for succinct table with information on each arch.
48062306a36Sopenharmony_ci */
48162306a36Sopenharmony_ci#ifndef __PT_PARM7_SYSCALL_REG
48262306a36Sopenharmony_ci#define __PT_PARM7_SYSCALL_REG __unsupported__
48362306a36Sopenharmony_ci#endif
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci#define PT_REGS_PARM1(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG)
48662306a36Sopenharmony_ci#define PT_REGS_PARM2(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG)
48762306a36Sopenharmony_ci#define PT_REGS_PARM3(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG)
48862306a36Sopenharmony_ci#define PT_REGS_PARM4(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG)
48962306a36Sopenharmony_ci#define PT_REGS_PARM5(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG)
49062306a36Sopenharmony_ci#define PT_REGS_PARM6(x) (__PT_REGS_CAST(x)->__PT_PARM6_REG)
49162306a36Sopenharmony_ci#define PT_REGS_PARM7(x) (__PT_REGS_CAST(x)->__PT_PARM7_REG)
49262306a36Sopenharmony_ci#define PT_REGS_PARM8(x) (__PT_REGS_CAST(x)->__PT_PARM8_REG)
49362306a36Sopenharmony_ci#define PT_REGS_RET(x) (__PT_REGS_CAST(x)->__PT_RET_REG)
49462306a36Sopenharmony_ci#define PT_REGS_FP(x) (__PT_REGS_CAST(x)->__PT_FP_REG)
49562306a36Sopenharmony_ci#define PT_REGS_RC(x) (__PT_REGS_CAST(x)->__PT_RC_REG)
49662306a36Sopenharmony_ci#define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG)
49762306a36Sopenharmony_ci#define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG)
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci#define PT_REGS_PARM1_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG)
50062306a36Sopenharmony_ci#define PT_REGS_PARM2_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG)
50162306a36Sopenharmony_ci#define PT_REGS_PARM3_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG)
50262306a36Sopenharmony_ci#define PT_REGS_PARM4_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG)
50362306a36Sopenharmony_ci#define PT_REGS_PARM5_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG)
50462306a36Sopenharmony_ci#define PT_REGS_PARM6_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_REG)
50562306a36Sopenharmony_ci#define PT_REGS_PARM7_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_REG)
50662306a36Sopenharmony_ci#define PT_REGS_PARM8_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM8_REG)
50762306a36Sopenharmony_ci#define PT_REGS_RET_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RET_REG)
50862306a36Sopenharmony_ci#define PT_REGS_FP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_FP_REG)
50962306a36Sopenharmony_ci#define PT_REGS_RC_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RC_REG)
51062306a36Sopenharmony_ci#define PT_REGS_SP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_SP_REG)
51162306a36Sopenharmony_ci#define PT_REGS_IP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_IP_REG)
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci#if defined(bpf_target_powerpc)
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci#define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = (ctx)->link; })
51662306a36Sopenharmony_ci#define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci#elif defined(bpf_target_sparc)
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci#define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = PT_REGS_RET(ctx); })
52162306a36Sopenharmony_ci#define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci#else
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci#define BPF_KPROBE_READ_RET_IP(ip, ctx)					    \
52662306a36Sopenharmony_ci	({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
52762306a36Sopenharmony_ci#define BPF_KRETPROBE_READ_RET_IP(ip, ctx)				    \
52862306a36Sopenharmony_ci	({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci#endif
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci#ifndef PT_REGS_PARM1_SYSCALL
53362306a36Sopenharmony_ci#define PT_REGS_PARM1_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM1_SYSCALL_REG)
53462306a36Sopenharmony_ci#define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_SYSCALL_REG)
53562306a36Sopenharmony_ci#endif
53662306a36Sopenharmony_ci#ifndef PT_REGS_PARM2_SYSCALL
53762306a36Sopenharmony_ci#define PT_REGS_PARM2_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM2_SYSCALL_REG)
53862306a36Sopenharmony_ci#define PT_REGS_PARM2_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_SYSCALL_REG)
53962306a36Sopenharmony_ci#endif
54062306a36Sopenharmony_ci#ifndef PT_REGS_PARM3_SYSCALL
54162306a36Sopenharmony_ci#define PT_REGS_PARM3_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM3_SYSCALL_REG)
54262306a36Sopenharmony_ci#define PT_REGS_PARM3_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_SYSCALL_REG)
54362306a36Sopenharmony_ci#endif
54462306a36Sopenharmony_ci#ifndef PT_REGS_PARM4_SYSCALL
54562306a36Sopenharmony_ci#define PT_REGS_PARM4_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM4_SYSCALL_REG)
54662306a36Sopenharmony_ci#define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_SYSCALL_REG)
54762306a36Sopenharmony_ci#endif
54862306a36Sopenharmony_ci#ifndef PT_REGS_PARM5_SYSCALL
54962306a36Sopenharmony_ci#define PT_REGS_PARM5_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM5_SYSCALL_REG)
55062306a36Sopenharmony_ci#define PT_REGS_PARM5_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_SYSCALL_REG)
55162306a36Sopenharmony_ci#endif
55262306a36Sopenharmony_ci#ifndef PT_REGS_PARM6_SYSCALL
55362306a36Sopenharmony_ci#define PT_REGS_PARM6_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM6_SYSCALL_REG)
55462306a36Sopenharmony_ci#define PT_REGS_PARM6_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_SYSCALL_REG)
55562306a36Sopenharmony_ci#endif
55662306a36Sopenharmony_ci#ifndef PT_REGS_PARM7_SYSCALL
55762306a36Sopenharmony_ci#define PT_REGS_PARM7_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM7_SYSCALL_REG)
55862306a36Sopenharmony_ci#define PT_REGS_PARM7_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_SYSCALL_REG)
55962306a36Sopenharmony_ci#endif
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci#else /* defined(bpf_target_defined) */
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci#define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
56462306a36Sopenharmony_ci#define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
56562306a36Sopenharmony_ci#define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
56662306a36Sopenharmony_ci#define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
56762306a36Sopenharmony_ci#define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
56862306a36Sopenharmony_ci#define PT_REGS_PARM6(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
56962306a36Sopenharmony_ci#define PT_REGS_PARM7(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57062306a36Sopenharmony_ci#define PT_REGS_PARM8(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57162306a36Sopenharmony_ci#define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57262306a36Sopenharmony_ci#define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57362306a36Sopenharmony_ci#define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57462306a36Sopenharmony_ci#define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57562306a36Sopenharmony_ci#define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci#define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57862306a36Sopenharmony_ci#define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
57962306a36Sopenharmony_ci#define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58062306a36Sopenharmony_ci#define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58162306a36Sopenharmony_ci#define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58262306a36Sopenharmony_ci#define PT_REGS_PARM6_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58362306a36Sopenharmony_ci#define PT_REGS_PARM7_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58462306a36Sopenharmony_ci#define PT_REGS_PARM8_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58562306a36Sopenharmony_ci#define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58662306a36Sopenharmony_ci#define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58762306a36Sopenharmony_ci#define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58862306a36Sopenharmony_ci#define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
58962306a36Sopenharmony_ci#define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59262306a36Sopenharmony_ci#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci#define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59562306a36Sopenharmony_ci#define PT_REGS_PARM2_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59662306a36Sopenharmony_ci#define PT_REGS_PARM3_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59762306a36Sopenharmony_ci#define PT_REGS_PARM4_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59862306a36Sopenharmony_ci#define PT_REGS_PARM5_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
59962306a36Sopenharmony_ci#define PT_REGS_PARM6_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60062306a36Sopenharmony_ci#define PT_REGS_PARM7_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci#define PT_REGS_PARM1_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60362306a36Sopenharmony_ci#define PT_REGS_PARM2_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60462306a36Sopenharmony_ci#define PT_REGS_PARM3_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60562306a36Sopenharmony_ci#define PT_REGS_PARM4_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60662306a36Sopenharmony_ci#define PT_REGS_PARM5_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60762306a36Sopenharmony_ci#define PT_REGS_PARM6_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60862306a36Sopenharmony_ci#define PT_REGS_PARM7_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci#endif /* defined(bpf_target_defined) */
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci/*
61362306a36Sopenharmony_ci * When invoked from a syscall handler kprobe, returns a pointer to a
61462306a36Sopenharmony_ci * struct pt_regs containing syscall arguments and suitable for passing to
61562306a36Sopenharmony_ci * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
61662306a36Sopenharmony_ci */
61762306a36Sopenharmony_ci#ifndef PT_REGS_SYSCALL_REGS
61862306a36Sopenharmony_ci/* By default, assume that the arch selects ARCH_HAS_SYSCALL_WRAPPER. */
61962306a36Sopenharmony_ci#define PT_REGS_SYSCALL_REGS(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
62062306a36Sopenharmony_ci#endif
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci#ifndef ___bpf_concat
62362306a36Sopenharmony_ci#define ___bpf_concat(a, b) a ## b
62462306a36Sopenharmony_ci#endif
62562306a36Sopenharmony_ci#ifndef ___bpf_apply
62662306a36Sopenharmony_ci#define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
62762306a36Sopenharmony_ci#endif
62862306a36Sopenharmony_ci#ifndef ___bpf_nth
62962306a36Sopenharmony_ci#define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
63062306a36Sopenharmony_ci#endif
63162306a36Sopenharmony_ci#ifndef ___bpf_narg
63262306a36Sopenharmony_ci#define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
63362306a36Sopenharmony_ci#endif
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci#define ___bpf_ctx_cast0()            ctx
63662306a36Sopenharmony_ci#define ___bpf_ctx_cast1(x)           ___bpf_ctx_cast0(), (void *)ctx[0]
63762306a36Sopenharmony_ci#define ___bpf_ctx_cast2(x, args...)  ___bpf_ctx_cast1(args), (void *)ctx[1]
63862306a36Sopenharmony_ci#define ___bpf_ctx_cast3(x, args...)  ___bpf_ctx_cast2(args), (void *)ctx[2]
63962306a36Sopenharmony_ci#define ___bpf_ctx_cast4(x, args...)  ___bpf_ctx_cast3(args), (void *)ctx[3]
64062306a36Sopenharmony_ci#define ___bpf_ctx_cast5(x, args...)  ___bpf_ctx_cast4(args), (void *)ctx[4]
64162306a36Sopenharmony_ci#define ___bpf_ctx_cast6(x, args...)  ___bpf_ctx_cast5(args), (void *)ctx[5]
64262306a36Sopenharmony_ci#define ___bpf_ctx_cast7(x, args...)  ___bpf_ctx_cast6(args), (void *)ctx[6]
64362306a36Sopenharmony_ci#define ___bpf_ctx_cast8(x, args...)  ___bpf_ctx_cast7(args), (void *)ctx[7]
64462306a36Sopenharmony_ci#define ___bpf_ctx_cast9(x, args...)  ___bpf_ctx_cast8(args), (void *)ctx[8]
64562306a36Sopenharmony_ci#define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
64662306a36Sopenharmony_ci#define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
64762306a36Sopenharmony_ci#define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
64862306a36Sopenharmony_ci#define ___bpf_ctx_cast(args...)      ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci/*
65162306a36Sopenharmony_ci * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
65262306a36Sopenharmony_ci * similar kinds of BPF programs, that accept input arguments as a single
65362306a36Sopenharmony_ci * pointer to untyped u64 array, where each u64 can actually be a typed
65462306a36Sopenharmony_ci * pointer or integer of different size. Instead of requring user to write
65562306a36Sopenharmony_ci * manual casts and work with array elements by index, BPF_PROG macro
65662306a36Sopenharmony_ci * allows user to declare a list of named and typed input arguments in the
65762306a36Sopenharmony_ci * same syntax as for normal C function. All the casting is hidden and
65862306a36Sopenharmony_ci * performed transparently, while user code can just assume working with
65962306a36Sopenharmony_ci * function arguments of specified type and name.
66062306a36Sopenharmony_ci *
66162306a36Sopenharmony_ci * Original raw context argument is preserved as well as 'ctx' argument.
66262306a36Sopenharmony_ci * This is useful when using BPF helpers that expect original context
66362306a36Sopenharmony_ci * as one of the parameters (e.g., for bpf_perf_event_output()).
66462306a36Sopenharmony_ci */
66562306a36Sopenharmony_ci#define BPF_PROG(name, args...)						    \
66662306a36Sopenharmony_ciname(unsigned long long *ctx);						    \
66762306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
66862306a36Sopenharmony_ci____##name(unsigned long long *ctx, ##args);				    \
66962306a36Sopenharmony_citypeof(name(0)) name(unsigned long long *ctx)				    \
67062306a36Sopenharmony_ci{									    \
67162306a36Sopenharmony_ci	_Pragma("GCC diagnostic push")					    \
67262306a36Sopenharmony_ci	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")		    \
67362306a36Sopenharmony_ci	return ____##name(___bpf_ctx_cast(args));			    \
67462306a36Sopenharmony_ci	_Pragma("GCC diagnostic pop")					    \
67562306a36Sopenharmony_ci}									    \
67662306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
67762306a36Sopenharmony_ci____##name(unsigned long long *ctx, ##args)
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci#ifndef ___bpf_nth2
68062306a36Sopenharmony_ci#define ___bpf_nth2(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13,	\
68162306a36Sopenharmony_ci		    _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, N, ...) N
68262306a36Sopenharmony_ci#endif
68362306a36Sopenharmony_ci#ifndef ___bpf_narg2
68462306a36Sopenharmony_ci#define ___bpf_narg2(...)	\
68562306a36Sopenharmony_ci	___bpf_nth2(_, ##__VA_ARGS__, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7,	\
68662306a36Sopenharmony_ci		    6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0)
68762306a36Sopenharmony_ci#endif
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci#define ___bpf_treg_cnt(t) \
69062306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 1, 1,	\
69162306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 2, 1,	\
69262306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 4, 1,	\
69362306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 8, 1,	\
69462306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 16, 2,	\
69562306a36Sopenharmony_ci			      (void)0)))))
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci#define ___bpf_reg_cnt0()		(0)
69862306a36Sopenharmony_ci#define ___bpf_reg_cnt1(t, x)		(___bpf_reg_cnt0() + ___bpf_treg_cnt(t))
69962306a36Sopenharmony_ci#define ___bpf_reg_cnt2(t, x, args...)	(___bpf_reg_cnt1(args) + ___bpf_treg_cnt(t))
70062306a36Sopenharmony_ci#define ___bpf_reg_cnt3(t, x, args...)	(___bpf_reg_cnt2(args) + ___bpf_treg_cnt(t))
70162306a36Sopenharmony_ci#define ___bpf_reg_cnt4(t, x, args...)	(___bpf_reg_cnt3(args) + ___bpf_treg_cnt(t))
70262306a36Sopenharmony_ci#define ___bpf_reg_cnt5(t, x, args...)	(___bpf_reg_cnt4(args) + ___bpf_treg_cnt(t))
70362306a36Sopenharmony_ci#define ___bpf_reg_cnt6(t, x, args...)	(___bpf_reg_cnt5(args) + ___bpf_treg_cnt(t))
70462306a36Sopenharmony_ci#define ___bpf_reg_cnt7(t, x, args...)	(___bpf_reg_cnt6(args) + ___bpf_treg_cnt(t))
70562306a36Sopenharmony_ci#define ___bpf_reg_cnt8(t, x, args...)	(___bpf_reg_cnt7(args) + ___bpf_treg_cnt(t))
70662306a36Sopenharmony_ci#define ___bpf_reg_cnt9(t, x, args...)	(___bpf_reg_cnt8(args) + ___bpf_treg_cnt(t))
70762306a36Sopenharmony_ci#define ___bpf_reg_cnt10(t, x, args...)	(___bpf_reg_cnt9(args) + ___bpf_treg_cnt(t))
70862306a36Sopenharmony_ci#define ___bpf_reg_cnt11(t, x, args...)	(___bpf_reg_cnt10(args) + ___bpf_treg_cnt(t))
70962306a36Sopenharmony_ci#define ___bpf_reg_cnt12(t, x, args...)	(___bpf_reg_cnt11(args) + ___bpf_treg_cnt(t))
71062306a36Sopenharmony_ci#define ___bpf_reg_cnt(args...)	 ___bpf_apply(___bpf_reg_cnt, ___bpf_narg2(args))(args)
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_ci#define ___bpf_union_arg(t, x, n) \
71362306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 1, ({ union { __u8 z[1]; t x; } ___t = { .z = {ctx[n]}}; ___t.x; }), \
71462306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 2, ({ union { __u16 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \
71562306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 4, ({ union { __u32 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \
71662306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 8, ({ union { __u64 z[1]; t x; } ___t = {.z = {ctx[n]} }; ___t.x; }), \
71762306a36Sopenharmony_ci	__builtin_choose_expr(sizeof(t) == 16, ({ union { __u64 z[2]; t x; } ___t = {.z = {ctx[n], ctx[n + 1]} }; ___t.x; }), \
71862306a36Sopenharmony_ci			      (void)0)))))
71962306a36Sopenharmony_ci
72062306a36Sopenharmony_ci#define ___bpf_ctx_arg0(n, args...)
72162306a36Sopenharmony_ci#define ___bpf_ctx_arg1(n, t, x)		, ___bpf_union_arg(t, x, n - ___bpf_reg_cnt1(t, x))
72262306a36Sopenharmony_ci#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)
72362306a36Sopenharmony_ci#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)
72462306a36Sopenharmony_ci#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)
72562306a36Sopenharmony_ci#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)
72662306a36Sopenharmony_ci#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)
72762306a36Sopenharmony_ci#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)
72862306a36Sopenharmony_ci#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)
72962306a36Sopenharmony_ci#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)
73062306a36Sopenharmony_ci#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)
73162306a36Sopenharmony_ci#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)
73262306a36Sopenharmony_ci#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)
73362306a36Sopenharmony_ci#define ___bpf_ctx_arg(args...)	___bpf_apply(___bpf_ctx_arg, ___bpf_narg2(args))(___bpf_reg_cnt(args), args)
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci#define ___bpf_ctx_decl0()
73662306a36Sopenharmony_ci#define ___bpf_ctx_decl1(t, x)			, t x
73762306a36Sopenharmony_ci#define ___bpf_ctx_decl2(t, x, args...)		, t x ___bpf_ctx_decl1(args)
73862306a36Sopenharmony_ci#define ___bpf_ctx_decl3(t, x, args...)		, t x ___bpf_ctx_decl2(args)
73962306a36Sopenharmony_ci#define ___bpf_ctx_decl4(t, x, args...)		, t x ___bpf_ctx_decl3(args)
74062306a36Sopenharmony_ci#define ___bpf_ctx_decl5(t, x, args...)		, t x ___bpf_ctx_decl4(args)
74162306a36Sopenharmony_ci#define ___bpf_ctx_decl6(t, x, args...)		, t x ___bpf_ctx_decl5(args)
74262306a36Sopenharmony_ci#define ___bpf_ctx_decl7(t, x, args...)		, t x ___bpf_ctx_decl6(args)
74362306a36Sopenharmony_ci#define ___bpf_ctx_decl8(t, x, args...)		, t x ___bpf_ctx_decl7(args)
74462306a36Sopenharmony_ci#define ___bpf_ctx_decl9(t, x, args...)		, t x ___bpf_ctx_decl8(args)
74562306a36Sopenharmony_ci#define ___bpf_ctx_decl10(t, x, args...)	, t x ___bpf_ctx_decl9(args)
74662306a36Sopenharmony_ci#define ___bpf_ctx_decl11(t, x, args...)	, t x ___bpf_ctx_decl10(args)
74762306a36Sopenharmony_ci#define ___bpf_ctx_decl12(t, x, args...)	, t x ___bpf_ctx_decl11(args)
74862306a36Sopenharmony_ci#define ___bpf_ctx_decl(args...)	___bpf_apply(___bpf_ctx_decl, ___bpf_narg2(args))(args)
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci/*
75162306a36Sopenharmony_ci * BPF_PROG2 is an enhanced version of BPF_PROG in order to handle struct
75262306a36Sopenharmony_ci * arguments. Since each struct argument might take one or two u64 values
75362306a36Sopenharmony_ci * in the trampoline stack, argument type size is needed to place proper number
75462306a36Sopenharmony_ci * of u64 values for each argument. Therefore, BPF_PROG2 has different
75562306a36Sopenharmony_ci * syntax from BPF_PROG. For example, for the following BPF_PROG syntax:
75662306a36Sopenharmony_ci *
75762306a36Sopenharmony_ci *   int BPF_PROG(test2, int a, int b) { ... }
75862306a36Sopenharmony_ci *
75962306a36Sopenharmony_ci * the corresponding BPF_PROG2 syntax is:
76062306a36Sopenharmony_ci *
76162306a36Sopenharmony_ci *   int BPF_PROG2(test2, int, a, int, b) { ... }
76262306a36Sopenharmony_ci *
76362306a36Sopenharmony_ci * where type and the corresponding argument name are separated by comma.
76462306a36Sopenharmony_ci *
76562306a36Sopenharmony_ci * Use BPF_PROG2 macro if one of the arguments might be a struct/union larger
76662306a36Sopenharmony_ci * than 8 bytes:
76762306a36Sopenharmony_ci *
76862306a36Sopenharmony_ci *   int BPF_PROG2(test_struct_arg, struct bpf_testmod_struct_arg_1, a, int, b,
76962306a36Sopenharmony_ci *		   int, c, int, d, struct bpf_testmod_struct_arg_2, e, int, ret)
77062306a36Sopenharmony_ci *   {
77162306a36Sopenharmony_ci *        // access a, b, c, d, e, and ret directly
77262306a36Sopenharmony_ci *        ...
77362306a36Sopenharmony_ci *   }
77462306a36Sopenharmony_ci */
77562306a36Sopenharmony_ci#define BPF_PROG2(name, args...)						\
77662306a36Sopenharmony_ciname(unsigned long long *ctx);							\
77762306a36Sopenharmony_cistatic __always_inline typeof(name(0))						\
77862306a36Sopenharmony_ci____##name(unsigned long long *ctx ___bpf_ctx_decl(args));			\
77962306a36Sopenharmony_citypeof(name(0)) name(unsigned long long *ctx)					\
78062306a36Sopenharmony_ci{										\
78162306a36Sopenharmony_ci	return ____##name(ctx ___bpf_ctx_arg(args));				\
78262306a36Sopenharmony_ci}										\
78362306a36Sopenharmony_cistatic __always_inline typeof(name(0))						\
78462306a36Sopenharmony_ci____##name(unsigned long long *ctx ___bpf_ctx_decl(args))
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_cistruct pt_regs;
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_ci#define ___bpf_kprobe_args0()           ctx
78962306a36Sopenharmony_ci#define ___bpf_kprobe_args1(x)          ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx)
79062306a36Sopenharmony_ci#define ___bpf_kprobe_args2(x, args...) ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx)
79162306a36Sopenharmony_ci#define ___bpf_kprobe_args3(x, args...) ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx)
79262306a36Sopenharmony_ci#define ___bpf_kprobe_args4(x, args...) ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx)
79362306a36Sopenharmony_ci#define ___bpf_kprobe_args5(x, args...) ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx)
79462306a36Sopenharmony_ci#define ___bpf_kprobe_args6(x, args...) ___bpf_kprobe_args5(args), (void *)PT_REGS_PARM6(ctx)
79562306a36Sopenharmony_ci#define ___bpf_kprobe_args7(x, args...) ___bpf_kprobe_args6(args), (void *)PT_REGS_PARM7(ctx)
79662306a36Sopenharmony_ci#define ___bpf_kprobe_args8(x, args...) ___bpf_kprobe_args7(args), (void *)PT_REGS_PARM8(ctx)
79762306a36Sopenharmony_ci#define ___bpf_kprobe_args(args...)     ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args)
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci/*
80062306a36Sopenharmony_ci * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for
80162306a36Sopenharmony_ci * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific
80262306a36Sopenharmony_ci * low-level way of getting kprobe input arguments from struct pt_regs, and
80362306a36Sopenharmony_ci * provides a familiar typed and named function arguments syntax and
80462306a36Sopenharmony_ci * semantics of accessing kprobe input paremeters.
80562306a36Sopenharmony_ci *
80662306a36Sopenharmony_ci * Original struct pt_regs* context is preserved as 'ctx' argument. This might
80762306a36Sopenharmony_ci * be necessary when using BPF helpers like bpf_perf_event_output().
80862306a36Sopenharmony_ci */
80962306a36Sopenharmony_ci#define BPF_KPROBE(name, args...)					    \
81062306a36Sopenharmony_ciname(struct pt_regs *ctx);						    \
81162306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
81262306a36Sopenharmony_ci____##name(struct pt_regs *ctx, ##args);				    \
81362306a36Sopenharmony_citypeof(name(0)) name(struct pt_regs *ctx)				    \
81462306a36Sopenharmony_ci{									    \
81562306a36Sopenharmony_ci	_Pragma("GCC diagnostic push")					    \
81662306a36Sopenharmony_ci	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")		    \
81762306a36Sopenharmony_ci	return ____##name(___bpf_kprobe_args(args));			    \
81862306a36Sopenharmony_ci	_Pragma("GCC diagnostic pop")					    \
81962306a36Sopenharmony_ci}									    \
82062306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
82162306a36Sopenharmony_ci____##name(struct pt_regs *ctx, ##args)
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci#define ___bpf_kretprobe_args0()       ctx
82462306a36Sopenharmony_ci#define ___bpf_kretprobe_args1(x)      ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx)
82562306a36Sopenharmony_ci#define ___bpf_kretprobe_args(args...) ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args)
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci/*
82862306a36Sopenharmony_ci * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional
82962306a36Sopenharmony_ci * return value (in addition to `struct pt_regs *ctx`), but no input
83062306a36Sopenharmony_ci * arguments, because they will be clobbered by the time probed function
83162306a36Sopenharmony_ci * returns.
83262306a36Sopenharmony_ci */
83362306a36Sopenharmony_ci#define BPF_KRETPROBE(name, args...)					    \
83462306a36Sopenharmony_ciname(struct pt_regs *ctx);						    \
83562306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
83662306a36Sopenharmony_ci____##name(struct pt_regs *ctx, ##args);				    \
83762306a36Sopenharmony_citypeof(name(0)) name(struct pt_regs *ctx)				    \
83862306a36Sopenharmony_ci{									    \
83962306a36Sopenharmony_ci	_Pragma("GCC diagnostic push")					    \
84062306a36Sopenharmony_ci	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")		    \
84162306a36Sopenharmony_ci	return ____##name(___bpf_kretprobe_args(args));			    \
84262306a36Sopenharmony_ci	_Pragma("GCC diagnostic pop")					    \
84362306a36Sopenharmony_ci}									    \
84462306a36Sopenharmony_cistatic __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci/* If kernel has CONFIG_ARCH_HAS_SYSCALL_WRAPPER, read pt_regs directly */
84762306a36Sopenharmony_ci#define ___bpf_syscall_args0()           ctx
84862306a36Sopenharmony_ci#define ___bpf_syscall_args1(x)          ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_SYSCALL(regs)
84962306a36Sopenharmony_ci#define ___bpf_syscall_args2(x, args...) ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_SYSCALL(regs)
85062306a36Sopenharmony_ci#define ___bpf_syscall_args3(x, args...) ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_SYSCALL(regs)
85162306a36Sopenharmony_ci#define ___bpf_syscall_args4(x, args...) ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_SYSCALL(regs)
85262306a36Sopenharmony_ci#define ___bpf_syscall_args5(x, args...) ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_SYSCALL(regs)
85362306a36Sopenharmony_ci#define ___bpf_syscall_args6(x, args...) ___bpf_syscall_args5(args), (void *)PT_REGS_PARM6_SYSCALL(regs)
85462306a36Sopenharmony_ci#define ___bpf_syscall_args7(x, args...) ___bpf_syscall_args6(args), (void *)PT_REGS_PARM7_SYSCALL(regs)
85562306a36Sopenharmony_ci#define ___bpf_syscall_args(args...)     ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args)
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ci/* If kernel doesn't have CONFIG_ARCH_HAS_SYSCALL_WRAPPER, we have to BPF_CORE_READ from pt_regs */
85862306a36Sopenharmony_ci#define ___bpf_syswrap_args0()           ctx
85962306a36Sopenharmony_ci#define ___bpf_syswrap_args1(x)          ___bpf_syswrap_args0(), (void *)PT_REGS_PARM1_CORE_SYSCALL(regs)
86062306a36Sopenharmony_ci#define ___bpf_syswrap_args2(x, args...) ___bpf_syswrap_args1(args), (void *)PT_REGS_PARM2_CORE_SYSCALL(regs)
86162306a36Sopenharmony_ci#define ___bpf_syswrap_args3(x, args...) ___bpf_syswrap_args2(args), (void *)PT_REGS_PARM3_CORE_SYSCALL(regs)
86262306a36Sopenharmony_ci#define ___bpf_syswrap_args4(x, args...) ___bpf_syswrap_args3(args), (void *)PT_REGS_PARM4_CORE_SYSCALL(regs)
86362306a36Sopenharmony_ci#define ___bpf_syswrap_args5(x, args...) ___bpf_syswrap_args4(args), (void *)PT_REGS_PARM5_CORE_SYSCALL(regs)
86462306a36Sopenharmony_ci#define ___bpf_syswrap_args6(x, args...) ___bpf_syswrap_args5(args), (void *)PT_REGS_PARM6_CORE_SYSCALL(regs)
86562306a36Sopenharmony_ci#define ___bpf_syswrap_args7(x, args...) ___bpf_syswrap_args6(args), (void *)PT_REGS_PARM7_CORE_SYSCALL(regs)
86662306a36Sopenharmony_ci#define ___bpf_syswrap_args(args...)     ___bpf_apply(___bpf_syswrap_args, ___bpf_narg(args))(args)
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci/*
86962306a36Sopenharmony_ci * BPF_KSYSCALL is a variant of BPF_KPROBE, which is intended for
87062306a36Sopenharmony_ci * tracing syscall functions, like __x64_sys_close. It hides the underlying
87162306a36Sopenharmony_ci * platform-specific low-level way of getting syscall input arguments from
87262306a36Sopenharmony_ci * struct pt_regs, and provides a familiar typed and named function arguments
87362306a36Sopenharmony_ci * syntax and semantics of accessing syscall input parameters.
87462306a36Sopenharmony_ci *
87562306a36Sopenharmony_ci * Original struct pt_regs * context is preserved as 'ctx' argument. This might
87662306a36Sopenharmony_ci * be necessary when using BPF helpers like bpf_perf_event_output().
87762306a36Sopenharmony_ci *
87862306a36Sopenharmony_ci * At the moment BPF_KSYSCALL does not transparently handle all the calling
87962306a36Sopenharmony_ci * convention quirks for the following syscalls:
88062306a36Sopenharmony_ci *
88162306a36Sopenharmony_ci * - mmap(): __ARCH_WANT_SYS_OLD_MMAP.
88262306a36Sopenharmony_ci * - clone(): CONFIG_CLONE_BACKWARDS, CONFIG_CLONE_BACKWARDS2 and
88362306a36Sopenharmony_ci *            CONFIG_CLONE_BACKWARDS3.
88462306a36Sopenharmony_ci * - socket-related syscalls: __ARCH_WANT_SYS_SOCKETCALL.
88562306a36Sopenharmony_ci * - compat syscalls.
88662306a36Sopenharmony_ci *
88762306a36Sopenharmony_ci * This may or may not change in the future. User needs to take extra measures
88862306a36Sopenharmony_ci * to handle such quirks explicitly, if necessary.
88962306a36Sopenharmony_ci *
89062306a36Sopenharmony_ci * This macro relies on BPF CO-RE support and virtual __kconfig externs.
89162306a36Sopenharmony_ci */
89262306a36Sopenharmony_ci#define BPF_KSYSCALL(name, args...)					    \
89362306a36Sopenharmony_ciname(struct pt_regs *ctx);						    \
89462306a36Sopenharmony_ciextern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig;			    \
89562306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
89662306a36Sopenharmony_ci____##name(struct pt_regs *ctx, ##args);				    \
89762306a36Sopenharmony_citypeof(name(0)) name(struct pt_regs *ctx)				    \
89862306a36Sopenharmony_ci{									    \
89962306a36Sopenharmony_ci	struct pt_regs *regs = LINUX_HAS_SYSCALL_WRAPPER		    \
90062306a36Sopenharmony_ci			       ? (struct pt_regs *)PT_REGS_PARM1(ctx)	    \
90162306a36Sopenharmony_ci			       : ctx;					    \
90262306a36Sopenharmony_ci	_Pragma("GCC diagnostic push")					    \
90362306a36Sopenharmony_ci	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")		    \
90462306a36Sopenharmony_ci	if (LINUX_HAS_SYSCALL_WRAPPER)					    \
90562306a36Sopenharmony_ci		return ____##name(___bpf_syswrap_args(args));		    \
90662306a36Sopenharmony_ci	else								    \
90762306a36Sopenharmony_ci		return ____##name(___bpf_syscall_args(args));		    \
90862306a36Sopenharmony_ci	_Pragma("GCC diagnostic pop")					    \
90962306a36Sopenharmony_ci}									    \
91062306a36Sopenharmony_cistatic __always_inline typeof(name(0))					    \
91162306a36Sopenharmony_ci____##name(struct pt_regs *ctx, ##args)
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci#define BPF_KPROBE_SYSCALL BPF_KSYSCALL
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci/* BPF_UPROBE and BPF_URETPROBE are identical to BPF_KPROBE and BPF_KRETPROBE,
91662306a36Sopenharmony_ci * but are named way less confusingly for SEC("uprobe") and SEC("uretprobe")
91762306a36Sopenharmony_ci * use cases.
91862306a36Sopenharmony_ci */
91962306a36Sopenharmony_ci#define BPF_UPROBE(name, args...)  BPF_KPROBE(name, ##args)
92062306a36Sopenharmony_ci#define BPF_URETPROBE(name, args...)  BPF_KRETPROBE(name, ##args)
92162306a36Sopenharmony_ci
92262306a36Sopenharmony_ci#endif
923