Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <stdalign.h> |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 3 | #include <stdint.h> |
| 4 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 5 | #include "hf/mm.h" |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 6 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 7 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 8 | #include "vmapi/hf/call.h" |
| 9 | |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 10 | #include "hftest.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 11 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 12 | static alignas(PAGE_SIZE) uint8_t send_page[PAGE_SIZE]; |
| 13 | static alignas(PAGE_SIZE) uint8_t recv_page[PAGE_SIZE]; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 14 | static_assert(sizeof(send_page) == PAGE_SIZE, "Send page is not a page."); |
| 15 | static_assert(sizeof(recv_page) == PAGE_SIZE, "Recv page is not a page."); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 16 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 17 | static hf_ipaddr_t send_page_addr = (hf_ipaddr_t)send_page; |
| 18 | static hf_ipaddr_t recv_page_addr = (hf_ipaddr_t)recv_page; |
| 19 | |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 20 | /* Keep macro alignment */ |
| 21 | /* clang-format off */ |
| 22 | |
| 23 | #define RELAY_A_VM_ID 1 |
| 24 | #define RELAY_B_VM_ID 2 |
| 25 | #define ECHO_VM_ID 3 |
| 26 | |
| 27 | /* clang-format on */ |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 28 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 29 | /** |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 30 | * Confirm there are 3 secondary VMs. |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 31 | */ |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 32 | TEST(hf_vm_get_count, three_secondary_vms) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 33 | { |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 34 | EXPECT_EQ(hf_vm_get_count(), 4); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 37 | /** |
| 38 | * Confirm there that secondary VM has 1 VCPU. |
| 39 | */ |
| 40 | TEST(hf_vcpu_get_count, secondary_has_one_vcpu) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 41 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 42 | EXPECT_EQ(hf_vcpu_get_count(1), 1); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 45 | /** |
| 46 | * Confirm it is an error to query how many VCPUs are assigned to a nonexistent |
| 47 | * secondary VM. |
| 48 | */ |
| 49 | TEST(hf_vcpu_get_count, large_invalid_vm_index) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 50 | { |
| 51 | EXPECT_EQ(hf_vcpu_get_count(0xffffffff), -1); |
| 52 | } |
| 53 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 54 | /** |
| 55 | * The configured send/receive addresses can't be unaligned. |
| 56 | */ |
| 57 | TEST(hf_vm_configure, fails_with_unaligned_pointer) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 58 | { |
| 59 | uint8_t maybe_aligned[2]; |
| 60 | hf_ipaddr_t unaligned_addr = (hf_ipaddr_t)&maybe_aligned[1]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 61 | hf_ipaddr_t aligned_addr = (hf_ipaddr_t)send_page; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 62 | |
| 63 | /* Check the the address is unaligned. */ |
| 64 | ASSERT_EQ(unaligned_addr & 1, 1); |
| 65 | |
| 66 | EXPECT_EQ(hf_vm_configure(aligned_addr, unaligned_addr), -1); |
| 67 | EXPECT_EQ(hf_vm_configure(unaligned_addr, aligned_addr), -1); |
| 68 | EXPECT_EQ(hf_vm_configure(unaligned_addr, unaligned_addr), -1); |
| 69 | } |
| 70 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 71 | /** |
| 72 | * The configured send/receive addresses can't be the same page. |
| 73 | */ |
| 74 | TEST(hf_vm_configure, fails_with_same_page) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 75 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 76 | EXPECT_EQ(hf_vm_configure(send_page_addr, send_page_addr), -1); |
| 77 | EXPECT_EQ(hf_vm_configure(recv_page_addr, recv_page_addr), -1); |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 80 | /** |
| 81 | * The configuration of the send/receive addresses can only happen once. |
| 82 | */ |
| 83 | TEST(hf_vm_configure, fails_if_already_succeeded) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 84 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 85 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
| 86 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), -1); |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 89 | /** |
| 90 | * The configuration of the send/receive address is successful with valid |
| 91 | * arguments. |
| 92 | */ |
| 93 | TEST(hf_vm_configure, succeeds) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 94 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 95 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 96 | } |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Send and receive the same message from the echo VM. |
| 100 | */ |
| 101 | TEST(mailbox, echo) |
| 102 | { |
| 103 | const char message[] = "Echo this back to me!"; |
| 104 | |
| 105 | /* Configure mailbox pages. */ |
| 106 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
| 107 | EXPECT_EQ(hf_vcpu_run(ECHO_VM_ID, 0), |
| 108 | HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAIT_FOR_INTERRUPT, 0, 0)); |
| 109 | |
| 110 | /* Set the message, echo it and check it didn't change. */ |
| 111 | memcpy(send_page, message, sizeof(message)); |
| 112 | EXPECT_EQ(hf_mailbox_send(ECHO_VM_ID, sizeof(message)), 0); |
| 113 | EXPECT_EQ( |
| 114 | hf_vcpu_run(ECHO_VM_ID, 0), |
| 115 | HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_MESSAGE, 0, sizeof(message))); |
| 116 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 117 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 118 | } |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Send a message to relay_a which will forward it to relay_b where it will be |
| 122 | * sent back here. |
| 123 | */ |
| 124 | TEST(mailbox, relay) |
| 125 | { |
| 126 | const char message[] = "Send this round the relay!"; |
| 127 | |
| 128 | /* Configure mailbox pages. */ |
| 129 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
| 130 | EXPECT_EQ(hf_vcpu_run(RELAY_A_VM_ID, 0), |
| 131 | HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAIT_FOR_INTERRUPT, 0, 0)); |
| 132 | EXPECT_EQ(hf_vcpu_run(RELAY_B_VM_ID, 0), |
| 133 | HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAIT_FOR_INTERRUPT, 0, 0)); |
| 134 | |
| 135 | /* |
| 136 | * Send the message to relay_a which is then sent to relay_b before |
| 137 | * checking that relay_b send the message back here. |
| 138 | */ |
| 139 | memcpy(send_page, message, sizeof(message)); |
| 140 | EXPECT_EQ(hf_mailbox_send(RELAY_A_VM_ID, sizeof(message)), 0); |
| 141 | EXPECT_EQ(hf_vcpu_run(RELAY_A_VM_ID, 0), |
| 142 | HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAKE_UP, RELAY_B_VM_ID, 0)); |
| 143 | EXPECT_EQ( |
| 144 | hf_vcpu_run(RELAY_B_VM_ID, 0), |
| 145 | HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_MESSAGE, 0, sizeof(message))); |
| 146 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 147 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 148 | } |