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/types.h>
24 #include <linux/string.h>
26 #include <linux/highmem.h>
27 #include <linux/module.h>
30 #include <asm/cmpxchg.h>
37 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
39 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
44 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
45 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
49 #define pgprintk(x...) do { } while (0)
50 #define rmap_printk(x...) do { } while (0)
54 #if defined(MMU_DEBUG) || defined(AUDIT)
59 #define ASSERT(x) do { } while (0)
63 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
64 __FILE__, __LINE__, #x); \
68 #define PT64_PT_BITS 9
69 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
70 #define PT32_PT_BITS 10
71 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
73 #define PT_WRITABLE_SHIFT 1
75 #define PT_PRESENT_MASK (1ULL << 0)
76 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
77 #define PT_USER_MASK (1ULL << 2)
78 #define PT_PWT_MASK (1ULL << 3)
79 #define PT_PCD_MASK (1ULL << 4)
80 #define PT_ACCESSED_MASK (1ULL << 5)
81 #define PT_DIRTY_MASK (1ULL << 6)
82 #define PT_PAGE_SIZE_MASK (1ULL << 7)
83 #define PT_PAT_MASK (1ULL << 7)
84 #define PT_GLOBAL_MASK (1ULL << 8)
85 #define PT64_NX_MASK (1ULL << 63)
87 #define PT_PAT_SHIFT 7
88 #define PT_DIR_PAT_SHIFT 12
89 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
91 #define PT32_DIR_PSE36_SIZE 4
92 #define PT32_DIR_PSE36_SHIFT 13
93 #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
96 #define PT_FIRST_AVAIL_BITS_SHIFT 9
97 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
99 #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
101 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
103 #define PT64_LEVEL_BITS 9
105 #define PT64_LEVEL_SHIFT(level) \
106 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
108 #define PT64_LEVEL_MASK(level) \
109 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
111 #define PT64_INDEX(address, level)\
112 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
115 #define PT32_LEVEL_BITS 10
117 #define PT32_LEVEL_SHIFT(level) \
118 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
120 #define PT32_LEVEL_MASK(level) \
121 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
123 #define PT32_INDEX(address, level)\
124 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
127 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
128 #define PT64_DIR_BASE_ADDR_MASK \
129 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
131 #define PT32_BASE_ADDR_MASK PAGE_MASK
132 #define PT32_DIR_BASE_ADDR_MASK \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
136 #define PFERR_PRESENT_MASK (1U << 0)
137 #define PFERR_WRITE_MASK (1U << 1)
138 #define PFERR_USER_MASK (1U << 2)
139 #define PFERR_FETCH_MASK (1U << 4)
141 #define PT64_ROOT_LEVEL 4
142 #define PT32_ROOT_LEVEL 2
143 #define PT32E_ROOT_LEVEL 3
145 #define PT_DIRECTORY_LEVEL 2
146 #define PT_PAGE_TABLE_LEVEL 1
150 struct kvm_rmap_desc {
151 u64 *shadow_ptes[RMAP_EXT];
152 struct kvm_rmap_desc *more;
155 static struct kmem_cache *pte_chain_cache;
156 static struct kmem_cache *rmap_desc_cache;
157 static struct kmem_cache *mmu_page_header_cache;
159 static int is_write_protection(struct kvm_vcpu *vcpu)
161 return vcpu->cr0 & CR0_WP_MASK;
164 static int is_cpuid_PSE36(void)
169 static int is_nx(struct kvm_vcpu *vcpu)
171 return vcpu->shadow_efer & EFER_NX;
174 static int is_present_pte(unsigned long pte)
176 return pte & PT_PRESENT_MASK;
179 static int is_writeble_pte(unsigned long pte)
181 return pte & PT_WRITABLE_MASK;
184 static int is_io_pte(unsigned long pte)
186 return pte & PT_SHADOW_IO_MARK;
189 static int is_rmap_pte(u64 pte)
191 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
192 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
195 static void set_shadow_pte(u64 *sptep, u64 spte)
198 set_64bit((unsigned long *)sptep, spte);
200 set_64bit((unsigned long long *)sptep, spte);
204 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
205 struct kmem_cache *base_cache, int min,
210 if (cache->nobjs >= min)
212 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
213 obj = kmem_cache_zalloc(base_cache, gfp_flags);
216 cache->objects[cache->nobjs++] = obj;
221 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
224 kfree(mc->objects[--mc->nobjs]);
227 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
228 int min, gfp_t gfp_flags)
232 if (cache->nobjs >= min)
234 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
235 page = alloc_page(gfp_flags);
238 set_page_private(page, 0);
239 cache->objects[cache->nobjs++] = page_address(page);
244 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
247 free_page((unsigned long)mc->objects[--mc->nobjs]);
250 static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
254 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
255 pte_chain_cache, 4, gfp_flags);
258 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
259 rmap_desc_cache, 1, gfp_flags);
262 r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 4, gfp_flags);
265 r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
266 mmu_page_header_cache, 4, gfp_flags);
271 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
275 r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
277 spin_unlock(&vcpu->kvm->lock);
278 kvm_arch_ops->vcpu_put(vcpu);
279 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
280 kvm_arch_ops->vcpu_load(vcpu);
281 spin_lock(&vcpu->kvm->lock);
286 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
288 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
289 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
290 mmu_free_memory_cache_page(&vcpu->mmu_page_cache);
291 mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
294 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
300 p = mc->objects[--mc->nobjs];
305 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
307 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
308 sizeof(struct kvm_pte_chain));
311 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
316 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
318 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
319 sizeof(struct kvm_rmap_desc));
322 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
328 * Reverse mapping data structures:
330 * If page->private bit zero is zero, then page->private points to the
331 * shadow page table entry that points to page_address(page).
333 * If page->private bit zero is one, (then page->private & ~1) points
334 * to a struct kvm_rmap_desc containing more mappings.
336 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
339 struct kvm_rmap_desc *desc;
342 if (!is_rmap_pte(*spte))
344 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
345 if (!page_private(page)) {
346 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
347 set_page_private(page,(unsigned long)spte);
348 } else if (!(page_private(page) & 1)) {
349 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
350 desc = mmu_alloc_rmap_desc(vcpu);
351 desc->shadow_ptes[0] = (u64 *)page_private(page);
352 desc->shadow_ptes[1] = spte;
353 set_page_private(page,(unsigned long)desc | 1);
355 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
356 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
357 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
359 if (desc->shadow_ptes[RMAP_EXT-1]) {
360 desc->more = mmu_alloc_rmap_desc(vcpu);
363 for (i = 0; desc->shadow_ptes[i]; ++i)
365 desc->shadow_ptes[i] = spte;
369 static void rmap_desc_remove_entry(struct page *page,
370 struct kvm_rmap_desc *desc,
372 struct kvm_rmap_desc *prev_desc)
376 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
378 desc->shadow_ptes[i] = desc->shadow_ptes[j];
379 desc->shadow_ptes[j] = NULL;
382 if (!prev_desc && !desc->more)
383 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
386 prev_desc->more = desc->more;
388 set_page_private(page,(unsigned long)desc->more | 1);
389 mmu_free_rmap_desc(desc);
392 static void rmap_remove(u64 *spte)
395 struct kvm_rmap_desc *desc;
396 struct kvm_rmap_desc *prev_desc;
399 if (!is_rmap_pte(*spte))
401 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
402 if (!page_private(page)) {
403 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
405 } else if (!(page_private(page) & 1)) {
406 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
407 if ((u64 *)page_private(page) != spte) {
408 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
412 set_page_private(page,0);
414 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
415 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
418 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
419 if (desc->shadow_ptes[i] == spte) {
420 rmap_desc_remove_entry(page,
432 static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
434 struct kvm *kvm = vcpu->kvm;
436 struct kvm_rmap_desc *desc;
439 page = gfn_to_page(kvm, gfn);
442 while (page_private(page)) {
443 if (!(page_private(page) & 1))
444 spte = (u64 *)page_private(page);
446 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
447 spte = desc->shadow_ptes[0];
450 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
451 != page_to_pfn(page));
452 BUG_ON(!(*spte & PT_PRESENT_MASK));
453 BUG_ON(!(*spte & PT_WRITABLE_MASK));
454 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
456 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
457 kvm_flush_remote_tlbs(vcpu->kvm);
462 static int is_empty_shadow_page(u64 *spt)
467 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
469 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
477 static void kvm_mmu_free_page(struct kvm *kvm,
478 struct kvm_mmu_page *page_head)
480 ASSERT(is_empty_shadow_page(page_head->spt));
481 list_del(&page_head->link);
482 __free_page(virt_to_page(page_head->spt));
484 ++kvm->n_free_mmu_pages;
487 static unsigned kvm_page_table_hashfn(gfn_t gfn)
492 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
495 struct kvm_mmu_page *page;
497 if (!vcpu->kvm->n_free_mmu_pages)
500 page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
502 page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
503 set_page_private(virt_to_page(page->spt), (unsigned long)page);
504 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
505 ASSERT(is_empty_shadow_page(page->spt));
506 page->slot_bitmap = 0;
507 page->multimapped = 0;
508 page->parent_pte = parent_pte;
509 --vcpu->kvm->n_free_mmu_pages;
513 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
514 struct kvm_mmu_page *page, u64 *parent_pte)
516 struct kvm_pte_chain *pte_chain;
517 struct hlist_node *node;
522 if (!page->multimapped) {
523 u64 *old = page->parent_pte;
526 page->parent_pte = parent_pte;
529 page->multimapped = 1;
530 pte_chain = mmu_alloc_pte_chain(vcpu);
531 INIT_HLIST_HEAD(&page->parent_ptes);
532 hlist_add_head(&pte_chain->link, &page->parent_ptes);
533 pte_chain->parent_ptes[0] = old;
535 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
536 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
538 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
539 if (!pte_chain->parent_ptes[i]) {
540 pte_chain->parent_ptes[i] = parent_pte;
544 pte_chain = mmu_alloc_pte_chain(vcpu);
546 hlist_add_head(&pte_chain->link, &page->parent_ptes);
547 pte_chain->parent_ptes[0] = parent_pte;
550 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
553 struct kvm_pte_chain *pte_chain;
554 struct hlist_node *node;
557 if (!page->multimapped) {
558 BUG_ON(page->parent_pte != parent_pte);
559 page->parent_pte = NULL;
562 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
563 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
564 if (!pte_chain->parent_ptes[i])
566 if (pte_chain->parent_ptes[i] != parent_pte)
568 while (i + 1 < NR_PTE_CHAIN_ENTRIES
569 && pte_chain->parent_ptes[i + 1]) {
570 pte_chain->parent_ptes[i]
571 = pte_chain->parent_ptes[i + 1];
574 pte_chain->parent_ptes[i] = NULL;
576 hlist_del(&pte_chain->link);
577 mmu_free_pte_chain(pte_chain);
578 if (hlist_empty(&page->parent_ptes)) {
579 page->multimapped = 0;
580 page->parent_pte = NULL;
588 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
592 struct hlist_head *bucket;
593 struct kvm_mmu_page *page;
594 struct hlist_node *node;
596 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
597 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
598 bucket = &vcpu->kvm->mmu_page_hash[index];
599 hlist_for_each_entry(page, node, bucket, hash_link)
600 if (page->gfn == gfn && !page->role.metaphysical) {
601 pgprintk("%s: found role %x\n",
602 __FUNCTION__, page->role.word);
608 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
613 unsigned hugepage_access,
616 union kvm_mmu_page_role role;
619 struct hlist_head *bucket;
620 struct kvm_mmu_page *page;
621 struct hlist_node *node;
624 role.glevels = vcpu->mmu.root_level;
626 role.metaphysical = metaphysical;
627 role.hugepage_access = hugepage_access;
628 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
629 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
630 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
631 role.quadrant = quadrant;
633 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
635 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
636 bucket = &vcpu->kvm->mmu_page_hash[index];
637 hlist_for_each_entry(page, node, bucket, hash_link)
638 if (page->gfn == gfn && page->role.word == role.word) {
639 mmu_page_add_parent_pte(vcpu, page, parent_pte);
640 pgprintk("%s: found\n", __FUNCTION__);
643 page = kvm_mmu_alloc_page(vcpu, parent_pte);
646 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
649 hlist_add_head(&page->hash_link, bucket);
651 rmap_write_protect(vcpu, gfn);
655 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
656 struct kvm_mmu_page *page)
664 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
665 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
666 if (pt[i] & PT_PRESENT_MASK)
670 kvm_flush_remote_tlbs(kvm);
674 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
678 if (!(ent & PT_PRESENT_MASK))
680 ent &= PT64_BASE_ADDR_MASK;
681 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
683 kvm_flush_remote_tlbs(kvm);
686 static void kvm_mmu_put_page(struct kvm_mmu_page *page,
689 mmu_page_remove_parent_pte(page, parent_pte);
692 static void kvm_mmu_zap_page(struct kvm *kvm,
693 struct kvm_mmu_page *page)
697 while (page->multimapped || page->parent_pte) {
698 if (!page->multimapped)
699 parent_pte = page->parent_pte;
701 struct kvm_pte_chain *chain;
703 chain = container_of(page->parent_ptes.first,
704 struct kvm_pte_chain, link);
705 parent_pte = chain->parent_ptes[0];
708 kvm_mmu_put_page(page, parent_pte);
709 set_shadow_pte(parent_pte, 0);
711 kvm_mmu_page_unlink_children(kvm, page);
712 if (!page->root_count) {
713 hlist_del(&page->hash_link);
714 kvm_mmu_free_page(kvm, page);
716 list_move(&page->link, &kvm->active_mmu_pages);
719 static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
722 struct hlist_head *bucket;
723 struct kvm_mmu_page *page;
724 struct hlist_node *node, *n;
727 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
729 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
730 bucket = &vcpu->kvm->mmu_page_hash[index];
731 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
732 if (page->gfn == gfn && !page->role.metaphysical) {
733 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
735 kvm_mmu_zap_page(vcpu->kvm, page);
741 static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn)
743 struct kvm_mmu_page *page;
745 while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) {
746 pgprintk("%s: zap %lx %x\n",
747 __FUNCTION__, gfn, page->role.word);
748 kvm_mmu_zap_page(vcpu->kvm, page);
752 static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
754 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
755 struct kvm_mmu_page *page_head = page_header(__pa(pte));
757 __set_bit(slot, &page_head->slot_bitmap);
760 hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
762 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
764 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
767 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
771 ASSERT((gpa & HPA_ERR_MASK) == 0);
772 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
774 return gpa | HPA_ERR_MASK;
775 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
776 | (gpa & (PAGE_SIZE-1));
779 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
781 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
783 if (gpa == UNMAPPED_GVA)
785 return gpa_to_hpa(vcpu, gpa);
788 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
790 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
792 if (gpa == UNMAPPED_GVA)
794 return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
797 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
801 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
803 int level = PT32E_ROOT_LEVEL;
804 hpa_t table_addr = vcpu->mmu.root_hpa;
807 u32 index = PT64_INDEX(v, level);
811 ASSERT(VALID_PAGE(table_addr));
812 table = __va(table_addr);
816 if (is_present_pte(pte) && is_writeble_pte(pte))
818 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
819 page_header_update_slot(vcpu->kvm, table, v);
820 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
822 rmap_add(vcpu, &table[index]);
826 if (table[index] == 0) {
827 struct kvm_mmu_page *new_table;
830 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
832 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
834 1, 0, &table[index]);
836 pgprintk("nonpaging_map: ENOMEM\n");
840 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
841 | PT_WRITABLE_MASK | PT_USER_MASK;
843 table_addr = table[index] & PT64_BASE_ADDR_MASK;
847 static void mmu_free_roots(struct kvm_vcpu *vcpu)
850 struct kvm_mmu_page *page;
852 if (!VALID_PAGE(vcpu->mmu.root_hpa))
855 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
856 hpa_t root = vcpu->mmu.root_hpa;
858 page = page_header(root);
860 vcpu->mmu.root_hpa = INVALID_PAGE;
864 for (i = 0; i < 4; ++i) {
865 hpa_t root = vcpu->mmu.pae_root[i];
868 root &= PT64_BASE_ADDR_MASK;
869 page = page_header(root);
872 vcpu->mmu.pae_root[i] = INVALID_PAGE;
874 vcpu->mmu.root_hpa = INVALID_PAGE;
877 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
881 struct kvm_mmu_page *page;
883 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
886 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
887 hpa_t root = vcpu->mmu.root_hpa;
889 ASSERT(!VALID_PAGE(root));
890 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
891 PT64_ROOT_LEVEL, 0, 0, NULL);
892 root = __pa(page->spt);
894 vcpu->mmu.root_hpa = root;
898 for (i = 0; i < 4; ++i) {
899 hpa_t root = vcpu->mmu.pae_root[i];
901 ASSERT(!VALID_PAGE(root));
902 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
903 if (!is_present_pte(vcpu->pdptrs[i])) {
904 vcpu->mmu.pae_root[i] = 0;
907 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
908 } else if (vcpu->mmu.root_level == 0)
910 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
911 PT32_ROOT_LEVEL, !is_paging(vcpu),
913 root = __pa(page->spt);
915 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
917 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
920 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
925 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
932 r = mmu_topup_memory_caches(vcpu);
937 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
940 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
942 if (is_error_hpa(paddr))
945 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
948 static void nonpaging_free(struct kvm_vcpu *vcpu)
950 mmu_free_roots(vcpu);
953 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
955 struct kvm_mmu *context = &vcpu->mmu;
957 context->new_cr3 = nonpaging_new_cr3;
958 context->page_fault = nonpaging_page_fault;
959 context->gva_to_gpa = nonpaging_gva_to_gpa;
960 context->free = nonpaging_free;
961 context->root_level = 0;
962 context->shadow_root_level = PT32E_ROOT_LEVEL;
963 context->root_hpa = INVALID_PAGE;
967 static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
969 ++vcpu->stat.tlb_flush;
970 kvm_arch_ops->tlb_flush(vcpu);
973 static void paging_new_cr3(struct kvm_vcpu *vcpu)
975 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
976 mmu_free_roots(vcpu);
979 static void inject_page_fault(struct kvm_vcpu *vcpu,
983 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
986 static void paging_free(struct kvm_vcpu *vcpu)
988 nonpaging_free(vcpu);
992 #include "paging_tmpl.h"
996 #include "paging_tmpl.h"
999 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1001 struct kvm_mmu *context = &vcpu->mmu;
1003 ASSERT(is_pae(vcpu));
1004 context->new_cr3 = paging_new_cr3;
1005 context->page_fault = paging64_page_fault;
1006 context->gva_to_gpa = paging64_gva_to_gpa;
1007 context->free = paging_free;
1008 context->root_level = level;
1009 context->shadow_root_level = level;
1010 context->root_hpa = INVALID_PAGE;
1014 static int paging64_init_context(struct kvm_vcpu *vcpu)
1016 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1019 static int paging32_init_context(struct kvm_vcpu *vcpu)
1021 struct kvm_mmu *context = &vcpu->mmu;
1023 context->new_cr3 = paging_new_cr3;
1024 context->page_fault = paging32_page_fault;
1025 context->gva_to_gpa = paging32_gva_to_gpa;
1026 context->free = paging_free;
1027 context->root_level = PT32_ROOT_LEVEL;
1028 context->shadow_root_level = PT32E_ROOT_LEVEL;
1029 context->root_hpa = INVALID_PAGE;
1033 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1035 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1038 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1041 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1043 if (!is_paging(vcpu))
1044 return nonpaging_init_context(vcpu);
1045 else if (is_long_mode(vcpu))
1046 return paging64_init_context(vcpu);
1047 else if (is_pae(vcpu))
1048 return paging32E_init_context(vcpu);
1050 return paging32_init_context(vcpu);
1053 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1056 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1057 vcpu->mmu.free(vcpu);
1058 vcpu->mmu.root_hpa = INVALID_PAGE;
1062 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1064 destroy_kvm_mmu(vcpu);
1065 return init_kvm_mmu(vcpu);
1068 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1072 spin_lock(&vcpu->kvm->lock);
1073 r = mmu_topup_memory_caches(vcpu);
1076 mmu_alloc_roots(vcpu);
1077 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
1078 kvm_mmu_flush_tlb(vcpu);
1080 spin_unlock(&vcpu->kvm->lock);
1083 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1085 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1087 mmu_free_roots(vcpu);
1090 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1091 struct kvm_mmu_page *page,
1095 struct kvm_mmu_page *child;
1098 if (is_present_pte(pte)) {
1099 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1102 child = page_header(pte & PT64_BASE_ADDR_MASK);
1103 mmu_page_remove_parent_pte(child, spte);
1107 kvm_flush_remote_tlbs(vcpu->kvm);
1110 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1111 struct kvm_mmu_page *page,
1113 const void *new, int bytes)
1115 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1118 if (page->role.glevels == PT32_ROOT_LEVEL)
1119 paging32_update_pte(vcpu, page, spte, new, bytes);
1121 paging64_update_pte(vcpu, page, spte, new, bytes);
1124 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1125 const u8 *old, const u8 *new, int bytes)
1127 gfn_t gfn = gpa >> PAGE_SHIFT;
1128 struct kvm_mmu_page *page;
1129 struct hlist_node *node, *n;
1130 struct hlist_head *bucket;
1133 unsigned offset = offset_in_page(gpa);
1135 unsigned page_offset;
1136 unsigned misaligned;
1142 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
1143 if (gfn == vcpu->last_pt_write_gfn) {
1144 ++vcpu->last_pt_write_count;
1145 if (vcpu->last_pt_write_count >= 3)
1148 vcpu->last_pt_write_gfn = gfn;
1149 vcpu->last_pt_write_count = 1;
1151 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1152 bucket = &vcpu->kvm->mmu_page_hash[index];
1153 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
1154 if (page->gfn != gfn || page->role.metaphysical)
1156 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1157 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1158 misaligned |= bytes < 4;
1159 if (misaligned || flooded) {
1161 * Misaligned accesses are too much trouble to fix
1162 * up; also, they usually indicate a page is not used
1165 * If we're seeing too many writes to a page,
1166 * it may no longer be a page table, or we may be
1167 * forking, in which case it is better to unmap the
1170 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1171 gpa, bytes, page->role.word);
1172 kvm_mmu_zap_page(vcpu->kvm, page);
1175 page_offset = offset;
1176 level = page->role.level;
1178 if (page->role.glevels == PT32_ROOT_LEVEL) {
1179 page_offset <<= 1; /* 32->64 */
1181 * A 32-bit pde maps 4MB while the shadow pdes map
1182 * only 2MB. So we need to double the offset again
1183 * and zap two pdes instead of one.
1185 if (level == PT32_ROOT_LEVEL) {
1186 page_offset &= ~7; /* kill rounding error */
1190 quadrant = page_offset >> PAGE_SHIFT;
1191 page_offset &= ~PAGE_MASK;
1192 if (quadrant != page->role.quadrant)
1195 spte = &page->spt[page_offset / sizeof(*spte)];
1197 mmu_pte_write_zap_pte(vcpu, page, spte);
1198 mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
1204 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1206 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1208 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1211 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1213 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1214 struct kvm_mmu_page *page;
1216 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1217 struct kvm_mmu_page, link);
1218 kvm_mmu_zap_page(vcpu->kvm, page);
1221 EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1223 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1225 struct kvm_mmu_page *page;
1227 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1228 page = container_of(vcpu->kvm->active_mmu_pages.next,
1229 struct kvm_mmu_page, link);
1230 kvm_mmu_zap_page(vcpu->kvm, page);
1232 free_page((unsigned long)vcpu->mmu.pae_root);
1235 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1242 vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES;
1245 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1246 * Therefore we need to allocate shadow page tables in the first
1247 * 4GB of memory, which happens to fit the DMA32 zone.
1249 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1252 vcpu->mmu.pae_root = page_address(page);
1253 for (i = 0; i < 4; ++i)
1254 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1259 free_mmu_pages(vcpu);
1263 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1266 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1268 return alloc_mmu_pages(vcpu);
1271 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1274 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1276 return init_kvm_mmu(vcpu);
1279 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1283 destroy_kvm_mmu(vcpu);
1284 free_mmu_pages(vcpu);
1285 mmu_free_memory_caches(vcpu);
1288 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1290 struct kvm_mmu_page *page;
1292 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1296 if (!test_bit(slot, &page->slot_bitmap))
1300 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1302 if (pt[i] & PT_WRITABLE_MASK) {
1303 rmap_remove(&pt[i]);
1304 pt[i] &= ~PT_WRITABLE_MASK;
1309 void kvm_mmu_zap_all(struct kvm *kvm)
1311 struct kvm_mmu_page *page, *node;
1313 list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link)
1314 kvm_mmu_zap_page(kvm, page);
1316 kvm_flush_remote_tlbs(kvm);
1319 void kvm_mmu_module_exit(void)
1321 if (pte_chain_cache)
1322 kmem_cache_destroy(pte_chain_cache);
1323 if (rmap_desc_cache)
1324 kmem_cache_destroy(rmap_desc_cache);
1325 if (mmu_page_header_cache)
1326 kmem_cache_destroy(mmu_page_header_cache);
1329 int kvm_mmu_module_init(void)
1331 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1332 sizeof(struct kvm_pte_chain),
1334 if (!pte_chain_cache)
1336 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1337 sizeof(struct kvm_rmap_desc),
1339 if (!rmap_desc_cache)
1342 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1343 sizeof(struct kvm_mmu_page),
1345 if (!mmu_page_header_cache)
1351 kvm_mmu_module_exit();
1357 static const char *audit_msg;
1359 static gva_t canonicalize(gva_t gva)
1361 #ifdef CONFIG_X86_64
1362 gva = (long long)(gva << 16) >> 16;
1367 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1368 gva_t va, int level)
1370 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1372 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1374 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1377 if (!(ent & PT_PRESENT_MASK))
1380 va = canonicalize(va);
1382 audit_mappings_page(vcpu, ent, va, level - 1);
1384 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1385 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1387 if ((ent & PT_PRESENT_MASK)
1388 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1389 printk(KERN_ERR "audit error: (%s) levels %d"
1390 " gva %lx gpa %llx hpa %llx ent %llx\n",
1391 audit_msg, vcpu->mmu.root_level,
1397 static void audit_mappings(struct kvm_vcpu *vcpu)
1401 if (vcpu->mmu.root_level == 4)
1402 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1404 for (i = 0; i < 4; ++i)
1405 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1406 audit_mappings_page(vcpu,
1407 vcpu->mmu.pae_root[i],
1412 static int count_rmaps(struct kvm_vcpu *vcpu)
1417 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1418 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1419 struct kvm_rmap_desc *d;
1421 for (j = 0; j < m->npages; ++j) {
1422 struct page *page = m->phys_mem[j];
1426 if (!(page->private & 1)) {
1430 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1432 for (k = 0; k < RMAP_EXT; ++k)
1433 if (d->shadow_ptes[k])
1444 static int count_writable_mappings(struct kvm_vcpu *vcpu)
1447 struct kvm_mmu_page *page;
1450 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1451 u64 *pt = page->spt;
1453 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1456 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1459 if (!(ent & PT_PRESENT_MASK))
1461 if (!(ent & PT_WRITABLE_MASK))
1469 static void audit_rmap(struct kvm_vcpu *vcpu)
1471 int n_rmap = count_rmaps(vcpu);
1472 int n_actual = count_writable_mappings(vcpu);
1474 if (n_rmap != n_actual)
1475 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1476 __FUNCTION__, audit_msg, n_rmap, n_actual);
1479 static void audit_write_protection(struct kvm_vcpu *vcpu)
1481 struct kvm_mmu_page *page;
1483 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1487 if (page->role.metaphysical)
1490 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1492 pg = pfn_to_page(hfn);
1494 printk(KERN_ERR "%s: (%s) shadow page has writable"
1495 " mappings: gfn %lx role %x\n",
1496 __FUNCTION__, audit_msg, page->gfn,
1501 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1508 audit_write_protection(vcpu);
1509 audit_mappings(vcpu);