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)
15 /* These need to be macros, as the expressions are used in assembler input */
16 #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */
17 #define KF_RELEASE (1 << 1) /* kfunc is a release function */
18 #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */
19 #define KF_KPTR_GET (1 << 3) /* kfunc returns reference to a kptr */
20 /* Trusted arguments are those which are meant to be referenced arguments with
21 * unchanged offset. It is used to enforce that pointers obtained from acquire
22 * kfuncs remain unmodified when being passed to helpers taking trusted args.
35 * struct foo *f = alloc_foo(); // Acquire kfunc
36 * struct bar *b = alloc_bar(); // Acquire kfunc
38 * If a kfunc set_foo_data() wants to operate only on the allocated object, it
39 * will set the KF_TRUSTED_ARGS flag, which will prevent unsafe usage like:
41 * set_foo_data(f, 42); // Allowed
42 * set_foo_data(f->next, 42); // Rejected, non-referenced pointer
43 * set_foo_data(&f->next, 42);// Rejected, referenced, but wrong type
44 * set_foo_data(&b->f, 42); // Rejected, referenced, but bad offset
46 * In the final case, usually for the purposes of type matching, it is deduced
47 * by looking at the type of the member at the offset, but due to the
48 * requirement of trusted argument, this deduction will be strict and not done
51 #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
60 struct btf_kfunc_id_set {
62 struct btf_id_set8 *set;
65 struct btf_id_dtor_kfunc {
70 typedef void (*btf_dtor_kfunc_t)(void *);
72 extern const struct file_operations btf_fops;
74 void btf_get(struct btf *btf);
75 void btf_put(struct btf *btf);
76 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
77 struct btf *btf_get_by_fd(int fd);
78 int btf_get_info_by_fd(const struct btf *btf,
79 const union bpf_attr *attr,
80 union bpf_attr __user *uattr);
81 /* Figure out the size of a type_id. If type_id is a modifier
82 * (e.g. const), it will be resolved to find out the type with size.
85 * In describing "const void *", type_id is "const" and "const"
86 * refers to "void *". The return type will be "void *".
88 * If type_id is a simple "int", then return type will be "int".
90 * @btf: struct btf object
91 * @type_id: Find out the size of type_id. The type_id of the return
92 * type is set to *type_id.
93 * @ret_size: It can be NULL. If not NULL, the size of the return
94 * type is set to *ret_size.
95 * Return: The btf_type (resolved to another type with size info if needed).
96 * NULL is returned if type_id itself does not have size info
97 * (e.g. void) or it cannot be resolved to another type that
99 * *type_id and *ret_size will not be changed in the
102 const struct btf_type *btf_type_id_size(const struct btf *btf,
107 * Options to control show behaviour.
108 * - BTF_SHOW_COMPACT: no formatting around type information
109 * - BTF_SHOW_NONAME: no struct/union member names/types
110 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
112 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
113 * are not displayed by default
114 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
115 * data before displaying it.
117 #define BTF_SHOW_COMPACT BTF_F_COMPACT
118 #define BTF_SHOW_NONAME BTF_F_NONAME
119 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
120 #define BTF_SHOW_ZERO BTF_F_ZERO
121 #define BTF_SHOW_UNSAFE (1ULL << 4)
123 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
125 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
126 struct seq_file *m, u64 flags);
129 * Copy len bytes of string representation of obj of BTF type_id into buf.
131 * @btf: struct btf object
132 * @type_id: type id of type obj points to
133 * @obj: pointer to typed data
134 * @buf: buffer to write to
135 * @len: maximum length to write to buf
136 * @flags: show options (see above)
138 * Return: length that would have been/was copied as per snprintf, or
141 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
142 char *buf, int len, u64 flags);
144 int btf_get_fd_by_id(u32 id);
145 u32 btf_obj_id(const struct btf *btf);
146 bool btf_is_kernel(const struct btf *btf);
147 bool btf_is_module(const struct btf *btf);
148 struct module *btf_try_get_module(const struct btf *btf);
149 u32 btf_nr_types(const struct btf *btf);
150 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
151 const struct btf_member *m,
152 u32 expected_offset, u32 expected_size);
153 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
154 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
155 struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf,
156 const struct btf_type *t);
157 bool btf_type_is_void(const struct btf_type *t);
158 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
159 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
160 u32 id, u32 *res_id);
161 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
162 u32 id, u32 *res_id);
163 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
164 u32 id, u32 *res_id);
165 const struct btf_type *
166 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
168 const char *btf_type_str(const struct btf_type *t);
170 #define for_each_member(i, struct_type, member) \
171 for (i = 0, member = btf_type_member(struct_type); \
172 i < btf_type_vlen(struct_type); \
175 #define for_each_vsi(i, datasec_type, member) \
176 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
177 i < btf_type_vlen(datasec_type); \
180 static inline bool btf_type_is_ptr(const struct btf_type *t)
182 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
185 static inline bool btf_type_is_int(const struct btf_type *t)
187 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
190 static inline bool btf_type_is_small_int(const struct btf_type *t)
192 return btf_type_is_int(t) && t->size <= sizeof(u64);
195 static inline bool btf_type_is_enum(const struct btf_type *t)
197 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
200 static inline bool btf_is_any_enum(const struct btf_type *t)
202 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
203 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
206 static inline bool btf_kind_core_compat(const struct btf_type *t1,
207 const struct btf_type *t2)
209 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
210 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
213 static inline bool str_is_empty(const char *s)
218 static inline u16 btf_kind(const struct btf_type *t)
220 return BTF_INFO_KIND(t->info);
223 static inline bool btf_is_enum(const struct btf_type *t)
225 return btf_kind(t) == BTF_KIND_ENUM;
228 static inline bool btf_is_enum64(const struct btf_type *t)
230 return btf_kind(t) == BTF_KIND_ENUM64;
233 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
235 return ((u64)e->val_hi32 << 32) | e->val_lo32;
238 static inline bool btf_is_composite(const struct btf_type *t)
240 u16 kind = btf_kind(t);
242 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
245 static inline bool btf_is_array(const struct btf_type *t)
247 return btf_kind(t) == BTF_KIND_ARRAY;
250 static inline bool btf_is_int(const struct btf_type *t)
252 return btf_kind(t) == BTF_KIND_INT;
255 static inline bool btf_is_ptr(const struct btf_type *t)
257 return btf_kind(t) == BTF_KIND_PTR;
260 static inline u8 btf_int_offset(const struct btf_type *t)
262 return BTF_INT_OFFSET(*(u32 *)(t + 1));
265 static inline u8 btf_int_encoding(const struct btf_type *t)
267 return BTF_INT_ENCODING(*(u32 *)(t + 1));
270 static inline bool btf_type_is_scalar(const struct btf_type *t)
272 return btf_type_is_int(t) || btf_type_is_enum(t);
275 static inline bool btf_type_is_typedef(const struct btf_type *t)
277 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
280 static inline bool btf_type_is_func(const struct btf_type *t)
282 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
285 static inline bool btf_type_is_func_proto(const struct btf_type *t)
287 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
290 static inline bool btf_type_is_var(const struct btf_type *t)
292 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
295 static inline bool btf_type_is_type_tag(const struct btf_type *t)
297 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
300 /* union is only a special case of struct:
301 * all its offsetof(member) == 0
303 static inline bool btf_type_is_struct(const struct btf_type *t)
305 u8 kind = BTF_INFO_KIND(t->info);
307 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
310 static inline u16 btf_type_vlen(const struct btf_type *t)
312 return BTF_INFO_VLEN(t->info);
315 static inline u16 btf_vlen(const struct btf_type *t)
317 return btf_type_vlen(t);
320 static inline u16 btf_func_linkage(const struct btf_type *t)
322 return BTF_INFO_VLEN(t->info);
325 static inline bool btf_type_kflag(const struct btf_type *t)
327 return BTF_INFO_KFLAG(t->info);
330 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
331 const struct btf_member *member)
333 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
337 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
338 const struct btf_member *member)
340 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
344 static inline struct btf_member *btf_members(const struct btf_type *t)
346 return (struct btf_member *)(t + 1);
349 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
351 const struct btf_member *m = btf_members(t) + member_idx;
353 return __btf_member_bit_offset(t, m);
356 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
358 const struct btf_member *m = btf_members(t) + member_idx;
360 return __btf_member_bitfield_size(t, m);
363 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
365 return (const struct btf_member *)(t + 1);
368 static inline struct btf_array *btf_array(const struct btf_type *t)
370 return (struct btf_array *)(t + 1);
373 static inline struct btf_enum *btf_enum(const struct btf_type *t)
375 return (struct btf_enum *)(t + 1);
378 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
380 return (struct btf_enum64 *)(t + 1);
383 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
384 const struct btf_type *t)
386 return (const struct btf_var_secinfo *)(t + 1);
389 static inline struct btf_param *btf_params(const struct btf_type *t)
391 return (struct btf_param *)(t + 1);
394 #ifdef CONFIG_BPF_SYSCALL
397 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
398 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
399 struct btf *btf_parse_vmlinux(void);
400 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
401 u32 *btf_kfunc_id_set_contains(const struct btf *btf,
402 enum bpf_prog_type prog_type,
404 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
405 const struct btf_kfunc_id_set *s);
406 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
407 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
408 struct module *owner);
410 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
415 static inline const char *btf_name_by_offset(const struct btf *btf,
420 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
421 enum bpf_prog_type prog_type,
426 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
427 const struct btf_kfunc_id_set *s)
431 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
435 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
436 u32 add_cnt, struct module *owner)