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;
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 /* always return old for EPT */
715 if (!shadow_accessed_mask)
718 spte = rmap_next(kvm, rmapp, NULL);
722 BUG_ON(!(_spte & PT_PRESENT_MASK));
723 _young = _spte & PT_ACCESSED_MASK;
726 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
728 spte = rmap_next(kvm, rmapp, spte);
733 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
735 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
739 static int is_empty_shadow_page(u64 *spt)
744 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
745 if (is_shadow_present_pte(*pos)) {
746 printk(KERN_ERR "%s: %p %llx\n", __func__,
754 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
756 ASSERT(is_empty_shadow_page(sp->spt));
758 __free_page(virt_to_page(sp->spt));
759 __free_page(virt_to_page(sp->gfns));
761 ++kvm->arch.n_free_mmu_pages;
764 static unsigned kvm_page_table_hashfn(gfn_t gfn)
766 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
769 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
772 struct kvm_mmu_page *sp;
774 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
775 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
776 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
777 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
778 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
779 ASSERT(is_empty_shadow_page(sp->spt));
782 sp->parent_pte = parent_pte;
783 --vcpu->kvm->arch.n_free_mmu_pages;
787 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
788 struct kvm_mmu_page *sp, u64 *parent_pte)
790 struct kvm_pte_chain *pte_chain;
791 struct hlist_node *node;
796 if (!sp->multimapped) {
797 u64 *old = sp->parent_pte;
800 sp->parent_pte = parent_pte;
804 pte_chain = mmu_alloc_pte_chain(vcpu);
805 INIT_HLIST_HEAD(&sp->parent_ptes);
806 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
807 pte_chain->parent_ptes[0] = old;
809 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
810 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
812 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
813 if (!pte_chain->parent_ptes[i]) {
814 pte_chain->parent_ptes[i] = parent_pte;
818 pte_chain = mmu_alloc_pte_chain(vcpu);
820 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
821 pte_chain->parent_ptes[0] = parent_pte;
824 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
827 struct kvm_pte_chain *pte_chain;
828 struct hlist_node *node;
831 if (!sp->multimapped) {
832 BUG_ON(sp->parent_pte != parent_pte);
833 sp->parent_pte = NULL;
836 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
837 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
838 if (!pte_chain->parent_ptes[i])
840 if (pte_chain->parent_ptes[i] != parent_pte)
842 while (i + 1 < NR_PTE_CHAIN_ENTRIES
843 && pte_chain->parent_ptes[i + 1]) {
844 pte_chain->parent_ptes[i]
845 = pte_chain->parent_ptes[i + 1];
848 pte_chain->parent_ptes[i] = NULL;
850 hlist_del(&pte_chain->link);
851 mmu_free_pte_chain(pte_chain);
852 if (hlist_empty(&sp->parent_ptes)) {
854 sp->parent_pte = NULL;
862 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
863 struct kvm_mmu_page *sp)
867 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
868 sp->spt[i] = shadow_trap_nonpresent_pte;
871 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
874 struct hlist_head *bucket;
875 struct kvm_mmu_page *sp;
876 struct hlist_node *node;
878 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
879 index = kvm_page_table_hashfn(gfn);
880 bucket = &kvm->arch.mmu_page_hash[index];
881 hlist_for_each_entry(sp, node, bucket, hash_link)
882 if (sp->gfn == gfn && !sp->role.metaphysical
883 && !sp->role.invalid) {
884 pgprintk("%s: found role %x\n",
885 __func__, sp->role.word);
891 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
899 union kvm_mmu_page_role role;
902 struct hlist_head *bucket;
903 struct kvm_mmu_page *sp;
904 struct hlist_node *node;
907 role.glevels = vcpu->arch.mmu.root_level;
909 role.metaphysical = metaphysical;
910 role.access = access;
911 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
912 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
913 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
914 role.quadrant = quadrant;
916 pgprintk("%s: looking gfn %lx role %x\n", __func__,
918 index = kvm_page_table_hashfn(gfn);
919 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
920 hlist_for_each_entry(sp, node, bucket, hash_link)
921 if (sp->gfn == gfn && sp->role.word == role.word) {
922 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
923 pgprintk("%s: found\n", __func__);
926 ++vcpu->kvm->stat.mmu_cache_miss;
927 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
930 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
933 hlist_add_head(&sp->hash_link, bucket);
935 rmap_write_protect(vcpu->kvm, gfn);
936 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
937 vcpu->arch.mmu.prefetch_page(vcpu, sp);
939 nonpaging_prefetch_page(vcpu, sp);
943 static int walk_shadow(struct kvm_shadow_walk *walker,
944 struct kvm_vcpu *vcpu, u64 addr)
952 shadow_addr = vcpu->arch.mmu.root_hpa;
953 level = vcpu->arch.mmu.shadow_root_level;
954 if (level == PT32E_ROOT_LEVEL) {
955 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
956 shadow_addr &= PT64_BASE_ADDR_MASK;
960 while (level >= PT_PAGE_TABLE_LEVEL) {
961 index = SHADOW_PT_INDEX(addr, level);
962 sptep = ((u64 *)__va(shadow_addr)) + index;
963 r = walker->entry(walker, vcpu, addr, sptep, level);
966 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
972 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
973 struct kvm_mmu_page *sp)
981 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
982 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
983 if (is_shadow_present_pte(pt[i]))
984 rmap_remove(kvm, &pt[i]);
985 pt[i] = shadow_trap_nonpresent_pte;
990 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
993 if (is_shadow_present_pte(ent)) {
994 if (!is_large_pte(ent)) {
995 ent &= PT64_BASE_ADDR_MASK;
996 mmu_page_remove_parent_pte(page_header(ent),
1000 rmap_remove(kvm, &pt[i]);
1003 pt[i] = shadow_trap_nonpresent_pte;
1007 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1009 mmu_page_remove_parent_pte(sp, parent_pte);
1012 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1016 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1018 kvm->vcpus[i]->arch.last_pte_updated = NULL;
1021 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1025 while (sp->multimapped || sp->parent_pte) {
1026 if (!sp->multimapped)
1027 parent_pte = sp->parent_pte;
1029 struct kvm_pte_chain *chain;
1031 chain = container_of(sp->parent_ptes.first,
1032 struct kvm_pte_chain, link);
1033 parent_pte = chain->parent_ptes[0];
1035 BUG_ON(!parent_pte);
1036 kvm_mmu_put_page(sp, parent_pte);
1037 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1041 static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1043 ++kvm->stat.mmu_shadow_zapped;
1044 kvm_mmu_page_unlink_children(kvm, sp);
1045 kvm_mmu_unlink_parents(kvm, sp);
1046 kvm_flush_remote_tlbs(kvm);
1047 if (!sp->role.invalid && !sp->role.metaphysical)
1048 unaccount_shadowed(kvm, sp->gfn);
1049 if (!sp->root_count) {
1050 hlist_del(&sp->hash_link);
1051 kvm_mmu_free_page(kvm, sp);
1053 sp->role.invalid = 1;
1054 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1055 kvm_reload_remote_mmus(kvm);
1057 kvm_mmu_reset_last_pte_updated(kvm);
1061 * Changing the number of mmu pages allocated to the vm
1062 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1064 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1067 * If we set the number of mmu pages to be smaller be than the
1068 * number of actived pages , we must to free some mmu pages before we
1072 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
1074 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1075 - kvm->arch.n_free_mmu_pages;
1077 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1078 struct kvm_mmu_page *page;
1080 page = container_of(kvm->arch.active_mmu_pages.prev,
1081 struct kvm_mmu_page, link);
1082 kvm_mmu_zap_page(kvm, page);
1085 kvm->arch.n_free_mmu_pages = 0;
1088 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1089 - kvm->arch.n_alloc_mmu_pages;
1091 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1094 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1097 struct hlist_head *bucket;
1098 struct kvm_mmu_page *sp;
1099 struct hlist_node *node, *n;
1102 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1104 index = kvm_page_table_hashfn(gfn);
1105 bucket = &kvm->arch.mmu_page_hash[index];
1106 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1107 if (sp->gfn == gfn && !sp->role.metaphysical) {
1108 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1110 kvm_mmu_zap_page(kvm, sp);
1116 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1118 struct kvm_mmu_page *sp;
1120 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
1121 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
1122 kvm_mmu_zap_page(kvm, sp);
1126 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1128 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1129 struct kvm_mmu_page *sp = page_header(__pa(pte));
1131 __set_bit(slot, &sp->slot_bitmap);
1134 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1138 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1140 if (gpa == UNMAPPED_GVA)
1143 down_read(¤t->mm->mmap_sem);
1144 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1145 up_read(¤t->mm->mmap_sem);
1150 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1151 unsigned pt_access, unsigned pte_access,
1152 int user_fault, int write_fault, int dirty,
1153 int *ptwrite, int largepage, gfn_t gfn,
1154 pfn_t pfn, bool speculative)
1157 int was_rmapped = 0;
1158 int was_writeble = is_writeble_pte(*shadow_pte);
1160 pgprintk("%s: spte %llx access %x write_fault %d"
1161 " user_fault %d gfn %lx\n",
1162 __func__, *shadow_pte, pt_access,
1163 write_fault, user_fault, gfn);
1165 if (is_rmap_pte(*shadow_pte)) {
1167 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1168 * the parent of the now unreachable PTE.
1170 if (largepage && !is_large_pte(*shadow_pte)) {
1171 struct kvm_mmu_page *child;
1172 u64 pte = *shadow_pte;
1174 child = page_header(pte & PT64_BASE_ADDR_MASK);
1175 mmu_page_remove_parent_pte(child, shadow_pte);
1176 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1177 pgprintk("hfn old %lx new %lx\n",
1178 spte_to_pfn(*shadow_pte), pfn);
1179 rmap_remove(vcpu->kvm, shadow_pte);
1182 was_rmapped = is_large_pte(*shadow_pte);
1189 * We don't set the accessed bit, since we sometimes want to see
1190 * whether the guest actually used the pte (in order to detect
1193 spte = shadow_base_present_pte | shadow_dirty_mask;
1195 spte |= shadow_accessed_mask;
1197 pte_access &= ~ACC_WRITE_MASK;
1198 if (pte_access & ACC_EXEC_MASK)
1199 spte |= shadow_x_mask;
1201 spte |= shadow_nx_mask;
1202 if (pte_access & ACC_USER_MASK)
1203 spte |= shadow_user_mask;
1205 spte |= PT_PAGE_SIZE_MASK;
1207 spte |= (u64)pfn << PAGE_SHIFT;
1209 if ((pte_access & ACC_WRITE_MASK)
1210 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1211 struct kvm_mmu_page *shadow;
1213 spte |= PT_WRITABLE_MASK;
1215 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1217 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
1218 pgprintk("%s: found shadow page for %lx, marking ro\n",
1220 pte_access &= ~ACC_WRITE_MASK;
1221 if (is_writeble_pte(spte)) {
1222 spte &= ~PT_WRITABLE_MASK;
1223 kvm_x86_ops->tlb_flush(vcpu);
1230 if (pte_access & ACC_WRITE_MASK)
1231 mark_page_dirty(vcpu->kvm, gfn);
1233 pgprintk("%s: setting spte %llx\n", __func__, spte);
1234 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1235 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1236 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
1237 set_shadow_pte(shadow_pte, spte);
1238 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1239 && (spte & PT_PRESENT_MASK))
1240 ++vcpu->kvm->stat.lpages;
1242 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1244 rmap_add(vcpu, shadow_pte, gfn, largepage);
1245 if (!is_rmap_pte(*shadow_pte))
1246 kvm_release_pfn_clean(pfn);
1249 kvm_release_pfn_dirty(pfn);
1251 kvm_release_pfn_clean(pfn);
1254 vcpu->arch.last_pte_updated = shadow_pte;
1255 vcpu->arch.last_pte_gfn = gfn;
1259 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1263 struct direct_shadow_walk {
1264 struct kvm_shadow_walk walker;
1271 static int direct_map_entry(struct kvm_shadow_walk *_walk,
1272 struct kvm_vcpu *vcpu,
1273 u64 addr, u64 *sptep, int level)
1275 struct direct_shadow_walk *walk =
1276 container_of(_walk, struct direct_shadow_walk, walker);
1277 struct kvm_mmu_page *sp;
1279 gfn_t gfn = addr >> PAGE_SHIFT;
1281 if (level == PT_PAGE_TABLE_LEVEL
1282 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1283 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1284 0, walk->write, 1, &walk->pt_write,
1285 walk->largepage, gfn, walk->pfn, false);
1286 ++vcpu->stat.pf_fixed;
1290 if (*sptep == shadow_trap_nonpresent_pte) {
1291 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1292 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
1295 pgprintk("nonpaging_map: ENOMEM\n");
1296 kvm_release_pfn_clean(walk->pfn);
1300 set_shadow_pte(sptep,
1302 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1303 | shadow_user_mask | shadow_x_mask);
1308 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1309 int largepage, gfn_t gfn, pfn_t pfn)
1312 struct direct_shadow_walk walker = {
1313 .walker = { .entry = direct_map_entry, },
1315 .largepage = largepage,
1320 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
1323 return walker.pt_write;
1326 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1331 unsigned long mmu_seq;
1333 down_read(¤t->mm->mmap_sem);
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;
1340 /* implicit mb(), we'll read before PT lock is unlocked */
1341 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1342 up_read(¤t->mm->mmap_sem);
1345 if (is_error_pfn(pfn)) {
1346 kvm_release_pfn_clean(pfn);
1350 spin_lock(&vcpu->kvm->mmu_lock);
1351 if (mmu_notifier_retry(vcpu, mmu_seq))
1353 kvm_mmu_free_some_pages(vcpu);
1354 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1355 spin_unlock(&vcpu->kvm->mmu_lock);
1361 spin_unlock(&vcpu->kvm->mmu_lock);
1362 kvm_release_pfn_clean(pfn);
1367 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1370 struct kvm_mmu_page *sp;
1372 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1374 spin_lock(&vcpu->kvm->mmu_lock);
1375 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1376 hpa_t root = vcpu->arch.mmu.root_hpa;
1378 sp = page_header(root);
1380 if (!sp->root_count && sp->role.invalid)
1381 kvm_mmu_zap_page(vcpu->kvm, sp);
1382 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1383 spin_unlock(&vcpu->kvm->mmu_lock);
1386 for (i = 0; i < 4; ++i) {
1387 hpa_t root = vcpu->arch.mmu.pae_root[i];
1390 root &= PT64_BASE_ADDR_MASK;
1391 sp = page_header(root);
1393 if (!sp->root_count && sp->role.invalid)
1394 kvm_mmu_zap_page(vcpu->kvm, sp);
1396 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1398 spin_unlock(&vcpu->kvm->mmu_lock);
1399 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1402 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1406 struct kvm_mmu_page *sp;
1407 int metaphysical = 0;
1409 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1411 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1412 hpa_t root = vcpu->arch.mmu.root_hpa;
1414 ASSERT(!VALID_PAGE(root));
1417 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1418 PT64_ROOT_LEVEL, metaphysical,
1420 root = __pa(sp->spt);
1422 vcpu->arch.mmu.root_hpa = root;
1425 metaphysical = !is_paging(vcpu);
1428 for (i = 0; i < 4; ++i) {
1429 hpa_t root = vcpu->arch.mmu.pae_root[i];
1431 ASSERT(!VALID_PAGE(root));
1432 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1433 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1434 vcpu->arch.mmu.pae_root[i] = 0;
1437 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1438 } else if (vcpu->arch.mmu.root_level == 0)
1440 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1441 PT32_ROOT_LEVEL, metaphysical,
1443 root = __pa(sp->spt);
1445 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1447 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1450 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1455 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1461 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
1462 r = mmu_topup_memory_caches(vcpu);
1467 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1469 gfn = gva >> PAGE_SHIFT;
1471 return nonpaging_map(vcpu, gva & PAGE_MASK,
1472 error_code & PFERR_WRITE_MASK, gfn);
1475 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1481 gfn_t gfn = gpa >> PAGE_SHIFT;
1482 unsigned long mmu_seq;
1485 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1487 r = mmu_topup_memory_caches(vcpu);
1491 down_read(¤t->mm->mmap_sem);
1492 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1493 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1496 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1497 /* implicit mb(), we'll read before PT lock is unlocked */
1498 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1499 up_read(¤t->mm->mmap_sem);
1500 if (is_error_pfn(pfn)) {
1501 kvm_release_pfn_clean(pfn);
1504 spin_lock(&vcpu->kvm->mmu_lock);
1505 if (mmu_notifier_retry(vcpu, mmu_seq))
1507 kvm_mmu_free_some_pages(vcpu);
1508 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1509 largepage, gfn, pfn);
1510 spin_unlock(&vcpu->kvm->mmu_lock);
1515 spin_unlock(&vcpu->kvm->mmu_lock);
1516 kvm_release_pfn_clean(pfn);
1520 static void nonpaging_free(struct kvm_vcpu *vcpu)
1522 mmu_free_roots(vcpu);
1525 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1527 struct kvm_mmu *context = &vcpu->arch.mmu;
1529 context->new_cr3 = nonpaging_new_cr3;
1530 context->page_fault = nonpaging_page_fault;
1531 context->gva_to_gpa = nonpaging_gva_to_gpa;
1532 context->free = nonpaging_free;
1533 context->prefetch_page = nonpaging_prefetch_page;
1534 context->root_level = 0;
1535 context->shadow_root_level = PT32E_ROOT_LEVEL;
1536 context->root_hpa = INVALID_PAGE;
1540 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1542 ++vcpu->stat.tlb_flush;
1543 kvm_x86_ops->tlb_flush(vcpu);
1546 static void paging_new_cr3(struct kvm_vcpu *vcpu)
1548 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
1549 mmu_free_roots(vcpu);
1552 static void inject_page_fault(struct kvm_vcpu *vcpu,
1556 kvm_inject_page_fault(vcpu, addr, err_code);
1559 static void paging_free(struct kvm_vcpu *vcpu)
1561 nonpaging_free(vcpu);
1565 #include "paging_tmpl.h"
1569 #include "paging_tmpl.h"
1572 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1574 struct kvm_mmu *context = &vcpu->arch.mmu;
1576 ASSERT(is_pae(vcpu));
1577 context->new_cr3 = paging_new_cr3;
1578 context->page_fault = paging64_page_fault;
1579 context->gva_to_gpa = paging64_gva_to_gpa;
1580 context->prefetch_page = paging64_prefetch_page;
1581 context->free = paging_free;
1582 context->root_level = level;
1583 context->shadow_root_level = level;
1584 context->root_hpa = INVALID_PAGE;
1588 static int paging64_init_context(struct kvm_vcpu *vcpu)
1590 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1593 static int paging32_init_context(struct kvm_vcpu *vcpu)
1595 struct kvm_mmu *context = &vcpu->arch.mmu;
1597 context->new_cr3 = paging_new_cr3;
1598 context->page_fault = paging32_page_fault;
1599 context->gva_to_gpa = paging32_gva_to_gpa;
1600 context->free = paging_free;
1601 context->prefetch_page = paging32_prefetch_page;
1602 context->root_level = PT32_ROOT_LEVEL;
1603 context->shadow_root_level = PT32E_ROOT_LEVEL;
1604 context->root_hpa = INVALID_PAGE;
1608 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1610 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1613 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1615 struct kvm_mmu *context = &vcpu->arch.mmu;
1617 context->new_cr3 = nonpaging_new_cr3;
1618 context->page_fault = tdp_page_fault;
1619 context->free = nonpaging_free;
1620 context->prefetch_page = nonpaging_prefetch_page;
1621 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
1622 context->root_hpa = INVALID_PAGE;
1624 if (!is_paging(vcpu)) {
1625 context->gva_to_gpa = nonpaging_gva_to_gpa;
1626 context->root_level = 0;
1627 } else if (is_long_mode(vcpu)) {
1628 context->gva_to_gpa = paging64_gva_to_gpa;
1629 context->root_level = PT64_ROOT_LEVEL;
1630 } else if (is_pae(vcpu)) {
1631 context->gva_to_gpa = paging64_gva_to_gpa;
1632 context->root_level = PT32E_ROOT_LEVEL;
1634 context->gva_to_gpa = paging32_gva_to_gpa;
1635 context->root_level = PT32_ROOT_LEVEL;
1641 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
1644 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1646 if (!is_paging(vcpu))
1647 return nonpaging_init_context(vcpu);
1648 else if (is_long_mode(vcpu))
1649 return paging64_init_context(vcpu);
1650 else if (is_pae(vcpu))
1651 return paging32E_init_context(vcpu);
1653 return paging32_init_context(vcpu);
1656 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1658 vcpu->arch.update_pte.pfn = bad_pfn;
1661 return init_kvm_tdp_mmu(vcpu);
1663 return init_kvm_softmmu(vcpu);
1666 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1669 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1670 vcpu->arch.mmu.free(vcpu);
1671 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1675 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1677 destroy_kvm_mmu(vcpu);
1678 return init_kvm_mmu(vcpu);
1680 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
1682 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1686 r = mmu_topup_memory_caches(vcpu);
1689 spin_lock(&vcpu->kvm->mmu_lock);
1690 kvm_mmu_free_some_pages(vcpu);
1691 mmu_alloc_roots(vcpu);
1692 spin_unlock(&vcpu->kvm->mmu_lock);
1693 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
1694 kvm_mmu_flush_tlb(vcpu);
1698 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1700 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1702 mmu_free_roots(vcpu);
1705 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1706 struct kvm_mmu_page *sp,
1710 struct kvm_mmu_page *child;
1713 if (is_shadow_present_pte(pte)) {
1714 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1716 rmap_remove(vcpu->kvm, spte);
1718 child = page_header(pte & PT64_BASE_ADDR_MASK);
1719 mmu_page_remove_parent_pte(child, spte);
1722 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
1723 if (is_large_pte(pte))
1724 --vcpu->kvm->stat.lpages;
1727 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1728 struct kvm_mmu_page *sp,
1732 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1733 if (!vcpu->arch.update_pte.largepage ||
1734 sp->role.glevels == PT32_ROOT_LEVEL) {
1735 ++vcpu->kvm->stat.mmu_pde_zapped;
1740 ++vcpu->kvm->stat.mmu_pte_updated;
1741 if (sp->role.glevels == PT32_ROOT_LEVEL)
1742 paging32_update_pte(vcpu, sp, spte, new);
1744 paging64_update_pte(vcpu, sp, spte, new);
1747 static bool need_remote_flush(u64 old, u64 new)
1749 if (!is_shadow_present_pte(old))
1751 if (!is_shadow_present_pte(new))
1753 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1755 old ^= PT64_NX_MASK;
1756 new ^= PT64_NX_MASK;
1757 return (old & ~new & PT64_PERM_MASK) != 0;
1760 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1762 if (need_remote_flush(old, new))
1763 kvm_flush_remote_tlbs(vcpu->kvm);
1765 kvm_mmu_flush_tlb(vcpu);
1768 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1770 u64 *spte = vcpu->arch.last_pte_updated;
1772 return !!(spte && (*spte & shadow_accessed_mask));
1775 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1776 const u8 *new, int bytes)
1783 vcpu->arch.update_pte.largepage = 0;
1785 if (bytes != 4 && bytes != 8)
1789 * Assume that the pte write on a page table of the same type
1790 * as the current vcpu paging mode. This is nearly always true
1791 * (might be false while changing modes). Note it is verified later
1795 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1796 if ((bytes == 4) && (gpa % 4 == 0)) {
1797 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1800 memcpy((void *)&gpte + (gpa % 8), new, 4);
1801 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1802 memcpy((void *)&gpte, new, 8);
1805 if ((bytes == 4) && (gpa % 4 == 0))
1806 memcpy((void *)&gpte, new, 4);
1808 if (!is_present_pte(gpte))
1810 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1812 down_read(¤t->mm->mmap_sem);
1813 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1814 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1815 vcpu->arch.update_pte.largepage = 1;
1817 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
1818 /* implicit mb(), we'll read before PT lock is unlocked */
1819 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1820 up_read(¤t->mm->mmap_sem);
1822 if (is_error_pfn(pfn)) {
1823 kvm_release_pfn_clean(pfn);
1826 vcpu->arch.update_pte.gfn = gfn;
1827 vcpu->arch.update_pte.pfn = pfn;
1830 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1832 u64 *spte = vcpu->arch.last_pte_updated;
1835 && vcpu->arch.last_pte_gfn == gfn
1836 && shadow_accessed_mask
1837 && !(*spte & shadow_accessed_mask)
1838 && is_shadow_present_pte(*spte))
1839 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1842 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1843 const u8 *new, int bytes)
1845 gfn_t gfn = gpa >> PAGE_SHIFT;
1846 struct kvm_mmu_page *sp;
1847 struct hlist_node *node, *n;
1848 struct hlist_head *bucket;
1852 unsigned offset = offset_in_page(gpa);
1854 unsigned page_offset;
1855 unsigned misaligned;
1862 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
1863 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
1864 spin_lock(&vcpu->kvm->mmu_lock);
1865 kvm_mmu_access_page(vcpu, gfn);
1866 kvm_mmu_free_some_pages(vcpu);
1867 ++vcpu->kvm->stat.mmu_pte_write;
1868 kvm_mmu_audit(vcpu, "pre pte write");
1869 if (gfn == vcpu->arch.last_pt_write_gfn
1870 && !last_updated_pte_accessed(vcpu)) {
1871 ++vcpu->arch.last_pt_write_count;
1872 if (vcpu->arch.last_pt_write_count >= 3)
1875 vcpu->arch.last_pt_write_gfn = gfn;
1876 vcpu->arch.last_pt_write_count = 1;
1877 vcpu->arch.last_pte_updated = NULL;
1879 index = kvm_page_table_hashfn(gfn);
1880 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1881 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1882 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
1884 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1885 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1886 misaligned |= bytes < 4;
1887 if (misaligned || flooded) {
1889 * Misaligned accesses are too much trouble to fix
1890 * up; also, they usually indicate a page is not used
1893 * If we're seeing too many writes to a page,
1894 * it may no longer be a page table, or we may be
1895 * forking, in which case it is better to unmap the
1898 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1899 gpa, bytes, sp->role.word);
1900 kvm_mmu_zap_page(vcpu->kvm, sp);
1901 ++vcpu->kvm->stat.mmu_flooded;
1904 page_offset = offset;
1905 level = sp->role.level;
1907 if (sp->role.glevels == PT32_ROOT_LEVEL) {
1908 page_offset <<= 1; /* 32->64 */
1910 * A 32-bit pde maps 4MB while the shadow pdes map
1911 * only 2MB. So we need to double the offset again
1912 * and zap two pdes instead of one.
1914 if (level == PT32_ROOT_LEVEL) {
1915 page_offset &= ~7; /* kill rounding error */
1919 quadrant = page_offset >> PAGE_SHIFT;
1920 page_offset &= ~PAGE_MASK;
1921 if (quadrant != sp->role.quadrant)
1924 spte = &sp->spt[page_offset / sizeof(*spte)];
1925 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1927 r = kvm_read_guest_atomic(vcpu->kvm,
1928 gpa & ~(u64)(pte_size - 1),
1930 new = (const void *)&gentry;
1936 mmu_pte_write_zap_pte(vcpu, sp, spte);
1938 mmu_pte_write_new_pte(vcpu, sp, spte, new);
1939 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
1943 kvm_mmu_audit(vcpu, "post pte write");
1944 spin_unlock(&vcpu->kvm->mmu_lock);
1945 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1946 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1947 vcpu->arch.update_pte.pfn = bad_pfn;
1951 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1956 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1958 spin_lock(&vcpu->kvm->mmu_lock);
1959 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1960 spin_unlock(&vcpu->kvm->mmu_lock);
1963 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
1965 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1967 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
1968 struct kvm_mmu_page *sp;
1970 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
1971 struct kvm_mmu_page, link);
1972 kvm_mmu_zap_page(vcpu->kvm, sp);
1973 ++vcpu->kvm->stat.mmu_recycled;
1977 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1980 enum emulation_result er;
1982 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
1991 r = mmu_topup_memory_caches(vcpu);
1995 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2000 case EMULATE_DO_MMIO:
2001 ++vcpu->stat.mmio_exits;
2004 kvm_report_emulation_failure(vcpu, "pagetable");
2012 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2014 void kvm_enable_tdp(void)
2018 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2020 void kvm_disable_tdp(void)
2022 tdp_enabled = false;
2024 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2026 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2028 struct kvm_mmu_page *sp;
2030 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2031 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
2032 struct kvm_mmu_page, link);
2033 kvm_mmu_zap_page(vcpu->kvm, sp);
2036 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2039 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2046 if (vcpu->kvm->arch.n_requested_mmu_pages)
2047 vcpu->kvm->arch.n_free_mmu_pages =
2048 vcpu->kvm->arch.n_requested_mmu_pages;
2050 vcpu->kvm->arch.n_free_mmu_pages =
2051 vcpu->kvm->arch.n_alloc_mmu_pages;
2053 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2054 * Therefore we need to allocate shadow page tables in the first
2055 * 4GB of memory, which happens to fit the DMA32 zone.
2057 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2060 vcpu->arch.mmu.pae_root = page_address(page);
2061 for (i = 0; i < 4; ++i)
2062 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2067 free_mmu_pages(vcpu);
2071 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2074 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2076 return alloc_mmu_pages(vcpu);
2079 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2082 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2084 return init_kvm_mmu(vcpu);
2087 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2091 destroy_kvm_mmu(vcpu);
2092 free_mmu_pages(vcpu);
2093 mmu_free_memory_caches(vcpu);
2096 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2098 struct kvm_mmu_page *sp;
2100 spin_lock(&kvm->mmu_lock);
2101 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2105 if (!test_bit(slot, &sp->slot_bitmap))
2109 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2111 if (pt[i] & PT_WRITABLE_MASK)
2112 pt[i] &= ~PT_WRITABLE_MASK;
2114 kvm_flush_remote_tlbs(kvm);
2115 spin_unlock(&kvm->mmu_lock);
2118 void kvm_mmu_zap_all(struct kvm *kvm)
2120 struct kvm_mmu_page *sp, *node;
2122 spin_lock(&kvm->mmu_lock);
2123 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2124 kvm_mmu_zap_page(kvm, sp);
2125 spin_unlock(&kvm->mmu_lock);
2127 kvm_flush_remote_tlbs(kvm);
2130 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2132 struct kvm_mmu_page *page;
2134 page = container_of(kvm->arch.active_mmu_pages.prev,
2135 struct kvm_mmu_page, link);
2136 kvm_mmu_zap_page(kvm, page);
2139 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2142 struct kvm *kvm_freed = NULL;
2143 int cache_count = 0;
2145 spin_lock(&kvm_lock);
2147 list_for_each_entry(kvm, &vm_list, vm_list) {
2150 if (!down_read_trylock(&kvm->slots_lock))
2152 spin_lock(&kvm->mmu_lock);
2153 npages = kvm->arch.n_alloc_mmu_pages -
2154 kvm->arch.n_free_mmu_pages;
2155 cache_count += npages;
2156 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2157 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2163 spin_unlock(&kvm->mmu_lock);
2164 up_read(&kvm->slots_lock);
2167 list_move_tail(&kvm_freed->vm_list, &vm_list);
2169 spin_unlock(&kvm_lock);
2174 static struct shrinker mmu_shrinker = {
2175 .shrink = mmu_shrink,
2176 .seeks = DEFAULT_SEEKS * 10,
2179 static void mmu_destroy_caches(void)
2181 if (pte_chain_cache)
2182 kmem_cache_destroy(pte_chain_cache);
2183 if (rmap_desc_cache)
2184 kmem_cache_destroy(rmap_desc_cache);
2185 if (mmu_page_header_cache)
2186 kmem_cache_destroy(mmu_page_header_cache);
2189 void kvm_mmu_module_exit(void)
2191 mmu_destroy_caches();
2192 unregister_shrinker(&mmu_shrinker);
2195 int kvm_mmu_module_init(void)
2197 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2198 sizeof(struct kvm_pte_chain),
2200 if (!pte_chain_cache)
2202 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2203 sizeof(struct kvm_rmap_desc),
2205 if (!rmap_desc_cache)
2208 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2209 sizeof(struct kvm_mmu_page),
2211 if (!mmu_page_header_cache)
2214 register_shrinker(&mmu_shrinker);
2219 mmu_destroy_caches();
2224 * Caculate mmu pages needed for kvm.
2226 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2229 unsigned int nr_mmu_pages;
2230 unsigned int nr_pages = 0;
2232 for (i = 0; i < kvm->nmemslots; i++)
2233 nr_pages += kvm->memslots[i].npages;
2235 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2236 nr_mmu_pages = max(nr_mmu_pages,
2237 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2239 return nr_mmu_pages;
2242 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2245 if (len > buffer->len)
2250 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2255 ret = pv_mmu_peek_buffer(buffer, len);
2260 buffer->processed += len;
2264 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2265 gpa_t addr, gpa_t value)
2270 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2273 r = mmu_topup_memory_caches(vcpu);
2277 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2283 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2285 kvm_x86_ops->tlb_flush(vcpu);
2289 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2291 spin_lock(&vcpu->kvm->mmu_lock);
2292 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2293 spin_unlock(&vcpu->kvm->mmu_lock);
2297 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2298 struct kvm_pv_mmu_op_buffer *buffer)
2300 struct kvm_mmu_op_header *header;
2302 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2305 switch (header->op) {
2306 case KVM_MMU_OP_WRITE_PTE: {
2307 struct kvm_mmu_op_write_pte *wpte;
2309 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2312 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2315 case KVM_MMU_OP_FLUSH_TLB: {
2316 struct kvm_mmu_op_flush_tlb *ftlb;
2318 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2321 return kvm_pv_mmu_flush_tlb(vcpu);
2323 case KVM_MMU_OP_RELEASE_PT: {
2324 struct kvm_mmu_op_release_pt *rpt;
2326 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2329 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2335 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2336 gpa_t addr, unsigned long *ret)
2339 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2341 buffer->ptr = buffer->buf;
2342 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2343 buffer->processed = 0;
2345 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2349 while (buffer->len) {
2350 r = kvm_pv_mmu_op_one(vcpu, buffer);
2359 *ret = buffer->processed;
2365 static const char *audit_msg;
2367 static gva_t canonicalize(gva_t gva)
2369 #ifdef CONFIG_X86_64
2370 gva = (long long)(gva << 16) >> 16;
2375 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2376 gva_t va, int level)
2378 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2380 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2382 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2385 if (ent == shadow_trap_nonpresent_pte)
2388 va = canonicalize(va);
2390 if (ent == shadow_notrap_nonpresent_pte)
2391 printk(KERN_ERR "audit: (%s) nontrapping pte"
2392 " in nonleaf level: levels %d gva %lx"
2393 " level %d pte %llx\n", audit_msg,
2394 vcpu->arch.mmu.root_level, va, level, ent);
2396 audit_mappings_page(vcpu, ent, va, level - 1);
2398 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
2399 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
2401 if (is_shadow_present_pte(ent)
2402 && (ent & PT64_BASE_ADDR_MASK) != hpa)
2403 printk(KERN_ERR "xx audit error: (%s) levels %d"
2404 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2405 audit_msg, vcpu->arch.mmu.root_level,
2407 is_shadow_present_pte(ent));
2408 else if (ent == shadow_notrap_nonpresent_pte
2409 && !is_error_hpa(hpa))
2410 printk(KERN_ERR "audit: (%s) notrap shadow,"
2411 " valid guest gva %lx\n", audit_msg, va);
2412 kvm_release_pfn_clean(pfn);
2418 static void audit_mappings(struct kvm_vcpu *vcpu)
2422 if (vcpu->arch.mmu.root_level == 4)
2423 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
2425 for (i = 0; i < 4; ++i)
2426 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
2427 audit_mappings_page(vcpu,
2428 vcpu->arch.mmu.pae_root[i],
2433 static int count_rmaps(struct kvm_vcpu *vcpu)
2438 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2439 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2440 struct kvm_rmap_desc *d;
2442 for (j = 0; j < m->npages; ++j) {
2443 unsigned long *rmapp = &m->rmap[j];
2447 if (!(*rmapp & 1)) {
2451 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
2453 for (k = 0; k < RMAP_EXT; ++k)
2454 if (d->shadow_ptes[k])
2465 static int count_writable_mappings(struct kvm_vcpu *vcpu)
2468 struct kvm_mmu_page *sp;
2471 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2474 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
2477 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2480 if (!(ent & PT_PRESENT_MASK))
2482 if (!(ent & PT_WRITABLE_MASK))
2490 static void audit_rmap(struct kvm_vcpu *vcpu)
2492 int n_rmap = count_rmaps(vcpu);
2493 int n_actual = count_writable_mappings(vcpu);
2495 if (n_rmap != n_actual)
2496 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2497 __func__, audit_msg, n_rmap, n_actual);
2500 static void audit_write_protection(struct kvm_vcpu *vcpu)
2502 struct kvm_mmu_page *sp;
2503 struct kvm_memory_slot *slot;
2504 unsigned long *rmapp;
2507 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2508 if (sp->role.metaphysical)
2511 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2512 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
2513 rmapp = &slot->rmap[gfn - slot->base_gfn];
2515 printk(KERN_ERR "%s: (%s) shadow page has writable"
2516 " mappings: gfn %lx role %x\n",
2517 __func__, audit_msg, sp->gfn,
2522 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2529 audit_write_protection(vcpu);
2530 audit_mappings(vcpu);