9defc217a5bd1a1410fd04c8daca22a22f93278b
[platform/kernel/linux-starfive.git] / tools / testing / selftests / bpf / progs / bpf_misc.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BPF_MISC_H__
3 #define __BPF_MISC_H__
4
5 /* This set of attributes controls behavior of the
6  * test_loader.c:test_loader__run_subtests().
7  *
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
12  *   unprivileged mode.
13  * If combination of privileged and unprivileged attributes is present
14  * both modes are used. If none are present privileged mode is implied.
15  *
16  * See test_loader.c:drop_capabilities() for exact set of capabilities
17  * that differ between privileged and unprivileged modes.
18  *
19  * For test filtering purposes the name of the program loaded in
20  * unprivileged mode is derived from the usual program name by adding
21  * `@unpriv' suffix.
22  *
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.
26  *
27  * __success         Expect program load success in privileged mode.
28  * __success_unpriv  Expect program load success in unprivileged mode.
29  *
30  * __failure         Expect program load failure in privileged mode.
31  * __failure_unpriv  Expect program load failure in unprivileged mode.
32  *
33  * __description     Text to be used instead of a program name for display
34  *                   and filtering purposes.
35  *
36  * __log_level       Log level to use for the program, numeric value expected.
37  *
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;
42  *                   - BPF_F_SLEEPABLE;
43  *                   - BPF_F_XDP_HAS_FRAGS;
44  *                   - A numeric value.
45  *                   Multiple __flag attributes could be specified, the final flags
46  *                   value is derived by applying binary "or" to all specified values.
47  */
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)))
57
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))
67
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_"
77 #else
78 #define SYSCALL_WRAPPER 0
79 #define SYS_PREFIX "__se_"
80 #endif
81
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
101 #else
102 /* default to 5 for others */
103 #define FUNC_REG_ARG_CNT 5
104 #endif
105
106 /* make it look to compiler like value is read and written */
107 #define __sink(expr) asm volatile("" : "+g"(expr))
108
109 struct bpf_iter_num;
110
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;
114
115 #ifndef bpf_for_each
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:
121  *
122  * struct cgroup *cg, *parent_cg = <...>;
123  *
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)
127  *         break;
128  * }
129  *
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.
132  *
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.
137  *
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
141  * Clang.
142  */
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)));                                             \
155 )
156 #endif /* bpf_for_each */
157
158 #ifndef bpf_for
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
163  * extra checks.
164  *
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.
168  *
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.
171  */
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);                         \
182         ({                                                                                      \
183                 /* iteration step */                                                            \
184                 int *___t = bpf_iter_num_next(&___it);                                          \
185                 /* termination and bounds check */                                              \
186                 (___t && ((i) = *___t, (i) >= (start) && (i) < (end)));                         \
187         });                                                                                     \
188 )
189 #endif /* bpf_for */
190
191 #ifndef bpf_repeat
192 /* bpf_repeat(N) performs N iterations without exposing iteration number
193  *
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.
196  */
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);                                                              \
208         /* nothing here  */                                                                     \
209 )
210 #endif /* bpf_repeat */
211
212 #endif