xref: /linux/tools/include/linux/btf_ids.h (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _LINUX_BTF_IDS_H
4 #define _LINUX_BTF_IDS_H
5 
6 #include <linux/types.h> /* for u32 */
7 
8 struct btf_id_set {
9 	u32 cnt;
10 	u32 ids[];
11 };
12 
13 /* This flag implies BTF_SET8 holds kfunc(s) */
14 #define BTF_SET8_KFUNCS		(1 << 0)
15 
16 struct btf_id_set8 {
17 	u32 cnt;
18 	u32 flags;
19 	struct {
20 		u32 id;
21 		u32 flags;
22 	} pairs[];
23 };
24 
25 #ifdef CONFIG_DEBUG_INFO_BTF
26 
27 #include <linux/compiler.h> /* for __PASTE */
28 #include <linux/stringify.h>
29 
30 /*
31  * Following macros help to define lists of BTF IDs placed
32  * in .BTF_ids section. They are initially filled with zeros
33  * (during compilation) and resolved later during the
34  * linking phase by resolve_btfids tool.
35  *
36  * Any change in list layout must be reflected in resolve_btfids
37  * tool logic.
38  */
39 
40 #define BTF_IDS_SECTION ".BTF_ids"
41 
42 #define ____BTF_ID(symbol, word)			\
43 asm(							\
44 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
45 ".local " #symbol " ;                          \n"	\
46 ".type  " #symbol ", STT_OBJECT;               \n"	\
47 ".size  " #symbol ", 4;                        \n"	\
48 #symbol ":                                     \n"	\
49 ".zero 4                                       \n"	\
50 word							\
51 ".popsection;                                  \n");
52 
53 #define __BTF_ID(symbol, word) \
54 	____BTF_ID(symbol, word)
55 
56 #define __ID(prefix) \
57 	__PASTE(__PASTE(prefix, __COUNTER__), __LINE__)
58 
59 /*
60  * The BTF_ID defines unique symbol for each ID pointing
61  * to 4 zero bytes.
62  */
63 #define BTF_ID(prefix, name) \
64 	__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__), "")
65 
66 #define ____BTF_ID_FLAGS(prefix, name, flags) \
67 	__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__), ".long " #flags "\n")
68 #define __BTF_ID_FLAGS(prefix, name, flags, ...) \
69 	____BTF_ID_FLAGS(prefix, name, flags)
70 #define BTF_ID_FLAGS(prefix, name, ...) \
71 	__BTF_ID_FLAGS(prefix, name, ##__VA_ARGS__, 0)
72 
73 /*
74  * The BTF_ID_LIST macro defines pure (unsorted) list
75  * of BTF IDs, with following layout:
76  *
77  * BTF_ID_LIST(list1)
78  * BTF_ID(type1, name1)
79  * BTF_ID(type2, name2)
80  *
81  * list1:
82  * __BTF_ID__type1__name1__1:
83  * .zero 4
84  * __BTF_ID__type2__name2__2:
85  * .zero 4
86  *
87  */
88 #define __BTF_ID_LIST(name, scope)			\
89 asm(							\
90 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
91 "." #scope " " #name ";                        \n"	\
92 #name ":;                                      \n"	\
93 ".popsection;                                  \n");
94 
95 #define BTF_ID_LIST(name)				\
96 __BTF_ID_LIST(name, local)				\
97 extern u32 name[];
98 
99 #define BTF_ID_LIST_GLOBAL(name, n)			\
100 __BTF_ID_LIST(name, globl)
101 
102 /* The BTF_ID_LIST_SINGLE macro defines a BTF_ID_LIST with
103  * a single entry.
104  */
105 #define BTF_ID_LIST_SINGLE(name, prefix, typename)	\
106 	BTF_ID_LIST(name) \
107 	BTF_ID(prefix, typename)
108 #define BTF_ID_LIST_GLOBAL_SINGLE(name, prefix, typename) \
109 	BTF_ID_LIST_GLOBAL(name, 1)			  \
110 	BTF_ID(prefix, typename)
111 
112 /*
113  * The BTF_ID_UNUSED macro defines 4 zero bytes.
114  * It's used when we want to define 'unused' entry
115  * in BTF_ID_LIST, like:
116  *
117  *   BTF_ID_LIST(bpf_skb_output_btf_ids)
118  *   BTF_ID(struct, sk_buff)
119  *   BTF_ID_UNUSED
120  *   BTF_ID(struct, task_struct)
121  */
122 
123 #define BTF_ID_UNUSED					\
124 asm(							\
125 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
126 ".zero 4                                       \n"	\
127 ".popsection;                                  \n");
128 
129 /*
130  * The BTF_SET_START/END macros pair defines sorted list of
131  * BTF IDs plus its members count, with following layout:
132  *
133  * BTF_SET_START(list)
134  * BTF_ID(type1, name1)
135  * BTF_ID(type2, name2)
136  * BTF_SET_END(list)
137  *
138  * __BTF_ID__set__list:
139  * .zero 4
140  * list:
141  * __BTF_ID__type1__name1__3:
142  * .zero 4
143  * __BTF_ID__type2__name2__4:
144  * .zero 4
145  *
146  */
147 #define __BTF_SET_START(name, scope)			\
148 asm(							\
149 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
150 "." #scope " __BTF_ID__set__" #name ";         \n"	\
151 "__BTF_ID__set__" #name ":;                    \n"	\
152 ".zero 4                                       \n"	\
153 ".popsection;                                  \n");
154 
155 #define BTF_SET_START(name)				\
156 __BTF_ID_LIST(name, local)				\
157 __BTF_SET_START(name, local)
158 
159 #define BTF_SET_START_GLOBAL(name)			\
160 __BTF_ID_LIST(name, globl)				\
161 __BTF_SET_START(name, globl)
162 
163 #define BTF_SET_END(name)				\
164 asm(							\
165 ".pushsection " BTF_IDS_SECTION ",\"a\";      \n"	\
166 ".size __BTF_ID__set__" #name ", .-" #name "  \n"	\
167 ".popsection;                                 \n");	\
168 extern struct btf_id_set name;
169 
170 /*
171  * The BTF_SET8_START/END macros pair defines sorted list of
172  * BTF IDs and their flags plus its members count, with the
173  * following layout:
174  *
175  * BTF_SET8_START(list)
176  * BTF_ID_FLAGS(type1, name1, flags)
177  * BTF_ID_FLAGS(type2, name2, flags)
178  * BTF_SET8_END(list)
179  *
180  * __BTF_ID__set8__list:
181  * .zero 8
182  * list:
183  * __BTF_ID__type1__name1__3:
184  * .zero 4
185  * .word (1 << 0) | (1 << 2)
186  * __BTF_ID__type2__name2__5:
187  * .zero 4
188  * .word (1 << 3) | (1 << 1) | (1 << 2)
189  *
190  */
191 #define __BTF_SET8_START(name, scope, flags)		\
192 __BTF_ID_LIST(name, local)				\
193 asm(							\
194 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
195 "." #scope " __BTF_ID__set8__" #name ";        \n"	\
196 "__BTF_ID__set8__" #name ":;                   \n"	\
197 ".zero 4                                       \n"	\
198 ".long " __stringify(flags)                   "\n"	\
199 ".popsection;                                  \n");
200 
201 #define BTF_SET8_START(name)				\
202 __BTF_SET8_START(name, local, 0)
203 
204 #define BTF_SET8_END(name)				\
205 asm(							\
206 ".pushsection " BTF_IDS_SECTION ",\"a\";      \n"	\
207 ".size __BTF_ID__set8__" #name ", .-" #name "  \n"	\
208 ".popsection;                                 \n");	\
209 extern struct btf_id_set8 name;
210 
211 #define BTF_KFUNCS_START(name)				\
212 __BTF_SET8_START(name, local, BTF_SET8_KFUNCS)
213 
214 #define BTF_KFUNCS_END(name)				\
215 BTF_SET8_END(name)
216 
217 #else
218 
219 #define BTF_ID_LIST(name) static u32 __maybe_unused name[128];
220 #define BTF_ID(prefix, name)
221 #define BTF_ID_FLAGS(prefix, name, ...)
222 #define BTF_ID_UNUSED
223 #define BTF_ID_LIST_GLOBAL(name, n) u32 __maybe_unused name[n];
224 #define BTF_ID_LIST_SINGLE(name, prefix, typename) static u32 __maybe_unused name[1];
225 #define BTF_ID_LIST_GLOBAL_SINGLE(name, prefix, typename) u32 __maybe_unused name[1];
226 #define BTF_SET_START(name) static struct btf_id_set __maybe_unused name = { 0 };
227 #define BTF_SET_START_GLOBAL(name) static struct btf_id_set __maybe_unused name = { 0 };
228 #define BTF_SET_END(name)
229 #define BTF_SET8_START(name) static struct btf_id_set8 __maybe_unused name = { 0 };
230 #define BTF_SET8_END(name)
231 #define BTF_KFUNCS_START(name) static struct btf_id_set8 __maybe_unused name = { .flags = BTF_SET8_KFUNCS };
232 #define BTF_KFUNCS_END(name)
233 
234 #endif /* CONFIG_DEBUG_INFO_BTF */
235 
236 #ifdef CONFIG_NET
237 /* Define a list of socket types which can be the argument for
238  * skc_to_*_sock() helpers. All these sockets should have
239  * sock_common as the first argument in its memory layout.
240  */
241 #define BTF_SOCK_TYPE_xxx \
242 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET, inet_sock)			\
243 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_CONN, inet_connection_sock)	\
244 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_REQ, inet_request_sock)	\
245 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_TW, inet_timewait_sock)	\
246 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_REQ, request_sock)			\
247 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK, sock)				\
248 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK_COMMON, sock_common)		\
249 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP, tcp_sock)			\
250 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_REQ, tcp_request_sock)		\
251 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_TW, tcp_timewait_sock)		\
252 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock)			\
253 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock)			\
254 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock)			\
255 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UNIX, unix_sock)			\
256 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_MPTCP, mptcp_sock)			\
257 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCKET, socket)
258 
259 enum {
260 #define BTF_SOCK_TYPE(name, str) name,
261 BTF_SOCK_TYPE_xxx
262 #undef BTF_SOCK_TYPE
263 MAX_BTF_SOCK_TYPE,
264 };
265 
266 extern u32 btf_sock_ids[];
267 #endif
268 
269 #define BTF_TRACING_TYPE_xxx	\
270 	BTF_TRACING_TYPE(BTF_TRACING_TYPE_TASK, task_struct)	\
271 	BTF_TRACING_TYPE(BTF_TRACING_TYPE_FILE, file)		\
272 	BTF_TRACING_TYPE(BTF_TRACING_TYPE_VMA, vm_area_struct)
273 
274 enum {
275 #define BTF_TRACING_TYPE(name, type) name,
276 BTF_TRACING_TYPE_xxx
277 #undef BTF_TRACING_TYPE
278 MAX_BTF_TRACING_TYPE,
279 };
280 
281 extern u32 btf_tracing_ids[];
282 extern u32 bpf_cgroup_btf_id[];
283 extern u32 bpf_local_storage_map_btf_id[];
284 extern u32 btf_bpf_map_id[];
285 extern u32 bpf_kmem_cache_btf_id[];
286 
287 #endif
288