1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (C) 2019 ARM Limited */ 3 #ifndef __TESTCASES_H__ 4 #define __TESTCASES_H__ 5 6 #include <stddef.h> 7 #include <stdio.h> 8 #include <stdbool.h> 9 #include <stdint.h> 10 #include <stdlib.h> 11 #include <ucontext.h> 12 #include <signal.h> 13 14 /* Architecture specific sigframe definitions */ 15 #include <asm/sigcontext.h> 16 17 #define FPSIMD_CTX (1 << 0) 18 #define SVE_CTX (1 << 1) 19 #define ZA_CTX (1 << 2) 20 #define EXTRA_CTX (1 << 3) 21 #define ZT_CTX (1 << 4) 22 #define FPMR_CTX (1 << 5) 23 #define GCS_CTX (1 << 6) 24 25 #define KSFT_BAD_MAGIC 0xdeadbeef 26 27 #define HDR_SZ \ 28 sizeof(struct _aarch64_ctx) 29 30 #define GET_UC_RESV_HEAD(uc) \ 31 (struct _aarch64_ctx *)(&(uc->uc_mcontext.__reserved)) 32 33 #define GET_SF_RESV_HEAD(sf) \ 34 (struct _aarch64_ctx *)(&(sf).uc.uc_mcontext.__reserved) 35 36 #define GET_SF_RESV_SIZE(sf) \ 37 sizeof((sf).uc.uc_mcontext.__reserved) 38 39 #define GET_BUF_RESV_HEAD(buf) \ 40 (struct _aarch64_ctx *)(&(buf).uc.uc_mcontext.__reserved) 41 42 #define GET_BUF_RESV_SIZE(buf) \ 43 (sizeof(buf) - sizeof(buf.uc) + \ 44 sizeof((buf).uc.uc_mcontext.__reserved)) 45 46 #define GET_UCP_RESV_SIZE(ucp) \ 47 sizeof((ucp)->uc_mcontext.__reserved) 48 49 #define ASSERT_BAD_CONTEXT(uc) do { \ 50 char *err = NULL; \ 51 if (!validate_reserved((uc), GET_UCP_RESV_SIZE((uc)), &err)) { \ 52 if (err) \ 53 fprintf(stderr, \ 54 "Using badly built context - ERR: %s\n",\ 55 err); \ 56 } else { \ 57 abort(); \ 58 } \ 59 } while (0) 60 61 #define ASSERT_GOOD_CONTEXT(uc) do { \ 62 char *err = NULL; \ 63 if (!validate_reserved((uc), GET_UCP_RESV_SIZE((uc)), &err)) { \ 64 if (err) \ 65 fprintf(stderr, \ 66 "Detected BAD context - ERR: %s\n", err);\ 67 abort(); \ 68 } else { \ 69 fprintf(stderr, "uc context validated.\n"); \ 70 } \ 71 } while (0) 72 73 /* 74 * A simple record-walker for __reserved area: it walks through assuming 75 * only to find a proper struct __aarch64_ctx header descriptor. 76 * 77 * Instead it makes no assumptions on the content and ordering of the 78 * records, any needed bounds checking must be enforced by the caller 79 * if wanted: this way can be used by caller on any maliciously built bad 80 * contexts. 81 * 82 * head->size accounts both for payload and header _aarch64_ctx size ! 83 */ 84 #define GET_RESV_NEXT_HEAD(h) \ 85 (struct _aarch64_ctx *)((char *)(h) + (h)->size) 86 87 struct fake_sigframe { 88 siginfo_t info; 89 ucontext_t uc; 90 }; 91 92 93 bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err); 94 95 static inline struct _aarch64_ctx *get_header(struct _aarch64_ctx *head, uint32_t magic, 96 size_t resv_sz, size_t *offset) 97 { 98 size_t offs = 0; 99 struct _aarch64_ctx *found = NULL; 100 101 if (!head || resv_sz < HDR_SZ) 102 return found; 103 104 while (offs <= resv_sz - HDR_SZ && 105 head->magic != magic && head->magic) { 106 offs += head->size; 107 head = GET_RESV_NEXT_HEAD(head); 108 } 109 if (head->magic == magic) { 110 found = head; 111 if (offset) 112 *offset = offs; 113 } 114 115 return found; 116 } 117 118 119 static inline struct _aarch64_ctx *get_terminator(struct _aarch64_ctx *head, 120 size_t resv_sz, 121 size_t *offset) 122 { 123 return get_header(head, 0, resv_sz, offset); 124 } 125 126 static inline void write_terminator_record(struct _aarch64_ctx *tail) 127 { 128 if (tail) { 129 tail->magic = 0; 130 tail->size = 0; 131 } 132 } 133 134 struct _aarch64_ctx *get_starting_head(struct _aarch64_ctx *shead, 135 size_t need_sz, size_t resv_sz, 136 size_t *offset); 137 #endif 138