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.
19 #include <linux/types.h>
20 #include <linux/string.h>
23 #include <linux/highmem.h>
24 #include <linux/module.h>
34 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
36 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
41 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
42 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
46 #define pgprintk(x...) do { } while (0)
47 #define rmap_printk(x...) do { } while (0)
51 #if defined(MMU_DEBUG) || defined(AUDIT)
57 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
58 __FILE__, __LINE__, #x); \
61 #define PT64_PT_BITS 9
62 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
63 #define PT32_PT_BITS 10
64 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
66 #define PT_WRITABLE_SHIFT 1
68 #define PT_PRESENT_MASK (1ULL << 0)
69 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
70 #define PT_USER_MASK (1ULL << 2)
71 #define PT_PWT_MASK (1ULL << 3)
72 #define PT_PCD_MASK (1ULL << 4)
73 #define PT_ACCESSED_MASK (1ULL << 5)
74 #define PT_DIRTY_MASK (1ULL << 6)
75 #define PT_PAGE_SIZE_MASK (1ULL << 7)
76 #define PT_PAT_MASK (1ULL << 7)
77 #define PT_GLOBAL_MASK (1ULL << 8)
78 #define PT64_NX_MASK (1ULL << 63)
80 #define PT_PAT_SHIFT 7
81 #define PT_DIR_PAT_SHIFT 12
82 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
84 #define PT32_DIR_PSE36_SIZE 4
85 #define PT32_DIR_PSE36_SHIFT 13
86 #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
89 #define PT32_PTE_COPY_MASK \
90 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
92 #define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
94 #define PT_FIRST_AVAIL_BITS_SHIFT 9
95 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
97 #define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
98 #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
100 #define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
101 #define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
103 #define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
104 #define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
106 #define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
108 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
110 #define PT64_LEVEL_BITS 9
112 #define PT64_LEVEL_SHIFT(level) \
113 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
115 #define PT64_LEVEL_MASK(level) \
116 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
118 #define PT64_INDEX(address, level)\
119 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
122 #define PT32_LEVEL_BITS 10
124 #define PT32_LEVEL_SHIFT(level) \
125 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
127 #define PT32_LEVEL_MASK(level) \
128 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
130 #define PT32_INDEX(address, level)\
131 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
134 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
135 #define PT64_DIR_BASE_ADDR_MASK \
136 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
138 #define PT32_BASE_ADDR_MASK PAGE_MASK
139 #define PT32_DIR_BASE_ADDR_MASK \
140 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
143 #define PFERR_PRESENT_MASK (1U << 0)
144 #define PFERR_WRITE_MASK (1U << 1)
145 #define PFERR_USER_MASK (1U << 2)
146 #define PFERR_FETCH_MASK (1U << 4)
148 #define PT64_ROOT_LEVEL 4
149 #define PT32_ROOT_LEVEL 2
150 #define PT32E_ROOT_LEVEL 3
152 #define PT_DIRECTORY_LEVEL 2
153 #define PT_PAGE_TABLE_LEVEL 1
157 struct kvm_rmap_desc {
158 u64 *shadow_ptes[RMAP_EXT];
159 struct kvm_rmap_desc *more;
162 static int is_write_protection(struct kvm_vcpu *vcpu)
164 return vcpu->cr0 & CR0_WP_MASK;
167 static int is_cpuid_PSE36(void)
172 static int is_nx(struct kvm_vcpu *vcpu)
174 return vcpu->shadow_efer & EFER_NX;
177 static int is_present_pte(unsigned long pte)
179 return pte & PT_PRESENT_MASK;
182 static int is_writeble_pte(unsigned long pte)
184 return pte & PT_WRITABLE_MASK;
187 static int is_io_pte(unsigned long pte)
189 return pte & PT_SHADOW_IO_MARK;
192 static int is_rmap_pte(u64 pte)
194 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
195 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
198 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
199 size_t objsize, int min)
203 if (cache->nobjs >= min)
205 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
206 obj = kzalloc(objsize, GFP_NOWAIT);
209 cache->objects[cache->nobjs++] = obj;
214 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
217 kfree(mc->objects[--mc->nobjs]);
220 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
224 r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
225 sizeof(struct kvm_pte_chain), 4);
228 r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
229 sizeof(struct kvm_rmap_desc), 1);
234 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
236 mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
237 mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
240 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
246 p = mc->objects[--mc->nobjs];
251 static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
253 if (mc->nobjs < KVM_NR_MEM_OBJS)
254 mc->objects[mc->nobjs++] = obj;
259 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
261 return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
262 sizeof(struct kvm_pte_chain));
265 static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
266 struct kvm_pte_chain *pc)
268 mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
271 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
273 return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
274 sizeof(struct kvm_rmap_desc));
277 static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
278 struct kvm_rmap_desc *rd)
280 mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
284 * Reverse mapping data structures:
286 * If page->private bit zero is zero, then page->private points to the
287 * shadow page table entry that points to page_address(page).
289 * If page->private bit zero is one, (then page->private & ~1) points
290 * to a struct kvm_rmap_desc containing more mappings.
292 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
295 struct kvm_rmap_desc *desc;
298 if (!is_rmap_pte(*spte))
300 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
301 if (!page_private(page)) {
302 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
303 set_page_private(page,(unsigned long)spte);
304 } else if (!(page_private(page) & 1)) {
305 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
306 desc = mmu_alloc_rmap_desc(vcpu);
307 desc->shadow_ptes[0] = (u64 *)page_private(page);
308 desc->shadow_ptes[1] = spte;
309 set_page_private(page,(unsigned long)desc | 1);
311 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
312 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
313 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
315 if (desc->shadow_ptes[RMAP_EXT-1]) {
316 desc->more = mmu_alloc_rmap_desc(vcpu);
319 for (i = 0; desc->shadow_ptes[i]; ++i)
321 desc->shadow_ptes[i] = spte;
325 static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
327 struct kvm_rmap_desc *desc,
329 struct kvm_rmap_desc *prev_desc)
333 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
335 desc->shadow_ptes[i] = desc->shadow_ptes[j];
336 desc->shadow_ptes[j] = NULL;
339 if (!prev_desc && !desc->more)
340 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
343 prev_desc->more = desc->more;
345 set_page_private(page,(unsigned long)desc->more | 1);
346 mmu_free_rmap_desc(vcpu, desc);
349 static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
352 struct kvm_rmap_desc *desc;
353 struct kvm_rmap_desc *prev_desc;
356 if (!is_rmap_pte(*spte))
358 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
359 if (!page_private(page)) {
360 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
362 } else if (!(page_private(page) & 1)) {
363 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
364 if ((u64 *)page_private(page) != spte) {
365 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
369 set_page_private(page,0);
371 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
372 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
375 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
376 if (desc->shadow_ptes[i] == spte) {
377 rmap_desc_remove_entry(vcpu, page,
389 static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
391 struct kvm *kvm = vcpu->kvm;
393 struct kvm_memory_slot *slot;
394 struct kvm_rmap_desc *desc;
397 slot = gfn_to_memslot(kvm, gfn);
399 page = gfn_to_page(slot, gfn);
401 while (page_private(page)) {
402 if (!(page_private(page) & 1))
403 spte = (u64 *)page_private(page);
405 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
406 spte = desc->shadow_ptes[0];
409 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
410 != page_to_pfn(page));
411 BUG_ON(!(*spte & PT_PRESENT_MASK));
412 BUG_ON(!(*spte & PT_WRITABLE_MASK));
413 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
414 rmap_remove(vcpu, spte);
415 kvm_arch_ops->tlb_flush(vcpu);
416 *spte &= ~(u64)PT_WRITABLE_MASK;
420 static int is_empty_shadow_page(hpa_t page_hpa)
425 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64);
428 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
435 static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
437 struct kvm_mmu_page *page_head = page_header(page_hpa);
439 ASSERT(is_empty_shadow_page(page_hpa));
440 list_del(&page_head->link);
441 page_head->page_hpa = page_hpa;
442 list_add(&page_head->link, &vcpu->free_pages);
443 ++vcpu->kvm->n_free_mmu_pages;
446 static unsigned kvm_page_table_hashfn(gfn_t gfn)
451 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
454 struct kvm_mmu_page *page;
456 if (list_empty(&vcpu->free_pages))
459 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
460 list_del(&page->link);
461 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
462 ASSERT(is_empty_shadow_page(page->page_hpa));
463 page->slot_bitmap = 0;
465 page->multimapped = 0;
466 page->parent_pte = parent_pte;
467 --vcpu->kvm->n_free_mmu_pages;
471 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
472 struct kvm_mmu_page *page, u64 *parent_pte)
474 struct kvm_pte_chain *pte_chain;
475 struct hlist_node *node;
480 if (!page->multimapped) {
481 u64 *old = page->parent_pte;
484 page->parent_pte = parent_pte;
487 page->multimapped = 1;
488 pte_chain = mmu_alloc_pte_chain(vcpu);
489 INIT_HLIST_HEAD(&page->parent_ptes);
490 hlist_add_head(&pte_chain->link, &page->parent_ptes);
491 pte_chain->parent_ptes[0] = old;
493 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
494 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
496 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
497 if (!pte_chain->parent_ptes[i]) {
498 pte_chain->parent_ptes[i] = parent_pte;
502 pte_chain = mmu_alloc_pte_chain(vcpu);
504 hlist_add_head(&pte_chain->link, &page->parent_ptes);
505 pte_chain->parent_ptes[0] = parent_pte;
508 static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
509 struct kvm_mmu_page *page,
512 struct kvm_pte_chain *pte_chain;
513 struct hlist_node *node;
516 if (!page->multimapped) {
517 BUG_ON(page->parent_pte != parent_pte);
518 page->parent_pte = NULL;
521 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
522 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
523 if (!pte_chain->parent_ptes[i])
525 if (pte_chain->parent_ptes[i] != parent_pte)
527 while (i + 1 < NR_PTE_CHAIN_ENTRIES
528 && pte_chain->parent_ptes[i + 1]) {
529 pte_chain->parent_ptes[i]
530 = pte_chain->parent_ptes[i + 1];
533 pte_chain->parent_ptes[i] = NULL;
535 hlist_del(&pte_chain->link);
536 mmu_free_pte_chain(vcpu, pte_chain);
537 if (hlist_empty(&page->parent_ptes)) {
538 page->multimapped = 0;
539 page->parent_pte = NULL;
547 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
551 struct hlist_head *bucket;
552 struct kvm_mmu_page *page;
553 struct hlist_node *node;
555 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
556 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
557 bucket = &vcpu->kvm->mmu_page_hash[index];
558 hlist_for_each_entry(page, node, bucket, hash_link)
559 if (page->gfn == gfn && !page->role.metaphysical) {
560 pgprintk("%s: found role %x\n",
561 __FUNCTION__, page->role.word);
567 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
574 union kvm_mmu_page_role role;
577 struct hlist_head *bucket;
578 struct kvm_mmu_page *page;
579 struct hlist_node *node;
582 role.glevels = vcpu->mmu.root_level;
584 role.metaphysical = metaphysical;
585 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
586 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
587 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
588 role.quadrant = quadrant;
590 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
592 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
593 bucket = &vcpu->kvm->mmu_page_hash[index];
594 hlist_for_each_entry(page, node, bucket, hash_link)
595 if (page->gfn == gfn && page->role.word == role.word) {
596 mmu_page_add_parent_pte(vcpu, page, parent_pte);
597 pgprintk("%s: found\n", __FUNCTION__);
600 page = kvm_mmu_alloc_page(vcpu, parent_pte);
603 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
606 hlist_add_head(&page->hash_link, bucket);
608 rmap_write_protect(vcpu, gfn);
612 static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
613 struct kvm_mmu_page *page)
619 pt = __va(page->page_hpa);
621 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
622 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
623 if (pt[i] & PT_PRESENT_MASK)
624 rmap_remove(vcpu, &pt[i]);
627 kvm_arch_ops->tlb_flush(vcpu);
631 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
635 if (!(ent & PT_PRESENT_MASK))
637 ent &= PT64_BASE_ADDR_MASK;
638 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
642 static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
643 struct kvm_mmu_page *page,
646 mmu_page_remove_parent_pte(vcpu, page, parent_pte);
649 static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
650 struct kvm_mmu_page *page)
654 while (page->multimapped || page->parent_pte) {
655 if (!page->multimapped)
656 parent_pte = page->parent_pte;
658 struct kvm_pte_chain *chain;
660 chain = container_of(page->parent_ptes.first,
661 struct kvm_pte_chain, link);
662 parent_pte = chain->parent_ptes[0];
665 kvm_mmu_put_page(vcpu, page, parent_pte);
668 kvm_mmu_page_unlink_children(vcpu, page);
669 if (!page->root_count) {
670 hlist_del(&page->hash_link);
671 kvm_mmu_free_page(vcpu, page->page_hpa);
673 list_del(&page->link);
674 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
678 static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
681 struct hlist_head *bucket;
682 struct kvm_mmu_page *page;
683 struct hlist_node *node, *n;
686 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
688 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
689 bucket = &vcpu->kvm->mmu_page_hash[index];
690 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
691 if (page->gfn == gfn && !page->role.metaphysical) {
692 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
694 kvm_mmu_zap_page(vcpu, page);
700 static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
702 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
703 struct kvm_mmu_page *page_head = page_header(__pa(pte));
705 __set_bit(slot, &page_head->slot_bitmap);
708 hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
710 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
712 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
715 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
717 struct kvm_memory_slot *slot;
720 ASSERT((gpa & HPA_ERR_MASK) == 0);
721 slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
723 return gpa | HPA_ERR_MASK;
724 page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
725 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
726 | (gpa & (PAGE_SIZE-1));
729 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
731 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
733 if (gpa == UNMAPPED_GVA)
735 return gpa_to_hpa(vcpu, gpa);
738 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
742 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
744 int level = PT32E_ROOT_LEVEL;
745 hpa_t table_addr = vcpu->mmu.root_hpa;
748 u32 index = PT64_INDEX(v, level);
752 ASSERT(VALID_PAGE(table_addr));
753 table = __va(table_addr);
757 if (is_present_pte(pte) && is_writeble_pte(pte))
759 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
760 page_header_update_slot(vcpu->kvm, table, v);
761 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
763 rmap_add(vcpu, &table[index]);
767 if (table[index] == 0) {
768 struct kvm_mmu_page *new_table;
771 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
773 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
777 pgprintk("nonpaging_map: ENOMEM\n");
781 table[index] = new_table->page_hpa | PT_PRESENT_MASK
782 | PT_WRITABLE_MASK | PT_USER_MASK;
784 table_addr = table[index] & PT64_BASE_ADDR_MASK;
788 static void mmu_free_roots(struct kvm_vcpu *vcpu)
791 struct kvm_mmu_page *page;
794 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
795 hpa_t root = vcpu->mmu.root_hpa;
797 ASSERT(VALID_PAGE(root));
798 page = page_header(root);
800 vcpu->mmu.root_hpa = INVALID_PAGE;
804 for (i = 0; i < 4; ++i) {
805 hpa_t root = vcpu->mmu.pae_root[i];
807 ASSERT(VALID_PAGE(root));
808 root &= PT64_BASE_ADDR_MASK;
809 page = page_header(root);
811 vcpu->mmu.pae_root[i] = INVALID_PAGE;
813 vcpu->mmu.root_hpa = INVALID_PAGE;
816 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
820 struct kvm_mmu_page *page;
822 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
825 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
826 hpa_t root = vcpu->mmu.root_hpa;
828 ASSERT(!VALID_PAGE(root));
829 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
830 PT64_ROOT_LEVEL, 0, NULL);
831 root = page->page_hpa;
833 vcpu->mmu.root_hpa = root;
837 for (i = 0; i < 4; ++i) {
838 hpa_t root = vcpu->mmu.pae_root[i];
840 ASSERT(!VALID_PAGE(root));
841 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
842 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
843 else if (vcpu->mmu.root_level == 0)
845 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
846 PT32_ROOT_LEVEL, !is_paging(vcpu),
848 root = page->page_hpa;
850 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
852 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
855 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
860 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
867 r = mmu_topup_memory_caches(vcpu);
872 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
875 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
877 if (is_error_hpa(paddr))
880 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
883 static void nonpaging_free(struct kvm_vcpu *vcpu)
885 mmu_free_roots(vcpu);
888 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
890 struct kvm_mmu *context = &vcpu->mmu;
892 context->new_cr3 = nonpaging_new_cr3;
893 context->page_fault = nonpaging_page_fault;
894 context->gva_to_gpa = nonpaging_gva_to_gpa;
895 context->free = nonpaging_free;
896 context->root_level = 0;
897 context->shadow_root_level = PT32E_ROOT_LEVEL;
898 mmu_alloc_roots(vcpu);
899 ASSERT(VALID_PAGE(context->root_hpa));
900 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
904 static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
906 ++kvm_stat.tlb_flush;
907 kvm_arch_ops->tlb_flush(vcpu);
910 static void paging_new_cr3(struct kvm_vcpu *vcpu)
912 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
913 mmu_free_roots(vcpu);
914 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
915 kvm_mmu_free_some_pages(vcpu);
916 mmu_alloc_roots(vcpu);
917 kvm_mmu_flush_tlb(vcpu);
918 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
921 static void mark_pagetable_nonglobal(void *shadow_pte)
923 page_header(__pa(shadow_pte))->global = 0;
926 static inline void set_pte_common(struct kvm_vcpu *vcpu,
935 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
937 access_bits &= ~PT_WRITABLE_MASK;
939 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
941 *shadow_pte |= access_bits;
943 if (!(*shadow_pte & PT_GLOBAL_MASK))
944 mark_pagetable_nonglobal(shadow_pte);
946 if (is_error_hpa(paddr)) {
947 *shadow_pte |= gaddr;
948 *shadow_pte |= PT_SHADOW_IO_MARK;
949 *shadow_pte &= ~PT_PRESENT_MASK;
953 *shadow_pte |= paddr;
955 if (access_bits & PT_WRITABLE_MASK) {
956 struct kvm_mmu_page *shadow;
958 shadow = kvm_mmu_lookup_page(vcpu, gfn);
960 pgprintk("%s: found shadow page for %lx, marking ro\n",
962 access_bits &= ~PT_WRITABLE_MASK;
963 if (is_writeble_pte(*shadow_pte)) {
964 *shadow_pte &= ~PT_WRITABLE_MASK;
965 kvm_arch_ops->tlb_flush(vcpu);
970 if (access_bits & PT_WRITABLE_MASK)
971 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
973 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
974 rmap_add(vcpu, shadow_pte);
977 static void inject_page_fault(struct kvm_vcpu *vcpu,
981 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
984 static inline int fix_read_pf(u64 *shadow_ent)
986 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
987 !(*shadow_ent & PT_USER_MASK)) {
989 * If supervisor write protect is disabled, we shadow kernel
990 * pages as user pages so we can trap the write access.
992 *shadow_ent |= PT_USER_MASK;
993 *shadow_ent &= ~PT_WRITABLE_MASK;
1001 static void paging_free(struct kvm_vcpu *vcpu)
1003 nonpaging_free(vcpu);
1007 #include "paging_tmpl.h"
1011 #include "paging_tmpl.h"
1014 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1016 struct kvm_mmu *context = &vcpu->mmu;
1018 ASSERT(is_pae(vcpu));
1019 context->new_cr3 = paging_new_cr3;
1020 context->page_fault = paging64_page_fault;
1021 context->gva_to_gpa = paging64_gva_to_gpa;
1022 context->free = paging_free;
1023 context->root_level = level;
1024 context->shadow_root_level = level;
1025 mmu_alloc_roots(vcpu);
1026 ASSERT(VALID_PAGE(context->root_hpa));
1027 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1028 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1032 static int paging64_init_context(struct kvm_vcpu *vcpu)
1034 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1037 static int paging32_init_context(struct kvm_vcpu *vcpu)
1039 struct kvm_mmu *context = &vcpu->mmu;
1041 context->new_cr3 = paging_new_cr3;
1042 context->page_fault = paging32_page_fault;
1043 context->gva_to_gpa = paging32_gva_to_gpa;
1044 context->free = paging_free;
1045 context->root_level = PT32_ROOT_LEVEL;
1046 context->shadow_root_level = PT32E_ROOT_LEVEL;
1047 mmu_alloc_roots(vcpu);
1048 ASSERT(VALID_PAGE(context->root_hpa));
1049 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
1050 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
1054 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1056 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1059 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1062 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1064 if (!is_paging(vcpu))
1065 return nonpaging_init_context(vcpu);
1066 else if (is_long_mode(vcpu))
1067 return paging64_init_context(vcpu);
1068 else if (is_pae(vcpu))
1069 return paging32E_init_context(vcpu);
1071 return paging32_init_context(vcpu);
1074 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1077 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1078 vcpu->mmu.free(vcpu);
1079 vcpu->mmu.root_hpa = INVALID_PAGE;
1083 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1087 destroy_kvm_mmu(vcpu);
1088 r = init_kvm_mmu(vcpu);
1091 r = mmu_topup_memory_caches(vcpu);
1096 static void mmu_pre_write_zap_pte(struct kvm_vcpu *vcpu,
1097 struct kvm_mmu_page *page,
1101 struct kvm_mmu_page *child;
1104 if (is_present_pte(pte)) {
1105 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1106 rmap_remove(vcpu, spte);
1108 child = page_header(pte & PT64_BASE_ADDR_MASK);
1109 mmu_page_remove_parent_pte(vcpu, child, spte);
1115 void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1117 gfn_t gfn = gpa >> PAGE_SHIFT;
1118 struct kvm_mmu_page *page;
1119 struct hlist_node *node, *n;
1120 struct hlist_head *bucket;
1123 unsigned offset = offset_in_page(gpa);
1125 unsigned page_offset;
1126 unsigned misaligned;
1131 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
1132 if (gfn == vcpu->last_pt_write_gfn) {
1133 ++vcpu->last_pt_write_count;
1134 if (vcpu->last_pt_write_count >= 3)
1137 vcpu->last_pt_write_gfn = gfn;
1138 vcpu->last_pt_write_count = 1;
1140 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1141 bucket = &vcpu->kvm->mmu_page_hash[index];
1142 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
1143 if (page->gfn != gfn || page->role.metaphysical)
1145 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1146 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1147 if (misaligned || flooded) {
1149 * Misaligned accesses are too much trouble to fix
1150 * up; also, they usually indicate a page is not used
1153 * If we're seeing too many writes to a page,
1154 * it may no longer be a page table, or we may be
1155 * forking, in which case it is better to unmap the
1158 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1159 gpa, bytes, page->role.word);
1160 kvm_mmu_zap_page(vcpu, page);
1163 page_offset = offset;
1164 level = page->role.level;
1166 if (page->role.glevels == PT32_ROOT_LEVEL) {
1167 page_offset <<= 1; /* 32->64 */
1169 * A 32-bit pde maps 4MB while the shadow pdes map
1170 * only 2MB. So we need to double the offset again
1171 * and zap two pdes instead of one.
1173 if (level == PT32_ROOT_LEVEL) {
1174 page_offset &= ~7; /* kill rounding error */
1178 page_offset &= ~PAGE_MASK;
1180 spte = __va(page->page_hpa);
1181 spte += page_offset / sizeof(*spte);
1183 mmu_pre_write_zap_pte(vcpu, page, spte);
1189 void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1193 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1195 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1197 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1200 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1202 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1203 struct kvm_mmu_page *page;
1205 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1206 struct kvm_mmu_page, link);
1207 kvm_mmu_zap_page(vcpu, page);
1210 EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1212 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1214 struct kvm_mmu_page *page;
1216 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1217 page = container_of(vcpu->kvm->active_mmu_pages.next,
1218 struct kvm_mmu_page, link);
1219 kvm_mmu_zap_page(vcpu, page);
1221 while (!list_empty(&vcpu->free_pages)) {
1222 page = list_entry(vcpu->free_pages.next,
1223 struct kvm_mmu_page, link);
1224 list_del(&page->link);
1225 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1226 page->page_hpa = INVALID_PAGE;
1228 free_page((unsigned long)vcpu->mmu.pae_root);
1231 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1238 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
1239 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1241 INIT_LIST_HEAD(&page_header->link);
1242 if ((page = alloc_page(GFP_KERNEL)) == NULL)
1244 set_page_private(page, (unsigned long)page_header);
1245 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1246 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1247 list_add(&page_header->link, &vcpu->free_pages);
1248 ++vcpu->kvm->n_free_mmu_pages;
1252 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1253 * Therefore we need to allocate shadow page tables in the first
1254 * 4GB of memory, which happens to fit the DMA32 zone.
1256 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1259 vcpu->mmu.pae_root = page_address(page);
1260 for (i = 0; i < 4; ++i)
1261 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1266 free_mmu_pages(vcpu);
1270 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1273 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1274 ASSERT(list_empty(&vcpu->free_pages));
1276 return alloc_mmu_pages(vcpu);
1279 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1282 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1283 ASSERT(!list_empty(&vcpu->free_pages));
1285 return init_kvm_mmu(vcpu);
1288 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1292 destroy_kvm_mmu(vcpu);
1293 free_mmu_pages(vcpu);
1294 mmu_free_memory_caches(vcpu);
1297 void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
1299 struct kvm *kvm = vcpu->kvm;
1300 struct kvm_mmu_page *page;
1302 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1306 if (!test_bit(slot, &page->slot_bitmap))
1309 pt = __va(page->page_hpa);
1310 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1312 if (pt[i] & PT_WRITABLE_MASK) {
1313 rmap_remove(vcpu, &pt[i]);
1314 pt[i] &= ~PT_WRITABLE_MASK;
1321 static const char *audit_msg;
1323 static gva_t canonicalize(gva_t gva)
1325 #ifdef CONFIG_X86_64
1326 gva = (long long)(gva << 16) >> 16;
1331 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1332 gva_t va, int level)
1334 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1336 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1338 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1341 if (!ent & PT_PRESENT_MASK)
1344 va = canonicalize(va);
1346 audit_mappings_page(vcpu, ent, va, level - 1);
1348 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1349 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1351 if ((ent & PT_PRESENT_MASK)
1352 && (ent & PT64_BASE_ADDR_MASK) != hpa)
1353 printk(KERN_ERR "audit error: (%s) levels %d"
1354 " gva %lx gpa %llx hpa %llx ent %llx\n",
1355 audit_msg, vcpu->mmu.root_level,
1361 static void audit_mappings(struct kvm_vcpu *vcpu)
1365 if (vcpu->mmu.root_level == 4)
1366 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1368 for (i = 0; i < 4; ++i)
1369 if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1370 audit_mappings_page(vcpu,
1371 vcpu->mmu.pae_root[i],
1376 static int count_rmaps(struct kvm_vcpu *vcpu)
1381 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1382 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1383 struct kvm_rmap_desc *d;
1385 for (j = 0; j < m->npages; ++j) {
1386 struct page *page = m->phys_mem[j];
1390 if (!(page->private & 1)) {
1394 d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1396 for (k = 0; k < RMAP_EXT; ++k)
1397 if (d->shadow_ptes[k])
1408 static int count_writable_mappings(struct kvm_vcpu *vcpu)
1411 struct kvm_mmu_page *page;
1414 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1415 u64 *pt = __va(page->page_hpa);
1417 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1420 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1423 if (!(ent & PT_PRESENT_MASK))
1425 if (!(ent & PT_WRITABLE_MASK))
1433 static void audit_rmap(struct kvm_vcpu *vcpu)
1435 int n_rmap = count_rmaps(vcpu);
1436 int n_actual = count_writable_mappings(vcpu);
1438 if (n_rmap != n_actual)
1439 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1440 __FUNCTION__, audit_msg, n_rmap, n_actual);
1443 static void audit_write_protection(struct kvm_vcpu *vcpu)
1445 struct kvm_mmu_page *page;
1447 list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1451 if (page->role.metaphysical)
1454 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1456 pg = pfn_to_page(hfn);
1458 printk(KERN_ERR "%s: (%s) shadow page has writable"
1459 " mappings: gfn %lx role %x\n",
1460 __FUNCTION__, audit_msg, page->gfn,
1465 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1472 audit_write_protection(vcpu);
1473 audit_mappings(vcpu);