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