blob: 743e087766508e5f7a7f065c255356cddf996e45 [file] [log] [blame]
Andrew Scullf0551c82018-12-15 20:38:47 +00001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scullf0551c82018-12-15 20:38:47 +00003 *
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 Walbran4a53ba62019-03-05 17:26:12 +000020#include "hf/arch/std.h"
21
Andrew Scullf0551c82018-12-15 20:38:47 +000022#include "hf/memiter.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000023#include "hf/spci.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000024
25#include "vmapi/hf/call.h"
26
27#include "hftest.h"
28
29alignas(4096) uint8_t kstack[4096];
30
31HFTEST_ENABLE();
32
33extern struct hftest_test hftest_begin[];
34extern struct hftest_test hftest_end[];
35
36static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE];
37static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE];
38
39static hf_ipaddr_t send_addr = (hf_ipaddr_t)send;
40static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv;
41
42static struct hftest_context global_context;
43
44struct hftest_context *hftest_get_context(void)
45{
46 return &global_context;
47}
48
49/** Find the service with the name passed in the arguments. */
50static hftest_test_fn find_service(struct memiter *args)
51{
52 struct memiter service_name;
53 struct hftest_test *test;
54
55 if (!memiter_parse_str(args, &service_name)) {
56 return NULL;
57 }
58
59 for (test = hftest_begin; test < hftest_end; ++test) {
60 if (test->kind == HFTEST_KIND_SERVICE &&
61 memiter_iseq(&service_name, test->name)) {
62 return test->fn;
63 }
64 }
65
66 return NULL;
67}
68
Andrew Sculla59f9bc2019-04-03 15:24:35 +010069noreturn void abort(void)
Andrew Scullf0551c82018-12-15 20:38:47 +000070{
71 HFTEST_LOG("Service contained failures.");
72 for (;;) {
73 /*
74 * Hang if the service aborts as a secondary can't power down
75 * the machine.
76 */
77 }
78}
79
80noreturn void kmain(void)
81{
82 struct memiter args;
83 hftest_test_fn service;
Andrew Scullf0551c82018-12-15 20:38:47 +000084 struct hftest_context *ctx;
85
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000086 struct spci_message *recv_msg = (struct spci_message *)recv;
87
Andrew Scullf0551c82018-12-15 20:38:47 +000088 /* Prepare the context. */
89
90 /* Set up the mailbox. */
91 hf_vm_configure(send_addr, recv_addr);
92
93 /* Receive the name of the service to run. */
Jose Marinho3e2442f2019-03-12 13:30:37 +000094 spci_msg_recv(SPCI_MSG_RECV_BLOCK);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000095 memiter_init(&args, recv_msg->payload, recv_msg->length);
Andrew Scullf0551c82018-12-15 20:38:47 +000096 service = find_service(&args);
97 hf_mailbox_clear();
98
99 /* Check the service was found. */
100 if (service == NULL) {
101 HFTEST_LOG_FAILURE();
102 HFTEST_LOG(HFTEST_LOG_INDENT
103 "Unable to find requested service");
104 for (;;) {
105 /* Hang if the service was unknown. */
106 }
107 }
108
109 /* Clean the context. */
110 ctx = hftest_get_context();
111 memset(ctx, 0, sizeof(*ctx));
112 ctx->abort = abort;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000113 ctx->send = (struct spci_message *)send;
114 ctx->recv = (struct spci_message *)recv;
Andrew Scullf0551c82018-12-15 20:38:47 +0000115
116 /* Pause so the next time cycles are given the service will be run. */
117 hf_vcpu_yield();
118
119 /* Let the service run. */
120 service();
121
122 /* Cleanly handle it if the service returns. */
123 if (ctx->failures) {
124 abort();
125 }
126
127 for (;;) {
128 /* Hang if the service returns. */
129 }
130}