18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#ifndef _PERF_BPF_H
38c2ecf20Sopenharmony_ci#define _PERF_BPF_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * A helper structure used by eBPF C program to describe map attributes to
98c2ecf20Sopenharmony_ci * elf_bpf loader, taken from tools/testing/selftests/bpf/bpf_helpers.h:
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_cistruct bpf_map {
128c2ecf20Sopenharmony_ci        unsigned int type;
138c2ecf20Sopenharmony_ci        unsigned int key_size;
148c2ecf20Sopenharmony_ci        unsigned int value_size;
158c2ecf20Sopenharmony_ci        unsigned int max_entries;
168c2ecf20Sopenharmony_ci        unsigned int map_flags;
178c2ecf20Sopenharmony_ci        unsigned int inner_map_idx;
188c2ecf20Sopenharmony_ci        unsigned int numa_node;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define bpf_map(name, _type, type_key, type_val, _max_entries)	\
228c2ecf20Sopenharmony_cistruct bpf_map SEC("maps") name = {				\
238c2ecf20Sopenharmony_ci	.type	     = BPF_MAP_TYPE_##_type,			\
248c2ecf20Sopenharmony_ci	.key_size    = sizeof(type_key),			\
258c2ecf20Sopenharmony_ci	.value_size  = sizeof(type_val),			\
268c2ecf20Sopenharmony_ci	.max_entries = _max_entries,				\
278c2ecf20Sopenharmony_ci};								\
288c2ecf20Sopenharmony_cistruct ____btf_map_##name {					\
298c2ecf20Sopenharmony_ci	type_key key;						\
308c2ecf20Sopenharmony_ci	type_val value;                                 	\
318c2ecf20Sopenharmony_ci};								\
328c2ecf20Sopenharmony_cistruct ____btf_map_##name __attribute__((section(".maps." #name), used)) \
338c2ecf20Sopenharmony_ci	____btf_map_##name = { }
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/*
368c2ecf20Sopenharmony_ci * FIXME: this should receive .max_entries as a parameter, as careful
378c2ecf20Sopenharmony_ci *	  tuning of these limits is needed to avoid hitting limits that
388c2ecf20Sopenharmony_ci *	  prevents other BPF constructs, such as tracepoint handlers,
398c2ecf20Sopenharmony_ci *	  to get installed, with cryptic messages from libbpf, etc.
408c2ecf20Sopenharmony_ci *	  For the current need, 'perf trace --filter-pids', 64 should
418c2ecf20Sopenharmony_ci *	  be good enough, but this surely needs to be revisited.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#define pid_map(name, value_type) bpf_map(name, HASH, pid_t, value_type, 64)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int (*bpf_map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags) = (void *)BPF_FUNC_map_update_elem;
468c2ecf20Sopenharmony_cistatic void *(*bpf_map_lookup_elem)(struct bpf_map *map, void *key) = (void *)BPF_FUNC_map_lookup_elem;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void (*bpf_tail_call)(void *ctx, void *map, int index) = (void *)BPF_FUNC_tail_call;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define SEC(NAME) __attribute__((section(NAME),  used))
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define probe(function, vars) \
538c2ecf20Sopenharmony_ci	SEC(#function "=" #function " " #vars) function
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define syscall_enter(name) \
568c2ecf20Sopenharmony_ci	SEC("syscalls:sys_enter_" #name) syscall_enter_ ## name
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define syscall_exit(name) \
598c2ecf20Sopenharmony_ci	SEC("syscalls:sys_exit_" #name) syscall_exit_ ## name
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define license(name) \
628c2ecf20Sopenharmony_cichar _license[] SEC("license") = #name; \
638c2ecf20Sopenharmony_ciint _version SEC("version") = LINUX_VERSION_CODE;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
668c2ecf20Sopenharmony_cistatic int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#endif /* _PERF_BPF_H */
71