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