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