2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
9 * Copyright (C) 2006 Qumranet, Inc.
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
22 #include "kvm_cache_regs.h"
24 #include <linux/kvm_host.h>
25 #include <linux/types.h>
26 #include <linux/string.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>
37 #include <asm/cmpxchg.h>
42 * When setting this variable to true it enables Two-Dimensional-Paging
43 * where the hardware walks 2 page tables:
44 * 1. the guest-virtual to guest-physical
45 * 2. while doing 1. it walks guest-physical to host-physical
46 * If the hardware supports that we don't need to do shadow paging.
48 bool tdp_enabled
= false;
55 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
);
57 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
) {}
62 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
63 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
67 #define pgprintk(x...) do { } while (0)
68 #define rmap_printk(x...) do { } while (0)
72 #if defined(MMU_DEBUG) || defined(AUDIT)
74 module_param(dbg
, bool, 0644);
77 static int oos_shadow
= 1;
78 module_param(oos_shadow
, bool, 0644);
81 #define ASSERT(x) do { } while (0)
85 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
86 __FILE__, __LINE__, #x); \
90 #define PT_FIRST_AVAIL_BITS_SHIFT 9
91 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
93 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
95 #define PT64_LEVEL_BITS 9
97 #define PT64_LEVEL_SHIFT(level) \
98 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
100 #define PT64_LEVEL_MASK(level) \
101 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
103 #define PT64_INDEX(address, level)\
104 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
107 #define PT32_LEVEL_BITS 10
109 #define PT32_LEVEL_SHIFT(level) \
110 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
112 #define PT32_LEVEL_MASK(level) \
113 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
114 #define PT32_LVL_OFFSET_MASK(level) \
115 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
116 * PT32_LEVEL_BITS))) - 1))
118 #define PT32_INDEX(address, level)\
119 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
122 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
123 #define PT64_DIR_BASE_ADDR_MASK \
124 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
125 #define PT64_LVL_ADDR_MASK(level) \
126 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
127 * PT64_LEVEL_BITS))) - 1))
128 #define PT64_LVL_OFFSET_MASK(level) \
129 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT64_LEVEL_BITS))) - 1))
132 #define PT32_BASE_ADDR_MASK PAGE_MASK
133 #define PT32_DIR_BASE_ADDR_MASK \
134 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
135 #define PT32_LVL_ADDR_MASK(level) \
136 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
137 * PT32_LEVEL_BITS))) - 1))
139 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
144 #define ACC_EXEC_MASK 1
145 #define ACC_WRITE_MASK PT_WRITABLE_MASK
146 #define ACC_USER_MASK PT_USER_MASK
147 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
149 #include <trace/events/kvm.h>
151 #define CREATE_TRACE_POINTS
152 #include "mmutrace.h"
154 #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
156 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
158 struct kvm_rmap_desc
{
159 u64
*sptes
[RMAP_EXT
];
160 struct kvm_rmap_desc
*more
;
163 struct kvm_shadow_walk_iterator
{
171 #define for_each_shadow_entry(_vcpu, _addr, _walker) \
172 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
173 shadow_walk_okay(&(_walker)); \
174 shadow_walk_next(&(_walker)))
176 typedef int (*mmu_parent_walk_fn
) (struct kvm_mmu_page
*sp
);
178 static struct kmem_cache
*pte_chain_cache
;
179 static struct kmem_cache
*rmap_desc_cache
;
180 static struct kmem_cache
*mmu_page_header_cache
;
182 static u64 __read_mostly shadow_trap_nonpresent_pte
;
183 static u64 __read_mostly shadow_notrap_nonpresent_pte
;
184 static u64 __read_mostly shadow_base_present_pte
;
185 static u64 __read_mostly shadow_nx_mask
;
186 static u64 __read_mostly shadow_x_mask
; /* mutual exclusive with nx_mask */
187 static u64 __read_mostly shadow_user_mask
;
188 static u64 __read_mostly shadow_accessed_mask
;
189 static u64 __read_mostly shadow_dirty_mask
;
191 static inline u64
rsvd_bits(int s
, int e
)
193 return ((1ULL << (e
- s
+ 1)) - 1) << s
;
196 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte
, u64 notrap_pte
)
198 shadow_trap_nonpresent_pte
= trap_pte
;
199 shadow_notrap_nonpresent_pte
= notrap_pte
;
201 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes
);
203 void kvm_mmu_set_base_ptes(u64 base_pte
)
205 shadow_base_present_pte
= base_pte
;
207 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_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 int 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
)
285 set_64bit((unsigned long *)sptep
, spte
);
287 set_64bit((unsigned long long *)sptep
, spte
);
291 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
292 struct kmem_cache
*base_cache
, int min
)
296 if (cache
->nobjs
>= min
)
298 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
299 obj
= kmem_cache_zalloc(base_cache
, GFP_KERNEL
);
302 cache
->objects
[cache
->nobjs
++] = obj
;
307 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
310 kfree(mc
->objects
[--mc
->nobjs
]);
313 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache
*cache
,
318 if (cache
->nobjs
>= min
)
320 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
321 page
= alloc_page(GFP_KERNEL
);
324 cache
->objects
[cache
->nobjs
++] = page_address(page
);
329 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache
*mc
)
332 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
335 static int mmu_topup_memory_caches(struct kvm_vcpu
*vcpu
)
339 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
,
343 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
,
347 r
= mmu_topup_memory_cache_page(&vcpu
->arch
.mmu_page_cache
, 8);
350 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_page_header_cache
,
351 mmu_page_header_cache
, 4);
356 static void mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
358 mmu_free_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
);
359 mmu_free_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
);
360 mmu_free_memory_cache_page(&vcpu
->arch
.mmu_page_cache
);
361 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_header_cache
);
364 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
,
370 p
= mc
->objects
[--mc
->nobjs
];
374 static struct kvm_pte_chain
*mmu_alloc_pte_chain(struct kvm_vcpu
*vcpu
)
376 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_pte_chain_cache
,
377 sizeof(struct kvm_pte_chain
));
380 static void mmu_free_pte_chain(struct kvm_pte_chain
*pc
)
385 static struct kvm_rmap_desc
*mmu_alloc_rmap_desc(struct kvm_vcpu
*vcpu
)
387 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_rmap_desc_cache
,
388 sizeof(struct kvm_rmap_desc
));
391 static void mmu_free_rmap_desc(struct kvm_rmap_desc
*rd
)
397 * Return the pointer to the largepage write count for a given
398 * gfn, handling slots that are not large page aligned.
400 static int *slot_largepage_idx(gfn_t gfn
,
401 struct kvm_memory_slot
*slot
,
406 idx
= (gfn
/ KVM_PAGES_PER_HPAGE(level
)) -
407 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE(level
));
408 return &slot
->lpage_info
[level
- 2][idx
].write_count
;
411 static void account_shadowed(struct kvm
*kvm
, gfn_t gfn
)
413 struct kvm_memory_slot
*slot
;
417 gfn
= unalias_gfn(kvm
, gfn
);
419 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
420 for (i
= PT_DIRECTORY_LEVEL
;
421 i
< PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
; ++i
) {
422 write_count
= slot_largepage_idx(gfn
, slot
, i
);
427 static void unaccount_shadowed(struct kvm
*kvm
, gfn_t gfn
)
429 struct kvm_memory_slot
*slot
;
433 gfn
= unalias_gfn(kvm
, gfn
);
434 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
435 for (i
= PT_DIRECTORY_LEVEL
;
436 i
< PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
; ++i
) {
437 write_count
= slot_largepage_idx(gfn
, slot
, i
);
439 WARN_ON(*write_count
< 0);
443 static int has_wrprotected_page(struct kvm
*kvm
,
447 struct kvm_memory_slot
*slot
;
450 gfn
= unalias_gfn(kvm
, gfn
);
451 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
453 largepage_idx
= slot_largepage_idx(gfn
, slot
, level
);
454 return *largepage_idx
;
460 static int host_mapping_level(struct kvm
*kvm
, gfn_t gfn
)
462 unsigned long page_size
;
465 page_size
= kvm_host_page_size(kvm
, gfn
);
467 for (i
= PT_PAGE_TABLE_LEVEL
;
468 i
< (PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
); ++i
) {
469 if (page_size
>= KVM_HPAGE_SIZE(i
))
478 static int mapping_level(struct kvm_vcpu
*vcpu
, gfn_t large_gfn
)
480 struct kvm_memory_slot
*slot
;
481 int host_level
, level
, max_level
;
483 slot
= gfn_to_memslot(vcpu
->kvm
, large_gfn
);
484 if (slot
&& slot
->dirty_bitmap
)
485 return PT_PAGE_TABLE_LEVEL
;
487 host_level
= host_mapping_level(vcpu
->kvm
, large_gfn
);
489 if (host_level
== PT_PAGE_TABLE_LEVEL
)
492 max_level
= kvm_x86_ops
->get_lpage_level() < host_level
?
493 kvm_x86_ops
->get_lpage_level() : host_level
;
495 for (level
= PT_DIRECTORY_LEVEL
; level
<= max_level
; ++level
)
496 if (has_wrprotected_page(vcpu
->kvm
, large_gfn
, level
))
503 * Take gfn and return the reverse mapping to it.
504 * Note: gfn must be unaliased before this function get called
507 static unsigned long *gfn_to_rmap(struct kvm
*kvm
, gfn_t gfn
, int level
)
509 struct kvm_memory_slot
*slot
;
512 slot
= gfn_to_memslot(kvm
, gfn
);
513 if (likely(level
== PT_PAGE_TABLE_LEVEL
))
514 return &slot
->rmap
[gfn
- slot
->base_gfn
];
516 idx
= (gfn
/ KVM_PAGES_PER_HPAGE(level
)) -
517 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE(level
));
519 return &slot
->lpage_info
[level
- 2][idx
].rmap_pde
;
523 * Reverse mapping data structures:
525 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
526 * that points to page_address(page).
528 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
529 * containing more mappings.
531 * Returns the number of rmap entries before the spte was added or zero if
532 * the spte was not added.
535 static int rmap_add(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
)
537 struct kvm_mmu_page
*sp
;
538 struct kvm_rmap_desc
*desc
;
539 unsigned long *rmapp
;
542 if (!is_rmap_spte(*spte
))
544 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
545 sp
= page_header(__pa(spte
));
546 sp
->gfns
[spte
- sp
->spt
] = gfn
;
547 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
, sp
->role
.level
);
549 rmap_printk("rmap_add: %p %llx 0->1\n", spte
, *spte
);
550 *rmapp
= (unsigned long)spte
;
551 } else if (!(*rmapp
& 1)) {
552 rmap_printk("rmap_add: %p %llx 1->many\n", spte
, *spte
);
553 desc
= mmu_alloc_rmap_desc(vcpu
);
554 desc
->sptes
[0] = (u64
*)*rmapp
;
555 desc
->sptes
[1] = spte
;
556 *rmapp
= (unsigned long)desc
| 1;
558 rmap_printk("rmap_add: %p %llx many->many\n", spte
, *spte
);
559 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
560 while (desc
->sptes
[RMAP_EXT
-1] && desc
->more
) {
564 if (desc
->sptes
[RMAP_EXT
-1]) {
565 desc
->more
= mmu_alloc_rmap_desc(vcpu
);
568 for (i
= 0; desc
->sptes
[i
]; ++i
)
570 desc
->sptes
[i
] = spte
;
575 static void rmap_desc_remove_entry(unsigned long *rmapp
,
576 struct kvm_rmap_desc
*desc
,
578 struct kvm_rmap_desc
*prev_desc
)
582 for (j
= RMAP_EXT
- 1; !desc
->sptes
[j
] && j
> i
; --j
)
584 desc
->sptes
[i
] = desc
->sptes
[j
];
585 desc
->sptes
[j
] = NULL
;
588 if (!prev_desc
&& !desc
->more
)
589 *rmapp
= (unsigned long)desc
->sptes
[0];
592 prev_desc
->more
= desc
->more
;
594 *rmapp
= (unsigned long)desc
->more
| 1;
595 mmu_free_rmap_desc(desc
);
598 static void rmap_remove(struct kvm
*kvm
, u64
*spte
)
600 struct kvm_rmap_desc
*desc
;
601 struct kvm_rmap_desc
*prev_desc
;
602 struct kvm_mmu_page
*sp
;
604 unsigned long *rmapp
;
607 if (!is_rmap_spte(*spte
))
609 sp
= page_header(__pa(spte
));
610 pfn
= spte_to_pfn(*spte
);
611 if (*spte
& shadow_accessed_mask
)
612 kvm_set_pfn_accessed(pfn
);
613 if (is_writable_pte(*spte
))
614 kvm_set_pfn_dirty(pfn
);
615 rmapp
= gfn_to_rmap(kvm
, sp
->gfns
[spte
- sp
->spt
], sp
->role
.level
);
617 printk(KERN_ERR
"rmap_remove: %p %llx 0->BUG\n", spte
, *spte
);
619 } else if (!(*rmapp
& 1)) {
620 rmap_printk("rmap_remove: %p %llx 1->0\n", spte
, *spte
);
621 if ((u64
*)*rmapp
!= spte
) {
622 printk(KERN_ERR
"rmap_remove: %p %llx 1->BUG\n",
628 rmap_printk("rmap_remove: %p %llx many->many\n", spte
, *spte
);
629 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
632 for (i
= 0; i
< RMAP_EXT
&& desc
->sptes
[i
]; ++i
)
633 if (desc
->sptes
[i
] == spte
) {
634 rmap_desc_remove_entry(rmapp
,
642 pr_err("rmap_remove: %p %llx many->many\n", spte
, *spte
);
647 static u64
*rmap_next(struct kvm
*kvm
, unsigned long *rmapp
, u64
*spte
)
649 struct kvm_rmap_desc
*desc
;
655 else if (!(*rmapp
& 1)) {
657 return (u64
*)*rmapp
;
660 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
663 for (i
= 0; i
< RMAP_EXT
&& desc
->sptes
[i
]; ++i
) {
664 if (prev_spte
== spte
)
665 return desc
->sptes
[i
];
666 prev_spte
= desc
->sptes
[i
];
673 static int rmap_write_protect(struct kvm
*kvm
, u64 gfn
)
675 unsigned long *rmapp
;
677 int i
, write_protected
= 0;
679 gfn
= unalias_gfn(kvm
, gfn
);
680 rmapp
= gfn_to_rmap(kvm
, gfn
, PT_PAGE_TABLE_LEVEL
);
682 spte
= rmap_next(kvm
, rmapp
, NULL
);
685 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
686 rmap_printk("rmap_write_protect: spte %p %llx\n", spte
, *spte
);
687 if (is_writable_pte(*spte
)) {
688 __set_spte(spte
, *spte
& ~PT_WRITABLE_MASK
);
691 spte
= rmap_next(kvm
, rmapp
, spte
);
693 if (write_protected
) {
696 spte
= rmap_next(kvm
, rmapp
, NULL
);
697 pfn
= spte_to_pfn(*spte
);
698 kvm_set_pfn_dirty(pfn
);
701 /* check for huge page mappings */
702 for (i
= PT_DIRECTORY_LEVEL
;
703 i
< PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
; ++i
) {
704 rmapp
= gfn_to_rmap(kvm
, gfn
, i
);
705 spte
= rmap_next(kvm
, rmapp
, NULL
);
708 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
709 BUG_ON((*spte
& (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
)) != (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
));
710 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte
, *spte
, gfn
);
711 if (is_writable_pte(*spte
)) {
712 rmap_remove(kvm
, spte
);
714 __set_spte(spte
, shadow_trap_nonpresent_pte
);
718 spte
= rmap_next(kvm
, rmapp
, spte
);
722 return write_protected
;
725 static int kvm_unmap_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
729 int need_tlb_flush
= 0;
731 while ((spte
= rmap_next(kvm
, rmapp
, NULL
))) {
732 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
733 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte
, *spte
);
734 rmap_remove(kvm
, spte
);
735 __set_spte(spte
, shadow_trap_nonpresent_pte
);
738 return need_tlb_flush
;
741 static int kvm_set_pte_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
746 pte_t
*ptep
= (pte_t
*)data
;
749 WARN_ON(pte_huge(*ptep
));
750 new_pfn
= pte_pfn(*ptep
);
751 spte
= rmap_next(kvm
, rmapp
, NULL
);
753 BUG_ON(!is_shadow_present_pte(*spte
));
754 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte
, *spte
);
756 if (pte_write(*ptep
)) {
757 rmap_remove(kvm
, spte
);
758 __set_spte(spte
, shadow_trap_nonpresent_pte
);
759 spte
= rmap_next(kvm
, rmapp
, NULL
);
761 new_spte
= *spte
&~ (PT64_BASE_ADDR_MASK
);
762 new_spte
|= (u64
)new_pfn
<< PAGE_SHIFT
;
764 new_spte
&= ~PT_WRITABLE_MASK
;
765 new_spte
&= ~SPTE_HOST_WRITEABLE
;
766 if (is_writable_pte(*spte
))
767 kvm_set_pfn_dirty(spte_to_pfn(*spte
));
768 __set_spte(spte
, new_spte
);
769 spte
= rmap_next(kvm
, rmapp
, spte
);
773 kvm_flush_remote_tlbs(kvm
);
778 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
780 int (*handler
)(struct kvm
*kvm
, unsigned long *rmapp
,
786 struct kvm_memslots
*slots
;
788 slots
= kvm_memslots(kvm
);
790 for (i
= 0; i
< slots
->nmemslots
; i
++) {
791 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
792 unsigned long start
= memslot
->userspace_addr
;
795 end
= start
+ (memslot
->npages
<< PAGE_SHIFT
);
796 if (hva
>= start
&& hva
< end
) {
797 gfn_t gfn_offset
= (hva
- start
) >> PAGE_SHIFT
;
799 ret
= handler(kvm
, &memslot
->rmap
[gfn_offset
], data
);
801 for (j
= 0; j
< KVM_NR_PAGE_SIZES
- 1; ++j
) {
802 int idx
= gfn_offset
;
803 idx
/= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL
+ j
);
805 &memslot
->lpage_info
[j
][idx
].rmap_pde
,
808 trace_kvm_age_page(hva
, memslot
, ret
);
816 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
818 return kvm_handle_hva(kvm
, hva
, 0, kvm_unmap_rmapp
);
821 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
823 kvm_handle_hva(kvm
, hva
, (unsigned long)&pte
, kvm_set_pte_rmapp
);
826 static int kvm_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
833 * Emulate the accessed bit for EPT, by checking if this page has
834 * an EPT mapping, and clearing it if it does. On the next access,
835 * a new EPT mapping will be established.
836 * This has some overhead, but not as much as the cost of swapping
837 * out actively used pages or breaking up actively used hugepages.
839 if (!shadow_accessed_mask
)
840 return kvm_unmap_rmapp(kvm
, rmapp
, data
);
842 spte
= rmap_next(kvm
, rmapp
, NULL
);
846 BUG_ON(!(_spte
& PT_PRESENT_MASK
));
847 _young
= _spte
& PT_ACCESSED_MASK
;
850 clear_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
852 spte
= rmap_next(kvm
, rmapp
, spte
);
857 #define RMAP_RECYCLE_THRESHOLD 1000
859 static void rmap_recycle(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
)
861 unsigned long *rmapp
;
862 struct kvm_mmu_page
*sp
;
864 sp
= page_header(__pa(spte
));
866 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
867 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
, sp
->role
.level
);
869 kvm_unmap_rmapp(vcpu
->kvm
, rmapp
, 0);
870 kvm_flush_remote_tlbs(vcpu
->kvm
);
873 int kvm_age_hva(struct kvm
*kvm
, unsigned long hva
)
875 return kvm_handle_hva(kvm
, hva
, 0, kvm_age_rmapp
);
879 static int is_empty_shadow_page(u64
*spt
)
884 for (pos
= spt
, end
= pos
+ PAGE_SIZE
/ sizeof(u64
); pos
!= end
; pos
++)
885 if (is_shadow_present_pte(*pos
)) {
886 printk(KERN_ERR
"%s: %p %llx\n", __func__
,
894 static void kvm_mmu_free_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
896 ASSERT(is_empty_shadow_page(sp
->spt
));
898 __free_page(virt_to_page(sp
->spt
));
899 __free_page(virt_to_page(sp
->gfns
));
901 ++kvm
->arch
.n_free_mmu_pages
;
904 static unsigned kvm_page_table_hashfn(gfn_t gfn
)
906 return gfn
& ((1 << KVM_MMU_HASH_SHIFT
) - 1);
909 static struct kvm_mmu_page
*kvm_mmu_alloc_page(struct kvm_vcpu
*vcpu
,
912 struct kvm_mmu_page
*sp
;
914 sp
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_header_cache
, sizeof *sp
);
915 sp
->spt
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
916 sp
->gfns
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
917 set_page_private(virt_to_page(sp
->spt
), (unsigned long)sp
);
918 list_add(&sp
->link
, &vcpu
->kvm
->arch
.active_mmu_pages
);
919 bitmap_zero(sp
->slot_bitmap
, KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
);
921 sp
->parent_pte
= parent_pte
;
922 --vcpu
->kvm
->arch
.n_free_mmu_pages
;
926 static void mmu_page_add_parent_pte(struct kvm_vcpu
*vcpu
,
927 struct kvm_mmu_page
*sp
, u64
*parent_pte
)
929 struct kvm_pte_chain
*pte_chain
;
930 struct hlist_node
*node
;
935 if (!sp
->multimapped
) {
936 u64
*old
= sp
->parent_pte
;
939 sp
->parent_pte
= parent_pte
;
943 pte_chain
= mmu_alloc_pte_chain(vcpu
);
944 INIT_HLIST_HEAD(&sp
->parent_ptes
);
945 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
946 pte_chain
->parent_ptes
[0] = old
;
948 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
) {
949 if (pte_chain
->parent_ptes
[NR_PTE_CHAIN_ENTRIES
-1])
951 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
)
952 if (!pte_chain
->parent_ptes
[i
]) {
953 pte_chain
->parent_ptes
[i
] = parent_pte
;
957 pte_chain
= mmu_alloc_pte_chain(vcpu
);
959 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
960 pte_chain
->parent_ptes
[0] = parent_pte
;
963 static void mmu_page_remove_parent_pte(struct kvm_mmu_page
*sp
,
966 struct kvm_pte_chain
*pte_chain
;
967 struct hlist_node
*node
;
970 if (!sp
->multimapped
) {
971 BUG_ON(sp
->parent_pte
!= parent_pte
);
972 sp
->parent_pte
= NULL
;
975 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
976 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
977 if (!pte_chain
->parent_ptes
[i
])
979 if (pte_chain
->parent_ptes
[i
] != parent_pte
)
981 while (i
+ 1 < NR_PTE_CHAIN_ENTRIES
982 && pte_chain
->parent_ptes
[i
+ 1]) {
983 pte_chain
->parent_ptes
[i
]
984 = pte_chain
->parent_ptes
[i
+ 1];
987 pte_chain
->parent_ptes
[i
] = NULL
;
989 hlist_del(&pte_chain
->link
);
990 mmu_free_pte_chain(pte_chain
);
991 if (hlist_empty(&sp
->parent_ptes
)) {
993 sp
->parent_pte
= NULL
;
1002 static void mmu_parent_walk(struct kvm_mmu_page
*sp
, mmu_parent_walk_fn fn
)
1004 struct kvm_pte_chain
*pte_chain
;
1005 struct hlist_node
*node
;
1006 struct kvm_mmu_page
*parent_sp
;
1009 if (!sp
->multimapped
&& sp
->parent_pte
) {
1010 parent_sp
= page_header(__pa(sp
->parent_pte
));
1012 mmu_parent_walk(parent_sp
, fn
);
1015 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
1016 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
1017 if (!pte_chain
->parent_ptes
[i
])
1019 parent_sp
= page_header(__pa(pte_chain
->parent_ptes
[i
]));
1021 mmu_parent_walk(parent_sp
, fn
);
1025 static void kvm_mmu_update_unsync_bitmap(u64
*spte
)
1028 struct kvm_mmu_page
*sp
= page_header(__pa(spte
));
1030 index
= spte
- sp
->spt
;
1031 if (!__test_and_set_bit(index
, sp
->unsync_child_bitmap
))
1032 sp
->unsync_children
++;
1033 WARN_ON(!sp
->unsync_children
);
1036 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page
*sp
)
1038 struct kvm_pte_chain
*pte_chain
;
1039 struct hlist_node
*node
;
1042 if (!sp
->parent_pte
)
1045 if (!sp
->multimapped
) {
1046 kvm_mmu_update_unsync_bitmap(sp
->parent_pte
);
1050 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
1051 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
1052 if (!pte_chain
->parent_ptes
[i
])
1054 kvm_mmu_update_unsync_bitmap(pte_chain
->parent_ptes
[i
]);
1058 static int unsync_walk_fn(struct kvm_mmu_page
*sp
)
1060 kvm_mmu_update_parents_unsync(sp
);
1064 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page
*sp
)
1066 mmu_parent_walk(sp
, unsync_walk_fn
);
1067 kvm_mmu_update_parents_unsync(sp
);
1070 static void nonpaging_prefetch_page(struct kvm_vcpu
*vcpu
,
1071 struct kvm_mmu_page
*sp
)
1075 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
1076 sp
->spt
[i
] = shadow_trap_nonpresent_pte
;
1079 static int nonpaging_sync_page(struct kvm_vcpu
*vcpu
,
1080 struct kvm_mmu_page
*sp
)
1085 static void nonpaging_invlpg(struct kvm_vcpu
*vcpu
, gva_t gva
)
1089 #define KVM_PAGE_ARRAY_NR 16
1091 struct kvm_mmu_pages
{
1092 struct mmu_page_and_offset
{
1093 struct kvm_mmu_page
*sp
;
1095 } page
[KVM_PAGE_ARRAY_NR
];
1099 #define for_each_unsync_children(bitmap, idx) \
1100 for (idx = find_first_bit(bitmap, 512); \
1102 idx = find_next_bit(bitmap, 512, idx+1))
1104 static int mmu_pages_add(struct kvm_mmu_pages
*pvec
, struct kvm_mmu_page
*sp
,
1110 for (i
=0; i
< pvec
->nr
; i
++)
1111 if (pvec
->page
[i
].sp
== sp
)
1114 pvec
->page
[pvec
->nr
].sp
= sp
;
1115 pvec
->page
[pvec
->nr
].idx
= idx
;
1117 return (pvec
->nr
== KVM_PAGE_ARRAY_NR
);
1120 static int __mmu_unsync_walk(struct kvm_mmu_page
*sp
,
1121 struct kvm_mmu_pages
*pvec
)
1123 int i
, ret
, nr_unsync_leaf
= 0;
1125 for_each_unsync_children(sp
->unsync_child_bitmap
, i
) {
1126 u64 ent
= sp
->spt
[i
];
1128 if (is_shadow_present_pte(ent
) && !is_large_pte(ent
)) {
1129 struct kvm_mmu_page
*child
;
1130 child
= page_header(ent
& PT64_BASE_ADDR_MASK
);
1132 if (child
->unsync_children
) {
1133 if (mmu_pages_add(pvec
, child
, i
))
1136 ret
= __mmu_unsync_walk(child
, pvec
);
1138 __clear_bit(i
, sp
->unsync_child_bitmap
);
1140 nr_unsync_leaf
+= ret
;
1145 if (child
->unsync
) {
1147 if (mmu_pages_add(pvec
, child
, i
))
1153 if (find_first_bit(sp
->unsync_child_bitmap
, 512) == 512)
1154 sp
->unsync_children
= 0;
1156 return nr_unsync_leaf
;
1159 static int mmu_unsync_walk(struct kvm_mmu_page
*sp
,
1160 struct kvm_mmu_pages
*pvec
)
1162 if (!sp
->unsync_children
)
1165 mmu_pages_add(pvec
, sp
, 0);
1166 return __mmu_unsync_walk(sp
, pvec
);
1169 static struct kvm_mmu_page
*kvm_mmu_lookup_page(struct kvm
*kvm
, gfn_t gfn
)
1172 struct hlist_head
*bucket
;
1173 struct kvm_mmu_page
*sp
;
1174 struct hlist_node
*node
;
1176 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1177 index
= kvm_page_table_hashfn(gfn
);
1178 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1179 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
1180 if (sp
->gfn
== gfn
&& !sp
->role
.direct
1181 && !sp
->role
.invalid
) {
1182 pgprintk("%s: found role %x\n",
1183 __func__
, sp
->role
.word
);
1189 static void kvm_unlink_unsync_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1191 WARN_ON(!sp
->unsync
);
1192 trace_kvm_mmu_sync_page(sp
);
1194 --kvm
->stat
.mmu_unsync
;
1197 static int kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
);
1199 static int kvm_sync_page(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1201 if (sp
->role
.cr4_pae
!= !!is_pae(vcpu
)) {
1202 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1206 if (rmap_write_protect(vcpu
->kvm
, sp
->gfn
))
1207 kvm_flush_remote_tlbs(vcpu
->kvm
);
1208 kvm_unlink_unsync_page(vcpu
->kvm
, sp
);
1209 if (vcpu
->arch
.mmu
.sync_page(vcpu
, sp
)) {
1210 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1214 kvm_mmu_flush_tlb(vcpu
);
1218 struct mmu_page_path
{
1219 struct kvm_mmu_page
*parent
[PT64_ROOT_LEVEL
-1];
1220 unsigned int idx
[PT64_ROOT_LEVEL
-1];
1223 #define for_each_sp(pvec, sp, parents, i) \
1224 for (i = mmu_pages_next(&pvec, &parents, -1), \
1225 sp = pvec.page[i].sp; \
1226 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1227 i = mmu_pages_next(&pvec, &parents, i))
1229 static int mmu_pages_next(struct kvm_mmu_pages
*pvec
,
1230 struct mmu_page_path
*parents
,
1235 for (n
= i
+1; n
< pvec
->nr
; n
++) {
1236 struct kvm_mmu_page
*sp
= pvec
->page
[n
].sp
;
1238 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
) {
1239 parents
->idx
[0] = pvec
->page
[n
].idx
;
1243 parents
->parent
[sp
->role
.level
-2] = sp
;
1244 parents
->idx
[sp
->role
.level
-1] = pvec
->page
[n
].idx
;
1250 static void mmu_pages_clear_parents(struct mmu_page_path
*parents
)
1252 struct kvm_mmu_page
*sp
;
1253 unsigned int level
= 0;
1256 unsigned int idx
= parents
->idx
[level
];
1258 sp
= parents
->parent
[level
];
1262 --sp
->unsync_children
;
1263 WARN_ON((int)sp
->unsync_children
< 0);
1264 __clear_bit(idx
, sp
->unsync_child_bitmap
);
1266 } while (level
< PT64_ROOT_LEVEL
-1 && !sp
->unsync_children
);
1269 static void kvm_mmu_pages_init(struct kvm_mmu_page
*parent
,
1270 struct mmu_page_path
*parents
,
1271 struct kvm_mmu_pages
*pvec
)
1273 parents
->parent
[parent
->role
.level
-1] = NULL
;
1277 static void mmu_sync_children(struct kvm_vcpu
*vcpu
,
1278 struct kvm_mmu_page
*parent
)
1281 struct kvm_mmu_page
*sp
;
1282 struct mmu_page_path parents
;
1283 struct kvm_mmu_pages pages
;
1285 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1286 while (mmu_unsync_walk(parent
, &pages
)) {
1289 for_each_sp(pages
, sp
, parents
, i
)
1290 protected |= rmap_write_protect(vcpu
->kvm
, sp
->gfn
);
1293 kvm_flush_remote_tlbs(vcpu
->kvm
);
1295 for_each_sp(pages
, sp
, parents
, i
) {
1296 kvm_sync_page(vcpu
, sp
);
1297 mmu_pages_clear_parents(&parents
);
1299 cond_resched_lock(&vcpu
->kvm
->mmu_lock
);
1300 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1304 static struct kvm_mmu_page
*kvm_mmu_get_page(struct kvm_vcpu
*vcpu
,
1312 union kvm_mmu_page_role role
;
1315 struct hlist_head
*bucket
;
1316 struct kvm_mmu_page
*sp
;
1317 struct hlist_node
*node
, *tmp
;
1319 role
= vcpu
->arch
.mmu
.base_role
;
1321 role
.direct
= direct
;
1324 role
.access
= access
;
1325 if (vcpu
->arch
.mmu
.root_level
<= PT32_ROOT_LEVEL
) {
1326 quadrant
= gaddr
>> (PAGE_SHIFT
+ (PT64_PT_BITS
* level
));
1327 quadrant
&= (1 << ((PT32_PT_BITS
- PT64_PT_BITS
) * level
)) - 1;
1328 role
.quadrant
= quadrant
;
1330 index
= kvm_page_table_hashfn(gfn
);
1331 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1332 hlist_for_each_entry_safe(sp
, node
, tmp
, bucket
, hash_link
)
1333 if (sp
->gfn
== gfn
) {
1335 if (kvm_sync_page(vcpu
, sp
))
1338 if (sp
->role
.word
!= role
.word
)
1341 mmu_page_add_parent_pte(vcpu
, sp
, parent_pte
);
1342 if (sp
->unsync_children
) {
1343 set_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
);
1344 kvm_mmu_mark_parents_unsync(sp
);
1346 trace_kvm_mmu_get_page(sp
, false);
1349 ++vcpu
->kvm
->stat
.mmu_cache_miss
;
1350 sp
= kvm_mmu_alloc_page(vcpu
, parent_pte
);
1355 hlist_add_head(&sp
->hash_link
, bucket
);
1357 if (rmap_write_protect(vcpu
->kvm
, gfn
))
1358 kvm_flush_remote_tlbs(vcpu
->kvm
);
1359 account_shadowed(vcpu
->kvm
, gfn
);
1361 if (shadow_trap_nonpresent_pte
!= shadow_notrap_nonpresent_pte
)
1362 vcpu
->arch
.mmu
.prefetch_page(vcpu
, sp
);
1364 nonpaging_prefetch_page(vcpu
, sp
);
1365 trace_kvm_mmu_get_page(sp
, true);
1369 static void shadow_walk_init(struct kvm_shadow_walk_iterator
*iterator
,
1370 struct kvm_vcpu
*vcpu
, u64 addr
)
1372 iterator
->addr
= addr
;
1373 iterator
->shadow_addr
= vcpu
->arch
.mmu
.root_hpa
;
1374 iterator
->level
= vcpu
->arch
.mmu
.shadow_root_level
;
1375 if (iterator
->level
== PT32E_ROOT_LEVEL
) {
1376 iterator
->shadow_addr
1377 = vcpu
->arch
.mmu
.pae_root
[(addr
>> 30) & 3];
1378 iterator
->shadow_addr
&= PT64_BASE_ADDR_MASK
;
1380 if (!iterator
->shadow_addr
)
1381 iterator
->level
= 0;
1385 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator
*iterator
)
1387 if (iterator
->level
< PT_PAGE_TABLE_LEVEL
)
1390 if (iterator
->level
== PT_PAGE_TABLE_LEVEL
)
1391 if (is_large_pte(*iterator
->sptep
))
1394 iterator
->index
= SHADOW_PT_INDEX(iterator
->addr
, iterator
->level
);
1395 iterator
->sptep
= ((u64
*)__va(iterator
->shadow_addr
)) + iterator
->index
;
1399 static void shadow_walk_next(struct kvm_shadow_walk_iterator
*iterator
)
1401 iterator
->shadow_addr
= *iterator
->sptep
& PT64_BASE_ADDR_MASK
;
1405 static void kvm_mmu_page_unlink_children(struct kvm
*kvm
,
1406 struct kvm_mmu_page
*sp
)
1414 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1417 if (is_shadow_present_pte(ent
)) {
1418 if (!is_last_spte(ent
, sp
->role
.level
)) {
1419 ent
&= PT64_BASE_ADDR_MASK
;
1420 mmu_page_remove_parent_pte(page_header(ent
),
1423 if (is_large_pte(ent
))
1425 rmap_remove(kvm
, &pt
[i
]);
1428 pt
[i
] = shadow_trap_nonpresent_pte
;
1432 static void kvm_mmu_put_page(struct kvm_mmu_page
*sp
, u64
*parent_pte
)
1434 mmu_page_remove_parent_pte(sp
, parent_pte
);
1437 static void kvm_mmu_reset_last_pte_updated(struct kvm
*kvm
)
1440 struct kvm_vcpu
*vcpu
;
1442 kvm_for_each_vcpu(i
, vcpu
, kvm
)
1443 vcpu
->arch
.last_pte_updated
= NULL
;
1446 static void kvm_mmu_unlink_parents(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1450 while (sp
->multimapped
|| sp
->parent_pte
) {
1451 if (!sp
->multimapped
)
1452 parent_pte
= sp
->parent_pte
;
1454 struct kvm_pte_chain
*chain
;
1456 chain
= container_of(sp
->parent_ptes
.first
,
1457 struct kvm_pte_chain
, link
);
1458 parent_pte
= chain
->parent_ptes
[0];
1460 BUG_ON(!parent_pte
);
1461 kvm_mmu_put_page(sp
, parent_pte
);
1462 __set_spte(parent_pte
, shadow_trap_nonpresent_pte
);
1466 static int mmu_zap_unsync_children(struct kvm
*kvm
,
1467 struct kvm_mmu_page
*parent
)
1470 struct mmu_page_path parents
;
1471 struct kvm_mmu_pages pages
;
1473 if (parent
->role
.level
== PT_PAGE_TABLE_LEVEL
)
1476 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1477 while (mmu_unsync_walk(parent
, &pages
)) {
1478 struct kvm_mmu_page
*sp
;
1480 for_each_sp(pages
, sp
, parents
, i
) {
1481 kvm_mmu_zap_page(kvm
, sp
);
1482 mmu_pages_clear_parents(&parents
);
1485 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1491 static int kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1495 trace_kvm_mmu_zap_page(sp
);
1496 ++kvm
->stat
.mmu_shadow_zapped
;
1497 ret
= mmu_zap_unsync_children(kvm
, sp
);
1498 kvm_mmu_page_unlink_children(kvm
, sp
);
1499 kvm_mmu_unlink_parents(kvm
, sp
);
1500 kvm_flush_remote_tlbs(kvm
);
1501 if (!sp
->role
.invalid
&& !sp
->role
.direct
)
1502 unaccount_shadowed(kvm
, sp
->gfn
);
1504 kvm_unlink_unsync_page(kvm
, sp
);
1505 if (!sp
->root_count
) {
1506 hlist_del(&sp
->hash_link
);
1507 kvm_mmu_free_page(kvm
, sp
);
1509 sp
->role
.invalid
= 1;
1510 list_move(&sp
->link
, &kvm
->arch
.active_mmu_pages
);
1511 kvm_reload_remote_mmus(kvm
);
1513 kvm_mmu_reset_last_pte_updated(kvm
);
1518 * Changing the number of mmu pages allocated to the vm
1519 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1521 void kvm_mmu_change_mmu_pages(struct kvm
*kvm
, unsigned int kvm_nr_mmu_pages
)
1525 used_pages
= kvm
->arch
.n_alloc_mmu_pages
- kvm
->arch
.n_free_mmu_pages
;
1526 used_pages
= max(0, used_pages
);
1529 * If we set the number of mmu pages to be smaller be than the
1530 * number of actived pages , we must to free some mmu pages before we
1534 if (used_pages
> kvm_nr_mmu_pages
) {
1535 while (used_pages
> kvm_nr_mmu_pages
&&
1536 !list_empty(&kvm
->arch
.active_mmu_pages
)) {
1537 struct kvm_mmu_page
*page
;
1539 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
1540 struct kvm_mmu_page
, link
);
1541 used_pages
-= kvm_mmu_zap_page(kvm
, page
);
1544 kvm_nr_mmu_pages
= used_pages
;
1545 kvm
->arch
.n_free_mmu_pages
= 0;
1548 kvm
->arch
.n_free_mmu_pages
+= kvm_nr_mmu_pages
1549 - kvm
->arch
.n_alloc_mmu_pages
;
1551 kvm
->arch
.n_alloc_mmu_pages
= kvm_nr_mmu_pages
;
1554 static int kvm_mmu_unprotect_page(struct kvm
*kvm
, gfn_t gfn
)
1557 struct hlist_head
*bucket
;
1558 struct kvm_mmu_page
*sp
;
1559 struct hlist_node
*node
, *n
;
1562 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1564 index
= kvm_page_table_hashfn(gfn
);
1565 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1567 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
)
1568 if (sp
->gfn
== gfn
&& !sp
->role
.direct
) {
1569 pgprintk("%s: gfn %lx role %x\n", __func__
, gfn
,
1572 if (kvm_mmu_zap_page(kvm
, sp
))
1578 static void mmu_unshadow(struct kvm
*kvm
, gfn_t gfn
)
1581 struct hlist_head
*bucket
;
1582 struct kvm_mmu_page
*sp
;
1583 struct hlist_node
*node
, *nn
;
1585 index
= kvm_page_table_hashfn(gfn
);
1586 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1588 hlist_for_each_entry_safe(sp
, node
, nn
, bucket
, hash_link
) {
1589 if (sp
->gfn
== gfn
&& !sp
->role
.direct
1590 && !sp
->role
.invalid
) {
1591 pgprintk("%s: zap %lx %x\n",
1592 __func__
, gfn
, sp
->role
.word
);
1593 if (kvm_mmu_zap_page(kvm
, sp
))
1599 static void page_header_update_slot(struct kvm
*kvm
, void *pte
, gfn_t gfn
)
1601 int slot
= memslot_id(kvm
, gfn
);
1602 struct kvm_mmu_page
*sp
= page_header(__pa(pte
));
1604 __set_bit(slot
, sp
->slot_bitmap
);
1607 static void mmu_convert_notrap(struct kvm_mmu_page
*sp
)
1612 if (shadow_trap_nonpresent_pte
== shadow_notrap_nonpresent_pte
)
1615 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1616 if (pt
[i
] == shadow_notrap_nonpresent_pte
)
1617 __set_spte(&pt
[i
], shadow_trap_nonpresent_pte
);
1622 * The function is based on mtrr_type_lookup() in
1623 * arch/x86/kernel/cpu/mtrr/generic.c
1625 static int get_mtrr_type(struct mtrr_state_type
*mtrr_state
,
1630 u8 prev_match
, curr_match
;
1631 int num_var_ranges
= KVM_NR_VAR_MTRR
;
1633 if (!mtrr_state
->enabled
)
1636 /* Make end inclusive end, instead of exclusive */
1639 /* Look in fixed ranges. Just return the type as per start */
1640 if (mtrr_state
->have_fixed
&& (start
< 0x100000)) {
1643 if (start
< 0x80000) {
1645 idx
+= (start
>> 16);
1646 return mtrr_state
->fixed_ranges
[idx
];
1647 } else if (start
< 0xC0000) {
1649 idx
+= ((start
- 0x80000) >> 14);
1650 return mtrr_state
->fixed_ranges
[idx
];
1651 } else if (start
< 0x1000000) {
1653 idx
+= ((start
- 0xC0000) >> 12);
1654 return mtrr_state
->fixed_ranges
[idx
];
1659 * Look in variable ranges
1660 * Look of multiple ranges matching this address and pick type
1661 * as per MTRR precedence
1663 if (!(mtrr_state
->enabled
& 2))
1664 return mtrr_state
->def_type
;
1667 for (i
= 0; i
< num_var_ranges
; ++i
) {
1668 unsigned short start_state
, end_state
;
1670 if (!(mtrr_state
->var_ranges
[i
].mask_lo
& (1 << 11)))
1673 base
= (((u64
)mtrr_state
->var_ranges
[i
].base_hi
) << 32) +
1674 (mtrr_state
->var_ranges
[i
].base_lo
& PAGE_MASK
);
1675 mask
= (((u64
)mtrr_state
->var_ranges
[i
].mask_hi
) << 32) +
1676 (mtrr_state
->var_ranges
[i
].mask_lo
& PAGE_MASK
);
1678 start_state
= ((start
& mask
) == (base
& mask
));
1679 end_state
= ((end
& mask
) == (base
& mask
));
1680 if (start_state
!= end_state
)
1683 if ((start
& mask
) != (base
& mask
))
1686 curr_match
= mtrr_state
->var_ranges
[i
].base_lo
& 0xff;
1687 if (prev_match
== 0xFF) {
1688 prev_match
= curr_match
;
1692 if (prev_match
== MTRR_TYPE_UNCACHABLE
||
1693 curr_match
== MTRR_TYPE_UNCACHABLE
)
1694 return MTRR_TYPE_UNCACHABLE
;
1696 if ((prev_match
== MTRR_TYPE_WRBACK
&&
1697 curr_match
== MTRR_TYPE_WRTHROUGH
) ||
1698 (prev_match
== MTRR_TYPE_WRTHROUGH
&&
1699 curr_match
== MTRR_TYPE_WRBACK
)) {
1700 prev_match
= MTRR_TYPE_WRTHROUGH
;
1701 curr_match
= MTRR_TYPE_WRTHROUGH
;
1704 if (prev_match
!= curr_match
)
1705 return MTRR_TYPE_UNCACHABLE
;
1708 if (prev_match
!= 0xFF)
1711 return mtrr_state
->def_type
;
1714 u8
kvm_get_guest_memory_type(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
1718 mtrr
= get_mtrr_type(&vcpu
->arch
.mtrr_state
, gfn
<< PAGE_SHIFT
,
1719 (gfn
<< PAGE_SHIFT
) + PAGE_SIZE
);
1720 if (mtrr
== 0xfe || mtrr
== 0xff)
1721 mtrr
= MTRR_TYPE_WRBACK
;
1724 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type
);
1726 static int kvm_unsync_page(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1729 struct hlist_head
*bucket
;
1730 struct kvm_mmu_page
*s
;
1731 struct hlist_node
*node
, *n
;
1733 index
= kvm_page_table_hashfn(sp
->gfn
);
1734 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1735 /* don't unsync if pagetable is shadowed with multiple roles */
1736 hlist_for_each_entry_safe(s
, node
, n
, bucket
, hash_link
) {
1737 if (s
->gfn
!= sp
->gfn
|| s
->role
.direct
)
1739 if (s
->role
.word
!= sp
->role
.word
)
1742 trace_kvm_mmu_unsync_page(sp
);
1743 ++vcpu
->kvm
->stat
.mmu_unsync
;
1746 kvm_mmu_mark_parents_unsync(sp
);
1748 mmu_convert_notrap(sp
);
1752 static int mmu_need_write_protect(struct kvm_vcpu
*vcpu
, gfn_t gfn
,
1755 struct kvm_mmu_page
*shadow
;
1757 shadow
= kvm_mmu_lookup_page(vcpu
->kvm
, gfn
);
1759 if (shadow
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
1763 if (can_unsync
&& oos_shadow
)
1764 return kvm_unsync_page(vcpu
, shadow
);
1770 static int set_spte(struct kvm_vcpu
*vcpu
, u64
*sptep
,
1771 unsigned pte_access
, int user_fault
,
1772 int write_fault
, int dirty
, int level
,
1773 gfn_t gfn
, pfn_t pfn
, bool speculative
,
1774 bool can_unsync
, bool reset_host_protection
)
1780 * We don't set the accessed bit, since we sometimes want to see
1781 * whether the guest actually used the pte (in order to detect
1784 spte
= shadow_base_present_pte
| shadow_dirty_mask
;
1786 spte
|= shadow_accessed_mask
;
1788 pte_access
&= ~ACC_WRITE_MASK
;
1789 if (pte_access
& ACC_EXEC_MASK
)
1790 spte
|= shadow_x_mask
;
1792 spte
|= shadow_nx_mask
;
1793 if (pte_access
& ACC_USER_MASK
)
1794 spte
|= shadow_user_mask
;
1795 if (level
> PT_PAGE_TABLE_LEVEL
)
1796 spte
|= PT_PAGE_SIZE_MASK
;
1798 spte
|= kvm_x86_ops
->get_mt_mask(vcpu
, gfn
,
1799 kvm_is_mmio_pfn(pfn
));
1801 if (reset_host_protection
)
1802 spte
|= SPTE_HOST_WRITEABLE
;
1804 spte
|= (u64
)pfn
<< PAGE_SHIFT
;
1806 if ((pte_access
& ACC_WRITE_MASK
)
1807 || (write_fault
&& !is_write_protection(vcpu
) && !user_fault
)) {
1809 if (level
> PT_PAGE_TABLE_LEVEL
&&
1810 has_wrprotected_page(vcpu
->kvm
, gfn
, level
)) {
1812 spte
= shadow_trap_nonpresent_pte
;
1816 spte
|= PT_WRITABLE_MASK
;
1819 * Optimization: for pte sync, if spte was writable the hash
1820 * lookup is unnecessary (and expensive). Write protection
1821 * is responsibility of mmu_get_page / kvm_sync_page.
1822 * Same reasoning can be applied to dirty page accounting.
1824 if (!can_unsync
&& is_writable_pte(*sptep
))
1827 if (mmu_need_write_protect(vcpu
, gfn
, can_unsync
)) {
1828 pgprintk("%s: found shadow page for %lx, marking ro\n",
1831 pte_access
&= ~ACC_WRITE_MASK
;
1832 if (is_writable_pte(spte
))
1833 spte
&= ~PT_WRITABLE_MASK
;
1837 if (pte_access
& ACC_WRITE_MASK
)
1838 mark_page_dirty(vcpu
->kvm
, gfn
);
1841 __set_spte(sptep
, spte
);
1845 static void mmu_set_spte(struct kvm_vcpu
*vcpu
, u64
*sptep
,
1846 unsigned pt_access
, unsigned pte_access
,
1847 int user_fault
, int write_fault
, int dirty
,
1848 int *ptwrite
, int level
, gfn_t gfn
,
1849 pfn_t pfn
, bool speculative
,
1850 bool reset_host_protection
)
1852 int was_rmapped
= 0;
1853 int was_writable
= is_writable_pte(*sptep
);
1856 pgprintk("%s: spte %llx access %x write_fault %d"
1857 " user_fault %d gfn %lx\n",
1858 __func__
, *sptep
, pt_access
,
1859 write_fault
, user_fault
, gfn
);
1861 if (is_rmap_spte(*sptep
)) {
1863 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1864 * the parent of the now unreachable PTE.
1866 if (level
> PT_PAGE_TABLE_LEVEL
&&
1867 !is_large_pte(*sptep
)) {
1868 struct kvm_mmu_page
*child
;
1871 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
1872 mmu_page_remove_parent_pte(child
, sptep
);
1873 } else if (pfn
!= spte_to_pfn(*sptep
)) {
1874 pgprintk("hfn old %lx new %lx\n",
1875 spte_to_pfn(*sptep
), pfn
);
1876 rmap_remove(vcpu
->kvm
, sptep
);
1881 if (set_spte(vcpu
, sptep
, pte_access
, user_fault
, write_fault
,
1882 dirty
, level
, gfn
, pfn
, speculative
, true,
1883 reset_host_protection
)) {
1886 kvm_x86_ops
->tlb_flush(vcpu
);
1889 pgprintk("%s: setting spte %llx\n", __func__
, *sptep
);
1890 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1891 is_large_pte(*sptep
)? "2MB" : "4kB",
1892 *sptep
& PT_PRESENT_MASK
?"RW":"R", gfn
,
1894 if (!was_rmapped
&& is_large_pte(*sptep
))
1895 ++vcpu
->kvm
->stat
.lpages
;
1897 page_header_update_slot(vcpu
->kvm
, sptep
, gfn
);
1899 rmap_count
= rmap_add(vcpu
, sptep
, gfn
);
1900 kvm_release_pfn_clean(pfn
);
1901 if (rmap_count
> RMAP_RECYCLE_THRESHOLD
)
1902 rmap_recycle(vcpu
, sptep
, gfn
);
1905 kvm_release_pfn_dirty(pfn
);
1907 kvm_release_pfn_clean(pfn
);
1910 vcpu
->arch
.last_pte_updated
= sptep
;
1911 vcpu
->arch
.last_pte_gfn
= gfn
;
1915 static void nonpaging_new_cr3(struct kvm_vcpu
*vcpu
)
1919 static int __direct_map(struct kvm_vcpu
*vcpu
, gpa_t v
, int write
,
1920 int level
, gfn_t gfn
, pfn_t pfn
)
1922 struct kvm_shadow_walk_iterator iterator
;
1923 struct kvm_mmu_page
*sp
;
1927 for_each_shadow_entry(vcpu
, (u64
)gfn
<< PAGE_SHIFT
, iterator
) {
1928 if (iterator
.level
== level
) {
1929 mmu_set_spte(vcpu
, iterator
.sptep
, ACC_ALL
, ACC_ALL
,
1930 0, write
, 1, &pt_write
,
1931 level
, gfn
, pfn
, false, true);
1932 ++vcpu
->stat
.pf_fixed
;
1936 if (*iterator
.sptep
== shadow_trap_nonpresent_pte
) {
1937 pseudo_gfn
= (iterator
.addr
& PT64_DIR_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
1938 sp
= kvm_mmu_get_page(vcpu
, pseudo_gfn
, iterator
.addr
,
1940 1, ACC_ALL
, iterator
.sptep
);
1942 pgprintk("nonpaging_map: ENOMEM\n");
1943 kvm_release_pfn_clean(pfn
);
1947 __set_spte(iterator
.sptep
,
1949 | PT_PRESENT_MASK
| PT_WRITABLE_MASK
1950 | shadow_user_mask
| shadow_x_mask
);
1956 static int nonpaging_map(struct kvm_vcpu
*vcpu
, gva_t v
, int write
, gfn_t gfn
)
1961 unsigned long mmu_seq
;
1963 level
= mapping_level(vcpu
, gfn
);
1966 * This path builds a PAE pagetable - so we can map 2mb pages at
1967 * maximum. Therefore check if the level is larger than that.
1969 if (level
> PT_DIRECTORY_LEVEL
)
1970 level
= PT_DIRECTORY_LEVEL
;
1972 gfn
&= ~(KVM_PAGES_PER_HPAGE(level
) - 1);
1974 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1976 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
1979 if (is_error_pfn(pfn
)) {
1980 kvm_release_pfn_clean(pfn
);
1984 spin_lock(&vcpu
->kvm
->mmu_lock
);
1985 if (mmu_notifier_retry(vcpu
, mmu_seq
))
1987 kvm_mmu_free_some_pages(vcpu
);
1988 r
= __direct_map(vcpu
, v
, write
, level
, gfn
, pfn
);
1989 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1995 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1996 kvm_release_pfn_clean(pfn
);
2001 static void mmu_free_roots(struct kvm_vcpu
*vcpu
)
2004 struct kvm_mmu_page
*sp
;
2006 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
2008 spin_lock(&vcpu
->kvm
->mmu_lock
);
2009 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
2010 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
2012 sp
= page_header(root
);
2014 if (!sp
->root_count
&& sp
->role
.invalid
)
2015 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2016 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
2017 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2020 for (i
= 0; i
< 4; ++i
) {
2021 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
2024 root
&= PT64_BASE_ADDR_MASK
;
2025 sp
= page_header(root
);
2027 if (!sp
->root_count
&& sp
->role
.invalid
)
2028 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2030 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
2032 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2033 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
2036 static int mmu_check_root(struct kvm_vcpu
*vcpu
, gfn_t root_gfn
)
2040 if (!kvm_is_visible_gfn(vcpu
->kvm
, root_gfn
)) {
2041 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
2048 static int mmu_alloc_roots(struct kvm_vcpu
*vcpu
)
2052 struct kvm_mmu_page
*sp
;
2056 root_gfn
= vcpu
->arch
.cr3
>> PAGE_SHIFT
;
2058 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
2059 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
2061 ASSERT(!VALID_PAGE(root
));
2062 if (mmu_check_root(vcpu
, root_gfn
))
2068 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, 0,
2069 PT64_ROOT_LEVEL
, direct
,
2071 root
= __pa(sp
->spt
);
2073 vcpu
->arch
.mmu
.root_hpa
= root
;
2076 direct
= !is_paging(vcpu
);
2077 for (i
= 0; i
< 4; ++i
) {
2078 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
2080 ASSERT(!VALID_PAGE(root
));
2081 if (vcpu
->arch
.mmu
.root_level
== PT32E_ROOT_LEVEL
) {
2082 pdptr
= kvm_pdptr_read(vcpu
, i
);
2083 if (!is_present_gpte(pdptr
)) {
2084 vcpu
->arch
.mmu
.pae_root
[i
] = 0;
2087 root_gfn
= pdptr
>> PAGE_SHIFT
;
2088 } else if (vcpu
->arch
.mmu
.root_level
== 0)
2090 if (mmu_check_root(vcpu
, root_gfn
))
2096 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, i
<< 30,
2097 PT32_ROOT_LEVEL
, direct
,
2099 root
= __pa(sp
->spt
);
2101 vcpu
->arch
.mmu
.pae_root
[i
] = root
| PT_PRESENT_MASK
;
2103 vcpu
->arch
.mmu
.root_hpa
= __pa(vcpu
->arch
.mmu
.pae_root
);
2107 static void mmu_sync_roots(struct kvm_vcpu
*vcpu
)
2110 struct kvm_mmu_page
*sp
;
2112 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
2114 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
2115 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
2116 sp
= page_header(root
);
2117 mmu_sync_children(vcpu
, sp
);
2120 for (i
= 0; i
< 4; ++i
) {
2121 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
2123 if (root
&& VALID_PAGE(root
)) {
2124 root
&= PT64_BASE_ADDR_MASK
;
2125 sp
= page_header(root
);
2126 mmu_sync_children(vcpu
, sp
);
2131 void kvm_mmu_sync_roots(struct kvm_vcpu
*vcpu
)
2133 spin_lock(&vcpu
->kvm
->mmu_lock
);
2134 mmu_sync_roots(vcpu
);
2135 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2138 static gpa_t
nonpaging_gva_to_gpa(struct kvm_vcpu
*vcpu
, gva_t vaddr
,
2139 u32 access
, u32
*error
)
2146 static int nonpaging_page_fault(struct kvm_vcpu
*vcpu
, gva_t gva
,
2152 pgprintk("%s: gva %lx error %x\n", __func__
, gva
, error_code
);
2153 r
= mmu_topup_memory_caches(vcpu
);
2158 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2160 gfn
= gva
>> PAGE_SHIFT
;
2162 return nonpaging_map(vcpu
, gva
& PAGE_MASK
,
2163 error_code
& PFERR_WRITE_MASK
, gfn
);
2166 static int tdp_page_fault(struct kvm_vcpu
*vcpu
, gva_t gpa
,
2172 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
2173 unsigned long mmu_seq
;
2176 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2178 r
= mmu_topup_memory_caches(vcpu
);
2182 level
= mapping_level(vcpu
, gfn
);
2184 gfn
&= ~(KVM_PAGES_PER_HPAGE(level
) - 1);
2186 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
2188 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
2189 if (is_error_pfn(pfn
)) {
2190 kvm_release_pfn_clean(pfn
);
2193 spin_lock(&vcpu
->kvm
->mmu_lock
);
2194 if (mmu_notifier_retry(vcpu
, mmu_seq
))
2196 kvm_mmu_free_some_pages(vcpu
);
2197 r
= __direct_map(vcpu
, gpa
, error_code
& PFERR_WRITE_MASK
,
2199 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2204 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2205 kvm_release_pfn_clean(pfn
);
2209 static void nonpaging_free(struct kvm_vcpu
*vcpu
)
2211 mmu_free_roots(vcpu
);
2214 static int nonpaging_init_context(struct kvm_vcpu
*vcpu
)
2216 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2218 context
->new_cr3
= nonpaging_new_cr3
;
2219 context
->page_fault
= nonpaging_page_fault
;
2220 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
2221 context
->free
= nonpaging_free
;
2222 context
->prefetch_page
= nonpaging_prefetch_page
;
2223 context
->sync_page
= nonpaging_sync_page
;
2224 context
->invlpg
= nonpaging_invlpg
;
2225 context
->root_level
= 0;
2226 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
2227 context
->root_hpa
= INVALID_PAGE
;
2231 void kvm_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
2233 ++vcpu
->stat
.tlb_flush
;
2234 kvm_x86_ops
->tlb_flush(vcpu
);
2237 static void paging_new_cr3(struct kvm_vcpu
*vcpu
)
2239 pgprintk("%s: cr3 %lx\n", __func__
, vcpu
->arch
.cr3
);
2240 mmu_free_roots(vcpu
);
2243 static void inject_page_fault(struct kvm_vcpu
*vcpu
,
2247 kvm_inject_page_fault(vcpu
, addr
, err_code
);
2250 static void paging_free(struct kvm_vcpu
*vcpu
)
2252 nonpaging_free(vcpu
);
2255 static bool is_rsvd_bits_set(struct kvm_vcpu
*vcpu
, u64 gpte
, int level
)
2259 bit7
= (gpte
>> 7) & 1;
2260 return (gpte
& vcpu
->arch
.mmu
.rsvd_bits_mask
[bit7
][level
-1]) != 0;
2264 #include "paging_tmpl.h"
2268 #include "paging_tmpl.h"
2271 static void reset_rsvds_bits_mask(struct kvm_vcpu
*vcpu
, int level
)
2273 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2274 int maxphyaddr
= cpuid_maxphyaddr(vcpu
);
2275 u64 exb_bit_rsvd
= 0;
2278 exb_bit_rsvd
= rsvd_bits(63, 63);
2280 case PT32_ROOT_LEVEL
:
2281 /* no rsvd bits for 2 level 4K page table entries */
2282 context
->rsvd_bits_mask
[0][1] = 0;
2283 context
->rsvd_bits_mask
[0][0] = 0;
2284 context
->rsvd_bits_mask
[1][0] = context
->rsvd_bits_mask
[0][0];
2286 if (!is_pse(vcpu
)) {
2287 context
->rsvd_bits_mask
[1][1] = 0;
2291 if (is_cpuid_PSE36())
2292 /* 36bits PSE 4MB page */
2293 context
->rsvd_bits_mask
[1][1] = rsvd_bits(17, 21);
2295 /* 32 bits PSE 4MB page */
2296 context
->rsvd_bits_mask
[1][1] = rsvd_bits(13, 21);
2298 case PT32E_ROOT_LEVEL
:
2299 context
->rsvd_bits_mask
[0][2] =
2300 rsvd_bits(maxphyaddr
, 63) |
2301 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2302 context
->rsvd_bits_mask
[0][1] = exb_bit_rsvd
|
2303 rsvd_bits(maxphyaddr
, 62); /* PDE */
2304 context
->rsvd_bits_mask
[0][0] = exb_bit_rsvd
|
2305 rsvd_bits(maxphyaddr
, 62); /* PTE */
2306 context
->rsvd_bits_mask
[1][1] = exb_bit_rsvd
|
2307 rsvd_bits(maxphyaddr
, 62) |
2308 rsvd_bits(13, 20); /* large page */
2309 context
->rsvd_bits_mask
[1][0] = context
->rsvd_bits_mask
[0][0];
2311 case PT64_ROOT_LEVEL
:
2312 context
->rsvd_bits_mask
[0][3] = exb_bit_rsvd
|
2313 rsvd_bits(maxphyaddr
, 51) | rsvd_bits(7, 8);
2314 context
->rsvd_bits_mask
[0][2] = exb_bit_rsvd
|
2315 rsvd_bits(maxphyaddr
, 51) | rsvd_bits(7, 8);
2316 context
->rsvd_bits_mask
[0][1] = exb_bit_rsvd
|
2317 rsvd_bits(maxphyaddr
, 51);
2318 context
->rsvd_bits_mask
[0][0] = exb_bit_rsvd
|
2319 rsvd_bits(maxphyaddr
, 51);
2320 context
->rsvd_bits_mask
[1][3] = context
->rsvd_bits_mask
[0][3];
2321 context
->rsvd_bits_mask
[1][2] = exb_bit_rsvd
|
2322 rsvd_bits(maxphyaddr
, 51) |
2324 context
->rsvd_bits_mask
[1][1] = exb_bit_rsvd
|
2325 rsvd_bits(maxphyaddr
, 51) |
2326 rsvd_bits(13, 20); /* large page */
2327 context
->rsvd_bits_mask
[1][0] = context
->rsvd_bits_mask
[0][0];
2332 static int paging64_init_context_common(struct kvm_vcpu
*vcpu
, int level
)
2334 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2336 ASSERT(is_pae(vcpu
));
2337 context
->new_cr3
= paging_new_cr3
;
2338 context
->page_fault
= paging64_page_fault
;
2339 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2340 context
->prefetch_page
= paging64_prefetch_page
;
2341 context
->sync_page
= paging64_sync_page
;
2342 context
->invlpg
= paging64_invlpg
;
2343 context
->free
= paging_free
;
2344 context
->root_level
= level
;
2345 context
->shadow_root_level
= level
;
2346 context
->root_hpa
= INVALID_PAGE
;
2350 static int paging64_init_context(struct kvm_vcpu
*vcpu
)
2352 reset_rsvds_bits_mask(vcpu
, PT64_ROOT_LEVEL
);
2353 return paging64_init_context_common(vcpu
, PT64_ROOT_LEVEL
);
2356 static int paging32_init_context(struct kvm_vcpu
*vcpu
)
2358 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2360 reset_rsvds_bits_mask(vcpu
, PT32_ROOT_LEVEL
);
2361 context
->new_cr3
= paging_new_cr3
;
2362 context
->page_fault
= paging32_page_fault
;
2363 context
->gva_to_gpa
= paging32_gva_to_gpa
;
2364 context
->free
= paging_free
;
2365 context
->prefetch_page
= paging32_prefetch_page
;
2366 context
->sync_page
= paging32_sync_page
;
2367 context
->invlpg
= paging32_invlpg
;
2368 context
->root_level
= PT32_ROOT_LEVEL
;
2369 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
2370 context
->root_hpa
= INVALID_PAGE
;
2374 static int paging32E_init_context(struct kvm_vcpu
*vcpu
)
2376 reset_rsvds_bits_mask(vcpu
, PT32E_ROOT_LEVEL
);
2377 return paging64_init_context_common(vcpu
, PT32E_ROOT_LEVEL
);
2380 static int init_kvm_tdp_mmu(struct kvm_vcpu
*vcpu
)
2382 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2384 context
->new_cr3
= nonpaging_new_cr3
;
2385 context
->page_fault
= tdp_page_fault
;
2386 context
->free
= nonpaging_free
;
2387 context
->prefetch_page
= nonpaging_prefetch_page
;
2388 context
->sync_page
= nonpaging_sync_page
;
2389 context
->invlpg
= nonpaging_invlpg
;
2390 context
->shadow_root_level
= kvm_x86_ops
->get_tdp_level();
2391 context
->root_hpa
= INVALID_PAGE
;
2393 if (!is_paging(vcpu
)) {
2394 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
2395 context
->root_level
= 0;
2396 } else if (is_long_mode(vcpu
)) {
2397 reset_rsvds_bits_mask(vcpu
, PT64_ROOT_LEVEL
);
2398 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2399 context
->root_level
= PT64_ROOT_LEVEL
;
2400 } else if (is_pae(vcpu
)) {
2401 reset_rsvds_bits_mask(vcpu
, PT32E_ROOT_LEVEL
);
2402 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2403 context
->root_level
= PT32E_ROOT_LEVEL
;
2405 reset_rsvds_bits_mask(vcpu
, PT32_ROOT_LEVEL
);
2406 context
->gva_to_gpa
= paging32_gva_to_gpa
;
2407 context
->root_level
= PT32_ROOT_LEVEL
;
2413 static int init_kvm_softmmu(struct kvm_vcpu
*vcpu
)
2418 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2420 if (!is_paging(vcpu
))
2421 r
= nonpaging_init_context(vcpu
);
2422 else if (is_long_mode(vcpu
))
2423 r
= paging64_init_context(vcpu
);
2424 else if (is_pae(vcpu
))
2425 r
= paging32E_init_context(vcpu
);
2427 r
= paging32_init_context(vcpu
);
2429 vcpu
->arch
.mmu
.base_role
.cr4_pae
= !!is_pae(vcpu
);
2434 static int init_kvm_mmu(struct kvm_vcpu
*vcpu
)
2436 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
2439 return init_kvm_tdp_mmu(vcpu
);
2441 return init_kvm_softmmu(vcpu
);
2444 static void destroy_kvm_mmu(struct kvm_vcpu
*vcpu
)
2447 if (VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
)) {
2448 vcpu
->arch
.mmu
.free(vcpu
);
2449 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
2453 int kvm_mmu_reset_context(struct kvm_vcpu
*vcpu
)
2455 destroy_kvm_mmu(vcpu
);
2456 return init_kvm_mmu(vcpu
);
2458 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context
);
2460 int kvm_mmu_load(struct kvm_vcpu
*vcpu
)
2464 r
= mmu_topup_memory_caches(vcpu
);
2467 spin_lock(&vcpu
->kvm
->mmu_lock
);
2468 kvm_mmu_free_some_pages(vcpu
);
2469 r
= mmu_alloc_roots(vcpu
);
2470 mmu_sync_roots(vcpu
);
2471 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2474 /* set_cr3() should ensure TLB has been flushed */
2475 kvm_x86_ops
->set_cr3(vcpu
, vcpu
->arch
.mmu
.root_hpa
);
2479 EXPORT_SYMBOL_GPL(kvm_mmu_load
);
2481 void kvm_mmu_unload(struct kvm_vcpu
*vcpu
)
2483 mmu_free_roots(vcpu
);
2486 static void mmu_pte_write_zap_pte(struct kvm_vcpu
*vcpu
,
2487 struct kvm_mmu_page
*sp
,
2491 struct kvm_mmu_page
*child
;
2494 if (is_shadow_present_pte(pte
)) {
2495 if (is_last_spte(pte
, sp
->role
.level
))
2496 rmap_remove(vcpu
->kvm
, spte
);
2498 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
2499 mmu_page_remove_parent_pte(child
, spte
);
2502 __set_spte(spte
, shadow_trap_nonpresent_pte
);
2503 if (is_large_pte(pte
))
2504 --vcpu
->kvm
->stat
.lpages
;
2507 static void mmu_pte_write_new_pte(struct kvm_vcpu
*vcpu
,
2508 struct kvm_mmu_page
*sp
,
2512 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
) {
2513 ++vcpu
->kvm
->stat
.mmu_pde_zapped
;
2517 ++vcpu
->kvm
->stat
.mmu_pte_updated
;
2518 if (!sp
->role
.cr4_pae
)
2519 paging32_update_pte(vcpu
, sp
, spte
, new);
2521 paging64_update_pte(vcpu
, sp
, spte
, new);
2524 static bool need_remote_flush(u64 old
, u64
new)
2526 if (!is_shadow_present_pte(old
))
2528 if (!is_shadow_present_pte(new))
2530 if ((old
^ new) & PT64_BASE_ADDR_MASK
)
2532 old
^= PT64_NX_MASK
;
2533 new ^= PT64_NX_MASK
;
2534 return (old
& ~new & PT64_PERM_MASK
) != 0;
2537 static void mmu_pte_write_flush_tlb(struct kvm_vcpu
*vcpu
, u64 old
, u64
new)
2539 if (need_remote_flush(old
, new))
2540 kvm_flush_remote_tlbs(vcpu
->kvm
);
2542 kvm_mmu_flush_tlb(vcpu
);
2545 static bool last_updated_pte_accessed(struct kvm_vcpu
*vcpu
)
2547 u64
*spte
= vcpu
->arch
.last_pte_updated
;
2549 return !!(spte
&& (*spte
& shadow_accessed_mask
));
2552 static void mmu_guess_page_from_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2558 if (!is_present_gpte(gpte
))
2560 gfn
= (gpte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
2562 vcpu
->arch
.update_pte
.mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
2564 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
2566 if (is_error_pfn(pfn
)) {
2567 kvm_release_pfn_clean(pfn
);
2570 vcpu
->arch
.update_pte
.gfn
= gfn
;
2571 vcpu
->arch
.update_pte
.pfn
= pfn
;
2574 static void kvm_mmu_access_page(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
2576 u64
*spte
= vcpu
->arch
.last_pte_updated
;
2579 && vcpu
->arch
.last_pte_gfn
== gfn
2580 && shadow_accessed_mask
2581 && !(*spte
& shadow_accessed_mask
)
2582 && is_shadow_present_pte(*spte
))
2583 set_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
2586 void kvm_mmu_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2587 const u8
*new, int bytes
,
2588 bool guest_initiated
)
2590 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
2591 struct kvm_mmu_page
*sp
;
2592 struct hlist_node
*node
, *n
;
2593 struct hlist_head
*bucket
;
2597 unsigned offset
= offset_in_page(gpa
);
2599 unsigned page_offset
;
2600 unsigned misaligned
;
2608 pgprintk("%s: gpa %llx bytes %d\n", __func__
, gpa
, bytes
);
2610 invlpg_counter
= atomic_read(&vcpu
->kvm
->arch
.invlpg_counter
);
2613 * Assume that the pte write on a page table of the same type
2614 * as the current vcpu paging mode. This is nearly always true
2615 * (might be false while changing modes). Note it is verified later
2618 if ((is_pae(vcpu
) && bytes
== 4) || !new) {
2619 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2624 r
= kvm_read_guest(vcpu
->kvm
, gpa
, &gentry
, min(bytes
, 8));
2627 new = (const u8
*)&gentry
;
2632 gentry
= *(const u32
*)new;
2635 gentry
= *(const u64
*)new;
2642 mmu_guess_page_from_pte_write(vcpu
, gpa
, gentry
);
2643 spin_lock(&vcpu
->kvm
->mmu_lock
);
2644 if (atomic_read(&vcpu
->kvm
->arch
.invlpg_counter
) != invlpg_counter
)
2646 kvm_mmu_access_page(vcpu
, gfn
);
2647 kvm_mmu_free_some_pages(vcpu
);
2648 ++vcpu
->kvm
->stat
.mmu_pte_write
;
2649 kvm_mmu_audit(vcpu
, "pre pte write");
2650 if (guest_initiated
) {
2651 if (gfn
== vcpu
->arch
.last_pt_write_gfn
2652 && !last_updated_pte_accessed(vcpu
)) {
2653 ++vcpu
->arch
.last_pt_write_count
;
2654 if (vcpu
->arch
.last_pt_write_count
>= 3)
2657 vcpu
->arch
.last_pt_write_gfn
= gfn
;
2658 vcpu
->arch
.last_pt_write_count
= 1;
2659 vcpu
->arch
.last_pte_updated
= NULL
;
2662 index
= kvm_page_table_hashfn(gfn
);
2663 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
2666 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
) {
2667 if (sp
->gfn
!= gfn
|| sp
->role
.direct
|| sp
->role
.invalid
)
2669 pte_size
= sp
->role
.cr4_pae
? 8 : 4;
2670 misaligned
= (offset
^ (offset
+ bytes
- 1)) & ~(pte_size
- 1);
2671 misaligned
|= bytes
< 4;
2672 if (misaligned
|| flooded
) {
2674 * Misaligned accesses are too much trouble to fix
2675 * up; also, they usually indicate a page is not used
2678 * If we're seeing too many writes to a page,
2679 * it may no longer be a page table, or we may be
2680 * forking, in which case it is better to unmap the
2683 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2684 gpa
, bytes
, sp
->role
.word
);
2685 if (kvm_mmu_zap_page(vcpu
->kvm
, sp
))
2687 ++vcpu
->kvm
->stat
.mmu_flooded
;
2690 page_offset
= offset
;
2691 level
= sp
->role
.level
;
2693 if (!sp
->role
.cr4_pae
) {
2694 page_offset
<<= 1; /* 32->64 */
2696 * A 32-bit pde maps 4MB while the shadow pdes map
2697 * only 2MB. So we need to double the offset again
2698 * and zap two pdes instead of one.
2700 if (level
== PT32_ROOT_LEVEL
) {
2701 page_offset
&= ~7; /* kill rounding error */
2705 quadrant
= page_offset
>> PAGE_SHIFT
;
2706 page_offset
&= ~PAGE_MASK
;
2707 if (quadrant
!= sp
->role
.quadrant
)
2710 spte
= &sp
->spt
[page_offset
/ sizeof(*spte
)];
2713 mmu_pte_write_zap_pte(vcpu
, sp
, spte
);
2715 mmu_pte_write_new_pte(vcpu
, sp
, spte
, &gentry
);
2716 mmu_pte_write_flush_tlb(vcpu
, entry
, *spte
);
2720 kvm_mmu_audit(vcpu
, "post pte write");
2721 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2722 if (!is_error_pfn(vcpu
->arch
.update_pte
.pfn
)) {
2723 kvm_release_pfn_clean(vcpu
->arch
.update_pte
.pfn
);
2724 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
2728 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu
*vcpu
, gva_t gva
)
2736 gpa
= kvm_mmu_gva_to_gpa_read(vcpu
, gva
, NULL
);
2738 spin_lock(&vcpu
->kvm
->mmu_lock
);
2739 r
= kvm_mmu_unprotect_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2740 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2743 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt
);
2745 void __kvm_mmu_free_some_pages(struct kvm_vcpu
*vcpu
)
2747 while (vcpu
->kvm
->arch
.n_free_mmu_pages
< KVM_REFILL_PAGES
&&
2748 !list_empty(&vcpu
->kvm
->arch
.active_mmu_pages
)) {
2749 struct kvm_mmu_page
*sp
;
2751 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.prev
,
2752 struct kvm_mmu_page
, link
);
2753 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2754 ++vcpu
->kvm
->stat
.mmu_recycled
;
2758 int kvm_mmu_page_fault(struct kvm_vcpu
*vcpu
, gva_t cr2
, u32 error_code
)
2761 enum emulation_result er
;
2763 r
= vcpu
->arch
.mmu
.page_fault(vcpu
, cr2
, error_code
);
2772 r
= mmu_topup_memory_caches(vcpu
);
2776 er
= emulate_instruction(vcpu
, cr2
, error_code
, 0);
2781 case EMULATE_DO_MMIO
:
2782 ++vcpu
->stat
.mmio_exits
;
2785 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
2786 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
2787 vcpu
->run
->internal
.ndata
= 0;
2795 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault
);
2797 void kvm_mmu_invlpg(struct kvm_vcpu
*vcpu
, gva_t gva
)
2799 vcpu
->arch
.mmu
.invlpg(vcpu
, gva
);
2800 kvm_mmu_flush_tlb(vcpu
);
2801 ++vcpu
->stat
.invlpg
;
2803 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg
);
2805 void kvm_enable_tdp(void)
2809 EXPORT_SYMBOL_GPL(kvm_enable_tdp
);
2811 void kvm_disable_tdp(void)
2813 tdp_enabled
= false;
2815 EXPORT_SYMBOL_GPL(kvm_disable_tdp
);
2817 static void free_mmu_pages(struct kvm_vcpu
*vcpu
)
2819 free_page((unsigned long)vcpu
->arch
.mmu
.pae_root
);
2822 static int alloc_mmu_pages(struct kvm_vcpu
*vcpu
)
2830 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2831 * Therefore we need to allocate shadow page tables in the first
2832 * 4GB of memory, which happens to fit the DMA32 zone.
2834 page
= alloc_page(GFP_KERNEL
| __GFP_DMA32
);
2838 vcpu
->arch
.mmu
.pae_root
= page_address(page
);
2839 for (i
= 0; i
< 4; ++i
)
2840 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
2845 int kvm_mmu_create(struct kvm_vcpu
*vcpu
)
2848 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2850 return alloc_mmu_pages(vcpu
);
2853 int kvm_mmu_setup(struct kvm_vcpu
*vcpu
)
2856 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2858 return init_kvm_mmu(vcpu
);
2861 void kvm_mmu_destroy(struct kvm_vcpu
*vcpu
)
2865 destroy_kvm_mmu(vcpu
);
2866 free_mmu_pages(vcpu
);
2867 mmu_free_memory_caches(vcpu
);
2870 void kvm_mmu_slot_remove_write_access(struct kvm
*kvm
, int slot
)
2872 struct kvm_mmu_page
*sp
;
2874 list_for_each_entry(sp
, &kvm
->arch
.active_mmu_pages
, link
) {
2878 if (!test_bit(slot
, sp
->slot_bitmap
))
2882 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
2884 if (pt
[i
] & PT_WRITABLE_MASK
)
2885 pt
[i
] &= ~PT_WRITABLE_MASK
;
2887 kvm_flush_remote_tlbs(kvm
);
2890 void kvm_mmu_zap_all(struct kvm
*kvm
)
2892 struct kvm_mmu_page
*sp
, *node
;
2894 spin_lock(&kvm
->mmu_lock
);
2896 list_for_each_entry_safe(sp
, node
, &kvm
->arch
.active_mmu_pages
, link
)
2897 if (kvm_mmu_zap_page(kvm
, sp
))
2900 spin_unlock(&kvm
->mmu_lock
);
2902 kvm_flush_remote_tlbs(kvm
);
2905 static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm
*kvm
)
2907 struct kvm_mmu_page
*page
;
2909 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
2910 struct kvm_mmu_page
, link
);
2911 return kvm_mmu_zap_page(kvm
, page
) + 1;
2914 static int mmu_shrink(int nr_to_scan
, gfp_t gfp_mask
)
2917 struct kvm
*kvm_freed
= NULL
;
2918 int cache_count
= 0;
2920 spin_lock(&kvm_lock
);
2922 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
2923 int npages
, idx
, freed_pages
;
2925 idx
= srcu_read_lock(&kvm
->srcu
);
2926 spin_lock(&kvm
->mmu_lock
);
2927 npages
= kvm
->arch
.n_alloc_mmu_pages
-
2928 kvm
->arch
.n_free_mmu_pages
;
2929 cache_count
+= npages
;
2930 if (!kvm_freed
&& nr_to_scan
> 0 && npages
> 0) {
2931 freed_pages
= kvm_mmu_remove_some_alloc_mmu_pages(kvm
);
2932 cache_count
-= freed_pages
;
2937 spin_unlock(&kvm
->mmu_lock
);
2938 srcu_read_unlock(&kvm
->srcu
, idx
);
2941 list_move_tail(&kvm_freed
->vm_list
, &vm_list
);
2943 spin_unlock(&kvm_lock
);
2948 static struct shrinker mmu_shrinker
= {
2949 .shrink
= mmu_shrink
,
2950 .seeks
= DEFAULT_SEEKS
* 10,
2953 static void mmu_destroy_caches(void)
2955 if (pte_chain_cache
)
2956 kmem_cache_destroy(pte_chain_cache
);
2957 if (rmap_desc_cache
)
2958 kmem_cache_destroy(rmap_desc_cache
);
2959 if (mmu_page_header_cache
)
2960 kmem_cache_destroy(mmu_page_header_cache
);
2963 void kvm_mmu_module_exit(void)
2965 mmu_destroy_caches();
2966 unregister_shrinker(&mmu_shrinker
);
2969 int kvm_mmu_module_init(void)
2971 pte_chain_cache
= kmem_cache_create("kvm_pte_chain",
2972 sizeof(struct kvm_pte_chain
),
2974 if (!pte_chain_cache
)
2976 rmap_desc_cache
= kmem_cache_create("kvm_rmap_desc",
2977 sizeof(struct kvm_rmap_desc
),
2979 if (!rmap_desc_cache
)
2982 mmu_page_header_cache
= kmem_cache_create("kvm_mmu_page_header",
2983 sizeof(struct kvm_mmu_page
),
2985 if (!mmu_page_header_cache
)
2988 register_shrinker(&mmu_shrinker
);
2993 mmu_destroy_caches();
2998 * Caculate mmu pages needed for kvm.
3000 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm
*kvm
)
3003 unsigned int nr_mmu_pages
;
3004 unsigned int nr_pages
= 0;
3005 struct kvm_memslots
*slots
;
3007 slots
= kvm_memslots(kvm
);
3009 for (i
= 0; i
< slots
->nmemslots
; i
++)
3010 nr_pages
+= slots
->memslots
[i
].npages
;
3012 nr_mmu_pages
= nr_pages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
3013 nr_mmu_pages
= max(nr_mmu_pages
,
3014 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
3016 return nr_mmu_pages
;
3019 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
3022 if (len
> buffer
->len
)
3027 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
3032 ret
= pv_mmu_peek_buffer(buffer
, len
);
3037 buffer
->processed
+= len
;
3041 static int kvm_pv_mmu_write(struct kvm_vcpu
*vcpu
,
3042 gpa_t addr
, gpa_t value
)
3047 if (!is_long_mode(vcpu
) && !is_pae(vcpu
))
3050 r
= mmu_topup_memory_caches(vcpu
);
3054 if (!emulator_write_phys(vcpu
, addr
, &value
, bytes
))
3060 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
3062 kvm_set_cr3(vcpu
, vcpu
->arch
.cr3
);
3066 static int kvm_pv_mmu_release_pt(struct kvm_vcpu
*vcpu
, gpa_t addr
)
3068 spin_lock(&vcpu
->kvm
->mmu_lock
);
3069 mmu_unshadow(vcpu
->kvm
, addr
>> PAGE_SHIFT
);
3070 spin_unlock(&vcpu
->kvm
->mmu_lock
);
3074 static int kvm_pv_mmu_op_one(struct kvm_vcpu
*vcpu
,
3075 struct kvm_pv_mmu_op_buffer
*buffer
)
3077 struct kvm_mmu_op_header
*header
;
3079 header
= pv_mmu_peek_buffer(buffer
, sizeof *header
);
3082 switch (header
->op
) {
3083 case KVM_MMU_OP_WRITE_PTE
: {
3084 struct kvm_mmu_op_write_pte
*wpte
;
3086 wpte
= pv_mmu_read_buffer(buffer
, sizeof *wpte
);
3089 return kvm_pv_mmu_write(vcpu
, wpte
->pte_phys
,
3092 case KVM_MMU_OP_FLUSH_TLB
: {
3093 struct kvm_mmu_op_flush_tlb
*ftlb
;
3095 ftlb
= pv_mmu_read_buffer(buffer
, sizeof *ftlb
);
3098 return kvm_pv_mmu_flush_tlb(vcpu
);
3100 case KVM_MMU_OP_RELEASE_PT
: {
3101 struct kvm_mmu_op_release_pt
*rpt
;
3103 rpt
= pv_mmu_read_buffer(buffer
, sizeof *rpt
);
3106 return kvm_pv_mmu_release_pt(vcpu
, rpt
->pt_phys
);
3112 int kvm_pv_mmu_op(struct kvm_vcpu
*vcpu
, unsigned long bytes
,
3113 gpa_t addr
, unsigned long *ret
)
3116 struct kvm_pv_mmu_op_buffer
*buffer
= &vcpu
->arch
.mmu_op_buffer
;
3118 buffer
->ptr
= buffer
->buf
;
3119 buffer
->len
= min_t(unsigned long, bytes
, sizeof buffer
->buf
);
3120 buffer
->processed
= 0;
3122 r
= kvm_read_guest(vcpu
->kvm
, addr
, buffer
->buf
, buffer
->len
);
3126 while (buffer
->len
) {
3127 r
= kvm_pv_mmu_op_one(vcpu
, buffer
);
3136 *ret
= buffer
->processed
;
3140 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu
*vcpu
, u64 addr
, u64 sptes
[4])
3142 struct kvm_shadow_walk_iterator iterator
;
3145 spin_lock(&vcpu
->kvm
->mmu_lock
);
3146 for_each_shadow_entry(vcpu
, addr
, iterator
) {
3147 sptes
[iterator
.level
-1] = *iterator
.sptep
;
3149 if (!is_shadow_present_pte(*iterator
.sptep
))
3152 spin_unlock(&vcpu
->kvm
->mmu_lock
);
3156 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy
);
3160 static const char *audit_msg
;
3162 static gva_t
canonicalize(gva_t gva
)
3164 #ifdef CONFIG_X86_64
3165 gva
= (long long)(gva
<< 16) >> 16;
3171 typedef void (*inspect_spte_fn
) (struct kvm
*kvm
, u64
*sptep
);
3173 static void __mmu_spte_walk(struct kvm
*kvm
, struct kvm_mmu_page
*sp
,
3178 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
3179 u64 ent
= sp
->spt
[i
];
3181 if (is_shadow_present_pte(ent
)) {
3182 if (!is_last_spte(ent
, sp
->role
.level
)) {
3183 struct kvm_mmu_page
*child
;
3184 child
= page_header(ent
& PT64_BASE_ADDR_MASK
);
3185 __mmu_spte_walk(kvm
, child
, fn
);
3187 fn(kvm
, &sp
->spt
[i
]);
3192 static void mmu_spte_walk(struct kvm_vcpu
*vcpu
, inspect_spte_fn fn
)
3195 struct kvm_mmu_page
*sp
;
3197 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
3199 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
3200 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
3201 sp
= page_header(root
);
3202 __mmu_spte_walk(vcpu
->kvm
, sp
, fn
);
3205 for (i
= 0; i
< 4; ++i
) {
3206 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
3208 if (root
&& VALID_PAGE(root
)) {
3209 root
&= PT64_BASE_ADDR_MASK
;
3210 sp
= page_header(root
);
3211 __mmu_spte_walk(vcpu
->kvm
, sp
, fn
);
3217 static void audit_mappings_page(struct kvm_vcpu
*vcpu
, u64 page_pte
,
3218 gva_t va
, int level
)
3220 u64
*pt
= __va(page_pte
& PT64_BASE_ADDR_MASK
);
3222 gva_t va_delta
= 1ul << (PAGE_SHIFT
+ 9 * (level
- 1));
3224 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
, va
+= va_delta
) {
3227 if (ent
== shadow_trap_nonpresent_pte
)
3230 va
= canonicalize(va
);
3231 if (is_shadow_present_pte(ent
) && !is_last_spte(ent
, level
))
3232 audit_mappings_page(vcpu
, ent
, va
, level
- 1);
3234 gpa_t gpa
= kvm_mmu_gva_to_gpa_read(vcpu
, va
, NULL
);
3235 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
3236 pfn_t pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
3237 hpa_t hpa
= (hpa_t
)pfn
<< PAGE_SHIFT
;
3239 if (is_error_pfn(pfn
)) {
3240 kvm_release_pfn_clean(pfn
);
3244 if (is_shadow_present_pte(ent
)
3245 && (ent
& PT64_BASE_ADDR_MASK
) != hpa
)
3246 printk(KERN_ERR
"xx audit error: (%s) levels %d"
3247 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3248 audit_msg
, vcpu
->arch
.mmu
.root_level
,
3250 is_shadow_present_pte(ent
));
3251 else if (ent
== shadow_notrap_nonpresent_pte
3252 && !is_error_hpa(hpa
))
3253 printk(KERN_ERR
"audit: (%s) notrap shadow,"
3254 " valid guest gva %lx\n", audit_msg
, va
);
3255 kvm_release_pfn_clean(pfn
);
3261 static void audit_mappings(struct kvm_vcpu
*vcpu
)
3265 if (vcpu
->arch
.mmu
.root_level
== 4)
3266 audit_mappings_page(vcpu
, vcpu
->arch
.mmu
.root_hpa
, 0, 4);
3268 for (i
= 0; i
< 4; ++i
)
3269 if (vcpu
->arch
.mmu
.pae_root
[i
] & PT_PRESENT_MASK
)
3270 audit_mappings_page(vcpu
,
3271 vcpu
->arch
.mmu
.pae_root
[i
],
3276 static int count_rmaps(struct kvm_vcpu
*vcpu
)
3278 struct kvm
*kvm
= vcpu
->kvm
;
3279 struct kvm_memslots
*slots
;
3283 idx
= srcu_read_lock(&kvm
->srcu
);
3284 slots
= kvm_memslots(kvm
);
3285 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
3286 struct kvm_memory_slot
*m
= &slots
->memslots
[i
];
3287 struct kvm_rmap_desc
*d
;
3289 for (j
= 0; j
< m
->npages
; ++j
) {
3290 unsigned long *rmapp
= &m
->rmap
[j
];
3294 if (!(*rmapp
& 1)) {
3298 d
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
3300 for (k
= 0; k
< RMAP_EXT
; ++k
)
3309 srcu_read_unlock(&kvm
->srcu
, idx
);
3313 void inspect_spte_has_rmap(struct kvm
*kvm
, u64
*sptep
)
3315 unsigned long *rmapp
;
3316 struct kvm_mmu_page
*rev_sp
;
3319 if (*sptep
& PT_WRITABLE_MASK
) {
3320 rev_sp
= page_header(__pa(sptep
));
3321 gfn
= rev_sp
->gfns
[sptep
- rev_sp
->spt
];
3323 if (!gfn_to_memslot(kvm
, gfn
)) {
3324 if (!printk_ratelimit())
3326 printk(KERN_ERR
"%s: no memslot for gfn %ld\n",
3328 printk(KERN_ERR
"%s: index %ld of sp (gfn=%lx)\n",
3329 audit_msg
, (long int)(sptep
- rev_sp
->spt
),
3335 rmapp
= gfn_to_rmap(kvm
, rev_sp
->gfns
[sptep
- rev_sp
->spt
],
3336 rev_sp
->role
.level
);
3338 if (!printk_ratelimit())
3340 printk(KERN_ERR
"%s: no rmap for writable spte %llx\n",
3348 void audit_writable_sptes_have_rmaps(struct kvm_vcpu
*vcpu
)
3350 mmu_spte_walk(vcpu
, inspect_spte_has_rmap
);
3353 static void check_writable_mappings_rmap(struct kvm_vcpu
*vcpu
)
3355 struct kvm_mmu_page
*sp
;
3358 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
3361 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
3364 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
3367 if (!(ent
& PT_PRESENT_MASK
))
3369 if (!(ent
& PT_WRITABLE_MASK
))
3371 inspect_spte_has_rmap(vcpu
->kvm
, &pt
[i
]);
3377 static void audit_rmap(struct kvm_vcpu
*vcpu
)
3379 check_writable_mappings_rmap(vcpu
);
3383 static void audit_write_protection(struct kvm_vcpu
*vcpu
)
3385 struct kvm_mmu_page
*sp
;
3386 struct kvm_memory_slot
*slot
;
3387 unsigned long *rmapp
;
3391 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
3392 if (sp
->role
.direct
)
3397 gfn
= unalias_gfn(vcpu
->kvm
, sp
->gfn
);
3398 slot
= gfn_to_memslot_unaliased(vcpu
->kvm
, sp
->gfn
);
3399 rmapp
= &slot
->rmap
[gfn
- slot
->base_gfn
];
3401 spte
= rmap_next(vcpu
->kvm
, rmapp
, NULL
);
3403 if (*spte
& PT_WRITABLE_MASK
)
3404 printk(KERN_ERR
"%s: (%s) shadow page has "
3405 "writable mappings: gfn %lx role %x\n",
3406 __func__
, audit_msg
, sp
->gfn
,
3408 spte
= rmap_next(vcpu
->kvm
, rmapp
, spte
);
3413 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
)
3420 audit_write_protection(vcpu
);
3421 if (strcmp("pre pte write", audit_msg
) != 0)
3422 audit_mappings(vcpu
);
3423 audit_writable_sptes_have_rmaps(vcpu
);