1f96bf434SCristian Marussi // SPDX-License-Identifier: GPL-2.0 2f96bf434SCristian Marussi /* Copyright (C) 2019 ARM Limited */ 3f96bf434SCristian Marussi #include "testcases.h" 4f96bf434SCristian Marussi 5f96bf434SCristian Marussi struct _aarch64_ctx *get_header(struct _aarch64_ctx *head, uint32_t magic, 6f96bf434SCristian Marussi size_t resv_sz, size_t *offset) 7f96bf434SCristian Marussi { 8f96bf434SCristian Marussi size_t offs = 0; 9f96bf434SCristian Marussi struct _aarch64_ctx *found = NULL; 10f96bf434SCristian Marussi 11f96bf434SCristian Marussi if (!head || resv_sz < HDR_SZ) 12f96bf434SCristian Marussi return found; 13f96bf434SCristian Marussi 14f96bf434SCristian Marussi while (offs <= resv_sz - HDR_SZ && 15f96bf434SCristian Marussi head->magic != magic && head->magic) { 16f96bf434SCristian Marussi offs += head->size; 17f96bf434SCristian Marussi head = GET_RESV_NEXT_HEAD(head); 18f96bf434SCristian Marussi } 19f96bf434SCristian Marussi if (head->magic == magic) { 20f96bf434SCristian Marussi found = head; 21f96bf434SCristian Marussi if (offset) 22f96bf434SCristian Marussi *offset = offs; 23f96bf434SCristian Marussi } 24f96bf434SCristian Marussi 25f96bf434SCristian Marussi return found; 26f96bf434SCristian Marussi } 27f96bf434SCristian Marussi 28f96bf434SCristian Marussi bool validate_extra_context(struct extra_context *extra, char **err) 29f96bf434SCristian Marussi { 30f96bf434SCristian Marussi struct _aarch64_ctx *term; 31f96bf434SCristian Marussi 32f96bf434SCristian Marussi if (!extra || !err) 33f96bf434SCristian Marussi return false; 34f96bf434SCristian Marussi 35f96bf434SCristian Marussi fprintf(stderr, "Validating EXTRA...\n"); 36f96bf434SCristian Marussi term = GET_RESV_NEXT_HEAD(extra); 37f96bf434SCristian Marussi if (!term || term->magic || term->size) { 38f96bf434SCristian Marussi *err = "Missing terminator after EXTRA context"; 39f96bf434SCristian Marussi return false; 40f96bf434SCristian Marussi } 41f96bf434SCristian Marussi if (extra->datap & 0x0fUL) 42f96bf434SCristian Marussi *err = "Extra DATAP misaligned"; 43f96bf434SCristian Marussi else if (extra->size & 0x0fUL) 44f96bf434SCristian Marussi *err = "Extra SIZE misaligned"; 45f96bf434SCristian Marussi else if (extra->datap != (uint64_t)term + sizeof(*term)) 4632667745SColin Ian King *err = "Extra DATAP misplaced (not contiguous)"; 47f96bf434SCristian Marussi if (*err) 48f96bf434SCristian Marussi return false; 49f96bf434SCristian Marussi 50f96bf434SCristian Marussi return true; 51f96bf434SCristian Marussi } 52f96bf434SCristian Marussi 53ace19b18SMark Brown bool validate_sve_context(struct sve_context *sve, char **err) 54ace19b18SMark Brown { 55ace19b18SMark Brown /* Size will be rounded up to a multiple of 16 bytes */ 56ace19b18SMark Brown size_t regs_size 57ace19b18SMark Brown = ((SVE_SIG_CONTEXT_SIZE(sve_vq_from_vl(sve->vl)) + 15) / 16) * 16; 58ace19b18SMark Brown 59ace19b18SMark Brown if (!sve || !err) 60ace19b18SMark Brown return false; 61ace19b18SMark Brown 62ace19b18SMark Brown /* Either a bare sve_context or a sve_context followed by regs data */ 63ace19b18SMark Brown if ((sve->head.size != sizeof(struct sve_context)) && 64ace19b18SMark Brown (sve->head.size != regs_size)) { 65ace19b18SMark Brown *err = "bad size for SVE context"; 66ace19b18SMark Brown return false; 67ace19b18SMark Brown } 68ace19b18SMark Brown 69ace19b18SMark Brown if (!sve_vl_valid(sve->vl)) { 70ace19b18SMark Brown *err = "SVE VL invalid"; 71ace19b18SMark Brown 72ace19b18SMark Brown return false; 73ace19b18SMark Brown } 74ace19b18SMark Brown 75ace19b18SMark Brown return true; 76ace19b18SMark Brown } 77ace19b18SMark Brown 78*1a792b54SMark Brown bool validate_za_context(struct za_context *za, char **err) 79*1a792b54SMark Brown { 80*1a792b54SMark Brown /* Size will be rounded up to a multiple of 16 bytes */ 81*1a792b54SMark Brown size_t regs_size 82*1a792b54SMark Brown = ((ZA_SIG_CONTEXT_SIZE(sve_vq_from_vl(za->vl)) + 15) / 16) * 16; 83*1a792b54SMark Brown 84*1a792b54SMark Brown if (!za || !err) 85*1a792b54SMark Brown return false; 86*1a792b54SMark Brown 87*1a792b54SMark Brown /* Either a bare za_context or a za_context followed by regs data */ 88*1a792b54SMark Brown if ((za->head.size != sizeof(struct za_context)) && 89*1a792b54SMark Brown (za->head.size != regs_size)) { 90*1a792b54SMark Brown *err = "bad size for ZA context"; 91*1a792b54SMark Brown return false; 92*1a792b54SMark Brown } 93*1a792b54SMark Brown 94*1a792b54SMark Brown if (!sve_vl_valid(za->vl)) { 95*1a792b54SMark Brown *err = "SME VL in ZA context invalid"; 96*1a792b54SMark Brown 97*1a792b54SMark Brown return false; 98*1a792b54SMark Brown } 99*1a792b54SMark Brown 100*1a792b54SMark Brown return true; 101*1a792b54SMark Brown } 102*1a792b54SMark Brown 103f96bf434SCristian Marussi bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err) 104f96bf434SCristian Marussi { 105f96bf434SCristian Marussi bool terminated = false; 106f96bf434SCristian Marussi size_t offs = 0; 107f96bf434SCristian Marussi int flags = 0; 108f96bf434SCristian Marussi struct extra_context *extra = NULL; 109ace19b18SMark Brown struct sve_context *sve = NULL; 110*1a792b54SMark Brown struct za_context *za = NULL; 111f96bf434SCristian Marussi struct _aarch64_ctx *head = 112f96bf434SCristian Marussi (struct _aarch64_ctx *)uc->uc_mcontext.__reserved; 113f96bf434SCristian Marussi 114f96bf434SCristian Marussi if (!err) 115f96bf434SCristian Marussi return false; 116f96bf434SCristian Marussi /* Walk till the end terminator verifying __reserved contents */ 117f96bf434SCristian Marussi while (head && !terminated && offs < resv_sz) { 118f96bf434SCristian Marussi if ((uint64_t)head & 0x0fUL) { 119f96bf434SCristian Marussi *err = "Misaligned HEAD"; 120f96bf434SCristian Marussi return false; 121f96bf434SCristian Marussi } 122f96bf434SCristian Marussi 123f96bf434SCristian Marussi switch (head->magic) { 124f96bf434SCristian Marussi case 0: 125f96bf434SCristian Marussi if (head->size) 126f96bf434SCristian Marussi *err = "Bad size for terminator"; 127f96bf434SCristian Marussi else 128f96bf434SCristian Marussi terminated = true; 129f96bf434SCristian Marussi break; 130f96bf434SCristian Marussi case FPSIMD_MAGIC: 131f96bf434SCristian Marussi if (flags & FPSIMD_CTX) 132f96bf434SCristian Marussi *err = "Multiple FPSIMD_MAGIC"; 133f96bf434SCristian Marussi else if (head->size != 134f96bf434SCristian Marussi sizeof(struct fpsimd_context)) 135f96bf434SCristian Marussi *err = "Bad size for fpsimd_context"; 136f96bf434SCristian Marussi flags |= FPSIMD_CTX; 137f96bf434SCristian Marussi break; 138f96bf434SCristian Marussi case ESR_MAGIC: 139f96bf434SCristian Marussi if (head->size != sizeof(struct esr_context)) 140f96bf434SCristian Marussi *err = "Bad size for esr_context"; 141f96bf434SCristian Marussi break; 142f96bf434SCristian Marussi case SVE_MAGIC: 143f96bf434SCristian Marussi if (flags & SVE_CTX) 144f96bf434SCristian Marussi *err = "Multiple SVE_MAGIC"; 145ace19b18SMark Brown /* Size is validated in validate_sve_context() */ 146ace19b18SMark Brown sve = (struct sve_context *)head; 147f96bf434SCristian Marussi flags |= SVE_CTX; 148f96bf434SCristian Marussi break; 149*1a792b54SMark Brown case ZA_MAGIC: 150*1a792b54SMark Brown if (flags & ZA_CTX) 151*1a792b54SMark Brown *err = "Multiple ZA_MAGIC"; 152*1a792b54SMark Brown /* Size is validated in validate_za_context() */ 153*1a792b54SMark Brown za = (struct za_context *)head; 154*1a792b54SMark Brown flags |= ZA_CTX; 155*1a792b54SMark Brown break; 156f96bf434SCristian Marussi case EXTRA_MAGIC: 157f96bf434SCristian Marussi if (flags & EXTRA_CTX) 158f96bf434SCristian Marussi *err = "Multiple EXTRA_MAGIC"; 159f96bf434SCristian Marussi else if (head->size != 160f96bf434SCristian Marussi sizeof(struct extra_context)) 161f96bf434SCristian Marussi *err = "Bad size for extra_context"; 162f96bf434SCristian Marussi flags |= EXTRA_CTX; 163f96bf434SCristian Marussi extra = (struct extra_context *)head; 164f96bf434SCristian Marussi break; 165f96bf434SCristian Marussi case KSFT_BAD_MAGIC: 166f96bf434SCristian Marussi /* 167f96bf434SCristian Marussi * This is a BAD magic header defined 168f96bf434SCristian Marussi * artificially by a testcase and surely 169f96bf434SCristian Marussi * unknown to the Kernel parse_user_sigframe(). 170f96bf434SCristian Marussi * It MUST cause a Kernel induced SEGV 171f96bf434SCristian Marussi */ 172f96bf434SCristian Marussi *err = "BAD MAGIC !"; 173f96bf434SCristian Marussi break; 174f96bf434SCristian Marussi default: 175f96bf434SCristian Marussi /* 176f96bf434SCristian Marussi * A still unknown Magic: potentially freshly added 177f96bf434SCristian Marussi * to the Kernel code and still unknown to the 178f96bf434SCristian Marussi * tests. 179f96bf434SCristian Marussi */ 180f96bf434SCristian Marussi fprintf(stdout, 181f96bf434SCristian Marussi "SKIP Unknown MAGIC: 0x%X - Is KSFT arm64/signal up to date ?\n", 182f96bf434SCristian Marussi head->magic); 183f96bf434SCristian Marussi break; 184f96bf434SCristian Marussi } 185f96bf434SCristian Marussi 186f96bf434SCristian Marussi if (*err) 187f96bf434SCristian Marussi return false; 188f96bf434SCristian Marussi 189f96bf434SCristian Marussi offs += head->size; 190f96bf434SCristian Marussi if (resv_sz < offs + sizeof(*head)) { 191f96bf434SCristian Marussi *err = "HEAD Overrun"; 192f96bf434SCristian Marussi return false; 193f96bf434SCristian Marussi } 194f96bf434SCristian Marussi 195f96bf434SCristian Marussi if (flags & EXTRA_CTX) 196f96bf434SCristian Marussi if (!validate_extra_context(extra, err)) 197f96bf434SCristian Marussi return false; 198ace19b18SMark Brown if (flags & SVE_CTX) 199ace19b18SMark Brown if (!validate_sve_context(sve, err)) 200ace19b18SMark Brown return false; 201*1a792b54SMark Brown if (flags & ZA_CTX) 202*1a792b54SMark Brown if (!validate_za_context(za, err)) 203*1a792b54SMark Brown return false; 204f96bf434SCristian Marussi 205f96bf434SCristian Marussi head = GET_RESV_NEXT_HEAD(head); 206f96bf434SCristian Marussi } 207f96bf434SCristian Marussi 208f96bf434SCristian Marussi if (terminated && !(flags & FPSIMD_CTX)) { 209f96bf434SCristian Marussi *err = "Missing FPSIMD"; 210f96bf434SCristian Marussi return false; 211f96bf434SCristian Marussi } 212f96bf434SCristian Marussi 213f96bf434SCristian Marussi return true; 214f96bf434SCristian Marussi } 2156c2aa428SCristian Marussi 2166c2aa428SCristian Marussi /* 2176c2aa428SCristian Marussi * This function walks through the records inside the provided reserved area 2186c2aa428SCristian Marussi * trying to find enough space to fit @need_sz bytes: if not enough space is 2196c2aa428SCristian Marussi * available and an extra_context record is present, it throws away the 2206c2aa428SCristian Marussi * extra_context record. 2216c2aa428SCristian Marussi * 2226c2aa428SCristian Marussi * It returns a pointer to a new header where it is possible to start storing 2236c2aa428SCristian Marussi * our need_sz bytes. 2246c2aa428SCristian Marussi * 2256c2aa428SCristian Marussi * @shead: points to the start of reserved area 2266c2aa428SCristian Marussi * @need_sz: needed bytes 2276c2aa428SCristian Marussi * @resv_sz: reserved area size in bytes 2286c2aa428SCristian Marussi * @offset: if not null, this will be filled with the offset of the return 2296c2aa428SCristian Marussi * head pointer from @shead 2306c2aa428SCristian Marussi * 2316c2aa428SCristian Marussi * @return: pointer to a new head where to start storing need_sz bytes, or 2326c2aa428SCristian Marussi * NULL if space could not be made available. 2336c2aa428SCristian Marussi */ 2346c2aa428SCristian Marussi struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead, 2356c2aa428SCristian Marussi size_t need_sz, size_t resv_sz, 2366c2aa428SCristian Marussi size_t *offset) 2376c2aa428SCristian Marussi { 2386c2aa428SCristian Marussi size_t offs = 0; 2396c2aa428SCristian Marussi struct _aarch64_ctx *head; 2406c2aa428SCristian Marussi 2416c2aa428SCristian Marussi head = get_terminator(shead, resv_sz, &offs); 2426c2aa428SCristian Marussi /* not found a terminator...no need to update offset if any */ 2436c2aa428SCristian Marussi if (!head) 2446c2aa428SCristian Marussi return head; 2456c2aa428SCristian Marussi if (resv_sz - offs < need_sz) { 2466c2aa428SCristian Marussi fprintf(stderr, "Low on space:%zd. Discarding extra_context.\n", 2476c2aa428SCristian Marussi resv_sz - offs); 2486c2aa428SCristian Marussi head = get_header(shead, EXTRA_MAGIC, resv_sz, &offs); 2496c2aa428SCristian Marussi if (!head || resv_sz - offs < need_sz) { 2506c2aa428SCristian Marussi fprintf(stderr, 2516c2aa428SCristian Marussi "Failed to reclaim space on sigframe.\n"); 2526c2aa428SCristian Marussi return NULL; 2536c2aa428SCristian Marussi } 2546c2aa428SCristian Marussi } 2556c2aa428SCristian Marussi 2566c2aa428SCristian Marussi fprintf(stderr, "Available space:%zd\n", resv_sz - offs); 2576c2aa428SCristian Marussi if (offset) 2586c2aa428SCristian Marussi *offset = offs; 2596c2aa428SCristian Marussi return head; 2606c2aa428SCristian Marussi } 261