]>
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> | |
38207291 | 8 | #include <uapi/linux/btf.h> |
c4d0bfb4 | 9 | #include <uapi/linux/bpf.h> |
eb3f595d | 10 | |
85d33df3 | 11 | #define BTF_TYPE_EMIT(type) ((void)(type *)0) |
97a19caf | 12 | #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) |
85d33df3 | 13 | |
eb3f595d | 14 | struct btf; |
ffa0c1cf | 15 | struct btf_member; |
eb3f595d | 16 | struct btf_type; |
f56a653c | 17 | union bpf_attr; |
31d0bc81 | 18 | struct btf_show; |
eb3f595d | 19 | |
60197cfb MKL |
20 | extern const struct file_operations btf_fops; |
21 | ||
22dc4a0f | 22 | void btf_get(struct btf *btf); |
f56a653c | 23 | void btf_put(struct btf *btf); |
c571bd75 | 24 | int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); |
f56a653c | 25 | struct btf *btf_get_by_fd(int fd); |
60197cfb MKL |
26 | int btf_get_info_by_fd(const struct btf *btf, |
27 | const union bpf_attr *attr, | |
28 | union bpf_attr __user *uattr); | |
eb3f595d MKL |
29 | /* Figure out the size of a type_id. If type_id is a modifier |
30 | * (e.g. const), it will be resolved to find out the type with size. | |
31 | * | |
32 | * For example: | |
33 | * In describing "const void *", type_id is "const" and "const" | |
34 | * refers to "void *". The return type will be "void *". | |
35 | * | |
36 | * If type_id is a simple "int", then return type will be "int". | |
37 | * | |
38 | * @btf: struct btf object | |
39 | * @type_id: Find out the size of type_id. The type_id of the return | |
40 | * type is set to *type_id. | |
41 | * @ret_size: It can be NULL. If not NULL, the size of the return | |
42 | * type is set to *ret_size. | |
43 | * Return: The btf_type (resolved to another type with size info if needed). | |
44 | * NULL is returned if type_id itself does not have size info | |
45 | * (e.g. void) or it cannot be resolved to another type that | |
46 | * has size info. | |
47 | * *type_id and *ret_size will not be changed in the | |
48 | * NULL return case. | |
49 | */ | |
50 | const struct btf_type *btf_type_id_size(const struct btf *btf, | |
51 | u32 *type_id, | |
52 | u32 *ret_size); | |
31d0bc81 AM |
53 | |
54 | /* | |
55 | * Options to control show behaviour. | |
56 | * - BTF_SHOW_COMPACT: no formatting around type information | |
57 | * - BTF_SHOW_NONAME: no struct/union member names/types | |
58 | * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; | |
59 | * equivalent to %px. | |
60 | * - BTF_SHOW_ZERO: show zero-valued struct/union members; they | |
61 | * are not displayed by default | |
62 | * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read | |
63 | * data before displaying it. | |
64 | */ | |
c4d0bfb4 AM |
65 | #define BTF_SHOW_COMPACT BTF_F_COMPACT |
66 | #define BTF_SHOW_NONAME BTF_F_NONAME | |
67 | #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW | |
68 | #define BTF_SHOW_ZERO BTF_F_ZERO | |
31d0bc81 AM |
69 | #define BTF_SHOW_UNSAFE (1ULL << 4) |
70 | ||
b00b8dae MKL |
71 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
72 | struct seq_file *m); | |
eb411377 AM |
73 | int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, |
74 | struct seq_file *m, u64 flags); | |
31d0bc81 AM |
75 | |
76 | /* | |
77 | * Copy len bytes of string representation of obj of BTF type_id into buf. | |
78 | * | |
79 | * @btf: struct btf object | |
80 | * @type_id: type id of type obj points to | |
81 | * @obj: pointer to typed data | |
82 | * @buf: buffer to write to | |
83 | * @len: maximum length to write to buf | |
84 | * @flags: show options (see above) | |
85 | * | |
86 | * Return: length that would have been/was copied as per snprintf, or | |
87 | * negative error. | |
88 | */ | |
89 | int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, | |
90 | char *buf, int len, u64 flags); | |
91 | ||
78958fca | 92 | int btf_get_fd_by_id(u32 id); |
22dc4a0f | 93 | u32 btf_obj_id(const struct btf *btf); |
290248a5 | 94 | bool btf_is_kernel(const struct btf *btf); |
541c3bad AN |
95 | bool btf_is_module(const struct btf *btf); |
96 | struct module *btf_try_get_module(const struct btf *btf); | |
97 | u32 btf_nr_types(const struct btf *btf); | |
ffa0c1cf YS |
98 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
99 | const struct btf_member *m, | |
100 | u32 expected_offset, u32 expected_size); | |
d83525ca | 101 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); |
68134668 | 102 | int btf_find_timer(const struct btf *btf, const struct btf_type *t); |
2824ecb7 | 103 | bool btf_type_is_void(const struct btf_type *t); |
27ae7997 MKL |
104 | s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); |
105 | const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, | |
106 | u32 id, u32 *res_id); | |
107 | const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, | |
108 | u32 id, u32 *res_id); | |
109 | const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, | |
110 | u32 id, u32 *res_id); | |
85d33df3 MKL |
111 | const struct btf_type * |
112 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
6298399b | 113 | u32 *type_size); |
e6ac2450 | 114 | const char *btf_type_str(const struct btf_type *t); |
27ae7997 MKL |
115 | |
116 | #define for_each_member(i, struct_type, member) \ | |
117 | for (i = 0, member = btf_type_member(struct_type); \ | |
118 | i < btf_type_vlen(struct_type); \ | |
119 | i++, member++) | |
f6161a8f | 120 | |
eaa6bcb7 HL |
121 | #define for_each_vsi(i, datasec_type, member) \ |
122 | for (i = 0, member = btf_type_var_secinfo(datasec_type); \ | |
123 | i < btf_type_vlen(datasec_type); \ | |
124 | i++, member++) | |
125 | ||
38207291 MKL |
126 | static inline bool btf_type_is_ptr(const struct btf_type *t) |
127 | { | |
128 | return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; | |
129 | } | |
130 | ||
131 | static inline bool btf_type_is_int(const struct btf_type *t) | |
132 | { | |
133 | return BTF_INFO_KIND(t->info) == BTF_KIND_INT; | |
134 | } | |
135 | ||
a9b59159 JF |
136 | static inline bool btf_type_is_small_int(const struct btf_type *t) |
137 | { | |
138 | return btf_type_is_int(t) && t->size <= sizeof(u64); | |
139 | } | |
140 | ||
38207291 MKL |
141 | static inline bool btf_type_is_enum(const struct btf_type *t) |
142 | { | |
143 | return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; | |
144 | } | |
145 | ||
34747c41 MKL |
146 | static inline bool btf_type_is_scalar(const struct btf_type *t) |
147 | { | |
148 | return btf_type_is_int(t) || btf_type_is_enum(t); | |
149 | } | |
150 | ||
38207291 MKL |
151 | static inline bool btf_type_is_typedef(const struct btf_type *t) |
152 | { | |
153 | return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; | |
154 | } | |
155 | ||
156 | static inline bool btf_type_is_func(const struct btf_type *t) | |
157 | { | |
158 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; | |
159 | } | |
160 | ||
161 | static inline bool btf_type_is_func_proto(const struct btf_type *t) | |
162 | { | |
163 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; | |
164 | } | |
165 | ||
4976b718 HL |
166 | static inline bool btf_type_is_var(const struct btf_type *t) |
167 | { | |
168 | return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; | |
169 | } | |
170 | ||
171 | /* union is only a special case of struct: | |
172 | * all its offsetof(member) == 0 | |
173 | */ | |
174 | static inline bool btf_type_is_struct(const struct btf_type *t) | |
175 | { | |
176 | u8 kind = BTF_INFO_KIND(t->info); | |
177 | ||
178 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; | |
179 | } | |
180 | ||
27ae7997 MKL |
181 | static inline u16 btf_type_vlen(const struct btf_type *t) |
182 | { | |
183 | return BTF_INFO_VLEN(t->info); | |
184 | } | |
185 | ||
be8704ff AS |
186 | static inline u16 btf_func_linkage(const struct btf_type *t) |
187 | { | |
188 | return BTF_INFO_VLEN(t->info); | |
189 | } | |
190 | ||
27ae7997 MKL |
191 | static inline bool btf_type_kflag(const struct btf_type *t) |
192 | { | |
193 | return BTF_INFO_KFLAG(t->info); | |
194 | } | |
195 | ||
85d33df3 MKL |
196 | static inline u32 btf_member_bit_offset(const struct btf_type *struct_type, |
197 | const struct btf_member *member) | |
198 | { | |
199 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) | |
200 | : member->offset; | |
201 | } | |
202 | ||
27ae7997 MKL |
203 | static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type, |
204 | const struct btf_member *member) | |
205 | { | |
206 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) | |
207 | : 0; | |
208 | } | |
209 | ||
210 | static inline const struct btf_member *btf_type_member(const struct btf_type *t) | |
211 | { | |
212 | return (const struct btf_member *)(t + 1); | |
213 | } | |
214 | ||
eaa6bcb7 HL |
215 | static inline const struct btf_var_secinfo *btf_type_var_secinfo( |
216 | const struct btf_type *t) | |
217 | { | |
218 | return (const struct btf_var_secinfo *)(t + 1); | |
219 | } | |
220 | ||
f6161a8f | 221 | #ifdef CONFIG_BPF_SYSCALL |
22dc4a0f AN |
222 | struct bpf_prog; |
223 | ||
838e9690 YS |
224 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); |
225 | const char *btf_name_by_offset(const struct btf *btf, u32 offset); | |
8580ac94 | 226 | struct btf *btf_parse_vmlinux(void); |
5b92a28a | 227 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); |
f6161a8f YS |
228 | #else |
229 | static inline const struct btf_type *btf_type_by_id(const struct btf *btf, | |
230 | u32 type_id) | |
231 | { | |
232 | return NULL; | |
233 | } | |
234 | static inline const char *btf_name_by_offset(const struct btf *btf, | |
235 | u32 offset) | |
236 | { | |
237 | return NULL; | |
238 | } | |
239 | #endif | |
eb3f595d MKL |
240 | |
241 | #endif |