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