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 str_is_empty(const char *s)
185 static inline u16 btf_kind(const struct btf_type *t)
187 return BTF_INFO_KIND(t->info);
190 static inline bool btf_is_enum(const struct btf_type *t)
192 return btf_kind(t) == BTF_KIND_ENUM;
195 static inline bool btf_is_composite(const struct btf_type *t)
197 u16 kind = btf_kind(t);
199 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
202 static inline bool btf_is_array(const struct btf_type *t)
204 return btf_kind(t) == BTF_KIND_ARRAY;
207 static inline bool btf_is_int(const struct btf_type *t)
209 return btf_kind(t) == BTF_KIND_INT;
212 static inline bool btf_is_ptr(const struct btf_type *t)
214 return btf_kind(t) == BTF_KIND_PTR;
217 static inline u8 btf_int_offset(const struct btf_type *t)
219 return BTF_INT_OFFSET(*(u32 *)(t + 1));
222 static inline u8 btf_int_encoding(const struct btf_type *t)
224 return BTF_INT_ENCODING(*(u32 *)(t + 1));
227 static inline bool btf_type_is_scalar(const struct btf_type *t)
229 return btf_type_is_int(t) || btf_type_is_enum(t);
232 static inline bool btf_type_is_typedef(const struct btf_type *t)
234 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
237 static inline bool btf_type_is_func(const struct btf_type *t)
239 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
242 static inline bool btf_type_is_func_proto(const struct btf_type *t)
244 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
247 static inline bool btf_type_is_var(const struct btf_type *t)
249 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
252 static inline bool btf_type_is_type_tag(const struct btf_type *t)
254 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
257 /* union is only a special case of struct:
258 * all its offsetof(member) == 0
260 static inline bool btf_type_is_struct(const struct btf_type *t)
262 u8 kind = BTF_INFO_KIND(t->info);
264 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
267 static inline u16 btf_type_vlen(const struct btf_type *t)
269 return BTF_INFO_VLEN(t->info);
272 static inline u16 btf_vlen(const struct btf_type *t)
274 return btf_type_vlen(t);
277 static inline u16 btf_func_linkage(const struct btf_type *t)
279 return BTF_INFO_VLEN(t->info);
282 static inline bool btf_type_kflag(const struct btf_type *t)
284 return BTF_INFO_KFLAG(t->info);
287 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
288 const struct btf_member *member)
290 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
294 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
295 const struct btf_member *member)
297 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
301 static inline struct btf_member *btf_members(const struct btf_type *t)
303 return (struct btf_member *)(t + 1);
306 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
308 const struct btf_member *m = btf_members(t) + member_idx;
310 return __btf_member_bit_offset(t, m);
313 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
315 const struct btf_member *m = btf_members(t) + member_idx;
317 return __btf_member_bitfield_size(t, m);
320 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
322 return (const struct btf_member *)(t + 1);
325 static inline struct btf_array *btf_array(const struct btf_type *t)
327 return (struct btf_array *)(t + 1);
330 static inline struct btf_enum *btf_enum(const struct btf_type *t)
332 return (struct btf_enum *)(t + 1);
335 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
336 const struct btf_type *t)
338 return (const struct btf_var_secinfo *)(t + 1);
341 static inline struct btf_param *btf_params(const struct btf_type *t)
343 return (struct btf_param *)(t + 1);
346 #ifdef CONFIG_BPF_SYSCALL
349 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
350 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
351 struct btf *btf_parse_vmlinux(void);
352 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
353 bool btf_kfunc_id_set_contains(const struct btf *btf,
354 enum bpf_prog_type prog_type,
355 enum btf_kfunc_type type, u32 kfunc_btf_id);
356 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
357 const struct btf_kfunc_id_set *s);
358 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
359 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
360 struct module *owner);
362 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
367 static inline const char *btf_name_by_offset(const struct btf *btf,
372 static inline bool btf_kfunc_id_set_contains(const struct btf *btf,
373 enum bpf_prog_type prog_type,
374 enum btf_kfunc_type type,
379 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
380 const struct btf_kfunc_id_set *s)
384 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
388 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
389 u32 add_cnt, struct module *owner)