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 btf_type_is_scalar(const struct btf_type *t)
149 return btf_type_is_int(t) || btf_type_is_enum(t);
152 static inline bool btf_type_is_typedef(const struct btf_type *t)
154 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
157 static inline bool btf_type_is_func(const struct btf_type *t)
159 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
162 static inline bool btf_type_is_func_proto(const struct btf_type *t)
164 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
167 static inline bool btf_type_is_var(const struct btf_type *t)
169 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
172 /* union is only a special case of struct:
173 * all its offsetof(member) == 0
175 static inline bool btf_type_is_struct(const struct btf_type *t)
177 u8 kind = BTF_INFO_KIND(t->info);
179 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
182 static inline u16 btf_type_vlen(const struct btf_type *t)
184 return BTF_INFO_VLEN(t->info);
187 static inline u16 btf_func_linkage(const struct btf_type *t)
189 return BTF_INFO_VLEN(t->info);
192 static inline bool btf_type_kflag(const struct btf_type *t)
194 return BTF_INFO_KFLAG(t->info);
197 static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
198 const struct btf_member *member)
200 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
204 static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
205 const struct btf_member *member)
207 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
211 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
213 return (const struct btf_member *)(t + 1);
216 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
217 const struct btf_type *t)
219 return (const struct btf_var_secinfo *)(t + 1);
222 #ifdef CONFIG_BPF_SYSCALL
225 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
226 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
227 struct btf *btf_parse_vmlinux(void);
228 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
230 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
235 static inline const char *btf_name_by_offset(const struct btf *btf,
242 struct kfunc_btf_id_set {
243 struct list_head list;
244 struct btf_id_set *set;
245 struct module *owner;
248 struct kfunc_btf_id_list;
250 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
251 void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
252 struct kfunc_btf_id_set *s);
253 void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
254 struct kfunc_btf_id_set *s);
255 bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, u32 kfunc_id,
256 struct module *owner);
258 static inline void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
259 struct kfunc_btf_id_set *s)
262 static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
263 struct kfunc_btf_id_set *s)
266 static inline bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist,
267 u32 kfunc_id, struct module *owner)
273 #define DEFINE_KFUNC_BTF_ID_SET(set, name) \
274 struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set), \
277 extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list;
278 extern struct kfunc_btf_id_list prog_test_kfunc_list;