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