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  * __retval          Execute the program using BPF_PROG_TEST_RUN command,
34  *                   expect return value to match passed parameter:
35  *                   - a decimal number
36  *                   - a hexadecimal number, when starts from 0x
37  *                   - literal INT_MIN
38  *                   - literal POINTER_VALUE (see definition below)
39  *                   - literal TEST_DATA_LEN (see definition below)
40  * __retval_unpriv   Same, but load program in unprivileged mode.
41  *
42  * __description     Text to be used instead of a program name for display
43  *                   and filtering purposes.
44  *
45  * __log_level       Log level to use for the program, numeric value expected.
46  *
47  * __flag            Adds one flag use for the program, the following values are valid:
48  *                   - BPF_F_STRICT_ALIGNMENT;
49  *                   - BPF_F_TEST_RND_HI32;
50  *                   - BPF_F_TEST_STATE_FREQ;
51  *                   - BPF_F_SLEEPABLE;
52  *                   - BPF_F_XDP_HAS_FRAGS;
53  *                   - A numeric value.
54  *                   Multiple __flag attributes could be specified, the final flags
55  *                   value is derived by applying binary "or" to all specified values.
56  */
57 #define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
58 #define __failure		__attribute__((btf_decl_tag("comment:test_expect_failure")))
59 #define __success		__attribute__((btf_decl_tag("comment:test_expect_success")))
60 #define __description(desc)	__attribute__((btf_decl_tag("comment:test_description=" desc)))
61 #define __msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" msg)))
62 #define __failure_unpriv	__attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
63 #define __success_unpriv	__attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
64 #define __log_level(lvl)	__attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
65 #define __flag(flag)		__attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
66 #define __retval(val)		__attribute__((btf_decl_tag("comment:test_retval="#val)))
67 #define __retval_unpriv(val)	__attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
68 
69 /* Convenience macro for use with 'asm volatile' blocks */
70 #define __naked __attribute__((naked))
71 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
72 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
73 #define __imm(name) [name]"i"(name)
74 #define __imm_const(name, expr) [name]"i"(expr)
75 #define __imm_addr(name) [name]"i"(&name)
76 #define __imm_ptr(name) [name]"p"(&name)
77 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
78 
79 /* Magic constants used with __retval() */
80 #define POINTER_VALUE	0xcafe4all
81 #define TEST_DATA_LEN	64
82 
83 #if defined(__TARGET_ARCH_x86)
84 #define SYSCALL_WRAPPER 1
85 #define SYS_PREFIX "__x64_"
86 #elif defined(__TARGET_ARCH_s390)
87 #define SYSCALL_WRAPPER 1
88 #define SYS_PREFIX "__s390x_"
89 #elif defined(__TARGET_ARCH_arm64)
90 #define SYSCALL_WRAPPER 1
91 #define SYS_PREFIX "__arm64_"
92 #else
93 #define SYSCALL_WRAPPER 0
94 #define SYS_PREFIX "__se_"
95 #endif
96 
97 /* How many arguments are passed to function in register */
98 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
99 #define FUNC_REG_ARG_CNT 6
100 #elif defined(__i386__)
101 #define FUNC_REG_ARG_CNT 3
102 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
103 #define FUNC_REG_ARG_CNT 5
104 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
105 #define FUNC_REG_ARG_CNT 4
106 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
107 #define FUNC_REG_ARG_CNT 8
108 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
109 #define FUNC_REG_ARG_CNT 8
110 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
111 #define FUNC_REG_ARG_CNT 8
112 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
113 #define FUNC_REG_ARG_CNT 6
114 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
115 #define FUNC_REG_ARG_CNT 8
116 #else
117 /* default to 5 for others */
118 #define FUNC_REG_ARG_CNT 5
119 #endif
120 
121 /* make it look to compiler like value is read and written */
122 #define __sink(expr) asm volatile("" : "+g"(expr))
123 
124 struct bpf_iter_num;
125 
126 extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __ksym;
127 extern int *bpf_iter_num_next(struct bpf_iter_num *it) __ksym;
128 extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __ksym;
129 
130 #ifndef bpf_for_each
131 /* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for
132  * using BPF open-coded iterators without having to write mundane explicit
133  * low-level loop logic. Instead, it provides for()-like generic construct
134  * that can be used pretty naturally. E.g., for some hypothetical cgroup
135  * iterator, you'd write:
136  *
137  * struct cgroup *cg, *parent_cg = <...>;
138  *
139  * bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {
140  *     bpf_printk("Child cgroup id = %d", cg->cgroup_id);
141  *     if (cg->cgroup_id == 123)
142  *         break;
143  * }
144  *
145  * I.e., it looks almost like high-level for each loop in other languages,
146  * supports continue/break, and is verifiable by BPF verifier.
147  *
148  * For iterating integers, the difference betwen bpf_for_each(num, i, N, M)
149  * and bpf_for(i, N, M) is in that bpf_for() provides additional proof to
150  * verifier that i is in [N, M) range, and in bpf_for_each() case i is `int
151  * *`, not just `int`. So for integers bpf_for() is more convenient.
152  *
153  * Note: this macro relies on C99 feature of allowing to declare variables
154  * inside for() loop, bound to for() loop lifetime. It also utilizes GCC
155  * extension: __attribute__((cleanup(<func>))), supported by both GCC and
156  * Clang.
157  */
158 #define bpf_for_each(type, cur, args...) for (							\
159 	/* initialize and define destructor */							\
160 	struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */,	\
161 						    cleanup(bpf_iter_##type##_destroy))),	\
162 	/* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */		\
163 			       *___p __attribute__((unused)) = (				\
164 					bpf_iter_##type##_new(&___it, ##args),			\
165 	/* this is a workaround for Clang bug: it currently doesn't emit BTF */			\
166 	/* for bpf_iter_##type##_destroy() when used from cleanup() attribute */		\
167 					(void)bpf_iter_##type##_destroy, (void *)0);		\
168 	/* iteration and termination check */							\
169 	(((cur) = bpf_iter_##type##_next(&___it)));						\
170 )
171 #endif /* bpf_for_each */
172 
173 #ifndef bpf_for
174 /* bpf_for(i, start, end) implements a for()-like looping construct that sets
175  * provided integer variable *i* to values starting from *start* through,
176  * but not including, *end*. It also proves to BPF verifier that *i* belongs
177  * to range [start, end), so this can be used for accessing arrays without
178  * extra checks.
179  *
180  * Note: *start* and *end* are assumed to be expressions with no side effects
181  * and whose values do not change throughout bpf_for() loop execution. They do
182  * not have to be statically known or constant, though.
183  *
184  * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
185  * loop bound variables and cleanup attribute, supported by GCC and Clang.
186  */
187 #define bpf_for(i, start, end) for (								\
188 	/* initialize and define destructor */							\
189 	struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */	\
190 						 cleanup(bpf_iter_num_destroy))),		\
191 	/* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */		\
192 			    *___p __attribute__((unused)) = (					\
193 				bpf_iter_num_new(&___it, (start), (end)),			\
194 	/* this is a workaround for Clang bug: it currently doesn't emit BTF */			\
195 	/* for bpf_iter_num_destroy() when used from cleanup() attribute */			\
196 				(void)bpf_iter_num_destroy, (void *)0);				\
197 	({											\
198 		/* iteration step */								\
199 		int *___t = bpf_iter_num_next(&___it);						\
200 		/* termination and bounds check */						\
201 		(___t && ((i) = *___t, (i) >= (start) && (i) < (end)));				\
202 	});											\
203 )
204 #endif /* bpf_for */
205 
206 #ifndef bpf_repeat
207 /* bpf_repeat(N) performs N iterations without exposing iteration number
208  *
209  * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
210  * loop bound variables and cleanup attribute, supported by GCC and Clang.
211  */
212 #define bpf_repeat(N) for (									\
213 	/* initialize and define destructor */							\
214 	struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */	\
215 						 cleanup(bpf_iter_num_destroy))),		\
216 	/* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */		\
217 			    *___p __attribute__((unused)) = (					\
218 				bpf_iter_num_new(&___it, 0, (N)),				\
219 	/* this is a workaround for Clang bug: it currently doesn't emit BTF */			\
220 	/* for bpf_iter_num_destroy() when used from cleanup() attribute */			\
221 				(void)bpf_iter_num_destroy, (void *)0);				\
222 	bpf_iter_num_next(&___it);								\
223 	/* nothing here  */									\
224 )
225 #endif /* bpf_repeat */
226 
227 #endif
228