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