3e5cdc2ba9637cd89f6d0c84e412af9d9907c3ca
[linux-2.6-microblaze.git] / include / linux / btf.h
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 <uapi/linux/btf.h>
9 #include <uapi/linux/bpf.h>
10
11 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
12
13 struct btf;
14 struct btf_member;
15 struct btf_type;
16 union bpf_attr;
17 struct btf_show;
18
19 extern const struct file_operations btf_fops;
20
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);
24 int btf_get_info_by_fd(const struct btf *btf,
25                        const union bpf_attr *attr,
26                        union bpf_attr __user *uattr);
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);
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  */
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
69 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
70                        struct seq_file *m);
71
72 /*
73  * Copy len bytes of string representation of obj of BTF type_id into buf.
74  *
75  * @btf: struct btf object
76  * @type_id: type id of type obj points to
77  * @obj: pointer to typed data
78  * @buf: buffer to write to
79  * @len: maximum length to write to buf
80  * @flags: show options (see above)
81  *
82  * Return: length that would have been/was copied as per snprintf, or
83  *         negative error.
84  */
85 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
86                            char *buf, int len, u64 flags);
87
88 int btf_get_fd_by_id(u32 id);
89 u32 btf_id(const struct btf *btf);
90 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
91                            const struct btf_member *m,
92                            u32 expected_offset, u32 expected_size);
93 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
94 bool btf_type_is_void(const struct btf_type *t);
95 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
96 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
97                                                u32 id, u32 *res_id);
98 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
99                                             u32 id, u32 *res_id);
100 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
101                                                  u32 id, u32 *res_id);
102 const struct btf_type *
103 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
104                  u32 *type_size);
105
106 #define for_each_member(i, struct_type, member)                 \
107         for (i = 0, member = btf_type_member(struct_type);      \
108              i < btf_type_vlen(struct_type);                    \
109              i++, member++)
110
111 static inline bool btf_type_is_ptr(const struct btf_type *t)
112 {
113         return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
114 }
115
116 static inline bool btf_type_is_int(const struct btf_type *t)
117 {
118         return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
119 }
120
121 static inline bool btf_type_is_small_int(const struct btf_type *t)
122 {
123         return btf_type_is_int(t) && t->size <= sizeof(u64);
124 }
125
126 static inline bool btf_type_is_enum(const struct btf_type *t)
127 {
128         return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
129 }
130
131 static inline bool btf_type_is_typedef(const struct btf_type *t)
132 {
133         return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
134 }
135
136 static inline bool btf_type_is_func(const struct btf_type *t)
137 {
138         return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
139 }
140
141 static inline bool btf_type_is_func_proto(const struct btf_type *t)
142 {
143         return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
144 }
145
146 static inline u16 btf_type_vlen(const struct btf_type *t)
147 {
148         return BTF_INFO_VLEN(t->info);
149 }
150
151 static inline u16 btf_func_linkage(const struct btf_type *t)
152 {
153         return BTF_INFO_VLEN(t->info);
154 }
155
156 static inline bool btf_type_kflag(const struct btf_type *t)
157 {
158         return BTF_INFO_KFLAG(t->info);
159 }
160
161 static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
162                                         const struct btf_member *member)
163 {
164         return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
165                                            : member->offset;
166 }
167
168 static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
169                                            const struct btf_member *member)
170 {
171         return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
172                                            : 0;
173 }
174
175 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
176 {
177         return (const struct btf_member *)(t + 1);
178 }
179
180 #ifdef CONFIG_BPF_SYSCALL
181 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
182 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
183 struct btf *btf_parse_vmlinux(void);
184 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
185 #else
186 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
187                                                     u32 type_id)
188 {
189         return NULL;
190 }
191 static inline const char *btf_name_by_offset(const struct btf *btf,
192                                              u32 offset)
193 {
194         return NULL;
195 }
196 #endif
197
198 #endif