xref: /openbmc/linux/arch/x86/kvm/mtrr.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
120c8ccb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ff53604bSXiao Guangrong /*
3ff53604bSXiao Guangrong  * vMTRR implementation
4ff53604bSXiao Guangrong  *
5ff53604bSXiao Guangrong  * Copyright (C) 2006 Qumranet, Inc.
6ff53604bSXiao Guangrong  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
7ff53604bSXiao Guangrong  * Copyright(C) 2015 Intel Corporation.
8ff53604bSXiao Guangrong  *
9ff53604bSXiao Guangrong  * Authors:
10ff53604bSXiao Guangrong  *   Yaniv Kamay  <yaniv@qumranet.com>
11ff53604bSXiao Guangrong  *   Avi Kivity   <avi@qumranet.com>
12ff53604bSXiao Guangrong  *   Marcelo Tosatti <mtosatti@redhat.com>
13ff53604bSXiao Guangrong  *   Paolo Bonzini <pbonzini@redhat.com>
14ff53604bSXiao Guangrong  *   Xiao Guangrong <guangrong.xiao@linux.intel.com>
15ff53604bSXiao Guangrong  */
168d20bd63SSean Christopherson #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17ff53604bSXiao Guangrong 
18ff53604bSXiao Guangrong #include <linux/kvm_host.h>
19ff53604bSXiao Guangrong #include <asm/mtrr.h>
20ff53604bSXiao Guangrong 
21ff53604bSXiao Guangrong #include "cpuid.h"
22ff53604bSXiao Guangrong #include "mmu.h"
23ff53604bSXiao Guangrong 
2410fac2dcSXiao Guangrong #define IA32_MTRR_DEF_TYPE_E		(1ULL << 11)
2510fac2dcSXiao Guangrong #define IA32_MTRR_DEF_TYPE_FE		(1ULL << 10)
2610fac2dcSXiao Guangrong #define IA32_MTRR_DEF_TYPE_TYPE_MASK	(0xff)
2710fac2dcSXiao Guangrong 
is_mtrr_base_msr(unsigned int msr)28ebda79e5SSean Christopherson static bool is_mtrr_base_msr(unsigned int msr)
29ebda79e5SSean Christopherson {
30ebda79e5SSean Christopherson 	/* MTRR base MSRs use even numbers, masks use odd numbers. */
31ebda79e5SSean Christopherson 	return !(msr & 0x1);
32ebda79e5SSean Christopherson }
33ebda79e5SSean Christopherson 
var_mtrr_msr_to_range(struct kvm_vcpu * vcpu,unsigned int msr)349ae38b4fSSean Christopherson static struct kvm_mtrr_range *var_mtrr_msr_to_range(struct kvm_vcpu *vcpu,
359ae38b4fSSean Christopherson 						    unsigned int msr)
369ae38b4fSSean Christopherson {
3734a83deaSSean Christopherson 	int index = (msr - MTRRphysBase_MSR(0)) / 2;
389ae38b4fSSean Christopherson 
399ae38b4fSSean Christopherson 	return &vcpu->arch.mtrr_state.var_ranges[index];
409ae38b4fSSean Christopherson }
419ae38b4fSSean Christopherson 
msr_mtrr_valid(unsigned msr)42ff53604bSXiao Guangrong static bool msr_mtrr_valid(unsigned msr)
43ff53604bSXiao Guangrong {
44ff53604bSXiao Guangrong 	switch (msr) {
4534a83deaSSean Christopherson 	case MTRRphysBase_MSR(0) ... MTRRphysMask_MSR(KVM_NR_VAR_MTRR - 1):
46ff53604bSXiao Guangrong 	case MSR_MTRRfix64K_00000:
47ff53604bSXiao Guangrong 	case MSR_MTRRfix16K_80000:
48ff53604bSXiao Guangrong 	case MSR_MTRRfix16K_A0000:
49ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_C0000:
50ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_C8000:
51ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_D0000:
52ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_D8000:
53ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_E0000:
54ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_E8000:
55ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_F0000:
56ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_F8000:
57ff53604bSXiao Guangrong 	case MSR_MTRRdefType:
58ff53604bSXiao Guangrong 		return true;
59ff53604bSXiao Guangrong 	}
60ff53604bSXiao Guangrong 	return false;
61ff53604bSXiao Guangrong }
62ff53604bSXiao Guangrong 
valid_mtrr_type(unsigned t)63ff53604bSXiao Guangrong static bool valid_mtrr_type(unsigned t)
64ff53604bSXiao Guangrong {
65ff53604bSXiao Guangrong 	return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
66ff53604bSXiao Guangrong }
67ff53604bSXiao Guangrong 
kvm_mtrr_valid(struct kvm_vcpu * vcpu,u32 msr,u64 data)68*3a5f4907SSean Christopherson static bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
69ff53604bSXiao Guangrong {
70ff53604bSXiao Guangrong 	int i;
71ff53604bSXiao Guangrong 	u64 mask;
72ff53604bSXiao Guangrong 
73ff53604bSXiao Guangrong 	if (!msr_mtrr_valid(msr))
74ff53604bSXiao Guangrong 		return false;
75ff53604bSXiao Guangrong 
76bc7fe2f0SSean Christopherson 	if (msr == MSR_MTRRdefType) {
77ff53604bSXiao Guangrong 		if (data & ~0xcff)
78ff53604bSXiao Guangrong 			return false;
79ff53604bSXiao Guangrong 		return valid_mtrr_type(data & 0xff);
80ff53604bSXiao Guangrong 	} else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
81ff53604bSXiao Guangrong 		for (i = 0; i < 8 ; i++)
82ff53604bSXiao Guangrong 			if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
83ff53604bSXiao Guangrong 				return false;
84ff53604bSXiao Guangrong 		return true;
85ff53604bSXiao Guangrong 	}
86ff53604bSXiao Guangrong 
87ff53604bSXiao Guangrong 	/* variable MTRRs */
8834a83deaSSean Christopherson 	WARN_ON(!(msr >= MTRRphysBase_MSR(0) &&
8934a83deaSSean Christopherson 		  msr <= MTRRphysMask_MSR(KVM_NR_VAR_MTRR - 1)));
90ff53604bSXiao Guangrong 
91a8ac864aSSean Christopherson 	mask = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
92ff53604bSXiao Guangrong 	if ((msr & 1) == 0) {
93ff53604bSXiao Guangrong 		/* MTRR base */
94ff53604bSXiao Guangrong 		if (!valid_mtrr_type(data & 0xff))
95ff53604bSXiao Guangrong 			return false;
96ff53604bSXiao Guangrong 		mask |= 0xf00;
97ff53604bSXiao Guangrong 	} else
98ff53604bSXiao Guangrong 		/* MTRR mask */
99ff53604bSXiao Guangrong 		mask |= 0x7ff;
100ff53604bSXiao Guangrong 
1019caec4bfSPaolo Bonzini 	return (data & mask) == 0;
102ff53604bSXiao Guangrong }
103ff53604bSXiao Guangrong 
mtrr_is_enabled(struct kvm_mtrr * mtrr_state)10410fac2dcSXiao Guangrong static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
10510fac2dcSXiao Guangrong {
10610fac2dcSXiao Guangrong 	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
10710fac2dcSXiao Guangrong }
10810fac2dcSXiao Guangrong 
fixed_mtrr_is_enabled(struct kvm_mtrr * mtrr_state)10910fac2dcSXiao Guangrong static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
11010fac2dcSXiao Guangrong {
11110fac2dcSXiao Guangrong 	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
11210fac2dcSXiao Guangrong }
11310fac2dcSXiao Guangrong 
mtrr_default_type(struct kvm_mtrr * mtrr_state)11410fac2dcSXiao Guangrong static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
11510fac2dcSXiao Guangrong {
11610fac2dcSXiao Guangrong 	return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
11710fac2dcSXiao Guangrong }
11810fac2dcSXiao Guangrong 
mtrr_disabled_type(struct kvm_vcpu * vcpu)119e24dea2aSPaolo Bonzini static u8 mtrr_disabled_type(struct kvm_vcpu *vcpu)
12010dc331fSXiao Guangrong {
12110dc331fSXiao Guangrong 	/*
12210dc331fSXiao Guangrong 	 * Intel SDM 11.11.2.2: all MTRRs are disabled when
12310dc331fSXiao Guangrong 	 * IA32_MTRR_DEF_TYPE.E bit is cleared, and the UC
12410dc331fSXiao Guangrong 	 * memory type is applied to all of physical memory.
125e24dea2aSPaolo Bonzini 	 *
126e24dea2aSPaolo Bonzini 	 * However, virtual machines can be run with CPUID such that
127e24dea2aSPaolo Bonzini 	 * there are no MTRRs.  In that case, the firmware will never
128e24dea2aSPaolo Bonzini 	 * enable MTRRs and it is obviously undesirable to run the
129e24dea2aSPaolo Bonzini 	 * guest entirely with UC memory and we use WB.
13010dc331fSXiao Guangrong 	 */
131d6321d49SRadim Krčmář 	if (guest_cpuid_has(vcpu, X86_FEATURE_MTRR))
13210dc331fSXiao Guangrong 		return MTRR_TYPE_UNCACHABLE;
133e24dea2aSPaolo Bonzini 	else
134e24dea2aSPaolo Bonzini 		return MTRR_TYPE_WRBACK;
13510dc331fSXiao Guangrong }
13610dc331fSXiao Guangrong 
137de9aef5eSXiao Guangrong /*
138de9aef5eSXiao Guangrong * Three terms are used in the following code:
139de9aef5eSXiao Guangrong * - segment, it indicates the address segments covered by fixed MTRRs.
140de9aef5eSXiao Guangrong * - unit, it corresponds to the MSR entry in the segment.
141de9aef5eSXiao Guangrong * - range, a range is covered in one memory cache type.
142de9aef5eSXiao Guangrong */
143de9aef5eSXiao Guangrong struct fixed_mtrr_segment {
144de9aef5eSXiao Guangrong 	u64 start;
145de9aef5eSXiao Guangrong 	u64 end;
146de9aef5eSXiao Guangrong 
147de9aef5eSXiao Guangrong 	int range_shift;
148de9aef5eSXiao Guangrong 
149de9aef5eSXiao Guangrong 	/* the start position in kvm_mtrr.fixed_ranges[]. */
150de9aef5eSXiao Guangrong 	int range_start;
151de9aef5eSXiao Guangrong };
152de9aef5eSXiao Guangrong 
153de9aef5eSXiao Guangrong static struct fixed_mtrr_segment fixed_seg_table[] = {
154de9aef5eSXiao Guangrong 	/* MSR_MTRRfix64K_00000, 1 unit. 64K fixed mtrr. */
155de9aef5eSXiao Guangrong 	{
156de9aef5eSXiao Guangrong 		.start = 0x0,
157de9aef5eSXiao Guangrong 		.end = 0x80000,
158de9aef5eSXiao Guangrong 		.range_shift = 16, /* 64K */
159de9aef5eSXiao Guangrong 		.range_start = 0,
160de9aef5eSXiao Guangrong 	},
161de9aef5eSXiao Guangrong 
162de9aef5eSXiao Guangrong 	/*
163de9aef5eSXiao Guangrong 	 * MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000, 2 units,
164de9aef5eSXiao Guangrong 	 * 16K fixed mtrr.
165de9aef5eSXiao Guangrong 	 */
166de9aef5eSXiao Guangrong 	{
167de9aef5eSXiao Guangrong 		.start = 0x80000,
168de9aef5eSXiao Guangrong 		.end = 0xc0000,
169de9aef5eSXiao Guangrong 		.range_shift = 14, /* 16K */
170de9aef5eSXiao Guangrong 		.range_start = 8,
171de9aef5eSXiao Guangrong 	},
172de9aef5eSXiao Guangrong 
173de9aef5eSXiao Guangrong 	/*
174de9aef5eSXiao Guangrong 	 * MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000, 8 units,
175de9aef5eSXiao Guangrong 	 * 4K fixed mtrr.
176de9aef5eSXiao Guangrong 	 */
177de9aef5eSXiao Guangrong 	{
178de9aef5eSXiao Guangrong 		.start = 0xc0000,
179de9aef5eSXiao Guangrong 		.end = 0x100000,
180de9aef5eSXiao Guangrong 		.range_shift = 12, /* 12K */
181de9aef5eSXiao Guangrong 		.range_start = 24,
182de9aef5eSXiao Guangrong 	}
183de9aef5eSXiao Guangrong };
184de9aef5eSXiao Guangrong 
185de9aef5eSXiao Guangrong /*
186de9aef5eSXiao Guangrong  * The size of unit is covered in one MSR, one MSR entry contains
187de9aef5eSXiao Guangrong  * 8 ranges so that unit size is always 8 * 2^range_shift.
188de9aef5eSXiao Guangrong  */
fixed_mtrr_seg_unit_size(int seg)189de9aef5eSXiao Guangrong static u64 fixed_mtrr_seg_unit_size(int seg)
190de9aef5eSXiao Guangrong {
191de9aef5eSXiao Guangrong 	return 8 << fixed_seg_table[seg].range_shift;
192de9aef5eSXiao Guangrong }
193de9aef5eSXiao Guangrong 
fixed_msr_to_seg_unit(u32 msr,int * seg,int * unit)194de9aef5eSXiao Guangrong static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
195de9aef5eSXiao Guangrong {
196de9aef5eSXiao Guangrong 	switch (msr) {
197de9aef5eSXiao Guangrong 	case MSR_MTRRfix64K_00000:
198de9aef5eSXiao Guangrong 		*seg = 0;
199de9aef5eSXiao Guangrong 		*unit = 0;
200de9aef5eSXiao Guangrong 		break;
201de9aef5eSXiao Guangrong 	case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
202de9aef5eSXiao Guangrong 		*seg = 1;
20325a5edeaSMarios Pomonis 		*unit = array_index_nospec(
20425a5edeaSMarios Pomonis 			msr - MSR_MTRRfix16K_80000,
20525a5edeaSMarios Pomonis 			MSR_MTRRfix16K_A0000 - MSR_MTRRfix16K_80000 + 1);
206de9aef5eSXiao Guangrong 		break;
207de9aef5eSXiao Guangrong 	case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
208de9aef5eSXiao Guangrong 		*seg = 2;
20925a5edeaSMarios Pomonis 		*unit = array_index_nospec(
21025a5edeaSMarios Pomonis 			msr - MSR_MTRRfix4K_C0000,
21125a5edeaSMarios Pomonis 			MSR_MTRRfix4K_F8000 - MSR_MTRRfix4K_C0000 + 1);
212de9aef5eSXiao Guangrong 		break;
213de9aef5eSXiao Guangrong 	default:
214de9aef5eSXiao Guangrong 		return false;
215de9aef5eSXiao Guangrong 	}
216de9aef5eSXiao Guangrong 
217de9aef5eSXiao Guangrong 	return true;
218de9aef5eSXiao Guangrong }
219de9aef5eSXiao Guangrong 
fixed_mtrr_seg_unit_range(int seg,int unit,u64 * start,u64 * end)220de9aef5eSXiao Guangrong static void fixed_mtrr_seg_unit_range(int seg, int unit, u64 *start, u64 *end)
221de9aef5eSXiao Guangrong {
222de9aef5eSXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
223de9aef5eSXiao Guangrong 	u64 unit_size = fixed_mtrr_seg_unit_size(seg);
224de9aef5eSXiao Guangrong 
225de9aef5eSXiao Guangrong 	*start = mtrr_seg->start + unit * unit_size;
226de9aef5eSXiao Guangrong 	*end = *start + unit_size;
227de9aef5eSXiao Guangrong 	WARN_ON(*end > mtrr_seg->end);
228de9aef5eSXiao Guangrong }
229de9aef5eSXiao Guangrong 
fixed_mtrr_seg_unit_range_index(int seg,int unit)230de9aef5eSXiao Guangrong static int fixed_mtrr_seg_unit_range_index(int seg, int unit)
231de9aef5eSXiao Guangrong {
232de9aef5eSXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
233de9aef5eSXiao Guangrong 
234de9aef5eSXiao Guangrong 	WARN_ON(mtrr_seg->start + unit * fixed_mtrr_seg_unit_size(seg)
235de9aef5eSXiao Guangrong 		> mtrr_seg->end);
236de9aef5eSXiao Guangrong 
237de9aef5eSXiao Guangrong 	/* each unit has 8 ranges. */
238de9aef5eSXiao Guangrong 	return mtrr_seg->range_start + 8 * unit;
239de9aef5eSXiao Guangrong }
240de9aef5eSXiao Guangrong 
fixed_mtrr_seg_end_range_index(int seg)241f571c097SXiao Guangrong static int fixed_mtrr_seg_end_range_index(int seg)
242f571c097SXiao Guangrong {
243f571c097SXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
244f571c097SXiao Guangrong 	int n;
245f571c097SXiao Guangrong 
246f571c097SXiao Guangrong 	n = (mtrr_seg->end - mtrr_seg->start) >> mtrr_seg->range_shift;
247f571c097SXiao Guangrong 	return mtrr_seg->range_start + n - 1;
248f571c097SXiao Guangrong }
249f571c097SXiao Guangrong 
fixed_msr_to_range(u32 msr,u64 * start,u64 * end)250de9aef5eSXiao Guangrong static bool fixed_msr_to_range(u32 msr, u64 *start, u64 *end)
251de9aef5eSXiao Guangrong {
252de9aef5eSXiao Guangrong 	int seg, unit;
253de9aef5eSXiao Guangrong 
254de9aef5eSXiao Guangrong 	if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
255de9aef5eSXiao Guangrong 		return false;
256de9aef5eSXiao Guangrong 
257de9aef5eSXiao Guangrong 	fixed_mtrr_seg_unit_range(seg, unit, start, end);
258de9aef5eSXiao Guangrong 	return true;
259de9aef5eSXiao Guangrong }
260de9aef5eSXiao Guangrong 
fixed_msr_to_range_index(u32 msr)261de9aef5eSXiao Guangrong static int fixed_msr_to_range_index(u32 msr)
262de9aef5eSXiao Guangrong {
263de9aef5eSXiao Guangrong 	int seg, unit;
264de9aef5eSXiao Guangrong 
265de9aef5eSXiao Guangrong 	if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
266de9aef5eSXiao Guangrong 		return -1;
267de9aef5eSXiao Guangrong 
268de9aef5eSXiao Guangrong 	return fixed_mtrr_seg_unit_range_index(seg, unit);
269de9aef5eSXiao Guangrong }
270de9aef5eSXiao Guangrong 
fixed_mtrr_addr_to_seg(u64 addr)271f7bfb57bSXiao Guangrong static int fixed_mtrr_addr_to_seg(u64 addr)
272f7bfb57bSXiao Guangrong {
273f7bfb57bSXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg;
274f7bfb57bSXiao Guangrong 	int seg, seg_num = ARRAY_SIZE(fixed_seg_table);
275f7bfb57bSXiao Guangrong 
276f7bfb57bSXiao Guangrong 	for (seg = 0; seg < seg_num; seg++) {
277f7bfb57bSXiao Guangrong 		mtrr_seg = &fixed_seg_table[seg];
278a7f2d786SAlexis Dambricourt 		if (mtrr_seg->start <= addr && addr < mtrr_seg->end)
279f7bfb57bSXiao Guangrong 			return seg;
280f7bfb57bSXiao Guangrong 	}
281f7bfb57bSXiao Guangrong 
282f7bfb57bSXiao Guangrong 	return -1;
283f7bfb57bSXiao Guangrong }
284f7bfb57bSXiao Guangrong 
fixed_mtrr_addr_seg_to_range_index(u64 addr,int seg)285f7bfb57bSXiao Guangrong static int fixed_mtrr_addr_seg_to_range_index(u64 addr, int seg)
286f7bfb57bSXiao Guangrong {
287f7bfb57bSXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg;
288f7bfb57bSXiao Guangrong 	int index;
289f7bfb57bSXiao Guangrong 
290f7bfb57bSXiao Guangrong 	mtrr_seg = &fixed_seg_table[seg];
291f7bfb57bSXiao Guangrong 	index = mtrr_seg->range_start;
292f7bfb57bSXiao Guangrong 	index += (addr - mtrr_seg->start) >> mtrr_seg->range_shift;
293f7bfb57bSXiao Guangrong 	return index;
294f7bfb57bSXiao Guangrong }
295f7bfb57bSXiao Guangrong 
fixed_mtrr_range_end_addr(int seg,int index)296f571c097SXiao Guangrong static u64 fixed_mtrr_range_end_addr(int seg, int index)
297f571c097SXiao Guangrong {
298f571c097SXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
299f571c097SXiao Guangrong 	int pos = index - mtrr_seg->range_start;
300f571c097SXiao Guangrong 
301f571c097SXiao Guangrong 	return mtrr_seg->start + ((pos + 1) << mtrr_seg->range_shift);
302f571c097SXiao Guangrong }
303f571c097SXiao Guangrong 
var_mtrr_range(struct kvm_mtrr_range * range,u64 * start,u64 * end)304a13842dcSXiao Guangrong static void var_mtrr_range(struct kvm_mtrr_range *range, u64 *start, u64 *end)
305a13842dcSXiao Guangrong {
306a13842dcSXiao Guangrong 	u64 mask;
307a13842dcSXiao Guangrong 
308a13842dcSXiao Guangrong 	*start = range->base & PAGE_MASK;
309a13842dcSXiao Guangrong 
310a13842dcSXiao Guangrong 	mask = range->mask & PAGE_MASK;
311a13842dcSXiao Guangrong 
312a13842dcSXiao Guangrong 	/* This cannot overflow because writing to the reserved bits of
313a13842dcSXiao Guangrong 	 * variable MTRRs causes a #GP.
314a13842dcSXiao Guangrong 	 */
315a13842dcSXiao Guangrong 	*end = (*start | ~mask) + 1;
316a13842dcSXiao Guangrong }
317a13842dcSXiao Guangrong 
update_mtrr(struct kvm_vcpu * vcpu,u32 msr)318ff53604bSXiao Guangrong static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
319ff53604bSXiao Guangrong {
32070109e7dSXiao Guangrong 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
321a13842dcSXiao Guangrong 	gfn_t start, end;
322ff53604bSXiao Guangrong 
323bc7fe2f0SSean Christopherson 	if (!tdp_enabled || !kvm_arch_has_noncoherent_dma(vcpu->kvm))
324ff53604bSXiao Guangrong 		return;
325ff53604bSXiao Guangrong 
32610fac2dcSXiao Guangrong 	if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
327ff53604bSXiao Guangrong 		return;
328ff53604bSXiao Guangrong 
329de9aef5eSXiao Guangrong 	/* fixed MTRRs. */
330de9aef5eSXiao Guangrong 	if (fixed_msr_to_range(msr, &start, &end)) {
331de9aef5eSXiao Guangrong 		if (!fixed_mtrr_is_enabled(mtrr_state))
332de9aef5eSXiao Guangrong 			return;
333de9aef5eSXiao Guangrong 	} else if (msr == MSR_MTRRdefType) {
334ff53604bSXiao Guangrong 		start = 0x0;
335ff53604bSXiao Guangrong 		end = ~0ULL;
336de9aef5eSXiao Guangrong 	} else {
337ff53604bSXiao Guangrong 		/* variable range MTRRs. */
3389ae38b4fSSean Christopherson 		var_mtrr_range(var_mtrr_msr_to_range(vcpu, msr), &start, &end);
339ff53604bSXiao Guangrong 	}
340ff53604bSXiao Guangrong 
341ff53604bSXiao Guangrong 	kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
342ff53604bSXiao Guangrong }
343ff53604bSXiao Guangrong 
var_mtrr_range_is_valid(struct kvm_mtrr_range * range)34419efffa2SXiao Guangrong static bool var_mtrr_range_is_valid(struct kvm_mtrr_range *range)
34519efffa2SXiao Guangrong {
34619efffa2SXiao Guangrong 	return (range->mask & (1 << 11)) != 0;
34719efffa2SXiao Guangrong }
34819efffa2SXiao Guangrong 
set_var_mtrr_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data)34919efffa2SXiao Guangrong static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
35019efffa2SXiao Guangrong {
35119efffa2SXiao Guangrong 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
35219efffa2SXiao Guangrong 	struct kvm_mtrr_range *tmp, *cur;
35319efffa2SXiao Guangrong 
3549ae38b4fSSean Christopherson 	cur = var_mtrr_msr_to_range(vcpu, msr);
35519efffa2SXiao Guangrong 
35619efffa2SXiao Guangrong 	/* remove the entry if it's in the list. */
35719efffa2SXiao Guangrong 	if (var_mtrr_range_is_valid(cur))
3589ae38b4fSSean Christopherson 		list_del(&cur->node);
35919efffa2SXiao Guangrong 
360a8ac864aSSean Christopherson 	/*
361a8ac864aSSean Christopherson 	 * Set all illegal GPA bits in the mask, since those bits must
362a8ac864aSSean Christopherson 	 * implicitly be 0.  The bits are then cleared when reading them.
363fa7c4ebdSPaolo Bonzini 	 */
364ebda79e5SSean Christopherson 	if (is_mtrr_base_msr(msr))
36519efffa2SXiao Guangrong 		cur->base = data;
36619efffa2SXiao Guangrong 	else
367a8ac864aSSean Christopherson 		cur->mask = data | kvm_vcpu_reserved_gpa_bits_raw(vcpu);
36819efffa2SXiao Guangrong 
36919efffa2SXiao Guangrong 	/* add it to the list if it's enabled. */
37019efffa2SXiao Guangrong 	if (var_mtrr_range_is_valid(cur)) {
37119efffa2SXiao Guangrong 		list_for_each_entry(tmp, &mtrr_state->head, node)
37219efffa2SXiao Guangrong 			if (cur->base >= tmp->base)
37319efffa2SXiao Guangrong 				break;
37419efffa2SXiao Guangrong 		list_add_tail(&cur->node, &tmp->node);
37519efffa2SXiao Guangrong 	}
37619efffa2SXiao Guangrong }
37719efffa2SXiao Guangrong 
kvm_mtrr_set_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data)378ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
379ff53604bSXiao Guangrong {
380de9aef5eSXiao Guangrong 	int index;
381ff53604bSXiao Guangrong 
382ff53604bSXiao Guangrong 	if (!kvm_mtrr_valid(vcpu, msr, data))
383ff53604bSXiao Guangrong 		return 1;
384ff53604bSXiao Guangrong 
385de9aef5eSXiao Guangrong 	index = fixed_msr_to_range_index(msr);
386de9aef5eSXiao Guangrong 	if (index >= 0)
387de9aef5eSXiao Guangrong 		*(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
388de9aef5eSXiao Guangrong 	else if (msr == MSR_MTRRdefType)
38910fac2dcSXiao Guangrong 		vcpu->arch.mtrr_state.deftype = data;
390ff53604bSXiao Guangrong 	else
39119efffa2SXiao Guangrong 		set_var_mtrr_msr(vcpu, msr, data);
392ff53604bSXiao Guangrong 
393ff53604bSXiao Guangrong 	update_mtrr(vcpu, msr);
394ff53604bSXiao Guangrong 	return 0;
395ff53604bSXiao Guangrong }
396ff53604bSXiao Guangrong 
kvm_mtrr_get_msr(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata)397ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
398ff53604bSXiao Guangrong {
399de9aef5eSXiao Guangrong 	int index;
400ff53604bSXiao Guangrong 
401eb839917SXiao Guangrong 	/* MSR_MTRRcap is a readonly MSR. */
402eb839917SXiao Guangrong 	if (msr == MSR_MTRRcap) {
403eb839917SXiao Guangrong 		/*
404eb839917SXiao Guangrong 		 * SMRR = 0
405eb839917SXiao Guangrong 		 * WC = 1
406eb839917SXiao Guangrong 		 * FIX = 1
407eb839917SXiao Guangrong 		 * VCNT = KVM_NR_VAR_MTRR
408eb839917SXiao Guangrong 		 */
409eb839917SXiao Guangrong 		*pdata = 0x500 | KVM_NR_VAR_MTRR;
410eb839917SXiao Guangrong 		return 0;
411eb839917SXiao Guangrong 	}
412eb839917SXiao Guangrong 
413ff53604bSXiao Guangrong 	if (!msr_mtrr_valid(msr))
414ff53604bSXiao Guangrong 		return 1;
415ff53604bSXiao Guangrong 
416de9aef5eSXiao Guangrong 	index = fixed_msr_to_range_index(msr);
417bc7fe2f0SSean Christopherson 	if (index >= 0) {
418de9aef5eSXiao Guangrong 		*pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
419bc7fe2f0SSean Christopherson 	} else if (msr == MSR_MTRRdefType) {
42010fac2dcSXiao Guangrong 		*pdata = vcpu->arch.mtrr_state.deftype;
421bc7fe2f0SSean Christopherson 	} else {
422bc7fe2f0SSean Christopherson 		/* Variable MTRRs */
423ebda79e5SSean Christopherson 		if (is_mtrr_base_msr(msr))
4249ae38b4fSSean Christopherson 			*pdata = var_mtrr_msr_to_range(vcpu, msr)->base;
425ff53604bSXiao Guangrong 		else
4269ae38b4fSSean Christopherson 			*pdata = var_mtrr_msr_to_range(vcpu, msr)->mask;
427fa7c4ebdSPaolo Bonzini 
428a8ac864aSSean Christopherson 		*pdata &= ~kvm_vcpu_reserved_gpa_bits_raw(vcpu);
429ff53604bSXiao Guangrong 	}
430ff53604bSXiao Guangrong 
431ff53604bSXiao Guangrong 	return 0;
432ff53604bSXiao Guangrong }
433ff53604bSXiao Guangrong 
kvm_vcpu_mtrr_init(struct kvm_vcpu * vcpu)43419efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu)
43519efffa2SXiao Guangrong {
43619efffa2SXiao Guangrong 	INIT_LIST_HEAD(&vcpu->arch.mtrr_state.head);
43719efffa2SXiao Guangrong }
43819efffa2SXiao Guangrong 
439f571c097SXiao Guangrong struct mtrr_iter {
440f571c097SXiao Guangrong 	/* input fields. */
441f571c097SXiao Guangrong 	struct kvm_mtrr *mtrr_state;
442f571c097SXiao Guangrong 	u64 start;
443f571c097SXiao Guangrong 	u64 end;
444f571c097SXiao Guangrong 
445f571c097SXiao Guangrong 	/* output fields. */
446f571c097SXiao Guangrong 	int mem_type;
44710dc331fSXiao Guangrong 	/* mtrr is completely disabled? */
44810dc331fSXiao Guangrong 	bool mtrr_disabled;
449f571c097SXiao Guangrong 	/* [start, end) is not fully covered in MTRRs? */
450f571c097SXiao Guangrong 	bool partial_map;
451f571c097SXiao Guangrong 
452f571c097SXiao Guangrong 	/* private fields. */
453f571c097SXiao Guangrong 	union {
454f571c097SXiao Guangrong 		/* used for fixed MTRRs. */
455f571c097SXiao Guangrong 		struct {
456f571c097SXiao Guangrong 			int index;
457f571c097SXiao Guangrong 			int seg;
458f571c097SXiao Guangrong 		};
459f571c097SXiao Guangrong 
460f571c097SXiao Guangrong 		/* used for var MTRRs. */
461f571c097SXiao Guangrong 		struct {
462f571c097SXiao Guangrong 			struct kvm_mtrr_range *range;
463f571c097SXiao Guangrong 			/* max address has been covered in var MTRRs. */
464f571c097SXiao Guangrong 			u64 start_max;
465f571c097SXiao Guangrong 		};
466f571c097SXiao Guangrong 	};
467f571c097SXiao Guangrong 
468f571c097SXiao Guangrong 	bool fixed;
469f571c097SXiao Guangrong };
470f571c097SXiao Guangrong 
mtrr_lookup_fixed_start(struct mtrr_iter * iter)471f571c097SXiao Guangrong static bool mtrr_lookup_fixed_start(struct mtrr_iter *iter)
472f571c097SXiao Guangrong {
473f571c097SXiao Guangrong 	int seg, index;
474f571c097SXiao Guangrong 
475f571c097SXiao Guangrong 	if (!fixed_mtrr_is_enabled(iter->mtrr_state))
476f571c097SXiao Guangrong 		return false;
477f571c097SXiao Guangrong 
478f571c097SXiao Guangrong 	seg = fixed_mtrr_addr_to_seg(iter->start);
479f571c097SXiao Guangrong 	if (seg < 0)
480f571c097SXiao Guangrong 		return false;
481f571c097SXiao Guangrong 
482f571c097SXiao Guangrong 	iter->fixed = true;
483f571c097SXiao Guangrong 	index = fixed_mtrr_addr_seg_to_range_index(iter->start, seg);
484f571c097SXiao Guangrong 	iter->index = index;
485f571c097SXiao Guangrong 	iter->seg = seg;
486f571c097SXiao Guangrong 	return true;
487f571c097SXiao Guangrong }
488f571c097SXiao Guangrong 
match_var_range(struct mtrr_iter * iter,struct kvm_mtrr_range * range)489f571c097SXiao Guangrong static bool match_var_range(struct mtrr_iter *iter,
490f571c097SXiao Guangrong 			    struct kvm_mtrr_range *range)
491f571c097SXiao Guangrong {
492f571c097SXiao Guangrong 	u64 start, end;
493f571c097SXiao Guangrong 
494f571c097SXiao Guangrong 	var_mtrr_range(range, &start, &end);
495f571c097SXiao Guangrong 	if (!(start >= iter->end || end <= iter->start)) {
496f571c097SXiao Guangrong 		iter->range = range;
497f571c097SXiao Guangrong 
498f571c097SXiao Guangrong 		/*
499f571c097SXiao Guangrong 		 * the function is called when we do kvm_mtrr.head walking.
500f571c097SXiao Guangrong 		 * Range has the minimum base address which interleaves
501f571c097SXiao Guangrong 		 * [looker->start_max, looker->end).
502f571c097SXiao Guangrong 		 */
503f571c097SXiao Guangrong 		iter->partial_map |= iter->start_max < start;
504f571c097SXiao Guangrong 
505f571c097SXiao Guangrong 		/* update the max address has been covered. */
506f571c097SXiao Guangrong 		iter->start_max = max(iter->start_max, end);
507f571c097SXiao Guangrong 		return true;
508f571c097SXiao Guangrong 	}
509f571c097SXiao Guangrong 
510f571c097SXiao Guangrong 	return false;
511f571c097SXiao Guangrong }
512f571c097SXiao Guangrong 
__mtrr_lookup_var_next(struct mtrr_iter * iter)513f571c097SXiao Guangrong static void __mtrr_lookup_var_next(struct mtrr_iter *iter)
514f571c097SXiao Guangrong {
515f571c097SXiao Guangrong 	struct kvm_mtrr *mtrr_state = iter->mtrr_state;
516f571c097SXiao Guangrong 
517f571c097SXiao Guangrong 	list_for_each_entry_continue(iter->range, &mtrr_state->head, node)
518f571c097SXiao Guangrong 		if (match_var_range(iter, iter->range))
519f571c097SXiao Guangrong 			return;
520f571c097SXiao Guangrong 
521f571c097SXiao Guangrong 	iter->range = NULL;
522f571c097SXiao Guangrong 	iter->partial_map |= iter->start_max < iter->end;
523f571c097SXiao Guangrong }
524f571c097SXiao Guangrong 
mtrr_lookup_var_start(struct mtrr_iter * iter)525f571c097SXiao Guangrong static void mtrr_lookup_var_start(struct mtrr_iter *iter)
526f571c097SXiao Guangrong {
527f571c097SXiao Guangrong 	struct kvm_mtrr *mtrr_state = iter->mtrr_state;
528f571c097SXiao Guangrong 
529f571c097SXiao Guangrong 	iter->fixed = false;
530f571c097SXiao Guangrong 	iter->start_max = iter->start;
53130b072ceSAlexis Dambricourt 	iter->range = NULL;
532f571c097SXiao Guangrong 	iter->range = list_prepare_entry(iter->range, &mtrr_state->head, node);
533f571c097SXiao Guangrong 
534f571c097SXiao Guangrong 	__mtrr_lookup_var_next(iter);
535f571c097SXiao Guangrong }
536f571c097SXiao Guangrong 
mtrr_lookup_fixed_next(struct mtrr_iter * iter)537f571c097SXiao Guangrong static void mtrr_lookup_fixed_next(struct mtrr_iter *iter)
538f571c097SXiao Guangrong {
539f571c097SXiao Guangrong 	/* terminate the lookup. */
540f571c097SXiao Guangrong 	if (fixed_mtrr_range_end_addr(iter->seg, iter->index) >= iter->end) {
541f571c097SXiao Guangrong 		iter->fixed = false;
542f571c097SXiao Guangrong 		iter->range = NULL;
543f571c097SXiao Guangrong 		return;
544f571c097SXiao Guangrong 	}
545f571c097SXiao Guangrong 
546f571c097SXiao Guangrong 	iter->index++;
547f571c097SXiao Guangrong 
548f571c097SXiao Guangrong 	/* have looked up for all fixed MTRRs. */
549f571c097SXiao Guangrong 	if (iter->index >= ARRAY_SIZE(iter->mtrr_state->fixed_ranges))
550f571c097SXiao Guangrong 		return mtrr_lookup_var_start(iter);
551f571c097SXiao Guangrong 
552f571c097SXiao Guangrong 	/* switch to next segment. */
553f571c097SXiao Guangrong 	if (iter->index > fixed_mtrr_seg_end_range_index(iter->seg))
554f571c097SXiao Guangrong 		iter->seg++;
555f571c097SXiao Guangrong }
556f571c097SXiao Guangrong 
mtrr_lookup_var_next(struct mtrr_iter * iter)557f571c097SXiao Guangrong static void mtrr_lookup_var_next(struct mtrr_iter *iter)
558f571c097SXiao Guangrong {
559f571c097SXiao Guangrong 	__mtrr_lookup_var_next(iter);
560f571c097SXiao Guangrong }
561f571c097SXiao Guangrong 
mtrr_lookup_start(struct mtrr_iter * iter)562f571c097SXiao Guangrong static void mtrr_lookup_start(struct mtrr_iter *iter)
563f571c097SXiao Guangrong {
564f571c097SXiao Guangrong 	if (!mtrr_is_enabled(iter->mtrr_state)) {
56510dc331fSXiao Guangrong 		iter->mtrr_disabled = true;
566f571c097SXiao Guangrong 		return;
567f571c097SXiao Guangrong 	}
568f571c097SXiao Guangrong 
569f571c097SXiao Guangrong 	if (!mtrr_lookup_fixed_start(iter))
570f571c097SXiao Guangrong 		mtrr_lookup_var_start(iter);
571f571c097SXiao Guangrong }
572f571c097SXiao Guangrong 
mtrr_lookup_init(struct mtrr_iter * iter,struct kvm_mtrr * mtrr_state,u64 start,u64 end)573f571c097SXiao Guangrong static void mtrr_lookup_init(struct mtrr_iter *iter,
574f571c097SXiao Guangrong 			     struct kvm_mtrr *mtrr_state, u64 start, u64 end)
575f571c097SXiao Guangrong {
576f571c097SXiao Guangrong 	iter->mtrr_state = mtrr_state;
577f571c097SXiao Guangrong 	iter->start = start;
578f571c097SXiao Guangrong 	iter->end = end;
57910dc331fSXiao Guangrong 	iter->mtrr_disabled = false;
580f571c097SXiao Guangrong 	iter->partial_map = false;
581f571c097SXiao Guangrong 	iter->fixed = false;
582f571c097SXiao Guangrong 	iter->range = NULL;
583f571c097SXiao Guangrong 
584f571c097SXiao Guangrong 	mtrr_lookup_start(iter);
585f571c097SXiao Guangrong }
586f571c097SXiao Guangrong 
mtrr_lookup_okay(struct mtrr_iter * iter)587f571c097SXiao Guangrong static bool mtrr_lookup_okay(struct mtrr_iter *iter)
588f571c097SXiao Guangrong {
589f571c097SXiao Guangrong 	if (iter->fixed) {
590f571c097SXiao Guangrong 		iter->mem_type = iter->mtrr_state->fixed_ranges[iter->index];
591f571c097SXiao Guangrong 		return true;
592f571c097SXiao Guangrong 	}
593f571c097SXiao Guangrong 
594f571c097SXiao Guangrong 	if (iter->range) {
595f571c097SXiao Guangrong 		iter->mem_type = iter->range->base & 0xff;
596f571c097SXiao Guangrong 		return true;
597f571c097SXiao Guangrong 	}
598f571c097SXiao Guangrong 
599f571c097SXiao Guangrong 	return false;
600f571c097SXiao Guangrong }
601f571c097SXiao Guangrong 
mtrr_lookup_next(struct mtrr_iter * iter)602f571c097SXiao Guangrong static void mtrr_lookup_next(struct mtrr_iter *iter)
603f571c097SXiao Guangrong {
604f571c097SXiao Guangrong 	if (iter->fixed)
605f571c097SXiao Guangrong 		mtrr_lookup_fixed_next(iter);
606f571c097SXiao Guangrong 	else
607f571c097SXiao Guangrong 		mtrr_lookup_var_next(iter);
608f571c097SXiao Guangrong }
609f571c097SXiao Guangrong 
610f571c097SXiao Guangrong #define mtrr_for_each_mem_type(_iter_, _mtrr_, _gpa_start_, _gpa_end_) \
611f571c097SXiao Guangrong 	for (mtrr_lookup_init(_iter_, _mtrr_, _gpa_start_, _gpa_end_); \
612f571c097SXiao Guangrong 	     mtrr_lookup_okay(_iter_); mtrr_lookup_next(_iter_))
613f571c097SXiao Guangrong 
kvm_mtrr_get_guest_memory_type(struct kvm_vcpu * vcpu,gfn_t gfn)6143f3f78b6SXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
615ff53604bSXiao Guangrong {
6163f3f78b6SXiao Guangrong 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
617fa612137SXiao Guangrong 	struct mtrr_iter iter;
618fa612137SXiao Guangrong 	u64 start, end;
619fa612137SXiao Guangrong 	int type = -1;
6203f3f78b6SXiao Guangrong 	const int wt_wb_mask = (1 << MTRR_TYPE_WRBACK)
6213f3f78b6SXiao Guangrong 			       | (1 << MTRR_TYPE_WRTHROUGH);
6223f3f78b6SXiao Guangrong 
6233f3f78b6SXiao Guangrong 	start = gfn_to_gpa(gfn);
624fa612137SXiao Guangrong 	end = start + PAGE_SIZE;
625ff53604bSXiao Guangrong 
626fa612137SXiao Guangrong 	mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
627fa612137SXiao Guangrong 		int curr_type = iter.mem_type;
628ff53604bSXiao Guangrong 
6293f3f78b6SXiao Guangrong 		/*
6303f3f78b6SXiao Guangrong 		 * Please refer to Intel SDM Volume 3: 11.11.4.1 MTRR
6313f3f78b6SXiao Guangrong 		 * Precedences.
6323f3f78b6SXiao Guangrong 		 */
6333f3f78b6SXiao Guangrong 
6343f3f78b6SXiao Guangrong 		if (type == -1) {
6353f3f78b6SXiao Guangrong 			type = curr_type;
636ff53604bSXiao Guangrong 			continue;
637ff53604bSXiao Guangrong 		}
638ff53604bSXiao Guangrong 
6393f3f78b6SXiao Guangrong 		/*
6403f3f78b6SXiao Guangrong 		 * If two or more variable memory ranges match and the
6413f3f78b6SXiao Guangrong 		 * memory types are identical, then that memory type is
6423f3f78b6SXiao Guangrong 		 * used.
6433f3f78b6SXiao Guangrong 		 */
6443f3f78b6SXiao Guangrong 		if (type == curr_type)
6453f3f78b6SXiao Guangrong 			continue;
6463f3f78b6SXiao Guangrong 
6473f3f78b6SXiao Guangrong 		/*
6483f3f78b6SXiao Guangrong 		 * If two or more variable memory ranges match and one of
6493f3f78b6SXiao Guangrong 		 * the memory types is UC, the UC memory type used.
6503f3f78b6SXiao Guangrong 		 */
6513f3f78b6SXiao Guangrong 		if (curr_type == MTRR_TYPE_UNCACHABLE)
652ff53604bSXiao Guangrong 			return MTRR_TYPE_UNCACHABLE;
653ff53604bSXiao Guangrong 
6543f3f78b6SXiao Guangrong 		/*
6553f3f78b6SXiao Guangrong 		 * If two or more variable memory ranges match and the
6563f3f78b6SXiao Guangrong 		 * memory types are WT and WB, the WT memory type is used.
6573f3f78b6SXiao Guangrong 		 */
6583f3f78b6SXiao Guangrong 		if (((1 << type) & wt_wb_mask) &&
6593f3f78b6SXiao Guangrong 		      ((1 << curr_type) & wt_wb_mask)) {
6603f3f78b6SXiao Guangrong 			type = MTRR_TYPE_WRTHROUGH;
6613f3f78b6SXiao Guangrong 			continue;
662ff53604bSXiao Guangrong 		}
663ff53604bSXiao Guangrong 
6643f3f78b6SXiao Guangrong 		/*
6653f3f78b6SXiao Guangrong 		 * For overlaps not defined by the above rules, processor
6663f3f78b6SXiao Guangrong 		 * behavior is undefined.
6673f3f78b6SXiao Guangrong 		 */
6683f3f78b6SXiao Guangrong 
6693f3f78b6SXiao Guangrong 		/* We use WB for this undefined behavior. :( */
6703f3f78b6SXiao Guangrong 		return MTRR_TYPE_WRBACK;
671ff53604bSXiao Guangrong 	}
672ff53604bSXiao Guangrong 
67310dc331fSXiao Guangrong 	if (iter.mtrr_disabled)
674e24dea2aSPaolo Bonzini 		return mtrr_disabled_type(vcpu);
67510dc331fSXiao Guangrong 
676fc1a8126SAlex Williamson 	/* not contained in any MTRRs. */
677fc1a8126SAlex Williamson 	if (type == -1)
678fc1a8126SAlex Williamson 		return mtrr_default_type(mtrr_state);
679fc1a8126SAlex Williamson 
680fa612137SXiao Guangrong 	/*
681fa612137SXiao Guangrong 	 * We just check one page, partially covered by MTRRs is
682fa612137SXiao Guangrong 	 * impossible.
683fa612137SXiao Guangrong 	 */
6843e5d2fdcSXiao Guangrong 	WARN_ON(iter.partial_map);
6853e5d2fdcSXiao Guangrong 
6863f3f78b6SXiao Guangrong 	return type;
687ff53604bSXiao Guangrong }
688ff53604bSXiao Guangrong EXPORT_SYMBOL_GPL(kvm_mtrr_get_guest_memory_type);
6896a39bbc5SXiao Guangrong 
kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu * vcpu,gfn_t gfn,int page_num)6906a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
6916a39bbc5SXiao Guangrong 					  int page_num)
6926a39bbc5SXiao Guangrong {
6936a39bbc5SXiao Guangrong 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
6946a39bbc5SXiao Guangrong 	struct mtrr_iter iter;
6956a39bbc5SXiao Guangrong 	u64 start, end;
6966a39bbc5SXiao Guangrong 	int type = -1;
6976a39bbc5SXiao Guangrong 
6986a39bbc5SXiao Guangrong 	start = gfn_to_gpa(gfn);
6996a39bbc5SXiao Guangrong 	end = gfn_to_gpa(gfn + page_num);
7006a39bbc5SXiao Guangrong 	mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
7016a39bbc5SXiao Guangrong 		if (type == -1) {
7026a39bbc5SXiao Guangrong 			type = iter.mem_type;
7036a39bbc5SXiao Guangrong 			continue;
7046a39bbc5SXiao Guangrong 		}
7056a39bbc5SXiao Guangrong 
7066a39bbc5SXiao Guangrong 		if (type != iter.mem_type)
7076a39bbc5SXiao Guangrong 			return false;
7086a39bbc5SXiao Guangrong 	}
7096a39bbc5SXiao Guangrong 
71010dc331fSXiao Guangrong 	if (iter.mtrr_disabled)
71110dc331fSXiao Guangrong 		return true;
71210dc331fSXiao Guangrong 
7136a39bbc5SXiao Guangrong 	if (!iter.partial_map)
7146a39bbc5SXiao Guangrong 		return true;
7156a39bbc5SXiao Guangrong 
7166a39bbc5SXiao Guangrong 	if (type == -1)
7176a39bbc5SXiao Guangrong 		return true;
7186a39bbc5SXiao Guangrong 
7196a39bbc5SXiao Guangrong 	return type == mtrr_default_type(mtrr_state);
7206a39bbc5SXiao Guangrong }
721