xref: /linux/tools/testing/selftests/arm64/signal/testcases/testcases.c (revision afe6f182752625cadf4ff97613bd2f362383bcbf)
1f96bf434SCristian Marussi // SPDX-License-Identifier: GPL-2.0
2f96bf434SCristian Marussi /* Copyright (C) 2019 ARM Limited */
39b283888SMark Brown 
49b283888SMark Brown #include <ctype.h>
59b283888SMark Brown #include <string.h>
69b283888SMark Brown 
7f96bf434SCristian Marussi #include "testcases.h"
8f96bf434SCristian Marussi 
9f96bf434SCristian Marussi struct _aarch64_ctx *get_header(struct _aarch64_ctx *head, uint32_t magic,
10f96bf434SCristian Marussi 				size_t resv_sz, size_t *offset)
11f96bf434SCristian Marussi {
12f96bf434SCristian Marussi 	size_t offs = 0;
13f96bf434SCristian Marussi 	struct _aarch64_ctx *found = NULL;
14f96bf434SCristian Marussi 
15f96bf434SCristian Marussi 	if (!head || resv_sz < HDR_SZ)
16f96bf434SCristian Marussi 		return found;
17f96bf434SCristian Marussi 
18f96bf434SCristian Marussi 	while (offs <= resv_sz - HDR_SZ &&
19f96bf434SCristian Marussi 	       head->magic != magic && head->magic) {
20f96bf434SCristian Marussi 		offs += head->size;
21f96bf434SCristian Marussi 		head = GET_RESV_NEXT_HEAD(head);
22f96bf434SCristian Marussi 	}
23f96bf434SCristian Marussi 	if (head->magic == magic) {
24f96bf434SCristian Marussi 		found = head;
25f96bf434SCristian Marussi 		if (offset)
26f96bf434SCristian Marussi 			*offset = offs;
27f96bf434SCristian Marussi 	}
28f96bf434SCristian Marussi 
29f96bf434SCristian Marussi 	return found;
30f96bf434SCristian Marussi }
31f96bf434SCristian Marussi 
32ff89a819SMark Brown bool validate_extra_context(struct extra_context *extra, char **err,
33ff89a819SMark Brown 			    void **extra_data, size_t *extra_size)
34f96bf434SCristian Marussi {
35f96bf434SCristian Marussi 	struct _aarch64_ctx *term;
36f96bf434SCristian Marussi 
37f96bf434SCristian Marussi 	if (!extra || !err)
38f96bf434SCristian Marussi 		return false;
39f96bf434SCristian Marussi 
40f96bf434SCristian Marussi 	fprintf(stderr, "Validating EXTRA...\n");
415c152c2fSMark Brown 	term = GET_RESV_NEXT_HEAD(&extra->head);
42f96bf434SCristian Marussi 	if (!term || term->magic || term->size) {
43f96bf434SCristian Marussi 		*err = "Missing terminator after EXTRA context";
44f96bf434SCristian Marussi 		return false;
45f96bf434SCristian Marussi 	}
46f96bf434SCristian Marussi 	if (extra->datap & 0x0fUL)
47f96bf434SCristian Marussi 		*err = "Extra DATAP misaligned";
48f96bf434SCristian Marussi 	else if (extra->size & 0x0fUL)
49f96bf434SCristian Marussi 		*err = "Extra SIZE misaligned";
50b721c823SMark Brown 	else if (extra->datap != (uint64_t)term + 0x10UL)
5132667745SColin Ian King 		*err = "Extra DATAP misplaced (not contiguous)";
52f96bf434SCristian Marussi 	if (*err)
53f96bf434SCristian Marussi 		return false;
54f96bf434SCristian Marussi 
55ff89a819SMark Brown 	*extra_data = (void *)extra->datap;
56ff89a819SMark Brown 	*extra_size = extra->size;
57ff89a819SMark Brown 
58f96bf434SCristian Marussi 	return true;
59f96bf434SCristian Marussi }
60f96bf434SCristian Marussi 
61ace19b18SMark Brown bool validate_sve_context(struct sve_context *sve, char **err)
62ace19b18SMark Brown {
63ace19b18SMark Brown 	/* Size will be rounded up to a multiple of 16 bytes */
64ace19b18SMark Brown 	size_t regs_size
65ace19b18SMark Brown 		= ((SVE_SIG_CONTEXT_SIZE(sve_vq_from_vl(sve->vl)) + 15) / 16) * 16;
66ace19b18SMark Brown 
67ace19b18SMark Brown 	if (!sve || !err)
68ace19b18SMark Brown 		return false;
69ace19b18SMark Brown 
70ace19b18SMark Brown 	/* Either a bare sve_context or a sve_context followed by regs data */
71ace19b18SMark Brown 	if ((sve->head.size != sizeof(struct sve_context)) &&
72ace19b18SMark Brown 	    (sve->head.size != regs_size)) {
73ace19b18SMark Brown 		*err = "bad size for SVE context";
74ace19b18SMark Brown 		return false;
75ace19b18SMark Brown 	}
76ace19b18SMark Brown 
77ace19b18SMark Brown 	if (!sve_vl_valid(sve->vl)) {
78ace19b18SMark Brown 		*err = "SVE VL invalid";
79ace19b18SMark Brown 
80ace19b18SMark Brown 		return false;
81ace19b18SMark Brown 	}
82ace19b18SMark Brown 
83ace19b18SMark Brown 	return true;
84ace19b18SMark Brown }
85ace19b18SMark Brown 
861a792b54SMark Brown bool validate_za_context(struct za_context *za, char **err)
871a792b54SMark Brown {
881a792b54SMark Brown 	/* Size will be rounded up to a multiple of 16 bytes */
891a792b54SMark Brown 	size_t regs_size
901a792b54SMark Brown 		= ((ZA_SIG_CONTEXT_SIZE(sve_vq_from_vl(za->vl)) + 15) / 16) * 16;
911a792b54SMark Brown 
921a792b54SMark Brown 	if (!za || !err)
931a792b54SMark Brown 		return false;
941a792b54SMark Brown 
951a792b54SMark Brown 	/* Either a bare za_context or a za_context followed by regs data */
961a792b54SMark Brown 	if ((za->head.size != sizeof(struct za_context)) &&
971a792b54SMark Brown 	    (za->head.size != regs_size)) {
981a792b54SMark Brown 		*err = "bad size for ZA context";
991a792b54SMark Brown 		return false;
1001a792b54SMark Brown 	}
1011a792b54SMark Brown 
1021a792b54SMark Brown 	if (!sve_vl_valid(za->vl)) {
1031a792b54SMark Brown 		*err = "SME VL in ZA context invalid";
1041a792b54SMark Brown 
1051a792b54SMark Brown 		return false;
1061a792b54SMark Brown 	}
1071a792b54SMark Brown 
1081a792b54SMark Brown 	return true;
1091a792b54SMark Brown }
1101a792b54SMark Brown 
111*afe6f182SMark Brown bool validate_zt_context(struct zt_context *zt, char **err)
112*afe6f182SMark Brown {
113*afe6f182SMark Brown 	if (!zt || !err)
114*afe6f182SMark Brown 		return false;
115*afe6f182SMark Brown 
116*afe6f182SMark Brown 	/* If the context is present there should be at least one register */
117*afe6f182SMark Brown 	if (zt->nregs == 0) {
118*afe6f182SMark Brown 		*err = "no registers";
119*afe6f182SMark Brown 		return false;
120*afe6f182SMark Brown 	}
121*afe6f182SMark Brown 
122*afe6f182SMark Brown 	/* Size should agree with the number of registers */
123*afe6f182SMark Brown 	if (zt->head.size != ZT_SIG_CONTEXT_SIZE(zt->nregs)) {
124*afe6f182SMark Brown 		*err = "register count does not match size";
125*afe6f182SMark Brown 		return false;
126*afe6f182SMark Brown 	}
127*afe6f182SMark Brown 
128*afe6f182SMark Brown 	return true;
129*afe6f182SMark Brown }
130*afe6f182SMark Brown 
131f96bf434SCristian Marussi bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err)
132f96bf434SCristian Marussi {
133f96bf434SCristian Marussi 	bool terminated = false;
134f96bf434SCristian Marussi 	size_t offs = 0;
135f96bf434SCristian Marussi 	int flags = 0;
1369b283888SMark Brown 	int new_flags, i;
137f96bf434SCristian Marussi 	struct extra_context *extra = NULL;
138ace19b18SMark Brown 	struct sve_context *sve = NULL;
1391a792b54SMark Brown 	struct za_context *za = NULL;
140*afe6f182SMark Brown 	struct zt_context *zt = NULL;
141f96bf434SCristian Marussi 	struct _aarch64_ctx *head =
142f96bf434SCristian Marussi 		(struct _aarch64_ctx *)uc->uc_mcontext.__reserved;
143ff89a819SMark Brown 	void *extra_data = NULL;
144ff89a819SMark Brown 	size_t extra_sz = 0;
1459b283888SMark Brown 	char magic[4];
146f96bf434SCristian Marussi 
147f96bf434SCristian Marussi 	if (!err)
148f96bf434SCristian Marussi 		return false;
149f96bf434SCristian Marussi 	/* Walk till the end terminator verifying __reserved contents */
150f96bf434SCristian Marussi 	while (head && !terminated && offs < resv_sz) {
151f96bf434SCristian Marussi 		if ((uint64_t)head & 0x0fUL) {
152f96bf434SCristian Marussi 			*err = "Misaligned HEAD";
153f96bf434SCristian Marussi 			return false;
154f96bf434SCristian Marussi 		}
155f96bf434SCristian Marussi 
1561998c823SMark Brown 		new_flags = 0;
1571998c823SMark Brown 
158f96bf434SCristian Marussi 		switch (head->magic) {
159f96bf434SCristian Marussi 		case 0:
160ff89a819SMark Brown 			if (head->size) {
161f96bf434SCristian Marussi 				*err = "Bad size for terminator";
162ff89a819SMark Brown 			} else if (extra_data) {
163ff89a819SMark Brown 				/* End of main data, walking the extra data */
164ff89a819SMark Brown 				head = extra_data;
165ff89a819SMark Brown 				resv_sz = extra_sz;
166ff89a819SMark Brown 				offs = 0;
167ff89a819SMark Brown 
168ff89a819SMark Brown 				extra_data = NULL;
169ff89a819SMark Brown 				extra_sz = 0;
170ff89a819SMark Brown 				continue;
171ff89a819SMark Brown 			} else {
172f96bf434SCristian Marussi 				terminated = true;
173ff89a819SMark Brown 			}
174f96bf434SCristian Marussi 			break;
175f96bf434SCristian Marussi 		case FPSIMD_MAGIC:
176f96bf434SCristian Marussi 			if (flags & FPSIMD_CTX)
177f96bf434SCristian Marussi 				*err = "Multiple FPSIMD_MAGIC";
178f96bf434SCristian Marussi 			else if (head->size !=
179f96bf434SCristian Marussi 				 sizeof(struct fpsimd_context))
180f96bf434SCristian Marussi 				*err = "Bad size for fpsimd_context";
1811998c823SMark Brown 			new_flags |= FPSIMD_CTX;
182f96bf434SCristian Marussi 			break;
183f96bf434SCristian Marussi 		case ESR_MAGIC:
184f96bf434SCristian Marussi 			if (head->size != sizeof(struct esr_context))
185f96bf434SCristian Marussi 				*err = "Bad size for esr_context";
186f96bf434SCristian Marussi 			break;
187f96bf434SCristian Marussi 		case SVE_MAGIC:
188f96bf434SCristian Marussi 			if (flags & SVE_CTX)
189f96bf434SCristian Marussi 				*err = "Multiple SVE_MAGIC";
190ace19b18SMark Brown 			/* Size is validated in validate_sve_context() */
191ace19b18SMark Brown 			sve = (struct sve_context *)head;
1921998c823SMark Brown 			new_flags |= SVE_CTX;
193f96bf434SCristian Marussi 			break;
1941a792b54SMark Brown 		case ZA_MAGIC:
1951a792b54SMark Brown 			if (flags & ZA_CTX)
1961a792b54SMark Brown 				*err = "Multiple ZA_MAGIC";
1971a792b54SMark Brown 			/* Size is validated in validate_za_context() */
1981a792b54SMark Brown 			za = (struct za_context *)head;
1991998c823SMark Brown 			new_flags |= ZA_CTX;
2001a792b54SMark Brown 			break;
201*afe6f182SMark Brown 		case ZT_MAGIC:
202*afe6f182SMark Brown 			if (flags & ZT_CTX)
203*afe6f182SMark Brown 				*err = "Multiple ZT_MAGIC";
204*afe6f182SMark Brown 			/* Size is validated in validate_za_context() */
205*afe6f182SMark Brown 			zt = (struct zt_context *)head;
206*afe6f182SMark Brown 			new_flags |= ZT_CTX;
207*afe6f182SMark Brown 			break;
208f96bf434SCristian Marussi 		case EXTRA_MAGIC:
209f96bf434SCristian Marussi 			if (flags & EXTRA_CTX)
210f96bf434SCristian Marussi 				*err = "Multiple EXTRA_MAGIC";
211f96bf434SCristian Marussi 			else if (head->size !=
212f96bf434SCristian Marussi 				 sizeof(struct extra_context))
213f96bf434SCristian Marussi 				*err = "Bad size for extra_context";
2141998c823SMark Brown 			new_flags |= EXTRA_CTX;
215f96bf434SCristian Marussi 			extra = (struct extra_context *)head;
216f96bf434SCristian Marussi 			break;
217f96bf434SCristian Marussi 		case KSFT_BAD_MAGIC:
218f96bf434SCristian Marussi 			/*
219f96bf434SCristian Marussi 			 * This is a BAD magic header defined
220f96bf434SCristian Marussi 			 * artificially by a testcase and surely
221f96bf434SCristian Marussi 			 * unknown to the Kernel parse_user_sigframe().
222f96bf434SCristian Marussi 			 * It MUST cause a Kernel induced SEGV
223f96bf434SCristian Marussi 			 */
224f96bf434SCristian Marussi 			*err = "BAD MAGIC !";
225f96bf434SCristian Marussi 			break;
226f96bf434SCristian Marussi 		default:
227f96bf434SCristian Marussi 			/*
228f96bf434SCristian Marussi 			 * A still unknown Magic: potentially freshly added
229f96bf434SCristian Marussi 			 * to the Kernel code and still unknown to the
2309b283888SMark Brown 			 * tests.  Magic numbers are supposed to be allocated
2319b283888SMark Brown 			 * as somewhat meaningful ASCII strings so try to
2329b283888SMark Brown 			 * print as such as well as the raw number.
233f96bf434SCristian Marussi 			 */
2349b283888SMark Brown 			memcpy(magic, &head->magic, sizeof(magic));
2359b283888SMark Brown 			for (i = 0; i < sizeof(magic); i++)
2369b283888SMark Brown 				if (!isalnum(magic[i]))
2379b283888SMark Brown 					magic[i] = '?';
2389b283888SMark Brown 
239f96bf434SCristian Marussi 			fprintf(stdout,
2409b283888SMark Brown 				"SKIP Unknown MAGIC: 0x%X (%c%c%c%c) - Is KSFT arm64/signal up to date ?\n",
2419b283888SMark Brown 				head->magic,
2429b283888SMark Brown 				magic[3], magic[2], magic[1], magic[0]);
243f96bf434SCristian Marussi 			break;
244f96bf434SCristian Marussi 		}
245f96bf434SCristian Marussi 
246f96bf434SCristian Marussi 		if (*err)
247f96bf434SCristian Marussi 			return false;
248f96bf434SCristian Marussi 
249f96bf434SCristian Marussi 		offs += head->size;
250f96bf434SCristian Marussi 		if (resv_sz < offs + sizeof(*head)) {
251f96bf434SCristian Marussi 			*err = "HEAD Overrun";
252f96bf434SCristian Marussi 			return false;
253f96bf434SCristian Marussi 		}
254f96bf434SCristian Marussi 
2551998c823SMark Brown 		if (new_flags & EXTRA_CTX)
256ff89a819SMark Brown 			if (!validate_extra_context(extra, err,
257ff89a819SMark Brown 						    &extra_data, &extra_sz))
258f96bf434SCristian Marussi 				return false;
2591998c823SMark Brown 		if (new_flags & SVE_CTX)
260ace19b18SMark Brown 			if (!validate_sve_context(sve, err))
261ace19b18SMark Brown 				return false;
2621998c823SMark Brown 		if (new_flags & ZA_CTX)
2631a792b54SMark Brown 			if (!validate_za_context(za, err))
2641a792b54SMark Brown 				return false;
265*afe6f182SMark Brown 		if (new_flags & ZT_CTX)
266*afe6f182SMark Brown 			if (!validate_zt_context(zt, err))
267*afe6f182SMark Brown 				return false;
268f96bf434SCristian Marussi 
2691998c823SMark Brown 		flags |= new_flags;
2701998c823SMark Brown 
271f96bf434SCristian Marussi 		head = GET_RESV_NEXT_HEAD(head);
272f96bf434SCristian Marussi 	}
273f96bf434SCristian Marussi 
274f96bf434SCristian Marussi 	if (terminated && !(flags & FPSIMD_CTX)) {
275f96bf434SCristian Marussi 		*err = "Missing FPSIMD";
276f96bf434SCristian Marussi 		return false;
277f96bf434SCristian Marussi 	}
278f96bf434SCristian Marussi 
279*afe6f182SMark Brown 	if (terminated && (flags & ZT_CTX) && !(flags & ZA_CTX)) {
280*afe6f182SMark Brown 		*err = "ZT context but no ZA context";
281*afe6f182SMark Brown 		return false;
282*afe6f182SMark Brown 	}
283*afe6f182SMark Brown 
284f96bf434SCristian Marussi 	return true;
285f96bf434SCristian Marussi }
2866c2aa428SCristian Marussi 
2876c2aa428SCristian Marussi /*
2886c2aa428SCristian Marussi  * This function walks through the records inside the provided reserved area
2896c2aa428SCristian Marussi  * trying to find enough space to fit @need_sz bytes: if not enough space is
2906c2aa428SCristian Marussi  * available and an extra_context record is present, it throws away the
2916c2aa428SCristian Marussi  * extra_context record.
2926c2aa428SCristian Marussi  *
2936c2aa428SCristian Marussi  * It returns a pointer to a new header where it is possible to start storing
2946c2aa428SCristian Marussi  * our need_sz bytes.
2956c2aa428SCristian Marussi  *
2966c2aa428SCristian Marussi  * @shead: points to the start of reserved area
2976c2aa428SCristian Marussi  * @need_sz: needed bytes
2986c2aa428SCristian Marussi  * @resv_sz: reserved area size in bytes
2996c2aa428SCristian Marussi  * @offset: if not null, this will be filled with the offset of the return
3006c2aa428SCristian Marussi  *	    head pointer from @shead
3016c2aa428SCristian Marussi  *
3026c2aa428SCristian Marussi  * @return: pointer to a new head where to start storing need_sz bytes, or
3036c2aa428SCristian Marussi  *	    NULL if space could not be made available.
3046c2aa428SCristian Marussi  */
3056c2aa428SCristian Marussi struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead,
3066c2aa428SCristian Marussi 				       size_t need_sz, size_t resv_sz,
3076c2aa428SCristian Marussi 				       size_t *offset)
3086c2aa428SCristian Marussi {
3096c2aa428SCristian Marussi 	size_t offs = 0;
3106c2aa428SCristian Marussi 	struct _aarch64_ctx *head;
3116c2aa428SCristian Marussi 
3126c2aa428SCristian Marussi 	head = get_terminator(shead, resv_sz, &offs);
3136c2aa428SCristian Marussi 	/* not found a terminator...no need to update offset if any */
3146c2aa428SCristian Marussi 	if (!head)
3156c2aa428SCristian Marussi 		return head;
3166c2aa428SCristian Marussi 	if (resv_sz - offs < need_sz) {
3176c2aa428SCristian Marussi 		fprintf(stderr, "Low on space:%zd. Discarding extra_context.\n",
3186c2aa428SCristian Marussi 			resv_sz - offs);
3196c2aa428SCristian Marussi 		head = get_header(shead, EXTRA_MAGIC, resv_sz, &offs);
3206c2aa428SCristian Marussi 		if (!head || resv_sz - offs < need_sz) {
3216c2aa428SCristian Marussi 			fprintf(stderr,
3226c2aa428SCristian Marussi 				"Failed to reclaim space on sigframe.\n");
3236c2aa428SCristian Marussi 			return NULL;
3246c2aa428SCristian Marussi 		}
3256c2aa428SCristian Marussi 	}
3266c2aa428SCristian Marussi 
3276c2aa428SCristian Marussi 	fprintf(stderr, "Available space:%zd\n", resv_sz - offs);
3286c2aa428SCristian Marussi 	if (offset)
3296c2aa428SCristian Marussi 		*offset = offs;
3306c2aa428SCristian Marussi 	return head;
3316c2aa428SCristian Marussi }
332