2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
9 * Copyright (C) 2006 Qumranet, Inc.
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
23 #include <linux/kvm_host.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
27 #include <linux/highmem.h>
28 #include <linux/module.h>
29 #include <linux/swap.h>
30 #include <linux/hugetlb.h>
31 #include <linux/compiler.h>
34 #include <asm/cmpxchg.h>
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
44 bool tdp_enabled = false;
51 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
53 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
58 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
63 #define pgprintk(x...) do { } while (0)
64 #define rmap_printk(x...) do { } while (0)
68 #if defined(MMU_DEBUG) || defined(AUDIT)
70 module_param(dbg, bool, 0644);
74 #define ASSERT(x) do { } while (0)
78 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
79 __FILE__, __LINE__, #x); \
83 #define PT_FIRST_AVAIL_BITS_SHIFT 9
84 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
86 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
88 #define PT64_LEVEL_BITS 9
90 #define PT64_LEVEL_SHIFT(level) \
91 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
93 #define PT64_LEVEL_MASK(level) \
94 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
96 #define PT64_INDEX(address, level)\
97 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
100 #define PT32_LEVEL_BITS 10
102 #define PT32_LEVEL_SHIFT(level) \
103 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
105 #define PT32_LEVEL_MASK(level) \
106 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
108 #define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
112 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
113 #define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
116 #define PT32_BASE_ADDR_MASK PAGE_MASK
117 #define PT32_DIR_BASE_ADDR_MASK \
118 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
120 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
123 #define PFERR_PRESENT_MASK (1U << 0)
124 #define PFERR_WRITE_MASK (1U << 1)
125 #define PFERR_USER_MASK (1U << 2)
126 #define PFERR_FETCH_MASK (1U << 4)
128 #define PT_DIRECTORY_LEVEL 2
129 #define PT_PAGE_TABLE_LEVEL 1
133 #define ACC_EXEC_MASK 1
134 #define ACC_WRITE_MASK PT_WRITABLE_MASK
135 #define ACC_USER_MASK PT_USER_MASK
136 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
138 struct kvm_pv_mmu_op_buffer {
142 char buf[512] __aligned(sizeof(long));
145 struct kvm_rmap_desc {
146 u64 *shadow_ptes[RMAP_EXT];
147 struct kvm_rmap_desc *more;
150 static struct kmem_cache *pte_chain_cache;
151 static struct kmem_cache *rmap_desc_cache;
152 static struct kmem_cache *mmu_page_header_cache;
154 static u64 __read_mostly shadow_trap_nonpresent_pte;
155 static u64 __read_mostly shadow_notrap_nonpresent_pte;
156 static u64 __read_mostly shadow_base_present_pte;
157 static u64 __read_mostly shadow_nx_mask;
158 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
159 static u64 __read_mostly shadow_user_mask;
160 static u64 __read_mostly shadow_accessed_mask;
161 static u64 __read_mostly shadow_dirty_mask;
163 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
165 shadow_trap_nonpresent_pte = trap_pte;
166 shadow_notrap_nonpresent_pte = notrap_pte;
168 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
170 void kvm_mmu_set_base_ptes(u64 base_pte)
172 shadow_base_present_pte = base_pte;
174 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
176 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
177 u64 dirty_mask, u64 nx_mask, u64 x_mask)
179 shadow_user_mask = user_mask;
180 shadow_accessed_mask = accessed_mask;
181 shadow_dirty_mask = dirty_mask;
182 shadow_nx_mask = nx_mask;
183 shadow_x_mask = x_mask;
185 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
187 static int is_write_protection(struct kvm_vcpu *vcpu)
189 return vcpu->arch.cr0 & X86_CR0_WP;
192 static int is_cpuid_PSE36(void)
197 static int is_nx(struct kvm_vcpu *vcpu)
199 return vcpu->arch.shadow_efer & EFER_NX;
202 static int is_present_pte(unsigned long pte)
204 return pte & PT_PRESENT_MASK;
207 static int is_shadow_present_pte(u64 pte)
209 return pte != shadow_trap_nonpresent_pte
210 && pte != shadow_notrap_nonpresent_pte;
213 static int is_large_pte(u64 pte)
215 return pte & PT_PAGE_SIZE_MASK;
218 static int is_writeble_pte(unsigned long pte)
220 return pte & PT_WRITABLE_MASK;
223 static int is_dirty_pte(unsigned long pte)
225 return pte & shadow_dirty_mask;
228 static int is_rmap_pte(u64 pte)
230 return is_shadow_present_pte(pte);
233 static pfn_t spte_to_pfn(u64 pte)
235 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
238 static gfn_t pse36_gfn_delta(u32 gpte)
240 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
242 return (gpte & PT32_DIR_PSE36_MASK) << shift;
245 static void set_shadow_pte(u64 *sptep, u64 spte)
248 set_64bit((unsigned long *)sptep, spte);
250 set_64bit((unsigned long long *)sptep, spte);
254 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
255 struct kmem_cache *base_cache, int min)
259 if (cache->nobjs >= min)
261 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
262 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
265 cache->objects[cache->nobjs++] = obj;
270 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
273 kfree(mc->objects[--mc->nobjs]);
276 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
281 if (cache->nobjs >= min)
283 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
284 page = alloc_page(GFP_KERNEL);
287 set_page_private(page, 0);
288 cache->objects[cache->nobjs++] = page_address(page);
293 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
296 free_page((unsigned long)mc->objects[--mc->nobjs]);
299 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
303 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
307 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
311 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
314 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
315 mmu_page_header_cache, 4);
320 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
322 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
323 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
324 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
325 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
328 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
334 p = mc->objects[--mc->nobjs];
339 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
341 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
342 sizeof(struct kvm_pte_chain));
345 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
350 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
352 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
353 sizeof(struct kvm_rmap_desc));
356 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
362 * Return the pointer to the largepage write count for a given
363 * gfn, handling slots that are not large page aligned.
365 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
369 idx = (gfn / KVM_PAGES_PER_HPAGE) -
370 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
371 return &slot->lpage_info[idx].write_count;
374 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
378 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
382 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
386 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
388 WARN_ON(*write_count < 0);
391 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
393 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
397 largepage_idx = slot_largepage_idx(gfn, slot);
398 return *largepage_idx;
404 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
406 struct vm_area_struct *vma;
409 addr = gfn_to_hva(kvm, gfn);
410 if (kvm_is_error_hva(addr))
413 vma = find_vma(current->mm, addr);
414 if (vma && is_vm_hugetlb_page(vma))
420 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
422 struct kvm_memory_slot *slot;
424 if (has_wrprotected_page(vcpu->kvm, large_gfn))
427 if (!host_largepage_backed(vcpu->kvm, large_gfn))
430 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
431 if (slot && slot->dirty_bitmap)
438 * Take gfn and return the reverse mapping to it.
439 * Note: gfn must be unaliased before this function get called
442 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
444 struct kvm_memory_slot *slot;
447 slot = gfn_to_memslot(kvm, gfn);
449 return &slot->rmap[gfn - slot->base_gfn];
451 idx = (gfn / KVM_PAGES_PER_HPAGE) -
452 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
454 return &slot->lpage_info[idx].rmap_pde;
458 * Reverse mapping data structures:
460 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
461 * that points to page_address(page).
463 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
464 * containing more mappings.
466 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
468 struct kvm_mmu_page *sp;
469 struct kvm_rmap_desc *desc;
470 unsigned long *rmapp;
473 if (!is_rmap_pte(*spte))
475 gfn = unalias_gfn(vcpu->kvm, gfn);
476 sp = page_header(__pa(spte));
477 sp->gfns[spte - sp->spt] = gfn;
478 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
480 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
481 *rmapp = (unsigned long)spte;
482 } else if (!(*rmapp & 1)) {
483 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
484 desc = mmu_alloc_rmap_desc(vcpu);
485 desc->shadow_ptes[0] = (u64 *)*rmapp;
486 desc->shadow_ptes[1] = spte;
487 *rmapp = (unsigned long)desc | 1;
489 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
490 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
491 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
493 if (desc->shadow_ptes[RMAP_EXT-1]) {
494 desc->more = mmu_alloc_rmap_desc(vcpu);
497 for (i = 0; desc->shadow_ptes[i]; ++i)
499 desc->shadow_ptes[i] = spte;
503 static void rmap_desc_remove_entry(unsigned long *rmapp,
504 struct kvm_rmap_desc *desc,
506 struct kvm_rmap_desc *prev_desc)
510 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
512 desc->shadow_ptes[i] = desc->shadow_ptes[j];
513 desc->shadow_ptes[j] = NULL;
516 if (!prev_desc && !desc->more)
517 *rmapp = (unsigned long)desc->shadow_ptes[0];
520 prev_desc->more = desc->more;
522 *rmapp = (unsigned long)desc->more | 1;
523 mmu_free_rmap_desc(desc);
526 static void rmap_remove(struct kvm *kvm, u64 *spte)
528 struct kvm_rmap_desc *desc;
529 struct kvm_rmap_desc *prev_desc;
530 struct kvm_mmu_page *sp;
532 unsigned long *rmapp;
535 if (!is_rmap_pte(*spte))
537 sp = page_header(__pa(spte));
538 pfn = spte_to_pfn(*spte);
539 if (*spte & shadow_accessed_mask)
540 kvm_set_pfn_accessed(pfn);
541 if (is_writeble_pte(*spte))
542 kvm_release_pfn_dirty(pfn);
544 kvm_release_pfn_clean(pfn);
545 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
547 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
549 } else if (!(*rmapp & 1)) {
550 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
551 if ((u64 *)*rmapp != spte) {
552 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
558 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
559 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
562 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
563 if (desc->shadow_ptes[i] == spte) {
564 rmap_desc_remove_entry(rmapp,
576 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
578 struct kvm_rmap_desc *desc;
579 struct kvm_rmap_desc *prev_desc;
585 else if (!(*rmapp & 1)) {
587 return (u64 *)*rmapp;
590 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
594 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
595 if (prev_spte == spte)
596 return desc->shadow_ptes[i];
597 prev_spte = desc->shadow_ptes[i];
604 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
606 unsigned long *rmapp;
608 int write_protected = 0;
610 gfn = unalias_gfn(kvm, gfn);
611 rmapp = gfn_to_rmap(kvm, gfn, 0);
613 spte = rmap_next(kvm, rmapp, NULL);
616 BUG_ON(!(*spte & PT_PRESENT_MASK));
617 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
618 if (is_writeble_pte(*spte)) {
619 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
622 spte = rmap_next(kvm, rmapp, spte);
624 if (write_protected) {
627 spte = rmap_next(kvm, rmapp, NULL);
628 pfn = spte_to_pfn(*spte);
629 kvm_set_pfn_dirty(pfn);
632 /* check for huge page mappings */
633 rmapp = gfn_to_rmap(kvm, gfn, 1);
634 spte = rmap_next(kvm, rmapp, NULL);
637 BUG_ON(!(*spte & PT_PRESENT_MASK));
638 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
639 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
640 if (is_writeble_pte(*spte)) {
641 rmap_remove(kvm, spte);
643 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
647 spte = rmap_next(kvm, rmapp, spte);
651 kvm_flush_remote_tlbs(kvm);
653 account_shadowed(kvm, gfn);
657 static int is_empty_shadow_page(u64 *spt)
662 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
663 if (is_shadow_present_pte(*pos)) {
664 printk(KERN_ERR "%s: %p %llx\n", __func__,
672 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
674 ASSERT(is_empty_shadow_page(sp->spt));
676 __free_page(virt_to_page(sp->spt));
677 __free_page(virt_to_page(sp->gfns));
679 ++kvm->arch.n_free_mmu_pages;
682 static unsigned kvm_page_table_hashfn(gfn_t gfn)
684 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
687 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
690 struct kvm_mmu_page *sp;
692 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
693 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
694 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
695 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
696 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
697 ASSERT(is_empty_shadow_page(sp->spt));
700 sp->parent_pte = parent_pte;
701 --vcpu->kvm->arch.n_free_mmu_pages;
705 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
706 struct kvm_mmu_page *sp, u64 *parent_pte)
708 struct kvm_pte_chain *pte_chain;
709 struct hlist_node *node;
714 if (!sp->multimapped) {
715 u64 *old = sp->parent_pte;
718 sp->parent_pte = parent_pte;
722 pte_chain = mmu_alloc_pte_chain(vcpu);
723 INIT_HLIST_HEAD(&sp->parent_ptes);
724 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
725 pte_chain->parent_ptes[0] = old;
727 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
728 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
730 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
731 if (!pte_chain->parent_ptes[i]) {
732 pte_chain->parent_ptes[i] = parent_pte;
736 pte_chain = mmu_alloc_pte_chain(vcpu);
738 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
739 pte_chain->parent_ptes[0] = parent_pte;
742 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
745 struct kvm_pte_chain *pte_chain;
746 struct hlist_node *node;
749 if (!sp->multimapped) {
750 BUG_ON(sp->parent_pte != parent_pte);
751 sp->parent_pte = NULL;
754 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
755 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
756 if (!pte_chain->parent_ptes[i])
758 if (pte_chain->parent_ptes[i] != parent_pte)
760 while (i + 1 < NR_PTE_CHAIN_ENTRIES
761 && pte_chain->parent_ptes[i + 1]) {
762 pte_chain->parent_ptes[i]
763 = pte_chain->parent_ptes[i + 1];
766 pte_chain->parent_ptes[i] = NULL;
768 hlist_del(&pte_chain->link);
769 mmu_free_pte_chain(pte_chain);
770 if (hlist_empty(&sp->parent_ptes)) {
772 sp->parent_pte = NULL;
780 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
781 struct kvm_mmu_page *sp)
785 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
786 sp->spt[i] = shadow_trap_nonpresent_pte;
789 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
792 struct hlist_head *bucket;
793 struct kvm_mmu_page *sp;
794 struct hlist_node *node;
796 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
797 index = kvm_page_table_hashfn(gfn);
798 bucket = &kvm->arch.mmu_page_hash[index];
799 hlist_for_each_entry(sp, node, bucket, hash_link)
800 if (sp->gfn == gfn && !sp->role.metaphysical
801 && !sp->role.invalid) {
802 pgprintk("%s: found role %x\n",
803 __func__, sp->role.word);
809 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
817 union kvm_mmu_page_role role;
820 struct hlist_head *bucket;
821 struct kvm_mmu_page *sp;
822 struct hlist_node *node;
825 role.glevels = vcpu->arch.mmu.root_level;
827 role.metaphysical = metaphysical;
828 role.access = access;
829 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
830 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
831 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
832 role.quadrant = quadrant;
834 pgprintk("%s: looking gfn %lx role %x\n", __func__,
836 index = kvm_page_table_hashfn(gfn);
837 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
838 hlist_for_each_entry(sp, node, bucket, hash_link)
839 if (sp->gfn == gfn && sp->role.word == role.word) {
840 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
841 pgprintk("%s: found\n", __func__);
844 ++vcpu->kvm->stat.mmu_cache_miss;
845 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
848 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
851 hlist_add_head(&sp->hash_link, bucket);
853 rmap_write_protect(vcpu->kvm, gfn);
854 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
855 vcpu->arch.mmu.prefetch_page(vcpu, sp);
857 nonpaging_prefetch_page(vcpu, sp);
861 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
862 struct kvm_mmu_page *sp)
870 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
871 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
872 if (is_shadow_present_pte(pt[i]))
873 rmap_remove(kvm, &pt[i]);
874 pt[i] = shadow_trap_nonpresent_pte;
876 kvm_flush_remote_tlbs(kvm);
880 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
883 if (is_shadow_present_pte(ent)) {
884 if (!is_large_pte(ent)) {
885 ent &= PT64_BASE_ADDR_MASK;
886 mmu_page_remove_parent_pte(page_header(ent),
890 rmap_remove(kvm, &pt[i]);
893 pt[i] = shadow_trap_nonpresent_pte;
895 kvm_flush_remote_tlbs(kvm);
898 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
900 mmu_page_remove_parent_pte(sp, parent_pte);
903 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
907 for (i = 0; i < KVM_MAX_VCPUS; ++i)
909 kvm->vcpus[i]->arch.last_pte_updated = NULL;
912 static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
916 ++kvm->stat.mmu_shadow_zapped;
917 while (sp->multimapped || sp->parent_pte) {
918 if (!sp->multimapped)
919 parent_pte = sp->parent_pte;
921 struct kvm_pte_chain *chain;
923 chain = container_of(sp->parent_ptes.first,
924 struct kvm_pte_chain, link);
925 parent_pte = chain->parent_ptes[0];
928 kvm_mmu_put_page(sp, parent_pte);
929 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
931 kvm_mmu_page_unlink_children(kvm, sp);
932 if (!sp->root_count) {
933 if (!sp->role.metaphysical)
934 unaccount_shadowed(kvm, sp->gfn);
935 hlist_del(&sp->hash_link);
936 kvm_mmu_free_page(kvm, sp);
938 list_move(&sp->link, &kvm->arch.active_mmu_pages);
939 sp->role.invalid = 1;
940 kvm_reload_remote_mmus(kvm);
942 kvm_mmu_reset_last_pte_updated(kvm);
946 * Changing the number of mmu pages allocated to the vm
947 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
949 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
952 * If we set the number of mmu pages to be smaller be than the
953 * number of actived pages , we must to free some mmu pages before we
957 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
959 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
960 - kvm->arch.n_free_mmu_pages;
962 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
963 struct kvm_mmu_page *page;
965 page = container_of(kvm->arch.active_mmu_pages.prev,
966 struct kvm_mmu_page, link);
967 kvm_mmu_zap_page(kvm, page);
970 kvm->arch.n_free_mmu_pages = 0;
973 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
974 - kvm->arch.n_alloc_mmu_pages;
976 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
979 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
982 struct hlist_head *bucket;
983 struct kvm_mmu_page *sp;
984 struct hlist_node *node, *n;
987 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
989 index = kvm_page_table_hashfn(gfn);
990 bucket = &kvm->arch.mmu_page_hash[index];
991 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
992 if (sp->gfn == gfn && !sp->role.metaphysical) {
993 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
995 kvm_mmu_zap_page(kvm, sp);
1001 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1003 struct kvm_mmu_page *sp;
1005 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
1006 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
1007 kvm_mmu_zap_page(kvm, sp);
1011 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1013 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1014 struct kvm_mmu_page *sp = page_header(__pa(pte));
1016 __set_bit(slot, &sp->slot_bitmap);
1019 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1023 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1025 if (gpa == UNMAPPED_GVA)
1028 down_read(¤t->mm->mmap_sem);
1029 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1030 up_read(¤t->mm->mmap_sem);
1035 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1036 unsigned pt_access, unsigned pte_access,
1037 int user_fault, int write_fault, int dirty,
1038 int *ptwrite, int largepage, gfn_t gfn,
1039 pfn_t pfn, bool speculative)
1042 int was_rmapped = 0;
1043 int was_writeble = is_writeble_pte(*shadow_pte);
1045 pgprintk("%s: spte %llx access %x write_fault %d"
1046 " user_fault %d gfn %lx\n",
1047 __func__, *shadow_pte, pt_access,
1048 write_fault, user_fault, gfn);
1050 if (is_rmap_pte(*shadow_pte)) {
1052 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1053 * the parent of the now unreachable PTE.
1055 if (largepage && !is_large_pte(*shadow_pte)) {
1056 struct kvm_mmu_page *child;
1057 u64 pte = *shadow_pte;
1059 child = page_header(pte & PT64_BASE_ADDR_MASK);
1060 mmu_page_remove_parent_pte(child, shadow_pte);
1061 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1062 pgprintk("hfn old %lx new %lx\n",
1063 spte_to_pfn(*shadow_pte), pfn);
1064 rmap_remove(vcpu->kvm, shadow_pte);
1067 was_rmapped = is_large_pte(*shadow_pte);
1074 * We don't set the accessed bit, since we sometimes want to see
1075 * whether the guest actually used the pte (in order to detect
1078 spte = shadow_base_present_pte | shadow_dirty_mask;
1080 pte_access |= PT_ACCESSED_MASK;
1082 pte_access &= ~ACC_WRITE_MASK;
1083 if (pte_access & ACC_EXEC_MASK)
1084 spte |= shadow_x_mask;
1086 spte |= shadow_nx_mask;
1087 if (pte_access & ACC_USER_MASK)
1088 spte |= shadow_user_mask;
1090 spte |= PT_PAGE_SIZE_MASK;
1092 spte |= (u64)pfn << PAGE_SHIFT;
1094 if ((pte_access & ACC_WRITE_MASK)
1095 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1096 struct kvm_mmu_page *shadow;
1098 spte |= PT_WRITABLE_MASK;
1100 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1102 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
1103 pgprintk("%s: found shadow page for %lx, marking ro\n",
1105 pte_access &= ~ACC_WRITE_MASK;
1106 if (is_writeble_pte(spte)) {
1107 spte &= ~PT_WRITABLE_MASK;
1108 kvm_x86_ops->tlb_flush(vcpu);
1115 if (pte_access & ACC_WRITE_MASK)
1116 mark_page_dirty(vcpu->kvm, gfn);
1118 pgprintk("%s: setting spte %llx\n", __func__, spte);
1119 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1120 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1121 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
1122 set_shadow_pte(shadow_pte, spte);
1123 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1124 && (spte & PT_PRESENT_MASK))
1125 ++vcpu->kvm->stat.lpages;
1127 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1129 rmap_add(vcpu, shadow_pte, gfn, largepage);
1130 if (!is_rmap_pte(*shadow_pte))
1131 kvm_release_pfn_clean(pfn);
1134 kvm_release_pfn_dirty(pfn);
1136 kvm_release_pfn_clean(pfn);
1139 vcpu->arch.last_pte_updated = shadow_pte;
1140 vcpu->arch.last_pte_gfn = gfn;
1144 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1148 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1149 int largepage, gfn_t gfn, pfn_t pfn,
1152 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
1156 u32 index = PT64_INDEX(v, level);
1159 ASSERT(VALID_PAGE(table_addr));
1160 table = __va(table_addr);
1163 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1164 0, write, 1, &pt_write, 0, gfn, pfn, false);
1168 if (largepage && level == 2) {
1169 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1170 0, write, 1, &pt_write, 1, gfn, pfn, false);
1174 if (table[index] == shadow_trap_nonpresent_pte) {
1175 struct kvm_mmu_page *new_table;
1178 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1180 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1182 1, ACC_ALL, &table[index]);
1184 pgprintk("nonpaging_map: ENOMEM\n");
1185 kvm_release_pfn_clean(pfn);
1189 table[index] = __pa(new_table->spt)
1190 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1191 | shadow_user_mask | shadow_x_mask;
1193 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1197 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1203 down_read(¤t->mm->mmap_sem);
1204 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1205 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1209 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1210 up_read(¤t->mm->mmap_sem);
1213 if (is_error_pfn(pfn)) {
1214 kvm_release_pfn_clean(pfn);
1218 spin_lock(&vcpu->kvm->mmu_lock);
1219 kvm_mmu_free_some_pages(vcpu);
1220 r = __direct_map(vcpu, v, write, largepage, gfn, pfn,
1222 spin_unlock(&vcpu->kvm->mmu_lock);
1229 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1232 struct kvm_mmu_page *sp;
1234 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1236 spin_lock(&vcpu->kvm->mmu_lock);
1237 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1238 hpa_t root = vcpu->arch.mmu.root_hpa;
1240 sp = page_header(root);
1242 if (!sp->root_count && sp->role.invalid)
1243 kvm_mmu_zap_page(vcpu->kvm, sp);
1244 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1245 spin_unlock(&vcpu->kvm->mmu_lock);
1248 for (i = 0; i < 4; ++i) {
1249 hpa_t root = vcpu->arch.mmu.pae_root[i];
1252 root &= PT64_BASE_ADDR_MASK;
1253 sp = page_header(root);
1255 if (!sp->root_count && sp->role.invalid)
1256 kvm_mmu_zap_page(vcpu->kvm, sp);
1258 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1260 spin_unlock(&vcpu->kvm->mmu_lock);
1261 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1264 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1268 struct kvm_mmu_page *sp;
1269 int metaphysical = 0;
1271 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1273 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1274 hpa_t root = vcpu->arch.mmu.root_hpa;
1276 ASSERT(!VALID_PAGE(root));
1279 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1280 PT64_ROOT_LEVEL, metaphysical,
1282 root = __pa(sp->spt);
1284 vcpu->arch.mmu.root_hpa = root;
1287 metaphysical = !is_paging(vcpu);
1290 for (i = 0; i < 4; ++i) {
1291 hpa_t root = vcpu->arch.mmu.pae_root[i];
1293 ASSERT(!VALID_PAGE(root));
1294 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1295 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1296 vcpu->arch.mmu.pae_root[i] = 0;
1299 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1300 } else if (vcpu->arch.mmu.root_level == 0)
1302 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1303 PT32_ROOT_LEVEL, metaphysical,
1305 root = __pa(sp->spt);
1307 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1309 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1312 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1317 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1323 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
1324 r = mmu_topup_memory_caches(vcpu);
1329 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1331 gfn = gva >> PAGE_SHIFT;
1333 return nonpaging_map(vcpu, gva & PAGE_MASK,
1334 error_code & PFERR_WRITE_MASK, gfn);
1337 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1343 gfn_t gfn = gpa >> PAGE_SHIFT;
1346 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1348 r = mmu_topup_memory_caches(vcpu);
1352 down_read(¤t->mm->mmap_sem);
1353 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1354 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1357 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1358 up_read(¤t->mm->mmap_sem);
1359 if (is_error_pfn(pfn)) {
1360 kvm_release_pfn_clean(pfn);
1363 spin_lock(&vcpu->kvm->mmu_lock);
1364 kvm_mmu_free_some_pages(vcpu);
1365 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1366 largepage, gfn, pfn, kvm_x86_ops->get_tdp_level());
1367 spin_unlock(&vcpu->kvm->mmu_lock);
1372 static void nonpaging_free(struct kvm_vcpu *vcpu)
1374 mmu_free_roots(vcpu);
1377 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1379 struct kvm_mmu *context = &vcpu->arch.mmu;
1381 context->new_cr3 = nonpaging_new_cr3;
1382 context->page_fault = nonpaging_page_fault;
1383 context->gva_to_gpa = nonpaging_gva_to_gpa;
1384 context->free = nonpaging_free;
1385 context->prefetch_page = nonpaging_prefetch_page;
1386 context->root_level = 0;
1387 context->shadow_root_level = PT32E_ROOT_LEVEL;
1388 context->root_hpa = INVALID_PAGE;
1392 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1394 ++vcpu->stat.tlb_flush;
1395 kvm_x86_ops->tlb_flush(vcpu);
1398 static void paging_new_cr3(struct kvm_vcpu *vcpu)
1400 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
1401 mmu_free_roots(vcpu);
1404 static void inject_page_fault(struct kvm_vcpu *vcpu,
1408 kvm_inject_page_fault(vcpu, addr, err_code);
1411 static void paging_free(struct kvm_vcpu *vcpu)
1413 nonpaging_free(vcpu);
1417 #include "paging_tmpl.h"
1421 #include "paging_tmpl.h"
1424 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1426 struct kvm_mmu *context = &vcpu->arch.mmu;
1428 ASSERT(is_pae(vcpu));
1429 context->new_cr3 = paging_new_cr3;
1430 context->page_fault = paging64_page_fault;
1431 context->gva_to_gpa = paging64_gva_to_gpa;
1432 context->prefetch_page = paging64_prefetch_page;
1433 context->free = paging_free;
1434 context->root_level = level;
1435 context->shadow_root_level = level;
1436 context->root_hpa = INVALID_PAGE;
1440 static int paging64_init_context(struct kvm_vcpu *vcpu)
1442 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1445 static int paging32_init_context(struct kvm_vcpu *vcpu)
1447 struct kvm_mmu *context = &vcpu->arch.mmu;
1449 context->new_cr3 = paging_new_cr3;
1450 context->page_fault = paging32_page_fault;
1451 context->gva_to_gpa = paging32_gva_to_gpa;
1452 context->free = paging_free;
1453 context->prefetch_page = paging32_prefetch_page;
1454 context->root_level = PT32_ROOT_LEVEL;
1455 context->shadow_root_level = PT32E_ROOT_LEVEL;
1456 context->root_hpa = INVALID_PAGE;
1460 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1462 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1465 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1467 struct kvm_mmu *context = &vcpu->arch.mmu;
1469 context->new_cr3 = nonpaging_new_cr3;
1470 context->page_fault = tdp_page_fault;
1471 context->free = nonpaging_free;
1472 context->prefetch_page = nonpaging_prefetch_page;
1473 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
1474 context->root_hpa = INVALID_PAGE;
1476 if (!is_paging(vcpu)) {
1477 context->gva_to_gpa = nonpaging_gva_to_gpa;
1478 context->root_level = 0;
1479 } else if (is_long_mode(vcpu)) {
1480 context->gva_to_gpa = paging64_gva_to_gpa;
1481 context->root_level = PT64_ROOT_LEVEL;
1482 } else if (is_pae(vcpu)) {
1483 context->gva_to_gpa = paging64_gva_to_gpa;
1484 context->root_level = PT32E_ROOT_LEVEL;
1486 context->gva_to_gpa = paging32_gva_to_gpa;
1487 context->root_level = PT32_ROOT_LEVEL;
1493 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
1496 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1498 if (!is_paging(vcpu))
1499 return nonpaging_init_context(vcpu);
1500 else if (is_long_mode(vcpu))
1501 return paging64_init_context(vcpu);
1502 else if (is_pae(vcpu))
1503 return paging32E_init_context(vcpu);
1505 return paging32_init_context(vcpu);
1508 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1510 vcpu->arch.update_pte.pfn = bad_pfn;
1513 return init_kvm_tdp_mmu(vcpu);
1515 return init_kvm_softmmu(vcpu);
1518 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1521 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1522 vcpu->arch.mmu.free(vcpu);
1523 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1527 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1529 destroy_kvm_mmu(vcpu);
1530 return init_kvm_mmu(vcpu);
1532 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
1534 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1538 r = mmu_topup_memory_caches(vcpu);
1541 spin_lock(&vcpu->kvm->mmu_lock);
1542 kvm_mmu_free_some_pages(vcpu);
1543 mmu_alloc_roots(vcpu);
1544 spin_unlock(&vcpu->kvm->mmu_lock);
1545 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
1546 kvm_mmu_flush_tlb(vcpu);
1550 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1552 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1554 mmu_free_roots(vcpu);
1557 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1558 struct kvm_mmu_page *sp,
1562 struct kvm_mmu_page *child;
1565 if (is_shadow_present_pte(pte)) {
1566 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1568 rmap_remove(vcpu->kvm, spte);
1570 child = page_header(pte & PT64_BASE_ADDR_MASK);
1571 mmu_page_remove_parent_pte(child, spte);
1574 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
1575 if (is_large_pte(pte))
1576 --vcpu->kvm->stat.lpages;
1579 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1580 struct kvm_mmu_page *sp,
1584 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1585 if (!vcpu->arch.update_pte.largepage ||
1586 sp->role.glevels == PT32_ROOT_LEVEL) {
1587 ++vcpu->kvm->stat.mmu_pde_zapped;
1592 ++vcpu->kvm->stat.mmu_pte_updated;
1593 if (sp->role.glevels == PT32_ROOT_LEVEL)
1594 paging32_update_pte(vcpu, sp, spte, new);
1596 paging64_update_pte(vcpu, sp, spte, new);
1599 static bool need_remote_flush(u64 old, u64 new)
1601 if (!is_shadow_present_pte(old))
1603 if (!is_shadow_present_pte(new))
1605 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1607 old ^= PT64_NX_MASK;
1608 new ^= PT64_NX_MASK;
1609 return (old & ~new & PT64_PERM_MASK) != 0;
1612 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1614 if (need_remote_flush(old, new))
1615 kvm_flush_remote_tlbs(vcpu->kvm);
1617 kvm_mmu_flush_tlb(vcpu);
1620 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1622 u64 *spte = vcpu->arch.last_pte_updated;
1624 return !!(spte && (*spte & shadow_accessed_mask));
1627 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1628 const u8 *new, int bytes)
1635 vcpu->arch.update_pte.largepage = 0;
1637 if (bytes != 4 && bytes != 8)
1641 * Assume that the pte write on a page table of the same type
1642 * as the current vcpu paging mode. This is nearly always true
1643 * (might be false while changing modes). Note it is verified later
1647 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1648 if ((bytes == 4) && (gpa % 4 == 0)) {
1649 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1652 memcpy((void *)&gpte + (gpa % 8), new, 4);
1653 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1654 memcpy((void *)&gpte, new, 8);
1657 if ((bytes == 4) && (gpa % 4 == 0))
1658 memcpy((void *)&gpte, new, 4);
1660 if (!is_present_pte(gpte))
1662 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1664 down_read(¤t->mm->mmap_sem);
1665 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1666 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1667 vcpu->arch.update_pte.largepage = 1;
1669 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1670 up_read(¤t->mm->mmap_sem);
1672 if (is_error_pfn(pfn)) {
1673 kvm_release_pfn_clean(pfn);
1676 vcpu->arch.update_pte.gfn = gfn;
1677 vcpu->arch.update_pte.pfn = pfn;
1680 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1682 u64 *spte = vcpu->arch.last_pte_updated;
1685 && vcpu->arch.last_pte_gfn == gfn
1686 && shadow_accessed_mask
1687 && !(*spte & shadow_accessed_mask)
1688 && is_shadow_present_pte(*spte))
1689 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1692 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1693 const u8 *new, int bytes)
1695 gfn_t gfn = gpa >> PAGE_SHIFT;
1696 struct kvm_mmu_page *sp;
1697 struct hlist_node *node, *n;
1698 struct hlist_head *bucket;
1702 unsigned offset = offset_in_page(gpa);
1704 unsigned page_offset;
1705 unsigned misaligned;
1712 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
1713 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
1714 spin_lock(&vcpu->kvm->mmu_lock);
1715 kvm_mmu_access_page(vcpu, gfn);
1716 kvm_mmu_free_some_pages(vcpu);
1717 ++vcpu->kvm->stat.mmu_pte_write;
1718 kvm_mmu_audit(vcpu, "pre pte write");
1719 if (gfn == vcpu->arch.last_pt_write_gfn
1720 && !last_updated_pte_accessed(vcpu)) {
1721 ++vcpu->arch.last_pt_write_count;
1722 if (vcpu->arch.last_pt_write_count >= 3)
1725 vcpu->arch.last_pt_write_gfn = gfn;
1726 vcpu->arch.last_pt_write_count = 1;
1727 vcpu->arch.last_pte_updated = NULL;
1729 index = kvm_page_table_hashfn(gfn);
1730 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1731 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1732 if (sp->gfn != gfn || sp->role.metaphysical)
1734 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1735 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1736 misaligned |= bytes < 4;
1737 if (misaligned || flooded) {
1739 * Misaligned accesses are too much trouble to fix
1740 * up; also, they usually indicate a page is not used
1743 * If we're seeing too many writes to a page,
1744 * it may no longer be a page table, or we may be
1745 * forking, in which case it is better to unmap the
1748 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1749 gpa, bytes, sp->role.word);
1750 kvm_mmu_zap_page(vcpu->kvm, sp);
1751 ++vcpu->kvm->stat.mmu_flooded;
1754 page_offset = offset;
1755 level = sp->role.level;
1757 if (sp->role.glevels == PT32_ROOT_LEVEL) {
1758 page_offset <<= 1; /* 32->64 */
1760 * A 32-bit pde maps 4MB while the shadow pdes map
1761 * only 2MB. So we need to double the offset again
1762 * and zap two pdes instead of one.
1764 if (level == PT32_ROOT_LEVEL) {
1765 page_offset &= ~7; /* kill rounding error */
1769 quadrant = page_offset >> PAGE_SHIFT;
1770 page_offset &= ~PAGE_MASK;
1771 if (quadrant != sp->role.quadrant)
1774 spte = &sp->spt[page_offset / sizeof(*spte)];
1775 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1777 r = kvm_read_guest_atomic(vcpu->kvm,
1778 gpa & ~(u64)(pte_size - 1),
1780 new = (const void *)&gentry;
1786 mmu_pte_write_zap_pte(vcpu, sp, spte);
1788 mmu_pte_write_new_pte(vcpu, sp, spte, new);
1789 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
1793 kvm_mmu_audit(vcpu, "post pte write");
1794 spin_unlock(&vcpu->kvm->mmu_lock);
1795 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1796 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1797 vcpu->arch.update_pte.pfn = bad_pfn;
1801 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1806 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1808 spin_lock(&vcpu->kvm->mmu_lock);
1809 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1810 spin_unlock(&vcpu->kvm->mmu_lock);
1814 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1816 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
1817 struct kvm_mmu_page *sp;
1819 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
1820 struct kvm_mmu_page, link);
1821 kvm_mmu_zap_page(vcpu->kvm, sp);
1822 ++vcpu->kvm->stat.mmu_recycled;
1826 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1829 enum emulation_result er;
1831 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
1840 r = mmu_topup_memory_caches(vcpu);
1844 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1849 case EMULATE_DO_MMIO:
1850 ++vcpu->stat.mmio_exits;
1853 kvm_report_emulation_failure(vcpu, "pagetable");
1861 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1863 void kvm_enable_tdp(void)
1867 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1869 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1871 struct kvm_mmu_page *sp;
1873 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1874 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
1875 struct kvm_mmu_page, link);
1876 kvm_mmu_zap_page(vcpu->kvm, sp);
1879 free_page((unsigned long)vcpu->arch.mmu.pae_root);
1882 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1889 if (vcpu->kvm->arch.n_requested_mmu_pages)
1890 vcpu->kvm->arch.n_free_mmu_pages =
1891 vcpu->kvm->arch.n_requested_mmu_pages;
1893 vcpu->kvm->arch.n_free_mmu_pages =
1894 vcpu->kvm->arch.n_alloc_mmu_pages;
1896 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1897 * Therefore we need to allocate shadow page tables in the first
1898 * 4GB of memory, which happens to fit the DMA32 zone.
1900 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1903 vcpu->arch.mmu.pae_root = page_address(page);
1904 for (i = 0; i < 4; ++i)
1905 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1910 free_mmu_pages(vcpu);
1914 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1917 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1919 return alloc_mmu_pages(vcpu);
1922 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1925 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1927 return init_kvm_mmu(vcpu);
1930 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1934 destroy_kvm_mmu(vcpu);
1935 free_mmu_pages(vcpu);
1936 mmu_free_memory_caches(vcpu);
1939 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1941 struct kvm_mmu_page *sp;
1943 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
1947 if (!test_bit(slot, &sp->slot_bitmap))
1951 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1953 if (pt[i] & PT_WRITABLE_MASK)
1954 pt[i] &= ~PT_WRITABLE_MASK;
1958 void kvm_mmu_zap_all(struct kvm *kvm)
1960 struct kvm_mmu_page *sp, *node;
1962 spin_lock(&kvm->mmu_lock);
1963 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
1964 kvm_mmu_zap_page(kvm, sp);
1965 spin_unlock(&kvm->mmu_lock);
1967 kvm_flush_remote_tlbs(kvm);
1970 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
1972 struct kvm_mmu_page *page;
1974 page = container_of(kvm->arch.active_mmu_pages.prev,
1975 struct kvm_mmu_page, link);
1976 kvm_mmu_zap_page(kvm, page);
1979 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
1982 struct kvm *kvm_freed = NULL;
1983 int cache_count = 0;
1985 spin_lock(&kvm_lock);
1987 list_for_each_entry(kvm, &vm_list, vm_list) {
1990 if (!down_read_trylock(&kvm->slots_lock))
1992 spin_lock(&kvm->mmu_lock);
1993 npages = kvm->arch.n_alloc_mmu_pages -
1994 kvm->arch.n_free_mmu_pages;
1995 cache_count += npages;
1996 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
1997 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2003 spin_unlock(&kvm->mmu_lock);
2004 up_read(&kvm->slots_lock);
2007 list_move_tail(&kvm_freed->vm_list, &vm_list);
2009 spin_unlock(&kvm_lock);
2014 static struct shrinker mmu_shrinker = {
2015 .shrink = mmu_shrink,
2016 .seeks = DEFAULT_SEEKS * 10,
2019 static void mmu_destroy_caches(void)
2021 if (pte_chain_cache)
2022 kmem_cache_destroy(pte_chain_cache);
2023 if (rmap_desc_cache)
2024 kmem_cache_destroy(rmap_desc_cache);
2025 if (mmu_page_header_cache)
2026 kmem_cache_destroy(mmu_page_header_cache);
2029 void kvm_mmu_module_exit(void)
2031 mmu_destroy_caches();
2032 unregister_shrinker(&mmu_shrinker);
2035 int kvm_mmu_module_init(void)
2037 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2038 sizeof(struct kvm_pte_chain),
2040 if (!pte_chain_cache)
2042 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2043 sizeof(struct kvm_rmap_desc),
2045 if (!rmap_desc_cache)
2048 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2049 sizeof(struct kvm_mmu_page),
2051 if (!mmu_page_header_cache)
2054 register_shrinker(&mmu_shrinker);
2059 mmu_destroy_caches();
2064 * Caculate mmu pages needed for kvm.
2066 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2069 unsigned int nr_mmu_pages;
2070 unsigned int nr_pages = 0;
2072 for (i = 0; i < kvm->nmemslots; i++)
2073 nr_pages += kvm->memslots[i].npages;
2075 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2076 nr_mmu_pages = max(nr_mmu_pages,
2077 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2079 return nr_mmu_pages;
2082 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2085 if (len > buffer->len)
2090 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2095 ret = pv_mmu_peek_buffer(buffer, len);
2100 buffer->processed += len;
2104 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2105 gpa_t addr, gpa_t value)
2110 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2113 r = mmu_topup_memory_caches(vcpu);
2117 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2123 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2125 kvm_x86_ops->tlb_flush(vcpu);
2129 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2131 spin_lock(&vcpu->kvm->mmu_lock);
2132 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2133 spin_unlock(&vcpu->kvm->mmu_lock);
2137 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2138 struct kvm_pv_mmu_op_buffer *buffer)
2140 struct kvm_mmu_op_header *header;
2142 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2145 switch (header->op) {
2146 case KVM_MMU_OP_WRITE_PTE: {
2147 struct kvm_mmu_op_write_pte *wpte;
2149 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2152 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2155 case KVM_MMU_OP_FLUSH_TLB: {
2156 struct kvm_mmu_op_flush_tlb *ftlb;
2158 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2161 return kvm_pv_mmu_flush_tlb(vcpu);
2163 case KVM_MMU_OP_RELEASE_PT: {
2164 struct kvm_mmu_op_release_pt *rpt;
2166 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2169 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2175 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2176 gpa_t addr, unsigned long *ret)
2179 struct kvm_pv_mmu_op_buffer buffer;
2181 buffer.ptr = buffer.buf;
2182 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2183 buffer.processed = 0;
2185 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2189 while (buffer.len) {
2190 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2199 *ret = buffer.processed;
2205 static const char *audit_msg;
2207 static gva_t canonicalize(gva_t gva)
2209 #ifdef CONFIG_X86_64
2210 gva = (long long)(gva << 16) >> 16;
2215 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2216 gva_t va, int level)
2218 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2220 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2222 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2225 if (ent == shadow_trap_nonpresent_pte)
2228 va = canonicalize(va);
2230 if (ent == shadow_notrap_nonpresent_pte)
2231 printk(KERN_ERR "audit: (%s) nontrapping pte"
2232 " in nonleaf level: levels %d gva %lx"
2233 " level %d pte %llx\n", audit_msg,
2234 vcpu->arch.mmu.root_level, va, level, ent);
2236 audit_mappings_page(vcpu, ent, va, level - 1);
2238 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
2239 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
2241 if (is_shadow_present_pte(ent)
2242 && (ent & PT64_BASE_ADDR_MASK) != hpa)
2243 printk(KERN_ERR "xx audit error: (%s) levels %d"
2244 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2245 audit_msg, vcpu->arch.mmu.root_level,
2247 is_shadow_present_pte(ent));
2248 else if (ent == shadow_notrap_nonpresent_pte
2249 && !is_error_hpa(hpa))
2250 printk(KERN_ERR "audit: (%s) notrap shadow,"
2251 " valid guest gva %lx\n", audit_msg, va);
2252 kvm_release_pfn_clean(pfn);
2258 static void audit_mappings(struct kvm_vcpu *vcpu)
2262 if (vcpu->arch.mmu.root_level == 4)
2263 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
2265 for (i = 0; i < 4; ++i)
2266 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
2267 audit_mappings_page(vcpu,
2268 vcpu->arch.mmu.pae_root[i],
2273 static int count_rmaps(struct kvm_vcpu *vcpu)
2278 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2279 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2280 struct kvm_rmap_desc *d;
2282 for (j = 0; j < m->npages; ++j) {
2283 unsigned long *rmapp = &m->rmap[j];
2287 if (!(*rmapp & 1)) {
2291 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
2293 for (k = 0; k < RMAP_EXT; ++k)
2294 if (d->shadow_ptes[k])
2305 static int count_writable_mappings(struct kvm_vcpu *vcpu)
2308 struct kvm_mmu_page *sp;
2311 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2314 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
2317 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2320 if (!(ent & PT_PRESENT_MASK))
2322 if (!(ent & PT_WRITABLE_MASK))
2330 static void audit_rmap(struct kvm_vcpu *vcpu)
2332 int n_rmap = count_rmaps(vcpu);
2333 int n_actual = count_writable_mappings(vcpu);
2335 if (n_rmap != n_actual)
2336 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2337 __func__, audit_msg, n_rmap, n_actual);
2340 static void audit_write_protection(struct kvm_vcpu *vcpu)
2342 struct kvm_mmu_page *sp;
2343 struct kvm_memory_slot *slot;
2344 unsigned long *rmapp;
2347 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2348 if (sp->role.metaphysical)
2351 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2352 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
2353 rmapp = &slot->rmap[gfn - slot->base_gfn];
2355 printk(KERN_ERR "%s: (%s) shadow page has writable"
2356 " mappings: gfn %lx role %x\n",
2357 __func__, audit_msg, sp->gfn,
2362 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2369 audit_write_protection(vcpu);
2370 audit_mappings(vcpu);