1 // SPDX-License-Identifier: GPL-2.0-only
3 * psci_test - Tests relating to KVM's PSCI implementation.
5 * Copyright (c) 2021 Google LLC.
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.
14 #include <linux/kernel.h>
15 #include <linux/psci.h>
16 #include <asm/cputype.h>
19 #include "processor.h"
20 #include "test_util.h"
22 #define CPU_ON_ENTRY_ADDR 0xfeedf00dul
23 #define CPU_ON_CONTEXT_ID 0xdeadc0deul
25 static uint64_t psci_cpu_on(uint64_t target_cpu
, uint64_t entry_addr
,
28 struct arm_smccc_res res
;
30 smccc_hvc(PSCI_0_2_FN64_CPU_ON
, target_cpu
, entry_addr
, context_id
,
36 static uint64_t psci_affinity_info(uint64_t target_affinity
,
37 uint64_t lowest_affinity_level
)
39 struct arm_smccc_res res
;
41 smccc_hvc(PSCI_0_2_FN64_AFFINITY_INFO
, target_affinity
, lowest_affinity_level
,
47 static uint64_t psci_system_suspend(uint64_t entry_addr
, uint64_t context_id
)
49 struct arm_smccc_res res
;
51 smccc_hvc(PSCI_1_0_FN64_SYSTEM_SUSPEND
, entry_addr
, context_id
,
57 static uint64_t psci_system_off2(uint64_t type
, uint64_t cookie
)
59 struct arm_smccc_res res
;
61 smccc_hvc(PSCI_1_3_FN64_SYSTEM_OFF2
, type
, cookie
, 0, 0, 0, 0, 0, &res
);
66 static uint64_t psci_features(uint32_t func_id
)
68 struct arm_smccc_res res
;
70 smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES
, func_id
, 0, 0, 0, 0, 0, 0, &res
);
75 static void vcpu_power_off(struct kvm_vcpu
*vcpu
)
77 struct kvm_mp_state mp_state
= {
78 .mp_state
= KVM_MP_STATE_STOPPED
,
81 vcpu_mp_state_set(vcpu
, &mp_state
);
84 static struct kvm_vm
*setup_vm(void *guest_code
, struct kvm_vcpu
**source
,
85 struct kvm_vcpu
**target
)
87 struct kvm_vcpu_init init
;
92 vm_ioctl(vm
, KVM_ARM_PREFERRED_TARGET
, &init
);
93 init
.features
[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2
);
95 *source
= aarch64_vcpu_add(vm
, 0, &init
, guest_code
);
96 *target
= aarch64_vcpu_add(vm
, 1, &init
, guest_code
);
101 static void enter_guest(struct kvm_vcpu
*vcpu
)
106 if (get_ucall(vcpu
, &uc
) == UCALL_ABORT
)
107 REPORT_GUEST_ASSERT(uc
);
110 static void assert_vcpu_reset(struct kvm_vcpu
*vcpu
)
112 uint64_t obs_pc
, obs_x0
;
114 vcpu_get_reg(vcpu
, ARM64_CORE_REG(regs
.pc
), &obs_pc
);
115 vcpu_get_reg(vcpu
, ARM64_CORE_REG(regs
.regs
[0]), &obs_x0
);
117 TEST_ASSERT(obs_pc
== CPU_ON_ENTRY_ADDR
,
118 "unexpected target cpu pc: %lx (expected: %lx)",
119 obs_pc
, CPU_ON_ENTRY_ADDR
);
120 TEST_ASSERT(obs_x0
== CPU_ON_CONTEXT_ID
,
121 "unexpected target context id: %lx (expected: %lx)",
122 obs_x0
, CPU_ON_CONTEXT_ID
);
125 static void guest_test_cpu_on(uint64_t target_cpu
)
127 uint64_t target_state
;
129 GUEST_ASSERT(!psci_cpu_on(target_cpu
, CPU_ON_ENTRY_ADDR
, CPU_ON_CONTEXT_ID
));
132 target_state
= psci_affinity_info(target_cpu
, 0);
134 GUEST_ASSERT((target_state
== PSCI_0_2_AFFINITY_LEVEL_ON
) ||
135 (target_state
== PSCI_0_2_AFFINITY_LEVEL_OFF
));
136 } while (target_state
!= PSCI_0_2_AFFINITY_LEVEL_ON
);
141 static void host_test_cpu_on(void)
143 struct kvm_vcpu
*source
, *target
;
144 uint64_t target_mpidr
;
148 vm
= setup_vm(guest_test_cpu_on
, &source
, &target
);
151 * make sure the target is already off when executing the test.
153 vcpu_power_off(target
);
155 vcpu_get_reg(target
, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1
), &target_mpidr
);
156 vcpu_args_set(source
, 1, target_mpidr
& MPIDR_HWID_BITMASK
);
159 if (get_ucall(source
, &uc
) != UCALL_DONE
)
160 TEST_FAIL("Unhandled ucall: %lu", uc
.cmd
);
162 assert_vcpu_reset(target
);
166 static void guest_test_system_suspend(void)
170 /* assert that SYSTEM_SUSPEND is discoverable */
171 GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND
));
172 GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND
));
174 ret
= psci_system_suspend(CPU_ON_ENTRY_ADDR
, CPU_ON_CONTEXT_ID
);
178 static void host_test_system_suspend(void)
180 struct kvm_vcpu
*source
, *target
;
184 vm
= setup_vm(guest_test_system_suspend
, &source
, &target
);
185 vm_enable_cap(vm
, KVM_CAP_ARM_SYSTEM_SUSPEND
, 0);
187 vcpu_power_off(target
);
192 TEST_ASSERT_KVM_EXIT_REASON(source
, KVM_EXIT_SYSTEM_EVENT
);
193 TEST_ASSERT(run
->system_event
.type
== KVM_SYSTEM_EVENT_SUSPEND
,
194 "Unhandled system event: %u (expected: %u)",
195 run
->system_event
.type
, KVM_SYSTEM_EVENT_SUSPEND
);
200 static void guest_test_system_off2(void)
204 /* assert that SYSTEM_OFF2 is discoverable */
205 GUEST_ASSERT(psci_features(PSCI_1_3_FN_SYSTEM_OFF2
) &
206 PSCI_1_3_OFF_TYPE_HIBERNATE_OFF
);
207 GUEST_ASSERT(psci_features(PSCI_1_3_FN64_SYSTEM_OFF2
) &
208 PSCI_1_3_OFF_TYPE_HIBERNATE_OFF
);
210 /* With non-zero 'cookie' field, it should fail */
211 ret
= psci_system_off2(PSCI_1_3_OFF_TYPE_HIBERNATE_OFF
, 1);
212 GUEST_ASSERT(ret
== PSCI_RET_INVALID_PARAMS
);
215 * This would normally never return, so KVM sets the return value
216 * to PSCI_RET_INTERNAL_FAILURE. The test case *does* return, so
217 * that it can test both values for HIBERNATE_OFF.
219 ret
= psci_system_off2(PSCI_1_3_OFF_TYPE_HIBERNATE_OFF
, 0);
220 GUEST_ASSERT(ret
== PSCI_RET_INTERNAL_FAILURE
);
223 * Revision F.b of the PSCI v1.3 specification documents zero as an
224 * alias for HIBERNATE_OFF, since that's the value used in earlier
225 * revisions of the spec and some implementations in the field.
227 ret
= psci_system_off2(0, 1);
228 GUEST_ASSERT(ret
== PSCI_RET_INVALID_PARAMS
);
230 ret
= psci_system_off2(0, 0);
231 GUEST_ASSERT(ret
== PSCI_RET_INTERNAL_FAILURE
);
236 static void host_test_system_off2(void)
238 struct kvm_vcpu
*source
, *target
;
239 struct kvm_mp_state mps
;
240 uint64_t psci_version
= 0;
241 int nr_shutdowns
= 0;
245 setup_vm(guest_test_system_off2
, &source
, &target
);
247 vcpu_get_reg(target
, KVM_REG_ARM_PSCI_VERSION
, &psci_version
);
249 TEST_ASSERT(psci_version
>= PSCI_VERSION(1, 3),
250 "Unexpected PSCI version %lu.%lu",
251 PSCI_VERSION_MAJOR(psci_version
),
252 PSCI_VERSION_MINOR(psci_version
));
254 vcpu_power_off(target
);
258 while (run
->exit_reason
== KVM_EXIT_SYSTEM_EVENT
) {
259 TEST_ASSERT(run
->system_event
.type
== KVM_SYSTEM_EVENT_SHUTDOWN
,
260 "Unhandled system event: %u (expected: %u)",
261 run
->system_event
.type
, KVM_SYSTEM_EVENT_SHUTDOWN
);
262 TEST_ASSERT(run
->system_event
.ndata
>= 1,
263 "Unexpected amount of system event data: %u (expected, >= 1)",
264 run
->system_event
.ndata
);
265 TEST_ASSERT(run
->system_event
.data
[0] & KVM_SYSTEM_EVENT_SHUTDOWN_FLAG_PSCI_OFF2
,
266 "PSCI_OFF2 flag not set. Flags %llu (expected %llu)",
267 run
->system_event
.data
[0], KVM_SYSTEM_EVENT_SHUTDOWN_FLAG_PSCI_OFF2
);
271 /* Restart the vCPU */
272 mps
.mp_state
= KVM_MP_STATE_RUNNABLE
;
273 vcpu_mp_state_set(source
, &mps
);
278 TEST_ASSERT(get_ucall(source
, &uc
) == UCALL_DONE
, "Guest did not exit cleanly");
279 TEST_ASSERT(nr_shutdowns
== 2, "Two shutdown events were expected, but saw %d", nr_shutdowns
);
284 TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SYSTEM_SUSPEND
));
287 host_test_system_suspend();
288 host_test_system_off2();