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