Skip to content

Commit 66d0304

Browse files
geomatsiPaul Walmsley
authored andcommitted
selftests: riscv: verify initial vector state with ptrace
Add a test case that attaches to a traced process immediately after its first executed vector instructions to verify the initial vector context. 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-7-geomatsi@gmail.com Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent 600f72d commit 66d0304

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "kselftest_harness.h"
1313
#include "v_helpers.h"
1414

15+
#define SR_FS_DIRTY 0x00006000UL
16+
#define CSR_VXRM_SHIFT 1
17+
1518
volatile unsigned long chld_lock;
1619

1720
TEST(ptrace_v_not_enabled)
@@ -76,4 +79,136 @@ TEST(ptrace_v_not_enabled)
7679
}
7780
}
7881

82+
TEST(ptrace_v_early_debug)
83+
{
84+
static volatile unsigned long vstart;
85+
static volatile unsigned long vtype;
86+
static volatile unsigned long vlenb;
87+
static volatile unsigned long vcsr;
88+
static volatile unsigned long vl;
89+
bool xtheadvector;
90+
pid_t pid;
91+
92+
if (!(is_vector_supported() || is_xtheadvector_supported()))
93+
SKIP(return, "Vector not supported");
94+
95+
xtheadvector = is_xtheadvector_supported();
96+
97+
chld_lock = 1;
98+
pid = fork();
99+
ASSERT_LE(0, pid)
100+
TH_LOG("fork: %m");
101+
102+
if (pid == 0) {
103+
unsigned long vxsat, vxrm;
104+
105+
vlenb = get_vr_len();
106+
107+
while (chld_lock == 1)
108+
asm volatile ("" : : "g"(chld_lock) : "memory");
109+
110+
asm volatile (
111+
"csrr %[vstart], vstart\n"
112+
"csrr %[vtype], vtype\n"
113+
"csrr %[vl], vl\n"
114+
: [vtype] "=r"(vtype), [vstart] "=r"(vstart), [vl] "=r"(vl)
115+
:
116+
: "memory");
117+
118+
/* no 'is_xtheadvector_supported()' here to avoid clobbering v-state by syscall */
119+
if (xtheadvector) {
120+
asm volatile (
121+
"csrs sstatus, %[bit]\n"
122+
"csrr %[vxsat], vxsat\n"
123+
"csrr %[vxrm], vxrm\n"
124+
: [vxsat] "=r"(vxsat), [vxrm] "=r"(vxrm)
125+
: [bit] "r" (SR_FS_DIRTY)
126+
: "memory");
127+
vcsr = vxsat | vxrm << CSR_VXRM_SHIFT;
128+
} else {
129+
asm volatile (
130+
"csrr %[vcsr], vcsr\n"
131+
: [vcsr] "=r"(vcsr)
132+
:
133+
: "memory");
134+
}
135+
136+
asm volatile (
137+
".option push\n"
138+
".option norvc\n"
139+
"ebreak\n"
140+
".option pop\n");
141+
} else {
142+
struct __riscv_v_regset_state *regset_data;
143+
unsigned long vstart_csr;
144+
unsigned long vlenb_csr;
145+
unsigned long vtype_csr;
146+
unsigned long vcsr_csr;
147+
unsigned long vl_csr;
148+
size_t regset_size;
149+
struct iovec iov;
150+
int status;
151+
152+
/* attach */
153+
154+
ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid, NULL, NULL));
155+
ASSERT_EQ(pid, waitpid(pid, &status, 0));
156+
ASSERT_TRUE(WIFSTOPPED(status));
157+
158+
/* unlock */
159+
160+
ASSERT_EQ(0, ptrace(PTRACE_POKEDATA, pid, &chld_lock, 0));
161+
162+
/* resume and wait for ebreak */
163+
164+
ASSERT_EQ(0, ptrace(PTRACE_CONT, pid, NULL, NULL));
165+
ASSERT_EQ(pid, waitpid(pid, &status, 0));
166+
ASSERT_TRUE(WIFSTOPPED(status));
167+
168+
/* read tracee vector csr regs using ptrace PEEKDATA */
169+
170+
errno = 0;
171+
vstart_csr = ptrace(PTRACE_PEEKDATA, pid, &vstart, NULL);
172+
ASSERT_FALSE((errno != 0) && (vstart_csr == -1));
173+
174+
errno = 0;
175+
vl_csr = ptrace(PTRACE_PEEKDATA, pid, &vl, NULL);
176+
ASSERT_FALSE((errno != 0) && (vl_csr == -1));
177+
178+
errno = 0;
179+
vtype_csr = ptrace(PTRACE_PEEKDATA, pid, &vtype, NULL);
180+
ASSERT_FALSE((errno != 0) && (vtype_csr == -1));
181+
182+
errno = 0;
183+
vcsr_csr = ptrace(PTRACE_PEEKDATA, pid, &vcsr, NULL);
184+
ASSERT_FALSE((errno != 0) && (vcsr_csr == -1));
185+
186+
errno = 0;
187+
vlenb_csr = ptrace(PTRACE_PEEKDATA, pid, &vlenb, NULL);
188+
ASSERT_FALSE((errno != 0) && (vlenb_csr == -1));
189+
190+
/* read tracee csr regs using ptrace GETREGSET */
191+
192+
regset_size = sizeof(*regset_data) + vlenb_csr * 32;
193+
regset_data = calloc(1, regset_size);
194+
195+
iov.iov_base = regset_data;
196+
iov.iov_len = regset_size;
197+
198+
ASSERT_EQ(0, ptrace(PTRACE_GETREGSET, pid, NT_RISCV_VECTOR, &iov));
199+
200+
/* compare */
201+
202+
EXPECT_EQ(vstart_csr, regset_data->vstart);
203+
EXPECT_EQ(vtype_csr, regset_data->vtype);
204+
EXPECT_EQ(vlenb_csr, regset_data->vlenb);
205+
EXPECT_EQ(vcsr_csr, regset_data->vcsr);
206+
EXPECT_EQ(vl_csr, regset_data->vl);
207+
208+
/* cleanup */
209+
210+
ASSERT_EQ(0, kill(pid, SIGKILL));
211+
}
212+
}
213+
79214
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)