blob: 048d2670de7f52f1ba9e7e3ac0e74d5c55a66421 [file] [log] [blame]
Andrew Scullf0551c82018-12-15 20:38:47 +00001/*
2 * Copyright 2018 Google LLC
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
20#include "hf/memiter.h"
21#include "hf/std.h"
22
23#include "vmapi/hf/call.h"
24
25#include "hftest.h"
26
27alignas(4096) uint8_t kstack[4096];
28
29HFTEST_ENABLE();
30
31extern struct hftest_test hftest_begin[];
32extern struct hftest_test hftest_end[];
33
34static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE];
35static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE];
36
37static hf_ipaddr_t send_addr = (hf_ipaddr_t)send;
38static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv;
39
40static struct hftest_context global_context;
41
42struct hftest_context *hftest_get_context(void)
43{
44 return &global_context;
45}
46
47/** Find the service with the name passed in the arguments. */
48static 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
67static noreturn void abort(void)
68{
69 HFTEST_LOG("Service contained failures.");
70 for (;;) {
71 /*
72 * Hang if the service aborts as a secondary can't power down
73 * the machine.
74 */
75 }
76}
77
78noreturn void kmain(void)
79{
80 struct memiter args;
81 hftest_test_fn service;
82 struct hf_mailbox_receive_return res;
83 struct hftest_context *ctx;
84
85 /* Prepare the context. */
86
87 /* Set up the mailbox. */
88 hf_vm_configure(send_addr, recv_addr);
89
90 /* Receive the name of the service to run. */
91 res = hf_mailbox_receive(true);
92 memiter_init(&args, recv, res.size);
93 service = find_service(&args);
94 hf_mailbox_clear();
95
96 /* Check the service was found. */
97 if (service == NULL) {
98 HFTEST_LOG_FAILURE();
99 HFTEST_LOG(HFTEST_LOG_INDENT
100 "Unable to find requested service");
101 for (;;) {
102 /* Hang if the service was unknown. */
103 }
104 }
105
106 /* Clean the context. */
107 ctx = hftest_get_context();
108 memset(ctx, 0, sizeof(*ctx));
109 ctx->abort = abort;
110 ctx->send = send;
111 ctx->recv = recv;
112
113 /* Pause so the next time cycles are given the service will be run. */
114 hf_vcpu_yield();
115
116 /* Let the service run. */
117 service();
118
119 /* Cleanly handle it if the service returns. */
120 if (ctx->failures) {
121 abort();
122 }
123
124 for (;;) {
125 /* Hang if the service returns. */
126 }
127}