162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __BPF_MISC_H__
362306a36Sopenharmony_ci#define __BPF_MISC_H__
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/* This set of attributes controls behavior of the
662306a36Sopenharmony_ci * test_loader.c:test_loader__run_subtests().
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * The test_loader sequentially loads each program in a skeleton.
962306a36Sopenharmony_ci * Programs could be loaded in privileged and unprivileged modes.
1062306a36Sopenharmony_ci * - __success, __failure, __msg imply privileged mode;
1162306a36Sopenharmony_ci * - __success_unpriv, __failure_unpriv, __msg_unpriv imply
1262306a36Sopenharmony_ci *   unprivileged mode.
1362306a36Sopenharmony_ci * If combination of privileged and unprivileged attributes is present
1462306a36Sopenharmony_ci * both modes are used. If none are present privileged mode is implied.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * See test_loader.c:drop_capabilities() for exact set of capabilities
1762306a36Sopenharmony_ci * that differ between privileged and unprivileged modes.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * For test filtering purposes the name of the program loaded in
2062306a36Sopenharmony_ci * unprivileged mode is derived from the usual program name by adding
2162306a36Sopenharmony_ci * `@unpriv' suffix.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * __msg             Message expected to be found in the verifier log.
2462306a36Sopenharmony_ci *                   Multiple __msg attributes could be specified.
2562306a36Sopenharmony_ci * __msg_unpriv      Same as __msg but for unprivileged mode.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * __success         Expect program load success in privileged mode.
2862306a36Sopenharmony_ci * __success_unpriv  Expect program load success in unprivileged mode.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * __failure         Expect program load failure in privileged mode.
3162306a36Sopenharmony_ci * __failure_unpriv  Expect program load failure in unprivileged mode.
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * __retval          Execute the program using BPF_PROG_TEST_RUN command,
3462306a36Sopenharmony_ci *                   expect return value to match passed parameter:
3562306a36Sopenharmony_ci *                   - a decimal number
3662306a36Sopenharmony_ci *                   - a hexadecimal number, when starts from 0x
3762306a36Sopenharmony_ci *                   - literal INT_MIN
3862306a36Sopenharmony_ci *                   - literal POINTER_VALUE (see definition below)
3962306a36Sopenharmony_ci *                   - literal TEST_DATA_LEN (see definition below)
4062306a36Sopenharmony_ci * __retval_unpriv   Same, but load program in unprivileged mode.
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * __description     Text to be used instead of a program name for display
4362306a36Sopenharmony_ci *                   and filtering purposes.
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * __log_level       Log level to use for the program, numeric value expected.
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * __flag            Adds one flag use for the program, the following values are valid:
4862306a36Sopenharmony_ci *                   - BPF_F_STRICT_ALIGNMENT;
4962306a36Sopenharmony_ci *                   - BPF_F_TEST_RND_HI32;
5062306a36Sopenharmony_ci *                   - BPF_F_TEST_STATE_FREQ;
5162306a36Sopenharmony_ci *                   - BPF_F_SLEEPABLE;
5262306a36Sopenharmony_ci *                   - BPF_F_XDP_HAS_FRAGS;
5362306a36Sopenharmony_ci *                   - A numeric value.
5462306a36Sopenharmony_ci *                   Multiple __flag attributes could be specified, the final flags
5562306a36Sopenharmony_ci *                   value is derived by applying binary "or" to all specified values.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * __auxiliary         Annotated program is not a separate test, but used as auxiliary
5862306a36Sopenharmony_ci *                     for some other test cases and should always be loaded.
5962306a36Sopenharmony_ci * __auxiliary_unpriv  Same, but load program in unprivileged mode.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci#define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
6262306a36Sopenharmony_ci#define __failure		__attribute__((btf_decl_tag("comment:test_expect_failure")))
6362306a36Sopenharmony_ci#define __success		__attribute__((btf_decl_tag("comment:test_expect_success")))
6462306a36Sopenharmony_ci#define __description(desc)	__attribute__((btf_decl_tag("comment:test_description=" desc)))
6562306a36Sopenharmony_ci#define __msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" msg)))
6662306a36Sopenharmony_ci#define __failure_unpriv	__attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
6762306a36Sopenharmony_ci#define __success_unpriv	__attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
6862306a36Sopenharmony_ci#define __log_level(lvl)	__attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
6962306a36Sopenharmony_ci#define __flag(flag)		__attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
7062306a36Sopenharmony_ci#define __retval(val)		__attribute__((btf_decl_tag("comment:test_retval="#val)))
7162306a36Sopenharmony_ci#define __retval_unpriv(val)	__attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
7262306a36Sopenharmony_ci#define __auxiliary		__attribute__((btf_decl_tag("comment:test_auxiliary")))
7362306a36Sopenharmony_ci#define __auxiliary_unpriv	__attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/* Convenience macro for use with 'asm volatile' blocks */
7662306a36Sopenharmony_ci#define __naked __attribute__((naked))
7762306a36Sopenharmony_ci#define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
7862306a36Sopenharmony_ci#define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
7962306a36Sopenharmony_ci#define __imm(name) [name]"i"(name)
8062306a36Sopenharmony_ci#define __imm_const(name, expr) [name]"i"(expr)
8162306a36Sopenharmony_ci#define __imm_addr(name) [name]"i"(&name)
8262306a36Sopenharmony_ci#define __imm_ptr(name) [name]"p"(&name)
8362306a36Sopenharmony_ci#define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* Magic constants used with __retval() */
8662306a36Sopenharmony_ci#define POINTER_VALUE	0xcafe4all
8762306a36Sopenharmony_ci#define TEST_DATA_LEN	64
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#ifndef __used
9062306a36Sopenharmony_ci#define __used __attribute__((used))
9162306a36Sopenharmony_ci#endif
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#if defined(__TARGET_ARCH_x86)
9462306a36Sopenharmony_ci#define SYSCALL_WRAPPER 1
9562306a36Sopenharmony_ci#define SYS_PREFIX "__x64_"
9662306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_s390)
9762306a36Sopenharmony_ci#define SYSCALL_WRAPPER 1
9862306a36Sopenharmony_ci#define SYS_PREFIX "__s390x_"
9962306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_arm64)
10062306a36Sopenharmony_ci#define SYSCALL_WRAPPER 1
10162306a36Sopenharmony_ci#define SYS_PREFIX "__arm64_"
10262306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_riscv)
10362306a36Sopenharmony_ci#define SYSCALL_WRAPPER 1
10462306a36Sopenharmony_ci#define SYS_PREFIX "__riscv_"
10562306a36Sopenharmony_ci#else
10662306a36Sopenharmony_ci#define SYSCALL_WRAPPER 0
10762306a36Sopenharmony_ci#define SYS_PREFIX "__se_"
10862306a36Sopenharmony_ci#endif
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/* How many arguments are passed to function in register */
11162306a36Sopenharmony_ci#if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
11262306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 6
11362306a36Sopenharmony_ci#elif defined(__i386__)
11462306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 3
11562306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
11662306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 5
11762306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_arm) || defined(__arm__)
11862306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 4
11962306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
12062306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 8
12162306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_mips) || defined(__mips__)
12262306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 8
12362306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
12462306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 8
12562306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
12662306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 6
12762306a36Sopenharmony_ci#elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
12862306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 8
12962306a36Sopenharmony_ci#else
13062306a36Sopenharmony_ci/* default to 5 for others */
13162306a36Sopenharmony_ci#define FUNC_REG_ARG_CNT 5
13262306a36Sopenharmony_ci#endif
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/* make it look to compiler like value is read and written */
13562306a36Sopenharmony_ci#define __sink(expr) asm volatile("" : "+g"(expr))
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#endif
138