aboutsummaryrefslogtreecommitdiff
path: root/include/common/test_helpers.h
blob: 3f69df9ea65e2de5f322bf3779666bd82913a7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef TEST_HELPERS_H__
#define TEST_HELPERS_H__

#include <uuid.h>
#include <arch_features.h>
#include <ffa_helpers.h>
#include <ffa_svc.h>
#include <psci.h>
#include <tftf_lib.h>
#include <trusted_os.h>
#include <tsp.h>
#include <uuid_utils.h>
#include <plat_topology.h>

typedef struct {
	uintptr_t addr;
	size_t size;
	unsigned int attr;
	void *arg;
} map_args_unmap_t;

typedef test_result_t (*test_function_arg_t)(void *arg);

#ifndef __aarch64__
#define SKIP_TEST_IF_AARCH32()							\
	do {									\
		tftf_testcase_printf("Test not supported on aarch32\n");	\
		return TEST_RESULT_SKIPPED;					\
	} while (0)
#else
#define SKIP_TEST_IF_AARCH32()
#endif

#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n)					\
	do {									\
		unsigned int clusters_cnt;					\
		clusters_cnt = tftf_get_total_clusters_count();			\
		if (clusters_cnt < (n)) {					\
			tftf_testcase_printf(					\
				"Need at least %u clusters, only found %u\n",	\
				(n), clusters_cnt);				\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n)					\
	do {									\
		unsigned int cpus_cnt;						\
		cpus_cnt = tftf_get_total_cpus_count();				\
		if (cpus_cnt < (n)) {						\
			tftf_testcase_printf(					\
				"Need at least %u CPUs, only found %u\n",	\
				(n), cpus_cnt);					\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT()					\
	do {									\
		uuid_t tos_uuid;						\
										\
		if (!is_trusted_os_present(&tos_uuid)) {			\
			tftf_testcase_printf("No Trusted OS detected\n");	\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_TSP_NOT_PRESENT()						\
	do {									\
		uuid_t tos_uuid;						\
		char tos_uuid_str[UUID_STR_SIZE];				\
										\
		if (!is_trusted_os_present(&tos_uuid)) {			\
			tftf_testcase_printf("No Trusted OS detected\n");	\
			return TEST_RESULT_SKIPPED;				\
		}								\
										\
		if (!uuid_equal(&tos_uuid, &tsp_uuid)) {			\
			tftf_testcase_printf(					\
				"Trusted OS is not the TSP, its UUID is: %s\n",	\
				uuid_to_str(&tos_uuid, tos_uuid_str));		\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED()					\
	do {									\
		if (!is_armv8_3_pauth_present()) {				\
			tftf_testcase_printf(					\
				"Pointer Authentication not supported\n");	\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_FGT_NOT_SUPPORTED()					\
	do {									\
		if (!is_armv8_6_fgt_present()) {				\
			tftf_testcase_printf(					\
				"Fine Grained Traps not supported\n");		\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC()					\
	do {									\
		if (get_armv8_6_ecv_support() !=				\
			ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) {			\
			tftf_testcase_printf("ARMv8.6-ECV not supported\n");	\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_MM_NOT_PRESENT()						\
	do {									\
		smc_args version_smc = { MM_VERSION_AARCH32 };			\
		smc_ret_values smc_ret = tftf_smc(&version_smc);		\
		uint32_t version = smc_ret.ret0;				\
										\
		if (version == (uint32_t) SMC_UNKNOWN) {			\
			tftf_testcase_printf("SPM not detected.\n");		\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n)					\
	do {									\
		if (get_armv8_5_mte_support() < (n)) {				\
			tftf_testcase_printf(					\
				"Memory Tagging Extension not supported\n");	\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor)				\
	do {									\
		smc_args version_smc = { MM_VERSION_AARCH32 };			\
		smc_ret_values smc_ret = tftf_smc(&version_smc);		\
		uint32_t version = smc_ret.ret0;				\
										\
		if (version == (uint32_t) SMC_UNKNOWN) {			\
			tftf_testcase_printf("SPM not detected.\n");		\
			return TEST_RESULT_SKIPPED;				\
		}								\
										\
		if (version < MM_VERSION_FORM(major, minor)) {			\
			tftf_testcase_printf("MM_VERSION returned %u.%u\n"	\
					"The required version is %u.%u\n",	\
					version >> MM_VERSION_MAJOR_SHIFT,	\
					version & MM_VERSION_MINOR_MASK,	\
					major, minor);				\
			return TEST_RESULT_SKIPPED;				\
		}								\
										\
		VERBOSE("MM_VERSION returned %u.%u\n",				\
			version >> MM_VERSION_MAJOR_SHIFT,			\
			version & MM_VERSION_MINOR_MASK);			\
	} while (0)

#define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor)			\
	do {									\
		smc_ret_values smc_ret = ffa_version(				\
					MAKE_FFA_VERSION(major, minor));	\
		uint32_t version = smc_ret.ret0;				\
										\
		if (version == FFA_ERROR_NOT_SUPPORTED) {			\
			tftf_testcase_printf("FFA_VERSION not supported.\n");	\
			return TEST_RESULT_SKIPPED;				\
		}								\
										\
		if ((version & FFA_VERSION_BIT31_MASK) != 0U) {				\
			tftf_testcase_printf("FFA_VERSION bad response: %x\n",	\
					version);				\
			return TEST_RESULT_FAIL;				\
		}								\
										\
		if (version < MAKE_FFA_VERSION(major, minor)) {			\
			tftf_testcase_printf("FFA_VERSION returned %u.%u\n"	\
					"The required version is %u.%u\n",	\
					version >> FFA_VERSION_MAJOR_SHIFT,	\
					version & FFA_VERSION_MINOR_MASK,	\
					major, minor);				\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version)			\
	do {									\
		uint32_t debug_ver = arch_get_debug_version();			\
										\
		if (debug_ver < version) {					\
			tftf_testcase_printf("Debug version returned %d\n"	\
					     "The required version is %d\n",	\
					     debug_ver,				\
					     version);				\
			return TEST_RESULT_SKIPPED;				\
		}								\
	} while (0)

#define SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(mb, ffa_uuid)			\
	do {									\
		smc_ret_values smc_ret = ffa_partition_info_get(ffa_uuid);	\
		ffa_rx_release();						\
		if (smc_ret.ret0 == FFA_ERROR && 				\
		    smc_ret.ret2 == FFA_ERROR_INVALID_PARAMETER) {		\
			tftf_testcase_printf("FFA endpoint not deployed!\n");	\
			return TEST_RESULT_SKIPPED;				\
		} else if (smc_ret.ret0 != FFA_SUCCESS_SMC32) {			\
			ERROR("ffa_partition_info_get failed!\n");		\
			return TEST_RESULT_FAIL;				\
		}								\
	} while (0)

#define GET_TFTF_MAILBOX(mb)							\
	do {									\
		if (!get_tftf_mailbox(&mb)) {					\
			ERROR("Mailbox not configured!\nThis test relies on"	\
			" test suite \"FF-A RXTX Mapping\" to map/configure"	\
			" RXTX buffers\n");					\
			return TEST_RESULT_FAIL;				\
		}								\
	} while (false);

#define CHECK_SPMC_TESTING_SETUP(ffa_major, ffa_minor, expected_uuids)		\
	do {									\
		const size_t expected_uuids_size =				\
			 sizeof(expected_uuids) / sizeof(struct ffa_uuid);	\
		test_result_t ret = check_spmc_testing_set_up(			\
			ffa_major, ffa_minor, expected_uuids, 			\
			expected_uuids_size);					\
		if (ret != TEST_RESULT_SUCCESS) {				\
			return ret;						\
		}								\
	} while (false);

/* Helper macro to verify if system suspend API is supported */
#define is_psci_sys_susp_supported()	\
		(tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND)		\
					== PSCI_E_SUCCESS)

/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
#define is_psci_stat_count_supported()	\
		(tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT)		\
					== PSCI_E_SUCCESS)

/*
 * Helper function to verify the system state is ready for system
 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
 * Returns 1 if the system is ready to suspend, 0 otherwise.
 */
int is_sys_suspend_state_ready(void);

/*
 * Helper function to reset the system. This function shouldn't return.
 * It is not marked with __dead to help the test to catch some error in
 * TF
 */
void psci_system_reset(void);

/*
 * Helper function that enables/disables the mem_protect mechanism
 */
int psci_mem_protect(int val);


/*
 * Helper function to call PSCI MEM_PROTECT_CHECK
 */
int psci_mem_protect_check(uintptr_t addr, size_t size);


/*
 * Helper function to get a sentinel address that can be used to test mem_protect
 */
unsigned char *psci_mem_prot_get_sentinel(void);

/*
 * Helper function to memory map and unmap a region needed by a test.
 *
 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
 * unmapped. Otherwise, return the test functions's result.
 */
test_result_t map_test_unmap(const map_args_unmap_t *args,
			     test_function_arg_t test);

/*
 * Helper function to set TFTF global mailbox for SPM related tests.
 * This function should be invoked by the first TFTF test that requires
 * RX and/or TX buffers.
 */
void set_tftf_mailbox(const struct mailbox_buffers *mb);

/*
 * Helper function to get TFTF global mailbox for SPM related tests.
 * This function should be called by all tests that require access to RX or TX
 * buffers, after the function 'set_tftf_mailbox' has been used by the first
 * test to rely on RX and TX buffers.
 */
bool get_tftf_mailbox(struct mailbox_buffers *mb);

test_result_t check_spmc_testing_set_up(uint32_t ffa_version_major,
	uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids,
	size_t ffa_uuids_size);

#endif /* __TEST_HELPERS_H__ */