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,
30 struct btf_kfunc_id_set {
34 struct btf_id_set *check_set;
35 struct btf_id_set *acquire_set;
36 struct btf_id_set *release_set;
37 struct btf_id_set *ret_null_set;
39 struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
43 extern const struct file_operations btf_fops;
45 void btf_get(struct btf *btf);
46 void btf_put(struct btf *btf);
47 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
48 struct btf *btf_get_by_fd(int fd);
49 int btf_get_info_by_fd(const struct btf *btf,
50 const union bpf_attr *attr,
51 union bpf_attr __user *uattr);
52 /* Figure out the size of a type_id. If type_id is a modifier
53 * (e.g. const), it will be resolved to find out the type with size.
56 * In describing "const void *", type_id is "const" and "const"
57 * refers to "void *". The return type will be "void *".
59 * If type_id is a simple "int", then return type will be "int".
61 * @btf: struct btf object
62 * @type_id: Find out the size of type_id. The type_id of the return
63 * type is set to *type_id.
64 * @ret_size: It can be NULL. If not NULL, the size of the return
65 * type is set to *ret_size.
66 * Return: The btf_type (resolved to another type with size info if needed).
67 * NULL is returned if type_id itself does not have size info
68 * (e.g. void) or it cannot be resolved to another type that
70 * *type_id and *ret_size will not be changed in the
73 const struct btf_type *btf_type_id_size(const struct btf *btf,
78 * Options to control show behaviour.
79 * - BTF_SHOW_COMPACT: no formatting around type information
80 * - BTF_SHOW_NONAME: no struct/union member names/types
81 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
83 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
84 * are not displayed by default
85 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
86 * data before displaying it.
88 #define BTF_SHOW_COMPACT BTF_F_COMPACT
89 #define BTF_SHOW_NONAME BTF_F_NONAME
90 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
91 #define BTF_SHOW_ZERO BTF_F_ZERO
92 #define BTF_SHOW_UNSAFE (1ULL << 4)
94 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
96 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
97 struct seq_file *m, u64 flags);
100 * Copy len bytes of string representation of obj of BTF type_id into buf.
102 * @btf: struct btf object
103 * @type_id: type id of type obj points to
104 * @obj: pointer to typed data
105 * @buf: buffer to write to
106 * @len: maximum length to write to buf
107 * @flags: show options (see above)
109 * Return: length that would have been/was copied as per snprintf, or
112 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
113 char *buf, int len, u64 flags);
115 int btf_get_fd_by_id(u32 id);
116 u32 btf_obj_id(const struct btf *btf);
117 bool btf_is_kernel(const struct btf *btf);
118 bool btf_is_module(const struct btf *btf);
119 struct module *btf_try_get_module(const struct btf *btf);
120 u32 btf_nr_types(const struct btf *btf);
121 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
122 const struct btf_member *m,
123 u32 expected_offset, u32 expected_size);
124 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
125 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
126 bool btf_type_is_void(const struct btf_type *t);
127 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
128 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
129 u32 id, u32 *res_id);
130 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
131 u32 id, u32 *res_id);
132 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
133 u32 id, u32 *res_id);
134 const struct btf_type *
135 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
137 const char *btf_type_str(const struct btf_type *t);
139 #define for_each_member(i, struct_type, member) \
140 for (i = 0, member = btf_type_member(struct_type); \
141 i < btf_type_vlen(struct_type); \
144 #define for_each_vsi(i, datasec_type, member) \
145 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
146 i < btf_type_vlen(datasec_type); \
149 static inline bool btf_type_is_ptr(const struct btf_type *t)
151 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
154 static inline bool btf_type_is_int(const struct btf_type *t)
156 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
159 static inline bool btf_type_is_small_int(const struct btf_type *t)
161 return btf_type_is_int(t) && t->size <= sizeof(u64);
164 static inline bool btf_type_is_enum(const struct btf_type *t)
166 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
169 static inline bool str_is_empty(const char *s)
174 static inline u16 btf_kind(const struct btf_type *t)
176 return BTF_INFO_KIND(t->info);
179 static inline bool btf_is_enum(const struct btf_type *t)
181 return btf_kind(t) == BTF_KIND_ENUM;
184 static inline bool btf_is_composite(const struct btf_type *t)
186 u16 kind = btf_kind(t);
188 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
191 static inline bool btf_is_array(const struct btf_type *t)
193 return btf_kind(t) == BTF_KIND_ARRAY;
196 static inline bool btf_is_int(const struct btf_type *t)
198 return btf_kind(t) == BTF_KIND_INT;
201 static inline bool btf_is_ptr(const struct btf_type *t)
203 return btf_kind(t) == BTF_KIND_PTR;
206 static inline u8 btf_int_offset(const struct btf_type *t)
208 return BTF_INT_OFFSET(*(u32 *)(t + 1));
211 static inline u8 btf_int_encoding(const struct btf_type *t)
213 return BTF_INT_ENCODING(*(u32 *)(t + 1));
216 static inline bool btf_type_is_scalar(const struct btf_type *t)
218 return btf_type_is_int(t) || btf_type_is_enum(t);
221 static inline bool btf_type_is_typedef(const struct btf_type *t)
223 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
226 static inline bool btf_type_is_func(const struct btf_type *t)
228 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
231 static inline bool btf_type_is_func_proto(const struct btf_type *t)
233 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
236 static inline bool btf_type_is_var(const struct btf_type *t)
238 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
241 static inline bool btf_type_is_type_tag(const struct btf_type *t)
243 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
246 /* union is only a special case of struct:
247 * all its offsetof(member) == 0
249 static inline bool btf_type_is_struct(const struct btf_type *t)
251 u8 kind = BTF_INFO_KIND(t->info);
253 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
256 static inline u16 btf_type_vlen(const struct btf_type *t)
258 return BTF_INFO_VLEN(t->info);
261 static inline u16 btf_vlen(const struct btf_type *t)
263 return btf_type_vlen(t);
266 static inline u16 btf_func_linkage(const struct btf_type *t)
268 return BTF_INFO_VLEN(t->info);
271 static inline bool btf_type_kflag(const struct btf_type *t)
273 return BTF_INFO_KFLAG(t->info);
276 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
277 const struct btf_member *member)
279 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
283 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
284 const struct btf_member *member)
286 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
290 static inline struct btf_member *btf_members(const struct btf_type *t)
292 return (struct btf_member *)(t + 1);
295 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
297 const struct btf_member *m = btf_members(t) + member_idx;
299 return __btf_member_bit_offset(t, m);
302 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
304 const struct btf_member *m = btf_members(t) + member_idx;
306 return __btf_member_bitfield_size(t, m);
309 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
311 return (const struct btf_member *)(t + 1);
314 static inline struct btf_array *btf_array(const struct btf_type *t)
316 return (struct btf_array *)(t + 1);
319 static inline struct btf_enum *btf_enum(const struct btf_type *t)
321 return (struct btf_enum *)(t + 1);
324 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
325 const struct btf_type *t)
327 return (const struct btf_var_secinfo *)(t + 1);
330 static inline struct btf_param *btf_params(const struct btf_type *t)
332 return (struct btf_param *)(t + 1);
335 #ifdef CONFIG_BPF_SYSCALL
338 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
339 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
340 struct btf *btf_parse_vmlinux(void);
341 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
342 bool btf_kfunc_id_set_contains(const struct btf *btf,
343 enum bpf_prog_type prog_type,
344 enum btf_kfunc_type type, u32 kfunc_btf_id);
345 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
346 const struct btf_kfunc_id_set *s);
348 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
353 static inline const char *btf_name_by_offset(const struct btf *btf,
358 static inline bool btf_kfunc_id_set_contains(const struct btf *btf,
359 enum bpf_prog_type prog_type,
360 enum btf_kfunc_type type,
365 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
366 const struct btf_kfunc_id_set *s)