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 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
140 struct kvm_rmap_desc {
141 u64 *shadow_ptes[RMAP_EXT];
142 struct kvm_rmap_desc *more;
145 struct kvm_shadow_walk {
146 int (*entry)(struct kvm_shadow_walk *walk, struct kvm_vcpu *vcpu,
147 u64 addr, u64 *spte, int level);
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;
410 addr = gfn_to_hva(kvm, gfn);
411 if (kvm_is_error_hva(addr))
414 down_read(¤t->mm->mmap_sem);
415 vma = find_vma(current->mm, addr);
416 if (vma && is_vm_hugetlb_page(vma))
418 up_read(¤t->mm->mmap_sem);
423 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
425 struct kvm_memory_slot *slot;
427 if (has_wrprotected_page(vcpu->kvm, large_gfn))
430 if (!host_largepage_backed(vcpu->kvm, large_gfn))
433 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
434 if (slot && slot->dirty_bitmap)
441 * Take gfn and return the reverse mapping to it.
442 * Note: gfn must be unaliased before this function get called
445 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
447 struct kvm_memory_slot *slot;
450 slot = gfn_to_memslot(kvm, gfn);
452 return &slot->rmap[gfn - slot->base_gfn];
454 idx = (gfn / KVM_PAGES_PER_HPAGE) -
455 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
457 return &slot->lpage_info[idx].rmap_pde;
461 * Reverse mapping data structures:
463 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
464 * that points to page_address(page).
466 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
467 * containing more mappings.
469 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
471 struct kvm_mmu_page *sp;
472 struct kvm_rmap_desc *desc;
473 unsigned long *rmapp;
476 if (!is_rmap_pte(*spte))
478 gfn = unalias_gfn(vcpu->kvm, gfn);
479 sp = page_header(__pa(spte));
480 sp->gfns[spte - sp->spt] = gfn;
481 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
483 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
484 *rmapp = (unsigned long)spte;
485 } else if (!(*rmapp & 1)) {
486 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
487 desc = mmu_alloc_rmap_desc(vcpu);
488 desc->shadow_ptes[0] = (u64 *)*rmapp;
489 desc->shadow_ptes[1] = spte;
490 *rmapp = (unsigned long)desc | 1;
492 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
493 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
494 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
496 if (desc->shadow_ptes[RMAP_EXT-1]) {
497 desc->more = mmu_alloc_rmap_desc(vcpu);
500 for (i = 0; desc->shadow_ptes[i]; ++i)
502 desc->shadow_ptes[i] = spte;
506 static void rmap_desc_remove_entry(unsigned long *rmapp,
507 struct kvm_rmap_desc *desc,
509 struct kvm_rmap_desc *prev_desc)
513 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
515 desc->shadow_ptes[i] = desc->shadow_ptes[j];
516 desc->shadow_ptes[j] = NULL;
519 if (!prev_desc && !desc->more)
520 *rmapp = (unsigned long)desc->shadow_ptes[0];
523 prev_desc->more = desc->more;
525 *rmapp = (unsigned long)desc->more | 1;
526 mmu_free_rmap_desc(desc);
529 static void rmap_remove(struct kvm *kvm, u64 *spte)
531 struct kvm_rmap_desc *desc;
532 struct kvm_rmap_desc *prev_desc;
533 struct kvm_mmu_page *sp;
535 unsigned long *rmapp;
538 if (!is_rmap_pte(*spte))
540 sp = page_header(__pa(spte));
541 pfn = spte_to_pfn(*spte);
542 if (*spte & shadow_accessed_mask)
543 kvm_set_pfn_accessed(pfn);
544 if (is_writeble_pte(*spte))
545 kvm_release_pfn_dirty(pfn);
547 kvm_release_pfn_clean(pfn);
548 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
550 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
552 } else if (!(*rmapp & 1)) {
553 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
554 if ((u64 *)*rmapp != spte) {
555 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
561 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
562 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
565 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
566 if (desc->shadow_ptes[i] == spte) {
567 rmap_desc_remove_entry(rmapp,
579 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
581 struct kvm_rmap_desc *desc;
582 struct kvm_rmap_desc *prev_desc;
588 else if (!(*rmapp & 1)) {
590 return (u64 *)*rmapp;
593 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
597 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
598 if (prev_spte == spte)
599 return desc->shadow_ptes[i];
600 prev_spte = desc->shadow_ptes[i];
607 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
609 unsigned long *rmapp;
611 int write_protected = 0;
613 gfn = unalias_gfn(kvm, gfn);
614 rmapp = gfn_to_rmap(kvm, gfn, 0);
616 spte = rmap_next(kvm, rmapp, NULL);
619 BUG_ON(!(*spte & PT_PRESENT_MASK));
620 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
621 if (is_writeble_pte(*spte)) {
622 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
625 spte = rmap_next(kvm, rmapp, spte);
627 if (write_protected) {
630 spte = rmap_next(kvm, rmapp, NULL);
631 pfn = spte_to_pfn(*spte);
632 kvm_set_pfn_dirty(pfn);
635 /* check for huge page mappings */
636 rmapp = gfn_to_rmap(kvm, gfn, 1);
637 spte = rmap_next(kvm, rmapp, NULL);
640 BUG_ON(!(*spte & PT_PRESENT_MASK));
641 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
642 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
643 if (is_writeble_pte(*spte)) {
644 rmap_remove(kvm, spte);
646 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
650 spte = rmap_next(kvm, rmapp, spte);
654 kvm_flush_remote_tlbs(kvm);
656 account_shadowed(kvm, gfn);
659 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
662 int need_tlb_flush = 0;
664 while ((spte = rmap_next(kvm, rmapp, NULL))) {
665 BUG_ON(!(*spte & PT_PRESENT_MASK));
666 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
667 rmap_remove(kvm, spte);
668 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
671 return need_tlb_flush;
674 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
675 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
681 * If mmap_sem isn't taken, we can look the memslots with only
682 * the mmu_lock by skipping over the slots with userspace_addr == 0.
684 for (i = 0; i < kvm->nmemslots; i++) {
685 struct kvm_memory_slot *memslot = &kvm->memslots[i];
686 unsigned long start = memslot->userspace_addr;
689 /* mmu_lock protects userspace_addr */
693 end = start + (memslot->npages << PAGE_SHIFT);
694 if (hva >= start && hva < end) {
695 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
696 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
697 retval |= handler(kvm,
698 &memslot->lpage_info[
700 KVM_PAGES_PER_HPAGE].rmap_pde);
707 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
709 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
712 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
717 /* always return old for EPT */
718 if (!shadow_accessed_mask)
721 spte = rmap_next(kvm, rmapp, NULL);
725 BUG_ON(!(_spte & PT_PRESENT_MASK));
726 _young = _spte & PT_ACCESSED_MASK;
729 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
731 spte = rmap_next(kvm, rmapp, spte);
736 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
738 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
742 static int is_empty_shadow_page(u64 *spt)
747 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
748 if (is_shadow_present_pte(*pos)) {
749 printk(KERN_ERR "%s: %p %llx\n", __func__,
757 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
759 ASSERT(is_empty_shadow_page(sp->spt));
761 __free_page(virt_to_page(sp->spt));
762 __free_page(virt_to_page(sp->gfns));
764 ++kvm->arch.n_free_mmu_pages;
767 static unsigned kvm_page_table_hashfn(gfn_t gfn)
769 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
772 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
775 struct kvm_mmu_page *sp;
777 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
778 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
779 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
780 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
781 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
782 ASSERT(is_empty_shadow_page(sp->spt));
785 sp->parent_pte = parent_pte;
786 --vcpu->kvm->arch.n_free_mmu_pages;
790 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
791 struct kvm_mmu_page *sp, u64 *parent_pte)
793 struct kvm_pte_chain *pte_chain;
794 struct hlist_node *node;
799 if (!sp->multimapped) {
800 u64 *old = sp->parent_pte;
803 sp->parent_pte = parent_pte;
807 pte_chain = mmu_alloc_pte_chain(vcpu);
808 INIT_HLIST_HEAD(&sp->parent_ptes);
809 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
810 pte_chain->parent_ptes[0] = old;
812 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
813 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
815 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
816 if (!pte_chain->parent_ptes[i]) {
817 pte_chain->parent_ptes[i] = parent_pte;
821 pte_chain = mmu_alloc_pte_chain(vcpu);
823 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
824 pte_chain->parent_ptes[0] = parent_pte;
827 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
830 struct kvm_pte_chain *pte_chain;
831 struct hlist_node *node;
834 if (!sp->multimapped) {
835 BUG_ON(sp->parent_pte != parent_pte);
836 sp->parent_pte = NULL;
839 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
840 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
841 if (!pte_chain->parent_ptes[i])
843 if (pte_chain->parent_ptes[i] != parent_pte)
845 while (i + 1 < NR_PTE_CHAIN_ENTRIES
846 && pte_chain->parent_ptes[i + 1]) {
847 pte_chain->parent_ptes[i]
848 = pte_chain->parent_ptes[i + 1];
851 pte_chain->parent_ptes[i] = NULL;
853 hlist_del(&pte_chain->link);
854 mmu_free_pte_chain(pte_chain);
855 if (hlist_empty(&sp->parent_ptes)) {
857 sp->parent_pte = NULL;
865 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
866 struct kvm_mmu_page *sp)
870 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
871 sp->spt[i] = shadow_trap_nonpresent_pte;
874 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
877 struct hlist_head *bucket;
878 struct kvm_mmu_page *sp;
879 struct hlist_node *node;
881 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
882 index = kvm_page_table_hashfn(gfn);
883 bucket = &kvm->arch.mmu_page_hash[index];
884 hlist_for_each_entry(sp, node, bucket, hash_link)
885 if (sp->gfn == gfn && !sp->role.metaphysical
886 && !sp->role.invalid) {
887 pgprintk("%s: found role %x\n",
888 __func__, sp->role.word);
894 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
902 union kvm_mmu_page_role role;
905 struct hlist_head *bucket;
906 struct kvm_mmu_page *sp;
907 struct hlist_node *node;
910 role.glevels = vcpu->arch.mmu.root_level;
912 role.metaphysical = metaphysical;
913 role.access = access;
914 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
915 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
916 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
917 role.quadrant = quadrant;
919 pgprintk("%s: looking gfn %lx role %x\n", __func__,
921 index = kvm_page_table_hashfn(gfn);
922 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
923 hlist_for_each_entry(sp, node, bucket, hash_link)
924 if (sp->gfn == gfn && sp->role.word == role.word) {
925 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
926 pgprintk("%s: found\n", __func__);
929 ++vcpu->kvm->stat.mmu_cache_miss;
930 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
933 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
936 hlist_add_head(&sp->hash_link, bucket);
938 rmap_write_protect(vcpu->kvm, gfn);
939 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
940 vcpu->arch.mmu.prefetch_page(vcpu, sp);
942 nonpaging_prefetch_page(vcpu, sp);
946 static int walk_shadow(struct kvm_shadow_walk *walker,
947 struct kvm_vcpu *vcpu, u64 addr)
955 shadow_addr = vcpu->arch.mmu.root_hpa;
956 level = vcpu->arch.mmu.shadow_root_level;
957 if (level == PT32E_ROOT_LEVEL) {
958 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
959 shadow_addr &= PT64_BASE_ADDR_MASK;
963 while (level >= PT_PAGE_TABLE_LEVEL) {
964 index = SHADOW_PT_INDEX(addr, level);
965 sptep = ((u64 *)__va(shadow_addr)) + index;
966 r = walker->entry(walker, vcpu, addr, sptep, level);
969 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
975 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
976 struct kvm_mmu_page *sp)
984 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
985 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
986 if (is_shadow_present_pte(pt[i]))
987 rmap_remove(kvm, &pt[i]);
988 pt[i] = shadow_trap_nonpresent_pte;
993 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
996 if (is_shadow_present_pte(ent)) {
997 if (!is_large_pte(ent)) {
998 ent &= PT64_BASE_ADDR_MASK;
999 mmu_page_remove_parent_pte(page_header(ent),
1003 rmap_remove(kvm, &pt[i]);
1006 pt[i] = shadow_trap_nonpresent_pte;
1010 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1012 mmu_page_remove_parent_pte(sp, parent_pte);
1015 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1019 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1021 kvm->vcpus[i]->arch.last_pte_updated = NULL;
1024 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1028 while (sp->multimapped || sp->parent_pte) {
1029 if (!sp->multimapped)
1030 parent_pte = sp->parent_pte;
1032 struct kvm_pte_chain *chain;
1034 chain = container_of(sp->parent_ptes.first,
1035 struct kvm_pte_chain, link);
1036 parent_pte = chain->parent_ptes[0];
1038 BUG_ON(!parent_pte);
1039 kvm_mmu_put_page(sp, parent_pte);
1040 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1044 static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1046 ++kvm->stat.mmu_shadow_zapped;
1047 kvm_mmu_page_unlink_children(kvm, sp);
1048 kvm_mmu_unlink_parents(kvm, sp);
1049 kvm_flush_remote_tlbs(kvm);
1050 if (!sp->role.invalid && !sp->role.metaphysical)
1051 unaccount_shadowed(kvm, sp->gfn);
1052 if (!sp->root_count) {
1053 hlist_del(&sp->hash_link);
1054 kvm_mmu_free_page(kvm, sp);
1056 sp->role.invalid = 1;
1057 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1058 kvm_reload_remote_mmus(kvm);
1060 kvm_mmu_reset_last_pte_updated(kvm);
1064 * Changing the number of mmu pages allocated to the vm
1065 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1067 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1070 * If we set the number of mmu pages to be smaller be than the
1071 * number of actived pages , we must to free some mmu pages before we
1075 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
1077 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1078 - kvm->arch.n_free_mmu_pages;
1080 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1081 struct kvm_mmu_page *page;
1083 page = container_of(kvm->arch.active_mmu_pages.prev,
1084 struct kvm_mmu_page, link);
1085 kvm_mmu_zap_page(kvm, page);
1088 kvm->arch.n_free_mmu_pages = 0;
1091 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1092 - kvm->arch.n_alloc_mmu_pages;
1094 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1097 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1100 struct hlist_head *bucket;
1101 struct kvm_mmu_page *sp;
1102 struct hlist_node *node, *n;
1105 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1107 index = kvm_page_table_hashfn(gfn);
1108 bucket = &kvm->arch.mmu_page_hash[index];
1109 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1110 if (sp->gfn == gfn && !sp->role.metaphysical) {
1111 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1113 kvm_mmu_zap_page(kvm, sp);
1119 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1121 struct kvm_mmu_page *sp;
1123 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
1124 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
1125 kvm_mmu_zap_page(kvm, sp);
1129 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1131 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1132 struct kvm_mmu_page *sp = page_header(__pa(pte));
1134 __set_bit(slot, &sp->slot_bitmap);
1137 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1141 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1143 if (gpa == UNMAPPED_GVA)
1146 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1151 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1152 unsigned pt_access, unsigned pte_access,
1153 int user_fault, int write_fault, int dirty,
1154 int *ptwrite, int largepage, gfn_t gfn,
1155 pfn_t pfn, bool speculative)
1158 int was_rmapped = 0;
1159 int was_writeble = is_writeble_pte(*shadow_pte);
1161 pgprintk("%s: spte %llx access %x write_fault %d"
1162 " user_fault %d gfn %lx\n",
1163 __func__, *shadow_pte, pt_access,
1164 write_fault, user_fault, gfn);
1166 if (is_rmap_pte(*shadow_pte)) {
1168 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1169 * the parent of the now unreachable PTE.
1171 if (largepage && !is_large_pte(*shadow_pte)) {
1172 struct kvm_mmu_page *child;
1173 u64 pte = *shadow_pte;
1175 child = page_header(pte & PT64_BASE_ADDR_MASK);
1176 mmu_page_remove_parent_pte(child, shadow_pte);
1177 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1178 pgprintk("hfn old %lx new %lx\n",
1179 spte_to_pfn(*shadow_pte), pfn);
1180 rmap_remove(vcpu->kvm, shadow_pte);
1183 was_rmapped = is_large_pte(*shadow_pte);
1190 * We don't set the accessed bit, since we sometimes want to see
1191 * whether the guest actually used the pte (in order to detect
1194 spte = shadow_base_present_pte | shadow_dirty_mask;
1196 spte |= shadow_accessed_mask;
1198 pte_access &= ~ACC_WRITE_MASK;
1199 if (pte_access & ACC_EXEC_MASK)
1200 spte |= shadow_x_mask;
1202 spte |= shadow_nx_mask;
1203 if (pte_access & ACC_USER_MASK)
1204 spte |= shadow_user_mask;
1206 spte |= PT_PAGE_SIZE_MASK;
1208 spte |= (u64)pfn << PAGE_SHIFT;
1210 if ((pte_access & ACC_WRITE_MASK)
1211 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1212 struct kvm_mmu_page *shadow;
1214 spte |= PT_WRITABLE_MASK;
1216 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1218 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
1219 pgprintk("%s: found shadow page for %lx, marking ro\n",
1221 pte_access &= ~ACC_WRITE_MASK;
1222 if (is_writeble_pte(spte)) {
1223 spte &= ~PT_WRITABLE_MASK;
1224 kvm_x86_ops->tlb_flush(vcpu);
1231 if (pte_access & ACC_WRITE_MASK)
1232 mark_page_dirty(vcpu->kvm, gfn);
1234 pgprintk("%s: setting spte %llx\n", __func__, spte);
1235 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1236 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1237 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
1238 set_shadow_pte(shadow_pte, spte);
1239 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1240 && (spte & PT_PRESENT_MASK))
1241 ++vcpu->kvm->stat.lpages;
1243 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1245 rmap_add(vcpu, shadow_pte, gfn, largepage);
1246 if (!is_rmap_pte(*shadow_pte))
1247 kvm_release_pfn_clean(pfn);
1250 kvm_release_pfn_dirty(pfn);
1252 kvm_release_pfn_clean(pfn);
1255 vcpu->arch.last_pte_updated = shadow_pte;
1256 vcpu->arch.last_pte_gfn = gfn;
1260 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1264 struct direct_shadow_walk {
1265 struct kvm_shadow_walk walker;
1272 static int direct_map_entry(struct kvm_shadow_walk *_walk,
1273 struct kvm_vcpu *vcpu,
1274 u64 addr, u64 *sptep, int level)
1276 struct direct_shadow_walk *walk =
1277 container_of(_walk, struct direct_shadow_walk, walker);
1278 struct kvm_mmu_page *sp;
1280 gfn_t gfn = addr >> PAGE_SHIFT;
1282 if (level == PT_PAGE_TABLE_LEVEL
1283 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1284 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1285 0, walk->write, 1, &walk->pt_write,
1286 walk->largepage, gfn, walk->pfn, false);
1287 ++vcpu->stat.pf_fixed;
1291 if (*sptep == shadow_trap_nonpresent_pte) {
1292 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1293 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
1296 pgprintk("nonpaging_map: ENOMEM\n");
1297 kvm_release_pfn_clean(walk->pfn);
1301 set_shadow_pte(sptep,
1303 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1304 | shadow_user_mask | shadow_x_mask);
1309 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1310 int largepage, gfn_t gfn, pfn_t pfn)
1313 struct direct_shadow_walk walker = {
1314 .walker = { .entry = direct_map_entry, },
1316 .largepage = largepage,
1321 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
1324 return walker.pt_write;
1327 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1332 unsigned long mmu_seq;
1334 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1335 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1339 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1341 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1344 if (is_error_pfn(pfn)) {
1345 kvm_release_pfn_clean(pfn);
1349 spin_lock(&vcpu->kvm->mmu_lock);
1350 if (mmu_notifier_retry(vcpu, mmu_seq))
1352 kvm_mmu_free_some_pages(vcpu);
1353 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1354 spin_unlock(&vcpu->kvm->mmu_lock);
1360 spin_unlock(&vcpu->kvm->mmu_lock);
1361 kvm_release_pfn_clean(pfn);
1366 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1369 struct kvm_mmu_page *sp;
1371 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1373 spin_lock(&vcpu->kvm->mmu_lock);
1374 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1375 hpa_t root = vcpu->arch.mmu.root_hpa;
1377 sp = page_header(root);
1379 if (!sp->root_count && sp->role.invalid)
1380 kvm_mmu_zap_page(vcpu->kvm, sp);
1381 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1382 spin_unlock(&vcpu->kvm->mmu_lock);
1385 for (i = 0; i < 4; ++i) {
1386 hpa_t root = vcpu->arch.mmu.pae_root[i];
1389 root &= PT64_BASE_ADDR_MASK;
1390 sp = page_header(root);
1392 if (!sp->root_count && sp->role.invalid)
1393 kvm_mmu_zap_page(vcpu->kvm, sp);
1395 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1397 spin_unlock(&vcpu->kvm->mmu_lock);
1398 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1401 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1405 struct kvm_mmu_page *sp;
1406 int metaphysical = 0;
1408 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1410 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1411 hpa_t root = vcpu->arch.mmu.root_hpa;
1413 ASSERT(!VALID_PAGE(root));
1416 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1417 PT64_ROOT_LEVEL, metaphysical,
1419 root = __pa(sp->spt);
1421 vcpu->arch.mmu.root_hpa = root;
1424 metaphysical = !is_paging(vcpu);
1427 for (i = 0; i < 4; ++i) {
1428 hpa_t root = vcpu->arch.mmu.pae_root[i];
1430 ASSERT(!VALID_PAGE(root));
1431 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1432 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1433 vcpu->arch.mmu.pae_root[i] = 0;
1436 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1437 } else if (vcpu->arch.mmu.root_level == 0)
1439 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1440 PT32_ROOT_LEVEL, metaphysical,
1442 root = __pa(sp->spt);
1444 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1446 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1449 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1454 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1460 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
1461 r = mmu_topup_memory_caches(vcpu);
1466 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1468 gfn = gva >> PAGE_SHIFT;
1470 return nonpaging_map(vcpu, gva & PAGE_MASK,
1471 error_code & PFERR_WRITE_MASK, gfn);
1474 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1480 gfn_t gfn = gpa >> PAGE_SHIFT;
1481 unsigned long mmu_seq;
1484 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1486 r = mmu_topup_memory_caches(vcpu);
1490 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1491 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1494 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1496 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1497 if (is_error_pfn(pfn)) {
1498 kvm_release_pfn_clean(pfn);
1501 spin_lock(&vcpu->kvm->mmu_lock);
1502 if (mmu_notifier_retry(vcpu, mmu_seq))
1504 kvm_mmu_free_some_pages(vcpu);
1505 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1506 largepage, gfn, pfn);
1507 spin_unlock(&vcpu->kvm->mmu_lock);
1512 spin_unlock(&vcpu->kvm->mmu_lock);
1513 kvm_release_pfn_clean(pfn);
1517 static void nonpaging_free(struct kvm_vcpu *vcpu)
1519 mmu_free_roots(vcpu);
1522 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1524 struct kvm_mmu *context = &vcpu->arch.mmu;
1526 context->new_cr3 = nonpaging_new_cr3;
1527 context->page_fault = nonpaging_page_fault;
1528 context->gva_to_gpa = nonpaging_gva_to_gpa;
1529 context->free = nonpaging_free;
1530 context->prefetch_page = nonpaging_prefetch_page;
1531 context->root_level = 0;
1532 context->shadow_root_level = PT32E_ROOT_LEVEL;
1533 context->root_hpa = INVALID_PAGE;
1537 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1539 ++vcpu->stat.tlb_flush;
1540 kvm_x86_ops->tlb_flush(vcpu);
1543 static void paging_new_cr3(struct kvm_vcpu *vcpu)
1545 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
1546 mmu_free_roots(vcpu);
1549 static void inject_page_fault(struct kvm_vcpu *vcpu,
1553 kvm_inject_page_fault(vcpu, addr, err_code);
1556 static void paging_free(struct kvm_vcpu *vcpu)
1558 nonpaging_free(vcpu);
1562 #include "paging_tmpl.h"
1566 #include "paging_tmpl.h"
1569 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1571 struct kvm_mmu *context = &vcpu->arch.mmu;
1573 ASSERT(is_pae(vcpu));
1574 context->new_cr3 = paging_new_cr3;
1575 context->page_fault = paging64_page_fault;
1576 context->gva_to_gpa = paging64_gva_to_gpa;
1577 context->prefetch_page = paging64_prefetch_page;
1578 context->free = paging_free;
1579 context->root_level = level;
1580 context->shadow_root_level = level;
1581 context->root_hpa = INVALID_PAGE;
1585 static int paging64_init_context(struct kvm_vcpu *vcpu)
1587 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1590 static int paging32_init_context(struct kvm_vcpu *vcpu)
1592 struct kvm_mmu *context = &vcpu->arch.mmu;
1594 context->new_cr3 = paging_new_cr3;
1595 context->page_fault = paging32_page_fault;
1596 context->gva_to_gpa = paging32_gva_to_gpa;
1597 context->free = paging_free;
1598 context->prefetch_page = paging32_prefetch_page;
1599 context->root_level = PT32_ROOT_LEVEL;
1600 context->shadow_root_level = PT32E_ROOT_LEVEL;
1601 context->root_hpa = INVALID_PAGE;
1605 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1607 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1610 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1612 struct kvm_mmu *context = &vcpu->arch.mmu;
1614 context->new_cr3 = nonpaging_new_cr3;
1615 context->page_fault = tdp_page_fault;
1616 context->free = nonpaging_free;
1617 context->prefetch_page = nonpaging_prefetch_page;
1618 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
1619 context->root_hpa = INVALID_PAGE;
1621 if (!is_paging(vcpu)) {
1622 context->gva_to_gpa = nonpaging_gva_to_gpa;
1623 context->root_level = 0;
1624 } else if (is_long_mode(vcpu)) {
1625 context->gva_to_gpa = paging64_gva_to_gpa;
1626 context->root_level = PT64_ROOT_LEVEL;
1627 } else if (is_pae(vcpu)) {
1628 context->gva_to_gpa = paging64_gva_to_gpa;
1629 context->root_level = PT32E_ROOT_LEVEL;
1631 context->gva_to_gpa = paging32_gva_to_gpa;
1632 context->root_level = PT32_ROOT_LEVEL;
1638 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
1641 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1643 if (!is_paging(vcpu))
1644 return nonpaging_init_context(vcpu);
1645 else if (is_long_mode(vcpu))
1646 return paging64_init_context(vcpu);
1647 else if (is_pae(vcpu))
1648 return paging32E_init_context(vcpu);
1650 return paging32_init_context(vcpu);
1653 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1655 vcpu->arch.update_pte.pfn = bad_pfn;
1658 return init_kvm_tdp_mmu(vcpu);
1660 return init_kvm_softmmu(vcpu);
1663 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1666 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1667 vcpu->arch.mmu.free(vcpu);
1668 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1672 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1674 destroy_kvm_mmu(vcpu);
1675 return init_kvm_mmu(vcpu);
1677 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
1679 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1683 r = mmu_topup_memory_caches(vcpu);
1686 spin_lock(&vcpu->kvm->mmu_lock);
1687 kvm_mmu_free_some_pages(vcpu);
1688 mmu_alloc_roots(vcpu);
1689 spin_unlock(&vcpu->kvm->mmu_lock);
1690 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
1691 kvm_mmu_flush_tlb(vcpu);
1695 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1697 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1699 mmu_free_roots(vcpu);
1702 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1703 struct kvm_mmu_page *sp,
1707 struct kvm_mmu_page *child;
1710 if (is_shadow_present_pte(pte)) {
1711 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1713 rmap_remove(vcpu->kvm, spte);
1715 child = page_header(pte & PT64_BASE_ADDR_MASK);
1716 mmu_page_remove_parent_pte(child, spte);
1719 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
1720 if (is_large_pte(pte))
1721 --vcpu->kvm->stat.lpages;
1724 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1725 struct kvm_mmu_page *sp,
1729 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1730 if (!vcpu->arch.update_pte.largepage ||
1731 sp->role.glevels == PT32_ROOT_LEVEL) {
1732 ++vcpu->kvm->stat.mmu_pde_zapped;
1737 ++vcpu->kvm->stat.mmu_pte_updated;
1738 if (sp->role.glevels == PT32_ROOT_LEVEL)
1739 paging32_update_pte(vcpu, sp, spte, new);
1741 paging64_update_pte(vcpu, sp, spte, new);
1744 static bool need_remote_flush(u64 old, u64 new)
1746 if (!is_shadow_present_pte(old))
1748 if (!is_shadow_present_pte(new))
1750 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1752 old ^= PT64_NX_MASK;
1753 new ^= PT64_NX_MASK;
1754 return (old & ~new & PT64_PERM_MASK) != 0;
1757 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1759 if (need_remote_flush(old, new))
1760 kvm_flush_remote_tlbs(vcpu->kvm);
1762 kvm_mmu_flush_tlb(vcpu);
1765 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1767 u64 *spte = vcpu->arch.last_pte_updated;
1769 return !!(spte && (*spte & shadow_accessed_mask));
1772 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1773 const u8 *new, int bytes)
1780 vcpu->arch.update_pte.largepage = 0;
1782 if (bytes != 4 && bytes != 8)
1786 * Assume that the pte write on a page table of the same type
1787 * as the current vcpu paging mode. This is nearly always true
1788 * (might be false while changing modes). Note it is verified later
1792 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1793 if ((bytes == 4) && (gpa % 4 == 0)) {
1794 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1797 memcpy((void *)&gpte + (gpa % 8), new, 4);
1798 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1799 memcpy((void *)&gpte, new, 8);
1802 if ((bytes == 4) && (gpa % 4 == 0))
1803 memcpy((void *)&gpte, new, 4);
1805 if (!is_present_pte(gpte))
1807 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1809 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1810 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1811 vcpu->arch.update_pte.largepage = 1;
1813 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
1815 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1817 if (is_error_pfn(pfn)) {
1818 kvm_release_pfn_clean(pfn);
1821 vcpu->arch.update_pte.gfn = gfn;
1822 vcpu->arch.update_pte.pfn = pfn;
1825 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1827 u64 *spte = vcpu->arch.last_pte_updated;
1830 && vcpu->arch.last_pte_gfn == gfn
1831 && shadow_accessed_mask
1832 && !(*spte & shadow_accessed_mask)
1833 && is_shadow_present_pte(*spte))
1834 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1837 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1838 const u8 *new, int bytes)
1840 gfn_t gfn = gpa >> PAGE_SHIFT;
1841 struct kvm_mmu_page *sp;
1842 struct hlist_node *node, *n;
1843 struct hlist_head *bucket;
1847 unsigned offset = offset_in_page(gpa);
1849 unsigned page_offset;
1850 unsigned misaligned;
1857 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
1858 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
1859 spin_lock(&vcpu->kvm->mmu_lock);
1860 kvm_mmu_access_page(vcpu, gfn);
1861 kvm_mmu_free_some_pages(vcpu);
1862 ++vcpu->kvm->stat.mmu_pte_write;
1863 kvm_mmu_audit(vcpu, "pre pte write");
1864 if (gfn == vcpu->arch.last_pt_write_gfn
1865 && !last_updated_pte_accessed(vcpu)) {
1866 ++vcpu->arch.last_pt_write_count;
1867 if (vcpu->arch.last_pt_write_count >= 3)
1870 vcpu->arch.last_pt_write_gfn = gfn;
1871 vcpu->arch.last_pt_write_count = 1;
1872 vcpu->arch.last_pte_updated = NULL;
1874 index = kvm_page_table_hashfn(gfn);
1875 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1876 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1877 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
1879 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1880 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1881 misaligned |= bytes < 4;
1882 if (misaligned || flooded) {
1884 * Misaligned accesses are too much trouble to fix
1885 * up; also, they usually indicate a page is not used
1888 * If we're seeing too many writes to a page,
1889 * it may no longer be a page table, or we may be
1890 * forking, in which case it is better to unmap the
1893 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1894 gpa, bytes, sp->role.word);
1895 kvm_mmu_zap_page(vcpu->kvm, sp);
1896 ++vcpu->kvm->stat.mmu_flooded;
1899 page_offset = offset;
1900 level = sp->role.level;
1902 if (sp->role.glevels == PT32_ROOT_LEVEL) {
1903 page_offset <<= 1; /* 32->64 */
1905 * A 32-bit pde maps 4MB while the shadow pdes map
1906 * only 2MB. So we need to double the offset again
1907 * and zap two pdes instead of one.
1909 if (level == PT32_ROOT_LEVEL) {
1910 page_offset &= ~7; /* kill rounding error */
1914 quadrant = page_offset >> PAGE_SHIFT;
1915 page_offset &= ~PAGE_MASK;
1916 if (quadrant != sp->role.quadrant)
1919 spte = &sp->spt[page_offset / sizeof(*spte)];
1920 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1922 r = kvm_read_guest_atomic(vcpu->kvm,
1923 gpa & ~(u64)(pte_size - 1),
1925 new = (const void *)&gentry;
1931 mmu_pte_write_zap_pte(vcpu, sp, spte);
1933 mmu_pte_write_new_pte(vcpu, sp, spte, new);
1934 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
1938 kvm_mmu_audit(vcpu, "post pte write");
1939 spin_unlock(&vcpu->kvm->mmu_lock);
1940 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1941 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1942 vcpu->arch.update_pte.pfn = bad_pfn;
1946 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1951 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1953 spin_lock(&vcpu->kvm->mmu_lock);
1954 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1955 spin_unlock(&vcpu->kvm->mmu_lock);
1958 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
1960 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1962 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
1963 struct kvm_mmu_page *sp;
1965 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
1966 struct kvm_mmu_page, link);
1967 kvm_mmu_zap_page(vcpu->kvm, sp);
1968 ++vcpu->kvm->stat.mmu_recycled;
1972 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1975 enum emulation_result er;
1977 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
1986 r = mmu_topup_memory_caches(vcpu);
1990 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1995 case EMULATE_DO_MMIO:
1996 ++vcpu->stat.mmio_exits;
1999 kvm_report_emulation_failure(vcpu, "pagetable");
2007 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2009 void kvm_enable_tdp(void)
2013 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2015 void kvm_disable_tdp(void)
2017 tdp_enabled = false;
2019 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2021 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2023 struct kvm_mmu_page *sp;
2025 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2026 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
2027 struct kvm_mmu_page, link);
2028 kvm_mmu_zap_page(vcpu->kvm, sp);
2031 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2034 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2041 if (vcpu->kvm->arch.n_requested_mmu_pages)
2042 vcpu->kvm->arch.n_free_mmu_pages =
2043 vcpu->kvm->arch.n_requested_mmu_pages;
2045 vcpu->kvm->arch.n_free_mmu_pages =
2046 vcpu->kvm->arch.n_alloc_mmu_pages;
2048 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2049 * Therefore we need to allocate shadow page tables in the first
2050 * 4GB of memory, which happens to fit the DMA32 zone.
2052 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2055 vcpu->arch.mmu.pae_root = page_address(page);
2056 for (i = 0; i < 4; ++i)
2057 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2062 free_mmu_pages(vcpu);
2066 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2069 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2071 return alloc_mmu_pages(vcpu);
2074 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2077 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2079 return init_kvm_mmu(vcpu);
2082 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2086 destroy_kvm_mmu(vcpu);
2087 free_mmu_pages(vcpu);
2088 mmu_free_memory_caches(vcpu);
2091 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2093 struct kvm_mmu_page *sp;
2095 spin_lock(&kvm->mmu_lock);
2096 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2100 if (!test_bit(slot, &sp->slot_bitmap))
2104 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2106 if (pt[i] & PT_WRITABLE_MASK)
2107 pt[i] &= ~PT_WRITABLE_MASK;
2109 kvm_flush_remote_tlbs(kvm);
2110 spin_unlock(&kvm->mmu_lock);
2113 void kvm_mmu_zap_all(struct kvm *kvm)
2115 struct kvm_mmu_page *sp, *node;
2117 spin_lock(&kvm->mmu_lock);
2118 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2119 kvm_mmu_zap_page(kvm, sp);
2120 spin_unlock(&kvm->mmu_lock);
2122 kvm_flush_remote_tlbs(kvm);
2125 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2127 struct kvm_mmu_page *page;
2129 page = container_of(kvm->arch.active_mmu_pages.prev,
2130 struct kvm_mmu_page, link);
2131 kvm_mmu_zap_page(kvm, page);
2134 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2137 struct kvm *kvm_freed = NULL;
2138 int cache_count = 0;
2140 spin_lock(&kvm_lock);
2142 list_for_each_entry(kvm, &vm_list, vm_list) {
2145 if (!down_read_trylock(&kvm->slots_lock))
2147 spin_lock(&kvm->mmu_lock);
2148 npages = kvm->arch.n_alloc_mmu_pages -
2149 kvm->arch.n_free_mmu_pages;
2150 cache_count += npages;
2151 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2152 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2158 spin_unlock(&kvm->mmu_lock);
2159 up_read(&kvm->slots_lock);
2162 list_move_tail(&kvm_freed->vm_list, &vm_list);
2164 spin_unlock(&kvm_lock);
2169 static struct shrinker mmu_shrinker = {
2170 .shrink = mmu_shrink,
2171 .seeks = DEFAULT_SEEKS * 10,
2174 static void mmu_destroy_caches(void)
2176 if (pte_chain_cache)
2177 kmem_cache_destroy(pte_chain_cache);
2178 if (rmap_desc_cache)
2179 kmem_cache_destroy(rmap_desc_cache);
2180 if (mmu_page_header_cache)
2181 kmem_cache_destroy(mmu_page_header_cache);
2184 void kvm_mmu_module_exit(void)
2186 mmu_destroy_caches();
2187 unregister_shrinker(&mmu_shrinker);
2190 int kvm_mmu_module_init(void)
2192 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2193 sizeof(struct kvm_pte_chain),
2195 if (!pte_chain_cache)
2197 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2198 sizeof(struct kvm_rmap_desc),
2200 if (!rmap_desc_cache)
2203 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2204 sizeof(struct kvm_mmu_page),
2206 if (!mmu_page_header_cache)
2209 register_shrinker(&mmu_shrinker);
2214 mmu_destroy_caches();
2219 * Caculate mmu pages needed for kvm.
2221 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2224 unsigned int nr_mmu_pages;
2225 unsigned int nr_pages = 0;
2227 for (i = 0; i < kvm->nmemslots; i++)
2228 nr_pages += kvm->memslots[i].npages;
2230 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2231 nr_mmu_pages = max(nr_mmu_pages,
2232 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2234 return nr_mmu_pages;
2237 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2240 if (len > buffer->len)
2245 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2250 ret = pv_mmu_peek_buffer(buffer, len);
2255 buffer->processed += len;
2259 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2260 gpa_t addr, gpa_t value)
2265 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2268 r = mmu_topup_memory_caches(vcpu);
2272 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2278 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2280 kvm_x86_ops->tlb_flush(vcpu);
2284 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2286 spin_lock(&vcpu->kvm->mmu_lock);
2287 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2288 spin_unlock(&vcpu->kvm->mmu_lock);
2292 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2293 struct kvm_pv_mmu_op_buffer *buffer)
2295 struct kvm_mmu_op_header *header;
2297 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2300 switch (header->op) {
2301 case KVM_MMU_OP_WRITE_PTE: {
2302 struct kvm_mmu_op_write_pte *wpte;
2304 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2307 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2310 case KVM_MMU_OP_FLUSH_TLB: {
2311 struct kvm_mmu_op_flush_tlb *ftlb;
2313 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2316 return kvm_pv_mmu_flush_tlb(vcpu);
2318 case KVM_MMU_OP_RELEASE_PT: {
2319 struct kvm_mmu_op_release_pt *rpt;
2321 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2324 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2330 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2331 gpa_t addr, unsigned long *ret)
2334 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2336 buffer->ptr = buffer->buf;
2337 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2338 buffer->processed = 0;
2340 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2344 while (buffer->len) {
2345 r = kvm_pv_mmu_op_one(vcpu, buffer);
2354 *ret = buffer->processed;
2360 static const char *audit_msg;
2362 static gva_t canonicalize(gva_t gva)
2364 #ifdef CONFIG_X86_64
2365 gva = (long long)(gva << 16) >> 16;
2370 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2371 gva_t va, int level)
2373 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2375 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2377 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2380 if (ent == shadow_trap_nonpresent_pte)
2383 va = canonicalize(va);
2385 if (ent == shadow_notrap_nonpresent_pte)
2386 printk(KERN_ERR "audit: (%s) nontrapping pte"
2387 " in nonleaf level: levels %d gva %lx"
2388 " level %d pte %llx\n", audit_msg,
2389 vcpu->arch.mmu.root_level, va, level, ent);
2391 audit_mappings_page(vcpu, ent, va, level - 1);
2393 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
2394 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
2396 if (is_shadow_present_pte(ent)
2397 && (ent & PT64_BASE_ADDR_MASK) != hpa)
2398 printk(KERN_ERR "xx audit error: (%s) levels %d"
2399 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2400 audit_msg, vcpu->arch.mmu.root_level,
2402 is_shadow_present_pte(ent));
2403 else if (ent == shadow_notrap_nonpresent_pte
2404 && !is_error_hpa(hpa))
2405 printk(KERN_ERR "audit: (%s) notrap shadow,"
2406 " valid guest gva %lx\n", audit_msg, va);
2407 kvm_release_pfn_clean(pfn);
2413 static void audit_mappings(struct kvm_vcpu *vcpu)
2417 if (vcpu->arch.mmu.root_level == 4)
2418 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
2420 for (i = 0; i < 4; ++i)
2421 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
2422 audit_mappings_page(vcpu,
2423 vcpu->arch.mmu.pae_root[i],
2428 static int count_rmaps(struct kvm_vcpu *vcpu)
2433 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2434 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2435 struct kvm_rmap_desc *d;
2437 for (j = 0; j < m->npages; ++j) {
2438 unsigned long *rmapp = &m->rmap[j];
2442 if (!(*rmapp & 1)) {
2446 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
2448 for (k = 0; k < RMAP_EXT; ++k)
2449 if (d->shadow_ptes[k])
2460 static int count_writable_mappings(struct kvm_vcpu *vcpu)
2463 struct kvm_mmu_page *sp;
2466 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2469 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
2472 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2475 if (!(ent & PT_PRESENT_MASK))
2477 if (!(ent & PT_WRITABLE_MASK))
2485 static void audit_rmap(struct kvm_vcpu *vcpu)
2487 int n_rmap = count_rmaps(vcpu);
2488 int n_actual = count_writable_mappings(vcpu);
2490 if (n_rmap != n_actual)
2491 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2492 __func__, audit_msg, n_rmap, n_actual);
2495 static void audit_write_protection(struct kvm_vcpu *vcpu)
2497 struct kvm_mmu_page *sp;
2498 struct kvm_memory_slot *slot;
2499 unsigned long *rmapp;
2502 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2503 if (sp->role.metaphysical)
2506 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2507 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
2508 rmapp = &slot->rmap[gfn - slot->base_gfn];
2510 printk(KERN_ERR "%s: (%s) shadow page has writable"
2511 " mappings: gfn %lx role %x\n",
2512 __func__, audit_msg, sp->gfn,
2517 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2524 audit_write_protection(vcpu);
2525 audit_mappings(vcpu);