1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __BPF_MISC_H__ 3 #define __BPF_MISC_H__ 4 5 #define XSTR(s) STR(s) 6 #define STR(s) #s 7 8 /* This set of attributes controls behavior of the 9 * test_loader.c:test_loader__run_subtests(). 10 * 11 * The test_loader sequentially loads each program in a skeleton. 12 * Programs could be loaded in privileged and unprivileged modes. 13 * - __success, __failure, __msg, __regex imply privileged mode; 14 * - __success_unpriv, __failure_unpriv, __msg_unpriv, __regex_unpriv 15 * imply unprivileged mode. 16 * If combination of privileged and unprivileged attributes is present 17 * both modes are used. If none are present privileged mode is implied. 18 * 19 * See test_loader.c:drop_capabilities() for exact set of capabilities 20 * that differ between privileged and unprivileged modes. 21 * 22 * For test filtering purposes the name of the program loaded in 23 * unprivileged mode is derived from the usual program name by adding 24 * `@unpriv' suffix. 25 * 26 * __msg Message expected to be found in the verifier log. 27 * Multiple __msg attributes could be specified. 28 * To match a regular expression use "{{" "}}" brackets, 29 * e.g. "foo{{[0-9]+}}" matches strings like "foo007". 30 * Extended POSIX regular expression syntax is allowed 31 * inside the brackets. 32 * __msg_unpriv Same as __msg but for unprivileged mode. 33 * 34 * __xlated Expect a line in a disassembly log after verifier applies rewrites. 35 * Multiple __xlated attributes could be specified. 36 * Regular expressions could be specified same way as in __msg. 37 * __xlated_unpriv Same as __xlated but for unprivileged mode. 38 * 39 * __jited Match a line in a disassembly of the jited BPF program. 40 * Has to be used after __arch_* macro. 41 * For example: 42 * 43 * __arch_x86_64 44 * __jited(" endbr64") 45 * __jited(" nopl (%rax,%rax)") 46 * __jited(" xorq %rax, %rax") 47 * ... 48 * __naked void some_test(void) 49 * { 50 * asm volatile (... ::: __clobber_all); 51 * } 52 * 53 * Regular expressions could be included in patterns same way 54 * as in __msg. 55 * 56 * By default assume that each pattern has to be matched on the 57 * next consecutive line of disassembly, e.g.: 58 * 59 * __jited(" endbr64") # matched on line N 60 * __jited(" nopl (%rax,%rax)") # matched on line N+1 61 * 62 * If match occurs on a wrong line an error is reported. 63 * To override this behaviour use literal "...", e.g.: 64 * 65 * __jited(" endbr64") # matched on line N 66 * __jited("...") # not matched 67 * __jited(" nopl (%rax,%rax)") # matched on any line >= N 68 * 69 * __jited_unpriv Same as __jited but for unprivileged mode. 70 * 71 * 72 * __success Expect program load success in privileged mode. 73 * __success_unpriv Expect program load success in unprivileged mode. 74 * 75 * __failure Expect program load failure in privileged mode. 76 * __failure_unpriv Expect program load failure in unprivileged mode. 77 * 78 * __retval Execute the program using BPF_PROG_TEST_RUN command, 79 * expect return value to match passed parameter: 80 * - a decimal number 81 * - a hexadecimal number, when starts from 0x 82 * - literal INT_MIN 83 * - literal POINTER_VALUE (see definition below) 84 * - literal TEST_DATA_LEN (see definition below) 85 * __retval_unpriv Same, but load program in unprivileged mode. 86 * 87 * __description Text to be used instead of a program name for display 88 * and filtering purposes. 89 * 90 * __log_level Log level to use for the program, numeric value expected. 91 * 92 * __flag Adds one flag use for the program, the following values are valid: 93 * - BPF_F_STRICT_ALIGNMENT; 94 * - BPF_F_TEST_RND_HI32; 95 * - BPF_F_TEST_STATE_FREQ; 96 * - BPF_F_SLEEPABLE; 97 * - BPF_F_XDP_HAS_FRAGS; 98 * - A numeric value. 99 * Multiple __flag attributes could be specified, the final flags 100 * value is derived by applying binary "or" to all specified values. 101 * 102 * __auxiliary Annotated program is not a separate test, but used as auxiliary 103 * for some other test cases and should always be loaded. 104 * __auxiliary_unpriv Same, but load program in unprivileged mode. 105 * 106 * __arch_* Specify on which architecture the test case should be tested. 107 * Several __arch_* annotations could be specified at once. 108 * When test case is not run on current arch it is marked as skipped. 109 */ 110 #define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg))) 111 #define __xlated(msg) __attribute__((btf_decl_tag("comment:test_expect_xlated=" XSTR(__COUNTER__) "=" msg))) 112 #define __jited(msg) __attribute__((btf_decl_tag("comment:test_jited=" XSTR(__COUNTER__) "=" msg))) 113 #define __failure __attribute__((btf_decl_tag("comment:test_expect_failure"))) 114 #define __success __attribute__((btf_decl_tag("comment:test_expect_success"))) 115 #define __description(desc) __attribute__((btf_decl_tag("comment:test_description=" desc))) 116 #define __msg_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" XSTR(__COUNTER__) "=" msg))) 117 #define __xlated_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_xlated_unpriv=" XSTR(__COUNTER__) "=" msg))) 118 #define __jited_unpriv(msg) __attribute__((btf_decl_tag("comment:test_jited=" XSTR(__COUNTER__) "=" msg))) 119 #define __failure_unpriv __attribute__((btf_decl_tag("comment:test_expect_failure_unpriv"))) 120 #define __success_unpriv __attribute__((btf_decl_tag("comment:test_expect_success_unpriv"))) 121 #define __log_level(lvl) __attribute__((btf_decl_tag("comment:test_log_level="#lvl))) 122 #define __flag(flag) __attribute__((btf_decl_tag("comment:test_prog_flags="#flag))) 123 #define __retval(val) __attribute__((btf_decl_tag("comment:test_retval="#val))) 124 #define __retval_unpriv(val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val))) 125 #define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary"))) 126 #define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv"))) 127 #define __btf_path(path) __attribute__((btf_decl_tag("comment:test_btf_path=" path))) 128 #define __arch(arch) __attribute__((btf_decl_tag("comment:test_arch=" arch))) 129 #define __arch_x86_64 __arch("X86_64") 130 #define __arch_arm64 __arch("ARM64") 131 #define __arch_riscv64 __arch("RISCV64") 132 133 /* Convenience macro for use with 'asm volatile' blocks */ 134 #define __naked __attribute__((naked)) 135 #define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory" 136 #define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory" 137 #define __imm(name) [name]"i"(name) 138 #define __imm_const(name, expr) [name]"i"(expr) 139 #define __imm_addr(name) [name]"i"(&name) 140 #define __imm_ptr(name) [name]"r"(&name) 141 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr)) 142 143 /* Magic constants used with __retval() */ 144 #define POINTER_VALUE 0xcafe4all 145 #define TEST_DATA_LEN 64 146 147 #ifndef __used 148 #define __used __attribute__((used)) 149 #endif 150 151 #if defined(__TARGET_ARCH_x86) 152 #define SYSCALL_WRAPPER 1 153 #define SYS_PREFIX "__x64_" 154 #elif defined(__TARGET_ARCH_s390) 155 #define SYSCALL_WRAPPER 1 156 #define SYS_PREFIX "__s390x_" 157 #elif defined(__TARGET_ARCH_arm64) 158 #define SYSCALL_WRAPPER 1 159 #define SYS_PREFIX "__arm64_" 160 #elif defined(__TARGET_ARCH_riscv) 161 #define SYSCALL_WRAPPER 1 162 #define SYS_PREFIX "__riscv_" 163 #else 164 #define SYSCALL_WRAPPER 0 165 #define SYS_PREFIX "__se_" 166 #endif 167 168 /* How many arguments are passed to function in register */ 169 #if defined(__TARGET_ARCH_x86) || defined(__x86_64__) 170 #define FUNC_REG_ARG_CNT 6 171 #elif defined(__i386__) 172 #define FUNC_REG_ARG_CNT 3 173 #elif defined(__TARGET_ARCH_s390) || defined(__s390x__) 174 #define FUNC_REG_ARG_CNT 5 175 #elif defined(__TARGET_ARCH_arm) || defined(__arm__) 176 #define FUNC_REG_ARG_CNT 4 177 #elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__) 178 #define FUNC_REG_ARG_CNT 8 179 #elif defined(__TARGET_ARCH_mips) || defined(__mips__) 180 #define FUNC_REG_ARG_CNT 8 181 #elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__) 182 #define FUNC_REG_ARG_CNT 8 183 #elif defined(__TARGET_ARCH_sparc) || defined(__sparc__) 184 #define FUNC_REG_ARG_CNT 6 185 #elif defined(__TARGET_ARCH_riscv) || defined(__riscv__) 186 #define FUNC_REG_ARG_CNT 8 187 #else 188 /* default to 5 for others */ 189 #define FUNC_REG_ARG_CNT 5 190 #endif 191 192 /* make it look to compiler like value is read and written */ 193 #define __sink(expr) asm volatile("" : "+g"(expr)) 194 195 #ifndef ARRAY_SIZE 196 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 197 #endif 198 199 #endif 200