xref: /openbmc/linux/arch/x86/kvm/mtrr.c (revision de9aef5e1ad6e504d6992df17dfa193e8ba5cc34)
1ff53604bSXiao Guangrong /*
2ff53604bSXiao Guangrong  * vMTRR implementation
3ff53604bSXiao Guangrong  *
4ff53604bSXiao Guangrong  * Copyright (C) 2006 Qumranet, Inc.
5ff53604bSXiao Guangrong  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6ff53604bSXiao Guangrong  * Copyright(C) 2015 Intel Corporation.
7ff53604bSXiao Guangrong  *
8ff53604bSXiao Guangrong  * Authors:
9ff53604bSXiao Guangrong  *   Yaniv Kamay  <yaniv@qumranet.com>
10ff53604bSXiao Guangrong  *   Avi Kivity   <avi@qumranet.com>
11ff53604bSXiao Guangrong  *   Marcelo Tosatti <mtosatti@redhat.com>
12ff53604bSXiao Guangrong  *   Paolo Bonzini <pbonzini@redhat.com>
13ff53604bSXiao Guangrong  *   Xiao Guangrong <guangrong.xiao@linux.intel.com>
14ff53604bSXiao Guangrong  *
15ff53604bSXiao Guangrong  * This work is licensed under the terms of the GNU GPL, version 2.  See
16ff53604bSXiao Guangrong  * the COPYING file in the top-level directory.
17ff53604bSXiao Guangrong  */
18ff53604bSXiao Guangrong 
19ff53604bSXiao Guangrong #include <linux/kvm_host.h>
20ff53604bSXiao Guangrong #include <asm/mtrr.h>
21ff53604bSXiao Guangrong 
22ff53604bSXiao Guangrong #include "cpuid.h"
23ff53604bSXiao Guangrong #include "mmu.h"
24ff53604bSXiao Guangrong 
2510fac2dcSXiao Guangrong #define IA32_MTRR_DEF_TYPE_E		(1ULL << 11)
2610fac2dcSXiao Guangrong #define IA32_MTRR_DEF_TYPE_FE		(1ULL << 10)
2710fac2dcSXiao Guangrong #define IA32_MTRR_DEF_TYPE_TYPE_MASK	(0xff)
2810fac2dcSXiao Guangrong 
29ff53604bSXiao Guangrong static bool msr_mtrr_valid(unsigned msr)
30ff53604bSXiao Guangrong {
31ff53604bSXiao Guangrong 	switch (msr) {
32ff53604bSXiao Guangrong 	case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
33ff53604bSXiao Guangrong 	case MSR_MTRRfix64K_00000:
34ff53604bSXiao Guangrong 	case MSR_MTRRfix16K_80000:
35ff53604bSXiao Guangrong 	case MSR_MTRRfix16K_A0000:
36ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_C0000:
37ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_C8000:
38ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_D0000:
39ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_D8000:
40ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_E0000:
41ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_E8000:
42ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_F0000:
43ff53604bSXiao Guangrong 	case MSR_MTRRfix4K_F8000:
44ff53604bSXiao Guangrong 	case MSR_MTRRdefType:
45ff53604bSXiao Guangrong 	case MSR_IA32_CR_PAT:
46ff53604bSXiao Guangrong 		return true;
47ff53604bSXiao Guangrong 	case 0x2f8:
48ff53604bSXiao Guangrong 		return true;
49ff53604bSXiao Guangrong 	}
50ff53604bSXiao Guangrong 	return false;
51ff53604bSXiao Guangrong }
52ff53604bSXiao Guangrong 
53ff53604bSXiao Guangrong static bool valid_pat_type(unsigned t)
54ff53604bSXiao Guangrong {
55ff53604bSXiao Guangrong 	return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
56ff53604bSXiao Guangrong }
57ff53604bSXiao Guangrong 
58ff53604bSXiao Guangrong static bool valid_mtrr_type(unsigned t)
59ff53604bSXiao Guangrong {
60ff53604bSXiao Guangrong 	return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
61ff53604bSXiao Guangrong }
62ff53604bSXiao Guangrong 
63ff53604bSXiao Guangrong bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
64ff53604bSXiao Guangrong {
65ff53604bSXiao Guangrong 	int i;
66ff53604bSXiao Guangrong 	u64 mask;
67ff53604bSXiao Guangrong 
68ff53604bSXiao Guangrong 	if (!msr_mtrr_valid(msr))
69ff53604bSXiao Guangrong 		return false;
70ff53604bSXiao Guangrong 
71ff53604bSXiao Guangrong 	if (msr == MSR_IA32_CR_PAT) {
72ff53604bSXiao Guangrong 		for (i = 0; i < 8; i++)
73ff53604bSXiao Guangrong 			if (!valid_pat_type((data >> (i * 8)) & 0xff))
74ff53604bSXiao Guangrong 				return false;
75ff53604bSXiao Guangrong 		return true;
76ff53604bSXiao Guangrong 	} else 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 */
88ff53604bSXiao Guangrong 	WARN_ON(!(msr >= 0x200 && msr < 0x200 + 2 * KVM_NR_VAR_MTRR));
89ff53604bSXiao Guangrong 
90ff53604bSXiao Guangrong 	mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
91ff53604bSXiao Guangrong 	if ((msr & 1) == 0) {
92ff53604bSXiao Guangrong 		/* MTRR base */
93ff53604bSXiao Guangrong 		if (!valid_mtrr_type(data & 0xff))
94ff53604bSXiao Guangrong 			return false;
95ff53604bSXiao Guangrong 		mask |= 0xf00;
96ff53604bSXiao Guangrong 	} else
97ff53604bSXiao Guangrong 		/* MTRR mask */
98ff53604bSXiao Guangrong 		mask |= 0x7ff;
99ff53604bSXiao Guangrong 	if (data & mask) {
100ff53604bSXiao Guangrong 		kvm_inject_gp(vcpu, 0);
101ff53604bSXiao Guangrong 		return false;
102ff53604bSXiao Guangrong 	}
103ff53604bSXiao Guangrong 
104ff53604bSXiao Guangrong 	return true;
105ff53604bSXiao Guangrong }
106ff53604bSXiao Guangrong EXPORT_SYMBOL_GPL(kvm_mtrr_valid);
107ff53604bSXiao Guangrong 
10810fac2dcSXiao Guangrong static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
10910fac2dcSXiao Guangrong {
11010fac2dcSXiao Guangrong 	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
11110fac2dcSXiao Guangrong }
11210fac2dcSXiao Guangrong 
11310fac2dcSXiao Guangrong static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
11410fac2dcSXiao Guangrong {
11510fac2dcSXiao Guangrong 	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
11610fac2dcSXiao Guangrong }
11710fac2dcSXiao Guangrong 
11810fac2dcSXiao Guangrong static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
11910fac2dcSXiao Guangrong {
12010fac2dcSXiao Guangrong 	return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
12110fac2dcSXiao Guangrong }
12210fac2dcSXiao Guangrong 
123*de9aef5eSXiao Guangrong /*
124*de9aef5eSXiao Guangrong * Three terms are used in the following code:
125*de9aef5eSXiao Guangrong * - segment, it indicates the address segments covered by fixed MTRRs.
126*de9aef5eSXiao Guangrong * - unit, it corresponds to the MSR entry in the segment.
127*de9aef5eSXiao Guangrong * - range, a range is covered in one memory cache type.
128*de9aef5eSXiao Guangrong */
129*de9aef5eSXiao Guangrong struct fixed_mtrr_segment {
130*de9aef5eSXiao Guangrong 	u64 start;
131*de9aef5eSXiao Guangrong 	u64 end;
132*de9aef5eSXiao Guangrong 
133*de9aef5eSXiao Guangrong 	int range_shift;
134*de9aef5eSXiao Guangrong 
135*de9aef5eSXiao Guangrong 	/* the start position in kvm_mtrr.fixed_ranges[]. */
136*de9aef5eSXiao Guangrong 	int range_start;
137*de9aef5eSXiao Guangrong };
138*de9aef5eSXiao Guangrong 
139*de9aef5eSXiao Guangrong static struct fixed_mtrr_segment fixed_seg_table[] = {
140*de9aef5eSXiao Guangrong 	/* MSR_MTRRfix64K_00000, 1 unit. 64K fixed mtrr. */
141*de9aef5eSXiao Guangrong 	{
142*de9aef5eSXiao Guangrong 		.start = 0x0,
143*de9aef5eSXiao Guangrong 		.end = 0x80000,
144*de9aef5eSXiao Guangrong 		.range_shift = 16, /* 64K */
145*de9aef5eSXiao Guangrong 		.range_start = 0,
146*de9aef5eSXiao Guangrong 	},
147*de9aef5eSXiao Guangrong 
148*de9aef5eSXiao Guangrong 	/*
149*de9aef5eSXiao Guangrong 	 * MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000, 2 units,
150*de9aef5eSXiao Guangrong 	 * 16K fixed mtrr.
151*de9aef5eSXiao Guangrong 	 */
152*de9aef5eSXiao Guangrong 	{
153*de9aef5eSXiao Guangrong 		.start = 0x80000,
154*de9aef5eSXiao Guangrong 		.end = 0xc0000,
155*de9aef5eSXiao Guangrong 		.range_shift = 14, /* 16K */
156*de9aef5eSXiao Guangrong 		.range_start = 8,
157*de9aef5eSXiao Guangrong 	},
158*de9aef5eSXiao Guangrong 
159*de9aef5eSXiao Guangrong 	/*
160*de9aef5eSXiao Guangrong 	 * MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000, 8 units,
161*de9aef5eSXiao Guangrong 	 * 4K fixed mtrr.
162*de9aef5eSXiao Guangrong 	 */
163*de9aef5eSXiao Guangrong 	{
164*de9aef5eSXiao Guangrong 		.start = 0xc0000,
165*de9aef5eSXiao Guangrong 		.end = 0x100000,
166*de9aef5eSXiao Guangrong 		.range_shift = 12, /* 12K */
167*de9aef5eSXiao Guangrong 		.range_start = 24,
168*de9aef5eSXiao Guangrong 	}
169*de9aef5eSXiao Guangrong };
170*de9aef5eSXiao Guangrong 
171*de9aef5eSXiao Guangrong /*
172*de9aef5eSXiao Guangrong  * The size of unit is covered in one MSR, one MSR entry contains
173*de9aef5eSXiao Guangrong  * 8 ranges so that unit size is always 8 * 2^range_shift.
174*de9aef5eSXiao Guangrong  */
175*de9aef5eSXiao Guangrong static u64 fixed_mtrr_seg_unit_size(int seg)
176*de9aef5eSXiao Guangrong {
177*de9aef5eSXiao Guangrong 	return 8 << fixed_seg_table[seg].range_shift;
178*de9aef5eSXiao Guangrong }
179*de9aef5eSXiao Guangrong 
180*de9aef5eSXiao Guangrong static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
181*de9aef5eSXiao Guangrong {
182*de9aef5eSXiao Guangrong 	switch (msr) {
183*de9aef5eSXiao Guangrong 	case MSR_MTRRfix64K_00000:
184*de9aef5eSXiao Guangrong 		*seg = 0;
185*de9aef5eSXiao Guangrong 		*unit = 0;
186*de9aef5eSXiao Guangrong 		break;
187*de9aef5eSXiao Guangrong 	case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
188*de9aef5eSXiao Guangrong 		*seg = 1;
189*de9aef5eSXiao Guangrong 		*unit = msr - MSR_MTRRfix16K_80000;
190*de9aef5eSXiao Guangrong 		break;
191*de9aef5eSXiao Guangrong 	case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
192*de9aef5eSXiao Guangrong 		*seg = 2;
193*de9aef5eSXiao Guangrong 		*unit = msr - MSR_MTRRfix4K_C0000;
194*de9aef5eSXiao Guangrong 		break;
195*de9aef5eSXiao Guangrong 	default:
196*de9aef5eSXiao Guangrong 		return false;
197*de9aef5eSXiao Guangrong 	}
198*de9aef5eSXiao Guangrong 
199*de9aef5eSXiao Guangrong 	return true;
200*de9aef5eSXiao Guangrong }
201*de9aef5eSXiao Guangrong 
202*de9aef5eSXiao Guangrong static void fixed_mtrr_seg_unit_range(int seg, int unit, u64 *start, u64 *end)
203*de9aef5eSXiao Guangrong {
204*de9aef5eSXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
205*de9aef5eSXiao Guangrong 	u64 unit_size = fixed_mtrr_seg_unit_size(seg);
206*de9aef5eSXiao Guangrong 
207*de9aef5eSXiao Guangrong 	*start = mtrr_seg->start + unit * unit_size;
208*de9aef5eSXiao Guangrong 	*end = *start + unit_size;
209*de9aef5eSXiao Guangrong 	WARN_ON(*end > mtrr_seg->end);
210*de9aef5eSXiao Guangrong }
211*de9aef5eSXiao Guangrong 
212*de9aef5eSXiao Guangrong static int fixed_mtrr_seg_unit_range_index(int seg, int unit)
213*de9aef5eSXiao Guangrong {
214*de9aef5eSXiao Guangrong 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
215*de9aef5eSXiao Guangrong 
216*de9aef5eSXiao Guangrong 	WARN_ON(mtrr_seg->start + unit * fixed_mtrr_seg_unit_size(seg)
217*de9aef5eSXiao Guangrong 		> mtrr_seg->end);
218*de9aef5eSXiao Guangrong 
219*de9aef5eSXiao Guangrong 	/* each unit has 8 ranges. */
220*de9aef5eSXiao Guangrong 	return mtrr_seg->range_start + 8 * unit;
221*de9aef5eSXiao Guangrong }
222*de9aef5eSXiao Guangrong 
223*de9aef5eSXiao Guangrong static bool fixed_msr_to_range(u32 msr, u64 *start, u64 *end)
224*de9aef5eSXiao Guangrong {
225*de9aef5eSXiao Guangrong 	int seg, unit;
226*de9aef5eSXiao Guangrong 
227*de9aef5eSXiao Guangrong 	if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
228*de9aef5eSXiao Guangrong 		return false;
229*de9aef5eSXiao Guangrong 
230*de9aef5eSXiao Guangrong 	fixed_mtrr_seg_unit_range(seg, unit, start, end);
231*de9aef5eSXiao Guangrong 	return true;
232*de9aef5eSXiao Guangrong }
233*de9aef5eSXiao Guangrong 
234*de9aef5eSXiao Guangrong static int fixed_msr_to_range_index(u32 msr)
235*de9aef5eSXiao Guangrong {
236*de9aef5eSXiao Guangrong 	int seg, unit;
237*de9aef5eSXiao Guangrong 
238*de9aef5eSXiao Guangrong 	if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
239*de9aef5eSXiao Guangrong 		return -1;
240*de9aef5eSXiao Guangrong 
241*de9aef5eSXiao Guangrong 	return fixed_mtrr_seg_unit_range_index(seg, unit);
242*de9aef5eSXiao Guangrong }
243*de9aef5eSXiao Guangrong 
244ff53604bSXiao Guangrong static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
245ff53604bSXiao Guangrong {
24670109e7dSXiao Guangrong 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
247ff53604bSXiao Guangrong 	gfn_t start, end, mask;
248ff53604bSXiao Guangrong 	int index;
249ff53604bSXiao Guangrong 
250ff53604bSXiao Guangrong 	if (msr == MSR_IA32_CR_PAT || !tdp_enabled ||
251ff53604bSXiao Guangrong 	      !kvm_arch_has_noncoherent_dma(vcpu->kvm))
252ff53604bSXiao Guangrong 		return;
253ff53604bSXiao Guangrong 
25410fac2dcSXiao Guangrong 	if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
255ff53604bSXiao Guangrong 		return;
256ff53604bSXiao Guangrong 
257*de9aef5eSXiao Guangrong 	/* fixed MTRRs. */
258*de9aef5eSXiao Guangrong 	if (fixed_msr_to_range(msr, &start, &end)) {
259*de9aef5eSXiao Guangrong 		if (!fixed_mtrr_is_enabled(mtrr_state))
260*de9aef5eSXiao Guangrong 			return;
261*de9aef5eSXiao Guangrong 	} else if (msr == MSR_MTRRdefType) {
262ff53604bSXiao Guangrong 		start = 0x0;
263ff53604bSXiao Guangrong 		end = ~0ULL;
264*de9aef5eSXiao Guangrong 	} else {
265ff53604bSXiao Guangrong 		/* variable range MTRRs. */
266ff53604bSXiao Guangrong 		index = (msr - 0x200) / 2;
26786fd5270SXiao Guangrong 		start = mtrr_state->var_ranges[index].base & PAGE_MASK;
26886fd5270SXiao Guangrong 		mask = mtrr_state->var_ranges[index].mask & PAGE_MASK;
269ff53604bSXiao Guangrong 		mask |= ~0ULL << cpuid_maxphyaddr(vcpu);
270ff53604bSXiao Guangrong 
271ff53604bSXiao Guangrong 		end = ((start & mask) | ~mask) + 1;
272ff53604bSXiao Guangrong 	}
273ff53604bSXiao Guangrong 
274ff53604bSXiao Guangrong 	kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
275ff53604bSXiao Guangrong }
276ff53604bSXiao Guangrong 
277ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
278ff53604bSXiao Guangrong {
279*de9aef5eSXiao Guangrong 	int index;
280ff53604bSXiao Guangrong 
281ff53604bSXiao Guangrong 	if (!kvm_mtrr_valid(vcpu, msr, data))
282ff53604bSXiao Guangrong 		return 1;
283ff53604bSXiao Guangrong 
284*de9aef5eSXiao Guangrong 	index = fixed_msr_to_range_index(msr);
285*de9aef5eSXiao Guangrong 	if (index >= 0)
286*de9aef5eSXiao Guangrong 		*(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
287*de9aef5eSXiao Guangrong 	else if (msr == MSR_MTRRdefType)
28810fac2dcSXiao Guangrong 		vcpu->arch.mtrr_state.deftype = data;
289ff53604bSXiao Guangrong 	else if (msr == MSR_IA32_CR_PAT)
290ff53604bSXiao Guangrong 		vcpu->arch.pat = data;
291ff53604bSXiao Guangrong 	else {	/* Variable MTRRs */
292*de9aef5eSXiao Guangrong 		int is_mtrr_mask;
293ff53604bSXiao Guangrong 
294*de9aef5eSXiao Guangrong 		index = (msr - 0x200) / 2;
295*de9aef5eSXiao Guangrong 		is_mtrr_mask = msr - 0x200 - 2 * index;
296ff53604bSXiao Guangrong 		if (!is_mtrr_mask)
297*de9aef5eSXiao Guangrong 			vcpu->arch.mtrr_state.var_ranges[index].base = data;
298ff53604bSXiao Guangrong 		else
299*de9aef5eSXiao Guangrong 			vcpu->arch.mtrr_state.var_ranges[index].mask = data;
300ff53604bSXiao Guangrong 	}
301ff53604bSXiao Guangrong 
302ff53604bSXiao Guangrong 	update_mtrr(vcpu, msr);
303ff53604bSXiao Guangrong 	return 0;
304ff53604bSXiao Guangrong }
305ff53604bSXiao Guangrong 
306ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
307ff53604bSXiao Guangrong {
308*de9aef5eSXiao Guangrong 	int index;
309ff53604bSXiao Guangrong 
310eb839917SXiao Guangrong 	/* MSR_MTRRcap is a readonly MSR. */
311eb839917SXiao Guangrong 	if (msr == MSR_MTRRcap) {
312eb839917SXiao Guangrong 		/*
313eb839917SXiao Guangrong 		 * SMRR = 0
314eb839917SXiao Guangrong 		 * WC = 1
315eb839917SXiao Guangrong 		 * FIX = 1
316eb839917SXiao Guangrong 		 * VCNT = KVM_NR_VAR_MTRR
317eb839917SXiao Guangrong 		 */
318eb839917SXiao Guangrong 		*pdata = 0x500 | KVM_NR_VAR_MTRR;
319eb839917SXiao Guangrong 		return 0;
320eb839917SXiao Guangrong 	}
321eb839917SXiao Guangrong 
322ff53604bSXiao Guangrong 	if (!msr_mtrr_valid(msr))
323ff53604bSXiao Guangrong 		return 1;
324ff53604bSXiao Guangrong 
325*de9aef5eSXiao Guangrong 	index = fixed_msr_to_range_index(msr);
326*de9aef5eSXiao Guangrong 	if (index >= 0)
327*de9aef5eSXiao Guangrong 		*pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
328*de9aef5eSXiao Guangrong 	else if (msr == MSR_MTRRdefType)
32910fac2dcSXiao Guangrong 		*pdata = vcpu->arch.mtrr_state.deftype;
330ff53604bSXiao Guangrong 	else if (msr == MSR_IA32_CR_PAT)
331ff53604bSXiao Guangrong 		*pdata = vcpu->arch.pat;
332ff53604bSXiao Guangrong 	else {	/* Variable MTRRs */
333*de9aef5eSXiao Guangrong 		int is_mtrr_mask;
334ff53604bSXiao Guangrong 
335*de9aef5eSXiao Guangrong 		index = (msr - 0x200) / 2;
336*de9aef5eSXiao Guangrong 		is_mtrr_mask = msr - 0x200 - 2 * index;
337ff53604bSXiao Guangrong 		if (!is_mtrr_mask)
338*de9aef5eSXiao Guangrong 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
339ff53604bSXiao Guangrong 		else
340*de9aef5eSXiao Guangrong 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].mask;
341ff53604bSXiao Guangrong 	}
342ff53604bSXiao Guangrong 
343ff53604bSXiao Guangrong 	return 0;
344ff53604bSXiao Guangrong }
345ff53604bSXiao Guangrong 
3463f3f78b6SXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
347ff53604bSXiao Guangrong {
3483f3f78b6SXiao Guangrong 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
3493f3f78b6SXiao Guangrong 	u64 base, mask, start;
3503f3f78b6SXiao Guangrong 	int i, num_var_ranges, type;
3513f3f78b6SXiao Guangrong 	const int wt_wb_mask = (1 << MTRR_TYPE_WRBACK)
3523f3f78b6SXiao Guangrong 			       | (1 << MTRR_TYPE_WRTHROUGH);
3533f3f78b6SXiao Guangrong 
3543f3f78b6SXiao Guangrong 	start = gfn_to_gpa(gfn);
3553f3f78b6SXiao Guangrong 	num_var_ranges = KVM_NR_VAR_MTRR;
3563f3f78b6SXiao Guangrong 	type = -1;
357ff53604bSXiao Guangrong 
358ff53604bSXiao Guangrong 	/* MTRR is completely disabled, use UC for all of physical memory. */
35910fac2dcSXiao Guangrong 	if (!mtrr_is_enabled(mtrr_state))
360ff53604bSXiao Guangrong 		return MTRR_TYPE_UNCACHABLE;
361ff53604bSXiao Guangrong 
362ff53604bSXiao Guangrong 	/* Look in fixed ranges. Just return the type as per start */
36310fac2dcSXiao Guangrong 	if (fixed_mtrr_is_enabled(mtrr_state) && (start < 0x100000)) {
364ff53604bSXiao Guangrong 		int idx;
365ff53604bSXiao Guangrong 
366ff53604bSXiao Guangrong 		if (start < 0x80000) {
367ff53604bSXiao Guangrong 			idx = 0;
368ff53604bSXiao Guangrong 			idx += (start >> 16);
369ff53604bSXiao Guangrong 			return mtrr_state->fixed_ranges[idx];
370ff53604bSXiao Guangrong 		} else if (start < 0xC0000) {
371ff53604bSXiao Guangrong 			idx = 1 * 8;
372ff53604bSXiao Guangrong 			idx += ((start - 0x80000) >> 14);
373ff53604bSXiao Guangrong 			return mtrr_state->fixed_ranges[idx];
374ff53604bSXiao Guangrong 		} else if (start < 0x1000000) {
375ff53604bSXiao Guangrong 			idx = 3 * 8;
376ff53604bSXiao Guangrong 			idx += ((start - 0xC0000) >> 12);
377ff53604bSXiao Guangrong 			return mtrr_state->fixed_ranges[idx];
378ff53604bSXiao Guangrong 		}
379ff53604bSXiao Guangrong 	}
380ff53604bSXiao Guangrong 
381ff53604bSXiao Guangrong 	/*
382ff53604bSXiao Guangrong 	 * Look in variable ranges
383ff53604bSXiao Guangrong 	 * Look of multiple ranges matching this address and pick type
384ff53604bSXiao Guangrong 	 * as per MTRR precedence
385ff53604bSXiao Guangrong 	 */
386ff53604bSXiao Guangrong 	for (i = 0; i < num_var_ranges; ++i) {
3873f3f78b6SXiao Guangrong 		int curr_type;
388ff53604bSXiao Guangrong 
38986fd5270SXiao Guangrong 		if (!(mtrr_state->var_ranges[i].mask & (1 << 11)))
390ff53604bSXiao Guangrong 			continue;
391ff53604bSXiao Guangrong 
39286fd5270SXiao Guangrong 		base = mtrr_state->var_ranges[i].base & PAGE_MASK;
39386fd5270SXiao Guangrong 		mask = mtrr_state->var_ranges[i].mask & PAGE_MASK;
394ff53604bSXiao Guangrong 
395ff53604bSXiao Guangrong 		if ((start & mask) != (base & mask))
396ff53604bSXiao Guangrong 			continue;
397ff53604bSXiao Guangrong 
3983f3f78b6SXiao Guangrong 		/*
3993f3f78b6SXiao Guangrong 		 * Please refer to Intel SDM Volume 3: 11.11.4.1 MTRR
4003f3f78b6SXiao Guangrong 		 * Precedences.
4013f3f78b6SXiao Guangrong 		 */
4023f3f78b6SXiao Guangrong 
4033f3f78b6SXiao Guangrong 		curr_type = mtrr_state->var_ranges[i].base & 0xff;
4043f3f78b6SXiao Guangrong 		if (type == -1) {
4053f3f78b6SXiao Guangrong 			type = curr_type;
406ff53604bSXiao Guangrong 			continue;
407ff53604bSXiao Guangrong 		}
408ff53604bSXiao Guangrong 
4093f3f78b6SXiao Guangrong 		/*
4103f3f78b6SXiao Guangrong 		 * If two or more variable memory ranges match and the
4113f3f78b6SXiao Guangrong 		 * memory types are identical, then that memory type is
4123f3f78b6SXiao Guangrong 		 * used.
4133f3f78b6SXiao Guangrong 		 */
4143f3f78b6SXiao Guangrong 		if (type == curr_type)
4153f3f78b6SXiao Guangrong 			continue;
4163f3f78b6SXiao Guangrong 
4173f3f78b6SXiao Guangrong 		/*
4183f3f78b6SXiao Guangrong 		 * If two or more variable memory ranges match and one of
4193f3f78b6SXiao Guangrong 		 * the memory types is UC, the UC memory type used.
4203f3f78b6SXiao Guangrong 		 */
4213f3f78b6SXiao Guangrong 		if (curr_type == MTRR_TYPE_UNCACHABLE)
422ff53604bSXiao Guangrong 			return MTRR_TYPE_UNCACHABLE;
423ff53604bSXiao Guangrong 
4243f3f78b6SXiao Guangrong 		/*
4253f3f78b6SXiao Guangrong 		 * If two or more variable memory ranges match and the
4263f3f78b6SXiao Guangrong 		 * memory types are WT and WB, the WT memory type is used.
4273f3f78b6SXiao Guangrong 		 */
4283f3f78b6SXiao Guangrong 		if (((1 << type) & wt_wb_mask) &&
4293f3f78b6SXiao Guangrong 		      ((1 << curr_type) & wt_wb_mask)) {
4303f3f78b6SXiao Guangrong 			type = MTRR_TYPE_WRTHROUGH;
4313f3f78b6SXiao Guangrong 			continue;
432ff53604bSXiao Guangrong 		}
433ff53604bSXiao Guangrong 
4343f3f78b6SXiao Guangrong 		/*
4353f3f78b6SXiao Guangrong 		 * For overlaps not defined by the above rules, processor
4363f3f78b6SXiao Guangrong 		 * behavior is undefined.
4373f3f78b6SXiao Guangrong 		 */
4383f3f78b6SXiao Guangrong 
4393f3f78b6SXiao Guangrong 		/* We use WB for this undefined behavior. :( */
4403f3f78b6SXiao Guangrong 		return MTRR_TYPE_WRBACK;
441ff53604bSXiao Guangrong 	}
442ff53604bSXiao Guangrong 
4433f3f78b6SXiao Guangrong 	if (type != -1)
4443f3f78b6SXiao Guangrong 		return type;
445ff53604bSXiao Guangrong 
44610fac2dcSXiao Guangrong 	return mtrr_default_type(mtrr_state);
447ff53604bSXiao Guangrong }
448ff53604bSXiao Guangrong EXPORT_SYMBOL_GPL(kvm_mtrr_get_guest_memory_type);
449