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 111afe6f182SMark Brown bool validate_zt_context(struct zt_context *zt, char **err) 112afe6f182SMark Brown { 113afe6f182SMark Brown if (!zt || !err) 114afe6f182SMark Brown return false; 115afe6f182SMark Brown 116afe6f182SMark Brown /* If the context is present there should be at least one register */ 117afe6f182SMark Brown if (zt->nregs == 0) { 118afe6f182SMark Brown *err = "no registers"; 119afe6f182SMark Brown return false; 120afe6f182SMark Brown } 121afe6f182SMark Brown 122afe6f182SMark Brown /* Size should agree with the number of registers */ 123afe6f182SMark Brown if (zt->head.size != ZT_SIG_CONTEXT_SIZE(zt->nregs)) { 124afe6f182SMark Brown *err = "register count does not match size"; 125afe6f182SMark Brown return false; 126afe6f182SMark Brown } 127afe6f182SMark Brown 128afe6f182SMark Brown return true; 129afe6f182SMark Brown } 130afe6f182SMark 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; 140afe6f182SMark 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; 187bae393daSMark Brown case TPIDR2_MAGIC: 188bae393daSMark Brown if (head->size != sizeof(struct tpidr2_context)) 189bae393daSMark Brown *err = "Bad size for tpidr2_context"; 190bae393daSMark Brown break; 191f96bf434SCristian Marussi case SVE_MAGIC: 192f96bf434SCristian Marussi if (flags & SVE_CTX) 193f96bf434SCristian Marussi *err = "Multiple SVE_MAGIC"; 194ace19b18SMark Brown /* Size is validated in validate_sve_context() */ 195ace19b18SMark Brown sve = (struct sve_context *)head; 1961998c823SMark Brown new_flags |= SVE_CTX; 197f96bf434SCristian Marussi break; 1981a792b54SMark Brown case ZA_MAGIC: 1991a792b54SMark Brown if (flags & ZA_CTX) 2001a792b54SMark Brown *err = "Multiple ZA_MAGIC"; 2011a792b54SMark Brown /* Size is validated in validate_za_context() */ 2021a792b54SMark Brown za = (struct za_context *)head; 2031998c823SMark Brown new_flags |= ZA_CTX; 2041a792b54SMark Brown break; 205afe6f182SMark Brown case ZT_MAGIC: 206afe6f182SMark Brown if (flags & ZT_CTX) 207afe6f182SMark Brown *err = "Multiple ZT_MAGIC"; 208afe6f182SMark Brown /* Size is validated in validate_za_context() */ 209afe6f182SMark Brown zt = (struct zt_context *)head; 210afe6f182SMark Brown new_flags |= ZT_CTX; 211afe6f182SMark Brown break; 212*f4dcccddSMark Brown case FPMR_MAGIC: 213*f4dcccddSMark Brown if (flags & FPMR_CTX) 214*f4dcccddSMark Brown *err = "Multiple FPMR_MAGIC"; 215*f4dcccddSMark Brown else if (head->size != 216*f4dcccddSMark Brown sizeof(struct fpmr_context)) 217*f4dcccddSMark Brown *err = "Bad size for fpmr_context"; 218*f4dcccddSMark Brown new_flags |= FPMR_CTX; 219*f4dcccddSMark Brown break; 220f96bf434SCristian Marussi case EXTRA_MAGIC: 221f96bf434SCristian Marussi if (flags & EXTRA_CTX) 222f96bf434SCristian Marussi *err = "Multiple EXTRA_MAGIC"; 223f96bf434SCristian Marussi else if (head->size != 224f96bf434SCristian Marussi sizeof(struct extra_context)) 225f96bf434SCristian Marussi *err = "Bad size for extra_context"; 2261998c823SMark Brown new_flags |= EXTRA_CTX; 227f96bf434SCristian Marussi extra = (struct extra_context *)head; 228f96bf434SCristian Marussi break; 229f96bf434SCristian Marussi case KSFT_BAD_MAGIC: 230f96bf434SCristian Marussi /* 231f96bf434SCristian Marussi * This is a BAD magic header defined 232f96bf434SCristian Marussi * artificially by a testcase and surely 233f96bf434SCristian Marussi * unknown to the Kernel parse_user_sigframe(). 234f96bf434SCristian Marussi * It MUST cause a Kernel induced SEGV 235f96bf434SCristian Marussi */ 236f96bf434SCristian Marussi *err = "BAD MAGIC !"; 237f96bf434SCristian Marussi break; 238f96bf434SCristian Marussi default: 239f96bf434SCristian Marussi /* 240f96bf434SCristian Marussi * A still unknown Magic: potentially freshly added 241f96bf434SCristian Marussi * to the Kernel code and still unknown to the 2429b283888SMark Brown * tests. Magic numbers are supposed to be allocated 2439b283888SMark Brown * as somewhat meaningful ASCII strings so try to 2449b283888SMark Brown * print as such as well as the raw number. 245f96bf434SCristian Marussi */ 2469b283888SMark Brown memcpy(magic, &head->magic, sizeof(magic)); 2479b283888SMark Brown for (i = 0; i < sizeof(magic); i++) 2489b283888SMark Brown if (!isalnum(magic[i])) 2499b283888SMark Brown magic[i] = '?'; 2509b283888SMark Brown 251f96bf434SCristian Marussi fprintf(stdout, 2529b283888SMark Brown "SKIP Unknown MAGIC: 0x%X (%c%c%c%c) - Is KSFT arm64/signal up to date ?\n", 2539b283888SMark Brown head->magic, 2549b283888SMark Brown magic[3], magic[2], magic[1], magic[0]); 255f96bf434SCristian Marussi break; 256f96bf434SCristian Marussi } 257f96bf434SCristian Marussi 258f96bf434SCristian Marussi if (*err) 259f96bf434SCristian Marussi return false; 260f96bf434SCristian Marussi 261f96bf434SCristian Marussi offs += head->size; 262f96bf434SCristian Marussi if (resv_sz < offs + sizeof(*head)) { 263f96bf434SCristian Marussi *err = "HEAD Overrun"; 264f96bf434SCristian Marussi return false; 265f96bf434SCristian Marussi } 266f96bf434SCristian Marussi 2671998c823SMark Brown if (new_flags & EXTRA_CTX) 268ff89a819SMark Brown if (!validate_extra_context(extra, err, 269ff89a819SMark Brown &extra_data, &extra_sz)) 270f96bf434SCristian Marussi return false; 2711998c823SMark Brown if (new_flags & SVE_CTX) 272ace19b18SMark Brown if (!validate_sve_context(sve, err)) 273ace19b18SMark Brown return false; 2741998c823SMark Brown if (new_flags & ZA_CTX) 2751a792b54SMark Brown if (!validate_za_context(za, err)) 2761a792b54SMark Brown return false; 277afe6f182SMark Brown if (new_flags & ZT_CTX) 278afe6f182SMark Brown if (!validate_zt_context(zt, err)) 279afe6f182SMark Brown return false; 280f96bf434SCristian Marussi 2811998c823SMark Brown flags |= new_flags; 2821998c823SMark Brown 283f96bf434SCristian Marussi head = GET_RESV_NEXT_HEAD(head); 284f96bf434SCristian Marussi } 285f96bf434SCristian Marussi 286f96bf434SCristian Marussi if (terminated && !(flags & FPSIMD_CTX)) { 287f96bf434SCristian Marussi *err = "Missing FPSIMD"; 288f96bf434SCristian Marussi return false; 289f96bf434SCristian Marussi } 290f96bf434SCristian Marussi 291afe6f182SMark Brown if (terminated && (flags & ZT_CTX) && !(flags & ZA_CTX)) { 292afe6f182SMark Brown *err = "ZT context but no ZA context"; 293afe6f182SMark Brown return false; 294afe6f182SMark Brown } 295afe6f182SMark Brown 296f96bf434SCristian Marussi return true; 297f96bf434SCristian Marussi } 2986c2aa428SCristian Marussi 2996c2aa428SCristian Marussi /* 3006c2aa428SCristian Marussi * This function walks through the records inside the provided reserved area 3016c2aa428SCristian Marussi * trying to find enough space to fit @need_sz bytes: if not enough space is 3026c2aa428SCristian Marussi * available and an extra_context record is present, it throws away the 3036c2aa428SCristian Marussi * extra_context record. 3046c2aa428SCristian Marussi * 3056c2aa428SCristian Marussi * It returns a pointer to a new header where it is possible to start storing 3066c2aa428SCristian Marussi * our need_sz bytes. 3076c2aa428SCristian Marussi * 3086c2aa428SCristian Marussi * @shead: points to the start of reserved area 3096c2aa428SCristian Marussi * @need_sz: needed bytes 3106c2aa428SCristian Marussi * @resv_sz: reserved area size in bytes 3116c2aa428SCristian Marussi * @offset: if not null, this will be filled with the offset of the return 3126c2aa428SCristian Marussi * head pointer from @shead 3136c2aa428SCristian Marussi * 3146c2aa428SCristian Marussi * @return: pointer to a new head where to start storing need_sz bytes, or 3156c2aa428SCristian Marussi * NULL if space could not be made available. 3166c2aa428SCristian Marussi */ 3176c2aa428SCristian Marussi struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead, 3186c2aa428SCristian Marussi size_t need_sz, size_t resv_sz, 3196c2aa428SCristian Marussi size_t *offset) 3206c2aa428SCristian Marussi { 3216c2aa428SCristian Marussi size_t offs = 0; 3226c2aa428SCristian Marussi struct _aarch64_ctx *head; 3236c2aa428SCristian Marussi 3246c2aa428SCristian Marussi head = get_terminator(shead, resv_sz, &offs); 3256c2aa428SCristian Marussi /* not found a terminator...no need to update offset if any */ 3266c2aa428SCristian Marussi if (!head) 3276c2aa428SCristian Marussi return head; 3286c2aa428SCristian Marussi if (resv_sz - offs < need_sz) { 3296c2aa428SCristian Marussi fprintf(stderr, "Low on space:%zd. Discarding extra_context.\n", 3306c2aa428SCristian Marussi resv_sz - offs); 3316c2aa428SCristian Marussi head = get_header(shead, EXTRA_MAGIC, resv_sz, &offs); 3326c2aa428SCristian Marussi if (!head || resv_sz - offs < need_sz) { 3336c2aa428SCristian Marussi fprintf(stderr, 3346c2aa428SCristian Marussi "Failed to reclaim space on sigframe.\n"); 3356c2aa428SCristian Marussi return NULL; 3366c2aa428SCristian Marussi } 3376c2aa428SCristian Marussi } 3386c2aa428SCristian Marussi 3396c2aa428SCristian Marussi fprintf(stderr, "Available space:%zd\n", resv_sz - offs); 3406c2aa428SCristian Marussi if (offset) 3416c2aa428SCristian Marussi *offset = offs; 3426c2aa428SCristian Marussi return head; 3436c2aa428SCristian Marussi } 344