xref: /linux/tools/testing/selftests/bpf/progs/bpf_misc.h (revision e7d759f31ca295d589f7420719c311870bb3166f)
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  * __auxiliary         Annotated program is not a separate test, but used as auxiliary
58  *                     for some other test cases and should always be loaded.
59  * __auxiliary_unpriv  Same, but load program in unprivileged mode.
60  */
61 #define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
62 #define __failure		__attribute__((btf_decl_tag("comment:test_expect_failure")))
63 #define __success		__attribute__((btf_decl_tag("comment:test_expect_success")))
64 #define __description(desc)	__attribute__((btf_decl_tag("comment:test_description=" desc)))
65 #define __msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" msg)))
66 #define __failure_unpriv	__attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
67 #define __success_unpriv	__attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
68 #define __log_level(lvl)	__attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
69 #define __flag(flag)		__attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
70 #define __retval(val)		__attribute__((btf_decl_tag("comment:test_retval="#val)))
71 #define __retval_unpriv(val)	__attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
72 #define __auxiliary		__attribute__((btf_decl_tag("comment:test_auxiliary")))
73 #define __auxiliary_unpriv	__attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
74 #define __btf_path(path)	__attribute__((btf_decl_tag("comment:test_btf_path=" path)))
75 
76 /* Convenience macro for use with 'asm volatile' blocks */
77 #define __naked __attribute__((naked))
78 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
79 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
80 #define __imm(name) [name]"i"(name)
81 #define __imm_const(name, expr) [name]"i"(expr)
82 #define __imm_addr(name) [name]"i"(&name)
83 #define __imm_ptr(name) [name]"p"(&name)
84 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
85 
86 /* Magic constants used with __retval() */
87 #define POINTER_VALUE	0xcafe4all
88 #define TEST_DATA_LEN	64
89 
90 #ifndef __used
91 #define __used __attribute__((used))
92 #endif
93 
94 #if defined(__TARGET_ARCH_x86)
95 #define SYSCALL_WRAPPER 1
96 #define SYS_PREFIX "__x64_"
97 #elif defined(__TARGET_ARCH_s390)
98 #define SYSCALL_WRAPPER 1
99 #define SYS_PREFIX "__s390x_"
100 #elif defined(__TARGET_ARCH_arm64)
101 #define SYSCALL_WRAPPER 1
102 #define SYS_PREFIX "__arm64_"
103 #elif defined(__TARGET_ARCH_riscv)
104 #define SYSCALL_WRAPPER 1
105 #define SYS_PREFIX "__riscv_"
106 #else
107 #define SYSCALL_WRAPPER 0
108 #define SYS_PREFIX "__se_"
109 #endif
110 
111 /* How many arguments are passed to function in register */
112 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
113 #define FUNC_REG_ARG_CNT 6
114 #elif defined(__i386__)
115 #define FUNC_REG_ARG_CNT 3
116 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
117 #define FUNC_REG_ARG_CNT 5
118 #elif defined(__TARGET_ARCH_arm) || defined(__arm__)
119 #define FUNC_REG_ARG_CNT 4
120 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
121 #define FUNC_REG_ARG_CNT 8
122 #elif defined(__TARGET_ARCH_mips) || defined(__mips__)
123 #define FUNC_REG_ARG_CNT 8
124 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
125 #define FUNC_REG_ARG_CNT 8
126 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
127 #define FUNC_REG_ARG_CNT 6
128 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
129 #define FUNC_REG_ARG_CNT 8
130 #else
131 /* default to 5 for others */
132 #define FUNC_REG_ARG_CNT 5
133 #endif
134 
135 /* make it look to compiler like value is read and written */
136 #define __sink(expr) asm volatile("" : "+g"(expr))
137 
138 #endif
139