Skip to contentSkip to conversation

x86: Add test cases for LAM

PATCH Superseded Binbin Wu 2 participants 13 messages Apr 12, 2023 – Apr 23, 2023 KVM

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Binbin Wu

author
#0 lore raw

v3 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
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 |   7 +
 x86/Makefile.x86_64 |   1 +
 x86/lam.c           | 315 ++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |  10 ++
 x86/vmx_tests.c     |  66 +++++++++-
 5 files changed, 398 insertions(+), 1 deletion(-)
 create mode 100644 x86/lam.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 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
index 3d58ef7..e00a32b 100644
@@ -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)
@@ -248,6 +250,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/vmx_tests.c +5 -1
index 7bba816..5ee1264 100644
@@ -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
#2 lore raw

Re: [kvm-unit-tests v3 1/4] x86: Allow setting of CR3 LAM bits if LAM supported

Quoted 39 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.

---
lib/x86/processor.h | 3 +++
x86/vmx_tests.c | 6 +++++-

2 files changed, 8 insertions(+), 1 deletion(-)
lib/x86/processor.h +3 -0
index 3d58ef7..e00a32b 100644
@@ -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)
@@ -248,6 +250,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/vmx_tests.c +0 -0
index 7bba816..5ee1264 100644

x86/vmx_tests.c static void test_host_ctl_regs(void) · line 7004

Quoted 6 lines
x86/vmx_tests.c +2 -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)))

^ stray space

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
author
#3 lore raw

Re: [kvm-unit-tests v3 1/4] x86: Allow setting of CR3 LAM bits if LAM supported

Quoted 33 lines · Chao Gao
Quoted 32 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.

---
lib/x86/processor.h | 3 +++
x86/vmx_tests.c | 6 +++++-

2 files changed, 8 insertions(+), 1 deletion(-)
lib/x86/processor.h +3 -0
index 3d58ef7..e00a32b 100644
@@ -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)
@@ -248,6 +250,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))
 
Quoted 14 lines

x86/vmx_tests.c static void test_host_ctl_regs(void) · line 7004

Quoted 13 lines
lib/x86/processor.h +0 -0
 /*
  * Extended Leafs, a.k.a. AMD defined
x86/vmx_tests.c +2 -1
index 7bba816..5ee1264 100644
@@ -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)))

^ stray space

Thanks, will remove it.

Quoted 10 lines
Quoted 10 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
author
#4 lore raw

PATCH v3 2/4 x86: Add test case for LAM_SUP

In reply to Binbin Wu

From: 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 |   2 +
 x86/Makefile.x86_64 |   1 +
 x86/lam.c           | 244 ++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |  10 ++
 4 files changed, 257 insertions(+)
 create mode 100644 x86/lam.c
lib/x86/processor.h +2 -0
index e00a32b..4bb8cd7 100644
@@ -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)
x86/Makefile.x86_64 +1 -0
index f483dea..fa11eb3 100644
@@ -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 +244 -0

244 lines not on this page · raw message

x86/unittests.cfg +10 -0
index f324e32..34b09eb 100644
@@ -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
Reviewed-by on v3 2/4
#5 lore raw

Re: [kvm-unit-tests v3 2/4] x86: Add test case for LAM_SUP

Quoted 10 lines · Binbin Wu

From: 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.

Quoted 8 lines

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)

one nit below

x86/lam.c line 138

Quoted 3 lines
x86/lam.c new file +3 -0
+static void do_invpcid(void *desc)
+{
+	unsigned long type = 0;

the local variable isn't needed ...

x86/lam.c line 142

Quoted 4 lines
x86/lam.c new file +4 -0
+	struct invpcid_desc *desc_ptr = (struct invpcid_desc *)desc;
+
+	asm volatile("invpcid %0, %1" :
+	                              : "m" (*desc_ptr), "r" (type)

You can simply use

	"r" (0)

.

Quoted 2 lines
(quoted diff) +2 -0
+	                              : "memory");
+}
author
#6 lore raw

PATCH v3 3/4 x86: Add test cases for LAM_{U48,U57}

In reply to Binbin Wu

This 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           | 71 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
lib/x86/processor.h +2 -0
index 4bb8cd7..a181e0b 100644
@@ -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 +71 -0

110 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v3 3/4
#7 lore raw

Re: [kvm-unit-tests v3 3/4] x86: Add test cases for LAM_{U48,U57}

Quoted 5 lines · Binbin Wu

This unit test covers:
1. CR3 LAM bits toggles.
2. Memory/MMIO access with user mode address containing LAM metadata.

two nits below:

x86/lam.c static void test_lam_sup(bool has_lam, bool fep_available) · line 285

Quoted 11 lines

---

x86/lam.c +10 -0
+static void test_lam_user(bool has_lam)
+{
+	phys_addr_t paddr;
+	unsigned long cr3 = read_cr3();
+
+	/*
+	 * The physical address width is within 36 bits, so that using identical
+	 * mapping, the linear address will be considered as user mode address
+	 * from the view of LAM.
+	 */

Why 36 bits (i.e., 64G)?

would you mind adding a comment in patch 2 to explain why the virtual addresses are kernel mode addresses?

x86/lam.c static void test_lam_sup(bool has_lam, bool fep_available) · line 288

Quoted 3 lines
x86/lam.c +3 -0
+	paddr = virt_to_phys(alloc_page());
+	install_page((void *)cr3, paddr, (void *)paddr);
+	install_page((void *)cr3, IORAM_BASE_PHYS, (void *)IORAM_BASE_PHYS);

are the two lines necessary?

Quoted 19 lines
x86/lam.c +5 -0
+
+	test_lam_user_mode(has_lam, LAM48_MASK, paddr, IORAM_BASE_PHYS);
+	test_lam_user_mode(has_lam, LAM57_MASK, paddr, IORAM_BASE_PHYS);
+}
+

int main(int ac, char **av)

{
	bool has_lam;
x86/lam.c +1 -0
@@ -239,6 +309,7 @@ int main(int ac, char **av)
 			    "use kvm.force_emulation_prefix=1 to enable\n");
 
 	test_lam_sup(has_lam, fep_available);
+	test_lam_user(has_lam);
 
 	return report_summary();
 }
Signature · 1 line
2.25.1
author
#8 lore raw

Re: [kvm-unit-tests v3 3/4] x86: Add test cases for LAM_{U48,U57}

Quoted 10 lines · Chao Gao
Quoted 5 lines · Binbin Wu

This unit test covers:
1. CR3 LAM bits toggles.
2. Memory/MMIO access with user mode address containing LAM metadata.

two nits below:

Quoted 12 lines

x86/lam.c static void test_lam_sup(bool has_lam, bool fep_available) · line 285

Quoted 11 lines

---

x86/lam.c +10 -0
+static void test_lam_user(bool has_lam)
+{
+	phys_addr_t paddr;
+	unsigned long cr3 = read_cr3();
+
+	/*
+	 * The physical address width is within 36 bits, so that using identical
+	 * mapping, the linear address will be considered as user mode address
+	 * from the view of LAM.
+	 */

Why 36 bits (i.e., 64G)?

KUT memory allocator supports 4 Memory Areas:
AREA_NORMAL, AREA_HIGH, AREA_LOW and AREA_LOWEST. AREA_NORMAL has the maximum address width, which is 36 bits.

How about change the comment to this:

+ /*
+ * The physical address width supported by KUT memory allocator is within 36 bits,
+ * so that using identical mapping, the linear address will be considered as user
+ * mode address from the view of LAM.
+ */

Quoted 3 lines

would you mind adding a comment in patch 2 to explain why the virtual addresses are kernel mode addresses?

OK. Will add the following comments:

    /*
     * KUT initializes vfree_top to 0 for X86_64, and each virtual address
     * allocation decreases the size from vfree_top. It's guaranteed that
     * the return value of alloc_vpage() is considered as kernel mode
     * address and canonical since only a small mount virtual address range
     * is allocated in this test.
     */

Quoted 5 lines

x86/lam.c static void test_lam_sup(bool has_lam, bool fep_available) · line 288

Quoted 4 lines
x86/lam.c +3 -0
+	paddr = virt_to_phys(alloc_page());
+	install_page((void *)cr3, paddr, (void *)paddr);
+	install_page((void *)cr3, IORAM_BASE_PHYS, (void *)IORAM_BASE_PHYS);

are the two lines necessary?

The two lines are not necessary.
Check setup_mmu(), it already sets up the identical mapping for avialble physcial memory/MMIO. Will remove them.

Quoted 20 lines
Quoted 20 lines
x86/lam.c +5 -0
+
+	test_lam_user_mode(has_lam, LAM48_MASK, paddr, IORAM_BASE_PHYS);
+	test_lam_user_mode(has_lam, LAM57_MASK, paddr, IORAM_BASE_PHYS);
+}
+

int main(int ac, char **av)

{
	bool has_lam;
x86/lam.c +1 -0
@@ -239,6 +309,7 @@ int main(int ac, char **av)
 			    "use kvm.force_emulation_prefix=1 to enable\n");
 
 	test_lam_sup(has_lam, fep_available);
+	test_lam_user(has_lam);
 
 	return report_summary();
 }
Signature · 1 line
2.25.1
author
#9 lore raw

PATCH v3 4/4 x86: Add test case for INVVPID with LAM

In reply to 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.

---
 x86/vmx_tests.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
x86/vmx_tests.c +60 -0
index 5ee1264..381ca1c 100644
@@ -3225,6 +3225,65 @@ static void invvpid_test_not_in_vmx_operation(void)
 	TEST_ASSERT(!vmx_on());
 }
 
+#define LAM57_MASK	GENMASK_ULL(62, 57)
+#define LAM48_MASK	GENMASK_ULL(62, 48)
+
+static inline u64 set_metadata(u64 src, u64 metadata_mask)
+{
+	return (src & ~metadata_mask) | (NONCANONICAL & metadata_mask);
+}
+
+/* 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;
+	}
+
+	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;
+
+	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_mask);
+	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_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_metadata((u64)operand, lam_mask);
+	operand->gla = set_metadata(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.
@@ -3282,6 +3341,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
Reviewed-by on v3 4/4
#10 lore raw

Re: [kvm-unit-tests v3 4/4] x86: Add test case for INVVPID with LAM

Quoted 11 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.

with a few cosmetic comments below:

Quoted 9 lines

---
x86/vmx_tests.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 60 insertions(+)
x86/vmx_tests.c +0 -0
index 5ee1264..381ca1c 100644

x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3234

Quoted 10 lines
x86/vmx_tests.c +7 -0
@@ -3225,6 +3225,65 @@ static void invvpid_test_not_in_vmx_operation(void)
 	TEST_ASSERT(!vmx_on());
 }
 
+#define LAM57_MASK	GENMASK_ULL(62, 57)
+#define LAM48_MASK	GENMASK_ULL(62, 48)
+
+static inline u64 set_metadata(u64 src, u64 metadata_mask)
+{
+	return (src & ~metadata_mask) | (NONCANONICAL & metadata_mask);
+}

Can you move the duplicate defintions and functions to a header file?

Quoted 13 lines
x86/vmx_tests.c +13 -0
+
+/* 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;
+	}

...

Quoted 10 lines
x86/vmx_tests.c +10 -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.

x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3267

Quoted 10 lines
x86/vmx_tests.c +10 -0
+	 */
+	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;
+	}

It might be better to enable LAM_SUP right after above check for the LAM CPUID bit. And no need to verify the result because there is a dedicated test case already in patch 2.

Quoted 23 lines
x86/vmx_tests.c +19 -0
+
+	operand = (struct invvpid_operand *)vaddr;
+	operand->gla = set_metadata(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_metadata((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_metadata((u64)operand, lam_mask);
+	operand->gla = set_metadata(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.

x86/vmx_tests.c static void invvpid_test(void) · line 3344

Quoted 4 lines
x86/vmx_tests.c +1 -0
@@ -3282,6 +3341,7 @@ static void invvpid_test(void)
 	invvpid_test_pf();
 	invvpid_test_compatibility_mode();
 	invvpid_test_not_in_vmx_operation();
+	invvpid_test_lam();

operand->gla is checked only in INVVPID_ADDR mode. So, the lam test should be moved under "if (types & (1u << INVVPID_ADDR))" a few lines above.

Quoted 6 lines
x86/vmx_tests.c +0 -0
 }
 
 /*
Signature · 1 line
2.25.1
author
#11 lore raw

Re: [kvm-unit-tests v3 4/4] x86: Add test case for INVVPID with LAM

Quoted 28 lines · Chao Gao
Quoted 11 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.

with a few cosmetic comments below:

Quoted 12 lines

---
x86/vmx_tests.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 60 insertions(+)
x86/vmx_tests.c +0 -0
index 5ee1264..381ca1c 100644
@@ -3225,6 +3225,65 @@ static void invvpid_test_not_in_vmx_operation(void)
 	TEST_ASSERT(!vmx_on());
 }
 
Quoted 8 lines

x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3234

Quoted 7 lines
x86/vmx_tests.c +7 -0
+#define LAM57_MASK	GENMASK_ULL(62, 57)
+#define LAM48_MASK	GENMASK_ULL(62, 48)
+
+static inline u64 set_metadata(u64 src, u64 metadata_mask)
+{
+	return (src & ~metadata_mask) | (NONCANONICAL & metadata_mask);
+}

Can you move the duplicate defintions and functions to a header file?

Then add a new header file lam.h?
Didn't find a suitable existant header file to add these definitions.

Quoted 31 lines
Quoted 14 lines
x86/vmx_tests.c +13 -0
+
+/* 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;
+	}

...

Quoted 15 lines
x86/vmx_tests.c +15 -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;
+
Quoted 8 lines

x86/vmx_tests.c static void invvpid_test_not_in_vmx_operation(void) · line 3267

Quoted 5 lines
x86/vmx_tests.c +5 -0
+	write_cr4_safe(read_cr4() | X86_CR4_LAM_SUP);
+	if (!(read_cr4() & X86_CR4_LAM_SUP)) {
+		report_skip("Failed to enable LAM_SUP");
+		return;
+	}

It might be better to enable LAM_SUP right after above check for the LAM CPUID bit. And no need to verify the result because there is a dedicated test case already in patch 2.

OK.

Quoted 21 lines
Quoted 21 lines
x86/vmx_tests.c +19 -0
+
+	operand = (struct invvpid_operand *)vaddr;
+	operand->gla = set_metadata(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_metadata((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_metadata((u64)operand, lam_mask);
+	operand->gla = set_metadata(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,

Quoted 8 lines

x86/vmx_tests.c static void invvpid_test(void) · line 3344

Quoted 6 lines

* or CPL > 0.

x86/vmx_tests.c +1 -0
@@ -3282,6 +3341,7 @@ static void invvpid_test(void)
 	invvpid_test_pf();
 	invvpid_test_compatibility_mode();
 	invvpid_test_not_in_vmx_operation();
+	invvpid_test_lam();

operand->gla is checked only in INVVPID_ADDR mode. So, the lam test should be moved under "if (types & (1u << INVVPID_ADDR))" a few lines above.

Yes, will update it.

Quoted 7 lines
Quoted 7 lines
x86/vmx_tests.c +0 -0
 }
 
 /*
Signature · 1 line
2.25.1
author
#12 lore raw

Re: [kvm-unit-tests v3 4/4] x86: Add test case for INVVPID with LAM

Quoted 40 lines · Binbin Wu
Quoted 29 lines · Chao Gao
Quoted 11 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.

with a few cosmetic comments below:

Quoted 13 lines

---
x86/vmx_tests.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 60 insertions(+)
x86/vmx_tests.c +0 -0
index 5ee1264..381ca1c 100644
@@ -3225,6 +3225,65 @@ static void 
 invvpid_test_not_in_vmx_operation(void)
     TEST_ASSERT(!vmx_on());
 }
 
Quoted 8 lines

x86/vmx_tests.c static void · line 3235

Quoted 7 lines
x86/vmx_tests.c +7 -0
+#define LAM57_MASK    GENMASK_ULL(62, 57)
+#define LAM48_MASK    GENMASK_ULL(62, 48)
+
+static inline u64 set_metadata(u64 src, u64 metadata_mask)
+{
+    return (src & ~metadata_mask) | (NONCANONICAL & metadata_mask);
+}

Can you move the duplicate defintions and functions to a header file?

Quoted 2 lines

Then add a new header file lam.h?
Didn't find a suitable existant header file to add these definitions.

I am about to move the definitions to lib/x86/processor.h and rename set_metadata() to set_la_non_canonical() to be more generic.

index e00a32b..236b537 100644

lib/x86/processor.h +0 -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);
+}

Quoted 93 lines
Quoted 37 lines
Quoted 17 lines
x86/vmx_tests.c +2 -0
+
+/* LAM applies to the target address inside the descriptor of 

invvpid */

x86/vmx_tests.c +11 -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;
+    }

...

Quoted 18 lines
x86/vmx_tests.c +6 -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;
+

Quoted 8 lines
Quoted 5 lines

+    write_cr4_safe(read_cr4() | X86_CR4_LAM_SUP); +    if (!(read_cr4() & X86_CR4_LAM_SUP)) { +        report_skip("Failed to enable LAM_SUP"); +        return;
+    }

It might be better to enable LAM_SUP right after above check for the LAM CPUID bit. And no need to verify the result because there is a dedicated test case already in patch 2.

OK.

Quoted 25 lines
Quoted 25 lines

+
+    operand = (struct invvpid_operand *)vaddr;
+    operand->gla = set_metadata(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_metadata((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_metadata((u64)operand, lam_mask);
+    operand->gla = set_metadata(operand->gla, lam_mask);
+    fault = test_for_exception(GP_VECTOR, ds_invvpid, operand); +    report(!fault, "INVVPID (LAM on): tagged pointer + tagged addr");

x86/vmx_tests.c +4 -0
+
+    write_cr4_safe(read_cr4() & ~X86_CR4_LAM_SUP);
+}
+

/*
  * This does not test real-address mode, virtual-8086 mode, protected mode,

Quoted 8 lines

x86/vmx_tests.c static void invvpid_test(void) · line 3344

Quoted 6 lines

  * or CPL > 0.

x86/vmx_tests.c +1 -0
@@ -3282,6 +3341,7 @@ static void invvpid_test(void)
     invvpid_test_pf();
     invvpid_test_compatibility_mode();
     invvpid_test_not_in_vmx_operation();
+    invvpid_test_lam();

operand->gla is checked only in INVVPID_ADDR mode. So, the lam test should be moved under "if (types & (1u << INVVPID_ADDR))" a few lines above.

Yes, will update it.

Quoted 7 lines
Quoted 7 lines
x86/vmx_tests.c +0 -0
 }
 
 /*
Signature · 1 line
2.25.1