x86: nSVM: Fix a bug with nNPT+x2AVIC
Sean Christopherson
reviewercover x86: nSVM: Fix a bug with nNPT+x2AVIC
Fix a bug in the nNPT passthrough APIC (MMIO) test where it runs with x2APIC enabled. The test currently passes because KVM provides consistent behavior when the APIC is accessed via MMIO in x2APIC mode (see KVM_X86_QUIRK_LAPIC_MMIO_HOLE), and because it runs with an in-kernel PIT, which inhibits x2AVIC.
But with a split IRQCHIP, i.e. no preconfigured PIT, x2AVIC gets enabled and the test fails because hardware doesn't provide the exact same behavior as KVM (returns 0s instead of FFs).
Sean Christopherson (2):
x86: apic: Move helpers for querying APIC state to library code
x86: nSVM: Ensure APIC MMIO tests run with APIC in xAPIC mode
lib/x86/apic.h | 21 +++++++++++++++++++++ x86/apic.c | 20 -------------------- x86/svm_npt.c | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 20 deletions(-)
base-commit: 68fee697b589b7eb7b82e8dd60155c5ccf054275
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Sean Christopherson
reviewerPATCH 1/2 x86: apic: Move helpers for querying APIC state to library code
Expose the helpers to query if an APIC is enabled, and in xAPIC vs. x2APIC mode, to library code so that the helpers can be used by all tests.
No funtional change intended.
--- lib/x86/apic.h | 21 +++++++++++++++++++++ x86/apic.c | 20 -------------------- 2 files changed, 21 insertions(+), 20 deletions(-)
lib/x86/apic.h +21 -0
@@ -5,6 +5,7 @@#include <stdint.h> #include "apic-defs.h" +#include "processor.h" #include "smp.h" extern u8 id_map[MAX_TEST_CPUS ];
@@ -67,6 +68,26 @@ void apic_setup_timer(int vector, u32 mode);void apic_start_timer (u32 counter);void apic_stop_timer (void ); +static inline bool is_apic_hw_enabled (void ) +{ +return rdmsr (MSR_IA32_APICBASE ) &APIC_EN ; +} + +static inline bool is_apic_sw_enabled (void ) +{ +return apic_read (APIC_SPIV ) &APIC_SPIV_APIC_ENABLED ; +} + +static inline bool is_x2apic_enabled (void ) +{ +return (rdmsr (MSR_IA32_APICBASE ) & (APIC_EN |APIC_EXTD )) == (APIC_EN |APIC_EXTD ); +} + +static inline bool is_xapic_enabled (void ) +{ +return (rdmsr (MSR_IA32_APICBASE ) & (APIC_EN |APIC_EXTD )) ==APIC_EN ; +} +/* Converts byte-addressable APIC register offset to 4-byte offset. */ static inline u32 apic_reg_index (u32 reg) {
x86/apic.c +0 -20
@@ -10,26 +10,6 @@#define MAX_TPR 0xf -static bool is_apic_hw_enabled (void ) -{ -return rdmsr (MSR_IA32_APICBASE ) &APIC_EN ; -} - -static bool is_apic_sw_enabled (void ) -{ -return apic_read (APIC_SPIV ) &APIC_SPIV_APIC_ENABLED ; -} - -static bool is_x2apic_enabled (void ) -{ -return (rdmsr (MSR_IA32_APICBASE ) & (APIC_EN |APIC_EXTD )) == (APIC_EN |APIC_EXTD ); -} - -static bool is_xapic_enabled (void ) -{ -return (rdmsr (MSR_IA32_APICBASE ) & (APIC_EN |APIC_EXTD )) ==APIC_EN ; -} -static void test_lapic_existence (void ) {u8 version;
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Sean Christopherson
reviewerPATCH 2/2 x86: nSVM: Ensure APIC MMIO tests run with APIC in xAPIC mode
In reply to Sean ChristophersonImplement prepare/restore logic for the nSVM/nNPT APIC MMIO passthrough tests to ensure the CPU is actually running with xAPIC enabled. As is, the test is effectively validating KVM's KVM_X86_QUIRK_LAPIC_MMIO_HOLE, or if x2AVIC is support, CPU behavior.
The latter (x2AVIC enabled) is especially problematic, as AMD CPUs appear to return '0' for xAPIC reads when x2AVIC is enabled. And because KVM disables/inhibits AVIC and x2AVIC when running L2, the divergence in behavior (KVM provies 0xffs, CPU provides 0s) results in test failures.
Opportunistically make the hardcoded APIC base pointer (eww) an unsigned long literal.
Note, svm_test.finished() is invoked *before* svm_test.succeeded(), i.e. restoring x2APIC (if it was enabled) must be done in the "check" code.
--- x86/svm_npt.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
x86/svm_npt.c +27 -0
@@ -1,3 +1,4 @@ +#include "apic.h" #include "svm.h" #include "vm.h" #include "alloc_page.h"
@@ -134,8 +135,27 @@ static bool npt_rw_pfwalk_check(struct svm_test *test) && (vmcb->control.exit_info_2 ==read_cr3 ()); } +static bool was_x2apic; + +static void npt_apic_prepare (void ) +{ + was_x2apic =is_x2apic_enabled (); + +if (was_x2apic) +reset_apic (); +} + +static void npt_apic_restore (void ) +{ +if (was_x2apic) +enable_x2apic (); + + was_x2apic = false; +} +static void npt_l1mmio_prepare (struct svm_test *test) { +npt_apic_prepare (); }u32 nested_apic_version1;
@@ -154,6 +174,9 @@ static bool npt_l1mmio_check(struct svm_test *test)volatile u32 *data = (volatile void *)(0xfee00030 );u32 lvr = *data; +/* Restore APIC state *after* reading LVR. */ +npt_apic_restore (); +return nested_apic_version1 == lvr && nested_apic_version2 == lvr; }
@@ -162,6 +185,8 @@ static void npt_rw_l1mmio_prepare(struct svm_test *test)u64 *pte; +npt_apic_prepare (); + pte =npt_get_pte (0xfee00080 ); *pte &= ~(1ULL <<1 );
@@ -180,6 +205,8 @@ static bool npt_rw_l1mmio_check(struct svm_test *test) *pte |= (1ULL <<1 ); +npt_apic_restore (); +return (vmcb->control.exit_code ==SVM_EXIT_NPF ) && (vmcb->control.exit_info_1 ==0x100000007ULL ); }
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Quoted 11 lines · Sean Christopherson
Fix a bug in the nNPT passthrough APIC (MMIO) test where it runs with x2APIC enabled. The test currently passes because KVM provides consistent behavior when the APIC is accessed via MMIO in x2APIC mode (see
KVM_X86_QUIRK_LAPIC_MMIO_HOLE), and because it runs with an in-kernel PIT, which inhibits x2AVIC.But with a split IRQCHIP, i.e. no preconfigured PIT, x2AVIC gets enabled and the test fails because hardware doesn't provide the exact same behavior as KVM (returns 0s instead of FFs).
[...]
Applied to kvm-x86 next, thanks!
[1/2] x86: apic: Move helpers for querying APIC state to library code
https://github.com/kvm-x86/kvm-unit-tests/commit/d427851a1e91 [2/2] x86: nSVM: Ensure APIC MMIO tests run with APIC in xAPIC mode https://github.com/kvm-x86/kvm-unit-tests/commit/63a180cba760