Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * 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. |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdalign.h> |
| 10 | #include <stdint.h> |
| 11 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 12 | #include "hf/ffa.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 13 | #include "hf/memiter.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 14 | #include "hf/std.h" |
| 15 | |
| 16 | #include "vmapi/hf/call.h" |
| 17 | #include "vmapi/hf/transport.h" |
| 18 | |
Andrew Walbran | 1e7c774 | 2019-12-13 17:10:02 +0000 | [diff] [blame] | 19 | #include "test/hftest.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 20 | #include "test/vmapi/ffa.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 21 | |
| 22 | alignas(4096) uint8_t kstack[4096]; |
| 23 | |
| 24 | static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE]; |
| 25 | static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE]; |
| 26 | |
| 27 | static hf_ipaddr_t send_addr = (hf_ipaddr_t)send; |
| 28 | static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv; |
| 29 | |
| 30 | static struct hftest_context global_context; |
| 31 | |
| 32 | struct hftest_context *hftest_get_context(void) |
| 33 | { |
| 34 | return &global_context; |
| 35 | } |
| 36 | |
| 37 | noreturn void abort(void) |
| 38 | { |
| 39 | HFTEST_LOG("Service contained failures."); |
| 40 | /* Cause a fault, as a secondary can't power down the machine. */ |
| 41 | *((volatile uint8_t *)1) = 1; |
| 42 | |
| 43 | /* This should never be reached, but to make the compiler happy... */ |
| 44 | for (;;) { |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static void swap(uint64_t *a, uint64_t *b) |
| 49 | { |
| 50 | uint64_t t = *a; |
| 51 | *a = *b; |
| 52 | *b = t; |
| 53 | } |
| 54 | |
| 55 | noreturn void kmain(size_t memory_size) |
| 56 | { |
| 57 | struct hftest_context *ctx; |
| 58 | |
| 59 | /* Prepare the context. */ |
| 60 | |
| 61 | /* Set up the mailbox. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 62 | ffa_rxtx_map(send_addr, recv_addr); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 63 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 64 | EXPECT_FFA_ERROR(ffa_rx_release(), FFA_DENIED); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 65 | |
| 66 | /* Clean the context. */ |
| 67 | ctx = hftest_get_context(); |
| 68 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
| 69 | ctx->abort = abort; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 70 | ctx->send = send; |
| 71 | ctx->recv = recv; |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 72 | ctx->memory_size = memory_size; |
| 73 | |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 74 | for (;;) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 75 | struct ffa_value ret; |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 76 | |
| 77 | /* Receive the packet. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 78 | ret = ffa_msg_wait(); |
| 79 | EXPECT_EQ(ret.func, FFA_MSG_SEND_32); |
| 80 | EXPECT_LE(ffa_msg_send_size(ret), FFA_MSG_PAYLOAD_MAX); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 81 | |
| 82 | /* Echo the message back to the sender. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 83 | memcpy_s(send, FFA_MSG_PAYLOAD_MAX, recv, |
| 84 | ffa_msg_send_size(ret)); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 85 | |
| 86 | /* Swap the socket's source and destination ports */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 87 | struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)send; |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 88 | swap(&(hdr->src_port), &(hdr->dst_port)); |
| 89 | |
| 90 | /* Swap the destination and source ids. */ |
J-Alves | d6f4e14 | 2021-03-05 13:33:59 +0000 | [diff] [blame] | 91 | ffa_vm_id_t dst_id = ffa_sender(ret); |
| 92 | ffa_vm_id_t src_id = ffa_receiver(ret); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 93 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 94 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
| 95 | EXPECT_EQ( |
| 96 | ffa_msg_send(src_id, dst_id, ffa_msg_send_size(ret), 0) |
| 97 | .func, |
| 98 | FFA_SUCCESS_32); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 99 | } |
| 100 | } |