1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2023,2024 Arm Ltd
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/auxv.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <sys/ucontext.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <machine/armreg.h>
#include <machine/sysarch.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <atf-c.h>
static unsigned long vl;
static void
check_for_sve(void)
{
unsigned long hwcap;
if (elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)) != 0)
atf_tc_skip("No HWCAP");
if ((hwcap & HWCAP_SVE) == 0)
atf_tc_skip("No SVE support in HW");
ATF_REQUIRE(sysarch(ARM64_GET_SVE_VL, &vl) == 0);
}
ATF_TC_WITHOUT_HEAD(sve_registers);
ATF_TC_BODY(sve_registers, tc)
{
uint64_t reg, val;
check_for_sve();
/* Check the ID registers are sane */
reg = READ_SPECIALREG(id_aa64pfr0_el1);
ATF_REQUIRE((reg & ID_AA64PFR0_SVE_MASK) >= ID_AA64PFR0_SVE_IMPL);
/*
* Store 1.0 in z0, repeated every 16 bits. Read it from d0 as
* the registers alias.
*/
asm volatile(
".arch_extension sve \n"
"fmov z0.h, #1 \n"
"str d0, [%0] \n"
".arch_extension nosve \n"
:: "r"(&val) : "z0", "d0"
);
/* Check for the 1.0 bit pattern */
ATF_REQUIRE_EQ(val, 0x3c003c003c003c00);
}
static void
sve_signal_handler(int sig __unused, siginfo_t *info, void *context)
{
struct arm64_reg_context *regctx;
struct sve_context *svectx;
ucontext_t *ctx;
uint64_t *sveregs;
ctx = context;
/* Check the trap is from a breakpoint instruction */
ATF_REQUIRE_EQ(info->si_trapno, EXCP_BRK);
ctx->uc_mcontext.mc_gpregs.gp_elr += 4;
/* Trash z0 to check it's not kept when exiting the handler */
asm volatile(
".arch_extension sve \n"
"fmov z0.h, #2 \n"
".arch_extension nosve \n"
::: "z0");
/* Change the lower bits of z1 through the siginfo struct */
ctx->uc_mcontext.mc_fpregs.fp_q[1] = 0x5a5a5a5a5a5a5a5a;
/* Find the SVE registers */
regctx = (struct arm64_reg_context *)ctx->uc_mcontext.mc_ptr;
if (regctx != NULL) {
int idx, next;
do {
if (regctx->ctx_id == ARM64_CTX_SVE)
break;
ATF_REQUIRE(regctx->ctx_id != ARM64_CTX_END);
regctx = (struct arm64_reg_context *)
((uintptr_t)regctx + regctx->ctx_size);
} while (1);
/* Update the register context */
svectx = (struct sve_context *)regctx;
ATF_REQUIRE_EQ(svectx->sve_vector_len, vl);
sveregs = (uint64_t *)(void *)(svectx + 1);
/* Find the array entries to change */
idx = 2 * svectx->sve_vector_len / sizeof(*sveregs);
next = 3 * svectx->sve_vector_len / sizeof(*sveregs);
while (idx != next) {
sveregs[idx] = 0xdeaddeaddeaddead;
idx++;
}
}
}
ATF_TC_WITHOUT_HEAD(sve_signal);
ATF_TC_BODY(sve_signal, tc)
{
struct sigaction sa = {
.sa_sigaction = sve_signal_handler,
.sa_flags = SA_SIGINFO,
};
uint64_t val0, val1, *val2;
check_for_sve();
ATF_REQUIRE(sigaction(SIGTRAP, &sa, NULL) == 0);
val2 = malloc(vl);
ATF_REQUIRE(val2 != NULL);
asm volatile(
".arch_extension sve \n"
"fmov z0.h, #1 \n"
"fmov z1.h, #1 \n"
"fmov z2.h, #1 \n"
/* Raise a SIGTRAP */
"brk #1 \n"
"str d0, [%0] \n"
"str d1, [%1] \n"
"str z2, [%2] \n"
".arch_extension nosve \n"
:: "r"(&val0), "r"(&val1), "r"(val2) : "z0", "z1", "z2", "d0", "d1"
);
/* Check for the 1.0 bit pattern */
ATF_REQUIRE_EQ(val0, 0x3c003c003c003c00);
/* Check for the changed bit pattern */
ATF_REQUIRE_EQ(val1, 0x5a5a5a5a5a5a5a5a);
/*
* Check the lower 128 bits are restored from fp_q and the
* upper bits are restored from the sve data region
*/
for (size_t i = 0; i < vl / sizeof(*val2); i++) {
if (i < 2)
ATF_REQUIRE_EQ(val2[i], 0x3c003c003c003c00);
else
ATF_REQUIRE_EQ(val2[i], 0xdeaddeaddeaddead);
}
free(val2);
}
ATF_TC_WITHOUT_HEAD(sve_signal_altstack);
ATF_TC_BODY(sve_signal_altstack, tc)
{
struct sigaction sa = {
.sa_sigaction = sve_signal_handler,
.sa_flags = SA_ONSTACK | SA_SIGINFO,
};
stack_t ss = {
.ss_size = SIGSTKSZ,
};
uint64_t val0, val1, *val2;
check_for_sve();
ss.ss_sp = malloc(ss.ss_size);
ATF_REQUIRE(ss.ss_sp != NULL);
ATF_REQUIRE(sigaltstack(&ss, NULL) == 0);
ATF_REQUIRE(sigaction(SIGTRAP, &sa, NULL) == 0);
val2 = malloc(vl);
ATF_REQUIRE(val2 != NULL);
asm volatile(
".arch_extension sve \n"
"fmov z0.h, #1 \n"
"fmov z1.h, #1 \n"
"fmov z2.h, #1 \n"
/* Raise a SIGTRAP */
"brk #1 \n"
"str d0, [%0] \n"
"str d1, [%1] \n"
"str z2, [%2] \n"
".arch_extension nosve \n"
:: "r"(&val0), "r"(&val1), "r"(val2) : "z0", "z1", "z2", "d0", "d1"
);
/* Check for the 1.0 bit pattern */
ATF_REQUIRE_EQ(val0, 0x3c003c003c003c00);
/* Check for the changed bit pattern */
ATF_REQUIRE_EQ(val1, 0x5a5a5a5a5a5a5a5a);
/*
* Check the lower 128 bits are restored from fp_q and the
* upper bits are restored from the sve data region
*/
for (size_t i = 0; i < vl / sizeof(*val2); i++) {
if (i < 2)
ATF_REQUIRE_EQ(val2[i], 0x3c003c003c003c00);
else
ATF_REQUIRE_EQ(val2[i], 0xdeaddeaddeaddead);
}
free(val2);
}
ATF_TC_WITHOUT_HEAD(sve_ptrace);
ATF_TC_BODY(sve_ptrace, tc)
{
struct reg reg;
struct iovec fpvec, svevec;
struct svereg_header *header;
pid_t child, wpid;
int status;
check_for_sve();
child = fork();
ATF_REQUIRE(child >= 0);
if (child == 0) {
char exec_path[1024];
/* Calculate the location of the helper */
snprintf(exec_path, sizeof(exec_path), "%s/sve_ptrace_helper",
atf_tc_get_config_var(tc, "srcdir"));
ptrace(PT_TRACE_ME, 0, NULL, 0);
/* Execute the helper so SVE will be disabled */
execl(exec_path, "sve_ptrace_helper", NULL);
_exit(1);
}
/* The first event should be the SIGSTOP at the start of the child */
wpid = waitpid(child, &status, 0);
ATF_REQUIRE(WIFSTOPPED(status));
fpvec.iov_base = NULL;
fpvec.iov_len = 0;
/* Read the length before SVE has been used */
ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&fpvec, NT_ARM_SVE) ==
0);
ATF_REQUIRE(fpvec.iov_len == (sizeof(struct svereg_header) +
sizeof(struct fpregs)));
fpvec.iov_base = malloc(fpvec.iov_len);
header = fpvec.iov_base;
ATF_REQUIRE(fpvec.iov_base != NULL);
ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&fpvec, NT_ARM_SVE) ==
0);
ATF_REQUIRE((header->sve_flags & SVEREG_FLAG_REGS_MASK) ==
SVEREG_FLAG_FP);
/* Check writing back the FP registers works */
ATF_REQUIRE(ptrace(PT_SETREGSET, wpid, (caddr_t)&fpvec, NT_ARM_SVE) ==
0);
ptrace(PT_CONTINUE, wpid, (caddr_t)1, 0);
/* The second event should be the SIGINFO at the end */
wpid = waitpid(child, &status, 0);
ATF_REQUIRE(WIFSTOPPED(status));
ATF_REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
/* Check writing back FP registers when SVE has started will fail */
ATF_REQUIRE(ptrace(PT_SETREGSET, wpid, (caddr_t)&fpvec, NT_ARM_SVE) ==
-1);
svevec.iov_base = NULL;
svevec.iov_len = 0;
/* Read the length after SVE has been used */
ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&svevec, NT_ARM_SVE) ==
0);
/* TODO: Check the length is correct based on vector length */
ATF_REQUIRE(svevec.iov_len > (sizeof(struct svereg_header) +
sizeof(struct fpregs)));
svevec.iov_base = malloc(svevec.iov_len);
header = svevec.iov_base;
ATF_REQUIRE(svevec.iov_base != NULL);
ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&svevec, NT_ARM_SVE) ==
0);
ATF_REQUIRE((header->sve_flags & SVEREG_FLAG_REGS_MASK) ==
SVEREG_FLAG_SVE);
/* Test writing back the SVE registers works */
ATF_REQUIRE(ptrace(PT_SETREGSET, wpid, (caddr_t)&svevec, NT_ARM_SVE) ==
0);
free(svevec.iov_base);
free(fpvec.iov_base);
/* Step over the brk instruction */
ATF_REQUIRE(ptrace(PT_GETREGS, wpid, (caddr_t)®, 0) != -1);
reg.elr += 4;
ATF_REQUIRE(ptrace(PT_SETREGS, wpid, (caddr_t)®, 0) != -1);
ptrace(PT_CONTINUE, wpid, (caddr_t)1, 0);
waitpid(child, &status, 0);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
}
ATF_TC_WITHOUT_HEAD(sve_fork_env);
ATF_TC_BODY(sve_fork_env, tc)
{
pid_t child, wpid;
int status;
check_for_sve();
child = fork();
ATF_REQUIRE(child >= 0);
/* Check the child environment is sane */
if (child == 0) {
unsigned long child_vl, hwcap;
if (elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)) != 0)
_exit(1);
if ((hwcap & HWCAP_SVE) == 0)
_exit(2);
if (sysarch(ARM64_GET_SVE_VL, &child_vl) != 0)
_exit(3);
if (child_vl != vl)
_exit(4);
_exit(0);
}
wpid = waitpid(child, &status, 0);
ATF_REQUIRE_EQ(child, wpid);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE(WEXITSTATUS(status) == 0);
}
ATF_TC_WITHOUT_HEAD(sve_fork_regs);
ATF_TC_BODY(sve_fork_regs, tc)
{
pid_t child, wpid;
uint64_t *val;
int status;
check_for_sve();
/*
* Malloc before fork to reduce the change of trashing sve registers
*/
val = malloc(vl);
ATF_REQUIRE(val != NULL);
/*
* Store 1.0 in z0, repeated every 16 bits. Read it from d0 as
* the registers alias.
*/
asm volatile(
".arch_extension sve \n"
"fmov z8.h, #1 \n"
".arch_extension nosve \n"
::: "z8"
);
/* TODO: Move to asm to ensure z8 isn't trashed */
child = fork();
asm volatile(
".arch_extension sve \n"
"str z8, [%0] \n"
".arch_extension nosve \n"
:: "r"(val) : "z8"
);
ATF_REQUIRE(child >= 0);
/* Check the child environment is sane */
if (child == 0) {
for (size_t i = 0; i < vl / sizeof(*val); i++) {
if (val[i] != 0x3c003c003c003c00)
_exit(i + 1);
}
free(val);
_exit(0);
}
free(val);
wpid = waitpid(child, &status, 0);
ATF_REQUIRE_EQ(child, wpid);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE(WEXITSTATUS(status) == 0);
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, sve_registers);
ATF_TP_ADD_TC(tp, sve_signal);
ATF_TP_ADD_TC(tp, sve_signal_altstack);
/* TODO: Check a too small signal stack */
ATF_TP_ADD_TC(tp, sve_ptrace);
ATF_TP_ADD_TC(tp, sve_fork_env);
ATF_TP_ADD_TC(tp, sve_fork_regs);
return (atf_no_error());
}
|