blob: ffb28ffe8d03e8e949a9304755be9e0c95d02c02 [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 {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000162 uint32_t *chain = (uint32_t *)mb.send->payload;
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000163 *chain++ = htole32(SERVICE_VM1);
164 *chain++ = htole32(HF_PRIMARY_VM_ID);
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100165 memcpy_s(chain, SPCI_MSG_PAYLOAD_MAX - (2 * sizeof(uint32_t)),
166 message, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000167
168 spci_message_init(mb.send,
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000169 sizeof(message) + (2 * sizeof(uint32_t)),
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000170 SERVICE_VM0, HF_PRIMARY_VM_ID);
171 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000172 }
173
174 /* Let SERVICE_VM0 forward the message. */
175 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000176 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
177 EXPECT_EQ(run_res.message.vm_id, SERVICE_VM1);
Andrew Scull80232362019-04-01 12:37:41 +0100178 EXPECT_EQ(mb.recv->length, 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000179
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 Scullb06d1752019-02-04 10:15:48 +0000185 EXPECT_EQ(run_res.message.vm_id, HF_PRIMARY_VM_ID);
Andrew Scull80232362019-04-01 12:37:41 +0100186 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000187 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000188 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 */
195TEST(mailbox, no_primary_to_secondary_notification_on_configure)
196{
197 struct hf_vcpu_run_return run_res;
198
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000199 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 Scull2e7a76d2019-01-24 11:47:26 +0000202
203 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000204 spci_message_init(mb.send, 0, SERVICE_VM0, HF_PRIMARY_VM_ID);
Andrew Scullb06d1752019-02-04 10:15:48 +0000205 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
206 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000207
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000208 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000209}
210
211/**
212 * Send a message before the secondary VM is configured, and receive a
213 * notification when it configures.
214 */
215TEST(mailbox, secondary_to_primary_notification_on_configure)
216{
217 struct hf_vcpu_run_return run_res;
218
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000219 struct mailbox_buffers mb = set_up_mailbox();
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000220
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000221 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 Scull2e7a76d2019-01-24 11:47:26 +0000223
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 Scullb06d1752019-02-04 10:15:48 +0000235 /* Send should now succeed. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000236 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000237}
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 */
244TEST(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 Scullb06d1752019-02-04 10:15:48 +0000253 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
254 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000255
256 /* Send a message to echo service, and get response back. */
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100257 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
258 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000259 spci_message_init(mb.send, sizeof(message), SERVICE_VM0,
260 HF_PRIMARY_VM_ID);
261 EXPECT_EQ(spci_msg_send(0), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000262 run_res = hf_vcpu_run(SERVICE_VM0, 0);
263 EXPECT_EQ(run_res.code, HF_VCPU_RUN_MESSAGE);
Andrew Scull80232362019-04-01 12:37:41 +0100264 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000265 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000266
267 /* Let secondary VM continue running so that it will wait again. */
268 run_res = hf_vcpu_run(SERVICE_VM0, 0);
Andrew Scullb06d1752019-02-04 10:15:48 +0000269 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
270 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000271
272 /* Without clearing our mailbox, send message again. */
273 reverse(message, strlen(message));
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100274 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
275 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000276 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 Scull2e7a76d2019-01-24 11:47:26 +0000281 run_res = hf_vcpu_run(SERVICE_VM0, 0);
282 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT);
Andrew Scullb06d1752019-02-04 10:15:48 +0000283 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000284
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 Scull80232362019-04-01 12:37:41 +0100301 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000302 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000303}
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 */
310TEST(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 Scullb06d1752019-02-04 10:15:48 +0000319 EXPECT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_MESSAGE);
320 EXPECT_EQ(run_res.sleep.ns, HF_SLEEP_INDEFINITE);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000321
322 /* Send a message to echo service twice. The second should fail. */
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100323 memcpy_s(mb.send->payload, SPCI_MSG_PAYLOAD_MAX, message,
324 sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000325 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 Scull2e7a76d2019-01-24 11:47:26 +0000329
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 Scull80232362019-04-01 12:37:41 +0100333 EXPECT_EQ(mb.recv->length, sizeof(message));
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000334 EXPECT_EQ(memcmp(mb.recv->payload, message, sizeof(message)), 0);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000335
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 Scullb06d1752019-02-04 10:15:48 +0000344 /* Send should now succeed. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000345 EXPECT_EQ(spci_msg_send(0), SPCI_SUCCESS);
Andrew Scull2e7a76d2019-01-24 11:47:26 +0000346}