x86: Add test cases for LAM
v5 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
v4 --> v5:
- LAM doesn't apply to the target linear address in the INVVPID descriptor.
Change the test cases in patch 4 accordingly. Dropped the reviewed-by from Chao due to the change.
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 | 52 +++++++- 5 files changed, 395 insertions(+), 2 deletions(-) create mode 100644 x86/lam.c
base-commit: 02d8befe99f8205d4caea402d8b0800354255681
Signature · 1 line
2.25.1
PATCH v5 1/4 x86: Allow setting of CR3 LAM bits if LAM supported
If LINEAR ADDRESS MASKING (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 v5 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,
Chapter 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 v5 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 v5 4/4 x86: Add test case for INVVPID with LAM
In reply to Binbin WuLAM applies to the linear address of INVVPID operand, however, it doesn't apply to the linear address in the INVVPID descriptor.
The added cases use tagged operand or tagged target invalidation address to make sure the behaviors are expected when LAM is on.
Also, INVVPID case using tagged operand can be used as the common test cases for VMX instruction VMExits.
--- x86/vmx_tests.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)
x86/vmx_tests.c +45 -1
@@ -3225,6 +3225,48 @@ 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 *)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 ); + +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 +3316,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: [PATCH v5 4/4] x86: Add test case for INVVPID with LAM
Quoted 19 lines · Binbin Wu
LAM applies to the linear address of INVVPID operand, however, it doesn't apply to the linear address in the INVVPID descriptor.
The added cases use tagged operand or tagged target invalidation address to make sure the behaviors are expected when LAM is on.
Also, INVVPID case using tagged operand can be used as the common test cases for VMX instruction VMExits.
Signed-off-by Binbin Wu <[email protected]>---
x86/vmx_tests.c| 46 +++++++++++++++++++++++++++++++++++++++++++++-1 file changed, 45 insertions(+), 1 deletion(-)x86/vmx_tests.c +0
-0
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3228
Quoted 4 lines
x86/vmx_tests.c +1
-0@@ -3225,6 +3225,48 @@ static void invvpid_test_not_in_vmx_operation(void)TEST_ASSERT (!vmx_on ()); } +/* LAM applies to the target address inside the descriptor of invvpid */
This isn't correct. LAM doesn't apply to that address. Right?
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3240
Quoted 12 lines
x86/vmx_tests.c +12
-0+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 );
why write_cr4_safe()?
This should succeed if LAM is supported. So it is better to use write_cr4() because write_cr4() has an assertion which can catch unexpected exceptions.
Quoted 14 lines
x86/vmx_tests.c +14
-0+ +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;
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3264
Quoted 10 lines
x86/vmx_tests.c +10
-0+ 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. +*/
Maybe
/*
* Verify that LAM doesn't apply to the address inside the descriptor
* even when LAM is enabled. i.e., the address in the descriptor should
* be canonical.
*/
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3265
Quoted 1 line
x86/vmx_tests.c +1
-0+try_invvpid (INVVPID_ADDR ,0xffff ,NONCANONICAL );
shouldn't we use a kernel address here? e.g., vaddr. otherwise, we cannot tell if there is an error in KVM's emulation because in this test, LAM is enabled only for kernel address while INVVPID_ADDR is a userspace address.
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3267
Quoted 2 lines
x86/vmx_tests.c +2
-0+ +write_cr4_safe (read_cr4 () & ~X86_CR4_LAM_SUP );
ditto.
With all above fixed:
Quoted 20 lines
x86/vmx_tests.c +5
-1+} +/* * This does not test real-address mode, virtual-8086 mode, protected mode, * or CPL > 0. @@ -3274,8 +3316,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: [PATCH v5 4/4] x86: Add test case for INVVPID with LAM
Quoted 23 lines · Chao Gao
Quoted 22 lines · Binbin Wu
LAM applies to the linear address of INVVPID operand, however, it doesn't apply to the linear address in the INVVPID descriptor.
The added cases use tagged operand or tagged target invalidation address to make sure the behaviors are expected when LAM is on.
Also, INVVPID case using tagged operand can be used as the common test cases for VMX instruction VMExits.
Signed-off-by Binbin Wu <[email protected]>---
x86/vmx_tests.c| 46 +++++++++++++++++++++++++++++++++++++++++++++-1 file changed, 45 insertions(+), 1 deletion(-)x86/vmx_tests.c +0
-0@@ -3225,6 +3225,48 @@ static void invvpid_test_not_in_vmx_operation(void)TEST_ASSERT (!vmx_on ()); }
Quoted 2 lines
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3228
Quoted 1 line
x86/vmx_tests.c +1
-0+/* LAM applies to the target address inside the descriptor of invvpid */ This isn't correct. LAM doesn't apply to that address. Right?
Oops, will fix it, thanks.
Quoted 15 lines
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3240
Quoted 13 lines
x86/vmx_tests.c +12
-0+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 );why
write_cr4_safe()?
Quoted 3 lines
This should succeed if LAM is supported. So it is better to use
write_cr4()becausewrite_cr4()has an assertion which can catch unexpected exceptions.
OK.
Quoted 32 lines
Quoted 15 lines
x86/vmx_tests.c +14
-0+ +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;x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3264
Quoted 10 lines
x86/vmx_tests.c +10
-0+ 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. +*/ Maybe
/*
* Verify that LAM doesn't apply to the address inside the descriptor
* even when LAM is enabled. i.e., the address in the descriptor should
* be canonical.
*/
Quoted 5 lines
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3265
Quoted 1 line
x86/vmx_tests.c +1
-0+try_invvpid (INVVPID_ADDR ,0xffff ,NONCANONICAL );shouldn't we use a kernel address here? e.g., vaddr. otherwise, we cannot tell if there is an error in KVM's emulation because in this test, LAM is enabled only for kernel address while
INVVPID_ADDRis a userspace address.
INVVPID_ADDR is the invalidation type, not the address.
The address used here is NONCANONICAL, which is 0xaaaaaaaaaaaaaaaaull and is considered as kernel address.
Quoted 30 lines
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3267
Quoted 3 lines
x86/vmx_tests.c +2
-0+ +write_cr4_safe (read_cr4 () & ~X86_CR4_LAM_SUP );ditto.
With all above fixed:
Reviewed-by Chao Gao <[email protected]>Quoted 20 lines
x86/vmx_tests.c +5
-1+} +/* * This does not test real-address mode, virtual-8086 mode, protected mode, * or CPL > 0. @@ -3274,8 +3316,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: [PATCH v5 4/4] x86: Add test case for INVVPID with LAM
Quoted 9 lines · Binbin Wu
Quoted 5 lines
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3265
Quoted 1 line
x86/vmx_tests.c +1
-0+try_invvpid (INVVPID_ADDR ,0xffff ,NONCANONICAL );shouldn't we use a kernel address here? e.g., vaddr. otherwise, we cannot tell if there is an error in KVM's emulation because in this test, LAM is enabled only for kernel address while
INVVPID_ADDRis a userspace address.
INVVPID_ADDRis the invalidation type, not the address.
The address used here is NONCANONICAL, which is0xaaaaaaaaaaaaaaaaulland is considered as kernel address.
Yes. Sorry about this misunderstanding.
Do you need the address to be canonical after masking metadata?
Re: [PATCH v5 4/4] x86: Add test case for INVVPID with LAM
Quoted 6 lines · Chao Gao
Quoted 5 lines · Binbin Wu
Quoted 5 lines
x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3265
Quoted 1 line
x86/vmx_tests.c +1
-0+try_invvpid (INVVPID_ADDR ,0xffff ,NONCANONICAL );shouldn't we use a kernel address here? e.g., vaddr. otherwise, we cannot tell if there is an error in KVM's emulation because in this test, LAM is enabled only for kernel address while
INVVPID_ADDRis a userspace address.
Quoted 6 lines
Quoted 3 lines
INVVPID_ADDRis the invalidation type, not the address.
The address used here is NONCANONICAL, which is0xaaaaaaaaaaaaaaaaulland is considered as kernel address.Yes. Sorry about this misunderstanding.
Do you need the address to be canonical after masking metadata?
You are right, I will use set_la_non_canonical(), which is added in 2/4 to pass a proper address for test. Thanks.