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