J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Hafnium Authors. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
| 7 | */ |
| 8 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 9 | #include "hf/check.h" |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 10 | #include "hf/fdt_handler.h" |
| 11 | #include "hf/ffa.h" |
| 12 | #include "hf/memiter.h" |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 13 | #include "hf/mm.h" |
| 14 | #include "hf/std.h" |
| 15 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 16 | #include "vmapi/hf/call.h" |
| 17 | |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 18 | #include "../msr.h" |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 19 | #include "test/hftest.h" |
| 20 | #include "test/hftest_impl.h" |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 21 | #include "test/vmapi/arch/exception_handler.h" |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 22 | #include "test/vmapi/ffa.h" |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 23 | |
| 24 | HFTEST_ENABLE(); |
| 25 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 26 | extern struct hftest_test hftest_begin[]; |
| 27 | extern struct hftest_test hftest_end[]; |
| 28 | |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 29 | static struct hftest_context global_context; |
| 30 | |
| 31 | struct hftest_context *hftest_get_context(void) |
| 32 | { |
| 33 | return &global_context; |
| 34 | } |
| 35 | |
Kathleen Capella | bed1dae | 2024-01-18 16:20:15 -0500 | [diff] [blame] | 36 | static bool uint32list_has_next(const struct memiter *list) |
| 37 | { |
| 38 | return memiter_size(list) > 0; |
| 39 | } |
| 40 | |
| 41 | static void uint32list_get_next(struct memiter *list, uint32_t *out) |
| 42 | { |
| 43 | uint64_t num; |
| 44 | |
| 45 | CHECK(uint32list_has_next(list)); |
| 46 | if (!fdt_parse_number(list, sizeof(uint32_t), &num)) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | *out = (uint32_t)num; |
| 51 | } |
| 52 | |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 53 | noreturn void abort(void) |
| 54 | { |
| 55 | HFTEST_LOG("Service contained failures."); |
| 56 | /* Cause a fault, as a secondary/SP can't power down the machine. */ |
| 57 | *((volatile uint8_t *)1) = 1; |
| 58 | |
| 59 | /* This should never be reached, but to make the compiler happy... */ |
| 60 | for (;;) { |
| 61 | } |
| 62 | } |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 63 | |
| 64 | /** Find the service with the name passed in the arguments. */ |
| 65 | static hftest_test_fn find_service(struct memiter *args) |
| 66 | { |
| 67 | struct memiter service_name; |
| 68 | struct hftest_test *test; |
| 69 | |
| 70 | if (!memiter_parse_str(args, &service_name)) { |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | for (test = hftest_begin; test < hftest_end; ++test) { |
| 75 | if (test->kind == HFTEST_KIND_SERVICE && |
| 76 | memiter_iseq(&service_name, test->name)) { |
| 77 | return test->fn; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
J-Alves | e235486 | 2022-12-08 17:35:51 +0000 | [diff] [blame] | 84 | void hftest_context_init(struct hftest_context *ctx, void *send, void *recv) |
| 85 | { |
| 86 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
| 87 | ctx->abort = abort; |
| 88 | ctx->send = send; |
| 89 | ctx->recv = recv; |
| 90 | } |
| 91 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 92 | /* |
| 93 | * Parse the FF-A partition's manifest. |
| 94 | * This function assumes the 'fdt' field of the passed 'ctx' has been |
| 95 | * initialized. |
| 96 | * TODO: Parse other fields as needed. |
| 97 | */ |
| 98 | static void hftest_parse_ffa_manifest(struct hftest_context *ctx, |
| 99 | struct fdt *fdt) |
| 100 | { |
| 101 | struct fdt_node root; |
| 102 | struct fdt_node ffa_node; |
| 103 | struct string mem_region_node_name = STRING_INIT("memory-regions"); |
Madhukar Pappireddy | 8922671 | 2024-01-31 14:34:46 -0600 | [diff] [blame] | 104 | struct string dev_region_node_name = STRING_INIT("device-regions"); |
Kathleen Capella | bed1dae | 2024-01-18 16:20:15 -0500 | [diff] [blame] | 105 | struct memiter uuid; |
| 106 | uint32_t uuid_word = 0; |
| 107 | uint16_t j = 0; |
| 108 | uint16_t i = 0; |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 109 | uint64_t number; |
| 110 | |
| 111 | CHECK(ctx != NULL); |
| 112 | CHECK(fdt != NULL); |
| 113 | |
| 114 | ASSERT_TRUE(fdt_find_node(fdt, "/", &root)); |
| 115 | EXPECT_TRUE(fdt_is_compatible(&root, "arm,ffa-manifest-1.0")); |
| 116 | ASSERT_TRUE(fdt_read_number(&root, "load-address", |
| 117 | &ctx->partition_manifest.load_addr)); |
| 118 | EXPECT_TRUE(fdt_read_number(&root, "ffa-version", &number)); |
| 119 | |
Kathleen Capella | bed1dae | 2024-01-18 16:20:15 -0500 | [diff] [blame] | 120 | EXPECT_TRUE(fdt_read_property(&root, "uuid", &uuid)); |
| 121 | |
| 122 | /* Parse UUIDs and populate uuid count.*/ |
| 123 | while (uint32list_has_next(&uuid) && j < PARTITION_MAX_UUIDS) { |
| 124 | while (uint32list_has_next(&uuid) && i < 4) { |
| 125 | uint32list_get_next(&uuid, &uuid_word); |
| 126 | ctx->partition_manifest.uuids[j].uuid[i] = uuid_word; |
| 127 | i++; |
| 128 | } |
| 129 | |
| 130 | EXPECT_FALSE( |
| 131 | ffa_uuid_is_null(&ctx->partition_manifest.uuids[j])); |
| 132 | |
| 133 | dlog_verbose(" UUID %#x-%x-%x-%x\n", |
| 134 | ctx->partition_manifest.uuids[j].uuid[0], |
| 135 | ctx->partition_manifest.uuids[j].uuid[1], |
| 136 | ctx->partition_manifest.uuids[j].uuid[2], |
| 137 | ctx->partition_manifest.uuids[j].uuid[3]); |
| 138 | j++; |
| 139 | i = 0; |
| 140 | } |
| 141 | |
| 142 | ctx->partition_manifest.uuid_count = j; |
| 143 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 144 | ffa_node = root; |
| 145 | |
| 146 | /* Look for the memory region node. */ |
| 147 | if (fdt_find_child(&ffa_node, &mem_region_node_name) && |
| 148 | fdt_first_child(&ffa_node)) { |
| 149 | uint32_t mem_count = 0; |
| 150 | |
| 151 | do { |
| 152 | struct memory_region *cur_region = |
| 153 | &ctx->partition_manifest.mem_regions[mem_count]; |
| 154 | EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count", |
| 155 | &number)); |
| 156 | cur_region->page_count = (uint32_t)number; |
Karl Meakin | c236015 | 2023-06-01 14:17:32 +0100 | [diff] [blame] | 157 | |
| 158 | if (!fdt_read_number(&ffa_node, "base-address", |
| 159 | &cur_region->base_address)) { |
| 160 | EXPECT_TRUE(fdt_read_number(&ffa_node, |
| 161 | "relative-address", |
| 162 | &number)); |
| 163 | cur_region->base_address = |
| 164 | ctx->partition_manifest.load_addr + |
| 165 | number; |
| 166 | } |
| 167 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 168 | EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes", |
| 169 | &number)); |
| 170 | cur_region->attributes = (uint32_t)number; |
| 171 | mem_count++; |
| 172 | } while (fdt_next_sibling(&ffa_node)); |
| 173 | |
| 174 | assert(mem_count < PARTITION_MAX_MEMORY_REGIONS); |
| 175 | |
| 176 | ctx->partition_manifest.mem_region_count = mem_count; |
| 177 | } |
| 178 | |
Madhukar Pappireddy | 8922671 | 2024-01-31 14:34:46 -0600 | [diff] [blame] | 179 | ffa_node = root; |
| 180 | |
| 181 | /* Look for the device region node. */ |
| 182 | if (fdt_find_child(&ffa_node, &dev_region_node_name) && |
| 183 | fdt_first_child(&ffa_node)) { |
| 184 | uint32_t dev_region_count = 0; |
| 185 | |
| 186 | do { |
| 187 | struct device_region *cur_region = |
| 188 | &ctx->partition_manifest |
| 189 | .dev_regions[dev_region_count]; |
| 190 | EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count", |
| 191 | &number)); |
| 192 | cur_region->page_count = (uint32_t)number; |
| 193 | |
| 194 | if (!fdt_read_number(&ffa_node, "base-address", |
| 195 | &cur_region->base_address)) { |
| 196 | EXPECT_TRUE(fdt_read_number(&ffa_node, |
| 197 | "relative-address", |
| 198 | &number)); |
| 199 | cur_region->base_address = |
| 200 | ctx->partition_manifest.load_addr + |
| 201 | number; |
| 202 | } |
| 203 | |
| 204 | EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes", |
| 205 | &number)); |
| 206 | cur_region->attributes = (uint32_t)number; |
| 207 | dev_region_count++; |
| 208 | } while (fdt_next_sibling(&ffa_node)); |
| 209 | |
| 210 | assert(dev_region_count < PARTITION_MAX_DEVICE_REGIONS); |
| 211 | |
| 212 | ctx->partition_manifest.dev_region_count = dev_region_count; |
| 213 | } |
| 214 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 215 | ctx->is_ffa_manifest_parsed = true; |
| 216 | } |
| 217 | |
Daniel Boulby | 792b75c | 2023-08-09 16:53:53 +0100 | [diff] [blame] | 218 | static void run_service_set_up(struct hftest_context *ctx, struct fdt *fdt) |
| 219 | { |
| 220 | struct fdt_node node; |
| 221 | struct hftest_test *hftest_info; |
| 222 | |
| 223 | ASSERT_TRUE(fdt_find_node(fdt, "/", &node)); |
| 224 | |
| 225 | if (!fdt_find_child(&node, &(STRING_INIT("hftest-service-setup")))) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | EXPECT_TRUE(fdt_is_compatible(&node, "arm,hftest")); |
| 230 | |
| 231 | for (hftest_info = hftest_begin; hftest_info < hftest_end; |
| 232 | ++hftest_info) { |
| 233 | struct memiter data; |
| 234 | if (hftest_info->kind != HFTEST_KIND_SERVICE_SET_UP) { |
| 235 | continue; |
| 236 | } |
| 237 | if (fdt_read_property(&node, hftest_info->name, &data)) { |
| 238 | HFTEST_LOG("Running service_setup: %s\n", |
| 239 | hftest_info->name); |
| 240 | hftest_info->fn(); |
| 241 | if (ctx->failures) { |
| 242 | HFTEST_LOG_FAILURE(); |
| 243 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 244 | "%s service_setup failed\n", |
| 245 | hftest_info->name); |
| 246 | abort(); |
| 247 | } |
| 248 | } else { |
| 249 | HFTEST_LOG("Skipping service_setup: %s\n", |
| 250 | hftest_info->name); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 255 | noreturn void hftest_service_main(const void *fdt_ptr) |
| 256 | { |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 257 | struct hftest_context *ctx; |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 258 | struct memiter args; |
| 259 | hftest_test_fn service; |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 260 | struct ffa_value ret; |
| 261 | struct fdt fdt; |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 262 | const ffa_id_t own_id = hf_vm_get_id(); |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 263 | ffa_notifications_bitmap_t bitmap; |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 264 | struct ffa_partition_msg *message; |
| 265 | uint32_t vcpu = get_current_vcpu_index(); |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 266 | |
Daniel Boulby | 792b75c | 2023-08-09 16:53:53 +0100 | [diff] [blame] | 267 | ctx = hftest_get_context(); |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 268 | |
| 269 | /* If boot vcpu, set up mailbox and intialize context abort function. */ |
| 270 | if (vcpu == 0) { |
| 271 | struct mailbox_buffers mb; |
| 272 | mb = set_up_mailbox(); |
| 273 | hftest_context_init(ctx, mb.send, mb.recv); |
| 274 | } |
Daniel Boulby | 792b75c | 2023-08-09 16:53:53 +0100 | [diff] [blame] | 275 | |
| 276 | if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) { |
| 277 | HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT"); |
| 278 | abort(); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * The memory size argument is to be used only by VMs. It is part of |
| 283 | * the dt provided by the Hypervisor. SPs expect to receive their |
| 284 | * FF-A manifest which doesn't have a memory size field. |
| 285 | */ |
| 286 | if (ffa_is_vm_id(own_id) && |
| 287 | !fdt_get_memory_size(&fdt, &ctx->memory_size)) { |
| 288 | HFTEST_LOG_FAILURE(); |
| 289 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 290 | "No entry in the FDT on memory size details"); |
| 291 | abort(); |
| 292 | } else if (!ffa_is_vm_id(own_id)) { |
| 293 | /* |
| 294 | * It is secure partition. We are currently using the partition |
| 295 | * manifest for the SP. |
| 296 | */ |
| 297 | hftest_parse_ffa_manifest(ctx, &fdt); |
| 298 | |
| 299 | /* TODO: Determine memory size referring to the SP Pkg. */ |
| 300 | ctx->memory_size = 1048576; |
| 301 | } |
| 302 | |
| 303 | run_service_set_up(ctx, &fdt); |
| 304 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 305 | /* Receive the name of the service to run. */ |
J-Alves | 8dacd98 | 2023-02-08 17:42:25 +0000 | [diff] [blame] | 306 | ret = ffa_msg_wait(); |
| 307 | EXPECT_EQ(ret.func, FFA_RUN_32); |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 308 | |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 309 | message = (struct ffa_partition_msg *)SERVICE_RECV_BUFFER(); |
| 310 | |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 311 | /* |
| 312 | * Expect to wake up with indirect message related to the next service |
| 313 | * to be executed. |
| 314 | */ |
J-Alves | 0c029c2 | 2023-06-21 10:32:57 +0100 | [diff] [blame] | 315 | ret = ffa_notification_get(own_id, vcpu, |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 316 | FFA_NOTIFICATION_FLAG_BITMAP_SPM | |
| 317 | FFA_NOTIFICATION_FLAG_BITMAP_HYP); |
| 318 | ASSERT_EQ(ret.func, FFA_SUCCESS_32); |
| 319 | bitmap = ffa_notification_get_from_framework(ret); |
| 320 | ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) || |
| 321 | is_ffa_hyp_buffer_full_notification(bitmap)); |
| 322 | ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header)); |
| 323 | memiter_init(&args, message->payload, message->header.size); |
| 324 | |
| 325 | /* Find service handler. */ |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 326 | service = find_service(&args); |
| 327 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
| 328 | |
| 329 | /* Check the service was found. */ |
| 330 | if (service == NULL) { |
| 331 | HFTEST_LOG_FAILURE(); |
| 332 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 333 | "Unable to find requested service"); |
| 334 | abort(); |
| 335 | } |
| 336 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 337 | /* Pause so the next time cycles are given the service will be run. */ |
| 338 | ffa_yield(); |
| 339 | |
| 340 | /* Let the service run. */ |
| 341 | service(); |
| 342 | |
| 343 | /* Cleanly handle it if the service returns. */ |
| 344 | if (ctx->failures) { |
| 345 | abort(); |
| 346 | } |
| 347 | |
| 348 | for (;;) { |
| 349 | /* Hang if the service returns. */ |
| 350 | } |
| 351 | } |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 352 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 353 | ffa_id_t hftest_get_dir_req_source_id(void) |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 354 | { |
| 355 | struct hftest_context *ctx = hftest_get_context(); |
| 356 | return ctx->dir_req_source_id; |
| 357 | } |
| 358 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 359 | void hftest_set_dir_req_source_id(ffa_id_t id) |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 360 | { |
| 361 | struct hftest_context *ctx = hftest_get_context(); |
| 362 | ctx->dir_req_source_id = id; |
| 363 | } |