x86: Add test cases for LAM
v2 cover x86: Add test cases for LAM
Intel Linear-address masking (LAM) [1], modifies the checking that is applied to *64-bit* linear addresses, allowing software to use of the untranslated address bits for metadata.
The patch series add test cases for LAM:
Patch 1 makes change to HOST_CR3 tests in vmx.
If LAM is supported, VM entry allows CR3.LAM_U48 (bit 62) and CR3.LAM_U57 (bit 61) to be set in CR3 field. Change the test result expectations when setting CR3.LAM_U48 or CR3.LAM_U57 on vmlaunch tests when LAM is supported.
Patch 2~4 add test cases for LAM supervisor mode and user mode, including:
- For supervisor mode
CR4.LAM_SUP toggle
Memory/MMIO access with tagged pointer
INVLPG
INVPCID
INVVPID (also used to cover VMX instruction vmexit path)
- For user mode
CR3 LAM bits toggle
Memory/MMIO access with tagged pointer
[1] Intel ISE https://cdrdv2.intel.com/v1/dl/getContent/671368
Chapter Linear Address Masking (LAM)
---
Changelog
v1 --> v2:
Add cases to test INVLPG, INVPCID, INVVPID with LAM_SUP Add cases to test LAM_{U48,U57}
Binbin Wu (3):
x86: Allow setting of CR3 LAM bits if LAM supported
x86: Add test cases for LAM_{U48,U57}
x86: Add test case for INVVPID with LAM
Robert Hoo (1):
x86: Add test case for LAM_SUP
lib/x86/processor.h | 7 + x86/Makefile.x86_64 | 1 + x86/lam.c | 340 ++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 10 ++ x86/vmx_tests.c | 79 +++++++++- 5 files changed, 436 insertions(+), 1 deletion(-) create mode 100644 x86/lam.c
base-commit: e3c5c3ef2524c58023073c0fadde2e8ae3c04ec6
Signature · 1 line
2.25.1
PATCH v2 1/4 x86: Allow setting of CR3 LAM bits if LAM supported
If LAM is supported, VM entry allows CR3.LAM_U48 (bit 62) and CR3.LAM_U57 (bit 61) to be set in CR3 field.
Change the test result expectations when setting CR3.LAM_U48 or CR3.LAM_U57 on vmlaunch tests when LAM is supported.
--- lib/x86/processor.h | 2 ++ x86/vmx_tests.c | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-)
lib/x86/processor.h +2 -0
@@ -55,6 +55,8 @@#define X86_CR0_PG BIT(X86_CR0_PG_BIT)#define X86_CR3_PCID_MASK GENMASK(11, 0) +#define X86_CR3_LAM_U57_BIT (61) +#define X86_CR3_LAM_U48_BIT (62)#define X86_CR4_VME_BIT (0)#define X86_CR4_VME BIT(X86_CR4_VME_BIT)
x86/vmx_tests.c +5 -1
@@ -7000,7 +7000,11 @@ static void test_host_ctl_regs(void) cr3 = cr3_saved | (1ul << i);vmcs_write (HOST_CR3 , cr3);report_prefix_pushf ("HOST_CR3 %lx" , cr3); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +if (this_cpu_has (X86_FEATURE_LAM ) && + ((i==X86_CR3_LAM_U57_BIT ) || (i==X86_CR3_LAM_U48_BIT ))) +test_vmx_vmlaunch (0 ); +else +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD );report_prefix_pop (); }
Signature · 1 line
2.25.1
Re: [kvm-unit-tests PATCH v2 1/4] x86: Allow setting of CR3 LAM bits if LAM supported
Quoted 7 lines · Binbin Wu
If LAM is supported, VM entry allows CR3.LAM_U48 (bit 62) and CR3.LAM_U57 (bit 61) to be set in CR3 field.
Change the test result expectations when setting CR3.LAM_U48 or CR3.LAM_U57 on vmlaunch tests when LAM is supported.
Signed-off-by Binbin Wu <[email protected]>
and two nits below:
Quoted 23 lines
---
lib/x86/processor.h| 2 ++x86/vmx_tests.c| 6 +++++-2 files changed, 7 insertions(+), 1 deletion(-)lib/x86/processor.h +2
-0@@ -55,6 +55,8 @@#define X86_CR0_PG BIT(X86_CR0_PG_BIT)#define X86_CR3_PCID_MASK GENMASK(11, 0) +#define X86_CR3_LAM_U57_BIT (61) +#define X86_CR3_LAM_U48_BIT (62)#define X86_CR4_VME_BIT (0)#define X86_CR4_VME BIT(X86_CR4_VME_BIT)x86/vmx_tests.c +0
-0
x86/vmx_tests.c static void test_host_ctl_regs(void) · line 7003
Quoted 5 lines
x86/vmx_tests.c +1
-1@@ -7000,7 +7000,11 @@ static void test_host_ctl_regs(void) cr3 = cr3_saved | (1ul << i);vmcs_write (HOST_CR3 , cr3);report_prefix_pushf ("HOST_CR3 %lx" , cr3); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); + if (this_cpu_has(X86_FEATURE_LAM) &&
Nit: X86_FEATURE_LAM should be defined in this patch (instead of patch 2).
x86/vmx_tests.c static void test_host_ctl_regs(void) · line 7004
Quoted 1 line
x86/vmx_tests.c +1
-0+ ((i==X86_CR3_LAM_U57_BIT) || (i==X86_CR3_LAM_U48_BIT)))
Nit: spaces are needed around "==" i.e.,
((i == X86_CR3_LAM_U57_BIT) || (i == X86_CR3_LAM_U48_BIT)))
Quoted 9 lines
x86/vmx_tests.c +3
-0+test_vmx_vmlaunch (0 ); + else +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD );report_prefix_pop (); }Signature · 1 line
2.25.1
Re: [kvm-unit-tests PATCH v2 1/4] x86: Allow setting of CR3 LAM bits if LAM supported
Quoted 11 lines · Chao Gao
Quoted 7 lines · Binbin Wu
If LAM is supported, VM entry allows CR3.LAM_U48 (bit 62) and CR3.LAM_U57 (bit 61) to be set in CR3 field.
Change the test result expectations when setting CR3.LAM_U48 or CR3.LAM_U57 on vmlaunch tests when LAM is supported.
Signed-off-by Binbin Wu <[email protected]>Reviewed-by Chao Gao <[email protected]>and two nits below:
Thanks, will update it.
Quoted 45 lines
Quoted 24 lines
---
lib/x86/processor.h| 2 ++x86/vmx_tests.c| 6 +++++-2 files changed, 7 insertions(+), 1 deletion(-)lib/x86/processor.h +2
-0@@ -55,6 +55,8 @@#define X86_CR0_PG BIT(X86_CR0_PG_BIT)#define X86_CR3_PCID_MASK GENMASK(11, 0) +#define X86_CR3_LAM_U57_BIT (61) +#define X86_CR3_LAM_U48_BIT (62)#define X86_CR4_VME_BIT (0)#define X86_CR4_VME BIT(X86_CR4_VME_BIT)x86/vmx_tests.c +0
-0x86/vmx_tests.c static void test_host_ctl_regs(void) · line 7003
Quoted 5 lines
x86/vmx_tests.c +1
-1@@ -7000,7 +7000,11 @@ static void test_host_ctl_regs(void) cr3 = cr3_saved | (1ul << i);vmcs_write (HOST_CR3 , cr3);report_prefix_pushf ("HOST_CR3 %lx" , cr3); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); + if (this_cpu_has(X86_FEATURE_LAM) &&Nit:
X86_FEATURE_LAMshould be defined in this patch (instead of patch 2).x86/vmx_tests.c static void test_host_ctl_regs(void) · line 7004
Quoted 1 line
x86/vmx_tests.c +1
-0+ ((i==X86_CR3_LAM_U57_BIT) || (i==X86_CR3_LAM_U48_BIT)))Nit: spaces are needed around "==" i.e.,
((i == X86_CR3_LAM_U57_BIT) || (i == X86_CR3_LAM_U48_BIT)))Quoted 9 lines
x86/vmx_tests.c +3
-0+test_vmx_vmlaunch (0 ); + else +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD );report_prefix_pop (); }Signature · 1 line
2.25.1
PATCH v2 2/4 x86: Add test case for LAM_SUP
In reply to Binbin WuFrom: Robert Hoo <[email protected]>
This unit test covers:
1. CR4.LAM_SUP toggle has expected behavior according to LAM status.
2. Memory access (here is strcpy() for test example) with supervisor mode address containing LAM meta data, behave as expected per LAM status.
3. MMIO memory access with supervisor mode address containing LAM meta data, behave as expected per LAM status.
4. INVLPG memory operand doens't contain LAM meta data, if the address is non-canonical form then the INVLPG is the same as a NOP (no #GP).
5. INVPCID memory operand (descriptor pointer) could contain LAM meta data, however, the address in the descriptor should be canonical.
In x86/unittests.cfg, add 2 test cases/guest conf, with and without LAM.
LAM feature spec: https://cdrdv2.intel.com/v1/dl/getContent/671368, Chap 7 LINEAR ADDRESS MASKING (LAM)
--- lib/x86/processor.h | 3 + x86/Makefile.x86_64 | 1 + x86/lam.c | 296 ++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 10 ++ 4 files changed, 310 insertions(+) create mode 100644 x86/lam.c
lib/x86/processor.h +3 -0
@@ -107,6 +107,8 @@#define X86_CR4_CET BIT(X86_CR4_CET_BIT)#define X86_CR4_PKS_BIT (24)#define X86_CR4_PKS BIT(X86_CR4_PKS_BIT) +#define X86_CR4_LAM_SUP_BIT (28) +#define X86_CR4_LAM_SUP BIT(X86_CR4_LAM_SUP_BIT)#define X86_EFLAGS_CF_BIT (0)#define X86_EFLAGS_CF BIT(X86_EFLAGS_CF_BIT)
@@ -250,6 +252,7 @@ static inline bool is_intel(void)#define X86_FEATURE_SPEC_CTRL (CPUID(0x7, 0, EDX, 26))#define X86_FEATURE_ARCH_CAPABILITIES (CPUID(0x7, 0, EDX, 29))#define X86_FEATURE_PKS (CPUID(0x7, 0, ECX, 31)) +#define X86_FEATURE_LAM (CPUID(0x7, 1, EAX, 26))/* * Extended Leafs, a.k.a. AMD defined
x86/Makefile.x86_64 +1 -0
@@ -34,6 +34,7 @@ tests += $(TEST_DIR)/rdpru.$(exe) tests += $(TEST_DIR)/pks.$(exe) tests += $(TEST_DIR)/pmu_lbr.$(exe) tests += $(TEST_DIR)/pmu_pebs.$(exe) +tests += $(TEST_DIR)/lam.$(exe) ifeq ($(CONFIG_EFI),y) tests += $(TEST_DIR)/amd_sev.$(exe)
x86/lam.c new file +296 -0
296 lines not on this page · raw message
x86/unittests.cfg +10 -0
@@ -478,3 +478,13 @@ file = cet.flat arch = x86_64 smp = 2 extra_params = -enable-kvm -m 2048 -cpu host + +[intel-lam] +file = lam.flat +arch = x86_64 +extra_params = -enable-kvm -cpu host + +[intel-no-lam] +file = lam.flat +arch = x86_64 +extra_params = -enable-kvm -cpu host,-lam
Signature · 1 line
2.25.1
PATCH v2 3/4 x86: Add test cases for LAM_{U48,U57}
In reply to Binbin WuThis unit test covers:
1. CR3 LAM bits toggle has expected behavior according to LAM status.
2. Memory access using strcpy() with user mode address containing LAM meta data, behave as expected per LAM status.
3. MMIO memory access with user mode address containing LAM meta data, behave as expected per LAM status.
--- lib/x86/processor.h | 2 ++ x86/lam.c | 46 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-)
lib/x86/processor.h +2 -0
@@ -56,7 +56,9 @@#define X86_CR3_PCID_MASK GENMASK(11, 0)#define X86_CR3_LAM_U57_BIT (61) +#define X86_CR3_LAM_U57 BIT_ULL(X86_CR3_LAM_U57_BIT)#define X86_CR3_LAM_U48_BIT (62) +#define X86_CR3_LAM_U48 BIT_ULL(X86_CR3_LAM_U48_BIT)#define X86_CR4_VME_BIT (0)#define X86_CR4_VME BIT(X86_CR4_VME_BIT)
x86/lam.c +45 -1
73 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 4/4 x86: Add test case for INVVPID with LAM
In reply to Binbin WuWhen LAM is on, the linear address of INVVPID operand can contain metadata, and the linear address in the INVVPID descriptor can caontain metadata.
The added cases use tagged descriptor address or/and tagged target invalidation address to make sure the behaviors are expected when LAM is on.
Also, INVVPID cases can be used as the common test cases for VMX instruction vmexits.
--- x86/vmx_tests.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
x86/vmx_tests.c +73 -0
@@ -3225,6 +3225,78 @@ static void invvpid_test_not_in_vmx_operation(void)TEST_ASSERT (!vmx_on ()); } +#define LAM57_BITS 6 +#define LAM48_BITS 15 +#define LAM57_MASK GENMASK_ULL(62, 57) +#define LAM48_MASK GENMASK_ULL(62, 48) + +/* According to LAM mode, set metadata in high bits */ +static u64 set_metadata (u64 src,unsigned long lam) +{ +u64 metadata; + +switch (lam) { +case LAM57_BITS :/* Set metadata in bits 62:57 */ + metadata = (NONCANONICAL & ((1UL <<LAM57_BITS ) -1 )) <<57 ; + metadata |= (src & ~(LAM57_MASK )); +break ; +case LAM48_BITS :/* Set metadata in bits 62:48 */ + metadata = (NONCANONICAL & ((1UL <<LAM48_BITS ) -1 )) <<48 ; + metadata |= (src & ~(LAM48_MASK )); +break ; +default : + metadata = src; +break ; + } + +return metadata; +} + +static void invvpid_test_lam (void ) +{ +void *vaddr; +struct invvpid_operand *operand; +int lam_bits =LAM48_BITS ; +bool fault; + +if (!this_cpu_has (X86_FEATURE_LAM )) { +report_skip ("LAM is not supported, skip INVVPID with LAM" ); +return ; + } + +if (this_cpu_has (X86_FEATURE_LA57 ) &&read_cr4 () &X86_CR4_LA57 ) + lam_bits =LAM57_BITS ; + + vaddr =alloc_vpage (); +install_page (current_page_table (),virt_to_phys (alloc_page ()), vaddr); + operand = (struct invvpid_operand *)vaddr; + operand->vpid =0xffff ; + operand->gla = (u64 )vaddr; + +write_cr4_safe (read_cr4 () |X86_CR4_LAM_SUP ); +if (!(read_cr4 () &X86_CR4_LAM_SUP )) { +report_skip ("Failed to enable LAM_SUP" ); +return ; + } + + operand = (struct invvpid_operand *)vaddr; + operand->gla =set_metadata (operand->gla, lam_bits); + fault =test_for_exception (GP_VECTOR , ds_invvpid, operand); +report (!fault,"INVVPID (LAM on): untagged pointer + tagged addr" ); + + operand = (struct invvpid_operand *)set_metadata ((u64 )operand, lam_bits); + operand->gla = (u64 )vaddr; + fault =test_for_exception (GP_VECTOR , ds_invvpid, operand); +report (!fault,"INVVPID (LAM on): tagged pointer + untagged addr" ); + + operand = (struct invvpid_operand *)set_metadata ((u64 )operand, lam_bits); + operand->gla =set_metadata (operand->gla, lam_bits); + fault =test_for_exception (GP_VECTOR , ds_invvpid, operand); +report (!fault,"INVVPID (LAM on): tagged pointer + tagged addr" ); + +write_cr4_safe (read_cr4 () & ~X86_CR4_LAM_SUP ); +} +/* * This does not test real-address mode, virtual-8086 mode, protected mode, * or CPL > 0.
@@ -3282,6 +3354,7 @@ static void invvpid_test(void)invvpid_test_pf ();invvpid_test_compatibility_mode ();invvpid_test_not_in_vmx_operation (); +invvpid_test_lam (); }/*
Signature · 1 line
2.25.1