x86: Add test cases for LAM
v4 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 KVM LAM:
Patch 1 makes change to allow setting of CR3 LAM bits in vmlaunch tests. 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
v3 --> v4:
Remove an extra space in patch 1.
Remove an unnecessary local variable in patch 2.
Add some comments about the linear address used for LAM tests in patch 2 and patch 3. Add a static assert to check the physical address range for LAM user mode tests in patch 3. Remove the unnecessary identical mapping code in patch 3.
Move common definitions and helper function to header file in patch 2.
LAM test of INVVPID is tested only when individual-address invalidation is supported in patch 4. Add reviewed-by from Chao Gao in patch 2~4.
v2 --> v3:
Simplify the implementation of set_metadata() (Suggested by Chao Gao) Move definition of X86_FEATURE_LAM to patch 1.
Remove strcpy case and unify memory & MMIO address access using MOV instruction. Some code cleanups.
Add reviewed-by from Chao Gao in patch 1.
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 | 15 +++ x86/Makefile.x86_64 | 1 + x86/lam.c | 319 ++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 10 ++ x86/vmx_tests.c | 58 +++++++- 5 files changed, 401 insertions(+), 2 deletions(-) create mode 100644 x86/lam.c
base-commit: 02d8befe99f8205d4caea402d8b0800354255681
Signature · 1 line
2.25.1
PATCH v4 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 | 3 +++ x86/vmx_tests.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-)
lib/x86/processor.h +3 -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)
@@ -249,6 +251,7 @@ static inline bool is_intel(void)#define X86_FEATURE_FLUSH_L1D (CPUID(0x7, 0, EDX, 28))#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/vmx_tests.c +5 -1
@@ -6998,7 +6998,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
PATCH v4 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 toggles.
2. Memory & MMIO access with supervisor mode address with LAM metadata.
3. INVLPG memory operand doesn't contain LAM meta data, if the address is non-canonical form then the INVLPG is the same as a NOP (no #GP).
4. 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 | 10 ++ x86/Makefile.x86_64 | 1 + x86/lam.c | 243 ++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 10 ++ 4 files changed, 264 insertions(+) create mode 100644 x86/lam.c
lib/x86/processor.h +10 -0
@@ -8,6 +8,14 @@#include <stdint.h> #define NONCANONICAL 0xaaaaaaaaaaaaaaaaull +#define LAM57_MASK GENMASK_ULL(62, 57) +#define LAM48_MASK GENMASK_ULL(62, 48) + +/* Set metadata with non-canonical pattern in mask bits of a linear address */ +static inline u64 set_la_non_canonical (u64 src,u64 mask) +{ +return (src & ~mask) | (NONCANONICAL & mask); +}#ifdef __x86_64__# define R "r"
@@ -107,6 +115,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)
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 +243 -0
243 lines not on this page · raw message
x86/unittests.cfg +10 -0
@@ -492,3 +492,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 v4 3/4 x86: Add test cases for LAM_{U48,U57}
In reply to Binbin WuThis unit test covers:
1. CR3 LAM bits toggles.
2. Memory/MMIO access with user mode address containing LAM metadata.
--- lib/x86/processor.h | 2 ++ x86/lam.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
lib/x86/processor.h +2 -0
@@ -64,7 +64,9 @@ static inline u64 set_la_non_canonical(u64 src, u64 mask)#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 +76 -0
112 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 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 contain 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 | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-)
x86/vmx_tests.c +51 -1
@@ -3225,6 +3225,54 @@ static void invvpid_test_not_in_vmx_operation(void)TEST_ASSERT (!vmx_on ()); } +/* LAM applies to the target address inside the descriptor of invvpid */ +static void invvpid_test_lam (void ) +{ +void *vaddr; +struct invvpid_operand *operand; +u64 lam_mask =LAM48_MASK ; +bool fault; + +if (!this_cpu_has (X86_FEATURE_LAM )) { +report_skip ("LAM is not supported, skip INVVPID with LAM" ); +return ; + } +write_cr4_safe (read_cr4 () |X86_CR4_LAM_SUP ); + +if (this_cpu_has (X86_FEATURE_LA57 ) &&read_cr4 () &X86_CR4_LA57 ) + lam_mask =LAM57_MASK ; + + vaddr =alloc_vpage (); +install_page (current_page_table (),virt_to_phys (alloc_page ()), vaddr); +/* +* Since the stack memory address in KUT doesn't follow kernel address +* space partition rule, reuse the memory address for descriptor and +* the target address in the descriptor of invvpid. +*/ + operand = (struct invvpid_operand *)vaddr; + operand->vpid =0xffff ; + operand->gla = (u64 )vaddr; + + operand = (struct invvpid_operand *)vaddr; + operand->gla =set_la_non_canonical (operand->gla, lam_mask); + fault =test_for_exception (GP_VECTOR , ds_invvpid, operand); +report (!fault,"INVVPID (LAM on): untagged pointer + tagged addr" ); + + operand = (struct invvpid_operand *)set_la_non_canonical ((u64 )operand, + lam_mask); + 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_la_non_canonical ((u64 )operand, + lam_mask); + operand->gla =set_la_non_canonical (operand->gla, lam_mask); + 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.
@@ -3274,8 +3322,10 @@ static void invvpid_test(void)/* * The gla operand is only validated for single-address INVVPID. */ -if (types & (1u <<INVVPID_ADDR )) +if (types & (1u <<INVVPID_ADDR )) {try_invvpid (INVVPID_ADDR ,0xffff ,NONCANONICAL ); +invvpid_test_lam (); + }invvpid_test_gp ();invvpid_test_ss ();
Signature · 1 line
2.25.1
Re: [kvm-unit-tests v4 4/4] x86: Add test case for INVVPID with LAM
Quoted 58 lines · Binbin Wu
When LAM is on, the linear address of INVVPID operand can contain metadata, and the linear address in the INVVPID descriptor can contain 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.Signed-off-by Binbin Wu <[email protected]>Reviewed-by Chao Gao <[email protected]>--- x86/vmx_tests.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-)x86/vmx_tests.c +34
-0@@ -3225,6 +3225,54 @@ static void invvpid_test_not_in_vmx_operation(void)TEST_ASSERT (!vmx_on ()); } +/* LAM applies to the target address inside the descriptor of invvpid */ +static void invvpid_test_lam (void ) +{ +void *vaddr; +struct invvpid_operand *operand; +u64 lam_mask =LAM48_MASK ; +bool fault; + +if (!this_cpu_has (X86_FEATURE_LAM )) { +report_skip ("LAM is not supported, skip INVVPID with LAM" ); +return ; + } +write_cr4_safe (read_cr4 () |X86_CR4_LAM_SUP ); + +if (this_cpu_has (X86_FEATURE_LA57 ) &&read_cr4 () &X86_CR4_LA57 ) + lam_mask =LAM57_MASK ; + + vaddr =alloc_vpage (); +install_page (current_page_table (),virt_to_phys (alloc_page ()), vaddr); +/* +* Since the stack memory address in KUT doesn't follow kernel address +* space partition rule, reuse the memory address for descriptor and +* the target address in the descriptor of invvpid. +*/ + operand = (struct invvpid_operand *)vaddr; + operand->vpid =0xffff ; + operand->gla = (u64 )vaddr; + + operand = (struct invvpid_operand *)vaddr; + operand->gla =set_la_non_canonical (operand->gla, lam_mask); + fault =test_for_exception (GP_VECTOR , ds_invvpid, operand); +report (!fault,"INVVPID (LAM on): untagged pointer + tagged addr" ); + + operand = (struct invvpid_operand *)set_la_non_canonical((u64)operand,
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3271
Quoted 10 lines
x86/vmx_tests.c +10
-0+ lam_mask); + 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_la_non_canonical ((u64 )operand, + lam_mask); + operand->gla =set_la_non_canonical (operand->gla, lam_mask); + fault =test_for_exception (GP_VECTOR , ds_invvpid, operand); +report (!fault,"INVVPID (LAM on): tagged pointer + tagged addr" );
The test cases designed for invvpid with LAM is not right.
Will use two test cases to test invvpid when LAM is activated:
One to test with tagged operand expecting no #GP.
The other one to test with tagged target address inside the descriptor expecting failure and VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID set in VMX_INST_ERROR field of VMCS.
The new test code proposed as below:
....
operand = (struct invvpid_operand *)vaddr;
operand->vpid = 0xffff;
operand->gla = (u64)vaddr;
operand = (struct invvpid_operand *)set_la_non_canonical((u64)operand, lam_mask);
fault = test_for_exception(GP_VECTOR, ds_invvpid, operand); report(!fault, "INVVPID (LAM on): tagged operand");
/*
* LAM doesn't apply to the address inside the descriptor, expected
* failure and VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID set in
* VMX_INST_ERROR.
*/
try_invvpid(INVVPID_ADDR, 0xffff, NONCANONICAL);
Quoted 19 lines
x86/vmx_tests.c +7
-1+ +write_cr4_safe (read_cr4 () & ~X86_CR4_LAM_SUP ); +} +/* * This does not test real-address mode, virtual-8086 mode, protected mode, * or CPL > 0. @@ -3274,8 +3322,10 @@ static void invvpid_test(void)/* * The gla operand is only validated for single-address INVVPID. */ -if (types & (1u <<INVVPID_ADDR )) +if (types & (1u <<INVVPID_ADDR )) {try_invvpid (INVVPID_ADDR ,0xffff ,NONCANONICAL ); +invvpid_test_lam (); + }invvpid_test_gp ();invvpid_test_ss ();