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>
35 #include <linux/uaccess.h>
38 #include <asm/cmpxchg.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;
56 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
);
58 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
) {}
63 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
64 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
68 #define pgprintk(x...) do { } while (0)
69 #define rmap_printk(x...) do { } while (0)
73 #if defined(MMU_DEBUG) || defined(AUDIT)
75 module_param(dbg
, bool, 0644);
78 static int oos_shadow
= 1;
79 module_param(oos_shadow
, bool, 0644);
82 #define ASSERT(x) do { } while (0)
86 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
87 __FILE__, __LINE__, #x); \
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 \
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
{
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)
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
)
266 if (is_large_pte(pte
))
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
)
286 set_64bit((unsigned long *)sptep
, spte
);
288 set_64bit((unsigned long long *)sptep
, spte
);
292 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
293 struct kmem_cache
*base_cache
, int min
)
297 if (cache
->nobjs
>= min
)
299 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
300 obj
= kmem_cache_zalloc(base_cache
, GFP_KERNEL
);
303 cache
->objects
[cache
->nobjs
++] = obj
;
308 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
,
309 struct kmem_cache
*cache
)
312 kmem_cache_free(cache
, mc
->objects
[--mc
->nobjs
]);
315 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache
*cache
,
320 if (cache
->nobjs
>= min
)
322 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
323 page
= alloc_page(GFP_KERNEL
);
326 cache
->objects
[cache
->nobjs
++] = page_address(page
);
331 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache
*mc
)
334 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
337 static int mmu_topup_memory_caches(struct kvm_vcpu
*vcpu
)
341 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
,
345 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
,
349 r
= mmu_topup_memory_cache_page(&vcpu
->arch
.mmu_page_cache
, 8);
352 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_page_header_cache
,
353 mmu_page_header_cache
, 4);
358 static void mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
360 mmu_free_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
, pte_chain_cache
);
361 mmu_free_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
, rmap_desc_cache
);
362 mmu_free_memory_cache_page(&vcpu
->arch
.mmu_page_cache
);
363 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_header_cache
,
364 mmu_page_header_cache
);
367 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
,
373 p
= mc
->objects
[--mc
->nobjs
];
377 static struct kvm_pte_chain
*mmu_alloc_pte_chain(struct kvm_vcpu
*vcpu
)
379 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_pte_chain_cache
,
380 sizeof(struct kvm_pte_chain
));
383 static void mmu_free_pte_chain(struct kvm_pte_chain
*pc
)
385 kmem_cache_free(pte_chain_cache
, pc
);
388 static struct kvm_rmap_desc
*mmu_alloc_rmap_desc(struct kvm_vcpu
*vcpu
)
390 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_rmap_desc_cache
,
391 sizeof(struct kvm_rmap_desc
));
394 static void mmu_free_rmap_desc(struct kvm_rmap_desc
*rd
)
396 kmem_cache_free(rmap_desc_cache
, rd
);
400 * Return the pointer to the largepage write count for a given
401 * gfn, handling slots that are not large page aligned.
403 static int *slot_largepage_idx(gfn_t gfn
,
404 struct kvm_memory_slot
*slot
,
409 idx
= (gfn
/ KVM_PAGES_PER_HPAGE(level
)) -
410 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE(level
));
411 return &slot
->lpage_info
[level
- 2][idx
].write_count
;
414 static void account_shadowed(struct kvm
*kvm
, gfn_t gfn
)
416 struct kvm_memory_slot
*slot
;
420 gfn
= unalias_gfn(kvm
, gfn
);
422 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
423 for (i
= PT_DIRECTORY_LEVEL
;
424 i
< PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
; ++i
) {
425 write_count
= slot_largepage_idx(gfn
, slot
, i
);
430 static void unaccount_shadowed(struct kvm
*kvm
, gfn_t gfn
)
432 struct kvm_memory_slot
*slot
;
436 gfn
= unalias_gfn(kvm
, gfn
);
437 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
438 for (i
= PT_DIRECTORY_LEVEL
;
439 i
< PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
; ++i
) {
440 write_count
= slot_largepage_idx(gfn
, slot
, i
);
442 WARN_ON(*write_count
< 0);
446 static int has_wrprotected_page(struct kvm
*kvm
,
450 struct kvm_memory_slot
*slot
;
453 gfn
= unalias_gfn(kvm
, gfn
);
454 slot
= gfn_to_memslot_unaliased(kvm
, gfn
);
456 largepage_idx
= slot_largepage_idx(gfn
, slot
, level
);
457 return *largepage_idx
;
463 static int host_mapping_level(struct kvm
*kvm
, gfn_t gfn
)
465 unsigned long page_size
;
468 page_size
= kvm_host_page_size(kvm
, gfn
);
470 for (i
= PT_PAGE_TABLE_LEVEL
;
471 i
< (PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
); ++i
) {
472 if (page_size
>= KVM_HPAGE_SIZE(i
))
481 static int mapping_level(struct kvm_vcpu
*vcpu
, gfn_t large_gfn
)
483 struct kvm_memory_slot
*slot
;
484 int host_level
, level
, max_level
;
486 slot
= gfn_to_memslot(vcpu
->kvm
, large_gfn
);
487 if (slot
&& slot
->dirty_bitmap
)
488 return PT_PAGE_TABLE_LEVEL
;
490 host_level
= host_mapping_level(vcpu
->kvm
, large_gfn
);
492 if (host_level
== PT_PAGE_TABLE_LEVEL
)
495 max_level
= kvm_x86_ops
->get_lpage_level() < host_level
?
496 kvm_x86_ops
->get_lpage_level() : host_level
;
498 for (level
= PT_DIRECTORY_LEVEL
; level
<= max_level
; ++level
)
499 if (has_wrprotected_page(vcpu
->kvm
, large_gfn
, level
))
506 * Take gfn and return the reverse mapping to it.
507 * Note: gfn must be unaliased before this function get called
510 static unsigned long *gfn_to_rmap(struct kvm
*kvm
, gfn_t gfn
, int level
)
512 struct kvm_memory_slot
*slot
;
515 slot
= gfn_to_memslot(kvm
, gfn
);
516 if (likely(level
== PT_PAGE_TABLE_LEVEL
))
517 return &slot
->rmap
[gfn
- slot
->base_gfn
];
519 idx
= (gfn
/ KVM_PAGES_PER_HPAGE(level
)) -
520 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE(level
));
522 return &slot
->lpage_info
[level
- 2][idx
].rmap_pde
;
526 * Reverse mapping data structures:
528 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
529 * that points to page_address(page).
531 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
532 * containing more mappings.
534 * Returns the number of rmap entries before the spte was added or zero if
535 * the spte was not added.
538 static int rmap_add(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
)
540 struct kvm_mmu_page
*sp
;
541 struct kvm_rmap_desc
*desc
;
542 unsigned long *rmapp
;
545 if (!is_rmap_spte(*spte
))
547 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
548 sp
= page_header(__pa(spte
));
549 sp
->gfns
[spte
- sp
->spt
] = gfn
;
550 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
, sp
->role
.level
);
552 rmap_printk("rmap_add: %p %llx 0->1\n", spte
, *spte
);
553 *rmapp
= (unsigned long)spte
;
554 } else if (!(*rmapp
& 1)) {
555 rmap_printk("rmap_add: %p %llx 1->many\n", spte
, *spte
);
556 desc
= mmu_alloc_rmap_desc(vcpu
);
557 desc
->sptes
[0] = (u64
*)*rmapp
;
558 desc
->sptes
[1] = spte
;
559 *rmapp
= (unsigned long)desc
| 1;
561 rmap_printk("rmap_add: %p %llx many->many\n", spte
, *spte
);
562 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
563 while (desc
->sptes
[RMAP_EXT
-1] && desc
->more
) {
567 if (desc
->sptes
[RMAP_EXT
-1]) {
568 desc
->more
= mmu_alloc_rmap_desc(vcpu
);
571 for (i
= 0; desc
->sptes
[i
]; ++i
)
573 desc
->sptes
[i
] = spte
;
578 static void rmap_desc_remove_entry(unsigned long *rmapp
,
579 struct kvm_rmap_desc
*desc
,
581 struct kvm_rmap_desc
*prev_desc
)
585 for (j
= RMAP_EXT
- 1; !desc
->sptes
[j
] && j
> i
; --j
)
587 desc
->sptes
[i
] = desc
->sptes
[j
];
588 desc
->sptes
[j
] = NULL
;
591 if (!prev_desc
&& !desc
->more
)
592 *rmapp
= (unsigned long)desc
->sptes
[0];
595 prev_desc
->more
= desc
->more
;
597 *rmapp
= (unsigned long)desc
->more
| 1;
598 mmu_free_rmap_desc(desc
);
601 static void rmap_remove(struct kvm
*kvm
, u64
*spte
)
603 struct kvm_rmap_desc
*desc
;
604 struct kvm_rmap_desc
*prev_desc
;
605 struct kvm_mmu_page
*sp
;
607 unsigned long *rmapp
;
610 if (!is_rmap_spte(*spte
))
612 sp
= page_header(__pa(spte
));
613 pfn
= spte_to_pfn(*spte
);
614 if (*spte
& shadow_accessed_mask
)
615 kvm_set_pfn_accessed(pfn
);
616 if (is_writable_pte(*spte
))
617 kvm_set_pfn_dirty(pfn
);
618 rmapp
= gfn_to_rmap(kvm
, sp
->gfns
[spte
- sp
->spt
], sp
->role
.level
);
620 printk(KERN_ERR
"rmap_remove: %p %llx 0->BUG\n", spte
, *spte
);
622 } else if (!(*rmapp
& 1)) {
623 rmap_printk("rmap_remove: %p %llx 1->0\n", spte
, *spte
);
624 if ((u64
*)*rmapp
!= spte
) {
625 printk(KERN_ERR
"rmap_remove: %p %llx 1->BUG\n",
631 rmap_printk("rmap_remove: %p %llx many->many\n", spte
, *spte
);
632 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
635 for (i
= 0; i
< RMAP_EXT
&& desc
->sptes
[i
]; ++i
)
636 if (desc
->sptes
[i
] == spte
) {
637 rmap_desc_remove_entry(rmapp
,
645 pr_err("rmap_remove: %p %llx many->many\n", spte
, *spte
);
650 static u64
*rmap_next(struct kvm
*kvm
, unsigned long *rmapp
, u64
*spte
)
652 struct kvm_rmap_desc
*desc
;
658 else if (!(*rmapp
& 1)) {
660 return (u64
*)*rmapp
;
663 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
666 for (i
= 0; i
< RMAP_EXT
&& desc
->sptes
[i
]; ++i
) {
667 if (prev_spte
== spte
)
668 return desc
->sptes
[i
];
669 prev_spte
= desc
->sptes
[i
];
676 static int rmap_write_protect(struct kvm
*kvm
, u64 gfn
)
678 unsigned long *rmapp
;
680 int i
, write_protected
= 0;
682 gfn
= unalias_gfn(kvm
, gfn
);
683 rmapp
= gfn_to_rmap(kvm
, gfn
, PT_PAGE_TABLE_LEVEL
);
685 spte
= rmap_next(kvm
, rmapp
, NULL
);
688 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
689 rmap_printk("rmap_write_protect: spte %p %llx\n", spte
, *spte
);
690 if (is_writable_pte(*spte
)) {
691 __set_spte(spte
, *spte
& ~PT_WRITABLE_MASK
);
694 spte
= rmap_next(kvm
, rmapp
, spte
);
696 if (write_protected
) {
699 spte
= rmap_next(kvm
, rmapp
, NULL
);
700 pfn
= spte_to_pfn(*spte
);
701 kvm_set_pfn_dirty(pfn
);
704 /* check for huge page mappings */
705 for (i
= PT_DIRECTORY_LEVEL
;
706 i
< PT_PAGE_TABLE_LEVEL
+ KVM_NR_PAGE_SIZES
; ++i
) {
707 rmapp
= gfn_to_rmap(kvm
, gfn
, i
);
708 spte
= rmap_next(kvm
, rmapp
, NULL
);
711 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
712 BUG_ON((*spte
& (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
)) != (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
));
713 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte
, *spte
, gfn
);
714 if (is_writable_pte(*spte
)) {
715 rmap_remove(kvm
, spte
);
717 __set_spte(spte
, shadow_trap_nonpresent_pte
);
721 spte
= rmap_next(kvm
, rmapp
, spte
);
725 return write_protected
;
728 static int kvm_unmap_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
732 int need_tlb_flush
= 0;
734 while ((spte
= rmap_next(kvm
, rmapp
, NULL
))) {
735 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
736 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte
, *spte
);
737 rmap_remove(kvm
, spte
);
738 __set_spte(spte
, shadow_trap_nonpresent_pte
);
741 return need_tlb_flush
;
744 static int kvm_set_pte_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
749 pte_t
*ptep
= (pte_t
*)data
;
752 WARN_ON(pte_huge(*ptep
));
753 new_pfn
= pte_pfn(*ptep
);
754 spte
= rmap_next(kvm
, rmapp
, NULL
);
756 BUG_ON(!is_shadow_present_pte(*spte
));
757 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte
, *spte
);
759 if (pte_write(*ptep
)) {
760 rmap_remove(kvm
, spte
);
761 __set_spte(spte
, shadow_trap_nonpresent_pte
);
762 spte
= rmap_next(kvm
, rmapp
, NULL
);
764 new_spte
= *spte
&~ (PT64_BASE_ADDR_MASK
);
765 new_spte
|= (u64
)new_pfn
<< PAGE_SHIFT
;
767 new_spte
&= ~PT_WRITABLE_MASK
;
768 new_spte
&= ~SPTE_HOST_WRITEABLE
;
769 if (is_writable_pte(*spte
))
770 kvm_set_pfn_dirty(spte_to_pfn(*spte
));
771 __set_spte(spte
, new_spte
);
772 spte
= rmap_next(kvm
, rmapp
, spte
);
776 kvm_flush_remote_tlbs(kvm
);
781 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
783 int (*handler
)(struct kvm
*kvm
, unsigned long *rmapp
,
789 struct kvm_memslots
*slots
;
791 slots
= kvm_memslots(kvm
);
793 for (i
= 0; i
< slots
->nmemslots
; i
++) {
794 struct kvm_memory_slot
*memslot
= &slots
->memslots
[i
];
795 unsigned long start
= memslot
->userspace_addr
;
798 end
= start
+ (memslot
->npages
<< PAGE_SHIFT
);
799 if (hva
>= start
&& hva
< end
) {
800 gfn_t gfn_offset
= (hva
- start
) >> PAGE_SHIFT
;
802 ret
= handler(kvm
, &memslot
->rmap
[gfn_offset
], data
);
804 for (j
= 0; j
< KVM_NR_PAGE_SIZES
- 1; ++j
) {
805 int idx
= gfn_offset
;
806 idx
/= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL
+ j
);
808 &memslot
->lpage_info
[j
][idx
].rmap_pde
,
811 trace_kvm_age_page(hva
, memslot
, ret
);
819 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
821 return kvm_handle_hva(kvm
, hva
, 0, kvm_unmap_rmapp
);
824 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
826 kvm_handle_hva(kvm
, hva
, (unsigned long)&pte
, kvm_set_pte_rmapp
);
829 static int kvm_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
836 * Emulate the accessed bit for EPT, by checking if this page has
837 * an EPT mapping, and clearing it if it does. On the next access,
838 * a new EPT mapping will be established.
839 * This has some overhead, but not as much as the cost of swapping
840 * out actively used pages or breaking up actively used hugepages.
842 if (!shadow_accessed_mask
)
843 return kvm_unmap_rmapp(kvm
, rmapp
, data
);
845 spte
= rmap_next(kvm
, rmapp
, NULL
);
849 BUG_ON(!(_spte
& PT_PRESENT_MASK
));
850 _young
= _spte
& PT_ACCESSED_MASK
;
853 clear_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
855 spte
= rmap_next(kvm
, rmapp
, spte
);
860 #define RMAP_RECYCLE_THRESHOLD 1000
862 static void rmap_recycle(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
)
864 unsigned long *rmapp
;
865 struct kvm_mmu_page
*sp
;
867 sp
= page_header(__pa(spte
));
869 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
870 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
, sp
->role
.level
);
872 kvm_unmap_rmapp(vcpu
->kvm
, rmapp
, 0);
873 kvm_flush_remote_tlbs(vcpu
->kvm
);
876 int kvm_age_hva(struct kvm
*kvm
, unsigned long hva
)
878 return kvm_handle_hva(kvm
, hva
, 0, kvm_age_rmapp
);
882 static int is_empty_shadow_page(u64
*spt
)
887 for (pos
= spt
, end
= pos
+ PAGE_SIZE
/ sizeof(u64
); pos
!= end
; pos
++)
888 if (is_shadow_present_pte(*pos
)) {
889 printk(KERN_ERR
"%s: %p %llx\n", __func__
,
897 static void kvm_mmu_free_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
899 ASSERT(is_empty_shadow_page(sp
->spt
));
901 __free_page(virt_to_page(sp
->spt
));
902 __free_page(virt_to_page(sp
->gfns
));
903 kmem_cache_free(mmu_page_header_cache
, sp
);
904 ++kvm
->arch
.n_free_mmu_pages
;
907 static unsigned kvm_page_table_hashfn(gfn_t gfn
)
909 return gfn
& ((1 << KVM_MMU_HASH_SHIFT
) - 1);
912 static struct kvm_mmu_page
*kvm_mmu_alloc_page(struct kvm_vcpu
*vcpu
,
915 struct kvm_mmu_page
*sp
;
917 sp
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_header_cache
, sizeof *sp
);
918 sp
->spt
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
919 sp
->gfns
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
920 set_page_private(virt_to_page(sp
->spt
), (unsigned long)sp
);
921 list_add(&sp
->link
, &vcpu
->kvm
->arch
.active_mmu_pages
);
922 bitmap_zero(sp
->slot_bitmap
, KVM_MEMORY_SLOTS
+ KVM_PRIVATE_MEM_SLOTS
);
924 sp
->parent_pte
= parent_pte
;
925 --vcpu
->kvm
->arch
.n_free_mmu_pages
;
929 static void mmu_page_add_parent_pte(struct kvm_vcpu
*vcpu
,
930 struct kvm_mmu_page
*sp
, u64
*parent_pte
)
932 struct kvm_pte_chain
*pte_chain
;
933 struct hlist_node
*node
;
938 if (!sp
->multimapped
) {
939 u64
*old
= sp
->parent_pte
;
942 sp
->parent_pte
= parent_pte
;
946 pte_chain
= mmu_alloc_pte_chain(vcpu
);
947 INIT_HLIST_HEAD(&sp
->parent_ptes
);
948 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
949 pte_chain
->parent_ptes
[0] = old
;
951 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
) {
952 if (pte_chain
->parent_ptes
[NR_PTE_CHAIN_ENTRIES
-1])
954 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
)
955 if (!pte_chain
->parent_ptes
[i
]) {
956 pte_chain
->parent_ptes
[i
] = parent_pte
;
960 pte_chain
= mmu_alloc_pte_chain(vcpu
);
962 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
963 pte_chain
->parent_ptes
[0] = parent_pte
;
966 static void mmu_page_remove_parent_pte(struct kvm_mmu_page
*sp
,
969 struct kvm_pte_chain
*pte_chain
;
970 struct hlist_node
*node
;
973 if (!sp
->multimapped
) {
974 BUG_ON(sp
->parent_pte
!= parent_pte
);
975 sp
->parent_pte
= NULL
;
978 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
979 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
980 if (!pte_chain
->parent_ptes
[i
])
982 if (pte_chain
->parent_ptes
[i
] != parent_pte
)
984 while (i
+ 1 < NR_PTE_CHAIN_ENTRIES
985 && pte_chain
->parent_ptes
[i
+ 1]) {
986 pte_chain
->parent_ptes
[i
]
987 = pte_chain
->parent_ptes
[i
+ 1];
990 pte_chain
->parent_ptes
[i
] = NULL
;
992 hlist_del(&pte_chain
->link
);
993 mmu_free_pte_chain(pte_chain
);
994 if (hlist_empty(&sp
->parent_ptes
)) {
996 sp
->parent_pte
= NULL
;
1005 static void mmu_parent_walk(struct kvm_mmu_page
*sp
, mmu_parent_walk_fn fn
)
1007 struct kvm_pte_chain
*pte_chain
;
1008 struct hlist_node
*node
;
1009 struct kvm_mmu_page
*parent_sp
;
1012 if (!sp
->multimapped
&& sp
->parent_pte
) {
1013 parent_sp
= page_header(__pa(sp
->parent_pte
));
1015 mmu_parent_walk(parent_sp
, fn
);
1018 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
1019 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
1020 if (!pte_chain
->parent_ptes
[i
])
1022 parent_sp
= page_header(__pa(pte_chain
->parent_ptes
[i
]));
1024 mmu_parent_walk(parent_sp
, fn
);
1028 static void kvm_mmu_update_unsync_bitmap(u64
*spte
)
1031 struct kvm_mmu_page
*sp
= page_header(__pa(spte
));
1033 index
= spte
- sp
->spt
;
1034 if (!__test_and_set_bit(index
, sp
->unsync_child_bitmap
))
1035 sp
->unsync_children
++;
1036 WARN_ON(!sp
->unsync_children
);
1039 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page
*sp
)
1041 struct kvm_pte_chain
*pte_chain
;
1042 struct hlist_node
*node
;
1045 if (!sp
->parent_pte
)
1048 if (!sp
->multimapped
) {
1049 kvm_mmu_update_unsync_bitmap(sp
->parent_pte
);
1053 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
1054 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
1055 if (!pte_chain
->parent_ptes
[i
])
1057 kvm_mmu_update_unsync_bitmap(pte_chain
->parent_ptes
[i
]);
1061 static int unsync_walk_fn(struct kvm_mmu_page
*sp
)
1063 kvm_mmu_update_parents_unsync(sp
);
1067 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page
*sp
)
1069 mmu_parent_walk(sp
, unsync_walk_fn
);
1070 kvm_mmu_update_parents_unsync(sp
);
1073 static void nonpaging_prefetch_page(struct kvm_vcpu
*vcpu
,
1074 struct kvm_mmu_page
*sp
)
1078 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
1079 sp
->spt
[i
] = shadow_trap_nonpresent_pte
;
1082 static int nonpaging_sync_page(struct kvm_vcpu
*vcpu
,
1083 struct kvm_mmu_page
*sp
)
1088 static void nonpaging_invlpg(struct kvm_vcpu
*vcpu
, gva_t gva
)
1092 #define KVM_PAGE_ARRAY_NR 16
1094 struct kvm_mmu_pages
{
1095 struct mmu_page_and_offset
{
1096 struct kvm_mmu_page
*sp
;
1098 } page
[KVM_PAGE_ARRAY_NR
];
1102 #define for_each_unsync_children(bitmap, idx) \
1103 for (idx = find_first_bit(bitmap, 512); \
1105 idx = find_next_bit(bitmap, 512, idx+1))
1107 static int mmu_pages_add(struct kvm_mmu_pages
*pvec
, struct kvm_mmu_page
*sp
,
1113 for (i
=0; i
< pvec
->nr
; i
++)
1114 if (pvec
->page
[i
].sp
== sp
)
1117 pvec
->page
[pvec
->nr
].sp
= sp
;
1118 pvec
->page
[pvec
->nr
].idx
= idx
;
1120 return (pvec
->nr
== KVM_PAGE_ARRAY_NR
);
1123 static int __mmu_unsync_walk(struct kvm_mmu_page
*sp
,
1124 struct kvm_mmu_pages
*pvec
)
1126 int i
, ret
, nr_unsync_leaf
= 0;
1128 for_each_unsync_children(sp
->unsync_child_bitmap
, i
) {
1129 u64 ent
= sp
->spt
[i
];
1131 if (is_shadow_present_pte(ent
) && !is_large_pte(ent
)) {
1132 struct kvm_mmu_page
*child
;
1133 child
= page_header(ent
& PT64_BASE_ADDR_MASK
);
1135 if (child
->unsync_children
) {
1136 if (mmu_pages_add(pvec
, child
, i
))
1139 ret
= __mmu_unsync_walk(child
, pvec
);
1141 __clear_bit(i
, sp
->unsync_child_bitmap
);
1143 nr_unsync_leaf
+= ret
;
1148 if (child
->unsync
) {
1150 if (mmu_pages_add(pvec
, child
, i
))
1156 if (find_first_bit(sp
->unsync_child_bitmap
, 512) == 512)
1157 sp
->unsync_children
= 0;
1159 return nr_unsync_leaf
;
1162 static int mmu_unsync_walk(struct kvm_mmu_page
*sp
,
1163 struct kvm_mmu_pages
*pvec
)
1165 if (!sp
->unsync_children
)
1168 mmu_pages_add(pvec
, sp
, 0);
1169 return __mmu_unsync_walk(sp
, pvec
);
1172 static struct kvm_mmu_page
*kvm_mmu_lookup_page(struct kvm
*kvm
, gfn_t gfn
)
1175 struct hlist_head
*bucket
;
1176 struct kvm_mmu_page
*sp
;
1177 struct hlist_node
*node
;
1179 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1180 index
= kvm_page_table_hashfn(gfn
);
1181 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1182 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
1183 if (sp
->gfn
== gfn
&& !sp
->role
.direct
1184 && !sp
->role
.invalid
) {
1185 pgprintk("%s: found role %x\n",
1186 __func__
, sp
->role
.word
);
1192 static void kvm_unlink_unsync_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1194 WARN_ON(!sp
->unsync
);
1195 trace_kvm_mmu_sync_page(sp
);
1197 --kvm
->stat
.mmu_unsync
;
1200 static int kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
);
1202 static int kvm_sync_page(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1204 if (sp
->role
.cr4_pae
!= !!is_pae(vcpu
)) {
1205 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1209 if (rmap_write_protect(vcpu
->kvm
, sp
->gfn
))
1210 kvm_flush_remote_tlbs(vcpu
->kvm
);
1211 kvm_unlink_unsync_page(vcpu
->kvm
, sp
);
1212 if (vcpu
->arch
.mmu
.sync_page(vcpu
, sp
)) {
1213 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1217 kvm_mmu_flush_tlb(vcpu
);
1221 struct mmu_page_path
{
1222 struct kvm_mmu_page
*parent
[PT64_ROOT_LEVEL
-1];
1223 unsigned int idx
[PT64_ROOT_LEVEL
-1];
1226 #define for_each_sp(pvec, sp, parents, i) \
1227 for (i = mmu_pages_next(&pvec, &parents, -1), \
1228 sp = pvec.page[i].sp; \
1229 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1230 i = mmu_pages_next(&pvec, &parents, i))
1232 static int mmu_pages_next(struct kvm_mmu_pages
*pvec
,
1233 struct mmu_page_path
*parents
,
1238 for (n
= i
+1; n
< pvec
->nr
; n
++) {
1239 struct kvm_mmu_page
*sp
= pvec
->page
[n
].sp
;
1241 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
) {
1242 parents
->idx
[0] = pvec
->page
[n
].idx
;
1246 parents
->parent
[sp
->role
.level
-2] = sp
;
1247 parents
->idx
[sp
->role
.level
-1] = pvec
->page
[n
].idx
;
1253 static void mmu_pages_clear_parents(struct mmu_page_path
*parents
)
1255 struct kvm_mmu_page
*sp
;
1256 unsigned int level
= 0;
1259 unsigned int idx
= parents
->idx
[level
];
1261 sp
= parents
->parent
[level
];
1265 --sp
->unsync_children
;
1266 WARN_ON((int)sp
->unsync_children
< 0);
1267 __clear_bit(idx
, sp
->unsync_child_bitmap
);
1269 } while (level
< PT64_ROOT_LEVEL
-1 && !sp
->unsync_children
);
1272 static void kvm_mmu_pages_init(struct kvm_mmu_page
*parent
,
1273 struct mmu_page_path
*parents
,
1274 struct kvm_mmu_pages
*pvec
)
1276 parents
->parent
[parent
->role
.level
-1] = NULL
;
1280 static void mmu_sync_children(struct kvm_vcpu
*vcpu
,
1281 struct kvm_mmu_page
*parent
)
1284 struct kvm_mmu_page
*sp
;
1285 struct mmu_page_path parents
;
1286 struct kvm_mmu_pages pages
;
1288 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1289 while (mmu_unsync_walk(parent
, &pages
)) {
1292 for_each_sp(pages
, sp
, parents
, i
)
1293 protected |= rmap_write_protect(vcpu
->kvm
, sp
->gfn
);
1296 kvm_flush_remote_tlbs(vcpu
->kvm
);
1298 for_each_sp(pages
, sp
, parents
, i
) {
1299 kvm_sync_page(vcpu
, sp
);
1300 mmu_pages_clear_parents(&parents
);
1302 cond_resched_lock(&vcpu
->kvm
->mmu_lock
);
1303 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1307 static struct kvm_mmu_page
*kvm_mmu_get_page(struct kvm_vcpu
*vcpu
,
1315 union kvm_mmu_page_role role
;
1318 struct hlist_head
*bucket
;
1319 struct kvm_mmu_page
*sp
;
1320 struct hlist_node
*node
, *tmp
;
1322 role
= vcpu
->arch
.mmu
.base_role
;
1324 role
.direct
= direct
;
1327 role
.access
= access
;
1328 if (vcpu
->arch
.mmu
.root_level
<= PT32_ROOT_LEVEL
) {
1329 quadrant
= gaddr
>> (PAGE_SHIFT
+ (PT64_PT_BITS
* level
));
1330 quadrant
&= (1 << ((PT32_PT_BITS
- PT64_PT_BITS
) * level
)) - 1;
1331 role
.quadrant
= quadrant
;
1333 index
= kvm_page_table_hashfn(gfn
);
1334 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1335 hlist_for_each_entry_safe(sp
, node
, tmp
, bucket
, hash_link
)
1336 if (sp
->gfn
== gfn
) {
1338 if (kvm_sync_page(vcpu
, sp
))
1341 if (sp
->role
.word
!= role
.word
)
1344 mmu_page_add_parent_pte(vcpu
, sp
, parent_pte
);
1345 if (sp
->unsync_children
) {
1346 set_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
);
1347 kvm_mmu_mark_parents_unsync(sp
);
1349 trace_kvm_mmu_get_page(sp
, false);
1352 ++vcpu
->kvm
->stat
.mmu_cache_miss
;
1353 sp
= kvm_mmu_alloc_page(vcpu
, parent_pte
);
1358 hlist_add_head(&sp
->hash_link
, bucket
);
1360 if (rmap_write_protect(vcpu
->kvm
, gfn
))
1361 kvm_flush_remote_tlbs(vcpu
->kvm
);
1362 account_shadowed(vcpu
->kvm
, gfn
);
1364 if (shadow_trap_nonpresent_pte
!= shadow_notrap_nonpresent_pte
)
1365 vcpu
->arch
.mmu
.prefetch_page(vcpu
, sp
);
1367 nonpaging_prefetch_page(vcpu
, sp
);
1368 trace_kvm_mmu_get_page(sp
, true);
1372 static void shadow_walk_init(struct kvm_shadow_walk_iterator
*iterator
,
1373 struct kvm_vcpu
*vcpu
, u64 addr
)
1375 iterator
->addr
= addr
;
1376 iterator
->shadow_addr
= vcpu
->arch
.mmu
.root_hpa
;
1377 iterator
->level
= vcpu
->arch
.mmu
.shadow_root_level
;
1378 if (iterator
->level
== PT32E_ROOT_LEVEL
) {
1379 iterator
->shadow_addr
1380 = vcpu
->arch
.mmu
.pae_root
[(addr
>> 30) & 3];
1381 iterator
->shadow_addr
&= PT64_BASE_ADDR_MASK
;
1383 if (!iterator
->shadow_addr
)
1384 iterator
->level
= 0;
1388 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator
*iterator
)
1390 if (iterator
->level
< PT_PAGE_TABLE_LEVEL
)
1393 if (iterator
->level
== PT_PAGE_TABLE_LEVEL
)
1394 if (is_large_pte(*iterator
->sptep
))
1397 iterator
->index
= SHADOW_PT_INDEX(iterator
->addr
, iterator
->level
);
1398 iterator
->sptep
= ((u64
*)__va(iterator
->shadow_addr
)) + iterator
->index
;
1402 static void shadow_walk_next(struct kvm_shadow_walk_iterator
*iterator
)
1404 iterator
->shadow_addr
= *iterator
->sptep
& PT64_BASE_ADDR_MASK
;
1408 static void kvm_mmu_page_unlink_children(struct kvm
*kvm
,
1409 struct kvm_mmu_page
*sp
)
1417 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1420 if (is_shadow_present_pte(ent
)) {
1421 if (!is_last_spte(ent
, sp
->role
.level
)) {
1422 ent
&= PT64_BASE_ADDR_MASK
;
1423 mmu_page_remove_parent_pte(page_header(ent
),
1426 if (is_large_pte(ent
))
1428 rmap_remove(kvm
, &pt
[i
]);
1431 pt
[i
] = shadow_trap_nonpresent_pte
;
1435 static void kvm_mmu_put_page(struct kvm_mmu_page
*sp
, u64
*parent_pte
)
1437 mmu_page_remove_parent_pte(sp
, parent_pte
);
1440 static void kvm_mmu_reset_last_pte_updated(struct kvm
*kvm
)
1443 struct kvm_vcpu
*vcpu
;
1445 kvm_for_each_vcpu(i
, vcpu
, kvm
)
1446 vcpu
->arch
.last_pte_updated
= NULL
;
1449 static void kvm_mmu_unlink_parents(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1453 while (sp
->multimapped
|| sp
->parent_pte
) {
1454 if (!sp
->multimapped
)
1455 parent_pte
= sp
->parent_pte
;
1457 struct kvm_pte_chain
*chain
;
1459 chain
= container_of(sp
->parent_ptes
.first
,
1460 struct kvm_pte_chain
, link
);
1461 parent_pte
= chain
->parent_ptes
[0];
1463 BUG_ON(!parent_pte
);
1464 kvm_mmu_put_page(sp
, parent_pte
);
1465 __set_spte(parent_pte
, shadow_trap_nonpresent_pte
);
1469 static int mmu_zap_unsync_children(struct kvm
*kvm
,
1470 struct kvm_mmu_page
*parent
)
1473 struct mmu_page_path parents
;
1474 struct kvm_mmu_pages pages
;
1476 if (parent
->role
.level
== PT_PAGE_TABLE_LEVEL
)
1479 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1480 while (mmu_unsync_walk(parent
, &pages
)) {
1481 struct kvm_mmu_page
*sp
;
1483 for_each_sp(pages
, sp
, parents
, i
) {
1484 kvm_mmu_zap_page(kvm
, sp
);
1485 mmu_pages_clear_parents(&parents
);
1488 kvm_mmu_pages_init(parent
, &parents
, &pages
);
1494 static int kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1498 trace_kvm_mmu_zap_page(sp
);
1499 ++kvm
->stat
.mmu_shadow_zapped
;
1500 ret
= mmu_zap_unsync_children(kvm
, sp
);
1501 kvm_mmu_page_unlink_children(kvm
, sp
);
1502 kvm_mmu_unlink_parents(kvm
, sp
);
1503 kvm_flush_remote_tlbs(kvm
);
1504 if (!sp
->role
.invalid
&& !sp
->role
.direct
)
1505 unaccount_shadowed(kvm
, sp
->gfn
);
1507 kvm_unlink_unsync_page(kvm
, sp
);
1508 if (!sp
->root_count
) {
1511 hlist_del(&sp
->hash_link
);
1512 kvm_mmu_free_page(kvm
, sp
);
1514 sp
->role
.invalid
= 1;
1515 list_move(&sp
->link
, &kvm
->arch
.active_mmu_pages
);
1516 kvm_reload_remote_mmus(kvm
);
1518 kvm_mmu_reset_last_pte_updated(kvm
);
1523 * Changing the number of mmu pages allocated to the vm
1524 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1526 void kvm_mmu_change_mmu_pages(struct kvm
*kvm
, unsigned int kvm_nr_mmu_pages
)
1530 used_pages
= kvm
->arch
.n_alloc_mmu_pages
- kvm
->arch
.n_free_mmu_pages
;
1531 used_pages
= max(0, used_pages
);
1534 * If we set the number of mmu pages to be smaller be than the
1535 * number of actived pages , we must to free some mmu pages before we
1539 if (used_pages
> kvm_nr_mmu_pages
) {
1540 while (used_pages
> kvm_nr_mmu_pages
&&
1541 !list_empty(&kvm
->arch
.active_mmu_pages
)) {
1542 struct kvm_mmu_page
*page
;
1544 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
1545 struct kvm_mmu_page
, link
);
1546 used_pages
-= kvm_mmu_zap_page(kvm
, page
);
1548 kvm_nr_mmu_pages
= used_pages
;
1549 kvm
->arch
.n_free_mmu_pages
= 0;
1552 kvm
->arch
.n_free_mmu_pages
+= kvm_nr_mmu_pages
1553 - kvm
->arch
.n_alloc_mmu_pages
;
1555 kvm
->arch
.n_alloc_mmu_pages
= kvm_nr_mmu_pages
;
1558 static int kvm_mmu_unprotect_page(struct kvm
*kvm
, gfn_t gfn
)
1561 struct hlist_head
*bucket
;
1562 struct kvm_mmu_page
*sp
;
1563 struct hlist_node
*node
, *n
;
1566 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1568 index
= kvm_page_table_hashfn(gfn
);
1569 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1571 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
)
1572 if (sp
->gfn
== gfn
&& !sp
->role
.direct
) {
1573 pgprintk("%s: gfn %lx role %x\n", __func__
, gfn
,
1576 if (kvm_mmu_zap_page(kvm
, sp
))
1582 static void mmu_unshadow(struct kvm
*kvm
, gfn_t gfn
)
1585 struct hlist_head
*bucket
;
1586 struct kvm_mmu_page
*sp
;
1587 struct hlist_node
*node
, *nn
;
1589 index
= kvm_page_table_hashfn(gfn
);
1590 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1592 hlist_for_each_entry_safe(sp
, node
, nn
, bucket
, hash_link
) {
1593 if (sp
->gfn
== gfn
&& !sp
->role
.direct
1594 && !sp
->role
.invalid
) {
1595 pgprintk("%s: zap %lx %x\n",
1596 __func__
, gfn
, sp
->role
.word
);
1597 if (kvm_mmu_zap_page(kvm
, sp
))
1603 static void page_header_update_slot(struct kvm
*kvm
, void *pte
, gfn_t gfn
)
1605 int slot
= memslot_id(kvm
, gfn
);
1606 struct kvm_mmu_page
*sp
= page_header(__pa(pte
));
1608 __set_bit(slot
, sp
->slot_bitmap
);
1611 static void mmu_convert_notrap(struct kvm_mmu_page
*sp
)
1616 if (shadow_trap_nonpresent_pte
== shadow_notrap_nonpresent_pte
)
1619 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1620 if (pt
[i
] == shadow_notrap_nonpresent_pte
)
1621 __set_spte(&pt
[i
], shadow_trap_nonpresent_pte
);
1626 * The function is based on mtrr_type_lookup() in
1627 * arch/x86/kernel/cpu/mtrr/generic.c
1629 static int get_mtrr_type(struct mtrr_state_type
*mtrr_state
,
1634 u8 prev_match
, curr_match
;
1635 int num_var_ranges
= KVM_NR_VAR_MTRR
;
1637 if (!mtrr_state
->enabled
)
1640 /* Make end inclusive end, instead of exclusive */
1643 /* Look in fixed ranges. Just return the type as per start */
1644 if (mtrr_state
->have_fixed
&& (start
< 0x100000)) {
1647 if (start
< 0x80000) {
1649 idx
+= (start
>> 16);
1650 return mtrr_state
->fixed_ranges
[idx
];
1651 } else if (start
< 0xC0000) {
1653 idx
+= ((start
- 0x80000) >> 14);
1654 return mtrr_state
->fixed_ranges
[idx
];
1655 } else if (start
< 0x1000000) {
1657 idx
+= ((start
- 0xC0000) >> 12);
1658 return mtrr_state
->fixed_ranges
[idx
];
1663 * Look in variable ranges
1664 * Look of multiple ranges matching this address and pick type
1665 * as per MTRR precedence
1667 if (!(mtrr_state
->enabled
& 2))
1668 return mtrr_state
->def_type
;
1671 for (i
= 0; i
< num_var_ranges
; ++i
) {
1672 unsigned short start_state
, end_state
;
1674 if (!(mtrr_state
->var_ranges
[i
].mask_lo
& (1 << 11)))
1677 base
= (((u64
)mtrr_state
->var_ranges
[i
].base_hi
) << 32) +
1678 (mtrr_state
->var_ranges
[i
].base_lo
& PAGE_MASK
);
1679 mask
= (((u64
)mtrr_state
->var_ranges
[i
].mask_hi
) << 32) +
1680 (mtrr_state
->var_ranges
[i
].mask_lo
& PAGE_MASK
);
1682 start_state
= ((start
& mask
) == (base
& mask
));
1683 end_state
= ((end
& mask
) == (base
& mask
));
1684 if (start_state
!= end_state
)
1687 if ((start
& mask
) != (base
& mask
))
1690 curr_match
= mtrr_state
->var_ranges
[i
].base_lo
& 0xff;
1691 if (prev_match
== 0xFF) {
1692 prev_match
= curr_match
;
1696 if (prev_match
== MTRR_TYPE_UNCACHABLE
||
1697 curr_match
== MTRR_TYPE_UNCACHABLE
)
1698 return MTRR_TYPE_UNCACHABLE
;
1700 if ((prev_match
== MTRR_TYPE_WRBACK
&&
1701 curr_match
== MTRR_TYPE_WRTHROUGH
) ||
1702 (prev_match
== MTRR_TYPE_WRTHROUGH
&&
1703 curr_match
== MTRR_TYPE_WRBACK
)) {
1704 prev_match
= MTRR_TYPE_WRTHROUGH
;
1705 curr_match
= MTRR_TYPE_WRTHROUGH
;
1708 if (prev_match
!= curr_match
)
1709 return MTRR_TYPE_UNCACHABLE
;
1712 if (prev_match
!= 0xFF)
1715 return mtrr_state
->def_type
;
1718 u8
kvm_get_guest_memory_type(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
1722 mtrr
= get_mtrr_type(&vcpu
->arch
.mtrr_state
, gfn
<< PAGE_SHIFT
,
1723 (gfn
<< PAGE_SHIFT
) + PAGE_SIZE
);
1724 if (mtrr
== 0xfe || mtrr
== 0xff)
1725 mtrr
= MTRR_TYPE_WRBACK
;
1728 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type
);
1730 static int kvm_unsync_page(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1733 struct hlist_head
*bucket
;
1734 struct kvm_mmu_page
*s
;
1735 struct hlist_node
*node
, *n
;
1737 index
= kvm_page_table_hashfn(sp
->gfn
);
1738 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1739 /* don't unsync if pagetable is shadowed with multiple roles */
1740 hlist_for_each_entry_safe(s
, node
, n
, bucket
, hash_link
) {
1741 if (s
->gfn
!= sp
->gfn
|| s
->role
.direct
)
1743 if (s
->role
.word
!= sp
->role
.word
)
1746 trace_kvm_mmu_unsync_page(sp
);
1747 ++vcpu
->kvm
->stat
.mmu_unsync
;
1750 kvm_mmu_mark_parents_unsync(sp
);
1752 mmu_convert_notrap(sp
);
1756 static int mmu_need_write_protect(struct kvm_vcpu
*vcpu
, gfn_t gfn
,
1759 struct kvm_mmu_page
*shadow
;
1761 shadow
= kvm_mmu_lookup_page(vcpu
->kvm
, gfn
);
1763 if (shadow
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
1767 if (can_unsync
&& oos_shadow
)
1768 return kvm_unsync_page(vcpu
, shadow
);
1774 static int set_spte(struct kvm_vcpu
*vcpu
, u64
*sptep
,
1775 unsigned pte_access
, int user_fault
,
1776 int write_fault
, int dirty
, int level
,
1777 gfn_t gfn
, pfn_t pfn
, bool speculative
,
1778 bool can_unsync
, bool reset_host_protection
)
1784 * We don't set the accessed bit, since we sometimes want to see
1785 * whether the guest actually used the pte (in order to detect
1788 spte
= shadow_base_present_pte
| shadow_dirty_mask
;
1790 spte
|= shadow_accessed_mask
;
1792 pte_access
&= ~ACC_WRITE_MASK
;
1793 if (pte_access
& ACC_EXEC_MASK
)
1794 spte
|= shadow_x_mask
;
1796 spte
|= shadow_nx_mask
;
1797 if (pte_access
& ACC_USER_MASK
)
1798 spte
|= shadow_user_mask
;
1799 if (level
> PT_PAGE_TABLE_LEVEL
)
1800 spte
|= PT_PAGE_SIZE_MASK
;
1802 spte
|= kvm_x86_ops
->get_mt_mask(vcpu
, gfn
,
1803 kvm_is_mmio_pfn(pfn
));
1805 if (reset_host_protection
)
1806 spte
|= SPTE_HOST_WRITEABLE
;
1808 spte
|= (u64
)pfn
<< PAGE_SHIFT
;
1810 if ((pte_access
& ACC_WRITE_MASK
)
1811 || (write_fault
&& !is_write_protection(vcpu
) && !user_fault
)) {
1813 if (level
> PT_PAGE_TABLE_LEVEL
&&
1814 has_wrprotected_page(vcpu
->kvm
, gfn
, level
)) {
1816 spte
= shadow_trap_nonpresent_pte
;
1820 spte
|= PT_WRITABLE_MASK
;
1822 if (!tdp_enabled
&& !(pte_access
& ACC_WRITE_MASK
))
1823 spte
&= ~PT_USER_MASK
;
1826 * Optimization: for pte sync, if spte was writable the hash
1827 * lookup is unnecessary (and expensive). Write protection
1828 * is responsibility of mmu_get_page / kvm_sync_page.
1829 * Same reasoning can be applied to dirty page accounting.
1831 if (!can_unsync
&& is_writable_pte(*sptep
))
1834 if (mmu_need_write_protect(vcpu
, gfn
, can_unsync
)) {
1835 pgprintk("%s: found shadow page for %lx, marking ro\n",
1838 pte_access
&= ~ACC_WRITE_MASK
;
1839 if (is_writable_pte(spte
))
1840 spte
&= ~PT_WRITABLE_MASK
;
1844 if (pte_access
& ACC_WRITE_MASK
)
1845 mark_page_dirty(vcpu
->kvm
, gfn
);
1848 __set_spte(sptep
, spte
);
1852 static void mmu_set_spte(struct kvm_vcpu
*vcpu
, u64
*sptep
,
1853 unsigned pt_access
, unsigned pte_access
,
1854 int user_fault
, int write_fault
, int dirty
,
1855 int *ptwrite
, int level
, gfn_t gfn
,
1856 pfn_t pfn
, bool speculative
,
1857 bool reset_host_protection
)
1859 int was_rmapped
= 0;
1860 int was_writable
= is_writable_pte(*sptep
);
1863 pgprintk("%s: spte %llx access %x write_fault %d"
1864 " user_fault %d gfn %lx\n",
1865 __func__
, *sptep
, pt_access
,
1866 write_fault
, user_fault
, gfn
);
1868 if (is_rmap_spte(*sptep
)) {
1870 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1871 * the parent of the now unreachable PTE.
1873 if (level
> PT_PAGE_TABLE_LEVEL
&&
1874 !is_large_pte(*sptep
)) {
1875 struct kvm_mmu_page
*child
;
1878 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
1879 mmu_page_remove_parent_pte(child
, sptep
);
1880 __set_spte(sptep
, shadow_trap_nonpresent_pte
);
1881 kvm_flush_remote_tlbs(vcpu
->kvm
);
1882 } else if (pfn
!= spte_to_pfn(*sptep
)) {
1883 pgprintk("hfn old %lx new %lx\n",
1884 spte_to_pfn(*sptep
), pfn
);
1885 rmap_remove(vcpu
->kvm
, sptep
);
1886 __set_spte(sptep
, shadow_trap_nonpresent_pte
);
1887 kvm_flush_remote_tlbs(vcpu
->kvm
);
1892 if (set_spte(vcpu
, sptep
, pte_access
, user_fault
, write_fault
,
1893 dirty
, level
, gfn
, pfn
, speculative
, true,
1894 reset_host_protection
)) {
1897 kvm_x86_ops
->tlb_flush(vcpu
);
1900 pgprintk("%s: setting spte %llx\n", __func__
, *sptep
);
1901 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1902 is_large_pte(*sptep
)? "2MB" : "4kB",
1903 *sptep
& PT_PRESENT_MASK
?"RW":"R", gfn
,
1905 if (!was_rmapped
&& is_large_pte(*sptep
))
1906 ++vcpu
->kvm
->stat
.lpages
;
1908 page_header_update_slot(vcpu
->kvm
, sptep
, gfn
);
1910 rmap_count
= rmap_add(vcpu
, sptep
, gfn
);
1911 kvm_release_pfn_clean(pfn
);
1912 if (rmap_count
> RMAP_RECYCLE_THRESHOLD
)
1913 rmap_recycle(vcpu
, sptep
, gfn
);
1916 kvm_release_pfn_dirty(pfn
);
1918 kvm_release_pfn_clean(pfn
);
1921 vcpu
->arch
.last_pte_updated
= sptep
;
1922 vcpu
->arch
.last_pte_gfn
= gfn
;
1926 static void nonpaging_new_cr3(struct kvm_vcpu
*vcpu
)
1930 static int __direct_map(struct kvm_vcpu
*vcpu
, gpa_t v
, int write
,
1931 int level
, gfn_t gfn
, pfn_t pfn
)
1933 struct kvm_shadow_walk_iterator iterator
;
1934 struct kvm_mmu_page
*sp
;
1938 for_each_shadow_entry(vcpu
, (u64
)gfn
<< PAGE_SHIFT
, iterator
) {
1939 if (iterator
.level
== level
) {
1940 mmu_set_spte(vcpu
, iterator
.sptep
, ACC_ALL
, ACC_ALL
,
1941 0, write
, 1, &pt_write
,
1942 level
, gfn
, pfn
, false, true);
1943 ++vcpu
->stat
.pf_fixed
;
1947 if (*iterator
.sptep
== shadow_trap_nonpresent_pte
) {
1948 pseudo_gfn
= (iterator
.addr
& PT64_DIR_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
1949 sp
= kvm_mmu_get_page(vcpu
, pseudo_gfn
, iterator
.addr
,
1951 1, ACC_ALL
, iterator
.sptep
);
1953 pgprintk("nonpaging_map: ENOMEM\n");
1954 kvm_release_pfn_clean(pfn
);
1958 __set_spte(iterator
.sptep
,
1960 | PT_PRESENT_MASK
| PT_WRITABLE_MASK
1961 | shadow_user_mask
| shadow_x_mask
);
1967 static void kvm_send_hwpoison_signal(struct kvm
*kvm
, gfn_t gfn
)
1973 /* Touch the page, so send SIGBUS */
1974 hva
= (void __user
*)gfn_to_hva(kvm
, gfn
);
1975 r
= copy_from_user(buf
, hva
, 1);
1978 static int kvm_handle_bad_page(struct kvm
*kvm
, gfn_t gfn
, pfn_t pfn
)
1980 kvm_release_pfn_clean(pfn
);
1981 if (is_hwpoison_pfn(pfn
)) {
1982 kvm_send_hwpoison_signal(kvm
, gfn
);
1988 static int nonpaging_map(struct kvm_vcpu
*vcpu
, gva_t v
, int write
, gfn_t gfn
)
1993 unsigned long mmu_seq
;
1995 level
= mapping_level(vcpu
, gfn
);
1998 * This path builds a PAE pagetable - so we can map 2mb pages at
1999 * maximum. Therefore check if the level is larger than that.
2001 if (level
> PT_DIRECTORY_LEVEL
)
2002 level
= PT_DIRECTORY_LEVEL
;
2004 gfn
&= ~(KVM_PAGES_PER_HPAGE(level
) - 1);
2006 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
2008 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
2011 if (is_error_pfn(pfn
))
2012 return kvm_handle_bad_page(vcpu
->kvm
, gfn
, pfn
);
2014 spin_lock(&vcpu
->kvm
->mmu_lock
);
2015 if (mmu_notifier_retry(vcpu
, mmu_seq
))
2017 kvm_mmu_free_some_pages(vcpu
);
2018 r
= __direct_map(vcpu
, v
, write
, level
, gfn
, pfn
);
2019 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2025 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2026 kvm_release_pfn_clean(pfn
);
2031 static void mmu_free_roots(struct kvm_vcpu
*vcpu
)
2034 struct kvm_mmu_page
*sp
;
2036 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
2038 spin_lock(&vcpu
->kvm
->mmu_lock
);
2039 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
2040 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
2042 sp
= page_header(root
);
2044 if (!sp
->root_count
&& sp
->role
.invalid
)
2045 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2046 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
2047 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2050 for (i
= 0; i
< 4; ++i
) {
2051 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
2054 root
&= PT64_BASE_ADDR_MASK
;
2055 sp
= page_header(root
);
2057 if (!sp
->root_count
&& sp
->role
.invalid
)
2058 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2060 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
2062 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2063 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
2066 static int mmu_check_root(struct kvm_vcpu
*vcpu
, gfn_t root_gfn
)
2070 if (!kvm_is_visible_gfn(vcpu
->kvm
, root_gfn
)) {
2071 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
2078 static int mmu_alloc_roots(struct kvm_vcpu
*vcpu
)
2082 struct kvm_mmu_page
*sp
;
2086 root_gfn
= vcpu
->arch
.cr3
>> PAGE_SHIFT
;
2088 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
2089 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
2091 ASSERT(!VALID_PAGE(root
));
2092 if (mmu_check_root(vcpu
, root_gfn
))
2098 spin_lock(&vcpu
->kvm
->mmu_lock
);
2099 kvm_mmu_free_some_pages(vcpu
);
2100 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, 0,
2101 PT64_ROOT_LEVEL
, direct
,
2103 root
= __pa(sp
->spt
);
2105 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2106 vcpu
->arch
.mmu
.root_hpa
= root
;
2109 direct
= !is_paging(vcpu
);
2110 for (i
= 0; i
< 4; ++i
) {
2111 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
2113 ASSERT(!VALID_PAGE(root
));
2114 if (vcpu
->arch
.mmu
.root_level
== PT32E_ROOT_LEVEL
) {
2115 pdptr
= kvm_pdptr_read(vcpu
, i
);
2116 if (!is_present_gpte(pdptr
)) {
2117 vcpu
->arch
.mmu
.pae_root
[i
] = 0;
2120 root_gfn
= pdptr
>> PAGE_SHIFT
;
2121 } else if (vcpu
->arch
.mmu
.root_level
== 0)
2123 if (mmu_check_root(vcpu
, root_gfn
))
2129 spin_lock(&vcpu
->kvm
->mmu_lock
);
2130 kvm_mmu_free_some_pages(vcpu
);
2131 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, i
<< 30,
2132 PT32_ROOT_LEVEL
, direct
,
2134 root
= __pa(sp
->spt
);
2136 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2138 vcpu
->arch
.mmu
.pae_root
[i
] = root
| PT_PRESENT_MASK
;
2140 vcpu
->arch
.mmu
.root_hpa
= __pa(vcpu
->arch
.mmu
.pae_root
);
2144 static void mmu_sync_roots(struct kvm_vcpu
*vcpu
)
2147 struct kvm_mmu_page
*sp
;
2149 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
2151 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
2152 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
2153 sp
= page_header(root
);
2154 mmu_sync_children(vcpu
, sp
);
2157 for (i
= 0; i
< 4; ++i
) {
2158 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
2160 if (root
&& VALID_PAGE(root
)) {
2161 root
&= PT64_BASE_ADDR_MASK
;
2162 sp
= page_header(root
);
2163 mmu_sync_children(vcpu
, sp
);
2168 void kvm_mmu_sync_roots(struct kvm_vcpu
*vcpu
)
2170 spin_lock(&vcpu
->kvm
->mmu_lock
);
2171 mmu_sync_roots(vcpu
);
2172 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2175 static gpa_t
nonpaging_gva_to_gpa(struct kvm_vcpu
*vcpu
, gva_t vaddr
,
2176 u32 access
, u32
*error
)
2183 static int nonpaging_page_fault(struct kvm_vcpu
*vcpu
, gva_t gva
,
2189 pgprintk("%s: gva %lx error %x\n", __func__
, gva
, error_code
);
2190 r
= mmu_topup_memory_caches(vcpu
);
2195 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2197 gfn
= gva
>> PAGE_SHIFT
;
2199 return nonpaging_map(vcpu
, gva
& PAGE_MASK
,
2200 error_code
& PFERR_WRITE_MASK
, gfn
);
2203 static int tdp_page_fault(struct kvm_vcpu
*vcpu
, gva_t gpa
,
2209 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
2210 unsigned long mmu_seq
;
2213 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2215 r
= mmu_topup_memory_caches(vcpu
);
2219 level
= mapping_level(vcpu
, gfn
);
2221 gfn
&= ~(KVM_PAGES_PER_HPAGE(level
) - 1);
2223 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
2225 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
2226 if (is_error_pfn(pfn
))
2227 return kvm_handle_bad_page(vcpu
->kvm
, gfn
, pfn
);
2228 spin_lock(&vcpu
->kvm
->mmu_lock
);
2229 if (mmu_notifier_retry(vcpu
, mmu_seq
))
2231 kvm_mmu_free_some_pages(vcpu
);
2232 r
= __direct_map(vcpu
, gpa
, error_code
& PFERR_WRITE_MASK
,
2234 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2239 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2240 kvm_release_pfn_clean(pfn
);
2244 static void nonpaging_free(struct kvm_vcpu
*vcpu
)
2246 mmu_free_roots(vcpu
);
2249 static int nonpaging_init_context(struct kvm_vcpu
*vcpu
)
2251 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2253 context
->new_cr3
= nonpaging_new_cr3
;
2254 context
->page_fault
= nonpaging_page_fault
;
2255 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
2256 context
->free
= nonpaging_free
;
2257 context
->prefetch_page
= nonpaging_prefetch_page
;
2258 context
->sync_page
= nonpaging_sync_page
;
2259 context
->invlpg
= nonpaging_invlpg
;
2260 context
->root_level
= 0;
2261 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
2262 context
->root_hpa
= INVALID_PAGE
;
2266 void kvm_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
2268 ++vcpu
->stat
.tlb_flush
;
2269 kvm_x86_ops
->tlb_flush(vcpu
);
2272 static void paging_new_cr3(struct kvm_vcpu
*vcpu
)
2274 pgprintk("%s: cr3 %lx\n", __func__
, vcpu
->arch
.cr3
);
2275 mmu_free_roots(vcpu
);
2278 static void inject_page_fault(struct kvm_vcpu
*vcpu
,
2282 kvm_inject_page_fault(vcpu
, addr
, err_code
);
2285 static void paging_free(struct kvm_vcpu
*vcpu
)
2287 nonpaging_free(vcpu
);
2290 static bool is_rsvd_bits_set(struct kvm_vcpu
*vcpu
, u64 gpte
, int level
)
2294 bit7
= (gpte
>> 7) & 1;
2295 return (gpte
& vcpu
->arch
.mmu
.rsvd_bits_mask
[bit7
][level
-1]) != 0;
2299 #include "paging_tmpl.h"
2303 #include "paging_tmpl.h"
2306 static void reset_rsvds_bits_mask(struct kvm_vcpu
*vcpu
, int level
)
2308 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2309 int maxphyaddr
= cpuid_maxphyaddr(vcpu
);
2310 u64 exb_bit_rsvd
= 0;
2313 exb_bit_rsvd
= rsvd_bits(63, 63);
2315 case PT32_ROOT_LEVEL
:
2316 /* no rsvd bits for 2 level 4K page table entries */
2317 context
->rsvd_bits_mask
[0][1] = 0;
2318 context
->rsvd_bits_mask
[0][0] = 0;
2319 context
->rsvd_bits_mask
[1][0] = context
->rsvd_bits_mask
[0][0];
2321 if (!is_pse(vcpu
)) {
2322 context
->rsvd_bits_mask
[1][1] = 0;
2326 if (is_cpuid_PSE36())
2327 /* 36bits PSE 4MB page */
2328 context
->rsvd_bits_mask
[1][1] = rsvd_bits(17, 21);
2330 /* 32 bits PSE 4MB page */
2331 context
->rsvd_bits_mask
[1][1] = rsvd_bits(13, 21);
2333 case PT32E_ROOT_LEVEL
:
2334 context
->rsvd_bits_mask
[0][2] =
2335 rsvd_bits(maxphyaddr
, 63) |
2336 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
2337 context
->rsvd_bits_mask
[0][1] = exb_bit_rsvd
|
2338 rsvd_bits(maxphyaddr
, 62); /* PDE */
2339 context
->rsvd_bits_mask
[0][0] = exb_bit_rsvd
|
2340 rsvd_bits(maxphyaddr
, 62); /* PTE */
2341 context
->rsvd_bits_mask
[1][1] = exb_bit_rsvd
|
2342 rsvd_bits(maxphyaddr
, 62) |
2343 rsvd_bits(13, 20); /* large page */
2344 context
->rsvd_bits_mask
[1][0] = context
->rsvd_bits_mask
[0][0];
2346 case PT64_ROOT_LEVEL
:
2347 context
->rsvd_bits_mask
[0][3] = exb_bit_rsvd
|
2348 rsvd_bits(maxphyaddr
, 51) | rsvd_bits(7, 8);
2349 context
->rsvd_bits_mask
[0][2] = exb_bit_rsvd
|
2350 rsvd_bits(maxphyaddr
, 51) | rsvd_bits(7, 8);
2351 context
->rsvd_bits_mask
[0][1] = exb_bit_rsvd
|
2352 rsvd_bits(maxphyaddr
, 51);
2353 context
->rsvd_bits_mask
[0][0] = exb_bit_rsvd
|
2354 rsvd_bits(maxphyaddr
, 51);
2355 context
->rsvd_bits_mask
[1][3] = context
->rsvd_bits_mask
[0][3];
2356 context
->rsvd_bits_mask
[1][2] = exb_bit_rsvd
|
2357 rsvd_bits(maxphyaddr
, 51) |
2359 context
->rsvd_bits_mask
[1][1] = exb_bit_rsvd
|
2360 rsvd_bits(maxphyaddr
, 51) |
2361 rsvd_bits(13, 20); /* large page */
2362 context
->rsvd_bits_mask
[1][0] = context
->rsvd_bits_mask
[0][0];
2367 static int paging64_init_context_common(struct kvm_vcpu
*vcpu
, int level
)
2369 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2371 ASSERT(is_pae(vcpu
));
2372 context
->new_cr3
= paging_new_cr3
;
2373 context
->page_fault
= paging64_page_fault
;
2374 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2375 context
->prefetch_page
= paging64_prefetch_page
;
2376 context
->sync_page
= paging64_sync_page
;
2377 context
->invlpg
= paging64_invlpg
;
2378 context
->free
= paging_free
;
2379 context
->root_level
= level
;
2380 context
->shadow_root_level
= level
;
2381 context
->root_hpa
= INVALID_PAGE
;
2385 static int paging64_init_context(struct kvm_vcpu
*vcpu
)
2387 reset_rsvds_bits_mask(vcpu
, PT64_ROOT_LEVEL
);
2388 return paging64_init_context_common(vcpu
, PT64_ROOT_LEVEL
);
2391 static int paging32_init_context(struct kvm_vcpu
*vcpu
)
2393 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2395 reset_rsvds_bits_mask(vcpu
, PT32_ROOT_LEVEL
);
2396 context
->new_cr3
= paging_new_cr3
;
2397 context
->page_fault
= paging32_page_fault
;
2398 context
->gva_to_gpa
= paging32_gva_to_gpa
;
2399 context
->free
= paging_free
;
2400 context
->prefetch_page
= paging32_prefetch_page
;
2401 context
->sync_page
= paging32_sync_page
;
2402 context
->invlpg
= paging32_invlpg
;
2403 context
->root_level
= PT32_ROOT_LEVEL
;
2404 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
2405 context
->root_hpa
= INVALID_PAGE
;
2409 static int paging32E_init_context(struct kvm_vcpu
*vcpu
)
2411 reset_rsvds_bits_mask(vcpu
, PT32E_ROOT_LEVEL
);
2412 return paging64_init_context_common(vcpu
, PT32E_ROOT_LEVEL
);
2415 static int init_kvm_tdp_mmu(struct kvm_vcpu
*vcpu
)
2417 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2419 context
->new_cr3
= nonpaging_new_cr3
;
2420 context
->page_fault
= tdp_page_fault
;
2421 context
->free
= nonpaging_free
;
2422 context
->prefetch_page
= nonpaging_prefetch_page
;
2423 context
->sync_page
= nonpaging_sync_page
;
2424 context
->invlpg
= nonpaging_invlpg
;
2425 context
->shadow_root_level
= kvm_x86_ops
->get_tdp_level();
2426 context
->root_hpa
= INVALID_PAGE
;
2428 if (!is_paging(vcpu
)) {
2429 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
2430 context
->root_level
= 0;
2431 } else if (is_long_mode(vcpu
)) {
2432 reset_rsvds_bits_mask(vcpu
, PT64_ROOT_LEVEL
);
2433 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2434 context
->root_level
= PT64_ROOT_LEVEL
;
2435 } else if (is_pae(vcpu
)) {
2436 reset_rsvds_bits_mask(vcpu
, PT32E_ROOT_LEVEL
);
2437 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2438 context
->root_level
= PT32E_ROOT_LEVEL
;
2440 reset_rsvds_bits_mask(vcpu
, PT32_ROOT_LEVEL
);
2441 context
->gva_to_gpa
= paging32_gva_to_gpa
;
2442 context
->root_level
= PT32_ROOT_LEVEL
;
2448 static int init_kvm_softmmu(struct kvm_vcpu
*vcpu
)
2453 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2455 if (!is_paging(vcpu
))
2456 r
= nonpaging_init_context(vcpu
);
2457 else if (is_long_mode(vcpu
))
2458 r
= paging64_init_context(vcpu
);
2459 else if (is_pae(vcpu
))
2460 r
= paging32E_init_context(vcpu
);
2462 r
= paging32_init_context(vcpu
);
2464 vcpu
->arch
.mmu
.base_role
.cr4_pae
= !!is_pae(vcpu
);
2465 vcpu
->arch
.mmu
.base_role
.cr0_wp
= is_write_protection(vcpu
);
2470 static int init_kvm_mmu(struct kvm_vcpu
*vcpu
)
2472 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
2475 return init_kvm_tdp_mmu(vcpu
);
2477 return init_kvm_softmmu(vcpu
);
2480 static void destroy_kvm_mmu(struct kvm_vcpu
*vcpu
)
2483 if (VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
2484 /* mmu.free() should set root_hpa = INVALID_PAGE */
2485 vcpu
->arch
.mmu
.free(vcpu
);
2488 int kvm_mmu_reset_context(struct kvm_vcpu
*vcpu
)
2490 destroy_kvm_mmu(vcpu
);
2491 return init_kvm_mmu(vcpu
);
2493 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context
);
2495 int kvm_mmu_load(struct kvm_vcpu
*vcpu
)
2499 r
= mmu_topup_memory_caches(vcpu
);
2502 r
= mmu_alloc_roots(vcpu
);
2503 spin_lock(&vcpu
->kvm
->mmu_lock
);
2504 mmu_sync_roots(vcpu
);
2505 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2508 /* set_cr3() should ensure TLB has been flushed */
2509 kvm_x86_ops
->set_cr3(vcpu
, vcpu
->arch
.mmu
.root_hpa
);
2513 EXPORT_SYMBOL_GPL(kvm_mmu_load
);
2515 void kvm_mmu_unload(struct kvm_vcpu
*vcpu
)
2517 mmu_free_roots(vcpu
);
2520 static void mmu_pte_write_zap_pte(struct kvm_vcpu
*vcpu
,
2521 struct kvm_mmu_page
*sp
,
2525 struct kvm_mmu_page
*child
;
2528 if (is_shadow_present_pte(pte
)) {
2529 if (is_last_spte(pte
, sp
->role
.level
))
2530 rmap_remove(vcpu
->kvm
, spte
);
2532 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
2533 mmu_page_remove_parent_pte(child
, spte
);
2536 __set_spte(spte
, shadow_trap_nonpresent_pte
);
2537 if (is_large_pte(pte
))
2538 --vcpu
->kvm
->stat
.lpages
;
2541 static void mmu_pte_write_new_pte(struct kvm_vcpu
*vcpu
,
2542 struct kvm_mmu_page
*sp
,
2546 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
) {
2547 ++vcpu
->kvm
->stat
.mmu_pde_zapped
;
2551 ++vcpu
->kvm
->stat
.mmu_pte_updated
;
2552 if (!sp
->role
.cr4_pae
)
2553 paging32_update_pte(vcpu
, sp
, spte
, new);
2555 paging64_update_pte(vcpu
, sp
, spte
, new);
2558 static bool need_remote_flush(u64 old
, u64
new)
2560 if (!is_shadow_present_pte(old
))
2562 if (!is_shadow_present_pte(new))
2564 if ((old
^ new) & PT64_BASE_ADDR_MASK
)
2566 old
^= PT64_NX_MASK
;
2567 new ^= PT64_NX_MASK
;
2568 return (old
& ~new & PT64_PERM_MASK
) != 0;
2571 static void mmu_pte_write_flush_tlb(struct kvm_vcpu
*vcpu
, u64 old
, u64
new)
2573 if (need_remote_flush(old
, new))
2574 kvm_flush_remote_tlbs(vcpu
->kvm
);
2576 kvm_mmu_flush_tlb(vcpu
);
2579 static bool last_updated_pte_accessed(struct kvm_vcpu
*vcpu
)
2581 u64
*spte
= vcpu
->arch
.last_pte_updated
;
2583 return !!(spte
&& (*spte
& shadow_accessed_mask
));
2586 static void mmu_guess_page_from_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2592 if (!is_present_gpte(gpte
))
2594 gfn
= (gpte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
2596 vcpu
->arch
.update_pte
.mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
2598 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
2600 if (is_error_pfn(pfn
)) {
2601 kvm_release_pfn_clean(pfn
);
2604 vcpu
->arch
.update_pte
.gfn
= gfn
;
2605 vcpu
->arch
.update_pte
.pfn
= pfn
;
2608 static void kvm_mmu_access_page(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
2610 u64
*spte
= vcpu
->arch
.last_pte_updated
;
2613 && vcpu
->arch
.last_pte_gfn
== gfn
2614 && shadow_accessed_mask
2615 && !(*spte
& shadow_accessed_mask
)
2616 && is_shadow_present_pte(*spte
))
2617 set_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
2620 void kvm_mmu_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2621 const u8
*new, int bytes
,
2622 bool guest_initiated
)
2624 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
2625 struct kvm_mmu_page
*sp
;
2626 struct hlist_node
*node
, *n
;
2627 struct hlist_head
*bucket
;
2631 unsigned offset
= offset_in_page(gpa
);
2633 unsigned page_offset
;
2634 unsigned misaligned
;
2642 pgprintk("%s: gpa %llx bytes %d\n", __func__
, gpa
, bytes
);
2644 invlpg_counter
= atomic_read(&vcpu
->kvm
->arch
.invlpg_counter
);
2647 * Assume that the pte write on a page table of the same type
2648 * as the current vcpu paging mode. This is nearly always true
2649 * (might be false while changing modes). Note it is verified later
2652 if ((is_pae(vcpu
) && bytes
== 4) || !new) {
2653 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2658 r
= kvm_read_guest(vcpu
->kvm
, gpa
, &gentry
, min(bytes
, 8));
2661 new = (const u8
*)&gentry
;
2666 gentry
= *(const u32
*)new;
2669 gentry
= *(const u64
*)new;
2676 mmu_guess_page_from_pte_write(vcpu
, gpa
, gentry
);
2677 spin_lock(&vcpu
->kvm
->mmu_lock
);
2678 if (atomic_read(&vcpu
->kvm
->arch
.invlpg_counter
) != invlpg_counter
)
2680 kvm_mmu_access_page(vcpu
, gfn
);
2681 kvm_mmu_free_some_pages(vcpu
);
2682 ++vcpu
->kvm
->stat
.mmu_pte_write
;
2683 kvm_mmu_audit(vcpu
, "pre pte write");
2684 if (guest_initiated
) {
2685 if (gfn
== vcpu
->arch
.last_pt_write_gfn
2686 && !last_updated_pte_accessed(vcpu
)) {
2687 ++vcpu
->arch
.last_pt_write_count
;
2688 if (vcpu
->arch
.last_pt_write_count
>= 3)
2691 vcpu
->arch
.last_pt_write_gfn
= gfn
;
2692 vcpu
->arch
.last_pt_write_count
= 1;
2693 vcpu
->arch
.last_pte_updated
= NULL
;
2696 index
= kvm_page_table_hashfn(gfn
);
2697 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
2700 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
) {
2701 if (sp
->gfn
!= gfn
|| sp
->role
.direct
|| sp
->role
.invalid
)
2703 pte_size
= sp
->role
.cr4_pae
? 8 : 4;
2704 misaligned
= (offset
^ (offset
+ bytes
- 1)) & ~(pte_size
- 1);
2705 misaligned
|= bytes
< 4;
2706 if (misaligned
|| flooded
) {
2708 * Misaligned accesses are too much trouble to fix
2709 * up; also, they usually indicate a page is not used
2712 * If we're seeing too many writes to a page,
2713 * it may no longer be a page table, or we may be
2714 * forking, in which case it is better to unmap the
2717 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2718 gpa
, bytes
, sp
->role
.word
);
2719 if (kvm_mmu_zap_page(vcpu
->kvm
, sp
))
2721 ++vcpu
->kvm
->stat
.mmu_flooded
;
2724 page_offset
= offset
;
2725 level
= sp
->role
.level
;
2727 if (!sp
->role
.cr4_pae
) {
2728 page_offset
<<= 1; /* 32->64 */
2730 * A 32-bit pde maps 4MB while the shadow pdes map
2731 * only 2MB. So we need to double the offset again
2732 * and zap two pdes instead of one.
2734 if (level
== PT32_ROOT_LEVEL
) {
2735 page_offset
&= ~7; /* kill rounding error */
2739 quadrant
= page_offset
>> PAGE_SHIFT
;
2740 page_offset
&= ~PAGE_MASK
;
2741 if (quadrant
!= sp
->role
.quadrant
)
2744 spte
= &sp
->spt
[page_offset
/ sizeof(*spte
)];
2747 mmu_pte_write_zap_pte(vcpu
, sp
, spte
);
2749 mmu_pte_write_new_pte(vcpu
, sp
, spte
, &gentry
);
2750 mmu_pte_write_flush_tlb(vcpu
, entry
, *spte
);
2754 kvm_mmu_audit(vcpu
, "post pte write");
2755 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2756 if (!is_error_pfn(vcpu
->arch
.update_pte
.pfn
)) {
2757 kvm_release_pfn_clean(vcpu
->arch
.update_pte
.pfn
);
2758 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
2762 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu
*vcpu
, gva_t gva
)
2770 gpa
= kvm_mmu_gva_to_gpa_read(vcpu
, gva
, NULL
);
2772 spin_lock(&vcpu
->kvm
->mmu_lock
);
2773 r
= kvm_mmu_unprotect_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2774 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2777 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt
);
2779 void __kvm_mmu_free_some_pages(struct kvm_vcpu
*vcpu
)
2781 while (vcpu
->kvm
->arch
.n_free_mmu_pages
< KVM_REFILL_PAGES
&&
2782 !list_empty(&vcpu
->kvm
->arch
.active_mmu_pages
)) {
2783 struct kvm_mmu_page
*sp
;
2785 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.prev
,
2786 struct kvm_mmu_page
, link
);
2787 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2788 ++vcpu
->kvm
->stat
.mmu_recycled
;
2792 int kvm_mmu_page_fault(struct kvm_vcpu
*vcpu
, gva_t cr2
, u32 error_code
)
2795 enum emulation_result er
;
2797 r
= vcpu
->arch
.mmu
.page_fault(vcpu
, cr2
, error_code
);
2806 r
= mmu_topup_memory_caches(vcpu
);
2810 er
= emulate_instruction(vcpu
, cr2
, error_code
, 0);
2815 case EMULATE_DO_MMIO
:
2816 ++vcpu
->stat
.mmio_exits
;
2826 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault
);
2828 void kvm_mmu_invlpg(struct kvm_vcpu
*vcpu
, gva_t gva
)
2830 vcpu
->arch
.mmu
.invlpg(vcpu
, gva
);
2831 kvm_mmu_flush_tlb(vcpu
);
2832 ++vcpu
->stat
.invlpg
;
2834 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg
);
2836 void kvm_enable_tdp(void)
2840 EXPORT_SYMBOL_GPL(kvm_enable_tdp
);
2842 void kvm_disable_tdp(void)
2844 tdp_enabled
= false;
2846 EXPORT_SYMBOL_GPL(kvm_disable_tdp
);
2848 static void free_mmu_pages(struct kvm_vcpu
*vcpu
)
2850 free_page((unsigned long)vcpu
->arch
.mmu
.pae_root
);
2853 static int alloc_mmu_pages(struct kvm_vcpu
*vcpu
)
2861 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2862 * Therefore we need to allocate shadow page tables in the first
2863 * 4GB of memory, which happens to fit the DMA32 zone.
2865 page
= alloc_page(GFP_KERNEL
| __GFP_DMA32
);
2869 vcpu
->arch
.mmu
.pae_root
= page_address(page
);
2870 for (i
= 0; i
< 4; ++i
)
2871 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
2876 int kvm_mmu_create(struct kvm_vcpu
*vcpu
)
2879 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2881 return alloc_mmu_pages(vcpu
);
2884 int kvm_mmu_setup(struct kvm_vcpu
*vcpu
)
2887 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2889 return init_kvm_mmu(vcpu
);
2892 void kvm_mmu_destroy(struct kvm_vcpu
*vcpu
)
2896 destroy_kvm_mmu(vcpu
);
2897 free_mmu_pages(vcpu
);
2898 mmu_free_memory_caches(vcpu
);
2901 void kvm_mmu_slot_remove_write_access(struct kvm
*kvm
, int slot
)
2903 struct kvm_mmu_page
*sp
;
2905 list_for_each_entry(sp
, &kvm
->arch
.active_mmu_pages
, link
) {
2909 if (!test_bit(slot
, sp
->slot_bitmap
))
2913 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
2915 if (pt
[i
] & PT_WRITABLE_MASK
)
2916 pt
[i
] &= ~PT_WRITABLE_MASK
;
2918 kvm_flush_remote_tlbs(kvm
);
2921 void kvm_mmu_zap_all(struct kvm
*kvm
)
2923 struct kvm_mmu_page
*sp
, *node
;
2925 spin_lock(&kvm
->mmu_lock
);
2927 list_for_each_entry_safe(sp
, node
, &kvm
->arch
.active_mmu_pages
, link
)
2928 if (kvm_mmu_zap_page(kvm
, sp
))
2931 spin_unlock(&kvm
->mmu_lock
);
2933 kvm_flush_remote_tlbs(kvm
);
2936 static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm
*kvm
)
2938 struct kvm_mmu_page
*page
;
2940 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
2941 struct kvm_mmu_page
, link
);
2942 return kvm_mmu_zap_page(kvm
, page
);
2945 static int mmu_shrink(struct shrinker
*shrink
, int nr_to_scan
, gfp_t gfp_mask
)
2948 struct kvm
*kvm_freed
= NULL
;
2949 int cache_count
= 0;
2951 spin_lock(&kvm_lock
);
2953 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
2954 int npages
, idx
, freed_pages
;
2956 idx
= srcu_read_lock(&kvm
->srcu
);
2957 spin_lock(&kvm
->mmu_lock
);
2958 npages
= kvm
->arch
.n_alloc_mmu_pages
-
2959 kvm
->arch
.n_free_mmu_pages
;
2960 cache_count
+= npages
;
2961 if (!kvm_freed
&& nr_to_scan
> 0 && npages
> 0) {
2962 freed_pages
= kvm_mmu_remove_some_alloc_mmu_pages(kvm
);
2963 cache_count
-= freed_pages
;
2968 spin_unlock(&kvm
->mmu_lock
);
2969 srcu_read_unlock(&kvm
->srcu
, idx
);
2972 list_move_tail(&kvm_freed
->vm_list
, &vm_list
);
2974 spin_unlock(&kvm_lock
);
2979 static struct shrinker mmu_shrinker
= {
2980 .shrink
= mmu_shrink
,
2981 .seeks
= DEFAULT_SEEKS
* 10,
2984 static void mmu_destroy_caches(void)
2986 if (pte_chain_cache
)
2987 kmem_cache_destroy(pte_chain_cache
);
2988 if (rmap_desc_cache
)
2989 kmem_cache_destroy(rmap_desc_cache
);
2990 if (mmu_page_header_cache
)
2991 kmem_cache_destroy(mmu_page_header_cache
);
2994 void kvm_mmu_module_exit(void)
2996 mmu_destroy_caches();
2997 unregister_shrinker(&mmu_shrinker
);
3000 int kvm_mmu_module_init(void)
3002 pte_chain_cache
= kmem_cache_create("kvm_pte_chain",
3003 sizeof(struct kvm_pte_chain
),
3005 if (!pte_chain_cache
)
3007 rmap_desc_cache
= kmem_cache_create("kvm_rmap_desc",
3008 sizeof(struct kvm_rmap_desc
),
3010 if (!rmap_desc_cache
)
3013 mmu_page_header_cache
= kmem_cache_create("kvm_mmu_page_header",
3014 sizeof(struct kvm_mmu_page
),
3016 if (!mmu_page_header_cache
)
3019 register_shrinker(&mmu_shrinker
);
3024 mmu_destroy_caches();
3029 * Caculate mmu pages needed for kvm.
3031 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm
*kvm
)
3034 unsigned int nr_mmu_pages
;
3035 unsigned int nr_pages
= 0;
3036 struct kvm_memslots
*slots
;
3038 slots
= kvm_memslots(kvm
);
3040 for (i
= 0; i
< slots
->nmemslots
; i
++)
3041 nr_pages
+= slots
->memslots
[i
].npages
;
3043 nr_mmu_pages
= nr_pages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
3044 nr_mmu_pages
= max(nr_mmu_pages
,
3045 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
3047 return nr_mmu_pages
;
3050 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
3053 if (len
> buffer
->len
)
3058 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
3063 ret
= pv_mmu_peek_buffer(buffer
, len
);
3068 buffer
->processed
+= len
;
3072 static int kvm_pv_mmu_write(struct kvm_vcpu
*vcpu
,
3073 gpa_t addr
, gpa_t value
)
3078 if (!is_long_mode(vcpu
) && !is_pae(vcpu
))
3081 r
= mmu_topup_memory_caches(vcpu
);
3085 if (!emulator_write_phys(vcpu
, addr
, &value
, bytes
))
3091 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
3093 kvm_set_cr3(vcpu
, vcpu
->arch
.cr3
);
3097 static int kvm_pv_mmu_release_pt(struct kvm_vcpu
*vcpu
, gpa_t addr
)
3099 spin_lock(&vcpu
->kvm
->mmu_lock
);
3100 mmu_unshadow(vcpu
->kvm
, addr
>> PAGE_SHIFT
);
3101 spin_unlock(&vcpu
->kvm
->mmu_lock
);
3105 static int kvm_pv_mmu_op_one(struct kvm_vcpu
*vcpu
,
3106 struct kvm_pv_mmu_op_buffer
*buffer
)
3108 struct kvm_mmu_op_header
*header
;
3110 header
= pv_mmu_peek_buffer(buffer
, sizeof *header
);
3113 switch (header
->op
) {
3114 case KVM_MMU_OP_WRITE_PTE
: {
3115 struct kvm_mmu_op_write_pte
*wpte
;
3117 wpte
= pv_mmu_read_buffer(buffer
, sizeof *wpte
);
3120 return kvm_pv_mmu_write(vcpu
, wpte
->pte_phys
,
3123 case KVM_MMU_OP_FLUSH_TLB
: {
3124 struct kvm_mmu_op_flush_tlb
*ftlb
;
3126 ftlb
= pv_mmu_read_buffer(buffer
, sizeof *ftlb
);
3129 return kvm_pv_mmu_flush_tlb(vcpu
);
3131 case KVM_MMU_OP_RELEASE_PT
: {
3132 struct kvm_mmu_op_release_pt
*rpt
;
3134 rpt
= pv_mmu_read_buffer(buffer
, sizeof *rpt
);
3137 return kvm_pv_mmu_release_pt(vcpu
, rpt
->pt_phys
);
3143 int kvm_pv_mmu_op(struct kvm_vcpu
*vcpu
, unsigned long bytes
,
3144 gpa_t addr
, unsigned long *ret
)
3147 struct kvm_pv_mmu_op_buffer
*buffer
= &vcpu
->arch
.mmu_op_buffer
;
3149 buffer
->ptr
= buffer
->buf
;
3150 buffer
->len
= min_t(unsigned long, bytes
, sizeof buffer
->buf
);
3151 buffer
->processed
= 0;
3153 r
= kvm_read_guest(vcpu
->kvm
, addr
, buffer
->buf
, buffer
->len
);
3157 while (buffer
->len
) {
3158 r
= kvm_pv_mmu_op_one(vcpu
, buffer
);
3167 *ret
= buffer
->processed
;
3171 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu
*vcpu
, u64 addr
, u64 sptes
[4])
3173 struct kvm_shadow_walk_iterator iterator
;
3176 spin_lock(&vcpu
->kvm
->mmu_lock
);
3177 for_each_shadow_entry(vcpu
, addr
, iterator
) {
3178 sptes
[iterator
.level
-1] = *iterator
.sptep
;
3180 if (!is_shadow_present_pte(*iterator
.sptep
))
3183 spin_unlock(&vcpu
->kvm
->mmu_lock
);
3187 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy
);
3191 static const char *audit_msg
;
3193 static gva_t
canonicalize(gva_t gva
)
3195 #ifdef CONFIG_X86_64
3196 gva
= (long long)(gva
<< 16) >> 16;
3202 typedef void (*inspect_spte_fn
) (struct kvm
*kvm
, u64
*sptep
);
3204 static void __mmu_spte_walk(struct kvm
*kvm
, struct kvm_mmu_page
*sp
,
3209 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
3210 u64 ent
= sp
->spt
[i
];
3212 if (is_shadow_present_pte(ent
)) {
3213 if (!is_last_spte(ent
, sp
->role
.level
)) {
3214 struct kvm_mmu_page
*child
;
3215 child
= page_header(ent
& PT64_BASE_ADDR_MASK
);
3216 __mmu_spte_walk(kvm
, child
, fn
);
3218 fn(kvm
, &sp
->spt
[i
]);
3223 static void mmu_spte_walk(struct kvm_vcpu
*vcpu
, inspect_spte_fn fn
)
3226 struct kvm_mmu_page
*sp
;
3228 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
3230 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
3231 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
3232 sp
= page_header(root
);
3233 __mmu_spte_walk(vcpu
->kvm
, sp
, fn
);
3236 for (i
= 0; i
< 4; ++i
) {
3237 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
3239 if (root
&& VALID_PAGE(root
)) {
3240 root
&= PT64_BASE_ADDR_MASK
;
3241 sp
= page_header(root
);
3242 __mmu_spte_walk(vcpu
->kvm
, sp
, fn
);
3248 static void audit_mappings_page(struct kvm_vcpu
*vcpu
, u64 page_pte
,
3249 gva_t va
, int level
)
3251 u64
*pt
= __va(page_pte
& PT64_BASE_ADDR_MASK
);
3253 gva_t va_delta
= 1ul << (PAGE_SHIFT
+ 9 * (level
- 1));
3255 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
, va
+= va_delta
) {
3258 if (ent
== shadow_trap_nonpresent_pte
)
3261 va
= canonicalize(va
);
3262 if (is_shadow_present_pte(ent
) && !is_last_spte(ent
, level
))
3263 audit_mappings_page(vcpu
, ent
, va
, level
- 1);
3265 gpa_t gpa
= kvm_mmu_gva_to_gpa_read(vcpu
, va
, NULL
);
3266 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
3267 pfn_t pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
3268 hpa_t hpa
= (hpa_t
)pfn
<< PAGE_SHIFT
;
3270 if (is_error_pfn(pfn
)) {
3271 kvm_release_pfn_clean(pfn
);
3275 if (is_shadow_present_pte(ent
)
3276 && (ent
& PT64_BASE_ADDR_MASK
) != hpa
)
3277 printk(KERN_ERR
"xx audit error: (%s) levels %d"
3278 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3279 audit_msg
, vcpu
->arch
.mmu
.root_level
,
3281 is_shadow_present_pte(ent
));
3282 else if (ent
== shadow_notrap_nonpresent_pte
3283 && !is_error_hpa(hpa
))
3284 printk(KERN_ERR
"audit: (%s) notrap shadow,"
3285 " valid guest gva %lx\n", audit_msg
, va
);
3286 kvm_release_pfn_clean(pfn
);
3292 static void audit_mappings(struct kvm_vcpu
*vcpu
)
3296 if (vcpu
->arch
.mmu
.root_level
== 4)
3297 audit_mappings_page(vcpu
, vcpu
->arch
.mmu
.root_hpa
, 0, 4);
3299 for (i
= 0; i
< 4; ++i
)
3300 if (vcpu
->arch
.mmu
.pae_root
[i
] & PT_PRESENT_MASK
)
3301 audit_mappings_page(vcpu
,
3302 vcpu
->arch
.mmu
.pae_root
[i
],
3307 static int count_rmaps(struct kvm_vcpu
*vcpu
)
3309 struct kvm
*kvm
= vcpu
->kvm
;
3310 struct kvm_memslots
*slots
;
3314 idx
= srcu_read_lock(&kvm
->srcu
);
3315 slots
= kvm_memslots(kvm
);
3316 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
3317 struct kvm_memory_slot
*m
= &slots
->memslots
[i
];
3318 struct kvm_rmap_desc
*d
;
3320 for (j
= 0; j
< m
->npages
; ++j
) {
3321 unsigned long *rmapp
= &m
->rmap
[j
];
3325 if (!(*rmapp
& 1)) {
3329 d
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
3331 for (k
= 0; k
< RMAP_EXT
; ++k
)
3340 srcu_read_unlock(&kvm
->srcu
, idx
);
3344 void inspect_spte_has_rmap(struct kvm
*kvm
, u64
*sptep
)
3346 unsigned long *rmapp
;
3347 struct kvm_mmu_page
*rev_sp
;
3350 if (*sptep
& PT_WRITABLE_MASK
) {
3351 rev_sp
= page_header(__pa(sptep
));
3352 gfn
= rev_sp
->gfns
[sptep
- rev_sp
->spt
];
3354 if (!gfn_to_memslot(kvm
, gfn
)) {
3355 if (!printk_ratelimit())
3357 printk(KERN_ERR
"%s: no memslot for gfn %ld\n",
3359 printk(KERN_ERR
"%s: index %ld of sp (gfn=%lx)\n",
3360 audit_msg
, (long int)(sptep
- rev_sp
->spt
),
3366 rmapp
= gfn_to_rmap(kvm
, rev_sp
->gfns
[sptep
- rev_sp
->spt
],
3367 rev_sp
->role
.level
);
3369 if (!printk_ratelimit())
3371 printk(KERN_ERR
"%s: no rmap for writable spte %llx\n",
3379 void audit_writable_sptes_have_rmaps(struct kvm_vcpu
*vcpu
)
3381 mmu_spte_walk(vcpu
, inspect_spte_has_rmap
);
3384 static void check_writable_mappings_rmap(struct kvm_vcpu
*vcpu
)
3386 struct kvm_mmu_page
*sp
;
3389 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
3392 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
3395 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
3398 if (!(ent
& PT_PRESENT_MASK
))
3400 if (!(ent
& PT_WRITABLE_MASK
))
3402 inspect_spte_has_rmap(vcpu
->kvm
, &pt
[i
]);
3408 static void audit_rmap(struct kvm_vcpu
*vcpu
)
3410 check_writable_mappings_rmap(vcpu
);
3414 static void audit_write_protection(struct kvm_vcpu
*vcpu
)
3416 struct kvm_mmu_page
*sp
;
3417 struct kvm_memory_slot
*slot
;
3418 unsigned long *rmapp
;
3422 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
3423 if (sp
->role
.direct
)
3428 gfn
= unalias_gfn(vcpu
->kvm
, sp
->gfn
);
3429 slot
= gfn_to_memslot_unaliased(vcpu
->kvm
, sp
->gfn
);
3430 rmapp
= &slot
->rmap
[gfn
- slot
->base_gfn
];
3432 spte
= rmap_next(vcpu
->kvm
, rmapp
, NULL
);
3434 if (*spte
& PT_WRITABLE_MASK
)
3435 printk(KERN_ERR
"%s: (%s) shadow page has "
3436 "writable mappings: gfn %lx role %x\n",
3437 __func__
, audit_msg
, sp
->gfn
,
3439 spte
= rmap_next(vcpu
->kvm
, rmapp
, spte
);
3444 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
)
3451 audit_write_protection(vcpu
);
3452 if (strcmp("pre pte write", audit_msg
) != 0)
3453 audit_mappings(vcpu
);
3454 audit_writable_sptes_have_rmaps(vcpu
);