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