mtrr.c 16 KB

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