1 /* SPDX-License-Identifier: GPL-2.0 */
5 /* This set of attributes controls behavior of the
6 * test_loader.c:test_loader__run_subtests().
8 * The test_loader sequentially loads each program in a skeleton.
9 * Programs could be loaded in privileged and unprivileged modes.
10 * - __success, __failure, __msg imply privileged mode;
11 * - __success_unpriv, __failure_unpriv, __msg_unpriv imply
13 * If combination of privileged and unprivileged attributes is present
14 * both modes are used. If none are present privileged mode is implied.
16 * See test_loader.c:drop_capabilities() for exact set of capabilities
17 * that differ between privileged and unprivileged modes.
19 * For test filtering purposes the name of the program loaded in
20 * unprivileged mode is derived from the usual program name by adding
23 * __msg Message expected to be found in the verifier log.
24 * Multiple __msg attributes could be specified.
25 * __msg_unpriv Same as __msg but for unprivileged mode.
27 * __success Expect program load success in privileged mode.
28 * __success_unpriv Expect program load success in unprivileged mode.
30 * __failure Expect program load failure in privileged mode.
31 * __failure_unpriv Expect program load failure in unprivileged mode.
33 * __description Text to be used instead of a program name for display
34 * and filtering purposes.
36 * __log_level Log level to use for the program, numeric value expected.
38 * __flag Adds one flag use for the program, the following values are valid:
39 * - BPF_F_STRICT_ALIGNMENT;
40 * - BPF_F_TEST_RND_HI32;
41 * - BPF_F_TEST_STATE_FREQ;
43 * - BPF_F_XDP_HAS_FRAGS;
45 * Multiple __flag attributes could be specified, the final flags
46 * value is derived by applying binary "or" to all specified values.
48 #define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
49 #define __failure __attribute__((btf_decl_tag("comment:test_expect_failure")))
50 #define __success __attribute__((btf_decl_tag("comment:test_expect_success")))
51 #define __description(desc) __attribute__((btf_decl_tag("comment:test_description=" desc)))
52 #define __msg_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" msg)))
53 #define __failure_unpriv __attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
54 #define __success_unpriv __attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
55 #define __log_level(lvl) __attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
56 #define __flag(flag) __attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
58 /* Convenience macro for use with 'asm volatile' blocks */
59 #define __naked __attribute__((naked))
60 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
61 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
62 #define __imm(name) [name]"i"(name)
63 #define __imm_const(name, expr) [name]"i"(expr)
64 #define __imm_addr(name) [name]"i"(&name)
65 #define __imm_ptr(name) [name]"p"(&name)
66 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
68 #if defined(__TARGET_ARCH_x86)
69 #define SYSCALL_WRAPPER 1
70 #define SYS_PREFIX "__x64_"
71 #elif defined(__TARGET_ARCH_s390)
72 #define SYSCALL_WRAPPER 1
73 #define SYS_PREFIX "__s390x_"
74 #elif defined(__TARGET_ARCH_arm64)
75 #define SYSCALL_WRAPPER 1
76 #define SYS_PREFIX "__arm64_"
78 #define SYSCALL_WRAPPER 0
79 #define SYS_PREFIX "__se_"
82 /* How many arguments are passed to function in register */
83 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
84 #define FUNC_REG_ARG_CNT 6
85 #elif defined(__i386__)
86 #define FUNC_REG_ARG_CNT 3
87 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
88 #define FUNC_REG_ARG_CNT 5
89 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
90 #define FUNC_REG_ARG_CNT 4
91 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
92 #define FUNC_REG_ARG_CNT 8
93 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
94 #define FUNC_REG_ARG_CNT 8
95 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
96 #define FUNC_REG_ARG_CNT 8
97 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
98 #define FUNC_REG_ARG_CNT 6
99 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
100 #define FUNC_REG_ARG_CNT 8
102 /* default to 5 for others */
103 #define FUNC_REG_ARG_CNT 5
106 /* make it look to compiler like value is read and written */
107 #define __sink(expr) asm volatile("" : "+g"(expr))
111 extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __ksym;
112 extern int *bpf_iter_num_next(struct bpf_iter_num *it) __ksym;
113 extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __ksym;
116 /* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for
117 * using BPF open-coded iterators without having to write mundane explicit
118 * low-level loop logic. Instead, it provides for()-like generic construct
119 * that can be used pretty naturally. E.g., for some hypothetical cgroup
120 * iterator, you'd write:
122 * struct cgroup *cg, *parent_cg = <...>;
124 * bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {
125 * bpf_printk("Child cgroup id = %d", cg->cgroup_id);
126 * if (cg->cgroup_id == 123)
130 * I.e., it looks almost like high-level for each loop in other languages,
131 * supports continue/break, and is verifiable by BPF verifier.
133 * For iterating integers, the difference betwen bpf_for_each(num, i, N, M)
134 * and bpf_for(i, N, M) is in that bpf_for() provides additional proof to
135 * verifier that i is in [N, M) range, and in bpf_for_each() case i is `int
136 * *`, not just `int`. So for integers bpf_for() is more convenient.
138 * Note: this macro relies on C99 feature of allowing to declare variables
139 * inside for() loop, bound to for() loop lifetime. It also utilizes GCC
140 * extension: __attribute__((cleanup(<func>))), supported by both GCC and
143 #define bpf_for_each(type, cur, args...) for ( \
144 /* initialize and define destructor */ \
145 struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */, \
146 cleanup(bpf_iter_##type##_destroy))), \
147 /* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */ \
148 *___p __attribute__((unused)) = ( \
149 bpf_iter_##type##_new(&___it, ##args), \
150 /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
151 /* for bpf_iter_##type##_destroy() when used from cleanup() attribute */ \
152 (void)bpf_iter_##type##_destroy, (void *)0); \
153 /* iteration and termination check */ \
154 (((cur) = bpf_iter_##type##_next(&___it))); \
156 #endif /* bpf_for_each */
159 /* bpf_for(i, start, end) implements a for()-like looping construct that sets
160 * provided integer variable *i* to values starting from *start* through,
161 * but not including, *end*. It also proves to BPF verifier that *i* belongs
162 * to range [start, end), so this can be used for accessing arrays without
165 * Note: *start* and *end* are assumed to be expressions with no side effects
166 * and whose values do not change throughout bpf_for() loop execution. They do
167 * not have to be statically known or constant, though.
169 * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
170 * loop bound variables and cleanup attribute, supported by GCC and Clang.
172 #define bpf_for(i, start, end) for ( \
173 /* initialize and define destructor */ \
174 struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
175 cleanup(bpf_iter_num_destroy))), \
176 /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
177 *___p __attribute__((unused)) = ( \
178 bpf_iter_num_new(&___it, (start), (end)), \
179 /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
180 /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
181 (void)bpf_iter_num_destroy, (void *)0); \
183 /* iteration step */ \
184 int *___t = bpf_iter_num_next(&___it); \
185 /* termination and bounds check */ \
186 (___t && ((i) = *___t, (i) >= (start) && (i) < (end))); \
192 /* bpf_repeat(N) performs N iterations without exposing iteration number
194 * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
195 * loop bound variables and cleanup attribute, supported by GCC and Clang.
197 #define bpf_repeat(N) for ( \
198 /* initialize and define destructor */ \
199 struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
200 cleanup(bpf_iter_num_destroy))), \
201 /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
202 *___p __attribute__((unused)) = ( \
203 bpf_iter_num_new(&___it, 0, (N)), \
204 /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
205 /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
206 (void)bpf_iter_num_destroy, (void *)0); \
207 bpf_iter_num_next(&___it); \
210 #endif /* bpf_repeat */