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