xref: /linux/tools/testing/selftests/kvm/aarch64/psci_test.c (revision b26dafc8a9e74254a390e8f21ff028a2573ee4fc)
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 
48*b26dafc8SOliver Upton static uint64_t psci_system_suspend(uint64_t entry_addr, uint64_t context_id)
49*b26dafc8SOliver Upton {
50*b26dafc8SOliver Upton 	struct arm_smccc_res res;
51*b26dafc8SOliver Upton 
52*b26dafc8SOliver Upton 	smccc_hvc(PSCI_1_0_FN64_SYSTEM_SUSPEND, entry_addr, context_id,
53*b26dafc8SOliver Upton 		  0, 0, 0, 0, 0, &res);
54*b26dafc8SOliver Upton 
55*b26dafc8SOliver Upton 	return res.a0;
56*b26dafc8SOliver Upton }
57*b26dafc8SOliver Upton 
58*b26dafc8SOliver Upton static uint64_t psci_features(uint32_t func_id)
59*b26dafc8SOliver Upton {
60*b26dafc8SOliver Upton 	struct arm_smccc_res res;
61*b26dafc8SOliver Upton 
62*b26dafc8SOliver Upton 	smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES, func_id, 0, 0, 0, 0, 0, 0, &res);
63*b26dafc8SOliver Upton 
64*b26dafc8SOliver Upton 	return res.a0;
65*b26dafc8SOliver Upton }
66*b26dafc8SOliver Upton 
6767a36a82SOliver Upton static void vcpu_power_off(struct kvm_vm *vm, uint32_t vcpuid)
686689fb8fSOliver Upton {
6967a36a82SOliver Upton 	struct kvm_mp_state mp_state = {
7067a36a82SOliver Upton 		.mp_state = KVM_MP_STATE_STOPPED,
7167a36a82SOliver Upton 	};
7267a36a82SOliver Upton 
7367a36a82SOliver Upton 	vcpu_set_mp_state(vm, vcpuid, &mp_state);
7467a36a82SOliver Upton }
7567a36a82SOliver Upton 
7667a36a82SOliver Upton static struct kvm_vm *setup_vm(void *guest_code)
7767a36a82SOliver Upton {
7867a36a82SOliver Upton 	struct kvm_vcpu_init init;
7967a36a82SOliver Upton 	struct kvm_vm *vm;
8067a36a82SOliver Upton 
8167a36a82SOliver Upton 	vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
8267a36a82SOliver Upton 	kvm_vm_elf_load(vm, program_invocation_name);
8367a36a82SOliver Upton 	ucall_init(vm, NULL);
8467a36a82SOliver Upton 
8567a36a82SOliver Upton 	vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init);
8667a36a82SOliver Upton 	init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2);
8767a36a82SOliver Upton 
8867a36a82SOliver Upton 	aarch64_vcpu_add_default(vm, VCPU_ID_SOURCE, &init, guest_code);
8967a36a82SOliver Upton 	aarch64_vcpu_add_default(vm, VCPU_ID_TARGET, &init, guest_code);
9067a36a82SOliver Upton 
9167a36a82SOliver Upton 	return vm;
9267a36a82SOliver Upton }
9367a36a82SOliver Upton 
9467a36a82SOliver Upton static void enter_guest(struct kvm_vm *vm, uint32_t vcpuid)
9567a36a82SOliver Upton {
9667a36a82SOliver Upton 	struct ucall uc;
9767a36a82SOliver Upton 
9867a36a82SOliver Upton 	vcpu_run(vm, vcpuid);
9967a36a82SOliver Upton 	if (get_ucall(vm, vcpuid, &uc) == UCALL_ABORT)
10067a36a82SOliver Upton 		TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0], __FILE__,
10167a36a82SOliver Upton 			  uc.args[1]);
10267a36a82SOliver Upton }
10367a36a82SOliver Upton 
10467a36a82SOliver Upton static void assert_vcpu_reset(struct kvm_vm *vm, uint32_t vcpuid)
10567a36a82SOliver Upton {
10667a36a82SOliver Upton 	uint64_t obs_pc, obs_x0;
10767a36a82SOliver Upton 
10867a36a82SOliver Upton 	get_reg(vm, vcpuid, ARM64_CORE_REG(regs.pc), &obs_pc);
10967a36a82SOliver Upton 	get_reg(vm, vcpuid, ARM64_CORE_REG(regs.regs[0]), &obs_x0);
11067a36a82SOliver Upton 
11167a36a82SOliver Upton 	TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR,
11267a36a82SOliver Upton 		    "unexpected target cpu pc: %lx (expected: %lx)",
11367a36a82SOliver Upton 		    obs_pc, CPU_ON_ENTRY_ADDR);
11467a36a82SOliver Upton 	TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID,
11567a36a82SOliver Upton 		    "unexpected target context id: %lx (expected: %lx)",
11667a36a82SOliver Upton 		    obs_x0, CPU_ON_CONTEXT_ID);
11767a36a82SOliver Upton }
11867a36a82SOliver Upton 
11967a36a82SOliver Upton static void guest_test_cpu_on(uint64_t target_cpu)
12067a36a82SOliver Upton {
1216689fb8fSOliver Upton 	uint64_t target_state;
1226689fb8fSOliver Upton 
12367a36a82SOliver Upton 	GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID));
12467a36a82SOliver Upton 
1256689fb8fSOliver Upton 	do {
1266689fb8fSOliver Upton 		target_state = psci_affinity_info(target_cpu, 0);
1276689fb8fSOliver Upton 
1286689fb8fSOliver Upton 		GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) ||
1296689fb8fSOliver Upton 			     (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF));
1306689fb8fSOliver Upton 	} while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON);
1316689fb8fSOliver Upton 
1326689fb8fSOliver Upton 	GUEST_DONE();
1336689fb8fSOliver Upton }
1346689fb8fSOliver Upton 
13567a36a82SOliver Upton static void host_test_cpu_on(void)
136d135399aSOliver Upton {
13767a36a82SOliver Upton 	uint64_t target_mpidr;
1386689fb8fSOliver Upton 	struct kvm_vm *vm;
1396689fb8fSOliver Upton 	struct ucall uc;
1406689fb8fSOliver Upton 
14167a36a82SOliver Upton 	vm = setup_vm(guest_test_cpu_on);
1426689fb8fSOliver Upton 
1436689fb8fSOliver Upton 	/*
1446689fb8fSOliver Upton 	 * make sure the target is already off when executing the test.
1456689fb8fSOliver Upton 	 */
146d135399aSOliver Upton 	vcpu_power_off(vm, VCPU_ID_TARGET);
1476689fb8fSOliver Upton 
1486689fb8fSOliver Upton 	get_reg(vm, VCPU_ID_TARGET, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr);
1496689fb8fSOliver Upton 	vcpu_args_set(vm, VCPU_ID_SOURCE, 1, target_mpidr & MPIDR_HWID_BITMASK);
15067a36a82SOliver Upton 	enter_guest(vm, VCPU_ID_SOURCE);
1516689fb8fSOliver Upton 
15267a36a82SOliver Upton 	if (get_ucall(vm, VCPU_ID_SOURCE, &uc) != UCALL_DONE)
1536689fb8fSOliver Upton 		TEST_FAIL("Unhandled ucall: %lu", uc.cmd);
15467a36a82SOliver Upton 
15567a36a82SOliver Upton 	assert_vcpu_reset(vm, VCPU_ID_TARGET);
15667a36a82SOliver Upton 	kvm_vm_free(vm);
1576689fb8fSOliver Upton }
1586689fb8fSOliver Upton 
159*b26dafc8SOliver Upton static void enable_system_suspend(struct kvm_vm *vm)
160*b26dafc8SOliver Upton {
161*b26dafc8SOliver Upton 	struct kvm_enable_cap cap = {
162*b26dafc8SOliver Upton 		.cap = KVM_CAP_ARM_SYSTEM_SUSPEND,
163*b26dafc8SOliver Upton 	};
164*b26dafc8SOliver Upton 
165*b26dafc8SOliver Upton 	vm_enable_cap(vm, &cap);
166*b26dafc8SOliver Upton }
167*b26dafc8SOliver Upton 
168*b26dafc8SOliver Upton static void guest_test_system_suspend(void)
169*b26dafc8SOliver Upton {
170*b26dafc8SOliver Upton 	uint64_t ret;
171*b26dafc8SOliver Upton 
172*b26dafc8SOliver Upton 	/* assert that SYSTEM_SUSPEND is discoverable */
173*b26dafc8SOliver Upton 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND));
174*b26dafc8SOliver Upton 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND));
175*b26dafc8SOliver Upton 
176*b26dafc8SOliver Upton 	ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID);
177*b26dafc8SOliver Upton 	GUEST_SYNC(ret);
178*b26dafc8SOliver Upton }
179*b26dafc8SOliver Upton 
180*b26dafc8SOliver Upton static void host_test_system_suspend(void)
181*b26dafc8SOliver Upton {
182*b26dafc8SOliver Upton 	struct kvm_run *run;
183*b26dafc8SOliver Upton 	struct kvm_vm *vm;
184*b26dafc8SOliver Upton 
185*b26dafc8SOliver Upton 	vm = setup_vm(guest_test_system_suspend);
186*b26dafc8SOliver Upton 	enable_system_suspend(vm);
187*b26dafc8SOliver Upton 
188*b26dafc8SOliver Upton 	vcpu_power_off(vm, VCPU_ID_TARGET);
189*b26dafc8SOliver Upton 	run = vcpu_state(vm, VCPU_ID_SOURCE);
190*b26dafc8SOliver Upton 
191*b26dafc8SOliver Upton 	enter_guest(vm, VCPU_ID_SOURCE);
192*b26dafc8SOliver Upton 
193*b26dafc8SOliver Upton 	TEST_ASSERT(run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
194*b26dafc8SOliver Upton 		    "Unhandled exit reason: %u (%s)",
195*b26dafc8SOliver Upton 		    run->exit_reason, exit_reason_str(run->exit_reason));
196*b26dafc8SOliver Upton 	TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND,
197*b26dafc8SOliver Upton 		    "Unhandled system event: %u (expected: %u)",
198*b26dafc8SOliver Upton 		    run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND);
199*b26dafc8SOliver Upton 
200*b26dafc8SOliver Upton 	kvm_vm_free(vm);
201*b26dafc8SOliver Upton }
202*b26dafc8SOliver Upton 
20367a36a82SOliver Upton int main(void)
20467a36a82SOliver Upton {
205*b26dafc8SOliver Upton 	if (!kvm_check_cap(KVM_CAP_ARM_SYSTEM_SUSPEND)) {
206*b26dafc8SOliver Upton 		print_skip("KVM_CAP_ARM_SYSTEM_SUSPEND not supported");
207*b26dafc8SOliver Upton 		exit(KSFT_SKIP);
208*b26dafc8SOliver Upton 	}
209*b26dafc8SOliver Upton 
21067a36a82SOliver Upton 	host_test_cpu_on();
211*b26dafc8SOliver Upton 	host_test_system_suspend();
2126689fb8fSOliver Upton 	return 0;
2136689fb8fSOliver Upton }
214