selftests/bpf: __imm_insn & __imm_const macro for bpf_misc.h
[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  * __msg             Message expected to be found in the verifier log.
9  *                   Multiple __msg attributes could be specified.
10  *
11  * __success         Expect program load success in privileged mode.
12  *
13  * __failure         Expect program load failure in privileged mode.
14  *
15  * __log_level       Log level to use for the program, numeric value expected.
16  *
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;
21  *                   - BPF_F_SLEEPABLE;
22  *                   - BPF_F_XDP_HAS_FRAGS;
23  *                   - A numeric value.
24  *                   Multiple __flag attributes could be specified, the final flags
25  *                   value is derived by applying binary "or" to all specified values.
26  */
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)))
32
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))
42
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_"
52 #else
53 #define SYSCALL_WRAPPER 0
54 #define SYS_PREFIX "__se_"
55 #endif
56
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
76 #else
77 /* default to 5 for others */
78 #define FUNC_REG_ARG_CNT 5
79 #endif
80
81 /* make it look to compiler like value is read and written */
82 #define __sink(expr) asm volatile("" : "+g"(expr))
83
84 struct bpf_iter_num;
85
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;
89
90 #ifndef bpf_for_each
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:
96  *
97  * struct cgroup *cg, *parent_cg = <...>;
98  *
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)
102  *         break;
103  * }
104  *
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.
107  *
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.
112  *
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
116  * Clang.
117  */
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)));                                             \
130 )
131 #endif /* bpf_for_each */
132
133 #ifndef bpf_for
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
138  * extra checks.
139  *
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.
143  *
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.
146  */
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);                         \
157         ({                                                                                      \
158                 /* iteration step */                                                            \
159                 int *___t = bpf_iter_num_next(&___it);                                          \
160                 /* termination and bounds check */                                              \
161                 (___t && ((i) = *___t, (i) >= (start) && (i) < (end)));                         \
162         });                                                                                     \
163 )
164 #endif /* bpf_for */
165
166 #ifndef bpf_repeat
167 /* bpf_repeat(N) performs N iterations without exposing iteration number
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_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);                                                              \
183         /* nothing here  */                                                                     \
184 )
185 #endif /* bpf_repeat */
186
187 #endif