mtrr.c 16 KB

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