Skip to content

Commit 5a7c7d1

Browse files
committed
KVM: selftests: Play nice with AMD's AVIC errata
When AVIC, and thus IPI virtualization on AMD, is enabled, the CPU will virtualize ICR writes. Unfortunately, the CPU doesn't do a very good job, as it fails to clear the BUSY bit and also allows writing ICR2[23:0], despite them being "RESERVED MBZ". Account for the quirky behavior in the xapic_state test to avoid failures in a configuration that likely has no hope of ever being enabled in production. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 0cb26ec commit 5a7c7d1

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
struct xapic_vcpu {
1414
struct kvm_vcpu *vcpu;
1515
bool is_x2apic;
16+
bool has_xavic_errata;
1617
};
1718

1819
static void xapic_guest_code(void)
@@ -79,12 +80,17 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val)
7980
vcpu_ioctl(vcpu, KVM_GET_LAPIC, &xapic);
8081
icr = (u64)(*((u32 *)&xapic.regs[APIC_ICR])) |
8182
(u64)(*((u32 *)&xapic.regs[APIC_ICR2])) << 32;
82-
if (!x->is_x2apic)
83-
val &= (-1u | (0xffull << (32 + 24)));
84-
else if (val & X2APIC_RSVD_BITS_MASK)
83+
if (!x->is_x2apic) {
84+
if (!x->has_xavic_errata)
85+
val &= (-1u | (0xffull << (32 + 24)));
86+
} else if (val & X2APIC_RSVD_BITS_MASK) {
8587
return;
88+
}
8689

87-
TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY);
90+
if (x->has_xavic_errata)
91+
TEST_ASSERT_EQ(icr & ~APIC_ICR_BUSY, val & ~APIC_ICR_BUSY);
92+
else
93+
TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY);
8894
}
8995

9096
static void __test_icr(struct xapic_vcpu *x, uint64_t val)
@@ -236,6 +242,15 @@ int main(int argc, char *argv[])
236242
vm = vm_create_with_one_vcpu(&x.vcpu, xapic_guest_code);
237243
x.is_x2apic = false;
238244

245+
/*
246+
* AMD's AVIC implementation is buggy (fails to clear the ICR BUSY bit),
247+
* and also diverges from KVM with respect to ICR2[23:0] (KVM and Intel
248+
* drops writes, AMD does not). Account for the errata when checking
249+
* that KVM reads back what was written.
250+
*/
251+
x.has_xavic_errata = host_cpu_is_amd &&
252+
get_kvm_amd_param_bool("avic");
253+
239254
vcpu_clear_cpuid_feature(x.vcpu, X86_FEATURE_X2APIC);
240255

241256
virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);

0 commit comments

Comments
 (0)