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.
22 #include <linux/kvm_host.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
26 #include <linux/highmem.h>
27 #include <linux/module.h>
28 #include <linux/swap.h>
29 #include <linux/hugetlb.h>
30 #include <linux/compiler.h>
33 #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);
73 static int oos_shadow = 1;
74 module_param(oos_shadow, bool, 0644);
77 #define ASSERT(x) do { } while (0)
81 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
82 __FILE__, __LINE__, #x); \
86 #define PT_FIRST_AVAIL_BITS_SHIFT 9
87 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
89 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
91 #define PT64_LEVEL_BITS 9
93 #define PT64_LEVEL_SHIFT(level) \
94 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
96 #define PT64_LEVEL_MASK(level) \
97 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
99 #define PT64_INDEX(address, level)\
100 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
103 #define PT32_LEVEL_BITS 10
105 #define PT32_LEVEL_SHIFT(level) \
106 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
108 #define PT32_LEVEL_MASK(level) \
109 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
111 #define PT32_INDEX(address, level)\
112 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
115 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
116 #define PT64_DIR_BASE_ADDR_MASK \
117 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
119 #define PT32_BASE_ADDR_MASK PAGE_MASK
120 #define PT32_DIR_BASE_ADDR_MASK \
121 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
123 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
126 #define PFERR_PRESENT_MASK (1U << 0)
127 #define PFERR_WRITE_MASK (1U << 1)
128 #define PFERR_USER_MASK (1U << 2)
129 #define PFERR_RSVD_MASK (1U << 3)
130 #define PFERR_FETCH_MASK (1U << 4)
132 #define PT_DIRECTORY_LEVEL 2
133 #define PT_PAGE_TABLE_LEVEL 1
137 #define ACC_EXEC_MASK 1
138 #define ACC_WRITE_MASK PT_WRITABLE_MASK
139 #define ACC_USER_MASK PT_USER_MASK
140 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
142 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
144 struct kvm_rmap_desc {
145 u64 *shadow_ptes[RMAP_EXT];
146 struct kvm_rmap_desc *more;
149 struct kvm_shadow_walk_iterator {
157 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
158 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
159 shadow_walk_okay(&(_walker)); \
160 shadow_walk_next(&(_walker)))
163 struct kvm_unsync_walk {
164 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
167 typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
169 static struct kmem_cache *pte_chain_cache;
170 static struct kmem_cache *rmap_desc_cache;
171 static struct kmem_cache *mmu_page_header_cache;
173 static u64 __read_mostly shadow_trap_nonpresent_pte;
174 static u64 __read_mostly shadow_notrap_nonpresent_pte;
175 static u64 __read_mostly shadow_base_present_pte;
176 static u64 __read_mostly shadow_nx_mask;
177 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
178 static u64 __read_mostly shadow_user_mask;
179 static u64 __read_mostly shadow_accessed_mask;
180 static u64 __read_mostly shadow_dirty_mask;
181 static u64 __read_mostly shadow_mt_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, u64 mt_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;
209 shadow_mt_mask = mt_mask;
211 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
213 static int is_write_protection(struct kvm_vcpu *vcpu)
215 return vcpu->arch.cr0 & X86_CR0_WP;
218 static int is_cpuid_PSE36(void)
223 static int is_nx(struct kvm_vcpu *vcpu)
225 return vcpu->arch.shadow_efer & EFER_NX;
228 static int is_shadow_present_pte(u64 pte)
230 return pte != shadow_trap_nonpresent_pte
231 && pte != shadow_notrap_nonpresent_pte;
234 static int is_large_pte(u64 pte)
236 return pte & PT_PAGE_SIZE_MASK;
239 static int is_writeble_pte(unsigned long pte)
241 return pte & PT_WRITABLE_MASK;
244 static int is_dirty_pte(unsigned long pte)
246 return pte & shadow_dirty_mask;
249 static int is_rmap_pte(u64 pte)
251 return is_shadow_present_pte(pte);
254 static pfn_t spte_to_pfn(u64 pte)
256 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
259 static gfn_t pse36_gfn_delta(u32 gpte)
261 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
263 return (gpte & PT32_DIR_PSE36_MASK) << shift;
266 static void set_shadow_pte(u64 *sptep, u64 spte)
269 set_64bit((unsigned long *)sptep, spte);
271 set_64bit((unsigned long long *)sptep, spte);
275 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
276 struct kmem_cache *base_cache, int min)
280 if (cache->nobjs >= min)
282 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
283 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
286 cache->objects[cache->nobjs++] = obj;
291 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
294 kfree(mc->objects[--mc->nobjs]);
297 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
302 if (cache->nobjs >= min)
304 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
305 page = alloc_page(GFP_KERNEL);
308 set_page_private(page, 0);
309 cache->objects[cache->nobjs++] = page_address(page);
314 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
317 free_page((unsigned long)mc->objects[--mc->nobjs]);
320 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
324 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
328 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
332 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
335 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
336 mmu_page_header_cache, 4);
341 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
343 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
344 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
345 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
346 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
349 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
355 p = mc->objects[--mc->nobjs];
359 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
361 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
362 sizeof(struct kvm_pte_chain));
365 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
370 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
372 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
373 sizeof(struct kvm_rmap_desc));
376 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
382 * Return the pointer to the largepage write count for a given
383 * gfn, handling slots that are not large page aligned.
385 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
389 idx = (gfn / KVM_PAGES_PER_HPAGE) -
390 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
391 return &slot->lpage_info[idx].write_count;
394 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
398 gfn = unalias_gfn(kvm, gfn);
399 write_count = slot_largepage_idx(gfn,
400 gfn_to_memslot_unaliased(kvm, gfn));
404 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
408 gfn = unalias_gfn(kvm, gfn);
409 write_count = slot_largepage_idx(gfn,
410 gfn_to_memslot_unaliased(kvm, gfn));
412 WARN_ON(*write_count < 0);
415 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
417 struct kvm_memory_slot *slot;
420 gfn = unalias_gfn(kvm, gfn);
421 slot = gfn_to_memslot_unaliased(kvm, gfn);
423 largepage_idx = slot_largepage_idx(gfn, slot);
424 return *largepage_idx;
430 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
432 struct vm_area_struct *vma;
436 addr = gfn_to_hva(kvm, gfn);
437 if (kvm_is_error_hva(addr))
440 down_read(¤t->mm->mmap_sem);
441 vma = find_vma(current->mm, addr);
442 if (vma && is_vm_hugetlb_page(vma))
444 up_read(¤t->mm->mmap_sem);
449 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
451 struct kvm_memory_slot *slot;
453 if (has_wrprotected_page(vcpu->kvm, large_gfn))
456 if (!host_largepage_backed(vcpu->kvm, large_gfn))
459 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
460 if (slot && slot->dirty_bitmap)
467 * Take gfn and return the reverse mapping to it.
468 * Note: gfn must be unaliased before this function get called
471 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
473 struct kvm_memory_slot *slot;
476 slot = gfn_to_memslot(kvm, gfn);
478 return &slot->rmap[gfn - slot->base_gfn];
480 idx = (gfn / KVM_PAGES_PER_HPAGE) -
481 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
483 return &slot->lpage_info[idx].rmap_pde;
487 * Reverse mapping data structures:
489 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
490 * that points to page_address(page).
492 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
493 * containing more mappings.
495 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
497 struct kvm_mmu_page *sp;
498 struct kvm_rmap_desc *desc;
499 unsigned long *rmapp;
502 if (!is_rmap_pte(*spte))
504 gfn = unalias_gfn(vcpu->kvm, gfn);
505 sp = page_header(__pa(spte));
506 sp->gfns[spte - sp->spt] = gfn;
507 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
509 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
510 *rmapp = (unsigned long)spte;
511 } else if (!(*rmapp & 1)) {
512 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
513 desc = mmu_alloc_rmap_desc(vcpu);
514 desc->shadow_ptes[0] = (u64 *)*rmapp;
515 desc->shadow_ptes[1] = spte;
516 *rmapp = (unsigned long)desc | 1;
518 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
519 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
520 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
522 if (desc->shadow_ptes[RMAP_EXT-1]) {
523 desc->more = mmu_alloc_rmap_desc(vcpu);
526 for (i = 0; desc->shadow_ptes[i]; ++i)
528 desc->shadow_ptes[i] = spte;
532 static void rmap_desc_remove_entry(unsigned long *rmapp,
533 struct kvm_rmap_desc *desc,
535 struct kvm_rmap_desc *prev_desc)
539 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
541 desc->shadow_ptes[i] = desc->shadow_ptes[j];
542 desc->shadow_ptes[j] = NULL;
545 if (!prev_desc && !desc->more)
546 *rmapp = (unsigned long)desc->shadow_ptes[0];
549 prev_desc->more = desc->more;
551 *rmapp = (unsigned long)desc->more | 1;
552 mmu_free_rmap_desc(desc);
555 static void rmap_remove(struct kvm *kvm, u64 *spte)
557 struct kvm_rmap_desc *desc;
558 struct kvm_rmap_desc *prev_desc;
559 struct kvm_mmu_page *sp;
561 unsigned long *rmapp;
564 if (!is_rmap_pte(*spte))
566 sp = page_header(__pa(spte));
567 pfn = spte_to_pfn(*spte);
568 if (*spte & shadow_accessed_mask)
569 kvm_set_pfn_accessed(pfn);
570 if (is_writeble_pte(*spte))
571 kvm_release_pfn_dirty(pfn);
573 kvm_release_pfn_clean(pfn);
574 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
576 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
578 } else if (!(*rmapp & 1)) {
579 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
580 if ((u64 *)*rmapp != spte) {
581 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
587 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
588 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
591 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
592 if (desc->shadow_ptes[i] == spte) {
593 rmap_desc_remove_entry(rmapp,
605 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
607 struct kvm_rmap_desc *desc;
608 struct kvm_rmap_desc *prev_desc;
614 else if (!(*rmapp & 1)) {
616 return (u64 *)*rmapp;
619 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
623 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
624 if (prev_spte == spte)
625 return desc->shadow_ptes[i];
626 prev_spte = desc->shadow_ptes[i];
633 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
635 unsigned long *rmapp;
637 int write_protected = 0;
639 gfn = unalias_gfn(kvm, gfn);
640 rmapp = gfn_to_rmap(kvm, gfn, 0);
642 spte = rmap_next(kvm, rmapp, NULL);
645 BUG_ON(!(*spte & PT_PRESENT_MASK));
646 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
647 if (is_writeble_pte(*spte)) {
648 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
651 spte = rmap_next(kvm, rmapp, spte);
653 if (write_protected) {
656 spte = rmap_next(kvm, rmapp, NULL);
657 pfn = spte_to_pfn(*spte);
658 kvm_set_pfn_dirty(pfn);
661 /* check for huge page mappings */
662 rmapp = gfn_to_rmap(kvm, gfn, 1);
663 spte = rmap_next(kvm, rmapp, NULL);
666 BUG_ON(!(*spte & PT_PRESENT_MASK));
667 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
668 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
669 if (is_writeble_pte(*spte)) {
670 rmap_remove(kvm, spte);
672 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
676 spte = rmap_next(kvm, rmapp, spte);
679 return write_protected;
682 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
685 int need_tlb_flush = 0;
687 while ((spte = rmap_next(kvm, rmapp, NULL))) {
688 BUG_ON(!(*spte & PT_PRESENT_MASK));
689 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
690 rmap_remove(kvm, spte);
691 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
694 return need_tlb_flush;
697 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
698 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
704 * If mmap_sem isn't taken, we can look the memslots with only
705 * the mmu_lock by skipping over the slots with userspace_addr == 0.
707 for (i = 0; i < kvm->nmemslots; i++) {
708 struct kvm_memory_slot *memslot = &kvm->memslots[i];
709 unsigned long start = memslot->userspace_addr;
712 /* mmu_lock protects userspace_addr */
716 end = start + (memslot->npages << PAGE_SHIFT);
717 if (hva >= start && hva < end) {
718 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
719 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
720 retval |= handler(kvm,
721 &memslot->lpage_info[
723 KVM_PAGES_PER_HPAGE].rmap_pde);
730 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
732 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
735 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
740 /* always return old for EPT */
741 if (!shadow_accessed_mask)
744 spte = rmap_next(kvm, rmapp, NULL);
748 BUG_ON(!(_spte & PT_PRESENT_MASK));
749 _young = _spte & PT_ACCESSED_MASK;
752 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
754 spte = rmap_next(kvm, rmapp, spte);
759 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
761 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
765 static int is_empty_shadow_page(u64 *spt)
770 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
771 if (is_shadow_present_pte(*pos)) {
772 printk(KERN_ERR "%s: %p %llx\n", __func__,
780 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
782 ASSERT(is_empty_shadow_page(sp->spt));
784 __free_page(virt_to_page(sp->spt));
785 __free_page(virt_to_page(sp->gfns));
787 ++kvm->arch.n_free_mmu_pages;
790 static unsigned kvm_page_table_hashfn(gfn_t gfn)
792 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
795 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
798 struct kvm_mmu_page *sp;
800 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
801 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
802 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
803 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
804 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
805 INIT_LIST_HEAD(&sp->oos_link);
806 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
808 sp->parent_pte = parent_pte;
809 --vcpu->kvm->arch.n_free_mmu_pages;
813 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
814 struct kvm_mmu_page *sp, u64 *parent_pte)
816 struct kvm_pte_chain *pte_chain;
817 struct hlist_node *node;
822 if (!sp->multimapped) {
823 u64 *old = sp->parent_pte;
826 sp->parent_pte = parent_pte;
830 pte_chain = mmu_alloc_pte_chain(vcpu);
831 INIT_HLIST_HEAD(&sp->parent_ptes);
832 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
833 pte_chain->parent_ptes[0] = old;
835 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
836 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
838 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
839 if (!pte_chain->parent_ptes[i]) {
840 pte_chain->parent_ptes[i] = parent_pte;
844 pte_chain = mmu_alloc_pte_chain(vcpu);
846 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
847 pte_chain->parent_ptes[0] = parent_pte;
850 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
853 struct kvm_pte_chain *pte_chain;
854 struct hlist_node *node;
857 if (!sp->multimapped) {
858 BUG_ON(sp->parent_pte != parent_pte);
859 sp->parent_pte = NULL;
862 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
863 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
864 if (!pte_chain->parent_ptes[i])
866 if (pte_chain->parent_ptes[i] != parent_pte)
868 while (i + 1 < NR_PTE_CHAIN_ENTRIES
869 && pte_chain->parent_ptes[i + 1]) {
870 pte_chain->parent_ptes[i]
871 = pte_chain->parent_ptes[i + 1];
874 pte_chain->parent_ptes[i] = NULL;
876 hlist_del(&pte_chain->link);
877 mmu_free_pte_chain(pte_chain);
878 if (hlist_empty(&sp->parent_ptes)) {
880 sp->parent_pte = NULL;
889 static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
890 mmu_parent_walk_fn fn)
892 struct kvm_pte_chain *pte_chain;
893 struct hlist_node *node;
894 struct kvm_mmu_page *parent_sp;
897 if (!sp->multimapped && sp->parent_pte) {
898 parent_sp = page_header(__pa(sp->parent_pte));
900 mmu_parent_walk(vcpu, parent_sp, fn);
903 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
904 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
905 if (!pte_chain->parent_ptes[i])
907 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
909 mmu_parent_walk(vcpu, parent_sp, fn);
913 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
916 struct kvm_mmu_page *sp = page_header(__pa(spte));
918 index = spte - sp->spt;
919 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
920 sp->unsync_children++;
921 WARN_ON(!sp->unsync_children);
924 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
926 struct kvm_pte_chain *pte_chain;
927 struct hlist_node *node;
933 if (!sp->multimapped) {
934 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
938 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
939 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
940 if (!pte_chain->parent_ptes[i])
942 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
946 static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
948 kvm_mmu_update_parents_unsync(sp);
952 static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
953 struct kvm_mmu_page *sp)
955 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
956 kvm_mmu_update_parents_unsync(sp);
959 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
960 struct kvm_mmu_page *sp)
964 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
965 sp->spt[i] = shadow_trap_nonpresent_pte;
968 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
969 struct kvm_mmu_page *sp)
974 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
978 #define KVM_PAGE_ARRAY_NR 16
980 struct kvm_mmu_pages {
981 struct mmu_page_and_offset {
982 struct kvm_mmu_page *sp;
984 } page[KVM_PAGE_ARRAY_NR];
988 #define for_each_unsync_children(bitmap, idx) \
989 for (idx = find_first_bit(bitmap, 512); \
991 idx = find_next_bit(bitmap, 512, idx+1))
993 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
999 for (i=0; i < pvec->nr; i++)
1000 if (pvec->page[i].sp == sp)
1003 pvec->page[pvec->nr].sp = sp;
1004 pvec->page[pvec->nr].idx = idx;
1006 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1009 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1010 struct kvm_mmu_pages *pvec)
1012 int i, ret, nr_unsync_leaf = 0;
1014 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1015 u64 ent = sp->spt[i];
1017 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1018 struct kvm_mmu_page *child;
1019 child = page_header(ent & PT64_BASE_ADDR_MASK);
1021 if (child->unsync_children) {
1022 if (mmu_pages_add(pvec, child, i))
1025 ret = __mmu_unsync_walk(child, pvec);
1027 __clear_bit(i, sp->unsync_child_bitmap);
1029 nr_unsync_leaf += ret;
1034 if (child->unsync) {
1036 if (mmu_pages_add(pvec, child, i))
1042 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1043 sp->unsync_children = 0;
1045 return nr_unsync_leaf;
1048 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1049 struct kvm_mmu_pages *pvec)
1051 if (!sp->unsync_children)
1054 mmu_pages_add(pvec, sp, 0);
1055 return __mmu_unsync_walk(sp, pvec);
1058 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1061 struct hlist_head *bucket;
1062 struct kvm_mmu_page *sp;
1063 struct hlist_node *node;
1065 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1066 index = kvm_page_table_hashfn(gfn);
1067 bucket = &kvm->arch.mmu_page_hash[index];
1068 hlist_for_each_entry(sp, node, bucket, hash_link)
1069 if (sp->gfn == gfn && !sp->role.direct
1070 && !sp->role.invalid) {
1071 pgprintk("%s: found role %x\n",
1072 __func__, sp->role.word);
1078 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1080 WARN_ON(!sp->unsync);
1082 --kvm->stat.mmu_unsync;
1085 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1087 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1089 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1090 kvm_mmu_zap_page(vcpu->kvm, sp);
1094 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1095 kvm_flush_remote_tlbs(vcpu->kvm);
1096 kvm_unlink_unsync_page(vcpu->kvm, sp);
1097 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1098 kvm_mmu_zap_page(vcpu->kvm, sp);
1102 kvm_mmu_flush_tlb(vcpu);
1106 struct mmu_page_path {
1107 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1108 unsigned int idx[PT64_ROOT_LEVEL-1];
1111 #define for_each_sp(pvec, sp, parents, i) \
1112 for (i = mmu_pages_next(&pvec, &parents, -1), \
1113 sp = pvec.page[i].sp; \
1114 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1115 i = mmu_pages_next(&pvec, &parents, i))
1117 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1118 struct mmu_page_path *parents,
1123 for (n = i+1; n < pvec->nr; n++) {
1124 struct kvm_mmu_page *sp = pvec->page[n].sp;
1126 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1127 parents->idx[0] = pvec->page[n].idx;
1131 parents->parent[sp->role.level-2] = sp;
1132 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1138 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1140 struct kvm_mmu_page *sp;
1141 unsigned int level = 0;
1144 unsigned int idx = parents->idx[level];
1146 sp = parents->parent[level];
1150 --sp->unsync_children;
1151 WARN_ON((int)sp->unsync_children < 0);
1152 __clear_bit(idx, sp->unsync_child_bitmap);
1154 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1157 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1158 struct mmu_page_path *parents,
1159 struct kvm_mmu_pages *pvec)
1161 parents->parent[parent->role.level-1] = NULL;
1165 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1166 struct kvm_mmu_page *parent)
1169 struct kvm_mmu_page *sp;
1170 struct mmu_page_path parents;
1171 struct kvm_mmu_pages pages;
1173 kvm_mmu_pages_init(parent, &parents, &pages);
1174 while (mmu_unsync_walk(parent, &pages)) {
1177 for_each_sp(pages, sp, parents, i)
1178 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1181 kvm_flush_remote_tlbs(vcpu->kvm);
1183 for_each_sp(pages, sp, parents, i) {
1184 kvm_sync_page(vcpu, sp);
1185 mmu_pages_clear_parents(&parents);
1187 cond_resched_lock(&vcpu->kvm->mmu_lock);
1188 kvm_mmu_pages_init(parent, &parents, &pages);
1192 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1200 union kvm_mmu_page_role role;
1203 struct hlist_head *bucket;
1204 struct kvm_mmu_page *sp;
1205 struct hlist_node *node, *tmp;
1207 role = vcpu->arch.mmu.base_role;
1209 role.direct = direct;
1210 role.access = access;
1211 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1212 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1213 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1214 role.quadrant = quadrant;
1216 pgprintk("%s: looking gfn %lx role %x\n", __func__,
1218 index = kvm_page_table_hashfn(gfn);
1219 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1220 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1221 if (sp->gfn == gfn) {
1223 if (kvm_sync_page(vcpu, sp))
1226 if (sp->role.word != role.word)
1229 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1230 if (sp->unsync_children) {
1231 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1232 kvm_mmu_mark_parents_unsync(vcpu, sp);
1234 pgprintk("%s: found\n", __func__);
1237 ++vcpu->kvm->stat.mmu_cache_miss;
1238 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1241 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
1244 hlist_add_head(&sp->hash_link, bucket);
1246 if (rmap_write_protect(vcpu->kvm, gfn))
1247 kvm_flush_remote_tlbs(vcpu->kvm);
1248 account_shadowed(vcpu->kvm, gfn);
1250 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1251 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1253 nonpaging_prefetch_page(vcpu, sp);
1257 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1258 struct kvm_vcpu *vcpu, u64 addr)
1260 iterator->addr = addr;
1261 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1262 iterator->level = vcpu->arch.mmu.shadow_root_level;
1263 if (iterator->level == PT32E_ROOT_LEVEL) {
1264 iterator->shadow_addr
1265 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1266 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1268 if (!iterator->shadow_addr)
1269 iterator->level = 0;
1273 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1275 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1277 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1278 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1282 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1284 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1288 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1289 struct kvm_mmu_page *sp)
1297 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1298 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1299 if (is_shadow_present_pte(pt[i]))
1300 rmap_remove(kvm, &pt[i]);
1301 pt[i] = shadow_trap_nonpresent_pte;
1306 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1309 if (is_shadow_present_pte(ent)) {
1310 if (!is_large_pte(ent)) {
1311 ent &= PT64_BASE_ADDR_MASK;
1312 mmu_page_remove_parent_pte(page_header(ent),
1316 rmap_remove(kvm, &pt[i]);
1319 pt[i] = shadow_trap_nonpresent_pte;
1323 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1325 mmu_page_remove_parent_pte(sp, parent_pte);
1328 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1332 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1334 kvm->vcpus[i]->arch.last_pte_updated = NULL;
1337 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1341 while (sp->multimapped || sp->parent_pte) {
1342 if (!sp->multimapped)
1343 parent_pte = sp->parent_pte;
1345 struct kvm_pte_chain *chain;
1347 chain = container_of(sp->parent_ptes.first,
1348 struct kvm_pte_chain, link);
1349 parent_pte = chain->parent_ptes[0];
1351 BUG_ON(!parent_pte);
1352 kvm_mmu_put_page(sp, parent_pte);
1353 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1357 static int mmu_zap_unsync_children(struct kvm *kvm,
1358 struct kvm_mmu_page *parent)
1361 struct mmu_page_path parents;
1362 struct kvm_mmu_pages pages;
1364 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1367 kvm_mmu_pages_init(parent, &parents, &pages);
1368 while (mmu_unsync_walk(parent, &pages)) {
1369 struct kvm_mmu_page *sp;
1371 for_each_sp(pages, sp, parents, i) {
1372 kvm_mmu_zap_page(kvm, sp);
1373 mmu_pages_clear_parents(&parents);
1376 kvm_mmu_pages_init(parent, &parents, &pages);
1382 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1385 ++kvm->stat.mmu_shadow_zapped;
1386 ret = mmu_zap_unsync_children(kvm, sp);
1387 kvm_mmu_page_unlink_children(kvm, sp);
1388 kvm_mmu_unlink_parents(kvm, sp);
1389 kvm_flush_remote_tlbs(kvm);
1390 if (!sp->role.invalid && !sp->role.direct)
1391 unaccount_shadowed(kvm, sp->gfn);
1393 kvm_unlink_unsync_page(kvm, sp);
1394 if (!sp->root_count) {
1395 hlist_del(&sp->hash_link);
1396 kvm_mmu_free_page(kvm, sp);
1398 sp->role.invalid = 1;
1399 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1400 kvm_reload_remote_mmus(kvm);
1402 kvm_mmu_reset_last_pte_updated(kvm);
1407 * Changing the number of mmu pages allocated to the vm
1408 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1410 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1413 * If we set the number of mmu pages to be smaller be than the
1414 * number of actived pages , we must to free some mmu pages before we
1418 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
1420 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1421 - kvm->arch.n_free_mmu_pages;
1423 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1424 struct kvm_mmu_page *page;
1426 page = container_of(kvm->arch.active_mmu_pages.prev,
1427 struct kvm_mmu_page, link);
1428 kvm_mmu_zap_page(kvm, page);
1431 kvm->arch.n_free_mmu_pages = 0;
1434 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1435 - kvm->arch.n_alloc_mmu_pages;
1437 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1440 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1443 struct hlist_head *bucket;
1444 struct kvm_mmu_page *sp;
1445 struct hlist_node *node, *n;
1448 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1450 index = kvm_page_table_hashfn(gfn);
1451 bucket = &kvm->arch.mmu_page_hash[index];
1452 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1453 if (sp->gfn == gfn && !sp->role.direct) {
1454 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1457 if (kvm_mmu_zap_page(kvm, sp))
1463 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1466 struct hlist_head *bucket;
1467 struct kvm_mmu_page *sp;
1468 struct hlist_node *node, *nn;
1470 index = kvm_page_table_hashfn(gfn);
1471 bucket = &kvm->arch.mmu_page_hash[index];
1472 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1473 if (sp->gfn == gfn && !sp->role.direct
1474 && !sp->role.invalid) {
1475 pgprintk("%s: zap %lx %x\n",
1476 __func__, gfn, sp->role.word);
1477 kvm_mmu_zap_page(kvm, sp);
1482 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1484 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1485 struct kvm_mmu_page *sp = page_header(__pa(pte));
1487 __set_bit(slot, sp->slot_bitmap);
1490 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1495 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1498 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1499 if (pt[i] == shadow_notrap_nonpresent_pte)
1500 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1504 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1508 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1510 if (gpa == UNMAPPED_GVA)
1513 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1519 * The function is based on mtrr_type_lookup() in
1520 * arch/x86/kernel/cpu/mtrr/generic.c
1522 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1527 u8 prev_match, curr_match;
1528 int num_var_ranges = KVM_NR_VAR_MTRR;
1530 if (!mtrr_state->enabled)
1533 /* Make end inclusive end, instead of exclusive */
1536 /* Look in fixed ranges. Just return the type as per start */
1537 if (mtrr_state->have_fixed && (start < 0x100000)) {
1540 if (start < 0x80000) {
1542 idx += (start >> 16);
1543 return mtrr_state->fixed_ranges[idx];
1544 } else if (start < 0xC0000) {
1546 idx += ((start - 0x80000) >> 14);
1547 return mtrr_state->fixed_ranges[idx];
1548 } else if (start < 0x1000000) {
1550 idx += ((start - 0xC0000) >> 12);
1551 return mtrr_state->fixed_ranges[idx];
1556 * Look in variable ranges
1557 * Look of multiple ranges matching this address and pick type
1558 * as per MTRR precedence
1560 if (!(mtrr_state->enabled & 2))
1561 return mtrr_state->def_type;
1564 for (i = 0; i < num_var_ranges; ++i) {
1565 unsigned short start_state, end_state;
1567 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1570 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1571 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1572 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1573 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1575 start_state = ((start & mask) == (base & mask));
1576 end_state = ((end & mask) == (base & mask));
1577 if (start_state != end_state)
1580 if ((start & mask) != (base & mask))
1583 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1584 if (prev_match == 0xFF) {
1585 prev_match = curr_match;
1589 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1590 curr_match == MTRR_TYPE_UNCACHABLE)
1591 return MTRR_TYPE_UNCACHABLE;
1593 if ((prev_match == MTRR_TYPE_WRBACK &&
1594 curr_match == MTRR_TYPE_WRTHROUGH) ||
1595 (prev_match == MTRR_TYPE_WRTHROUGH &&
1596 curr_match == MTRR_TYPE_WRBACK)) {
1597 prev_match = MTRR_TYPE_WRTHROUGH;
1598 curr_match = MTRR_TYPE_WRTHROUGH;
1601 if (prev_match != curr_match)
1602 return MTRR_TYPE_UNCACHABLE;
1605 if (prev_match != 0xFF)
1608 return mtrr_state->def_type;
1611 static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1615 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1616 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1617 if (mtrr == 0xfe || mtrr == 0xff)
1618 mtrr = MTRR_TYPE_WRBACK;
1622 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1625 struct hlist_head *bucket;
1626 struct kvm_mmu_page *s;
1627 struct hlist_node *node, *n;
1629 index = kvm_page_table_hashfn(sp->gfn);
1630 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1631 /* don't unsync if pagetable is shadowed with multiple roles */
1632 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1633 if (s->gfn != sp->gfn || s->role.direct)
1635 if (s->role.word != sp->role.word)
1638 ++vcpu->kvm->stat.mmu_unsync;
1641 kvm_mmu_mark_parents_unsync(vcpu, sp);
1643 mmu_convert_notrap(sp);
1647 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1650 struct kvm_mmu_page *shadow;
1652 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1654 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1658 if (can_unsync && oos_shadow)
1659 return kvm_unsync_page(vcpu, shadow);
1665 static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1666 unsigned pte_access, int user_fault,
1667 int write_fault, int dirty, int largepage,
1668 gfn_t gfn, pfn_t pfn, bool speculative,
1673 u64 mt_mask = shadow_mt_mask;
1676 * We don't set the accessed bit, since we sometimes want to see
1677 * whether the guest actually used the pte (in order to detect
1680 spte = shadow_base_present_pte | shadow_dirty_mask;
1682 spte |= shadow_accessed_mask;
1684 pte_access &= ~ACC_WRITE_MASK;
1685 if (pte_access & ACC_EXEC_MASK)
1686 spte |= shadow_x_mask;
1688 spte |= shadow_nx_mask;
1689 if (pte_access & ACC_USER_MASK)
1690 spte |= shadow_user_mask;
1692 spte |= PT_PAGE_SIZE_MASK;
1694 if (!kvm_is_mmio_pfn(pfn)) {
1695 mt_mask = get_memory_type(vcpu, gfn) <<
1696 kvm_x86_ops->get_mt_mask_shift();
1697 mt_mask |= VMX_EPT_IGMT_BIT;
1699 mt_mask = MTRR_TYPE_UNCACHABLE <<
1700 kvm_x86_ops->get_mt_mask_shift();
1704 spte |= (u64)pfn << PAGE_SHIFT;
1706 if ((pte_access & ACC_WRITE_MASK)
1707 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1709 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1711 spte = shadow_trap_nonpresent_pte;
1715 spte |= PT_WRITABLE_MASK;
1718 * Optimization: for pte sync, if spte was writable the hash
1719 * lookup is unnecessary (and expensive). Write protection
1720 * is responsibility of mmu_get_page / kvm_sync_page.
1721 * Same reasoning can be applied to dirty page accounting.
1723 if (!can_unsync && is_writeble_pte(*shadow_pte))
1726 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1727 pgprintk("%s: found shadow page for %lx, marking ro\n",
1730 pte_access &= ~ACC_WRITE_MASK;
1731 if (is_writeble_pte(spte))
1732 spte &= ~PT_WRITABLE_MASK;
1736 if (pte_access & ACC_WRITE_MASK)
1737 mark_page_dirty(vcpu->kvm, gfn);
1740 set_shadow_pte(shadow_pte, spte);
1744 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1745 unsigned pt_access, unsigned pte_access,
1746 int user_fault, int write_fault, int dirty,
1747 int *ptwrite, int largepage, gfn_t gfn,
1748 pfn_t pfn, bool speculative)
1750 int was_rmapped = 0;
1751 int was_writeble = is_writeble_pte(*shadow_pte);
1753 pgprintk("%s: spte %llx access %x write_fault %d"
1754 " user_fault %d gfn %lx\n",
1755 __func__, *shadow_pte, pt_access,
1756 write_fault, user_fault, gfn);
1758 if (is_rmap_pte(*shadow_pte)) {
1760 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1761 * the parent of the now unreachable PTE.
1763 if (largepage && !is_large_pte(*shadow_pte)) {
1764 struct kvm_mmu_page *child;
1765 u64 pte = *shadow_pte;
1767 child = page_header(pte & PT64_BASE_ADDR_MASK);
1768 mmu_page_remove_parent_pte(child, shadow_pte);
1769 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1770 pgprintk("hfn old %lx new %lx\n",
1771 spte_to_pfn(*shadow_pte), pfn);
1772 rmap_remove(vcpu->kvm, shadow_pte);
1776 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
1777 dirty, largepage, gfn, pfn, speculative, true)) {
1780 kvm_x86_ops->tlb_flush(vcpu);
1783 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1784 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1785 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1786 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1787 *shadow_pte, shadow_pte);
1788 if (!was_rmapped && is_large_pte(*shadow_pte))
1789 ++vcpu->kvm->stat.lpages;
1791 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1793 rmap_add(vcpu, shadow_pte, gfn, largepage);
1794 if (!is_rmap_pte(*shadow_pte))
1795 kvm_release_pfn_clean(pfn);
1798 kvm_release_pfn_dirty(pfn);
1800 kvm_release_pfn_clean(pfn);
1803 vcpu->arch.last_pte_updated = shadow_pte;
1804 vcpu->arch.last_pte_gfn = gfn;
1808 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1812 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1813 int largepage, gfn_t gfn, pfn_t pfn)
1815 struct kvm_shadow_walk_iterator iterator;
1816 struct kvm_mmu_page *sp;
1820 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1821 if (iterator.level == PT_PAGE_TABLE_LEVEL
1822 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1823 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1824 0, write, 1, &pt_write,
1825 largepage, gfn, pfn, false);
1826 ++vcpu->stat.pf_fixed;
1830 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1831 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1832 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1834 1, ACC_ALL, iterator.sptep);
1836 pgprintk("nonpaging_map: ENOMEM\n");
1837 kvm_release_pfn_clean(pfn);
1841 set_shadow_pte(iterator.sptep,
1843 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1844 | shadow_user_mask | shadow_x_mask);
1850 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1855 unsigned long mmu_seq;
1857 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1858 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1862 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1864 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1867 if (is_error_pfn(pfn)) {
1868 kvm_release_pfn_clean(pfn);
1872 spin_lock(&vcpu->kvm->mmu_lock);
1873 if (mmu_notifier_retry(vcpu, mmu_seq))
1875 kvm_mmu_free_some_pages(vcpu);
1876 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1877 spin_unlock(&vcpu->kvm->mmu_lock);
1883 spin_unlock(&vcpu->kvm->mmu_lock);
1884 kvm_release_pfn_clean(pfn);
1889 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1892 struct kvm_mmu_page *sp;
1894 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1896 spin_lock(&vcpu->kvm->mmu_lock);
1897 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1898 hpa_t root = vcpu->arch.mmu.root_hpa;
1900 sp = page_header(root);
1902 if (!sp->root_count && sp->role.invalid)
1903 kvm_mmu_zap_page(vcpu->kvm, sp);
1904 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1905 spin_unlock(&vcpu->kvm->mmu_lock);
1908 for (i = 0; i < 4; ++i) {
1909 hpa_t root = vcpu->arch.mmu.pae_root[i];
1912 root &= PT64_BASE_ADDR_MASK;
1913 sp = page_header(root);
1915 if (!sp->root_count && sp->role.invalid)
1916 kvm_mmu_zap_page(vcpu->kvm, sp);
1918 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1920 spin_unlock(&vcpu->kvm->mmu_lock);
1921 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1924 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1928 struct kvm_mmu_page *sp;
1931 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1933 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1934 hpa_t root = vcpu->arch.mmu.root_hpa;
1936 ASSERT(!VALID_PAGE(root));
1939 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1940 PT64_ROOT_LEVEL, direct,
1942 root = __pa(sp->spt);
1944 vcpu->arch.mmu.root_hpa = root;
1947 direct = !is_paging(vcpu);
1950 for (i = 0; i < 4; ++i) {
1951 hpa_t root = vcpu->arch.mmu.pae_root[i];
1953 ASSERT(!VALID_PAGE(root));
1954 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1955 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1956 vcpu->arch.mmu.pae_root[i] = 0;
1959 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1960 } else if (vcpu->arch.mmu.root_level == 0)
1962 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1963 PT32_ROOT_LEVEL, direct,
1965 root = __pa(sp->spt);
1967 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1969 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1972 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1975 struct kvm_mmu_page *sp;
1977 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1979 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1980 hpa_t root = vcpu->arch.mmu.root_hpa;
1981 sp = page_header(root);
1982 mmu_sync_children(vcpu, sp);
1985 for (i = 0; i < 4; ++i) {
1986 hpa_t root = vcpu->arch.mmu.pae_root[i];
1989 root &= PT64_BASE_ADDR_MASK;
1990 sp = page_header(root);
1991 mmu_sync_children(vcpu, sp);
1996 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1998 spin_lock(&vcpu->kvm->mmu_lock);
1999 mmu_sync_roots(vcpu);
2000 spin_unlock(&vcpu->kvm->mmu_lock);
2003 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2008 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2014 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2015 r = mmu_topup_memory_caches(vcpu);
2020 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2022 gfn = gva >> PAGE_SHIFT;
2024 return nonpaging_map(vcpu, gva & PAGE_MASK,
2025 error_code & PFERR_WRITE_MASK, gfn);
2028 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2034 gfn_t gfn = gpa >> PAGE_SHIFT;
2035 unsigned long mmu_seq;
2038 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2040 r = mmu_topup_memory_caches(vcpu);
2044 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2045 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2048 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2050 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2051 if (is_error_pfn(pfn)) {
2052 kvm_release_pfn_clean(pfn);
2055 spin_lock(&vcpu->kvm->mmu_lock);
2056 if (mmu_notifier_retry(vcpu, mmu_seq))
2058 kvm_mmu_free_some_pages(vcpu);
2059 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2060 largepage, gfn, pfn);
2061 spin_unlock(&vcpu->kvm->mmu_lock);
2066 spin_unlock(&vcpu->kvm->mmu_lock);
2067 kvm_release_pfn_clean(pfn);
2071 static void nonpaging_free(struct kvm_vcpu *vcpu)
2073 mmu_free_roots(vcpu);
2076 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2078 struct kvm_mmu *context = &vcpu->arch.mmu;
2080 context->new_cr3 = nonpaging_new_cr3;
2081 context->page_fault = nonpaging_page_fault;
2082 context->gva_to_gpa = nonpaging_gva_to_gpa;
2083 context->free = nonpaging_free;
2084 context->prefetch_page = nonpaging_prefetch_page;
2085 context->sync_page = nonpaging_sync_page;
2086 context->invlpg = nonpaging_invlpg;
2087 context->root_level = 0;
2088 context->shadow_root_level = PT32E_ROOT_LEVEL;
2089 context->root_hpa = INVALID_PAGE;
2093 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2095 ++vcpu->stat.tlb_flush;
2096 kvm_x86_ops->tlb_flush(vcpu);
2099 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2101 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2102 mmu_free_roots(vcpu);
2105 static void inject_page_fault(struct kvm_vcpu *vcpu,
2109 kvm_inject_page_fault(vcpu, addr, err_code);
2112 static void paging_free(struct kvm_vcpu *vcpu)
2114 nonpaging_free(vcpu);
2117 static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2121 bit7 = (gpte >> 7) & 1;
2122 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2126 #include "paging_tmpl.h"
2130 #include "paging_tmpl.h"
2133 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2135 struct kvm_mmu *context = &vcpu->arch.mmu;
2136 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2137 u64 exb_bit_rsvd = 0;
2140 exb_bit_rsvd = rsvd_bits(63, 63);
2142 case PT32_ROOT_LEVEL:
2143 /* no rsvd bits for 2 level 4K page table entries */
2144 context->rsvd_bits_mask[0][1] = 0;
2145 context->rsvd_bits_mask[0][0] = 0;
2146 if (is_cpuid_PSE36())
2147 /* 36bits PSE 4MB page */
2148 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2150 /* 32 bits PSE 4MB page */
2151 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2152 context->rsvd_bits_mask[1][0] = ~0ull;
2154 case PT32E_ROOT_LEVEL:
2155 context->rsvd_bits_mask[0][2] =
2156 rsvd_bits(maxphyaddr, 63) |
2157 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2158 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2159 rsvd_bits(maxphyaddr, 62); /* PDE */
2160 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2161 rsvd_bits(maxphyaddr, 62); /* PTE */
2162 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2163 rsvd_bits(maxphyaddr, 62) |
2164 rsvd_bits(13, 20); /* large page */
2165 context->rsvd_bits_mask[1][0] = ~0ull;
2167 case PT64_ROOT_LEVEL:
2168 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2169 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2170 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2171 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2172 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2173 rsvd_bits(maxphyaddr, 51);
2174 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2175 rsvd_bits(maxphyaddr, 51);
2176 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2177 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2178 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2179 rsvd_bits(maxphyaddr, 51) |
2180 rsvd_bits(13, 20); /* large page */
2181 context->rsvd_bits_mask[1][0] = ~0ull;
2186 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2188 struct kvm_mmu *context = &vcpu->arch.mmu;
2190 ASSERT(is_pae(vcpu));
2191 context->new_cr3 = paging_new_cr3;
2192 context->page_fault = paging64_page_fault;
2193 context->gva_to_gpa = paging64_gva_to_gpa;
2194 context->prefetch_page = paging64_prefetch_page;
2195 context->sync_page = paging64_sync_page;
2196 context->invlpg = paging64_invlpg;
2197 context->free = paging_free;
2198 context->root_level = level;
2199 context->shadow_root_level = level;
2200 context->root_hpa = INVALID_PAGE;
2204 static int paging64_init_context(struct kvm_vcpu *vcpu)
2206 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2207 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2210 static int paging32_init_context(struct kvm_vcpu *vcpu)
2212 struct kvm_mmu *context = &vcpu->arch.mmu;
2214 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2215 context->new_cr3 = paging_new_cr3;
2216 context->page_fault = paging32_page_fault;
2217 context->gva_to_gpa = paging32_gva_to_gpa;
2218 context->free = paging_free;
2219 context->prefetch_page = paging32_prefetch_page;
2220 context->sync_page = paging32_sync_page;
2221 context->invlpg = paging32_invlpg;
2222 context->root_level = PT32_ROOT_LEVEL;
2223 context->shadow_root_level = PT32E_ROOT_LEVEL;
2224 context->root_hpa = INVALID_PAGE;
2228 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2230 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2231 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2234 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2236 struct kvm_mmu *context = &vcpu->arch.mmu;
2238 context->new_cr3 = nonpaging_new_cr3;
2239 context->page_fault = tdp_page_fault;
2240 context->free = nonpaging_free;
2241 context->prefetch_page = nonpaging_prefetch_page;
2242 context->sync_page = nonpaging_sync_page;
2243 context->invlpg = nonpaging_invlpg;
2244 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2245 context->root_hpa = INVALID_PAGE;
2247 if (!is_paging(vcpu)) {
2248 context->gva_to_gpa = nonpaging_gva_to_gpa;
2249 context->root_level = 0;
2250 } else if (is_long_mode(vcpu)) {
2251 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2252 context->gva_to_gpa = paging64_gva_to_gpa;
2253 context->root_level = PT64_ROOT_LEVEL;
2254 } else if (is_pae(vcpu)) {
2255 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2256 context->gva_to_gpa = paging64_gva_to_gpa;
2257 context->root_level = PT32E_ROOT_LEVEL;
2259 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2260 context->gva_to_gpa = paging32_gva_to_gpa;
2261 context->root_level = PT32_ROOT_LEVEL;
2267 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2272 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2274 if (!is_paging(vcpu))
2275 r = nonpaging_init_context(vcpu);
2276 else if (is_long_mode(vcpu))
2277 r = paging64_init_context(vcpu);
2278 else if (is_pae(vcpu))
2279 r = paging32E_init_context(vcpu);
2281 r = paging32_init_context(vcpu);
2283 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2288 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2290 vcpu->arch.update_pte.pfn = bad_pfn;
2293 return init_kvm_tdp_mmu(vcpu);
2295 return init_kvm_softmmu(vcpu);
2298 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2301 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2302 vcpu->arch.mmu.free(vcpu);
2303 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2307 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2309 destroy_kvm_mmu(vcpu);
2310 return init_kvm_mmu(vcpu);
2312 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2314 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2318 r = mmu_topup_memory_caches(vcpu);
2321 spin_lock(&vcpu->kvm->mmu_lock);
2322 kvm_mmu_free_some_pages(vcpu);
2323 mmu_alloc_roots(vcpu);
2324 mmu_sync_roots(vcpu);
2325 spin_unlock(&vcpu->kvm->mmu_lock);
2326 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2327 kvm_mmu_flush_tlb(vcpu);
2331 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2333 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2335 mmu_free_roots(vcpu);
2338 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2339 struct kvm_mmu_page *sp,
2343 struct kvm_mmu_page *child;
2346 if (is_shadow_present_pte(pte)) {
2347 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2349 rmap_remove(vcpu->kvm, spte);
2351 child = page_header(pte & PT64_BASE_ADDR_MASK);
2352 mmu_page_remove_parent_pte(child, spte);
2355 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
2356 if (is_large_pte(pte))
2357 --vcpu->kvm->stat.lpages;
2360 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2361 struct kvm_mmu_page *sp,
2365 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2366 if (!vcpu->arch.update_pte.largepage ||
2367 sp->role.glevels == PT32_ROOT_LEVEL) {
2368 ++vcpu->kvm->stat.mmu_pde_zapped;
2373 ++vcpu->kvm->stat.mmu_pte_updated;
2374 if (sp->role.glevels == PT32_ROOT_LEVEL)
2375 paging32_update_pte(vcpu, sp, spte, new);
2377 paging64_update_pte(vcpu, sp, spte, new);
2380 static bool need_remote_flush(u64 old, u64 new)
2382 if (!is_shadow_present_pte(old))
2384 if (!is_shadow_present_pte(new))
2386 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2388 old ^= PT64_NX_MASK;
2389 new ^= PT64_NX_MASK;
2390 return (old & ~new & PT64_PERM_MASK) != 0;
2393 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2395 if (need_remote_flush(old, new))
2396 kvm_flush_remote_tlbs(vcpu->kvm);
2398 kvm_mmu_flush_tlb(vcpu);
2401 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2403 u64 *spte = vcpu->arch.last_pte_updated;
2405 return !!(spte && (*spte & shadow_accessed_mask));
2408 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2409 const u8 *new, int bytes)
2416 vcpu->arch.update_pte.largepage = 0;
2418 if (bytes != 4 && bytes != 8)
2422 * Assume that the pte write on a page table of the same type
2423 * as the current vcpu paging mode. This is nearly always true
2424 * (might be false while changing modes). Note it is verified later
2428 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2429 if ((bytes == 4) && (gpa % 4 == 0)) {
2430 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2433 memcpy((void *)&gpte + (gpa % 8), new, 4);
2434 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2435 memcpy((void *)&gpte, new, 8);
2438 if ((bytes == 4) && (gpa % 4 == 0))
2439 memcpy((void *)&gpte, new, 4);
2441 if (!is_present_pte(gpte))
2443 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2445 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2446 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2447 vcpu->arch.update_pte.largepage = 1;
2449 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2451 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2453 if (is_error_pfn(pfn)) {
2454 kvm_release_pfn_clean(pfn);
2457 vcpu->arch.update_pte.gfn = gfn;
2458 vcpu->arch.update_pte.pfn = pfn;
2461 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2463 u64 *spte = vcpu->arch.last_pte_updated;
2466 && vcpu->arch.last_pte_gfn == gfn
2467 && shadow_accessed_mask
2468 && !(*spte & shadow_accessed_mask)
2469 && is_shadow_present_pte(*spte))
2470 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2473 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2474 const u8 *new, int bytes,
2475 bool guest_initiated)
2477 gfn_t gfn = gpa >> PAGE_SHIFT;
2478 struct kvm_mmu_page *sp;
2479 struct hlist_node *node, *n;
2480 struct hlist_head *bucket;
2484 unsigned offset = offset_in_page(gpa);
2486 unsigned page_offset;
2487 unsigned misaligned;
2494 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2495 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
2496 spin_lock(&vcpu->kvm->mmu_lock);
2497 kvm_mmu_access_page(vcpu, gfn);
2498 kvm_mmu_free_some_pages(vcpu);
2499 ++vcpu->kvm->stat.mmu_pte_write;
2500 kvm_mmu_audit(vcpu, "pre pte write");
2501 if (guest_initiated) {
2502 if (gfn == vcpu->arch.last_pt_write_gfn
2503 && !last_updated_pte_accessed(vcpu)) {
2504 ++vcpu->arch.last_pt_write_count;
2505 if (vcpu->arch.last_pt_write_count >= 3)
2508 vcpu->arch.last_pt_write_gfn = gfn;
2509 vcpu->arch.last_pt_write_count = 1;
2510 vcpu->arch.last_pte_updated = NULL;
2513 index = kvm_page_table_hashfn(gfn);
2514 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2515 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2516 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2518 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
2519 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2520 misaligned |= bytes < 4;
2521 if (misaligned || flooded) {
2523 * Misaligned accesses are too much trouble to fix
2524 * up; also, they usually indicate a page is not used
2527 * If we're seeing too many writes to a page,
2528 * it may no longer be a page table, or we may be
2529 * forking, in which case it is better to unmap the
2532 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2533 gpa, bytes, sp->role.word);
2534 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2536 ++vcpu->kvm->stat.mmu_flooded;
2539 page_offset = offset;
2540 level = sp->role.level;
2542 if (sp->role.glevels == PT32_ROOT_LEVEL) {
2543 page_offset <<= 1; /* 32->64 */
2545 * A 32-bit pde maps 4MB while the shadow pdes map
2546 * only 2MB. So we need to double the offset again
2547 * and zap two pdes instead of one.
2549 if (level == PT32_ROOT_LEVEL) {
2550 page_offset &= ~7; /* kill rounding error */
2554 quadrant = page_offset >> PAGE_SHIFT;
2555 page_offset &= ~PAGE_MASK;
2556 if (quadrant != sp->role.quadrant)
2559 spte = &sp->spt[page_offset / sizeof(*spte)];
2560 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2562 r = kvm_read_guest_atomic(vcpu->kvm,
2563 gpa & ~(u64)(pte_size - 1),
2565 new = (const void *)&gentry;
2571 mmu_pte_write_zap_pte(vcpu, sp, spte);
2573 mmu_pte_write_new_pte(vcpu, sp, spte, new);
2574 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2578 kvm_mmu_audit(vcpu, "post pte write");
2579 spin_unlock(&vcpu->kvm->mmu_lock);
2580 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2581 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2582 vcpu->arch.update_pte.pfn = bad_pfn;
2586 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2591 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
2593 spin_lock(&vcpu->kvm->mmu_lock);
2594 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2595 spin_unlock(&vcpu->kvm->mmu_lock);
2598 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2600 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2602 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
2603 struct kvm_mmu_page *sp;
2605 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2606 struct kvm_mmu_page, link);
2607 kvm_mmu_zap_page(vcpu->kvm, sp);
2608 ++vcpu->kvm->stat.mmu_recycled;
2612 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2615 enum emulation_result er;
2617 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2626 r = mmu_topup_memory_caches(vcpu);
2630 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2635 case EMULATE_DO_MMIO:
2636 ++vcpu->stat.mmio_exits;
2639 kvm_report_emulation_failure(vcpu, "pagetable");
2647 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2649 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2651 vcpu->arch.mmu.invlpg(vcpu, gva);
2652 kvm_mmu_flush_tlb(vcpu);
2653 ++vcpu->stat.invlpg;
2655 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2657 void kvm_enable_tdp(void)
2661 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2663 void kvm_disable_tdp(void)
2665 tdp_enabled = false;
2667 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2669 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2671 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2674 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2681 if (vcpu->kvm->arch.n_requested_mmu_pages)
2682 vcpu->kvm->arch.n_free_mmu_pages =
2683 vcpu->kvm->arch.n_requested_mmu_pages;
2685 vcpu->kvm->arch.n_free_mmu_pages =
2686 vcpu->kvm->arch.n_alloc_mmu_pages;
2688 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2689 * Therefore we need to allocate shadow page tables in the first
2690 * 4GB of memory, which happens to fit the DMA32 zone.
2692 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2695 vcpu->arch.mmu.pae_root = page_address(page);
2696 for (i = 0; i < 4; ++i)
2697 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2702 free_mmu_pages(vcpu);
2706 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2709 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2711 return alloc_mmu_pages(vcpu);
2714 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2717 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2719 return init_kvm_mmu(vcpu);
2722 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2726 destroy_kvm_mmu(vcpu);
2727 free_mmu_pages(vcpu);
2728 mmu_free_memory_caches(vcpu);
2731 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2733 struct kvm_mmu_page *sp;
2735 spin_lock(&kvm->mmu_lock);
2736 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2740 if (!test_bit(slot, sp->slot_bitmap))
2744 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2746 if (pt[i] & PT_WRITABLE_MASK)
2747 pt[i] &= ~PT_WRITABLE_MASK;
2749 kvm_flush_remote_tlbs(kvm);
2750 spin_unlock(&kvm->mmu_lock);
2753 void kvm_mmu_zap_all(struct kvm *kvm)
2755 struct kvm_mmu_page *sp, *node;
2757 spin_lock(&kvm->mmu_lock);
2758 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2759 if (kvm_mmu_zap_page(kvm, sp))
2760 node = container_of(kvm->arch.active_mmu_pages.next,
2761 struct kvm_mmu_page, link);
2762 spin_unlock(&kvm->mmu_lock);
2764 kvm_flush_remote_tlbs(kvm);
2767 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2769 struct kvm_mmu_page *page;
2771 page = container_of(kvm->arch.active_mmu_pages.prev,
2772 struct kvm_mmu_page, link);
2773 kvm_mmu_zap_page(kvm, page);
2776 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2779 struct kvm *kvm_freed = NULL;
2780 int cache_count = 0;
2782 spin_lock(&kvm_lock);
2784 list_for_each_entry(kvm, &vm_list, vm_list) {
2787 if (!down_read_trylock(&kvm->slots_lock))
2789 spin_lock(&kvm->mmu_lock);
2790 npages = kvm->arch.n_alloc_mmu_pages -
2791 kvm->arch.n_free_mmu_pages;
2792 cache_count += npages;
2793 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2794 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2800 spin_unlock(&kvm->mmu_lock);
2801 up_read(&kvm->slots_lock);
2804 list_move_tail(&kvm_freed->vm_list, &vm_list);
2806 spin_unlock(&kvm_lock);
2811 static struct shrinker mmu_shrinker = {
2812 .shrink = mmu_shrink,
2813 .seeks = DEFAULT_SEEKS * 10,
2816 static void mmu_destroy_caches(void)
2818 if (pte_chain_cache)
2819 kmem_cache_destroy(pte_chain_cache);
2820 if (rmap_desc_cache)
2821 kmem_cache_destroy(rmap_desc_cache);
2822 if (mmu_page_header_cache)
2823 kmem_cache_destroy(mmu_page_header_cache);
2826 void kvm_mmu_module_exit(void)
2828 mmu_destroy_caches();
2829 unregister_shrinker(&mmu_shrinker);
2832 int kvm_mmu_module_init(void)
2834 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2835 sizeof(struct kvm_pte_chain),
2837 if (!pte_chain_cache)
2839 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2840 sizeof(struct kvm_rmap_desc),
2842 if (!rmap_desc_cache)
2845 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2846 sizeof(struct kvm_mmu_page),
2848 if (!mmu_page_header_cache)
2851 register_shrinker(&mmu_shrinker);
2856 mmu_destroy_caches();
2861 * Caculate mmu pages needed for kvm.
2863 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2866 unsigned int nr_mmu_pages;
2867 unsigned int nr_pages = 0;
2869 for (i = 0; i < kvm->nmemslots; i++)
2870 nr_pages += kvm->memslots[i].npages;
2872 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2873 nr_mmu_pages = max(nr_mmu_pages,
2874 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2876 return nr_mmu_pages;
2879 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2882 if (len > buffer->len)
2887 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2892 ret = pv_mmu_peek_buffer(buffer, len);
2897 buffer->processed += len;
2901 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2902 gpa_t addr, gpa_t value)
2907 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2910 r = mmu_topup_memory_caches(vcpu);
2914 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2920 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2922 kvm_set_cr3(vcpu, vcpu->arch.cr3);
2926 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2928 spin_lock(&vcpu->kvm->mmu_lock);
2929 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2930 spin_unlock(&vcpu->kvm->mmu_lock);
2934 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2935 struct kvm_pv_mmu_op_buffer *buffer)
2937 struct kvm_mmu_op_header *header;
2939 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2942 switch (header->op) {
2943 case KVM_MMU_OP_WRITE_PTE: {
2944 struct kvm_mmu_op_write_pte *wpte;
2946 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2949 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2952 case KVM_MMU_OP_FLUSH_TLB: {
2953 struct kvm_mmu_op_flush_tlb *ftlb;
2955 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2958 return kvm_pv_mmu_flush_tlb(vcpu);
2960 case KVM_MMU_OP_RELEASE_PT: {
2961 struct kvm_mmu_op_release_pt *rpt;
2963 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2966 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2972 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2973 gpa_t addr, unsigned long *ret)
2976 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2978 buffer->ptr = buffer->buf;
2979 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2980 buffer->processed = 0;
2982 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2986 while (buffer->len) {
2987 r = kvm_pv_mmu_op_one(vcpu, buffer);
2996 *ret = buffer->processed;
3002 static const char *audit_msg;
3004 static gva_t canonicalize(gva_t gva)
3006 #ifdef CONFIG_X86_64
3007 gva = (long long)(gva << 16) >> 16;
3012 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3013 gva_t va, int level)
3015 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3017 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3019 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3022 if (ent == shadow_trap_nonpresent_pte)
3025 va = canonicalize(va);
3027 if (ent == shadow_notrap_nonpresent_pte)
3028 printk(KERN_ERR "audit: (%s) nontrapping pte"
3029 " in nonleaf level: levels %d gva %lx"
3030 " level %d pte %llx\n", audit_msg,
3031 vcpu->arch.mmu.root_level, va, level, ent);
3033 audit_mappings_page(vcpu, ent, va, level - 1);
3035 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
3036 gfn_t gfn = gpa >> PAGE_SHIFT;
3037 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3038 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
3040 if (is_shadow_present_pte(ent)
3041 && (ent & PT64_BASE_ADDR_MASK) != hpa)
3042 printk(KERN_ERR "xx audit error: (%s) levels %d"
3043 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3044 audit_msg, vcpu->arch.mmu.root_level,
3046 is_shadow_present_pte(ent));
3047 else if (ent == shadow_notrap_nonpresent_pte
3048 && !is_error_hpa(hpa))
3049 printk(KERN_ERR "audit: (%s) notrap shadow,"
3050 " valid guest gva %lx\n", audit_msg, va);
3051 kvm_release_pfn_clean(pfn);
3057 static void audit_mappings(struct kvm_vcpu *vcpu)
3061 if (vcpu->arch.mmu.root_level == 4)
3062 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3064 for (i = 0; i < 4; ++i)
3065 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3066 audit_mappings_page(vcpu,
3067 vcpu->arch.mmu.pae_root[i],
3072 static int count_rmaps(struct kvm_vcpu *vcpu)
3077 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3078 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3079 struct kvm_rmap_desc *d;
3081 for (j = 0; j < m->npages; ++j) {
3082 unsigned long *rmapp = &m->rmap[j];
3086 if (!(*rmapp & 1)) {
3090 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3092 for (k = 0; k < RMAP_EXT; ++k)
3093 if (d->shadow_ptes[k])
3104 static int count_writable_mappings(struct kvm_vcpu *vcpu)
3107 struct kvm_mmu_page *sp;
3110 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3113 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3116 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3119 if (!(ent & PT_PRESENT_MASK))
3121 if (!(ent & PT_WRITABLE_MASK))
3129 static void audit_rmap(struct kvm_vcpu *vcpu)
3131 int n_rmap = count_rmaps(vcpu);
3132 int n_actual = count_writable_mappings(vcpu);
3134 if (n_rmap != n_actual)
3135 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
3136 __func__, audit_msg, n_rmap, n_actual);
3139 static void audit_write_protection(struct kvm_vcpu *vcpu)
3141 struct kvm_mmu_page *sp;
3142 struct kvm_memory_slot *slot;
3143 unsigned long *rmapp;
3146 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3147 if (sp->role.direct)
3150 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3151 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3152 rmapp = &slot->rmap[gfn - slot->base_gfn];
3154 printk(KERN_ERR "%s: (%s) shadow page has writable"
3155 " mappings: gfn %lx role %x\n",
3156 __func__, audit_msg, sp->gfn,
3161 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3168 audit_write_protection(vcpu);
3169 audit_mappings(vcpu);