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.
23 #include <linux/kvm_host.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
27 #include <linux/highmem.h>
28 #include <linux/module.h>
29 #include <linux/swap.h>
30 #include <linux/hugetlb.h>
31 #include <linux/compiler.h>
34 #include <asm/cmpxchg.h>
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
44 bool tdp_enabled
= false;
51 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
);
53 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
) {}
58 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
63 #define pgprintk(x...) do { } while (0)
64 #define rmap_printk(x...) do { } while (0)
68 #if defined(MMU_DEBUG) || defined(AUDIT)
70 module_param(dbg
, bool, 0644);
73 static int oos_shadow
= 1;
74 module_param(oos_shadow
, bool, 0644);
77 #define ASSERT(x) do { } while (0)
81 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
82 __FILE__, __LINE__, #x); \
86 #define PT_FIRST_AVAIL_BITS_SHIFT 9
87 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
89 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
91 #define PT64_LEVEL_BITS 9
93 #define PT64_LEVEL_SHIFT(level) \
94 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
96 #define PT64_LEVEL_MASK(level) \
97 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
99 #define PT64_INDEX(address, level)\
100 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
103 #define PT32_LEVEL_BITS 10
105 #define PT32_LEVEL_SHIFT(level) \
106 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
108 #define PT32_LEVEL_MASK(level) \
109 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
111 #define PT32_INDEX(address, level)\
112 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
115 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
116 #define PT64_DIR_BASE_ADDR_MASK \
117 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
119 #define PT32_BASE_ADDR_MASK PAGE_MASK
120 #define PT32_DIR_BASE_ADDR_MASK \
121 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
123 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
126 #define PFERR_PRESENT_MASK (1U << 0)
127 #define PFERR_WRITE_MASK (1U << 1)
128 #define PFERR_USER_MASK (1U << 2)
129 #define PFERR_FETCH_MASK (1U << 4)
131 #define PT_DIRECTORY_LEVEL 2
132 #define PT_PAGE_TABLE_LEVEL 1
136 #define ACC_EXEC_MASK 1
137 #define ACC_WRITE_MASK PT_WRITABLE_MASK
138 #define ACC_USER_MASK PT_USER_MASK
139 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
141 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
143 struct kvm_rmap_desc
{
144 u64
*shadow_ptes
[RMAP_EXT
];
145 struct kvm_rmap_desc
*more
;
148 struct kvm_shadow_walk
{
149 int (*entry
)(struct kvm_shadow_walk
*walk
, struct kvm_vcpu
*vcpu
,
150 u64 addr
, u64
*spte
, int level
);
153 struct kvm_unsync_walk
{
154 int (*entry
) (struct kvm_mmu_page
*sp
, struct kvm_unsync_walk
*walk
);
157 typedef int (*mmu_parent_walk_fn
) (struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
);
159 static struct kmem_cache
*pte_chain_cache
;
160 static struct kmem_cache
*rmap_desc_cache
;
161 static struct kmem_cache
*mmu_page_header_cache
;
163 static u64 __read_mostly shadow_trap_nonpresent_pte
;
164 static u64 __read_mostly shadow_notrap_nonpresent_pte
;
165 static u64 __read_mostly shadow_base_present_pte
;
166 static u64 __read_mostly shadow_nx_mask
;
167 static u64 __read_mostly shadow_x_mask
; /* mutual exclusive with nx_mask */
168 static u64 __read_mostly shadow_user_mask
;
169 static u64 __read_mostly shadow_accessed_mask
;
170 static u64 __read_mostly shadow_dirty_mask
;
171 static u64 __read_mostly shadow_mt_mask
;
173 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte
, u64 notrap_pte
)
175 shadow_trap_nonpresent_pte
= trap_pte
;
176 shadow_notrap_nonpresent_pte
= notrap_pte
;
178 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes
);
180 void kvm_mmu_set_base_ptes(u64 base_pte
)
182 shadow_base_present_pte
= base_pte
;
184 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes
);
186 void kvm_mmu_set_mask_ptes(u64 user_mask
, u64 accessed_mask
,
187 u64 dirty_mask
, u64 nx_mask
, u64 x_mask
, u64 mt_mask
)
189 shadow_user_mask
= user_mask
;
190 shadow_accessed_mask
= accessed_mask
;
191 shadow_dirty_mask
= dirty_mask
;
192 shadow_nx_mask
= nx_mask
;
193 shadow_x_mask
= x_mask
;
194 shadow_mt_mask
= mt_mask
;
196 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes
);
198 static int is_write_protection(struct kvm_vcpu
*vcpu
)
200 return vcpu
->arch
.cr0
& X86_CR0_WP
;
203 static int is_cpuid_PSE36(void)
208 static int is_nx(struct kvm_vcpu
*vcpu
)
210 return vcpu
->arch
.shadow_efer
& EFER_NX
;
213 static int is_present_pte(unsigned long pte
)
215 return pte
& PT_PRESENT_MASK
;
218 static int is_shadow_present_pte(u64 pte
)
220 return pte
!= shadow_trap_nonpresent_pte
221 && pte
!= shadow_notrap_nonpresent_pte
;
224 static int is_large_pte(u64 pte
)
226 return pte
& PT_PAGE_SIZE_MASK
;
229 static int is_writeble_pte(unsigned long pte
)
231 return pte
& PT_WRITABLE_MASK
;
234 static int is_dirty_pte(unsigned long pte
)
236 return pte
& shadow_dirty_mask
;
239 static int is_rmap_pte(u64 pte
)
241 return is_shadow_present_pte(pte
);
244 static pfn_t
spte_to_pfn(u64 pte
)
246 return (pte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
249 static gfn_t
pse36_gfn_delta(u32 gpte
)
251 int shift
= 32 - PT32_DIR_PSE36_SHIFT
- PAGE_SHIFT
;
253 return (gpte
& PT32_DIR_PSE36_MASK
) << shift
;
256 static void set_shadow_pte(u64
*sptep
, u64 spte
)
259 set_64bit((unsigned long *)sptep
, spte
);
261 set_64bit((unsigned long long *)sptep
, spte
);
265 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
266 struct kmem_cache
*base_cache
, int min
)
270 if (cache
->nobjs
>= min
)
272 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
273 obj
= kmem_cache_zalloc(base_cache
, GFP_KERNEL
);
276 cache
->objects
[cache
->nobjs
++] = obj
;
281 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
284 kfree(mc
->objects
[--mc
->nobjs
]);
287 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache
*cache
,
292 if (cache
->nobjs
>= min
)
294 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
295 page
= alloc_page(GFP_KERNEL
);
298 set_page_private(page
, 0);
299 cache
->objects
[cache
->nobjs
++] = page_address(page
);
304 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache
*mc
)
307 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
310 static int mmu_topup_memory_caches(struct kvm_vcpu
*vcpu
)
314 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
,
318 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
,
322 r
= mmu_topup_memory_cache_page(&vcpu
->arch
.mmu_page_cache
, 8);
325 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_page_header_cache
,
326 mmu_page_header_cache
, 4);
331 static void mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
333 mmu_free_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
);
334 mmu_free_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
);
335 mmu_free_memory_cache_page(&vcpu
->arch
.mmu_page_cache
);
336 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_header_cache
);
339 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
,
345 p
= mc
->objects
[--mc
->nobjs
];
350 static struct kvm_pte_chain
*mmu_alloc_pte_chain(struct kvm_vcpu
*vcpu
)
352 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_pte_chain_cache
,
353 sizeof(struct kvm_pte_chain
));
356 static void mmu_free_pte_chain(struct kvm_pte_chain
*pc
)
361 static struct kvm_rmap_desc
*mmu_alloc_rmap_desc(struct kvm_vcpu
*vcpu
)
363 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_rmap_desc_cache
,
364 sizeof(struct kvm_rmap_desc
));
367 static void mmu_free_rmap_desc(struct kvm_rmap_desc
*rd
)
373 * Return the pointer to the largepage write count for a given
374 * gfn, handling slots that are not large page aligned.
376 static int *slot_largepage_idx(gfn_t gfn
, struct kvm_memory_slot
*slot
)
380 idx
= (gfn
/ KVM_PAGES_PER_HPAGE
) -
381 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE
);
382 return &slot
->lpage_info
[idx
].write_count
;
385 static void account_shadowed(struct kvm
*kvm
, gfn_t gfn
)
389 write_count
= slot_largepage_idx(gfn
, gfn_to_memslot(kvm
, gfn
));
393 static void unaccount_shadowed(struct kvm
*kvm
, gfn_t gfn
)
397 write_count
= slot_largepage_idx(gfn
, gfn_to_memslot(kvm
, gfn
));
399 WARN_ON(*write_count
< 0);
402 static int has_wrprotected_page(struct kvm
*kvm
, gfn_t gfn
)
404 struct kvm_memory_slot
*slot
= gfn_to_memslot(kvm
, gfn
);
408 largepage_idx
= slot_largepage_idx(gfn
, slot
);
409 return *largepage_idx
;
415 static int host_largepage_backed(struct kvm
*kvm
, gfn_t gfn
)
417 struct vm_area_struct
*vma
;
421 addr
= gfn_to_hva(kvm
, gfn
);
422 if (kvm_is_error_hva(addr
))
425 down_read(¤t
->mm
->mmap_sem
);
426 vma
= find_vma(current
->mm
, addr
);
427 if (vma
&& is_vm_hugetlb_page(vma
))
429 up_read(¤t
->mm
->mmap_sem
);
434 static int is_largepage_backed(struct kvm_vcpu
*vcpu
, gfn_t large_gfn
)
436 struct kvm_memory_slot
*slot
;
438 if (has_wrprotected_page(vcpu
->kvm
, large_gfn
))
441 if (!host_largepage_backed(vcpu
->kvm
, large_gfn
))
444 slot
= gfn_to_memslot(vcpu
->kvm
, large_gfn
);
445 if (slot
&& slot
->dirty_bitmap
)
452 * Take gfn and return the reverse mapping to it.
453 * Note: gfn must be unaliased before this function get called
456 static unsigned long *gfn_to_rmap(struct kvm
*kvm
, gfn_t gfn
, int lpage
)
458 struct kvm_memory_slot
*slot
;
461 slot
= gfn_to_memslot(kvm
, gfn
);
463 return &slot
->rmap
[gfn
- slot
->base_gfn
];
465 idx
= (gfn
/ KVM_PAGES_PER_HPAGE
) -
466 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE
);
468 return &slot
->lpage_info
[idx
].rmap_pde
;
472 * Reverse mapping data structures:
474 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
475 * that points to page_address(page).
477 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
478 * containing more mappings.
480 static void rmap_add(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
, int lpage
)
482 struct kvm_mmu_page
*sp
;
483 struct kvm_rmap_desc
*desc
;
484 unsigned long *rmapp
;
487 if (!is_rmap_pte(*spte
))
489 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
490 sp
= page_header(__pa(spte
));
491 sp
->gfns
[spte
- sp
->spt
] = gfn
;
492 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
, lpage
);
494 rmap_printk("rmap_add: %p %llx 0->1\n", spte
, *spte
);
495 *rmapp
= (unsigned long)spte
;
496 } else if (!(*rmapp
& 1)) {
497 rmap_printk("rmap_add: %p %llx 1->many\n", spte
, *spte
);
498 desc
= mmu_alloc_rmap_desc(vcpu
);
499 desc
->shadow_ptes
[0] = (u64
*)*rmapp
;
500 desc
->shadow_ptes
[1] = spte
;
501 *rmapp
= (unsigned long)desc
| 1;
503 rmap_printk("rmap_add: %p %llx many->many\n", spte
, *spte
);
504 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
505 while (desc
->shadow_ptes
[RMAP_EXT
-1] && desc
->more
)
507 if (desc
->shadow_ptes
[RMAP_EXT
-1]) {
508 desc
->more
= mmu_alloc_rmap_desc(vcpu
);
511 for (i
= 0; desc
->shadow_ptes
[i
]; ++i
)
513 desc
->shadow_ptes
[i
] = spte
;
517 static void rmap_desc_remove_entry(unsigned long *rmapp
,
518 struct kvm_rmap_desc
*desc
,
520 struct kvm_rmap_desc
*prev_desc
)
524 for (j
= RMAP_EXT
- 1; !desc
->shadow_ptes
[j
] && j
> i
; --j
)
526 desc
->shadow_ptes
[i
] = desc
->shadow_ptes
[j
];
527 desc
->shadow_ptes
[j
] = NULL
;
530 if (!prev_desc
&& !desc
->more
)
531 *rmapp
= (unsigned long)desc
->shadow_ptes
[0];
534 prev_desc
->more
= desc
->more
;
536 *rmapp
= (unsigned long)desc
->more
| 1;
537 mmu_free_rmap_desc(desc
);
540 static void rmap_remove(struct kvm
*kvm
, u64
*spte
)
542 struct kvm_rmap_desc
*desc
;
543 struct kvm_rmap_desc
*prev_desc
;
544 struct kvm_mmu_page
*sp
;
546 unsigned long *rmapp
;
549 if (!is_rmap_pte(*spte
))
551 sp
= page_header(__pa(spte
));
552 pfn
= spte_to_pfn(*spte
);
553 if (*spte
& shadow_accessed_mask
)
554 kvm_set_pfn_accessed(pfn
);
555 if (is_writeble_pte(*spte
))
556 kvm_release_pfn_dirty(pfn
);
558 kvm_release_pfn_clean(pfn
);
559 rmapp
= gfn_to_rmap(kvm
, sp
->gfns
[spte
- sp
->spt
], is_large_pte(*spte
));
561 printk(KERN_ERR
"rmap_remove: %p %llx 0->BUG\n", spte
, *spte
);
563 } else if (!(*rmapp
& 1)) {
564 rmap_printk("rmap_remove: %p %llx 1->0\n", spte
, *spte
);
565 if ((u64
*)*rmapp
!= spte
) {
566 printk(KERN_ERR
"rmap_remove: %p %llx 1->BUG\n",
572 rmap_printk("rmap_remove: %p %llx many->many\n", spte
, *spte
);
573 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
576 for (i
= 0; i
< RMAP_EXT
&& desc
->shadow_ptes
[i
]; ++i
)
577 if (desc
->shadow_ptes
[i
] == spte
) {
578 rmap_desc_remove_entry(rmapp
,
590 static u64
*rmap_next(struct kvm
*kvm
, unsigned long *rmapp
, u64
*spte
)
592 struct kvm_rmap_desc
*desc
;
593 struct kvm_rmap_desc
*prev_desc
;
599 else if (!(*rmapp
& 1)) {
601 return (u64
*)*rmapp
;
604 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
608 for (i
= 0; i
< RMAP_EXT
&& desc
->shadow_ptes
[i
]; ++i
) {
609 if (prev_spte
== spte
)
610 return desc
->shadow_ptes
[i
];
611 prev_spte
= desc
->shadow_ptes
[i
];
618 static void rmap_write_protect(struct kvm
*kvm
, u64 gfn
)
620 unsigned long *rmapp
;
622 int write_protected
= 0;
624 gfn
= unalias_gfn(kvm
, gfn
);
625 rmapp
= gfn_to_rmap(kvm
, gfn
, 0);
627 spte
= rmap_next(kvm
, rmapp
, NULL
);
630 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
631 rmap_printk("rmap_write_protect: spte %p %llx\n", spte
, *spte
);
632 if (is_writeble_pte(*spte
)) {
633 set_shadow_pte(spte
, *spte
& ~PT_WRITABLE_MASK
);
636 spte
= rmap_next(kvm
, rmapp
, spte
);
638 if (write_protected
) {
641 spte
= rmap_next(kvm
, rmapp
, NULL
);
642 pfn
= spte_to_pfn(*spte
);
643 kvm_set_pfn_dirty(pfn
);
646 /* check for huge page mappings */
647 rmapp
= gfn_to_rmap(kvm
, gfn
, 1);
648 spte
= rmap_next(kvm
, rmapp
, NULL
);
651 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
652 BUG_ON((*spte
& (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
)) != (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
));
653 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte
, *spte
, gfn
);
654 if (is_writeble_pte(*spte
)) {
655 rmap_remove(kvm
, spte
);
657 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
661 spte
= rmap_next(kvm
, rmapp
, spte
);
665 kvm_flush_remote_tlbs(kvm
);
668 static int kvm_unmap_rmapp(struct kvm
*kvm
, unsigned long *rmapp
)
671 int need_tlb_flush
= 0;
673 while ((spte
= rmap_next(kvm
, rmapp
, NULL
))) {
674 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
675 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte
, *spte
);
676 rmap_remove(kvm
, spte
);
677 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
680 return need_tlb_flush
;
683 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
684 int (*handler
)(struct kvm
*kvm
, unsigned long *rmapp
))
690 * If mmap_sem isn't taken, we can look the memslots with only
691 * the mmu_lock by skipping over the slots with userspace_addr == 0.
693 for (i
= 0; i
< kvm
->nmemslots
; i
++) {
694 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
695 unsigned long start
= memslot
->userspace_addr
;
698 /* mmu_lock protects userspace_addr */
702 end
= start
+ (memslot
->npages
<< PAGE_SHIFT
);
703 if (hva
>= start
&& hva
< end
) {
704 gfn_t gfn_offset
= (hva
- start
) >> PAGE_SHIFT
;
705 retval
|= handler(kvm
, &memslot
->rmap
[gfn_offset
]);
706 retval
|= handler(kvm
,
707 &memslot
->lpage_info
[
709 KVM_PAGES_PER_HPAGE
].rmap_pde
);
716 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
718 return kvm_handle_hva(kvm
, hva
, kvm_unmap_rmapp
);
721 static int kvm_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
)
726 /* always return old for EPT */
727 if (!shadow_accessed_mask
)
730 spte
= rmap_next(kvm
, rmapp
, NULL
);
734 BUG_ON(!(_spte
& PT_PRESENT_MASK
));
735 _young
= _spte
& PT_ACCESSED_MASK
;
738 clear_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
740 spte
= rmap_next(kvm
, rmapp
, spte
);
745 int kvm_age_hva(struct kvm
*kvm
, unsigned long hva
)
747 return kvm_handle_hva(kvm
, hva
, kvm_age_rmapp
);
751 static int is_empty_shadow_page(u64
*spt
)
756 for (pos
= spt
, end
= pos
+ PAGE_SIZE
/ sizeof(u64
); pos
!= end
; pos
++)
757 if (is_shadow_present_pte(*pos
)) {
758 printk(KERN_ERR
"%s: %p %llx\n", __func__
,
766 static void kvm_mmu_free_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
768 ASSERT(is_empty_shadow_page(sp
->spt
));
770 __free_page(virt_to_page(sp
->spt
));
771 __free_page(virt_to_page(sp
->gfns
));
773 ++kvm
->arch
.n_free_mmu_pages
;
776 static unsigned kvm_page_table_hashfn(gfn_t gfn
)
778 return gfn
& ((1 << KVM_MMU_HASH_SHIFT
) - 1);
781 static struct kvm_mmu_page
*kvm_mmu_alloc_page(struct kvm_vcpu
*vcpu
,
784 struct kvm_mmu_page
*sp
;
786 sp
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_header_cache
, sizeof *sp
);
787 sp
->spt
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
788 sp
->gfns
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
789 set_page_private(virt_to_page(sp
->spt
), (unsigned long)sp
);
790 list_add(&sp
->link
, &vcpu
->kvm
->arch
.active_mmu_pages
);
791 ASSERT(is_empty_shadow_page(sp
->spt
));
794 sp
->parent_pte
= parent_pte
;
795 --vcpu
->kvm
->arch
.n_free_mmu_pages
;
799 static void mmu_page_add_parent_pte(struct kvm_vcpu
*vcpu
,
800 struct kvm_mmu_page
*sp
, u64
*parent_pte
)
802 struct kvm_pte_chain
*pte_chain
;
803 struct hlist_node
*node
;
808 if (!sp
->multimapped
) {
809 u64
*old
= sp
->parent_pte
;
812 sp
->parent_pte
= parent_pte
;
816 pte_chain
= mmu_alloc_pte_chain(vcpu
);
817 INIT_HLIST_HEAD(&sp
->parent_ptes
);
818 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
819 pte_chain
->parent_ptes
[0] = old
;
821 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
) {
822 if (pte_chain
->parent_ptes
[NR_PTE_CHAIN_ENTRIES
-1])
824 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
)
825 if (!pte_chain
->parent_ptes
[i
]) {
826 pte_chain
->parent_ptes
[i
] = parent_pte
;
830 pte_chain
= mmu_alloc_pte_chain(vcpu
);
832 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
833 pte_chain
->parent_ptes
[0] = parent_pte
;
836 static void mmu_page_remove_parent_pte(struct kvm_mmu_page
*sp
,
839 struct kvm_pte_chain
*pte_chain
;
840 struct hlist_node
*node
;
843 if (!sp
->multimapped
) {
844 BUG_ON(sp
->parent_pte
!= parent_pte
);
845 sp
->parent_pte
= NULL
;
848 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
849 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
850 if (!pte_chain
->parent_ptes
[i
])
852 if (pte_chain
->parent_ptes
[i
] != parent_pte
)
854 while (i
+ 1 < NR_PTE_CHAIN_ENTRIES
855 && pte_chain
->parent_ptes
[i
+ 1]) {
856 pte_chain
->parent_ptes
[i
]
857 = pte_chain
->parent_ptes
[i
+ 1];
860 pte_chain
->parent_ptes
[i
] = NULL
;
862 hlist_del(&pte_chain
->link
);
863 mmu_free_pte_chain(pte_chain
);
864 if (hlist_empty(&sp
->parent_ptes
)) {
866 sp
->parent_pte
= NULL
;
875 static void mmu_parent_walk(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
,
876 mmu_parent_walk_fn fn
)
878 struct kvm_pte_chain
*pte_chain
;
879 struct hlist_node
*node
;
880 struct kvm_mmu_page
*parent_sp
;
883 if (!sp
->multimapped
&& sp
->parent_pte
) {
884 parent_sp
= page_header(__pa(sp
->parent_pte
));
886 mmu_parent_walk(vcpu
, parent_sp
, fn
);
889 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
890 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
891 if (!pte_chain
->parent_ptes
[i
])
893 parent_sp
= page_header(__pa(pte_chain
->parent_ptes
[i
]));
895 mmu_parent_walk(vcpu
, parent_sp
, fn
);
899 static void kvm_mmu_update_unsync_bitmap(u64
*spte
)
902 struct kvm_mmu_page
*sp
= page_header(__pa(spte
));
904 index
= spte
- sp
->spt
;
905 __set_bit(index
, sp
->unsync_child_bitmap
);
906 sp
->unsync_children
= 1;
909 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page
*sp
)
911 struct kvm_pte_chain
*pte_chain
;
912 struct hlist_node
*node
;
918 if (!sp
->multimapped
) {
919 kvm_mmu_update_unsync_bitmap(sp
->parent_pte
);
923 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
924 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
925 if (!pte_chain
->parent_ptes
[i
])
927 kvm_mmu_update_unsync_bitmap(pte_chain
->parent_ptes
[i
]);
931 static int unsync_walk_fn(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
933 sp
->unsync_children
= 1;
934 kvm_mmu_update_parents_unsync(sp
);
938 static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu
*vcpu
,
939 struct kvm_mmu_page
*sp
)
941 mmu_parent_walk(vcpu
, sp
, unsync_walk_fn
);
942 kvm_mmu_update_parents_unsync(sp
);
945 static void nonpaging_prefetch_page(struct kvm_vcpu
*vcpu
,
946 struct kvm_mmu_page
*sp
)
950 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
951 sp
->spt
[i
] = shadow_trap_nonpresent_pte
;
954 static int nonpaging_sync_page(struct kvm_vcpu
*vcpu
,
955 struct kvm_mmu_page
*sp
)
960 static void nonpaging_invlpg(struct kvm_vcpu
*vcpu
, gva_t gva
)
964 #define for_each_unsync_children(bitmap, idx) \
965 for (idx = find_first_bit(bitmap, 512); \
967 idx = find_next_bit(bitmap, 512, idx+1))
969 static int mmu_unsync_walk(struct kvm_mmu_page
*sp
,
970 struct kvm_unsync_walk
*walker
)
974 if (!sp
->unsync_children
)
977 for_each_unsync_children(sp
->unsync_child_bitmap
, i
) {
978 u64 ent
= sp
->spt
[i
];
980 if (is_shadow_present_pte(ent
)) {
981 struct kvm_mmu_page
*child
;
982 child
= page_header(ent
& PT64_BASE_ADDR_MASK
);
984 if (child
->unsync_children
) {
985 ret
= mmu_unsync_walk(child
, walker
);
988 __clear_bit(i
, sp
->unsync_child_bitmap
);
992 ret
= walker
->entry(child
, walker
);
993 __clear_bit(i
, sp
->unsync_child_bitmap
);
1000 if (find_first_bit(sp
->unsync_child_bitmap
, 512) == 512)
1001 sp
->unsync_children
= 0;
1006 static struct kvm_mmu_page
*kvm_mmu_lookup_page(struct kvm
*kvm
, gfn_t gfn
)
1009 struct hlist_head
*bucket
;
1010 struct kvm_mmu_page
*sp
;
1011 struct hlist_node
*node
;
1013 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1014 index
= kvm_page_table_hashfn(gfn
);
1015 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1016 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
1017 if (sp
->gfn
== gfn
&& !sp
->role
.metaphysical
1018 && !sp
->role
.invalid
) {
1019 pgprintk("%s: found role %x\n",
1020 __func__
, sp
->role
.word
);
1026 static void kvm_unlink_unsync_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1028 WARN_ON(!sp
->unsync
);
1030 --kvm
->stat
.mmu_unsync
;
1033 static int kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
);
1035 static int kvm_sync_page(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1037 if (sp
->role
.glevels
!= vcpu
->arch
.mmu
.root_level
) {
1038 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1042 rmap_write_protect(vcpu
->kvm
, sp
->gfn
);
1043 kvm_unlink_unsync_page(vcpu
->kvm
, sp
);
1044 if (vcpu
->arch
.mmu
.sync_page(vcpu
, sp
)) {
1045 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1049 kvm_mmu_flush_tlb(vcpu
);
1053 struct sync_walker
{
1054 struct kvm_vcpu
*vcpu
;
1055 struct kvm_unsync_walk walker
;
1058 static int mmu_sync_fn(struct kvm_mmu_page
*sp
, struct kvm_unsync_walk
*walk
)
1060 struct sync_walker
*sync_walk
= container_of(walk
, struct sync_walker
,
1062 struct kvm_vcpu
*vcpu
= sync_walk
->vcpu
;
1064 kvm_sync_page(vcpu
, sp
);
1065 return (need_resched() || spin_needbreak(&vcpu
->kvm
->mmu_lock
));
1068 static void mmu_sync_children(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1070 struct sync_walker walker
= {
1071 .walker
= { .entry
= mmu_sync_fn
, },
1075 while (mmu_unsync_walk(sp
, &walker
.walker
))
1076 cond_resched_lock(&vcpu
->kvm
->mmu_lock
);
1079 static struct kvm_mmu_page
*kvm_mmu_get_page(struct kvm_vcpu
*vcpu
,
1087 union kvm_mmu_page_role role
;
1090 struct hlist_head
*bucket
;
1091 struct kvm_mmu_page
*sp
;
1092 struct hlist_node
*node
, *tmp
;
1095 role
.glevels
= vcpu
->arch
.mmu
.root_level
;
1097 role
.metaphysical
= metaphysical
;
1098 role
.access
= access
;
1099 if (vcpu
->arch
.mmu
.root_level
<= PT32_ROOT_LEVEL
) {
1100 quadrant
= gaddr
>> (PAGE_SHIFT
+ (PT64_PT_BITS
* level
));
1101 quadrant
&= (1 << ((PT32_PT_BITS
- PT64_PT_BITS
) * level
)) - 1;
1102 role
.quadrant
= quadrant
;
1104 pgprintk("%s: looking gfn %lx role %x\n", __func__
,
1106 index
= kvm_page_table_hashfn(gfn
);
1107 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1108 hlist_for_each_entry_safe(sp
, node
, tmp
, bucket
, hash_link
)
1109 if (sp
->gfn
== gfn
) {
1111 if (kvm_sync_page(vcpu
, sp
))
1114 if (sp
->role
.word
!= role
.word
)
1117 mmu_page_add_parent_pte(vcpu
, sp
, parent_pte
);
1118 if (sp
->unsync_children
) {
1119 set_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
);
1120 kvm_mmu_mark_parents_unsync(vcpu
, sp
);
1122 pgprintk("%s: found\n", __func__
);
1125 ++vcpu
->kvm
->stat
.mmu_cache_miss
;
1126 sp
= kvm_mmu_alloc_page(vcpu
, parent_pte
);
1129 pgprintk("%s: adding gfn %lx role %x\n", __func__
, gfn
, role
.word
);
1132 hlist_add_head(&sp
->hash_link
, bucket
);
1133 if (!metaphysical
) {
1134 rmap_write_protect(vcpu
->kvm
, gfn
);
1135 account_shadowed(vcpu
->kvm
, gfn
);
1137 if (shadow_trap_nonpresent_pte
!= shadow_notrap_nonpresent_pte
)
1138 vcpu
->arch
.mmu
.prefetch_page(vcpu
, sp
);
1140 nonpaging_prefetch_page(vcpu
, sp
);
1144 static int walk_shadow(struct kvm_shadow_walk
*walker
,
1145 struct kvm_vcpu
*vcpu
, u64 addr
)
1153 shadow_addr
= vcpu
->arch
.mmu
.root_hpa
;
1154 level
= vcpu
->arch
.mmu
.shadow_root_level
;
1155 if (level
== PT32E_ROOT_LEVEL
) {
1156 shadow_addr
= vcpu
->arch
.mmu
.pae_root
[(addr
>> 30) & 3];
1157 shadow_addr
&= PT64_BASE_ADDR_MASK
;
1161 while (level
>= PT_PAGE_TABLE_LEVEL
) {
1162 index
= SHADOW_PT_INDEX(addr
, level
);
1163 sptep
= ((u64
*)__va(shadow_addr
)) + index
;
1164 r
= walker
->entry(walker
, vcpu
, addr
, sptep
, level
);
1167 shadow_addr
= *sptep
& PT64_BASE_ADDR_MASK
;
1173 static void kvm_mmu_page_unlink_children(struct kvm
*kvm
,
1174 struct kvm_mmu_page
*sp
)
1182 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
) {
1183 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1184 if (is_shadow_present_pte(pt
[i
]))
1185 rmap_remove(kvm
, &pt
[i
]);
1186 pt
[i
] = shadow_trap_nonpresent_pte
;
1191 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1194 if (is_shadow_present_pte(ent
)) {
1195 if (!is_large_pte(ent
)) {
1196 ent
&= PT64_BASE_ADDR_MASK
;
1197 mmu_page_remove_parent_pte(page_header(ent
),
1201 rmap_remove(kvm
, &pt
[i
]);
1204 pt
[i
] = shadow_trap_nonpresent_pte
;
1208 static void kvm_mmu_put_page(struct kvm_mmu_page
*sp
, u64
*parent_pte
)
1210 mmu_page_remove_parent_pte(sp
, parent_pte
);
1213 static void kvm_mmu_reset_last_pte_updated(struct kvm
*kvm
)
1217 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
1219 kvm
->vcpus
[i
]->arch
.last_pte_updated
= NULL
;
1222 static void kvm_mmu_unlink_parents(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1226 while (sp
->multimapped
|| sp
->parent_pte
) {
1227 if (!sp
->multimapped
)
1228 parent_pte
= sp
->parent_pte
;
1230 struct kvm_pte_chain
*chain
;
1232 chain
= container_of(sp
->parent_ptes
.first
,
1233 struct kvm_pte_chain
, link
);
1234 parent_pte
= chain
->parent_ptes
[0];
1236 BUG_ON(!parent_pte
);
1237 kvm_mmu_put_page(sp
, parent_pte
);
1238 set_shadow_pte(parent_pte
, shadow_trap_nonpresent_pte
);
1243 struct kvm_unsync_walk walker
;
1248 static int mmu_zap_fn(struct kvm_mmu_page
*sp
, struct kvm_unsync_walk
*walk
)
1250 struct zap_walker
*zap_walk
= container_of(walk
, struct zap_walker
,
1252 kvm_mmu_zap_page(zap_walk
->kvm
, sp
);
1253 zap_walk
->zapped
= 1;
1257 static int mmu_zap_unsync_children(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1259 struct zap_walker walker
= {
1260 .walker
= { .entry
= mmu_zap_fn
, },
1265 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
)
1267 mmu_unsync_walk(sp
, &walker
.walker
);
1268 return walker
.zapped
;
1271 static int kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1274 ++kvm
->stat
.mmu_shadow_zapped
;
1275 ret
= mmu_zap_unsync_children(kvm
, sp
);
1276 kvm_mmu_page_unlink_children(kvm
, sp
);
1277 kvm_mmu_unlink_parents(kvm
, sp
);
1278 kvm_flush_remote_tlbs(kvm
);
1279 if (!sp
->role
.invalid
&& !sp
->role
.metaphysical
)
1280 unaccount_shadowed(kvm
, sp
->gfn
);
1282 kvm_unlink_unsync_page(kvm
, sp
);
1283 if (!sp
->root_count
) {
1284 hlist_del(&sp
->hash_link
);
1285 kvm_mmu_free_page(kvm
, sp
);
1287 sp
->role
.invalid
= 1;
1288 list_move(&sp
->link
, &kvm
->arch
.active_mmu_pages
);
1289 kvm_reload_remote_mmus(kvm
);
1291 kvm_mmu_reset_last_pte_updated(kvm
);
1296 * Changing the number of mmu pages allocated to the vm
1297 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1299 void kvm_mmu_change_mmu_pages(struct kvm
*kvm
, unsigned int kvm_nr_mmu_pages
)
1302 * If we set the number of mmu pages to be smaller be than the
1303 * number of actived pages , we must to free some mmu pages before we
1307 if ((kvm
->arch
.n_alloc_mmu_pages
- kvm
->arch
.n_free_mmu_pages
) >
1309 int n_used_mmu_pages
= kvm
->arch
.n_alloc_mmu_pages
1310 - kvm
->arch
.n_free_mmu_pages
;
1312 while (n_used_mmu_pages
> kvm_nr_mmu_pages
) {
1313 struct kvm_mmu_page
*page
;
1315 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
1316 struct kvm_mmu_page
, link
);
1317 kvm_mmu_zap_page(kvm
, page
);
1320 kvm
->arch
.n_free_mmu_pages
= 0;
1323 kvm
->arch
.n_free_mmu_pages
+= kvm_nr_mmu_pages
1324 - kvm
->arch
.n_alloc_mmu_pages
;
1326 kvm
->arch
.n_alloc_mmu_pages
= kvm_nr_mmu_pages
;
1329 static int kvm_mmu_unprotect_page(struct kvm
*kvm
, gfn_t gfn
)
1332 struct hlist_head
*bucket
;
1333 struct kvm_mmu_page
*sp
;
1334 struct hlist_node
*node
, *n
;
1337 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1339 index
= kvm_page_table_hashfn(gfn
);
1340 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1341 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
)
1342 if (sp
->gfn
== gfn
&& !sp
->role
.metaphysical
) {
1343 pgprintk("%s: gfn %lx role %x\n", __func__
, gfn
,
1346 if (kvm_mmu_zap_page(kvm
, sp
))
1352 static void mmu_unshadow(struct kvm
*kvm
, gfn_t gfn
)
1354 struct kvm_mmu_page
*sp
;
1356 while ((sp
= kvm_mmu_lookup_page(kvm
, gfn
)) != NULL
) {
1357 pgprintk("%s: zap %lx %x\n", __func__
, gfn
, sp
->role
.word
);
1358 kvm_mmu_zap_page(kvm
, sp
);
1362 static void page_header_update_slot(struct kvm
*kvm
, void *pte
, gfn_t gfn
)
1364 int slot
= memslot_id(kvm
, gfn_to_memslot(kvm
, gfn
));
1365 struct kvm_mmu_page
*sp
= page_header(__pa(pte
));
1367 __set_bit(slot
, &sp
->slot_bitmap
);
1370 static void mmu_convert_notrap(struct kvm_mmu_page
*sp
)
1375 if (shadow_trap_nonpresent_pte
== shadow_notrap_nonpresent_pte
)
1378 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1379 if (pt
[i
] == shadow_notrap_nonpresent_pte
)
1380 set_shadow_pte(&pt
[i
], shadow_trap_nonpresent_pte
);
1384 struct page
*gva_to_page(struct kvm_vcpu
*vcpu
, gva_t gva
)
1388 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
);
1390 if (gpa
== UNMAPPED_GVA
)
1393 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1399 * The function is based on mtrr_type_lookup() in
1400 * arch/x86/kernel/cpu/mtrr/generic.c
1402 static int get_mtrr_type(struct mtrr_state_type
*mtrr_state
,
1407 u8 prev_match
, curr_match
;
1408 int num_var_ranges
= KVM_NR_VAR_MTRR
;
1410 if (!mtrr_state
->enabled
)
1413 /* Make end inclusive end, instead of exclusive */
1416 /* Look in fixed ranges. Just return the type as per start */
1417 if (mtrr_state
->have_fixed
&& (start
< 0x100000)) {
1420 if (start
< 0x80000) {
1422 idx
+= (start
>> 16);
1423 return mtrr_state
->fixed_ranges
[idx
];
1424 } else if (start
< 0xC0000) {
1426 idx
+= ((start
- 0x80000) >> 14);
1427 return mtrr_state
->fixed_ranges
[idx
];
1428 } else if (start
< 0x1000000) {
1430 idx
+= ((start
- 0xC0000) >> 12);
1431 return mtrr_state
->fixed_ranges
[idx
];
1436 * Look in variable ranges
1437 * Look of multiple ranges matching this address and pick type
1438 * as per MTRR precedence
1440 if (!(mtrr_state
->enabled
& 2))
1441 return mtrr_state
->def_type
;
1444 for (i
= 0; i
< num_var_ranges
; ++i
) {
1445 unsigned short start_state
, end_state
;
1447 if (!(mtrr_state
->var_ranges
[i
].mask_lo
& (1 << 11)))
1450 base
= (((u64
)mtrr_state
->var_ranges
[i
].base_hi
) << 32) +
1451 (mtrr_state
->var_ranges
[i
].base_lo
& PAGE_MASK
);
1452 mask
= (((u64
)mtrr_state
->var_ranges
[i
].mask_hi
) << 32) +
1453 (mtrr_state
->var_ranges
[i
].mask_lo
& PAGE_MASK
);
1455 start_state
= ((start
& mask
) == (base
& mask
));
1456 end_state
= ((end
& mask
) == (base
& mask
));
1457 if (start_state
!= end_state
)
1460 if ((start
& mask
) != (base
& mask
))
1463 curr_match
= mtrr_state
->var_ranges
[i
].base_lo
& 0xff;
1464 if (prev_match
== 0xFF) {
1465 prev_match
= curr_match
;
1469 if (prev_match
== MTRR_TYPE_UNCACHABLE
||
1470 curr_match
== MTRR_TYPE_UNCACHABLE
)
1471 return MTRR_TYPE_UNCACHABLE
;
1473 if ((prev_match
== MTRR_TYPE_WRBACK
&&
1474 curr_match
== MTRR_TYPE_WRTHROUGH
) ||
1475 (prev_match
== MTRR_TYPE_WRTHROUGH
&&
1476 curr_match
== MTRR_TYPE_WRBACK
)) {
1477 prev_match
= MTRR_TYPE_WRTHROUGH
;
1478 curr_match
= MTRR_TYPE_WRTHROUGH
;
1481 if (prev_match
!= curr_match
)
1482 return MTRR_TYPE_UNCACHABLE
;
1485 if (prev_match
!= 0xFF)
1488 return mtrr_state
->def_type
;
1491 static u8
get_memory_type(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
1495 mtrr
= get_mtrr_type(&vcpu
->arch
.mtrr_state
, gfn
<< PAGE_SHIFT
,
1496 (gfn
<< PAGE_SHIFT
) + PAGE_SIZE
);
1497 if (mtrr
== 0xfe || mtrr
== 0xff)
1498 mtrr
= MTRR_TYPE_WRBACK
;
1502 static int kvm_unsync_page(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
)
1505 struct hlist_head
*bucket
;
1506 struct kvm_mmu_page
*s
;
1507 struct hlist_node
*node
, *n
;
1509 index
= kvm_page_table_hashfn(sp
->gfn
);
1510 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1511 /* don't unsync if pagetable is shadowed with multiple roles */
1512 hlist_for_each_entry_safe(s
, node
, n
, bucket
, hash_link
) {
1513 if (s
->gfn
!= sp
->gfn
|| s
->role
.metaphysical
)
1515 if (s
->role
.word
!= sp
->role
.word
)
1518 kvm_mmu_mark_parents_unsync(vcpu
, sp
);
1519 ++vcpu
->kvm
->stat
.mmu_unsync
;
1521 mmu_convert_notrap(sp
);
1525 static int mmu_need_write_protect(struct kvm_vcpu
*vcpu
, gfn_t gfn
,
1528 struct kvm_mmu_page
*shadow
;
1530 shadow
= kvm_mmu_lookup_page(vcpu
->kvm
, gfn
);
1532 if (shadow
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
1536 if (can_unsync
&& oos_shadow
)
1537 return kvm_unsync_page(vcpu
, shadow
);
1543 static int set_spte(struct kvm_vcpu
*vcpu
, u64
*shadow_pte
,
1544 unsigned pte_access
, int user_fault
,
1545 int write_fault
, int dirty
, int largepage
,
1546 gfn_t gfn
, pfn_t pfn
, bool speculative
,
1551 u64 mt_mask
= shadow_mt_mask
;
1554 * We don't set the accessed bit, since we sometimes want to see
1555 * whether the guest actually used the pte (in order to detect
1558 spte
= shadow_base_present_pte
| shadow_dirty_mask
;
1560 spte
|= shadow_accessed_mask
;
1562 pte_access
&= ~ACC_WRITE_MASK
;
1563 if (pte_access
& ACC_EXEC_MASK
)
1564 spte
|= shadow_x_mask
;
1566 spte
|= shadow_nx_mask
;
1567 if (pte_access
& ACC_USER_MASK
)
1568 spte
|= shadow_user_mask
;
1570 spte
|= PT_PAGE_SIZE_MASK
;
1572 mt_mask
= get_memory_type(vcpu
, gfn
) <<
1573 kvm_x86_ops
->get_mt_mask_shift();
1577 spte
|= (u64
)pfn
<< PAGE_SHIFT
;
1579 if ((pte_access
& ACC_WRITE_MASK
)
1580 || (write_fault
&& !is_write_protection(vcpu
) && !user_fault
)) {
1582 if (largepage
&& has_wrprotected_page(vcpu
->kvm
, gfn
)) {
1584 spte
= shadow_trap_nonpresent_pte
;
1588 spte
|= PT_WRITABLE_MASK
;
1590 if (mmu_need_write_protect(vcpu
, gfn
, can_unsync
)) {
1591 pgprintk("%s: found shadow page for %lx, marking ro\n",
1594 pte_access
&= ~ACC_WRITE_MASK
;
1595 if (is_writeble_pte(spte
))
1596 spte
&= ~PT_WRITABLE_MASK
;
1600 if (pte_access
& ACC_WRITE_MASK
)
1601 mark_page_dirty(vcpu
->kvm
, gfn
);
1604 set_shadow_pte(shadow_pte
, spte
);
1608 static void mmu_set_spte(struct kvm_vcpu
*vcpu
, u64
*shadow_pte
,
1609 unsigned pt_access
, unsigned pte_access
,
1610 int user_fault
, int write_fault
, int dirty
,
1611 int *ptwrite
, int largepage
, gfn_t gfn
,
1612 pfn_t pfn
, bool speculative
)
1614 int was_rmapped
= 0;
1615 int was_writeble
= is_writeble_pte(*shadow_pte
);
1617 pgprintk("%s: spte %llx access %x write_fault %d"
1618 " user_fault %d gfn %lx\n",
1619 __func__
, *shadow_pte
, pt_access
,
1620 write_fault
, user_fault
, gfn
);
1622 if (is_rmap_pte(*shadow_pte
)) {
1624 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1625 * the parent of the now unreachable PTE.
1627 if (largepage
&& !is_large_pte(*shadow_pte
)) {
1628 struct kvm_mmu_page
*child
;
1629 u64 pte
= *shadow_pte
;
1631 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
1632 mmu_page_remove_parent_pte(child
, shadow_pte
);
1633 } else if (pfn
!= spte_to_pfn(*shadow_pte
)) {
1634 pgprintk("hfn old %lx new %lx\n",
1635 spte_to_pfn(*shadow_pte
), pfn
);
1636 rmap_remove(vcpu
->kvm
, shadow_pte
);
1639 was_rmapped
= is_large_pte(*shadow_pte
);
1644 if (set_spte(vcpu
, shadow_pte
, pte_access
, user_fault
, write_fault
,
1645 dirty
, largepage
, gfn
, pfn
, speculative
, true)) {
1648 kvm_x86_ops
->tlb_flush(vcpu
);
1651 pgprintk("%s: setting spte %llx\n", __func__
, *shadow_pte
);
1652 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1653 is_large_pte(*shadow_pte
)? "2MB" : "4kB",
1654 is_present_pte(*shadow_pte
)?"RW":"R", gfn
,
1655 *shadow_pte
, shadow_pte
);
1656 if (!was_rmapped
&& is_large_pte(*shadow_pte
))
1657 ++vcpu
->kvm
->stat
.lpages
;
1659 page_header_update_slot(vcpu
->kvm
, shadow_pte
, gfn
);
1661 rmap_add(vcpu
, shadow_pte
, gfn
, largepage
);
1662 if (!is_rmap_pte(*shadow_pte
))
1663 kvm_release_pfn_clean(pfn
);
1666 kvm_release_pfn_dirty(pfn
);
1668 kvm_release_pfn_clean(pfn
);
1671 vcpu
->arch
.last_pte_updated
= shadow_pte
;
1672 vcpu
->arch
.last_pte_gfn
= gfn
;
1676 static void nonpaging_new_cr3(struct kvm_vcpu
*vcpu
)
1680 struct direct_shadow_walk
{
1681 struct kvm_shadow_walk walker
;
1688 static int direct_map_entry(struct kvm_shadow_walk
*_walk
,
1689 struct kvm_vcpu
*vcpu
,
1690 u64 addr
, u64
*sptep
, int level
)
1692 struct direct_shadow_walk
*walk
=
1693 container_of(_walk
, struct direct_shadow_walk
, walker
);
1694 struct kvm_mmu_page
*sp
;
1696 gfn_t gfn
= addr
>> PAGE_SHIFT
;
1698 if (level
== PT_PAGE_TABLE_LEVEL
1699 || (walk
->largepage
&& level
== PT_DIRECTORY_LEVEL
)) {
1700 mmu_set_spte(vcpu
, sptep
, ACC_ALL
, ACC_ALL
,
1701 0, walk
->write
, 1, &walk
->pt_write
,
1702 walk
->largepage
, gfn
, walk
->pfn
, false);
1703 ++vcpu
->stat
.pf_fixed
;
1707 if (*sptep
== shadow_trap_nonpresent_pte
) {
1708 pseudo_gfn
= (addr
& PT64_DIR_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
1709 sp
= kvm_mmu_get_page(vcpu
, pseudo_gfn
, (gva_t
)addr
, level
- 1,
1712 pgprintk("nonpaging_map: ENOMEM\n");
1713 kvm_release_pfn_clean(walk
->pfn
);
1717 set_shadow_pte(sptep
,
1719 | PT_PRESENT_MASK
| PT_WRITABLE_MASK
1720 | shadow_user_mask
| shadow_x_mask
);
1725 static int __direct_map(struct kvm_vcpu
*vcpu
, gpa_t v
, int write
,
1726 int largepage
, gfn_t gfn
, pfn_t pfn
)
1729 struct direct_shadow_walk walker
= {
1730 .walker
= { .entry
= direct_map_entry
, },
1732 .largepage
= largepage
,
1737 r
= walk_shadow(&walker
.walker
, vcpu
, gfn
<< PAGE_SHIFT
);
1740 return walker
.pt_write
;
1743 static int nonpaging_map(struct kvm_vcpu
*vcpu
, gva_t v
, int write
, gfn_t gfn
)
1748 unsigned long mmu_seq
;
1750 if (is_largepage_backed(vcpu
, gfn
& ~(KVM_PAGES_PER_HPAGE
-1))) {
1751 gfn
&= ~(KVM_PAGES_PER_HPAGE
-1);
1755 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1757 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
1760 if (is_error_pfn(pfn
)) {
1761 kvm_release_pfn_clean(pfn
);
1765 spin_lock(&vcpu
->kvm
->mmu_lock
);
1766 if (mmu_notifier_retry(vcpu
, mmu_seq
))
1768 kvm_mmu_free_some_pages(vcpu
);
1769 r
= __direct_map(vcpu
, v
, write
, largepage
, gfn
, pfn
);
1770 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1776 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1777 kvm_release_pfn_clean(pfn
);
1782 static void mmu_free_roots(struct kvm_vcpu
*vcpu
)
1785 struct kvm_mmu_page
*sp
;
1787 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
1789 spin_lock(&vcpu
->kvm
->mmu_lock
);
1790 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1791 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1793 sp
= page_header(root
);
1795 if (!sp
->root_count
&& sp
->role
.invalid
)
1796 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1797 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1798 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1801 for (i
= 0; i
< 4; ++i
) {
1802 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1805 root
&= PT64_BASE_ADDR_MASK
;
1806 sp
= page_header(root
);
1808 if (!sp
->root_count
&& sp
->role
.invalid
)
1809 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1811 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
1813 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1814 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1817 static void mmu_alloc_roots(struct kvm_vcpu
*vcpu
)
1821 struct kvm_mmu_page
*sp
;
1822 int metaphysical
= 0;
1824 root_gfn
= vcpu
->arch
.cr3
>> PAGE_SHIFT
;
1826 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1827 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1829 ASSERT(!VALID_PAGE(root
));
1832 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, 0,
1833 PT64_ROOT_LEVEL
, metaphysical
,
1835 root
= __pa(sp
->spt
);
1837 vcpu
->arch
.mmu
.root_hpa
= root
;
1840 metaphysical
= !is_paging(vcpu
);
1843 for (i
= 0; i
< 4; ++i
) {
1844 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1846 ASSERT(!VALID_PAGE(root
));
1847 if (vcpu
->arch
.mmu
.root_level
== PT32E_ROOT_LEVEL
) {
1848 if (!is_present_pte(vcpu
->arch
.pdptrs
[i
])) {
1849 vcpu
->arch
.mmu
.pae_root
[i
] = 0;
1852 root_gfn
= vcpu
->arch
.pdptrs
[i
] >> PAGE_SHIFT
;
1853 } else if (vcpu
->arch
.mmu
.root_level
== 0)
1855 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, i
<< 30,
1856 PT32_ROOT_LEVEL
, metaphysical
,
1858 root
= __pa(sp
->spt
);
1860 vcpu
->arch
.mmu
.pae_root
[i
] = root
| PT_PRESENT_MASK
;
1862 vcpu
->arch
.mmu
.root_hpa
= __pa(vcpu
->arch
.mmu
.pae_root
);
1865 static void mmu_sync_roots(struct kvm_vcpu
*vcpu
)
1868 struct kvm_mmu_page
*sp
;
1870 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
1872 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1873 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1874 sp
= page_header(root
);
1875 mmu_sync_children(vcpu
, sp
);
1878 for (i
= 0; i
< 4; ++i
) {
1879 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1882 root
&= PT64_BASE_ADDR_MASK
;
1883 sp
= page_header(root
);
1884 mmu_sync_children(vcpu
, sp
);
1889 void kvm_mmu_sync_roots(struct kvm_vcpu
*vcpu
)
1891 spin_lock(&vcpu
->kvm
->mmu_lock
);
1892 mmu_sync_roots(vcpu
);
1893 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1896 static gpa_t
nonpaging_gva_to_gpa(struct kvm_vcpu
*vcpu
, gva_t vaddr
)
1901 static int nonpaging_page_fault(struct kvm_vcpu
*vcpu
, gva_t gva
,
1907 pgprintk("%s: gva %lx error %x\n", __func__
, gva
, error_code
);
1908 r
= mmu_topup_memory_caches(vcpu
);
1913 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1915 gfn
= gva
>> PAGE_SHIFT
;
1917 return nonpaging_map(vcpu
, gva
& PAGE_MASK
,
1918 error_code
& PFERR_WRITE_MASK
, gfn
);
1921 static int tdp_page_fault(struct kvm_vcpu
*vcpu
, gva_t gpa
,
1927 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1928 unsigned long mmu_seq
;
1931 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1933 r
= mmu_topup_memory_caches(vcpu
);
1937 if (is_largepage_backed(vcpu
, gfn
& ~(KVM_PAGES_PER_HPAGE
-1))) {
1938 gfn
&= ~(KVM_PAGES_PER_HPAGE
-1);
1941 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1943 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
1944 if (is_error_pfn(pfn
)) {
1945 kvm_release_pfn_clean(pfn
);
1948 spin_lock(&vcpu
->kvm
->mmu_lock
);
1949 if (mmu_notifier_retry(vcpu
, mmu_seq
))
1951 kvm_mmu_free_some_pages(vcpu
);
1952 r
= __direct_map(vcpu
, gpa
, error_code
& PFERR_WRITE_MASK
,
1953 largepage
, gfn
, pfn
);
1954 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1959 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1960 kvm_release_pfn_clean(pfn
);
1964 static void nonpaging_free(struct kvm_vcpu
*vcpu
)
1966 mmu_free_roots(vcpu
);
1969 static int nonpaging_init_context(struct kvm_vcpu
*vcpu
)
1971 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1973 context
->new_cr3
= nonpaging_new_cr3
;
1974 context
->page_fault
= nonpaging_page_fault
;
1975 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
1976 context
->free
= nonpaging_free
;
1977 context
->prefetch_page
= nonpaging_prefetch_page
;
1978 context
->sync_page
= nonpaging_sync_page
;
1979 context
->invlpg
= nonpaging_invlpg
;
1980 context
->root_level
= 0;
1981 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
1982 context
->root_hpa
= INVALID_PAGE
;
1986 void kvm_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
1988 ++vcpu
->stat
.tlb_flush
;
1989 kvm_x86_ops
->tlb_flush(vcpu
);
1992 static void paging_new_cr3(struct kvm_vcpu
*vcpu
)
1994 pgprintk("%s: cr3 %lx\n", __func__
, vcpu
->arch
.cr3
);
1995 mmu_free_roots(vcpu
);
1998 static void inject_page_fault(struct kvm_vcpu
*vcpu
,
2002 kvm_inject_page_fault(vcpu
, addr
, err_code
);
2005 static void paging_free(struct kvm_vcpu
*vcpu
)
2007 nonpaging_free(vcpu
);
2011 #include "paging_tmpl.h"
2015 #include "paging_tmpl.h"
2018 static int paging64_init_context_common(struct kvm_vcpu
*vcpu
, int level
)
2020 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2022 ASSERT(is_pae(vcpu
));
2023 context
->new_cr3
= paging_new_cr3
;
2024 context
->page_fault
= paging64_page_fault
;
2025 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2026 context
->prefetch_page
= paging64_prefetch_page
;
2027 context
->sync_page
= paging64_sync_page
;
2028 context
->invlpg
= paging64_invlpg
;
2029 context
->free
= paging_free
;
2030 context
->root_level
= level
;
2031 context
->shadow_root_level
= level
;
2032 context
->root_hpa
= INVALID_PAGE
;
2036 static int paging64_init_context(struct kvm_vcpu
*vcpu
)
2038 return paging64_init_context_common(vcpu
, PT64_ROOT_LEVEL
);
2041 static int paging32_init_context(struct kvm_vcpu
*vcpu
)
2043 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2045 context
->new_cr3
= paging_new_cr3
;
2046 context
->page_fault
= paging32_page_fault
;
2047 context
->gva_to_gpa
= paging32_gva_to_gpa
;
2048 context
->free
= paging_free
;
2049 context
->prefetch_page
= paging32_prefetch_page
;
2050 context
->sync_page
= paging32_sync_page
;
2051 context
->invlpg
= paging32_invlpg
;
2052 context
->root_level
= PT32_ROOT_LEVEL
;
2053 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
2054 context
->root_hpa
= INVALID_PAGE
;
2058 static int paging32E_init_context(struct kvm_vcpu
*vcpu
)
2060 return paging64_init_context_common(vcpu
, PT32E_ROOT_LEVEL
);
2063 static int init_kvm_tdp_mmu(struct kvm_vcpu
*vcpu
)
2065 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
2067 context
->new_cr3
= nonpaging_new_cr3
;
2068 context
->page_fault
= tdp_page_fault
;
2069 context
->free
= nonpaging_free
;
2070 context
->prefetch_page
= nonpaging_prefetch_page
;
2071 context
->sync_page
= nonpaging_sync_page
;
2072 context
->invlpg
= nonpaging_invlpg
;
2073 context
->shadow_root_level
= kvm_x86_ops
->get_tdp_level();
2074 context
->root_hpa
= INVALID_PAGE
;
2076 if (!is_paging(vcpu
)) {
2077 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
2078 context
->root_level
= 0;
2079 } else if (is_long_mode(vcpu
)) {
2080 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2081 context
->root_level
= PT64_ROOT_LEVEL
;
2082 } else if (is_pae(vcpu
)) {
2083 context
->gva_to_gpa
= paging64_gva_to_gpa
;
2084 context
->root_level
= PT32E_ROOT_LEVEL
;
2086 context
->gva_to_gpa
= paging32_gva_to_gpa
;
2087 context
->root_level
= PT32_ROOT_LEVEL
;
2093 static int init_kvm_softmmu(struct kvm_vcpu
*vcpu
)
2096 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2098 if (!is_paging(vcpu
))
2099 return nonpaging_init_context(vcpu
);
2100 else if (is_long_mode(vcpu
))
2101 return paging64_init_context(vcpu
);
2102 else if (is_pae(vcpu
))
2103 return paging32E_init_context(vcpu
);
2105 return paging32_init_context(vcpu
);
2108 static int init_kvm_mmu(struct kvm_vcpu
*vcpu
)
2110 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
2113 return init_kvm_tdp_mmu(vcpu
);
2115 return init_kvm_softmmu(vcpu
);
2118 static void destroy_kvm_mmu(struct kvm_vcpu
*vcpu
)
2121 if (VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
)) {
2122 vcpu
->arch
.mmu
.free(vcpu
);
2123 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
2127 int kvm_mmu_reset_context(struct kvm_vcpu
*vcpu
)
2129 destroy_kvm_mmu(vcpu
);
2130 return init_kvm_mmu(vcpu
);
2132 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context
);
2134 int kvm_mmu_load(struct kvm_vcpu
*vcpu
)
2138 r
= mmu_topup_memory_caches(vcpu
);
2141 spin_lock(&vcpu
->kvm
->mmu_lock
);
2142 kvm_mmu_free_some_pages(vcpu
);
2143 mmu_alloc_roots(vcpu
);
2144 mmu_sync_roots(vcpu
);
2145 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2146 kvm_x86_ops
->set_cr3(vcpu
, vcpu
->arch
.mmu
.root_hpa
);
2147 kvm_mmu_flush_tlb(vcpu
);
2151 EXPORT_SYMBOL_GPL(kvm_mmu_load
);
2153 void kvm_mmu_unload(struct kvm_vcpu
*vcpu
)
2155 mmu_free_roots(vcpu
);
2158 static void mmu_pte_write_zap_pte(struct kvm_vcpu
*vcpu
,
2159 struct kvm_mmu_page
*sp
,
2163 struct kvm_mmu_page
*child
;
2166 if (is_shadow_present_pte(pte
)) {
2167 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
||
2169 rmap_remove(vcpu
->kvm
, spte
);
2171 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
2172 mmu_page_remove_parent_pte(child
, spte
);
2175 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
2176 if (is_large_pte(pte
))
2177 --vcpu
->kvm
->stat
.lpages
;
2180 static void mmu_pte_write_new_pte(struct kvm_vcpu
*vcpu
,
2181 struct kvm_mmu_page
*sp
,
2185 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
) {
2186 if (!vcpu
->arch
.update_pte
.largepage
||
2187 sp
->role
.glevels
== PT32_ROOT_LEVEL
) {
2188 ++vcpu
->kvm
->stat
.mmu_pde_zapped
;
2193 ++vcpu
->kvm
->stat
.mmu_pte_updated
;
2194 if (sp
->role
.glevels
== PT32_ROOT_LEVEL
)
2195 paging32_update_pte(vcpu
, sp
, spte
, new);
2197 paging64_update_pte(vcpu
, sp
, spte
, new);
2200 static bool need_remote_flush(u64 old
, u64
new)
2202 if (!is_shadow_present_pte(old
))
2204 if (!is_shadow_present_pte(new))
2206 if ((old
^ new) & PT64_BASE_ADDR_MASK
)
2208 old
^= PT64_NX_MASK
;
2209 new ^= PT64_NX_MASK
;
2210 return (old
& ~new & PT64_PERM_MASK
) != 0;
2213 static void mmu_pte_write_flush_tlb(struct kvm_vcpu
*vcpu
, u64 old
, u64
new)
2215 if (need_remote_flush(old
, new))
2216 kvm_flush_remote_tlbs(vcpu
->kvm
);
2218 kvm_mmu_flush_tlb(vcpu
);
2221 static bool last_updated_pte_accessed(struct kvm_vcpu
*vcpu
)
2223 u64
*spte
= vcpu
->arch
.last_pte_updated
;
2225 return !!(spte
&& (*spte
& shadow_accessed_mask
));
2228 static void mmu_guess_page_from_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2229 const u8
*new, int bytes
)
2236 vcpu
->arch
.update_pte
.largepage
= 0;
2238 if (bytes
!= 4 && bytes
!= 8)
2242 * Assume that the pte write on a page table of the same type
2243 * as the current vcpu paging mode. This is nearly always true
2244 * (might be false while changing modes). Note it is verified later
2248 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2249 if ((bytes
== 4) && (gpa
% 4 == 0)) {
2250 r
= kvm_read_guest(vcpu
->kvm
, gpa
& ~(u64
)7, &gpte
, 8);
2253 memcpy((void *)&gpte
+ (gpa
% 8), new, 4);
2254 } else if ((bytes
== 8) && (gpa
% 8 == 0)) {
2255 memcpy((void *)&gpte
, new, 8);
2258 if ((bytes
== 4) && (gpa
% 4 == 0))
2259 memcpy((void *)&gpte
, new, 4);
2261 if (!is_present_pte(gpte
))
2263 gfn
= (gpte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
2265 if (is_large_pte(gpte
) && is_largepage_backed(vcpu
, gfn
)) {
2266 gfn
&= ~(KVM_PAGES_PER_HPAGE
-1);
2267 vcpu
->arch
.update_pte
.largepage
= 1;
2269 vcpu
->arch
.update_pte
.mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
2271 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
2273 if (is_error_pfn(pfn
)) {
2274 kvm_release_pfn_clean(pfn
);
2277 vcpu
->arch
.update_pte
.gfn
= gfn
;
2278 vcpu
->arch
.update_pte
.pfn
= pfn
;
2281 static void kvm_mmu_access_page(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
2283 u64
*spte
= vcpu
->arch
.last_pte_updated
;
2286 && vcpu
->arch
.last_pte_gfn
== gfn
2287 && shadow_accessed_mask
2288 && !(*spte
& shadow_accessed_mask
)
2289 && is_shadow_present_pte(*spte
))
2290 set_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
2293 void kvm_mmu_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2294 const u8
*new, int bytes
)
2296 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
2297 struct kvm_mmu_page
*sp
;
2298 struct hlist_node
*node
, *n
;
2299 struct hlist_head
*bucket
;
2303 unsigned offset
= offset_in_page(gpa
);
2305 unsigned page_offset
;
2306 unsigned misaligned
;
2313 pgprintk("%s: gpa %llx bytes %d\n", __func__
, gpa
, bytes
);
2314 mmu_guess_page_from_pte_write(vcpu
, gpa
, new, bytes
);
2315 spin_lock(&vcpu
->kvm
->mmu_lock
);
2316 kvm_mmu_access_page(vcpu
, gfn
);
2317 kvm_mmu_free_some_pages(vcpu
);
2318 ++vcpu
->kvm
->stat
.mmu_pte_write
;
2319 kvm_mmu_audit(vcpu
, "pre pte write");
2320 if (gfn
== vcpu
->arch
.last_pt_write_gfn
2321 && !last_updated_pte_accessed(vcpu
)) {
2322 ++vcpu
->arch
.last_pt_write_count
;
2323 if (vcpu
->arch
.last_pt_write_count
>= 3)
2326 vcpu
->arch
.last_pt_write_gfn
= gfn
;
2327 vcpu
->arch
.last_pt_write_count
= 1;
2328 vcpu
->arch
.last_pte_updated
= NULL
;
2330 index
= kvm_page_table_hashfn(gfn
);
2331 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
2332 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
) {
2333 if (sp
->gfn
!= gfn
|| sp
->role
.metaphysical
|| sp
->role
.invalid
)
2335 pte_size
= sp
->role
.glevels
== PT32_ROOT_LEVEL
? 4 : 8;
2336 misaligned
= (offset
^ (offset
+ bytes
- 1)) & ~(pte_size
- 1);
2337 misaligned
|= bytes
< 4;
2338 if (misaligned
|| flooded
) {
2340 * Misaligned accesses are too much trouble to fix
2341 * up; also, they usually indicate a page is not used
2344 * If we're seeing too many writes to a page,
2345 * it may no longer be a page table, or we may be
2346 * forking, in which case it is better to unmap the
2349 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2350 gpa
, bytes
, sp
->role
.word
);
2351 if (kvm_mmu_zap_page(vcpu
->kvm
, sp
))
2353 ++vcpu
->kvm
->stat
.mmu_flooded
;
2356 page_offset
= offset
;
2357 level
= sp
->role
.level
;
2359 if (sp
->role
.glevels
== PT32_ROOT_LEVEL
) {
2360 page_offset
<<= 1; /* 32->64 */
2362 * A 32-bit pde maps 4MB while the shadow pdes map
2363 * only 2MB. So we need to double the offset again
2364 * and zap two pdes instead of one.
2366 if (level
== PT32_ROOT_LEVEL
) {
2367 page_offset
&= ~7; /* kill rounding error */
2371 quadrant
= page_offset
>> PAGE_SHIFT
;
2372 page_offset
&= ~PAGE_MASK
;
2373 if (quadrant
!= sp
->role
.quadrant
)
2376 spte
= &sp
->spt
[page_offset
/ sizeof(*spte
)];
2377 if ((gpa
& (pte_size
- 1)) || (bytes
< pte_size
)) {
2379 r
= kvm_read_guest_atomic(vcpu
->kvm
,
2380 gpa
& ~(u64
)(pte_size
- 1),
2382 new = (const void *)&gentry
;
2388 mmu_pte_write_zap_pte(vcpu
, sp
, spte
);
2390 mmu_pte_write_new_pte(vcpu
, sp
, spte
, new);
2391 mmu_pte_write_flush_tlb(vcpu
, entry
, *spte
);
2395 kvm_mmu_audit(vcpu
, "post pte write");
2396 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2397 if (!is_error_pfn(vcpu
->arch
.update_pte
.pfn
)) {
2398 kvm_release_pfn_clean(vcpu
->arch
.update_pte
.pfn
);
2399 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
2403 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu
*vcpu
, gva_t gva
)
2408 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
);
2410 spin_lock(&vcpu
->kvm
->mmu_lock
);
2411 r
= kvm_mmu_unprotect_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2412 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2415 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt
);
2417 void __kvm_mmu_free_some_pages(struct kvm_vcpu
*vcpu
)
2419 while (vcpu
->kvm
->arch
.n_free_mmu_pages
< KVM_REFILL_PAGES
) {
2420 struct kvm_mmu_page
*sp
;
2422 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.prev
,
2423 struct kvm_mmu_page
, link
);
2424 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2425 ++vcpu
->kvm
->stat
.mmu_recycled
;
2429 int kvm_mmu_page_fault(struct kvm_vcpu
*vcpu
, gva_t cr2
, u32 error_code
)
2432 enum emulation_result er
;
2434 r
= vcpu
->arch
.mmu
.page_fault(vcpu
, cr2
, error_code
);
2443 r
= mmu_topup_memory_caches(vcpu
);
2447 er
= emulate_instruction(vcpu
, vcpu
->run
, cr2
, error_code
, 0);
2452 case EMULATE_DO_MMIO
:
2453 ++vcpu
->stat
.mmio_exits
;
2456 kvm_report_emulation_failure(vcpu
, "pagetable");
2464 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault
);
2466 void kvm_mmu_invlpg(struct kvm_vcpu
*vcpu
, gva_t gva
)
2468 spin_lock(&vcpu
->kvm
->mmu_lock
);
2469 vcpu
->arch
.mmu
.invlpg(vcpu
, gva
);
2470 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2471 kvm_mmu_flush_tlb(vcpu
);
2472 ++vcpu
->stat
.invlpg
;
2474 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg
);
2476 void kvm_enable_tdp(void)
2480 EXPORT_SYMBOL_GPL(kvm_enable_tdp
);
2482 void kvm_disable_tdp(void)
2484 tdp_enabled
= false;
2486 EXPORT_SYMBOL_GPL(kvm_disable_tdp
);
2488 static void free_mmu_pages(struct kvm_vcpu
*vcpu
)
2490 struct kvm_mmu_page
*sp
;
2492 while (!list_empty(&vcpu
->kvm
->arch
.active_mmu_pages
)) {
2493 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.next
,
2494 struct kvm_mmu_page
, link
);
2495 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2498 free_page((unsigned long)vcpu
->arch
.mmu
.pae_root
);
2501 static int alloc_mmu_pages(struct kvm_vcpu
*vcpu
)
2508 if (vcpu
->kvm
->arch
.n_requested_mmu_pages
)
2509 vcpu
->kvm
->arch
.n_free_mmu_pages
=
2510 vcpu
->kvm
->arch
.n_requested_mmu_pages
;
2512 vcpu
->kvm
->arch
.n_free_mmu_pages
=
2513 vcpu
->kvm
->arch
.n_alloc_mmu_pages
;
2515 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2516 * Therefore we need to allocate shadow page tables in the first
2517 * 4GB of memory, which happens to fit the DMA32 zone.
2519 page
= alloc_page(GFP_KERNEL
| __GFP_DMA32
);
2522 vcpu
->arch
.mmu
.pae_root
= page_address(page
);
2523 for (i
= 0; i
< 4; ++i
)
2524 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
2529 free_mmu_pages(vcpu
);
2533 int kvm_mmu_create(struct kvm_vcpu
*vcpu
)
2536 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2538 return alloc_mmu_pages(vcpu
);
2541 int kvm_mmu_setup(struct kvm_vcpu
*vcpu
)
2544 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2546 return init_kvm_mmu(vcpu
);
2549 void kvm_mmu_destroy(struct kvm_vcpu
*vcpu
)
2553 destroy_kvm_mmu(vcpu
);
2554 free_mmu_pages(vcpu
);
2555 mmu_free_memory_caches(vcpu
);
2558 void kvm_mmu_slot_remove_write_access(struct kvm
*kvm
, int slot
)
2560 struct kvm_mmu_page
*sp
;
2562 spin_lock(&kvm
->mmu_lock
);
2563 list_for_each_entry(sp
, &kvm
->arch
.active_mmu_pages
, link
) {
2567 if (!test_bit(slot
, &sp
->slot_bitmap
))
2571 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
2573 if (pt
[i
] & PT_WRITABLE_MASK
)
2574 pt
[i
] &= ~PT_WRITABLE_MASK
;
2576 kvm_flush_remote_tlbs(kvm
);
2577 spin_unlock(&kvm
->mmu_lock
);
2580 void kvm_mmu_zap_all(struct kvm
*kvm
)
2582 struct kvm_mmu_page
*sp
, *node
;
2584 spin_lock(&kvm
->mmu_lock
);
2585 list_for_each_entry_safe(sp
, node
, &kvm
->arch
.active_mmu_pages
, link
)
2586 if (kvm_mmu_zap_page(kvm
, sp
))
2587 node
= container_of(kvm
->arch
.active_mmu_pages
.next
,
2588 struct kvm_mmu_page
, link
);
2589 spin_unlock(&kvm
->mmu_lock
);
2591 kvm_flush_remote_tlbs(kvm
);
2594 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm
*kvm
)
2596 struct kvm_mmu_page
*page
;
2598 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
2599 struct kvm_mmu_page
, link
);
2600 kvm_mmu_zap_page(kvm
, page
);
2603 static int mmu_shrink(int nr_to_scan
, gfp_t gfp_mask
)
2606 struct kvm
*kvm_freed
= NULL
;
2607 int cache_count
= 0;
2609 spin_lock(&kvm_lock
);
2611 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
2614 if (!down_read_trylock(&kvm
->slots_lock
))
2616 spin_lock(&kvm
->mmu_lock
);
2617 npages
= kvm
->arch
.n_alloc_mmu_pages
-
2618 kvm
->arch
.n_free_mmu_pages
;
2619 cache_count
+= npages
;
2620 if (!kvm_freed
&& nr_to_scan
> 0 && npages
> 0) {
2621 kvm_mmu_remove_one_alloc_mmu_page(kvm
);
2627 spin_unlock(&kvm
->mmu_lock
);
2628 up_read(&kvm
->slots_lock
);
2631 list_move_tail(&kvm_freed
->vm_list
, &vm_list
);
2633 spin_unlock(&kvm_lock
);
2638 static struct shrinker mmu_shrinker
= {
2639 .shrink
= mmu_shrink
,
2640 .seeks
= DEFAULT_SEEKS
* 10,
2643 static void mmu_destroy_caches(void)
2645 if (pte_chain_cache
)
2646 kmem_cache_destroy(pte_chain_cache
);
2647 if (rmap_desc_cache
)
2648 kmem_cache_destroy(rmap_desc_cache
);
2649 if (mmu_page_header_cache
)
2650 kmem_cache_destroy(mmu_page_header_cache
);
2653 void kvm_mmu_module_exit(void)
2655 mmu_destroy_caches();
2656 unregister_shrinker(&mmu_shrinker
);
2659 int kvm_mmu_module_init(void)
2661 pte_chain_cache
= kmem_cache_create("kvm_pte_chain",
2662 sizeof(struct kvm_pte_chain
),
2664 if (!pte_chain_cache
)
2666 rmap_desc_cache
= kmem_cache_create("kvm_rmap_desc",
2667 sizeof(struct kvm_rmap_desc
),
2669 if (!rmap_desc_cache
)
2672 mmu_page_header_cache
= kmem_cache_create("kvm_mmu_page_header",
2673 sizeof(struct kvm_mmu_page
),
2675 if (!mmu_page_header_cache
)
2678 register_shrinker(&mmu_shrinker
);
2683 mmu_destroy_caches();
2688 * Caculate mmu pages needed for kvm.
2690 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm
*kvm
)
2693 unsigned int nr_mmu_pages
;
2694 unsigned int nr_pages
= 0;
2696 for (i
= 0; i
< kvm
->nmemslots
; i
++)
2697 nr_pages
+= kvm
->memslots
[i
].npages
;
2699 nr_mmu_pages
= nr_pages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
2700 nr_mmu_pages
= max(nr_mmu_pages
,
2701 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
2703 return nr_mmu_pages
;
2706 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
2709 if (len
> buffer
->len
)
2714 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
2719 ret
= pv_mmu_peek_buffer(buffer
, len
);
2724 buffer
->processed
+= len
;
2728 static int kvm_pv_mmu_write(struct kvm_vcpu
*vcpu
,
2729 gpa_t addr
, gpa_t value
)
2734 if (!is_long_mode(vcpu
) && !is_pae(vcpu
))
2737 r
= mmu_topup_memory_caches(vcpu
);
2741 if (!emulator_write_phys(vcpu
, addr
, &value
, bytes
))
2747 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
2749 kvm_x86_ops
->tlb_flush(vcpu
);
2750 set_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
);
2754 static int kvm_pv_mmu_release_pt(struct kvm_vcpu
*vcpu
, gpa_t addr
)
2756 spin_lock(&vcpu
->kvm
->mmu_lock
);
2757 mmu_unshadow(vcpu
->kvm
, addr
>> PAGE_SHIFT
);
2758 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2762 static int kvm_pv_mmu_op_one(struct kvm_vcpu
*vcpu
,
2763 struct kvm_pv_mmu_op_buffer
*buffer
)
2765 struct kvm_mmu_op_header
*header
;
2767 header
= pv_mmu_peek_buffer(buffer
, sizeof *header
);
2770 switch (header
->op
) {
2771 case KVM_MMU_OP_WRITE_PTE
: {
2772 struct kvm_mmu_op_write_pte
*wpte
;
2774 wpte
= pv_mmu_read_buffer(buffer
, sizeof *wpte
);
2777 return kvm_pv_mmu_write(vcpu
, wpte
->pte_phys
,
2780 case KVM_MMU_OP_FLUSH_TLB
: {
2781 struct kvm_mmu_op_flush_tlb
*ftlb
;
2783 ftlb
= pv_mmu_read_buffer(buffer
, sizeof *ftlb
);
2786 return kvm_pv_mmu_flush_tlb(vcpu
);
2788 case KVM_MMU_OP_RELEASE_PT
: {
2789 struct kvm_mmu_op_release_pt
*rpt
;
2791 rpt
= pv_mmu_read_buffer(buffer
, sizeof *rpt
);
2794 return kvm_pv_mmu_release_pt(vcpu
, rpt
->pt_phys
);
2800 int kvm_pv_mmu_op(struct kvm_vcpu
*vcpu
, unsigned long bytes
,
2801 gpa_t addr
, unsigned long *ret
)
2804 struct kvm_pv_mmu_op_buffer
*buffer
= &vcpu
->arch
.mmu_op_buffer
;
2806 buffer
->ptr
= buffer
->buf
;
2807 buffer
->len
= min_t(unsigned long, bytes
, sizeof buffer
->buf
);
2808 buffer
->processed
= 0;
2810 r
= kvm_read_guest(vcpu
->kvm
, addr
, buffer
->buf
, buffer
->len
);
2814 while (buffer
->len
) {
2815 r
= kvm_pv_mmu_op_one(vcpu
, buffer
);
2824 *ret
= buffer
->processed
;
2830 static const char *audit_msg
;
2832 static gva_t
canonicalize(gva_t gva
)
2834 #ifdef CONFIG_X86_64
2835 gva
= (long long)(gva
<< 16) >> 16;
2840 static void audit_mappings_page(struct kvm_vcpu
*vcpu
, u64 page_pte
,
2841 gva_t va
, int level
)
2843 u64
*pt
= __va(page_pte
& PT64_BASE_ADDR_MASK
);
2845 gva_t va_delta
= 1ul << (PAGE_SHIFT
+ 9 * (level
- 1));
2847 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
, va
+= va_delta
) {
2850 if (ent
== shadow_trap_nonpresent_pte
)
2853 va
= canonicalize(va
);
2855 if (ent
== shadow_notrap_nonpresent_pte
)
2856 printk(KERN_ERR
"audit: (%s) nontrapping pte"
2857 " in nonleaf level: levels %d gva %lx"
2858 " level %d pte %llx\n", audit_msg
,
2859 vcpu
->arch
.mmu
.root_level
, va
, level
, ent
);
2861 audit_mappings_page(vcpu
, ent
, va
, level
- 1);
2863 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, va
);
2864 hpa_t hpa
= (hpa_t
)gpa_to_pfn(vcpu
, gpa
) << PAGE_SHIFT
;
2866 if (is_shadow_present_pte(ent
)
2867 && (ent
& PT64_BASE_ADDR_MASK
) != hpa
)
2868 printk(KERN_ERR
"xx audit error: (%s) levels %d"
2869 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2870 audit_msg
, vcpu
->arch
.mmu
.root_level
,
2872 is_shadow_present_pte(ent
));
2873 else if (ent
== shadow_notrap_nonpresent_pte
2874 && !is_error_hpa(hpa
))
2875 printk(KERN_ERR
"audit: (%s) notrap shadow,"
2876 " valid guest gva %lx\n", audit_msg
, va
);
2877 kvm_release_pfn_clean(pfn
);
2883 static void audit_mappings(struct kvm_vcpu
*vcpu
)
2887 if (vcpu
->arch
.mmu
.root_level
== 4)
2888 audit_mappings_page(vcpu
, vcpu
->arch
.mmu
.root_hpa
, 0, 4);
2890 for (i
= 0; i
< 4; ++i
)
2891 if (vcpu
->arch
.mmu
.pae_root
[i
] & PT_PRESENT_MASK
)
2892 audit_mappings_page(vcpu
,
2893 vcpu
->arch
.mmu
.pae_root
[i
],
2898 static int count_rmaps(struct kvm_vcpu
*vcpu
)
2903 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
2904 struct kvm_memory_slot
*m
= &vcpu
->kvm
->memslots
[i
];
2905 struct kvm_rmap_desc
*d
;
2907 for (j
= 0; j
< m
->npages
; ++j
) {
2908 unsigned long *rmapp
= &m
->rmap
[j
];
2912 if (!(*rmapp
& 1)) {
2916 d
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
2918 for (k
= 0; k
< RMAP_EXT
; ++k
)
2919 if (d
->shadow_ptes
[k
])
2930 static int count_writable_mappings(struct kvm_vcpu
*vcpu
)
2933 struct kvm_mmu_page
*sp
;
2936 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
2939 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
2942 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
2945 if (!(ent
& PT_PRESENT_MASK
))
2947 if (!(ent
& PT_WRITABLE_MASK
))
2955 static void audit_rmap(struct kvm_vcpu
*vcpu
)
2957 int n_rmap
= count_rmaps(vcpu
);
2958 int n_actual
= count_writable_mappings(vcpu
);
2960 if (n_rmap
!= n_actual
)
2961 printk(KERN_ERR
"%s: (%s) rmap %d actual %d\n",
2962 __func__
, audit_msg
, n_rmap
, n_actual
);
2965 static void audit_write_protection(struct kvm_vcpu
*vcpu
)
2967 struct kvm_mmu_page
*sp
;
2968 struct kvm_memory_slot
*slot
;
2969 unsigned long *rmapp
;
2972 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
2973 if (sp
->role
.metaphysical
)
2976 slot
= gfn_to_memslot(vcpu
->kvm
, sp
->gfn
);
2977 gfn
= unalias_gfn(vcpu
->kvm
, sp
->gfn
);
2978 rmapp
= &slot
->rmap
[gfn
- slot
->base_gfn
];
2980 printk(KERN_ERR
"%s: (%s) shadow page has writable"
2981 " mappings: gfn %lx role %x\n",
2982 __func__
, audit_msg
, sp
->gfn
,
2987 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
)
2994 audit_write_protection(vcpu
);
2995 audit_mappings(vcpu
);