1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
3 
4 #ifndef _LINUX_BTF_H
5 #define _LINUX_BTF_H 1
6 
7 #include <linux/types.h>
8 #include <linux/bpfptr.h>
9 #include <uapi/linux/btf.h>
10 #include <uapi/linux/bpf.h>
11 
12 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
13 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
14 
15 enum btf_kfunc_type {
16 	BTF_KFUNC_TYPE_CHECK,
17 	BTF_KFUNC_TYPE_ACQUIRE,
18 	BTF_KFUNC_TYPE_RELEASE,
19 	BTF_KFUNC_TYPE_RET_NULL,
20 	BTF_KFUNC_TYPE_KPTR_ACQUIRE,
21 	BTF_KFUNC_TYPE_MAX,
22 };
23 
24 struct btf;
25 struct btf_member;
26 struct btf_type;
27 union bpf_attr;
28 struct btf_show;
29 struct btf_id_set;
30 
31 struct btf_kfunc_id_set {
32 	struct module *owner;
33 	union {
34 		struct {
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;
40 		};
41 		struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
42 	};
43 };
44 
45 struct btf_id_dtor_kfunc {
46 	u32 btf_id;
47 	u32 kfunc_btf_id;
48 };
49 
50 typedef void (*btf_dtor_kfunc_t)(void *);
51 
52 extern const struct file_operations btf_fops;
53 
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.
63  *
64  * For example:
65  * In describing "const void *",  type_id is "const" and "const"
66  * refers to "void *".  The return type will be "void *".
67  *
68  * If type_id is a simple "int", then return type will be "int".
69  *
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
78  *         has size info.
79  *         *type_id and *ret_size will not be changed in the
80  *         NULL return case.
81  */
82 const struct btf_type *btf_type_id_size(const struct btf *btf,
83 					u32 *type_id,
84 					u32 *ret_size);
85 
86 /*
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;
91  *	  equivalent to %px.
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.
96  */
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)
102 
103 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
104 		       struct seq_file *m);
105 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
106 			    struct seq_file *m, u64 flags);
107 
108 /*
109  * Copy len bytes of string representation of obj of BTF type_id into buf.
110  *
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)
117  *
118  * Return: length that would have been/was copied as per snprintf, or
119  *	   negative error.
120  */
121 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
122 			   char *buf, int len, u64 flags);
123 
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,
147 		 u32 *type_size);
148 const char *btf_type_str(const struct btf_type *t);
149 
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);			\
153 	     i++, member++)
154 
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);			\
158 	     i++, member++)
159 
btf_type_is_ptr(const struct btf_type * t)160 static inline bool btf_type_is_ptr(const struct btf_type *t)
161 {
162 	return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
163 }
164 
btf_type_is_int(const struct btf_type * t)165 static inline bool btf_type_is_int(const struct btf_type *t)
166 {
167 	return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
168 }
169 
btf_type_is_small_int(const struct btf_type * t)170 static inline bool btf_type_is_small_int(const struct btf_type *t)
171 {
172 	return btf_type_is_int(t) && t->size <= sizeof(u64);
173 }
174 
btf_type_is_enum(const struct btf_type * t)175 static inline bool btf_type_is_enum(const struct btf_type *t)
176 {
177 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
178 }
179 
str_is_empty(const char * s)180 static inline bool str_is_empty(const char *s)
181 {
182 	return !s || !s[0];
183 }
184 
btf_kind(const struct btf_type * t)185 static inline u16 btf_kind(const struct btf_type *t)
186 {
187 	return BTF_INFO_KIND(t->info);
188 }
189 
btf_is_enum(const struct btf_type * t)190 static inline bool btf_is_enum(const struct btf_type *t)
191 {
192 	return btf_kind(t) == BTF_KIND_ENUM;
193 }
194 
btf_is_composite(const struct btf_type * t)195 static inline bool btf_is_composite(const struct btf_type *t)
196 {
197 	u16 kind = btf_kind(t);
198 
199 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
200 }
201 
btf_is_array(const struct btf_type * t)202 static inline bool btf_is_array(const struct btf_type *t)
203 {
204 	return btf_kind(t) == BTF_KIND_ARRAY;
205 }
206 
btf_is_int(const struct btf_type * t)207 static inline bool btf_is_int(const struct btf_type *t)
208 {
209 	return btf_kind(t) == BTF_KIND_INT;
210 }
211 
btf_is_ptr(const struct btf_type * t)212 static inline bool btf_is_ptr(const struct btf_type *t)
213 {
214 	return btf_kind(t) == BTF_KIND_PTR;
215 }
216 
btf_int_offset(const struct btf_type * t)217 static inline u8 btf_int_offset(const struct btf_type *t)
218 {
219 	return BTF_INT_OFFSET(*(u32 *)(t + 1));
220 }
221 
btf_int_encoding(const struct btf_type * t)222 static inline u8 btf_int_encoding(const struct btf_type *t)
223 {
224 	return BTF_INT_ENCODING(*(u32 *)(t + 1));
225 }
226 
btf_type_is_scalar(const struct btf_type * t)227 static inline bool btf_type_is_scalar(const struct btf_type *t)
228 {
229 	return btf_type_is_int(t) || btf_type_is_enum(t);
230 }
231 
btf_type_is_typedef(const struct btf_type * t)232 static inline bool btf_type_is_typedef(const struct btf_type *t)
233 {
234 	return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
235 }
236 
btf_type_is_func(const struct btf_type * t)237 static inline bool btf_type_is_func(const struct btf_type *t)
238 {
239 	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
240 }
241 
btf_type_is_func_proto(const struct btf_type * t)242 static inline bool btf_type_is_func_proto(const struct btf_type *t)
243 {
244 	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
245 }
246 
btf_type_is_var(const struct btf_type * t)247 static inline bool btf_type_is_var(const struct btf_type *t)
248 {
249 	return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
250 }
251 
btf_type_is_type_tag(const struct btf_type * t)252 static inline bool btf_type_is_type_tag(const struct btf_type *t)
253 {
254 	return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
255 }
256 
257 /* union is only a special case of struct:
258  * all its offsetof(member) == 0
259  */
btf_type_is_struct(const struct btf_type * t)260 static inline bool btf_type_is_struct(const struct btf_type *t)
261 {
262 	u8 kind = BTF_INFO_KIND(t->info);
263 
264 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
265 }
266 
btf_type_vlen(const struct btf_type * t)267 static inline u16 btf_type_vlen(const struct btf_type *t)
268 {
269 	return BTF_INFO_VLEN(t->info);
270 }
271 
btf_vlen(const struct btf_type * t)272 static inline u16 btf_vlen(const struct btf_type *t)
273 {
274 	return btf_type_vlen(t);
275 }
276 
btf_func_linkage(const struct btf_type * t)277 static inline u16 btf_func_linkage(const struct btf_type *t)
278 {
279 	return BTF_INFO_VLEN(t->info);
280 }
281 
btf_type_kflag(const struct btf_type * t)282 static inline bool btf_type_kflag(const struct btf_type *t)
283 {
284 	return BTF_INFO_KFLAG(t->info);
285 }
286 
__btf_member_bit_offset(const struct btf_type * struct_type,const struct btf_member * member)287 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
288 					  const struct btf_member *member)
289 {
290 	return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
291 					   : member->offset;
292 }
293 
__btf_member_bitfield_size(const struct btf_type * struct_type,const struct btf_member * member)294 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
295 					     const struct btf_member *member)
296 {
297 	return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
298 					   : 0;
299 }
300 
btf_members(const struct btf_type * t)301 static inline struct btf_member *btf_members(const struct btf_type *t)
302 {
303 	return (struct btf_member *)(t + 1);
304 }
305 
btf_member_bit_offset(const struct btf_type * t,u32 member_idx)306 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
307 {
308 	const struct btf_member *m = btf_members(t) + member_idx;
309 
310 	return __btf_member_bit_offset(t, m);
311 }
312 
btf_member_bitfield_size(const struct btf_type * t,u32 member_idx)313 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
314 {
315 	const struct btf_member *m = btf_members(t) + member_idx;
316 
317 	return __btf_member_bitfield_size(t, m);
318 }
319 
btf_type_member(const struct btf_type * t)320 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
321 {
322 	return (const struct btf_member *)(t + 1);
323 }
324 
btf_array(const struct btf_type * t)325 static inline struct btf_array *btf_array(const struct btf_type *t)
326 {
327 	return (struct btf_array *)(t + 1);
328 }
329 
btf_enum(const struct btf_type * t)330 static inline struct btf_enum *btf_enum(const struct btf_type *t)
331 {
332 	return (struct btf_enum *)(t + 1);
333 }
334 
btf_type_var_secinfo(const struct btf_type * t)335 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
336 		const struct btf_type *t)
337 {
338 	return (const struct btf_var_secinfo *)(t + 1);
339 }
340 
btf_params(const struct btf_type * t)341 static inline struct btf_param *btf_params(const struct btf_type *t)
342 {
343 	return (struct btf_param *)(t + 1);
344 }
345 
346 #ifdef CONFIG_BPF_SYSCALL
347 struct bpf_prog;
348 
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);
361 #else
btf_type_by_id(const struct btf * btf,u32 type_id)362 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
363 						    u32 type_id)
364 {
365 	return NULL;
366 }
btf_name_by_offset(const struct btf * btf,u32 offset)367 static inline const char *btf_name_by_offset(const struct btf *btf,
368 					     u32 offset)
369 {
370 	return NULL;
371 }
btf_kfunc_id_set_contains(const struct btf * btf,enum bpf_prog_type prog_type,enum btf_kfunc_type type,u32 kfunc_btf_id)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,
375 					     u32 kfunc_btf_id)
376 {
377 	return false;
378 }
register_btf_kfunc_id_set(enum bpf_prog_type prog_type,const struct btf_kfunc_id_set * s)379 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
380 					    const struct btf_kfunc_id_set *s)
381 {
382 	return 0;
383 }
btf_find_dtor_kfunc(struct btf * btf,u32 btf_id)384 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
385 {
386 	return -ENOENT;
387 }
register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc * dtors,u32 add_cnt,struct module * owner)388 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
389 					      u32 add_cnt, struct module *owner)
390 {
391 	return 0;
392 }
393 #endif
394 
395 #endif
396