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