Hi Drew, On Tue, Oct 5, 2021 at 6:49 AM Andrew Jones <drjones@xxxxxxxxxx> wrote: > > On Thu, Sep 23, 2021 at 07:16:10PM +0000, Oliver Upton wrote: > > Assert that the vCPU exits to userspace with KVM_SYSTEM_EVENT_SUSPEND if > > it correctly executes the SYSTEM_SUSPEND PSCI call. Additionally, assert > > that the guest PSCI call fails if preconditions are not met (more than 1 > > running vCPU). > > > > Signed-off-by: Oliver Upton <oupton@xxxxxxxxxx> > > --- > > .../testing/selftests/kvm/aarch64/psci_test.c | 75 +++++++++++++++++++ > > 1 file changed, 75 insertions(+) > > > > diff --git a/tools/testing/selftests/kvm/aarch64/psci_test.c b/tools/testing/selftests/kvm/aarch64/psci_test.c > > index 90312be335da..5b881ca4d102 100644 > > --- a/tools/testing/selftests/kvm/aarch64/psci_test.c > > +++ b/tools/testing/selftests/kvm/aarch64/psci_test.c > > @@ -45,6 +45,16 @@ 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 void guest_test_cpu_on(uint64_t target_cpu) > > { > > GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID)); > > @@ -69,6 +79,13 @@ static void vcpu_power_off(struct kvm_vm *vm, uint32_t vcpuid) > > vcpu_set_mp_state(vm, vcpuid, &mp_state); > > } > > > > +static void guest_test_system_suspend(void) > > +{ > > + uint64_t r = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID); > > + > > + GUEST_SYNC(r); > > +} > > + > > static struct kvm_vm *setup_vm(void *guest_code) > > { > > struct kvm_vcpu_init init; > > @@ -136,8 +153,66 @@ 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 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); > > + > > + assert_vcpu_reset(vm, VCPU_ID_SOURCE); > > + kvm_vm_free(vm); > > +} > > + > > +static void host_test_system_suspend_fails(void) > > +{ > > + struct kvm_vm *vm; > > + struct ucall uc; > > + > > + vm = setup_vm(guest_test_system_suspend); > > + enable_system_suspend(vm); > > + > > + enter_guest(vm, VCPU_ID_SOURCE); > > + TEST_ASSERT(get_ucall(vm, VCPU_ID_SOURCE, &uc) == UCALL_SYNC, > > + "Unhandled ucall: %lu", uc.cmd); > > + TEST_ASSERT(uc.args[1] == PSCI_RET_DENIED, > > + "Unrecognized PSCI return code: %lu (expected: %u)", > > + uc.args[1], PSCI_RET_DENIED); > > + > > + 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); > > + } > > How about only guarding the new tests with this, so we can still do the > cpu_on test when this feature isn't present? > Great suggestion, thanks! -- Best, Oliver