blob: e90e94498314a79b4daad991d497096cfe346d3d [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
Kathleen Capellabed1dae2024-01-18 16:20:15 -0500127 EXPECT_TRUE(fdt_read_property(&root, "uuid", &uuid));
128
129 /* Parse UUIDs and populate uuid count.*/
130 while (uint32list_has_next(&uuid) && j < PARTITION_MAX_UUIDS) {
131 while (uint32list_has_next(&uuid) && i < 4) {
132 uint32list_get_next(&uuid, &uuid_word);
133 ctx->partition_manifest.uuids[j].uuid[i] = uuid_word;
134 i++;
135 }
136
137 EXPECT_FALSE(
138 ffa_uuid_is_null(&ctx->partition_manifest.uuids[j]));
139
140 dlog_verbose(" UUID %#x-%x-%x-%x\n",
141 ctx->partition_manifest.uuids[j].uuid[0],
142 ctx->partition_manifest.uuids[j].uuid[1],
143 ctx->partition_manifest.uuids[j].uuid[2],
144 ctx->partition_manifest.uuids[j].uuid[3]);
145 j++;
146 i = 0;
147 }
148
149 ctx->partition_manifest.uuid_count = j;
150
J-Alves3c282a32023-02-28 18:27:31 +0000151 ffa_node = root;
152
153 /* Look for the memory region node. */
154 if (fdt_find_child(&ffa_node, &mem_region_node_name) &&
155 fdt_first_child(&ffa_node)) {
156 uint32_t mem_count = 0;
157
158 do {
159 struct memory_region *cur_region =
160 &ctx->partition_manifest.mem_regions[mem_count];
161 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
162 &number));
163 cur_region->page_count = (uint32_t)number;
Karl Meakinc2360152023-06-01 14:17:32 +0100164
165 if (!fdt_read_number(&ffa_node, "base-address",
166 &cur_region->base_address)) {
167 EXPECT_TRUE(fdt_read_number(&ffa_node,
168 "relative-address",
169 &number));
170 cur_region->base_address =
171 ctx->partition_manifest.load_addr +
172 number;
173 }
174
J-Alves3c282a32023-02-28 18:27:31 +0000175 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
176 &number));
177 cur_region->attributes = (uint32_t)number;
178 mem_count++;
179 } while (fdt_next_sibling(&ffa_node));
180
181 assert(mem_count < PARTITION_MAX_MEMORY_REGIONS);
182
183 ctx->partition_manifest.mem_region_count = mem_count;
184 }
185
Madhukar Pappireddy89226712024-01-31 14:34:46 -0600186 ffa_node = root;
187
188 /* Look for the device region node. */
189 if (fdt_find_child(&ffa_node, &dev_region_node_name) &&
190 fdt_first_child(&ffa_node)) {
191 uint32_t dev_region_count = 0;
192
193 do {
194 struct device_region *cur_region =
195 &ctx->partition_manifest
196 .dev_regions[dev_region_count];
197 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
198 &number));
199 cur_region->page_count = (uint32_t)number;
200
201 if (!fdt_read_number(&ffa_node, "base-address",
202 &cur_region->base_address)) {
203 EXPECT_TRUE(fdt_read_number(&ffa_node,
204 "relative-address",
205 &number));
206 cur_region->base_address =
207 ctx->partition_manifest.load_addr +
208 number;
209 }
210
211 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
212 &number));
213 cur_region->attributes = (uint32_t)number;
214 dev_region_count++;
215 } while (fdt_next_sibling(&ffa_node));
216
217 assert(dev_region_count < PARTITION_MAX_DEVICE_REGIONS);
218
219 ctx->partition_manifest.dev_region_count = dev_region_count;
220 }
221
J-Alves3c282a32023-02-28 18:27:31 +0000222 ctx->is_ffa_manifest_parsed = true;
223}
224
Daniel Boulby792b75c2023-08-09 16:53:53 +0100225static void run_service_set_up(struct hftest_context *ctx, struct fdt *fdt)
226{
227 struct fdt_node node;
228 struct hftest_test *hftest_info;
229
230 ASSERT_TRUE(fdt_find_node(fdt, "/", &node));
231
232 if (!fdt_find_child(&node, &(STRING_INIT("hftest-service-setup")))) {
233 return;
234 }
235
236 EXPECT_TRUE(fdt_is_compatible(&node, "arm,hftest"));
237
238 for (hftest_info = hftest_begin; hftest_info < hftest_end;
239 ++hftest_info) {
240 struct memiter data;
241 if (hftest_info->kind != HFTEST_KIND_SERVICE_SET_UP) {
242 continue;
243 }
244 if (fdt_read_property(&node, hftest_info->name, &data)) {
245 HFTEST_LOG("Running service_setup: %s\n",
246 hftest_info->name);
247 hftest_info->fn();
248 if (ctx->failures) {
249 HFTEST_LOG_FAILURE();
250 HFTEST_LOG(HFTEST_LOG_INDENT
251 "%s service_setup failed\n",
252 hftest_info->name);
253 abort();
254 }
255 } else {
256 HFTEST_LOG("Skipping service_setup: %s\n",
257 hftest_info->name);
258 }
259 }
260}
261
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800262noreturn void hftest_service_main(const void *fdt_ptr)
263{
J-Alves0c029c22023-06-21 10:32:57 +0100264 struct hftest_context *ctx;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800265 struct memiter args;
266 hftest_test_fn service;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800267 struct ffa_value ret;
268 struct fdt fdt;
J-Alves0c029c22023-06-21 10:32:57 +0100269 const ffa_id_t own_id = hf_vm_get_id();
J-Alves4dff3e92022-05-23 11:33:52 +0100270 ffa_notifications_bitmap_t bitmap;
J-Alves0c029c22023-06-21 10:32:57 +0100271 struct ffa_partition_msg *message;
272 uint32_t vcpu = get_current_vcpu_index();
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800273
Daniel Boulby792b75c2023-08-09 16:53:53 +0100274 ctx = hftest_get_context();
J-Alves0c029c22023-06-21 10:32:57 +0100275
276 /* If boot vcpu, set up mailbox and intialize context abort function. */
277 if (vcpu == 0) {
278 struct mailbox_buffers mb;
279 mb = set_up_mailbox();
280 hftest_context_init(ctx, mb.send, mb.recv);
281 }
Daniel Boulby792b75c2023-08-09 16:53:53 +0100282
283 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
284 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
285 abort();
286 }
287
288 /*
289 * The memory size argument is to be used only by VMs. It is part of
290 * the dt provided by the Hypervisor. SPs expect to receive their
291 * FF-A manifest which doesn't have a memory size field.
292 */
293 if (ffa_is_vm_id(own_id) &&
294 !fdt_get_memory_size(&fdt, &ctx->memory_size)) {
295 HFTEST_LOG_FAILURE();
296 HFTEST_LOG(HFTEST_LOG_INDENT
297 "No entry in the FDT on memory size details");
298 abort();
299 } else if (!ffa_is_vm_id(own_id)) {
300 /*
301 * It is secure partition. We are currently using the partition
302 * manifest for the SP.
303 */
304 hftest_parse_ffa_manifest(ctx, &fdt);
Karl Meakin133ae6e2024-04-10 12:05:36 +0100305 stdout_init(ctx->partition_manifest.ffa_version);
Daniel Boulby792b75c2023-08-09 16:53:53 +0100306
307 /* TODO: Determine memory size referring to the SP Pkg. */
308 ctx->memory_size = 1048576;
309 }
310
J-Alves1c56a252024-08-27 17:32:58 +0100311 /* If boot vcpu, it means it is running in RTM_INIT. */
312 if (vcpu == 0) {
313 run_service_set_up(ctx, &fdt);
314 }
Daniel Boulby792b75c2023-08-09 16:53:53 +0100315
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800316 /* Receive the name of the service to run. */
J-Alves8dacd982023-02-08 17:42:25 +0000317 ret = ffa_msg_wait();
318 EXPECT_EQ(ret.func, FFA_RUN_32);
J-Alves4dff3e92022-05-23 11:33:52 +0100319
J-Alves0c029c22023-06-21 10:32:57 +0100320 message = (struct ffa_partition_msg *)SERVICE_RECV_BUFFER();
321
J-Alves4dff3e92022-05-23 11:33:52 +0100322 /*
323 * Expect to wake up with indirect message related to the next service
324 * to be executed.
325 */
J-Alves0c029c22023-06-21 10:32:57 +0100326 ret = ffa_notification_get(own_id, vcpu,
J-Alves4dff3e92022-05-23 11:33:52 +0100327 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
328 FFA_NOTIFICATION_FLAG_BITMAP_HYP);
329 ASSERT_EQ(ret.func, FFA_SUCCESS_32);
330 bitmap = ffa_notification_get_from_framework(ret);
331 ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) ||
332 is_ffa_hyp_buffer_full_notification(bitmap));
333 ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header));
334 memiter_init(&args, message->payload, message->header.size);
335
336 /* Find service handler. */
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800337 service = find_service(&args);
338 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
339
340 /* Check the service was found. */
341 if (service == NULL) {
342 HFTEST_LOG_FAILURE();
343 HFTEST_LOG(HFTEST_LOG_INDENT
344 "Unable to find requested service");
345 abort();
346 }
347
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800348 /* Pause so the next time cycles are given the service will be run. */
349 ffa_yield();
350
351 /* Let the service run. */
352 service();
353
354 /* Cleanly handle it if the service returns. */
355 if (ctx->failures) {
356 abort();
357 }
358
359 for (;;) {
360 /* Hang if the service returns. */
361 }
362}
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600363
J-Alves19e20cf2023-08-02 12:48:55 +0100364ffa_id_t hftest_get_dir_req_source_id(void)
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600365{
366 struct hftest_context *ctx = hftest_get_context();
367 return ctx->dir_req_source_id;
368}
369
J-Alves19e20cf2023-08-02 12:48:55 +0100370void hftest_set_dir_req_source_id(ffa_id_t id)
Madhukar Pappireddy29235ec2022-12-22 10:18:41 -0600371{
372 struct hftest_context *ctx = hftest_get_context();
373 ctx->dir_req_source_id = id;
374}
Madhukar Pappireddyae519e12024-08-20 16:16:23 -0500375
376void hftest_map_device_regions(struct hftest_context *ctx)
377{
378 struct device_region *dev_region;
379 uint32_t dev_region_count;
380
381 /*
382 * The running partition must have received and parsed its own
383 * partition manifest by now.
384 */
385 if (!ctx || !ctx->is_ffa_manifest_parsed) {
386 panic("Partition manifest not parsed.\n");
387 }
388
389 dev_region_count = ctx->partition_manifest.dev_region_count;
390
391 /* Map the MMIO address space of the devices. */
392 for (uint32_t i = 0; i < dev_region_count; i++) {
393 dev_region = &ctx->partition_manifest.dev_regions[i];
394
395 hftest_mm_identity_map(
396 // NOLINTNEXTLINE(performance-no-int-to-ptr)
397 (const void *)dev_region->base_address,
398 dev_region->page_count * PAGE_SIZE,
399 dev_region->attributes);
400 }
401}