Skip to content

Commit a807b78

Browse files
esposembonzini
authored andcommitted
kvm: vmx: Add IA32_FLUSH_CMD guest support
Expose IA32_FLUSH_CMD to the guest if the guest CPUID enumerates support for this MSR. As with IA32_PRED_CMD, permission for unintercepted writes to this MSR will be granted to the guest after the first non-zero write. Co-developed-by: Jim Mattson <jmattson@google.com> Signed-off-by: Jim Mattson <jmattson@google.com> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com> Message-Id: <20230201132905.549148-2-eesposit@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent fbc722a commit a807b78

2 files changed

Lines changed: 46 additions & 25 deletions

File tree

arch/x86/kvm/vmx/nested.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
654654
nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
655655
MSR_IA32_PRED_CMD, MSR_TYPE_W);
656656

657+
nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
658+
MSR_IA32_FLUSH_CMD, MSR_TYPE_W);
659+
657660
kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false);
658661

659662
vmx->nested.force_msr_bitmap_recalc = false;

arch/x86/kvm/vmx/vmx.c

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,39 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated
21332133
return debugctl;
21342134
}
21352135

2136+
static int vmx_set_msr_ia32_cmd(struct kvm_vcpu *vcpu,
2137+
struct msr_data *msr_info,
2138+
bool guest_has_feat, u64 cmd,
2139+
int x86_feature_bit)
2140+
{
2141+
if (!msr_info->host_initiated && !guest_has_feat)
2142+
return 1;
2143+
2144+
if (!(msr_info->data & ~cmd))
2145+
return 1;
2146+
if (!boot_cpu_has(x86_feature_bit))
2147+
return 1;
2148+
if (!msr_info->data)
2149+
return 0;
2150+
2151+
wrmsrl(msr_info->index, cmd);
2152+
2153+
/*
2154+
* For non-nested:
2155+
* When it's written (to non-zero) for the first time, pass
2156+
* it through.
2157+
*
2158+
* For nested:
2159+
* The handling of the MSR bitmap for L2 guests is done in
2160+
* nested_vmx_prepare_msr_bitmap. We should not touch the
2161+
* vmcs02.msr_bitmap here since it gets completely overwritten
2162+
* in the merging.
2163+
*/
2164+
vmx_disable_intercept_for_msr(vcpu, msr_info->index, MSR_TYPE_W);
2165+
2166+
return 0;
2167+
}
2168+
21362169
/*
21372170
* Writes msr value into the appropriate "register".
21382171
* Returns 0 on success, non-0 otherwise.
@@ -2286,31 +2319,16 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
22862319
return 1;
22872320
goto find_uret_msr;
22882321
case MSR_IA32_PRED_CMD:
2289-
if (!msr_info->host_initiated &&
2290-
!guest_has_pred_cmd_msr(vcpu))
2291-
return 1;
2292-
2293-
if (data & ~PRED_CMD_IBPB)
2294-
return 1;
2295-
if (!boot_cpu_has(X86_FEATURE_IBPB))
2296-
return 1;
2297-
if (!data)
2298-
break;
2299-
2300-
wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2301-
2302-
/*
2303-
* For non-nested:
2304-
* When it's written (to non-zero) for the first time, pass
2305-
* it through.
2306-
*
2307-
* For nested:
2308-
* The handling of the MSR bitmap for L2 guests is done in
2309-
* nested_vmx_prepare_msr_bitmap. We should not touch the
2310-
* vmcs02.msr_bitmap here since it gets completely overwritten
2311-
* in the merging.
2312-
*/
2313-
vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W);
2322+
ret = vmx_set_msr_ia32_cmd(vcpu, msr_info,
2323+
guest_has_pred_cmd_msr(vcpu),
2324+
PRED_CMD_IBPB,
2325+
X86_FEATURE_IBPB);
2326+
break;
2327+
case MSR_IA32_FLUSH_CMD:
2328+
ret = vmx_set_msr_ia32_cmd(vcpu, msr_info,
2329+
guest_cpuid_has(vcpu, X86_FEATURE_FLUSH_L1D),
2330+
L1D_FLUSH,
2331+
X86_FEATURE_FLUSH_L1D);
23142332
break;
23152333
case MSR_IA32_CR_PAT:
23162334
if (!kvm_pat_valid(data))

0 commit comments

Comments
 (0)