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 | /** |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame^] | 46 | * Reverses the order of the elements in the given array. |
| 47 | */ |
| 48 | void reverse(char *s, size_t len) |
| 49 | { |
| 50 | size_t i; |
| 51 | |
| 52 | for (i = 0; i < len / 2; i++) { |
| 53 | char t = s[i]; |
| 54 | s[i] = s[len - 1 - i]; |
| 55 | s[len - 1 - i] = t; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Finds the next lexicographic permutation of the given array, if there is one. |
| 61 | */ |
| 62 | void next_permutation(char *s, size_t len) |
| 63 | { |
| 64 | size_t i, j; |
| 65 | |
| 66 | for (i = len - 2; i < len; i--) { |
| 67 | const char t = s[i]; |
| 68 | if (t >= s[i + 1]) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | for (j = len - 1; t >= s[j]; j--) { |
| 73 | } |
| 74 | |
| 75 | s[i] = s[j]; |
| 76 | s[j] = t; |
| 77 | reverse(s + i + 1, len - i - 1); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 83 | * Confirm there are 3 secondary VMs as well as this primary VM. |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 84 | */ |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 85 | TEST(hf_vm_get_count, three_secondary_vms) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 86 | { |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 87 | EXPECT_EQ(hf_vm_get_count(), 4); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 90 | /** |
| 91 | * Confirm there that secondary VM has 1 VCPU. |
| 92 | */ |
| 93 | TEST(hf_vcpu_get_count, secondary_has_one_vcpu) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 94 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 95 | EXPECT_EQ(hf_vcpu_get_count(1), 1); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 98 | /** |
| 99 | * Confirm it is an error to query how many VCPUs are assigned to a nonexistent |
| 100 | * secondary VM. |
| 101 | */ |
| 102 | TEST(hf_vcpu_get_count, large_invalid_vm_index) |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 103 | { |
| 104 | EXPECT_EQ(hf_vcpu_get_count(0xffffffff), -1); |
| 105 | } |
| 106 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 107 | /** |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 108 | * The primary can't be run by the hypervisor. |
| 109 | */ |
| 110 | TEST(hf_vcpu_run, cannot_run_primary) |
| 111 | { |
| 112 | struct hf_vcpu_run_return res = hf_vcpu_run(HF_PRIMARY_VM_ID, 0); |
| 113 | EXPECT_EQ(res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Can only run a VM that exists. |
| 118 | */ |
| 119 | TEST(hf_vcpu_run, cannot_run_absent_secondary) |
| 120 | { |
| 121 | struct hf_vcpu_run_return res = hf_vcpu_run(1234, 0); |
| 122 | EXPECT_EQ(res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Can only run a vcpu that exists. |
| 127 | */ |
| 128 | TEST(hf_vcpu_run, cannot_run_absent_vcpu) |
| 129 | { |
| 130 | struct hf_vcpu_run_return res = hf_vcpu_run(ECHO_VM_ID, 1234); |
| 131 | EXPECT_EQ(res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 132 | } |
| 133 | |
| 134 | /** |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 135 | * The configured send/receive addresses can't be unaligned. |
| 136 | */ |
| 137 | TEST(hf_vm_configure, fails_with_unaligned_pointer) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 138 | { |
| 139 | uint8_t maybe_aligned[2]; |
| 140 | hf_ipaddr_t unaligned_addr = (hf_ipaddr_t)&maybe_aligned[1]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 141 | hf_ipaddr_t aligned_addr = (hf_ipaddr_t)send_page; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 142 | |
| 143 | /* Check the the address is unaligned. */ |
| 144 | ASSERT_EQ(unaligned_addr & 1, 1); |
| 145 | |
| 146 | EXPECT_EQ(hf_vm_configure(aligned_addr, unaligned_addr), -1); |
| 147 | EXPECT_EQ(hf_vm_configure(unaligned_addr, aligned_addr), -1); |
| 148 | EXPECT_EQ(hf_vm_configure(unaligned_addr, unaligned_addr), -1); |
| 149 | } |
| 150 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 151 | /** |
| 152 | * The configured send/receive addresses can't be the same page. |
| 153 | */ |
| 154 | TEST(hf_vm_configure, fails_with_same_page) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 155 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 156 | EXPECT_EQ(hf_vm_configure(send_page_addr, send_page_addr), -1); |
| 157 | EXPECT_EQ(hf_vm_configure(recv_page_addr, recv_page_addr), -1); |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 160 | /** |
| 161 | * The configuration of the send/receive addresses can only happen once. |
| 162 | */ |
| 163 | TEST(hf_vm_configure, fails_if_already_succeeded) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 164 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 165 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
| 166 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), -1); |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 169 | /** |
| 170 | * The configuration of the send/receive address is successful with valid |
| 171 | * arguments. |
| 172 | */ |
| 173 | TEST(hf_vm_configure, succeeds) |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 174 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 175 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 176 | } |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 177 | |
| 178 | /** |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 179 | * The primary receives messages from hf_vcpu_run(). |
| 180 | */ |
| 181 | TEST(hf_mailbox_receive, cannot_receive_from_primary_blocking) |
| 182 | { |
| 183 | struct hf_mailbox_receive_return res = hf_mailbox_receive(true); |
| 184 | EXPECT_EQ(res.vm_id, HF_INVALID_VM_ID); |
| 185 | EXPECT_EQ(res.size, 0); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * The primary receives messages from hf_vcpu_run(). |
| 190 | */ |
| 191 | TEST(hf_mailbox_receive, cannot_receive_from_primary_non_blocking) |
| 192 | { |
| 193 | struct hf_mailbox_receive_return res = hf_mailbox_receive(false); |
| 194 | EXPECT_EQ(res.vm_id, HF_INVALID_VM_ID); |
| 195 | EXPECT_EQ(res.size, 0); |
| 196 | } |
| 197 | |
| 198 | /** |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 199 | * Send and receive the same message from the echo VM. |
| 200 | */ |
| 201 | TEST(mailbox, echo) |
| 202 | { |
| 203 | const char message[] = "Echo this back to me!"; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 204 | struct hf_vcpu_run_return run_res; |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 205 | |
| 206 | /* Configure mailbox pages. */ |
| 207 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 208 | run_res = hf_vcpu_run(ECHO_VM_ID, 0); |
| 209 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 210 | |
| 211 | /* Set the message, echo it and check it didn't change. */ |
| 212 | memcpy(send_page, message, sizeof(message)); |
| 213 | EXPECT_EQ(hf_mailbox_send(ECHO_VM_ID, sizeof(message)), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 214 | run_res = hf_vcpu_run(ECHO_VM_ID, 0); |
| 215 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 216 | EXPECT_EQ(run_res.message.size, sizeof(message)); |
Andrew Scull | 4b6c2fc | 2018-10-05 18:14:02 +0100 | [diff] [blame] | 217 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 218 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 219 | } |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 220 | |
| 221 | /** |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame^] | 222 | * Repeatedly send a message and receive it back from the echo VM. |
| 223 | */ |
| 224 | TEST(mailbox, repeated_echo) |
| 225 | { |
| 226 | char message[] = "Echo this back to me!"; |
| 227 | struct hf_vcpu_run_return run_res; |
| 228 | uint8_t i; |
| 229 | |
| 230 | /* Configure mailbox pages. */ |
| 231 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
| 232 | |
| 233 | for (i = 0; i < 100; i++) { |
| 234 | /* Run secondary until it reaches the wait for messages. */ |
| 235 | run_res = hf_vcpu_run(ECHO_VM_ID, 0); |
| 236 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 237 | |
| 238 | /* Set the message, echo it and check it didn't change. */ |
| 239 | next_permutation(message, sizeof(message) - 1); |
| 240 | memcpy(send_page, message, sizeof(message)); |
| 241 | EXPECT_EQ(hf_mailbox_send(ECHO_VM_ID, sizeof(message)), 0); |
| 242 | run_res = hf_vcpu_run(ECHO_VM_ID, 0); |
| 243 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 244 | EXPECT_EQ(run_res.message.size, sizeof(message)); |
| 245 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 246 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 251 | * Send a message to relay_a which will forward it to relay_b where it will be |
| 252 | * sent back here. |
| 253 | */ |
| 254 | TEST(mailbox, relay) |
| 255 | { |
| 256 | const char message[] = "Send this round the relay!"; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 257 | struct hf_vcpu_run_return run_res; |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 258 | |
| 259 | /* Configure mailbox pages. */ |
| 260 | EXPECT_EQ(hf_vm_configure(send_page_addr, recv_page_addr), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 261 | run_res = hf_vcpu_run(RELAY_A_VM_ID, 0); |
| 262 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
| 263 | run_res = hf_vcpu_run(RELAY_B_VM_ID, 0); |
| 264 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * Send the message to relay_a which is then sent to relay_b before |
| 268 | * checking that relay_b send the message back here. |
| 269 | */ |
| 270 | memcpy(send_page, message, sizeof(message)); |
| 271 | EXPECT_EQ(hf_mailbox_send(RELAY_A_VM_ID, sizeof(message)), 0); |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 272 | run_res = hf_vcpu_run(RELAY_A_VM_ID, 0); |
| 273 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAKE_UP); |
| 274 | EXPECT_EQ(run_res.wake_up.vm_id, RELAY_B_VM_ID); |
| 275 | EXPECT_EQ(run_res.wake_up.vcpu, 0); |
| 276 | run_res = hf_vcpu_run(RELAY_B_VM_ID, 0); |
| 277 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 278 | EXPECT_EQ(run_res.message.size, sizeof(message)); |
Andrew Scull | 3a94257 | 2018-10-05 21:36:09 +0100 | [diff] [blame] | 279 | EXPECT_EQ(memcmp(recv_page, message, sizeof(message)), 0); |
| 280 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 281 | } |