mtrr.c 16 KB

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