Skip to content

Commit 34a83de

Browse files
committed
KVM: x86: Use MTRR macros to define possible MTRR MSR ranges
Use the MTRR macros to identify the ranges of possible MTRR MSRs instead of bounding the ranges with a mismash of open coded values and unrelated MSR indices. Carving out the gap for the machine check MSRs in particular is confusing, as it's easy to incorrectly think the case statement handles MCE MSRs instead of skipping them. Drop the range-based funneling of MSRs between the end of the MCE MSRs and MTRR_DEF_TYPE, i.e. 0x2A0-0x2FF, and instead handle MTTR_DEF_TYPE as the one-off case that it is. Extract PAT (0x277) as well in anticipation of dropping PAT "handling" from the MTRR code. Keep the range-based handling for the variable+fixed MTRRs even though capturing unknown MSRs 0x214-0x24F is arguably "wrong". There is a gap in the fixed MTRRs, 0x260-0x267, i.e. the MTRR code needs to filter out unknown MSRs anyways, and using a single range generates marginally better code for the big switch statement. Reviewed-by: Kai Huang <kai.huang@intel.com> Link: https://lore.kernel.org/r/20230511233351.635053-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9ae38b4 commit 34a83de

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

arch/x86/kvm/mtrr.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ static bool is_mtrr_base_msr(unsigned int msr)
3434
static struct kvm_mtrr_range *var_mtrr_msr_to_range(struct kvm_vcpu *vcpu,
3535
unsigned int msr)
3636
{
37-
int index = (msr - 0x200) / 2;
37+
int index = (msr - MTRRphysBase_MSR(0)) / 2;
3838

3939
return &vcpu->arch.mtrr_state.var_ranges[index];
4040
}
4141

4242
static bool msr_mtrr_valid(unsigned msr)
4343
{
4444
switch (msr) {
45-
case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
45+
case MTRRphysBase_MSR(0) ... MTRRphysMask_MSR(KVM_NR_VAR_MTRR - 1):
4646
case MSR_MTRRfix64K_00000:
4747
case MSR_MTRRfix16K_80000:
4848
case MSR_MTRRfix16K_A0000:
@@ -88,7 +88,8 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
8888
}
8989

9090
/* variable MTRRs */
91-
WARN_ON(!(msr >= 0x200 && msr < 0x200 + 2 * KVM_NR_VAR_MTRR));
91+
WARN_ON(!(msr >= MTRRphysBase_MSR(0) &&
92+
msr <= MTRRphysMask_MSR(KVM_NR_VAR_MTRR - 1)));
9293

9394
mask = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
9495
if ((msr & 1) == 0) {

arch/x86/kvm/x86.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3702,8 +3702,9 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
37023702
return 1;
37033703
}
37043704
break;
3705-
case 0x200 ... MSR_IA32_MC0_CTL2 - 1:
3706-
case MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) ... 0x2ff:
3705+
case MSR_IA32_CR_PAT:
3706+
case MTRRphysBase_MSR(0) ... MSR_MTRRfix4K_F8000:
3707+
case MSR_MTRRdefType:
37073708
return kvm_mtrr_set_msr(vcpu, msr, data);
37083709
case MSR_IA32_APICBASE:
37093710
return kvm_set_apic_base(vcpu, msr_info);
@@ -4110,9 +4111,10 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
41104111
msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset;
41114112
break;
41124113
}
4114+
case MSR_IA32_CR_PAT:
41134115
case MSR_MTRRcap:
4114-
case 0x200 ... MSR_IA32_MC0_CTL2 - 1:
4115-
case MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) ... 0x2ff:
4116+
case MTRRphysBase_MSR(0) ... MSR_MTRRfix4K_F8000:
4117+
case MSR_MTRRdefType:
41164118
return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
41174119
case 0xcd: /* fsb frequency */
41184120
msr_info->data = 3;

0 commit comments

Comments
 (0)