1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * psci_cpu_on_test - Test that the observable state of a vCPU targeted by the
4 * CPU_ON PSCI call matches what the caller requested.
5 *
6 * Copyright (c) 2021 Google LLC.
7 *
8 * This is a regression test for a race between KVM servicing the PSCI call and
9 * userspace reading the vCPUs registers.
10 */
11
12 #define _GNU_SOURCE
13
14 #include <linux/psci.h>
15
16 #include "kvm_util.h"
17 #include "processor.h"
18 #include "test_util.h"
19
20 #define VCPU_ID_SOURCE 0
21 #define VCPU_ID_TARGET 1
22
23 #define CPU_ON_ENTRY_ADDR 0xfeedf00dul
24 #define CPU_ON_CONTEXT_ID 0xdeadc0deul
25
psci_cpu_on(uint64_t target_cpu,uint64_t entry_addr,uint64_t context_id)26 static uint64_t psci_cpu_on(uint64_t target_cpu, uint64_t entry_addr,
27 uint64_t context_id)
28 {
29 register uint64_t x0 asm("x0") = PSCI_0_2_FN64_CPU_ON;
30 register uint64_t x1 asm("x1") = target_cpu;
31 register uint64_t x2 asm("x2") = entry_addr;
32 register uint64_t x3 asm("x3") = context_id;
33
34 asm("hvc #0"
35 : "=r"(x0)
36 : "r"(x0), "r"(x1), "r"(x2), "r"(x3)
37 : "memory");
38
39 return x0;
40 }
41
psci_affinity_info(uint64_t target_affinity,uint64_t lowest_affinity_level)42 static uint64_t psci_affinity_info(uint64_t target_affinity,
43 uint64_t lowest_affinity_level)
44 {
45 register uint64_t x0 asm("x0") = PSCI_0_2_FN64_AFFINITY_INFO;
46 register uint64_t x1 asm("x1") = target_affinity;
47 register uint64_t x2 asm("x2") = lowest_affinity_level;
48
49 asm("hvc #0"
50 : "=r"(x0)
51 : "r"(x0), "r"(x1), "r"(x2)
52 : "memory");
53
54 return x0;
55 }
56
guest_main(uint64_t target_cpu)57 static void guest_main(uint64_t target_cpu)
58 {
59 GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID));
60 uint64_t target_state;
61
62 do {
63 target_state = psci_affinity_info(target_cpu, 0);
64
65 GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) ||
66 (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF));
67 } while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON);
68
69 GUEST_DONE();
70 }
71
main(void)72 int main(void)
73 {
74 uint64_t target_mpidr, obs_pc, obs_x0;
75 struct kvm_vcpu_init init;
76 struct kvm_vm *vm;
77 struct ucall uc;
78
79 vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
80 kvm_vm_elf_load(vm, program_invocation_name);
81 ucall_init(vm, NULL);
82
83 vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init);
84 init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2);
85
86 aarch64_vcpu_add_default(vm, VCPU_ID_SOURCE, &init, guest_main);
87
88 /*
89 * make sure the target is already off when executing the test.
90 */
91 init.features[0] |= (1 << KVM_ARM_VCPU_POWER_OFF);
92 aarch64_vcpu_add_default(vm, VCPU_ID_TARGET, &init, guest_main);
93
94 get_reg(vm, VCPU_ID_TARGET, ARM64_SYS_REG(MPIDR_EL1), &target_mpidr);
95 vcpu_args_set(vm, VCPU_ID_SOURCE, 1, target_mpidr & MPIDR_HWID_BITMASK);
96 vcpu_run(vm, VCPU_ID_SOURCE);
97
98 switch (get_ucall(vm, VCPU_ID_SOURCE, &uc)) {
99 case UCALL_DONE:
100 break;
101 case UCALL_ABORT:
102 TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0], __FILE__,
103 uc.args[1]);
104 break;
105 default:
106 TEST_FAIL("Unhandled ucall: %lu", uc.cmd);
107 }
108
109 get_reg(vm, VCPU_ID_TARGET, ARM64_CORE_REG(regs.pc), &obs_pc);
110 get_reg(vm, VCPU_ID_TARGET, ARM64_CORE_REG(regs.regs[0]), &obs_x0);
111
112 TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR,
113 "unexpected target cpu pc: %lx (expected: %lx)",
114 obs_pc, CPU_ON_ENTRY_ADDR);
115 TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID,
116 "unexpected target context id: %lx (expected: %lx)",
117 obs_x0, CPU_ON_CONTEXT_ID);
118
119 kvm_vm_free(vm);
120 return 0;
121 }
122