• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * vMTRR implementation
3  *
4  * Copyright (C) 2006 Qumranet, Inc.
5  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6  * Copyright(C) 2015 Intel Corporation.
7  *
8  * Authors:
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>
14  *
15  * This work is licensed under the terms of the GNU GPL, version 2.  See
16  * the COPYING file in the top-level directory.
17  */
18 
19 #include <linux/kvm_host.h>
20 #include <linux/nospec.h>
21 #include <asm/mtrr.h>
22 
23 #include "cpuid.h"
24 #include "mmu.h"
25 
26 #define IA32_MTRR_DEF_TYPE_E		(1ULL << 11)
27 #define IA32_MTRR_DEF_TYPE_FE		(1ULL << 10)
28 #define IA32_MTRR_DEF_TYPE_TYPE_MASK	(0xff)
29 
msr_mtrr_valid(unsigned msr)30 static bool msr_mtrr_valid(unsigned msr)
31 {
32 	switch (msr) {
33 	case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
34 	case MSR_MTRRfix64K_00000:
35 	case MSR_MTRRfix16K_80000:
36 	case MSR_MTRRfix16K_A0000:
37 	case MSR_MTRRfix4K_C0000:
38 	case MSR_MTRRfix4K_C8000:
39 	case MSR_MTRRfix4K_D0000:
40 	case MSR_MTRRfix4K_D8000:
41 	case MSR_MTRRfix4K_E0000:
42 	case MSR_MTRRfix4K_E8000:
43 	case MSR_MTRRfix4K_F0000:
44 	case MSR_MTRRfix4K_F8000:
45 	case MSR_MTRRdefType:
46 	case MSR_IA32_CR_PAT:
47 		return true;
48 	}
49 	return false;
50 }
51 
valid_pat_type(unsigned t)52 static bool valid_pat_type(unsigned t)
53 {
54 	return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
55 }
56 
valid_mtrr_type(unsigned t)57 static bool valid_mtrr_type(unsigned t)
58 {
59 	return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
60 }
61 
kvm_mtrr_valid(struct kvm_vcpu * vcpu,u32 msr,u64 data)62 bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
63 {
64 	int i;
65 	u64 mask;
66 
67 	if (!msr_mtrr_valid(msr))
68 		return false;
69 
70 	if (msr == MSR_IA32_CR_PAT) {
71 		for (i = 0; i < 8; i++)
72 			if (!valid_pat_type((data >> (i * 8)) & 0xff))
73 				return false;
74 		return true;
75 	} else if (msr == MSR_MTRRdefType) {
76 		if (data & ~0xcff)
77 			return false;
78 		return valid_mtrr_type(data & 0xff);
79 	} else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
80 		for (i = 0; i < 8 ; i++)
81 			if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
82 				return false;
83 		return true;
84 	}
85 
86 	/* variable MTRRs */
87 	WARN_ON(!(msr >= 0x200 && msr < 0x200 + 2 * KVM_NR_VAR_MTRR));
88 
89 	mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
90 	if ((msr & 1) == 0) {
91 		/* MTRR base */
92 		if (!valid_mtrr_type(data & 0xff))
93 			return false;
94 		mask |= 0xf00;
95 	} else
96 		/* MTRR mask */
97 		mask |= 0x7ff;
98 	if (data & mask) {
99 		kvm_inject_gp(vcpu, 0);
100 		return false;
101 	}
102 
103 	return true;
104 }
105 EXPORT_SYMBOL_GPL(kvm_mtrr_valid);
106 
mtrr_is_enabled(struct kvm_mtrr * mtrr_state)107 static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
108 {
109 	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
110 }
111 
fixed_mtrr_is_enabled(struct kvm_mtrr * mtrr_state)112 static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
113 {
114 	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
115 }
116 
mtrr_default_type(struct kvm_mtrr * mtrr_state)117 static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
118 {
119 	return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
120 }
121 
mtrr_disabled_type(struct kvm_vcpu * vcpu)122 static u8 mtrr_disabled_type(struct kvm_vcpu *vcpu)
123 {
124 	/*
125 	 * Intel SDM 11.11.2.2: all MTRRs are disabled when
126 	 * IA32_MTRR_DEF_TYPE.E bit is cleared, and the UC
127 	 * memory type is applied to all of physical memory.
128 	 *
129 	 * However, virtual machines can be run with CPUID such that
130 	 * there are no MTRRs.  In that case, the firmware will never
131 	 * enable MTRRs and it is obviously undesirable to run the
132 	 * guest entirely with UC memory and we use WB.
133 	 */
134 	if (guest_cpuid_has_mtrr(vcpu))
135 		return MTRR_TYPE_UNCACHABLE;
136 	else
137 		return MTRR_TYPE_WRBACK;
138 }
139 
140 /*
141 * Three terms are used in the following code:
142 * - segment, it indicates the address segments covered by fixed MTRRs.
143 * - unit, it corresponds to the MSR entry in the segment.
144 * - range, a range is covered in one memory cache type.
145 */
146 struct fixed_mtrr_segment {
147 	u64 start;
148 	u64 end;
149 
150 	int range_shift;
151 
152 	/* the start position in kvm_mtrr.fixed_ranges[]. */
153 	int range_start;
154 };
155 
156 static struct fixed_mtrr_segment fixed_seg_table[] = {
157 	/* MSR_MTRRfix64K_00000, 1 unit. 64K fixed mtrr. */
158 	{
159 		.start = 0x0,
160 		.end = 0x80000,
161 		.range_shift = 16, /* 64K */
162 		.range_start = 0,
163 	},
164 
165 	/*
166 	 * MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000, 2 units,
167 	 * 16K fixed mtrr.
168 	 */
169 	{
170 		.start = 0x80000,
171 		.end = 0xc0000,
172 		.range_shift = 14, /* 16K */
173 		.range_start = 8,
174 	},
175 
176 	/*
177 	 * MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000, 8 units,
178 	 * 4K fixed mtrr.
179 	 */
180 	{
181 		.start = 0xc0000,
182 		.end = 0x100000,
183 		.range_shift = 12, /* 12K */
184 		.range_start = 24,
185 	}
186 };
187 
188 /*
189  * The size of unit is covered in one MSR, one MSR entry contains
190  * 8 ranges so that unit size is always 8 * 2^range_shift.
191  */
fixed_mtrr_seg_unit_size(int seg)192 static u64 fixed_mtrr_seg_unit_size(int seg)
193 {
194 	return 8 << fixed_seg_table[seg].range_shift;
195 }
196 
fixed_msr_to_seg_unit(u32 msr,int * seg,int * unit)197 static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
198 {
199 	switch (msr) {
200 	case MSR_MTRRfix64K_00000:
201 		*seg = 0;
202 		*unit = 0;
203 		break;
204 	case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
205 		*seg = 1;
206 		*unit = array_index_nospec(
207 			msr - MSR_MTRRfix16K_80000,
208 			MSR_MTRRfix16K_A0000 - MSR_MTRRfix16K_80000 + 1);
209 		break;
210 	case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
211 		*seg = 2;
212 		*unit = array_index_nospec(
213 			msr - MSR_MTRRfix4K_C0000,
214 			MSR_MTRRfix4K_F8000 - MSR_MTRRfix4K_C0000 + 1);
215 		break;
216 	default:
217 		return false;
218 	}
219 
220 	return true;
221 }
222 
fixed_mtrr_seg_unit_range(int seg,int unit,u64 * start,u64 * end)223 static void fixed_mtrr_seg_unit_range(int seg, int unit, u64 *start, u64 *end)
224 {
225 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
226 	u64 unit_size = fixed_mtrr_seg_unit_size(seg);
227 
228 	*start = mtrr_seg->start + unit * unit_size;
229 	*end = *start + unit_size;
230 	WARN_ON(*end > mtrr_seg->end);
231 }
232 
fixed_mtrr_seg_unit_range_index(int seg,int unit)233 static int fixed_mtrr_seg_unit_range_index(int seg, int unit)
234 {
235 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
236 
237 	WARN_ON(mtrr_seg->start + unit * fixed_mtrr_seg_unit_size(seg)
238 		> mtrr_seg->end);
239 
240 	/* each unit has 8 ranges. */
241 	return mtrr_seg->range_start + 8 * unit;
242 }
243 
fixed_mtrr_seg_end_range_index(int seg)244 static int fixed_mtrr_seg_end_range_index(int seg)
245 {
246 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
247 	int n;
248 
249 	n = (mtrr_seg->end - mtrr_seg->start) >> mtrr_seg->range_shift;
250 	return mtrr_seg->range_start + n - 1;
251 }
252 
fixed_msr_to_range(u32 msr,u64 * start,u64 * end)253 static bool fixed_msr_to_range(u32 msr, u64 *start, u64 *end)
254 {
255 	int seg, unit;
256 
257 	if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
258 		return false;
259 
260 	fixed_mtrr_seg_unit_range(seg, unit, start, end);
261 	return true;
262 }
263 
fixed_msr_to_range_index(u32 msr)264 static int fixed_msr_to_range_index(u32 msr)
265 {
266 	int seg, unit;
267 
268 	if (!fixed_msr_to_seg_unit(msr, &seg, &unit))
269 		return -1;
270 
271 	return fixed_mtrr_seg_unit_range_index(seg, unit);
272 }
273 
fixed_mtrr_addr_to_seg(u64 addr)274 static int fixed_mtrr_addr_to_seg(u64 addr)
275 {
276 	struct fixed_mtrr_segment *mtrr_seg;
277 	int seg, seg_num = ARRAY_SIZE(fixed_seg_table);
278 
279 	for (seg = 0; seg < seg_num; seg++) {
280 		mtrr_seg = &fixed_seg_table[seg];
281 		if (mtrr_seg->start <= addr && addr < mtrr_seg->end)
282 			return seg;
283 	}
284 
285 	return -1;
286 }
287 
fixed_mtrr_addr_seg_to_range_index(u64 addr,int seg)288 static int fixed_mtrr_addr_seg_to_range_index(u64 addr, int seg)
289 {
290 	struct fixed_mtrr_segment *mtrr_seg;
291 	int index;
292 
293 	mtrr_seg = &fixed_seg_table[seg];
294 	index = mtrr_seg->range_start;
295 	index += (addr - mtrr_seg->start) >> mtrr_seg->range_shift;
296 	return index;
297 }
298 
fixed_mtrr_range_end_addr(int seg,int index)299 static u64 fixed_mtrr_range_end_addr(int seg, int index)
300 {
301 	struct fixed_mtrr_segment *mtrr_seg = &fixed_seg_table[seg];
302 	int pos = index - mtrr_seg->range_start;
303 
304 	return mtrr_seg->start + ((pos + 1) << mtrr_seg->range_shift);
305 }
306 
var_mtrr_range(struct kvm_mtrr_range * range,u64 * start,u64 * end)307 static void var_mtrr_range(struct kvm_mtrr_range *range, u64 *start, u64 *end)
308 {
309 	u64 mask;
310 
311 	*start = range->base & PAGE_MASK;
312 
313 	mask = range->mask & PAGE_MASK;
314 
315 	/* This cannot overflow because writing to the reserved bits of
316 	 * variable MTRRs causes a #GP.
317 	 */
318 	*end = (*start | ~mask) + 1;
319 }
320 
update_mtrr(struct kvm_vcpu * vcpu,u32 msr)321 static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
322 {
323 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
324 	gfn_t start, end;
325 	int index;
326 
327 	if (msr == MSR_IA32_CR_PAT || !tdp_enabled ||
328 	      !kvm_arch_has_noncoherent_dma(vcpu->kvm))
329 		return;
330 
331 	if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
332 		return;
333 
334 	/* fixed MTRRs. */
335 	if (fixed_msr_to_range(msr, &start, &end)) {
336 		if (!fixed_mtrr_is_enabled(mtrr_state))
337 			return;
338 	} else if (msr == MSR_MTRRdefType) {
339 		start = 0x0;
340 		end = ~0ULL;
341 	} else {
342 		/* variable range MTRRs. */
343 		index = (msr - 0x200) / 2;
344 		var_mtrr_range(&mtrr_state->var_ranges[index], &start, &end);
345 	}
346 
347 	kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
348 }
349 
var_mtrr_range_is_valid(struct kvm_mtrr_range * range)350 static bool var_mtrr_range_is_valid(struct kvm_mtrr_range *range)
351 {
352 	return (range->mask & (1 << 11)) != 0;
353 }
354 
set_var_mtrr_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data)355 static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
356 {
357 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
358 	struct kvm_mtrr_range *tmp, *cur;
359 	int index, is_mtrr_mask;
360 
361 	index = (msr - 0x200) / 2;
362 	is_mtrr_mask = msr - 0x200 - 2 * index;
363 	cur = &mtrr_state->var_ranges[index];
364 
365 	/* remove the entry if it's in the list. */
366 	if (var_mtrr_range_is_valid(cur))
367 		list_del(&mtrr_state->var_ranges[index].node);
368 
369 	/* Extend the mask with all 1 bits to the left, since those
370 	 * bits must implicitly be 0.  The bits are then cleared
371 	 * when reading them.
372 	 */
373 	if (!is_mtrr_mask)
374 		cur->base = data;
375 	else
376 		cur->mask = data | (-1LL << cpuid_maxphyaddr(vcpu));
377 
378 	/* add it to the list if it's enabled. */
379 	if (var_mtrr_range_is_valid(cur)) {
380 		list_for_each_entry(tmp, &mtrr_state->head, node)
381 			if (cur->base >= tmp->base)
382 				break;
383 		list_add_tail(&cur->node, &tmp->node);
384 	}
385 }
386 
kvm_mtrr_set_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data)387 int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
388 {
389 	int index;
390 
391 	if (!kvm_mtrr_valid(vcpu, msr, data))
392 		return 1;
393 
394 	index = fixed_msr_to_range_index(msr);
395 	if (index >= 0)
396 		*(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
397 	else if (msr == MSR_MTRRdefType)
398 		vcpu->arch.mtrr_state.deftype = data;
399 	else if (msr == MSR_IA32_CR_PAT)
400 		vcpu->arch.pat = data;
401 	else
402 		set_var_mtrr_msr(vcpu, msr, data);
403 
404 	update_mtrr(vcpu, msr);
405 	return 0;
406 }
407 
kvm_mtrr_get_msr(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata)408 int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
409 {
410 	int index;
411 
412 	/* MSR_MTRRcap is a readonly MSR. */
413 	if (msr == MSR_MTRRcap) {
414 		/*
415 		 * SMRR = 0
416 		 * WC = 1
417 		 * FIX = 1
418 		 * VCNT = KVM_NR_VAR_MTRR
419 		 */
420 		*pdata = 0x500 | KVM_NR_VAR_MTRR;
421 		return 0;
422 	}
423 
424 	if (!msr_mtrr_valid(msr))
425 		return 1;
426 
427 	index = fixed_msr_to_range_index(msr);
428 	if (index >= 0)
429 		*pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
430 	else if (msr == MSR_MTRRdefType)
431 		*pdata = vcpu->arch.mtrr_state.deftype;
432 	else if (msr == MSR_IA32_CR_PAT)
433 		*pdata = vcpu->arch.pat;
434 	else {	/* Variable MTRRs */
435 		int is_mtrr_mask;
436 
437 		index = (msr - 0x200) / 2;
438 		is_mtrr_mask = msr - 0x200 - 2 * index;
439 		if (!is_mtrr_mask)
440 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
441 		else
442 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].mask;
443 
444 		*pdata &= (1ULL << cpuid_maxphyaddr(vcpu)) - 1;
445 	}
446 
447 	return 0;
448 }
449 
kvm_vcpu_mtrr_init(struct kvm_vcpu * vcpu)450 void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu)
451 {
452 	INIT_LIST_HEAD(&vcpu->arch.mtrr_state.head);
453 }
454 
455 struct mtrr_iter {
456 	/* input fields. */
457 	struct kvm_mtrr *mtrr_state;
458 	u64 start;
459 	u64 end;
460 
461 	/* output fields. */
462 	int mem_type;
463 	/* mtrr is completely disabled? */
464 	bool mtrr_disabled;
465 	/* [start, end) is not fully covered in MTRRs? */
466 	bool partial_map;
467 
468 	/* private fields. */
469 	union {
470 		/* used for fixed MTRRs. */
471 		struct {
472 			int index;
473 			int seg;
474 		};
475 
476 		/* used for var MTRRs. */
477 		struct {
478 			struct kvm_mtrr_range *range;
479 			/* max address has been covered in var MTRRs. */
480 			u64 start_max;
481 		};
482 	};
483 
484 	bool fixed;
485 };
486 
mtrr_lookup_fixed_start(struct mtrr_iter * iter)487 static bool mtrr_lookup_fixed_start(struct mtrr_iter *iter)
488 {
489 	int seg, index;
490 
491 	if (!fixed_mtrr_is_enabled(iter->mtrr_state))
492 		return false;
493 
494 	seg = fixed_mtrr_addr_to_seg(iter->start);
495 	if (seg < 0)
496 		return false;
497 
498 	iter->fixed = true;
499 	index = fixed_mtrr_addr_seg_to_range_index(iter->start, seg);
500 	iter->index = index;
501 	iter->seg = seg;
502 	return true;
503 }
504 
match_var_range(struct mtrr_iter * iter,struct kvm_mtrr_range * range)505 static bool match_var_range(struct mtrr_iter *iter,
506 			    struct kvm_mtrr_range *range)
507 {
508 	u64 start, end;
509 
510 	var_mtrr_range(range, &start, &end);
511 	if (!(start >= iter->end || end <= iter->start)) {
512 		iter->range = range;
513 
514 		/*
515 		 * the function is called when we do kvm_mtrr.head walking.
516 		 * Range has the minimum base address which interleaves
517 		 * [looker->start_max, looker->end).
518 		 */
519 		iter->partial_map |= iter->start_max < start;
520 
521 		/* update the max address has been covered. */
522 		iter->start_max = max(iter->start_max, end);
523 		return true;
524 	}
525 
526 	return false;
527 }
528 
__mtrr_lookup_var_next(struct mtrr_iter * iter)529 static void __mtrr_lookup_var_next(struct mtrr_iter *iter)
530 {
531 	struct kvm_mtrr *mtrr_state = iter->mtrr_state;
532 
533 	list_for_each_entry_continue(iter->range, &mtrr_state->head, node)
534 		if (match_var_range(iter, iter->range))
535 			return;
536 
537 	iter->range = NULL;
538 	iter->partial_map |= iter->start_max < iter->end;
539 }
540 
mtrr_lookup_var_start(struct mtrr_iter * iter)541 static void mtrr_lookup_var_start(struct mtrr_iter *iter)
542 {
543 	struct kvm_mtrr *mtrr_state = iter->mtrr_state;
544 
545 	iter->fixed = false;
546 	iter->start_max = iter->start;
547 	iter->range = NULL;
548 	iter->range = list_prepare_entry(iter->range, &mtrr_state->head, node);
549 
550 	__mtrr_lookup_var_next(iter);
551 }
552 
mtrr_lookup_fixed_next(struct mtrr_iter * iter)553 static void mtrr_lookup_fixed_next(struct mtrr_iter *iter)
554 {
555 	/* terminate the lookup. */
556 	if (fixed_mtrr_range_end_addr(iter->seg, iter->index) >= iter->end) {
557 		iter->fixed = false;
558 		iter->range = NULL;
559 		return;
560 	}
561 
562 	iter->index++;
563 
564 	/* have looked up for all fixed MTRRs. */
565 	if (iter->index >= ARRAY_SIZE(iter->mtrr_state->fixed_ranges))
566 		return mtrr_lookup_var_start(iter);
567 
568 	/* switch to next segment. */
569 	if (iter->index > fixed_mtrr_seg_end_range_index(iter->seg))
570 		iter->seg++;
571 }
572 
mtrr_lookup_var_next(struct mtrr_iter * iter)573 static void mtrr_lookup_var_next(struct mtrr_iter *iter)
574 {
575 	__mtrr_lookup_var_next(iter);
576 }
577 
mtrr_lookup_start(struct mtrr_iter * iter)578 static void mtrr_lookup_start(struct mtrr_iter *iter)
579 {
580 	if (!mtrr_is_enabled(iter->mtrr_state)) {
581 		iter->mtrr_disabled = true;
582 		return;
583 	}
584 
585 	if (!mtrr_lookup_fixed_start(iter))
586 		mtrr_lookup_var_start(iter);
587 }
588 
mtrr_lookup_init(struct mtrr_iter * iter,struct kvm_mtrr * mtrr_state,u64 start,u64 end)589 static void mtrr_lookup_init(struct mtrr_iter *iter,
590 			     struct kvm_mtrr *mtrr_state, u64 start, u64 end)
591 {
592 	iter->mtrr_state = mtrr_state;
593 	iter->start = start;
594 	iter->end = end;
595 	iter->mtrr_disabled = false;
596 	iter->partial_map = false;
597 	iter->fixed = false;
598 	iter->range = NULL;
599 
600 	mtrr_lookup_start(iter);
601 }
602 
mtrr_lookup_okay(struct mtrr_iter * iter)603 static bool mtrr_lookup_okay(struct mtrr_iter *iter)
604 {
605 	if (iter->fixed) {
606 		iter->mem_type = iter->mtrr_state->fixed_ranges[iter->index];
607 		return true;
608 	}
609 
610 	if (iter->range) {
611 		iter->mem_type = iter->range->base & 0xff;
612 		return true;
613 	}
614 
615 	return false;
616 }
617 
mtrr_lookup_next(struct mtrr_iter * iter)618 static void mtrr_lookup_next(struct mtrr_iter *iter)
619 {
620 	if (iter->fixed)
621 		mtrr_lookup_fixed_next(iter);
622 	else
623 		mtrr_lookup_var_next(iter);
624 }
625 
626 #define mtrr_for_each_mem_type(_iter_, _mtrr_, _gpa_start_, _gpa_end_) \
627 	for (mtrr_lookup_init(_iter_, _mtrr_, _gpa_start_, _gpa_end_); \
628 	     mtrr_lookup_okay(_iter_); mtrr_lookup_next(_iter_))
629 
kvm_mtrr_get_guest_memory_type(struct kvm_vcpu * vcpu,gfn_t gfn)630 u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
631 {
632 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
633 	struct mtrr_iter iter;
634 	u64 start, end;
635 	int type = -1;
636 	const int wt_wb_mask = (1 << MTRR_TYPE_WRBACK)
637 			       | (1 << MTRR_TYPE_WRTHROUGH);
638 
639 	start = gfn_to_gpa(gfn);
640 	end = start + PAGE_SIZE;
641 
642 	mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
643 		int curr_type = iter.mem_type;
644 
645 		/*
646 		 * Please refer to Intel SDM Volume 3: 11.11.4.1 MTRR
647 		 * Precedences.
648 		 */
649 
650 		if (type == -1) {
651 			type = curr_type;
652 			continue;
653 		}
654 
655 		/*
656 		 * If two or more variable memory ranges match and the
657 		 * memory types are identical, then that memory type is
658 		 * used.
659 		 */
660 		if (type == curr_type)
661 			continue;
662 
663 		/*
664 		 * If two or more variable memory ranges match and one of
665 		 * the memory types is UC, the UC memory type used.
666 		 */
667 		if (curr_type == MTRR_TYPE_UNCACHABLE)
668 			return MTRR_TYPE_UNCACHABLE;
669 
670 		/*
671 		 * If two or more variable memory ranges match and the
672 		 * memory types are WT and WB, the WT memory type is used.
673 		 */
674 		if (((1 << type) & wt_wb_mask) &&
675 		      ((1 << curr_type) & wt_wb_mask)) {
676 			type = MTRR_TYPE_WRTHROUGH;
677 			continue;
678 		}
679 
680 		/*
681 		 * For overlaps not defined by the above rules, processor
682 		 * behavior is undefined.
683 		 */
684 
685 		/* We use WB for this undefined behavior. :( */
686 		return MTRR_TYPE_WRBACK;
687 	}
688 
689 	if (iter.mtrr_disabled)
690 		return mtrr_disabled_type(vcpu);
691 
692 	/* not contained in any MTRRs. */
693 	if (type == -1)
694 		return mtrr_default_type(mtrr_state);
695 
696 	/*
697 	 * We just check one page, partially covered by MTRRs is
698 	 * impossible.
699 	 */
700 	WARN_ON(iter.partial_map);
701 
702 	return type;
703 }
704 EXPORT_SYMBOL_GPL(kvm_mtrr_get_guest_memory_type);
705 
kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu * vcpu,gfn_t gfn,int page_num)706 bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
707 					  int page_num)
708 {
709 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
710 	struct mtrr_iter iter;
711 	u64 start, end;
712 	int type = -1;
713 
714 	start = gfn_to_gpa(gfn);
715 	end = gfn_to_gpa(gfn + page_num);
716 	mtrr_for_each_mem_type(&iter, mtrr_state, start, end) {
717 		if (type == -1) {
718 			type = iter.mem_type;
719 			continue;
720 		}
721 
722 		if (type != iter.mem_type)
723 			return false;
724 	}
725 
726 	if (iter.mtrr_disabled)
727 		return true;
728 
729 	if (!iter.partial_map)
730 		return true;
731 
732 	if (type == -1)
733 		return true;
734 
735 	return type == mtrr_default_type(mtrr_state);
736 }
737