bpf: Add BTF_SET_START/END macros
[linux-2.6-microblaze.git] / include / linux / btf_ids.h
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)                        \
77 __BTF_ID_LIST(name, globl)
78
79 /*
80  * The BTF_ID_UNUSED macro defines 4 zero bytes.
81  * It's used when we want to define 'unused' entry
82  * in BTF_ID_LIST, like:
83  *
84  *   BTF_ID_LIST(bpf_skb_output_btf_ids)
85  *   BTF_ID(struct, sk_buff)
86  *   BTF_ID_UNUSED
87  *   BTF_ID(struct, task_struct)
88  */
89
90 #define BTF_ID_UNUSED                                   \
91 asm(                                                    \
92 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"      \
93 ".zero 4                                       \n"      \
94 ".popsection;                                  \n");
95
96 /*
97  * The BTF_SET_START/END macros pair defines sorted list of
98  * BTF IDs plus its members count, with following layout:
99  *
100  * BTF_SET_START(list)
101  * BTF_ID(type1, name1)
102  * BTF_ID(type2, name2)
103  * BTF_SET_END(list)
104  *
105  * __BTF_ID__set__list:
106  * .zero 4
107  * list:
108  * __BTF_ID__type1__name1__3:
109  * .zero 4
110  * __BTF_ID__type2__name2__4:
111  * .zero 4
112  *
113  */
114 #define __BTF_SET_START(name, scope)                    \
115 asm(                                                    \
116 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"      \
117 "." #scope " __BTF_ID__set__" #name ";         \n"      \
118 "__BTF_ID__set__" #name ":;                    \n"      \
119 ".zero 4                                       \n"      \
120 ".popsection;                                  \n");
121
122 #define BTF_SET_START(name)                             \
123 __BTF_ID_LIST(name, local)                              \
124 __BTF_SET_START(name, local)
125
126 #define BTF_SET_START_GLOBAL(name)                      \
127 __BTF_ID_LIST(name, globl)                              \
128 __BTF_SET_START(name, globl)
129
130 #define BTF_SET_END(name)                               \
131 asm(                                                    \
132 ".pushsection " BTF_IDS_SECTION ",\"a\";      \n"       \
133 ".size __BTF_ID__set__" #name ", .-" #name "  \n"       \
134 ".popsection;                                 \n");     \
135 extern struct btf_id_set name;
136
137 #else
138
139 #define BTF_ID_LIST(name) static u32 name[5];
140 #define BTF_ID(prefix, name)
141 #define BTF_ID_UNUSED
142 #define BTF_ID_LIST_GLOBAL(name) u32 name[1];
143 #define BTF_SET_START(name) static struct btf_id_set name = { 0 };
144 #define BTF_SET_START_GLOBAL(name) static struct btf_id_set name = { 0 };
145 #define BTF_SET_END(name)
146
147 #endif /* CONFIG_DEBUG_INFO_BTF */
148
149 #ifdef CONFIG_NET
150 /* Define a list of socket types which can be the argument for
151  * skc_to_*_sock() helpers. All these sockets should have
152  * sock_common as the first argument in its memory layout.
153  */
154 #define BTF_SOCK_TYPE_xxx \
155         BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET, inet_sock)                    \
156         BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_CONN, inet_connection_sock)    \
157         BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_REQ, inet_request_sock)        \
158         BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_TW, inet_timewait_sock)        \
159         BTF_SOCK_TYPE(BTF_SOCK_TYPE_REQ, request_sock)                  \
160         BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK, sock)                         \
161         BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK_COMMON, sock_common)           \
162         BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP, tcp_sock)                      \
163         BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_REQ, tcp_request_sock)          \
164         BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_TW, tcp_timewait_sock)          \
165         BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock)                    \
166         BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock)                      \
167         BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock)
168
169 enum {
170 #define BTF_SOCK_TYPE(name, str) name,
171 BTF_SOCK_TYPE_xxx
172 #undef BTF_SOCK_TYPE
173 MAX_BTF_SOCK_TYPE,
174 };
175
176 extern u32 btf_sock_ids[];
177 #endif
178
179 #endif