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>
29 #define pgprintk(x...) do { printk(x); } while (0)
30 #define rmap_printk(x...) do { printk(x); } while (0)
34 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
35 __FILE__, __LINE__, #x); \
38 #define PT64_PT_BITS 9
39 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
40 #define PT32_PT_BITS 10
41 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
43 #define PT_WRITABLE_SHIFT 1
45 #define PT_PRESENT_MASK (1ULL << 0)
46 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
47 #define PT_USER_MASK (1ULL << 2)
48 #define PT_PWT_MASK (1ULL << 3)
49 #define PT_PCD_MASK (1ULL << 4)
50 #define PT_ACCESSED_MASK (1ULL << 5)
51 #define PT_DIRTY_MASK (1ULL << 6)
52 #define PT_PAGE_SIZE_MASK (1ULL << 7)
53 #define PT_PAT_MASK (1ULL << 7)
54 #define PT_GLOBAL_MASK (1ULL << 8)
55 #define PT64_NX_MASK (1ULL << 63)
57 #define PT_PAT_SHIFT 7
58 #define PT_DIR_PAT_SHIFT 12
59 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
61 #define PT32_DIR_PSE36_SIZE 4
62 #define PT32_DIR_PSE36_SHIFT 13
63 #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
66 #define PT32_PTE_COPY_MASK \
67 (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
69 #define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
71 #define PT_FIRST_AVAIL_BITS_SHIFT 9
72 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
74 #define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
75 #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
77 #define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
78 #define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
80 #define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
81 #define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
83 #define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
85 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
87 #define PT64_LEVEL_BITS 9
89 #define PT64_LEVEL_SHIFT(level) \
90 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
92 #define PT64_LEVEL_MASK(level) \
93 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
95 #define PT64_INDEX(address, level)\
96 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
99 #define PT32_LEVEL_BITS 10
101 #define PT32_LEVEL_SHIFT(level) \
102 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
104 #define PT32_LEVEL_MASK(level) \
105 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
107 #define PT32_INDEX(address, level)\
108 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
111 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & PAGE_MASK)
112 #define PT64_DIR_BASE_ADDR_MASK \
113 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
115 #define PT32_BASE_ADDR_MASK PAGE_MASK
116 #define PT32_DIR_BASE_ADDR_MASK \
117 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
120 #define PFERR_PRESENT_MASK (1U << 0)
121 #define PFERR_WRITE_MASK (1U << 1)
122 #define PFERR_USER_MASK (1U << 2)
124 #define PT64_ROOT_LEVEL 4
125 #define PT32_ROOT_LEVEL 2
126 #define PT32E_ROOT_LEVEL 3
128 #define PT_DIRECTORY_LEVEL 2
129 #define PT_PAGE_TABLE_LEVEL 1
133 struct kvm_rmap_desc {
134 u64 *shadow_ptes[RMAP_EXT];
135 struct kvm_rmap_desc *more;
138 static int is_write_protection(struct kvm_vcpu *vcpu)
140 return vcpu->cr0 & CR0_WP_MASK;
143 static int is_cpuid_PSE36(void)
148 static int is_present_pte(unsigned long pte)
150 return pte & PT_PRESENT_MASK;
153 static int is_writeble_pte(unsigned long pte)
155 return pte & PT_WRITABLE_MASK;
158 static int is_io_pte(unsigned long pte)
160 return pte & PT_SHADOW_IO_MARK;
163 static int is_rmap_pte(u64 pte)
165 return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
166 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
170 * Reverse mapping data structures:
172 * If page->private bit zero is zero, then page->private points to the
173 * shadow page table entry that points to page_address(page).
175 * If page->private bit zero is one, (then page->private & ~1) points
176 * to a struct kvm_rmap_desc containing more mappings.
178 static void rmap_add(struct kvm *kvm, u64 *spte)
181 struct kvm_rmap_desc *desc;
184 if (!is_rmap_pte(*spte))
186 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
187 if (!page->private) {
188 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
189 page->private = (unsigned long)spte;
190 } else if (!(page->private & 1)) {
191 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
192 desc = kzalloc(sizeof *desc, GFP_NOWAIT);
194 BUG(); /* FIXME: return error */
195 desc->shadow_ptes[0] = (u64 *)page->private;
196 desc->shadow_ptes[1] = spte;
197 page->private = (unsigned long)desc | 1;
199 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
200 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
201 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
203 if (desc->shadow_ptes[RMAP_EXT-1]) {
204 desc->more = kzalloc(sizeof *desc->more, GFP_NOWAIT);
206 BUG(); /* FIXME: return error */
209 for (i = 0; desc->shadow_ptes[i]; ++i)
211 desc->shadow_ptes[i] = spte;
215 static void rmap_desc_remove_entry(struct page *page,
216 struct kvm_rmap_desc *desc,
218 struct kvm_rmap_desc *prev_desc)
222 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
224 desc->shadow_ptes[i] = desc->shadow_ptes[j];
225 desc->shadow_ptes[j] = 0;
228 if (!prev_desc && !desc->more)
229 page->private = (unsigned long)desc->shadow_ptes[0];
232 prev_desc->more = desc->more;
234 page->private = (unsigned long)desc->more | 1;
238 static void rmap_remove(struct kvm *kvm, u64 *spte)
241 struct kvm_rmap_desc *desc;
242 struct kvm_rmap_desc *prev_desc;
245 if (!is_rmap_pte(*spte))
247 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
248 if (!page->private) {
249 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
251 } else if (!(page->private & 1)) {
252 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
253 if ((u64 *)page->private != spte) {
254 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
260 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
261 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
264 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
265 if (desc->shadow_ptes[i] == spte) {
266 rmap_desc_remove_entry(page, desc, i,
277 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
280 struct kvm_memory_slot *slot;
281 struct kvm_rmap_desc *desc;
284 slot = gfn_to_memslot(kvm, gfn);
286 page = gfn_to_page(slot, gfn);
288 while (page->private) {
289 if (!(page->private & 1))
290 spte = (u64 *)page->private;
292 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
293 spte = desc->shadow_ptes[0];
296 BUG_ON((*spte & PT64_BASE_ADDR_MASK) !=
297 page_to_pfn(page) << PAGE_SHIFT);
298 BUG_ON(!(*spte & PT_PRESENT_MASK));
299 BUG_ON(!(*spte & PT_WRITABLE_MASK));
300 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
301 rmap_remove(kvm, spte);
302 *spte &= ~(u64)PT_WRITABLE_MASK;
306 static int is_empty_shadow_page(hpa_t page_hpa)
311 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u64);
314 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
321 static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
323 struct kvm_mmu_page *page_head = page_header(page_hpa);
325 ASSERT(is_empty_shadow_page(page_hpa));
326 list_del(&page_head->link);
327 page_head->page_hpa = page_hpa;
328 list_add(&page_head->link, &vcpu->free_pages);
329 ++vcpu->kvm->n_free_mmu_pages;
332 static unsigned kvm_page_table_hashfn(gfn_t gfn)
337 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
340 struct kvm_mmu_page *page;
342 if (list_empty(&vcpu->free_pages))
345 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
346 list_del(&page->link);
347 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
348 ASSERT(is_empty_shadow_page(page->page_hpa));
349 page->slot_bitmap = 0;
351 page->multimapped = 0;
352 page->parent_pte = parent_pte;
353 --vcpu->kvm->n_free_mmu_pages;
357 static void mmu_page_add_parent_pte(struct kvm_mmu_page *page, u64 *parent_pte)
359 struct kvm_pte_chain *pte_chain;
360 struct hlist_node *node;
365 if (!page->multimapped) {
366 u64 *old = page->parent_pte;
369 page->parent_pte = parent_pte;
372 page->multimapped = 1;
373 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
375 INIT_HLIST_HEAD(&page->parent_ptes);
376 hlist_add_head(&pte_chain->link, &page->parent_ptes);
377 pte_chain->parent_ptes[0] = old;
379 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
380 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
382 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
383 if (!pte_chain->parent_ptes[i]) {
384 pte_chain->parent_ptes[i] = parent_pte;
388 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
390 hlist_add_head(&pte_chain->link, &page->parent_ptes);
391 pte_chain->parent_ptes[0] = parent_pte;
394 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
397 struct kvm_pte_chain *pte_chain;
398 struct hlist_node *node;
401 if (!page->multimapped) {
402 BUG_ON(page->parent_pte != parent_pte);
403 page->parent_pte = NULL;
406 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
407 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
408 if (!pte_chain->parent_ptes[i])
410 if (pte_chain->parent_ptes[i] != parent_pte)
412 while (i + 1 < NR_PTE_CHAIN_ENTRIES
413 && pte_chain->parent_ptes[i + 1]) {
414 pte_chain->parent_ptes[i]
415 = pte_chain->parent_ptes[i + 1];
418 pte_chain->parent_ptes[i] = NULL;
420 hlist_del(&pte_chain->link);
422 if (hlist_empty(&page->parent_ptes)) {
423 page->multimapped = 0;
424 page->parent_pte = NULL;
432 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
436 struct hlist_head *bucket;
437 struct kvm_mmu_page *page;
438 struct hlist_node *node;
440 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
441 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
442 bucket = &vcpu->kvm->mmu_page_hash[index];
443 hlist_for_each_entry(page, node, bucket, hash_link)
444 if (page->gfn == gfn && !page->role.metaphysical) {
445 pgprintk("%s: found role %x\n",
446 __FUNCTION__, page->role.word);
452 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
459 union kvm_mmu_page_role role;
462 struct hlist_head *bucket;
463 struct kvm_mmu_page *page;
464 struct hlist_node *node;
467 role.glevels = vcpu->mmu.root_level;
469 role.metaphysical = metaphysical;
470 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
471 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
472 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
473 role.quadrant = quadrant;
475 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
477 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
478 bucket = &vcpu->kvm->mmu_page_hash[index];
479 hlist_for_each_entry(page, node, bucket, hash_link)
480 if (page->gfn == gfn && page->role.word == role.word) {
481 mmu_page_add_parent_pte(page, parent_pte);
482 pgprintk("%s: found\n", __FUNCTION__);
485 page = kvm_mmu_alloc_page(vcpu, parent_pte);
488 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
491 hlist_add_head(&page->hash_link, bucket);
493 rmap_write_protect(vcpu->kvm, gfn);
497 static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
498 struct kvm_mmu_page *page)
504 pt = __va(page->page_hpa);
506 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
507 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
508 if (pt[i] & PT_PRESENT_MASK)
509 rmap_remove(vcpu->kvm, &pt[i]);
515 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
519 if (!(ent & PT_PRESENT_MASK))
521 ent &= PT64_BASE_ADDR_MASK;
522 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
526 static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
527 struct kvm_mmu_page *page,
530 mmu_page_remove_parent_pte(page, parent_pte);
533 static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
534 struct kvm_mmu_page *page)
538 while (page->multimapped || page->parent_pte) {
539 if (!page->multimapped)
540 parent_pte = page->parent_pte;
542 struct kvm_pte_chain *chain;
544 chain = container_of(page->parent_ptes.first,
545 struct kvm_pte_chain, link);
546 parent_pte = chain->parent_ptes[0];
549 kvm_mmu_put_page(vcpu, page, parent_pte);
552 kvm_mmu_page_unlink_children(vcpu, page);
553 if (!page->root_count) {
554 hlist_del(&page->hash_link);
555 kvm_mmu_free_page(vcpu, page->page_hpa);
557 list_del(&page->link);
558 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
562 static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
565 struct hlist_head *bucket;
566 struct kvm_mmu_page *page;
567 struct hlist_node *node, *n;
570 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
572 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
573 bucket = &vcpu->kvm->mmu_page_hash[index];
574 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
575 if (page->gfn == gfn && !page->role.metaphysical) {
576 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
578 kvm_mmu_zap_page(vcpu, page);
584 static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
586 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
587 struct kvm_mmu_page *page_head = page_header(__pa(pte));
589 __set_bit(slot, &page_head->slot_bitmap);
592 hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
594 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
596 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
599 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
601 struct kvm_memory_slot *slot;
604 ASSERT((gpa & HPA_ERR_MASK) == 0);
605 slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
607 return gpa | HPA_ERR_MASK;
608 page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
609 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
610 | (gpa & (PAGE_SIZE-1));
613 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
615 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
617 if (gpa == UNMAPPED_GVA)
619 return gpa_to_hpa(vcpu, gpa);
622 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
626 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
628 int level = PT32E_ROOT_LEVEL;
629 hpa_t table_addr = vcpu->mmu.root_hpa;
632 u32 index = PT64_INDEX(v, level);
636 ASSERT(VALID_PAGE(table_addr));
637 table = __va(table_addr);
641 if (is_present_pte(pte) && is_writeble_pte(pte))
643 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
644 page_header_update_slot(vcpu->kvm, table, v);
645 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
647 rmap_add(vcpu->kvm, &table[index]);
651 if (table[index] == 0) {
652 struct kvm_mmu_page *new_table;
655 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
657 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
661 pgprintk("nonpaging_map: ENOMEM\n");
665 table[index] = new_table->page_hpa | PT_PRESENT_MASK
666 | PT_WRITABLE_MASK | PT_USER_MASK;
668 table_addr = table[index] & PT64_BASE_ADDR_MASK;
672 static void mmu_free_roots(struct kvm_vcpu *vcpu)
675 struct kvm_mmu_page *page;
678 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
679 hpa_t root = vcpu->mmu.root_hpa;
681 ASSERT(VALID_PAGE(root));
682 page = page_header(root);
684 vcpu->mmu.root_hpa = INVALID_PAGE;
688 for (i = 0; i < 4; ++i) {
689 hpa_t root = vcpu->mmu.pae_root[i];
691 ASSERT(VALID_PAGE(root));
692 root &= PT64_BASE_ADDR_MASK;
693 page = page_header(root);
695 vcpu->mmu.pae_root[i] = INVALID_PAGE;
697 vcpu->mmu.root_hpa = INVALID_PAGE;
700 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
704 struct kvm_mmu_page *page;
706 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
709 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
710 hpa_t root = vcpu->mmu.root_hpa;
712 ASSERT(!VALID_PAGE(root));
713 root = kvm_mmu_get_page(vcpu, root_gfn, 0,
714 PT64_ROOT_LEVEL, 0, NULL)->page_hpa;
715 page = page_header(root);
717 vcpu->mmu.root_hpa = root;
721 for (i = 0; i < 4; ++i) {
722 hpa_t root = vcpu->mmu.pae_root[i];
724 ASSERT(!VALID_PAGE(root));
725 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
726 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
727 else if (vcpu->mmu.root_level == 0)
729 root = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
730 PT32_ROOT_LEVEL, !is_paging(vcpu),
732 page = page_header(root);
734 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
736 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
739 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
744 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
751 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
754 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
756 if (is_error_hpa(paddr))
759 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
762 static void nonpaging_free(struct kvm_vcpu *vcpu)
764 mmu_free_roots(vcpu);
767 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
769 struct kvm_mmu *context = &vcpu->mmu;
771 context->new_cr3 = nonpaging_new_cr3;
772 context->page_fault = nonpaging_page_fault;
773 context->gva_to_gpa = nonpaging_gva_to_gpa;
774 context->free = nonpaging_free;
775 context->root_level = 0;
776 context->shadow_root_level = PT32E_ROOT_LEVEL;
777 mmu_alloc_roots(vcpu);
778 ASSERT(VALID_PAGE(context->root_hpa));
779 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
783 static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
785 ++kvm_stat.tlb_flush;
786 kvm_arch_ops->tlb_flush(vcpu);
789 static void paging_new_cr3(struct kvm_vcpu *vcpu)
791 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
792 mmu_free_roots(vcpu);
793 mmu_alloc_roots(vcpu);
794 kvm_mmu_flush_tlb(vcpu);
795 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
798 static void mark_pagetable_nonglobal(void *shadow_pte)
800 page_header(__pa(shadow_pte))->global = 0;
803 static inline void set_pte_common(struct kvm_vcpu *vcpu,
812 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
814 access_bits &= ~PT_WRITABLE_MASK;
816 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
818 *shadow_pte |= access_bits;
820 if (!(*shadow_pte & PT_GLOBAL_MASK))
821 mark_pagetable_nonglobal(shadow_pte);
823 if (is_error_hpa(paddr)) {
824 *shadow_pte |= gaddr;
825 *shadow_pte |= PT_SHADOW_IO_MARK;
826 *shadow_pte &= ~PT_PRESENT_MASK;
830 *shadow_pte |= paddr;
832 if (access_bits & PT_WRITABLE_MASK) {
833 struct kvm_mmu_page *shadow;
835 shadow = kvm_mmu_lookup_page(vcpu, gfn);
837 pgprintk("%s: found shadow page for %lx, marking ro\n",
839 access_bits &= ~PT_WRITABLE_MASK;
840 *shadow_pte &= ~PT_WRITABLE_MASK;
844 if (access_bits & PT_WRITABLE_MASK)
845 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
847 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
848 rmap_add(vcpu->kvm, shadow_pte);
851 static void inject_page_fault(struct kvm_vcpu *vcpu,
855 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
858 static inline int fix_read_pf(u64 *shadow_ent)
860 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
861 !(*shadow_ent & PT_USER_MASK)) {
863 * If supervisor write protect is disabled, we shadow kernel
864 * pages as user pages so we can trap the write access.
866 *shadow_ent |= PT_USER_MASK;
867 *shadow_ent &= ~PT_WRITABLE_MASK;
875 static int may_access(u64 pte, int write, int user)
878 if (user && !(pte & PT_USER_MASK))
880 if (write && !(pte & PT_WRITABLE_MASK))
885 static void paging_free(struct kvm_vcpu *vcpu)
887 nonpaging_free(vcpu);
891 #include "paging_tmpl.h"
895 #include "paging_tmpl.h"
898 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
900 struct kvm_mmu *context = &vcpu->mmu;
902 ASSERT(is_pae(vcpu));
903 context->new_cr3 = paging_new_cr3;
904 context->page_fault = paging64_page_fault;
905 context->gva_to_gpa = paging64_gva_to_gpa;
906 context->free = paging_free;
907 context->root_level = level;
908 context->shadow_root_level = level;
909 mmu_alloc_roots(vcpu);
910 ASSERT(VALID_PAGE(context->root_hpa));
911 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
912 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
916 static int paging64_init_context(struct kvm_vcpu *vcpu)
918 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
921 static int paging32_init_context(struct kvm_vcpu *vcpu)
923 struct kvm_mmu *context = &vcpu->mmu;
925 context->new_cr3 = paging_new_cr3;
926 context->page_fault = paging32_page_fault;
927 context->gva_to_gpa = paging32_gva_to_gpa;
928 context->free = paging_free;
929 context->root_level = PT32_ROOT_LEVEL;
930 context->shadow_root_level = PT32E_ROOT_LEVEL;
931 mmu_alloc_roots(vcpu);
932 ASSERT(VALID_PAGE(context->root_hpa));
933 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
934 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
938 static int paging32E_init_context(struct kvm_vcpu *vcpu)
940 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
943 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
946 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
948 if (!is_paging(vcpu))
949 return nonpaging_init_context(vcpu);
950 else if (is_long_mode(vcpu))
951 return paging64_init_context(vcpu);
952 else if (is_pae(vcpu))
953 return paging32E_init_context(vcpu);
955 return paging32_init_context(vcpu);
958 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
961 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
962 vcpu->mmu.free(vcpu);
963 vcpu->mmu.root_hpa = INVALID_PAGE;
967 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
969 destroy_kvm_mmu(vcpu);
970 return init_kvm_mmu(vcpu);
973 void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
975 gfn_t gfn = gpa >> PAGE_SHIFT;
976 struct kvm_mmu_page *page;
977 struct kvm_mmu_page *child;
978 struct hlist_node *node, *n;
979 struct hlist_head *bucket;
983 unsigned offset = offset_in_page(gpa);
985 unsigned page_offset;
990 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
991 if (gfn == vcpu->last_pt_write_gfn) {
992 ++vcpu->last_pt_write_count;
993 if (vcpu->last_pt_write_count >= 3)
996 vcpu->last_pt_write_gfn = gfn;
997 vcpu->last_pt_write_count = 1;
999 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1000 bucket = &vcpu->kvm->mmu_page_hash[index];
1001 hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
1002 if (page->gfn != gfn || page->role.metaphysical)
1004 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1005 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1006 if (misaligned || flooded) {
1008 * Misaligned accesses are too much trouble to fix
1009 * up; also, they usually indicate a page is not used
1012 * If we're seeing too many writes to a page,
1013 * it may no longer be a page table, or we may be
1014 * forking, in which case it is better to unmap the
1017 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1018 gpa, bytes, page->role.word);
1019 kvm_mmu_zap_page(vcpu, page);
1022 page_offset = offset;
1023 level = page->role.level;
1024 if (page->role.glevels == PT32_ROOT_LEVEL) {
1025 page_offset <<= 1; /* 32->64 */
1026 page_offset &= ~PAGE_MASK;
1028 spte = __va(page->page_hpa);
1029 spte += page_offset / sizeof(*spte);
1031 if (is_present_pte(pte)) {
1032 if (level == PT_PAGE_TABLE_LEVEL)
1033 rmap_remove(vcpu->kvm, spte);
1035 child = page_header(pte & PT64_BASE_ADDR_MASK);
1036 mmu_page_remove_parent_pte(child, spte);
1043 void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1047 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1049 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1051 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1054 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1056 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1057 struct kvm_mmu_page *page;
1059 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1060 struct kvm_mmu_page, link);
1061 kvm_mmu_zap_page(vcpu, page);
1064 EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1066 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1068 struct kvm_mmu_page *page;
1070 while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1071 page = container_of(vcpu->kvm->active_mmu_pages.next,
1072 struct kvm_mmu_page, link);
1073 kvm_mmu_zap_page(vcpu, page);
1075 while (!list_empty(&vcpu->free_pages)) {
1076 page = list_entry(vcpu->free_pages.next,
1077 struct kvm_mmu_page, link);
1078 list_del(&page->link);
1079 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1080 page->page_hpa = INVALID_PAGE;
1082 free_page((unsigned long)vcpu->mmu.pae_root);
1085 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1092 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
1093 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1095 INIT_LIST_HEAD(&page_header->link);
1096 if ((page = alloc_page(GFP_KERNEL)) == NULL)
1098 page->private = (unsigned long)page_header;
1099 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1100 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1101 list_add(&page_header->link, &vcpu->free_pages);
1102 ++vcpu->kvm->n_free_mmu_pages;
1106 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1107 * Therefore we need to allocate shadow page tables in the first
1108 * 4GB of memory, which happens to fit the DMA32 zone.
1110 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1113 vcpu->mmu.pae_root = page_address(page);
1114 for (i = 0; i < 4; ++i)
1115 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1120 free_mmu_pages(vcpu);
1124 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1127 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1128 ASSERT(list_empty(&vcpu->free_pages));
1130 return alloc_mmu_pages(vcpu);
1133 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1136 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1137 ASSERT(!list_empty(&vcpu->free_pages));
1139 return init_kvm_mmu(vcpu);
1142 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1146 destroy_kvm_mmu(vcpu);
1147 free_mmu_pages(vcpu);
1150 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1152 struct kvm_mmu_page *page;
1154 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1158 if (!test_bit(slot, &page->slot_bitmap))
1161 pt = __va(page->page_hpa);
1162 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1164 if (pt[i] & PT_WRITABLE_MASK) {
1165 rmap_remove(kvm, &pt[i]);
1166 pt[i] &= ~PT_WRITABLE_MASK;