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 * __msg Message expected to be found in the verifier log.
9 * Multiple __msg attributes could be specified.
11 * __success Expect program load success in privileged mode.
13 * __failure Expect program load failure in privileged mode.
15 * __log_level Log level to use for the program, numeric value expected.
17 * __flag Adds one flag use for the program, the following values are valid:
18 * - BPF_F_STRICT_ALIGNMENT;
19 * - BPF_F_TEST_RND_HI32;
20 * - BPF_F_TEST_STATE_FREQ;
22 * - BPF_F_XDP_HAS_FRAGS;
24 * Multiple __flag attributes could be specified, the final flags
25 * value is derived by applying binary "or" to all specified values.
27 #define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
28 #define __failure __attribute__((btf_decl_tag("comment:test_expect_failure")))
29 #define __success __attribute__((btf_decl_tag("comment:test_expect_success")))
30 #define __log_level(lvl) __attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
31 #define __flag(flag) __attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
33 /* Convenience macro for use with 'asm volatile' blocks */
34 #define __naked __attribute__((naked))
35 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
36 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
37 #define __imm(name) [name]"i"(name)
38 #define __imm_const(name, expr) [name]"i"(expr)
39 #define __imm_addr(name) [name]"i"(&name)
40 #define __imm_ptr(name) [name]"p"(&name)
41 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
43 #if defined(__TARGET_ARCH_x86)
44 #define SYSCALL_WRAPPER 1
45 #define SYS_PREFIX "__x64_"
46 #elif defined(__TARGET_ARCH_s390)
47 #define SYSCALL_WRAPPER 1
48 #define SYS_PREFIX "__s390x_"
49 #elif defined(__TARGET_ARCH_arm64)
50 #define SYSCALL_WRAPPER 1
51 #define SYS_PREFIX "__arm64_"
53 #define SYSCALL_WRAPPER 0
54 #define SYS_PREFIX "__se_"
57 /* How many arguments are passed to function in register */
58 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
59 #define FUNC_REG_ARG_CNT 6
60 #elif defined(__i386__)
61 #define FUNC_REG_ARG_CNT 3
62 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
63 #define FUNC_REG_ARG_CNT 5
64 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
65 #define FUNC_REG_ARG_CNT 4
66 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
67 #define FUNC_REG_ARG_CNT 8
68 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
69 #define FUNC_REG_ARG_CNT 8
70 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
71 #define FUNC_REG_ARG_CNT 8
72 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
73 #define FUNC_REG_ARG_CNT 6
74 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
75 #define FUNC_REG_ARG_CNT 8
77 /* default to 5 for others */
78 #define FUNC_REG_ARG_CNT 5
81 /* make it look to compiler like value is read and written */
82 #define __sink(expr) asm volatile("" : "+g"(expr))
86 extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __ksym;
87 extern int *bpf_iter_num_next(struct bpf_iter_num *it) __ksym;
88 extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __ksym;
91 /* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for
92 * using BPF open-coded iterators without having to write mundane explicit
93 * low-level loop logic. Instead, it provides for()-like generic construct
94 * that can be used pretty naturally. E.g., for some hypothetical cgroup
95 * iterator, you'd write:
97 * struct cgroup *cg, *parent_cg = <...>;
99 * bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {
100 * bpf_printk("Child cgroup id = %d", cg->cgroup_id);
101 * if (cg->cgroup_id == 123)
105 * I.e., it looks almost like high-level for each loop in other languages,
106 * supports continue/break, and is verifiable by BPF verifier.
108 * For iterating integers, the difference betwen bpf_for_each(num, i, N, M)
109 * and bpf_for(i, N, M) is in that bpf_for() provides additional proof to
110 * verifier that i is in [N, M) range, and in bpf_for_each() case i is `int
111 * *`, not just `int`. So for integers bpf_for() is more convenient.
113 * Note: this macro relies on C99 feature of allowing to declare variables
114 * inside for() loop, bound to for() loop lifetime. It also utilizes GCC
115 * extension: __attribute__((cleanup(<func>))), supported by both GCC and
118 #define bpf_for_each(type, cur, args...) for ( \
119 /* initialize and define destructor */ \
120 struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */, \
121 cleanup(bpf_iter_##type##_destroy))), \
122 /* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */ \
123 *___p __attribute__((unused)) = ( \
124 bpf_iter_##type##_new(&___it, ##args), \
125 /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
126 /* for bpf_iter_##type##_destroy() when used from cleanup() attribute */ \
127 (void)bpf_iter_##type##_destroy, (void *)0); \
128 /* iteration and termination check */ \
129 (((cur) = bpf_iter_##type##_next(&___it))); \
131 #endif /* bpf_for_each */
134 /* bpf_for(i, start, end) implements a for()-like looping construct that sets
135 * provided integer variable *i* to values starting from *start* through,
136 * but not including, *end*. It also proves to BPF verifier that *i* belongs
137 * to range [start, end), so this can be used for accessing arrays without
140 * Note: *start* and *end* are assumed to be expressions with no side effects
141 * and whose values do not change throughout bpf_for() loop execution. They do
142 * not have to be statically known or constant, though.
144 * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
145 * loop bound variables and cleanup attribute, supported by GCC and Clang.
147 #define bpf_for(i, start, end) for ( \
148 /* initialize and define destructor */ \
149 struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
150 cleanup(bpf_iter_num_destroy))), \
151 /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
152 *___p __attribute__((unused)) = ( \
153 bpf_iter_num_new(&___it, (start), (end)), \
154 /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
155 /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
156 (void)bpf_iter_num_destroy, (void *)0); \
158 /* iteration step */ \
159 int *___t = bpf_iter_num_next(&___it); \
160 /* termination and bounds check */ \
161 (___t && ((i) = *___t, (i) >= (start) && (i) < (end))); \
167 /* bpf_repeat(N) performs N iterations without exposing iteration number
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_repeat(N) 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, 0, (N)), \
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); \
182 bpf_iter_num_next(&___it); \
185 #endif /* bpf_repeat */