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>
32 #include <asm/cmpxchg.h>
40 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
);
42 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
) {}
47 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
48 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
52 #define pgprintk(x...) do { } while (0)
53 #define rmap_printk(x...) do { } while (0)
57 #if defined(MMU_DEBUG) || defined(AUDIT)
62 #define ASSERT(x) do { } while (0)
66 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
67 __FILE__, __LINE__, #x); \
71 #define PT64_PT_BITS 9
72 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
73 #define PT32_PT_BITS 10
74 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
76 #define PT_WRITABLE_SHIFT 1
78 #define PT_PRESENT_MASK (1ULL << 0)
79 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
80 #define PT_USER_MASK (1ULL << 2)
81 #define PT_PWT_MASK (1ULL << 3)
82 #define PT_PCD_MASK (1ULL << 4)
83 #define PT_ACCESSED_MASK (1ULL << 5)
84 #define PT_DIRTY_MASK (1ULL << 6)
85 #define PT_PAGE_SIZE_MASK (1ULL << 7)
86 #define PT_PAT_MASK (1ULL << 7)
87 #define PT_GLOBAL_MASK (1ULL << 8)
88 #define PT64_NX_SHIFT 63
89 #define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
91 #define PT_PAT_SHIFT 7
92 #define PT_DIR_PAT_SHIFT 12
93 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
95 #define PT32_DIR_PSE36_SIZE 4
96 #define PT32_DIR_PSE36_SHIFT 13
97 #define PT32_DIR_PSE36_MASK \
98 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
101 #define PT_FIRST_AVAIL_BITS_SHIFT 9
102 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
104 #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
106 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
108 #define PT64_LEVEL_BITS 9
110 #define PT64_LEVEL_SHIFT(level) \
111 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
113 #define PT64_LEVEL_MASK(level) \
114 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
116 #define PT64_INDEX(address, level)\
117 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
120 #define PT32_LEVEL_BITS 10
122 #define PT32_LEVEL_SHIFT(level) \
123 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
125 #define PT32_LEVEL_MASK(level) \
126 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
128 #define PT32_INDEX(address, level)\
129 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
132 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
133 #define PT64_DIR_BASE_ADDR_MASK \
134 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
136 #define PT32_BASE_ADDR_MASK PAGE_MASK
137 #define PT32_DIR_BASE_ADDR_MASK \
138 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
140 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
143 #define PFERR_PRESENT_MASK (1U << 0)
144 #define PFERR_WRITE_MASK (1U << 1)
145 #define PFERR_USER_MASK (1U << 2)
146 #define PFERR_FETCH_MASK (1U << 4)
148 #define PT64_ROOT_LEVEL 4
149 #define PT32_ROOT_LEVEL 2
150 #define PT32E_ROOT_LEVEL 3
152 #define PT_DIRECTORY_LEVEL 2
153 #define PT_PAGE_TABLE_LEVEL 1
157 #define ACC_EXEC_MASK 1
158 #define ACC_WRITE_MASK PT_WRITABLE_MASK
159 #define ACC_USER_MASK PT_USER_MASK
160 #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
162 struct kvm_rmap_desc
{
163 u64
*shadow_ptes
[RMAP_EXT
];
164 struct kvm_rmap_desc
*more
;
167 static struct kmem_cache
*pte_chain_cache
;
168 static struct kmem_cache
*rmap_desc_cache
;
169 static struct kmem_cache
*mmu_page_header_cache
;
171 static u64 __read_mostly shadow_trap_nonpresent_pte
;
172 static u64 __read_mostly shadow_notrap_nonpresent_pte
;
174 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte
, u64 notrap_pte
)
176 shadow_trap_nonpresent_pte
= trap_pte
;
177 shadow_notrap_nonpresent_pte
= notrap_pte
;
179 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes
);
181 static int is_write_protection(struct kvm_vcpu
*vcpu
)
183 return vcpu
->arch
.cr0
& X86_CR0_WP
;
186 static int is_cpuid_PSE36(void)
191 static int is_nx(struct kvm_vcpu
*vcpu
)
193 return vcpu
->arch
.shadow_efer
& EFER_NX
;
196 static int is_present_pte(unsigned long pte
)
198 return pte
& PT_PRESENT_MASK
;
201 static int is_shadow_present_pte(u64 pte
)
203 pte
&= ~PT_SHADOW_IO_MARK
;
204 return pte
!= shadow_trap_nonpresent_pte
205 && pte
!= shadow_notrap_nonpresent_pte
;
208 static int is_writeble_pte(unsigned long pte
)
210 return pte
& PT_WRITABLE_MASK
;
213 static int is_dirty_pte(unsigned long pte
)
215 return pte
& PT_DIRTY_MASK
;
218 static int is_io_pte(unsigned long pte
)
220 return pte
& PT_SHADOW_IO_MARK
;
223 static int is_rmap_pte(u64 pte
)
225 return pte
!= shadow_trap_nonpresent_pte
226 && pte
!= shadow_notrap_nonpresent_pte
;
229 static gfn_t
pse36_gfn_delta(u32 gpte
)
231 int shift
= 32 - PT32_DIR_PSE36_SHIFT
- PAGE_SHIFT
;
233 return (gpte
& PT32_DIR_PSE36_MASK
) << shift
;
236 static void set_shadow_pte(u64
*sptep
, u64 spte
)
239 set_64bit((unsigned long *)sptep
, spte
);
241 set_64bit((unsigned long long *)sptep
, spte
);
245 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
246 struct kmem_cache
*base_cache
, int min
)
250 if (cache
->nobjs
>= min
)
252 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
253 obj
= kmem_cache_zalloc(base_cache
, GFP_KERNEL
);
256 cache
->objects
[cache
->nobjs
++] = obj
;
261 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
264 kfree(mc
->objects
[--mc
->nobjs
]);
267 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache
*cache
,
272 if (cache
->nobjs
>= min
)
274 while (cache
->nobjs
< ARRAY_SIZE(cache
->objects
)) {
275 page
= alloc_page(GFP_KERNEL
);
278 set_page_private(page
, 0);
279 cache
->objects
[cache
->nobjs
++] = page_address(page
);
284 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache
*mc
)
287 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
290 static int mmu_topup_memory_caches(struct kvm_vcpu
*vcpu
)
294 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
,
298 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
,
302 r
= mmu_topup_memory_cache_page(&vcpu
->arch
.mmu_page_cache
, 8);
305 r
= mmu_topup_memory_cache(&vcpu
->arch
.mmu_page_header_cache
,
306 mmu_page_header_cache
, 4);
311 static void mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
313 mmu_free_memory_cache(&vcpu
->arch
.mmu_pte_chain_cache
);
314 mmu_free_memory_cache(&vcpu
->arch
.mmu_rmap_desc_cache
);
315 mmu_free_memory_cache_page(&vcpu
->arch
.mmu_page_cache
);
316 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_header_cache
);
319 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
,
325 p
= mc
->objects
[--mc
->nobjs
];
330 static struct kvm_pte_chain
*mmu_alloc_pte_chain(struct kvm_vcpu
*vcpu
)
332 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_pte_chain_cache
,
333 sizeof(struct kvm_pte_chain
));
336 static void mmu_free_pte_chain(struct kvm_pte_chain
*pc
)
341 static struct kvm_rmap_desc
*mmu_alloc_rmap_desc(struct kvm_vcpu
*vcpu
)
343 return mmu_memory_cache_alloc(&vcpu
->arch
.mmu_rmap_desc_cache
,
344 sizeof(struct kvm_rmap_desc
));
347 static void mmu_free_rmap_desc(struct kvm_rmap_desc
*rd
)
353 * Take gfn and return the reverse mapping to it.
354 * Note: gfn must be unaliased before this function get called
357 static unsigned long *gfn_to_rmap(struct kvm
*kvm
, gfn_t gfn
)
359 struct kvm_memory_slot
*slot
;
361 slot
= gfn_to_memslot(kvm
, gfn
);
362 return &slot
->rmap
[gfn
- slot
->base_gfn
];
366 * Reverse mapping data structures:
368 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
369 * that points to page_address(page).
371 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
372 * containing more mappings.
374 static void rmap_add(struct kvm_vcpu
*vcpu
, u64
*spte
, gfn_t gfn
)
376 struct kvm_mmu_page
*sp
;
377 struct kvm_rmap_desc
*desc
;
378 unsigned long *rmapp
;
381 if (!is_rmap_pte(*spte
))
383 gfn
= unalias_gfn(vcpu
->kvm
, gfn
);
384 sp
= page_header(__pa(spte
));
385 sp
->gfns
[spte
- sp
->spt
] = gfn
;
386 rmapp
= gfn_to_rmap(vcpu
->kvm
, gfn
);
388 rmap_printk("rmap_add: %p %llx 0->1\n", spte
, *spte
);
389 *rmapp
= (unsigned long)spte
;
390 } else if (!(*rmapp
& 1)) {
391 rmap_printk("rmap_add: %p %llx 1->many\n", spte
, *spte
);
392 desc
= mmu_alloc_rmap_desc(vcpu
);
393 desc
->shadow_ptes
[0] = (u64
*)*rmapp
;
394 desc
->shadow_ptes
[1] = spte
;
395 *rmapp
= (unsigned long)desc
| 1;
397 rmap_printk("rmap_add: %p %llx many->many\n", spte
, *spte
);
398 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
399 while (desc
->shadow_ptes
[RMAP_EXT
-1] && desc
->more
)
401 if (desc
->shadow_ptes
[RMAP_EXT
-1]) {
402 desc
->more
= mmu_alloc_rmap_desc(vcpu
);
405 for (i
= 0; desc
->shadow_ptes
[i
]; ++i
)
407 desc
->shadow_ptes
[i
] = spte
;
411 static void rmap_desc_remove_entry(unsigned long *rmapp
,
412 struct kvm_rmap_desc
*desc
,
414 struct kvm_rmap_desc
*prev_desc
)
418 for (j
= RMAP_EXT
- 1; !desc
->shadow_ptes
[j
] && j
> i
; --j
)
420 desc
->shadow_ptes
[i
] = desc
->shadow_ptes
[j
];
421 desc
->shadow_ptes
[j
] = NULL
;
424 if (!prev_desc
&& !desc
->more
)
425 *rmapp
= (unsigned long)desc
->shadow_ptes
[0];
428 prev_desc
->more
= desc
->more
;
430 *rmapp
= (unsigned long)desc
->more
| 1;
431 mmu_free_rmap_desc(desc
);
434 static void rmap_remove(struct kvm
*kvm
, u64
*spte
)
436 struct kvm_rmap_desc
*desc
;
437 struct kvm_rmap_desc
*prev_desc
;
438 struct kvm_mmu_page
*sp
;
440 unsigned long *rmapp
;
443 if (!is_rmap_pte(*spte
))
445 sp
= page_header(__pa(spte
));
446 page
= pfn_to_page((*spte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
);
447 mark_page_accessed(page
);
448 if (is_writeble_pte(*spte
))
449 kvm_release_page_dirty(page
);
451 kvm_release_page_clean(page
);
452 rmapp
= gfn_to_rmap(kvm
, sp
->gfns
[spte
- sp
->spt
]);
454 printk(KERN_ERR
"rmap_remove: %p %llx 0->BUG\n", spte
, *spte
);
456 } else if (!(*rmapp
& 1)) {
457 rmap_printk("rmap_remove: %p %llx 1->0\n", spte
, *spte
);
458 if ((u64
*)*rmapp
!= spte
) {
459 printk(KERN_ERR
"rmap_remove: %p %llx 1->BUG\n",
465 rmap_printk("rmap_remove: %p %llx many->many\n", spte
, *spte
);
466 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
469 for (i
= 0; i
< RMAP_EXT
&& desc
->shadow_ptes
[i
]; ++i
)
470 if (desc
->shadow_ptes
[i
] == spte
) {
471 rmap_desc_remove_entry(rmapp
,
483 static u64
*rmap_next(struct kvm
*kvm
, unsigned long *rmapp
, u64
*spte
)
485 struct kvm_rmap_desc
*desc
;
486 struct kvm_rmap_desc
*prev_desc
;
492 else if (!(*rmapp
& 1)) {
494 return (u64
*)*rmapp
;
497 desc
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
501 for (i
= 0; i
< RMAP_EXT
&& desc
->shadow_ptes
[i
]; ++i
) {
502 if (prev_spte
== spte
)
503 return desc
->shadow_ptes
[i
];
504 prev_spte
= desc
->shadow_ptes
[i
];
511 static void rmap_write_protect(struct kvm
*kvm
, u64 gfn
)
513 unsigned long *rmapp
;
515 int write_protected
= 0;
517 gfn
= unalias_gfn(kvm
, gfn
);
518 rmapp
= gfn_to_rmap(kvm
, gfn
);
520 spte
= rmap_next(kvm
, rmapp
, NULL
);
523 BUG_ON(!(*spte
& PT_PRESENT_MASK
));
524 rmap_printk("rmap_write_protect: spte %p %llx\n", spte
, *spte
);
525 if (is_writeble_pte(*spte
)) {
526 set_shadow_pte(spte
, *spte
& ~PT_WRITABLE_MASK
);
529 spte
= rmap_next(kvm
, rmapp
, spte
);
532 kvm_flush_remote_tlbs(kvm
);
536 static int is_empty_shadow_page(u64
*spt
)
541 for (pos
= spt
, end
= pos
+ PAGE_SIZE
/ sizeof(u64
); pos
!= end
; pos
++)
542 if ((*pos
& ~PT_SHADOW_IO_MARK
) != shadow_trap_nonpresent_pte
) {
543 printk(KERN_ERR
"%s: %p %llx\n", __FUNCTION__
,
551 static void kvm_mmu_free_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
553 ASSERT(is_empty_shadow_page(sp
->spt
));
555 __free_page(virt_to_page(sp
->spt
));
556 __free_page(virt_to_page(sp
->gfns
));
558 ++kvm
->arch
.n_free_mmu_pages
;
561 static unsigned kvm_page_table_hashfn(gfn_t gfn
)
566 static struct kvm_mmu_page
*kvm_mmu_alloc_page(struct kvm_vcpu
*vcpu
,
569 struct kvm_mmu_page
*sp
;
571 sp
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_header_cache
, sizeof *sp
);
572 sp
->spt
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
573 sp
->gfns
= mmu_memory_cache_alloc(&vcpu
->arch
.mmu_page_cache
, PAGE_SIZE
);
574 set_page_private(virt_to_page(sp
->spt
), (unsigned long)sp
);
575 list_add(&sp
->link
, &vcpu
->kvm
->arch
.active_mmu_pages
);
576 ASSERT(is_empty_shadow_page(sp
->spt
));
579 sp
->parent_pte
= parent_pte
;
580 --vcpu
->kvm
->arch
.n_free_mmu_pages
;
584 static void mmu_page_add_parent_pte(struct kvm_vcpu
*vcpu
,
585 struct kvm_mmu_page
*sp
, u64
*parent_pte
)
587 struct kvm_pte_chain
*pte_chain
;
588 struct hlist_node
*node
;
593 if (!sp
->multimapped
) {
594 u64
*old
= sp
->parent_pte
;
597 sp
->parent_pte
= parent_pte
;
601 pte_chain
= mmu_alloc_pte_chain(vcpu
);
602 INIT_HLIST_HEAD(&sp
->parent_ptes
);
603 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
604 pte_chain
->parent_ptes
[0] = old
;
606 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
) {
607 if (pte_chain
->parent_ptes
[NR_PTE_CHAIN_ENTRIES
-1])
609 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
)
610 if (!pte_chain
->parent_ptes
[i
]) {
611 pte_chain
->parent_ptes
[i
] = parent_pte
;
615 pte_chain
= mmu_alloc_pte_chain(vcpu
);
617 hlist_add_head(&pte_chain
->link
, &sp
->parent_ptes
);
618 pte_chain
->parent_ptes
[0] = parent_pte
;
621 static void mmu_page_remove_parent_pte(struct kvm_mmu_page
*sp
,
624 struct kvm_pte_chain
*pte_chain
;
625 struct hlist_node
*node
;
628 if (!sp
->multimapped
) {
629 BUG_ON(sp
->parent_pte
!= parent_pte
);
630 sp
->parent_pte
= NULL
;
633 hlist_for_each_entry(pte_chain
, node
, &sp
->parent_ptes
, link
)
634 for (i
= 0; i
< NR_PTE_CHAIN_ENTRIES
; ++i
) {
635 if (!pte_chain
->parent_ptes
[i
])
637 if (pte_chain
->parent_ptes
[i
] != parent_pte
)
639 while (i
+ 1 < NR_PTE_CHAIN_ENTRIES
640 && pte_chain
->parent_ptes
[i
+ 1]) {
641 pte_chain
->parent_ptes
[i
]
642 = pte_chain
->parent_ptes
[i
+ 1];
645 pte_chain
->parent_ptes
[i
] = NULL
;
647 hlist_del(&pte_chain
->link
);
648 mmu_free_pte_chain(pte_chain
);
649 if (hlist_empty(&sp
->parent_ptes
)) {
651 sp
->parent_pte
= NULL
;
659 static struct kvm_mmu_page
*kvm_mmu_lookup_page(struct kvm
*kvm
, gfn_t gfn
)
662 struct hlist_head
*bucket
;
663 struct kvm_mmu_page
*sp
;
664 struct hlist_node
*node
;
666 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__
, gfn
);
667 index
= kvm_page_table_hashfn(gfn
) % KVM_NUM_MMU_PAGES
;
668 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
669 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
670 if (sp
->gfn
== gfn
&& !sp
->role
.metaphysical
) {
671 pgprintk("%s: found role %x\n",
672 __FUNCTION__
, sp
->role
.word
);
678 static struct kvm_mmu_page
*kvm_mmu_get_page(struct kvm_vcpu
*vcpu
,
684 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
689 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
691 union kvm_mmu_page_role role
;
694 struct hlist_head
*bucket
;
695 struct kvm_mmu_page
*sp
;
696 struct hlist_node
*node
;
699 role
.glevels
= vcpu
->arch
.mmu
.root_level
;
701 role
.metaphysical
= metaphysical
;
702 role
.access
= access
;
703 if (vcpu
->arch
.mmu
.root_level
<= PT32_ROOT_LEVEL
) {
704 quadrant
= gaddr
>> (PAGE_SHIFT
+ (PT64_PT_BITS
* level
));
705 quadrant
&= (1 << ((PT32_PT_BITS
- PT64_PT_BITS
) * level
)) - 1;
706 role
.quadrant
= quadrant
;
708 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__
,
710 index
= kvm_page_table_hashfn(gfn
) % KVM_NUM_MMU_PAGES
;
711 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
712 hlist_for_each_entry(sp
, node
, bucket
, hash_link
)
713 if (sp
->gfn
== gfn
&& sp
->role
.word
== role
.word
) {
714 mmu_page_add_parent_pte(vcpu
, sp
, parent_pte
);
715 pgprintk("%s: found\n", __FUNCTION__
);
718 ++vcpu
->kvm
->stat
.mmu_cache_miss
;
719 sp
= kvm_mmu_alloc_page(vcpu
, parent_pte
);
722 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__
, gfn
, role
.word
);
725 hlist_add_head(&sp
->hash_link
, bucket
);
726 vcpu
->arch
.mmu
.prefetch_page(vcpu
, sp
);
728 rmap_write_protect(vcpu
->kvm
, gfn
);
729 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
733 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
737 static void kvm_mmu_page_unlink_children(struct kvm
*kvm
,
738 struct kvm_mmu_page
*sp
)
746 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
) {
747 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
748 if (is_shadow_present_pte(pt
[i
]))
749 rmap_remove(kvm
, &pt
[i
]);
750 pt
[i
] = shadow_trap_nonpresent_pte
;
752 kvm_flush_remote_tlbs(kvm
);
756 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
759 pt
[i
] = shadow_trap_nonpresent_pte
;
760 if (!is_shadow_present_pte(ent
))
762 ent
&= PT64_BASE_ADDR_MASK
;
763 mmu_page_remove_parent_pte(page_header(ent
), &pt
[i
]);
765 kvm_flush_remote_tlbs(kvm
);
768 static void kvm_mmu_put_page(struct kvm_mmu_page
*sp
, u64
*parent_pte
)
770 mmu_page_remove_parent_pte(sp
, parent_pte
);
773 static void kvm_mmu_reset_last_pte_updated(struct kvm
*kvm
)
777 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
779 kvm
->vcpus
[i
]->arch
.last_pte_updated
= NULL
;
782 static void kvm_mmu_zap_page(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
786 ++kvm
->stat
.mmu_shadow_zapped
;
787 while (sp
->multimapped
|| sp
->parent_pte
) {
788 if (!sp
->multimapped
)
789 parent_pte
= sp
->parent_pte
;
791 struct kvm_pte_chain
*chain
;
793 chain
= container_of(sp
->parent_ptes
.first
,
794 struct kvm_pte_chain
, link
);
795 parent_pte
= chain
->parent_ptes
[0];
798 kvm_mmu_put_page(sp
, parent_pte
);
799 set_shadow_pte(parent_pte
, shadow_trap_nonpresent_pte
);
801 kvm_mmu_page_unlink_children(kvm
, sp
);
802 if (!sp
->root_count
) {
803 hlist_del(&sp
->hash_link
);
804 kvm_mmu_free_page(kvm
, sp
);
806 list_move(&sp
->link
, &kvm
->arch
.active_mmu_pages
);
807 kvm_mmu_reset_last_pte_updated(kvm
);
811 * Changing the number of mmu pages allocated to the vm
812 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
814 void kvm_mmu_change_mmu_pages(struct kvm
*kvm
, unsigned int kvm_nr_mmu_pages
)
817 * If we set the number of mmu pages to be smaller be than the
818 * number of actived pages , we must to free some mmu pages before we
822 if ((kvm
->arch
.n_alloc_mmu_pages
- kvm
->arch
.n_free_mmu_pages
) >
824 int n_used_mmu_pages
= kvm
->arch
.n_alloc_mmu_pages
825 - kvm
->arch
.n_free_mmu_pages
;
827 while (n_used_mmu_pages
> kvm_nr_mmu_pages
) {
828 struct kvm_mmu_page
*page
;
830 page
= container_of(kvm
->arch
.active_mmu_pages
.prev
,
831 struct kvm_mmu_page
, link
);
832 kvm_mmu_zap_page(kvm
, page
);
835 kvm
->arch
.n_free_mmu_pages
= 0;
838 kvm
->arch
.n_free_mmu_pages
+= kvm_nr_mmu_pages
839 - kvm
->arch
.n_alloc_mmu_pages
;
841 kvm
->arch
.n_alloc_mmu_pages
= kvm_nr_mmu_pages
;
844 static int kvm_mmu_unprotect_page(struct kvm
*kvm
, gfn_t gfn
)
847 struct hlist_head
*bucket
;
848 struct kvm_mmu_page
*sp
;
849 struct hlist_node
*node
, *n
;
852 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__
, gfn
);
854 index
= kvm_page_table_hashfn(gfn
) % KVM_NUM_MMU_PAGES
;
855 bucket
= &kvm
->arch
.mmu_page_hash
[index
];
856 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
)
857 if (sp
->gfn
== gfn
&& !sp
->role
.metaphysical
) {
858 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__
, gfn
,
860 kvm_mmu_zap_page(kvm
, sp
);
866 static void mmu_unshadow(struct kvm
*kvm
, gfn_t gfn
)
868 struct kvm_mmu_page
*sp
;
870 while ((sp
= kvm_mmu_lookup_page(kvm
, gfn
)) != NULL
) {
871 pgprintk("%s: zap %lx %x\n", __FUNCTION__
, gfn
, sp
->role
.word
);
872 kvm_mmu_zap_page(kvm
, sp
);
876 static void page_header_update_slot(struct kvm
*kvm
, void *pte
, gfn_t gfn
)
878 int slot
= memslot_id(kvm
, gfn_to_memslot(kvm
, gfn
));
879 struct kvm_mmu_page
*sp
= page_header(__pa(pte
));
881 __set_bit(slot
, &sp
->slot_bitmap
);
884 struct page
*gva_to_page(struct kvm_vcpu
*vcpu
, gva_t gva
)
886 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
890 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
891 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
);
893 if (gpa
== UNMAPPED_GVA
)
895 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
896 return gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
899 down_read(¤t
->mm
->mmap_sem
);
900 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
901 up_read(¤t
->mm
->mmap_sem
);
904 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
907 static void mmu_set_spte(struct kvm_vcpu
*vcpu
, u64
*shadow_pte
,
908 unsigned pt_access
, unsigned pte_access
,
909 int user_fault
, int write_fault
, int dirty
,
910 int *ptwrite
, gfn_t gfn
, struct page
*page
)
913 int was_rmapped
= is_rmap_pte(*shadow_pte
);
914 int was_writeble
= is_writeble_pte(*shadow_pte
);
916 pgprintk("%s: spte %llx access %x write_fault %d"
917 " user_fault %d gfn %lx\n",
918 __FUNCTION__
, *shadow_pte
, pt_access
,
919 write_fault
, user_fault
, gfn
);
922 * We don't set the accessed bit, since we sometimes want to see
923 * whether the guest actually used the pte (in order to detect
926 spte
= PT_PRESENT_MASK
| PT_DIRTY_MASK
;
928 pte_access
&= ~ACC_WRITE_MASK
;
929 if (!(pte_access
& ACC_EXEC_MASK
))
930 spte
|= PT64_NX_MASK
;
932 spte
|= PT_PRESENT_MASK
;
933 if (pte_access
& ACC_USER_MASK
)
934 spte
|= PT_USER_MASK
;
936 if (is_error_page(page
)) {
937 set_shadow_pte(shadow_pte
,
938 shadow_trap_nonpresent_pte
| PT_SHADOW_IO_MARK
);
939 kvm_release_page_clean(page
);
943 spte
|= page_to_phys(page
);
945 if ((pte_access
& ACC_WRITE_MASK
)
946 || (write_fault
&& !is_write_protection(vcpu
) && !user_fault
)) {
947 struct kvm_mmu_page
*shadow
;
949 spte
|= PT_WRITABLE_MASK
;
951 mmu_unshadow(vcpu
->kvm
, gfn
);
955 shadow
= kvm_mmu_lookup_page(vcpu
->kvm
, gfn
);
957 pgprintk("%s: found shadow page for %lx, marking ro\n",
959 pte_access
&= ~ACC_WRITE_MASK
;
960 if (is_writeble_pte(spte
)) {
961 spte
&= ~PT_WRITABLE_MASK
;
962 kvm_x86_ops
->tlb_flush(vcpu
);
971 if (pte_access
& ACC_WRITE_MASK
)
972 mark_page_dirty(vcpu
->kvm
, gfn
);
974 pgprintk("%s: setting spte %llx\n", __FUNCTION__
, spte
);
975 set_shadow_pte(shadow_pte
, spte
);
976 page_header_update_slot(vcpu
->kvm
, shadow_pte
, gfn
);
978 rmap_add(vcpu
, shadow_pte
, gfn
);
979 if (!is_rmap_pte(*shadow_pte
))
980 kvm_release_page_clean(page
);
983 kvm_release_page_dirty(page
);
985 kvm_release_page_clean(page
);
987 if (!ptwrite
|| !*ptwrite
)
988 vcpu
->arch
.last_pte_updated
= shadow_pte
;
991 static void nonpaging_new_cr3(struct kvm_vcpu
*vcpu
)
995 static int __nonpaging_map(struct kvm_vcpu
*vcpu
, gva_t v
, int write
,
996 gfn_t gfn
, struct page
*page
)
998 int level
= PT32E_ROOT_LEVEL
;
999 hpa_t table_addr
= vcpu
->arch
.mmu
.root_hpa
;
1003 u32 index
= PT64_INDEX(v
, level
);
1006 ASSERT(VALID_PAGE(table_addr
));
1007 table
= __va(table_addr
);
1010 mmu_set_spte(vcpu
, &table
[index
], ACC_ALL
, ACC_ALL
,
1011 0, write
, 1, &pt_write
, gfn
, page
);
1012 return pt_write
|| is_io_pte(table
[index
]);
1015 if (table
[index
] == shadow_trap_nonpresent_pte
) {
1016 struct kvm_mmu_page
*new_table
;
1019 pseudo_gfn
= (v
& PT64_DIR_BASE_ADDR_MASK
)
1021 new_table
= kvm_mmu_get_page(vcpu
, pseudo_gfn
,
1023 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1024 1, ACC_ALL
, &table
[index
],
1027 1, ACC_ALL
, &table
[index
]);
1028 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1030 pgprintk("nonpaging_map: ENOMEM\n");
1031 kvm_release_page_clean(page
);
1035 table
[index
] = __pa(new_table
->spt
) | PT_PRESENT_MASK
1036 | PT_WRITABLE_MASK
| PT_USER_MASK
;
1038 table_addr
= table
[index
] & PT64_BASE_ADDR_MASK
;
1042 static int nonpaging_map(struct kvm_vcpu
*vcpu
, gva_t v
, int write
, gfn_t gfn
)
1048 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1050 down_read(&vcpu
->kvm
->slots_lock
);
1052 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1053 down_read(¤t
->mm
->mmap_sem
);
1054 page
= gfn_to_page(vcpu
->kvm
, gfn
);
1055 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1057 up_read(¤t
->mm
->mmap_sem
);
1058 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1060 spin_lock(&vcpu
->kvm
->mmu_lock
);
1061 kvm_mmu_free_some_pages(vcpu
);
1062 r
= __nonpaging_map(vcpu
, v
, write
, gfn
, page
);
1063 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1065 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1066 up_read(¤t
->mm
->mmap_sem
);
1068 up_read(&vcpu
->kvm
->slots_lock
);
1069 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1075 static void nonpaging_prefetch_page(struct kvm_vcpu
*vcpu
,
1076 struct kvm_mmu_page
*sp
)
1080 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
1081 sp
->spt
[i
] = shadow_trap_nonpresent_pte
;
1084 static void mmu_free_roots(struct kvm_vcpu
*vcpu
)
1087 struct kvm_mmu_page
*sp
;
1089 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
1091 spin_lock(&vcpu
->kvm
->mmu_lock
);
1092 #ifdef CONFIG_X86_64
1093 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1094 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1096 sp
= page_header(root
);
1098 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1099 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1103 for (i
= 0; i
< 4; ++i
) {
1104 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1107 root
&= PT64_BASE_ADDR_MASK
;
1108 sp
= page_header(root
);
1111 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
1113 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1114 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1117 static void mmu_alloc_roots(struct kvm_vcpu
*vcpu
)
1121 struct kvm_mmu_page
*sp
;
1123 root_gfn
= vcpu
->arch
.cr3
>> PAGE_SHIFT
;
1125 #ifdef CONFIG_X86_64
1126 if (vcpu
->arch
.mmu
.shadow_root_level
== PT64_ROOT_LEVEL
) {
1127 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
1129 ASSERT(!VALID_PAGE(root
));
1130 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, 0,
1131 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1132 PT64_ROOT_LEVEL
, 0, ACC_ALL
, NULL
, NULL
);
1134 PT64_ROOT_LEVEL
, 0, ACC_ALL
, NULL
);
1135 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1136 root
= __pa(sp
->spt
);
1138 vcpu
->arch
.mmu
.root_hpa
= root
;
1142 for (i
= 0; i
< 4; ++i
) {
1143 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
1145 ASSERT(!VALID_PAGE(root
));
1146 if (vcpu
->arch
.mmu
.root_level
== PT32E_ROOT_LEVEL
) {
1147 if (!is_present_pte(vcpu
->arch
.pdptrs
[i
])) {
1148 vcpu
->arch
.mmu
.pae_root
[i
] = 0;
1151 root_gfn
= vcpu
->arch
.pdptrs
[i
] >> PAGE_SHIFT
;
1152 } else if (vcpu
->arch
.mmu
.root_level
== 0)
1154 sp
= kvm_mmu_get_page(vcpu
, root_gfn
, i
<< 30,
1155 PT32_ROOT_LEVEL
, !is_paging(vcpu
),
1156 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1157 ACC_ALL
, NULL
, NULL
);
1160 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1161 root
= __pa(sp
->spt
);
1163 vcpu
->arch
.mmu
.pae_root
[i
] = root
| PT_PRESENT_MASK
;
1165 vcpu
->arch
.mmu
.root_hpa
= __pa(vcpu
->arch
.mmu
.pae_root
);
1168 static gpa_t
nonpaging_gva_to_gpa(struct kvm_vcpu
*vcpu
, gva_t vaddr
)
1173 static int nonpaging_page_fault(struct kvm_vcpu
*vcpu
, gva_t gva
,
1179 pgprintk("%s: gva %lx error %x\n", __FUNCTION__
, gva
, error_code
);
1180 r
= mmu_topup_memory_caches(vcpu
);
1185 ASSERT(VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1187 gfn
= gva
>> PAGE_SHIFT
;
1189 return nonpaging_map(vcpu
, gva
& PAGE_MASK
,
1190 error_code
& PFERR_WRITE_MASK
, gfn
);
1193 static void nonpaging_free(struct kvm_vcpu
*vcpu
)
1195 mmu_free_roots(vcpu
);
1198 static int nonpaging_init_context(struct kvm_vcpu
*vcpu
)
1200 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1202 context
->new_cr3
= nonpaging_new_cr3
;
1203 context
->page_fault
= nonpaging_page_fault
;
1204 context
->gva_to_gpa
= nonpaging_gva_to_gpa
;
1205 context
->free
= nonpaging_free
;
1206 context
->prefetch_page
= nonpaging_prefetch_page
;
1207 context
->root_level
= 0;
1208 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
1209 context
->root_hpa
= INVALID_PAGE
;
1213 void kvm_mmu_flush_tlb(struct kvm_vcpu
*vcpu
)
1215 ++vcpu
->stat
.tlb_flush
;
1216 kvm_x86_ops
->tlb_flush(vcpu
);
1219 static void paging_new_cr3(struct kvm_vcpu
*vcpu
)
1221 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1222 pgprintk("%s: cr3 %lx\n", __FUNCTION__
, vcpu
->cr3
);
1224 pgprintk("%s: cr3 %lx\n", __FUNCTION__
, vcpu
->arch
.cr3
);
1225 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1226 mmu_free_roots(vcpu
);
1229 static void inject_page_fault(struct kvm_vcpu
*vcpu
,
1233 kvm_inject_page_fault(vcpu
, addr
, err_code
);
1236 static void paging_free(struct kvm_vcpu
*vcpu
)
1238 nonpaging_free(vcpu
);
1242 #include "paging_tmpl.h"
1246 #include "paging_tmpl.h"
1249 static int paging64_init_context_common(struct kvm_vcpu
*vcpu
, int level
)
1251 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1253 ASSERT(is_pae(vcpu
));
1254 context
->new_cr3
= paging_new_cr3
;
1255 context
->page_fault
= paging64_page_fault
;
1256 context
->gva_to_gpa
= paging64_gva_to_gpa
;
1257 context
->prefetch_page
= paging64_prefetch_page
;
1258 context
->free
= paging_free
;
1259 context
->root_level
= level
;
1260 context
->shadow_root_level
= level
;
1261 context
->root_hpa
= INVALID_PAGE
;
1265 static int paging64_init_context(struct kvm_vcpu
*vcpu
)
1267 return paging64_init_context_common(vcpu
, PT64_ROOT_LEVEL
);
1270 static int paging32_init_context(struct kvm_vcpu
*vcpu
)
1272 struct kvm_mmu
*context
= &vcpu
->arch
.mmu
;
1274 context
->new_cr3
= paging_new_cr3
;
1275 context
->page_fault
= paging32_page_fault
;
1276 context
->gva_to_gpa
= paging32_gva_to_gpa
;
1277 context
->free
= paging_free
;
1278 context
->prefetch_page
= paging32_prefetch_page
;
1279 context
->root_level
= PT32_ROOT_LEVEL
;
1280 context
->shadow_root_level
= PT32E_ROOT_LEVEL
;
1281 context
->root_hpa
= INVALID_PAGE
;
1285 static int paging32E_init_context(struct kvm_vcpu
*vcpu
)
1287 return paging64_init_context_common(vcpu
, PT32E_ROOT_LEVEL
);
1290 static int init_kvm_mmu(struct kvm_vcpu
*vcpu
)
1293 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1295 if (!is_paging(vcpu
))
1296 return nonpaging_init_context(vcpu
);
1297 else if (is_long_mode(vcpu
))
1298 return paging64_init_context(vcpu
);
1299 else if (is_pae(vcpu
))
1300 return paging32E_init_context(vcpu
);
1302 return paging32_init_context(vcpu
);
1305 static void destroy_kvm_mmu(struct kvm_vcpu
*vcpu
)
1308 if (VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
)) {
1309 vcpu
->arch
.mmu
.free(vcpu
);
1310 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
1314 int kvm_mmu_reset_context(struct kvm_vcpu
*vcpu
)
1316 destroy_kvm_mmu(vcpu
);
1317 return init_kvm_mmu(vcpu
);
1319 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context
);
1321 int kvm_mmu_load(struct kvm_vcpu
*vcpu
)
1325 r
= mmu_topup_memory_caches(vcpu
);
1328 spin_lock(&vcpu
->kvm
->mmu_lock
);
1329 kvm_mmu_free_some_pages(vcpu
);
1330 mmu_alloc_roots(vcpu
);
1331 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1332 kvm_x86_ops
->set_cr3(vcpu
, vcpu
->arch
.mmu
.root_hpa
);
1333 kvm_mmu_flush_tlb(vcpu
);
1337 EXPORT_SYMBOL_GPL(kvm_mmu_load
);
1339 void kvm_mmu_unload(struct kvm_vcpu
*vcpu
)
1341 mmu_free_roots(vcpu
);
1344 static void mmu_pte_write_zap_pte(struct kvm_vcpu
*vcpu
,
1345 struct kvm_mmu_page
*sp
,
1349 struct kvm_mmu_page
*child
;
1352 if (is_shadow_present_pte(pte
)) {
1353 if (sp
->role
.level
== PT_PAGE_TABLE_LEVEL
)
1354 rmap_remove(vcpu
->kvm
, spte
);
1356 child
= page_header(pte
& PT64_BASE_ADDR_MASK
);
1357 mmu_page_remove_parent_pte(child
, spte
);
1360 set_shadow_pte(spte
, shadow_trap_nonpresent_pte
);
1363 static void mmu_pte_write_new_pte(struct kvm_vcpu
*vcpu
,
1364 struct kvm_mmu_page
*sp
,
1366 const void *new, int bytes
,
1369 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
) {
1370 ++vcpu
->kvm
->stat
.mmu_pde_zapped
;
1374 ++vcpu
->kvm
->stat
.mmu_pte_updated
;
1375 if (sp
->role
.glevels
== PT32_ROOT_LEVEL
)
1376 paging32_update_pte(vcpu
, sp
, spte
, new, bytes
, offset_in_pte
);
1378 paging64_update_pte(vcpu
, sp
, spte
, new, bytes
, offset_in_pte
);
1381 static bool need_remote_flush(u64 old
, u64
new)
1383 if (!is_shadow_present_pte(old
))
1385 if (!is_shadow_present_pte(new))
1387 if ((old
^ new) & PT64_BASE_ADDR_MASK
)
1389 old
^= PT64_NX_MASK
;
1390 new ^= PT64_NX_MASK
;
1391 return (old
& ~new & PT64_PERM_MASK
) != 0;
1394 static void mmu_pte_write_flush_tlb(struct kvm_vcpu
*vcpu
, u64 old
, u64
new)
1396 if (need_remote_flush(old
, new))
1397 kvm_flush_remote_tlbs(vcpu
->kvm
);
1399 kvm_mmu_flush_tlb(vcpu
);
1402 static bool last_updated_pte_accessed(struct kvm_vcpu
*vcpu
)
1404 u64
*spte
= vcpu
->arch
.last_pte_updated
;
1406 return !!(spte
&& (*spte
& PT_ACCESSED_MASK
));
1409 static void mmu_guess_page_from_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1410 const u8
*new, int bytes
)
1415 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1418 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1420 if (bytes
!= 4 && bytes
!= 8)
1424 * Assume that the pte write on a page table of the same type
1425 * as the current vcpu paging mode. This is nearly always true
1426 * (might be false while changing modes). Note it is verified later
1430 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1431 if ((bytes
== 4) && (gpa
% 4 == 0)) {
1432 r
= kvm_read_guest(vcpu
->kvm
, gpa
& ~(u64
)7, &gpte
, 8);
1435 memcpy((void *)&gpte
+ (gpa
% 8), new, 4);
1436 } else if ((bytes
== 8) && (gpa
% 8 == 0)) {
1437 memcpy((void *)&gpte
, new, 8);
1440 if ((bytes
== 4) && (gpa
% 4 == 0))
1441 memcpy((void *)&gpte
, new, 4);
1443 if (!is_present_pte(gpte
))
1445 gfn
= (gpte
& PT64_BASE_ADDR_MASK
) >> PAGE_SHIFT
;
1446 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1449 down_read(¤t
->mm
->mmap_sem
);
1450 page
= gfn_to_page(vcpu
->kvm
, gfn
);
1451 up_read(¤t
->mm
->mmap_sem
);
1453 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1454 vcpu
->arch
.update_pte
.gfn
= gfn
;
1455 vcpu
->arch
.update_pte
.page
= gfn_to_page(vcpu
->kvm
, gfn
);
1458 void kvm_mmu_pte_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1459 const u8
*new, int bytes
)
1461 gfn_t gfn
= gpa
>> PAGE_SHIFT
;
1462 struct kvm_mmu_page
*sp
;
1463 struct hlist_node
*node
, *n
;
1464 struct hlist_head
*bucket
;
1468 unsigned offset
= offset_in_page(gpa
);
1470 unsigned page_offset
;
1471 unsigned misaligned
;
1477 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__
, gpa
, bytes
);
1478 mmu_guess_page_from_pte_write(vcpu
, gpa
, new, bytes
);
1479 spin_lock(&vcpu
->kvm
->mmu_lock
);
1480 kvm_mmu_free_some_pages(vcpu
);
1481 ++vcpu
->kvm
->stat
.mmu_pte_write
;
1482 kvm_mmu_audit(vcpu
, "pre pte write");
1483 if (gfn
== vcpu
->arch
.last_pt_write_gfn
1484 && !last_updated_pte_accessed(vcpu
)) {
1485 ++vcpu
->arch
.last_pt_write_count
;
1486 if (vcpu
->arch
.last_pt_write_count
>= 3)
1489 vcpu
->arch
.last_pt_write_gfn
= gfn
;
1490 vcpu
->arch
.last_pt_write_count
= 1;
1491 vcpu
->arch
.last_pte_updated
= NULL
;
1493 index
= kvm_page_table_hashfn(gfn
) % KVM_NUM_MMU_PAGES
;
1494 bucket
= &vcpu
->kvm
->arch
.mmu_page_hash
[index
];
1495 hlist_for_each_entry_safe(sp
, node
, n
, bucket
, hash_link
) {
1496 if (sp
->gfn
!= gfn
|| sp
->role
.metaphysical
)
1498 pte_size
= sp
->role
.glevels
== PT32_ROOT_LEVEL
? 4 : 8;
1499 misaligned
= (offset
^ (offset
+ bytes
- 1)) & ~(pte_size
- 1);
1500 misaligned
|= bytes
< 4;
1501 if (misaligned
|| flooded
) {
1503 * Misaligned accesses are too much trouble to fix
1504 * up; also, they usually indicate a page is not used
1507 * If we're seeing too many writes to a page,
1508 * it may no longer be a page table, or we may be
1509 * forking, in which case it is better to unmap the
1512 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1513 gpa
, bytes
, sp
->role
.word
);
1514 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1515 ++vcpu
->kvm
->stat
.mmu_flooded
;
1518 page_offset
= offset
;
1519 level
= sp
->role
.level
;
1521 if (sp
->role
.glevels
== PT32_ROOT_LEVEL
) {
1522 page_offset
<<= 1; /* 32->64 */
1524 * A 32-bit pde maps 4MB while the shadow pdes map
1525 * only 2MB. So we need to double the offset again
1526 * and zap two pdes instead of one.
1528 if (level
== PT32_ROOT_LEVEL
) {
1529 page_offset
&= ~7; /* kill rounding error */
1533 quadrant
= page_offset
>> PAGE_SHIFT
;
1534 page_offset
&= ~PAGE_MASK
;
1535 if (quadrant
!= sp
->role
.quadrant
)
1538 spte
= &sp
->spt
[page_offset
/ sizeof(*spte
)];
1541 mmu_pte_write_zap_pte(vcpu
, sp
, spte
);
1542 mmu_pte_write_new_pte(vcpu
, sp
, spte
, new, bytes
,
1543 page_offset
& (pte_size
- 1));
1544 mmu_pte_write_flush_tlb(vcpu
, entry
, *spte
);
1548 kvm_mmu_audit(vcpu
, "post pte write");
1549 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1550 if (vcpu
->arch
.update_pte
.page
) {
1551 kvm_release_page_clean(vcpu
->arch
.update_pte
.page
);
1552 vcpu
->arch
.update_pte
.page
= NULL
;
1556 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu
*vcpu
, gva_t gva
)
1561 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1562 down_read(¤t
->mm
->mmap_sem
);
1564 down_read(&vcpu
->kvm
->slots_lock
);
1565 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1566 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, gva
);
1567 <<<<<<< HEAD
:arch
/x86
/kvm
/mmu
.c
1568 up_read(¤t
->mm
->mmap_sem
);
1570 up_read(&vcpu
->kvm
->slots_lock
);
1571 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/mmu
.c
1573 spin_lock(&vcpu
->kvm
->mmu_lock
);
1574 r
= kvm_mmu_unprotect_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1575 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1579 void __kvm_mmu_free_some_pages(struct kvm_vcpu
*vcpu
)
1581 while (vcpu
->kvm
->arch
.n_free_mmu_pages
< KVM_REFILL_PAGES
) {
1582 struct kvm_mmu_page
*sp
;
1584 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.prev
,
1585 struct kvm_mmu_page
, link
);
1586 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1587 ++vcpu
->kvm
->stat
.mmu_recycled
;
1591 int kvm_mmu_page_fault(struct kvm_vcpu
*vcpu
, gva_t cr2
, u32 error_code
)
1594 enum emulation_result er
;
1596 r
= vcpu
->arch
.mmu
.page_fault(vcpu
, cr2
, error_code
);
1605 r
= mmu_topup_memory_caches(vcpu
);
1609 er
= emulate_instruction(vcpu
, vcpu
->run
, cr2
, error_code
, 0);
1614 case EMULATE_DO_MMIO
:
1615 ++vcpu
->stat
.mmio_exits
;
1618 kvm_report_emulation_failure(vcpu
, "pagetable");
1626 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault
);
1628 static void free_mmu_pages(struct kvm_vcpu
*vcpu
)
1630 struct kvm_mmu_page
*sp
;
1632 while (!list_empty(&vcpu
->kvm
->arch
.active_mmu_pages
)) {
1633 sp
= container_of(vcpu
->kvm
->arch
.active_mmu_pages
.next
,
1634 struct kvm_mmu_page
, link
);
1635 kvm_mmu_zap_page(vcpu
->kvm
, sp
);
1637 free_page((unsigned long)vcpu
->arch
.mmu
.pae_root
);
1640 static int alloc_mmu_pages(struct kvm_vcpu
*vcpu
)
1647 if (vcpu
->kvm
->arch
.n_requested_mmu_pages
)
1648 vcpu
->kvm
->arch
.n_free_mmu_pages
=
1649 vcpu
->kvm
->arch
.n_requested_mmu_pages
;
1651 vcpu
->kvm
->arch
.n_free_mmu_pages
=
1652 vcpu
->kvm
->arch
.n_alloc_mmu_pages
;
1654 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1655 * Therefore we need to allocate shadow page tables in the first
1656 * 4GB of memory, which happens to fit the DMA32 zone.
1658 page
= alloc_page(GFP_KERNEL
| __GFP_DMA32
);
1661 vcpu
->arch
.mmu
.pae_root
= page_address(page
);
1662 for (i
= 0; i
< 4; ++i
)
1663 vcpu
->arch
.mmu
.pae_root
[i
] = INVALID_PAGE
;
1668 free_mmu_pages(vcpu
);
1672 int kvm_mmu_create(struct kvm_vcpu
*vcpu
)
1675 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1677 return alloc_mmu_pages(vcpu
);
1680 int kvm_mmu_setup(struct kvm_vcpu
*vcpu
)
1683 ASSERT(!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
));
1685 return init_kvm_mmu(vcpu
);
1688 void kvm_mmu_destroy(struct kvm_vcpu
*vcpu
)
1692 destroy_kvm_mmu(vcpu
);
1693 free_mmu_pages(vcpu
);
1694 mmu_free_memory_caches(vcpu
);
1697 void kvm_mmu_slot_remove_write_access(struct kvm
*kvm
, int slot
)
1699 struct kvm_mmu_page
*sp
;
1701 list_for_each_entry(sp
, &kvm
->arch
.active_mmu_pages
, link
) {
1705 if (!test_bit(slot
, &sp
->slot_bitmap
))
1709 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
)
1711 if (pt
[i
] & PT_WRITABLE_MASK
)
1712 pt
[i
] &= ~PT_WRITABLE_MASK
;
1716 void kvm_mmu_zap_all(struct kvm
*kvm
)
1718 struct kvm_mmu_page
*sp
, *node
;
1720 spin_lock(&kvm
->mmu_lock
);
1721 list_for_each_entry_safe(sp
, node
, &kvm
->arch
.active_mmu_pages
, link
)
1722 kvm_mmu_zap_page(kvm
, sp
);
1723 spin_unlock(&kvm
->mmu_lock
);
1725 kvm_flush_remote_tlbs(kvm
);
1728 void kvm_mmu_module_exit(void)
1730 if (pte_chain_cache
)
1731 kmem_cache_destroy(pte_chain_cache
);
1732 if (rmap_desc_cache
)
1733 kmem_cache_destroy(rmap_desc_cache
);
1734 if (mmu_page_header_cache
)
1735 kmem_cache_destroy(mmu_page_header_cache
);
1738 int kvm_mmu_module_init(void)
1740 pte_chain_cache
= kmem_cache_create("kvm_pte_chain",
1741 sizeof(struct kvm_pte_chain
),
1743 if (!pte_chain_cache
)
1745 rmap_desc_cache
= kmem_cache_create("kvm_rmap_desc",
1746 sizeof(struct kvm_rmap_desc
),
1748 if (!rmap_desc_cache
)
1751 mmu_page_header_cache
= kmem_cache_create("kvm_mmu_page_header",
1752 sizeof(struct kvm_mmu_page
),
1754 if (!mmu_page_header_cache
)
1760 kvm_mmu_module_exit();
1765 * Caculate mmu pages needed for kvm.
1767 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm
*kvm
)
1770 unsigned int nr_mmu_pages
;
1771 unsigned int nr_pages
= 0;
1773 for (i
= 0; i
< kvm
->nmemslots
; i
++)
1774 nr_pages
+= kvm
->memslots
[i
].npages
;
1776 nr_mmu_pages
= nr_pages
* KVM_PERMILLE_MMU_PAGES
/ 1000;
1777 nr_mmu_pages
= max(nr_mmu_pages
,
1778 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES
);
1780 return nr_mmu_pages
;
1785 static const char *audit_msg
;
1787 static gva_t
canonicalize(gva_t gva
)
1789 #ifdef CONFIG_X86_64
1790 gva
= (long long)(gva
<< 16) >> 16;
1795 static void audit_mappings_page(struct kvm_vcpu
*vcpu
, u64 page_pte
,
1796 gva_t va
, int level
)
1798 u64
*pt
= __va(page_pte
& PT64_BASE_ADDR_MASK
);
1800 gva_t va_delta
= 1ul << (PAGE_SHIFT
+ 9 * (level
- 1));
1802 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
, va
+= va_delta
) {
1805 if (ent
== shadow_trap_nonpresent_pte
)
1808 va
= canonicalize(va
);
1810 if (ent
== shadow_notrap_nonpresent_pte
)
1811 printk(KERN_ERR
"audit: (%s) nontrapping pte"
1812 " in nonleaf level: levels %d gva %lx"
1813 " level %d pte %llx\n", audit_msg
,
1814 vcpu
->arch
.mmu
.root_level
, va
, level
, ent
);
1816 audit_mappings_page(vcpu
, ent
, va
, level
- 1);
1818 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, va
);
1819 struct page
*page
= gpa_to_page(vcpu
, gpa
);
1820 hpa_t hpa
= page_to_phys(page
);
1822 if (is_shadow_present_pte(ent
)
1823 && (ent
& PT64_BASE_ADDR_MASK
) != hpa
)
1824 printk(KERN_ERR
"xx audit error: (%s) levels %d"
1825 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
1826 audit_msg
, vcpu
->arch
.mmu
.root_level
,
1828 is_shadow_present_pte(ent
));
1829 else if (ent
== shadow_notrap_nonpresent_pte
1830 && !is_error_hpa(hpa
))
1831 printk(KERN_ERR
"audit: (%s) notrap shadow,"
1832 " valid guest gva %lx\n", audit_msg
, va
);
1833 kvm_release_page_clean(page
);
1839 static void audit_mappings(struct kvm_vcpu
*vcpu
)
1843 if (vcpu
->arch
.mmu
.root_level
== 4)
1844 audit_mappings_page(vcpu
, vcpu
->arch
.mmu
.root_hpa
, 0, 4);
1846 for (i
= 0; i
< 4; ++i
)
1847 if (vcpu
->arch
.mmu
.pae_root
[i
] & PT_PRESENT_MASK
)
1848 audit_mappings_page(vcpu
,
1849 vcpu
->arch
.mmu
.pae_root
[i
],
1854 static int count_rmaps(struct kvm_vcpu
*vcpu
)
1859 for (i
= 0; i
< KVM_MEMORY_SLOTS
; ++i
) {
1860 struct kvm_memory_slot
*m
= &vcpu
->kvm
->memslots
[i
];
1861 struct kvm_rmap_desc
*d
;
1863 for (j
= 0; j
< m
->npages
; ++j
) {
1864 unsigned long *rmapp
= &m
->rmap
[j
];
1868 if (!(*rmapp
& 1)) {
1872 d
= (struct kvm_rmap_desc
*)(*rmapp
& ~1ul);
1874 for (k
= 0; k
< RMAP_EXT
; ++k
)
1875 if (d
->shadow_ptes
[k
])
1886 static int count_writable_mappings(struct kvm_vcpu
*vcpu
)
1889 struct kvm_mmu_page
*sp
;
1892 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
1895 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
1898 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
1901 if (!(ent
& PT_PRESENT_MASK
))
1903 if (!(ent
& PT_WRITABLE_MASK
))
1911 static void audit_rmap(struct kvm_vcpu
*vcpu
)
1913 int n_rmap
= count_rmaps(vcpu
);
1914 int n_actual
= count_writable_mappings(vcpu
);
1916 if (n_rmap
!= n_actual
)
1917 printk(KERN_ERR
"%s: (%s) rmap %d actual %d\n",
1918 __FUNCTION__
, audit_msg
, n_rmap
, n_actual
);
1921 static void audit_write_protection(struct kvm_vcpu
*vcpu
)
1923 struct kvm_mmu_page
*sp
;
1924 struct kvm_memory_slot
*slot
;
1925 unsigned long *rmapp
;
1928 list_for_each_entry(sp
, &vcpu
->kvm
->arch
.active_mmu_pages
, link
) {
1929 if (sp
->role
.metaphysical
)
1932 slot
= gfn_to_memslot(vcpu
->kvm
, sp
->gfn
);
1933 gfn
= unalias_gfn(vcpu
->kvm
, sp
->gfn
);
1934 rmapp
= &slot
->rmap
[gfn
- slot
->base_gfn
];
1936 printk(KERN_ERR
"%s: (%s) shadow page has writable"
1937 " mappings: gfn %lx role %x\n",
1938 __FUNCTION__
, audit_msg
, sp
->gfn
,
1943 static void kvm_mmu_audit(struct kvm_vcpu
*vcpu
, const char *msg
)
1950 audit_write_protection(vcpu
);
1951 audit_mappings(vcpu
);