Commit | Line | Data |
---|---|---|
eb3f595d MKL |
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> | |
14f267d9 | 8 | #include <linux/bpfptr.h> |
38207291 | 9 | #include <uapi/linux/btf.h> |
c4d0bfb4 | 10 | #include <uapi/linux/bpf.h> |
eb3f595d | 11 | |
85d33df3 | 12 | #define BTF_TYPE_EMIT(type) ((void)(type *)0) |
97a19caf | 13 | #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) |
85d33df3 | 14 | |
dee872e1 KKD |
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, | |
a1ef1959 | 20 | BTF_KFUNC_TYPE_KPTR_ACQUIRE, |
dee872e1 KKD |
21 | BTF_KFUNC_TYPE_MAX, |
22 | }; | |
23 | ||
eb3f595d | 24 | struct btf; |
ffa0c1cf | 25 | struct btf_member; |
eb3f595d | 26 | struct btf_type; |
f56a653c | 27 | union bpf_attr; |
31d0bc81 | 28 | struct btf_show; |
dee872e1 KKD |
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; | |
a1ef1959 | 39 | struct btf_id_set *kptr_acquire_set; |
dee872e1 KKD |
40 | }; |
41 | struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX]; | |
42 | }; | |
43 | }; | |
eb3f595d | 44 | |
5ce937d6 KKD |
45 | struct btf_id_dtor_kfunc { |
46 | u32 btf_id; | |
47 | u32 kfunc_btf_id; | |
48 | }; | |
49 | ||
14a324f6 KKD |
50 | typedef void (*btf_dtor_kfunc_t)(void *); |
51 | ||
60197cfb MKL |
52 | extern const struct file_operations btf_fops; |
53 | ||
22dc4a0f | 54 | void btf_get(struct btf *btf); |
f56a653c | 55 | void btf_put(struct btf *btf); |
c571bd75 | 56 | int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); |
f56a653c | 57 | struct btf *btf_get_by_fd(int fd); |
60197cfb MKL |
58 | int btf_get_info_by_fd(const struct btf *btf, |
59 | const union bpf_attr *attr, | |
60 | union bpf_attr __user *uattr); | |
eb3f595d MKL |
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); | |
31d0bc81 AM |
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 | */ | |
c4d0bfb4 AM |
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 | |
31d0bc81 AM |
101 | #define BTF_SHOW_UNSAFE (1ULL << 4) |
102 | ||
b00b8dae MKL |
103 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
104 | struct seq_file *m); | |
eb411377 AM |
105 | int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, |
106 | struct seq_file *m, u64 flags); | |
31d0bc81 AM |
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 | ||
78958fca | 124 | int btf_get_fd_by_id(u32 id); |
22dc4a0f | 125 | u32 btf_obj_id(const struct btf *btf); |
290248a5 | 126 | bool btf_is_kernel(const struct btf *btf); |
541c3bad AN |
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); | |
ffa0c1cf YS |
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); | |
d83525ca | 133 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); |
68134668 | 134 | int btf_find_timer(const struct btf *btf, const struct btf_type *t); |
61df10c7 KKD |
135 | struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, |
136 | const struct btf_type *t); | |
2824ecb7 | 137 | bool btf_type_is_void(const struct btf_type *t); |
27ae7997 MKL |
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); | |
85d33df3 MKL |
145 | const struct btf_type * |
146 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
6298399b | 147 | u32 *type_size); |
e6ac2450 | 148 | const char *btf_type_str(const struct btf_type *t); |
27ae7997 MKL |
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++) | |
f6161a8f | 154 | |
eaa6bcb7 HL |
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 | ||
38207291 MKL |
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 | ||
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 | ||
a9b59159 JF |
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 | ||
38207291 MKL |
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 | ||
29db4bea AS |
180 | static inline bool str_is_empty(const char *s) |
181 | { | |
182 | return !s || !s[0]; | |
183 | } | |
184 | ||
185 | static inline u16 btf_kind(const struct btf_type *t) | |
186 | { | |
187 | return BTF_INFO_KIND(t->info); | |
188 | } | |
189 | ||
190 | static inline bool btf_is_enum(const struct btf_type *t) | |
191 | { | |
192 | return btf_kind(t) == BTF_KIND_ENUM; | |
193 | } | |
194 | ||
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 | ||
202 | static inline bool btf_is_array(const struct btf_type *t) | |
203 | { | |
204 | return btf_kind(t) == BTF_KIND_ARRAY; | |
205 | } | |
206 | ||
207 | static inline bool btf_is_int(const struct btf_type *t) | |
208 | { | |
209 | return btf_kind(t) == BTF_KIND_INT; | |
210 | } | |
211 | ||
212 | static inline bool btf_is_ptr(const struct btf_type *t) | |
213 | { | |
214 | return btf_kind(t) == BTF_KIND_PTR; | |
215 | } | |
216 | ||
217 | static inline u8 btf_int_offset(const struct btf_type *t) | |
218 | { | |
219 | return BTF_INT_OFFSET(*(u32 *)(t + 1)); | |
220 | } | |
221 | ||
222 | static inline u8 btf_int_encoding(const struct btf_type *t) | |
223 | { | |
224 | return BTF_INT_ENCODING(*(u32 *)(t + 1)); | |
225 | } | |
226 | ||
34747c41 MKL |
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 | ||
38207291 MKL |
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 | ||
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 | ||
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 | ||
4976b718 HL |
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 | ||
c6f1bfe8 YS |
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 | ||
4976b718 HL |
257 | /* union is only a special case of struct: |
258 | * all its offsetof(member) == 0 | |
259 | */ | |
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 | ||
27ae7997 MKL |
267 | static inline u16 btf_type_vlen(const struct btf_type *t) |
268 | { | |
269 | return BTF_INFO_VLEN(t->info); | |
270 | } | |
271 | ||
29db4bea AS |
272 | static inline u16 btf_vlen(const struct btf_type *t) |
273 | { | |
274 | return btf_type_vlen(t); | |
275 | } | |
276 | ||
be8704ff AS |
277 | static inline u16 btf_func_linkage(const struct btf_type *t) |
278 | { | |
279 | return BTF_INFO_VLEN(t->info); | |
280 | } | |
281 | ||
27ae7997 MKL |
282 | static inline bool btf_type_kflag(const struct btf_type *t) |
283 | { | |
284 | return BTF_INFO_KFLAG(t->info); | |
285 | } | |
286 | ||
8293eb99 AS |
287 | static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, |
288 | const struct btf_member *member) | |
85d33df3 MKL |
289 | { |
290 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) | |
291 | : member->offset; | |
292 | } | |
293 | ||
8293eb99 AS |
294 | static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, |
295 | const struct btf_member *member) | |
27ae7997 MKL |
296 | { |
297 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) | |
298 | : 0; | |
299 | } | |
300 | ||
29db4bea AS |
301 | static inline struct btf_member *btf_members(const struct btf_type *t) |
302 | { | |
303 | return (struct btf_member *)(t + 1); | |
304 | } | |
305 | ||
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 | ||
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 | ||
27ae7997 MKL |
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 | ||
29db4bea AS |
325 | static inline struct btf_array *btf_array(const struct btf_type *t) |
326 | { | |
327 | return (struct btf_array *)(t + 1); | |
328 | } | |
329 | ||
330 | static inline struct btf_enum *btf_enum(const struct btf_type *t) | |
331 | { | |
332 | return (struct btf_enum *)(t + 1); | |
333 | } | |
334 | ||
eaa6bcb7 HL |
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 | ||
e70e13e7 MC |
341 | static inline struct btf_param *btf_params(const struct btf_type *t) |
342 | { | |
343 | return (struct btf_param *)(t + 1); | |
344 | } | |
345 | ||
f6161a8f | 346 | #ifdef CONFIG_BPF_SYSCALL |
22dc4a0f AN |
347 | struct bpf_prog; |
348 | ||
838e9690 YS |
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); | |
8580ac94 | 351 | struct btf *btf_parse_vmlinux(void); |
5b92a28a | 352 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); |
dee872e1 KKD |
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); | |
5ce937d6 KKD |
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); | |
f6161a8f YS |
361 | #else |
362 | static inline const struct btf_type *btf_type_by_id(const struct btf *btf, | |
363 | u32 type_id) | |
364 | { | |
365 | return NULL; | |
366 | } | |
367 | static inline const char *btf_name_by_offset(const struct btf *btf, | |
368 | u32 offset) | |
369 | { | |
370 | return NULL; | |
371 | } | |
dee872e1 KKD |
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 | } | |
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 | } | |
5ce937d6 KKD |
384 | static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) |
385 | { | |
386 | return -ENOENT; | |
387 | } | |
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 | } | |
f6161a8f | 393 | #endif |
eb3f595d MKL |
394 | |
395 | #endif |