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 #ifdef CONFIG_DEBUG_INFO_BTF
12 
13 #include <linux/compiler.h> /* for __PASTE */
14 
15 /*
16  * Following macros help to define lists of BTF IDs placed
17  * in .BTF_ids section. They are initially filled with zeros
18  * (during compilation) and resolved later during the
19  * linking phase by resolve_btfids tool.
20  *
21  * Any change in list layout must be reflected in resolve_btfids
22  * tool logic.
23  */
24 
25 #define BTF_IDS_SECTION ".BTF_ids"
26 
27 #define ____BTF_ID(symbol)				\
28 asm(							\
29 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
30 ".local " #symbol " ;                          \n"	\
31 ".type  " #symbol ", STT_OBJECT;               \n"	\
32 ".size  " #symbol ", 4;                        \n"	\
33 #symbol ":                                     \n"	\
34 ".zero 4                                       \n"	\
35 ".popsection;                                  \n");
36 
37 #define __BTF_ID(symbol) \
38 	____BTF_ID(symbol)
39 
40 #define __ID(prefix) \
41 	__PASTE(prefix, __COUNTER__)
42 
43 /*
44  * The BTF_ID defines unique symbol for each ID pointing
45  * to 4 zero bytes.
46  */
47 #define BTF_ID(prefix, name) \
48 	__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__))
49 
50 /*
51  * The BTF_ID_LIST macro defines pure (unsorted) list
52  * of BTF IDs, with following layout:
53  *
54  * BTF_ID_LIST(list1)
55  * BTF_ID(type1, name1)
56  * BTF_ID(type2, name2)
57  *
58  * list1:
59  * __BTF_ID__type1__name1__1:
60  * .zero 4
61  * __BTF_ID__type2__name2__2:
62  * .zero 4
63  *
64  */
65 #define __BTF_ID_LIST(name, scope)			\
66 asm(							\
67 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
68 "." #scope " " #name ";                        \n"	\
69 #name ":;                                      \n"	\
70 ".popsection;                                  \n");
71 
72 #define BTF_ID_LIST(name)				\
73 __BTF_ID_LIST(name, local)				\
74 extern u32 name[];
75 
76 #define BTF_ID_LIST_GLOBAL(name, n)			\
77 __BTF_ID_LIST(name, globl)
78 
79 /* The BTF_ID_LIST_SINGLE macro defines a BTF_ID_LIST with
80  * a single entry.
81  */
82 #define BTF_ID_LIST_SINGLE(name, prefix, typename)	\
83 	BTF_ID_LIST(name) \
84 	BTF_ID(prefix, typename)
85 #define BTF_ID_LIST_GLOBAL_SINGLE(name, prefix, typename) \
86 	BTF_ID_LIST_GLOBAL(name, 1)			  \
87 	BTF_ID(prefix, typename)
88 
89 /*
90  * The BTF_ID_UNUSED macro defines 4 zero bytes.
91  * It's used when we want to define 'unused' entry
92  * in BTF_ID_LIST, like:
93  *
94  *   BTF_ID_LIST(bpf_skb_output_btf_ids)
95  *   BTF_ID(struct, sk_buff)
96  *   BTF_ID_UNUSED
97  *   BTF_ID(struct, task_struct)
98  */
99 
100 #define BTF_ID_UNUSED					\
101 asm(							\
102 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
103 ".zero 4                                       \n"	\
104 ".popsection;                                  \n");
105 
106 /*
107  * The BTF_SET_START/END macros pair defines sorted list of
108  * BTF IDs plus its members count, with following layout:
109  *
110  * BTF_SET_START(list)
111  * BTF_ID(type1, name1)
112  * BTF_ID(type2, name2)
113  * BTF_SET_END(list)
114  *
115  * __BTF_ID__set__list:
116  * .zero 4
117  * list:
118  * __BTF_ID__type1__name1__3:
119  * .zero 4
120  * __BTF_ID__type2__name2__4:
121  * .zero 4
122  *
123  */
124 #define __BTF_SET_START(name, scope)			\
125 asm(							\
126 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
127 "." #scope " __BTF_ID__set__" #name ";         \n"	\
128 "__BTF_ID__set__" #name ":;                    \n"	\
129 ".zero 4                                       \n"	\
130 ".popsection;                                  \n");
131 
132 #define BTF_SET_START(name)				\
133 __BTF_ID_LIST(name, local)				\
134 __BTF_SET_START(name, local)
135 
136 #define BTF_SET_START_GLOBAL(name)			\
137 __BTF_ID_LIST(name, globl)				\
138 __BTF_SET_START(name, globl)
139 
140 #define BTF_SET_END(name)				\
141 asm(							\
142 ".pushsection " BTF_IDS_SECTION ",\"a\";      \n"	\
143 ".size __BTF_ID__set__" #name ", .-" #name "  \n"	\
144 ".popsection;                                 \n");	\
145 extern struct btf_id_set name;
146 
147 #else
148 
149 #define BTF_ID_LIST(name) static u32 __maybe_unused name[5];
150 #define BTF_ID(prefix, name)
151 #define BTF_ID_UNUSED
152 #define BTF_ID_LIST_GLOBAL(name, n) u32 __maybe_unused name[n];
153 #define BTF_ID_LIST_SINGLE(name, prefix, typename) static u32 __maybe_unused name[1];
154 #define BTF_ID_LIST_GLOBAL_SINGLE(name, prefix, typename) u32 __maybe_unused name[1];
155 #define BTF_SET_START(name) static struct btf_id_set __maybe_unused name = { 0 };
156 #define BTF_SET_START_GLOBAL(name) static struct btf_id_set __maybe_unused name = { 0 };
157 #define BTF_SET_END(name)
158 
159 #endif /* CONFIG_DEBUG_INFO_BTF */
160 
161 #ifdef CONFIG_NET
162 /* Define a list of socket types which can be the argument for
163  * skc_to_*_sock() helpers. All these sockets should have
164  * sock_common as the first argument in its memory layout.
165  */
166 #define BTF_SOCK_TYPE_xxx \
167 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET, inet_sock)			\
168 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_CONN, inet_connection_sock)	\
169 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_REQ, inet_request_sock)	\
170 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_TW, inet_timewait_sock)	\
171 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_REQ, request_sock)			\
172 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK, sock)				\
173 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK_COMMON, sock_common)		\
174 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP, tcp_sock)			\
175 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_REQ, tcp_request_sock)		\
176 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_TW, tcp_timewait_sock)		\
177 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock)			\
178 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock)			\
179 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock)			\
180 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UNIX, unix_sock)			\
181 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_MPTCP, mptcp_sock)			\
182 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCKET, socket)
183 
184 enum {
185 #define BTF_SOCK_TYPE(name, str) name,
186 BTF_SOCK_TYPE_xxx
187 #undef BTF_SOCK_TYPE
188 MAX_BTF_SOCK_TYPE,
189 };
190 
191 extern u32 btf_sock_ids[];
192 #endif
193 
194 #define BTF_TRACING_TYPE_xxx	\
195 	BTF_TRACING_TYPE(BTF_TRACING_TYPE_TASK, task_struct)	\
196 	BTF_TRACING_TYPE(BTF_TRACING_TYPE_FILE, file)		\
197 	BTF_TRACING_TYPE(BTF_TRACING_TYPE_VMA, vm_area_struct)
198 
199 enum {
200 #define BTF_TRACING_TYPE(name, type) name,
201 BTF_TRACING_TYPE_xxx
202 #undef BTF_TRACING_TYPE
203 MAX_BTF_TRACING_TYPE,
204 };
205 
206 extern u32 btf_tracing_ids[];
207 
208 #endif
209