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_reserved(ucontext_t *uc, size_t resv_sz, char **err)
112 {
113 	bool terminated = false;
114 	size_t offs = 0;
115 	int flags = 0;
116 	int new_flags, i;
117 	struct extra_context *extra = NULL;
118 	struct sve_context *sve = NULL;
119 	struct za_context *za = NULL;
120 	struct _aarch64_ctx *head =
121 		(struct _aarch64_ctx *)uc->uc_mcontext.__reserved;
122 	void *extra_data = NULL;
123 	size_t extra_sz = 0;
124 	char magic[4];
125 
126 	if (!err)
127 		return false;
128 	/* Walk till the end terminator verifying __reserved contents */
129 	while (head && !terminated && offs < resv_sz) {
130 		if ((uint64_t)head & 0x0fUL) {
131 			*err = "Misaligned HEAD";
132 			return false;
133 		}
134 
135 		new_flags = 0;
136 
137 		switch (head->magic) {
138 		case 0:
139 			if (head->size) {
140 				*err = "Bad size for terminator";
141 			} else if (extra_data) {
142 				/* End of main data, walking the extra data */
143 				head = extra_data;
144 				resv_sz = extra_sz;
145 				offs = 0;
146 
147 				extra_data = NULL;
148 				extra_sz = 0;
149 				continue;
150 			} else {
151 				terminated = true;
152 			}
153 			break;
154 		case FPSIMD_MAGIC:
155 			if (flags & FPSIMD_CTX)
156 				*err = "Multiple FPSIMD_MAGIC";
157 			else if (head->size !=
158 				 sizeof(struct fpsimd_context))
159 				*err = "Bad size for fpsimd_context";
160 			new_flags |= FPSIMD_CTX;
161 			break;
162 		case ESR_MAGIC:
163 			if (head->size != sizeof(struct esr_context))
164 				*err = "Bad size for esr_context";
165 			break;
166 		case SVE_MAGIC:
167 			if (flags & SVE_CTX)
168 				*err = "Multiple SVE_MAGIC";
169 			/* Size is validated in validate_sve_context() */
170 			sve = (struct sve_context *)head;
171 			new_flags |= SVE_CTX;
172 			break;
173 		case ZA_MAGIC:
174 			if (flags & ZA_CTX)
175 				*err = "Multiple ZA_MAGIC";
176 			/* Size is validated in validate_za_context() */
177 			za = (struct za_context *)head;
178 			new_flags |= ZA_CTX;
179 			break;
180 		case EXTRA_MAGIC:
181 			if (flags & EXTRA_CTX)
182 				*err = "Multiple EXTRA_MAGIC";
183 			else if (head->size !=
184 				 sizeof(struct extra_context))
185 				*err = "Bad size for extra_context";
186 			new_flags |= EXTRA_CTX;
187 			extra = (struct extra_context *)head;
188 			break;
189 		case KSFT_BAD_MAGIC:
190 			/*
191 			 * This is a BAD magic header defined
192 			 * artificially by a testcase and surely
193 			 * unknown to the Kernel parse_user_sigframe().
194 			 * It MUST cause a Kernel induced SEGV
195 			 */
196 			*err = "BAD MAGIC !";
197 			break;
198 		default:
199 			/*
200 			 * A still unknown Magic: potentially freshly added
201 			 * to the Kernel code and still unknown to the
202 			 * tests.  Magic numbers are supposed to be allocated
203 			 * as somewhat meaningful ASCII strings so try to
204 			 * print as such as well as the raw number.
205 			 */
206 			memcpy(magic, &head->magic, sizeof(magic));
207 			for (i = 0; i < sizeof(magic); i++)
208 				if (!isalnum(magic[i]))
209 					magic[i] = '?';
210 
211 			fprintf(stdout,
212 				"SKIP Unknown MAGIC: 0x%X (%c%c%c%c) - Is KSFT arm64/signal up to date ?\n",
213 				head->magic,
214 				magic[3], magic[2], magic[1], magic[0]);
215 			break;
216 		}
217 
218 		if (*err)
219 			return false;
220 
221 		offs += head->size;
222 		if (resv_sz < offs + sizeof(*head)) {
223 			*err = "HEAD Overrun";
224 			return false;
225 		}
226 
227 		if (new_flags & EXTRA_CTX)
228 			if (!validate_extra_context(extra, err,
229 						    &extra_data, &extra_sz))
230 				return false;
231 		if (new_flags & SVE_CTX)
232 			if (!validate_sve_context(sve, err))
233 				return false;
234 		if (new_flags & ZA_CTX)
235 			if (!validate_za_context(za, err))
236 				return false;
237 
238 		flags |= new_flags;
239 
240 		head = GET_RESV_NEXT_HEAD(head);
241 	}
242 
243 	if (terminated && !(flags & FPSIMD_CTX)) {
244 		*err = "Missing FPSIMD";
245 		return false;
246 	}
247 
248 	return true;
249 }
250 
251 /*
252  * This function walks through the records inside the provided reserved area
253  * trying to find enough space to fit @need_sz bytes: if not enough space is
254  * available and an extra_context record is present, it throws away the
255  * extra_context record.
256  *
257  * It returns a pointer to a new header where it is possible to start storing
258  * our need_sz bytes.
259  *
260  * @shead: points to the start of reserved area
261  * @need_sz: needed bytes
262  * @resv_sz: reserved area size in bytes
263  * @offset: if not null, this will be filled with the offset of the return
264  *	    head pointer from @shead
265  *
266  * @return: pointer to a new head where to start storing need_sz bytes, or
267  *	    NULL if space could not be made available.
268  */
269 struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead,
270 				       size_t need_sz, size_t resv_sz,
271 				       size_t *offset)
272 {
273 	size_t offs = 0;
274 	struct _aarch64_ctx *head;
275 
276 	head = get_terminator(shead, resv_sz, &offs);
277 	/* not found a terminator...no need to update offset if any */
278 	if (!head)
279 		return head;
280 	if (resv_sz - offs < need_sz) {
281 		fprintf(stderr, "Low on space:%zd. Discarding extra_context.\n",
282 			resv_sz - offs);
283 		head = get_header(shead, EXTRA_MAGIC, resv_sz, &offs);
284 		if (!head || resv_sz - offs < need_sz) {
285 			fprintf(stderr,
286 				"Failed to reclaim space on sigframe.\n");
287 			return NULL;
288 		}
289 	}
290 
291 	fprintf(stderr, "Available space:%zd\n", resv_sz - offs);
292 	if (offset)
293 		*offset = offs;
294 	return head;
295 }
296