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