blob: 6212ecbc99e2acccf2f078bce6ea224c47e7bc5f [file] [log] [blame]
/*
* Copyright 2018 The Hafnium Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdalign.h>
#include <stdint.h>
#include "hf/arch/vm/power_mgmt.h"
#include "hf/mm.h"
#include "hf/std.h"
#include "vmapi/hf/call.h"
#include "hftest.h"
#include "primary_with_secondary.h"
#include "util.h"
/**
* Iterates trying to run vCPU of the secondary VM. Returns when a message
* of non-zero length is received.
*/
static bool run_loop(struct mailbox_buffers *mb)
{
struct spci_value run_res;
bool ok = false;
for (;;) {
/* Run until it manages to schedule vCPU on this CPU. */
do {
run_res = spci_run(SERVICE_VM1, 0);
} while (run_res.func == SPCI_ERROR_32 &&
run_res.arg2 == SPCI_BUSY);
/* Break out if we received a message with non-zero length. */
if (run_res.func == SPCI_MSG_SEND_32 &&
spci_msg_send_size(run_res) != 0) {
break;
}
/* Clear mailbox so that next message can be received. */
spci_rx_release();
}
/* Copies the contents of the received boolean to the return value. */
if (spci_msg_send_size(run_res) == sizeof(ok)) {
ok = *(bool *)mb->recv;
}
spci_rx_release();
return ok;
}
/**
* This is the entry point of the additional primary VM vCPU. It just calls
* the run loop so that two cpus compete for the chance to run a secondary VM.
*/
static void vm_cpu_entry(uintptr_t arg)
{
run_loop((struct mailbox_buffers *)arg);
}
/**
* This test tries to run the same secondary vCPU from two different physical
* CPUs concurrently. The vCPU checks that the state is ok while it bounces
* between the physical CPUs.
*
* Test is marked long-running because our implementation of spin-locks does not
* perform well under QEMU.
*/
TEST_LONG_RUNNING(vcpu_state, concurrent_save_restore)
{
alignas(4096) static char stack[4096];
static struct mailbox_buffers mb;
mb = set_up_mailbox();
SERVICE_SELECT(SERVICE_VM1, "check_state", mb.send);
/* Start second vCPU. */
ASSERT_TRUE(hftest_cpu_start(hftest_get_cpu_id(1), stack, sizeof(stack),
vm_cpu_entry, (uintptr_t)&mb));
/* Run on a loop until the secondary VM is done. */
EXPECT_TRUE(run_loop(&mb));
}