x86: Add testcases for x2APIC MSRs
Sean Christopherson
reviewercover x86: Add testcases for x2APIC MSRs
Add tests to verify that RDMSR and WRMSR to x2APIC MSRs behave according to Intel's architecture, e.g. that writes to read-only MSRs and reads to write-only MSRs #GP, accesses to non-existent MSRs #GP, etc...
Many of the testcases fail without the associated KVM fixes:
https://lore.kernel.org/all/[email protected]
Sean Christopherson (3):
x86/msr: Skip built-in testcases if user provides custom MSR+value to
test
x86/apic: Refactor x2APIC reg helper to provide exact semantics x86/msr: Add testcases for x2APIC MSRs
lib/x86/apic.h | 59 ++++++++++--- x86/msr.c | 229 ++++++++++++++++++++++++++++++++---------------- x86/vmx_tests.c | 6 +- 3 files changed, 203 insertions(+), 91 deletions(-)
base-commit: e11a0e2f881d7bc038f44d8d4f99b2d55a01bc4e
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerPATCH 1/3 x86/msr: Skip built-in testcases if user provides custom MSR+value to test
Skip the built-in MSR testcases if the user provides a custom MSR+value to test. If the user is asking to test a specific MSR+value, they likely don't want to wait for the test to burn though a pile of MCE MSRs.
--- x86/msr.c | 170 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 74 deletions(-)
x86/msr.c +96 -74
@@ -120,88 +120,110 @@ static void test_msr(struct msr_info *msr, bool is_64bit_host) } } -int main (int ac,char **av) +static void test_custom_msr (int ac,char **av) +{ +bool is_64bit_host =this_cpu_has (X86_FEATURE_LM ); +char msr_name[32 ]; +int index =strtoul (av[1 ],NULL ,0x10 ); +snprintf (msr_name,sizeof (msr_name),"MSR:0x%x" , index); + +struct msr_info msr = { + .index = index, + .name = msr_name, + .value =strtoull (av[2 ],NULL ,0x10 ) + }; +test_msr (&msr, is_64bit_host); +} + +static void test_misc_msrs (void ) +{ +bool is_64bit_host =this_cpu_has (X86_FEATURE_LM ); +int i; + +for (i =0 ; i <ARRAY_SIZE (msr_info); i++) +test_msr (&msr_info[i], is_64bit_host); +} + +static void test_mce_msrs (void ) {bool is_64bit_host =this_cpu_has (X86_FEATURE_LM );unsigned int nr_mce_banks;char msr_name[32 ];int i; + nr_mce_banks =rdmsr (MSR_IA32_MCG_CAP ) &0xff ; +for (i =0 ; i < nr_mce_banks; i++) { +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_CTL" , i); +test_msr_rw (MSR_IA32_MCx_CTL (i), msr_name,0 ); +test_msr_rw (MSR_IA32_MCx_CTL (i), msr_name,-1ull ); +test_wrmsr_fault (MSR_IA32_MCx_CTL (i), msr_name,NONCANONICAL ); + +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_STATUS" , i); +test_msr_rw (MSR_IA32_MCx_STATUS (i), msr_name,0 ); +/* +* STATUS MSRs can only be written with '0' (to clear the MSR), +* except on AMD-based systems with bit 18 set in MSR_K7_HWCR. +* That bit is not architectural and should not be set by +* default by KVM or by the VMM (though this might fail if run +* on bare metal). +*/ +test_wrmsr_fault (MSR_IA32_MCx_STATUS (i), msr_name,1 ); + +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_ADDR" , i); +test_msr_rw (MSR_IA32_MCx_ADDR (i), msr_name,0 ); +test_msr_rw (MSR_IA32_MCx_ADDR (i), msr_name,-1ull ); +/* +* The ADDR is a physical address, and all bits are writable on +* 64-bit hosts. Don't test the negative case, as KVM doesn't +* enforce checks on bits 63:36 for 32-bit hosts. The behavior +* depends on the underlying hardware, e.g. a 32-bit guest on a +* 64-bit host may observe 64-bit values in the ADDR MSRs. +*/ +if (is_64bit_host) +test_msr_rw (MSR_IA32_MCx_ADDR (i), msr_name,NONCANONICAL ); + +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_MISC" , i); +test_msr_rw (MSR_IA32_MCx_MISC (i), msr_name,0 ); +test_msr_rw (MSR_IA32_MCx_MISC (i), msr_name,-1ull ); +test_msr_rw (MSR_IA32_MCx_MISC (i), msr_name,NONCANONICAL ); + } + +/* +* The theoretical maximum number of MCE banks is 32 (on Intel CPUs, +* without jumping to a new base address), as the last unclaimed MSR is +* 0x479; 0x480 begins the VMX MSRs. Verify accesses to theoretically +* legal, unsupported MSRs fault. +*/ +for (i = nr_mce_banks; i <32 ; i++) { +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_CTL" , i); +test_rdmsr_fault (MSR_IA32_MCx_CTL (i), msr_name); +test_wrmsr_fault (MSR_IA32_MCx_CTL (i), msr_name,0 ); + +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_STATUS" , i); +test_rdmsr_fault (MSR_IA32_MCx_STATUS (i), msr_name); +test_wrmsr_fault (MSR_IA32_MCx_STATUS (i), msr_name,0 ); + +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_ADDR" , i); +test_rdmsr_fault (MSR_IA32_MCx_ADDR (i), msr_name); +test_wrmsr_fault (MSR_IA32_MCx_ADDR (i), msr_name,0 ); + +snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_MISC" , i); +test_rdmsr_fault (MSR_IA32_MCx_MISC (i), msr_name); +test_wrmsr_fault (MSR_IA32_MCx_MISC (i), msr_name,0 ); + } +} + +int main (int ac,char **av) +{ +/* +* If the user provided an MSR+value, test exactly that and skip all +* built-in testcases. +*/ if (ac ==3 ) { -int index =strtoul (av[1 ],NULL ,0x10 ); -snprintf (msr_name,sizeof (msr_name),"MSR:0x%x" , index); - -struct msr_info msr = { - .index = index, - .name = msr_name, - .value =strtoull (av[2 ],NULL ,0x10 ) - }; -test_msr (&msr, is_64bit_host); +test_custom_msr (ac, av); }else { -for (i =0 ; i <ARRAY_SIZE (msr_info); i++) -test_msr (&msr_info[i], is_64bit_host); - - nr_mce_banks =rdmsr (MSR_IA32_MCG_CAP ) &0xff ; -for (i =0 ; i < nr_mce_banks; i++) { -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_CTL" , i); -test_msr_rw (MSR_IA32_MCx_CTL (i), msr_name,0 ); -test_msr_rw (MSR_IA32_MCx_CTL (i), msr_name,-1ull ); -test_wrmsr_fault (MSR_IA32_MCx_CTL (i), msr_name,NONCANONICAL ); - -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_STATUS" , i); -test_msr_rw (MSR_IA32_MCx_STATUS (i), msr_name,0 ); -/* -* STATUS MSRs can only be written with '0' (to clear -* the MSR), except on AMD-based systems with bit 18 -* set in MSR_K7_HWCR. That bit is not architectural -* and should not be set by default by KVM or by the -* VMM (though this might fail if run on bare metal). -*/ -test_wrmsr_fault (MSR_IA32_MCx_STATUS (i), msr_name,1 ); - -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_ADDR" , i); -test_msr_rw (MSR_IA32_MCx_ADDR (i), msr_name,0 ); -test_msr_rw (MSR_IA32_MCx_ADDR (i), msr_name,-1ull ); -/* -* The ADDR is a physical address, and all bits are -* writable on 64-bit hosts. Don't test the negative -* case, as KVM doesn't enforce checks on bits 63:36 -* for 32-bit hosts. The behavior depends on the -* underlying hardware, e.g. a 32-bit guest on a 64-bit -* host may observe 64-bit values in the ADDR MSRs. -*/ -if (is_64bit_host) -test_msr_rw (MSR_IA32_MCx_ADDR (i), msr_name,NONCANONICAL ); - -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_MISC" , i); -test_msr_rw (MSR_IA32_MCx_MISC (i), msr_name,0 ); -test_msr_rw (MSR_IA32_MCx_MISC (i), msr_name,-1ull ); -test_msr_rw (MSR_IA32_MCx_MISC (i), msr_name,NONCANONICAL ); - } - -/* -* The theoretical maximum number of MCE banks is 32 (on Intel -* CPUs, without jumping to a new base address), as the last -* unclaimed MSR is 0x479; 0x480 begins the VMX MSRs. Verify -* accesses to theoretically legal, unsupported MSRs fault. -*/ -for (i = nr_mce_banks; i <32 ; i++) { -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_CTL" , i); -test_rdmsr_fault (MSR_IA32_MCx_CTL (i), msr_name); -test_wrmsr_fault (MSR_IA32_MCx_CTL (i), msr_name,0 ); - -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_STATUS" , i); -test_rdmsr_fault (MSR_IA32_MCx_STATUS (i), msr_name); -test_wrmsr_fault (MSR_IA32_MCx_STATUS (i), msr_name,0 ); - -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_ADDR" , i); -test_rdmsr_fault (MSR_IA32_MCx_ADDR (i), msr_name); -test_wrmsr_fault (MSR_IA32_MCx_ADDR (i), msr_name,0 ); - -snprintf (msr_name,sizeof (msr_name),"MSR_IA32_MC%u_MISC" , i); -test_rdmsr_fault (MSR_IA32_MCx_MISC (i), msr_name); -test_wrmsr_fault (MSR_IA32_MCx_MISC (i), msr_name,0 ); - } +test_misc_msrs (); +test_mce_msrs (); }return report_summary ();
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerPATCH 2/3 x86/apic: Refactor x2APIC reg helper to provide exact semantics
In reply to Sean ChristophersonRefactor x2apic_reg_reserved() into get_x2apic_reg_semantics() and have it provide the semantics for all registers. The full semantics will be used by the MSR test to verify KVM correctly emulates all x2APIC MSRs.
No functional change intended.
--- lib/x86/apic.h | 59 ++++++++++++++++++++++++++++++++++++++----------- x86/vmx_tests.c | 6 ++--- 2 files changed, 48 insertions(+), 17 deletions(-)
lib/x86/apic.h +46 -13
@@ -1,6 +1,7 @@#ifndef _X86_APIC_H_#define _X86_APIC_H_ +#include <bitops.h> #include <stdint.h> #include "apic-defs.h"
@@ -74,24 +75,56 @@ static inline bool apic_lvt_entry_supported(int idx)return GET_APIC_MAXLVT (apic_read (APIC_LVR )) >= idx; } -static inline bool x2apic_reg_reserved (u32 reg) +enum x2apic_reg_semantics { +X2APIC_INVALID =0 , +X2APIC_READABLE =BIT (0 ), +X2APIC_WRITABLE =BIT (1 ), +X2APIC_RO =X2APIC_READABLE , +X2APIC_WO =X2APIC_WRITABLE , +X2APIC_RW =X2APIC_READABLE |X2APIC_WRITABLE , +}; + +static inline enum x2apic_reg_semantics get_x2apic_reg_semantics (u32 reg) { +assert (!(reg &0xf )); +switch (reg) { - case 0x000 ... 0x010: - case 0x040 ... 0x070: -case 0x090 : -case 0x0c0 : -case 0x0e0 : - case 0x290 ... 0x2e0: -case 0x310 : - case 0x3a0 ... 0x3d0: -case 0x3f0 : -return true; +case APIC_ID : +case APIC_LVR : +case APIC_PROCPRI : +case APIC_LDR : + case APIC_ISR ... APIC_ISR + 0x70: + case APIC_TMR ... APIC_TMR + 0x70: + case APIC_IRR ... APIC_IRR + 0x70: +case APIC_TMCCT : +return X2APIC_RO ; +case APIC_TASKPRI : +case APIC_SPIV : +case APIC_ESR : +case APIC_ICR : +case APIC_LVTT : +case APIC_LVTTHMR : +case APIC_LVTPC : +case APIC_LVT0 : +case APIC_LVT1 : +case APIC_LVTERR : +case APIC_TMICT : +case APIC_TDCR : +return X2APIC_RW ; +case APIC_EOI : +case APIC_SELF_IPI : +return X2APIC_WO ;case APIC_CMCI : -return !apic_lvt_entry_supported (6 ); +if (apic_lvt_entry_supported (6 )) +return X2APIC_RW ; +break ; +case APIC_RRR : +case APIC_DFR : +case APIC_ICR2 :default : -return false; +break ; } +return X2APIC_INVALID ; }#endif
x86/vmx_tests.c +2 -4
@@ -6191,9 +6191,7 @@ static void virt_x2apic_mode_rd_expectation( bool apic_register_virtualization, bool virtual_interrupt_delivery, struct virt_x2apic_mode_expectation *expectation) { - bool readable = - !x2apic_reg_reserved (reg) && - reg !=APIC_EOI ; + enum x2apic_reg_semantics semantics = get_x2apic_reg_semantics(reg); expectation->rd_exit_reason =VMX_VMCALL ; expectation->virt_fn = virt_x2apic_mode_identity;
@@ -6209,7 +6207,7 @@ static void virt_x2apic_mode_rd_expectation( expectation->rd_val =MAGIC_VAL_1 ; expectation->virt_fn = virt_x2apic_mode_nibble1; expectation->rd_behavior =X2APIC_ACCESS_VIRTUALIZED ; - }else if (!disable_x2apic && readable) { + }else if (!disable_x2apic && (semantics &X2APIC_READABLE )) { expectation->rd_val =apic_read (reg); expectation->rd_behavior =X2APIC_ACCESS_PASSED_THROUGH ; }else {
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerPATCH 3/3 x86/msr: Add testcases for x2APIC MSRs
In reply to Sean ChristophersonVerify that reads and writes to x2APIC MSRs #GP when x2APIC is disabled, and that reads and writes honor KVM's emulation (which follows Intel behavior) when x2APIC is enabled. E.g. verify that writes to read-only registers #GP, reads to write-onliy registers #GP, etc...
Write '0' to write-only registers to play nice with AMD's more restrictive behavior (Intel doesn't care what value is written).
Note, the x2APIC enabled testcases will likely fail if run on AMD bare metal as the KVM doesn't emulate registers above self-IPI, i.e. MSRs that are expected to #GP are presumably handled by AMD hardware.
--- x86/msr.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
x86/msr.c +59 -0
@@ -1,6 +1,7 @@/* msr tests */ #include "libcflat.h" +#include "apic.h" #include "processor.h" #include "msr.h" #include <stdlib.h>
@@ -213,6 +214,63 @@ static void test_mce_msrs(void) } } +static void __test_x2apic_msrs (bool x2apic_enabled) +{ +enum x2apic_reg_semantics semantics; +unsigned int index, i; +char msr_name[32 ]; + +for (i =0 ; i <0x1000 ; i +=0x10 ) { + index =x2apic_msr (i); +snprintf (msr_name,sizeof (msr_name),"x2APIC MSR 0x%x" , index); + +if (x2apic_enabled) + semantics =get_x2apic_reg_semantics (i); +else + semantics =X2APIC_INVALID ; + +if (!(semantics &X2APIC_WRITABLE )) +test_wrmsr_fault (index, msr_name,0 ); + +if (!(semantics &X2APIC_READABLE )) +test_rdmsr_fault (index, msr_name); + +/* +* Except for ICR, the only 64-bit x2APIC register, bits 64:32 +* are reserved. ICR is testable if x2APIC is disabled. +*/ +if (!x2apic_enabled || i !=APIC_ICR ) +test_wrmsr_fault (index, msr_name,-1ull ); + +/* Bits 31:8 of self-IPI are reserved. */ +if (i ==APIC_SELF_IPI ) { +test_wrmsr_fault (index,"x2APIC Self-IPI" ,0x100 ); +test_wrmsr_fault (index,"x2APIC Self-IPI" ,0xff00 ); +test_wrmsr_fault (index,"x2APIC Self-IPI" ,0xff000000ull ); + } + +if (semantics ==X2APIC_RW ) +__test_msr_rw (index, msr_name,0 ,-1ull ); +else if (semantics ==X2APIC_WO ) +wrmsr (index,0 ); +else if (semantics ==X2APIC_RO ) +report (!(rdmsr (index) >>32 ), +"Expected bits 63:32 == 0 for '%s'" , msr_name); + } +} + +static void test_x2apic_msrs (void ) +{ +reset_apic (); + +__test_x2apic_msrs (false); + +if (!enable_x2apic ()) +return ; + +__test_x2apic_msrs (true); +} +int main (int ac,char **av) {/*
@@ -224,6 +282,7 @@ int main(int ac, char **av) }else {test_misc_msrs ();test_mce_msrs (); +test_x2apic_msrs (); }return report_summary ();
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Quoted 8 lines · Sean Christopherson
Add tests to verify that RDMSR and WRMSR to x2APIC MSRs behave according to Intel's architecture, e.g. that writes to read-only MSRs and reads to write-only MSRs #GP, accesses to non-existent MSRs #GP, etc...
Many of the testcases fail without the associated KVM fixes:
https://lore.kernel.org/all/[email protected][...]
Applied to kvm-x86 next, thanks!
[1/3] x86/msr: Skip built-in testcases if user provides custom MSR+value to test
https://github.com/kvm-x86/kvm-unit-tests/commit/7fb714f76d70 [2/3] x86/apic: Refactor x2APIC reg helper to provide exact semantics https://github.com/kvm-x86/kvm-unit-tests/commit/14432cc362f1 [3/3] x86/msr: Add testcases for x2APIC MSRs https://github.com/kvm-x86/kvm-unit-tests/commit/a192b5c85e75