Skip to contentSkip to conversation

KVM: VMX: Cleanup VMX basic information defines and usages

PATCH Superseded Xin Li (Intel) 2 participants 4 messages Sep 27, 2023 – Oct 19, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: VMX: Cleanup VMX basic information defines and usages

From: Xin Li <[email protected]>

Add IA32_VMX_BASIC MSR bitfield shift macros and use them to define VMX basic information bitfields.

Add VMX_BASIC_FEATURES and VMX_BASIC_RESERVED_BITS to form a valid bitmask of IA32_VMX_BASIC MSR. As a result, to add a new VMX basic feature bit, just change the 2 new macros in the header file.

Also replace hardcoded VMX basic numbers with the new VMX basic macros.

---
 arch/x86/include/asm/msr-index.h       | 31 ++++++++++++++++++++------
 arch/x86/kvm/vmx/nested.c              | 10 +++------
 arch/x86/kvm/vmx/vmx.c                 |  2 +-
 tools/arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------
 4 files changed, 52 insertions(+), 22 deletions(-)
arch/x86/include/asm/msr-index.h +24 -7
index 1d111350197f..4607448ff805 100644
@@ -1084,13 +1084,30 @@
 #define MSR_IA32_VMX_PROCBASED_CTLS3	0x00000492
 
 /* VMX_BASIC bits and bitmasks */
-#define VMX_BASIC_VMCS_SIZE_SHIFT	32
-#define VMX_BASIC_TRUE_CTLS		(1ULL << 55)
-#define VMX_BASIC_64		0x0001000000000000LLU
-#define VMX_BASIC_MEM_TYPE_SHIFT	50
-#define VMX_BASIC_MEM_TYPE_MASK	0x003c000000000000LLU
-#define VMX_BASIC_MEM_TYPE_WB	6LLU
-#define VMX_BASIC_INOUT		0x0040000000000000LLU
+#define VMX_BASIC_VMCS_SIZE_SHIFT		32
+#define VMX_BASIC_ALWAYS_0			BIT_ULL(31)
+#define VMX_BASIC_RESERVED_RANGE_1		GENMASK_ULL(47, 45)
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT	48
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY		BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT)
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT	49
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT	BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT)
+#define VMX_BASIC_MEM_TYPE_SHIFT		50
+#define VMX_BASIC_MEM_TYPE_WB			6LLU
+#define VMX_BASIC_INOUT_SHIFT			54
+#define VMX_BASIC_INOUT				BIT_ULL(VMX_BASIC_INOUT_SHIFT)
+#define VMX_BASIC_TRUE_CTLS_SHIFT		55
+#define VMX_BASIC_TRUE_CTLS			BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT)
+#define VMX_BASIC_RESERVED_RANGE_2		GENMASK_ULL(63, 56)
+
+#define VMX_BASIC_FEATURES			\
+	(VMX_BASIC_DUAL_MONITOR_TREATMENT |	\
+	 VMX_BASIC_INOUT |			\
+	 VMX_BASIC_TRUE_CTLS)
+
+#define VMX_BASIC_RESERVED_BITS			\
+	(VMX_BASIC_ALWAYS_0 |			\
+	 VMX_BASIC_RESERVED_RANGE_1 |		\
+	 VMX_BASIC_RESERVED_RANGE_2)
 
 /* Resctrl MSRs: */
 /* - Intel: */
arch/x86/kvm/vmx/nested.c +3 -7
index c5ec0ef51ff7..5280ba944c87 100644
@@ -1203,21 +1203,17 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
 
 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
 {
-	const u64 feature_and_reserved =
-		/* feature (except bit 48; see below) */
-		BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
-		/* reserved */
-		BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
 	u64 vmx_basic = vmcs_config.nested.basic;
 
-	if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
+	if (!is_bitwise_subset(vmx_basic, data,
+			       VMX_BASIC_FEATURES | VMX_BASIC_RESERVED_BITS))
 		return -EINVAL;
 
 	/*
 	 * KVM does not emulate a version of VMX that constrains physical
 	 * addresses of VMX structures (e.g. VMCS) to 32-bits.
 	 */
-	if (data & BIT_ULL(48))
+	if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY)
 		return -EINVAL;
 
 	if (vmx_basic_vmcs_revision_id(vmx_basic) !=
arch/x86/kvm/vmx/vmx.c +1 -1
index 72e3943f3693..f597243d6a72 100644
@@ -2701,7 +2701,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 
 #ifdef CONFIG_X86_64
 	/* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
-	if (vmx_msr_high & (1u<<16))
+	if (vmx_msr_high & (1u << (VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT - 32)))
 		return -EIO;
 #endif
 
tools/arch/x86/include/asm/msr-index.h +24 -7
index 1d111350197f..4607448ff805 100644
@@ -1084,13 +1084,30 @@
 #define MSR_IA32_VMX_PROCBASED_CTLS3	0x00000492
 
 /* VMX_BASIC bits and bitmasks */
-#define VMX_BASIC_VMCS_SIZE_SHIFT	32
-#define VMX_BASIC_TRUE_CTLS		(1ULL << 55)
-#define VMX_BASIC_64		0x0001000000000000LLU
-#define VMX_BASIC_MEM_TYPE_SHIFT	50
-#define VMX_BASIC_MEM_TYPE_MASK	0x003c000000000000LLU
-#define VMX_BASIC_MEM_TYPE_WB	6LLU
-#define VMX_BASIC_INOUT		0x0040000000000000LLU
+#define VMX_BASIC_VMCS_SIZE_SHIFT		32
+#define VMX_BASIC_ALWAYS_0			BIT_ULL(31)
+#define VMX_BASIC_RESERVED_RANGE_1		GENMASK_ULL(47, 45)
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT	48
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY		BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT)
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT	49
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT	BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT)
+#define VMX_BASIC_MEM_TYPE_SHIFT		50
+#define VMX_BASIC_MEM_TYPE_WB			6LLU
+#define VMX_BASIC_INOUT_SHIFT			54
+#define VMX_BASIC_INOUT				BIT_ULL(VMX_BASIC_INOUT_SHIFT)
+#define VMX_BASIC_TRUE_CTLS_SHIFT		55
+#define VMX_BASIC_TRUE_CTLS			BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT)
+#define VMX_BASIC_RESERVED_RANGE_2		GENMASK_ULL(63, 56)
+
+#define VMX_BASIC_FEATURES			\
+	(VMX_BASIC_DUAL_MONITOR_TREATMENT |	\
+	 VMX_BASIC_INOUT |			\
+	 VMX_BASIC_TRUE_CTLS)
+
+#define VMX_BASIC_RESERVED_BITS			\
+	(VMX_BASIC_ALWAYS_0 |			\
+	 VMX_BASIC_RESERVED_RANGE_1 |		\
+	 VMX_BASIC_RESERVED_RANGE_2)
 
 /* Resctrl MSRs: */
 /* - Intel: */
Signature · 1 line
2.40.1
#1 lore raw
Quoted 4 lines · Xin Li (Intel)

From: Xin Li <[email protected]>

Add IA32_VMX_BASIC MSR bitfield shift macros and use them to define VMX basic information bitfields.

Why? Unless something actually uses the shift independently, just define the BIT_ULL(...) straightaway.

Quoted 3 lines

Add VMX_BASIC_FEATURES and VMX_BASIC_RESERVED_BITS to form a valid bitmask of IA32_VMX_BASIC MSR. As a result, to add a new VMX basic feature bit, just change the 2 new macros in the header file.

Not if a new feature bit lands in the middle of one of the reserved ranges, then the developer will have to update at least three macros, and add a new one. More below.

Quoted 9 lines

Also replace hardcoded VMX basic numbers with the new VMX basic macros.

---
 arch/x86/include/asm/msr-index.h       | 31 ++++++++++++++++++++------
 arch/x86/kvm/vmx/nested.c              | 10 +++------
 arch/x86/kvm/vmx/vmx.c                 |  2 +-
 tools/arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------

Please drop the tools/ update, copying kernel headers into tools is a perf tools thing that I want no part of.

https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]

Quoted 23 lines
 4 files changed, 52 insertions(+), 22 deletions(-)
arch/x86/include/asm/msr-index.h +6 -7
index 1d111350197f..4607448ff805 100644
@@ -1084,13 +1084,30 @@
 #define MSR_IA32_VMX_PROCBASED_CTLS3	0x00000492
 
 /* VMX_BASIC bits and bitmasks */
-#define VMX_BASIC_VMCS_SIZE_SHIFT	32
-#define VMX_BASIC_TRUE_CTLS		(1ULL << 55)
-#define VMX_BASIC_64		0x0001000000000000LLU
-#define VMX_BASIC_MEM_TYPE_SHIFT	50
-#define VMX_BASIC_MEM_TYPE_MASK	0x003c000000000000LLU
-#define VMX_BASIC_MEM_TYPE_WB	6LLU
-#define VMX_BASIC_INOUT		0x0040000000000000LLU
+#define VMX_BASIC_VMCS_SIZE_SHIFT		32
+#define VMX_BASIC_ALWAYS_0			BIT_ULL(31)
+#define VMX_BASIC_RESERVED_RANGE_1		GENMASK_ULL(47, 45)
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT	48
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY		BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT)
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT	49

arch/x86/include/asm/msr-index.h line 1102

Quoted 10 lines
arch/x86/include/asm/msr-index.h +10 -0
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT	BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT)
+#define VMX_BASIC_MEM_TYPE_SHIFT		50
+#define VMX_BASIC_MEM_TYPE_WB			6LLU
+#define VMX_BASIC_INOUT_SHIFT			54
+#define VMX_BASIC_INOUT				BIT_ULL(VMX_BASIC_INOUT_SHIFT)
+#define VMX_BASIC_TRUE_CTLS_SHIFT		55
+#define VMX_BASIC_TRUE_CTLS			BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT)
+#define VMX_BASIC_RESERVED_RANGE_2		GENMASK_ULL(63, 56)
+
+#define VMX_BASIC_FEATURES			\

Maybe VMX_BASIC_FEATURES_MASK to make it more obvious it's a mask of multiple bits?

arch/x86/include/asm/msr-index.h line 1110

Quoted 8 lines
arch/x86/include/asm/msr-index.h +8 -0
+	(VMX_BASIC_DUAL_MONITOR_TREATMENT |	\
+	 VMX_BASIC_INOUT |			\
+	 VMX_BASIC_TRUE_CTLS)
+
+#define VMX_BASIC_RESERVED_BITS			\
+	(VMX_BASIC_ALWAYS_0 |			\
+	 VMX_BASIC_RESERVED_RANGE_1 |		\
+	 VMX_BASIC_RESERVED_RANGE_2)

I don't see any value in defining VMX_BASIC_RESERVED_RANGE_1 and
VMX_BASIC_RESERVED_RANGE_2 separately. Or VMX_BASIC_ALWAYS_0 for the matter.
And I don't think these macros need to go in msr-index.h, e.g. just define them above vmx_restore_vmx_basic() as that's likely going to be the only user, ever.

And what's really missing is a static_assert() or BUILD_BUG_ON() to ensure that VMX_BASIC_FEATURES doesn't overlap with VMX_BASIC_RESERVED_BITS.

Quoted 36 lines
arch/x86/include/asm/msr-index.h +0 -0
 /* Resctrl MSRs: */
 /* - Intel: */
arch/x86/kvm/vmx/nested.c +3 -7
index c5ec0ef51ff7..5280ba944c87 100644
@@ -1203,21 +1203,17 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
 
 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
 {
-	const u64 feature_and_reserved =
-		/* feature (except bit 48; see below) */
-		BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
-		/* reserved */
-		BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
 	u64 vmx_basic = vmcs_config.nested.basic;
 
-	if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
+	if (!is_bitwise_subset(vmx_basic, data,
+			       VMX_BASIC_FEATURES | VMX_BASIC_RESERVED_BITS))
 		return -EINVAL;
 
 	/*
 	 * KVM does not emulate a version of VMX that constrains physical
 	 * addresses of VMX structures (e.g. VMCS) to 32-bits.
 	 */
-	if (data & BIT_ULL(48))
+	if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY)
 		return -EINVAL;
 
 	if (vmx_basic_vmcs_revision_id(vmx_basic) !=
arch/x86/kvm/vmx/vmx.c +0 -0
index 72e3943f3693..f597243d6a72 100644

arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2704

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2701,7 +2701,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 
 #ifdef CONFIG_X86_64
 	/* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
-	if (vmx_msr_high & (1u<<16))
+	if (vmx_msr_high & (1u << (VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT - 32)))

In all honestly, I find the existing code easier to read. I'm definitely not saying the existing code is good, but IMO this is at best a wash.

I would much rather we do something like this and move away from the hi/lo crud entirely:

arch/x86/kvm/vmx/vmx.c +7 -7
index 86ce9efe6c66..f103980c3d02 100644
@@ -2693,28 +2693,28 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
                _vmexit_control &= ~x_ctrl;
        }
 
-       rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
+       rdmsrl(MSR_IA32_VMX_BASIC, vmx_msr);
 
        /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
-       if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
+       if ((VMX_BASIC_VMCS_SIZE(vmx_msr) > PAGE_SIZE)
                return -EIO;
 
 #ifdef CONFIG_X86_64
        /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
-       if (vmx_msr_high & (1u<<16))
+       if (vmx_msr & VMX_BASIC_32BIT_PHYS_ADDR_ONLY)
                return -EIO;
 #endif
 
        /* Require Write-Back (WB) memory type for VMCS accesses. */
-       if (((vmx_msr_high >> 18) & 15) != 6)
+       if (VMX_BASIC_VMCS_MEMTYPE(vmx_msr) != VMX_BASIC_MEM_TYPE_WB)
                return -EIO;
 
        rdmsrl(MSR_IA32_VMX_MISC, misc_msr);
 
-       vmcs_conf->size = vmx_msr_high & 0x1fff;
-       vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
+       vmcs_conf->size = VMX_BASIC_VMCS_SIZE(vmx_msr);
+       vmcs_conf->basic_cap = ????(vmx_msr);
 
-       vmcs_conf->revision_id = vmx_msr_low;
+       vmcs_conf->revision_id = (u32)vmx_msr;
 
        vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
        vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
author
#2 lore raw
Quoted 5 lines · Sean Christopherson
Quoted 2 lines

Add IA32_VMX_BASIC MSR bitfield shift macros and use them to define VMX basic information bitfields.

Why? Unless something actually uses the shift independently, just define the BIT_ULL(...) straightaway.

Well, reading "BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |" is hard.

But Lemme remove those shifts not being used now.

Quoted 7 lines
Quoted 3 lines

Add VMX_BASIC_FEATURES and VMX_BASIC_RESERVED_BITS to form a valid bitmask of IA32_VMX_BASIC MSR. As a result, to add a new VMX basic feature bit, just change the 2 new macros in the header file.

Not if a new feature bit lands in the middle of one of the reserved ranges, then the developer will have to update at least three macros, and add a new one. More below.

yes, it is the case for VMX nested exception feature bit.

Quoted 11 lines
Quoted 10 lines

Also replace hardcoded VMX basic numbers with the new VMX basic macros.

---
  arch/x86/include/asm/msr-index.h       | 31 ++++++++++++++++++++------
  arch/x86/kvm/vmx/nested.c              | 10 +++------
  arch/x86/kvm/vmx/vmx.c                 |  2 +-
  tools/arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------
Quoted 4 lines

Please drop the tools/ update, copying kernel headers into tools is a perf tools thing that I want no part of.

https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]

why can't we simply remove tools/arch/x86/include/asm/msr-index.h?

Quoted 29 lines
Quoted 29 lines
  4 files changed, 52 insertions(+), 22 deletions(-)
arch/x86/include/asm/msr-index.h +11 -7
index 1d111350197f..4607448ff805 100644
@@ -1084,13 +1084,30 @@
  #define MSR_IA32_VMX_PROCBASED_CTLS3	0x00000492
 
  /* VMX_BASIC bits and bitmasks */
-#define VMX_BASIC_VMCS_SIZE_SHIFT	32
-#define VMX_BASIC_TRUE_CTLS		(1ULL << 55)
-#define VMX_BASIC_64		0x0001000000000000LLU
-#define VMX_BASIC_MEM_TYPE_SHIFT	50
-#define VMX_BASIC_MEM_TYPE_MASK	0x003c000000000000LLU
-#define VMX_BASIC_MEM_TYPE_WB	6LLU
-#define VMX_BASIC_INOUT		0x0040000000000000LLU
+#define VMX_BASIC_VMCS_SIZE_SHIFT		32
+#define VMX_BASIC_ALWAYS_0			BIT_ULL(31)
+#define VMX_BASIC_RESERVED_RANGE_1		GENMASK_ULL(47, 45)
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT	48
+#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY		BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT)
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT	49
+#define VMX_BASIC_DUAL_MONITOR_TREATMENT	BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT)
+#define VMX_BASIC_MEM_TYPE_SHIFT		50
+#define VMX_BASIC_MEM_TYPE_WB			6LLU
+#define VMX_BASIC_INOUT_SHIFT			54
+#define VMX_BASIC_INOUT				BIT_ULL(VMX_BASIC_INOUT_SHIFT)
Quoted 8 lines

arch/x86/include/asm/msr-index.h line 1102

Quoted 5 lines
arch/x86/include/asm/msr-index.h +5 -0
+#define VMX_BASIC_TRUE_CTLS_SHIFT		55
+#define VMX_BASIC_TRUE_CTLS			BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT)
+#define VMX_BASIC_RESERVED_RANGE_2		GENMASK_ULL(63, 56)
+
+#define VMX_BASIC_FEATURES			\

Maybe VMX_BASIC_FEATURES_MASK to make it more obvious it's a mask of multiple bits?

This is better!

Quoted 14 lines

arch/x86/include/asm/msr-index.h line 1110

Quoted 9 lines
arch/x86/include/asm/msr-index.h +8 -0
+	(VMX_BASIC_DUAL_MONITOR_TREATMENT |	\
+	 VMX_BASIC_INOUT |			\
+	 VMX_BASIC_TRUE_CTLS)
+
+#define VMX_BASIC_RESERVED_BITS			\
+	(VMX_BASIC_ALWAYS_0 |			\
+	 VMX_BASIC_RESERVED_RANGE_1 |		\
+	 VMX_BASIC_RESERVED_RANGE_2)

I don't see any value in defining VMX_BASIC_RESERVED_RANGE_1 and
VMX_BASIC_RESERVED_RANGE_2 separately. Or VMX_BASIC_ALWAYS_0 for the matter.
And I don't think these macros need to go in msr-index.h, e.g. just define them above vmx_restore_vmx_basic() as that's likely going to be the only user, ever.

hmm, I'm overusing macros, better do:

#define VMX_BASIC_RESERVED_BITS			\
	(BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56))

Probably should also move VMX MSR field defs from msr-index.h to a vmx header file.

Quoted 3 lines

And what's really missing is a static_assert() or BUILD_BUG_ON() to ensure that VMX_BASIC_FEATURES doesn't overlap with VMX_BASIC_RESERVED_BITS.

good idea, human beings are not good at such jobs by staring at it.

Quoted 46 lines
Quoted 37 lines
arch/x86/include/asm/msr-index.h +0 -0
  /* Resctrl MSRs: */
  /* - Intel: */
arch/x86/kvm/vmx/nested.c +3 -7
index c5ec0ef51ff7..5280ba944c87 100644
@@ -1203,21 +1203,17 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
 
  static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  {
-	const u64 feature_and_reserved =
-		/* feature (except bit 48; see below) */
-		BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
-		/* reserved */
-		BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  	u64 vmx_basic = vmcs_config.nested.basic;
 
-	if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
+	if (!is_bitwise_subset(vmx_basic, data,
+			       VMX_BASIC_FEATURES | VMX_BASIC_RESERVED_BITS))
  		return -EINVAL;
 
  	/*
  	 * KVM does not emulate a version of VMX that constrains physical
  	 * addresses of VMX structures (e.g. VMCS) to 32-bits.
  	 */
-	if (data & BIT_ULL(48))
+	if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY)
  		return -EINVAL;
 
  	if (vmx_basic_vmcs_revision_id(vmx_basic) !=
arch/x86/kvm/vmx/vmx.c +0 -0
index 72e3943f3693..f597243d6a72 100644

arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2704

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2701,7 +2701,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 
  #ifdef CONFIG_X86_64
  	/* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
-	if (vmx_msr_high & (1u<<16))
+	if (vmx_msr_high & (1u << (VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT - 32)))

In all honestly, I find the existing code easier to read. I'm definitely not saying the existing code is good, but IMO this is at best a wash.

Quoted 2 lines

I would much rather we do something like this and move away from the hi/lo crud entirely:

I ever saw a TDX patch does most of this cleanup.

Quoted 42 lines
arch/x86/kvm/vmx/vmx.c +7 -7
index 86ce9efe6c66..f103980c3d02 100644
@@ -2693,28 +2693,28 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
                 _vmexit_control &= ~x_ctrl;
         }
 
-       rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
+       rdmsrl(MSR_IA32_VMX_BASIC, vmx_msr);
 
         /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
-       if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
+       if ((VMX_BASIC_VMCS_SIZE(vmx_msr) > PAGE_SIZE)
                 return -EIO;
 
  #ifdef CONFIG_X86_64
         /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
-       if (vmx_msr_high & (1u<<16))
+       if (vmx_msr & VMX_BASIC_32BIT_PHYS_ADDR_ONLY)
                 return -EIO;
  #endif
 
         /* Require Write-Back (WB) memory type for VMCS accesses. */
-       if (((vmx_msr_high >> 18) & 15) != 6)
+       if (VMX_BASIC_VMCS_MEMTYPE(vmx_msr) != VMX_BASIC_MEM_TYPE_WB)
                 return -EIO;
 
         rdmsrl(MSR_IA32_VMX_MISC, misc_msr);
 
-       vmcs_conf->size = vmx_msr_high & 0x1fff;
-       vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
+       vmcs_conf->size = VMX_BASIC_VMCS_SIZE(vmx_msr);
+       vmcs_conf->basic_cap = ????(vmx_msr);
 
-       vmcs_conf->revision_id = vmx_msr_low;
+       vmcs_conf->revision_id = (u32)vmx_msr;
 
         vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
         vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Signature · 2 lines
Thanks!
    Xin
#3 lore raw
Quoted 9 lines · Xin Li
Quoted 5 lines · Sean Christopherson
Quoted 2 lines

Add IA32_VMX_BASIC MSR bitfield shift macros and use them to define VMX basic information bitfields.

Why? Unless something actually uses the shift independently, just define the BIT_ULL(...) straightaway.

Well, reading "BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |" is hard.

I wasn't suggesting that, I was suggesting:

#define VMX_BASIC_INOUT BIT_ULL(54)

instead of

#define VMX_BASIC_INOUT BIT_ULL(VMX_BASIC_INOUT_SHIFT)

Defining a shift adds a pointless layer of indirection (if the shift isn't used directly). It's especially problematic when there are a series of definitions. E.g. if I want to know which bit a flag corresponds to, this:

  #define VMX_BASIC_32BIT_PHYS_ADDR_ONLY          BIT_ULL(48)
  #define VMX_BASIC_DUAL_MONITOR_TREATMENT        BIT_ULL(49)
  #define VMX_BASIC_MEM_TYPE(x)                   (((x) & GENMASK_ULL(53, 50)) >> 50)
  #define VMX_BASIC_INOUT                         BIT_ULL(54)
  #define VMX_BASIC_TRUE_CTLS                     BIT_ULL(55)

is much easier for me to process than this

  #define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT    48
  #define VMX_BASIC_32BIT_PHYS_ADDR_ONLY          BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT)
  #define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT  49
  #define VMX_BASIC_DUAL_MONITOR_TREATMENT        BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT)
  #define VMX_BASIC_MEM_TYPE_SHIFT                50
  #define VMX_BASIC_INOUT_SHIFT                   54
  #define VMX_BASIC_INOUT                         BIT_ULL(VMX_BASIC_INOUT_SHIFT)
  #define VMX_BASIC_TRUE_CTLS_SHIFT               55
  #define VMX_BASIC_TRUE_CTLS                     BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT)

and the former also tends to work better for IDEs that support peeking at macro definitions.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
---
  arch/x86/include/asm/msr-index.h       | 31 ++++++++++++++++++++------
  arch/x86/kvm/vmx/nested.c              | 10 +++------
  arch/x86/kvm/vmx/vmx.c                 |  2 +-
  tools/arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------
Quoted 6 lines
Quoted 4 lines

Please drop the tools/ update, copying kernel headers into tools is a perf tools thing that I want no part of.

https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]

why can't we simply remove tools/arch/x86/include/asm/msr-index.h?

That's a question for the tools/perf folks, though I believe the answer is partly that the perf tooling relies on *exactly* matching kernel-internal structures, and so tools/perf doesn't want to rely on installed headers.

Quoted 10 lines
Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+#define VMX_BASIC_RESERVED_BITS			\
+	(VMX_BASIC_ALWAYS_0 |			\
+	 VMX_BASIC_RESERVED_RANGE_1 |		\
+	 VMX_BASIC_RESERVED_RANGE_2)

I don't see any value in defining VMX_BASIC_RESERVED_RANGE_1 and
VMX_BASIC_RESERVED_RANGE_2 separately. Or VMX_BASIC_ALWAYS_0 for the matter.
And I don't think these macros need to go in msr-index.h, e.g. just define them above vmx_restore_vmx_basic() as that's likely going to be the only user, ever.

Quoted 3 lines

hmm, I'm overusing macros, better do:

#define VMX_BASIC_RESERVED_BITS			\
	(BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56))

Please define from high=>low, x86 is little-endian. I.e.

	 (GENMASK_ULL(63, 56) | GENMASK_ULL(47, 45) | BIT_ULL(31))
Quoted 2 lines

Probably should also move VMX MSR field defs from msr-index.h to a vmx header file.

Why bother putting them in a header? As above, it's extremely unlikely anything besides vmx_restore_vmx_basic() will ever care about exactly which bits are reserved.