blob: f0ab7016ba59b9909f5cc42a929294f55364a075 [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
25HFTEST_ENABLE();
26
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080027extern struct hftest_test hftest_begin[];
28extern struct hftest_test hftest_end[];
29
J-Alves35e61922021-05-06 10:01:05 +010030static struct hftest_context global_context;
31
J-Alves31e5c952024-07-12 09:45:05 +010032static alignas(PAGE_SIZE) uint8_t secondary_ec_stack[MAX_CPUS][PAGE_SIZE];
33
34uint8_t *hftest_get_secondary_ec_stack(size_t id)
35{
36 assert(id < MAX_CPUS);
37 return secondary_ec_stack[id];
38}
39
J-Alves35e61922021-05-06 10:01:05 +010040struct hftest_context *hftest_get_context(void)
41{
42 return &global_context;
43}
44
Kathleen Capellabed1dae2024-01-18 16:20:15 -050045static bool uint32list_has_next(const struct memiter *list)
46{
47 return memiter_size(list) > 0;
48}
49
50static void uint32list_get_next(struct memiter *list, uint32_t *out)
51{
52 uint64_t num;
53
54 CHECK(uint32list_has_next(list));
55 if (!fdt_parse_number(list, sizeof(uint32_t), &num)) {
56 return;
57 }
58
59 *out = (uint32_t)num;
60}
61
J-Alves35e61922021-05-06 10:01:05 +010062noreturn void abort(void)
63{
64 HFTEST_LOG("Service contained failures.");
65 /* Cause a fault, as a secondary/SP can't power down the machine. */
66 *((volatile uint8_t *)1) = 1;
67
68 /* This should never be reached, but to make the compiler happy... */
69 for (;;) {
70 }
71}
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -080072
73/** Find the service with the name passed in the arguments. */
74static hftest_test_fn find_service(struct memiter *args)
75{
76 struct memiter service_name;
77 struct hftest_test *test;
78
79 if (!memiter_parse_str(args, &service_name)) {
80 return NULL;
81 }
82
83 for (test = hftest_begin; test < hftest_end; ++test) {
84 if (test->kind == HFTEST_KIND_SERVICE &&
85 memiter_iseq(&service_name, test->name)) {
86 return test->fn;
87 }
88 }
89
90 return NULL;
91}
92
J-Alvese2354862022-12-08 17:35:51 +000093void hftest_context_init(struct hftest_context *ctx, void *send, void *recv)
94{
95 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
96 ctx->abort = abort;
97 ctx->send = send;
98 ctx->recv = recv;
99}
100
J-Alves3c282a32023-02-28 18:27:31 +0000101/*
102 * Parse the FF-A partition's manifest.
103 * This function assumes the 'fdt' field of the passed 'ctx' has been
104 * initialized.
105 * TODO: Parse other fields as needed.
106 */
107static void hftest_parse_ffa_manifest(struct hftest_context *ctx,
108 struct fdt *fdt)
109{
110 struct fdt_node root;
111 struct fdt_node ffa_node;
112 struct string mem_region_node_name = STRING_INIT("memory-regions");
Madhukar Pappireddy89226712024-01-31 14:34:46 -0600113 struct string dev_region_node_name = STRING_INIT("device-regions");
Kathleen Capellabed1dae2024-01-18 16:20:15 -0500114 struct memiter uuid;
115 uint32_t uuid_word = 0;
116 uint16_t j = 0;
117 uint16_t i = 0;
J-Alves3c282a32023-02-28 18:27:31 +0000118 uint64_t number;
119
120 CHECK(ctx != NULL);
121 CHECK(fdt != NULL);
122
123 ASSERT_TRUE(fdt_find_node(fdt, "/", &root));
124 EXPECT_TRUE(fdt_is_compatible(&root, "arm,ffa-manifest-1.0"));
125 ASSERT_TRUE(fdt_read_number(&root, "load-address",
126 &ctx->partition_manifest.load_addr));
127 EXPECT_TRUE(fdt_read_number(&root, "ffa-version", &number));
Karl Meakin133ae6e2024-04-10 12:05:36 +0100128 ctx->partition_manifest.ffa_version = number;
J-Alves3c282a32023-02-28 18:27:31 +0000129
Kathleen Capellabed1dae2024-01-18 16:20:15 -0500130 EXPECT_TRUE(fdt_read_property(&root, "uuid", &uuid));
131
132 /* Parse UUIDs and populate uuid count.*/
133 while (uint32list_has_next(&uuid) && j < PARTITION_MAX_UUIDS) {
134 while (uint32list_has_next(&uuid) && i < 4) {
135 uint32list_get_next(&uuid, &uuid_word);
136 ctx->partition_manifest.uuids[j].uuid[i] = uuid_word;
137 i++;
138 }
139
140 EXPECT_FALSE(
141 ffa_uuid_is_null(&ctx->partition_manifest.uuids[j]));
142
143 dlog_verbose(" UUID %#x-%x-%x-%x\n",
144 ctx->partition_manifest.uuids[j].uuid[0],
145 ctx->partition_manifest.uuids[j].uuid[1],
146 ctx->partition_manifest.uuids[j].uuid[2],
147 ctx->partition_manifest.uuids[j].uuid[3]);
148 j++;
149 i = 0;
150 }
151
152 ctx->partition_manifest.uuid_count = j;
153
J-Alves3c282a32023-02-28 18:27:31 +0000154 ffa_node = root;
155
156 /* Look for the memory region node. */
157 if (fdt_find_child(&ffa_node, &mem_region_node_name) &&
158 fdt_first_child(&ffa_node)) {
159 uint32_t mem_count = 0;
160
161 do {
162 struct memory_region *cur_region =
163 &ctx->partition_manifest.mem_regions[mem_count];
164 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
165 &number));
166 cur_region->page_count = (uint32_t)number;
Karl Meakinc2360152023-06-01 14:17:32 +0100167
168 if (!fdt_read_number(&ffa_node, "base-address",
169 &cur_region->base_address)) {
170 EXPECT_TRUE(fdt_read_number(&ffa_node,
171 "relative-address",
172 &number));
173 cur_region->base_address =
174 ctx->partition_manifest.load_addr +
175 number;
176 }
177
J-Alves3c282a32023-02-28 18:27:31 +0000178 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
179 &number));
180 cur_region->attributes = (uint32_t)number;
181 mem_count++;
182 } while (fdt_next_sibling(&ffa_node));
183
184 assert(mem_count < PARTITION_MAX_MEMORY_REGIONS);
185
186 ctx->partition_manifest.mem_region_count = mem_count;
187 }
188
Madhukar Pappireddy89226712024-01-31 14:34:46 -0600189 ffa_node = root;
190
191 /* Look for the device region node. */
192 if (fdt_find_child(&ffa_node, &dev_region_node_name) &&
193 fdt_first_child(&ffa_node)) {
194 uint32_t dev_region_count = 0;
195
196 do {
197 struct device_region *cur_region =
198 &ctx->partition_manifest
199 .dev_regions[dev_region_count];
200 EXPECT_TRUE(fdt_read_number(&ffa_node, "pages-count",
201 &number));
202 cur_region->page_count = (uint32_t)number;
203
204 if (!fdt_read_number(&ffa_node, "base-address",
205 &cur_region->base_address)) {
206 EXPECT_TRUE(fdt_read_number(&ffa_node,
207 "relative-address",
208 &number));
209 cur_region->base_address =
210 ctx->partition_manifest.load_addr +
211 number;
212 }
213
214 EXPECT_TRUE(fdt_read_number(&ffa_node, "attributes",
215 &number));
216 cur_region->attributes = (uint32_t)number;
217 dev_region_count++;
218 } while (fdt_next_sibling(&ffa_node));
219
220 assert(dev_region_count < PARTITION_MAX_DEVICE_REGIONS);
221
222 ctx->partition_manifest.dev_region_count = dev_region_count;
223 }
224
J-Alves3c282a32023-02-28 18:27:31 +0000225 ctx->is_ffa_manifest_parsed = true;
226}
227
Daniel Boulby792b75c2023-08-09 16:53:53 +0100228static void run_service_set_up(struct hftest_context *ctx, struct fdt *fdt)
229{
230 struct fdt_node node;
231 struct hftest_test *hftest_info;
232
233 ASSERT_TRUE(fdt_find_node(fdt, "/", &node));
234
235 if (!fdt_find_child(&node, &(STRING_INIT("hftest-service-setup")))) {
236 return;
237 }
238
239 EXPECT_TRUE(fdt_is_compatible(&node, "arm,hftest"));
240
241 for (hftest_info = hftest_begin; hftest_info < hftest_end;
242 ++hftest_info) {
243 struct memiter data;
244 if (hftest_info->kind != HFTEST_KIND_SERVICE_SET_UP) {
245 continue;
246 }
247 if (fdt_read_property(&node, hftest_info->name, &data)) {
248 HFTEST_LOG("Running service_setup: %s\n",
249 hftest_info->name);
250 hftest_info->fn();
251 if (ctx->failures) {
252 HFTEST_LOG_FAILURE();
253 HFTEST_LOG(HFTEST_LOG_INDENT
254 "%s service_setup failed\n",
255 hftest_info->name);
256 abort();
257 }
258 } else {
259 HFTEST_LOG("Skipping service_setup: %s\n",
260 hftest_info->name);
261 }
262 }
263}
264
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800265noreturn void hftest_service_main(const void *fdt_ptr)
266{
J-Alves0c029c22023-06-21 10:32:57 +0100267 struct hftest_context *ctx;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800268 struct memiter args;
269 hftest_test_fn service;
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800270 struct ffa_value ret;
271 struct fdt fdt;
J-Alves0c029c22023-06-21 10:32:57 +0100272 const ffa_id_t own_id = hf_vm_get_id();
J-Alves4dff3e92022-05-23 11:33:52 +0100273 ffa_notifications_bitmap_t bitmap;
J-Alves0c029c22023-06-21 10:32:57 +0100274 struct ffa_partition_msg *message;
275 uint32_t vcpu = get_current_vcpu_index();
Raghu Krishnamurthy50af6602021-12-12 15:23:09 -0800276
Daniel Boulby792b75c2023-08-09 16:53:53 +0100277 ctx = hftest_get_context();
J-Alves0c029c22023-06-21 10:32:57 +0100278
279 /* If boot vcpu, set up mailbox and intialize context abort function. */
280 if (vcpu == 0) {
281 struct mailbox_buffers mb;
282 mb = set_up_mailbox();
283 hftest_context_init(ctx, mb.send, mb.recv);
284 }
Daniel Boulby792b75c2023-08-09 16:53:53 +0100285
286 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
287 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
288 abort();
289 }
290
291 /*
292 * The memory size argument is to be used only by VMs. It is part of
293 * the dt provided by the Hypervisor. SPs expect to receive their
294 * FF-A manifest which doesn't have a memory size field.
295 */
296 if (ffa_is_vm_id(own_id) &&
297 !fdt_get_memory_size(&fdt, &ctx->memory_size)) {
298 HFTEST_LOG_FAILURE();
299 HFTEST_LOG(HFTEST_LOG_INDENT
300 "No entry in the FDT on memory size details");
301 abort();
302 } else if (!ffa_is_vm_id(own_id)) {
303 /*
304 * It is secure partition. We are currently using the partition
305 * manifest for the SP.
306 */
307 hftest_parse_ffa_manifest(ctx, &fdt);
Karl Meakin133ae6e2024-04-10 12:05:36 +0100308 stdout_init(ctx->partition_manifest.ffa_version);
Daniel Boulby792b75c2023-08-09 16:53:53 +0100309
310 /* TODO: Determine memory size referring to the SP Pkg. */
311 ctx->memory_size = 1048576;
312 }
313
314 run_service_set_up(ctx, &fdt);
315
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}