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