Skip to content

Commit 600f72d

Browse files
geomatsiPaul Walmsley
authored andcommitted
selftests: riscv: test ptrace vector interface
Add a test case to check ptrace behavior in the case when vector extension is supported by the system, but vector context is not yet enabled for the traced process. Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com> Reviewed-by: Andy Chiu <andybnac@gmail.com> Tested-by: Andy Chiu <andybnac@gmail.com> Link: https://patch.msgid.link/20251214163537.1054292-6-geomatsi@gmail.com [pjw@kernel.org: dropped duplicate sys/wait.h include] Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent f4be988 commit 600f72d

5 files changed

Lines changed: 115 additions & 1 deletion

File tree

tools/testing/selftests/riscv/vector/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vstate_exec_nolibc
22
vstate_prctl
33
v_initval
44
v_exec_initval_nolibc
5+
vstate_ptrace
6+
validate_v_ptrace

tools/testing/selftests/riscv/vector/Makefile

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

5-
TEST_GEN_PROGS := v_initval vstate_prctl vstate_ptrace
5+
TEST_GEN_PROGS := v_initval vstate_prctl vstate_ptrace validate_v_ptrace
66
TEST_GEN_PROGS_EXTENDED := vstate_exec_nolibc v_exec_initval_nolibc
7+
TEST_GEN_LIBS := v_helpers.c sys_hwprobe.c
78

89
include ../../lib.mk
910

11+
TEST_GEN_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(TEST_GEN_LIBS))
12+
1013
$(OUTPUT)/sys_hwprobe.o: ../hwprobe/sys_hwprobe.S
1114
$(CC) -static -c -o$@ $(CFLAGS) $^
1215

@@ -29,3 +32,8 @@ $(OUTPUT)/v_exec_initval_nolibc: v_exec_initval_nolibc.c
2932

3033
$(OUTPUT)/vstate_ptrace: vstate_ptrace.c $(OUTPUT)/sys_hwprobe.o $(OUTPUT)/v_helpers.o
3134
$(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^
35+
36+
$(OUTPUT)/validate_v_ptrace: validate_v_ptrace.c $(OUTPUT)/sys_hwprobe.o $(OUTPUT)/v_helpers.o
37+
$(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^
38+
39+
EXTRA_CLEAN += $(TEST_GEN_OBJ)

tools/testing/selftests/riscv/vector/v_helpers.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ bool is_vector_supported(void)
2626
return pair.value & RISCV_HWPROBE_EXT_ZVE32X;
2727
}
2828

29+
unsigned long get_vr_len(void)
30+
{
31+
unsigned long vlenb;
32+
33+
if (is_vector_supported()) {
34+
asm volatile("csrr %[vlenb], vlenb" : [vlenb] "=r"(vlenb));
35+
return vlenb;
36+
}
37+
38+
if (is_xtheadvector_supported()) {
39+
asm volatile (
40+
// 0 | zimm[10:0] | rs1 | 1 1 1 | rd | 1010111 | vsetvli
41+
// vsetvli t4, x0, e8, m1, d1
42+
".4byte 0b00000000000000000111111011010111\n\t"
43+
"mv %[vlenb], t4\n\t"
44+
: [vlenb] "=r"(vlenb) : : "memory", "t4");
45+
return vlenb;
46+
}
47+
48+
printf("WARNING: vector not supported\n");
49+
return 0;
50+
}
51+
2952
int launch_test(char *next_program, int test_inherit, int xtheadvector)
3053
{
3154
char *exec_argv[4], *exec_envp[1];

tools/testing/selftests/riscv/vector/v_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ bool is_xtheadvector_supported(void);
55

66
bool is_vector_supported(void);
77

8+
unsigned long get_vr_len(void);
9+
810
int launch_test(char *next_program, int test_inherit, int xtheadvector);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <sys/ptrace.h>
3+
#include <sys/types.h>
4+
#include <sys/wait.h>
5+
#include <sys/uio.h>
6+
#include <unistd.h>
7+
#include <errno.h>
8+
9+
#include <linux/ptrace.h>
10+
#include <linux/elf.h>
11+
12+
#include "kselftest_harness.h"
13+
#include "v_helpers.h"
14+
15+
volatile unsigned long chld_lock;
16+
17+
TEST(ptrace_v_not_enabled)
18+
{
19+
pid_t pid;
20+
21+
if (!(is_vector_supported() || is_xtheadvector_supported()))
22+
SKIP(return, "Vector not supported");
23+
24+
chld_lock = 1;
25+
pid = fork();
26+
ASSERT_LE(0, pid)
27+
TH_LOG("fork: %m");
28+
29+
if (pid == 0) {
30+
while (chld_lock == 1)
31+
asm volatile("" : : "g"(chld_lock) : "memory");
32+
33+
asm volatile ("ebreak" : : : );
34+
} else {
35+
struct __riscv_v_regset_state *regset_data;
36+
unsigned long vlenb = get_vr_len();
37+
size_t regset_size;
38+
struct iovec iov;
39+
int status;
40+
int ret;
41+
42+
/* attach */
43+
44+
ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid, NULL, NULL));
45+
ASSERT_EQ(pid, waitpid(pid, &status, 0));
46+
ASSERT_TRUE(WIFSTOPPED(status));
47+
48+
/* unlock */
49+
50+
ASSERT_EQ(0, ptrace(PTRACE_POKEDATA, pid, &chld_lock, 0));
51+
52+
/* resume and wait for ebreak */
53+
54+
ASSERT_EQ(0, ptrace(PTRACE_CONT, pid, NULL, NULL));
55+
ASSERT_EQ(pid, waitpid(pid, &status, 0));
56+
ASSERT_TRUE(WIFSTOPPED(status));
57+
58+
/* try to read vector registers from the tracee */
59+
60+
regset_size = sizeof(*regset_data) + vlenb * 32;
61+
regset_data = calloc(1, regset_size);
62+
63+
iov.iov_base = regset_data;
64+
iov.iov_len = regset_size;
65+
66+
/* V extension is available, but not yet enabled for the tracee */
67+
68+
errno = 0;
69+
ret = ptrace(PTRACE_GETREGSET, pid, NT_RISCV_VECTOR, &iov);
70+
ASSERT_EQ(ENODATA, errno);
71+
ASSERT_EQ(-1, ret);
72+
73+
/* cleanup */
74+
75+
ASSERT_EQ(0, kill(pid, SIGKILL));
76+
}
77+
}
78+
79+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)