Commit b26dafc8 authored by Oliver Upton's avatar Oliver Upton Committed by Marc Zyngier
Browse files

selftests: KVM: Test SYSTEM_SUSPEND PSCI call



Assert that the vCPU exits to userspace with KVM_SYSTEM_EVENT_SUSPEND if
the guest calls PSCI SYSTEM_SUSPEND. Additionally, guarantee that the
SMC32 and SMC64 flavors of this call are discoverable with the
PSCI_FEATURES call.

Signed-off-by: default avatarOliver Upton <oupton@google.com>
Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220504032446.4133305-13-oupton@google.com
parent 67a36a82
Loading
Loading
Loading
Loading
+69 −0
Original line number Diff line number Diff line
@@ -45,6 +45,25 @@ static uint64_t psci_affinity_info(uint64_t target_affinity,
	return res.a0;
}

static uint64_t psci_system_suspend(uint64_t entry_addr, uint64_t context_id)
{
	struct arm_smccc_res res;

	smccc_hvc(PSCI_1_0_FN64_SYSTEM_SUSPEND, entry_addr, context_id,
		  0, 0, 0, 0, 0, &res);

	return res.a0;
}

static uint64_t psci_features(uint32_t func_id)
{
	struct arm_smccc_res res;

	smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES, func_id, 0, 0, 0, 0, 0, 0, &res);

	return res.a0;
}

static void vcpu_power_off(struct kvm_vm *vm, uint32_t vcpuid)
{
	struct kvm_mp_state mp_state = {
@@ -137,8 +156,58 @@ static void host_test_cpu_on(void)
	kvm_vm_free(vm);
}

static void enable_system_suspend(struct kvm_vm *vm)
{
	struct kvm_enable_cap cap = {
		.cap = KVM_CAP_ARM_SYSTEM_SUSPEND,
	};

	vm_enable_cap(vm, &cap);
}

static void guest_test_system_suspend(void)
{
	uint64_t ret;

	/* assert that SYSTEM_SUSPEND is discoverable */
	GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND));
	GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND));

	ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID);
	GUEST_SYNC(ret);
}

static void host_test_system_suspend(void)
{
	struct kvm_run *run;
	struct kvm_vm *vm;

	vm = setup_vm(guest_test_system_suspend);
	enable_system_suspend(vm);

	vcpu_power_off(vm, VCPU_ID_TARGET);
	run = vcpu_state(vm, VCPU_ID_SOURCE);

	enter_guest(vm, VCPU_ID_SOURCE);

	TEST_ASSERT(run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
		    "Unhandled exit reason: %u (%s)",
		    run->exit_reason, exit_reason_str(run->exit_reason));
	TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND,
		    "Unhandled system event: %u (expected: %u)",
		    run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND);

	kvm_vm_free(vm);
}

int main(void)
{
	if (!kvm_check_cap(KVM_CAP_ARM_SYSTEM_SUSPEND)) {
		print_skip("KVM_CAP_ARM_SYSTEM_SUSPEND not supported");
		exit(KSFT_SKIP);
	}

	host_test_cpu_on();
	host_test_system_suspend();
	return 0;
}