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)
21 extern const struct file_operations btf_fops;
23 void btf_get(struct btf *btf);
24 void btf_put(struct btf *btf);
25 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
26 struct btf *btf_get_by_fd(int fd);
27 int btf_get_info_by_fd(const struct btf *btf,
28 const union bpf_attr *attr,
29 union bpf_attr __user *uattr);
30 /* Figure out the size of a type_id. If type_id is a modifier
31 * (e.g. const), it will be resolved to find out the type with size.
34 * In describing "const void *", type_id is "const" and "const"
35 * refers to "void *". The return type will be "void *".
37 * If type_id is a simple "int", then return type will be "int".
39 * @btf: struct btf object
40 * @type_id: Find out the size of type_id. The type_id of the return
41 * type is set to *type_id.
42 * @ret_size: It can be NULL. If not NULL, the size of the return
43 * type is set to *ret_size.
44 * Return: The btf_type (resolved to another type with size info if needed).
45 * NULL is returned if type_id itself does not have size info
46 * (e.g. void) or it cannot be resolved to another type that
48 * *type_id and *ret_size will not be changed in the
51 const struct btf_type *btf_type_id_size(const struct btf *btf,
56 * Options to control show behaviour.
57 * - BTF_SHOW_COMPACT: no formatting around type information
58 * - BTF_SHOW_NONAME: no struct/union member names/types
59 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
61 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
62 * are not displayed by default
63 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
64 * data before displaying it.
66 #define BTF_SHOW_COMPACT BTF_F_COMPACT
67 #define BTF_SHOW_NONAME BTF_F_NONAME
68 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
69 #define BTF_SHOW_ZERO BTF_F_ZERO
70 #define BTF_SHOW_UNSAFE (1ULL << 4)
72 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
74 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
75 struct seq_file *m, u64 flags);
78 * Copy len bytes of string representation of obj of BTF type_id into buf.
80 * @btf: struct btf object
81 * @type_id: type id of type obj points to
82 * @obj: pointer to typed data
83 * @buf: buffer to write to
84 * @len: maximum length to write to buf
85 * @flags: show options (see above)
87 * Return: length that would have been/was copied as per snprintf, or
90 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
91 char *buf, int len, u64 flags);
93 int btf_get_fd_by_id(u32 id);
94 u32 btf_obj_id(const struct btf *btf);
95 bool btf_is_kernel(const struct btf *btf);
96 bool btf_is_module(const struct btf *btf);
97 struct module *btf_try_get_module(const struct btf *btf);
98 u32 btf_nr_types(const struct btf *btf);
99 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
100 const struct btf_member *m,
101 u32 expected_offset, u32 expected_size);
102 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
103 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
104 bool btf_type_is_void(const struct btf_type *t);
105 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
106 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
107 u32 id, u32 *res_id);
108 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
109 u32 id, u32 *res_id);
110 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
111 u32 id, u32 *res_id);
112 const struct btf_type *
113 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
115 const char *btf_type_str(const struct btf_type *t);
117 #define for_each_member(i, struct_type, member) \
118 for (i = 0, member = btf_type_member(struct_type); \
119 i < btf_type_vlen(struct_type); \
122 #define for_each_vsi(i, datasec_type, member) \
123 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
124 i < btf_type_vlen(datasec_type); \
127 static inline bool btf_type_is_ptr(const struct btf_type *t)
129 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
132 static inline bool btf_type_is_int(const struct btf_type *t)
134 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
137 static inline bool btf_type_is_small_int(const struct btf_type *t)
139 return btf_type_is_int(t) && t->size <= sizeof(u64);
142 static inline bool btf_type_is_enum(const struct btf_type *t)
144 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
147 static inline bool str_is_empty(const char *s)
152 static inline u16 btf_kind(const struct btf_type *t)
154 return BTF_INFO_KIND(t->info);
157 static inline bool btf_is_enum(const struct btf_type *t)
159 return btf_kind(t) == BTF_KIND_ENUM;
162 static inline bool btf_is_composite(const struct btf_type *t)
164 u16 kind = btf_kind(t);
166 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
169 static inline bool btf_is_array(const struct btf_type *t)
171 return btf_kind(t) == BTF_KIND_ARRAY;
174 static inline bool btf_is_int(const struct btf_type *t)
176 return btf_kind(t) == BTF_KIND_INT;
179 static inline bool btf_is_ptr(const struct btf_type *t)
181 return btf_kind(t) == BTF_KIND_PTR;
184 static inline u8 btf_int_offset(const struct btf_type *t)
186 return BTF_INT_OFFSET(*(u32 *)(t + 1));
189 static inline u8 btf_int_encoding(const struct btf_type *t)
191 return BTF_INT_ENCODING(*(u32 *)(t + 1));
194 static inline bool btf_type_is_scalar(const struct btf_type *t)
196 return btf_type_is_int(t) || btf_type_is_enum(t);
199 static inline bool btf_type_is_typedef(const struct btf_type *t)
201 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
204 static inline bool btf_type_is_func(const struct btf_type *t)
206 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
209 static inline bool btf_type_is_func_proto(const struct btf_type *t)
211 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
214 static inline bool btf_type_is_var(const struct btf_type *t)
216 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
219 /* union is only a special case of struct:
220 * all its offsetof(member) == 0
222 static inline bool btf_type_is_struct(const struct btf_type *t)
224 u8 kind = BTF_INFO_KIND(t->info);
226 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
229 static inline u16 btf_type_vlen(const struct btf_type *t)
231 return BTF_INFO_VLEN(t->info);
234 static inline u16 btf_vlen(const struct btf_type *t)
236 return btf_type_vlen(t);
239 static inline u16 btf_func_linkage(const struct btf_type *t)
241 return BTF_INFO_VLEN(t->info);
244 static inline bool btf_type_kflag(const struct btf_type *t)
246 return BTF_INFO_KFLAG(t->info);
249 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
250 const struct btf_member *member)
252 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
256 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
257 const struct btf_member *member)
259 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
263 static inline struct btf_member *btf_members(const struct btf_type *t)
265 return (struct btf_member *)(t + 1);
268 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
270 const struct btf_member *m = btf_members(t) + member_idx;
272 return __btf_member_bit_offset(t, m);
275 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
277 const struct btf_member *m = btf_members(t) + member_idx;
279 return __btf_member_bitfield_size(t, m);
282 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
284 return (const struct btf_member *)(t + 1);
287 static inline struct btf_array *btf_array(const struct btf_type *t)
289 return (struct btf_array *)(t + 1);
292 static inline struct btf_enum *btf_enum(const struct btf_type *t)
294 return (struct btf_enum *)(t + 1);
297 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
298 const struct btf_type *t)
300 return (const struct btf_var_secinfo *)(t + 1);
303 #ifdef CONFIG_BPF_SYSCALL
306 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
307 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
308 struct btf *btf_parse_vmlinux(void);
309 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
311 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
316 static inline const char *btf_name_by_offset(const struct btf *btf,
323 struct kfunc_btf_id_set {
324 struct list_head list;
325 struct btf_id_set *set;
326 struct module *owner;
329 struct kfunc_btf_id_list {
330 struct list_head list;
334 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
335 void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
336 struct kfunc_btf_id_set *s);
337 void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
338 struct kfunc_btf_id_set *s);
339 bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, u32 kfunc_id,
340 struct module *owner);
342 extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list;
343 extern struct kfunc_btf_id_list prog_test_kfunc_list;
345 static inline void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
346 struct kfunc_btf_id_set *s)
349 static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
350 struct kfunc_btf_id_set *s)
353 static inline bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist,
354 u32 kfunc_id, struct module *owner)
359 static struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list __maybe_unused;
360 static struct kfunc_btf_id_list prog_test_kfunc_list __maybe_unused;
363 #define DEFINE_KFUNC_BTF_ID_SET(set, name) \
364 struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set), \