Re: [PATCH v5] kvm: vmx: Raise #UD on unsupported RDSEED

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
> ---
> 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@xxxxxxxxxx>
---
 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




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux