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 | |
| 9 | #include <stdalign.h> |
| 10 | #include <stdint.h> |
| 11 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 12 | #include "hf/fdt_handler.h" |
| 13 | #include "hf/ffa.h" |
| 14 | #include "hf/memiter.h" |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 15 | #include "hf/mm.h" |
| 16 | #include "hf/std.h" |
| 17 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 18 | #include "vmapi/hf/call.h" |
| 19 | |
J-Alves | 35e6192 | 2021-05-06 10:01:05 +0100 | [diff] [blame] | 20 | #include "test/hftest.h" |
| 21 | #include "test/hftest_impl.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 | |
| 36 | noreturn void abort(void) |
| 37 | { |
| 38 | HFTEST_LOG("Service contained failures."); |
| 39 | /* Cause a fault, as a secondary/SP can't power down the machine. */ |
| 40 | *((volatile uint8_t *)1) = 1; |
| 41 | |
| 42 | /* This should never be reached, but to make the compiler happy... */ |
| 43 | for (;;) { |
| 44 | } |
| 45 | } |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 46 | |
| 47 | /** Find the service with the name passed in the arguments. */ |
| 48 | static hftest_test_fn find_service(struct memiter *args) |
| 49 | { |
| 50 | struct memiter service_name; |
| 51 | struct hftest_test *test; |
| 52 | |
| 53 | if (!memiter_parse_str(args, &service_name)) { |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | for (test = hftest_begin; test < hftest_end; ++test) { |
| 58 | if (test->kind == HFTEST_KIND_SERVICE && |
| 59 | memiter_iseq(&service_name, test->name)) { |
| 60 | return test->fn; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return NULL; |
| 65 | } |
| 66 | |
J-Alves | e235486 | 2022-12-08 17:35:51 +0000 | [diff] [blame] | 67 | void hftest_context_init(struct hftest_context *ctx, void *send, void *recv) |
| 68 | { |
| 69 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
| 70 | ctx->abort = abort; |
| 71 | ctx->send = send; |
| 72 | ctx->recv = recv; |
| 73 | } |
| 74 | |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 75 | noreturn void hftest_service_main(const void *fdt_ptr) |
| 76 | { |
| 77 | struct memiter args; |
| 78 | hftest_test_fn service; |
| 79 | struct hftest_context *ctx; |
| 80 | struct ffa_value ret; |
| 81 | struct fdt fdt; |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 82 | ffa_vm_id_t own_id = hf_vm_get_id(); |
| 83 | struct mailbox_buffers mb = set_up_mailbox(); |
| 84 | ffa_notifications_bitmap_t bitmap; |
| 85 | struct ffa_partition_msg *message = (struct ffa_partition_msg *)mb.recv; |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 86 | |
| 87 | /* Receive the name of the service to run. */ |
J-Alves | 8dacd98 | 2023-02-08 17:42:25 +0000 | [diff] [blame^] | 88 | ret = ffa_msg_wait(); |
| 89 | EXPECT_EQ(ret.func, FFA_RUN_32); |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Expect to wake up with indirect message related to the next service |
| 93 | * to be executed. |
| 94 | */ |
| 95 | ret = ffa_notification_get(own_id, 0, |
| 96 | FFA_NOTIFICATION_FLAG_BITMAP_SPM | |
| 97 | FFA_NOTIFICATION_FLAG_BITMAP_HYP); |
| 98 | ASSERT_EQ(ret.func, FFA_SUCCESS_32); |
| 99 | bitmap = ffa_notification_get_from_framework(ret); |
| 100 | ASSERT_TRUE(is_ffa_spm_buffer_full_notification(bitmap) || |
| 101 | is_ffa_hyp_buffer_full_notification(bitmap)); |
| 102 | ASSERT_EQ(own_id, ffa_rxtx_header_receiver(&message->header)); |
| 103 | memiter_init(&args, message->payload, message->header.size); |
| 104 | |
| 105 | /* Find service handler. */ |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 106 | service = find_service(&args); |
| 107 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
| 108 | |
| 109 | /* Check the service was found. */ |
| 110 | if (service == NULL) { |
| 111 | HFTEST_LOG_FAILURE(); |
| 112 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 113 | "Unable to find requested service"); |
| 114 | abort(); |
| 115 | } |
| 116 | |
| 117 | if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) { |
| 118 | HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT"); |
| 119 | abort(); |
| 120 | } |
| 121 | |
| 122 | /* Clean the context. */ |
| 123 | ctx = hftest_get_context(); |
J-Alves | e235486 | 2022-12-08 17:35:51 +0000 | [diff] [blame] | 124 | hftest_context_init(ctx, mb.send, mb.recv); |
J-Alves | 4dff3e9 | 2022-05-23 11:33:52 +0100 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * The memory size argument is to be used only by VMs. It is part of |
| 128 | * the dt provided by the Hypervisor. SPs expect to receive their |
| 129 | * FF-A manifest which doesn't have a memory size field. |
| 130 | */ |
| 131 | if (!IS_SP_ID(own_id) && |
| 132 | !fdt_get_memory_size(&fdt, &ctx->memory_size)) { |
Raghu Krishnamurthy | 50af660 | 2021-12-12 15:23:09 -0800 | [diff] [blame] | 133 | HFTEST_LOG_FAILURE(); |
| 134 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 135 | "No entry in the FDT on memory size details"); |
| 136 | abort(); |
| 137 | } |
| 138 | |
| 139 | /* Pause so the next time cycles are given the service will be run. */ |
| 140 | ffa_yield(); |
| 141 | |
| 142 | /* Let the service run. */ |
| 143 | service(); |
| 144 | |
| 145 | /* Cleanly handle it if the service returns. */ |
| 146 | if (ctx->failures) { |
| 147 | abort(); |
| 148 | } |
| 149 | |
| 150 | for (;;) { |
| 151 | /* Hang if the service returns. */ |
| 152 | } |
| 153 | } |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 154 | |
Kathleen Capella | cf31348 | 2023-02-02 17:29:44 -0500 | [diff] [blame] | 155 | ffa_vm_id_t hftest_get_dir_req_source_id(void) |
Madhukar Pappireddy | 29235ec | 2022-12-22 10:18:41 -0600 | [diff] [blame] | 156 | { |
| 157 | struct hftest_context *ctx = hftest_get_context(); |
| 158 | return ctx->dir_req_source_id; |
| 159 | } |
| 160 | |
| 161 | void hftest_set_dir_req_source_id(ffa_vm_id_t id) |
| 162 | { |
| 163 | struct hftest_context *ctx = hftest_get_context(); |
| 164 | ctx->dir_req_source_id = id; |
| 165 | } |