KVM: MMU: mode specific sync_page
[linux-2.6/verdex.git] / arch / x86 / kvm / mmu.c
blob90f01169c8f00d64dedca4207972a9e2261c579c
1 /*
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.
7 * MMU support
9 * Copyright (C) 2006 Qumranet, Inc.
11 * Authors:
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.
20 #include "vmx.h"
21 #include "mmu.h"
23 #include <linux/kvm_host.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/highmem.h>
28 #include <linux/module.h>
29 #include <linux/swap.h>
30 #include <linux/hugetlb.h>
31 #include <linux/compiler.h>
33 #include <asm/page.h>
34 #include <asm/cmpxchg.h>
35 #include <asm/io.h>
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
44 bool tdp_enabled = false;
46 #undef MMU_DEBUG
48 #undef AUDIT
50 #ifdef AUDIT
51 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52 #else
53 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54 #endif
56 #ifdef MMU_DEBUG
58 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
61 #else
63 #define pgprintk(x...) do { } while (0)
64 #define rmap_printk(x...) do { } while (0)
66 #endif
68 #if defined(MMU_DEBUG) || defined(AUDIT)
69 static int dbg = 0;
70 module_param(dbg, bool, 0644);
71 #endif
73 #ifndef MMU_DEBUG
74 #define ASSERT(x) do { } while (0)
75 #else
76 #define ASSERT(x) \
77 if (!(x)) { \
78 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
79 __FILE__, __LINE__, #x); \
81 #endif
83 #define PT_FIRST_AVAIL_BITS_SHIFT 9
84 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
86 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
88 #define PT64_LEVEL_BITS 9
90 #define PT64_LEVEL_SHIFT(level) \
91 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
93 #define PT64_LEVEL_MASK(level) \
94 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
96 #define PT64_INDEX(address, level)\
97 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
100 #define PT32_LEVEL_BITS 10
102 #define PT32_LEVEL_SHIFT(level) \
103 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
105 #define PT32_LEVEL_MASK(level) \
106 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
108 #define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
112 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
113 #define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
116 #define PT32_BASE_ADDR_MASK PAGE_MASK
117 #define PT32_DIR_BASE_ADDR_MASK \
118 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
120 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
121 | PT64_NX_MASK)
123 #define PFERR_PRESENT_MASK (1U << 0)
124 #define PFERR_WRITE_MASK (1U << 1)
125 #define PFERR_USER_MASK (1U << 2)
126 #define PFERR_FETCH_MASK (1U << 4)
128 #define PT_DIRECTORY_LEVEL 2
129 #define PT_PAGE_TABLE_LEVEL 1
131 #define RMAP_EXT 4
133 #define ACC_EXEC_MASK 1
134 #define ACC_WRITE_MASK PT_WRITABLE_MASK
135 #define ACC_USER_MASK PT_USER_MASK
136 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
138 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
140 struct kvm_rmap_desc {
141 u64 *shadow_ptes[RMAP_EXT];
142 struct kvm_rmap_desc *more;
145 struct kvm_shadow_walk {
146 int (*entry)(struct kvm_shadow_walk *walk, struct kvm_vcpu *vcpu,
147 u64 addr, u64 *spte, int level);
150 static struct kmem_cache *pte_chain_cache;
151 static struct kmem_cache *rmap_desc_cache;
152 static struct kmem_cache *mmu_page_header_cache;
154 static u64 __read_mostly shadow_trap_nonpresent_pte;
155 static u64 __read_mostly shadow_notrap_nonpresent_pte;
156 static u64 __read_mostly shadow_base_present_pte;
157 static u64 __read_mostly shadow_nx_mask;
158 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
159 static u64 __read_mostly shadow_user_mask;
160 static u64 __read_mostly shadow_accessed_mask;
161 static u64 __read_mostly shadow_dirty_mask;
163 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
165 shadow_trap_nonpresent_pte = trap_pte;
166 shadow_notrap_nonpresent_pte = notrap_pte;
168 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
170 void kvm_mmu_set_base_ptes(u64 base_pte)
172 shadow_base_present_pte = base_pte;
174 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
176 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
177 u64 dirty_mask, u64 nx_mask, u64 x_mask)
179 shadow_user_mask = user_mask;
180 shadow_accessed_mask = accessed_mask;
181 shadow_dirty_mask = dirty_mask;
182 shadow_nx_mask = nx_mask;
183 shadow_x_mask = x_mask;
185 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
187 static int is_write_protection(struct kvm_vcpu *vcpu)
189 return vcpu->arch.cr0 & X86_CR0_WP;
192 static int is_cpuid_PSE36(void)
194 return 1;
197 static int is_nx(struct kvm_vcpu *vcpu)
199 return vcpu->arch.shadow_efer & EFER_NX;
202 static int is_present_pte(unsigned long pte)
204 return pte & PT_PRESENT_MASK;
207 static int is_shadow_present_pte(u64 pte)
209 return pte != shadow_trap_nonpresent_pte
210 && pte != shadow_notrap_nonpresent_pte;
213 static int is_large_pte(u64 pte)
215 return pte & PT_PAGE_SIZE_MASK;
218 static int is_writeble_pte(unsigned long pte)
220 return pte & PT_WRITABLE_MASK;
223 static int is_dirty_pte(unsigned long pte)
225 return pte & shadow_dirty_mask;
228 static int is_rmap_pte(u64 pte)
230 return is_shadow_present_pte(pte);
233 static pfn_t spte_to_pfn(u64 pte)
235 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
238 static gfn_t pse36_gfn_delta(u32 gpte)
240 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
242 return (gpte & PT32_DIR_PSE36_MASK) << shift;
245 static void set_shadow_pte(u64 *sptep, u64 spte)
247 #ifdef CONFIG_X86_64
248 set_64bit((unsigned long *)sptep, spte);
249 #else
250 set_64bit((unsigned long long *)sptep, spte);
251 #endif
254 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
255 struct kmem_cache *base_cache, int min)
257 void *obj;
259 if (cache->nobjs >= min)
260 return 0;
261 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
262 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
263 if (!obj)
264 return -ENOMEM;
265 cache->objects[cache->nobjs++] = obj;
267 return 0;
270 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
272 while (mc->nobjs)
273 kfree(mc->objects[--mc->nobjs]);
276 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
277 int min)
279 struct page *page;
281 if (cache->nobjs >= min)
282 return 0;
283 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
284 page = alloc_page(GFP_KERNEL);
285 if (!page)
286 return -ENOMEM;
287 set_page_private(page, 0);
288 cache->objects[cache->nobjs++] = page_address(page);
290 return 0;
293 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
295 while (mc->nobjs)
296 free_page((unsigned long)mc->objects[--mc->nobjs]);
299 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
301 int r;
303 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
304 pte_chain_cache, 4);
305 if (r)
306 goto out;
307 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
308 rmap_desc_cache, 1);
309 if (r)
310 goto out;
311 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
312 if (r)
313 goto out;
314 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
315 mmu_page_header_cache, 4);
316 out:
317 return r;
320 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
322 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
323 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
324 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
325 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
328 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
329 size_t size)
331 void *p;
333 BUG_ON(!mc->nobjs);
334 p = mc->objects[--mc->nobjs];
335 memset(p, 0, size);
336 return p;
339 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
341 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
342 sizeof(struct kvm_pte_chain));
345 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
347 kfree(pc);
350 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
352 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
353 sizeof(struct kvm_rmap_desc));
356 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
358 kfree(rd);
362 * Return the pointer to the largepage write count for a given
363 * gfn, handling slots that are not large page aligned.
365 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
367 unsigned long idx;
369 idx = (gfn / KVM_PAGES_PER_HPAGE) -
370 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
371 return &slot->lpage_info[idx].write_count;
374 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
376 int *write_count;
378 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
379 *write_count += 1;
382 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
384 int *write_count;
386 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
387 *write_count -= 1;
388 WARN_ON(*write_count < 0);
391 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
393 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
394 int *largepage_idx;
396 if (slot) {
397 largepage_idx = slot_largepage_idx(gfn, slot);
398 return *largepage_idx;
401 return 1;
404 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
406 struct vm_area_struct *vma;
407 unsigned long addr;
408 int ret = 0;
410 addr = gfn_to_hva(kvm, gfn);
411 if (kvm_is_error_hva(addr))
412 return ret;
414 down_read(&current->mm->mmap_sem);
415 vma = find_vma(current->mm, addr);
416 if (vma && is_vm_hugetlb_page(vma))
417 ret = 1;
418 up_read(&current->mm->mmap_sem);
420 return ret;
423 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
425 struct kvm_memory_slot *slot;
427 if (has_wrprotected_page(vcpu->kvm, large_gfn))
428 return 0;
430 if (!host_largepage_backed(vcpu->kvm, large_gfn))
431 return 0;
433 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
434 if (slot && slot->dirty_bitmap)
435 return 0;
437 return 1;
441 * Take gfn and return the reverse mapping to it.
442 * Note: gfn must be unaliased before this function get called
445 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
447 struct kvm_memory_slot *slot;
448 unsigned long idx;
450 slot = gfn_to_memslot(kvm, gfn);
451 if (!lpage)
452 return &slot->rmap[gfn - slot->base_gfn];
454 idx = (gfn / KVM_PAGES_PER_HPAGE) -
455 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
457 return &slot->lpage_info[idx].rmap_pde;
461 * Reverse mapping data structures:
463 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
464 * that points to page_address(page).
466 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
467 * containing more mappings.
469 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
471 struct kvm_mmu_page *sp;
472 struct kvm_rmap_desc *desc;
473 unsigned long *rmapp;
474 int i;
476 if (!is_rmap_pte(*spte))
477 return;
478 gfn = unalias_gfn(vcpu->kvm, gfn);
479 sp = page_header(__pa(spte));
480 sp->gfns[spte - sp->spt] = gfn;
481 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
482 if (!*rmapp) {
483 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
484 *rmapp = (unsigned long)spte;
485 } else if (!(*rmapp & 1)) {
486 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
487 desc = mmu_alloc_rmap_desc(vcpu);
488 desc->shadow_ptes[0] = (u64 *)*rmapp;
489 desc->shadow_ptes[1] = spte;
490 *rmapp = (unsigned long)desc | 1;
491 } else {
492 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
493 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
494 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
495 desc = desc->more;
496 if (desc->shadow_ptes[RMAP_EXT-1]) {
497 desc->more = mmu_alloc_rmap_desc(vcpu);
498 desc = desc->more;
500 for (i = 0; desc->shadow_ptes[i]; ++i)
502 desc->shadow_ptes[i] = spte;
506 static void rmap_desc_remove_entry(unsigned long *rmapp,
507 struct kvm_rmap_desc *desc,
508 int i,
509 struct kvm_rmap_desc *prev_desc)
511 int j;
513 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
515 desc->shadow_ptes[i] = desc->shadow_ptes[j];
516 desc->shadow_ptes[j] = NULL;
517 if (j != 0)
518 return;
519 if (!prev_desc && !desc->more)
520 *rmapp = (unsigned long)desc->shadow_ptes[0];
521 else
522 if (prev_desc)
523 prev_desc->more = desc->more;
524 else
525 *rmapp = (unsigned long)desc->more | 1;
526 mmu_free_rmap_desc(desc);
529 static void rmap_remove(struct kvm *kvm, u64 *spte)
531 struct kvm_rmap_desc *desc;
532 struct kvm_rmap_desc *prev_desc;
533 struct kvm_mmu_page *sp;
534 pfn_t pfn;
535 unsigned long *rmapp;
536 int i;
538 if (!is_rmap_pte(*spte))
539 return;
540 sp = page_header(__pa(spte));
541 pfn = spte_to_pfn(*spte);
542 if (*spte & shadow_accessed_mask)
543 kvm_set_pfn_accessed(pfn);
544 if (is_writeble_pte(*spte))
545 kvm_release_pfn_dirty(pfn);
546 else
547 kvm_release_pfn_clean(pfn);
548 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
549 if (!*rmapp) {
550 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
551 BUG();
552 } else if (!(*rmapp & 1)) {
553 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
554 if ((u64 *)*rmapp != spte) {
555 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
556 spte, *spte);
557 BUG();
559 *rmapp = 0;
560 } else {
561 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
562 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
563 prev_desc = NULL;
564 while (desc) {
565 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
566 if (desc->shadow_ptes[i] == spte) {
567 rmap_desc_remove_entry(rmapp,
568 desc, i,
569 prev_desc);
570 return;
572 prev_desc = desc;
573 desc = desc->more;
575 BUG();
579 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
581 struct kvm_rmap_desc *desc;
582 struct kvm_rmap_desc *prev_desc;
583 u64 *prev_spte;
584 int i;
586 if (!*rmapp)
587 return NULL;
588 else if (!(*rmapp & 1)) {
589 if (!spte)
590 return (u64 *)*rmapp;
591 return NULL;
593 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
594 prev_desc = NULL;
595 prev_spte = NULL;
596 while (desc) {
597 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
598 if (prev_spte == spte)
599 return desc->shadow_ptes[i];
600 prev_spte = desc->shadow_ptes[i];
602 desc = desc->more;
604 return NULL;
607 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
609 unsigned long *rmapp;
610 u64 *spte;
611 int write_protected = 0;
613 gfn = unalias_gfn(kvm, gfn);
614 rmapp = gfn_to_rmap(kvm, gfn, 0);
616 spte = rmap_next(kvm, rmapp, NULL);
617 while (spte) {
618 BUG_ON(!spte);
619 BUG_ON(!(*spte & PT_PRESENT_MASK));
620 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
621 if (is_writeble_pte(*spte)) {
622 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
623 write_protected = 1;
625 spte = rmap_next(kvm, rmapp, spte);
627 if (write_protected) {
628 pfn_t pfn;
630 spte = rmap_next(kvm, rmapp, NULL);
631 pfn = spte_to_pfn(*spte);
632 kvm_set_pfn_dirty(pfn);
635 /* check for huge page mappings */
636 rmapp = gfn_to_rmap(kvm, gfn, 1);
637 spte = rmap_next(kvm, rmapp, NULL);
638 while (spte) {
639 BUG_ON(!spte);
640 BUG_ON(!(*spte & PT_PRESENT_MASK));
641 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
642 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
643 if (is_writeble_pte(*spte)) {
644 rmap_remove(kvm, spte);
645 --kvm->stat.lpages;
646 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
647 spte = NULL;
648 write_protected = 1;
650 spte = rmap_next(kvm, rmapp, spte);
653 if (write_protected)
654 kvm_flush_remote_tlbs(kvm);
656 account_shadowed(kvm, gfn);
659 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
661 u64 *spte;
662 int need_tlb_flush = 0;
664 while ((spte = rmap_next(kvm, rmapp, NULL))) {
665 BUG_ON(!(*spte & PT_PRESENT_MASK));
666 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
667 rmap_remove(kvm, spte);
668 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
669 need_tlb_flush = 1;
671 return need_tlb_flush;
674 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
675 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
677 int i;
678 int retval = 0;
681 * If mmap_sem isn't taken, we can look the memslots with only
682 * the mmu_lock by skipping over the slots with userspace_addr == 0.
684 for (i = 0; i < kvm->nmemslots; i++) {
685 struct kvm_memory_slot *memslot = &kvm->memslots[i];
686 unsigned long start = memslot->userspace_addr;
687 unsigned long end;
689 /* mmu_lock protects userspace_addr */
690 if (!start)
691 continue;
693 end = start + (memslot->npages << PAGE_SHIFT);
694 if (hva >= start && hva < end) {
695 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
696 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
697 retval |= handler(kvm,
698 &memslot->lpage_info[
699 gfn_offset /
700 KVM_PAGES_PER_HPAGE].rmap_pde);
704 return retval;
707 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
709 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
712 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
714 u64 *spte;
715 int young = 0;
717 /* always return old for EPT */
718 if (!shadow_accessed_mask)
719 return 0;
721 spte = rmap_next(kvm, rmapp, NULL);
722 while (spte) {
723 int _young;
724 u64 _spte = *spte;
725 BUG_ON(!(_spte & PT_PRESENT_MASK));
726 _young = _spte & PT_ACCESSED_MASK;
727 if (_young) {
728 young = 1;
729 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
731 spte = rmap_next(kvm, rmapp, spte);
733 return young;
736 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
738 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
741 #ifdef MMU_DEBUG
742 static int is_empty_shadow_page(u64 *spt)
744 u64 *pos;
745 u64 *end;
747 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
748 if (is_shadow_present_pte(*pos)) {
749 printk(KERN_ERR "%s: %p %llx\n", __func__,
750 pos, *pos);
751 return 0;
753 return 1;
755 #endif
757 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
759 ASSERT(is_empty_shadow_page(sp->spt));
760 list_del(&sp->link);
761 __free_page(virt_to_page(sp->spt));
762 __free_page(virt_to_page(sp->gfns));
763 kfree(sp);
764 ++kvm->arch.n_free_mmu_pages;
767 static unsigned kvm_page_table_hashfn(gfn_t gfn)
769 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
772 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
773 u64 *parent_pte)
775 struct kvm_mmu_page *sp;
777 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
778 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
779 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
780 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
781 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
782 ASSERT(is_empty_shadow_page(sp->spt));
783 sp->slot_bitmap = 0;
784 sp->multimapped = 0;
785 sp->parent_pte = parent_pte;
786 --vcpu->kvm->arch.n_free_mmu_pages;
787 return sp;
790 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
791 struct kvm_mmu_page *sp, u64 *parent_pte)
793 struct kvm_pte_chain *pte_chain;
794 struct hlist_node *node;
795 int i;
797 if (!parent_pte)
798 return;
799 if (!sp->multimapped) {
800 u64 *old = sp->parent_pte;
802 if (!old) {
803 sp->parent_pte = parent_pte;
804 return;
806 sp->multimapped = 1;
807 pte_chain = mmu_alloc_pte_chain(vcpu);
808 INIT_HLIST_HEAD(&sp->parent_ptes);
809 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
810 pte_chain->parent_ptes[0] = old;
812 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
813 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
814 continue;
815 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
816 if (!pte_chain->parent_ptes[i]) {
817 pte_chain->parent_ptes[i] = parent_pte;
818 return;
821 pte_chain = mmu_alloc_pte_chain(vcpu);
822 BUG_ON(!pte_chain);
823 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
824 pte_chain->parent_ptes[0] = parent_pte;
827 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
828 u64 *parent_pte)
830 struct kvm_pte_chain *pte_chain;
831 struct hlist_node *node;
832 int i;
834 if (!sp->multimapped) {
835 BUG_ON(sp->parent_pte != parent_pte);
836 sp->parent_pte = NULL;
837 return;
839 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
840 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
841 if (!pte_chain->parent_ptes[i])
842 break;
843 if (pte_chain->parent_ptes[i] != parent_pte)
844 continue;
845 while (i + 1 < NR_PTE_CHAIN_ENTRIES
846 && pte_chain->parent_ptes[i + 1]) {
847 pte_chain->parent_ptes[i]
848 = pte_chain->parent_ptes[i + 1];
849 ++i;
851 pte_chain->parent_ptes[i] = NULL;
852 if (i == 0) {
853 hlist_del(&pte_chain->link);
854 mmu_free_pte_chain(pte_chain);
855 if (hlist_empty(&sp->parent_ptes)) {
856 sp->multimapped = 0;
857 sp->parent_pte = NULL;
860 return;
862 BUG();
865 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
866 struct kvm_mmu_page *sp)
868 int i;
870 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
871 sp->spt[i] = shadow_trap_nonpresent_pte;
874 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
875 struct kvm_mmu_page *sp)
877 return 1;
880 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
882 unsigned index;
883 struct hlist_head *bucket;
884 struct kvm_mmu_page *sp;
885 struct hlist_node *node;
887 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
888 index = kvm_page_table_hashfn(gfn);
889 bucket = &kvm->arch.mmu_page_hash[index];
890 hlist_for_each_entry(sp, node, bucket, hash_link)
891 if (sp->gfn == gfn && !sp->role.metaphysical
892 && !sp->role.invalid) {
893 pgprintk("%s: found role %x\n",
894 __func__, sp->role.word);
895 return sp;
897 return NULL;
900 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
901 gfn_t gfn,
902 gva_t gaddr,
903 unsigned level,
904 int metaphysical,
905 unsigned access,
906 u64 *parent_pte)
908 union kvm_mmu_page_role role;
909 unsigned index;
910 unsigned quadrant;
911 struct hlist_head *bucket;
912 struct kvm_mmu_page *sp;
913 struct hlist_node *node;
915 role.word = 0;
916 role.glevels = vcpu->arch.mmu.root_level;
917 role.level = level;
918 role.metaphysical = metaphysical;
919 role.access = access;
920 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
921 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
922 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
923 role.quadrant = quadrant;
925 pgprintk("%s: looking gfn %lx role %x\n", __func__,
926 gfn, role.word);
927 index = kvm_page_table_hashfn(gfn);
928 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
929 hlist_for_each_entry(sp, node, bucket, hash_link)
930 if (sp->gfn == gfn && sp->role.word == role.word) {
931 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
932 pgprintk("%s: found\n", __func__);
933 return sp;
935 ++vcpu->kvm->stat.mmu_cache_miss;
936 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
937 if (!sp)
938 return sp;
939 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
940 sp->gfn = gfn;
941 sp->role = role;
942 hlist_add_head(&sp->hash_link, bucket);
943 if (!metaphysical)
944 rmap_write_protect(vcpu->kvm, gfn);
945 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
946 vcpu->arch.mmu.prefetch_page(vcpu, sp);
947 else
948 nonpaging_prefetch_page(vcpu, sp);
949 return sp;
952 static int walk_shadow(struct kvm_shadow_walk *walker,
953 struct kvm_vcpu *vcpu, u64 addr)
955 hpa_t shadow_addr;
956 int level;
957 int r;
958 u64 *sptep;
959 unsigned index;
961 shadow_addr = vcpu->arch.mmu.root_hpa;
962 level = vcpu->arch.mmu.shadow_root_level;
963 if (level == PT32E_ROOT_LEVEL) {
964 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
965 shadow_addr &= PT64_BASE_ADDR_MASK;
966 --level;
969 while (level >= PT_PAGE_TABLE_LEVEL) {
970 index = SHADOW_PT_INDEX(addr, level);
971 sptep = ((u64 *)__va(shadow_addr)) + index;
972 r = walker->entry(walker, vcpu, addr, sptep, level);
973 if (r)
974 return r;
975 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
976 --level;
978 return 0;
981 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
982 struct kvm_mmu_page *sp)
984 unsigned i;
985 u64 *pt;
986 u64 ent;
988 pt = sp->spt;
990 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
991 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
992 if (is_shadow_present_pte(pt[i]))
993 rmap_remove(kvm, &pt[i]);
994 pt[i] = shadow_trap_nonpresent_pte;
996 return;
999 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1000 ent = pt[i];
1002 if (is_shadow_present_pte(ent)) {
1003 if (!is_large_pte(ent)) {
1004 ent &= PT64_BASE_ADDR_MASK;
1005 mmu_page_remove_parent_pte(page_header(ent),
1006 &pt[i]);
1007 } else {
1008 --kvm->stat.lpages;
1009 rmap_remove(kvm, &pt[i]);
1012 pt[i] = shadow_trap_nonpresent_pte;
1016 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1018 mmu_page_remove_parent_pte(sp, parent_pte);
1021 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1023 int i;
1025 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1026 if (kvm->vcpus[i])
1027 kvm->vcpus[i]->arch.last_pte_updated = NULL;
1030 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1032 u64 *parent_pte;
1034 while (sp->multimapped || sp->parent_pte) {
1035 if (!sp->multimapped)
1036 parent_pte = sp->parent_pte;
1037 else {
1038 struct kvm_pte_chain *chain;
1040 chain = container_of(sp->parent_ptes.first,
1041 struct kvm_pte_chain, link);
1042 parent_pte = chain->parent_ptes[0];
1044 BUG_ON(!parent_pte);
1045 kvm_mmu_put_page(sp, parent_pte);
1046 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
1050 static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1052 ++kvm->stat.mmu_shadow_zapped;
1053 kvm_mmu_page_unlink_children(kvm, sp);
1054 kvm_mmu_unlink_parents(kvm, sp);
1055 kvm_flush_remote_tlbs(kvm);
1056 if (!sp->role.invalid && !sp->role.metaphysical)
1057 unaccount_shadowed(kvm, sp->gfn);
1058 if (!sp->root_count) {
1059 hlist_del(&sp->hash_link);
1060 kvm_mmu_free_page(kvm, sp);
1061 } else {
1062 sp->role.invalid = 1;
1063 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1064 kvm_reload_remote_mmus(kvm);
1066 kvm_mmu_reset_last_pte_updated(kvm);
1070 * Changing the number of mmu pages allocated to the vm
1071 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1073 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1076 * If we set the number of mmu pages to be smaller be than the
1077 * number of actived pages , we must to free some mmu pages before we
1078 * change the value
1081 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
1082 kvm_nr_mmu_pages) {
1083 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1084 - kvm->arch.n_free_mmu_pages;
1086 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1087 struct kvm_mmu_page *page;
1089 page = container_of(kvm->arch.active_mmu_pages.prev,
1090 struct kvm_mmu_page, link);
1091 kvm_mmu_zap_page(kvm, page);
1092 n_used_mmu_pages--;
1094 kvm->arch.n_free_mmu_pages = 0;
1096 else
1097 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1098 - kvm->arch.n_alloc_mmu_pages;
1100 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1103 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1105 unsigned index;
1106 struct hlist_head *bucket;
1107 struct kvm_mmu_page *sp;
1108 struct hlist_node *node, *n;
1109 int r;
1111 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1112 r = 0;
1113 index = kvm_page_table_hashfn(gfn);
1114 bucket = &kvm->arch.mmu_page_hash[index];
1115 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1116 if (sp->gfn == gfn && !sp->role.metaphysical) {
1117 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1118 sp->role.word);
1119 kvm_mmu_zap_page(kvm, sp);
1120 r = 1;
1122 return r;
1125 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1127 struct kvm_mmu_page *sp;
1129 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
1130 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
1131 kvm_mmu_zap_page(kvm, sp);
1135 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1137 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1138 struct kvm_mmu_page *sp = page_header(__pa(pte));
1140 __set_bit(slot, &sp->slot_bitmap);
1143 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1145 struct page *page;
1147 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1149 if (gpa == UNMAPPED_GVA)
1150 return NULL;
1152 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1154 return page;
1157 static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1158 unsigned pte_access, int user_fault,
1159 int write_fault, int dirty, int largepage,
1160 gfn_t gfn, pfn_t pfn, bool speculative)
1162 u64 spte;
1163 int ret = 0;
1165 * We don't set the accessed bit, since we sometimes want to see
1166 * whether the guest actually used the pte (in order to detect
1167 * demand paging).
1169 spte = shadow_base_present_pte | shadow_dirty_mask;
1170 if (!speculative)
1171 spte |= shadow_accessed_mask;
1172 if (!dirty)
1173 pte_access &= ~ACC_WRITE_MASK;
1174 if (pte_access & ACC_EXEC_MASK)
1175 spte |= shadow_x_mask;
1176 else
1177 spte |= shadow_nx_mask;
1178 if (pte_access & ACC_USER_MASK)
1179 spte |= shadow_user_mask;
1180 if (largepage)
1181 spte |= PT_PAGE_SIZE_MASK;
1183 spte |= (u64)pfn << PAGE_SHIFT;
1185 if ((pte_access & ACC_WRITE_MASK)
1186 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1187 struct kvm_mmu_page *shadow;
1189 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1190 ret = 1;
1191 spte = shadow_trap_nonpresent_pte;
1192 goto set_pte;
1195 spte |= PT_WRITABLE_MASK;
1197 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1198 if (shadow) {
1199 pgprintk("%s: found shadow page for %lx, marking ro\n",
1200 __func__, gfn);
1201 ret = 1;
1202 pte_access &= ~ACC_WRITE_MASK;
1203 if (is_writeble_pte(spte))
1204 spte &= ~PT_WRITABLE_MASK;
1208 if (pte_access & ACC_WRITE_MASK)
1209 mark_page_dirty(vcpu->kvm, gfn);
1211 set_pte:
1212 set_shadow_pte(shadow_pte, spte);
1213 return ret;
1217 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1218 unsigned pt_access, unsigned pte_access,
1219 int user_fault, int write_fault, int dirty,
1220 int *ptwrite, int largepage, gfn_t gfn,
1221 pfn_t pfn, bool speculative)
1223 int was_rmapped = 0;
1224 int was_writeble = is_writeble_pte(*shadow_pte);
1226 pgprintk("%s: spte %llx access %x write_fault %d"
1227 " user_fault %d gfn %lx\n",
1228 __func__, *shadow_pte, pt_access,
1229 write_fault, user_fault, gfn);
1231 if (is_rmap_pte(*shadow_pte)) {
1233 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1234 * the parent of the now unreachable PTE.
1236 if (largepage && !is_large_pte(*shadow_pte)) {
1237 struct kvm_mmu_page *child;
1238 u64 pte = *shadow_pte;
1240 child = page_header(pte & PT64_BASE_ADDR_MASK);
1241 mmu_page_remove_parent_pte(child, shadow_pte);
1242 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1243 pgprintk("hfn old %lx new %lx\n",
1244 spte_to_pfn(*shadow_pte), pfn);
1245 rmap_remove(vcpu->kvm, shadow_pte);
1246 } else {
1247 if (largepage)
1248 was_rmapped = is_large_pte(*shadow_pte);
1249 else
1250 was_rmapped = 1;
1253 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
1254 dirty, largepage, gfn, pfn, speculative)) {
1255 if (write_fault)
1256 *ptwrite = 1;
1257 kvm_x86_ops->tlb_flush(vcpu);
1260 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1261 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1262 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1263 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1264 *shadow_pte, shadow_pte);
1265 if (!was_rmapped && is_large_pte(*shadow_pte))
1266 ++vcpu->kvm->stat.lpages;
1268 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1269 if (!was_rmapped) {
1270 rmap_add(vcpu, shadow_pte, gfn, largepage);
1271 if (!is_rmap_pte(*shadow_pte))
1272 kvm_release_pfn_clean(pfn);
1273 } else {
1274 if (was_writeble)
1275 kvm_release_pfn_dirty(pfn);
1276 else
1277 kvm_release_pfn_clean(pfn);
1279 if (speculative) {
1280 vcpu->arch.last_pte_updated = shadow_pte;
1281 vcpu->arch.last_pte_gfn = gfn;
1285 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1289 struct direct_shadow_walk {
1290 struct kvm_shadow_walk walker;
1291 pfn_t pfn;
1292 int write;
1293 int largepage;
1294 int pt_write;
1297 static int direct_map_entry(struct kvm_shadow_walk *_walk,
1298 struct kvm_vcpu *vcpu,
1299 u64 addr, u64 *sptep, int level)
1301 struct direct_shadow_walk *walk =
1302 container_of(_walk, struct direct_shadow_walk, walker);
1303 struct kvm_mmu_page *sp;
1304 gfn_t pseudo_gfn;
1305 gfn_t gfn = addr >> PAGE_SHIFT;
1307 if (level == PT_PAGE_TABLE_LEVEL
1308 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1309 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1310 0, walk->write, 1, &walk->pt_write,
1311 walk->largepage, gfn, walk->pfn, false);
1312 ++vcpu->stat.pf_fixed;
1313 return 1;
1316 if (*sptep == shadow_trap_nonpresent_pte) {
1317 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1318 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
1319 1, ACC_ALL, sptep);
1320 if (!sp) {
1321 pgprintk("nonpaging_map: ENOMEM\n");
1322 kvm_release_pfn_clean(walk->pfn);
1323 return -ENOMEM;
1326 set_shadow_pte(sptep,
1327 __pa(sp->spt)
1328 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1329 | shadow_user_mask | shadow_x_mask);
1331 return 0;
1334 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1335 int largepage, gfn_t gfn, pfn_t pfn)
1337 int r;
1338 struct direct_shadow_walk walker = {
1339 .walker = { .entry = direct_map_entry, },
1340 .pfn = pfn,
1341 .largepage = largepage,
1342 .write = write,
1343 .pt_write = 0,
1346 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
1347 if (r < 0)
1348 return r;
1349 return walker.pt_write;
1352 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1354 int r;
1355 int largepage = 0;
1356 pfn_t pfn;
1357 unsigned long mmu_seq;
1359 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1360 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1361 largepage = 1;
1364 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1365 smp_rmb();
1366 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1368 /* mmio */
1369 if (is_error_pfn(pfn)) {
1370 kvm_release_pfn_clean(pfn);
1371 return 1;
1374 spin_lock(&vcpu->kvm->mmu_lock);
1375 if (mmu_notifier_retry(vcpu, mmu_seq))
1376 goto out_unlock;
1377 kvm_mmu_free_some_pages(vcpu);
1378 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1379 spin_unlock(&vcpu->kvm->mmu_lock);
1382 return r;
1384 out_unlock:
1385 spin_unlock(&vcpu->kvm->mmu_lock);
1386 kvm_release_pfn_clean(pfn);
1387 return 0;
1391 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1393 int i;
1394 struct kvm_mmu_page *sp;
1396 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1397 return;
1398 spin_lock(&vcpu->kvm->mmu_lock);
1399 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1400 hpa_t root = vcpu->arch.mmu.root_hpa;
1402 sp = page_header(root);
1403 --sp->root_count;
1404 if (!sp->root_count && sp->role.invalid)
1405 kvm_mmu_zap_page(vcpu->kvm, sp);
1406 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1407 spin_unlock(&vcpu->kvm->mmu_lock);
1408 return;
1410 for (i = 0; i < 4; ++i) {
1411 hpa_t root = vcpu->arch.mmu.pae_root[i];
1413 if (root) {
1414 root &= PT64_BASE_ADDR_MASK;
1415 sp = page_header(root);
1416 --sp->root_count;
1417 if (!sp->root_count && sp->role.invalid)
1418 kvm_mmu_zap_page(vcpu->kvm, sp);
1420 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1422 spin_unlock(&vcpu->kvm->mmu_lock);
1423 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1426 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1428 int i;
1429 gfn_t root_gfn;
1430 struct kvm_mmu_page *sp;
1431 int metaphysical = 0;
1433 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1435 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1436 hpa_t root = vcpu->arch.mmu.root_hpa;
1438 ASSERT(!VALID_PAGE(root));
1439 if (tdp_enabled)
1440 metaphysical = 1;
1441 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1442 PT64_ROOT_LEVEL, metaphysical,
1443 ACC_ALL, NULL);
1444 root = __pa(sp->spt);
1445 ++sp->root_count;
1446 vcpu->arch.mmu.root_hpa = root;
1447 return;
1449 metaphysical = !is_paging(vcpu);
1450 if (tdp_enabled)
1451 metaphysical = 1;
1452 for (i = 0; i < 4; ++i) {
1453 hpa_t root = vcpu->arch.mmu.pae_root[i];
1455 ASSERT(!VALID_PAGE(root));
1456 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1457 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1458 vcpu->arch.mmu.pae_root[i] = 0;
1459 continue;
1461 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1462 } else if (vcpu->arch.mmu.root_level == 0)
1463 root_gfn = 0;
1464 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1465 PT32_ROOT_LEVEL, metaphysical,
1466 ACC_ALL, NULL);
1467 root = __pa(sp->spt);
1468 ++sp->root_count;
1469 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
1471 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
1474 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1476 return vaddr;
1479 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1480 u32 error_code)
1482 gfn_t gfn;
1483 int r;
1485 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
1486 r = mmu_topup_memory_caches(vcpu);
1487 if (r)
1488 return r;
1490 ASSERT(vcpu);
1491 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1493 gfn = gva >> PAGE_SHIFT;
1495 return nonpaging_map(vcpu, gva & PAGE_MASK,
1496 error_code & PFERR_WRITE_MASK, gfn);
1499 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1500 u32 error_code)
1502 pfn_t pfn;
1503 int r;
1504 int largepage = 0;
1505 gfn_t gfn = gpa >> PAGE_SHIFT;
1506 unsigned long mmu_seq;
1508 ASSERT(vcpu);
1509 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1511 r = mmu_topup_memory_caches(vcpu);
1512 if (r)
1513 return r;
1515 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1516 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1517 largepage = 1;
1519 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1520 smp_rmb();
1521 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1522 if (is_error_pfn(pfn)) {
1523 kvm_release_pfn_clean(pfn);
1524 return 1;
1526 spin_lock(&vcpu->kvm->mmu_lock);
1527 if (mmu_notifier_retry(vcpu, mmu_seq))
1528 goto out_unlock;
1529 kvm_mmu_free_some_pages(vcpu);
1530 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1531 largepage, gfn, pfn);
1532 spin_unlock(&vcpu->kvm->mmu_lock);
1534 return r;
1536 out_unlock:
1537 spin_unlock(&vcpu->kvm->mmu_lock);
1538 kvm_release_pfn_clean(pfn);
1539 return 0;
1542 static void nonpaging_free(struct kvm_vcpu *vcpu)
1544 mmu_free_roots(vcpu);
1547 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1549 struct kvm_mmu *context = &vcpu->arch.mmu;
1551 context->new_cr3 = nonpaging_new_cr3;
1552 context->page_fault = nonpaging_page_fault;
1553 context->gva_to_gpa = nonpaging_gva_to_gpa;
1554 context->free = nonpaging_free;
1555 context->prefetch_page = nonpaging_prefetch_page;
1556 context->sync_page = nonpaging_sync_page;
1557 context->root_level = 0;
1558 context->shadow_root_level = PT32E_ROOT_LEVEL;
1559 context->root_hpa = INVALID_PAGE;
1560 return 0;
1563 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
1565 ++vcpu->stat.tlb_flush;
1566 kvm_x86_ops->tlb_flush(vcpu);
1569 static void paging_new_cr3(struct kvm_vcpu *vcpu)
1571 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
1572 mmu_free_roots(vcpu);
1575 static void inject_page_fault(struct kvm_vcpu *vcpu,
1576 u64 addr,
1577 u32 err_code)
1579 kvm_inject_page_fault(vcpu, addr, err_code);
1582 static void paging_free(struct kvm_vcpu *vcpu)
1584 nonpaging_free(vcpu);
1587 #define PTTYPE 64
1588 #include "paging_tmpl.h"
1589 #undef PTTYPE
1591 #define PTTYPE 32
1592 #include "paging_tmpl.h"
1593 #undef PTTYPE
1595 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
1597 struct kvm_mmu *context = &vcpu->arch.mmu;
1599 ASSERT(is_pae(vcpu));
1600 context->new_cr3 = paging_new_cr3;
1601 context->page_fault = paging64_page_fault;
1602 context->gva_to_gpa = paging64_gva_to_gpa;
1603 context->prefetch_page = paging64_prefetch_page;
1604 context->sync_page = paging64_sync_page;
1605 context->free = paging_free;
1606 context->root_level = level;
1607 context->shadow_root_level = level;
1608 context->root_hpa = INVALID_PAGE;
1609 return 0;
1612 static int paging64_init_context(struct kvm_vcpu *vcpu)
1614 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1617 static int paging32_init_context(struct kvm_vcpu *vcpu)
1619 struct kvm_mmu *context = &vcpu->arch.mmu;
1621 context->new_cr3 = paging_new_cr3;
1622 context->page_fault = paging32_page_fault;
1623 context->gva_to_gpa = paging32_gva_to_gpa;
1624 context->free = paging_free;
1625 context->prefetch_page = paging32_prefetch_page;
1626 context->sync_page = paging32_sync_page;
1627 context->root_level = PT32_ROOT_LEVEL;
1628 context->shadow_root_level = PT32E_ROOT_LEVEL;
1629 context->root_hpa = INVALID_PAGE;
1630 return 0;
1633 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1635 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1638 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1640 struct kvm_mmu *context = &vcpu->arch.mmu;
1642 context->new_cr3 = nonpaging_new_cr3;
1643 context->page_fault = tdp_page_fault;
1644 context->free = nonpaging_free;
1645 context->prefetch_page = nonpaging_prefetch_page;
1646 context->sync_page = nonpaging_sync_page;
1647 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
1648 context->root_hpa = INVALID_PAGE;
1650 if (!is_paging(vcpu)) {
1651 context->gva_to_gpa = nonpaging_gva_to_gpa;
1652 context->root_level = 0;
1653 } else if (is_long_mode(vcpu)) {
1654 context->gva_to_gpa = paging64_gva_to_gpa;
1655 context->root_level = PT64_ROOT_LEVEL;
1656 } else if (is_pae(vcpu)) {
1657 context->gva_to_gpa = paging64_gva_to_gpa;
1658 context->root_level = PT32E_ROOT_LEVEL;
1659 } else {
1660 context->gva_to_gpa = paging32_gva_to_gpa;
1661 context->root_level = PT32_ROOT_LEVEL;
1664 return 0;
1667 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
1669 ASSERT(vcpu);
1670 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
1672 if (!is_paging(vcpu))
1673 return nonpaging_init_context(vcpu);
1674 else if (is_long_mode(vcpu))
1675 return paging64_init_context(vcpu);
1676 else if (is_pae(vcpu))
1677 return paging32E_init_context(vcpu);
1678 else
1679 return paging32_init_context(vcpu);
1682 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1684 vcpu->arch.update_pte.pfn = bad_pfn;
1686 if (tdp_enabled)
1687 return init_kvm_tdp_mmu(vcpu);
1688 else
1689 return init_kvm_softmmu(vcpu);
1692 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1694 ASSERT(vcpu);
1695 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1696 vcpu->arch.mmu.free(vcpu);
1697 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1701 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1703 destroy_kvm_mmu(vcpu);
1704 return init_kvm_mmu(vcpu);
1706 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
1708 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1710 int r;
1712 r = mmu_topup_memory_caches(vcpu);
1713 if (r)
1714 goto out;
1715 spin_lock(&vcpu->kvm->mmu_lock);
1716 kvm_mmu_free_some_pages(vcpu);
1717 mmu_alloc_roots(vcpu);
1718 spin_unlock(&vcpu->kvm->mmu_lock);
1719 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
1720 kvm_mmu_flush_tlb(vcpu);
1721 out:
1722 return r;
1724 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1726 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1728 mmu_free_roots(vcpu);
1731 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1732 struct kvm_mmu_page *sp,
1733 u64 *spte)
1735 u64 pte;
1736 struct kvm_mmu_page *child;
1738 pte = *spte;
1739 if (is_shadow_present_pte(pte)) {
1740 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1741 is_large_pte(pte))
1742 rmap_remove(vcpu->kvm, spte);
1743 else {
1744 child = page_header(pte & PT64_BASE_ADDR_MASK);
1745 mmu_page_remove_parent_pte(child, spte);
1748 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
1749 if (is_large_pte(pte))
1750 --vcpu->kvm->stat.lpages;
1753 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1754 struct kvm_mmu_page *sp,
1755 u64 *spte,
1756 const void *new)
1758 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1759 if (!vcpu->arch.update_pte.largepage ||
1760 sp->role.glevels == PT32_ROOT_LEVEL) {
1761 ++vcpu->kvm->stat.mmu_pde_zapped;
1762 return;
1766 ++vcpu->kvm->stat.mmu_pte_updated;
1767 if (sp->role.glevels == PT32_ROOT_LEVEL)
1768 paging32_update_pte(vcpu, sp, spte, new);
1769 else
1770 paging64_update_pte(vcpu, sp, spte, new);
1773 static bool need_remote_flush(u64 old, u64 new)
1775 if (!is_shadow_present_pte(old))
1776 return false;
1777 if (!is_shadow_present_pte(new))
1778 return true;
1779 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1780 return true;
1781 old ^= PT64_NX_MASK;
1782 new ^= PT64_NX_MASK;
1783 return (old & ~new & PT64_PERM_MASK) != 0;
1786 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1788 if (need_remote_flush(old, new))
1789 kvm_flush_remote_tlbs(vcpu->kvm);
1790 else
1791 kvm_mmu_flush_tlb(vcpu);
1794 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1796 u64 *spte = vcpu->arch.last_pte_updated;
1798 return !!(spte && (*spte & shadow_accessed_mask));
1801 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1802 const u8 *new, int bytes)
1804 gfn_t gfn;
1805 int r;
1806 u64 gpte = 0;
1807 pfn_t pfn;
1809 vcpu->arch.update_pte.largepage = 0;
1811 if (bytes != 4 && bytes != 8)
1812 return;
1815 * Assume that the pte write on a page table of the same type
1816 * as the current vcpu paging mode. This is nearly always true
1817 * (might be false while changing modes). Note it is verified later
1818 * by update_pte().
1820 if (is_pae(vcpu)) {
1821 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1822 if ((bytes == 4) && (gpa % 4 == 0)) {
1823 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1824 if (r)
1825 return;
1826 memcpy((void *)&gpte + (gpa % 8), new, 4);
1827 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1828 memcpy((void *)&gpte, new, 8);
1830 } else {
1831 if ((bytes == 4) && (gpa % 4 == 0))
1832 memcpy((void *)&gpte, new, 4);
1834 if (!is_present_pte(gpte))
1835 return;
1836 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1838 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1839 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1840 vcpu->arch.update_pte.largepage = 1;
1842 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
1843 smp_rmb();
1844 pfn = gfn_to_pfn(vcpu->kvm, gfn);
1846 if (is_error_pfn(pfn)) {
1847 kvm_release_pfn_clean(pfn);
1848 return;
1850 vcpu->arch.update_pte.gfn = gfn;
1851 vcpu->arch.update_pte.pfn = pfn;
1854 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1856 u64 *spte = vcpu->arch.last_pte_updated;
1858 if (spte
1859 && vcpu->arch.last_pte_gfn == gfn
1860 && shadow_accessed_mask
1861 && !(*spte & shadow_accessed_mask)
1862 && is_shadow_present_pte(*spte))
1863 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1866 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1867 const u8 *new, int bytes)
1869 gfn_t gfn = gpa >> PAGE_SHIFT;
1870 struct kvm_mmu_page *sp;
1871 struct hlist_node *node, *n;
1872 struct hlist_head *bucket;
1873 unsigned index;
1874 u64 entry, gentry;
1875 u64 *spte;
1876 unsigned offset = offset_in_page(gpa);
1877 unsigned pte_size;
1878 unsigned page_offset;
1879 unsigned misaligned;
1880 unsigned quadrant;
1881 int level;
1882 int flooded = 0;
1883 int npte;
1884 int r;
1886 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
1887 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
1888 spin_lock(&vcpu->kvm->mmu_lock);
1889 kvm_mmu_access_page(vcpu, gfn);
1890 kvm_mmu_free_some_pages(vcpu);
1891 ++vcpu->kvm->stat.mmu_pte_write;
1892 kvm_mmu_audit(vcpu, "pre pte write");
1893 if (gfn == vcpu->arch.last_pt_write_gfn
1894 && !last_updated_pte_accessed(vcpu)) {
1895 ++vcpu->arch.last_pt_write_count;
1896 if (vcpu->arch.last_pt_write_count >= 3)
1897 flooded = 1;
1898 } else {
1899 vcpu->arch.last_pt_write_gfn = gfn;
1900 vcpu->arch.last_pt_write_count = 1;
1901 vcpu->arch.last_pte_updated = NULL;
1903 index = kvm_page_table_hashfn(gfn);
1904 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1905 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1906 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
1907 continue;
1908 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1909 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1910 misaligned |= bytes < 4;
1911 if (misaligned || flooded) {
1913 * Misaligned accesses are too much trouble to fix
1914 * up; also, they usually indicate a page is not used
1915 * as a page table.
1917 * If we're seeing too many writes to a page,
1918 * it may no longer be a page table, or we may be
1919 * forking, in which case it is better to unmap the
1920 * page.
1922 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1923 gpa, bytes, sp->role.word);
1924 kvm_mmu_zap_page(vcpu->kvm, sp);
1925 ++vcpu->kvm->stat.mmu_flooded;
1926 continue;
1928 page_offset = offset;
1929 level = sp->role.level;
1930 npte = 1;
1931 if (sp->role.glevels == PT32_ROOT_LEVEL) {
1932 page_offset <<= 1; /* 32->64 */
1934 * A 32-bit pde maps 4MB while the shadow pdes map
1935 * only 2MB. So we need to double the offset again
1936 * and zap two pdes instead of one.
1938 if (level == PT32_ROOT_LEVEL) {
1939 page_offset &= ~7; /* kill rounding error */
1940 page_offset <<= 1;
1941 npte = 2;
1943 quadrant = page_offset >> PAGE_SHIFT;
1944 page_offset &= ~PAGE_MASK;
1945 if (quadrant != sp->role.quadrant)
1946 continue;
1948 spte = &sp->spt[page_offset / sizeof(*spte)];
1949 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1950 gentry = 0;
1951 r = kvm_read_guest_atomic(vcpu->kvm,
1952 gpa & ~(u64)(pte_size - 1),
1953 &gentry, pte_size);
1954 new = (const void *)&gentry;
1955 if (r < 0)
1956 new = NULL;
1958 while (npte--) {
1959 entry = *spte;
1960 mmu_pte_write_zap_pte(vcpu, sp, spte);
1961 if (new)
1962 mmu_pte_write_new_pte(vcpu, sp, spte, new);
1963 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
1964 ++spte;
1967 kvm_mmu_audit(vcpu, "post pte write");
1968 spin_unlock(&vcpu->kvm->mmu_lock);
1969 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1970 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1971 vcpu->arch.update_pte.pfn = bad_pfn;
1975 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1977 gpa_t gpa;
1978 int r;
1980 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1982 spin_lock(&vcpu->kvm->mmu_lock);
1983 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1984 spin_unlock(&vcpu->kvm->mmu_lock);
1985 return r;
1987 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
1989 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1991 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
1992 struct kvm_mmu_page *sp;
1994 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
1995 struct kvm_mmu_page, link);
1996 kvm_mmu_zap_page(vcpu->kvm, sp);
1997 ++vcpu->kvm->stat.mmu_recycled;
2001 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2003 int r;
2004 enum emulation_result er;
2006 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2007 if (r < 0)
2008 goto out;
2010 if (!r) {
2011 r = 1;
2012 goto out;
2015 r = mmu_topup_memory_caches(vcpu);
2016 if (r)
2017 goto out;
2019 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2021 switch (er) {
2022 case EMULATE_DONE:
2023 return 1;
2024 case EMULATE_DO_MMIO:
2025 ++vcpu->stat.mmio_exits;
2026 return 0;
2027 case EMULATE_FAIL:
2028 kvm_report_emulation_failure(vcpu, "pagetable");
2029 return 1;
2030 default:
2031 BUG();
2033 out:
2034 return r;
2036 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2038 void kvm_enable_tdp(void)
2040 tdp_enabled = true;
2042 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2044 void kvm_disable_tdp(void)
2046 tdp_enabled = false;
2048 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2050 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2052 struct kvm_mmu_page *sp;
2054 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2055 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
2056 struct kvm_mmu_page, link);
2057 kvm_mmu_zap_page(vcpu->kvm, sp);
2058 cond_resched();
2060 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2063 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2065 struct page *page;
2066 int i;
2068 ASSERT(vcpu);
2070 if (vcpu->kvm->arch.n_requested_mmu_pages)
2071 vcpu->kvm->arch.n_free_mmu_pages =
2072 vcpu->kvm->arch.n_requested_mmu_pages;
2073 else
2074 vcpu->kvm->arch.n_free_mmu_pages =
2075 vcpu->kvm->arch.n_alloc_mmu_pages;
2077 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2078 * Therefore we need to allocate shadow page tables in the first
2079 * 4GB of memory, which happens to fit the DMA32 zone.
2081 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2082 if (!page)
2083 goto error_1;
2084 vcpu->arch.mmu.pae_root = page_address(page);
2085 for (i = 0; i < 4; ++i)
2086 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2088 return 0;
2090 error_1:
2091 free_mmu_pages(vcpu);
2092 return -ENOMEM;
2095 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2097 ASSERT(vcpu);
2098 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2100 return alloc_mmu_pages(vcpu);
2103 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2105 ASSERT(vcpu);
2106 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2108 return init_kvm_mmu(vcpu);
2111 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2113 ASSERT(vcpu);
2115 destroy_kvm_mmu(vcpu);
2116 free_mmu_pages(vcpu);
2117 mmu_free_memory_caches(vcpu);
2120 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2122 struct kvm_mmu_page *sp;
2124 spin_lock(&kvm->mmu_lock);
2125 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2126 int i;
2127 u64 *pt;
2129 if (!test_bit(slot, &sp->slot_bitmap))
2130 continue;
2132 pt = sp->spt;
2133 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2134 /* avoid RMW */
2135 if (pt[i] & PT_WRITABLE_MASK)
2136 pt[i] &= ~PT_WRITABLE_MASK;
2138 kvm_flush_remote_tlbs(kvm);
2139 spin_unlock(&kvm->mmu_lock);
2142 void kvm_mmu_zap_all(struct kvm *kvm)
2144 struct kvm_mmu_page *sp, *node;
2146 spin_lock(&kvm->mmu_lock);
2147 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2148 kvm_mmu_zap_page(kvm, sp);
2149 spin_unlock(&kvm->mmu_lock);
2151 kvm_flush_remote_tlbs(kvm);
2154 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2156 struct kvm_mmu_page *page;
2158 page = container_of(kvm->arch.active_mmu_pages.prev,
2159 struct kvm_mmu_page, link);
2160 kvm_mmu_zap_page(kvm, page);
2163 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2165 struct kvm *kvm;
2166 struct kvm *kvm_freed = NULL;
2167 int cache_count = 0;
2169 spin_lock(&kvm_lock);
2171 list_for_each_entry(kvm, &vm_list, vm_list) {
2172 int npages;
2174 if (!down_read_trylock(&kvm->slots_lock))
2175 continue;
2176 spin_lock(&kvm->mmu_lock);
2177 npages = kvm->arch.n_alloc_mmu_pages -
2178 kvm->arch.n_free_mmu_pages;
2179 cache_count += npages;
2180 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2181 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2182 cache_count--;
2183 kvm_freed = kvm;
2185 nr_to_scan--;
2187 spin_unlock(&kvm->mmu_lock);
2188 up_read(&kvm->slots_lock);
2190 if (kvm_freed)
2191 list_move_tail(&kvm_freed->vm_list, &vm_list);
2193 spin_unlock(&kvm_lock);
2195 return cache_count;
2198 static struct shrinker mmu_shrinker = {
2199 .shrink = mmu_shrink,
2200 .seeks = DEFAULT_SEEKS * 10,
2203 static void mmu_destroy_caches(void)
2205 if (pte_chain_cache)
2206 kmem_cache_destroy(pte_chain_cache);
2207 if (rmap_desc_cache)
2208 kmem_cache_destroy(rmap_desc_cache);
2209 if (mmu_page_header_cache)
2210 kmem_cache_destroy(mmu_page_header_cache);
2213 void kvm_mmu_module_exit(void)
2215 mmu_destroy_caches();
2216 unregister_shrinker(&mmu_shrinker);
2219 int kvm_mmu_module_init(void)
2221 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2222 sizeof(struct kvm_pte_chain),
2223 0, 0, NULL);
2224 if (!pte_chain_cache)
2225 goto nomem;
2226 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2227 sizeof(struct kvm_rmap_desc),
2228 0, 0, NULL);
2229 if (!rmap_desc_cache)
2230 goto nomem;
2232 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2233 sizeof(struct kvm_mmu_page),
2234 0, 0, NULL);
2235 if (!mmu_page_header_cache)
2236 goto nomem;
2238 register_shrinker(&mmu_shrinker);
2240 return 0;
2242 nomem:
2243 mmu_destroy_caches();
2244 return -ENOMEM;
2248 * Caculate mmu pages needed for kvm.
2250 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2252 int i;
2253 unsigned int nr_mmu_pages;
2254 unsigned int nr_pages = 0;
2256 for (i = 0; i < kvm->nmemslots; i++)
2257 nr_pages += kvm->memslots[i].npages;
2259 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2260 nr_mmu_pages = max(nr_mmu_pages,
2261 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2263 return nr_mmu_pages;
2266 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2267 unsigned len)
2269 if (len > buffer->len)
2270 return NULL;
2271 return buffer->ptr;
2274 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2275 unsigned len)
2277 void *ret;
2279 ret = pv_mmu_peek_buffer(buffer, len);
2280 if (!ret)
2281 return ret;
2282 buffer->ptr += len;
2283 buffer->len -= len;
2284 buffer->processed += len;
2285 return ret;
2288 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2289 gpa_t addr, gpa_t value)
2291 int bytes = 8;
2292 int r;
2294 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2295 bytes = 4;
2297 r = mmu_topup_memory_caches(vcpu);
2298 if (r)
2299 return r;
2301 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2302 return -EFAULT;
2304 return 1;
2307 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2309 kvm_x86_ops->tlb_flush(vcpu);
2310 return 1;
2313 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2315 spin_lock(&vcpu->kvm->mmu_lock);
2316 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2317 spin_unlock(&vcpu->kvm->mmu_lock);
2318 return 1;
2321 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2322 struct kvm_pv_mmu_op_buffer *buffer)
2324 struct kvm_mmu_op_header *header;
2326 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2327 if (!header)
2328 return 0;
2329 switch (header->op) {
2330 case KVM_MMU_OP_WRITE_PTE: {
2331 struct kvm_mmu_op_write_pte *wpte;
2333 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2334 if (!wpte)
2335 return 0;
2336 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2337 wpte->pte_val);
2339 case KVM_MMU_OP_FLUSH_TLB: {
2340 struct kvm_mmu_op_flush_tlb *ftlb;
2342 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2343 if (!ftlb)
2344 return 0;
2345 return kvm_pv_mmu_flush_tlb(vcpu);
2347 case KVM_MMU_OP_RELEASE_PT: {
2348 struct kvm_mmu_op_release_pt *rpt;
2350 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2351 if (!rpt)
2352 return 0;
2353 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2355 default: return 0;
2359 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2360 gpa_t addr, unsigned long *ret)
2362 int r;
2363 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2365 buffer->ptr = buffer->buf;
2366 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2367 buffer->processed = 0;
2369 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2370 if (r)
2371 goto out;
2373 while (buffer->len) {
2374 r = kvm_pv_mmu_op_one(vcpu, buffer);
2375 if (r < 0)
2376 goto out;
2377 if (r == 0)
2378 break;
2381 r = 1;
2382 out:
2383 *ret = buffer->processed;
2384 return r;
2387 #ifdef AUDIT
2389 static const char *audit_msg;
2391 static gva_t canonicalize(gva_t gva)
2393 #ifdef CONFIG_X86_64
2394 gva = (long long)(gva << 16) >> 16;
2395 #endif
2396 return gva;
2399 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2400 gva_t va, int level)
2402 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2403 int i;
2404 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2406 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2407 u64 ent = pt[i];
2409 if (ent == shadow_trap_nonpresent_pte)
2410 continue;
2412 va = canonicalize(va);
2413 if (level > 1) {
2414 if (ent == shadow_notrap_nonpresent_pte)
2415 printk(KERN_ERR "audit: (%s) nontrapping pte"
2416 " in nonleaf level: levels %d gva %lx"
2417 " level %d pte %llx\n", audit_msg,
2418 vcpu->arch.mmu.root_level, va, level, ent);
2420 audit_mappings_page(vcpu, ent, va, level - 1);
2421 } else {
2422 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
2423 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
2425 if (is_shadow_present_pte(ent)
2426 && (ent & PT64_BASE_ADDR_MASK) != hpa)
2427 printk(KERN_ERR "xx audit error: (%s) levels %d"
2428 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2429 audit_msg, vcpu->arch.mmu.root_level,
2430 va, gpa, hpa, ent,
2431 is_shadow_present_pte(ent));
2432 else if (ent == shadow_notrap_nonpresent_pte
2433 && !is_error_hpa(hpa))
2434 printk(KERN_ERR "audit: (%s) notrap shadow,"
2435 " valid guest gva %lx\n", audit_msg, va);
2436 kvm_release_pfn_clean(pfn);
2442 static void audit_mappings(struct kvm_vcpu *vcpu)
2444 unsigned i;
2446 if (vcpu->arch.mmu.root_level == 4)
2447 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
2448 else
2449 for (i = 0; i < 4; ++i)
2450 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
2451 audit_mappings_page(vcpu,
2452 vcpu->arch.mmu.pae_root[i],
2453 i << 30,
2457 static int count_rmaps(struct kvm_vcpu *vcpu)
2459 int nmaps = 0;
2460 int i, j, k;
2462 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2463 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2464 struct kvm_rmap_desc *d;
2466 for (j = 0; j < m->npages; ++j) {
2467 unsigned long *rmapp = &m->rmap[j];
2469 if (!*rmapp)
2470 continue;
2471 if (!(*rmapp & 1)) {
2472 ++nmaps;
2473 continue;
2475 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
2476 while (d) {
2477 for (k = 0; k < RMAP_EXT; ++k)
2478 if (d->shadow_ptes[k])
2479 ++nmaps;
2480 else
2481 break;
2482 d = d->more;
2486 return nmaps;
2489 static int count_writable_mappings(struct kvm_vcpu *vcpu)
2491 int nmaps = 0;
2492 struct kvm_mmu_page *sp;
2493 int i;
2495 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2496 u64 *pt = sp->spt;
2498 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
2499 continue;
2501 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2502 u64 ent = pt[i];
2504 if (!(ent & PT_PRESENT_MASK))
2505 continue;
2506 if (!(ent & PT_WRITABLE_MASK))
2507 continue;
2508 ++nmaps;
2511 return nmaps;
2514 static void audit_rmap(struct kvm_vcpu *vcpu)
2516 int n_rmap = count_rmaps(vcpu);
2517 int n_actual = count_writable_mappings(vcpu);
2519 if (n_rmap != n_actual)
2520 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2521 __func__, audit_msg, n_rmap, n_actual);
2524 static void audit_write_protection(struct kvm_vcpu *vcpu)
2526 struct kvm_mmu_page *sp;
2527 struct kvm_memory_slot *slot;
2528 unsigned long *rmapp;
2529 gfn_t gfn;
2531 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
2532 if (sp->role.metaphysical)
2533 continue;
2535 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2536 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
2537 rmapp = &slot->rmap[gfn - slot->base_gfn];
2538 if (*rmapp)
2539 printk(KERN_ERR "%s: (%s) shadow page has writable"
2540 " mappings: gfn %lx role %x\n",
2541 __func__, audit_msg, sp->gfn,
2542 sp->role.word);
2546 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2548 int olddbg = dbg;
2550 dbg = 0;
2551 audit_msg = msg;
2552 audit_rmap(vcpu);
2553 audit_write_protection(vcpu);
2554 audit_mappings(vcpu);
2555 dbg = olddbg;
2558 #endif