1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
7 #include <linux/types.h>
8 #include <linux/bpfptr.h>
9 #include <uapi/linux/btf.h>
10 #include <uapi/linux/bpf.h>
12 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
13 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
17 BTF_KFUNC_TYPE_ACQUIRE,
18 BTF_KFUNC_TYPE_RELEASE,
19 BTF_KFUNC_TYPE_RET_NULL,
20 BTF_KFUNC_TYPE_KPTR_ACQUIRE,
31 struct btf_kfunc_id_set {
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;
41 struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
45 struct btf_id_dtor_kfunc {
50 typedef void (*btf_dtor_kfunc_t)(void *);
52 extern const struct file_operations btf_fops;
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.
65 * In describing "const void *", type_id is "const" and "const"
66 * refers to "void *". The return type will be "void *".
68 * If type_id is a simple "int", then return type will be "int".
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
79 * *type_id and *ret_size will not be changed in the
82 const struct btf_type *btf_type_id_size(const struct btf *btf,
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;
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.
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)
103 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
105 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
106 struct seq_file *m, u64 flags);
109 * Copy len bytes of string representation of obj of BTF type_id into buf.
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)
118 * Return: length that would have been/was copied as per snprintf, or
121 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
122 char *buf, int len, u64 flags);
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,
148 const char *btf_type_str(const struct btf_type *t);
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); \
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); \
160 static inline bool btf_type_is_ptr(const struct btf_type *t)
162 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
165 static inline bool btf_type_is_int(const struct btf_type *t)
167 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
170 static inline bool btf_type_is_small_int(const struct btf_type *t)
172 return btf_type_is_int(t) && t->size <= sizeof(u64);
175 static inline bool btf_type_is_enum(const struct btf_type *t)
177 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
180 static inline bool btf_is_any_enum(const struct btf_type *t)
182 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
183 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
186 static inline bool btf_kind_core_compat(const struct btf_type *t1,
187 const struct btf_type *t2)
189 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
190 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
193 static inline bool str_is_empty(const char *s)
198 static inline u16 btf_kind(const struct btf_type *t)
200 return BTF_INFO_KIND(t->info);
203 static inline bool btf_is_enum(const struct btf_type *t)
205 return btf_kind(t) == BTF_KIND_ENUM;
208 static inline bool btf_is_enum64(const struct btf_type *t)
210 return btf_kind(t) == BTF_KIND_ENUM64;
213 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
215 return ((u64)e->val_hi32 << 32) | e->val_lo32;
218 static inline bool btf_is_composite(const struct btf_type *t)
220 u16 kind = btf_kind(t);
222 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
225 static inline bool btf_is_array(const struct btf_type *t)
227 return btf_kind(t) == BTF_KIND_ARRAY;
230 static inline bool btf_is_int(const struct btf_type *t)
232 return btf_kind(t) == BTF_KIND_INT;
235 static inline bool btf_is_ptr(const struct btf_type *t)
237 return btf_kind(t) == BTF_KIND_PTR;
240 static inline u8 btf_int_offset(const struct btf_type *t)
242 return BTF_INT_OFFSET(*(u32 *)(t + 1));
245 static inline u8 btf_int_encoding(const struct btf_type *t)
247 return BTF_INT_ENCODING(*(u32 *)(t + 1));
250 static inline bool btf_type_is_scalar(const struct btf_type *t)
252 return btf_type_is_int(t) || btf_type_is_enum(t);
255 static inline bool btf_type_is_typedef(const struct btf_type *t)
257 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
260 static inline bool btf_type_is_func(const struct btf_type *t)
262 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
265 static inline bool btf_type_is_func_proto(const struct btf_type *t)
267 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
270 static inline bool btf_type_is_var(const struct btf_type *t)
272 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
275 static inline bool btf_type_is_type_tag(const struct btf_type *t)
277 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
280 /* union is only a special case of struct:
281 * all its offsetof(member) == 0
283 static inline bool btf_type_is_struct(const struct btf_type *t)
285 u8 kind = BTF_INFO_KIND(t->info);
287 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
290 static inline u16 btf_type_vlen(const struct btf_type *t)
292 return BTF_INFO_VLEN(t->info);
295 static inline u16 btf_vlen(const struct btf_type *t)
297 return btf_type_vlen(t);
300 static inline u16 btf_func_linkage(const struct btf_type *t)
302 return BTF_INFO_VLEN(t->info);
305 static inline bool btf_type_kflag(const struct btf_type *t)
307 return BTF_INFO_KFLAG(t->info);
310 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
311 const struct btf_member *member)
313 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
317 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
318 const struct btf_member *member)
320 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
324 static inline struct btf_member *btf_members(const struct btf_type *t)
326 return (struct btf_member *)(t + 1);
329 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
331 const struct btf_member *m = btf_members(t) + member_idx;
333 return __btf_member_bit_offset(t, m);
336 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
338 const struct btf_member *m = btf_members(t) + member_idx;
340 return __btf_member_bitfield_size(t, m);
343 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
345 return (const struct btf_member *)(t + 1);
348 static inline struct btf_array *btf_array(const struct btf_type *t)
350 return (struct btf_array *)(t + 1);
353 static inline struct btf_enum *btf_enum(const struct btf_type *t)
355 return (struct btf_enum *)(t + 1);
358 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
360 return (struct btf_enum64 *)(t + 1);
363 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
364 const struct btf_type *t)
366 return (const struct btf_var_secinfo *)(t + 1);
369 static inline struct btf_param *btf_params(const struct btf_type *t)
371 return (struct btf_param *)(t + 1);
374 #ifdef CONFIG_BPF_SYSCALL
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);
390 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
395 static inline const char *btf_name_by_offset(const struct btf *btf,
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,
407 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
408 const struct btf_kfunc_id_set *s)
412 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
416 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
417 u32 add_cnt, struct module *owner)