Skip to content

Commit

Permalink
KVM: MTRR: fix memory type handling if MTRR is completely disabled
Browse files Browse the repository at this point in the history
Currently code uses default memory type if MTRR is fully disabled,
fix it by using UC instead.

Signed-off-by: Xiao Guangrong <guangrong.xiao@intel.com>
Tested-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Xiao Guangrong authored and Paolo Bonzini committed Jul 23, 2015
1 parent c5dfd65 commit 10dc331
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion arch/x86/kvm/mtrr.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
}

static u8 mtrr_disabled_type(void)
{
/*
* Intel SDM 11.11.2.2: all MTRRs are disabled when
* IA32_MTRR_DEF_TYPE.E bit is cleared, and the UC
* memory type is applied to all of physical memory.
*/
return MTRR_TYPE_UNCACHABLE;
}

/*
* Three terms are used in the following code:
* - segment, it indicates the address segments covered by fixed MTRRs.
Expand Down Expand Up @@ -434,6 +444,8 @@ struct mtrr_iter {

/* output fields. */
int mem_type;
/* mtrr is completely disabled? */
bool mtrr_disabled;
/* [start, end) is not fully covered in MTRRs? */
bool partial_map;

Expand Down Expand Up @@ -549,7 +561,7 @@ static void mtrr_lookup_var_next(struct mtrr_iter *iter)
static void mtrr_lookup_start(struct mtrr_iter *iter)
{
if (!mtrr_is_enabled(iter->mtrr_state)) {
iter->partial_map = true;
iter->mtrr_disabled = true;
return;
}

Expand All @@ -563,6 +575,7 @@ static void mtrr_lookup_init(struct mtrr_iter *iter,
iter->mtrr_state = mtrr_state;
iter->start = start;
iter->end = end;
iter->mtrr_disabled = false;
iter->partial_map = false;
iter->fixed = false;
iter->range = NULL;
Expand Down Expand Up @@ -656,6 +669,9 @@ u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
return MTRR_TYPE_WRBACK;
}

if (iter.mtrr_disabled)
return mtrr_disabled_type();

/* It is not covered by MTRRs. */
if (iter.partial_map) {
/*
Expand Down Expand Up @@ -689,6 +705,9 @@ bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
return false;
}

if (iter.mtrr_disabled)
return true;

if (!iter.partial_map)
return true;

Expand Down

0 comments on commit 10dc331

Please sign in to comment.