Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 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. |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdalign.h> |
| 10 | #include <stdint.h> |
| 11 | |
Raghu Krishnamurthy | fffe761 | 2020-12-08 14:57:28 -0800 | [diff] [blame] | 12 | #include "hf/arch/vm/interrupts.h" |
| 13 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 14 | #include "hf/fdt_handler.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 15 | #include "hf/ffa.h" |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 16 | #include "hf/memiter.h" |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 17 | #include "hf/mm.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 18 | #include "hf/std.h" |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 19 | |
| 20 | #include "vmapi/hf/call.h" |
| 21 | |
Andrew Walbran | 1e7c774 | 2019-12-13 17:10:02 +0000 | [diff] [blame] | 22 | #include "test/hftest.h" |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 23 | |
| 24 | alignas(4096) uint8_t kstack[4096]; |
| 25 | |
| 26 | HFTEST_ENABLE(); |
| 27 | |
| 28 | extern struct hftest_test hftest_begin[]; |
| 29 | extern struct hftest_test hftest_end[]; |
| 30 | |
| 31 | static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE]; |
| 32 | static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE]; |
| 33 | |
| 34 | static hf_ipaddr_t send_addr = (hf_ipaddr_t)send; |
| 35 | static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv; |
| 36 | |
| 37 | static struct hftest_context global_context; |
| 38 | |
| 39 | struct hftest_context *hftest_get_context(void) |
| 40 | { |
| 41 | return &global_context; |
| 42 | } |
| 43 | |
| 44 | /** Find the service with the name passed in the arguments. */ |
| 45 | static hftest_test_fn find_service(struct memiter *args) |
| 46 | { |
| 47 | struct memiter service_name; |
| 48 | struct hftest_test *test; |
| 49 | |
| 50 | if (!memiter_parse_str(args, &service_name)) { |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | for (test = hftest_begin; test < hftest_end; ++test) { |
| 55 | if (test->kind == HFTEST_KIND_SERVICE && |
| 56 | memiter_iseq(&service_name, test->name)) { |
| 57 | return test->fn; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Andrew Scull | a59f9bc | 2019-04-03 15:24:35 +0100 | [diff] [blame] | 64 | noreturn void abort(void) |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 65 | { |
| 66 | HFTEST_LOG("Service contained failures."); |
Andrew Walbran | 2432e67 | 2019-04-17 15:23:38 +0100 | [diff] [blame] | 67 | /* Cause a fault, as a secondary can't power down the machine. */ |
| 68 | *((volatile uint8_t *)1) = 1; |
| 69 | |
| 70 | /* This should never be reached, but to make the compiler happy... */ |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 71 | for (;;) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Raghu Krishnamurthy | 1c264f3 | 2021-02-27 20:37:13 -0800 | [diff] [blame^] | 75 | noreturn void hftest_service_main(const void *fdt_ptr) |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 76 | { |
| 77 | struct memiter args; |
| 78 | hftest_test_fn service; |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 79 | struct hftest_context *ctx; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 80 | struct ffa_value ret; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 81 | struct fdt fdt; |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 82 | |
| 83 | /* Prepare the context. */ |
| 84 | |
| 85 | /* Set up the mailbox. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 86 | ffa_rxtx_map(send_addr, recv_addr); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 87 | |
| 88 | /* Receive the name of the service to run. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 89 | ret = ffa_msg_wait(); |
| 90 | ASSERT_EQ(ret.func, FFA_MSG_SEND_32); |
| 91 | memiter_init(&args, recv, ffa_msg_send_size(ret)); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 92 | service = find_service(&args); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 93 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 94 | |
| 95 | /* Check the service was found. */ |
| 96 | if (service == NULL) { |
| 97 | HFTEST_LOG_FAILURE(); |
| 98 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 99 | "Unable to find requested service"); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 100 | abort(); |
| 101 | } |
| 102 | |
| 103 | if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) { |
| 104 | HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT"); |
| 105 | abort(); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Clean the context. */ |
| 109 | ctx = hftest_get_context(); |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 110 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 111 | ctx->abort = abort; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 112 | ctx->send = send; |
| 113 | ctx->recv = recv; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 114 | if (!fdt_get_memory_size(&fdt, &ctx->memory_size)) { |
| 115 | HFTEST_LOG_FAILURE(); |
| 116 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 117 | "No entry in the FDT on memory size details"); |
| 118 | abort(); |
| 119 | } |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 120 | |
| 121 | /* Pause so the next time cycles are given the service will be run. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 122 | ffa_yield(); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 123 | |
| 124 | /* Let the service run. */ |
| 125 | service(); |
| 126 | |
| 127 | /* Cleanly handle it if the service returns. */ |
| 128 | if (ctx->failures) { |
| 129 | abort(); |
| 130 | } |
| 131 | |
| 132 | for (;;) { |
| 133 | /* Hang if the service returns. */ |
| 134 | } |
| 135 | } |
Raghu Krishnamurthy | 1c264f3 | 2021-02-27 20:37:13 -0800 | [diff] [blame^] | 136 | |
| 137 | noreturn void kmain(const void *fdt_ptr) |
| 138 | { |
| 139 | /* |
| 140 | * Initialize the stage-1 MMU and identity-map the entire address space. |
| 141 | */ |
| 142 | if (!hftest_mm_init()) { |
| 143 | HFTEST_LOG_FAILURE(); |
| 144 | HFTEST_LOG(HFTEST_LOG_INDENT "Memory initialization failed"); |
| 145 | abort(); |
| 146 | } |
| 147 | |
| 148 | /* Setup basic exception handling. */ |
| 149 | exception_setup(NULL, NULL); |
| 150 | |
| 151 | hftest_service_main(fdt_ptr); |
| 152 | } |