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 void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
308 struct kvm_mmu_page *page_head = page_header(page_hpa);
310 list_del(&page_head->link);
311 page_head->page_hpa = page_hpa;
312 list_add(&page_head->link, &vcpu->free_pages);
313 ++vcpu->kvm->n_free_mmu_pages;
316 static int is_empty_shadow_page(hpa_t page_hpa)
320 for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u32);
327 static unsigned kvm_page_table_hashfn(gfn_t gfn)
332 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
335 struct kvm_mmu_page *page;
337 if (list_empty(&vcpu->free_pages))
340 page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
341 list_del(&page->link);
342 list_add(&page->link, &vcpu->kvm->active_mmu_pages);
343 ASSERT(is_empty_shadow_page(page->page_hpa));
344 page->slot_bitmap = 0;
346 page->multimapped = 0;
347 page->parent_pte = parent_pte;
348 --vcpu->kvm->n_free_mmu_pages;
352 static void mmu_page_add_parent_pte(struct kvm_mmu_page *page, u64 *parent_pte)
354 struct kvm_pte_chain *pte_chain;
355 struct hlist_node *node;
360 if (!page->multimapped) {
361 u64 *old = page->parent_pte;
364 page->parent_pte = parent_pte;
367 page->multimapped = 1;
368 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
370 INIT_HLIST_HEAD(&page->parent_ptes);
371 hlist_add_head(&pte_chain->link, &page->parent_ptes);
372 pte_chain->parent_ptes[0] = old;
374 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
375 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
377 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
378 if (!pte_chain->parent_ptes[i]) {
379 pte_chain->parent_ptes[i] = parent_pte;
383 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
385 hlist_add_head(&pte_chain->link, &page->parent_ptes);
386 pte_chain->parent_ptes[0] = parent_pte;
389 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
392 struct kvm_pte_chain *pte_chain;
393 struct hlist_node *node;
396 if (!page->multimapped) {
397 BUG_ON(page->parent_pte != parent_pte);
398 page->parent_pte = NULL;
401 hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
402 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
403 if (!pte_chain->parent_ptes[i])
405 if (pte_chain->parent_ptes[i] != parent_pte)
407 while (i + 1 < NR_PTE_CHAIN_ENTRIES
408 && pte_chain->parent_ptes[i + 1]) {
409 pte_chain->parent_ptes[i]
410 = pte_chain->parent_ptes[i + 1];
413 pte_chain->parent_ptes[i] = NULL;
415 hlist_del(&pte_chain->link);
417 if (hlist_empty(&page->parent_ptes)) {
418 page->multimapped = 0;
419 page->parent_pte = NULL;
427 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
431 struct hlist_head *bucket;
432 struct kvm_mmu_page *page;
433 struct hlist_node *node;
435 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
436 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
437 bucket = &vcpu->kvm->mmu_page_hash[index];
438 hlist_for_each_entry(page, node, bucket, hash_link)
439 if (page->gfn == gfn && !page->role.metaphysical) {
440 pgprintk("%s: found role %x\n",
441 __FUNCTION__, page->role.word);
447 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
454 union kvm_mmu_page_role role;
457 struct hlist_head *bucket;
458 struct kvm_mmu_page *page;
459 struct hlist_node *node;
462 role.glevels = vcpu->mmu.root_level;
464 role.metaphysical = metaphysical;
465 if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
466 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
467 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
468 role.quadrant = quadrant;
470 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
472 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
473 bucket = &vcpu->kvm->mmu_page_hash[index];
474 hlist_for_each_entry(page, node, bucket, hash_link)
475 if (page->gfn == gfn && page->role.word == role.word) {
476 mmu_page_add_parent_pte(page, parent_pte);
477 pgprintk("%s: found\n", __FUNCTION__);
480 page = kvm_mmu_alloc_page(vcpu, parent_pte);
483 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
486 hlist_add_head(&page->hash_link, bucket);
488 rmap_write_protect(vcpu->kvm, gfn);
492 static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
493 struct kvm_mmu_page *page)
499 pt = __va(page->page_hpa);
501 if (page->role.level == PT_PAGE_TABLE_LEVEL) {
502 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
503 if (pt[i] & PT_PRESENT_MASK)
504 rmap_remove(vcpu->kvm, &pt[i]);
510 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
514 if (!(ent & PT_PRESENT_MASK))
516 ent &= PT64_BASE_ADDR_MASK;
517 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
521 static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
522 struct kvm_mmu_page *page,
525 mmu_page_remove_parent_pte(page, parent_pte);
528 static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
529 struct kvm_mmu_page *page)
533 while (page->multimapped || page->parent_pte) {
534 if (!page->multimapped)
535 parent_pte = page->parent_pte;
537 struct kvm_pte_chain *chain;
539 chain = container_of(page->parent_ptes.first,
540 struct kvm_pte_chain, link);
541 parent_pte = chain->parent_ptes[0];
544 kvm_mmu_put_page(vcpu, page, parent_pte);
547 kvm_mmu_page_unlink_children(vcpu, page);
548 hlist_del(&page->hash_link);
549 kvm_mmu_free_page(vcpu, page->page_hpa);
552 static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
555 struct hlist_head *bucket;
556 struct kvm_mmu_page *page;
557 struct hlist_node *node, *n;
560 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
562 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
563 bucket = &vcpu->kvm->mmu_page_hash[index];
564 hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
565 if (page->gfn == gfn && !page->role.metaphysical) {
566 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
568 kvm_mmu_zap_page(vcpu, page);
574 static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
576 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
577 struct kvm_mmu_page *page_head = page_header(__pa(pte));
579 __set_bit(slot, &page_head->slot_bitmap);
582 hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
584 hpa_t hpa = gpa_to_hpa(vcpu, gpa);
586 return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
589 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
591 struct kvm_memory_slot *slot;
594 ASSERT((gpa & HPA_ERR_MASK) == 0);
595 slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
597 return gpa | HPA_ERR_MASK;
598 page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
599 return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
600 | (gpa & (PAGE_SIZE-1));
603 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
605 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
607 if (gpa == UNMAPPED_GVA)
609 return gpa_to_hpa(vcpu, gpa);
612 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
616 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
618 int level = PT32E_ROOT_LEVEL;
619 hpa_t table_addr = vcpu->mmu.root_hpa;
622 u32 index = PT64_INDEX(v, level);
626 ASSERT(VALID_PAGE(table_addr));
627 table = __va(table_addr);
631 if (is_present_pte(pte) && is_writeble_pte(pte))
633 mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
634 page_header_update_slot(vcpu->kvm, table, v);
635 table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
637 rmap_add(vcpu->kvm, &table[index]);
641 if (table[index] == 0) {
642 struct kvm_mmu_page *new_table;
645 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
647 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
651 pgprintk("nonpaging_map: ENOMEM\n");
655 table[index] = new_table->page_hpa | PT_PRESENT_MASK
656 | PT_WRITABLE_MASK | PT_USER_MASK;
658 table_addr = table[index] & PT64_BASE_ADDR_MASK;
662 static void mmu_free_roots(struct kvm_vcpu *vcpu)
667 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
668 hpa_t root = vcpu->mmu.root_hpa;
670 ASSERT(VALID_PAGE(root));
671 vcpu->mmu.root_hpa = INVALID_PAGE;
675 for (i = 0; i < 4; ++i) {
676 hpa_t root = vcpu->mmu.pae_root[i];
678 ASSERT(VALID_PAGE(root));
679 root &= PT64_BASE_ADDR_MASK;
680 vcpu->mmu.pae_root[i] = INVALID_PAGE;
682 vcpu->mmu.root_hpa = INVALID_PAGE;
685 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
689 root_gfn = vcpu->cr3 >> PAGE_SHIFT;
692 if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
693 hpa_t root = vcpu->mmu.root_hpa;
695 ASSERT(!VALID_PAGE(root));
696 root = kvm_mmu_get_page(vcpu, root_gfn, 0,
697 PT64_ROOT_LEVEL, 0, NULL)->page_hpa;
698 vcpu->mmu.root_hpa = root;
702 for (i = 0; i < 4; ++i) {
703 hpa_t root = vcpu->mmu.pae_root[i];
705 ASSERT(!VALID_PAGE(root));
706 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
707 root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
708 else if (vcpu->mmu.root_level == 0)
710 root = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
711 PT32_ROOT_LEVEL, !is_paging(vcpu),
713 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
715 vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
718 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
723 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
730 ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
733 paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
735 if (is_error_hpa(paddr))
738 return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
741 static void nonpaging_free(struct kvm_vcpu *vcpu)
743 mmu_free_roots(vcpu);
746 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
748 struct kvm_mmu *context = &vcpu->mmu;
750 context->new_cr3 = nonpaging_new_cr3;
751 context->page_fault = nonpaging_page_fault;
752 context->gva_to_gpa = nonpaging_gva_to_gpa;
753 context->free = nonpaging_free;
754 context->root_level = 0;
755 context->shadow_root_level = PT32E_ROOT_LEVEL;
756 mmu_alloc_roots(vcpu);
757 ASSERT(VALID_PAGE(context->root_hpa));
758 kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
762 static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
764 ++kvm_stat.tlb_flush;
765 kvm_arch_ops->tlb_flush(vcpu);
768 static void paging_new_cr3(struct kvm_vcpu *vcpu)
770 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
771 mmu_free_roots(vcpu);
772 mmu_alloc_roots(vcpu);
773 kvm_mmu_flush_tlb(vcpu);
774 kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
777 static void mark_pagetable_nonglobal(void *shadow_pte)
779 page_header(__pa(shadow_pte))->global = 0;
782 static inline void set_pte_common(struct kvm_vcpu *vcpu,
791 *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
793 access_bits &= ~PT_WRITABLE_MASK;
795 paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
797 *shadow_pte |= access_bits;
799 if (!(*shadow_pte & PT_GLOBAL_MASK))
800 mark_pagetable_nonglobal(shadow_pte);
802 if (is_error_hpa(paddr)) {
803 *shadow_pte |= gaddr;
804 *shadow_pte |= PT_SHADOW_IO_MARK;
805 *shadow_pte &= ~PT_PRESENT_MASK;
809 *shadow_pte |= paddr;
811 if (access_bits & PT_WRITABLE_MASK) {
812 struct kvm_mmu_page *shadow;
814 shadow = kvm_mmu_lookup_page(vcpu, gfn);
816 pgprintk("%s: found shadow page for %lx, marking ro\n",
818 access_bits &= ~PT_WRITABLE_MASK;
819 *shadow_pte &= ~PT_WRITABLE_MASK;
823 if (access_bits & PT_WRITABLE_MASK)
824 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
826 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
827 rmap_add(vcpu->kvm, shadow_pte);
830 static void inject_page_fault(struct kvm_vcpu *vcpu,
834 kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
837 static inline int fix_read_pf(u64 *shadow_ent)
839 if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
840 !(*shadow_ent & PT_USER_MASK)) {
842 * If supervisor write protect is disabled, we shadow kernel
843 * pages as user pages so we can trap the write access.
845 *shadow_ent |= PT_USER_MASK;
846 *shadow_ent &= ~PT_WRITABLE_MASK;
854 static int may_access(u64 pte, int write, int user)
857 if (user && !(pte & PT_USER_MASK))
859 if (write && !(pte & PT_WRITABLE_MASK))
864 static void paging_free(struct kvm_vcpu *vcpu)
866 nonpaging_free(vcpu);
870 #include "paging_tmpl.h"
874 #include "paging_tmpl.h"
877 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
879 struct kvm_mmu *context = &vcpu->mmu;
881 ASSERT(is_pae(vcpu));
882 context->new_cr3 = paging_new_cr3;
883 context->page_fault = paging64_page_fault;
884 context->gva_to_gpa = paging64_gva_to_gpa;
885 context->free = paging_free;
886 context->root_level = level;
887 context->shadow_root_level = level;
888 mmu_alloc_roots(vcpu);
889 ASSERT(VALID_PAGE(context->root_hpa));
890 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
891 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
895 static int paging64_init_context(struct kvm_vcpu *vcpu)
897 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
900 static int paging32_init_context(struct kvm_vcpu *vcpu)
902 struct kvm_mmu *context = &vcpu->mmu;
904 context->new_cr3 = paging_new_cr3;
905 context->page_fault = paging32_page_fault;
906 context->gva_to_gpa = paging32_gva_to_gpa;
907 context->free = paging_free;
908 context->root_level = PT32_ROOT_LEVEL;
909 context->shadow_root_level = PT32E_ROOT_LEVEL;
910 mmu_alloc_roots(vcpu);
911 ASSERT(VALID_PAGE(context->root_hpa));
912 kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
913 (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
917 static int paging32E_init_context(struct kvm_vcpu *vcpu)
919 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
922 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
925 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
927 if (!is_paging(vcpu))
928 return nonpaging_init_context(vcpu);
929 else if (is_long_mode(vcpu))
930 return paging64_init_context(vcpu);
931 else if (is_pae(vcpu))
932 return paging32E_init_context(vcpu);
934 return paging32_init_context(vcpu);
937 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
940 if (VALID_PAGE(vcpu->mmu.root_hpa)) {
941 vcpu->mmu.free(vcpu);
942 vcpu->mmu.root_hpa = INVALID_PAGE;
946 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
948 destroy_kvm_mmu(vcpu);
949 return init_kvm_mmu(vcpu);
952 void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
954 gfn_t gfn = gpa >> PAGE_SHIFT;
955 struct kvm_mmu_page *page;
956 struct kvm_mmu_page *child;
957 struct hlist_node *node;
958 struct hlist_head *bucket;
962 unsigned offset = offset_in_page(gpa);
963 unsigned page_offset;
966 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
967 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
968 bucket = &vcpu->kvm->mmu_page_hash[index];
969 hlist_for_each_entry(page, node, bucket, hash_link) {
970 if (page->gfn != gfn || page->role.metaphysical)
972 page_offset = offset;
973 level = page->role.level;
974 if (page->role.glevels == PT32_ROOT_LEVEL) {
975 page_offset <<= 1; /* 32->64 */
976 page_offset &= ~PAGE_MASK;
978 spte = __va(page->page_hpa);
979 spte += page_offset / sizeof(*spte);
981 if (is_present_pte(pte)) {
982 if (level == PT_PAGE_TABLE_LEVEL)
983 rmap_remove(vcpu->kvm, spte);
985 child = page_header(pte & PT64_BASE_ADDR_MASK);
986 mmu_page_remove_parent_pte(child, spte);
993 void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
997 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
999 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1001 return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1004 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1006 while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1007 struct kvm_mmu_page *page;
1009 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1010 struct kvm_mmu_page, link);
1011 kvm_mmu_zap_page(vcpu, page);
1014 EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1016 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1018 while (!list_empty(&vcpu->free_pages)) {
1019 struct kvm_mmu_page *page;
1021 page = list_entry(vcpu->free_pages.next,
1022 struct kvm_mmu_page, link);
1023 list_del(&page->link);
1024 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1025 page->page_hpa = INVALID_PAGE;
1027 free_page((unsigned long)vcpu->mmu.pae_root);
1030 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1037 for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
1038 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1040 INIT_LIST_HEAD(&page_header->link);
1041 if ((page = alloc_page(GFP_KERNEL)) == NULL)
1043 page->private = (unsigned long)page_header;
1044 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1045 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1046 list_add(&page_header->link, &vcpu->free_pages);
1047 ++vcpu->kvm->n_free_mmu_pages;
1051 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1052 * Therefore we need to allocate shadow page tables in the first
1053 * 4GB of memory, which happens to fit the DMA32 zone.
1055 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1058 vcpu->mmu.pae_root = page_address(page);
1059 for (i = 0; i < 4; ++i)
1060 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1065 free_mmu_pages(vcpu);
1069 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1072 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1073 ASSERT(list_empty(&vcpu->free_pages));
1075 return alloc_mmu_pages(vcpu);
1078 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1081 ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1082 ASSERT(!list_empty(&vcpu->free_pages));
1084 return init_kvm_mmu(vcpu);
1087 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1091 destroy_kvm_mmu(vcpu);
1092 free_mmu_pages(vcpu);
1095 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1097 struct kvm_mmu_page *page;
1099 list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1103 if (!test_bit(slot, &page->slot_bitmap))
1106 pt = __va(page->page_hpa);
1107 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1109 if (pt[i] & PT_WRITABLE_MASK) {
1110 rmap_remove(kvm, &pt[i]);
1111 pt[i] &= ~PT_WRITABLE_MASK;