Skip to contentSkip to conversation

Linux Patch 5

PATCH Superseded [email protected] 1 participant 1 message Apr 18, 2018

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 5/7 Linux Patch 5

That is the guest may futz with the IBRS (say it zeros the MD bit). Hence when causing an VMEXIT we MUST apply our MSR value.

However when entering the guest we shall use whatever the guest decided to use.

Also expose the CPUID.7.EDX[31] bit to the guest.

---
v2: Make sure that sure guest view of SPEC_CTRL is exact.
That is - if the guest wrote 0 at the point of VMEXIT
and we set the MSR to 4 (MD bit), then the guest should not see 4 but rather 0.
v3: s/md/mdd/
When exiting the guest we MUST write the higher priv MSR value, not lower.
- s/spec_ctrl_msr_val_on/spec_ctrl_priv/
- s/spec_Ctrl_msr_val_off/spec_ctrl/unpriv/
v3.1: Remove the assembler macro patch from this one. Must have snuck in.

---
 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/kvm/cpuid.c               |  2 +-
 arch/x86/kvm/vmx.c                 | 14 ++++++++------
 3 files changed, 10 insertions(+), 7 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
index 7490798a70e9..874b55f53604 100644
@@ -214,6 +214,7 @@
 
 #define X86_FEATURE_USE_IBPB		( 7*32+21) /* "" Indirect Branch Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW		( 7*32+22) /* "" Use IBRS during runtime firmware calls */
+#define X86_FEATURE_USE_MDD		( 7*32+23) /* "" Use Memory Disambiguation Disable when entering userspace. */
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW		( 8*32+ 0) /* Intel TPR Shadow */
arch/x86/kvm/cpuid.c +1 -1
index b671fc2d0422..0df863018b50 100644
@@ -402,7 +402,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 
 	/* cpuid 7.0.edx*/
 	const u32 kvm_cpuid_7_0_edx_x86_features =
-		F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
+		F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) | F(MDD) |
 		F(ARCH_CAPABILITIES);
 
 	/* all calls to cpuid_count() should be made on the same cpu */
arch/x86/kvm/vmx.c +8 -6
index 657c93409042..960fd2dd53d9 100644
@@ -3270,7 +3270,8 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_MDD))
 			return 1;
 
 		msr_info->data = to_vmx(vcpu)->spec_ctrl;
@@ -3391,11 +3392,12 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
-		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
+		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_MDD))
 			return 1;
 
 		/* The STIBP bit doesn't fault even if it's not advertised */
-		if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
+		if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_MDD))
 			return 1;
 
 		vmx->spec_ctrl = data;
@@ -9466,7 +9468,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	 * is no need to worry about the conditional branch over the wrmsr
 	 * being speculatively taken.
 	 */
-	if (vmx->spec_ctrl)
+	if (vmx->spec_ctrl || spec_ctrl_unpriv)
 		native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
 
 	vmx->__launched = vmx->loaded_vmcs->launched;
@@ -9605,8 +9607,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
 		vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
 
-	if (vmx->spec_ctrl)
-		native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
+	if (vmx->spec_ctrl || boot_cpu_has(X86_FEATURE_MDD))
+		native_wrmsrl(MSR_IA32_SPEC_CTRL, spec_ctrl_priv);
 
 	/* Eliminate branch target predictions from guest mode */
 	vmexit_fill_RSB();
Signature · 1 line
2.14.3