KVM: x86: Fix ICR handling when x2AVIC is active
Sean Christopherson
maintainercover KVM: x86: Fix ICR handling when x2AVIC is active
I made the mistake of expanding my testing to run with and without AVIC enabled, and to my surprise (wow, sarcasm), x2AVIC failed hard on the xapic_state_test due to ICR issues.
AFAICT, the issue is that AMD splits the 64-bit ICR into the legacy ICR and ICR2 fields when storing the ICR in the vAPIC (apparently "it's a single 64-bit register" is open to intepretation). Aside from causing the selftest failure and potential live migration issues, botching the format is quite bad, as KVM will mishandle incomplete virtualized IPIs, e.g. generate IRQs to the wrong vCPU, drop IRQs, etc.
Patch 1 fixes are rather annoying wart where the xapic_state *deliberately* skips reserved bit tests to work around a KVM bug. *sigh*
I couldn't find anything definitive in the APM, my findings are based on testing on Genoa.
Sean Christopherson (8):
KVM: x86: Enforce x2APIC's must-be-zero reserved ICR bits
KVM: x86: Move x2APIC ICR helper above kvm_apic_write_nodecode()
KVM: x86: Re-split x2APIC ICR into ICR+ICR2 for AMD (x2AVIC)
KVM: selftests: Open code vcpu_run() equivalent in guest_printf test
KVM: selftests: Report unhandled exceptions on x86 as regular guest
asserts
KVM: selftests: Add x86 helpers to play nice with x2APIC MSR #GPs
KVM: selftests: Skip ICR.BUSY test in xapic_state_test if x2APIC is
enabled
KVM: selftests: Test x2APIC ICR reserved bits
arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/lapic.c | 73 +++++++++++++------ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/vmx/main.c | 2 + .../testing/selftests/kvm/guest_print_test.c | 19 ++++- .../selftests/kvm/include/x86_64/apic.h | 21 +++++- .../selftests/kvm/lib/x86_64/processor.c | 8 +- .../selftests/kvm/x86_64/xapic_state_test.c | 39 +++++----- 8 files changed, 119 insertions(+), 47 deletions(-)
base-commit: 332d2c1d713e232e163386c35a3ba0c1b90df83f
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 1/8 KVM: x86: Enforce x2APIC's must-be-zero reserved ICR bits
Inject a #GP on a WRMSR(ICR) that attempts to set any reserved bits that are must-be-zero on both Intel and AMD, i.e. any reserved bits other than the BUSY bit, which Intel ignores and basically says is undefined.
KVM's xapic_state_test selftest has been fudging the bug since commit 4b88b1a518b3 ("KVM: selftests: Enhance handling WRMSR ICR register in x2APIC mode"), which essentially removed the testcase instead of fixing the bug.
WARN if the nodecode path triggers a #GP, as the CPU is supposed to check reserved bits for ICR when it's partially virtualized.
--- arch/x86/kvm/lapic.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +14 -1
@@ -2472,7 +2472,7 @@ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)* maybe-unecessary write, and both are in the noise anyways. */ if (apic_x2apic_mode (apic) && offset ==APIC_ICR ) -kvm_x2apic_icr_write (apic,kvm_lapic_get_reg64 (apic,APIC_ICR )); +WARN_ON_ONCE (kvm_x2apic_icr_write (apic,kvm_lapic_get_reg64 (apic,APIC_ICR )));else kvm_lapic_reg_write (apic, offset,kvm_lapic_get_reg (apic, offset)); }
@@ -3186,8 +3186,21 @@ int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)return 0 ; } +#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13)) +int kvm_x2apic_icr_write (struct kvm_lapic *apic,u64 data) { +if (data &X2APIC_ICR_RESERVED_BITS ) +return 1 ; + +/* +* The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but +* only AMD requires it to be zero, Intel essentially just ignores the +* bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled, +* the CPU performs the reserved bits checks, i.e. the underlying CPU +* behavior will "win". Arbitrarily clear the BUSY bit, as there is no +* sane way to provide consistent behavior with respect to hardware. +*/ data &= ~APIC_ICR_BUSY ;kvm_apic_send_ipi (apic, (u32 )data, (u32)(data >>32 ));
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 2/8 KVM: x86: Move x2APIC ICR helper above kvm_apic_write_nodecode()
In reply to Sean ChristophersonHoist kvm_x2apic_icr_write() above kvm_apic_write_nodecode() so that a local helper to _read_ the x2APIC ICR can be added and used in the nodecode path without needing a forward declaration.
No functional change intended.
--- arch/x86/kvm/lapic.c | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-)
arch/x86/kvm/lapic.c +23 -23
@@ -2455,6 +2455,29 @@ void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_lapic_set_eoi); +#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13)) + +int kvm_x2apic_icr_write (struct kvm_lapic *apic,u64 data) +{ +if (data &X2APIC_ICR_RESERVED_BITS ) +return 1 ; + +/* +* The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but +* only AMD requires it to be zero, Intel essentially just ignores the +* bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled, +* the CPU performs the reserved bits checks, i.e. the underlying CPU +* behavior will "win". Arbitrarily clear the BUSY bit, as there is no +* sane way to provide consistent behavior with respect to hardware. +*/ + data &= ~APIC_ICR_BUSY ; + +kvm_apic_send_ipi (apic, (u32 )data, (u32)(data >>32 )); +kvm_lapic_set_reg64 (apic,APIC_ICR , data); +trace_kvm_apic_write (APIC_ICR , data); +return 0 ; +} +/* emulate APIC access in a trap manner */ void kvm_apic_write_nodecode (struct kvm_vcpu *vcpu,u32 offset) {
@@ -3186,29 +3209,6 @@ int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)return 0 ; } -#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13)) - -int kvm_x2apic_icr_write (struct kvm_lapic *apic,u64 data) -{ -if (data &X2APIC_ICR_RESERVED_BITS ) -return 1 ; - -/* -* The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but -* only AMD requires it to be zero, Intel essentially just ignores the -* bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled, -* the CPU performs the reserved bits checks, i.e. the underlying CPU -* behavior will "win". Arbitrarily clear the BUSY bit, as there is no -* sane way to provide consistent behavior with respect to hardware. -*/ - data &= ~APIC_ICR_BUSY ; - -kvm_apic_send_ipi (apic, (u32 )data, (u32)(data >>32 )); -kvm_lapic_set_reg64 (apic,APIC_ICR , data); -trace_kvm_apic_write (APIC_ICR , data); -return 0 ; -} -static int kvm_lapic_msr_read (struct kvm_lapic *apic,u32 reg,u64 *data) {u32 low;
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 3/8 KVM: x86: Re-split x2APIC ICR into ICR+ICR2 for AMD (x2AVIC)
In reply to Sean ChristophersonRe-introduce the "split" x2APIC ICR storage that KVM used prior to Intel's IPI virtualization support, but only for AMD. While not stated anywhere in the APM, despite stating the ICR is a single 64-bit register, AMD CPUs store the 64-bit ICR as two separate 32-bit values in ICR and ICR2. When IPI virtualization (IPIv on Intel, all AVIC flavors on AMD) is enabled, KVM needs to match CPU behavior as some ICR ICR writes will be handled by the CPU, not by KVM.
Add a kvm_x86_ops knob to control the underlying format used by the CPU to store the x2APIC ICR, and tune it to AMD vs. Intel regardless of whether or not x2AVIC is enabled. If KVM is handling all ICR writes, the storage format for x2APIC mode doesn't matter, and having the behavior follow AMD versus Intel will provide better test coverage and ease debugging.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/lapic.c | 42 +++++++++++++++++++++++---------- arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/vmx/main.c | 2 ++ 4 files changed, 36 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1726,6 +1726,8 @@ struct kvm_x86_ops {void (*enable_nmi_window)(struct kvm_vcpu *vcpu);void (*enable_irq_window)(struct kvm_vcpu *vcpu);void (*update_cr8_intercept)(struct kvm_vcpu *vcpu,int tpr,int irr); + +const bool x2apic_icr_is_split;const unsigned long required_apicv_inhibits;bool allow_apicv_in_x2apic_without_x2apic_virtualization;void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu);
arch/x86/kvm/lapic.c +30 -12
73 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +2 -0
@@ -5049,6 +5049,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .enable_nmi_window = svm_enable_nmi_window, .enable_irq_window = svm_enable_irq_window, .update_cr8_intercept = svm_update_cr8_intercept, + + .x2apic_icr_is_split = true, .set_virtual_apic_mode = avic_refresh_virtual_apic_mode, .refresh_apicv_exec_ctrl = avic_refresh_apicv_exec_ctrl, .apicv_post_state_restore = avic_apicv_post_state_restore,
arch/x86/kvm/vmx/main.c +2 -0
@@ -89,6 +89,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .enable_nmi_window = vmx_enable_nmi_window, .enable_irq_window = vmx_enable_irq_window, .update_cr8_intercept = vmx_update_cr8_intercept, + + .x2apic_icr_is_split = false, .set_virtual_apic_mode = vmx_set_virtual_apic_mode, .set_apic_access_page_addr = vmx_set_apic_access_page_addr, .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 4/8 KVM: selftests: Open code vcpu_run() equivalent in guest_printf test
In reply to Sean ChristophersonOpen code a version of vcpu_run() in the guest_printf test in anticipation of adding UCALL_ABORT handling to _vcpu_run(). The guest_printf test intentionally generates asserts to verify the output, and thus needs to bypass common assert handling.
Open code a helper in the guest_printf test, as it's not expected that any other test would want to skip _only_ the UCALL_ABORT handling.
--- .../testing/selftests/kvm/guest_print_test.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/guest_print_test.c +17 -2
@@ -107,6 +107,21 @@ static void ucall_abort(const char *assert_msg, const char *expected_assert_msg) expected_assert_msg, &assert_msg[offset]); } +/* +* Open code vcpu_run(), sans the UCALL_ABORT handling, so that intentional +* guest asserts guest can be verified instead of being reported as failures. +*/ +static void do_vcpu_run (struct kvm_vcpu *vcpu) +{ +int r; + +do { + r =__vcpu_run (vcpu); + }while (r ==-1 && errno ==EINTR ); + +TEST_ASSERT (!r,KVM_IOCTL_ERROR (KVM_RUN , r)); +} +static void run_test (struct kvm_vcpu *vcpu,const char *expected_printf,const char *expected_assert) {
@@ -114,7 +129,7 @@ static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,struct ucall uc;while (1 ) { -vcpu_run (vcpu); +do_vcpu_run (vcpu); TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON, "Unexpected exit reason: %u (%s),",
@@ -159,7 +174,7 @@ static void test_limits(void) vm =vm_create_with_one_vcpu (&vcpu, guest_code_limits); run = vcpu->run; -vcpu_run (vcpu); +do_vcpu_run (vcpu); TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON, "Unexpected exit reason: %u (%s),",
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 5/8 KVM: selftests: Report unhandled exceptions on x86 as regular guest asserts
In reply to Sean ChristophersonNow that selftests support printf() in the guest, report unexpected exceptions via the regular assertion framework. Exceptions were special cased purely to provide a better error message. Convert only x86 for now, as it's low-hanging fruit (already formats the assertion in the guest), and converting x86 will allow adding asserts in x86 library code without needing to update multiple tests.
Once all other architectures are converted, this will allow moving the reporting to common code, which will in turn allow adding asserts in common library code, and will also allow removing UCALL_UNHANDLED.
--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +3 -5
@@ -566,10 +566,8 @@ void route_exception(struct ex_regs *regs)if (kvm_fixup_exception (regs))return ; -ucall_assert (UCALL_UNHANDLED , -"Unhandled exception in guest" , __FILE__, __LINE__, -"Unhandled exception '0x%lx' at guest RIP '0x%lx'" , - regs->vector, regs->rip); +GUEST_FAIL ("Unhandled exception '0x%lx' at guest RIP '0x%lx'" , + regs->vector, regs->rip); }static void vm_init_descriptor_tables (struct kvm_vm *vm)
@@ -611,7 +609,7 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu) {struct ucall uc; -if (get_ucall (vcpu, &uc) ==UCALL_UNHANDLED ) +if (get_ucall (vcpu, &uc) ==UCALL_ABORT )REPORT_GUEST_ASSERT (uc); }
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 6/8 KVM: selftests: Add x86 helpers to play nice with x2APIC MSR #GPs
In reply to Sean ChristophersonAdd helpers to allow and expect #GP on x2APIC MSRs, and opportunistically have the existing helper spit out a more useful error message if an unexpected exception occurs.
--- .../selftests/kvm/include/x86_64/apic.h | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/x86_64/apic.h +20 -1
@@ -11,6 +11,7 @@#include <stdint.h> #include "processor.h" +#include "ucall_common.h" #define APIC_DEFAULT_GPA 0xfee00000ULL
@@ -93,9 +94,27 @@ static inline uint64_t x2apic_read_reg(unsigned int reg) return rdmsr(APIC_BASE_MSR + (reg >> 4)); } +static inline uint8_t x2apic_write_reg_safe (unsigned int reg,uint64_t value) +{ +return wrmsr_safe (APIC_BASE_MSR + (reg >>4 ), value); +} +static inline void x2apic_write_reg (unsigned int reg,uint64_t value) { -wrmsr (APIC_BASE_MSR + (reg >>4 ), value); +uint8_t fault =x2apic_write_reg_safe (reg, value); + +__GUEST_ASSERT (!fault,"Unexpected fault 0x%x on WRMSR(%x) = %lx\n" , + fault,APIC_BASE_MSR + (reg >>4 ), value); } +static inline void x2apic_write_reg_fault (unsigned int reg,uint64_t value) +{ +uint8_t fault =x2apic_write_reg_safe (reg, value); + +__GUEST_ASSERT (fault ==GP_VECTOR , +"Wanted #GP on WRMSR(%x) = %lx, got 0x%x\n" , +APIC_BASE_MSR + (reg >>4 ), value, fault); +} + +#endif /* SELFTEST_KVM_APIC_H */
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 7/8 KVM: selftests: Skip ICR.BUSY test in xapic_state_test if x2APIC is enabled
In reply to Sean ChristophersonDon't test the ICR BUSY bit when x2APIC is enabled as AMD and Intel have different behavior (AMD #GPs, Intel ignores), and the fact that the CPU performs the reserved bit checks when IPI virtualization is enabled makes it impossible for KVM to precisely emulate one or the other.
--- .../selftests/kvm/x86_64/xapic_state_test.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/x86_64/xapic_state_test.c +12 -6
@@ -70,12 +70,10 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val)vcpu_ioctl (vcpu,KVM_GET_LAPIC , &xapic); icr = (u64)(*((u32 *)&xapic.regs[APIC_ICR ])) | (u64)(*((u32 *)&xapic.regs[APIC_ICR2 ])) <<32 ; -if (!x->is_x2apic) { +if (!x->is_x2apic) val &= (-1u | (0xffull << (32 +24 ))); -TEST_ASSERT_EQ (icr, val & ~APIC_ICR_BUSY ); - }else { -TEST_ASSERT_EQ (icr & ~APIC_ICR_BUSY , val & ~APIC_ICR_BUSY ); - } + +TEST_ASSERT_EQ (icr, val & ~APIC_ICR_BUSY ); }#define X2APIC_RSVED_BITS_MASK (GENMASK_ULL(31,20) | \
@@ -91,7 +89,15 @@ static void __test_icr(struct xapic_vcpu *x, uint64_t val)*/ val &= ~X2APIC_RSVED_BITS_MASK ; } -____test_icr (x, val |APIC_ICR_BUSY ); + +/* +* 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 ); +____test_icr (x, val & ~(u64 )APIC_ICR_BUSY ); }
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerPATCH 8/8 KVM: selftests: Test x2APIC ICR reserved bits
In reply to Sean ChristophersonActually test x2APIC ICR reserved bits instead of deliberately skipping them. The behavior that is observed when IPI virtualization is enabled is the architecturally correct behavior, KVM is the one who was wrong, i.e. KVM was missing reserved bit checks.
--- .../selftests/kvm/x86_64/xapic_state_test.c | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/x86_64/xapic_state_test.c +10 -13
@@ -31,6 +31,10 @@ static void xapic_guest_code(void) } } +#define X2APIC_RSVD_BITS_MASK (GENMASK_ULL(31, 20) | \ + GENMASK_ULL(17, 16) | \ + GENMASK_ULL(13, 13)) +static void x2apic_guest_code (void ) { asmvolatile ("cli" );
@@ -41,7 +45,10 @@ static void x2apic_guest_code(void)uint64_t val =x2apic_read_reg (APIC_IRR ) |x2apic_read_reg (APIC_IRR +0x10 ) <<32 ; -x2apic_write_reg (APIC_ICR , val); +if (val &X2APIC_RSVD_BITS_MASK ) +x2apic_write_reg_fault (APIC_ICR , val); +else +x2apic_write_reg (APIC_ICR , val);GUEST_SYNC (val); }while (1 ); }
@@ -72,24 +79,14 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val) (u64)(*((u32 *)&xapic.regs[APIC_ICR2 ])) <<32 ;if (!x->is_x2apic) val &= (-1u | (0xffull << (32 +24 ))); +else if (val &X2APIC_RSVD_BITS_MASK ) +return ;TEST_ASSERT_EQ (icr, val & ~APIC_ICR_BUSY ); } -#define X2APIC_RSVED_BITS_MASK (GENMASK_ULL(31,20) | \ - GENMASK_ULL(17,16) | \ - GENMASK_ULL(13,13)) -static void __test_icr (struct xapic_vcpu *x,uint64_t val) { -if (x->is_x2apic) { -/* Hardware writing vICR register requires reserved bits 31:20, -* 17:16 and 13 kept as zero to avoid #GP exception. Data value -* written to vICR should mask out those bits above. -*/ - val &= ~X2APIC_RSVED_BITS_MASK ; - } -/* * 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
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Sean Christopherson
maintainerQuoted 17 lines · Sean Christopherson
I made the mistake of expanding my testing to run with and without AVIC enabled, and to my surprise (wow, sarcasm), x2AVIC failed hard on the
xapic_state_testdue to ICR issues.AFAICT, the issue is that AMD splits the 64-bit ICR into the legacy ICR and ICR2 fields when storing the ICR in the vAPIC (apparently "it's a single 64-bit register" is open to intepretation). Aside from causing the selftest failure and potential live migration issues, botching the format is quite bad, as KVM will mishandle incomplete virtualized IPIs, e.g. generate IRQs to the wrong vCPU, drop IRQs, etc.
Patch 1 fixes are rather annoying wart where the
xapic_state*deliberately* skips reserved bit tests to work around a KVM bug. *sigh*I couldn't find anything definitive in the APM, my findings are based on testing on Genoa.
Quoted 11 lines
Sean Christopherson (8):
KVM: x86: Enforce x2APIC's must-be-zero reserved ICR bits
KVM: x86: Move x2APIC ICR helper abovekvm_apic_write_nodecode()
KVM: x86: Re-split x2APIC ICR into ICR+ICR2 for AMD (x2AVIC)
KVM: selftests: Open codevcpu_run()equivalent inguest_printftest
KVM: selftests: Report unhandled exceptions on x86 as regular guest
asserts
KVM: selftests: Add x86 helpers to play nice with x2APIC MSR #GPs
KVM: selftests: Skip ICR.BUSY test inxapic_state_testif x2APIC is
enabled
KVM: selftests: Test x2APIC ICR reserved bits
Gah, ignore this version, I managed to hit send in the middle of a rebase and left off two patches. I'll post a v2 to minimize confusion.