16689fb8fSOliver Upton // SPDX-License-Identifier: GPL-2.0-only 26689fb8fSOliver Upton /* 36689fb8fSOliver Upton * psci_cpu_on_test - Test that the observable state of a vCPU targeted by the 46689fb8fSOliver Upton * CPU_ON PSCI call matches what the caller requested. 56689fb8fSOliver Upton * 66689fb8fSOliver Upton * Copyright (c) 2021 Google LLC. 76689fb8fSOliver Upton * 86689fb8fSOliver Upton * This is a regression test for a race between KVM servicing the PSCI call and 96689fb8fSOliver Upton * userspace reading the vCPUs registers. 106689fb8fSOliver Upton */ 116689fb8fSOliver Upton 126689fb8fSOliver Upton #define _GNU_SOURCE 136689fb8fSOliver Upton 146689fb8fSOliver Upton #include <linux/psci.h> 156689fb8fSOliver Upton 166689fb8fSOliver Upton #include "kvm_util.h" 176689fb8fSOliver Upton #include "processor.h" 186689fb8fSOliver Upton #include "test_util.h" 196689fb8fSOliver Upton 206689fb8fSOliver Upton #define CPU_ON_ENTRY_ADDR 0xfeedf00dul 216689fb8fSOliver Upton #define CPU_ON_CONTEXT_ID 0xdeadc0deul 226689fb8fSOliver Upton 236689fb8fSOliver Upton static uint64_t psci_cpu_on(uint64_t target_cpu, uint64_t entry_addr, 246689fb8fSOliver Upton uint64_t context_id) 256689fb8fSOliver Upton { 26694e3dccSOliver Upton struct arm_smccc_res res; 276689fb8fSOliver Upton 28694e3dccSOliver Upton smccc_hvc(PSCI_0_2_FN64_CPU_ON, target_cpu, entry_addr, context_id, 29694e3dccSOliver Upton 0, 0, 0, 0, &res); 306689fb8fSOliver Upton 31694e3dccSOliver Upton return res.a0; 326689fb8fSOliver Upton } 336689fb8fSOliver Upton 346689fb8fSOliver Upton static uint64_t psci_affinity_info(uint64_t target_affinity, 356689fb8fSOliver Upton uint64_t lowest_affinity_level) 366689fb8fSOliver Upton { 37694e3dccSOliver Upton struct arm_smccc_res res; 386689fb8fSOliver Upton 39694e3dccSOliver Upton smccc_hvc(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity, lowest_affinity_level, 40694e3dccSOliver Upton 0, 0, 0, 0, 0, &res); 416689fb8fSOliver Upton 42694e3dccSOliver Upton return res.a0; 436689fb8fSOliver Upton } 446689fb8fSOliver Upton 45b26dafc8SOliver Upton static uint64_t psci_system_suspend(uint64_t entry_addr, uint64_t context_id) 46b26dafc8SOliver Upton { 47b26dafc8SOliver Upton struct arm_smccc_res res; 48b26dafc8SOliver Upton 49b26dafc8SOliver Upton smccc_hvc(PSCI_1_0_FN64_SYSTEM_SUSPEND, entry_addr, context_id, 50b26dafc8SOliver Upton 0, 0, 0, 0, 0, &res); 51b26dafc8SOliver Upton 52b26dafc8SOliver Upton return res.a0; 53b26dafc8SOliver Upton } 54b26dafc8SOliver Upton 55b26dafc8SOliver Upton static uint64_t psci_features(uint32_t func_id) 56b26dafc8SOliver Upton { 57b26dafc8SOliver Upton struct arm_smccc_res res; 58b26dafc8SOliver Upton 59b26dafc8SOliver Upton smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES, func_id, 0, 0, 0, 0, 0, 0, &res); 60b26dafc8SOliver Upton 61b26dafc8SOliver Upton return res.a0; 62b26dafc8SOliver Upton } 63b26dafc8SOliver Upton 64b093da65SSean Christopherson static void vcpu_power_off(struct kvm_vcpu *vcpu) 656689fb8fSOliver Upton { 6667a36a82SOliver Upton struct kvm_mp_state mp_state = { 6767a36a82SOliver Upton .mp_state = KVM_MP_STATE_STOPPED, 6867a36a82SOliver Upton }; 6967a36a82SOliver Upton 70768e9a61SSean Christopherson vcpu_mp_state_set(vcpu, &mp_state); 7167a36a82SOliver Upton } 7267a36a82SOliver Upton 73b093da65SSean Christopherson static struct kvm_vm *setup_vm(void *guest_code, struct kvm_vcpu **source, 74b093da65SSean Christopherson struct kvm_vcpu **target) 7567a36a82SOliver Upton { 7667a36a82SOliver Upton struct kvm_vcpu_init init; 7767a36a82SOliver Upton struct kvm_vm *vm; 7867a36a82SOliver Upton 796e1d13bfSSean Christopherson vm = vm_create(2); 8067a36a82SOliver Upton ucall_init(vm, NULL); 8167a36a82SOliver Upton 8267a36a82SOliver Upton vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init); 8367a36a82SOliver Upton init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2); 8467a36a82SOliver Upton 85b093da65SSean Christopherson *source = aarch64_vcpu_add(vm, 0, &init, guest_code); 86b093da65SSean Christopherson *target = aarch64_vcpu_add(vm, 1, &init, guest_code); 8767a36a82SOliver Upton 8867a36a82SOliver Upton return vm; 8967a36a82SOliver Upton } 9067a36a82SOliver Upton 91b093da65SSean Christopherson static void enter_guest(struct kvm_vcpu *vcpu) 9267a36a82SOliver Upton { 9367a36a82SOliver Upton struct ucall uc; 9467a36a82SOliver Upton 95768e9a61SSean Christopherson vcpu_run(vcpu); 96768e9a61SSean Christopherson if (get_ucall(vcpu, &uc) == UCALL_ABORT) 97*594a1c27SColton Lewis REPORT_GUEST_ASSERT(uc); 9867a36a82SOliver Upton } 9967a36a82SOliver Upton 100b093da65SSean Christopherson static void assert_vcpu_reset(struct kvm_vcpu *vcpu) 10167a36a82SOliver Upton { 10267a36a82SOliver Upton uint64_t obs_pc, obs_x0; 10367a36a82SOliver Upton 104768e9a61SSean Christopherson vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &obs_pc); 105768e9a61SSean Christopherson vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.regs[0]), &obs_x0); 10667a36a82SOliver Upton 10767a36a82SOliver Upton TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR, 10867a36a82SOliver Upton "unexpected target cpu pc: %lx (expected: %lx)", 10967a36a82SOliver Upton obs_pc, CPU_ON_ENTRY_ADDR); 11067a36a82SOliver Upton TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID, 11167a36a82SOliver Upton "unexpected target context id: %lx (expected: %lx)", 11267a36a82SOliver Upton obs_x0, CPU_ON_CONTEXT_ID); 11367a36a82SOliver Upton } 11467a36a82SOliver Upton 11567a36a82SOliver Upton static void guest_test_cpu_on(uint64_t target_cpu) 11667a36a82SOliver Upton { 1176689fb8fSOliver Upton uint64_t target_state; 1186689fb8fSOliver Upton 11967a36a82SOliver Upton GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID)); 12067a36a82SOliver Upton 1216689fb8fSOliver Upton do { 1226689fb8fSOliver Upton target_state = psci_affinity_info(target_cpu, 0); 1236689fb8fSOliver Upton 1246689fb8fSOliver Upton GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) || 1256689fb8fSOliver Upton (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF)); 1266689fb8fSOliver Upton } while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON); 1276689fb8fSOliver Upton 1286689fb8fSOliver Upton GUEST_DONE(); 1296689fb8fSOliver Upton } 1306689fb8fSOliver Upton 13167a36a82SOliver Upton static void host_test_cpu_on(void) 132d135399aSOliver Upton { 133b093da65SSean Christopherson struct kvm_vcpu *source, *target; 13467a36a82SOliver Upton uint64_t target_mpidr; 1356689fb8fSOliver Upton struct kvm_vm *vm; 1366689fb8fSOliver Upton struct ucall uc; 1376689fb8fSOliver Upton 138b093da65SSean Christopherson vm = setup_vm(guest_test_cpu_on, &source, &target); 1396689fb8fSOliver Upton 1406689fb8fSOliver Upton /* 1416689fb8fSOliver Upton * make sure the target is already off when executing the test. 1426689fb8fSOliver Upton */ 143b093da65SSean Christopherson vcpu_power_off(target); 1446689fb8fSOliver Upton 145768e9a61SSean Christopherson vcpu_get_reg(target, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr); 146768e9a61SSean Christopherson vcpu_args_set(source, 1, target_mpidr & MPIDR_HWID_BITMASK); 147b093da65SSean Christopherson enter_guest(source); 1486689fb8fSOliver Upton 149768e9a61SSean Christopherson if (get_ucall(source, &uc) != UCALL_DONE) 1506689fb8fSOliver Upton TEST_FAIL("Unhandled ucall: %lu", uc.cmd); 15167a36a82SOliver Upton 152b093da65SSean Christopherson assert_vcpu_reset(target); 15367a36a82SOliver Upton kvm_vm_free(vm); 1546689fb8fSOliver Upton } 1556689fb8fSOliver Upton 156b26dafc8SOliver Upton static void guest_test_system_suspend(void) 157b26dafc8SOliver Upton { 158b26dafc8SOliver Upton uint64_t ret; 159b26dafc8SOliver Upton 160b26dafc8SOliver Upton /* assert that SYSTEM_SUSPEND is discoverable */ 161b26dafc8SOliver Upton GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND)); 162b26dafc8SOliver Upton GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND)); 163b26dafc8SOliver Upton 164b26dafc8SOliver Upton ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID); 165b26dafc8SOliver Upton GUEST_SYNC(ret); 166b26dafc8SOliver Upton } 167b26dafc8SOliver Upton 168b26dafc8SOliver Upton static void host_test_system_suspend(void) 169b26dafc8SOliver Upton { 170b093da65SSean Christopherson struct kvm_vcpu *source, *target; 171b26dafc8SOliver Upton struct kvm_run *run; 172b26dafc8SOliver Upton struct kvm_vm *vm; 173b26dafc8SOliver Upton 174b093da65SSean Christopherson vm = setup_vm(guest_test_system_suspend, &source, &target); 175a12c86c4SSean Christopherson vm_enable_cap(vm, KVM_CAP_ARM_SYSTEM_SUSPEND, 0); 176b26dafc8SOliver Upton 177b093da65SSean Christopherson vcpu_power_off(target); 178b093da65SSean Christopherson run = source->run; 179b26dafc8SOliver Upton 180b093da65SSean Christopherson enter_guest(source); 181b26dafc8SOliver Upton 182b26dafc8SOliver Upton TEST_ASSERT(run->exit_reason == KVM_EXIT_SYSTEM_EVENT, 183b26dafc8SOliver Upton "Unhandled exit reason: %u (%s)", 184b26dafc8SOliver Upton run->exit_reason, exit_reason_str(run->exit_reason)); 185b26dafc8SOliver Upton TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND, 186b26dafc8SOliver Upton "Unhandled system event: %u (expected: %u)", 187b26dafc8SOliver Upton run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND); 188b26dafc8SOliver Upton 189b26dafc8SOliver Upton kvm_vm_free(vm); 190b26dafc8SOliver Upton } 191b26dafc8SOliver Upton 19267a36a82SOliver Upton int main(void) 19367a36a82SOliver Upton { 1949393cb13SSean Christopherson TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SYSTEM_SUSPEND)); 195b26dafc8SOliver Upton 19667a36a82SOliver Upton host_test_cpu_on(); 197b26dafc8SOliver Upton host_test_system_suspend(); 1986689fb8fSOliver Upton return 0; 1996689fb8fSOliver Upton } 200