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