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);
74 #define ASSERT(x) do { } while (0)
78 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
79 __FILE__, __LINE__, #x); \
83 #define PT_FIRST_AVAIL_BITS_SHIFT 9
84 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
86 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
88 #define PT64_LEVEL_BITS 9
90 #define PT64_LEVEL_SHIFT(level) \
91 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
93 #define PT64_LEVEL_MASK(level) \
94 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
96 #define PT64_INDEX(address, level)\
97 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
100 #define PT32_LEVEL_BITS 10
102 #define PT32_LEVEL_SHIFT(level) \
103 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
105 #define PT32_LEVEL_MASK(level) \
106 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
108 #define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
112 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
113 #define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
116 #define PT32_BASE_ADDR_MASK PAGE_MASK
117 #define PT32_DIR_BASE_ADDR_MASK \
118 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
120 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
123 #define PFERR_PRESENT_MASK (1U << 0)
124 #define PFERR_WRITE_MASK (1U << 1)
125 #define PFERR_USER_MASK (1U << 2)
126 #define PFERR_FETCH_MASK (1U << 4)
128 #define PT_DIRECTORY_LEVEL 2
129 #define PT_PAGE_TABLE_LEVEL 1
133 #define ACC_EXEC_MASK 1
134 #define ACC_WRITE_MASK PT_WRITABLE_MASK
135 #define ACC_USER_MASK PT_USER_MASK
136 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
138 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
140 struct kvm_rmap_desc
{
141 u64
*shadow_ptes
[RMAP_EXT
];
142 struct kvm_rmap_desc
*more
;
145 struct kvm_shadow_walk
{
146 int (*entry
)(struct kvm_shadow_walk
*walk
, struct kvm_vcpu
*vcpu
,
147 u64 addr
, u64
*spte
, int level
);
150 static struct kmem_cache
*pte_chain_cache
;
151 static struct kmem_cache
*rmap_desc_cache
;
152 static struct kmem_cache
*mmu_page_header_cache
;
154 static u64 __read_mostly shadow_trap_nonpresent_pte
;
155 static u64 __read_mostly shadow_notrap_nonpresent_pte
;
156 static u64 __read_mostly shadow_base_present_pte
;
157 static u64 __read_mostly shadow_nx_mask
;
158 static u64 __read_mostly shadow_x_mask
; /* mutual exclusive with nx_mask */
159 static u64 __read_mostly shadow_user_mask
;
160 static u64 __read_mostly shadow_accessed_mask
;
161 static u64 __read_mostly shadow_dirty_mask
;
163 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte
, u64 notrap_pte
)
165 shadow_trap_nonpresent_pte
= trap_pte
;
166 shadow_notrap_nonpresent_pte
= notrap_pte
;
168 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes
);
170 void kvm_mmu_set_base_ptes(u64 base_pte
)
172 shadow_base_present_pte
= base_pte
;
174 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes
);
176 void kvm_mmu_set_mask_ptes(u64 user_mask
, u64 accessed_mask
,
177 u64 dirty_mask
, u64 nx_mask
, u64 x_mask
)
179 shadow_user_mask
= user_mask
;
180 shadow_accessed_mask
= accessed_mask
;
181 shadow_dirty_mask
= dirty_mask
;
182 shadow_nx_mask
= nx_mask
;
183 shadow_x_mask
= x_mask
;
185 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes
);
187 static int is_write_protection(struct kvm_vcpu
*vcpu
)
189 return vcpu
->arch
.cr0
& X86_CR0_WP
;
192 static int is_cpuid_PSE36(void)
197 static int is_nx(struct kvm_vcpu
*vcpu
)
199 return vcpu
->arch
.shadow_efer
& EFER_NX
;
202 static int is_present_pte(unsigned long pte
)
204 return pte
& PT_PRESENT_MASK
;
207 static int is_shadow_present_pte(u64 pte
)
209 return pte
!= shadow_trap_nonpresent_pte
210 && pte
!= shadow_notrap_nonpresent_pte
;
213 static int is_large_pte(u64 pte
)
215 return pte
& PT_PAGE_SIZE_MASK
;
218 static int is_writeble_pte(unsigned long pte
)
220 return pte
& PT_WRITABLE_MASK
;
223 static int is_dirty_pte(unsigned long pte
)
225 return pte
& shadow_dirty_mask
;
228 static int is_rmap_pte(u64 pte
)
230 return is_shadow_present_pte(pte
);
233 static pfn_t
spte_to_pfn(u64 pte
)
235 return (pte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
238 static gfn_t
pse36_gfn_delta(u32 gpte
)
240 int shift
= 32 - PT32_DIR_PSE36_SHIFT
- PAGE_SHIFT
;
242 return (gpte
& PT32_DIR_PSE36_MASK
) << shift
;
245 static void set_shadow_pte(u64
*sptep
, u64 spte
)
248 set_64bit((unsigned long *)sptep
, spte
);
250 set_64bit((unsigned long long *)sptep
, spte
);
254 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
255 struct kmem_cache
*base_cache
, int min
)
259 if (cache
->nobjs
>= min
)
261 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
262 obj
= kmem_cache_zalloc(base_cache
, GFP_KERNEL
);
265 cache
->objects
[cache
->nobjs
++] = obj
;
270 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
273 kfree(mc
->objects
[--mc
->nobjs
]);
276 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache
*cache
,
281 if (cache
->nobjs
>= min
)
283 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
284 page
= alloc_page(GFP_KERNEL
);
287 set_page_private(page
, 0);
288 cache
->objects
[cache
->nobjs
++] = page_address(page
);
293 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache
*mc
)
296 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
299 static int mmu_topup_memory_caches(struct kvm_vcpu
*vcpu
)
303 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
,
307 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
,
311 r
= mmu_topup_memory_cache_page(&vcpu
->arch
.mmu_page_cache
, 8);
314 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_page_header_cache
,
315 mmu_page_header_cache
, 4);
320 static void mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
322 mmu_free_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
);
323 mmu_free_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
);
324 mmu_free_memory_cache_page(&vcpu
->arch
.mmu_page_cache
);
325 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_header_cache
);
328 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
,
334 p
= mc
->objects
[--mc
->nobjs
];
339 static struct kvm_pte_chain
*mmu_alloc_pte_chain(struct kvm_vcpu
*vcpu
)
341 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_pte_chain_cache
,
342 sizeof(struct kvm_pte_chain
));
345 static void mmu_free_pte_chain(struct kvm_pte_chain
*pc
)
350 static struct kvm_rmap_desc
*mmu_alloc_rmap_desc(struct kvm_vcpu
*vcpu
)
352 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_rmap_desc_cache
,
353 sizeof(struct kvm_rmap_desc
));
356 static void mmu_free_rmap_desc(struct kvm_rmap_desc
*rd
)
362 * Return the pointer to the largepage write count for a given
363 * gfn, handling slots that are not large page aligned.
365 static int *slot_largepage_idx(gfn_t gfn
, struct kvm_memory_slot
*slot
)
369 idx
= (gfn
/ KVM_PAGES_PER_HPAGE
) -
370 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE
);
371 return &slot
->lpage_info
[idx
].write_count
;
374 static void account_shadowed(struct kvm
*kvm
, gfn_t gfn
)
378 write_count
= slot_largepage_idx(gfn
, gfn_to_memslot(kvm
, gfn
));
382 static void unaccount_shadowed(struct kvm
*kvm
, gfn_t gfn
)
386 write_count
= slot_largepage_idx(gfn
, gfn_to_memslot(kvm
, gfn
));
388 WARN_ON(*write_count
< 0);
391 static int has_wrprotected_page(struct kvm
*kvm
, gfn_t gfn
)
393 struct kvm_memory_slot
*slot
= gfn_to_memslot(kvm
, gfn
);
397 largepage_idx
= slot_largepage_idx(gfn
, slot
);
398 return *largepage_idx
;
404 static int host_largepage_backed(struct kvm
*kvm
, gfn_t gfn
)
406 struct vm_area_struct
*vma
;
410 addr
= gfn_to_hva(kvm
, gfn
);
411 if (kvm_is_error_hva(addr
))
414 down_read(¤t
->mm
->mmap_sem
);
415 vma
= find_vma(current
->mm
, addr
);
416 if (vma
&& is_vm_hugetlb_page(vma
))
418 up_read(¤t
->mm
->mmap_sem
);
423 static int is_largepage_backed(struct kvm_vcpu
*vcpu
, gfn_t large_gfn
)
425 struct kvm_memory_slot
*slot
;
427 if (has_wrprotected_page(vcpu
->kvm
, large_gfn
))
430 if (!host_largepage_backed(vcpu
->kvm
, large_gfn
))
433 slot
= gfn_to_memslot(vcpu
->kvm
, large_gfn
);
434 if (slot
&& slot
->dirty_bitmap
)
441 * Take gfn and return the reverse mapping to it.
442 * Note: gfn must be unaliased before this function get called
445 static unsigned long *gfn_to_rmap(struct kvm
*kvm
, gfn_t gfn
, int lpage
)
447 struct kvm_memory_slot
*slot
;
450 slot
= gfn_to_memslot(kvm
, gfn
);
452 return &slot
->rmap
[gfn
- slot
->base_gfn
];
454 idx
= (gfn
/ KVM_PAGES_PER_HPAGE
) -
455 (slot
->base_gfn
/ KVM_PAGES_PER_HPAGE
);
457 return &slot
->lpage_info
[idx
].rmap_pde
;
461 * Reverse mapping data structures:
463 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
464 * that points to page_address(page).
466 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
467 * containing more mappings.
469 static void rmap_add(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
, int lpage
)
471 struct kvm_mmu_page
*sp
;
472 struct kvm_rmap_desc
*desc
;
473 unsigned long *rmapp
;
476 if (!is_rmap_pte(*spte
))
478 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
479 sp
= page_header(__pa(spte
));
480 sp
->gfns
[spte
- sp
->spt
] = gfn
;
481 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
, lpage
);
483 rmap_printk("rmap_add: %p %llx 0->1\n", spte
, *spte
);
484 *rmapp
= (unsigned long)spte
;
485 } else if (!(*rmapp
& 1)) {
486 rmap_printk("rmap_add: %p %llx 1->many\n", spte
, *spte
);
487 desc
= mmu_alloc_rmap_desc(vcpu
);
488 desc
->shadow_ptes
[0] = (u64
*)*rmapp
;
489 desc
->shadow_ptes
[1] = spte
;
490 *rmapp
= (unsigned long)desc
| 1;
492 rmap_printk("rmap_add: %p %llx many->many\n", spte
, *spte
);
493 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
494 while (desc
->shadow_ptes
[RMAP_EXT
-1] && desc
->more
)
496 if (desc
->shadow_ptes
[RMAP_EXT
-1]) {
497 desc
->more
= mmu_alloc_rmap_desc(vcpu
);
500 for (i
= 0; desc
->shadow_ptes
[i
]; ++i
)
502 desc
->shadow_ptes
[i
] = spte
;
506 static void rmap_desc_remove_entry(unsigned long *rmapp
,
507 struct kvm_rmap_desc
*desc
,
509 struct kvm_rmap_desc
*prev_desc
)
513 for (j
= RMAP_EXT
- 1; !desc
->shadow_ptes
[j
] && j
> i
; --j
)
515 desc
->shadow_ptes
[i
] = desc
->shadow_ptes
[j
];
516 desc
->shadow_ptes
[j
] = NULL
;
519 if (!prev_desc
&& !desc
->more
)
520 *rmapp
= (unsigned long)desc
->shadow_ptes
[0];
523 prev_desc
->more
= desc
->more
;
525 *rmapp
= (unsigned long)desc
->more
| 1;
526 mmu_free_rmap_desc(desc
);
529 static void rmap_remove(struct kvm
*kvm
, u64
*spte
)
531 struct kvm_rmap_desc
*desc
;
532 struct kvm_rmap_desc
*prev_desc
;
533 struct kvm_mmu_page
*sp
;
535 unsigned long *rmapp
;
538 if (!is_rmap_pte(*spte
))
540 sp
= page_header(__pa(spte
));
541 pfn
= spte_to_pfn(*spte
);
542 if (*spte
& shadow_accessed_mask
)
543 kvm_set_pfn_accessed(pfn
);
544 if (is_writeble_pte(*spte
))
545 kvm_release_pfn_dirty(pfn
);
547 kvm_release_pfn_clean(pfn
);
548 rmapp
= gfn_to_rmap(kvm
, sp
->gfns
[spte
- sp
->spt
], is_large_pte(*spte
));
550 printk(KERN_ERR
"rmap_remove: %p %llx 0->BUG\n", spte
, *spte
);
552 } else if (!(*rmapp
& 1)) {
553 rmap_printk("rmap_remove: %p %llx 1->0\n", spte
, *spte
);
554 if ((u64
*)*rmapp
!= spte
) {
555 printk(KERN_ERR
"rmap_remove: %p %llx 1->BUG\n",
561 rmap_printk("rmap_remove: %p %llx many->many\n", spte
, *spte
);
562 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
565 for (i
= 0; i
< RMAP_EXT
&& desc
->shadow_ptes
[i
]; ++i
)
566 if (desc
->shadow_ptes
[i
] == spte
) {
567 rmap_desc_remove_entry(rmapp
,
579 static u64
*rmap_next(struct kvm
*kvm
, unsigned long *rmapp
, u64
*spte
)
581 struct kvm_rmap_desc
*desc
;
582 struct kvm_rmap_desc
*prev_desc
;
588 else if (!(*rmapp
& 1)) {
590 return (u64
*)*rmapp
;
593 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
597 for (i
= 0; i
< RMAP_EXT
&& desc
->shadow_ptes
[i
]; ++i
) {
598 if (prev_spte
== spte
)
599 return desc
->shadow_ptes
[i
];
600 prev_spte
= desc
->shadow_ptes
[i
];
607 static void rmap_write_protect(struct kvm
*kvm
, u64 gfn
)
609 unsigned long *rmapp
;
611 int write_protected
= 0;
613 gfn
= unalias_gfn(kvm
, gfn
);
614 rmapp
= gfn_to_rmap(kvm
, gfn
, 0);
616 spte
= rmap_next(kvm
, rmapp
, NULL
);
619 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
620 rmap_printk("rmap_write_protect: spte %p %llx\n", spte
, *spte
);
621 if (is_writeble_pte(*spte
)) {
622 set_shadow_pte(spte
, *spte
& ~PT_WRITABLE_MASK
);
625 spte
= rmap_next(kvm
, rmapp
, spte
);
627 if (write_protected
) {
630 spte
= rmap_next(kvm
, rmapp
, NULL
);
631 pfn
= spte_to_pfn(*spte
);
632 kvm_set_pfn_dirty(pfn
);
635 /* check for huge page mappings */
636 rmapp
= gfn_to_rmap(kvm
, gfn
, 1);
637 spte
= rmap_next(kvm
, rmapp
, NULL
);
640 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
641 BUG_ON((*spte
& (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
)) != (PT_PAGE_SIZE_MASK
|PT_PRESENT_MASK
));
642 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte
, *spte
, gfn
);
643 if (is_writeble_pte(*spte
)) {
644 rmap_remove(kvm
, spte
);
646 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
650 spte
= rmap_next(kvm
, rmapp
, spte
);
654 kvm_flush_remote_tlbs(kvm
);
656 account_shadowed(kvm
, gfn
);
659 static int kvm_unmap_rmapp(struct kvm
*kvm
, unsigned long *rmapp
)
662 int need_tlb_flush
= 0;
664 while ((spte
= rmap_next(kvm
, rmapp
, NULL
))) {
665 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
666 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte
, *spte
);
667 rmap_remove(kvm
, spte
);
668 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
671 return need_tlb_flush
;
674 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
675 int (*handler
)(struct kvm
*kvm
, unsigned long *rmapp
))
681 * If mmap_sem isn't taken, we can look the memslots with only
682 * the mmu_lock by skipping over the slots with userspace_addr == 0.
684 for (i
= 0; i
< kvm
->nmemslots
; i
++) {
685 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[i
];
686 unsigned long start
= memslot
->userspace_addr
;
689 /* mmu_lock protects userspace_addr */
693 end
= start
+ (memslot
->npages
<< PAGE_SHIFT
);
694 if (hva
>= start
&& hva
< end
) {
695 gfn_t gfn_offset
= (hva
- start
) >> PAGE_SHIFT
;
696 retval
|= handler(kvm
, &memslot
->rmap
[gfn_offset
]);
697 retval
|= handler(kvm
,
698 &memslot
->lpage_info
[
700 KVM_PAGES_PER_HPAGE
].rmap_pde
);
707 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
709 return kvm_handle_hva(kvm
, hva
, kvm_unmap_rmapp
);
712 static int kvm_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
)
717 /* always return old for EPT */
718 if (!shadow_accessed_mask
)
721 spte
= rmap_next(kvm
, rmapp
, NULL
);
725 BUG_ON(!(_spte
& PT_PRESENT_MASK
));
726 _young
= _spte
& PT_ACCESSED_MASK
;
729 clear_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
731 spte
= rmap_next(kvm
, rmapp
, spte
);
736 int kvm_age_hva(struct kvm
*kvm
, unsigned long hva
)
738 return kvm_handle_hva(kvm
, hva
, kvm_age_rmapp
);
742 static int is_empty_shadow_page(u64
*spt
)
747 for (pos
= spt
, end
= pos
+ PAGE_SIZE
/ sizeof(u64
); pos
!= end
; pos
++)
748 if (is_shadow_present_pte(*pos
)) {
749 printk(KERN_ERR
"%s: %p %llx\n", __func__
,
757 static void kvm_mmu_free_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
759 ASSERT(is_empty_shadow_page(sp
->spt
));
761 __free_page(virt_to_page(sp
->spt
));
762 __free_page(virt_to_page(sp
->gfns
));
764 ++kvm
->arch
.n_free_mmu_pages
;
767 static unsigned kvm_page_table_hashfn(gfn_t gfn
)
769 return gfn
& ((1 << KVM_MMU_HASH_SHIFT
) - 1);
772 static struct kvm_mmu_page
*kvm_mmu_alloc_page(struct kvm_vcpu
*vcpu
,
775 struct kvm_mmu_page
*sp
;
777 sp
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_header_cache
, sizeof *sp
);
778 sp
->spt
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
779 sp
->gfns
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
780 set_page_private(virt_to_page(sp
->spt
), (unsigned long)sp
);
781 list_add(&sp
->link
, &vcpu
->kvm
->arch
.active_mmu_pages
);
782 ASSERT(is_empty_shadow_page(sp
->spt
));
785 sp
->parent_pte
= parent_pte
;
786 --vcpu
->kvm
->arch
.n_free_mmu_pages
;
790 static void mmu_page_add_parent_pte(struct kvm_vcpu
*vcpu
,
791 struct kvm_mmu_page
*sp
, u64
*parent_pte
)
793 struct kvm_pte_chain
*pte_chain
;
794 struct hlist_node
*node
;
799 if (!sp
->multimapped
) {
800 u64
*old
= sp
->parent_pte
;
803 sp
->parent_pte
= parent_pte
;
807 pte_chain
= mmu_alloc_pte_chain(vcpu
);
808 INIT_HLIST_HEAD(&sp
->parent_ptes
);
809 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
810 pte_chain
->parent_ptes
[0] = old
;
812 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
) {
813 if (pte_chain
->parent_ptes
[NR_PTE_CHAIN_ENTRIES
-1])
815 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
)
816 if (!pte_chain
->parent_ptes
[i
]) {
817 pte_chain
->parent_ptes
[i
] = parent_pte
;
821 pte_chain
= mmu_alloc_pte_chain(vcpu
);
823 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
824 pte_chain
->parent_ptes
[0] = parent_pte
;
827 static void mmu_page_remove_parent_pte(struct kvm_mmu_page
*sp
,
830 struct kvm_pte_chain
*pte_chain
;
831 struct hlist_node
*node
;
834 if (!sp
->multimapped
) {
835 BUG_ON(sp
->parent_pte
!= parent_pte
);
836 sp
->parent_pte
= NULL
;
839 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
840 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
841 if (!pte_chain
->parent_ptes
[i
])
843 if (pte_chain
->parent_ptes
[i
] != parent_pte
)
845 while (i
+ 1 < NR_PTE_CHAIN_ENTRIES
846 && pte_chain
->parent_ptes
[i
+ 1]) {
847 pte_chain
->parent_ptes
[i
]
848 = pte_chain
->parent_ptes
[i
+ 1];
851 pte_chain
->parent_ptes
[i
] = NULL
;
853 hlist_del(&pte_chain
->link
);
854 mmu_free_pte_chain(pte_chain
);
855 if (hlist_empty(&sp
->parent_ptes
)) {
857 sp
->parent_pte
= NULL
;
865 static void nonpaging_prefetch_page(struct kvm_vcpu
*vcpu
,
866 struct kvm_mmu_page
*sp
)
870 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
871 sp
->spt
[i
] = shadow_trap_nonpresent_pte
;
874 static int nonpaging_sync_page(struct kvm_vcpu
*vcpu
,
875 struct kvm_mmu_page
*sp
)
880 static struct kvm_mmu_page
*kvm_mmu_lookup_page(struct kvm
*kvm
, gfn_t gfn
)
883 struct hlist_head
*bucket
;
884 struct kvm_mmu_page
*sp
;
885 struct hlist_node
*node
;
887 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
888 index
= kvm_page_table_hashfn(gfn
);
889 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
890 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
891 if (sp
->gfn
== gfn
&& !sp
->role
.metaphysical
892 && !sp
->role
.invalid
) {
893 pgprintk("%s: found role %x\n",
894 __func__
, sp
->role
.word
);
900 static struct kvm_mmu_page
*kvm_mmu_get_page(struct kvm_vcpu
*vcpu
,
908 union kvm_mmu_page_role role
;
911 struct hlist_head
*bucket
;
912 struct kvm_mmu_page
*sp
;
913 struct hlist_node
*node
;
916 role
.glevels
= vcpu
->arch
.mmu
.root_level
;
918 role
.metaphysical
= metaphysical
;
919 role
.access
= access
;
920 if (vcpu
->arch
.mmu
.root_level
<= PT32_ROOT_LEVEL
) {
921 quadrant
= gaddr
>> (PAGE_SHIFT
+ (PT64_PT_BITS
* level
));
922 quadrant
&= (1 << ((PT32_PT_BITS
- PT64_PT_BITS
) * level
)) - 1;
923 role
.quadrant
= quadrant
;
925 pgprintk("%s: looking gfn %lx role %x\n", __func__
,
927 index
= kvm_page_table_hashfn(gfn
);
928 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
929 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
930 if (sp
->gfn
== gfn
&& sp
->role
.word
== role
.word
) {
931 mmu_page_add_parent_pte(vcpu
, sp
, parent_pte
);
932 pgprintk("%s: found\n", __func__
);
935 ++vcpu
->kvm
->stat
.mmu_cache_miss
;
936 sp
= kvm_mmu_alloc_page(vcpu
, parent_pte
);
939 pgprintk("%s: adding gfn %lx role %x\n", __func__
, gfn
, role
.word
);
942 hlist_add_head(&sp
->hash_link
, bucket
);
944 rmap_write_protect(vcpu
->kvm
, gfn
);
945 if (shadow_trap_nonpresent_pte
!= shadow_notrap_nonpresent_pte
)
946 vcpu
->arch
.mmu
.prefetch_page(vcpu
, sp
);
948 nonpaging_prefetch_page(vcpu
, sp
);
952 static int walk_shadow(struct kvm_shadow_walk
*walker
,
953 struct kvm_vcpu
*vcpu
, u64 addr
)
961 shadow_addr
= vcpu
->arch
.mmu
.root_hpa
;
962 level
= vcpu
->arch
.mmu
.shadow_root_level
;
963 if (level
== PT32E_ROOT_LEVEL
) {
964 shadow_addr
= vcpu
->arch
.mmu
.pae_root
[(addr
>> 30) & 3];
965 shadow_addr
&= PT64_BASE_ADDR_MASK
;
969 while (level
>= PT_PAGE_TABLE_LEVEL
) {
970 index
= SHADOW_PT_INDEX(addr
, level
);
971 sptep
= ((u64
*)__va(shadow_addr
)) + index
;
972 r
= walker
->entry(walker
, vcpu
, addr
, sptep
, level
);
975 shadow_addr
= *sptep
& PT64_BASE_ADDR_MASK
;
981 static void kvm_mmu_page_unlink_children(struct kvm
*kvm
,
982 struct kvm_mmu_page
*sp
)
990 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
) {
991 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
992 if (is_shadow_present_pte(pt
[i
]))
993 rmap_remove(kvm
, &pt
[i
]);
994 pt
[i
] = shadow_trap_nonpresent_pte
;
999 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1002 if (is_shadow_present_pte(ent
)) {
1003 if (!is_large_pte(ent
)) {
1004 ent
&= PT64_BASE_ADDR_MASK
;
1005 mmu_page_remove_parent_pte(page_header(ent
),
1009 rmap_remove(kvm
, &pt
[i
]);
1012 pt
[i
] = shadow_trap_nonpresent_pte
;
1016 static void kvm_mmu_put_page(struct kvm_mmu_page
*sp
, u64
*parent_pte
)
1018 mmu_page_remove_parent_pte(sp
, parent_pte
);
1021 static void kvm_mmu_reset_last_pte_updated(struct kvm
*kvm
)
1025 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
1027 kvm
->vcpus
[i
]->arch
.last_pte_updated
= NULL
;
1030 static void kvm_mmu_unlink_parents(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1034 while (sp
->multimapped
|| sp
->parent_pte
) {
1035 if (!sp
->multimapped
)
1036 parent_pte
= sp
->parent_pte
;
1038 struct kvm_pte_chain
*chain
;
1040 chain
= container_of(sp
->parent_ptes
.first
,
1041 struct kvm_pte_chain
, link
);
1042 parent_pte
= chain
->parent_ptes
[0];
1044 BUG_ON(!parent_pte
);
1045 kvm_mmu_put_page(sp
, parent_pte
);
1046 set_shadow_pte(parent_pte
, shadow_trap_nonpresent_pte
);
1050 static void kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
1052 ++kvm
->stat
.mmu_shadow_zapped
;
1053 kvm_mmu_page_unlink_children(kvm
, sp
);
1054 kvm_mmu_unlink_parents(kvm
, sp
);
1055 kvm_flush_remote_tlbs(kvm
);
1056 if (!sp
->role
.invalid
&& !sp
->role
.metaphysical
)
1057 unaccount_shadowed(kvm
, sp
->gfn
);
1058 if (!sp
->root_count
) {
1059 hlist_del(&sp
->hash_link
);
1060 kvm_mmu_free_page(kvm
, sp
);
1062 sp
->role
.invalid
= 1;
1063 list_move(&sp
->link
, &kvm
->arch
.active_mmu_pages
);
1064 kvm_reload_remote_mmus(kvm
);
1066 kvm_mmu_reset_last_pte_updated(kvm
);
1070 * Changing the number of mmu pages allocated to the vm
1071 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1073 void kvm_mmu_change_mmu_pages(struct kvm
*kvm
, unsigned int kvm_nr_mmu_pages
)
1076 * If we set the number of mmu pages to be smaller be than the
1077 * number of actived pages , we must to free some mmu pages before we
1081 if ((kvm
->arch
.n_alloc_mmu_pages
- kvm
->arch
.n_free_mmu_pages
) >
1083 int n_used_mmu_pages
= kvm
->arch
.n_alloc_mmu_pages
1084 - kvm
->arch
.n_free_mmu_pages
;
1086 while (n_used_mmu_pages
> kvm_nr_mmu_pages
) {
1087 struct kvm_mmu_page
*page
;
1089 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
1090 struct kvm_mmu_page
, link
);
1091 kvm_mmu_zap_page(kvm
, page
);
1094 kvm
->arch
.n_free_mmu_pages
= 0;
1097 kvm
->arch
.n_free_mmu_pages
+= kvm_nr_mmu_pages
1098 - kvm
->arch
.n_alloc_mmu_pages
;
1100 kvm
->arch
.n_alloc_mmu_pages
= kvm_nr_mmu_pages
;
1103 static int kvm_mmu_unprotect_page(struct kvm
*kvm
, gfn_t gfn
)
1106 struct hlist_head
*bucket
;
1107 struct kvm_mmu_page
*sp
;
1108 struct hlist_node
*node
, *n
;
1111 pgprintk("%s: looking for gfn %lx\n", __func__
, gfn
);
1113 index
= kvm_page_table_hashfn(gfn
);
1114 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
1115 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
)
1116 if (sp
->gfn
== gfn
&& !sp
->role
.metaphysical
) {
1117 pgprintk("%s: gfn %lx role %x\n", __func__
, gfn
,
1119 kvm_mmu_zap_page(kvm
, sp
);
1125 static void mmu_unshadow(struct kvm
*kvm
, gfn_t gfn
)
1127 struct kvm_mmu_page
*sp
;
1129 while ((sp
= kvm_mmu_lookup_page(kvm
, gfn
)) != NULL
) {
1130 pgprintk("%s: zap %lx %x\n", __func__
, gfn
, sp
->role
.word
);
1131 kvm_mmu_zap_page(kvm
, sp
);
1135 static void page_header_update_slot(struct kvm
*kvm
, void *pte
, gfn_t gfn
)
1137 int slot
= memslot_id(kvm
, gfn_to_memslot(kvm
, gfn
));
1138 struct kvm_mmu_page
*sp
= page_header(__pa(pte
));
1140 __set_bit(slot
, &sp
->slot_bitmap
);
1143 struct page
*gva_to_page(struct kvm_vcpu
*vcpu
, gva_t gva
)
1147 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
);
1149 if (gpa
== UNMAPPED_GVA
)
1152 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1157 static int set_spte(struct kvm_vcpu
*vcpu
, u64
*shadow_pte
,
1158 unsigned pte_access
, int user_fault
,
1159 int write_fault
, int dirty
, int largepage
,
1160 gfn_t gfn
, pfn_t pfn
, bool speculative
)
1165 * We don't set the accessed bit, since we sometimes want to see
1166 * whether the guest actually used the pte (in order to detect
1169 spte
= shadow_base_present_pte
| shadow_dirty_mask
;
1171 spte
|= shadow_accessed_mask
;
1173 pte_access
&= ~ACC_WRITE_MASK
;
1174 if (pte_access
& ACC_EXEC_MASK
)
1175 spte
|= shadow_x_mask
;
1177 spte
|= shadow_nx_mask
;
1178 if (pte_access
& ACC_USER_MASK
)
1179 spte
|= shadow_user_mask
;
1181 spte
|= PT_PAGE_SIZE_MASK
;
1183 spte
|= (u64
)pfn
<< PAGE_SHIFT
;
1185 if ((pte_access
& ACC_WRITE_MASK
)
1186 || (write_fault
&& !is_write_protection(vcpu
) && !user_fault
)) {
1187 struct kvm_mmu_page
*shadow
;
1189 if (largepage
&& has_wrprotected_page(vcpu
->kvm
, gfn
)) {
1191 spte
= shadow_trap_nonpresent_pte
;
1195 spte
|= PT_WRITABLE_MASK
;
1197 shadow
= kvm_mmu_lookup_page(vcpu
->kvm
, gfn
);
1199 pgprintk("%s: found shadow page for %lx, marking ro\n",
1202 pte_access
&= ~ACC_WRITE_MASK
;
1203 if (is_writeble_pte(spte
))
1204 spte
&= ~PT_WRITABLE_MASK
;
1208 if (pte_access
& ACC_WRITE_MASK
)
1209 mark_page_dirty(vcpu
->kvm
, gfn
);
1212 set_shadow_pte(shadow_pte
, spte
);
1217 static void mmu_set_spte(struct kvm_vcpu
*vcpu
, u64
*shadow_pte
,
1218 unsigned pt_access
, unsigned pte_access
,
1219 int user_fault
, int write_fault
, int dirty
,
1220 int *ptwrite
, int largepage
, gfn_t gfn
,
1221 pfn_t pfn
, bool speculative
)
1223 int was_rmapped
= 0;
1224 int was_writeble
= is_writeble_pte(*shadow_pte
);
1226 pgprintk("%s: spte %llx access %x write_fault %d"
1227 " user_fault %d gfn %lx\n",
1228 __func__
, *shadow_pte
, pt_access
,
1229 write_fault
, user_fault
, gfn
);
1231 if (is_rmap_pte(*shadow_pte
)) {
1233 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1234 * the parent of the now unreachable PTE.
1236 if (largepage
&& !is_large_pte(*shadow_pte
)) {
1237 struct kvm_mmu_page
*child
;
1238 u64 pte
= *shadow_pte
;
1240 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
1241 mmu_page_remove_parent_pte(child
, shadow_pte
);
1242 } else if (pfn
!= spte_to_pfn(*shadow_pte
)) {
1243 pgprintk("hfn old %lx new %lx\n",
1244 spte_to_pfn(*shadow_pte
), pfn
);
1245 rmap_remove(vcpu
->kvm
, shadow_pte
);
1248 was_rmapped
= is_large_pte(*shadow_pte
);
1253 if (set_spte(vcpu
, shadow_pte
, pte_access
, user_fault
, write_fault
,
1254 dirty
, largepage
, gfn
, pfn
, speculative
)) {
1257 kvm_x86_ops
->tlb_flush(vcpu
);
1260 pgprintk("%s: setting spte %llx\n", __func__
, *shadow_pte
);
1261 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1262 is_large_pte(*shadow_pte
)? "2MB" : "4kB",
1263 is_present_pte(*shadow_pte
)?"RW":"R", gfn
,
1264 *shadow_pte
, shadow_pte
);
1265 if (!was_rmapped
&& is_large_pte(*shadow_pte
))
1266 ++vcpu
->kvm
->stat
.lpages
;
1268 page_header_update_slot(vcpu
->kvm
, shadow_pte
, gfn
);
1270 rmap_add(vcpu
, shadow_pte
, gfn
, largepage
);
1271 if (!is_rmap_pte(*shadow_pte
))
1272 kvm_release_pfn_clean(pfn
);
1275 kvm_release_pfn_dirty(pfn
);
1277 kvm_release_pfn_clean(pfn
);
1280 vcpu
->arch
.last_pte_updated
= shadow_pte
;
1281 vcpu
->arch
.last_pte_gfn
= gfn
;
1285 static void nonpaging_new_cr3(struct kvm_vcpu
*vcpu
)
1289 struct direct_shadow_walk
{
1290 struct kvm_shadow_walk walker
;
1297 static int direct_map_entry(struct kvm_shadow_walk
*_walk
,
1298 struct kvm_vcpu
*vcpu
,
1299 u64 addr
, u64
*sptep
, int level
)
1301 struct direct_shadow_walk
*walk
=
1302 container_of(_walk
, struct direct_shadow_walk
, walker
);
1303 struct kvm_mmu_page
*sp
;
1305 gfn_t gfn
= addr
>> PAGE_SHIFT
;
1307 if (level
== PT_PAGE_TABLE_LEVEL
1308 || (walk
->largepage
&& level
== PT_DIRECTORY_LEVEL
)) {
1309 mmu_set_spte(vcpu
, sptep
, ACC_ALL
, ACC_ALL
,
1310 0, walk
->write
, 1, &walk
->pt_write
,
1311 walk
->largepage
, gfn
, walk
->pfn
, false);
1312 ++vcpu
->stat
.pf_fixed
;
1316 if (*sptep
== shadow_trap_nonpresent_pte
) {
1317 pseudo_gfn
= (addr
& PT64_DIR_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
1318 sp
= kvm_mmu_get_page(vcpu
, pseudo_gfn
, (gva_t
)addr
, level
- 1,
1321 pgprintk("nonpaging_map: ENOMEM\n");
1322 kvm_release_pfn_clean(walk
->pfn
);
1326 set_shadow_pte(sptep
,
1328 | PT_PRESENT_MASK
| PT_WRITABLE_MASK
1329 | shadow_user_mask
| shadow_x_mask
);
1334 static int __direct_map(struct kvm_vcpu
*vcpu
, gpa_t v
, int write
,
1335 int largepage
, gfn_t gfn
, pfn_t pfn
)
1338 struct direct_shadow_walk walker
= {
1339 .walker
= { .entry
= direct_map_entry
, },
1341 .largepage
= largepage
,
1346 r
= walk_shadow(&walker
.walker
, vcpu
, gfn
<< PAGE_SHIFT
);
1349 return walker
.pt_write
;
1352 static int nonpaging_map(struct kvm_vcpu
*vcpu
, gva_t v
, int write
, gfn_t gfn
)
1357 unsigned long mmu_seq
;
1359 if (is_largepage_backed(vcpu
, gfn
& ~(KVM_PAGES_PER_HPAGE
-1))) {
1360 gfn
&= ~(KVM_PAGES_PER_HPAGE
-1);
1364 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1366 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
1369 if (is_error_pfn(pfn
)) {
1370 kvm_release_pfn_clean(pfn
);
1374 spin_lock(&vcpu
->kvm
->mmu_lock
);
1375 if (mmu_notifier_retry(vcpu
, mmu_seq
))
1377 kvm_mmu_free_some_pages(vcpu
);
1378 r
= __direct_map(vcpu
, v
, write
, largepage
, gfn
, pfn
);
1379 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1385 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1386 kvm_release_pfn_clean(pfn
);
1391 static void mmu_free_roots(struct kvm_vcpu
*vcpu
)
1394 struct kvm_mmu_page
*sp
;
1396 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
1398 spin_lock(&vcpu
->kvm
->mmu_lock
);
1399 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1400 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1402 sp
= page_header(root
);
1404 if (!sp
->root_count
&& sp
->role
.invalid
)
1405 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1406 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1407 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1410 for (i
= 0; i
< 4; ++i
) {
1411 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1414 root
&= PT64_BASE_ADDR_MASK
;
1415 sp
= page_header(root
);
1417 if (!sp
->root_count
&& sp
->role
.invalid
)
1418 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1420 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
1422 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1423 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1426 static void mmu_alloc_roots(struct kvm_vcpu
*vcpu
)
1430 struct kvm_mmu_page
*sp
;
1431 int metaphysical
= 0;
1433 root_gfn
= vcpu
->arch
.cr3
>> PAGE_SHIFT
;
1435 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1436 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1438 ASSERT(!VALID_PAGE(root
));
1441 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, 0,
1442 PT64_ROOT_LEVEL
, metaphysical
,
1444 root
= __pa(sp
->spt
);
1446 vcpu
->arch
.mmu
.root_hpa
= root
;
1449 metaphysical
= !is_paging(vcpu
);
1452 for (i
= 0; i
< 4; ++i
) {
1453 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1455 ASSERT(!VALID_PAGE(root
));
1456 if (vcpu
->arch
.mmu
.root_level
== PT32E_ROOT_LEVEL
) {
1457 if (!is_present_pte(vcpu
->arch
.pdptrs
[i
])) {
1458 vcpu
->arch
.mmu
.pae_root
[i
] = 0;
1461 root_gfn
= vcpu
->arch
.pdptrs
[i
] >> PAGE_SHIFT
;
1462 } else if (vcpu
->arch
.mmu
.root_level
== 0)
1464 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, i
<< 30,
1465 PT32_ROOT_LEVEL
, metaphysical
,
1467 root
= __pa(sp
->spt
);
1469 vcpu
->arch
.mmu
.pae_root
[i
] = root
| PT_PRESENT_MASK
;
1471 vcpu
->arch
.mmu
.root_hpa
= __pa(vcpu
->arch
.mmu
.pae_root
);
1474 static gpa_t
nonpaging_gva_to_gpa(struct kvm_vcpu
*vcpu
, gva_t vaddr
)
1479 static int nonpaging_page_fault(struct kvm_vcpu
*vcpu
, gva_t gva
,
1485 pgprintk("%s: gva %lx error %x\n", __func__
, gva
, error_code
);
1486 r
= mmu_topup_memory_caches(vcpu
);
1491 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1493 gfn
= gva
>> PAGE_SHIFT
;
1495 return nonpaging_map(vcpu
, gva
& PAGE_MASK
,
1496 error_code
& PFERR_WRITE_MASK
, gfn
);
1499 static int tdp_page_fault(struct kvm_vcpu
*vcpu
, gva_t gpa
,
1505 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1506 unsigned long mmu_seq
;
1509 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1511 r
= mmu_topup_memory_caches(vcpu
);
1515 if (is_largepage_backed(vcpu
, gfn
& ~(KVM_PAGES_PER_HPAGE
-1))) {
1516 gfn
&= ~(KVM_PAGES_PER_HPAGE
-1);
1519 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1521 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
1522 if (is_error_pfn(pfn
)) {
1523 kvm_release_pfn_clean(pfn
);
1526 spin_lock(&vcpu
->kvm
->mmu_lock
);
1527 if (mmu_notifier_retry(vcpu
, mmu_seq
))
1529 kvm_mmu_free_some_pages(vcpu
);
1530 r
= __direct_map(vcpu
, gpa
, error_code
& PFERR_WRITE_MASK
,
1531 largepage
, gfn
, pfn
);
1532 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1537 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1538 kvm_release_pfn_clean(pfn
);
1542 static void nonpaging_free(struct kvm_vcpu
*vcpu
)
1544 mmu_free_roots(vcpu
);
1547 static int nonpaging_init_context(struct kvm_vcpu
*vcpu
)
1549 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1551 context
->new_cr3
= nonpaging_new_cr3
;
1552 context
->page_fault
= nonpaging_page_fault
;
1553 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
1554 context
->free
= nonpaging_free
;
1555 context
->prefetch_page
= nonpaging_prefetch_page
;
1556 context
->sync_page
= nonpaging_sync_page
;
1557 context
->root_level
= 0;
1558 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
1559 context
->root_hpa
= INVALID_PAGE
;
1563 void kvm_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
1565 ++vcpu
->stat
.tlb_flush
;
1566 kvm_x86_ops
->tlb_flush(vcpu
);
1569 static void paging_new_cr3(struct kvm_vcpu
*vcpu
)
1571 pgprintk("%s: cr3 %lx\n", __func__
, vcpu
->arch
.cr3
);
1572 mmu_free_roots(vcpu
);
1575 static void inject_page_fault(struct kvm_vcpu
*vcpu
,
1579 kvm_inject_page_fault(vcpu
, addr
, err_code
);
1582 static void paging_free(struct kvm_vcpu
*vcpu
)
1584 nonpaging_free(vcpu
);
1588 #include "paging_tmpl.h"
1592 #include "paging_tmpl.h"
1595 static int paging64_init_context_common(struct kvm_vcpu
*vcpu
, int level
)
1597 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1599 ASSERT(is_pae(vcpu
));
1600 context
->new_cr3
= paging_new_cr3
;
1601 context
->page_fault
= paging64_page_fault
;
1602 context
->gva_to_gpa
= paging64_gva_to_gpa
;
1603 context
->prefetch_page
= paging64_prefetch_page
;
1604 context
->sync_page
= paging64_sync_page
;
1605 context
->free
= paging_free
;
1606 context
->root_level
= level
;
1607 context
->shadow_root_level
= level
;
1608 context
->root_hpa
= INVALID_PAGE
;
1612 static int paging64_init_context(struct kvm_vcpu
*vcpu
)
1614 return paging64_init_context_common(vcpu
, PT64_ROOT_LEVEL
);
1617 static int paging32_init_context(struct kvm_vcpu
*vcpu
)
1619 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1621 context
->new_cr3
= paging_new_cr3
;
1622 context
->page_fault
= paging32_page_fault
;
1623 context
->gva_to_gpa
= paging32_gva_to_gpa
;
1624 context
->free
= paging_free
;
1625 context
->prefetch_page
= paging32_prefetch_page
;
1626 context
->sync_page
= paging32_sync_page
;
1627 context
->root_level
= PT32_ROOT_LEVEL
;
1628 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
1629 context
->root_hpa
= INVALID_PAGE
;
1633 static int paging32E_init_context(struct kvm_vcpu
*vcpu
)
1635 return paging64_init_context_common(vcpu
, PT32E_ROOT_LEVEL
);
1638 static int init_kvm_tdp_mmu(struct kvm_vcpu
*vcpu
)
1640 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1642 context
->new_cr3
= nonpaging_new_cr3
;
1643 context
->page_fault
= tdp_page_fault
;
1644 context
->free
= nonpaging_free
;
1645 context
->prefetch_page
= nonpaging_prefetch_page
;
1646 context
->sync_page
= nonpaging_sync_page
;
1647 context
->shadow_root_level
= kvm_x86_ops
->get_tdp_level();
1648 context
->root_hpa
= INVALID_PAGE
;
1650 if (!is_paging(vcpu
)) {
1651 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
1652 context
->root_level
= 0;
1653 } else if (is_long_mode(vcpu
)) {
1654 context
->gva_to_gpa
= paging64_gva_to_gpa
;
1655 context
->root_level
= PT64_ROOT_LEVEL
;
1656 } else if (is_pae(vcpu
)) {
1657 context
->gva_to_gpa
= paging64_gva_to_gpa
;
1658 context
->root_level
= PT32E_ROOT_LEVEL
;
1660 context
->gva_to_gpa
= paging32_gva_to_gpa
;
1661 context
->root_level
= PT32_ROOT_LEVEL
;
1667 static int init_kvm_softmmu(struct kvm_vcpu
*vcpu
)
1670 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1672 if (!is_paging(vcpu
))
1673 return nonpaging_init_context(vcpu
);
1674 else if (is_long_mode(vcpu
))
1675 return paging64_init_context(vcpu
);
1676 else if (is_pae(vcpu
))
1677 return paging32E_init_context(vcpu
);
1679 return paging32_init_context(vcpu
);
1682 static int init_kvm_mmu(struct kvm_vcpu
*vcpu
)
1684 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
1687 return init_kvm_tdp_mmu(vcpu
);
1689 return init_kvm_softmmu(vcpu
);
1692 static void destroy_kvm_mmu(struct kvm_vcpu
*vcpu
)
1695 if (VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
)) {
1696 vcpu
->arch
.mmu
.free(vcpu
);
1697 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1701 int kvm_mmu_reset_context(struct kvm_vcpu
*vcpu
)
1703 destroy_kvm_mmu(vcpu
);
1704 return init_kvm_mmu(vcpu
);
1706 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context
);
1708 int kvm_mmu_load(struct kvm_vcpu
*vcpu
)
1712 r
= mmu_topup_memory_caches(vcpu
);
1715 spin_lock(&vcpu
->kvm
->mmu_lock
);
1716 kvm_mmu_free_some_pages(vcpu
);
1717 mmu_alloc_roots(vcpu
);
1718 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1719 kvm_x86_ops
->set_cr3(vcpu
, vcpu
->arch
.mmu
.root_hpa
);
1720 kvm_mmu_flush_tlb(vcpu
);
1724 EXPORT_SYMBOL_GPL(kvm_mmu_load
);
1726 void kvm_mmu_unload(struct kvm_vcpu
*vcpu
)
1728 mmu_free_roots(vcpu
);
1731 static void mmu_pte_write_zap_pte(struct kvm_vcpu
*vcpu
,
1732 struct kvm_mmu_page
*sp
,
1736 struct kvm_mmu_page
*child
;
1739 if (is_shadow_present_pte(pte
)) {
1740 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
||
1742 rmap_remove(vcpu
->kvm
, spte
);
1744 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
1745 mmu_page_remove_parent_pte(child
, spte
);
1748 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
1749 if (is_large_pte(pte
))
1750 --vcpu
->kvm
->stat
.lpages
;
1753 static void mmu_pte_write_new_pte(struct kvm_vcpu
*vcpu
,
1754 struct kvm_mmu_page
*sp
,
1758 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
) {
1759 if (!vcpu
->arch
.update_pte
.largepage
||
1760 sp
->role
.glevels
== PT32_ROOT_LEVEL
) {
1761 ++vcpu
->kvm
->stat
.mmu_pde_zapped
;
1766 ++vcpu
->kvm
->stat
.mmu_pte_updated
;
1767 if (sp
->role
.glevels
== PT32_ROOT_LEVEL
)
1768 paging32_update_pte(vcpu
, sp
, spte
, new);
1770 paging64_update_pte(vcpu
, sp
, spte
, new);
1773 static bool need_remote_flush(u64 old
, u64
new)
1775 if (!is_shadow_present_pte(old
))
1777 if (!is_shadow_present_pte(new))
1779 if ((old
^ new) & PT64_BASE_ADDR_MASK
)
1781 old
^= PT64_NX_MASK
;
1782 new ^= PT64_NX_MASK
;
1783 return (old
& ~new & PT64_PERM_MASK
) != 0;
1786 static void mmu_pte_write_flush_tlb(struct kvm_vcpu
*vcpu
, u64 old
, u64
new)
1788 if (need_remote_flush(old
, new))
1789 kvm_flush_remote_tlbs(vcpu
->kvm
);
1791 kvm_mmu_flush_tlb(vcpu
);
1794 static bool last_updated_pte_accessed(struct kvm_vcpu
*vcpu
)
1796 u64
*spte
= vcpu
->arch
.last_pte_updated
;
1798 return !!(spte
&& (*spte
& shadow_accessed_mask
));
1801 static void mmu_guess_page_from_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1802 const u8
*new, int bytes
)
1809 vcpu
->arch
.update_pte
.largepage
= 0;
1811 if (bytes
!= 4 && bytes
!= 8)
1815 * Assume that the pte write on a page table of the same type
1816 * as the current vcpu paging mode. This is nearly always true
1817 * (might be false while changing modes). Note it is verified later
1821 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1822 if ((bytes
== 4) && (gpa
% 4 == 0)) {
1823 r
= kvm_read_guest(vcpu
->kvm
, gpa
& ~(u64
)7, &gpte
, 8);
1826 memcpy((void *)&gpte
+ (gpa
% 8), new, 4);
1827 } else if ((bytes
== 8) && (gpa
% 8 == 0)) {
1828 memcpy((void *)&gpte
, new, 8);
1831 if ((bytes
== 4) && (gpa
% 4 == 0))
1832 memcpy((void *)&gpte
, new, 4);
1834 if (!is_present_pte(gpte
))
1836 gfn
= (gpte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
1838 if (is_large_pte(gpte
) && is_largepage_backed(vcpu
, gfn
)) {
1839 gfn
&= ~(KVM_PAGES_PER_HPAGE
-1);
1840 vcpu
->arch
.update_pte
.largepage
= 1;
1842 vcpu
->arch
.update_pte
.mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1844 pfn
= gfn_to_pfn(vcpu
->kvm
, gfn
);
1846 if (is_error_pfn(pfn
)) {
1847 kvm_release_pfn_clean(pfn
);
1850 vcpu
->arch
.update_pte
.gfn
= gfn
;
1851 vcpu
->arch
.update_pte
.pfn
= pfn
;
1854 static void kvm_mmu_access_page(struct kvm_vcpu
*vcpu
, gfn_t gfn
)
1856 u64
*spte
= vcpu
->arch
.last_pte_updated
;
1859 && vcpu
->arch
.last_pte_gfn
== gfn
1860 && shadow_accessed_mask
1861 && !(*spte
& shadow_accessed_mask
)
1862 && is_shadow_present_pte(*spte
))
1863 set_bit(PT_ACCESSED_SHIFT
, (unsigned long *)spte
);
1866 void kvm_mmu_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1867 const u8
*new, int bytes
)
1869 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1870 struct kvm_mmu_page
*sp
;
1871 struct hlist_node
*node
, *n
;
1872 struct hlist_head
*bucket
;
1876 unsigned offset
= offset_in_page(gpa
);
1878 unsigned page_offset
;
1879 unsigned misaligned
;
1886 pgprintk("%s: gpa %llx bytes %d\n", __func__
, gpa
, bytes
);
1887 mmu_guess_page_from_pte_write(vcpu
, gpa
, new, bytes
);
1888 spin_lock(&vcpu
->kvm
->mmu_lock
);
1889 kvm_mmu_access_page(vcpu
, gfn
);
1890 kvm_mmu_free_some_pages(vcpu
);
1891 ++vcpu
->kvm
->stat
.mmu_pte_write
;
1892 kvm_mmu_audit(vcpu
, "pre pte write");
1893 if (gfn
== vcpu
->arch
.last_pt_write_gfn
1894 && !last_updated_pte_accessed(vcpu
)) {
1895 ++vcpu
->arch
.last_pt_write_count
;
1896 if (vcpu
->arch
.last_pt_write_count
>= 3)
1899 vcpu
->arch
.last_pt_write_gfn
= gfn
;
1900 vcpu
->arch
.last_pt_write_count
= 1;
1901 vcpu
->arch
.last_pte_updated
= NULL
;
1903 index
= kvm_page_table_hashfn(gfn
);
1904 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1905 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
) {
1906 if (sp
->gfn
!= gfn
|| sp
->role
.metaphysical
|| sp
->role
.invalid
)
1908 pte_size
= sp
->role
.glevels
== PT32_ROOT_LEVEL
? 4 : 8;
1909 misaligned
= (offset
^ (offset
+ bytes
- 1)) & ~(pte_size
- 1);
1910 misaligned
|= bytes
< 4;
1911 if (misaligned
|| flooded
) {
1913 * Misaligned accesses are too much trouble to fix
1914 * up; also, they usually indicate a page is not used
1917 * If we're seeing too many writes to a page,
1918 * it may no longer be a page table, or we may be
1919 * forking, in which case it is better to unmap the
1922 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1923 gpa
, bytes
, sp
->role
.word
);
1924 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1925 ++vcpu
->kvm
->stat
.mmu_flooded
;
1928 page_offset
= offset
;
1929 level
= sp
->role
.level
;
1931 if (sp
->role
.glevels
== PT32_ROOT_LEVEL
) {
1932 page_offset
<<= 1; /* 32->64 */
1934 * A 32-bit pde maps 4MB while the shadow pdes map
1935 * only 2MB. So we need to double the offset again
1936 * and zap two pdes instead of one.
1938 if (level
== PT32_ROOT_LEVEL
) {
1939 page_offset
&= ~7; /* kill rounding error */
1943 quadrant
= page_offset
>> PAGE_SHIFT
;
1944 page_offset
&= ~PAGE_MASK
;
1945 if (quadrant
!= sp
->role
.quadrant
)
1948 spte
= &sp
->spt
[page_offset
/ sizeof(*spte
)];
1949 if ((gpa
& (pte_size
- 1)) || (bytes
< pte_size
)) {
1951 r
= kvm_read_guest_atomic(vcpu
->kvm
,
1952 gpa
& ~(u64
)(pte_size
- 1),
1954 new = (const void *)&gentry
;
1960 mmu_pte_write_zap_pte(vcpu
, sp
, spte
);
1962 mmu_pte_write_new_pte(vcpu
, sp
, spte
, new);
1963 mmu_pte_write_flush_tlb(vcpu
, entry
, *spte
);
1967 kvm_mmu_audit(vcpu
, "post pte write");
1968 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1969 if (!is_error_pfn(vcpu
->arch
.update_pte
.pfn
)) {
1970 kvm_release_pfn_clean(vcpu
->arch
.update_pte
.pfn
);
1971 vcpu
->arch
.update_pte
.pfn
= bad_pfn
;
1975 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu
*vcpu
, gva_t gva
)
1980 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
);
1982 spin_lock(&vcpu
->kvm
->mmu_lock
);
1983 r
= kvm_mmu_unprotect_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1984 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1987 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt
);
1989 void __kvm_mmu_free_some_pages(struct kvm_vcpu
*vcpu
)
1991 while (vcpu
->kvm
->arch
.n_free_mmu_pages
< KVM_REFILL_PAGES
) {
1992 struct kvm_mmu_page
*sp
;
1994 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.prev
,
1995 struct kvm_mmu_page
, link
);
1996 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1997 ++vcpu
->kvm
->stat
.mmu_recycled
;
2001 int kvm_mmu_page_fault(struct kvm_vcpu
*vcpu
, gva_t cr2
, u32 error_code
)
2004 enum emulation_result er
;
2006 r
= vcpu
->arch
.mmu
.page_fault(vcpu
, cr2
, error_code
);
2015 r
= mmu_topup_memory_caches(vcpu
);
2019 er
= emulate_instruction(vcpu
, vcpu
->run
, cr2
, error_code
, 0);
2024 case EMULATE_DO_MMIO
:
2025 ++vcpu
->stat
.mmio_exits
;
2028 kvm_report_emulation_failure(vcpu
, "pagetable");
2036 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault
);
2038 void kvm_enable_tdp(void)
2042 EXPORT_SYMBOL_GPL(kvm_enable_tdp
);
2044 void kvm_disable_tdp(void)
2046 tdp_enabled
= false;
2048 EXPORT_SYMBOL_GPL(kvm_disable_tdp
);
2050 static void free_mmu_pages(struct kvm_vcpu
*vcpu
)
2052 struct kvm_mmu_page
*sp
;
2054 while (!list_empty(&vcpu
->kvm
->arch
.active_mmu_pages
)) {
2055 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.next
,
2056 struct kvm_mmu_page
, link
);
2057 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
2060 free_page((unsigned long)vcpu
->arch
.mmu
.pae_root
);
2063 static int alloc_mmu_pages(struct kvm_vcpu
*vcpu
)
2070 if (vcpu
->kvm
->arch
.n_requested_mmu_pages
)
2071 vcpu
->kvm
->arch
.n_free_mmu_pages
=
2072 vcpu
->kvm
->arch
.n_requested_mmu_pages
;
2074 vcpu
->kvm
->arch
.n_free_mmu_pages
=
2075 vcpu
->kvm
->arch
.n_alloc_mmu_pages
;
2077 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2078 * Therefore we need to allocate shadow page tables in the first
2079 * 4GB of memory, which happens to fit the DMA32 zone.
2081 page
= alloc_page(GFP_KERNEL
| __GFP_DMA32
);
2084 vcpu
->arch
.mmu
.pae_root
= page_address(page
);
2085 for (i
= 0; i
< 4; ++i
)
2086 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
2091 free_mmu_pages(vcpu
);
2095 int kvm_mmu_create(struct kvm_vcpu
*vcpu
)
2098 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2100 return alloc_mmu_pages(vcpu
);
2103 int kvm_mmu_setup(struct kvm_vcpu
*vcpu
)
2106 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
2108 return init_kvm_mmu(vcpu
);
2111 void kvm_mmu_destroy(struct kvm_vcpu
*vcpu
)
2115 destroy_kvm_mmu(vcpu
);
2116 free_mmu_pages(vcpu
);
2117 mmu_free_memory_caches(vcpu
);
2120 void kvm_mmu_slot_remove_write_access(struct kvm
*kvm
, int slot
)
2122 struct kvm_mmu_page
*sp
;
2124 spin_lock(&kvm
->mmu_lock
);
2125 list_for_each_entry(sp
, &kvm
->arch
.active_mmu_pages
, link
) {
2129 if (!test_bit(slot
, &sp
->slot_bitmap
))
2133 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
2135 if (pt
[i
] & PT_WRITABLE_MASK
)
2136 pt
[i
] &= ~PT_WRITABLE_MASK
;
2138 kvm_flush_remote_tlbs(kvm
);
2139 spin_unlock(&kvm
->mmu_lock
);
2142 void kvm_mmu_zap_all(struct kvm
*kvm
)
2144 struct kvm_mmu_page
*sp
, *node
;
2146 spin_lock(&kvm
->mmu_lock
);
2147 list_for_each_entry_safe(sp
, node
, &kvm
->arch
.active_mmu_pages
, link
)
2148 kvm_mmu_zap_page(kvm
, sp
);
2149 spin_unlock(&kvm
->mmu_lock
);
2151 kvm_flush_remote_tlbs(kvm
);
2154 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm
*kvm
)
2156 struct kvm_mmu_page
*page
;
2158 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
2159 struct kvm_mmu_page
, link
);
2160 kvm_mmu_zap_page(kvm
, page
);
2163 static int mmu_shrink(int nr_to_scan
, gfp_t gfp_mask
)
2166 struct kvm
*kvm_freed
= NULL
;
2167 int cache_count
= 0;
2169 spin_lock(&kvm_lock
);
2171 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
2174 if (!down_read_trylock(&kvm
->slots_lock
))
2176 spin_lock(&kvm
->mmu_lock
);
2177 npages
= kvm
->arch
.n_alloc_mmu_pages
-
2178 kvm
->arch
.n_free_mmu_pages
;
2179 cache_count
+= npages
;
2180 if (!kvm_freed
&& nr_to_scan
> 0 && npages
> 0) {
2181 kvm_mmu_remove_one_alloc_mmu_page(kvm
);
2187 spin_unlock(&kvm
->mmu_lock
);
2188 up_read(&kvm
->slots_lock
);
2191 list_move_tail(&kvm_freed
->vm_list
, &vm_list
);
2193 spin_unlock(&kvm_lock
);
2198 static struct shrinker mmu_shrinker
= {
2199 .shrink
= mmu_shrink
,
2200 .seeks
= DEFAULT_SEEKS
* 10,
2203 static void mmu_destroy_caches(void)
2205 if (pte_chain_cache
)
2206 kmem_cache_destroy(pte_chain_cache
);
2207 if (rmap_desc_cache
)
2208 kmem_cache_destroy(rmap_desc_cache
);
2209 if (mmu_page_header_cache
)
2210 kmem_cache_destroy(mmu_page_header_cache
);
2213 void kvm_mmu_module_exit(void)
2215 mmu_destroy_caches();
2216 unregister_shrinker(&mmu_shrinker
);
2219 int kvm_mmu_module_init(void)
2221 pte_chain_cache
= kmem_cache_create("kvm_pte_chain",
2222 sizeof(struct kvm_pte_chain
),
2224 if (!pte_chain_cache
)
2226 rmap_desc_cache
= kmem_cache_create("kvm_rmap_desc",
2227 sizeof(struct kvm_rmap_desc
),
2229 if (!rmap_desc_cache
)
2232 mmu_page_header_cache
= kmem_cache_create("kvm_mmu_page_header",
2233 sizeof(struct kvm_mmu_page
),
2235 if (!mmu_page_header_cache
)
2238 register_shrinker(&mmu_shrinker
);
2243 mmu_destroy_caches();
2248 * Caculate mmu pages needed for kvm.
2250 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm
*kvm
)
2253 unsigned int nr_mmu_pages
;
2254 unsigned int nr_pages
= 0;
2256 for (i
= 0; i
< kvm
->nmemslots
; i
++)
2257 nr_pages
+= kvm
->memslots
[i
].npages
;
2259 nr_mmu_pages
= nr_pages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
2260 nr_mmu_pages
= max(nr_mmu_pages
,
2261 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
2263 return nr_mmu_pages
;
2266 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
2269 if (len
> buffer
->len
)
2274 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer
*buffer
,
2279 ret
= pv_mmu_peek_buffer(buffer
, len
);
2284 buffer
->processed
+= len
;
2288 static int kvm_pv_mmu_write(struct kvm_vcpu
*vcpu
,
2289 gpa_t addr
, gpa_t value
)
2294 if (!is_long_mode(vcpu
) && !is_pae(vcpu
))
2297 r
= mmu_topup_memory_caches(vcpu
);
2301 if (!emulator_write_phys(vcpu
, addr
, &value
, bytes
))
2307 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
2309 kvm_x86_ops
->tlb_flush(vcpu
);
2313 static int kvm_pv_mmu_release_pt(struct kvm_vcpu
*vcpu
, gpa_t addr
)
2315 spin_lock(&vcpu
->kvm
->mmu_lock
);
2316 mmu_unshadow(vcpu
->kvm
, addr
>> PAGE_SHIFT
);
2317 spin_unlock(&vcpu
->kvm
->mmu_lock
);
2321 static int kvm_pv_mmu_op_one(struct kvm_vcpu
*vcpu
,
2322 struct kvm_pv_mmu_op_buffer
*buffer
)
2324 struct kvm_mmu_op_header
*header
;
2326 header
= pv_mmu_peek_buffer(buffer
, sizeof *header
);
2329 switch (header
->op
) {
2330 case KVM_MMU_OP_WRITE_PTE
: {
2331 struct kvm_mmu_op_write_pte
*wpte
;
2333 wpte
= pv_mmu_read_buffer(buffer
, sizeof *wpte
);
2336 return kvm_pv_mmu_write(vcpu
, wpte
->pte_phys
,
2339 case KVM_MMU_OP_FLUSH_TLB
: {
2340 struct kvm_mmu_op_flush_tlb
*ftlb
;
2342 ftlb
= pv_mmu_read_buffer(buffer
, sizeof *ftlb
);
2345 return kvm_pv_mmu_flush_tlb(vcpu
);
2347 case KVM_MMU_OP_RELEASE_PT
: {
2348 struct kvm_mmu_op_release_pt
*rpt
;
2350 rpt
= pv_mmu_read_buffer(buffer
, sizeof *rpt
);
2353 return kvm_pv_mmu_release_pt(vcpu
, rpt
->pt_phys
);
2359 int kvm_pv_mmu_op(struct kvm_vcpu
*vcpu
, unsigned long bytes
,
2360 gpa_t addr
, unsigned long *ret
)
2363 struct kvm_pv_mmu_op_buffer
*buffer
= &vcpu
->arch
.mmu_op_buffer
;
2365 buffer
->ptr
= buffer
->buf
;
2366 buffer
->len
= min_t(unsigned long, bytes
, sizeof buffer
->buf
);
2367 buffer
->processed
= 0;
2369 r
= kvm_read_guest(vcpu
->kvm
, addr
, buffer
->buf
, buffer
->len
);
2373 while (buffer
->len
) {
2374 r
= kvm_pv_mmu_op_one(vcpu
, buffer
);
2383 *ret
= buffer
->processed
;
2389 static const char *audit_msg
;
2391 static gva_t
canonicalize(gva_t gva
)
2393 #ifdef CONFIG_X86_64
2394 gva
= (long long)(gva
<< 16) >> 16;
2399 static void audit_mappings_page(struct kvm_vcpu
*vcpu
, u64 page_pte
,
2400 gva_t va
, int level
)
2402 u64
*pt
= __va(page_pte
& PT64_BASE_ADDR_MASK
);
2404 gva_t va_delta
= 1ul << (PAGE_SHIFT
+ 9 * (level
- 1));
2406 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
, va
+= va_delta
) {
2409 if (ent
== shadow_trap_nonpresent_pte
)
2412 va
= canonicalize(va
);
2414 if (ent
== shadow_notrap_nonpresent_pte
)
2415 printk(KERN_ERR
"audit: (%s) nontrapping pte"
2416 " in nonleaf level: levels %d gva %lx"
2417 " level %d pte %llx\n", audit_msg
,
2418 vcpu
->arch
.mmu
.root_level
, va
, level
, ent
);
2420 audit_mappings_page(vcpu
, ent
, va
, level
- 1);
2422 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, va
);
2423 hpa_t hpa
= (hpa_t
)gpa_to_pfn(vcpu
, gpa
) << PAGE_SHIFT
;
2425 if (is_shadow_present_pte(ent
)
2426 && (ent
& PT64_BASE_ADDR_MASK
) != hpa
)
2427 printk(KERN_ERR
"xx audit error: (%s) levels %d"
2428 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
2429 audit_msg
, vcpu
->arch
.mmu
.root_level
,
2431 is_shadow_present_pte(ent
));
2432 else if (ent
== shadow_notrap_nonpresent_pte
2433 && !is_error_hpa(hpa
))
2434 printk(KERN_ERR
"audit: (%s) notrap shadow,"
2435 " valid guest gva %lx\n", audit_msg
, va
);
2436 kvm_release_pfn_clean(pfn
);
2442 static void audit_mappings(struct kvm_vcpu
*vcpu
)
2446 if (vcpu
->arch
.mmu
.root_level
== 4)
2447 audit_mappings_page(vcpu
, vcpu
->arch
.mmu
.root_hpa
, 0, 4);
2449 for (i
= 0; i
< 4; ++i
)
2450 if (vcpu
->arch
.mmu
.pae_root
[i
] & PT_PRESENT_MASK
)
2451 audit_mappings_page(vcpu
,
2452 vcpu
->arch
.mmu
.pae_root
[i
],
2457 static int count_rmaps(struct kvm_vcpu
*vcpu
)
2462 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
2463 struct kvm_memory_slot
*m
= &vcpu
->kvm
->memslots
[i
];
2464 struct kvm_rmap_desc
*d
;
2466 for (j
= 0; j
< m
->npages
; ++j
) {
2467 unsigned long *rmapp
= &m
->rmap
[j
];
2471 if (!(*rmapp
& 1)) {
2475 d
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
2477 for (k
= 0; k
< RMAP_EXT
; ++k
)
2478 if (d
->shadow_ptes
[k
])
2489 static int count_writable_mappings(struct kvm_vcpu
*vcpu
)
2492 struct kvm_mmu_page
*sp
;
2495 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
2498 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
2501 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
2504 if (!(ent
& PT_PRESENT_MASK
))
2506 if (!(ent
& PT_WRITABLE_MASK
))
2514 static void audit_rmap(struct kvm_vcpu
*vcpu
)
2516 int n_rmap
= count_rmaps(vcpu
);
2517 int n_actual
= count_writable_mappings(vcpu
);
2519 if (n_rmap
!= n_actual
)
2520 printk(KERN_ERR
"%s: (%s) rmap %d actual %d\n",
2521 __func__
, audit_msg
, n_rmap
, n_actual
);
2524 static void audit_write_protection(struct kvm_vcpu
*vcpu
)
2526 struct kvm_mmu_page
*sp
;
2527 struct kvm_memory_slot
*slot
;
2528 unsigned long *rmapp
;
2531 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
2532 if (sp
->role
.metaphysical
)
2535 slot
= gfn_to_memslot(vcpu
->kvm
, sp
->gfn
);
2536 gfn
= unalias_gfn(vcpu
->kvm
, sp
->gfn
);
2537 rmapp
= &slot
->rmap
[gfn
- slot
->base_gfn
];
2539 printk(KERN_ERR
"%s: (%s) shadow page has writable"
2540 " mappings: gfn %lx role %x\n",
2541 __func__
, audit_msg
, sp
->gfn
,
2546 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
)
2553 audit_write_protection(vcpu
);
2554 audit_mappings(vcpu
);