KVM: MMU: do not need atomicly to set/clear spte
[pandora-kernel.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  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11  *
12  * Authors:
13  *   Yaniv Kamay  <yaniv@qumranet.com>
14  *   Avi Kivity   <avi@qumranet.com>
15  *
16  * This work is licensed under the terms of the GNU GPL, version 2.  See
17  * the COPYING file in the top-level directory.
18  *
19  */
20
21 #include "irq.h"
22 #include "mmu.h"
23 #include "x86.h"
24 #include "kvm_cache_regs.h"
25 #include "x86.h"
26
27 #include <linux/kvm_host.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/mm.h>
31 #include <linux/highmem.h>
32 #include <linux/module.h>
33 #include <linux/swap.h>
34 #include <linux/hugetlb.h>
35 #include <linux/compiler.h>
36 #include <linux/srcu.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39
40 #include <asm/page.h>
41 #include <asm/cmpxchg.h>
42 #include <asm/io.h>
43 #include <asm/vmx.h>
44
45 /*
46  * When setting this variable to true it enables Two-Dimensional-Paging
47  * where the hardware walks 2 page tables:
48  * 1. the guest-virtual to guest-physical
49  * 2. while doing 1. it walks guest-physical to host-physical
50  * If the hardware supports that we don't need to do shadow paging.
51  */
52 bool tdp_enabled = false;
53
54 enum {
55         AUDIT_PRE_PAGE_FAULT,
56         AUDIT_POST_PAGE_FAULT,
57         AUDIT_PRE_PTE_WRITE,
58         AUDIT_POST_PTE_WRITE,
59         AUDIT_PRE_SYNC,
60         AUDIT_POST_SYNC
61 };
62
63 char *audit_point_name[] = {
64         "pre page fault",
65         "post page fault",
66         "pre pte write",
67         "post pte write",
68         "pre sync",
69         "post sync"
70 };
71
72 #undef MMU_DEBUG
73
74 #ifdef MMU_DEBUG
75
76 #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
77 #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
78
79 #else
80
81 #define pgprintk(x...) do { } while (0)
82 #define rmap_printk(x...) do { } while (0)
83
84 #endif
85
86 #ifdef MMU_DEBUG
87 static int dbg = 0;
88 module_param(dbg, bool, 0644);
89 #endif
90
91 static int oos_shadow = 1;
92 module_param(oos_shadow, bool, 0644);
93
94 #ifndef MMU_DEBUG
95 #define ASSERT(x) do { } while (0)
96 #else
97 #define ASSERT(x)                                                       \
98         if (!(x)) {                                                     \
99                 printk(KERN_WARNING "assertion failed %s:%d: %s\n",     \
100                        __FILE__, __LINE__, #x);                         \
101         }
102 #endif
103
104 #define PTE_PREFETCH_NUM                8
105
106 #define PT_FIRST_AVAIL_BITS_SHIFT 9
107 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
108
109 #define PT64_LEVEL_BITS 9
110
111 #define PT64_LEVEL_SHIFT(level) \
112                 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
113
114 #define PT64_INDEX(address, level)\
115         (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
116
117
118 #define PT32_LEVEL_BITS 10
119
120 #define PT32_LEVEL_SHIFT(level) \
121                 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
122
123 #define PT32_LVL_OFFSET_MASK(level) \
124         (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
125                                                 * PT32_LEVEL_BITS))) - 1))
126
127 #define PT32_INDEX(address, level)\
128         (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
129
130
131 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
132 #define PT64_DIR_BASE_ADDR_MASK \
133         (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
134 #define PT64_LVL_ADDR_MASK(level) \
135         (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136                                                 * PT64_LEVEL_BITS))) - 1))
137 #define PT64_LVL_OFFSET_MASK(level) \
138         (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
139                                                 * PT64_LEVEL_BITS))) - 1))
140
141 #define PT32_BASE_ADDR_MASK PAGE_MASK
142 #define PT32_DIR_BASE_ADDR_MASK \
143         (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
144 #define PT32_LVL_ADDR_MASK(level) \
145         (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
146                                             * PT32_LEVEL_BITS))) - 1))
147
148 #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
149                         | PT64_NX_MASK)
150
151 #define PTE_LIST_EXT 4
152
153 #define ACC_EXEC_MASK    1
154 #define ACC_WRITE_MASK   PT_WRITABLE_MASK
155 #define ACC_USER_MASK    PT_USER_MASK
156 #define ACC_ALL          (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
157
158 #include <trace/events/kvm.h>
159
160 #define CREATE_TRACE_POINTS
161 #include "mmutrace.h"
162
163 #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
164
165 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
166
167 struct pte_list_desc {
168         u64 *sptes[PTE_LIST_EXT];
169         struct pte_list_desc *more;
170 };
171
172 struct kvm_shadow_walk_iterator {
173         u64 addr;
174         hpa_t shadow_addr;
175         int level;
176         u64 *sptep;
177         unsigned index;
178 };
179
180 #define for_each_shadow_entry(_vcpu, _addr, _walker)    \
181         for (shadow_walk_init(&(_walker), _vcpu, _addr);        \
182              shadow_walk_okay(&(_walker));                      \
183              shadow_walk_next(&(_walker)))
184
185 static struct kmem_cache *pte_list_desc_cache;
186 static struct kmem_cache *mmu_page_header_cache;
187 static struct percpu_counter kvm_total_used_mmu_pages;
188
189 static u64 __read_mostly shadow_nx_mask;
190 static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
191 static u64 __read_mostly shadow_user_mask;
192 static u64 __read_mostly shadow_accessed_mask;
193 static u64 __read_mostly shadow_dirty_mask;
194
195 static inline u64 rsvd_bits(int s, int e)
196 {
197         return ((1ULL << (e - s + 1)) - 1) << s;
198 }
199
200 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
201                 u64 dirty_mask, u64 nx_mask, u64 x_mask)
202 {
203         shadow_user_mask = user_mask;
204         shadow_accessed_mask = accessed_mask;
205         shadow_dirty_mask = dirty_mask;
206         shadow_nx_mask = nx_mask;
207         shadow_x_mask = x_mask;
208 }
209 EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
210
211 static int is_cpuid_PSE36(void)
212 {
213         return 1;
214 }
215
216 static int is_nx(struct kvm_vcpu *vcpu)
217 {
218         return vcpu->arch.efer & EFER_NX;
219 }
220
221 static int is_shadow_present_pte(u64 pte)
222 {
223         return pte & PT_PRESENT_MASK;
224 }
225
226 static int is_large_pte(u64 pte)
227 {
228         return pte & PT_PAGE_SIZE_MASK;
229 }
230
231 static int is_dirty_gpte(unsigned long pte)
232 {
233         return pte & PT_DIRTY_MASK;
234 }
235
236 static int is_rmap_spte(u64 pte)
237 {
238         return is_shadow_present_pte(pte);
239 }
240
241 static int is_last_spte(u64 pte, int level)
242 {
243         if (level == PT_PAGE_TABLE_LEVEL)
244                 return 1;
245         if (is_large_pte(pte))
246                 return 1;
247         return 0;
248 }
249
250 static pfn_t spte_to_pfn(u64 pte)
251 {
252         return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
253 }
254
255 static gfn_t pse36_gfn_delta(u32 gpte)
256 {
257         int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
258
259         return (gpte & PT32_DIR_PSE36_MASK) << shift;
260 }
261
262 #ifdef CONFIG_X86_64
263 static void __set_spte(u64 *sptep, u64 spte)
264 {
265         *sptep = spte;
266 }
267
268 static void __update_clear_spte_fast(u64 *sptep, u64 spte)
269 {
270         *sptep = spte;
271 }
272
273 static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
274 {
275         return xchg(sptep, spte);
276 }
277 #else
278 union split_spte {
279         struct {
280                 u32 spte_low;
281                 u32 spte_high;
282         };
283         u64 spte;
284 };
285
286 static void __set_spte(u64 *sptep, u64 spte)
287 {
288         union split_spte *ssptep, sspte;
289
290         ssptep = (union split_spte *)sptep;
291         sspte = (union split_spte)spte;
292
293         ssptep->spte_high = sspte.spte_high;
294
295         /*
296          * If we map the spte from nonpresent to present, We should store
297          * the high bits firstly, then set present bit, so cpu can not
298          * fetch this spte while we are setting the spte.
299          */
300         smp_wmb();
301
302         ssptep->spte_low = sspte.spte_low;
303 }
304
305 static void __update_clear_spte_fast(u64 *sptep, u64 spte)
306 {
307         union split_spte *ssptep, sspte;
308
309         ssptep = (union split_spte *)sptep;
310         sspte = (union split_spte)spte;
311
312         ssptep->spte_low = sspte.spte_low;
313
314         /*
315          * If we map the spte from present to nonpresent, we should clear
316          * present bit firstly to avoid vcpu fetch the old high bits.
317          */
318         smp_wmb();
319
320         ssptep->spte_high = sspte.spte_high;
321 }
322
323 static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
324 {
325         union split_spte *ssptep, sspte, orig;
326
327         ssptep = (union split_spte *)sptep;
328         sspte = (union split_spte)spte;
329
330         /* xchg acts as a barrier before the setting of the high bits */
331         orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
332         orig.spte_high = ssptep->spte_high = sspte.spte_high;
333
334         return orig.spte;
335 }
336 #endif
337
338 static bool spte_has_volatile_bits(u64 spte)
339 {
340         if (!shadow_accessed_mask)
341                 return false;
342
343         if (!is_shadow_present_pte(spte))
344                 return false;
345
346         if ((spte & shadow_accessed_mask) &&
347               (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
348                 return false;
349
350         return true;
351 }
352
353 static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
354 {
355         return (old_spte & bit_mask) && !(new_spte & bit_mask);
356 }
357
358 /* Rules for using mmu_spte_set:
359  * Set the sptep from nonpresent to present.
360  * Note: the sptep being assigned *must* be either not present
361  * or in a state where the hardware will not attempt to update
362  * the spte.
363  */
364 static void mmu_spte_set(u64 *sptep, u64 new_spte)
365 {
366         WARN_ON(is_shadow_present_pte(*sptep));
367         __set_spte(sptep, new_spte);
368 }
369
370 /* Rules for using mmu_spte_update:
371  * Update the state bits, it means the mapped pfn is not changged.
372  */
373 static void mmu_spte_update(u64 *sptep, u64 new_spte)
374 {
375         u64 mask, old_spte = *sptep;
376
377         WARN_ON(!is_rmap_spte(new_spte));
378
379         if (!is_shadow_present_pte(old_spte))
380                 return mmu_spte_set(sptep, new_spte);
381
382         new_spte |= old_spte & shadow_dirty_mask;
383
384         mask = shadow_accessed_mask;
385         if (is_writable_pte(old_spte))
386                 mask |= shadow_dirty_mask;
387
388         if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
389                 __update_clear_spte_fast(sptep, new_spte);
390         else
391                 old_spte = __update_clear_spte_slow(sptep, new_spte);
392
393         if (!shadow_accessed_mask)
394                 return;
395
396         if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
397                 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
398         if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
399                 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
400 }
401
402 /*
403  * Rules for using mmu_spte_clear_track_bits:
404  * It sets the sptep from present to nonpresent, and track the
405  * state bits, it is used to clear the last level sptep.
406  */
407 static int mmu_spte_clear_track_bits(u64 *sptep)
408 {
409         pfn_t pfn;
410         u64 old_spte = *sptep;
411
412         if (!spte_has_volatile_bits(old_spte))
413                 __update_clear_spte_fast(sptep, 0ull);
414         else
415                 old_spte = __update_clear_spte_slow(sptep, 0ull);
416
417         if (!is_rmap_spte(old_spte))
418                 return 0;
419
420         pfn = spte_to_pfn(old_spte);
421         if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
422                 kvm_set_pfn_accessed(pfn);
423         if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
424                 kvm_set_pfn_dirty(pfn);
425         return 1;
426 }
427
428 /*
429  * Rules for using mmu_spte_clear_no_track:
430  * Directly clear spte without caring the state bits of sptep,
431  * it is used to set the upper level spte.
432  */
433 static void mmu_spte_clear_no_track(u64 *sptep)
434 {
435         __update_clear_spte_fast(sptep, 0ull);
436 }
437
438 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
439                                   struct kmem_cache *base_cache, int min)
440 {
441         void *obj;
442
443         if (cache->nobjs >= min)
444                 return 0;
445         while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
446                 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
447                 if (!obj)
448                         return -ENOMEM;
449                 cache->objects[cache->nobjs++] = obj;
450         }
451         return 0;
452 }
453
454 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
455                                   struct kmem_cache *cache)
456 {
457         while (mc->nobjs)
458                 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
459 }
460
461 static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
462                                        int min)
463 {
464         void *page;
465
466         if (cache->nobjs >= min)
467                 return 0;
468         while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
469                 page = (void *)__get_free_page(GFP_KERNEL);
470                 if (!page)
471                         return -ENOMEM;
472                 cache->objects[cache->nobjs++] = page;
473         }
474         return 0;
475 }
476
477 static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
478 {
479         while (mc->nobjs)
480                 free_page((unsigned long)mc->objects[--mc->nobjs]);
481 }
482
483 static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
484 {
485         int r;
486
487         r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
488                                    pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
489         if (r)
490                 goto out;
491         r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
492         if (r)
493                 goto out;
494         r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
495                                    mmu_page_header_cache, 4);
496 out:
497         return r;
498 }
499
500 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
501 {
502         mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
503                                 pte_list_desc_cache);
504         mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
505         mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
506                                 mmu_page_header_cache);
507 }
508
509 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
510                                     size_t size)
511 {
512         void *p;
513
514         BUG_ON(!mc->nobjs);
515         p = mc->objects[--mc->nobjs];
516         return p;
517 }
518
519 static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
520 {
521         return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
522                                       sizeof(struct pte_list_desc));
523 }
524
525 static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
526 {
527         kmem_cache_free(pte_list_desc_cache, pte_list_desc);
528 }
529
530 static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
531 {
532         if (!sp->role.direct)
533                 return sp->gfns[index];
534
535         return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
536 }
537
538 static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
539 {
540         if (sp->role.direct)
541                 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
542         else
543                 sp->gfns[index] = gfn;
544 }
545
546 /*
547  * Return the pointer to the large page information for a given gfn,
548  * handling slots that are not large page aligned.
549  */
550 static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
551                                               struct kvm_memory_slot *slot,
552                                               int level)
553 {
554         unsigned long idx;
555
556         idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
557               (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
558         return &slot->lpage_info[level - 2][idx];
559 }
560
561 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
562 {
563         struct kvm_memory_slot *slot;
564         struct kvm_lpage_info *linfo;
565         int i;
566
567         slot = gfn_to_memslot(kvm, gfn);
568         for (i = PT_DIRECTORY_LEVEL;
569              i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
570                 linfo = lpage_info_slot(gfn, slot, i);
571                 linfo->write_count += 1;
572         }
573         kvm->arch.indirect_shadow_pages++;
574 }
575
576 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
577 {
578         struct kvm_memory_slot *slot;
579         struct kvm_lpage_info *linfo;
580         int i;
581
582         slot = gfn_to_memslot(kvm, gfn);
583         for (i = PT_DIRECTORY_LEVEL;
584              i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
585                 linfo = lpage_info_slot(gfn, slot, i);
586                 linfo->write_count -= 1;
587                 WARN_ON(linfo->write_count < 0);
588         }
589         kvm->arch.indirect_shadow_pages--;
590 }
591
592 static int has_wrprotected_page(struct kvm *kvm,
593                                 gfn_t gfn,
594                                 int level)
595 {
596         struct kvm_memory_slot *slot;
597         struct kvm_lpage_info *linfo;
598
599         slot = gfn_to_memslot(kvm, gfn);
600         if (slot) {
601                 linfo = lpage_info_slot(gfn, slot, level);
602                 return linfo->write_count;
603         }
604
605         return 1;
606 }
607
608 static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
609 {
610         unsigned long page_size;
611         int i, ret = 0;
612
613         page_size = kvm_host_page_size(kvm, gfn);
614
615         for (i = PT_PAGE_TABLE_LEVEL;
616              i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
617                 if (page_size >= KVM_HPAGE_SIZE(i))
618                         ret = i;
619                 else
620                         break;
621         }
622
623         return ret;
624 }
625
626 static struct kvm_memory_slot *
627 gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
628                             bool no_dirty_log)
629 {
630         struct kvm_memory_slot *slot;
631
632         slot = gfn_to_memslot(vcpu->kvm, gfn);
633         if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
634               (no_dirty_log && slot->dirty_bitmap))
635                 slot = NULL;
636
637         return slot;
638 }
639
640 static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
641 {
642         return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
643 }
644
645 static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
646 {
647         int host_level, level, max_level;
648
649         host_level = host_mapping_level(vcpu->kvm, large_gfn);
650
651         if (host_level == PT_PAGE_TABLE_LEVEL)
652                 return host_level;
653
654         max_level = kvm_x86_ops->get_lpage_level() < host_level ?
655                 kvm_x86_ops->get_lpage_level() : host_level;
656
657         for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
658                 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
659                         break;
660
661         return level - 1;
662 }
663
664 /*
665  * Pte mapping structures:
666  *
667  * If pte_list bit zero is zero, then pte_list point to the spte.
668  *
669  * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
670  * pte_list_desc containing more mappings.
671  *
672  * Returns the number of pte entries before the spte was added or zero if
673  * the spte was not added.
674  *
675  */
676 static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
677                         unsigned long *pte_list)
678 {
679         struct pte_list_desc *desc;
680         int i, count = 0;
681
682         if (!*pte_list) {
683                 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
684                 *pte_list = (unsigned long)spte;
685         } else if (!(*pte_list & 1)) {
686                 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
687                 desc = mmu_alloc_pte_list_desc(vcpu);
688                 desc->sptes[0] = (u64 *)*pte_list;
689                 desc->sptes[1] = spte;
690                 *pte_list = (unsigned long)desc | 1;
691                 ++count;
692         } else {
693                 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
694                 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
695                 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
696                         desc = desc->more;
697                         count += PTE_LIST_EXT;
698                 }
699                 if (desc->sptes[PTE_LIST_EXT-1]) {
700                         desc->more = mmu_alloc_pte_list_desc(vcpu);
701                         desc = desc->more;
702                 }
703                 for (i = 0; desc->sptes[i]; ++i)
704                         ++count;
705                 desc->sptes[i] = spte;
706         }
707         return count;
708 }
709
710 static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
711 {
712         struct pte_list_desc *desc;
713         u64 *prev_spte;
714         int i;
715
716         if (!*pte_list)
717                 return NULL;
718         else if (!(*pte_list & 1)) {
719                 if (!spte)
720                         return (u64 *)*pte_list;
721                 return NULL;
722         }
723         desc = (struct pte_list_desc *)(*pte_list & ~1ul);
724         prev_spte = NULL;
725         while (desc) {
726                 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
727                         if (prev_spte == spte)
728                                 return desc->sptes[i];
729                         prev_spte = desc->sptes[i];
730                 }
731                 desc = desc->more;
732         }
733         return NULL;
734 }
735
736 static void
737 pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
738                            int i, struct pte_list_desc *prev_desc)
739 {
740         int j;
741
742         for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
743                 ;
744         desc->sptes[i] = desc->sptes[j];
745         desc->sptes[j] = NULL;
746         if (j != 0)
747                 return;
748         if (!prev_desc && !desc->more)
749                 *pte_list = (unsigned long)desc->sptes[0];
750         else
751                 if (prev_desc)
752                         prev_desc->more = desc->more;
753                 else
754                         *pte_list = (unsigned long)desc->more | 1;
755         mmu_free_pte_list_desc(desc);
756 }
757
758 static void pte_list_remove(u64 *spte, unsigned long *pte_list)
759 {
760         struct pte_list_desc *desc;
761         struct pte_list_desc *prev_desc;
762         int i;
763
764         if (!*pte_list) {
765                 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
766                 BUG();
767         } else if (!(*pte_list & 1)) {
768                 rmap_printk("pte_list_remove:  %p 1->0\n", spte);
769                 if ((u64 *)*pte_list != spte) {
770                         printk(KERN_ERR "pte_list_remove:  %p 1->BUG\n", spte);
771                         BUG();
772                 }
773                 *pte_list = 0;
774         } else {
775                 rmap_printk("pte_list_remove:  %p many->many\n", spte);
776                 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
777                 prev_desc = NULL;
778                 while (desc) {
779                         for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
780                                 if (desc->sptes[i] == spte) {
781                                         pte_list_desc_remove_entry(pte_list,
782                                                                desc, i,
783                                                                prev_desc);
784                                         return;
785                                 }
786                         prev_desc = desc;
787                         desc = desc->more;
788                 }
789                 pr_err("pte_list_remove: %p many->many\n", spte);
790                 BUG();
791         }
792 }
793
794 typedef void (*pte_list_walk_fn) (u64 *spte);
795 static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
796 {
797         struct pte_list_desc *desc;
798         int i;
799
800         if (!*pte_list)
801                 return;
802
803         if (!(*pte_list & 1))
804                 return fn((u64 *)*pte_list);
805
806         desc = (struct pte_list_desc *)(*pte_list & ~1ul);
807         while (desc) {
808                 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
809                         fn(desc->sptes[i]);
810                 desc = desc->more;
811         }
812 }
813
814 /*
815  * Take gfn and return the reverse mapping to it.
816  */
817 static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
818 {
819         struct kvm_memory_slot *slot;
820         struct kvm_lpage_info *linfo;
821
822         slot = gfn_to_memslot(kvm, gfn);
823         if (likely(level == PT_PAGE_TABLE_LEVEL))
824                 return &slot->rmap[gfn - slot->base_gfn];
825
826         linfo = lpage_info_slot(gfn, slot, level);
827
828         return &linfo->rmap_pde;
829 }
830
831 static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
832 {
833         struct kvm_mmu_page *sp;
834         unsigned long *rmapp;
835
836         sp = page_header(__pa(spte));
837         kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
838         rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
839         return pte_list_add(vcpu, spte, rmapp);
840 }
841
842 static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
843 {
844         return pte_list_next(rmapp, spte);
845 }
846
847 static void rmap_remove(struct kvm *kvm, u64 *spte)
848 {
849         struct kvm_mmu_page *sp;
850         gfn_t gfn;
851         unsigned long *rmapp;
852
853         sp = page_header(__pa(spte));
854         gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
855         rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
856         pte_list_remove(spte, rmapp);
857 }
858
859 static void drop_spte(struct kvm *kvm, u64 *sptep)
860 {
861         if (mmu_spte_clear_track_bits(sptep))
862                 rmap_remove(kvm, sptep);
863 }
864
865 static int rmap_write_protect(struct kvm *kvm, u64 gfn)
866 {
867         unsigned long *rmapp;
868         u64 *spte;
869         int i, write_protected = 0;
870
871         rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
872
873         spte = rmap_next(kvm, rmapp, NULL);
874         while (spte) {
875                 BUG_ON(!spte);
876                 BUG_ON(!(*spte & PT_PRESENT_MASK));
877                 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
878                 if (is_writable_pte(*spte)) {
879                         mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK);
880                         write_protected = 1;
881                 }
882                 spte = rmap_next(kvm, rmapp, spte);
883         }
884
885         /* check for huge page mappings */
886         for (i = PT_DIRECTORY_LEVEL;
887              i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
888                 rmapp = gfn_to_rmap(kvm, gfn, i);
889                 spte = rmap_next(kvm, rmapp, NULL);
890                 while (spte) {
891                         BUG_ON(!spte);
892                         BUG_ON(!(*spte & PT_PRESENT_MASK));
893                         BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
894                         pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
895                         if (is_writable_pte(*spte)) {
896                                 drop_spte(kvm, spte);
897                                 --kvm->stat.lpages;
898                                 spte = NULL;
899                                 write_protected = 1;
900                         }
901                         spte = rmap_next(kvm, rmapp, spte);
902                 }
903         }
904
905         return write_protected;
906 }
907
908 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
909                            unsigned long data)
910 {
911         u64 *spte;
912         int need_tlb_flush = 0;
913
914         while ((spte = rmap_next(kvm, rmapp, NULL))) {
915                 BUG_ON(!(*spte & PT_PRESENT_MASK));
916                 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
917                 drop_spte(kvm, spte);
918                 need_tlb_flush = 1;
919         }
920         return need_tlb_flush;
921 }
922
923 static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
924                              unsigned long data)
925 {
926         int need_flush = 0;
927         u64 *spte, new_spte;
928         pte_t *ptep = (pte_t *)data;
929         pfn_t new_pfn;
930
931         WARN_ON(pte_huge(*ptep));
932         new_pfn = pte_pfn(*ptep);
933         spte = rmap_next(kvm, rmapp, NULL);
934         while (spte) {
935                 BUG_ON(!is_shadow_present_pte(*spte));
936                 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
937                 need_flush = 1;
938                 if (pte_write(*ptep)) {
939                         drop_spte(kvm, spte);
940                         spte = rmap_next(kvm, rmapp, NULL);
941                 } else {
942                         new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
943                         new_spte |= (u64)new_pfn << PAGE_SHIFT;
944
945                         new_spte &= ~PT_WRITABLE_MASK;
946                         new_spte &= ~SPTE_HOST_WRITEABLE;
947                         new_spte &= ~shadow_accessed_mask;
948                         mmu_spte_clear_track_bits(spte);
949                         mmu_spte_set(spte, new_spte);
950                         spte = rmap_next(kvm, rmapp, spte);
951                 }
952         }
953         if (need_flush)
954                 kvm_flush_remote_tlbs(kvm);
955
956         return 0;
957 }
958
959 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
960                           unsigned long data,
961                           int (*handler)(struct kvm *kvm, unsigned long *rmapp,
962                                          unsigned long data))
963 {
964         int i, j;
965         int ret;
966         int retval = 0;
967         struct kvm_memslots *slots;
968
969         slots = kvm_memslots(kvm);
970
971         for (i = 0; i < slots->nmemslots; i++) {
972                 struct kvm_memory_slot *memslot = &slots->memslots[i];
973                 unsigned long start = memslot->userspace_addr;
974                 unsigned long end;
975
976                 end = start + (memslot->npages << PAGE_SHIFT);
977                 if (hva >= start && hva < end) {
978                         gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
979                         gfn_t gfn = memslot->base_gfn + gfn_offset;
980
981                         ret = handler(kvm, &memslot->rmap[gfn_offset], data);
982
983                         for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
984                                 struct kvm_lpage_info *linfo;
985
986                                 linfo = lpage_info_slot(gfn, memslot,
987                                                         PT_DIRECTORY_LEVEL + j);
988                                 ret |= handler(kvm, &linfo->rmap_pde, data);
989                         }
990                         trace_kvm_age_page(hva, memslot, ret);
991                         retval |= ret;
992                 }
993         }
994
995         return retval;
996 }
997
998 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
999 {
1000         return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
1001 }
1002
1003 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1004 {
1005         kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
1006 }
1007
1008 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1009                          unsigned long data)
1010 {
1011         u64 *spte;
1012         int young = 0;
1013
1014         /*
1015          * Emulate the accessed bit for EPT, by checking if this page has
1016          * an EPT mapping, and clearing it if it does. On the next access,
1017          * a new EPT mapping will be established.
1018          * This has some overhead, but not as much as the cost of swapping
1019          * out actively used pages or breaking up actively used hugepages.
1020          */
1021         if (!shadow_accessed_mask)
1022                 return kvm_unmap_rmapp(kvm, rmapp, data);
1023
1024         spte = rmap_next(kvm, rmapp, NULL);
1025         while (spte) {
1026                 int _young;
1027                 u64 _spte = *spte;
1028                 BUG_ON(!(_spte & PT_PRESENT_MASK));
1029                 _young = _spte & PT_ACCESSED_MASK;
1030                 if (_young) {
1031                         young = 1;
1032                         clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1033                 }
1034                 spte = rmap_next(kvm, rmapp, spte);
1035         }
1036         return young;
1037 }
1038
1039 static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1040                               unsigned long data)
1041 {
1042         u64 *spte;
1043         int young = 0;
1044
1045         /*
1046          * If there's no access bit in the secondary pte set by the
1047          * hardware it's up to gup-fast/gup to set the access bit in
1048          * the primary pte or in the page structure.
1049          */
1050         if (!shadow_accessed_mask)
1051                 goto out;
1052
1053         spte = rmap_next(kvm, rmapp, NULL);
1054         while (spte) {
1055                 u64 _spte = *spte;
1056                 BUG_ON(!(_spte & PT_PRESENT_MASK));
1057                 young = _spte & PT_ACCESSED_MASK;
1058                 if (young) {
1059                         young = 1;
1060                         break;
1061                 }
1062                 spte = rmap_next(kvm, rmapp, spte);
1063         }
1064 out:
1065         return young;
1066 }
1067
1068 #define RMAP_RECYCLE_THRESHOLD 1000
1069
1070 static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
1071 {
1072         unsigned long *rmapp;
1073         struct kvm_mmu_page *sp;
1074
1075         sp = page_header(__pa(spte));
1076
1077         rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
1078
1079         kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
1080         kvm_flush_remote_tlbs(vcpu->kvm);
1081 }
1082
1083 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1084 {
1085         return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
1086 }
1087
1088 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1089 {
1090         return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1091 }
1092
1093 #ifdef MMU_DEBUG
1094 static int is_empty_shadow_page(u64 *spt)
1095 {
1096         u64 *pos;
1097         u64 *end;
1098
1099         for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
1100                 if (is_shadow_present_pte(*pos)) {
1101                         printk(KERN_ERR "%s: %p %llx\n", __func__,
1102                                pos, *pos);
1103                         return 0;
1104                 }
1105         return 1;
1106 }
1107 #endif
1108
1109 /*
1110  * This value is the sum of all of the kvm instances's
1111  * kvm->arch.n_used_mmu_pages values.  We need a global,
1112  * aggregate version in order to make the slab shrinker
1113  * faster
1114  */
1115 static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1116 {
1117         kvm->arch.n_used_mmu_pages += nr;
1118         percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1119 }
1120
1121 /*
1122  * Remove the sp from shadow page cache, after call it,
1123  * we can not find this sp from the cache, and the shadow
1124  * page table is still valid.
1125  * It should be under the protection of mmu lock.
1126  */
1127 static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
1128 {
1129         ASSERT(is_empty_shadow_page(sp->spt));
1130         hlist_del(&sp->hash_link);
1131         if (!sp->role.direct)
1132                 free_page((unsigned long)sp->gfns);
1133 }
1134
1135 /*
1136  * Free the shadow page table and the sp, we can do it
1137  * out of the protection of mmu lock.
1138  */
1139 static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
1140 {
1141         list_del(&sp->link);
1142         free_page((unsigned long)sp->spt);
1143         kmem_cache_free(mmu_page_header_cache, sp);
1144 }
1145
1146 static unsigned kvm_page_table_hashfn(gfn_t gfn)
1147 {
1148         return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
1149 }
1150
1151 static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1152                                     struct kvm_mmu_page *sp, u64 *parent_pte)
1153 {
1154         if (!parent_pte)
1155                 return;
1156
1157         pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1158 }
1159
1160 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1161                                        u64 *parent_pte)
1162 {
1163         pte_list_remove(parent_pte, &sp->parent_ptes);
1164 }
1165
1166 static void drop_parent_pte(struct kvm_mmu_page *sp,
1167                             u64 *parent_pte)
1168 {
1169         mmu_page_remove_parent_pte(sp, parent_pte);
1170         mmu_spte_clear_no_track(parent_pte);
1171 }
1172
1173 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
1174                                                u64 *parent_pte, int direct)
1175 {
1176         struct kvm_mmu_page *sp;
1177         sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1178                                         sizeof *sp);
1179         sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
1180         if (!direct)
1181                 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1182                                                   PAGE_SIZE);
1183         set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
1184         list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
1185         bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
1186         sp->parent_ptes = 0;
1187         mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1188         kvm_mod_used_mmu_pages(vcpu->kvm, +1);
1189         return sp;
1190 }
1191
1192 static void mark_unsync(u64 *spte);
1193 static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
1194 {
1195         pte_list_walk(&sp->parent_ptes, mark_unsync);
1196 }
1197
1198 static void mark_unsync(u64 *spte)
1199 {
1200         struct kvm_mmu_page *sp;
1201         unsigned int index;
1202
1203         sp = page_header(__pa(spte));
1204         index = spte - sp->spt;
1205         if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1206                 return;
1207         if (sp->unsync_children++)
1208                 return;
1209         kvm_mmu_mark_parents_unsync(sp);
1210 }
1211
1212 static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1213                                struct kvm_mmu_page *sp)
1214 {
1215         return 1;
1216 }
1217
1218 static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1219 {
1220 }
1221
1222 static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1223                                  struct kvm_mmu_page *sp, u64 *spte,
1224                                  const void *pte)
1225 {
1226         WARN_ON(1);
1227 }
1228
1229 #define KVM_PAGE_ARRAY_NR 16
1230
1231 struct kvm_mmu_pages {
1232         struct mmu_page_and_offset {
1233                 struct kvm_mmu_page *sp;
1234                 unsigned int idx;
1235         } page[KVM_PAGE_ARRAY_NR];
1236         unsigned int nr;
1237 };
1238
1239 #define for_each_unsync_children(bitmap, idx)           \
1240         for (idx = find_first_bit(bitmap, 512);         \
1241              idx < 512;                                 \
1242              idx = find_next_bit(bitmap, 512, idx+1))
1243
1244 static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1245                          int idx)
1246 {
1247         int i;
1248
1249         if (sp->unsync)
1250                 for (i=0; i < pvec->nr; i++)
1251                         if (pvec->page[i].sp == sp)
1252                                 return 0;
1253
1254         pvec->page[pvec->nr].sp = sp;
1255         pvec->page[pvec->nr].idx = idx;
1256         pvec->nr++;
1257         return (pvec->nr == KVM_PAGE_ARRAY_NR);
1258 }
1259
1260 static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1261                            struct kvm_mmu_pages *pvec)
1262 {
1263         int i, ret, nr_unsync_leaf = 0;
1264
1265         for_each_unsync_children(sp->unsync_child_bitmap, i) {
1266                 struct kvm_mmu_page *child;
1267                 u64 ent = sp->spt[i];
1268
1269                 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1270                         goto clear_child_bitmap;
1271
1272                 child = page_header(ent & PT64_BASE_ADDR_MASK);
1273
1274                 if (child->unsync_children) {
1275                         if (mmu_pages_add(pvec, child, i))
1276                                 return -ENOSPC;
1277
1278                         ret = __mmu_unsync_walk(child, pvec);
1279                         if (!ret)
1280                                 goto clear_child_bitmap;
1281                         else if (ret > 0)
1282                                 nr_unsync_leaf += ret;
1283                         else
1284                                 return ret;
1285                 } else if (child->unsync) {
1286                         nr_unsync_leaf++;
1287                         if (mmu_pages_add(pvec, child, i))
1288                                 return -ENOSPC;
1289                 } else
1290                          goto clear_child_bitmap;
1291
1292                 continue;
1293
1294 clear_child_bitmap:
1295                 __clear_bit(i, sp->unsync_child_bitmap);
1296                 sp->unsync_children--;
1297                 WARN_ON((int)sp->unsync_children < 0);
1298         }
1299
1300
1301         return nr_unsync_leaf;
1302 }
1303
1304 static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1305                            struct kvm_mmu_pages *pvec)
1306 {
1307         if (!sp->unsync_children)
1308                 return 0;
1309
1310         mmu_pages_add(pvec, sp, 0);
1311         return __mmu_unsync_walk(sp, pvec);
1312 }
1313
1314 static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1315 {
1316         WARN_ON(!sp->unsync);
1317         trace_kvm_mmu_sync_page(sp);
1318         sp->unsync = 0;
1319         --kvm->stat.mmu_unsync;
1320 }
1321
1322 static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1323                                     struct list_head *invalid_list);
1324 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1325                                     struct list_head *invalid_list);
1326
1327 #define for_each_gfn_sp(kvm, sp, gfn, pos)                              \
1328   hlist_for_each_entry(sp, pos,                                         \
1329    &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link)   \
1330         if ((sp)->gfn != (gfn)) {} else
1331
1332 #define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos)               \
1333   hlist_for_each_entry(sp, pos,                                         \
1334    &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link)   \
1335                 if ((sp)->gfn != (gfn) || (sp)->role.direct ||          \
1336                         (sp)->role.invalid) {} else
1337
1338 /* @sp->gfn should be write-protected at the call site */
1339 static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1340                            struct list_head *invalid_list, bool clear_unsync)
1341 {
1342         if (sp->role.cr4_pae != !!is_pae(vcpu)) {
1343                 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
1344                 return 1;
1345         }
1346
1347         if (clear_unsync)
1348                 kvm_unlink_unsync_page(vcpu->kvm, sp);
1349
1350         if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1351                 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
1352                 return 1;
1353         }
1354
1355         kvm_mmu_flush_tlb(vcpu);
1356         return 0;
1357 }
1358
1359 static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1360                                    struct kvm_mmu_page *sp)
1361 {
1362         LIST_HEAD(invalid_list);
1363         int ret;
1364
1365         ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
1366         if (ret)
1367                 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1368
1369         return ret;
1370 }
1371
1372 static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1373                          struct list_head *invalid_list)
1374 {
1375         return __kvm_sync_page(vcpu, sp, invalid_list, true);
1376 }
1377
1378 /* @gfn should be write-protected at the call site */
1379 static void kvm_sync_pages(struct kvm_vcpu *vcpu,  gfn_t gfn)
1380 {
1381         struct kvm_mmu_page *s;
1382         struct hlist_node *node;
1383         LIST_HEAD(invalid_list);
1384         bool flush = false;
1385
1386         for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
1387                 if (!s->unsync)
1388                         continue;
1389
1390                 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1391                 kvm_unlink_unsync_page(vcpu->kvm, s);
1392                 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
1393                         (vcpu->arch.mmu.sync_page(vcpu, s))) {
1394                         kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
1395                         continue;
1396                 }
1397                 flush = true;
1398         }
1399
1400         kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1401         if (flush)
1402                 kvm_mmu_flush_tlb(vcpu);
1403 }
1404
1405 struct mmu_page_path {
1406         struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1407         unsigned int idx[PT64_ROOT_LEVEL-1];
1408 };
1409
1410 #define for_each_sp(pvec, sp, parents, i)                       \
1411                 for (i = mmu_pages_next(&pvec, &parents, -1),   \
1412                         sp = pvec.page[i].sp;                   \
1413                         i < pvec.nr && ({ sp = pvec.page[i].sp; 1;});   \
1414                         i = mmu_pages_next(&pvec, &parents, i))
1415
1416 static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1417                           struct mmu_page_path *parents,
1418                           int i)
1419 {
1420         int n;
1421
1422         for (n = i+1; n < pvec->nr; n++) {
1423                 struct kvm_mmu_page *sp = pvec->page[n].sp;
1424
1425                 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1426                         parents->idx[0] = pvec->page[n].idx;
1427                         return n;
1428                 }
1429
1430                 parents->parent[sp->role.level-2] = sp;
1431                 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1432         }
1433
1434         return n;
1435 }
1436
1437 static void mmu_pages_clear_parents(struct mmu_page_path *parents)
1438 {
1439         struct kvm_mmu_page *sp;
1440         unsigned int level = 0;
1441
1442         do {
1443                 unsigned int idx = parents->idx[level];
1444
1445                 sp = parents->parent[level];
1446                 if (!sp)
1447                         return;
1448
1449                 --sp->unsync_children;
1450                 WARN_ON((int)sp->unsync_children < 0);
1451                 __clear_bit(idx, sp->unsync_child_bitmap);
1452                 level++;
1453         } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1454 }
1455
1456 static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1457                                struct mmu_page_path *parents,
1458                                struct kvm_mmu_pages *pvec)
1459 {
1460         parents->parent[parent->role.level-1] = NULL;
1461         pvec->nr = 0;
1462 }
1463
1464 static void mmu_sync_children(struct kvm_vcpu *vcpu,
1465                               struct kvm_mmu_page *parent)
1466 {
1467         int i;
1468         struct kvm_mmu_page *sp;
1469         struct mmu_page_path parents;
1470         struct kvm_mmu_pages pages;
1471         LIST_HEAD(invalid_list);
1472
1473         kvm_mmu_pages_init(parent, &parents, &pages);
1474         while (mmu_unsync_walk(parent, &pages)) {
1475                 int protected = 0;
1476
1477                 for_each_sp(pages, sp, parents, i)
1478                         protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1479
1480                 if (protected)
1481                         kvm_flush_remote_tlbs(vcpu->kvm);
1482
1483                 for_each_sp(pages, sp, parents, i) {
1484                         kvm_sync_page(vcpu, sp, &invalid_list);
1485                         mmu_pages_clear_parents(&parents);
1486                 }
1487                 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1488                 cond_resched_lock(&vcpu->kvm->mmu_lock);
1489                 kvm_mmu_pages_init(parent, &parents, &pages);
1490         }
1491 }
1492
1493 static void init_shadow_page_table(struct kvm_mmu_page *sp)
1494 {
1495         int i;
1496
1497         for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1498                 sp->spt[i] = 0ull;
1499 }
1500
1501 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1502                                              gfn_t gfn,
1503                                              gva_t gaddr,
1504                                              unsigned level,
1505                                              int direct,
1506                                              unsigned access,
1507                                              u64 *parent_pte)
1508 {
1509         union kvm_mmu_page_role role;
1510         unsigned quadrant;
1511         struct kvm_mmu_page *sp;
1512         struct hlist_node *node;
1513         bool need_sync = false;
1514
1515         role = vcpu->arch.mmu.base_role;
1516         role.level = level;
1517         role.direct = direct;
1518         if (role.direct)
1519                 role.cr4_pae = 0;
1520         role.access = access;
1521         if (!vcpu->arch.mmu.direct_map
1522             && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
1523                 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1524                 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1525                 role.quadrant = quadrant;
1526         }
1527         for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
1528                 if (!need_sync && sp->unsync)
1529                         need_sync = true;
1530
1531                 if (sp->role.word != role.word)
1532                         continue;
1533
1534                 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1535                         break;
1536
1537                 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1538                 if (sp->unsync_children) {
1539                         kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
1540                         kvm_mmu_mark_parents_unsync(sp);
1541                 } else if (sp->unsync)
1542                         kvm_mmu_mark_parents_unsync(sp);
1543
1544                 trace_kvm_mmu_get_page(sp, false);
1545                 return sp;
1546         }
1547         ++vcpu->kvm->stat.mmu_cache_miss;
1548         sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
1549         if (!sp)
1550                 return sp;
1551         sp->gfn = gfn;
1552         sp->role = role;
1553         hlist_add_head(&sp->hash_link,
1554                 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
1555         if (!direct) {
1556                 if (rmap_write_protect(vcpu->kvm, gfn))
1557                         kvm_flush_remote_tlbs(vcpu->kvm);
1558                 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1559                         kvm_sync_pages(vcpu, gfn);
1560
1561                 account_shadowed(vcpu->kvm, gfn);
1562         }
1563         init_shadow_page_table(sp);
1564         trace_kvm_mmu_get_page(sp, true);
1565         return sp;
1566 }
1567
1568 static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1569                              struct kvm_vcpu *vcpu, u64 addr)
1570 {
1571         iterator->addr = addr;
1572         iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1573         iterator->level = vcpu->arch.mmu.shadow_root_level;
1574
1575         if (iterator->level == PT64_ROOT_LEVEL &&
1576             vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1577             !vcpu->arch.mmu.direct_map)
1578                 --iterator->level;
1579
1580         if (iterator->level == PT32E_ROOT_LEVEL) {
1581                 iterator->shadow_addr
1582                         = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1583                 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1584                 --iterator->level;
1585                 if (!iterator->shadow_addr)
1586                         iterator->level = 0;
1587         }
1588 }
1589
1590 static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1591 {
1592         if (iterator->level < PT_PAGE_TABLE_LEVEL)
1593                 return false;
1594
1595         iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1596         iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1597         return true;
1598 }
1599
1600 static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1601 {
1602         if (is_last_spte(*iterator->sptep, iterator->level)) {
1603                 iterator->level = 0;
1604                 return;
1605         }
1606
1607         iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1608         --iterator->level;
1609 }
1610
1611 static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1612 {
1613         u64 spte;
1614
1615         spte = __pa(sp->spt)
1616                 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1617                 | PT_WRITABLE_MASK | PT_USER_MASK;
1618         mmu_spte_set(sptep, spte);
1619 }
1620
1621 static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1622 {
1623         if (is_large_pte(*sptep)) {
1624                 drop_spte(vcpu->kvm, sptep);
1625                 kvm_flush_remote_tlbs(vcpu->kvm);
1626         }
1627 }
1628
1629 static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1630                                    unsigned direct_access)
1631 {
1632         if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1633                 struct kvm_mmu_page *child;
1634
1635                 /*
1636                  * For the direct sp, if the guest pte's dirty bit
1637                  * changed form clean to dirty, it will corrupt the
1638                  * sp's access: allow writable in the read-only sp,
1639                  * so we should update the spte at this point to get
1640                  * a new sp with the correct access.
1641                  */
1642                 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1643                 if (child->role.access == direct_access)
1644                         return;
1645
1646                 drop_parent_pte(child, sptep);
1647                 kvm_flush_remote_tlbs(vcpu->kvm);
1648         }
1649 }
1650
1651 static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1652                              u64 *spte)
1653 {
1654         u64 pte;
1655         struct kvm_mmu_page *child;
1656
1657         pte = *spte;
1658         if (is_shadow_present_pte(pte)) {
1659                 if (is_last_spte(pte, sp->role.level))
1660                         drop_spte(kvm, spte);
1661                 else {
1662                         child = page_header(pte & PT64_BASE_ADDR_MASK);
1663                         drop_parent_pte(child, spte);
1664                 }
1665         }
1666
1667         if (is_large_pte(pte))
1668                 --kvm->stat.lpages;
1669 }
1670
1671 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
1672                                          struct kvm_mmu_page *sp)
1673 {
1674         unsigned i;
1675
1676         for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1677                 mmu_page_zap_pte(kvm, sp, sp->spt + i);
1678 }
1679
1680 static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
1681 {
1682         mmu_page_remove_parent_pte(sp, parent_pte);
1683 }
1684
1685 static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1686 {
1687         int i;
1688         struct kvm_vcpu *vcpu;
1689
1690         kvm_for_each_vcpu(i, vcpu, kvm)
1691                 vcpu->arch.last_pte_updated = NULL;
1692 }
1693
1694 static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
1695 {
1696         u64 *parent_pte;
1697
1698         while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1699                 drop_parent_pte(sp, parent_pte);
1700 }
1701
1702 static int mmu_zap_unsync_children(struct kvm *kvm,
1703                                    struct kvm_mmu_page *parent,
1704                                    struct list_head *invalid_list)
1705 {
1706         int i, zapped = 0;
1707         struct mmu_page_path parents;
1708         struct kvm_mmu_pages pages;
1709
1710         if (parent->role.level == PT_PAGE_TABLE_LEVEL)
1711                 return 0;
1712
1713         kvm_mmu_pages_init(parent, &parents, &pages);
1714         while (mmu_unsync_walk(parent, &pages)) {
1715                 struct kvm_mmu_page *sp;
1716
1717                 for_each_sp(pages, sp, parents, i) {
1718                         kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
1719                         mmu_pages_clear_parents(&parents);
1720                         zapped++;
1721                 }
1722                 kvm_mmu_pages_init(parent, &parents, &pages);
1723         }
1724
1725         return zapped;
1726 }
1727
1728 static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1729                                     struct list_head *invalid_list)
1730 {
1731         int ret;
1732
1733         trace_kvm_mmu_prepare_zap_page(sp);
1734         ++kvm->stat.mmu_shadow_zapped;
1735         ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
1736         kvm_mmu_page_unlink_children(kvm, sp);
1737         kvm_mmu_unlink_parents(kvm, sp);
1738         if (!sp->role.invalid && !sp->role.direct)
1739                 unaccount_shadowed(kvm, sp->gfn);
1740         if (sp->unsync)
1741                 kvm_unlink_unsync_page(kvm, sp);
1742         if (!sp->root_count) {
1743                 /* Count self */
1744                 ret++;
1745                 list_move(&sp->link, invalid_list);
1746                 kvm_mod_used_mmu_pages(kvm, -1);
1747         } else {
1748                 list_move(&sp->link, &kvm->arch.active_mmu_pages);
1749                 kvm_reload_remote_mmus(kvm);
1750         }
1751
1752         sp->role.invalid = 1;
1753         kvm_mmu_reset_last_pte_updated(kvm);
1754         return ret;
1755 }
1756
1757 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1758                                     struct list_head *invalid_list)
1759 {
1760         struct kvm_mmu_page *sp;
1761
1762         if (list_empty(invalid_list))
1763                 return;
1764
1765         kvm_flush_remote_tlbs(kvm);
1766
1767         do {
1768                 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1769                 WARN_ON(!sp->role.invalid || sp->root_count);
1770                 kvm_mmu_isolate_page(sp);
1771                 kvm_mmu_free_page(sp);
1772         } while (!list_empty(invalid_list));
1773
1774 }
1775
1776 /*
1777  * Changing the number of mmu pages allocated to the vm
1778  * Note: if goal_nr_mmu_pages is too small, you will get dead lock
1779  */
1780 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
1781 {
1782         LIST_HEAD(invalid_list);
1783         /*
1784          * If we set the number of mmu pages to be smaller be than the
1785          * number of actived pages , we must to free some mmu pages before we
1786          * change the value
1787          */
1788
1789         if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1790                 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
1791                         !list_empty(&kvm->arch.active_mmu_pages)) {
1792                         struct kvm_mmu_page *page;
1793
1794                         page = container_of(kvm->arch.active_mmu_pages.prev,
1795                                             struct kvm_mmu_page, link);
1796                         kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1797                 }
1798                 kvm_mmu_commit_zap_page(kvm, &invalid_list);
1799                 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
1800         }
1801
1802         kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
1803 }
1804
1805 static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
1806 {
1807         struct kvm_mmu_page *sp;
1808         struct hlist_node *node;
1809         LIST_HEAD(invalid_list);
1810         int r;
1811
1812         pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
1813         r = 0;
1814
1815         for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
1816                 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
1817                          sp->role.word);
1818                 r = 1;
1819                 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
1820         }
1821         kvm_mmu_commit_zap_page(kvm, &invalid_list);
1822         return r;
1823 }
1824
1825 static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
1826 {
1827         struct kvm_mmu_page *sp;
1828         struct hlist_node *node;
1829         LIST_HEAD(invalid_list);
1830
1831         for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
1832                 pgprintk("%s: zap %llx %x\n",
1833                          __func__, gfn, sp->role.word);
1834                 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
1835         }
1836         kvm_mmu_commit_zap_page(kvm, &invalid_list);
1837 }
1838
1839 static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
1840 {
1841         int slot = memslot_id(kvm, gfn);
1842         struct kvm_mmu_page *sp = page_header(__pa(pte));
1843
1844         __set_bit(slot, sp->slot_bitmap);
1845 }
1846
1847 /*
1848  * The function is based on mtrr_type_lookup() in
1849  * arch/x86/kernel/cpu/mtrr/generic.c
1850  */
1851 static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1852                          u64 start, u64 end)
1853 {
1854         int i;
1855         u64 base, mask;
1856         u8 prev_match, curr_match;
1857         int num_var_ranges = KVM_NR_VAR_MTRR;
1858
1859         if (!mtrr_state->enabled)
1860                 return 0xFF;
1861
1862         /* Make end inclusive end, instead of exclusive */
1863         end--;
1864
1865         /* Look in fixed ranges. Just return the type as per start */
1866         if (mtrr_state->have_fixed && (start < 0x100000)) {
1867                 int idx;
1868
1869                 if (start < 0x80000) {
1870                         idx = 0;
1871                         idx += (start >> 16);
1872                         return mtrr_state->fixed_ranges[idx];
1873                 } else if (start < 0xC0000) {
1874                         idx = 1 * 8;
1875                         idx += ((start - 0x80000) >> 14);
1876                         return mtrr_state->fixed_ranges[idx];
1877                 } else if (start < 0x1000000) {
1878                         idx = 3 * 8;
1879                         idx += ((start - 0xC0000) >> 12);
1880                         return mtrr_state->fixed_ranges[idx];
1881                 }
1882         }
1883
1884         /*
1885          * Look in variable ranges
1886          * Look of multiple ranges matching this address and pick type
1887          * as per MTRR precedence
1888          */
1889         if (!(mtrr_state->enabled & 2))
1890                 return mtrr_state->def_type;
1891
1892         prev_match = 0xFF;
1893         for (i = 0; i < num_var_ranges; ++i) {
1894                 unsigned short start_state, end_state;
1895
1896                 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1897                         continue;
1898
1899                 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1900                        (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1901                 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1902                        (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1903
1904                 start_state = ((start & mask) == (base & mask));
1905                 end_state = ((end & mask) == (base & mask));
1906                 if (start_state != end_state)
1907                         return 0xFE;
1908
1909                 if ((start & mask) != (base & mask))
1910                         continue;
1911
1912                 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1913                 if (prev_match == 0xFF) {
1914                         prev_match = curr_match;
1915                         continue;
1916                 }
1917
1918                 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1919                     curr_match == MTRR_TYPE_UNCACHABLE)
1920                         return MTRR_TYPE_UNCACHABLE;
1921
1922                 if ((prev_match == MTRR_TYPE_WRBACK &&
1923                      curr_match == MTRR_TYPE_WRTHROUGH) ||
1924                     (prev_match == MTRR_TYPE_WRTHROUGH &&
1925                      curr_match == MTRR_TYPE_WRBACK)) {
1926                         prev_match = MTRR_TYPE_WRTHROUGH;
1927                         curr_match = MTRR_TYPE_WRTHROUGH;
1928                 }
1929
1930                 if (prev_match != curr_match)
1931                         return MTRR_TYPE_UNCACHABLE;
1932         }
1933
1934         if (prev_match != 0xFF)
1935                 return prev_match;
1936
1937         return mtrr_state->def_type;
1938 }
1939
1940 u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
1941 {
1942         u8 mtrr;
1943
1944         mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1945                              (gfn << PAGE_SHIFT) + PAGE_SIZE);
1946         if (mtrr == 0xfe || mtrr == 0xff)
1947                 mtrr = MTRR_TYPE_WRBACK;
1948         return mtrr;
1949 }
1950 EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
1951
1952 static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1953 {
1954         trace_kvm_mmu_unsync_page(sp);
1955         ++vcpu->kvm->stat.mmu_unsync;
1956         sp->unsync = 1;
1957
1958         kvm_mmu_mark_parents_unsync(sp);
1959 }
1960
1961 static void kvm_unsync_pages(struct kvm_vcpu *vcpu,  gfn_t gfn)
1962 {
1963         struct kvm_mmu_page *s;
1964         struct hlist_node *node;
1965
1966         for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
1967                 if (s->unsync)
1968                         continue;
1969                 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1970                 __kvm_unsync_page(vcpu, s);
1971         }
1972 }
1973
1974 static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1975                                   bool can_unsync)
1976 {
1977         struct kvm_mmu_page *s;
1978         struct hlist_node *node;
1979         bool need_unsync = false;
1980
1981         for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
1982                 if (!can_unsync)
1983                         return 1;
1984
1985                 if (s->role.level != PT_PAGE_TABLE_LEVEL)
1986                         return 1;
1987
1988                 if (!need_unsync && !s->unsync) {
1989                         if (!oos_shadow)
1990                                 return 1;
1991                         need_unsync = true;
1992                 }
1993         }
1994         if (need_unsync)
1995                 kvm_unsync_pages(vcpu, gfn);
1996         return 0;
1997 }
1998
1999 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2000                     unsigned pte_access, int user_fault,
2001                     int write_fault, int level,
2002                     gfn_t gfn, pfn_t pfn, bool speculative,
2003                     bool can_unsync, bool host_writable)
2004 {
2005         u64 spte, entry = *sptep;
2006         int ret = 0;
2007
2008         /*
2009          * We don't set the accessed bit, since we sometimes want to see
2010          * whether the guest actually used the pte (in order to detect
2011          * demand paging).
2012          */
2013         spte = PT_PRESENT_MASK;
2014         if (!speculative)
2015                 spte |= shadow_accessed_mask;
2016
2017         if (pte_access & ACC_EXEC_MASK)
2018                 spte |= shadow_x_mask;
2019         else
2020                 spte |= shadow_nx_mask;
2021         if (pte_access & ACC_USER_MASK)
2022                 spte |= shadow_user_mask;
2023         if (level > PT_PAGE_TABLE_LEVEL)
2024                 spte |= PT_PAGE_SIZE_MASK;
2025         if (tdp_enabled)
2026                 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
2027                         kvm_is_mmio_pfn(pfn));
2028
2029         if (host_writable)
2030                 spte |= SPTE_HOST_WRITEABLE;
2031         else
2032                 pte_access &= ~ACC_WRITE_MASK;
2033
2034         spte |= (u64)pfn << PAGE_SHIFT;
2035
2036         if ((pte_access & ACC_WRITE_MASK)
2037             || (!vcpu->arch.mmu.direct_map && write_fault
2038                 && !is_write_protection(vcpu) && !user_fault)) {
2039
2040                 if (level > PT_PAGE_TABLE_LEVEL &&
2041                     has_wrprotected_page(vcpu->kvm, gfn, level)) {
2042                         ret = 1;
2043                         drop_spte(vcpu->kvm, sptep);
2044                         goto done;
2045                 }
2046
2047                 spte |= PT_WRITABLE_MASK;
2048
2049                 if (!vcpu->arch.mmu.direct_map
2050                     && !(pte_access & ACC_WRITE_MASK)) {
2051                         spte &= ~PT_USER_MASK;
2052                         /*
2053                          * If we converted a user page to a kernel page,
2054                          * so that the kernel can write to it when cr0.wp=0,
2055                          * then we should prevent the kernel from executing it
2056                          * if SMEP is enabled.
2057                          */
2058                         if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
2059                                 spte |= PT64_NX_MASK;
2060                 }
2061
2062                 /*
2063                  * Optimization: for pte sync, if spte was writable the hash
2064                  * lookup is unnecessary (and expensive). Write protection
2065                  * is responsibility of mmu_get_page / kvm_sync_page.
2066                  * Same reasoning can be applied to dirty page accounting.
2067                  */
2068                 if (!can_unsync && is_writable_pte(*sptep))
2069                         goto set_pte;
2070
2071                 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
2072                         pgprintk("%s: found shadow page for %llx, marking ro\n",
2073                                  __func__, gfn);
2074                         ret = 1;
2075                         pte_access &= ~ACC_WRITE_MASK;
2076                         if (is_writable_pte(spte))
2077                                 spte &= ~PT_WRITABLE_MASK;
2078                 }
2079         }
2080
2081         if (pte_access & ACC_WRITE_MASK)
2082                 mark_page_dirty(vcpu->kvm, gfn);
2083
2084 set_pte:
2085         mmu_spte_update(sptep, spte);
2086         /*
2087          * If we overwrite a writable spte with a read-only one we
2088          * should flush remote TLBs. Otherwise rmap_write_protect
2089          * will find a read-only spte, even though the writable spte
2090          * might be cached on a CPU's TLB.
2091          */
2092         if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2093                 kvm_flush_remote_tlbs(vcpu->kvm);
2094 done:
2095         return ret;
2096 }
2097
2098 static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2099                          unsigned pt_access, unsigned pte_access,
2100                          int user_fault, int write_fault,
2101                          int *emulate, int level, gfn_t gfn,
2102                          pfn_t pfn, bool speculative,
2103                          bool host_writable)
2104 {
2105         int was_rmapped = 0;
2106         int rmap_count;
2107
2108         pgprintk("%s: spte %llx access %x write_fault %d"
2109                  " user_fault %d gfn %llx\n",
2110                  __func__, *sptep, pt_access,
2111                  write_fault, user_fault, gfn);
2112
2113         if (is_rmap_spte(*sptep)) {
2114                 /*
2115                  * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2116                  * the parent of the now unreachable PTE.
2117                  */
2118                 if (level > PT_PAGE_TABLE_LEVEL &&
2119                     !is_large_pte(*sptep)) {
2120                         struct kvm_mmu_page *child;
2121                         u64 pte = *sptep;
2122
2123                         child = page_header(pte & PT64_BASE_ADDR_MASK);
2124                         drop_parent_pte(child, sptep);
2125                         kvm_flush_remote_tlbs(vcpu->kvm);
2126                 } else if (pfn != spte_to_pfn(*sptep)) {
2127                         pgprintk("hfn old %llx new %llx\n",
2128                                  spte_to_pfn(*sptep), pfn);
2129                         drop_spte(vcpu->kvm, sptep);
2130                         kvm_flush_remote_tlbs(vcpu->kvm);
2131                 } else
2132                         was_rmapped = 1;
2133         }
2134
2135         if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
2136                       level, gfn, pfn, speculative, true,
2137                       host_writable)) {
2138                 if (write_fault)
2139                         *emulate = 1;
2140                 kvm_mmu_flush_tlb(vcpu);
2141         }
2142
2143         pgprintk("%s: setting spte %llx\n", __func__, *sptep);
2144         pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
2145                  is_large_pte(*sptep)? "2MB" : "4kB",
2146                  *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2147                  *sptep, sptep);
2148         if (!was_rmapped && is_large_pte(*sptep))
2149                 ++vcpu->kvm->stat.lpages;
2150
2151         if (is_shadow_present_pte(*sptep)) {
2152                 page_header_update_slot(vcpu->kvm, sptep, gfn);
2153                 if (!was_rmapped) {
2154                         rmap_count = rmap_add(vcpu, sptep, gfn);
2155                         if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2156                                 rmap_recycle(vcpu, sptep, gfn);
2157                 }
2158         }
2159         kvm_release_pfn_clean(pfn);
2160         if (speculative) {
2161                 vcpu->arch.last_pte_updated = sptep;
2162                 vcpu->arch.last_pte_gfn = gfn;
2163         }
2164 }
2165
2166 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2167 {
2168 }
2169
2170 static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2171                                      bool no_dirty_log)
2172 {
2173         struct kvm_memory_slot *slot;
2174         unsigned long hva;
2175
2176         slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
2177         if (!slot) {
2178                 get_page(fault_page);
2179                 return page_to_pfn(fault_page);
2180         }
2181
2182         hva = gfn_to_hva_memslot(slot, gfn);
2183
2184         return hva_to_pfn_atomic(vcpu->kvm, hva);
2185 }
2186
2187 static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2188                                     struct kvm_mmu_page *sp,
2189                                     u64 *start, u64 *end)
2190 {
2191         struct page *pages[PTE_PREFETCH_NUM];
2192         unsigned access = sp->role.access;
2193         int i, ret;
2194         gfn_t gfn;
2195
2196         gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2197         if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
2198                 return -1;
2199
2200         ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2201         if (ret <= 0)
2202                 return -1;
2203
2204         for (i = 0; i < ret; i++, gfn++, start++)
2205                 mmu_set_spte(vcpu, start, ACC_ALL,
2206                              access, 0, 0, NULL,
2207                              sp->role.level, gfn,
2208                              page_to_pfn(pages[i]), true, true);
2209
2210         return 0;
2211 }
2212
2213 static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2214                                   struct kvm_mmu_page *sp, u64 *sptep)
2215 {
2216         u64 *spte, *start = NULL;
2217         int i;
2218
2219         WARN_ON(!sp->role.direct);
2220
2221         i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2222         spte = sp->spt + i;
2223
2224         for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2225                 if (is_shadow_present_pte(*spte) || spte == sptep) {
2226                         if (!start)
2227                                 continue;
2228                         if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2229                                 break;
2230                         start = NULL;
2231                 } else if (!start)
2232                         start = spte;
2233         }
2234 }
2235
2236 static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2237 {
2238         struct kvm_mmu_page *sp;
2239
2240         /*
2241          * Since it's no accessed bit on EPT, it's no way to
2242          * distinguish between actually accessed translations
2243          * and prefetched, so disable pte prefetch if EPT is
2244          * enabled.
2245          */
2246         if (!shadow_accessed_mask)
2247                 return;
2248
2249         sp = page_header(__pa(sptep));
2250         if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2251                 return;
2252
2253         __direct_pte_prefetch(vcpu, sp, sptep);
2254 }
2255
2256 static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
2257                         int map_writable, int level, gfn_t gfn, pfn_t pfn,
2258                         bool prefault)
2259 {
2260         struct kvm_shadow_walk_iterator iterator;
2261         struct kvm_mmu_page *sp;
2262         int emulate = 0;
2263         gfn_t pseudo_gfn;
2264
2265         for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
2266                 if (iterator.level == level) {
2267                         unsigned pte_access = ACC_ALL;
2268
2269                         mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
2270                                      0, write, &emulate,
2271                                      level, gfn, pfn, prefault, map_writable);
2272                         direct_pte_prefetch(vcpu, iterator.sptep);
2273                         ++vcpu->stat.pf_fixed;
2274                         break;
2275                 }
2276
2277                 if (!is_shadow_present_pte(*iterator.sptep)) {
2278                         u64 base_addr = iterator.addr;
2279
2280                         base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2281                         pseudo_gfn = base_addr >> PAGE_SHIFT;
2282                         sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2283                                               iterator.level - 1,
2284                                               1, ACC_ALL, iterator.sptep);
2285                         if (!sp) {
2286                                 pgprintk("nonpaging_map: ENOMEM\n");
2287                                 kvm_release_pfn_clean(pfn);
2288                                 return -ENOMEM;
2289                         }
2290
2291                         mmu_spte_set(iterator.sptep,
2292                                      __pa(sp->spt)
2293                                      | PT_PRESENT_MASK | PT_WRITABLE_MASK
2294                                      | shadow_user_mask | shadow_x_mask
2295                                      | shadow_accessed_mask);
2296                 }
2297         }
2298         return emulate;
2299 }
2300
2301 static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
2302 {
2303         siginfo_t info;
2304
2305         info.si_signo   = SIGBUS;
2306         info.si_errno   = 0;
2307         info.si_code    = BUS_MCEERR_AR;
2308         info.si_addr    = (void __user *)address;
2309         info.si_addr_lsb = PAGE_SHIFT;
2310
2311         send_sig_info(SIGBUS, &info, tsk);
2312 }
2313
2314 static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, pfn_t pfn)
2315 {
2316         kvm_release_pfn_clean(pfn);
2317         if (is_hwpoison_pfn(pfn)) {
2318                 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
2319                 return 0;
2320         }
2321
2322         return -EFAULT;
2323 }
2324
2325 static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2326                                         gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2327 {
2328         pfn_t pfn = *pfnp;
2329         gfn_t gfn = *gfnp;
2330         int level = *levelp;
2331
2332         /*
2333          * Check if it's a transparent hugepage. If this would be an
2334          * hugetlbfs page, level wouldn't be set to
2335          * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2336          * here.
2337          */
2338         if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2339             level == PT_PAGE_TABLE_LEVEL &&
2340             PageTransCompound(pfn_to_page(pfn)) &&
2341             !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2342                 unsigned long mask;
2343                 /*
2344                  * mmu_notifier_retry was successful and we hold the
2345                  * mmu_lock here, so the pmd can't become splitting
2346                  * from under us, and in turn
2347                  * __split_huge_page_refcount() can't run from under
2348                  * us and we can safely transfer the refcount from
2349                  * PG_tail to PG_head as we switch the pfn to tail to
2350                  * head.
2351                  */
2352                 *levelp = level = PT_DIRECTORY_LEVEL;
2353                 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2354                 VM_BUG_ON((gfn & mask) != (pfn & mask));
2355                 if (pfn & mask) {
2356                         gfn &= ~mask;
2357                         *gfnp = gfn;
2358                         kvm_release_pfn_clean(pfn);
2359                         pfn &= ~mask;
2360                         if (!get_page_unless_zero(pfn_to_page(pfn)))
2361                                 BUG();
2362                         *pfnp = pfn;
2363                 }
2364         }
2365 }
2366
2367 static bool mmu_invalid_pfn(pfn_t pfn)
2368 {
2369         return unlikely(is_invalid_pfn(pfn) || is_noslot_pfn(pfn));
2370 }
2371
2372 static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
2373                                 pfn_t pfn, unsigned access, int *ret_val)
2374 {
2375         bool ret = true;
2376
2377         /* The pfn is invalid, report the error! */
2378         if (unlikely(is_invalid_pfn(pfn))) {
2379                 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
2380                 goto exit;
2381         }
2382
2383         if (unlikely(is_noslot_pfn(pfn))) {
2384                 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
2385                 *ret_val = 1;
2386                 goto exit;
2387         }
2388
2389         ret = false;
2390 exit:
2391         return ret;
2392 }
2393
2394 static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
2395                          gva_t gva, pfn_t *pfn, bool write, bool *writable);
2396
2397 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
2398                          bool prefault)
2399 {
2400         int r;
2401         int level;
2402         int force_pt_level;
2403         pfn_t pfn;
2404         unsigned long mmu_seq;
2405         bool map_writable;
2406
2407         force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2408         if (likely(!force_pt_level)) {
2409                 level = mapping_level(vcpu, gfn);
2410                 /*
2411                  * This path builds a PAE pagetable - so we can map
2412                  * 2mb pages at maximum. Therefore check if the level
2413                  * is larger than that.
2414                  */
2415                 if (level > PT_DIRECTORY_LEVEL)
2416                         level = PT_DIRECTORY_LEVEL;
2417
2418                 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2419         } else
2420                 level = PT_PAGE_TABLE_LEVEL;
2421
2422         mmu_seq = vcpu->kvm->mmu_notifier_seq;
2423         smp_rmb();
2424
2425         if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
2426                 return 0;
2427
2428         if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
2429                 return r;
2430
2431         spin_lock(&vcpu->kvm->mmu_lock);
2432         if (mmu_notifier_retry(vcpu, mmu_seq))
2433                 goto out_unlock;
2434         kvm_mmu_free_some_pages(vcpu);
2435         if (likely(!force_pt_level))
2436                 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
2437         r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2438                          prefault);
2439         spin_unlock(&vcpu->kvm->mmu_lock);
2440
2441
2442         return r;
2443
2444 out_unlock:
2445         spin_unlock(&vcpu->kvm->mmu_lock);
2446         kvm_release_pfn_clean(pfn);
2447         return 0;
2448 }
2449
2450
2451 static void mmu_free_roots(struct kvm_vcpu *vcpu)
2452 {
2453         int i;
2454         struct kvm_mmu_page *sp;
2455         LIST_HEAD(invalid_list);
2456
2457         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2458                 return;
2459         spin_lock(&vcpu->kvm->mmu_lock);
2460         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2461             (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2462              vcpu->arch.mmu.direct_map)) {
2463                 hpa_t root = vcpu->arch.mmu.root_hpa;
2464
2465                 sp = page_header(root);
2466                 --sp->root_count;
2467                 if (!sp->root_count && sp->role.invalid) {
2468                         kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2469                         kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2470                 }
2471                 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2472                 spin_unlock(&vcpu->kvm->mmu_lock);
2473                 return;
2474         }
2475         for (i = 0; i < 4; ++i) {
2476                 hpa_t root = vcpu->arch.mmu.pae_root[i];
2477
2478                 if (root) {
2479                         root &= PT64_BASE_ADDR_MASK;
2480                         sp = page_header(root);
2481                         --sp->root_count;
2482                         if (!sp->root_count && sp->role.invalid)
2483                                 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2484                                                          &invalid_list);
2485                 }
2486                 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
2487         }
2488         kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2489         spin_unlock(&vcpu->kvm->mmu_lock);
2490         vcpu->arch.mmu.root_hpa = INVALID_PAGE;
2491 }
2492
2493 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2494 {
2495         int ret = 0;
2496
2497         if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2498                 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2499                 ret = 1;
2500         }
2501
2502         return ret;
2503 }
2504
2505 static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2506 {
2507         struct kvm_mmu_page *sp;
2508         unsigned i;
2509
2510         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2511                 spin_lock(&vcpu->kvm->mmu_lock);
2512                 kvm_mmu_free_some_pages(vcpu);
2513                 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2514                                       1, ACC_ALL, NULL);
2515                 ++sp->root_count;
2516                 spin_unlock(&vcpu->kvm->mmu_lock);
2517                 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2518         } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2519                 for (i = 0; i < 4; ++i) {
2520                         hpa_t root = vcpu->arch.mmu.pae_root[i];
2521
2522                         ASSERT(!VALID_PAGE(root));
2523                         spin_lock(&vcpu->kvm->mmu_lock);
2524                         kvm_mmu_free_some_pages(vcpu);
2525                         sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2526                                               i << 30,
2527                                               PT32_ROOT_LEVEL, 1, ACC_ALL,
2528                                               NULL);
2529                         root = __pa(sp->spt);
2530                         ++sp->root_count;
2531                         spin_unlock(&vcpu->kvm->mmu_lock);
2532                         vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2533                 }
2534                 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2535         } else
2536                 BUG();
2537
2538         return 0;
2539 }
2540
2541 static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
2542 {
2543         struct kvm_mmu_page *sp;
2544         u64 pdptr, pm_mask;
2545         gfn_t root_gfn;
2546         int i;
2547
2548         root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
2549
2550         if (mmu_check_root(vcpu, root_gfn))
2551                 return 1;
2552
2553         /*
2554          * Do we shadow a long mode page table? If so we need to
2555          * write-protect the guests page table root.
2556          */
2557         if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
2558                 hpa_t root = vcpu->arch.mmu.root_hpa;
2559
2560                 ASSERT(!VALID_PAGE(root));
2561
2562                 spin_lock(&vcpu->kvm->mmu_lock);
2563                 kvm_mmu_free_some_pages(vcpu);
2564                 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2565                                       0, ACC_ALL, NULL);
2566                 root = __pa(sp->spt);
2567                 ++sp->root_count;
2568                 spin_unlock(&vcpu->kvm->mmu_lock);
2569                 vcpu->arch.mmu.root_hpa = root;
2570                 return 0;
2571         }
2572
2573         /*
2574          * We shadow a 32 bit page table. This may be a legacy 2-level
2575          * or a PAE 3-level page table. In either case we need to be aware that
2576          * the shadow page table may be a PAE or a long mode page table.
2577          */
2578         pm_mask = PT_PRESENT_MASK;
2579         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2580                 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2581
2582         for (i = 0; i < 4; ++i) {
2583                 hpa_t root = vcpu->arch.mmu.pae_root[i];
2584
2585                 ASSERT(!VALID_PAGE(root));
2586                 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
2587                         pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
2588                         if (!is_present_gpte(pdptr)) {
2589                                 vcpu->arch.mmu.pae_root[i] = 0;
2590                                 continue;
2591                         }
2592                         root_gfn = pdptr >> PAGE_SHIFT;
2593                         if (mmu_check_root(vcpu, root_gfn))
2594                                 return 1;
2595                 }
2596                 spin_lock(&vcpu->kvm->mmu_lock);
2597                 kvm_mmu_free_some_pages(vcpu);
2598                 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
2599                                       PT32_ROOT_LEVEL, 0,
2600                                       ACC_ALL, NULL);
2601                 root = __pa(sp->spt);
2602                 ++sp->root_count;
2603                 spin_unlock(&vcpu->kvm->mmu_lock);
2604
2605                 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
2606         }
2607         vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2608
2609         /*
2610          * If we shadow a 32 bit page table with a long mode page
2611          * table we enter this path.
2612          */
2613         if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2614                 if (vcpu->arch.mmu.lm_root == NULL) {
2615                         /*
2616                          * The additional page necessary for this is only
2617                          * allocated on demand.
2618                          */
2619
2620                         u64 *lm_root;
2621
2622                         lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2623                         if (lm_root == NULL)
2624                                 return 1;
2625
2626                         lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2627
2628                         vcpu->arch.mmu.lm_root = lm_root;
2629                 }
2630
2631                 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2632         }
2633
2634         return 0;
2635 }
2636
2637 static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2638 {
2639         if (vcpu->arch.mmu.direct_map)
2640                 return mmu_alloc_direct_roots(vcpu);
2641         else
2642                 return mmu_alloc_shadow_roots(vcpu);
2643 }
2644
2645 static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2646 {
2647         int i;
2648         struct kvm_mmu_page *sp;
2649
2650         if (vcpu->arch.mmu.direct_map)
2651                 return;
2652
2653         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2654                 return;
2655
2656         vcpu_clear_mmio_info(vcpu, ~0ul);
2657         trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
2658         if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
2659                 hpa_t root = vcpu->arch.mmu.root_hpa;
2660                 sp = page_header(root);
2661                 mmu_sync_children(vcpu, sp);
2662                 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
2663                 return;
2664         }
2665         for (i = 0; i < 4; ++i) {
2666                 hpa_t root = vcpu->arch.mmu.pae_root[i];
2667
2668                 if (root && VALID_PAGE(root)) {
2669                         root &= PT64_BASE_ADDR_MASK;
2670                         sp = page_header(root);
2671                         mmu_sync_children(vcpu, sp);
2672                 }
2673         }
2674         trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
2675 }
2676
2677 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2678 {
2679         spin_lock(&vcpu->kvm->mmu_lock);
2680         mmu_sync_roots(vcpu);
2681         spin_unlock(&vcpu->kvm->mmu_lock);
2682 }
2683
2684 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2685                                   u32 access, struct x86_exception *exception)
2686 {
2687         if (exception)
2688                 exception->error_code = 0;
2689         return vaddr;
2690 }
2691
2692 static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
2693                                          u32 access,
2694                                          struct x86_exception *exception)
2695 {
2696         if (exception)
2697                 exception->error_code = 0;
2698         return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2699 }
2700
2701 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
2702                                 u32 error_code, bool prefault)
2703 {
2704         gfn_t gfn;
2705         int r;
2706
2707         pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
2708         r = mmu_topup_memory_caches(vcpu);
2709         if (r)
2710                 return r;
2711
2712         ASSERT(vcpu);
2713         ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2714
2715         gfn = gva >> PAGE_SHIFT;
2716
2717         return nonpaging_map(vcpu, gva & PAGE_MASK,
2718                              error_code & PFERR_WRITE_MASK, gfn, prefault);
2719 }
2720
2721 static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
2722 {
2723         struct kvm_arch_async_pf arch;
2724
2725         arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
2726         arch.gfn = gfn;
2727         arch.direct_map = vcpu->arch.mmu.direct_map;
2728         arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
2729
2730         return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2731 }
2732
2733 static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2734 {
2735         if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2736                      kvm_event_needs_reinjection(vcpu)))
2737                 return false;
2738
2739         return kvm_x86_ops->interrupt_allowed(vcpu);
2740 }
2741
2742 static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
2743                          gva_t gva, pfn_t *pfn, bool write, bool *writable)
2744 {
2745         bool async;
2746
2747         *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
2748
2749         if (!async)
2750                 return false; /* *pfn has correct page already */
2751
2752         put_page(pfn_to_page(*pfn));
2753
2754         if (!prefault && can_do_async_pf(vcpu)) {
2755                 trace_kvm_try_async_get_page(gva, gfn);
2756                 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2757                         trace_kvm_async_pf_doublefault(gva, gfn);
2758                         kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2759                         return true;
2760                 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2761                         return true;
2762         }
2763
2764         *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
2765
2766         return false;
2767 }
2768
2769 static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
2770                           bool prefault)
2771 {
2772         pfn_t pfn;
2773         int r;
2774         int level;
2775         int force_pt_level;
2776         gfn_t gfn = gpa >> PAGE_SHIFT;
2777         unsigned long mmu_seq;
2778         int write = error_code & PFERR_WRITE_MASK;
2779         bool map_writable;
2780
2781         ASSERT(vcpu);
2782         ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2783
2784         r = mmu_topup_memory_caches(vcpu);
2785         if (r)
2786                 return r;
2787
2788         force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2789         if (likely(!force_pt_level)) {
2790                 level = mapping_level(vcpu, gfn);
2791                 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2792         } else
2793                 level = PT_PAGE_TABLE_LEVEL;
2794
2795         mmu_seq = vcpu->kvm->mmu_notifier_seq;
2796         smp_rmb();
2797
2798         if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
2799                 return 0;
2800
2801         if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
2802                 return r;
2803
2804         spin_lock(&vcpu->kvm->mmu_lock);
2805         if (mmu_notifier_retry(vcpu, mmu_seq))
2806                 goto out_unlock;
2807         kvm_mmu_free_some_pages(vcpu);
2808         if (likely(!force_pt_level))
2809                 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
2810         r = __direct_map(vcpu, gpa, write, map_writable,
2811                          level, gfn, pfn, prefault);
2812         spin_unlock(&vcpu->kvm->mmu_lock);
2813
2814         return r;
2815
2816 out_unlock:
2817         spin_unlock(&vcpu->kvm->mmu_lock);
2818         kvm_release_pfn_clean(pfn);
2819         return 0;
2820 }
2821
2822 static void nonpaging_free(struct kvm_vcpu *vcpu)
2823 {
2824         mmu_free_roots(vcpu);
2825 }
2826
2827 static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2828                                   struct kvm_mmu *context)
2829 {
2830         context->new_cr3 = nonpaging_new_cr3;
2831         context->page_fault = nonpaging_page_fault;
2832         context->gva_to_gpa = nonpaging_gva_to_gpa;
2833         context->free = nonpaging_free;
2834         context->sync_page = nonpaging_sync_page;
2835         context->invlpg = nonpaging_invlpg;
2836         context->update_pte = nonpaging_update_pte;
2837         context->root_level = 0;
2838         context->shadow_root_level = PT32E_ROOT_LEVEL;
2839         context->root_hpa = INVALID_PAGE;
2840         context->direct_map = true;
2841         context->nx = false;
2842         return 0;
2843 }
2844
2845 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2846 {
2847         ++vcpu->stat.tlb_flush;
2848         kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2849 }
2850
2851 static void paging_new_cr3(struct kvm_vcpu *vcpu)
2852 {
2853         pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
2854         mmu_free_roots(vcpu);
2855 }
2856
2857 static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2858 {
2859         return kvm_read_cr3(vcpu);
2860 }
2861
2862 static void inject_page_fault(struct kvm_vcpu *vcpu,
2863                               struct x86_exception *fault)
2864 {
2865         vcpu->arch.mmu.inject_page_fault(vcpu, fault);
2866 }
2867
2868 static void paging_free(struct kvm_vcpu *vcpu)
2869 {
2870         nonpaging_free(vcpu);
2871 }
2872
2873 static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
2874 {
2875         int bit7;
2876
2877         bit7 = (gpte >> 7) & 1;
2878         return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
2879 }
2880
2881 #define PTTYPE 64
2882 #include "paging_tmpl.h"
2883 #undef PTTYPE
2884
2885 #define PTTYPE 32
2886 #include "paging_tmpl.h"
2887 #undef PTTYPE
2888
2889 static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2890                                   struct kvm_mmu *context,
2891                                   int level)
2892 {
2893         int maxphyaddr = cpuid_maxphyaddr(vcpu);
2894         u64 exb_bit_rsvd = 0;
2895
2896         if (!context->nx)
2897                 exb_bit_rsvd = rsvd_bits(63, 63);
2898         switch (level) {
2899         case PT32_ROOT_LEVEL:
2900                 /* no rsvd bits for 2 level 4K page table entries */
2901                 context->rsvd_bits_mask[0][1] = 0;
2902                 context->rsvd_bits_mask[0][0] = 0;
2903                 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2904
2905                 if (!is_pse(vcpu)) {
2906                         context->rsvd_bits_mask[1][1] = 0;
2907                         break;
2908                 }
2909
2910                 if (is_cpuid_PSE36())
2911                         /* 36bits PSE 4MB page */
2912                         context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2913                 else
2914                         /* 32 bits PSE 4MB page */
2915                         context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
2916                 break;
2917         case PT32E_ROOT_LEVEL:
2918                 context->rsvd_bits_mask[0][2] =
2919                         rsvd_bits(maxphyaddr, 63) |
2920                         rsvd_bits(7, 8) | rsvd_bits(1, 2);      /* PDPTE */
2921                 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2922                         rsvd_bits(maxphyaddr, 62);      /* PDE */
2923                 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2924                         rsvd_bits(maxphyaddr, 62);      /* PTE */
2925                 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2926                         rsvd_bits(maxphyaddr, 62) |
2927                         rsvd_bits(13, 20);              /* large page */
2928                 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2929                 break;
2930         case PT64_ROOT_LEVEL:
2931                 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2932                         rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2933                 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2934                         rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2935                 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
2936                         rsvd_bits(maxphyaddr, 51);
2937                 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2938                         rsvd_bits(maxphyaddr, 51);
2939                 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2940                 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2941                         rsvd_bits(maxphyaddr, 51) |
2942                         rsvd_bits(13, 29);
2943                 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2944                         rsvd_bits(maxphyaddr, 51) |
2945                         rsvd_bits(13, 20);              /* large page */
2946                 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2947                 break;
2948         }
2949 }
2950
2951 static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2952                                         struct kvm_mmu *context,
2953                                         int level)
2954 {
2955         context->nx = is_nx(vcpu);
2956
2957         reset_rsvds_bits_mask(vcpu, context, level);
2958
2959         ASSERT(is_pae(vcpu));
2960         context->new_cr3 = paging_new_cr3;
2961         context->page_fault = paging64_page_fault;
2962         context->gva_to_gpa = paging64_gva_to_gpa;
2963         context->sync_page = paging64_sync_page;
2964         context->invlpg = paging64_invlpg;
2965         context->update_pte = paging64_update_pte;
2966         context->free = paging_free;
2967         context->root_level = level;
2968         context->shadow_root_level = level;
2969         context->root_hpa = INVALID_PAGE;
2970         context->direct_map = false;
2971         return 0;
2972 }
2973
2974 static int paging64_init_context(struct kvm_vcpu *vcpu,
2975                                  struct kvm_mmu *context)
2976 {
2977         return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
2978 }
2979
2980 static int paging32_init_context(struct kvm_vcpu *vcpu,
2981                                  struct kvm_mmu *context)
2982 {
2983         context->nx = false;
2984
2985         reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
2986
2987         context->new_cr3 = paging_new_cr3;
2988         context->page_fault = paging32_page_fault;
2989         context->gva_to_gpa = paging32_gva_to_gpa;
2990         context->free = paging_free;
2991         context->sync_page = paging32_sync_page;
2992         context->invlpg = paging32_invlpg;
2993         context->update_pte = paging32_update_pte;
2994         context->root_level = PT32_ROOT_LEVEL;
2995         context->shadow_root_level = PT32E_ROOT_LEVEL;
2996         context->root_hpa = INVALID_PAGE;
2997         context->direct_map = false;
2998         return 0;
2999 }
3000
3001 static int paging32E_init_context(struct kvm_vcpu *vcpu,
3002                                   struct kvm_mmu *context)
3003 {
3004         return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
3005 }
3006
3007 static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
3008 {
3009         struct kvm_mmu *context = vcpu->arch.walk_mmu;
3010
3011         context->base_role.word = 0;
3012         context->new_cr3 = nonpaging_new_cr3;
3013         context->page_fault = tdp_page_fault;
3014         context->free = nonpaging_free;
3015         context->sync_page = nonpaging_sync_page;
3016         context->invlpg = nonpaging_invlpg;
3017         context->update_pte = nonpaging_update_pte;
3018         context->shadow_root_level = kvm_x86_ops->get_tdp_level();
3019         context->root_hpa = INVALID_PAGE;
3020         context->direct_map = true;
3021         context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
3022         context->get_cr3 = get_cr3;
3023         context->inject_page_fault = kvm_inject_page_fault;
3024         context->nx = is_nx(vcpu);
3025
3026         if (!is_paging(vcpu)) {
3027                 context->nx = false;
3028                 context->gva_to_gpa = nonpaging_gva_to_gpa;
3029                 context->root_level = 0;
3030         } else if (is_long_mode(vcpu)) {
3031                 context->nx = is_nx(vcpu);
3032                 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
3033                 context->gva_to_gpa = paging64_gva_to_gpa;
3034                 context->root_level = PT64_ROOT_LEVEL;
3035         } else if (is_pae(vcpu)) {
3036                 context->nx = is_nx(vcpu);
3037                 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
3038                 context->gva_to_gpa = paging64_gva_to_gpa;
3039                 context->root_level = PT32E_ROOT_LEVEL;
3040         } else {
3041                 context->nx = false;
3042                 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
3043                 context->gva_to_gpa = paging32_gva_to_gpa;
3044                 context->root_level = PT32_ROOT_LEVEL;
3045         }
3046
3047         return 0;
3048 }
3049
3050 int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
3051 {
3052         int r;
3053         bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
3054         ASSERT(vcpu);
3055         ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
3056
3057         if (!is_paging(vcpu))
3058                 r = nonpaging_init_context(vcpu, context);
3059         else if (is_long_mode(vcpu))
3060                 r = paging64_init_context(vcpu, context);
3061         else if (is_pae(vcpu))
3062                 r = paging32E_init_context(vcpu, context);
3063         else
3064                 r = paging32_init_context(vcpu, context);
3065
3066         vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
3067         vcpu->arch.mmu.base_role.cr0_wp  = is_write_protection(vcpu);
3068         vcpu->arch.mmu.base_role.smep_andnot_wp
3069                 = smep && !is_write_protection(vcpu);
3070
3071         return r;
3072 }
3073 EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3074
3075 static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3076 {
3077         int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
3078
3079         vcpu->arch.walk_mmu->set_cr3           = kvm_x86_ops->set_cr3;
3080         vcpu->arch.walk_mmu->get_cr3           = get_cr3;
3081         vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
3082
3083         return r;
3084 }
3085
3086 static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3087 {
3088         struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3089
3090         g_context->get_cr3           = get_cr3;
3091         g_context->inject_page_fault = kvm_inject_page_fault;
3092
3093         /*
3094          * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3095          * translation of l2_gpa to l1_gpa addresses is done using the
3096          * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3097          * functions between mmu and nested_mmu are swapped.
3098          */
3099         if (!is_paging(vcpu)) {
3100                 g_context->nx = false;
3101                 g_context->root_level = 0;
3102                 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3103         } else if (is_long_mode(vcpu)) {
3104                 g_context->nx = is_nx(vcpu);
3105                 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3106                 g_context->root_level = PT64_ROOT_LEVEL;
3107                 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3108         } else if (is_pae(vcpu)) {
3109                 g_context->nx = is_nx(vcpu);
3110                 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3111                 g_context->root_level = PT32E_ROOT_LEVEL;
3112                 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3113         } else {
3114                 g_context->nx = false;
3115                 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3116                 g_context->root_level = PT32_ROOT_LEVEL;
3117                 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3118         }
3119
3120         return 0;
3121 }
3122
3123 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3124 {
3125         if (mmu_is_nested(vcpu))
3126                 return init_kvm_nested_mmu(vcpu);
3127         else if (tdp_enabled)
3128                 return init_kvm_tdp_mmu(vcpu);
3129         else
3130                 return init_kvm_softmmu(vcpu);
3131 }
3132
3133 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3134 {
3135         ASSERT(vcpu);
3136         if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3137                 /* mmu.free() should set root_hpa = INVALID_PAGE */
3138                 vcpu->arch.mmu.free(vcpu);
3139 }
3140
3141 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3142 {
3143         destroy_kvm_mmu(vcpu);
3144         return init_kvm_mmu(vcpu);
3145 }
3146 EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
3147
3148 int kvm_mmu_load(struct kvm_vcpu *vcpu)
3149 {
3150         int r;
3151
3152         r = mmu_topup_memory_caches(vcpu);
3153         if (r)
3154                 goto out;
3155         r = mmu_alloc_roots(vcpu);
3156         spin_lock(&vcpu->kvm->mmu_lock);
3157         mmu_sync_roots(vcpu);
3158         spin_unlock(&vcpu->kvm->mmu_lock);
3159         if (r)
3160                 goto out;
3161         /* set_cr3() should ensure TLB has been flushed */
3162         vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
3163 out:
3164         return r;
3165 }
3166 EXPORT_SYMBOL_GPL(kvm_mmu_load);
3167
3168 void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3169 {
3170         mmu_free_roots(vcpu);
3171 }
3172 EXPORT_SYMBOL_GPL(kvm_mmu_unload);
3173
3174 static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
3175                                   struct kvm_mmu_page *sp, u64 *spte,
3176                                   const void *new)
3177 {
3178         if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
3179                 ++vcpu->kvm->stat.mmu_pde_zapped;
3180                 return;
3181         }
3182
3183         ++vcpu->kvm->stat.mmu_pte_updated;
3184         vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
3185 }
3186
3187 static bool need_remote_flush(u64 old, u64 new)
3188 {
3189         if (!is_shadow_present_pte(old))
3190                 return false;
3191         if (!is_shadow_present_pte(new))
3192                 return true;
3193         if ((old ^ new) & PT64_BASE_ADDR_MASK)
3194                 return true;
3195         old ^= PT64_NX_MASK;
3196         new ^= PT64_NX_MASK;
3197         return (old & ~new & PT64_PERM_MASK) != 0;
3198 }
3199
3200 static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3201                                     bool remote_flush, bool local_flush)
3202 {
3203         if (zap_page)
3204                 return;
3205
3206         if (remote_flush)
3207                 kvm_flush_remote_tlbs(vcpu->kvm);
3208         else if (local_flush)
3209                 kvm_mmu_flush_tlb(vcpu);
3210 }
3211
3212 static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3213 {
3214         u64 *spte = vcpu->arch.last_pte_updated;
3215
3216         return !!(spte && (*spte & shadow_accessed_mask));
3217 }
3218
3219 static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3220 {
3221         u64 *spte = vcpu->arch.last_pte_updated;
3222
3223         if (spte
3224             && vcpu->arch.last_pte_gfn == gfn
3225             && shadow_accessed_mask
3226             && !(*spte & shadow_accessed_mask)
3227             && is_shadow_present_pte(*spte))
3228                 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3229 }
3230
3231 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
3232                        const u8 *new, int bytes,
3233                        bool guest_initiated)
3234 {
3235         gfn_t gfn = gpa >> PAGE_SHIFT;
3236         union kvm_mmu_page_role mask = { .word = 0 };
3237         struct kvm_mmu_page *sp;
3238         struct hlist_node *node;
3239         LIST_HEAD(invalid_list);
3240         u64 entry, gentry, *spte;
3241         unsigned pte_size, page_offset, misaligned, quadrant, offset;
3242         int level, npte, invlpg_counter, r, flooded = 0;
3243         bool remote_flush, local_flush, zap_page;
3244
3245         /*
3246          * If we don't have indirect shadow pages, it means no page is
3247          * write-protected, so we can exit simply.
3248          */
3249         if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3250                 return;
3251
3252         zap_page = remote_flush = local_flush = false;
3253         offset = offset_in_page(gpa);
3254
3255         pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
3256
3257         invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3258
3259         /*
3260          * Assume that the pte write on a page table of the same type
3261          * as the current vcpu paging mode since we update the sptes only
3262          * when they have the same mode.
3263          */
3264         if ((is_pae(vcpu) && bytes == 4) || !new) {
3265                 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3266                 if (is_pae(vcpu)) {
3267                         gpa &= ~(gpa_t)7;
3268                         bytes = 8;
3269                 }
3270                 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3271                 if (r)
3272                         gentry = 0;
3273                 new = (const u8 *)&gentry;
3274         }
3275
3276         switch (bytes) {
3277         case 4:
3278                 gentry = *(const u32 *)new;
3279                 break;
3280         case 8:
3281                 gentry = *(const u64 *)new;
3282                 break;
3283         default:
3284                 gentry = 0;
3285                 break;
3286         }
3287
3288         spin_lock(&vcpu->kvm->mmu_lock);
3289         if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3290                 gentry = 0;
3291         kvm_mmu_free_some_pages(vcpu);
3292         ++vcpu->kvm->stat.mmu_pte_write;
3293         trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
3294         if (guest_initiated) {
3295                 kvm_mmu_access_page(vcpu, gfn);
3296                 if (gfn == vcpu->arch.last_pt_write_gfn
3297                     && !last_updated_pte_accessed(vcpu)) {
3298                         ++vcpu->arch.last_pt_write_count;
3299                         if (vcpu->arch.last_pt_write_count >= 3)
3300                                 flooded = 1;
3301                 } else {
3302                         vcpu->arch.last_pt_write_gfn = gfn;
3303                         vcpu->arch.last_pt_write_count = 1;
3304                         vcpu->arch.last_pte_updated = NULL;
3305                 }
3306         }
3307
3308         mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
3309         for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
3310                 pte_size = sp->role.cr4_pae ? 8 : 4;
3311                 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
3312                 misaligned |= bytes < 4;
3313                 if (misaligned || flooded) {
3314                         /*
3315                          * Misaligned accesses are too much trouble to fix
3316                          * up; also, they usually indicate a page is not used
3317                          * as a page table.
3318                          *
3319                          * If we're seeing too many writes to a page,
3320                          * it may no longer be a page table, or we may be
3321                          * forking, in which case it is better to unmap the
3322                          * page.
3323                          */
3324                         pgprintk("misaligned: gpa %llx bytes %d role %x\n",
3325                                  gpa, bytes, sp->role.word);
3326                         zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
3327                                                      &invalid_list);
3328                         ++vcpu->kvm->stat.mmu_flooded;
3329                         continue;
3330                 }
3331                 page_offset = offset;
3332                 level = sp->role.level;
3333                 npte = 1;
3334                 if (!sp->role.cr4_pae) {
3335                         page_offset <<= 1;      /* 32->64 */
3336                         /*
3337                          * A 32-bit pde maps 4MB while the shadow pdes map
3338                          * only 2MB.  So we need to double the offset again
3339                          * and zap two pdes instead of one.
3340                          */
3341                         if (level == PT32_ROOT_LEVEL) {
3342                                 page_offset &= ~7; /* kill rounding error */
3343                                 page_offset <<= 1;
3344                                 npte = 2;
3345                         }
3346                         quadrant = page_offset >> PAGE_SHIFT;
3347                         page_offset &= ~PAGE_MASK;
3348                         if (quadrant != sp->role.quadrant)
3349                                 continue;
3350                 }
3351                 local_flush = true;
3352                 spte = &sp->spt[page_offset / sizeof(*spte)];
3353                 while (npte--) {
3354                         entry = *spte;
3355                         mmu_page_zap_pte(vcpu->kvm, sp, spte);
3356                         if (gentry &&
3357                               !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3358                               & mask.word))
3359                                 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
3360                         if (!remote_flush && need_remote_flush(entry, *spte))
3361                                 remote_flush = true;
3362                         ++spte;
3363                 }
3364         }
3365         mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
3366         kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
3367         trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
3368         spin_unlock(&vcpu->kvm->mmu_lock);
3369 }
3370
3371 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3372 {
3373         gpa_t gpa;
3374         int r;
3375
3376         if (vcpu->arch.mmu.direct_map)
3377                 return 0;
3378
3379         gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
3380
3381         spin_lock(&vcpu->kvm->mmu_lock);
3382         r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
3383         spin_unlock(&vcpu->kvm->mmu_lock);
3384         return r;
3385 }
3386 EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
3387
3388 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
3389 {
3390         LIST_HEAD(invalid_list);
3391
3392         while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
3393                !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
3394                 struct kvm_mmu_page *sp;
3395
3396                 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
3397                                   struct kvm_mmu_page, link);
3398                 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
3399                 ++vcpu->kvm->stat.mmu_recycled;
3400         }
3401         kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
3402 }
3403
3404 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3405                        void *insn, int insn_len)
3406 {
3407         int r;
3408         enum emulation_result er;
3409
3410         r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
3411         if (r < 0)
3412                 goto out;
3413
3414         if (!r) {
3415                 r = 1;
3416                 goto out;
3417         }
3418
3419         r = mmu_topup_memory_caches(vcpu);
3420         if (r)
3421                 goto out;
3422
3423         er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
3424
3425         switch (er) {
3426         case EMULATE_DONE:
3427                 return 1;
3428         case EMULATE_DO_MMIO:
3429                 ++vcpu->stat.mmio_exits;
3430                 /* fall through */
3431         case EMULATE_FAIL:
3432                 return 0;
3433         default:
3434                 BUG();
3435         }
3436 out:
3437         return r;
3438 }
3439 EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3440
3441 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3442 {
3443         vcpu->arch.mmu.invlpg(vcpu, gva);
3444         kvm_mmu_flush_tlb(vcpu);
3445         ++vcpu->stat.invlpg;
3446 }
3447 EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3448
3449 void kvm_enable_tdp(void)
3450 {
3451         tdp_enabled = true;
3452 }
3453 EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3454
3455 void kvm_disable_tdp(void)
3456 {
3457         tdp_enabled = false;
3458 }
3459 EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3460
3461 static void free_mmu_pages(struct kvm_vcpu *vcpu)
3462 {
3463         free_page((unsigned long)vcpu->arch.mmu.pae_root);
3464         if (vcpu->arch.mmu.lm_root != NULL)
3465                 free_page((unsigned long)vcpu->arch.mmu.lm_root);
3466 }
3467
3468 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3469 {
3470         struct page *page;
3471         int i;
3472
3473         ASSERT(vcpu);
3474
3475         /*
3476          * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3477          * Therefore we need to allocate shadow page tables in the first
3478          * 4GB of memory, which happens to fit the DMA32 zone.
3479          */
3480         page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3481         if (!page)
3482                 return -ENOMEM;
3483
3484         vcpu->arch.mmu.pae_root = page_address(page);
3485         for (i = 0; i < 4; ++i)
3486                 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
3487
3488         return 0;
3489 }
3490
3491 int kvm_mmu_create(struct kvm_vcpu *vcpu)
3492 {
3493         ASSERT(vcpu);
3494         ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
3495
3496         return alloc_mmu_pages(vcpu);
3497 }
3498
3499 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3500 {
3501         ASSERT(vcpu);
3502         ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
3503
3504         return init_kvm_mmu(vcpu);
3505 }
3506
3507 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
3508 {
3509         struct kvm_mmu_page *sp;
3510
3511         list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
3512                 int i;
3513                 u64 *pt;
3514
3515                 if (!test_bit(slot, sp->slot_bitmap))
3516                         continue;
3517
3518                 pt = sp->spt;
3519                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3520                         if (!is_shadow_present_pte(pt[i]) ||
3521                               !is_last_spte(pt[i], sp->role.level))
3522                                 continue;
3523
3524                         if (is_large_pte(pt[i])) {
3525                                 drop_spte(kvm, &pt[i]);
3526                                 --kvm->stat.lpages;
3527                                 continue;
3528                         }
3529
3530                         /* avoid RMW */
3531                         if (is_writable_pte(pt[i]))
3532                                 mmu_spte_update(&pt[i],
3533                                                 pt[i] & ~PT_WRITABLE_MASK);
3534                 }
3535         }
3536         kvm_flush_remote_tlbs(kvm);
3537 }
3538
3539 void kvm_mmu_zap_all(struct kvm *kvm)
3540 {
3541         struct kvm_mmu_page *sp, *node;
3542         LIST_HEAD(invalid_list);
3543
3544         spin_lock(&kvm->mmu_lock);
3545 restart:
3546         list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
3547                 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
3548                         goto restart;
3549
3550         kvm_mmu_commit_zap_page(kvm, &invalid_list);
3551         spin_unlock(&kvm->mmu_lock);
3552 }
3553
3554 static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3555                                                struct list_head *invalid_list)
3556 {
3557         struct kvm_mmu_page *page;
3558
3559         page = container_of(kvm->arch.active_mmu_pages.prev,
3560                             struct kvm_mmu_page, link);
3561         return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
3562 }
3563
3564 static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
3565 {
3566         struct kvm *kvm;
3567         struct kvm *kvm_freed = NULL;
3568         int nr_to_scan = sc->nr_to_scan;
3569
3570         if (nr_to_scan == 0)
3571                 goto out;
3572
3573         raw_spin_lock(&kvm_lock);
3574
3575         list_for_each_entry(kvm, &vm_list, vm_list) {
3576                 int idx, freed_pages;
3577                 LIST_HEAD(invalid_list);
3578
3579                 idx = srcu_read_lock(&kvm->srcu);
3580                 spin_lock(&kvm->mmu_lock);
3581                 if (!kvm_freed && nr_to_scan > 0 &&
3582                     kvm->arch.n_used_mmu_pages > 0) {
3583                         freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3584                                                           &invalid_list);
3585                         kvm_freed = kvm;
3586                 }
3587                 nr_to_scan--;
3588
3589                 kvm_mmu_commit_zap_page(kvm, &invalid_list);
3590                 spin_unlock(&kvm->mmu_lock);
3591                 srcu_read_unlock(&kvm->srcu, idx);
3592         }
3593         if (kvm_freed)
3594                 list_move_tail(&kvm_freed->vm_list, &vm_list);
3595
3596         raw_spin_unlock(&kvm_lock);
3597
3598 out:
3599         return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
3600 }
3601
3602 static struct shrinker mmu_shrinker = {
3603         .shrink = mmu_shrink,
3604         .seeks = DEFAULT_SEEKS * 10,
3605 };
3606
3607 static void mmu_destroy_caches(void)
3608 {
3609         if (pte_list_desc_cache)
3610                 kmem_cache_destroy(pte_list_desc_cache);
3611         if (mmu_page_header_cache)
3612                 kmem_cache_destroy(mmu_page_header_cache);
3613 }
3614
3615 int kvm_mmu_module_init(void)
3616 {
3617         pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3618                                             sizeof(struct pte_list_desc),
3619                                             0, 0, NULL);
3620         if (!pte_list_desc_cache)
3621                 goto nomem;
3622
3623         mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3624                                                   sizeof(struct kvm_mmu_page),
3625                                                   0, 0, NULL);
3626         if (!mmu_page_header_cache)
3627                 goto nomem;
3628
3629         if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3630                 goto nomem;
3631
3632         register_shrinker(&mmu_shrinker);
3633
3634         return 0;
3635
3636 nomem:
3637         mmu_destroy_caches();
3638         return -ENOMEM;
3639 }
3640
3641 /*
3642  * Caculate mmu pages needed for kvm.
3643  */
3644 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3645 {
3646         int i;
3647         unsigned int nr_mmu_pages;
3648         unsigned int  nr_pages = 0;
3649         struct kvm_memslots *slots;
3650
3651         slots = kvm_memslots(kvm);
3652
3653         for (i = 0; i < slots->nmemslots; i++)
3654                 nr_pages += slots->memslots[i].npages;
3655
3656         nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3657         nr_mmu_pages = max(nr_mmu_pages,
3658                         (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3659
3660         return nr_mmu_pages;
3661 }
3662
3663 static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3664                                 unsigned len)
3665 {
3666         if (len > buffer->len)
3667                 return NULL;
3668         return buffer->ptr;
3669 }
3670
3671 static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3672                                 unsigned len)
3673 {
3674         void *ret;
3675
3676         ret = pv_mmu_peek_buffer(buffer, len);
3677         if (!ret)
3678                 return ret;
3679         buffer->ptr += len;
3680         buffer->len -= len;
3681         buffer->processed += len;
3682         return ret;
3683 }
3684
3685 static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3686                              gpa_t addr, gpa_t value)
3687 {
3688         int bytes = 8;
3689         int r;
3690
3691         if (!is_long_mode(vcpu) && !is_pae(vcpu))
3692                 bytes = 4;
3693
3694         r = mmu_topup_memory_caches(vcpu);
3695         if (r)
3696                 return r;
3697
3698         if (!emulator_write_phys(vcpu, addr, &value, bytes))
3699                 return -EFAULT;
3700
3701         return 1;
3702 }
3703
3704 static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3705 {
3706         (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
3707         return 1;
3708 }
3709
3710 static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3711 {
3712         spin_lock(&vcpu->kvm->mmu_lock);
3713         mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3714         spin_unlock(&vcpu->kvm->mmu_lock);
3715         return 1;
3716 }
3717
3718 static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3719                              struct kvm_pv_mmu_op_buffer *buffer)
3720 {
3721         struct kvm_mmu_op_header *header;
3722
3723         header = pv_mmu_peek_buffer(buffer, sizeof *header);
3724         if (!header)
3725                 return 0;
3726         switch (header->op) {
3727         case KVM_MMU_OP_WRITE_PTE: {
3728                 struct kvm_mmu_op_write_pte *wpte;
3729
3730                 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3731                 if (!wpte)
3732                         return 0;
3733                 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3734                                         wpte->pte_val);
3735         }
3736         case KVM_MMU_OP_FLUSH_TLB: {
3737                 struct kvm_mmu_op_flush_tlb *ftlb;
3738
3739                 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3740                 if (!ftlb)
3741                         return 0;
3742                 return kvm_pv_mmu_flush_tlb(vcpu);
3743         }
3744         case KVM_MMU_OP_RELEASE_PT: {
3745                 struct kvm_mmu_op_release_pt *rpt;
3746
3747                 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3748                 if (!rpt)
3749                         return 0;
3750                 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3751         }
3752         default: return 0;
3753         }
3754 }
3755
3756 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3757                   gpa_t addr, unsigned long *ret)
3758 {
3759         int r;
3760         struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
3761
3762         buffer->ptr = buffer->buf;
3763         buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3764         buffer->processed = 0;
3765
3766         r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
3767         if (r)
3768                 goto out;
3769
3770         while (buffer->len) {
3771                 r = kvm_pv_mmu_op_one(vcpu, buffer);
3772                 if (r < 0)
3773                         goto out;
3774                 if (r == 0)
3775                         break;
3776         }
3777
3778         r = 1;
3779 out:
3780         *ret = buffer->processed;
3781         return r;
3782 }
3783
3784 int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3785 {
3786         struct kvm_shadow_walk_iterator iterator;
3787         int nr_sptes = 0;
3788
3789         spin_lock(&vcpu->kvm->mmu_lock);
3790         for_each_shadow_entry(vcpu, addr, iterator) {
3791                 sptes[iterator.level-1] = *iterator.sptep;
3792                 nr_sptes++;
3793                 if (!is_shadow_present_pte(*iterator.sptep))
3794                         break;
3795         }
3796         spin_unlock(&vcpu->kvm->mmu_lock);
3797
3798         return nr_sptes;
3799 }
3800 EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3801
3802 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3803 {
3804         ASSERT(vcpu);
3805
3806         destroy_kvm_mmu(vcpu);
3807         free_mmu_pages(vcpu);
3808         mmu_free_memory_caches(vcpu);
3809 }
3810
3811 #ifdef CONFIG_KVM_MMU_AUDIT
3812 #include "mmu_audit.c"
3813 #else
3814 static void mmu_audit_disable(void) { }
3815 #endif
3816
3817 void kvm_mmu_module_exit(void)
3818 {
3819         mmu_destroy_caches();
3820         percpu_counter_destroy(&kvm_total_used_mmu_pages);
3821         unregister_shrinker(&mmu_shrinker);
3822         mmu_audit_disable();
3823 }