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