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