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.
23 #include <linux/kvm_host.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
27 #include <linux/highmem.h>
28 #include <linux/module.h>
29 #include <linux/swap.h>
32 #include <asm/cmpxchg.h>
36 * When setting this variable to true it enables Two-Dimensional-Paging
37 * where the hardware walks 2 page tables:
38 * 1. the guest-virtual to guest-physical
39 * 2. while doing 1. it walks guest-physical to host-physical
40 * If the hardware supports that we don't need to do shadow paging.
42 static bool tdp_enabled = false;
49 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
51 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
56 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
57 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
61 #define pgprintk(x...) do { } while (0)
62 #define rmap_printk(x...) do { } while (0)
66 #if defined(MMU_DEBUG) || defined(AUDIT)
71 #define ASSERT(x) do { } while (0)
75 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
76 __FILE__, __LINE__, #x); \
80 #define PT64_PT_BITS 9
81 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
82 #define PT32_PT_BITS 10
83 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
85 #define PT_WRITABLE_SHIFT 1
87 #define PT_PRESENT_MASK (1ULL << 0)
88 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
89 #define PT_USER_MASK (1ULL << 2)
90 #define PT_PWT_MASK (1ULL << 3)
91 #define PT_PCD_MASK (1ULL << 4)
92 #define PT_ACCESSED_MASK (1ULL << 5)
93 #define PT_DIRTY_MASK (1ULL << 6)
94 #define PT_PAGE_SIZE_MASK (1ULL << 7)
95 #define PT_PAT_MASK (1ULL << 7)
96 #define PT_GLOBAL_MASK (1ULL << 8)
97 #define PT64_NX_SHIFT 63
98 #define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
100 #define PT_PAT_SHIFT 7
101 #define PT_DIR_PAT_SHIFT 12
102 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
104 #define PT32_DIR_PSE36_SIZE 4
105 #define PT32_DIR_PSE36_SHIFT 13
106 #define PT32_DIR_PSE36_MASK \
107 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
110 #define PT_FIRST_AVAIL_BITS_SHIFT 9
111 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
113 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
115 #define PT64_LEVEL_BITS 9
117 #define PT64_LEVEL_SHIFT(level) \
118 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
120 #define PT64_LEVEL_MASK(level) \
121 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
123 #define PT64_INDEX(address, level)\
124 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
127 #define PT32_LEVEL_BITS 10
129 #define PT32_LEVEL_SHIFT(level) \
130 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
132 #define PT32_LEVEL_MASK(level) \
133 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
135 #define PT32_INDEX(address, level)\
136 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
139 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
140 #define PT64_DIR_BASE_ADDR_MASK \
141 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
143 #define PT32_BASE_ADDR_MASK PAGE_MASK
144 #define PT32_DIR_BASE_ADDR_MASK \
145 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
147 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
150 #define PFERR_PRESENT_MASK (1U << 0)
151 #define PFERR_WRITE_MASK (1U << 1)
152 #define PFERR_USER_MASK (1U << 2)
153 #define PFERR_FETCH_MASK (1U << 4)
155 #define PT64_ROOT_LEVEL 4
156 #define PT32_ROOT_LEVEL 2
157 #define PT32E_ROOT_LEVEL 3
159 #define PT_DIRECTORY_LEVEL 2
160 #define PT_PAGE_TABLE_LEVEL 1
164 #define ACC_EXEC_MASK 1
165 #define ACC_WRITE_MASK PT_WRITABLE_MASK
166 #define ACC_USER_MASK PT_USER_MASK
167 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
169 struct kvm_rmap_desc {
170 u64 *shadow_ptes[RMAP_EXT];
171 struct kvm_rmap_desc *more;
174 static struct kmem_cache *pte_chain_cache;
175 static struct kmem_cache *rmap_desc_cache;
176 static struct kmem_cache *mmu_page_header_cache;
178 static u64 __read_mostly shadow_trap_nonpresent_pte;
179 static u64 __read_mostly shadow_notrap_nonpresent_pte;
181 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
183 shadow_trap_nonpresent_pte = trap_pte;
184 shadow_notrap_nonpresent_pte = notrap_pte;
186 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
188 static int is_write_protection(struct kvm_vcpu *vcpu)
190 return vcpu->arch.cr0 & X86_CR0_WP;
193 static int is_cpuid_PSE36(void)
198 static int is_nx(struct kvm_vcpu *vcpu)
200 return vcpu->arch.shadow_efer & EFER_NX;
203 static int is_present_pte(unsigned long pte)
205 return pte & PT_PRESENT_MASK;
208 static int is_shadow_present_pte(u64 pte)
210 return pte != shadow_trap_nonpresent_pte
211 && pte != shadow_notrap_nonpresent_pte;
214 static int is_writeble_pte(unsigned long pte)
216 return pte & PT_WRITABLE_MASK;
219 static int is_dirty_pte(unsigned long pte)
221 return pte & PT_DIRTY_MASK;
224 static int is_rmap_pte(u64 pte)
226 return is_shadow_present_pte(pte);
229 static gfn_t pse36_gfn_delta(u32 gpte)
231 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
233 return (gpte & PT32_DIR_PSE36_MASK) << shift;
236 static void set_shadow_pte(u64 *sptep, u64 spte)
239 set_64bit((unsigned long *)sptep, spte);
241 set_64bit((unsigned long long *)sptep, spte);
245 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
246 struct kmem_cache *base_cache, int min)
250 if (cache->nobjs >= min)
252 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
253 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
256 cache->objects[cache->nobjs++] = obj;
261 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
264 kfree(mc->objects[--mc->nobjs]);
267 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
272 if (cache->nobjs >= min)
274 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
275 page = alloc_page(GFP_KERNEL);
278 set_page_private(page, 0);
279 cache->objects[cache->nobjs++] = page_address(page);
284 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
287 free_page((unsigned long)mc->objects[--mc->nobjs]);
290 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
294 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
298 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
302 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
305 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
306 mmu_page_header_cache, 4);
311 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
313 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
314 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
315 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
316 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
319 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
325 p = mc->objects[--mc->nobjs];
330 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
332 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
333 sizeof(struct kvm_pte_chain));
336 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
341 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
343 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
344 sizeof(struct kvm_rmap_desc));
347 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
353 * Take gfn and return the reverse mapping to it.
354 * Note: gfn must be unaliased before this function get called
357 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
359 struct kvm_memory_slot *slot;
361 slot = gfn_to_memslot(kvm, gfn);
362 return &slot->rmap[gfn - slot->base_gfn];
366 * Reverse mapping data structures:
368 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
369 * that points to page_address(page).
371 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
372 * containing more mappings.
374 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
376 struct kvm_mmu_page *sp;
377 struct kvm_rmap_desc *desc;
378 unsigned long *rmapp;
381 if (!is_rmap_pte(*spte))
383 gfn = unalias_gfn(vcpu->kvm, gfn);
384 sp = page_header(__pa(spte));
385 sp->gfns[spte - sp->spt] = gfn;
386 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
388 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
389 *rmapp = (unsigned long)spte;
390 } else if (!(*rmapp & 1)) {
391 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
392 desc = mmu_alloc_rmap_desc(vcpu);
393 desc->shadow_ptes[0] = (u64 *)*rmapp;
394 desc->shadow_ptes[1] = spte;
395 *rmapp = (unsigned long)desc | 1;
397 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
398 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
399 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
401 if (desc->shadow_ptes[RMAP_EXT-1]) {
402 desc->more = mmu_alloc_rmap_desc(vcpu);
405 for (i = 0; desc->shadow_ptes[i]; ++i)
407 desc->shadow_ptes[i] = spte;
411 static void rmap_desc_remove_entry(unsigned long *rmapp,
412 struct kvm_rmap_desc *desc,
414 struct kvm_rmap_desc *prev_desc)
418 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
420 desc->shadow_ptes[i] = desc->shadow_ptes[j];
421 desc->shadow_ptes[j] = NULL;
424 if (!prev_desc && !desc->more)
425 *rmapp = (unsigned long)desc->shadow_ptes[0];
428 prev_desc->more = desc->more;
430 *rmapp = (unsigned long)desc->more | 1;
431 mmu_free_rmap_desc(desc);
434 static void rmap_remove(struct kvm *kvm, u64 *spte)
436 struct kvm_rmap_desc *desc;
437 struct kvm_rmap_desc *prev_desc;
438 struct kvm_mmu_page *sp;
440 unsigned long *rmapp;
443 if (!is_rmap_pte(*spte))
445 sp = page_header(__pa(spte));
446 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
447 mark_page_accessed(page);
448 if (is_writeble_pte(*spte))
449 kvm_release_page_dirty(page);
451 kvm_release_page_clean(page);
452 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
454 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
456 } else if (!(*rmapp & 1)) {
457 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
458 if ((u64 *)*rmapp != spte) {
459 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
465 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
466 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
469 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
470 if (desc->shadow_ptes[i] == spte) {
471 rmap_desc_remove_entry(rmapp,
483 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
485 struct kvm_rmap_desc *desc;
486 struct kvm_rmap_desc *prev_desc;
492 else if (!(*rmapp & 1)) {
494 return (u64 *)*rmapp;
497 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
501 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
502 if (prev_spte == spte)
503 return desc->shadow_ptes[i];
504 prev_spte = desc->shadow_ptes[i];
511 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
513 unsigned long *rmapp;
515 int write_protected = 0;
517 gfn = unalias_gfn(kvm, gfn);
518 rmapp = gfn_to_rmap(kvm, gfn);
520 spte = rmap_next(kvm, rmapp, NULL);
523 BUG_ON(!(*spte & PT_PRESENT_MASK));
524 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
525 if (is_writeble_pte(*spte)) {
526 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
529 spte = rmap_next(kvm, rmapp, spte);
532 kvm_flush_remote_tlbs(kvm);
536 static int is_empty_shadow_page(u64 *spt)
541 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
542 if (*pos != shadow_trap_nonpresent_pte) {
543 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
551 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
553 ASSERT(is_empty_shadow_page(sp->spt));
555 __free_page(virt_to_page(sp->spt));
556 __free_page(virt_to_page(sp->gfns));
558 ++kvm->arch.n_free_mmu_pages;
561 static unsigned kvm_page_table_hashfn(gfn_t gfn)
563 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
566 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
569 struct kvm_mmu_page *sp;
571 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
572 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
573 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
574 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
575 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
576 ASSERT(is_empty_shadow_page(sp->spt));
579 sp->parent_pte = parent_pte;
580 --vcpu->kvm->arch.n_free_mmu_pages;
584 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
585 struct kvm_mmu_page *sp, u64 *parent_pte)
587 struct kvm_pte_chain *pte_chain;
588 struct hlist_node *node;
593 if (!sp->multimapped) {
594 u64 *old = sp->parent_pte;
597 sp->parent_pte = parent_pte;
601 pte_chain = mmu_alloc_pte_chain(vcpu);
602 INIT_HLIST_HEAD(&sp->parent_ptes);
603 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
604 pte_chain->parent_ptes[0] = old;
606 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
607 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
609 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
610 if (!pte_chain->parent_ptes[i]) {
611 pte_chain->parent_ptes[i] = parent_pte;
615 pte_chain = mmu_alloc_pte_chain(vcpu);
617 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
618 pte_chain->parent_ptes[0] = parent_pte;
621 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
624 struct kvm_pte_chain *pte_chain;
625 struct hlist_node *node;
628 if (!sp->multimapped) {
629 BUG_ON(sp->parent_pte != parent_pte);
630 sp->parent_pte = NULL;
633 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
634 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
635 if (!pte_chain->parent_ptes[i])
637 if (pte_chain->parent_ptes[i] != parent_pte)
639 while (i + 1 < NR_PTE_CHAIN_ENTRIES
640 && pte_chain->parent_ptes[i + 1]) {
641 pte_chain->parent_ptes[i]
642 = pte_chain->parent_ptes[i + 1];
645 pte_chain->parent_ptes[i] = NULL;
647 hlist_del(&pte_chain->link);
648 mmu_free_pte_chain(pte_chain);
649 if (hlist_empty(&sp->parent_ptes)) {
651 sp->parent_pte = NULL;
659 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
662 struct hlist_head *bucket;
663 struct kvm_mmu_page *sp;
664 struct hlist_node *node;
666 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
667 index = kvm_page_table_hashfn(gfn);
668 bucket = &kvm->arch.mmu_page_hash[index];
669 hlist_for_each_entry(sp, node, bucket, hash_link)
670 if (sp->gfn == gfn && !sp->role.metaphysical) {
671 pgprintk("%s: found role %x\n",
672 __FUNCTION__, sp->role.word);
678 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
686 union kvm_mmu_page_role role;
689 struct hlist_head *bucket;
690 struct kvm_mmu_page *sp;
691 struct hlist_node *node;
694 role.glevels = vcpu->arch.mmu.root_level;
696 role.metaphysical = metaphysical;
697 role.access = access;
698 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
699 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
700 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
701 role.quadrant = quadrant;
703 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
705 index = kvm_page_table_hashfn(gfn);
706 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
707 hlist_for_each_entry(sp, node, bucket, hash_link)
708 if (sp->gfn == gfn && sp->role.word == role.word) {
709 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
710 pgprintk("%s: found\n", __FUNCTION__);
713 ++vcpu->kvm->stat.mmu_cache_miss;
714 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
717 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
720 hlist_add_head(&sp->hash_link, bucket);
721 vcpu->arch.mmu.prefetch_page(vcpu, sp);
723 rmap_write_protect(vcpu->kvm, gfn);
727 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
728 struct kvm_mmu_page *sp)
736 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
737 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
738 if (is_shadow_present_pte(pt[i]))
739 rmap_remove(kvm, &pt[i]);
740 pt[i] = shadow_trap_nonpresent_pte;
742 kvm_flush_remote_tlbs(kvm);
746 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
749 pt[i] = shadow_trap_nonpresent_pte;
750 if (!is_shadow_present_pte(ent))
752 ent &= PT64_BASE_ADDR_MASK;
753 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
755 kvm_flush_remote_tlbs(kvm);
758 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
760 mmu_page_remove_parent_pte(sp, parent_pte);
763 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
767 for (i = 0; i < KVM_MAX_VCPUS; ++i)
769 kvm->vcpus[i]->arch.last_pte_updated = NULL;
772 static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
776 ++kvm->stat.mmu_shadow_zapped;
777 while (sp->multimapped || sp->parent_pte) {
778 if (!sp->multimapped)
779 parent_pte = sp->parent_pte;
781 struct kvm_pte_chain *chain;
783 chain = container_of(sp->parent_ptes.first,
784 struct kvm_pte_chain, link);
785 parent_pte = chain->parent_ptes[0];
788 kvm_mmu_put_page(sp, parent_pte);
789 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
791 kvm_mmu_page_unlink_children(kvm, sp);
792 if (!sp->root_count) {
793 hlist_del(&sp->hash_link);
794 kvm_mmu_free_page(kvm, sp);
796 list_move(&sp->link, &kvm->arch.active_mmu_pages);
797 kvm_mmu_reset_last_pte_updated(kvm);
801 * Changing the number of mmu pages allocated to the vm
802 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
804 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
807 * If we set the number of mmu pages to be smaller be than the
808 * number of actived pages , we must to free some mmu pages before we
812 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
814 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
815 - kvm->arch.n_free_mmu_pages;
817 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
818 struct kvm_mmu_page *page;
820 page = container_of(kvm->arch.active_mmu_pages.prev,
821 struct kvm_mmu_page, link);
822 kvm_mmu_zap_page(kvm, page);
825 kvm->arch.n_free_mmu_pages = 0;
828 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
829 - kvm->arch.n_alloc_mmu_pages;
831 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
834 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
837 struct hlist_head *bucket;
838 struct kvm_mmu_page *sp;
839 struct hlist_node *node, *n;
842 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
844 index = kvm_page_table_hashfn(gfn);
845 bucket = &kvm->arch.mmu_page_hash[index];
846 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
847 if (sp->gfn == gfn && !sp->role.metaphysical) {
848 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
850 kvm_mmu_zap_page(kvm, sp);
856 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
858 struct kvm_mmu_page *sp;
860 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
861 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
862 kvm_mmu_zap_page(kvm, sp);
866 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
868 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
869 struct kvm_mmu_page *sp = page_header(__pa(pte));
871 __set_bit(slot, &sp->slot_bitmap);
874 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
878 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
880 if (gpa == UNMAPPED_GVA)
883 down_read(¤t->mm->mmap_sem);
884 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
885 up_read(¤t->mm->mmap_sem);
890 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
891 unsigned pt_access, unsigned pte_access,
892 int user_fault, int write_fault, int dirty,
893 int *ptwrite, gfn_t gfn, struct page *page)
897 int was_writeble = is_writeble_pte(*shadow_pte);
898 hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
900 pgprintk("%s: spte %llx access %x write_fault %d"
901 " user_fault %d gfn %lx\n",
902 __FUNCTION__, *shadow_pte, pt_access,
903 write_fault, user_fault, gfn);
905 if (is_rmap_pte(*shadow_pte)) {
906 if (host_pfn != page_to_pfn(page)) {
907 pgprintk("hfn old %lx new %lx\n",
908 host_pfn, page_to_pfn(page));
909 rmap_remove(vcpu->kvm, shadow_pte);
916 * We don't set the accessed bit, since we sometimes want to see
917 * whether the guest actually used the pte (in order to detect
920 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
922 pte_access &= ~ACC_WRITE_MASK;
923 if (!(pte_access & ACC_EXEC_MASK))
924 spte |= PT64_NX_MASK;
926 spte |= PT_PRESENT_MASK;
927 if (pte_access & ACC_USER_MASK)
928 spte |= PT_USER_MASK;
930 spte |= page_to_phys(page);
932 if ((pte_access & ACC_WRITE_MASK)
933 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
934 struct kvm_mmu_page *shadow;
936 spte |= PT_WRITABLE_MASK;
938 mmu_unshadow(vcpu->kvm, gfn);
942 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
944 pgprintk("%s: found shadow page for %lx, marking ro\n",
946 pte_access &= ~ACC_WRITE_MASK;
947 if (is_writeble_pte(spte)) {
948 spte &= ~PT_WRITABLE_MASK;
949 kvm_x86_ops->tlb_flush(vcpu);
958 if (pte_access & ACC_WRITE_MASK)
959 mark_page_dirty(vcpu->kvm, gfn);
961 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
962 set_shadow_pte(shadow_pte, spte);
963 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
965 rmap_add(vcpu, shadow_pte, gfn);
966 if (!is_rmap_pte(*shadow_pte))
967 kvm_release_page_clean(page);
970 kvm_release_page_dirty(page);
972 kvm_release_page_clean(page);
974 if (!ptwrite || !*ptwrite)
975 vcpu->arch.last_pte_updated = shadow_pte;
978 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
982 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
983 gfn_t gfn, struct page *page, int level)
985 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
989 u32 index = PT64_INDEX(v, level);
992 ASSERT(VALID_PAGE(table_addr));
993 table = __va(table_addr);
996 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
997 0, write, 1, &pt_write, gfn, page);
1001 if (table[index] == shadow_trap_nonpresent_pte) {
1002 struct kvm_mmu_page *new_table;
1005 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1007 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1009 1, ACC_ALL, &table[index]);
1011 pgprintk("nonpaging_map: ENOMEM\n");
1012 kvm_release_page_clean(page);
1016 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
1017 | PT_WRITABLE_MASK | PT_USER_MASK;
1019 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1023 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1029 down_read(&vcpu->kvm->slots_lock);
1031 down_read(¤t->mm->mmap_sem);
1032 page = gfn_to_page(vcpu->kvm, gfn);
1033 up_read(¤t->mm->mmap_sem);
1036 if (is_error_page(page)) {
1037 kvm_release_page_clean(page);
1038 up_read(&vcpu->kvm->slots_lock);
1042 spin_lock(&vcpu->kvm->mmu_lock);
1043 kvm_mmu_free_some_pages(vcpu);
1044 r = __direct_map(vcpu, v, write, gfn, page, PT32E_ROOT_LEVEL);
1045 spin_unlock(&vcpu->kvm->mmu_lock);
1047 up_read(&vcpu->kvm->slots_lock);
1053 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1054 struct kvm_mmu_page *sp)
1058 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1059 sp->spt[i] = shadow_trap_nonpresent_pte;
1062 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1065 struct kvm_mmu_page *sp;
1067 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1069 spin_lock(&vcpu->kvm->mmu_lock);
1070 #ifdef CONFIG_X86_64
1071 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1072 hpa_t root = vcpu->arch.mmu.root_hpa;
1074 sp = page_header(root);
1076 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1077 spin_unlock(&vcpu->kvm->mmu_lock);
1081 for (i = 0; i < 4; ++i) {
1082 hpa_t root = vcpu->arch.mmu.pae_root[i];
1085 root &= PT64_BASE_ADDR_MASK;
1086 sp = page_header(root);
1089 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1091 spin_unlock(&vcpu->kvm->mmu_lock);
1092 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1095 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1099 struct kvm_mmu_page *sp;
1100 int metaphysical = 0;
1102 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1104 #ifdef CONFIG_X86_64
1105 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1106 hpa_t root = vcpu->arch.mmu.root_hpa;
1108 ASSERT(!VALID_PAGE(root));
1111 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1112 PT64_ROOT_LEVEL, metaphysical,
1114 root = __pa(sp->spt);
1116 vcpu->arch.mmu.root_hpa = root;
1120 metaphysical = !is_paging(vcpu);
1123 for (i = 0; i < 4; ++i) {
1124 hpa_t root = vcpu->arch.mmu.pae_root[i];
1126 ASSERT(!VALID_PAGE(root));
1127 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1128 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1129 vcpu->arch.mmu.pae_root[i] = 0;
1132 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1133 } else if (vcpu->arch.mmu.root_level == 0)
1135 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1136 PT32_ROOT_LEVEL, metaphysical,
1138 root = __pa(sp->spt);
1140 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1142 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1145 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1150 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1156 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
1157 r = mmu_topup_memory_caches(vcpu);
1162 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1164 gfn = gva >> PAGE_SHIFT;
1166 return nonpaging_map(vcpu, gva & PAGE_MASK,
1167 error_code & PFERR_WRITE_MASK, gfn);
1170 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1177 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1179 r = mmu_topup_memory_caches(vcpu);
1183 down_read(¤t->mm->mmap_sem);
1184 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1185 if (is_error_page(page)) {
1186 kvm_release_page_clean(page);
1187 up_read(¤t->mm->mmap_sem);
1190 spin_lock(&vcpu->kvm->mmu_lock);
1191 kvm_mmu_free_some_pages(vcpu);
1192 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1193 gpa >> PAGE_SHIFT, page, TDP_ROOT_LEVEL);
1194 spin_unlock(&vcpu->kvm->mmu_lock);
1195 up_read(¤t->mm->mmap_sem);
1200 static void nonpaging_free(struct kvm_vcpu *vcpu)
1202 mmu_free_roots(vcpu);
1205 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1207 struct kvm_mmu *context = &vcpu->arch.mmu;
1209 context->new_cr3 = nonpaging_new_cr3;
1210 context->page_fault = nonpaging_page_fault;
1211 context->gva_to_gpa = nonpaging_gva_to_gpa;
1212 context->free = nonpaging_free;
1213 context->prefetch_page = nonpaging_prefetch_page;
1214 context->root_level = 0;
1215 context->shadow_root_level = PT32E_ROOT_LEVEL;
1216 context->root_hpa = INVALID_PAGE;
1220 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1222 ++vcpu->stat.tlb_flush;
1223 kvm_x86_ops->tlb_flush(vcpu);
1226 static void paging_new_cr3(struct kvm_vcpu *vcpu)
1228 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->arch.cr3);
1229 mmu_free_roots(vcpu);
1232 static void inject_page_fault(struct kvm_vcpu *vcpu,
1236 kvm_inject_page_fault(vcpu, addr, err_code);
1239 static void paging_free(struct kvm_vcpu *vcpu)
1241 nonpaging_free(vcpu);
1245 #include "paging_tmpl.h"
1249 #include "paging_tmpl.h"
1252 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1254 struct kvm_mmu *context = &vcpu->arch.mmu;
1256 ASSERT(is_pae(vcpu));
1257 context->new_cr3 = paging_new_cr3;
1258 context->page_fault = paging64_page_fault;
1259 context->gva_to_gpa = paging64_gva_to_gpa;
1260 context->prefetch_page = paging64_prefetch_page;
1261 context->free = paging_free;
1262 context->root_level = level;
1263 context->shadow_root_level = level;
1264 context->root_hpa = INVALID_PAGE;
1268 static int paging64_init_context(struct kvm_vcpu *vcpu)
1270 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1273 static int paging32_init_context(struct kvm_vcpu *vcpu)
1275 struct kvm_mmu *context = &vcpu->arch.mmu;
1277 context->new_cr3 = paging_new_cr3;
1278 context->page_fault = paging32_page_fault;
1279 context->gva_to_gpa = paging32_gva_to_gpa;
1280 context->free = paging_free;
1281 context->prefetch_page = paging32_prefetch_page;
1282 context->root_level = PT32_ROOT_LEVEL;
1283 context->shadow_root_level = PT32E_ROOT_LEVEL;
1284 context->root_hpa = INVALID_PAGE;
1288 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1290 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1293 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1295 struct kvm_mmu *context = &vcpu->arch.mmu;
1297 context->new_cr3 = nonpaging_new_cr3;
1298 context->page_fault = tdp_page_fault;
1299 context->free = nonpaging_free;
1300 context->prefetch_page = nonpaging_prefetch_page;
1301 context->shadow_root_level = TDP_ROOT_LEVEL;
1302 context->root_hpa = INVALID_PAGE;
1304 if (!is_paging(vcpu)) {
1305 context->gva_to_gpa = nonpaging_gva_to_gpa;
1306 context->root_level = 0;
1307 } else if (is_long_mode(vcpu)) {
1308 context->gva_to_gpa = paging64_gva_to_gpa;
1309 context->root_level = PT64_ROOT_LEVEL;
1310 } else if (is_pae(vcpu)) {
1311 context->gva_to_gpa = paging64_gva_to_gpa;
1312 context->root_level = PT32E_ROOT_LEVEL;
1314 context->gva_to_gpa = paging32_gva_to_gpa;
1315 context->root_level = PT32_ROOT_LEVEL;
1321 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
1324 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1326 if (!is_paging(vcpu))
1327 return nonpaging_init_context(vcpu);
1328 else if (is_long_mode(vcpu))
1329 return paging64_init_context(vcpu);
1330 else if (is_pae(vcpu))
1331 return paging32E_init_context(vcpu);
1333 return paging32_init_context(vcpu);
1336 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1339 return init_kvm_tdp_mmu(vcpu);
1341 return init_kvm_softmmu(vcpu);
1344 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1347 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1348 vcpu->arch.mmu.free(vcpu);
1349 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1353 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1355 destroy_kvm_mmu(vcpu);
1356 return init_kvm_mmu(vcpu);
1358 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
1360 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1364 r = mmu_topup_memory_caches(vcpu);
1367 spin_lock(&vcpu->kvm->mmu_lock);
1368 kvm_mmu_free_some_pages(vcpu);
1369 mmu_alloc_roots(vcpu);
1370 spin_unlock(&vcpu->kvm->mmu_lock);
1371 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
1372 kvm_mmu_flush_tlb(vcpu);
1376 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1378 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1380 mmu_free_roots(vcpu);
1383 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1384 struct kvm_mmu_page *sp,
1388 struct kvm_mmu_page *child;
1391 if (is_shadow_present_pte(pte)) {
1392 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
1393 rmap_remove(vcpu->kvm, spte);
1395 child = page_header(pte & PT64_BASE_ADDR_MASK);
1396 mmu_page_remove_parent_pte(child, spte);
1399 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
1402 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1403 struct kvm_mmu_page *sp,
1407 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1408 ++vcpu->kvm->stat.mmu_pde_zapped;
1412 ++vcpu->kvm->stat.mmu_pte_updated;
1413 if (sp->role.glevels == PT32_ROOT_LEVEL)
1414 paging32_update_pte(vcpu, sp, spte, new);
1416 paging64_update_pte(vcpu, sp, spte, new);
1419 static bool need_remote_flush(u64 old, u64 new)
1421 if (!is_shadow_present_pte(old))
1423 if (!is_shadow_present_pte(new))
1425 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1427 old ^= PT64_NX_MASK;
1428 new ^= PT64_NX_MASK;
1429 return (old & ~new & PT64_PERM_MASK) != 0;
1432 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1434 if (need_remote_flush(old, new))
1435 kvm_flush_remote_tlbs(vcpu->kvm);
1437 kvm_mmu_flush_tlb(vcpu);
1440 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1442 u64 *spte = vcpu->arch.last_pte_updated;
1444 return !!(spte && (*spte & PT_ACCESSED_MASK));
1447 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1448 const u8 *new, int bytes)
1455 if (bytes != 4 && bytes != 8)
1459 * Assume that the pte write on a page table of the same type
1460 * as the current vcpu paging mode. This is nearly always true
1461 * (might be false while changing modes). Note it is verified later
1465 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1466 if ((bytes == 4) && (gpa % 4 == 0)) {
1467 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1470 memcpy((void *)&gpte + (gpa % 8), new, 4);
1471 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1472 memcpy((void *)&gpte, new, 8);
1475 if ((bytes == 4) && (gpa % 4 == 0))
1476 memcpy((void *)&gpte, new, 4);
1478 if (!is_present_pte(gpte))
1480 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1482 down_read(&vcpu->kvm->slots_lock);
1483 page = gfn_to_page(vcpu->kvm, gfn);
1484 up_read(&vcpu->kvm->slots_lock);
1486 if (is_error_page(page)) {
1487 kvm_release_page_clean(page);
1490 vcpu->arch.update_pte.gfn = gfn;
1491 vcpu->arch.update_pte.page = page;
1494 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1495 const u8 *new, int bytes)
1497 gfn_t gfn = gpa >> PAGE_SHIFT;
1498 struct kvm_mmu_page *sp;
1499 struct hlist_node *node, *n;
1500 struct hlist_head *bucket;
1504 unsigned offset = offset_in_page(gpa);
1506 unsigned page_offset;
1507 unsigned misaligned;
1514 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
1515 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
1516 spin_lock(&vcpu->kvm->mmu_lock);
1517 kvm_mmu_free_some_pages(vcpu);
1518 ++vcpu->kvm->stat.mmu_pte_write;
1519 kvm_mmu_audit(vcpu, "pre pte write");
1520 if (gfn == vcpu->arch.last_pt_write_gfn
1521 && !last_updated_pte_accessed(vcpu)) {
1522 ++vcpu->arch.last_pt_write_count;
1523 if (vcpu->arch.last_pt_write_count >= 3)
1526 vcpu->arch.last_pt_write_gfn = gfn;
1527 vcpu->arch.last_pt_write_count = 1;
1528 vcpu->arch.last_pte_updated = NULL;
1530 index = kvm_page_table_hashfn(gfn);
1531 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1532 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1533 if (sp->gfn != gfn || sp->role.metaphysical)
1535 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1536 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1537 misaligned |= bytes < 4;
1538 if (misaligned || flooded) {
1540 * Misaligned accesses are too much trouble to fix
1541 * up; also, they usually indicate a page is not used
1544 * If we're seeing too many writes to a page,
1545 * it may no longer be a page table, or we may be
1546 * forking, in which case it is better to unmap the
1549 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1550 gpa, bytes, sp->role.word);
1551 kvm_mmu_zap_page(vcpu->kvm, sp);
1552 ++vcpu->kvm->stat.mmu_flooded;
1555 page_offset = offset;
1556 level = sp->role.level;
1558 if (sp->role.glevels == PT32_ROOT_LEVEL) {
1559 page_offset <<= 1; /* 32->64 */
1561 * A 32-bit pde maps 4MB while the shadow pdes map
1562 * only 2MB. So we need to double the offset again
1563 * and zap two pdes instead of one.
1565 if (level == PT32_ROOT_LEVEL) {
1566 page_offset &= ~7; /* kill rounding error */
1570 quadrant = page_offset >> PAGE_SHIFT;
1571 page_offset &= ~PAGE_MASK;
1572 if (quadrant != sp->role.quadrant)
1575 spte = &sp->spt[page_offset / sizeof(*spte)];
1576 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1578 r = kvm_read_guest_atomic(vcpu->kvm,
1579 gpa & ~(u64)(pte_size - 1),
1581 new = (const void *)&gentry;
1587 mmu_pte_write_zap_pte(vcpu, sp, spte);
1589 mmu_pte_write_new_pte(vcpu, sp, spte, new);
1590 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
1594 kvm_mmu_audit(vcpu, "post pte write");
1595 spin_unlock(&vcpu->kvm->mmu_lock);
1596 if (vcpu->arch.update_pte.page) {
1597 kvm_release_page_clean(vcpu->arch.update_pte.page);
1598 vcpu->arch.update_pte.page = NULL;
1602 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1607 down_read(&vcpu->kvm->slots_lock);
1608 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1609 up_read(&vcpu->kvm->slots_lock);
1611 spin_lock(&vcpu->kvm->mmu_lock);
1612 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1613 spin_unlock(&vcpu->kvm->mmu_lock);
1617 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1619 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
1620 struct kvm_mmu_page *sp;
1622 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
1623 struct kvm_mmu_page, link);
1624 kvm_mmu_zap_page(vcpu->kvm, sp);
1625 ++vcpu->kvm->stat.mmu_recycled;
1629 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1632 enum emulation_result er;
1634 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
1643 r = mmu_topup_memory_caches(vcpu);
1647 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1652 case EMULATE_DO_MMIO:
1653 ++vcpu->stat.mmio_exits;
1656 kvm_report_emulation_failure(vcpu, "pagetable");
1664 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1666 void kvm_enable_tdp(void)
1670 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1672 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1674 struct kvm_mmu_page *sp;
1676 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1677 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
1678 struct kvm_mmu_page, link);
1679 kvm_mmu_zap_page(vcpu->kvm, sp);
1681 free_page((unsigned long)vcpu->arch.mmu.pae_root);
1684 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1691 if (vcpu->kvm->arch.n_requested_mmu_pages)
1692 vcpu->kvm->arch.n_free_mmu_pages =
1693 vcpu->kvm->arch.n_requested_mmu_pages;
1695 vcpu->kvm->arch.n_free_mmu_pages =
1696 vcpu->kvm->arch.n_alloc_mmu_pages;
1698 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1699 * Therefore we need to allocate shadow page tables in the first
1700 * 4GB of memory, which happens to fit the DMA32 zone.
1702 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1705 vcpu->arch.mmu.pae_root = page_address(page);
1706 for (i = 0; i < 4; ++i)
1707 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1712 free_mmu_pages(vcpu);
1716 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1719 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1721 return alloc_mmu_pages(vcpu);
1724 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1727 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1729 return init_kvm_mmu(vcpu);
1732 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1736 destroy_kvm_mmu(vcpu);
1737 free_mmu_pages(vcpu);
1738 mmu_free_memory_caches(vcpu);
1741 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1743 struct kvm_mmu_page *sp;
1745 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
1749 if (!test_bit(slot, &sp->slot_bitmap))
1753 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1755 if (pt[i] & PT_WRITABLE_MASK)
1756 pt[i] &= ~PT_WRITABLE_MASK;
1760 void kvm_mmu_zap_all(struct kvm *kvm)
1762 struct kvm_mmu_page *sp, *node;
1764 spin_lock(&kvm->mmu_lock);
1765 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
1766 kvm_mmu_zap_page(kvm, sp);
1767 spin_unlock(&kvm->mmu_lock);
1769 kvm_flush_remote_tlbs(kvm);
1772 void kvm_mmu_module_exit(void)
1774 if (pte_chain_cache)
1775 kmem_cache_destroy(pte_chain_cache);
1776 if (rmap_desc_cache)
1777 kmem_cache_destroy(rmap_desc_cache);
1778 if (mmu_page_header_cache)
1779 kmem_cache_destroy(mmu_page_header_cache);
1782 int kvm_mmu_module_init(void)
1784 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1785 sizeof(struct kvm_pte_chain),
1787 if (!pte_chain_cache)
1789 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1790 sizeof(struct kvm_rmap_desc),
1792 if (!rmap_desc_cache)
1795 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1796 sizeof(struct kvm_mmu_page),
1798 if (!mmu_page_header_cache)
1804 kvm_mmu_module_exit();
1809 * Caculate mmu pages needed for kvm.
1811 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1814 unsigned int nr_mmu_pages;
1815 unsigned int nr_pages = 0;
1817 for (i = 0; i < kvm->nmemslots; i++)
1818 nr_pages += kvm->memslots[i].npages;
1820 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1821 nr_mmu_pages = max(nr_mmu_pages,
1822 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1824 return nr_mmu_pages;
1829 static const char *audit_msg;
1831 static gva_t canonicalize(gva_t gva)
1833 #ifdef CONFIG_X86_64
1834 gva = (long long)(gva << 16) >> 16;
1839 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1840 gva_t va, int level)
1842 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1844 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1846 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1849 if (ent == shadow_trap_nonpresent_pte)
1852 va = canonicalize(va);
1854 if (ent == shadow_notrap_nonpresent_pte)
1855 printk(KERN_ERR "audit: (%s) nontrapping pte"
1856 " in nonleaf level: levels %d gva %lx"
1857 " level %d pte %llx\n", audit_msg,
1858 vcpu->arch.mmu.root_level, va, level, ent);
1860 audit_mappings_page(vcpu, ent, va, level - 1);
1862 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
1863 struct page *page = gpa_to_page(vcpu, gpa);
1864 hpa_t hpa = page_to_phys(page);
1866 if (is_shadow_present_pte(ent)
1867 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1868 printk(KERN_ERR "xx audit error: (%s) levels %d"
1869 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
1870 audit_msg, vcpu->arch.mmu.root_level,
1872 is_shadow_present_pte(ent));
1873 else if (ent == shadow_notrap_nonpresent_pte
1874 && !is_error_hpa(hpa))
1875 printk(KERN_ERR "audit: (%s) notrap shadow,"
1876 " valid guest gva %lx\n", audit_msg, va);
1877 kvm_release_page_clean(page);
1883 static void audit_mappings(struct kvm_vcpu *vcpu)
1887 if (vcpu->arch.mmu.root_level == 4)
1888 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
1890 for (i = 0; i < 4; ++i)
1891 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
1892 audit_mappings_page(vcpu,
1893 vcpu->arch.mmu.pae_root[i],
1898 static int count_rmaps(struct kvm_vcpu *vcpu)
1903 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1904 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1905 struct kvm_rmap_desc *d;
1907 for (j = 0; j < m->npages; ++j) {
1908 unsigned long *rmapp = &m->rmap[j];
1912 if (!(*rmapp & 1)) {
1916 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
1918 for (k = 0; k < RMAP_EXT; ++k)
1919 if (d->shadow_ptes[k])
1930 static int count_writable_mappings(struct kvm_vcpu *vcpu)
1933 struct kvm_mmu_page *sp;
1936 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
1939 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
1942 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1945 if (!(ent & PT_PRESENT_MASK))
1947 if (!(ent & PT_WRITABLE_MASK))
1955 static void audit_rmap(struct kvm_vcpu *vcpu)
1957 int n_rmap = count_rmaps(vcpu);
1958 int n_actual = count_writable_mappings(vcpu);
1960 if (n_rmap != n_actual)
1961 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1962 __FUNCTION__, audit_msg, n_rmap, n_actual);
1965 static void audit_write_protection(struct kvm_vcpu *vcpu)
1967 struct kvm_mmu_page *sp;
1968 struct kvm_memory_slot *slot;
1969 unsigned long *rmapp;
1972 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
1973 if (sp->role.metaphysical)
1976 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1977 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
1978 rmapp = &slot->rmap[gfn - slot->base_gfn];
1980 printk(KERN_ERR "%s: (%s) shadow page has writable"
1981 " mappings: gfn %lx role %x\n",
1982 __FUNCTION__, audit_msg, sp->gfn,
1987 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1994 audit_write_protection(vcpu);
1995 audit_mappings(vcpu);