All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jim Mattson <jmattson@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
	David Hildenbrand <david@redhat.com>,
	kvm list <kvm@vger.kernel.org>
Cc: Jim Mattson <jmattson@google.com>
Subject: [PATCH v6 2/2] kvm: vmx: Raise #UD on unsupported RDRAND
Date: Wed, 23 Aug 2017 16:32:04 -0700	[thread overview]
Message-ID: <20170823233204.101113-2-jmattson@google.com> (raw)
In-Reply-To: <20170823233204.101113-1-jmattson@google.com>

A guest may not be configured to support RDRAND, even when the host
does. If the guest does not support RDRAND, intercept the instruction
and synthesize #UD. Also clear the "allowed-1" bit for RDRAND exiting
in the IA32_VMX_PROCBASED_CTLS2 MSR.

Signed-off-by: Jim Mattson <jmattson@google.com>
---
 arch/x86/kvm/vmx.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 9dd8637c3392..1d59806b315b 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -3668,6 +3668,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 			SECONDARY_EXEC_SHADOW_VMCS |
 			SECONDARY_EXEC_XSAVES |
 			SECONDARY_EXEC_RDSEED |
+			SECONDARY_EXEC_RDRAND |
 			SECONDARY_EXEC_ENABLE_PML |
 			SECONDARY_EXEC_TSC_SCALING |
 			SECONDARY_EXEC_ENABLE_VMFUNC;
@@ -5304,6 +5305,9 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
 	if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDSEED))
 		exec_control &= ~SECONDARY_EXEC_RDSEED;
 
+	if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDRAND))
+		exec_control &= ~SECONDARY_EXEC_RDRAND;
+
 	return exec_control;
 }
 
@@ -8058,6 +8062,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_INVEPT]                  = handle_invept,
 	[EXIT_REASON_INVVPID]                 = handle_invvpid,
 	[EXIT_REASON_RDSEED]                  = handle_invalid_op,
+	[EXIT_REASON_RDRAND]                  = handle_invalid_op,
 	[EXIT_REASON_XSAVES]                  = handle_xsaves,
 	[EXIT_REASON_XRSTORS]                 = handle_xrstors,
 	[EXIT_REASON_PML_FULL]		      = handle_pml_full,
@@ -8992,6 +8997,12 @@ static bool vmx_rdseed_supported(void)
 		SECONDARY_EXEC_RDSEED;
 }
 
+static bool vmx_rdrand_supported(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_RDRAND;
+}
+
 static bool vmx_xsaves_supported(void)
 {
 	return vmcs_config.cpu_based_2nd_exec_ctrl &
@@ -9690,6 +9701,12 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
 		secondary_exec_ctl |= SECONDARY_EXEC_RDSEED;
 	}
 
+	if (vmx_rdrand_supported() &&
+	    !guest_cpuid_has(vcpu, X86_FEATURE_RDRAND)) {
+		nested_vmx_secondary_ctrls_clear(vcpu, SECONDARY_EXEC_RDRAND);
+		secondary_exec_ctl |= SECONDARY_EXEC_RDRAND;
+	}
+
 	if (cpu_has_secondary_exec_ctrls())
 		vmcs_set_secondary_exec_control(secondary_exec_ctl);
 
-- 
2.14.1.342.g6490525c54-goog

      reply	other threads:[~2017-08-23 23:33 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ář
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             ` Jim Mattson [this message]

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=20170823233204.101113-2-jmattson@google.com \
    --to=jmattson@google.com \
    --cc=david@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.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.