blob: ed35e74fe57b73dd0422e0357a4efdd080d748bf [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"
David Brazdil711fbe92019-08-06 13:39:58 +010021#include "hf/mm.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000022#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010023#include "hf/std.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.");
Andrew Walbran2432e672019-04-17 15:23:38 +010072 /* Cause a fault, as a secondary can't power down the machine. */
73 *((volatile uint8_t *)1) = 1;
74
75 /* This should never be reached, but to make the compiler happy... */
Andrew Scullf0551c82018-12-15 20:38:47 +000076 for (;;) {
Andrew Scullf0551c82018-12-15 20:38:47 +000077 }
78}
79
Andrew Walbran2b87c702019-04-16 18:16:05 +010080noreturn void kmain(size_t memory_size)
Andrew Scullf0551c82018-12-15 20:38:47 +000081{
82 struct memiter args;
83 hftest_test_fn service;
Andrew Scullf0551c82018-12-15 20:38:47 +000084 struct hftest_context *ctx;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +010085 struct spci_value ret;
Andrew Scullf0551c82018-12-15 20:38:47 +000086
David Brazdil711fbe92019-08-06 13:39:58 +010087 /*
88 * Initialize the stage-1 MMU and identity-map the entire address space.
89 */
90 if (!hftest_mm_init()) {
91 HFTEST_LOG_FAILURE();
92 HFTEST_LOG(HFTEST_LOG_INDENT "Memory initialization failed");
93 for (;;) {
94 /* Hang if memory init failed. */
95 }
96 }
97
Andrew Scullf0551c82018-12-15 20:38:47 +000098 /* Prepare the context. */
99
100 /* Set up the mailbox. */
101 hf_vm_configure(send_addr, recv_addr);
102
103 /* Receive the name of the service to run. */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100104 ret = spci_msg_wait();
Andrew Walbran70bc8622019-10-07 14:15:58 +0100105 ASSERT_EQ(ret.func, SPCI_MSG_SEND_32);
106 memiter_init(&args, recv, spci_msg_send_size(ret));
Andrew Scullf0551c82018-12-15 20:38:47 +0000107 service = find_service(&args);
108 hf_mailbox_clear();
109
110 /* Check the service was found. */
111 if (service == NULL) {
112 HFTEST_LOG_FAILURE();
113 HFTEST_LOG(HFTEST_LOG_INDENT
114 "Unable to find requested service");
115 for (;;) {
116 /* Hang if the service was unknown. */
117 }
118 }
119
120 /* Clean the context. */
121 ctx = hftest_get_context();
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100122 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
Andrew Scullf0551c82018-12-15 20:38:47 +0000123 ctx->abort = abort;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100124 ctx->send = send;
125 ctx->recv = recv;
Andrew Walbran2b87c702019-04-16 18:16:05 +0100126 ctx->memory_size = memory_size;
Andrew Scullf0551c82018-12-15 20:38:47 +0000127
128 /* Pause so the next time cycles are given the service will be run. */
Jose Marinho135dff32019-02-28 10:25:57 +0000129 spci_yield();
Andrew Scullf0551c82018-12-15 20:38:47 +0000130
131 /* Let the service run. */
132 service();
133
134 /* Cleanly handle it if the service returns. */
135 if (ctx->failures) {
136 abort();
137 }
138
139 for (;;) {
140 /* Hang if the service returns. */
141 }
142}