blob: ca16206ca051fca233bc2fd8d61537bc378ba4df [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 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scullf0551c82018-12-15 20:38:47 +00007 */
8
9#include <stdalign.h>
10#include <stdint.h>
11
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010012#include "hf/ffa.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000013#include "hf/memiter.h"
David Brazdil711fbe92019-08-06 13:39:58 +010014#include "hf/mm.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010015#include "hf/std.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000016
17#include "vmapi/hf/call.h"
18
Andrew Walbran1e7c7742019-12-13 17:10:02 +000019#include "test/hftest.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000020
21alignas(4096) uint8_t kstack[4096];
22
23HFTEST_ENABLE();
24
25extern struct hftest_test hftest_begin[];
26extern struct hftest_test hftest_end[];
27
28static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE];
29static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE];
30
31static hf_ipaddr_t send_addr = (hf_ipaddr_t)send;
32static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv;
33
34static struct hftest_context global_context;
35
36struct hftest_context *hftest_get_context(void)
37{
38 return &global_context;
39}
40
41/** Find the service with the name passed in the arguments. */
42static hftest_test_fn find_service(struct memiter *args)
43{
44 struct memiter service_name;
45 struct hftest_test *test;
46
47 if (!memiter_parse_str(args, &service_name)) {
48 return NULL;
49 }
50
51 for (test = hftest_begin; test < hftest_end; ++test) {
52 if (test->kind == HFTEST_KIND_SERVICE &&
53 memiter_iseq(&service_name, test->name)) {
54 return test->fn;
55 }
56 }
57
58 return NULL;
59}
60
Andrew Sculla59f9bc2019-04-03 15:24:35 +010061noreturn void abort(void)
Andrew Scullf0551c82018-12-15 20:38:47 +000062{
63 HFTEST_LOG("Service contained failures.");
Andrew Walbran2432e672019-04-17 15:23:38 +010064 /* Cause a fault, as a secondary can't power down the machine. */
65 *((volatile uint8_t *)1) = 1;
66
67 /* This should never be reached, but to make the compiler happy... */
Andrew Scullf0551c82018-12-15 20:38:47 +000068 for (;;) {
Andrew Scullf0551c82018-12-15 20:38:47 +000069 }
70}
71
Andrew Walbran2b87c702019-04-16 18:16:05 +010072noreturn void kmain(size_t memory_size)
Andrew Scullf0551c82018-12-15 20:38:47 +000073{
74 struct memiter args;
75 hftest_test_fn service;
Andrew Scullf0551c82018-12-15 20:38:47 +000076 struct hftest_context *ctx;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010077 struct ffa_value ret;
Andrew Scullf0551c82018-12-15 20:38:47 +000078
David Brazdil711fbe92019-08-06 13:39:58 +010079 /*
80 * Initialize the stage-1 MMU and identity-map the entire address space.
81 */
82 if (!hftest_mm_init()) {
83 HFTEST_LOG_FAILURE();
84 HFTEST_LOG(HFTEST_LOG_INDENT "Memory initialization failed");
85 for (;;) {
86 /* Hang if memory init failed. */
87 }
88 }
89
Andrew Scullf0551c82018-12-15 20:38:47 +000090 /* Prepare the context. */
91
92 /* Set up the mailbox. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010093 ffa_rxtx_map(send_addr, recv_addr);
Andrew Scullf0551c82018-12-15 20:38:47 +000094
95 /* Receive the name of the service to run. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096 ret = ffa_msg_wait();
97 ASSERT_EQ(ret.func, FFA_MSG_SEND_32);
98 memiter_init(&args, recv, ffa_msg_send_size(ret));
Andrew Scullf0551c82018-12-15 20:38:47 +000099 service = find_service(&args);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100100 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
Andrew Scullf0551c82018-12-15 20:38:47 +0000101
102 /* Check the service was found. */
103 if (service == NULL) {
104 HFTEST_LOG_FAILURE();
105 HFTEST_LOG(HFTEST_LOG_INDENT
106 "Unable to find requested service");
107 for (;;) {
108 /* Hang if the service was unknown. */
109 }
110 }
111
112 /* Clean the context. */
113 ctx = hftest_get_context();
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100114 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
Andrew Scullf0551c82018-12-15 20:38:47 +0000115 ctx->abort = abort;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100116 ctx->send = send;
117 ctx->recv = recv;
Andrew Walbran2b87c702019-04-16 18:16:05 +0100118 ctx->memory_size = memory_size;
Andrew Scullf0551c82018-12-15 20:38:47 +0000119
120 /* Pause so the next time cycles are given the service will be run. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100121 ffa_yield();
Andrew Scullf0551c82018-12-15 20:38:47 +0000122
123 /* Let the service run. */
124 service();
125
126 /* Cleanly handle it if the service returns. */
127 if (ctx->failures) {
128 abort();
129 }
130
131 for (;;) {
132 /* Hang if the service returns. */
133 }
134}