]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kvm/mmu.c
KVM: SVM: Implement MMU helper functions for Nested Nested Paging
[mirror_ubuntu-bionic-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;
c5a78f2b
JR
1451 if (!vcpu->arch.mmu.direct_map
1452 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
cea0f0e7
AK
1453 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1454 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1455 role.quadrant = quadrant;
1456 }
f41d335a 1457 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
7ae680eb
XG
1458 if (!need_sync && sp->unsync)
1459 need_sync = true;
4731d4c7 1460
7ae680eb
XG
1461 if (sp->role.word != role.word)
1462 continue;
4731d4c7 1463
7ae680eb
XG
1464 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1465 break;
e02aa901 1466
7ae680eb
XG
1467 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1468 if (sp->unsync_children) {
a8eeb04a 1469 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
7ae680eb
XG
1470 kvm_mmu_mark_parents_unsync(sp);
1471 } else if (sp->unsync)
1472 kvm_mmu_mark_parents_unsync(sp);
e02aa901 1473
7ae680eb
XG
1474 trace_kvm_mmu_get_page(sp, false);
1475 return sp;
1476 }
dfc5aa00 1477 ++vcpu->kvm->stat.mmu_cache_miss;
2032a93d 1478 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
4db35314
AK
1479 if (!sp)
1480 return sp;
4db35314
AK
1481 sp->gfn = gfn;
1482 sp->role = role;
7ae680eb
XG
1483 hlist_add_head(&sp->hash_link,
1484 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
f6e2c02b 1485 if (!direct) {
b1a36821
MT
1486 if (rmap_write_protect(vcpu->kvm, gfn))
1487 kvm_flush_remote_tlbs(vcpu->kvm);
9f1a122f
XG
1488 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1489 kvm_sync_pages(vcpu, gfn);
1490
4731d4c7
MT
1491 account_shadowed(vcpu->kvm, gfn);
1492 }
131d8279
AK
1493 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1494 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1495 else
1496 nonpaging_prefetch_page(vcpu, sp);
f691fe1d 1497 trace_kvm_mmu_get_page(sp, true);
4db35314 1498 return sp;
cea0f0e7
AK
1499}
1500
2d11123a
AK
1501static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1502 struct kvm_vcpu *vcpu, u64 addr)
1503{
1504 iterator->addr = addr;
1505 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1506 iterator->level = vcpu->arch.mmu.shadow_root_level;
81407ca5
JR
1507
1508 if (iterator->level == PT64_ROOT_LEVEL &&
1509 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1510 !vcpu->arch.mmu.direct_map)
1511 --iterator->level;
1512
2d11123a
AK
1513 if (iterator->level == PT32E_ROOT_LEVEL) {
1514 iterator->shadow_addr
1515 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1516 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1517 --iterator->level;
1518 if (!iterator->shadow_addr)
1519 iterator->level = 0;
1520 }
1521}
1522
1523static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1524{
1525 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1526 return false;
4d88954d
MT
1527
1528 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1529 if (is_large_pte(*iterator->sptep))
1530 return false;
1531
2d11123a
AK
1532 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1533 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1534 return true;
1535}
1536
1537static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1538{
1539 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1540 --iterator->level;
1541}
1542
32ef26a3
AK
1543static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1544{
1545 u64 spte;
1546
1547 spte = __pa(sp->spt)
1548 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1549 | PT_WRITABLE_MASK | PT_USER_MASK;
121eee97 1550 __set_spte(sptep, spte);
32ef26a3
AK
1551}
1552
a3aa51cf
AK
1553static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1554{
1555 if (is_large_pte(*sptep)) {
1556 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1557 kvm_flush_remote_tlbs(vcpu->kvm);
1558 }
1559}
1560
a357bd22
AK
1561static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1562 unsigned direct_access)
1563{
1564 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1565 struct kvm_mmu_page *child;
1566
1567 /*
1568 * For the direct sp, if the guest pte's dirty bit
1569 * changed form clean to dirty, it will corrupt the
1570 * sp's access: allow writable in the read-only sp,
1571 * so we should update the spte at this point to get
1572 * a new sp with the correct access.
1573 */
1574 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1575 if (child->role.access == direct_access)
1576 return;
1577
1578 mmu_page_remove_parent_pte(child, sptep);
1579 __set_spte(sptep, shadow_trap_nonpresent_pte);
1580 kvm_flush_remote_tlbs(vcpu->kvm);
1581 }
1582}
1583
90cb0529 1584static void kvm_mmu_page_unlink_children(struct kvm *kvm,
4db35314 1585 struct kvm_mmu_page *sp)
a436036b 1586{
697fe2e2
AK
1587 unsigned i;
1588 u64 *pt;
1589 u64 ent;
1590
4db35314 1591 pt = sp->spt;
697fe2e2 1592
697fe2e2
AK
1593 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1594 ent = pt[i];
1595
05da4558 1596 if (is_shadow_present_pte(ent)) {
776e6633 1597 if (!is_last_spte(ent, sp->role.level)) {
05da4558
MT
1598 ent &= PT64_BASE_ADDR_MASK;
1599 mmu_page_remove_parent_pte(page_header(ent),
1600 &pt[i]);
1601 } else {
776e6633
MT
1602 if (is_large_pte(ent))
1603 --kvm->stat.lpages;
be38d276
AK
1604 drop_spte(kvm, &pt[i],
1605 shadow_trap_nonpresent_pte);
05da4558
MT
1606 }
1607 }
c7addb90 1608 pt[i] = shadow_trap_nonpresent_pte;
697fe2e2 1609 }
a436036b
AK
1610}
1611
4db35314 1612static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
cea0f0e7 1613{
4db35314 1614 mmu_page_remove_parent_pte(sp, parent_pte);
a436036b
AK
1615}
1616
12b7d28f
AK
1617static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1618{
1619 int i;
988a2cae 1620 struct kvm_vcpu *vcpu;
12b7d28f 1621
988a2cae
GN
1622 kvm_for_each_vcpu(i, vcpu, kvm)
1623 vcpu->arch.last_pte_updated = NULL;
12b7d28f
AK
1624}
1625
31aa2b44 1626static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
a436036b
AK
1627{
1628 u64 *parent_pte;
1629
4db35314
AK
1630 while (sp->multimapped || sp->parent_pte) {
1631 if (!sp->multimapped)
1632 parent_pte = sp->parent_pte;
a436036b
AK
1633 else {
1634 struct kvm_pte_chain *chain;
1635
4db35314 1636 chain = container_of(sp->parent_ptes.first,
a436036b
AK
1637 struct kvm_pte_chain, link);
1638 parent_pte = chain->parent_ptes[0];
1639 }
697fe2e2 1640 BUG_ON(!parent_pte);
4db35314 1641 kvm_mmu_put_page(sp, parent_pte);
d555c333 1642 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
a436036b 1643 }
31aa2b44
AK
1644}
1645
60c8aec6 1646static int mmu_zap_unsync_children(struct kvm *kvm,
7775834a
XG
1647 struct kvm_mmu_page *parent,
1648 struct list_head *invalid_list)
4731d4c7 1649{
60c8aec6
MT
1650 int i, zapped = 0;
1651 struct mmu_page_path parents;
1652 struct kvm_mmu_pages pages;
4731d4c7 1653
60c8aec6 1654 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
4731d4c7 1655 return 0;
60c8aec6
MT
1656
1657 kvm_mmu_pages_init(parent, &parents, &pages);
1658 while (mmu_unsync_walk(parent, &pages)) {
1659 struct kvm_mmu_page *sp;
1660
1661 for_each_sp(pages, sp, parents, i) {
7775834a 1662 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
60c8aec6 1663 mmu_pages_clear_parents(&parents);
77662e00 1664 zapped++;
60c8aec6 1665 }
60c8aec6
MT
1666 kvm_mmu_pages_init(parent, &parents, &pages);
1667 }
1668
1669 return zapped;
4731d4c7
MT
1670}
1671
7775834a
XG
1672static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1673 struct list_head *invalid_list)
31aa2b44 1674{
4731d4c7 1675 int ret;
f691fe1d 1676
7775834a 1677 trace_kvm_mmu_prepare_zap_page(sp);
31aa2b44 1678 ++kvm->stat.mmu_shadow_zapped;
7775834a 1679 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
4db35314 1680 kvm_mmu_page_unlink_children(kvm, sp);
31aa2b44 1681 kvm_mmu_unlink_parents(kvm, sp);
f6e2c02b 1682 if (!sp->role.invalid && !sp->role.direct)
5b5c6a5a 1683 unaccount_shadowed(kvm, sp->gfn);
4731d4c7
MT
1684 if (sp->unsync)
1685 kvm_unlink_unsync_page(kvm, sp);
4db35314 1686 if (!sp->root_count) {
54a4f023
GJ
1687 /* Count self */
1688 ret++;
7775834a 1689 list_move(&sp->link, invalid_list);
2e53d63a 1690 } else {
5b5c6a5a 1691 list_move(&sp->link, &kvm->arch.active_mmu_pages);
2e53d63a
MT
1692 kvm_reload_remote_mmus(kvm);
1693 }
7775834a
XG
1694
1695 sp->role.invalid = 1;
12b7d28f 1696 kvm_mmu_reset_last_pte_updated(kvm);
4731d4c7 1697 return ret;
a436036b
AK
1698}
1699
7775834a
XG
1700static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1701 struct list_head *invalid_list)
1702{
1703 struct kvm_mmu_page *sp;
1704
1705 if (list_empty(invalid_list))
1706 return;
1707
1708 kvm_flush_remote_tlbs(kvm);
1709
1710 do {
1711 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1712 WARN_ON(!sp->role.invalid || sp->root_count);
1713 kvm_mmu_free_page(kvm, sp);
1714 } while (!list_empty(invalid_list));
1715
1716}
1717
82ce2c96
IE
1718/*
1719 * Changing the number of mmu pages allocated to the vm
49d5ca26 1720 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
82ce2c96 1721 */
49d5ca26 1722void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
82ce2c96 1723{
d98ba053 1724 LIST_HEAD(invalid_list);
82ce2c96
IE
1725 /*
1726 * If we set the number of mmu pages to be smaller be than the
1727 * number of actived pages , we must to free some mmu pages before we
1728 * change the value
1729 */
1730
49d5ca26
DH
1731 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1732 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
77662e00 1733 !list_empty(&kvm->arch.active_mmu_pages)) {
82ce2c96
IE
1734 struct kvm_mmu_page *page;
1735
f05e70ac 1736 page = container_of(kvm->arch.active_mmu_pages.prev,
82ce2c96 1737 struct kvm_mmu_page, link);
80b63faf
XF
1738 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1739 kvm_mmu_commit_zap_page(kvm, &invalid_list);
82ce2c96 1740 }
49d5ca26 1741 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
82ce2c96 1742 }
82ce2c96 1743
49d5ca26 1744 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
82ce2c96
IE
1745}
1746
f67a46f4 1747static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
a436036b 1748{
4db35314 1749 struct kvm_mmu_page *sp;
f41d335a 1750 struct hlist_node *node;
d98ba053 1751 LIST_HEAD(invalid_list);
a436036b
AK
1752 int r;
1753
9ad17b10 1754 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
a436036b 1755 r = 0;
f41d335a
XG
1756
1757 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
9ad17b10 1758 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
7ae680eb
XG
1759 sp->role.word);
1760 r = 1;
f41d335a 1761 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
7ae680eb 1762 }
d98ba053 1763 kvm_mmu_commit_zap_page(kvm, &invalid_list);
a436036b 1764 return r;
cea0f0e7
AK
1765}
1766
f67a46f4 1767static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
97a0a01e 1768{
4db35314 1769 struct kvm_mmu_page *sp;
f41d335a 1770 struct hlist_node *node;
d98ba053 1771 LIST_HEAD(invalid_list);
97a0a01e 1772
f41d335a 1773 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
9ad17b10 1774 pgprintk("%s: zap %llx %x\n",
7ae680eb 1775 __func__, gfn, sp->role.word);
f41d335a 1776 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
97a0a01e 1777 }
d98ba053 1778 kvm_mmu_commit_zap_page(kvm, &invalid_list);
97a0a01e
AK
1779}
1780
38c335f1 1781static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
6aa8b732 1782{
bc6678a3 1783 int slot = memslot_id(kvm, gfn);
4db35314 1784 struct kvm_mmu_page *sp = page_header(__pa(pte));
6aa8b732 1785
291f26bc 1786 __set_bit(slot, sp->slot_bitmap);
6aa8b732
AK
1787}
1788
6844dec6
MT
1789static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1790{
1791 int i;
1792 u64 *pt = sp->spt;
1793
1794 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1795 return;
1796
1797 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1798 if (pt[i] == shadow_notrap_nonpresent_pte)
d555c333 1799 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
6844dec6
MT
1800 }
1801}
1802
74be52e3
SY
1803/*
1804 * The function is based on mtrr_type_lookup() in
1805 * arch/x86/kernel/cpu/mtrr/generic.c
1806 */
1807static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1808 u64 start, u64 end)
1809{
1810 int i;
1811 u64 base, mask;
1812 u8 prev_match, curr_match;
1813 int num_var_ranges = KVM_NR_VAR_MTRR;
1814
1815 if (!mtrr_state->enabled)
1816 return 0xFF;
1817
1818 /* Make end inclusive end, instead of exclusive */
1819 end--;
1820
1821 /* Look in fixed ranges. Just return the type as per start */
1822 if (mtrr_state->have_fixed && (start < 0x100000)) {
1823 int idx;
1824
1825 if (start < 0x80000) {
1826 idx = 0;
1827 idx += (start >> 16);
1828 return mtrr_state->fixed_ranges[idx];
1829 } else if (start < 0xC0000) {
1830 idx = 1 * 8;
1831 idx += ((start - 0x80000) >> 14);
1832 return mtrr_state->fixed_ranges[idx];
1833 } else if (start < 0x1000000) {
1834 idx = 3 * 8;
1835 idx += ((start - 0xC0000) >> 12);
1836 return mtrr_state->fixed_ranges[idx];
1837 }
1838 }
1839
1840 /*
1841 * Look in variable ranges
1842 * Look of multiple ranges matching this address and pick type
1843 * as per MTRR precedence
1844 */
1845 if (!(mtrr_state->enabled & 2))
1846 return mtrr_state->def_type;
1847
1848 prev_match = 0xFF;
1849 for (i = 0; i < num_var_ranges; ++i) {
1850 unsigned short start_state, end_state;
1851
1852 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1853 continue;
1854
1855 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1856 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1857 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1858 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1859
1860 start_state = ((start & mask) == (base & mask));
1861 end_state = ((end & mask) == (base & mask));
1862 if (start_state != end_state)
1863 return 0xFE;
1864
1865 if ((start & mask) != (base & mask))
1866 continue;
1867
1868 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1869 if (prev_match == 0xFF) {
1870 prev_match = curr_match;
1871 continue;
1872 }
1873
1874 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1875 curr_match == MTRR_TYPE_UNCACHABLE)
1876 return MTRR_TYPE_UNCACHABLE;
1877
1878 if ((prev_match == MTRR_TYPE_WRBACK &&
1879 curr_match == MTRR_TYPE_WRTHROUGH) ||
1880 (prev_match == MTRR_TYPE_WRTHROUGH &&
1881 curr_match == MTRR_TYPE_WRBACK)) {
1882 prev_match = MTRR_TYPE_WRTHROUGH;
1883 curr_match = MTRR_TYPE_WRTHROUGH;
1884 }
1885
1886 if (prev_match != curr_match)
1887 return MTRR_TYPE_UNCACHABLE;
1888 }
1889
1890 if (prev_match != 0xFF)
1891 return prev_match;
1892
1893 return mtrr_state->def_type;
1894}
1895
4b12f0de 1896u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
74be52e3
SY
1897{
1898 u8 mtrr;
1899
1900 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1901 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1902 if (mtrr == 0xfe || mtrr == 0xff)
1903 mtrr = MTRR_TYPE_WRBACK;
1904 return mtrr;
1905}
4b12f0de 1906EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
74be52e3 1907
9cf5cf5a
XG
1908static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1909{
1910 trace_kvm_mmu_unsync_page(sp);
1911 ++vcpu->kvm->stat.mmu_unsync;
1912 sp->unsync = 1;
1913
1914 kvm_mmu_mark_parents_unsync(sp);
1915 mmu_convert_notrap(sp);
1916}
1917
1918static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
4731d4c7 1919{
4731d4c7 1920 struct kvm_mmu_page *s;
f41d335a 1921 struct hlist_node *node;
9cf5cf5a 1922
f41d335a 1923 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
7ae680eb 1924 if (s->unsync)
4731d4c7 1925 continue;
9cf5cf5a
XG
1926 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1927 __kvm_unsync_page(vcpu, s);
4731d4c7 1928 }
4731d4c7
MT
1929}
1930
1931static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1932 bool can_unsync)
1933{
9cf5cf5a 1934 struct kvm_mmu_page *s;
f41d335a 1935 struct hlist_node *node;
9cf5cf5a
XG
1936 bool need_unsync = false;
1937
f41d335a 1938 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
36a2e677
XG
1939 if (!can_unsync)
1940 return 1;
1941
9cf5cf5a 1942 if (s->role.level != PT_PAGE_TABLE_LEVEL)
4731d4c7 1943 return 1;
9cf5cf5a
XG
1944
1945 if (!need_unsync && !s->unsync) {
36a2e677 1946 if (!oos_shadow)
9cf5cf5a
XG
1947 return 1;
1948 need_unsync = true;
1949 }
4731d4c7 1950 }
9cf5cf5a
XG
1951 if (need_unsync)
1952 kvm_unsync_pages(vcpu, gfn);
4731d4c7
MT
1953 return 0;
1954}
1955
d555c333 1956static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1e73f9dd 1957 unsigned pte_access, int user_fault,
852e3c19 1958 int write_fault, int dirty, int level,
c2d0ee46 1959 gfn_t gfn, pfn_t pfn, bool speculative,
1403283a 1960 bool can_unsync, bool reset_host_protection)
1c4f1fd6
AK
1961{
1962 u64 spte;
1e73f9dd 1963 int ret = 0;
64d4d521 1964
1c4f1fd6
AK
1965 /*
1966 * We don't set the accessed bit, since we sometimes want to see
1967 * whether the guest actually used the pte (in order to detect
1968 * demand paging).
1969 */
4132779b 1970 spte = shadow_base_present_pte;
947da538 1971 if (!speculative)
3201b5d9 1972 spte |= shadow_accessed_mask;
1c4f1fd6
AK
1973 if (!dirty)
1974 pte_access &= ~ACC_WRITE_MASK;
7b52345e
SY
1975 if (pte_access & ACC_EXEC_MASK)
1976 spte |= shadow_x_mask;
1977 else
1978 spte |= shadow_nx_mask;
1c4f1fd6 1979 if (pte_access & ACC_USER_MASK)
7b52345e 1980 spte |= shadow_user_mask;
852e3c19 1981 if (level > PT_PAGE_TABLE_LEVEL)
05da4558 1982 spte |= PT_PAGE_SIZE_MASK;
c5a78f2b 1983 if (vcpu->arch.mmu.direct_map)
4b12f0de
SY
1984 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1985 kvm_is_mmio_pfn(pfn));
1c4f1fd6 1986
1403283a
IE
1987 if (reset_host_protection)
1988 spte |= SPTE_HOST_WRITEABLE;
1989
35149e21 1990 spte |= (u64)pfn << PAGE_SHIFT;
1c4f1fd6
AK
1991
1992 if ((pte_access & ACC_WRITE_MASK)
c5a78f2b
JR
1993 || (!vcpu->arch.mmu.direct_map && write_fault
1994 && !is_write_protection(vcpu) && !user_fault)) {
1c4f1fd6 1995
852e3c19
JR
1996 if (level > PT_PAGE_TABLE_LEVEL &&
1997 has_wrprotected_page(vcpu->kvm, gfn, level)) {
38187c83 1998 ret = 1;
be38d276
AK
1999 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
2000 goto done;
38187c83
MT
2001 }
2002
1c4f1fd6 2003 spte |= PT_WRITABLE_MASK;
1c4f1fd6 2004
c5a78f2b
JR
2005 if (!vcpu->arch.mmu.direct_map
2006 && !(pte_access & ACC_WRITE_MASK))
69325a12
AK
2007 spte &= ~PT_USER_MASK;
2008
ecc5589f
MT
2009 /*
2010 * Optimization: for pte sync, if spte was writable the hash
2011 * lookup is unnecessary (and expensive). Write protection
2012 * is responsibility of mmu_get_page / kvm_sync_page.
2013 * Same reasoning can be applied to dirty page accounting.
2014 */
8dae4445 2015 if (!can_unsync && is_writable_pte(*sptep))
ecc5589f
MT
2016 goto set_pte;
2017
4731d4c7 2018 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
9ad17b10 2019 pgprintk("%s: found shadow page for %llx, marking ro\n",
b8688d51 2020 __func__, gfn);
1e73f9dd 2021 ret = 1;
1c4f1fd6 2022 pte_access &= ~ACC_WRITE_MASK;
8dae4445 2023 if (is_writable_pte(spte))
1c4f1fd6 2024 spte &= ~PT_WRITABLE_MASK;
1c4f1fd6
AK
2025 }
2026 }
2027
1c4f1fd6
AK
2028 if (pte_access & ACC_WRITE_MASK)
2029 mark_page_dirty(vcpu->kvm, gfn);
2030
38187c83 2031set_pte:
b79b93f9 2032 update_spte(sptep, spte);
be38d276 2033done:
1e73f9dd
MT
2034 return ret;
2035}
2036
d555c333 2037static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1e73f9dd
MT
2038 unsigned pt_access, unsigned pte_access,
2039 int user_fault, int write_fault, int dirty,
852e3c19 2040 int *ptwrite, int level, gfn_t gfn,
1403283a
IE
2041 pfn_t pfn, bool speculative,
2042 bool reset_host_protection)
1e73f9dd
MT
2043{
2044 int was_rmapped = 0;
53a27b39 2045 int rmap_count;
1e73f9dd
MT
2046
2047 pgprintk("%s: spte %llx access %x write_fault %d"
9ad17b10 2048 " user_fault %d gfn %llx\n",
d555c333 2049 __func__, *sptep, pt_access,
1e73f9dd
MT
2050 write_fault, user_fault, gfn);
2051
d555c333 2052 if (is_rmap_spte(*sptep)) {
1e73f9dd
MT
2053 /*
2054 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2055 * the parent of the now unreachable PTE.
2056 */
852e3c19
JR
2057 if (level > PT_PAGE_TABLE_LEVEL &&
2058 !is_large_pte(*sptep)) {
1e73f9dd 2059 struct kvm_mmu_page *child;
d555c333 2060 u64 pte = *sptep;
1e73f9dd
MT
2061
2062 child = page_header(pte & PT64_BASE_ADDR_MASK);
d555c333 2063 mmu_page_remove_parent_pte(child, sptep);
3be2264b
MT
2064 __set_spte(sptep, shadow_trap_nonpresent_pte);
2065 kvm_flush_remote_tlbs(vcpu->kvm);
d555c333 2066 } else if (pfn != spte_to_pfn(*sptep)) {
9ad17b10 2067 pgprintk("hfn old %llx new %llx\n",
d555c333 2068 spte_to_pfn(*sptep), pfn);
be38d276 2069 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
91546356 2070 kvm_flush_remote_tlbs(vcpu->kvm);
6bed6b9e
JR
2071 } else
2072 was_rmapped = 1;
1e73f9dd 2073 }
852e3c19 2074
d555c333 2075 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
1403283a
IE
2076 dirty, level, gfn, pfn, speculative, true,
2077 reset_host_protection)) {
1e73f9dd
MT
2078 if (write_fault)
2079 *ptwrite = 1;
5304efde 2080 kvm_mmu_flush_tlb(vcpu);
a378b4e6 2081 }
1e73f9dd 2082
d555c333 2083 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
9ad17b10 2084 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
d555c333 2085 is_large_pte(*sptep)? "2MB" : "4kB",
a205bc19
JR
2086 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2087 *sptep, sptep);
d555c333 2088 if (!was_rmapped && is_large_pte(*sptep))
05da4558
MT
2089 ++vcpu->kvm->stat.lpages;
2090
d555c333 2091 page_header_update_slot(vcpu->kvm, sptep, gfn);
1c4f1fd6 2092 if (!was_rmapped) {
44ad9944 2093 rmap_count = rmap_add(vcpu, sptep, gfn);
53a27b39 2094 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
852e3c19 2095 rmap_recycle(vcpu, sptep, gfn);
1c4f1fd6 2096 }
9ed5520d 2097 kvm_release_pfn_clean(pfn);
1b7fcd32 2098 if (speculative) {
d555c333 2099 vcpu->arch.last_pte_updated = sptep;
1b7fcd32
AK
2100 vcpu->arch.last_pte_gfn = gfn;
2101 }
1c4f1fd6
AK
2102}
2103
6aa8b732
AK
2104static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2105{
2106}
2107
957ed9ef
XG
2108static struct kvm_memory_slot *
2109pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2110{
2111 struct kvm_memory_slot *slot;
2112
2113 slot = gfn_to_memslot(vcpu->kvm, gfn);
2114 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2115 (no_dirty_log && slot->dirty_bitmap))
2116 slot = NULL;
2117
2118 return slot;
2119}
2120
2121static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2122 bool no_dirty_log)
2123{
2124 struct kvm_memory_slot *slot;
2125 unsigned long hva;
2126
2127 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2128 if (!slot) {
2129 get_page(bad_page);
2130 return page_to_pfn(bad_page);
2131 }
2132
2133 hva = gfn_to_hva_memslot(slot, gfn);
2134
2135 return hva_to_pfn_atomic(vcpu->kvm, hva);
2136}
2137
2138static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2139 struct kvm_mmu_page *sp,
2140 u64 *start, u64 *end)
2141{
2142 struct page *pages[PTE_PREFETCH_NUM];
2143 unsigned access = sp->role.access;
2144 int i, ret;
2145 gfn_t gfn;
2146
2147 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2148 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2149 return -1;
2150
2151 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2152 if (ret <= 0)
2153 return -1;
2154
2155 for (i = 0; i < ret; i++, gfn++, start++)
2156 mmu_set_spte(vcpu, start, ACC_ALL,
2157 access, 0, 0, 1, NULL,
2158 sp->role.level, gfn,
2159 page_to_pfn(pages[i]), true, true);
2160
2161 return 0;
2162}
2163
2164static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2165 struct kvm_mmu_page *sp, u64 *sptep)
2166{
2167 u64 *spte, *start = NULL;
2168 int i;
2169
2170 WARN_ON(!sp->role.direct);
2171
2172 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2173 spte = sp->spt + i;
2174
2175 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2176 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2177 if (!start)
2178 continue;
2179 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2180 break;
2181 start = NULL;
2182 } else if (!start)
2183 start = spte;
2184 }
2185}
2186
2187static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2188{
2189 struct kvm_mmu_page *sp;
2190
2191 /*
2192 * Since it's no accessed bit on EPT, it's no way to
2193 * distinguish between actually accessed translations
2194 * and prefetched, so disable pte prefetch if EPT is
2195 * enabled.
2196 */
2197 if (!shadow_accessed_mask)
2198 return;
2199
2200 sp = page_header(__pa(sptep));
2201 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2202 return;
2203
2204 __direct_pte_prefetch(vcpu, sp, sptep);
2205}
2206
9f652d21 2207static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
852e3c19 2208 int level, gfn_t gfn, pfn_t pfn)
140754bc 2209{
9f652d21 2210 struct kvm_shadow_walk_iterator iterator;
140754bc 2211 struct kvm_mmu_page *sp;
9f652d21 2212 int pt_write = 0;
140754bc 2213 gfn_t pseudo_gfn;
6aa8b732 2214
9f652d21 2215 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
852e3c19 2216 if (iterator.level == level) {
9f652d21
AK
2217 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2218 0, write, 1, &pt_write,
1403283a 2219 level, gfn, pfn, false, true);
957ed9ef 2220 direct_pte_prefetch(vcpu, iterator.sptep);
9f652d21
AK
2221 ++vcpu->stat.pf_fixed;
2222 break;
6aa8b732
AK
2223 }
2224
9f652d21 2225 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
c9fa0b3b
LJ
2226 u64 base_addr = iterator.addr;
2227
2228 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2229 pseudo_gfn = base_addr >> PAGE_SHIFT;
9f652d21
AK
2230 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2231 iterator.level - 1,
2232 1, ACC_ALL, iterator.sptep);
2233 if (!sp) {
2234 pgprintk("nonpaging_map: ENOMEM\n");
2235 kvm_release_pfn_clean(pfn);
2236 return -ENOMEM;
2237 }
140754bc 2238
d555c333
AK
2239 __set_spte(iterator.sptep,
2240 __pa(sp->spt)
2241 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2242 | shadow_user_mask | shadow_x_mask);
9f652d21
AK
2243 }
2244 }
2245 return pt_write;
6aa8b732
AK
2246}
2247
bf998156
HY
2248static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
2249{
2250 char buf[1];
2251 void __user *hva;
2252 int r;
2253
2254 /* Touch the page, so send SIGBUS */
2255 hva = (void __user *)gfn_to_hva(kvm, gfn);
2256 r = copy_from_user(buf, hva, 1);
2257}
2258
2259static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2260{
2261 kvm_release_pfn_clean(pfn);
2262 if (is_hwpoison_pfn(pfn)) {
2263 kvm_send_hwpoison_signal(kvm, gfn);
2264 return 0;
edba23e5
GN
2265 } else if (is_fault_pfn(pfn))
2266 return -EFAULT;
2267
bf998156
HY
2268 return 1;
2269}
2270
10589a46
MT
2271static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2272{
2273 int r;
852e3c19 2274 int level;
35149e21 2275 pfn_t pfn;
e930bffe 2276 unsigned long mmu_seq;
aaee2c94 2277
852e3c19
JR
2278 level = mapping_level(vcpu, gfn);
2279
2280 /*
2281 * This path builds a PAE pagetable - so we can map 2mb pages at
2282 * maximum. Therefore check if the level is larger than that.
2283 */
2284 if (level > PT_DIRECTORY_LEVEL)
2285 level = PT_DIRECTORY_LEVEL;
2286
2287 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
05da4558 2288
e930bffe 2289 mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 2290 smp_rmb();
35149e21 2291 pfn = gfn_to_pfn(vcpu->kvm, gfn);
aaee2c94 2292
d196e343 2293 /* mmio */
bf998156
HY
2294 if (is_error_pfn(pfn))
2295 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
d196e343 2296
aaee2c94 2297 spin_lock(&vcpu->kvm->mmu_lock);
e930bffe
AA
2298 if (mmu_notifier_retry(vcpu, mmu_seq))
2299 goto out_unlock;
eb787d10 2300 kvm_mmu_free_some_pages(vcpu);
852e3c19 2301 r = __direct_map(vcpu, v, write, level, gfn, pfn);
aaee2c94
MT
2302 spin_unlock(&vcpu->kvm->mmu_lock);
2303
aaee2c94 2304
10589a46 2305 return r;
e930bffe
AA
2306
2307out_unlock:
2308 spin_unlock(&vcpu->kvm->mmu_lock);
2309 kvm_release_pfn_clean(pfn);
2310 return 0;
10589a46
MT
2311}
2312
2313
17ac10ad
AK
2314static void mmu_free_roots(struct kvm_vcpu *vcpu)
2315{
2316 int i;
4db35314 2317 struct kvm_mmu_page *sp;
d98ba053 2318 LIST_HEAD(invalid_list);
17ac10ad 2319
ad312c7c 2320 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
7b53aa56 2321 return;
aaee2c94 2322 spin_lock(&vcpu->kvm->mmu_lock);
81407ca5
JR
2323 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2324 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2325 vcpu->arch.mmu.direct_map)) {
ad312c7c 2326 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad 2327
4db35314
AK
2328 sp = page_header(root);
2329 --sp->root_count;
d98ba053
XG
2330 if (!sp->root_count && sp->role.invalid) {
2331 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2332 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2333 }
ad312c7c 2334 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
aaee2c94 2335 spin_unlock(&vcpu->kvm->mmu_lock);
17ac10ad
AK
2336 return;
2337 }
17ac10ad 2338 for (i = 0; i < 4; ++i) {
ad312c7c 2339 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad 2340
417726a3 2341 if (root) {
417726a3 2342 root &= PT64_BASE_ADDR_MASK;
4db35314
AK
2343 sp = page_header(root);
2344 --sp->root_count;
2e53d63a 2345 if (!sp->root_count && sp->role.invalid)
d98ba053
XG
2346 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2347 &invalid_list);
417726a3 2348 }
ad312c7c 2349 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 2350 }
d98ba053 2351 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
aaee2c94 2352 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 2353 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
17ac10ad
AK
2354}
2355
8986ecc0
MT
2356static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2357{
2358 int ret = 0;
2359
2360 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
a8eeb04a 2361 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
8986ecc0
MT
2362 ret = 1;
2363 }
2364
2365 return ret;
2366}
2367
651dd37a
JR
2368static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2369{
2370 struct kvm_mmu_page *sp;
2371 int i;
2372
2373 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2374 spin_lock(&vcpu->kvm->mmu_lock);
2375 kvm_mmu_free_some_pages(vcpu);
2376 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2377 1, ACC_ALL, NULL);
2378 ++sp->root_count;
2379 spin_unlock(&vcpu->kvm->mmu_lock);
2380 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2381 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2382 for (i = 0; i < 4; ++i) {
2383 hpa_t root = vcpu->arch.mmu.pae_root[i];
2384
2385 ASSERT(!VALID_PAGE(root));
2386 spin_lock(&vcpu->kvm->mmu_lock);
2387 kvm_mmu_free_some_pages(vcpu);
2388 sp = kvm_mmu_get_page(vcpu, i << 30, i << 30,
2389 PT32_ROOT_LEVEL, 1, ACC_ALL,
2390 NULL);
2391 root = __pa(sp->spt);
2392 ++sp->root_count;
2393 spin_unlock(&vcpu->kvm->mmu_lock);
2394 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
2395 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
2396 }
2397 } else
2398 BUG();
2399
2400 return 0;
2401}
2402
2403static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
17ac10ad 2404{
4db35314 2405 struct kvm_mmu_page *sp;
81407ca5
JR
2406 u64 pdptr, pm_mask;
2407 gfn_t root_gfn;
2408 int i;
3bb65a22 2409
5777ed34 2410 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
17ac10ad 2411
651dd37a
JR
2412 if (mmu_check_root(vcpu, root_gfn))
2413 return 1;
2414
2415 /*
2416 * Do we shadow a long mode page table? If so we need to
2417 * write-protect the guests page table root.
2418 */
2419 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
ad312c7c 2420 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad
AK
2421
2422 ASSERT(!VALID_PAGE(root));
651dd37a 2423
8facbbff 2424 spin_lock(&vcpu->kvm->mmu_lock);
24955b6c 2425 kvm_mmu_free_some_pages(vcpu);
651dd37a
JR
2426 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2427 0, ACC_ALL, NULL);
4db35314
AK
2428 root = __pa(sp->spt);
2429 ++sp->root_count;
8facbbff 2430 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 2431 vcpu->arch.mmu.root_hpa = root;
8986ecc0 2432 return 0;
17ac10ad 2433 }
f87f9288 2434
651dd37a
JR
2435 /*
2436 * We shadow a 32 bit page table. This may be a legacy 2-level
81407ca5
JR
2437 * or a PAE 3-level page table. In either case we need to be aware that
2438 * the shadow page table may be a PAE or a long mode page table.
651dd37a 2439 */
81407ca5
JR
2440 pm_mask = PT_PRESENT_MASK;
2441 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2442 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2443
17ac10ad 2444 for (i = 0; i < 4; ++i) {
ad312c7c 2445 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad
AK
2446
2447 ASSERT(!VALID_PAGE(root));
ad312c7c 2448 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
d41d1895 2449 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
43a3795a 2450 if (!is_present_gpte(pdptr)) {
ad312c7c 2451 vcpu->arch.mmu.pae_root[i] = 0;
417726a3
AK
2452 continue;
2453 }
6de4f3ad 2454 root_gfn = pdptr >> PAGE_SHIFT;
f87f9288
JR
2455 if (mmu_check_root(vcpu, root_gfn))
2456 return 1;
5a7388c2 2457 }
8facbbff 2458 spin_lock(&vcpu->kvm->mmu_lock);
24955b6c 2459 kvm_mmu_free_some_pages(vcpu);
4db35314 2460 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
651dd37a 2461 PT32_ROOT_LEVEL, 0,
f7d9c7b7 2462 ACC_ALL, NULL);
4db35314
AK
2463 root = __pa(sp->spt);
2464 ++sp->root_count;
8facbbff
AK
2465 spin_unlock(&vcpu->kvm->mmu_lock);
2466
81407ca5
JR
2467 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
2468 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
17ac10ad 2469 }
81407ca5
JR
2470
2471 /*
2472 * If we shadow a 32 bit page table with a long mode page
2473 * table we enter this path.
2474 */
2475 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2476 if (vcpu->arch.mmu.lm_root == NULL) {
2477 /*
2478 * The additional page necessary for this is only
2479 * allocated on demand.
2480 */
2481
2482 u64 *lm_root;
2483
2484 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2485 if (lm_root == NULL)
2486 return 1;
2487
2488 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2489
2490 vcpu->arch.mmu.lm_root = lm_root;
2491 }
2492
2493 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2494 }
2495
8986ecc0 2496 return 0;
17ac10ad
AK
2497}
2498
651dd37a
JR
2499static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2500{
2501 if (vcpu->arch.mmu.direct_map)
2502 return mmu_alloc_direct_roots(vcpu);
2503 else
2504 return mmu_alloc_shadow_roots(vcpu);
2505}
2506
0ba73cda
MT
2507static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2508{
2509 int i;
2510 struct kvm_mmu_page *sp;
2511
81407ca5
JR
2512 if (vcpu->arch.mmu.direct_map)
2513 return;
2514
0ba73cda
MT
2515 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2516 return;
81407ca5 2517 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
0ba73cda
MT
2518 hpa_t root = vcpu->arch.mmu.root_hpa;
2519 sp = page_header(root);
2520 mmu_sync_children(vcpu, sp);
2521 return;
2522 }
2523 for (i = 0; i < 4; ++i) {
2524 hpa_t root = vcpu->arch.mmu.pae_root[i];
2525
8986ecc0 2526 if (root && VALID_PAGE(root)) {
0ba73cda
MT
2527 root &= PT64_BASE_ADDR_MASK;
2528 sp = page_header(root);
2529 mmu_sync_children(vcpu, sp);
2530 }
2531 }
2532}
2533
2534void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2535{
2536 spin_lock(&vcpu->kvm->mmu_lock);
2537 mmu_sync_roots(vcpu);
6cffe8ca 2538 spin_unlock(&vcpu->kvm->mmu_lock);
0ba73cda
MT
2539}
2540
1871c602
GN
2541static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2542 u32 access, u32 *error)
6aa8b732 2543{
1871c602
GN
2544 if (error)
2545 *error = 0;
6aa8b732
AK
2546 return vaddr;
2547}
2548
6539e738
JR
2549static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
2550 u32 access, u32 *error)
2551{
2552 if (error)
2553 *error = 0;
2554 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2555}
2556
6aa8b732 2557static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
3f3e7124 2558 u32 error_code)
6aa8b732 2559{
e833240f 2560 gfn_t gfn;
e2dec939 2561 int r;
6aa8b732 2562
b8688d51 2563 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
e2dec939
AK
2564 r = mmu_topup_memory_caches(vcpu);
2565 if (r)
2566 return r;
714b93da 2567
6aa8b732 2568 ASSERT(vcpu);
ad312c7c 2569 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 2570
e833240f 2571 gfn = gva >> PAGE_SHIFT;
6aa8b732 2572
e833240f
AK
2573 return nonpaging_map(vcpu, gva & PAGE_MASK,
2574 error_code & PFERR_WRITE_MASK, gfn);
6aa8b732
AK
2575}
2576
fb72d167
JR
2577static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2578 u32 error_code)
2579{
35149e21 2580 pfn_t pfn;
fb72d167 2581 int r;
852e3c19 2582 int level;
05da4558 2583 gfn_t gfn = gpa >> PAGE_SHIFT;
e930bffe 2584 unsigned long mmu_seq;
fb72d167
JR
2585
2586 ASSERT(vcpu);
2587 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2588
2589 r = mmu_topup_memory_caches(vcpu);
2590 if (r)
2591 return r;
2592
852e3c19
JR
2593 level = mapping_level(vcpu, gfn);
2594
2595 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2596
e930bffe 2597 mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 2598 smp_rmb();
35149e21 2599 pfn = gfn_to_pfn(vcpu->kvm, gfn);
bf998156
HY
2600 if (is_error_pfn(pfn))
2601 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
fb72d167 2602 spin_lock(&vcpu->kvm->mmu_lock);
e930bffe
AA
2603 if (mmu_notifier_retry(vcpu, mmu_seq))
2604 goto out_unlock;
fb72d167
JR
2605 kvm_mmu_free_some_pages(vcpu);
2606 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
852e3c19 2607 level, gfn, pfn);
fb72d167 2608 spin_unlock(&vcpu->kvm->mmu_lock);
fb72d167
JR
2609
2610 return r;
e930bffe
AA
2611
2612out_unlock:
2613 spin_unlock(&vcpu->kvm->mmu_lock);
2614 kvm_release_pfn_clean(pfn);
2615 return 0;
fb72d167
JR
2616}
2617
6aa8b732
AK
2618static void nonpaging_free(struct kvm_vcpu *vcpu)
2619{
17ac10ad 2620 mmu_free_roots(vcpu);
6aa8b732
AK
2621}
2622
52fde8df
JR
2623static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2624 struct kvm_mmu *context)
6aa8b732 2625{
6aa8b732
AK
2626 context->new_cr3 = nonpaging_new_cr3;
2627 context->page_fault = nonpaging_page_fault;
6aa8b732
AK
2628 context->gva_to_gpa = nonpaging_gva_to_gpa;
2629 context->free = nonpaging_free;
c7addb90 2630 context->prefetch_page = nonpaging_prefetch_page;
e8bc217a 2631 context->sync_page = nonpaging_sync_page;
a7052897 2632 context->invlpg = nonpaging_invlpg;
cea0f0e7 2633 context->root_level = 0;
6aa8b732 2634 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 2635 context->root_hpa = INVALID_PAGE;
c5a78f2b 2636 context->direct_map = true;
2d48a985 2637 context->nx = false;
6aa8b732
AK
2638 return 0;
2639}
2640
d835dfec 2641void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
6aa8b732 2642{
1165f5fe 2643 ++vcpu->stat.tlb_flush;
a8eeb04a 2644 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
6aa8b732
AK
2645}
2646
2647static void paging_new_cr3(struct kvm_vcpu *vcpu)
2648{
b8688d51 2649 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
cea0f0e7 2650 mmu_free_roots(vcpu);
6aa8b732
AK
2651}
2652
5777ed34
JR
2653static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2654{
2655 return vcpu->arch.cr3;
2656}
2657
8df25a32 2658static void inject_page_fault(struct kvm_vcpu *vcpu)
6aa8b732 2659{
8df25a32 2660 vcpu->arch.mmu.inject_page_fault(vcpu);
6aa8b732
AK
2661}
2662
6aa8b732
AK
2663static void paging_free(struct kvm_vcpu *vcpu)
2664{
2665 nonpaging_free(vcpu);
2666}
2667
3241f22d 2668static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
82725b20
DE
2669{
2670 int bit7;
2671
2672 bit7 = (gpte >> 7) & 1;
3241f22d 2673 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
82725b20
DE
2674}
2675
6aa8b732
AK
2676#define PTTYPE 64
2677#include "paging_tmpl.h"
2678#undef PTTYPE
2679
2680#define PTTYPE 32
2681#include "paging_tmpl.h"
2682#undef PTTYPE
2683
52fde8df
JR
2684static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2685 struct kvm_mmu *context,
2686 int level)
82725b20 2687{
82725b20
DE
2688 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2689 u64 exb_bit_rsvd = 0;
2690
2d48a985 2691 if (!context->nx)
82725b20
DE
2692 exb_bit_rsvd = rsvd_bits(63, 63);
2693 switch (level) {
2694 case PT32_ROOT_LEVEL:
2695 /* no rsvd bits for 2 level 4K page table entries */
2696 context->rsvd_bits_mask[0][1] = 0;
2697 context->rsvd_bits_mask[0][0] = 0;
f815bce8
XG
2698 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2699
2700 if (!is_pse(vcpu)) {
2701 context->rsvd_bits_mask[1][1] = 0;
2702 break;
2703 }
2704
82725b20
DE
2705 if (is_cpuid_PSE36())
2706 /* 36bits PSE 4MB page */
2707 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2708 else
2709 /* 32 bits PSE 4MB page */
2710 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
82725b20
DE
2711 break;
2712 case PT32E_ROOT_LEVEL:
20c466b5
DE
2713 context->rsvd_bits_mask[0][2] =
2714 rsvd_bits(maxphyaddr, 63) |
2715 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
82725b20 2716 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4c26b4cd 2717 rsvd_bits(maxphyaddr, 62); /* PDE */
82725b20
DE
2718 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2719 rsvd_bits(maxphyaddr, 62); /* PTE */
2720 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2721 rsvd_bits(maxphyaddr, 62) |
2722 rsvd_bits(13, 20); /* large page */
f815bce8 2723 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
82725b20
DE
2724 break;
2725 case PT64_ROOT_LEVEL:
2726 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2727 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2728 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2729 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2730 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4c26b4cd 2731 rsvd_bits(maxphyaddr, 51);
82725b20
DE
2732 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2733 rsvd_bits(maxphyaddr, 51);
2734 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
e04da980
JR
2735 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2736 rsvd_bits(maxphyaddr, 51) |
2737 rsvd_bits(13, 29);
82725b20 2738 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
4c26b4cd
SY
2739 rsvd_bits(maxphyaddr, 51) |
2740 rsvd_bits(13, 20); /* large page */
f815bce8 2741 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
82725b20
DE
2742 break;
2743 }
2744}
2745
52fde8df
JR
2746static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2747 struct kvm_mmu *context,
2748 int level)
6aa8b732 2749{
2d48a985
JR
2750 context->nx = is_nx(vcpu);
2751
52fde8df 2752 reset_rsvds_bits_mask(vcpu, context, level);
6aa8b732
AK
2753
2754 ASSERT(is_pae(vcpu));
2755 context->new_cr3 = paging_new_cr3;
2756 context->page_fault = paging64_page_fault;
6aa8b732 2757 context->gva_to_gpa = paging64_gva_to_gpa;
c7addb90 2758 context->prefetch_page = paging64_prefetch_page;
e8bc217a 2759 context->sync_page = paging64_sync_page;
a7052897 2760 context->invlpg = paging64_invlpg;
6aa8b732 2761 context->free = paging_free;
17ac10ad
AK
2762 context->root_level = level;
2763 context->shadow_root_level = level;
17c3ba9d 2764 context->root_hpa = INVALID_PAGE;
c5a78f2b 2765 context->direct_map = false;
6aa8b732
AK
2766 return 0;
2767}
2768
52fde8df
JR
2769static int paging64_init_context(struct kvm_vcpu *vcpu,
2770 struct kvm_mmu *context)
17ac10ad 2771{
52fde8df 2772 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
17ac10ad
AK
2773}
2774
52fde8df
JR
2775static int paging32_init_context(struct kvm_vcpu *vcpu,
2776 struct kvm_mmu *context)
6aa8b732 2777{
2d48a985
JR
2778 context->nx = false;
2779
52fde8df 2780 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
6aa8b732
AK
2781
2782 context->new_cr3 = paging_new_cr3;
2783 context->page_fault = paging32_page_fault;
6aa8b732
AK
2784 context->gva_to_gpa = paging32_gva_to_gpa;
2785 context->free = paging_free;
c7addb90 2786 context->prefetch_page = paging32_prefetch_page;
e8bc217a 2787 context->sync_page = paging32_sync_page;
a7052897 2788 context->invlpg = paging32_invlpg;
6aa8b732
AK
2789 context->root_level = PT32_ROOT_LEVEL;
2790 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 2791 context->root_hpa = INVALID_PAGE;
c5a78f2b 2792 context->direct_map = false;
6aa8b732
AK
2793 return 0;
2794}
2795
52fde8df
JR
2796static int paging32E_init_context(struct kvm_vcpu *vcpu,
2797 struct kvm_mmu *context)
6aa8b732 2798{
52fde8df 2799 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
6aa8b732
AK
2800}
2801
fb72d167
JR
2802static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2803{
14dfe855 2804 struct kvm_mmu *context = vcpu->arch.walk_mmu;
fb72d167
JR
2805
2806 context->new_cr3 = nonpaging_new_cr3;
2807 context->page_fault = tdp_page_fault;
2808 context->free = nonpaging_free;
2809 context->prefetch_page = nonpaging_prefetch_page;
e8bc217a 2810 context->sync_page = nonpaging_sync_page;
a7052897 2811 context->invlpg = nonpaging_invlpg;
67253af5 2812 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
fb72d167 2813 context->root_hpa = INVALID_PAGE;
c5a78f2b 2814 context->direct_map = true;
1c97f0a0 2815 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
5777ed34 2816 context->get_cr3 = get_cr3;
cb659db8 2817 context->inject_page_fault = kvm_inject_page_fault;
2d48a985 2818 context->nx = is_nx(vcpu);
fb72d167
JR
2819
2820 if (!is_paging(vcpu)) {
2d48a985 2821 context->nx = false;
fb72d167
JR
2822 context->gva_to_gpa = nonpaging_gva_to_gpa;
2823 context->root_level = 0;
2824 } else if (is_long_mode(vcpu)) {
2d48a985 2825 context->nx = is_nx(vcpu);
52fde8df 2826 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
fb72d167
JR
2827 context->gva_to_gpa = paging64_gva_to_gpa;
2828 context->root_level = PT64_ROOT_LEVEL;
2829 } else if (is_pae(vcpu)) {
2d48a985 2830 context->nx = is_nx(vcpu);
52fde8df 2831 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
fb72d167
JR
2832 context->gva_to_gpa = paging64_gva_to_gpa;
2833 context->root_level = PT32E_ROOT_LEVEL;
2834 } else {
2d48a985 2835 context->nx = false;
52fde8df 2836 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
fb72d167
JR
2837 context->gva_to_gpa = paging32_gva_to_gpa;
2838 context->root_level = PT32_ROOT_LEVEL;
2839 }
2840
2841 return 0;
2842}
2843
52fde8df 2844int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
6aa8b732 2845{
a770f6f2 2846 int r;
6aa8b732 2847 ASSERT(vcpu);
ad312c7c 2848 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732
AK
2849
2850 if (!is_paging(vcpu))
52fde8df 2851 r = nonpaging_init_context(vcpu, context);
a9058ecd 2852 else if (is_long_mode(vcpu))
52fde8df 2853 r = paging64_init_context(vcpu, context);
6aa8b732 2854 else if (is_pae(vcpu))
52fde8df 2855 r = paging32E_init_context(vcpu, context);
6aa8b732 2856 else
52fde8df 2857 r = paging32_init_context(vcpu, context);
a770f6f2 2858
5b7e0102 2859 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
f43addd4 2860 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
52fde8df
JR
2861
2862 return r;
2863}
2864EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2865
2866static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2867{
14dfe855 2868 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
52fde8df 2869
14dfe855
JR
2870 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2871 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2872 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
a770f6f2
AK
2873
2874 return r;
6aa8b732
AK
2875}
2876
02f59dc9
JR
2877static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2878{
2879 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2880
2881 g_context->get_cr3 = get_cr3;
2882 g_context->inject_page_fault = kvm_inject_page_fault;
2883
2884 /*
2885 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
2886 * translation of l2_gpa to l1_gpa addresses is done using the
2887 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
2888 * functions between mmu and nested_mmu are swapped.
2889 */
2890 if (!is_paging(vcpu)) {
2d48a985 2891 g_context->nx = false;
02f59dc9
JR
2892 g_context->root_level = 0;
2893 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
2894 } else if (is_long_mode(vcpu)) {
2d48a985 2895 g_context->nx = is_nx(vcpu);
02f59dc9
JR
2896 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
2897 g_context->root_level = PT64_ROOT_LEVEL;
2898 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2899 } else if (is_pae(vcpu)) {
2d48a985 2900 g_context->nx = is_nx(vcpu);
02f59dc9
JR
2901 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
2902 g_context->root_level = PT32E_ROOT_LEVEL;
2903 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2904 } else {
2d48a985 2905 g_context->nx = false;
02f59dc9
JR
2906 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
2907 g_context->root_level = PT32_ROOT_LEVEL;
2908 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
2909 }
2910
2911 return 0;
2912}
2913
fb72d167
JR
2914static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2915{
35149e21
AL
2916 vcpu->arch.update_pte.pfn = bad_pfn;
2917
02f59dc9
JR
2918 if (mmu_is_nested(vcpu))
2919 return init_kvm_nested_mmu(vcpu);
2920 else if (tdp_enabled)
fb72d167
JR
2921 return init_kvm_tdp_mmu(vcpu);
2922 else
2923 return init_kvm_softmmu(vcpu);
2924}
2925
6aa8b732
AK
2926static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2927{
2928 ASSERT(vcpu);
62ad0755
SY
2929 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2930 /* mmu.free() should set root_hpa = INVALID_PAGE */
ad312c7c 2931 vcpu->arch.mmu.free(vcpu);
6aa8b732
AK
2932}
2933
2934int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
17c3ba9d
AK
2935{
2936 destroy_kvm_mmu(vcpu);
2937 return init_kvm_mmu(vcpu);
2938}
8668a3c4 2939EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
17c3ba9d
AK
2940
2941int kvm_mmu_load(struct kvm_vcpu *vcpu)
6aa8b732 2942{
714b93da
AK
2943 int r;
2944
e2dec939 2945 r = mmu_topup_memory_caches(vcpu);
17c3ba9d
AK
2946 if (r)
2947 goto out;
8986ecc0 2948 r = mmu_alloc_roots(vcpu);
8facbbff 2949 spin_lock(&vcpu->kvm->mmu_lock);
0ba73cda 2950 mmu_sync_roots(vcpu);
aaee2c94 2951 spin_unlock(&vcpu->kvm->mmu_lock);
8986ecc0
MT
2952 if (r)
2953 goto out;
3662cb1c 2954 /* set_cr3() should ensure TLB has been flushed */
f43addd4 2955 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
714b93da
AK
2956out:
2957 return r;
6aa8b732 2958}
17c3ba9d
AK
2959EXPORT_SYMBOL_GPL(kvm_mmu_load);
2960
2961void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2962{
2963 mmu_free_roots(vcpu);
2964}
6aa8b732 2965
09072daf 2966static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
4db35314 2967 struct kvm_mmu_page *sp,
ac1b714e
AK
2968 u64 *spte)
2969{
2970 u64 pte;
2971 struct kvm_mmu_page *child;
2972
2973 pte = *spte;
c7addb90 2974 if (is_shadow_present_pte(pte)) {
776e6633 2975 if (is_last_spte(pte, sp->role.level))
be38d276 2976 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
ac1b714e
AK
2977 else {
2978 child = page_header(pte & PT64_BASE_ADDR_MASK);
90cb0529 2979 mmu_page_remove_parent_pte(child, spte);
ac1b714e
AK
2980 }
2981 }
d555c333 2982 __set_spte(spte, shadow_trap_nonpresent_pte);
05da4558
MT
2983 if (is_large_pte(pte))
2984 --vcpu->kvm->stat.lpages;
ac1b714e
AK
2985}
2986
0028425f 2987static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
4db35314 2988 struct kvm_mmu_page *sp,
0028425f 2989 u64 *spte,
489f1d65 2990 const void *new)
0028425f 2991{
30945387 2992 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
7e4e4056
JR
2993 ++vcpu->kvm->stat.mmu_pde_zapped;
2994 return;
30945387 2995 }
0028425f 2996
3241f22d 2997 if (is_rsvd_bits_set(&vcpu->arch.mmu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
fa1de2bf
XG
2998 return;
2999
4cee5764 3000 ++vcpu->kvm->stat.mmu_pte_updated;
5b7e0102 3001 if (!sp->role.cr4_pae)
489f1d65 3002 paging32_update_pte(vcpu, sp, spte, new);
0028425f 3003 else
489f1d65 3004 paging64_update_pte(vcpu, sp, spte, new);
0028425f
AK
3005}
3006
79539cec
AK
3007static bool need_remote_flush(u64 old, u64 new)
3008{
3009 if (!is_shadow_present_pte(old))
3010 return false;
3011 if (!is_shadow_present_pte(new))
3012 return true;
3013 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3014 return true;
3015 old ^= PT64_NX_MASK;
3016 new ^= PT64_NX_MASK;
3017 return (old & ~new & PT64_PERM_MASK) != 0;
3018}
3019
0671a8e7
XG
3020static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3021 bool remote_flush, bool local_flush)
79539cec 3022{
0671a8e7
XG
3023 if (zap_page)
3024 return;
3025
3026 if (remote_flush)
79539cec 3027 kvm_flush_remote_tlbs(vcpu->kvm);
0671a8e7 3028 else if (local_flush)
79539cec
AK
3029 kvm_mmu_flush_tlb(vcpu);
3030}
3031
12b7d28f
AK
3032static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3033{
ad312c7c 3034 u64 *spte = vcpu->arch.last_pte_updated;
12b7d28f 3035
7b52345e 3036 return !!(spte && (*spte & shadow_accessed_mask));
12b7d28f
AK
3037}
3038
d7824fff 3039static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
72016f3a 3040 u64 gpte)
d7824fff
AK
3041{
3042 gfn_t gfn;
35149e21 3043 pfn_t pfn;
d7824fff 3044
43a3795a 3045 if (!is_present_gpte(gpte))
d7824fff
AK
3046 return;
3047 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
72dc67a6 3048
e930bffe 3049 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 3050 smp_rmb();
35149e21 3051 pfn = gfn_to_pfn(vcpu->kvm, gfn);
72dc67a6 3052
35149e21
AL
3053 if (is_error_pfn(pfn)) {
3054 kvm_release_pfn_clean(pfn);
d196e343
AK
3055 return;
3056 }
d7824fff 3057 vcpu->arch.update_pte.gfn = gfn;
35149e21 3058 vcpu->arch.update_pte.pfn = pfn;
d7824fff
AK
3059}
3060
1b7fcd32
AK
3061static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3062{
3063 u64 *spte = vcpu->arch.last_pte_updated;
3064
3065 if (spte
3066 && vcpu->arch.last_pte_gfn == gfn
3067 && shadow_accessed_mask
3068 && !(*spte & shadow_accessed_mask)
3069 && is_shadow_present_pte(*spte))
3070 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3071}
3072
09072daf 3073void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
ad218f85
MT
3074 const u8 *new, int bytes,
3075 bool guest_initiated)
da4a00f0 3076{
9b7a0325 3077 gfn_t gfn = gpa >> PAGE_SHIFT;
fa1de2bf 3078 union kvm_mmu_page_role mask = { .word = 0 };
4db35314 3079 struct kvm_mmu_page *sp;
f41d335a 3080 struct hlist_node *node;
d98ba053 3081 LIST_HEAD(invalid_list);
489f1d65 3082 u64 entry, gentry;
9b7a0325 3083 u64 *spte;
9b7a0325 3084 unsigned offset = offset_in_page(gpa);
0e7bc4b9 3085 unsigned pte_size;
9b7a0325 3086 unsigned page_offset;
0e7bc4b9 3087 unsigned misaligned;
fce0657f 3088 unsigned quadrant;
9b7a0325 3089 int level;
86a5ba02 3090 int flooded = 0;
ac1b714e 3091 int npte;
489f1d65 3092 int r;
08e850c6 3093 int invlpg_counter;
0671a8e7
XG
3094 bool remote_flush, local_flush, zap_page;
3095
3096 zap_page = remote_flush = local_flush = false;
9b7a0325 3097
b8688d51 3098 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
72016f3a 3099
08e850c6 3100 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
72016f3a
AK
3101
3102 /*
3103 * Assume that the pte write on a page table of the same type
3104 * as the current vcpu paging mode. This is nearly always true
3105 * (might be false while changing modes). Note it is verified later
3106 * by update_pte().
3107 */
08e850c6 3108 if ((is_pae(vcpu) && bytes == 4) || !new) {
72016f3a 3109 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
08e850c6
AK
3110 if (is_pae(vcpu)) {
3111 gpa &= ~(gpa_t)7;
3112 bytes = 8;
3113 }
3114 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
72016f3a
AK
3115 if (r)
3116 gentry = 0;
08e850c6
AK
3117 new = (const u8 *)&gentry;
3118 }
3119
3120 switch (bytes) {
3121 case 4:
3122 gentry = *(const u32 *)new;
3123 break;
3124 case 8:
3125 gentry = *(const u64 *)new;
3126 break;
3127 default:
3128 gentry = 0;
3129 break;
72016f3a
AK
3130 }
3131
3132 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
aaee2c94 3133 spin_lock(&vcpu->kvm->mmu_lock);
08e850c6
AK
3134 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3135 gentry = 0;
1b7fcd32 3136 kvm_mmu_access_page(vcpu, gfn);
eb787d10 3137 kvm_mmu_free_some_pages(vcpu);
4cee5764 3138 ++vcpu->kvm->stat.mmu_pte_write;
8b1fe17c 3139 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
ad218f85
MT
3140 if (guest_initiated) {
3141 if (gfn == vcpu->arch.last_pt_write_gfn
3142 && !last_updated_pte_accessed(vcpu)) {
3143 ++vcpu->arch.last_pt_write_count;
3144 if (vcpu->arch.last_pt_write_count >= 3)
3145 flooded = 1;
3146 } else {
3147 vcpu->arch.last_pt_write_gfn = gfn;
3148 vcpu->arch.last_pt_write_count = 1;
3149 vcpu->arch.last_pte_updated = NULL;
3150 }
86a5ba02 3151 }
3246af0e 3152
fa1de2bf 3153 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
f41d335a 3154 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
5b7e0102 3155 pte_size = sp->role.cr4_pae ? 8 : 4;
0e7bc4b9 3156 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
e925c5ba 3157 misaligned |= bytes < 4;
86a5ba02 3158 if (misaligned || flooded) {
0e7bc4b9
AK
3159 /*
3160 * Misaligned accesses are too much trouble to fix
3161 * up; also, they usually indicate a page is not used
3162 * as a page table.
86a5ba02
AK
3163 *
3164 * If we're seeing too many writes to a page,
3165 * it may no longer be a page table, or we may be
3166 * forking, in which case it is better to unmap the
3167 * page.
0e7bc4b9
AK
3168 */
3169 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
4db35314 3170 gpa, bytes, sp->role.word);
0671a8e7 3171 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
f41d335a 3172 &invalid_list);
4cee5764 3173 ++vcpu->kvm->stat.mmu_flooded;
0e7bc4b9
AK
3174 continue;
3175 }
9b7a0325 3176 page_offset = offset;
4db35314 3177 level = sp->role.level;
ac1b714e 3178 npte = 1;
5b7e0102 3179 if (!sp->role.cr4_pae) {
ac1b714e
AK
3180 page_offset <<= 1; /* 32->64 */
3181 /*
3182 * A 32-bit pde maps 4MB while the shadow pdes map
3183 * only 2MB. So we need to double the offset again
3184 * and zap two pdes instead of one.
3185 */
3186 if (level == PT32_ROOT_LEVEL) {
6b8d0f9b 3187 page_offset &= ~7; /* kill rounding error */
ac1b714e
AK
3188 page_offset <<= 1;
3189 npte = 2;
3190 }
fce0657f 3191 quadrant = page_offset >> PAGE_SHIFT;
9b7a0325 3192 page_offset &= ~PAGE_MASK;
4db35314 3193 if (quadrant != sp->role.quadrant)
fce0657f 3194 continue;
9b7a0325 3195 }
0671a8e7 3196 local_flush = true;
4db35314 3197 spte = &sp->spt[page_offset / sizeof(*spte)];
ac1b714e 3198 while (npte--) {
79539cec 3199 entry = *spte;
4db35314 3200 mmu_pte_write_zap_pte(vcpu, sp, spte);
fa1de2bf
XG
3201 if (gentry &&
3202 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3203 & mask.word))
72016f3a 3204 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
0671a8e7
XG
3205 if (!remote_flush && need_remote_flush(entry, *spte))
3206 remote_flush = true;
ac1b714e 3207 ++spte;
9b7a0325 3208 }
9b7a0325 3209 }
0671a8e7 3210 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
d98ba053 3211 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
8b1fe17c 3212 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
aaee2c94 3213 spin_unlock(&vcpu->kvm->mmu_lock);
35149e21
AL
3214 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3215 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3216 vcpu->arch.update_pte.pfn = bad_pfn;
d7824fff 3217 }
da4a00f0
AK
3218}
3219
a436036b
AK
3220int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3221{
10589a46
MT
3222 gpa_t gpa;
3223 int r;
a436036b 3224
c5a78f2b 3225 if (vcpu->arch.mmu.direct_map)
60f24784
AK
3226 return 0;
3227
1871c602 3228 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
10589a46 3229
aaee2c94 3230 spin_lock(&vcpu->kvm->mmu_lock);
10589a46 3231 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
aaee2c94 3232 spin_unlock(&vcpu->kvm->mmu_lock);
10589a46 3233 return r;
a436036b 3234}
577bdc49 3235EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
a436036b 3236
22d95b12 3237void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
ebeace86 3238{
d98ba053 3239 LIST_HEAD(invalid_list);
103ad25a 3240
e0df7b9f 3241 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
3b80fffe 3242 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
4db35314 3243 struct kvm_mmu_page *sp;
ebeace86 3244
f05e70ac 3245 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
4db35314 3246 struct kvm_mmu_page, link);
e0df7b9f 3247 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
80b63faf 3248 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
4cee5764 3249 ++vcpu->kvm->stat.mmu_recycled;
ebeace86
AK
3250 }
3251}
ebeace86 3252
3067714c
AK
3253int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3254{
3255 int r;
3256 enum emulation_result er;
3257
ad312c7c 3258 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
3067714c
AK
3259 if (r < 0)
3260 goto out;
3261
3262 if (!r) {
3263 r = 1;
3264 goto out;
3265 }
3266
b733bfb5
AK
3267 r = mmu_topup_memory_caches(vcpu);
3268 if (r)
3269 goto out;
3270
851ba692 3271 er = emulate_instruction(vcpu, cr2, error_code, 0);
3067714c
AK
3272
3273 switch (er) {
3274 case EMULATE_DONE:
3275 return 1;
3276 case EMULATE_DO_MMIO:
3277 ++vcpu->stat.mmio_exits;
6d77dbfc 3278 /* fall through */
3067714c 3279 case EMULATE_FAIL:
3f5d18a9 3280 return 0;
3067714c
AK
3281 default:
3282 BUG();
3283 }
3284out:
3067714c
AK
3285 return r;
3286}
3287EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3288
a7052897
MT
3289void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3290{
a7052897 3291 vcpu->arch.mmu.invlpg(vcpu, gva);
a7052897
MT
3292 kvm_mmu_flush_tlb(vcpu);
3293 ++vcpu->stat.invlpg;
3294}
3295EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3296
18552672
JR
3297void kvm_enable_tdp(void)
3298{
3299 tdp_enabled = true;
3300}
3301EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3302
5f4cb662
JR
3303void kvm_disable_tdp(void)
3304{
3305 tdp_enabled = false;
3306}
3307EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3308
6aa8b732
AK
3309static void free_mmu_pages(struct kvm_vcpu *vcpu)
3310{
ad312c7c 3311 free_page((unsigned long)vcpu->arch.mmu.pae_root);
81407ca5
JR
3312 if (vcpu->arch.mmu.lm_root != NULL)
3313 free_page((unsigned long)vcpu->arch.mmu.lm_root);
6aa8b732
AK
3314}
3315
3316static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3317{
17ac10ad 3318 struct page *page;
6aa8b732
AK
3319 int i;
3320
3321 ASSERT(vcpu);
3322
17ac10ad
AK
3323 /*
3324 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3325 * Therefore we need to allocate shadow page tables in the first
3326 * 4GB of memory, which happens to fit the DMA32 zone.
3327 */
3328 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3329 if (!page)
d7fa6ab2
WY
3330 return -ENOMEM;
3331
ad312c7c 3332 vcpu->arch.mmu.pae_root = page_address(page);
17ac10ad 3333 for (i = 0; i < 4; ++i)
ad312c7c 3334 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 3335
6aa8b732 3336 return 0;
6aa8b732
AK
3337}
3338
8018c27b 3339int kvm_mmu_create(struct kvm_vcpu *vcpu)
6aa8b732 3340{
6aa8b732 3341 ASSERT(vcpu);
ad312c7c 3342 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 3343
8018c27b
IM
3344 return alloc_mmu_pages(vcpu);
3345}
6aa8b732 3346
8018c27b
IM
3347int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3348{
3349 ASSERT(vcpu);
ad312c7c 3350 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2c264957 3351
8018c27b 3352 return init_kvm_mmu(vcpu);
6aa8b732
AK
3353}
3354
3355void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3356{
3357 ASSERT(vcpu);
3358
3359 destroy_kvm_mmu(vcpu);
3360 free_mmu_pages(vcpu);
714b93da 3361 mmu_free_memory_caches(vcpu);
6aa8b732
AK
3362}
3363
90cb0529 3364void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
6aa8b732 3365{
4db35314 3366 struct kvm_mmu_page *sp;
6aa8b732 3367
f05e70ac 3368 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
6aa8b732
AK
3369 int i;
3370 u64 *pt;
3371
291f26bc 3372 if (!test_bit(slot, sp->slot_bitmap))
6aa8b732
AK
3373 continue;
3374
4db35314 3375 pt = sp->spt;
6aa8b732
AK
3376 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3377 /* avoid RMW */
01c168ac 3378 if (is_writable_pte(pt[i]))
6aa8b732 3379 pt[i] &= ~PT_WRITABLE_MASK;
6aa8b732 3380 }
171d595d 3381 kvm_flush_remote_tlbs(kvm);
6aa8b732 3382}
37a7d8b0 3383
90cb0529 3384void kvm_mmu_zap_all(struct kvm *kvm)
e0fa826f 3385{
4db35314 3386 struct kvm_mmu_page *sp, *node;
d98ba053 3387 LIST_HEAD(invalid_list);
e0fa826f 3388
aaee2c94 3389 spin_lock(&kvm->mmu_lock);
3246af0e 3390restart:
f05e70ac 3391 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
d98ba053 3392 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
3246af0e
XG
3393 goto restart;
3394
d98ba053 3395 kvm_mmu_commit_zap_page(kvm, &invalid_list);
aaee2c94 3396 spin_unlock(&kvm->mmu_lock);
e0fa826f
DL
3397}
3398
d98ba053
XG
3399static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3400 struct list_head *invalid_list)
3ee16c81
IE
3401{
3402 struct kvm_mmu_page *page;
3403
3404 page = container_of(kvm->arch.active_mmu_pages.prev,
3405 struct kvm_mmu_page, link);
d98ba053 3406 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
3ee16c81
IE
3407}
3408
7f8275d0 3409static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
3ee16c81
IE
3410{
3411 struct kvm *kvm;
3412 struct kvm *kvm_freed = NULL;
45221ab6
DH
3413
3414 if (nr_to_scan == 0)
3415 goto out;
3ee16c81
IE
3416
3417 spin_lock(&kvm_lock);
3418
3419 list_for_each_entry(kvm, &vm_list, vm_list) {
45221ab6 3420 int idx, freed_pages;
d98ba053 3421 LIST_HEAD(invalid_list);
3ee16c81 3422
f656ce01 3423 idx = srcu_read_lock(&kvm->srcu);
3ee16c81 3424 spin_lock(&kvm->mmu_lock);
45221ab6
DH
3425 if (!kvm_freed && nr_to_scan > 0 &&
3426 kvm->arch.n_used_mmu_pages > 0) {
d98ba053
XG
3427 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3428 &invalid_list);
3ee16c81
IE
3429 kvm_freed = kvm;
3430 }
3431 nr_to_scan--;
3432
d98ba053 3433 kvm_mmu_commit_zap_page(kvm, &invalid_list);
3ee16c81 3434 spin_unlock(&kvm->mmu_lock);
f656ce01 3435 srcu_read_unlock(&kvm->srcu, idx);
3ee16c81
IE
3436 }
3437 if (kvm_freed)
3438 list_move_tail(&kvm_freed->vm_list, &vm_list);
3439
3440 spin_unlock(&kvm_lock);
3441
45221ab6
DH
3442out:
3443 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
3ee16c81
IE
3444}
3445
3446static struct shrinker mmu_shrinker = {
3447 .shrink = mmu_shrink,
3448 .seeks = DEFAULT_SEEKS * 10,
3449};
3450
2ddfd20e 3451static void mmu_destroy_caches(void)
b5a33a75
AK
3452{
3453 if (pte_chain_cache)
3454 kmem_cache_destroy(pte_chain_cache);
3455 if (rmap_desc_cache)
3456 kmem_cache_destroy(rmap_desc_cache);
d3d25b04
AK
3457 if (mmu_page_header_cache)
3458 kmem_cache_destroy(mmu_page_header_cache);
b5a33a75
AK
3459}
3460
3ee16c81
IE
3461void kvm_mmu_module_exit(void)
3462{
3463 mmu_destroy_caches();
45bf21a8 3464 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3ee16c81
IE
3465 unregister_shrinker(&mmu_shrinker);
3466}
3467
b5a33a75
AK
3468int kvm_mmu_module_init(void)
3469{
3470 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3471 sizeof(struct kvm_pte_chain),
20c2df83 3472 0, 0, NULL);
b5a33a75
AK
3473 if (!pte_chain_cache)
3474 goto nomem;
3475 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3476 sizeof(struct kvm_rmap_desc),
20c2df83 3477 0, 0, NULL);
b5a33a75
AK
3478 if (!rmap_desc_cache)
3479 goto nomem;
3480
d3d25b04
AK
3481 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3482 sizeof(struct kvm_mmu_page),
20c2df83 3483 0, 0, NULL);
d3d25b04
AK
3484 if (!mmu_page_header_cache)
3485 goto nomem;
3486
45bf21a8
WY
3487 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3488 goto nomem;
3489
3ee16c81
IE
3490 register_shrinker(&mmu_shrinker);
3491
b5a33a75
AK
3492 return 0;
3493
3494nomem:
3ee16c81 3495 mmu_destroy_caches();
b5a33a75
AK
3496 return -ENOMEM;
3497}
3498
3ad82a7e
ZX
3499/*
3500 * Caculate mmu pages needed for kvm.
3501 */
3502unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3503{
3504 int i;
3505 unsigned int nr_mmu_pages;
3506 unsigned int nr_pages = 0;
bc6678a3 3507 struct kvm_memslots *slots;
3ad82a7e 3508
90d83dc3
LJ
3509 slots = kvm_memslots(kvm);
3510
bc6678a3
MT
3511 for (i = 0; i < slots->nmemslots; i++)
3512 nr_pages += slots->memslots[i].npages;
3ad82a7e
ZX
3513
3514 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3515 nr_mmu_pages = max(nr_mmu_pages,
3516 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3517
3518 return nr_mmu_pages;
3519}
3520
2f333bcb
MT
3521static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3522 unsigned len)
3523{
3524 if (len > buffer->len)
3525 return NULL;
3526 return buffer->ptr;
3527}
3528
3529static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3530 unsigned len)
3531{
3532 void *ret;
3533
3534 ret = pv_mmu_peek_buffer(buffer, len);
3535 if (!ret)
3536 return ret;
3537 buffer->ptr += len;
3538 buffer->len -= len;
3539 buffer->processed += len;
3540 return ret;
3541}
3542
3543static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3544 gpa_t addr, gpa_t value)
3545{
3546 int bytes = 8;
3547 int r;
3548
3549 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3550 bytes = 4;
3551
3552 r = mmu_topup_memory_caches(vcpu);
3553 if (r)
3554 return r;
3555
3200f405 3556 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2f333bcb
MT
3557 return -EFAULT;
3558
3559 return 1;
3560}
3561
3562static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3563{
2390218b 3564 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
2f333bcb
MT
3565 return 1;
3566}
3567
3568static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3569{
3570 spin_lock(&vcpu->kvm->mmu_lock);
3571 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3572 spin_unlock(&vcpu->kvm->mmu_lock);
3573 return 1;
3574}
3575
3576static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3577 struct kvm_pv_mmu_op_buffer *buffer)
3578{
3579 struct kvm_mmu_op_header *header;
3580
3581 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3582 if (!header)
3583 return 0;
3584 switch (header->op) {
3585 case KVM_MMU_OP_WRITE_PTE: {
3586 struct kvm_mmu_op_write_pte *wpte;
3587
3588 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3589 if (!wpte)
3590 return 0;
3591 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3592 wpte->pte_val);
3593 }
3594 case KVM_MMU_OP_FLUSH_TLB: {
3595 struct kvm_mmu_op_flush_tlb *ftlb;
3596
3597 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3598 if (!ftlb)
3599 return 0;
3600 return kvm_pv_mmu_flush_tlb(vcpu);
3601 }
3602 case KVM_MMU_OP_RELEASE_PT: {
3603 struct kvm_mmu_op_release_pt *rpt;
3604
3605 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3606 if (!rpt)
3607 return 0;
3608 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3609 }
3610 default: return 0;
3611 }
3612}
3613
3614int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3615 gpa_t addr, unsigned long *ret)
3616{
3617 int r;
6ad18fba 3618 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2f333bcb 3619
6ad18fba
DH
3620 buffer->ptr = buffer->buf;
3621 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3622 buffer->processed = 0;
2f333bcb 3623
6ad18fba 3624 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2f333bcb
MT
3625 if (r)
3626 goto out;
3627
6ad18fba
DH
3628 while (buffer->len) {
3629 r = kvm_pv_mmu_op_one(vcpu, buffer);
2f333bcb
MT
3630 if (r < 0)
3631 goto out;
3632 if (r == 0)
3633 break;
3634 }
3635
3636 r = 1;
3637out:
6ad18fba 3638 *ret = buffer->processed;
2f333bcb
MT
3639 return r;
3640}
3641
94d8b056
MT
3642int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3643{
3644 struct kvm_shadow_walk_iterator iterator;
3645 int nr_sptes = 0;
3646
3647 spin_lock(&vcpu->kvm->mmu_lock);
3648 for_each_shadow_entry(vcpu, addr, iterator) {
3649 sptes[iterator.level-1] = *iterator.sptep;
3650 nr_sptes++;
3651 if (!is_shadow_present_pte(*iterator.sptep))
3652 break;
3653 }
3654 spin_unlock(&vcpu->kvm->mmu_lock);
3655
3656 return nr_sptes;
3657}
3658EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3659
8b1fe17c 3660#ifdef CONFIG_KVM_MMU_AUDIT
2f4f3372 3661#include "mmu_audit.c"
37a7d8b0 3662#endif