From: "Radim Krčmář" <rkrcmar@redhat.com>
To: Jim Mattson <jmattson@google.com>
Cc: David Hildenbrand <david@redhat.com>, kvm@vger.kernel.org
Subject: Re: [PATCH v5] kvm: vmx: Raise #UD on unsupported RDSEED
Date: Mon, 21 Aug 2017 22:32:06 +0200 [thread overview]
Message-ID: <20170821203205.GA31206@flask> (raw)
In-Reply-To: <20170821192640.30817-1-jmattson@google.com>
2017-08-21 12:26-0700, Jim Mattson:
> A guest may not be configured to support RDSEED, even when the host
> does. If the guest does not support RDSEED, intercept the instruction
> and synthesize #UD. Also clear the "allowed-1" bit for RDSEED exiting
> in the IA32_VMX_PROCBASED_CTLS2 MSR.
(RDRAND looks the same.)
> Signed-off-by: Jim Mattson <jmattson@google.com>
> ---
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> @@ -5298,6 +5299,9 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
> if (!enable_pml)
> exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
>
> + if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDSEED))
> + exec_control &= ~SECONDARY_EXEC_RDSEED;
> +
> return exec_control;
> }
>
> @@ -9665,6 +9682,24 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
> }
> }
>
> + if (vmx_rdseed_supported()) {
> + bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
> +
> + if (rdseed_enabled)
> + secondary_exec_ctl &= ~SECONDARY_EXEC_RDSEED;
All other CPUID-controlled features use vmx_cpuid_update(), but I would
actually prefer to have it in vmx_secondary_exec_control. In any case,
combining those two is weird.
> + else
> + secondary_exec_ctl |= SECONDARY_EXEC_RDSEED;
The feature can never be unset here, so we can have just the first
branch,
thanks.
> +
> + if (nested) {
> + if (rdseed_enabled)
> + vmx->nested.nested_vmx_secondary_ctls_high |=
> + SECONDARY_EXEC_RDSEED;
> + else
> + vmx->nested.nested_vmx_secondary_ctls_high &=
> + ~SECONDARY_EXEC_RDSEED;
> + }
> + }
I think it would be nicer to generalize that pattern:
(We can call it after updating the MSRs too.)
---8<---
Subject: [PATCH] KVM: nVMX: refactor secondary_ctls_high updates
We should not enable a VMX feature if its instruction is not in guest
CPUID or not provided by hardware. The change allows us to easily add
more features.
RDTSCP will always get configured with CPUID, so there is no need to set
it from the beginning, just like INVPCID.
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
arch/x86/kvm/vmx.c | 46 +++++++++++++++++++++-------------------------
1 file changed, 21 insertions(+), 25 deletions(-)
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 2b92c2de2b3a..7e2b33e0948d 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -2810,7 +2810,6 @@ static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
vmx->nested.nested_vmx_secondary_ctls_high &=
SECONDARY_EXEC_RDRAND | SECONDARY_EXEC_RDSEED |
SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
- SECONDARY_EXEC_RDTSCP |
SECONDARY_EXEC_DESC |
SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
SECONDARY_EXEC_APIC_REGISTER_VIRT |
@@ -9623,25 +9622,28 @@ static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
#undef cr4_fixed1_update
}
+/*
+ * Update MSR_IA32_VMX_PROCBASED_CTLS2 according to CPUID. Selected features
+ * are enabled iff they are enabled in CPUID and supported by the host.
+ */
+static void nested_vmx_secondary_ctls_high_update(struct kvm_vcpu *vcpu,
+ u32 host_secondary_exec_ctl)
+{
+ u32 mask = SECONDARY_EXEC_RDTSCP | SECONDARY_EXEC_ENABLE_INVPCID;
+
+ vcpu->vmx.nested.nested_vmx_secondary_ctls_high &= ~mask
+ vcpu->vmx.nested.nested_vmx_secondary_ctls_high |=
+ host_secondary_exec_ctl & mask;
+}
+
static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
{
struct vcpu_vmx *vmx = to_vmx(vcpu);
u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
- if (vmx_rdtscp_supported()) {
- bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
- if (!rdtscp_enabled)
- secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
-
- if (nested) {
- if (rdtscp_enabled)
- vmx->nested.nested_vmx_secondary_ctls_high |=
- SECONDARY_EXEC_RDTSCP;
- else
- vmx->nested.nested_vmx_secondary_ctls_high &=
- ~SECONDARY_EXEC_RDTSCP;
- }
- }
+ if (vmx_rdtscp_supported() &&
+ !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
+ secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
if (vmx_invpcid_supported()) {
/* Exposing INVPCID only when PCID is exposed */
@@ -9653,15 +9655,6 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
}
-
- if (nested) {
- if (invpcid_enabled)
- vmx->nested.nested_vmx_secondary_ctls_high |=
- SECONDARY_EXEC_ENABLE_INVPCID;
- else
- vmx->nested.nested_vmx_secondary_ctls_high &=
- ~SECONDARY_EXEC_ENABLE_INVPCID;
- }
}
if (cpu_has_secondary_exec_ctrls())
@@ -9674,8 +9667,11 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
- if (nested_vmx_allowed(vcpu))
+ if (nested_vmx_allowed(vcpu)) {
nested_vmx_cr_fixed1_bits_update(vcpu);
+ nested_vmx_secondary_ctls_high_update(vcpu, secondary_exec_ctl);
+ }
+
}
static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
--
2.13.3
next prev parent reply other threads:[~2017-08-21 20:32 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-18 18:43 [PATCH] kvm: vmx: Raise #UD on unsupported RDSEED Jim Mattson
2017-08-18 18:45 ` [PATCH v2] " Jim Mattson
2017-08-21 1:49 ` Wanpeng Li
2017-08-21 13:00 ` [PATCH] " David Hildenbrand
2017-08-21 16:37 ` Jim Mattson
2017-08-21 16:50 ` David Hildenbrand
2017-08-21 17:01 ` Jim Mattson
2017-08-21 18:37 ` Jim Mattson
2017-08-21 19:16 ` [PATCH v4] " Jim Mattson
2017-08-21 16:38 ` [PATCH v3] " Jim Mattson
2017-08-21 19:26 ` [PATCH v5] " Jim Mattson
2017-08-21 20:32 ` Radim Krčmář [this message]
2017-08-21 22:03 ` Jim Mattson
2017-08-22 11:21 ` David Hildenbrand
2017-08-22 11:11 ` David Hildenbrand
2017-08-23 21:34 ` Paolo Bonzini
2017-08-23 22:37 ` Jim Mattson
2017-08-23 23:32 ` [PATCH v6 1/2] " Jim Mattson
2017-08-23 23:32 ` [PATCH v6 2/2] kvm: vmx: Raise #UD on unsupported RDRAND Jim Mattson
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=20170821203205.GA31206@flask \
--to=rkrcmar@redhat.com \
--cc=david@redhat.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
/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.