Skip to content

Commit ebda79e

Browse files
committed
KVM: x86: Add helper to query if variable MTRR MSR is base (versus mask)
Add a helper to query whether a variable MTRR MSR is a base versus as mask MSR. Replace the unnecessarily complex math with a simple check on bit 0; base MSRs are even, mask MSRs are odd. Link: https://lore.kernel.org/r/20230511233351.635053-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7aeae02 commit ebda79e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

arch/x86/kvm/mtrr.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#define IA32_MTRR_DEF_TYPE_FE (1ULL << 10)
2626
#define IA32_MTRR_DEF_TYPE_TYPE_MASK (0xff)
2727

28+
static bool is_mtrr_base_msr(unsigned int msr)
29+
{
30+
/* MTRR base MSRs use even numbers, masks use odd numbers. */
31+
return !(msr & 0x1);
32+
}
33+
2834
static bool msr_mtrr_valid(unsigned msr)
2935
{
3036
switch (msr) {
@@ -342,10 +348,9 @@ static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
342348
{
343349
struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
344350
struct kvm_mtrr_range *tmp, *cur;
345-
int index, is_mtrr_mask;
351+
int index;
346352

347353
index = (msr - 0x200) / 2;
348-
is_mtrr_mask = msr - 0x200 - 2 * index;
349354
cur = &mtrr_state->var_ranges[index];
350355

351356
/* remove the entry if it's in the list. */
@@ -356,7 +361,7 @@ static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
356361
* Set all illegal GPA bits in the mask, since those bits must
357362
* implicitly be 0. The bits are then cleared when reading them.
358363
*/
359-
if (!is_mtrr_mask)
364+
if (is_mtrr_base_msr(msr))
360365
cur->base = data;
361366
else
362367
cur->mask = data | kvm_vcpu_reserved_gpa_bits_raw(vcpu);
@@ -418,11 +423,8 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
418423
else if (msr == MSR_IA32_CR_PAT)
419424
*pdata = vcpu->arch.pat;
420425
else { /* Variable MTRRs */
421-
int is_mtrr_mask;
422-
423426
index = (msr - 0x200) / 2;
424-
is_mtrr_mask = msr - 0x200 - 2 * index;
425-
if (!is_mtrr_mask)
427+
if (is_mtrr_base_msr(msr))
426428
*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
427429
else
428430
*pdata = vcpu->arch.mtrr_state.var_ranges[index].mask;

0 commit comments

Comments
 (0)