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