blob: 0c34c2119d950ba2a5a9ae8b11b8c5d255be8e6d [file] [log] [blame]
Fuad Tabbabfa42bc2019-08-06 13:45:50 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
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 Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa.h"
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010021#include "hf/memiter.h"
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010022#include "hf/std.h"
23
24#include "vmapi/hf/call.h"
25#include "vmapi/hf/transport.h"
26
Andrew Walbran1e7c7742019-12-13 17:10:02 +000027#include "test/hftest.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010028#include "test/vmapi/ffa.h"
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010029
30alignas(4096) uint8_t kstack[4096];
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
45noreturn void abort(void)
46{
47 HFTEST_LOG("Service contained failures.");
48 /* Cause a fault, as a secondary can't power down the machine. */
49 *((volatile uint8_t *)1) = 1;
50
51 /* This should never be reached, but to make the compiler happy... */
52 for (;;) {
53 }
54}
55
56static void swap(uint64_t *a, uint64_t *b)
57{
58 uint64_t t = *a;
59 *a = *b;
60 *b = t;
61}
62
63noreturn void kmain(size_t memory_size)
64{
65 struct hftest_context *ctx;
66
67 /* Prepare the context. */
68
69 /* Set up the mailbox. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010070 ffa_rxtx_map(send_addr, recv_addr);
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010071
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010072 EXPECT_FFA_ERROR(ffa_rx_release(), FFA_DENIED);
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010073
74 /* Clean the context. */
75 ctx = hftest_get_context();
76 memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
77 ctx->abort = abort;
Andrew Walbran70bc8622019-10-07 14:15:58 +010078 ctx->send = send;
79 ctx->recv = recv;
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010080 ctx->memory_size = memory_size;
81
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010082 for (;;) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010083 struct ffa_value ret;
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010084
85 /* Receive the packet. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010086 ret = ffa_msg_wait();
87 EXPECT_EQ(ret.func, FFA_MSG_SEND_32);
88 EXPECT_LE(ffa_msg_send_size(ret), FFA_MSG_PAYLOAD_MAX);
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010089
90 /* Echo the message back to the sender. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010091 memcpy_s(send, FFA_MSG_PAYLOAD_MAX, recv,
92 ffa_msg_send_size(ret));
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010093
94 /* Swap the socket's source and destination ports */
Andrew Walbran70bc8622019-10-07 14:15:58 +010095 struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)send;
Fuad Tabbabfa42bc2019-08-06 13:45:50 +010096 swap(&(hdr->src_port), &(hdr->dst_port));
97
98 /* Swap the destination and source ids. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010099 ffa_vm_id_t dst_id = ffa_msg_send_sender(ret);
100 ffa_vm_id_t src_id = ffa_msg_send_receiver(ret);
Fuad Tabbabfa42bc2019-08-06 13:45:50 +0100101
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100102 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
103 EXPECT_EQ(
104 ffa_msg_send(src_id, dst_id, ffa_msg_send_size(ret), 0)
105 .func,
106 FFA_SUCCESS_32);
Fuad Tabbabfa42bc2019-08-06 13:45:50 +0100107 }
108}