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