Skip to content

Commit 5c93c4c

Browse files
AndybnACTpalmer-dabbelt
authored andcommitted
selftests: Test RISC-V Vector's first-use handler
This add a test to check if the kernel zero-initializes all V registers after the first-use trap handler returns. If V registers are not zero-initialized, then the test should fail one out of several runs: ``` root@sifive-fpga:~# ./v_initval_nolibc # vl = 256 not ok 1 detect stale values on v-regesters 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4c 41 4e 47 3d 43 0 50 41 54 48 3d 2f 75 73 72 2f 6c 6f 63 61 6c 2f 73 62 69 6e 3a 2f 75 73 72 2f 6c 6f 63 61 6c 2f 62 69 6e 3a 2f 75 73 72 ff ff 81 0 0 0 0 0 0 0 0 0 0 0 0 0 ``` Otherwise, the test passes without errors each run. Signed-off-by: Andy Chiu <[email protected]> Reviewed-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 75b59f2 commit 5c93c4c

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vstate_exec_nolibc
22
vstate_prctl
3+
v_initval_nolibc

tools/testing/selftests/riscv/vector/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (C) 2021 ARM Limited
33
# Originally tools/testing/arm64/abi/Makefile
44

5-
TEST_GEN_PROGS := vstate_prctl
5+
TEST_GEN_PROGS := vstate_prctl v_initval_nolibc
66
TEST_GEN_PROGS_EXTENDED := vstate_exec_nolibc
77

88
include ../../lib.mk
@@ -13,3 +13,7 @@ $(OUTPUT)/vstate_prctl: vstate_prctl.c ../hwprobe/sys_hwprobe.S
1313
$(OUTPUT)/vstate_exec_nolibc: vstate_exec_nolibc.c
1414
$(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
1515
-Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc
16+
17+
$(OUTPUT)/v_initval_nolibc: v_initval_nolibc.c
18+
$(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
19+
-Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include "../../kselftest.h"
4+
#define MAX_VSIZE (8192 * 32)
5+
6+
void dump(char *ptr, int size)
7+
{
8+
int i = 0;
9+
10+
for (i = 0; i < size; i++) {
11+
if (i != 0) {
12+
if (i % 16 == 0)
13+
printf("\n");
14+
else if (i % 8 == 0)
15+
printf(" ");
16+
}
17+
printf("%02x ", ptr[i]);
18+
}
19+
printf("\n");
20+
}
21+
22+
int main(void)
23+
{
24+
int i;
25+
unsigned long vl;
26+
char *datap, *tmp;
27+
28+
datap = malloc(MAX_VSIZE);
29+
if (!datap) {
30+
ksft_test_result_fail("fail to allocate memory for size = %lu\n", MAX_VSIZE);
31+
exit(-1);
32+
}
33+
34+
tmp = datap;
35+
asm volatile (
36+
".option push\n\t"
37+
".option arch, +v\n\t"
38+
"vsetvli %0, x0, e8, m8, ta, ma\n\t"
39+
"vse8.v v0, (%2)\n\t"
40+
"add %1, %2, %0\n\t"
41+
"vse8.v v8, (%1)\n\t"
42+
"add %1, %1, %0\n\t"
43+
"vse8.v v16, (%1)\n\t"
44+
"add %1, %1, %0\n\t"
45+
"vse8.v v24, (%1)\n\t"
46+
".option pop\n\t"
47+
: "=&r" (vl), "=r" (tmp) : "r" (datap) : "memory");
48+
49+
ksft_print_msg("vl = %lu\n", vl);
50+
51+
if (datap[0] != 0x00 && datap[0] != 0xff) {
52+
ksft_test_result_fail("v-regesters are not properly initialized\n");
53+
dump(datap, vl * 4);
54+
exit(-1);
55+
}
56+
57+
for (i = 1; i < vl * 4; i++) {
58+
if (datap[i] != datap[0]) {
59+
ksft_test_result_fail("detect stale values on v-regesters\n");
60+
dump(datap, vl * 4);
61+
exit(-2);
62+
}
63+
}
64+
65+
free(datap);
66+
ksft_exit_pass();
67+
return 0;
68+
}

0 commit comments

Comments
 (0)