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