12974de88aa5c0864bf8ea0e43185aca8621b42f
[linux-2.6.git] / arch / x86 / 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 "mmu.h"
21 #include "kvm_cache_regs.h"
22
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>
30 #include <linux/hugetlb.h>
31 #include <linux/compiler.h>
32
33 #include <asm/page.h>
34 #include <asm/cmpxchg.h>
35 #include <asm/io.h>
36 #include <asm/vmx.h>
37
38 /*
39  * When setting this variable to true it enables Two-Dimensional-Paging
40  * where the hardware walks 2 page tables:
41  * 1. the guest-virtual to guest-physical
42  * 2. while doing 1. it walks guest-physical to host-physical
43  * If the hardware supports that we don't need to do shadow paging.
44  */
45 bool tdp_enabled = false;
46
47 #undef MMU_DEBUG
48
49 #undef AUDIT
50
51 #ifdef AUDIT
52 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
53 #else
54 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
55 #endif
56
57 #ifdef MMU_DEBUG
58
59 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
60 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
61
62 #else
63
64 #define pgprintk(x...) do { } while (0)
65 #define rmap_printk(x...) do { } while (0)
66
67 #endif
68
69 #if defined(MMU_DEBUG) || defined(AUDIT)
70 static int dbg = 0;
71 module_param(dbg, bool, 0644);
72 #endif
73
74 static int oos_shadow = 1;
75 module_param(oos_shadow, bool, 0644);
76
77 #ifndef MMU_DEBUG
78 #define ASSERT(x) do { } while (0)
79 #else
80 #define ASSERT(x)                                                       \
81         if (!(x)) {                                                     \
82                 printk(KERN_WARNING "assertion failed %s:%d: %s\n",     \
83                        __FILE__, __LINE__, #x);                         \
84         }
85 #endif
86
87 #define PT_FIRST_AVAIL_BITS_SHIFT 9
88 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
89
90 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
91
92 #define PT64_LEVEL_BITS 9
93
94 #define PT64_LEVEL_SHIFT(level) \
95                 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
96
97 #define PT64_LEVEL_MASK(level) \
98                 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
99
100 #define PT64_INDEX(address, level)\
101         (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
102
103
104 #define PT32_LEVEL_BITS 10
105
106 #define PT32_LEVEL_SHIFT(level) \
107                 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
108
109 #define PT32_LEVEL_MASK(level) \
110                 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
111
112 #define PT32_INDEX(address, level)\
113         (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
114
115
116 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
117 #define PT64_DIR_BASE_ADDR_MASK \
118         (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
119
120 #define PT32_BASE_ADDR_MASK PAGE_MASK
121 #define PT32_DIR_BASE_ADDR_MASK \
122         (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
123
124 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
125                         | PT64_NX_MASK)
126
127 #define PFERR_PRESENT_MASK (1U << 0)
128 #define PFERR_WRITE_MASK (1U << 1)
129 #define PFERR_USER_MASK (1U << 2)
130 #define PFERR_RSVD_MASK (1U << 3)
131 #define PFERR_FETCH_MASK (1U << 4)
132
133 #define PT_DIRECTORY_LEVEL 2
134 #define PT_PAGE_TABLE_LEVEL 1
135
136 #define RMAP_EXT 4
137
138 #define ACC_EXEC_MASK    1
139 #define ACC_WRITE_MASK   PT_WRITABLE_MASK
140 #define ACC_USER_MASK    PT_USER_MASK
141 #define ACC_ALL          (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
142
143 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
144
145 struct kvm_rmap_desc {
146         u64 *sptes[RMAP_EXT];
147         struct kvm_rmap_desc *more;
148 };
149
150 struct kvm_shadow_walk_iterator {
151         u64 addr;
152         hpa_t shadow_addr;
153         int level;
154         u64 *sptep;
155         unsigned index;
156 };
157
158 #define for_each_shadow_entry(_vcpu, _addr, _walker)    \
159         for (shadow_walk_init(&(_walker), _vcpu, _addr);        \
160              shadow_walk_okay(&(_walker));                      \
161              shadow_walk_next(&(_walker)))
162
163
164 struct kvm_unsync_walk {
165         int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
166 };
167
168 typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
169
170 static struct kmem_cache *pte_chain_cache;
171 static struct kmem_cache *rmap_desc_cache;
172 static struct kmem_cache *mmu_page_header_cache;
173
174 static u64 __read_mostly shadow_trap_nonpresent_pte;
175 static u64 __read_mostly shadow_notrap_nonpresent_pte;
176 static u64 __read_mostly shadow_base_present_pte;
177 static u64 __read_mostly shadow_nx_mask;
178 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
179 static u64 __read_mostly shadow_user_mask;
180 static u64 __read_mostly shadow_accessed_mask;
181 static u64 __read_mostly shadow_dirty_mask;
182
183 static inline u64 rsvd_bits(int s, int e)
184 {
185         return ((1ULL << (e - s + 1)) - 1) << s;
186 }
187
188 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
189 {
190         shadow_trap_nonpresent_pte = trap_pte;
191         shadow_notrap_nonpresent_pte = notrap_pte;
192 }
193 EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
194
195 void kvm_mmu_set_base_ptes(u64 base_pte)
196 {
197         shadow_base_present_pte = base_pte;
198 }
199 EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
200
201 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
202                 u64 dirty_mask, u64 nx_mask, u64 x_mask)
203 {
204         shadow_user_mask = user_mask;
205         shadow_accessed_mask = accessed_mask;
206         shadow_dirty_mask = dirty_mask;
207         shadow_nx_mask = nx_mask;
208         shadow_x_mask = x_mask;
209 }
210 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
211
212 static int is_write_protection(struct kvm_vcpu *vcpu)
213 {
214         return vcpu->arch.cr0 & X86_CR0_WP;
215 }
216
217 static int is_cpuid_PSE36(void)
218 {
219         return 1;
220 }
221
222 static int is_nx(struct kvm_vcpu *vcpu)
223 {
224         return vcpu->arch.shadow_efer & EFER_NX;
225 }
226
227 static int is_shadow_present_pte(u64 pte)
228 {
229         return pte != shadow_trap_nonpresent_pte
230                 && pte != shadow_notrap_nonpresent_pte;
231 }
232
233 static int is_large_pte(u64 pte)
234 {
235         return pte & PT_PAGE_SIZE_MASK;
236 }
237
238 static int is_writeble_pte(unsigned long pte)
239 {
240         return pte & PT_WRITABLE_MASK;
241 }
242
243 static int is_dirty_gpte(unsigned long pte)
244 {
245         return pte & PT_DIRTY_MASK;
246 }
247
248 static int is_rmap_spte(u64 pte)
249 {
250         return is_shadow_present_pte(pte);
251 }
252
253 static int is_last_spte(u64 pte, int level)
254 {
255         if (level == PT_PAGE_TABLE_LEVEL)
256                 return 1;
257         if (level == PT_DIRECTORY_LEVEL && is_large_pte(pte))
258                 return 1;
259         return 0;
260 }
261
262 static pfn_t spte_to_pfn(u64 pte)
263 {
264         return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
265 }
266
267 static gfn_t pse36_gfn_delta(u32 gpte)
268 {
269         int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
270
271         return (gpte & PT32_DIR_PSE36_MASK) << shift;
272 }
273
274 static void __set_spte(u64 *sptep, u64 spte)
275 {
276 #ifdef CONFIG_X86_64
277         set_64bit((unsigned long *)sptep, spte);
278 #else
279         set_64bit((unsigned long long *)sptep, spte);
280 #endif
281 }
282
283 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
284                                   struct kmem_cache *base_cache, int min)
285 {
286         void *obj;
287
288         if (cache->nobjs >= min)
289                 return 0;
290         while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
291                 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
292                 if (!obj)
293                         return -ENOMEM;
294                 cache->objects[cache->nobjs++] = obj;
295         }
296         return 0;
297 }
298
299 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
300 {
301         while (mc->nobjs)
302                 kfree(mc->objects[--mc->nobjs]);
303 }
304
305 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
306                                        int min)
307 {
308         struct page *page;
309
310         if (cache->nobjs >= min)
311                 return 0;
312         while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
313                 page = alloc_page(GFP_KERNEL);
314                 if (!page)
315                         return -ENOMEM;
316                 set_page_private(page, 0);
317                 cache->objects[cache->nobjs++] = page_address(page);
318         }
319         return 0;
320 }
321
322 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
323 {
324         while (mc->nobjs)
325                 free_page((unsigned long)mc->objects[--mc->nobjs]);
326 }
327
328 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
329 {
330         int r;
331
332         r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
333                                    pte_chain_cache, 4);
334         if (r)
335                 goto out;
336         r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
337                                    rmap_desc_cache, 4);
338         if (r)
339                 goto out;
340         r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
341         if (r)
342                 goto out;
343         r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
344                                    mmu_page_header_cache, 4);
345 out:
346         return r;
347 }
348
349 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
350 {
351         mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
352         mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
353         mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
354         mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
355 }
356
357 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
358                                     size_t size)
359 {
360         void *p;
361
362         BUG_ON(!mc->nobjs);
363         p = mc->objects[--mc->nobjs];
364         return p;
365 }
366
367 static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
368 {
369         return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
370                                       sizeof(struct kvm_pte_chain));
371 }
372
373 static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
374 {
375         kfree(pc);
376 }
377
378 static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
379 {
380         return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
381                                       sizeof(struct kvm_rmap_desc));
382 }
383
384 static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
385 {
386         kfree(rd);
387 }
388
389 /*
390  * Return the pointer to the largepage write count for a given
391  * gfn, handling slots that are not large page aligned.
392  */
393 static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
394 {
395         unsigned long idx;
396
397         idx = (gfn / KVM_PAGES_PER_HPAGE) -
398               (slot->base_gfn / KVM_PAGES_PER_HPAGE);
399         return &slot->lpage_info[idx].write_count;
400 }
401
402 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
403 {
404         int *write_count;
405
406         gfn = unalias_gfn(kvm, gfn);
407         write_count = slot_largepage_idx(gfn,
408                                          gfn_to_memslot_unaliased(kvm, gfn));
409         *write_count += 1;
410 }
411
412 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
413 {
414         int *write_count;
415
416         gfn = unalias_gfn(kvm, gfn);
417         write_count = slot_largepage_idx(gfn,
418                                          gfn_to_memslot_unaliased(kvm, gfn));
419         *write_count -= 1;
420         WARN_ON(*write_count < 0);
421 }
422
423 static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
424 {
425         struct kvm_memory_slot *slot;
426         int *largepage_idx;
427
428         gfn = unalias_gfn(kvm, gfn);
429         slot = gfn_to_memslot_unaliased(kvm, gfn);
430         if (slot) {
431                 largepage_idx = slot_largepage_idx(gfn, slot);
432                 return *largepage_idx;
433         }
434
435         return 1;
436 }
437
438 static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
439 {
440         struct vm_area_struct *vma;
441         unsigned long addr;
442         int ret = 0;
443
444         addr = gfn_to_hva(kvm, gfn);
445         if (kvm_is_error_hva(addr))
446                 return ret;
447
448         down_read(&current->mm->mmap_sem);
449         vma = find_vma(current->mm, addr);
450         if (vma && is_vm_hugetlb_page(vma))
451                 ret = 1;
452         up_read(&current->mm->mmap_sem);
453
454         return ret;
455 }
456
457 static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
458 {
459         struct kvm_memory_slot *slot;
460
461         if (has_wrprotected_page(vcpu->kvm, large_gfn))
462                 return 0;
463
464         if (!host_largepage_backed(vcpu->kvm, large_gfn))
465                 return 0;
466
467         slot = gfn_to_memslot(vcpu->kvm, large_gfn);
468         if (slot && slot->dirty_bitmap)
469                 return 0;
470
471         return 1;
472 }
473
474 /*
475  * Take gfn and return the reverse mapping to it.
476  * Note: gfn must be unaliased before this function get called
477  */
478
479 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
480 {
481         struct kvm_memory_slot *slot;
482         unsigned long idx;
483
484         slot = gfn_to_memslot(kvm, gfn);
485         if (!lpage)
486                 return &slot->rmap[gfn - slot->base_gfn];
487
488         idx = (gfn / KVM_PAGES_PER_HPAGE) -
489               (slot->base_gfn / KVM_PAGES_PER_HPAGE);
490
491         return &slot->lpage_info[idx].rmap_pde;
492 }
493
494 /*
495  * Reverse mapping data structures:
496  *
497  * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
498  * that points to page_address(page).
499  *
500  * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
501  * containing more mappings.
502  *
503  * Returns the number of rmap entries before the spte was added or zero if
504  * the spte was not added.
505  *
506  */
507 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
508 {
509         struct kvm_mmu_page *sp;
510         struct kvm_rmap_desc *desc;
511         unsigned long *rmapp;
512         int i, count = 0;
513
514         if (!is_rmap_spte(*spte))
515                 return count;
516         gfn = unalias_gfn(vcpu->kvm, gfn);
517         sp = page_header(__pa(spte));
518         sp->gfns[spte - sp->spt] = gfn;
519         rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
520         if (!*rmapp) {
521                 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
522                 *rmapp = (unsigned long)spte;
523         } else if (!(*rmapp & 1)) {
524                 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
525                 desc = mmu_alloc_rmap_desc(vcpu);
526                 desc->sptes[0] = (u64 *)*rmapp;
527                 desc->sptes[1] = spte;
528                 *rmapp = (unsigned long)desc | 1;
529         } else {
530                 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
531                 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
532                 while (desc->sptes[RMAP_EXT-1] && desc->more) {
533                         desc = desc->more;
534                         count += RMAP_EXT;
535                 }
536                 if (desc->sptes[RMAP_EXT-1]) {
537                         desc->more = mmu_alloc_rmap_desc(vcpu);
538                         desc = desc->more;
539                 }
540                 for (i = 0; desc->sptes[i]; ++i)
541                         ;
542                 desc->sptes[i] = spte;
543         }
544         return count;
545 }
546
547 static void rmap_desc_remove_entry(unsigned long *rmapp,
548                                    struct kvm_rmap_desc *desc,
549                                    int i,
550                                    struct kvm_rmap_desc *prev_desc)
551 {
552         int j;
553
554         for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
555                 ;
556         desc->sptes[i] = desc->sptes[j];
557         desc->sptes[j] = NULL;
558         if (j != 0)
559                 return;
560         if (!prev_desc && !desc->more)
561                 *rmapp = (unsigned long)desc->sptes[0];
562         else
563                 if (prev_desc)
564                         prev_desc->more = desc->more;
565                 else
566                         *rmapp = (unsigned long)desc->more | 1;
567         mmu_free_rmap_desc(desc);
568 }
569
570 static void rmap_remove(struct kvm *kvm, u64 *spte)
571 {
572         struct kvm_rmap_desc *desc;
573         struct kvm_rmap_desc *prev_desc;
574         struct kvm_mmu_page *sp;
575         pfn_t pfn;
576         unsigned long *rmapp;
577         int i;
578
579         if (!is_rmap_spte(*spte))
580                 return;
581         sp = page_header(__pa(spte));
582         pfn = spte_to_pfn(*spte);
583         if (*spte & shadow_accessed_mask)
584                 kvm_set_pfn_accessed(pfn);
585         if (is_writeble_pte(*spte))
586                 kvm_release_pfn_dirty(pfn);
587         else
588                 kvm_release_pfn_clean(pfn);
589         rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
590         if (!*rmapp) {
591                 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
592                 BUG();
593         } else if (!(*rmapp & 1)) {
594                 rmap_printk("rmap_remove:  %p %llx 1->0\n", spte, *spte);
595                 if ((u64 *)*rmapp != spte) {
596                         printk(KERN_ERR "rmap_remove:  %p %llx 1->BUG\n",
597                                spte, *spte);
598                         BUG();
599                 }
600                 *rmapp = 0;
601         } else {
602                 rmap_printk("rmap_remove:  %p %llx many->many\n", spte, *spte);
603                 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
604                 prev_desc = NULL;
605                 while (desc) {
606                         for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
607                                 if (desc->sptes[i] == spte) {
608                                         rmap_desc_remove_entry(rmapp,
609                                                                desc, i,
610                                                                prev_desc);
611                                         return;
612                                 }
613                         prev_desc = desc;
614                         desc = desc->more;
615                 }
616                 BUG();
617         }
618 }
619
620 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
621 {
622         struct kvm_rmap_desc *desc;
623         struct kvm_rmap_desc *prev_desc;
624         u64 *prev_spte;
625         int i;
626
627         if (!*rmapp)
628                 return NULL;
629         else if (!(*rmapp & 1)) {
630                 if (!spte)
631                         return (u64 *)*rmapp;
632                 return NULL;
633         }
634         desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
635         prev_desc = NULL;
636         prev_spte = NULL;
637         while (desc) {
638                 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
639                         if (prev_spte == spte)
640                                 return desc->sptes[i];
641                         prev_spte = desc->sptes[i];
642                 }
643                 desc = desc->more;
644         }
645         return NULL;
646 }
647
648 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
649 {
650         unsigned long *rmapp;
651         u64 *spte;
652         int write_protected = 0;
653
654         gfn = unalias_gfn(kvm, gfn);
655         rmapp = gfn_to_rmap(kvm, gfn, 0);
656
657         spte = rmap_next(kvm, rmapp, NULL);
658         while (spte) {
659                 BUG_ON(!spte);
660                 BUG_ON(!(*spte & PT_PRESENT_MASK));
661                 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
662                 if (is_writeble_pte(*spte)) {
663                         __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
664                         write_protected = 1;
665                 }
666                 spte = rmap_next(kvm, rmapp, spte);
667         }
668         if (write_protected) {
669                 pfn_t pfn;
670
671                 spte = rmap_next(kvm, rmapp, NULL);
672                 pfn = spte_to_pfn(*spte);
673                 kvm_set_pfn_dirty(pfn);
674         }
675
676         /* check for huge page mappings */
677         rmapp = gfn_to_rmap(kvm, gfn, 1);
678         spte = rmap_next(kvm, rmapp, NULL);
679         while (spte) {
680                 BUG_ON(!spte);
681                 BUG_ON(!(*spte & PT_PRESENT_MASK));
682                 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
683                 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
684                 if (is_writeble_pte(*spte)) {
685                         rmap_remove(kvm, spte);
686                         --kvm->stat.lpages;
687                         __set_spte(spte, shadow_trap_nonpresent_pte);
688                         spte = NULL;
689                         write_protected = 1;
690                 }
691                 spte = rmap_next(kvm, rmapp, spte);
692         }
693
694         return write_protected;
695 }
696
697 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
698 {
699         u64 *spte;
700         int need_tlb_flush = 0;
701
702         while ((spte = rmap_next(kvm, rmapp, NULL))) {
703                 BUG_ON(!(*spte & PT_PRESENT_MASK));
704                 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
705                 rmap_remove(kvm, spte);
706                 __set_spte(spte, shadow_trap_nonpresent_pte);
707                 need_tlb_flush = 1;
708         }
709         return need_tlb_flush;
710 }
711
712 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
713                           int (*handler)(struct kvm *kvm, unsigned long *rmapp))
714 {
715         int i;
716         int retval = 0;
717
718         /*
719          * If mmap_sem isn't taken, we can look the memslots with only
720          * the mmu_lock by skipping over the slots with userspace_addr == 0.
721          */
722         for (i = 0; i < kvm->nmemslots; i++) {
723                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
724                 unsigned long start = memslot->userspace_addr;
725                 unsigned long end;
726
727                 /* mmu_lock protects userspace_addr */
728                 if (!start)
729                         continue;
730
731                 end = start + (memslot->npages << PAGE_SHIFT);
732                 if (hva >= start && hva < end) {
733                         gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
734                         retval |= handler(kvm, &memslot->rmap[gfn_offset]);
735                         retval |= handler(kvm,
736                                           &memslot->lpage_info[
737                                                   gfn_offset /
738                                                   KVM_PAGES_PER_HPAGE].rmap_pde);
739                 }
740         }
741
742         return retval;
743 }
744
745 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
746 {
747         return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
748 }
749
750 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
751 {
752         u64 *spte;
753         int young = 0;
754
755         /* always return old for EPT */
756         if (!shadow_accessed_mask)
757                 return 0;
758
759         spte = rmap_next(kvm, rmapp, NULL);
760         while (spte) {
761                 int _young;
762                 u64 _spte = *spte;
763                 BUG_ON(!(_spte & PT_PRESENT_MASK));
764                 _young = _spte & PT_ACCESSED_MASK;
765                 if (_young) {
766                         young = 1;
767                         clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
768                 }
769                 spte = rmap_next(kvm, rmapp, spte);
770         }
771         return young;
772 }
773
774 #define RMAP_RECYCLE_THRESHOLD 1000
775
776 static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
777 {
778         unsigned long *rmapp;
779
780         gfn = unalias_gfn(vcpu->kvm, gfn);
781         rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
782
783         kvm_unmap_rmapp(vcpu->kvm, rmapp);
784         kvm_flush_remote_tlbs(vcpu->kvm);
785 }
786
787 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
788 {
789         return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
790 }
791
792 #ifdef MMU_DEBUG
793 static int is_empty_shadow_page(u64 *spt)
794 {
795         u64 *pos;
796         u64 *end;
797
798         for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
799                 if (is_shadow_present_pte(*pos)) {
800                         printk(KERN_ERR "%s: %p %llx\n", __func__,
801                                pos, *pos);
802                         return 0;
803                 }
804         return 1;
805 }
806 #endif
807
808 static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
809 {
810         ASSERT(is_empty_shadow_page(sp->spt));
811         list_del(&sp->link);
812         __free_page(virt_to_page(sp->spt));
813         __free_page(virt_to_page(sp->gfns));
814         kfree(sp);
815         ++kvm->arch.n_free_mmu_pages;
816 }
817
818 static unsigned kvm_page_table_hashfn(gfn_t gfn)
819 {
820         return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
821 }
822
823 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
824                                                u64 *parent_pte)
825 {
826         struct kvm_mmu_page *sp;
827
828         sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
829         sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
830         sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
831         set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
832         list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
833         INIT_LIST_HEAD(&sp->oos_link);
834         bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
835         sp->multimapped = 0;
836         sp->parent_pte = parent_pte;
837         --vcpu->kvm->arch.n_free_mmu_pages;
838         return sp;
839 }
840
841 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
842                                     struct kvm_mmu_page *sp, u64 *parent_pte)
843 {
844         struct kvm_pte_chain *pte_chain;
845         struct hlist_node *node;
846         int i;
847
848         if (!parent_pte)
849                 return;
850         if (!sp->multimapped) {
851                 u64 *old = sp->parent_pte;
852
853                 if (!old) {
854                         sp->parent_pte = parent_pte;
855                         return;
856                 }
857                 sp->multimapped = 1;
858                 pte_chain = mmu_alloc_pte_chain(vcpu);
859                 INIT_HLIST_HEAD(&sp->parent_ptes);
860                 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
861                 pte_chain->parent_ptes[0] = old;
862         }
863         hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
864                 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
865                         continue;
866                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
867                         if (!pte_chain->parent_ptes[i]) {
868                                 pte_chain->parent_ptes[i] = parent_pte;
869                                 return;
870                         }
871         }
872         pte_chain = mmu_alloc_pte_chain(vcpu);
873         BUG_ON(!pte_chain);
874         hlist_add_head(&pte_chain->link, &sp->parent_ptes);
875         pte_chain->parent_ptes[0] = parent_pte;
876 }
877
878 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
879                                        u64 *parent_pte)
880 {
881         struct kvm_pte_chain *pte_chain;
882         struct hlist_node *node;
883         int i;
884
885         if (!sp->multimapped) {
886                 BUG_ON(sp->parent_pte != parent_pte);
887                 sp->parent_pte = NULL;
888                 return;
889         }
890         hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
891                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
892                         if (!pte_chain->parent_ptes[i])
893                                 break;
894                         if (pte_chain->parent_ptes[i] != parent_pte)
895                                 continue;
896                         while (i + 1 < NR_PTE_CHAIN_ENTRIES
897                                 && pte_chain->parent_ptes[i + 1]) {
898                                 pte_chain->parent_ptes[i]
899                                         = pte_chain->parent_ptes[i + 1];
900                                 ++i;
901                         }
902                         pte_chain->parent_ptes[i] = NULL;
903                         if (i == 0) {
904                                 hlist_del(&pte_chain->link);
905                                 mmu_free_pte_chain(pte_chain);
906                                 if (hlist_empty(&sp->parent_ptes)) {
907                                         sp->multimapped = 0;
908                                         sp->parent_pte = NULL;
909                                 }
910                         }
911                         return;
912                 }
913         BUG();
914 }
915
916
917 static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
918                             mmu_parent_walk_fn fn)
919 {
920         struct kvm_pte_chain *pte_chain;
921         struct hlist_node *node;
922         struct kvm_mmu_page *parent_sp;
923         int i;
924
925         if (!sp->multimapped && sp->parent_pte) {
926                 parent_sp = page_header(__pa(sp->parent_pte));
927                 fn(vcpu, parent_sp);
928                 mmu_parent_walk(vcpu, parent_sp, fn);
929                 return;
930         }
931         hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
932                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
933                         if (!pte_chain->parent_ptes[i])
934                                 break;
935                         parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
936                         fn(vcpu, parent_sp);
937                         mmu_parent_walk(vcpu, parent_sp, fn);
938                 }
939 }
940
941 static void kvm_mmu_update_unsync_bitmap(u64 *spte)
942 {
943         unsigned int index;
944         struct kvm_mmu_page *sp = page_header(__pa(spte));
945
946         index = spte - sp->spt;
947         if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
948                 sp->unsync_children++;
949         WARN_ON(!sp->unsync_children);
950 }
951
952 static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
953 {
954         struct kvm_pte_chain *pte_chain;
955         struct hlist_node *node;
956         int i;
957
958         if (!sp->parent_pte)
959                 return;
960
961         if (!sp->multimapped) {
962                 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
963                 return;
964         }
965
966         hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
967                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
968                         if (!pte_chain->parent_ptes[i])
969                                 break;
970                         kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
971                 }
972 }
973
974 static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
975 {
976         kvm_mmu_update_parents_unsync(sp);
977         return 1;
978 }
979
980 static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
981                                         struct kvm_mmu_page *sp)
982 {
983         mmu_parent_walk(vcpu, sp, unsync_walk_fn);
984         kvm_mmu_update_parents_unsync(sp);
985 }
986
987 static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
988                                     struct kvm_mmu_page *sp)
989 {
990         int i;
991
992         for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
993                 sp->spt[i] = shadow_trap_nonpresent_pte;
994 }
995
996 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
997                                struct kvm_mmu_page *sp)
998 {
999         return 1;
1000 }
1001
1002 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1003 {
1004 }
1005
1006 #define KVM_PAGE_ARRAY_NR 16
1007
1008 struct kvm_mmu_pages {
1009         struct mmu_page_and_offset {
1010                 struct kvm_mmu_page *sp;
1011                 unsigned int idx;
1012         } page[KVM_PAGE_ARRAY_NR];
1013         unsigned int nr;
1014 };
1015
1016 #define for_each_unsync_children(bitmap, idx)           \
1017         for (idx = find_first_bit(bitmap, 512);         \
1018              idx < 512;                                 \
1019              idx = find_next_bit(bitmap, 512, idx+1))
1020
1021 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1022                          int idx)
1023 {
1024         int i;
1025
1026         if (sp->unsync)
1027                 for (i=0; i < pvec->nr; i++)
1028                         if (pvec->page[i].sp == sp)
1029                                 return 0;
1030
1031         pvec->page[pvec->nr].sp = sp;
1032         pvec->page[pvec->nr].idx = idx;
1033         pvec->nr++;
1034         return (pvec->nr == KVM_PAGE_ARRAY_NR);
1035 }
1036
1037 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1038                            struct kvm_mmu_pages *pvec)
1039 {
1040         int i, ret, nr_unsync_leaf = 0;
1041
1042         for_each_unsync_children(sp->unsync_child_bitmap, i) {
1043                 u64 ent = sp->spt[i];
1044
1045                 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
1046                         struct kvm_mmu_page *child;
1047                         child = page_header(ent & PT64_BASE_ADDR_MASK);
1048
1049                         if (child->unsync_children) {
1050                                 if (mmu_pages_add(pvec, child, i))
1051                                         return -ENOSPC;
1052
1053                                 ret = __mmu_unsync_walk(child, pvec);
1054                                 if (!ret)
1055                                         __clear_bit(i, sp->unsync_child_bitmap);
1056                                 else if (ret > 0)
1057                                         nr_unsync_leaf += ret;
1058                                 else
1059                                         return ret;
1060                         }
1061
1062                         if (child->unsync) {
1063                                 nr_unsync_leaf++;
1064                                 if (mmu_pages_add(pvec, child, i))
1065                                         return -ENOSPC;
1066                         }
1067                 }
1068         }
1069
1070         if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
1071                 sp->unsync_children = 0;
1072
1073         return nr_unsync_leaf;
1074 }
1075
1076 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1077                            struct kvm_mmu_pages *pvec)
1078 {
1079         if (!sp->unsync_children)
1080                 return 0;
1081
1082         mmu_pages_add(pvec, sp, 0);
1083         return __mmu_unsync_walk(sp, pvec);
1084 }
1085
1086 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
1087 {
1088         unsigned index;
1089         struct hlist_head *bucket;
1090         struct kvm_mmu_page *sp;
1091         struct hlist_node *node;
1092
1093         pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1094         index = kvm_page_table_hashfn(gfn);
1095         bucket = &kvm->arch.mmu_page_hash[index];
1096         hlist_for_each_entry(sp, node, bucket, hash_link)
1097                 if (sp->gfn == gfn && !sp->role.direct
1098                     && !sp->role.invalid) {
1099                         pgprintk("%s: found role %x\n",
1100                                  __func__, sp->role.word);
1101                         return sp;
1102                 }
1103         return NULL;
1104 }
1105
1106 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1107 {
1108         WARN_ON(!sp->unsync);
1109         sp->unsync = 0;
1110         --kvm->stat.mmu_unsync;
1111 }
1112
1113 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1114
1115 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1116 {
1117         if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1118                 kvm_mmu_zap_page(vcpu->kvm, sp);
1119                 return 1;
1120         }
1121
1122         if (rmap_write_protect(vcpu->kvm, sp->gfn))
1123                 kvm_flush_remote_tlbs(vcpu->kvm);
1124         kvm_unlink_unsync_page(vcpu->kvm, sp);
1125         if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1126                 kvm_mmu_zap_page(vcpu->kvm, sp);
1127                 return 1;
1128         }
1129
1130         kvm_mmu_flush_tlb(vcpu);
1131         return 0;
1132 }
1133
1134 struct mmu_page_path {
1135         struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1136         unsigned int idx[PT64_ROOT_LEVEL-1];
1137 };
1138
1139 #define for_each_sp(pvec, sp, parents, i)                       \
1140                 for (i = mmu_pages_next(&pvec, &parents, -1),   \
1141                         sp = pvec.page[i].sp;                   \
1142                         i < pvec.nr && ({ sp = pvec.page[i].sp; 1;});   \
1143                         i = mmu_pages_next(&pvec, &parents, i))
1144
1145 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1146                           struct mmu_page_path *parents,
1147                           int i)
1148 {
1149         int n;
1150
1151         for (n = i+1; n < pvec->nr; n++) {
1152                 struct kvm_mmu_page *sp = pvec->page[n].sp;
1153
1154                 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1155                         parents->idx[0] = pvec->page[n].idx;
1156                         return n;
1157                 }
1158
1159                 parents->parent[sp->role.level-2] = sp;
1160                 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1161         }
1162
1163         return n;
1164 }
1165
1166 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1167 {
1168         struct kvm_mmu_page *sp;
1169         unsigned int level = 0;
1170
1171         do {
1172                 unsigned int idx = parents->idx[level];
1173
1174                 sp = parents->parent[level];
1175                 if (!sp)
1176                         return;
1177
1178                 --sp->unsync_children;
1179                 WARN_ON((int)sp->unsync_children < 0);
1180                 __clear_bit(idx, sp->unsync_child_bitmap);
1181                 level++;
1182         } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1183 }
1184
1185 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1186                                struct mmu_page_path *parents,
1187                                struct kvm_mmu_pages *pvec)
1188 {
1189         parents->parent[parent->role.level-1] = NULL;
1190         pvec->nr = 0;
1191 }
1192
1193 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1194                               struct kvm_mmu_page *parent)
1195 {
1196         int i;
1197         struct kvm_mmu_page *sp;
1198         struct mmu_page_path parents;
1199         struct kvm_mmu_pages pages;
1200
1201         kvm_mmu_pages_init(parent, &parents, &pages);
1202         while (mmu_unsync_walk(parent, &pages)) {
1203                 int protected = 0;
1204
1205                 for_each_sp(pages, sp, parents, i)
1206                         protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1207
1208                 if (protected)
1209                         kvm_flush_remote_tlbs(vcpu->kvm);
1210
1211                 for_each_sp(pages, sp, parents, i) {
1212                         kvm_sync_page(vcpu, sp);
1213                         mmu_pages_clear_parents(&parents);
1214                 }
1215                 cond_resched_lock(&vcpu->kvm->mmu_lock);
1216                 kvm_mmu_pages_init(parent, &parents, &pages);
1217         }
1218 }
1219
1220 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1221                                              gfn_t gfn,
1222                                              gva_t gaddr,
1223                                              unsigned level,
1224                                              int direct,
1225                                              unsigned access,
1226                                              u64 *parent_pte)
1227 {
1228         union kvm_mmu_page_role role;
1229         unsigned index;
1230         unsigned quadrant;
1231         struct hlist_head *bucket;
1232         struct kvm_mmu_page *sp;
1233         struct hlist_node *node, *tmp;
1234
1235         role = vcpu->arch.mmu.base_role;
1236         role.level = level;
1237         role.direct = direct;
1238         role.access = access;
1239         if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1240                 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1241                 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1242                 role.quadrant = quadrant;
1243         }
1244         pgprintk("%s: looking gfn %lx role %x\n", __func__,
1245                  gfn, role.word);
1246         index = kvm_page_table_hashfn(gfn);
1247         bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1248         hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1249                 if (sp->gfn == gfn) {
1250                         if (sp->unsync)
1251                                 if (kvm_sync_page(vcpu, sp))
1252                                         continue;
1253
1254                         if (sp->role.word != role.word)
1255                                 continue;
1256
1257                         mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1258                         if (sp->unsync_children) {
1259                                 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1260                                 kvm_mmu_mark_parents_unsync(vcpu, sp);
1261                         }
1262                         pgprintk("%s: found\n", __func__);
1263                         return sp;
1264                 }
1265         ++vcpu->kvm->stat.mmu_cache_miss;
1266         sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1267         if (!sp)
1268                 return sp;
1269         pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
1270         sp->gfn = gfn;
1271         sp->role = role;
1272         hlist_add_head(&sp->hash_link, bucket);
1273         if (!direct) {
1274                 if (rmap_write_protect(vcpu->kvm, gfn))
1275                         kvm_flush_remote_tlbs(vcpu->kvm);
1276                 account_shadowed(vcpu->kvm, gfn);
1277         }
1278         if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1279                 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1280         else
1281                 nonpaging_prefetch_page(vcpu, sp);
1282         return sp;
1283 }
1284
1285 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1286                              struct kvm_vcpu *vcpu, u64 addr)
1287 {
1288         iterator->addr = addr;
1289         iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1290         iterator->level = vcpu->arch.mmu.shadow_root_level;
1291         if (iterator->level == PT32E_ROOT_LEVEL) {
1292                 iterator->shadow_addr
1293                         = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1294                 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1295                 --iterator->level;
1296                 if (!iterator->shadow_addr)
1297                         iterator->level = 0;
1298         }
1299 }
1300
1301 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1302 {
1303         if (iterator->level < PT_PAGE_TABLE_LEVEL)
1304                 return false;
1305
1306         if (iterator->level == PT_PAGE_TABLE_LEVEL)
1307                 if (is_large_pte(*iterator->sptep))
1308                         return false;
1309
1310         iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1311         iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1312         return true;
1313 }
1314
1315 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1316 {
1317         iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1318         --iterator->level;
1319 }
1320
1321 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1322                                          struct kvm_mmu_page *sp)
1323 {
1324         unsigned i;
1325         u64 *pt;
1326         u64 ent;
1327
1328         pt = sp->spt;
1329
1330         for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1331                 ent = pt[i];
1332
1333                 if (is_shadow_present_pte(ent)) {
1334                         if (!is_last_spte(ent, sp->role.level)) {
1335                                 ent &= PT64_BASE_ADDR_MASK;
1336                                 mmu_page_remove_parent_pte(page_header(ent),
1337                                                            &pt[i]);
1338                         } else {
1339                                 if (is_large_pte(ent))
1340                                         --kvm->stat.lpages;
1341                                 rmap_remove(kvm, &pt[i]);
1342                         }
1343                 }
1344                 pt[i] = shadow_trap_nonpresent_pte;
1345         }
1346 }
1347
1348 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1349 {
1350         mmu_page_remove_parent_pte(sp, parent_pte);
1351 }
1352
1353 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1354 {
1355         int i;
1356         struct kvm_vcpu *vcpu;
1357
1358         kvm_for_each_vcpu(i, vcpu, kvm)
1359                 vcpu->arch.last_pte_updated = NULL;
1360 }
1361
1362 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1363 {
1364         u64 *parent_pte;
1365
1366         while (sp->multimapped || sp->parent_pte) {
1367                 if (!sp->multimapped)
1368                         parent_pte = sp->parent_pte;
1369                 else {
1370                         struct kvm_pte_chain *chain;
1371
1372                         chain = container_of(sp->parent_ptes.first,
1373                                              struct kvm_pte_chain, link);
1374                         parent_pte = chain->parent_ptes[0];
1375                 }
1376                 BUG_ON(!parent_pte);
1377                 kvm_mmu_put_page(sp, parent_pte);
1378                 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1379         }
1380 }
1381
1382 static int mmu_zap_unsync_children(struct kvm *kvm,
1383                                    struct kvm_mmu_page *parent)
1384 {
1385         int i, zapped = 0;
1386         struct mmu_page_path parents;
1387         struct kvm_mmu_pages pages;
1388
1389         if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1390                 return 0;
1391
1392         kvm_mmu_pages_init(parent, &parents, &pages);
1393         while (mmu_unsync_walk(parent, &pages)) {
1394                 struct kvm_mmu_page *sp;
1395
1396                 for_each_sp(pages, sp, parents, i) {
1397                         kvm_mmu_zap_page(kvm, sp);
1398                         mmu_pages_clear_parents(&parents);
1399                 }
1400                 zapped += pages.nr;
1401                 kvm_mmu_pages_init(parent, &parents, &pages);
1402         }
1403
1404         return zapped;
1405 }
1406
1407 static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1408 {
1409         int ret;
1410         ++kvm->stat.mmu_shadow_zapped;
1411         ret = mmu_zap_unsync_children(kvm, sp);
1412         kvm_mmu_page_unlink_children(kvm, sp);
1413         kvm_mmu_unlink_parents(kvm, sp);
1414         kvm_flush_remote_tlbs(kvm);
1415         if (!sp->role.invalid && !sp->role.direct)
1416                 unaccount_shadowed(kvm, sp->gfn);
1417         if (sp->unsync)
1418                 kvm_unlink_unsync_page(kvm, sp);
1419         if (!sp->root_count) {
1420                 hlist_del(&sp->hash_link);
1421                 kvm_mmu_free_page(kvm, sp);
1422         } else {
1423                 sp->role.invalid = 1;
1424                 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1425                 kvm_reload_remote_mmus(kvm);
1426         }
1427         kvm_mmu_reset_last_pte_updated(kvm);
1428         return ret;
1429 }
1430
1431 /*
1432  * Changing the number of mmu pages allocated to the vm
1433  * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1434  */
1435 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1436 {
1437         int used_pages;
1438
1439         used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1440         used_pages = max(0, used_pages);
1441
1442         /*
1443          * If we set the number of mmu pages to be smaller be than the
1444          * number of actived pages , we must to free some mmu pages before we
1445          * change the value
1446          */
1447
1448         if (used_pages > kvm_nr_mmu_pages) {
1449                 while (used_pages > kvm_nr_mmu_pages) {
1450                         struct kvm_mmu_page *page;
1451
1452                         page = container_of(kvm->arch.active_mmu_pages.prev,
1453                                             struct kvm_mmu_page, link);
1454                         kvm_mmu_zap_page(kvm, page);
1455                         used_pages--;
1456                 }
1457                 kvm->arch.n_free_mmu_pages = 0;
1458         }
1459         else
1460                 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1461                                          - kvm->arch.n_alloc_mmu_pages;
1462
1463         kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
1464 }
1465
1466 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1467 {
1468         unsigned index;
1469         struct hlist_head *bucket;
1470         struct kvm_mmu_page *sp;
1471         struct hlist_node *node, *n;
1472         int r;
1473
1474         pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
1475         r = 0;
1476         index = kvm_page_table_hashfn(gfn);
1477         bucket = &kvm->arch.mmu_page_hash[index];
1478         hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1479                 if (sp->gfn == gfn && !sp->role.direct) {
1480                         pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
1481                                  sp->role.word);
1482                         r = 1;
1483                         if (kvm_mmu_zap_page(kvm, sp))
1484                                 n = bucket->first;
1485                 }
1486         return r;
1487 }
1488
1489 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1490 {
1491         unsigned index;
1492         struct hlist_head *bucket;
1493         struct kvm_mmu_page *sp;
1494         struct hlist_node *node, *nn;
1495
1496         index = kvm_page_table_hashfn(gfn);
1497         bucket = &kvm->arch.mmu_page_hash[index];
1498         hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
1499                 if (sp->gfn == gfn && !sp->role.direct
1500                     && !sp->role.invalid) {
1501                         pgprintk("%s: zap %lx %x\n",
1502                                  __func__, gfn, sp->role.word);
1503                         kvm_mmu_zap_page(kvm, sp);
1504                 }
1505         }
1506 }
1507
1508 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1509 {
1510         int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
1511         struct kvm_mmu_page *sp = page_header(__pa(pte));
1512
1513         __set_bit(slot, sp->slot_bitmap);
1514 }
1515
1516 static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1517 {
1518         int i;
1519         u64 *pt = sp->spt;
1520
1521         if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1522                 return;
1523
1524         for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1525                 if (pt[i] == shadow_notrap_nonpresent_pte)
1526                         __set_spte(&pt[i], shadow_trap_nonpresent_pte);
1527         }
1528 }
1529
1530 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1531 {
1532         struct page *page;
1533
1534         gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1535
1536         if (gpa == UNMAPPED_GVA)
1537                 return NULL;
1538
1539         page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1540
1541         return page;
1542 }
1543
1544 /*
1545  * The function is based on mtrr_type_lookup() in
1546  * arch/x86/kernel/cpu/mtrr/generic.c
1547  */
1548 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1549                          u64 start, u64 end)
1550 {
1551         int i;
1552         u64 base, mask;
1553         u8 prev_match, curr_match;
1554         int num_var_ranges = KVM_NR_VAR_MTRR;
1555
1556         if (!mtrr_state->enabled)
1557                 return 0xFF;
1558
1559         /* Make end inclusive end, instead of exclusive */
1560         end--;
1561
1562         /* Look in fixed ranges. Just return the type as per start */
1563         if (mtrr_state->have_fixed && (start < 0x100000)) {
1564                 int idx;
1565
1566                 if (start < 0x80000) {
1567                         idx = 0;
1568                         idx += (start >> 16);
1569                         return mtrr_state->fixed_ranges[idx];
1570                 } else if (start < 0xC0000) {
1571                         idx = 1 * 8;
1572                         idx += ((start - 0x80000) >> 14);
1573                         return mtrr_state->fixed_ranges[idx];
1574                 } else if (start < 0x1000000) {
1575                         idx = 3 * 8;
1576                         idx += ((start - 0xC0000) >> 12);
1577                         return mtrr_state->fixed_ranges[idx];
1578                 }
1579         }
1580
1581         /*
1582          * Look in variable ranges
1583          * Look of multiple ranges matching this address and pick type
1584          * as per MTRR precedence
1585          */
1586         if (!(mtrr_state->enabled & 2))
1587                 return mtrr_state->def_type;
1588
1589         prev_match = 0xFF;
1590         for (i = 0; i < num_var_ranges; ++i) {
1591                 unsigned short start_state, end_state;
1592
1593                 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1594                         continue;
1595
1596                 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1597                        (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1598                 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1599                        (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1600
1601                 start_state = ((start & mask) == (base & mask));
1602                 end_state = ((end & mask) == (base & mask));
1603                 if (start_state != end_state)
1604                         return 0xFE;
1605
1606                 if ((start & mask) != (base & mask))
1607                         continue;
1608
1609                 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1610                 if (prev_match == 0xFF) {
1611                         prev_match = curr_match;
1612                         continue;
1613                 }
1614
1615                 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1616                     curr_match == MTRR_TYPE_UNCACHABLE)
1617                         return MTRR_TYPE_UNCACHABLE;
1618
1619                 if ((prev_match == MTRR_TYPE_WRBACK &&
1620                      curr_match == MTRR_TYPE_WRTHROUGH) ||
1621                     (prev_match == MTRR_TYPE_WRTHROUGH &&
1622                      curr_match == MTRR_TYPE_WRBACK)) {
1623                         prev_match = MTRR_TYPE_WRTHROUGH;
1624                         curr_match = MTRR_TYPE_WRTHROUGH;
1625                 }
1626
1627                 if (prev_match != curr_match)
1628                         return MTRR_TYPE_UNCACHABLE;
1629         }
1630
1631         if (prev_match != 0xFF)
1632                 return prev_match;
1633
1634         return mtrr_state->def_type;
1635 }
1636
1637 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1638 {
1639         u8 mtrr;
1640
1641         mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1642                              (gfn << PAGE_SHIFT) + PAGE_SIZE);
1643         if (mtrr == 0xfe || mtrr == 0xff)
1644                 mtrr = MTRR_TYPE_WRBACK;
1645         return mtrr;
1646 }
1647 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1648
1649 static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1650 {
1651         unsigned index;
1652         struct hlist_head *bucket;
1653         struct kvm_mmu_page *s;
1654         struct hlist_node *node, *n;
1655
1656         index = kvm_page_table_hashfn(sp->gfn);
1657         bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1658         /* don't unsync if pagetable is shadowed with multiple roles */
1659         hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
1660                 if (s->gfn != sp->gfn || s->role.direct)
1661                         continue;
1662                 if (s->role.word != sp->role.word)
1663                         return 1;
1664         }
1665         ++vcpu->kvm->stat.mmu_unsync;
1666         sp->unsync = 1;
1667
1668         kvm_mmu_mark_parents_unsync(vcpu, sp);
1669
1670         mmu_convert_notrap(sp);
1671         return 0;
1672 }
1673
1674 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1675                                   bool can_unsync)
1676 {
1677         struct kvm_mmu_page *shadow;
1678
1679         shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1680         if (shadow) {
1681                 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1682                         return 1;
1683                 if (shadow->unsync)
1684                         return 0;
1685                 if (can_unsync && oos_shadow)
1686                         return kvm_unsync_page(vcpu, shadow);
1687                 return 1;
1688         }
1689         return 0;
1690 }
1691
1692 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1693                     unsigned pte_access, int user_fault,
1694                     int write_fault, int dirty, int largepage,
1695                     gfn_t gfn, pfn_t pfn, bool speculative,
1696                     bool can_unsync)
1697 {
1698         u64 spte;
1699         int ret = 0;
1700
1701         /*
1702          * We don't set the accessed bit, since we sometimes want to see
1703          * whether the guest actually used the pte (in order to detect
1704          * demand paging).
1705          */
1706         spte = shadow_base_present_pte | shadow_dirty_mask;
1707         if (!speculative)
1708                 spte |= shadow_accessed_mask;
1709         if (!dirty)
1710                 pte_access &= ~ACC_WRITE_MASK;
1711         if (pte_access & ACC_EXEC_MASK)
1712                 spte |= shadow_x_mask;
1713         else
1714                 spte |= shadow_nx_mask;
1715         if (pte_access & ACC_USER_MASK)
1716                 spte |= shadow_user_mask;
1717         if (largepage)
1718                 spte |= PT_PAGE_SIZE_MASK;
1719         if (tdp_enabled)
1720                 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1721                         kvm_is_mmio_pfn(pfn));
1722
1723         spte |= (u64)pfn << PAGE_SHIFT;
1724
1725         if ((pte_access & ACC_WRITE_MASK)
1726             || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1727
1728                 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1729                         ret = 1;
1730                         spte = shadow_trap_nonpresent_pte;
1731                         goto set_pte;
1732                 }
1733
1734                 spte |= PT_WRITABLE_MASK;
1735
1736                 /*
1737                  * Optimization: for pte sync, if spte was writable the hash
1738                  * lookup is unnecessary (and expensive). Write protection
1739                  * is responsibility of mmu_get_page / kvm_sync_page.
1740                  * Same reasoning can be applied to dirty page accounting.
1741                  */
1742                 if (!can_unsync && is_writeble_pte(*sptep))
1743                         goto set_pte;
1744
1745                 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
1746                         pgprintk("%s: found shadow page for %lx, marking ro\n",
1747                                  __func__, gfn);
1748                         ret = 1;
1749                         pte_access &= ~ACC_WRITE_MASK;
1750                         if (is_writeble_pte(spte))
1751                                 spte &= ~PT_WRITABLE_MASK;
1752                 }
1753         }
1754
1755         if (pte_access & ACC_WRITE_MASK)
1756                 mark_page_dirty(vcpu->kvm, gfn);
1757
1758 set_pte:
1759         __set_spte(sptep, spte);
1760         return ret;
1761 }
1762
1763 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1764                          unsigned pt_access, unsigned pte_access,
1765                          int user_fault, int write_fault, int dirty,
1766                          int *ptwrite, int largepage, gfn_t gfn,
1767                          pfn_t pfn, bool speculative)
1768 {
1769         int was_rmapped = 0;
1770         int was_writeble = is_writeble_pte(*sptep);
1771         int rmap_count;
1772
1773         pgprintk("%s: spte %llx access %x write_fault %d"
1774                  " user_fault %d gfn %lx\n",
1775                  __func__, *sptep, pt_access,
1776                  write_fault, user_fault, gfn);
1777
1778         if (is_rmap_spte(*sptep)) {
1779                 /*
1780                  * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1781                  * the parent of the now unreachable PTE.
1782                  */
1783                 if (largepage && !is_large_pte(*sptep)) {
1784                         struct kvm_mmu_page *child;
1785                         u64 pte = *sptep;
1786
1787                         child = page_header(pte & PT64_BASE_ADDR_MASK);
1788                         mmu_page_remove_parent_pte(child, sptep);
1789                 } else if (pfn != spte_to_pfn(*sptep)) {
1790                         pgprintk("hfn old %lx new %lx\n",
1791                                  spte_to_pfn(*sptep), pfn);
1792                         rmap_remove(vcpu->kvm, sptep);
1793                 } else
1794                         was_rmapped = 1;
1795         }
1796         if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
1797                       dirty, largepage, gfn, pfn, speculative, true)) {
1798                 if (write_fault)
1799                         *ptwrite = 1;
1800                 kvm_x86_ops->tlb_flush(vcpu);
1801         }
1802
1803         pgprintk("%s: setting spte %llx\n", __func__, *sptep);
1804         pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1805                  is_large_pte(*sptep)? "2MB" : "4kB",
1806                  is_present_pte(*sptep)?"RW":"R", gfn,
1807                  *shadow_pte, sptep);
1808         if (!was_rmapped && is_large_pte(*sptep))
1809                 ++vcpu->kvm->stat.lpages;
1810
1811         page_header_update_slot(vcpu->kvm, sptep, gfn);
1812         if (!was_rmapped) {
1813                 rmap_count = rmap_add(vcpu, sptep, gfn, largepage);
1814                 if (!is_rmap_spte(*sptep))
1815                         kvm_release_pfn_clean(pfn);
1816                 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1817                         rmap_recycle(vcpu, gfn, largepage);
1818         } else {
1819                 if (was_writeble)
1820                         kvm_release_pfn_dirty(pfn);
1821                 else
1822                         kvm_release_pfn_clean(pfn);
1823         }
1824         if (speculative) {
1825                 vcpu->arch.last_pte_updated = sptep;
1826                 vcpu->arch.last_pte_gfn = gfn;
1827         }
1828 }
1829
1830 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1831 {
1832 }
1833
1834 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
1835                         int largepage, gfn_t gfn, pfn_t pfn)
1836 {
1837         struct kvm_shadow_walk_iterator iterator;
1838         struct kvm_mmu_page *sp;
1839         int pt_write = 0;
1840         gfn_t pseudo_gfn;
1841
1842         for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1843                 if (iterator.level == PT_PAGE_TABLE_LEVEL
1844                     || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1845                         mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1846                                      0, write, 1, &pt_write,
1847                                      largepage, gfn, pfn, false);
1848                         ++vcpu->stat.pf_fixed;
1849                         break;
1850                 }
1851
1852                 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1853                         pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1854                         sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1855                                               iterator.level - 1,
1856                                               1, ACC_ALL, iterator.sptep);
1857                         if (!sp) {
1858                                 pgprintk("nonpaging_map: ENOMEM\n");
1859                                 kvm_release_pfn_clean(pfn);
1860                                 return -ENOMEM;
1861                         }
1862
1863                         __set_spte(iterator.sptep,
1864                                    __pa(sp->spt)
1865                                    | PT_PRESENT_MASK | PT_WRITABLE_MASK
1866                                    | shadow_user_mask | shadow_x_mask);
1867                 }
1868         }
1869         return pt_write;
1870 }
1871
1872 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1873 {
1874         int r;
1875         int largepage = 0;
1876         pfn_t pfn;
1877         unsigned long mmu_seq;
1878
1879         if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1880                 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1881                 largepage = 1;
1882         }
1883
1884         mmu_seq = vcpu->kvm->mmu_notifier_seq;
1885         smp_rmb();
1886         pfn = gfn_to_pfn(vcpu->kvm, gfn);
1887
1888         /* mmio */
1889         if (is_error_pfn(pfn)) {
1890                 kvm_release_pfn_clean(pfn);
1891                 return 1;
1892         }
1893
1894         spin_lock(&vcpu->kvm->mmu_lock);
1895         if (mmu_notifier_retry(vcpu, mmu_seq))
1896                 goto out_unlock;
1897         kvm_mmu_free_some_pages(vcpu);
1898         r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
1899         spin_unlock(&vcpu->kvm->mmu_lock);
1900
1901
1902         return r;
1903
1904 out_unlock:
1905         spin_unlock(&vcpu->kvm->mmu_lock);
1906         kvm_release_pfn_clean(pfn);
1907         return 0;
1908 }
1909
1910
1911 static void mmu_free_roots(struct kvm_vcpu *vcpu)
1912 {
1913         int i;
1914         struct kvm_mmu_page *sp;
1915
1916         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1917                 return;
1918         spin_lock(&vcpu->kvm->mmu_lock);
1919         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1920                 hpa_t root = vcpu->arch.mmu.root_hpa;
1921
1922                 sp = page_header(root);
1923                 --sp->root_count;
1924                 if (!sp->root_count && sp->role.invalid)
1925                         kvm_mmu_zap_page(vcpu->kvm, sp);
1926                 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1927                 spin_unlock(&vcpu->kvm->mmu_lock);
1928                 return;
1929         }
1930         for (i = 0; i < 4; ++i) {
1931                 hpa_t root = vcpu->arch.mmu.pae_root[i];
1932
1933                 if (root) {
1934                         root &= PT64_BASE_ADDR_MASK;
1935                         sp = page_header(root);
1936                         --sp->root_count;
1937                         if (!sp->root_count && sp->role.invalid)
1938                                 kvm_mmu_zap_page(vcpu->kvm, sp);
1939                 }
1940                 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
1941         }
1942         spin_unlock(&vcpu->kvm->mmu_lock);
1943         vcpu->arch.mmu.root_hpa = INVALID_PAGE;
1944 }
1945
1946 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1947 {
1948         int ret = 0;
1949
1950         if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1951                 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1952                 ret = 1;
1953         }
1954
1955         return ret;
1956 }
1957
1958 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
1959 {
1960         int i;
1961         gfn_t root_gfn;
1962         struct kvm_mmu_page *sp;
1963         int direct = 0;
1964         u64 pdptr;
1965
1966         root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
1967
1968         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1969                 hpa_t root = vcpu->arch.mmu.root_hpa;
1970
1971                 ASSERT(!VALID_PAGE(root));
1972                 if (tdp_enabled)
1973                         direct = 1;
1974                 if (mmu_check_root(vcpu, root_gfn))
1975                         return 1;
1976                 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
1977                                       PT64_ROOT_LEVEL, direct,
1978                                       ACC_ALL, NULL);
1979                 root = __pa(sp->spt);
1980                 ++sp->root_count;
1981                 vcpu->arch.mmu.root_hpa = root;
1982                 return 0;
1983         }
1984         direct = !is_paging(vcpu);
1985         if (tdp_enabled)
1986                 direct = 1;
1987         for (i = 0; i < 4; ++i) {
1988                 hpa_t root = vcpu->arch.mmu.pae_root[i];
1989
1990                 ASSERT(!VALID_PAGE(root));
1991                 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1992                         pdptr = kvm_pdptr_read(vcpu, i);
1993                         if (!is_present_gpte(pdptr)) {
1994                                 vcpu->arch.mmu.pae_root[i] = 0;
1995                                 continue;
1996                         }
1997                         root_gfn = pdptr >> PAGE_SHIFT;
1998                 } else if (vcpu->arch.mmu.root_level == 0)
1999                         root_gfn = 0;
2000                 if (mmu_check_root(vcpu, root_gfn))
2001                         return 1;
2002                 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
2003                                       PT32_ROOT_LEVEL, direct,
2004                                       ACC_ALL, NULL);
2005                 root = __pa(sp->spt);
2006                 ++sp->root_count;
2007                 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2008         }
2009         vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2010         return 0;
2011 }
2012
2013 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2014 {
2015         int i;
2016         struct kvm_mmu_page *sp;
2017
2018         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2019                 return;
2020         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2021                 hpa_t root = vcpu->arch.mmu.root_hpa;
2022                 sp = page_header(root);
2023                 mmu_sync_children(vcpu, sp);
2024                 return;
2025         }
2026         for (i = 0; i < 4; ++i) {
2027                 hpa_t root = vcpu->arch.mmu.pae_root[i];
2028
2029                 if (root && VALID_PAGE(root)) {
2030                         root &= PT64_BASE_ADDR_MASK;
2031                         sp = page_header(root);
2032                         mmu_sync_children(vcpu, sp);
2033                 }
2034         }
2035 }
2036
2037 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2038 {
2039         spin_lock(&vcpu->kvm->mmu_lock);
2040         mmu_sync_roots(vcpu);
2041         spin_unlock(&vcpu->kvm->mmu_lock);
2042 }
2043
2044 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2045 {
2046         return vaddr;
2047 }
2048
2049 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2050                                 u32 error_code)
2051 {
2052         gfn_t gfn;
2053         int r;
2054
2055         pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2056         r = mmu_topup_memory_caches(vcpu);
2057         if (r)
2058                 return r;
2059
2060         ASSERT(vcpu);
2061         ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2062
2063         gfn = gva >> PAGE_SHIFT;
2064
2065         return nonpaging_map(vcpu, gva & PAGE_MASK,
2066                              error_code & PFERR_WRITE_MASK, gfn);
2067 }
2068
2069 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2070                                 u32 error_code)
2071 {
2072         pfn_t pfn;
2073         int r;
2074         int largepage = 0;
2075         gfn_t gfn = gpa >> PAGE_SHIFT;
2076         unsigned long mmu_seq;
2077
2078         ASSERT(vcpu);
2079         ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2080
2081         r = mmu_topup_memory_caches(vcpu);
2082         if (r)
2083                 return r;
2084
2085         if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
2086                 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2087                 largepage = 1;
2088         }
2089         mmu_seq = vcpu->kvm->mmu_notifier_seq;
2090         smp_rmb();
2091         pfn = gfn_to_pfn(vcpu->kvm, gfn);
2092         if (is_error_pfn(pfn)) {
2093                 kvm_release_pfn_clean(pfn);
2094                 return 1;
2095         }
2096         spin_lock(&vcpu->kvm->mmu_lock);
2097         if (mmu_notifier_retry(vcpu, mmu_seq))
2098                 goto out_unlock;
2099         kvm_mmu_free_some_pages(vcpu);
2100         r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
2101                          largepage, gfn, pfn);
2102         spin_unlock(&vcpu->kvm->mmu_lock);
2103
2104         return r;
2105
2106 out_unlock:
2107         spin_unlock(&vcpu->kvm->mmu_lock);
2108         kvm_release_pfn_clean(pfn);
2109         return 0;
2110 }
2111
2112 static void nonpaging_free(struct kvm_vcpu *vcpu)
2113 {
2114         mmu_free_roots(vcpu);
2115 }
2116
2117 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2118 {
2119         struct kvm_mmu *context = &vcpu->arch.mmu;
2120
2121         context->new_cr3 = nonpaging_new_cr3;
2122         context->page_fault = nonpaging_page_fault;
2123         context->gva_to_gpa = nonpaging_gva_to_gpa;
2124         context->free = nonpaging_free;
2125         context->prefetch_page = nonpaging_prefetch_page;
2126         context->sync_page = nonpaging_sync_page;
2127         context->invlpg = nonpaging_invlpg;
2128         context->root_level = 0;
2129         context->shadow_root_level = PT32E_ROOT_LEVEL;
2130         context->root_hpa = INVALID_PAGE;
2131         return 0;
2132 }
2133
2134 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2135 {
2136         ++vcpu->stat.tlb_flush;
2137         kvm_x86_ops->tlb_flush(vcpu);
2138 }
2139
2140 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2141 {
2142         pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
2143         mmu_free_roots(vcpu);
2144 }
2145
2146 static void inject_page_fault(struct kvm_vcpu *vcpu,
2147                               u64 addr,
2148                               u32 err_code)
2149 {
2150         kvm_inject_page_fault(vcpu, addr, err_code);
2151 }
2152
2153 static void paging_free(struct kvm_vcpu *vcpu)
2154 {
2155         nonpaging_free(vcpu);
2156 }
2157
2158 static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2159 {
2160         int bit7;
2161
2162         bit7 = (gpte >> 7) & 1;
2163         return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2164 }
2165
2166 #define PTTYPE 64
2167 #include "paging_tmpl.h"
2168 #undef PTTYPE
2169
2170 #define PTTYPE 32
2171 #include "paging_tmpl.h"
2172 #undef PTTYPE
2173
2174 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2175 {
2176         struct kvm_mmu *context = &vcpu->arch.mmu;
2177         int maxphyaddr = cpuid_maxphyaddr(vcpu);
2178         u64 exb_bit_rsvd = 0;
2179
2180         if (!is_nx(vcpu))
2181                 exb_bit_rsvd = rsvd_bits(63, 63);
2182         switch (level) {
2183         case PT32_ROOT_LEVEL:
2184                 /* no rsvd bits for 2 level 4K page table entries */
2185                 context->rsvd_bits_mask[0][1] = 0;
2186                 context->rsvd_bits_mask[0][0] = 0;
2187                 if (is_cpuid_PSE36())
2188                         /* 36bits PSE 4MB page */
2189                         context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2190                 else
2191                         /* 32 bits PSE 4MB page */
2192                         context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2193                 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2194                 break;
2195         case PT32E_ROOT_LEVEL:
2196                 context->rsvd_bits_mask[0][2] =
2197                         rsvd_bits(maxphyaddr, 63) |
2198                         rsvd_bits(7, 8) | rsvd_bits(1, 2);      /* PDPTE */
2199                 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2200                         rsvd_bits(maxphyaddr, 62);      /* PDE */
2201                 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2202                         rsvd_bits(maxphyaddr, 62);      /* PTE */
2203                 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2204                         rsvd_bits(maxphyaddr, 62) |
2205                         rsvd_bits(13, 20);              /* large page */
2206                 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2207                 break;
2208         case PT64_ROOT_LEVEL:
2209                 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2210                         rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2211                 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2212                         rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2213                 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2214                         rsvd_bits(maxphyaddr, 51);
2215                 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2216                         rsvd_bits(maxphyaddr, 51);
2217                 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2218                 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2219                 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2220                         rsvd_bits(maxphyaddr, 51) |
2221                         rsvd_bits(13, 20);              /* large page */
2222                 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
2223                 break;
2224         }
2225 }
2226
2227 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
2228 {
2229         struct kvm_mmu *context = &vcpu->arch.mmu;
2230
2231         ASSERT(is_pae(vcpu));
2232         context->new_cr3 = paging_new_cr3;
2233         context->page_fault = paging64_page_fault;
2234         context->gva_to_gpa = paging64_gva_to_gpa;
2235         context->prefetch_page = paging64_prefetch_page;
2236         context->sync_page = paging64_sync_page;
2237         context->invlpg = paging64_invlpg;
2238         context->free = paging_free;
2239         context->root_level = level;
2240         context->shadow_root_level = level;
2241         context->root_hpa = INVALID_PAGE;
2242         return 0;
2243 }
2244
2245 static int paging64_init_context(struct kvm_vcpu *vcpu)
2246 {
2247         reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2248         return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2249 }
2250
2251 static int paging32_init_context(struct kvm_vcpu *vcpu)
2252 {
2253         struct kvm_mmu *context = &vcpu->arch.mmu;
2254
2255         reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2256         context->new_cr3 = paging_new_cr3;
2257         context->page_fault = paging32_page_fault;
2258         context->gva_to_gpa = paging32_gva_to_gpa;
2259         context->free = paging_free;
2260         context->prefetch_page = paging32_prefetch_page;
2261         context->sync_page = paging32_sync_page;
2262         context->invlpg = paging32_invlpg;
2263         context->root_level = PT32_ROOT_LEVEL;
2264         context->shadow_root_level = PT32E_ROOT_LEVEL;
2265         context->root_hpa = INVALID_PAGE;
2266         return 0;
2267 }
2268
2269 static int paging32E_init_context(struct kvm_vcpu *vcpu)
2270 {
2271         reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2272         return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
2273 }
2274
2275 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2276 {
2277         struct kvm_mmu *context = &vcpu->arch.mmu;
2278
2279         context->new_cr3 = nonpaging_new_cr3;
2280         context->page_fault = tdp_page_fault;
2281         context->free = nonpaging_free;
2282         context->prefetch_page = nonpaging_prefetch_page;
2283         context->sync_page = nonpaging_sync_page;
2284         context->invlpg = nonpaging_invlpg;
2285         context->shadow_root_level = kvm_x86_ops->get_tdp_level();
2286         context->root_hpa = INVALID_PAGE;
2287
2288         if (!is_paging(vcpu)) {
2289                 context->gva_to_gpa = nonpaging_gva_to_gpa;
2290                 context->root_level = 0;
2291         } else if (is_long_mode(vcpu)) {
2292                 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
2293                 context->gva_to_gpa = paging64_gva_to_gpa;
2294                 context->root_level = PT64_ROOT_LEVEL;
2295         } else if (is_pae(vcpu)) {
2296                 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
2297                 context->gva_to_gpa = paging64_gva_to_gpa;
2298                 context->root_level = PT32E_ROOT_LEVEL;
2299         } else {
2300                 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
2301                 context->gva_to_gpa = paging32_gva_to_gpa;
2302                 context->root_level = PT32_ROOT_LEVEL;
2303         }
2304
2305         return 0;
2306 }
2307
2308 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2309 {
2310         int r;
2311
2312         ASSERT(vcpu);
2313         ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2314
2315         if (!is_paging(vcpu))
2316                 r = nonpaging_init_context(vcpu);
2317         else if (is_long_mode(vcpu))
2318                 r = paging64_init_context(vcpu);
2319         else if (is_pae(vcpu))
2320                 r = paging32E_init_context(vcpu);
2321         else
2322                 r = paging32_init_context(vcpu);
2323
2324         vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2325
2326         return r;
2327 }
2328
2329 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2330 {
2331         vcpu->arch.update_pte.pfn = bad_pfn;
2332
2333         if (tdp_enabled)
2334                 return init_kvm_tdp_mmu(vcpu);
2335         else
2336                 return init_kvm_softmmu(vcpu);
2337 }
2338
2339 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2340 {
2341         ASSERT(vcpu);
2342         if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2343                 vcpu->arch.mmu.free(vcpu);
2344                 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2345         }
2346 }
2347
2348 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2349 {
2350         destroy_kvm_mmu(vcpu);
2351         return init_kvm_mmu(vcpu);
2352 }
2353 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
2354
2355 int kvm_mmu_load(struct kvm_vcpu *vcpu)
2356 {
2357         int r;
2358
2359         r = mmu_topup_memory_caches(vcpu);
2360         if (r)
2361                 goto out;
2362         spin_lock(&vcpu->kvm->mmu_lock);
2363         kvm_mmu_free_some_pages(vcpu);
2364         r = mmu_alloc_roots(vcpu);
2365         mmu_sync_roots(vcpu);
2366         spin_unlock(&vcpu->kvm->mmu_lock);
2367         if (r)
2368                 goto out;
2369         kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
2370         kvm_mmu_flush_tlb(vcpu);
2371 out:
2372         return r;
2373 }
2374 EXPORT_SYMBOL_GPL(kvm_mmu_load);
2375
2376 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2377 {
2378         mmu_free_roots(vcpu);
2379 }
2380
2381 static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
2382                                   struct kvm_mmu_page *sp,
2383                                   u64 *spte)
2384 {
2385         u64 pte;
2386         struct kvm_mmu_page *child;
2387
2388         pte = *spte;
2389         if (is_shadow_present_pte(pte)) {
2390                 if (is_last_spte(pte, sp->role.level))
2391                         rmap_remove(vcpu->kvm, spte);
2392                 else {
2393                         child = page_header(pte & PT64_BASE_ADDR_MASK);
2394                         mmu_page_remove_parent_pte(child, spte);
2395                 }
2396         }
2397         __set_spte(spte, shadow_trap_nonpresent_pte);
2398         if (is_large_pte(pte))
2399                 --vcpu->kvm->stat.lpages;
2400 }
2401
2402 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
2403                                   struct kvm_mmu_page *sp,
2404                                   u64 *spte,
2405                                   const void *new)
2406 {
2407         if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2408                 if (!vcpu->arch.update_pte.largepage ||
2409                     sp->role.glevels == PT32_ROOT_LEVEL) {
2410                         ++vcpu->kvm->stat.mmu_pde_zapped;
2411                         return;
2412                 }
2413         }
2414
2415         ++vcpu->kvm->stat.mmu_pte_updated;
2416         if (sp->role.glevels == PT32_ROOT_LEVEL)
2417                 paging32_update_pte(vcpu, sp, spte, new);
2418         else
2419                 paging64_update_pte(vcpu, sp, spte, new);
2420 }
2421
2422 static bool need_remote_flush(u64 old, u64 new)
2423 {
2424         if (!is_shadow_present_pte(old))
2425                 return false;
2426         if (!is_shadow_present_pte(new))
2427                 return true;
2428         if ((old ^ new) & PT64_BASE_ADDR_MASK)
2429                 return true;
2430         old ^= PT64_NX_MASK;
2431         new ^= PT64_NX_MASK;
2432         return (old & ~new & PT64_PERM_MASK) != 0;
2433 }
2434
2435 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2436 {
2437         if (need_remote_flush(old, new))
2438                 kvm_flush_remote_tlbs(vcpu->kvm);
2439         else
2440                 kvm_mmu_flush_tlb(vcpu);
2441 }
2442
2443 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2444 {
2445         u64 *spte = vcpu->arch.last_pte_updated;
2446
2447         return !!(spte && (*spte & shadow_accessed_mask));
2448 }
2449
2450 static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2451                                           const u8 *new, int bytes)
2452 {
2453         gfn_t gfn;
2454         int r;
2455         u64 gpte = 0;
2456         pfn_t pfn;
2457
2458         vcpu->arch.update_pte.largepage = 0;
2459
2460         if (bytes != 4 && bytes != 8)
2461                 return;
2462
2463         /*
2464          * Assume that the pte write on a page table of the same type
2465          * as the current vcpu paging mode.  This is nearly always true
2466          * (might be false while changing modes).  Note it is verified later
2467          * by update_pte().
2468          */
2469         if (is_pae(vcpu)) {
2470                 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2471                 if ((bytes == 4) && (gpa % 4 == 0)) {
2472                         r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2473                         if (r)
2474                                 return;
2475                         memcpy((void *)&gpte + (gpa % 8), new, 4);
2476                 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2477                         memcpy((void *)&gpte, new, 8);
2478                 }
2479         } else {
2480                 if ((bytes == 4) && (gpa % 4 == 0))
2481                         memcpy((void *)&gpte, new, 4);
2482         }
2483         if (!is_present_gpte(gpte))
2484                 return;
2485         gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
2486
2487         if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
2488                 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
2489                 vcpu->arch.update_pte.largepage = 1;
2490         }
2491         vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
2492         smp_rmb();
2493         pfn = gfn_to_pfn(vcpu->kvm, gfn);
2494
2495         if (is_error_pfn(pfn)) {
2496                 kvm_release_pfn_clean(pfn);
2497                 return;
2498         }
2499         vcpu->arch.update_pte.gfn = gfn;
2500         vcpu->arch.update_pte.pfn = pfn;
2501 }
2502
2503 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2504 {
2505         u64 *spte = vcpu->arch.last_pte_updated;
2506
2507         if (spte
2508             && vcpu->arch.last_pte_gfn == gfn
2509             && shadow_accessed_mask
2510             && !(*spte & shadow_accessed_mask)
2511             && is_shadow_present_pte(*spte))
2512                 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2513 }
2514
2515 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2516                        const u8 *new, int bytes,
2517                        bool guest_initiated)
2518 {
2519         gfn_t gfn = gpa >> PAGE_SHIFT;
2520         struct kvm_mmu_page *sp;
2521         struct hlist_node *node, *n;
2522         struct hlist_head *bucket;
2523         unsigned index;
2524         u64 entry, gentry;
2525         u64 *spte;
2526         unsigned offset = offset_in_page(gpa);
2527         unsigned pte_size;
2528         unsigned page_offset;
2529         unsigned misaligned;
2530         unsigned quadrant;
2531         int level;
2532         int flooded = 0;
2533         int npte;
2534         int r;
2535
2536         pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
2537         mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
2538         spin_lock(&vcpu->kvm->mmu_lock);
2539         kvm_mmu_access_page(vcpu, gfn);
2540         kvm_mmu_free_some_pages(vcpu);
2541         ++vcpu->kvm->stat.mmu_pte_write;
2542         kvm_mmu_audit(vcpu, "pre pte write");
2543         if (guest_initiated) {
2544                 if (gfn == vcpu->arch.last_pt_write_gfn
2545                     && !last_updated_pte_accessed(vcpu)) {
2546                         ++vcpu->arch.last_pt_write_count;
2547                         if (vcpu->arch.last_pt_write_count >= 3)
2548                                 flooded = 1;
2549                 } else {
2550                         vcpu->arch.last_pt_write_gfn = gfn;
2551                         vcpu->arch.last_pt_write_count = 1;
2552                         vcpu->arch.last_pte_updated = NULL;
2553                 }
2554         }
2555         index = kvm_page_table_hashfn(gfn);
2556         bucket = &vcpu->kvm->arch.mmu_page_hash[index];
2557         hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
2558                 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
2559                         continue;
2560                 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
2561                 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
2562                 misaligned |= bytes < 4;
2563                 if (misaligned || flooded) {
2564                         /*
2565                          * Misaligned accesses are too much trouble to fix
2566                          * up; also, they usually indicate a page is not used
2567                          * as a page table.
2568                          *
2569                          * If we're seeing too many writes to a page,
2570                          * it may no longer be a page table, or we may be
2571                          * forking, in which case it is better to unmap the
2572                          * page.
2573                          */
2574                         pgprintk("misaligned: gpa %llx bytes %d role %x\n",
2575                                  gpa, bytes, sp->role.word);
2576                         if (kvm_mmu_zap_page(vcpu->kvm, sp))
2577                                 n = bucket->first;
2578                         ++vcpu->kvm->stat.mmu_flooded;
2579                         continue;
2580                 }
2581                 page_offset = offset;
2582                 level = sp->role.level;
2583                 npte = 1;
2584                 if (sp->role.glevels == PT32_ROOT_LEVEL) {
2585                         page_offset <<= 1;      /* 32->64 */
2586                         /*
2587                          * A 32-bit pde maps 4MB while the shadow pdes map
2588                          * only 2MB.  So we need to double the offset again
2589                          * and zap two pdes instead of one.
2590                          */
2591                         if (level == PT32_ROOT_LEVEL) {
2592                                 page_offset &= ~7; /* kill rounding error */
2593                                 page_offset <<= 1;
2594                                 npte = 2;
2595                         }
2596                         quadrant = page_offset >> PAGE_SHIFT;
2597                         page_offset &= ~PAGE_MASK;
2598                         if (quadrant != sp->role.quadrant)
2599                                 continue;
2600                 }
2601                 spte = &sp->spt[page_offset / sizeof(*spte)];
2602                 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2603                         gentry = 0;
2604                         r = kvm_read_guest_atomic(vcpu->kvm,
2605                                                   gpa & ~(u64)(pte_size - 1),
2606                                                   &gentry, pte_size);
2607                         new = (const void *)&gentry;
2608                         if (r < 0)
2609                                 new = NULL;
2610                 }
2611                 while (npte--) {
2612                         entry = *spte;
2613                         mmu_pte_write_zap_pte(vcpu, sp, spte);
2614                         if (new)
2615                                 mmu_pte_write_new_pte(vcpu, sp, spte, new);
2616                         mmu_pte_write_flush_tlb(vcpu, entry, *spte);
2617                         ++spte;
2618                 }
2619         }
2620         kvm_mmu_audit(vcpu, "post pte write");
2621         spin_unlock(&vcpu->kvm->mmu_lock);
2622         if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2623                 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2624                 vcpu->arch.update_pte.pfn = bad_pfn;
2625         }
2626 }
2627
2628 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2629 {
2630         gpa_t gpa;
2631         int r;
2632
2633         gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
2634
2635         spin_lock(&vcpu->kvm->mmu_lock);
2636         r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2637         spin_unlock(&vcpu->kvm->mmu_lock);
2638         return r;
2639 }
2640 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
2641
2642 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
2643 {
2644         while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
2645                 struct kvm_mmu_page *sp;
2646
2647                 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
2648                                   struct kvm_mmu_page, link);
2649                 kvm_mmu_zap_page(vcpu->kvm, sp);
2650                 ++vcpu->kvm->stat.mmu_recycled;
2651         }
2652 }
2653
2654 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2655 {
2656         int r;
2657         enum emulation_result er;
2658
2659         r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
2660         if (r < 0)
2661                 goto out;
2662
2663         if (!r) {
2664                 r = 1;
2665                 goto out;
2666         }
2667
2668         r = mmu_topup_memory_caches(vcpu);
2669         if (r)
2670                 goto out;
2671
2672         er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
2673
2674         switch (er) {
2675         case EMULATE_DONE:
2676                 return 1;
2677         case EMULATE_DO_MMIO:
2678                 ++vcpu->stat.mmio_exits;
2679                 return 0;
2680         case EMULATE_FAIL:
2681                 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2682                 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2683                 return 0;
2684         default:
2685                 BUG();
2686         }
2687 out:
2688         return r;
2689 }
2690 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2691
2692 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2693 {
2694         vcpu->arch.mmu.invlpg(vcpu, gva);
2695         kvm_mmu_flush_tlb(vcpu);
2696         ++vcpu->stat.invlpg;
2697 }
2698 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2699
2700 void kvm_enable_tdp(void)
2701 {
2702         tdp_enabled = true;
2703 }
2704 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2705
2706 void kvm_disable_tdp(void)
2707 {
2708         tdp_enabled = false;
2709 }
2710 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2711
2712 static void free_mmu_pages(struct kvm_vcpu *vcpu)
2713 {
2714         free_page((unsigned long)vcpu->arch.mmu.pae_root);
2715 }
2716
2717 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2718 {
2719         struct page *page;
2720         int i;
2721
2722         ASSERT(vcpu);
2723
2724         if (vcpu->kvm->arch.n_requested_mmu_pages)
2725                 vcpu->kvm->arch.n_free_mmu_pages =
2726                                         vcpu->kvm->arch.n_requested_mmu_pages;
2727         else
2728                 vcpu->kvm->arch.n_free_mmu_pages =
2729                                         vcpu->kvm->arch.n_alloc_mmu_pages;
2730         /*
2731          * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2732          * Therefore we need to allocate shadow page tables in the first
2733          * 4GB of memory, which happens to fit the DMA32 zone.
2734          */
2735         page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2736         if (!page)
2737                 goto error_1;
2738         vcpu->arch.mmu.pae_root = page_address(page);
2739         for (i = 0; i < 4; ++i)
2740                 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2741
2742         return 0;
2743
2744 error_1:
2745         free_mmu_pages(vcpu);
2746         return -ENOMEM;
2747 }
2748
2749 int kvm_mmu_create(struct kvm_vcpu *vcpu)
2750 {
2751         ASSERT(vcpu);
2752         ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2753
2754         return alloc_mmu_pages(vcpu);
2755 }
2756
2757 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2758 {
2759         ASSERT(vcpu);
2760         ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2761
2762         return init_kvm_mmu(vcpu);
2763 }
2764
2765 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2766 {
2767         ASSERT(vcpu);
2768
2769         destroy_kvm_mmu(vcpu);
2770         free_mmu_pages(vcpu);
2771         mmu_free_memory_caches(vcpu);
2772 }
2773
2774 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
2775 {
2776         struct kvm_mmu_page *sp;
2777
2778         list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
2779                 int i;
2780                 u64 *pt;
2781
2782                 if (!test_bit(slot, sp->slot_bitmap))
2783                         continue;
2784
2785                 pt = sp->spt;
2786                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2787                         /* avoid RMW */
2788                         if (pt[i] & PT_WRITABLE_MASK)
2789                                 pt[i] &= ~PT_WRITABLE_MASK;
2790         }
2791         kvm_flush_remote_tlbs(kvm);
2792 }
2793
2794 void kvm_mmu_zap_all(struct kvm *kvm)
2795 {
2796         struct kvm_mmu_page *sp, *node;
2797
2798         spin_lock(&kvm->mmu_lock);
2799         list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
2800                 if (kvm_mmu_zap_page(kvm, sp))
2801                         node = container_of(kvm->arch.active_mmu_pages.next,
2802                                             struct kvm_mmu_page, link);
2803         spin_unlock(&kvm->mmu_lock);
2804
2805         kvm_flush_remote_tlbs(kvm);
2806 }
2807
2808 static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
2809 {
2810         struct kvm_mmu_page *page;
2811
2812         page = container_of(kvm->arch.active_mmu_pages.prev,
2813                             struct kvm_mmu_page, link);
2814         kvm_mmu_zap_page(kvm, page);
2815 }
2816
2817 static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2818 {
2819         struct kvm *kvm;
2820         struct kvm *kvm_freed = NULL;
2821         int cache_count = 0;
2822
2823         spin_lock(&kvm_lock);
2824
2825         list_for_each_entry(kvm, &vm_list, vm_list) {
2826                 int npages;
2827
2828                 if (!down_read_trylock(&kvm->slots_lock))
2829                         continue;
2830                 spin_lock(&kvm->mmu_lock);
2831                 npages = kvm->arch.n_alloc_mmu_pages -
2832                          kvm->arch.n_free_mmu_pages;
2833                 cache_count += npages;
2834                 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2835                         kvm_mmu_remove_one_alloc_mmu_page(kvm);
2836                         cache_count--;
2837                         kvm_freed = kvm;
2838                 }
2839                 nr_to_scan--;
2840
2841                 spin_unlock(&kvm->mmu_lock);
2842                 up_read(&kvm->slots_lock);
2843         }
2844         if (kvm_freed)
2845                 list_move_tail(&kvm_freed->vm_list, &vm_list);
2846
2847         spin_unlock(&kvm_lock);
2848
2849         return cache_count;
2850 }
2851
2852 static struct shrinker mmu_shrinker = {
2853         .shrink = mmu_shrink,
2854         .seeks = DEFAULT_SEEKS * 10,
2855 };
2856
2857 static void mmu_destroy_caches(void)
2858 {
2859         if (pte_chain_cache)
2860                 kmem_cache_destroy(pte_chain_cache);
2861         if (rmap_desc_cache)
2862                 kmem_cache_destroy(rmap_desc_cache);
2863         if (mmu_page_header_cache)
2864                 kmem_cache_destroy(mmu_page_header_cache);
2865 }
2866
2867 void kvm_mmu_module_exit(void)
2868 {
2869         mmu_destroy_caches();
2870         unregister_shrinker(&mmu_shrinker);
2871 }
2872
2873 int kvm_mmu_module_init(void)
2874 {
2875         pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2876                                             sizeof(struct kvm_pte_chain),
2877                                             0, 0, NULL);
2878         if (!pte_chain_cache)
2879                 goto nomem;
2880         rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2881                                             sizeof(struct kvm_rmap_desc),
2882                                             0, 0, NULL);
2883         if (!rmap_desc_cache)
2884                 goto nomem;
2885
2886         mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2887                                                   sizeof(struct kvm_mmu_page),
2888                                                   0, 0, NULL);
2889         if (!mmu_page_header_cache)
2890                 goto nomem;
2891
2892         register_shrinker(&mmu_shrinker);
2893
2894         return 0;
2895
2896 nomem:
2897         mmu_destroy_caches();
2898         return -ENOMEM;
2899 }
2900
2901 /*
2902  * Caculate mmu pages needed for kvm.
2903  */
2904 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2905 {
2906         int i;
2907         unsigned int nr_mmu_pages;
2908         unsigned int  nr_pages = 0;
2909
2910         for (i = 0; i < kvm->nmemslots; i++)
2911                 nr_pages += kvm->memslots[i].npages;
2912
2913         nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2914         nr_mmu_pages = max(nr_mmu_pages,
2915                         (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2916
2917         return nr_mmu_pages;
2918 }
2919
2920 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2921                                 unsigned len)
2922 {
2923         if (len > buffer->len)
2924                 return NULL;
2925         return buffer->ptr;
2926 }
2927
2928 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2929                                 unsigned len)
2930 {
2931         void *ret;
2932
2933         ret = pv_mmu_peek_buffer(buffer, len);
2934         if (!ret)
2935                 return ret;
2936         buffer->ptr += len;
2937         buffer->len -= len;
2938         buffer->processed += len;
2939         return ret;
2940 }
2941
2942 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2943                              gpa_t addr, gpa_t value)
2944 {
2945         int bytes = 8;
2946         int r;
2947
2948         if (!is_long_mode(vcpu) && !is_pae(vcpu))
2949                 bytes = 4;
2950
2951         r = mmu_topup_memory_caches(vcpu);
2952         if (r)
2953                 return r;
2954
2955         if (!emulator_write_phys(vcpu, addr, &value, bytes))
2956                 return -EFAULT;
2957
2958         return 1;
2959 }
2960
2961 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2962 {
2963         kvm_set_cr3(vcpu, vcpu->arch.cr3);
2964         return 1;
2965 }
2966
2967 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2968 {
2969         spin_lock(&vcpu->kvm->mmu_lock);
2970         mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2971         spin_unlock(&vcpu->kvm->mmu_lock);
2972         return 1;
2973 }
2974
2975 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2976                              struct kvm_pv_mmu_op_buffer *buffer)
2977 {
2978         struct kvm_mmu_op_header *header;
2979
2980         header = pv_mmu_peek_buffer(buffer, sizeof *header);
2981         if (!header)
2982                 return 0;
2983         switch (header->op) {
2984         case KVM_MMU_OP_WRITE_PTE: {
2985                 struct kvm_mmu_op_write_pte *wpte;
2986
2987                 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2988                 if (!wpte)
2989                         return 0;
2990                 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2991                                         wpte->pte_val);
2992         }
2993         case KVM_MMU_OP_FLUSH_TLB: {
2994                 struct kvm_mmu_op_flush_tlb *ftlb;
2995
2996                 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2997                 if (!ftlb)
2998                         return 0;
2999                 return kvm_pv_mmu_flush_tlb(vcpu);
3000         }
3001         case KVM_MMU_OP_RELEASE_PT: {
3002                 struct kvm_mmu_op_release_pt *rpt;
3003
3004                 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3005                 if (!rpt)
3006                         return 0;
3007                 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3008         }
3009         default: return 0;
3010         }
3011 }
3012
3013 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3014                   gpa_t addr, unsigned long *ret)
3015 {
3016         int r;
3017         struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3018
3019         buffer->ptr = buffer->buf;
3020         buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3021         buffer->processed = 0;
3022
3023         r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3024         if (r)
3025                 goto out;
3026
3027         while (buffer->len) {
3028                 r = kvm_pv_mmu_op_one(vcpu, buffer);
3029                 if (r < 0)
3030                         goto out;
3031                 if (r == 0)
3032                         break;
3033         }
3034
3035         r = 1;
3036 out:
3037         *ret = buffer->processed;
3038         return r;
3039 }
3040
3041 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3042 {
3043         struct kvm_shadow_walk_iterator iterator;
3044         int nr_sptes = 0;
3045
3046         spin_lock(&vcpu->kvm->mmu_lock);
3047         for_each_shadow_entry(vcpu, addr, iterator) {
3048                 sptes[iterator.level-1] = *iterator.sptep;
3049                 nr_sptes++;
3050                 if (!is_shadow_present_pte(*iterator.sptep))
3051                         break;
3052         }
3053         spin_unlock(&vcpu->kvm->mmu_lock);
3054
3055         return nr_sptes;
3056 }
3057 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3058
3059 #ifdef AUDIT
3060
3061 static const char *audit_msg;
3062
3063 static gva_t canonicalize(gva_t gva)
3064 {
3065 #ifdef CONFIG_X86_64
3066         gva = (long long)(gva << 16) >> 16;
3067 #endif
3068         return gva;
3069 }
3070
3071
3072 typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3073                                  u64 *sptep);
3074
3075 static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3076                             inspect_spte_fn fn)
3077 {
3078         int i;
3079
3080         for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3081                 u64 ent = sp->spt[i];
3082
3083                 if (is_shadow_present_pte(ent)) {
3084                         if (!is_last_spte(ent, sp->role.level)) {
3085                                 struct kvm_mmu_page *child;
3086                                 child = page_header(ent & PT64_BASE_ADDR_MASK);
3087                                 __mmu_spte_walk(kvm, child, fn);
3088                         } else
3089                                 fn(kvm, sp, &sp->spt[i]);
3090                 }
3091         }
3092 }
3093
3094 static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3095 {
3096         int i;
3097         struct kvm_mmu_page *sp;
3098
3099         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3100                 return;
3101         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3102                 hpa_t root = vcpu->arch.mmu.root_hpa;
3103                 sp = page_header(root);
3104                 __mmu_spte_walk(vcpu->kvm, sp, fn);
3105                 return;
3106         }
3107         for (i = 0; i < 4; ++i) {
3108                 hpa_t root = vcpu->arch.mmu.pae_root[i];
3109
3110                 if (root && VALID_PAGE(root)) {
3111                         root &= PT64_BASE_ADDR_MASK;
3112                         sp = page_header(root);
3113                         __mmu_spte_walk(vcpu->kvm, sp, fn);
3114                 }
3115         }
3116         return;
3117 }
3118
3119 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3120                                 gva_t va, int level)
3121 {
3122         u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3123         int i;
3124         gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3125
3126         for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3127                 u64 ent = pt[i];
3128
3129                 if (ent == shadow_trap_nonpresent_pte)
3130                         continue;
3131
3132                 va = canonicalize(va);
3133                 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3134                         audit_mappings_page(vcpu, ent, va, level - 1);
3135                 else {
3136                         gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
3137                         gfn_t gfn = gpa >> PAGE_SHIFT;
3138                         pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3139                         hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
3140
3141                         if (is_error_pfn(pfn)) {
3142                                 kvm_release_pfn_clean(pfn);
3143                                 continue;
3144                         }
3145
3146                         if (is_shadow_present_pte(ent)
3147                             && (ent & PT64_BASE_ADDR_MASK) != hpa)
3148                                 printk(KERN_ERR "xx audit error: (%s) levels %d"
3149                                        " gva %lx gpa %llx hpa %llx ent %llx %d\n",
3150                                        audit_msg, vcpu->arch.mmu.root_level,
3151                                        va, gpa, hpa, ent,
3152                                        is_shadow_present_pte(ent));
3153                         else if (ent == shadow_notrap_nonpresent_pte
3154                                  && !is_error_hpa(hpa))
3155                                 printk(KERN_ERR "audit: (%s) notrap shadow,"
3156                                        " valid guest gva %lx\n", audit_msg, va);
3157                         kvm_release_pfn_clean(pfn);
3158
3159                 }
3160         }
3161 }
3162
3163 static void audit_mappings(struct kvm_vcpu *vcpu)
3164 {
3165         unsigned i;
3166
3167         if (vcpu->arch.mmu.root_level == 4)
3168                 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
3169         else
3170                 for (i = 0; i < 4; ++i)
3171                         if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
3172                                 audit_mappings_page(vcpu,
3173                                                     vcpu->arch.mmu.pae_root[i],
3174                                                     i << 30,
3175                                                     2);
3176 }
3177
3178 static int count_rmaps(struct kvm_vcpu *vcpu)
3179 {
3180         int nmaps = 0;
3181         int i, j, k;
3182
3183         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3184                 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3185                 struct kvm_rmap_desc *d;
3186
3187                 for (j = 0; j < m->npages; ++j) {
3188                         unsigned long *rmapp = &m->rmap[j];
3189
3190                         if (!*rmapp)
3191                                 continue;
3192                         if (!(*rmapp & 1)) {
3193                                 ++nmaps;
3194                                 continue;
3195                         }
3196                         d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
3197                         while (d) {
3198                                 for (k = 0; k < RMAP_EXT; ++k)
3199                                         if (d->sptes[k])
3200                                                 ++nmaps;
3201                                         else
3202                                                 break;
3203                                 d = d->more;
3204                         }
3205                 }
3206         }
3207         return nmaps;
3208 }
3209
3210 void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
3211 {
3212         unsigned long *rmapp;
3213         struct kvm_mmu_page *rev_sp;
3214         gfn_t gfn;
3215
3216         if (*sptep & PT_WRITABLE_MASK) {
3217                 rev_sp = page_header(__pa(sptep));
3218                 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3219
3220                 if (!gfn_to_memslot(kvm, gfn)) {
3221                         if (!printk_ratelimit())
3222                                 return;
3223                         printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3224                                          audit_msg, gfn);
3225                         printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3226                                         audit_msg, sptep - rev_sp->spt,
3227                                         rev_sp->gfn);
3228                         dump_stack();
3229                         return;
3230                 }
3231
3232                 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3233                                     is_large_pte(*sptep));
3234                 if (!*rmapp) {
3235                         if (!printk_ratelimit())
3236                                 return;
3237                         printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3238                                          audit_msg, *sptep);
3239                         dump_stack();
3240                 }
3241         }
3242
3243 }
3244
3245 void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3246 {
3247         mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3248 }
3249
3250 static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3251 {
3252         struct kvm_mmu_page *sp;
3253         int i;
3254
3255         list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3256                 u64 *pt = sp->spt;
3257
3258                 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
3259                         continue;
3260
3261                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3262                         u64 ent = pt[i];
3263
3264                         if (!(ent & PT_PRESENT_MASK))
3265                                 continue;
3266                         if (!(ent & PT_WRITABLE_MASK))
3267                                 continue;
3268                         inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
3269                 }
3270         }
3271         return;
3272 }
3273
3274 static void audit_rmap(struct kvm_vcpu *vcpu)
3275 {
3276         check_writable_mappings_rmap(vcpu);
3277         count_rmaps(vcpu);
3278 }
3279
3280 static void audit_write_protection(struct kvm_vcpu *vcpu)
3281 {
3282         struct kvm_mmu_page *sp;
3283         struct kvm_memory_slot *slot;
3284         unsigned long *rmapp;
3285         u64 *spte;
3286         gfn_t gfn;
3287
3288         list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
3289                 if (sp->role.direct)
3290                         continue;
3291                 if (sp->unsync)
3292                         continue;
3293
3294                 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
3295                 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
3296                 rmapp = &slot->rmap[gfn - slot->base_gfn];
3297
3298                 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3299                 while (spte) {
3300                         if (*spte & PT_WRITABLE_MASK)
3301                                 printk(KERN_ERR "%s: (%s) shadow page has "
3302                                 "writable mappings: gfn %lx role %x\n",
3303                                __func__, audit_msg, sp->gfn,
3304                                sp->role.word);
3305                         spte = rmap_next(vcpu->kvm, rmapp, spte);
3306                 }
3307         }
3308 }
3309
3310 static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3311 {
3312         int olddbg = dbg;
3313
3314         dbg = 0;
3315         audit_msg = msg;
3316         audit_rmap(vcpu);
3317         audit_write_protection(vcpu);
3318         if (strcmp("pre pte write", audit_msg) != 0)
3319                 audit_mappings(vcpu);
3320         audit_writable_sptes_have_rmaps(vcpu);
3321         dbg = olddbg;
3322 }
3323
3324 #endif