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