xref: /linux/tools/testing/selftests/bpf/progs/bpf_misc.h (revision 0074281bb6316108e0cff094bd4db78ab3eee236)
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 /* This set of attributes controls behavior of the
13  * test_loader.c:test_loader__run_subtests().
14  *
15  * The test_loader sequentially loads each program in a skeleton.
16  * Programs could be loaded in privileged and unprivileged modes.
17  * - __success, __failure, __msg, __regex imply privileged mode;
18  * - __success_unpriv, __failure_unpriv, __msg_unpriv, __regex_unpriv
19  *   imply unprivileged mode.
20  * If combination of privileged and unprivileged attributes is present
21  * both modes are used. If none are present privileged mode is implied.
22  *
23  * See test_loader.c:drop_capabilities() for exact set of capabilities
24  * that differ between privileged and unprivileged modes.
25  *
26  * For test filtering purposes the name of the program loaded in
27  * unprivileged mode is derived from the usual program name by adding
28  * `@unpriv' suffix.
29  *
30  * __msg             Message expected to be found in the verifier log.
31  *                   Multiple __msg attributes could be specified.
32  *                   To match a regular expression use "{{" "}}" brackets,
33  *                   e.g. "foo{{[0-9]+}}"  matches strings like "foo007".
34  *                   Extended POSIX regular expression syntax is allowed
35  *                   inside the brackets.
36  * __msg_unpriv      Same as __msg but for unprivileged mode.
37  *
38  * __xlated          Expect a line in a disassembly log after verifier applies rewrites.
39  *                   Multiple __xlated attributes could be specified.
40  *                   Regular expressions could be specified same way as in __msg.
41  * __xlated_unpriv   Same as __xlated but for unprivileged mode.
42  *
43  * __jited           Match a line in a disassembly of the jited BPF program.
44  *                   Has to be used after __arch_* macro.
45  *                   For example:
46  *
47  *                       __arch_x86_64
48  *                       __jited("   endbr64")
49  *                       __jited("   nopl    (%rax,%rax)")
50  *                       __jited("   xorq    %rax, %rax")
51  *                       ...
52  *                       __naked void some_test(void)
53  *                       {
54  *                           asm volatile (... ::: __clobber_all);
55  *                       }
56  *
57  *                   Regular expressions could be included in patterns same way
58  *                   as in __msg.
59  *
60  *                   By default assume that each pattern has to be matched on the
61  *                   next consecutive line of disassembly, e.g.:
62  *
63  *                       __jited("   endbr64")             # matched on line N
64  *                       __jited("   nopl    (%rax,%rax)") # matched on line N+1
65  *
66  *                   If match occurs on a wrong line an error is reported.
67  *                   To override this behaviour use literal "...", e.g.:
68  *
69  *                       __jited("   endbr64")             # matched on line N
70  *                       __jited("...")                    # not matched
71  *                       __jited("   nopl    (%rax,%rax)") # matched on any line >= N
72  *
73  * __jited_unpriv    Same as __jited but for unprivileged mode.
74  *
75  *
76  * __success         Expect program load success in privileged mode.
77  * __success_unpriv  Expect program load success in unprivileged mode.
78  *
79  * __failure         Expect program load failure in privileged mode.
80  * __failure_unpriv  Expect program load failure in unprivileged mode.
81  *
82  * __retval          Execute the program using BPF_PROG_TEST_RUN command,
83  *                   expect return value to match passed parameter:
84  *                   - a decimal number
85  *                   - a hexadecimal number, when starts from 0x
86  *                   - a macro which expands to one of the above
87  *                   - literal _INT_MIN (expands to INT_MIN)
88  *                   In addition, two special macros are defined below:
89  *                   - POINTER_VALUE
90  *                   - TEST_DATA_LEN
91  * __retval_unpriv   Same, but load program in unprivileged mode.
92  *
93  * __description     Text to be used instead of a program name for display
94  *                   and filtering purposes.
95  *
96  * __log_level       Log level to use for the program, numeric value expected.
97  *
98  * __flag            Adds one flag use for the program, the following values are valid:
99  *                   - BPF_F_STRICT_ALIGNMENT;
100  *                   - BPF_F_TEST_RND_HI32;
101  *                   - BPF_F_TEST_STATE_FREQ;
102  *                   - BPF_F_SLEEPABLE;
103  *                   - BPF_F_XDP_HAS_FRAGS;
104  *                   - A numeric value.
105  *                   Multiple __flag attributes could be specified, the final flags
106  *                   value is derived by applying binary "or" to all specified values.
107  *
108  * __auxiliary         Annotated program is not a separate test, but used as auxiliary
109  *                     for some other test cases and should always be loaded.
110  * __auxiliary_unpriv  Same, but load program in unprivileged mode.
111  *
112  * __arch_*          Specify on which architecture the test case should be tested.
113  *                   Several __arch_* annotations could be specified at once.
114  *                   When test case is not run on current arch it is marked as skipped.
115  * __caps_unpriv     Specify the capabilities that should be set when running the test.
116  */
117 #define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg)))
118 #define __xlated(msg)		__attribute__((btf_decl_tag("comment:test_expect_xlated=" XSTR(__COUNTER__) "=" msg)))
119 #define __jited(msg)		__attribute__((btf_decl_tag("comment:test_jited=" XSTR(__COUNTER__) "=" msg)))
120 #define __failure		__attribute__((btf_decl_tag("comment:test_expect_failure")))
121 #define __success		__attribute__((btf_decl_tag("comment:test_expect_success")))
122 #define __description(desc)	__attribute__((btf_decl_tag("comment:test_description=" desc)))
123 #define __msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" XSTR(__COUNTER__) "=" msg)))
124 #define __xlated_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_xlated_unpriv=" XSTR(__COUNTER__) "=" msg)))
125 #define __jited_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_jited=" XSTR(__COUNTER__) "=" msg)))
126 #define __failure_unpriv	__attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
127 #define __success_unpriv	__attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
128 #define __log_level(lvl)	__attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
129 #define __flag(flag)		__attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
130 #define __retval(val)		__attribute__((btf_decl_tag("comment:test_retval="XSTR(val))))
131 #define __retval_unpriv(val)	__attribute__((btf_decl_tag("comment:test_retval_unpriv="XSTR(val))))
132 #define __auxiliary		__attribute__((btf_decl_tag("comment:test_auxiliary")))
133 #define __auxiliary_unpriv	__attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
134 #define __btf_path(path)	__attribute__((btf_decl_tag("comment:test_btf_path=" path)))
135 #define __arch(arch)		__attribute__((btf_decl_tag("comment:test_arch=" arch)))
136 #define __arch_x86_64		__arch("X86_64")
137 #define __arch_arm64		__arch("ARM64")
138 #define __arch_riscv64		__arch("RISCV64")
139 #define __caps_unpriv(caps)	__attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
140 #define __load_if_JITed()	__attribute__((btf_decl_tag("comment:load_mode=jited")))
141 #define __load_if_no_JITed()	__attribute__((btf_decl_tag("comment:load_mode=no_jited")))
142 
143 /* Define common capabilities tested using __caps_unpriv */
144 #define CAP_NET_ADMIN		12
145 #define CAP_SYS_ADMIN		21
146 #define CAP_PERFMON		38
147 #define CAP_BPF			39
148 
149 /* Convenience macro for use with 'asm volatile' blocks */
150 #define __naked __attribute__((naked))
151 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
152 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
153 #define __imm(name) [name]"i"(name)
154 #define __imm_const(name, expr) [name]"i"(expr)
155 #define __imm_addr(name) [name]"i"(&name)
156 #define __imm_ptr(name) [name]"r"(&name)
157 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
158 
159 /* Magic constants used with __retval() */
160 #define POINTER_VALUE	0xbadcafe
161 #define TEST_DATA_LEN	64
162 
163 #ifndef __used
164 #define __used __attribute__((used))
165 #endif
166 
167 #if defined(__TARGET_ARCH_x86)
168 #define SYSCALL_WRAPPER 1
169 #define SYS_PREFIX "__x64_"
170 #elif defined(__TARGET_ARCH_s390)
171 #define SYSCALL_WRAPPER 1
172 #define SYS_PREFIX "__s390x_"
173 #elif defined(__TARGET_ARCH_arm64)
174 #define SYSCALL_WRAPPER 1
175 #define SYS_PREFIX "__arm64_"
176 #elif defined(__TARGET_ARCH_riscv)
177 #define SYSCALL_WRAPPER 1
178 #define SYS_PREFIX "__riscv_"
179 #elif defined(__TARGET_ARCH_powerpc)
180 #define SYSCALL_WRAPPER 1
181 #define SYS_PREFIX ""
182 #else
183 #define SYSCALL_WRAPPER 0
184 #define SYS_PREFIX "__se_"
185 #endif
186 
187 /* How many arguments are passed to function in register */
188 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
189 #define FUNC_REG_ARG_CNT 6
190 #elif defined(__i386__)
191 #define FUNC_REG_ARG_CNT 3
192 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
193 #define FUNC_REG_ARG_CNT 5
194 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
195 #define FUNC_REG_ARG_CNT 4
196 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
197 #define FUNC_REG_ARG_CNT 8
198 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
199 #define FUNC_REG_ARG_CNT 8
200 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
201 #define FUNC_REG_ARG_CNT 8
202 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
203 #define FUNC_REG_ARG_CNT 6
204 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
205 #define FUNC_REG_ARG_CNT 8
206 #else
207 /* default to 5 for others */
208 #define FUNC_REG_ARG_CNT 5
209 #endif
210 
211 /* make it look to compiler like value is read and written */
212 #define __sink(expr) asm volatile("" : "+g"(expr))
213 
214 #ifndef ARRAY_SIZE
215 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
216 #endif
217 
218 #if (defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) ||	\
219      (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64) ||		\
220      defined(__TARGET_ARCH_arm) || defined(__TARGET_ARCH_s390) ||	\
221      defined(__TARGET_ARCH_loongarch)) &&				\
222 	__clang_major__ >= 18
223 #define CAN_USE_GOTOL
224 #endif
225 
226 #if __clang_major__ >= 18
227 #define CAN_USE_BPF_ST
228 #endif
229 
230 #if __clang_major__ >= 18 && defined(ENABLE_ATOMICS_TESTS) &&		\
231 	(defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) ||	\
232 	 (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64)) || \
233 	  (defined(__TARGET_ARCH_powerpc))
234 #define CAN_USE_LOAD_ACQ_STORE_REL
235 #endif
236 
237 #if defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86)
238 #define SPEC_V1
239 #endif
240 
241 #if defined(__TARGET_ARCH_x86)
242 #define SPEC_V4
243 #endif
244 
245 #endif
246