1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 #ifndef __BPF_TRACING_H__ 3 #define __BPF_TRACING_H__ 4 5 /* Scan the ARCH passed in from ARCH env variable (see Makefile) */ 6 #if defined(__TARGET_ARCH_x86) 7 #define bpf_target_x86 8 #define bpf_target_defined 9 #elif defined(__TARGET_ARCH_s390) 10 #define bpf_target_s390 11 #define bpf_target_defined 12 #elif defined(__TARGET_ARCH_arm) 13 #define bpf_target_arm 14 #define bpf_target_defined 15 #elif defined(__TARGET_ARCH_arm64) 16 #define bpf_target_arm64 17 #define bpf_target_defined 18 #elif defined(__TARGET_ARCH_mips) 19 #define bpf_target_mips 20 #define bpf_target_defined 21 #elif defined(__TARGET_ARCH_powerpc) 22 #define bpf_target_powerpc 23 #define bpf_target_defined 24 #elif defined(__TARGET_ARCH_sparc) 25 #define bpf_target_sparc 26 #define bpf_target_defined 27 #elif defined(__TARGET_ARCH_riscv) 28 #define bpf_target_riscv 29 #define bpf_target_defined 30 #else 31 32 /* Fall back to what the compiler says */ 33 #if defined(__x86_64__) 34 #define bpf_target_x86 35 #define bpf_target_defined 36 #elif defined(__s390__) 37 #define bpf_target_s390 38 #define bpf_target_defined 39 #elif defined(__arm__) 40 #define bpf_target_arm 41 #define bpf_target_defined 42 #elif defined(__aarch64__) 43 #define bpf_target_arm64 44 #define bpf_target_defined 45 #elif defined(__mips__) 46 #define bpf_target_mips 47 #define bpf_target_defined 48 #elif defined(__powerpc__) 49 #define bpf_target_powerpc 50 #define bpf_target_defined 51 #elif defined(__sparc__) 52 #define bpf_target_sparc 53 #define bpf_target_defined 54 #elif defined(__riscv) && __riscv_xlen == 64 55 #define bpf_target_riscv 56 #define bpf_target_defined 57 #endif /* no compiler target */ 58 59 #endif 60 61 #ifndef __BPF_TARGET_MISSING 62 #define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\"" 63 #endif 64 65 #if defined(bpf_target_x86) 66 67 #if defined(__KERNEL__) || defined(__VMLINUX_H__) 68 69 #define PT_REGS_PARM1(x) ((x)->di) 70 #define PT_REGS_PARM2(x) ((x)->si) 71 #define PT_REGS_PARM3(x) ((x)->dx) 72 #define PT_REGS_PARM4(x) ((x)->cx) 73 #define PT_REGS_PARM5(x) ((x)->r8) 74 #define PT_REGS_RET(x) ((x)->sp) 75 #define PT_REGS_FP(x) ((x)->bp) 76 #define PT_REGS_RC(x) ((x)->ax) 77 #define PT_REGS_SP(x) ((x)->sp) 78 #define PT_REGS_IP(x) ((x)->ip) 79 80 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), di) 81 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), si) 82 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), dx) 83 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), cx) 84 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), r8) 85 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), sp) 86 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), bp) 87 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), ax) 88 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), sp) 89 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), ip) 90 91 #else 92 93 #ifdef __i386__ 94 /* i386 kernel is built with -mregparm=3 */ 95 #define PT_REGS_PARM1(x) ((x)->eax) 96 #define PT_REGS_PARM2(x) ((x)->edx) 97 #define PT_REGS_PARM3(x) ((x)->ecx) 98 #define PT_REGS_PARM4(x) 0 99 #define PT_REGS_PARM5(x) 0 100 #define PT_REGS_RET(x) ((x)->esp) 101 #define PT_REGS_FP(x) ((x)->ebp) 102 #define PT_REGS_RC(x) ((x)->eax) 103 #define PT_REGS_SP(x) ((x)->esp) 104 #define PT_REGS_IP(x) ((x)->eip) 105 106 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), eax) 107 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), edx) 108 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), ecx) 109 #define PT_REGS_PARM4_CORE(x) 0 110 #define PT_REGS_PARM5_CORE(x) 0 111 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), esp) 112 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), ebp) 113 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), eax) 114 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), esp) 115 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), eip) 116 117 #else 118 119 #define PT_REGS_PARM1(x) ((x)->rdi) 120 #define PT_REGS_PARM2(x) ((x)->rsi) 121 #define PT_REGS_PARM3(x) ((x)->rdx) 122 #define PT_REGS_PARM4(x) ((x)->rcx) 123 #define PT_REGS_PARM5(x) ((x)->r8) 124 #define PT_REGS_RET(x) ((x)->rsp) 125 #define PT_REGS_FP(x) ((x)->rbp) 126 #define PT_REGS_RC(x) ((x)->rax) 127 #define PT_REGS_SP(x) ((x)->rsp) 128 #define PT_REGS_IP(x) ((x)->rip) 129 130 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), rdi) 131 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), rsi) 132 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), rdx) 133 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), rcx) 134 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), r8) 135 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), rsp) 136 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), rbp) 137 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), rax) 138 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), rsp) 139 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), rip) 140 141 #endif 142 #endif 143 144 #elif defined(bpf_target_s390) 145 146 /* s390 provides user_pt_regs instead of struct pt_regs to userspace */ 147 struct pt_regs; 148 #define PT_REGS_S390 const volatile user_pt_regs 149 #define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2]) 150 #define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3]) 151 #define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4]) 152 #define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5]) 153 #define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6]) 154 #define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14]) 155 /* Works only with CONFIG_FRAME_POINTER */ 156 #define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11]) 157 #define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2]) 158 #define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15]) 159 #define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr) 160 161 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[2]) 162 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[3]) 163 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[4]) 164 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[5]) 165 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[6]) 166 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[14]) 167 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[11]) 168 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[2]) 169 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[15]) 170 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), psw.addr) 171 172 #elif defined(bpf_target_arm) 173 174 #define PT_REGS_PARM1(x) ((x)->uregs[0]) 175 #define PT_REGS_PARM2(x) ((x)->uregs[1]) 176 #define PT_REGS_PARM3(x) ((x)->uregs[2]) 177 #define PT_REGS_PARM4(x) ((x)->uregs[3]) 178 #define PT_REGS_PARM5(x) ((x)->uregs[4]) 179 #define PT_REGS_RET(x) ((x)->uregs[14]) 180 #define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */ 181 #define PT_REGS_RC(x) ((x)->uregs[0]) 182 #define PT_REGS_SP(x) ((x)->uregs[13]) 183 #define PT_REGS_IP(x) ((x)->uregs[12]) 184 185 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), uregs[0]) 186 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), uregs[1]) 187 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), uregs[2]) 188 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), uregs[3]) 189 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), uregs[4]) 190 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), uregs[14]) 191 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), uregs[11]) 192 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), uregs[0]) 193 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), uregs[13]) 194 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), uregs[12]) 195 196 #elif defined(bpf_target_arm64) 197 198 /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ 199 struct pt_regs; 200 #define PT_REGS_ARM64 const volatile struct user_pt_regs 201 #define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0]) 202 #define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1]) 203 #define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2]) 204 #define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3]) 205 #define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4]) 206 #define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30]) 207 /* Works only with CONFIG_FRAME_POINTER */ 208 #define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29]) 209 #define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0]) 210 #define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp) 211 #define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc) 212 213 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[0]) 214 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[1]) 215 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[2]) 216 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[3]) 217 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[4]) 218 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[30]) 219 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[29]) 220 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[0]) 221 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), sp) 222 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), pc) 223 224 #elif defined(bpf_target_mips) 225 226 #define PT_REGS_PARM1(x) ((x)->regs[4]) 227 #define PT_REGS_PARM2(x) ((x)->regs[5]) 228 #define PT_REGS_PARM3(x) ((x)->regs[6]) 229 #define PT_REGS_PARM4(x) ((x)->regs[7]) 230 #define PT_REGS_PARM5(x) ((x)->regs[8]) 231 #define PT_REGS_RET(x) ((x)->regs[31]) 232 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */ 233 #define PT_REGS_RC(x) ((x)->regs[2]) 234 #define PT_REGS_SP(x) ((x)->regs[29]) 235 #define PT_REGS_IP(x) ((x)->cp0_epc) 236 237 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), regs[4]) 238 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), regs[5]) 239 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), regs[6]) 240 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), regs[7]) 241 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), regs[8]) 242 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), regs[31]) 243 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), regs[30]) 244 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), regs[2]) 245 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), regs[29]) 246 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), cp0_epc) 247 248 #elif defined(bpf_target_powerpc) 249 250 #define PT_REGS_PARM1(x) ((x)->gpr[3]) 251 #define PT_REGS_PARM2(x) ((x)->gpr[4]) 252 #define PT_REGS_PARM3(x) ((x)->gpr[5]) 253 #define PT_REGS_PARM4(x) ((x)->gpr[6]) 254 #define PT_REGS_PARM5(x) ((x)->gpr[7]) 255 #define PT_REGS_RC(x) ((x)->gpr[3]) 256 #define PT_REGS_SP(x) ((x)->sp) 257 #define PT_REGS_IP(x) ((x)->nip) 258 259 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), gpr[3]) 260 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), gpr[4]) 261 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), gpr[5]) 262 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), gpr[6]) 263 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), gpr[7]) 264 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), gpr[3]) 265 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), sp) 266 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), nip) 267 268 #elif defined(bpf_target_sparc) 269 270 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) 271 #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) 272 #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) 273 #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) 274 #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) 275 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) 276 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) 277 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) 278 279 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I0]) 280 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I1]) 281 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I2]) 282 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I3]) 283 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I4]) 284 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I7]) 285 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I0]) 286 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), u_regs[UREG_FP]) 287 288 /* Should this also be a bpf_target check for the sparc case? */ 289 #if defined(__arch64__) 290 #define PT_REGS_IP(x) ((x)->tpc) 291 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), tpc) 292 #else 293 #define PT_REGS_IP(x) ((x)->pc) 294 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), pc) 295 #endif 296 297 #elif defined(bpf_target_riscv) 298 299 struct pt_regs; 300 #define PT_REGS_RV const volatile struct user_regs_struct 301 #define PT_REGS_PARM1(x) (((PT_REGS_RV *)(x))->a0) 302 #define PT_REGS_PARM2(x) (((PT_REGS_RV *)(x))->a1) 303 #define PT_REGS_PARM3(x) (((PT_REGS_RV *)(x))->a2) 304 #define PT_REGS_PARM4(x) (((PT_REGS_RV *)(x))->a3) 305 #define PT_REGS_PARM5(x) (((PT_REGS_RV *)(x))->a4) 306 #define PT_REGS_RET(x) (((PT_REGS_RV *)(x))->ra) 307 #define PT_REGS_FP(x) (((PT_REGS_RV *)(x))->s5) 308 #define PT_REGS_RC(x) (((PT_REGS_RV *)(x))->a5) 309 #define PT_REGS_SP(x) (((PT_REGS_RV *)(x))->sp) 310 #define PT_REGS_IP(x) (((PT_REGS_RV *)(x))->epc) 311 312 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a0) 313 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a1) 314 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a2) 315 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a3) 316 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a4) 317 #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), ra) 318 #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), fp) 319 #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a5) 320 #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), sp) 321 #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), epc) 322 323 #endif 324 325 #if defined(bpf_target_powerpc) 326 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) 327 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 328 #elif defined(bpf_target_sparc) 329 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) 330 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 331 #elif defined(bpf_target_defined) 332 #define BPF_KPROBE_READ_RET_IP(ip, ctx) \ 333 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) 334 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \ 335 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), \ 336 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) 337 #endif 338 339 #if !defined(bpf_target_defined) 340 341 #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 342 #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 343 #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 344 #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 345 #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 346 #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 347 #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 348 #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 349 #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 350 #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 351 352 #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 353 #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 354 #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 355 #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 356 #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 357 #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 358 #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 359 #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 360 #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 361 #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 362 363 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 364 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 365 366 #endif /* !defined(bpf_target_defined) */ 367 368 #ifndef ___bpf_concat 369 #define ___bpf_concat(a, b) a ## b 370 #endif 371 #ifndef ___bpf_apply 372 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 373 #endif 374 #ifndef ___bpf_nth 375 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 376 #endif 377 #ifndef ___bpf_narg 378 #define ___bpf_narg(...) \ 379 ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 380 #endif 381 382 #define ___bpf_ctx_cast0() ctx 383 #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] 384 #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] 385 #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] 386 #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] 387 #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] 388 #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] 389 #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] 390 #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] 391 #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] 392 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] 393 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] 394 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] 395 #define ___bpf_ctx_cast(args...) \ 396 ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) 397 398 /* 399 * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and 400 * similar kinds of BPF programs, that accept input arguments as a single 401 * pointer to untyped u64 array, where each u64 can actually be a typed 402 * pointer or integer of different size. Instead of requring user to write 403 * manual casts and work with array elements by index, BPF_PROG macro 404 * allows user to declare a list of named and typed input arguments in the 405 * same syntax as for normal C function. All the casting is hidden and 406 * performed transparently, while user code can just assume working with 407 * function arguments of specified type and name. 408 * 409 * Original raw context argument is preserved as well as 'ctx' argument. 410 * This is useful when using BPF helpers that expect original context 411 * as one of the parameters (e.g., for bpf_perf_event_output()). 412 */ 413 #define BPF_PROG(name, args...) \ 414 name(unsigned long long *ctx); \ 415 static __attribute__((always_inline)) typeof(name(0)) \ 416 ____##name(unsigned long long *ctx, ##args); \ 417 typeof(name(0)) name(unsigned long long *ctx) \ 418 { \ 419 _Pragma("GCC diagnostic push") \ 420 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 421 return ____##name(___bpf_ctx_cast(args)); \ 422 _Pragma("GCC diagnostic pop") \ 423 } \ 424 static __attribute__((always_inline)) typeof(name(0)) \ 425 ____##name(unsigned long long *ctx, ##args) 426 427 struct pt_regs; 428 429 #define ___bpf_kprobe_args0() ctx 430 #define ___bpf_kprobe_args1(x) \ 431 ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) 432 #define ___bpf_kprobe_args2(x, args...) \ 433 ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) 434 #define ___bpf_kprobe_args3(x, args...) \ 435 ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) 436 #define ___bpf_kprobe_args4(x, args...) \ 437 ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) 438 #define ___bpf_kprobe_args5(x, args...) \ 439 ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) 440 #define ___bpf_kprobe_args(args...) \ 441 ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) 442 443 /* 444 * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for 445 * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific 446 * low-level way of getting kprobe input arguments from struct pt_regs, and 447 * provides a familiar typed and named function arguments syntax and 448 * semantics of accessing kprobe input paremeters. 449 * 450 * Original struct pt_regs* context is preserved as 'ctx' argument. This might 451 * be necessary when using BPF helpers like bpf_perf_event_output(). 452 */ 453 #define BPF_KPROBE(name, args...) \ 454 name(struct pt_regs *ctx); \ 455 static __attribute__((always_inline)) typeof(name(0)) \ 456 ____##name(struct pt_regs *ctx, ##args); \ 457 typeof(name(0)) name(struct pt_regs *ctx) \ 458 { \ 459 _Pragma("GCC diagnostic push") \ 460 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 461 return ____##name(___bpf_kprobe_args(args)); \ 462 _Pragma("GCC diagnostic pop") \ 463 } \ 464 static __attribute__((always_inline)) typeof(name(0)) \ 465 ____##name(struct pt_regs *ctx, ##args) 466 467 #define ___bpf_kretprobe_args0() ctx 468 #define ___bpf_kretprobe_args1(x) \ 469 ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx) 470 #define ___bpf_kretprobe_args(args...) \ 471 ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) 472 473 /* 474 * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional 475 * return value (in addition to `struct pt_regs *ctx`), but no input 476 * arguments, because they will be clobbered by the time probed function 477 * returns. 478 */ 479 #define BPF_KRETPROBE(name, args...) \ 480 name(struct pt_regs *ctx); \ 481 static __attribute__((always_inline)) typeof(name(0)) \ 482 ____##name(struct pt_regs *ctx, ##args); \ 483 typeof(name(0)) name(struct pt_regs *ctx) \ 484 { \ 485 _Pragma("GCC diagnostic push") \ 486 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 487 return ____##name(___bpf_kretprobe_args(args)); \ 488 _Pragma("GCC diagnostic pop") \ 489 } \ 490 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 491 492 #endif 493