blob: 666480328ce7a255c2d6be9a7b81ef6447c5da81 [file] [log] [blame]
J-Alves35e61922021-05-06 10:01:05 +01001/*
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-Alves3c282a32023-02-28 18:27:31 +00009#include "hf/check.h"
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080010#include "hf/fdt_handler.h"
11#include "hf/ffa.h"
12#include "hf/memiter.h"
J-Alves35e61922021-05-06 10:01:05 +010013#include "hf/mm.h"
14#include "hf/std.h"
Karl Meakin133ae6e2024-04-10 12:05:36 +010015#include "hf/stdout.h"
J-Alves35e61922021-05-06 10:01:05 +010016
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080017#include "vmapi/hf/call.h"
18
J-Alves0c029c22023-06-21 10:32:57 +010019#include "../msr.h"
J-Alves35e61922021-05-06 10:01:05 +010020#include "test/hftest.h"
21#include "test/hftest_impl.h"
J-Alves0c029c22023-06-21 10:32:57 +010022#include "test/vmapi/arch/exception_handler.h"
J-Alves4dff3e92022-05-23 11:33:52 +010023#include "test/vmapi/ffa.h"
J-Alves35e61922021-05-06 10:01:05 +010024
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080025extern struct hftest_test hftest_begin[];
26extern struct hftest_test hftest_end[];
27
J-Alves35e61922021-05-06 10:01:05 +010028static struct hftest_context global_context;
29
J-Alves31e5c952024-07-12 09:45:05 +010030static alignas(PAGE_SIZE) uint8_t secondary_ec_stack[MAX_CPUS][PAGE_SIZE];
31
32uint8_t *hftest_get_secondary_ec_stack(size_t id)
33{
34 assert(id < MAX_CPUS);
35 return secondary_ec_stack[id];
36}
37
J-Alves35e61922021-05-06 10:01:05 +010038struct hftest_context *hftest_get_context(void)
39{
40 return &global_context;
41}
42
Kathleen Capellabed1dae2024-01-18 16:20:15 -050043static bool uint32list_has_next(const struct memiter *list)
44{
45 return memiter_size(list) > 0;
46}
47
48static void uint32list_get_next(struct memiter *list, uint32_t *out)
49{
50 uint64_t num;
51
52 CHECK(uint32list_has_next(list));
53 if (!fdt_parse_number(list, sizeof(uint32_t), &num)) {
54 return;
55 }
56
57 *out = (uint32_t)num;
58}
59
J-Alves35e61922021-05-06 10:01:05 +010060noreturn void abort(void)
61{
62 HFTEST_LOG("Service contained failures.");
63 /* Cause a fault, as a secondary/SP can't power down the machine. */
64 *((volatile uint8_t *)1) = 1;
65
66 /* This should never be reached, but to make the compiler happy... */
67 for (;;) {
68 }
69}
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080070
71/** Find the service with the name passed in the arguments. */
72static hftest_test_fn find_service(struct memiter *args)
73{
74 struct memiter service_name;
75 struct hftest_test *test;
76
77 if (!memiter_parse_str(args, &service_name)) {
78 return NULL;
79 }
80
81 for (test = hftest_begin; test < hftest_end; ++test) {
82 if (test->kind == HFTEST_KIND_SERVICE &&
83 memiter_iseq(&service_name, test->name)) {
84 return test->fn;
85 }
86 }
87
88 return NULL;
89}
90
J-Alvese2354862022-12-08 17:35:51 +000091void hftest_context_init(struct hftest_context *ctx, void *send, void *recv)
92{
93 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
94 ctx->abort = abort;
95 ctx->send = send;
96 ctx->recv = recv;
97}
98
J-Alves3c282a32023-02-28 18:27:31 +000099/*
100 * Parse the FF-A partition's manifest.
101 * This function assumes the 'fdt' field of the passed 'ctx' has been
102 * initialized.
103 * TODO: Parse other fields as needed.
104 */
Madhukar Pappireddyae519e12024-08-20 16:16:23 -0500105void hftest_parse_ffa_manifest(struct hftest_context *ctx, struct fdt *fdt)
J-Alves3c282a32023-02-28 18:27:31 +0000106{
107 struct fdt_node root;
108 struct fdt_node ffa_node;
109 struct string mem_region_node_name = STRING_INIT("memory-regions");
Madhukar Pappireddy89226712024-01-31 14:34:46 -0600110 struct string dev_region_node_name = STRING_INIT("device-regions");
Kathleen Capellabed1dae2024-01-18 16:20:15 -0500111 struct memiter uuid;
112 uint32_t uuid_word = 0;
113 uint16_t j = 0;
114 uint16_t i = 0;
J-Alves3c282a32023-02-28 18:27:31 +0000115 uint64_t number;
116
117 CHECK(ctx != NULL);
118 CHECK(fdt != NULL);
119
120 ASSERT_TRUE(fdt_find_node(fdt, "/", &root));
121 EXPECT_TRUE(fdt_is_compatible(&root, "arm,ffa-manifest-1.0"));
122 ASSERT_TRUE(fdt_read_number(&root, "load-address",
123 &ctx->partition_manifest.load_addr));
124 EXPECT_TRUE(fdt_read_number(&root, "ffa-version", &number));
Karl Meakin133ae6e2024-04-10 12:05:36 +0100125 ctx->partition_manifest.ffa_version = number;
J-Alves3c282a32023-02-28 18:27:31 +0000126
Madhukar Pappireddy538b6882024-08-20 16:50:52 -0500127 EXPECT_TRUE(fdt_read_number(&root, "execution-ctx-count", &number));
128 ctx->partition_manifest.execution_ctx_count = (uint16_t)number;
Kathleen Capellabed1dae2024-01-18 16:20:15 -0500129 EXPECT_TRUE(fdt_read_property(&root, "uuid", &uuid));
130
131 /* Parse UUIDs and populate uuid count.*/
132 while (uint32list_has_next(&uuid) && j < PARTITION_MAX_UUIDS) {
133 while (uint32list_has_next(&uuid) && i < 4) {
134 uint32list_get_next(&uuid, &uuid_word);
135 ctx->partition_manifest.uuids[j].uuid[i] = uuid_word;
136 i++;
137 }
138
139 EXPECT_FALSE(
140 ffa_uuid_is_null(&ctx->partition_manifest.uuids[j]));
141
142 dlog_verbose(" UUID %#x-%x-%x-%x\n",
143 ctx->partition_manifest.uuids[j].uuid[0],
144 ctx->partition_manifest.uuids[j].uuid[1],
145 ctx->partition_manifest.uuids[j].uuid[2],
146 ctx->partition_manifest.uuids[j].uuid[3]);
147 j++;
148 i = 0;
149 }
150
151 ctx->partition_manifest.uuid_count = j;
152
J-Alves3c282a32023-02-28 18:27:31 +0000153 ffa_node = root;
154
155 /* Look for the memory region node. */
156 if (fdt_find_child(&ffa_node, &mem_region_node_name) &&
157 fdt_first_child(&ffa_node)) {
158 uint32_t mem_count = 0;
159
160 do {
161 struct memory_region *cur_region =
162 &ctx->partition_manifest.mem_regions[mem_count];
163 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
164 &number));
165 cur_region->page_count = (uint32_t)number;
Karl Meakinc2360152023-06-01 14:17:32 +0100166
167 if (!fdt_read_number(&ffa_node, "base-address",
168 &cur_region->base_address)) {
169 EXPECT_TRUE(fdt_read_number(&ffa_node,
170 "relative-address",
171 &number));
172 cur_region->base_address =
173 ctx->partition_manifest.load_addr +
174 number;
175 }
176
J-Alves3c282a32023-02-28 18:27:31 +0000177 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
178 &number));
179 cur_region->attributes = (uint32_t)number;
180 mem_count++;
181 } while (fdt_next_sibling(&ffa_node));
182
183 assert(mem_count < PARTITION_MAX_MEMORY_REGIONS);
184
185 ctx->partition_manifest.mem_region_count = mem_count;
186 }
187
Madhukar Pappireddy89226712024-01-31 14:34:46 -0600188 ffa_node = root;
189
190 /* Look for the device region node. */
191 if (fdt_find_child(&ffa_node, &dev_region_node_name) &&
192 fdt_first_child(&ffa_node)) {
193 uint32_t dev_region_count = 0;
194
195 do {
196 struct device_region *cur_region =
197 &ctx->partition_manifest
198 .dev_regions[dev_region_count];
199 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
200 &number));
201 cur_region->page_count = (uint32_t)number;
202
203 if (!fdt_read_number(&ffa_node, "base-address",
204 &cur_region->base_address)) {
205 EXPECT_TRUE(fdt_read_number(&ffa_node,
206 "relative-address",
207 &number));
208 cur_region->base_address =
209 ctx->partition_manifest.load_addr +
210 number;
211 }
212
213 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
214 &number));
215 cur_region->attributes = (uint32_t)number;
216 dev_region_count++;
217 } while (fdt_next_sibling(&ffa_node));
218
219 assert(dev_region_count < PARTITION_MAX_DEVICE_REGIONS);
220
221 ctx->partition_manifest.dev_region_count = dev_region_count;
222 }
223
J-Alves3c282a32023-02-28 18:27:31 +0000224 ctx->is_ffa_manifest_parsed = true;
225}
226
Madhukar Pappireddy538b6882024-08-20 16:50:52 -0500227void hftest_service_set_up(struct hftest_context *ctx, struct fdt *fdt)
Daniel Boulby792b75c2023-08-09 16:53:53 +0100228{
229 struct fdt_node node;
230 struct hftest_test *hftest_info;
231
232 ASSERT_TRUE(fdt_find_node(fdt, "/", &node));
233
234 if (!fdt_find_child(&node, &(STRING_INIT("hftest-service-setup")))) {
235 return;
236 }
237
238 EXPECT_TRUE(fdt_is_compatible(&node, "arm,hftest"));
239
240 for (hftest_info = hftest_begin; hftest_info < hftest_end;
241 ++hftest_info) {
242 struct memiter data;
243 if (hftest_info->kind != HFTEST_KIND_SERVICE_SET_UP) {
244 continue;
245 }
246 if (fdt_read_property(&node, hftest_info->name, &data)) {
247 HFTEST_LOG("Running service_setup: %s\n",
248 hftest_info->name);
249 hftest_info->fn();
250 if (ctx->failures) {
251 HFTEST_LOG_FAILURE();
252 HFTEST_LOG(HFTEST_LOG_INDENT
253 "%s service_setup failed\n",
254 hftest_info->name);
255 abort();
256 }
257 } else {
258 HFTEST_LOG("Skipping service_setup: %s\n",
259 hftest_info->name);
260 }
261 }
262}
263
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800264noreturn void hftest_service_main(const void *fdt_ptr)
265{
J-Alves0c029c22023-06-21 10:32:57 +0100266 struct hftest_context *ctx;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800267 struct memiter args;
268 hftest_test_fn service;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800269 struct ffa_value ret;
270 struct fdt fdt;
J-Alves0c029c22023-06-21 10:32:57 +0100271 const ffa_id_t own_id = hf_vm_get_id();
J-Alves4dff3e92022-05-23 11:33:52 +0100272 ffa_notifications_bitmap_t bitmap;
J-Alves0c029c22023-06-21 10:32:57 +0100273 struct ffa_partition_msg *message;
274 uint32_t vcpu = get_current_vcpu_index();
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800275
Daniel Boulby792b75c2023-08-09 16:53:53 +0100276 ctx = hftest_get_context();
J-Alves0c029c22023-06-21 10:32:57 +0100277
278 /* If boot vcpu, set up mailbox and intialize context abort function. */
279 if (vcpu == 0) {
280 struct mailbox_buffers mb;
281 mb = set_up_mailbox();
282 hftest_context_init(ctx, mb.send, mb.recv);
283 }
Daniel Boulby792b75c2023-08-09 16:53:53 +0100284
285 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
286 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
287 abort();
288 }
289
290 /*
291 * The memory size argument is to be used only by VMs. It is part of
292 * the dt provided by the Hypervisor. SPs expect to receive their
293 * FF-A manifest which doesn't have a memory size field.
294 */
295 if (ffa_is_vm_id(own_id) &&
296 !fdt_get_memory_size(&fdt, &ctx->memory_size)) {
297 HFTEST_LOG_FAILURE();
298 HFTEST_LOG(HFTEST_LOG_INDENT
299 "No entry in the FDT on memory size details");
300 abort();
301 } else if (!ffa_is_vm_id(own_id)) {
302 /*
303 * It is secure partition. We are currently using the partition
304 * manifest for the SP.
305 */
306 hftest_parse_ffa_manifest(ctx, &fdt);
Karl Meakin133ae6e2024-04-10 12:05:36 +0100307 stdout_init(ctx->partition_manifest.ffa_version);
Daniel Boulby792b75c2023-08-09 16:53:53 +0100308
309 /* TODO: Determine memory size referring to the SP Pkg. */
310 ctx->memory_size = 1048576;
311 }
312
J-Alves1c56a252024-08-27 17:32:58 +0100313 /* If boot vcpu, it means it is running in RTM_INIT. */
314 if (vcpu == 0) {
315 run_service_set_up(ctx, &fdt);
316 }
Daniel Boulby792b75c2023-08-09 16:53:53 +0100317
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800318 /* Receive the name of the service to run. */
J-Alves8dacd982023-02-08 17:42:25 +0000319 ret = ffa_msg_wait();
320 EXPECT_EQ(ret.func, FFA_RUN_32);
J-Alves4dff3e92022-05-23 11:33:52 +0100321
J-Alves0c029c22023-06-21 10:32:57 +0100322 message = (struct ffa_partition_msg *)SERVICE_RECV_BUFFER();
323
J-Alves4dff3e92022-05-23 11:33:52 +0100324 /*
325 * Expect to wake up with indirect message related to the next service
326 * to be executed.
327 */
J-Alves0c029c22023-06-21 10:32:57 +0100328 ret = ffa_notification_get(own_id, vcpu,
J-Alves4dff3e92022-05-23 11:33:52 +0100329 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
330 FFA_NOTIFICATION_FLAG_BITMAP_HYP);
331 ASSERT_EQ(ret.func, FFA_SUCCESS_32);
332 bitmap = ffa_notification_get_from_framework(ret);
333 ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) ||
334 is_ffa_hyp_buffer_full_notification(bitmap));
335 ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header));
336 memiter_init(&args, message->payload, message->header.size);
337
338 /* Find service handler. */
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800339 service = find_service(&args);
340 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
341
342 /* Check the service was found. */
343 if (service == NULL) {
344 HFTEST_LOG_FAILURE();
345 HFTEST_LOG(HFTEST_LOG_INDENT
346 "Unable to find requested service");
347 abort();
348 }
349
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800350 /* Pause so the next time cycles are given the service will be run. */
351 ffa_yield();
352
353 /* Let the service run. */
354 service();
355
356 /* Cleanly handle it if the service returns. */
357 if (ctx->failures) {
358 abort();
359 }
360
361 for (;;) {
362 /* Hang if the service returns. */
363 }
364}
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600365
J-Alves19e20cf2023-08-02 12:48:55 +0100366ffa_id_t hftest_get_dir_req_source_id(void)
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600367{
368 struct hftest_context *ctx = hftest_get_context();
369 return ctx->dir_req_source_id;
370}
371
J-Alves19e20cf2023-08-02 12:48:55 +0100372void hftest_set_dir_req_source_id(ffa_id_t id)
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600373{
374 struct hftest_context *ctx = hftest_get_context();
375 ctx->dir_req_source_id = id;
376}
Madhukar Pappireddyae519e12024-08-20 16:16:23 -0500377
378void hftest_map_device_regions(struct hftest_context *ctx)
379{
380 struct device_region *dev_region;
381 uint32_t dev_region_count;
382
383 /*
384 * The running partition must have received and parsed its own
385 * partition manifest by now.
386 */
387 if (!ctx || !ctx->is_ffa_manifest_parsed) {
388 panic("Partition manifest not parsed.\n");
389 }
390
391 dev_region_count = ctx->partition_manifest.dev_region_count;
392
393 /* Map the MMIO address space of the devices. */
394 for (uint32_t i = 0; i < dev_region_count; i++) {
395 dev_region = &ctx->partition_manifest.dev_regions[i];
396
397 hftest_mm_identity_map(
398 // NOLINTNEXTLINE(performance-no-int-to-ptr)
399 (const void *)dev_region->base_address,
400 dev_region->page_count * PAGE_SIZE,
401 dev_region->attributes);
402 }
403}