xref: /openbmc/linux/include/linux/btf.h (revision faf69551)
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 struct btf;
16 struct btf_member;
17 struct btf_type;
18 union bpf_attr;
19 struct btf_show;
20 
21 extern const struct file_operations btf_fops;
22 
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.
32  *
33  * For example:
34  * In describing "const void *",  type_id is "const" and "const"
35  * refers to "void *".  The return type will be "void *".
36  *
37  * If type_id is a simple "int", then return type will be "int".
38  *
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
47  *         has size info.
48  *         *type_id and *ret_size will not be changed in the
49  *         NULL return case.
50  */
51 const struct btf_type *btf_type_id_size(const struct btf *btf,
52 					u32 *type_id,
53 					u32 *ret_size);
54 
55 /*
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;
60  *	  equivalent to %px.
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.
65  */
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)
71 
72 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
73 		       struct seq_file *m);
74 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
75 			    struct seq_file *m, u64 flags);
76 
77 /*
78  * Copy len bytes of string representation of obj of BTF type_id into buf.
79  *
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)
86  *
87  * Return: length that would have been/was copied as per snprintf, or
88  *	   negative error.
89  */
90 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
91 			   char *buf, int len, u64 flags);
92 
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,
114 		 u32 *type_size);
115 const char *btf_type_str(const struct btf_type *t);
116 
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);			\
120 	     i++, member++)
121 
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);			\
125 	     i++, member++)
126 
127 static inline bool btf_type_is_ptr(const struct btf_type *t)
128 {
129 	return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
130 }
131 
132 static inline bool btf_type_is_int(const struct btf_type *t)
133 {
134 	return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
135 }
136 
137 static inline bool btf_type_is_small_int(const struct btf_type *t)
138 {
139 	return btf_type_is_int(t) && t->size <= sizeof(u64);
140 }
141 
142 static inline bool btf_type_is_enum(const struct btf_type *t)
143 {
144 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
145 }
146 
147 static inline bool btf_type_is_scalar(const struct btf_type *t)
148 {
149 	return btf_type_is_int(t) || btf_type_is_enum(t);
150 }
151 
152 static inline bool btf_type_is_typedef(const struct btf_type *t)
153 {
154 	return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
155 }
156 
157 static inline bool btf_type_is_func(const struct btf_type *t)
158 {
159 	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
160 }
161 
162 static inline bool btf_type_is_func_proto(const struct btf_type *t)
163 {
164 	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
165 }
166 
167 static inline bool btf_type_is_var(const struct btf_type *t)
168 {
169 	return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
170 }
171 
172 /* union is only a special case of struct:
173  * all its offsetof(member) == 0
174  */
175 static inline bool btf_type_is_struct(const struct btf_type *t)
176 {
177 	u8 kind = BTF_INFO_KIND(t->info);
178 
179 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
180 }
181 
182 static inline u16 btf_type_vlen(const struct btf_type *t)
183 {
184 	return BTF_INFO_VLEN(t->info);
185 }
186 
187 static inline u16 btf_func_linkage(const struct btf_type *t)
188 {
189 	return BTF_INFO_VLEN(t->info);
190 }
191 
192 static inline bool btf_type_kflag(const struct btf_type *t)
193 {
194 	return BTF_INFO_KFLAG(t->info);
195 }
196 
197 static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
198 					const struct btf_member *member)
199 {
200 	return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
201 					   : member->offset;
202 }
203 
204 static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
205 					   const struct btf_member *member)
206 {
207 	return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
208 					   : 0;
209 }
210 
211 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
212 {
213 	return (const struct btf_member *)(t + 1);
214 }
215 
216 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
217 		const struct btf_type *t)
218 {
219 	return (const struct btf_var_secinfo *)(t + 1);
220 }
221 
222 #ifdef CONFIG_BPF_SYSCALL
223 struct bpf_prog;
224 
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);
229 #else
230 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
231 						    u32 type_id)
232 {
233 	return NULL;
234 }
235 static inline const char *btf_name_by_offset(const struct btf *btf,
236 					     u32 offset)
237 {
238 	return NULL;
239 }
240 #endif
241 
242 struct kfunc_btf_id_set {
243 	struct list_head list;
244 	struct btf_id_set *set;
245 	struct module *owner;
246 };
247 
248 struct kfunc_btf_id_list;
249 
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);
257 #else
258 static inline void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
259 					     struct kfunc_btf_id_set *s)
260 {
261 }
262 static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
263 					       struct kfunc_btf_id_set *s)
264 {
265 }
266 static inline bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist,
267 					    u32 kfunc_id, struct module *owner)
268 {
269 	return false;
270 }
271 #endif
272 
273 #define DEFINE_KFUNC_BTF_ID_SET(set, name)                                     \
274 	struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set),     \
275 					 THIS_MODULE }
276 
277 extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list;
278 extern struct kfunc_btf_id_list prog_test_kfunc_list;
279 
280 #endif
281