blob: 1a6974be5282993f1bfcb55f7722b920033fa82f [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
Raghu Krishnamurthyfffe7612020-12-08 14:57:28 -080012#include "hf/arch/vm/interrupts.h"
13
Fuad Tabba50469e02020-06-30 15:14:28 +010014#include "hf/fdt_handler.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010015#include "hf/ffa.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000016#include "hf/memiter.h"
David Brazdil711fbe92019-08-06 13:39:58 +010017#include "hf/mm.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010018#include "hf/std.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000019
20#include "vmapi/hf/call.h"
21
Raghu Krishnamurthyfffe7612020-12-08 14:57:28 -080022#include "msr.h"
Andrew Walbran1e7c7742019-12-13 17:10:02 +000023#include "test/hftest.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000024
25alignas(4096) uint8_t kstack[4096];
26
27HFTEST_ENABLE();
28
29extern struct hftest_test hftest_begin[];
30extern struct hftest_test hftest_end[];
31
32static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE];
33static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE];
34
35static hf_ipaddr_t send_addr = (hf_ipaddr_t)send;
36static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv;
37
38static struct hftest_context global_context;
39
40struct hftest_context *hftest_get_context(void)
41{
42 return &global_context;
43}
44
45/** Find the service with the name passed in the arguments. */
46static hftest_test_fn find_service(struct memiter *args)
47{
48 struct memiter service_name;
49 struct hftest_test *test;
50
51 if (!memiter_parse_str(args, &service_name)) {
52 return NULL;
53 }
54
55 for (test = hftest_begin; test < hftest_end; ++test) {
56 if (test->kind == HFTEST_KIND_SERVICE &&
57 memiter_iseq(&service_name, test->name)) {
58 return test->fn;
59 }
60 }
61
62 return NULL;
63}
64
Andrew Sculla59f9bc2019-04-03 15:24:35 +010065noreturn void abort(void)
Andrew Scullf0551c82018-12-15 20:38:47 +000066{
67 HFTEST_LOG("Service contained failures.");
Andrew Walbran2432e672019-04-17 15:23:38 +010068 /* Cause a fault, as a secondary can't power down the machine. */
69 *((volatile uint8_t *)1) = 1;
70
71 /* This should never be reached, but to make the compiler happy... */
Andrew Scullf0551c82018-12-15 20:38:47 +000072 for (;;) {
Andrew Scullf0551c82018-12-15 20:38:47 +000073 }
74}
75
Fuad Tabba50469e02020-06-30 15:14:28 +010076noreturn void kmain(const void *fdt_ptr)
Andrew Scullf0551c82018-12-15 20:38:47 +000077{
78 struct memiter args;
79 hftest_test_fn service;
Andrew Scullf0551c82018-12-15 20:38:47 +000080 struct hftest_context *ctx;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010081 struct ffa_value ret;
Fuad Tabba50469e02020-06-30 15:14:28 +010082 struct fdt fdt;
Andrew Scullf0551c82018-12-15 20:38:47 +000083
David Brazdil711fbe92019-08-06 13:39:58 +010084 /*
85 * Initialize the stage-1 MMU and identity-map the entire address space.
86 */
87 if (!hftest_mm_init()) {
88 HFTEST_LOG_FAILURE();
89 HFTEST_LOG(HFTEST_LOG_INDENT "Memory initialization failed");
Fuad Tabba50469e02020-06-30 15:14:28 +010090 abort();
David Brazdil711fbe92019-08-06 13:39:58 +010091 }
92
Raghu Krishnamurthyfffe7612020-12-08 14:57:28 -080093 /* Setup basic exception handling. */
94 exception_setup(NULL, NULL);
95
Andrew Scullf0551c82018-12-15 20:38:47 +000096 /* Prepare the context. */
97
98 /* Set up the mailbox. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010099 ffa_rxtx_map(send_addr, recv_addr);
Andrew Scullf0551c82018-12-15 20:38:47 +0000100
101 /* Receive the name of the service to run. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100102 ret = ffa_msg_wait();
103 ASSERT_EQ(ret.func, FFA_MSG_SEND_32);
104 memiter_init(&args, recv, ffa_msg_send_size(ret));
Andrew Scullf0551c82018-12-15 20:38:47 +0000105 service = find_service(&args);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100106 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
Andrew Scullf0551c82018-12-15 20:38:47 +0000107
108 /* Check the service was found. */
109 if (service == NULL) {
110 HFTEST_LOG_FAILURE();
111 HFTEST_LOG(HFTEST_LOG_INDENT
112 "Unable to find requested service");
Fuad Tabba50469e02020-06-30 15:14:28 +0100113 abort();
114 }
115
116 if (!fdt_struct_from_ptr(fdt_ptr, &fdt)) {
117 HFTEST_LOG(HFTEST_LOG_INDENT "Unable to access the FDT");
118 abort();
Andrew Scullf0551c82018-12-15 20:38:47 +0000119 }
120
121 /* Clean the context. */
122 ctx = hftest_get_context();
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100123 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
Andrew Scullf0551c82018-12-15 20:38:47 +0000124 ctx->abort = abort;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100125 ctx->send = send;
126 ctx->recv = recv;
Fuad Tabba50469e02020-06-30 15:14:28 +0100127 if (!fdt_get_memory_size(&fdt, &ctx->memory_size)) {
128 HFTEST_LOG_FAILURE();
129 HFTEST_LOG(HFTEST_LOG_INDENT
130 "No entry in the FDT on memory size details");
131 abort();
132 }
Andrew Scullf0551c82018-12-15 20:38:47 +0000133
134 /* Pause so the next time cycles are given the service will be run. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100135 ffa_yield();
Andrew Scullf0551c82018-12-15 20:38:47 +0000136
137 /* Let the service run. */
138 service();
139
140 /* Cleanly handle it if the service returns. */
141 if (ctx->failures) {
142 abort();
143 }
144
145 for (;;) {
146 /* Hang if the service returns. */
147 }
148}