aboutsummaryrefslogtreecommitdiff
path: root/spm/cactus/cactus_ffa_tests.c
blob: 032d2e2e326703086b0c560fe1d319e61f6b1f6e (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
#include <assert.h>
#include <debug.h>
#include <errno.h>

#include <cactus_def.h>
#include <cactus_platform_def.h>
#include <ffa_endpoints.h>
#include <sp_helpers.h>
#include <spm_common.h>

#include <lib/libc/string.h>
#include <lib/xlat_tables/xlat_tables_v2.h>

/* FFA version test helpers */
#define FFA_MAJOR 1U
#define FFA_MINOR 0U

static const uint32_t primary_uuid[4] = PRIMARY_UUID;
static const uint32_t secondary_uuid[4] = SECONDARY_UUID;
static const uint32_t tertiary_uuid[4] = TERTIARY_UUID;
static const uint32_t null_uuid[4] = {0};

/*
 * Fill SIMD vectors from secure world side with a unique value.
 * 0x22 is just a dummy value to be distinguished from the value
 * in the normal world.
 */
void fill_simd_vectors(void)
{
	simd_vector_t simd_vectors[SIMD_NUM_VECTORS];

	for (unsigned int num = 0U; num < SIMD_NUM_VECTORS; num++) {
		memset(simd_vectors[num], 0x22 * num, sizeof(simd_vector_t));
	}

	fill_simd_vector_regs(simd_vectors);
}

/*
 * Test FFA_FEATURES interface.
 */
static void ffa_features_test(void)
{
	const char *test_features = "FFA Features interface";
	smc_ret_values ffa_ret;
	const struct ffa_features_test *ffa_feature_test_target;
	unsigned int i, test_target_size =
		get_ffa_feature_test_target(&ffa_feature_test_target);


	announce_test_section_start(test_features);

	for (i = 0U; i < test_target_size; i++) {
		announce_test_start(ffa_feature_test_target[i].test_name);

		ffa_ret = ffa_features(ffa_feature_test_target[i].feature);
		expect(ffa_func_id(ffa_ret), ffa_feature_test_target[i].expected_ret);
		if (ffa_feature_test_target[i].expected_ret == FFA_ERROR) {
			expect(ffa_error_code(ffa_ret), FFA_ERROR_NOT_SUPPORTED);
		}

		announce_test_end(ffa_feature_test_target[i].test_name);
	}

	announce_test_section_end(test_features);
}

static void ffa_partition_info_helper(struct mailbox_buffers *mb, const uint32_t uuid[4],
			       const struct ffa_partition_info *expected,
			       const uint16_t expected_size)
{
	smc_ret_values ret = ffa_partition_info_get(uuid);
	unsigned int i;
	expect(ffa_func_id(ret), FFA_SUCCESS_SMC32);

	struct ffa_partition_info *info = (struct ffa_partition_info *)(mb->recv);
	for (i = 0U; i < expected_size; i++) {
		expect(info[i].id, expected[i].id);
		expect(info[i].exec_context, expected[i].exec_context);
		expect(info[i].properties, expected[i].properties);
	}

	ret = ffa_rx_release();
	expect(ffa_func_id(ret), FFA_SUCCESS_SMC32);
}

static void ffa_partition_info_wrong_test(void)
{
	const char *test_wrong_uuid = "Request wrong UUID";
	uint32_t uuid[4] = {1};

	announce_test_start(test_wrong_uuid);

	smc_ret_values ret = ffa_partition_info_get(uuid);
	expect(ffa_func_id(ret), FFA_ERROR);
	expect(ffa_error_code(ret), FFA_ERROR_INVALID_PARAMETER);

	announce_test_end(test_wrong_uuid);
}

static void ffa_partition_info_get_test(struct mailbox_buffers *mb)
{
	const char *test_partition_info = "FFA Partition info interface";
	const char *test_primary = "Get primary partition info";
	const char *test_secondary = "Get secondary partition info";
	const char *test_tertiary = "Get tertiary partition info";
	const char *test_all = "Get all partitions info";

	const struct ffa_partition_info expected_info[] = {
		/* Primary partition info */
		{
			.id = SPM_VM_ID_FIRST,
			.exec_context = CACTUS_PRIMARY_EC_COUNT,
			/* Supports receipt of direct message requests. */
			.properties = 1U
		},
		/* Secondary partition info */
		{
			.id = SPM_VM_ID_FIRST + 1U,
			.exec_context = CACTUS_SECONDARY_EC_COUNT,
			.properties = 1U
		},
		/* Tertiary partition info */
		{
			.id = SPM_VM_ID_FIRST + 2U,
			.exec_context = CACTUS_TERTIARY_EC_COUNT,
			.properties = 1U
		}
	};

	announce_test_section_start(test_partition_info);

	announce_test_start(test_tertiary);
	ffa_partition_info_helper(mb, tertiary_uuid, &expected_info[2], 1);
	announce_test_end(test_tertiary);

	announce_test_start(test_secondary);
	ffa_partition_info_helper(mb, secondary_uuid, &expected_info[1], 1);
	announce_test_end(test_secondary);

	announce_test_start(test_primary);
	ffa_partition_info_helper(mb, primary_uuid, &expected_info[0], 1);
	announce_test_end(test_primary);

	announce_test_start(test_all);
	ffa_partition_info_helper(mb, null_uuid, expected_info, 3);
	announce_test_end(test_all);

	ffa_partition_info_wrong_test();

	announce_test_section_end(test_partition_info);
}

void ffa_version_test(void)
{
	const char *test_ffa_version = "FFA Version interface";

	announce_test_start(test_ffa_version);

	smc_ret_values ret = ffa_version(MAKE_FFA_VERSION(FFA_MAJOR, FFA_MINOR));
	uint32_t spm_version = (uint32_t)ret.ret0;

	bool ffa_version_compatible =
		((spm_version >> FFA_VERSION_MAJOR_SHIFT) == FFA_MAJOR &&
		 (spm_version & FFA_VERSION_MINOR_MASK) >= FFA_MINOR);

	NOTICE("FFA_VERSION returned %u.%u; Compatible: %i\n",
		spm_version >> FFA_VERSION_MAJOR_SHIFT,
		spm_version & FFA_VERSION_MINOR_MASK,
		(int)ffa_version_compatible);

	expect((int)ffa_version_compatible, (int)true);

	announce_test_end(test_ffa_version);
}

bool ffa_memory_retrieve_test(struct mailbox_buffers *mb,
			 struct ffa_memory_region **retrieved,
			 uint64_t handle, ffa_vm_id_t sender,
			 ffa_vm_id_t receiver, uint32_t mem_func)
{
	smc_ret_values ret;
	uint32_t fragment_size;
	uint32_t total_size;
	uint32_t descriptor_size;

	if (retrieved == NULL || mb == NULL) {
		ERROR("Invalid parameters!\n");
		return false;
	}

	/*
	 * TODO: Revise shareability attribute in function call
	 * below.
	 * https://lists.trustedfirmware.org/pipermail/hafnium/2020-June/000023.html
	 */
	descriptor_size = ffa_memory_retrieve_request_init(
	    mb->send, handle, sender, receiver, 0, 0,
	    FFA_DATA_ACCESS_RW,
	    FFA_INSTRUCTION_ACCESS_NX,
	    FFA_MEMORY_NORMAL_MEM,
	    FFA_MEMORY_CACHE_WRITE_BACK,
	    FFA_MEMORY_OUTER_SHAREABLE);

	ret = ffa_mem_retrieve_req(descriptor_size, descriptor_size);

	if (ffa_func_id(ret) != FFA_MEM_RETRIEVE_RESP) {
		ERROR("Couldn't retrieve the memory page. Error: %lx\n",
		      ret.ret2);
		return false;
	}

	/*
	 * Following total_size and fragment_size are useful to keep track
	 * of the state of transaction. When the sum of all fragment_size of all
	 * fragments is equal to total_size, the memory transaction has been
	 * completed.
	 * This is a simple test with only one segment. As such, upon
	 * successful ffa_mem_retrieve_req, total_size must be equal to
	 * fragment_size.
	 */
	total_size = ret.ret1;
	fragment_size = ret.ret2;

	if (total_size != fragment_size) {
		ERROR("Only expect one memory segment to be sent!\n");
		return false;
	}

	if (fragment_size > PAGE_SIZE) {
		ERROR("Fragment should be smaller than RX buffer!\n");
		return false;
	}

	*retrieved = (struct ffa_memory_region *)mb->recv;

	if ((*retrieved)->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
		VERBOSE("SPMC memory sharing operations support max of %u "
			"receivers!\n", MAX_MEM_SHARE_RECIPIENTS);
		return false;
	}

	VERBOSE("Memory Retrieved!\n");

	return true;
}

bool ffa_memory_relinquish_test(struct ffa_mem_relinquish *m,
			   uint64_t handle,
			   ffa_vm_id_t id)
{
	smc_ret_values ret;

	ffa_mem_relinquish_init(m, handle, 0, id);
	ret = ffa_mem_relinquish();
	if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
		ERROR("%s failed to relinquish memory! error: %x\n",
		      __func__, ffa_error_code(ret));
		return false;
	}

	VERBOSE("Memory Relinquished!\n");
	return true;
}

void ffa_memory_management_test(struct mailbox_buffers *mb, ffa_vm_id_t vm_id,
				ffa_vm_id_t sender, uint32_t mem_func,
				uint64_t handle)
{
	const char *test_ffa = "Memory Management";
	struct ffa_memory_region *m;
	struct ffa_composite_memory_region *composite;
	int ret;
	unsigned int mem_attrs;
	uint32_t *ptr;

	announce_test_section_start(test_ffa);

	expect(ffa_memory_retrieve_test(
				mb, &m, handle, sender, vm_id, mem_func),
		true);

	composite = ffa_memory_region_get_composite(m, 0);

	VERBOSE("Address: %p; page_count: %x %x\n",
		composite->constituents[0].address,
		composite->constituents[0].page_count, PAGE_SIZE);

	/* This test is only concerned with RW permissions. */
	expect(ffa_get_data_access_attr(
			m->receivers[0].receiver_permissions.permissions),
		FFA_DATA_ACCESS_RW);

	mem_attrs = MT_RW_DATA | MT_EXECUTE_NEVER;

	if (!IS_SP_ID(sender)) {
		mem_attrs |= MT_NS;
	}

	ret = mmap_add_dynamic_region(
			(uint64_t)composite->constituents[0].address,
			(uint64_t)composite->constituents[0].address,
			composite->constituents[0].page_count * PAGE_SIZE,
			mem_attrs);
	expect(ret, 0);

	VERBOSE("Memory has been mapped\n");

	ptr = (uint32_t *) composite->constituents[0].address;

	/* Write mem_func to retrieved memory region for validation purposes. */
	VERBOSE("Writing: %x\n", mem_func);
	for (unsigned int i = 0U; i < 5U; i++)
		ptr[i] = mem_func;

	/*
	 * A FFA_MEM_DONATE changes the ownership of the page, as such no
	 * relinquish is needed.
	 */
	if (mem_func != FFA_MEM_DONATE_SMC32) {
		ret = mmap_remove_dynamic_region(
			(uint64_t)composite->constituents[0].address,
			composite->constituents[0].page_count * PAGE_SIZE);
		expect(ret, 0);

		expect(ffa_memory_relinquish_test(
			   (struct ffa_mem_relinquish *)mb->send,
			   m->handle, vm_id),
		       true);
	}

	expect(ffa_func_id(ffa_rx_release()), FFA_SUCCESS_SMC32);

	announce_test_section_end(test_ffa);
}

void ffa_tests(struct mailbox_buffers *mb)
{
	const char *test_ffa = "FFA Interfaces";

	announce_test_section_start(test_ffa);

	ffa_features_test();
	ffa_version_test();
	ffa_partition_info_get_test(mb);

	announce_test_section_end(test_ffa);
}