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 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdalign.h> |
| 18 | #include <stdint.h> |
| 19 | |
Andrew Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame] | 20 | #include "hf/arch/std.h" |
| 21 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 22 | #include "hf/memiter.h" |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 23 | |
| 24 | #include "vmapi/hf/call.h" |
| 25 | |
| 26 | #include "hftest.h" |
| 27 | |
| 28 | alignas(4096) uint8_t kstack[4096]; |
| 29 | |
| 30 | HFTEST_ENABLE(); |
| 31 | |
| 32 | extern struct hftest_test hftest_begin[]; |
| 33 | extern struct hftest_test hftest_end[]; |
| 34 | |
| 35 | static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE]; |
| 36 | static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE]; |
| 37 | |
| 38 | static hf_ipaddr_t send_addr = (hf_ipaddr_t)send; |
| 39 | static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv; |
| 40 | |
| 41 | static struct hftest_context global_context; |
| 42 | |
| 43 | struct hftest_context *hftest_get_context(void) |
| 44 | { |
| 45 | return &global_context; |
| 46 | } |
| 47 | |
| 48 | /** Find the service with the name passed in the arguments. */ |
| 49 | static hftest_test_fn find_service(struct memiter *args) |
| 50 | { |
| 51 | struct memiter service_name; |
| 52 | struct hftest_test *test; |
| 53 | |
| 54 | if (!memiter_parse_str(args, &service_name)) { |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | for (test = hftest_begin; test < hftest_end; ++test) { |
| 59 | if (test->kind == HFTEST_KIND_SERVICE && |
| 60 | memiter_iseq(&service_name, test->name)) { |
| 61 | return test->fn; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | static noreturn void abort(void) |
| 69 | { |
| 70 | HFTEST_LOG("Service contained failures."); |
| 71 | for (;;) { |
| 72 | /* |
| 73 | * Hang if the service aborts as a secondary can't power down |
| 74 | * the machine. |
| 75 | */ |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | noreturn void kmain(void) |
| 80 | { |
| 81 | struct memiter args; |
| 82 | hftest_test_fn service; |
| 83 | struct hf_mailbox_receive_return res; |
| 84 | struct hftest_context *ctx; |
| 85 | |
| 86 | /* Prepare the context. */ |
| 87 | |
| 88 | /* Set up the mailbox. */ |
| 89 | hf_vm_configure(send_addr, recv_addr); |
| 90 | |
| 91 | /* Receive the name of the service to run. */ |
| 92 | res = hf_mailbox_receive(true); |
| 93 | memiter_init(&args, recv, res.size); |
| 94 | service = find_service(&args); |
| 95 | hf_mailbox_clear(); |
| 96 | |
| 97 | /* Check the service was found. */ |
| 98 | if (service == NULL) { |
| 99 | HFTEST_LOG_FAILURE(); |
| 100 | HFTEST_LOG(HFTEST_LOG_INDENT |
| 101 | "Unable to find requested service"); |
| 102 | for (;;) { |
| 103 | /* Hang if the service was unknown. */ |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* Clean the context. */ |
| 108 | ctx = hftest_get_context(); |
| 109 | memset(ctx, 0, sizeof(*ctx)); |
| 110 | ctx->abort = abort; |
| 111 | ctx->send = send; |
| 112 | ctx->recv = recv; |
| 113 | |
| 114 | /* Pause so the next time cycles are given the service will be run. */ |
| 115 | hf_vcpu_yield(); |
| 116 | |
| 117 | /* Let the service run. */ |
| 118 | service(); |
| 119 | |
| 120 | /* Cleanly handle it if the service returns. */ |
| 121 | if (ctx->failures) { |
| 122 | abort(); |
| 123 | } |
| 124 | |
| 125 | for (;;) { |
| 126 | /* Hang if the service returns. */ |
| 127 | } |
| 128 | } |