1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
7 * Copyright(C) 2015 Intel Corporation.
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 * Marcelo Tosatti <mtosatti@redhat.com>
13 * Paolo Bonzini <pbonzini@redhat.com>
14 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kvm_host.h>
24 #define IA32_MTRR_DEF_TYPE_E (1ULL << 11)
25 #define IA32_MTRR_DEF_TYPE_FE (1ULL << 10)
26 #define IA32_MTRR_DEF_TYPE_TYPE_MASK (0xff)
28 static bool is_mtrr_base_msr(unsigned int msr)
30 /* MTRR base MSRs use even numbers, masks use odd numbers. */
34 static struct kvm_mtrr_range *var_mtrr_msr_to_range(struct kvm_vcpu *vcpu,
37 int index = (msr - MTRRphysBase_MSR(0)) / 2;
39 return &vcpu->arch.mtrr_state.var_ranges[index];
42 static bool msr_mtrr_valid(unsigned msr)
45 case MTRRphysBase_MSR(0) ... MTRRphysMask_MSR(KVM_NR_VAR_MTRR - 1):
46 case MSR_MTRRfix64K_00000:
47 case MSR_MTRRfix16K_80000:
48 case MSR_MTRRfix16K_A0000:
49 case MSR_MTRRfix4K_C0000:
50 case MSR_MTRRfix4K_C8000:
51 case MSR_MTRRfix4K_D0000:
52 case MSR_MTRRfix4K_D8000:
53 case MSR_MTRRfix4K_E0000:
54 case MSR_MTRRfix4K_E8000:
55 case MSR_MTRRfix4K_F0000:
56 case MSR_MTRRfix4K_F8000:
63 static bool valid_mtrr_type(unsigned t)
65 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
68 static bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
73 if (!msr_mtrr_valid(msr))
76 if (msr == MSR_MTRRdefType) {
79 return valid_mtrr_type(data & 0xff);
80 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
81 for (i = 0; i < 8 ; i++)
82 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
88 WARN_ON(!(msr >= MTRRphysBase_MSR(0) &&
89 msr <= MTRRphysMask_MSR(KVM_NR_VAR_MTRR - 1)));
91 mask = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
94 if (!valid_mtrr_type(data & 0xff))
101 return (data & mask) == 0;
104 static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
106 return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
109 static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
111 return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
114 static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
116 return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
119 static u8 mtrr_disabled_type(struct kvm_vcpu *vcpu)
122 * Intel SDM 11.11.2.2: all MTRRs are disabled when
123 * IA32_MTRR_DEF_TYPE.E bit is cleared, and the UC
124 * memory type is applied to all of physical memory.
126 * However, virtual machines can be run with CPUID such that
127 * there are no MTRRs. In that case, the firmware will never
128 * enable MTRRs and it is obviously undesirable to run the
129 * guest entirely with UC memory and we use WB.
131 if (guest_cpuid_has(vcpu, X86_FEATURE_MTRR))
132 return MTRR_TYPE_UNCACHABLE;
134 return MTRR_TYPE_WRBACK;
138 * Three terms are used in the following code:
139 * - segment, it indicates the address segments covered by fixed MTRRs.
140 * - unit, it corresponds to the MSR entry in the segment.
141 * - range, a range is covered in one memory cache type.
143 struct fixed_mtrr_segment {
149 /* the start position in kvm_mtrr.fixed_ranges[]. */
153 static struct fixed_mtrr_segment fixed_seg_table[] = {
154 /* MSR_MTRRfix64K_00000, 1 unit. 64K fixed mtrr. */
158 .range_shift = 16, /* 64K */
163 * MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000, 2 units,
169 .range_shift = 14, /* 16K */
174 * MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000, 8 units,
180 .range_shift = 12, /* 12K */
186 * The size of unit is covered in one MSR, one MSR entry contains
187 * 8 ranges so that unit size is always 8 * 2^range_shift.
189 static u64 fixed_mtrr_seg_unit_size(int seg)
191 return 8 << fixed_seg_table[seg].range_shift;
194 static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
197 case MSR_MTRRfix64K_00000:
201 case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
203 *unit = array_index_nospec(
204 msr - MSR_MTRRfix16K_80000,
205 MSR_MTRRfix16K_A0000 - MSR_MTRRfix16K_80000 + 1);
207 case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
209 *unit = array_index_nospec(
210 msr - MSR_MTRRfix4K_C0000,
211 MSR_MTRRfix4K_F8000 - MSR_MTRRfix4K_C0000 + 1);
220 static void fixed_mtrr_seg_unit_range(int seg, int unit, u64 *start, u64 *end)
222 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
223 u64 unit_size = fixed_mtrr_seg_unit_size(seg);
225 *start = mtrr_seg->start + unit * unit_size;
226 *end = *start + unit_size;
227 WARN_ON(*end > mtrr_seg->end);
230 static int fixed_mtrr_seg_unit_range_index(int seg, int unit)
232 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
234 WARN_ON(mtrr_seg->start + unit * fixed_mtrr_seg_unit_size(seg)
237 /* each unit has 8 ranges. */
238 return mtrr_seg->range_start + 8 * unit;
241 static int fixed_mtrr_seg_end_range_index(int seg)
243 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
246 n = (mtrr_seg->end - mtrr_seg->start) >> mtrr_seg->range_shift;
247 return mtrr_seg->range_start + n - 1;
250 static bool fixed_msr_to_range(u32 msr, u64 *start, u64 *end)
254 if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
257 fixed_mtrr_seg_unit_range(seg, unit, start, end);
261 static int fixed_msr_to_range_index(u32 msr)
265 if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
268 return fixed_mtrr_seg_unit_range_index(seg, unit);
271 static int fixed_mtrr_addr_to_seg(u64 addr)
273 struct fixed_mtrr_segment *mtrr_seg;
274 int seg, seg_num = ARRAY_SIZE(fixed_seg_table);
276 for (seg = 0; seg < seg_num; seg++) {
277 mtrr_seg = &fixed_seg_table[seg];
278 if (mtrr_seg->start <= addr && addr < mtrr_seg->end)
285 static int fixed_mtrr_addr_seg_to_range_index(u64 addr, int seg)
287 struct fixed_mtrr_segment *mtrr_seg;
290 mtrr_seg = &fixed_seg_table[seg];
291 index = mtrr_seg->range_start;
292 index += (addr - mtrr_seg->start) >> mtrr_seg->range_shift;
296 static u64 fixed_mtrr_range_end_addr(int seg, int index)
298 struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
299 int pos = index - mtrr_seg->range_start;
301 return mtrr_seg->start + ((pos + 1) << mtrr_seg->range_shift);
304 static void var_mtrr_range(struct kvm_mtrr_range *range, u64 *start, u64 *end)
308 *start = range->base & PAGE_MASK;
310 mask = range->mask & PAGE_MASK;
312 /* This cannot overflow because writing to the reserved bits of
313 * variable MTRRs causes a #GP.
315 *end = (*start | ~mask) + 1;
318 static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
320 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
323 if (!kvm_mmu_honors_guest_mtrrs(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 var_mtrr_range(var_mtrr_msr_to_range(vcpu, msr), &start, &end);
341 kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
344 static bool var_mtrr_range_is_valid(struct kvm_mtrr_range *range)
346 return (range->mask & (1 << 11)) != 0;
349 static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
351 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
352 struct kvm_mtrr_range *tmp, *cur;
354 cur = var_mtrr_msr_to_range(vcpu, msr);
356 /* remove the entry if it's in the list. */
357 if (var_mtrr_range_is_valid(cur))
358 list_del(&cur->node);
361 * Set all illegal GPA bits in the mask, since those bits must
362 * implicitly be 0. The bits are then cleared when reading them.
364 if (is_mtrr_base_msr(msr))
367 cur->mask = data | kvm_vcpu_reserved_gpa_bits_raw(vcpu);
369 /* add it to the list if it's enabled. */
370 if (var_mtrr_range_is_valid(cur)) {
371 list_for_each_entry(tmp, &mtrr_state->head, node)
372 if (cur->base >= tmp->base)
374 list_add_tail(&cur->node, &tmp->node);
378 int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
382 if (!kvm_mtrr_valid(vcpu, msr, data))
385 index = fixed_msr_to_range_index(msr);
387 *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
388 else if (msr == MSR_MTRRdefType)
389 vcpu->arch.mtrr_state.deftype = data;
391 set_var_mtrr_msr(vcpu, msr, data);
393 update_mtrr(vcpu, msr);
397 int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
401 /* MSR_MTRRcap is a readonly MSR. */
402 if (msr == MSR_MTRRcap) {
407 * VCNT = KVM_NR_VAR_MTRR
409 *pdata = 0x500 | KVM_NR_VAR_MTRR;
413 if (!msr_mtrr_valid(msr))
416 index = fixed_msr_to_range_index(msr);
418 *pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
419 } else if (msr == MSR_MTRRdefType) {
420 *pdata = vcpu->arch.mtrr_state.deftype;
423 if (is_mtrr_base_msr(msr))
424 *pdata = var_mtrr_msr_to_range(vcpu, msr)->base;
426 *pdata = var_mtrr_msr_to_range(vcpu, msr)->mask;
428 *pdata &= ~kvm_vcpu_reserved_gpa_bits_raw(vcpu);
434 void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu)
436 INIT_LIST_HEAD(&vcpu->arch.mtrr_state.head);
441 struct kvm_mtrr *mtrr_state;
447 /* mtrr is completely disabled? */
449 /* [start, end) is not fully covered in MTRRs? */
452 /* private fields. */
454 /* used for fixed MTRRs. */
460 /* used for var MTRRs. */
462 struct kvm_mtrr_range *range;
463 /* max address has been covered in var MTRRs. */
471 static bool mtrr_lookup_fixed_start(struct mtrr_iter *iter)
475 if (!fixed_mtrr_is_enabled(iter->mtrr_state))
478 seg = fixed_mtrr_addr_to_seg(iter->start);
483 index = fixed_mtrr_addr_seg_to_range_index(iter->start, seg);
489 static bool match_var_range(struct mtrr_iter *iter,
490 struct kvm_mtrr_range *range)
494 var_mtrr_range(range, &start, &end);
495 if (!(start >= iter->end || end <= iter->start)) {
499 * the function is called when we do kvm_mtrr.head walking.
500 * Range has the minimum base address which interleaves
501 * [looker->start_max, looker->end).
503 iter->partial_map |= iter->start_max < start;
505 /* update the max address has been covered. */
506 iter->start_max = max(iter->start_max, end);
513 static void __mtrr_lookup_var_next(struct mtrr_iter *iter)
515 struct kvm_mtrr *mtrr_state = iter->mtrr_state;
517 list_for_each_entry_continue(iter->range, &mtrr_state->head, node)
518 if (match_var_range(iter, iter->range))
522 iter->partial_map |= iter->start_max < iter->end;
525 static void mtrr_lookup_var_start(struct mtrr_iter *iter)
527 struct kvm_mtrr *mtrr_state = iter->mtrr_state;
530 iter->start_max = iter->start;
532 iter->range = list_prepare_entry(iter->range, &mtrr_state->head, node);
534 __mtrr_lookup_var_next(iter);
537 static void mtrr_lookup_fixed_next(struct mtrr_iter *iter)
539 /* terminate the lookup. */
540 if (fixed_mtrr_range_end_addr(iter->seg, iter->index) >= iter->end) {
548 /* have looked up for all fixed MTRRs. */
549 if (iter->index >= ARRAY_SIZE(iter->mtrr_state->fixed_ranges))
550 return mtrr_lookup_var_start(iter);
552 /* switch to next segment. */
553 if (iter->index > fixed_mtrr_seg_end_range_index(iter->seg))
557 static void mtrr_lookup_var_next(struct mtrr_iter *iter)
559 __mtrr_lookup_var_next(iter);
562 static void mtrr_lookup_start(struct mtrr_iter *iter)
564 if (!mtrr_is_enabled(iter->mtrr_state)) {
565 iter->mtrr_disabled = true;
569 if (!mtrr_lookup_fixed_start(iter))
570 mtrr_lookup_var_start(iter);
573 static void mtrr_lookup_init(struct mtrr_iter *iter,
574 struct kvm_mtrr *mtrr_state, u64 start, u64 end)
576 iter->mtrr_state = mtrr_state;
579 iter->mtrr_disabled = false;
580 iter->partial_map = false;
584 mtrr_lookup_start(iter);
587 static bool mtrr_lookup_okay(struct mtrr_iter *iter)
590 iter->mem_type = iter->mtrr_state->fixed_ranges[iter->index];
595 iter->mem_type = iter->range->base & 0xff;
602 static void mtrr_lookup_next(struct mtrr_iter *iter)
605 mtrr_lookup_fixed_next(iter);
607 mtrr_lookup_var_next(iter);
610 #define mtrr_for_each_mem_type(_iter_, _mtrr_, _gpa_start_, _gpa_end_) \
611 for (mtrr_lookup_init(_iter_, _mtrr_, _gpa_start_, _gpa_end_); \
612 mtrr_lookup_okay(_iter_); mtrr_lookup_next(_iter_))
614 u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
616 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
617 struct mtrr_iter iter;
620 const int wt_wb_mask = (1 << MTRR_TYPE_WRBACK)
621 | (1 << MTRR_TYPE_WRTHROUGH);
623 start = gfn_to_gpa(gfn);
624 end = start + PAGE_SIZE;
626 mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
627 int curr_type = iter.mem_type;
630 * Please refer to Intel SDM Volume 3: 11.11.4.1 MTRR
640 * If two or more variable memory ranges match and the
641 * memory types are identical, then that memory type is
644 if (type == curr_type)
648 * If two or more variable memory ranges match and one of
649 * the memory types is UC, the UC memory type used.
651 if (curr_type == MTRR_TYPE_UNCACHABLE)
652 return MTRR_TYPE_UNCACHABLE;
655 * If two or more variable memory ranges match and the
656 * memory types are WT and WB, the WT memory type is used.
658 if (((1 << type) & wt_wb_mask) &&
659 ((1 << curr_type) & wt_wb_mask)) {
660 type = MTRR_TYPE_WRTHROUGH;
665 * For overlaps not defined by the above rules, processor
666 * behavior is undefined.
669 /* We use WB for this undefined behavior. :( */
670 return MTRR_TYPE_WRBACK;
673 if (iter.mtrr_disabled)
674 return mtrr_disabled_type(vcpu);
676 /* not contained in any MTRRs. */
678 return mtrr_default_type(mtrr_state);
681 * We just check one page, partially covered by MTRRs is
684 WARN_ON(iter.partial_map);
688 EXPORT_SYMBOL_GPL(kvm_mtrr_get_guest_memory_type);
690 bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
693 struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
694 struct mtrr_iter iter;
698 start = gfn_to_gpa(gfn);
699 end = gfn_to_gpa(gfn + page_num);
700 mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
702 type = iter.mem_type;
706 if (type != iter.mem_type)
710 if (iter.mtrr_disabled)
713 if (!iter.partial_map)
719 return type == mtrr_default_type(mtrr_state);