Lines Matching +full:mixed +full:- +full:signals

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
5 * This module enables machines with Intel VT-x extensions to run virtual
57 #include <asm/spec-ctrl.h>
64 int __read_mostly nx_huge_pages = -1;
100 * When setting this variable to true it enables Two-Dimensional-Paging
102 * 1. the guest-virtual to guest-physical
103 * 2. while doing 1. it walks guest-physical to host-physical
204 return !!(regs->reg & flag); \
226 return !!(mmu->cpu_role. base_or_ext . reg##_##name); \
239 return mmu->cpu_role.base.level > 0; in is_cr0_pg()
244 return !mmu->cpu_role.base.has_4_byte_gpte; in is_cr4_pae()
252 .efer = vcpu->arch.efer, in vcpu_to_role_regs()
266 if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && mmu->get_guest_pgd == get_guest_cr3) in kvm_mmu_get_guest_pgd()
269 return mmu->get_guest_pgd(vcpu); in kvm_mmu_get_guest_pgd()
289 kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level); in kvm_flush_remote_tlbs_sptep()
320 gen = kvm_vcpu_memslots(vcpu)->generation; in check_mmio_spte()
377 sp->clear_spte_count++; in count_spte_clear()
387 ssptep->spte_high = sspte.spte_high; in __set_spte()
396 WRITE_ONCE(ssptep->spte_low, sspte.spte_low); in __set_spte()
406 WRITE_ONCE(ssptep->spte_low, sspte.spte_low); in __update_clear_spte_fast()
414 ssptep->spte_high = sspte.spte_high; in __update_clear_spte_fast()
426 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low); in __update_clear_spte_slow()
427 orig.spte_high = ssptep->spte_high; in __update_clear_spte_slow()
428 ssptep->spte_high = sspte.spte_high; in __update_clear_spte_slow()
440 * we need to protect against in-progress updates of the spte.
443 * for the high part of the spte. The race is fine for a present->non-present
444 * change (because the high part of the spte is ignored for non-present spte),
445 * but for a present->present change we must reread the spte.
447 * All such changes are done in two steps (present->non-present and
448 * non-present->present), hence it is enough to count the number of
449 * present->non-present updates: if it changed while reading the spte,
459 count = sp->clear_spte_count; in __get_spte_lockless()
462 spte.spte_low = orig->spte_low; in __get_spte_lockless()
465 spte.spte_high = orig->spte_high; in __get_spte_lockless()
468 if (unlikely(spte.spte_low != orig->spte_low || in __get_spte_lockless()
469 count != sp->clear_spte_count)) in __get_spte_lockless()
517 * Whenever an MMU-writable SPTE is overwritten with a read-only SPTE, remote
518 * TLBs must be flushed. Otherwise rmap_write_protect will find a read-only
532 * For the spte updated out of mmu-lock is safe, since in mmu_spte_update()
568 int level = sptep_to_sp(sptep)->role.level; in mmu_spte_clear_track_bits()
580 kvm_update_page_stats(kvm, level, -1); in mmu_spte_clear_track_bits()
619 return tdp_mmu_enabled && vcpu->arch.mmu->root_role.direct; in is_tdp_mmu_active()
628 * Prevent page table teardown by making any free-er wait during in walk_shadow_page_lockless_begin()
635 * to vcpu->mode. in walk_shadow_page_lockless_begin()
637 smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES); in walk_shadow_page_lockless_begin()
647 * Make sure the write to vcpu->mode is not reordered in front of in walk_shadow_page_lockless_end()
651 smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE); in walk_shadow_page_lockless_end()
661 r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache, in mmu_topup_memory_caches()
665 r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadow_page_cache, in mmu_topup_memory_caches()
670 r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadowed_info_cache, in mmu_topup_memory_caches()
675 return kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, in mmu_topup_memory_caches()
681 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache); in mmu_free_memory_caches()
682 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_shadow_page_cache); in mmu_free_memory_caches()
683 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_shadowed_info_cache); in mmu_free_memory_caches()
684 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache); in mmu_free_memory_caches()
696 if (sp->role.passthrough) in kvm_mmu_page_get_gfn()
697 return sp->gfn; in kvm_mmu_page_get_gfn()
699 if (sp->shadowed_translation) in kvm_mmu_page_get_gfn()
700 return sp->shadowed_translation[index] >> PAGE_SHIFT; in kvm_mmu_page_get_gfn()
702 return sp->gfn + (index << ((sp->role.level - 1) * SPTE_LEVEL_BITS)); in kvm_mmu_page_get_gfn()
713 if (sp->shadowed_translation) in kvm_mmu_page_get_access()
714 return sp->shadowed_translation[index] & ACC_ALL; in kvm_mmu_page_get_access()
717 * For direct MMUs (e.g. TDP or non-paging guests) or passthrough SPs, in kvm_mmu_page_get_access()
726 * In both cases, sp->role.access contains the correct access bits. in kvm_mmu_page_get_access()
728 return sp->role.access; in kvm_mmu_page_get_access()
734 if (sp->shadowed_translation) { in kvm_mmu_page_set_translation()
735 sp->shadowed_translation[index] = (gfn << PAGE_SHIFT) | access; in kvm_mmu_page_set_translation()
741 sp->role.passthrough ? "passthrough" : "direct", in kvm_mmu_page_set_translation()
742 sp->gfn, kvm_mmu_page_get_access(sp, index), access); in kvm_mmu_page_set_translation()
746 sp->role.passthrough ? "passthrough" : "direct", in kvm_mmu_page_set_translation()
747 sp->gfn, kvm_mmu_page_get_gfn(sp, index), gfn); in kvm_mmu_page_set_translation()
767 idx = gfn_to_index(gfn, slot->base_gfn, level); in lpage_info_slot()
768 return &slot->arch.lpage_info[level - 2][idx]; in lpage_info_slot()
773 * attributes are mixed, i.e. not identical for all gfns at the current level.
788 old = linfo->disallow_lpage; in update_gfn_disallow_lpage_count()
789 linfo->disallow_lpage += count; in update_gfn_disallow_lpage_count()
790 WARN_ON_ONCE((old ^ linfo->disallow_lpage) & KVM_LPAGE_MIXED_FLAG); in update_gfn_disallow_lpage_count()
801 update_gfn_disallow_lpage_count(slot, gfn, -1); in kvm_mmu_gfn_allow_lpage()
810 kvm->arch.indirect_shadow_pages++; in account_shadowed()
812 * Ensure indirect_shadow_pages is elevated prior to re-reading guest in account_shadowed()
813 * child PTEs in FNAME(gpte_changed), i.e. guarantee either in-flight in account_shadowed()
814 * emulated writes are visible before re-reading guest PTEs, or that in account_shadowed()
820 gfn = sp->gfn; in account_shadowed()
821 slots = kvm_memslots_for_spte_role(kvm, sp->role); in account_shadowed()
824 /* the non-leaf shadow pages are keeping readonly. */ in account_shadowed()
825 if (sp->role.level > PG_LEVEL_4K) in account_shadowed()
844 if (!list_empty(&sp->possible_nx_huge_page_link)) in track_possible_nx_huge_page()
847 ++kvm->stat.nx_lpage_splits; in track_possible_nx_huge_page()
848 list_add_tail(&sp->possible_nx_huge_page_link, in track_possible_nx_huge_page()
849 &kvm->arch.possible_nx_huge_pages); in track_possible_nx_huge_page()
855 sp->nx_huge_page_disallowed = true; in account_nx_huge_page()
867 kvm->arch.indirect_shadow_pages--; in unaccount_shadowed()
868 gfn = sp->gfn; in unaccount_shadowed()
869 slots = kvm_memslots_for_spte_role(kvm, sp->role); in unaccount_shadowed()
871 if (sp->role.level > PG_LEVEL_4K) in unaccount_shadowed()
879 if (list_empty(&sp->possible_nx_huge_page_link)) in untrack_possible_nx_huge_page()
882 --kvm->stat.nx_lpage_splits; in untrack_possible_nx_huge_page()
883 list_del_init(&sp->possible_nx_huge_page_link); in untrack_possible_nx_huge_page()
888 sp->nx_huge_page_disallowed = false; in unaccount_nx_huge_page()
900 if (!slot || slot->flags & KVM_MEMSLOT_INVALID) in gfn_to_memslot_dirty_bitmap()
911 * If the bit zero of rmap_head->val is clear, then it points to the only spte
912 * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
926 if (!rmap_head->val) { in pte_list_add()
927 rmap_head->val = (unsigned long)spte; in pte_list_add()
928 } else if (!(rmap_head->val & KVM_RMAP_MANY)) { in pte_list_add()
930 desc->sptes[0] = (u64 *)rmap_head->val; in pte_list_add()
931 desc->sptes[1] = spte; in pte_list_add()
932 desc->spte_count = 2; in pte_list_add()
933 desc->tail_count = 0; in pte_list_add()
934 rmap_head->val = (unsigned long)desc | KVM_RMAP_MANY; in pte_list_add()
937 desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in pte_list_add()
938 count = desc->tail_count + desc->spte_count; in pte_list_add()
944 if (desc->spte_count == PTE_LIST_EXT) { in pte_list_add()
946 desc->more = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in pte_list_add()
947 desc->spte_count = 0; in pte_list_add()
948 desc->tail_count = count; in pte_list_add()
949 rmap_head->val = (unsigned long)desc | KVM_RMAP_MANY; in pte_list_add()
951 desc->sptes[desc->spte_count++] = spte; in pte_list_add()
960 struct pte_list_desc *head_desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in pte_list_desc_remove_entry()
961 int j = head_desc->spte_count - 1; in pte_list_desc_remove_entry()
971 * Replace the to-be-freed SPTE with the last valid entry from the head in pte_list_desc_remove_entry()
975 desc->sptes[i] = head_desc->sptes[j]; in pte_list_desc_remove_entry()
976 head_desc->sptes[j] = NULL; in pte_list_desc_remove_entry()
977 head_desc->spte_count--; in pte_list_desc_remove_entry()
978 if (head_desc->spte_count) in pte_list_desc_remove_entry()
986 if (!head_desc->more) in pte_list_desc_remove_entry()
987 rmap_head->val = 0; in pte_list_desc_remove_entry()
989 rmap_head->val = (unsigned long)head_desc->more | KVM_RMAP_MANY; in pte_list_desc_remove_entry()
999 if (KVM_BUG_ON_DATA_CORRUPTION(!rmap_head->val, kvm)) in pte_list_remove()
1002 if (!(rmap_head->val & KVM_RMAP_MANY)) { in pte_list_remove()
1003 if (KVM_BUG_ON_DATA_CORRUPTION((u64 *)rmap_head->val != spte, kvm)) in pte_list_remove()
1006 rmap_head->val = 0; in pte_list_remove()
1008 desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in pte_list_remove()
1010 for (i = 0; i < desc->spte_count; ++i) { in pte_list_remove()
1011 if (desc->sptes[i] == spte) { in pte_list_remove()
1017 desc = desc->more; in pte_list_remove()
1038 if (!rmap_head->val) in kvm_zap_all_rmap_sptes()
1041 if (!(rmap_head->val & KVM_RMAP_MANY)) { in kvm_zap_all_rmap_sptes()
1042 mmu_spte_clear_track_bits(kvm, (u64 *)rmap_head->val); in kvm_zap_all_rmap_sptes()
1046 desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in kvm_zap_all_rmap_sptes()
1049 for (i = 0; i < desc->spte_count; i++) in kvm_zap_all_rmap_sptes()
1050 mmu_spte_clear_track_bits(kvm, desc->sptes[i]); in kvm_zap_all_rmap_sptes()
1051 next = desc->more; in kvm_zap_all_rmap_sptes()
1056 rmap_head->val = 0; in kvm_zap_all_rmap_sptes()
1064 if (!rmap_head->val) in pte_list_count()
1066 else if (!(rmap_head->val & KVM_RMAP_MANY)) in pte_list_count()
1069 desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in pte_list_count()
1070 return desc->tail_count + desc->spte_count; in pte_list_count()
1078 idx = gfn_to_index(gfn, slot->base_gfn, level); in gfn_to_rmap()
1079 return &slot->arch.rmap[level - PG_LEVEL_4K][idx]; in gfn_to_rmap()
1096 * information in sp->role. in rmap_remove()
1098 slots = kvm_memslots_for_spte_role(kvm, sp->role); in rmap_remove()
1101 rmap_head = gfn_to_rmap(gfn, sp->role.level, slot); in rmap_remove()
1128 if (!rmap_head->val) in rmap_get_first()
1131 if (!(rmap_head->val & KVM_RMAP_MANY)) { in rmap_get_first()
1132 iter->desc = NULL; in rmap_get_first()
1133 sptep = (u64 *)rmap_head->val; in rmap_get_first()
1137 iter->desc = (struct pte_list_desc *)(rmap_head->val & ~KVM_RMAP_MANY); in rmap_get_first()
1138 iter->pos = 0; in rmap_get_first()
1139 sptep = iter->desc->sptes[iter->pos]; in rmap_get_first()
1154 if (iter->desc) { in rmap_get_next()
1155 if (iter->pos < PTE_LIST_EXT - 1) { in rmap_get_next()
1156 ++iter->pos; in rmap_get_next()
1157 sptep = iter->desc->sptes[iter->pos]; in rmap_get_next()
1162 iter->desc = iter->desc->more; in rmap_get_next()
1164 if (iter->desc) { in rmap_get_next()
1165 iter->pos = 0; in rmap_get_next()
1166 /* desc->sptes[0] cannot be NULL */ in rmap_get_next()
1167 sptep = iter->desc->sptes[iter->pos]; in rmap_get_next()
1195 WARN_ON_ONCE(sp->role.level == PG_LEVEL_4K); in drop_large_spte()
1204 * Write-protect on the specified @sptep, @pt_protect indicates whether
1205 * spte write-protection is caused by protecting shadow page table.
1209 * - for dirty logging, the spte can be set to writable at anytime if
1211 * - for spte protection, the spte can be writable only after unsync-ing
1265 * - D bit on ad-enabled SPTEs, and
1266 * - W bit on ad-disabled SPTEs.
1293 slot->base_gfn + gfn_offset, mask, true); in kvm_mmu_write_protect_pt_masked()
1299 rmap_head = gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask), in kvm_mmu_write_protect_pt_masked()
1304 mask &= mask - 1; in kvm_mmu_write_protect_pt_masked()
1316 slot->base_gfn + gfn_offset, mask, false); in kvm_mmu_clear_dirty_pt_masked()
1322 rmap_head = gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask), in kvm_mmu_clear_dirty_pt_masked()
1327 mask &= mask - 1; in kvm_mmu_clear_dirty_pt_masked()
1336 * If the slot was assumed to be "initially all dirty", write-protect in kvm_arch_mmu_enable_log_dirty_pt_masked()
1347 gfn_t start = slot->base_gfn + gfn_offset + __ffs(mask); in kvm_arch_mmu_enable_log_dirty_pt_masked()
1348 gfn_t end = slot->base_gfn + gfn_offset + __fls(mask); in kvm_arch_mmu_enable_log_dirty_pt_masked()
1364 * mask. If PML is enabled and the GFN doesn't need to be write- in kvm_arch_mmu_enable_log_dirty_pt_masked()
1410 return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn, PG_LEVEL_4K); in kvm_vcpu_write_protect_gfn()
1439 iterator->level = level; in rmap_walk_init_level()
1440 iterator->gfn = iterator->start_gfn; in rmap_walk_init_level()
1441 iterator->rmap = gfn_to_rmap(iterator->gfn, level, iterator->slot); in rmap_walk_init_level()
1442 iterator->end_rmap = gfn_to_rmap(iterator->end_gfn, level, iterator->slot); in rmap_walk_init_level()
1450 iterator->slot = slot; in slot_rmap_walk_init()
1451 iterator->start_level = start_level; in slot_rmap_walk_init()
1452 iterator->end_level = end_level; in slot_rmap_walk_init()
1453 iterator->start_gfn = start_gfn; in slot_rmap_walk_init()
1454 iterator->end_gfn = end_gfn; in slot_rmap_walk_init()
1456 rmap_walk_init_level(iterator, iterator->start_level); in slot_rmap_walk_init()
1461 return !!iterator->rmap; in slot_rmap_walk_okay()
1466 while (++iterator->rmap <= iterator->end_rmap) { in slot_rmap_walk_next()
1467 iterator->gfn += KVM_PAGES_PER_HPAGE(iterator->level); in slot_rmap_walk_next()
1469 if (iterator->rmap->val) in slot_rmap_walk_next()
1473 if (++iterator->level > iterator->end_level) { in slot_rmap_walk_next()
1474 iterator->rmap = NULL; in slot_rmap_walk_next()
1478 rmap_walk_init_level(iterator, iterator->level); in slot_rmap_walk_next()
1503 lockdep_assert_held_write(&kvm->mmu_lock); in __walk_slot_rmaps()
1513 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) { in __walk_slot_rmaps()
1516 iterator.gfn - start_gfn + 1); in __walk_slot_rmaps()
1519 cond_resched_rwlock_write(&kvm->mmu_lock); in __walk_slot_rmaps()
1533 slot->base_gfn, slot->base_gfn + slot->npages - 1, in walk_slot_rmaps()
1552 start, end - 1, can_yield, true, flush); in __kvm_rmap_zap_gfn_range()
1567 lockdep_assert_once(kvm->mmu_invalidate_in_progress || in kvm_unmap_gfn_range()
1568 lockdep_is_held(&kvm->slots_lock)); in kvm_unmap_gfn_range()
1571 flush = __kvm_rmap_zap_gfn_range(kvm, range->slot, in kvm_unmap_gfn_range()
1572 range->start, range->end, in kvm_unmap_gfn_range()
1573 range->may_block, flush); in kvm_unmap_gfn_range()
1579 range->slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT) in kvm_unmap_gfn_range()
1598 kvm_update_page_stats(kvm, sp->role.level, 1); in __rmap_add()
1600 rmap_head = gfn_to_rmap(gfn, sp->role.level, slot); in __rmap_add()
1603 if (rmap_count > kvm->stat.max_mmu_rmap_size) in __rmap_add()
1604 kvm->stat.max_mmu_rmap_size = rmap_count; in __rmap_add()
1607 kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level); in __rmap_add()
1614 struct kvm_mmu_memory_cache *cache = &vcpu->arch.mmu_pte_list_desc_cache; in rmap_add()
1616 __rmap_add(vcpu->kvm, cache, slot, spte, gfn, access); in rmap_add()
1627 for_each_slot_rmap_range(range->slot, PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL, in kvm_rmap_age_gfn_range()
1628 range->start, range->end - 1, &iterator) { in kvm_rmap_age_gfn_range()
1639 clear_bit((ffs(shadow_accessed_mask) - 1), in kvm_rmap_age_gfn_range()
1691 if (KVM_MMU_WARN_ON(is_shadow_present_pte(sp->spt[i]))) in kvm_mmu_check_sptes_at_free()
1692 pr_err_ratelimited("SPTE %llx (@ %p) for gfn %llx shadow-present at free", in kvm_mmu_check_sptes_at_free()
1693 sp->spt[i], &sp->spt[i], in kvm_mmu_check_sptes_at_free()
1701 * kvm->arch.n_used_mmu_pages values. We need a global,
1707 kvm->arch.n_used_mmu_pages += nr; in kvm_mod_used_mmu_pages()
1714 kvm_account_pgtable_pages((void *)sp->spt, +1); in kvm_account_mmu_page()
1719 kvm_mod_used_mmu_pages(kvm, -1); in kvm_unaccount_mmu_page()
1720 kvm_account_pgtable_pages((void *)sp->spt, -1); in kvm_unaccount_mmu_page()
1727 hlist_del(&sp->hash_link); in kvm_mmu_free_shadow_page()
1728 list_del(&sp->link); in kvm_mmu_free_shadow_page()
1729 free_page((unsigned long)sp->spt); in kvm_mmu_free_shadow_page()
1730 free_page((unsigned long)sp->shadowed_translation); in kvm_mmu_free_shadow_page()
1745 pte_list_add(cache, parent_pte, &sp->parent_ptes); in mmu_page_add_parent_pte()
1751 pte_list_remove(kvm, parent_pte, &sp->parent_ptes); in mmu_page_remove_parent_pte()
1767 for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) { in kvm_mmu_mark_parents_unsync()
1777 if (__test_and_set_bit(spte_index(spte), sp->unsync_child_bitmap)) in mark_unsync()
1779 if (sp->unsync_children++) in mark_unsync()
1799 if (sp->unsync) in mmu_pages_add()
1800 for (i=0; i < pvec->nr; i++) in mmu_pages_add()
1801 if (pvec->page[i].sp == sp) in mmu_pages_add()
1804 pvec->page[pvec->nr].sp = sp; in mmu_pages_add()
1805 pvec->page[pvec->nr].idx = idx; in mmu_pages_add()
1806 pvec->nr++; in mmu_pages_add()
1807 return (pvec->nr == KVM_PAGE_ARRAY_NR); in mmu_pages_add()
1812 --sp->unsync_children; in clear_unsync_child_bit()
1813 WARN_ON_ONCE((int)sp->unsync_children < 0); in clear_unsync_child_bit()
1814 __clear_bit(idx, sp->unsync_child_bitmap); in clear_unsync_child_bit()
1822 for_each_set_bit(i, sp->unsync_child_bitmap, 512) { in __mmu_unsync_walk()
1824 u64 ent = sp->spt[i]; in __mmu_unsync_walk()
1833 if (child->unsync_children) { in __mmu_unsync_walk()
1835 return -ENOSPC; in __mmu_unsync_walk()
1845 } else if (child->unsync) { in __mmu_unsync_walk()
1848 return -ENOSPC; in __mmu_unsync_walk()
1856 #define INVALID_INDEX (-1)
1861 pvec->nr = 0; in mmu_unsync_walk()
1862 if (!sp->unsync_children) in mmu_unsync_walk()
1871 WARN_ON_ONCE(!sp->unsync); in kvm_unlink_unsync_page()
1873 sp->unsync = 0; in kvm_unlink_unsync_page()
1874 --kvm->stat.mmu_unsync; in kvm_unlink_unsync_page()
1884 if (sp->role.direct) in sp_has_gptes()
1887 if (sp->role.passthrough) in sp_has_gptes()
1900 &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \
1901 if ((_sp)->gfn != (_gfn) || !sp_has_gptes(_sp)) {} else
1905 union kvm_mmu_page_role root_role = vcpu->arch.mmu->root_role; in kvm_sync_page_check()
1911 * - level: not part of the overall MMU role and will never match as the MMU's in kvm_sync_page_check()
1913 * - access: updated based on the new guest PTE in kvm_sync_page_check()
1914 * - quadrant: not part of the overall MMU role (similar to level) in kvm_sync_page_check()
1926 * differs then the memslot lookup (SMM vs. non-SMM) will be bogus, the in kvm_sync_page_check()
1929 if (WARN_ON_ONCE(sp->role.direct || !vcpu->arch.mmu->sync_spte || in kvm_sync_page_check()
1930 (sp->role.word ^ root_role.word) & ~sync_role_ign.word)) in kvm_sync_page_check()
1938 /* sp->spt[i] has initial value of shadow page table allocation */ in kvm_sync_spte()
1939 if (sp->spt[i] == SHADOW_NONPRESENT_VALUE) in kvm_sync_spte()
1942 return vcpu->arch.mmu->sync_spte(vcpu, sp, i); in kvm_sync_spte()
1951 return -1; in __kvm_sync_page()
1956 if (ret < -1) in __kvm_sync_page()
1957 return -1; in __kvm_sync_page()
1979 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); in kvm_sync_page()
1999 if (sp->role.invalid) in is_obsolete_sp()
2004 unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen); in is_obsolete_sp()
2023 for (n = i+1; n < pvec->nr; n++) { in mmu_pages_next()
2024 struct kvm_mmu_page *sp = pvec->page[n].sp; in mmu_pages_next()
2025 unsigned idx = pvec->page[n].idx; in mmu_pages_next()
2026 int level = sp->role.level; in mmu_pages_next()
2028 parents->idx[level-1] = idx; in mmu_pages_next()
2032 parents->parent[level-2] = sp; in mmu_pages_next()
2044 if (pvec->nr == 0) in mmu_pages_first()
2047 WARN_ON_ONCE(pvec->page[0].idx != INVALID_INDEX); in mmu_pages_first()
2049 sp = pvec->page[0].sp; in mmu_pages_first()
2050 level = sp->role.level; in mmu_pages_first()
2053 parents->parent[level-2] = sp; in mmu_pages_first()
2058 parents->parent[level-1] = NULL; in mmu_pages_first()
2068 unsigned int idx = parents->idx[level]; in mmu_pages_clear_parents()
2069 sp = parents->parent[level]; in mmu_pages_clear_parents()
2076 } while (!sp->unsync_children); in mmu_pages_clear_parents()
2093 protected |= kvm_vcpu_write_protect_gfn(vcpu, sp->gfn); in mmu_sync_children()
2096 kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, true); in mmu_sync_children()
2101 kvm_unlink_unsync_page(vcpu->kvm, sp); in mmu_sync_children()
2105 if (need_resched() || rwlock_needbreak(&vcpu->kvm->mmu_lock)) { in mmu_sync_children()
2106 kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, flush); in mmu_sync_children()
2109 return -EINTR; in mmu_sync_children()
2112 cond_resched_rwlock_write(&vcpu->kvm->mmu_lock); in mmu_sync_children()
2117 kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, flush); in mmu_sync_children()
2123 atomic_set(&sp->write_flooding_count, 0); in __clear_sp_write_flooding_count()
2149 if (sp->gfn != gfn) { in kvm_mmu_find_shadow_page()
2154 if (sp->role.word != role.word) { in kvm_mmu_find_shadow_page()
2156 * If the guest is creating an upper-level page, zap in kvm_mmu_find_shadow_page()
2162 * upper-level page will be write-protected. in kvm_mmu_find_shadow_page()
2164 if (role.level > PG_LEVEL_4K && sp->unsync) in kvm_mmu_find_shadow_page()
2170 /* unsync and write-flooding only apply to indirect SPs. */ in kvm_mmu_find_shadow_page()
2171 if (sp->role.direct) in kvm_mmu_find_shadow_page()
2174 if (sp->unsync) { in kvm_mmu_find_shadow_page()
2181 * it doesn't write-protect the page or mark it synchronized! in kvm_mmu_find_shadow_page()
2205 ++kvm->stat.mmu_cache_miss; in kvm_mmu_find_shadow_page()
2210 if (collisions > kvm->stat.max_mmu_page_hash_collisions) in kvm_mmu_find_shadow_page()
2211 kvm->stat.max_mmu_page_hash_collisions = collisions; in kvm_mmu_find_shadow_page()
2230 sp = kvm_mmu_memory_cache_alloc(caches->page_header_cache); in kvm_mmu_alloc_shadow_page()
2231 sp->spt = kvm_mmu_memory_cache_alloc(caches->shadow_page_cache); in kvm_mmu_alloc_shadow_page()
2233 sp->shadowed_translation = kvm_mmu_memory_cache_alloc(caches->shadowed_info_cache); in kvm_mmu_alloc_shadow_page()
2235 set_page_private(virt_to_page(sp->spt), (unsigned long)sp); in kvm_mmu_alloc_shadow_page()
2237 INIT_LIST_HEAD(&sp->possible_nx_huge_page_link); in kvm_mmu_alloc_shadow_page()
2244 sp->mmu_valid_gen = kvm->arch.mmu_valid_gen; in kvm_mmu_alloc_shadow_page()
2245 list_add(&sp->link, &kvm->arch.active_mmu_pages); in kvm_mmu_alloc_shadow_page()
2248 sp->gfn = gfn; in kvm_mmu_alloc_shadow_page()
2249 sp->role = role; in kvm_mmu_alloc_shadow_page()
2250 hlist_add_head(&sp->hash_link, sp_list); in kvm_mmu_alloc_shadow_page()
2268 sp_list = &kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]; in __kvm_mmu_get_shadow_page()
2285 .page_header_cache = &vcpu->arch.mmu_page_header_cache, in kvm_mmu_get_shadow_page()
2286 .shadow_page_cache = &vcpu->arch.mmu_shadow_page_cache, in kvm_mmu_get_shadow_page()
2287 .shadowed_info_cache = &vcpu->arch.mmu_shadowed_info_cache, in kvm_mmu_get_shadow_page()
2290 return __kvm_mmu_get_shadow_page(vcpu->kvm, vcpu, &caches, gfn, role); in kvm_mmu_get_shadow_page()
2299 role = parent_sp->role; in kvm_mmu_child_role()
2300 role.level--; in kvm_mmu_child_role()
2306 * If the guest has 4-byte PTEs then that means it's using 32-bit, in kvm_mmu_child_role()
2307 * 2-level, non-PAE paging. KVM shadows such guests with PAE paging in kvm_mmu_child_role()
2308 * (i.e. 8-byte PTEs). The difference in PTE size means that KVM must in kvm_mmu_child_role()
2321 * Concretely, a 4-byte PDE consumes bits 31:22, while an 8-byte PDE in kvm_mmu_child_role()
2323 * PDPTEs; those 4 PAE page directories are pre-allocated and their in kvm_mmu_child_role()
2324 * quadrant is assigned in mmu_alloc_root(). A 4-byte PTE consumes in kvm_mmu_child_role()
2325 * bits 21:12, while an 8-byte PTE consumes bits 20:12. To consume in kvm_mmu_child_role()
2327 * quadrant, i.e. sets quadrant to '0' or '1'. The parent 8-byte PDE in kvm_mmu_child_role()
2346 return ERR_PTR(-EEXIST); in kvm_mmu_get_child_sp()
2356 iterator->addr = addr; in shadow_walk_init_using_root()
2357 iterator->shadow_addr = root; in shadow_walk_init_using_root()
2358 iterator->level = vcpu->arch.mmu->root_role.level; in shadow_walk_init_using_root()
2360 if (iterator->level >= PT64_ROOT_4LEVEL && in shadow_walk_init_using_root()
2361 vcpu->arch.mmu->cpu_role.base.level < PT64_ROOT_4LEVEL && in shadow_walk_init_using_root()
2362 !vcpu->arch.mmu->root_role.direct) in shadow_walk_init_using_root()
2363 iterator->level = PT32E_ROOT_LEVEL; in shadow_walk_init_using_root()
2365 if (iterator->level == PT32E_ROOT_LEVEL) { in shadow_walk_init_using_root()
2367 * prev_root is currently only used for 64-bit hosts. So only in shadow_walk_init_using_root()
2370 BUG_ON(root != vcpu->arch.mmu->root.hpa); in shadow_walk_init_using_root()
2372 iterator->shadow_addr in shadow_walk_init_using_root()
2373 = vcpu->arch.mmu->pae_root[(addr >> 30) & 3]; in shadow_walk_init_using_root()
2374 iterator->shadow_addr &= SPTE_BASE_ADDR_MASK; in shadow_walk_init_using_root()
2375 --iterator->level; in shadow_walk_init_using_root()
2376 if (!iterator->shadow_addr) in shadow_walk_init_using_root()
2377 iterator->level = 0; in shadow_walk_init_using_root()
2384 shadow_walk_init_using_root(iterator, vcpu, vcpu->arch.mmu->root.hpa, in shadow_walk_init()
2390 if (iterator->level < PG_LEVEL_4K) in shadow_walk_okay()
2393 iterator->index = SPTE_INDEX(iterator->addr, iterator->level); in shadow_walk_okay()
2394 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index; in shadow_walk_okay()
2401 if (!is_shadow_present_pte(spte) || is_last_spte(spte, iterator->level)) { in __shadow_walk_next()
2402 iterator->level = 0; in __shadow_walk_next()
2406 iterator->shadow_addr = spte & SPTE_BASE_ADDR_MASK; in __shadow_walk_next()
2407 --iterator->level; in __shadow_walk_next()
2412 __shadow_walk_next(iterator, *iterator->sptep); in shadow_walk_next()
2431 spte = make_nonleaf_spte(sp->spt, sp_ad_disabled(sp)); in __link_shadow_page()
2438 * The non-direct sub-pagetable must be updated before linking. For in __link_shadow_page()
2440 * kvm_mmu_find_shadow_page() without write-protecting the gfn, in __link_shadow_page()
2441 * so sp->unsync can be true or false. For higher level non-direct in __link_shadow_page()
2443 * FNAME(fetch)(), so sp->unsync_children can only be false. in __link_shadow_page()
2446 if (WARN_ON_ONCE(sp->unsync_children) || sp->unsync) in __link_shadow_page()
2453 __link_shadow_page(vcpu->kvm, &vcpu->arch.mmu_pte_list_desc_cache, sptep, sp, true); in link_shadow_page()
2465 * sp's access: allow writable in the read-only sp, in validate_direct_spte()
2470 if (child->role.access == direct_access) in validate_direct_spte()
2473 drop_parent_pte(vcpu->kvm, child, sptep); in validate_direct_spte()
2474 kvm_flush_remote_tlbs_sptep(vcpu->kvm, sptep); in validate_direct_spte()
2478 /* Returns the number of zapped non-leaf child shadow pages. */
2487 if (is_last_spte(pte, sp->role.level)) { in mmu_page_zap_pte()
2499 child->role.guest_mode && !child->parent_ptes.val) in mmu_page_zap_pte()
2517 zapped += mmu_page_zap_pte(kvm, sp, sp->spt + i, invalid_list); in kvm_mmu_page_unlink_children()
2527 while ((sptep = rmap_get_first(&sp->parent_ptes, &iter))) in kvm_mmu_unlink_parents()
2539 if (parent->role.level == PG_LEVEL_4K) in mmu_zap_unsync_children()
2562 lockdep_assert_held_write(&kvm->mmu_lock); in __kvm_mmu_prepare_zap_page()
2564 ++kvm->stat.mmu_shadow_zapped; in __kvm_mmu_prepare_zap_page()
2572 if (!sp->role.invalid && sp_has_gptes(sp)) in __kvm_mmu_prepare_zap_page()
2575 if (sp->unsync) in __kvm_mmu_prepare_zap_page()
2577 if (!sp->root_count) { in __kvm_mmu_prepare_zap_page()
2584 * !sp->root_count. in __kvm_mmu_prepare_zap_page()
2586 if (sp->role.invalid) in __kvm_mmu_prepare_zap_page()
2587 list_add(&sp->link, invalid_list); in __kvm_mmu_prepare_zap_page()
2589 list_move(&sp->link, invalid_list); in __kvm_mmu_prepare_zap_page()
2596 list_del(&sp->link); in __kvm_mmu_prepare_zap_page()
2606 if (sp->nx_huge_page_disallowed) in __kvm_mmu_prepare_zap_page()
2609 sp->role.invalid = 1; in __kvm_mmu_prepare_zap_page()
2639 * the page tables and see changes to vcpu->mode here. The barrier in kvm_mmu_commit_zap_page()
2649 WARN_ON_ONCE(!sp->role.invalid || sp->root_count); in kvm_mmu_commit_zap_page()
2663 if (list_empty(&kvm->arch.active_mmu_pages)) in kvm_mmu_zap_oldest_mmu_pages()
2667 list_for_each_entry_safe_reverse(sp, tmp, &kvm->arch.active_mmu_pages, link) { in kvm_mmu_zap_oldest_mmu_pages()
2672 if (sp->root_count) in kvm_mmu_zap_oldest_mmu_pages()
2687 kvm->stat.mmu_recycled += total_zapped; in kvm_mmu_zap_oldest_mmu_pages()
2693 if (kvm->arch.n_max_mmu_pages > kvm->arch.n_used_mmu_pages) in kvm_mmu_available_pages()
2694 return kvm->arch.n_max_mmu_pages - in kvm_mmu_available_pages()
2695 kvm->arch.n_used_mmu_pages; in kvm_mmu_available_pages()
2702 unsigned long avail = kvm_mmu_available_pages(vcpu->kvm); in make_mmu_pages_available()
2707 kvm_mmu_zap_oldest_mmu_pages(vcpu->kvm, KVM_REFILL_PAGES - avail); in make_mmu_pages_available()
2712 * four pages, e.g. for PAE roots or for 5-level paging. Temporarily in make_mmu_pages_available()
2718 if (!kvm_mmu_available_pages(vcpu->kvm)) in make_mmu_pages_available()
2719 return -ENOSPC; in make_mmu_pages_available()
2729 write_lock(&kvm->mmu_lock); in kvm_mmu_change_mmu_pages()
2731 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { in kvm_mmu_change_mmu_pages()
2732 kvm_mmu_zap_oldest_mmu_pages(kvm, kvm->arch.n_used_mmu_pages - in kvm_mmu_change_mmu_pages()
2735 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; in kvm_mmu_change_mmu_pages()
2738 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages; in kvm_mmu_change_mmu_pages()
2740 write_unlock(&kvm->mmu_lock); in kvm_mmu_change_mmu_pages()
2746 struct kvm *kvm = vcpu->kvm; in __kvm_mmu_unprotect_gfn_and_retry()
2753 * Bail early if there aren't any write-protected shadow pages to avoid in __kvm_mmu_unprotect_gfn_and_retry()
2754 * unnecessarily taking mmu_lock lock, e.g. if the gfn is write-tracked in __kvm_mmu_unprotect_gfn_and_retry()
2760 if (!READ_ONCE(kvm->arch.indirect_shadow_pages)) in __kvm_mmu_unprotect_gfn_and_retry()
2763 if (!vcpu->arch.mmu->root_role.direct) { in __kvm_mmu_unprotect_gfn_and_retry()
2769 write_lock(&kvm->mmu_lock); in __kvm_mmu_unprotect_gfn_and_retry()
2779 write_unlock(&kvm->mmu_lock); in __kvm_mmu_unprotect_gfn_and_retry()
2783 vcpu->arch.last_retry_eip = kvm_rip_read(vcpu); in __kvm_mmu_unprotect_gfn_and_retry()
2784 vcpu->arch.last_retry_addr = cr2_or_gpa; in __kvm_mmu_unprotect_gfn_and_retry()
2792 ++kvm->stat.mmu_unsync; in kvm_unsync_page()
2793 sp->unsync = 1; in kvm_unsync_page()
2801 * were marked unsync (or if there is no shadow page), -EPERM if the SPTE must
2802 * be write-protected.
2811 * Force write-protection if the page is being tracked. Note, the page in mmu_try_to_unsync_pages()
2812 * track machinery is used to write-protect upper-level shadow pages, in mmu_try_to_unsync_pages()
2816 return -EPERM; in mmu_try_to_unsync_pages()
2819 * The page is not write-tracked, mark existing shadow pages unsync in mmu_try_to_unsync_pages()
2826 return -EPERM; in mmu_try_to_unsync_pages()
2828 if (sp->unsync) in mmu_try_to_unsync_pages()
2832 return -EEXIST; in mmu_try_to_unsync_pages()
2843 spin_lock(&kvm->arch.mmu_unsync_pages_lock); in mmu_try_to_unsync_pages()
2849 * possible as clearing sp->unsync _must_ hold mmu_lock in mmu_try_to_unsync_pages()
2850 * for write, i.e. unsync cannot transition from 1->0 in mmu_try_to_unsync_pages()
2853 if (READ_ONCE(sp->unsync)) in mmu_try_to_unsync_pages()
2857 WARN_ON_ONCE(sp->role.level != PG_LEVEL_4K); in mmu_try_to_unsync_pages()
2861 spin_unlock(&kvm->arch.mmu_unsync_pages_lock); in mmu_try_to_unsync_pages()
2868 * before the page had been marked as unsync-ed, something like the in mmu_try_to_unsync_pages()
2872 * --------------------------------------------------------------------- in mmu_try_to_unsync_pages()
2885 * 2.3 Walking of unsync pages sees sp->unsync is in mmu_try_to_unsync_pages()
2894 * (sp->unsync = true) in mmu_try_to_unsync_pages()
2910 int level = sp->role.level; in mmu_set_spte()
2918 bool host_writable = !fault || fault->map_writable; in mmu_set_spte()
2919 bool prefetch = !fault || fault->prefetch; in mmu_set_spte()
2920 bool write_fault = fault && fault->write; in mmu_set_spte()
2923 vcpu->stat.pf_mmio_spte_created++; in mmu_set_spte()
2938 drop_parent_pte(vcpu->kvm, child, sptep); in mmu_set_spte()
2941 drop_spte(vcpu->kvm, sptep); in mmu_set_spte()
2961 kvm_flush_remote_tlbs_gfn(vcpu->kvm, gfn, level); in mmu_set_spte()
2980 unsigned int access = sp->role.access; in direct_pte_prefetch_many()
2987 return -1; in direct_pte_prefetch_many()
2989 ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start); in direct_pte_prefetch_many()
2991 return -1; in direct_pte_prefetch_many()
3008 WARN_ON_ONCE(!sp->role.direct); in __direct_pte_prefetch()
3010 i = spte_index(sptep) & ~(PTE_PREFETCH_NUM - 1); in __direct_pte_prefetch()
3011 spte = sp->spt + i; in __direct_pte_prefetch()
3041 if (sp->role.level > PG_LEVEL_4K) in direct_pte_prefetch()
3048 if (unlikely(vcpu->kvm->mmu_invalidate_in_progress)) in direct_pte_prefetch()
3062 * - Check mmu_invalidate_retry_gfn() after grabbing the mapping level, before
3066 * - Hold mmu_lock AND ensure there is no in-progress MMU notifier invalidation
3070 * - Do not use the result to install new mappings, e.g. use the host mapping
3091 * Note, using the already-retrieved memslot and __gfn_to_hva_memslot() in host_pfn_mapping_level()
3094 * read-only memslots due to gfn_to_hva() assuming writes. Earlier in host_pfn_mapping_level()
3096 * read-only memslot. in host_pfn_mapping_level()
3108 * Read each entry once. As above, a non-leaf entry can be promoted to in host_pfn_mapping_level()
3109 * a huge page _during_ this walk. Re-reading the entry could send the in host_pfn_mapping_level()
3114 pgd = READ_ONCE(*pgd_offset(kvm->mm, hva)); in host_pfn_mapping_level()
3151 for ( ; max_level > PG_LEVEL_4K; max_level--) { in __kvm_mmu_max_mapping_level()
3153 if (!linfo->disallow_lpage) in __kvm_mmu_max_mapping_level()
3179 struct kvm_memory_slot *slot = fault->slot; in kvm_mmu_hugepage_adjust()
3182 fault->huge_page_disallowed = fault->exec && fault->nx_huge_page_workaround_enabled; in kvm_mmu_hugepage_adjust()
3184 if (unlikely(fault->max_level == PG_LEVEL_4K)) in kvm_mmu_hugepage_adjust()
3187 if (is_error_noslot_pfn(fault->pfn)) in kvm_mmu_hugepage_adjust()
3197 fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot, in kvm_mmu_hugepage_adjust()
3198 fault->gfn, fault->max_level, in kvm_mmu_hugepage_adjust()
3199 fault->is_private); in kvm_mmu_hugepage_adjust()
3200 if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed) in kvm_mmu_hugepage_adjust()
3207 fault->goal_level = fault->req_level; in kvm_mmu_hugepage_adjust()
3208 mask = KVM_PAGES_PER_HPAGE(fault->goal_level) - 1; in kvm_mmu_hugepage_adjust()
3209 VM_BUG_ON((fault->gfn & mask) != (fault->pfn & mask)); in kvm_mmu_hugepage_adjust()
3210 fault->pfn &= ~mask; in kvm_mmu_hugepage_adjust()
3216 cur_level == fault->goal_level && in disallowed_hugepage_adjust()
3219 spte_to_child_sp(spte)->nx_huge_page_disallowed) { in disallowed_hugepage_adjust()
3227 u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) - in disallowed_hugepage_adjust()
3228 KVM_PAGES_PER_HPAGE(cur_level - 1); in disallowed_hugepage_adjust()
3229 fault->pfn |= fault->gfn & page_mask; in disallowed_hugepage_adjust()
3230 fault->goal_level--; in disallowed_hugepage_adjust()
3239 gfn_t base_gfn = fault->gfn; in direct_map()
3244 for_each_shadow_entry(vcpu, fault->addr, it) { in direct_map()
3249 if (fault->nx_huge_page_workaround_enabled) in direct_map()
3252 base_gfn = gfn_round_for_level(fault->gfn, it.level); in direct_map()
3253 if (it.level == fault->goal_level) in direct_map()
3257 if (sp == ERR_PTR(-EEXIST)) in direct_map()
3261 if (fault->huge_page_disallowed) in direct_map()
3262 account_nx_huge_page(vcpu->kvm, sp, in direct_map()
3263 fault->req_level >= it.level); in direct_map()
3266 if (WARN_ON_ONCE(it.level != fault->goal_level)) in direct_map()
3267 return -EFAULT; in direct_map()
3269 ret = mmu_set_spte(vcpu, fault->slot, it.sptep, ACC_ALL, in direct_map()
3270 base_gfn, fault->pfn, fault); in direct_map()
3287 if (is_sigpending_pfn(fault->pfn)) { in kvm_handle_error_pfn()
3289 return -EINTR; in kvm_handle_error_pfn()
3297 if (fault->pfn == KVM_PFN_ERR_RO_FAULT) in kvm_handle_error_pfn()
3300 if (fault->pfn == KVM_PFN_ERR_HWPOISON) { in kvm_handle_error_pfn()
3301 kvm_send_hwpoison_signal(fault->slot, fault->gfn); in kvm_handle_error_pfn()
3305 return -EFAULT; in kvm_handle_error_pfn()
3312 gva_t gva = fault->is_tdp ? 0 : fault->addr; in kvm_handle_noslot_fault()
3314 if (fault->is_private) { in kvm_handle_noslot_fault()
3316 return -EFAULT; in kvm_handle_noslot_fault()
3319 vcpu_cache_mmio_info(vcpu, gva, fault->gfn, in kvm_handle_noslot_fault()
3322 fault->slot = NULL; in kvm_handle_noslot_fault()
3323 fault->pfn = KVM_PFN_NOSLOT; in kvm_handle_noslot_fault()
3324 fault->map_writable = false; in kvm_handle_noslot_fault()
3325 fault->hva = KVM_HVA_ERR_BAD; in kvm_handle_noslot_fault()
3342 if (unlikely(fault->gfn > kvm_mmu_max_gfn())) in kvm_handle_noslot_fault()
3356 if (fault->rsvd) in page_fault_can_be_fast()
3360 * For hardware-protected VMs, certain conditions like attempting to in page_fault_can_be_fast()
3364 * result of a write-protected access, and treat it as a spurious case in page_fault_can_be_fast()
3375 if (kvm->arch.has_private_mem && in page_fault_can_be_fast()
3376 fault->is_private != kvm_mem_is_private(kvm, fault->gfn)) in page_fault_can_be_fast()
3390 * the fault was caused by a write-protection violation. If the in page_fault_can_be_fast()
3391 * SPTE is MMU-writable (determined later), the fault can be fixed in page_fault_can_be_fast()
3394 if (!fault->present) in page_fault_can_be_fast()
3401 return fault->write; in page_fault_can_be_fast()
3420 * so non-PML cases won't be impacted. in fast_pf_fix_direct_spte()
3428 mark_page_dirty_in_slot(vcpu->kvm, fault->slot, fault->gfn); in fast_pf_fix_direct_spte()
3435 if (fault->exec) in is_access_allowed()
3438 if (fault->write) in is_access_allowed()
3447 * gpa, and sets *spte to the spte value. This spte may be non-preset. If no
3451 * - Must be called between walk_shadow_page_lockless_{begin,end}.
3452 * - The returned sptep must not be used after walk_shadow_page_lockless_end.
3479 if (!page_fault_can_be_fast(vcpu->kvm, fault)) in fast_page_fault()
3488 sptep = kvm_tdp_mmu_fast_pf_get_last_sptep(vcpu, fault->gfn, &spte); in fast_page_fault()
3490 sptep = fast_pf_get_last_sptep(vcpu, fault->addr, &spte); in fast_page_fault()
3504 if (!is_last_spte(spte, sp->role.level)) in fast_page_fault()
3527 * uses A/D bits for non-nested MMUs. Thus, if A/D bits are in fast_page_fault()
3528 * enabled, the SPTE can't be an access-tracked SPTE. in fast_page_fault()
3534 * To keep things simple, only SPTEs that are MMU-writable can in fast_page_fault()
3536 * that were write-protected for dirty-logging or access in fast_page_fault()
3541 * shadow-present, i.e. except for access tracking restoration in fast_page_fault()
3544 if (fault->write && is_mmu_writable_spte(spte)) { in fast_page_fault()
3548 * Do not fix write-permission on the large spte when in fast_page_fault()
3550 * first page into the dirty-bitmap in in fast_page_fault()
3557 if (sp->role.level > PG_LEVEL_4K && in fast_page_fault()
3558 kvm_slot_dirty_track_enabled(fault->slot)) in fast_page_fault()
3588 vcpu->stat.pf_fast++; in fast_page_fault()
3606 lockdep_assert_held_read(&kvm->mmu_lock); in mmu_free_root_page()
3609 lockdep_assert_held_write(&kvm->mmu_lock); in mmu_free_root_page()
3610 if (!--sp->root_count && sp->role.invalid) in mmu_free_root_page()
3621 bool is_tdp_mmu = tdp_mmu_enabled && mmu->root_role.direct; in kvm_mmu_free_roots()
3632 && VALID_PAGE(mmu->root.hpa); in kvm_mmu_free_roots()
3637 VALID_PAGE(mmu->prev_roots[i].hpa)) in kvm_mmu_free_roots()
3645 read_lock(&kvm->mmu_lock); in kvm_mmu_free_roots()
3647 write_lock(&kvm->mmu_lock); in kvm_mmu_free_roots()
3651 mmu_free_root_page(kvm, &mmu->prev_roots[i].hpa, in kvm_mmu_free_roots()
3655 if (kvm_mmu_is_dummy_root(mmu->root.hpa)) { in kvm_mmu_free_roots()
3657 } else if (root_to_sp(mmu->root.hpa)) { in kvm_mmu_free_roots()
3658 mmu_free_root_page(kvm, &mmu->root.hpa, &invalid_list); in kvm_mmu_free_roots()
3659 } else if (mmu->pae_root) { in kvm_mmu_free_roots()
3661 if (!IS_VALID_PAE_ROOT(mmu->pae_root[i])) in kvm_mmu_free_roots()
3664 mmu_free_root_page(kvm, &mmu->pae_root[i], in kvm_mmu_free_roots()
3666 mmu->pae_root[i] = INVALID_PAE_ROOT; in kvm_mmu_free_roots()
3669 mmu->root.hpa = INVALID_PAGE; in kvm_mmu_free_roots()
3670 mmu->root.pgd = 0; in kvm_mmu_free_roots()
3674 read_unlock(&kvm->mmu_lock); in kvm_mmu_free_roots()
3678 write_unlock(&kvm->mmu_lock); in kvm_mmu_free_roots()
3694 WARN_ON_ONCE(mmu->root_role.guest_mode); in kvm_mmu_free_guest_mode_roots()
3697 root_hpa = mmu->prev_roots[i].hpa; in kvm_mmu_free_guest_mode_roots()
3702 if (!sp || sp->role.guest_mode) in kvm_mmu_free_guest_mode_roots()
3713 union kvm_mmu_page_role role = vcpu->arch.mmu->root_role; in mmu_alloc_root()
3723 ++sp->root_count; in mmu_alloc_root()
3725 return __pa(sp->spt); in mmu_alloc_root()
3730 struct kvm_mmu *mmu = vcpu->arch.mmu; in mmu_alloc_direct_roots()
3731 u8 shadow_root_level = mmu->root_role.level; in mmu_alloc_direct_roots()
3739 write_lock(&vcpu->kvm->mmu_lock); in mmu_alloc_direct_roots()
3746 mmu->root.hpa = root; in mmu_alloc_direct_roots()
3748 if (WARN_ON_ONCE(!mmu->pae_root)) { in mmu_alloc_direct_roots()
3749 r = -EIO; in mmu_alloc_direct_roots()
3754 WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i])); in mmu_alloc_direct_roots()
3756 root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), 0, in mmu_alloc_direct_roots()
3758 mmu->pae_root[i] = root | PT_PRESENT_MASK | in mmu_alloc_direct_roots()
3761 mmu->root.hpa = __pa(mmu->pae_root); in mmu_alloc_direct_roots()
3764 r = -EIO; in mmu_alloc_direct_roots()
3769 mmu->root.pgd = 0; in mmu_alloc_direct_roots()
3771 write_unlock(&vcpu->kvm->mmu_lock); in mmu_alloc_direct_roots()
3788 mutex_lock(&kvm->slots_arch_lock); in mmu_first_shadow_root_alloc()
3806 * Both of these functions are no-ops if the target is in mmu_first_shadow_root_alloc()
3815 r = memslot_rmap_alloc(slot, slot->npages); in mmu_first_shadow_root_alloc()
3829 smp_store_release(&kvm->arch.shadow_root_allocated, true); in mmu_first_shadow_root_alloc()
3832 mutex_unlock(&kvm->slots_arch_lock); in mmu_first_shadow_root_alloc()
3838 struct kvm_mmu *mmu = vcpu->arch.mmu; in mmu_alloc_shadow_roots()
3848 mmu->root.hpa = kvm_mmu_get_dummy_root(); in mmu_alloc_shadow_roots()
3856 if (mmu->cpu_role.base.level == PT32E_ROOT_LEVEL) { in mmu_alloc_shadow_roots()
3858 pdptrs[i] = mmu->get_pdptr(vcpu, i); in mmu_alloc_shadow_roots()
3867 r = mmu_first_shadow_root_alloc(vcpu->kvm); in mmu_alloc_shadow_roots()
3871 write_lock(&vcpu->kvm->mmu_lock); in mmu_alloc_shadow_roots()
3878 * write-protect the guests page table root. in mmu_alloc_shadow_roots()
3880 if (mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL) { in mmu_alloc_shadow_roots()
3882 mmu->root_role.level); in mmu_alloc_shadow_roots()
3883 mmu->root.hpa = root; in mmu_alloc_shadow_roots()
3887 if (WARN_ON_ONCE(!mmu->pae_root)) { in mmu_alloc_shadow_roots()
3888 r = -EIO; in mmu_alloc_shadow_roots()
3893 * We shadow a 32 bit page table. This may be a legacy 2-level in mmu_alloc_shadow_roots()
3894 * or a PAE 3-level page table. In either case we need to be aware that in mmu_alloc_shadow_roots()
3898 if (mmu->root_role.level >= PT64_ROOT_4LEVEL) { in mmu_alloc_shadow_roots()
3901 if (WARN_ON_ONCE(!mmu->pml4_root)) { in mmu_alloc_shadow_roots()
3902 r = -EIO; in mmu_alloc_shadow_roots()
3905 mmu->pml4_root[0] = __pa(mmu->pae_root) | pm_mask; in mmu_alloc_shadow_roots()
3907 if (mmu->root_role.level == PT64_ROOT_5LEVEL) { in mmu_alloc_shadow_roots()
3908 if (WARN_ON_ONCE(!mmu->pml5_root)) { in mmu_alloc_shadow_roots()
3909 r = -EIO; in mmu_alloc_shadow_roots()
3912 mmu->pml5_root[0] = __pa(mmu->pml4_root) | pm_mask; in mmu_alloc_shadow_roots()
3917 WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i])); in mmu_alloc_shadow_roots()
3919 if (mmu->cpu_role.base.level == PT32E_ROOT_LEVEL) { in mmu_alloc_shadow_roots()
3921 mmu->pae_root[i] = INVALID_PAE_ROOT; in mmu_alloc_shadow_roots()
3928 * If shadowing 32-bit non-PAE page tables, each PAE page in mmu_alloc_shadow_roots()
3929 * directory maps one quarter of the guest's non-PAE page in mmu_alloc_shadow_roots()
3933 quadrant = (mmu->cpu_role.base.level == PT32_ROOT_LEVEL) ? i : 0; in mmu_alloc_shadow_roots()
3936 mmu->pae_root[i] = root | pm_mask; in mmu_alloc_shadow_roots()
3939 if (mmu->root_role.level == PT64_ROOT_5LEVEL) in mmu_alloc_shadow_roots()
3940 mmu->root.hpa = __pa(mmu->pml5_root); in mmu_alloc_shadow_roots()
3941 else if (mmu->root_role.level == PT64_ROOT_4LEVEL) in mmu_alloc_shadow_roots()
3942 mmu->root.hpa = __pa(mmu->pml4_root); in mmu_alloc_shadow_roots()
3944 mmu->root.hpa = __pa(mmu->pae_root); in mmu_alloc_shadow_roots()
3947 mmu->root.pgd = root_pgd; in mmu_alloc_shadow_roots()
3949 write_unlock(&vcpu->kvm->mmu_lock); in mmu_alloc_shadow_roots()
3956 struct kvm_mmu *mmu = vcpu->arch.mmu; in mmu_alloc_special_roots()
3957 bool need_pml5 = mmu->root_role.level > PT64_ROOT_4LEVEL; in mmu_alloc_special_roots()
3963 * When shadowing 32-bit or PAE NPT with 64-bit NPT, the PML4 and PDP in mmu_alloc_special_roots()
3966 * on demand, as running a 32-bit L1 VMM on 64-bit KVM is very rare. in mmu_alloc_special_roots()
3968 if (mmu->root_role.direct || in mmu_alloc_special_roots()
3969 mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL || in mmu_alloc_special_roots()
3970 mmu->root_role.level < PT64_ROOT_4LEVEL) in mmu_alloc_special_roots()
3975 * of levels for the shadow page tables, e.g. all MMUs are 4-level or in mmu_alloc_special_roots()
3976 * all MMus are 5-level. Thus, this can safely require that pml5_root in mmu_alloc_special_roots()
3980 if (mmu->pae_root && mmu->pml4_root && (!need_pml5 || mmu->pml5_root)) in mmu_alloc_special_roots()
3987 if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root || in mmu_alloc_special_roots()
3988 (need_pml5 && mmu->pml5_root))) in mmu_alloc_special_roots()
3989 return -EIO; in mmu_alloc_special_roots()
3992 * Unlike 32-bit NPT, the PDP table doesn't need to be in low mem, and in mmu_alloc_special_roots()
3997 return -ENOMEM; in mmu_alloc_special_roots()
4011 mmu->pae_root = pae_root; in mmu_alloc_special_roots()
4012 mmu->pml4_root = pml4_root; in mmu_alloc_special_roots()
4013 mmu->pml5_root = pml5_root; in mmu_alloc_special_roots()
4022 return -ENOMEM; in mmu_alloc_special_roots()
4035 * walk before the reads of sp->unsync/sp->unsync_children here. in is_unsync_root()
4037 * Even if another CPU was marking the SP as unsync-ed simultaneously, in is_unsync_root()
4055 if (sp->unsync || sp->unsync_children) in is_unsync_root()
4066 if (vcpu->arch.mmu->root_role.direct) in kvm_mmu_sync_roots()
4069 if (!VALID_PAGE(vcpu->arch.mmu->root.hpa)) in kvm_mmu_sync_roots()
4074 if (vcpu->arch.mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL) { in kvm_mmu_sync_roots()
4075 hpa_t root = vcpu->arch.mmu->root.hpa; in kvm_mmu_sync_roots()
4082 write_lock(&vcpu->kvm->mmu_lock); in kvm_mmu_sync_roots()
4084 write_unlock(&vcpu->kvm->mmu_lock); in kvm_mmu_sync_roots()
4088 write_lock(&vcpu->kvm->mmu_lock); in kvm_mmu_sync_roots()
4091 hpa_t root = vcpu->arch.mmu->pae_root[i]; in kvm_mmu_sync_roots()
4099 write_unlock(&vcpu->kvm->mmu_lock); in kvm_mmu_sync_roots()
4108 if (is_unsync_root(vcpu->arch.mmu->prev_roots[i].hpa)) in kvm_mmu_sync_prev_roots()
4112 kvm_mmu_free_roots(vcpu->kvm, vcpu->arch.mmu, roots_to_free); in kvm_mmu_sync_prev_roots()
4120 exception->error_code = 0; in nonpaging_gva_to_gpa()
4141 * That SPTE may be non-present.
4148 int leaf = -1; in get_walk()
4180 /* return true if reserved bit(s) are detected on a valid, non-MMIO SPTE. */
4200 * to detect reserved bits on non-MMIO SPTEs. i.e. buggy SPTEs. in get_mmio_spte()
4205 rsvd_check = &vcpu->arch.mmu->shadow_zero_check; in get_mmio_spte()
4207 for (level = root; level >= leaf; level--) in get_mmio_spte()
4211 pr_err("%s: reserved bits set on MMU-present spte, addr 0x%llx, hierarchy:\n", in get_mmio_spte()
4213 for (level = root; level >= leaf; level--) in get_mmio_spte()
4214 pr_err("------ spte = 0x%llx level = %d, rsvd bits = 0x%llx", in get_mmio_spte()
4232 return -EINVAL; in handle_mmio_page_fault()
4234 if (is_mmio_spte(vcpu->kvm, spte)) { in handle_mmio_page_fault()
4259 if (unlikely(fault->rsvd)) in page_fault_handle_page_track()
4262 if (!fault->present || !fault->write) in page_fault_handle_page_track()
4269 if (kvm_gfn_is_write_tracked(vcpu->kvm, fault->slot, fault->gfn)) in page_fault_handle_page_track()
4289 u32 id = vcpu->arch.apf.id; in alloc_apf_token()
4292 vcpu->arch.apf.id = 1; in alloc_apf_token()
4294 return (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; in alloc_apf_token()
4303 arch.gfn = fault->gfn; in kvm_arch_setup_async_pf()
4304 arch.error_code = fault->error_code; in kvm_arch_setup_async_pf()
4305 arch.direct_map = vcpu->arch.mmu->root_role.direct; in kvm_arch_setup_async_pf()
4306 arch.cr3 = kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu); in kvm_arch_setup_async_pf()
4308 return kvm_setup_async_pf(vcpu, fault->addr, in kvm_arch_setup_async_pf()
4309 kvm_vcpu_gfn_to_hva(vcpu, fault->gfn), &arch); in kvm_arch_setup_async_pf()
4316 if (WARN_ON_ONCE(work->arch.error_code & PFERR_PRIVATE_ACCESS)) in kvm_arch_async_page_ready()
4319 if ((vcpu->arch.mmu->root_role.direct != work->arch.direct_map) || in kvm_arch_async_page_ready()
4320 work->wakeup_all) in kvm_arch_async_page_ready()
4327 if (!vcpu->arch.mmu->root_role.direct && in kvm_arch_async_page_ready()
4328 work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu)) in kvm_arch_async_page_ready()
4331 r = kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code, in kvm_arch_async_page_ready()
4336 * ignore stats for all other return times. Page-ready "faults" aren't in kvm_arch_async_page_ready()
4340 vcpu->stat.pf_fixed++; in kvm_arch_async_page_ready()
4384 if (!kvm_slot_can_be_private(fault->slot)) { in kvm_faultin_pfn_private()
4386 return -EFAULT; in kvm_faultin_pfn_private()
4389 r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &fault->pfn, in kvm_faultin_pfn_private()
4396 fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); in kvm_faultin_pfn_private()
4397 fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->pfn, in kvm_faultin_pfn_private()
4398 fault->max_level, max_order); in kvm_faultin_pfn_private()
4407 if (fault->is_private) in __kvm_faultin_pfn()
4411 fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, false, in __kvm_faultin_pfn()
4412 &async, fault->write, in __kvm_faultin_pfn()
4413 &fault->map_writable, &fault->hva); in __kvm_faultin_pfn()
4417 if (!fault->prefetch && kvm_can_do_async_pf(vcpu)) { in __kvm_faultin_pfn()
4418 trace_kvm_try_async_get_page(fault->addr, fault->gfn); in __kvm_faultin_pfn()
4419 if (kvm_find_async_pf_gfn(vcpu, fault->gfn)) { in __kvm_faultin_pfn()
4420 trace_kvm_async_pf_repeated_fault(fault->addr, fault->gfn); in __kvm_faultin_pfn()
4429 * Allow gup to bail on pending non-fatal signals when it's also allowed in __kvm_faultin_pfn()
4433 fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, in __kvm_faultin_pfn()
4434 NULL, fault->write, in __kvm_faultin_pfn()
4435 &fault->map_writable, &fault->hva); in __kvm_faultin_pfn()
4442 struct kvm_memory_slot *slot = fault->slot; in kvm_faultin_pfn()
4448 * invalidation relate to fault->fn and resume the guest without in kvm_faultin_pfn()
4451 fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq; in kvm_faultin_pfn()
4458 if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { in kvm_faultin_pfn()
4460 return -EFAULT; in kvm_faultin_pfn()
4471 if (slot->flags & KVM_MEMSLOT_INVALID) in kvm_faultin_pfn()
4474 if (slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT) { in kvm_faultin_pfn()
4493 * when the AVIC is re-enabled. in kvm_faultin_pfn()
4495 if (!kvm_apicv_activated(vcpu->kvm)) in kvm_faultin_pfn()
4503 * For mmu_lock, if there is an in-progress invalidation and the kernel in kvm_faultin_pfn()
4505 * in response to mmu_lock being contended, which is *very* counter- in kvm_faultin_pfn()
4515 * Do the pre-check even for non-preemtible kernels, i.e. even if KVM in kvm_faultin_pfn()
4520 if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) in kvm_faultin_pfn()
4527 if (unlikely(is_error_pfn(fault->pfn))) in kvm_faultin_pfn()
4530 if (WARN_ON_ONCE(!fault->slot || is_noslot_pfn(fault->pfn))) in kvm_faultin_pfn()
4540 if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) { in kvm_faultin_pfn()
4541 kvm_release_pfn_clean(fault->pfn); in kvm_faultin_pfn()
4555 struct kvm_mmu_page *sp = root_to_sp(vcpu->arch.mmu->root.hpa); in is_page_fault_stale()
4558 if (sp && is_obsolete_sp(vcpu->kvm, sp)) in is_page_fault_stale()
4566 * previous root, then __kvm_mmu_prepare_zap_page() signals all vCPUs in is_page_fault_stale()
4577 return fault->slot && in is_page_fault_stale()
4578 mmu_invalidate_retry_gfn(vcpu->kvm, fault->mmu_seq, fault->gfn); in is_page_fault_stale()
4586 if (WARN_ON_ONCE(kvm_mmu_is_dummy_root(vcpu->arch.mmu->root.hpa))) in direct_page_fault()
4605 write_lock(&vcpu->kvm->mmu_lock); in direct_page_fault()
4617 write_unlock(&vcpu->kvm->mmu_lock); in direct_page_fault()
4618 kvm_release_pfn_clean(fault->pfn); in direct_page_fault()
4626 fault->max_level = PG_LEVEL_2M; in nonpaging_page_fault()
4634 u32 flags = vcpu->arch.apf.host_apf_flags; in kvm_handle_page_fault()
4637 /* A 64-bit CR2 should be impossible on 32-bit KVM. */ in kvm_handle_page_fault()
4639 return -EFAULT; in kvm_handle_page_fault()
4642 * Legacy #PF exception only have a 32-bit error code. Simply drop the in kvm_handle_page_fault()
4644 * set), and to ensure there are no collisions with KVM-defined bits. in kvm_handle_page_fault()
4650 * Restrict KVM-defined flags to bits 63:32 so that it's impossible for in kvm_handle_page_fault()
4655 vcpu->arch.l1tf_flush_l1d = true; in kvm_handle_page_fault()
4662 vcpu->arch.apf.host_apf_flags = 0; in kvm_handle_page_fault()
4696 read_lock(&vcpu->kvm->mmu_lock); in kvm_tdp_mmu_page_fault()
4704 read_unlock(&vcpu->kvm->mmu_lock); in kvm_tdp_mmu_page_fault()
4705 kvm_release_pfn_clean(fault->pfn); in kvm_tdp_mmu_page_fault()
4713 * When EPT is enabled (shadow_memtype_mask is non-zero), and the VM in kvm_mmu_may_ignore_guest_pat()
4714 * has non-coherent DMA (DMA doesn't snoop CPU caches), KVM's ABI is to in kvm_mmu_may_ignore_guest_pat()
4717 * result, KVM _may_ ignore guest PAT, whereas without non-coherent DMA, in kvm_mmu_may_ignore_guest_pat()
4742 if (vcpu->arch.mmu->page_fault != kvm_tdp_page_fault) in kvm_tdp_map_page()
4743 return -EOPNOTSUPP; in kvm_tdp_map_page()
4747 return -EINTR; in kvm_tdp_map_page()
4762 return -ENOENT; in kvm_tdp_map_page()
4769 return -EIO; in kvm_tdp_map_page()
4781 if (!vcpu->kvm->arch.pre_fault_allowed) in kvm_arch_vcpu_pre_fault_memory()
4782 return -EOPNOTSUPP; in kvm_arch_vcpu_pre_fault_memory()
4792 if (kvm_arch_has_private_mem(vcpu->kvm) && in kvm_arch_vcpu_pre_fault_memory()
4793 kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa))) in kvm_arch_vcpu_pre_fault_memory()
4798 * two-dimensional paging. in kvm_arch_vcpu_pre_fault_memory()
4800 r = kvm_tdp_map_page(vcpu, range->gpa, error_code, &level); in kvm_arch_vcpu_pre_fault_memory()
4805 * If the mapping that covers range->gpa can use a huge page, it in kvm_arch_vcpu_pre_fault_memory()
4806 * may start below it or end after range->gpa + range->size. in kvm_arch_vcpu_pre_fault_memory()
4808 end = (range->gpa & KVM_HPAGE_MASK(level)) + KVM_HPAGE_SIZE(level); in kvm_arch_vcpu_pre_fault_memory()
4809 return min(range->size, end - range->gpa); in kvm_arch_vcpu_pre_fault_memory()
4814 context->page_fault = nonpaging_page_fault; in nonpaging_init_context()
4815 context->gva_to_gpa = nonpaging_gva_to_gpa; in nonpaging_init_context()
4816 context->sync_spte = NULL; in nonpaging_init_context()
4824 if (!VALID_PAGE(root->hpa)) in is_root_usable()
4827 if (!role.direct && pgd != root->pgd) in is_root_usable()
4830 sp = root_to_sp(root->hpa); in is_root_usable()
4834 return role.word == sp->role.word; in is_root_usable()
4840 * If a matching root is found, it is assigned to kvm_mmu->root and
4842 * If no match is found, kvm_mmu->root is left invalid, the LRU root is
4851 if (is_root_usable(&mmu->root, new_pgd, new_role)) in cached_root_find_and_keep_current()
4863 swap(mmu->root, mmu->prev_roots[i]); in cached_root_find_and_keep_current()
4864 if (is_root_usable(&mmu->root, new_pgd, new_role)) in cached_root_find_and_keep_current()
4874 * On entry, mmu->root is invalid.
4875 * If a matching root is found, it is assigned to kvm_mmu->root, the LRU entry
4877 * If no match is found, kvm_mmu->root is left invalid and false is returned.
4886 if (is_root_usable(&mmu->prev_roots[i], new_pgd, new_role)) in cached_root_find_without_current()
4892 swap(mmu->root, mmu->prev_roots[i]); in cached_root_find_without_current()
4894 for (; i < KVM_MMU_NUM_PREV_ROOTS - 1; i++) in cached_root_find_without_current()
4895 mmu->prev_roots[i] = mmu->prev_roots[i + 1]; in cached_root_find_without_current()
4896 mmu->prev_roots[i].hpa = INVALID_PAGE; in cached_root_find_without_current()
4904 * Limit reuse to 64-bit hosts+VMs without "special" roots in order to in fast_pgd_switch()
4907 if (VALID_PAGE(mmu->root.hpa) && !root_to_sp(mmu->root.hpa)) in fast_pgd_switch()
4910 if (VALID_PAGE(mmu->root.hpa)) in fast_pgd_switch()
4918 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_mmu_new_pgd()
4919 union kvm_mmu_page_role new_role = mmu->root_role; in kvm_mmu_new_pgd()
4923 * will establish a valid root prior to the next VM-Enter. in kvm_mmu_new_pgd()
4925 if (!fast_pgd_switch(vcpu->kvm, mmu, new_pgd, new_role)) in kvm_mmu_new_pgd()
4943 * switching to a new CR3, that GVA->GPA mapping may no longer be in kvm_mmu_new_pgd()
4954 struct kvm_mmu_page *sp = root_to_sp(vcpu->arch.mmu->root.hpa); in kvm_mmu_new_pgd()
4965 if (unlikely(is_mmio_spte(vcpu->kvm, *sptep))) { in sync_mmio_spte()
4999 rsvd_check->bad_mt_xwr = 0; in __reset_rsvds_bits_mask()
5014 * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for in __reset_rsvds_bits_mask()
5023 rsvd_check->rsvd_bits_mask[0][1] = 0; in __reset_rsvds_bits_mask()
5024 rsvd_check->rsvd_bits_mask[0][0] = 0; in __reset_rsvds_bits_mask()
5025 rsvd_check->rsvd_bits_mask[1][0] = in __reset_rsvds_bits_mask()
5026 rsvd_check->rsvd_bits_mask[0][0]; in __reset_rsvds_bits_mask()
5029 rsvd_check->rsvd_bits_mask[1][1] = 0; in __reset_rsvds_bits_mask()
5035 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); in __reset_rsvds_bits_mask()
5038 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); in __reset_rsvds_bits_mask()
5041 rsvd_check->rsvd_bits_mask[0][2] = rsvd_bits(63, 63) | in __reset_rsvds_bits_mask()
5045 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd; /* PDE */ in __reset_rsvds_bits_mask()
5046 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd; /* PTE */ in __reset_rsvds_bits_mask()
5047 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd | in __reset_rsvds_bits_mask()
5049 rsvd_check->rsvd_bits_mask[1][0] = in __reset_rsvds_bits_mask()
5050 rsvd_check->rsvd_bits_mask[0][0]; in __reset_rsvds_bits_mask()
5053 rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd | in __reset_rsvds_bits_mask()
5056 rsvd_check->rsvd_bits_mask[1][4] = in __reset_rsvds_bits_mask()
5057 rsvd_check->rsvd_bits_mask[0][4]; in __reset_rsvds_bits_mask()
5060 rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd | in __reset_rsvds_bits_mask()
5063 rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd | in __reset_rsvds_bits_mask()
5065 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd; in __reset_rsvds_bits_mask()
5066 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd; in __reset_rsvds_bits_mask()
5067 rsvd_check->rsvd_bits_mask[1][3] = in __reset_rsvds_bits_mask()
5068 rsvd_check->rsvd_bits_mask[0][3]; in __reset_rsvds_bits_mask()
5069 rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd | in __reset_rsvds_bits_mask()
5072 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd | in __reset_rsvds_bits_mask()
5074 rsvd_check->rsvd_bits_mask[1][0] = in __reset_rsvds_bits_mask()
5075 rsvd_check->rsvd_bits_mask[0][0]; in __reset_rsvds_bits_mask()
5083 __reset_rsvds_bits_mask(&context->guest_rsvd_check, in reset_guest_rsvds_bits_mask()
5084 vcpu->arch.reserved_gpa_bits, in reset_guest_rsvds_bits_mask()
5085 context->cpu_role.base.level, is_efer_nx(context), in reset_guest_rsvds_bits_mask()
5104 rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd | rsvd_bits(3, 7); in __reset_rsvds_bits_mask_ept()
5105 rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd | rsvd_bits(3, 7); in __reset_rsvds_bits_mask_ept()
5106 rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd | rsvd_bits(3, 6) | large_1g_rsvd; in __reset_rsvds_bits_mask_ept()
5107 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd | rsvd_bits(3, 6) | large_2m_rsvd; in __reset_rsvds_bits_mask_ept()
5108 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd; in __reset_rsvds_bits_mask_ept()
5111 rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4]; in __reset_rsvds_bits_mask_ept()
5112 rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3]; in __reset_rsvds_bits_mask_ept()
5113 rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd | rsvd_bits(12, 29) | large_1g_rsvd; in __reset_rsvds_bits_mask_ept()
5114 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd | rsvd_bits(12, 20) | large_2m_rsvd; in __reset_rsvds_bits_mask_ept()
5115 rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0]; in __reset_rsvds_bits_mask_ept()
5126 rsvd_check->bad_mt_xwr = bad_mt_xwr; in __reset_rsvds_bits_mask_ept()
5132 __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check, in reset_rsvds_bits_mask_ept()
5133 vcpu->arch.reserved_gpa_bits, execonly, in reset_rsvds_bits_mask_ept()
5152 /* KVM doesn't use 2-level page tables for the shadow MMU. */ in reset_shadow_zero_bits_mask()
5157 WARN_ON_ONCE(context->root_role.level < PT32E_ROOT_LEVEL); in reset_shadow_zero_bits_mask()
5159 shadow_zero_check = &context->shadow_zero_check; in reset_shadow_zero_bits_mask()
5161 context->root_role.level, in reset_shadow_zero_bits_mask()
5162 context->root_role.efer_nx, in reset_shadow_zero_bits_mask()
5169 for (i = context->root_role.level; --i >= 0;) { in reset_shadow_zero_bits_mask()
5176 shadow_zero_check->rsvd_bits_mask[0][i] |= shadow_me_mask; in reset_shadow_zero_bits_mask()
5177 shadow_zero_check->rsvd_bits_mask[1][i] |= shadow_me_mask; in reset_shadow_zero_bits_mask()
5178 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_value; in reset_shadow_zero_bits_mask()
5179 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_value; in reset_shadow_zero_bits_mask()
5192 * possible, however, kvm currently does not do execution-protection.
5199 shadow_zero_check = &context->shadow_zero_check; in reset_tdp_shadow_zero_bits_mask()
5203 context->root_role.level, true, in reset_tdp_shadow_zero_bits_mask()
5214 for (i = context->root_role.level; --i >= 0;) { in reset_tdp_shadow_zero_bits_mask()
5215 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask; in reset_tdp_shadow_zero_bits_mask()
5216 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask; in reset_tdp_shadow_zero_bits_mask()
5227 __reset_rsvds_bits_mask_ept(&context->shadow_zero_check, in reset_ept_shadow_zero_bits_mask()
5255 for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) { in update_permission_bitmask()
5263 /* Faults from writes to non-writable pages */ in update_permission_bitmask()
5267 /* Faults from fetches of non-executable pages*/ in update_permission_bitmask()
5291 * SMAP:kernel-mode data accesses from user-mode in update_permission_bitmask()
5295 * - X86_CR4_SMAP is set in CR4 in update_permission_bitmask()
5296 * - A user page is accessed in update_permission_bitmask()
5297 * - The access is not a fetch in update_permission_bitmask()
5298 * - The access is supervisor mode in update_permission_bitmask()
5299 * - If implicit supervisor access or X86_EFLAGS_AC is clear in update_permission_bitmask()
5310 mmu->permissions[byte] = ff | uf | wf | smepf | smapf; in update_permission_bitmask()
5316 * user-mode addresses based on the value in the PKRU register. Protection
5325 * - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
5326 * - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
5327 * - PK is always zero if U=0 in the page tables
5328 * - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
5343 mmu->pkru_mask = 0; in update_pkru_bitmask()
5350 for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) { in update_pkru_bitmask()
5378 mmu->pkru_mask |= (pkey_bits & 3) << pfec; in update_pkru_bitmask()
5395 context->page_fault = paging64_page_fault; in paging64_init_context()
5396 context->gva_to_gpa = paging64_gva_to_gpa; in paging64_init_context()
5397 context->sync_spte = paging64_sync_spte; in paging64_init_context()
5402 context->page_fault = paging32_page_fault; in paging32_init_context()
5403 context->gva_to_gpa = paging32_gva_to_gpa; in paging32_init_context()
5404 context->sync_spte = paging32_sync_spte; in paging32_init_context()
5458 mmu->cpu_role.base.cr0_wp = cr0_wp; in __kvm_mmu_refresh_passthrough_bits()
5468 /* Use 5-level TDP if and only if it's useful/necessary. */ in kvm_mmu_get_tdp_level()
5502 struct kvm_mmu *context = &vcpu->arch.root_mmu; in init_kvm_tdp_mmu()
5505 if (cpu_role.as_u64 == context->cpu_role.as_u64 && in init_kvm_tdp_mmu()
5506 root_role.word == context->root_role.word) in init_kvm_tdp_mmu()
5509 context->cpu_role.as_u64 = cpu_role.as_u64; in init_kvm_tdp_mmu()
5510 context->root_role.word = root_role.word; in init_kvm_tdp_mmu()
5511 context->page_fault = kvm_tdp_page_fault; in init_kvm_tdp_mmu()
5512 context->sync_spte = NULL; in init_kvm_tdp_mmu()
5513 context->get_guest_pgd = get_guest_cr3; in init_kvm_tdp_mmu()
5514 context->get_pdptr = kvm_pdptr_read; in init_kvm_tdp_mmu()
5515 context->inject_page_fault = kvm_inject_page_fault; in init_kvm_tdp_mmu()
5518 context->gva_to_gpa = nonpaging_gva_to_gpa; in init_kvm_tdp_mmu()
5520 context->gva_to_gpa = paging64_gva_to_gpa; in init_kvm_tdp_mmu()
5522 context->gva_to_gpa = paging32_gva_to_gpa; in init_kvm_tdp_mmu()
5532 if (cpu_role.as_u64 == context->cpu_role.as_u64 && in shadow_mmu_init_context()
5533 root_role.word == context->root_role.word) in shadow_mmu_init_context()
5536 context->cpu_role.as_u64 = cpu_role.as_u64; in shadow_mmu_init_context()
5537 context->root_role.word = root_role.word; in shadow_mmu_init_context()
5553 struct kvm_mmu *context = &vcpu->arch.root_mmu; in kvm_init_shadow_mmu()
5558 /* KVM uses PAE paging whenever the guest isn't using 64-bit paging. */ in kvm_init_shadow_mmu()
5564 * notably for huge SPTEs if iTLB multi-hit mitigation is enabled and in kvm_init_shadow_mmu()
5566 * The iTLB multi-hit workaround can be toggled at any time, so assume in kvm_init_shadow_mmu()
5567 * NX can be used by any non-nested shadow MMU to avoid having to reset in kvm_init_shadow_mmu()
5578 struct kvm_mmu *context = &vcpu->arch.guest_mmu; in kvm_init_shadow_npt_mmu()
5630 struct kvm_mmu *context = &vcpu->arch.guest_mmu; in kvm_init_shadow_ept_mmu()
5636 if (new_mode.as_u64 != context->cpu_role.as_u64) { in kvm_init_shadow_ept_mmu()
5638 context->cpu_role.as_u64 = new_mode.as_u64; in kvm_init_shadow_ept_mmu()
5639 context->root_role.word = new_mode.base.word; in kvm_init_shadow_ept_mmu()
5641 context->page_fault = ept_page_fault; in kvm_init_shadow_ept_mmu()
5642 context->gva_to_gpa = ept_gva_to_gpa; in kvm_init_shadow_ept_mmu()
5643 context->sync_spte = ept_sync_spte; in kvm_init_shadow_ept_mmu()
5646 context->pkru_mask = 0; in kvm_init_shadow_ept_mmu()
5658 struct kvm_mmu *context = &vcpu->arch.root_mmu; in init_kvm_softmmu()
5662 context->get_guest_pgd = get_guest_cr3; in init_kvm_softmmu()
5663 context->get_pdptr = kvm_pdptr_read; in init_kvm_softmmu()
5664 context->inject_page_fault = kvm_inject_page_fault; in init_kvm_softmmu()
5670 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu; in init_kvm_nested_mmu()
5672 if (new_mode.as_u64 == g_context->cpu_role.as_u64) in init_kvm_nested_mmu()
5675 g_context->cpu_role.as_u64 = new_mode.as_u64; in init_kvm_nested_mmu()
5676 g_context->get_guest_pgd = get_guest_cr3; in init_kvm_nested_mmu()
5677 g_context->get_pdptr = kvm_pdptr_read; in init_kvm_nested_mmu()
5678 g_context->inject_page_fault = kvm_inject_page_fault; in init_kvm_nested_mmu()
5684 g_context->sync_spte = NULL; in init_kvm_nested_mmu()
5687 * Note that arch.mmu->gva_to_gpa translates l2_gpa to l1_gpa using in init_kvm_nested_mmu()
5695 g_context->gva_to_gpa = nonpaging_gva_to_gpa; in init_kvm_nested_mmu()
5697 g_context->gva_to_gpa = paging64_gva_to_gpa; in init_kvm_nested_mmu()
5699 g_context->gva_to_gpa = paging64_gva_to_gpa; in init_kvm_nested_mmu()
5701 g_context->gva_to_gpa = paging32_gva_to_gpa; in init_kvm_nested_mmu()
5734 vcpu->arch.root_mmu.root_role.invalid = 1; in kvm_mmu_after_set_cpuid()
5735 vcpu->arch.guest_mmu.root_role.invalid = 1; in kvm_mmu_after_set_cpuid()
5736 vcpu->arch.nested_mmu.root_role.invalid = 1; in kvm_mmu_after_set_cpuid()
5737 vcpu->arch.root_mmu.cpu_role.ext.valid = 0; in kvm_mmu_after_set_cpuid()
5738 vcpu->arch.guest_mmu.cpu_role.ext.valid = 0; in kvm_mmu_after_set_cpuid()
5739 vcpu->arch.nested_mmu.cpu_role.ext.valid = 0; in kvm_mmu_after_set_cpuid()
5746 KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm); in kvm_mmu_after_set_cpuid()
5760 r = mmu_topup_memory_caches(vcpu, !vcpu->arch.mmu->root_role.direct); in kvm_mmu_load()
5766 if (vcpu->arch.mmu->root_role.direct) in kvm_mmu_load()
5791 struct kvm *kvm = vcpu->kvm; in kvm_mmu_unload()
5793 kvm_mmu_free_roots(kvm, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL); in kvm_mmu_unload()
5794 WARN_ON_ONCE(VALID_PAGE(vcpu->arch.root_mmu.root.hpa)); in kvm_mmu_unload()
5795 kvm_mmu_free_roots(kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); in kvm_mmu_unload()
5796 WARN_ON_ONCE(VALID_PAGE(vcpu->arch.guest_mmu.root.hpa)); in kvm_mmu_unload()
5818 * is unlikely to zap an in-use PGD. in is_obsolete_root()
5832 if (is_obsolete_root(kvm, mmu->root.hpa)) in __kvm_mmu_free_obsolete_roots()
5836 if (is_obsolete_root(kvm, mmu->prev_roots[i].hpa)) in __kvm_mmu_free_obsolete_roots()
5846 __kvm_mmu_free_obsolete_roots(vcpu->kvm, &vcpu->arch.root_mmu); in kvm_mmu_free_obsolete_roots()
5847 __kvm_mmu_free_obsolete_roots(vcpu->kvm, &vcpu->arch.guest_mmu); in kvm_mmu_free_obsolete_roots()
5862 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ in mmu_pte_write_fetch_gpte()
5883 * Skip write-flooding detected for the sp whose level is 1, because in detect_write_flooding()
5884 * it can become unsync, then the guest page is not write-protected. in detect_write_flooding()
5886 if (sp->role.level == PG_LEVEL_4K) in detect_write_flooding()
5889 atomic_inc(&sp->write_flooding_count); in detect_write_flooding()
5890 return atomic_read(&sp->write_flooding_count) >= 3; in detect_write_flooding()
5903 pte_size = sp->role.has_4_byte_gpte ? 4 : 8; in detect_write_misaligned()
5909 if (!(offset & (pte_size - 1)) && bytes == 1) in detect_write_misaligned()
5912 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); in detect_write_misaligned()
5925 level = sp->role.level; in get_written_sptes()
5927 if (sp->role.has_4_byte_gpte) { in get_written_sptes()
5928 page_offset <<= 1; /* 32->64 */ in get_written_sptes()
5930 * A 32-bit pde maps 4MB while the shadow pdes map in get_written_sptes()
5941 if (quadrant != sp->role.quadrant) in get_written_sptes()
5945 spte = &sp->spt[page_offset / sizeof(*spte)]; in get_written_sptes()
5964 * a non-zero indirect_shadow_pages. Pairs with the smp_mb() in in kvm_mmu_track_write()
5968 if (!vcpu->kvm->arch.indirect_shadow_pages) in kvm_mmu_track_write()
5971 write_lock(&vcpu->kvm->mmu_lock); in kvm_mmu_track_write()
5975 ++vcpu->kvm->stat.mmu_pte_write; in kvm_mmu_track_write()
5977 for_each_gfn_valid_sp_with_gptes(vcpu->kvm, sp, gfn) { in kvm_mmu_track_write()
5980 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); in kvm_mmu_track_write()
5981 ++vcpu->kvm->stat.mmu_flooded; in kvm_mmu_track_write()
5989 while (npte--) { in kvm_mmu_track_write()
5991 mmu_page_zap_pte(vcpu->kvm, sp, spte, NULL); in kvm_mmu_track_write()
5992 if (gentry && sp->role.level != PG_LEVEL_4K) in kvm_mmu_track_write()
5993 ++vcpu->kvm->stat.mmu_pde_zapped; in kvm_mmu_track_write()
5999 kvm_mmu_remote_flush_or_zap(vcpu->kvm, &invalid_list, flush); in kvm_mmu_track_write()
6000 write_unlock(&vcpu->kvm->mmu_lock); in kvm_mmu_track_write()
6013 bool direct = vcpu->arch.mmu->root_role.direct; in kvm_mmu_write_protect_fault()
6016 * Do not try to unprotect and retry if the vCPU re-faulted on the same in kvm_mmu_write_protect_fault()
6019 * a non-page-table modifying instruction on the PDE that points to the in kvm_mmu_write_protect_fault()
6023 if (vcpu->arch.last_retry_eip == kvm_rip_read(vcpu) && in kvm_mmu_write_protect_fault()
6024 vcpu->arch.last_retry_addr == cr2_or_gpa) in kvm_mmu_write_protect_fault()
6033 vcpu->arch.last_retry_eip = 0; in kvm_mmu_write_protect_fault()
6034 vcpu->arch.last_retry_addr = 0; in kvm_mmu_write_protect_fault()
6043 * will keep faulting on the non-existent MMIO address. in kvm_mmu_write_protect_fault()
6050 * to a read-only violation while the CPU was walking non-nested NPT in kvm_mmu_write_protect_fault()
6054 * (L0) write-protects the nested NPTs, i.e. npt12 entries, KVM is also in kvm_mmu_write_protect_fault()
6055 * unknowingly write-protecting L1's guest page tables, which KVM isn't in kvm_mmu_write_protect_fault()
6063 * isn't shadowed by KVM, there is no need to write-protect L1's gPTEs in kvm_mmu_write_protect_fault()
6069 * the instruction. If no shadow pages were zapped, then the write- in kvm_mmu_write_protect_fault()
6089 * The gfn is write-protected, but if KVM detects its emulating an in kvm_mmu_write_protect_fault()
6092 * re-execute the instruction that caused the page fault. Do not allow in kvm_mmu_write_protect_fault()
6107 bool direct = vcpu->arch.mmu->root_role.direct; in kvm_mmu_page_fault()
6109 if (WARN_ON_ONCE(!VALID_PAGE(vcpu->arch.mmu->root.hpa))) in kvm_mmu_page_fault()
6113 * Except for reserved faults (emulated MMIO is shared-only), set the in kvm_mmu_page_fault()
6114 * PFERR_PRIVATE_ACCESS flag for software-protected VMs based on the gfn's in kvm_mmu_page_fault()
6118 * for software-protected VMs. in kvm_mmu_page_fault()
6122 vcpu->kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM && in kvm_mmu_page_fault()
6123 kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(cr2_or_gpa))) in kvm_mmu_page_fault()
6129 return -EFAULT; in kvm_mmu_page_fault()
6137 vcpu->stat.pf_taken++; in kvm_mmu_page_fault()
6141 if (KVM_BUG_ON(r == RET_PF_INVALID, vcpu->kvm)) in kvm_mmu_page_fault()
6142 return -EIO; in kvm_mmu_page_fault()
6153 vcpu->stat.pf_fixed++; in kvm_mmu_page_fault()
6155 vcpu->stat.pf_emulate++; in kvm_mmu_page_fault()
6157 vcpu->stat.pf_spurious++; in kvm_mmu_page_fault()
6178 for (level = root_level; level >= leaf; level--) in kvm_mmu_print_sptes()
6196 if (WARN_ON_ONCE(mmu != vcpu->arch.mmu)) in __kvm_mmu_invalidate_addr()
6202 write_lock(&vcpu->kvm->mmu_lock); in __kvm_mmu_invalidate_addr()
6206 if (sp->unsync) { in __kvm_mmu_invalidate_addr()
6210 mmu_page_zap_pte(vcpu->kvm, sp, iterator.sptep, NULL); in __kvm_mmu_invalidate_addr()
6212 kvm_flush_remote_tlbs_sptep(vcpu->kvm, iterator.sptep); in __kvm_mmu_invalidate_addr()
6215 if (!sp->unsync_children) in __kvm_mmu_invalidate_addr()
6218 write_unlock(&vcpu->kvm->mmu_lock); in __kvm_mmu_invalidate_addr()
6228 /* It's actually a GPA for vcpu->arch.guest_mmu. */ in kvm_mmu_invalidate_addr()
6229 if (mmu != &vcpu->arch.guest_mmu) { in kvm_mmu_invalidate_addr()
6230 /* INVLPG on a non-canonical address is a NOP according to the SDM. */ in kvm_mmu_invalidate_addr()
6237 if (!mmu->sync_spte) in kvm_mmu_invalidate_addr()
6241 __kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu->root.hpa); in kvm_mmu_invalidate_addr()
6245 __kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu->prev_roots[i].hpa); in kvm_mmu_invalidate_addr()
6262 kvm_mmu_invalidate_addr(vcpu, vcpu->arch.walk_mmu, gva, KVM_MMU_ROOTS_ALL); in kvm_mmu_invlpg()
6263 ++vcpu->stat.invlpg; in kvm_mmu_invlpg()
6270 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_mmu_invpcid_gva()
6278 if (VALID_PAGE(mmu->prev_roots[i].hpa) && in kvm_mmu_invpcid_gva()
6279 pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd)) in kvm_mmu_invpcid_gva()
6285 ++vcpu->stat.invlpg; in kvm_mmu_invpcid_gva()
6322 if (!tdp_enabled && mmu->pae_root) in free_mmu_pages()
6323 set_memory_encrypted((unsigned long)mmu->pae_root, 1); in free_mmu_pages()
6324 free_page((unsigned long)mmu->pae_root); in free_mmu_pages()
6325 free_page((unsigned long)mmu->pml4_root); in free_mmu_pages()
6326 free_page((unsigned long)mmu->pml5_root); in free_mmu_pages()
6334 mmu->root.hpa = INVALID_PAGE; in __kvm_mmu_create()
6335 mmu->root.pgd = 0; in __kvm_mmu_create()
6337 mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID; in __kvm_mmu_create()
6339 /* vcpu->arch.guest_mmu isn't used when !tdp_enabled. */ in __kvm_mmu_create()
6340 if (!tdp_enabled && mmu == &vcpu->arch.guest_mmu) in __kvm_mmu_create()
6345 * while the PDP table is a per-vCPU construct that's allocated at MMU in __kvm_mmu_create()
6346 * creation. When emulating 32-bit mode, cr3 is only 32 bits even on in __kvm_mmu_create()
6350 * table. The main exception, handled here, is SVM's 32-bit NPT. The in __kvm_mmu_create()
6351 * other exception is for shadowing L1's 32-bit or PAE NPT on 64-bit in __kvm_mmu_create()
6352 * KVM; that horror is handled on-demand by mmu_alloc_special_roots(). in __kvm_mmu_create()
6359 return -ENOMEM; in __kvm_mmu_create()
6361 mmu->pae_root = page_address(page); in __kvm_mmu_create()
6367 * only necessary when using shadow paging, as 64-bit NPT can get at in __kvm_mmu_create()
6368 * the C-bit even when shadowing 32-bit NPT, and SME isn't supported in __kvm_mmu_create()
6369 * by 32-bit kernels (when KVM itself uses 32-bit NPT). in __kvm_mmu_create()
6372 set_memory_decrypted((unsigned long)mmu->pae_root, 1); in __kvm_mmu_create()
6377 mmu->pae_root[i] = INVALID_PAE_ROOT; in __kvm_mmu_create()
6386 vcpu->arch.mmu_pte_list_desc_cache.kmem_cache = pte_list_desc_cache; in kvm_mmu_create()
6387 vcpu->arch.mmu_pte_list_desc_cache.gfp_zero = __GFP_ZERO; in kvm_mmu_create()
6389 vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; in kvm_mmu_create()
6390 vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; in kvm_mmu_create()
6392 vcpu->arch.mmu_shadow_page_cache.init_value = in kvm_mmu_create()
6394 if (!vcpu->arch.mmu_shadow_page_cache.init_value) in kvm_mmu_create()
6395 vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; in kvm_mmu_create()
6397 vcpu->arch.mmu = &vcpu->arch.root_mmu; in kvm_mmu_create()
6398 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; in kvm_mmu_create()
6400 ret = __kvm_mmu_create(vcpu, &vcpu->arch.guest_mmu); in kvm_mmu_create()
6404 ret = __kvm_mmu_create(vcpu, &vcpu->arch.root_mmu); in kvm_mmu_create()
6410 free_mmu_pages(&vcpu->arch.guest_mmu); in kvm_mmu_create()
6423 &kvm->arch.active_mmu_pages, link) { in kvm_zap_obsolete_pages()
6436 if (WARN_ON_ONCE(sp->role.invalid)) in kvm_zap_obsolete_pages()
6446 cond_resched_rwlock_write(&kvm->mmu_lock)) { in kvm_zap_obsolete_pages()
6452 &kvm->arch.zapped_obsolete_pages, &nr_zapped); in kvm_zap_obsolete_pages()
6468 kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages); in kvm_zap_obsolete_pages()
6472 * Fast invalidate all shadow pages and use lock-break technique
6477 * not use any resource of the being-deleted slot or all slots
6482 lockdep_assert_held(&kvm->slots_lock); in kvm_mmu_zap_all_fast()
6484 write_lock(&kvm->mmu_lock); in kvm_mmu_zap_all_fast()
6494 kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1; in kvm_mmu_zap_all_fast()
6497 * In order to ensure all vCPUs drop their soon-to-be invalid roots, in kvm_mmu_zap_all_fast()
6517 write_unlock(&kvm->mmu_lock); in kvm_mmu_zap_all_fast()
6525 * lead to use-after-free. in kvm_mmu_zap_all_fast()
6533 return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages)); in kvm_has_zapped_obsolete_pages()
6538 kvm->arch.shadow_mmio_value = shadow_mmio_value; in kvm_mmu_init_vm()
6539 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); in kvm_mmu_init_vm()
6540 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages); in kvm_mmu_init_vm()
6541 INIT_LIST_HEAD(&kvm->arch.possible_nx_huge_pages); in kvm_mmu_init_vm()
6542 spin_lock_init(&kvm->arch.mmu_unsync_pages_lock); in kvm_mmu_init_vm()
6547 kvm->arch.split_page_header_cache.kmem_cache = mmu_page_header_cache; in kvm_mmu_init_vm()
6548 kvm->arch.split_page_header_cache.gfp_zero = __GFP_ZERO; in kvm_mmu_init_vm()
6550 kvm->arch.split_shadow_page_cache.gfp_zero = __GFP_ZERO; in kvm_mmu_init_vm()
6552 kvm->arch.split_desc_cache.kmem_cache = pte_list_desc_cache; in kvm_mmu_init_vm()
6553 kvm->arch.split_desc_cache.gfp_zero = __GFP_ZERO; in kvm_mmu_init_vm()
6558 kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache); in mmu_free_vm_memory_caches()
6559 kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache); in mmu_free_vm_memory_caches()
6560 kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache); in mmu_free_vm_memory_caches()
6588 start = max(gfn_start, memslot->base_gfn); in kvm_rmap_zap_gfn_range()
6589 end = min(gfn_end, memslot->base_gfn + memslot->npages); in kvm_rmap_zap_gfn_range()
6612 write_lock(&kvm->mmu_lock); in kvm_zap_gfn_range()
6624 kvm_flush_remote_tlbs_range(kvm, gfn_start, gfn_end - gfn_start); in kvm_zap_gfn_range()
6628 write_unlock(&kvm->mmu_lock); in kvm_zap_gfn_range()
6643 write_lock(&kvm->mmu_lock); in kvm_mmu_slot_remove_write_access()
6646 write_unlock(&kvm->mmu_lock); in kvm_mmu_slot_remove_write_access()
6650 read_lock(&kvm->mmu_lock); in kvm_mmu_slot_remove_write_access()
6652 read_unlock(&kvm->mmu_lock); in kvm_mmu_slot_remove_write_access()
6663 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) in need_topup_split_caches_or_resched()
6671 return need_topup(&kvm->arch.split_desc_cache, SPLIT_DESC_CACHE_MIN_NR_OBJECTS) || in need_topup_split_caches_or_resched()
6672 need_topup(&kvm->arch.split_page_header_cache, 1) || in need_topup_split_caches_or_resched()
6673 need_topup(&kvm->arch.split_shadow_page_cache, 1); in need_topup_split_caches_or_resched()
6685 * but aliasing rarely occurs post-boot or for many gfns. If there is in topup_split_caches()
6686 * only one rmap entry, rmap->val points directly at that one entry and in topup_split_caches()
6695 lockdep_assert_held(&kvm->slots_lock); in topup_split_caches()
6697 r = __kvm_mmu_topup_memory_cache(&kvm->arch.split_desc_cache, capacity, in topup_split_caches()
6702 r = kvm_mmu_topup_memory_cache(&kvm->arch.split_page_header_cache, 1); in topup_split_caches()
6706 return kvm_mmu_topup_memory_cache(&kvm->arch.split_shadow_page_cache, 1); in topup_split_caches()
6729 caches.page_header_cache = &kvm->arch.split_page_header_cache; in shadow_mmu_get_sp_for_split()
6730 caches.shadow_page_cache = &kvm->arch.split_shadow_page_cache; in shadow_mmu_get_sp_for_split()
6741 struct kvm_mmu_memory_cache *cache = &kvm->arch.split_desc_cache; in shadow_mmu_split_huge_page()
6752 sptep = &sp->spt[index]; in shadow_mmu_split_huge_page()
6759 * gfn-to-pfn translation since the SP is direct, so no need to in shadow_mmu_split_huge_page()
6770 flush |= !is_last_spte(*sptep, sp->role.level); in shadow_mmu_split_huge_page()
6774 spte = make_huge_page_split_spte(kvm, huge_spte, sp->role, index); in shadow_mmu_split_huge_page()
6776 __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access); in shadow_mmu_split_huge_page()
6793 level = huge_sp->role.level; in shadow_mmu_try_split_huge_page()
6797 r = -ENOSPC; in shadow_mmu_try_split_huge_page()
6802 write_unlock(&kvm->mmu_lock); in shadow_mmu_try_split_huge_page()
6805 * If the topup succeeds, return -EAGAIN to indicate that the in shadow_mmu_try_split_huge_page()
6809 r = topup_split_caches(kvm) ?: -EAGAIN; in shadow_mmu_try_split_huge_page()
6810 write_lock(&kvm->mmu_lock); in shadow_mmu_try_split_huge_page()
6835 if (WARN_ON_ONCE(!sp->role.guest_mode)) in shadow_mmu_try_split_huge_pages()
6838 /* The rmaps should never contain non-leaf SPTEs. */ in shadow_mmu_try_split_huge_pages()
6843 if (WARN_ON_ONCE(sp->unsync)) in shadow_mmu_try_split_huge_pages()
6847 if (sp->role.invalid) in shadow_mmu_try_split_huge_pages()
6857 if (!r || r == -EAGAIN) in shadow_mmu_try_split_huge_pages()
6860 /* The split failed and shouldn't be retried (e.g. -ENOMEM). */ in shadow_mmu_try_split_huge_pages()
6880 for (level = KVM_MAX_HUGEPAGE_LEVEL; level > target_level; level--) in kvm_shadow_mmu_try_split_huge_pages()
6882 level, level, start, end - 1, true, true, false); in kvm_shadow_mmu_try_split_huge_pages()
6885 /* Must be called with the mmu_lock held in write-mode. */
6909 u64 start = memslot->base_gfn; in kvm_mmu_slot_try_split_huge_pages()
6910 u64 end = start + memslot->npages; in kvm_mmu_slot_try_split_huge_pages()
6916 write_lock(&kvm->mmu_lock); in kvm_mmu_slot_try_split_huge_pages()
6918 write_unlock(&kvm->mmu_lock); in kvm_mmu_slot_try_split_huge_pages()
6921 read_lock(&kvm->mmu_lock); in kvm_mmu_slot_try_split_huge_pages()
6923 read_unlock(&kvm->mmu_lock); in kvm_mmu_slot_try_split_huge_pages()
6927 * write-protecting and/or clearing dirty on the newly split SPTEs to in kvm_mmu_slot_try_split_huge_pages()
6956 if (sp->role.direct && in kvm_mmu_zap_collapsible_spte()
6957 sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn, in kvm_mmu_zap_collapsible_spte()
6978 * Note, use KVM_MAX_HUGEPAGE_LEVEL - 1 since there's no need to zap in kvm_rmap_zap_collapsible_sptes()
6982 PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL - 1, true)) in kvm_rmap_zap_collapsible_sptes()
6990 write_lock(&kvm->mmu_lock); in kvm_mmu_zap_collapsible_sptes()
6992 write_unlock(&kvm->mmu_lock); in kvm_mmu_zap_collapsible_sptes()
6996 read_lock(&kvm->mmu_lock); in kvm_mmu_zap_collapsible_sptes()
6998 read_unlock(&kvm->mmu_lock); in kvm_mmu_zap_collapsible_sptes()
7006 write_lock(&kvm->mmu_lock); in kvm_mmu_slot_leaf_clear_dirty()
7012 write_unlock(&kvm->mmu_lock); in kvm_mmu_slot_leaf_clear_dirty()
7016 read_lock(&kvm->mmu_lock); in kvm_mmu_slot_leaf_clear_dirty()
7018 read_unlock(&kvm->mmu_lock); in kvm_mmu_slot_leaf_clear_dirty()
7037 write_lock(&kvm->mmu_lock); in kvm_mmu_zap_all()
7039 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) { in kvm_mmu_zap_all()
7040 if (WARN_ON_ONCE(sp->role.invalid)) in kvm_mmu_zap_all()
7044 if (cond_resched_rwlock_write(&kvm->mmu_lock)) in kvm_mmu_zap_all()
7053 write_unlock(&kvm->mmu_lock); in kvm_mmu_zap_all()
7068 if (list_empty(&kvm->arch.active_mmu_pages)) in kvm_mmu_zap_memslot_pages_and_flush()
7075 * will result in use-after-free, e.g. in unaccount_shadowed(). in kvm_mmu_zap_memslot_pages_and_flush()
7077 for (i = 0; i < slot->npages; i++) { in kvm_mmu_zap_memslot_pages_and_flush()
7079 gfn_t gfn = slot->base_gfn + i; in kvm_mmu_zap_memslot_pages_and_flush()
7084 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) { in kvm_mmu_zap_memslot_pages_and_flush()
7087 cond_resched_rwlock_write(&kvm->mmu_lock); in kvm_mmu_zap_memslot_pages_and_flush()
7100 .start = slot->base_gfn, in kvm_mmu_zap_memslot()
7101 .end = slot->base_gfn + slot->npages, in kvm_mmu_zap_memslot()
7106 write_lock(&kvm->mmu_lock); in kvm_mmu_zap_memslot()
7109 write_unlock(&kvm->mmu_lock); in kvm_mmu_zap_memslot()
7114 return kvm->arch.vm_type == KVM_X86_DEFAULT_VM && in kvm_memslot_flush_zap_all()
7140 gen &= ~((u64)kvm_arch_nr_memslot_as_ids(kvm) - 1); in kvm_mmu_invalidate_mmio_sptes()
7156 int nr_to_scan = sc->nr_to_scan; in mmu_shrink_scan()
7165 * Never scan more than sc->nr_to_scan VM instances. in mmu_shrink_scan()
7170 if (!nr_to_scan--) in mmu_shrink_scan()
7173 * n_used_mmu_pages is accessed without holding kvm->mmu_lock in mmu_shrink_scan()
7178 if (!kvm->arch.n_used_mmu_pages && in mmu_shrink_scan()
7182 idx = srcu_read_lock(&kvm->srcu); in mmu_shrink_scan()
7183 write_lock(&kvm->mmu_lock); in mmu_shrink_scan()
7187 &kvm->arch.zapped_obsolete_pages); in mmu_shrink_scan()
7191 freed = kvm_mmu_zap_oldest_mmu_pages(kvm, sc->nr_to_scan); in mmu_shrink_scan()
7194 write_unlock(&kvm->mmu_lock); in mmu_shrink_scan()
7195 srcu_read_unlock(&kvm->srcu, idx); in mmu_shrink_scan()
7199 * per-vm shrinkers cry out in mmu_shrink_scan()
7202 list_move_tail(&kvm->vm_list, &vm_list); in mmu_shrink_scan()
7249 return -EPERM; in set_nx_huge_pages()
7264 return -EBUSY; in set_nx_huge_pages()
7269 return -EINVAL; in set_nx_huge_pages()
7280 mutex_lock(&kvm->slots_lock); in set_nx_huge_pages()
7282 mutex_unlock(&kvm->slots_lock); in set_nx_huge_pages()
7284 wake_up_process(kvm->arch.nx_huge_page_recovery_thread); in set_nx_huge_pages()
7294 * its default value of -1 is technically undefined behavior for a boolean.
7300 if (nx_huge_pages == -1) in kvm_mmu_x86_module_init()
7320 int ret = -ENOMEM; in kvm_mmu_vendor_module_init()
7347 mmu_shrinker = shrinker_alloc(0, "x86-mmu"); in kvm_mmu_vendor_module_init()
7351 mmu_shrinker->count_objects = mmu_shrink_count; in kvm_mmu_vendor_module_init()
7352 mmu_shrinker->scan_objects = mmu_shrink_scan; in kvm_mmu_vendor_module_init()
7353 mmu_shrinker->seeks = DEFAULT_SEEKS * 10; in kvm_mmu_vendor_module_init()
7369 free_mmu_pages(&vcpu->arch.root_mmu); in kvm_mmu_destroy()
7370 free_mmu_pages(&vcpu->arch.guest_mmu); in kvm_mmu_destroy()
7413 return -EPERM; in set_nx_huge_pages_recovery_param()
7430 wake_up_process(kvm->arch.nx_huge_page_recovery_thread); in set_nx_huge_pages_recovery_param()
7440 unsigned long nx_lpage_splits = kvm->stat.nx_lpage_splits; in kvm_recover_nx_huge_pages()
7449 rcu_idx = srcu_read_lock(&kvm->srcu); in kvm_recover_nx_huge_pages()
7450 write_lock(&kvm->mmu_lock); in kvm_recover_nx_huge_pages()
7461 for ( ; to_zap; --to_zap) { in kvm_recover_nx_huge_pages()
7462 if (list_empty(&kvm->arch.possible_nx_huge_pages)) in kvm_recover_nx_huge_pages()
7472 sp = list_first_entry(&kvm->arch.possible_nx_huge_pages, in kvm_recover_nx_huge_pages()
7475 WARN_ON_ONCE(!sp->nx_huge_page_disallowed); in kvm_recover_nx_huge_pages()
7476 WARN_ON_ONCE(!sp->role.direct); in kvm_recover_nx_huge_pages()
7492 * of kvm->nr_memslots_dirty_logging is not a problem: if it is in kvm_recover_nx_huge_pages()
7499 if (atomic_read(&kvm->nr_memslots_dirty_logging)) { in kvm_recover_nx_huge_pages()
7502 slots = kvm_memslots_for_spte_role(kvm, sp->role); in kvm_recover_nx_huge_pages()
7503 slot = __gfn_to_memslot(slots, sp->gfn); in kvm_recover_nx_huge_pages()
7513 WARN_ON_ONCE(sp->nx_huge_page_disallowed); in kvm_recover_nx_huge_pages()
7515 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) { in kvm_recover_nx_huge_pages()
7519 cond_resched_rwlock_write(&kvm->mmu_lock); in kvm_recover_nx_huge_pages()
7529 write_unlock(&kvm->mmu_lock); in kvm_recover_nx_huge_pages()
7530 srcu_read_unlock(&kvm->srcu, rcu_idx); in kvm_recover_nx_huge_pages()
7540 return enabled ? start_time + msecs_to_jiffies(period) - get_jiffies_64() in get_nx_huge_page_recovery_timeout()
7577 "kvm-nx-lpage-recovery", in kvm_mmu_post_init_vm()
7578 &kvm->arch.nx_huge_page_recovery_thread); in kvm_mmu_post_init_vm()
7580 kthread_unpark(kvm->arch.nx_huge_page_recovery_thread); in kvm_mmu_post_init_vm()
7587 if (kvm->arch.nx_huge_page_recovery_thread) in kvm_mmu_pre_destroy_vm()
7588 kthread_stop(kvm->arch.nx_huge_page_recovery_thread); in kvm_mmu_pre_destroy_vm()
7615 return lpage_info_slot(gfn, slot, level)->disallow_lpage & KVM_LPAGE_MIXED_FLAG; in hugepage_test_mixed()
7621 lpage_info_slot(gfn, slot, level)->disallow_lpage &= ~KVM_LPAGE_MIXED_FLAG; in hugepage_clear_mixed()
7627 lpage_info_slot(gfn, slot, level)->disallow_lpage |= KVM_LPAGE_MIXED_FLAG; in hugepage_set_mixed()
7639 for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { in hugepage_has_attrs()
7640 if (hugepage_test_mixed(slot, gfn, level - 1) || in hugepage_has_attrs()
7650 unsigned long attrs = range->arg.attributes; in kvm_arch_post_set_memory_attributes()
7651 struct kvm_memory_slot *slot = range->slot; in kvm_arch_post_set_memory_attributes()
7654 lockdep_assert_held_write(&kvm->mmu_lock); in kvm_arch_post_set_memory_attributes()
7655 lockdep_assert_held(&kvm->slots_lock); in kvm_arch_post_set_memory_attributes()
7672 gfn_t gfn = gfn_round_for_level(range->start, level); in kvm_arch_post_set_memory_attributes()
7675 if (gfn != range->start || gfn + nr_pages > range->end) { in kvm_arch_post_set_memory_attributes()
7677 * Skip mixed tracking if the aligned gfn isn't covered in kvm_arch_post_set_memory_attributes()
7681 if (gfn >= slot->base_gfn && in kvm_arch_post_set_memory_attributes()
7682 gfn + nr_pages <= slot->base_gfn + slot->npages) { in kvm_arch_post_set_memory_attributes()
7695 for ( ; gfn + nr_pages <= range->end; gfn += nr_pages) in kvm_arch_post_set_memory_attributes()
7703 if (gfn < range->end && in kvm_arch_post_set_memory_attributes()
7704 (gfn + nr_pages) <= (slot->base_gfn + slot->npages)) { in kvm_arch_post_set_memory_attributes()
7724 * Don't bother tracking mixed attributes for pages that can't in kvm_mmu_init_memslot_memory_attributes()
7728 gfn_t end = gfn_round_for_level(slot->base_gfn + slot->npages, level); in kvm_mmu_init_memslot_memory_attributes()
7729 gfn_t start = gfn_round_for_level(slot->base_gfn, level); in kvm_mmu_init_memslot_memory_attributes()
7733 if (start < slot->base_gfn) in kvm_mmu_init_memslot_memory_attributes()
7738 * be manually checked as the attributes may already be mixed. in kvm_mmu_init_memslot_memory_attributes()