Skip to content

Commit

Permalink
selftests: riscv: Support xtheadvector in vector tests
Browse files Browse the repository at this point in the history
Extend existing vector tests to be compatible with the xtheadvector
instruction set.

Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
  • Loading branch information
charlie-rivos authored and Björn Töpel committed Apr 12, 2024
1 parent cc10190 commit a0b99b0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 49 deletions.
23 changes: 16 additions & 7 deletions tools/testing/selftests/riscv/vector/v_exec_initval_nolibc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ int main(int argc, char **argv)
unsigned long vl;
int first = 1;

asm volatile (
".option push\n\t"
".option arch, +v\n\t"
"vsetvli %[vl], x0, e8, m1, ta, ma\n\t"
".option pop\n\t"
: [vl] "=r" (vl)
);
if (argc > 2 && strcmp(argv[2], "x"))
asm volatile (
// 0 | zimm[10:0] | rs1 | 1 1 1 | rd |1010111| vsetvli
// vsetvli t4, x0, e8, m1, d1
".insn 0b00000000000000000111111011010111\n\t"
"mv %[vl], t4\n\t"
: [vl] "=r" (vl) : : "t4"
);
else
asm volatile (
".option push\n\t"
".option arch, +v\n\t"
"vsetvli %[vl], x0, e8, m1, ta, ma\n\t"
".option pop\n\t"
: [vl] "=r" (vl)
);

#define CHECK_VECTOR_REGISTER(register) ({ \
for (int i = 0; i < vl; i++) { \
Expand Down
16 changes: 13 additions & 3 deletions tools/testing/selftests/riscv/vector/v_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
#include <unistd.h>
#include <sys/wait.h>

int is_xtheadvector_supported(void)
{
struct riscv_hwprobe pair;

pair.key = RISCV_HWPROBE_KEY_VENDOR_EXT_0;
riscv_hwprobe(&pair, 1, 0, NULL, 0);
return pair.value & RISCV_HWPROBE_VENDOR_EXT_XTHEADVECTOR;
}

int is_vector_supported(void)
{
struct riscv_hwprobe pair;
Expand All @@ -15,9 +24,9 @@ int is_vector_supported(void)
return pair.value & RISCV_HWPROBE_IMA_V;
}

int launch_test(char *next_program, int test_inherit)
int launch_test(char *next_program, int test_inherit, int xtheadvector)
{
char *exec_argv[3], *exec_envp[1];
char *exec_argv[4], *exec_envp[1];
int rc, pid, status;

pid = fork();
Expand All @@ -29,7 +38,8 @@ int launch_test(char *next_program, int test_inherit)
if (!pid) {
exec_argv[0] = next_program;
exec_argv[1] = test_inherit != 0 ? "x" : NULL;
exec_argv[2] = NULL;
exec_argv[2] = xtheadvector != 0 ? "x" : NULL;
exec_argv[3] = NULL;
exec_envp[0] = NULL;
/* launch the program again to check inherit */
rc = execve(next_program, exec_argv, exec_envp);
Expand Down
4 changes: 3 additions & 1 deletion tools/testing/selftests/riscv/vector/v_helpers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-only */

int is_xtheadvector_supported(void);

int is_vector_supported(void);

int launch_test(char *next_program, int test_inherit);
int launch_test(char *next_program, int test_inherit, int xtheadvector);
12 changes: 9 additions & 3 deletions tools/testing/selftests/riscv/vector/v_initval.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

TEST(v_initval)
{
if (!is_vector_supported())
SKIP(return, "Vector not supported");
int xtheadvector = 0;

ASSERT_EQ(0, launch_test(NEXT_PROGRAM, 0));
if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}

ASSERT_EQ(0, launch_test(NEXT_PROGRAM, 0, xtheadvector));
}

TEST_HARNESS_MAIN
20 changes: 13 additions & 7 deletions tools/testing/selftests/riscv/vector/vstate_exec_nolibc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

int main(int argc, char **argv)
{
int rc, pid, status, test_inherit = 0;
int rc, pid, status, test_inherit = 0, xtheadvector = 0;
long ctrl, ctrl_c;
char *exec_argv[2], *exec_envp[2];

if (argc > 1)
if (argc > 1 && strcmp(argv[1], "x"))
test_inherit = 1;

if (argc > 2 && strcmp(argv[2], "x"))
xtheadvector = 1;

ctrl = my_syscall1(__NR_prctl, PR_RISCV_V_GET_CONTROL);
if (ctrl < 0) {
puts("PR_RISCV_V_GET_CONTROL is not supported\n");
Expand Down Expand Up @@ -53,11 +56,14 @@ int main(int argc, char **argv)
puts("child's vstate_ctrl not equal to parent's\n");
exit(-1);
}
asm volatile (".option push\n\t"
".option arch, +v\n\t"
"vsetvli x0, x0, e32, m8, ta, ma\n\t"
".option pop\n\t"
);
if (xtheadvector)
asm volatile (".insn 0x00007ed7");
else
asm volatile (".option push\n\t"
".option arch, +v\n\t"
"vsetvli x0, x0, e32, m8, ta, ma\n\t"
".option pop\n\t"
);
exit(ctrl);
}
}
Expand Down
85 changes: 57 additions & 28 deletions tools/testing/selftests/riscv/vector/vstate_prctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define NEXT_PROGRAM "./vstate_exec_nolibc"

int test_and_compare_child(long provided, long expected, int inherit)
int test_and_compare_child(long provided, long expected, int inherit, int xtheadvector)
{
int rc;

Expand All @@ -21,7 +21,7 @@ int test_and_compare_child(long provided, long expected, int inherit)
provided, rc);
return -1;
}
rc = launch_test(NEXT_PROGRAM, inherit);
rc = launch_test(NEXT_PROGRAM, inherit, xtheadvector);
if (rc != expected) {
printf("Test failed, check %d != %ld\n", rc, expected);
return -2;
Expand All @@ -36,7 +36,7 @@ TEST(get_control_no_v)
{
long rc;

if (is_vector_supported())
if (is_vector_supported() || is_xtheadvector_supported())
SKIP(return, "Test expects vector to be not supported");

rc = prctl(PR_RISCV_V_GET_CONTROL);
Expand All @@ -48,7 +48,7 @@ TEST(set_control_no_v)
{
long rc;

if (is_vector_supported())
if (is_vector_supported() || is_xtheadvector_supported())
SKIP(return, "Test expects vector to be not supported");

rc = prctl(PR_RISCV_V_SET_CONTROL, PR_RISCV_V_VSTATE_CTRL_ON);
Expand All @@ -61,7 +61,7 @@ TEST(vstate_on_current)
long flag;
long rc;

if (!is_vector_supported())
if (!is_vector_supported() && !is_xtheadvector_supported())
SKIP(return, "Vector not supported");

flag = PR_RISCV_V_VSTATE_CTRL_ON;
Expand All @@ -74,7 +74,7 @@ TEST(vstate_off_eperm)
long flag;
long rc;

if (!is_vector_supported())
if (!is_vector_supported() && !is_xtheadvector_supported())
SKIP(return, "Vector not supported");

flag = PR_RISCV_V_VSTATE_CTRL_OFF;
Expand All @@ -86,95 +86,124 @@ TEST(vstate_off_eperm)
TEST(vstate_on_no_nesting)
{
long flag;
int xtheadvector = 0;

if (!is_vector_supported())
SKIP(return, "Vector not supported");
if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}

/* Turn on next's vector explicitly and test */
flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;

EXPECT_EQ(0, test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_ON, 0));
EXPECT_EQ(0, test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_ON, 0, xtheadvector));
}

TEST(vstate_off_nesting)
{
long flag;
int xtheadvector = 0;

if (!is_vector_supported())
SKIP(return, "Vector not supported");
if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}

/* Turn off next's vector explicitly and test */
flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;

EXPECT_EQ(0, test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_OFF, 1));
EXPECT_EQ(0, test_and_compare_child(flag, PR_RISCV_V_VSTATE_CTRL_OFF, 1, xtheadvector));
}

TEST(vstate_on_inherit_no_nesting)
{
long flag, expected;
int xtheadvector = 0;

if (!is_vector_supported())
SKIP(return, "Vector not supported");
if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}

/* Turn on next's vector explicitly and test no inherit */
flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
expected = flag | PR_RISCV_V_VSTATE_CTRL_ON;

EXPECT_EQ(0, test_and_compare_child(flag, expected, 0));
EXPECT_EQ(0, test_and_compare_child(flag, expected, 0, xtheadvector));
}

TEST(vstate_on_inherit)
{
long flag, expected;
int xtheadvector = 0;

if (!is_vector_supported())
SKIP(return, "Vector not supported");
if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}

/* Turn on next's vector explicitly and test inherit */
flag = PR_RISCV_V_VSTATE_CTRL_ON << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
expected = flag | PR_RISCV_V_VSTATE_CTRL_ON;

EXPECT_EQ(0, test_and_compare_child(flag, expected, 1));
EXPECT_EQ(0, test_and_compare_child(flag, expected, 1, xtheadvector));
}

TEST(vstate_off_inherit_no_nesting)
{
long flag, expected;
int xtheadvector = 0;

if (!is_vector_supported())
SKIP(return, "Vector not supported");

if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}
/* Turn off next's vector explicitly and test no inherit */
flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
expected = flag | PR_RISCV_V_VSTATE_CTRL_OFF;

EXPECT_EQ(0, test_and_compare_child(flag, expected, 0));
EXPECT_EQ(0, test_and_compare_child(flag, expected, 0, xtheadvector));
}

TEST(vstate_off_inherit)
{
long flag, expected;
int xtheadvector = 0;

if (!is_vector_supported())
SKIP(return, "Vector not supported");
if (!is_vector_supported()) {
if (is_xtheadvector_supported())
xtheadvector = 1;
else
SKIP(return, "Vector not supported");
}

/* Turn off next's vector explicitly and test inherit */
flag = PR_RISCV_V_VSTATE_CTRL_OFF << PR_RISCV_V_VSTATE_CTRL_NEXT_SHIFT;
flag |= PR_RISCV_V_VSTATE_CTRL_INHERIT;
expected = flag | PR_RISCV_V_VSTATE_CTRL_OFF;

EXPECT_EQ(0, test_and_compare_child(flag, expected, 1));
EXPECT_EQ(0, test_and_compare_child(flag, expected, 1, xtheadvector));
}

/* arguments should fail with EINVAL */
TEST(inval_set_control_1)
{
int rc;

if (!is_vector_supported())
if (!is_vector_supported() && !is_xtheadvector_supported())
SKIP(return, "Vector not supported");

rc = prctl(PR_RISCV_V_SET_CONTROL, 0xff0);
Expand All @@ -187,7 +216,7 @@ TEST(inval_set_control_2)
{
int rc;

if (!is_vector_supported())
if (!is_vector_supported() && !is_xtheadvector_supported())
SKIP(return, "Vector not supported");

rc = prctl(PR_RISCV_V_SET_CONTROL, 0x3);
Expand All @@ -200,7 +229,7 @@ TEST(inval_set_control_3)
{
int rc;

if (!is_vector_supported())
if (!is_vector_supported() && !is_xtheadvector_supported())
SKIP(return, "Vector not supported");

rc = prctl(PR_RISCV_V_SET_CONTROL, 0xc);
Expand Down

0 comments on commit a0b99b0

Please sign in to comment.