1
2
3
4#ifndef _LINUX_BTF_H
5#define _LINUX_BTF_H 1
6
7#include <linux/types.h>
8#include <uapi/linux/btf.h>
9#include <uapi/linux/bpf.h>
10
11#define BTF_TYPE_EMIT(type) ((void)(type *)0)
12
13struct btf;
14struct btf_member;
15struct btf_type;
16union bpf_attr;
17struct btf_show;
18
19extern const struct file_operations btf_fops;
20
21void btf_put(struct btf *btf);
22int btf_new_fd(const union bpf_attr *attr);
23struct btf *btf_get_by_fd(int fd);
24int btf_get_info_by_fd(const struct btf *btf,
25 const union bpf_attr *attr,
26 union bpf_attr __user *uattr);
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48const struct btf_type *btf_type_id_size(const struct btf *btf,
49 u32 *type_id,
50 u32 *ret_size);
51
52
53
54
55
56
57
58
59
60
61
62
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
67#define BTF_SHOW_UNSAFE (1ULL << 4)
68
69void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
70 struct seq_file *m);
71int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
72 struct seq_file *m, u64 flags);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
88 char *buf, int len, u64 flags);
89
90int btf_get_fd_by_id(u32 id);
91u32 btf_id(const struct btf *btf);
92bool 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);
95int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
96bool btf_type_is_void(const struct btf_type *t);
97s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
98const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
99 u32 id, u32 *res_id);
100const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
101 u32 id, u32 *res_id);
102const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
103 u32 id, u32 *res_id);
104const struct btf_type *
105btf_resolve_size(const struct btf *btf, const struct btf_type *type,
106 u32 *type_size);
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++)
112
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
118static inline bool btf_type_is_ptr(const struct btf_type *t)
119{
120 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
121}
122
123static inline bool btf_type_is_int(const struct btf_type *t)
124{
125 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
126}
127
128static 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
133static inline bool btf_type_is_enum(const struct btf_type *t)
134{
135 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
136}
137
138static inline bool btf_type_is_typedef(const struct btf_type *t)
139{
140 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
141}
142
143static inline bool btf_type_is_func(const struct btf_type *t)
144{
145 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
146}
147
148static 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
153static 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
159
160
161static 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
168static inline u16 btf_type_vlen(const struct btf_type *t)
169{
170 return BTF_INFO_VLEN(t->info);
171}
172
173static inline u16 btf_func_linkage(const struct btf_type *t)
174{
175 return BTF_INFO_VLEN(t->info);
176}
177
178static inline bool btf_type_kflag(const struct btf_type *t)
179{
180 return BTF_INFO_KFLAG(t->info);
181}
182
183static 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
190static 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
197static inline const struct btf_member *btf_type_member(const struct btf_type *t)
198{
199 return (const struct btf_member *)(t + 1);
200}
201
202static 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
208#ifdef CONFIG_BPF_SYSCALL
209const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
210const char *btf_name_by_offset(const struct btf *btf, u32 offset);
211struct btf *btf_parse_vmlinux(void);
212struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
213#else
214static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
215 u32 type_id)
216{
217 return NULL;
218}
219static inline const char *btf_name_by_offset(const struct btf *btf,
220 u32 offset)
221{
222 return NULL;
223}
224#endif
225
226#endif
227