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