VMCS shadowing fix
cover VMCS shadowing fix
64-bit VMCS fields were not being handled properly by the VMCS shadowing code. In particular, some full-width fields (TSC_OFFSET and GUEST_PHYSICAL_ADDRESS) were being shadowed, but accesses to the high 32-bits of these fields were not. Interleaved L1 accesses to the full-width fields and the high 32-bits returned inconsistent results.
To make this cherry-pick easier, I also pulled in three other commits which are in preparation for virtualized VMCS shadowing support. I plan on submitting the patches for virtualized VMCS shadowing in the very near future.
Jim Mattson (4):
kvm: nVMX: Add support for "VMWRITE to any supported field"
kvm: vmx: Introduce vmcs_read_any and vmcs_write_any
kvm: nVMX: Introduce/rename enums for VMCS field encodings
kvm: nVMX: Shadow "high" parts of shadowed 64-bit VMCS fields
arch/x86/kvm/vmx.c | 231 +++++++++++++++++++++++++++-------------------------- 1 file changed, 119 insertions(+), 112 deletions(-)
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
PATCH 1/4 kvm: nVMX: Add support for "VMWRITE to any supported field"
Allow VMWRITE in L1 to modify VM-exit information fields and report this feature in L1's IA32_VMX_MISC MSR.
Note that this feature is a prerequisite for kvm in L1 to use VMCS shadowing, once that feature is available.
--- arch/x86/kvm/vmx.c | 105 ++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 66 deletions(-)
arch/x86/kvm/vmx.c +39 -66
@@ -674,7 +674,7 @@ static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) [number##_HIGH] = VMCS12_OFFSET(name)+4 -static unsigned long shadow_read_only_fields[] = { +static unsigned long shadow_fields[] = {/* * We do NOT shadow fields that are modified when L0 * traps and emulates any vmx instruction (e.g. VMPTRLD,
@@ -695,12 +695,7 @@ static unsigned long shadow_read_only_fields[] = {VM_EXIT_INTR_ERROR_CODE ,EXIT_QUALIFICATION ,GUEST_LINEAR_ADDRESS , -GUEST_PHYSICAL_ADDRESS -}; -static int max_shadow_read_only_fields = -ARRAY_SIZE (shadow_read_only_fields); - -static unsigned long shadow_read_write_fields[] = { +GUEST_PHYSICAL_ADDRESS ,TPR_THRESHOLD ,GUEST_RIP , GUEST_RSP,
@@ -728,10 +723,9 @@ static unsigned long shadow_read_write_fields[] = {HOST_FS_BASE ,HOST_GS_BASE ,HOST_FS_SELECTOR , -HOST_GS_SELECTOR + HOST_GS_SELECTOR, }; -static int max_shadow_read_write_fields = -ARRAY_SIZE (shadow_read_write_fields); +static int max_shadow_fields =ARRAY_SIZE (shadow_fields);static const unsigned short vmcs_field_to_offset_table[] = { FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
@@ -2803,6 +2797,13 @@ static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)VMX_MISC_ACTIVITY_HLT ; vmx->nested.nested_vmx_misc_high =0 ; +/* +* We can emulate "VMWRITE to any supported field," even if +* the hardware doesn't support it. +*/ + vmx->nested.nested_vmx_misc_low |= +MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; +/* * This MSR reports some information about VMX support. We * should return information about the VMX we emulate for the
@@ -3765,10 +3766,8 @@ static void init_vmcs_shadow_fields(void) {int i, j; -/* No checks for read only fields yet */ - -for (i = j =0 ; i < max_shadow_read_write_fields; i++) { -switch (shadow_read_write_fields[i]) { +for (i = j =0 ; i < max_shadow_fields; i++) { +switch (shadow_fields[i]) {case GUEST_BNDCFGS :if (!kvm_mpx_supported ())continue ;
@@ -3778,22 +3777,16 @@ static void init_vmcs_shadow_fields(void) }if (j < i) - shadow_read_write_fields[j] = - shadow_read_write_fields[i]; + shadow_fields[j] = shadow_fields[i]; j++; } - max_shadow_read_write_fields = j; + max_shadow_fields = j;/* shadowed fields guest access without vmexit */ -for (i =0 ; i < max_shadow_read_write_fields; i++) { -clear_bit (shadow_read_write_fields[i], - vmx_vmwrite_bitmap); -clear_bit (shadow_read_write_fields[i], - vmx_vmread_bitmap); +for (i =0 ; i < max_shadow_fields; i++) { +clear_bit (shadow_fields[i], vmx_vmwrite_bitmap); +clear_bit (shadow_fields[i], vmx_vmread_bitmap); } -for (i =0 ; i < max_shadow_read_only_fields; i++) -clear_bit (shadow_read_only_fields[i], - vmx_vmread_bitmap); }static __init int alloc_kvm_area (void )
@@ -7294,14 +7287,13 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)unsigned long field;u64 field_value;struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; -const unsigned long *fields = shadow_read_write_fields; -const int num_fields = max_shadow_read_write_fields; +unsigned long *fields = shadow_fields;preempt_disable ();vmcs_load (shadow_vmcs); -for (i =0 ; i < num_fields; i++) { +for (i =0 ; i < max_shadow_fields; i++) { field = fields[i];switch (vmcs_field_type (field)) {case VMCS_FIELD_TYPE_U16 :
@@ -7331,43 +7323,31 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)static void copy_vmcs12_to_shadow (struct vcpu_vmx *vmx) { -const unsigned long *fields[] = { - shadow_read_write_fields, - shadow_read_only_fields - }; -const int max_fields[] = { - max_shadow_read_write_fields, - max_shadow_read_only_fields - }; -int i, q; +int i;unsigned long field;u64 field_value =0 ;struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; +unsigned long *fields = shadow_fields;vmcs_load (shadow_vmcs); -for (q =0 ; q <ARRAY_SIZE (fields); q++) { -for (i =0 ; i < max_fields[q]; i++) { - field = fields[q][i]; -vmcs12_read_any (&vmx->vcpu, field, &field_value); - -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : -vmcs_write16 (field, (u16 )field_value); -break ; -case VMCS_FIELD_TYPE_U32 : -vmcs_write32 (field, (u32 )field_value); -break ; -case VMCS_FIELD_TYPE_U64 : -vmcs_write64 (field, (u64 )field_value); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : -vmcs_writel (field, (long )field_value); -break ; -default : -WARN_ON (1 ); -break ; - } +for (i =0 ; i < max_shadow_fields; i++) { + field = fields[i]; +vmcs12_read_any (&vmx->vcpu, field, &field_value); + +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : +vmcs_write16 (field, (u16 )field_value); +break ; +case VMCS_FIELD_TYPE_U32 : +vmcs_write32 (field, (u32 )field_value); +break ; +case VMCS_FIELD_TYPE_U64 : +vmcs_write64 (field, (u64 )field_value); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : +vmcs_writel (field, (long )field_value); +break ; } }
@@ -7467,14 +7447,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) } } - field =kvm_register_readl (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); -if (vmcs_field_readonly (field)) { -nested_vmx_failValid (vcpu, -VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT ); -return kvm_skip_emulated_instruction (vcpu); - } -if (vmcs12_write_any (vcpu, field, field_value) <0 ) {nested_vmx_failValid (vcpu,VMXERR_UNSUPPORTED_VMCS_COMPONENT );return kvm_skip_emulated_instruction (vcpu);
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
PATCH 2/4 kvm: vmx: Introduce vmcs_read_any and vmcs_write_any
In reply to Jim MattsonThese functions will be used in virtualizing VMCS shadowing.
--- arch/x86/kvm/vmx.c | 73 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 32 deletions(-)
arch/x86/kvm/vmx.c +41 -32
@@ -7281,6 +7281,27 @@ static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, } +static u64 vmcs_read_any (unsigned long field) +{ +u64 field_value; + +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : + field_value =vmcs_read16 (field); +break ; +case VMCS_FIELD_TYPE_U32 : + field_value =vmcs_read32 (field); +break ; +case VMCS_FIELD_TYPE_U64 : + field_value =vmcs_read64 (field); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : + field_value =vmcs_readl (field); +break ; + } +return field_value; +} +static void copy_shadow_to_vmcs12 (struct vcpu_vmx *vmx) {int i;
@@ -7295,23 +7316,7 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)for (i =0 ; i < max_shadow_fields; i++) { field = fields[i]; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : - field_value =vmcs_read16 (field); -break ; -case VMCS_FIELD_TYPE_U32 : - field_value =vmcs_read32 (field); -break ; -case VMCS_FIELD_TYPE_U64 : - field_value =vmcs_read64 (field); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : - field_value =vmcs_readl (field); -break ; -default : -WARN_ON (1 ); -continue ; - } + field_value =vmcs_read_any (field);vmcs12_write_any (&vmx->vcpu, field, field_value); }
@@ -7321,6 +7326,24 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)preempt_enable (); } +static void vmcs_write_any (unsigned long field,u64 field_value) +{ +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : +vmcs_write16 (field, (u16 )field_value); +break ; +case VMCS_FIELD_TYPE_U32 : +vmcs_write32 (field, (u32 )field_value); +break ; +case VMCS_FIELD_TYPE_U64 : +vmcs_write64 (field, (u64 )field_value); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : +vmcs_writel (field, (long )field_value); +break ; + } +} +static void copy_vmcs12_to_shadow (struct vcpu_vmx *vmx) {int i;
@@ -7334,21 +7357,7 @@ static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)for (i =0 ; i < max_shadow_fields; i++) { field = fields[i];vmcs12_read_any (&vmx->vcpu, field, &field_value); - -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : -vmcs_write16 (field, (u16 )field_value); -break ; -case VMCS_FIELD_TYPE_U32 : -vmcs_write32 (field, (u32 )field_value); -break ; -case VMCS_FIELD_TYPE_U64 : -vmcs_write64 (field, (u64 )field_value); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : -vmcs_writel (field, (long )field_value); -break ; - } +vmcs_write_any (field, field_value); }vmcs_clear (shadow_vmcs);
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
PATCH 3/4 kvm: nVMX: Introduce/rename enums for VMCS field encodings
In reply to Jim MattsonA new vmcs_field_type enum is introduced, which describes the 'type' of a VMCS field. The old vmcs_field_type enum is renamed to vmcs_field_width, since it actually refers to the width rather than the type of a VMCS field.
--- arch/x86/kvm/vmx.c | 63 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 27 deletions(-)
arch/x86/kvm/vmx.c +36 -27
@@ -7197,18 +7197,27 @@ static int handle_vmresume(struct kvm_vcpu *vcpu)return nested_vmx_run (vcpu, false); } +enum vmcs_field_width { +VMCS_FIELD_WIDTH_U16 =0 , +VMCS_FIELD_WIDTH_U64 =1 , +VMCS_FIELD_WIDTH_U32 =2 , +VMCS_FIELD_WIDTH_NATURAL =3 , +VMCS_FIELD_WIDTHS =4 +}; +enum vmcs_field_type { -VMCS_FIELD_TYPE_U16 =0 , -VMCS_FIELD_TYPE_U64 =1 , -VMCS_FIELD_TYPE_U32 =2 , -VMCS_FIELD_TYPE_NATURAL_WIDTH =3 +VMCS_FIELD_TYPE_CONTROL =0 , +VMCS_FIELD_TYPE_DATA =1 , +VMCS_FIELD_TYPE_GUEST =2 , +VMCS_FIELD_TYPE_HOST =3 , +VMCS_FIELD_TYPES =4 }; -static inline int vmcs_field_type (unsigned long field) +static inline int vmcs_field_width (unsigned long field) {if (0x1 & field)/* the *_HIGH fields are all 32 bit */ -return VMCS_FIELD_TYPE_U32 ; -return (field >>13 ) &0x3 ; +return VMCS_FIELD_WIDTH_U32 ; +return (field >>13 ) &0x3 ; }static inline int vmcs_field_readonly (unsigned long field)
@@ -7234,17 +7243,17 @@ static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, p = ((char *)(get_vmcs12 (vcpu))) + offset; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_NATURAL : *ret = *((natural_width *)p);return 0 ; -case VMCS_FIELD_TYPE_U16 : +case VMCS_FIELD_WIDTH_U16 : *ret = *((u16 *)p);return 0 ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : *ret = *((u32 *)p);return 0 ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : *ret = *((u64 *)p);return 0 ;default :
@@ -7261,17 +7270,17 @@ static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,if (offset <0 )return offset; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 : *(u16 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : *(u32 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : *(u64 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL : *(natural_width *)p = field_value;return 0 ;default :
@@ -7285,17 +7294,17 @@ static u64 vmcs_read_any(unsigned long field) {u64 field_value; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 : field_value =vmcs_read16 (field);break ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : field_value =vmcs_read32 (field);break ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : field_value =vmcs_read64 (field);break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL : field_value =vmcs_readl (field);break ; }
@@ -7328,17 +7337,17 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)static void vmcs_write_any (unsigned long field,u64 field_value) { -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 :vmcs_write16 (field, (u16 )field_value);break ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 :vmcs_write32 (field, (u32 )field_value);break ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 :vmcs_write64 (field, (u64 )field_value);break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL :vmcs_writel (field, (long )field_value);break ; }
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
PATCH 4/4 kvm: nVMX: Shadow "high" parts of shadowed 64-bit VMCS fields
In reply to Jim MattsonInconsistencies result from shadowing only accesses to the full 64-bits of a 64-bit VMCS field, but not shadowing accesses to the high 32-bits of the field. The "high" part of a 64-bit field should be shadowed whenever the full 64-bit field is shadowed.
--- arch/x86/kvm/vmx.c | 70 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 27 deletions(-)
arch/x86/kvm/vmx.c +43 -27
@@ -3762,6 +3762,43 @@ static void free_kvm_area(void) } } +enum vmcs_field_width { +VMCS_FIELD_WIDTH_U16 =0 , +VMCS_FIELD_WIDTH_U64 =1 , +VMCS_FIELD_WIDTH_U32 =2 , +VMCS_FIELD_WIDTH_NATURAL =3 , +VMCS_FIELD_WIDTHS =4 +}; + +enum vmcs_field_type { +VMCS_FIELD_TYPE_CONTROL =0 , +VMCS_FIELD_TYPE_DATA =1 , +VMCS_FIELD_TYPE_GUEST =2 , +VMCS_FIELD_TYPE_HOST =3 , +VMCS_FIELD_TYPES =4 +}; + +static inline short encoding (enum vmcs_field_width width, +enum vmcs_field_type type, +unsigned index,bool high) +{ +WARN_ON (index >=1u <<9 ); + index &= (1u <<9 ) -1 ; +return (width <<13 ) | (type <<10 ) | (index <<1 ) | (high ?1 :0 ); +} + +static inline int vmcs_field_width (unsigned long field) +{ +if (0x1 & field)/* the *_HIGH fields are all 32 bit */ +return VMCS_FIELD_WIDTH_U32 ; +return (field >>13 ) &0x3 ; +} + +static inline int vmcs_field_readonly (unsigned long field) +{ +return (((field >>10 ) &0x3 ) ==1 ); +} +static void init_vmcs_shadow_fields (void ) {int i, j;
@@ -3784,8 +3821,14 @@ static void init_vmcs_shadow_fields(void)/* shadowed fields guest access without vmexit */ for (i =0 ; i < max_shadow_fields; i++) { +int width =vmcs_field_width (shadow_fields[i]); +clear_bit (shadow_fields[i], vmx_vmwrite_bitmap);clear_bit (shadow_fields[i], vmx_vmread_bitmap); +if (width ==VMCS_FIELD_WIDTH_U64 ) { +clear_bit (shadow_fields[i] +1 , vmx_vmwrite_bitmap); +clear_bit (shadow_fields[i] +1 , vmx_vmread_bitmap); + } } }
@@ -7197,33 +7240,6 @@ static int handle_vmresume(struct kvm_vcpu *vcpu)return nested_vmx_run (vcpu, false); } -enum vmcs_field_width { -VMCS_FIELD_WIDTH_U16 =0 , -VMCS_FIELD_WIDTH_U64 =1 , -VMCS_FIELD_WIDTH_U32 =2 , -VMCS_FIELD_WIDTH_NATURAL =3 , -VMCS_FIELD_WIDTHS =4 -}; - -enum vmcs_field_type { -VMCS_FIELD_TYPE_CONTROL =0 , -VMCS_FIELD_TYPE_DATA =1 , -VMCS_FIELD_TYPE_GUEST =2 , -VMCS_FIELD_TYPE_HOST =3 , -VMCS_FIELD_TYPES =4 -}; - -static inline int vmcs_field_width (unsigned long field) -{ -if (0x1 & field)/* the *_HIGH fields are all 32 bit */ -return VMCS_FIELD_WIDTH_U32 ; -return (field >>13 ) &0x3 ; -} - -static inline int vmcs_field_readonly (unsigned long field) -{ -return (((field >>10 ) &0x3 ) ==1 ); -}/* * Read a vmcs12 field. Since these can have varying lengths and we return
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
Re: [PATCH 1/4] kvm: nVMX: Add support for "VMWRITE to any supported field"
In reply to Jim MattsonQuoted 21 lines · Jim Mattson
Allow VMWRITE in L1 to modify VM-exit information fields and report this feature in L1's
IA32_VMX_MISCMSR.Note that this feature is a prerequisite for kvm in L1 to use VMCS shadowing, once that feature is available.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 105 ++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 66 deletions(-)arch/x86/kvm/vmx.c +0
-1@@ -7467,14 +7447,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) } } -
arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 7453
Quoted 10 lines
arch/x86/kvm/vmx.c +0
-6field =kvm_register_readl (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); -if (vmcs_field_readonly (field)) { -nested_vmx_failValid (vcpu, -VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT ); -return kvm_skip_emulated_instruction (vcpu); - } -if (vmcs12_write_any (vcpu, field, field_value) <0 ) {nested_vmx_failValid (vcpu,VMXERR_UNSUPPORTED_VMCS_COMPONENT );return kvm_skip_emulated_instruction (vcpu);
vmcs_field_readonly is now unused. With that removed,
Re: [PATCH 2/4] kvm: vmx: Introduce vmcs_read_any and vmcs_write_any
In reply to Jim MattsonQuoted 103 lines · Jim Mattson
These functions will be used in virtualizing VMCS shadowing.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 73 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 32 deletions(-)arch/x86/kvm/vmx.c +40
-26@@ -7281,6 +7281,27 @@ static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, } +static u64 vmcs_read_any (unsigned long field) +{ +u64 field_value; + +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : + field_value =vmcs_read16 (field); +break ; +case VMCS_FIELD_TYPE_U32 : + field_value =vmcs_read32 (field); +break ; +case VMCS_FIELD_TYPE_U64 : + field_value =vmcs_read64 (field); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : + field_value =vmcs_readl (field); +break ; + } +return field_value; +} +static void copy_shadow_to_vmcs12 (struct vcpu_vmx *vmx) {int i;@@ -7295,23 +7316,7 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)for (i =0 ; i < max_shadow_fields; i++) { field = fields[i]; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : - field_value =vmcs_read16 (field); -break ; -case VMCS_FIELD_TYPE_U32 : - field_value =vmcs_read32 (field); -break ; -case VMCS_FIELD_TYPE_U64 : - field_value =vmcs_read64 (field); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : - field_value =vmcs_readl (field); -break ; -default : -WARN_ON (1 ); -continue ; - } + field_value =vmcs_read_any (field);vmcs12_write_any (&vmx->vcpu, field, field_value); }@@ -7321,6 +7326,24 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)preempt_enable (); } +static void vmcs_write_any (unsigned long field,u64 field_value) +{ +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : +vmcs_write16 (field, (u16 )field_value); +break ; +case VMCS_FIELD_TYPE_U32 : +vmcs_write32 (field, (u32 )field_value); +break ; +case VMCS_FIELD_TYPE_U64 : +vmcs_write64 (field, (u64 )field_value); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : +vmcs_writel (field, (long )field_value); +break ; + } +} +static void copy_vmcs12_to_shadow (struct vcpu_vmx *vmx) {int i;@@ -7334,21 +7357,7 @@ static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)for (i =0 ; i < max_shadow_fields; i++) { field = fields[i];vmcs12_read_any (&vmx->vcpu, field, &field_value); - -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : -vmcs_write16 (field, (u16 )field_value); -break ; -case VMCS_FIELD_TYPE_U32 : -vmcs_write32 (field, (u32 )field_value); -break ; -case VMCS_FIELD_TYPE_U64 :
arch/x86/kvm/vmx.c static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) · line 7363
Quoted 10 lines
arch/x86/kvm/vmx.c +1
-6-vmcs_write64 (field, (u64 )field_value); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : -vmcs_writel (field, (long )field_value); -break ; - } +vmcs_write_any (field, field_value); }vmcs_clear (shadow_vmcs);
Re: [PATCH 4/4] kvm: nVMX: Shadow "high" parts of shadowed 64-bit VMCS fields
In reply to Jim MattsonQuoted 44 lines · Jim Mattson
Inconsistencies result from shadowing only accesses to the full 64-bits of a 64-bit VMCS field, but not shadowing accesses to the high 32-bits of the field. The "high" part of a 64-bit field should be shadowed whenever the full 64-bit field is shadowed.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 70 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 27 deletions(-)arch/x86/kvm/vmx.c +26
-0@@ -3762,6 +3762,43 @@ static void free_kvm_area(void) } } +enum vmcs_field_width { +VMCS_FIELD_WIDTH_U16 =0 , +VMCS_FIELD_WIDTH_U64 =1 , +VMCS_FIELD_WIDTH_U32 =2 , +VMCS_FIELD_WIDTH_NATURAL =3 , +VMCS_FIELD_WIDTHS =4 +}; + +enum vmcs_field_type { +VMCS_FIELD_TYPE_CONTROL =0 , +VMCS_FIELD_TYPE_DATA =1 , +VMCS_FIELD_TYPE_GUEST =2 , +VMCS_FIELD_TYPE_HOST =3 , +VMCS_FIELD_TYPES =4 +}; + +static inline short encoding (enum vmcs_field_width width, +enum vmcs_field_type type, +unsigned index,bool high) +{ +WARN_ON (index >=1u <<9 ); + index &= (1u <<9 ) -1 ; +return (width <<13 ) | (type <<10 ) | (index <<1 ) | (high ?1 :0 ); +} + +static inline int vmcs_field_width (unsigned long field)
arch/x86/kvm/vmx.c static void free_kvm_area(void) · line 3800
Quoted 10 lines
arch/x86/kvm/vmx.c +10
-0+{ +if (0x1 & field)/* the *_HIGH fields are all 32 bit */ +return VMCS_FIELD_WIDTH_U32 ; +return (field >>13 ) &0x3 ; +} + +static inline int vmcs_field_readonly (unsigned long field) +{ +return (((field >>10 ) &0x3 ) ==1 ); +}
See review to patch 1; with vmcs_field_readonly removed,
Paolo
Quoted 53 lines
arch/x86/kvm/vmx.c +6
-27static void init_vmcs_shadow_fields (void ) {int i, j;@@ -3784,8 +3821,14 @@ static void init_vmcs_shadow_fields(void)/* shadowed fields guest access without vmexit */ for (i =0 ; i < max_shadow_fields; i++) { +int width =vmcs_field_width (shadow_fields[i]); +clear_bit (shadow_fields[i], vmx_vmwrite_bitmap);clear_bit (shadow_fields[i], vmx_vmread_bitmap); +if (width ==VMCS_FIELD_WIDTH_U64 ) { +clear_bit (shadow_fields[i] +1 , vmx_vmwrite_bitmap); +clear_bit (shadow_fields[i] +1 , vmx_vmread_bitmap); + } } }@@ -7197,33 +7240,6 @@ static int handle_vmresume(struct kvm_vcpu *vcpu)return nested_vmx_run (vcpu, false); } -enum vmcs_field_width { -VMCS_FIELD_WIDTH_U16 =0 , -VMCS_FIELD_WIDTH_U64 =1 , -VMCS_FIELD_WIDTH_U32 =2 , -VMCS_FIELD_WIDTH_NATURAL =3 , -VMCS_FIELD_WIDTHS =4 -}; - -enum vmcs_field_type { -VMCS_FIELD_TYPE_CONTROL =0 , -VMCS_FIELD_TYPE_DATA =1 , -VMCS_FIELD_TYPE_GUEST =2 , -VMCS_FIELD_TYPE_HOST =3 , -VMCS_FIELD_TYPES =4 -}; - -static inline int vmcs_field_width (unsigned long field) -{ -if (0x1 & field)/* the *_HIGH fields are all 32 bit */ -return VMCS_FIELD_WIDTH_U32 ; -return (field >>13 ) &0x3 ; -} - -static inline int vmcs_field_readonly (unsigned long field) -{ -return (((field >>10 ) &0x3 ) ==1 ); -}/* * Read a vmcs12 field. Since these can have varying lengths and we return
Re: [PATCH 3/4] kvm: nVMX: Introduce/rename enums for VMCS field encodings
In reply to Jim MattsonQuoted 132 lines · Jim Mattson
A new
vmcs_field_typeenum is introduced, which describes the 'type' of a VMCS field. The oldvmcs_field_typeenum is renamed tovmcs_field_width, since it actually refers to the width rather than the type of a VMCS field.Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 63 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 27 deletions(-)arch/x86/kvm/vmx.c +34
-25@@ -7197,18 +7197,27 @@ static int handle_vmresume(struct kvm_vcpu *vcpu)return nested_vmx_run (vcpu, false); } +enum vmcs_field_width { +VMCS_FIELD_WIDTH_U16 =0 , +VMCS_FIELD_WIDTH_U64 =1 , +VMCS_FIELD_WIDTH_U32 =2 , +VMCS_FIELD_WIDTH_NATURAL =3 , +VMCS_FIELD_WIDTHS =4 +}; +enum vmcs_field_type { -VMCS_FIELD_TYPE_U16 =0 , -VMCS_FIELD_TYPE_U64 =1 , -VMCS_FIELD_TYPE_U32 =2 , -VMCS_FIELD_TYPE_NATURAL_WIDTH =3 +VMCS_FIELD_TYPE_CONTROL =0 , +VMCS_FIELD_TYPE_DATA =1 , +VMCS_FIELD_TYPE_GUEST =2 , +VMCS_FIELD_TYPE_HOST =3 , +VMCS_FIELD_TYPES =4 }; -static inline int vmcs_field_type (unsigned long field) +static inline int vmcs_field_width (unsigned long field) {if (0x1 & field)/* the *_HIGH fields are all 32 bit */ -return VMCS_FIELD_TYPE_U32 ; -return (field >>13 ) &0x3 ; +return VMCS_FIELD_WIDTH_U32 ; +return (field >>13 ) &0x3 ; }static inline int vmcs_field_readonly (unsigned long field)@@ -7234,17 +7243,17 @@ static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, p = ((char *)(get_vmcs12 (vcpu))) + offset; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_NATURAL : *ret = *((natural_width *)p);return 0 ; -case VMCS_FIELD_TYPE_U16 : +case VMCS_FIELD_WIDTH_U16 : *ret = *((u16 *)p);return 0 ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : *ret = *((u32 *)p);return 0 ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : *ret = *((u64 *)p);return 0 ;default :@@ -7261,17 +7270,17 @@ static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,if (offset <0 )return offset; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 : *(u16 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : *(u32 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : *(u64 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL : *(natural_width *)p = field_value;return 0 ;default :@@ -7285,17 +7294,17 @@ static u64 vmcs_read_any(unsigned long field) {u64 field_value; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 : field_value =vmcs_read16 (field);break ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : field_value =vmcs_read32 (field);break ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : field_value =vmcs_read64 (field);break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL : field_value =vmcs_readl (field);break ; }@@ -7328,17 +7337,17 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)static void vmcs_write_any (unsigned long field,u64 field_value) { -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 :vmcs_write16 (field, (u16 )field_value);break ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 :vmcs_write32 (field, (u32 )field_value);
arch/x86/kvm/vmx.c static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) · line 7353
Quoted 10 lines
arch/x86/kvm/vmx.c +2
-2break ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 :vmcs_write64 (field, (u64 )field_value);break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL :vmcs_writel (field, (long )field_value);break ; }
Paolo Bonzini
maintainerRe: [PATCH 1/4] kvm: nVMX: Add support for "VMWRITE to any supported field"
In reply to Paolo BonziniQuoted 37 lines · Paolo Bonzini
Quoted 21 lines · Jim Mattson
Allow VMWRITE in L1 to modify VM-exit information fields and report this feature in L1's
IA32_VMX_MISCMSR.Note that this feature is a prerequisite for kvm in L1 to use VMCS shadowing, once that feature is available.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 105 ++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 66 deletions(-)arch/x86/kvm/vmx.c +0
-1@@ -7467,14 +7447,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) } } -arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 7453
Quoted 10 lines
arch/x86/kvm/vmx.c +0
-6field =kvm_register_readl (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); -if (vmcs_field_readonly (field)) { -nested_vmx_failValid (vcpu, -VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT ); -return kvm_skip_emulated_instruction (vcpu); - } -if (vmcs12_write_any (vcpu, field, field_value) <0 ) {nested_vmx_failValid (vcpu,VMXERR_UNSUPPORTED_VMCS_COMPONENT );return kvm_skip_emulated_instruction (vcpu);
vmcs_field_readonlyis now unused. With that removed,
Quoted 1 line
Reviewed-by Paolo Bonzini <[email protected]>
Actually, no. The error must be kept if the host has disabled the feature with a KVM_SET_MSR ioctl for MSR_IA32_VMX_MISC.
The upside is that patch 4 is good as is. :)
Also:
Quoted 8 lines
arch/x86/kvm/vmx.c static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) · line 2806
Quoted 8 lines
arch/x86/kvm/vmx.c +7
-0+/* +* We can emulate "VMWRITE to any supported field," even if +* the hardware doesn't support it. +*/ + vmx->nested.nested_vmx_misc_low |= +MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; +
There is already a "vmx->nested.nested_vmx_misc_low |= " statement a couple lines above. Please generalize the comment to something like "We can always emulate these features, even if the hardware doesn't support them".
Paolo
Re: [PATCH 1/4] kvm: nVMX: Add support for "VMWRITE to any supported field"
Quoted 41 lines · Paolo Bonzini
Quoted 37 lines · Paolo Bonzini
Quoted 21 lines · Jim Mattson
Allow VMWRITE in L1 to modify VM-exit information fields and report this feature in L1's
IA32_VMX_MISCMSR.Note that this feature is a prerequisite for kvm in L1 to use VMCS shadowing, once that feature is available.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 105 ++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 66 deletions(-)arch/x86/kvm/vmx.c +0
-1@@ -7467,14 +7447,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) } } -arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 7453
Quoted 10 lines
arch/x86/kvm/vmx.c +0
-6field =kvm_register_readl (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); -if (vmcs_field_readonly (field)) { -nested_vmx_failValid (vcpu, -VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT ); -return kvm_skip_emulated_instruction (vcpu); - } -if (vmcs12_write_any (vcpu, field, field_value) <0 ) {nested_vmx_failValid (vcpu,VMXERR_UNSUPPORTED_VMCS_COMPONENT );return kvm_skip_emulated_instruction (vcpu);
vmcs_field_readonlyis now unused. With that removed,
Quoted 4 lines
Quoted 1 line
Reviewed-by Paolo Bonzini <[email protected]>Actually, no. The error must be kept if the host has disabled the feature with a
KVM_SET_MSRioctl forMSR_IA32_VMX_MISC.
Supporting both settings of this feature bit is a bit more complicated.
Quoted 1 line
The upside is that patch 4 is good as is. :)
Not quite, but close.
Quoted 17 lines
Also:
Quoted 8 lines
arch/x86/kvm/vmx.c static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) · line 2806
Quoted 8 lines
arch/x86/kvm/vmx.c +7
-0+/* +* We can emulate "VMWRITE to any supported field," even if +* the hardware doesn't support it. +*/ + vmx->nested.nested_vmx_misc_low |= +MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; +There is already a "
vmx->nested.nested_vmx_misc_low|= " statement a couple lines above. Please generalize the comment to something like "We can always emulate these features, even if the hardware doesn't support them".Paolo
Paolo Bonzini
maintainerRe: [PATCH 1/4] kvm: nVMX: Add support for "VMWRITE to any supported field"
Quoted 42 lines · Jim Mattson
Quoted 41 lines · Paolo Bonzini
Quoted 37 lines · Paolo Bonzini
Quoted 21 lines · Jim Mattson
Allow VMWRITE in L1 to modify VM-exit information fields and report this feature in L1's
IA32_VMX_MISCMSR.Note that this feature is a prerequisite for kvm in L1 to use VMCS shadowing, once that feature is available.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 105 ++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 66 deletions(-)arch/x86/kvm/vmx.c +0
-1@@ -7467,14 +7447,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) } } -arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 7453
Quoted 10 lines
arch/x86/kvm/vmx.c +0
-6field =kvm_register_readl (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); -if (vmcs_field_readonly (field)) { -nested_vmx_failValid (vcpu, -VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT ); -return kvm_skip_emulated_instruction (vcpu); - } -if (vmcs12_write_any (vcpu, field, field_value) <0 ) {nested_vmx_failValid (vcpu,VMXERR_UNSUPPORTED_VMCS_COMPONENT );return kvm_skip_emulated_instruction (vcpu);
vmcs_field_readonlyis now unused. With that removed,
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Reviewed-by Paolo Bonzini <[email protected]>Actually, no. The error must be kept if the host has disabled the feature with a
KVM_SET_MSRioctl forMSR_IA32_VMX_MISC.Supporting both settings of this feature bit is a bit more complicated.
Right, you have to set up the vmwrite bitmap correctly. But actually it should be a couple lines of code and your patch would be much simpler, because you keep the RW and RO field lists separate.
After your changes here, vmx_vmread_bitmap and vmx_vmwrite_bitmap have always the same content. Instead, it should be possible for the read and write bitmap to point to the same address. KVM_SET_MSR can do:
if (enable_shadow_vmcs) {
if (/* L1 has vmwrite to all fields */)
vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
else
vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
}And in vmx_vcpu_setup,
- vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
+ vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
so that the default is to expose the feature.
Paolo
Quoted 21 lines
Quoted 1 line
The upside is that patch 4 is good as is. :)
Not quite, but close.
Quoted 17 lines
Also:
Quoted 8 lines
arch/x86/kvm/vmx.c static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) · line 2806
Quoted 8 lines
arch/x86/kvm/vmx.c +7
-0+/* +* We can emulate "VMWRITE to any supported field," even if +* the hardware doesn't support it. +*/ + vmx->nested.nested_vmx_misc_low |= +MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; +There is already a "
vmx->nested.nested_vmx_misc_low|= " statement a couple lines above. Please generalize the comment to something like "We can always emulate these features, even if the hardware doesn't support them".Paolo
Re: [PATCH 1/4] kvm: nVMX: Add support for "VMWRITE to any supported field"
Quoted 60 lines · Paolo Bonzini
Quoted 42 lines · Jim Mattson
Quoted 41 lines · Paolo Bonzini
Quoted 37 lines · Paolo Bonzini
Quoted 21 lines · Jim Mattson
Allow VMWRITE in L1 to modify VM-exit information fields and report this feature in L1's
IA32_VMX_MISCMSR.Note that this feature is a prerequisite for kvm in L1 to use VMCS shadowing, once that feature is available.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 105 ++++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 66 deletions(-)arch/x86/kvm/vmx.c +0
-1@@ -7467,14 +7447,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) } } -arch/x86/kvm/vmx.c static int handle_vmwrite(struct kvm_vcpu *vcpu) · line 7453
Quoted 10 lines
arch/x86/kvm/vmx.c +0
-6field =kvm_register_readl (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); -if (vmcs_field_readonly (field)) { -nested_vmx_failValid (vcpu, -VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT ); -return kvm_skip_emulated_instruction (vcpu); - } -if (vmcs12_write_any (vcpu, field, field_value) <0 ) {nested_vmx_failValid (vcpu,VMXERR_UNSUPPORTED_VMCS_COMPONENT );return kvm_skip_emulated_instruction (vcpu);
vmcs_field_readonlyis now unused. With that removed,Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Reviewed-by Paolo Bonzini <[email protected]>Actually, no. The error must be kept if the host has disabled the feature with a
KVM_SET_MSRioctl forMSR_IA32_VMX_MISC.Supporting both settings of this feature bit is a bit more complicated.
Right, you have to set up the vmwrite bitmap correctly. But actually it should be a couple lines of code and your patch would be much simpler, because you keep the RW and RO field lists separate.
After your changes here,
vmx_vmread_bitmapandvmx_vmwrite_bitmaphave always the same content. Instead, it should be possible for the read and write bitmap to point to the same address.KVM_SET_MSRcan do:
Quoted 7 lines
if (enable_shadow_vmcs) {
if (/* L1 has vmwrite to all fields */)
vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
else
vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
}Actually, KVM_SET_MSR can only disable the feature.
Quoted 6 lines
And in
vmx_vcpu_setup,(quoted diff) +1
-1- vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); + vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));so that the default is to expose the feature.
Yes, but copy_shadow_to_vmcs12() also needs to walk the "read-only" fields when they are read/write. Dealing with the two arrays of indices is awkward. Let me return to this later. I'll fix up patch 4 to deal with the code as it stands today.
Quoted 24 lines
Paolo
Quoted 21 lines
Quoted 1 line
The upside is that patch 4 is good as is. :)
Not quite, but close.
Quoted 17 lines
Also:
Quoted 8 lines
arch/x86/kvm/vmx.c static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) · line 2806
Quoted 8 lines
arch/x86/kvm/vmx.c +7
-0+/* +* We can emulate "VMWRITE to any supported field," even if +* the hardware doesn't support it. +*/ + vmx->nested.nested_vmx_misc_low |= +MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; +There is already a "
vmx->nested.nested_vmx_misc_low|= " statement a couple lines above. Please generalize the comment to something like "We can always emulate these features, even if the hardware doesn't support them".Paolo
PATCH v2 1/1 kvm: nVMX: Shadow "high" parts of shadowed 64-bit VMCS fields
In reply to Paolo BonziniInconsistencies result from shadowing only accesses to the full 64-bits of a 64-bit VMCS field, but not shadowing accesses to the high 32-bits of the field. The "high" part of a 64-bit field should be shadowed whenever the full 64-bit field is shadowed.
--- arch/x86/kvm/vmx.c | 60 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-)
arch/x86/kvm/vmx.c +34 -26
@@ -3761,6 +3761,25 @@ static void free_kvm_area(void) } } +enum vmcs_field_type { +VMCS_FIELD_TYPE_U16 =0 , +VMCS_FIELD_TYPE_U64 =1 , +VMCS_FIELD_TYPE_U32 =2 , +VMCS_FIELD_TYPE_NATURAL_WIDTH =3 +}; + +static inline int vmcs_field_type (unsigned long field) +{ +if (0x1 & field)/* the *_HIGH fields are all 32 bit */ +return VMCS_FIELD_TYPE_U32 ; +return (field >>13 ) &0x3 ; +} + +static inline int vmcs_field_readonly (unsigned long field) +{ +return (((field >>10 ) &0x3 ) ==1 ); +} +static void init_vmcs_shadow_fields (void ) {int i, j;
@@ -3786,14 +3805,22 @@ static void init_vmcs_shadow_fields(void)/* shadowed fields guest access without vmexit */ for (i =0 ; i < max_shadow_read_write_fields; i++) { -clear_bit (shadow_read_write_fields[i], - vmx_vmwrite_bitmap); -clear_bit (shadow_read_write_fields[i], - vmx_vmread_bitmap); +unsigned long field = shadow_read_write_fields[i]; + +clear_bit (field, vmx_vmwrite_bitmap); +clear_bit (field, vmx_vmread_bitmap); +if (vmcs_field_type (field) ==VMCS_FIELD_TYPE_U64 ) { +clear_bit (field +1 , vmx_vmwrite_bitmap); +clear_bit (field +1 , vmx_vmread_bitmap); + } + } +for (i =0 ; i < max_shadow_read_only_fields; i++) { +unsigned long field = shadow_read_only_fields[i]; + +clear_bit (field, vmx_vmread_bitmap); +if (vmcs_field_type (field) ==VMCS_FIELD_TYPE_U64 ) +clear_bit (field +1 , vmx_vmread_bitmap); } -for (i =0 ; i < max_shadow_read_only_fields; i++) -clear_bit (shadow_read_only_fields[i], - vmx_vmread_bitmap); }static __init int alloc_kvm_area (void )
@@ -7204,25 +7231,6 @@ static int handle_vmresume(struct kvm_vcpu *vcpu)return nested_vmx_run (vcpu, false); } -enum vmcs_field_type { -VMCS_FIELD_TYPE_U16 =0 , -VMCS_FIELD_TYPE_U64 =1 , -VMCS_FIELD_TYPE_U32 =2 , -VMCS_FIELD_TYPE_NATURAL_WIDTH =3 -}; - -static inline int vmcs_field_type (unsigned long field) -{ -if (0x1 & field)/* the *_HIGH fields are all 32 bit */ -return VMCS_FIELD_TYPE_U32 ; -return (field >>13 ) &0x3 ; -} - -static inline int vmcs_field_readonly (unsigned long field) -{ -return (((field >>10 ) &0x3 ) ==1 ); -} -/* * Read a vmcs12 field. Since these can have varying lengths and we return * one type, we chose the biggest type (u64) and zero-extend the return value
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
Re: [PATCH 2/4] kvm: vmx: Introduce vmcs_read_any and vmcs_write_any
In reply to Paolo BonziniPlease abandon this change for now.
Quoted 118 lines · Paolo Bonzini
Quoted 103 lines · Jim Mattson
These functions will be used in virtualizing VMCS shadowing.
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 73 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 32 deletions(-)arch/x86/kvm/vmx.c +40
-26@@ -7281,6 +7281,27 @@ static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, } +static u64 vmcs_read_any (unsigned long field) +{ +u64 field_value; + +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : + field_value =vmcs_read16 (field); +break ; +case VMCS_FIELD_TYPE_U32 : + field_value =vmcs_read32 (field); +break ; +case VMCS_FIELD_TYPE_U64 : + field_value =vmcs_read64 (field); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : + field_value =vmcs_readl (field); +break ; + } +return field_value; +} +static void copy_shadow_to_vmcs12 (struct vcpu_vmx *vmx) {int i;@@ -7295,23 +7316,7 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)for (i =0 ; i < max_shadow_fields; i++) { field = fields[i]; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : - field_value =vmcs_read16 (field); -break ; -case VMCS_FIELD_TYPE_U32 : - field_value =vmcs_read32 (field); -break ; -case VMCS_FIELD_TYPE_U64 : - field_value =vmcs_read64 (field); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : - field_value =vmcs_readl (field); -break ; -default : -WARN_ON (1 ); -continue ; - } + field_value =vmcs_read_any (field);vmcs12_write_any (&vmx->vcpu, field, field_value); }@@ -7321,6 +7326,24 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)preempt_enable (); } +static void vmcs_write_any (unsigned long field,u64 field_value) +{ +switch (vmcs_field_type (field)) { +case VMCS_FIELD_TYPE_U16 : +vmcs_write16 (field, (u16 )field_value); +break ; +case VMCS_FIELD_TYPE_U32 : +vmcs_write32 (field, (u32 )field_value); +break ; +case VMCS_FIELD_TYPE_U64 : +vmcs_write64 (field, (u64 )field_value); +break ; +case VMCS_FIELD_TYPE_NATURAL_WIDTH : +vmcs_writel (field, (long )field_value); +break ; + } +} +static void copy_vmcs12_to_shadow (struct vcpu_vmx *vmx) {int i;@@ -7334,21 +7357,7 @@ static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)for (i =0 ; i < max_shadow_fields; i++) { field = fields[i];vmcs12_read_any (&vmx->vcpu, field, &field_value); - -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : -vmcs_write16 (field, (u16 )field_value); -break ; -case VMCS_FIELD_TYPE_U32 : -vmcs_write32 (field, (u32 )field_value); -break ; -case VMCS_FIELD_TYPE_U64 :arch/x86/kvm/vmx.c static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) · line 7363
Quoted 10 lines
arch/x86/kvm/vmx.c +1
-6-vmcs_write64 (field, (u64 )field_value); -break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : -vmcs_writel (field, (long )field_value); -break ; - } +vmcs_write_any (field, field_value); }vmcs_clear (shadow_vmcs);Reviewed-by Paolo Bonzini <[email protected]>
Re: [PATCH 3/4] kvm: nVMX: Introduce/rename enums for VMCS field encodings
In reply to Paolo BonziniPlease abandon this change for now.
Quoted 147 lines · Paolo Bonzini
Quoted 132 lines · Jim Mattson
A new
vmcs_field_typeenum is introduced, which describes the 'type' of a VMCS field. The oldvmcs_field_typeenum is renamed tovmcs_field_width, since it actually refers to the width rather than the type of a VMCS field.Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx.c | 63 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 27 deletions(-)arch/x86/kvm/vmx.c +34
-25@@ -7197,18 +7197,27 @@ static int handle_vmresume(struct kvm_vcpu *vcpu)return nested_vmx_run (vcpu, false); } +enum vmcs_field_width { +VMCS_FIELD_WIDTH_U16 =0 , +VMCS_FIELD_WIDTH_U64 =1 , +VMCS_FIELD_WIDTH_U32 =2 , +VMCS_FIELD_WIDTH_NATURAL =3 , +VMCS_FIELD_WIDTHS =4 +}; +enum vmcs_field_type { -VMCS_FIELD_TYPE_U16 =0 , -VMCS_FIELD_TYPE_U64 =1 , -VMCS_FIELD_TYPE_U32 =2 , -VMCS_FIELD_TYPE_NATURAL_WIDTH =3 +VMCS_FIELD_TYPE_CONTROL =0 , +VMCS_FIELD_TYPE_DATA =1 , +VMCS_FIELD_TYPE_GUEST =2 , +VMCS_FIELD_TYPE_HOST =3 , +VMCS_FIELD_TYPES =4 }; -static inline int vmcs_field_type (unsigned long field) +static inline int vmcs_field_width (unsigned long field) {if (0x1 & field)/* the *_HIGH fields are all 32 bit */ -return VMCS_FIELD_TYPE_U32 ; -return (field >>13 ) &0x3 ; +return VMCS_FIELD_WIDTH_U32 ; +return (field >>13 ) &0x3 ; }static inline int vmcs_field_readonly (unsigned long field)@@ -7234,17 +7243,17 @@ static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, p = ((char *)(get_vmcs12 (vcpu))) + offset; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_NATURAL : *ret = *((natural_width *)p);return 0 ; -case VMCS_FIELD_TYPE_U16 : +case VMCS_FIELD_WIDTH_U16 : *ret = *((u16 *)p);return 0 ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : *ret = *((u32 *)p);return 0 ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : *ret = *((u64 *)p);return 0 ;default :@@ -7261,17 +7270,17 @@ static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,if (offset <0 )return offset; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 : *(u16 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : *(u32 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : *(u64 *)p = field_value;return 0 ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL : *(natural_width *)p = field_value;return 0 ;default :@@ -7285,17 +7294,17 @@ static u64 vmcs_read_any(unsigned long field) {u64 field_value; -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 : field_value =vmcs_read16 (field);break ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 : field_value =vmcs_read32 (field);break ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 : field_value =vmcs_read64 (field);break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL : field_value =vmcs_readl (field);break ; }@@ -7328,17 +7337,17 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)static void vmcs_write_any (unsigned long field,u64 field_value) { -switch (vmcs_field_type (field)) { -case VMCS_FIELD_TYPE_U16 : +switch (vmcs_field_width (field)) { +case VMCS_FIELD_WIDTH_U16 :vmcs_write16 (field, (u16 )field_value);break ; -case VMCS_FIELD_TYPE_U32 : +case VMCS_FIELD_WIDTH_U32 :vmcs_write32 (field, (u32 )field_value);arch/x86/kvm/vmx.c static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) · line 7353
Quoted 10 lines
arch/x86/kvm/vmx.c +2
-2break ; -case VMCS_FIELD_TYPE_U64 : +case VMCS_FIELD_WIDTH_U64 :vmcs_write64 (field, (u64 )field_value);break ; -case VMCS_FIELD_TYPE_NATURAL_WIDTH : +case VMCS_FIELD_WIDTH_NATURAL :vmcs_writel (field, (long )field_value);break ; }Reviewed-by Paolo Bonzini <[email protected]>
Paolo Bonzini
maintainerRe: [PATCH 1/4] kvm: nVMX: Add support for "VMWRITE to any supported field"
In reply to Jim MattsonQuoted 7 lines
Quoted 4 lines
After your changes here,
vmx_vmread_bitmapandvmx_vmwrite_bitmaphave always the same content. Instead, it should be possible for the read and write bitmap to point to the same address.Yes, but
copy_shadow_to_vmcs12()also needs to walk the "read-only" fields when they are read/write. Dealing with the two arrays of indices is awkward.
It's just the code that's already in copy_vmcs12_to_shadow. If you first introduce vmcs_read/write_any, the two nested for loops aren't bad. Or, make the "for (i = 0; i < num_fields; i++)" loop its own function, like copy_vmcs12_fields_to_shadow and copy_shadow_fields_to_vmcs12, and call it twice.
Paolo