Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | #include "hf/memiter.h" |
| 21 | #include "hf/spci.h" |
| 22 | #include "hf/std.h" |
| 23 | |
| 24 | #include "vmapi/hf/call.h" |
| 25 | #include "vmapi/hf/transport.h" |
| 26 | |
| 27 | #include "hftest.h" |
| 28 | |
| 29 | alignas(4096) uint8_t kstack[4096]; |
| 30 | |
| 31 | static alignas(HF_MAILBOX_SIZE) uint8_t send[HF_MAILBOX_SIZE]; |
| 32 | static alignas(HF_MAILBOX_SIZE) uint8_t recv[HF_MAILBOX_SIZE]; |
| 33 | |
| 34 | static hf_ipaddr_t send_addr = (hf_ipaddr_t)send; |
| 35 | static hf_ipaddr_t recv_addr = (hf_ipaddr_t)recv; |
| 36 | |
| 37 | static struct hftest_context global_context; |
| 38 | |
| 39 | struct hftest_context *hftest_get_context(void) |
| 40 | { |
| 41 | return &global_context; |
| 42 | } |
| 43 | |
| 44 | noreturn void abort(void) |
| 45 | { |
| 46 | HFTEST_LOG("Service contained failures."); |
| 47 | /* Cause a fault, as a secondary can't power down the machine. */ |
| 48 | *((volatile uint8_t *)1) = 1; |
| 49 | |
| 50 | /* This should never be reached, but to make the compiler happy... */ |
| 51 | for (;;) { |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void swap(uint64_t *a, uint64_t *b) |
| 56 | { |
| 57 | uint64_t t = *a; |
| 58 | *a = *b; |
| 59 | *b = t; |
| 60 | } |
| 61 | |
| 62 | noreturn void kmain(size_t memory_size) |
| 63 | { |
| 64 | struct hftest_context *ctx; |
| 65 | |
| 66 | /* Prepare the context. */ |
| 67 | |
| 68 | /* Set up the mailbox. */ |
| 69 | hf_vm_configure(send_addr, recv_addr); |
| 70 | |
| 71 | hf_mailbox_clear(); |
| 72 | |
| 73 | /* Clean the context. */ |
| 74 | ctx = hftest_get_context(); |
| 75 | memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); |
| 76 | ctx->abort = abort; |
| 77 | ctx->send = (struct spci_message *)send; |
| 78 | ctx->recv = (struct spci_message *)recv; |
| 79 | ctx->memory_size = memory_size; |
| 80 | |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 81 | for (;;) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 82 | struct spci_value ret; |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 83 | struct spci_message *send_buf = (struct spci_message *)send; |
| 84 | struct spci_message *recv_buf = (struct spci_message *)recv; |
| 85 | |
| 86 | /* Receive the packet. */ |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 87 | ret = spci_msg_wait(); |
Andrew Walbran | f1bd632 | 2019-10-03 16:45:11 +0100 | [diff] [blame] | 88 | EXPECT_EQ(ret.func, SPCI_MSG_SEND_32); |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 89 | EXPECT_LE(spci_msg_send_size(ret), SPCI_MSG_PAYLOAD_MAX); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 90 | |
| 91 | /* Echo the message back to the sender. */ |
| 92 | memcpy_s(send_buf->payload, SPCI_MSG_PAYLOAD_MAX, |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 93 | recv_buf->payload, spci_msg_send_size(ret)); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 94 | |
| 95 | /* Swap the socket's source and destination ports */ |
| 96 | struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)send_buf->payload; |
| 97 | swap(&(hdr->src_port), &(hdr->dst_port)); |
| 98 | |
| 99 | /* Swap the destination and source ids. */ |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 100 | spci_vm_id_t dst_id = spci_msg_send_sender(ret); |
| 101 | spci_vm_id_t src_id = spci_msg_send_receiver(ret); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 102 | |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 103 | spci_message_init(send_buf, spci_msg_send_size(ret), dst_id, |
| 104 | src_id); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 105 | |
| 106 | hf_mailbox_clear(); |
| 107 | EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS); |
| 108 | } |
| 109 | } |