Skip to contentSkip to conversation

x86: Add test cases for LAM

PATCH Applied Binbin Wu 2 participants 8 messages Jul 1, 2024 – Feb 24, 2025 KVM

lore
Share & tools
lore

Binbin Wu

author
#0 lore raw

v7 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 moves struct invpcid_desc to header file for new test cases. Patch 2 makes change to allow setting of CR3 LAM bits in vmlaunch tests. Patch 3~5 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:
v7
- Move struct invpcid_desc to header file instead of defining a new copy in lam.c.
- Rename is_la57()/lam_sup_active() to is_la57_enabled()/is_lam_sup_enabled(), and move them to processor.h (Sean)
- Drop cr4_set_lam_sup()/cr4_clear_lam_sup() and use write_cr4_safe() instead. (Sean)
- Add get_lam_mask() to get lam status based on the address and vCPU state. (Sean)
- Drop the wrappers for INVLPG since INVLPG never faults. (Sean)
- Drop the wrapper for INVPCID and use invpcid_safe() instead. (Sean)
- Drop the check for X86_FEATURE_PCID. (Sean)
- Rename lam_{u48,u57}_active() to is_lam_{u48,u57}_enabled(), and move them to processor.h (Sean)
- Test LAM userspace address in kernel mode directly to simplify the interface of test_ptr() since LAM identifies a address as kernel or user only based on the address itself.
- Add comments about the virtualization hole of CR3 LAM bits.
- Drop the check of X86_FEATURE_LA57 when check LA57. (Sean)

v6
- https://lore.kernel.org/kvm/[email protected]/

v5
- https://lore.kernel.org/kvm/[email protected]/

Binbin Wu (4):
x86: Move struct invpcid_desc to processor.h
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 |  41 +++++++
 x86/Makefile.x86_64 |   1 +
 x86/lam.c           | 281 ++++++++++++++++++++++++++++++++++++++++++++
 x86/pcid.c          |   6 -
 x86/unittests.cfg   |  10 ++
 x86/vmx_tests.c     |  51 +++++++-
 6 files changed, 382 insertions(+), 8 deletions(-)
 create mode 100644 x86/lam.c

base-commit: d301d0187f5db09531a1c2c7608997cc3b0a5c7d

Signature · 1 line
2.43.2
author
#1 lore raw

PATCH v7 1/5 x86: Move struct invpcid_desc to processor.h

Move struct invpcid_desc to processor.h so that new test cases need to do invpcid can use the definition.

Opportunistically add packed attribute, because according to C standard, the allocation of storage unit for bit-field is implementation specific.

---
v7:
- New added.

---
 lib/x86/processor.h | 6 ++++++
 x86/pcid.c          | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)
lib/x86/processor.h +6 -0
index da1ed662..85a1781b 100644
@@ -959,4 +959,10 @@ static inline void generate_cr0_em_nm(void)
 	fnop();
 }
 
+struct invpcid_desc {
+	u64 pcid : 12;
+	u64 rsv  : 52;
+	u64 addr : 64;
+} __attribute__((packed));
+
 #endif
x86/pcid.c +0 -6
index c503efb8..7425e0fe 100644
@@ -4,12 +4,6 @@
 #include "processor.h"
 #include "desc.h"
 
-struct invpcid_desc {
-    u64 pcid : 12;
-    u64 rsv  : 52;
-    u64 addr : 64;
-};
-
 static void test_pcid_enabled(void)
 {
     int passed = 0;
Signature · 1 line
2.43.2
author
#2 lore raw

PATCH v7 2/5 x86: Allow setting of CR3 LAM bits if LAM supported

In reply to Binbin Wu

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
index 85a1781b..f7f2df50 100644
@@ -68,6 +68,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)
@@ -262,6 +264,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))
 
 /*
  * KVM defined leafs
x86/vmx_tests.c +5 -1
index ffe7064c..4b161c3c 100644
@@ -7032,7 +7032,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.43.2
author
#3 lore raw

PATCH v7 3/5 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,
Chapter LINEAR ADDRESS MASKING (LAM)

---
v7:
- Rename is_la57()/lam_sup_active() to is_la57_enabled()/is_lam_sup_enabled(), and move them to processor.h (Sean)
- Drop cr4_set_lam_sup()/cr4_clear_lam_sup() and use write_cr4_safe() instead. (Sean)
- Add get_lam_mask() to get lam status based on the address and vCPU state. (Sean)
- Drop the wrappers for INVLPG since INVLPG never faults. (Sean)
- Drop the wrapper for INVPCID and use invpcid_safe() instead. (Sean)
- Drop the check for X86_FEATURE_PCID. (Sean)

---
 lib/x86/processor.h |  20 +++++
 x86/Makefile.x86_64 |   1 +
 x86/lam.c           | 214 ++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |  10 +++
 4 files changed, 245 insertions(+)
 create mode 100644 x86/lam.c
lib/x86/processor.h +20 -0
index f7f2df50..a38f87ed 100644
@@ -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"
@@ -120,6 +128,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)
@@ -968,4 +978,14 @@ struct invpcid_desc {
 	u64 addr : 64;
 } __attribute__((packed));
 
+static inline bool is_la57_enabled(void)
+{
+	return !!(read_cr4() & X86_CR4_LA57);
+}
+
+static inline bool is_lam_sup_enabled(void)
+{
+	return !!(read_cr4() & X86_CR4_LAM_SUP);
+}
+
 #endif
x86/Makefile.x86_64 +1 -0
index 2771a6fa..e5db2365 100644
@@ -38,6 +38,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 +214 -0

214 lines not on this page · raw message

x86/unittests.cfg +10 -0
index 7c1691a9..f1178edd 100644
@@ -490,3 +490,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.43.2
author
#4 lore raw

PATCH v7 4/5 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.

---
v7:
- Rename lam_{u48,u57}_active() to is_lam_{u48,u57}_enabled(), and move them to processor.h (Sean)
- Get lam status based on the address and vCPU state. (Sean)
- Test LAM userspace address in kernel mode directly to simplify the interface of test_ptr() since LAM identifies a address as kernel or user only based on the address itself.
- Add comments about the virtualization hole of CR3 LAM bits.

---
 lib/x86/processor.h | 12 ++++++++
 x86/lam.c           | 69 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 1 deletion(-)
lib/x86/processor.h +12 -0
index a38f87ed..c2cafb01 100644
@@ -77,7 +77,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)
@@ -988,4 +990,14 @@ static inline bool is_lam_sup_enabled(void)
 	return !!(read_cr4() & X86_CR4_LAM_SUP);
 }
 
+static inline bool is_lam_u48_enabled(void)
+{
+	return (read_cr3() & (X86_CR3_LAM_U48 | X86_CR3_LAM_U57)) == X86_CR3_LAM_U48;
+}
+
+static inline bool is_lam_u57_enabled(void)
+{
+	return !!(read_cr3() & X86_CR3_LAM_U57);
+}
+
 #endif
x86/lam.c +68 -1

93 lines not on this page · raw message

Signature · 1 line
2.43.2
author
#5 lore raw

PATCH v7 5/5 x86: Add test case for INVVPID with LAM

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

---
v7:
- Drop the check of X86_FEATURE_LA57 when check LA57. (Sean)

---
 x86/vmx_tests.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
x86/vmx_tests.c +44 -1
index 4b161c3c..758ab0d3 100644
@@ -3233,6 +3233,47 @@ static void invvpid_test_not_in_vmx_operation(void)
 	TEST_ASSERT(!vmx_on());
 }
 
+/* LAM doesn't apply to the linear address inside the descriptor of invvpid */
+static void invvpid_test_lam(void)
+{
+	void *vaddr;
+	struct invvpid_operand *operand;
+	u64 lam_mask;
+	bool fault;
+
+	if (!this_cpu_has(X86_FEATURE_LAM)) {
+		report_skip("LAM is not supported, skip INVVPID with LAM");
+		return;
+	}
+
+	write_cr4(read_cr4() | X86_CR4_LAM_SUP);
+	lam_mask = is_la57_enabled() ? LAM57_MASK : LAM48_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, "Expected INVVPID with tagged operand when LAM is enabled to succeed");
+
+	/*
+	 * 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.
+	 */
+	try_invvpid(INVVPID_ADDR, 0xffff, (u64)operand);
+
+	write_cr4(read_cr4() & ~X86_CR4_LAM_SUP);
+}
+
 /*
  * This does not test real-address mode, virtual-8086 mode, protected mode,
  * or CPL > 0.
@@ -3282,8 +3323,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.43.2
author
#6 lore raw
In reply to Binbin Wu

Ping...

Quoted 66 lines · Binbin Wu

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 moves struct invpcid_desc to header file for new test cases. Patch 2 makes change to allow setting of CR3 LAM bits in vmlaunch tests. Patch 3~5 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:
v7
- Move struct invpcid_desc to header file instead of defining a new copy in lam.c.
- Rename is_la57()/lam_sup_active() to is_la57_enabled()/is_lam_sup_enabled(), and move them to processor.h (Sean)
- Drop cr4_set_lam_sup()/cr4_clear_lam_sup() and use write_cr4_safe() instead. (Sean)
- Add get_lam_mask() to get lam status based on the address and vCPU state. (Sean)
- Drop the wrappers for INVLPG since INVLPG never faults. (Sean)
- Drop the wrapper for INVPCID and use invpcid_safe() instead. (Sean)
- Drop the check for X86_FEATURE_PCID. (Sean)
- Rename lam_{u48,u57}_active() to is_lam_{u48,u57}_enabled(), and move them to processor.h (Sean)
- Test LAM userspace address in kernel mode directly to simplify the interface of test_ptr() since LAM identifies a address as kernel or user only based on the address itself.
- Add comments about the virtualization hole of CR3 LAM bits.
- Drop the check of X86_FEATURE_LA57 when check LA57. (Sean)

v6
- https://lore.kernel.org/kvm/[email protected]/

v5
- https://lore.kernel.org/kvm/[email protected]/

Binbin Wu (4):
x86: Move struct invpcid_desc to processor.h
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 |  41 +++++++
  x86/Makefile.x86_64 |   1 +
  x86/lam.c           | 281 ++++++++++++++++++++++++++++++++++++++++++++
  x86/pcid.c          |   6 -
  x86/unittests.cfg   |  10 ++
  x86/vmx_tests.c     |  51 +++++++-
  6 files changed, 382 insertions(+), 8 deletions(-)

create mode 100644 x86/lam.c

base-commit: d301d0187f5db09531a1c2c7608997cc3b0a5c7d

reviewer Applied
#7 lore raw
In reply to Binbin Wu
Quoted 19 lines · Binbin Wu

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 moves struct invpcid_desc to header file for new test cases. Patch 2 makes change to allow setting of CR3 LAM bits in vmlaunch tests. Patch 3~5 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

Quoted 1 line

[...]

Applied to kvm-x86 next (and now pulled by Paolo), except for patch 1, as a previous commit from a different series moved the INVPCID helpers to common code.

Note, I deliberately dropped the "packed" addition. The storage size may be implementation specific, but the layout is not. If the compiler does something bizarre, e.g. allocates 32 instead of 16 bytes for the union, INVPCID will still work correctly as the CPU won't be aware that the compiler reserved 16 extra bytes.

[1/5] x86: Move struct invpcid_desc to processor.h
(no commit info)
[2/5] x86: Allow setting of CR3 LAM bits if LAM supported

      https://github.com/kvm-x86/kvm-unit-tests/commit/0a6b8b7d601a
[3/5] x86: Add test case for LAM_SUP
      https://github.com/kvm-x86/kvm-unit-tests/commit/14520f8e9709
[4/5] x86: Add test cases for LAM_{U48,U57}
      https://github.com/kvm-x86/kvm-unit-tests/commit/0164d7595c85
[5/5] x86: Add test case for INVVPID with LAM
      https://github.com/kvm-x86/kvm-unit-tests/commit/a33a3ac8db56

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next