Skip to content

Commit c258504

Browse files
vittyvksean-jc
authored andcommitted
KVM: selftests: Check that PV_UNHALT is cleared when HLT exiting is disabled
KVM_FEATURE_PV_UNHALT is expected to get cleared from KVM PV feature CPUID data when KVM_X86_DISABLE_EXITS_HLT is enabled. Add the corresponding test to kvm_pv_test. Note, the newly added code doesn't actually test KVM_FEATURE_PV_UNHALT and KVM_X86_DISABLE_EXITS_HLT features. Signed-off-by: Vitaly Kuznetsov <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: add and use vcpu_cpuid_has()] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 4736d85 commit c258504

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,15 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)
995995
vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
996996
}
997997

998+
static inline bool vcpu_cpuid_has(struct kvm_vcpu *vcpu,
999+
struct kvm_x86_cpu_feature feature)
1000+
{
1001+
struct kvm_cpuid_entry2 *entry;
1002+
1003+
entry = __vcpu_get_cpuid_entry(vcpu, feature.function, feature.index);
1004+
return *((&entry->eax) + feature.reg) & BIT(feature.bit);
1005+
}
1006+
9981007
void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyaddr);
9991008

10001009
void vcpu_clear_cpuid_entry(struct kvm_vcpu *vcpu, uint32_t function);

tools/testing/selftests/kvm/x86_64/kvm_pv_test.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,43 @@ static void enter_guest(struct kvm_vcpu *vcpu)
133133
}
134134
}
135135

136+
static void test_pv_unhalt(void)
137+
{
138+
struct kvm_vcpu *vcpu;
139+
struct kvm_vm *vm;
140+
struct kvm_cpuid_entry2 *ent;
141+
u32 kvm_sig_old;
142+
143+
pr_info("testing KVM_FEATURE_PV_UNHALT\n");
144+
145+
TEST_REQUIRE(KVM_CAP_X86_DISABLE_EXITS);
146+
147+
/* KVM_PV_UNHALT test */
148+
vm = vm_create_with_one_vcpu(&vcpu, guest_main);
149+
vcpu_set_cpuid_feature(vcpu, X86_FEATURE_KVM_PV_UNHALT);
150+
151+
TEST_ASSERT(vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
152+
"Enabling X86_FEATURE_KVM_PV_UNHALT had no effect");
153+
154+
/* Make sure KVM clears vcpu->arch.kvm_cpuid */
155+
ent = vcpu_get_cpuid_entry(vcpu, KVM_CPUID_SIGNATURE);
156+
kvm_sig_old = ent->ebx;
157+
ent->ebx = 0xdeadbeef;
158+
vcpu_set_cpuid(vcpu);
159+
160+
vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS, KVM_X86_DISABLE_EXITS_HLT);
161+
ent = vcpu_get_cpuid_entry(vcpu, KVM_CPUID_SIGNATURE);
162+
ent->ebx = kvm_sig_old;
163+
vcpu_set_cpuid(vcpu);
164+
165+
TEST_ASSERT(!vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
166+
"KVM_FEATURE_PV_UNHALT is set with KVM_CAP_X86_DISABLE_EXITS");
167+
168+
/* FIXME: actually test KVM_FEATURE_PV_UNHALT feature */
169+
170+
kvm_vm_free(vm);
171+
}
172+
136173
int main(void)
137174
{
138175
struct kvm_vcpu *vcpu;
@@ -151,4 +188,6 @@ int main(void)
151188

152189
enter_guest(vcpu);
153190
kvm_vm_free(vm);
191+
192+
test_pv_unhalt();
154193
}

0 commit comments

Comments
 (0)