xref: /linux/tools/testing/selftests/bpf/progs/bpf_misc.h (revision daa2be74b1b2302004945b2a5e32424e177cc7da)
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, __regex imply privileged mode;
11  * - __success_unpriv, __failure_unpriv, __msg_unpriv, __regex_unpriv
12  *   imply 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  * __regex           Same as __msg, but using a regular expression.
28  * __regex_unpriv    Same as __msg_unpriv but using a regular expression.
29  *
30  * __success         Expect program load success in privileged mode.
31  * __success_unpriv  Expect program load success in unprivileged mode.
32  *
33  * __failure         Expect program load failure in privileged mode.
34  * __failure_unpriv  Expect program load failure in unprivileged mode.
35  *
36  * __retval          Execute the program using BPF_PROG_TEST_RUN command,
37  *                   expect return value to match passed parameter:
38  *                   - a decimal number
39  *                   - a hexadecimal number, when starts from 0x
40  *                   - literal INT_MIN
41  *                   - literal POINTER_VALUE (see definition below)
42  *                   - literal TEST_DATA_LEN (see definition below)
43  * __retval_unpriv   Same, but load program in unprivileged mode.
44  *
45  * __description     Text to be used instead of a program name for display
46  *                   and filtering purposes.
47  *
48  * __log_level       Log level to use for the program, numeric value expected.
49  *
50  * __flag            Adds one flag use for the program, the following values are valid:
51  *                   - BPF_F_STRICT_ALIGNMENT;
52  *                   - BPF_F_TEST_RND_HI32;
53  *                   - BPF_F_TEST_STATE_FREQ;
54  *                   - BPF_F_SLEEPABLE;
55  *                   - BPF_F_XDP_HAS_FRAGS;
56  *                   - A numeric value.
57  *                   Multiple __flag attributes could be specified, the final flags
58  *                   value is derived by applying binary "or" to all specified values.
59  *
60  * __auxiliary         Annotated program is not a separate test, but used as auxiliary
61  *                     for some other test cases and should always be loaded.
62  * __auxiliary_unpriv  Same, but load program in unprivileged mode.
63  */
64 #define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
65 #define __regex(regex)		__attribute__((btf_decl_tag("comment:test_expect_regex=" regex)))
66 #define __failure		__attribute__((btf_decl_tag("comment:test_expect_failure")))
67 #define __success		__attribute__((btf_decl_tag("comment:test_expect_success")))
68 #define __description(desc)	__attribute__((btf_decl_tag("comment:test_description=" desc)))
69 #define __msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" msg)))
70 #define __regex_unpriv(regex)	__attribute__((btf_decl_tag("comment:test_expect_regex_unpriv=" regex)))
71 #define __failure_unpriv	__attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
72 #define __success_unpriv	__attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
73 #define __log_level(lvl)	__attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
74 #define __flag(flag)		__attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
75 #define __retval(val)		__attribute__((btf_decl_tag("comment:test_retval="#val)))
76 #define __retval_unpriv(val)	__attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
77 #define __auxiliary		__attribute__((btf_decl_tag("comment:test_auxiliary")))
78 #define __auxiliary_unpriv	__attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
79 #define __btf_path(path)	__attribute__((btf_decl_tag("comment:test_btf_path=" path)))
80 
81 /* Convenience macro for use with 'asm volatile' blocks */
82 #define __naked __attribute__((naked))
83 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
84 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
85 #define __imm(name) [name]"i"(name)
86 #define __imm_const(name, expr) [name]"i"(expr)
87 #define __imm_addr(name) [name]"i"(&name)
88 #define __imm_ptr(name) [name]"r"(&name)
89 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
90 
91 /* Magic constants used with __retval() */
92 #define POINTER_VALUE	0xcafe4all
93 #define TEST_DATA_LEN	64
94 
95 #ifndef __used
96 #define __used __attribute__((used))
97 #endif
98 
99 #if defined(__TARGET_ARCH_x86)
100 #define SYSCALL_WRAPPER 1
101 #define SYS_PREFIX "__x64_"
102 #elif defined(__TARGET_ARCH_s390)
103 #define SYSCALL_WRAPPER 1
104 #define SYS_PREFIX "__s390x_"
105 #elif defined(__TARGET_ARCH_arm64)
106 #define SYSCALL_WRAPPER 1
107 #define SYS_PREFIX "__arm64_"
108 #elif defined(__TARGET_ARCH_riscv)
109 #define SYSCALL_WRAPPER 1
110 #define SYS_PREFIX "__riscv_"
111 #else
112 #define SYSCALL_WRAPPER 0
113 #define SYS_PREFIX "__se_"
114 #endif
115 
116 /* How many arguments are passed to function in register */
117 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
118 #define FUNC_REG_ARG_CNT 6
119 #elif defined(__i386__)
120 #define FUNC_REG_ARG_CNT 3
121 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
122 #define FUNC_REG_ARG_CNT 5
123 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
124 #define FUNC_REG_ARG_CNT 4
125 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
126 #define FUNC_REG_ARG_CNT 8
127 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
128 #define FUNC_REG_ARG_CNT 8
129 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
130 #define FUNC_REG_ARG_CNT 8
131 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
132 #define FUNC_REG_ARG_CNT 6
133 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
134 #define FUNC_REG_ARG_CNT 8
135 #else
136 /* default to 5 for others */
137 #define FUNC_REG_ARG_CNT 5
138 #endif
139 
140 /* make it look to compiler like value is read and written */
141 #define __sink(expr) asm volatile("" : "+g"(expr))
142 
143 #ifndef ARRAY_SIZE
144 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
145 #endif
146 
147 #endif
148