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 VCPU_ID_SOURCE 0 216689fb8fSOliver Upton #define VCPU_ID_TARGET 1 226689fb8fSOliver Upton 236689fb8fSOliver Upton #define CPU_ON_ENTRY_ADDR 0xfeedf00dul 246689fb8fSOliver Upton #define CPU_ON_CONTEXT_ID 0xdeadc0deul 256689fb8fSOliver Upton 266689fb8fSOliver Upton static uint64_t psci_cpu_on(uint64_t target_cpu, uint64_t entry_addr, 276689fb8fSOliver Upton uint64_t context_id) 286689fb8fSOliver Upton { 29694e3dccSOliver Upton struct arm_smccc_res res; 306689fb8fSOliver Upton 31694e3dccSOliver Upton smccc_hvc(PSCI_0_2_FN64_CPU_ON, target_cpu, entry_addr, context_id, 32694e3dccSOliver Upton 0, 0, 0, 0, &res); 336689fb8fSOliver Upton 34694e3dccSOliver Upton return res.a0; 356689fb8fSOliver Upton } 366689fb8fSOliver Upton 376689fb8fSOliver Upton static uint64_t psci_affinity_info(uint64_t target_affinity, 386689fb8fSOliver Upton uint64_t lowest_affinity_level) 396689fb8fSOliver Upton { 40694e3dccSOliver Upton struct arm_smccc_res res; 416689fb8fSOliver Upton 42694e3dccSOliver Upton smccc_hvc(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity, lowest_affinity_level, 43694e3dccSOliver Upton 0, 0, 0, 0, 0, &res); 446689fb8fSOliver Upton 45694e3dccSOliver Upton return res.a0; 466689fb8fSOliver Upton } 476689fb8fSOliver Upton 486689fb8fSOliver Upton static void guest_main(uint64_t target_cpu) 496689fb8fSOliver Upton { 506689fb8fSOliver Upton GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID)); 516689fb8fSOliver Upton uint64_t target_state; 526689fb8fSOliver Upton 536689fb8fSOliver Upton do { 546689fb8fSOliver Upton target_state = psci_affinity_info(target_cpu, 0); 556689fb8fSOliver Upton 566689fb8fSOliver Upton GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) || 576689fb8fSOliver Upton (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF)); 586689fb8fSOliver Upton } while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON); 596689fb8fSOliver Upton 606689fb8fSOliver Upton GUEST_DONE(); 616689fb8fSOliver Upton } 626689fb8fSOliver Upton 63*d135399aSOliver Upton static void vcpu_power_off(struct kvm_vm *vm, uint32_t vcpuid) 64*d135399aSOliver Upton { 65*d135399aSOliver Upton struct kvm_mp_state mp_state = { 66*d135399aSOliver Upton .mp_state = KVM_MP_STATE_STOPPED, 67*d135399aSOliver Upton }; 68*d135399aSOliver Upton 69*d135399aSOliver Upton vcpu_set_mp_state(vm, vcpuid, &mp_state); 70*d135399aSOliver Upton } 71*d135399aSOliver Upton 726689fb8fSOliver Upton int main(void) 736689fb8fSOliver Upton { 746689fb8fSOliver Upton uint64_t target_mpidr, obs_pc, obs_x0; 756689fb8fSOliver Upton struct kvm_vcpu_init init; 766689fb8fSOliver Upton struct kvm_vm *vm; 776689fb8fSOliver Upton struct ucall uc; 786689fb8fSOliver Upton 796689fb8fSOliver Upton vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR); 806689fb8fSOliver Upton kvm_vm_elf_load(vm, program_invocation_name); 816689fb8fSOliver Upton ucall_init(vm, NULL); 826689fb8fSOliver Upton 836689fb8fSOliver Upton vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init); 846689fb8fSOliver Upton init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2); 856689fb8fSOliver Upton 866689fb8fSOliver Upton aarch64_vcpu_add_default(vm, VCPU_ID_SOURCE, &init, guest_main); 87*d135399aSOliver Upton aarch64_vcpu_add_default(vm, VCPU_ID_TARGET, &init, guest_main); 886689fb8fSOliver Upton 896689fb8fSOliver Upton /* 906689fb8fSOliver Upton * make sure the target is already off when executing the test. 916689fb8fSOliver Upton */ 92*d135399aSOliver Upton vcpu_power_off(vm, VCPU_ID_TARGET); 936689fb8fSOliver Upton 946689fb8fSOliver Upton get_reg(vm, VCPU_ID_TARGET, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr); 956689fb8fSOliver Upton vcpu_args_set(vm, VCPU_ID_SOURCE, 1, target_mpidr & MPIDR_HWID_BITMASK); 966689fb8fSOliver Upton vcpu_run(vm, VCPU_ID_SOURCE); 976689fb8fSOliver Upton 986689fb8fSOliver Upton switch (get_ucall(vm, VCPU_ID_SOURCE, &uc)) { 996689fb8fSOliver Upton case UCALL_DONE: 1006689fb8fSOliver Upton break; 1016689fb8fSOliver Upton case UCALL_ABORT: 1026689fb8fSOliver Upton TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0], __FILE__, 1036689fb8fSOliver Upton uc.args[1]); 1046689fb8fSOliver Upton break; 1056689fb8fSOliver Upton default: 1066689fb8fSOliver Upton TEST_FAIL("Unhandled ucall: %lu", uc.cmd); 1076689fb8fSOliver Upton } 1086689fb8fSOliver Upton 1096689fb8fSOliver Upton get_reg(vm, VCPU_ID_TARGET, ARM64_CORE_REG(regs.pc), &obs_pc); 1106689fb8fSOliver Upton get_reg(vm, VCPU_ID_TARGET, ARM64_CORE_REG(regs.regs[0]), &obs_x0); 1116689fb8fSOliver Upton 1126689fb8fSOliver Upton TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR, 1136689fb8fSOliver Upton "unexpected target cpu pc: %lx (expected: %lx)", 1146689fb8fSOliver Upton obs_pc, CPU_ON_ENTRY_ADDR); 1156689fb8fSOliver Upton TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID, 1166689fb8fSOliver Upton "unexpected target context id: %lx (expected: %lx)", 1176689fb8fSOliver Upton obs_x0, CPU_ON_CONTEXT_ID); 1186689fb8fSOliver Upton 1196689fb8fSOliver Upton kvm_vm_free(vm); 1206689fb8fSOliver Upton return 0; 1216689fb8fSOliver Upton } 122