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