J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 1 | /* |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | #include <assert.h> |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 7 | #include <debug.h> |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 9 | |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 10 | #include <cactus_def.h> |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 11 | #include <cactus_platform_def.h> |
J-Alves | 8f4a56f | 2020-10-28 10:29:05 +0000 | [diff] [blame] | 12 | #include <ffa_endpoints.h> |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 13 | #include <sp_helpers.h> |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 14 | #include <spm_common.h> |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 15 | |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 16 | #include <lib/libc/string.h> |
| 17 | #include <lib/xlat_tables/xlat_tables_v2.h> |
| 18 | |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 19 | /* FFA version test helpers */ |
| 20 | #define FFA_MAJOR 1U |
| 21 | #define FFA_MINOR 0U |
| 22 | |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 23 | static const uint32_t primary_uuid[4] = PRIMARY_UUID; |
| 24 | static const uint32_t secondary_uuid[4] = SECONDARY_UUID; |
Ruari Phipps | d75596a | 2020-07-17 16:41:34 +0100 | [diff] [blame] | 25 | static const uint32_t tertiary_uuid[4] = TERTIARY_UUID; |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 26 | static const uint32_t null_uuid[4] = {0}; |
| 27 | |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Test FFA_FEATURES interface. |
| 30 | */ |
| 31 | static void ffa_features_test(void) |
| 32 | { |
| 33 | const char *test_features = "FFA Features interface"; |
| 34 | smc_ret_values ffa_ret; |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 35 | const struct ffa_features_test *ffa_feature_test_target; |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 36 | unsigned int i, test_target_size = |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 37 | get_ffa_feature_test_target(&ffa_feature_test_target); |
| 38 | |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 39 | |
| 40 | announce_test_section_start(test_features); |
| 41 | |
| 42 | for (i = 0U; i < test_target_size; i++) { |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 43 | announce_test_start(ffa_feature_test_target[i].test_name); |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 44 | |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 45 | ffa_ret = ffa_features(ffa_feature_test_target[i].feature); |
| 46 | expect(ffa_ret.ret0, ffa_feature_test_target[i].expected_ret); |
| 47 | if (ffa_feature_test_target[i].expected_ret == FFA_ERROR) { |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 48 | expect(ffa_ret.ret2, FFA_ERROR_NOT_SUPPORTED); |
| 49 | } |
| 50 | |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame^] | 51 | announce_test_end(ffa_feature_test_target[i].test_name); |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | announce_test_section_end(test_features); |
| 55 | } |
| 56 | |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 57 | static void ffa_partition_info_helper(struct mailbox_buffers *mb, const uint32_t uuid[4], |
| 58 | const struct ffa_partition_info *expected, |
| 59 | const uint16_t expected_size) |
| 60 | { |
| 61 | smc_ret_values ret = ffa_partition_info_get(uuid); |
| 62 | unsigned int i; |
| 63 | expect(ret.ret0, FFA_SUCCESS_SMC32); |
| 64 | |
| 65 | struct ffa_partition_info *info = (struct ffa_partition_info *)(mb->recv); |
| 66 | for (i = 0U; i < expected_size; i++) { |
| 67 | expect(info[i].id, expected[i].id); |
| 68 | expect(info[i].exec_context, expected[i].exec_context); |
| 69 | expect(info[i].properties, expected[i].properties); |
| 70 | } |
| 71 | |
| 72 | ret = ffa_rx_release(); |
| 73 | expect(ret.ret0, FFA_SUCCESS_SMC32); |
| 74 | } |
| 75 | |
| 76 | static void ffa_partition_info_wrong_test(void) |
| 77 | { |
| 78 | const char *test_wrong_uuid = "Request wrong UUID"; |
| 79 | uint32_t uuid[4] = {1}; |
| 80 | |
| 81 | announce_test_start(test_wrong_uuid); |
| 82 | |
| 83 | smc_ret_values ret = ffa_partition_info_get(uuid); |
| 84 | expect(ret.ret0, FFA_ERROR); |
| 85 | expect(ret.ret2, FFA_ERROR_INVALID_PARAMETER); |
| 86 | |
| 87 | announce_test_end(test_wrong_uuid); |
| 88 | } |
| 89 | |
| 90 | static void ffa_partition_info_get_test(struct mailbox_buffers *mb) |
| 91 | { |
| 92 | const char *test_partition_info = "FFA Partition info interface"; |
| 93 | const char *test_primary = "Get primary partition info"; |
| 94 | const char *test_secondary = "Get secondary partition info"; |
Ruari Phipps | d75596a | 2020-07-17 16:41:34 +0100 | [diff] [blame] | 95 | const char *test_tertiary = "Get tertiary partition info"; |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 96 | const char *test_all = "Get all partitions info"; |
| 97 | |
| 98 | const struct ffa_partition_info expected_info[] = { |
Arunachalam Ganapathy | 51be1fe | 2020-09-22 13:25:21 +0100 | [diff] [blame] | 99 | /* Primary partition info */ |
| 100 | { |
| 101 | .id = SPM_VM_ID_FIRST, |
| 102 | .exec_context = CACTUS_PRIMARY_EC_COUNT, |
Olivier Deprez | 6d274a4 | 2020-10-23 09:12:27 +0200 | [diff] [blame] | 103 | /* Supports receipt of direct message requests. */ |
| 104 | .properties = 1U |
Arunachalam Ganapathy | 51be1fe | 2020-09-22 13:25:21 +0100 | [diff] [blame] | 105 | }, |
| 106 | /* Secondary partition info */ |
| 107 | { |
| 108 | .id = SPM_VM_ID_FIRST + 1U, |
| 109 | .exec_context = CACTUS_SECONDARY_EC_COUNT, |
Olivier Deprez | 6d274a4 | 2020-10-23 09:12:27 +0200 | [diff] [blame] | 110 | .properties = 1U |
Arunachalam Ganapathy | 51be1fe | 2020-09-22 13:25:21 +0100 | [diff] [blame] | 111 | }, |
| 112 | /* Tertiary partition info */ |
| 113 | { |
| 114 | .id = SPM_VM_ID_FIRST + 2U, |
| 115 | .exec_context = CACTUS_TERTIARY_EC_COUNT, |
Olivier Deprez | 6d274a4 | 2020-10-23 09:12:27 +0200 | [diff] [blame] | 116 | .properties = 1U |
Arunachalam Ganapathy | 51be1fe | 2020-09-22 13:25:21 +0100 | [diff] [blame] | 117 | } |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | announce_test_section_start(test_partition_info); |
| 121 | |
Ruari Phipps | d75596a | 2020-07-17 16:41:34 +0100 | [diff] [blame] | 122 | announce_test_start(test_tertiary); |
| 123 | ffa_partition_info_helper(mb, tertiary_uuid, &expected_info[2], 1); |
| 124 | announce_test_end(test_tertiary); |
| 125 | |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 126 | announce_test_start(test_secondary); |
| 127 | ffa_partition_info_helper(mb, secondary_uuid, &expected_info[1], 1); |
| 128 | announce_test_end(test_secondary); |
| 129 | |
| 130 | announce_test_start(test_primary); |
| 131 | ffa_partition_info_helper(mb, primary_uuid, &expected_info[0], 1); |
| 132 | announce_test_end(test_primary); |
| 133 | |
| 134 | announce_test_start(test_all); |
Ruari Phipps | d75596a | 2020-07-17 16:41:34 +0100 | [diff] [blame] | 135 | ffa_partition_info_helper(mb, null_uuid, expected_info, 3); |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 136 | announce_test_end(test_all); |
| 137 | |
| 138 | ffa_partition_info_wrong_test(); |
| 139 | |
| 140 | announce_test_section_end(test_partition_info); |
| 141 | } |
| 142 | |
Max Shvetsov | 57c6ddb | 2020-07-01 14:09:48 +0100 | [diff] [blame] | 143 | void ffa_version_test(void) |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 144 | { |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 145 | const char *test_ffa_version = "FFA Version interface"; |
| 146 | |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 147 | announce_test_start(test_ffa_version); |
| 148 | |
| 149 | smc_ret_values ret = ffa_version(MAKE_FFA_VERSION(FFA_MAJOR, FFA_MINOR)); |
Max Shvetsov | 57c6ddb | 2020-07-01 14:09:48 +0100 | [diff] [blame] | 150 | uint32_t spm_version = (uint32_t)ret.ret0; |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 151 | |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 152 | bool ffa_version_compatible = |
| 153 | ((spm_version >> FFA_VERSION_MAJOR_SHIFT) == FFA_MAJOR && |
| 154 | (spm_version & FFA_VERSION_MINOR_MASK) >= FFA_MINOR); |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 155 | |
| 156 | NOTICE("FFA_VERSION returned %u.%u; Compatible: %i\n", |
| 157 | spm_version >> FFA_VERSION_MAJOR_SHIFT, |
| 158 | spm_version & FFA_VERSION_MINOR_MASK, |
| 159 | (int)ffa_version_compatible); |
| 160 | |
| 161 | expect((int)ffa_version_compatible, (int)true); |
| 162 | |
| 163 | announce_test_end(test_ffa_version); |
Max Shvetsov | 57c6ddb | 2020-07-01 14:09:48 +0100 | [diff] [blame] | 164 | } |
| 165 | |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 166 | bool ffa_memory_retrieve_test(struct mailbox_buffers *mb, |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 167 | struct ffa_memory_region **retrieved, |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 168 | uint64_t handle, ffa_vm_id_t sender, |
| 169 | ffa_vm_id_t receiver, uint32_t mem_func) |
| 170 | { |
| 171 | smc_ret_values ret; |
| 172 | uint32_t fragment_size; |
| 173 | uint32_t total_size; |
| 174 | uint32_t descriptor_size; |
| 175 | |
| 176 | if (retrieved == NULL || mb == NULL) { |
| 177 | ERROR("Invalid parameters!\n"); |
| 178 | return false; |
| 179 | } |
| 180 | |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 181 | /* |
| 182 | * TODO: Revise shareability attribute in function call |
| 183 | * below. |
| 184 | * https://lists.trustedfirmware.org/pipermail/hafnium/2020-June/000023.html |
| 185 | */ |
| 186 | descriptor_size = ffa_memory_retrieve_request_init( |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 187 | mb->send, handle, sender, receiver, 0, 0, |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 188 | FFA_DATA_ACCESS_RW, |
| 189 | FFA_INSTRUCTION_ACCESS_NX, |
| 190 | FFA_MEMORY_NORMAL_MEM, |
| 191 | FFA_MEMORY_CACHE_WRITE_BACK, |
| 192 | FFA_MEMORY_OUTER_SHAREABLE); |
| 193 | |
| 194 | ret = ffa_mem_retrieve_req(descriptor_size, descriptor_size); |
| 195 | |
| 196 | if (ret.ret0 != FFA_MEM_RETRIEVE_RESP) { |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 197 | ERROR("Couldn't retrieve the memory page. Error: %lx\n", |
| 198 | ret.ret2); |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Following total_size and fragment_size are useful to keep track |
| 204 | * of the state of transaction. When the sum of all fragment_size of all |
| 205 | * fragments is equal to total_size, the memory transaction has been |
| 206 | * completed. |
| 207 | * This is a simple test with only one segment. As such, upon |
| 208 | * successful ffa_mem_retrieve_req, total_size must be equal to |
| 209 | * fragment_size. |
| 210 | */ |
| 211 | total_size = ret.ret1; |
| 212 | fragment_size = ret.ret2; |
| 213 | |
| 214 | if (total_size != fragment_size) { |
| 215 | ERROR("Only expect one memory segment to be sent!\n"); |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | if (fragment_size > PAGE_SIZE) { |
| 220 | ERROR("Fragment should be smaller than RX buffer!\n"); |
| 221 | return false; |
| 222 | } |
| 223 | |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 224 | *retrieved = (struct ffa_memory_region *)mb->recv; |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 225 | |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 226 | if ((*retrieved)->receiver_count > MAX_MEM_SHARE_RECIPIENTS) { |
| 227 | VERBOSE("SPMC memory sharing operations support max of %u " |
| 228 | "receivers!\n", MAX_MEM_SHARE_RECIPIENTS); |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | |
Olivier Deprez | aed7f08 | 2020-11-04 15:11:59 +0100 | [diff] [blame] | 232 | VERBOSE("Memory Retrieved!\n"); |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool ffa_memory_relinquish_test(struct ffa_mem_relinquish *m, |
| 238 | uint64_t handle, |
| 239 | ffa_vm_id_t id) |
| 240 | { |
| 241 | ffa_mem_relinquish_init(m, handle, 0, id); |
| 242 | |
| 243 | if (ffa_mem_relinquish().ret0 != FFA_SUCCESS_SMC32) { |
| 244 | ERROR("%s failed to relinquish memory!\n", __func__); |
| 245 | return false; |
| 246 | } |
| 247 | |
Olivier Deprez | aed7f08 | 2020-11-04 15:11:59 +0100 | [diff] [blame] | 248 | VERBOSE("Memory Relinquished!\n"); |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | void ffa_memory_management_test(struct mailbox_buffers *mb, ffa_vm_id_t vm_id, |
| 253 | ffa_vm_id_t sender, uint32_t mem_func, |
| 254 | uint64_t handle) |
| 255 | { |
| 256 | const char *test_ffa = "Memory Management"; |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 257 | struct ffa_memory_region *m; |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 258 | struct ffa_composite_memory_region *composite; |
| 259 | int ret; |
| 260 | unsigned int mem_attrs; |
| 261 | uint32_t *ptr; |
| 262 | |
| 263 | announce_test_section_start(test_ffa); |
| 264 | |
| 265 | expect(ffa_memory_retrieve_test( |
| 266 | mb, &m, handle, sender, vm_id, mem_func), |
| 267 | true); |
| 268 | |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 269 | composite = ffa_memory_region_get_composite(m, 0); |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 270 | |
Olivier Deprez | aed7f08 | 2020-11-04 15:11:59 +0100 | [diff] [blame] | 271 | VERBOSE("Address: %p; page_count: %x %x\n", |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 272 | composite->constituents[0].address, |
| 273 | composite->constituents[0].page_count, PAGE_SIZE); |
| 274 | |
| 275 | /* This test is only concerned with RW permissions. */ |
| 276 | expect(ffa_get_data_access_attr( |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 277 | m->receivers[0].receiver_permissions.permissions), |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 278 | FFA_DATA_ACCESS_RW); |
| 279 | |
J-Alves | d8edeed | 2020-11-18 10:48:12 +0000 | [diff] [blame] | 280 | mem_attrs = MT_RW_DATA | MT_EXECUTE_NEVER; |
| 281 | |
| 282 | if (!IS_SP_ID(sender)) { |
| 283 | mem_attrs |= MT_NS; |
| 284 | } |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 285 | |
| 286 | ret = mmap_add_dynamic_region( |
| 287 | (uint64_t)composite->constituents[0].address, |
| 288 | (uint64_t)composite->constituents[0].address, |
| 289 | composite->constituents[0].page_count * PAGE_SIZE, |
| 290 | mem_attrs); |
| 291 | expect(ret, 0); |
| 292 | |
| 293 | VERBOSE("Memory has been mapped\n"); |
| 294 | |
| 295 | ptr = (uint32_t *) composite->constituents[0].address; |
| 296 | |
| 297 | /* Write mem_func to retrieved memory region for validation purposes. */ |
| 298 | VERBOSE("Writing: %x\n", mem_func); |
| 299 | for (unsigned int i = 0U; i < 5U; i++) |
| 300 | ptr[i] = mem_func; |
| 301 | |
| 302 | /* |
| 303 | * A FFA_MEM_DONATE changes the ownership of the page, as such no |
| 304 | * relinquish is needed. |
| 305 | */ |
| 306 | if (mem_func != FFA_MEM_DONATE_SMC32) { |
| 307 | ret = mmap_remove_dynamic_region( |
| 308 | (uint64_t)composite->constituents[0].address, |
| 309 | composite->constituents[0].page_count * PAGE_SIZE); |
| 310 | expect(ret, 0); |
| 311 | |
| 312 | expect(ffa_memory_relinquish_test( |
| 313 | (struct ffa_mem_relinquish *)mb->send, |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 314 | m->handle, vm_id), |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 315 | true); |
| 316 | } |
| 317 | |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 318 | expect(ffa_rx_release().ret0, FFA_SUCCESS_SMC32); |
| 319 | |
J-Alves | 63cdaa7 | 2020-10-08 17:22:45 +0100 | [diff] [blame] | 320 | announce_test_section_end(test_ffa); |
| 321 | } |
| 322 | |
Max Shvetsov | 57c6ddb | 2020-07-01 14:09:48 +0100 | [diff] [blame] | 323 | void ffa_tests(struct mailbox_buffers *mb) |
| 324 | { |
| 325 | const char *test_ffa = "FFA Interfaces"; |
| 326 | |
| 327 | announce_test_section_start(test_ffa); |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 328 | |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 329 | ffa_features_test(); |
Max Shvetsov | 57c6ddb | 2020-07-01 14:09:48 +0100 | [diff] [blame] | 330 | ffa_version_test(); |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 331 | ffa_partition_info_get_test(mb); |
Max Shvetsov | 40eb6a2 | 2020-06-08 11:15:30 +0100 | [diff] [blame] | 332 | |
J-Alves | 9f6f014 | 2020-06-17 15:37:59 +0100 | [diff] [blame] | 333 | announce_test_section_end(test_ffa); |
| 334 | } |