From: Robert Hoo <robert.hu@linux.intel.com>
To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com,
wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org
Cc: kvm@vger.kernel.org, yu.c.zhang@linux.intel.com,
Robert Hoo <robert.hu@linux.intel.com>
Subject: [PATCH v1 3/5] KVM: x86: nVMX: VMCS12 field's read/write respects field existence bitmap
Date: Tue, 17 Aug 2021 17:31:11 +0800 [thread overview]
Message-ID: <1629192673-9911-4-git-send-email-robert.hu@linux.intel.com> (raw)
In-Reply-To: <1629192673-9911-1-git-send-email-robert.hu@linux.intel.com>
In vmcs12_{read,write}_any(), check the field exist or not. If not, return
failure. Hence their function prototype changed a little accordingly.
In handle_vm{read,write}(), above function's caller, check return value, if
failed, emulate nested vmx fail with instruction error of
VMXERR_UNSUPPORTED_VMCS_COMPONENT.
Signed-off-by: Robert Hoo <robert.hu@linux.intel.com>
Signed-off-by: Yu Zhang <yu.c.zhang@linux.intel.com>
---
arch/x86/kvm/vmx/nested.c | 20 ++++++++++++------
arch/x86/kvm/vmx/vmcs12.h | 43 ++++++++++++++++++++++++++++++---------
2 files changed, 47 insertions(+), 16 deletions(-)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index b8121f8f6d96..9a35953ede22 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -1547,7 +1547,8 @@ static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
for (i = 0; i < max_shadow_read_write_fields; i++) {
field = shadow_read_write_fields[i];
val = __vmcs_readl(field.encoding);
- vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
+ vmcs12_write_any(vmcs12, field.encoding, field.offset, val,
+ vmx->nested.vmcs12_field_existence_bitmap);
}
vmcs_clear(shadow_vmcs);
@@ -1580,8 +1581,9 @@ static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
for (q = 0; q < ARRAY_SIZE(fields); q++) {
for (i = 0; i < max_fields[q]; i++) {
field = fields[q][i];
- val = vmcs12_read_any(vmcs12, field.encoding,
- field.offset);
+ vmcs12_read_any(vmcs12, field.encoding,
+ field.offset, &val,
+ vmx->nested.vmcs12_field_existence_bitmap);
__vmcs_writel(field.encoding, val);
}
}
@@ -5070,7 +5072,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
struct vcpu_vmx *vmx = to_vmx(vcpu);
struct x86_exception e;
unsigned long field;
- u64 value;
+ unsigned long value;
gva_t gva = 0;
short offset;
int len, r;
@@ -5098,7 +5100,10 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
/* Read the field, zero-extended to a u64 value */
- value = vmcs12_read_any(vmcs12, field, offset);
+ r = vmcs12_read_any(vmcs12, field, offset, &value,
+ vmx->nested.vmcs12_field_existence_bitmap);
+ if (r < 0)
+ return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
/*
* Now copy part of this value to register or memory, as requested.
@@ -5223,7 +5228,10 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
value &= 0x1f0ff;
- vmcs12_write_any(vmcs12, field, offset, value);
+ r = vmcs12_write_any(vmcs12, field, offset, value,
+ vmx->nested.vmcs12_field_existence_bitmap);
+ if (r < 0)
+ return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
/*
* Do not track vmcs12 dirty-state if in guest-mode as we actually
diff --git a/arch/x86/kvm/vmx/vmcs12.h b/arch/x86/kvm/vmx/vmcs12.h
index 5c39370dff3c..9ac3d6ac1b6b 100644
--- a/arch/x86/kvm/vmx/vmcs12.h
+++ b/arch/x86/kvm/vmx/vmcs12.h
@@ -413,31 +413,51 @@ static inline short vmcs_field_to_offset(unsigned long field)
#undef ROL16
-static inline u64 vmcs12_read_any(struct vmcs12 *vmcs12, unsigned long field,
- u16 offset)
+static inline int vmcs12_read_any(struct vmcs12 *vmcs12, unsigned long field,
+ u16 offset, unsigned long *value, unsigned long *bitmap)
{
char *p = (char *)vmcs12 + offset;
+ if (unlikely(bitmap == NULL)) {
+ pr_err_once("vmcs12 read: NULL bitmap");
+ return -EINVAL;
+ }
+ if (!test_bit(offset / sizeof(u16), bitmap))
+ return -ENOENT;
+
switch (vmcs_field_width(field)) {
case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
- return *((natural_width *)p);
+ *value = *((natural_width *)p);
+ break;
case VMCS_FIELD_WIDTH_U16:
- return *((u16 *)p);
+ *value = *((u16 *)p);
+ break;
case VMCS_FIELD_WIDTH_U32:
- return *((u32 *)p);
+ *value = *((u32 *)p);
+ break;
case VMCS_FIELD_WIDTH_U64:
- return *((u64 *)p);
+ *value = *((u64 *)p);
+ break;
default:
WARN_ON_ONCE(1);
- return -1;
+ return -ENOENT;
}
+
+ return 0;
}
-static inline void vmcs12_write_any(struct vmcs12 *vmcs12, unsigned long field,
- u16 offset, u64 field_value)
+static inline int vmcs12_write_any(struct vmcs12 *vmcs12, unsigned long field,
+ u16 offset, u64 field_value, unsigned long *bitmap)
{
char *p = (char *)vmcs12 + offset;
+ if (unlikely(bitmap == NULL)) {
+ pr_err_once("%s: NULL bitmap", __func__);
+ return -EINVAL;
+ }
+ if (!test_bit(offset / sizeof(u16), bitmap))
+ return -ENOENT;
+
switch (vmcs_field_width(field)) {
case VMCS_FIELD_WIDTH_U16:
*(u16 *)p = field_value;
@@ -453,8 +473,11 @@ static inline void vmcs12_write_any(struct vmcs12 *vmcs12, unsigned long field,
break;
default:
WARN_ON_ONCE(1);
- break;
+ return -ENOENT;
}
+
+ return 0;
}
+
#endif /* __KVM_X86_VMX_VMCS12_H */
--
2.27.0
next prev parent reply other threads:[~2021-08-17 9:31 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-17 9:31 [PATCH v1 0/5] KVM/x86/nVMX: Add field existence support in VMCS12 Robert Hoo
2021-08-17 9:31 ` [PATCH v1 1/5] KVM: x86: nVMX: Add vmcs12 field existence bitmap in nested_vmx Robert Hoo
2021-10-20 15:10 ` Paolo Bonzini
2021-10-21 12:41 ` Robert Hoo
2021-08-17 9:31 ` [PATCH v1 2/5] KVM: x86: nVMX: Update VMCS12 fields existence when nVMX MSRs are set Robert Hoo
2021-10-20 15:11 ` Paolo Bonzini
2021-10-21 13:08 ` Robert Hoo
2021-08-17 9:31 ` Robert Hoo [this message]
2021-08-17 15:54 ` [PATCH v1 3/5] KVM: x86: nVMX: VMCS12 field's read/write respects field existence bitmap Sean Christopherson
2021-08-18 5:50 ` Robert Hoo
2021-08-18 23:10 ` Sean Christopherson
2021-08-18 23:45 ` Jim Mattson
2021-08-18 23:49 ` Sean Christopherson
2021-08-19 9:58 ` Robert Hoo
2021-09-01 20:42 ` Sean Christopherson
2021-09-03 8:51 ` Robert Hoo
2021-09-03 15:11 ` Sean Christopherson
2021-09-28 10:05 ` Robert Hoo
2021-10-05 16:15 ` Sean Christopherson
2021-10-05 17:32 ` Jim Mattson
2021-10-05 17:59 ` Sean Christopherson
2021-10-05 20:42 ` Jim Mattson
2021-10-05 20:50 ` Sean Christopherson
2021-10-05 22:40 ` Jim Mattson
2021-10-05 23:22 ` Sean Christopherson
2021-10-08 8:23 ` Yu Zhang
2021-10-08 15:09 ` Robert Hoo
2021-10-08 23:49 ` Jim Mattson
2021-10-09 0:05 ` Robert Hoo
2021-10-29 19:53 ` Jim Mattson
2021-11-03 1:31 ` Robert Hoo
2021-11-09 22:33 ` Sean Christopherson
2021-11-10 5:35 ` Yu Zhang
2021-11-18 1:19 ` Sean Christopherson
2021-11-19 7:32 ` Robert Hoo
2021-08-17 9:31 ` [PATCH v1 4/5] KVM: x86: nVMX: Respect vmcs12 field existence when calc vmx_vmcs_enum_msr Robert Hoo
2021-08-17 9:31 ` [PATCH v1 5/5] KVM: x86: nVMX: Ignore user space set value to MSR_IA32_VMX_VMCS_ENUM Robert Hoo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1629192673-9911-4-git-send-email-robert.hu@linux.intel.com \
--to=robert.hu@linux.intel.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=yu.c.zhang@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.