Skip to content

Commit 10fac2d

Browse files
Xiao Guangrongbonzini
authored andcommitted
KVM: MTRR: clean up mtrr default type
Drop kvm_mtrr->enable, omit the decode/code workload and get rid of all the hard code Signed-off-by: Xiao Guangrong <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 910a6aa commit 10fac2d

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,7 @@ enum {
345345
struct kvm_mtrr {
346346
struct mtrr_var_range var_ranges[KVM_NR_VAR_MTRR];
347347
mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
348-
unsigned char enabled;
349-
mtrr_type def_type;
348+
u64 deftype;
350349
};
351350

352351
struct kvm_vcpu_arch {

arch/x86/kvm/mtrr.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "cpuid.h"
2323
#include "mmu.h"
2424

25+
#define IA32_MTRR_DEF_TYPE_E (1ULL << 11)
26+
#define IA32_MTRR_DEF_TYPE_FE (1ULL << 10)
27+
#define IA32_MTRR_DEF_TYPE_TYPE_MASK (0xff)
28+
2529
static bool msr_mtrr_valid(unsigned msr)
2630
{
2731
switch (msr) {
@@ -101,10 +105,24 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
101105
}
102106
EXPORT_SYMBOL_GPL(kvm_mtrr_valid);
103107

108+
static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
109+
{
110+
return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
111+
}
112+
113+
static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
114+
{
115+
return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
116+
}
117+
118+
static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
119+
{
120+
return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
121+
}
122+
104123
static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
105124
{
106125
struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
107-
unsigned char mtrr_enabled = mtrr_state->enabled;
108126
gfn_t start, end, mask;
109127
int index;
110128
bool is_fixed = true;
@@ -113,7 +131,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
113131
!kvm_arch_has_noncoherent_dma(vcpu->kvm))
114132
return;
115133

116-
if (!(mtrr_enabled & 0x2) && msr != MSR_MTRRdefType)
134+
if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
117135
return;
118136

119137
switch (msr) {
@@ -152,7 +170,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
152170
end = ((start & mask) | ~mask) + 1;
153171
}
154172

155-
if (is_fixed && !(mtrr_enabled & 0x1))
173+
if (is_fixed && !fixed_mtrr_is_enabled(mtrr_state))
156174
return;
157175

158176
kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
@@ -165,10 +183,9 @@ int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
165183
if (!kvm_mtrr_valid(vcpu, msr, data))
166184
return 1;
167185

168-
if (msr == MSR_MTRRdefType) {
169-
vcpu->arch.mtrr_state.def_type = data;
170-
vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
171-
} else if (msr == MSR_MTRRfix64K_00000)
186+
if (msr == MSR_MTRRdefType)
187+
vcpu->arch.mtrr_state.deftype = data;
188+
else if (msr == MSR_MTRRfix64K_00000)
172189
p[0] = data;
173190
else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
174191
p[1 + msr - MSR_MTRRfix16K_80000] = data;
@@ -215,8 +232,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
215232
return 1;
216233

217234
if (msr == MSR_MTRRdefType)
218-
*pdata = vcpu->arch.mtrr_state.def_type +
219-
(vcpu->arch.mtrr_state.enabled << 10);
235+
*pdata = vcpu->arch.mtrr_state.deftype;
220236
else if (msr == MSR_MTRRfix64K_00000)
221237
*pdata = p[0];
222238
else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
@@ -255,14 +271,14 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
255271
int i, num_var_ranges = KVM_NR_VAR_MTRR;
256272

257273
/* MTRR is completely disabled, use UC for all of physical memory. */
258-
if (!(mtrr_state->enabled & 0x2))
274+
if (!mtrr_is_enabled(mtrr_state))
259275
return MTRR_TYPE_UNCACHABLE;
260276

261277
/* Make end inclusive end, instead of exclusive */
262278
end--;
263279

264280
/* Look in fixed ranges. Just return the type as per start */
265-
if ((mtrr_state->enabled & 0x1) && (start < 0x100000)) {
281+
if (fixed_mtrr_is_enabled(mtrr_state) && (start < 0x100000)) {
266282
int idx;
267283

268284
if (start < 0x80000) {
@@ -330,7 +346,7 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
330346
if (prev_match != 0xFF)
331347
return prev_match;
332348

333-
return mtrr_state->def_type;
349+
return mtrr_default_type(mtrr_state);
334350
}
335351

336352
u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)

0 commit comments

Comments
 (0)