Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2019 The Hafnium Authors. |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 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 <stdint.h> |
| 18 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 19 | #include "hf/spci.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 20 | #include "hf/std.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 21 | |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 22 | #include "vmapi/hf/call.h" |
| 23 | |
| 24 | #include "hftest.h" |
| 25 | #include "primary_with_secondary.h" |
Andrew Walbran | 53d9d04 | 2019-03-28 11:35:49 +0000 | [diff] [blame] | 26 | #include "util.h" |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Reverses the order of the elements in the given array. |
| 30 | */ |
| 31 | static void reverse(char *s, size_t len) |
| 32 | { |
| 33 | size_t i; |
| 34 | |
| 35 | for (i = 0; i < len / 2; i++) { |
| 36 | char t = s[i]; |
| 37 | s[i] = s[len - 1 - i]; |
| 38 | s[len - 1 - i] = t; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Finds the next lexicographic permutation of the given array, if there is one. |
| 44 | */ |
| 45 | static void next_permutation(char *s, size_t len) |
| 46 | { |
| 47 | size_t i, j; |
| 48 | |
| 49 | for (i = len - 2; i < len; i--) { |
| 50 | const char t = s[i]; |
| 51 | if (t >= s[i + 1]) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | for (j = len - 1; t >= s[j]; j--) { |
| 56 | } |
| 57 | |
| 58 | s[i] = s[j]; |
| 59 | s[j] = t; |
| 60 | reverse(s + i + 1, len - i - 1); |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 66 | * Clearing an empty mailbox is a noop. |
| 67 | */ |
| 68 | TEST(mailbox, clear_empty) |
| 69 | { |
| 70 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 71 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 72 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 73 | } |
| 74 | |
| 75 | /** |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 76 | * Send and receive the same message from the echo VM. |
| 77 | */ |
| 78 | TEST(mailbox, echo) |
| 79 | { |
| 80 | const char message[] = "Echo this back to me!"; |
| 81 | struct hf_vcpu_run_return run_res; |
| 82 | struct mailbox_buffers mb = set_up_mailbox(); |
| 83 | |
| 84 | SERVICE_SELECT(SERVICE_VM0, "echo", mb.send); |
| 85 | |
| 86 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 87 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 88 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 89 | |
| 90 | /* Set the message, echo it and check it didn't change. */ |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame^] | 91 | memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message, |
| 92 | sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 93 | spci_message_init(mb.send, sizeof(message), SERVICE_VM0, |
| 94 | HF_PRIMARY_VM_ID); |
| 95 | EXPECT_EQ(spci_msg_send(0), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 96 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 97 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 98 | EXPECT_EQ(mb.recv->length, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 99 | EXPECT_EQ(memcmp(mb.send->payload, message, sizeof(message)), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 100 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Repeatedly send a message and receive it back from the echo VM. |
| 105 | */ |
| 106 | TEST(mailbox, repeated_echo) |
| 107 | { |
| 108 | char message[] = "Echo this back to me!"; |
| 109 | struct hf_vcpu_run_return run_res; |
| 110 | uint8_t i; |
| 111 | struct mailbox_buffers mb = set_up_mailbox(); |
| 112 | |
| 113 | SERVICE_SELECT(SERVICE_VM0, "echo", mb.send); |
| 114 | |
| 115 | for (i = 0; i < 100; i++) { |
| 116 | /* Run secondary until it reaches the wait for messages. */ |
| 117 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 118 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 119 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 120 | |
| 121 | /* Set the message, echo it and check it didn't change. */ |
| 122 | next_permutation(message, sizeof(message) - 1); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame^] | 123 | memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message, |
| 124 | sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 125 | spci_message_init(mb.send, sizeof(message), SERVICE_VM0, |
| 126 | HF_PRIMARY_VM_ID); |
| 127 | EXPECT_EQ(spci_msg_send(0), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 128 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 129 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 130 | EXPECT_EQ(mb.recv->length, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 131 | EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), |
| 132 | 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 133 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Send a message to relay_a which will forward it to relay_b where it will be |
| 139 | * sent back here. |
| 140 | */ |
| 141 | TEST(mailbox, relay) |
| 142 | { |
| 143 | const char message[] = "Send this round the relay!"; |
| 144 | struct hf_vcpu_run_return run_res; |
| 145 | struct mailbox_buffers mb = set_up_mailbox(); |
| 146 | |
| 147 | SERVICE_SELECT(SERVICE_VM0, "relay", mb.send); |
| 148 | SERVICE_SELECT(SERVICE_VM1, "relay", mb.send); |
| 149 | |
| 150 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 151 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 152 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 153 | run_res = hf_vcpu_run(SERVICE_VM1, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 154 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 155 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Build the message chain so the message is sent from here to |
| 159 | * SERVICE_VM0, then to SERVICE_VM1 and finally back to here. |
| 160 | */ |
| 161 | { |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 162 | uint32_t *chain = (uint32_t *)mb.send->payload; |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 163 | *chain++ = htole32(SERVICE_VM1); |
| 164 | *chain++ = htole32(HF_PRIMARY_VM_ID); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame^] | 165 | memcpy_s(chain, SPCI_MSG_PAYLOAD_MAX - (2 * sizeof(uint32_t)), |
| 166 | message, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 167 | |
| 168 | spci_message_init(mb.send, |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 169 | sizeof(message) + (2 * sizeof(uint32_t)), |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 170 | SERVICE_VM0, HF_PRIMARY_VM_ID); |
| 171 | EXPECT_EQ(spci_msg_send(0), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Let SERVICE_VM0 forward the message. */ |
| 175 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 176 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 177 | EXPECT_EQ(run_res.message.vm_id, SERVICE_VM1); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 178 | EXPECT_EQ(mb.recv->length, 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 179 | |
| 180 | /* Let SERVICE_VM1 forward the message. */ |
| 181 | run_res = hf_vcpu_run(SERVICE_VM1, 0); |
| 182 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
| 183 | |
| 184 | /* Ensure the message is in tact. */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 185 | EXPECT_EQ(run_res.message.vm_id, HF_PRIMARY_VM_ID); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 186 | EXPECT_EQ(mb.recv->length, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 187 | EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 188 | EXPECT_EQ(hf_mailbox_clear(), 0); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Send a message before the secondary VM is configured, but do not register |
| 193 | * for notification. Ensure we're not notified. |
| 194 | */ |
| 195 | TEST(mailbox, no_primary_to_secondary_notification_on_configure) |
| 196 | { |
| 197 | struct hf_vcpu_run_return run_res; |
| 198 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 199 | struct mailbox_buffers mb = set_up_mailbox(); |
| 200 | spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID); |
| 201 | EXPECT_EQ(spci_msg_send(0), SPCI_BUSY); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 202 | |
| 203 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 204 | spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 205 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 206 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 207 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 208 | EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Send a message before the secondary VM is configured, and receive a |
| 213 | * notification when it configures. |
| 214 | */ |
| 215 | TEST(mailbox, secondary_to_primary_notification_on_configure) |
| 216 | { |
| 217 | struct hf_vcpu_run_return run_res; |
| 218 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 219 | struct mailbox_buffers mb = set_up_mailbox(); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 220 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 221 | spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID); |
| 222 | EXPECT_EQ(spci_msg_send(SPCI_MSG_SEND_NOTIFY), SPCI_BUSY); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * Run first VM for it to configure itself. It should result in |
| 226 | * notifications having to be issued. |
| 227 | */ |
| 228 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 229 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_NOTIFY_WAITERS); |
| 230 | |
| 231 | /* A single waiter is returned. */ |
| 232 | EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), HF_PRIMARY_VM_ID); |
| 233 | EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), -1); |
| 234 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 235 | /* Send should now succeed. */ |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 236 | EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Causes secondary VM to send two messages to primary VM. The second message |
| 241 | * will reach the mailbox while it's not writable. Checks that notifications are |
| 242 | * properly delivered when mailbox is cleared. |
| 243 | */ |
| 244 | TEST(mailbox, primary_to_secondary) |
| 245 | { |
| 246 | char message[] = "not ready echo"; |
| 247 | struct hf_vcpu_run_return run_res; |
| 248 | struct mailbox_buffers mb = set_up_mailbox(); |
| 249 | |
| 250 | SERVICE_SELECT(SERVICE_VM0, "echo_with_notification", mb.send); |
| 251 | |
| 252 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 253 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 254 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 255 | |
| 256 | /* Send a message to echo service, and get response back. */ |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame^] | 257 | memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message, |
| 258 | sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 259 | spci_message_init(mb.send, sizeof(message), SERVICE_VM0, |
| 260 | HF_PRIMARY_VM_ID); |
| 261 | EXPECT_EQ(spci_msg_send(0), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 262 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 263 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 264 | EXPECT_EQ(mb.recv->length, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 265 | EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 266 | |
| 267 | /* Let secondary VM continue running so that it will wait again. */ |
| 268 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 269 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 270 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 271 | |
| 272 | /* Without clearing our mailbox, send message again. */ |
| 273 | reverse(message, strlen(message)); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame^] | 274 | memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message, |
| 275 | sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 276 | spci_message_init(mb.send, sizeof(message), SERVICE_VM0, |
| 277 | HF_PRIMARY_VM_ID); |
| 278 | |
| 279 | /* Message should be dropped since the mailbox was not cleared. */ |
| 280 | EXPECT_EQ(spci_msg_send(0), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 281 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 282 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 283 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 284 | |
| 285 | /* Clear the mailbox. We expect to be told there are pending waiters. */ |
| 286 | EXPECT_EQ(hf_mailbox_clear(), 1); |
| 287 | |
| 288 | /* Retrieve a single waiter. */ |
| 289 | EXPECT_EQ(hf_mailbox_waiter_get(HF_PRIMARY_VM_ID), SERVICE_VM0); |
| 290 | EXPECT_EQ(hf_mailbox_waiter_get(HF_PRIMARY_VM_ID), -1); |
| 291 | |
| 292 | /* |
| 293 | * Inject interrupt into VM and let it run again. We should receive |
| 294 | * the echoed message. |
| 295 | */ |
| 296 | EXPECT_EQ( |
| 297 | hf_interrupt_inject(SERVICE_VM0, 0, HF_MAILBOX_WRITABLE_INTID), |
| 298 | 1); |
| 299 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 300 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 301 | EXPECT_EQ(mb.recv->length, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 302 | EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Sends two messages to secondary VM without letting it run, so second message |
| 307 | * won't go through. Ensure that a notification is delivered when secondary VM |
| 308 | * clears the mailbox. |
| 309 | */ |
| 310 | TEST(mailbox, secondary_to_primary_notification) |
| 311 | { |
| 312 | const char message[] = "not ready echo"; |
| 313 | struct hf_vcpu_run_return run_res; |
| 314 | struct mailbox_buffers mb = set_up_mailbox(); |
| 315 | |
| 316 | SERVICE_SELECT(SERVICE_VM0, "echo_with_notification", mb.send); |
| 317 | |
| 318 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 319 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE); |
| 320 | EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 321 | |
| 322 | /* Send a message to echo service twice. The second should fail. */ |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame^] | 323 | memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message, |
| 324 | sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 325 | spci_message_init(mb.send, sizeof(message), SERVICE_VM0, |
| 326 | HF_PRIMARY_VM_ID); |
| 327 | EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS); |
| 328 | EXPECT_EQ(spci_msg_send(SPCI_MSG_SEND_NOTIFY), SPCI_BUSY); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 329 | |
| 330 | /* Receive a reply for the first message. */ |
| 331 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 332 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE); |
Andrew Scull | 8023236 | 2019-04-01 12:37:41 +0100 | [diff] [blame] | 333 | EXPECT_EQ(mb.recv->length, sizeof(message)); |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 334 | EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 335 | |
| 336 | /* Run VM again so that it clears its mailbox. */ |
| 337 | run_res = hf_vcpu_run(SERVICE_VM0, 0); |
| 338 | EXPECT_EQ(run_res.code, HF_VCPU_RUN_NOTIFY_WAITERS); |
| 339 | |
| 340 | /* Retrieve a single waiter. */ |
| 341 | EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), HF_PRIMARY_VM_ID); |
| 342 | EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), -1); |
| 343 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 344 | /* Send should now succeed. */ |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 345 | EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS); |
Andrew Scull | 2e7a76d | 2019-01-24 11:47:26 +0000 | [diff] [blame] | 346 | } |