Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / x86 / kvm / mmu.c
blob1138a537e5c4b9c9b559fcf53b0f83b4750f59ca
1 /*
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.
7 * MMU support
9 * Copyright (C) 2006 Qumranet, Inc.
11 * Authors:
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.
20 #include "vmx.h"
21 #include "mmu.h"
23 #include <linux/kvm_host.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/highmem.h>
28 #include <linux/module.h>
29 #include <linux/swap.h>
31 #include <asm/page.h>
32 #include <asm/cmpxchg.h>
33 #include <asm/io.h>
35 #undef MMU_DEBUG
37 #undef AUDIT
39 #ifdef AUDIT
40 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
41 #else
42 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
43 #endif
45 #ifdef MMU_DEBUG
47 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
48 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
50 #else
52 #define pgprintk(x...) do { } while (0)
53 #define rmap_printk(x...) do { } while (0)
55 #endif
57 #if defined(MMU_DEBUG) || defined(AUDIT)
58 static int dbg = 1;
59 #endif
61 #ifndef MMU_DEBUG
62 #define ASSERT(x) do { } while (0)
63 #else
64 #define ASSERT(x) \
65 if (!(x)) { \
66 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
67 __FILE__, __LINE__, #x); \
69 #endif
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 \
141 | PT64_NX_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
155 #define RMAP_EXT 4
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)
188 return 1;
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)
238 #ifdef CONFIG_X86_64
239 set_64bit((unsigned long *)sptep, spte);
240 #else
241 set_64bit((unsigned long long *)sptep, spte);
242 #endif
245 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
246 struct kmem_cache *base_cache, int min)
248 void *obj;
250 if (cache->nobjs >= min)
251 return 0;
252 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
253 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
254 if (!obj)
255 return -ENOMEM;
256 cache->objects[cache->nobjs++] = obj;
258 return 0;
261 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
263 while (mc->nobjs)
264 kfree(mc->objects[--mc->nobjs]);
267 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
268 int min)
270 struct page *page;
272 if (cache->nobjs >= min)
273 return 0;
274 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
275 page = alloc_page(GFP_KERNEL);
276 if (!page)
277 return -ENOMEM;
278 set_page_private(page, 0);
279 cache->objects[cache->nobjs++] = page_address(page);
281 return 0;
284 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
286 while (mc->nobjs)
287 free_page((unsigned long)mc->objects[--mc->nobjs]);
290 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
292 int r;
294 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
295 pte_chain_cache, 4);
296 if (r)
297 goto out;
298 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
299 rmap_desc_cache, 1);
300 if (r)
301 goto out;
302 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
303 if (r)
304 goto out;
305 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
306 mmu_page_header_cache, 4);
307 out:
308 return r;
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,
320 size_t size)
322 void *p;
324 BUG_ON(!mc->nobjs);
325 p = mc->objects[--mc->nobjs];
326 memset(p, 0, size);
327 return p;
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)
338 kfree(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)
349 kfree(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;
379 int i;
381 if (!is_rmap_pte(*spte))
382 return;
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);
387 if (!*rmapp) {
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;
396 } else {
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)
400 desc = desc->more;
401 if (desc->shadow_ptes[RMAP_EXT-1]) {
402 desc->more = mmu_alloc_rmap_desc(vcpu);
403 desc = desc->more;
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,
413 int i,
414 struct kvm_rmap_desc *prev_desc)
416 int j;
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;
422 if (j != 0)
423 return;
424 if (!prev_desc && !desc->more)
425 *rmapp = (unsigned long)desc->shadow_ptes[0];
426 else
427 if (prev_desc)
428 prev_desc->more = desc->more;
429 else
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;
439 struct page *page;
440 unsigned long *rmapp;
441 int i;
443 if (!is_rmap_pte(*spte))
444 return;
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);
450 else
451 kvm_release_page_clean(page);
452 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
453 if (!*rmapp) {
454 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
455 BUG();
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",
460 spte, *spte);
461 BUG();
463 *rmapp = 0;
464 } else {
465 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
466 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
467 prev_desc = NULL;
468 while (desc) {
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,
472 desc, i,
473 prev_desc);
474 return;
476 prev_desc = desc;
477 desc = desc->more;
479 BUG();
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;
487 u64 *prev_spte;
488 int i;
490 if (!*rmapp)
491 return NULL;
492 else if (!(*rmapp & 1)) {
493 if (!spte)
494 return (u64 *)*rmapp;
495 return NULL;
497 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
498 prev_desc = NULL;
499 prev_spte = NULL;
500 while (desc) {
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];
506 desc = desc->more;
508 return NULL;
511 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
513 unsigned long *rmapp;
514 u64 *spte;
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);
521 while (spte) {
522 BUG_ON(!spte);
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);
527 write_protected = 1;
529 spte = rmap_next(kvm, rmapp, spte);
531 if (write_protected)
532 kvm_flush_remote_tlbs(kvm);
535 #ifdef MMU_DEBUG
536 static int is_empty_shadow_page(u64 *spt)
538 u64 *pos;
539 u64 *end;
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__,
544 pos, *pos);
545 return 0;
547 return 1;
549 #endif
551 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
553 ASSERT(is_empty_shadow_page(sp->spt));
554 list_del(&sp->link);
555 __free_page(virt_to_page(sp->spt));
556 __free_page(virt_to_page(sp->gfns));
557 kfree(sp);
558 ++kvm->arch.n_free_mmu_pages;
561 static unsigned kvm_page_table_hashfn(gfn_t gfn)
563 return gfn;
566 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
567 u64 *parent_pte)
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));
577 sp->slot_bitmap = 0;
578 sp->multimapped = 0;
579 sp->parent_pte = parent_pte;
580 --vcpu->kvm->arch.n_free_mmu_pages;
581 return sp;
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;
589 int i;
591 if (!parent_pte)
592 return;
593 if (!sp->multimapped) {
594 u64 *old = sp->parent_pte;
596 if (!old) {
597 sp->parent_pte = parent_pte;
598 return;
600 sp->multimapped = 1;
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])
608 continue;
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;
612 return;
615 pte_chain = mmu_alloc_pte_chain(vcpu);
616 BUG_ON(!pte_chain);
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,
622 u64 *parent_pte)
624 struct kvm_pte_chain *pte_chain;
625 struct hlist_node *node;
626 int i;
628 if (!sp->multimapped) {
629 BUG_ON(sp->parent_pte != parent_pte);
630 sp->parent_pte = NULL;
631 return;
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])
636 break;
637 if (pte_chain->parent_ptes[i] != parent_pte)
638 continue;
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];
643 ++i;
645 pte_chain->parent_ptes[i] = NULL;
646 if (i == 0) {
647 hlist_del(&pte_chain->link);
648 mmu_free_pte_chain(pte_chain);
649 if (hlist_empty(&sp->parent_ptes)) {
650 sp->multimapped = 0;
651 sp->parent_pte = NULL;
654 return;
656 BUG();
659 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
661 unsigned index;
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);
673 return sp;
675 return NULL;
678 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
679 gfn_t gfn,
680 gva_t gaddr,
681 unsigned level,
682 int metaphysical,
683 unsigned access,
684 <<<<<<< HEAD:arch/x86/kvm/mmu.c
685 u64 *parent_pte,
686 bool *new_page)
687 =======
688 u64 *parent_pte)
689 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
691 union kvm_mmu_page_role role;
692 unsigned index;
693 unsigned quadrant;
694 struct hlist_head *bucket;
695 struct kvm_mmu_page *sp;
696 struct hlist_node *node;
698 role.word = 0;
699 role.glevels = vcpu->arch.mmu.root_level;
700 role.level = 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__,
709 gfn, role.word);
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__);
716 return sp;
718 ++vcpu->kvm->stat.mmu_cache_miss;
719 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
720 if (!sp)
721 return sp;
722 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
723 sp->gfn = gfn;
724 sp->role = role;
725 hlist_add_head(&sp->hash_link, bucket);
726 vcpu->arch.mmu.prefetch_page(vcpu, sp);
727 if (!metaphysical)
728 rmap_write_protect(vcpu->kvm, gfn);
729 <<<<<<< HEAD:arch/x86/kvm/mmu.c
730 if (new_page)
731 *new_page = 1;
732 =======
733 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
734 return sp;
737 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
738 struct kvm_mmu_page *sp)
740 unsigned i;
741 u64 *pt;
742 u64 ent;
744 pt = sp->spt;
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);
753 return;
756 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
757 ent = pt[i];
759 pt[i] = shadow_trap_nonpresent_pte;
760 if (!is_shadow_present_pte(ent))
761 continue;
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)
775 int i;
777 for (i = 0; i < KVM_MAX_VCPUS; ++i)
778 if (kvm->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)
784 u64 *parent_pte;
786 ++kvm->stat.mmu_shadow_zapped;
787 while (sp->multimapped || sp->parent_pte) {
788 if (!sp->multimapped)
789 parent_pte = sp->parent_pte;
790 else {
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];
797 BUG_ON(!parent_pte);
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);
805 } else
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
819 * change the value
822 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
823 kvm_nr_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);
833 n_used_mmu_pages--;
835 kvm->arch.n_free_mmu_pages = 0;
837 else
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)
846 unsigned index;
847 struct hlist_head *bucket;
848 struct kvm_mmu_page *sp;
849 struct hlist_node *node, *n;
850 int r;
852 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
853 r = 0;
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,
859 sp->role.word);
860 kvm_mmu_zap_page(kvm, sp);
861 r = 1;
863 return r;
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
887 =======
888 struct page *page;
890 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
891 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
893 if (gpa == UNMAPPED_GVA)
894 return NULL;
895 <<<<<<< HEAD:arch/x86/kvm/mmu.c
896 return gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
897 =======
899 down_read(&current->mm->mmap_sem);
900 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
901 up_read(&current->mm->mmap_sem);
903 return page;
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)
912 u64 spte;
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
924 * demand paging).
926 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
927 if (!dirty)
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);
940 return;
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;
950 if (user_fault) {
951 mmu_unshadow(vcpu->kvm, gfn);
952 goto unshadowed;
955 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
956 if (shadow) {
957 pgprintk("%s: found shadow page for %lx, marking ro\n",
958 __FUNCTION__, gfn);
959 pte_access &= ~ACC_WRITE_MASK;
960 if (is_writeble_pte(spte)) {
961 spte &= ~PT_WRITABLE_MASK;
962 kvm_x86_ops->tlb_flush(vcpu);
964 if (write_fault)
965 *ptwrite = 1;
969 unshadowed:
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);
977 if (!was_rmapped) {
978 rmap_add(vcpu, shadow_pte, gfn);
979 if (!is_rmap_pte(*shadow_pte))
980 kvm_release_page_clean(page);
981 } else {
982 if (was_writeble)
983 kvm_release_page_dirty(page);
984 else
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;
1000 int pt_write = 0;
1002 for (; ; level--) {
1003 u32 index = PT64_INDEX(v, level);
1004 u64 *table;
1006 ASSERT(VALID_PAGE(table_addr));
1007 table = __va(table_addr);
1009 if (level == 1) {
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;
1017 gfn_t pseudo_gfn;
1019 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1020 >> PAGE_SHIFT;
1021 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1022 v, level - 1,
1023 <<<<<<< HEAD:arch/x86/kvm/mmu.c
1024 1, ACC_ALL, &table[index],
1025 NULL);
1026 =======
1027 1, ACC_ALL, &table[index]);
1028 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
1029 if (!new_table) {
1030 pgprintk("nonpaging_map: ENOMEM\n");
1031 kvm_release_page_clean(page);
1032 return -ENOMEM;
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)
1044 int r;
1046 struct page *page;
1048 <<<<<<< HEAD:arch/x86/kvm/mmu.c
1049 =======
1050 down_read(&vcpu->kvm->slots_lock);
1052 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
1053 down_read(&current->mm->mmap_sem);
1054 page = gfn_to_page(vcpu->kvm, gfn);
1055 <<<<<<< HEAD:arch/x86/kvm/mmu.c
1056 =======
1057 up_read(&current->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(&current->mm->mmap_sem);
1067 =======
1068 up_read(&vcpu->kvm->slots_lock);
1069 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
1071 return r;
1075 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1076 struct kvm_mmu_page *sp)
1078 int i;
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)
1086 int i;
1087 struct kvm_mmu_page *sp;
1089 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1090 return;
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);
1097 --sp->root_count;
1098 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1099 spin_unlock(&vcpu->kvm->mmu_lock);
1100 return;
1102 #endif
1103 for (i = 0; i < 4; ++i) {
1104 hpa_t root = vcpu->arch.mmu.pae_root[i];
1106 if (root) {
1107 root &= PT64_BASE_ADDR_MASK;
1108 sp = page_header(root);
1109 --sp->root_count;
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)
1119 int i;
1120 gfn_t root_gfn;
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);
1133 =======
1134 PT64_ROOT_LEVEL, 0, ACC_ALL, NULL);
1135 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
1136 root = __pa(sp->spt);
1137 ++sp->root_count;
1138 vcpu->arch.mmu.root_hpa = root;
1139 return;
1141 #endif
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;
1149 continue;
1151 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1152 } else if (vcpu->arch.mmu.root_level == 0)
1153 root_gfn = 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);
1158 =======
1159 ACC_ALL, NULL);
1160 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
1161 root = __pa(sp->spt);
1162 ++sp->root_count;
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)
1170 return vaddr;
1173 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
1174 u32 error_code)
1176 gfn_t gfn;
1177 int r;
1179 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
1180 r = mmu_topup_memory_caches(vcpu);
1181 if (r)
1182 return r;
1184 ASSERT(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;
1210 return 0;
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);
1223 =======
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,
1230 u64 addr,
1231 u32 err_code)
1233 kvm_inject_page_fault(vcpu, addr, err_code);
1236 static void paging_free(struct kvm_vcpu *vcpu)
1238 nonpaging_free(vcpu);
1241 #define PTTYPE 64
1242 #include "paging_tmpl.h"
1243 #undef PTTYPE
1245 #define PTTYPE 32
1246 #include "paging_tmpl.h"
1247 #undef PTTYPE
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;
1262 return 0;
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;
1282 return 0;
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)
1292 ASSERT(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);
1301 else
1302 return paging32_init_context(vcpu);
1305 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1307 ASSERT(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)
1323 int r;
1325 r = mmu_topup_memory_caches(vcpu);
1326 if (r)
1327 goto out;
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);
1334 out:
1335 return r;
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,
1346 u64 *spte)
1348 u64 pte;
1349 struct kvm_mmu_page *child;
1351 pte = *spte;
1352 if (is_shadow_present_pte(pte)) {
1353 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
1354 rmap_remove(vcpu->kvm, spte);
1355 else {
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,
1365 u64 *spte,
1366 const void *new, int bytes,
1367 int offset_in_pte)
1369 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1370 ++vcpu->kvm->stat.mmu_pde_zapped;
1371 return;
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);
1377 else
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))
1384 return false;
1385 if (!is_shadow_present_pte(new))
1386 return true;
1387 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1388 return true;
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);
1398 else
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)
1412 gfn_t gfn;
1413 int r;
1414 u64 gpte = 0;
1415 <<<<<<< HEAD:arch/x86/kvm/mmu.c
1416 =======
1417 struct page *page;
1418 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kvm/mmu.c
1420 if (bytes != 4 && bytes != 8)
1421 return;
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
1427 * by update_pte().
1429 if (is_pae(vcpu)) {
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);
1433 if (r)
1434 return;
1435 memcpy((void *)&gpte + (gpa % 8), new, 4);
1436 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1437 memcpy((void *)&gpte, new, 8);
1439 } else {
1440 if ((bytes == 4) && (gpa % 4 == 0))
1441 memcpy((void *)&gpte, new, 4);
1443 if (!is_present_pte(gpte))
1444 return;
1445 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1446 <<<<<<< HEAD:arch/x86/kvm/mmu.c
1447 =======
1449 down_read(&current->mm->mmap_sem);
1450 page = gfn_to_page(vcpu->kvm, gfn);
1451 up_read(&current->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;
1465 unsigned index;
1466 u64 entry;
1467 u64 *spte;
1468 unsigned offset = offset_in_page(gpa);
1469 unsigned pte_size;
1470 unsigned page_offset;
1471 unsigned misaligned;
1472 unsigned quadrant;
1473 int level;
1474 int flooded = 0;
1475 int npte;
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)
1487 flooded = 1;
1488 } else {
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)
1497 continue;
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
1505 * as a page table.
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
1510 * page.
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;
1516 continue;
1518 page_offset = offset;
1519 level = sp->role.level;
1520 npte = 1;
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 */
1530 page_offset <<= 1;
1531 npte = 2;
1533 quadrant = page_offset >> PAGE_SHIFT;
1534 page_offset &= ~PAGE_MASK;
1535 if (quadrant != sp->role.quadrant)
1536 continue;
1538 spte = &sp->spt[page_offset / sizeof(*spte)];
1539 while (npte--) {
1540 entry = *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);
1545 ++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)
1558 gpa_t gpa;
1559 int r;
1561 <<<<<<< HEAD:arch/x86/kvm/mmu.c
1562 down_read(&current->mm->mmap_sem);
1563 =======
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(&current->mm->mmap_sem);
1569 =======
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);
1576 return r;
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)
1593 int r;
1594 enum emulation_result er;
1596 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
1597 if (r < 0)
1598 goto out;
1600 if (!r) {
1601 r = 1;
1602 goto out;
1605 r = mmu_topup_memory_caches(vcpu);
1606 if (r)
1607 goto out;
1609 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
1611 switch (er) {
1612 case EMULATE_DONE:
1613 return 1;
1614 case EMULATE_DO_MMIO:
1615 ++vcpu->stat.mmio_exits;
1616 return 0;
1617 case EMULATE_FAIL:
1618 kvm_report_emulation_failure(vcpu, "pagetable");
1619 return 1;
1620 default:
1621 BUG();
1623 out:
1624 return r;
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)
1642 struct page *page;
1643 int i;
1645 ASSERT(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;
1650 else
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);
1659 if (!page)
1660 goto error_1;
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;
1665 return 0;
1667 error_1:
1668 free_mmu_pages(vcpu);
1669 return -ENOMEM;
1672 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1674 ASSERT(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)
1682 ASSERT(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)
1690 ASSERT(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) {
1702 int i;
1703 u64 *pt;
1705 if (!test_bit(slot, &sp->slot_bitmap))
1706 continue;
1708 pt = sp->spt;
1709 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1710 /* avoid RMW */
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),
1742 0, 0, NULL);
1743 if (!pte_chain_cache)
1744 goto nomem;
1745 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1746 sizeof(struct kvm_rmap_desc),
1747 0, 0, NULL);
1748 if (!rmap_desc_cache)
1749 goto nomem;
1751 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1752 sizeof(struct kvm_mmu_page),
1753 0, 0, NULL);
1754 if (!mmu_page_header_cache)
1755 goto nomem;
1757 return 0;
1759 nomem:
1760 kvm_mmu_module_exit();
1761 return -ENOMEM;
1765 * Caculate mmu pages needed for kvm.
1767 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1769 int i;
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;
1783 #ifdef AUDIT
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;
1791 #endif
1792 return gva;
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);
1799 int i;
1800 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1802 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1803 u64 ent = pt[i];
1805 if (ent == shadow_trap_nonpresent_pte)
1806 continue;
1808 va = canonicalize(va);
1809 if (level > 1) {
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);
1817 } else {
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,
1827 va, gpa, hpa, ent,
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)
1841 unsigned i;
1843 if (vcpu->arch.mmu.root_level == 4)
1844 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
1845 else
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],
1850 i << 30,
1854 static int count_rmaps(struct kvm_vcpu *vcpu)
1856 int nmaps = 0;
1857 int i, j, k;
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];
1866 if (!*rmapp)
1867 continue;
1868 if (!(*rmapp & 1)) {
1869 ++nmaps;
1870 continue;
1872 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
1873 while (d) {
1874 for (k = 0; k < RMAP_EXT; ++k)
1875 if (d->shadow_ptes[k])
1876 ++nmaps;
1877 else
1878 break;
1879 d = d->more;
1883 return nmaps;
1886 static int count_writable_mappings(struct kvm_vcpu *vcpu)
1888 int nmaps = 0;
1889 struct kvm_mmu_page *sp;
1890 int i;
1892 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
1893 u64 *pt = sp->spt;
1895 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
1896 continue;
1898 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1899 u64 ent = pt[i];
1901 if (!(ent & PT_PRESENT_MASK))
1902 continue;
1903 if (!(ent & PT_WRITABLE_MASK))
1904 continue;
1905 ++nmaps;
1908 return nmaps;
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;
1926 gfn_t gfn;
1928 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
1929 if (sp->role.metaphysical)
1930 continue;
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];
1935 if (*rmapp)
1936 printk(KERN_ERR "%s: (%s) shadow page has writable"
1937 " mappings: gfn %lx role %x\n",
1938 __FUNCTION__, audit_msg, sp->gfn,
1939 sp->role.word);
1943 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1945 int olddbg = dbg;
1947 dbg = 0;
1948 audit_msg = msg;
1949 audit_rmap(vcpu);
1950 audit_write_protection(vcpu);
1951 audit_mappings(vcpu);
1952 dbg = olddbg;
1955 #endif