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