blob: ac0629f053651be7f769af74ed13edf2e2b7a7a2 [file] [log] [blame]
Mark Brown0dca2762020-08-19 12:48:33 +01001// SPDX-License-Identifier: GPL-2.0-only
2/*
Mark Browna1d71112021-09-13 13:55:04 +01003 * Copyright (C) 2015-2021 ARM Limited.
Mark Brown0dca2762020-08-19 12:48:33 +01004 * Original author: Dave Martin <Dave.Martin@arm.com>
5 */
6#include <errno.h>
Mark Browna1d71112021-09-13 13:55:04 +01007#include <stdbool.h>
Mark Brown0dca2762020-08-19 12:48:33 +01008#include <stddef.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <sys/auxv.h>
Mark Browna1d71112021-09-13 13:55:04 +010014#include <sys/prctl.h>
Mark Brown0dca2762020-08-19 12:48:33 +010015#include <sys/ptrace.h>
16#include <sys/types.h>
17#include <sys/uio.h>
18#include <sys/wait.h>
19#include <asm/sigcontext.h>
20#include <asm/ptrace.h>
21
22#include "../../kselftest.h"
23
Mark Browna1d71112021-09-13 13:55:04 +010024#define VL_TESTS (((SVE_VQ_MAX - SVE_VQ_MIN) + 1) * 3)
25#define FPSIMD_TESTS 3
26
27#define EXPECTED_TESTS (VL_TESTS + FPSIMD_TESTS)
Mark Brown78d2d812021-09-13 13:54:58 +010028
Mark Brown0dca2762020-08-19 12:48:33 +010029/* <linux/elf.h> and <sys/auxv.h> don't like each other, so: */
30#ifndef NT_ARM_SVE
31#define NT_ARM_SVE 0x405
32#endif
33
Mark Browna1d71112021-09-13 13:55:04 +010034static void fill_buf(char *buf, size_t size)
Mark Brown0dca2762020-08-19 12:48:33 +010035{
Mark Browna1d71112021-09-13 13:55:04 +010036 int i;
Mark Brown0dca2762020-08-19 12:48:33 +010037
Mark Browna1d71112021-09-13 13:55:04 +010038 for (i = 0; i < size; i++)
39 buf[i] = random();
Mark Brown0dca2762020-08-19 12:48:33 +010040}
41
Mark Brown0dca2762020-08-19 12:48:33 +010042static int do_child(void)
43{
44 if (ptrace(PTRACE_TRACEME, -1, NULL, NULL))
45 ksft_exit_fail_msg("PTRACE_TRACEME", strerror(errno));
46
47 if (raise(SIGSTOP))
48 ksft_exit_fail_msg("raise(SIGSTOP)", strerror(errno));
49
50 return EXIT_SUCCESS;
51}
52
Mark Brown9f7d03a2021-09-13 13:55:03 +010053static int get_fpsimd(pid_t pid, struct user_fpsimd_state *fpsimd)
54{
55 struct iovec iov;
56
57 iov.iov_base = fpsimd;
58 iov.iov_len = sizeof(*fpsimd);
59 return ptrace(PTRACE_GETREGSET, pid, NT_PRFPREG, &iov);
60}
61
Mark Brown0dca2762020-08-19 12:48:33 +010062static struct user_sve_header *get_sve(pid_t pid, void **buf, size_t *size)
63{
64 struct user_sve_header *sve;
65 void *p;
66 size_t sz = sizeof *sve;
67 struct iovec iov;
68
69 while (1) {
70 if (*size < sz) {
71 p = realloc(*buf, sz);
72 if (!p) {
73 errno = ENOMEM;
74 goto error;
75 }
76
77 *buf = p;
78 *size = sz;
79 }
80
81 iov.iov_base = *buf;
82 iov.iov_len = sz;
83 if (ptrace(PTRACE_GETREGSET, pid, NT_ARM_SVE, &iov))
84 goto error;
85
86 sve = *buf;
87 if (sve->size <= sz)
88 break;
89
90 sz = sve->size;
91 }
92
93 return sve;
94
95error:
96 return NULL;
97}
98
99static int set_sve(pid_t pid, const struct user_sve_header *sve)
100{
101 struct iovec iov;
102
103 iov.iov_base = (void *)sve;
104 iov.iov_len = sve->size;
105 return ptrace(PTRACE_SETREGSET, pid, NT_ARM_SVE, &iov);
106}
107
Mark Browna1d71112021-09-13 13:55:04 +0100108/* Validate attempting to set the specfied VL via ptrace */
109static void ptrace_set_get_vl(pid_t child, unsigned int vl, bool *supported)
Mark Brown0dca2762020-08-19 12:48:33 +0100110{
Mark Browna1d71112021-09-13 13:55:04 +0100111 struct user_sve_header sve;
112 struct user_sve_header *new_sve = NULL;
113 size_t new_sve_size = 0;
114 int ret, prctl_vl;
Mark Brown0dca2762020-08-19 12:48:33 +0100115
Mark Browna1d71112021-09-13 13:55:04 +0100116 *supported = false;
Mark Brown0dca2762020-08-19 12:48:33 +0100117
Mark Browna1d71112021-09-13 13:55:04 +0100118 /* Check if the VL is supported in this process */
119 prctl_vl = prctl(PR_SVE_SET_VL, vl);
120 if (prctl_vl == -1)
121 ksft_exit_fail_msg("prctl(PR_SVE_SET_VL) failed: %s (%d)\n",
122 strerror(errno), errno);
Mark Brown0dca2762020-08-19 12:48:33 +0100123
Mark Browna1d71112021-09-13 13:55:04 +0100124 /* If the VL is not supported then a supported VL will be returned */
125 *supported = (prctl_vl == vl);
126
127 /* Set the VL by doing a set with no register payload */
128 memset(&sve, 0, sizeof(sve));
129 sve.size = sizeof(sve);
130 sve.vl = vl;
131 ret = set_sve(child, &sve);
132 if (ret != 0) {
133 ksft_test_result_fail("Failed to set VL %u\n", vl);
134 return;
Mark Brown0dca2762020-08-19 12:48:33 +0100135 }
Mark Browna1d71112021-09-13 13:55:04 +0100136
137 /*
138 * Read back the new register state and verify that we have the
139 * same VL that we got from prctl() on ourselves.
140 */
141 if (!get_sve(child, (void **)&new_sve, &new_sve_size)) {
142 ksft_test_result_fail("Failed to read VL %u\n", vl);
143 return;
144 }
145
146 ksft_test_result(new_sve->vl = prctl_vl, "Set VL %u\n", vl);
147
148 free(new_sve);
149}
150
151static void check_u32(unsigned int vl, const char *reg,
152 uint32_t *in, uint32_t *out, int *errors)
153{
154 if (*in != *out) {
155 printf("# VL %d %s wrote %x read %x\n",
156 vl, reg, *in, *out);
157 (*errors)++;
158 }
159}
160
Mark Brown34785032021-09-13 13:55:05 +0100161/* Access the FPSIMD registers via the SVE regset */
162static void ptrace_sve_fpsimd(pid_t child)
163{
164 void *svebuf = NULL;
165 size_t svebufsz = 0;
166 struct user_sve_header *sve;
167 struct user_fpsimd_state *fpsimd, new_fpsimd;
168 unsigned int i, j;
169 unsigned char *p;
170
171 /* New process should start with FPSIMD registers only */
172 sve = get_sve(child, &svebuf, &svebufsz);
173 if (!sve) {
174 ksft_test_result_fail("get_sve: %s\n", strerror(errno));
175
176 return;
177 } else {
178 ksft_test_result_pass("get_sve(FPSIMD)\n");
179 }
180
181 ksft_test_result((sve->flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD,
182 "Set FPSIMD registers\n");
183 if ((sve->flags & SVE_PT_REGS_MASK) != SVE_PT_REGS_FPSIMD)
184 goto out;
185
186 /* Try to set a known FPSIMD state via PT_REGS_SVE */
187 fpsimd = (struct user_fpsimd_state *)((char *)sve +
188 SVE_PT_FPSIMD_OFFSET);
189 for (i = 0; i < 32; ++i) {
190 p = (unsigned char *)&fpsimd->vregs[i];
191
192 for (j = 0; j < sizeof(fpsimd->vregs[i]); ++j)
193 p[j] = j;
194 }
195
196 if (set_sve(child, sve)) {
197 ksft_test_result_fail("set_sve(FPSIMD): %s\n",
198 strerror(errno));
199
200 goto out;
201 }
202
203 /* Verify via the FPSIMD regset */
204 if (get_fpsimd(child, &new_fpsimd)) {
205 ksft_test_result_fail("get_fpsimd(): %s\n",
206 strerror(errno));
207 goto out;
208 }
209 if (memcmp(fpsimd, &new_fpsimd, sizeof(*fpsimd)) == 0)
210 ksft_test_result_pass("get_fpsimd() gave same state\n");
211 else
212 ksft_test_result_fail("get_fpsimd() gave different state\n");
213
214out:
215 free(svebuf);
216}
217
Mark Browna1d71112021-09-13 13:55:04 +0100218/* Validate attempting to set SVE data and read SVE data */
219static void ptrace_set_sve_get_sve_data(pid_t child, unsigned int vl)
220{
221 void *write_buf;
222 void *read_buf = NULL;
223 struct user_sve_header *write_sve;
224 struct user_sve_header *read_sve;
225 size_t read_sve_size = 0;
226 unsigned int vq = sve_vq_from_vl(vl);
227 int ret, i;
228 size_t data_size;
229 int errors = 0;
230
231 data_size = SVE_PT_SVE_OFFSET + SVE_PT_SVE_SIZE(vq, SVE_PT_REGS_SVE);
232 write_buf = malloc(data_size);
233 if (!write_buf) {
234 ksft_test_result_fail("Error allocating %d byte buffer for VL %u\n",
235 data_size, vl);
236 return;
237 }
238 write_sve = write_buf;
239
240 /* Set up some data and write it out */
241 memset(write_sve, 0, data_size);
242 write_sve->size = data_size;
243 write_sve->vl = vl;
244 write_sve->flags = SVE_PT_REGS_SVE;
245
246 for (i = 0; i < __SVE_NUM_ZREGS; i++)
247 fill_buf(write_buf + SVE_PT_SVE_ZREG_OFFSET(vq, i),
248 SVE_PT_SVE_ZREG_SIZE(vq));
249
250 for (i = 0; i < __SVE_NUM_PREGS; i++)
251 fill_buf(write_buf + SVE_PT_SVE_PREG_OFFSET(vq, i),
252 SVE_PT_SVE_PREG_SIZE(vq));
253
254 fill_buf(write_buf + SVE_PT_SVE_FPSR_OFFSET(vq), SVE_PT_SVE_FPSR_SIZE);
255 fill_buf(write_buf + SVE_PT_SVE_FPCR_OFFSET(vq), SVE_PT_SVE_FPCR_SIZE);
256
257 /* TODO: Generate a valid FFR pattern */
258
259 ret = set_sve(child, write_sve);
260 if (ret != 0) {
261 ksft_test_result_fail("Failed to set VL %u data\n", vl);
262 goto out;
263 }
264
265 /* Read the data back */
266 if (!get_sve(child, (void **)&read_buf, &read_sve_size)) {
267 ksft_test_result_fail("Failed to read VL %u data\n", vl);
268 goto out;
269 }
270 read_sve = read_buf;
271
272 /* We might read more data if there's extensions we don't know */
273 if (read_sve->size < write_sve->size) {
274 ksft_test_result_fail("Wrote %d bytes, only read %d\n",
275 write_sve->size, read_sve->size);
276 goto out_read;
277 }
278
279 for (i = 0; i < __SVE_NUM_ZREGS; i++) {
280 if (memcmp(write_buf + SVE_PT_SVE_ZREG_OFFSET(vq, i),
281 read_buf + SVE_PT_SVE_ZREG_OFFSET(vq, i),
282 SVE_PT_SVE_ZREG_SIZE(vq)) != 0) {
283 printf("# Mismatch in %u Z%d\n", vl, i);
284 errors++;
285 }
286 }
287
288 for (i = 0; i < __SVE_NUM_PREGS; i++) {
289 if (memcmp(write_buf + SVE_PT_SVE_PREG_OFFSET(vq, i),
290 read_buf + SVE_PT_SVE_PREG_OFFSET(vq, i),
291 SVE_PT_SVE_PREG_SIZE(vq)) != 0) {
292 printf("# Mismatch in %u P%d\n", vl, i);
293 errors++;
294 }
295 }
296
297 check_u32(vl, "FPSR", write_buf + SVE_PT_SVE_FPSR_OFFSET(vq),
298 read_buf + SVE_PT_SVE_FPSR_OFFSET(vq), &errors);
299 check_u32(vl, "FPCR", write_buf + SVE_PT_SVE_FPCR_OFFSET(vq),
300 read_buf + SVE_PT_SVE_FPCR_OFFSET(vq), &errors);
301
302 ksft_test_result(errors == 0, "Set and get SVE data for VL %u\n", vl);
303
304out_read:
305 free(read_buf);
306out:
307 free(write_buf);
308}
309
310/* Validate attempting to set SVE data and read SVE data */
311static void ptrace_set_sve_get_fpsimd_data(pid_t child, unsigned int vl)
312{
313 void *write_buf;
314 struct user_sve_header *write_sve;
315 unsigned int vq = sve_vq_from_vl(vl);
316 struct user_fpsimd_state fpsimd_state;
317 int ret, i;
318 size_t data_size;
319 int errors = 0;
320
321 if (__BYTE_ORDER == __BIG_ENDIAN) {
322 ksft_test_result_skip("Big endian not supported\n");
323 return;
324 }
325
326 data_size = SVE_PT_SVE_OFFSET + SVE_PT_SVE_SIZE(vq, SVE_PT_REGS_SVE);
327 write_buf = malloc(data_size);
328 if (!write_buf) {
329 ksft_test_result_fail("Error allocating %d byte buffer for VL %u\n",
330 data_size, vl);
331 return;
332 }
333 write_sve = write_buf;
334
335 /* Set up some data and write it out */
336 memset(write_sve, 0, data_size);
337 write_sve->size = data_size;
338 write_sve->vl = vl;
339 write_sve->flags = SVE_PT_REGS_SVE;
340
341 for (i = 0; i < __SVE_NUM_ZREGS; i++)
342 fill_buf(write_buf + SVE_PT_SVE_ZREG_OFFSET(vq, i),
343 SVE_PT_SVE_ZREG_SIZE(vq));
344
345 fill_buf(write_buf + SVE_PT_SVE_FPSR_OFFSET(vq), SVE_PT_SVE_FPSR_SIZE);
346 fill_buf(write_buf + SVE_PT_SVE_FPCR_OFFSET(vq), SVE_PT_SVE_FPCR_SIZE);
347
348 ret = set_sve(child, write_sve);
349 if (ret != 0) {
350 ksft_test_result_fail("Failed to set VL %u data\n", vl);
351 goto out;
352 }
353
354 /* Read the data back */
355 if (get_fpsimd(child, &fpsimd_state)) {
356 ksft_test_result_fail("Failed to read VL %u FPSIMD data\n",
357 vl);
358 goto out;
359 }
360
361 for (i = 0; i < __SVE_NUM_ZREGS; i++) {
362 __uint128_t tmp = 0;
363
364 /*
365 * Z regs are stored endianness invariant, this won't
366 * work for big endian
367 */
368 memcpy(&tmp, write_buf + SVE_PT_SVE_ZREG_OFFSET(vq, i),
369 sizeof(tmp));
370
371 if (tmp != fpsimd_state.vregs[i]) {
372 printf("# Mismatch in FPSIMD for VL %u Z%d\n", vl, i);
373 errors++;
374 }
375 }
376
377 check_u32(vl, "FPSR", write_buf + SVE_PT_SVE_FPSR_OFFSET(vq),
378 &fpsimd_state.fpsr, &errors);
379 check_u32(vl, "FPCR", write_buf + SVE_PT_SVE_FPCR_OFFSET(vq),
380 &fpsimd_state.fpcr, &errors);
381
382 ksft_test_result(errors == 0, "Set and get FPSIMD data for VL %u\n",
383 vl);
384
385out:
386 free(write_buf);
Mark Brown0dca2762020-08-19 12:48:33 +0100387}
388
389static int do_parent(pid_t child)
390{
391 int ret = EXIT_FAILURE;
392 pid_t pid;
393 int status;
394 siginfo_t si;
Mark Browna1d71112021-09-13 13:55:04 +0100395 unsigned int vq, vl;
396 bool vl_supported;
Mark Brown0dca2762020-08-19 12:48:33 +0100397
398 /* Attach to the child */
399 while (1) {
400 int sig;
401
402 pid = wait(&status);
403 if (pid == -1) {
404 perror("wait");
405 goto error;
406 }
407
408 /*
409 * This should never happen but it's hard to flag in
410 * the framework.
411 */
412 if (pid != child)
413 continue;
414
415 if (WIFEXITED(status) || WIFSIGNALED(status))
416 ksft_exit_fail_msg("Child died unexpectedly\n");
417
Mark Brown0dca2762020-08-19 12:48:33 +0100418 if (!WIFSTOPPED(status))
419 goto error;
420
421 sig = WSTOPSIG(status);
422
423 if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) {
424 if (errno == ESRCH)
425 goto disappeared;
426
427 if (errno == EINVAL) {
428 sig = 0; /* bust group-stop */
429 goto cont;
430 }
431
432 ksft_test_result_fail("PTRACE_GETSIGINFO: %s\n",
433 strerror(errno));
434 goto error;
435 }
436
437 if (sig == SIGSTOP && si.si_code == SI_TKILL &&
438 si.si_pid == pid)
439 break;
440
441 cont:
442 if (ptrace(PTRACE_CONT, pid, NULL, sig)) {
443 if (errno == ESRCH)
444 goto disappeared;
445
446 ksft_test_result_fail("PTRACE_CONT: %s\n",
447 strerror(errno));
448 goto error;
449 }
450 }
451
Mark Brown34785032021-09-13 13:55:05 +0100452 /* FPSIMD via SVE regset */
453 ptrace_sve_fpsimd(child);
Mark Brown9f7d03a2021-09-13 13:55:03 +0100454
Mark Browna1d71112021-09-13 13:55:04 +0100455 /* Step through every possible VQ */
456 for (vq = SVE_VQ_MIN; vq <= SVE_VQ_MAX; vq++) {
457 vl = sve_vl_from_vq(vq);
Mark Brown0dca2762020-08-19 12:48:33 +0100458
Mark Browna1d71112021-09-13 13:55:04 +0100459 /* First, try to set this vector length */
460 ptrace_set_get_vl(child, vl, &vl_supported);
Mark Brown0dca2762020-08-19 12:48:33 +0100461
Mark Browna1d71112021-09-13 13:55:04 +0100462 /* If the VL is supported validate data set/get */
463 if (vl_supported) {
464 ptrace_set_sve_get_sve_data(child, vl);
465 ptrace_set_sve_get_fpsimd_data(child, vl);
466 } else {
467 ksft_test_result_skip("set SVE get SVE for VL %d\n", vl);
468 ksft_test_result_skip("set SVE get FPSIMD for VL %d\n", vl);
469 }
Mark Brown0dca2762020-08-19 12:48:33 +0100470 }
471
472 ret = EXIT_SUCCESS;
473
474error:
475 kill(child, SIGKILL);
476
477disappeared:
478 return ret;
479}
480
481int main(void)
482{
483 int ret = EXIT_SUCCESS;
Mark Brown0dca2762020-08-19 12:48:33 +0100484 pid_t child;
485
Mark Browna1d71112021-09-13 13:55:04 +0100486 srandom(getpid());
487
Mark Brown0dca2762020-08-19 12:48:33 +0100488 ksft_print_header();
Mark Brown78d2d812021-09-13 13:54:58 +0100489 ksft_set_plan(EXPECTED_TESTS);
Mark Brown0dca2762020-08-19 12:48:33 +0100490
491 if (!(getauxval(AT_HWCAP) & HWCAP_SVE))
492 ksft_exit_skip("SVE not available\n");
493
Mark Brown0dca2762020-08-19 12:48:33 +0100494 child = fork();
495 if (!child)
496 return do_child();
497
498 if (do_parent(child))
499 ret = EXIT_FAILURE;
500
501 ksft_print_cnts();
502
Mark Brown07e64482021-03-09 19:03:04 +0000503 return ret;
Mark Brown0dca2762020-08-19 12:48:33 +0100504}