xref: /linux/tools/testing/selftests/bpf/progs/bpf_misc.h (revision 9fb8927b956b5e1e540756d3d786b522e5ca332a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BPF_MISC_H__
3 #define __BPF_MISC_H__
4 
5 #define XSTR(s) STR(s)
6 #define STR(s) #s
7 
8 /* Expand a macro and then stringize the expansion */
9 #define QUOTE(str) #str
10 #define EXPAND_QUOTE(str) QUOTE(str)
11 
12 /*
13  * This set of attributes controls behavior of the
14  * test_loader.c:test_loader__run_subtests().
15  *
16  * The test_loader sequentially loads each program in a skeleton.
17  * Programs could be loaded in privileged and unprivileged modes.
18  * - __success, __failure, __msg, __regex imply privileged mode;
19  * - __success_unpriv, __failure_unpriv, __msg_unpriv, __regex_unpriv
20  *   imply unprivileged mode.
21  * If combination of privileged and unprivileged attributes is present
22  * both modes are used. If none are present privileged mode is implied.
23  *
24  * See test_loader.c:drop_capabilities() for exact set of capabilities
25  * that differ between privileged and unprivileged modes.
26  *
27  * For test filtering purposes the name of the program loaded in
28  * unprivileged mode is derived from the usual program name by adding
29  * `@unpriv' suffix.
30  *
31  * __msg             Message expected to be found in the verifier log.
32  *                   Multiple __msg attributes could be specified.
33  *                   To match a regular expression use "{{" "}}" brackets,
34  *                   e.g. "foo{{[0-9]+}}"  matches strings like "foo007".
35  *                   Extended POSIX regular expression syntax is allowed
36  *                   inside the brackets.
37  * __not_msg         Message not expected to be found in verifier log.
38  *                   If __msg_not is situated between __msg tags
39  *                   framework matches __msg tags first, and then
40  *                   checks that __msg_not is not present in a portion of
41  *                   a log between bracketing __msg tags.
42  *                   Same regex syntax as for __msg is supported.
43  * __msg_unpriv      Same as __msg but for unprivileged mode.
44  * __not_msg_unpriv  Same as __not_msg but for unprivileged mode.
45  *
46  * __msg_next        Same as __msg but expects the match to be on the next
47  * __msg_next_unpriv log line compared to the last match, not just some line
48  *                   after the last match.
49  *
50  * __stderr          Message expected to be found in bpf stderr stream. The
51  *                   same regex rules apply like __msg.
52  * __stderr_unpriv   Same as __stderr but for unpriveleged mode.
53  * __stdout          Same as __stderr but for stdout stream.
54  * __stdout_unpriv   Same as __stdout but for unpriveleged mode.
55  *
56  * __xlated          Expect a line in a disassembly log after verifier applies rewrites.
57  *                   Multiple __xlated attributes could be specified.
58  *                   Regular expressions could be specified same way as in __msg.
59  * __xlated_unpriv   Same as __xlated but for unprivileged mode.
60  *
61  * __jited           Match a line in a disassembly of the jited BPF program.
62  *                   Has to be used after __arch_* macro.
63  *                   For example:
64  *
65  *                       __arch_x86_64
66  *                       __jited("   endbr64")
67  *                       __jited("   nopl    (%rax,%rax)")
68  *                       __jited("   xorq    %rax, %rax")
69  *                       ...
70  *                       __naked void some_test(void)
71  *                       {
72  *                           asm volatile (... ::: __clobber_all);
73  *                       }
74  *
75  *                   Regular expressions could be included in patterns same way
76  *                   as in __msg.
77  *
78  *                   By default assume that each pattern has to be matched on the
79  *                   next consecutive line of disassembly, e.g.:
80  *
81  *                       __jited("   endbr64")             # matched on line N
82  *                       __jited("   nopl    (%rax,%rax)") # matched on line N+1
83  *
84  *                   If match occurs on a wrong line an error is reported.
85  *                   To override this behaviour use literal "...", e.g.:
86  *
87  *                       __jited("   endbr64")             # matched on line N
88  *                       __jited("...")                    # not matched
89  *                       __jited("   nopl    (%rax,%rax)") # matched on any line >= N
90  *
91  * __jited_unpriv    Same as __jited but for unprivileged mode.
92  *
93  *
94  * __success         Expect program load success in privileged mode.
95  * __success_unpriv  Expect program load success in unprivileged mode.
96  *
97  * __failure         Expect program load failure in privileged mode.
98  * __failure_unpriv  Expect program load failure in unprivileged mode.
99  *
100  * __retval          Execute the program using BPF_PROG_TEST_RUN command,
101  *                   expect return value to match passed parameter:
102  *                   - a decimal number
103  *                   - a hexadecimal number, when starts from 0x
104  *                   - a macro which expands to one of the above
105  *                   - literal _INT_MIN (expands to INT_MIN)
106  *                   In addition, two special macros are defined below:
107  *                   - POINTER_VALUE
108  *                   - TEST_DATA_LEN
109  * __retval_unpriv   Same, but load program in unprivileged mode.
110  *
111  * __description     Text to be used for display and as an additional filter
112  *                   alias, while the original program name stays matchable.
113  *
114  * __skip            Report the test as SKIP with the given reason instead of
115  *                   running it. For placeholder programs that stand in for a
116  *                   feature the toolchain or JIT cannot provide, so a run does
117  *                   not report OK for coverage it never executed.
118  *
119  * __log_level       Log level to use for the program, numeric value expected.
120  *
121  * __flag            Adds one flag use for the program, the following values are valid:
122  *                   - BPF_F_STRICT_ALIGNMENT;
123  *                   - BPF_F_TEST_RND_HI32;
124  *                   - BPF_F_TEST_STATE_FREQ;
125  *                   - BPF_F_SLEEPABLE;
126  *                   - BPF_F_XDP_HAS_FRAGS;
127  *                   - A numeric value.
128  *                   Multiple __flag attributes could be specified, the final flags
129  *                   value is derived by applying binary "or" to all specified values.
130  *
131  * __auxiliary         Annotated program is not a separate test, but used as auxiliary
132  *                     for some other test cases and should always be loaded.
133  * __auxiliary_unpriv  Same, but load program in unprivileged mode.
134  *
135  * __arch_*          Specify on which architecture the test case should be tested.
136  *                   Several __arch_* annotations could be specified at once.
137  *                   When test case is not run on current arch it is marked as skipped.
138  * __caps_unpriv     Specify the capabilities that should be set when running the test.
139  * __prepare_priv    In unprivileged mode, prepare the object with the fixture's
140  *                   initial capabilities before dropping them for program loading.
141  *                   Preparation includes map creation and BTF/kfunc resolution;
142  *                   these operations are not tested at the reduced capabilities.
143  *                   Program loading uses the normal __caps_unpriv selection.
144  *
145  * __linear_size     Specify the size of the linear area of non-linear skbs, or
146  *                   0 for linear skbs.
147  */
148 #define __test_tag(tag)		__attribute__((btf_decl_tag("comment:" XSTR(__COUNTER__) ":" tag)))
149 
150 #define __msg(msg)		__test_tag("test_expect_msg=" msg)
151 #define __not_msg(msg)		__test_tag("test_expect_not_msg=" msg)
152 #define __msg_next(msg)		__test_tag("test_expect_msg_next=" msg)
153 #define __xlated(msg)		__test_tag("test_expect_xlated=" msg)
154 #define __jited(msg)		__test_tag("test_jited=" msg)
155 #define __failure		__test_tag("test_expect_failure")
156 #define __success		__test_tag("test_expect_success")
157 #define __description(desc)	__test_tag("test_description=" desc)
158 #define __skip(reason)		__test_tag("test_skip=" reason)
159 #define __msg_unpriv(msg)	__test_tag("test_expect_msg_unpriv=" msg)
160 #define __not_msg_unpriv(msg)	__test_tag("test_expect_not_msg_unpriv=" msg)
161 #define __msg_next_unpriv(msg)	__test_tag("test_expect_msg_next_unpriv=" msg)
162 #define __xlated_unpriv(msg)	__test_tag("test_expect_xlated_unpriv=" msg)
163 #define __jited_unpriv(msg)	__test_tag("test_jited_unpriv=" msg)
164 #define __failure_unpriv	__test_tag("test_expect_failure_unpriv")
165 #define __success_unpriv	__test_tag("test_expect_success_unpriv")
166 #define __log_level(lvl)	__test_tag("test_log_level=" #lvl)
167 #define __flag(flag)		__test_tag("test_prog_flags=" #flag)
168 #define __retval(val)		__test_tag("test_retval=" XSTR(val))
169 #define __retval_unpriv(val)	__test_tag("test_retval_unpriv=" XSTR(val))
170 #define __auxiliary		__test_tag("test_auxiliary")
171 #define __auxiliary_unpriv	__test_tag("test_auxiliary_unpriv")
172 #define __btf_path(path)	__test_tag("test_btf_path=" path)
173 #define __btf_func_path(path)	__test_tag("test_btf_func_path=" path)
174 #define __arch(arch)		__test_tag("test_arch=" arch)
175 #define __arch_x86_64		__arch("X86_64")
176 #define __arch_arm64		__arch("ARM64")
177 #define __arch_riscv64		__arch("RISCV64")
178 #define __arch_s390x		__arch("s390x")
179 #define __arch_loongarch	__arch("LOONGARCH")
180 #define __caps_unpriv(caps)	__test_tag("test_caps_unpriv=" EXPAND_QUOTE(caps))
181 #define __prepare_priv		__test_tag("test_prepare_priv")
182 #define __load_if_JITed()	__test_tag("load_mode=jited")
183 #define __load_if_no_JITed()	__test_tag("load_mode=no_jited")
184 /* Whether programs may use more than 512 bytes of stack on this kernel and JIT */
185 #define __load_if_large_stack()		__test_tag("stack_mode=large")
186 #define __load_if_no_large_stack()	__test_tag("stack_mode=small")
187 #define __stderr(msg)		__test_tag("test_expect_stderr=" msg)
188 #define __stderr_unpriv(msg)	__test_tag("test_expect_stderr_unpriv=" msg)
189 #define __stdout(msg)		__test_tag("test_expect_stdout=" msg)
190 #define __stdout_unpriv(msg)	__test_tag("test_expect_stdout_unpriv=" msg)
191 #define __linear_size(sz)	__test_tag("test_linear_size=" XSTR(sz))
192 
193 /* Define common capabilities tested using __caps_unpriv */
194 #define CAP_NET_ADMIN		12
195 #define CAP_SYS_ADMIN		21
196 #define CAP_PERFMON		38
197 #define CAP_BPF			39
198 
199 /* Convenience macro for use with 'asm volatile' blocks */
200 #define __naked __attribute__((naked))
201 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
202 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
203 #define __imm(name) [name]"i"(name)
204 #define __imm_const(name, expr) [name]"i"(expr)
205 #define __imm_addr(name) [name]"i"(&name)
206 #define __imm_ptr(name) [name]"r"(&name)
207 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
208 
209 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
210 #define offsetofend(TYPE, MEMBER) \
211 	(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
212 
213 /* Magic constants used with __retval() */
214 #define POINTER_VALUE	0xbadcafe
215 #define TEST_DATA_LEN	64
216 
217 #ifndef __aligned
218 #define __aligned(x) __attribute__((aligned(x)))
219 #endif
220 
221 #ifndef __used
222 #define __used __attribute__((used))
223 #endif
224 
225 #if defined(__TARGET_ARCH_x86)
226 #define SYSCALL_WRAPPER 1
227 #define SYS_PREFIX "__x64_"
228 #elif defined(__TARGET_ARCH_s390)
229 #define SYSCALL_WRAPPER 1
230 #define SYS_PREFIX "__s390x_"
231 #elif defined(__TARGET_ARCH_arm64)
232 #define SYSCALL_WRAPPER 1
233 #define SYS_PREFIX "__arm64_"
234 #elif defined(__TARGET_ARCH_riscv)
235 #define SYSCALL_WRAPPER 1
236 #define SYS_PREFIX "__riscv_"
237 #elif defined(__TARGET_ARCH_powerpc)
238 #define SYSCALL_WRAPPER 1
239 #define SYS_PREFIX ""
240 #else
241 #define SYSCALL_WRAPPER 0
242 #define SYS_PREFIX "__se_"
243 #endif
244 
245 /* How many arguments are passed to function in register */
246 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
247 #define FUNC_REG_ARG_CNT 6
248 #elif defined(__i386__)
249 #define FUNC_REG_ARG_CNT 3
250 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
251 #define FUNC_REG_ARG_CNT 5
252 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
253 #define FUNC_REG_ARG_CNT 4
254 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
255 #define FUNC_REG_ARG_CNT 8
256 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
257 #define FUNC_REG_ARG_CNT 8
258 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
259 #define FUNC_REG_ARG_CNT 8
260 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
261 #define FUNC_REG_ARG_CNT 6
262 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
263 #define FUNC_REG_ARG_CNT 8
264 #else
265 /* default to 5 for others */
266 #define FUNC_REG_ARG_CNT 5
267 #endif
268 
269 /* make it look to compiler like value is read and written */
270 #define __sink(expr) asm volatile("" : "+g"(expr))
271 
272 #ifndef ARRAY_SIZE
273 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
274 #endif
275 
276 #if (defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) ||	\
277      (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64) ||		\
278      defined(__TARGET_ARCH_arm) || defined(__TARGET_ARCH_s390) ||	\
279      defined(__TARGET_ARCH_loongarch)) &&				\
280 	(__clang_major__ >= 18 || defined(__BPF_FEATURE_GOTOL))
281 #define CAN_USE_GOTOL
282 #endif
283 
284 #if __clang_major__ >= 18 || defined(__BPF_FEATURE_ST)
285 #define CAN_USE_BPF_ST
286 #endif
287 
288 #if __clang_major__ >= 18 && defined(ENABLE_ATOMICS_TESTS) &&		\
289 	(defined(__TARGET_ARCH_arm64) ||				\
290 	 defined(__TARGET_ARCH_x86) ||					\
291 	 (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64) ||	\
292 	 defined(__TARGET_ARCH_s390) ||					\
293 	 defined(__TARGET_ARCH_powerpc) ||				\
294 	 defined(__TARGET_ARCH_loongarch))
295 #define CAN_USE_LOAD_ACQ_STORE_REL
296 #endif
297 
298 #if defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86)
299 #define SPEC_V1
300 #endif
301 
302 #if defined(__TARGET_ARCH_x86)
303 #define SPEC_V4
304 #endif
305 
306 #endif
307