xref: /linux/tools/testing/selftests/kvm/aarch64/psci_test.c (revision 730cfa45b5f4f170095707b526dc7af99c9f0959)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * psci_test - Tests relating to KVM's PSCI implementation.
4  *
5  * Copyright (c) 2021 Google LLC.
6  *
7  * This test includes:
8  *  - A regression test for a race between KVM servicing the PSCI CPU_ON call
9  *    and userspace reading the targeted vCPU's registers.
10  *  - A test for KVM's handling of PSCI SYSTEM_SUSPEND and the associated
11  *    KVM_SYSTEM_EVENT_SUSPEND UAPI.
12  */
13 #include <linux/psci.h>
14 
15 #include "kvm_util.h"
16 #include "processor.h"
17 #include "test_util.h"
18 
19 #define CPU_ON_ENTRY_ADDR 0xfeedf00dul
20 #define CPU_ON_CONTEXT_ID 0xdeadc0deul
21 
22 static uint64_t psci_cpu_on(uint64_t target_cpu, uint64_t entry_addr,
23 			    uint64_t context_id)
24 {
25 	struct arm_smccc_res res;
26 
27 	smccc_hvc(PSCI_0_2_FN64_CPU_ON, target_cpu, entry_addr, context_id,
28 		  0, 0, 0, 0, &res);
29 
30 	return res.a0;
31 }
32 
33 static uint64_t psci_affinity_info(uint64_t target_affinity,
34 				   uint64_t lowest_affinity_level)
35 {
36 	struct arm_smccc_res res;
37 
38 	smccc_hvc(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity, lowest_affinity_level,
39 		  0, 0, 0, 0, 0, &res);
40 
41 	return res.a0;
42 }
43 
44 static uint64_t psci_system_suspend(uint64_t entry_addr, uint64_t context_id)
45 {
46 	struct arm_smccc_res res;
47 
48 	smccc_hvc(PSCI_1_0_FN64_SYSTEM_SUSPEND, entry_addr, context_id,
49 		  0, 0, 0, 0, 0, &res);
50 
51 	return res.a0;
52 }
53 
54 static uint64_t psci_features(uint32_t func_id)
55 {
56 	struct arm_smccc_res res;
57 
58 	smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES, func_id, 0, 0, 0, 0, 0, 0, &res);
59 
60 	return res.a0;
61 }
62 
63 static void vcpu_power_off(struct kvm_vcpu *vcpu)
64 {
65 	struct kvm_mp_state mp_state = {
66 		.mp_state = KVM_MP_STATE_STOPPED,
67 	};
68 
69 	vcpu_mp_state_set(vcpu, &mp_state);
70 }
71 
72 static struct kvm_vm *setup_vm(void *guest_code, struct kvm_vcpu **source,
73 			       struct kvm_vcpu **target)
74 {
75 	struct kvm_vcpu_init init;
76 	struct kvm_vm *vm;
77 
78 	vm = vm_create(2);
79 
80 	vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init);
81 	init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2);
82 
83 	*source = aarch64_vcpu_add(vm, 0, &init, guest_code);
84 	*target = aarch64_vcpu_add(vm, 1, &init, guest_code);
85 
86 	return vm;
87 }
88 
89 static void enter_guest(struct kvm_vcpu *vcpu)
90 {
91 	struct ucall uc;
92 
93 	vcpu_run(vcpu);
94 	if (get_ucall(vcpu, &uc) == UCALL_ABORT)
95 		REPORT_GUEST_ASSERT(uc);
96 }
97 
98 static void assert_vcpu_reset(struct kvm_vcpu *vcpu)
99 {
100 	uint64_t obs_pc, obs_x0;
101 
102 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &obs_pc);
103 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.regs[0]), &obs_x0);
104 
105 	TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR,
106 		    "unexpected target cpu pc: %lx (expected: %lx)",
107 		    obs_pc, CPU_ON_ENTRY_ADDR);
108 	TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID,
109 		    "unexpected target context id: %lx (expected: %lx)",
110 		    obs_x0, CPU_ON_CONTEXT_ID);
111 }
112 
113 static void guest_test_cpu_on(uint64_t target_cpu)
114 {
115 	uint64_t target_state;
116 
117 	GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID));
118 
119 	do {
120 		target_state = psci_affinity_info(target_cpu, 0);
121 
122 		GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) ||
123 			     (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF));
124 	} while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON);
125 
126 	GUEST_DONE();
127 }
128 
129 static void host_test_cpu_on(void)
130 {
131 	struct kvm_vcpu *source, *target;
132 	uint64_t target_mpidr;
133 	struct kvm_vm *vm;
134 	struct ucall uc;
135 
136 	vm = setup_vm(guest_test_cpu_on, &source, &target);
137 
138 	/*
139 	 * make sure the target is already off when executing the test.
140 	 */
141 	vcpu_power_off(target);
142 
143 	vcpu_get_reg(target, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr);
144 	vcpu_args_set(source, 1, target_mpidr & MPIDR_HWID_BITMASK);
145 	enter_guest(source);
146 
147 	if (get_ucall(source, &uc) != UCALL_DONE)
148 		TEST_FAIL("Unhandled ucall: %lu", uc.cmd);
149 
150 	assert_vcpu_reset(target);
151 	kvm_vm_free(vm);
152 }
153 
154 static void guest_test_system_suspend(void)
155 {
156 	uint64_t ret;
157 
158 	/* assert that SYSTEM_SUSPEND is discoverable */
159 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND));
160 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND));
161 
162 	ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID);
163 	GUEST_SYNC(ret);
164 }
165 
166 static void host_test_system_suspend(void)
167 {
168 	struct kvm_vcpu *source, *target;
169 	struct kvm_run *run;
170 	struct kvm_vm *vm;
171 
172 	vm = setup_vm(guest_test_system_suspend, &source, &target);
173 	vm_enable_cap(vm, KVM_CAP_ARM_SYSTEM_SUSPEND, 0);
174 
175 	vcpu_power_off(target);
176 	run = source->run;
177 
178 	enter_guest(source);
179 
180 	TEST_ASSERT_KVM_EXIT_REASON(source, KVM_EXIT_SYSTEM_EVENT);
181 	TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND,
182 		    "Unhandled system event: %u (expected: %u)",
183 		    run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND);
184 
185 	kvm_vm_free(vm);
186 }
187 
188 int main(void)
189 {
190 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SYSTEM_SUSPEND));
191 
192 	host_test_cpu_on();
193 	host_test_system_suspend();
194 	return 0;
195 }
196