KVM: MMU: filter out the mmio pfn from the fault pfn
[deliverable/linux.git] / arch / x86 / kvm / mmu.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
9611c187 10 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6aa8b732
AK
11 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
e495606d 20
af585b92 21#include "irq.h"
1d737c8a 22#include "mmu.h"
836a1b3c 23#include "x86.h"
6de4f3ad 24#include "kvm_cache_regs.h"
af585b92 25#include "x86.h"
e495606d 26
edf88417 27#include <linux/kvm_host.h>
6aa8b732
AK
28#include <linux/types.h>
29#include <linux/string.h>
6aa8b732
AK
30#include <linux/mm.h>
31#include <linux/highmem.h>
32#include <linux/module.h>
448353ca 33#include <linux/swap.h>
05da4558 34#include <linux/hugetlb.h>
2f333bcb 35#include <linux/compiler.h>
bc6678a3 36#include <linux/srcu.h>
5a0e3ad6 37#include <linux/slab.h>
bf998156 38#include <linux/uaccess.h>
6aa8b732 39
e495606d
AK
40#include <asm/page.h>
41#include <asm/cmpxchg.h>
4e542370 42#include <asm/io.h>
13673a90 43#include <asm/vmx.h>
6aa8b732 44
18552672
JR
45/*
46 * When setting this variable to true it enables Two-Dimensional-Paging
47 * where the hardware walks 2 page tables:
48 * 1. the guest-virtual to guest-physical
49 * 2. while doing 1. it walks guest-physical to host-physical
50 * If the hardware supports that we don't need to do shadow paging.
51 */
2f333bcb 52bool tdp_enabled = false;
18552672 53
8b1fe17c
XG
54enum {
55 AUDIT_PRE_PAGE_FAULT,
56 AUDIT_POST_PAGE_FAULT,
57 AUDIT_PRE_PTE_WRITE,
6903074c
XG
58 AUDIT_POST_PTE_WRITE,
59 AUDIT_PRE_SYNC,
60 AUDIT_POST_SYNC
8b1fe17c 61};
37a7d8b0 62
8b1fe17c
XG
63char *audit_point_name[] = {
64 "pre page fault",
65 "post page fault",
66 "pre pte write",
6903074c
XG
67 "post pte write",
68 "pre sync",
69 "post sync"
8b1fe17c 70};
37a7d8b0 71
8b1fe17c 72#undef MMU_DEBUG
37a7d8b0
AK
73
74#ifdef MMU_DEBUG
75
76#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
77#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
78
79#else
80
81#define pgprintk(x...) do { } while (0)
82#define rmap_printk(x...) do { } while (0)
83
84#endif
85
8b1fe17c 86#ifdef MMU_DEBUG
6ada8cca
AK
87static int dbg = 0;
88module_param(dbg, bool, 0644);
37a7d8b0 89#endif
6aa8b732 90
582801a9
MT
91static int oos_shadow = 1;
92module_param(oos_shadow, bool, 0644);
93
d6c69ee9
YD
94#ifndef MMU_DEBUG
95#define ASSERT(x) do { } while (0)
96#else
6aa8b732
AK
97#define ASSERT(x) \
98 if (!(x)) { \
99 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
100 __FILE__, __LINE__, #x); \
101 }
d6c69ee9 102#endif
6aa8b732 103
957ed9ef
XG
104#define PTE_PREFETCH_NUM 8
105
6aa8b732
AK
106#define PT_FIRST_AVAIL_BITS_SHIFT 9
107#define PT64_SECOND_AVAIL_BITS_SHIFT 52
108
6aa8b732
AK
109#define PT64_LEVEL_BITS 9
110
111#define PT64_LEVEL_SHIFT(level) \
d77c26fc 112 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
6aa8b732 113
6aa8b732
AK
114#define PT64_INDEX(address, level)\
115 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
116
117
118#define PT32_LEVEL_BITS 10
119
120#define PT32_LEVEL_SHIFT(level) \
d77c26fc 121 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
6aa8b732 122
e04da980
JR
123#define PT32_LVL_OFFSET_MASK(level) \
124 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
125 * PT32_LEVEL_BITS))) - 1))
6aa8b732
AK
126
127#define PT32_INDEX(address, level)\
128 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
129
130
27aba766 131#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
6aa8b732
AK
132#define PT64_DIR_BASE_ADDR_MASK \
133 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
e04da980
JR
134#define PT64_LVL_ADDR_MASK(level) \
135 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136 * PT64_LEVEL_BITS))) - 1))
137#define PT64_LVL_OFFSET_MASK(level) \
138 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT64_LEVEL_BITS))) - 1))
6aa8b732
AK
140
141#define PT32_BASE_ADDR_MASK PAGE_MASK
142#define PT32_DIR_BASE_ADDR_MASK \
143 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
e04da980
JR
144#define PT32_LVL_ADDR_MASK(level) \
145 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
146 * PT32_LEVEL_BITS))) - 1))
6aa8b732 147
79539cec
AK
148#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
149 | PT64_NX_MASK)
6aa8b732 150
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
bebb106a
XG
2224static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gva_t gva,
2225 unsigned access, gfn_t gfn, pfn_t pfn)
bf998156
HY
2226{
2227 kvm_release_pfn_clean(pfn);
2228 if (is_hwpoison_pfn(pfn)) {
bebb106a 2229 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
bf998156 2230 return 0;
edba23e5
GN
2231 } else if (is_fault_pfn(pfn))
2232 return -EFAULT;
2233
bebb106a 2234 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
bf998156
HY
2235 return 1;
2236}
2237
936a5fe6
AA
2238static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2239 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2240{
2241 pfn_t pfn = *pfnp;
2242 gfn_t gfn = *gfnp;
2243 int level = *levelp;
2244
2245 /*
2246 * Check if it's a transparent hugepage. If this would be an
2247 * hugetlbfs page, level wouldn't be set to
2248 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2249 * here.
2250 */
2251 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2252 level == PT_PAGE_TABLE_LEVEL &&
2253 PageTransCompound(pfn_to_page(pfn)) &&
2254 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2255 unsigned long mask;
2256 /*
2257 * mmu_notifier_retry was successful and we hold the
2258 * mmu_lock here, so the pmd can't become splitting
2259 * from under us, and in turn
2260 * __split_huge_page_refcount() can't run from under
2261 * us and we can safely transfer the refcount from
2262 * PG_tail to PG_head as we switch the pfn to tail to
2263 * head.
2264 */
2265 *levelp = level = PT_DIRECTORY_LEVEL;
2266 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2267 VM_BUG_ON((gfn & mask) != (pfn & mask));
2268 if (pfn & mask) {
2269 gfn &= ~mask;
2270 *gfnp = gfn;
2271 kvm_release_pfn_clean(pfn);
2272 pfn &= ~mask;
2273 if (!get_page_unless_zero(pfn_to_page(pfn)))
2274 BUG();
2275 *pfnp = pfn;
2276 }
2277 }
2278}
2279
78b2c54a 2280static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
060c2abe
XG
2281 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2282
2283static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
78b2c54a 2284 bool prefault)
10589a46
MT
2285{
2286 int r;
852e3c19 2287 int level;
936a5fe6 2288 int force_pt_level;
35149e21 2289 pfn_t pfn;
e930bffe 2290 unsigned long mmu_seq;
612819c3 2291 bool map_writable;
aaee2c94 2292
936a5fe6
AA
2293 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2294 if (likely(!force_pt_level)) {
2295 level = mapping_level(vcpu, gfn);
2296 /*
2297 * This path builds a PAE pagetable - so we can map
2298 * 2mb pages at maximum. Therefore check if the level
2299 * is larger than that.
2300 */
2301 if (level > PT_DIRECTORY_LEVEL)
2302 level = PT_DIRECTORY_LEVEL;
852e3c19 2303
936a5fe6
AA
2304 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2305 } else
2306 level = PT_PAGE_TABLE_LEVEL;
05da4558 2307
e930bffe 2308 mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 2309 smp_rmb();
060c2abe 2310
78b2c54a 2311 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
060c2abe 2312 return 0;
aaee2c94 2313
d196e343 2314 /* mmio */
bf998156 2315 if (is_error_pfn(pfn))
bebb106a 2316 return kvm_handle_bad_page(vcpu, v, ACC_ALL, gfn, pfn);
d196e343 2317
aaee2c94 2318 spin_lock(&vcpu->kvm->mmu_lock);
e930bffe
AA
2319 if (mmu_notifier_retry(vcpu, mmu_seq))
2320 goto out_unlock;
eb787d10 2321 kvm_mmu_free_some_pages(vcpu);
936a5fe6
AA
2322 if (likely(!force_pt_level))
2323 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
2ec4739d
XG
2324 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2325 prefault);
aaee2c94
MT
2326 spin_unlock(&vcpu->kvm->mmu_lock);
2327
aaee2c94 2328
10589a46 2329 return r;
e930bffe
AA
2330
2331out_unlock:
2332 spin_unlock(&vcpu->kvm->mmu_lock);
2333 kvm_release_pfn_clean(pfn);
2334 return 0;
10589a46
MT
2335}
2336
2337
17ac10ad
AK
2338static void mmu_free_roots(struct kvm_vcpu *vcpu)
2339{
2340 int i;
4db35314 2341 struct kvm_mmu_page *sp;
d98ba053 2342 LIST_HEAD(invalid_list);
17ac10ad 2343
ad312c7c 2344 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
7b53aa56 2345 return;
aaee2c94 2346 spin_lock(&vcpu->kvm->mmu_lock);
81407ca5
JR
2347 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2348 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2349 vcpu->arch.mmu.direct_map)) {
ad312c7c 2350 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad 2351
4db35314
AK
2352 sp = page_header(root);
2353 --sp->root_count;
d98ba053
XG
2354 if (!sp->root_count && sp->role.invalid) {
2355 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2356 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2357 }
ad312c7c 2358 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
aaee2c94 2359 spin_unlock(&vcpu->kvm->mmu_lock);
17ac10ad
AK
2360 return;
2361 }
17ac10ad 2362 for (i = 0; i < 4; ++i) {
ad312c7c 2363 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad 2364
417726a3 2365 if (root) {
417726a3 2366 root &= PT64_BASE_ADDR_MASK;
4db35314
AK
2367 sp = page_header(root);
2368 --sp->root_count;
2e53d63a 2369 if (!sp->root_count && sp->role.invalid)
d98ba053
XG
2370 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2371 &invalid_list);
417726a3 2372 }
ad312c7c 2373 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 2374 }
d98ba053 2375 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
aaee2c94 2376 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 2377 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
17ac10ad
AK
2378}
2379
8986ecc0
MT
2380static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2381{
2382 int ret = 0;
2383
2384 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
a8eeb04a 2385 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
8986ecc0
MT
2386 ret = 1;
2387 }
2388
2389 return ret;
2390}
2391
651dd37a
JR
2392static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2393{
2394 struct kvm_mmu_page *sp;
7ebaf15e 2395 unsigned i;
651dd37a
JR
2396
2397 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2398 spin_lock(&vcpu->kvm->mmu_lock);
2399 kvm_mmu_free_some_pages(vcpu);
2400 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2401 1, ACC_ALL, NULL);
2402 ++sp->root_count;
2403 spin_unlock(&vcpu->kvm->mmu_lock);
2404 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2405 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2406 for (i = 0; i < 4; ++i) {
2407 hpa_t root = vcpu->arch.mmu.pae_root[i];
2408
2409 ASSERT(!VALID_PAGE(root));
2410 spin_lock(&vcpu->kvm->mmu_lock);
2411 kvm_mmu_free_some_pages(vcpu);
649497d1
AK
2412 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2413 i << 30,
651dd37a
JR
2414 PT32_ROOT_LEVEL, 1, ACC_ALL,
2415 NULL);
2416 root = __pa(sp->spt);
2417 ++sp->root_count;
2418 spin_unlock(&vcpu->kvm->mmu_lock);
2419 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
651dd37a 2420 }
6292757f 2421 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
651dd37a
JR
2422 } else
2423 BUG();
2424
2425 return 0;
2426}
2427
2428static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
17ac10ad 2429{
4db35314 2430 struct kvm_mmu_page *sp;
81407ca5
JR
2431 u64 pdptr, pm_mask;
2432 gfn_t root_gfn;
2433 int i;
3bb65a22 2434
5777ed34 2435 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
17ac10ad 2436
651dd37a
JR
2437 if (mmu_check_root(vcpu, root_gfn))
2438 return 1;
2439
2440 /*
2441 * Do we shadow a long mode page table? If so we need to
2442 * write-protect the guests page table root.
2443 */
2444 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
ad312c7c 2445 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad
AK
2446
2447 ASSERT(!VALID_PAGE(root));
651dd37a 2448
8facbbff 2449 spin_lock(&vcpu->kvm->mmu_lock);
24955b6c 2450 kvm_mmu_free_some_pages(vcpu);
651dd37a
JR
2451 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2452 0, ACC_ALL, NULL);
4db35314
AK
2453 root = __pa(sp->spt);
2454 ++sp->root_count;
8facbbff 2455 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 2456 vcpu->arch.mmu.root_hpa = root;
8986ecc0 2457 return 0;
17ac10ad 2458 }
f87f9288 2459
651dd37a
JR
2460 /*
2461 * We shadow a 32 bit page table. This may be a legacy 2-level
81407ca5
JR
2462 * or a PAE 3-level page table. In either case we need to be aware that
2463 * the shadow page table may be a PAE or a long mode page table.
651dd37a 2464 */
81407ca5
JR
2465 pm_mask = PT_PRESENT_MASK;
2466 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2467 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2468
17ac10ad 2469 for (i = 0; i < 4; ++i) {
ad312c7c 2470 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad
AK
2471
2472 ASSERT(!VALID_PAGE(root));
ad312c7c 2473 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
d41d1895 2474 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
43a3795a 2475 if (!is_present_gpte(pdptr)) {
ad312c7c 2476 vcpu->arch.mmu.pae_root[i] = 0;
417726a3
AK
2477 continue;
2478 }
6de4f3ad 2479 root_gfn = pdptr >> PAGE_SHIFT;
f87f9288
JR
2480 if (mmu_check_root(vcpu, root_gfn))
2481 return 1;
5a7388c2 2482 }
8facbbff 2483 spin_lock(&vcpu->kvm->mmu_lock);
24955b6c 2484 kvm_mmu_free_some_pages(vcpu);
4db35314 2485 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
651dd37a 2486 PT32_ROOT_LEVEL, 0,
f7d9c7b7 2487 ACC_ALL, NULL);
4db35314
AK
2488 root = __pa(sp->spt);
2489 ++sp->root_count;
8facbbff
AK
2490 spin_unlock(&vcpu->kvm->mmu_lock);
2491
81407ca5 2492 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
17ac10ad 2493 }
6292757f 2494 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
81407ca5
JR
2495
2496 /*
2497 * If we shadow a 32 bit page table with a long mode page
2498 * table we enter this path.
2499 */
2500 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2501 if (vcpu->arch.mmu.lm_root == NULL) {
2502 /*
2503 * The additional page necessary for this is only
2504 * allocated on demand.
2505 */
2506
2507 u64 *lm_root;
2508
2509 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2510 if (lm_root == NULL)
2511 return 1;
2512
2513 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2514
2515 vcpu->arch.mmu.lm_root = lm_root;
2516 }
2517
2518 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2519 }
2520
8986ecc0 2521 return 0;
17ac10ad
AK
2522}
2523
651dd37a
JR
2524static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2525{
2526 if (vcpu->arch.mmu.direct_map)
2527 return mmu_alloc_direct_roots(vcpu);
2528 else
2529 return mmu_alloc_shadow_roots(vcpu);
2530}
2531
0ba73cda
MT
2532static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2533{
2534 int i;
2535 struct kvm_mmu_page *sp;
2536
81407ca5
JR
2537 if (vcpu->arch.mmu.direct_map)
2538 return;
2539
0ba73cda
MT
2540 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2541 return;
6903074c 2542
bebb106a 2543 vcpu_clear_mmio_info(vcpu, ~0ul);
6903074c 2544 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
81407ca5 2545 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
0ba73cda
MT
2546 hpa_t root = vcpu->arch.mmu.root_hpa;
2547 sp = page_header(root);
2548 mmu_sync_children(vcpu, sp);
5054c0de 2549 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
0ba73cda
MT
2550 return;
2551 }
2552 for (i = 0; i < 4; ++i) {
2553 hpa_t root = vcpu->arch.mmu.pae_root[i];
2554
8986ecc0 2555 if (root && VALID_PAGE(root)) {
0ba73cda
MT
2556 root &= PT64_BASE_ADDR_MASK;
2557 sp = page_header(root);
2558 mmu_sync_children(vcpu, sp);
2559 }
2560 }
6903074c 2561 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
0ba73cda
MT
2562}
2563
2564void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2565{
2566 spin_lock(&vcpu->kvm->mmu_lock);
2567 mmu_sync_roots(vcpu);
6cffe8ca 2568 spin_unlock(&vcpu->kvm->mmu_lock);
0ba73cda
MT
2569}
2570
1871c602 2571static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
ab9ae313 2572 u32 access, struct x86_exception *exception)
6aa8b732 2573{
ab9ae313
AK
2574 if (exception)
2575 exception->error_code = 0;
6aa8b732
AK
2576 return vaddr;
2577}
2578
6539e738 2579static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
ab9ae313
AK
2580 u32 access,
2581 struct x86_exception *exception)
6539e738 2582{
ab9ae313
AK
2583 if (exception)
2584 exception->error_code = 0;
6539e738
JR
2585 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2586}
2587
6aa8b732 2588static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
78b2c54a 2589 u32 error_code, bool prefault)
6aa8b732 2590{
e833240f 2591 gfn_t gfn;
e2dec939 2592 int r;
6aa8b732 2593
b8688d51 2594 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
e2dec939
AK
2595 r = mmu_topup_memory_caches(vcpu);
2596 if (r)
2597 return r;
714b93da 2598
6aa8b732 2599 ASSERT(vcpu);
ad312c7c 2600 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 2601
e833240f 2602 gfn = gva >> PAGE_SHIFT;
6aa8b732 2603
e833240f 2604 return nonpaging_map(vcpu, gva & PAGE_MASK,
78b2c54a 2605 error_code & PFERR_WRITE_MASK, gfn, prefault);
6aa8b732
AK
2606}
2607
7e1fbeac 2608static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
af585b92
GN
2609{
2610 struct kvm_arch_async_pf arch;
fb67e14f 2611
7c90705b 2612 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
af585b92 2613 arch.gfn = gfn;
c4806acd 2614 arch.direct_map = vcpu->arch.mmu.direct_map;
fb67e14f 2615 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
af585b92
GN
2616
2617 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2618}
2619
2620static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2621{
2622 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2623 kvm_event_needs_reinjection(vcpu)))
2624 return false;
2625
2626 return kvm_x86_ops->interrupt_allowed(vcpu);
2627}
2628
78b2c54a 2629static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
612819c3 2630 gva_t gva, pfn_t *pfn, bool write, bool *writable)
af585b92
GN
2631{
2632 bool async;
2633
612819c3 2634 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
af585b92
GN
2635
2636 if (!async)
2637 return false; /* *pfn has correct page already */
2638
2639 put_page(pfn_to_page(*pfn));
2640
78b2c54a 2641 if (!prefault && can_do_async_pf(vcpu)) {
c9b263d2 2642 trace_kvm_try_async_get_page(gva, gfn);
af585b92
GN
2643 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2644 trace_kvm_async_pf_doublefault(gva, gfn);
2645 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2646 return true;
2647 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2648 return true;
2649 }
2650
612819c3 2651 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
af585b92
GN
2652
2653 return false;
2654}
2655
56028d08 2656static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
78b2c54a 2657 bool prefault)
fb72d167 2658{
35149e21 2659 pfn_t pfn;
fb72d167 2660 int r;
852e3c19 2661 int level;
936a5fe6 2662 int force_pt_level;
05da4558 2663 gfn_t gfn = gpa >> PAGE_SHIFT;
e930bffe 2664 unsigned long mmu_seq;
612819c3
MT
2665 int write = error_code & PFERR_WRITE_MASK;
2666 bool map_writable;
fb72d167
JR
2667
2668 ASSERT(vcpu);
2669 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2670
2671 r = mmu_topup_memory_caches(vcpu);
2672 if (r)
2673 return r;
2674
936a5fe6
AA
2675 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2676 if (likely(!force_pt_level)) {
2677 level = mapping_level(vcpu, gfn);
2678 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2679 } else
2680 level = PT_PAGE_TABLE_LEVEL;
852e3c19 2681
e930bffe 2682 mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 2683 smp_rmb();
af585b92 2684
78b2c54a 2685 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
af585b92
GN
2686 return 0;
2687
2688 /* mmio */
bf998156 2689 if (is_error_pfn(pfn))
bebb106a 2690 return kvm_handle_bad_page(vcpu, 0, 0, gfn, pfn);
fb72d167 2691 spin_lock(&vcpu->kvm->mmu_lock);
e930bffe
AA
2692 if (mmu_notifier_retry(vcpu, mmu_seq))
2693 goto out_unlock;
fb72d167 2694 kvm_mmu_free_some_pages(vcpu);
936a5fe6
AA
2695 if (likely(!force_pt_level))
2696 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
612819c3 2697 r = __direct_map(vcpu, gpa, write, map_writable,
2ec4739d 2698 level, gfn, pfn, prefault);
fb72d167 2699 spin_unlock(&vcpu->kvm->mmu_lock);
fb72d167
JR
2700
2701 return r;
e930bffe
AA
2702
2703out_unlock:
2704 spin_unlock(&vcpu->kvm->mmu_lock);
2705 kvm_release_pfn_clean(pfn);
2706 return 0;
fb72d167
JR
2707}
2708
6aa8b732
AK
2709static void nonpaging_free(struct kvm_vcpu *vcpu)
2710{
17ac10ad 2711 mmu_free_roots(vcpu);
6aa8b732
AK
2712}
2713
52fde8df
JR
2714static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2715 struct kvm_mmu *context)
6aa8b732 2716{
6aa8b732
AK
2717 context->new_cr3 = nonpaging_new_cr3;
2718 context->page_fault = nonpaging_page_fault;
6aa8b732
AK
2719 context->gva_to_gpa = nonpaging_gva_to_gpa;
2720 context->free = nonpaging_free;
e8bc217a 2721 context->sync_page = nonpaging_sync_page;
a7052897 2722 context->invlpg = nonpaging_invlpg;
0f53b5b1 2723 context->update_pte = nonpaging_update_pte;
cea0f0e7 2724 context->root_level = 0;
6aa8b732 2725 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 2726 context->root_hpa = INVALID_PAGE;
c5a78f2b 2727 context->direct_map = true;
2d48a985 2728 context->nx = false;
6aa8b732
AK
2729 return 0;
2730}
2731
d835dfec 2732void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
6aa8b732 2733{
1165f5fe 2734 ++vcpu->stat.tlb_flush;
a8eeb04a 2735 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
6aa8b732
AK
2736}
2737
2738static void paging_new_cr3(struct kvm_vcpu *vcpu)
2739{
9f8fe504 2740 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
cea0f0e7 2741 mmu_free_roots(vcpu);
6aa8b732
AK
2742}
2743
5777ed34
JR
2744static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2745{
9f8fe504 2746 return kvm_read_cr3(vcpu);
5777ed34
JR
2747}
2748
6389ee94
AK
2749static void inject_page_fault(struct kvm_vcpu *vcpu,
2750 struct x86_exception *fault)
6aa8b732 2751{
6389ee94 2752 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
6aa8b732
AK
2753}
2754
6aa8b732
AK
2755static void paging_free(struct kvm_vcpu *vcpu)
2756{
2757 nonpaging_free(vcpu);
2758}
2759
3241f22d 2760static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
82725b20
DE
2761{
2762 int bit7;
2763
2764 bit7 = (gpte >> 7) & 1;
3241f22d 2765 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
82725b20
DE
2766}
2767
6aa8b732
AK
2768#define PTTYPE 64
2769#include "paging_tmpl.h"
2770#undef PTTYPE
2771
2772#define PTTYPE 32
2773#include "paging_tmpl.h"
2774#undef PTTYPE
2775
52fde8df
JR
2776static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2777 struct kvm_mmu *context,
2778 int level)
82725b20 2779{
82725b20
DE
2780 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2781 u64 exb_bit_rsvd = 0;
2782
2d48a985 2783 if (!context->nx)
82725b20
DE
2784 exb_bit_rsvd = rsvd_bits(63, 63);
2785 switch (level) {
2786 case PT32_ROOT_LEVEL:
2787 /* no rsvd bits for 2 level 4K page table entries */
2788 context->rsvd_bits_mask[0][1] = 0;
2789 context->rsvd_bits_mask[0][0] = 0;
f815bce8
XG
2790 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2791
2792 if (!is_pse(vcpu)) {
2793 context->rsvd_bits_mask[1][1] = 0;
2794 break;
2795 }
2796
82725b20
DE
2797 if (is_cpuid_PSE36())
2798 /* 36bits PSE 4MB page */
2799 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2800 else
2801 /* 32 bits PSE 4MB page */
2802 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
82725b20
DE
2803 break;
2804 case PT32E_ROOT_LEVEL:
20c466b5
DE
2805 context->rsvd_bits_mask[0][2] =
2806 rsvd_bits(maxphyaddr, 63) |
2807 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
82725b20 2808 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4c26b4cd 2809 rsvd_bits(maxphyaddr, 62); /* PDE */
82725b20
DE
2810 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2811 rsvd_bits(maxphyaddr, 62); /* PTE */
2812 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2813 rsvd_bits(maxphyaddr, 62) |
2814 rsvd_bits(13, 20); /* large page */
f815bce8 2815 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
82725b20
DE
2816 break;
2817 case PT64_ROOT_LEVEL:
2818 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2819 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2820 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2821 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2822 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4c26b4cd 2823 rsvd_bits(maxphyaddr, 51);
82725b20
DE
2824 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2825 rsvd_bits(maxphyaddr, 51);
2826 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
e04da980
JR
2827 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2828 rsvd_bits(maxphyaddr, 51) |
2829 rsvd_bits(13, 29);
82725b20 2830 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
4c26b4cd
SY
2831 rsvd_bits(maxphyaddr, 51) |
2832 rsvd_bits(13, 20); /* large page */
f815bce8 2833 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
82725b20
DE
2834 break;
2835 }
2836}
2837
52fde8df
JR
2838static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2839 struct kvm_mmu *context,
2840 int level)
6aa8b732 2841{
2d48a985
JR
2842 context->nx = is_nx(vcpu);
2843
52fde8df 2844 reset_rsvds_bits_mask(vcpu, context, level);
6aa8b732
AK
2845
2846 ASSERT(is_pae(vcpu));
2847 context->new_cr3 = paging_new_cr3;
2848 context->page_fault = paging64_page_fault;
6aa8b732 2849 context->gva_to_gpa = paging64_gva_to_gpa;
e8bc217a 2850 context->sync_page = paging64_sync_page;
a7052897 2851 context->invlpg = paging64_invlpg;
0f53b5b1 2852 context->update_pte = paging64_update_pte;
6aa8b732 2853 context->free = paging_free;
17ac10ad
AK
2854 context->root_level = level;
2855 context->shadow_root_level = level;
17c3ba9d 2856 context->root_hpa = INVALID_PAGE;
c5a78f2b 2857 context->direct_map = false;
6aa8b732
AK
2858 return 0;
2859}
2860
52fde8df
JR
2861static int paging64_init_context(struct kvm_vcpu *vcpu,
2862 struct kvm_mmu *context)
17ac10ad 2863{
52fde8df 2864 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
17ac10ad
AK
2865}
2866
52fde8df
JR
2867static int paging32_init_context(struct kvm_vcpu *vcpu,
2868 struct kvm_mmu *context)
6aa8b732 2869{
2d48a985
JR
2870 context->nx = false;
2871
52fde8df 2872 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
6aa8b732
AK
2873
2874 context->new_cr3 = paging_new_cr3;
2875 context->page_fault = paging32_page_fault;
6aa8b732
AK
2876 context->gva_to_gpa = paging32_gva_to_gpa;
2877 context->free = paging_free;
e8bc217a 2878 context->sync_page = paging32_sync_page;
a7052897 2879 context->invlpg = paging32_invlpg;
0f53b5b1 2880 context->update_pte = paging32_update_pte;
6aa8b732
AK
2881 context->root_level = PT32_ROOT_LEVEL;
2882 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 2883 context->root_hpa = INVALID_PAGE;
c5a78f2b 2884 context->direct_map = false;
6aa8b732
AK
2885 return 0;
2886}
2887
52fde8df
JR
2888static int paging32E_init_context(struct kvm_vcpu *vcpu,
2889 struct kvm_mmu *context)
6aa8b732 2890{
52fde8df 2891 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
6aa8b732
AK
2892}
2893
fb72d167
JR
2894static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2895{
14dfe855 2896 struct kvm_mmu *context = vcpu->arch.walk_mmu;
fb72d167 2897
c445f8ef 2898 context->base_role.word = 0;
fb72d167
JR
2899 context->new_cr3 = nonpaging_new_cr3;
2900 context->page_fault = tdp_page_fault;
2901 context->free = nonpaging_free;
e8bc217a 2902 context->sync_page = nonpaging_sync_page;
a7052897 2903 context->invlpg = nonpaging_invlpg;
0f53b5b1 2904 context->update_pte = nonpaging_update_pte;
67253af5 2905 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
fb72d167 2906 context->root_hpa = INVALID_PAGE;
c5a78f2b 2907 context->direct_map = true;
1c97f0a0 2908 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
5777ed34 2909 context->get_cr3 = get_cr3;
cb659db8 2910 context->inject_page_fault = kvm_inject_page_fault;
2d48a985 2911 context->nx = is_nx(vcpu);
fb72d167
JR
2912
2913 if (!is_paging(vcpu)) {
2d48a985 2914 context->nx = false;
fb72d167
JR
2915 context->gva_to_gpa = nonpaging_gva_to_gpa;
2916 context->root_level = 0;
2917 } else if (is_long_mode(vcpu)) {
2d48a985 2918 context->nx = is_nx(vcpu);
52fde8df 2919 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
fb72d167
JR
2920 context->gva_to_gpa = paging64_gva_to_gpa;
2921 context->root_level = PT64_ROOT_LEVEL;
2922 } else if (is_pae(vcpu)) {
2d48a985 2923 context->nx = is_nx(vcpu);
52fde8df 2924 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
fb72d167
JR
2925 context->gva_to_gpa = paging64_gva_to_gpa;
2926 context->root_level = PT32E_ROOT_LEVEL;
2927 } else {
2d48a985 2928 context->nx = false;
52fde8df 2929 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
fb72d167
JR
2930 context->gva_to_gpa = paging32_gva_to_gpa;
2931 context->root_level = PT32_ROOT_LEVEL;
2932 }
2933
2934 return 0;
2935}
2936
52fde8df 2937int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
6aa8b732 2938{
a770f6f2 2939 int r;
411c588d 2940 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
6aa8b732 2941 ASSERT(vcpu);
ad312c7c 2942 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732
AK
2943
2944 if (!is_paging(vcpu))
52fde8df 2945 r = nonpaging_init_context(vcpu, context);
a9058ecd 2946 else if (is_long_mode(vcpu))
52fde8df 2947 r = paging64_init_context(vcpu, context);
6aa8b732 2948 else if (is_pae(vcpu))
52fde8df 2949 r = paging32E_init_context(vcpu, context);
6aa8b732 2950 else
52fde8df 2951 r = paging32_init_context(vcpu, context);
a770f6f2 2952
5b7e0102 2953 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
f43addd4 2954 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
411c588d
AK
2955 vcpu->arch.mmu.base_role.smep_andnot_wp
2956 = smep && !is_write_protection(vcpu);
52fde8df
JR
2957
2958 return r;
2959}
2960EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2961
2962static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2963{
14dfe855 2964 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
52fde8df 2965
14dfe855
JR
2966 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2967 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2968 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
a770f6f2
AK
2969
2970 return r;
6aa8b732
AK
2971}
2972
02f59dc9
JR
2973static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2974{
2975 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2976
2977 g_context->get_cr3 = get_cr3;
2978 g_context->inject_page_fault = kvm_inject_page_fault;
2979
2980 /*
2981 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
2982 * translation of l2_gpa to l1_gpa addresses is done using the
2983 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
2984 * functions between mmu and nested_mmu are swapped.
2985 */
2986 if (!is_paging(vcpu)) {
2d48a985 2987 g_context->nx = false;
02f59dc9
JR
2988 g_context->root_level = 0;
2989 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
2990 } else if (is_long_mode(vcpu)) {
2d48a985 2991 g_context->nx = is_nx(vcpu);
02f59dc9
JR
2992 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
2993 g_context->root_level = PT64_ROOT_LEVEL;
2994 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2995 } else if (is_pae(vcpu)) {
2d48a985 2996 g_context->nx = is_nx(vcpu);
02f59dc9
JR
2997 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
2998 g_context->root_level = PT32E_ROOT_LEVEL;
2999 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3000 } else {
2d48a985 3001 g_context->nx = false;
02f59dc9
JR
3002 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3003 g_context->root_level = PT32_ROOT_LEVEL;
3004 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3005 }
3006
3007 return 0;
3008}
3009
fb72d167
JR
3010static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3011{
02f59dc9
JR
3012 if (mmu_is_nested(vcpu))
3013 return init_kvm_nested_mmu(vcpu);
3014 else if (tdp_enabled)
fb72d167
JR
3015 return init_kvm_tdp_mmu(vcpu);
3016 else
3017 return init_kvm_softmmu(vcpu);
3018}
3019
6aa8b732
AK
3020static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3021{
3022 ASSERT(vcpu);
62ad0755
SY
3023 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3024 /* mmu.free() should set root_hpa = INVALID_PAGE */
ad312c7c 3025 vcpu->arch.mmu.free(vcpu);
6aa8b732
AK
3026}
3027
3028int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
17c3ba9d
AK
3029{
3030 destroy_kvm_mmu(vcpu);
f8f7e5ee 3031 return init_kvm_mmu(vcpu);
17c3ba9d 3032}
8668a3c4 3033EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
17c3ba9d
AK
3034
3035int kvm_mmu_load(struct kvm_vcpu *vcpu)
6aa8b732 3036{
714b93da
AK
3037 int r;
3038
e2dec939 3039 r = mmu_topup_memory_caches(vcpu);
17c3ba9d
AK
3040 if (r)
3041 goto out;
8986ecc0 3042 r = mmu_alloc_roots(vcpu);
8facbbff 3043 spin_lock(&vcpu->kvm->mmu_lock);
0ba73cda 3044 mmu_sync_roots(vcpu);
aaee2c94 3045 spin_unlock(&vcpu->kvm->mmu_lock);
8986ecc0
MT
3046 if (r)
3047 goto out;
3662cb1c 3048 /* set_cr3() should ensure TLB has been flushed */
f43addd4 3049 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
714b93da
AK
3050out:
3051 return r;
6aa8b732 3052}
17c3ba9d
AK
3053EXPORT_SYMBOL_GPL(kvm_mmu_load);
3054
3055void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3056{
3057 mmu_free_roots(vcpu);
3058}
4b16184c 3059EXPORT_SYMBOL_GPL(kvm_mmu_unload);
6aa8b732 3060
0028425f 3061static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
7c562522
XG
3062 struct kvm_mmu_page *sp, u64 *spte,
3063 const void *new)
0028425f 3064{
30945387 3065 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
7e4e4056
JR
3066 ++vcpu->kvm->stat.mmu_pde_zapped;
3067 return;
30945387 3068 }
0028425f 3069
4cee5764 3070 ++vcpu->kvm->stat.mmu_pte_updated;
7c562522 3071 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
0028425f
AK
3072}
3073
79539cec
AK
3074static bool need_remote_flush(u64 old, u64 new)
3075{
3076 if (!is_shadow_present_pte(old))
3077 return false;
3078 if (!is_shadow_present_pte(new))
3079 return true;
3080 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3081 return true;
3082 old ^= PT64_NX_MASK;
3083 new ^= PT64_NX_MASK;
3084 return (old & ~new & PT64_PERM_MASK) != 0;
3085}
3086
0671a8e7
XG
3087static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3088 bool remote_flush, bool local_flush)
79539cec 3089{
0671a8e7
XG
3090 if (zap_page)
3091 return;
3092
3093 if (remote_flush)
79539cec 3094 kvm_flush_remote_tlbs(vcpu->kvm);
0671a8e7 3095 else if (local_flush)
79539cec
AK
3096 kvm_mmu_flush_tlb(vcpu);
3097}
3098
12b7d28f
AK
3099static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3100{
ad312c7c 3101 u64 *spte = vcpu->arch.last_pte_updated;
12b7d28f 3102
7b52345e 3103 return !!(spte && (*spte & shadow_accessed_mask));
12b7d28f
AK
3104}
3105
1b7fcd32
AK
3106static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3107{
3108 u64 *spte = vcpu->arch.last_pte_updated;
3109
3110 if (spte
3111 && vcpu->arch.last_pte_gfn == gfn
3112 && shadow_accessed_mask
3113 && !(*spte & shadow_accessed_mask)
3114 && is_shadow_present_pte(*spte))
3115 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3116}
3117
09072daf 3118void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
ad218f85
MT
3119 const u8 *new, int bytes,
3120 bool guest_initiated)
da4a00f0 3121{
9b7a0325 3122 gfn_t gfn = gpa >> PAGE_SHIFT;
fa1de2bf 3123 union kvm_mmu_page_role mask = { .word = 0 };
4db35314 3124 struct kvm_mmu_page *sp;
f41d335a 3125 struct hlist_node *node;
d98ba053 3126 LIST_HEAD(invalid_list);
0f53b5b1
XG
3127 u64 entry, gentry, *spte;
3128 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3129 int level, npte, invlpg_counter, r, flooded = 0;
0671a8e7
XG
3130 bool remote_flush, local_flush, zap_page;
3131
332b207d
XG
3132 /*
3133 * If we don't have indirect shadow pages, it means no page is
3134 * write-protected, so we can exit simply.
3135 */
3136 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3137 return;
3138
0671a8e7 3139 zap_page = remote_flush = local_flush = false;
0f53b5b1 3140 offset = offset_in_page(gpa);
9b7a0325 3141
b8688d51 3142 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
72016f3a 3143
08e850c6 3144 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
72016f3a
AK
3145
3146 /*
3147 * Assume that the pte write on a page table of the same type
49b26e26
XG
3148 * as the current vcpu paging mode since we update the sptes only
3149 * when they have the same mode.
72016f3a 3150 */
08e850c6 3151 if ((is_pae(vcpu) && bytes == 4) || !new) {
72016f3a 3152 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
08e850c6
AK
3153 if (is_pae(vcpu)) {
3154 gpa &= ~(gpa_t)7;
3155 bytes = 8;
3156 }
3157 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
72016f3a
AK
3158 if (r)
3159 gentry = 0;
08e850c6
AK
3160 new = (const u8 *)&gentry;
3161 }
3162
3163 switch (bytes) {
3164 case 4:
3165 gentry = *(const u32 *)new;
3166 break;
3167 case 8:
3168 gentry = *(const u64 *)new;
3169 break;
3170 default:
3171 gentry = 0;
3172 break;
72016f3a
AK
3173 }
3174
aaee2c94 3175 spin_lock(&vcpu->kvm->mmu_lock);
08e850c6
AK
3176 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3177 gentry = 0;
eb787d10 3178 kvm_mmu_free_some_pages(vcpu);
4cee5764 3179 ++vcpu->kvm->stat.mmu_pte_write;
8b1fe17c 3180 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
ad218f85 3181 if (guest_initiated) {
1b7fd45c 3182 kvm_mmu_access_page(vcpu, gfn);
ad218f85
MT
3183 if (gfn == vcpu->arch.last_pt_write_gfn
3184 && !last_updated_pte_accessed(vcpu)) {
3185 ++vcpu->arch.last_pt_write_count;
3186 if (vcpu->arch.last_pt_write_count >= 3)
3187 flooded = 1;
3188 } else {
3189 vcpu->arch.last_pt_write_gfn = gfn;
3190 vcpu->arch.last_pt_write_count = 1;
3191 vcpu->arch.last_pte_updated = NULL;
3192 }
86a5ba02 3193 }
3246af0e 3194
fa1de2bf 3195 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
f41d335a 3196 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
5b7e0102 3197 pte_size = sp->role.cr4_pae ? 8 : 4;
0e7bc4b9 3198 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
e925c5ba 3199 misaligned |= bytes < 4;
86a5ba02 3200 if (misaligned || flooded) {
0e7bc4b9
AK
3201 /*
3202 * Misaligned accesses are too much trouble to fix
3203 * up; also, they usually indicate a page is not used
3204 * as a page table.
86a5ba02
AK
3205 *
3206 * If we're seeing too many writes to a page,
3207 * it may no longer be a page table, or we may be
3208 * forking, in which case it is better to unmap the
3209 * page.
0e7bc4b9
AK
3210 */
3211 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
4db35314 3212 gpa, bytes, sp->role.word);
0671a8e7 3213 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
f41d335a 3214 &invalid_list);
4cee5764 3215 ++vcpu->kvm->stat.mmu_flooded;
0e7bc4b9
AK
3216 continue;
3217 }
9b7a0325 3218 page_offset = offset;
4db35314 3219 level = sp->role.level;
ac1b714e 3220 npte = 1;
5b7e0102 3221 if (!sp->role.cr4_pae) {
ac1b714e
AK
3222 page_offset <<= 1; /* 32->64 */
3223 /*
3224 * A 32-bit pde maps 4MB while the shadow pdes map
3225 * only 2MB. So we need to double the offset again
3226 * and zap two pdes instead of one.
3227 */
3228 if (level == PT32_ROOT_LEVEL) {
6b8d0f9b 3229 page_offset &= ~7; /* kill rounding error */
ac1b714e
AK
3230 page_offset <<= 1;
3231 npte = 2;
3232 }
fce0657f 3233 quadrant = page_offset >> PAGE_SHIFT;
9b7a0325 3234 page_offset &= ~PAGE_MASK;
4db35314 3235 if (quadrant != sp->role.quadrant)
fce0657f 3236 continue;
9b7a0325 3237 }
0671a8e7 3238 local_flush = true;
4db35314 3239 spte = &sp->spt[page_offset / sizeof(*spte)];
ac1b714e 3240 while (npte--) {
79539cec 3241 entry = *spte;
38e3b2b2 3242 mmu_page_zap_pte(vcpu->kvm, sp, spte);
fa1de2bf
XG
3243 if (gentry &&
3244 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3245 & mask.word))
7c562522 3246 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
0671a8e7
XG
3247 if (!remote_flush && need_remote_flush(entry, *spte))
3248 remote_flush = true;
ac1b714e 3249 ++spte;
9b7a0325 3250 }
9b7a0325 3251 }
0671a8e7 3252 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
d98ba053 3253 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
8b1fe17c 3254 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
aaee2c94 3255 spin_unlock(&vcpu->kvm->mmu_lock);
da4a00f0
AK
3256}
3257
a436036b
AK
3258int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3259{
10589a46
MT
3260 gpa_t gpa;
3261 int r;
a436036b 3262
c5a78f2b 3263 if (vcpu->arch.mmu.direct_map)
60f24784
AK
3264 return 0;
3265
1871c602 3266 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
10589a46 3267
aaee2c94 3268 spin_lock(&vcpu->kvm->mmu_lock);
10589a46 3269 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
aaee2c94 3270 spin_unlock(&vcpu->kvm->mmu_lock);
10589a46 3271 return r;
a436036b 3272}
577bdc49 3273EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
a436036b 3274
22d95b12 3275void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
ebeace86 3276{
d98ba053 3277 LIST_HEAD(invalid_list);
103ad25a 3278
e0df7b9f 3279 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
3b80fffe 3280 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
4db35314 3281 struct kvm_mmu_page *sp;
ebeace86 3282
f05e70ac 3283 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
4db35314 3284 struct kvm_mmu_page, link);
e0df7b9f 3285 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
4cee5764 3286 ++vcpu->kvm->stat.mmu_recycled;
ebeace86 3287 }
aa6bd187 3288 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
ebeace86 3289}
ebeace86 3290
dc25e89e
AP
3291int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3292 void *insn, int insn_len)
3067714c
AK
3293{
3294 int r;
3295 enum emulation_result er;
3296
56028d08 3297 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
3067714c
AK
3298 if (r < 0)
3299 goto out;
3300
3301 if (!r) {
3302 r = 1;
3303 goto out;
3304 }
3305
b733bfb5
AK
3306 r = mmu_topup_memory_caches(vcpu);
3307 if (r)
3308 goto out;
3309
dc25e89e 3310 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
3067714c
AK
3311
3312 switch (er) {
3313 case EMULATE_DONE:
3314 return 1;
3315 case EMULATE_DO_MMIO:
3316 ++vcpu->stat.mmio_exits;
6d77dbfc 3317 /* fall through */
3067714c 3318 case EMULATE_FAIL:
3f5d18a9 3319 return 0;
3067714c
AK
3320 default:
3321 BUG();
3322 }
3323out:
3067714c
AK
3324 return r;
3325}
3326EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3327
a7052897
MT
3328void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3329{
a7052897 3330 vcpu->arch.mmu.invlpg(vcpu, gva);
a7052897
MT
3331 kvm_mmu_flush_tlb(vcpu);
3332 ++vcpu->stat.invlpg;
3333}
3334EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3335
18552672
JR
3336void kvm_enable_tdp(void)
3337{
3338 tdp_enabled = true;
3339}
3340EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3341
5f4cb662
JR
3342void kvm_disable_tdp(void)
3343{
3344 tdp_enabled = false;
3345}
3346EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3347
6aa8b732
AK
3348static void free_mmu_pages(struct kvm_vcpu *vcpu)
3349{
ad312c7c 3350 free_page((unsigned long)vcpu->arch.mmu.pae_root);
81407ca5
JR
3351 if (vcpu->arch.mmu.lm_root != NULL)
3352 free_page((unsigned long)vcpu->arch.mmu.lm_root);
6aa8b732
AK
3353}
3354
3355static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3356{
17ac10ad 3357 struct page *page;
6aa8b732
AK
3358 int i;
3359
3360 ASSERT(vcpu);
3361
17ac10ad
AK
3362 /*
3363 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3364 * Therefore we need to allocate shadow page tables in the first
3365 * 4GB of memory, which happens to fit the DMA32 zone.
3366 */
3367 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3368 if (!page)
d7fa6ab2
WY
3369 return -ENOMEM;
3370
ad312c7c 3371 vcpu->arch.mmu.pae_root = page_address(page);
17ac10ad 3372 for (i = 0; i < 4; ++i)
ad312c7c 3373 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 3374
6aa8b732 3375 return 0;
6aa8b732
AK
3376}
3377
8018c27b 3378int kvm_mmu_create(struct kvm_vcpu *vcpu)
6aa8b732 3379{
6aa8b732 3380 ASSERT(vcpu);
ad312c7c 3381 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 3382
8018c27b
IM
3383 return alloc_mmu_pages(vcpu);
3384}
6aa8b732 3385
8018c27b
IM
3386int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3387{
3388 ASSERT(vcpu);
ad312c7c 3389 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2c264957 3390
8018c27b 3391 return init_kvm_mmu(vcpu);
6aa8b732
AK
3392}
3393
90cb0529 3394void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
6aa8b732 3395{
4db35314 3396 struct kvm_mmu_page *sp;
6aa8b732 3397
f05e70ac 3398 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
6aa8b732
AK
3399 int i;
3400 u64 *pt;
3401
291f26bc 3402 if (!test_bit(slot, sp->slot_bitmap))
6aa8b732
AK
3403 continue;
3404
4db35314 3405 pt = sp->spt;
8234b22e 3406 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
da8dc75f
XG
3407 if (!is_shadow_present_pte(pt[i]) ||
3408 !is_last_spte(pt[i], sp->role.level))
3409 continue;
3410
3411 if (is_large_pte(pt[i])) {
c3707958 3412 drop_spte(kvm, &pt[i]);
8234b22e 3413 --kvm->stat.lpages;
da8dc75f 3414 continue;
8234b22e 3415 }
da8dc75f 3416
6aa8b732 3417 /* avoid RMW */
01c168ac 3418 if (is_writable_pte(pt[i]))
700e1b12 3419 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
8234b22e 3420 }
6aa8b732 3421 }
171d595d 3422 kvm_flush_remote_tlbs(kvm);
6aa8b732 3423}
37a7d8b0 3424
90cb0529 3425void kvm_mmu_zap_all(struct kvm *kvm)
e0fa826f 3426{
4db35314 3427 struct kvm_mmu_page *sp, *node;
d98ba053 3428 LIST_HEAD(invalid_list);
e0fa826f 3429
aaee2c94 3430 spin_lock(&kvm->mmu_lock);
3246af0e 3431restart:
f05e70ac 3432 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
d98ba053 3433 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
3246af0e
XG
3434 goto restart;
3435
d98ba053 3436 kvm_mmu_commit_zap_page(kvm, &invalid_list);
aaee2c94 3437 spin_unlock(&kvm->mmu_lock);
e0fa826f
DL
3438}
3439
d98ba053
XG
3440static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3441 struct list_head *invalid_list)
3ee16c81
IE
3442{
3443 struct kvm_mmu_page *page;
3444
3445 page = container_of(kvm->arch.active_mmu_pages.prev,
3446 struct kvm_mmu_page, link);
d98ba053 3447 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
3ee16c81
IE
3448}
3449
1495f230 3450static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
3ee16c81
IE
3451{
3452 struct kvm *kvm;
3453 struct kvm *kvm_freed = NULL;
1495f230 3454 int nr_to_scan = sc->nr_to_scan;
45221ab6
DH
3455
3456 if (nr_to_scan == 0)
3457 goto out;
3ee16c81 3458
e935b837 3459 raw_spin_lock(&kvm_lock);
3ee16c81
IE
3460
3461 list_for_each_entry(kvm, &vm_list, vm_list) {
45221ab6 3462 int idx, freed_pages;
d98ba053 3463 LIST_HEAD(invalid_list);
3ee16c81 3464
f656ce01 3465 idx = srcu_read_lock(&kvm->srcu);
3ee16c81 3466 spin_lock(&kvm->mmu_lock);
45221ab6
DH
3467 if (!kvm_freed && nr_to_scan > 0 &&
3468 kvm->arch.n_used_mmu_pages > 0) {
d98ba053
XG
3469 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3470 &invalid_list);
3ee16c81
IE
3471 kvm_freed = kvm;
3472 }
3473 nr_to_scan--;
3474
d98ba053 3475 kvm_mmu_commit_zap_page(kvm, &invalid_list);
3ee16c81 3476 spin_unlock(&kvm->mmu_lock);
f656ce01 3477 srcu_read_unlock(&kvm->srcu, idx);
3ee16c81
IE
3478 }
3479 if (kvm_freed)
3480 list_move_tail(&kvm_freed->vm_list, &vm_list);
3481
e935b837 3482 raw_spin_unlock(&kvm_lock);
3ee16c81 3483
45221ab6
DH
3484out:
3485 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
3ee16c81
IE
3486}
3487
3488static struct shrinker mmu_shrinker = {
3489 .shrink = mmu_shrink,
3490 .seeks = DEFAULT_SEEKS * 10,
3491};
3492
2ddfd20e 3493static void mmu_destroy_caches(void)
b5a33a75 3494{
53c07b18
XG
3495 if (pte_list_desc_cache)
3496 kmem_cache_destroy(pte_list_desc_cache);
d3d25b04
AK
3497 if (mmu_page_header_cache)
3498 kmem_cache_destroy(mmu_page_header_cache);
b5a33a75
AK
3499}
3500
3501int kvm_mmu_module_init(void)
3502{
53c07b18
XG
3503 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3504 sizeof(struct pte_list_desc),
20c2df83 3505 0, 0, NULL);
53c07b18 3506 if (!pte_list_desc_cache)
b5a33a75
AK
3507 goto nomem;
3508
d3d25b04
AK
3509 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3510 sizeof(struct kvm_mmu_page),
20c2df83 3511 0, 0, NULL);
d3d25b04
AK
3512 if (!mmu_page_header_cache)
3513 goto nomem;
3514
45bf21a8
WY
3515 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3516 goto nomem;
3517
3ee16c81
IE
3518 register_shrinker(&mmu_shrinker);
3519
b5a33a75
AK
3520 return 0;
3521
3522nomem:
3ee16c81 3523 mmu_destroy_caches();
b5a33a75
AK
3524 return -ENOMEM;
3525}
3526
3ad82a7e
ZX
3527/*
3528 * Caculate mmu pages needed for kvm.
3529 */
3530unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3531{
3532 int i;
3533 unsigned int nr_mmu_pages;
3534 unsigned int nr_pages = 0;
bc6678a3 3535 struct kvm_memslots *slots;
3ad82a7e 3536
90d83dc3
LJ
3537 slots = kvm_memslots(kvm);
3538
bc6678a3
MT
3539 for (i = 0; i < slots->nmemslots; i++)
3540 nr_pages += slots->memslots[i].npages;
3ad82a7e
ZX
3541
3542 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3543 nr_mmu_pages = max(nr_mmu_pages,
3544 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3545
3546 return nr_mmu_pages;
3547}
3548
2f333bcb
MT
3549static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3550 unsigned len)
3551{
3552 if (len > buffer->len)
3553 return NULL;
3554 return buffer->ptr;
3555}
3556
3557static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3558 unsigned len)
3559{
3560 void *ret;
3561
3562 ret = pv_mmu_peek_buffer(buffer, len);
3563 if (!ret)
3564 return ret;
3565 buffer->ptr += len;
3566 buffer->len -= len;
3567 buffer->processed += len;
3568 return ret;
3569}
3570
3571static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3572 gpa_t addr, gpa_t value)
3573{
3574 int bytes = 8;
3575 int r;
3576
3577 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3578 bytes = 4;
3579
3580 r = mmu_topup_memory_caches(vcpu);
3581 if (r)
3582 return r;
3583
3200f405 3584 if (!emulator_write_phys(vcpu, addr, &value, bytes))
2f333bcb
MT
3585 return -EFAULT;
3586
3587 return 1;
3588}
3589
3590static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3591{
9f8fe504 3592 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
2f333bcb
MT
3593 return 1;
3594}
3595
3596static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3597{
3598 spin_lock(&vcpu->kvm->mmu_lock);
3599 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3600 spin_unlock(&vcpu->kvm->mmu_lock);
3601 return 1;
3602}
3603
3604static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3605 struct kvm_pv_mmu_op_buffer *buffer)
3606{
3607 struct kvm_mmu_op_header *header;
3608
3609 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3610 if (!header)
3611 return 0;
3612 switch (header->op) {
3613 case KVM_MMU_OP_WRITE_PTE: {
3614 struct kvm_mmu_op_write_pte *wpte;
3615
3616 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3617 if (!wpte)
3618 return 0;
3619 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3620 wpte->pte_val);
3621 }
3622 case KVM_MMU_OP_FLUSH_TLB: {
3623 struct kvm_mmu_op_flush_tlb *ftlb;
3624
3625 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3626 if (!ftlb)
3627 return 0;
3628 return kvm_pv_mmu_flush_tlb(vcpu);
3629 }
3630 case KVM_MMU_OP_RELEASE_PT: {
3631 struct kvm_mmu_op_release_pt *rpt;
3632
3633 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3634 if (!rpt)
3635 return 0;
3636 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3637 }
3638 default: return 0;
3639 }
3640}
3641
3642int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3643 gpa_t addr, unsigned long *ret)
3644{
3645 int r;
6ad18fba 3646 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
2f333bcb 3647
6ad18fba
DH
3648 buffer->ptr = buffer->buf;
3649 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3650 buffer->processed = 0;
2f333bcb 3651
6ad18fba 3652 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
2f333bcb
MT
3653 if (r)
3654 goto out;
3655
6ad18fba
DH
3656 while (buffer->len) {
3657 r = kvm_pv_mmu_op_one(vcpu, buffer);
2f333bcb
MT
3658 if (r < 0)
3659 goto out;
3660 if (r == 0)
3661 break;
3662 }
3663
3664 r = 1;
3665out:
6ad18fba 3666 *ret = buffer->processed;
2f333bcb
MT
3667 return r;
3668}
3669
94d8b056
MT
3670int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3671{
3672 struct kvm_shadow_walk_iterator iterator;
3673 int nr_sptes = 0;
3674
3675 spin_lock(&vcpu->kvm->mmu_lock);
3676 for_each_shadow_entry(vcpu, addr, iterator) {
3677 sptes[iterator.level-1] = *iterator.sptep;
3678 nr_sptes++;
3679 if (!is_shadow_present_pte(*iterator.sptep))
3680 break;
3681 }
3682 spin_unlock(&vcpu->kvm->mmu_lock);
3683
3684 return nr_sptes;
3685}
3686EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3687
c42fffe3
XG
3688void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3689{
3690 ASSERT(vcpu);
3691
3692 destroy_kvm_mmu(vcpu);
3693 free_mmu_pages(vcpu);
3694 mmu_free_memory_caches(vcpu);
b034cf01
XG
3695}
3696
3697#ifdef CONFIG_KVM_MMU_AUDIT
3698#include "mmu_audit.c"
3699#else
3700static void mmu_audit_disable(void) { }
3701#endif
3702
3703void kvm_mmu_module_exit(void)
3704{
3705 mmu_destroy_caches();
3706 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3707 unregister_shrinker(&mmu_shrinker);
c42fffe3
XG
3708 mmu_audit_disable();
3709}
This page took 0.722195 seconds and 5 git commands to generate.