22
22
#include "cpuid.h"
23
23
#include "mmu.h"
24
24
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
+
25
29
static bool msr_mtrr_valid (unsigned msr )
26
30
{
27
31
switch (msr ) {
@@ -101,10 +105,24 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
101
105
}
102
106
EXPORT_SYMBOL_GPL (kvm_mtrr_valid );
103
107
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
+
104
123
static void update_mtrr (struct kvm_vcpu * vcpu , u32 msr )
105
124
{
106
125
struct kvm_mtrr * mtrr_state = & vcpu -> arch .mtrr_state ;
107
- unsigned char mtrr_enabled = mtrr_state -> enabled ;
108
126
gfn_t start , end , mask ;
109
127
int index ;
110
128
bool is_fixed = true;
@@ -113,7 +131,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
113
131
!kvm_arch_has_noncoherent_dma (vcpu -> kvm ))
114
132
return ;
115
133
116
- if (!( mtrr_enabled & 0x2 ) && msr != MSR_MTRRdefType )
134
+ if (!mtrr_is_enabled ( mtrr_state ) && msr != MSR_MTRRdefType )
117
135
return ;
118
136
119
137
switch (msr ) {
@@ -152,7 +170,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
152
170
end = ((start & mask ) | ~mask ) + 1 ;
153
171
}
154
172
155
- if (is_fixed && !( mtrr_enabled & 0x1 ))
173
+ if (is_fixed && !fixed_mtrr_is_enabled ( mtrr_state ))
156
174
return ;
157
175
158
176
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)
165
183
if (!kvm_mtrr_valid (vcpu , msr , data ))
166
184
return 1 ;
167
185
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 )
172
189
p [0 ] = data ;
173
190
else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000 )
174
191
p [1 + msr - MSR_MTRRfix16K_80000 ] = data ;
@@ -215,8 +232,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
215
232
return 1 ;
216
233
217
234
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 ;
220
236
else if (msr == MSR_MTRRfix64K_00000 )
221
237
* pdata = p [0 ];
222
238
else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000 )
@@ -255,14 +271,14 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
255
271
int i , num_var_ranges = KVM_NR_VAR_MTRR ;
256
272
257
273
/* MTRR is completely disabled, use UC for all of physical memory. */
258
- if (!(mtrr_state -> enabled & 0x2 ))
274
+ if (!mtrr_is_enabled (mtrr_state ))
259
275
return MTRR_TYPE_UNCACHABLE ;
260
276
261
277
/* Make end inclusive end, instead of exclusive */
262
278
end -- ;
263
279
264
280
/* 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 )) {
266
282
int idx ;
267
283
268
284
if (start < 0x80000 ) {
@@ -330,7 +346,7 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
330
346
if (prev_match != 0xFF )
331
347
return prev_match ;
332
348
333
- return mtrr_state -> def_type ;
349
+ return mtrr_default_type ( mtrr_state ) ;
334
350
}
335
351
336
352
u8 kvm_mtrr_get_guest_memory_type (struct kvm_vcpu * vcpu , gfn_t gfn )
0 commit comments