Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-microblaze.git] / include / linux / btf.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
3
4 #ifndef _LINUX_BTF_H
5 #define _LINUX_BTF_H 1
6
7 #include <linux/types.h>
8 #include <linux/bpfptr.h>
9 #include <uapi/linux/btf.h>
10 #include <uapi/linux/bpf.h>
11
12 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
13 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
14
15 enum btf_kfunc_type {
16         BTF_KFUNC_TYPE_CHECK,
17         BTF_KFUNC_TYPE_ACQUIRE,
18         BTF_KFUNC_TYPE_RELEASE,
19         BTF_KFUNC_TYPE_RET_NULL,
20         BTF_KFUNC_TYPE_KPTR_ACQUIRE,
21         BTF_KFUNC_TYPE_MAX,
22 };
23
24 struct btf;
25 struct btf_member;
26 struct btf_type;
27 union bpf_attr;
28 struct btf_show;
29 struct btf_id_set;
30
31 struct btf_kfunc_id_set {
32         struct module *owner;
33         union {
34                 struct {
35                         struct btf_id_set *check_set;
36                         struct btf_id_set *acquire_set;
37                         struct btf_id_set *release_set;
38                         struct btf_id_set *ret_null_set;
39                         struct btf_id_set *kptr_acquire_set;
40                 };
41                 struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
42         };
43 };
44
45 struct btf_id_dtor_kfunc {
46         u32 btf_id;
47         u32 kfunc_btf_id;
48 };
49
50 typedef void (*btf_dtor_kfunc_t)(void *);
51
52 extern const struct file_operations btf_fops;
53
54 void btf_get(struct btf *btf);
55 void btf_put(struct btf *btf);
56 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
57 struct btf *btf_get_by_fd(int fd);
58 int btf_get_info_by_fd(const struct btf *btf,
59                        const union bpf_attr *attr,
60                        union bpf_attr __user *uattr);
61 /* Figure out the size of a type_id.  If type_id is a modifier
62  * (e.g. const), it will be resolved to find out the type with size.
63  *
64  * For example:
65  * In describing "const void *",  type_id is "const" and "const"
66  * refers to "void *".  The return type will be "void *".
67  *
68  * If type_id is a simple "int", then return type will be "int".
69  *
70  * @btf: struct btf object
71  * @type_id: Find out the size of type_id. The type_id of the return
72  *           type is set to *type_id.
73  * @ret_size: It can be NULL.  If not NULL, the size of the return
74  *            type is set to *ret_size.
75  * Return: The btf_type (resolved to another type with size info if needed).
76  *         NULL is returned if type_id itself does not have size info
77  *         (e.g. void) or it cannot be resolved to another type that
78  *         has size info.
79  *         *type_id and *ret_size will not be changed in the
80  *         NULL return case.
81  */
82 const struct btf_type *btf_type_id_size(const struct btf *btf,
83                                         u32 *type_id,
84                                         u32 *ret_size);
85
86 /*
87  * Options to control show behaviour.
88  *      - BTF_SHOW_COMPACT: no formatting around type information
89  *      - BTF_SHOW_NONAME: no struct/union member names/types
90  *      - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
91  *        equivalent to %px.
92  *      - BTF_SHOW_ZERO: show zero-valued struct/union members; they
93  *        are not displayed by default
94  *      - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
95  *        data before displaying it.
96  */
97 #define BTF_SHOW_COMPACT        BTF_F_COMPACT
98 #define BTF_SHOW_NONAME         BTF_F_NONAME
99 #define BTF_SHOW_PTR_RAW        BTF_F_PTR_RAW
100 #define BTF_SHOW_ZERO           BTF_F_ZERO
101 #define BTF_SHOW_UNSAFE         (1ULL << 4)
102
103 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
104                        struct seq_file *m);
105 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
106                             struct seq_file *m, u64 flags);
107
108 /*
109  * Copy len bytes of string representation of obj of BTF type_id into buf.
110  *
111  * @btf: struct btf object
112  * @type_id: type id of type obj points to
113  * @obj: pointer to typed data
114  * @buf: buffer to write to
115  * @len: maximum length to write to buf
116  * @flags: show options (see above)
117  *
118  * Return: length that would have been/was copied as per snprintf, or
119  *         negative error.
120  */
121 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
122                            char *buf, int len, u64 flags);
123
124 int btf_get_fd_by_id(u32 id);
125 u32 btf_obj_id(const struct btf *btf);
126 bool btf_is_kernel(const struct btf *btf);
127 bool btf_is_module(const struct btf *btf);
128 struct module *btf_try_get_module(const struct btf *btf);
129 u32 btf_nr_types(const struct btf *btf);
130 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
131                            const struct btf_member *m,
132                            u32 expected_offset, u32 expected_size);
133 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
134 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
135 struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf,
136                                           const struct btf_type *t);
137 bool btf_type_is_void(const struct btf_type *t);
138 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
139 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
140                                                u32 id, u32 *res_id);
141 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
142                                             u32 id, u32 *res_id);
143 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
144                                                  u32 id, u32 *res_id);
145 const struct btf_type *
146 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
147                  u32 *type_size);
148 const char *btf_type_str(const struct btf_type *t);
149
150 #define for_each_member(i, struct_type, member)                 \
151         for (i = 0, member = btf_type_member(struct_type);      \
152              i < btf_type_vlen(struct_type);                    \
153              i++, member++)
154
155 #define for_each_vsi(i, datasec_type, member)                   \
156         for (i = 0, member = btf_type_var_secinfo(datasec_type);        \
157              i < btf_type_vlen(datasec_type);                   \
158              i++, member++)
159
160 static inline bool btf_type_is_ptr(const struct btf_type *t)
161 {
162         return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
163 }
164
165 static inline bool btf_type_is_int(const struct btf_type *t)
166 {
167         return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
168 }
169
170 static inline bool btf_type_is_small_int(const struct btf_type *t)
171 {
172         return btf_type_is_int(t) && t->size <= sizeof(u64);
173 }
174
175 static inline bool btf_type_is_enum(const struct btf_type *t)
176 {
177         return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
178 }
179
180 static inline bool btf_is_any_enum(const struct btf_type *t)
181 {
182         return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
183                BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
184 }
185
186 static inline bool btf_kind_core_compat(const struct btf_type *t1,
187                                         const struct btf_type *t2)
188 {
189         return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
190                (btf_is_any_enum(t1) && btf_is_any_enum(t2));
191 }
192
193 static inline bool str_is_empty(const char *s)
194 {
195         return !s || !s[0];
196 }
197
198 static inline u16 btf_kind(const struct btf_type *t)
199 {
200         return BTF_INFO_KIND(t->info);
201 }
202
203 static inline bool btf_is_enum(const struct btf_type *t)
204 {
205         return btf_kind(t) == BTF_KIND_ENUM;
206 }
207
208 static inline bool btf_is_enum64(const struct btf_type *t)
209 {
210         return btf_kind(t) == BTF_KIND_ENUM64;
211 }
212
213 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
214 {
215         return ((u64)e->val_hi32 << 32) | e->val_lo32;
216 }
217
218 static inline bool btf_is_composite(const struct btf_type *t)
219 {
220         u16 kind = btf_kind(t);
221
222         return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
223 }
224
225 static inline bool btf_is_array(const struct btf_type *t)
226 {
227         return btf_kind(t) == BTF_KIND_ARRAY;
228 }
229
230 static inline bool btf_is_int(const struct btf_type *t)
231 {
232         return btf_kind(t) == BTF_KIND_INT;
233 }
234
235 static inline bool btf_is_ptr(const struct btf_type *t)
236 {
237         return btf_kind(t) == BTF_KIND_PTR;
238 }
239
240 static inline u8 btf_int_offset(const struct btf_type *t)
241 {
242         return BTF_INT_OFFSET(*(u32 *)(t + 1));
243 }
244
245 static inline u8 btf_int_encoding(const struct btf_type *t)
246 {
247         return BTF_INT_ENCODING(*(u32 *)(t + 1));
248 }
249
250 static inline bool btf_type_is_scalar(const struct btf_type *t)
251 {
252         return btf_type_is_int(t) || btf_type_is_enum(t);
253 }
254
255 static inline bool btf_type_is_typedef(const struct btf_type *t)
256 {
257         return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
258 }
259
260 static inline bool btf_type_is_func(const struct btf_type *t)
261 {
262         return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
263 }
264
265 static inline bool btf_type_is_func_proto(const struct btf_type *t)
266 {
267         return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
268 }
269
270 static inline bool btf_type_is_var(const struct btf_type *t)
271 {
272         return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
273 }
274
275 static inline bool btf_type_is_type_tag(const struct btf_type *t)
276 {
277         return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
278 }
279
280 /* union is only a special case of struct:
281  * all its offsetof(member) == 0
282  */
283 static inline bool btf_type_is_struct(const struct btf_type *t)
284 {
285         u8 kind = BTF_INFO_KIND(t->info);
286
287         return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
288 }
289
290 static inline u16 btf_type_vlen(const struct btf_type *t)
291 {
292         return BTF_INFO_VLEN(t->info);
293 }
294
295 static inline u16 btf_vlen(const struct btf_type *t)
296 {
297         return btf_type_vlen(t);
298 }
299
300 static inline u16 btf_func_linkage(const struct btf_type *t)
301 {
302         return BTF_INFO_VLEN(t->info);
303 }
304
305 static inline bool btf_type_kflag(const struct btf_type *t)
306 {
307         return BTF_INFO_KFLAG(t->info);
308 }
309
310 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
311                                           const struct btf_member *member)
312 {
313         return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
314                                            : member->offset;
315 }
316
317 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
318                                              const struct btf_member *member)
319 {
320         return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
321                                            : 0;
322 }
323
324 static inline struct btf_member *btf_members(const struct btf_type *t)
325 {
326         return (struct btf_member *)(t + 1);
327 }
328
329 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
330 {
331         const struct btf_member *m = btf_members(t) + member_idx;
332
333         return __btf_member_bit_offset(t, m);
334 }
335
336 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
337 {
338         const struct btf_member *m = btf_members(t) + member_idx;
339
340         return __btf_member_bitfield_size(t, m);
341 }
342
343 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
344 {
345         return (const struct btf_member *)(t + 1);
346 }
347
348 static inline struct btf_array *btf_array(const struct btf_type *t)
349 {
350         return (struct btf_array *)(t + 1);
351 }
352
353 static inline struct btf_enum *btf_enum(const struct btf_type *t)
354 {
355         return (struct btf_enum *)(t + 1);
356 }
357
358 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
359 {
360         return (struct btf_enum64 *)(t + 1);
361 }
362
363 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
364                 const struct btf_type *t)
365 {
366         return (const struct btf_var_secinfo *)(t + 1);
367 }
368
369 static inline struct btf_param *btf_params(const struct btf_type *t)
370 {
371         return (struct btf_param *)(t + 1);
372 }
373
374 #ifdef CONFIG_BPF_SYSCALL
375 struct bpf_prog;
376
377 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
378 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
379 struct btf *btf_parse_vmlinux(void);
380 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
381 bool btf_kfunc_id_set_contains(const struct btf *btf,
382                                enum bpf_prog_type prog_type,
383                                enum btf_kfunc_type type, u32 kfunc_btf_id);
384 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
385                               const struct btf_kfunc_id_set *s);
386 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
387 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
388                                 struct module *owner);
389 #else
390 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
391                                                     u32 type_id)
392 {
393         return NULL;
394 }
395 static inline const char *btf_name_by_offset(const struct btf *btf,
396                                              u32 offset)
397 {
398         return NULL;
399 }
400 static inline bool btf_kfunc_id_set_contains(const struct btf *btf,
401                                              enum bpf_prog_type prog_type,
402                                              enum btf_kfunc_type type,
403                                              u32 kfunc_btf_id)
404 {
405         return false;
406 }
407 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
408                                             const struct btf_kfunc_id_set *s)
409 {
410         return 0;
411 }
412 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
413 {
414         return -ENOENT;
415 }
416 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
417                                               u32 add_cnt, struct module *owner)
418 {
419         return 0;
420 }
421 #endif
422
423 #endif