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);
656 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
659 int need_tlb_flush = 0;
661 while ((spte = rmap_next(kvm, rmapp, NULL))) {
662 BUG_ON(!(*spte & PT_PRESENT_MASK));
663 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
664 rmap_remove(kvm, spte);
665 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
668 return need_tlb_flush;
671 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
672 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
678 * If mmap_sem isn't taken, we can look the memslots with only
679 * the mmu_lock by skipping over the slots with userspace_addr == 0.
681 for (i = 0; i < kvm->nmemslots; i++) {
682 struct kvm_memory_slot *memslot = &kvm->memslots[i];
683 unsigned long start = memslot->userspace_addr;
686 /* mmu_lock protects userspace_addr */
690 end = start + (memslot->npages << PAGE_SHIFT);
691 if (hva >= start && hva < end) {
692 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
693 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
694 retval |= handler(kvm,
695 &memslot->lpage_info[
697 KVM_PAGES_PER_HPAGE].rmap_pde);
704 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
706 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
709 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
714 spte = rmap_next(kvm, rmapp, NULL);
718 BUG_ON(!(_spte & PT_PRESENT_MASK));
719 _young = _spte & PT_ACCESSED_MASK;
722 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
724 spte = rmap_next(kvm, rmapp, spte);
729 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
731 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
735 static int is_empty_shadow_page(u64 *spt)
740 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
741 if (is_shadow_present_pte(*pos)) {
742 printk(KERN_ERR "%s: %p %llx\n", __func__,
750 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
752 ASSERT(is_empty_shadow_page(sp->spt));
754 __free_page(virt_to_page(sp->spt));
755 __free_page(virt_to_page(sp->gfns));
757 ++kvm->arch.n_free_mmu_pages;
760 static unsigned kvm_page_table_hashfn(gfn_t gfn)
762 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
765 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
768 struct kvm_mmu_page *sp;
770 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
771 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
772 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
773 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
774 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
775 ASSERT(is_empty_shadow_page(sp->spt));
778 sp->parent_pte = parent_pte;
779 --vcpu->kvm->arch.n_free_mmu_pages;
783 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
784 struct kvm_mmu_page *sp, u64 *parent_pte)
786 struct kvm_pte_chain *pte_chain;
787 struct hlist_node *node;
792 if (!sp->multimapped) {
793 u64 *old = sp->parent_pte;
796 sp->parent_pte = parent_pte;
800 pte_chain = mmu_alloc_pte_chain(vcpu);
801 INIT_HLIST_HEAD(&sp->parent_ptes);
802 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
803 pte_chain->parent_ptes[0] = old;
805 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
806 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
808 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
809 if (!pte_chain->parent_ptes[i]) {
810 pte_chain->parent_ptes[i] = parent_pte;
814 pte_chain = mmu_alloc_pte_chain(vcpu);
816 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
817 pte_chain->parent_ptes[0] = parent_pte;
820 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
823 struct kvm_pte_chain *pte_chain;
824 struct hlist_node *node;
827 if (!sp->multimapped) {
828 BUG_ON(sp->parent_pte != parent_pte);
829 sp->parent_pte = NULL;
832 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
833 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
834 if (!pte_chain->parent_ptes[i])
836 if (pte_chain->parent_ptes[i] != parent_pte)
838 while (i + 1 < NR_PTE_CHAIN_ENTRIES
839 && pte_chain->parent_ptes[i + 1]) {
840 pte_chain->parent_ptes[i]
841 = pte_chain->parent_ptes[i + 1];
844 pte_chain->parent_ptes[i] = NULL;
846 hlist_del(&pte_chain->link);
847 mmu_free_pte_chain(pte_chain);
848 if (hlist_empty(&sp->parent_ptes)) {
850 sp->parent_pte = NULL;
858 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
859 struct kvm_mmu_page *sp)
863 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
864 sp->spt[i] = shadow_trap_nonpresent_pte;
867 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
870 struct hlist_head *bucket;
871 struct kvm_mmu_page *sp;
872 struct hlist_node *node;
874 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
875 index = kvm_page_table_hashfn(gfn);
876 bucket = &kvm->arch.mmu_page_hash[index];
877 hlist_for_each_entry(sp, node, bucket, hash_link)
878 if (sp->gfn == gfn && !sp->role.metaphysical
879 && !sp->role.invalid) {
880 pgprintk("%s: found role %x\n",
881 __func__, sp->role.word);
887 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
895 union kvm_mmu_page_role role;
898 struct hlist_head *bucket;
899 struct kvm_mmu_page *sp;
900 struct hlist_node *node;
903 role.glevels = vcpu->arch.mmu.root_level;
905 role.metaphysical = metaphysical;
906 role.access = access;
907 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
908 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
909 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
910 role.quadrant = quadrant;
912 pgprintk("%s: looking gfn %lx role %x\n", __func__,
914 index = kvm_page_table_hashfn(gfn);
915 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
916 hlist_for_each_entry(sp, node, bucket, hash_link)
917 if (sp->gfn == gfn && sp->role.word == role.word) {
918 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
919 pgprintk("%s: found\n", __func__);
922 ++vcpu->kvm->stat.mmu_cache_miss;
923 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
926 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
929 hlist_add_head(&sp->hash_link, bucket);
931 rmap_write_protect(vcpu->kvm, gfn);
932 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
933 vcpu->arch.mmu.prefetch_page(vcpu, sp);
935 nonpaging_prefetch_page(vcpu, sp);
939 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
940 struct kvm_mmu_page *sp)
948 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
949 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
950 if (is_shadow_present_pte(pt[i]))
951 rmap_remove(kvm, &pt[i]);
952 pt[i] = shadow_trap_nonpresent_pte;
954 kvm_flush_remote_tlbs(kvm);
958 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
961 if (is_shadow_present_pte(ent)) {
962 if (!is_large_pte(ent)) {
963 ent &= PT64_BASE_ADDR_MASK;
964 mmu_page_remove_parent_pte(page_header(ent),
968 rmap_remove(kvm, &pt[i]);
971 pt[i] = shadow_trap_nonpresent_pte;
973 kvm_flush_remote_tlbs(kvm);
976 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
978 mmu_page_remove_parent_pte(sp, parent_pte);
981 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
985 for (i = 0; i < KVM_MAX_VCPUS; ++i)
987 kvm->vcpus[i]->arch.last_pte_updated = NULL;
990 static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
994 ++kvm->stat.mmu_shadow_zapped;
995 while (sp->multimapped || sp->parent_pte) {
996 if (!sp->multimapped)
997 parent_pte = sp->parent_pte;
999 struct kvm_pte_chain *chain;
1001 chain = container_of(sp->parent_ptes.first,
1002 struct kvm_pte_chain, link);
1003 parent_pte = chain->parent_ptes[0];
1005 BUG_ON(!parent_pte);
1006 kvm_mmu_put_page(sp, parent_pte);
1007 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1009 kvm_mmu_page_unlink_children(kvm, sp);
1010 if (!sp->root_count) {
1011 if (!sp->role.metaphysical && !sp->role.invalid)
1012 unaccount_shadowed(kvm, sp->gfn);
1013 hlist_del(&sp->hash_link);
1014 kvm_mmu_free_page(kvm, sp);
1016 int invalid = sp->role.invalid;
1017 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1018 sp->role.invalid = 1;
1019 kvm_reload_remote_mmus(kvm);
1020 if (!sp->role.metaphysical && !invalid)
1021 unaccount_shadowed(kvm, sp->gfn);
1023 kvm_mmu_reset_last_pte_updated(kvm);
1027 * Changing the number of mmu pages allocated to the vm
1028 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1030 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1033 * If we set the number of mmu pages to be smaller be than the
1034 * number of actived pages , we must to free some mmu pages before we
1038 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
1040 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1041 - kvm->arch.n_free_mmu_pages;
1043 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1044 struct kvm_mmu_page *page;
1046 page = container_of(kvm->arch.active_mmu_pages.prev,
1047 struct kvm_mmu_page, link);
1048 kvm_mmu_zap_page(kvm, page);
1051 kvm->arch.n_free_mmu_pages = 0;
1054 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1055 - kvm->arch.n_alloc_mmu_pages;
1057 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1060 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1063 struct hlist_head *bucket;
1064 struct kvm_mmu_page *sp;
1065 struct hlist_node *node, *n;
1068 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1070 index = kvm_page_table_hashfn(gfn);
1071 bucket = &kvm->arch.mmu_page_hash[index];
1072 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1073 if (sp->gfn == gfn && !sp->role.metaphysical) {
1074 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1076 kvm_mmu_zap_page(kvm, sp);
1082 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1084 struct kvm_mmu_page *sp;
1086 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
1087 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
1088 kvm_mmu_zap_page(kvm, sp);
1092 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1094 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1095 struct kvm_mmu_page *sp = page_header(__pa(pte));
1097 __set_bit(slot, &sp->slot_bitmap);
1100 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1104 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1106 if (gpa == UNMAPPED_GVA)
1109 down_read(¤t->mm->mmap_sem);
1110 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1111 up_read(¤t->mm->mmap_sem);
1116 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1117 unsigned pt_access, unsigned pte_access,
1118 int user_fault, int write_fault, int dirty,
1119 int *ptwrite, int largepage, gfn_t gfn,
1120 pfn_t pfn, bool speculative)
1123 int was_rmapped = 0;
1124 int was_writeble = is_writeble_pte(*shadow_pte);
1126 pgprintk("%s: spte %llx access %x write_fault %d"
1127 " user_fault %d gfn %lx\n",
1128 __func__, *shadow_pte, pt_access,
1129 write_fault, user_fault, gfn);
1131 if (is_rmap_pte(*shadow_pte)) {
1133 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1134 * the parent of the now unreachable PTE.
1136 if (largepage && !is_large_pte(*shadow_pte)) {
1137 struct kvm_mmu_page *child;
1138 u64 pte = *shadow_pte;
1140 child = page_header(pte & PT64_BASE_ADDR_MASK);
1141 mmu_page_remove_parent_pte(child, shadow_pte);
1142 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1143 pgprintk("hfn old %lx new %lx\n",
1144 spte_to_pfn(*shadow_pte), pfn);
1145 rmap_remove(vcpu->kvm, shadow_pte);
1148 was_rmapped = is_large_pte(*shadow_pte);
1155 * We don't set the accessed bit, since we sometimes want to see
1156 * whether the guest actually used the pte (in order to detect
1159 spte = shadow_base_present_pte | shadow_dirty_mask;
1161 pte_access |= PT_ACCESSED_MASK;
1163 pte_access &= ~ACC_WRITE_MASK;
1164 if (pte_access & ACC_EXEC_MASK)
1165 spte |= shadow_x_mask;
1167 spte |= shadow_nx_mask;
1168 if (pte_access & ACC_USER_MASK)
1169 spte |= shadow_user_mask;
1171 spte |= PT_PAGE_SIZE_MASK;
1173 spte |= (u64)pfn << PAGE_SHIFT;
1175 if ((pte_access & ACC_WRITE_MASK)
1176 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1177 struct kvm_mmu_page *shadow;
1179 spte |= PT_WRITABLE_MASK;
1181 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1183 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
1184 pgprintk("%s: found shadow page for %lx, marking ro\n",
1186 pte_access &= ~ACC_WRITE_MASK;
1187 if (is_writeble_pte(spte)) {
1188 spte &= ~PT_WRITABLE_MASK;
1189 kvm_x86_ops->tlb_flush(vcpu);
1196 if (pte_access & ACC_WRITE_MASK)
1197 mark_page_dirty(vcpu->kvm, gfn);
1199 pgprintk("%s: setting spte %llx\n", __func__, spte);
1200 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1201 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1202 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
1203 set_shadow_pte(shadow_pte, spte);
1204 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1205 && (spte & PT_PRESENT_MASK))
1206 ++vcpu->kvm->stat.lpages;
1208 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1210 rmap_add(vcpu, shadow_pte, gfn, largepage);
1211 if (!is_rmap_pte(*shadow_pte))
1212 kvm_release_pfn_clean(pfn);
1215 kvm_release_pfn_dirty(pfn);
1217 kvm_release_pfn_clean(pfn);
1220 vcpu->arch.last_pte_updated = shadow_pte;
1221 vcpu->arch.last_pte_gfn = gfn;
1225 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1229 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1230 int largepage, gfn_t gfn, pfn_t pfn,
1233 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
1237 u32 index = PT64_INDEX(v, level);
1240 ASSERT(VALID_PAGE(table_addr));
1241 table = __va(table_addr);
1244 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1245 0, write, 1, &pt_write, 0, gfn, pfn, false);
1249 if (largepage && level == 2) {
1250 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1251 0, write, 1, &pt_write, 1, gfn, pfn, false);
1255 if (table[index] == shadow_trap_nonpresent_pte) {
1256 struct kvm_mmu_page *new_table;
1259 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1261 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1263 1, ACC_ALL, &table[index]);
1265 pgprintk("nonpaging_map: ENOMEM\n");
1266 kvm_release_pfn_clean(pfn);
1270 set_shadow_pte(&table[index],
1271 __pa(new_table->spt)
1272 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1273 | shadow_user_mask | shadow_x_mask);
1275 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1279 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1284 unsigned long mmu_seq;
1286 down_read(¤t->mm->mmap_sem);
1287 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1288 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1292 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1293 /* implicit mb(), we'll read before PT lock is unlocked */
1294 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1295 up_read(¤t->mm->mmap_sem);
1298 if (is_error_pfn(pfn)) {
1299 kvm_release_pfn_clean(pfn);
1303 spin_lock(&vcpu->kvm->mmu_lock);
1304 if (mmu_notifier_retry(vcpu, mmu_seq))
1306 kvm_mmu_free_some_pages(vcpu);
1307 r = __direct_map(vcpu, v, write, largepage, gfn, pfn,
1309 spin_unlock(&vcpu->kvm->mmu_lock);
1315 spin_unlock(&vcpu->kvm->mmu_lock);
1316 kvm_release_pfn_clean(pfn);
1321 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1324 struct kvm_mmu_page *sp;
1326 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1328 spin_lock(&vcpu->kvm->mmu_lock);
1329 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1330 hpa_t root = vcpu->arch.mmu.root_hpa;
1332 sp = page_header(root);
1334 if (!sp->root_count && sp->role.invalid)
1335 kvm_mmu_zap_page(vcpu->kvm, sp);
1336 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1337 spin_unlock(&vcpu->kvm->mmu_lock);
1340 for (i = 0; i < 4; ++i) {
1341 hpa_t root = vcpu->arch.mmu.pae_root[i];
1344 root &= PT64_BASE_ADDR_MASK;
1345 sp = page_header(root);
1347 if (!sp->root_count && sp->role.invalid)
1348 kvm_mmu_zap_page(vcpu->kvm, sp);
1350 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1352 spin_unlock(&vcpu->kvm->mmu_lock);
1353 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1356 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1360 struct kvm_mmu_page *sp;
1361 int metaphysical = 0;
1363 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1365 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1366 hpa_t root = vcpu->arch.mmu.root_hpa;
1368 ASSERT(!VALID_PAGE(root));
1371 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1372 PT64_ROOT_LEVEL, metaphysical,
1374 root = __pa(sp->spt);
1376 vcpu->arch.mmu.root_hpa = root;
1379 metaphysical = !is_paging(vcpu);
1382 for (i = 0; i < 4; ++i) {
1383 hpa_t root = vcpu->arch.mmu.pae_root[i];
1385 ASSERT(!VALID_PAGE(root));
1386 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1387 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1388 vcpu->arch.mmu.pae_root[i] = 0;
1391 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1392 } else if (vcpu->arch.mmu.root_level == 0)
1394 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1395 PT32_ROOT_LEVEL, metaphysical,
1397 root = __pa(sp->spt);
1399 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1401 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1404 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1409 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1415 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
1416 r = mmu_topup_memory_caches(vcpu);
1421 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1423 gfn = gva >> PAGE_SHIFT;
1425 return nonpaging_map(vcpu, gva & PAGE_MASK,
1426 error_code & PFERR_WRITE_MASK, gfn);
1429 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1435 gfn_t gfn = gpa >> PAGE_SHIFT;
1436 unsigned long mmu_seq;
1439 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1441 r = mmu_topup_memory_caches(vcpu);
1445 down_read(¤t->mm->mmap_sem);
1446 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1447 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1450 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1451 /* implicit mb(), we'll read before PT lock is unlocked */
1452 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1453 up_read(¤t->mm->mmap_sem);
1454 if (is_error_pfn(pfn)) {
1455 kvm_release_pfn_clean(pfn);
1458 spin_lock(&vcpu->kvm->mmu_lock);
1459 if (mmu_notifier_retry(vcpu, mmu_seq))
1461 kvm_mmu_free_some_pages(vcpu);
1462 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1463 largepage, gfn, pfn, kvm_x86_ops->get_tdp_level());
1464 spin_unlock(&vcpu->kvm->mmu_lock);
1469 spin_unlock(&vcpu->kvm->mmu_lock);
1470 kvm_release_pfn_clean(pfn);
1474 static void nonpaging_free(struct kvm_vcpu *vcpu)
1476 mmu_free_roots(vcpu);
1479 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1481 struct kvm_mmu *context = &vcpu->arch.mmu;
1483 context->new_cr3 = nonpaging_new_cr3;
1484 context->page_fault = nonpaging_page_fault;
1485 context->gva_to_gpa = nonpaging_gva_to_gpa;
1486 context->free = nonpaging_free;
1487 context->prefetch_page = nonpaging_prefetch_page;
1488 context->root_level = 0;
1489 context->shadow_root_level = PT32E_ROOT_LEVEL;
1490 context->root_hpa = INVALID_PAGE;
1494 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1496 ++vcpu->stat.tlb_flush;
1497 kvm_x86_ops->tlb_flush(vcpu);
1500 static void paging_new_cr3(struct kvm_vcpu *vcpu)
1502 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
1503 mmu_free_roots(vcpu);
1506 static void inject_page_fault(struct kvm_vcpu *vcpu,
1510 kvm_inject_page_fault(vcpu, addr, err_code);
1513 static void paging_free(struct kvm_vcpu *vcpu)
1515 nonpaging_free(vcpu);
1519 #include "paging_tmpl.h"
1523 #include "paging_tmpl.h"
1526 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1528 struct kvm_mmu *context = &vcpu->arch.mmu;
1530 ASSERT(is_pae(vcpu));
1531 context->new_cr3 = paging_new_cr3;
1532 context->page_fault = paging64_page_fault;
1533 context->gva_to_gpa = paging64_gva_to_gpa;
1534 context->prefetch_page = paging64_prefetch_page;
1535 context->free = paging_free;
1536 context->root_level = level;
1537 context->shadow_root_level = level;
1538 context->root_hpa = INVALID_PAGE;
1542 static int paging64_init_context(struct kvm_vcpu *vcpu)
1544 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1547 static int paging32_init_context(struct kvm_vcpu *vcpu)
1549 struct kvm_mmu *context = &vcpu->arch.mmu;
1551 context->new_cr3 = paging_new_cr3;
1552 context->page_fault = paging32_page_fault;
1553 context->gva_to_gpa = paging32_gva_to_gpa;
1554 context->free = paging_free;
1555 context->prefetch_page = paging32_prefetch_page;
1556 context->root_level = PT32_ROOT_LEVEL;
1557 context->shadow_root_level = PT32E_ROOT_LEVEL;
1558 context->root_hpa = INVALID_PAGE;
1562 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1564 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1567 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1569 struct kvm_mmu *context = &vcpu->arch.mmu;
1571 context->new_cr3 = nonpaging_new_cr3;
1572 context->page_fault = tdp_page_fault;
1573 context->free = nonpaging_free;
1574 context->prefetch_page = nonpaging_prefetch_page;
1575 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
1576 context->root_hpa = INVALID_PAGE;
1578 if (!is_paging(vcpu)) {
1579 context->gva_to_gpa = nonpaging_gva_to_gpa;
1580 context->root_level = 0;
1581 } else if (is_long_mode(vcpu)) {
1582 context->gva_to_gpa = paging64_gva_to_gpa;
1583 context->root_level = PT64_ROOT_LEVEL;
1584 } else if (is_pae(vcpu)) {
1585 context->gva_to_gpa = paging64_gva_to_gpa;
1586 context->root_level = PT32E_ROOT_LEVEL;
1588 context->gva_to_gpa = paging32_gva_to_gpa;
1589 context->root_level = PT32_ROOT_LEVEL;
1595 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
1598 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1600 if (!is_paging(vcpu))
1601 return nonpaging_init_context(vcpu);
1602 else if (is_long_mode(vcpu))
1603 return paging64_init_context(vcpu);
1604 else if (is_pae(vcpu))
1605 return paging32E_init_context(vcpu);
1607 return paging32_init_context(vcpu);
1610 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1612 vcpu->arch.update_pte.pfn = bad_pfn;
1615 return init_kvm_tdp_mmu(vcpu);
1617 return init_kvm_softmmu(vcpu);
1620 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1623 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1624 vcpu->arch.mmu.free(vcpu);
1625 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1629 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1631 destroy_kvm_mmu(vcpu);
1632 return init_kvm_mmu(vcpu);
1634 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
1636 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1640 r = mmu_topup_memory_caches(vcpu);
1643 spin_lock(&vcpu->kvm->mmu_lock);
1644 kvm_mmu_free_some_pages(vcpu);
1645 mmu_alloc_roots(vcpu);
1646 spin_unlock(&vcpu->kvm->mmu_lock);
1647 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
1648 kvm_mmu_flush_tlb(vcpu);
1652 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1654 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1656 mmu_free_roots(vcpu);
1659 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1660 struct kvm_mmu_page *sp,
1664 struct kvm_mmu_page *child;
1667 if (is_shadow_present_pte(pte)) {
1668 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1670 rmap_remove(vcpu->kvm, spte);
1672 child = page_header(pte & PT64_BASE_ADDR_MASK);
1673 mmu_page_remove_parent_pte(child, spte);
1676 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
1677 if (is_large_pte(pte))
1678 --vcpu->kvm->stat.lpages;
1681 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1682 struct kvm_mmu_page *sp,
1686 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1687 if (!vcpu->arch.update_pte.largepage ||
1688 sp->role.glevels == PT32_ROOT_LEVEL) {
1689 ++vcpu->kvm->stat.mmu_pde_zapped;
1694 ++vcpu->kvm->stat.mmu_pte_updated;
1695 if (sp->role.glevels == PT32_ROOT_LEVEL)
1696 paging32_update_pte(vcpu, sp, spte, new);
1698 paging64_update_pte(vcpu, sp, spte, new);
1701 static bool need_remote_flush(u64 old, u64 new)
1703 if (!is_shadow_present_pte(old))
1705 if (!is_shadow_present_pte(new))
1707 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1709 old ^= PT64_NX_MASK;
1710 new ^= PT64_NX_MASK;
1711 return (old & ~new & PT64_PERM_MASK) != 0;
1714 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1716 if (need_remote_flush(old, new))
1717 kvm_flush_remote_tlbs(vcpu->kvm);
1719 kvm_mmu_flush_tlb(vcpu);
1722 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1724 u64 *spte = vcpu->arch.last_pte_updated;
1726 return !!(spte && (*spte & shadow_accessed_mask));
1729 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1730 const u8 *new, int bytes)
1737 vcpu->arch.update_pte.largepage = 0;
1739 if (bytes != 4 && bytes != 8)
1743 * Assume that the pte write on a page table of the same type
1744 * as the current vcpu paging mode. This is nearly always true
1745 * (might be false while changing modes). Note it is verified later
1749 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1750 if ((bytes == 4) && (gpa % 4 == 0)) {
1751 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1754 memcpy((void *)&gpte + (gpa % 8), new, 4);
1755 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1756 memcpy((void *)&gpte, new, 8);
1759 if ((bytes == 4) && (gpa % 4 == 0))
1760 memcpy((void *)&gpte, new, 4);
1762 if (!is_present_pte(gpte))
1764 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1766 down_read(¤t->mm->mmap_sem);
1767 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1768 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1769 vcpu->arch.update_pte.largepage = 1;
1771 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
1772 /* implicit mb(), we'll read before PT lock is unlocked */
1773 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1774 up_read(¤t->mm->mmap_sem);
1776 if (is_error_pfn(pfn)) {
1777 kvm_release_pfn_clean(pfn);
1780 vcpu->arch.update_pte.gfn = gfn;
1781 vcpu->arch.update_pte.pfn = pfn;
1784 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1786 u64 *spte = vcpu->arch.last_pte_updated;
1789 && vcpu->arch.last_pte_gfn == gfn
1790 && shadow_accessed_mask
1791 && !(*spte & shadow_accessed_mask)
1792 && is_shadow_present_pte(*spte))
1793 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1796 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1797 const u8 *new, int bytes)
1799 gfn_t gfn = gpa >> PAGE_SHIFT;
1800 struct kvm_mmu_page *sp;
1801 struct hlist_node *node, *n;
1802 struct hlist_head *bucket;
1806 unsigned offset = offset_in_page(gpa);
1808 unsigned page_offset;
1809 unsigned misaligned;
1816 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
1817 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
1818 spin_lock(&vcpu->kvm->mmu_lock);
1819 kvm_mmu_access_page(vcpu, gfn);
1820 kvm_mmu_free_some_pages(vcpu);
1821 ++vcpu->kvm->stat.mmu_pte_write;
1822 kvm_mmu_audit(vcpu, "pre pte write");
1823 if (gfn == vcpu->arch.last_pt_write_gfn
1824 && !last_updated_pte_accessed(vcpu)) {
1825 ++vcpu->arch.last_pt_write_count;
1826 if (vcpu->arch.last_pt_write_count >= 3)
1829 vcpu->arch.last_pt_write_gfn = gfn;
1830 vcpu->arch.last_pt_write_count = 1;
1831 vcpu->arch.last_pte_updated = NULL;
1833 index = kvm_page_table_hashfn(gfn);
1834 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1835 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1836 if (sp->gfn != gfn || sp->role.metaphysical)
1838 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1839 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1840 misaligned |= bytes < 4;
1841 if (misaligned || flooded) {
1843 * Misaligned accesses are too much trouble to fix
1844 * up; also, they usually indicate a page is not used
1847 * If we're seeing too many writes to a page,
1848 * it may no longer be a page table, or we may be
1849 * forking, in which case it is better to unmap the
1852 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1853 gpa, bytes, sp->role.word);
1854 kvm_mmu_zap_page(vcpu->kvm, sp);
1855 ++vcpu->kvm->stat.mmu_flooded;
1858 page_offset = offset;
1859 level = sp->role.level;
1861 if (sp->role.glevels == PT32_ROOT_LEVEL) {
1862 page_offset <<= 1; /* 32->64 */
1864 * A 32-bit pde maps 4MB while the shadow pdes map
1865 * only 2MB. So we need to double the offset again
1866 * and zap two pdes instead of one.
1868 if (level == PT32_ROOT_LEVEL) {
1869 page_offset &= ~7; /* kill rounding error */
1873 quadrant = page_offset >> PAGE_SHIFT;
1874 page_offset &= ~PAGE_MASK;
1875 if (quadrant != sp->role.quadrant)
1878 spte = &sp->spt[page_offset / sizeof(*spte)];
1879 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1881 r = kvm_read_guest_atomic(vcpu->kvm,
1882 gpa & ~(u64)(pte_size - 1),
1884 new = (const void *)&gentry;
1890 mmu_pte_write_zap_pte(vcpu, sp, spte);
1892 mmu_pte_write_new_pte(vcpu, sp, spte, new);
1893 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
1897 kvm_mmu_audit(vcpu, "post pte write");
1898 spin_unlock(&vcpu->kvm->mmu_lock);
1899 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1900 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1901 vcpu->arch.update_pte.pfn = bad_pfn;
1905 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1910 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1912 spin_lock(&vcpu->kvm->mmu_lock);
1913 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1914 spin_unlock(&vcpu->kvm->mmu_lock);
1917 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
1919 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1921 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
1922 struct kvm_mmu_page *sp;
1924 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
1925 struct kvm_mmu_page, link);
1926 kvm_mmu_zap_page(vcpu->kvm, sp);
1927 ++vcpu->kvm->stat.mmu_recycled;
1931 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1934 enum emulation_result er;
1936 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
1945 r = mmu_topup_memory_caches(vcpu);
1949 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1954 case EMULATE_DO_MMIO:
1955 ++vcpu->stat.mmio_exits;
1958 kvm_report_emulation_failure(vcpu, "pagetable");
1966 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1968 void kvm_enable_tdp(void)
1972 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1974 void kvm_disable_tdp(void)
1976 tdp_enabled = false;
1978 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
1980 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1982 struct kvm_mmu_page *sp;
1984 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1985 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
1986 struct kvm_mmu_page, link);
1987 kvm_mmu_zap_page(vcpu->kvm, sp);
1990 free_page((unsigned long)vcpu->arch.mmu.pae_root);
1993 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2000 if (vcpu->kvm->arch.n_requested_mmu_pages)
2001 vcpu->kvm->arch.n_free_mmu_pages =
2002 vcpu->kvm->arch.n_requested_mmu_pages;
2004 vcpu->kvm->arch.n_free_mmu_pages =
2005 vcpu->kvm->arch.n_alloc_mmu_pages;
2007 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2008 * Therefore we need to allocate shadow page tables in the first
2009 * 4GB of memory, which happens to fit the DMA32 zone.
2011 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2014 vcpu->arch.mmu.pae_root = page_address(page);
2015 for (i = 0; i < 4; ++i)
2016 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2021 free_mmu_pages(vcpu);
2025 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2028 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2030 return alloc_mmu_pages(vcpu);
2033 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2036 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2038 return init_kvm_mmu(vcpu);
2041 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2045 destroy_kvm_mmu(vcpu);
2046 free_mmu_pages(vcpu);
2047 mmu_free_memory_caches(vcpu);
2050 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2052 struct kvm_mmu_page *sp;
2054 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2058 if (!test_bit(slot, &sp->slot_bitmap))
2062 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2064 if (pt[i] & PT_WRITABLE_MASK)
2065 pt[i] &= ~PT_WRITABLE_MASK;
2069 void kvm_mmu_zap_all(struct kvm *kvm)
2071 struct kvm_mmu_page *sp, *node;
2073 spin_lock(&kvm->mmu_lock);
2074 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2075 kvm_mmu_zap_page(kvm, sp);
2076 spin_unlock(&kvm->mmu_lock);
2078 kvm_flush_remote_tlbs(kvm);
2081 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2083 struct kvm_mmu_page *page;
2085 page = container_of(kvm->arch.active_mmu_pages.prev,
2086 struct kvm_mmu_page, link);
2087 kvm_mmu_zap_page(kvm, page);
2090 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2093 struct kvm *kvm_freed = NULL;
2094 int cache_count = 0;
2096 spin_lock(&kvm_lock);
2098 list_for_each_entry(kvm, &vm_list, vm_list) {
2101 if (!down_read_trylock(&kvm->slots_lock))
2103 spin_lock(&kvm->mmu_lock);
2104 npages = kvm->arch.n_alloc_mmu_pages -
2105 kvm->arch.n_free_mmu_pages;
2106 cache_count += npages;
2107 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2108 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2114 spin_unlock(&kvm->mmu_lock);
2115 up_read(&kvm->slots_lock);
2118 list_move_tail(&kvm_freed->vm_list, &vm_list);
2120 spin_unlock(&kvm_lock);
2125 static struct shrinker mmu_shrinker = {
2126 .shrink = mmu_shrink,
2127 .seeks = DEFAULT_SEEKS * 10,
2130 static void mmu_destroy_caches(void)
2132 if (pte_chain_cache)
2133 kmem_cache_destroy(pte_chain_cache);
2134 if (rmap_desc_cache)
2135 kmem_cache_destroy(rmap_desc_cache);
2136 if (mmu_page_header_cache)
2137 kmem_cache_destroy(mmu_page_header_cache);
2140 void kvm_mmu_module_exit(void)
2142 mmu_destroy_caches();
2143 unregister_shrinker(&mmu_shrinker);
2146 int kvm_mmu_module_init(void)
2148 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2149 sizeof(struct kvm_pte_chain),
2151 if (!pte_chain_cache)
2153 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2154 sizeof(struct kvm_rmap_desc),
2156 if (!rmap_desc_cache)
2159 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2160 sizeof(struct kvm_mmu_page),
2162 if (!mmu_page_header_cache)
2165 register_shrinker(&mmu_shrinker);
2170 mmu_destroy_caches();
2175 * Caculate mmu pages needed for kvm.
2177 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2180 unsigned int nr_mmu_pages;
2181 unsigned int nr_pages = 0;
2183 for (i = 0; i < kvm->nmemslots; i++)
2184 nr_pages += kvm->memslots[i].npages;
2186 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2187 nr_mmu_pages = max(nr_mmu_pages,
2188 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2190 return nr_mmu_pages;
2193 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2196 if (len > buffer->len)
2201 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2206 ret = pv_mmu_peek_buffer(buffer, len);
2211 buffer->processed += len;
2215 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2216 gpa_t addr, gpa_t value)
2221 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2224 r = mmu_topup_memory_caches(vcpu);
2228 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2234 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2236 kvm_x86_ops->tlb_flush(vcpu);
2240 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2242 spin_lock(&vcpu->kvm->mmu_lock);
2243 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2244 spin_unlock(&vcpu->kvm->mmu_lock);
2248 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2249 struct kvm_pv_mmu_op_buffer *buffer)
2251 struct kvm_mmu_op_header *header;
2253 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2256 switch (header->op) {
2257 case KVM_MMU_OP_WRITE_PTE: {
2258 struct kvm_mmu_op_write_pte *wpte;
2260 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2263 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2266 case KVM_MMU_OP_FLUSH_TLB: {
2267 struct kvm_mmu_op_flush_tlb *ftlb;
2269 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2272 return kvm_pv_mmu_flush_tlb(vcpu);
2274 case KVM_MMU_OP_RELEASE_PT: {
2275 struct kvm_mmu_op_release_pt *rpt;
2277 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2280 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2286 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2287 gpa_t addr, unsigned long *ret)
2290 struct kvm_pv_mmu_op_buffer buffer;
2292 buffer.ptr = buffer.buf;
2293 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2294 buffer.processed = 0;
2296 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2300 while (buffer.len) {
2301 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2310 *ret = buffer.processed;
2316 static const char *audit_msg;
2318 static gva_t canonicalize(gva_t gva)
2320 #ifdef CONFIG_X86_64
2321 gva = (long long)(gva << 16) >> 16;
2326 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2327 gva_t va, int level)
2329 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2331 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2333 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2336 if (ent == shadow_trap_nonpresent_pte)
2339 va = canonicalize(va);
2341 if (ent == shadow_notrap_nonpresent_pte)
2342 printk(KERN_ERR "audit: (%s) nontrapping pte"
2343 " in nonleaf level: levels %d gva %lx"
2344 " level %d pte %llx\n", audit_msg,
2345 vcpu->arch.mmu.root_level, va, level, ent);
2347 audit_mappings_page(vcpu, ent, va, level - 1);
2349 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
2350 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
2352 if (is_shadow_present_pte(ent)
2353 && (ent & PT64_BASE_ADDR_MASK) != hpa)
2354 printk(KERN_ERR "xx audit error: (%s) levels %d"
2355 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2356 audit_msg, vcpu->arch.mmu.root_level,
2358 is_shadow_present_pte(ent));
2359 else if (ent == shadow_notrap_nonpresent_pte
2360 && !is_error_hpa(hpa))
2361 printk(KERN_ERR "audit: (%s) notrap shadow,"
2362 " valid guest gva %lx\n", audit_msg, va);
2363 kvm_release_pfn_clean(pfn);
2369 static void audit_mappings(struct kvm_vcpu *vcpu)
2373 if (vcpu->arch.mmu.root_level == 4)
2374 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
2376 for (i = 0; i < 4; ++i)
2377 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
2378 audit_mappings_page(vcpu,
2379 vcpu->arch.mmu.pae_root[i],
2384 static int count_rmaps(struct kvm_vcpu *vcpu)
2389 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2390 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2391 struct kvm_rmap_desc *d;
2393 for (j = 0; j < m->npages; ++j) {
2394 unsigned long *rmapp = &m->rmap[j];
2398 if (!(*rmapp & 1)) {
2402 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
2404 for (k = 0; k < RMAP_EXT; ++k)
2405 if (d->shadow_ptes[k])
2416 static int count_writable_mappings(struct kvm_vcpu *vcpu)
2419 struct kvm_mmu_page *sp;
2422 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2425 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
2428 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2431 if (!(ent & PT_PRESENT_MASK))
2433 if (!(ent & PT_WRITABLE_MASK))
2441 static void audit_rmap(struct kvm_vcpu *vcpu)
2443 int n_rmap = count_rmaps(vcpu);
2444 int n_actual = count_writable_mappings(vcpu);
2446 if (n_rmap != n_actual)
2447 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2448 __func__, audit_msg, n_rmap, n_actual);
2451 static void audit_write_protection(struct kvm_vcpu *vcpu)
2453 struct kvm_mmu_page *sp;
2454 struct kvm_memory_slot *slot;
2455 unsigned long *rmapp;
2458 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2459 if (sp->role.metaphysical)
2462 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2463 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
2464 rmapp = &slot->rmap[gfn - slot->base_gfn];
2466 printk(KERN_ERR "%s: (%s) shadow page has writable"
2467 " mappings: gfn %lx role %x\n",
2468 __func__, audit_msg, sp->gfn,
2473 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2480 audit_write_protection(vcpu);
2481 audit_mappings(vcpu);