1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 ARM Limited */
3 
4 #include <ctype.h>
5 #include <string.h>
6 
7 #include "testcases.h"
8 
9 struct _aarch64_ctx *get_header(struct _aarch64_ctx *head, uint32_t magic,
10 				size_t resv_sz, size_t *offset)
11 {
12 	size_t offs = 0;
13 	struct _aarch64_ctx *found = NULL;
14 
15 	if (!head || resv_sz < HDR_SZ)
16 		return found;
17 
18 	while (offs <= resv_sz - HDR_SZ &&
19 	       head->magic != magic && head->magic) {
20 		offs += head->size;
21 		head = GET_RESV_NEXT_HEAD(head);
22 	}
23 	if (head->magic == magic) {
24 		found = head;
25 		if (offset)
26 			*offset = offs;
27 	}
28 
29 	return found;
30 }
31 
32 bool validate_extra_context(struct extra_context *extra, char **err,
33 			    void **extra_data, size_t *extra_size)
34 {
35 	struct _aarch64_ctx *term;
36 
37 	if (!extra || !err)
38 		return false;
39 
40 	fprintf(stderr, "Validating EXTRA...\n");
41 	term = GET_RESV_NEXT_HEAD(&extra->head);
42 	if (!term || term->magic || term->size) {
43 		*err = "Missing terminator after EXTRA context";
44 		return false;
45 	}
46 	if (extra->datap & 0x0fUL)
47 		*err = "Extra DATAP misaligned";
48 	else if (extra->size & 0x0fUL)
49 		*err = "Extra SIZE misaligned";
50 	else if (extra->datap != (uint64_t)term + 0x10UL)
51 		*err = "Extra DATAP misplaced (not contiguous)";
52 	if (*err)
53 		return false;
54 
55 	*extra_data = (void *)extra->datap;
56 	*extra_size = extra->size;
57 
58 	return true;
59 }
60 
61 bool validate_sve_context(struct sve_context *sve, char **err)
62 {
63 	/* Size will be rounded up to a multiple of 16 bytes */
64 	size_t regs_size
65 		= ((SVE_SIG_CONTEXT_SIZE(sve_vq_from_vl(sve->vl)) + 15) / 16) * 16;
66 
67 	if (!sve || !err)
68 		return false;
69 
70 	/* Either a bare sve_context or a sve_context followed by regs data */
71 	if ((sve->head.size != sizeof(struct sve_context)) &&
72 	    (sve->head.size != regs_size)) {
73 		*err = "bad size for SVE context";
74 		return false;
75 	}
76 
77 	if (!sve_vl_valid(sve->vl)) {
78 		*err = "SVE VL invalid";
79 
80 		return false;
81 	}
82 
83 	return true;
84 }
85 
86 bool validate_za_context(struct za_context *za, char **err)
87 {
88 	/* Size will be rounded up to a multiple of 16 bytes */
89 	size_t regs_size
90 		= ((ZA_SIG_CONTEXT_SIZE(sve_vq_from_vl(za->vl)) + 15) / 16) * 16;
91 
92 	if (!za || !err)
93 		return false;
94 
95 	/* Either a bare za_context or a za_context followed by regs data */
96 	if ((za->head.size != sizeof(struct za_context)) &&
97 	    (za->head.size != regs_size)) {
98 		*err = "bad size for ZA context";
99 		return false;
100 	}
101 
102 	if (!sve_vl_valid(za->vl)) {
103 		*err = "SME VL in ZA context invalid";
104 
105 		return false;
106 	}
107 
108 	return true;
109 }
110 
111 bool validate_zt_context(struct zt_context *zt, char **err)
112 {
113 	if (!zt || !err)
114 		return false;
115 
116 	/* If the context is present there should be at least one register */
117 	if (zt->nregs == 0) {
118 		*err = "no registers";
119 		return false;
120 	}
121 
122 	/* Size should agree with the number of registers */
123 	if (zt->head.size != ZT_SIG_CONTEXT_SIZE(zt->nregs)) {
124 		*err = "register count does not match size";
125 		return false;
126 	}
127 
128 	return true;
129 }
130 
131 bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err)
132 {
133 	bool terminated = false;
134 	size_t offs = 0;
135 	int flags = 0;
136 	int new_flags, i;
137 	struct extra_context *extra = NULL;
138 	struct sve_context *sve = NULL;
139 	struct za_context *za = NULL;
140 	struct zt_context *zt = NULL;
141 	struct _aarch64_ctx *head =
142 		(struct _aarch64_ctx *)uc->uc_mcontext.__reserved;
143 	void *extra_data = NULL;
144 	size_t extra_sz = 0;
145 	char magic[4];
146 
147 	if (!err)
148 		return false;
149 	/* Walk till the end terminator verifying __reserved contents */
150 	while (head && !terminated && offs < resv_sz) {
151 		if ((uint64_t)head & 0x0fUL) {
152 			*err = "Misaligned HEAD";
153 			return false;
154 		}
155 
156 		new_flags = 0;
157 
158 		switch (head->magic) {
159 		case 0:
160 			if (head->size) {
161 				*err = "Bad size for terminator";
162 			} else if (extra_data) {
163 				/* End of main data, walking the extra data */
164 				head = extra_data;
165 				resv_sz = extra_sz;
166 				offs = 0;
167 
168 				extra_data = NULL;
169 				extra_sz = 0;
170 				continue;
171 			} else {
172 				terminated = true;
173 			}
174 			break;
175 		case FPSIMD_MAGIC:
176 			if (flags & FPSIMD_CTX)
177 				*err = "Multiple FPSIMD_MAGIC";
178 			else if (head->size !=
179 				 sizeof(struct fpsimd_context))
180 				*err = "Bad size for fpsimd_context";
181 			new_flags |= FPSIMD_CTX;
182 			break;
183 		case ESR_MAGIC:
184 			if (head->size != sizeof(struct esr_context))
185 				*err = "Bad size for esr_context";
186 			break;
187 		case SVE_MAGIC:
188 			if (flags & SVE_CTX)
189 				*err = "Multiple SVE_MAGIC";
190 			/* Size is validated in validate_sve_context() */
191 			sve = (struct sve_context *)head;
192 			new_flags |= SVE_CTX;
193 			break;
194 		case ZA_MAGIC:
195 			if (flags & ZA_CTX)
196 				*err = "Multiple ZA_MAGIC";
197 			/* Size is validated in validate_za_context() */
198 			za = (struct za_context *)head;
199 			new_flags |= ZA_CTX;
200 			break;
201 		case ZT_MAGIC:
202 			if (flags & ZT_CTX)
203 				*err = "Multiple ZT_MAGIC";
204 			/* Size is validated in validate_za_context() */
205 			zt = (struct zt_context *)head;
206 			new_flags |= ZT_CTX;
207 			break;
208 		case EXTRA_MAGIC:
209 			if (flags & EXTRA_CTX)
210 				*err = "Multiple EXTRA_MAGIC";
211 			else if (head->size !=
212 				 sizeof(struct extra_context))
213 				*err = "Bad size for extra_context";
214 			new_flags |= EXTRA_CTX;
215 			extra = (struct extra_context *)head;
216 			break;
217 		case KSFT_BAD_MAGIC:
218 			/*
219 			 * This is a BAD magic header defined
220 			 * artificially by a testcase and surely
221 			 * unknown to the Kernel parse_user_sigframe().
222 			 * It MUST cause a Kernel induced SEGV
223 			 */
224 			*err = "BAD MAGIC !";
225 			break;
226 		default:
227 			/*
228 			 * A still unknown Magic: potentially freshly added
229 			 * to the Kernel code and still unknown to the
230 			 * tests.  Magic numbers are supposed to be allocated
231 			 * as somewhat meaningful ASCII strings so try to
232 			 * print as such as well as the raw number.
233 			 */
234 			memcpy(magic, &head->magic, sizeof(magic));
235 			for (i = 0; i < sizeof(magic); i++)
236 				if (!isalnum(magic[i]))
237 					magic[i] = '?';
238 
239 			fprintf(stdout,
240 				"SKIP Unknown MAGIC: 0x%X (%c%c%c%c) - Is KSFT arm64/signal up to date ?\n",
241 				head->magic,
242 				magic[3], magic[2], magic[1], magic[0]);
243 			break;
244 		}
245 
246 		if (*err)
247 			return false;
248 
249 		offs += head->size;
250 		if (resv_sz < offs + sizeof(*head)) {
251 			*err = "HEAD Overrun";
252 			return false;
253 		}
254 
255 		if (new_flags & EXTRA_CTX)
256 			if (!validate_extra_context(extra, err,
257 						    &extra_data, &extra_sz))
258 				return false;
259 		if (new_flags & SVE_CTX)
260 			if (!validate_sve_context(sve, err))
261 				return false;
262 		if (new_flags & ZA_CTX)
263 			if (!validate_za_context(za, err))
264 				return false;
265 		if (new_flags & ZT_CTX)
266 			if (!validate_zt_context(zt, err))
267 				return false;
268 
269 		flags |= new_flags;
270 
271 		head = GET_RESV_NEXT_HEAD(head);
272 	}
273 
274 	if (terminated && !(flags & FPSIMD_CTX)) {
275 		*err = "Missing FPSIMD";
276 		return false;
277 	}
278 
279 	if (terminated && (flags & ZT_CTX) && !(flags & ZA_CTX)) {
280 		*err = "ZT context but no ZA context";
281 		return false;
282 	}
283 
284 	return true;
285 }
286 
287 /*
288  * This function walks through the records inside the provided reserved area
289  * trying to find enough space to fit @need_sz bytes: if not enough space is
290  * available and an extra_context record is present, it throws away the
291  * extra_context record.
292  *
293  * It returns a pointer to a new header where it is possible to start storing
294  * our need_sz bytes.
295  *
296  * @shead: points to the start of reserved area
297  * @need_sz: needed bytes
298  * @resv_sz: reserved area size in bytes
299  * @offset: if not null, this will be filled with the offset of the return
300  *	    head pointer from @shead
301  *
302  * @return: pointer to a new head where to start storing need_sz bytes, or
303  *	    NULL if space could not be made available.
304  */
305 struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead,
306 				       size_t need_sz, size_t resv_sz,
307 				       size_t *offset)
308 {
309 	size_t offs = 0;
310 	struct _aarch64_ctx *head;
311 
312 	head = get_terminator(shead, resv_sz, &offs);
313 	/* not found a terminator...no need to update offset if any */
314 	if (!head)
315 		return head;
316 	if (resv_sz - offs < need_sz) {
317 		fprintf(stderr, "Low on space:%zd. Discarding extra_context.\n",
318 			resv_sz - offs);
319 		head = get_header(shead, EXTRA_MAGIC, resv_sz, &offs);
320 		if (!head || resv_sz - offs < need_sz) {
321 			fprintf(stderr,
322 				"Failed to reclaim space on sigframe.\n");
323 			return NULL;
324 		}
325 	}
326 
327 	fprintf(stderr, "Available space:%zd\n", resv_sz - offs);
328 	if (offset)
329 		*offset = offs;
330 	return head;
331 }
332