18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci#undef TRACE_SYSTEM_VAR 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#ifdef CONFIG_BPF_EVENTS 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#undef __entry 88c2ecf20Sopenharmony_ci#define __entry entry 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#undef __get_dynamic_array 118c2ecf20Sopenharmony_ci#define __get_dynamic_array(field) \ 128c2ecf20Sopenharmony_ci ((void *)__entry + (__entry->__data_loc_##field & 0xffff)) 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#undef __get_dynamic_array_len 158c2ecf20Sopenharmony_ci#define __get_dynamic_array_len(field) \ 168c2ecf20Sopenharmony_ci ((__entry->__data_loc_##field >> 16) & 0xffff) 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#undef __get_str 198c2ecf20Sopenharmony_ci#define __get_str(field) ((char *)__get_dynamic_array(field)) 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#undef __get_bitmask 228c2ecf20Sopenharmony_ci#define __get_bitmask(field) (char *)__get_dynamic_array(field) 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#undef __perf_count 258c2ecf20Sopenharmony_ci#define __perf_count(c) (c) 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#undef __perf_task 288c2ecf20Sopenharmony_ci#define __perf_task(t) (t) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* cast any integer, pointer, or small struct to u64 */ 318c2ecf20Sopenharmony_ci#define UINTTYPE(size) \ 328c2ecf20Sopenharmony_ci __typeof__(__builtin_choose_expr(size == 1, (u8)1, \ 338c2ecf20Sopenharmony_ci __builtin_choose_expr(size == 2, (u16)2, \ 348c2ecf20Sopenharmony_ci __builtin_choose_expr(size == 4, (u32)3, \ 358c2ecf20Sopenharmony_ci __builtin_choose_expr(size == 8, (u64)4, \ 368c2ecf20Sopenharmony_ci (void)5))))) 378c2ecf20Sopenharmony_ci#define __CAST_TO_U64(x) ({ \ 388c2ecf20Sopenharmony_ci typeof(x) __src = (x); \ 398c2ecf20Sopenharmony_ci UINTTYPE(sizeof(x)) __dst; \ 408c2ecf20Sopenharmony_ci memcpy(&__dst, &__src, sizeof(__dst)); \ 418c2ecf20Sopenharmony_ci (u64)__dst; }) 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#define __CAST1(a,...) __CAST_TO_U64(a) 448c2ecf20Sopenharmony_ci#define __CAST2(a,...) __CAST_TO_U64(a), __CAST1(__VA_ARGS__) 458c2ecf20Sopenharmony_ci#define __CAST3(a,...) __CAST_TO_U64(a), __CAST2(__VA_ARGS__) 468c2ecf20Sopenharmony_ci#define __CAST4(a,...) __CAST_TO_U64(a), __CAST3(__VA_ARGS__) 478c2ecf20Sopenharmony_ci#define __CAST5(a,...) __CAST_TO_U64(a), __CAST4(__VA_ARGS__) 488c2ecf20Sopenharmony_ci#define __CAST6(a,...) __CAST_TO_U64(a), __CAST5(__VA_ARGS__) 498c2ecf20Sopenharmony_ci#define __CAST7(a,...) __CAST_TO_U64(a), __CAST6(__VA_ARGS__) 508c2ecf20Sopenharmony_ci#define __CAST8(a,...) __CAST_TO_U64(a), __CAST7(__VA_ARGS__) 518c2ecf20Sopenharmony_ci#define __CAST9(a,...) __CAST_TO_U64(a), __CAST8(__VA_ARGS__) 528c2ecf20Sopenharmony_ci#define __CAST10(a,...) __CAST_TO_U64(a), __CAST9(__VA_ARGS__) 538c2ecf20Sopenharmony_ci#define __CAST11(a,...) __CAST_TO_U64(a), __CAST10(__VA_ARGS__) 548c2ecf20Sopenharmony_ci#define __CAST12(a,...) __CAST_TO_U64(a), __CAST11(__VA_ARGS__) 558c2ecf20Sopenharmony_ci/* tracepoints with more than 12 arguments will hit build error */ 568c2ecf20Sopenharmony_ci#define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#undef DECLARE_EVENT_CLASS 598c2ecf20Sopenharmony_ci#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \ 608c2ecf20Sopenharmony_cistatic notrace void \ 618c2ecf20Sopenharmony_ci__bpf_trace_##call(void *__data, proto) \ 628c2ecf20Sopenharmony_ci{ \ 638c2ecf20Sopenharmony_ci struct bpf_prog *prog = __data; \ 648c2ecf20Sopenharmony_ci CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(prog, CAST_TO_U64(args)); \ 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* 688c2ecf20Sopenharmony_ci * This part is compiled out, it is only here as a build time check 698c2ecf20Sopenharmony_ci * to make sure that if the tracepoint handling changes, the 708c2ecf20Sopenharmony_ci * bpf probe will fail to compile unless it too is updated. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_ci#define __DEFINE_EVENT(template, call, proto, args, size) \ 738c2ecf20Sopenharmony_cistatic inline void bpf_test_probe_##call(void) \ 748c2ecf20Sopenharmony_ci{ \ 758c2ecf20Sopenharmony_ci check_trace_callback_type_##call(__bpf_trace_##template); \ 768c2ecf20Sopenharmony_ci} \ 778c2ecf20Sopenharmony_citypedef void (*btf_trace_##call)(void *__data, proto); \ 788c2ecf20Sopenharmony_cistatic union { \ 798c2ecf20Sopenharmony_ci struct bpf_raw_event_map event; \ 808c2ecf20Sopenharmony_ci btf_trace_##call handler; \ 818c2ecf20Sopenharmony_ci} __bpf_trace_tp_map_##call __used \ 828c2ecf20Sopenharmony_ci__section("__bpf_raw_tp_map") = { \ 838c2ecf20Sopenharmony_ci .event = { \ 848c2ecf20Sopenharmony_ci .tp = &__tracepoint_##call, \ 858c2ecf20Sopenharmony_ci .bpf_func = __bpf_trace_##template, \ 868c2ecf20Sopenharmony_ci .num_args = COUNT_ARGS(args), \ 878c2ecf20Sopenharmony_ci .writable_size = size, \ 888c2ecf20Sopenharmony_ci }, \ 898c2ecf20Sopenharmony_ci}; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci#define FIRST(x, ...) x 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#undef DEFINE_EVENT_WRITABLE 948c2ecf20Sopenharmony_ci#define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \ 958c2ecf20Sopenharmony_cistatic inline void bpf_test_buffer_##call(void) \ 968c2ecf20Sopenharmony_ci{ \ 978c2ecf20Sopenharmony_ci /* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \ 988c2ecf20Sopenharmony_ci * BUILD_BUG_ON_ZERO() uses a different mechanism that is not \ 998c2ecf20Sopenharmony_ci * dead-code-eliminated. \ 1008c2ecf20Sopenharmony_ci */ \ 1018c2ecf20Sopenharmony_ci FIRST(proto); \ 1028c2ecf20Sopenharmony_ci (void)BUILD_BUG_ON_ZERO(size != sizeof(*FIRST(args))); \ 1038c2ecf20Sopenharmony_ci} \ 1048c2ecf20Sopenharmony_ci__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size) 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci#undef DEFINE_EVENT 1078c2ecf20Sopenharmony_ci#define DEFINE_EVENT(template, call, proto, args) \ 1088c2ecf20Sopenharmony_ci __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0) 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci#undef DEFINE_EVENT_PRINT 1118c2ecf20Sopenharmony_ci#define DEFINE_EVENT_PRINT(template, name, proto, args, print) \ 1128c2ecf20Sopenharmony_ci DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args)) 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci#include TRACE_INCLUDE(TRACE_INCLUDE_FILE) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci#undef DEFINE_EVENT_WRITABLE 1178c2ecf20Sopenharmony_ci#undef __DEFINE_EVENT 1188c2ecf20Sopenharmony_ci#undef FIRST 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci#endif /* CONFIG_BPF_EVENTS */ 121