1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun #ifndef _PERF_BPF_H 3*4882a593Smuzhiyun #define _PERF_BPF_H 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun #include <uapi/linux/bpf.h> 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun /* 8*4882a593Smuzhiyun * A helper structure used by eBPF C program to describe map attributes to 9*4882a593Smuzhiyun * elf_bpf loader, taken from tools/testing/selftests/bpf/bpf_helpers.h: 10*4882a593Smuzhiyun */ 11*4882a593Smuzhiyun struct bpf_map { 12*4882a593Smuzhiyun unsigned int type; 13*4882a593Smuzhiyun unsigned int key_size; 14*4882a593Smuzhiyun unsigned int value_size; 15*4882a593Smuzhiyun unsigned int max_entries; 16*4882a593Smuzhiyun unsigned int map_flags; 17*4882a593Smuzhiyun unsigned int inner_map_idx; 18*4882a593Smuzhiyun unsigned int numa_node; 19*4882a593Smuzhiyun }; 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun #define bpf_map(name, _type, type_key, type_val, _max_entries) \ 22*4882a593Smuzhiyun struct bpf_map SEC("maps") name = { \ 23*4882a593Smuzhiyun .type = BPF_MAP_TYPE_##_type, \ 24*4882a593Smuzhiyun .key_size = sizeof(type_key), \ 25*4882a593Smuzhiyun .value_size = sizeof(type_val), \ 26*4882a593Smuzhiyun .max_entries = _max_entries, \ 27*4882a593Smuzhiyun }; \ 28*4882a593Smuzhiyun struct ____btf_map_##name { \ 29*4882a593Smuzhiyun type_key key; \ 30*4882a593Smuzhiyun type_val value; \ 31*4882a593Smuzhiyun }; \ 32*4882a593Smuzhiyun struct ____btf_map_##name __attribute__((section(".maps." #name), used)) \ 33*4882a593Smuzhiyun ____btf_map_##name = { } 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun /* 36*4882a593Smuzhiyun * FIXME: this should receive .max_entries as a parameter, as careful 37*4882a593Smuzhiyun * tuning of these limits is needed to avoid hitting limits that 38*4882a593Smuzhiyun * prevents other BPF constructs, such as tracepoint handlers, 39*4882a593Smuzhiyun * to get installed, with cryptic messages from libbpf, etc. 40*4882a593Smuzhiyun * For the current need, 'perf trace --filter-pids', 64 should 41*4882a593Smuzhiyun * be good enough, but this surely needs to be revisited. 42*4882a593Smuzhiyun */ 43*4882a593Smuzhiyun #define pid_map(name, value_type) bpf_map(name, HASH, pid_t, value_type, 64) 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun static int (*bpf_map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags) = (void *)BPF_FUNC_map_update_elem; 46*4882a593Smuzhiyun static void *(*bpf_map_lookup_elem)(struct bpf_map *map, void *key) = (void *)BPF_FUNC_map_lookup_elem; 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun static void (*bpf_tail_call)(void *ctx, void *map, int index) = (void *)BPF_FUNC_tail_call; 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun #define SEC(NAME) __attribute__((section(NAME), used)) 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun #define probe(function, vars) \ 53*4882a593Smuzhiyun SEC(#function "=" #function " " #vars) function 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun #define syscall_enter(name) \ 56*4882a593Smuzhiyun SEC("syscalls:sys_enter_" #name) syscall_enter_ ## name 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun #define syscall_exit(name) \ 59*4882a593Smuzhiyun SEC("syscalls:sys_exit_" #name) syscall_exit_ ## name 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun #define license(name) \ 62*4882a593Smuzhiyun char _license[] SEC("license") = #name; \ 63*4882a593Smuzhiyun int _version SEC("version") = LINUX_VERSION_CODE; 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read; 66*4882a593Smuzhiyun static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str; 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output; 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun #endif /* _PERF_BPF_H */ 71