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_PARM1_REG di 70 #define __PT_PARM2_REG si 71 #define __PT_PARM3_REG dx 72 #define __PT_PARM4_REG cx 73 #define __PT_PARM5_REG r8 74 #define __PT_RET_REG sp 75 #define __PT_FP_REG bp 76 #define __PT_RC_REG ax 77 #define __PT_SP_REG sp 78 #define __PT_IP_REG ip 79 80 #else 81 82 #ifdef __i386__ 83 84 #define __PT_PARM1_REG eax 85 #define __PT_PARM2_REG edx 86 #define __PT_PARM3_REG ecx 87 /* i386 kernel is built with -mregparm=3 */ 88 #define __PT_PARM4_REG __unsupported__ 89 #define __PT_PARM5_REG __unsupported__ 90 #define __PT_RET_REG esp 91 #define __PT_FP_REG ebp 92 #define __PT_RC_REG eax 93 #define __PT_SP_REG esp 94 #define __PT_IP_REG eip 95 96 #else /* __i386__ */ 97 98 #define __PT_PARM1_REG rdi 99 #define __PT_PARM2_REG rsi 100 #define __PT_PARM3_REG rdx 101 #define __PT_PARM4_REG rcx 102 #define __PT_PARM5_REG r8 103 #define __PT_RET_REG rsp 104 #define __PT_FP_REG rbp 105 #define __PT_RC_REG rax 106 #define __PT_SP_REG rsp 107 #define __PT_IP_REG rip 108 109 #endif /* __i386__ */ 110 111 #endif /* __KERNEL__ || __VMLINUX_H__ */ 112 113 #elif defined(bpf_target_s390) 114 115 /* s390 provides user_pt_regs instead of struct pt_regs to userspace */ 116 #define __PT_REGS_CAST(x) ((const user_pt_regs *)(x)) 117 #define __PT_PARM1_REG gprs[2] 118 #define __PT_PARM2_REG gprs[3] 119 #define __PT_PARM3_REG gprs[4] 120 #define __PT_PARM4_REG gprs[5] 121 #define __PT_PARM5_REG gprs[6] 122 #define __PT_RET_REG grps[14] 123 #define __PT_FP_REG gprs[11] /* Works only with CONFIG_FRAME_POINTER */ 124 #define __PT_RC_REG gprs[2] 125 #define __PT_SP_REG gprs[15] 126 #define __PT_IP_REG psw.addr 127 128 #elif defined(bpf_target_arm) 129 130 #define __PT_PARM1_REG uregs[0] 131 #define __PT_PARM2_REG uregs[1] 132 #define __PT_PARM3_REG uregs[2] 133 #define __PT_PARM4_REG uregs[3] 134 #define __PT_PARM5_REG uregs[4] 135 #define __PT_RET_REG uregs[14] 136 #define __PT_FP_REG uregs[11] /* Works only with CONFIG_FRAME_POINTER */ 137 #define __PT_RC_REG uregs[0] 138 #define __PT_SP_REG uregs[13] 139 #define __PT_IP_REG uregs[12] 140 141 #elif defined(bpf_target_arm64) 142 143 /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ 144 #define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x)) 145 #define __PT_PARM1_REG regs[0] 146 #define __PT_PARM2_REG regs[1] 147 #define __PT_PARM3_REG regs[2] 148 #define __PT_PARM4_REG regs[3] 149 #define __PT_PARM5_REG regs[4] 150 #define __PT_RET_REG regs[30] 151 #define __PT_FP_REG regs[29] /* Works only with CONFIG_FRAME_POINTER */ 152 #define __PT_RC_REG regs[0] 153 #define __PT_SP_REG sp 154 #define __PT_IP_REG pc 155 156 #elif defined(bpf_target_mips) 157 158 #define __PT_PARM1_REG regs[4] 159 #define __PT_PARM2_REG regs[5] 160 #define __PT_PARM3_REG regs[6] 161 #define __PT_PARM4_REG regs[7] 162 #define __PT_PARM5_REG regs[8] 163 #define __PT_RET_REG regs[31] 164 #define __PT_FP_REG regs[30] /* Works only with CONFIG_FRAME_POINTER */ 165 #define __PT_RC_REG regs[2] 166 #define __PT_SP_REG regs[29] 167 #define __PT_IP_REG cp0_epc 168 169 #elif defined(bpf_target_powerpc) 170 171 #define __PT_PARM1_REG gpr[3] 172 #define __PT_PARM2_REG gpr[4] 173 #define __PT_PARM3_REG gpr[5] 174 #define __PT_PARM4_REG gpr[6] 175 #define __PT_PARM5_REG gpr[7] 176 #define __PT_RET_REG regs[31] 177 #define __PT_FP_REG __unsupported__ 178 #define __PT_RC_REG gpr[3] 179 #define __PT_SP_REG sp 180 #define __PT_IP_REG nip 181 182 #elif defined(bpf_target_sparc) 183 184 #define __PT_PARM1_REG u_regs[UREG_I0] 185 #define __PT_PARM2_REG u_regs[UREG_I1] 186 #define __PT_PARM3_REG u_regs[UREG_I2] 187 #define __PT_PARM4_REG u_regs[UREG_I3] 188 #define __PT_PARM5_REG u_regs[UREG_I4] 189 #define __PT_RET_REG u_regs[UREG_I7] 190 #define __PT_FP_REG __unsupported__ 191 #define __PT_RC_REG u_regs[UREG_I0] 192 #define __PT_SP_REG u_regs[UREG_FP] 193 /* Should this also be a bpf_target check for the sparc case? */ 194 #if defined(__arch64__) 195 #define __PT_IP_REG tpc 196 #else 197 #define __PT_IP_REG pc 198 #endif 199 200 #elif defined(bpf_target_riscv) 201 202 #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x)) 203 #define __PT_PARM1_REG a0 204 #define __PT_PARM2_REG a1 205 #define __PT_PARM3_REG a2 206 #define __PT_PARM4_REG a3 207 #define __PT_PARM5_REG a4 208 #define __PT_RET_REG ra 209 #define __PT_FP_REG fp 210 #define __PT_RC_REG a5 211 #define __PT_SP_REG sp 212 #define __PT_IP_REG epc 213 214 #endif 215 216 #if defined(bpf_target_defined) 217 218 struct pt_regs; 219 220 /* allow some architecutres to override `struct pt_regs` */ 221 #ifndef __PT_REGS_CAST 222 #define __PT_REGS_CAST(x) (x) 223 #endif 224 225 #define PT_REGS_PARM1(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG) 226 #define PT_REGS_PARM2(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG) 227 #define PT_REGS_PARM3(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG) 228 #define PT_REGS_PARM4(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG) 229 #define PT_REGS_PARM5(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG) 230 #define PT_REGS_RET(x) (__PT_REGS_CAST(x)->__PT_RET_REG) 231 #define PT_REGS_FP(x) (__PT_REGS_CAST(x)->__PT_FP_REG) 232 #define PT_REGS_RC(x) (__PT_REGS_CAST(x)->__PT_RC_REG) 233 #define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG) 234 #define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG) 235 236 #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG) 237 #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG) 238 #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG) 239 #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG) 240 #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG) 241 #define PT_REGS_RET_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RET_REG) 242 #define PT_REGS_FP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_FP_REG) 243 #define PT_REGS_RC_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RC_REG) 244 #define PT_REGS_SP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_SP_REG) 245 #define PT_REGS_IP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_IP_REG) 246 247 #if defined(bpf_target_powerpc) 248 249 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) 250 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 251 252 #elif defined(bpf_target_sparc) 253 254 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) 255 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 256 257 #else 258 259 #define BPF_KPROBE_READ_RET_IP(ip, ctx) \ 260 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) 261 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \ 262 ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) 263 264 #endif 265 266 #else /* defined(bpf_target_defined) */ 267 268 #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 269 #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 270 #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 271 #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 272 #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 273 #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 274 #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 275 #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 276 #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 277 #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 278 279 #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 280 #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 281 #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 282 #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 283 #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 284 #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 285 #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 286 #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 287 #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 288 #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 289 290 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 291 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 292 293 #endif /* defined(bpf_target_defined) */ 294 295 #ifndef ___bpf_concat 296 #define ___bpf_concat(a, b) a ## b 297 #endif 298 #ifndef ___bpf_apply 299 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 300 #endif 301 #ifndef ___bpf_nth 302 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 303 #endif 304 #ifndef ___bpf_narg 305 #define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 306 #endif 307 308 #define ___bpf_ctx_cast0() ctx 309 #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] 310 #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] 311 #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] 312 #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] 313 #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] 314 #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] 315 #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] 316 #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] 317 #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] 318 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] 319 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] 320 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] 321 #define ___bpf_ctx_cast(args...) ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) 322 323 /* 324 * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and 325 * similar kinds of BPF programs, that accept input arguments as a single 326 * pointer to untyped u64 array, where each u64 can actually be a typed 327 * pointer or integer of different size. Instead of requring user to write 328 * manual casts and work with array elements by index, BPF_PROG macro 329 * allows user to declare a list of named and typed input arguments in the 330 * same syntax as for normal C function. All the casting is hidden and 331 * performed transparently, while user code can just assume working with 332 * function arguments of specified type and name. 333 * 334 * Original raw context argument is preserved as well as 'ctx' argument. 335 * This is useful when using BPF helpers that expect original context 336 * as one of the parameters (e.g., for bpf_perf_event_output()). 337 */ 338 #define BPF_PROG(name, args...) \ 339 name(unsigned long long *ctx); \ 340 static __attribute__((always_inline)) typeof(name(0)) \ 341 ____##name(unsigned long long *ctx, ##args); \ 342 typeof(name(0)) name(unsigned long long *ctx) \ 343 { \ 344 _Pragma("GCC diagnostic push") \ 345 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 346 return ____##name(___bpf_ctx_cast(args)); \ 347 _Pragma("GCC diagnostic pop") \ 348 } \ 349 static __attribute__((always_inline)) typeof(name(0)) \ 350 ____##name(unsigned long long *ctx, ##args) 351 352 struct pt_regs; 353 354 #define ___bpf_kprobe_args0() ctx 355 #define ___bpf_kprobe_args1(x) ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) 356 #define ___bpf_kprobe_args2(x, args...) ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) 357 #define ___bpf_kprobe_args3(x, args...) ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) 358 #define ___bpf_kprobe_args4(x, args...) ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) 359 #define ___bpf_kprobe_args5(x, args...) ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) 360 #define ___bpf_kprobe_args(args...) ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) 361 362 /* 363 * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for 364 * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific 365 * low-level way of getting kprobe input arguments from struct pt_regs, and 366 * provides a familiar typed and named function arguments syntax and 367 * semantics of accessing kprobe input paremeters. 368 * 369 * Original struct pt_regs* context is preserved as 'ctx' argument. This might 370 * be necessary when using BPF helpers like bpf_perf_event_output(). 371 */ 372 #define BPF_KPROBE(name, args...) \ 373 name(struct pt_regs *ctx); \ 374 static __attribute__((always_inline)) typeof(name(0)) \ 375 ____##name(struct pt_regs *ctx, ##args); \ 376 typeof(name(0)) name(struct pt_regs *ctx) \ 377 { \ 378 _Pragma("GCC diagnostic push") \ 379 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 380 return ____##name(___bpf_kprobe_args(args)); \ 381 _Pragma("GCC diagnostic pop") \ 382 } \ 383 static __attribute__((always_inline)) typeof(name(0)) \ 384 ____##name(struct pt_regs *ctx, ##args) 385 386 #define ___bpf_kretprobe_args0() ctx 387 #define ___bpf_kretprobe_args1(x) ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx) 388 #define ___bpf_kretprobe_args(args...) ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) 389 390 /* 391 * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional 392 * return value (in addition to `struct pt_regs *ctx`), but no input 393 * arguments, because they will be clobbered by the time probed function 394 * returns. 395 */ 396 #define BPF_KRETPROBE(name, args...) \ 397 name(struct pt_regs *ctx); \ 398 static __attribute__((always_inline)) typeof(name(0)) \ 399 ____##name(struct pt_regs *ctx, ##args); \ 400 typeof(name(0)) name(struct pt_regs *ctx) \ 401 { \ 402 _Pragma("GCC diagnostic push") \ 403 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 404 return ____##name(___bpf_kretprobe_args(args)); \ 405 _Pragma("GCC diagnostic pop") \ 406 } \ 407 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 408 409 #endif 410