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