Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <stdalign.h> |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/mm.h" |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 22 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 24 | #include "vmapi/hf/call.h" |
| 25 | |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 26 | #include "hftest.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 27 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 28 | static alignas(PAGE_SIZE) uint8_t send_page[PAGE_SIZE]; |
| 29 | static alignas(PAGE_SIZE) uint8_t recv_page[PAGE_SIZE]; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 30 | static_assert(sizeof(send_page) == PAGE_SIZE, "Send page is not a page."); |
| 31 | 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] | 32 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 33 | static hf_ipaddr_t send_page_addr = (hf_ipaddr_t)send_page; |
| 34 | static hf_ipaddr_t recv_page_addr = (hf_ipaddr_t)recv_page; |
| 35 | |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 36 | /* Keep macro alignment */ |
| 37 | /* clang-format off */ |
| 38 | |
| 39 | #define RELAY_A_VM_ID 1 |
| 40 | #define RELAY_B_VM_ID 2 |
| 41 | #define ECHO_VM_ID 3 |
| 42 | |
| 43 | /* clang-format on */ |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 44 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 45 | /** |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 46 | * Confirm there are 3 secondary VMs as well as this primary VM. |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 47 | */ |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 48 | TEST(hf_vm_get_count, three_secondary_vms) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 49 | { |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 50 | EXPECT_EQ(hf_vm_get_count(), 4); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 53 | /** |
| 54 | * Confirm there that secondary VM has 1 VCPU. |
| 55 | */ |
| 56 | TEST(hf_vcpu_get_count, secondary_has_one_vcpu) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 57 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 58 | EXPECT_EQ(hf_vcpu_get_count(1), 1); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 61 | /** |
| 62 | * Confirm it is an error to query how many VCPUs are assigned to a nonexistent |
| 63 | * secondary VM. |
| 64 | */ |
| 65 | TEST(hf_vcpu_get_count, large_invalid_vm_index) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 66 | { |
| 67 | EXPECT_EQ(hf_vcpu_get_count(0xffffffff), -1); |
| 68 | } |
| 69 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 70 | /** |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 71 | * The primary can't be run by the hypervisor. |
| 72 | */ |
| 73 | TEST(hf_vcpu_run, cannot_run_primary) |
| 74 | { |
| 75 | struct hf_vcpu_run_return res = hf_vcpu_run(HF_PRIMARY_VM_ID, 0); |
| 76 | EXPECT_EQ(res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Can only run a VM that exists. |
| 81 | */ |
| 82 | TEST(hf_vcpu_run, cannot_run_absent_secondary) |
| 83 | { |
| 84 | struct hf_vcpu_run_return res = hf_vcpu_run(1234, 0); |
| 85 | EXPECT_EQ(res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Can only run a vcpu that exists. |
| 90 | */ |
| 91 | TEST(hf_vcpu_run, cannot_run_absent_vcpu) |
| 92 | { |
| 93 | struct hf_vcpu_run_return res = hf_vcpu_run(ECHO_VM_ID, 1234); |
| 94 | EXPECT_EQ(res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 95 | } |
| 96 | |
| 97 | /** |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 98 | * The configured send/receive addresses can't be unaligned. |
| 99 | */ |
| 100 | TEST(hf_vm_configure, fails_with_unaligned_pointer) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 101 | { |
| 102 | uint8_t maybe_aligned[2]; |
| 103 | hf_ipaddr_t unaligned_addr = (hf_ipaddr_t)&maybe_aligned[1]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 104 | hf_ipaddr_t aligned_addr = (hf_ipaddr_t)send_page; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 105 | |
| 106 | /* Check the the address is unaligned. */ |
| 107 | ASSERT_EQ(unaligned_addr & 1, 1); |
| 108 | |
| 109 | EXPECT_EQ(hf_vm_configure(aligned_addr, unaligned_addr), -1); |
| 110 | EXPECT_EQ(hf_vm_configure(unaligned_addr, aligned_addr), -1); |
| 111 | EXPECT_EQ(hf_vm_configure(unaligned_addr, unaligned_addr), -1); |
| 112 | } |
| 113 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 114 | /** |
| 115 | * The configured send/receive addresses can't be the same page. |
| 116 | */ |
| 117 | TEST(hf_vm_configure, fails_with_same_page) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 118 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 119 | EXPECT_EQ(hf_vm_configure(send_page_addr, send_page_addr), -1); |
| 120 | EXPECT_EQ(hf_vm_configure(recv_page_addr, recv_page_addr), -1); |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 123 | /** |
| 124 | * The configuration of the send/receive addresses can only happen once. |
| 125 | */ |
| 126 | TEST(hf_vm_configure, fails_if_already_succeeded) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 127 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 128 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
| 129 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), -1); |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 132 | /** |
| 133 | * The configuration of the send/receive address is successful with valid |
| 134 | * arguments. |
| 135 | */ |
| 136 | TEST(hf_vm_configure, succeeds) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 137 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 138 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 139 | } |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 140 | |
| 141 | /** |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 142 | * The primary receives messages from hf_vcpu_run(). |
| 143 | */ |
| 144 | TEST(hf_mailbox_receive, cannot_receive_from_primary_blocking) |
| 145 | { |
| 146 | struct hf_mailbox_receive_return res = hf_mailbox_receive(true); |
| 147 | EXPECT_EQ(res.vm_id, HF_INVALID_VM_ID); |
| 148 | EXPECT_EQ(res.size, 0); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * The primary receives messages from hf_vcpu_run(). |
| 153 | */ |
| 154 | TEST(hf_mailbox_receive, cannot_receive_from_primary_non_blocking) |
| 155 | { |
| 156 | struct hf_mailbox_receive_return res = hf_mailbox_receive(false); |
| 157 | EXPECT_EQ(res.vm_id, HF_INVALID_VM_ID); |
| 158 | EXPECT_EQ(res.size, 0); |
| 159 | } |
| 160 | |
| 161 | /** |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 162 | * Send and receive the same message from the echo VM. |
| 163 | */ |
| 164 | TEST(mailbox, echo) |
| 165 | { |
| 166 | const char message[] = "Echo this back to me!"; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 167 | struct hf_vcpu_run_return run_res; |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 168 | |
| 169 | /* Configure mailbox pages. */ |
| 170 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 171 | run_res = hf_vcpu_run(ECHO_VM_ID, 0); |
| 172 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 173 | |
| 174 | /* Set the message, echo it and check it didn't change. */ |
| 175 | memcpy(send_page, message, sizeof(message)); |
| 176 | EXPECT_EQ(hf_mailbox_send(ECHO_VM_ID, sizeof(message)), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 177 | run_res = hf_vcpu_run(ECHO_VM_ID, 0); |
| 178 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 179 | EXPECT_EQ(run_res.message.size, sizeof(message)); |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 180 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 181 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 182 | } |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Send a message to relay_a which will forward it to relay_b where it will be |
| 186 | * sent back here. |
| 187 | */ |
| 188 | TEST(mailbox, relay) |
| 189 | { |
| 190 | const char message[] = "Send this round the relay!"; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 191 | struct hf_vcpu_run_return run_res; |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 192 | |
| 193 | /* Configure mailbox pages. */ |
| 194 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 195 | run_res = hf_vcpu_run(RELAY_A_VM_ID, 0); |
| 196 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 197 | run_res = hf_vcpu_run(RELAY_B_VM_ID, 0); |
| 198 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Send the message to relay_a which is then sent to relay_b before |
| 202 | * checking that relay_b send the message back here. |
| 203 | */ |
| 204 | memcpy(send_page, message, sizeof(message)); |
| 205 | EXPECT_EQ(hf_mailbox_send(RELAY_A_VM_ID, sizeof(message)), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame^] | 206 | run_res = hf_vcpu_run(RELAY_A_VM_ID, 0); |
| 207 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAKE_UP); |
| 208 | EXPECT_EQ(run_res.wake_up.vm_id, RELAY_B_VM_ID); |
| 209 | EXPECT_EQ(run_res.wake_up.vcpu, 0); |
| 210 | run_res = hf_vcpu_run(RELAY_B_VM_ID, 0); |
| 211 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 212 | EXPECT_EQ(run_res.message.size, sizeof(message)); |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 213 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 214 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 215 | } |