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_FETCH_MASK (1U << 4)
131 #define PT_DIRECTORY_LEVEL 2
132 #define PT_PAGE_TABLE_LEVEL 1
136 #define ACC_EXEC_MASK 1
137 #define ACC_WRITE_MASK PT_WRITABLE_MASK
138 #define ACC_USER_MASK PT_USER_MASK
139 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
141 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
143 struct kvm_rmap_desc {
144 u64 *shadow_ptes[RMAP_EXT];
145 struct kvm_rmap_desc *more;
148 struct kvm_shadow_walk_iterator {
156 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
157 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
158 shadow_walk_okay(&(_walker)); \
159 shadow_walk_next(&(_walker)))
162 struct kvm_unsync_walk {
163 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
166 typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
168 static struct kmem_cache *pte_chain_cache;
169 static struct kmem_cache *rmap_desc_cache;
170 static struct kmem_cache *mmu_page_header_cache;
172 static u64 __read_mostly shadow_trap_nonpresent_pte;
173 static u64 __read_mostly shadow_notrap_nonpresent_pte;
174 static u64 __read_mostly shadow_base_present_pte;
175 static u64 __read_mostly shadow_nx_mask;
176 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
177 static u64 __read_mostly shadow_user_mask;
178 static u64 __read_mostly shadow_accessed_mask;
179 static u64 __read_mostly shadow_dirty_mask;
180 static u64 __read_mostly shadow_mt_mask;
182 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
184 shadow_trap_nonpresent_pte = trap_pte;
185 shadow_notrap_nonpresent_pte = notrap_pte;
187 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
189 void kvm_mmu_set_base_ptes(u64 base_pte)
191 shadow_base_present_pte = base_pte;
193 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
195 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
196 u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask)
198 shadow_user_mask = user_mask;
199 shadow_accessed_mask = accessed_mask;
200 shadow_dirty_mask = dirty_mask;
201 shadow_nx_mask = nx_mask;
202 shadow_x_mask = x_mask;
203 shadow_mt_mask = mt_mask;
205 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
207 static int is_write_protection(struct kvm_vcpu *vcpu)
209 return vcpu->arch.cr0 & X86_CR0_WP;
212 static int is_cpuid_PSE36(void)
217 static int is_nx(struct kvm_vcpu *vcpu)
219 return vcpu->arch.shadow_efer & EFER_NX;
222 static int is_present_pte(unsigned long pte)
224 return pte & PT_PRESENT_MASK;
227 static int is_shadow_present_pte(u64 pte)
229 return pte != shadow_trap_nonpresent_pte
230 && pte != shadow_notrap_nonpresent_pte;
233 static int is_large_pte(u64 pte)
235 return pte & PT_PAGE_SIZE_MASK;
238 static int is_writeble_pte(unsigned long pte)
240 return pte & PT_WRITABLE_MASK;
243 static int is_dirty_pte(unsigned long pte)
245 return pte & shadow_dirty_mask;
248 static int is_rmap_pte(u64 pte)
250 return is_shadow_present_pte(pte);
253 static pfn_t spte_to_pfn(u64 pte)
255 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
258 static gfn_t pse36_gfn_delta(u32 gpte)
260 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
262 return (gpte & PT32_DIR_PSE36_MASK) << shift;
265 static void set_shadow_pte(u64 *sptep, u64 spte)
268 set_64bit((unsigned long *)sptep, spte);
270 set_64bit((unsigned long long *)sptep, spte);
274 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
275 struct kmem_cache *base_cache, int min)
279 if (cache->nobjs >= min)
281 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
282 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
285 cache->objects[cache->nobjs++] = obj;
290 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
293 kfree(mc->objects[--mc->nobjs]);
296 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
301 if (cache->nobjs >= min)
303 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
304 page = alloc_page(GFP_KERNEL);
307 set_page_private(page, 0);
308 cache->objects[cache->nobjs++] = page_address(page);
313 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
316 free_page((unsigned long)mc->objects[--mc->nobjs]);
319 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
323 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
327 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
331 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
334 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
335 mmu_page_header_cache, 4);
340 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
342 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
343 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
344 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
345 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
348 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
354 p = mc->objects[--mc->nobjs];
358 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
360 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
361 sizeof(struct kvm_pte_chain));
364 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
369 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
371 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
372 sizeof(struct kvm_rmap_desc));
375 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
381 * Return the pointer to the largepage write count for a given
382 * gfn, handling slots that are not large page aligned.
384 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
388 idx = (gfn / KVM_PAGES_PER_HPAGE) -
389 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
390 return &slot->lpage_info[idx].write_count;
393 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
397 gfn = unalias_gfn(kvm, gfn);
398 write_count = slot_largepage_idx(gfn,
399 gfn_to_memslot_unaliased(kvm, gfn));
403 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
407 gfn = unalias_gfn(kvm, gfn);
408 write_count = slot_largepage_idx(gfn,
409 gfn_to_memslot_unaliased(kvm, gfn));
411 WARN_ON(*write_count < 0);
414 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
416 struct kvm_memory_slot *slot;
419 gfn = unalias_gfn(kvm, gfn);
420 slot = gfn_to_memslot_unaliased(kvm, gfn);
422 largepage_idx = slot_largepage_idx(gfn, slot);
423 return *largepage_idx;
429 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
431 struct vm_area_struct *vma;
435 addr = gfn_to_hva(kvm, gfn);
436 if (kvm_is_error_hva(addr))
439 down_read(¤t->mm->mmap_sem);
440 vma = find_vma(current->mm, addr);
441 if (vma && is_vm_hugetlb_page(vma))
443 up_read(¤t->mm->mmap_sem);
448 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
450 struct kvm_memory_slot *slot;
452 if (has_wrprotected_page(vcpu->kvm, large_gfn))
455 if (!host_largepage_backed(vcpu->kvm, large_gfn))
458 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
459 if (slot && slot->dirty_bitmap)
466 * Take gfn and return the reverse mapping to it.
467 * Note: gfn must be unaliased before this function get called
470 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
472 struct kvm_memory_slot *slot;
475 slot = gfn_to_memslot(kvm, gfn);
477 return &slot->rmap[gfn - slot->base_gfn];
479 idx = (gfn / KVM_PAGES_PER_HPAGE) -
480 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
482 return &slot->lpage_info[idx].rmap_pde;
486 * Reverse mapping data structures:
488 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
489 * that points to page_address(page).
491 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
492 * containing more mappings.
494 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
496 struct kvm_mmu_page *sp;
497 struct kvm_rmap_desc *desc;
498 unsigned long *rmapp;
501 if (!is_rmap_pte(*spte))
503 gfn = unalias_gfn(vcpu->kvm, gfn);
504 sp = page_header(__pa(spte));
505 sp->gfns[spte - sp->spt] = gfn;
506 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
508 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
509 *rmapp = (unsigned long)spte;
510 } else if (!(*rmapp & 1)) {
511 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
512 desc = mmu_alloc_rmap_desc(vcpu);
513 desc->shadow_ptes[0] = (u64 *)*rmapp;
514 desc->shadow_ptes[1] = spte;
515 *rmapp = (unsigned long)desc | 1;
517 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
518 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
519 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
521 if (desc->shadow_ptes[RMAP_EXT-1]) {
522 desc->more = mmu_alloc_rmap_desc(vcpu);
525 for (i = 0; desc->shadow_ptes[i]; ++i)
527 desc->shadow_ptes[i] = spte;
531 static void rmap_desc_remove_entry(unsigned long *rmapp,
532 struct kvm_rmap_desc *desc,
534 struct kvm_rmap_desc *prev_desc)
538 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
540 desc->shadow_ptes[i] = desc->shadow_ptes[j];
541 desc->shadow_ptes[j] = NULL;
544 if (!prev_desc && !desc->more)
545 *rmapp = (unsigned long)desc->shadow_ptes[0];
548 prev_desc->more = desc->more;
550 *rmapp = (unsigned long)desc->more | 1;
551 mmu_free_rmap_desc(desc);
554 static void rmap_remove(struct kvm *kvm, u64 *spte)
556 struct kvm_rmap_desc *desc;
557 struct kvm_rmap_desc *prev_desc;
558 struct kvm_mmu_page *sp;
560 unsigned long *rmapp;
563 if (!is_rmap_pte(*spte))
565 sp = page_header(__pa(spte));
566 pfn = spte_to_pfn(*spte);
567 if (*spte & shadow_accessed_mask)
568 kvm_set_pfn_accessed(pfn);
569 if (is_writeble_pte(*spte))
570 kvm_release_pfn_dirty(pfn);
572 kvm_release_pfn_clean(pfn);
573 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
575 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
577 } else if (!(*rmapp & 1)) {
578 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
579 if ((u64 *)*rmapp != spte) {
580 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
586 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
587 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
590 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
591 if (desc->shadow_ptes[i] == spte) {
592 rmap_desc_remove_entry(rmapp,
604 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
606 struct kvm_rmap_desc *desc;
607 struct kvm_rmap_desc *prev_desc;
613 else if (!(*rmapp & 1)) {
615 return (u64 *)*rmapp;
618 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
622 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
623 if (prev_spte == spte)
624 return desc->shadow_ptes[i];
625 prev_spte = desc->shadow_ptes[i];
632 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
634 unsigned long *rmapp;
636 int write_protected = 0;
638 gfn = unalias_gfn(kvm, gfn);
639 rmapp = gfn_to_rmap(kvm, gfn, 0);
641 spte = rmap_next(kvm, rmapp, NULL);
644 BUG_ON(!(*spte & PT_PRESENT_MASK));
645 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
646 if (is_writeble_pte(*spte)) {
647 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
650 spte = rmap_next(kvm, rmapp, spte);
652 if (write_protected) {
655 spte = rmap_next(kvm, rmapp, NULL);
656 pfn = spte_to_pfn(*spte);
657 kvm_set_pfn_dirty(pfn);
660 /* check for huge page mappings */
661 rmapp = gfn_to_rmap(kvm, gfn, 1);
662 spte = rmap_next(kvm, rmapp, NULL);
665 BUG_ON(!(*spte & PT_PRESENT_MASK));
666 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
667 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
668 if (is_writeble_pte(*spte)) {
669 rmap_remove(kvm, spte);
671 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
675 spte = rmap_next(kvm, rmapp, spte);
678 return write_protected;
681 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
684 int need_tlb_flush = 0;
686 while ((spte = rmap_next(kvm, rmapp, NULL))) {
687 BUG_ON(!(*spte & PT_PRESENT_MASK));
688 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
689 rmap_remove(kvm, spte);
690 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
693 return need_tlb_flush;
696 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
697 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
703 * If mmap_sem isn't taken, we can look the memslots with only
704 * the mmu_lock by skipping over the slots with userspace_addr == 0.
706 for (i = 0; i < kvm->nmemslots; i++) {
707 struct kvm_memory_slot *memslot = &kvm->memslots[i];
708 unsigned long start = memslot->userspace_addr;
711 /* mmu_lock protects userspace_addr */
715 end = start + (memslot->npages << PAGE_SHIFT);
716 if (hva >= start && hva < end) {
717 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
718 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
719 retval |= handler(kvm,
720 &memslot->lpage_info[
722 KVM_PAGES_PER_HPAGE].rmap_pde);
729 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
731 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
734 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
739 /* always return old for EPT */
740 if (!shadow_accessed_mask)
743 spte = rmap_next(kvm, rmapp, NULL);
747 BUG_ON(!(_spte & PT_PRESENT_MASK));
748 _young = _spte & PT_ACCESSED_MASK;
751 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
753 spte = rmap_next(kvm, rmapp, spte);
758 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
760 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
764 static int is_empty_shadow_page(u64 *spt)
769 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
770 if (is_shadow_present_pte(*pos)) {
771 printk(KERN_ERR "%s: %p %llx\n", __func__,
779 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
781 ASSERT(is_empty_shadow_page(sp->spt));
783 __free_page(virt_to_page(sp->spt));
784 __free_page(virt_to_page(sp->gfns));
786 ++kvm->arch.n_free_mmu_pages;
789 static unsigned kvm_page_table_hashfn(gfn_t gfn)
791 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
794 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
797 struct kvm_mmu_page *sp;
799 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
800 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
801 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
802 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
803 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
804 INIT_LIST_HEAD(&sp->oos_link);
805 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
807 sp->parent_pte = parent_pte;
808 --vcpu->kvm->arch.n_free_mmu_pages;
812 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
813 struct kvm_mmu_page *sp, u64 *parent_pte)
815 struct kvm_pte_chain *pte_chain;
816 struct hlist_node *node;
821 if (!sp->multimapped) {
822 u64 *old = sp->parent_pte;
825 sp->parent_pte = parent_pte;
829 pte_chain = mmu_alloc_pte_chain(vcpu);
830 INIT_HLIST_HEAD(&sp->parent_ptes);
831 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
832 pte_chain->parent_ptes[0] = old;
834 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
835 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
837 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
838 if (!pte_chain->parent_ptes[i]) {
839 pte_chain->parent_ptes[i] = parent_pte;
843 pte_chain = mmu_alloc_pte_chain(vcpu);
845 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
846 pte_chain->parent_ptes[0] = parent_pte;
849 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
852 struct kvm_pte_chain *pte_chain;
853 struct hlist_node *node;
856 if (!sp->multimapped) {
857 BUG_ON(sp->parent_pte != parent_pte);
858 sp->parent_pte = NULL;
861 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
862 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
863 if (!pte_chain->parent_ptes[i])
865 if (pte_chain->parent_ptes[i] != parent_pte)
867 while (i + 1 < NR_PTE_CHAIN_ENTRIES
868 && pte_chain->parent_ptes[i + 1]) {
869 pte_chain->parent_ptes[i]
870 = pte_chain->parent_ptes[i + 1];
873 pte_chain->parent_ptes[i] = NULL;
875 hlist_del(&pte_chain->link);
876 mmu_free_pte_chain(pte_chain);
877 if (hlist_empty(&sp->parent_ptes)) {
879 sp->parent_pte = NULL;
888 static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
889 mmu_parent_walk_fn fn)
891 struct kvm_pte_chain *pte_chain;
892 struct hlist_node *node;
893 struct kvm_mmu_page *parent_sp;
896 if (!sp->multimapped && sp->parent_pte) {
897 parent_sp = page_header(__pa(sp->parent_pte));
899 mmu_parent_walk(vcpu, parent_sp, fn);
902 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
903 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
904 if (!pte_chain->parent_ptes[i])
906 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
908 mmu_parent_walk(vcpu, parent_sp, fn);
912 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
915 struct kvm_mmu_page *sp = page_header(__pa(spte));
917 index = spte - sp->spt;
918 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
919 sp->unsync_children++;
920 WARN_ON(!sp->unsync_children);
923 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
925 struct kvm_pte_chain *pte_chain;
926 struct hlist_node *node;
932 if (!sp->multimapped) {
933 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
937 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
938 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
939 if (!pte_chain->parent_ptes[i])
941 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
945 static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
947 kvm_mmu_update_parents_unsync(sp);
951 static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
952 struct kvm_mmu_page *sp)
954 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
955 kvm_mmu_update_parents_unsync(sp);
958 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
959 struct kvm_mmu_page *sp)
963 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
964 sp->spt[i] = shadow_trap_nonpresent_pte;
967 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
968 struct kvm_mmu_page *sp)
973 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
977 #define KVM_PAGE_ARRAY_NR 16
979 struct kvm_mmu_pages {
980 struct mmu_page_and_offset {
981 struct kvm_mmu_page *sp;
983 } page[KVM_PAGE_ARRAY_NR];
987 #define for_each_unsync_children(bitmap, idx) \
988 for (idx = find_first_bit(bitmap, 512); \
990 idx = find_next_bit(bitmap, 512, idx+1))
992 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
998 for (i=0; i < pvec->nr; i++)
999 if (pvec->page[i].sp == sp)
1002 pvec->page[pvec->nr].sp = sp;
1003 pvec->page[pvec->nr].idx = idx;
1005 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1008 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1009 struct kvm_mmu_pages *pvec)
1011 int i, ret, nr_unsync_leaf = 0;
1013 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1014 u64 ent = sp->spt[i];
1016 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1017 struct kvm_mmu_page *child;
1018 child = page_header(ent & PT64_BASE_ADDR_MASK);
1020 if (child->unsync_children) {
1021 if (mmu_pages_add(pvec, child, i))
1024 ret = __mmu_unsync_walk(child, pvec);
1026 __clear_bit(i, sp->unsync_child_bitmap);
1028 nr_unsync_leaf += ret;
1033 if (child->unsync) {
1035 if (mmu_pages_add(pvec, child, i))
1041 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1042 sp->unsync_children = 0;
1044 return nr_unsync_leaf;
1047 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1048 struct kvm_mmu_pages *pvec)
1050 if (!sp->unsync_children)
1053 mmu_pages_add(pvec, sp, 0);
1054 return __mmu_unsync_walk(sp, pvec);
1057 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1060 struct hlist_head *bucket;
1061 struct kvm_mmu_page *sp;
1062 struct hlist_node *node;
1064 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1065 index = kvm_page_table_hashfn(gfn);
1066 bucket = &kvm->arch.mmu_page_hash[index];
1067 hlist_for_each_entry(sp, node, bucket, hash_link)
1068 if (sp->gfn == gfn && !sp->role.direct
1069 && !sp->role.invalid) {
1070 pgprintk("%s: found role %x\n",
1071 __func__, sp->role.word);
1077 static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp)
1079 list_del(&sp->oos_link);
1080 --kvm->stat.mmu_unsync_global;
1083 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1085 WARN_ON(!sp->unsync);
1088 kvm_unlink_unsync_global(kvm, sp);
1089 --kvm->stat.mmu_unsync;
1092 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1094 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1096 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1097 kvm_mmu_zap_page(vcpu->kvm, sp);
1101 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1102 kvm_flush_remote_tlbs(vcpu->kvm);
1103 kvm_unlink_unsync_page(vcpu->kvm, sp);
1104 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1105 kvm_mmu_zap_page(vcpu->kvm, sp);
1109 kvm_mmu_flush_tlb(vcpu);
1113 struct mmu_page_path {
1114 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1115 unsigned int idx[PT64_ROOT_LEVEL-1];
1118 #define for_each_sp(pvec, sp, parents, i) \
1119 for (i = mmu_pages_next(&pvec, &parents, -1), \
1120 sp = pvec.page[i].sp; \
1121 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1122 i = mmu_pages_next(&pvec, &parents, i))
1124 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1125 struct mmu_page_path *parents,
1130 for (n = i+1; n < pvec->nr; n++) {
1131 struct kvm_mmu_page *sp = pvec->page[n].sp;
1133 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1134 parents->idx[0] = pvec->page[n].idx;
1138 parents->parent[sp->role.level-2] = sp;
1139 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1145 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1147 struct kvm_mmu_page *sp;
1148 unsigned int level = 0;
1151 unsigned int idx = parents->idx[level];
1153 sp = parents->parent[level];
1157 --sp->unsync_children;
1158 WARN_ON((int)sp->unsync_children < 0);
1159 __clear_bit(idx, sp->unsync_child_bitmap);
1161 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1164 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1165 struct mmu_page_path *parents,
1166 struct kvm_mmu_pages *pvec)
1168 parents->parent[parent->role.level-1] = NULL;
1172 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1173 struct kvm_mmu_page *parent)
1176 struct kvm_mmu_page *sp;
1177 struct mmu_page_path parents;
1178 struct kvm_mmu_pages pages;
1180 kvm_mmu_pages_init(parent, &parents, &pages);
1181 while (mmu_unsync_walk(parent, &pages)) {
1184 for_each_sp(pages, sp, parents, i)
1185 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1188 kvm_flush_remote_tlbs(vcpu->kvm);
1190 for_each_sp(pages, sp, parents, i) {
1191 kvm_sync_page(vcpu, sp);
1192 mmu_pages_clear_parents(&parents);
1194 cond_resched_lock(&vcpu->kvm->mmu_lock);
1195 kvm_mmu_pages_init(parent, &parents, &pages);
1199 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1207 union kvm_mmu_page_role role;
1210 struct hlist_head *bucket;
1211 struct kvm_mmu_page *sp;
1212 struct hlist_node *node, *tmp;
1214 role = vcpu->arch.mmu.base_role;
1216 role.direct = direct;
1217 role.access = access;
1218 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1219 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1220 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1221 role.quadrant = quadrant;
1223 pgprintk("%s: looking gfn %lx role %x\n", __func__,
1225 index = kvm_page_table_hashfn(gfn);
1226 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1227 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1228 if (sp->gfn == gfn) {
1230 if (kvm_sync_page(vcpu, sp))
1233 if (sp->role.word != role.word)
1236 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1237 if (sp->unsync_children) {
1238 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1239 kvm_mmu_mark_parents_unsync(vcpu, sp);
1241 pgprintk("%s: found\n", __func__);
1244 ++vcpu->kvm->stat.mmu_cache_miss;
1245 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1248 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
1251 sp->global = role.cr4_pge;
1252 hlist_add_head(&sp->hash_link, bucket);
1254 if (rmap_write_protect(vcpu->kvm, gfn))
1255 kvm_flush_remote_tlbs(vcpu->kvm);
1256 account_shadowed(vcpu->kvm, gfn);
1258 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1259 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1261 nonpaging_prefetch_page(vcpu, sp);
1265 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1266 struct kvm_vcpu *vcpu, u64 addr)
1268 iterator->addr = addr;
1269 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1270 iterator->level = vcpu->arch.mmu.shadow_root_level;
1271 if (iterator->level == PT32E_ROOT_LEVEL) {
1272 iterator->shadow_addr
1273 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1274 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1276 if (!iterator->shadow_addr)
1277 iterator->level = 0;
1281 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1283 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1285 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1286 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1290 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1292 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1296 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1297 struct kvm_mmu_page *sp)
1305 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1306 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1307 if (is_shadow_present_pte(pt[i]))
1308 rmap_remove(kvm, &pt[i]);
1309 pt[i] = shadow_trap_nonpresent_pte;
1314 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1317 if (is_shadow_present_pte(ent)) {
1318 if (!is_large_pte(ent)) {
1319 ent &= PT64_BASE_ADDR_MASK;
1320 mmu_page_remove_parent_pte(page_header(ent),
1324 rmap_remove(kvm, &pt[i]);
1327 pt[i] = shadow_trap_nonpresent_pte;
1331 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1333 mmu_page_remove_parent_pte(sp, parent_pte);
1336 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1340 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1342 kvm->vcpus[i]->arch.last_pte_updated = NULL;
1345 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1349 while (sp->multimapped || sp->parent_pte) {
1350 if (!sp->multimapped)
1351 parent_pte = sp->parent_pte;
1353 struct kvm_pte_chain *chain;
1355 chain = container_of(sp->parent_ptes.first,
1356 struct kvm_pte_chain, link);
1357 parent_pte = chain->parent_ptes[0];
1359 BUG_ON(!parent_pte);
1360 kvm_mmu_put_page(sp, parent_pte);
1361 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1365 static int mmu_zap_unsync_children(struct kvm *kvm,
1366 struct kvm_mmu_page *parent)
1369 struct mmu_page_path parents;
1370 struct kvm_mmu_pages pages;
1372 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1375 kvm_mmu_pages_init(parent, &parents, &pages);
1376 while (mmu_unsync_walk(parent, &pages)) {
1377 struct kvm_mmu_page *sp;
1379 for_each_sp(pages, sp, parents, i) {
1380 kvm_mmu_zap_page(kvm, sp);
1381 mmu_pages_clear_parents(&parents);
1384 kvm_mmu_pages_init(parent, &parents, &pages);
1390 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1393 ++kvm->stat.mmu_shadow_zapped;
1394 ret = mmu_zap_unsync_children(kvm, sp);
1395 kvm_mmu_page_unlink_children(kvm, sp);
1396 kvm_mmu_unlink_parents(kvm, sp);
1397 kvm_flush_remote_tlbs(kvm);
1398 if (!sp->role.invalid && !sp->role.direct)
1399 unaccount_shadowed(kvm, sp->gfn);
1401 kvm_unlink_unsync_page(kvm, sp);
1402 if (!sp->root_count) {
1403 hlist_del(&sp->hash_link);
1404 kvm_mmu_free_page(kvm, sp);
1406 sp->role.invalid = 1;
1407 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1408 kvm_reload_remote_mmus(kvm);
1410 kvm_mmu_reset_last_pte_updated(kvm);
1415 * Changing the number of mmu pages allocated to the vm
1416 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1418 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1421 * If we set the number of mmu pages to be smaller be than the
1422 * number of actived pages , we must to free some mmu pages before we
1426 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
1428 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1429 - kvm->arch.n_free_mmu_pages;
1431 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1432 struct kvm_mmu_page *page;
1434 page = container_of(kvm->arch.active_mmu_pages.prev,
1435 struct kvm_mmu_page, link);
1436 kvm_mmu_zap_page(kvm, page);
1439 kvm->arch.n_free_mmu_pages = 0;
1442 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1443 - kvm->arch.n_alloc_mmu_pages;
1445 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1448 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1451 struct hlist_head *bucket;
1452 struct kvm_mmu_page *sp;
1453 struct hlist_node *node, *n;
1456 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1458 index = kvm_page_table_hashfn(gfn);
1459 bucket = &kvm->arch.mmu_page_hash[index];
1460 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1461 if (sp->gfn == gfn && !sp->role.direct) {
1462 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1465 if (kvm_mmu_zap_page(kvm, sp))
1471 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1474 struct hlist_head *bucket;
1475 struct kvm_mmu_page *sp;
1476 struct hlist_node *node, *nn;
1478 index = kvm_page_table_hashfn(gfn);
1479 bucket = &kvm->arch.mmu_page_hash[index];
1480 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1481 if (sp->gfn == gfn && !sp->role.direct
1482 && !sp->role.invalid) {
1483 pgprintk("%s: zap %lx %x\n",
1484 __func__, gfn, sp->role.word);
1485 kvm_mmu_zap_page(kvm, sp);
1490 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1492 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1493 struct kvm_mmu_page *sp = page_header(__pa(pte));
1495 __set_bit(slot, sp->slot_bitmap);
1498 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1503 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1506 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1507 if (pt[i] == shadow_notrap_nonpresent_pte)
1508 set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte);
1512 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1516 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1518 if (gpa == UNMAPPED_GVA)
1521 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1527 * The function is based on mtrr_type_lookup() in
1528 * arch/x86/kernel/cpu/mtrr/generic.c
1530 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1535 u8 prev_match, curr_match;
1536 int num_var_ranges = KVM_NR_VAR_MTRR;
1538 if (!mtrr_state->enabled)
1541 /* Make end inclusive end, instead of exclusive */
1544 /* Look in fixed ranges. Just return the type as per start */
1545 if (mtrr_state->have_fixed && (start < 0x100000)) {
1548 if (start < 0x80000) {
1550 idx += (start >> 16);
1551 return mtrr_state->fixed_ranges[idx];
1552 } else if (start < 0xC0000) {
1554 idx += ((start - 0x80000) >> 14);
1555 return mtrr_state->fixed_ranges[idx];
1556 } else if (start < 0x1000000) {
1558 idx += ((start - 0xC0000) >> 12);
1559 return mtrr_state->fixed_ranges[idx];
1564 * Look in variable ranges
1565 * Look of multiple ranges matching this address and pick type
1566 * as per MTRR precedence
1568 if (!(mtrr_state->enabled & 2))
1569 return mtrr_state->def_type;
1572 for (i = 0; i < num_var_ranges; ++i) {
1573 unsigned short start_state, end_state;
1575 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1578 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1579 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1580 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1581 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1583 start_state = ((start & mask) == (base & mask));
1584 end_state = ((end & mask) == (base & mask));
1585 if (start_state != end_state)
1588 if ((start & mask) != (base & mask))
1591 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1592 if (prev_match == 0xFF) {
1593 prev_match = curr_match;
1597 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1598 curr_match == MTRR_TYPE_UNCACHABLE)
1599 return MTRR_TYPE_UNCACHABLE;
1601 if ((prev_match == MTRR_TYPE_WRBACK &&
1602 curr_match == MTRR_TYPE_WRTHROUGH) ||
1603 (prev_match == MTRR_TYPE_WRTHROUGH &&
1604 curr_match == MTRR_TYPE_WRBACK)) {
1605 prev_match = MTRR_TYPE_WRTHROUGH;
1606 curr_match = MTRR_TYPE_WRTHROUGH;
1609 if (prev_match != curr_match)
1610 return MTRR_TYPE_UNCACHABLE;
1613 if (prev_match != 0xFF)
1616 return mtrr_state->def_type;
1619 static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1623 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1624 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1625 if (mtrr == 0xfe || mtrr == 0xff)
1626 mtrr = MTRR_TYPE_WRBACK;
1630 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1633 struct hlist_head *bucket;
1634 struct kvm_mmu_page *s;
1635 struct hlist_node *node, *n;
1637 index = kvm_page_table_hashfn(sp->gfn);
1638 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1639 /* don't unsync if pagetable is shadowed with multiple roles */
1640 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1641 if (s->gfn != sp->gfn || s->role.direct)
1643 if (s->role.word != sp->role.word)
1646 ++vcpu->kvm->stat.mmu_unsync;
1650 list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages);
1651 ++vcpu->kvm->stat.mmu_unsync_global;
1653 kvm_mmu_mark_parents_unsync(vcpu, sp);
1655 mmu_convert_notrap(sp);
1659 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1662 struct kvm_mmu_page *shadow;
1664 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1666 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1670 if (can_unsync && oos_shadow)
1671 return kvm_unsync_page(vcpu, shadow);
1677 static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1678 unsigned pte_access, int user_fault,
1679 int write_fault, int dirty, int largepage,
1680 int global, gfn_t gfn, pfn_t pfn, bool speculative,
1685 u64 mt_mask = shadow_mt_mask;
1686 struct kvm_mmu_page *sp = page_header(__pa(shadow_pte));
1688 if (!global && sp->global) {
1691 kvm_unlink_unsync_global(vcpu->kvm, sp);
1692 kvm_mmu_mark_parents_unsync(vcpu, sp);
1697 * We don't set the accessed bit, since we sometimes want to see
1698 * whether the guest actually used the pte (in order to detect
1701 spte = shadow_base_present_pte | shadow_dirty_mask;
1703 spte |= shadow_accessed_mask;
1705 pte_access &= ~ACC_WRITE_MASK;
1706 if (pte_access & ACC_EXEC_MASK)
1707 spte |= shadow_x_mask;
1709 spte |= shadow_nx_mask;
1710 if (pte_access & ACC_USER_MASK)
1711 spte |= shadow_user_mask;
1713 spte |= PT_PAGE_SIZE_MASK;
1715 if (!kvm_is_mmio_pfn(pfn)) {
1716 mt_mask = get_memory_type(vcpu, gfn) <<
1717 kvm_x86_ops->get_mt_mask_shift();
1718 mt_mask |= VMX_EPT_IGMT_BIT;
1720 mt_mask = MTRR_TYPE_UNCACHABLE <<
1721 kvm_x86_ops->get_mt_mask_shift();
1725 spte |= (u64)pfn << PAGE_SHIFT;
1727 if ((pte_access & ACC_WRITE_MASK)
1728 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1730 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1732 spte = shadow_trap_nonpresent_pte;
1736 spte |= PT_WRITABLE_MASK;
1739 * Optimization: for pte sync, if spte was writable the hash
1740 * lookup is unnecessary (and expensive). Write protection
1741 * is responsibility of mmu_get_page / kvm_sync_page.
1742 * Same reasoning can be applied to dirty page accounting.
1744 if (!can_unsync && is_writeble_pte(*shadow_pte))
1747 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1748 pgprintk("%s: found shadow page for %lx, marking ro\n",
1751 pte_access &= ~ACC_WRITE_MASK;
1752 if (is_writeble_pte(spte))
1753 spte &= ~PT_WRITABLE_MASK;
1757 if (pte_access & ACC_WRITE_MASK)
1758 mark_page_dirty(vcpu->kvm, gfn);
1761 set_shadow_pte(shadow_pte, spte);
1765 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1766 unsigned pt_access, unsigned pte_access,
1767 int user_fault, int write_fault, int dirty,
1768 int *ptwrite, int largepage, int global,
1769 gfn_t gfn, pfn_t pfn, bool speculative)
1771 int was_rmapped = 0;
1772 int was_writeble = is_writeble_pte(*shadow_pte);
1774 pgprintk("%s: spte %llx access %x write_fault %d"
1775 " user_fault %d gfn %lx\n",
1776 __func__, *shadow_pte, pt_access,
1777 write_fault, user_fault, gfn);
1779 if (is_rmap_pte(*shadow_pte)) {
1781 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1782 * the parent of the now unreachable PTE.
1784 if (largepage && !is_large_pte(*shadow_pte)) {
1785 struct kvm_mmu_page *child;
1786 u64 pte = *shadow_pte;
1788 child = page_header(pte & PT64_BASE_ADDR_MASK);
1789 mmu_page_remove_parent_pte(child, shadow_pte);
1790 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1791 pgprintk("hfn old %lx new %lx\n",
1792 spte_to_pfn(*shadow_pte), pfn);
1793 rmap_remove(vcpu->kvm, shadow_pte);
1797 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
1798 dirty, largepage, global, gfn, pfn, speculative, true)) {
1801 kvm_x86_ops->tlb_flush(vcpu);
1804 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1805 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1806 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1807 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1808 *shadow_pte, shadow_pte);
1809 if (!was_rmapped && is_large_pte(*shadow_pte))
1810 ++vcpu->kvm->stat.lpages;
1812 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1814 rmap_add(vcpu, shadow_pte, gfn, largepage);
1815 if (!is_rmap_pte(*shadow_pte))
1816 kvm_release_pfn_clean(pfn);
1819 kvm_release_pfn_dirty(pfn);
1821 kvm_release_pfn_clean(pfn);
1824 vcpu->arch.last_pte_updated = shadow_pte;
1825 vcpu->arch.last_pte_gfn = gfn;
1829 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1833 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1834 int largepage, gfn_t gfn, pfn_t pfn)
1836 struct kvm_shadow_walk_iterator iterator;
1837 struct kvm_mmu_page *sp;
1841 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1842 if (iterator.level == PT_PAGE_TABLE_LEVEL
1843 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1844 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1845 0, write, 1, &pt_write,
1846 largepage, 0, gfn, pfn, false);
1847 ++vcpu->stat.pf_fixed;
1851 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1852 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1853 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1855 1, ACC_ALL, iterator.sptep);
1857 pgprintk("nonpaging_map: ENOMEM\n");
1858 kvm_release_pfn_clean(pfn);
1862 set_shadow_pte(iterator.sptep,
1864 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1865 | shadow_user_mask | shadow_x_mask);
1871 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1876 unsigned long mmu_seq;
1878 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1879 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1883 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1885 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1888 if (is_error_pfn(pfn)) {
1889 kvm_release_pfn_clean(pfn);
1893 spin_lock(&vcpu->kvm->mmu_lock);
1894 if (mmu_notifier_retry(vcpu, mmu_seq))
1896 kvm_mmu_free_some_pages(vcpu);
1897 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1898 spin_unlock(&vcpu->kvm->mmu_lock);
1904 spin_unlock(&vcpu->kvm->mmu_lock);
1905 kvm_release_pfn_clean(pfn);
1910 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1913 struct kvm_mmu_page *sp;
1915 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1917 spin_lock(&vcpu->kvm->mmu_lock);
1918 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1919 hpa_t root = vcpu->arch.mmu.root_hpa;
1921 sp = page_header(root);
1923 if (!sp->root_count && sp->role.invalid)
1924 kvm_mmu_zap_page(vcpu->kvm, sp);
1925 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1926 spin_unlock(&vcpu->kvm->mmu_lock);
1929 for (i = 0; i < 4; ++i) {
1930 hpa_t root = vcpu->arch.mmu.pae_root[i];
1933 root &= PT64_BASE_ADDR_MASK;
1934 sp = page_header(root);
1936 if (!sp->root_count && sp->role.invalid)
1937 kvm_mmu_zap_page(vcpu->kvm, sp);
1939 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1941 spin_unlock(&vcpu->kvm->mmu_lock);
1942 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1945 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1949 struct kvm_mmu_page *sp;
1952 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1954 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1955 hpa_t root = vcpu->arch.mmu.root_hpa;
1957 ASSERT(!VALID_PAGE(root));
1960 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1961 PT64_ROOT_LEVEL, direct,
1963 root = __pa(sp->spt);
1965 vcpu->arch.mmu.root_hpa = root;
1968 direct = !is_paging(vcpu);
1971 for (i = 0; i < 4; ++i) {
1972 hpa_t root = vcpu->arch.mmu.pae_root[i];
1974 ASSERT(!VALID_PAGE(root));
1975 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1976 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1977 vcpu->arch.mmu.pae_root[i] = 0;
1980 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1981 } else if (vcpu->arch.mmu.root_level == 0)
1983 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1984 PT32_ROOT_LEVEL, direct,
1986 root = __pa(sp->spt);
1988 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1990 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1993 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1996 struct kvm_mmu_page *sp;
1998 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2000 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2001 hpa_t root = vcpu->arch.mmu.root_hpa;
2002 sp = page_header(root);
2003 mmu_sync_children(vcpu, sp);
2006 for (i = 0; i < 4; ++i) {
2007 hpa_t root = vcpu->arch.mmu.pae_root[i];
2010 root &= PT64_BASE_ADDR_MASK;
2011 sp = page_header(root);
2012 mmu_sync_children(vcpu, sp);
2017 static void mmu_sync_global(struct kvm_vcpu *vcpu)
2019 struct kvm *kvm = vcpu->kvm;
2020 struct kvm_mmu_page *sp, *n;
2022 list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link)
2023 kvm_sync_page(vcpu, sp);
2026 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2028 spin_lock(&vcpu->kvm->mmu_lock);
2029 mmu_sync_roots(vcpu);
2030 spin_unlock(&vcpu->kvm->mmu_lock);
2033 void kvm_mmu_sync_global(struct kvm_vcpu *vcpu)
2035 spin_lock(&vcpu->kvm->mmu_lock);
2036 mmu_sync_global(vcpu);
2037 spin_unlock(&vcpu->kvm->mmu_lock);
2040 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2045 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2051 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2052 r = mmu_topup_memory_caches(vcpu);
2057 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2059 gfn = gva >> PAGE_SHIFT;
2061 return nonpaging_map(vcpu, gva & PAGE_MASK,
2062 error_code & PFERR_WRITE_MASK, gfn);
2065 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2071 gfn_t gfn = gpa >> PAGE_SHIFT;
2072 unsigned long mmu_seq;
2075 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2077 r = mmu_topup_memory_caches(vcpu);
2081 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2082 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2085 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2087 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2088 if (is_error_pfn(pfn)) {
2089 kvm_release_pfn_clean(pfn);
2092 spin_lock(&vcpu->kvm->mmu_lock);
2093 if (mmu_notifier_retry(vcpu, mmu_seq))
2095 kvm_mmu_free_some_pages(vcpu);
2096 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2097 largepage, gfn, pfn);
2098 spin_unlock(&vcpu->kvm->mmu_lock);
2103 spin_unlock(&vcpu->kvm->mmu_lock);
2104 kvm_release_pfn_clean(pfn);
2108 static void nonpaging_free(struct kvm_vcpu *vcpu)
2110 mmu_free_roots(vcpu);
2113 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2115 struct kvm_mmu *context = &vcpu->arch.mmu;
2117 context->new_cr3 = nonpaging_new_cr3;
2118 context->page_fault = nonpaging_page_fault;
2119 context->gva_to_gpa = nonpaging_gva_to_gpa;
2120 context->free = nonpaging_free;
2121 context->prefetch_page = nonpaging_prefetch_page;
2122 context->sync_page = nonpaging_sync_page;
2123 context->invlpg = nonpaging_invlpg;
2124 context->root_level = 0;
2125 context->shadow_root_level = PT32E_ROOT_LEVEL;
2126 context->root_hpa = INVALID_PAGE;
2130 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2132 ++vcpu->stat.tlb_flush;
2133 kvm_x86_ops->tlb_flush(vcpu);
2136 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2138 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2139 mmu_free_roots(vcpu);
2142 static void inject_page_fault(struct kvm_vcpu *vcpu,
2146 kvm_inject_page_fault(vcpu, addr, err_code);
2149 static void paging_free(struct kvm_vcpu *vcpu)
2151 nonpaging_free(vcpu);
2155 #include "paging_tmpl.h"
2159 #include "paging_tmpl.h"
2162 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2164 struct kvm_mmu *context = &vcpu->arch.mmu;
2166 ASSERT(is_pae(vcpu));
2167 context->new_cr3 = paging_new_cr3;
2168 context->page_fault = paging64_page_fault;
2169 context->gva_to_gpa = paging64_gva_to_gpa;
2170 context->prefetch_page = paging64_prefetch_page;
2171 context->sync_page = paging64_sync_page;
2172 context->invlpg = paging64_invlpg;
2173 context->free = paging_free;
2174 context->root_level = level;
2175 context->shadow_root_level = level;
2176 context->root_hpa = INVALID_PAGE;
2180 static int paging64_init_context(struct kvm_vcpu *vcpu)
2182 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2185 static int paging32_init_context(struct kvm_vcpu *vcpu)
2187 struct kvm_mmu *context = &vcpu->arch.mmu;
2189 context->new_cr3 = paging_new_cr3;
2190 context->page_fault = paging32_page_fault;
2191 context->gva_to_gpa = paging32_gva_to_gpa;
2192 context->free = paging_free;
2193 context->prefetch_page = paging32_prefetch_page;
2194 context->sync_page = paging32_sync_page;
2195 context->invlpg = paging32_invlpg;
2196 context->root_level = PT32_ROOT_LEVEL;
2197 context->shadow_root_level = PT32E_ROOT_LEVEL;
2198 context->root_hpa = INVALID_PAGE;
2202 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2204 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2207 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2209 struct kvm_mmu *context = &vcpu->arch.mmu;
2211 context->new_cr3 = nonpaging_new_cr3;
2212 context->page_fault = tdp_page_fault;
2213 context->free = nonpaging_free;
2214 context->prefetch_page = nonpaging_prefetch_page;
2215 context->sync_page = nonpaging_sync_page;
2216 context->invlpg = nonpaging_invlpg;
2217 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2218 context->root_hpa = INVALID_PAGE;
2220 if (!is_paging(vcpu)) {
2221 context->gva_to_gpa = nonpaging_gva_to_gpa;
2222 context->root_level = 0;
2223 } else if (is_long_mode(vcpu)) {
2224 context->gva_to_gpa = paging64_gva_to_gpa;
2225 context->root_level = PT64_ROOT_LEVEL;
2226 } else if (is_pae(vcpu)) {
2227 context->gva_to_gpa = paging64_gva_to_gpa;
2228 context->root_level = PT32E_ROOT_LEVEL;
2230 context->gva_to_gpa = paging32_gva_to_gpa;
2231 context->root_level = PT32_ROOT_LEVEL;
2237 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2242 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2244 if (!is_paging(vcpu))
2245 r = nonpaging_init_context(vcpu);
2246 else if (is_long_mode(vcpu))
2247 r = paging64_init_context(vcpu);
2248 else if (is_pae(vcpu))
2249 r = paging32E_init_context(vcpu);
2251 r = paging32_init_context(vcpu);
2253 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2258 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2260 vcpu->arch.update_pte.pfn = bad_pfn;
2263 return init_kvm_tdp_mmu(vcpu);
2265 return init_kvm_softmmu(vcpu);
2268 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2271 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2272 vcpu->arch.mmu.free(vcpu);
2273 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2277 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2279 destroy_kvm_mmu(vcpu);
2280 return init_kvm_mmu(vcpu);
2282 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2284 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2288 r = mmu_topup_memory_caches(vcpu);
2291 spin_lock(&vcpu->kvm->mmu_lock);
2292 kvm_mmu_free_some_pages(vcpu);
2293 mmu_alloc_roots(vcpu);
2294 mmu_sync_roots(vcpu);
2295 spin_unlock(&vcpu->kvm->mmu_lock);
2296 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2297 kvm_mmu_flush_tlb(vcpu);
2301 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2303 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2305 mmu_free_roots(vcpu);
2308 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2309 struct kvm_mmu_page *sp,
2313 struct kvm_mmu_page *child;
2316 if (is_shadow_present_pte(pte)) {
2317 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
2319 rmap_remove(vcpu->kvm, spte);
2321 child = page_header(pte & PT64_BASE_ADDR_MASK);
2322 mmu_page_remove_parent_pte(child, spte);
2325 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
2326 if (is_large_pte(pte))
2327 --vcpu->kvm->stat.lpages;
2330 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2331 struct kvm_mmu_page *sp,
2335 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2336 if (!vcpu->arch.update_pte.largepage ||
2337 sp->role.glevels == PT32_ROOT_LEVEL) {
2338 ++vcpu->kvm->stat.mmu_pde_zapped;
2343 ++vcpu->kvm->stat.mmu_pte_updated;
2344 if (sp->role.glevels == PT32_ROOT_LEVEL)
2345 paging32_update_pte(vcpu, sp, spte, new);
2347 paging64_update_pte(vcpu, sp, spte, new);
2350 static bool need_remote_flush(u64 old, u64 new)
2352 if (!is_shadow_present_pte(old))
2354 if (!is_shadow_present_pte(new))
2356 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2358 old ^= PT64_NX_MASK;
2359 new ^= PT64_NX_MASK;
2360 return (old & ~new & PT64_PERM_MASK) != 0;
2363 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2365 if (need_remote_flush(old, new))
2366 kvm_flush_remote_tlbs(vcpu->kvm);
2368 kvm_mmu_flush_tlb(vcpu);
2371 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2373 u64 *spte = vcpu->arch.last_pte_updated;
2375 return !!(spte && (*spte & shadow_accessed_mask));
2378 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2379 const u8 *new, int bytes)
2386 vcpu->arch.update_pte.largepage = 0;
2388 if (bytes != 4 && bytes != 8)
2392 * Assume that the pte write on a page table of the same type
2393 * as the current vcpu paging mode. This is nearly always true
2394 * (might be false while changing modes). Note it is verified later
2398 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2399 if ((bytes == 4) && (gpa % 4 == 0)) {
2400 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2403 memcpy((void *)&gpte + (gpa % 8), new, 4);
2404 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2405 memcpy((void *)&gpte, new, 8);
2408 if ((bytes == 4) && (gpa % 4 == 0))
2409 memcpy((void *)&gpte, new, 4);
2411 if (!is_present_pte(gpte))
2413 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2415 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2416 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2417 vcpu->arch.update_pte.largepage = 1;
2419 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2421 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2423 if (is_error_pfn(pfn)) {
2424 kvm_release_pfn_clean(pfn);
2427 vcpu->arch.update_pte.gfn = gfn;
2428 vcpu->arch.update_pte.pfn = pfn;
2431 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2433 u64 *spte = vcpu->arch.last_pte_updated;
2436 && vcpu->arch.last_pte_gfn == gfn
2437 && shadow_accessed_mask
2438 && !(*spte & shadow_accessed_mask)
2439 && is_shadow_present_pte(*spte))
2440 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2443 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2444 const u8 *new, int bytes,
2445 bool guest_initiated)
2447 gfn_t gfn = gpa >> PAGE_SHIFT;
2448 struct kvm_mmu_page *sp;
2449 struct hlist_node *node, *n;
2450 struct hlist_head *bucket;
2454 unsigned offset = offset_in_page(gpa);
2456 unsigned page_offset;
2457 unsigned misaligned;
2464 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2465 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
2466 spin_lock(&vcpu->kvm->mmu_lock);
2467 kvm_mmu_access_page(vcpu, gfn);
2468 kvm_mmu_free_some_pages(vcpu);
2469 ++vcpu->kvm->stat.mmu_pte_write;
2470 kvm_mmu_audit(vcpu, "pre pte write");
2471 if (guest_initiated) {
2472 if (gfn == vcpu->arch.last_pt_write_gfn
2473 && !last_updated_pte_accessed(vcpu)) {
2474 ++vcpu->arch.last_pt_write_count;
2475 if (vcpu->arch.last_pt_write_count >= 3)
2478 vcpu->arch.last_pt_write_gfn = gfn;
2479 vcpu->arch.last_pt_write_count = 1;
2480 vcpu->arch.last_pte_updated = NULL;
2483 index = kvm_page_table_hashfn(gfn);
2484 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2485 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2486 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2488 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
2489 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2490 misaligned |= bytes < 4;
2491 if (misaligned || flooded) {
2493 * Misaligned accesses are too much trouble to fix
2494 * up; also, they usually indicate a page is not used
2497 * If we're seeing too many writes to a page,
2498 * it may no longer be a page table, or we may be
2499 * forking, in which case it is better to unmap the
2502 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2503 gpa, bytes, sp->role.word);
2504 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2506 ++vcpu->kvm->stat.mmu_flooded;
2509 page_offset = offset;
2510 level = sp->role.level;
2512 if (sp->role.glevels == PT32_ROOT_LEVEL) {
2513 page_offset <<= 1; /* 32->64 */
2515 * A 32-bit pde maps 4MB while the shadow pdes map
2516 * only 2MB. So we need to double the offset again
2517 * and zap two pdes instead of one.
2519 if (level == PT32_ROOT_LEVEL) {
2520 page_offset &= ~7; /* kill rounding error */
2524 quadrant = page_offset >> PAGE_SHIFT;
2525 page_offset &= ~PAGE_MASK;
2526 if (quadrant != sp->role.quadrant)
2529 spte = &sp->spt[page_offset / sizeof(*spte)];
2530 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2532 r = kvm_read_guest_atomic(vcpu->kvm,
2533 gpa & ~(u64)(pte_size - 1),
2535 new = (const void *)&gentry;
2541 mmu_pte_write_zap_pte(vcpu, sp, spte);
2543 mmu_pte_write_new_pte(vcpu, sp, spte, new);
2544 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2548 kvm_mmu_audit(vcpu, "post pte write");
2549 spin_unlock(&vcpu->kvm->mmu_lock);
2550 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2551 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2552 vcpu->arch.update_pte.pfn = bad_pfn;
2556 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2561 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
2563 spin_lock(&vcpu->kvm->mmu_lock);
2564 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2565 spin_unlock(&vcpu->kvm->mmu_lock);
2568 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2570 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2572 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
2573 struct kvm_mmu_page *sp;
2575 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2576 struct kvm_mmu_page, link);
2577 kvm_mmu_zap_page(vcpu->kvm, sp);
2578 ++vcpu->kvm->stat.mmu_recycled;
2582 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2585 enum emulation_result er;
2587 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2596 r = mmu_topup_memory_caches(vcpu);
2600 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2605 case EMULATE_DO_MMIO:
2606 ++vcpu->stat.mmio_exits;
2609 kvm_report_emulation_failure(vcpu, "pagetable");
2617 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2619 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2621 vcpu->arch.mmu.invlpg(vcpu, gva);
2622 kvm_mmu_flush_tlb(vcpu);
2623 ++vcpu->stat.invlpg;
2625 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2627 void kvm_enable_tdp(void)
2631 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2633 void kvm_disable_tdp(void)
2635 tdp_enabled = false;
2637 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2639 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2641 struct kvm_mmu_page *sp;
2643 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2644 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
2645 struct kvm_mmu_page, link);
2646 kvm_mmu_zap_page(vcpu->kvm, sp);
2649 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2652 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2659 if (vcpu->kvm->arch.n_requested_mmu_pages)
2660 vcpu->kvm->arch.n_free_mmu_pages =
2661 vcpu->kvm->arch.n_requested_mmu_pages;
2663 vcpu->kvm->arch.n_free_mmu_pages =
2664 vcpu->kvm->arch.n_alloc_mmu_pages;
2666 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2667 * Therefore we need to allocate shadow page tables in the first
2668 * 4GB of memory, which happens to fit the DMA32 zone.
2670 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2673 vcpu->arch.mmu.pae_root = page_address(page);
2674 for (i = 0; i < 4; ++i)
2675 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2680 free_mmu_pages(vcpu);
2684 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2687 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2689 return alloc_mmu_pages(vcpu);
2692 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2695 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2697 return init_kvm_mmu(vcpu);
2700 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2704 destroy_kvm_mmu(vcpu);
2705 free_mmu_pages(vcpu);
2706 mmu_free_memory_caches(vcpu);
2709 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2711 struct kvm_mmu_page *sp;
2713 spin_lock(&kvm->mmu_lock);
2714 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2718 if (!test_bit(slot, sp->slot_bitmap))
2722 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2724 if (pt[i] & PT_WRITABLE_MASK)
2725 pt[i] &= ~PT_WRITABLE_MASK;
2727 kvm_flush_remote_tlbs(kvm);
2728 spin_unlock(&kvm->mmu_lock);
2731 void kvm_mmu_zap_all(struct kvm *kvm)
2733 struct kvm_mmu_page *sp, *node;
2735 spin_lock(&kvm->mmu_lock);
2736 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2737 if (kvm_mmu_zap_page(kvm, sp))
2738 node = container_of(kvm->arch.active_mmu_pages.next,
2739 struct kvm_mmu_page, link);
2740 spin_unlock(&kvm->mmu_lock);
2742 kvm_flush_remote_tlbs(kvm);
2745 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2747 struct kvm_mmu_page *page;
2749 page = container_of(kvm->arch.active_mmu_pages.prev,
2750 struct kvm_mmu_page, link);
2751 kvm_mmu_zap_page(kvm, page);
2754 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2757 struct kvm *kvm_freed = NULL;
2758 int cache_count = 0;
2760 spin_lock(&kvm_lock);
2762 list_for_each_entry(kvm, &vm_list, vm_list) {
2765 if (!down_read_trylock(&kvm->slots_lock))
2767 spin_lock(&kvm->mmu_lock);
2768 npages = kvm->arch.n_alloc_mmu_pages -
2769 kvm->arch.n_free_mmu_pages;
2770 cache_count += npages;
2771 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2772 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2778 spin_unlock(&kvm->mmu_lock);
2779 up_read(&kvm->slots_lock);
2782 list_move_tail(&kvm_freed->vm_list, &vm_list);
2784 spin_unlock(&kvm_lock);
2789 static struct shrinker mmu_shrinker = {
2790 .shrink = mmu_shrink,
2791 .seeks = DEFAULT_SEEKS * 10,
2794 static void mmu_destroy_caches(void)
2796 if (pte_chain_cache)
2797 kmem_cache_destroy(pte_chain_cache);
2798 if (rmap_desc_cache)
2799 kmem_cache_destroy(rmap_desc_cache);
2800 if (mmu_page_header_cache)
2801 kmem_cache_destroy(mmu_page_header_cache);
2804 void kvm_mmu_module_exit(void)
2806 mmu_destroy_caches();
2807 unregister_shrinker(&mmu_shrinker);
2810 int kvm_mmu_module_init(void)
2812 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2813 sizeof(struct kvm_pte_chain),
2815 if (!pte_chain_cache)
2817 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2818 sizeof(struct kvm_rmap_desc),
2820 if (!rmap_desc_cache)
2823 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2824 sizeof(struct kvm_mmu_page),
2826 if (!mmu_page_header_cache)
2829 register_shrinker(&mmu_shrinker);
2834 mmu_destroy_caches();
2839 * Caculate mmu pages needed for kvm.
2841 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2844 unsigned int nr_mmu_pages;
2845 unsigned int nr_pages = 0;
2847 for (i = 0; i < kvm->nmemslots; i++)
2848 nr_pages += kvm->memslots[i].npages;
2850 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2851 nr_mmu_pages = max(nr_mmu_pages,
2852 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2854 return nr_mmu_pages;
2857 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2860 if (len > buffer->len)
2865 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2870 ret = pv_mmu_peek_buffer(buffer, len);
2875 buffer->processed += len;
2879 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2880 gpa_t addr, gpa_t value)
2885 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2888 r = mmu_topup_memory_caches(vcpu);
2892 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2898 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2900 kvm_x86_ops->tlb_flush(vcpu);
2901 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
2905 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2907 spin_lock(&vcpu->kvm->mmu_lock);
2908 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2909 spin_unlock(&vcpu->kvm->mmu_lock);
2913 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2914 struct kvm_pv_mmu_op_buffer *buffer)
2916 struct kvm_mmu_op_header *header;
2918 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2921 switch (header->op) {
2922 case KVM_MMU_OP_WRITE_PTE: {
2923 struct kvm_mmu_op_write_pte *wpte;
2925 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2928 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2931 case KVM_MMU_OP_FLUSH_TLB: {
2932 struct kvm_mmu_op_flush_tlb *ftlb;
2934 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2937 return kvm_pv_mmu_flush_tlb(vcpu);
2939 case KVM_MMU_OP_RELEASE_PT: {
2940 struct kvm_mmu_op_release_pt *rpt;
2942 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2945 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2951 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2952 gpa_t addr, unsigned long *ret)
2955 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2957 buffer->ptr = buffer->buf;
2958 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2959 buffer->processed = 0;
2961 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2965 while (buffer->len) {
2966 r = kvm_pv_mmu_op_one(vcpu, buffer);
2975 *ret = buffer->processed;
2981 static const char *audit_msg;
2983 static gva_t canonicalize(gva_t gva)
2985 #ifdef CONFIG_X86_64
2986 gva = (long long)(gva << 16) >> 16;
2991 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2992 gva_t va, int level)
2994 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2996 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2998 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3001 if (ent == shadow_trap_nonpresent_pte)
3004 va = canonicalize(va);
3006 if (ent == shadow_notrap_nonpresent_pte)
3007 printk(KERN_ERR "audit: (%s) nontrapping pte"
3008 " in nonleaf level: levels %d gva %lx"
3009 " level %d pte %llx\n", audit_msg,
3010 vcpu->arch.mmu.root_level, va, level, ent);
3012 audit_mappings_page(vcpu, ent, va, level - 1);
3014 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
3015 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
3017 if (is_shadow_present_pte(ent)
3018 && (ent & PT64_BASE_ADDR_MASK) != hpa)
3019 printk(KERN_ERR "xx audit error: (%s) levels %d"
3020 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3021 audit_msg, vcpu->arch.mmu.root_level,
3023 is_shadow_present_pte(ent));
3024 else if (ent == shadow_notrap_nonpresent_pte
3025 && !is_error_hpa(hpa))
3026 printk(KERN_ERR "audit: (%s) notrap shadow,"
3027 " valid guest gva %lx\n", audit_msg, va);
3028 kvm_release_pfn_clean(pfn);
3034 static void audit_mappings(struct kvm_vcpu *vcpu)
3038 if (vcpu->arch.mmu.root_level == 4)
3039 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3041 for (i = 0; i < 4; ++i)
3042 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3043 audit_mappings_page(vcpu,
3044 vcpu->arch.mmu.pae_root[i],
3049 static int count_rmaps(struct kvm_vcpu *vcpu)
3054 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3055 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3056 struct kvm_rmap_desc *d;
3058 for (j = 0; j < m->npages; ++j) {
3059 unsigned long *rmapp = &m->rmap[j];
3063 if (!(*rmapp & 1)) {
3067 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3069 for (k = 0; k < RMAP_EXT; ++k)
3070 if (d->shadow_ptes[k])
3081 static int count_writable_mappings(struct kvm_vcpu *vcpu)
3084 struct kvm_mmu_page *sp;
3087 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3090 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3093 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3096 if (!(ent & PT_PRESENT_MASK))
3098 if (!(ent & PT_WRITABLE_MASK))
3106 static void audit_rmap(struct kvm_vcpu *vcpu)
3108 int n_rmap = count_rmaps(vcpu);
3109 int n_actual = count_writable_mappings(vcpu);
3111 if (n_rmap != n_actual)
3112 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
3113 __func__, audit_msg, n_rmap, n_actual);
3116 static void audit_write_protection(struct kvm_vcpu *vcpu)
3118 struct kvm_mmu_page *sp;
3119 struct kvm_memory_slot *slot;
3120 unsigned long *rmapp;
3123 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3124 if (sp->role.direct)
3127 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3128 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3129 rmapp = &slot->rmap[gfn - slot->base_gfn];
3131 printk(KERN_ERR "%s: (%s) shadow page has writable"
3132 " mappings: gfn %lx role %x\n",
3133 __func__, audit_msg, sp->gfn,
3138 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3145 audit_write_protection(vcpu);
3146 audit_mappings(vcpu);