4 * Copyright (C) 2006 Qumranet, Inc.
5 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6 * Copyright(C) 2015 Intel Corporation.
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 * Marcelo Tosatti <mtosatti@redhat.com>
12 * Paolo Bonzini <pbonzini@redhat.com>
13 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
19 #include <linux/kvm_host.h>
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)
29 static bool msr_mtrr_valid(unsigned msr)
32 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
33 case MSR_MTRRfix64K_00000:
34 case MSR_MTRRfix16K_80000:
35 case MSR_MTRRfix16K_A0000:
36 case MSR_MTRRfix4K_C0000:
37 case MSR_MTRRfix4K_C8000:
38 case MSR_MTRRfix4K_D0000:
39 case MSR_MTRRfix4K_D8000:
40 case MSR_MTRRfix4K_E0000:
41 case MSR_MTRRfix4K_E8000:
42 case MSR_MTRRfix4K_F0000:
43 case MSR_MTRRfix4K_F8000:
51 static bool valid_pat_type(unsigned t)
53 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
56 static bool valid_mtrr_type(unsigned t)
58 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
61 bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
66 if (!msr_mtrr_valid(msr))
69 if (msr == MSR_IA32_CR_PAT) {
70 for (i = 0; i < 8; i++)
71 if (!valid_pat_type((data >> (i * 8)) & 0xff))
74 } else if (msr == MSR_MTRRdefType) {
77 return valid_mtrr_type(data & 0xff);
78 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
79 for (i = 0; i < 8 ; i++)
80 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
86 WARN_ON(!(msr >= 0x200 && msr < 0x200 + 2 * KVM_NR_VAR_MTRR));
88 mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
91 if (!valid_mtrr_type(data & 0xff))
98 kvm_inject_gp(vcpu, 0);
104 EXPORT_SYMBOL_GPL(kvm_mtrr_valid);
106 static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
108 return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
111 static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
113 return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
116 static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
118 return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
121 static u8 mtrr_disabled_type(struct kvm_vcpu *vcpu)
124 * Intel SDM 11.11.2.2: all MTRRs are disabled when
125 * IA32_MTRR_DEF_TYPE.E bit is cleared, and the UC
126 * memory type is applied to all of physical memory.
128 * However, virtual machines can be run with CPUID such that
129 * there are no MTRRs. In that case, the firmware will never
130 * enable MTRRs and it is obviously undesirable to run the
131 * guest entirely with UC memory and we use WB.
133 if (guest_cpuid_has_mtrr(vcpu))
134 return MTRR_TYPE_UNCACHABLE;
136 return MTRR_TYPE_WRBACK;
140 * Three terms are used in the following code:
141 * - segment, it indicates the address segments covered by fixed MTRRs.
142 * - unit, it corresponds to the MSR entry in the segment.
143 * - range, a range is covered in one memory cache type.
145 struct fixed_mtrr_segment {
151 /* the start position in kvm_mtrr.fixed_ranges[]. */
155 static struct fixed_mtrr_segment fixed_seg_table[] = {
156 /* MSR_MTRRfix64K_00000, 1 unit. 64K fixed mtrr. */
160 .range_shift = 16, /* 64K */
165 * MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000, 2 units,
171 .range_shift = 14, /* 16K */
176 * MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000, 8 units,
182 .range_shift = 12, /* 12K */
188 * The size of unit is covered in one MSR, one MSR entry contains
189 * 8 ranges so that unit size is always 8 * 2^range_shift.
191 static u64 fixed_mtrr_seg_unit_size(int seg)
193 return 8 << fixed_seg_table[seg].range_shift;
196 static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
199 case MSR_MTRRfix64K_00000:
203 case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
205 *unit = msr - MSR_MTRRfix16K_80000;
207 case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
209 *unit = msr - MSR_MTRRfix4K_C0000;
218 static void fixed_mtrr_seg_unit_range(int seg, int unit, u64 *start, u64 *end)
220 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
221 u64 unit_size = fixed_mtrr_seg_unit_size(seg);
223 *start = mtrr_seg->start + unit * unit_size;
224 *end = *start + unit_size;
225 WARN_ON(*end > mtrr_seg->end);
228 static int fixed_mtrr_seg_unit_range_index(int seg, int unit)
230 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
232 WARN_ON(mtrr_seg->start + unit * fixed_mtrr_seg_unit_size(seg)
235 /* each unit has 8 ranges. */
236 return mtrr_seg->range_start + 8 * unit;
239 static int fixed_mtrr_seg_end_range_index(int seg)
241 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
244 n = (mtrr_seg->end - mtrr_seg->start) >> mtrr_seg->range_shift;
245 return mtrr_seg->range_start + n - 1;
248 static bool fixed_msr_to_range(u32 msr, u64 *start, u64 *end)
252 if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
255 fixed_mtrr_seg_unit_range(seg, unit, start, end);
259 static int fixed_msr_to_range_index(u32 msr)
263 if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
266 return fixed_mtrr_seg_unit_range_index(seg, unit);
269 static int fixed_mtrr_addr_to_seg(u64 addr)
271 struct fixed_mtrr_segment *mtrr_seg;
272 int seg, seg_num = ARRAY_SIZE(fixed_seg_table);
274 for (seg = 0; seg < seg_num; seg++) {
275 mtrr_seg = &fixed_seg_table[seg];
276 if (mtrr_seg->start <= addr && addr < mtrr_seg->end)
283 static int fixed_mtrr_addr_seg_to_range_index(u64 addr, int seg)
285 struct fixed_mtrr_segment *mtrr_seg;
288 mtrr_seg = &fixed_seg_table[seg];
289 index = mtrr_seg->range_start;
290 index += (addr - mtrr_seg->start) >> mtrr_seg->range_shift;
294 static u64 fixed_mtrr_range_end_addr(int seg, int index)
296 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
297 int pos = index - mtrr_seg->range_start;
299 return mtrr_seg->start + ((pos + 1) << mtrr_seg->range_shift);
302 static void var_mtrr_range(struct kvm_mtrr_range *range, u64 *start, u64 *end)
306 *start = range->base & PAGE_MASK;
308 mask = range->mask & PAGE_MASK;
310 /* This cannot overflow because writing to the reserved bits of
311 * variable MTRRs causes a #GP.
313 *end = (*start | ~mask) + 1;
316 static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
318 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
322 if (msr == MSR_IA32_CR_PAT || !tdp_enabled ||
323 !kvm_arch_has_noncoherent_dma(vcpu->kvm))
326 if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
330 if (fixed_msr_to_range(msr, &start, &end)) {
331 if (!fixed_mtrr_is_enabled(mtrr_state))
333 } else if (msr == MSR_MTRRdefType) {
337 /* variable range MTRRs. */
338 index = (msr - 0x200) / 2;
339 var_mtrr_range(&mtrr_state->var_ranges[index], &start, &end);
342 kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
345 static bool var_mtrr_range_is_valid(struct kvm_mtrr_range *range)
347 return (range->mask & (1 << 11)) != 0;
350 static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
352 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
353 struct kvm_mtrr_range *tmp, *cur;
354 int index, is_mtrr_mask;
356 index = (msr - 0x200) / 2;
357 is_mtrr_mask = msr - 0x200 - 2 * index;
358 cur = &mtrr_state->var_ranges[index];
360 /* remove the entry if it's in the list. */
361 if (var_mtrr_range_is_valid(cur))
362 list_del(&mtrr_state->var_ranges[index].node);
364 /* Extend the mask with all 1 bits to the left, since those
365 * bits must implicitly be 0. The bits are then cleared
371 cur->mask = data | (-1LL << cpuid_maxphyaddr(vcpu));
373 /* add it to the list if it's enabled. */
374 if (var_mtrr_range_is_valid(cur)) {
375 list_for_each_entry(tmp, &mtrr_state->head, node)
376 if (cur->base >= tmp->base)
378 list_add_tail(&cur->node, &tmp->node);
382 int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
386 if (!kvm_mtrr_valid(vcpu, msr, data))
389 index = fixed_msr_to_range_index(msr);
391 *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
392 else if (msr == MSR_MTRRdefType)
393 vcpu->arch.mtrr_state.deftype = data;
394 else if (msr == MSR_IA32_CR_PAT)
395 vcpu->arch.pat = data;
397 set_var_mtrr_msr(vcpu, msr, data);
399 update_mtrr(vcpu, msr);
403 int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
407 /* MSR_MTRRcap is a readonly MSR. */
408 if (msr == MSR_MTRRcap) {
413 * VCNT = KVM_NR_VAR_MTRR
415 *pdata = 0x500 | KVM_NR_VAR_MTRR;
419 if (!msr_mtrr_valid(msr))
422 index = fixed_msr_to_range_index(msr);
424 *pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
425 else if (msr == MSR_MTRRdefType)
426 *pdata = vcpu->arch.mtrr_state.deftype;
427 else if (msr == MSR_IA32_CR_PAT)
428 *pdata = vcpu->arch.pat;
429 else { /* Variable MTRRs */
432 index = (msr - 0x200) / 2;
433 is_mtrr_mask = msr - 0x200 - 2 * index;
435 *pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
437 *pdata = vcpu->arch.mtrr_state.var_ranges[index].mask;
439 *pdata &= (1ULL << cpuid_maxphyaddr(vcpu)) - 1;
445 void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu)
447 INIT_LIST_HEAD(&vcpu->arch.mtrr_state.head);
452 struct kvm_mtrr *mtrr_state;
458 /* mtrr is completely disabled? */
460 /* [start, end) is not fully covered in MTRRs? */
463 /* private fields. */
465 /* used for fixed MTRRs. */
471 /* used for var MTRRs. */
473 struct kvm_mtrr_range *range;
474 /* max address has been covered in var MTRRs. */
482 static bool mtrr_lookup_fixed_start(struct mtrr_iter *iter)
486 if (!fixed_mtrr_is_enabled(iter->mtrr_state))
489 seg = fixed_mtrr_addr_to_seg(iter->start);
494 index = fixed_mtrr_addr_seg_to_range_index(iter->start, seg);
500 static bool match_var_range(struct mtrr_iter *iter,
501 struct kvm_mtrr_range *range)
505 var_mtrr_range(range, &start, &end);
506 if (!(start >= iter->end || end <= iter->start)) {
510 * the function is called when we do kvm_mtrr.head walking.
511 * Range has the minimum base address which interleaves
512 * [looker->start_max, looker->end).
514 iter->partial_map |= iter->start_max < start;
516 /* update the max address has been covered. */
517 iter->start_max = max(iter->start_max, end);
524 static void __mtrr_lookup_var_next(struct mtrr_iter *iter)
526 struct kvm_mtrr *mtrr_state = iter->mtrr_state;
528 list_for_each_entry_continue(iter->range, &mtrr_state->head, node)
529 if (match_var_range(iter, iter->range))
533 iter->partial_map |= iter->start_max < iter->end;
536 static void mtrr_lookup_var_start(struct mtrr_iter *iter)
538 struct kvm_mtrr *mtrr_state = iter->mtrr_state;
541 iter->start_max = iter->start;
543 iter->range = list_prepare_entry(iter->range, &mtrr_state->head, node);
545 __mtrr_lookup_var_next(iter);
548 static void mtrr_lookup_fixed_next(struct mtrr_iter *iter)
550 /* terminate the lookup. */
551 if (fixed_mtrr_range_end_addr(iter->seg, iter->index) >= iter->end) {
559 /* have looked up for all fixed MTRRs. */
560 if (iter->index >= ARRAY_SIZE(iter->mtrr_state->fixed_ranges))
561 return mtrr_lookup_var_start(iter);
563 /* switch to next segment. */
564 if (iter->index > fixed_mtrr_seg_end_range_index(iter->seg))
568 static void mtrr_lookup_var_next(struct mtrr_iter *iter)
570 __mtrr_lookup_var_next(iter);
573 static void mtrr_lookup_start(struct mtrr_iter *iter)
575 if (!mtrr_is_enabled(iter->mtrr_state)) {
576 iter->mtrr_disabled = true;
580 if (!mtrr_lookup_fixed_start(iter))
581 mtrr_lookup_var_start(iter);
584 static void mtrr_lookup_init(struct mtrr_iter *iter,
585 struct kvm_mtrr *mtrr_state, u64 start, u64 end)
587 iter->mtrr_state = mtrr_state;
590 iter->mtrr_disabled = false;
591 iter->partial_map = false;
595 mtrr_lookup_start(iter);
598 static bool mtrr_lookup_okay(struct mtrr_iter *iter)
601 iter->mem_type = iter->mtrr_state->fixed_ranges[iter->index];
606 iter->mem_type = iter->range->base & 0xff;
613 static void mtrr_lookup_next(struct mtrr_iter *iter)
616 mtrr_lookup_fixed_next(iter);
618 mtrr_lookup_var_next(iter);
621 #define mtrr_for_each_mem_type(_iter_, _mtrr_, _gpa_start_, _gpa_end_) \
622 for (mtrr_lookup_init(_iter_, _mtrr_, _gpa_start_, _gpa_end_); \
623 mtrr_lookup_okay(_iter_); mtrr_lookup_next(_iter_))
625 u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
627 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
628 struct mtrr_iter iter;
631 const int wt_wb_mask = (1 << MTRR_TYPE_WRBACK)
632 | (1 << MTRR_TYPE_WRTHROUGH);
634 start = gfn_to_gpa(gfn);
635 end = start + PAGE_SIZE;
637 mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
638 int curr_type = iter.mem_type;
641 * Please refer to Intel SDM Volume 3: 11.11.4.1 MTRR
651 * If two or more variable memory ranges match and the
652 * memory types are identical, then that memory type is
655 if (type == curr_type)
659 * If two or more variable memory ranges match and one of
660 * the memory types is UC, the UC memory type used.
662 if (curr_type == MTRR_TYPE_UNCACHABLE)
663 return MTRR_TYPE_UNCACHABLE;
666 * If two or more variable memory ranges match and the
667 * memory types are WT and WB, the WT memory type is used.
669 if (((1 << type) & wt_wb_mask) &&
670 ((1 << curr_type) & wt_wb_mask)) {
671 type = MTRR_TYPE_WRTHROUGH;
676 * For overlaps not defined by the above rules, processor
677 * behavior is undefined.
680 /* We use WB for this undefined behavior. :( */
681 return MTRR_TYPE_WRBACK;
684 if (iter.mtrr_disabled)
685 return mtrr_disabled_type(vcpu);
687 /* not contained in any MTRRs. */
689 return mtrr_default_type(mtrr_state);
692 * We just check one page, partially covered by MTRRs is
695 WARN_ON(iter.partial_map);
699 EXPORT_SYMBOL_GPL(kvm_mtrr_get_guest_memory_type);
701 bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
704 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
705 struct mtrr_iter iter;
709 start = gfn_to_gpa(gfn);
710 end = gfn_to_gpa(gfn + page_num);
711 mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
713 type = iter.mem_type;
717 if (type != iter.mem_type)
721 if (iter.mtrr_disabled)
724 if (!iter.partial_map)
730 return type == mtrr_default_type(mtrr_state);