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 | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 18 | #include "test/hftest.h" |
| 19 | #include "test/hftest_impl.h" |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 20 | #include "test/vmapi/ffa.h" |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 21 | |
| 22 | HFTEST_ENABLE(); |
| 23 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 24 | extern struct hftest_test hftest_begin[]; |
| 25 | extern struct hftest_test hftest_end[]; |
| 26 | |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 27 | static struct hftest_context global_context; |
| 28 | |
| 29 | struct hftest_context *hftest_get_context(void) |
| 30 | { |
| 31 | return &global_context; |
| 32 | } |
| 33 | |
| 34 | noreturn void abort(void) |
| 35 | { |
| 36 | HFTEST_LOG("Service contained failures."); |
| 37 | /* Cause a fault, as a secondary/SP can't power down the machine. */ |
| 38 | *((volatile uint8_t *)1) = 1; |
| 39 | |
| 40 | /* This should never be reached, but to make the compiler happy... */ |
| 41 | for (;;) { |
| 42 | } |
| 43 | } |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 44 | |
| 45 | /** Find the service with the name passed in the arguments. */ |
| 46 | static hftest_test_fn find_service(struct memiter *args) |
| 47 | { |
| 48 | struct memiter service_name; |
| 49 | struct hftest_test *test; |
| 50 | |
| 51 | if (!memiter_parse_str(args, &service_name)) { |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | for (test = hftest_begin; test < hftest_end; ++test) { |
| 56 | if (test->kind == HFTEST_KIND_SERVICE && |
| 57 | memiter_iseq(&service_name, test->name)) { |
| 58 | return test->fn; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | |
J-Alves | e235486 | 2022-12-08 17:35:51 +0000 | [diff] [blame] | 65 | void hftest_context_init(struct hftest_context *ctx, void *send, void *recv) |
| 66 | { |
| 67 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
| 68 | ctx->abort = abort; |
| 69 | ctx->send = send; |
| 70 | ctx->recv = recv; |
| 71 | } |
| 72 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 73 | /* |
| 74 | * Parse the FF-A partition's manifest. |
| 75 | * This function assumes the 'fdt' field of the passed 'ctx' has been |
| 76 | * initialized. |
| 77 | * TODO: Parse other fields as needed. |
| 78 | */ |
| 79 | static void hftest_parse_ffa_manifest(struct hftest_context *ctx, |
| 80 | struct fdt *fdt) |
| 81 | { |
| 82 | struct fdt_node root; |
| 83 | struct fdt_node ffa_node; |
| 84 | struct string mem_region_node_name = STRING_INIT("memory-regions"); |
| 85 | uint64_t number; |
| 86 | |
| 87 | CHECK(ctx != NULL); |
| 88 | CHECK(fdt != NULL); |
| 89 | |
| 90 | ASSERT_TRUE(fdt_find_node(fdt, "/", &root)); |
| 91 | EXPECT_TRUE(fdt_is_compatible(&root, "arm,ffa-manifest-1.0")); |
| 92 | ASSERT_TRUE(fdt_read_number(&root, "load-address", |
| 93 | &ctx->partition_manifest.load_addr)); |
| 94 | EXPECT_TRUE(fdt_read_number(&root, "ffa-version", &number)); |
| 95 | |
| 96 | ffa_node = root; |
| 97 | |
| 98 | /* Look for the memory region node. */ |
| 99 | if (fdt_find_child(&ffa_node, &mem_region_node_name) && |
| 100 | fdt_first_child(&ffa_node)) { |
| 101 | uint32_t mem_count = 0; |
| 102 | |
| 103 | do { |
| 104 | struct memory_region *cur_region = |
| 105 | &ctx->partition_manifest.mem_regions[mem_count]; |
| 106 | EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count", |
| 107 | &number)); |
| 108 | cur_region->page_count = (uint32_t)number; |
Karl Meakin | c236015 | 2023-06-01 14:17:32 +0100 | [diff] [blame] | 109 | |
| 110 | if (!fdt_read_number(&ffa_node, "base-address", |
| 111 | &cur_region->base_address)) { |
| 112 | EXPECT_TRUE(fdt_read_number(&ffa_node, |
| 113 | "relative-address", |
| 114 | &number)); |
| 115 | cur_region->base_address = |
| 116 | ctx->partition_manifest.load_addr + |
| 117 | number; |
| 118 | } |
| 119 | |
J-Alves | 3c282a3 | 2023-02-28 18:27:31 +0000 | [diff] [blame] | 120 | EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes", |
| 121 | &number)); |
| 122 | cur_region->attributes = (uint32_t)number; |
| 123 | mem_count++; |
| 124 | } while (fdt_next_sibling(&ffa_node)); |
| 125 | |
| 126 | assert(mem_count < PARTITION_MAX_MEMORY_REGIONS); |
| 127 | |
| 128 | ctx->partition_manifest.mem_region_count = mem_count; |
| 129 | } |
| 130 | |
| 131 | ctx->is_ffa_manifest_parsed = true; |
| 132 | } |
| 133 | |
Daniel Boulby | 792b75c | 2023-08-09 16:53:53 +0100 | [diff] [blame^] | 134 | static void run_service_set_up(struct hftest_context *ctx, struct fdt *fdt) |
| 135 | { |
| 136 | struct fdt_node node; |
| 137 | struct hftest_test *hftest_info; |
| 138 | |
| 139 | ASSERT_TRUE(fdt_find_node(fdt, "/", &node)); |
| 140 | |
| 141 | if (!fdt_find_child(&node, &(STRING_INIT("hftest-service-setup")))) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | EXPECT_TRUE(fdt_is_compatible(&node, "arm,hftest")); |
| 146 | |
| 147 | for (hftest_info = hftest_begin; hftest_info < hftest_end; |
| 148 | ++hftest_info) { |
| 149 | struct memiter data; |
| 150 | if (hftest_info->kind != HFTEST_KIND_SERVICE_SET_UP) { |
| 151 | continue; |
| 152 | } |
| 153 | if (fdt_read_property(&node, hftest_info->name, &data)) { |
| 154 | HFTEST_LOG("Running service_setup: %s\n", |
| 155 | hftest_info->name); |
| 156 | hftest_info->fn(); |
| 157 | if (ctx->failures) { |
| 158 | HFTEST_LOG_FAILURE(); |
| 159 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 160 | "%s service_setup failed\n", |
| 161 | hftest_info->name); |
| 162 | abort(); |
| 163 | } |
| 164 | } else { |
| 165 | HFTEST_LOG("Skipping service_setup: %s\n", |
| 166 | hftest_info->name); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 171 | noreturn void hftest_service_main(const void *fdt_ptr) |
| 172 | { |
| 173 | struct memiter args; |
| 174 | hftest_test_fn service; |
| 175 | struct hftest_context *ctx; |
| 176 | struct ffa_value ret; |
| 177 | struct fdt fdt; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 178 | ffa_id_t own_id = hf_vm_get_id(); |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 179 | struct mailbox_buffers mb = set_up_mailbox(); |
| 180 | ffa_notifications_bitmap_t bitmap; |
| 181 | struct ffa_partition_msg *message = (struct ffa_partition_msg *)mb.recv; |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 182 | |
Daniel Boulby | 792b75c | 2023-08-09 16:53:53 +0100 | [diff] [blame^] | 183 | /* Clean the context. */ |
| 184 | ctx = hftest_get_context(); |
| 185 | hftest_context_init(ctx, mb.send, mb.recv); |
| 186 | |
| 187 | if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) { |
| 188 | HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT"); |
| 189 | abort(); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * The memory size argument is to be used only by VMs. It is part of |
| 194 | * the dt provided by the Hypervisor. SPs expect to receive their |
| 195 | * FF-A manifest which doesn't have a memory size field. |
| 196 | */ |
| 197 | if (ffa_is_vm_id(own_id) && |
| 198 | !fdt_get_memory_size(&fdt, &ctx->memory_size)) { |
| 199 | HFTEST_LOG_FAILURE(); |
| 200 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 201 | "No entry in the FDT on memory size details"); |
| 202 | abort(); |
| 203 | } else if (!ffa_is_vm_id(own_id)) { |
| 204 | /* |
| 205 | * It is secure partition. We are currently using the partition |
| 206 | * manifest for the SP. |
| 207 | */ |
| 208 | hftest_parse_ffa_manifest(ctx, &fdt); |
| 209 | |
| 210 | /* TODO: Determine memory size referring to the SP Pkg. */ |
| 211 | ctx->memory_size = 1048576; |
| 212 | } |
| 213 | |
| 214 | run_service_set_up(ctx, &fdt); |
| 215 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 216 | /* Receive the name of the service to run. */ |
J-Alves | 8dacd98 | 2023-02-08 17:42:25 +0000 | [diff] [blame] | 217 | ret = ffa_msg_wait(); |
| 218 | EXPECT_EQ(ret.func, FFA_RUN_32); |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Expect to wake up with indirect message related to the next service |
| 222 | * to be executed. |
| 223 | */ |
| 224 | ret = ffa_notification_get(own_id, 0, |
| 225 | FFA_NOTIFICATION_FLAG_BITMAP_SPM | |
| 226 | FFA_NOTIFICATION_FLAG_BITMAP_HYP); |
| 227 | ASSERT_EQ(ret.func, FFA_SUCCESS_32); |
| 228 | bitmap = ffa_notification_get_from_framework(ret); |
| 229 | ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) || |
| 230 | is_ffa_hyp_buffer_full_notification(bitmap)); |
| 231 | ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header)); |
| 232 | memiter_init(&args, message->payload, message->header.size); |
| 233 | |
| 234 | /* Find service handler. */ |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 235 | service = find_service(&args); |
| 236 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
| 237 | |
| 238 | /* Check the service was found. */ |
| 239 | if (service == NULL) { |
| 240 | HFTEST_LOG_FAILURE(); |
| 241 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 242 | "Unable to find requested service"); |
| 243 | abort(); |
| 244 | } |
| 245 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 246 | /* Pause so the next time cycles are given the service will be run. */ |
| 247 | ffa_yield(); |
| 248 | |
| 249 | /* Let the service run. */ |
| 250 | service(); |
| 251 | |
| 252 | /* Cleanly handle it if the service returns. */ |
| 253 | if (ctx->failures) { |
| 254 | abort(); |
| 255 | } |
| 256 | |
| 257 | for (;;) { |
| 258 | /* Hang if the service returns. */ |
| 259 | } |
| 260 | } |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 261 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 262 | ffa_id_t hftest_get_dir_req_source_id(void) |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 263 | { |
| 264 | struct hftest_context *ctx = hftest_get_context(); |
| 265 | return ctx->dir_req_source_id; |
| 266 | } |
| 267 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 268 | void hftest_set_dir_req_source_id(ffa_id_t id) |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 269 | { |
| 270 | struct hftest_context *ctx = hftest_get_context(); |
| 271 | ctx->dir_req_source_id = id; |
| 272 | } |