Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 7 | #include <plat/common/platform.h> |
| 8 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 9 | #include <arch.h> |
| 10 | #include <arch_helpers.h> |
| 11 | #include <arch_features.h> |
| 12 | #include <debug.h> |
| 13 | #ifdef __aarch64__ |
| 14 | #include <sync.h> |
| 15 | #endif |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 16 | #include <host_realm_helper.h> |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 17 | #include <lib/aarch64/arch_features.h> |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 18 | #include <test_helpers.h> |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 19 | #include <tftf_lib.h> |
Manish Pandey | 368054b | 2022-03-15 13:24:59 +0000 | [diff] [blame] | 20 | #include <xlat_tables_v2.h> |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 21 | #include <platform_def.h> |
nabkah01 | 46b418d | 2022-02-16 17:01:54 +0000 | [diff] [blame] | 22 | #include <cactus_test_cmds.h> |
| 23 | #include <ffa_endpoints.h> |
| 24 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 25 | /* |
| 26 | * Using "__aarch64__" here looks weird but its unavoidable because of following reason |
| 27 | * This test is part of standard test which runs on all platforms but pre-requisite |
| 28 | * to run this test (custom sync exception handler) is only implemented for aarch64. |
| 29 | * TODO: Write a framework so that tests kept in standard list can be selectively |
| 30 | * run on a given architecture |
| 31 | */ |
| 32 | #ifdef __aarch64__ |
| 33 | |
nabkah01 | 46b418d | 2022-02-16 17:01:54 +0000 | [diff] [blame] | 34 | #define SENDER HYP_ID |
| 35 | #define RECEIVER SP_ID(1) |
| 36 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 37 | static volatile bool sync_exception_triggered; |
| 38 | static volatile bool data_abort_triggered; |
nabkah01 | 46b418d | 2022-02-16 17:01:54 +0000 | [diff] [blame] | 39 | static const struct ffa_uuid expected_sp_uuids[] = { |
| 40 | {PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID} |
| 41 | }; |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 42 | |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 43 | static __aligned(PAGE_SIZE) uint64_t share_page[PAGE_SIZE / sizeof(uint64_t)]; |
| 44 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 45 | static bool data_abort_handler(void) |
| 46 | { |
| 47 | uint64_t esr_elx = IS_IN_EL2() ? read_esr_el2() : read_esr_el1(); |
| 48 | unsigned int rme_supported = get_armv9_2_feat_rme_support(); |
| 49 | |
| 50 | sync_exception_triggered = true; |
| 51 | |
| 52 | VERBOSE("%s esr_elx %llx\n", __func__, esr_elx); |
| 53 | |
| 54 | if (EC_BITS(esr_elx) == EC_DABORT_CUR_EL) { |
| 55 | if (rme_supported == 0) { |
| 56 | /* Synchronous external data abort triggered by trustzone controller */ |
| 57 | if ((ISS_BITS(esr_elx) & ISS_DFSC_MASK) == DFSC_EXT_DABORT) { |
| 58 | VERBOSE("%s TZC Data Abort caught\n", __func__); |
| 59 | data_abort_triggered = true; |
| 60 | return true; |
| 61 | } |
| 62 | } else { |
| 63 | /* Synchronous data abort triggered by Granule protection */ |
| 64 | if ((ISS_BITS(esr_elx) & ISS_DFSC_MASK) == DFSC_GPF_DABORT) { |
| 65 | VERBOSE("%s GPF Data Abort caught\n", __func__); |
| 66 | data_abort_triggered = true; |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
nabkah01 | bf00f8d | 2022-03-22 12:37:19 +0000 | [diff] [blame] | 75 | test_result_t el3_memory_cannot_be_accessed_in_ns(void) |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 76 | { |
| 77 | const uintptr_t test_address = EL3_MEMORY_ACCESS_ADDR; |
| 78 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 79 | VERBOSE("Attempt to access el3 memory (0x%lx)\n", test_address); |
| 80 | |
Manish Pandey | 368054b | 2022-03-15 13:24:59 +0000 | [diff] [blame] | 81 | sync_exception_triggered = false; |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 82 | data_abort_triggered = false; |
Manish Pandey | 368054b | 2022-03-15 13:24:59 +0000 | [diff] [blame] | 83 | |
| 84 | int rc = mmap_add_dynamic_region(test_address, test_address, PAGE_SIZE, |
| 85 | MT_MEMORY | MT_RW | MT_NS); |
| 86 | if (rc != 0) { |
| 87 | tftf_testcase_printf("%d: mmap_add_dynamic_region() = %d\n", __LINE__, rc); |
| 88 | return TEST_RESULT_FAIL; |
| 89 | } |
| 90 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 91 | register_custom_sync_exception_handler(data_abort_handler); |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 92 | *((volatile uint64_t *)test_address); |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 93 | unregister_custom_sync_exception_handler(); |
| 94 | |
Manish Pandey | 368054b | 2022-03-15 13:24:59 +0000 | [diff] [blame] | 95 | rc = mmap_remove_dynamic_region(test_address, PAGE_SIZE); |
| 96 | if (rc != 0) { |
| 97 | tftf_testcase_printf("%d: mmap_remove_dynamic_region() = %d\n", __LINE__, rc); |
| 98 | return TEST_RESULT_FAIL; |
| 99 | } |
| 100 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 101 | if (sync_exception_triggered == false) { |
| 102 | tftf_testcase_printf("No sync exception while accessing (0x%lx)\n", test_address); |
| 103 | return TEST_RESULT_SKIPPED; |
| 104 | } |
| 105 | |
| 106 | if (data_abort_triggered == false) { |
| 107 | tftf_testcase_printf("Sync exception is not data abort\n"); |
| 108 | return TEST_RESULT_FAIL; |
| 109 | } |
| 110 | |
| 111 | return TEST_RESULT_SUCCESS; |
| 112 | } |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * @Test_Aim@ Check a realm region cannot be accessed from normal world. |
| 116 | * |
| 117 | * This test delegates a TFTF allocated buffer to Realm. It then attempts |
| 118 | * a read access to the region from normal world. This results in the PE |
| 119 | * triggering a GPF caught by a custom synchronous abort handler. |
| 120 | * |
| 121 | */ |
| 122 | test_result_t rl_memory_cannot_be_accessed_in_ns(void) |
| 123 | { |
| 124 | test_result_t result = TEST_RESULT_FAIL; |
| 125 | u_register_t retmm; |
| 126 | |
| 127 | if (get_armv9_2_feat_rme_support() == 0U) { |
| 128 | return TEST_RESULT_SKIPPED; |
| 129 | } |
| 130 | |
| 131 | sync_exception_triggered = false; |
| 132 | data_abort_triggered = false; |
| 133 | register_custom_sync_exception_handler(data_abort_handler); |
| 134 | |
| 135 | /* First read access to the test region must not fail. */ |
| 136 | *((volatile uint64_t *)share_page); |
| 137 | |
| 138 | if ((sync_exception_triggered != false) || |
| 139 | (data_abort_triggered != false)) { |
| 140 | goto out_unregister; |
| 141 | } |
| 142 | |
| 143 | /* Delegate the shared page to Realm. */ |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 144 | retmm = rmi_granule_delegate((u_register_t)&share_page); |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 145 | if (retmm != 0UL) { |
| 146 | ERROR("Granule delegate failed!\n"); |
| 147 | goto out_unregister; |
| 148 | } |
| 149 | |
| 150 | /* This access shall trigger a GPF. */ |
| 151 | *((volatile uint64_t *)share_page); |
| 152 | |
| 153 | if ((sync_exception_triggered != true) || |
| 154 | (data_abort_triggered != true)) { |
| 155 | goto out_undelegate; |
| 156 | } |
| 157 | |
| 158 | result = TEST_RESULT_SUCCESS; |
| 159 | |
| 160 | out_undelegate: |
| 161 | /* Undelegate the shared page. */ |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 162 | retmm = rmi_granule_undelegate((u_register_t)&share_page); |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 163 | if (retmm != 0UL) { |
| 164 | ERROR("Granule undelegate failed!\n"); |
| 165 | } |
| 166 | |
| 167 | out_unregister: |
| 168 | unregister_custom_sync_exception_handler(); |
| 169 | |
| 170 | return result; |
| 171 | } |
| 172 | |
nabkah01 | bf00f8d | 2022-03-22 12:37:19 +0000 | [diff] [blame] | 173 | /** |
| 174 | * @Test_Aim@ Check a secure region cannot be accessed from normal world. |
| 175 | * |
| 176 | * Following test intends to run on RME enabled platforms when EL3 |
| 177 | * is Root world. In a non RME platform, EL3 is secure. |
| 178 | * Access to secure memory from NS world is already covered |
| 179 | * by el3_memory_cannot_be_accessed_in_ns. |
| 180 | */ |
| 181 | test_result_t s_memory_cannot_be_accessed_in_ns(void) |
| 182 | { |
| 183 | const uintptr_t test_address = SECURE_MEMORY_ACCESS_ADDR; |
| 184 | |
| 185 | /* skipp non RME platforms */ |
| 186 | if (get_armv9_2_feat_rme_support() == 0U) { |
| 187 | return TEST_RESULT_SKIPPED; |
| 188 | } |
| 189 | |
| 190 | VERBOSE("Attempt to access secure memory (0x%lx)\n", test_address); |
| 191 | |
| 192 | data_abort_triggered = false; |
| 193 | sync_exception_triggered = false; |
| 194 | register_custom_sync_exception_handler(data_abort_handler); |
| 195 | dsbsy(); |
| 196 | |
| 197 | int rc = mmap_add_dynamic_region(test_address, test_address, PAGE_SIZE, |
| 198 | MT_MEMORY | MT_RW | MT_NS); |
| 199 | |
| 200 | if (rc != 0) { |
| 201 | tftf_testcase_printf("%d: mmap_add_dynamic_region() = %d\n", __LINE__, rc); |
| 202 | return TEST_RESULT_FAIL; |
| 203 | } |
| 204 | |
| 205 | *((volatile uint64_t *)test_address); |
| 206 | |
| 207 | mmap_remove_dynamic_region(test_address, PAGE_SIZE); |
| 208 | |
| 209 | dsbsy(); |
| 210 | unregister_custom_sync_exception_handler(); |
| 211 | |
| 212 | if (sync_exception_triggered == false) { |
| 213 | tftf_testcase_printf("No sync exception while accessing (0x%lx)\n", test_address); |
| 214 | return TEST_RESULT_SKIPPED; |
| 215 | } |
| 216 | |
| 217 | if (data_abort_triggered == false) { |
| 218 | tftf_testcase_printf("Sync exception is not data abort\n"); |
| 219 | return TEST_RESULT_FAIL; |
| 220 | } |
| 221 | |
| 222 | return TEST_RESULT_SUCCESS; |
| 223 | } |
| 224 | |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 225 | static test_result_t memory_cannot_be_accessed_in_rl(u_register_t params) |
| 226 | { |
| 227 | u_register_t retrmm; |
| 228 | static char rd[GRANULE_SIZE] __aligned(GRANULE_SIZE); |
| 229 | |
| 230 | if (get_armv9_2_feat_rme_support() == 0U) { |
| 231 | return TEST_RESULT_SKIPPED; |
| 232 | } |
| 233 | |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 234 | retrmm = rmi_version(); |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 235 | |
| 236 | VERBOSE("RMM version is: %lu.%lu\n", |
| 237 | RMI_ABI_VERSION_GET_MAJOR(retrmm), |
| 238 | RMI_ABI_VERSION_GET_MINOR(retrmm)); |
| 239 | |
| 240 | /* |
| 241 | * TODO: Remove this once SMC_RMM_REALM_CREATE is implemented in TRP |
| 242 | * For the moment skip the test if RMM is TRP, TRP version is always null. |
| 243 | */ |
| 244 | if (retrmm == 0U) { |
| 245 | return TEST_RESULT_SKIPPED; |
| 246 | } |
| 247 | |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 248 | retrmm = rmi_granule_delegate((u_register_t)&rd[0]); |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 249 | if (retrmm != 0UL) { |
| 250 | ERROR("Delegate operation returns fail, %lx\n", retrmm); |
| 251 | return TEST_RESULT_FAIL; |
| 252 | } |
| 253 | |
| 254 | /* Create a realm using a parameter in a secure physical address space should fail. */ |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 255 | retrmm = rmi_realm_create((u_register_t)&rd[0], params); |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 256 | if (retrmm == 0UL) { |
| 257 | ERROR("Realm create operation should fail, %lx\n", retrmm); |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 258 | retrmm = rmi_realm_destroy((u_register_t)&rd[0]); |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 259 | if (retrmm != 0UL) { |
| 260 | ERROR("Realm destroy operation returns fail, %lx\n", retrmm); |
| 261 | return TEST_RESULT_FAIL; |
| 262 | } |
| 263 | return TEST_RESULT_FAIL; |
| 264 | } else if (retrmm != RMM_STATUS_ERROR_INPUT) { |
| 265 | ERROR("Realm create operation should fail with code:%ld retrmm:%ld\n", |
| 266 | RMM_STATUS_ERROR_INPUT, retrmm); |
| 267 | return TEST_RESULT_FAIL; |
| 268 | } |
| 269 | |
nabkah01 | 002e569 | 2022-10-10 12:36:46 +0100 | [diff] [blame^] | 270 | retrmm = rmi_granule_undelegate((u_register_t)&rd[0]); |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 271 | if (retrmm != 0UL) { |
| 272 | INFO("Undelegate operation returns fail, %lx\n", retrmm); |
| 273 | return TEST_RESULT_FAIL; |
| 274 | } |
| 275 | |
| 276 | return TEST_RESULT_SUCCESS; |
| 277 | } |
| 278 | |
nabkah01 | 46b418d | 2022-02-16 17:01:54 +0000 | [diff] [blame] | 279 | /** |
| 280 | * @Test_Aim@ Check a root region cannot be accessed from a secure partition. |
| 281 | * |
| 282 | * This change adds TFTF and cactus test to permit checking a root region |
| 283 | * cannot be accessed from secure world. |
| 284 | * A hardcoded address marked Root in the GPT is shared to a secure |
| 285 | * partition. The SP retrieves the region from the SPM, maps it and |
| 286 | * attempts a read access to the region. It is expected to trigger a GPF |
| 287 | * data abort on the PE caught by a custom exception handler. |
| 288 | * |
| 289 | */ |
| 290 | test_result_t rt_memory_cannot_be_accessed_in_s(void) |
| 291 | { |
| 292 | const uintptr_t test_address = EL3_MEMORY_ACCESS_ADDR; |
| 293 | struct ffa_memory_region_constituent constituents[] = { |
| 294 | { |
| 295 | (void *)test_address, 1, 0 |
| 296 | } |
| 297 | }; |
| 298 | const uint32_t constituents_count = sizeof(constituents) / |
| 299 | sizeof(struct ffa_memory_region_constituent); |
| 300 | ffa_memory_handle_t handle; |
| 301 | struct mailbox_buffers mb; |
Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 302 | struct ffa_value ret; |
nabkah01 | 46b418d | 2022-02-16 17:01:54 +0000 | [diff] [blame] | 303 | |
| 304 | if (get_armv9_2_feat_rme_support() == 0U) { |
| 305 | return TEST_RESULT_SKIPPED; |
| 306 | } |
| 307 | |
| 308 | INIT_TFTF_MAILBOX(mb); |
| 309 | |
| 310 | CHECK_SPMC_TESTING_SETUP(1, 1, expected_sp_uuids); |
| 311 | |
| 312 | GET_TFTF_MAILBOX(mb); |
| 313 | |
| 314 | handle = memory_init_and_send((struct ffa_memory_region *)mb.send, |
| 315 | PAGE_SIZE, SENDER, RECEIVER, |
| 316 | constituents, constituents_count, |
| 317 | FFA_MEM_SHARE_SMC32, &ret); |
| 318 | |
| 319 | if (handle == FFA_MEMORY_HANDLE_INVALID) { |
| 320 | return TEST_RESULT_FAIL; |
| 321 | } |
| 322 | |
| 323 | VERBOSE("TFTF - Handle: %llx Address: %p\n", |
| 324 | handle, constituents[0].address); |
| 325 | |
| 326 | /* Retrieve the shared page and attempt accessing it. */ |
| 327 | ret = cactus_mem_send_cmd(SENDER, RECEIVER, FFA_MEM_SHARE_SMC32, |
| 328 | handle, 0, true, 1); |
| 329 | |
| 330 | if (is_ffa_call_error(ffa_mem_reclaim(handle, 0))) { |
| 331 | ERROR("Memory reclaim failed!\n"); |
| 332 | return TEST_RESULT_FAIL; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Expect success response with value 1 hinting an exception |
| 337 | * triggered while the SP accessed the region. |
| 338 | */ |
| 339 | if (!(cactus_get_response(ret) == CACTUS_SUCCESS && |
| 340 | cactus_error_code(ret) == 1)) { |
| 341 | ERROR("Exceptions test failed!\n"); |
| 342 | return TEST_RESULT_FAIL; |
| 343 | } |
| 344 | |
| 345 | return TEST_RESULT_SUCCESS; |
| 346 | } |
| 347 | |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 348 | test_result_t s_memory_cannot_be_accessed_in_rl(void) |
| 349 | { |
| 350 | u_register_t params = (u_register_t)SECURE_MEMORY_ACCESS_ADDR; |
| 351 | return memory_cannot_be_accessed_in_rl(params); |
| 352 | } |
| 353 | |
nabkah01 | e714786 | 2022-02-11 19:08:00 +0000 | [diff] [blame] | 354 | test_result_t rt_memory_cannot_be_accessed_in_rl(void) |
| 355 | { |
| 356 | u_register_t params = (u_register_t)EL3_MEMORY_ACCESS_ADDR; |
| 357 | return memory_cannot_be_accessed_in_rl(params); |
| 358 | } |
| 359 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 360 | #else |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 361 | |
nabkah01 | bf00f8d | 2022-03-22 12:37:19 +0000 | [diff] [blame] | 362 | test_result_t el3_memory_cannot_be_accessed_in_ns(void) |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 363 | { |
| 364 | tftf_testcase_printf("Test not ported to AArch32\n"); |
| 365 | return TEST_RESULT_SKIPPED; |
| 366 | } |
Olivier Deprez | 82bbc57 | 2022-01-11 11:50:36 +0100 | [diff] [blame] | 367 | |
| 368 | test_result_t rl_memory_cannot_be_accessed_in_ns(void) |
| 369 | { |
| 370 | tftf_testcase_printf("Test not ported to AArch32\n"); |
| 371 | return TEST_RESULT_SKIPPED; |
| 372 | } |
| 373 | |
nabkah01 | bf00f8d | 2022-03-22 12:37:19 +0000 | [diff] [blame] | 374 | test_result_t s_memory_cannot_be_accessed_in_ns(void) |
| 375 | { |
| 376 | tftf_testcase_printf("Test not ported to AArch32\n"); |
| 377 | return TEST_RESULT_SKIPPED; |
| 378 | } |
nabkah01 | 905b2c7 | 2022-02-10 10:46:32 +0000 | [diff] [blame] | 379 | |
| 380 | test_result_t s_memory_cannot_be_accessed_in_rl(void) |
| 381 | { |
| 382 | tftf_testcase_printf("Test not ported to AArch32\n"); |
| 383 | return TEST_RESULT_SKIPPED; |
| 384 | } |
| 385 | |
nabkah01 | e714786 | 2022-02-11 19:08:00 +0000 | [diff] [blame] | 386 | test_result_t rt_memory_cannot_be_accessed_in_rl(void) |
| 387 | { |
| 388 | tftf_testcase_printf("Test not ported to AArch32\n"); |
| 389 | return TEST_RESULT_SKIPPED; |
| 390 | } |
| 391 | |
Manish Pandey | 5f513d6 | 2022-01-17 11:05:53 +0000 | [diff] [blame] | 392 | #endif /* __aarch64__ */ |