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 "kvm_cache_regs.h"
24 #include <linux/kvm_host.h>
25 #include <linux/types.h>
26 #include <linux/string.h>
28 #include <linux/highmem.h>
29 #include <linux/module.h>
30 #include <linux/swap.h>
31 #include <linux/hugetlb.h>
32 #include <linux/compiler.h>
33 #include <linux/srcu.h>
34 #include <linux/slab.h>
37 #include <asm/cmpxchg.h>
42 * When setting this variable to true it enables Two-Dimensional-Paging
43 * where the hardware walks 2 page tables:
44 * 1. the guest-virtual to guest-physical
45 * 2. while doing 1. it walks guest-physical to host-physical
46 * If the hardware supports that we don't need to do shadow paging.
48 bool tdp_enabled = false;
55 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
57 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
62 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
63 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
67 #define pgprintk(x...) do { } while (0)
68 #define rmap_printk(x...) do { } while (0)
72 #if defined(MMU_DEBUG) || defined(AUDIT)
74 module_param(dbg, bool, 0644);
77 static int oos_shadow = 1;
78 module_param(oos_shadow, bool, 0644);
81 #define ASSERT(x) do { } while (0)
85 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
86 __FILE__, __LINE__, #x); \
90 #define PT_FIRST_AVAIL_BITS_SHIFT 9
91 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
93 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
95 #define PT64_LEVEL_BITS 9
97 #define PT64_LEVEL_SHIFT(level) \
98 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
100 #define PT64_LEVEL_MASK(level) \
101 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
103 #define PT64_INDEX(address, level)\
104 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
107 #define PT32_LEVEL_BITS 10
109 #define PT32_LEVEL_SHIFT(level) \
110 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
112 #define PT32_LEVEL_MASK(level) \
113 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
114 #define PT32_LVL_OFFSET_MASK(level) \
115 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
116 * PT32_LEVEL_BITS))) - 1))
118 #define PT32_INDEX(address, level)\
119 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
122 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
123 #define PT64_DIR_BASE_ADDR_MASK \
124 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
125 #define PT64_LVL_ADDR_MASK(level) \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
127 * PT64_LEVEL_BITS))) - 1))
128 #define PT64_LVL_OFFSET_MASK(level) \
129 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT64_LEVEL_BITS))) - 1))
132 #define PT32_BASE_ADDR_MASK PAGE_MASK
133 #define PT32_DIR_BASE_ADDR_MASK \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
135 #define PT32_LVL_ADDR_MASK(level) \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
137 * PT32_LEVEL_BITS))) - 1))
139 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
144 #define ACC_EXEC_MASK 1
145 #define ACC_WRITE_MASK PT_WRITABLE_MASK
146 #define ACC_USER_MASK PT_USER_MASK
147 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
149 #include <trace/events/kvm.h>
151 #define CREATE_TRACE_POINTS
152 #include "mmutrace.h"
154 #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
156 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
158 struct kvm_rmap_desc {
159 u64 *sptes[RMAP_EXT];
160 struct kvm_rmap_desc *more;
163 struct kvm_shadow_walk_iterator {
171 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
172 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
173 shadow_walk_okay(&(_walker)); \
174 shadow_walk_next(&(_walker)))
176 typedef int (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp);
178 static struct kmem_cache *pte_chain_cache;
179 static struct kmem_cache *rmap_desc_cache;
180 static struct kmem_cache *mmu_page_header_cache;
182 static u64 __read_mostly shadow_trap_nonpresent_pte;
183 static u64 __read_mostly shadow_notrap_nonpresent_pte;
184 static u64 __read_mostly shadow_base_present_pte;
185 static u64 __read_mostly shadow_nx_mask;
186 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
187 static u64 __read_mostly shadow_user_mask;
188 static u64 __read_mostly shadow_accessed_mask;
189 static u64 __read_mostly shadow_dirty_mask;
191 static inline u64 rsvd_bits(int s, int e)
193 return ((1ULL << (e - s + 1)) - 1) << s;
196 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
198 shadow_trap_nonpresent_pte = trap_pte;
199 shadow_notrap_nonpresent_pte = notrap_pte;
201 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
203 void kvm_mmu_set_base_ptes(u64 base_pte)
205 shadow_base_present_pte = base_pte;
207 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
209 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
210 u64 dirty_mask, u64 nx_mask, u64 x_mask)
212 shadow_user_mask = user_mask;
213 shadow_accessed_mask = accessed_mask;
214 shadow_dirty_mask = dirty_mask;
215 shadow_nx_mask = nx_mask;
216 shadow_x_mask = x_mask;
218 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
220 static int is_write_protection(struct kvm_vcpu *vcpu)
222 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
225 static int is_cpuid_PSE36(void)
230 static int is_nx(struct kvm_vcpu *vcpu)
232 return vcpu->arch.efer & EFER_NX;
235 static int is_shadow_present_pte(u64 pte)
237 return pte != shadow_trap_nonpresent_pte
238 && pte != shadow_notrap_nonpresent_pte;
241 static int is_large_pte(u64 pte)
243 return pte & PT_PAGE_SIZE_MASK;
246 static int is_writable_pte(unsigned long pte)
248 return pte & PT_WRITABLE_MASK;
251 static int is_dirty_gpte(unsigned long pte)
253 return pte & PT_DIRTY_MASK;
256 static int is_rmap_spte(u64 pte)
258 return is_shadow_present_pte(pte);
261 static int is_last_spte(u64 pte, int level)
263 if (level == PT_PAGE_TABLE_LEVEL)
265 if (is_large_pte(pte))
270 static pfn_t spte_to_pfn(u64 pte)
272 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
275 static gfn_t pse36_gfn_delta(u32 gpte)
277 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
279 return (gpte & PT32_DIR_PSE36_MASK) << shift;
282 static void __set_spte(u64 *sptep, u64 spte)
285 set_64bit((unsigned long *)sptep, spte);
287 set_64bit((unsigned long long *)sptep, spte);
291 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
292 struct kmem_cache *base_cache, int min)
296 if (cache->nobjs >= min)
298 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
299 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
302 cache->objects[cache->nobjs++] = obj;
307 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
310 kfree(mc->objects[--mc->nobjs]);
313 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
318 if (cache->nobjs >= min)
320 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
321 page = alloc_page(GFP_KERNEL);
324 cache->objects[cache->nobjs++] = page_address(page);
329 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
332 free_page((unsigned long)mc->objects[--mc->nobjs]);
335 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
339 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
343 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
347 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
350 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
351 mmu_page_header_cache, 4);
356 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
358 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
359 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
360 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
361 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
364 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
370 p = mc->objects[--mc->nobjs];
374 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
376 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
377 sizeof(struct kvm_pte_chain));
380 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
385 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
387 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
388 sizeof(struct kvm_rmap_desc));
391 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
397 * Return the pointer to the largepage write count for a given
398 * gfn, handling slots that are not large page aligned.
400 static int *slot_largepage_idx(gfn_t gfn,
401 struct kvm_memory_slot *slot,
406 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
407 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
408 return &slot->lpage_info[level - 2][idx].write_count;
411 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
413 struct kvm_memory_slot *slot;
417 gfn = unalias_gfn(kvm, gfn);
419 slot = gfn_to_memslot_unaliased(kvm, gfn);
420 for (i = PT_DIRECTORY_LEVEL;
421 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
422 write_count = slot_largepage_idx(gfn, slot, i);
427 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
429 struct kvm_memory_slot *slot;
433 gfn = unalias_gfn(kvm, gfn);
434 for (i = PT_DIRECTORY_LEVEL;
435 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
436 slot = gfn_to_memslot_unaliased(kvm, gfn);
437 write_count = slot_largepage_idx(gfn, slot, i);
439 WARN_ON(*write_count < 0);
443 static int has_wrprotected_page(struct kvm *kvm,
447 struct kvm_memory_slot *slot;
450 gfn = unalias_gfn(kvm, gfn);
451 slot = gfn_to_memslot_unaliased(kvm, gfn);
453 largepage_idx = slot_largepage_idx(gfn, slot, level);
454 return *largepage_idx;
460 static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
462 unsigned long page_size;
465 page_size = kvm_host_page_size(kvm, gfn);
467 for (i = PT_PAGE_TABLE_LEVEL;
468 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
469 if (page_size >= KVM_HPAGE_SIZE(i))
478 static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
480 struct kvm_memory_slot *slot;
481 int host_level, level, max_level;
483 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
484 if (slot && slot->dirty_bitmap)
485 return PT_PAGE_TABLE_LEVEL;
487 host_level = host_mapping_level(vcpu->kvm, large_gfn);
489 if (host_level == PT_PAGE_TABLE_LEVEL)
492 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
493 kvm_x86_ops->get_lpage_level() : host_level;
495 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
496 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
503 * Take gfn and return the reverse mapping to it.
504 * Note: gfn must be unaliased before this function get called
507 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
509 struct kvm_memory_slot *slot;
512 slot = gfn_to_memslot(kvm, gfn);
513 if (likely(level == PT_PAGE_TABLE_LEVEL))
514 return &slot->rmap[gfn - slot->base_gfn];
516 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
517 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
519 return &slot->lpage_info[level - 2][idx].rmap_pde;
523 * Reverse mapping data structures:
525 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
526 * that points to page_address(page).
528 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
529 * containing more mappings.
531 * Returns the number of rmap entries before the spte was added or zero if
532 * the spte was not added.
535 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
537 struct kvm_mmu_page *sp;
538 struct kvm_rmap_desc *desc;
539 unsigned long *rmapp;
542 if (!is_rmap_spte(*spte))
544 gfn = unalias_gfn(vcpu->kvm, gfn);
545 sp = page_header(__pa(spte));
546 sp->gfns[spte - sp->spt] = gfn;
547 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
549 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
550 *rmapp = (unsigned long)spte;
551 } else if (!(*rmapp & 1)) {
552 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
553 desc = mmu_alloc_rmap_desc(vcpu);
554 desc->sptes[0] = (u64 *)*rmapp;
555 desc->sptes[1] = spte;
556 *rmapp = (unsigned long)desc | 1;
558 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
559 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
560 while (desc->sptes[RMAP_EXT-1] && desc->more) {
564 if (desc->sptes[RMAP_EXT-1]) {
565 desc->more = mmu_alloc_rmap_desc(vcpu);
568 for (i = 0; desc->sptes[i]; ++i)
570 desc->sptes[i] = spte;
575 static void rmap_desc_remove_entry(unsigned long *rmapp,
576 struct kvm_rmap_desc *desc,
578 struct kvm_rmap_desc *prev_desc)
582 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
584 desc->sptes[i] = desc->sptes[j];
585 desc->sptes[j] = NULL;
588 if (!prev_desc && !desc->more)
589 *rmapp = (unsigned long)desc->sptes[0];
592 prev_desc->more = desc->more;
594 *rmapp = (unsigned long)desc->more | 1;
595 mmu_free_rmap_desc(desc);
598 static void rmap_remove(struct kvm *kvm, u64 *spte)
600 struct kvm_rmap_desc *desc;
601 struct kvm_rmap_desc *prev_desc;
602 struct kvm_mmu_page *sp;
604 unsigned long *rmapp;
607 if (!is_rmap_spte(*spte))
609 sp = page_header(__pa(spte));
610 pfn = spte_to_pfn(*spte);
611 if (*spte & shadow_accessed_mask)
612 kvm_set_pfn_accessed(pfn);
613 if (is_writable_pte(*spte))
614 kvm_set_pfn_dirty(pfn);
615 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
617 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
619 } else if (!(*rmapp & 1)) {
620 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
621 if ((u64 *)*rmapp != spte) {
622 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
628 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
629 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
632 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
633 if (desc->sptes[i] == spte) {
634 rmap_desc_remove_entry(rmapp,
642 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
647 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
649 struct kvm_rmap_desc *desc;
650 struct kvm_rmap_desc *prev_desc;
656 else if (!(*rmapp & 1)) {
658 return (u64 *)*rmapp;
661 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
665 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
666 if (prev_spte == spte)
667 return desc->sptes[i];
668 prev_spte = desc->sptes[i];
675 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
677 unsigned long *rmapp;
679 int i, write_protected = 0;
681 gfn = unalias_gfn(kvm, gfn);
682 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
684 spte = rmap_next(kvm, rmapp, NULL);
687 BUG_ON(!(*spte & PT_PRESENT_MASK));
688 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
689 if (is_writable_pte(*spte)) {
690 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
693 spte = rmap_next(kvm, rmapp, spte);
695 if (write_protected) {
698 spte = rmap_next(kvm, rmapp, NULL);
699 pfn = spte_to_pfn(*spte);
700 kvm_set_pfn_dirty(pfn);
703 /* check for huge page mappings */
704 for (i = PT_DIRECTORY_LEVEL;
705 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
706 rmapp = gfn_to_rmap(kvm, gfn, i);
707 spte = rmap_next(kvm, rmapp, NULL);
710 BUG_ON(!(*spte & PT_PRESENT_MASK));
711 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
712 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
713 if (is_writable_pte(*spte)) {
714 rmap_remove(kvm, spte);
716 __set_spte(spte, shadow_trap_nonpresent_pte);
720 spte = rmap_next(kvm, rmapp, spte);
724 return write_protected;
727 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
731 int need_tlb_flush = 0;
733 while ((spte = rmap_next(kvm, rmapp, NULL))) {
734 BUG_ON(!(*spte & PT_PRESENT_MASK));
735 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
736 rmap_remove(kvm, spte);
737 __set_spte(spte, shadow_trap_nonpresent_pte);
740 return need_tlb_flush;
743 static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
748 pte_t *ptep = (pte_t *)data;
751 WARN_ON(pte_huge(*ptep));
752 new_pfn = pte_pfn(*ptep);
753 spte = rmap_next(kvm, rmapp, NULL);
755 BUG_ON(!is_shadow_present_pte(*spte));
756 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
758 if (pte_write(*ptep)) {
759 rmap_remove(kvm, spte);
760 __set_spte(spte, shadow_trap_nonpresent_pte);
761 spte = rmap_next(kvm, rmapp, NULL);
763 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
764 new_spte |= (u64)new_pfn << PAGE_SHIFT;
766 new_spte &= ~PT_WRITABLE_MASK;
767 new_spte &= ~SPTE_HOST_WRITEABLE;
768 if (is_writable_pte(*spte))
769 kvm_set_pfn_dirty(spte_to_pfn(*spte));
770 __set_spte(spte, new_spte);
771 spte = rmap_next(kvm, rmapp, spte);
775 kvm_flush_remote_tlbs(kvm);
780 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
782 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
788 struct kvm_memslots *slots;
790 slots = rcu_dereference(kvm->memslots);
792 for (i = 0; i < slots->nmemslots; i++) {
793 struct kvm_memory_slot *memslot = &slots->memslots[i];
794 unsigned long start = memslot->userspace_addr;
797 end = start + (memslot->npages << PAGE_SHIFT);
798 if (hva >= start && hva < end) {
799 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
801 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
803 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
804 int idx = gfn_offset;
805 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
807 &memslot->lpage_info[j][idx].rmap_pde,
810 trace_kvm_age_page(hva, memslot, ret);
818 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
820 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
823 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
825 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
828 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
835 * Emulate the accessed bit for EPT, by checking if this page has
836 * an EPT mapping, and clearing it if it does. On the next access,
837 * a new EPT mapping will be established.
838 * This has some overhead, but not as much as the cost of swapping
839 * out actively used pages or breaking up actively used hugepages.
841 if (!shadow_accessed_mask)
842 return kvm_unmap_rmapp(kvm, rmapp, data);
844 spte = rmap_next(kvm, rmapp, NULL);
848 BUG_ON(!(_spte & PT_PRESENT_MASK));
849 _young = _spte & PT_ACCESSED_MASK;
852 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
854 spte = rmap_next(kvm, rmapp, spte);
859 #define RMAP_RECYCLE_THRESHOLD 1000
861 static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
863 unsigned long *rmapp;
864 struct kvm_mmu_page *sp;
866 sp = page_header(__pa(spte));
868 gfn = unalias_gfn(vcpu->kvm, gfn);
869 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
871 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
872 kvm_flush_remote_tlbs(vcpu->kvm);
875 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
877 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
881 static int is_empty_shadow_page(u64 *spt)
886 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
887 if (is_shadow_present_pte(*pos)) {
888 printk(KERN_ERR "%s: %p %llx\n", __func__,
896 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
898 ASSERT(is_empty_shadow_page(sp->spt));
900 __free_page(virt_to_page(sp->spt));
901 __free_page(virt_to_page(sp->gfns));
903 ++kvm->arch.n_free_mmu_pages;
906 static unsigned kvm_page_table_hashfn(gfn_t gfn)
908 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
911 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
914 struct kvm_mmu_page *sp;
916 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
917 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
918 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
919 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
920 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
921 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
923 sp->parent_pte = parent_pte;
924 --vcpu->kvm->arch.n_free_mmu_pages;
928 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
929 struct kvm_mmu_page *sp, u64 *parent_pte)
931 struct kvm_pte_chain *pte_chain;
932 struct hlist_node *node;
937 if (!sp->multimapped) {
938 u64 *old = sp->parent_pte;
941 sp->parent_pte = parent_pte;
945 pte_chain = mmu_alloc_pte_chain(vcpu);
946 INIT_HLIST_HEAD(&sp->parent_ptes);
947 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
948 pte_chain->parent_ptes[0] = old;
950 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
951 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
953 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
954 if (!pte_chain->parent_ptes[i]) {
955 pte_chain->parent_ptes[i] = parent_pte;
959 pte_chain = mmu_alloc_pte_chain(vcpu);
961 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
962 pte_chain->parent_ptes[0] = parent_pte;
965 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
968 struct kvm_pte_chain *pte_chain;
969 struct hlist_node *node;
972 if (!sp->multimapped) {
973 BUG_ON(sp->parent_pte != parent_pte);
974 sp->parent_pte = NULL;
977 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
978 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
979 if (!pte_chain->parent_ptes[i])
981 if (pte_chain->parent_ptes[i] != parent_pte)
983 while (i + 1 < NR_PTE_CHAIN_ENTRIES
984 && pte_chain->parent_ptes[i + 1]) {
985 pte_chain->parent_ptes[i]
986 = pte_chain->parent_ptes[i + 1];
989 pte_chain->parent_ptes[i] = NULL;
991 hlist_del(&pte_chain->link);
992 mmu_free_pte_chain(pte_chain);
993 if (hlist_empty(&sp->parent_ptes)) {
995 sp->parent_pte = NULL;
1004 static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
1006 struct kvm_pte_chain *pte_chain;
1007 struct hlist_node *node;
1008 struct kvm_mmu_page *parent_sp;
1011 if (!sp->multimapped && sp->parent_pte) {
1012 parent_sp = page_header(__pa(sp->parent_pte));
1014 mmu_parent_walk(parent_sp, fn);
1017 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1018 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1019 if (!pte_chain->parent_ptes[i])
1021 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1023 mmu_parent_walk(parent_sp, fn);
1027 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1030 struct kvm_mmu_page *sp = page_header(__pa(spte));
1032 index = spte - sp->spt;
1033 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1034 sp->unsync_children++;
1035 WARN_ON(!sp->unsync_children);
1038 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1040 struct kvm_pte_chain *pte_chain;
1041 struct hlist_node *node;
1044 if (!sp->parent_pte)
1047 if (!sp->multimapped) {
1048 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1052 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1053 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1054 if (!pte_chain->parent_ptes[i])
1056 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1060 static int unsync_walk_fn(struct kvm_mmu_page *sp)
1062 kvm_mmu_update_parents_unsync(sp);
1066 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
1068 mmu_parent_walk(sp, unsync_walk_fn);
1069 kvm_mmu_update_parents_unsync(sp);
1072 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1073 struct kvm_mmu_page *sp)
1077 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1078 sp->spt[i] = shadow_trap_nonpresent_pte;
1081 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1082 struct kvm_mmu_page *sp)
1087 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1091 #define KVM_PAGE_ARRAY_NR 16
1093 struct kvm_mmu_pages {
1094 struct mmu_page_and_offset {
1095 struct kvm_mmu_page *sp;
1097 } page[KVM_PAGE_ARRAY_NR];
1101 #define for_each_unsync_children(bitmap, idx) \
1102 for (idx = find_first_bit(bitmap, 512); \
1104 idx = find_next_bit(bitmap, 512, idx+1))
1106 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1112 for (i=0; i < pvec->nr; i++)
1113 if (pvec->page[i].sp == sp)
1116 pvec->page[pvec->nr].sp = sp;
1117 pvec->page[pvec->nr].idx = idx;
1119 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1122 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1123 struct kvm_mmu_pages *pvec)
1125 int i, ret, nr_unsync_leaf = 0;
1127 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1128 u64 ent = sp->spt[i];
1130 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1131 struct kvm_mmu_page *child;
1132 child = page_header(ent & PT64_BASE_ADDR_MASK);
1134 if (child->unsync_children) {
1135 if (mmu_pages_add(pvec, child, i))
1138 ret = __mmu_unsync_walk(child, pvec);
1140 __clear_bit(i, sp->unsync_child_bitmap);
1142 nr_unsync_leaf += ret;
1147 if (child->unsync) {
1149 if (mmu_pages_add(pvec, child, i))
1155 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1156 sp->unsync_children = 0;
1158 return nr_unsync_leaf;
1161 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1162 struct kvm_mmu_pages *pvec)
1164 if (!sp->unsync_children)
1167 mmu_pages_add(pvec, sp, 0);
1168 return __mmu_unsync_walk(sp, pvec);
1171 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1174 struct hlist_head *bucket;
1175 struct kvm_mmu_page *sp;
1176 struct hlist_node *node;
1178 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1179 index = kvm_page_table_hashfn(gfn);
1180 bucket = &kvm->arch.mmu_page_hash[index];
1181 hlist_for_each_entry(sp, node, bucket, hash_link)
1182 if (sp->gfn == gfn && !sp->role.direct
1183 && !sp->role.invalid) {
1184 pgprintk("%s: found role %x\n",
1185 __func__, sp->role.word);
1191 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1193 WARN_ON(!sp->unsync);
1195 --kvm->stat.mmu_unsync;
1198 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1200 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1202 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
1203 kvm_mmu_zap_page(vcpu->kvm, sp);
1207 trace_kvm_mmu_sync_page(sp);
1208 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1209 kvm_flush_remote_tlbs(vcpu->kvm);
1210 kvm_unlink_unsync_page(vcpu->kvm, sp);
1211 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1212 kvm_mmu_zap_page(vcpu->kvm, sp);
1216 kvm_mmu_flush_tlb(vcpu);
1220 struct mmu_page_path {
1221 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1222 unsigned int idx[PT64_ROOT_LEVEL-1];
1225 #define for_each_sp(pvec, sp, parents, i) \
1226 for (i = mmu_pages_next(&pvec, &parents, -1), \
1227 sp = pvec.page[i].sp; \
1228 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1229 i = mmu_pages_next(&pvec, &parents, i))
1231 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1232 struct mmu_page_path *parents,
1237 for (n = i+1; n < pvec->nr; n++) {
1238 struct kvm_mmu_page *sp = pvec->page[n].sp;
1240 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1241 parents->idx[0] = pvec->page[n].idx;
1245 parents->parent[sp->role.level-2] = sp;
1246 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1252 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1254 struct kvm_mmu_page *sp;
1255 unsigned int level = 0;
1258 unsigned int idx = parents->idx[level];
1260 sp = parents->parent[level];
1264 --sp->unsync_children;
1265 WARN_ON((int)sp->unsync_children < 0);
1266 __clear_bit(idx, sp->unsync_child_bitmap);
1268 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1271 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1272 struct mmu_page_path *parents,
1273 struct kvm_mmu_pages *pvec)
1275 parents->parent[parent->role.level-1] = NULL;
1279 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1280 struct kvm_mmu_page *parent)
1283 struct kvm_mmu_page *sp;
1284 struct mmu_page_path parents;
1285 struct kvm_mmu_pages pages;
1287 kvm_mmu_pages_init(parent, &parents, &pages);
1288 while (mmu_unsync_walk(parent, &pages)) {
1291 for_each_sp(pages, sp, parents, i)
1292 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1295 kvm_flush_remote_tlbs(vcpu->kvm);
1297 for_each_sp(pages, sp, parents, i) {
1298 kvm_sync_page(vcpu, sp);
1299 mmu_pages_clear_parents(&parents);
1301 cond_resched_lock(&vcpu->kvm->mmu_lock);
1302 kvm_mmu_pages_init(parent, &parents, &pages);
1306 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1314 union kvm_mmu_page_role role;
1317 struct hlist_head *bucket;
1318 struct kvm_mmu_page *sp;
1319 struct hlist_node *node, *tmp;
1321 role = vcpu->arch.mmu.base_role;
1323 role.direct = direct;
1326 role.access = access;
1327 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1328 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1329 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1330 role.quadrant = quadrant;
1332 index = kvm_page_table_hashfn(gfn);
1333 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1334 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1335 if (sp->gfn == gfn) {
1337 if (kvm_sync_page(vcpu, sp))
1340 if (sp->role.word != role.word)
1343 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1344 if (sp->unsync_children) {
1345 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1346 kvm_mmu_mark_parents_unsync(sp);
1348 trace_kvm_mmu_get_page(sp, false);
1351 ++vcpu->kvm->stat.mmu_cache_miss;
1352 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1357 hlist_add_head(&sp->hash_link, bucket);
1359 if (rmap_write_protect(vcpu->kvm, gfn))
1360 kvm_flush_remote_tlbs(vcpu->kvm);
1361 account_shadowed(vcpu->kvm, gfn);
1363 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1364 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1366 nonpaging_prefetch_page(vcpu, sp);
1367 trace_kvm_mmu_get_page(sp, true);
1371 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1372 struct kvm_vcpu *vcpu, u64 addr)
1374 iterator->addr = addr;
1375 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1376 iterator->level = vcpu->arch.mmu.shadow_root_level;
1377 if (iterator->level == PT32E_ROOT_LEVEL) {
1378 iterator->shadow_addr
1379 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1380 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1382 if (!iterator->shadow_addr)
1383 iterator->level = 0;
1387 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1389 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1392 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1393 if (is_large_pte(*iterator->sptep))
1396 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1397 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1401 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1403 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1407 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1408 struct kvm_mmu_page *sp)
1416 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1419 if (is_shadow_present_pte(ent)) {
1420 if (!is_last_spte(ent, sp->role.level)) {
1421 ent &= PT64_BASE_ADDR_MASK;
1422 mmu_page_remove_parent_pte(page_header(ent),
1425 if (is_large_pte(ent))
1427 rmap_remove(kvm, &pt[i]);
1430 pt[i] = shadow_trap_nonpresent_pte;
1434 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1436 mmu_page_remove_parent_pte(sp, parent_pte);
1439 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1442 struct kvm_vcpu *vcpu;
1444 kvm_for_each_vcpu(i, vcpu, kvm)
1445 vcpu->arch.last_pte_updated = NULL;
1448 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1452 while (sp->multimapped || sp->parent_pte) {
1453 if (!sp->multimapped)
1454 parent_pte = sp->parent_pte;
1456 struct kvm_pte_chain *chain;
1458 chain = container_of(sp->parent_ptes.first,
1459 struct kvm_pte_chain, link);
1460 parent_pte = chain->parent_ptes[0];
1462 BUG_ON(!parent_pte);
1463 kvm_mmu_put_page(sp, parent_pte);
1464 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1468 static int mmu_zap_unsync_children(struct kvm *kvm,
1469 struct kvm_mmu_page *parent)
1472 struct mmu_page_path parents;
1473 struct kvm_mmu_pages pages;
1475 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1478 kvm_mmu_pages_init(parent, &parents, &pages);
1479 while (mmu_unsync_walk(parent, &pages)) {
1480 struct kvm_mmu_page *sp;
1482 for_each_sp(pages, sp, parents, i) {
1483 kvm_mmu_zap_page(kvm, sp);
1484 mmu_pages_clear_parents(&parents);
1487 kvm_mmu_pages_init(parent, &parents, &pages);
1493 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1497 trace_kvm_mmu_zap_page(sp);
1498 ++kvm->stat.mmu_shadow_zapped;
1499 ret = mmu_zap_unsync_children(kvm, sp);
1500 kvm_mmu_page_unlink_children(kvm, sp);
1501 kvm_mmu_unlink_parents(kvm, sp);
1502 kvm_flush_remote_tlbs(kvm);
1503 if (!sp->role.invalid && !sp->role.direct)
1504 unaccount_shadowed(kvm, sp->gfn);
1506 kvm_unlink_unsync_page(kvm, sp);
1507 if (!sp->root_count) {
1508 hlist_del(&sp->hash_link);
1509 kvm_mmu_free_page(kvm, sp);
1511 sp->role.invalid = 1;
1512 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1513 kvm_reload_remote_mmus(kvm);
1515 kvm_mmu_reset_last_pte_updated(kvm);
1520 * Changing the number of mmu pages allocated to the vm
1521 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1523 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1527 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1528 used_pages = max(0, used_pages);
1531 * If we set the number of mmu pages to be smaller be than the
1532 * number of actived pages , we must to free some mmu pages before we
1536 if (used_pages > kvm_nr_mmu_pages) {
1537 while (used_pages > kvm_nr_mmu_pages &&
1538 !list_empty(&kvm->arch.active_mmu_pages)) {
1539 struct kvm_mmu_page *page;
1541 page = container_of(kvm->arch.active_mmu_pages.prev,
1542 struct kvm_mmu_page, link);
1543 used_pages -= kvm_mmu_zap_page(kvm, page);
1546 kvm_nr_mmu_pages = used_pages;
1547 kvm->arch.n_free_mmu_pages = 0;
1550 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1551 - kvm->arch.n_alloc_mmu_pages;
1553 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1556 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1559 struct hlist_head *bucket;
1560 struct kvm_mmu_page *sp;
1561 struct hlist_node *node, *n;
1564 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1566 index = kvm_page_table_hashfn(gfn);
1567 bucket = &kvm->arch.mmu_page_hash[index];
1568 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1569 if (sp->gfn == gfn && !sp->role.direct) {
1570 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1573 if (kvm_mmu_zap_page(kvm, sp))
1579 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1582 struct hlist_head *bucket;
1583 struct kvm_mmu_page *sp;
1584 struct hlist_node *node, *nn;
1586 index = kvm_page_table_hashfn(gfn);
1587 bucket = &kvm->arch.mmu_page_hash[index];
1588 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1589 if (sp->gfn == gfn && !sp->role.direct
1590 && !sp->role.invalid) {
1591 pgprintk("%s: zap %lx %x\n",
1592 __func__, gfn, sp->role.word);
1593 if (kvm_mmu_zap_page(kvm, sp))
1599 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1601 int slot = memslot_id(kvm, gfn);
1602 struct kvm_mmu_page *sp = page_header(__pa(pte));
1604 __set_bit(slot, sp->slot_bitmap);
1607 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1612 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1615 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1616 if (pt[i] == shadow_notrap_nonpresent_pte)
1617 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
1621 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1625 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
1627 if (gpa == UNMAPPED_GVA)
1630 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1636 * The function is based on mtrr_type_lookup() in
1637 * arch/x86/kernel/cpu/mtrr/generic.c
1639 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1644 u8 prev_match, curr_match;
1645 int num_var_ranges = KVM_NR_VAR_MTRR;
1647 if (!mtrr_state->enabled)
1650 /* Make end inclusive end, instead of exclusive */
1653 /* Look in fixed ranges. Just return the type as per start */
1654 if (mtrr_state->have_fixed && (start < 0x100000)) {
1657 if (start < 0x80000) {
1659 idx += (start >> 16);
1660 return mtrr_state->fixed_ranges[idx];
1661 } else if (start < 0xC0000) {
1663 idx += ((start - 0x80000) >> 14);
1664 return mtrr_state->fixed_ranges[idx];
1665 } else if (start < 0x1000000) {
1667 idx += ((start - 0xC0000) >> 12);
1668 return mtrr_state->fixed_ranges[idx];
1673 * Look in variable ranges
1674 * Look of multiple ranges matching this address and pick type
1675 * as per MTRR precedence
1677 if (!(mtrr_state->enabled & 2))
1678 return mtrr_state->def_type;
1681 for (i = 0; i < num_var_ranges; ++i) {
1682 unsigned short start_state, end_state;
1684 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1687 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1688 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1689 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1690 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1692 start_state = ((start & mask) == (base & mask));
1693 end_state = ((end & mask) == (base & mask));
1694 if (start_state != end_state)
1697 if ((start & mask) != (base & mask))
1700 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1701 if (prev_match == 0xFF) {
1702 prev_match = curr_match;
1706 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1707 curr_match == MTRR_TYPE_UNCACHABLE)
1708 return MTRR_TYPE_UNCACHABLE;
1710 if ((prev_match == MTRR_TYPE_WRBACK &&
1711 curr_match == MTRR_TYPE_WRTHROUGH) ||
1712 (prev_match == MTRR_TYPE_WRTHROUGH &&
1713 curr_match == MTRR_TYPE_WRBACK)) {
1714 prev_match = MTRR_TYPE_WRTHROUGH;
1715 curr_match = MTRR_TYPE_WRTHROUGH;
1718 if (prev_match != curr_match)
1719 return MTRR_TYPE_UNCACHABLE;
1722 if (prev_match != 0xFF)
1725 return mtrr_state->def_type;
1728 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1732 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1733 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1734 if (mtrr == 0xfe || mtrr == 0xff)
1735 mtrr = MTRR_TYPE_WRBACK;
1738 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1740 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1743 struct hlist_head *bucket;
1744 struct kvm_mmu_page *s;
1745 struct hlist_node *node, *n;
1747 trace_kvm_mmu_unsync_page(sp);
1748 index = kvm_page_table_hashfn(sp->gfn);
1749 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1750 /* don't unsync if pagetable is shadowed with multiple roles */
1751 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1752 if (s->gfn != sp->gfn || s->role.direct)
1754 if (s->role.word != sp->role.word)
1757 ++vcpu->kvm->stat.mmu_unsync;
1760 kvm_mmu_mark_parents_unsync(sp);
1762 mmu_convert_notrap(sp);
1766 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1769 struct kvm_mmu_page *shadow;
1771 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1773 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1777 if (can_unsync && oos_shadow)
1778 return kvm_unsync_page(vcpu, shadow);
1784 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1785 unsigned pte_access, int user_fault,
1786 int write_fault, int dirty, int level,
1787 gfn_t gfn, pfn_t pfn, bool speculative,
1788 bool can_unsync, bool reset_host_protection)
1794 * We don't set the accessed bit, since we sometimes want to see
1795 * whether the guest actually used the pte (in order to detect
1798 spte = shadow_base_present_pte | shadow_dirty_mask;
1800 spte |= shadow_accessed_mask;
1802 pte_access &= ~ACC_WRITE_MASK;
1803 if (pte_access & ACC_EXEC_MASK)
1804 spte |= shadow_x_mask;
1806 spte |= shadow_nx_mask;
1807 if (pte_access & ACC_USER_MASK)
1808 spte |= shadow_user_mask;
1809 if (level > PT_PAGE_TABLE_LEVEL)
1810 spte |= PT_PAGE_SIZE_MASK;
1812 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1813 kvm_is_mmio_pfn(pfn));
1815 if (reset_host_protection)
1816 spte |= SPTE_HOST_WRITEABLE;
1818 spte |= (u64)pfn << PAGE_SHIFT;
1820 if ((pte_access & ACC_WRITE_MASK)
1821 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1823 if (level > PT_PAGE_TABLE_LEVEL &&
1824 has_wrprotected_page(vcpu->kvm, gfn, level)) {
1826 spte = shadow_trap_nonpresent_pte;
1830 spte |= PT_WRITABLE_MASK;
1833 * Optimization: for pte sync, if spte was writable the hash
1834 * lookup is unnecessary (and expensive). Write protection
1835 * is responsibility of mmu_get_page / kvm_sync_page.
1836 * Same reasoning can be applied to dirty page accounting.
1838 if (!can_unsync && is_writable_pte(*sptep))
1841 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1842 pgprintk("%s: found shadow page for %lx, marking ro\n",
1845 pte_access &= ~ACC_WRITE_MASK;
1846 if (is_writable_pte(spte))
1847 spte &= ~PT_WRITABLE_MASK;
1851 if (pte_access & ACC_WRITE_MASK)
1852 mark_page_dirty(vcpu->kvm, gfn);
1855 __set_spte(sptep, spte);
1859 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1860 unsigned pt_access, unsigned pte_access,
1861 int user_fault, int write_fault, int dirty,
1862 int *ptwrite, int level, gfn_t gfn,
1863 pfn_t pfn, bool speculative,
1864 bool reset_host_protection)
1866 int was_rmapped = 0;
1867 int was_writable = is_writable_pte(*sptep);
1870 pgprintk("%s: spte %llx access %x write_fault %d"
1871 " user_fault %d gfn %lx\n",
1872 __func__, *sptep, pt_access,
1873 write_fault, user_fault, gfn);
1875 if (is_rmap_spte(*sptep)) {
1877 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1878 * the parent of the now unreachable PTE.
1880 if (level > PT_PAGE_TABLE_LEVEL &&
1881 !is_large_pte(*sptep)) {
1882 struct kvm_mmu_page *child;
1885 child = page_header(pte & PT64_BASE_ADDR_MASK);
1886 mmu_page_remove_parent_pte(child, sptep);
1887 } else if (pfn != spte_to_pfn(*sptep)) {
1888 pgprintk("hfn old %lx new %lx\n",
1889 spte_to_pfn(*sptep), pfn);
1890 rmap_remove(vcpu->kvm, sptep);
1895 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
1896 dirty, level, gfn, pfn, speculative, true,
1897 reset_host_protection)) {
1900 kvm_x86_ops->tlb_flush(vcpu);
1903 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
1904 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1905 is_large_pte(*sptep)? "2MB" : "4kB",
1906 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1908 if (!was_rmapped && is_large_pte(*sptep))
1909 ++vcpu->kvm->stat.lpages;
1911 page_header_update_slot(vcpu->kvm, sptep, gfn);
1913 rmap_count = rmap_add(vcpu, sptep, gfn);
1914 kvm_release_pfn_clean(pfn);
1915 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1916 rmap_recycle(vcpu, sptep, gfn);
1919 kvm_release_pfn_dirty(pfn);
1921 kvm_release_pfn_clean(pfn);
1924 vcpu->arch.last_pte_updated = sptep;
1925 vcpu->arch.last_pte_gfn = gfn;
1929 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1933 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1934 int level, gfn_t gfn, pfn_t pfn)
1936 struct kvm_shadow_walk_iterator iterator;
1937 struct kvm_mmu_page *sp;
1941 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1942 if (iterator.level == level) {
1943 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1944 0, write, 1, &pt_write,
1945 level, gfn, pfn, false, true);
1946 ++vcpu->stat.pf_fixed;
1950 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1951 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1952 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1954 1, ACC_ALL, iterator.sptep);
1956 pgprintk("nonpaging_map: ENOMEM\n");
1957 kvm_release_pfn_clean(pfn);
1961 __set_spte(iterator.sptep,
1963 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1964 | shadow_user_mask | shadow_x_mask);
1970 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1975 unsigned long mmu_seq;
1977 level = mapping_level(vcpu, gfn);
1980 * This path builds a PAE pagetable - so we can map 2mb pages at
1981 * maximum. Therefore check if the level is larger than that.
1983 if (level > PT_DIRECTORY_LEVEL)
1984 level = PT_DIRECTORY_LEVEL;
1986 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
1988 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1990 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1993 if (is_error_pfn(pfn)) {
1994 kvm_release_pfn_clean(pfn);
1998 spin_lock(&vcpu->kvm->mmu_lock);
1999 if (mmu_notifier_retry(vcpu, mmu_seq))
2001 kvm_mmu_free_some_pages(vcpu);
2002 r = __direct_map(vcpu, v, write, level, gfn, pfn);
2003 spin_unlock(&vcpu->kvm->mmu_lock);
2009 spin_unlock(&vcpu->kvm->mmu_lock);
2010 kvm_release_pfn_clean(pfn);
2015 static void mmu_free_roots(struct kvm_vcpu *vcpu)
2018 struct kvm_mmu_page *sp;
2020 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2022 spin_lock(&vcpu->kvm->mmu_lock);
2023 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2024 hpa_t root = vcpu->arch.mmu.root_hpa;
2026 sp = page_header(root);
2028 if (!sp->root_count && sp->role.invalid)
2029 kvm_mmu_zap_page(vcpu->kvm, sp);
2030 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2031 spin_unlock(&vcpu->kvm->mmu_lock);
2034 for (i = 0; i < 4; ++i) {
2035 hpa_t root = vcpu->arch.mmu.pae_root[i];
2038 root &= PT64_BASE_ADDR_MASK;
2039 sp = page_header(root);
2041 if (!sp->root_count && sp->role.invalid)
2042 kvm_mmu_zap_page(vcpu->kvm, sp);
2044 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2046 spin_unlock(&vcpu->kvm->mmu_lock);
2047 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2050 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2054 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2055 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2062 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2066 struct kvm_mmu_page *sp;
2070 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
2072 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2073 hpa_t root = vcpu->arch.mmu.root_hpa;
2075 ASSERT(!VALID_PAGE(root));
2078 if (mmu_check_root(vcpu, root_gfn))
2080 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
2081 PT64_ROOT_LEVEL, direct,
2083 root = __pa(sp->spt);
2085 vcpu->arch.mmu.root_hpa = root;
2088 direct = !is_paging(vcpu);
2091 for (i = 0; i < 4; ++i) {
2092 hpa_t root = vcpu->arch.mmu.pae_root[i];
2094 ASSERT(!VALID_PAGE(root));
2095 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
2096 pdptr = kvm_pdptr_read(vcpu, i);
2097 if (!is_present_gpte(pdptr)) {
2098 vcpu->arch.mmu.pae_root[i] = 0;
2101 root_gfn = pdptr >> PAGE_SHIFT;
2102 } else if (vcpu->arch.mmu.root_level == 0)
2104 if (mmu_check_root(vcpu, root_gfn))
2106 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
2107 PT32_ROOT_LEVEL, direct,
2109 root = __pa(sp->spt);
2111 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2113 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2117 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2120 struct kvm_mmu_page *sp;
2122 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2124 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2125 hpa_t root = vcpu->arch.mmu.root_hpa;
2126 sp = page_header(root);
2127 mmu_sync_children(vcpu, sp);
2130 for (i = 0; i < 4; ++i) {
2131 hpa_t root = vcpu->arch.mmu.pae_root[i];
2133 if (root && VALID_PAGE(root)) {
2134 root &= PT64_BASE_ADDR_MASK;
2135 sp = page_header(root);
2136 mmu_sync_children(vcpu, sp);
2141 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2143 spin_lock(&vcpu->kvm->mmu_lock);
2144 mmu_sync_roots(vcpu);
2145 spin_unlock(&vcpu->kvm->mmu_lock);
2148 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2149 u32 access, u32 *error)
2156 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2162 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2163 r = mmu_topup_memory_caches(vcpu);
2168 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2170 gfn = gva >> PAGE_SHIFT;
2172 return nonpaging_map(vcpu, gva & PAGE_MASK,
2173 error_code & PFERR_WRITE_MASK, gfn);
2176 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2182 gfn_t gfn = gpa >> PAGE_SHIFT;
2183 unsigned long mmu_seq;
2186 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2188 r = mmu_topup_memory_caches(vcpu);
2192 level = mapping_level(vcpu, gfn);
2194 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2196 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2198 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2199 if (is_error_pfn(pfn)) {
2200 kvm_release_pfn_clean(pfn);
2203 spin_lock(&vcpu->kvm->mmu_lock);
2204 if (mmu_notifier_retry(vcpu, mmu_seq))
2206 kvm_mmu_free_some_pages(vcpu);
2207 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2209 spin_unlock(&vcpu->kvm->mmu_lock);
2214 spin_unlock(&vcpu->kvm->mmu_lock);
2215 kvm_release_pfn_clean(pfn);
2219 static void nonpaging_free(struct kvm_vcpu *vcpu)
2221 mmu_free_roots(vcpu);
2224 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2226 struct kvm_mmu *context = &vcpu->arch.mmu;
2228 context->new_cr3 = nonpaging_new_cr3;
2229 context->page_fault = nonpaging_page_fault;
2230 context->gva_to_gpa = nonpaging_gva_to_gpa;
2231 context->free = nonpaging_free;
2232 context->prefetch_page = nonpaging_prefetch_page;
2233 context->sync_page = nonpaging_sync_page;
2234 context->invlpg = nonpaging_invlpg;
2235 context->root_level = 0;
2236 context->shadow_root_level = PT32E_ROOT_LEVEL;
2237 context->root_hpa = INVALID_PAGE;
2241 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2243 ++vcpu->stat.tlb_flush;
2244 kvm_x86_ops->tlb_flush(vcpu);
2247 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2249 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2250 mmu_free_roots(vcpu);
2253 static void inject_page_fault(struct kvm_vcpu *vcpu,
2257 kvm_inject_page_fault(vcpu, addr, err_code);
2260 static void paging_free(struct kvm_vcpu *vcpu)
2262 nonpaging_free(vcpu);
2265 static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2269 bit7 = (gpte >> 7) & 1;
2270 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2274 #include "paging_tmpl.h"
2278 #include "paging_tmpl.h"
2281 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2283 struct kvm_mmu *context = &vcpu->arch.mmu;
2284 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2285 u64 exb_bit_rsvd = 0;
2288 exb_bit_rsvd = rsvd_bits(63, 63);
2290 case PT32_ROOT_LEVEL:
2291 /* no rsvd bits for 2 level 4K page table entries */
2292 context->rsvd_bits_mask[0][1] = 0;
2293 context->rsvd_bits_mask[0][0] = 0;
2294 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2296 if (!is_pse(vcpu)) {
2297 context->rsvd_bits_mask[1][1] = 0;
2301 if (is_cpuid_PSE36())
2302 /* 36bits PSE 4MB page */
2303 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2305 /* 32 bits PSE 4MB page */
2306 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2308 case PT32E_ROOT_LEVEL:
2309 context->rsvd_bits_mask[0][2] =
2310 rsvd_bits(maxphyaddr, 63) |
2311 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2312 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2313 rsvd_bits(maxphyaddr, 62); /* PDE */
2314 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2315 rsvd_bits(maxphyaddr, 62); /* PTE */
2316 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2317 rsvd_bits(maxphyaddr, 62) |
2318 rsvd_bits(13, 20); /* large page */
2319 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2321 case PT64_ROOT_LEVEL:
2322 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2323 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2324 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2325 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2326 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2327 rsvd_bits(maxphyaddr, 51);
2328 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2329 rsvd_bits(maxphyaddr, 51);
2330 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2331 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2332 rsvd_bits(maxphyaddr, 51) |
2334 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2335 rsvd_bits(maxphyaddr, 51) |
2336 rsvd_bits(13, 20); /* large page */
2337 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2342 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2344 struct kvm_mmu *context = &vcpu->arch.mmu;
2346 ASSERT(is_pae(vcpu));
2347 context->new_cr3 = paging_new_cr3;
2348 context->page_fault = paging64_page_fault;
2349 context->gva_to_gpa = paging64_gva_to_gpa;
2350 context->prefetch_page = paging64_prefetch_page;
2351 context->sync_page = paging64_sync_page;
2352 context->invlpg = paging64_invlpg;
2353 context->free = paging_free;
2354 context->root_level = level;
2355 context->shadow_root_level = level;
2356 context->root_hpa = INVALID_PAGE;
2360 static int paging64_init_context(struct kvm_vcpu *vcpu)
2362 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2363 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2366 static int paging32_init_context(struct kvm_vcpu *vcpu)
2368 struct kvm_mmu *context = &vcpu->arch.mmu;
2370 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2371 context->new_cr3 = paging_new_cr3;
2372 context->page_fault = paging32_page_fault;
2373 context->gva_to_gpa = paging32_gva_to_gpa;
2374 context->free = paging_free;
2375 context->prefetch_page = paging32_prefetch_page;
2376 context->sync_page = paging32_sync_page;
2377 context->invlpg = paging32_invlpg;
2378 context->root_level = PT32_ROOT_LEVEL;
2379 context->shadow_root_level = PT32E_ROOT_LEVEL;
2380 context->root_hpa = INVALID_PAGE;
2384 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2386 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2387 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2390 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2392 struct kvm_mmu *context = &vcpu->arch.mmu;
2394 context->new_cr3 = nonpaging_new_cr3;
2395 context->page_fault = tdp_page_fault;
2396 context->free = nonpaging_free;
2397 context->prefetch_page = nonpaging_prefetch_page;
2398 context->sync_page = nonpaging_sync_page;
2399 context->invlpg = nonpaging_invlpg;
2400 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2401 context->root_hpa = INVALID_PAGE;
2403 if (!is_paging(vcpu)) {
2404 context->gva_to_gpa = nonpaging_gva_to_gpa;
2405 context->root_level = 0;
2406 } else if (is_long_mode(vcpu)) {
2407 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2408 context->gva_to_gpa = paging64_gva_to_gpa;
2409 context->root_level = PT64_ROOT_LEVEL;
2410 } else if (is_pae(vcpu)) {
2411 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2412 context->gva_to_gpa = paging64_gva_to_gpa;
2413 context->root_level = PT32E_ROOT_LEVEL;
2415 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2416 context->gva_to_gpa = paging32_gva_to_gpa;
2417 context->root_level = PT32_ROOT_LEVEL;
2423 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2428 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2430 if (!is_paging(vcpu))
2431 r = nonpaging_init_context(vcpu);
2432 else if (is_long_mode(vcpu))
2433 r = paging64_init_context(vcpu);
2434 else if (is_pae(vcpu))
2435 r = paging32E_init_context(vcpu);
2437 r = paging32_init_context(vcpu);
2439 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
2444 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2446 vcpu->arch.update_pte.pfn = bad_pfn;
2449 return init_kvm_tdp_mmu(vcpu);
2451 return init_kvm_softmmu(vcpu);
2454 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2457 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2458 vcpu->arch.mmu.free(vcpu);
2459 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2463 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2465 destroy_kvm_mmu(vcpu);
2466 return init_kvm_mmu(vcpu);
2468 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2470 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2474 r = mmu_topup_memory_caches(vcpu);
2477 spin_lock(&vcpu->kvm->mmu_lock);
2478 kvm_mmu_free_some_pages(vcpu);
2479 r = mmu_alloc_roots(vcpu);
2480 mmu_sync_roots(vcpu);
2481 spin_unlock(&vcpu->kvm->mmu_lock);
2484 /* set_cr3() should ensure TLB has been flushed */
2485 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2489 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2491 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2493 mmu_free_roots(vcpu);
2496 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2497 struct kvm_mmu_page *sp,
2501 struct kvm_mmu_page *child;
2504 if (is_shadow_present_pte(pte)) {
2505 if (is_last_spte(pte, sp->role.level))
2506 rmap_remove(vcpu->kvm, spte);
2508 child = page_header(pte & PT64_BASE_ADDR_MASK);
2509 mmu_page_remove_parent_pte(child, spte);
2512 __set_spte(spte, shadow_trap_nonpresent_pte);
2513 if (is_large_pte(pte))
2514 --vcpu->kvm->stat.lpages;
2517 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2518 struct kvm_mmu_page *sp,
2522 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2523 ++vcpu->kvm->stat.mmu_pde_zapped;
2527 ++vcpu->kvm->stat.mmu_pte_updated;
2528 if (!sp->role.cr4_pae)
2529 paging32_update_pte(vcpu, sp, spte, new);
2531 paging64_update_pte(vcpu, sp, spte, new);
2534 static bool need_remote_flush(u64 old, u64 new)
2536 if (!is_shadow_present_pte(old))
2538 if (!is_shadow_present_pte(new))
2540 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2542 old ^= PT64_NX_MASK;
2543 new ^= PT64_NX_MASK;
2544 return (old & ~new & PT64_PERM_MASK) != 0;
2547 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2549 if (need_remote_flush(old, new))
2550 kvm_flush_remote_tlbs(vcpu->kvm);
2552 kvm_mmu_flush_tlb(vcpu);
2555 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2557 u64 *spte = vcpu->arch.last_pte_updated;
2559 return !!(spte && (*spte & shadow_accessed_mask));
2562 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2568 if (!is_present_gpte(gpte))
2570 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2572 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2574 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2576 if (is_error_pfn(pfn)) {
2577 kvm_release_pfn_clean(pfn);
2580 vcpu->arch.update_pte.gfn = gfn;
2581 vcpu->arch.update_pte.pfn = pfn;
2584 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2586 u64 *spte = vcpu->arch.last_pte_updated;
2589 && vcpu->arch.last_pte_gfn == gfn
2590 && shadow_accessed_mask
2591 && !(*spte & shadow_accessed_mask)
2592 && is_shadow_present_pte(*spte))
2593 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2596 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2597 const u8 *new, int bytes,
2598 bool guest_initiated)
2600 gfn_t gfn = gpa >> PAGE_SHIFT;
2601 struct kvm_mmu_page *sp;
2602 struct hlist_node *node, *n;
2603 struct hlist_head *bucket;
2607 unsigned offset = offset_in_page(gpa);
2609 unsigned page_offset;
2610 unsigned misaligned;
2618 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2620 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2623 * Assume that the pte write on a page table of the same type
2624 * as the current vcpu paging mode. This is nearly always true
2625 * (might be false while changing modes). Note it is verified later
2628 if ((is_pae(vcpu) && bytes == 4) || !new) {
2629 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2634 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2637 new = (const u8 *)&gentry;
2642 gentry = *(const u32 *)new;
2645 gentry = *(const u64 *)new;
2652 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
2653 spin_lock(&vcpu->kvm->mmu_lock);
2654 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2656 kvm_mmu_access_page(vcpu, gfn);
2657 kvm_mmu_free_some_pages(vcpu);
2658 ++vcpu->kvm->stat.mmu_pte_write;
2659 kvm_mmu_audit(vcpu, "pre pte write");
2660 if (guest_initiated) {
2661 if (gfn == vcpu->arch.last_pt_write_gfn
2662 && !last_updated_pte_accessed(vcpu)) {
2663 ++vcpu->arch.last_pt_write_count;
2664 if (vcpu->arch.last_pt_write_count >= 3)
2667 vcpu->arch.last_pt_write_gfn = gfn;
2668 vcpu->arch.last_pt_write_count = 1;
2669 vcpu->arch.last_pte_updated = NULL;
2672 index = kvm_page_table_hashfn(gfn);
2673 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2674 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2675 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2677 pte_size = sp->role.cr4_pae ? 8 : 4;
2678 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2679 misaligned |= bytes < 4;
2680 if (misaligned || flooded) {
2682 * Misaligned accesses are too much trouble to fix
2683 * up; also, they usually indicate a page is not used
2686 * If we're seeing too many writes to a page,
2687 * it may no longer be a page table, or we may be
2688 * forking, in which case it is better to unmap the
2691 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2692 gpa, bytes, sp->role.word);
2693 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2695 ++vcpu->kvm->stat.mmu_flooded;
2698 page_offset = offset;
2699 level = sp->role.level;
2701 if (!sp->role.cr4_pae) {
2702 page_offset <<= 1; /* 32->64 */
2704 * A 32-bit pde maps 4MB while the shadow pdes map
2705 * only 2MB. So we need to double the offset again
2706 * and zap two pdes instead of one.
2708 if (level == PT32_ROOT_LEVEL) {
2709 page_offset &= ~7; /* kill rounding error */
2713 quadrant = page_offset >> PAGE_SHIFT;
2714 page_offset &= ~PAGE_MASK;
2715 if (quadrant != sp->role.quadrant)
2718 spte = &sp->spt[page_offset / sizeof(*spte)];
2721 mmu_pte_write_zap_pte(vcpu, sp, spte);
2723 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
2724 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2728 kvm_mmu_audit(vcpu, "post pte write");
2729 spin_unlock(&vcpu->kvm->mmu_lock);
2730 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2731 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2732 vcpu->arch.update_pte.pfn = bad_pfn;
2736 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2744 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
2746 spin_lock(&vcpu->kvm->mmu_lock);
2747 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2748 spin_unlock(&vcpu->kvm->mmu_lock);
2751 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2753 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2755 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2756 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2757 struct kvm_mmu_page *sp;
2759 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2760 struct kvm_mmu_page, link);
2761 kvm_mmu_zap_page(vcpu->kvm, sp);
2762 ++vcpu->kvm->stat.mmu_recycled;
2766 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2769 enum emulation_result er;
2771 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2780 r = mmu_topup_memory_caches(vcpu);
2784 er = emulate_instruction(vcpu, cr2, error_code, 0);
2789 case EMULATE_DO_MMIO:
2790 ++vcpu->stat.mmio_exits;
2793 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2794 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2795 vcpu->run->internal.ndata = 0;
2803 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2805 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2807 vcpu->arch.mmu.invlpg(vcpu, gva);
2808 kvm_mmu_flush_tlb(vcpu);
2809 ++vcpu->stat.invlpg;
2811 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2813 void kvm_enable_tdp(void)
2817 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2819 void kvm_disable_tdp(void)
2821 tdp_enabled = false;
2823 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2825 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2827 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2830 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2838 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2839 * Therefore we need to allocate shadow page tables in the first
2840 * 4GB of memory, which happens to fit the DMA32 zone.
2842 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2846 vcpu->arch.mmu.pae_root = page_address(page);
2847 for (i = 0; i < 4; ++i)
2848 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2853 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2856 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2858 return alloc_mmu_pages(vcpu);
2861 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2864 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2866 return init_kvm_mmu(vcpu);
2869 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2873 destroy_kvm_mmu(vcpu);
2874 free_mmu_pages(vcpu);
2875 mmu_free_memory_caches(vcpu);
2878 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2880 struct kvm_mmu_page *sp;
2882 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2886 if (!test_bit(slot, sp->slot_bitmap))
2890 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2892 if (pt[i] & PT_WRITABLE_MASK)
2893 pt[i] &= ~PT_WRITABLE_MASK;
2895 kvm_flush_remote_tlbs(kvm);
2898 void kvm_mmu_zap_all(struct kvm *kvm)
2900 struct kvm_mmu_page *sp, *node;
2902 spin_lock(&kvm->mmu_lock);
2903 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2904 if (kvm_mmu_zap_page(kvm, sp))
2905 node = container_of(kvm->arch.active_mmu_pages.next,
2906 struct kvm_mmu_page, link);
2907 spin_unlock(&kvm->mmu_lock);
2909 kvm_flush_remote_tlbs(kvm);
2912 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2914 struct kvm_mmu_page *page;
2916 page = container_of(kvm->arch.active_mmu_pages.prev,
2917 struct kvm_mmu_page, link);
2918 kvm_mmu_zap_page(kvm, page);
2921 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2924 struct kvm *kvm_freed = NULL;
2925 int cache_count = 0;
2927 spin_lock(&kvm_lock);
2929 list_for_each_entry(kvm, &vm_list, vm_list) {
2932 idx = srcu_read_lock(&kvm->srcu);
2933 spin_lock(&kvm->mmu_lock);
2934 npages = kvm->arch.n_alloc_mmu_pages -
2935 kvm->arch.n_free_mmu_pages;
2936 cache_count += npages;
2937 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2938 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2944 spin_unlock(&kvm->mmu_lock);
2945 srcu_read_unlock(&kvm->srcu, idx);
2948 list_move_tail(&kvm_freed->vm_list, &vm_list);
2950 spin_unlock(&kvm_lock);
2955 static struct shrinker mmu_shrinker = {
2956 .shrink = mmu_shrink,
2957 .seeks = DEFAULT_SEEKS * 10,
2960 static void mmu_destroy_caches(void)
2962 if (pte_chain_cache)
2963 kmem_cache_destroy(pte_chain_cache);
2964 if (rmap_desc_cache)
2965 kmem_cache_destroy(rmap_desc_cache);
2966 if (mmu_page_header_cache)
2967 kmem_cache_destroy(mmu_page_header_cache);
2970 void kvm_mmu_module_exit(void)
2972 mmu_destroy_caches();
2973 unregister_shrinker(&mmu_shrinker);
2976 int kvm_mmu_module_init(void)
2978 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2979 sizeof(struct kvm_pte_chain),
2981 if (!pte_chain_cache)
2983 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2984 sizeof(struct kvm_rmap_desc),
2986 if (!rmap_desc_cache)
2989 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2990 sizeof(struct kvm_mmu_page),
2992 if (!mmu_page_header_cache)
2995 register_shrinker(&mmu_shrinker);
3000 mmu_destroy_caches();
3005 * Caculate mmu pages needed for kvm.
3007 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3010 unsigned int nr_mmu_pages;
3011 unsigned int nr_pages = 0;
3012 struct kvm_memslots *slots;
3014 slots = rcu_dereference(kvm->memslots);
3015 for (i = 0; i < slots->nmemslots; i++)
3016 nr_pages += slots->memslots[i].npages;
3018 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3019 nr_mmu_pages = max(nr_mmu_pages,
3020 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3022 return nr_mmu_pages;
3025 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3028 if (len > buffer->len)
3033 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3038 ret = pv_mmu_peek_buffer(buffer, len);
3043 buffer->processed += len;
3047 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3048 gpa_t addr, gpa_t value)
3053 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3056 r = mmu_topup_memory_caches(vcpu);
3060 if (!emulator_write_phys(vcpu, addr, &value, bytes))
3066 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3068 kvm_set_cr3(vcpu, vcpu->arch.cr3);
3072 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3074 spin_lock(&vcpu->kvm->mmu_lock);
3075 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3076 spin_unlock(&vcpu->kvm->mmu_lock);
3080 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3081 struct kvm_pv_mmu_op_buffer *buffer)
3083 struct kvm_mmu_op_header *header;
3085 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3088 switch (header->op) {
3089 case KVM_MMU_OP_WRITE_PTE: {
3090 struct kvm_mmu_op_write_pte *wpte;
3092 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3095 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3098 case KVM_MMU_OP_FLUSH_TLB: {
3099 struct kvm_mmu_op_flush_tlb *ftlb;
3101 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3104 return kvm_pv_mmu_flush_tlb(vcpu);
3106 case KVM_MMU_OP_RELEASE_PT: {
3107 struct kvm_mmu_op_release_pt *rpt;
3109 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3112 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3118 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3119 gpa_t addr, unsigned long *ret)
3122 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3124 buffer->ptr = buffer->buf;
3125 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3126 buffer->processed = 0;
3128 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3132 while (buffer->len) {
3133 r = kvm_pv_mmu_op_one(vcpu, buffer);
3142 *ret = buffer->processed;
3146 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3148 struct kvm_shadow_walk_iterator iterator;
3151 spin_lock(&vcpu->kvm->mmu_lock);
3152 for_each_shadow_entry(vcpu, addr, iterator) {
3153 sptes[iterator.level-1] = *iterator.sptep;
3155 if (!is_shadow_present_pte(*iterator.sptep))
3158 spin_unlock(&vcpu->kvm->mmu_lock);
3162 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3166 static const char *audit_msg;
3168 static gva_t canonicalize(gva_t gva)
3170 #ifdef CONFIG_X86_64
3171 gva = (long long)(gva << 16) >> 16;
3177 typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
3179 static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3184 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3185 u64 ent = sp->spt[i];
3187 if (is_shadow_present_pte(ent)) {
3188 if (!is_last_spte(ent, sp->role.level)) {
3189 struct kvm_mmu_page *child;
3190 child = page_header(ent & PT64_BASE_ADDR_MASK);
3191 __mmu_spte_walk(kvm, child, fn);
3193 fn(kvm, &sp->spt[i]);
3198 static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3201 struct kvm_mmu_page *sp;
3203 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3205 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3206 hpa_t root = vcpu->arch.mmu.root_hpa;
3207 sp = page_header(root);
3208 __mmu_spte_walk(vcpu->kvm, sp, fn);
3211 for (i = 0; i < 4; ++i) {
3212 hpa_t root = vcpu->arch.mmu.pae_root[i];
3214 if (root && VALID_PAGE(root)) {
3215 root &= PT64_BASE_ADDR_MASK;
3216 sp = page_header(root);
3217 __mmu_spte_walk(vcpu->kvm, sp, fn);
3223 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3224 gva_t va, int level)
3226 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3228 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3230 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3233 if (ent == shadow_trap_nonpresent_pte)
3236 va = canonicalize(va);
3237 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3238 audit_mappings_page(vcpu, ent, va, level - 1);
3240 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
3241 gfn_t gfn = gpa >> PAGE_SHIFT;
3242 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3243 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
3245 if (is_error_pfn(pfn)) {
3246 kvm_release_pfn_clean(pfn);
3250 if (is_shadow_present_pte(ent)
3251 && (ent & PT64_BASE_ADDR_MASK) != hpa)
3252 printk(KERN_ERR "xx audit error: (%s) levels %d"
3253 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3254 audit_msg, vcpu->arch.mmu.root_level,
3256 is_shadow_present_pte(ent));
3257 else if (ent == shadow_notrap_nonpresent_pte
3258 && !is_error_hpa(hpa))
3259 printk(KERN_ERR "audit: (%s) notrap shadow,"
3260 " valid guest gva %lx\n", audit_msg, va);
3261 kvm_release_pfn_clean(pfn);
3267 static void audit_mappings(struct kvm_vcpu *vcpu)
3271 if (vcpu->arch.mmu.root_level == 4)
3272 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3274 for (i = 0; i < 4; ++i)
3275 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3276 audit_mappings_page(vcpu,
3277 vcpu->arch.mmu.pae_root[i],
3282 static int count_rmaps(struct kvm_vcpu *vcpu)
3284 struct kvm *kvm = vcpu->kvm;
3285 struct kvm_memslots *slots;
3289 idx = srcu_read_lock(&kvm->srcu);
3290 slots = rcu_dereference(kvm->memslots);
3291 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3292 struct kvm_memory_slot *m = &slots->memslots[i];
3293 struct kvm_rmap_desc *d;
3295 for (j = 0; j < m->npages; ++j) {
3296 unsigned long *rmapp = &m->rmap[j];
3300 if (!(*rmapp & 1)) {
3304 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3306 for (k = 0; k < RMAP_EXT; ++k)
3315 srcu_read_unlock(&kvm->srcu, idx);
3319 void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
3321 unsigned long *rmapp;
3322 struct kvm_mmu_page *rev_sp;
3325 if (*sptep & PT_WRITABLE_MASK) {
3326 rev_sp = page_header(__pa(sptep));
3327 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3329 if (!gfn_to_memslot(kvm, gfn)) {
3330 if (!printk_ratelimit())
3332 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3334 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3335 audit_msg, (long int)(sptep - rev_sp->spt),
3341 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3342 rev_sp->role.level);
3344 if (!printk_ratelimit())
3346 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3354 void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3356 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3359 static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3361 struct kvm_mmu_page *sp;
3364 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3367 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3370 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3373 if (!(ent & PT_PRESENT_MASK))
3375 if (!(ent & PT_WRITABLE_MASK))
3377 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
3383 static void audit_rmap(struct kvm_vcpu *vcpu)
3385 check_writable_mappings_rmap(vcpu);
3389 static void audit_write_protection(struct kvm_vcpu *vcpu)
3391 struct kvm_mmu_page *sp;
3392 struct kvm_memory_slot *slot;
3393 unsigned long *rmapp;
3397 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3398 if (sp->role.direct)
3403 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3404 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3405 rmapp = &slot->rmap[gfn - slot->base_gfn];
3407 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3409 if (*spte & PT_WRITABLE_MASK)
3410 printk(KERN_ERR "%s: (%s) shadow page has "
3411 "writable mappings: gfn %lx role %x\n",
3412 __func__, audit_msg, sp->gfn,
3414 spte = rmap_next(vcpu->kvm, rmapp, spte);
3419 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3426 audit_write_protection(vcpu);
3427 if (strcmp("pre pte write", audit_msg) != 0)
3428 audit_mappings(vcpu);
3429 audit_writable_sptes_have_rmaps(vcpu);