blob: 127861701bfd73b3aa65ce3e7d6adb2b29aba8d2 [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
20#include "hf/memiter.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000021#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010022#include "hf/std.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000023
24#include "vmapi/hf/call.h"
25
26#include "hftest.h"
27
28alignas(4096) uint8_t kstack[4096];
29
30HFTEST_ENABLE();
31
32extern struct hftest_test hftest_begin[];
33extern struct hftest_test hftest_end[];
34
35static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE];
36static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE];
37
38static hf_ipaddr_t send_addr = (hf_ipaddr_t)send;
39static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv;
40
41static struct hftest_context global_context;
42
43struct hftest_context *hftest_get_context(void)
44{
45 return &global_context;
46}
47
48/** Find the service with the name passed in the arguments. */
49static 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
Andrew Sculla59f9bc2019-04-03 15:24:35 +010068noreturn void abort(void)
Andrew Scullf0551c82018-12-15 20:38:47 +000069{
70 HFTEST_LOG("Service contained failures.");
Andrew Walbran2432e672019-04-17 15:23:38 +010071 /* Cause a fault, as a secondary can't power down the machine. */
72 *((volatile uint8_t *)1) = 1;
73
74 /* This should never be reached, but to make the compiler happy... */
Andrew Scullf0551c82018-12-15 20:38:47 +000075 for (;;) {
Andrew Scullf0551c82018-12-15 20:38:47 +000076 }
77}
78
Andrew Walbran2b87c702019-04-16 18:16:05 +010079noreturn void kmain(size_t memory_size)
Andrew Scullf0551c82018-12-15 20:38:47 +000080{
81 struct memiter args;
82 hftest_test_fn service;
Andrew Scullf0551c82018-12-15 20:38:47 +000083 struct hftest_context *ctx;
84
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000085 struct spci_message *recv_msg = (struct spci_message *)recv;
86
Andrew Scullf0551c82018-12-15 20:38:47 +000087 /* Prepare the context. */
88
89 /* Set up the mailbox. */
90 hf_vm_configure(send_addr, recv_addr);
91
92 /* Receive the name of the service to run. */
Jose Marinho3e2442f2019-03-12 13:30:37 +000093 spci_msg_recv(SPCI_MSG_RECV_BLOCK);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000094 memiter_init(&args, recv_msg->payload, recv_msg->length);
Andrew Scullf0551c82018-12-15 20:38:47 +000095 service = find_service(&args);
96 hf_mailbox_clear();
97
98 /* Check the service was found. */
99 if (service == NULL) {
100 HFTEST_LOG_FAILURE();
101 HFTEST_LOG(HFTEST_LOG_INDENT
102 "Unable to find requested service");
103 for (;;) {
104 /* Hang if the service was unknown. */
105 }
106 }
107
108 /* Clean the context. */
109 ctx = hftest_get_context();
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100110 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
Andrew Scullf0551c82018-12-15 20:38:47 +0000111 ctx->abort = abort;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000112 ctx->send = (struct spci_message *)send;
113 ctx->recv = (struct spci_message *)recv;
Andrew Walbran2b87c702019-04-16 18:16:05 +0100114 ctx->memory_size = memory_size;
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}