KVM: Clean up #includes
[linux-2.6.git] / drivers / kvm / mmu.c
1 /*
2  * Kernel-based Virtual Machine driver for Linux
3  *
4  * This module enables machines with Intel VT-x extensions to run virtual
5  * machines without emulation or binary translation.
6  *
7  * MMU support
8  *
9  * Copyright (C) 2006 Qumranet, Inc.
10  *
11  * Authors:
12  *   Yaniv Kamay  <yaniv@qumranet.com>
13  *   Avi Kivity   <avi@qumranet.com>
14  *
15  * This work is licensed under the terms of the GNU GPL, version 2.  See
16  * the COPYING file in the top-level directory.
17  *
18  */
19
20 #include "vmx.h"
21 #include "kvm.h"
22
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/mm.h>
26 #include <linux/highmem.h>
27 #include <linux/module.h>
28
29 #include <asm/page.h>
30 #include <asm/cmpxchg.h>
31
32 #undef MMU_DEBUG
33
34 #undef AUDIT
35
36 #ifdef AUDIT
37 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
38 #else
39 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
40 #endif
41
42 #ifdef MMU_DEBUG
43
44 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
45 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
46
47 #else
48
49 #define pgprintk(x...) do { } while (0)
50 #define rmap_printk(x...) do { } while (0)
51
52 #endif
53
54 #if defined(MMU_DEBUG) || defined(AUDIT)
55 static int dbg = 1;
56 #endif
57
58 #ifndef MMU_DEBUG
59 #define ASSERT(x) do { } while (0)
60 #else
61 #define ASSERT(x)                                                       \
62         if (!(x)) {                                                     \
63                 printk(KERN_WARNING "assertion failed %s:%d: %s\n",     \
64                        __FILE__, __LINE__, #x);                         \
65         }
66 #endif
67
68 #define PT64_PT_BITS 9
69 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
70 #define PT32_PT_BITS 10
71 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
72
73 #define PT_WRITABLE_SHIFT 1
74
75 #define PT_PRESENT_MASK (1ULL << 0)
76 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
77 #define PT_USER_MASK (1ULL << 2)
78 #define PT_PWT_MASK (1ULL << 3)
79 #define PT_PCD_MASK (1ULL << 4)
80 #define PT_ACCESSED_MASK (1ULL << 5)
81 #define PT_DIRTY_MASK (1ULL << 6)
82 #define PT_PAGE_SIZE_MASK (1ULL << 7)
83 #define PT_PAT_MASK (1ULL << 7)
84 #define PT_GLOBAL_MASK (1ULL << 8)
85 #define PT64_NX_MASK (1ULL << 63)
86
87 #define PT_PAT_SHIFT 7
88 #define PT_DIR_PAT_SHIFT 12
89 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
90
91 #define PT32_DIR_PSE36_SIZE 4
92 #define PT32_DIR_PSE36_SHIFT 13
93 #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
94
95
96 #define PT_FIRST_AVAIL_BITS_SHIFT 9
97 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
98
99 #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
100
101 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
102
103 #define PT64_LEVEL_BITS 9
104
105 #define PT64_LEVEL_SHIFT(level) \
106                 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
107
108 #define PT64_LEVEL_MASK(level) \
109                 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
110
111 #define PT64_INDEX(address, level)\
112         (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
113
114
115 #define PT32_LEVEL_BITS 10
116
117 #define PT32_LEVEL_SHIFT(level) \
118                 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
119
120 #define PT32_LEVEL_MASK(level) \
121                 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
122
123 #define PT32_INDEX(address, level)\
124         (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
125
126
127 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
128 #define PT64_DIR_BASE_ADDR_MASK \
129         (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
130
131 #define PT32_BASE_ADDR_MASK PAGE_MASK
132 #define PT32_DIR_BASE_ADDR_MASK \
133         (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
134
135
136 #define PFERR_PRESENT_MASK (1U << 0)
137 #define PFERR_WRITE_MASK (1U << 1)
138 #define PFERR_USER_MASK (1U << 2)
139 #define PFERR_FETCH_MASK (1U << 4)
140
141 #define PT64_ROOT_LEVEL 4
142 #define PT32_ROOT_LEVEL 2
143 #define PT32E_ROOT_LEVEL 3
144
145 #define PT_DIRECTORY_LEVEL 2
146 #define PT_PAGE_TABLE_LEVEL 1
147
148 #define RMAP_EXT 4
149
150 struct kvm_rmap_desc {
151         u64 *shadow_ptes[RMAP_EXT];
152         struct kvm_rmap_desc *more;
153 };
154
155 static struct kmem_cache *pte_chain_cache;
156 static struct kmem_cache *rmap_desc_cache;
157 static struct kmem_cache *mmu_page_cache;
158 static struct kmem_cache *mmu_page_header_cache;
159
160 static int is_write_protection(struct kvm_vcpu *vcpu)
161 {
162         return vcpu->cr0 & CR0_WP_MASK;
163 }
164
165 static int is_cpuid_PSE36(void)
166 {
167         return 1;
168 }
169
170 static int is_nx(struct kvm_vcpu *vcpu)
171 {
172         return vcpu->shadow_efer & EFER_NX;
173 }
174
175 static int is_present_pte(unsigned long pte)
176 {
177         return pte & PT_PRESENT_MASK;
178 }
179
180 static int is_writeble_pte(unsigned long pte)
181 {
182         return pte & PT_WRITABLE_MASK;
183 }
184
185 static int is_io_pte(unsigned long pte)
186 {
187         return pte & PT_SHADOW_IO_MARK;
188 }
189
190 static int is_rmap_pte(u64 pte)
191 {
192         return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
193                 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
194 }
195
196 static void set_shadow_pte(u64 *sptep, u64 spte)
197 {
198 #ifdef CONFIG_X86_64
199         set_64bit((unsigned long *)sptep, spte);
200 #else
201         set_64bit((unsigned long long *)sptep, spte);
202 #endif
203 }
204
205 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
206                                   struct kmem_cache *base_cache, int min,
207                                   gfp_t gfp_flags)
208 {
209         void *obj;
210
211         if (cache->nobjs >= min)
212                 return 0;
213         while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
214                 obj = kmem_cache_zalloc(base_cache, gfp_flags);
215                 if (!obj)
216                         return -ENOMEM;
217                 cache->objects[cache->nobjs++] = obj;
218         }
219         return 0;
220 }
221
222 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
223 {
224         while (mc->nobjs)
225                 kfree(mc->objects[--mc->nobjs]);
226 }
227
228 static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags)
229 {
230         int r;
231
232         r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
233                                    pte_chain_cache, 4, gfp_flags);
234         if (r)
235                 goto out;
236         r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
237                                    rmap_desc_cache, 1, gfp_flags);
238         if (r)
239                 goto out;
240         r = mmu_topup_memory_cache(&vcpu->mmu_page_cache,
241                                    mmu_page_cache, 4, gfp_flags);
242         if (r)
243                 goto out;
244         r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache,
245                                    mmu_page_header_cache, 4, gfp_flags);
246 out:
247         return r;
248 }
249
250 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
251 {
252         int r;
253
254         r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT);
255         if (r < 0) {
256                 spin_unlock(&vcpu->kvm->lock);
257                 kvm_arch_ops->vcpu_put(vcpu);
258                 r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL);
259                 kvm_arch_ops->vcpu_load(vcpu);
260                 spin_lock(&vcpu->kvm->lock);
261         }
262         return r;
263 }
264
265 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
266 {
267         mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache);
268         mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache);
269         mmu_free_memory_cache(&vcpu->mmu_page_cache);
270         mmu_free_memory_cache(&vcpu->mmu_page_header_cache);
271 }
272
273 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
274                                     size_t size)
275 {
276         void *p;
277
278         BUG_ON(!mc->nobjs);
279         p = mc->objects[--mc->nobjs];
280         memset(p, 0, size);
281         return p;
282 }
283
284 static void mmu_memory_cache_free(struct kvm_mmu_memory_cache *mc, void *obj)
285 {
286         if (mc->nobjs < KVM_NR_MEM_OBJS)
287                 mc->objects[mc->nobjs++] = obj;
288         else
289                 kfree(obj);
290 }
291
292 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
293 {
294         return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache,
295                                       sizeof(struct kvm_pte_chain));
296 }
297
298 static void mmu_free_pte_chain(struct kvm_vcpu *vcpu,
299                                struct kvm_pte_chain *pc)
300 {
301         mmu_memory_cache_free(&vcpu->mmu_pte_chain_cache, pc);
302 }
303
304 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
305 {
306         return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache,
307                                       sizeof(struct kvm_rmap_desc));
308 }
309
310 static void mmu_free_rmap_desc(struct kvm_vcpu *vcpu,
311                                struct kvm_rmap_desc *rd)
312 {
313         mmu_memory_cache_free(&vcpu->mmu_rmap_desc_cache, rd);
314 }
315
316 /*
317  * Reverse mapping data structures:
318  *
319  * If page->private bit zero is zero, then page->private points to the
320  * shadow page table entry that points to page_address(page).
321  *
322  * If page->private bit zero is one, (then page->private & ~1) points
323  * to a struct kvm_rmap_desc containing more mappings.
324  */
325 static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte)
326 {
327         struct page *page;
328         struct kvm_rmap_desc *desc;
329         int i;
330
331         if (!is_rmap_pte(*spte))
332                 return;
333         page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
334         if (!page_private(page)) {
335                 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
336                 set_page_private(page,(unsigned long)spte);
337         } else if (!(page_private(page) & 1)) {
338                 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
339                 desc = mmu_alloc_rmap_desc(vcpu);
340                 desc->shadow_ptes[0] = (u64 *)page_private(page);
341                 desc->shadow_ptes[1] = spte;
342                 set_page_private(page,(unsigned long)desc | 1);
343         } else {
344                 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
345                 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
346                 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
347                         desc = desc->more;
348                 if (desc->shadow_ptes[RMAP_EXT-1]) {
349                         desc->more = mmu_alloc_rmap_desc(vcpu);
350                         desc = desc->more;
351                 }
352                 for (i = 0; desc->shadow_ptes[i]; ++i)
353                         ;
354                 desc->shadow_ptes[i] = spte;
355         }
356 }
357
358 static void rmap_desc_remove_entry(struct kvm_vcpu *vcpu,
359                                    struct page *page,
360                                    struct kvm_rmap_desc *desc,
361                                    int i,
362                                    struct kvm_rmap_desc *prev_desc)
363 {
364         int j;
365
366         for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
367                 ;
368         desc->shadow_ptes[i] = desc->shadow_ptes[j];
369         desc->shadow_ptes[j] = NULL;
370         if (j != 0)
371                 return;
372         if (!prev_desc && !desc->more)
373                 set_page_private(page,(unsigned long)desc->shadow_ptes[0]);
374         else
375                 if (prev_desc)
376                         prev_desc->more = desc->more;
377                 else
378                         set_page_private(page,(unsigned long)desc->more | 1);
379         mmu_free_rmap_desc(vcpu, desc);
380 }
381
382 static void rmap_remove(struct kvm_vcpu *vcpu, u64 *spte)
383 {
384         struct page *page;
385         struct kvm_rmap_desc *desc;
386         struct kvm_rmap_desc *prev_desc;
387         int i;
388
389         if (!is_rmap_pte(*spte))
390                 return;
391         page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
392         if (!page_private(page)) {
393                 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
394                 BUG();
395         } else if (!(page_private(page) & 1)) {
396                 rmap_printk("rmap_remove:  %p %llx 1->0\n", spte, *spte);
397                 if ((u64 *)page_private(page) != spte) {
398                         printk(KERN_ERR "rmap_remove:  %p %llx 1->BUG\n",
399                                spte, *spte);
400                         BUG();
401                 }
402                 set_page_private(page,0);
403         } else {
404                 rmap_printk("rmap_remove:  %p %llx many->many\n", spte, *spte);
405                 desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
406                 prev_desc = NULL;
407                 while (desc) {
408                         for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
409                                 if (desc->shadow_ptes[i] == spte) {
410                                         rmap_desc_remove_entry(vcpu, page,
411                                                                desc, i,
412                                                                prev_desc);
413                                         return;
414                                 }
415                         prev_desc = desc;
416                         desc = desc->more;
417                 }
418                 BUG();
419         }
420 }
421
422 static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
423 {
424         struct kvm *kvm = vcpu->kvm;
425         struct page *page;
426         struct kvm_rmap_desc *desc;
427         u64 *spte;
428
429         page = gfn_to_page(kvm, gfn);
430         BUG_ON(!page);
431
432         while (page_private(page)) {
433                 if (!(page_private(page) & 1))
434                         spte = (u64 *)page_private(page);
435                 else {
436                         desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul);
437                         spte = desc->shadow_ptes[0];
438                 }
439                 BUG_ON(!spte);
440                 BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT
441                        != page_to_pfn(page));
442                 BUG_ON(!(*spte & PT_PRESENT_MASK));
443                 BUG_ON(!(*spte & PT_WRITABLE_MASK));
444                 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
445                 rmap_remove(vcpu, spte);
446                 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
447                 kvm_flush_remote_tlbs(vcpu->kvm);
448         }
449 }
450
451 #ifdef MMU_DEBUG
452 static int is_empty_shadow_page(u64 *spt)
453 {
454         u64 *pos;
455         u64 *end;
456
457         for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
458                 if (*pos != 0) {
459                         printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
460                                pos, *pos);
461                         return 0;
462                 }
463         return 1;
464 }
465 #endif
466
467 static void kvm_mmu_free_page(struct kvm_vcpu *vcpu,
468                               struct kvm_mmu_page *page_head)
469 {
470         ASSERT(is_empty_shadow_page(page_head->spt));
471         list_del(&page_head->link);
472         mmu_memory_cache_free(&vcpu->mmu_page_cache, page_head->spt);
473         mmu_memory_cache_free(&vcpu->mmu_page_header_cache, page_head);
474         ++vcpu->kvm->n_free_mmu_pages;
475 }
476
477 static unsigned kvm_page_table_hashfn(gfn_t gfn)
478 {
479         return gfn;
480 }
481
482 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
483                                                u64 *parent_pte)
484 {
485         struct kvm_mmu_page *page;
486
487         if (!vcpu->kvm->n_free_mmu_pages)
488                 return NULL;
489
490         page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache,
491                                       sizeof *page);
492         page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE);
493         set_page_private(virt_to_page(page->spt), (unsigned long)page);
494         list_add(&page->link, &vcpu->kvm->active_mmu_pages);
495         ASSERT(is_empty_shadow_page(page->spt));
496         page->slot_bitmap = 0;
497         page->multimapped = 0;
498         page->parent_pte = parent_pte;
499         --vcpu->kvm->n_free_mmu_pages;
500         return page;
501 }
502
503 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
504                                     struct kvm_mmu_page *page, u64 *parent_pte)
505 {
506         struct kvm_pte_chain *pte_chain;
507         struct hlist_node *node;
508         int i;
509
510         if (!parent_pte)
511                 return;
512         if (!page->multimapped) {
513                 u64 *old = page->parent_pte;
514
515                 if (!old) {
516                         page->parent_pte = parent_pte;
517                         return;
518                 }
519                 page->multimapped = 1;
520                 pte_chain = mmu_alloc_pte_chain(vcpu);
521                 INIT_HLIST_HEAD(&page->parent_ptes);
522                 hlist_add_head(&pte_chain->link, &page->parent_ptes);
523                 pte_chain->parent_ptes[0] = old;
524         }
525         hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
526                 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
527                         continue;
528                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
529                         if (!pte_chain->parent_ptes[i]) {
530                                 pte_chain->parent_ptes[i] = parent_pte;
531                                 return;
532                         }
533         }
534         pte_chain = mmu_alloc_pte_chain(vcpu);
535         BUG_ON(!pte_chain);
536         hlist_add_head(&pte_chain->link, &page->parent_ptes);
537         pte_chain->parent_ptes[0] = parent_pte;
538 }
539
540 static void mmu_page_remove_parent_pte(struct kvm_vcpu *vcpu,
541                                        struct kvm_mmu_page *page,
542                                        u64 *parent_pte)
543 {
544         struct kvm_pte_chain *pte_chain;
545         struct hlist_node *node;
546         int i;
547
548         if (!page->multimapped) {
549                 BUG_ON(page->parent_pte != parent_pte);
550                 page->parent_pte = NULL;
551                 return;
552         }
553         hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
554                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
555                         if (!pte_chain->parent_ptes[i])
556                                 break;
557                         if (pte_chain->parent_ptes[i] != parent_pte)
558                                 continue;
559                         while (i + 1 < NR_PTE_CHAIN_ENTRIES
560                                 && pte_chain->parent_ptes[i + 1]) {
561                                 pte_chain->parent_ptes[i]
562                                         = pte_chain->parent_ptes[i + 1];
563                                 ++i;
564                         }
565                         pte_chain->parent_ptes[i] = NULL;
566                         if (i == 0) {
567                                 hlist_del(&pte_chain->link);
568                                 mmu_free_pte_chain(vcpu, pte_chain);
569                                 if (hlist_empty(&page->parent_ptes)) {
570                                         page->multimapped = 0;
571                                         page->parent_pte = NULL;
572                                 }
573                         }
574                         return;
575                 }
576         BUG();
577 }
578
579 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
580                                                 gfn_t gfn)
581 {
582         unsigned index;
583         struct hlist_head *bucket;
584         struct kvm_mmu_page *page;
585         struct hlist_node *node;
586
587         pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
588         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
589         bucket = &vcpu->kvm->mmu_page_hash[index];
590         hlist_for_each_entry(page, node, bucket, hash_link)
591                 if (page->gfn == gfn && !page->role.metaphysical) {
592                         pgprintk("%s: found role %x\n",
593                                  __FUNCTION__, page->role.word);
594                         return page;
595                 }
596         return NULL;
597 }
598
599 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
600                                              gfn_t gfn,
601                                              gva_t gaddr,
602                                              unsigned level,
603                                              int metaphysical,
604                                              unsigned hugepage_access,
605                                              u64 *parent_pte)
606 {
607         union kvm_mmu_page_role role;
608         unsigned index;
609         unsigned quadrant;
610         struct hlist_head *bucket;
611         struct kvm_mmu_page *page;
612         struct hlist_node *node;
613
614         role.word = 0;
615         role.glevels = vcpu->mmu.root_level;
616         role.level = level;
617         role.metaphysical = metaphysical;
618         role.hugepage_access = hugepage_access;
619         if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
620                 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
621                 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
622                 role.quadrant = quadrant;
623         }
624         pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
625                  gfn, role.word);
626         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
627         bucket = &vcpu->kvm->mmu_page_hash[index];
628         hlist_for_each_entry(page, node, bucket, hash_link)
629                 if (page->gfn == gfn && page->role.word == role.word) {
630                         mmu_page_add_parent_pte(vcpu, page, parent_pte);
631                         pgprintk("%s: found\n", __FUNCTION__);
632                         return page;
633                 }
634         page = kvm_mmu_alloc_page(vcpu, parent_pte);
635         if (!page)
636                 return page;
637         pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
638         page->gfn = gfn;
639         page->role = role;
640         hlist_add_head(&page->hash_link, bucket);
641         if (!metaphysical)
642                 rmap_write_protect(vcpu, gfn);
643         return page;
644 }
645
646 static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
647                                          struct kvm_mmu_page *page)
648 {
649         unsigned i;
650         u64 *pt;
651         u64 ent;
652
653         pt = page->spt;
654
655         if (page->role.level == PT_PAGE_TABLE_LEVEL) {
656                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
657                         if (pt[i] & PT_PRESENT_MASK)
658                                 rmap_remove(vcpu, &pt[i]);
659                         pt[i] = 0;
660                 }
661                 kvm_flush_remote_tlbs(vcpu->kvm);
662                 return;
663         }
664
665         for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
666                 ent = pt[i];
667
668                 pt[i] = 0;
669                 if (!(ent & PT_PRESENT_MASK))
670                         continue;
671                 ent &= PT64_BASE_ADDR_MASK;
672                 mmu_page_remove_parent_pte(vcpu, page_header(ent), &pt[i]);
673         }
674         kvm_flush_remote_tlbs(vcpu->kvm);
675 }
676
677 static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
678                              struct kvm_mmu_page *page,
679                              u64 *parent_pte)
680 {
681         mmu_page_remove_parent_pte(vcpu, page, parent_pte);
682 }
683
684 static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
685                              struct kvm_mmu_page *page)
686 {
687         u64 *parent_pte;
688
689         while (page->multimapped || page->parent_pte) {
690                 if (!page->multimapped)
691                         parent_pte = page->parent_pte;
692                 else {
693                         struct kvm_pte_chain *chain;
694
695                         chain = container_of(page->parent_ptes.first,
696                                              struct kvm_pte_chain, link);
697                         parent_pte = chain->parent_ptes[0];
698                 }
699                 BUG_ON(!parent_pte);
700                 kvm_mmu_put_page(vcpu, page, parent_pte);
701                 set_shadow_pte(parent_pte, 0);
702         }
703         kvm_mmu_page_unlink_children(vcpu, page);
704         if (!page->root_count) {
705                 hlist_del(&page->hash_link);
706                 kvm_mmu_free_page(vcpu, page);
707         } else
708                 list_move(&page->link, &vcpu->kvm->active_mmu_pages);
709 }
710
711 static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
712 {
713         unsigned index;
714         struct hlist_head *bucket;
715         struct kvm_mmu_page *page;
716         struct hlist_node *node, *n;
717         int r;
718
719         pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
720         r = 0;
721         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
722         bucket = &vcpu->kvm->mmu_page_hash[index];
723         hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
724                 if (page->gfn == gfn && !page->role.metaphysical) {
725                         pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
726                                  page->role.word);
727                         kvm_mmu_zap_page(vcpu, page);
728                         r = 1;
729                 }
730         return r;
731 }
732
733 static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn)
734 {
735         struct kvm_mmu_page *page;
736
737         while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) {
738                 pgprintk("%s: zap %lx %x\n",
739                          __FUNCTION__, gfn, page->role.word);
740                 kvm_mmu_zap_page(vcpu, page);
741         }
742 }
743
744 static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
745 {
746         int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
747         struct kvm_mmu_page *page_head = page_header(__pa(pte));
748
749         __set_bit(slot, &page_head->slot_bitmap);
750 }
751
752 hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
753 {
754         hpa_t hpa = gpa_to_hpa(vcpu, gpa);
755
756         return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
757 }
758
759 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
760 {
761         struct page *page;
762
763         ASSERT((gpa & HPA_ERR_MASK) == 0);
764         page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
765         if (!page)
766                 return gpa | HPA_ERR_MASK;
767         return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
768                 | (gpa & (PAGE_SIZE-1));
769 }
770
771 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
772 {
773         gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
774
775         if (gpa == UNMAPPED_GVA)
776                 return UNMAPPED_GVA;
777         return gpa_to_hpa(vcpu, gpa);
778 }
779
780 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
781 {
782         gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
783
784         if (gpa == UNMAPPED_GVA)
785                 return NULL;
786         return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT);
787 }
788
789 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
790 {
791 }
792
793 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
794 {
795         int level = PT32E_ROOT_LEVEL;
796         hpa_t table_addr = vcpu->mmu.root_hpa;
797
798         for (; ; level--) {
799                 u32 index = PT64_INDEX(v, level);
800                 u64 *table;
801                 u64 pte;
802
803                 ASSERT(VALID_PAGE(table_addr));
804                 table = __va(table_addr);
805
806                 if (level == 1) {
807                         pte = table[index];
808                         if (is_present_pte(pte) && is_writeble_pte(pte))
809                                 return 0;
810                         mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
811                         page_header_update_slot(vcpu->kvm, table, v);
812                         table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
813                                                                 PT_USER_MASK;
814                         rmap_add(vcpu, &table[index]);
815                         return 0;
816                 }
817
818                 if (table[index] == 0) {
819                         struct kvm_mmu_page *new_table;
820                         gfn_t pseudo_gfn;
821
822                         pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
823                                 >> PAGE_SHIFT;
824                         new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
825                                                      v, level - 1,
826                                                      1, 0, &table[index]);
827                         if (!new_table) {
828                                 pgprintk("nonpaging_map: ENOMEM\n");
829                                 return -ENOMEM;
830                         }
831
832                         table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
833                                 | PT_WRITABLE_MASK | PT_USER_MASK;
834                 }
835                 table_addr = table[index] & PT64_BASE_ADDR_MASK;
836         }
837 }
838
839 static void mmu_free_roots(struct kvm_vcpu *vcpu)
840 {
841         int i;
842         struct kvm_mmu_page *page;
843
844         if (!VALID_PAGE(vcpu->mmu.root_hpa))
845                 return;
846 #ifdef CONFIG_X86_64
847         if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
848                 hpa_t root = vcpu->mmu.root_hpa;
849
850                 page = page_header(root);
851                 --page->root_count;
852                 vcpu->mmu.root_hpa = INVALID_PAGE;
853                 return;
854         }
855 #endif
856         for (i = 0; i < 4; ++i) {
857                 hpa_t root = vcpu->mmu.pae_root[i];
858
859                 if (root) {
860                         root &= PT64_BASE_ADDR_MASK;
861                         page = page_header(root);
862                         --page->root_count;
863                 }
864                 vcpu->mmu.pae_root[i] = INVALID_PAGE;
865         }
866         vcpu->mmu.root_hpa = INVALID_PAGE;
867 }
868
869 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
870 {
871         int i;
872         gfn_t root_gfn;
873         struct kvm_mmu_page *page;
874
875         root_gfn = vcpu->cr3 >> PAGE_SHIFT;
876
877 #ifdef CONFIG_X86_64
878         if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
879                 hpa_t root = vcpu->mmu.root_hpa;
880
881                 ASSERT(!VALID_PAGE(root));
882                 page = kvm_mmu_get_page(vcpu, root_gfn, 0,
883                                         PT64_ROOT_LEVEL, 0, 0, NULL);
884                 root = __pa(page->spt);
885                 ++page->root_count;
886                 vcpu->mmu.root_hpa = root;
887                 return;
888         }
889 #endif
890         for (i = 0; i < 4; ++i) {
891                 hpa_t root = vcpu->mmu.pae_root[i];
892
893                 ASSERT(!VALID_PAGE(root));
894                 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) {
895                         if (!is_present_pte(vcpu->pdptrs[i])) {
896                                 vcpu->mmu.pae_root[i] = 0;
897                                 continue;
898                         }
899                         root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
900                 } else if (vcpu->mmu.root_level == 0)
901                         root_gfn = 0;
902                 page = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
903                                         PT32_ROOT_LEVEL, !is_paging(vcpu),
904                                         0, NULL);
905                 root = __pa(page->spt);
906                 ++page->root_count;
907                 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
908         }
909         vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
910 }
911
912 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
913 {
914         return vaddr;
915 }
916
917 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
918                                u32 error_code)
919 {
920         gpa_t addr = gva;
921         hpa_t paddr;
922         int r;
923
924         r = mmu_topup_memory_caches(vcpu);
925         if (r)
926                 return r;
927
928         ASSERT(vcpu);
929         ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
930
931
932         paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
933
934         if (is_error_hpa(paddr))
935                 return 1;
936
937         return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
938 }
939
940 static void nonpaging_free(struct kvm_vcpu *vcpu)
941 {
942         mmu_free_roots(vcpu);
943 }
944
945 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
946 {
947         struct kvm_mmu *context = &vcpu->mmu;
948
949         context->new_cr3 = nonpaging_new_cr3;
950         context->page_fault = nonpaging_page_fault;
951         context->gva_to_gpa = nonpaging_gva_to_gpa;
952         context->free = nonpaging_free;
953         context->root_level = 0;
954         context->shadow_root_level = PT32E_ROOT_LEVEL;
955         context->root_hpa = INVALID_PAGE;
956         return 0;
957 }
958
959 static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
960 {
961         ++vcpu->stat.tlb_flush;
962         kvm_arch_ops->tlb_flush(vcpu);
963 }
964
965 static void paging_new_cr3(struct kvm_vcpu *vcpu)
966 {
967         pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
968         mmu_free_roots(vcpu);
969 }
970
971 static void inject_page_fault(struct kvm_vcpu *vcpu,
972                               u64 addr,
973                               u32 err_code)
974 {
975         kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
976 }
977
978 static void paging_free(struct kvm_vcpu *vcpu)
979 {
980         nonpaging_free(vcpu);
981 }
982
983 #define PTTYPE 64
984 #include "paging_tmpl.h"
985 #undef PTTYPE
986
987 #define PTTYPE 32
988 #include "paging_tmpl.h"
989 #undef PTTYPE
990
991 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
992 {
993         struct kvm_mmu *context = &vcpu->mmu;
994
995         ASSERT(is_pae(vcpu));
996         context->new_cr3 = paging_new_cr3;
997         context->page_fault = paging64_page_fault;
998         context->gva_to_gpa = paging64_gva_to_gpa;
999         context->free = paging_free;
1000         context->root_level = level;
1001         context->shadow_root_level = level;
1002         context->root_hpa = INVALID_PAGE;
1003         return 0;
1004 }
1005
1006 static int paging64_init_context(struct kvm_vcpu *vcpu)
1007 {
1008         return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1009 }
1010
1011 static int paging32_init_context(struct kvm_vcpu *vcpu)
1012 {
1013         struct kvm_mmu *context = &vcpu->mmu;
1014
1015         context->new_cr3 = paging_new_cr3;
1016         context->page_fault = paging32_page_fault;
1017         context->gva_to_gpa = paging32_gva_to_gpa;
1018         context->free = paging_free;
1019         context->root_level = PT32_ROOT_LEVEL;
1020         context->shadow_root_level = PT32E_ROOT_LEVEL;
1021         context->root_hpa = INVALID_PAGE;
1022         return 0;
1023 }
1024
1025 static int paging32E_init_context(struct kvm_vcpu *vcpu)
1026 {
1027         return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
1028 }
1029
1030 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1031 {
1032         ASSERT(vcpu);
1033         ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1034
1035         if (!is_paging(vcpu))
1036                 return nonpaging_init_context(vcpu);
1037         else if (is_long_mode(vcpu))
1038                 return paging64_init_context(vcpu);
1039         else if (is_pae(vcpu))
1040                 return paging32E_init_context(vcpu);
1041         else
1042                 return paging32_init_context(vcpu);
1043 }
1044
1045 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1046 {
1047         ASSERT(vcpu);
1048         if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1049                 vcpu->mmu.free(vcpu);
1050                 vcpu->mmu.root_hpa = INVALID_PAGE;
1051         }
1052 }
1053
1054 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1055 {
1056         destroy_kvm_mmu(vcpu);
1057         return init_kvm_mmu(vcpu);
1058 }
1059
1060 int kvm_mmu_load(struct kvm_vcpu *vcpu)
1061 {
1062         int r;
1063
1064         spin_lock(&vcpu->kvm->lock);
1065         r = mmu_topup_memory_caches(vcpu);
1066         if (r)
1067                 goto out;
1068         mmu_alloc_roots(vcpu);
1069         kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
1070         kvm_mmu_flush_tlb(vcpu);
1071 out:
1072         spin_unlock(&vcpu->kvm->lock);
1073         return r;
1074 }
1075 EXPORT_SYMBOL_GPL(kvm_mmu_load);
1076
1077 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1078 {
1079         mmu_free_roots(vcpu);
1080 }
1081
1082 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
1083                                   struct kvm_mmu_page *page,
1084                                   u64 *spte)
1085 {
1086         u64 pte;
1087         struct kvm_mmu_page *child;
1088
1089         pte = *spte;
1090         if (is_present_pte(pte)) {
1091                 if (page->role.level == PT_PAGE_TABLE_LEVEL)
1092                         rmap_remove(vcpu, spte);
1093                 else {
1094                         child = page_header(pte & PT64_BASE_ADDR_MASK);
1095                         mmu_page_remove_parent_pte(vcpu, child, spte);
1096                 }
1097         }
1098         *spte = 0;
1099         kvm_flush_remote_tlbs(vcpu->kvm);
1100 }
1101
1102 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
1103                                   struct kvm_mmu_page *page,
1104                                   u64 *spte,
1105                                   const void *new, int bytes)
1106 {
1107         if (page->role.level != PT_PAGE_TABLE_LEVEL)
1108                 return;
1109
1110         if (page->role.glevels == PT32_ROOT_LEVEL)
1111                 paging32_update_pte(vcpu, page, spte, new, bytes);
1112         else
1113                 paging64_update_pte(vcpu, page, spte, new, bytes);
1114 }
1115
1116 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1117                        const u8 *old, const u8 *new, int bytes)
1118 {
1119         gfn_t gfn = gpa >> PAGE_SHIFT;
1120         struct kvm_mmu_page *page;
1121         struct hlist_node *node, *n;
1122         struct hlist_head *bucket;
1123         unsigned index;
1124         u64 *spte;
1125         unsigned offset = offset_in_page(gpa);
1126         unsigned pte_size;
1127         unsigned page_offset;
1128         unsigned misaligned;
1129         unsigned quadrant;
1130         int level;
1131         int flooded = 0;
1132         int npte;
1133
1134         pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
1135         if (gfn == vcpu->last_pt_write_gfn) {
1136                 ++vcpu->last_pt_write_count;
1137                 if (vcpu->last_pt_write_count >= 3)
1138                         flooded = 1;
1139         } else {
1140                 vcpu->last_pt_write_gfn = gfn;
1141                 vcpu->last_pt_write_count = 1;
1142         }
1143         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1144         bucket = &vcpu->kvm->mmu_page_hash[index];
1145         hlist_for_each_entry_safe(page, node, n, bucket, hash_link) {
1146                 if (page->gfn != gfn || page->role.metaphysical)
1147                         continue;
1148                 pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
1149                 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
1150                 misaligned |= bytes < 4;
1151                 if (misaligned || flooded) {
1152                         /*
1153                          * Misaligned accesses are too much trouble to fix
1154                          * up; also, they usually indicate a page is not used
1155                          * as a page table.
1156                          *
1157                          * If we're seeing too many writes to a page,
1158                          * it may no longer be a page table, or we may be
1159                          * forking, in which case it is better to unmap the
1160                          * page.
1161                          */
1162                         pgprintk("misaligned: gpa %llx bytes %d role %x\n",
1163                                  gpa, bytes, page->role.word);
1164                         kvm_mmu_zap_page(vcpu, page);
1165                         continue;
1166                 }
1167                 page_offset = offset;
1168                 level = page->role.level;
1169                 npte = 1;
1170                 if (page->role.glevels == PT32_ROOT_LEVEL) {
1171                         page_offset <<= 1;      /* 32->64 */
1172                         /*
1173                          * A 32-bit pde maps 4MB while the shadow pdes map
1174                          * only 2MB.  So we need to double the offset again
1175                          * and zap two pdes instead of one.
1176                          */
1177                         if (level == PT32_ROOT_LEVEL) {
1178                                 page_offset &= ~7; /* kill rounding error */
1179                                 page_offset <<= 1;
1180                                 npte = 2;
1181                         }
1182                         quadrant = page_offset >> PAGE_SHIFT;
1183                         page_offset &= ~PAGE_MASK;
1184                         if (quadrant != page->role.quadrant)
1185                                 continue;
1186                 }
1187                 spte = &page->spt[page_offset / sizeof(*spte)];
1188                 while (npte--) {
1189                         mmu_pte_write_zap_pte(vcpu, page, spte);
1190                         mmu_pte_write_new_pte(vcpu, page, spte, new, bytes);
1191                         ++spte;
1192                 }
1193         }
1194 }
1195
1196 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1197 {
1198         gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1199
1200         return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1201 }
1202
1203 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1204 {
1205         while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1206                 struct kvm_mmu_page *page;
1207
1208                 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1209                                     struct kvm_mmu_page, link);
1210                 kvm_mmu_zap_page(vcpu, page);
1211         }
1212 }
1213 EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1214
1215 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1216 {
1217         struct kvm_mmu_page *page;
1218
1219         while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1220                 page = container_of(vcpu->kvm->active_mmu_pages.next,
1221                                     struct kvm_mmu_page, link);
1222                 kvm_mmu_zap_page(vcpu, page);
1223         }
1224         free_page((unsigned long)vcpu->mmu.pae_root);
1225 }
1226
1227 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1228 {
1229         struct page *page;
1230         int i;
1231
1232         ASSERT(vcpu);
1233
1234         vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES;
1235
1236         /*
1237          * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1238          * Therefore we need to allocate shadow page tables in the first
1239          * 4GB of memory, which happens to fit the DMA32 zone.
1240          */
1241         page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1242         if (!page)
1243                 goto error_1;
1244         vcpu->mmu.pae_root = page_address(page);
1245         for (i = 0; i < 4; ++i)
1246                 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1247
1248         return 0;
1249
1250 error_1:
1251         free_mmu_pages(vcpu);
1252         return -ENOMEM;
1253 }
1254
1255 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1256 {
1257         ASSERT(vcpu);
1258         ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1259
1260         return alloc_mmu_pages(vcpu);
1261 }
1262
1263 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1264 {
1265         ASSERT(vcpu);
1266         ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1267
1268         return init_kvm_mmu(vcpu);
1269 }
1270
1271 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1272 {
1273         ASSERT(vcpu);
1274
1275         destroy_kvm_mmu(vcpu);
1276         free_mmu_pages(vcpu);
1277         mmu_free_memory_caches(vcpu);
1278 }
1279
1280 void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)
1281 {
1282         struct kvm *kvm = vcpu->kvm;
1283         struct kvm_mmu_page *page;
1284
1285         list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1286                 int i;
1287                 u64 *pt;
1288
1289                 if (!test_bit(slot, &page->slot_bitmap))
1290                         continue;
1291
1292                 pt = page->spt;
1293                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1294                         /* avoid RMW */
1295                         if (pt[i] & PT_WRITABLE_MASK) {
1296                                 rmap_remove(vcpu, &pt[i]);
1297                                 pt[i] &= ~PT_WRITABLE_MASK;
1298                         }
1299         }
1300 }
1301
1302 void kvm_mmu_zap_all(struct kvm_vcpu *vcpu)
1303 {
1304         destroy_kvm_mmu(vcpu);
1305
1306         while (!list_empty(&vcpu->kvm->active_mmu_pages)) {
1307                 struct kvm_mmu_page *page;
1308
1309                 page = container_of(vcpu->kvm->active_mmu_pages.next,
1310                                     struct kvm_mmu_page, link);
1311                 kvm_mmu_zap_page(vcpu, page);
1312         }
1313
1314         mmu_free_memory_caches(vcpu);
1315         kvm_flush_remote_tlbs(vcpu->kvm);
1316         init_kvm_mmu(vcpu);
1317 }
1318
1319 void kvm_mmu_module_exit(void)
1320 {
1321         if (pte_chain_cache)
1322                 kmem_cache_destroy(pte_chain_cache);
1323         if (rmap_desc_cache)
1324                 kmem_cache_destroy(rmap_desc_cache);
1325         if (mmu_page_cache)
1326                 kmem_cache_destroy(mmu_page_cache);
1327         if (mmu_page_header_cache)
1328                 kmem_cache_destroy(mmu_page_header_cache);
1329 }
1330
1331 int kvm_mmu_module_init(void)
1332 {
1333         pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1334                                             sizeof(struct kvm_pte_chain),
1335                                             0, 0, NULL, NULL);
1336         if (!pte_chain_cache)
1337                 goto nomem;
1338         rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1339                                             sizeof(struct kvm_rmap_desc),
1340                                             0, 0, NULL, NULL);
1341         if (!rmap_desc_cache)
1342                 goto nomem;
1343
1344         mmu_page_cache = kmem_cache_create("kvm_mmu_page",
1345                                            PAGE_SIZE,
1346                                            PAGE_SIZE, 0, NULL, NULL);
1347         if (!mmu_page_cache)
1348                 goto nomem;
1349
1350         mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1351                                                   sizeof(struct kvm_mmu_page),
1352                                                   0, 0, NULL, NULL);
1353         if (!mmu_page_header_cache)
1354                 goto nomem;
1355
1356         return 0;
1357
1358 nomem:
1359         kvm_mmu_module_exit();
1360         return -ENOMEM;
1361 }
1362
1363 #ifdef AUDIT
1364
1365 static const char *audit_msg;
1366
1367 static gva_t canonicalize(gva_t gva)
1368 {
1369 #ifdef CONFIG_X86_64
1370         gva = (long long)(gva << 16) >> 16;
1371 #endif
1372         return gva;
1373 }
1374
1375 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1376                                 gva_t va, int level)
1377 {
1378         u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1379         int i;
1380         gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1381
1382         for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1383                 u64 ent = pt[i];
1384
1385                 if (!(ent & PT_PRESENT_MASK))
1386                         continue;
1387
1388                 va = canonicalize(va);
1389                 if (level > 1)
1390                         audit_mappings_page(vcpu, ent, va, level - 1);
1391                 else {
1392                         gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va);
1393                         hpa_t hpa = gpa_to_hpa(vcpu, gpa);
1394
1395                         if ((ent & PT_PRESENT_MASK)
1396                             && (ent & PT64_BASE_ADDR_MASK) != hpa)
1397                                 printk(KERN_ERR "audit error: (%s) levels %d"
1398                                        " gva %lx gpa %llx hpa %llx ent %llx\n",
1399                                        audit_msg, vcpu->mmu.root_level,
1400                                        va, gpa, hpa, ent);
1401                 }
1402         }
1403 }
1404
1405 static void audit_mappings(struct kvm_vcpu *vcpu)
1406 {
1407         unsigned i;
1408
1409         if (vcpu->mmu.root_level == 4)
1410                 audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4);
1411         else
1412                 for (i = 0; i < 4; ++i)
1413                         if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK)
1414                                 audit_mappings_page(vcpu,
1415                                                     vcpu->mmu.pae_root[i],
1416                                                     i << 30,
1417                                                     2);
1418 }
1419
1420 static int count_rmaps(struct kvm_vcpu *vcpu)
1421 {
1422         int nmaps = 0;
1423         int i, j, k;
1424
1425         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1426                 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1427                 struct kvm_rmap_desc *d;
1428
1429                 for (j = 0; j < m->npages; ++j) {
1430                         struct page *page = m->phys_mem[j];
1431
1432                         if (!page->private)
1433                                 continue;
1434                         if (!(page->private & 1)) {
1435                                 ++nmaps;
1436                                 continue;
1437                         }
1438                         d = (struct kvm_rmap_desc *)(page->private & ~1ul);
1439                         while (d) {
1440                                 for (k = 0; k < RMAP_EXT; ++k)
1441                                         if (d->shadow_ptes[k])
1442                                                 ++nmaps;
1443                                         else
1444                                                 break;
1445                                 d = d->more;
1446                         }
1447                 }
1448         }
1449         return nmaps;
1450 }
1451
1452 static int count_writable_mappings(struct kvm_vcpu *vcpu)
1453 {
1454         int nmaps = 0;
1455         struct kvm_mmu_page *page;
1456         int i;
1457
1458         list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1459                 u64 *pt = page->spt;
1460
1461                 if (page->role.level != PT_PAGE_TABLE_LEVEL)
1462                         continue;
1463
1464                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1465                         u64 ent = pt[i];
1466
1467                         if (!(ent & PT_PRESENT_MASK))
1468                                 continue;
1469                         if (!(ent & PT_WRITABLE_MASK))
1470                                 continue;
1471                         ++nmaps;
1472                 }
1473         }
1474         return nmaps;
1475 }
1476
1477 static void audit_rmap(struct kvm_vcpu *vcpu)
1478 {
1479         int n_rmap = count_rmaps(vcpu);
1480         int n_actual = count_writable_mappings(vcpu);
1481
1482         if (n_rmap != n_actual)
1483                 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1484                        __FUNCTION__, audit_msg, n_rmap, n_actual);
1485 }
1486
1487 static void audit_write_protection(struct kvm_vcpu *vcpu)
1488 {
1489         struct kvm_mmu_page *page;
1490
1491         list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) {
1492                 hfn_t hfn;
1493                 struct page *pg;
1494
1495                 if (page->role.metaphysical)
1496                         continue;
1497
1498                 hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT)
1499                         >> PAGE_SHIFT;
1500                 pg = pfn_to_page(hfn);
1501                 if (pg->private)
1502                         printk(KERN_ERR "%s: (%s) shadow page has writable"
1503                                " mappings: gfn %lx role %x\n",
1504                                __FUNCTION__, audit_msg, page->gfn,
1505                                page->role.word);
1506         }
1507 }
1508
1509 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1510 {
1511         int olddbg = dbg;
1512
1513         dbg = 0;
1514         audit_msg = msg;
1515         audit_rmap(vcpu);
1516         audit_write_protection(vcpu);
1517         audit_mappings(vcpu);
1518         dbg = olddbg;
1519 }
1520
1521 #endif