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