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