1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 ARM Limited
4  *
5  * Verify that the ZA register context in signal frames is set up as
6  * expected.
7  */
8 
9 #include <kselftest.h>
10 #include <signal.h>
11 #include <ucontext.h>
12 #include <sys/prctl.h>
13 
14 #include "test_signals_utils.h"
15 #include "sve_helpers.h"
16 #include "testcases.h"
17 
18 static union {
19 	ucontext_t uc;
20 	char buf[1024 * 128];
21 } context;
22 
sme_get_vls(struct tdescr * td)23 static bool sme_get_vls(struct tdescr *td)
24 {
25 	int res = sve_fill_vls(VLS_USE_SME, 1);
26 
27 	if (!res)
28 		return true;
29 
30 	if (res == KSFT_SKIP)
31 		td->result = KSFT_SKIP;
32 
33 	return false;
34 }
35 
setup_za_regs(void)36 static void setup_za_regs(void)
37 {
38 	/* smstart za; real data is TODO */
39 	asm volatile(".inst 0xd503457f" : : : );
40 }
41 
42 static char zeros[ZA_SIG_REGS_SIZE(SVE_VQ_MAX)];
43 
do_one_sme_vl(struct tdescr * td,siginfo_t * si,ucontext_t * uc,unsigned int vl)44 static int do_one_sme_vl(struct tdescr *td, siginfo_t *si, ucontext_t *uc,
45 			 unsigned int vl)
46 {
47 	size_t offset;
48 	struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);
49 	struct za_context *za;
50 
51 	fprintf(stderr, "Testing VL %d\n", vl);
52 
53 	if (prctl(PR_SME_SET_VL, vl) != vl) {
54 		fprintf(stderr, "Failed to set VL\n");
55 		return 1;
56 	}
57 
58 	/*
59 	 * Get a signal context which should have a SVE frame and registers
60 	 * in it.
61 	 */
62 	setup_za_regs();
63 	if (!get_current_context(td, &context.uc, sizeof(context)))
64 		return 1;
65 
66 	head = get_header(head, ZA_MAGIC, GET_BUF_RESV_SIZE(context), &offset);
67 	if (!head) {
68 		fprintf(stderr, "No ZA context\n");
69 		return 1;
70 	}
71 
72 	za = (struct za_context *)head;
73 	if (za->vl != vl) {
74 		fprintf(stderr, "Got VL %d, expected %d\n", za->vl, vl);
75 		return 1;
76 	}
77 
78 	if (head->size != ZA_SIG_CONTEXT_SIZE(sve_vq_from_vl(vl))) {
79 		fprintf(stderr, "ZA context size %u, expected %lu\n",
80 			head->size, ZA_SIG_CONTEXT_SIZE(sve_vq_from_vl(vl)));
81 		return 1;
82 	}
83 
84 	fprintf(stderr, "Got expected size %u and VL %d\n",
85 		head->size, za->vl);
86 
87 	/* We didn't load any data into ZA so it should be all zeros */
88 	if (memcmp(zeros, (char *)za + ZA_SIG_REGS_OFFSET,
89 		   ZA_SIG_REGS_SIZE(sve_vq_from_vl(za->vl))) != 0) {
90 		fprintf(stderr, "ZA data invalid\n");
91 		return 1;
92 	}
93 
94 	return 0;
95 }
96 
sme_regs(struct tdescr * td,siginfo_t * si,ucontext_t * uc)97 static int sme_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc)
98 {
99 	int i;
100 
101 	for (i = 0; i < nvls; i++) {
102 		if (do_one_sme_vl(td, si, uc, vls[i]))
103 			return 1;
104 	}
105 
106 	td->pass = 1;
107 
108 	return 0;
109 }
110 
111 struct tdescr tde = {
112 	.name = "ZA register",
113 	.descr = "Check that we get the right ZA registers reported",
114 	.feats_required = FEAT_SME,
115 	.timeout = 3,
116 	.init = sme_get_vls,
117 	.run = sme_regs,
118 };
119