blob: 444c61cdfee949b3245de11021cec8625750ef75 [file] [log] [blame]
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <CppUTest/CommandLineTestRunner.h>
7#include <CppUTest/TestHarness.h>
8
9extern "C" {
10#include <buffer.h> /* Interface to exercise */
11#include <buffer_private.h>
12#include <cpuid.h>
13#include <granule.h>
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +000014#include <host_defs.h>
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010015#include <host_harness.h>
16#include <host_utils.h>
17#include <realm_test_utils.h>
18#include <stdlib.h>
19#include <string.h>
20#include <test_harness.h>
21#include <test_helpers.h>
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010022#include <xlat_tables.h>
23}
24
25/*
26 * Size of a chunck of memory on a granule, used for random
27 * read and writes
28 */
29#define GRANULE_BLOCK_SIZE (GRANULE_SIZE >> 2U)
30#define GRANULE_BLOCKS (GRANULE_SIZE/GRANULE_BLOCK_SIZE)
31
32/*
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +000033 * Function to get a random granule address within the valid address range.
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010034 */
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +010035static inline uintptr_t get_rand_granule_addr(void)
36{
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010037 uintptr_t addr;
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +010038
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +010039 unsigned long random_granule = test_helpers_get_rand_in_range(0UL,
40 test_helpers_get_nr_granules() - 1);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010041
42 addr = (uintptr_t)(random_granule * GRANULE_SIZE)
43 + host_util_get_granule_base();
44
45 return addr;
46}
47
48/*
49 * Helper function to generate an array of random granule addresses
50 * in which none of them repeat.
51 */
52static void get_rand_granule_array(uintptr_t *arr, unsigned int count)
53{
54 for (unsigned int i = 0U; i < count; i++) {
55 arr[i] = get_rand_granule_addr();
56 if (i > 0U) {
57 bool match;
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +010058
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010059 do {
60 /* Check for duplicates so far */
61 match = false;
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +010062 for (unsigned int j = 0U; j < i; j++) {
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010063 if (arr[j] == arr[i]) {
64 arr[i] =
65 get_rand_granule_addr();
66 match = true;
67 break;
68 }
69 }
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +010070 } while (match == true);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010071 }
72 }
73
74}
75
76TEST_GROUP(slot_buffer) {
77 /*
78 * For this test, TEST_SETUP() initializes RMM which includes
79 * translation table and slot buffer mechanism initialization.
80 * Therefore, all the tests assume that the slot buffer mechanism
81 * has been properly initialized.
82 */
83 TEST_SETUP()
84 {
Javier Almansa Sobrino10cd6b82023-01-20 11:55:09 +000085 test_helpers_init();
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010086
87 /* Enable the platform with support for multiple PEs */
88 test_helpers_rmm_start(true);
89
90 /* Make sure current cpu id is 0 (primary processor) */
91 host_util_set_cpuid(0U);
92
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +010093 test_helpers_expect_assert_fail(false);
94 }
95
96 TEST_TEARDOWN()
97 {
98 /*
99 * Unregister any existing callback that might
100 * have been installed
101 */
102 (void)test_helpers_unregister_cb(CB_BUFFER_MAP);
103 (void)test_helpers_unregister_cb(CB_BUFFER_UNMAP);
104 }
105};
106
107TEST(slot_buffer, granule_map_buffer_unmap_TC1)
108{
109 uintptr_t slot_va, expected_va, granule_addr;
110 struct granule *test_granule;
111 union test_harness_cbs cb;
112
113 /******************************************************************
114 * TEST CASE 1:
115 *
116 * For all possible slot buffer types and all possible CPUs, try to
117 * map a random granule. Then unmap it.
118 ******************************************************************/
119
120 /* Register harness callbacks to use by this test */
121 cb.buffer_map = test_buffer_map_aarch64_vmsa;
122 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
123 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
124 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
125
126 granule_addr = get_rand_granule_addr();
127 test_granule = addr_to_granule(granule_addr);
128
129 for (unsigned int i = 0U; i < MAX_CPUS; i++) {
130 host_util_set_cpuid(i);
131 for (unsigned int j = 0U; j < NR_CPU_SLOTS; j++) {
132 if (j == SLOT_NS) {
133 /* Not supported. granule_map() would assert */
134 continue;
135 }
136 slot_va = (uintptr_t)granule_map(test_granule,
137 (enum buffer_slot)j);
138 expected_va = slot_to_va((enum buffer_slot)j);
139
140 /* Test the return value from granule_map() */
141 POINTERS_EQUAL(slot_va, expected_va);
142
143 /*
144 * Test that the granule is actually mapped to the
145 * expected VA in the Stage 1 xlat tables as per
146 * aarch64 VMSA.
147 */
148 POINTERS_EQUAL(expected_va,
149 realm_test_util_slot_va_from_pa(granule_addr));
150
151 /* Unmap the buffer */
152 buffer_unmap((void *)slot_va);
153
154 /*
155 * realm_test_util_slot_va_from_pa() return NULL
156 * if the address passed to it is not mapped to any
157 * slot buffer.
158 */
159 POINTERS_EQUAL(NULL,
160 realm_test_util_slot_va_from_pa(granule_addr));
161
162 } /* For each slot type */
163 } /* For each CPU */
164}
165
166TEST(slot_buffer, granule_map_buffer_unmap_TC2)
167{
168 uintptr_t mapped_pa;
169 struct granule *test_granule;
170 uintptr_t granules_per_cpu[MAX_CPUS];
171 void *slot_va[MAX_CPUS];
172 union test_harness_cbs cb;
173
174 /******************************************************************
175 * TEST CASE 2:
176 *
177 * For each possible slot buffer type, map a different random
178 * granule to each one of the available CPUs. Then validate that
179 * the same PA is not mapped to two different CPUs.
180 ******************************************************************/
181
182 /* Register harness callbacks to use by this test */
183 cb.buffer_map = test_buffer_map_aarch64_vmsa;
184 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
185 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
186 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
187
188 get_rand_granule_array(granules_per_cpu, MAX_CPUS);
189 for (unsigned int i = 0U; i < NR_CPU_SLOTS; i++) {
190 if (i == SLOT_NS) {
191 /* Not supported. granule_map() would assert */
192 continue;
193 }
194
195 /* Map a granule on each CPU for the same slot */
196 for (unsigned int j = 0U; j < MAX_CPUS; j++) {
197 host_util_set_cpuid(j);
198 test_granule = addr_to_granule(granules_per_cpu[j]);
199 slot_va[j] = granule_map(test_granule,
200 (enum buffer_slot)i);
201 }
202
203 /*
204 * Iterate over all CPUs, ensuring that the granules are mapped
205 * into the slots for the right CPU.
206 */
207 for (unsigned int j = 0U; j < MAX_CPUS; j++) {
208 /*
209 * Get the PA mapped to the slot 'i' for CPU 'j'
210 */
211 host_util_set_cpuid(j);
212 mapped_pa = realm_test_util_slot_to_pa(
213 (enum buffer_slot)i);
214
215 /*
216 * Check that the PA mapped to slot 'i' for CPU 'j'
217 * is only mapped on the same slot for the same CPU.
218 * For the rest of CPUs, the PAs should not match.
219 */
220 for (unsigned int k = 0U; k < MAX_CPUS; k++) {
221 if (j == k) {
222 POINTERS_EQUAL(granules_per_cpu[k],
223 mapped_pa);
224 } else {
225 CHECK_FALSE(granules_per_cpu[k] ==
226 mapped_pa);
227 }
228 }
229
230 }
231
232 /* Unmap the granules. */
233 for (unsigned int j = 0U; j < MAX_CPUS; j++) {
234 host_util_set_cpuid(j);
235 buffer_unmap((void *)slot_va[j]);
236 }
237 } /* NR_CPU_SLOTS */
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100238};
239
240TEST(slot_buffer, granule_map_buffer_unmap_TC3)
241{
242 /******************************************************************
243 * TEST CASE 3:
244 *
245 * Test that buffer_unmap() exits gracefully when an unmapped VA
246 * is used.
247 ******************************************************************/
248
249 buffer_unmap((void *)slot_to_va(SLOT_NS));
250 TEST_EXIT;
251}
252
253TEST(slot_buffer, granule_map_buffer_unmap_TC4)
254{
255 /******************************************************************
256 * TEST CASE 4:
257 *
258 * Test that buffer_unmap() exits gracefully when an invalid VA
259 * is used.
260 ******************************************************************/
261
262 buffer_unmap((void *)NULL);
263 TEST_EXIT;
264}
265
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000266ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC5)
267{
268 uintptr_t granule_addr;
269 struct granule *test_granule;
270 union test_harness_cbs cb;
271 unsigned int cpuid;
272
273 /******************************************************************
274 * TEST CASE 5:
275 *
276 * For a random CPU, try to map a random granule to a SLOT_NS buffer.
277 * The operation should generate an assertion failure.
278 ******************************************************************/
279
280 /* Register harness callbacks to use by this test */
281 cb.buffer_map = test_buffer_map_aarch64_vmsa;
282 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
283 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
284 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
285
286 granule_addr = get_rand_granule_addr();
287 test_granule = addr_to_granule(granule_addr);
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100288 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
289 (MAX_CPUS - 1));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000290 host_util_set_cpuid(cpuid);
291
292 test_helpers_expect_assert_fail(true);
293 (void)granule_map(test_granule, SLOT_NS);
294 test_helpers_fail_if_no_assert_failed();
295}
296
297ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC6)
298{
299 union test_harness_cbs cb;
300 unsigned int cpuid;
301 enum buffer_slot slot;
302
303 /******************************************************************
304 * TEST CASE 6:
305 *
306 * For a random CPU, try to map a NULL granule address to a random
307 * slot type other than SLOT_NS.
308 * The operation should generate an assertion failure.
309 ******************************************************************/
310
311 /* Register harness callbacks to use by this test */
312 cb.buffer_map = test_buffer_map_aarch64_vmsa;
313 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
314 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
315 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
316
317 slot = (enum buffer_slot)test_helpers_get_rand_in_range(
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100318 (unsigned long)(SLOT_NS + 1U),
319 (unsigned long)NR_CPU_SLOTS);
320 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
321 (unsigned long)(MAX_CPUS - 1));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000322 host_util_set_cpuid(cpuid);
323
324 test_helpers_expect_assert_fail(true);
325 (void)granule_map((struct granule *)NULL, slot);
326 test_helpers_fail_if_no_assert_failed();
327}
328
329ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC7)
330{
331 union test_harness_cbs cb;
332 unsigned int cpuid;
333 enum buffer_slot slot;
334 struct granule *test_granule;
335
336 /******************************************************************
337 * TEST CASE 7:
338 *
339 * For a random CPU, try to map a granule address less than the
340 * start of valid granule addr range to a random slot type other
341 * than SLOT_NS.
342 * The operation should generate an assertion failure.
343 ******************************************************************/
344
345 /* Register harness callbacks to use by this test */
346 cb.buffer_map = test_buffer_map_aarch64_vmsa;
347 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
348 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
349 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
350
351 test_granule = realm_test_util_granule_struct_base() - 1U;
352 slot = (enum buffer_slot)test_helpers_get_rand_in_range(
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100353 (unsigned long)(SLOT_NS + 1U),
354 (unsigned long)NR_CPU_SLOTS);
355 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
356 (unsigned long)(MAX_CPUS - 1));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000357 host_util_set_cpuid(cpuid);
358
359 test_helpers_expect_assert_fail(true);
360 (void)granule_map(test_granule, slot);
361 test_helpers_fail_if_no_assert_failed();
362}
363
364ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC8)
365{
366 union test_harness_cbs cb;
367 unsigned int cpuid;
368 enum buffer_slot slot;
369 struct granule *test_granule;
370
371 /******************************************************************
372 * TEST CASE 8:
373 *
374 * For a random CPU, try to map a granule address over the end of
375 * the granules array to a random slot type other than SLOT_NS.
376 * The operation should generate an assertion failure.
377 ******************************************************************/
378
379 /* Register harness callbacks to use by this test */
380 cb.buffer_map = test_buffer_map_aarch64_vmsa;
381 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
382 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
383 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
384
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +0100385 test_granule = realm_test_util_granule_struct_base() +
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000386 HOST_NR_GRANULES;
387 slot = (enum buffer_slot)test_helpers_get_rand_in_range(
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100388 (unsigned long)(SLOT_NS + 1U),
389 (unsigned long)NR_CPU_SLOTS);
390 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
391 MAX_CPUS - 1);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000392 host_util_set_cpuid(cpuid);
393
394 test_helpers_expect_assert_fail(true);
395 (void)granule_map(test_granule, slot);
396 test_helpers_fail_if_no_assert_failed();
397}
398
399ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC9)
400{
401 uintptr_t granule_addr;
402 uintptr_t test_granule;
403 union test_harness_cbs cb;
404 unsigned int cpuid;
405 enum buffer_slot slot;
406
407 /******************************************************************
408 * TEST CASE 9:
409 *
410 * For a random CPU, try to map an unaligned granule address to a
411 * random slot type other than SLOT_NS.
412 * The operation should generate an assertion failure.
413 ******************************************************************/
414
415 /* Register harness callbacks to use by this test */
416 cb.buffer_map = test_buffer_map_aarch64_vmsa;
417 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
418 cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
419 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
420
421 granule_addr = get_rand_granule_addr();
422 test_granule = (uintptr_t)addr_to_granule(granule_addr);
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100423 test_granule += test_helpers_get_rand_in_range(1UL,
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000424 sizeof(struct granule) - 1);
425
426 slot = (enum buffer_slot)test_helpers_get_rand_in_range(
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100427 (unsigned long)(SLOT_NS + 1U),
428 (unsigned long)NR_CPU_SLOTS);
429 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
430 MAX_CPUS - 1);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000431 host_util_set_cpuid(cpuid);
432
433 test_helpers_expect_assert_fail(true);
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +0100434 (void)granule_map((struct granule *)test_granule, slot);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000435 test_helpers_fail_if_no_assert_failed();
436}
437
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100438TEST(slot_buffer, ns_buffer_write_TC1)
439{
440 uintptr_t granule_addrs[3];
441 struct granule *test_granule;
442 union test_harness_cbs cb;
443
444 /******************************************************************
445 * TEST CASE 1:
446 *
447 * For each CPU, map a random granule to NS_SLOT and copy random
448 * data into it through several calls to ns_buffer_write().
449 * Then verify that for each call to ns_buffer_write(), the data
450 * is properly copied without affecting other areas of the dest
451 * granule.
452 ******************************************************************/
453
454 /* Register harness callbacks to use by this test */
455 cb.buffer_map = test_buffer_map_access;
456 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
457 cb.buffer_unmap = test_buffer_unmap_access;
458 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
459
460 /*
461 * Get two random granules:
462 * granule_addrs[0]: To be used as dest write operations (SLOT_NS).
463 * granule_addrs[1]: will hold a copy of the data to transfer, so we
464 * can verify later.
465 * granule_addrs[2]: Just a zeroed granule to easy some tests.
466 */
467 get_rand_granule_array(granule_addrs, 3U);
468
469 /* Granule to test zeroes */
470 (void)memset((void *)granule_addrs[2], 0, GRANULE_SIZE);
471
472 test_granule = addr_to_granule(granule_addrs[0]);
473
474 for (unsigned int i = 0U; i < MAX_CPUS; i++) {
475
476 /* Fill the granule with random data */
477 for (unsigned int i = 0U; i < GRANULE_SIZE/sizeof(int); i++) {
478 *((int *)granule_addrs[1] + i) = rand();
479 }
480
481 /* Clean the granule to test */
482 (void)memset((void *)granule_addrs[0], 0, GRANULE_SIZE);
483
484 host_util_set_cpuid(i);
485
486 /*
487 * Copy block by block, verifying that each copied block
488 * doesn't affect anything written before nor a block to be
489 * written yet.
490 */
491 for (unsigned int j = 0U; j < GRANULE_BLOCKS; j++) {
492 ns_buffer_write(SLOT_NS, test_granule,
493 GRANULE_BLOCK_SIZE * j,
494 GRANULE_BLOCK_SIZE,
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +0100495 (void *)(granule_addrs[1] +
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100496 (GRANULE_BLOCK_SIZE * j)));
497
498 MEMCMP_EQUAL((void *)granule_addrs[1],
499 (void *)granule_addrs[0],
500 (size_t)((j + 1U) * GRANULE_BLOCK_SIZE));
501
502 /*
503 * Verify than any block that has not been written yet
504 * is still all zeros.
505 */
506 MEMCMP_EQUAL((void *)granule_addrs[2],
507 (void *)(granule_addrs[0] +
508 ((j + 1U) * GRANULE_BLOCK_SIZE)),
509 (GRANULE_BLOCKS - (j + 1U)) *
510 GRANULE_BLOCK_SIZE);
511 }
512 }
513}
514
515TEST(slot_buffer, ns_buffer_write_TC2)
516{
517 uintptr_t granule_addrs[3];
518 struct granule *test_granule;
519 union test_harness_cbs cb;
520 int val;
521
522 /******************************************************************
523 * TEST CASE 3:
524 *
525 * For every CPU, verify that ns_buffer_write() does not alter the
526 * source.
527 ******************************************************************/
528
529 /* Register harness callbacks to use by this test */
530 cb.buffer_map = test_buffer_map_access;
531 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
532 cb.buffer_unmap = test_buffer_unmap_access;
533 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
534
535 /*
536 * Get three random granules:
537 * granule_addrs[0]: Will contain the original data to write.
538 * granule_addrs[1]: Will hold a copy of the src granule to compare.
539 * granule_addrs[2]: Destination granule.
540 */
541 get_rand_granule_array(granule_addrs, 3U);
542
543 /* Generate random data. */
544 for (unsigned int j = 0U; j < GRANULE_SIZE/sizeof(int); j++) {
545 val = rand();
546 *((int *)granule_addrs[0] + j) = val;
547 *((int *)granule_addrs[1] + j) = val;
548 }
549
550 test_granule = addr_to_granule(granule_addrs[2]);
551
552 for (unsigned int i = 0U; i < MAX_CPUS; i++) {
553 host_util_set_cpuid(i);
554
555 ns_buffer_write(SLOT_NS, test_granule, 0U,
556 GRANULE_SIZE, (void *)granule_addrs[0]);
557
558 /* Verify that the source has not been altered */
559 MEMCMP_EQUAL((void *)granule_addrs[1],
560 (void *)granule_addrs[0],
561 (size_t)GRANULE_SIZE);
562 }
563
564}
565
566TEST(slot_buffer, ns_buffer_write_TC3)
567{
568 uintptr_t granule_addrs[2];
569 unsigned int cpu[2];
570 long pattern[2];
571 long val;
572 union test_harness_cbs cb;
573
574 /******************************************************************
575 * TEST CASE 3:
576 *
577 * for two random CPUs, map a random granule to their SLOT_NS, then
578 * copy different random data to it. Verify that the data from one
579 * CPU's SLOT_NS hasn't been leaked to the other's CPU SLOT_NS.
580 * This test helps validating that ns_buffer_write() handles the
581 * translation contexts properly.
582 ******************************************************************/
583
584 /* Register harness callbacks to use by this test */
585 cb.buffer_map = test_buffer_map_access;
586 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
587 cb.buffer_unmap = test_buffer_unmap_access;
588 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
589
590 /* Get two random granules, one for each CPU to test. */
591 get_rand_granule_array(granule_addrs, 2U);
592
593 /* Get two random CPUs where to run the tests. */
594 do {
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100595 cpu[0] = (unsigned int)test_helpers_get_rand_in_range(0UL,
596 MAX_CPUS - 1U);
597 cpu[1] = (unsigned int)test_helpers_get_rand_in_range(0UL,
598 MAX_CPUS - 1U);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100599 } while (cpu[0] == cpu[1]);
600
601 /* Get two different patterns of data to copy. */
602 do {
603 pattern[0] = (long)rand();
604 pattern[1] = (long)rand();
605 } while (pattern[0] == pattern[1]);
606
607 /* Copy the patterns into the destination granules. */
608 for (unsigned int i = 0U; i < 2U; i++) {
609 host_util_set_cpuid(cpu[i]);
610
611 ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[i]), 0U,
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +0100612 sizeof(long), (void *)&pattern[i]);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100613 }
614
615 /*
616 * Verify that the granule for the first CPU doesn't contain the
617 * pattern on the second one.
618 */
619 val = *(long *)granule_addrs[0];
620 CHECK_FALSE(val == pattern[1]);
621
622 /*
623 * Repeat the same check, this time with the second CPU.
624 */
625 val = *(long *)granule_addrs[1];
626 CHECK_FALSE(val == pattern[0]);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000627}
628
629ASSERT_TEST(slot_buffer, ns_buffer_write_TC4)
630{
631 uintptr_t granule_addrs[2];
632 unsigned int cpuid;
633 union test_harness_cbs cb;
634 enum buffer_slot slot;
635
636 /******************************************************************
637 * TEST CASE 4:
638 *
639 * for a random CPU, try to call ns_buffer_write() with a
640 * random secure slot.
641 * ns_buffer_write() should cause an assertion failure.
642 ******************************************************************/
643
644 /* Register harness callbacks to use by this test */
645 cb.buffer_map = test_buffer_map_access;
646 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
647 cb.buffer_unmap = test_buffer_unmap_access;
648 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
649
650 /* Get two random granules, one for destination and one for source. */
651 get_rand_granule_array(granule_addrs, 2U);
652
653 /* Get a random slot. Secure slots are after SLOT_NS */
654 slot = (enum buffer_slot)test_helpers_get_rand_in_range(
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100655 (unsigned long)(SLOT_NS + 1U),
656 (unsigned long)NR_CPU_SLOTS);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000657
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100658 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
659 (unsigned long)(MAX_CPUS - 1U));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000660 host_util_set_cpuid(cpuid);
661
662 test_helpers_expect_assert_fail(true);
663 ns_buffer_write(slot, addr_to_granule(granule_addrs[0]), 0U,
664 (size_t)GRANULE_SIZE, (void *)granule_addrs[1]);
665 test_helpers_fail_if_no_assert_failed();
666}
667
668ASSERT_TEST(slot_buffer, ns_buffer_write_TC5)
669{
670 uintptr_t granule_addr;
671 unsigned int cpuid;
672 union test_harness_cbs cb;
673
674 /******************************************************************
675 * TEST CASE 5:
676 *
677 * for a random CPU, try to call ns_buffer_write() with a
678 * NULL pointer to copy from.
679 * ns_buffer_write() should cause an assertion failure.
680 ******************************************************************/
681
682 /* Register harness callbacks to use by this test */
683 cb.buffer_map = test_buffer_map_access;
684 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
685 cb.buffer_unmap = test_buffer_unmap_access;
686 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
687
688 granule_addr = get_rand_granule_addr();
689
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100690 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
691 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000692 host_util_set_cpuid(cpuid);
693
694 test_helpers_expect_assert_fail(true);
695 ns_buffer_write(SLOT_NS, addr_to_granule(granule_addr), 0U,
696 (size_t)GRANULE_SIZE, NULL);
697 test_helpers_fail_if_no_assert_failed();
698}
699
700ASSERT_TEST(slot_buffer, ns_buffer_write_TC6)
701{
702 uintptr_t granule_addr;
703 unsigned int cpuid;
704 union test_harness_cbs cb;
705
706 /******************************************************************
707 * TEST CASE 6:
708 *
709 * for a random CPU, try to call ns_buffer_write() with a
710 * NULL granule to topy to.
711 * ns_buffer_write() should cause an assertion failure.
712 ******************************************************************/
713
714 /* Register harness callbacks to use by this test */
715 cb.buffer_map = test_buffer_map_access;
716 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
717 cb.buffer_unmap = test_buffer_unmap_access;
718 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
719
720 granule_addr = get_rand_granule_addr();
721
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100722 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
723 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000724 host_util_set_cpuid(cpuid);
725
726 test_helpers_expect_assert_fail(true);
727 ns_buffer_write(SLOT_NS, NULL, 0U,
728 (size_t)GRANULE_SIZE, (void *)granule_addr);
729 test_helpers_fail_if_no_assert_failed();
730}
731
732ASSERT_TEST(slot_buffer, ns_buffer_write_TC7)
733{
734 uintptr_t granule_addrs[2];
735 unsigned int cpuid;
736 union test_harness_cbs cb;
737 size_t size;
738
739 /******************************************************************
740 * TEST CASE 7:
741 *
742 * for a random CPU, try to call ns_buffer_write() with a
743 * size not aligned to 8 bytes.
744 * ns_buffer_write() should cause an assertion failure.
745 ******************************************************************/
746
747 /* Register harness callbacks to use by this test */
748 cb.buffer_map = test_buffer_map_access;
749 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
750 cb.buffer_unmap = test_buffer_unmap_access;
751 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
752
753 /* Get two random granules, one for destination and one for source. */
754 get_rand_granule_array(granule_addrs, 2U);
755
756 /* Get a random size between 1 and 7 bytes */
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100757 size = (size_t)test_helpers_get_rand_in_range(1UL, 7UL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000758
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100759 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
760 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000761 host_util_set_cpuid(cpuid);
762
763 test_helpers_expect_assert_fail(true);
764 ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
765 size, (void *)granule_addrs[1]);
766 test_helpers_fail_if_no_assert_failed();
767}
768
769ASSERT_TEST(slot_buffer, ns_buffer_write_TC8)
770{
771 uintptr_t granule_addrs[2];
772 unsigned int cpuid;
773 union test_harness_cbs cb;
774 unsigned int offset;
775
776 /******************************************************************
777 * TEST CASE 8:
778 *
779 * for a random CPU, try to call ns_buffer_write() with an
780 * offset not aligned to 8 bytes.
781 * ns_buffer_write() should cause an assertion failure.
782 ******************************************************************/
783
784 /* Register harness callbacks to use by this test */
785 cb.buffer_map = test_buffer_map_access;
786 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
787 cb.buffer_unmap = test_buffer_unmap_access;
788 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
789
790 /* Get two random granules, one for destination and one for source. */
791 get_rand_granule_array(granule_addrs, 2U);
792
793 /* Get a random offset between 1 and 7 */
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100794 offset = (unsigned int)test_helpers_get_rand_in_range(1UL, 7UL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000795
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100796 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
797 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000798 host_util_set_cpuid(cpuid);
799
800 test_helpers_expect_assert_fail(true);
801 ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
802 GRANULE_SIZE, (void *)granule_addrs[1]);
803 test_helpers_fail_if_no_assert_failed();
804}
805
806ASSERT_TEST(slot_buffer, ns_buffer_write_TC9)
807{
808 uintptr_t granule_addrs[2];
809 unsigned int cpuid;
810 union test_harness_cbs cb;
811
812 /******************************************************************
813 * TEST CASE 9:
814 *
815 * for a random CPU, try to call ns_buffer_write() with an
816 * source not aligned to 8 bytes.
817 * ns_buffer_write() should cause an assertion failure.
818 ******************************************************************/
819
820 /* Register harness callbacks to use by this test */
821 cb.buffer_map = test_buffer_map_access;
822 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
823 cb.buffer_unmap = test_buffer_unmap_access;
824 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
825
826 /* Get two random granules, one for destination and one for source. */
827 get_rand_granule_array(granule_addrs, 2U);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100828
829 /*
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000830 * Misalign the address of the source.
831 * test_helpers_get_rand_in_range() will never return an address for
832 * the last granule, so we are safe increasing the address.
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100833 */
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100834 granule_addrs[1] += (unsigned int)test_helpers_get_rand_in_range(1UL,
835 7UL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000836
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100837 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
838 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000839 host_util_set_cpuid(cpuid);
840
841 test_helpers_expect_assert_fail(true);
842 ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
843 GRANULE_SIZE, (void *)granule_addrs[1]);
844 test_helpers_fail_if_no_assert_failed();
845}
846
847ASSERT_TEST(slot_buffer, ns_buffer_write_TC10)
848{
849 uintptr_t granule_addrs[2];
850 unsigned int cpuid;
851 size_t size;
852 unsigned int offset;
853 union test_harness_cbs cb;
854
855 /******************************************************************
856 * TEST CASE 10:
857 *
858 * for a random CPU, try to call ns_buffer_write() with an
859 * offset + size higher than GRANULE_SIZE.
860 * ns_buffer_write() should cause an assertion failure.
861 ******************************************************************/
862
863 /* Register harness callbacks to use by this test */
864 cb.buffer_map = test_buffer_map_access;
865 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
866 cb.buffer_unmap = test_buffer_unmap_access;
867 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
868
869 /* Get two random granules, one for destination and one for source. */
870 get_rand_granule_array(granule_addrs, 2U);
871
872 /*
873 * offset + granule = 1.5 * granule_size.
874 * Both parameters are properly aligned.
875 */
876 offset = GRANULE_SIZE >> 1U;
877 size = (size_t)GRANULE_SIZE;
878
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +0100879 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
880 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000881 host_util_set_cpuid(cpuid);
882
883 test_helpers_expect_assert_fail(true);
884 ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
885 size, (void *)granule_addrs[1]);
886 test_helpers_fail_if_no_assert_failed();
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100887}
888
889TEST(slot_buffer, ns_buffer_read_TC1)
890{
891 uintptr_t granule_addrs[3];
892 struct granule *test_granule;
893 union test_harness_cbs cb;
894
895 /******************************************************************
896 * TEST CASE 1:
897 *
898 * For each CPU, map a random granule to NS_SLOT and copy random
899 * data into it. Then verify that the data is properly read and
900 * that the source has not been altered.
901 ******************************************************************/
902
903 /* Register harness callbacks to use by this test */
904 cb.buffer_map = test_buffer_map_access;
905 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
906 cb.buffer_unmap = test_buffer_unmap_access;
907 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
908
909 /*
910 * Get three random granules:
911 * granule_addrs[0]: To be used as src for read operations (SLOT_NS).
912 * granule_addrs[1]: Will be the dst granule for the ns_buffer_read
913 * operation.
914 * granule_addrs[2]: Just a zeroed granule to easy some tests.
915 */
916 get_rand_granule_array(granule_addrs, 3U);
917
918 /* Granule to test zeroes */
919 (void)memset((void *)granule_addrs[2], 0, GRANULE_SIZE);
920
921 test_granule = addr_to_granule(granule_addrs[0]);
922
923 for (unsigned int i = 0U; i < MAX_CPUS; i++) {
924 host_util_set_cpuid(i);
925
926 /* Generate random data. */
927 for (unsigned int j = 0U; j < GRANULE_SIZE/sizeof(int); j++) {
928 *((int *)granule_addrs[0] + j) = rand();
929 }
930
931 /* Clean the dest granule */
932 (void)memset((void *)granule_addrs[1], 0, GRANULE_SIZE);
933
934 /*
935 * Read block by block, verifying that each copied block
936 * doesn't affect anything read before nor a block to be
937 * read yet.
938 */
939 for (unsigned int j = 0U; j < GRANULE_BLOCKS; j++) {
940 ns_buffer_read(SLOT_NS, test_granule,
941 GRANULE_BLOCK_SIZE * j,
942 GRANULE_BLOCK_SIZE,
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +0100943 (void *)(granule_addrs[1] +
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +0100944 (GRANULE_BLOCK_SIZE * j)));
945
946 MEMCMP_EQUAL((void *)granule_addrs[1],
947 (void *)granule_addrs[0],
948 (size_t)((j + 1U) * GRANULE_BLOCK_SIZE));
949
950 /*
951 * Verify than any block that has not been read yet
952 * is still all zeros.
953 */
954 MEMCMP_EQUAL((void *)granule_addrs[2],
955 (void *)(granule_addrs[1] +
956 ((j + 1U) * GRANULE_BLOCK_SIZE)),
957 (GRANULE_BLOCKS - (j + 1U)) *
958 GRANULE_BLOCK_SIZE);
959
960 }
961 }
962}
963
964TEST(slot_buffer, ns_buffer_read_TC2)
965{
966 uintptr_t granule_addrs[3];
967 struct granule *test_granule;
968 union test_harness_cbs cb;
969 int val;
970
971 /******************************************************************
972 * TEST CASE 3:
973 *
974 * For every CPU, verify that ns_buffer_read() does not alter the
975 * source.
976 ******************************************************************/
977
978 /* Register harness callbacks to use by this test */
979 cb.buffer_map = test_buffer_map_access;
980 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
981 cb.buffer_unmap = test_buffer_unmap_access;
982 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
983
984 /*
985 * Get three random granules:
986 * granule_addrs[0]: To be used as src for read operations (SLOT_NS).
987 * granule_addrs[1]: Will hold a copy of the src granule to compare.
988 * granule_addrs[2]: Destination granule.
989 */
990 get_rand_granule_array(granule_addrs, 3U);
991
992 /* Generate random data. */
993 for (unsigned int j = 0U; j < GRANULE_SIZE/sizeof(int); j++) {
994 val = rand();
995 *((int *)granule_addrs[0] + j) = val;
996 *((int *)granule_addrs[1] + j) = val;
997 }
998
999 test_granule = addr_to_granule(granule_addrs[0]);
1000
1001 for (unsigned int i = 0U; i < MAX_CPUS; i++) {
1002 host_util_set_cpuid(i);
1003
1004 ns_buffer_read(SLOT_NS, test_granule, 0U,
1005 GRANULE_SIZE, (void *)granule_addrs[2]);
1006
1007 /* Verify that the source has not been altered */
1008 MEMCMP_EQUAL((void *)granule_addrs[1],
1009 (void *)granule_addrs[0],
1010 (size_t)GRANULE_SIZE);
1011 }
1012
1013}
1014
1015TEST(slot_buffer, ns_buffer_read_TC3)
1016{
1017 uintptr_t granule_addrs[2];
1018 unsigned int cpu[2];
1019 long dest[2];
1020 long val;
1021 union test_harness_cbs cb;
1022
1023 /******************************************************************
1024 * TEST CASE 3:
1025 *
1026 * for two random CPUs, map a random granule with random data to
1027 * their SLOT_NS, then read the SLOT_NS on each CPU and ensure that
1028 * the destination buffers contain the data from their CPU SLOT_NS
1029 * only and no leak from the other CPU has happened.
1030 * This test helps validating that ns_buffer_read() handles the
1031 * translation contexts properly.
1032 ******************************************************************/
1033
1034 /* Register harness callbacks to use by this test */
1035 cb.buffer_map = test_buffer_map_access;
1036 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1037 cb.buffer_unmap = test_buffer_unmap_access;
1038 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1039
1040 /* Get a random granule for each CPU to use. */
1041 get_rand_granule_array(granule_addrs, 2U);
1042
1043 /* Get two random CPUs where to run the tests. */
1044 do {
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001045 cpu[0] = (unsigned int)test_helpers_get_rand_in_range(0UL,
1046 MAX_CPUS - 1U);
1047 cpu[1] = (unsigned int)test_helpers_get_rand_in_range(0UL,
1048 MAX_CPUS - 1U);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001049 } while (cpu[0] == cpu[1]);
1050
1051 /* Store random data at the beginning of each granule */
1052 *(long *)granule_addrs[0] = (long)rand();
1053 *(long *)granule_addrs[1] = (long)rand();
1054
1055 /* Read the granules and store the result in dest */
1056 for (unsigned int i = 0U; i < 2U; i++) {
1057 host_util_set_cpuid(cpu[i]);
1058
1059 ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[i]), 0U,
Javier Almansa Sobrino654ebf32023-06-13 10:51:39 +01001060 sizeof(long), (void *)&dest[i]);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001061 }
1062
1063 /*
1064 * Verify that the dest granule for the first CPU doesn't contain
1065 * the pattern for the second one.
1066 */
1067 val = *(long *)granule_addrs[0];
1068 CHECK_FALSE(val == dest[1]);
1069
1070 /*
1071 * Repeat the same check, this time with the second CPU.
1072 */
1073 val = *(long *)granule_addrs[1];
1074 CHECK_FALSE(val == dest[0]);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001075}
1076
1077ASSERT_TEST(slot_buffer, ns_buffer_read_TC4)
1078{
1079 uintptr_t granule_addrs[2];
1080 unsigned int cpuid;
1081 union test_harness_cbs cb;
1082 enum buffer_slot slot;
1083
1084 /******************************************************************
1085 * TEST CASE 4:
1086 *
1087 * for a random CPU, try to call ns_buffer_read() with a
1088 * random secure slot.
1089 * ns_buffer_read() should cause an assertion failure.
1090 ******************************************************************/
1091
1092 /* Register harness callbacks to use by this test */
1093 cb.buffer_map = test_buffer_map_access;
1094 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1095 cb.buffer_unmap = test_buffer_unmap_access;
1096 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1097
1098 /* Get two random granules, one for destination and one for source. */
1099 get_rand_granule_array(granule_addrs, 2U);
1100
1101 /* Get a random slot. Secure slots are after SLOT_NS */
1102 slot = (enum buffer_slot)test_helpers_get_rand_in_range(
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001103 (unsigned long)(SLOT_NS + 1U),
1104 (unsigned long)NR_CPU_SLOTS);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001105
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001106 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1107 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001108 host_util_set_cpuid(cpuid);
1109
1110 test_helpers_expect_assert_fail(true);
1111 ns_buffer_read(slot, addr_to_granule(granule_addrs[0]), 0U,
1112 (size_t)GRANULE_SIZE, (void *)granule_addrs[1]);
1113 test_helpers_fail_if_no_assert_failed();
1114}
1115
1116ASSERT_TEST(slot_buffer, ns_buffer_read_TC5)
1117{
1118 uintptr_t granule_addr;
1119 unsigned int cpuid;
1120 union test_harness_cbs cb;
1121
1122 /******************************************************************
1123 * TEST CASE 5:
1124 *
1125 * for a random CPU, try to call ns_buffer_read() with a
1126 * NULL pointer to copy to.
1127 * ns_buffer_read() should cause an assertion failure.
1128 ******************************************************************/
1129
1130 /* Register harness callbacks to use by this test */
1131 cb.buffer_map = test_buffer_map_access;
1132 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1133 cb.buffer_unmap = test_buffer_unmap_access;
1134 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1135
1136 granule_addr = get_rand_granule_addr();
1137
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001138 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1139 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001140 host_util_set_cpuid(cpuid);
1141
1142 test_helpers_expect_assert_fail(true);
1143 ns_buffer_read(SLOT_NS, addr_to_granule(granule_addr), 0U,
1144 (size_t)GRANULE_SIZE, NULL);
1145 test_helpers_fail_if_no_assert_failed();
1146}
1147
1148ASSERT_TEST(slot_buffer, ns_buffer_read_TC6)
1149{
1150 uintptr_t granule_addr;
1151 unsigned int cpuid;
1152 union test_harness_cbs cb;
1153
1154 /******************************************************************
1155 * TEST CASE 6:
1156 *
1157 * for a random CPU, try to call ns_buffer_read() with a
1158 * NULL granule to copy from.
1159 * ns_buffer_read() should cause an assertion failure.
1160 ******************************************************************/
1161
1162 /* Register harness callbacks to use by this test */
1163 cb.buffer_map = test_buffer_map_access;
1164 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1165 cb.buffer_unmap = test_buffer_unmap_access;
1166 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1167
1168 granule_addr = get_rand_granule_addr();
1169
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001170 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1171 (unsigned long)(MAX_CPUS - 1U));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001172 host_util_set_cpuid(cpuid);
1173
1174 test_helpers_expect_assert_fail(true);
1175 ns_buffer_read(SLOT_NS, NULL, 0U,
1176 (size_t)GRANULE_SIZE, (void *)granule_addr);
1177 test_helpers_fail_if_no_assert_failed();
1178}
1179
1180ASSERT_TEST(slot_buffer, ns_buffer_read_TC7)
1181{
1182 uintptr_t granule_addrs[2];
1183 unsigned int cpuid;
1184 union test_harness_cbs cb;
1185 size_t size;
1186
1187 /******************************************************************
1188 * TEST CASE 7:
1189 *
1190 * for a random CPU, try to call ns_buffer_read() with a
1191 * size not aligned to 8 bytes.
1192 * ns_buffer_read() should cause an assertion failure.
1193 ******************************************************************/
1194
1195 /* Register harness callbacks to use by this test */
1196 cb.buffer_map = test_buffer_map_access;
1197 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1198 cb.buffer_unmap = test_buffer_unmap_access;
1199 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1200
1201 /* Get two random granules, one for destination and one for source. */
1202 get_rand_granule_array(granule_addrs, 2U);
1203
1204 /* Get a random size between 1 and 7 bytes */
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001205 size = (size_t)test_helpers_get_rand_in_range(1UL, 7UL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001206
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001207 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1208 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001209 host_util_set_cpuid(cpuid);
1210
1211 test_helpers_expect_assert_fail(true);
1212 ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
1213 size, (void *)granule_addrs[1]);
1214 test_helpers_fail_if_no_assert_failed();
1215}
1216
1217ASSERT_TEST(slot_buffer, ns_buffer_read_TC8)
1218{
1219 uintptr_t granule_addrs[2];
1220 unsigned int cpuid;
1221 union test_harness_cbs cb;
1222 unsigned int offset;
1223
1224 /******************************************************************
1225 * TEST CASE 8:
1226 *
1227 * for a random CPU, try to call ns_buffer_read() with an
1228 * offset not aligned to 8 bytes.
1229 * ns_buffer_read() should cause an assertion failure.
1230 ******************************************************************/
1231
1232 /* Register harness callbacks to use by this test */
1233 cb.buffer_map = test_buffer_map_access;
1234 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1235 cb.buffer_unmap = test_buffer_unmap_access;
1236 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1237
1238 /* Get two random granules, one for destination and one for source. */
1239 get_rand_granule_array(granule_addrs, 2U);
1240
1241 /* Get a random offset between 1 and 7 */
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001242 offset = (unsigned int)test_helpers_get_rand_in_range(1UL, 7UL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001243
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001244 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1245 (unsigned long)(MAX_CPUS - 1U));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001246 host_util_set_cpuid(cpuid);
1247
1248 test_helpers_expect_assert_fail(true);
1249 ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
1250 GRANULE_SIZE, (void *)granule_addrs[1]);
1251 test_helpers_fail_if_no_assert_failed();
1252}
1253
1254ASSERT_TEST(slot_buffer, ns_buffer_read_TC9)
1255{
1256 uintptr_t granule_addrs[2];
1257 unsigned int cpuid;
1258 union test_harness_cbs cb;
1259
1260 /******************************************************************
1261 * TEST CASE 9:
1262 *
1263 * for a random CPU, try to call ns_buffer_read() with a
1264 * destination not aligned to 8 bytes.
1265 * ns_buffer_read() should cause an assertion failure.
1266 ******************************************************************/
1267
1268 /* Register harness callbacks to use by this test */
1269 cb.buffer_map = test_buffer_map_access;
1270 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1271 cb.buffer_unmap = test_buffer_unmap_access;
1272 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1273
1274 /* Get two random granules, one for destination and one for source. */
1275 get_rand_granule_array(granule_addrs, 2U);
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001276
1277 /*
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001278 * Misalign the address of the destination.
1279 * test_helpers_get_rand_in_range() will never return an address for
1280 * the last granule, so we are safe increasing the address.
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001281 */
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001282 granule_addrs[1] += test_helpers_get_rand_in_range(1UL, 7UL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001283
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001284 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1285 MAX_CPUS - 1U);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001286 host_util_set_cpuid(cpuid);
1287
1288 test_helpers_expect_assert_fail(true);
1289 ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
1290 GRANULE_SIZE, (void *)granule_addrs[1]);
1291 test_helpers_fail_if_no_assert_failed();
1292}
1293
1294ASSERT_TEST(slot_buffer, ns_buffer_read_TC10)
1295{
1296 uintptr_t granule_addrs[2];
1297 unsigned int cpuid;
1298 size_t size;
1299 unsigned int offset;
1300 union test_harness_cbs cb;
1301
1302 /******************************************************************
1303 * TEST CASE 10:
1304 *
1305 * for a random CPU, try to call ns_buffer_read() with an
1306 * offset + size higher than GRANULE_SIZE.
1307 * ns_buffer_read() should cause an assertion failure.
1308 ******************************************************************/
1309
1310 /* Register harness callbacks to use by this test */
1311 cb.buffer_map = test_buffer_map_access;
1312 (void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
1313 cb.buffer_unmap = test_buffer_unmap_access;
1314 (void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
1315
1316 /* Get two random granules, one for destination and one for source. */
1317 get_rand_granule_array(granule_addrs, 2U);
1318
1319 /*
1320 * offset + granule = 1.5 * granule_size.
1321 * Both parameters are properly aligned.
1322 */
1323 offset = GRANULE_SIZE >> 1U;
1324 size = (size_t)GRANULE_SIZE;
1325
Javier Almansa Sobrinoe627b4d2023-10-12 11:25:08 +01001326 cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
1327 (unsigned long)(MAX_CPUS - 1U));
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +00001328 host_util_set_cpuid(cpuid);
1329
1330 test_helpers_expect_assert_fail(true);
1331 ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
1332 size, (void *)granule_addrs[1]);
1333 test_helpers_fail_if_no_assert_failed();
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001334}
1335
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +00001336TEST(slot_buffer, slot_buf_finish_warmboot_init_TC1)
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001337{
1338 /*
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +00001339 * slot_buf_finish_warmboot_init() has already been used during
1340 * initialization for all tests, so skip it.
Javier Almansa Sobrinoed0ffd22022-07-25 09:35:32 +01001341 */
1342}