add BPF syscall number for x86 arch
[platform/upstream/bcc.git] / src / cc / libbpf / src / skel_internal.h
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2021 Facebook */
3 #ifndef __SKEL_INTERNAL_H
4 #define __SKEL_INTERNAL_H
5
6 #include <unistd.h>
7 #include <sys/syscall.h>
8 #include <sys/mman.h>
9
10 #ifndef __NR_bpf
11 # if defined(__i386__)
12 #  define __NR_bpf 357
13 # elif defined(__x86_64__)
14 #  define __NR_bpf 321
15 # elif defined(__mips__) && defined(_ABIO32)
16 #  define __NR_bpf 4355
17 # elif defined(__mips__) && defined(_ABIN32)
18 #  define __NR_bpf 6319
19 # elif defined(__mips__) && defined(_ABI64)
20 #  define __NR_bpf 5315
21 # elif defined(__aarch64__)
22 #  define __NR_bpf 280
23 # elif defined(__arm__)
24 #  define __NR_bpf 386
25 # endif
26 #endif
27
28 /* This file is a base header for auto-generated *.lskel.h files.
29  * Its contents will change and may become part of auto-generation in the future.
30  *
31  * The layout of bpf_[map|prog]_desc and bpf_loader_ctx is feature dependent
32  * and will change from one version of libbpf to another and features
33  * requested during loader program generation.
34  */
35 struct bpf_map_desc {
36         union {
37                 /* input for the loader prog */
38                 struct {
39                         __aligned_u64 initial_value;
40                         __u32 max_entries;
41                 };
42                 /* output of the loader prog */
43                 struct {
44                         int map_fd;
45                 };
46         };
47 };
48 struct bpf_prog_desc {
49         int prog_fd;
50 };
51
52 struct bpf_loader_ctx {
53         size_t sz;
54         __u32 log_level;
55         __u32 log_size;
56         __u64 log_buf;
57 };
58
59 struct bpf_load_and_run_opts {
60         struct bpf_loader_ctx *ctx;
61         const void *data;
62         const void *insns;
63         __u32 data_sz;
64         __u32 insns_sz;
65         const char *errstr;
66 };
67
68 static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
69                           unsigned int size)
70 {
71         return syscall(__NR_bpf, cmd, attr, size);
72 }
73
74 static inline int skel_closenz(int fd)
75 {
76         if (fd > 0)
77                 return close(fd);
78         return -EINVAL;
79 }
80
81 static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
82 {
83         int map_fd = -1, prog_fd = -1, key = 0, err;
84         union bpf_attr attr;
85
86         map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1, NULL);
87         if (map_fd < 0) {
88                 opts->errstr = "failed to create loader map";
89                 err = -errno;
90                 goto out;
91         }
92
93         err = bpf_map_update_elem(map_fd, &key, opts->data, 0);
94         if (err < 0) {
95                 opts->errstr = "failed to update loader map";
96                 err = -errno;
97                 goto out;
98         }
99
100         memset(&attr, 0, sizeof(attr));
101         attr.prog_type = BPF_PROG_TYPE_SYSCALL;
102         attr.insns = (long) opts->insns;
103         attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
104         attr.license = (long) "Dual BSD/GPL";
105         memcpy(attr.prog_name, "__loader.prog", sizeof("__loader.prog"));
106         attr.fd_array = (long) &map_fd;
107         attr.log_level = opts->ctx->log_level;
108         attr.log_size = opts->ctx->log_size;
109         attr.log_buf = opts->ctx->log_buf;
110         attr.prog_flags = BPF_F_SLEEPABLE;
111         prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
112         if (prog_fd < 0) {
113                 opts->errstr = "failed to load loader prog";
114                 err = -errno;
115                 goto out;
116         }
117
118         memset(&attr, 0, sizeof(attr));
119         attr.test.prog_fd = prog_fd;
120         attr.test.ctx_in = (long) opts->ctx;
121         attr.test.ctx_size_in = opts->ctx->sz;
122         err = skel_sys_bpf(BPF_PROG_RUN, &attr, sizeof(attr));
123         if (err < 0 || (int)attr.test.retval < 0) {
124                 opts->errstr = "failed to execute loader prog";
125                 if (err < 0) {
126                         err = -errno;
127                 } else {
128                         err = (int)attr.test.retval;
129                         errno = -err;
130                 }
131                 goto out;
132         }
133         err = 0;
134 out:
135         if (map_fd >= 0)
136                 close(map_fd);
137         if (prog_fd >= 0)
138                 close(prog_fd);
139         return err;
140 }
141
142 #endif