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 CREATE_TRACE_POINTS
144 #include "mmutrace.h"
146 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
148 struct kvm_rmap_desc {
149 u64 *sptes[RMAP_EXT];
150 struct kvm_rmap_desc *more;
153 struct kvm_shadow_walk_iterator {
161 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
162 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
163 shadow_walk_okay(&(_walker)); \
164 shadow_walk_next(&(_walker)))
167 struct kvm_unsync_walk {
168 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
171 typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
173 static struct kmem_cache *pte_chain_cache;
174 static struct kmem_cache *rmap_desc_cache;
175 static struct kmem_cache *mmu_page_header_cache;
177 static u64 __read_mostly shadow_trap_nonpresent_pte;
178 static u64 __read_mostly shadow_notrap_nonpresent_pte;
179 static u64 __read_mostly shadow_base_present_pte;
180 static u64 __read_mostly shadow_nx_mask;
181 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
182 static u64 __read_mostly shadow_user_mask;
183 static u64 __read_mostly shadow_accessed_mask;
184 static u64 __read_mostly shadow_dirty_mask;
186 static inline u64 rsvd_bits(int s, int e)
188 return ((1ULL << (e - s + 1)) - 1) << s;
191 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
193 shadow_trap_nonpresent_pte = trap_pte;
194 shadow_notrap_nonpresent_pte = notrap_pte;
196 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
198 void kvm_mmu_set_base_ptes(u64 base_pte)
200 shadow_base_present_pte = base_pte;
202 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
204 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
205 u64 dirty_mask, u64 nx_mask, u64 x_mask)
207 shadow_user_mask = user_mask;
208 shadow_accessed_mask = accessed_mask;
209 shadow_dirty_mask = dirty_mask;
210 shadow_nx_mask = nx_mask;
211 shadow_x_mask = x_mask;
213 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
215 static int is_write_protection(struct kvm_vcpu *vcpu)
217 return vcpu->arch.cr0 & X86_CR0_WP;
220 static int is_cpuid_PSE36(void)
225 static int is_nx(struct kvm_vcpu *vcpu)
227 return vcpu->arch.shadow_efer & EFER_NX;
230 static int is_shadow_present_pte(u64 pte)
232 return pte != shadow_trap_nonpresent_pte
233 && pte != shadow_notrap_nonpresent_pte;
236 static int is_large_pte(u64 pte)
238 return pte & PT_PAGE_SIZE_MASK;
241 static int is_writeble_pte(unsigned long pte)
243 return pte & PT_WRITABLE_MASK;
246 static int is_dirty_gpte(unsigned long pte)
248 return pte & PT_DIRTY_MASK;
251 static int is_rmap_spte(u64 pte)
253 return is_shadow_present_pte(pte);
256 static int is_last_spte(u64 pte, int level)
258 if (level == PT_PAGE_TABLE_LEVEL)
260 if (level == PT_DIRECTORY_LEVEL && is_large_pte(pte))
265 static pfn_t spte_to_pfn(u64 pte)
267 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
270 static gfn_t pse36_gfn_delta(u32 gpte)
272 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
274 return (gpte & PT32_DIR_PSE36_MASK) << shift;
277 static void __set_spte(u64 *sptep, u64 spte)
280 set_64bit((unsigned long *)sptep, spte);
282 set_64bit((unsigned long long *)sptep, spte);
286 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
287 struct kmem_cache *base_cache, int min)
291 if (cache->nobjs >= min)
293 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
294 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
297 cache->objects[cache->nobjs++] = obj;
302 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
305 kfree(mc->objects[--mc->nobjs]);
308 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
313 if (cache->nobjs >= min)
315 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
316 page = alloc_page(GFP_KERNEL);
319 set_page_private(page, 0);
320 cache->objects[cache->nobjs++] = page_address(page);
325 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
328 free_page((unsigned long)mc->objects[--mc->nobjs]);
331 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
335 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
339 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
343 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
346 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
347 mmu_page_header_cache, 4);
352 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
354 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
355 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
356 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
357 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
360 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
366 p = mc->objects[--mc->nobjs];
370 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
372 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
373 sizeof(struct kvm_pte_chain));
376 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
381 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
383 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
384 sizeof(struct kvm_rmap_desc));
387 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
393 * Return the pointer to the largepage write count for a given
394 * gfn, handling slots that are not large page aligned.
396 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
400 idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
401 (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
402 return &slot->lpage_info[0][idx].write_count;
405 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
409 gfn = unalias_gfn(kvm, gfn);
410 write_count = slot_largepage_idx(gfn,
411 gfn_to_memslot_unaliased(kvm, gfn));
415 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
419 gfn = unalias_gfn(kvm, gfn);
420 write_count = slot_largepage_idx(gfn,
421 gfn_to_memslot_unaliased(kvm, gfn));
423 WARN_ON(*write_count < 0);
426 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
428 struct kvm_memory_slot *slot;
431 gfn = unalias_gfn(kvm, gfn);
432 slot = gfn_to_memslot_unaliased(kvm, gfn);
434 largepage_idx = slot_largepage_idx(gfn, slot);
435 return *largepage_idx;
441 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
443 struct vm_area_struct *vma;
447 addr = gfn_to_hva(kvm, gfn);
448 if (kvm_is_error_hva(addr))
451 down_read(¤t->mm->mmap_sem);
452 vma = find_vma(current->mm, addr);
453 if (vma && is_vm_hugetlb_page(vma))
455 up_read(¤t->mm->mmap_sem);
460 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
462 struct kvm_memory_slot *slot;
464 if (has_wrprotected_page(vcpu->kvm, large_gfn))
467 if (!host_largepage_backed(vcpu->kvm, large_gfn))
470 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
471 if (slot && slot->dirty_bitmap)
478 * Take gfn and return the reverse mapping to it.
479 * Note: gfn must be unaliased before this function get called
482 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
484 struct kvm_memory_slot *slot;
487 slot = gfn_to_memslot(kvm, gfn);
489 return &slot->rmap[gfn - slot->base_gfn];
491 idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
492 (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
494 return &slot->lpage_info[0][idx].rmap_pde;
498 * Reverse mapping data structures:
500 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
501 * that points to page_address(page).
503 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
504 * containing more mappings.
506 * Returns the number of rmap entries before the spte was added or zero if
507 * the spte was not added.
510 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
512 struct kvm_mmu_page *sp;
513 struct kvm_rmap_desc *desc;
514 unsigned long *rmapp;
517 if (!is_rmap_spte(*spte))
519 gfn = unalias_gfn(vcpu->kvm, gfn);
520 sp = page_header(__pa(spte));
521 sp->gfns[spte - sp->spt] = gfn;
522 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
524 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
525 *rmapp = (unsigned long)spte;
526 } else if (!(*rmapp & 1)) {
527 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
528 desc = mmu_alloc_rmap_desc(vcpu);
529 desc->sptes[0] = (u64 *)*rmapp;
530 desc->sptes[1] = spte;
531 *rmapp = (unsigned long)desc | 1;
533 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
534 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
535 while (desc->sptes[RMAP_EXT-1] && desc->more) {
539 if (desc->sptes[RMAP_EXT-1]) {
540 desc->more = mmu_alloc_rmap_desc(vcpu);
543 for (i = 0; desc->sptes[i]; ++i)
545 desc->sptes[i] = spte;
550 static void rmap_desc_remove_entry(unsigned long *rmapp,
551 struct kvm_rmap_desc *desc,
553 struct kvm_rmap_desc *prev_desc)
557 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
559 desc->sptes[i] = desc->sptes[j];
560 desc->sptes[j] = NULL;
563 if (!prev_desc && !desc->more)
564 *rmapp = (unsigned long)desc->sptes[0];
567 prev_desc->more = desc->more;
569 *rmapp = (unsigned long)desc->more | 1;
570 mmu_free_rmap_desc(desc);
573 static void rmap_remove(struct kvm *kvm, u64 *spte)
575 struct kvm_rmap_desc *desc;
576 struct kvm_rmap_desc *prev_desc;
577 struct kvm_mmu_page *sp;
579 unsigned long *rmapp;
582 if (!is_rmap_spte(*spte))
584 sp = page_header(__pa(spte));
585 pfn = spte_to_pfn(*spte);
586 if (*spte & shadow_accessed_mask)
587 kvm_set_pfn_accessed(pfn);
588 if (is_writeble_pte(*spte))
589 kvm_release_pfn_dirty(pfn);
591 kvm_release_pfn_clean(pfn);
592 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
594 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
596 } else if (!(*rmapp & 1)) {
597 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
598 if ((u64 *)*rmapp != spte) {
599 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
605 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
606 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
609 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
610 if (desc->sptes[i] == spte) {
611 rmap_desc_remove_entry(rmapp,
623 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
625 struct kvm_rmap_desc *desc;
626 struct kvm_rmap_desc *prev_desc;
632 else if (!(*rmapp & 1)) {
634 return (u64 *)*rmapp;
637 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
641 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
642 if (prev_spte == spte)
643 return desc->sptes[i];
644 prev_spte = desc->sptes[i];
651 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
653 unsigned long *rmapp;
655 int write_protected = 0;
657 gfn = unalias_gfn(kvm, gfn);
658 rmapp = gfn_to_rmap(kvm, gfn, 0);
660 spte = rmap_next(kvm, rmapp, NULL);
663 BUG_ON(!(*spte & PT_PRESENT_MASK));
664 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
665 if (is_writeble_pte(*spte)) {
666 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
669 spte = rmap_next(kvm, rmapp, spte);
671 if (write_protected) {
674 spte = rmap_next(kvm, rmapp, NULL);
675 pfn = spte_to_pfn(*spte);
676 kvm_set_pfn_dirty(pfn);
679 /* check for huge page mappings */
680 rmapp = gfn_to_rmap(kvm, gfn, 1);
681 spte = rmap_next(kvm, rmapp, NULL);
684 BUG_ON(!(*spte & PT_PRESENT_MASK));
685 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
686 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
687 if (is_writeble_pte(*spte)) {
688 rmap_remove(kvm, spte);
690 __set_spte(spte, shadow_trap_nonpresent_pte);
694 spte = rmap_next(kvm, rmapp, spte);
697 return write_protected;
700 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
703 int need_tlb_flush = 0;
705 while ((spte = rmap_next(kvm, rmapp, NULL))) {
706 BUG_ON(!(*spte & PT_PRESENT_MASK));
707 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
708 rmap_remove(kvm, spte);
709 __set_spte(spte, shadow_trap_nonpresent_pte);
712 return need_tlb_flush;
715 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
716 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
722 * If mmap_sem isn't taken, we can look the memslots with only
723 * the mmu_lock by skipping over the slots with userspace_addr == 0.
725 for (i = 0; i < kvm->nmemslots; i++) {
726 struct kvm_memory_slot *memslot = &kvm->memslots[i];
727 unsigned long start = memslot->userspace_addr;
730 /* mmu_lock protects userspace_addr */
734 end = start + (memslot->npages << PAGE_SHIFT);
735 if (hva >= start && hva < end) {
736 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
737 int idx = gfn_offset /
738 KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL);
739 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
740 retval |= handler(kvm,
741 &memslot->lpage_info[0][idx].rmap_pde);
748 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
750 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
753 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
758 /* always return old for EPT */
759 if (!shadow_accessed_mask)
762 spte = rmap_next(kvm, rmapp, NULL);
766 BUG_ON(!(_spte & PT_PRESENT_MASK));
767 _young = _spte & PT_ACCESSED_MASK;
770 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
772 spte = rmap_next(kvm, rmapp, spte);
777 #define RMAP_RECYCLE_THRESHOLD 1000
779 static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
781 unsigned long *rmapp;
783 gfn = unalias_gfn(vcpu->kvm, gfn);
784 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
786 kvm_unmap_rmapp(vcpu->kvm, rmapp);
787 kvm_flush_remote_tlbs(vcpu->kvm);
790 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
792 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
796 static int is_empty_shadow_page(u64 *spt)
801 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
802 if (is_shadow_present_pte(*pos)) {
803 printk(KERN_ERR "%s: %p %llx\n", __func__,
811 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
813 ASSERT(is_empty_shadow_page(sp->spt));
815 __free_page(virt_to_page(sp->spt));
816 __free_page(virt_to_page(sp->gfns));
818 ++kvm->arch.n_free_mmu_pages;
821 static unsigned kvm_page_table_hashfn(gfn_t gfn)
823 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
826 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
829 struct kvm_mmu_page *sp;
831 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
832 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
833 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
834 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
835 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
836 INIT_LIST_HEAD(&sp->oos_link);
837 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
839 sp->parent_pte = parent_pte;
840 --vcpu->kvm->arch.n_free_mmu_pages;
844 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
845 struct kvm_mmu_page *sp, u64 *parent_pte)
847 struct kvm_pte_chain *pte_chain;
848 struct hlist_node *node;
853 if (!sp->multimapped) {
854 u64 *old = sp->parent_pte;
857 sp->parent_pte = parent_pte;
861 pte_chain = mmu_alloc_pte_chain(vcpu);
862 INIT_HLIST_HEAD(&sp->parent_ptes);
863 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
864 pte_chain->parent_ptes[0] = old;
866 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
867 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
869 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
870 if (!pte_chain->parent_ptes[i]) {
871 pte_chain->parent_ptes[i] = parent_pte;
875 pte_chain = mmu_alloc_pte_chain(vcpu);
877 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
878 pte_chain->parent_ptes[0] = parent_pte;
881 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
884 struct kvm_pte_chain *pte_chain;
885 struct hlist_node *node;
888 if (!sp->multimapped) {
889 BUG_ON(sp->parent_pte != parent_pte);
890 sp->parent_pte = NULL;
893 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
894 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
895 if (!pte_chain->parent_ptes[i])
897 if (pte_chain->parent_ptes[i] != parent_pte)
899 while (i + 1 < NR_PTE_CHAIN_ENTRIES
900 && pte_chain->parent_ptes[i + 1]) {
901 pte_chain->parent_ptes[i]
902 = pte_chain->parent_ptes[i + 1];
905 pte_chain->parent_ptes[i] = NULL;
907 hlist_del(&pte_chain->link);
908 mmu_free_pte_chain(pte_chain);
909 if (hlist_empty(&sp->parent_ptes)) {
911 sp->parent_pte = NULL;
920 static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
921 mmu_parent_walk_fn fn)
923 struct kvm_pte_chain *pte_chain;
924 struct hlist_node *node;
925 struct kvm_mmu_page *parent_sp;
928 if (!sp->multimapped && sp->parent_pte) {
929 parent_sp = page_header(__pa(sp->parent_pte));
931 mmu_parent_walk(vcpu, parent_sp, fn);
934 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
935 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
936 if (!pte_chain->parent_ptes[i])
938 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
940 mmu_parent_walk(vcpu, parent_sp, fn);
944 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
947 struct kvm_mmu_page *sp = page_header(__pa(spte));
949 index = spte - sp->spt;
950 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
951 sp->unsync_children++;
952 WARN_ON(!sp->unsync_children);
955 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
957 struct kvm_pte_chain *pte_chain;
958 struct hlist_node *node;
964 if (!sp->multimapped) {
965 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
969 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
970 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
971 if (!pte_chain->parent_ptes[i])
973 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
977 static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
979 kvm_mmu_update_parents_unsync(sp);
983 static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
984 struct kvm_mmu_page *sp)
986 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
987 kvm_mmu_update_parents_unsync(sp);
990 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
991 struct kvm_mmu_page *sp)
995 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
996 sp->spt[i] = shadow_trap_nonpresent_pte;
999 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1000 struct kvm_mmu_page *sp)
1005 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1009 #define KVM_PAGE_ARRAY_NR 16
1011 struct kvm_mmu_pages {
1012 struct mmu_page_and_offset {
1013 struct kvm_mmu_page *sp;
1015 } page[KVM_PAGE_ARRAY_NR];
1019 #define for_each_unsync_children(bitmap, idx) \
1020 for (idx = find_first_bit(bitmap, 512); \
1022 idx = find_next_bit(bitmap, 512, idx+1))
1024 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1030 for (i=0; i < pvec->nr; i++)
1031 if (pvec->page[i].sp == sp)
1034 pvec->page[pvec->nr].sp = sp;
1035 pvec->page[pvec->nr].idx = idx;
1037 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1040 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1041 struct kvm_mmu_pages *pvec)
1043 int i, ret, nr_unsync_leaf = 0;
1045 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1046 u64 ent = sp->spt[i];
1048 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1049 struct kvm_mmu_page *child;
1050 child = page_header(ent & PT64_BASE_ADDR_MASK);
1052 if (child->unsync_children) {
1053 if (mmu_pages_add(pvec, child, i))
1056 ret = __mmu_unsync_walk(child, pvec);
1058 __clear_bit(i, sp->unsync_child_bitmap);
1060 nr_unsync_leaf += ret;
1065 if (child->unsync) {
1067 if (mmu_pages_add(pvec, child, i))
1073 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1074 sp->unsync_children = 0;
1076 return nr_unsync_leaf;
1079 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1080 struct kvm_mmu_pages *pvec)
1082 if (!sp->unsync_children)
1085 mmu_pages_add(pvec, sp, 0);
1086 return __mmu_unsync_walk(sp, pvec);
1089 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1092 struct hlist_head *bucket;
1093 struct kvm_mmu_page *sp;
1094 struct hlist_node *node;
1096 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1097 index = kvm_page_table_hashfn(gfn);
1098 bucket = &kvm->arch.mmu_page_hash[index];
1099 hlist_for_each_entry(sp, node, bucket, hash_link)
1100 if (sp->gfn == gfn && !sp->role.direct
1101 && !sp->role.invalid) {
1102 pgprintk("%s: found role %x\n",
1103 __func__, sp->role.word);
1109 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1111 WARN_ON(!sp->unsync);
1113 --kvm->stat.mmu_unsync;
1116 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1118 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1120 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1121 kvm_mmu_zap_page(vcpu->kvm, sp);
1125 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1126 kvm_flush_remote_tlbs(vcpu->kvm);
1127 kvm_unlink_unsync_page(vcpu->kvm, sp);
1128 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1129 kvm_mmu_zap_page(vcpu->kvm, sp);
1133 kvm_mmu_flush_tlb(vcpu);
1137 struct mmu_page_path {
1138 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1139 unsigned int idx[PT64_ROOT_LEVEL-1];
1142 #define for_each_sp(pvec, sp, parents, i) \
1143 for (i = mmu_pages_next(&pvec, &parents, -1), \
1144 sp = pvec.page[i].sp; \
1145 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1146 i = mmu_pages_next(&pvec, &parents, i))
1148 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1149 struct mmu_page_path *parents,
1154 for (n = i+1; n < pvec->nr; n++) {
1155 struct kvm_mmu_page *sp = pvec->page[n].sp;
1157 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1158 parents->idx[0] = pvec->page[n].idx;
1162 parents->parent[sp->role.level-2] = sp;
1163 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1169 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1171 struct kvm_mmu_page *sp;
1172 unsigned int level = 0;
1175 unsigned int idx = parents->idx[level];
1177 sp = parents->parent[level];
1181 --sp->unsync_children;
1182 WARN_ON((int)sp->unsync_children < 0);
1183 __clear_bit(idx, sp->unsync_child_bitmap);
1185 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1188 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1189 struct mmu_page_path *parents,
1190 struct kvm_mmu_pages *pvec)
1192 parents->parent[parent->role.level-1] = NULL;
1196 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1197 struct kvm_mmu_page *parent)
1200 struct kvm_mmu_page *sp;
1201 struct mmu_page_path parents;
1202 struct kvm_mmu_pages pages;
1204 kvm_mmu_pages_init(parent, &parents, &pages);
1205 while (mmu_unsync_walk(parent, &pages)) {
1208 for_each_sp(pages, sp, parents, i)
1209 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1212 kvm_flush_remote_tlbs(vcpu->kvm);
1214 for_each_sp(pages, sp, parents, i) {
1215 kvm_sync_page(vcpu, sp);
1216 mmu_pages_clear_parents(&parents);
1218 cond_resched_lock(&vcpu->kvm->mmu_lock);
1219 kvm_mmu_pages_init(parent, &parents, &pages);
1223 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1231 union kvm_mmu_page_role role;
1234 struct hlist_head *bucket;
1235 struct kvm_mmu_page *sp;
1236 struct hlist_node *node, *tmp;
1238 role = vcpu->arch.mmu.base_role;
1240 role.direct = direct;
1241 role.access = access;
1242 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1243 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1244 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1245 role.quadrant = quadrant;
1247 pgprintk("%s: looking gfn %lx role %x\n", __func__,
1249 index = kvm_page_table_hashfn(gfn);
1250 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1251 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1252 if (sp->gfn == gfn) {
1254 if (kvm_sync_page(vcpu, sp))
1257 if (sp->role.word != role.word)
1260 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1261 if (sp->unsync_children) {
1262 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1263 kvm_mmu_mark_parents_unsync(vcpu, sp);
1265 pgprintk("%s: found\n", __func__);
1268 ++vcpu->kvm->stat.mmu_cache_miss;
1269 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1272 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
1275 hlist_add_head(&sp->hash_link, bucket);
1277 if (rmap_write_protect(vcpu->kvm, gfn))
1278 kvm_flush_remote_tlbs(vcpu->kvm);
1279 account_shadowed(vcpu->kvm, gfn);
1281 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1282 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1284 nonpaging_prefetch_page(vcpu, sp);
1288 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1289 struct kvm_vcpu *vcpu, u64 addr)
1291 iterator->addr = addr;
1292 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1293 iterator->level = vcpu->arch.mmu.shadow_root_level;
1294 if (iterator->level == PT32E_ROOT_LEVEL) {
1295 iterator->shadow_addr
1296 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1297 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1299 if (!iterator->shadow_addr)
1300 iterator->level = 0;
1304 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1306 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1309 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1310 if (is_large_pte(*iterator->sptep))
1313 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1314 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1318 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1320 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1324 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1325 struct kvm_mmu_page *sp)
1333 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1336 if (is_shadow_present_pte(ent)) {
1337 if (!is_last_spte(ent, sp->role.level)) {
1338 ent &= PT64_BASE_ADDR_MASK;
1339 mmu_page_remove_parent_pte(page_header(ent),
1342 if (is_large_pte(ent))
1344 rmap_remove(kvm, &pt[i]);
1347 pt[i] = shadow_trap_nonpresent_pte;
1351 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1353 mmu_page_remove_parent_pte(sp, parent_pte);
1356 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1359 struct kvm_vcpu *vcpu;
1361 kvm_for_each_vcpu(i, vcpu, kvm)
1362 vcpu->arch.last_pte_updated = NULL;
1365 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1369 while (sp->multimapped || sp->parent_pte) {
1370 if (!sp->multimapped)
1371 parent_pte = sp->parent_pte;
1373 struct kvm_pte_chain *chain;
1375 chain = container_of(sp->parent_ptes.first,
1376 struct kvm_pte_chain, link);
1377 parent_pte = chain->parent_ptes[0];
1379 BUG_ON(!parent_pte);
1380 kvm_mmu_put_page(sp, parent_pte);
1381 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1385 static int mmu_zap_unsync_children(struct kvm *kvm,
1386 struct kvm_mmu_page *parent)
1389 struct mmu_page_path parents;
1390 struct kvm_mmu_pages pages;
1392 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1395 kvm_mmu_pages_init(parent, &parents, &pages);
1396 while (mmu_unsync_walk(parent, &pages)) {
1397 struct kvm_mmu_page *sp;
1399 for_each_sp(pages, sp, parents, i) {
1400 kvm_mmu_zap_page(kvm, sp);
1401 mmu_pages_clear_parents(&parents);
1404 kvm_mmu_pages_init(parent, &parents, &pages);
1410 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1413 ++kvm->stat.mmu_shadow_zapped;
1414 ret = mmu_zap_unsync_children(kvm, sp);
1415 kvm_mmu_page_unlink_children(kvm, sp);
1416 kvm_mmu_unlink_parents(kvm, sp);
1417 kvm_flush_remote_tlbs(kvm);
1418 if (!sp->role.invalid && !sp->role.direct)
1419 unaccount_shadowed(kvm, sp->gfn);
1421 kvm_unlink_unsync_page(kvm, sp);
1422 if (!sp->root_count) {
1423 hlist_del(&sp->hash_link);
1424 kvm_mmu_free_page(kvm, sp);
1426 sp->role.invalid = 1;
1427 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1428 kvm_reload_remote_mmus(kvm);
1430 kvm_mmu_reset_last_pte_updated(kvm);
1435 * Changing the number of mmu pages allocated to the vm
1436 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1438 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1442 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1443 used_pages = max(0, used_pages);
1446 * If we set the number of mmu pages to be smaller be than the
1447 * number of actived pages , we must to free some mmu pages before we
1451 if (used_pages > kvm_nr_mmu_pages) {
1452 while (used_pages > kvm_nr_mmu_pages) {
1453 struct kvm_mmu_page *page;
1455 page = container_of(kvm->arch.active_mmu_pages.prev,
1456 struct kvm_mmu_page, link);
1457 kvm_mmu_zap_page(kvm, page);
1460 kvm->arch.n_free_mmu_pages = 0;
1463 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1464 - kvm->arch.n_alloc_mmu_pages;
1466 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1469 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1472 struct hlist_head *bucket;
1473 struct kvm_mmu_page *sp;
1474 struct hlist_node *node, *n;
1477 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1479 index = kvm_page_table_hashfn(gfn);
1480 bucket = &kvm->arch.mmu_page_hash[index];
1481 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1482 if (sp->gfn == gfn && !sp->role.direct) {
1483 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1486 if (kvm_mmu_zap_page(kvm, sp))
1492 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1495 struct hlist_head *bucket;
1496 struct kvm_mmu_page *sp;
1497 struct hlist_node *node, *nn;
1499 index = kvm_page_table_hashfn(gfn);
1500 bucket = &kvm->arch.mmu_page_hash[index];
1501 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1502 if (sp->gfn == gfn && !sp->role.direct
1503 && !sp->role.invalid) {
1504 pgprintk("%s: zap %lx %x\n",
1505 __func__, gfn, sp->role.word);
1506 kvm_mmu_zap_page(kvm, sp);
1511 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1513 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1514 struct kvm_mmu_page *sp = page_header(__pa(pte));
1516 __set_bit(slot, sp->slot_bitmap);
1519 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1524 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1527 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1528 if (pt[i] == shadow_notrap_nonpresent_pte)
1529 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
1533 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1537 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1539 if (gpa == UNMAPPED_GVA)
1542 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1548 * The function is based on mtrr_type_lookup() in
1549 * arch/x86/kernel/cpu/mtrr/generic.c
1551 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1556 u8 prev_match, curr_match;
1557 int num_var_ranges = KVM_NR_VAR_MTRR;
1559 if (!mtrr_state->enabled)
1562 /* Make end inclusive end, instead of exclusive */
1565 /* Look in fixed ranges. Just return the type as per start */
1566 if (mtrr_state->have_fixed && (start < 0x100000)) {
1569 if (start < 0x80000) {
1571 idx += (start >> 16);
1572 return mtrr_state->fixed_ranges[idx];
1573 } else if (start < 0xC0000) {
1575 idx += ((start - 0x80000) >> 14);
1576 return mtrr_state->fixed_ranges[idx];
1577 } else if (start < 0x1000000) {
1579 idx += ((start - 0xC0000) >> 12);
1580 return mtrr_state->fixed_ranges[idx];
1585 * Look in variable ranges
1586 * Look of multiple ranges matching this address and pick type
1587 * as per MTRR precedence
1589 if (!(mtrr_state->enabled & 2))
1590 return mtrr_state->def_type;
1593 for (i = 0; i < num_var_ranges; ++i) {
1594 unsigned short start_state, end_state;
1596 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1599 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1600 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1601 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1602 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1604 start_state = ((start & mask) == (base & mask));
1605 end_state = ((end & mask) == (base & mask));
1606 if (start_state != end_state)
1609 if ((start & mask) != (base & mask))
1612 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1613 if (prev_match == 0xFF) {
1614 prev_match = curr_match;
1618 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1619 curr_match == MTRR_TYPE_UNCACHABLE)
1620 return MTRR_TYPE_UNCACHABLE;
1622 if ((prev_match == MTRR_TYPE_WRBACK &&
1623 curr_match == MTRR_TYPE_WRTHROUGH) ||
1624 (prev_match == MTRR_TYPE_WRTHROUGH &&
1625 curr_match == MTRR_TYPE_WRBACK)) {
1626 prev_match = MTRR_TYPE_WRTHROUGH;
1627 curr_match = MTRR_TYPE_WRTHROUGH;
1630 if (prev_match != curr_match)
1631 return MTRR_TYPE_UNCACHABLE;
1634 if (prev_match != 0xFF)
1637 return mtrr_state->def_type;
1640 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1644 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1645 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1646 if (mtrr == 0xfe || mtrr == 0xff)
1647 mtrr = MTRR_TYPE_WRBACK;
1650 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1652 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1655 struct hlist_head *bucket;
1656 struct kvm_mmu_page *s;
1657 struct hlist_node *node, *n;
1659 index = kvm_page_table_hashfn(sp->gfn);
1660 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1661 /* don't unsync if pagetable is shadowed with multiple roles */
1662 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1663 if (s->gfn != sp->gfn || s->role.direct)
1665 if (s->role.word != sp->role.word)
1668 ++vcpu->kvm->stat.mmu_unsync;
1671 kvm_mmu_mark_parents_unsync(vcpu, sp);
1673 mmu_convert_notrap(sp);
1677 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1680 struct kvm_mmu_page *shadow;
1682 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1684 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1688 if (can_unsync && oos_shadow)
1689 return kvm_unsync_page(vcpu, shadow);
1695 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1696 unsigned pte_access, int user_fault,
1697 int write_fault, int dirty, int largepage,
1698 gfn_t gfn, pfn_t pfn, bool speculative,
1705 * We don't set the accessed bit, since we sometimes want to see
1706 * whether the guest actually used the pte (in order to detect
1709 spte = shadow_base_present_pte | shadow_dirty_mask;
1711 spte |= shadow_accessed_mask;
1713 pte_access &= ~ACC_WRITE_MASK;
1714 if (pte_access & ACC_EXEC_MASK)
1715 spte |= shadow_x_mask;
1717 spte |= shadow_nx_mask;
1718 if (pte_access & ACC_USER_MASK)
1719 spte |= shadow_user_mask;
1721 spte |= PT_PAGE_SIZE_MASK;
1723 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1724 kvm_is_mmio_pfn(pfn));
1726 spte |= (u64)pfn << PAGE_SHIFT;
1728 if ((pte_access & ACC_WRITE_MASK)
1729 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1731 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1733 spte = shadow_trap_nonpresent_pte;
1737 spte |= PT_WRITABLE_MASK;
1740 * Optimization: for pte sync, if spte was writable the hash
1741 * lookup is unnecessary (and expensive). Write protection
1742 * is responsibility of mmu_get_page / kvm_sync_page.
1743 * Same reasoning can be applied to dirty page accounting.
1745 if (!can_unsync && is_writeble_pte(*sptep))
1748 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1749 pgprintk("%s: found shadow page for %lx, marking ro\n",
1752 pte_access &= ~ACC_WRITE_MASK;
1753 if (is_writeble_pte(spte))
1754 spte &= ~PT_WRITABLE_MASK;
1758 if (pte_access & ACC_WRITE_MASK)
1759 mark_page_dirty(vcpu->kvm, gfn);
1762 __set_spte(sptep, spte);
1766 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1767 unsigned pt_access, unsigned pte_access,
1768 int user_fault, int write_fault, int dirty,
1769 int *ptwrite, int largepage, gfn_t gfn,
1770 pfn_t pfn, bool speculative)
1772 int was_rmapped = 0;
1773 int was_writeble = is_writeble_pte(*sptep);
1776 pgprintk("%s: spte %llx access %x write_fault %d"
1777 " user_fault %d gfn %lx\n",
1778 __func__, *sptep, pt_access,
1779 write_fault, user_fault, gfn);
1781 if (is_rmap_spte(*sptep)) {
1783 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1784 * the parent of the now unreachable PTE.
1786 if (largepage && !is_large_pte(*sptep)) {
1787 struct kvm_mmu_page *child;
1790 child = page_header(pte & PT64_BASE_ADDR_MASK);
1791 mmu_page_remove_parent_pte(child, sptep);
1792 } else if (pfn != spte_to_pfn(*sptep)) {
1793 pgprintk("hfn old %lx new %lx\n",
1794 spte_to_pfn(*sptep), pfn);
1795 rmap_remove(vcpu->kvm, sptep);
1799 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
1800 dirty, largepage, gfn, pfn, speculative, true)) {
1803 kvm_x86_ops->tlb_flush(vcpu);
1806 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
1807 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1808 is_large_pte(*sptep)? "2MB" : "4kB",
1809 is_present_pte(*sptep)?"RW":"R", gfn,
1810 *shadow_pte, sptep);
1811 if (!was_rmapped && is_large_pte(*sptep))
1812 ++vcpu->kvm->stat.lpages;
1814 page_header_update_slot(vcpu->kvm, sptep, gfn);
1816 rmap_count = rmap_add(vcpu, sptep, gfn, largepage);
1817 if (!is_rmap_spte(*sptep))
1818 kvm_release_pfn_clean(pfn);
1819 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1820 rmap_recycle(vcpu, gfn, largepage);
1823 kvm_release_pfn_dirty(pfn);
1825 kvm_release_pfn_clean(pfn);
1828 vcpu->arch.last_pte_updated = sptep;
1829 vcpu->arch.last_pte_gfn = gfn;
1833 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1837 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1838 int largepage, gfn_t gfn, pfn_t pfn)
1840 struct kvm_shadow_walk_iterator iterator;
1841 struct kvm_mmu_page *sp;
1845 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1846 if (iterator.level == PT_PAGE_TABLE_LEVEL
1847 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1848 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1849 0, write, 1, &pt_write,
1850 largepage, gfn, pfn, false);
1851 ++vcpu->stat.pf_fixed;
1855 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1856 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1857 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1859 1, ACC_ALL, iterator.sptep);
1861 pgprintk("nonpaging_map: ENOMEM\n");
1862 kvm_release_pfn_clean(pfn);
1866 __set_spte(iterator.sptep,
1868 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1869 | shadow_user_mask | shadow_x_mask);
1875 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1880 unsigned long mmu_seq;
1882 if (is_largepage_backed(vcpu, gfn &
1883 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
1884 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
1888 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1890 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1893 if (is_error_pfn(pfn)) {
1894 kvm_release_pfn_clean(pfn);
1898 spin_lock(&vcpu->kvm->mmu_lock);
1899 if (mmu_notifier_retry(vcpu, mmu_seq))
1901 kvm_mmu_free_some_pages(vcpu);
1902 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1903 spin_unlock(&vcpu->kvm->mmu_lock);
1909 spin_unlock(&vcpu->kvm->mmu_lock);
1910 kvm_release_pfn_clean(pfn);
1915 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1918 struct kvm_mmu_page *sp;
1920 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1922 spin_lock(&vcpu->kvm->mmu_lock);
1923 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1924 hpa_t root = vcpu->arch.mmu.root_hpa;
1926 sp = page_header(root);
1928 if (!sp->root_count && sp->role.invalid)
1929 kvm_mmu_zap_page(vcpu->kvm, sp);
1930 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1931 spin_unlock(&vcpu->kvm->mmu_lock);
1934 for (i = 0; i < 4; ++i) {
1935 hpa_t root = vcpu->arch.mmu.pae_root[i];
1938 root &= PT64_BASE_ADDR_MASK;
1939 sp = page_header(root);
1941 if (!sp->root_count && sp->role.invalid)
1942 kvm_mmu_zap_page(vcpu->kvm, sp);
1944 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1946 spin_unlock(&vcpu->kvm->mmu_lock);
1947 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1950 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1954 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1955 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1962 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
1966 struct kvm_mmu_page *sp;
1970 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1972 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1973 hpa_t root = vcpu->arch.mmu.root_hpa;
1975 ASSERT(!VALID_PAGE(root));
1978 if (mmu_check_root(vcpu, root_gfn))
1980 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1981 PT64_ROOT_LEVEL, direct,
1983 root = __pa(sp->spt);
1985 vcpu->arch.mmu.root_hpa = root;
1988 direct = !is_paging(vcpu);
1991 for (i = 0; i < 4; ++i) {
1992 hpa_t root = vcpu->arch.mmu.pae_root[i];
1994 ASSERT(!VALID_PAGE(root));
1995 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1996 pdptr = kvm_pdptr_read(vcpu, i);
1997 if (!is_present_gpte(pdptr)) {
1998 vcpu->arch.mmu.pae_root[i] = 0;
2001 root_gfn = pdptr >> PAGE_SHIFT;
2002 } else if (vcpu->arch.mmu.root_level == 0)
2004 if (mmu_check_root(vcpu, root_gfn))
2006 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
2007 PT32_ROOT_LEVEL, direct,
2009 root = __pa(sp->spt);
2011 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2013 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2017 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2020 struct kvm_mmu_page *sp;
2022 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2024 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2025 hpa_t root = vcpu->arch.mmu.root_hpa;
2026 sp = page_header(root);
2027 mmu_sync_children(vcpu, sp);
2030 for (i = 0; i < 4; ++i) {
2031 hpa_t root = vcpu->arch.mmu.pae_root[i];
2033 if (root && VALID_PAGE(root)) {
2034 root &= PT64_BASE_ADDR_MASK;
2035 sp = page_header(root);
2036 mmu_sync_children(vcpu, sp);
2041 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2043 spin_lock(&vcpu->kvm->mmu_lock);
2044 mmu_sync_roots(vcpu);
2045 spin_unlock(&vcpu->kvm->mmu_lock);
2048 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2053 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2059 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2060 r = mmu_topup_memory_caches(vcpu);
2065 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2067 gfn = gva >> PAGE_SHIFT;
2069 return nonpaging_map(vcpu, gva & PAGE_MASK,
2070 error_code & PFERR_WRITE_MASK, gfn);
2073 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2079 gfn_t gfn = gpa >> PAGE_SHIFT;
2080 unsigned long mmu_seq;
2083 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2085 r = mmu_topup_memory_caches(vcpu);
2089 if (is_largepage_backed(vcpu, gfn &
2090 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
2091 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
2094 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2096 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2097 if (is_error_pfn(pfn)) {
2098 kvm_release_pfn_clean(pfn);
2101 spin_lock(&vcpu->kvm->mmu_lock);
2102 if (mmu_notifier_retry(vcpu, mmu_seq))
2104 kvm_mmu_free_some_pages(vcpu);
2105 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2106 largepage, gfn, pfn);
2107 spin_unlock(&vcpu->kvm->mmu_lock);
2112 spin_unlock(&vcpu->kvm->mmu_lock);
2113 kvm_release_pfn_clean(pfn);
2117 static void nonpaging_free(struct kvm_vcpu *vcpu)
2119 mmu_free_roots(vcpu);
2122 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2124 struct kvm_mmu *context = &vcpu->arch.mmu;
2126 context->new_cr3 = nonpaging_new_cr3;
2127 context->page_fault = nonpaging_page_fault;
2128 context->gva_to_gpa = nonpaging_gva_to_gpa;
2129 context->free = nonpaging_free;
2130 context->prefetch_page = nonpaging_prefetch_page;
2131 context->sync_page = nonpaging_sync_page;
2132 context->invlpg = nonpaging_invlpg;
2133 context->root_level = 0;
2134 context->shadow_root_level = PT32E_ROOT_LEVEL;
2135 context->root_hpa = INVALID_PAGE;
2139 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2141 ++vcpu->stat.tlb_flush;
2142 kvm_x86_ops->tlb_flush(vcpu);
2145 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2147 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2148 mmu_free_roots(vcpu);
2151 static void inject_page_fault(struct kvm_vcpu *vcpu,
2155 kvm_inject_page_fault(vcpu, addr, err_code);
2158 static void paging_free(struct kvm_vcpu *vcpu)
2160 nonpaging_free(vcpu);
2163 static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2167 bit7 = (gpte >> 7) & 1;
2168 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2172 #include "paging_tmpl.h"
2176 #include "paging_tmpl.h"
2179 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2181 struct kvm_mmu *context = &vcpu->arch.mmu;
2182 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2183 u64 exb_bit_rsvd = 0;
2186 exb_bit_rsvd = rsvd_bits(63, 63);
2188 case PT32_ROOT_LEVEL:
2189 /* no rsvd bits for 2 level 4K page table entries */
2190 context->rsvd_bits_mask[0][1] = 0;
2191 context->rsvd_bits_mask[0][0] = 0;
2192 if (is_cpuid_PSE36())
2193 /* 36bits PSE 4MB page */
2194 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2196 /* 32 bits PSE 4MB page */
2197 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2198 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2200 case PT32E_ROOT_LEVEL:
2201 context->rsvd_bits_mask[0][2] =
2202 rsvd_bits(maxphyaddr, 63) |
2203 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2204 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2205 rsvd_bits(maxphyaddr, 62); /* PDE */
2206 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2207 rsvd_bits(maxphyaddr, 62); /* PTE */
2208 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2209 rsvd_bits(maxphyaddr, 62) |
2210 rsvd_bits(13, 20); /* large page */
2211 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2213 case PT64_ROOT_LEVEL:
2214 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2215 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2216 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2217 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2218 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2219 rsvd_bits(maxphyaddr, 51);
2220 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2221 rsvd_bits(maxphyaddr, 51);
2222 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2223 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2224 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2225 rsvd_bits(maxphyaddr, 51) |
2226 rsvd_bits(13, 20); /* large page */
2227 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2232 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2234 struct kvm_mmu *context = &vcpu->arch.mmu;
2236 ASSERT(is_pae(vcpu));
2237 context->new_cr3 = paging_new_cr3;
2238 context->page_fault = paging64_page_fault;
2239 context->gva_to_gpa = paging64_gva_to_gpa;
2240 context->prefetch_page = paging64_prefetch_page;
2241 context->sync_page = paging64_sync_page;
2242 context->invlpg = paging64_invlpg;
2243 context->free = paging_free;
2244 context->root_level = level;
2245 context->shadow_root_level = level;
2246 context->root_hpa = INVALID_PAGE;
2250 static int paging64_init_context(struct kvm_vcpu *vcpu)
2252 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2253 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2256 static int paging32_init_context(struct kvm_vcpu *vcpu)
2258 struct kvm_mmu *context = &vcpu->arch.mmu;
2260 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2261 context->new_cr3 = paging_new_cr3;
2262 context->page_fault = paging32_page_fault;
2263 context->gva_to_gpa = paging32_gva_to_gpa;
2264 context->free = paging_free;
2265 context->prefetch_page = paging32_prefetch_page;
2266 context->sync_page = paging32_sync_page;
2267 context->invlpg = paging32_invlpg;
2268 context->root_level = PT32_ROOT_LEVEL;
2269 context->shadow_root_level = PT32E_ROOT_LEVEL;
2270 context->root_hpa = INVALID_PAGE;
2274 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2276 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2277 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2280 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2282 struct kvm_mmu *context = &vcpu->arch.mmu;
2284 context->new_cr3 = nonpaging_new_cr3;
2285 context->page_fault = tdp_page_fault;
2286 context->free = nonpaging_free;
2287 context->prefetch_page = nonpaging_prefetch_page;
2288 context->sync_page = nonpaging_sync_page;
2289 context->invlpg = nonpaging_invlpg;
2290 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2291 context->root_hpa = INVALID_PAGE;
2293 if (!is_paging(vcpu)) {
2294 context->gva_to_gpa = nonpaging_gva_to_gpa;
2295 context->root_level = 0;
2296 } else if (is_long_mode(vcpu)) {
2297 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2298 context->gva_to_gpa = paging64_gva_to_gpa;
2299 context->root_level = PT64_ROOT_LEVEL;
2300 } else if (is_pae(vcpu)) {
2301 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2302 context->gva_to_gpa = paging64_gva_to_gpa;
2303 context->root_level = PT32E_ROOT_LEVEL;
2305 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2306 context->gva_to_gpa = paging32_gva_to_gpa;
2307 context->root_level = PT32_ROOT_LEVEL;
2313 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2318 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2320 if (!is_paging(vcpu))
2321 r = nonpaging_init_context(vcpu);
2322 else if (is_long_mode(vcpu))
2323 r = paging64_init_context(vcpu);
2324 else if (is_pae(vcpu))
2325 r = paging32E_init_context(vcpu);
2327 r = paging32_init_context(vcpu);
2329 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2334 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2336 vcpu->arch.update_pte.pfn = bad_pfn;
2339 return init_kvm_tdp_mmu(vcpu);
2341 return init_kvm_softmmu(vcpu);
2344 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2347 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2348 vcpu->arch.mmu.free(vcpu);
2349 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2353 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2355 destroy_kvm_mmu(vcpu);
2356 return init_kvm_mmu(vcpu);
2358 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2360 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2364 r = mmu_topup_memory_caches(vcpu);
2367 spin_lock(&vcpu->kvm->mmu_lock);
2368 kvm_mmu_free_some_pages(vcpu);
2369 r = mmu_alloc_roots(vcpu);
2370 mmu_sync_roots(vcpu);
2371 spin_unlock(&vcpu->kvm->mmu_lock);
2374 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2375 kvm_mmu_flush_tlb(vcpu);
2379 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2381 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2383 mmu_free_roots(vcpu);
2386 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2387 struct kvm_mmu_page *sp,
2391 struct kvm_mmu_page *child;
2394 if (is_shadow_present_pte(pte)) {
2395 if (is_last_spte(pte, sp->role.level))
2396 rmap_remove(vcpu->kvm, spte);
2398 child = page_header(pte & PT64_BASE_ADDR_MASK);
2399 mmu_page_remove_parent_pte(child, spte);
2402 __set_spte(spte, shadow_trap_nonpresent_pte);
2403 if (is_large_pte(pte))
2404 --vcpu->kvm->stat.lpages;
2407 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2408 struct kvm_mmu_page *sp,
2412 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2413 if (!vcpu->arch.update_pte.largepage ||
2414 sp->role.glevels == PT32_ROOT_LEVEL) {
2415 ++vcpu->kvm->stat.mmu_pde_zapped;
2420 ++vcpu->kvm->stat.mmu_pte_updated;
2421 if (sp->role.glevels == PT32_ROOT_LEVEL)
2422 paging32_update_pte(vcpu, sp, spte, new);
2424 paging64_update_pte(vcpu, sp, spte, new);
2427 static bool need_remote_flush(u64 old, u64 new)
2429 if (!is_shadow_present_pte(old))
2431 if (!is_shadow_present_pte(new))
2433 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2435 old ^= PT64_NX_MASK;
2436 new ^= PT64_NX_MASK;
2437 return (old & ~new & PT64_PERM_MASK) != 0;
2440 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2442 if (need_remote_flush(old, new))
2443 kvm_flush_remote_tlbs(vcpu->kvm);
2445 kvm_mmu_flush_tlb(vcpu);
2448 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2450 u64 *spte = vcpu->arch.last_pte_updated;
2452 return !!(spte && (*spte & shadow_accessed_mask));
2455 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2456 const u8 *new, int bytes)
2463 vcpu->arch.update_pte.largepage = 0;
2465 if (bytes != 4 && bytes != 8)
2469 * Assume that the pte write on a page table of the same type
2470 * as the current vcpu paging mode. This is nearly always true
2471 * (might be false while changing modes). Note it is verified later
2475 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2476 if ((bytes == 4) && (gpa % 4 == 0)) {
2477 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2480 memcpy((void *)&gpte + (gpa % 8), new, 4);
2481 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2482 memcpy((void *)&gpte, new, 8);
2485 if ((bytes == 4) && (gpa % 4 == 0))
2486 memcpy((void *)&gpte, new, 4);
2488 if (!is_present_gpte(gpte))
2490 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2492 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2493 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
2494 vcpu->arch.update_pte.largepage = 1;
2496 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2498 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2500 if (is_error_pfn(pfn)) {
2501 kvm_release_pfn_clean(pfn);
2504 vcpu->arch.update_pte.gfn = gfn;
2505 vcpu->arch.update_pte.pfn = pfn;
2508 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2510 u64 *spte = vcpu->arch.last_pte_updated;
2513 && vcpu->arch.last_pte_gfn == gfn
2514 && shadow_accessed_mask
2515 && !(*spte & shadow_accessed_mask)
2516 && is_shadow_present_pte(*spte))
2517 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2520 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2521 const u8 *new, int bytes,
2522 bool guest_initiated)
2524 gfn_t gfn = gpa >> PAGE_SHIFT;
2525 struct kvm_mmu_page *sp;
2526 struct hlist_node *node, *n;
2527 struct hlist_head *bucket;
2531 unsigned offset = offset_in_page(gpa);
2533 unsigned page_offset;
2534 unsigned misaligned;
2541 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2542 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
2543 spin_lock(&vcpu->kvm->mmu_lock);
2544 kvm_mmu_access_page(vcpu, gfn);
2545 kvm_mmu_free_some_pages(vcpu);
2546 ++vcpu->kvm->stat.mmu_pte_write;
2547 kvm_mmu_audit(vcpu, "pre pte write");
2548 if (guest_initiated) {
2549 if (gfn == vcpu->arch.last_pt_write_gfn
2550 && !last_updated_pte_accessed(vcpu)) {
2551 ++vcpu->arch.last_pt_write_count;
2552 if (vcpu->arch.last_pt_write_count >= 3)
2555 vcpu->arch.last_pt_write_gfn = gfn;
2556 vcpu->arch.last_pt_write_count = 1;
2557 vcpu->arch.last_pte_updated = NULL;
2560 index = kvm_page_table_hashfn(gfn);
2561 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2562 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2563 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2565 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
2566 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2567 misaligned |= bytes < 4;
2568 if (misaligned || flooded) {
2570 * Misaligned accesses are too much trouble to fix
2571 * up; also, they usually indicate a page is not used
2574 * If we're seeing too many writes to a page,
2575 * it may no longer be a page table, or we may be
2576 * forking, in which case it is better to unmap the
2579 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2580 gpa, bytes, sp->role.word);
2581 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2583 ++vcpu->kvm->stat.mmu_flooded;
2586 page_offset = offset;
2587 level = sp->role.level;
2589 if (sp->role.glevels == PT32_ROOT_LEVEL) {
2590 page_offset <<= 1; /* 32->64 */
2592 * A 32-bit pde maps 4MB while the shadow pdes map
2593 * only 2MB. So we need to double the offset again
2594 * and zap two pdes instead of one.
2596 if (level == PT32_ROOT_LEVEL) {
2597 page_offset &= ~7; /* kill rounding error */
2601 quadrant = page_offset >> PAGE_SHIFT;
2602 page_offset &= ~PAGE_MASK;
2603 if (quadrant != sp->role.quadrant)
2606 spte = &sp->spt[page_offset / sizeof(*spte)];
2607 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2609 r = kvm_read_guest_atomic(vcpu->kvm,
2610 gpa & ~(u64)(pte_size - 1),
2612 new = (const void *)&gentry;
2618 mmu_pte_write_zap_pte(vcpu, sp, spte);
2620 mmu_pte_write_new_pte(vcpu, sp, spte, new);
2621 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2625 kvm_mmu_audit(vcpu, "post pte write");
2626 spin_unlock(&vcpu->kvm->mmu_lock);
2627 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2628 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2629 vcpu->arch.update_pte.pfn = bad_pfn;
2633 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2638 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
2640 spin_lock(&vcpu->kvm->mmu_lock);
2641 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2642 spin_unlock(&vcpu->kvm->mmu_lock);
2645 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2647 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2649 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
2650 struct kvm_mmu_page *sp;
2652 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2653 struct kvm_mmu_page, link);
2654 kvm_mmu_zap_page(vcpu->kvm, sp);
2655 ++vcpu->kvm->stat.mmu_recycled;
2659 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2662 enum emulation_result er;
2664 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2673 r = mmu_topup_memory_caches(vcpu);
2677 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2682 case EMULATE_DO_MMIO:
2683 ++vcpu->stat.mmio_exits;
2686 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2687 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2695 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2697 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2699 vcpu->arch.mmu.invlpg(vcpu, gva);
2700 kvm_mmu_flush_tlb(vcpu);
2701 ++vcpu->stat.invlpg;
2703 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2705 void kvm_enable_tdp(void)
2709 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2711 void kvm_disable_tdp(void)
2713 tdp_enabled = false;
2715 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2717 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2719 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2722 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2729 if (vcpu->kvm->arch.n_requested_mmu_pages)
2730 vcpu->kvm->arch.n_free_mmu_pages =
2731 vcpu->kvm->arch.n_requested_mmu_pages;
2733 vcpu->kvm->arch.n_free_mmu_pages =
2734 vcpu->kvm->arch.n_alloc_mmu_pages;
2736 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2737 * Therefore we need to allocate shadow page tables in the first
2738 * 4GB of memory, which happens to fit the DMA32 zone.
2740 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2743 vcpu->arch.mmu.pae_root = page_address(page);
2744 for (i = 0; i < 4; ++i)
2745 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2750 free_mmu_pages(vcpu);
2754 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2757 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2759 return alloc_mmu_pages(vcpu);
2762 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2765 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2767 return init_kvm_mmu(vcpu);
2770 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2774 destroy_kvm_mmu(vcpu);
2775 free_mmu_pages(vcpu);
2776 mmu_free_memory_caches(vcpu);
2779 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2781 struct kvm_mmu_page *sp;
2783 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2787 if (!test_bit(slot, sp->slot_bitmap))
2791 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2793 if (pt[i] & PT_WRITABLE_MASK)
2794 pt[i] &= ~PT_WRITABLE_MASK;
2796 kvm_flush_remote_tlbs(kvm);
2799 void kvm_mmu_zap_all(struct kvm *kvm)
2801 struct kvm_mmu_page *sp, *node;
2803 spin_lock(&kvm->mmu_lock);
2804 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2805 if (kvm_mmu_zap_page(kvm, sp))
2806 node = container_of(kvm->arch.active_mmu_pages.next,
2807 struct kvm_mmu_page, link);
2808 spin_unlock(&kvm->mmu_lock);
2810 kvm_flush_remote_tlbs(kvm);
2813 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2815 struct kvm_mmu_page *page;
2817 page = container_of(kvm->arch.active_mmu_pages.prev,
2818 struct kvm_mmu_page, link);
2819 kvm_mmu_zap_page(kvm, page);
2822 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2825 struct kvm *kvm_freed = NULL;
2826 int cache_count = 0;
2828 spin_lock(&kvm_lock);
2830 list_for_each_entry(kvm, &vm_list, vm_list) {
2833 if (!down_read_trylock(&kvm->slots_lock))
2835 spin_lock(&kvm->mmu_lock);
2836 npages = kvm->arch.n_alloc_mmu_pages -
2837 kvm->arch.n_free_mmu_pages;
2838 cache_count += npages;
2839 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2840 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2846 spin_unlock(&kvm->mmu_lock);
2847 up_read(&kvm->slots_lock);
2850 list_move_tail(&kvm_freed->vm_list, &vm_list);
2852 spin_unlock(&kvm_lock);
2857 static struct shrinker mmu_shrinker = {
2858 .shrink = mmu_shrink,
2859 .seeks = DEFAULT_SEEKS * 10,
2862 static void mmu_destroy_caches(void)
2864 if (pte_chain_cache)
2865 kmem_cache_destroy(pte_chain_cache);
2866 if (rmap_desc_cache)
2867 kmem_cache_destroy(rmap_desc_cache);
2868 if (mmu_page_header_cache)
2869 kmem_cache_destroy(mmu_page_header_cache);
2872 void kvm_mmu_module_exit(void)
2874 mmu_destroy_caches();
2875 unregister_shrinker(&mmu_shrinker);
2878 int kvm_mmu_module_init(void)
2880 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2881 sizeof(struct kvm_pte_chain),
2883 if (!pte_chain_cache)
2885 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2886 sizeof(struct kvm_rmap_desc),
2888 if (!rmap_desc_cache)
2891 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2892 sizeof(struct kvm_mmu_page),
2894 if (!mmu_page_header_cache)
2897 register_shrinker(&mmu_shrinker);
2902 mmu_destroy_caches();
2907 * Caculate mmu pages needed for kvm.
2909 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2912 unsigned int nr_mmu_pages;
2913 unsigned int nr_pages = 0;
2915 for (i = 0; i < kvm->nmemslots; i++)
2916 nr_pages += kvm->memslots[i].npages;
2918 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2919 nr_mmu_pages = max(nr_mmu_pages,
2920 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2922 return nr_mmu_pages;
2925 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2928 if (len > buffer->len)
2933 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2938 ret = pv_mmu_peek_buffer(buffer, len);
2943 buffer->processed += len;
2947 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2948 gpa_t addr, gpa_t value)
2953 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2956 r = mmu_topup_memory_caches(vcpu);
2960 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2966 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2968 kvm_set_cr3(vcpu, vcpu->arch.cr3);
2972 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2974 spin_lock(&vcpu->kvm->mmu_lock);
2975 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2976 spin_unlock(&vcpu->kvm->mmu_lock);
2980 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2981 struct kvm_pv_mmu_op_buffer *buffer)
2983 struct kvm_mmu_op_header *header;
2985 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2988 switch (header->op) {
2989 case KVM_MMU_OP_WRITE_PTE: {
2990 struct kvm_mmu_op_write_pte *wpte;
2992 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2995 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2998 case KVM_MMU_OP_FLUSH_TLB: {
2999 struct kvm_mmu_op_flush_tlb *ftlb;
3001 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3004 return kvm_pv_mmu_flush_tlb(vcpu);
3006 case KVM_MMU_OP_RELEASE_PT: {
3007 struct kvm_mmu_op_release_pt *rpt;
3009 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3012 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3018 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3019 gpa_t addr, unsigned long *ret)
3022 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3024 buffer->ptr = buffer->buf;
3025 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3026 buffer->processed = 0;
3028 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3032 while (buffer->len) {
3033 r = kvm_pv_mmu_op_one(vcpu, buffer);
3042 *ret = buffer->processed;
3046 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3048 struct kvm_shadow_walk_iterator iterator;
3051 spin_lock(&vcpu->kvm->mmu_lock);
3052 for_each_shadow_entry(vcpu, addr, iterator) {
3053 sptes[iterator.level-1] = *iterator.sptep;
3055 if (!is_shadow_present_pte(*iterator.sptep))
3058 spin_unlock(&vcpu->kvm->mmu_lock);
3062 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3066 static const char *audit_msg;
3068 static gva_t canonicalize(gva_t gva)
3070 #ifdef CONFIG_X86_64
3071 gva = (long long)(gva << 16) >> 16;
3077 typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3080 static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3085 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3086 u64 ent = sp->spt[i];
3088 if (is_shadow_present_pte(ent)) {
3089 if (!is_last_spte(ent, sp->role.level)) {
3090 struct kvm_mmu_page *child;
3091 child = page_header(ent & PT64_BASE_ADDR_MASK);
3092 __mmu_spte_walk(kvm, child, fn);
3094 fn(kvm, sp, &sp->spt[i]);
3099 static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3102 struct kvm_mmu_page *sp;
3104 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3106 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3107 hpa_t root = vcpu->arch.mmu.root_hpa;
3108 sp = page_header(root);
3109 __mmu_spte_walk(vcpu->kvm, sp, fn);
3112 for (i = 0; i < 4; ++i) {
3113 hpa_t root = vcpu->arch.mmu.pae_root[i];
3115 if (root && VALID_PAGE(root)) {
3116 root &= PT64_BASE_ADDR_MASK;
3117 sp = page_header(root);
3118 __mmu_spte_walk(vcpu->kvm, sp, fn);
3124 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3125 gva_t va, int level)
3127 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3129 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3131 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3134 if (ent == shadow_trap_nonpresent_pte)
3137 va = canonicalize(va);
3138 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3139 audit_mappings_page(vcpu, ent, va, level - 1);
3141 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
3142 gfn_t gfn = gpa >> PAGE_SHIFT;
3143 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3144 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
3146 if (is_error_pfn(pfn)) {
3147 kvm_release_pfn_clean(pfn);
3151 if (is_shadow_present_pte(ent)
3152 && (ent & PT64_BASE_ADDR_MASK) != hpa)
3153 printk(KERN_ERR "xx audit error: (%s) levels %d"
3154 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3155 audit_msg, vcpu->arch.mmu.root_level,
3157 is_shadow_present_pte(ent));
3158 else if (ent == shadow_notrap_nonpresent_pte
3159 && !is_error_hpa(hpa))
3160 printk(KERN_ERR "audit: (%s) notrap shadow,"
3161 " valid guest gva %lx\n", audit_msg, va);
3162 kvm_release_pfn_clean(pfn);
3168 static void audit_mappings(struct kvm_vcpu *vcpu)
3172 if (vcpu->arch.mmu.root_level == 4)
3173 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3175 for (i = 0; i < 4; ++i)
3176 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3177 audit_mappings_page(vcpu,
3178 vcpu->arch.mmu.pae_root[i],
3183 static int count_rmaps(struct kvm_vcpu *vcpu)
3188 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3189 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3190 struct kvm_rmap_desc *d;
3192 for (j = 0; j < m->npages; ++j) {
3193 unsigned long *rmapp = &m->rmap[j];
3197 if (!(*rmapp & 1)) {
3201 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3203 for (k = 0; k < RMAP_EXT; ++k)
3215 void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
3217 unsigned long *rmapp;
3218 struct kvm_mmu_page *rev_sp;
3221 if (*sptep & PT_WRITABLE_MASK) {
3222 rev_sp = page_header(__pa(sptep));
3223 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3225 if (!gfn_to_memslot(kvm, gfn)) {
3226 if (!printk_ratelimit())
3228 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3230 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3231 audit_msg, sptep - rev_sp->spt,
3237 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3238 is_large_pte(*sptep));
3240 if (!printk_ratelimit())
3242 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3250 void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3252 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3255 static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3257 struct kvm_mmu_page *sp;
3260 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3263 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3266 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3269 if (!(ent & PT_PRESENT_MASK))
3271 if (!(ent & PT_WRITABLE_MASK))
3273 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
3279 static void audit_rmap(struct kvm_vcpu *vcpu)
3281 check_writable_mappings_rmap(vcpu);
3285 static void audit_write_protection(struct kvm_vcpu *vcpu)
3287 struct kvm_mmu_page *sp;
3288 struct kvm_memory_slot *slot;
3289 unsigned long *rmapp;
3293 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3294 if (sp->role.direct)
3299 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3300 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3301 rmapp = &slot->rmap[gfn - slot->base_gfn];
3303 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3305 if (*spte & PT_WRITABLE_MASK)
3306 printk(KERN_ERR "%s: (%s) shadow page has "
3307 "writable mappings: gfn %lx role %x\n",
3308 __func__, audit_msg, sp->gfn,
3310 spte = rmap_next(vcpu->kvm, rmapp, spte);
3315 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3322 audit_write_protection(vcpu);
3323 if (strcmp("pre pte write", audit_msg) != 0)
3324 audit_mappings(vcpu);
3325 audit_writable_sptes_have_rmaps(vcpu);