Skip to content

Commit cb97cf9

Browse files
ouptonMarc Zyngier
authored andcommitted
selftests: KVM: Introduce psci_cpu_on_test
Introduce a test for aarch64 that ensures CPU resets induced by PSCI are reflected in the target vCPU's state, even if the target is never run again. This is a regression test for a race between vCPU migration and PSCI. Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Oliver Upton <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e10ecb4 commit cb97cf9

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

tools/testing/selftests/kvm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
/aarch64/debug-exceptions
33
/aarch64/get-reg-list
4+
/aarch64/psci_cpu_on_test
45
/aarch64/vgic_init
56
/s390x/memop
67
/s390x/resets

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
8686

8787
TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
8888
TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
89+
TEST_GEN_PROGS_aarch64 += aarch64/psci_cpu_on_test
8990
TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
9091
TEST_GEN_PROGS_aarch64 += demand_paging_test
9192
TEST_GEN_PROGS_aarch64 += dirty_log_test
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
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+
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+
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+
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+
}

tools/testing/selftests/kvm/include/aarch64/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define CPACR_EL1 3, 0, 1, 0, 2
1818
#define TCR_EL1 3, 0, 2, 0, 2
1919
#define MAIR_EL1 3, 0, 10, 2, 0
20+
#define MPIDR_EL1 3, 0, 0, 0, 5
2021
#define TTBR0_EL1 3, 0, 2, 0, 0
2122
#define SCTLR_EL1 3, 0, 1, 0, 0
2223
#define VBAR_EL1 3, 0, 12, 0, 0
@@ -40,6 +41,8 @@
4041
(0xfful << (4 * 8)) | \
4142
(0xbbul << (5 * 8)))
4243

44+
#define MPIDR_HWID_BITMASK (0xff00fffffful)
45+
4346
static inline void get_reg(struct kvm_vm *vm, uint32_t vcpuid, uint64_t id, uint64_t *addr)
4447
{
4548
struct kvm_one_reg reg;

0 commit comments

Comments
 (0)