KVM: MMU: Fix free memory accounting race in mmu_alloc_roots()
[linux-2.6.git] / arch / x86 / kvm / mmu.c
blobd7aebafffdfe0cbf5bcb27801db4421e4132cd14
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 "mmu.h"
21 #include "x86.h"
22 #include "kvm_cache_regs.h"
24 #include <linux/kvm_host.h>
25 #include <linux/types.h>
26 #include <linux/string.h>
27 #include <linux/mm.h>
28 #include <linux/highmem.h>
29 #include <linux/module.h>
30 #include <linux/swap.h>
31 #include <linux/hugetlb.h>
32 #include <linux/compiler.h>
33 #include <linux/srcu.h>
34 #include <linux/slab.h>
35 #include <linux/uaccess.h>
37 #include <asm/page.h>
38 #include <asm/cmpxchg.h>
39 #include <asm/io.h>
40 #include <asm/vmx.h>
43 * When setting this variable to true it enables Two-Dimensional-Paging
44 * where the hardware walks 2 page tables:
45 * 1. the guest-virtual to guest-physical
46 * 2. while doing 1. it walks guest-physical to host-physical
47 * If the hardware supports that we don't need to do shadow paging.
49 bool tdp_enabled = false;
51 #undef MMU_DEBUG
53 #undef AUDIT
55 #ifdef AUDIT
56 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
57 #else
58 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
59 #endif
61 #ifdef MMU_DEBUG
63 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
64 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
66 #else
68 #define pgprintk(x...) do { } while (0)
69 #define rmap_printk(x...) do { } while (0)
71 #endif
73 #if defined(MMU_DEBUG) || defined(AUDIT)
74 static int dbg = 0;
75 module_param(dbg, bool, 0644);
76 #endif
78 static int oos_shadow = 1;
79 module_param(oos_shadow, bool, 0644);
81 #ifndef MMU_DEBUG
82 #define ASSERT(x) do { } while (0)
83 #else
84 #define ASSERT(x) \
85 if (!(x)) { \
86 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
87 __FILE__, __LINE__, #x); \
89 #endif
91 #define PT_FIRST_AVAIL_BITS_SHIFT 9
92 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
94 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
96 #define PT64_LEVEL_BITS 9
98 #define PT64_LEVEL_SHIFT(level) \
99 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
101 #define PT64_LEVEL_MASK(level) \
102 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
104 #define PT64_INDEX(address, level)\
105 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
108 #define PT32_LEVEL_BITS 10
110 #define PT32_LEVEL_SHIFT(level) \
111 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
113 #define PT32_LEVEL_MASK(level) \
114 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
115 #define PT32_LVL_OFFSET_MASK(level) \
116 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
117 * PT32_LEVEL_BITS))) - 1))
119 #define PT32_INDEX(address, level)\
120 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
123 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
124 #define PT64_DIR_BASE_ADDR_MASK \
125 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
126 #define PT64_LVL_ADDR_MASK(level) \
127 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
128 * PT64_LEVEL_BITS))) - 1))
129 #define PT64_LVL_OFFSET_MASK(level) \
130 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
131 * PT64_LEVEL_BITS))) - 1))
133 #define PT32_BASE_ADDR_MASK PAGE_MASK
134 #define PT32_DIR_BASE_ADDR_MASK \
135 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
136 #define PT32_LVL_ADDR_MASK(level) \
137 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
138 * PT32_LEVEL_BITS))) - 1))
140 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
141 | PT64_NX_MASK)
143 #define RMAP_EXT 4
145 #define ACC_EXEC_MASK 1
146 #define ACC_WRITE_MASK PT_WRITABLE_MASK
147 #define ACC_USER_MASK PT_USER_MASK
148 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
150 #include <trace/events/kvm.h>
152 #define CREATE_TRACE_POINTS
153 #include "mmutrace.h"
155 #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
157 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
159 struct kvm_rmap_desc {
160 u64 *sptes[RMAP_EXT];
161 struct kvm_rmap_desc *more;
164 struct kvm_shadow_walk_iterator {
165 u64 addr;
166 hpa_t shadow_addr;
167 int level;
168 u64 *sptep;
169 unsigned index;
172 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
173 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
174 shadow_walk_okay(&(_walker)); \
175 shadow_walk_next(&(_walker)))
177 typedef int (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp);
179 static struct kmem_cache *pte_chain_cache;
180 static struct kmem_cache *rmap_desc_cache;
181 static struct kmem_cache *mmu_page_header_cache;
183 static u64 __read_mostly shadow_trap_nonpresent_pte;
184 static u64 __read_mostly shadow_notrap_nonpresent_pte;
185 static u64 __read_mostly shadow_base_present_pte;
186 static u64 __read_mostly shadow_nx_mask;
187 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
188 static u64 __read_mostly shadow_user_mask;
189 static u64 __read_mostly shadow_accessed_mask;
190 static u64 __read_mostly shadow_dirty_mask;
192 static inline u64 rsvd_bits(int s, int e)
194 return ((1ULL << (e - s + 1)) - 1) << s;
197 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
199 shadow_trap_nonpresent_pte = trap_pte;
200 shadow_notrap_nonpresent_pte = notrap_pte;
202 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
204 void kvm_mmu_set_base_ptes(u64 base_pte)
206 shadow_base_present_pte = base_pte;
208 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
210 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
211 u64 dirty_mask, u64 nx_mask, u64 x_mask)
213 shadow_user_mask = user_mask;
214 shadow_accessed_mask = accessed_mask;
215 shadow_dirty_mask = dirty_mask;
216 shadow_nx_mask = nx_mask;
217 shadow_x_mask = x_mask;
219 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
221 static bool is_write_protection(struct kvm_vcpu *vcpu)
223 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
226 static int is_cpuid_PSE36(void)
228 return 1;
231 static int is_nx(struct kvm_vcpu *vcpu)
233 return vcpu->arch.efer & EFER_NX;
236 static int is_shadow_present_pte(u64 pte)
238 return pte != shadow_trap_nonpresent_pte
239 && pte != shadow_notrap_nonpresent_pte;
242 static int is_large_pte(u64 pte)
244 return pte & PT_PAGE_SIZE_MASK;
247 static int is_writable_pte(unsigned long pte)
249 return pte & PT_WRITABLE_MASK;
252 static int is_dirty_gpte(unsigned long pte)
254 return pte & PT_DIRTY_MASK;
257 static int is_rmap_spte(u64 pte)
259 return is_shadow_present_pte(pte);
262 static int is_last_spte(u64 pte, int level)
264 if (level == PT_PAGE_TABLE_LEVEL)
265 return 1;
266 if (is_large_pte(pte))
267 return 1;
268 return 0;
271 static pfn_t spte_to_pfn(u64 pte)
273 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
276 static gfn_t pse36_gfn_delta(u32 gpte)
278 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
280 return (gpte & PT32_DIR_PSE36_MASK) << shift;
283 static void __set_spte(u64 *sptep, u64 spte)
285 #ifdef CONFIG_X86_64
286 set_64bit((unsigned long *)sptep, spte);
287 #else
288 set_64bit((unsigned long long *)sptep, spte);
289 #endif
292 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
293 struct kmem_cache *base_cache, int min)
295 void *obj;
297 if (cache->nobjs >= min)
298 return 0;
299 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
300 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
301 if (!obj)
302 return -ENOMEM;
303 cache->objects[cache->nobjs++] = obj;
305 return 0;
308 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
310 while (mc->nobjs)
311 kfree(mc->objects[--mc->nobjs]);
314 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
315 int min)
317 struct page *page;
319 if (cache->nobjs >= min)
320 return 0;
321 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
322 page = alloc_page(GFP_KERNEL);
323 if (!page)
324 return -ENOMEM;
325 cache->objects[cache->nobjs++] = page_address(page);
327 return 0;
330 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
332 while (mc->nobjs)
333 free_page((unsigned long)mc->objects[--mc->nobjs]);
336 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
338 int r;
340 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
341 pte_chain_cache, 4);
342 if (r)
343 goto out;
344 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
345 rmap_desc_cache, 4);
346 if (r)
347 goto out;
348 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
349 if (r)
350 goto out;
351 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
352 mmu_page_header_cache, 4);
353 out:
354 return r;
357 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
359 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
360 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
361 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
362 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
365 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
366 size_t size)
368 void *p;
370 BUG_ON(!mc->nobjs);
371 p = mc->objects[--mc->nobjs];
372 return p;
375 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
377 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
378 sizeof(struct kvm_pte_chain));
381 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
383 kfree(pc);
386 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
388 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
389 sizeof(struct kvm_rmap_desc));
392 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
394 kfree(rd);
398 * Return the pointer to the largepage write count for a given
399 * gfn, handling slots that are not large page aligned.
401 static int *slot_largepage_idx(gfn_t gfn,
402 struct kvm_memory_slot *slot,
403 int level)
405 unsigned long idx;
407 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
408 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
409 return &slot->lpage_info[level - 2][idx].write_count;
412 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
414 struct kvm_memory_slot *slot;
415 int *write_count;
416 int i;
418 gfn = unalias_gfn(kvm, gfn);
420 slot = gfn_to_memslot_unaliased(kvm, gfn);
421 for (i = PT_DIRECTORY_LEVEL;
422 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
423 write_count = slot_largepage_idx(gfn, slot, i);
424 *write_count += 1;
428 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
430 struct kvm_memory_slot *slot;
431 int *write_count;
432 int i;
434 gfn = unalias_gfn(kvm, gfn);
435 slot = gfn_to_memslot_unaliased(kvm, gfn);
436 for (i = PT_DIRECTORY_LEVEL;
437 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
438 write_count = slot_largepage_idx(gfn, slot, i);
439 *write_count -= 1;
440 WARN_ON(*write_count < 0);
444 static int has_wrprotected_page(struct kvm *kvm,
445 gfn_t gfn,
446 int level)
448 struct kvm_memory_slot *slot;
449 int *largepage_idx;
451 gfn = unalias_gfn(kvm, gfn);
452 slot = gfn_to_memslot_unaliased(kvm, gfn);
453 if (slot) {
454 largepage_idx = slot_largepage_idx(gfn, slot, level);
455 return *largepage_idx;
458 return 1;
461 static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
463 unsigned long page_size;
464 int i, ret = 0;
466 page_size = kvm_host_page_size(kvm, gfn);
468 for (i = PT_PAGE_TABLE_LEVEL;
469 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
470 if (page_size >= KVM_HPAGE_SIZE(i))
471 ret = i;
472 else
473 break;
476 return ret;
479 static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
481 struct kvm_memory_slot *slot;
482 int host_level, level, max_level;
484 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
485 if (slot && slot->dirty_bitmap)
486 return PT_PAGE_TABLE_LEVEL;
488 host_level = host_mapping_level(vcpu->kvm, large_gfn);
490 if (host_level == PT_PAGE_TABLE_LEVEL)
491 return host_level;
493 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
494 kvm_x86_ops->get_lpage_level() : host_level;
496 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
497 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
498 break;
500 return level - 1;
504 * Take gfn and return the reverse mapping to it.
505 * Note: gfn must be unaliased before this function get called
508 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
510 struct kvm_memory_slot *slot;
511 unsigned long idx;
513 slot = gfn_to_memslot(kvm, gfn);
514 if (likely(level == PT_PAGE_TABLE_LEVEL))
515 return &slot->rmap[gfn - slot->base_gfn];
517 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
518 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
520 return &slot->lpage_info[level - 2][idx].rmap_pde;
524 * Reverse mapping data structures:
526 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
527 * that points to page_address(page).
529 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
530 * containing more mappings.
532 * Returns the number of rmap entries before the spte was added or zero if
533 * the spte was not added.
536 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
538 struct kvm_mmu_page *sp;
539 struct kvm_rmap_desc *desc;
540 unsigned long *rmapp;
541 int i, count = 0;
543 if (!is_rmap_spte(*spte))
544 return count;
545 gfn = unalias_gfn(vcpu->kvm, gfn);
546 sp = page_header(__pa(spte));
547 sp->gfns[spte - sp->spt] = gfn;
548 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
549 if (!*rmapp) {
550 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
551 *rmapp = (unsigned long)spte;
552 } else if (!(*rmapp & 1)) {
553 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
554 desc = mmu_alloc_rmap_desc(vcpu);
555 desc->sptes[0] = (u64 *)*rmapp;
556 desc->sptes[1] = spte;
557 *rmapp = (unsigned long)desc | 1;
558 } else {
559 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
560 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
561 while (desc->sptes[RMAP_EXT-1] && desc->more) {
562 desc = desc->more;
563 count += RMAP_EXT;
565 if (desc->sptes[RMAP_EXT-1]) {
566 desc->more = mmu_alloc_rmap_desc(vcpu);
567 desc = desc->more;
569 for (i = 0; desc->sptes[i]; ++i)
571 desc->sptes[i] = spte;
573 return count;
576 static void rmap_desc_remove_entry(unsigned long *rmapp,
577 struct kvm_rmap_desc *desc,
578 int i,
579 struct kvm_rmap_desc *prev_desc)
581 int j;
583 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
585 desc->sptes[i] = desc->sptes[j];
586 desc->sptes[j] = NULL;
587 if (j != 0)
588 return;
589 if (!prev_desc && !desc->more)
590 *rmapp = (unsigned long)desc->sptes[0];
591 else
592 if (prev_desc)
593 prev_desc->more = desc->more;
594 else
595 *rmapp = (unsigned long)desc->more | 1;
596 mmu_free_rmap_desc(desc);
599 static void rmap_remove(struct kvm *kvm, u64 *spte)
601 struct kvm_rmap_desc *desc;
602 struct kvm_rmap_desc *prev_desc;
603 struct kvm_mmu_page *sp;
604 pfn_t pfn;
605 unsigned long *rmapp;
606 int i;
608 if (!is_rmap_spte(*spte))
609 return;
610 sp = page_header(__pa(spte));
611 pfn = spte_to_pfn(*spte);
612 if (*spte & shadow_accessed_mask)
613 kvm_set_pfn_accessed(pfn);
614 if (is_writable_pte(*spte))
615 kvm_set_pfn_dirty(pfn);
616 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
617 if (!*rmapp) {
618 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
619 BUG();
620 } else if (!(*rmapp & 1)) {
621 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
622 if ((u64 *)*rmapp != spte) {
623 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
624 spte, *spte);
625 BUG();
627 *rmapp = 0;
628 } else {
629 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
630 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
631 prev_desc = NULL;
632 while (desc) {
633 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
634 if (desc->sptes[i] == spte) {
635 rmap_desc_remove_entry(rmapp,
636 desc, i,
637 prev_desc);
638 return;
640 prev_desc = desc;
641 desc = desc->more;
643 pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
644 BUG();
648 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
650 struct kvm_rmap_desc *desc;
651 u64 *prev_spte;
652 int i;
654 if (!*rmapp)
655 return NULL;
656 else if (!(*rmapp & 1)) {
657 if (!spte)
658 return (u64 *)*rmapp;
659 return NULL;
661 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
662 prev_spte = NULL;
663 while (desc) {
664 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
665 if (prev_spte == spte)
666 return desc->sptes[i];
667 prev_spte = desc->sptes[i];
669 desc = desc->more;
671 return NULL;
674 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
676 unsigned long *rmapp;
677 u64 *spte;
678 int i, write_protected = 0;
680 gfn = unalias_gfn(kvm, gfn);
681 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
683 spte = rmap_next(kvm, rmapp, NULL);
684 while (spte) {
685 BUG_ON(!spte);
686 BUG_ON(!(*spte & PT_PRESENT_MASK));
687 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
688 if (is_writable_pte(*spte)) {
689 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
690 write_protected = 1;
692 spte = rmap_next(kvm, rmapp, spte);
694 if (write_protected) {
695 pfn_t pfn;
697 spte = rmap_next(kvm, rmapp, NULL);
698 pfn = spte_to_pfn(*spte);
699 kvm_set_pfn_dirty(pfn);
702 /* check for huge page mappings */
703 for (i = PT_DIRECTORY_LEVEL;
704 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
705 rmapp = gfn_to_rmap(kvm, gfn, i);
706 spte = rmap_next(kvm, rmapp, NULL);
707 while (spte) {
708 BUG_ON(!spte);
709 BUG_ON(!(*spte & PT_PRESENT_MASK));
710 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
711 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
712 if (is_writable_pte(*spte)) {
713 rmap_remove(kvm, spte);
714 --kvm->stat.lpages;
715 __set_spte(spte, shadow_trap_nonpresent_pte);
716 spte = NULL;
717 write_protected = 1;
719 spte = rmap_next(kvm, rmapp, spte);
723 return write_protected;
726 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
727 unsigned long data)
729 u64 *spte;
730 int need_tlb_flush = 0;
732 while ((spte = rmap_next(kvm, rmapp, NULL))) {
733 BUG_ON(!(*spte & PT_PRESENT_MASK));
734 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
735 rmap_remove(kvm, spte);
736 __set_spte(spte, shadow_trap_nonpresent_pte);
737 need_tlb_flush = 1;
739 return need_tlb_flush;
742 static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
743 unsigned long data)
745 int need_flush = 0;
746 u64 *spte, new_spte;
747 pte_t *ptep = (pte_t *)data;
748 pfn_t new_pfn;
750 WARN_ON(pte_huge(*ptep));
751 new_pfn = pte_pfn(*ptep);
752 spte = rmap_next(kvm, rmapp, NULL);
753 while (spte) {
754 BUG_ON(!is_shadow_present_pte(*spte));
755 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
756 need_flush = 1;
757 if (pte_write(*ptep)) {
758 rmap_remove(kvm, spte);
759 __set_spte(spte, shadow_trap_nonpresent_pte);
760 spte = rmap_next(kvm, rmapp, NULL);
761 } else {
762 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
763 new_spte |= (u64)new_pfn << PAGE_SHIFT;
765 new_spte &= ~PT_WRITABLE_MASK;
766 new_spte &= ~SPTE_HOST_WRITEABLE;
767 if (is_writable_pte(*spte))
768 kvm_set_pfn_dirty(spte_to_pfn(*spte));
769 __set_spte(spte, new_spte);
770 spte = rmap_next(kvm, rmapp, spte);
773 if (need_flush)
774 kvm_flush_remote_tlbs(kvm);
776 return 0;
779 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
780 unsigned long data,
781 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
782 unsigned long data))
784 int i, j;
785 int ret;
786 int retval = 0;
787 struct kvm_memslots *slots;
789 slots = kvm_memslots(kvm);
791 for (i = 0; i < slots->nmemslots; i++) {
792 struct kvm_memory_slot *memslot = &slots->memslots[i];
793 unsigned long start = memslot->userspace_addr;
794 unsigned long end;
796 end = start + (memslot->npages << PAGE_SHIFT);
797 if (hva >= start && hva < end) {
798 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
800 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
802 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
803 int idx = gfn_offset;
804 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
805 ret |= handler(kvm,
806 &memslot->lpage_info[j][idx].rmap_pde,
807 data);
809 trace_kvm_age_page(hva, memslot, ret);
810 retval |= ret;
814 return retval;
817 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
819 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
822 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
824 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
827 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
828 unsigned long data)
830 u64 *spte;
831 int young = 0;
834 * Emulate the accessed bit for EPT, by checking if this page has
835 * an EPT mapping, and clearing it if it does. On the next access,
836 * a new EPT mapping will be established.
837 * This has some overhead, but not as much as the cost of swapping
838 * out actively used pages or breaking up actively used hugepages.
840 if (!shadow_accessed_mask)
841 return kvm_unmap_rmapp(kvm, rmapp, data);
843 spte = rmap_next(kvm, rmapp, NULL);
844 while (spte) {
845 int _young;
846 u64 _spte = *spte;
847 BUG_ON(!(_spte & PT_PRESENT_MASK));
848 _young = _spte & PT_ACCESSED_MASK;
849 if (_young) {
850 young = 1;
851 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
853 spte = rmap_next(kvm, rmapp, spte);
855 return young;
858 #define RMAP_RECYCLE_THRESHOLD 1000
860 static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
862 unsigned long *rmapp;
863 struct kvm_mmu_page *sp;
865 sp = page_header(__pa(spte));
867 gfn = unalias_gfn(vcpu->kvm, gfn);
868 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
870 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
871 kvm_flush_remote_tlbs(vcpu->kvm);
874 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
876 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
879 #ifdef MMU_DEBUG
880 static int is_empty_shadow_page(u64 *spt)
882 u64 *pos;
883 u64 *end;
885 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
886 if (is_shadow_present_pte(*pos)) {
887 printk(KERN_ERR "%s: %p %llx\n", __func__,
888 pos, *pos);
889 return 0;
891 return 1;
893 #endif
895 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
897 ASSERT(is_empty_shadow_page(sp->spt));
898 list_del(&sp->link);
899 __free_page(virt_to_page(sp->spt));
900 __free_page(virt_to_page(sp->gfns));
901 kfree(sp);
902 ++kvm->arch.n_free_mmu_pages;
905 static unsigned kvm_page_table_hashfn(gfn_t gfn)
907 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
910 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
911 u64 *parent_pte)
913 struct kvm_mmu_page *sp;
915 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
916 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
917 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
918 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
919 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
920 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
921 sp->multimapped = 0;
922 sp->parent_pte = parent_pte;
923 --vcpu->kvm->arch.n_free_mmu_pages;
924 return sp;
927 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
928 struct kvm_mmu_page *sp, u64 *parent_pte)
930 struct kvm_pte_chain *pte_chain;
931 struct hlist_node *node;
932 int i;
934 if (!parent_pte)
935 return;
936 if (!sp->multimapped) {
937 u64 *old = sp->parent_pte;
939 if (!old) {
940 sp->parent_pte = parent_pte;
941 return;
943 sp->multimapped = 1;
944 pte_chain = mmu_alloc_pte_chain(vcpu);
945 INIT_HLIST_HEAD(&sp->parent_ptes);
946 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
947 pte_chain->parent_ptes[0] = old;
949 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
950 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
951 continue;
952 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
953 if (!pte_chain->parent_ptes[i]) {
954 pte_chain->parent_ptes[i] = parent_pte;
955 return;
958 pte_chain = mmu_alloc_pte_chain(vcpu);
959 BUG_ON(!pte_chain);
960 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
961 pte_chain->parent_ptes[0] = parent_pte;
964 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
965 u64 *parent_pte)
967 struct kvm_pte_chain *pte_chain;
968 struct hlist_node *node;
969 int i;
971 if (!sp->multimapped) {
972 BUG_ON(sp->parent_pte != parent_pte);
973 sp->parent_pte = NULL;
974 return;
976 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
977 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
978 if (!pte_chain->parent_ptes[i])
979 break;
980 if (pte_chain->parent_ptes[i] != parent_pte)
981 continue;
982 while (i + 1 < NR_PTE_CHAIN_ENTRIES
983 && pte_chain->parent_ptes[i + 1]) {
984 pte_chain->parent_ptes[i]
985 = pte_chain->parent_ptes[i + 1];
986 ++i;
988 pte_chain->parent_ptes[i] = NULL;
989 if (i == 0) {
990 hlist_del(&pte_chain->link);
991 mmu_free_pte_chain(pte_chain);
992 if (hlist_empty(&sp->parent_ptes)) {
993 sp->multimapped = 0;
994 sp->parent_pte = NULL;
997 return;
999 BUG();
1003 static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
1005 struct kvm_pte_chain *pte_chain;
1006 struct hlist_node *node;
1007 struct kvm_mmu_page *parent_sp;
1008 int i;
1010 if (!sp->multimapped && sp->parent_pte) {
1011 parent_sp = page_header(__pa(sp->parent_pte));
1012 fn(parent_sp);
1013 mmu_parent_walk(parent_sp, fn);
1014 return;
1016 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1017 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1018 if (!pte_chain->parent_ptes[i])
1019 break;
1020 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1021 fn(parent_sp);
1022 mmu_parent_walk(parent_sp, fn);
1026 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1028 unsigned int index;
1029 struct kvm_mmu_page *sp = page_header(__pa(spte));
1031 index = spte - sp->spt;
1032 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1033 sp->unsync_children++;
1034 WARN_ON(!sp->unsync_children);
1037 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1039 struct kvm_pte_chain *pte_chain;
1040 struct hlist_node *node;
1041 int i;
1043 if (!sp->parent_pte)
1044 return;
1046 if (!sp->multimapped) {
1047 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1048 return;
1051 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1052 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1053 if (!pte_chain->parent_ptes[i])
1054 break;
1055 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1059 static int unsync_walk_fn(struct kvm_mmu_page *sp)
1061 kvm_mmu_update_parents_unsync(sp);
1062 return 1;
1065 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
1067 mmu_parent_walk(sp, unsync_walk_fn);
1068 kvm_mmu_update_parents_unsync(sp);
1071 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1072 struct kvm_mmu_page *sp)
1074 int i;
1076 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1077 sp->spt[i] = shadow_trap_nonpresent_pte;
1080 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1081 struct kvm_mmu_page *sp)
1083 return 1;
1086 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1090 #define KVM_PAGE_ARRAY_NR 16
1092 struct kvm_mmu_pages {
1093 struct mmu_page_and_offset {
1094 struct kvm_mmu_page *sp;
1095 unsigned int idx;
1096 } page[KVM_PAGE_ARRAY_NR];
1097 unsigned int nr;
1100 #define for_each_unsync_children(bitmap, idx) \
1101 for (idx = find_first_bit(bitmap, 512); \
1102 idx < 512; \
1103 idx = find_next_bit(bitmap, 512, idx+1))
1105 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1106 int idx)
1108 int i;
1110 if (sp->unsync)
1111 for (i=0; i < pvec->nr; i++)
1112 if (pvec->page[i].sp == sp)
1113 return 0;
1115 pvec->page[pvec->nr].sp = sp;
1116 pvec->page[pvec->nr].idx = idx;
1117 pvec->nr++;
1118 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1121 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1122 struct kvm_mmu_pages *pvec)
1124 int i, ret, nr_unsync_leaf = 0;
1126 for_each_unsync_children(sp->unsync_child_bitmap, i) {
1127 u64 ent = sp->spt[i];
1129 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1130 struct kvm_mmu_page *child;
1131 child = page_header(ent & PT64_BASE_ADDR_MASK);
1133 if (child->unsync_children) {
1134 if (mmu_pages_add(pvec, child, i))
1135 return -ENOSPC;
1137 ret = __mmu_unsync_walk(child, pvec);
1138 if (!ret)
1139 __clear_bit(i, sp->unsync_child_bitmap);
1140 else if (ret > 0)
1141 nr_unsync_leaf += ret;
1142 else
1143 return ret;
1146 if (child->unsync) {
1147 nr_unsync_leaf++;
1148 if (mmu_pages_add(pvec, child, i))
1149 return -ENOSPC;
1154 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1155 sp->unsync_children = 0;
1157 return nr_unsync_leaf;
1160 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1161 struct kvm_mmu_pages *pvec)
1163 if (!sp->unsync_children)
1164 return 0;
1166 mmu_pages_add(pvec, sp, 0);
1167 return __mmu_unsync_walk(sp, pvec);
1170 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1172 unsigned index;
1173 struct hlist_head *bucket;
1174 struct kvm_mmu_page *sp;
1175 struct hlist_node *node;
1177 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1178 index = kvm_page_table_hashfn(gfn);
1179 bucket = &kvm->arch.mmu_page_hash[index];
1180 hlist_for_each_entry(sp, node, bucket, hash_link)
1181 if (sp->gfn == gfn && !sp->role.direct
1182 && !sp->role.invalid) {
1183 pgprintk("%s: found role %x\n",
1184 __func__, sp->role.word);
1185 return sp;
1187 return NULL;
1190 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1192 WARN_ON(!sp->unsync);
1193 trace_kvm_mmu_sync_page(sp);
1194 sp->unsync = 0;
1195 --kvm->stat.mmu_unsync;
1198 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1200 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1202 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
1203 kvm_mmu_zap_page(vcpu->kvm, sp);
1204 return 1;
1207 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1208 kvm_flush_remote_tlbs(vcpu->kvm);
1209 kvm_unlink_unsync_page(vcpu->kvm, sp);
1210 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1211 kvm_mmu_zap_page(vcpu->kvm, sp);
1212 return 1;
1215 kvm_mmu_flush_tlb(vcpu);
1216 return 0;
1219 struct mmu_page_path {
1220 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1221 unsigned int idx[PT64_ROOT_LEVEL-1];
1224 #define for_each_sp(pvec, sp, parents, i) \
1225 for (i = mmu_pages_next(&pvec, &parents, -1), \
1226 sp = pvec.page[i].sp; \
1227 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1228 i = mmu_pages_next(&pvec, &parents, i))
1230 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1231 struct mmu_page_path *parents,
1232 int i)
1234 int n;
1236 for (n = i+1; n < pvec->nr; n++) {
1237 struct kvm_mmu_page *sp = pvec->page[n].sp;
1239 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1240 parents->idx[0] = pvec->page[n].idx;
1241 return n;
1244 parents->parent[sp->role.level-2] = sp;
1245 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1248 return n;
1251 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1253 struct kvm_mmu_page *sp;
1254 unsigned int level = 0;
1256 do {
1257 unsigned int idx = parents->idx[level];
1259 sp = parents->parent[level];
1260 if (!sp)
1261 return;
1263 --sp->unsync_children;
1264 WARN_ON((int)sp->unsync_children < 0);
1265 __clear_bit(idx, sp->unsync_child_bitmap);
1266 level++;
1267 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1270 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1271 struct mmu_page_path *parents,
1272 struct kvm_mmu_pages *pvec)
1274 parents->parent[parent->role.level-1] = NULL;
1275 pvec->nr = 0;
1278 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1279 struct kvm_mmu_page *parent)
1281 int i;
1282 struct kvm_mmu_page *sp;
1283 struct mmu_page_path parents;
1284 struct kvm_mmu_pages pages;
1286 kvm_mmu_pages_init(parent, &parents, &pages);
1287 while (mmu_unsync_walk(parent, &pages)) {
1288 int protected = 0;
1290 for_each_sp(pages, sp, parents, i)
1291 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1293 if (protected)
1294 kvm_flush_remote_tlbs(vcpu->kvm);
1296 for_each_sp(pages, sp, parents, i) {
1297 kvm_sync_page(vcpu, sp);
1298 mmu_pages_clear_parents(&parents);
1300 cond_resched_lock(&vcpu->kvm->mmu_lock);
1301 kvm_mmu_pages_init(parent, &parents, &pages);
1305 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1306 gfn_t gfn,
1307 gva_t gaddr,
1308 unsigned level,
1309 int direct,
1310 unsigned access,
1311 u64 *parent_pte)
1313 union kvm_mmu_page_role role;
1314 unsigned index;
1315 unsigned quadrant;
1316 struct hlist_head *bucket;
1317 struct kvm_mmu_page *sp;
1318 struct hlist_node *node, *tmp;
1320 role = vcpu->arch.mmu.base_role;
1321 role.level = level;
1322 role.direct = direct;
1323 if (role.direct)
1324 role.cr4_pae = 0;
1325 role.access = access;
1326 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1327 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1328 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1329 role.quadrant = quadrant;
1331 index = kvm_page_table_hashfn(gfn);
1332 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1333 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1334 if (sp->gfn == gfn) {
1335 if (sp->unsync)
1336 if (kvm_sync_page(vcpu, sp))
1337 continue;
1339 if (sp->role.word != role.word)
1340 continue;
1342 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1343 if (sp->unsync_children) {
1344 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1345 kvm_mmu_mark_parents_unsync(sp);
1347 trace_kvm_mmu_get_page(sp, false);
1348 return sp;
1350 ++vcpu->kvm->stat.mmu_cache_miss;
1351 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1352 if (!sp)
1353 return sp;
1354 sp->gfn = gfn;
1355 sp->role = role;
1356 hlist_add_head(&sp->hash_link, bucket);
1357 if (!direct) {
1358 if (rmap_write_protect(vcpu->kvm, gfn))
1359 kvm_flush_remote_tlbs(vcpu->kvm);
1360 account_shadowed(vcpu->kvm, gfn);
1362 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1363 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1364 else
1365 nonpaging_prefetch_page(vcpu, sp);
1366 trace_kvm_mmu_get_page(sp, true);
1367 return sp;
1370 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1371 struct kvm_vcpu *vcpu, u64 addr)
1373 iterator->addr = addr;
1374 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1375 iterator->level = vcpu->arch.mmu.shadow_root_level;
1376 if (iterator->level == PT32E_ROOT_LEVEL) {
1377 iterator->shadow_addr
1378 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1379 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1380 --iterator->level;
1381 if (!iterator->shadow_addr)
1382 iterator->level = 0;
1386 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1388 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1389 return false;
1391 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1392 if (is_large_pte(*iterator->sptep))
1393 return false;
1395 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1396 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1397 return true;
1400 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1402 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1403 --iterator->level;
1406 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1407 struct kvm_mmu_page *sp)
1409 unsigned i;
1410 u64 *pt;
1411 u64 ent;
1413 pt = sp->spt;
1415 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1416 ent = pt[i];
1418 if (is_shadow_present_pte(ent)) {
1419 if (!is_last_spte(ent, sp->role.level)) {
1420 ent &= PT64_BASE_ADDR_MASK;
1421 mmu_page_remove_parent_pte(page_header(ent),
1422 &pt[i]);
1423 } else {
1424 if (is_large_pte(ent))
1425 --kvm->stat.lpages;
1426 rmap_remove(kvm, &pt[i]);
1429 pt[i] = shadow_trap_nonpresent_pte;
1433 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1435 mmu_page_remove_parent_pte(sp, parent_pte);
1438 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1440 int i;
1441 struct kvm_vcpu *vcpu;
1443 kvm_for_each_vcpu(i, vcpu, kvm)
1444 vcpu->arch.last_pte_updated = NULL;
1447 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1449 u64 *parent_pte;
1451 while (sp->multimapped || sp->parent_pte) {
1452 if (!sp->multimapped)
1453 parent_pte = sp->parent_pte;
1454 else {
1455 struct kvm_pte_chain *chain;
1457 chain = container_of(sp->parent_ptes.first,
1458 struct kvm_pte_chain, link);
1459 parent_pte = chain->parent_ptes[0];
1461 BUG_ON(!parent_pte);
1462 kvm_mmu_put_page(sp, parent_pte);
1463 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1467 static int mmu_zap_unsync_children(struct kvm *kvm,
1468 struct kvm_mmu_page *parent)
1470 int i, zapped = 0;
1471 struct mmu_page_path parents;
1472 struct kvm_mmu_pages pages;
1474 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1475 return 0;
1477 kvm_mmu_pages_init(parent, &parents, &pages);
1478 while (mmu_unsync_walk(parent, &pages)) {
1479 struct kvm_mmu_page *sp;
1481 for_each_sp(pages, sp, parents, i) {
1482 kvm_mmu_zap_page(kvm, sp);
1483 mmu_pages_clear_parents(&parents);
1484 zapped++;
1486 kvm_mmu_pages_init(parent, &parents, &pages);
1489 return zapped;
1492 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1494 int ret;
1496 trace_kvm_mmu_zap_page(sp);
1497 ++kvm->stat.mmu_shadow_zapped;
1498 ret = mmu_zap_unsync_children(kvm, sp);
1499 kvm_mmu_page_unlink_children(kvm, sp);
1500 kvm_mmu_unlink_parents(kvm, sp);
1501 kvm_flush_remote_tlbs(kvm);
1502 if (!sp->role.invalid && !sp->role.direct)
1503 unaccount_shadowed(kvm, sp->gfn);
1504 if (sp->unsync)
1505 kvm_unlink_unsync_page(kvm, sp);
1506 if (!sp->root_count) {
1507 /* Count self */
1508 ret++;
1509 hlist_del(&sp->hash_link);
1510 kvm_mmu_free_page(kvm, sp);
1511 } else {
1512 sp->role.invalid = 1;
1513 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1514 kvm_reload_remote_mmus(kvm);
1516 kvm_mmu_reset_last_pte_updated(kvm);
1517 return ret;
1521 * Changing the number of mmu pages allocated to the vm
1522 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1524 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1526 int used_pages;
1528 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1529 used_pages = max(0, used_pages);
1532 * If we set the number of mmu pages to be smaller be than the
1533 * number of actived pages , we must to free some mmu pages before we
1534 * change the value
1537 if (used_pages > kvm_nr_mmu_pages) {
1538 while (used_pages > kvm_nr_mmu_pages &&
1539 !list_empty(&kvm->arch.active_mmu_pages)) {
1540 struct kvm_mmu_page *page;
1542 page = container_of(kvm->arch.active_mmu_pages.prev,
1543 struct kvm_mmu_page, link);
1544 used_pages -= kvm_mmu_zap_page(kvm, page);
1546 kvm_nr_mmu_pages = used_pages;
1547 kvm->arch.n_free_mmu_pages = 0;
1549 else
1550 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1551 - kvm->arch.n_alloc_mmu_pages;
1553 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1556 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1558 unsigned index;
1559 struct hlist_head *bucket;
1560 struct kvm_mmu_page *sp;
1561 struct hlist_node *node, *n;
1562 int r;
1564 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1565 r = 0;
1566 index = kvm_page_table_hashfn(gfn);
1567 bucket = &kvm->arch.mmu_page_hash[index];
1568 restart:
1569 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1570 if (sp->gfn == gfn && !sp->role.direct) {
1571 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1572 sp->role.word);
1573 r = 1;
1574 if (kvm_mmu_zap_page(kvm, sp))
1575 goto restart;
1577 return r;
1580 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1582 unsigned index;
1583 struct hlist_head *bucket;
1584 struct kvm_mmu_page *sp;
1585 struct hlist_node *node, *nn;
1587 index = kvm_page_table_hashfn(gfn);
1588 bucket = &kvm->arch.mmu_page_hash[index];
1589 restart:
1590 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1591 if (sp->gfn == gfn && !sp->role.direct
1592 && !sp->role.invalid) {
1593 pgprintk("%s: zap %lx %x\n",
1594 __func__, gfn, sp->role.word);
1595 if (kvm_mmu_zap_page(kvm, sp))
1596 goto restart;
1601 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1603 int slot = memslot_id(kvm, gfn);
1604 struct kvm_mmu_page *sp = page_header(__pa(pte));
1606 __set_bit(slot, sp->slot_bitmap);
1609 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1611 int i;
1612 u64 *pt = sp->spt;
1614 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1615 return;
1617 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1618 if (pt[i] == shadow_notrap_nonpresent_pte)
1619 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
1624 * The function is based on mtrr_type_lookup() in
1625 * arch/x86/kernel/cpu/mtrr/generic.c
1627 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1628 u64 start, u64 end)
1630 int i;
1631 u64 base, mask;
1632 u8 prev_match, curr_match;
1633 int num_var_ranges = KVM_NR_VAR_MTRR;
1635 if (!mtrr_state->enabled)
1636 return 0xFF;
1638 /* Make end inclusive end, instead of exclusive */
1639 end--;
1641 /* Look in fixed ranges. Just return the type as per start */
1642 if (mtrr_state->have_fixed && (start < 0x100000)) {
1643 int idx;
1645 if (start < 0x80000) {
1646 idx = 0;
1647 idx += (start >> 16);
1648 return mtrr_state->fixed_ranges[idx];
1649 } else if (start < 0xC0000) {
1650 idx = 1 * 8;
1651 idx += ((start - 0x80000) >> 14);
1652 return mtrr_state->fixed_ranges[idx];
1653 } else if (start < 0x1000000) {
1654 idx = 3 * 8;
1655 idx += ((start - 0xC0000) >> 12);
1656 return mtrr_state->fixed_ranges[idx];
1661 * Look in variable ranges
1662 * Look of multiple ranges matching this address and pick type
1663 * as per MTRR precedence
1665 if (!(mtrr_state->enabled & 2))
1666 return mtrr_state->def_type;
1668 prev_match = 0xFF;
1669 for (i = 0; i < num_var_ranges; ++i) {
1670 unsigned short start_state, end_state;
1672 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1673 continue;
1675 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1676 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1677 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1678 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1680 start_state = ((start & mask) == (base & mask));
1681 end_state = ((end & mask) == (base & mask));
1682 if (start_state != end_state)
1683 return 0xFE;
1685 if ((start & mask) != (base & mask))
1686 continue;
1688 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1689 if (prev_match == 0xFF) {
1690 prev_match = curr_match;
1691 continue;
1694 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1695 curr_match == MTRR_TYPE_UNCACHABLE)
1696 return MTRR_TYPE_UNCACHABLE;
1698 if ((prev_match == MTRR_TYPE_WRBACK &&
1699 curr_match == MTRR_TYPE_WRTHROUGH) ||
1700 (prev_match == MTRR_TYPE_WRTHROUGH &&
1701 curr_match == MTRR_TYPE_WRBACK)) {
1702 prev_match = MTRR_TYPE_WRTHROUGH;
1703 curr_match = MTRR_TYPE_WRTHROUGH;
1706 if (prev_match != curr_match)
1707 return MTRR_TYPE_UNCACHABLE;
1710 if (prev_match != 0xFF)
1711 return prev_match;
1713 return mtrr_state->def_type;
1716 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1718 u8 mtrr;
1720 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1721 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1722 if (mtrr == 0xfe || mtrr == 0xff)
1723 mtrr = MTRR_TYPE_WRBACK;
1724 return mtrr;
1726 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1728 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1730 unsigned index;
1731 struct hlist_head *bucket;
1732 struct kvm_mmu_page *s;
1733 struct hlist_node *node, *n;
1735 index = kvm_page_table_hashfn(sp->gfn);
1736 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1737 /* don't unsync if pagetable is shadowed with multiple roles */
1738 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1739 if (s->gfn != sp->gfn || s->role.direct)
1740 continue;
1741 if (s->role.word != sp->role.word)
1742 return 1;
1744 trace_kvm_mmu_unsync_page(sp);
1745 ++vcpu->kvm->stat.mmu_unsync;
1746 sp->unsync = 1;
1748 kvm_mmu_mark_parents_unsync(sp);
1750 mmu_convert_notrap(sp);
1751 return 0;
1754 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1755 bool can_unsync)
1757 struct kvm_mmu_page *shadow;
1759 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1760 if (shadow) {
1761 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1762 return 1;
1763 if (shadow->unsync)
1764 return 0;
1765 if (can_unsync && oos_shadow)
1766 return kvm_unsync_page(vcpu, shadow);
1767 return 1;
1769 return 0;
1772 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1773 unsigned pte_access, int user_fault,
1774 int write_fault, int dirty, int level,
1775 gfn_t gfn, pfn_t pfn, bool speculative,
1776 bool can_unsync, bool reset_host_protection)
1778 u64 spte;
1779 int ret = 0;
1782 * We don't set the accessed bit, since we sometimes want to see
1783 * whether the guest actually used the pte (in order to detect
1784 * demand paging).
1786 spte = shadow_base_present_pte | shadow_dirty_mask;
1787 if (!speculative)
1788 spte |= shadow_accessed_mask;
1789 if (!dirty)
1790 pte_access &= ~ACC_WRITE_MASK;
1791 if (pte_access & ACC_EXEC_MASK)
1792 spte |= shadow_x_mask;
1793 else
1794 spte |= shadow_nx_mask;
1795 if (pte_access & ACC_USER_MASK)
1796 spte |= shadow_user_mask;
1797 if (level > PT_PAGE_TABLE_LEVEL)
1798 spte |= PT_PAGE_SIZE_MASK;
1799 if (tdp_enabled)
1800 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1801 kvm_is_mmio_pfn(pfn));
1803 if (reset_host_protection)
1804 spte |= SPTE_HOST_WRITEABLE;
1806 spte |= (u64)pfn << PAGE_SHIFT;
1808 if ((pte_access & ACC_WRITE_MASK)
1809 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1811 if (level > PT_PAGE_TABLE_LEVEL &&
1812 has_wrprotected_page(vcpu->kvm, gfn, level)) {
1813 ret = 1;
1814 spte = shadow_trap_nonpresent_pte;
1815 goto set_pte;
1818 spte |= PT_WRITABLE_MASK;
1820 if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
1821 spte &= ~PT_USER_MASK;
1824 * Optimization: for pte sync, if spte was writable the hash
1825 * lookup is unnecessary (and expensive). Write protection
1826 * is responsibility of mmu_get_page / kvm_sync_page.
1827 * Same reasoning can be applied to dirty page accounting.
1829 if (!can_unsync && is_writable_pte(*sptep))
1830 goto set_pte;
1832 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1833 pgprintk("%s: found shadow page for %lx, marking ro\n",
1834 __func__, gfn);
1835 ret = 1;
1836 pte_access &= ~ACC_WRITE_MASK;
1837 if (is_writable_pte(spte))
1838 spte &= ~PT_WRITABLE_MASK;
1842 if (pte_access & ACC_WRITE_MASK)
1843 mark_page_dirty(vcpu->kvm, gfn);
1845 set_pte:
1846 __set_spte(sptep, spte);
1847 return ret;
1850 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1851 unsigned pt_access, unsigned pte_access,
1852 int user_fault, int write_fault, int dirty,
1853 int *ptwrite, int level, gfn_t gfn,
1854 pfn_t pfn, bool speculative,
1855 bool reset_host_protection)
1857 int was_rmapped = 0;
1858 int was_writable = is_writable_pte(*sptep);
1859 int rmap_count;
1861 pgprintk("%s: spte %llx access %x write_fault %d"
1862 " user_fault %d gfn %lx\n",
1863 __func__, *sptep, pt_access,
1864 write_fault, user_fault, gfn);
1866 if (is_rmap_spte(*sptep)) {
1868 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1869 * the parent of the now unreachable PTE.
1871 if (level > PT_PAGE_TABLE_LEVEL &&
1872 !is_large_pte(*sptep)) {
1873 struct kvm_mmu_page *child;
1874 u64 pte = *sptep;
1876 child = page_header(pte & PT64_BASE_ADDR_MASK);
1877 mmu_page_remove_parent_pte(child, sptep);
1878 __set_spte(sptep, shadow_trap_nonpresent_pte);
1879 kvm_flush_remote_tlbs(vcpu->kvm);
1880 } else if (pfn != spte_to_pfn(*sptep)) {
1881 pgprintk("hfn old %lx new %lx\n",
1882 spte_to_pfn(*sptep), pfn);
1883 rmap_remove(vcpu->kvm, sptep);
1884 __set_spte(sptep, shadow_trap_nonpresent_pte);
1885 kvm_flush_remote_tlbs(vcpu->kvm);
1886 } else
1887 was_rmapped = 1;
1890 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
1891 dirty, level, gfn, pfn, speculative, true,
1892 reset_host_protection)) {
1893 if (write_fault)
1894 *ptwrite = 1;
1895 kvm_x86_ops->tlb_flush(vcpu);
1898 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
1899 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1900 is_large_pte(*sptep)? "2MB" : "4kB",
1901 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1902 *sptep, sptep);
1903 if (!was_rmapped && is_large_pte(*sptep))
1904 ++vcpu->kvm->stat.lpages;
1906 page_header_update_slot(vcpu->kvm, sptep, gfn);
1907 if (!was_rmapped) {
1908 rmap_count = rmap_add(vcpu, sptep, gfn);
1909 kvm_release_pfn_clean(pfn);
1910 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1911 rmap_recycle(vcpu, sptep, gfn);
1912 } else {
1913 if (was_writable)
1914 kvm_release_pfn_dirty(pfn);
1915 else
1916 kvm_release_pfn_clean(pfn);
1918 if (speculative) {
1919 vcpu->arch.last_pte_updated = sptep;
1920 vcpu->arch.last_pte_gfn = gfn;
1924 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1928 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1929 int level, gfn_t gfn, pfn_t pfn)
1931 struct kvm_shadow_walk_iterator iterator;
1932 struct kvm_mmu_page *sp;
1933 int pt_write = 0;
1934 gfn_t pseudo_gfn;
1936 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1937 if (iterator.level == level) {
1938 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1939 0, write, 1, &pt_write,
1940 level, gfn, pfn, false, true);
1941 ++vcpu->stat.pf_fixed;
1942 break;
1945 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1946 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1947 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1948 iterator.level - 1,
1949 1, ACC_ALL, iterator.sptep);
1950 if (!sp) {
1951 pgprintk("nonpaging_map: ENOMEM\n");
1952 kvm_release_pfn_clean(pfn);
1953 return -ENOMEM;
1956 __set_spte(iterator.sptep,
1957 __pa(sp->spt)
1958 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1959 | shadow_user_mask | shadow_x_mask);
1962 return pt_write;
1965 static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
1967 char buf[1];
1968 void __user *hva;
1969 int r;
1971 /* Touch the page, so send SIGBUS */
1972 hva = (void __user *)gfn_to_hva(kvm, gfn);
1973 r = copy_from_user(buf, hva, 1);
1976 static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
1978 kvm_release_pfn_clean(pfn);
1979 if (is_hwpoison_pfn(pfn)) {
1980 kvm_send_hwpoison_signal(kvm, gfn);
1981 return 0;
1983 return 1;
1986 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1988 int r;
1989 int level;
1990 pfn_t pfn;
1991 unsigned long mmu_seq;
1993 level = mapping_level(vcpu, gfn);
1996 * This path builds a PAE pagetable - so we can map 2mb pages at
1997 * maximum. Therefore check if the level is larger than that.
1999 if (level > PT_DIRECTORY_LEVEL)
2000 level = PT_DIRECTORY_LEVEL;
2002 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2004 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2005 smp_rmb();
2006 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2008 /* mmio */
2009 if (is_error_pfn(pfn))
2010 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
2012 spin_lock(&vcpu->kvm->mmu_lock);
2013 if (mmu_notifier_retry(vcpu, mmu_seq))
2014 goto out_unlock;
2015 kvm_mmu_free_some_pages(vcpu);
2016 r = __direct_map(vcpu, v, write, level, gfn, pfn);
2017 spin_unlock(&vcpu->kvm->mmu_lock);
2020 return r;
2022 out_unlock:
2023 spin_unlock(&vcpu->kvm->mmu_lock);
2024 kvm_release_pfn_clean(pfn);
2025 return 0;
2029 static void mmu_free_roots(struct kvm_vcpu *vcpu)
2031 int i;
2032 struct kvm_mmu_page *sp;
2034 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2035 return;
2036 spin_lock(&vcpu->kvm->mmu_lock);
2037 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2038 hpa_t root = vcpu->arch.mmu.root_hpa;
2040 sp = page_header(root);
2041 --sp->root_count;
2042 if (!sp->root_count && sp->role.invalid)
2043 kvm_mmu_zap_page(vcpu->kvm, sp);
2044 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2045 spin_unlock(&vcpu->kvm->mmu_lock);
2046 return;
2048 for (i = 0; i < 4; ++i) {
2049 hpa_t root = vcpu->arch.mmu.pae_root[i];
2051 if (root) {
2052 root &= PT64_BASE_ADDR_MASK;
2053 sp = page_header(root);
2054 --sp->root_count;
2055 if (!sp->root_count && sp->role.invalid)
2056 kvm_mmu_zap_page(vcpu->kvm, sp);
2058 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2060 spin_unlock(&vcpu->kvm->mmu_lock);
2061 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2064 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2066 int ret = 0;
2068 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2069 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2070 ret = 1;
2073 return ret;
2076 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2078 int i;
2079 gfn_t root_gfn;
2080 struct kvm_mmu_page *sp;
2081 int direct = 0;
2082 u64 pdptr;
2084 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
2086 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2087 hpa_t root = vcpu->arch.mmu.root_hpa;
2089 ASSERT(!VALID_PAGE(root));
2090 if (mmu_check_root(vcpu, root_gfn))
2091 return 1;
2092 if (tdp_enabled) {
2093 direct = 1;
2094 root_gfn = 0;
2096 spin_lock(&vcpu->kvm->mmu_lock);
2097 kvm_mmu_free_some_pages(vcpu->kvm);
2098 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
2099 PT64_ROOT_LEVEL, direct,
2100 ACC_ALL, NULL);
2101 root = __pa(sp->spt);
2102 ++sp->root_count;
2103 spin_unlock(&vcpu->kvm->mmu_lock);
2104 vcpu->arch.mmu.root_hpa = root;
2105 return 0;
2107 direct = !is_paging(vcpu);
2108 for (i = 0; i < 4; ++i) {
2109 hpa_t root = vcpu->arch.mmu.pae_root[i];
2111 ASSERT(!VALID_PAGE(root));
2112 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
2113 pdptr = kvm_pdptr_read(vcpu, i);
2114 if (!is_present_gpte(pdptr)) {
2115 vcpu->arch.mmu.pae_root[i] = 0;
2116 continue;
2118 root_gfn = pdptr >> PAGE_SHIFT;
2119 } else if (vcpu->arch.mmu.root_level == 0)
2120 root_gfn = 0;
2121 if (mmu_check_root(vcpu, root_gfn))
2122 return 1;
2123 if (tdp_enabled) {
2124 direct = 1;
2125 root_gfn = i << 30;
2127 spin_lock(&vcpu->kvm->mmu_lock);
2128 kvm_mmu_free_some_pages(vcpu->kvm);
2129 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
2130 PT32_ROOT_LEVEL, direct,
2131 ACC_ALL, NULL);
2132 root = __pa(sp->spt);
2133 ++sp->root_count;
2134 spin_unlock(&vcpu->kvm->mmu_lock);
2136 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2138 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2139 return 0;
2142 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2144 int i;
2145 struct kvm_mmu_page *sp;
2147 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2148 return;
2149 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2150 hpa_t root = vcpu->arch.mmu.root_hpa;
2151 sp = page_header(root);
2152 mmu_sync_children(vcpu, sp);
2153 return;
2155 for (i = 0; i < 4; ++i) {
2156 hpa_t root = vcpu->arch.mmu.pae_root[i];
2158 if (root && VALID_PAGE(root)) {
2159 root &= PT64_BASE_ADDR_MASK;
2160 sp = page_header(root);
2161 mmu_sync_children(vcpu, sp);
2166 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2168 spin_lock(&vcpu->kvm->mmu_lock);
2169 mmu_sync_roots(vcpu);
2170 spin_unlock(&vcpu->kvm->mmu_lock);
2173 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2174 u32 access, u32 *error)
2176 if (error)
2177 *error = 0;
2178 return vaddr;
2181 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2182 u32 error_code)
2184 gfn_t gfn;
2185 int r;
2187 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2188 r = mmu_topup_memory_caches(vcpu);
2189 if (r)
2190 return r;
2192 ASSERT(vcpu);
2193 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2195 gfn = gva >> PAGE_SHIFT;
2197 return nonpaging_map(vcpu, gva & PAGE_MASK,
2198 error_code & PFERR_WRITE_MASK, gfn);
2201 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2202 u32 error_code)
2204 pfn_t pfn;
2205 int r;
2206 int level;
2207 gfn_t gfn = gpa >> PAGE_SHIFT;
2208 unsigned long mmu_seq;
2210 ASSERT(vcpu);
2211 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2213 r = mmu_topup_memory_caches(vcpu);
2214 if (r)
2215 return r;
2217 level = mapping_level(vcpu, gfn);
2219 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2221 mmu_seq = vcpu->kvm->mmu_notifier_seq;
2222 smp_rmb();
2223 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2224 if (is_error_pfn(pfn))
2225 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
2226 spin_lock(&vcpu->kvm->mmu_lock);
2227 if (mmu_notifier_retry(vcpu, mmu_seq))
2228 goto out_unlock;
2229 kvm_mmu_free_some_pages(vcpu);
2230 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2231 level, gfn, pfn);
2232 spin_unlock(&vcpu->kvm->mmu_lock);
2234 return r;
2236 out_unlock:
2237 spin_unlock(&vcpu->kvm->mmu_lock);
2238 kvm_release_pfn_clean(pfn);
2239 return 0;
2242 static void nonpaging_free(struct kvm_vcpu *vcpu)
2244 mmu_free_roots(vcpu);
2247 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2249 struct kvm_mmu *context = &vcpu->arch.mmu;
2251 context->new_cr3 = nonpaging_new_cr3;
2252 context->page_fault = nonpaging_page_fault;
2253 context->gva_to_gpa = nonpaging_gva_to_gpa;
2254 context->free = nonpaging_free;
2255 context->prefetch_page = nonpaging_prefetch_page;
2256 context->sync_page = nonpaging_sync_page;
2257 context->invlpg = nonpaging_invlpg;
2258 context->root_level = 0;
2259 context->shadow_root_level = PT32E_ROOT_LEVEL;
2260 context->root_hpa = INVALID_PAGE;
2261 return 0;
2264 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2266 ++vcpu->stat.tlb_flush;
2267 kvm_x86_ops->tlb_flush(vcpu);
2270 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2272 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2273 mmu_free_roots(vcpu);
2276 static void inject_page_fault(struct kvm_vcpu *vcpu,
2277 u64 addr,
2278 u32 err_code)
2280 kvm_inject_page_fault(vcpu, addr, err_code);
2283 static void paging_free(struct kvm_vcpu *vcpu)
2285 nonpaging_free(vcpu);
2288 static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2290 int bit7;
2292 bit7 = (gpte >> 7) & 1;
2293 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2296 #define PTTYPE 64
2297 #include "paging_tmpl.h"
2298 #undef PTTYPE
2300 #define PTTYPE 32
2301 #include "paging_tmpl.h"
2302 #undef PTTYPE
2304 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2306 struct kvm_mmu *context = &vcpu->arch.mmu;
2307 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2308 u64 exb_bit_rsvd = 0;
2310 if (!is_nx(vcpu))
2311 exb_bit_rsvd = rsvd_bits(63, 63);
2312 switch (level) {
2313 case PT32_ROOT_LEVEL:
2314 /* no rsvd bits for 2 level 4K page table entries */
2315 context->rsvd_bits_mask[0][1] = 0;
2316 context->rsvd_bits_mask[0][0] = 0;
2317 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2319 if (!is_pse(vcpu)) {
2320 context->rsvd_bits_mask[1][1] = 0;
2321 break;
2324 if (is_cpuid_PSE36())
2325 /* 36bits PSE 4MB page */
2326 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2327 else
2328 /* 32 bits PSE 4MB page */
2329 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2330 break;
2331 case PT32E_ROOT_LEVEL:
2332 context->rsvd_bits_mask[0][2] =
2333 rsvd_bits(maxphyaddr, 63) |
2334 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2335 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2336 rsvd_bits(maxphyaddr, 62); /* PDE */
2337 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2338 rsvd_bits(maxphyaddr, 62); /* PTE */
2339 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2340 rsvd_bits(maxphyaddr, 62) |
2341 rsvd_bits(13, 20); /* large page */
2342 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2343 break;
2344 case PT64_ROOT_LEVEL:
2345 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2346 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2347 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2348 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2349 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2350 rsvd_bits(maxphyaddr, 51);
2351 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2352 rsvd_bits(maxphyaddr, 51);
2353 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2354 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2355 rsvd_bits(maxphyaddr, 51) |
2356 rsvd_bits(13, 29);
2357 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2358 rsvd_bits(maxphyaddr, 51) |
2359 rsvd_bits(13, 20); /* large page */
2360 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2361 break;
2365 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2367 struct kvm_mmu *context = &vcpu->arch.mmu;
2369 ASSERT(is_pae(vcpu));
2370 context->new_cr3 = paging_new_cr3;
2371 context->page_fault = paging64_page_fault;
2372 context->gva_to_gpa = paging64_gva_to_gpa;
2373 context->prefetch_page = paging64_prefetch_page;
2374 context->sync_page = paging64_sync_page;
2375 context->invlpg = paging64_invlpg;
2376 context->free = paging_free;
2377 context->root_level = level;
2378 context->shadow_root_level = level;
2379 context->root_hpa = INVALID_PAGE;
2380 return 0;
2383 static int paging64_init_context(struct kvm_vcpu *vcpu)
2385 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2386 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2389 static int paging32_init_context(struct kvm_vcpu *vcpu)
2391 struct kvm_mmu *context = &vcpu->arch.mmu;
2393 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2394 context->new_cr3 = paging_new_cr3;
2395 context->page_fault = paging32_page_fault;
2396 context->gva_to_gpa = paging32_gva_to_gpa;
2397 context->free = paging_free;
2398 context->prefetch_page = paging32_prefetch_page;
2399 context->sync_page = paging32_sync_page;
2400 context->invlpg = paging32_invlpg;
2401 context->root_level = PT32_ROOT_LEVEL;
2402 context->shadow_root_level = PT32E_ROOT_LEVEL;
2403 context->root_hpa = INVALID_PAGE;
2404 return 0;
2407 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2409 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2410 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2413 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2415 struct kvm_mmu *context = &vcpu->arch.mmu;
2417 context->new_cr3 = nonpaging_new_cr3;
2418 context->page_fault = tdp_page_fault;
2419 context->free = nonpaging_free;
2420 context->prefetch_page = nonpaging_prefetch_page;
2421 context->sync_page = nonpaging_sync_page;
2422 context->invlpg = nonpaging_invlpg;
2423 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2424 context->root_hpa = INVALID_PAGE;
2426 if (!is_paging(vcpu)) {
2427 context->gva_to_gpa = nonpaging_gva_to_gpa;
2428 context->root_level = 0;
2429 } else if (is_long_mode(vcpu)) {
2430 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2431 context->gva_to_gpa = paging64_gva_to_gpa;
2432 context->root_level = PT64_ROOT_LEVEL;
2433 } else if (is_pae(vcpu)) {
2434 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2435 context->gva_to_gpa = paging64_gva_to_gpa;
2436 context->root_level = PT32E_ROOT_LEVEL;
2437 } else {
2438 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2439 context->gva_to_gpa = paging32_gva_to_gpa;
2440 context->root_level = PT32_ROOT_LEVEL;
2443 return 0;
2446 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2448 int r;
2450 ASSERT(vcpu);
2451 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2453 if (!is_paging(vcpu))
2454 r = nonpaging_init_context(vcpu);
2455 else if (is_long_mode(vcpu))
2456 r = paging64_init_context(vcpu);
2457 else if (is_pae(vcpu))
2458 r = paging32E_init_context(vcpu);
2459 else
2460 r = paging32_init_context(vcpu);
2462 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
2463 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
2465 return r;
2468 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2470 vcpu->arch.update_pte.pfn = bad_pfn;
2472 if (tdp_enabled)
2473 return init_kvm_tdp_mmu(vcpu);
2474 else
2475 return init_kvm_softmmu(vcpu);
2478 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2480 ASSERT(vcpu);
2481 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2482 vcpu->arch.mmu.free(vcpu);
2483 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2487 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2489 destroy_kvm_mmu(vcpu);
2490 return init_kvm_mmu(vcpu);
2492 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2494 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2496 int r;
2498 r = mmu_topup_memory_caches(vcpu);
2499 if (r)
2500 goto out;
2501 r = mmu_alloc_roots(vcpu);
2502 spin_lock(&vcpu->kvm->mmu_lock);
2503 mmu_sync_roots(vcpu);
2504 spin_unlock(&vcpu->kvm->mmu_lock);
2505 if (r)
2506 goto out;
2507 /* set_cr3() should ensure TLB has been flushed */
2508 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2509 out:
2510 return r;
2512 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2514 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2516 mmu_free_roots(vcpu);
2519 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2520 struct kvm_mmu_page *sp,
2521 u64 *spte)
2523 u64 pte;
2524 struct kvm_mmu_page *child;
2526 pte = *spte;
2527 if (is_shadow_present_pte(pte)) {
2528 if (is_last_spte(pte, sp->role.level))
2529 rmap_remove(vcpu->kvm, spte);
2530 else {
2531 child = page_header(pte & PT64_BASE_ADDR_MASK);
2532 mmu_page_remove_parent_pte(child, spte);
2535 __set_spte(spte, shadow_trap_nonpresent_pte);
2536 if (is_large_pte(pte))
2537 --vcpu->kvm->stat.lpages;
2540 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2541 struct kvm_mmu_page *sp,
2542 u64 *spte,
2543 const void *new)
2545 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2546 ++vcpu->kvm->stat.mmu_pde_zapped;
2547 return;
2550 ++vcpu->kvm->stat.mmu_pte_updated;
2551 if (!sp->role.cr4_pae)
2552 paging32_update_pte(vcpu, sp, spte, new);
2553 else
2554 paging64_update_pte(vcpu, sp, spte, new);
2557 static bool need_remote_flush(u64 old, u64 new)
2559 if (!is_shadow_present_pte(old))
2560 return false;
2561 if (!is_shadow_present_pte(new))
2562 return true;
2563 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2564 return true;
2565 old ^= PT64_NX_MASK;
2566 new ^= PT64_NX_MASK;
2567 return (old & ~new & PT64_PERM_MASK) != 0;
2570 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2572 if (need_remote_flush(old, new))
2573 kvm_flush_remote_tlbs(vcpu->kvm);
2574 else
2575 kvm_mmu_flush_tlb(vcpu);
2578 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2580 u64 *spte = vcpu->arch.last_pte_updated;
2582 return !!(spte && (*spte & shadow_accessed_mask));
2585 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2586 u64 gpte)
2588 gfn_t gfn;
2589 pfn_t pfn;
2591 if (!is_present_gpte(gpte))
2592 return;
2593 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2595 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2596 smp_rmb();
2597 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2599 if (is_error_pfn(pfn)) {
2600 kvm_release_pfn_clean(pfn);
2601 return;
2603 vcpu->arch.update_pte.gfn = gfn;
2604 vcpu->arch.update_pte.pfn = pfn;
2607 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2609 u64 *spte = vcpu->arch.last_pte_updated;
2611 if (spte
2612 && vcpu->arch.last_pte_gfn == gfn
2613 && shadow_accessed_mask
2614 && !(*spte & shadow_accessed_mask)
2615 && is_shadow_present_pte(*spte))
2616 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2619 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2620 const u8 *new, int bytes,
2621 bool guest_initiated)
2623 gfn_t gfn = gpa >> PAGE_SHIFT;
2624 struct kvm_mmu_page *sp;
2625 struct hlist_node *node, *n;
2626 struct hlist_head *bucket;
2627 unsigned index;
2628 u64 entry, gentry;
2629 u64 *spte;
2630 unsigned offset = offset_in_page(gpa);
2631 unsigned pte_size;
2632 unsigned page_offset;
2633 unsigned misaligned;
2634 unsigned quadrant;
2635 int level;
2636 int flooded = 0;
2637 int npte;
2638 int r;
2639 int invlpg_counter;
2641 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2643 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
2646 * Assume that the pte write on a page table of the same type
2647 * as the current vcpu paging mode. This is nearly always true
2648 * (might be false while changing modes). Note it is verified later
2649 * by update_pte().
2651 if ((is_pae(vcpu) && bytes == 4) || !new) {
2652 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2653 if (is_pae(vcpu)) {
2654 gpa &= ~(gpa_t)7;
2655 bytes = 8;
2657 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
2658 if (r)
2659 gentry = 0;
2660 new = (const u8 *)&gentry;
2663 switch (bytes) {
2664 case 4:
2665 gentry = *(const u32 *)new;
2666 break;
2667 case 8:
2668 gentry = *(const u64 *)new;
2669 break;
2670 default:
2671 gentry = 0;
2672 break;
2675 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
2676 spin_lock(&vcpu->kvm->mmu_lock);
2677 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
2678 gentry = 0;
2679 kvm_mmu_access_page(vcpu, gfn);
2680 kvm_mmu_free_some_pages(vcpu);
2681 ++vcpu->kvm->stat.mmu_pte_write;
2682 kvm_mmu_audit(vcpu, "pre pte write");
2683 if (guest_initiated) {
2684 if (gfn == vcpu->arch.last_pt_write_gfn
2685 && !last_updated_pte_accessed(vcpu)) {
2686 ++vcpu->arch.last_pt_write_count;
2687 if (vcpu->arch.last_pt_write_count >= 3)
2688 flooded = 1;
2689 } else {
2690 vcpu->arch.last_pt_write_gfn = gfn;
2691 vcpu->arch.last_pt_write_count = 1;
2692 vcpu->arch.last_pte_updated = NULL;
2695 index = kvm_page_table_hashfn(gfn);
2696 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2698 restart:
2699 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2700 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2701 continue;
2702 pte_size = sp->role.cr4_pae ? 8 : 4;
2703 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2704 misaligned |= bytes < 4;
2705 if (misaligned || flooded) {
2707 * Misaligned accesses are too much trouble to fix
2708 * up; also, they usually indicate a page is not used
2709 * as a page table.
2711 * If we're seeing too many writes to a page,
2712 * it may no longer be a page table, or we may be
2713 * forking, in which case it is better to unmap the
2714 * page.
2716 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2717 gpa, bytes, sp->role.word);
2718 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2719 goto restart;
2720 ++vcpu->kvm->stat.mmu_flooded;
2721 continue;
2723 page_offset = offset;
2724 level = sp->role.level;
2725 npte = 1;
2726 if (!sp->role.cr4_pae) {
2727 page_offset <<= 1; /* 32->64 */
2729 * A 32-bit pde maps 4MB while the shadow pdes map
2730 * only 2MB. So we need to double the offset again
2731 * and zap two pdes instead of one.
2733 if (level == PT32_ROOT_LEVEL) {
2734 page_offset &= ~7; /* kill rounding error */
2735 page_offset <<= 1;
2736 npte = 2;
2738 quadrant = page_offset >> PAGE_SHIFT;
2739 page_offset &= ~PAGE_MASK;
2740 if (quadrant != sp->role.quadrant)
2741 continue;
2743 spte = &sp->spt[page_offset / sizeof(*spte)];
2744 while (npte--) {
2745 entry = *spte;
2746 mmu_pte_write_zap_pte(vcpu, sp, spte);
2747 if (gentry)
2748 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
2749 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2750 ++spte;
2753 kvm_mmu_audit(vcpu, "post pte write");
2754 spin_unlock(&vcpu->kvm->mmu_lock);
2755 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2756 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2757 vcpu->arch.update_pte.pfn = bad_pfn;
2761 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2763 gpa_t gpa;
2764 int r;
2766 if (tdp_enabled)
2767 return 0;
2769 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
2771 spin_lock(&vcpu->kvm->mmu_lock);
2772 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2773 spin_unlock(&vcpu->kvm->mmu_lock);
2774 return r;
2776 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2778 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2780 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2781 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2782 struct kvm_mmu_page *sp;
2784 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2785 struct kvm_mmu_page, link);
2786 kvm_mmu_zap_page(vcpu->kvm, sp);
2787 ++vcpu->kvm->stat.mmu_recycled;
2791 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2793 int r;
2794 enum emulation_result er;
2796 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2797 if (r < 0)
2798 goto out;
2800 if (!r) {
2801 r = 1;
2802 goto out;
2805 r = mmu_topup_memory_caches(vcpu);
2806 if (r)
2807 goto out;
2809 er = emulate_instruction(vcpu, cr2, error_code, 0);
2811 switch (er) {
2812 case EMULATE_DONE:
2813 return 1;
2814 case EMULATE_DO_MMIO:
2815 ++vcpu->stat.mmio_exits;
2816 /* fall through */
2817 case EMULATE_FAIL:
2818 return 0;
2819 default:
2820 BUG();
2822 out:
2823 return r;
2825 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2827 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2829 vcpu->arch.mmu.invlpg(vcpu, gva);
2830 kvm_mmu_flush_tlb(vcpu);
2831 ++vcpu->stat.invlpg;
2833 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2835 void kvm_enable_tdp(void)
2837 tdp_enabled = true;
2839 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2841 void kvm_disable_tdp(void)
2843 tdp_enabled = false;
2845 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2847 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2849 free_page((unsigned long)vcpu->arch.mmu.pae_root);
2852 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2854 struct page *page;
2855 int i;
2857 ASSERT(vcpu);
2860 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2861 * Therefore we need to allocate shadow page tables in the first
2862 * 4GB of memory, which happens to fit the DMA32 zone.
2864 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2865 if (!page)
2866 return -ENOMEM;
2868 vcpu->arch.mmu.pae_root = page_address(page);
2869 for (i = 0; i < 4; ++i)
2870 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2872 return 0;
2875 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2877 ASSERT(vcpu);
2878 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2880 return alloc_mmu_pages(vcpu);
2883 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2885 ASSERT(vcpu);
2886 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2888 return init_kvm_mmu(vcpu);
2891 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2893 ASSERT(vcpu);
2895 destroy_kvm_mmu(vcpu);
2896 free_mmu_pages(vcpu);
2897 mmu_free_memory_caches(vcpu);
2900 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2902 struct kvm_mmu_page *sp;
2904 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2905 int i;
2906 u64 *pt;
2908 if (!test_bit(slot, sp->slot_bitmap))
2909 continue;
2911 pt = sp->spt;
2912 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2913 /* avoid RMW */
2914 if (pt[i] & PT_WRITABLE_MASK)
2915 pt[i] &= ~PT_WRITABLE_MASK;
2917 kvm_flush_remote_tlbs(kvm);
2920 void kvm_mmu_zap_all(struct kvm *kvm)
2922 struct kvm_mmu_page *sp, *node;
2924 spin_lock(&kvm->mmu_lock);
2925 restart:
2926 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2927 if (kvm_mmu_zap_page(kvm, sp))
2928 goto restart;
2930 spin_unlock(&kvm->mmu_lock);
2932 kvm_flush_remote_tlbs(kvm);
2935 static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm)
2937 struct kvm_mmu_page *page;
2939 page = container_of(kvm->arch.active_mmu_pages.prev,
2940 struct kvm_mmu_page, link);
2941 return kvm_mmu_zap_page(kvm, page);
2944 static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
2946 struct kvm *kvm;
2947 struct kvm *kvm_freed = NULL;
2948 int cache_count = 0;
2950 spin_lock(&kvm_lock);
2952 list_for_each_entry(kvm, &vm_list, vm_list) {
2953 int npages, idx, freed_pages;
2955 idx = srcu_read_lock(&kvm->srcu);
2956 spin_lock(&kvm->mmu_lock);
2957 npages = kvm->arch.n_alloc_mmu_pages -
2958 kvm->arch.n_free_mmu_pages;
2959 cache_count += npages;
2960 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2961 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm);
2962 cache_count -= freed_pages;
2963 kvm_freed = kvm;
2965 nr_to_scan--;
2967 spin_unlock(&kvm->mmu_lock);
2968 srcu_read_unlock(&kvm->srcu, idx);
2970 if (kvm_freed)
2971 list_move_tail(&kvm_freed->vm_list, &vm_list);
2973 spin_unlock(&kvm_lock);
2975 return cache_count;
2978 static struct shrinker mmu_shrinker = {
2979 .shrink = mmu_shrink,
2980 .seeks = DEFAULT_SEEKS * 10,
2983 static void mmu_destroy_caches(void)
2985 if (pte_chain_cache)
2986 kmem_cache_destroy(pte_chain_cache);
2987 if (rmap_desc_cache)
2988 kmem_cache_destroy(rmap_desc_cache);
2989 if (mmu_page_header_cache)
2990 kmem_cache_destroy(mmu_page_header_cache);
2993 void kvm_mmu_module_exit(void)
2995 mmu_destroy_caches();
2996 unregister_shrinker(&mmu_shrinker);
2999 int kvm_mmu_module_init(void)
3001 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3002 sizeof(struct kvm_pte_chain),
3003 0, 0, NULL);
3004 if (!pte_chain_cache)
3005 goto nomem;
3006 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3007 sizeof(struct kvm_rmap_desc),
3008 0, 0, NULL);
3009 if (!rmap_desc_cache)
3010 goto nomem;
3012 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3013 sizeof(struct kvm_mmu_page),
3014 0, 0, NULL);
3015 if (!mmu_page_header_cache)
3016 goto nomem;
3018 register_shrinker(&mmu_shrinker);
3020 return 0;
3022 nomem:
3023 mmu_destroy_caches();
3024 return -ENOMEM;
3028 * Caculate mmu pages needed for kvm.
3030 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3032 int i;
3033 unsigned int nr_mmu_pages;
3034 unsigned int nr_pages = 0;
3035 struct kvm_memslots *slots;
3037 slots = kvm_memslots(kvm);
3039 for (i = 0; i < slots->nmemslots; i++)
3040 nr_pages += slots->memslots[i].npages;
3042 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3043 nr_mmu_pages = max(nr_mmu_pages,
3044 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3046 return nr_mmu_pages;
3049 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3050 unsigned len)
3052 if (len > buffer->len)
3053 return NULL;
3054 return buffer->ptr;
3057 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3058 unsigned len)
3060 void *ret;
3062 ret = pv_mmu_peek_buffer(buffer, len);
3063 if (!ret)
3064 return ret;
3065 buffer->ptr += len;
3066 buffer->len -= len;
3067 buffer->processed += len;
3068 return ret;
3071 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3072 gpa_t addr, gpa_t value)
3074 int bytes = 8;
3075 int r;
3077 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3078 bytes = 4;
3080 r = mmu_topup_memory_caches(vcpu);
3081 if (r)
3082 return r;
3084 if (!emulator_write_phys(vcpu, addr, &value, bytes))
3085 return -EFAULT;
3087 return 1;
3090 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3092 kvm_set_cr3(vcpu, vcpu->arch.cr3);
3093 return 1;
3096 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3098 spin_lock(&vcpu->kvm->mmu_lock);
3099 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3100 spin_unlock(&vcpu->kvm->mmu_lock);
3101 return 1;
3104 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3105 struct kvm_pv_mmu_op_buffer *buffer)
3107 struct kvm_mmu_op_header *header;
3109 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3110 if (!header)
3111 return 0;
3112 switch (header->op) {
3113 case KVM_MMU_OP_WRITE_PTE: {
3114 struct kvm_mmu_op_write_pte *wpte;
3116 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3117 if (!wpte)
3118 return 0;
3119 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3120 wpte->pte_val);
3122 case KVM_MMU_OP_FLUSH_TLB: {
3123 struct kvm_mmu_op_flush_tlb *ftlb;
3125 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3126 if (!ftlb)
3127 return 0;
3128 return kvm_pv_mmu_flush_tlb(vcpu);
3130 case KVM_MMU_OP_RELEASE_PT: {
3131 struct kvm_mmu_op_release_pt *rpt;
3133 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3134 if (!rpt)
3135 return 0;
3136 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3138 default: return 0;
3142 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3143 gpa_t addr, unsigned long *ret)
3145 int r;
3146 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3148 buffer->ptr = buffer->buf;
3149 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3150 buffer->processed = 0;
3152 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3153 if (r)
3154 goto out;
3156 while (buffer->len) {
3157 r = kvm_pv_mmu_op_one(vcpu, buffer);
3158 if (r < 0)
3159 goto out;
3160 if (r == 0)
3161 break;
3164 r = 1;
3165 out:
3166 *ret = buffer->processed;
3167 return r;
3170 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3172 struct kvm_shadow_walk_iterator iterator;
3173 int nr_sptes = 0;
3175 spin_lock(&vcpu->kvm->mmu_lock);
3176 for_each_shadow_entry(vcpu, addr, iterator) {
3177 sptes[iterator.level-1] = *iterator.sptep;
3178 nr_sptes++;
3179 if (!is_shadow_present_pte(*iterator.sptep))
3180 break;
3182 spin_unlock(&vcpu->kvm->mmu_lock);
3184 return nr_sptes;
3186 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3188 #ifdef AUDIT
3190 static const char *audit_msg;
3192 static gva_t canonicalize(gva_t gva)
3194 #ifdef CONFIG_X86_64
3195 gva = (long long)(gva << 16) >> 16;
3196 #endif
3197 return gva;
3201 typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
3203 static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3204 inspect_spte_fn fn)
3206 int i;
3208 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3209 u64 ent = sp->spt[i];
3211 if (is_shadow_present_pte(ent)) {
3212 if (!is_last_spte(ent, sp->role.level)) {
3213 struct kvm_mmu_page *child;
3214 child = page_header(ent & PT64_BASE_ADDR_MASK);
3215 __mmu_spte_walk(kvm, child, fn);
3216 } else
3217 fn(kvm, &sp->spt[i]);
3222 static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3224 int i;
3225 struct kvm_mmu_page *sp;
3227 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3228 return;
3229 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3230 hpa_t root = vcpu->arch.mmu.root_hpa;
3231 sp = page_header(root);
3232 __mmu_spte_walk(vcpu->kvm, sp, fn);
3233 return;
3235 for (i = 0; i < 4; ++i) {
3236 hpa_t root = vcpu->arch.mmu.pae_root[i];
3238 if (root && VALID_PAGE(root)) {
3239 root &= PT64_BASE_ADDR_MASK;
3240 sp = page_header(root);
3241 __mmu_spte_walk(vcpu->kvm, sp, fn);
3244 return;
3247 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3248 gva_t va, int level)
3250 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3251 int i;
3252 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3254 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3255 u64 ent = pt[i];
3257 if (ent == shadow_trap_nonpresent_pte)
3258 continue;
3260 va = canonicalize(va);
3261 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3262 audit_mappings_page(vcpu, ent, va, level - 1);
3263 else {
3264 gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
3265 gfn_t gfn = gpa >> PAGE_SHIFT;
3266 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3267 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
3269 if (is_error_pfn(pfn)) {
3270 kvm_release_pfn_clean(pfn);
3271 continue;
3274 if (is_shadow_present_pte(ent)
3275 && (ent & PT64_BASE_ADDR_MASK) != hpa)
3276 printk(KERN_ERR "xx audit error: (%s) levels %d"
3277 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3278 audit_msg, vcpu->arch.mmu.root_level,
3279 va, gpa, hpa, ent,
3280 is_shadow_present_pte(ent));
3281 else if (ent == shadow_notrap_nonpresent_pte
3282 && !is_error_hpa(hpa))
3283 printk(KERN_ERR "audit: (%s) notrap shadow,"
3284 " valid guest gva %lx\n", audit_msg, va);
3285 kvm_release_pfn_clean(pfn);
3291 static void audit_mappings(struct kvm_vcpu *vcpu)
3293 unsigned i;
3295 if (vcpu->arch.mmu.root_level == 4)
3296 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3297 else
3298 for (i = 0; i < 4; ++i)
3299 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3300 audit_mappings_page(vcpu,
3301 vcpu->arch.mmu.pae_root[i],
3302 i << 30,
3306 static int count_rmaps(struct kvm_vcpu *vcpu)
3308 struct kvm *kvm = vcpu->kvm;
3309 struct kvm_memslots *slots;
3310 int nmaps = 0;
3311 int i, j, k, idx;
3313 idx = srcu_read_lock(&kvm->srcu);
3314 slots = kvm_memslots(kvm);
3315 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3316 struct kvm_memory_slot *m = &slots->memslots[i];
3317 struct kvm_rmap_desc *d;
3319 for (j = 0; j < m->npages; ++j) {
3320 unsigned long *rmapp = &m->rmap[j];
3322 if (!*rmapp)
3323 continue;
3324 if (!(*rmapp & 1)) {
3325 ++nmaps;
3326 continue;
3328 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3329 while (d) {
3330 for (k = 0; k < RMAP_EXT; ++k)
3331 if (d->sptes[k])
3332 ++nmaps;
3333 else
3334 break;
3335 d = d->more;
3339 srcu_read_unlock(&kvm->srcu, idx);
3340 return nmaps;
3343 void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
3345 unsigned long *rmapp;
3346 struct kvm_mmu_page *rev_sp;
3347 gfn_t gfn;
3349 if (*sptep & PT_WRITABLE_MASK) {
3350 rev_sp = page_header(__pa(sptep));
3351 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3353 if (!gfn_to_memslot(kvm, gfn)) {
3354 if (!printk_ratelimit())
3355 return;
3356 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3357 audit_msg, gfn);
3358 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3359 audit_msg, (long int)(sptep - rev_sp->spt),
3360 rev_sp->gfn);
3361 dump_stack();
3362 return;
3365 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3366 rev_sp->role.level);
3367 if (!*rmapp) {
3368 if (!printk_ratelimit())
3369 return;
3370 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3371 audit_msg, *sptep);
3372 dump_stack();
3378 void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3380 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3383 static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3385 struct kvm_mmu_page *sp;
3386 int i;
3388 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3389 u64 *pt = sp->spt;
3391 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3392 continue;
3394 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3395 u64 ent = pt[i];
3397 if (!(ent & PT_PRESENT_MASK))
3398 continue;
3399 if (!(ent & PT_WRITABLE_MASK))
3400 continue;
3401 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
3404 return;
3407 static void audit_rmap(struct kvm_vcpu *vcpu)
3409 check_writable_mappings_rmap(vcpu);
3410 count_rmaps(vcpu);
3413 static void audit_write_protection(struct kvm_vcpu *vcpu)
3415 struct kvm_mmu_page *sp;
3416 struct kvm_memory_slot *slot;
3417 unsigned long *rmapp;
3418 u64 *spte;
3419 gfn_t gfn;
3421 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3422 if (sp->role.direct)
3423 continue;
3424 if (sp->unsync)
3425 continue;
3427 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3428 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3429 rmapp = &slot->rmap[gfn - slot->base_gfn];
3431 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3432 while (spte) {
3433 if (*spte & PT_WRITABLE_MASK)
3434 printk(KERN_ERR "%s: (%s) shadow page has "
3435 "writable mappings: gfn %lx role %x\n",
3436 __func__, audit_msg, sp->gfn,
3437 sp->role.word);
3438 spte = rmap_next(vcpu->kvm, rmapp, spte);
3443 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3445 int olddbg = dbg;
3447 dbg = 0;
3448 audit_msg = msg;
3449 audit_rmap(vcpu);
3450 audit_write_protection(vcpu);
3451 if (strcmp("pre pte write", audit_msg) != 0)
3452 audit_mappings(vcpu);
3453 audit_writable_sptes_have_rmaps(vcpu);
3454 dbg = olddbg;
3457 #endif