Skip to contentSkip to conversation

KVM: nVMX: Disallow access to vmcs12 fields that aren't supported by "hardware"

PATCH Superseded Sean Christopherson 2 participants 10 messages Jan 9

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v3 cover KVM: nVMX: Disallow access to vmcs12 fields that aren't supported by "hardware"

Disallow accesses to vmcs12 fields that are defined by KVM, but are unsupported in the current incarnation of KVM, e.g. due to lack of hardware support for the underlying VMCS fields.

The primary motivation is to avoid having to carry the same logic for shadowed VMCS fields, which can't play nice with unsupported fields since VMREAD/VMWRITE will fail when attempting to transfer state between vmcs12 and the shadow VMCS.

v3:
- Collect reviews. [Xin, Chao]
- Actually filter out vmcs12 fields when configuring shadow VMCS. [Chao]
- Move VMX MSR configuration into nested_vmx_hardware_setup().
- Add ENC_TO_VMCS12_IDX. [Xiaoyao]
- Use a Xiaoyao's crazy double ROL16 math. :-) [Xiaoyao, obviously]

v2:
- https://lore.kernel.org/all/[email protected]
- Name the array of KVM-defined fields kvm_supported_vmcs12_field_offsets, e.g. so that it's no confused with what's supported by hardware. [Xin]
- Combine encodings in switch statements for fields shared fate. [Xin]
- Drop the extern declaration of supported_vmcs12_field_offsets. [Chao]
- Handle GUEST_INTR_STATUS in cpu_has_vmcs12_field() and add a patch to drop the custom handling from init_vmcs_shadow_fields(). [Chao]

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (4):
KVM: nVMX: Setup VMX MSRs on loading CPU during
nested_vmx_hardware_setup()
KVM: VMX: Add a wrapper around ROL16() to get a vmcs12 from a field
encoding
KVM: nVMX: Disallow access to vmcs12 fields that aren't supported by
"hardware"
KVM: nVMX: Remove explicit filtering of GUEST_INTR_STATUS from shadow
VMCS fields

 arch/x86/kvm/vmx/hyperv_evmcs.c |  2 +-
 arch/x86/kvm/vmx/hyperv_evmcs.h |  2 +-
 arch/x86/kvm/vmx/nested.c       | 28 +++++++------
 arch/x86/kvm/vmx/vmcs.h         |  9 ++++
 arch/x86/kvm/vmx/vmcs12.c       | 74 +++++++++++++++++++++++++++++++--
 arch/x86/kvm/vmx/vmcs12.h       |  8 ++--
 arch/x86/kvm/vmx/vmx.c          |  2 -
 7 files changed, 101 insertions(+), 24 deletions(-)

base-commit: 9448598b22c50c8a5bb77a9103e2d49f134c9578

Signature · 1 line
2.52.0.457.g6b5491de43-goog
author maintainer
#1 lore raw

PATCH v3 1/4 KVM: nVMX: Setup VMX MSRs on loading CPU during nested_vmx_hardware_setup()

Move the call to nested_vmx_setup_ctls_msrs() from vmx_hardware_setup() to nested_vmx_hardware_setup() so that the nested code can deal with ordering dependencies without having to straddle vmx_hardware_setup() and nested_vmx_hardware_setup(). Specifically, an upcoming change will sanitize the vmcs12 fields based on hardware support, and that code needs to run _before_ the MSRs are configured, because the lovely vmcs_enum MSR depends on the max support vmcs12 field.

No functional change intended.

---
 arch/x86/kvm/vmx/nested.c | 2 ++
 arch/x86/kvm/vmx/vmx.c    | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +2 -0
index 6137e5307d0f..61113ead3d7b 100644
@@ -7407,6 +7407,8 @@ __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
 {
 	int i;
 
+	nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept);
+
 	if (!cpu_has_vmx_shadow_vmcs())
 		enable_shadow_vmcs = 0;
 	if (enable_shadow_vmcs) {
arch/x86/kvm/vmx/vmx.c +0 -2
index 6b96f7aea20b..5bb67566e43a 100644
@@ -8670,8 +8670,6 @@ __init int vmx_hardware_setup(void)
 	 * can hide/show features based on kvm_cpu_cap_has().
 	 */
 	if (nested) {
-		nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept);
-
 		r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
 		if (r)
 			return r;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] KVM: nVMX: Setup VMX MSRs on loading CPU during nested_vmx_hardware_setup()

Quoted 11 lines · Sean Christopherson

Move the call to nested_vmx_setup_ctls_msrs() from vmx_hardware_setup() to nested_vmx_hardware_setup() so that the nested code can deal with ordering dependencies without having to straddle vmx_hardware_setup() and nested_vmx_hardware_setup(). Specifically, an upcoming change will sanitize the vmcs12 fields based on hardware support, and that code needs to run _before_ the MSRs are configured, because the lovely vmcs_enum MSR depends on the max support vmcs12 field.

No functional change intended.

Quoted 31 lines
---
  arch/x86/kvm/vmx/nested.c | 2 ++
  arch/x86/kvm/vmx/vmx.c    | 2 --
  2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +2 -0
index 6137e5307d0f..61113ead3d7b 100644
@@ -7407,6 +7407,8 @@ __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
  {
  	int i;
 
+	nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept);
+
  	if (!cpu_has_vmx_shadow_vmcs())
  		enable_shadow_vmcs = 0;
  	if (enable_shadow_vmcs) {
arch/x86/kvm/vmx/vmx.c +0 -2
index 6b96f7aea20b..5bb67566e43a 100644
@@ -8670,8 +8670,6 @@ __init int vmx_hardware_setup(void)
  	 * can hide/show features based on kvm_cpu_cap_has().
  	 */
  	if (nested) {
-		nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept);
-
  		r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
  		if (r)
  			return r;
author maintainer
#3 lore raw

PATCH v3 2/4 KVM: VMX: Add a wrapper around ROL16() to get a vmcs12 from a field encoding

In reply to Sean Christopherson

Add a wrapper macro, ENC_TO_VMCS12_IDX(), to get a vmcs12 index given a field encoding in anticipation of add a macro to get from a vmcs12 index back to the field encoding. And because open coding ROL16(n, 6) everywhere is gross.

No functional change intended.

---
 arch/x86/kvm/vmx/hyperv_evmcs.c | 2 +-
 arch/x86/kvm/vmx/hyperv_evmcs.h | 2 +-
 arch/x86/kvm/vmx/vmcs.h         | 1 +
 arch/x86/kvm/vmx/vmcs12.c       | 4 ++--
 arch/x86/kvm/vmx/vmcs12.h       | 2 +-
 5 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/hyperv_evmcs.c +1 -1
index 904bfcd1519b..cc728c9a3de5 100644
@@ -7,7 +7,7 @@
 #include "hyperv_evmcs.h"
 
 #define EVMCS1_OFFSET(x) offsetof(struct hv_enlightened_vmcs, x)
-#define EVMCS1_FIELD(number, name, clean_field)[ROL16(number, 6)] = \
+#define EVMCS1_FIELD(number, name, clean_field)[ENC_TO_VMCS12_IDX(number)] = \
 		{EVMCS1_OFFSET(name), clean_field}
 
 const struct evmcs_field vmcs_field_to_evmcs_1[] = {
arch/x86/kvm/vmx/hyperv_evmcs.h +1 -1
index 6536290f4274..fc7c4e7bd1bf 100644
@@ -130,7 +130,7 @@ static __always_inline int evmcs_field_offset(unsigned long field,
 					      u16 *clean_field)
 {
 	const struct evmcs_field *evmcs_field;
-	unsigned int index = ROL16(field, 6);
+	unsigned int index = ENC_TO_VMCS12_IDX(field);
 
 	if (unlikely(index >= nr_evmcs_1_fields))
 		return -ENOENT;
arch/x86/kvm/vmx/vmcs.h +1 -0
index b25625314658..9aa204c87661 100644
@@ -12,6 +12,7 @@
 #include "capabilities.h"
 
 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
+#define ENC_TO_VMCS12_IDX(enc) ROL16(enc, 6)
 
 struct vmcs_hdr {
 	u32 revision_id:31;
arch/x86/kvm/vmx/vmcs12.c +2 -2
index 4233b5ca9461..c2ac9e1a50b3 100644
@@ -4,10 +4,10 @@
 #include "vmcs12.h"
 
 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
-#define FIELD(number, name)	[ROL16(number, 6)] = VMCS12_OFFSET(name)
+#define FIELD(number, name)	[ENC_TO_VMCS12_IDX(number)] = VMCS12_OFFSET(name)
 #define FIELD64(number, name)						\
 	FIELD(number, name),						\
-	[ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
+	[ENC_TO_VMCS12_IDX(number##_HIGH)] = VMCS12_OFFSET(name) + sizeof(u32)
 
 const unsigned short vmcs12_field_offsets[] = {
 	FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
arch/x86/kvm/vmx/vmcs12.h +1 -1
index 4ad6b16525b9..7a5fdd9b27ba 100644
@@ -385,7 +385,7 @@ static inline short get_vmcs12_field_offset(unsigned long field)
 	if (field >> 15)
 		return -ENOENT;
 
-	index = ROL16(field, 6);
+	index = ENC_TO_VMCS12_IDX(field);
 	if (index >= nr_vmcs12_fields)
 		return -ENOENT;
 
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Reviewed-by on v3 2/4
#4 lore raw

Re: [PATCH v3 2/4] KVM: VMX: Add a wrapper around ROL16() to get a vmcs12 from a field encoding

Quoted 2 lines · Sean Christopherson

Add a wrapper macro, ENC_TO_VMCS12_IDX(), to get a vmcs12 index given a field encoding in anticipation of add a macro to get from a vmcs12 index

                                     ^

s/add/adding ?

Quoted 7 lines

back to the field encoding. And because open coding ROL16(n, 6) everywhere is gross.

No functional change intended.

Quoted 76 lines
---
  arch/x86/kvm/vmx/hyperv_evmcs.c | 2 +-
  arch/x86/kvm/vmx/hyperv_evmcs.h | 2 +-
  arch/x86/kvm/vmx/vmcs.h         | 1 +
  arch/x86/kvm/vmx/vmcs12.c       | 4 ++--
  arch/x86/kvm/vmx/vmcs12.h       | 2 +-
  5 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/hyperv_evmcs.c +1 -1
index 904bfcd1519b..cc728c9a3de5 100644
@@ -7,7 +7,7 @@
  #include "hyperv_evmcs.h"
 
  #define EVMCS1_OFFSET(x) offsetof(struct hv_enlightened_vmcs, x)
-#define EVMCS1_FIELD(number, name, clean_field)[ROL16(number, 6)] = \
+#define EVMCS1_FIELD(number, name, clean_field)[ENC_TO_VMCS12_IDX(number)] = \
  		{EVMCS1_OFFSET(name), clean_field}
 
  const struct evmcs_field vmcs_field_to_evmcs_1[] = {
arch/x86/kvm/vmx/hyperv_evmcs.h +1 -1
index 6536290f4274..fc7c4e7bd1bf 100644
@@ -130,7 +130,7 @@ static __always_inline int evmcs_field_offset(unsigned long field,
  					      u16 *clean_field)
  {
  	const struct evmcs_field *evmcs_field;
-	unsigned int index = ROL16(field, 6);
+	unsigned int index = ENC_TO_VMCS12_IDX(field);
 
  	if (unlikely(index >= nr_evmcs_1_fields))
  		return -ENOENT;
arch/x86/kvm/vmx/vmcs.h +1 -0
index b25625314658..9aa204c87661 100644
@@ -12,6 +12,7 @@
  #include "capabilities.h"
 
  #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
+#define ENC_TO_VMCS12_IDX(enc) ROL16(enc, 6)
 
  struct vmcs_hdr {
  	u32 revision_id:31;
arch/x86/kvm/vmx/vmcs12.c +2 -2
index 4233b5ca9461..c2ac9e1a50b3 100644
@@ -4,10 +4,10 @@
  #include "vmcs12.h"
 
  #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
-#define FIELD(number, name)	[ROL16(number, 6)] = VMCS12_OFFSET(name)
+#define FIELD(number, name)	[ENC_TO_VMCS12_IDX(number)] = VMCS12_OFFSET(name)
  #define FIELD64(number, name)						\
  	FIELD(number, name),						\
-	[ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
+	[ENC_TO_VMCS12_IDX(number##_HIGH)] = VMCS12_OFFSET(name) + sizeof(u32)
 
  const unsigned short vmcs12_field_offsets[] = {
  	FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
arch/x86/kvm/vmx/vmcs12.h +1 -1
index 4ad6b16525b9..7a5fdd9b27ba 100644
@@ -385,7 +385,7 @@ static inline short get_vmcs12_field_offset(unsigned long field)
  	if (field >> 15)
  		return -ENOENT;
 
-	index = ROL16(field, 6);
+	index = ENC_TO_VMCS12_IDX(field);
  	if (index >= nr_vmcs12_fields)
  		return -ENOENT;
 
author maintainer
#5 lore raw

PATCH v3 3/4 KVM: nVMX: Disallow access to vmcs12 fields that aren't supported by "hardware"

In reply to Sean Christopherson

Disallow access (VMREAD/VMWRITE), both emulated and via a shadow VMCS, to VMCS fields that the loaded incarnation of KVM doesn't support, e.g. due to lack of hardware support, as a middle ground between allowing access to any vmcs12 field defined by KVM (current behavior) and gating access based on the userspace-defined vCPU model (the most functionally correct, but very costly, implementation).

Disallowing access to unsupported fields helps a tiny bit in terms of closing the virtualization hole (see below), but the main motivation is to avoid having to weed out unsupported fields when synchronizing between vmcs12 and a shadow VMCS. Because shadow VMCS accesses are done via VMREAD and VMWRITE, KVM _must_ filter out unsupported fields (or eat VMREAD/VMWRITE failures), and filtering out just shadow VMCS fields is about the same amount of effort, and arguably much more confusing.

As a bonus, this also fixes a KVM-Unit-Test failure bug when running on _hardware_ without support for TSC Scaling, which fails with the same signature as the bug fixed by commit ba1f82456ba8 ("KVM: nVMX: Dynamically compute max VMCS index for vmcs12"):

FAIL: VMX_VMCS_ENUM.MAX_INDEX expected: 19, actual: 17

Dynamically computing the max VMCS index only resolved the issue where KVM was hardcoding max index, but for CPUs with TSC Scaling, that was "good enough".

---
 arch/x86/kvm/vmx/nested.c | 15 +++++----
 arch/x86/kvm/vmx/vmcs.h   |  8 +++++
 arch/x86/kvm/vmx/vmcs12.c | 70 +++++++++++++++++++++++++++++++++++++--
 arch/x86/kvm/vmx/vmcs12.h |  6 ++--
 4 files changed, 89 insertions(+), 10 deletions(-)
arch/x86/kvm/vmx/nested.c +9 -6
index 61113ead3d7b..ac7a17560c8f 100644
@@ -111,6 +111,9 @@ static void init_vmcs_shadow_fields(void)
 			  field <= GUEST_TR_AR_BYTES,
 			  "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
 
+		if (get_vmcs12_field_offset(field) < 0)
+			continue;
+
 		/*
 		 * PML and the preemption timer can be emulated, but the
 		 * processor cannot vmwrite to fields that don't exist
@@ -7074,12 +7077,6 @@ void nested_vmx_set_vmcs_shadowing_bitmap(void)
 	}
 }
 
-/*
- * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6.  Undo
- * that madness to get the encoding for comparison.
- */
-#define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10)))
-
 static u64 nested_vmx_calc_vmcs_enum_msr(void)
 {
 	/*
@@ -7407,6 +7404,12 @@ __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
 {
 	int i;
 
+	/*
+	 * Note!  The set of supported vmcs12 fields is consumed by both VMX
+	 * MSR and shadow VMCS setup.
+	 */
+	nested_vmx_setup_vmcs12_fields();
+
 	nested_vmx_setup_ctls_msrs(&vmcs_config, vmx_capability.ept);
 
 	if (!cpu_has_vmx_shadow_vmcs())
arch/x86/kvm/vmx/vmcs.h +8 -0
index 9aa204c87661..66d747e265b1 100644
@@ -11,7 +11,15 @@
 
 #include "capabilities.h"
 
+/*
+ * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6 as a very
+ * rudimentary compression of the range of indices.  The compression ratio is
+ * good enough to allow KVM to use a (very sparsely populated) array without
+ * wasting too much memory, while the "algorithm" is fast enough to be used to
+ * lookup vmcs12 fields on-demand, e.g. for emulation.
+ */
 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
+#define VMCS12_IDX_TO_ENC(idx) ROL16(idx, 10)
 #define ENC_TO_VMCS12_IDX(enc) ROL16(enc, 6)
 
 struct vmcs_hdr {
arch/x86/kvm/vmx/vmcs12.c +68 -2

79 lines not on this page · raw message

arch/x86/kvm/vmx/vmcs12.h +4 -2
index 7a5fdd9b27ba..21cd1b75e4fd 100644
@@ -374,8 +374,10 @@ static inline void vmx_check_vmcs12_offsets(void)
 	CHECK_OFFSET(guest_pml_index, 996);
 }
 
-extern const unsigned short vmcs12_field_offsets[];
-extern const unsigned int nr_vmcs12_fields;
+extern u16 vmcs12_field_offsets[] __ro_after_init;
+extern unsigned int nr_vmcs12_fields __ro_after_init;
+
+void __init nested_vmx_setup_vmcs12_fields(void);
 
 static inline short get_vmcs12_field_offset(unsigned long field)
 {
Signature · 1 line
2.52.0.457.g6b5491de43-goog
#6 lore raw

arch/x86/kvm/vmx/nested.c static void init_vmcs_shadow_fields(void) · line 116

Quoted 11 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +3 -0
index 61113ead3d7b..ac7a17560c8f 100644
@@ -111,6 +111,9 @@ static void init_vmcs_shadow_fields(void)
  			  field <= GUEST_TR_AR_BYTES,
  			  "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
 
+		if (get_vmcs12_field_offset(field) < 0)
+			continue;
+

why shadow_read_only_fields[] doesn't need such guard?

IIUC, copy_vmcs12_to_shadow() will VMWRITE shadowed readonly field even if it doesn't exist on the hardware?

author maintainer
#7 lore raw
Quoted 14 lines · Xiaoyao Li

arch/x86/kvm/vmx/nested.c static void init_vmcs_shadow_fields(void) · line 115

Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +3 -0
index 61113ead3d7b..ac7a17560c8f 100644
@@ -111,6 +111,9 @@ static void init_vmcs_shadow_fields(void)
  			  field <= GUEST_TR_AR_BYTES,
  			  "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
+		if (get_vmcs12_field_offset(field) < 0)
+			continue;
+

why shadow_read_only_fields[] doesn't need such guard?

Quoted 2 lines

IIUC, copy_vmcs12_to_shadow() will VMWRITE shadowed readonly field even if it doesn't exist on the hardware?

Because I fixated on the existing checks and didn't look at the first for-loop.

This time around I'll test by hacking in shadowed fields arbitrary shadow fields.

author maintainer
#8 lore raw
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Xiaoyao Li

arch/x86/kvm/vmx/nested.c static void init_vmcs_shadow_fields(void) · line 115

Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +3 -0
index 61113ead3d7b..ac7a17560c8f 100644
@@ -111,6 +111,9 @@ static void init_vmcs_shadow_fields(void)
  			  field <= GUEST_TR_AR_BYTES,
  			  "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
+		if (get_vmcs12_field_offset(field) < 0)
+			continue;
+

why shadow_read_only_fields[] doesn't need such guard?

Quoted 6 lines
Quoted 2 lines

IIUC, copy_vmcs12_to_shadow() will VMWRITE shadowed readonly field even if it doesn't exist on the hardware?

Because I fixated on the existing checks and didn't look at the first for-loop.

This time around I'll test by hacking in shadowed fields arbitrary shadow fields.

And with the RO fields handled, the below doesn't explode (I verified there failures aplenty if either of the RO or RW checks are commented out).

arch/x86/include/asm/vmx.h +4 -0
index c85c50019523..7d9bedd06afd 100644
@@ -262,8 +262,12 @@ enum vmcs_field {
        SHARED_EPT_POINTER              = 0x0000203C,
        PID_POINTER_TABLE               = 0x00002042,
        PID_POINTER_TABLE_HIGH          = 0x00002043,
+       INJECTED_EVENT_DATA             = 0x00002052,
+       INJECTED_EVENT_DATA_HIGH        = 0x00002053,
        GUEST_PHYSICAL_ADDRESS          = 0x00002400,
        GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
+       ORIGINAL_EVENT_DATA             = 0x00002404,
+       ORIGINAL_EVENT_DATA_HIGH        = 0x00002405,
        VMCS_LINK_POINTER               = 0x00002800,
        VMCS_LINK_POINTER_HIGH          = 0x00002801,
        GUEST_IA32_DEBUGCTL             = 0x00002802,
arch/x86/kvm/vmx/vmcs12.c +8 -0
index 1ebe67c384ad..7952d58fb2d8 100644
@@ -157,6 +157,8 @@ static const u16 kvm_supported_vmcs12_field_offsets[] __initconst = {
        FIELD(HOST_S_CET, host_s_cet),
        FIELD(HOST_SSP, host_ssp),
        FIELD(HOST_INTR_SSP_TABLE, host_ssp_tbl),
+       FIELD64(INJECTED_EVENT_DATA, injected_event_data),
+       FIELD64(ORIGINAL_EVENT_DATA, original_event_data),
 };
 
 u16 vmcs12_field_offsets[ARRAY_SIZE(kvm_supported_vmcs12_field_offsets)] __ro_after_init;
@@ -204,6 +206,12 @@ static __init bool cpu_has_vmcs12_field(unsigned int idx)
        case HOST_INTR_SSP_TABLE:
                return cpu_has_load_cet_ctrl();
 
+       case ORIGINAL_EVENT_DATA:
+       case ORIGINAL_EVENT_DATA_HIGH:
+       case INJECTED_EVENT_DATA:
+       case INJECTED_EVENT_DATA_HIGH:
+               return false;
+
        /* KVM always emulates PML and the VMX preemption timer in software. */
        case GUEST_PML_INDEX:
        case VMX_PREEMPTION_TIMER_VALUE:
arch/x86/kvm/vmx/vmcs12.h +3 -0
index 21cd1b75e4fd..56565722f527 100644
@@ -191,6 +191,9 @@ struct __packed vmcs12 {
        u16 host_gs_selector;
        u16 host_tr_selector;
        u16 guest_pml_index;
+
+       u64 injected_event_data;
+       u64 original_event_data;
 };
 
 /*
arch/x86/kvm/vmx/vmcs_shadow_fields.h +5 -0
index cad128d1657b..d23ffedaf25b 100644
@@ -75,5 +75,10 @@ SHADOW_FIELD_RW(HOST_GS_BASE, host_gs_base)
 SHADOW_FIELD_RO(GUEST_PHYSICAL_ADDRESS, guest_physical_address)
 SHADOW_FIELD_RO(GUEST_PHYSICAL_ADDRESS_HIGH, guest_physical_address)
 
+SHADOW_FIELD_RO(ORIGINAL_EVENT_DATA, original_event_data)
+SHADOW_FIELD_RO(ORIGINAL_EVENT_DATA_HIGH, original_event_data)
+SHADOW_FIELD_RW(INJECTED_EVENT_DATA, injected_event_data)
+SHADOW_FIELD_RW(INJECTED_EVENT_DATA_HIGH, injected_event_data)
+
 #undef SHADOW_FIELD_RO
 #undef SHADOW_FIELD_RW
author maintainer
#9 lore raw

PATCH v3 4/4 KVM: nVMX: Remove explicit filtering of GUEST_INTR_STATUS from shadow VMCS fields

In reply to Sean Christopherson

Drop KVM's filtering of GUEST_INTR_STATUS when generating the shadow VMCS bitmap now that KVM drops GUEST_INTR_STATUS from the set of supported vmcs12 fields if the field isn't supported by hardware, and initialization of the shadow VMCS fields omits unsupported vmcs12 fields.

Note, there is technically a small functional change here, as the vmcs12 filtering only requires support for Virtual Interrupt Delivery, whereas the shadow VMCS code being removed required "full" APICv support, i.e. required Virtual Interrupt Delivery *and* APIC Register Virtualizaton *and* Posted Interrupt support.

Opportunistically tweak the comment to more precisely explain why the PML and VMX preemption timer fields need to be explicitly checked.

---
 arch/x86/kvm/vmx/nested.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/nested.c +4 -7
index ac7a17560c8f..3ef4d7ab5723 100644
@@ -115,9 +115,10 @@ static void init_vmcs_shadow_fields(void)
 			continue;
 
 		/*
-		 * PML and the preemption timer can be emulated, but the
-		 * processor cannot vmwrite to fields that don't exist
-		 * on bare metal.
+		 * KVM emulates PML and the VMX preemption timer irrespective
+		 * of hardware support, but shadowing their related VMCS fields
+		 * requires hardware support as the CPU will reject VMWRITEs to
+		 * fields that don't exist.
 		 */
 		switch (field) {
 		case GUEST_PML_INDEX:
@@ -128,10 +129,6 @@ static void init_vmcs_shadow_fields(void)
 			if (!cpu_has_vmx_preemption_timer())
 				continue;
 			break;
-		case GUEST_INTR_STATUS:
-			if (!cpu_has_vmx_apicv())
-				continue;
-			break;
 		default:
 			break;
 		}
Signature · 1 line
2.52.0.457.g6b5491de43-goog