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 
70*768e9a61SSean 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 
793f44e7fdSSean Christopherson 	vm = vm_create(DEFAULT_GUEST_PHY_PAGES);
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 
95*768e9a61SSean Christopherson 	vcpu_run(vcpu);
96*768e9a61SSean Christopherson 	if (get_ucall(vcpu, &uc) == UCALL_ABORT)
9767a36a82SOliver Upton 		TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0], __FILE__,
9867a36a82SOliver Upton 			  uc.args[1]);
9967a36a82SOliver Upton }
10067a36a82SOliver Upton 
101b093da65SSean Christopherson static void assert_vcpu_reset(struct kvm_vcpu *vcpu)
10267a36a82SOliver Upton {
10367a36a82SOliver Upton 	uint64_t obs_pc, obs_x0;
10467a36a82SOliver Upton 
105*768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &obs_pc);
106*768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.regs[0]), &obs_x0);
10767a36a82SOliver Upton 
10867a36a82SOliver Upton 	TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR,
10967a36a82SOliver Upton 		    "unexpected target cpu pc: %lx (expected: %lx)",
11067a36a82SOliver Upton 		    obs_pc, CPU_ON_ENTRY_ADDR);
11167a36a82SOliver Upton 	TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID,
11267a36a82SOliver Upton 		    "unexpected target context id: %lx (expected: %lx)",
11367a36a82SOliver Upton 		    obs_x0, CPU_ON_CONTEXT_ID);
11467a36a82SOliver Upton }
11567a36a82SOliver Upton 
11667a36a82SOliver Upton static void guest_test_cpu_on(uint64_t target_cpu)
11767a36a82SOliver Upton {
1186689fb8fSOliver Upton 	uint64_t target_state;
1196689fb8fSOliver Upton 
12067a36a82SOliver Upton 	GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID));
12167a36a82SOliver Upton 
1226689fb8fSOliver Upton 	do {
1236689fb8fSOliver Upton 		target_state = psci_affinity_info(target_cpu, 0);
1246689fb8fSOliver Upton 
1256689fb8fSOliver Upton 		GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) ||
1266689fb8fSOliver Upton 			     (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF));
1276689fb8fSOliver Upton 	} while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON);
1286689fb8fSOliver Upton 
1296689fb8fSOliver Upton 	GUEST_DONE();
1306689fb8fSOliver Upton }
1316689fb8fSOliver Upton 
13267a36a82SOliver Upton static void host_test_cpu_on(void)
133d135399aSOliver Upton {
134b093da65SSean Christopherson 	struct kvm_vcpu *source, *target;
13567a36a82SOliver Upton 	uint64_t target_mpidr;
1366689fb8fSOliver Upton 	struct kvm_vm *vm;
1376689fb8fSOliver Upton 	struct ucall uc;
1386689fb8fSOliver Upton 
139b093da65SSean Christopherson 	vm = setup_vm(guest_test_cpu_on, &source, &target);
1406689fb8fSOliver Upton 
1416689fb8fSOliver Upton 	/*
1426689fb8fSOliver Upton 	 * make sure the target is already off when executing the test.
1436689fb8fSOliver Upton 	 */
144b093da65SSean Christopherson 	vcpu_power_off(target);
1456689fb8fSOliver Upton 
146*768e9a61SSean Christopherson 	vcpu_get_reg(target, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr);
147*768e9a61SSean Christopherson 	vcpu_args_set(source, 1, target_mpidr & MPIDR_HWID_BITMASK);
148b093da65SSean Christopherson 	enter_guest(source);
1496689fb8fSOliver Upton 
150*768e9a61SSean Christopherson 	if (get_ucall(source, &uc) != UCALL_DONE)
1516689fb8fSOliver Upton 		TEST_FAIL("Unhandled ucall: %lu", uc.cmd);
15267a36a82SOliver Upton 
153b093da65SSean Christopherson 	assert_vcpu_reset(target);
15467a36a82SOliver Upton 	kvm_vm_free(vm);
1556689fb8fSOliver Upton }
1566689fb8fSOliver Upton 
157b26dafc8SOliver Upton static void guest_test_system_suspend(void)
158b26dafc8SOliver Upton {
159b26dafc8SOliver Upton 	uint64_t ret;
160b26dafc8SOliver Upton 
161b26dafc8SOliver Upton 	/* assert that SYSTEM_SUSPEND is discoverable */
162b26dafc8SOliver Upton 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND));
163b26dafc8SOliver Upton 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND));
164b26dafc8SOliver Upton 
165b26dafc8SOliver Upton 	ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID);
166b26dafc8SOliver Upton 	GUEST_SYNC(ret);
167b26dafc8SOliver Upton }
168b26dafc8SOliver Upton 
169b26dafc8SOliver Upton static void host_test_system_suspend(void)
170b26dafc8SOliver Upton {
171b093da65SSean Christopherson 	struct kvm_vcpu *source, *target;
172b26dafc8SOliver Upton 	struct kvm_run *run;
173b26dafc8SOliver Upton 	struct kvm_vm *vm;
174b26dafc8SOliver Upton 
175b093da65SSean Christopherson 	vm = setup_vm(guest_test_system_suspend, &source, &target);
176a12c86c4SSean Christopherson 	vm_enable_cap(vm, KVM_CAP_ARM_SYSTEM_SUSPEND, 0);
177b26dafc8SOliver Upton 
178b093da65SSean Christopherson 	vcpu_power_off(target);
179b093da65SSean Christopherson 	run = source->run;
180b26dafc8SOliver Upton 
181b093da65SSean Christopherson 	enter_guest(source);
182b26dafc8SOliver Upton 
183b26dafc8SOliver Upton 	TEST_ASSERT(run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
184b26dafc8SOliver Upton 		    "Unhandled exit reason: %u (%s)",
185b26dafc8SOliver Upton 		    run->exit_reason, exit_reason_str(run->exit_reason));
186b26dafc8SOliver Upton 	TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND,
187b26dafc8SOliver Upton 		    "Unhandled system event: %u (expected: %u)",
188b26dafc8SOliver Upton 		    run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND);
189b26dafc8SOliver Upton 
190b26dafc8SOliver Upton 	kvm_vm_free(vm);
191b26dafc8SOliver Upton }
192b26dafc8SOliver Upton 
19367a36a82SOliver Upton int main(void)
19467a36a82SOliver Upton {
195b26dafc8SOliver Upton 	if (!kvm_check_cap(KVM_CAP_ARM_SYSTEM_SUSPEND)) {
196b26dafc8SOliver Upton 		print_skip("KVM_CAP_ARM_SYSTEM_SUSPEND not supported");
197b26dafc8SOliver Upton 		exit(KSFT_SKIP);
198b26dafc8SOliver Upton 	}
199b26dafc8SOliver Upton 
20067a36a82SOliver Upton 	host_test_cpu_on();
201b26dafc8SOliver Upton 	host_test_system_suspend();
2026689fb8fSOliver Upton 	return 0;
2036689fb8fSOliver Upton }
204