/* * Tell the guest what ICR value to write. Use the IRR to pass info, * all bits are valid and should not be modified by KVM (ignoring the * fact that vectors 0-15 are technically illegal).
*/
vcpu_ioctl(vcpu, KVM_GET_LAPIC, &xapic);
*((u32 *)&xapic.regs[APIC_IRR]) = val;
*((u32 *)&xapic.regs[APIC_IRR + 0x10]) = val >> 32;
vcpu_ioctl(vcpu, KVM_SET_LAPIC, &xapic);
if (x->has_xavic_errata)
TEST_ASSERT_EQ(icr & ~APIC_ICR_BUSY, val & ~APIC_ICR_BUSY); else
TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY);
}
staticvoid __test_icr(struct xapic_vcpu *x, uint64_t val)
{ /* * The BUSY bit is reserved on both AMD and Intel, but only AMD treats * it is as _must_ be zero. Intel simply ignores the bit. Don't test * the BUSY bit for x2APIC, as there is no single correct behavior.
*/ if (!x->is_x2apic)
____test_icr(x, val | APIC_ICR_BUSY);
icr = APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_FIXED; for (i = 0; i <= 0xff; i++)
__test_icr(x, icr | i);
icr = APIC_INT_ASSERT | APIC_DM_FIXED; for (i = 0; i <= 0xff; i++)
__test_icr(x, icr | i);
/* * Send all flavors of IPIs to non-existent vCPUs. TODO: use number of * vCPUs, not vcpu.id + 1. Arbitrarily use vector 0xff.
*/
icr = APIC_INT_ASSERT | 0xff; for (i = 0; i < 0xff; i++) { if (i == vcpu->id) continue; for (j = 0; j < 8; j++)
__test_icr(x, i << (32 + 24) | icr | (j << 8));
}
/* And again with a shorthand destination for all types of IPIs. */
icr = APIC_DEST_ALLBUT | APIC_INT_ASSERT; for (i = 0; i < 8; i++)
__test_icr(x, icr | (i << 8));
/* And a few garbage value, just make sure it's an IRQ (blocked). */
__test_icr(x, 0xa5a5a5a5a5a5a5a5 & ~APIC_DM_FIXED_MASK);
__test_icr(x, 0x5a5a5a5a5a5a5a5a & ~APIC_DM_FIXED_MASK);
__test_icr(x, -1ull & ~APIC_DM_FIXED_MASK);
}
TEST_ASSERT(apic_id == expected, "APIC_ID not set back to %s format; wanted = %x, got = %x",
(apic_base & X2APIC_ENABLE) ? "x2APIC" : "xAPIC",
expected, apic_id);
}
/* * Verify that KVM switches the APIC_ID between xAPIC and x2APIC when userspace * stuffs MSR_IA32_APICBASE. Setting the APIC_ID when x2APIC is enabled and * when the APIC transitions for DISABLED to ENABLED is architectural behavior * (on Intel), whereas the x2APIC => xAPIC transition behavior is KVM ABI since * attempted to transition from x2APIC to xAPIC without disabling the APIC is * architecturally disallowed.
*/ staticvoid test_apic_id(void)
{ const uint32_t NR_VCPUS = 3; struct kvm_vcpu *vcpus[NR_VCPUS];
uint64_t apic_base; struct kvm_vm *vm; int i;
vm = vm_create_with_vcpus(NR_VCPUS, NULL, vcpus);
vm_enable_cap(vm, KVM_CAP_X2APIC_API, KVM_X2APIC_API_USE_32BIT_IDS);
for (i = 0; i < NR_VCPUS; i++) {
apic_base = vcpu_get_msr(vcpus[i], MSR_IA32_APICBASE);
TEST_ASSERT(apic_base & MSR_IA32_APICBASE_ENABLE, "APIC not in ENABLED state at vCPU RESET");
TEST_ASSERT(!(apic_base & X2APIC_ENABLE), "APIC not in xAPIC mode at vCPU RESET");
vm = vm_create_with_one_vcpu(&vcpu, NULL);
vcpu_set_msr(vcpu, MSR_IA32_APICBASE, MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
/* * Try stuffing a modified x2APIC ID, KVM should ignore the value and * always return the vCPU's default/readonly x2APIC ID.
*/ for (i = 0; i <= 0xff; i++) {
*(u32 *)(lapic.regs + APIC_ID) = i << 24;
*(u32 *)(lapic.regs + APIC_SPIV) = APIC_SPIV_APIC_ENABLED;
vcpu_ioctl(vcpu, KVM_SET_LAPIC, &lapic);
vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic);
TEST_ASSERT(*((u32 *)&lapic.regs[APIC_ID]) == vcpu->id << 24, "x2APIC ID should be fully readonly");
}
vm = vm_create_with_one_vcpu(&x.vcpu, x2apic_guest_code);
test_icr(&x);
kvm_vm_free(vm);
/* * Use a second VM for the xAPIC test so that x2APIC can be hidden from * the guest in order to test AVIC. KVM disallows changing CPUID after * KVM_RUN and AVIC is disabled if _any_ vCPU is allowed to use x2APIC.
*/
vm = vm_create_with_one_vcpu(&x.vcpu, xapic_guest_code);
x.is_x2apic = false;
/* * AMD's AVIC implementation is buggy (fails to clear the ICR BUSY bit), * and also diverges from KVM with respect to ICR2[23:0] (KVM and Intel * drops writes, AMD does not). Account for the errata when checking * that KVM reads back what was written.
*/
x.has_xavic_errata = host_cpu_is_amd &&
get_kvm_amd_param_bool("avic");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.