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 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, 20 BTF_KFUNC_TYPE_MAX, 21 }; 22 23 struct btf; 24 struct btf_member; 25 struct btf_type; 26 union bpf_attr; 27 struct btf_show; 28 struct btf_id_set; 29 30 struct btf_kfunc_id_set { 31 struct module *owner; 32 union { 33 struct { 34 struct btf_id_set *check_set; 35 struct btf_id_set *acquire_set; 36 struct btf_id_set *release_set; 37 struct btf_id_set *ret_null_set; 38 }; 39 struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX]; 40 }; 41 }; 42 43 extern const struct file_operations btf_fops; 44 45 void btf_get(struct btf *btf); 46 void btf_put(struct btf *btf); 47 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); 48 struct btf *btf_get_by_fd(int fd); 49 int btf_get_info_by_fd(const struct btf *btf, 50 const union bpf_attr *attr, 51 union bpf_attr __user *uattr); 52 /* Figure out the size of a type_id. If type_id is a modifier 53 * (e.g. const), it will be resolved to find out the type with size. 54 * 55 * For example: 56 * In describing "const void *", type_id is "const" and "const" 57 * refers to "void *". The return type will be "void *". 58 * 59 * If type_id is a simple "int", then return type will be "int". 60 * 61 * @btf: struct btf object 62 * @type_id: Find out the size of type_id. The type_id of the return 63 * type is set to *type_id. 64 * @ret_size: It can be NULL. If not NULL, the size of the return 65 * type is set to *ret_size. 66 * Return: The btf_type (resolved to another type with size info if needed). 67 * NULL is returned if type_id itself does not have size info 68 * (e.g. void) or it cannot be resolved to another type that 69 * has size info. 70 * *type_id and *ret_size will not be changed in the 71 * NULL return case. 72 */ 73 const struct btf_type *btf_type_id_size(const struct btf *btf, 74 u32 *type_id, 75 u32 *ret_size); 76 77 /* 78 * Options to control show behaviour. 79 * - BTF_SHOW_COMPACT: no formatting around type information 80 * - BTF_SHOW_NONAME: no struct/union member names/types 81 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 82 * equivalent to %px. 83 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 84 * are not displayed by default 85 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 86 * data before displaying it. 87 */ 88 #define BTF_SHOW_COMPACT BTF_F_COMPACT 89 #define BTF_SHOW_NONAME BTF_F_NONAME 90 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 91 #define BTF_SHOW_ZERO BTF_F_ZERO 92 #define BTF_SHOW_UNSAFE (1ULL << 4) 93 94 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 95 struct seq_file *m); 96 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 97 struct seq_file *m, u64 flags); 98 99 /* 100 * Copy len bytes of string representation of obj of BTF type_id into buf. 101 * 102 * @btf: struct btf object 103 * @type_id: type id of type obj points to 104 * @obj: pointer to typed data 105 * @buf: buffer to write to 106 * @len: maximum length to write to buf 107 * @flags: show options (see above) 108 * 109 * Return: length that would have been/was copied as per snprintf, or 110 * negative error. 111 */ 112 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 113 char *buf, int len, u64 flags); 114 115 int btf_get_fd_by_id(u32 id); 116 u32 btf_obj_id(const struct btf *btf); 117 bool btf_is_kernel(const struct btf *btf); 118 bool btf_is_module(const struct btf *btf); 119 struct module *btf_try_get_module(const struct btf *btf); 120 u32 btf_nr_types(const struct btf *btf); 121 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 122 const struct btf_member *m, 123 u32 expected_offset, u32 expected_size); 124 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); 125 int btf_find_timer(const struct btf *btf, const struct btf_type *t); 126 bool btf_type_is_void(const struct btf_type *t); 127 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 128 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 129 u32 id, u32 *res_id); 130 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 131 u32 id, u32 *res_id); 132 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 133 u32 id, u32 *res_id); 134 const struct btf_type * 135 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 136 u32 *type_size); 137 const char *btf_type_str(const struct btf_type *t); 138 139 #define for_each_member(i, struct_type, member) \ 140 for (i = 0, member = btf_type_member(struct_type); \ 141 i < btf_type_vlen(struct_type); \ 142 i++, member++) 143 144 #define for_each_vsi(i, datasec_type, member) \ 145 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 146 i < btf_type_vlen(datasec_type); \ 147 i++, member++) 148 149 static inline bool btf_type_is_ptr(const struct btf_type *t) 150 { 151 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 152 } 153 154 static inline bool btf_type_is_int(const struct btf_type *t) 155 { 156 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 157 } 158 159 static inline bool btf_type_is_small_int(const struct btf_type *t) 160 { 161 return btf_type_is_int(t) && t->size <= sizeof(u64); 162 } 163 164 static inline bool btf_type_is_enum(const struct btf_type *t) 165 { 166 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 167 } 168 169 static inline bool str_is_empty(const char *s) 170 { 171 return !s || !s[0]; 172 } 173 174 static inline u16 btf_kind(const struct btf_type *t) 175 { 176 return BTF_INFO_KIND(t->info); 177 } 178 179 static inline bool btf_is_enum(const struct btf_type *t) 180 { 181 return btf_kind(t) == BTF_KIND_ENUM; 182 } 183 184 static inline bool btf_is_composite(const struct btf_type *t) 185 { 186 u16 kind = btf_kind(t); 187 188 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 189 } 190 191 static inline bool btf_is_array(const struct btf_type *t) 192 { 193 return btf_kind(t) == BTF_KIND_ARRAY; 194 } 195 196 static inline bool btf_is_int(const struct btf_type *t) 197 { 198 return btf_kind(t) == BTF_KIND_INT; 199 } 200 201 static inline bool btf_is_ptr(const struct btf_type *t) 202 { 203 return btf_kind(t) == BTF_KIND_PTR; 204 } 205 206 static inline u8 btf_int_offset(const struct btf_type *t) 207 { 208 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 209 } 210 211 static inline u8 btf_int_encoding(const struct btf_type *t) 212 { 213 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 214 } 215 216 static inline bool btf_type_is_scalar(const struct btf_type *t) 217 { 218 return btf_type_is_int(t) || btf_type_is_enum(t); 219 } 220 221 static inline bool btf_type_is_typedef(const struct btf_type *t) 222 { 223 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 224 } 225 226 static inline bool btf_type_is_func(const struct btf_type *t) 227 { 228 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 229 } 230 231 static inline bool btf_type_is_func_proto(const struct btf_type *t) 232 { 233 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 234 } 235 236 static inline bool btf_type_is_var(const struct btf_type *t) 237 { 238 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 239 } 240 241 static inline bool btf_type_is_type_tag(const struct btf_type *t) 242 { 243 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 244 } 245 246 /* union is only a special case of struct: 247 * all its offsetof(member) == 0 248 */ 249 static inline bool btf_type_is_struct(const struct btf_type *t) 250 { 251 u8 kind = BTF_INFO_KIND(t->info); 252 253 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 254 } 255 256 static inline u16 btf_type_vlen(const struct btf_type *t) 257 { 258 return BTF_INFO_VLEN(t->info); 259 } 260 261 static inline u16 btf_vlen(const struct btf_type *t) 262 { 263 return btf_type_vlen(t); 264 } 265 266 static inline u16 btf_func_linkage(const struct btf_type *t) 267 { 268 return BTF_INFO_VLEN(t->info); 269 } 270 271 static inline bool btf_type_kflag(const struct btf_type *t) 272 { 273 return BTF_INFO_KFLAG(t->info); 274 } 275 276 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 277 const struct btf_member *member) 278 { 279 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 280 : member->offset; 281 } 282 283 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 284 const struct btf_member *member) 285 { 286 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 287 : 0; 288 } 289 290 static inline struct btf_member *btf_members(const struct btf_type *t) 291 { 292 return (struct btf_member *)(t + 1); 293 } 294 295 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 296 { 297 const struct btf_member *m = btf_members(t) + member_idx; 298 299 return __btf_member_bit_offset(t, m); 300 } 301 302 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 303 { 304 const struct btf_member *m = btf_members(t) + member_idx; 305 306 return __btf_member_bitfield_size(t, m); 307 } 308 309 static inline const struct btf_member *btf_type_member(const struct btf_type *t) 310 { 311 return (const struct btf_member *)(t + 1); 312 } 313 314 static inline struct btf_array *btf_array(const struct btf_type *t) 315 { 316 return (struct btf_array *)(t + 1); 317 } 318 319 static inline struct btf_enum *btf_enum(const struct btf_type *t) 320 { 321 return (struct btf_enum *)(t + 1); 322 } 323 324 static inline const struct btf_var_secinfo *btf_type_var_secinfo( 325 const struct btf_type *t) 326 { 327 return (const struct btf_var_secinfo *)(t + 1); 328 } 329 330 static inline struct btf_param *btf_params(const struct btf_type *t) 331 { 332 return (struct btf_param *)(t + 1); 333 } 334 335 #ifdef CONFIG_BPF_SYSCALL 336 struct bpf_prog; 337 338 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 339 const char *btf_name_by_offset(const struct btf *btf, u32 offset); 340 struct btf *btf_parse_vmlinux(void); 341 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 342 bool btf_kfunc_id_set_contains(const struct btf *btf, 343 enum bpf_prog_type prog_type, 344 enum btf_kfunc_type type, u32 kfunc_btf_id); 345 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 346 const struct btf_kfunc_id_set *s); 347 #else 348 static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 349 u32 type_id) 350 { 351 return NULL; 352 } 353 static inline const char *btf_name_by_offset(const struct btf *btf, 354 u32 offset) 355 { 356 return NULL; 357 } 358 static inline bool btf_kfunc_id_set_contains(const struct btf *btf, 359 enum bpf_prog_type prog_type, 360 enum btf_kfunc_type type, 361 u32 kfunc_btf_id) 362 { 363 return false; 364 } 365 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 366 const struct btf_kfunc_id_set *s) 367 { 368 return 0; 369 } 370 #endif 371 372 #endif 373