blob: adb4ba267c992b0c8162a22c121bbd084f686b12 [file] [log] [blame]
Andrew Scull2e7a76d2019-01-24 11:47:26 +00001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2019 The Hafnium Authors.
Andrew Scull2e7a76d2019-01-24 11:47:26 +00003 *
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 Marinhoa1dfeda2019-02-27 16:46:03 +000019#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010020#include "hf/std.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000021
Andrew Scull2e7a76d2019-01-24 11:47:26 +000022#include "vmapi/hf/call.h"
23
24#include "hftest.h"
25#include "primary_with_secondary.h"
Andrew Walbran53d9d042019-03-28 11:35:49 +000026#include "util.h"
Andrew Scull2e7a76d2019-01-24 11:47:26 +000027
28/**
29 * Reverses the order of the elements in the given array.
30 */
31static 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 */
45static 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 Scullaa7db8e2019-02-01 14:12:19 +000066 * Clearing an empty mailbox is a noop.
67 */
68TEST(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 Scull2e7a76d2019-01-24 11:47:26 +000076 * Send and receive the same message from the echo VM.
77 */
78TEST(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 Scullb06d1752019-02-04 10:15:48 +000087 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
88 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +000089
90 /* Set the message, echo it and check it didn't change. */
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010091 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
92 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000093 spci_message_init(mb.send, sizeof(message), SERVICE_VM0,
94 HF_PRIMARY_VM_ID);
95 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +000096 run_res = hf_vcpu_run(SERVICE_VM0, 0);
97 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
Andrew Scull80232362019-04-01 12:37:41 +010098 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000099 EXPECT_EQ(memcmp(mb.send->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000100 EXPECT_EQ(hf_mailbox_clear(), 0);
101}
102
103/**
104 * Repeatedly send a message and receive it back from the echo VM.
105 */
106TEST(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 Scullb06d1752019-02-04 10:15:48 +0000118 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
119 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000120
121 /* Set the message, echo it and check it didn't change. */
122 next_permutation(message, sizeof(message) - 1);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100123 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
124 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000125 spci_message_init(mb.send, sizeof(message), SERVICE_VM0,
126 HF_PRIMARY_VM_ID);
127 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000128 run_res = hf_vcpu_run(SERVICE_VM0, 0);
129 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
Andrew Scull80232362019-04-01 12:37:41 +0100130 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000131 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)),
132 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000133 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 */
141TEST(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 Scullb06d1752019-02-04 10:15:48 +0000151 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
152 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000153 run_res = hf_vcpu_run(SERVICE_VM1, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000154 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
155 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000156
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 {
Andrew Walbran95534922019-06-19 11:32:54 +0100162 spci_vm_id_t *chain = (spci_vm_id_t *)mb.send->payload;
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000163 *chain++ = htole32(SERVICE_VM1);
164 *chain++ = htole32(HF_PRIMARY_VM_ID);
Andrew Walbran95534922019-06-19 11:32:54 +0100165 memcpy_s(chain,
166 SPCI_MSG_PAYLOAD_MAX - (2 * sizeof(spci_vm_id_t)),
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100167 message, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000168
169 spci_message_init(mb.send,
Andrew Walbran95534922019-06-19 11:32:54 +0100170 sizeof(message) + (2 * sizeof(spci_vm_id_t)),
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000171 SERVICE_VM0, HF_PRIMARY_VM_ID);
172 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000173 }
174
175 /* Let SERVICE_VM0 forward the message. */
176 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000177 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
178 EXPECT_EQ(run_res.message.vm_id, SERVICE_VM1);
Andrew Scull80232362019-04-01 12:37:41 +0100179 EXPECT_EQ(mb.recv->length, 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000180
181 /* Let SERVICE_VM1 forward the message. */
182 run_res = hf_vcpu_run(SERVICE_VM1, 0);
183 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
184
Andrew Walbran95534922019-06-19 11:32:54 +0100185 /* Ensure the message is intact. */
Andrew Scullb06d1752019-02-04 10:15:48 +0000186 EXPECT_EQ(run_res.message.vm_id, HF_PRIMARY_VM_ID);
Andrew Scull80232362019-04-01 12:37:41 +0100187 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000188 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000189 EXPECT_EQ(hf_mailbox_clear(), 0);
190}
191
192/**
193 * Send a message before the secondary VM is configured, but do not register
194 * for notification. Ensure we're not notified.
195 */
196TEST(mailbox, no_primary_to_secondary_notification_on_configure)
197{
198 struct hf_vcpu_run_return run_res;
199
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000200 struct mailbox_buffers mb = set_up_mailbox();
201 spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID);
202 EXPECT_EQ(spci_msg_send(0), SPCI_BUSY);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000203
204 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000205 spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID);
Andrew Scullb06d1752019-02-04 10:15:48 +0000206 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
207 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000208
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000209 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000210}
211
212/**
213 * Send a message before the secondary VM is configured, and receive a
214 * notification when it configures.
215 */
216TEST(mailbox, secondary_to_primary_notification_on_configure)
217{
218 struct hf_vcpu_run_return run_res;
219
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000220 struct mailbox_buffers mb = set_up_mailbox();
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000221
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000222 spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID);
223 EXPECT_EQ(spci_msg_send(SPCI_MSG_SEND_NOTIFY), SPCI_BUSY);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000224
225 /*
226 * Run first VM for it to configure itself. It should result in
227 * notifications having to be issued.
228 */
229 run_res = hf_vcpu_run(SERVICE_VM0, 0);
230 EXPECT_EQ(run_res.code, HF_VCPU_RUN_NOTIFY_WAITERS);
231
232 /* A single waiter is returned. */
233 EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), HF_PRIMARY_VM_ID);
234 EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), -1);
235
Andrew Scullb06d1752019-02-04 10:15:48 +0000236 /* Send should now succeed. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000237 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000238}
239
240/**
241 * Causes secondary VM to send two messages to primary VM. The second message
242 * will reach the mailbox while it's not writable. Checks that notifications are
243 * properly delivered when mailbox is cleared.
244 */
245TEST(mailbox, primary_to_secondary)
246{
247 char message[] = "not ready echo";
248 struct hf_vcpu_run_return run_res;
249 struct mailbox_buffers mb = set_up_mailbox();
250
251 SERVICE_SELECT(SERVICE_VM0, "echo_with_notification", mb.send);
252
253 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000254 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
255 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000256
257 /* Send a message to echo service, and get response back. */
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100258 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
259 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000260 spci_message_init(mb.send, sizeof(message), SERVICE_VM0,
261 HF_PRIMARY_VM_ID);
262 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000263 run_res = hf_vcpu_run(SERVICE_VM0, 0);
264 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
Andrew Scull80232362019-04-01 12:37:41 +0100265 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000266 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000267
268 /* Let secondary VM continue running so that it will wait again. */
269 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000270 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
271 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000272
273 /* Without clearing our mailbox, send message again. */
Andrew Scull55baca62019-04-05 14:56:20 +0100274 reverse(message, strnlen_s(message, sizeof(message)));
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100275 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
276 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000277 spci_message_init(mb.send, sizeof(message), SERVICE_VM0,
278 HF_PRIMARY_VM_ID);
279
280 /* Message should be dropped since the mailbox was not cleared. */
281 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000282 run_res = hf_vcpu_run(SERVICE_VM0, 0);
283 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT);
Andrew Scullb06d1752019-02-04 10:15:48 +0000284 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000285
286 /* Clear the mailbox. We expect to be told there are pending waiters. */
287 EXPECT_EQ(hf_mailbox_clear(), 1);
288
289 /* Retrieve a single waiter. */
290 EXPECT_EQ(hf_mailbox_waiter_get(HF_PRIMARY_VM_ID), SERVICE_VM0);
291 EXPECT_EQ(hf_mailbox_waiter_get(HF_PRIMARY_VM_ID), -1);
292
293 /*
294 * Inject interrupt into VM and let it run again. We should receive
295 * the echoed message.
296 */
297 EXPECT_EQ(
298 hf_interrupt_inject(SERVICE_VM0, 0, HF_MAILBOX_WRITABLE_INTID),
299 1);
300 run_res = hf_vcpu_run(SERVICE_VM0, 0);
301 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
Andrew Scull80232362019-04-01 12:37:41 +0100302 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000303 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000304}
305
306/**
307 * Sends two messages to secondary VM without letting it run, so second message
308 * won't go through. Ensure that a notification is delivered when secondary VM
309 * clears the mailbox.
310 */
311TEST(mailbox, secondary_to_primary_notification)
312{
313 const char message[] = "not ready echo";
314 struct hf_vcpu_run_return run_res;
315 struct mailbox_buffers mb = set_up_mailbox();
316
317 SERVICE_SELECT(SERVICE_VM0, "echo_with_notification", mb.send);
318
319 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000320 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
321 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000322
323 /* Send a message to echo service twice. The second should fail. */
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100324 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
325 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000326 spci_message_init(mb.send, sizeof(message), SERVICE_VM0,
327 HF_PRIMARY_VM_ID);
328 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
329 EXPECT_EQ(spci_msg_send(SPCI_MSG_SEND_NOTIFY), SPCI_BUSY);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000330
331 /* Receive a reply for the first message. */
332 run_res = hf_vcpu_run(SERVICE_VM0, 0);
333 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
Andrew Scull80232362019-04-01 12:37:41 +0100334 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000335 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000336
337 /* Run VM again so that it clears its mailbox. */
338 run_res = hf_vcpu_run(SERVICE_VM0, 0);
339 EXPECT_EQ(run_res.code, HF_VCPU_RUN_NOTIFY_WAITERS);
340
341 /* Retrieve a single waiter. */
342 EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), HF_PRIMARY_VM_ID);
343 EXPECT_EQ(hf_mailbox_waiter_get(SERVICE_VM0), -1);
344
Andrew Scullb06d1752019-02-04 10:15:48 +0000345 /* Send should now succeed. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000346 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000347}