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 #else 28 #undef bpf_target_defined 29 #endif 30 31 /* Fall back to what the compiler says */ 32 #ifndef bpf_target_defined 33 #if defined(__x86_64__) 34 #define bpf_target_x86 35 #elif defined(__s390__) 36 #define bpf_target_s390 37 #elif defined(__arm__) 38 #define bpf_target_arm 39 #elif defined(__aarch64__) 40 #define bpf_target_arm64 41 #elif defined(__mips__) 42 #define bpf_target_mips 43 #elif defined(__powerpc__) 44 #define bpf_target_powerpc 45 #elif defined(__sparc__) 46 #define bpf_target_sparc 47 #endif 48 #endif 49 50 #if defined(bpf_target_x86) 51 52 #if defined(__KERNEL__) || defined(__VMLINUX_H__) 53 #define PT_REGS_PARM1(x) ((x)->di) 54 #define PT_REGS_PARM2(x) ((x)->si) 55 #define PT_REGS_PARM3(x) ((x)->dx) 56 #define PT_REGS_PARM4(x) ((x)->cx) 57 #define PT_REGS_PARM5(x) ((x)->r8) 58 #define PT_REGS_RET(x) ((x)->sp) 59 #define PT_REGS_FP(x) ((x)->bp) 60 #define PT_REGS_RC(x) ((x)->ax) 61 #define PT_REGS_SP(x) ((x)->sp) 62 #define PT_REGS_IP(x) ((x)->ip) 63 #else 64 #ifdef __i386__ 65 /* i386 kernel is built with -mregparm=3 */ 66 #define PT_REGS_PARM1(x) ((x)->eax) 67 #define PT_REGS_PARM2(x) ((x)->edx) 68 #define PT_REGS_PARM3(x) ((x)->ecx) 69 #define PT_REGS_PARM4(x) 0 70 #define PT_REGS_PARM5(x) 0 71 #define PT_REGS_RET(x) ((x)->esp) 72 #define PT_REGS_FP(x) ((x)->ebp) 73 #define PT_REGS_RC(x) ((x)->eax) 74 #define PT_REGS_SP(x) ((x)->esp) 75 #define PT_REGS_IP(x) ((x)->eip) 76 #else 77 #define PT_REGS_PARM1(x) ((x)->rdi) 78 #define PT_REGS_PARM2(x) ((x)->rsi) 79 #define PT_REGS_PARM3(x) ((x)->rdx) 80 #define PT_REGS_PARM4(x) ((x)->rcx) 81 #define PT_REGS_PARM5(x) ((x)->r8) 82 #define PT_REGS_RET(x) ((x)->rsp) 83 #define PT_REGS_FP(x) ((x)->rbp) 84 #define PT_REGS_RC(x) ((x)->rax) 85 #define PT_REGS_SP(x) ((x)->rsp) 86 #define PT_REGS_IP(x) ((x)->rip) 87 #endif 88 #endif 89 90 #elif defined(bpf_target_s390) 91 92 /* s390 provides user_pt_regs instead of struct pt_regs to userspace */ 93 struct pt_regs; 94 #define PT_REGS_S390 const volatile user_pt_regs 95 #define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2]) 96 #define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3]) 97 #define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4]) 98 #define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5]) 99 #define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6]) 100 #define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14]) 101 /* Works only with CONFIG_FRAME_POINTER */ 102 #define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11]) 103 #define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2]) 104 #define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15]) 105 #define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr) 106 107 #elif defined(bpf_target_arm) 108 109 #define PT_REGS_PARM1(x) ((x)->uregs[0]) 110 #define PT_REGS_PARM2(x) ((x)->uregs[1]) 111 #define PT_REGS_PARM3(x) ((x)->uregs[2]) 112 #define PT_REGS_PARM4(x) ((x)->uregs[3]) 113 #define PT_REGS_PARM5(x) ((x)->uregs[4]) 114 #define PT_REGS_RET(x) ((x)->uregs[14]) 115 #define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */ 116 #define PT_REGS_RC(x) ((x)->uregs[0]) 117 #define PT_REGS_SP(x) ((x)->uregs[13]) 118 #define PT_REGS_IP(x) ((x)->uregs[12]) 119 120 #elif defined(bpf_target_arm64) 121 122 /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ 123 struct pt_regs; 124 #define PT_REGS_ARM64 const volatile struct user_pt_regs 125 #define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0]) 126 #define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1]) 127 #define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2]) 128 #define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3]) 129 #define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4]) 130 #define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30]) 131 /* Works only with CONFIG_FRAME_POINTER */ 132 #define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29]) 133 #define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0]) 134 #define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp) 135 #define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc) 136 137 #elif defined(bpf_target_mips) 138 139 #define PT_REGS_PARM1(x) ((x)->regs[4]) 140 #define PT_REGS_PARM2(x) ((x)->regs[5]) 141 #define PT_REGS_PARM3(x) ((x)->regs[6]) 142 #define PT_REGS_PARM4(x) ((x)->regs[7]) 143 #define PT_REGS_PARM5(x) ((x)->regs[8]) 144 #define PT_REGS_RET(x) ((x)->regs[31]) 145 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */ 146 #define PT_REGS_RC(x) ((x)->regs[1]) 147 #define PT_REGS_SP(x) ((x)->regs[29]) 148 #define PT_REGS_IP(x) ((x)->cp0_epc) 149 150 #elif defined(bpf_target_powerpc) 151 152 #define PT_REGS_PARM1(x) ((x)->gpr[3]) 153 #define PT_REGS_PARM2(x) ((x)->gpr[4]) 154 #define PT_REGS_PARM3(x) ((x)->gpr[5]) 155 #define PT_REGS_PARM4(x) ((x)->gpr[6]) 156 #define PT_REGS_PARM5(x) ((x)->gpr[7]) 157 #define PT_REGS_RC(x) ((x)->gpr[3]) 158 #define PT_REGS_SP(x) ((x)->sp) 159 #define PT_REGS_IP(x) ((x)->nip) 160 161 #elif defined(bpf_target_sparc) 162 163 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) 164 #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) 165 #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) 166 #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) 167 #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) 168 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) 169 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) 170 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) 171 172 /* Should this also be a bpf_target check for the sparc case? */ 173 #if defined(__arch64__) 174 #define PT_REGS_IP(x) ((x)->tpc) 175 #else 176 #define PT_REGS_IP(x) ((x)->pc) 177 #endif 178 179 #endif 180 181 #if defined(bpf_target_powerpc) 182 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) 183 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 184 #elif defined(bpf_target_sparc) 185 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) 186 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 187 #else 188 #define BPF_KPROBE_READ_RET_IP(ip, ctx) \ 189 ({ bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) 190 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \ 191 ({ bpf_probe_read(&(ip), sizeof(ip), \ 192 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) 193 #endif 194 195 #define ___bpf_concat(a, b) a ## b 196 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 197 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 198 #define ___bpf_narg(...) \ 199 ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 200 #define ___bpf_empty(...) \ 201 ___bpf_nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0) 202 203 #define ___bpf_ctx_cast0() ctx 204 #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] 205 #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] 206 #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] 207 #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] 208 #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] 209 #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] 210 #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] 211 #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] 212 #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] 213 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] 214 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] 215 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] 216 #define ___bpf_ctx_cast(args...) \ 217 ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) 218 219 /* 220 * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and 221 * similar kinds of BPF programs, that accept input arguments as a single 222 * pointer to untyped u64 array, where each u64 can actually be a typed 223 * pointer or integer of different size. Instead of requring user to write 224 * manual casts and work with array elements by index, BPF_PROG macro 225 * allows user to declare a list of named and typed input arguments in the 226 * same syntax as for normal C function. All the casting is hidden and 227 * performed transparently, while user code can just assume working with 228 * function arguments of specified type and name. 229 * 230 * Original raw context argument is preserved as well as 'ctx' argument. 231 * This is useful when using BPF helpers that expect original context 232 * as one of the parameters (e.g., for bpf_perf_event_output()). 233 */ 234 #define BPF_PROG(name, args...) \ 235 name(unsigned long long *ctx); \ 236 static __attribute__((always_inline)) typeof(name(0)) \ 237 ____##name(unsigned long long *ctx, ##args); \ 238 typeof(name(0)) name(unsigned long long *ctx) \ 239 { \ 240 _Pragma("GCC diagnostic push") \ 241 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 242 return ____##name(___bpf_ctx_cast(args)); \ 243 _Pragma("GCC diagnostic pop") \ 244 } \ 245 static __attribute__((always_inline)) typeof(name(0)) \ 246 ____##name(unsigned long long *ctx, ##args) 247 248 struct pt_regs; 249 250 #define ___bpf_kprobe_args0() ctx 251 #define ___bpf_kprobe_args1(x) \ 252 ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) 253 #define ___bpf_kprobe_args2(x, args...) \ 254 ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) 255 #define ___bpf_kprobe_args3(x, args...) \ 256 ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) 257 #define ___bpf_kprobe_args4(x, args...) \ 258 ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) 259 #define ___bpf_kprobe_args5(x, args...) \ 260 ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) 261 #define ___bpf_kprobe_args(args...) \ 262 ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) 263 264 /* 265 * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for 266 * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific 267 * low-level way of getting kprobe input arguments from struct pt_regs, and 268 * provides a familiar typed and named function arguments syntax and 269 * semantics of accessing kprobe input paremeters. 270 * 271 * Original struct pt_regs* context is preserved as 'ctx' argument. This might 272 * be necessary when using BPF helpers like bpf_perf_event_output(). 273 */ 274 #define BPF_KPROBE(name, args...) \ 275 name(struct pt_regs *ctx); \ 276 static __attribute__((always_inline)) typeof(name(0)) \ 277 ____##name(struct pt_regs *ctx, ##args); \ 278 typeof(name(0)) name(struct pt_regs *ctx) \ 279 { \ 280 _Pragma("GCC diagnostic push") \ 281 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 282 return ____##name(___bpf_kprobe_args(args)); \ 283 _Pragma("GCC diagnostic pop") \ 284 } \ 285 static __attribute__((always_inline)) typeof(name(0)) \ 286 ____##name(struct pt_regs *ctx, ##args) 287 288 #define ___bpf_kretprobe_args0() ctx 289 #define ___bpf_kretprobe_args1(x) \ 290 ___bpf_kretprobe_args0(), (void *)PT_REGS_RET(ctx) 291 #define ___bpf_kretprobe_args(args...) \ 292 ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) 293 294 /* 295 * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional 296 * return value (in addition to `struct pt_regs *ctx`), but no input 297 * arguments, because they will be clobbered by the time probed function 298 * returns. 299 */ 300 #define BPF_KRETPROBE(name, args...) \ 301 name(struct pt_regs *ctx); \ 302 static __attribute__((always_inline)) typeof(name(0)) \ 303 ____##name(struct pt_regs *ctx, ##args); \ 304 typeof(name(0)) name(struct pt_regs *ctx) \ 305 { \ 306 _Pragma("GCC diagnostic push") \ 307 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 308 return ____##name(___bpf_kretprobe_args(args)); \ 309 _Pragma("GCC diagnostic pop") \ 310 } \ 311 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 312 313 #endif 314