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.
10 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
24 #include "kvm_cache_regs.h"
27 #include <linux/kvm_host.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
31 #include <linux/highmem.h>
32 #include <linux/module.h>
33 #include <linux/swap.h>
34 #include <linux/hugetlb.h>
35 #include <linux/compiler.h>
36 #include <linux/srcu.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
41 #include <asm/cmpxchg.h>
46 * When setting this variable to true it enables Two-Dimensional-Paging
47 * where the hardware walks 2 page tables:
48 * 1. the guest-virtual to guest-physical
49 * 2. while doing 1. it walks guest-physical to host-physical
50 * If the hardware supports that we don't need to do shadow paging.
52 bool tdp_enabled = false;
56 AUDIT_POST_PAGE_FAULT,
63 char *audit_point_name[] = {
76 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
77 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
81 #define pgprintk(x...) do { } while (0)
82 #define rmap_printk(x...) do { } while (0)
88 module_param(dbg, bool, 0644);
91 static int oos_shadow = 1;
92 module_param(oos_shadow, bool, 0644);
95 #define ASSERT(x) do { } while (0)
99 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
100 __FILE__, __LINE__, #x); \
104 #define PTE_PREFETCH_NUM 8
106 #define PT_FIRST_AVAIL_BITS_SHIFT 9
107 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
109 #define PT64_LEVEL_BITS 9
111 #define PT64_LEVEL_SHIFT(level) \
112 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
114 #define PT64_INDEX(address, level)\
115 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
118 #define PT32_LEVEL_BITS 10
120 #define PT32_LEVEL_SHIFT(level) \
121 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
123 #define PT32_LVL_OFFSET_MASK(level) \
124 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
125 * PT32_LEVEL_BITS))) - 1))
127 #define PT32_INDEX(address, level)\
128 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
131 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
132 #define PT64_DIR_BASE_ADDR_MASK \
133 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
134 #define PT64_LVL_ADDR_MASK(level) \
135 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136 * PT64_LEVEL_BITS))) - 1))
137 #define PT64_LVL_OFFSET_MASK(level) \
138 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT64_LEVEL_BITS))) - 1))
141 #define PT32_BASE_ADDR_MASK PAGE_MASK
142 #define PT32_DIR_BASE_ADDR_MASK \
143 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
144 #define PT32_LVL_ADDR_MASK(level) \
145 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
146 * PT32_LEVEL_BITS))) - 1))
148 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
151 #define PTE_LIST_EXT 4
153 #define ACC_EXEC_MASK 1
154 #define ACC_WRITE_MASK PT_WRITABLE_MASK
155 #define ACC_USER_MASK PT_USER_MASK
156 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
158 #include <trace/events/kvm.h>
160 #define CREATE_TRACE_POINTS
161 #include "mmutrace.h"
163 #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
165 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
167 struct pte_list_desc {
168 u64 *sptes[PTE_LIST_EXT];
169 struct pte_list_desc *more;
172 struct kvm_shadow_walk_iterator {
180 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
181 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
182 shadow_walk_okay(&(_walker)); \
183 shadow_walk_next(&(_walker)))
185 static struct kmem_cache *pte_list_desc_cache;
186 static struct kmem_cache *mmu_page_header_cache;
187 static struct percpu_counter kvm_total_used_mmu_pages;
189 static u64 __read_mostly shadow_trap_nonpresent_pte;
190 static u64 __read_mostly shadow_notrap_nonpresent_pte;
191 static u64 __read_mostly shadow_nx_mask;
192 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
193 static u64 __read_mostly shadow_user_mask;
194 static u64 __read_mostly shadow_accessed_mask;
195 static u64 __read_mostly shadow_dirty_mask;
197 static inline u64 rsvd_bits(int s, int e)
199 return ((1ULL << (e - s + 1)) - 1) << s;
202 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
204 shadow_trap_nonpresent_pte = trap_pte;
205 shadow_notrap_nonpresent_pte = notrap_pte;
207 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
209 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
210 u64 dirty_mask, u64 nx_mask, u64 x_mask)
212 shadow_user_mask = user_mask;
213 shadow_accessed_mask = accessed_mask;
214 shadow_dirty_mask = dirty_mask;
215 shadow_nx_mask = nx_mask;
216 shadow_x_mask = x_mask;
218 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
220 static bool is_write_protection(struct kvm_vcpu *vcpu)
222 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
225 static int is_cpuid_PSE36(void)
230 static int is_nx(struct kvm_vcpu *vcpu)
232 return vcpu->arch.efer & EFER_NX;
235 static int is_shadow_present_pte(u64 pte)
237 return pte != shadow_trap_nonpresent_pte
238 && pte != shadow_notrap_nonpresent_pte;
241 static int is_large_pte(u64 pte)
243 return pte & PT_PAGE_SIZE_MASK;
246 static int is_writable_pte(unsigned long pte)
248 return pte & PT_WRITABLE_MASK;
251 static int is_dirty_gpte(unsigned long pte)
253 return pte & PT_DIRTY_MASK;
256 static int is_rmap_spte(u64 pte)
258 return is_shadow_present_pte(pte);
261 static int is_last_spte(u64 pte, int level)
263 if (level == PT_PAGE_TABLE_LEVEL)
265 if (is_large_pte(pte))
270 static pfn_t spte_to_pfn(u64 pte)
272 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
275 static gfn_t pse36_gfn_delta(u32 gpte)
277 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
279 return (gpte & PT32_DIR_PSE36_MASK) << shift;
282 static void __set_spte(u64 *sptep, u64 spte)
284 set_64bit(sptep, spte);
287 static u64 __xchg_spte(u64 *sptep, u64 new_spte)
290 return xchg(sptep, new_spte);
296 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
302 static bool spte_has_volatile_bits(u64 spte)
304 if (!shadow_accessed_mask)
307 if (!is_shadow_present_pte(spte))
310 if ((spte & shadow_accessed_mask) &&
311 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
317 static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
319 return (old_spte & bit_mask) && !(new_spte & bit_mask);
322 static void update_spte(u64 *sptep, u64 new_spte)
324 u64 mask, old_spte = *sptep;
326 WARN_ON(!is_rmap_spte(new_spte));
328 new_spte |= old_spte & shadow_dirty_mask;
330 mask = shadow_accessed_mask;
331 if (is_writable_pte(old_spte))
332 mask |= shadow_dirty_mask;
334 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
335 __set_spte(sptep, new_spte);
337 old_spte = __xchg_spte(sptep, new_spte);
339 if (!shadow_accessed_mask)
342 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
343 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
344 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
345 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
348 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
349 struct kmem_cache *base_cache, int min)
353 if (cache->nobjs >= min)
355 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
356 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
359 cache->objects[cache->nobjs++] = obj;
364 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
365 struct kmem_cache *cache)
368 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
371 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
376 if (cache->nobjs >= min)
378 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
379 page = (void *)__get_free_page(GFP_KERNEL);
382 cache->objects[cache->nobjs++] = page;
387 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
390 free_page((unsigned long)mc->objects[--mc->nobjs]);
393 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
397 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
398 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
401 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
404 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
405 mmu_page_header_cache, 4);
410 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
412 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
413 pte_list_desc_cache);
414 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
415 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
416 mmu_page_header_cache);
419 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
425 p = mc->objects[--mc->nobjs];
429 static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
431 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
432 sizeof(struct pte_list_desc));
435 static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
437 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
440 static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
442 if (!sp->role.direct)
443 return sp->gfns[index];
445 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
448 static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
451 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
453 sp->gfns[index] = gfn;
457 * Return the pointer to the large page information for a given gfn,
458 * handling slots that are not large page aligned.
460 static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
461 struct kvm_memory_slot *slot,
466 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
467 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
468 return &slot->lpage_info[level - 2][idx];
471 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
473 struct kvm_memory_slot *slot;
474 struct kvm_lpage_info *linfo;
477 slot = gfn_to_memslot(kvm, gfn);
478 for (i = PT_DIRECTORY_LEVEL;
479 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
480 linfo = lpage_info_slot(gfn, slot, i);
481 linfo->write_count += 1;
483 kvm->arch.indirect_shadow_pages++;
486 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
488 struct kvm_memory_slot *slot;
489 struct kvm_lpage_info *linfo;
492 slot = gfn_to_memslot(kvm, gfn);
493 for (i = PT_DIRECTORY_LEVEL;
494 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
495 linfo = lpage_info_slot(gfn, slot, i);
496 linfo->write_count -= 1;
497 WARN_ON(linfo->write_count < 0);
499 kvm->arch.indirect_shadow_pages--;
502 static int has_wrprotected_page(struct kvm *kvm,
506 struct kvm_memory_slot *slot;
507 struct kvm_lpage_info *linfo;
509 slot = gfn_to_memslot(kvm, gfn);
511 linfo = lpage_info_slot(gfn, slot, level);
512 return linfo->write_count;
518 static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
520 unsigned long page_size;
523 page_size = kvm_host_page_size(kvm, gfn);
525 for (i = PT_PAGE_TABLE_LEVEL;
526 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
527 if (page_size >= KVM_HPAGE_SIZE(i))
536 static struct kvm_memory_slot *
537 gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
540 struct kvm_memory_slot *slot;
542 slot = gfn_to_memslot(vcpu->kvm, gfn);
543 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
544 (no_dirty_log && slot->dirty_bitmap))
550 static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
552 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
555 static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
557 int host_level, level, max_level;
559 host_level = host_mapping_level(vcpu->kvm, large_gfn);
561 if (host_level == PT_PAGE_TABLE_LEVEL)
564 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
565 kvm_x86_ops->get_lpage_level() : host_level;
567 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
568 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
575 * Pte mapping structures:
577 * If pte_list bit zero is zero, then pte_list point to the spte.
579 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
580 * pte_list_desc containing more mappings.
582 * Returns the number of pte entries before the spte was added or zero if
583 * the spte was not added.
586 static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
587 unsigned long *pte_list)
589 struct pte_list_desc *desc;
593 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
594 *pte_list = (unsigned long)spte;
595 } else if (!(*pte_list & 1)) {
596 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
597 desc = mmu_alloc_pte_list_desc(vcpu);
598 desc->sptes[0] = (u64 *)*pte_list;
599 desc->sptes[1] = spte;
600 *pte_list = (unsigned long)desc | 1;
603 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
604 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
605 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
607 count += PTE_LIST_EXT;
609 if (desc->sptes[PTE_LIST_EXT-1]) {
610 desc->more = mmu_alloc_pte_list_desc(vcpu);
613 for (i = 0; desc->sptes[i]; ++i)
615 desc->sptes[i] = spte;
620 static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
622 struct pte_list_desc *desc;
628 else if (!(*pte_list & 1)) {
630 return (u64 *)*pte_list;
633 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
636 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
637 if (prev_spte == spte)
638 return desc->sptes[i];
639 prev_spte = desc->sptes[i];
647 pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
648 int i, struct pte_list_desc *prev_desc)
652 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
654 desc->sptes[i] = desc->sptes[j];
655 desc->sptes[j] = NULL;
658 if (!prev_desc && !desc->more)
659 *pte_list = (unsigned long)desc->sptes[0];
662 prev_desc->more = desc->more;
664 *pte_list = (unsigned long)desc->more | 1;
665 mmu_free_pte_list_desc(desc);
668 static void pte_list_remove(u64 *spte, unsigned long *pte_list)
670 struct pte_list_desc *desc;
671 struct pte_list_desc *prev_desc;
675 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
677 } else if (!(*pte_list & 1)) {
678 rmap_printk("pte_list_remove: %p 1->0\n", spte);
679 if ((u64 *)*pte_list != spte) {
680 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
685 rmap_printk("pte_list_remove: %p many->many\n", spte);
686 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
689 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
690 if (desc->sptes[i] == spte) {
691 pte_list_desc_remove_entry(pte_list,
699 pr_err("pte_list_remove: %p many->many\n", spte);
704 typedef void (*pte_list_walk_fn) (u64 *spte);
705 static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
707 struct pte_list_desc *desc;
713 if (!(*pte_list & 1))
714 return fn((u64 *)*pte_list);
716 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
718 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
725 * Take gfn and return the reverse mapping to it.
727 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
729 struct kvm_memory_slot *slot;
730 struct kvm_lpage_info *linfo;
732 slot = gfn_to_memslot(kvm, gfn);
733 if (likely(level == PT_PAGE_TABLE_LEVEL))
734 return &slot->rmap[gfn - slot->base_gfn];
736 linfo = lpage_info_slot(gfn, slot, level);
738 return &linfo->rmap_pde;
741 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
743 struct kvm_mmu_page *sp;
744 unsigned long *rmapp;
746 if (!is_rmap_spte(*spte))
749 sp = page_header(__pa(spte));
750 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
751 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
752 return pte_list_add(vcpu, spte, rmapp);
755 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
757 return pte_list_next(rmapp, spte);
760 static void rmap_remove(struct kvm *kvm, u64 *spte)
762 struct kvm_mmu_page *sp;
764 unsigned long *rmapp;
766 sp = page_header(__pa(spte));
767 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
768 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
769 pte_list_remove(spte, rmapp);
772 static int set_spte_track_bits(u64 *sptep, u64 new_spte)
775 u64 old_spte = *sptep;
777 if (!spte_has_volatile_bits(old_spte))
778 __set_spte(sptep, new_spte);
780 old_spte = __xchg_spte(sptep, new_spte);
782 if (!is_rmap_spte(old_spte))
785 pfn = spte_to_pfn(old_spte);
786 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
787 kvm_set_pfn_accessed(pfn);
788 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
789 kvm_set_pfn_dirty(pfn);
793 static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
795 if (set_spte_track_bits(sptep, new_spte))
796 rmap_remove(kvm, sptep);
799 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
801 unsigned long *rmapp;
803 int i, write_protected = 0;
805 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
807 spte = rmap_next(kvm, rmapp, NULL);
810 BUG_ON(!(*spte & PT_PRESENT_MASK));
811 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
812 if (is_writable_pte(*spte)) {
813 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
816 spte = rmap_next(kvm, rmapp, spte);
819 /* check for huge page mappings */
820 for (i = PT_DIRECTORY_LEVEL;
821 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
822 rmapp = gfn_to_rmap(kvm, gfn, i);
823 spte = rmap_next(kvm, rmapp, NULL);
826 BUG_ON(!(*spte & PT_PRESENT_MASK));
827 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
828 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
829 if (is_writable_pte(*spte)) {
831 shadow_trap_nonpresent_pte);
836 spte = rmap_next(kvm, rmapp, spte);
840 return write_protected;
843 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
847 int need_tlb_flush = 0;
849 while ((spte = rmap_next(kvm, rmapp, NULL))) {
850 BUG_ON(!(*spte & PT_PRESENT_MASK));
851 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
852 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
855 return need_tlb_flush;
858 static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
863 pte_t *ptep = (pte_t *)data;
866 WARN_ON(pte_huge(*ptep));
867 new_pfn = pte_pfn(*ptep);
868 spte = rmap_next(kvm, rmapp, NULL);
870 BUG_ON(!is_shadow_present_pte(*spte));
871 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
873 if (pte_write(*ptep)) {
874 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
875 spte = rmap_next(kvm, rmapp, NULL);
877 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
878 new_spte |= (u64)new_pfn << PAGE_SHIFT;
880 new_spte &= ~PT_WRITABLE_MASK;
881 new_spte &= ~SPTE_HOST_WRITEABLE;
882 new_spte &= ~shadow_accessed_mask;
883 set_spte_track_bits(spte, new_spte);
884 spte = rmap_next(kvm, rmapp, spte);
888 kvm_flush_remote_tlbs(kvm);
893 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
895 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
901 struct kvm_memslots *slots;
903 slots = kvm_memslots(kvm);
905 for (i = 0; i < slots->nmemslots; i++) {
906 struct kvm_memory_slot *memslot = &slots->memslots[i];
907 unsigned long start = memslot->userspace_addr;
910 end = start + (memslot->npages << PAGE_SHIFT);
911 if (hva >= start && hva < end) {
912 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
913 gfn_t gfn = memslot->base_gfn + gfn_offset;
915 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
917 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
918 struct kvm_lpage_info *linfo;
920 linfo = lpage_info_slot(gfn, memslot,
921 PT_DIRECTORY_LEVEL + j);
922 ret |= handler(kvm, &linfo->rmap_pde, data);
924 trace_kvm_age_page(hva, memslot, ret);
932 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
934 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
937 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
939 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
942 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
949 * Emulate the accessed bit for EPT, by checking if this page has
950 * an EPT mapping, and clearing it if it does. On the next access,
951 * a new EPT mapping will be established.
952 * This has some overhead, but not as much as the cost of swapping
953 * out actively used pages or breaking up actively used hugepages.
955 if (!shadow_accessed_mask)
956 return kvm_unmap_rmapp(kvm, rmapp, data);
958 spte = rmap_next(kvm, rmapp, NULL);
962 BUG_ON(!(_spte & PT_PRESENT_MASK));
963 _young = _spte & PT_ACCESSED_MASK;
966 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
968 spte = rmap_next(kvm, rmapp, spte);
973 static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
980 * If there's no access bit in the secondary pte set by the
981 * hardware it's up to gup-fast/gup to set the access bit in
982 * the primary pte or in the page structure.
984 if (!shadow_accessed_mask)
987 spte = rmap_next(kvm, rmapp, NULL);
990 BUG_ON(!(_spte & PT_PRESENT_MASK));
991 young = _spte & PT_ACCESSED_MASK;
996 spte = rmap_next(kvm, rmapp, spte);
1002 #define RMAP_RECYCLE_THRESHOLD 1000
1004 static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
1006 unsigned long *rmapp;
1007 struct kvm_mmu_page *sp;
1009 sp = page_header(__pa(spte));
1011 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
1013 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
1014 kvm_flush_remote_tlbs(vcpu->kvm);
1017 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1019 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
1022 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1024 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1028 static int is_empty_shadow_page(u64 *spt)
1033 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
1034 if (is_shadow_present_pte(*pos)) {
1035 printk(KERN_ERR "%s: %p %llx\n", __func__,
1044 * This value is the sum of all of the kvm instances's
1045 * kvm->arch.n_used_mmu_pages values. We need a global,
1046 * aggregate version in order to make the slab shrinker
1049 static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1051 kvm->arch.n_used_mmu_pages += nr;
1052 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1055 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1057 ASSERT(is_empty_shadow_page(sp->spt));
1058 hlist_del(&sp->hash_link);
1059 list_del(&sp->link);
1060 free_page((unsigned long)sp->spt);
1061 if (!sp->role.direct)
1062 free_page((unsigned long)sp->gfns);
1063 kmem_cache_free(mmu_page_header_cache, sp);
1064 kvm_mod_used_mmu_pages(kvm, -1);
1067 static unsigned kvm_page_table_hashfn(gfn_t gfn)
1069 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
1072 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1073 struct kvm_mmu_page *sp, u64 *parent_pte)
1078 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1081 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1084 pte_list_remove(parent_pte, &sp->parent_ptes);
1087 static void drop_parent_pte(struct kvm_mmu_page *sp,
1090 mmu_page_remove_parent_pte(sp, parent_pte);
1091 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1094 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
1095 u64 *parent_pte, int direct)
1097 struct kvm_mmu_page *sp;
1098 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1100 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
1102 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1104 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
1105 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
1106 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
1107 sp->parent_ptes = 0;
1108 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1109 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
1113 static void mark_unsync(u64 *spte);
1114 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
1116 pte_list_walk(&sp->parent_ptes, mark_unsync);
1119 static void mark_unsync(u64 *spte)
1121 struct kvm_mmu_page *sp;
1124 sp = page_header(__pa(spte));
1125 index = spte - sp->spt;
1126 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1128 if (sp->unsync_children++)
1130 kvm_mmu_mark_parents_unsync(sp);
1133 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1134 struct kvm_mmu_page *sp)
1138 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1139 sp->spt[i] = shadow_trap_nonpresent_pte;
1142 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1143 struct kvm_mmu_page *sp)
1148 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1152 static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1153 struct kvm_mmu_page *sp, u64 *spte,
1159 #define KVM_PAGE_ARRAY_NR 16
1161 struct kvm_mmu_pages {
1162 struct mmu_page_and_offset {
1163 struct kvm_mmu_page *sp;
1165 } page[KVM_PAGE_ARRAY_NR];
1169 #define for_each_unsync_children(bitmap, idx) \
1170 for (idx = find_first_bit(bitmap, 512); \
1172 idx = find_next_bit(bitmap, 512, idx+1))
1174 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1180 for (i=0; i < pvec->nr; i++)
1181 if (pvec->page[i].sp == sp)
1184 pvec->page[pvec->nr].sp = sp;
1185 pvec->page[pvec->nr].idx = idx;
1187 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1190 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1191 struct kvm_mmu_pages *pvec)
1193 int i, ret, nr_unsync_leaf = 0;
1195 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1196 struct kvm_mmu_page *child;
1197 u64 ent = sp->spt[i];
1199 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1200 goto clear_child_bitmap;
1202 child = page_header(ent & PT64_BASE_ADDR_MASK);
1204 if (child->unsync_children) {
1205 if (mmu_pages_add(pvec, child, i))
1208 ret = __mmu_unsync_walk(child, pvec);
1210 goto clear_child_bitmap;
1212 nr_unsync_leaf += ret;
1215 } else if (child->unsync) {
1217 if (mmu_pages_add(pvec, child, i))
1220 goto clear_child_bitmap;
1225 __clear_bit(i, sp->unsync_child_bitmap);
1226 sp->unsync_children--;
1227 WARN_ON((int)sp->unsync_children < 0);
1231 return nr_unsync_leaf;
1234 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1235 struct kvm_mmu_pages *pvec)
1237 if (!sp->unsync_children)
1240 mmu_pages_add(pvec, sp, 0);
1241 return __mmu_unsync_walk(sp, pvec);
1244 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1246 WARN_ON(!sp->unsync);
1247 trace_kvm_mmu_sync_page(sp);
1249 --kvm->stat.mmu_unsync;
1252 static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1253 struct list_head *invalid_list);
1254 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1255 struct list_head *invalid_list);
1257 #define for_each_gfn_sp(kvm, sp, gfn, pos) \
1258 hlist_for_each_entry(sp, pos, \
1259 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1260 if ((sp)->gfn != (gfn)) {} else
1262 #define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1263 hlist_for_each_entry(sp, pos, \
1264 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1265 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1266 (sp)->role.invalid) {} else
1268 /* @sp->gfn should be write-protected at the call site */
1269 static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1270 struct list_head *invalid_list, bool clear_unsync)
1272 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
1273 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
1278 kvm_unlink_unsync_page(vcpu->kvm, sp);
1280 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1281 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
1285 kvm_mmu_flush_tlb(vcpu);
1289 static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1290 struct kvm_mmu_page *sp)
1292 LIST_HEAD(invalid_list);
1295 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
1297 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1302 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1303 struct list_head *invalid_list)
1305 return __kvm_sync_page(vcpu, sp, invalid_list, true);
1308 /* @gfn should be write-protected at the call site */
1309 static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1311 struct kvm_mmu_page *s;
1312 struct hlist_node *node;
1313 LIST_HEAD(invalid_list);
1316 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
1320 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1321 kvm_unlink_unsync_page(vcpu->kvm, s);
1322 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
1323 (vcpu->arch.mmu.sync_page(vcpu, s))) {
1324 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
1330 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1332 kvm_mmu_flush_tlb(vcpu);
1335 struct mmu_page_path {
1336 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1337 unsigned int idx[PT64_ROOT_LEVEL-1];
1340 #define for_each_sp(pvec, sp, parents, i) \
1341 for (i = mmu_pages_next(&pvec, &parents, -1), \
1342 sp = pvec.page[i].sp; \
1343 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1344 i = mmu_pages_next(&pvec, &parents, i))
1346 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1347 struct mmu_page_path *parents,
1352 for (n = i+1; n < pvec->nr; n++) {
1353 struct kvm_mmu_page *sp = pvec->page[n].sp;
1355 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1356 parents->idx[0] = pvec->page[n].idx;
1360 parents->parent[sp->role.level-2] = sp;
1361 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1367 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1369 struct kvm_mmu_page *sp;
1370 unsigned int level = 0;
1373 unsigned int idx = parents->idx[level];
1375 sp = parents->parent[level];
1379 --sp->unsync_children;
1380 WARN_ON((int)sp->unsync_children < 0);
1381 __clear_bit(idx, sp->unsync_child_bitmap);
1383 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1386 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1387 struct mmu_page_path *parents,
1388 struct kvm_mmu_pages *pvec)
1390 parents->parent[parent->role.level-1] = NULL;
1394 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1395 struct kvm_mmu_page *parent)
1398 struct kvm_mmu_page *sp;
1399 struct mmu_page_path parents;
1400 struct kvm_mmu_pages pages;
1401 LIST_HEAD(invalid_list);
1403 kvm_mmu_pages_init(parent, &parents, &pages);
1404 while (mmu_unsync_walk(parent, &pages)) {
1407 for_each_sp(pages, sp, parents, i)
1408 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1411 kvm_flush_remote_tlbs(vcpu->kvm);
1413 for_each_sp(pages, sp, parents, i) {
1414 kvm_sync_page(vcpu, sp, &invalid_list);
1415 mmu_pages_clear_parents(&parents);
1417 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1418 cond_resched_lock(&vcpu->kvm->mmu_lock);
1419 kvm_mmu_pages_init(parent, &parents, &pages);
1423 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1431 union kvm_mmu_page_role role;
1433 struct kvm_mmu_page *sp;
1434 struct hlist_node *node;
1435 bool need_sync = false;
1437 role = vcpu->arch.mmu.base_role;
1439 role.direct = direct;
1442 role.access = access;
1443 if (!vcpu->arch.mmu.direct_map
1444 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1445 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1446 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1447 role.quadrant = quadrant;
1449 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
1450 if (!need_sync && sp->unsync)
1453 if (sp->role.word != role.word)
1456 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1459 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1460 if (sp->unsync_children) {
1461 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
1462 kvm_mmu_mark_parents_unsync(sp);
1463 } else if (sp->unsync)
1464 kvm_mmu_mark_parents_unsync(sp);
1466 trace_kvm_mmu_get_page(sp, false);
1469 ++vcpu->kvm->stat.mmu_cache_miss;
1470 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
1475 hlist_add_head(&sp->hash_link,
1476 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
1478 if (rmap_write_protect(vcpu->kvm, gfn))
1479 kvm_flush_remote_tlbs(vcpu->kvm);
1480 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1481 kvm_sync_pages(vcpu, gfn);
1483 account_shadowed(vcpu->kvm, gfn);
1485 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1486 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1488 nonpaging_prefetch_page(vcpu, sp);
1489 trace_kvm_mmu_get_page(sp, true);
1493 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1494 struct kvm_vcpu *vcpu, u64 addr)
1496 iterator->addr = addr;
1497 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1498 iterator->level = vcpu->arch.mmu.shadow_root_level;
1500 if (iterator->level == PT64_ROOT_LEVEL &&
1501 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1502 !vcpu->arch.mmu.direct_map)
1505 if (iterator->level == PT32E_ROOT_LEVEL) {
1506 iterator->shadow_addr
1507 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1508 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1510 if (!iterator->shadow_addr)
1511 iterator->level = 0;
1515 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1517 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1520 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1521 if (is_large_pte(*iterator->sptep))
1524 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1525 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1529 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1531 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1535 static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1539 spte = __pa(sp->spt)
1540 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1541 | PT_WRITABLE_MASK | PT_USER_MASK;
1542 __set_spte(sptep, spte);
1545 static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1547 if (is_large_pte(*sptep)) {
1548 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1549 kvm_flush_remote_tlbs(vcpu->kvm);
1553 static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1554 unsigned direct_access)
1556 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1557 struct kvm_mmu_page *child;
1560 * For the direct sp, if the guest pte's dirty bit
1561 * changed form clean to dirty, it will corrupt the
1562 * sp's access: allow writable in the read-only sp,
1563 * so we should update the spte at this point to get
1564 * a new sp with the correct access.
1566 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1567 if (child->role.access == direct_access)
1570 drop_parent_pte(child, sptep);
1571 kvm_flush_remote_tlbs(vcpu->kvm);
1575 static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1579 struct kvm_mmu_page *child;
1582 if (is_shadow_present_pte(pte)) {
1583 if (is_last_spte(pte, sp->role.level))
1584 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
1586 child = page_header(pte & PT64_BASE_ADDR_MASK);
1587 drop_parent_pte(child, spte);
1590 __set_spte(spte, shadow_trap_nonpresent_pte);
1591 if (is_large_pte(pte))
1595 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1596 struct kvm_mmu_page *sp)
1600 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1601 mmu_page_zap_pte(kvm, sp, sp->spt + i);
1604 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1606 mmu_page_remove_parent_pte(sp, parent_pte);
1609 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1612 struct kvm_vcpu *vcpu;
1614 kvm_for_each_vcpu(i, vcpu, kvm)
1615 vcpu->arch.last_pte_updated = NULL;
1618 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1622 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1623 drop_parent_pte(sp, parent_pte);
1626 static int mmu_zap_unsync_children(struct kvm *kvm,
1627 struct kvm_mmu_page *parent,
1628 struct list_head *invalid_list)
1631 struct mmu_page_path parents;
1632 struct kvm_mmu_pages pages;
1634 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1637 kvm_mmu_pages_init(parent, &parents, &pages);
1638 while (mmu_unsync_walk(parent, &pages)) {
1639 struct kvm_mmu_page *sp;
1641 for_each_sp(pages, sp, parents, i) {
1642 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
1643 mmu_pages_clear_parents(&parents);
1646 kvm_mmu_pages_init(parent, &parents, &pages);
1652 static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1653 struct list_head *invalid_list)
1657 trace_kvm_mmu_prepare_zap_page(sp);
1658 ++kvm->stat.mmu_shadow_zapped;
1659 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
1660 kvm_mmu_page_unlink_children(kvm, sp);
1661 kvm_mmu_unlink_parents(kvm, sp);
1662 if (!sp->role.invalid && !sp->role.direct)
1663 unaccount_shadowed(kvm, sp->gfn);
1665 kvm_unlink_unsync_page(kvm, sp);
1666 if (!sp->root_count) {
1669 list_move(&sp->link, invalid_list);
1671 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1672 kvm_reload_remote_mmus(kvm);
1675 sp->role.invalid = 1;
1676 kvm_mmu_reset_last_pte_updated(kvm);
1680 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1681 struct list_head *invalid_list)
1683 struct kvm_mmu_page *sp;
1685 if (list_empty(invalid_list))
1688 kvm_flush_remote_tlbs(kvm);
1691 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1692 WARN_ON(!sp->role.invalid || sp->root_count);
1693 kvm_mmu_free_page(kvm, sp);
1694 } while (!list_empty(invalid_list));
1699 * Changing the number of mmu pages allocated to the vm
1700 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
1702 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
1704 LIST_HEAD(invalid_list);
1706 * If we set the number of mmu pages to be smaller be than the
1707 * number of actived pages , we must to free some mmu pages before we
1711 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1712 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
1713 !list_empty(&kvm->arch.active_mmu_pages)) {
1714 struct kvm_mmu_page *page;
1716 page = container_of(kvm->arch.active_mmu_pages.prev,
1717 struct kvm_mmu_page, link);
1718 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1719 kvm_mmu_commit_zap_page(kvm, &invalid_list);
1721 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
1724 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
1727 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1729 struct kvm_mmu_page *sp;
1730 struct hlist_node *node;
1731 LIST_HEAD(invalid_list);
1734 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
1737 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
1738 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
1741 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
1743 kvm_mmu_commit_zap_page(kvm, &invalid_list);
1747 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1749 struct kvm_mmu_page *sp;
1750 struct hlist_node *node;
1751 LIST_HEAD(invalid_list);
1753 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
1754 pgprintk("%s: zap %llx %x\n",
1755 __func__, gfn, sp->role.word);
1756 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
1758 kvm_mmu_commit_zap_page(kvm, &invalid_list);
1761 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1763 int slot = memslot_id(kvm, gfn);
1764 struct kvm_mmu_page *sp = page_header(__pa(pte));
1766 __set_bit(slot, sp->slot_bitmap);
1769 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1774 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1777 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1778 if (pt[i] == shadow_notrap_nonpresent_pte)
1779 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
1784 * The function is based on mtrr_type_lookup() in
1785 * arch/x86/kernel/cpu/mtrr/generic.c
1787 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1792 u8 prev_match, curr_match;
1793 int num_var_ranges = KVM_NR_VAR_MTRR;
1795 if (!mtrr_state->enabled)
1798 /* Make end inclusive end, instead of exclusive */
1801 /* Look in fixed ranges. Just return the type as per start */
1802 if (mtrr_state->have_fixed && (start < 0x100000)) {
1805 if (start < 0x80000) {
1807 idx += (start >> 16);
1808 return mtrr_state->fixed_ranges[idx];
1809 } else if (start < 0xC0000) {
1811 idx += ((start - 0x80000) >> 14);
1812 return mtrr_state->fixed_ranges[idx];
1813 } else if (start < 0x1000000) {
1815 idx += ((start - 0xC0000) >> 12);
1816 return mtrr_state->fixed_ranges[idx];
1821 * Look in variable ranges
1822 * Look of multiple ranges matching this address and pick type
1823 * as per MTRR precedence
1825 if (!(mtrr_state->enabled & 2))
1826 return mtrr_state->def_type;
1829 for (i = 0; i < num_var_ranges; ++i) {
1830 unsigned short start_state, end_state;
1832 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1835 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1836 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1837 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1838 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1840 start_state = ((start & mask) == (base & mask));
1841 end_state = ((end & mask) == (base & mask));
1842 if (start_state != end_state)
1845 if ((start & mask) != (base & mask))
1848 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1849 if (prev_match == 0xFF) {
1850 prev_match = curr_match;
1854 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1855 curr_match == MTRR_TYPE_UNCACHABLE)
1856 return MTRR_TYPE_UNCACHABLE;
1858 if ((prev_match == MTRR_TYPE_WRBACK &&
1859 curr_match == MTRR_TYPE_WRTHROUGH) ||
1860 (prev_match == MTRR_TYPE_WRTHROUGH &&
1861 curr_match == MTRR_TYPE_WRBACK)) {
1862 prev_match = MTRR_TYPE_WRTHROUGH;
1863 curr_match = MTRR_TYPE_WRTHROUGH;
1866 if (prev_match != curr_match)
1867 return MTRR_TYPE_UNCACHABLE;
1870 if (prev_match != 0xFF)
1873 return mtrr_state->def_type;
1876 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1880 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1881 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1882 if (mtrr == 0xfe || mtrr == 0xff)
1883 mtrr = MTRR_TYPE_WRBACK;
1886 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1888 static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1890 trace_kvm_mmu_unsync_page(sp);
1891 ++vcpu->kvm->stat.mmu_unsync;
1894 kvm_mmu_mark_parents_unsync(sp);
1895 mmu_convert_notrap(sp);
1898 static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1900 struct kvm_mmu_page *s;
1901 struct hlist_node *node;
1903 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
1906 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1907 __kvm_unsync_page(vcpu, s);
1911 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1914 struct kvm_mmu_page *s;
1915 struct hlist_node *node;
1916 bool need_unsync = false;
1918 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
1922 if (s->role.level != PT_PAGE_TABLE_LEVEL)
1925 if (!need_unsync && !s->unsync) {
1932 kvm_unsync_pages(vcpu, gfn);
1936 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1937 unsigned pte_access, int user_fault,
1938 int write_fault, int dirty, int level,
1939 gfn_t gfn, pfn_t pfn, bool speculative,
1940 bool can_unsync, bool host_writable)
1942 u64 spte, entry = *sptep;
1946 * We don't set the accessed bit, since we sometimes want to see
1947 * whether the guest actually used the pte (in order to detect
1950 spte = PT_PRESENT_MASK;
1952 spte |= shadow_accessed_mask;
1954 pte_access &= ~ACC_WRITE_MASK;
1955 if (pte_access & ACC_EXEC_MASK)
1956 spte |= shadow_x_mask;
1958 spte |= shadow_nx_mask;
1959 if (pte_access & ACC_USER_MASK)
1960 spte |= shadow_user_mask;
1961 if (level > PT_PAGE_TABLE_LEVEL)
1962 spte |= PT_PAGE_SIZE_MASK;
1964 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1965 kvm_is_mmio_pfn(pfn));
1968 spte |= SPTE_HOST_WRITEABLE;
1970 pte_access &= ~ACC_WRITE_MASK;
1972 spte |= (u64)pfn << PAGE_SHIFT;
1974 if ((pte_access & ACC_WRITE_MASK)
1975 || (!vcpu->arch.mmu.direct_map && write_fault
1976 && !is_write_protection(vcpu) && !user_fault)) {
1978 if (level > PT_PAGE_TABLE_LEVEL &&
1979 has_wrprotected_page(vcpu->kvm, gfn, level)) {
1981 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1985 spte |= PT_WRITABLE_MASK;
1987 if (!vcpu->arch.mmu.direct_map
1988 && !(pte_access & ACC_WRITE_MASK)) {
1989 spte &= ~PT_USER_MASK;
1991 * If we converted a user page to a kernel page,
1992 * so that the kernel can write to it when cr0.wp=0,
1993 * then we should prevent the kernel from executing it
1994 * if SMEP is enabled.
1996 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
1997 spte |= PT64_NX_MASK;
2001 * Optimization: for pte sync, if spte was writable the hash
2002 * lookup is unnecessary (and expensive). Write protection
2003 * is responsibility of mmu_get_page / kvm_sync_page.
2004 * Same reasoning can be applied to dirty page accounting.
2006 if (!can_unsync && is_writable_pte(*sptep))
2009 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
2010 pgprintk("%s: found shadow page for %llx, marking ro\n",
2013 pte_access &= ~ACC_WRITE_MASK;
2014 if (is_writable_pte(spte))
2015 spte &= ~PT_WRITABLE_MASK;
2019 if (pte_access & ACC_WRITE_MASK)
2020 mark_page_dirty(vcpu->kvm, gfn);
2023 update_spte(sptep, spte);
2025 * If we overwrite a writable spte with a read-only one we
2026 * should flush remote TLBs. Otherwise rmap_write_protect
2027 * will find a read-only spte, even though the writable spte
2028 * might be cached on a CPU's TLB.
2030 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2031 kvm_flush_remote_tlbs(vcpu->kvm);
2036 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2037 unsigned pt_access, unsigned pte_access,
2038 int user_fault, int write_fault, int dirty,
2039 int *ptwrite, int level, gfn_t gfn,
2040 pfn_t pfn, bool speculative,
2043 int was_rmapped = 0;
2046 pgprintk("%s: spte %llx access %x write_fault %d"
2047 " user_fault %d gfn %llx\n",
2048 __func__, *sptep, pt_access,
2049 write_fault, user_fault, gfn);
2051 if (is_rmap_spte(*sptep)) {
2053 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2054 * the parent of the now unreachable PTE.
2056 if (level > PT_PAGE_TABLE_LEVEL &&
2057 !is_large_pte(*sptep)) {
2058 struct kvm_mmu_page *child;
2061 child = page_header(pte & PT64_BASE_ADDR_MASK);
2062 drop_parent_pte(child, sptep);
2063 kvm_flush_remote_tlbs(vcpu->kvm);
2064 } else if (pfn != spte_to_pfn(*sptep)) {
2065 pgprintk("hfn old %llx new %llx\n",
2066 spte_to_pfn(*sptep), pfn);
2067 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
2068 kvm_flush_remote_tlbs(vcpu->kvm);
2073 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
2074 dirty, level, gfn, pfn, speculative, true,
2078 kvm_mmu_flush_tlb(vcpu);
2081 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
2082 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
2083 is_large_pte(*sptep)? "2MB" : "4kB",
2084 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2086 if (!was_rmapped && is_large_pte(*sptep))
2087 ++vcpu->kvm->stat.lpages;
2089 page_header_update_slot(vcpu->kvm, sptep, gfn);
2091 rmap_count = rmap_add(vcpu, sptep, gfn);
2092 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2093 rmap_recycle(vcpu, sptep, gfn);
2095 kvm_release_pfn_clean(pfn);
2097 vcpu->arch.last_pte_updated = sptep;
2098 vcpu->arch.last_pte_gfn = gfn;
2102 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2106 static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2109 struct kvm_memory_slot *slot;
2112 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
2115 return page_to_pfn(bad_page);
2118 hva = gfn_to_hva_memslot(slot, gfn);
2120 return hva_to_pfn_atomic(vcpu->kvm, hva);
2123 static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2124 struct kvm_mmu_page *sp,
2125 u64 *start, u64 *end)
2127 struct page *pages[PTE_PREFETCH_NUM];
2128 unsigned access = sp->role.access;
2132 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2133 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
2136 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2140 for (i = 0; i < ret; i++, gfn++, start++)
2141 mmu_set_spte(vcpu, start, ACC_ALL,
2142 access, 0, 0, 1, NULL,
2143 sp->role.level, gfn,
2144 page_to_pfn(pages[i]), true, true);
2149 static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2150 struct kvm_mmu_page *sp, u64 *sptep)
2152 u64 *spte, *start = NULL;
2155 WARN_ON(!sp->role.direct);
2157 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2160 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2161 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2164 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2172 static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2174 struct kvm_mmu_page *sp;
2177 * Since it's no accessed bit on EPT, it's no way to
2178 * distinguish between actually accessed translations
2179 * and prefetched, so disable pte prefetch if EPT is
2182 if (!shadow_accessed_mask)
2185 sp = page_header(__pa(sptep));
2186 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2189 __direct_pte_prefetch(vcpu, sp, sptep);
2192 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
2193 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2196 struct kvm_shadow_walk_iterator iterator;
2197 struct kvm_mmu_page *sp;
2201 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
2202 if (iterator.level == level) {
2203 unsigned pte_access = ACC_ALL;
2205 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
2206 0, write, 1, &pt_write,
2207 level, gfn, pfn, prefault, map_writable);
2208 direct_pte_prefetch(vcpu, iterator.sptep);
2209 ++vcpu->stat.pf_fixed;
2213 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
2214 u64 base_addr = iterator.addr;
2216 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2217 pseudo_gfn = base_addr >> PAGE_SHIFT;
2218 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2220 1, ACC_ALL, iterator.sptep);
2222 pgprintk("nonpaging_map: ENOMEM\n");
2223 kvm_release_pfn_clean(pfn);
2227 __set_spte(iterator.sptep,
2229 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2230 | shadow_user_mask | shadow_x_mask
2231 | shadow_accessed_mask);
2237 static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
2241 info.si_signo = SIGBUS;
2243 info.si_code = BUS_MCEERR_AR;
2244 info.si_addr = (void __user *)address;
2245 info.si_addr_lsb = PAGE_SHIFT;
2247 send_sig_info(SIGBUS, &info, tsk);
2250 static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2252 kvm_release_pfn_clean(pfn);
2253 if (is_hwpoison_pfn(pfn)) {
2254 kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current);
2256 } else if (is_fault_pfn(pfn))
2262 static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2263 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2267 int level = *levelp;
2270 * Check if it's a transparent hugepage. If this would be an
2271 * hugetlbfs page, level wouldn't be set to
2272 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2275 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2276 level == PT_PAGE_TABLE_LEVEL &&
2277 PageTransCompound(pfn_to_page(pfn)) &&
2278 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2281 * mmu_notifier_retry was successful and we hold the
2282 * mmu_lock here, so the pmd can't become splitting
2283 * from under us, and in turn
2284 * __split_huge_page_refcount() can't run from under
2285 * us and we can safely transfer the refcount from
2286 * PG_tail to PG_head as we switch the pfn to tail to
2289 *levelp = level = PT_DIRECTORY_LEVEL;
2290 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2291 VM_BUG_ON((gfn & mask) != (pfn & mask));
2295 kvm_release_pfn_clean(pfn);
2297 if (!get_page_unless_zero(pfn_to_page(pfn)))
2304 static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
2305 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2307 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
2314 unsigned long mmu_seq;
2317 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2318 if (likely(!force_pt_level)) {
2319 level = mapping_level(vcpu, gfn);
2321 * This path builds a PAE pagetable - so we can map
2322 * 2mb pages at maximum. Therefore check if the level
2323 * is larger than that.
2325 if (level > PT_DIRECTORY_LEVEL)
2326 level = PT_DIRECTORY_LEVEL;
2328 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2330 level = PT_PAGE_TABLE_LEVEL;
2332 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2335 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
2339 if (is_error_pfn(pfn))
2340 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
2342 spin_lock(&vcpu->kvm->mmu_lock);
2343 if (mmu_notifier_retry(vcpu, mmu_seq))
2345 kvm_mmu_free_some_pages(vcpu);
2346 if (likely(!force_pt_level))
2347 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
2348 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2350 spin_unlock(&vcpu->kvm->mmu_lock);
2356 spin_unlock(&vcpu->kvm->mmu_lock);
2357 kvm_release_pfn_clean(pfn);
2362 static void mmu_free_roots(struct kvm_vcpu *vcpu)
2365 struct kvm_mmu_page *sp;
2366 LIST_HEAD(invalid_list);
2368 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2370 spin_lock(&vcpu->kvm->mmu_lock);
2371 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2372 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2373 vcpu->arch.mmu.direct_map)) {
2374 hpa_t root = vcpu->arch.mmu.root_hpa;
2376 sp = page_header(root);
2378 if (!sp->root_count && sp->role.invalid) {
2379 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2380 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2382 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2383 spin_unlock(&vcpu->kvm->mmu_lock);
2386 for (i = 0; i < 4; ++i) {
2387 hpa_t root = vcpu->arch.mmu.pae_root[i];
2390 root &= PT64_BASE_ADDR_MASK;
2391 sp = page_header(root);
2393 if (!sp->root_count && sp->role.invalid)
2394 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2397 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2399 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2400 spin_unlock(&vcpu->kvm->mmu_lock);
2401 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2404 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2408 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2409 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2416 static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2418 struct kvm_mmu_page *sp;
2421 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2422 spin_lock(&vcpu->kvm->mmu_lock);
2423 kvm_mmu_free_some_pages(vcpu);
2424 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2427 spin_unlock(&vcpu->kvm->mmu_lock);
2428 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2429 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2430 for (i = 0; i < 4; ++i) {
2431 hpa_t root = vcpu->arch.mmu.pae_root[i];
2433 ASSERT(!VALID_PAGE(root));
2434 spin_lock(&vcpu->kvm->mmu_lock);
2435 kvm_mmu_free_some_pages(vcpu);
2436 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2438 PT32_ROOT_LEVEL, 1, ACC_ALL,
2440 root = __pa(sp->spt);
2442 spin_unlock(&vcpu->kvm->mmu_lock);
2443 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2445 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2452 static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
2454 struct kvm_mmu_page *sp;
2459 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
2461 if (mmu_check_root(vcpu, root_gfn))
2465 * Do we shadow a long mode page table? If so we need to
2466 * write-protect the guests page table root.
2468 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
2469 hpa_t root = vcpu->arch.mmu.root_hpa;
2471 ASSERT(!VALID_PAGE(root));
2473 spin_lock(&vcpu->kvm->mmu_lock);
2474 kvm_mmu_free_some_pages(vcpu);
2475 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2477 root = __pa(sp->spt);
2479 spin_unlock(&vcpu->kvm->mmu_lock);
2480 vcpu->arch.mmu.root_hpa = root;
2485 * We shadow a 32 bit page table. This may be a legacy 2-level
2486 * or a PAE 3-level page table. In either case we need to be aware that
2487 * the shadow page table may be a PAE or a long mode page table.
2489 pm_mask = PT_PRESENT_MASK;
2490 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2491 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2493 for (i = 0; i < 4; ++i) {
2494 hpa_t root = vcpu->arch.mmu.pae_root[i];
2496 ASSERT(!VALID_PAGE(root));
2497 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
2498 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
2499 if (!is_present_gpte(pdptr)) {
2500 vcpu->arch.mmu.pae_root[i] = 0;
2503 root_gfn = pdptr >> PAGE_SHIFT;
2504 if (mmu_check_root(vcpu, root_gfn))
2507 spin_lock(&vcpu->kvm->mmu_lock);
2508 kvm_mmu_free_some_pages(vcpu);
2509 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
2512 root = __pa(sp->spt);
2514 spin_unlock(&vcpu->kvm->mmu_lock);
2516 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
2518 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2521 * If we shadow a 32 bit page table with a long mode page
2522 * table we enter this path.
2524 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2525 if (vcpu->arch.mmu.lm_root == NULL) {
2527 * The additional page necessary for this is only
2528 * allocated on demand.
2533 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2534 if (lm_root == NULL)
2537 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2539 vcpu->arch.mmu.lm_root = lm_root;
2542 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2548 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2550 if (vcpu->arch.mmu.direct_map)
2551 return mmu_alloc_direct_roots(vcpu);
2553 return mmu_alloc_shadow_roots(vcpu);
2556 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2559 struct kvm_mmu_page *sp;
2561 if (vcpu->arch.mmu.direct_map)
2564 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2567 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
2568 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
2569 hpa_t root = vcpu->arch.mmu.root_hpa;
2570 sp = page_header(root);
2571 mmu_sync_children(vcpu, sp);
2572 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
2575 for (i = 0; i < 4; ++i) {
2576 hpa_t root = vcpu->arch.mmu.pae_root[i];
2578 if (root && VALID_PAGE(root)) {
2579 root &= PT64_BASE_ADDR_MASK;
2580 sp = page_header(root);
2581 mmu_sync_children(vcpu, sp);
2584 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
2587 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2589 spin_lock(&vcpu->kvm->mmu_lock);
2590 mmu_sync_roots(vcpu);
2591 spin_unlock(&vcpu->kvm->mmu_lock);
2594 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2595 u32 access, struct x86_exception *exception)
2598 exception->error_code = 0;
2602 static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
2604 struct x86_exception *exception)
2607 exception->error_code = 0;
2608 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2611 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2612 u32 error_code, bool prefault)
2617 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2618 r = mmu_topup_memory_caches(vcpu);
2623 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2625 gfn = gva >> PAGE_SHIFT;
2627 return nonpaging_map(vcpu, gva & PAGE_MASK,
2628 error_code & PFERR_WRITE_MASK, gfn, prefault);
2631 static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
2633 struct kvm_arch_async_pf arch;
2635 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
2637 arch.direct_map = vcpu->arch.mmu.direct_map;
2638 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
2640 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2643 static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2645 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2646 kvm_event_needs_reinjection(vcpu)))
2649 return kvm_x86_ops->interrupt_allowed(vcpu);
2652 static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
2653 gva_t gva, pfn_t *pfn, bool write, bool *writable)
2657 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
2660 return false; /* *pfn has correct page already */
2662 put_page(pfn_to_page(*pfn));
2664 if (!prefault && can_do_async_pf(vcpu)) {
2665 trace_kvm_try_async_get_page(gva, gfn);
2666 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2667 trace_kvm_async_pf_doublefault(gva, gfn);
2668 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2670 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2674 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
2679 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
2686 gfn_t gfn = gpa >> PAGE_SHIFT;
2687 unsigned long mmu_seq;
2688 int write = error_code & PFERR_WRITE_MASK;
2692 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2694 r = mmu_topup_memory_caches(vcpu);
2698 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2699 if (likely(!force_pt_level)) {
2700 level = mapping_level(vcpu, gfn);
2701 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2703 level = PT_PAGE_TABLE_LEVEL;
2705 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2708 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
2712 if (is_error_pfn(pfn))
2713 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
2714 spin_lock(&vcpu->kvm->mmu_lock);
2715 if (mmu_notifier_retry(vcpu, mmu_seq))
2717 kvm_mmu_free_some_pages(vcpu);
2718 if (likely(!force_pt_level))
2719 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
2720 r = __direct_map(vcpu, gpa, write, map_writable,
2721 level, gfn, pfn, prefault);
2722 spin_unlock(&vcpu->kvm->mmu_lock);
2727 spin_unlock(&vcpu->kvm->mmu_lock);
2728 kvm_release_pfn_clean(pfn);
2732 static void nonpaging_free(struct kvm_vcpu *vcpu)
2734 mmu_free_roots(vcpu);
2737 static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2738 struct kvm_mmu *context)
2740 context->new_cr3 = nonpaging_new_cr3;
2741 context->page_fault = nonpaging_page_fault;
2742 context->gva_to_gpa = nonpaging_gva_to_gpa;
2743 context->free = nonpaging_free;
2744 context->prefetch_page = nonpaging_prefetch_page;
2745 context->sync_page = nonpaging_sync_page;
2746 context->invlpg = nonpaging_invlpg;
2747 context->update_pte = nonpaging_update_pte;
2748 context->root_level = 0;
2749 context->shadow_root_level = PT32E_ROOT_LEVEL;
2750 context->root_hpa = INVALID_PAGE;
2751 context->direct_map = true;
2752 context->nx = false;
2756 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2758 ++vcpu->stat.tlb_flush;
2759 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2762 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2764 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
2765 mmu_free_roots(vcpu);
2768 static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2770 return kvm_read_cr3(vcpu);
2773 static void inject_page_fault(struct kvm_vcpu *vcpu,
2774 struct x86_exception *fault)
2776 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
2779 static void paging_free(struct kvm_vcpu *vcpu)
2781 nonpaging_free(vcpu);
2784 static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
2788 bit7 = (gpte >> 7) & 1;
2789 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
2793 #include "paging_tmpl.h"
2797 #include "paging_tmpl.h"
2800 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2801 struct kvm_mmu *context,
2804 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2805 u64 exb_bit_rsvd = 0;
2808 exb_bit_rsvd = rsvd_bits(63, 63);
2810 case PT32_ROOT_LEVEL:
2811 /* no rsvd bits for 2 level 4K page table entries */
2812 context->rsvd_bits_mask[0][1] = 0;
2813 context->rsvd_bits_mask[0][0] = 0;
2814 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2816 if (!is_pse(vcpu)) {
2817 context->rsvd_bits_mask[1][1] = 0;
2821 if (is_cpuid_PSE36())
2822 /* 36bits PSE 4MB page */
2823 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2825 /* 32 bits PSE 4MB page */
2826 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2828 case PT32E_ROOT_LEVEL:
2829 context->rsvd_bits_mask[0][2] =
2830 rsvd_bits(maxphyaddr, 63) |
2831 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2832 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2833 rsvd_bits(maxphyaddr, 62); /* PDE */
2834 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2835 rsvd_bits(maxphyaddr, 62); /* PTE */
2836 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2837 rsvd_bits(maxphyaddr, 62) |
2838 rsvd_bits(13, 20); /* large page */
2839 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2841 case PT64_ROOT_LEVEL:
2842 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2843 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2844 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2845 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2846 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2847 rsvd_bits(maxphyaddr, 51);
2848 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2849 rsvd_bits(maxphyaddr, 51);
2850 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2851 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2852 rsvd_bits(maxphyaddr, 51) |
2854 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2855 rsvd_bits(maxphyaddr, 51) |
2856 rsvd_bits(13, 20); /* large page */
2857 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2862 static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2863 struct kvm_mmu *context,
2866 context->nx = is_nx(vcpu);
2868 reset_rsvds_bits_mask(vcpu, context, level);
2870 ASSERT(is_pae(vcpu));
2871 context->new_cr3 = paging_new_cr3;
2872 context->page_fault = paging64_page_fault;
2873 context->gva_to_gpa = paging64_gva_to_gpa;
2874 context->prefetch_page = paging64_prefetch_page;
2875 context->sync_page = paging64_sync_page;
2876 context->invlpg = paging64_invlpg;
2877 context->update_pte = paging64_update_pte;
2878 context->free = paging_free;
2879 context->root_level = level;
2880 context->shadow_root_level = level;
2881 context->root_hpa = INVALID_PAGE;
2882 context->direct_map = false;
2886 static int paging64_init_context(struct kvm_vcpu *vcpu,
2887 struct kvm_mmu *context)
2889 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
2892 static int paging32_init_context(struct kvm_vcpu *vcpu,
2893 struct kvm_mmu *context)
2895 context->nx = false;
2897 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
2899 context->new_cr3 = paging_new_cr3;
2900 context->page_fault = paging32_page_fault;
2901 context->gva_to_gpa = paging32_gva_to_gpa;
2902 context->free = paging_free;
2903 context->prefetch_page = paging32_prefetch_page;
2904 context->sync_page = paging32_sync_page;
2905 context->invlpg = paging32_invlpg;
2906 context->update_pte = paging32_update_pte;
2907 context->root_level = PT32_ROOT_LEVEL;
2908 context->shadow_root_level = PT32E_ROOT_LEVEL;
2909 context->root_hpa = INVALID_PAGE;
2910 context->direct_map = false;
2914 static int paging32E_init_context(struct kvm_vcpu *vcpu,
2915 struct kvm_mmu *context)
2917 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
2920 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2922 struct kvm_mmu *context = vcpu->arch.walk_mmu;
2924 context->base_role.word = 0;
2925 context->new_cr3 = nonpaging_new_cr3;
2926 context->page_fault = tdp_page_fault;
2927 context->free = nonpaging_free;
2928 context->prefetch_page = nonpaging_prefetch_page;
2929 context->sync_page = nonpaging_sync_page;
2930 context->invlpg = nonpaging_invlpg;
2931 context->update_pte = nonpaging_update_pte;
2932 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2933 context->root_hpa = INVALID_PAGE;
2934 context->direct_map = true;
2935 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
2936 context->get_cr3 = get_cr3;
2937 context->inject_page_fault = kvm_inject_page_fault;
2938 context->nx = is_nx(vcpu);
2940 if (!is_paging(vcpu)) {
2941 context->nx = false;
2942 context->gva_to_gpa = nonpaging_gva_to_gpa;
2943 context->root_level = 0;
2944 } else if (is_long_mode(vcpu)) {
2945 context->nx = is_nx(vcpu);
2946 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
2947 context->gva_to_gpa = paging64_gva_to_gpa;
2948 context->root_level = PT64_ROOT_LEVEL;
2949 } else if (is_pae(vcpu)) {
2950 context->nx = is_nx(vcpu);
2951 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
2952 context->gva_to_gpa = paging64_gva_to_gpa;
2953 context->root_level = PT32E_ROOT_LEVEL;
2955 context->nx = false;
2956 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
2957 context->gva_to_gpa = paging32_gva_to_gpa;
2958 context->root_level = PT32_ROOT_LEVEL;
2964 int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
2967 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
2969 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2971 if (!is_paging(vcpu))
2972 r = nonpaging_init_context(vcpu, context);
2973 else if (is_long_mode(vcpu))
2974 r = paging64_init_context(vcpu, context);
2975 else if (is_pae(vcpu))
2976 r = paging32E_init_context(vcpu, context);
2978 r = paging32_init_context(vcpu, context);
2980 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
2981 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
2982 vcpu->arch.mmu.base_role.smep_andnot_wp
2983 = smep && !is_write_protection(vcpu);
2987 EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2989 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2991 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
2993 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2994 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2995 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
3000 static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3002 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3004 g_context->get_cr3 = get_cr3;
3005 g_context->inject_page_fault = kvm_inject_page_fault;
3008 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3009 * translation of l2_gpa to l1_gpa addresses is done using the
3010 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3011 * functions between mmu and nested_mmu are swapped.
3013 if (!is_paging(vcpu)) {
3014 g_context->nx = false;
3015 g_context->root_level = 0;
3016 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3017 } else if (is_long_mode(vcpu)) {
3018 g_context->nx = is_nx(vcpu);
3019 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3020 g_context->root_level = PT64_ROOT_LEVEL;
3021 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3022 } else if (is_pae(vcpu)) {
3023 g_context->nx = is_nx(vcpu);
3024 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3025 g_context->root_level = PT32E_ROOT_LEVEL;
3026 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3028 g_context->nx = false;
3029 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3030 g_context->root_level = PT32_ROOT_LEVEL;
3031 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3037 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3039 if (mmu_is_nested(vcpu))
3040 return init_kvm_nested_mmu(vcpu);
3041 else if (tdp_enabled)
3042 return init_kvm_tdp_mmu(vcpu);
3044 return init_kvm_softmmu(vcpu);
3047 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3050 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3051 /* mmu.free() should set root_hpa = INVALID_PAGE */
3052 vcpu->arch.mmu.free(vcpu);
3055 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3057 destroy_kvm_mmu(vcpu);
3058 return init_kvm_mmu(vcpu);
3060 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
3062 int kvm_mmu_load(struct kvm_vcpu *vcpu)
3066 r = mmu_topup_memory_caches(vcpu);
3069 r = mmu_alloc_roots(vcpu);
3070 spin_lock(&vcpu->kvm->mmu_lock);
3071 mmu_sync_roots(vcpu);
3072 spin_unlock(&vcpu->kvm->mmu_lock);
3075 /* set_cr3() should ensure TLB has been flushed */
3076 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
3080 EXPORT_SYMBOL_GPL(kvm_mmu_load);
3082 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3084 mmu_free_roots(vcpu);
3086 EXPORT_SYMBOL_GPL(kvm_mmu_unload);
3088 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
3089 struct kvm_mmu_page *sp, u64 *spte,
3092 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
3093 ++vcpu->kvm->stat.mmu_pde_zapped;
3097 ++vcpu->kvm->stat.mmu_pte_updated;
3098 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
3101 static bool need_remote_flush(u64 old, u64 new)
3103 if (!is_shadow_present_pte(old))
3105 if (!is_shadow_present_pte(new))
3107 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3109 old ^= PT64_NX_MASK;
3110 new ^= PT64_NX_MASK;
3111 return (old & ~new & PT64_PERM_MASK) != 0;
3114 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3115 bool remote_flush, bool local_flush)
3121 kvm_flush_remote_tlbs(vcpu->kvm);
3122 else if (local_flush)
3123 kvm_mmu_flush_tlb(vcpu);
3126 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3128 u64 *spte = vcpu->arch.last_pte_updated;
3130 return !!(spte && (*spte & shadow_accessed_mask));
3133 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3135 u64 *spte = vcpu->arch.last_pte_updated;
3138 && vcpu->arch.last_pte_gfn == gfn
3139 && shadow_accessed_mask
3140 && !(*spte & shadow_accessed_mask)
3141 && is_shadow_present_pte(*spte))
3142 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3145 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
3146 const u8 *new, int bytes,
3147 bool guest_initiated)
3149 gfn_t gfn = gpa >> PAGE_SHIFT;
3150 union kvm_mmu_page_role mask = { .word = 0 };
3151 struct kvm_mmu_page *sp;
3152 struct hlist_node *node;
3153 LIST_HEAD(invalid_list);
3154 u64 entry, gentry, *spte;
3155 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3156 int level, npte, invlpg_counter, r, flooded = 0;
3157 bool remote_flush, local_flush, zap_page;
3160 * If we don't have indirect shadow pages, it means no page is
3161 * write-protected, so we can exit simply.
3163 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3166 zap_page = remote_flush = local_flush = false;
3167 offset = offset_in_page(gpa);
3169 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
3171 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3174 * Assume that the pte write on a page table of the same type
3175 * as the current vcpu paging mode since we update the sptes only
3176 * when they have the same mode.
3178 if ((is_pae(vcpu) && bytes == 4) || !new) {
3179 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3184 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3187 new = (const u8 *)&gentry;
3192 gentry = *(const u32 *)new;
3195 gentry = *(const u64 *)new;
3202 spin_lock(&vcpu->kvm->mmu_lock);
3203 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3205 kvm_mmu_free_some_pages(vcpu);
3206 ++vcpu->kvm->stat.mmu_pte_write;
3207 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
3208 if (guest_initiated) {
3209 kvm_mmu_access_page(vcpu, gfn);
3210 if (gfn == vcpu->arch.last_pt_write_gfn
3211 && !last_updated_pte_accessed(vcpu)) {
3212 ++vcpu->arch.last_pt_write_count;
3213 if (vcpu->arch.last_pt_write_count >= 3)
3216 vcpu->arch.last_pt_write_gfn = gfn;
3217 vcpu->arch.last_pt_write_count = 1;
3218 vcpu->arch.last_pte_updated = NULL;
3222 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
3223 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
3224 pte_size = sp->role.cr4_pae ? 8 : 4;
3225 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
3226 misaligned |= bytes < 4;
3227 if (misaligned || flooded) {
3229 * Misaligned accesses are too much trouble to fix
3230 * up; also, they usually indicate a page is not used
3233 * If we're seeing too many writes to a page,
3234 * it may no longer be a page table, or we may be
3235 * forking, in which case it is better to unmap the
3238 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
3239 gpa, bytes, sp->role.word);
3240 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
3242 ++vcpu->kvm->stat.mmu_flooded;
3245 page_offset = offset;
3246 level = sp->role.level;
3248 if (!sp->role.cr4_pae) {
3249 page_offset <<= 1; /* 32->64 */
3251 * A 32-bit pde maps 4MB while the shadow pdes map
3252 * only 2MB. So we need to double the offset again
3253 * and zap two pdes instead of one.
3255 if (level == PT32_ROOT_LEVEL) {
3256 page_offset &= ~7; /* kill rounding error */
3260 quadrant = page_offset >> PAGE_SHIFT;
3261 page_offset &= ~PAGE_MASK;
3262 if (quadrant != sp->role.quadrant)
3266 spte = &sp->spt[page_offset / sizeof(*spte)];
3269 mmu_page_zap_pte(vcpu->kvm, sp, spte);
3271 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3273 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
3274 if (!remote_flush && need_remote_flush(entry, *spte))
3275 remote_flush = true;
3279 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
3280 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
3281 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
3282 spin_unlock(&vcpu->kvm->mmu_lock);
3285 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3290 if (vcpu->arch.mmu.direct_map)
3293 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
3295 spin_lock(&vcpu->kvm->mmu_lock);
3296 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
3297 spin_unlock(&vcpu->kvm->mmu_lock);
3300 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
3302 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
3304 LIST_HEAD(invalid_list);
3306 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
3307 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
3308 struct kvm_mmu_page *sp;
3310 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
3311 struct kvm_mmu_page, link);
3312 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
3313 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
3314 ++vcpu->kvm->stat.mmu_recycled;
3318 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3319 void *insn, int insn_len)
3322 enum emulation_result er;
3324 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
3333 r = mmu_topup_memory_caches(vcpu);
3337 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
3342 case EMULATE_DO_MMIO:
3343 ++vcpu->stat.mmio_exits;
3353 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3355 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3357 vcpu->arch.mmu.invlpg(vcpu, gva);
3358 kvm_mmu_flush_tlb(vcpu);
3359 ++vcpu->stat.invlpg;
3361 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3363 void kvm_enable_tdp(void)
3367 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3369 void kvm_disable_tdp(void)
3371 tdp_enabled = false;
3373 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3375 static void free_mmu_pages(struct kvm_vcpu *vcpu)
3377 free_page((unsigned long)vcpu->arch.mmu.pae_root);
3378 if (vcpu->arch.mmu.lm_root != NULL)
3379 free_page((unsigned long)vcpu->arch.mmu.lm_root);
3382 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3390 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3391 * Therefore we need to allocate shadow page tables in the first
3392 * 4GB of memory, which happens to fit the DMA32 zone.
3394 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3398 vcpu->arch.mmu.pae_root = page_address(page);
3399 for (i = 0; i < 4; ++i)
3400 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
3405 int kvm_mmu_create(struct kvm_vcpu *vcpu)
3408 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
3410 return alloc_mmu_pages(vcpu);
3413 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3416 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
3418 return init_kvm_mmu(vcpu);
3421 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
3423 struct kvm_mmu_page *sp;
3425 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
3429 if (!test_bit(slot, sp->slot_bitmap))
3433 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3434 if (!is_shadow_present_pte(pt[i]) ||
3435 !is_last_spte(pt[i], sp->role.level))
3438 if (is_large_pte(pt[i])) {
3439 drop_spte(kvm, &pt[i],
3440 shadow_trap_nonpresent_pte);
3446 if (is_writable_pte(pt[i]))
3447 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
3450 kvm_flush_remote_tlbs(kvm);
3453 void kvm_mmu_zap_all(struct kvm *kvm)
3455 struct kvm_mmu_page *sp, *node;
3456 LIST_HEAD(invalid_list);
3458 spin_lock(&kvm->mmu_lock);
3460 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
3461 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
3464 kvm_mmu_commit_zap_page(kvm, &invalid_list);
3465 spin_unlock(&kvm->mmu_lock);
3468 static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3469 struct list_head *invalid_list)
3471 struct kvm_mmu_page *page;
3473 page = container_of(kvm->arch.active_mmu_pages.prev,
3474 struct kvm_mmu_page, link);
3475 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
3478 static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
3481 struct kvm *kvm_freed = NULL;
3482 int nr_to_scan = sc->nr_to_scan;
3484 if (nr_to_scan == 0)
3487 raw_spin_lock(&kvm_lock);
3489 list_for_each_entry(kvm, &vm_list, vm_list) {
3490 int idx, freed_pages;
3491 LIST_HEAD(invalid_list);
3493 idx = srcu_read_lock(&kvm->srcu);
3494 spin_lock(&kvm->mmu_lock);
3495 if (!kvm_freed && nr_to_scan > 0 &&
3496 kvm->arch.n_used_mmu_pages > 0) {
3497 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3503 kvm_mmu_commit_zap_page(kvm, &invalid_list);
3504 spin_unlock(&kvm->mmu_lock);
3505 srcu_read_unlock(&kvm->srcu, idx);
3508 list_move_tail(&kvm_freed->vm_list, &vm_list);
3510 raw_spin_unlock(&kvm_lock);
3513 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
3516 static struct shrinker mmu_shrinker = {
3517 .shrink = mmu_shrink,
3518 .seeks = DEFAULT_SEEKS * 10,
3521 static void mmu_destroy_caches(void)
3523 if (pte_list_desc_cache)
3524 kmem_cache_destroy(pte_list_desc_cache);
3525 if (mmu_page_header_cache)
3526 kmem_cache_destroy(mmu_page_header_cache);
3529 int kvm_mmu_module_init(void)
3531 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3532 sizeof(struct pte_list_desc),
3534 if (!pte_list_desc_cache)
3537 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3538 sizeof(struct kvm_mmu_page),
3540 if (!mmu_page_header_cache)
3543 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3546 register_shrinker(&mmu_shrinker);
3551 mmu_destroy_caches();
3556 * Caculate mmu pages needed for kvm.
3558 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3561 unsigned int nr_mmu_pages;
3562 unsigned int nr_pages = 0;
3563 struct kvm_memslots *slots;
3565 slots = kvm_memslots(kvm);
3567 for (i = 0; i < slots->nmemslots; i++)
3568 nr_pages += slots->memslots[i].npages;
3570 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3571 nr_mmu_pages = max(nr_mmu_pages,
3572 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3574 return nr_mmu_pages;
3577 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3580 if (len > buffer->len)
3585 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3590 ret = pv_mmu_peek_buffer(buffer, len);
3595 buffer->processed += len;
3599 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3600 gpa_t addr, gpa_t value)
3605 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3608 r = mmu_topup_memory_caches(vcpu);
3612 if (!emulator_write_phys(vcpu, addr, &value, bytes))
3618 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3620 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
3624 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3626 spin_lock(&vcpu->kvm->mmu_lock);
3627 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3628 spin_unlock(&vcpu->kvm->mmu_lock);
3632 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3633 struct kvm_pv_mmu_op_buffer *buffer)
3635 struct kvm_mmu_op_header *header;
3637 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3640 switch (header->op) {
3641 case KVM_MMU_OP_WRITE_PTE: {
3642 struct kvm_mmu_op_write_pte *wpte;
3644 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3647 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3650 case KVM_MMU_OP_FLUSH_TLB: {
3651 struct kvm_mmu_op_flush_tlb *ftlb;
3653 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3656 return kvm_pv_mmu_flush_tlb(vcpu);
3658 case KVM_MMU_OP_RELEASE_PT: {
3659 struct kvm_mmu_op_release_pt *rpt;
3661 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3664 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3670 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3671 gpa_t addr, unsigned long *ret)
3674 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3676 buffer->ptr = buffer->buf;
3677 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3678 buffer->processed = 0;
3680 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3684 while (buffer->len) {
3685 r = kvm_pv_mmu_op_one(vcpu, buffer);
3694 *ret = buffer->processed;
3698 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3700 struct kvm_shadow_walk_iterator iterator;
3703 spin_lock(&vcpu->kvm->mmu_lock);
3704 for_each_shadow_entry(vcpu, addr, iterator) {
3705 sptes[iterator.level-1] = *iterator.sptep;
3707 if (!is_shadow_present_pte(*iterator.sptep))
3710 spin_unlock(&vcpu->kvm->mmu_lock);
3714 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3716 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3720 destroy_kvm_mmu(vcpu);
3721 free_mmu_pages(vcpu);
3722 mmu_free_memory_caches(vcpu);
3725 #ifdef CONFIG_KVM_MMU_AUDIT
3726 #include "mmu_audit.c"
3728 static void mmu_audit_disable(void) { }
3731 void kvm_mmu_module_exit(void)
3733 mmu_destroy_caches();
3734 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3735 unregister_shrinker(&mmu_shrinker);
3736 mmu_audit_disable();