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.
21 #include "kvm_cache_regs.h"
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>
39 * When setting this variable to true it enables Two-Dimensional-Paging
40 * where the hardware walks 2 page tables:
41 * 1. the guest-virtual to guest-physical
42 * 2. while doing 1. it walks guest-physical to host-physical
43 * If the hardware supports that we don't need to do shadow paging.
45 bool tdp_enabled = false;
52 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
54 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
59 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
60 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
64 #define pgprintk(x...) do { } while (0)
65 #define rmap_printk(x...) do { } while (0)
69 #if defined(MMU_DEBUG) || defined(AUDIT)
71 module_param(dbg, bool, 0644);
74 static int oos_shadow = 1;
75 module_param(oos_shadow, bool, 0644);
78 #define ASSERT(x) do { } while (0)
82 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
83 __FILE__, __LINE__, #x); \
87 #define PT_FIRST_AVAIL_BITS_SHIFT 9
88 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
90 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
92 #define PT64_LEVEL_BITS 9
94 #define PT64_LEVEL_SHIFT(level) \
95 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
97 #define PT64_LEVEL_MASK(level) \
98 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
100 #define PT64_INDEX(address, level)\
101 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
104 #define PT32_LEVEL_BITS 10
106 #define PT32_LEVEL_SHIFT(level) \
107 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
109 #define PT32_LEVEL_MASK(level) \
110 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
112 #define PT32_INDEX(address, level)\
113 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
116 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
117 #define PT64_DIR_BASE_ADDR_MASK \
118 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
120 #define PT32_BASE_ADDR_MASK PAGE_MASK
121 #define PT32_DIR_BASE_ADDR_MASK \
122 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
124 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
127 #define PFERR_PRESENT_MASK (1U << 0)
128 #define PFERR_WRITE_MASK (1U << 1)
129 #define PFERR_USER_MASK (1U << 2)
130 #define PFERR_RSVD_MASK (1U << 3)
131 #define PFERR_FETCH_MASK (1U << 4)
133 #define PT_DIRECTORY_LEVEL 2
134 #define PT_PAGE_TABLE_LEVEL 1
138 #define ACC_EXEC_MASK 1
139 #define ACC_WRITE_MASK PT_WRITABLE_MASK
140 #define ACC_USER_MASK PT_USER_MASK
141 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
143 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
145 struct kvm_rmap_desc {
146 u64 *shadow_ptes[RMAP_EXT];
147 struct kvm_rmap_desc *more;
150 struct kvm_shadow_walk_iterator {
158 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
159 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
160 shadow_walk_okay(&(_walker)); \
161 shadow_walk_next(&(_walker)))
164 struct kvm_unsync_walk {
165 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
168 typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
170 static struct kmem_cache *pte_chain_cache;
171 static struct kmem_cache *rmap_desc_cache;
172 static struct kmem_cache *mmu_page_header_cache;
174 static u64 __read_mostly shadow_trap_nonpresent_pte;
175 static u64 __read_mostly shadow_notrap_nonpresent_pte;
176 static u64 __read_mostly shadow_base_present_pte;
177 static u64 __read_mostly shadow_nx_mask;
178 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
179 static u64 __read_mostly shadow_user_mask;
180 static u64 __read_mostly shadow_accessed_mask;
181 static u64 __read_mostly shadow_dirty_mask;
183 static inline u64 rsvd_bits(int s, int e)
185 return ((1ULL << (e - s + 1)) - 1) << s;
188 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
190 shadow_trap_nonpresent_pte = trap_pte;
191 shadow_notrap_nonpresent_pte = notrap_pte;
193 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
195 void kvm_mmu_set_base_ptes(u64 base_pte)
197 shadow_base_present_pte = base_pte;
199 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
201 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
202 u64 dirty_mask, u64 nx_mask, u64 x_mask)
204 shadow_user_mask = user_mask;
205 shadow_accessed_mask = accessed_mask;
206 shadow_dirty_mask = dirty_mask;
207 shadow_nx_mask = nx_mask;
208 shadow_x_mask = x_mask;
210 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
212 static int is_write_protection(struct kvm_vcpu *vcpu)
214 return vcpu->arch.cr0 & X86_CR0_WP;
217 static int is_cpuid_PSE36(void)
222 static int is_nx(struct kvm_vcpu *vcpu)
224 return vcpu->arch.shadow_efer & EFER_NX;
227 static int is_shadow_present_pte(u64 pte)
229 return pte != shadow_trap_nonpresent_pte
230 && pte != shadow_notrap_nonpresent_pte;
233 static int is_large_pte(u64 pte)
235 return pte & PT_PAGE_SIZE_MASK;
238 static int is_writeble_pte(unsigned long pte)
240 return pte & PT_WRITABLE_MASK;
243 static int is_dirty_pte(unsigned long pte)
245 return pte & PT_DIRTY_MASK;
248 static int is_rmap_pte(u64 pte)
250 return is_shadow_present_pte(pte);
253 static pfn_t spte_to_pfn(u64 pte)
255 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
258 static gfn_t pse36_gfn_delta(u32 gpte)
260 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
262 return (gpte & PT32_DIR_PSE36_MASK) << shift;
265 static void set_shadow_pte(u64 *sptep, u64 spte)
268 set_64bit((unsigned long *)sptep, spte);
270 set_64bit((unsigned long long *)sptep, spte);
274 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
275 struct kmem_cache *base_cache, int min)
279 if (cache->nobjs >= min)
281 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
282 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
285 cache->objects[cache->nobjs++] = obj;
290 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
293 kfree(mc->objects[--mc->nobjs]);
296 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
301 if (cache->nobjs >= min)
303 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
304 page = alloc_page(GFP_KERNEL);
307 set_page_private(page, 0);
308 cache->objects[cache->nobjs++] = page_address(page);
313 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
316 free_page((unsigned long)mc->objects[--mc->nobjs]);
319 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
323 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
327 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
331 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
334 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
335 mmu_page_header_cache, 4);
340 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
342 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
343 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
344 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
345 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
348 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
354 p = mc->objects[--mc->nobjs];
358 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
360 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
361 sizeof(struct kvm_pte_chain));
364 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
369 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
371 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
372 sizeof(struct kvm_rmap_desc));
375 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
381 * Return the pointer to the largepage write count for a given
382 * gfn, handling slots that are not large page aligned.
384 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
388 idx = (gfn / KVM_PAGES_PER_HPAGE) -
389 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
390 return &slot->lpage_info[idx].write_count;
393 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
397 gfn = unalias_gfn(kvm, gfn);
398 write_count = slot_largepage_idx(gfn,
399 gfn_to_memslot_unaliased(kvm, gfn));
403 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
407 gfn = unalias_gfn(kvm, gfn);
408 write_count = slot_largepage_idx(gfn,
409 gfn_to_memslot_unaliased(kvm, gfn));
411 WARN_ON(*write_count < 0);
414 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
416 struct kvm_memory_slot *slot;
419 gfn = unalias_gfn(kvm, gfn);
420 slot = gfn_to_memslot_unaliased(kvm, gfn);
422 largepage_idx = slot_largepage_idx(gfn, slot);
423 return *largepage_idx;
429 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
431 struct vm_area_struct *vma;
435 addr = gfn_to_hva(kvm, gfn);
436 if (kvm_is_error_hva(addr))
439 down_read(¤t->mm->mmap_sem);
440 vma = find_vma(current->mm, addr);
441 if (vma && is_vm_hugetlb_page(vma))
443 up_read(¤t->mm->mmap_sem);
448 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
450 struct kvm_memory_slot *slot;
452 if (has_wrprotected_page(vcpu->kvm, large_gfn))
455 if (!host_largepage_backed(vcpu->kvm, large_gfn))
458 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
459 if (slot && slot->dirty_bitmap)
466 * Take gfn and return the reverse mapping to it.
467 * Note: gfn must be unaliased before this function get called
470 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
472 struct kvm_memory_slot *slot;
475 slot = gfn_to_memslot(kvm, gfn);
477 return &slot->rmap[gfn - slot->base_gfn];
479 idx = (gfn / KVM_PAGES_PER_HPAGE) -
480 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
482 return &slot->lpage_info[idx].rmap_pde;
486 * Reverse mapping data structures:
488 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
489 * that points to page_address(page).
491 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
492 * containing more mappings.
494 * Returns the number of rmap entries before the spte was added or zero if
495 * the spte was not added.
498 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
500 struct kvm_mmu_page *sp;
501 struct kvm_rmap_desc *desc;
502 unsigned long *rmapp;
505 if (!is_rmap_pte(*spte))
507 gfn = unalias_gfn(vcpu->kvm, gfn);
508 sp = page_header(__pa(spte));
509 sp->gfns[spte - sp->spt] = gfn;
510 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
512 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
513 *rmapp = (unsigned long)spte;
514 } else if (!(*rmapp & 1)) {
515 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
516 desc = mmu_alloc_rmap_desc(vcpu);
517 desc->shadow_ptes[0] = (u64 *)*rmapp;
518 desc->shadow_ptes[1] = spte;
519 *rmapp = (unsigned long)desc | 1;
521 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
522 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
523 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) {
527 if (desc->shadow_ptes[RMAP_EXT-1]) {
528 desc->more = mmu_alloc_rmap_desc(vcpu);
531 for (i = 0; desc->shadow_ptes[i]; ++i)
533 desc->shadow_ptes[i] = spte;
538 static void rmap_desc_remove_entry(unsigned long *rmapp,
539 struct kvm_rmap_desc *desc,
541 struct kvm_rmap_desc *prev_desc)
545 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
547 desc->shadow_ptes[i] = desc->shadow_ptes[j];
548 desc->shadow_ptes[j] = NULL;
551 if (!prev_desc && !desc->more)
552 *rmapp = (unsigned long)desc->shadow_ptes[0];
555 prev_desc->more = desc->more;
557 *rmapp = (unsigned long)desc->more | 1;
558 mmu_free_rmap_desc(desc);
561 static void rmap_remove(struct kvm *kvm, u64 *spte)
563 struct kvm_rmap_desc *desc;
564 struct kvm_rmap_desc *prev_desc;
565 struct kvm_mmu_page *sp;
567 unsigned long *rmapp;
570 if (!is_rmap_pte(*spte))
572 sp = page_header(__pa(spte));
573 pfn = spte_to_pfn(*spte);
574 if (*spte & shadow_accessed_mask)
575 kvm_set_pfn_accessed(pfn);
576 if (is_writeble_pte(*spte))
577 kvm_release_pfn_dirty(pfn);
579 kvm_release_pfn_clean(pfn);
580 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
582 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
584 } else if (!(*rmapp & 1)) {
585 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
586 if ((u64 *)*rmapp != spte) {
587 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
593 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
594 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
597 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
598 if (desc->shadow_ptes[i] == spte) {
599 rmap_desc_remove_entry(rmapp,
611 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
613 struct kvm_rmap_desc *desc;
614 struct kvm_rmap_desc *prev_desc;
620 else if (!(*rmapp & 1)) {
622 return (u64 *)*rmapp;
625 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
629 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
630 if (prev_spte == spte)
631 return desc->shadow_ptes[i];
632 prev_spte = desc->shadow_ptes[i];
639 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
641 unsigned long *rmapp;
643 int write_protected = 0;
645 gfn = unalias_gfn(kvm, gfn);
646 rmapp = gfn_to_rmap(kvm, gfn, 0);
648 spte = rmap_next(kvm, rmapp, NULL);
651 BUG_ON(!(*spte & PT_PRESENT_MASK));
652 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
653 if (is_writeble_pte(*spte)) {
654 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
657 spte = rmap_next(kvm, rmapp, spte);
659 if (write_protected) {
662 spte = rmap_next(kvm, rmapp, NULL);
663 pfn = spte_to_pfn(*spte);
664 kvm_set_pfn_dirty(pfn);
667 /* check for huge page mappings */
668 rmapp = gfn_to_rmap(kvm, gfn, 1);
669 spte = rmap_next(kvm, rmapp, NULL);
672 BUG_ON(!(*spte & PT_PRESENT_MASK));
673 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
674 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
675 if (is_writeble_pte(*spte)) {
676 rmap_remove(kvm, spte);
678 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
682 spte = rmap_next(kvm, rmapp, spte);
685 return write_protected;
688 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
691 int need_tlb_flush = 0;
693 while ((spte = rmap_next(kvm, rmapp, NULL))) {
694 BUG_ON(!(*spte & PT_PRESENT_MASK));
695 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
696 rmap_remove(kvm, spte);
697 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
700 return need_tlb_flush;
703 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
704 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
710 * If mmap_sem isn't taken, we can look the memslots with only
711 * the mmu_lock by skipping over the slots with userspace_addr == 0.
713 for (i = 0; i < kvm->nmemslots; i++) {
714 struct kvm_memory_slot *memslot = &kvm->memslots[i];
715 unsigned long start = memslot->userspace_addr;
718 /* mmu_lock protects userspace_addr */
722 end = start + (memslot->npages << PAGE_SHIFT);
723 if (hva >= start && hva < end) {
724 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
725 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
726 retval |= handler(kvm,
727 &memslot->lpage_info[
729 KVM_PAGES_PER_HPAGE].rmap_pde);
736 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
738 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
741 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
746 /* always return old for EPT */
747 if (!shadow_accessed_mask)
750 spte = rmap_next(kvm, rmapp, NULL);
754 BUG_ON(!(_spte & PT_PRESENT_MASK));
755 _young = _spte & PT_ACCESSED_MASK;
758 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
760 spte = rmap_next(kvm, rmapp, spte);
765 #define RMAP_RECYCLE_THRESHOLD 1000
767 static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
769 unsigned long *rmapp;
771 gfn = unalias_gfn(vcpu->kvm, gfn);
772 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
774 kvm_unmap_rmapp(vcpu->kvm, rmapp);
775 kvm_flush_remote_tlbs(vcpu->kvm);
778 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
780 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
784 static int is_empty_shadow_page(u64 *spt)
789 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
790 if (is_shadow_present_pte(*pos)) {
791 printk(KERN_ERR "%s: %p %llx\n", __func__,
799 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
801 ASSERT(is_empty_shadow_page(sp->spt));
803 __free_page(virt_to_page(sp->spt));
804 __free_page(virt_to_page(sp->gfns));
806 ++kvm->arch.n_free_mmu_pages;
809 static unsigned kvm_page_table_hashfn(gfn_t gfn)
811 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
814 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
817 struct kvm_mmu_page *sp;
819 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
820 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
821 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
822 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
823 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
824 INIT_LIST_HEAD(&sp->oos_link);
825 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
827 sp->parent_pte = parent_pte;
828 --vcpu->kvm->arch.n_free_mmu_pages;
832 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
833 struct kvm_mmu_page *sp, u64 *parent_pte)
835 struct kvm_pte_chain *pte_chain;
836 struct hlist_node *node;
841 if (!sp->multimapped) {
842 u64 *old = sp->parent_pte;
845 sp->parent_pte = parent_pte;
849 pte_chain = mmu_alloc_pte_chain(vcpu);
850 INIT_HLIST_HEAD(&sp->parent_ptes);
851 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
852 pte_chain->parent_ptes[0] = old;
854 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
855 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
857 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
858 if (!pte_chain->parent_ptes[i]) {
859 pte_chain->parent_ptes[i] = parent_pte;
863 pte_chain = mmu_alloc_pte_chain(vcpu);
865 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
866 pte_chain->parent_ptes[0] = parent_pte;
869 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
872 struct kvm_pte_chain *pte_chain;
873 struct hlist_node *node;
876 if (!sp->multimapped) {
877 BUG_ON(sp->parent_pte != parent_pte);
878 sp->parent_pte = NULL;
881 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
882 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
883 if (!pte_chain->parent_ptes[i])
885 if (pte_chain->parent_ptes[i] != parent_pte)
887 while (i + 1 < NR_PTE_CHAIN_ENTRIES
888 && pte_chain->parent_ptes[i + 1]) {
889 pte_chain->parent_ptes[i]
890 = pte_chain->parent_ptes[i + 1];
893 pte_chain->parent_ptes[i] = NULL;
895 hlist_del(&pte_chain->link);
896 mmu_free_pte_chain(pte_chain);
897 if (hlist_empty(&sp->parent_ptes)) {
899 sp->parent_pte = NULL;
908 static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
909 mmu_parent_walk_fn fn)
911 struct kvm_pte_chain *pte_chain;
912 struct hlist_node *node;
913 struct kvm_mmu_page *parent_sp;
916 if (!sp->multimapped && sp->parent_pte) {
917 parent_sp = page_header(__pa(sp->parent_pte));
919 mmu_parent_walk(vcpu, parent_sp, fn);
922 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
923 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
924 if (!pte_chain->parent_ptes[i])
926 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
928 mmu_parent_walk(vcpu, parent_sp, fn);
932 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
935 struct kvm_mmu_page *sp = page_header(__pa(spte));
937 index = spte - sp->spt;
938 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
939 sp->unsync_children++;
940 WARN_ON(!sp->unsync_children);
943 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
945 struct kvm_pte_chain *pte_chain;
946 struct hlist_node *node;
952 if (!sp->multimapped) {
953 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
957 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
958 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
959 if (!pte_chain->parent_ptes[i])
961 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
965 static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
967 kvm_mmu_update_parents_unsync(sp);
971 static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
972 struct kvm_mmu_page *sp)
974 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
975 kvm_mmu_update_parents_unsync(sp);
978 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
979 struct kvm_mmu_page *sp)
983 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
984 sp->spt[i] = shadow_trap_nonpresent_pte;
987 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
988 struct kvm_mmu_page *sp)
993 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
997 #define KVM_PAGE_ARRAY_NR 16
999 struct kvm_mmu_pages {
1000 struct mmu_page_and_offset {
1001 struct kvm_mmu_page *sp;
1003 } page[KVM_PAGE_ARRAY_NR];
1007 #define for_each_unsync_children(bitmap, idx) \
1008 for (idx = find_first_bit(bitmap, 512); \
1010 idx = find_next_bit(bitmap, 512, idx+1))
1012 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1018 for (i=0; i < pvec->nr; i++)
1019 if (pvec->page[i].sp == sp)
1022 pvec->page[pvec->nr].sp = sp;
1023 pvec->page[pvec->nr].idx = idx;
1025 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1028 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1029 struct kvm_mmu_pages *pvec)
1031 int i, ret, nr_unsync_leaf = 0;
1033 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1034 u64 ent = sp->spt[i];
1036 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1037 struct kvm_mmu_page *child;
1038 child = page_header(ent & PT64_BASE_ADDR_MASK);
1040 if (child->unsync_children) {
1041 if (mmu_pages_add(pvec, child, i))
1044 ret = __mmu_unsync_walk(child, pvec);
1046 __clear_bit(i, sp->unsync_child_bitmap);
1048 nr_unsync_leaf += ret;
1053 if (child->unsync) {
1055 if (mmu_pages_add(pvec, child, i))
1061 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1062 sp->unsync_children = 0;
1064 return nr_unsync_leaf;
1067 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1068 struct kvm_mmu_pages *pvec)
1070 if (!sp->unsync_children)
1073 mmu_pages_add(pvec, sp, 0);
1074 return __mmu_unsync_walk(sp, pvec);
1077 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1080 struct hlist_head *bucket;
1081 struct kvm_mmu_page *sp;
1082 struct hlist_node *node;
1084 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1085 index = kvm_page_table_hashfn(gfn);
1086 bucket = &kvm->arch.mmu_page_hash[index];
1087 hlist_for_each_entry(sp, node, bucket, hash_link)
1088 if (sp->gfn == gfn && !sp->role.direct
1089 && !sp->role.invalid) {
1090 pgprintk("%s: found role %x\n",
1091 __func__, sp->role.word);
1097 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1099 WARN_ON(!sp->unsync);
1101 --kvm->stat.mmu_unsync;
1104 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1106 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1108 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1109 kvm_mmu_zap_page(vcpu->kvm, sp);
1113 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1114 kvm_flush_remote_tlbs(vcpu->kvm);
1115 kvm_unlink_unsync_page(vcpu->kvm, sp);
1116 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1117 kvm_mmu_zap_page(vcpu->kvm, sp);
1121 kvm_mmu_flush_tlb(vcpu);
1125 struct mmu_page_path {
1126 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1127 unsigned int idx[PT64_ROOT_LEVEL-1];
1130 #define for_each_sp(pvec, sp, parents, i) \
1131 for (i = mmu_pages_next(&pvec, &parents, -1), \
1132 sp = pvec.page[i].sp; \
1133 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1134 i = mmu_pages_next(&pvec, &parents, i))
1136 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1137 struct mmu_page_path *parents,
1142 for (n = i+1; n < pvec->nr; n++) {
1143 struct kvm_mmu_page *sp = pvec->page[n].sp;
1145 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1146 parents->idx[0] = pvec->page[n].idx;
1150 parents->parent[sp->role.level-2] = sp;
1151 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1157 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1159 struct kvm_mmu_page *sp;
1160 unsigned int level = 0;
1163 unsigned int idx = parents->idx[level];
1165 sp = parents->parent[level];
1169 --sp->unsync_children;
1170 WARN_ON((int)sp->unsync_children < 0);
1171 __clear_bit(idx, sp->unsync_child_bitmap);
1173 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1176 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1177 struct mmu_page_path *parents,
1178 struct kvm_mmu_pages *pvec)
1180 parents->parent[parent->role.level-1] = NULL;
1184 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1185 struct kvm_mmu_page *parent)
1188 struct kvm_mmu_page *sp;
1189 struct mmu_page_path parents;
1190 struct kvm_mmu_pages pages;
1192 kvm_mmu_pages_init(parent, &parents, &pages);
1193 while (mmu_unsync_walk(parent, &pages)) {
1196 for_each_sp(pages, sp, parents, i)
1197 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1200 kvm_flush_remote_tlbs(vcpu->kvm);
1202 for_each_sp(pages, sp, parents, i) {
1203 kvm_sync_page(vcpu, sp);
1204 mmu_pages_clear_parents(&parents);
1206 cond_resched_lock(&vcpu->kvm->mmu_lock);
1207 kvm_mmu_pages_init(parent, &parents, &pages);
1211 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1219 union kvm_mmu_page_role role;
1222 struct hlist_head *bucket;
1223 struct kvm_mmu_page *sp;
1224 struct hlist_node *node, *tmp;
1226 role = vcpu->arch.mmu.base_role;
1228 role.direct = direct;
1229 role.access = access;
1230 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1231 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1232 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1233 role.quadrant = quadrant;
1235 pgprintk("%s: looking gfn %lx role %x\n", __func__,
1237 index = kvm_page_table_hashfn(gfn);
1238 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1239 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1240 if (sp->gfn == gfn) {
1242 if (kvm_sync_page(vcpu, sp))
1245 if (sp->role.word != role.word)
1248 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1249 if (sp->unsync_children) {
1250 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1251 kvm_mmu_mark_parents_unsync(vcpu, sp);
1253 pgprintk("%s: found\n", __func__);
1256 ++vcpu->kvm->stat.mmu_cache_miss;
1257 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1260 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
1263 hlist_add_head(&sp->hash_link, bucket);
1265 if (rmap_write_protect(vcpu->kvm, gfn))
1266 kvm_flush_remote_tlbs(vcpu->kvm);
1267 account_shadowed(vcpu->kvm, gfn);
1269 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1270 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1272 nonpaging_prefetch_page(vcpu, sp);
1276 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1277 struct kvm_vcpu *vcpu, u64 addr)
1279 iterator->addr = addr;
1280 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1281 iterator->level = vcpu->arch.mmu.shadow_root_level;
1282 if (iterator->level == PT32E_ROOT_LEVEL) {
1283 iterator->shadow_addr
1284 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1285 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1287 if (!iterator->shadow_addr)
1288 iterator->level = 0;
1292 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1294 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1296 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1297 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1301 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1303 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1307 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1308 struct kvm_mmu_page *sp)
1316 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1317 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1318 if (is_shadow_present_pte(pt[i]))
1319 rmap_remove(kvm, &pt[i]);
1320 pt[i] = shadow_trap_nonpresent_pte;
1325 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1328 if (is_shadow_present_pte(ent)) {
1329 if (!is_large_pte(ent)) {
1330 ent &= PT64_BASE_ADDR_MASK;
1331 mmu_page_remove_parent_pte(page_header(ent),
1335 rmap_remove(kvm, &pt[i]);
1338 pt[i] = shadow_trap_nonpresent_pte;
1342 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1344 mmu_page_remove_parent_pte(sp, parent_pte);
1347 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1351 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1353 kvm->vcpus[i]->arch.last_pte_updated = NULL;
1356 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1360 while (sp->multimapped || sp->parent_pte) {
1361 if (!sp->multimapped)
1362 parent_pte = sp->parent_pte;
1364 struct kvm_pte_chain *chain;
1366 chain = container_of(sp->parent_ptes.first,
1367 struct kvm_pte_chain, link);
1368 parent_pte = chain->parent_ptes[0];
1370 BUG_ON(!parent_pte);
1371 kvm_mmu_put_page(sp, parent_pte);
1372 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1376 static int mmu_zap_unsync_children(struct kvm *kvm,
1377 struct kvm_mmu_page *parent)
1380 struct mmu_page_path parents;
1381 struct kvm_mmu_pages pages;
1383 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1386 kvm_mmu_pages_init(parent, &parents, &pages);
1387 while (mmu_unsync_walk(parent, &pages)) {
1388 struct kvm_mmu_page *sp;
1390 for_each_sp(pages, sp, parents, i) {
1391 kvm_mmu_zap_page(kvm, sp);
1392 mmu_pages_clear_parents(&parents);
1395 kvm_mmu_pages_init(parent, &parents, &pages);
1401 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1404 ++kvm->stat.mmu_shadow_zapped;
1405 ret = mmu_zap_unsync_children(kvm, sp);
1406 kvm_mmu_page_unlink_children(kvm, sp);
1407 kvm_mmu_unlink_parents(kvm, sp);
1408 kvm_flush_remote_tlbs(kvm);
1409 if (!sp->role.invalid && !sp->role.direct)
1410 unaccount_shadowed(kvm, sp->gfn);
1412 kvm_unlink_unsync_page(kvm, sp);
1413 if (!sp->root_count) {
1414 hlist_del(&sp->hash_link);
1415 kvm_mmu_free_page(kvm, sp);
1417 sp->role.invalid = 1;
1418 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1419 kvm_reload_remote_mmus(kvm);
1421 kvm_mmu_reset_last_pte_updated(kvm);
1426 * Changing the number of mmu pages allocated to the vm
1427 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1429 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1433 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1434 used_pages = max(0, used_pages);
1437 * If we set the number of mmu pages to be smaller be than the
1438 * number of actived pages , we must to free some mmu pages before we
1442 if (used_pages > kvm_nr_mmu_pages) {
1443 while (used_pages > kvm_nr_mmu_pages) {
1444 struct kvm_mmu_page *page;
1446 page = container_of(kvm->arch.active_mmu_pages.prev,
1447 struct kvm_mmu_page, link);
1448 kvm_mmu_zap_page(kvm, page);
1451 kvm->arch.n_free_mmu_pages = 0;
1454 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1455 - kvm->arch.n_alloc_mmu_pages;
1457 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1460 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1463 struct hlist_head *bucket;
1464 struct kvm_mmu_page *sp;
1465 struct hlist_node *node, *n;
1468 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1470 index = kvm_page_table_hashfn(gfn);
1471 bucket = &kvm->arch.mmu_page_hash[index];
1472 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1473 if (sp->gfn == gfn && !sp->role.direct) {
1474 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1477 if (kvm_mmu_zap_page(kvm, sp))
1483 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1486 struct hlist_head *bucket;
1487 struct kvm_mmu_page *sp;
1488 struct hlist_node *node, *nn;
1490 index = kvm_page_table_hashfn(gfn);
1491 bucket = &kvm->arch.mmu_page_hash[index];
1492 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1493 if (sp->gfn == gfn && !sp->role.direct
1494 && !sp->role.invalid) {
1495 pgprintk("%s: zap %lx %x\n",
1496 __func__, gfn, sp->role.word);
1497 kvm_mmu_zap_page(kvm, sp);
1502 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1504 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1505 struct kvm_mmu_page *sp = page_header(__pa(pte));
1507 __set_bit(slot, sp->slot_bitmap);
1510 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1515 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1518 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1519 if (pt[i] == shadow_notrap_nonpresent_pte)
1520 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1524 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1528 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1530 if (gpa == UNMAPPED_GVA)
1533 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1539 * The function is based on mtrr_type_lookup() in
1540 * arch/x86/kernel/cpu/mtrr/generic.c
1542 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1547 u8 prev_match, curr_match;
1548 int num_var_ranges = KVM_NR_VAR_MTRR;
1550 if (!mtrr_state->enabled)
1553 /* Make end inclusive end, instead of exclusive */
1556 /* Look in fixed ranges. Just return the type as per start */
1557 if (mtrr_state->have_fixed && (start < 0x100000)) {
1560 if (start < 0x80000) {
1562 idx += (start >> 16);
1563 return mtrr_state->fixed_ranges[idx];
1564 } else if (start < 0xC0000) {
1566 idx += ((start - 0x80000) >> 14);
1567 return mtrr_state->fixed_ranges[idx];
1568 } else if (start < 0x1000000) {
1570 idx += ((start - 0xC0000) >> 12);
1571 return mtrr_state->fixed_ranges[idx];
1576 * Look in variable ranges
1577 * Look of multiple ranges matching this address and pick type
1578 * as per MTRR precedence
1580 if (!(mtrr_state->enabled & 2))
1581 return mtrr_state->def_type;
1584 for (i = 0; i < num_var_ranges; ++i) {
1585 unsigned short start_state, end_state;
1587 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1590 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1591 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1592 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1593 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1595 start_state = ((start & mask) == (base & mask));
1596 end_state = ((end & mask) == (base & mask));
1597 if (start_state != end_state)
1600 if ((start & mask) != (base & mask))
1603 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1604 if (prev_match == 0xFF) {
1605 prev_match = curr_match;
1609 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1610 curr_match == MTRR_TYPE_UNCACHABLE)
1611 return MTRR_TYPE_UNCACHABLE;
1613 if ((prev_match == MTRR_TYPE_WRBACK &&
1614 curr_match == MTRR_TYPE_WRTHROUGH) ||
1615 (prev_match == MTRR_TYPE_WRTHROUGH &&
1616 curr_match == MTRR_TYPE_WRBACK)) {
1617 prev_match = MTRR_TYPE_WRTHROUGH;
1618 curr_match = MTRR_TYPE_WRTHROUGH;
1621 if (prev_match != curr_match)
1622 return MTRR_TYPE_UNCACHABLE;
1625 if (prev_match != 0xFF)
1628 return mtrr_state->def_type;
1631 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1635 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1636 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1637 if (mtrr == 0xfe || mtrr == 0xff)
1638 mtrr = MTRR_TYPE_WRBACK;
1641 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1643 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1646 struct hlist_head *bucket;
1647 struct kvm_mmu_page *s;
1648 struct hlist_node *node, *n;
1650 index = kvm_page_table_hashfn(sp->gfn);
1651 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1652 /* don't unsync if pagetable is shadowed with multiple roles */
1653 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1654 if (s->gfn != sp->gfn || s->role.direct)
1656 if (s->role.word != sp->role.word)
1659 ++vcpu->kvm->stat.mmu_unsync;
1662 kvm_mmu_mark_parents_unsync(vcpu, sp);
1664 mmu_convert_notrap(sp);
1668 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1671 struct kvm_mmu_page *shadow;
1673 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1675 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1679 if (can_unsync && oos_shadow)
1680 return kvm_unsync_page(vcpu, shadow);
1686 static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1687 unsigned pte_access, int user_fault,
1688 int write_fault, int dirty, int largepage,
1689 gfn_t gfn, pfn_t pfn, bool speculative,
1696 * We don't set the accessed bit, since we sometimes want to see
1697 * whether the guest actually used the pte (in order to detect
1700 spte = shadow_base_present_pte | shadow_dirty_mask;
1702 spte |= shadow_accessed_mask;
1704 pte_access &= ~ACC_WRITE_MASK;
1705 if (pte_access & ACC_EXEC_MASK)
1706 spte |= shadow_x_mask;
1708 spte |= shadow_nx_mask;
1709 if (pte_access & ACC_USER_MASK)
1710 spte |= shadow_user_mask;
1712 spte |= PT_PAGE_SIZE_MASK;
1714 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1715 kvm_is_mmio_pfn(pfn));
1717 spte |= (u64)pfn << PAGE_SHIFT;
1719 if ((pte_access & ACC_WRITE_MASK)
1720 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1722 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1724 spte = shadow_trap_nonpresent_pte;
1728 spte |= PT_WRITABLE_MASK;
1731 * Optimization: for pte sync, if spte was writable the hash
1732 * lookup is unnecessary (and expensive). Write protection
1733 * is responsibility of mmu_get_page / kvm_sync_page.
1734 * Same reasoning can be applied to dirty page accounting.
1736 if (!can_unsync && is_writeble_pte(*shadow_pte))
1739 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1740 pgprintk("%s: found shadow page for %lx, marking ro\n",
1743 pte_access &= ~ACC_WRITE_MASK;
1744 if (is_writeble_pte(spte))
1745 spte &= ~PT_WRITABLE_MASK;
1749 if (pte_access & ACC_WRITE_MASK)
1750 mark_page_dirty(vcpu->kvm, gfn);
1753 set_shadow_pte(shadow_pte, spte);
1757 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1758 unsigned pt_access, unsigned pte_access,
1759 int user_fault, int write_fault, int dirty,
1760 int *ptwrite, int largepage, gfn_t gfn,
1761 pfn_t pfn, bool speculative)
1763 int was_rmapped = 0;
1764 int was_writeble = is_writeble_pte(*shadow_pte);
1767 pgprintk("%s: spte %llx access %x write_fault %d"
1768 " user_fault %d gfn %lx\n",
1769 __func__, *shadow_pte, pt_access,
1770 write_fault, user_fault, gfn);
1772 if (is_rmap_pte(*shadow_pte)) {
1774 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1775 * the parent of the now unreachable PTE.
1777 if (largepage && !is_large_pte(*shadow_pte)) {
1778 struct kvm_mmu_page *child;
1779 u64 pte = *shadow_pte;
1781 child = page_header(pte & PT64_BASE_ADDR_MASK);
1782 mmu_page_remove_parent_pte(child, shadow_pte);
1783 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1784 pgprintk("hfn old %lx new %lx\n",
1785 spte_to_pfn(*shadow_pte), pfn);
1786 rmap_remove(vcpu->kvm, shadow_pte);
1790 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
1791 dirty, largepage, gfn, pfn, speculative, true)) {
1794 kvm_x86_ops->tlb_flush(vcpu);
1797 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1798 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1799 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1800 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1801 *shadow_pte, shadow_pte);
1802 if (!was_rmapped && is_large_pte(*shadow_pte))
1803 ++vcpu->kvm->stat.lpages;
1805 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1807 rmap_count = rmap_add(vcpu, shadow_pte, gfn, largepage);
1808 if (!is_rmap_pte(*shadow_pte))
1809 kvm_release_pfn_clean(pfn);
1810 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1811 rmap_recycle(vcpu, gfn, largepage);
1814 kvm_release_pfn_dirty(pfn);
1816 kvm_release_pfn_clean(pfn);
1819 vcpu->arch.last_pte_updated = shadow_pte;
1820 vcpu->arch.last_pte_gfn = gfn;
1824 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1828 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1829 int largepage, gfn_t gfn, pfn_t pfn)
1831 struct kvm_shadow_walk_iterator iterator;
1832 struct kvm_mmu_page *sp;
1836 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1837 if (iterator.level == PT_PAGE_TABLE_LEVEL
1838 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1839 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1840 0, write, 1, &pt_write,
1841 largepage, gfn, pfn, false);
1842 ++vcpu->stat.pf_fixed;
1846 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1847 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1848 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1850 1, ACC_ALL, iterator.sptep);
1852 pgprintk("nonpaging_map: ENOMEM\n");
1853 kvm_release_pfn_clean(pfn);
1857 set_shadow_pte(iterator.sptep,
1859 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1860 | shadow_user_mask | shadow_x_mask);
1866 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1871 unsigned long mmu_seq;
1873 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1874 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1878 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1880 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1883 if (is_error_pfn(pfn)) {
1884 kvm_release_pfn_clean(pfn);
1888 spin_lock(&vcpu->kvm->mmu_lock);
1889 if (mmu_notifier_retry(vcpu, mmu_seq))
1891 kvm_mmu_free_some_pages(vcpu);
1892 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1893 spin_unlock(&vcpu->kvm->mmu_lock);
1899 spin_unlock(&vcpu->kvm->mmu_lock);
1900 kvm_release_pfn_clean(pfn);
1905 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1908 struct kvm_mmu_page *sp;
1910 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1912 spin_lock(&vcpu->kvm->mmu_lock);
1913 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1914 hpa_t root = vcpu->arch.mmu.root_hpa;
1916 sp = page_header(root);
1918 if (!sp->root_count && sp->role.invalid)
1919 kvm_mmu_zap_page(vcpu->kvm, sp);
1920 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1921 spin_unlock(&vcpu->kvm->mmu_lock);
1924 for (i = 0; i < 4; ++i) {
1925 hpa_t root = vcpu->arch.mmu.pae_root[i];
1928 root &= PT64_BASE_ADDR_MASK;
1929 sp = page_header(root);
1931 if (!sp->root_count && sp->role.invalid)
1932 kvm_mmu_zap_page(vcpu->kvm, sp);
1934 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1936 spin_unlock(&vcpu->kvm->mmu_lock);
1937 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1940 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1944 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1945 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1952 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
1956 struct kvm_mmu_page *sp;
1960 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1962 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1963 hpa_t root = vcpu->arch.mmu.root_hpa;
1965 ASSERT(!VALID_PAGE(root));
1968 if (mmu_check_root(vcpu, root_gfn))
1970 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1971 PT64_ROOT_LEVEL, direct,
1973 root = __pa(sp->spt);
1975 vcpu->arch.mmu.root_hpa = root;
1978 direct = !is_paging(vcpu);
1981 for (i = 0; i < 4; ++i) {
1982 hpa_t root = vcpu->arch.mmu.pae_root[i];
1984 ASSERT(!VALID_PAGE(root));
1985 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1986 pdptr = kvm_pdptr_read(vcpu, i);
1987 if (!is_present_pte(pdptr)) {
1988 vcpu->arch.mmu.pae_root[i] = 0;
1991 root_gfn = pdptr >> PAGE_SHIFT;
1992 } else if (vcpu->arch.mmu.root_level == 0)
1994 if (mmu_check_root(vcpu, root_gfn))
1996 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1997 PT32_ROOT_LEVEL, direct,
1999 root = __pa(sp->spt);
2001 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2003 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2007 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2010 struct kvm_mmu_page *sp;
2012 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2014 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2015 hpa_t root = vcpu->arch.mmu.root_hpa;
2016 sp = page_header(root);
2017 mmu_sync_children(vcpu, sp);
2020 for (i = 0; i < 4; ++i) {
2021 hpa_t root = vcpu->arch.mmu.pae_root[i];
2023 if (root && VALID_PAGE(root)) {
2024 root &= PT64_BASE_ADDR_MASK;
2025 sp = page_header(root);
2026 mmu_sync_children(vcpu, sp);
2031 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2033 spin_lock(&vcpu->kvm->mmu_lock);
2034 mmu_sync_roots(vcpu);
2035 spin_unlock(&vcpu->kvm->mmu_lock);
2038 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2043 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2049 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2050 r = mmu_topup_memory_caches(vcpu);
2055 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2057 gfn = gva >> PAGE_SHIFT;
2059 return nonpaging_map(vcpu, gva & PAGE_MASK,
2060 error_code & PFERR_WRITE_MASK, gfn);
2063 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2069 gfn_t gfn = gpa >> PAGE_SHIFT;
2070 unsigned long mmu_seq;
2073 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2075 r = mmu_topup_memory_caches(vcpu);
2079 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2080 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2083 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2085 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2086 if (is_error_pfn(pfn)) {
2087 kvm_release_pfn_clean(pfn);
2090 spin_lock(&vcpu->kvm->mmu_lock);
2091 if (mmu_notifier_retry(vcpu, mmu_seq))
2093 kvm_mmu_free_some_pages(vcpu);
2094 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2095 largepage, gfn, pfn);
2096 spin_unlock(&vcpu->kvm->mmu_lock);
2101 spin_unlock(&vcpu->kvm->mmu_lock);
2102 kvm_release_pfn_clean(pfn);
2106 static void nonpaging_free(struct kvm_vcpu *vcpu)
2108 mmu_free_roots(vcpu);
2111 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2113 struct kvm_mmu *context = &vcpu->arch.mmu;
2115 context->new_cr3 = nonpaging_new_cr3;
2116 context->page_fault = nonpaging_page_fault;
2117 context->gva_to_gpa = nonpaging_gva_to_gpa;
2118 context->free = nonpaging_free;
2119 context->prefetch_page = nonpaging_prefetch_page;
2120 context->sync_page = nonpaging_sync_page;
2121 context->invlpg = nonpaging_invlpg;
2122 context->root_level = 0;
2123 context->shadow_root_level = PT32E_ROOT_LEVEL;
2124 context->root_hpa = INVALID_PAGE;
2128 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2130 ++vcpu->stat.tlb_flush;
2131 kvm_x86_ops->tlb_flush(vcpu);
2134 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2136 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2137 mmu_free_roots(vcpu);
2140 static void inject_page_fault(struct kvm_vcpu *vcpu,
2144 kvm_inject_page_fault(vcpu, addr, err_code);
2147 static void paging_free(struct kvm_vcpu *vcpu)
2149 nonpaging_free(vcpu);
2152 static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2156 bit7 = (gpte >> 7) & 1;
2157 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2161 #include "paging_tmpl.h"
2165 #include "paging_tmpl.h"
2168 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2170 struct kvm_mmu *context = &vcpu->arch.mmu;
2171 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2172 u64 exb_bit_rsvd = 0;
2175 exb_bit_rsvd = rsvd_bits(63, 63);
2177 case PT32_ROOT_LEVEL:
2178 /* no rsvd bits for 2 level 4K page table entries */
2179 context->rsvd_bits_mask[0][1] = 0;
2180 context->rsvd_bits_mask[0][0] = 0;
2181 if (is_cpuid_PSE36())
2182 /* 36bits PSE 4MB page */
2183 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2185 /* 32 bits PSE 4MB page */
2186 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2187 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2189 case PT32E_ROOT_LEVEL:
2190 context->rsvd_bits_mask[0][2] =
2191 rsvd_bits(maxphyaddr, 63) |
2192 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2193 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2194 rsvd_bits(maxphyaddr, 62); /* PDE */
2195 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2196 rsvd_bits(maxphyaddr, 62); /* PTE */
2197 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2198 rsvd_bits(maxphyaddr, 62) |
2199 rsvd_bits(13, 20); /* large page */
2200 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2202 case PT64_ROOT_LEVEL:
2203 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2204 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2205 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2206 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2207 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2208 rsvd_bits(maxphyaddr, 51);
2209 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2210 rsvd_bits(maxphyaddr, 51);
2211 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2212 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2213 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2214 rsvd_bits(maxphyaddr, 51) |
2215 rsvd_bits(13, 20); /* large page */
2216 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2221 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2223 struct kvm_mmu *context = &vcpu->arch.mmu;
2225 ASSERT(is_pae(vcpu));
2226 context->new_cr3 = paging_new_cr3;
2227 context->page_fault = paging64_page_fault;
2228 context->gva_to_gpa = paging64_gva_to_gpa;
2229 context->prefetch_page = paging64_prefetch_page;
2230 context->sync_page = paging64_sync_page;
2231 context->invlpg = paging64_invlpg;
2232 context->free = paging_free;
2233 context->root_level = level;
2234 context->shadow_root_level = level;
2235 context->root_hpa = INVALID_PAGE;
2239 static int paging64_init_context(struct kvm_vcpu *vcpu)
2241 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2242 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2245 static int paging32_init_context(struct kvm_vcpu *vcpu)
2247 struct kvm_mmu *context = &vcpu->arch.mmu;
2249 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2250 context->new_cr3 = paging_new_cr3;
2251 context->page_fault = paging32_page_fault;
2252 context->gva_to_gpa = paging32_gva_to_gpa;
2253 context->free = paging_free;
2254 context->prefetch_page = paging32_prefetch_page;
2255 context->sync_page = paging32_sync_page;
2256 context->invlpg = paging32_invlpg;
2257 context->root_level = PT32_ROOT_LEVEL;
2258 context->shadow_root_level = PT32E_ROOT_LEVEL;
2259 context->root_hpa = INVALID_PAGE;
2263 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2265 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2266 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2269 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2271 struct kvm_mmu *context = &vcpu->arch.mmu;
2273 context->new_cr3 = nonpaging_new_cr3;
2274 context->page_fault = tdp_page_fault;
2275 context->free = nonpaging_free;
2276 context->prefetch_page = nonpaging_prefetch_page;
2277 context->sync_page = nonpaging_sync_page;
2278 context->invlpg = nonpaging_invlpg;
2279 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2280 context->root_hpa = INVALID_PAGE;
2282 if (!is_paging(vcpu)) {
2283 context->gva_to_gpa = nonpaging_gva_to_gpa;
2284 context->root_level = 0;
2285 } else if (is_long_mode(vcpu)) {
2286 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2287 context->gva_to_gpa = paging64_gva_to_gpa;
2288 context->root_level = PT64_ROOT_LEVEL;
2289 } else if (is_pae(vcpu)) {
2290 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2291 context->gva_to_gpa = paging64_gva_to_gpa;
2292 context->root_level = PT32E_ROOT_LEVEL;
2294 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2295 context->gva_to_gpa = paging32_gva_to_gpa;
2296 context->root_level = PT32_ROOT_LEVEL;
2302 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2307 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2309 if (!is_paging(vcpu))
2310 r = nonpaging_init_context(vcpu);
2311 else if (is_long_mode(vcpu))
2312 r = paging64_init_context(vcpu);
2313 else if (is_pae(vcpu))
2314 r = paging32E_init_context(vcpu);
2316 r = paging32_init_context(vcpu);
2318 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2323 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2325 vcpu->arch.update_pte.pfn = bad_pfn;
2328 return init_kvm_tdp_mmu(vcpu);
2330 return init_kvm_softmmu(vcpu);
2333 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2336 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2337 vcpu->arch.mmu.free(vcpu);
2338 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2342 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2344 destroy_kvm_mmu(vcpu);
2345 return init_kvm_mmu(vcpu);
2347 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2349 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2353 r = mmu_topup_memory_caches(vcpu);
2356 spin_lock(&vcpu->kvm->mmu_lock);
2357 kvm_mmu_free_some_pages(vcpu);
2358 r = mmu_alloc_roots(vcpu);
2359 mmu_sync_roots(vcpu);
2360 spin_unlock(&vcpu->kvm->mmu_lock);
2363 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2364 kvm_mmu_flush_tlb(vcpu);
2368 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2370 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2372 mmu_free_roots(vcpu);
2375 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2376 struct kvm_mmu_page *sp,
2380 struct kvm_mmu_page *child;
2383 if (is_shadow_present_pte(pte)) {
2384 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2386 rmap_remove(vcpu->kvm, spte);
2388 child = page_header(pte & PT64_BASE_ADDR_MASK);
2389 mmu_page_remove_parent_pte(child, spte);
2392 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
2393 if (is_large_pte(pte))
2394 --vcpu->kvm->stat.lpages;
2397 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2398 struct kvm_mmu_page *sp,
2402 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2403 if (!vcpu->arch.update_pte.largepage ||
2404 sp->role.glevels == PT32_ROOT_LEVEL) {
2405 ++vcpu->kvm->stat.mmu_pde_zapped;
2410 ++vcpu->kvm->stat.mmu_pte_updated;
2411 if (sp->role.glevels == PT32_ROOT_LEVEL)
2412 paging32_update_pte(vcpu, sp, spte, new);
2414 paging64_update_pte(vcpu, sp, spte, new);
2417 static bool need_remote_flush(u64 old, u64 new)
2419 if (!is_shadow_present_pte(old))
2421 if (!is_shadow_present_pte(new))
2423 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2425 old ^= PT64_NX_MASK;
2426 new ^= PT64_NX_MASK;
2427 return (old & ~new & PT64_PERM_MASK) != 0;
2430 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2432 if (need_remote_flush(old, new))
2433 kvm_flush_remote_tlbs(vcpu->kvm);
2435 kvm_mmu_flush_tlb(vcpu);
2438 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2440 u64 *spte = vcpu->arch.last_pte_updated;
2442 return !!(spte && (*spte & shadow_accessed_mask));
2445 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2446 const u8 *new, int bytes)
2453 vcpu->arch.update_pte.largepage = 0;
2455 if (bytes != 4 && bytes != 8)
2459 * Assume that the pte write on a page table of the same type
2460 * as the current vcpu paging mode. This is nearly always true
2461 * (might be false while changing modes). Note it is verified later
2465 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2466 if ((bytes == 4) && (gpa % 4 == 0)) {
2467 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2470 memcpy((void *)&gpte + (gpa % 8), new, 4);
2471 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2472 memcpy((void *)&gpte, new, 8);
2475 if ((bytes == 4) && (gpa % 4 == 0))
2476 memcpy((void *)&gpte, new, 4);
2478 if (!is_present_pte(gpte))
2480 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2482 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2483 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2484 vcpu->arch.update_pte.largepage = 1;
2486 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2488 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2490 if (is_error_pfn(pfn)) {
2491 kvm_release_pfn_clean(pfn);
2494 vcpu->arch.update_pte.gfn = gfn;
2495 vcpu->arch.update_pte.pfn = pfn;
2498 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2500 u64 *spte = vcpu->arch.last_pte_updated;
2503 && vcpu->arch.last_pte_gfn == gfn
2504 && shadow_accessed_mask
2505 && !(*spte & shadow_accessed_mask)
2506 && is_shadow_present_pte(*spte))
2507 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2510 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2511 const u8 *new, int bytes,
2512 bool guest_initiated)
2514 gfn_t gfn = gpa >> PAGE_SHIFT;
2515 struct kvm_mmu_page *sp;
2516 struct hlist_node *node, *n;
2517 struct hlist_head *bucket;
2521 unsigned offset = offset_in_page(gpa);
2523 unsigned page_offset;
2524 unsigned misaligned;
2531 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2532 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
2533 spin_lock(&vcpu->kvm->mmu_lock);
2534 kvm_mmu_access_page(vcpu, gfn);
2535 kvm_mmu_free_some_pages(vcpu);
2536 ++vcpu->kvm->stat.mmu_pte_write;
2537 kvm_mmu_audit(vcpu, "pre pte write");
2538 if (guest_initiated) {
2539 if (gfn == vcpu->arch.last_pt_write_gfn
2540 && !last_updated_pte_accessed(vcpu)) {
2541 ++vcpu->arch.last_pt_write_count;
2542 if (vcpu->arch.last_pt_write_count >= 3)
2545 vcpu->arch.last_pt_write_gfn = gfn;
2546 vcpu->arch.last_pt_write_count = 1;
2547 vcpu->arch.last_pte_updated = NULL;
2550 index = kvm_page_table_hashfn(gfn);
2551 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2552 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2553 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2555 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
2556 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2557 misaligned |= bytes < 4;
2558 if (misaligned || flooded) {
2560 * Misaligned accesses are too much trouble to fix
2561 * up; also, they usually indicate a page is not used
2564 * If we're seeing too many writes to a page,
2565 * it may no longer be a page table, or we may be
2566 * forking, in which case it is better to unmap the
2569 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2570 gpa, bytes, sp->role.word);
2571 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2573 ++vcpu->kvm->stat.mmu_flooded;
2576 page_offset = offset;
2577 level = sp->role.level;
2579 if (sp->role.glevels == PT32_ROOT_LEVEL) {
2580 page_offset <<= 1; /* 32->64 */
2582 * A 32-bit pde maps 4MB while the shadow pdes map
2583 * only 2MB. So we need to double the offset again
2584 * and zap two pdes instead of one.
2586 if (level == PT32_ROOT_LEVEL) {
2587 page_offset &= ~7; /* kill rounding error */
2591 quadrant = page_offset >> PAGE_SHIFT;
2592 page_offset &= ~PAGE_MASK;
2593 if (quadrant != sp->role.quadrant)
2596 spte = &sp->spt[page_offset / sizeof(*spte)];
2597 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2599 r = kvm_read_guest_atomic(vcpu->kvm,
2600 gpa & ~(u64)(pte_size - 1),
2602 new = (const void *)&gentry;
2608 mmu_pte_write_zap_pte(vcpu, sp, spte);
2610 mmu_pte_write_new_pte(vcpu, sp, spte, new);
2611 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2615 kvm_mmu_audit(vcpu, "post pte write");
2616 spin_unlock(&vcpu->kvm->mmu_lock);
2617 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2618 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2619 vcpu->arch.update_pte.pfn = bad_pfn;
2623 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2628 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
2630 spin_lock(&vcpu->kvm->mmu_lock);
2631 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2632 spin_unlock(&vcpu->kvm->mmu_lock);
2635 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2637 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2639 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
2640 struct kvm_mmu_page *sp;
2642 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2643 struct kvm_mmu_page, link);
2644 kvm_mmu_zap_page(vcpu->kvm, sp);
2645 ++vcpu->kvm->stat.mmu_recycled;
2649 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2652 enum emulation_result er;
2654 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2663 r = mmu_topup_memory_caches(vcpu);
2667 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2672 case EMULATE_DO_MMIO:
2673 ++vcpu->stat.mmio_exits;
2676 kvm_report_emulation_failure(vcpu, "pagetable");
2684 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2686 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2688 vcpu->arch.mmu.invlpg(vcpu, gva);
2689 kvm_mmu_flush_tlb(vcpu);
2690 ++vcpu->stat.invlpg;
2692 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2694 void kvm_enable_tdp(void)
2698 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2700 void kvm_disable_tdp(void)
2702 tdp_enabled = false;
2704 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2706 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2708 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2711 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2718 if (vcpu->kvm->arch.n_requested_mmu_pages)
2719 vcpu->kvm->arch.n_free_mmu_pages =
2720 vcpu->kvm->arch.n_requested_mmu_pages;
2722 vcpu->kvm->arch.n_free_mmu_pages =
2723 vcpu->kvm->arch.n_alloc_mmu_pages;
2725 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2726 * Therefore we need to allocate shadow page tables in the first
2727 * 4GB of memory, which happens to fit the DMA32 zone.
2729 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2732 vcpu->arch.mmu.pae_root = page_address(page);
2733 for (i = 0; i < 4; ++i)
2734 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2739 free_mmu_pages(vcpu);
2743 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2746 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2748 return alloc_mmu_pages(vcpu);
2751 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2754 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2756 return init_kvm_mmu(vcpu);
2759 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2763 destroy_kvm_mmu(vcpu);
2764 free_mmu_pages(vcpu);
2765 mmu_free_memory_caches(vcpu);
2768 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2770 struct kvm_mmu_page *sp;
2772 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2776 if (!test_bit(slot, sp->slot_bitmap))
2780 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2782 if (pt[i] & PT_WRITABLE_MASK)
2783 pt[i] &= ~PT_WRITABLE_MASK;
2785 kvm_flush_remote_tlbs(kvm);
2788 void kvm_mmu_zap_all(struct kvm *kvm)
2790 struct kvm_mmu_page *sp, *node;
2792 spin_lock(&kvm->mmu_lock);
2793 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2794 if (kvm_mmu_zap_page(kvm, sp))
2795 node = container_of(kvm->arch.active_mmu_pages.next,
2796 struct kvm_mmu_page, link);
2797 spin_unlock(&kvm->mmu_lock);
2799 kvm_flush_remote_tlbs(kvm);
2802 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2804 struct kvm_mmu_page *page;
2806 page = container_of(kvm->arch.active_mmu_pages.prev,
2807 struct kvm_mmu_page, link);
2808 kvm_mmu_zap_page(kvm, page);
2811 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2814 struct kvm *kvm_freed = NULL;
2815 int cache_count = 0;
2817 spin_lock(&kvm_lock);
2819 list_for_each_entry(kvm, &vm_list, vm_list) {
2822 if (!down_read_trylock(&kvm->slots_lock))
2824 spin_lock(&kvm->mmu_lock);
2825 npages = kvm->arch.n_alloc_mmu_pages -
2826 kvm->arch.n_free_mmu_pages;
2827 cache_count += npages;
2828 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2829 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2835 spin_unlock(&kvm->mmu_lock);
2836 up_read(&kvm->slots_lock);
2839 list_move_tail(&kvm_freed->vm_list, &vm_list);
2841 spin_unlock(&kvm_lock);
2846 static struct shrinker mmu_shrinker = {
2847 .shrink = mmu_shrink,
2848 .seeks = DEFAULT_SEEKS * 10,
2851 static void mmu_destroy_caches(void)
2853 if (pte_chain_cache)
2854 kmem_cache_destroy(pte_chain_cache);
2855 if (rmap_desc_cache)
2856 kmem_cache_destroy(rmap_desc_cache);
2857 if (mmu_page_header_cache)
2858 kmem_cache_destroy(mmu_page_header_cache);
2861 void kvm_mmu_module_exit(void)
2863 mmu_destroy_caches();
2864 unregister_shrinker(&mmu_shrinker);
2867 int kvm_mmu_module_init(void)
2869 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2870 sizeof(struct kvm_pte_chain),
2872 if (!pte_chain_cache)
2874 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2875 sizeof(struct kvm_rmap_desc),
2877 if (!rmap_desc_cache)
2880 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2881 sizeof(struct kvm_mmu_page),
2883 if (!mmu_page_header_cache)
2886 register_shrinker(&mmu_shrinker);
2891 mmu_destroy_caches();
2896 * Caculate mmu pages needed for kvm.
2898 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2901 unsigned int nr_mmu_pages;
2902 unsigned int nr_pages = 0;
2904 for (i = 0; i < kvm->nmemslots; i++)
2905 nr_pages += kvm->memslots[i].npages;
2907 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2908 nr_mmu_pages = max(nr_mmu_pages,
2909 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2911 return nr_mmu_pages;
2914 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2917 if (len > buffer->len)
2922 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2927 ret = pv_mmu_peek_buffer(buffer, len);
2932 buffer->processed += len;
2936 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2937 gpa_t addr, gpa_t value)
2942 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2945 r = mmu_topup_memory_caches(vcpu);
2949 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2955 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2957 kvm_set_cr3(vcpu, vcpu->arch.cr3);
2961 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2963 spin_lock(&vcpu->kvm->mmu_lock);
2964 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2965 spin_unlock(&vcpu->kvm->mmu_lock);
2969 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2970 struct kvm_pv_mmu_op_buffer *buffer)
2972 struct kvm_mmu_op_header *header;
2974 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2977 switch (header->op) {
2978 case KVM_MMU_OP_WRITE_PTE: {
2979 struct kvm_mmu_op_write_pte *wpte;
2981 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2984 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2987 case KVM_MMU_OP_FLUSH_TLB: {
2988 struct kvm_mmu_op_flush_tlb *ftlb;
2990 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2993 return kvm_pv_mmu_flush_tlb(vcpu);
2995 case KVM_MMU_OP_RELEASE_PT: {
2996 struct kvm_mmu_op_release_pt *rpt;
2998 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3001 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3007 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3008 gpa_t addr, unsigned long *ret)
3011 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3013 buffer->ptr = buffer->buf;
3014 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3015 buffer->processed = 0;
3017 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3021 while (buffer->len) {
3022 r = kvm_pv_mmu_op_one(vcpu, buffer);
3031 *ret = buffer->processed;
3037 static const char *audit_msg;
3039 static gva_t canonicalize(gva_t gva)
3041 #ifdef CONFIG_X86_64
3042 gva = (long long)(gva << 16) >> 16;
3047 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3048 gva_t va, int level)
3050 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3052 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3054 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3057 if (ent == shadow_trap_nonpresent_pte)
3060 va = canonicalize(va);
3062 if (ent == shadow_notrap_nonpresent_pte)
3063 printk(KERN_ERR "audit: (%s) nontrapping pte"
3064 " in nonleaf level: levels %d gva %lx"
3065 " level %d pte %llx\n", audit_msg,
3066 vcpu->arch.mmu.root_level, va, level, ent);
3068 audit_mappings_page(vcpu, ent, va, level - 1);
3070 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
3071 gfn_t gfn = gpa >> PAGE_SHIFT;
3072 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3073 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
3075 if (is_shadow_present_pte(ent)
3076 && (ent & PT64_BASE_ADDR_MASK) != hpa)
3077 printk(KERN_ERR "xx audit error: (%s) levels %d"
3078 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3079 audit_msg, vcpu->arch.mmu.root_level,
3081 is_shadow_present_pte(ent));
3082 else if (ent == shadow_notrap_nonpresent_pte
3083 && !is_error_hpa(hpa))
3084 printk(KERN_ERR "audit: (%s) notrap shadow,"
3085 " valid guest gva %lx\n", audit_msg, va);
3086 kvm_release_pfn_clean(pfn);
3092 static void audit_mappings(struct kvm_vcpu *vcpu)
3096 if (vcpu->arch.mmu.root_level == 4)
3097 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3099 for (i = 0; i < 4; ++i)
3100 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3101 audit_mappings_page(vcpu,
3102 vcpu->arch.mmu.pae_root[i],
3107 static int count_rmaps(struct kvm_vcpu *vcpu)
3112 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3113 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3114 struct kvm_rmap_desc *d;
3116 for (j = 0; j < m->npages; ++j) {
3117 unsigned long *rmapp = &m->rmap[j];
3121 if (!(*rmapp & 1)) {
3125 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3127 for (k = 0; k < RMAP_EXT; ++k)
3128 if (d->shadow_ptes[k])
3139 static int count_writable_mappings(struct kvm_vcpu *vcpu)
3142 struct kvm_mmu_page *sp;
3145 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3148 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3151 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3154 if (!(ent & PT_PRESENT_MASK))
3156 if (!(ent & PT_WRITABLE_MASK))
3164 static void audit_rmap(struct kvm_vcpu *vcpu)
3166 int n_rmap = count_rmaps(vcpu);
3167 int n_actual = count_writable_mappings(vcpu);
3169 if (n_rmap != n_actual)
3170 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
3171 __func__, audit_msg, n_rmap, n_actual);
3174 static void audit_write_protection(struct kvm_vcpu *vcpu)
3176 struct kvm_mmu_page *sp;
3177 struct kvm_memory_slot *slot;
3178 unsigned long *rmapp;
3181 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3182 if (sp->role.direct)
3185 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3186 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3187 rmapp = &slot->rmap[gfn - slot->base_gfn];
3189 printk(KERN_ERR "%s: (%s) shadow page has writable"
3190 " mappings: gfn %lx role %x\n",
3191 __func__, audit_msg, sp->gfn,
3196 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3203 audit_write_protection(vcpu);
3204 audit_mappings(vcpu);