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 #include <linux/kernel.h> 15 #include <linux/psci.h> 16 #include <asm/cputype.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_system_off2(uint64_t type, uint64_t cookie) 58 { 59 struct arm_smccc_res res; 60 61 smccc_hvc(PSCI_1_3_FN64_SYSTEM_OFF2, type, cookie, 0, 0, 0, 0, 0, &res); 62 63 return res.a0; 64 } 65 66 static uint64_t psci_features(uint32_t func_id) 67 { 68 struct arm_smccc_res res; 69 70 smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES, func_id, 0, 0, 0, 0, 0, 0, &res); 71 72 return res.a0; 73 } 74 75 static void vcpu_power_off(struct kvm_vcpu *vcpu) 76 { 77 struct kvm_mp_state mp_state = { 78 .mp_state = KVM_MP_STATE_STOPPED, 79 }; 80 81 vcpu_mp_state_set(vcpu, &mp_state); 82 } 83 84 static struct kvm_vm *setup_vm(void *guest_code, struct kvm_vcpu **source, 85 struct kvm_vcpu **target) 86 { 87 struct kvm_vcpu_init init; 88 struct kvm_vm *vm; 89 90 vm = vm_create(2); 91 92 vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init); 93 init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2); 94 95 *source = aarch64_vcpu_add(vm, 0, &init, guest_code); 96 *target = aarch64_vcpu_add(vm, 1, &init, guest_code); 97 98 return vm; 99 } 100 101 static void enter_guest(struct kvm_vcpu *vcpu) 102 { 103 struct ucall uc; 104 105 vcpu_run(vcpu); 106 if (get_ucall(vcpu, &uc) == UCALL_ABORT) 107 REPORT_GUEST_ASSERT(uc); 108 } 109 110 static void assert_vcpu_reset(struct kvm_vcpu *vcpu) 111 { 112 uint64_t obs_pc, obs_x0; 113 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); 116 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); 123 } 124 125 static void guest_test_cpu_on(uint64_t target_cpu) 126 { 127 uint64_t target_state; 128 129 GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID)); 130 131 do { 132 target_state = psci_affinity_info(target_cpu, 0); 133 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); 137 138 GUEST_DONE(); 139 } 140 141 static void host_test_cpu_on(void) 142 { 143 struct kvm_vcpu *source, *target; 144 uint64_t target_mpidr; 145 struct kvm_vm *vm; 146 struct ucall uc; 147 148 vm = setup_vm(guest_test_cpu_on, &source, &target); 149 150 /* 151 * make sure the target is already off when executing the test. 152 */ 153 vcpu_power_off(target); 154 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); 157 enter_guest(source); 158 159 if (get_ucall(source, &uc) != UCALL_DONE) 160 TEST_FAIL("Unhandled ucall: %lu", uc.cmd); 161 162 assert_vcpu_reset(target); 163 kvm_vm_free(vm); 164 } 165 166 static void guest_test_system_suspend(void) 167 { 168 uint64_t ret; 169 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)); 173 174 ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID); 175 GUEST_SYNC(ret); 176 } 177 178 static void host_test_system_suspend(void) 179 { 180 struct kvm_vcpu *source, *target; 181 struct kvm_run *run; 182 struct kvm_vm *vm; 183 184 vm = setup_vm(guest_test_system_suspend, &source, &target); 185 vm_enable_cap(vm, KVM_CAP_ARM_SYSTEM_SUSPEND, 0); 186 187 vcpu_power_off(target); 188 run = source->run; 189 190 enter_guest(source); 191 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); 196 197 kvm_vm_free(vm); 198 } 199 200 static void guest_test_system_off2(void) 201 { 202 uint64_t ret; 203 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); 209 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); 213 214 /* 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. 218 */ 219 ret = psci_system_off2(PSCI_1_3_OFF_TYPE_HIBERNATE_OFF, 0); 220 GUEST_ASSERT(ret == PSCI_RET_INTERNAL_FAILURE); 221 222 /* 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. 226 */ 227 ret = psci_system_off2(0, 1); 228 GUEST_ASSERT(ret == PSCI_RET_INVALID_PARAMS); 229 230 ret = psci_system_off2(0, 0); 231 GUEST_ASSERT(ret == PSCI_RET_INTERNAL_FAILURE); 232 233 GUEST_DONE(); 234 } 235 236 static void host_test_system_off2(void) 237 { 238 struct kvm_vcpu *source, *target; 239 struct kvm_mp_state mps; 240 uint64_t psci_version = 0; 241 int nr_shutdowns = 0; 242 struct kvm_run *run; 243 struct ucall uc; 244 245 setup_vm(guest_test_system_off2, &source, &target); 246 247 vcpu_get_reg(target, KVM_REG_ARM_PSCI_VERSION, &psci_version); 248 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)); 253 254 vcpu_power_off(target); 255 run = source->run; 256 257 enter_guest(source); 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); 268 269 nr_shutdowns++; 270 271 /* Restart the vCPU */ 272 mps.mp_state = KVM_MP_STATE_RUNNABLE; 273 vcpu_mp_state_set(source, &mps); 274 275 enter_guest(source); 276 } 277 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); 280 } 281 282 int main(void) 283 { 284 TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SYSTEM_SUSPEND)); 285 286 host_test_cpu_on(); 287 host_test_system_suspend(); 288 host_test_system_off2(); 289 return 0; 290 } 291