]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kvm/mmu.c
KVM: detect if VCPU triple faults
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kvm / mmu.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
e495606d
AK
19
20#include "vmx.h"
1d737c8a 21#include "mmu.h"
e495606d 22
edf88417 23#include <linux/kvm_host.h>
6aa8b732
AK
24#include <linux/types.h>
25#include <linux/string.h>
6aa8b732
AK
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
448353ca 29#include <linux/swap.h>
05da4558 30#include <linux/hugetlb.h>
6aa8b732 31
e495606d
AK
32#include <asm/page.h>
33#include <asm/cmpxchg.h>
4e542370 34#include <asm/io.h>
6aa8b732 35
18552672
JR
36/*
37 * When setting this variable to true it enables Two-Dimensional-Paging
38 * where the hardware walks 2 page tables:
39 * 1. the guest-virtual to guest-physical
40 * 2. while doing 1. it walks guest-physical to host-physical
41 * If the hardware supports that we don't need to do shadow paging.
42 */
43static bool tdp_enabled = false;
44
37a7d8b0
AK
45#undef MMU_DEBUG
46
47#undef AUDIT
48
49#ifdef AUDIT
50static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
51#else
52static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
53#endif
54
55#ifdef MMU_DEBUG
56
57#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
58#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
59
60#else
61
62#define pgprintk(x...) do { } while (0)
63#define rmap_printk(x...) do { } while (0)
64
65#endif
66
67#if defined(MMU_DEBUG) || defined(AUDIT)
68static int dbg = 1;
69#endif
6aa8b732 70
d6c69ee9
YD
71#ifndef MMU_DEBUG
72#define ASSERT(x) do { } while (0)
73#else
6aa8b732
AK
74#define ASSERT(x) \
75 if (!(x)) { \
76 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
77 __FILE__, __LINE__, #x); \
78 }
d6c69ee9 79#endif
6aa8b732 80
cea0f0e7
AK
81#define PT64_PT_BITS 9
82#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
83#define PT32_PT_BITS 10
84#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
6aa8b732
AK
85
86#define PT_WRITABLE_SHIFT 1
87
88#define PT_PRESENT_MASK (1ULL << 0)
89#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
90#define PT_USER_MASK (1ULL << 2)
91#define PT_PWT_MASK (1ULL << 3)
92#define PT_PCD_MASK (1ULL << 4)
93#define PT_ACCESSED_MASK (1ULL << 5)
94#define PT_DIRTY_MASK (1ULL << 6)
95#define PT_PAGE_SIZE_MASK (1ULL << 7)
96#define PT_PAT_MASK (1ULL << 7)
97#define PT_GLOBAL_MASK (1ULL << 8)
fe135d2c
AK
98#define PT64_NX_SHIFT 63
99#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
6aa8b732
AK
100
101#define PT_PAT_SHIFT 7
102#define PT_DIR_PAT_SHIFT 12
103#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
104
105#define PT32_DIR_PSE36_SIZE 4
106#define PT32_DIR_PSE36_SHIFT 13
d77c26fc
MD
107#define PT32_DIR_PSE36_MASK \
108 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
6aa8b732
AK
109
110
6aa8b732
AK
111#define PT_FIRST_AVAIL_BITS_SHIFT 9
112#define PT64_SECOND_AVAIL_BITS_SHIFT 52
113
6aa8b732
AK
114#define VALID_PAGE(x) ((x) != INVALID_PAGE)
115
116#define PT64_LEVEL_BITS 9
117
118#define PT64_LEVEL_SHIFT(level) \
d77c26fc 119 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
6aa8b732
AK
120
121#define PT64_LEVEL_MASK(level) \
122 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
123
124#define PT64_INDEX(address, level)\
125 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
126
127
128#define PT32_LEVEL_BITS 10
129
130#define PT32_LEVEL_SHIFT(level) \
d77c26fc 131 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
6aa8b732
AK
132
133#define PT32_LEVEL_MASK(level) \
134 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
135
136#define PT32_INDEX(address, level)\
137 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
138
139
27aba766 140#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
6aa8b732
AK
141#define PT64_DIR_BASE_ADDR_MASK \
142 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
143
144#define PT32_BASE_ADDR_MASK PAGE_MASK
145#define PT32_DIR_BASE_ADDR_MASK \
146 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
147
79539cec
AK
148#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
149 | PT64_NX_MASK)
6aa8b732
AK
150
151#define PFERR_PRESENT_MASK (1U << 0)
152#define PFERR_WRITE_MASK (1U << 1)
153#define PFERR_USER_MASK (1U << 2)
73b1087e 154#define PFERR_FETCH_MASK (1U << 4)
6aa8b732
AK
155
156#define PT64_ROOT_LEVEL 4
157#define PT32_ROOT_LEVEL 2
158#define PT32E_ROOT_LEVEL 3
159
160#define PT_DIRECTORY_LEVEL 2
161#define PT_PAGE_TABLE_LEVEL 1
162
cd4a4e53
AK
163#define RMAP_EXT 4
164
fe135d2c
AK
165#define ACC_EXEC_MASK 1
166#define ACC_WRITE_MASK PT_WRITABLE_MASK
167#define ACC_USER_MASK PT_USER_MASK
168#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
169
cd4a4e53
AK
170struct kvm_rmap_desc {
171 u64 *shadow_ptes[RMAP_EXT];
172 struct kvm_rmap_desc *more;
173};
174
b5a33a75
AK
175static struct kmem_cache *pte_chain_cache;
176static struct kmem_cache *rmap_desc_cache;
d3d25b04 177static struct kmem_cache *mmu_page_header_cache;
b5a33a75 178
c7addb90
AK
179static u64 __read_mostly shadow_trap_nonpresent_pte;
180static u64 __read_mostly shadow_notrap_nonpresent_pte;
181
182void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
183{
184 shadow_trap_nonpresent_pte = trap_pte;
185 shadow_notrap_nonpresent_pte = notrap_pte;
186}
187EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
188
6aa8b732
AK
189static int is_write_protection(struct kvm_vcpu *vcpu)
190{
ad312c7c 191 return vcpu->arch.cr0 & X86_CR0_WP;
6aa8b732
AK
192}
193
194static int is_cpuid_PSE36(void)
195{
196 return 1;
197}
198
73b1087e
AK
199static int is_nx(struct kvm_vcpu *vcpu)
200{
ad312c7c 201 return vcpu->arch.shadow_efer & EFER_NX;
73b1087e
AK
202}
203
6aa8b732
AK
204static int is_present_pte(unsigned long pte)
205{
206 return pte & PT_PRESENT_MASK;
207}
208
c7addb90
AK
209static int is_shadow_present_pte(u64 pte)
210{
c7addb90
AK
211 return pte != shadow_trap_nonpresent_pte
212 && pte != shadow_notrap_nonpresent_pte;
213}
214
05da4558
MT
215static int is_large_pte(u64 pte)
216{
217 return pte & PT_PAGE_SIZE_MASK;
218}
219
6aa8b732
AK
220static int is_writeble_pte(unsigned long pte)
221{
222 return pte & PT_WRITABLE_MASK;
223}
224
e3c5e7ec
AK
225static int is_dirty_pte(unsigned long pte)
226{
227 return pte & PT_DIRTY_MASK;
228}
229
cd4a4e53
AK
230static int is_rmap_pte(u64 pte)
231{
4b1a80fa 232 return is_shadow_present_pte(pte);
cd4a4e53
AK
233}
234
da928521
AK
235static gfn_t pse36_gfn_delta(u32 gpte)
236{
237 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
238
239 return (gpte & PT32_DIR_PSE36_MASK) << shift;
240}
241
e663ee64
AK
242static void set_shadow_pte(u64 *sptep, u64 spte)
243{
244#ifdef CONFIG_X86_64
245 set_64bit((unsigned long *)sptep, spte);
246#else
247 set_64bit((unsigned long long *)sptep, spte);
248#endif
249}
250
e2dec939 251static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
2e3e5882 252 struct kmem_cache *base_cache, int min)
714b93da
AK
253{
254 void *obj;
255
256 if (cache->nobjs >= min)
e2dec939 257 return 0;
714b93da 258 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
2e3e5882 259 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
714b93da 260 if (!obj)
e2dec939 261 return -ENOMEM;
714b93da
AK
262 cache->objects[cache->nobjs++] = obj;
263 }
e2dec939 264 return 0;
714b93da
AK
265}
266
267static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
268{
269 while (mc->nobjs)
270 kfree(mc->objects[--mc->nobjs]);
271}
272
c1158e63 273static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
2e3e5882 274 int min)
c1158e63
AK
275{
276 struct page *page;
277
278 if (cache->nobjs >= min)
279 return 0;
280 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
2e3e5882 281 page = alloc_page(GFP_KERNEL);
c1158e63
AK
282 if (!page)
283 return -ENOMEM;
284 set_page_private(page, 0);
285 cache->objects[cache->nobjs++] = page_address(page);
286 }
287 return 0;
288}
289
290static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
291{
292 while (mc->nobjs)
c4d198d5 293 free_page((unsigned long)mc->objects[--mc->nobjs]);
c1158e63
AK
294}
295
2e3e5882 296static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
714b93da 297{
e2dec939
AK
298 int r;
299
ad312c7c 300 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
2e3e5882 301 pte_chain_cache, 4);
e2dec939
AK
302 if (r)
303 goto out;
ad312c7c 304 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
2e3e5882 305 rmap_desc_cache, 1);
d3d25b04
AK
306 if (r)
307 goto out;
ad312c7c 308 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
d3d25b04
AK
309 if (r)
310 goto out;
ad312c7c 311 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
2e3e5882 312 mmu_page_header_cache, 4);
e2dec939
AK
313out:
314 return r;
714b93da
AK
315}
316
317static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
318{
ad312c7c
ZX
319 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
320 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
321 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
322 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
714b93da
AK
323}
324
325static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
326 size_t size)
327{
328 void *p;
329
330 BUG_ON(!mc->nobjs);
331 p = mc->objects[--mc->nobjs];
332 memset(p, 0, size);
333 return p;
334}
335
714b93da
AK
336static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
337{
ad312c7c 338 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
714b93da
AK
339 sizeof(struct kvm_pte_chain));
340}
341
90cb0529 342static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
714b93da 343{
90cb0529 344 kfree(pc);
714b93da
AK
345}
346
347static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
348{
ad312c7c 349 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
714b93da
AK
350 sizeof(struct kvm_rmap_desc));
351}
352
90cb0529 353static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
714b93da 354{
90cb0529 355 kfree(rd);
714b93da
AK
356}
357
05da4558
MT
358/*
359 * Return the pointer to the largepage write count for a given
360 * gfn, handling slots that are not large page aligned.
361 */
362static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
363{
364 unsigned long idx;
365
366 idx = (gfn / KVM_PAGES_PER_HPAGE) -
367 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
368 return &slot->lpage_info[idx].write_count;
369}
370
371static void account_shadowed(struct kvm *kvm, gfn_t gfn)
372{
373 int *write_count;
374
375 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
376 *write_count += 1;
377 WARN_ON(*write_count > KVM_PAGES_PER_HPAGE);
378}
379
380static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
381{
382 int *write_count;
383
384 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
385 *write_count -= 1;
386 WARN_ON(*write_count < 0);
387}
388
389static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
390{
391 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
392 int *largepage_idx;
393
394 if (slot) {
395 largepage_idx = slot_largepage_idx(gfn, slot);
396 return *largepage_idx;
397 }
398
399 return 1;
400}
401
402static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
403{
404 struct vm_area_struct *vma;
405 unsigned long addr;
406
407 addr = gfn_to_hva(kvm, gfn);
408 if (kvm_is_error_hva(addr))
409 return 0;
410
411 vma = find_vma(current->mm, addr);
412 if (vma && is_vm_hugetlb_page(vma))
413 return 1;
414
415 return 0;
416}
417
418static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
419{
420 struct kvm_memory_slot *slot;
421
422 if (has_wrprotected_page(vcpu->kvm, large_gfn))
423 return 0;
424
425 if (!host_largepage_backed(vcpu->kvm, large_gfn))
426 return 0;
427
428 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
429 if (slot && slot->dirty_bitmap)
430 return 0;
431
432 return 1;
433}
434
290fc38d
IE
435/*
436 * Take gfn and return the reverse mapping to it.
437 * Note: gfn must be unaliased before this function get called
438 */
439
05da4558 440static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
290fc38d
IE
441{
442 struct kvm_memory_slot *slot;
05da4558 443 unsigned long idx;
290fc38d
IE
444
445 slot = gfn_to_memslot(kvm, gfn);
05da4558
MT
446 if (!lpage)
447 return &slot->rmap[gfn - slot->base_gfn];
448
449 idx = (gfn / KVM_PAGES_PER_HPAGE) -
450 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
451
452 return &slot->lpage_info[idx].rmap_pde;
290fc38d
IE
453}
454
cd4a4e53
AK
455/*
456 * Reverse mapping data structures:
457 *
290fc38d
IE
458 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
459 * that points to page_address(page).
cd4a4e53 460 *
290fc38d
IE
461 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
462 * containing more mappings.
cd4a4e53 463 */
05da4558 464static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
cd4a4e53 465{
4db35314 466 struct kvm_mmu_page *sp;
cd4a4e53 467 struct kvm_rmap_desc *desc;
290fc38d 468 unsigned long *rmapp;
cd4a4e53
AK
469 int i;
470
471 if (!is_rmap_pte(*spte))
472 return;
290fc38d 473 gfn = unalias_gfn(vcpu->kvm, gfn);
4db35314
AK
474 sp = page_header(__pa(spte));
475 sp->gfns[spte - sp->spt] = gfn;
05da4558 476 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
290fc38d 477 if (!*rmapp) {
cd4a4e53 478 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
290fc38d
IE
479 *rmapp = (unsigned long)spte;
480 } else if (!(*rmapp & 1)) {
cd4a4e53 481 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
714b93da 482 desc = mmu_alloc_rmap_desc(vcpu);
290fc38d 483 desc->shadow_ptes[0] = (u64 *)*rmapp;
cd4a4e53 484 desc->shadow_ptes[1] = spte;
290fc38d 485 *rmapp = (unsigned long)desc | 1;
cd4a4e53
AK
486 } else {
487 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
290fc38d 488 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
cd4a4e53
AK
489 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
490 desc = desc->more;
491 if (desc->shadow_ptes[RMAP_EXT-1]) {
714b93da 492 desc->more = mmu_alloc_rmap_desc(vcpu);
cd4a4e53
AK
493 desc = desc->more;
494 }
495 for (i = 0; desc->shadow_ptes[i]; ++i)
496 ;
497 desc->shadow_ptes[i] = spte;
498 }
499}
500
290fc38d 501static void rmap_desc_remove_entry(unsigned long *rmapp,
cd4a4e53
AK
502 struct kvm_rmap_desc *desc,
503 int i,
504 struct kvm_rmap_desc *prev_desc)
505{
506 int j;
507
508 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
509 ;
510 desc->shadow_ptes[i] = desc->shadow_ptes[j];
11718b4d 511 desc->shadow_ptes[j] = NULL;
cd4a4e53
AK
512 if (j != 0)
513 return;
514 if (!prev_desc && !desc->more)
290fc38d 515 *rmapp = (unsigned long)desc->shadow_ptes[0];
cd4a4e53
AK
516 else
517 if (prev_desc)
518 prev_desc->more = desc->more;
519 else
290fc38d 520 *rmapp = (unsigned long)desc->more | 1;
90cb0529 521 mmu_free_rmap_desc(desc);
cd4a4e53
AK
522}
523
290fc38d 524static void rmap_remove(struct kvm *kvm, u64 *spte)
cd4a4e53 525{
cd4a4e53
AK
526 struct kvm_rmap_desc *desc;
527 struct kvm_rmap_desc *prev_desc;
4db35314 528 struct kvm_mmu_page *sp;
76c35c6e 529 struct page *page;
290fc38d 530 unsigned long *rmapp;
cd4a4e53
AK
531 int i;
532
533 if (!is_rmap_pte(*spte))
534 return;
4db35314 535 sp = page_header(__pa(spte));
76c35c6e 536 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
448353ca 537 mark_page_accessed(page);
b4231d61 538 if (is_writeble_pte(*spte))
76c35c6e 539 kvm_release_page_dirty(page);
b4231d61 540 else
76c35c6e 541 kvm_release_page_clean(page);
05da4558 542 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
290fc38d 543 if (!*rmapp) {
cd4a4e53
AK
544 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
545 BUG();
290fc38d 546 } else if (!(*rmapp & 1)) {
cd4a4e53 547 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
290fc38d 548 if ((u64 *)*rmapp != spte) {
cd4a4e53
AK
549 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
550 spte, *spte);
551 BUG();
552 }
290fc38d 553 *rmapp = 0;
cd4a4e53
AK
554 } else {
555 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
290fc38d 556 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
cd4a4e53
AK
557 prev_desc = NULL;
558 while (desc) {
559 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
560 if (desc->shadow_ptes[i] == spte) {
290fc38d 561 rmap_desc_remove_entry(rmapp,
714b93da 562 desc, i,
cd4a4e53
AK
563 prev_desc);
564 return;
565 }
566 prev_desc = desc;
567 desc = desc->more;
568 }
569 BUG();
570 }
571}
572
98348e95 573static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
374cbac0 574{
374cbac0 575 struct kvm_rmap_desc *desc;
98348e95
IE
576 struct kvm_rmap_desc *prev_desc;
577 u64 *prev_spte;
578 int i;
579
580 if (!*rmapp)
581 return NULL;
582 else if (!(*rmapp & 1)) {
583 if (!spte)
584 return (u64 *)*rmapp;
585 return NULL;
586 }
587 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
588 prev_desc = NULL;
589 prev_spte = NULL;
590 while (desc) {
591 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
592 if (prev_spte == spte)
593 return desc->shadow_ptes[i];
594 prev_spte = desc->shadow_ptes[i];
595 }
596 desc = desc->more;
597 }
598 return NULL;
599}
600
601static void rmap_write_protect(struct kvm *kvm, u64 gfn)
602{
290fc38d 603 unsigned long *rmapp;
374cbac0 604 u64 *spte;
caa5b8a5 605 int write_protected = 0;
374cbac0 606
4a4c9924 607 gfn = unalias_gfn(kvm, gfn);
05da4558 608 rmapp = gfn_to_rmap(kvm, gfn, 0);
374cbac0 609
98348e95
IE
610 spte = rmap_next(kvm, rmapp, NULL);
611 while (spte) {
374cbac0 612 BUG_ON(!spte);
374cbac0 613 BUG_ON(!(*spte & PT_PRESENT_MASK));
374cbac0 614 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
caa5b8a5 615 if (is_writeble_pte(*spte)) {
9647c14c 616 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
caa5b8a5
ED
617 write_protected = 1;
618 }
9647c14c 619 spte = rmap_next(kvm, rmapp, spte);
374cbac0 620 }
05da4558
MT
621 /* check for huge page mappings */
622 rmapp = gfn_to_rmap(kvm, gfn, 1);
623 spte = rmap_next(kvm, rmapp, NULL);
624 while (spte) {
625 BUG_ON(!spte);
626 BUG_ON(!(*spte & PT_PRESENT_MASK));
627 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
628 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
629 if (is_writeble_pte(*spte)) {
630 rmap_remove(kvm, spte);
631 --kvm->stat.lpages;
632 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
633 write_protected = 1;
634 }
635 spte = rmap_next(kvm, rmapp, spte);
636 }
637
caa5b8a5
ED
638 if (write_protected)
639 kvm_flush_remote_tlbs(kvm);
05da4558
MT
640
641 account_shadowed(kvm, gfn);
374cbac0
AK
642}
643
d6c69ee9 644#ifdef MMU_DEBUG
47ad8e68 645static int is_empty_shadow_page(u64 *spt)
6aa8b732 646{
139bdb2d
AK
647 u64 *pos;
648 u64 *end;
649
47ad8e68 650 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
d196e343 651 if (*pos != shadow_trap_nonpresent_pte) {
139bdb2d
AK
652 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
653 pos, *pos);
6aa8b732 654 return 0;
139bdb2d 655 }
6aa8b732
AK
656 return 1;
657}
d6c69ee9 658#endif
6aa8b732 659
4db35314 660static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
260746c0 661{
4db35314
AK
662 ASSERT(is_empty_shadow_page(sp->spt));
663 list_del(&sp->link);
664 __free_page(virt_to_page(sp->spt));
665 __free_page(virt_to_page(sp->gfns));
666 kfree(sp);
f05e70ac 667 ++kvm->arch.n_free_mmu_pages;
260746c0
AK
668}
669
cea0f0e7
AK
670static unsigned kvm_page_table_hashfn(gfn_t gfn)
671{
1ae0a13d 672 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
cea0f0e7
AK
673}
674
25c0de2c
AK
675static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
676 u64 *parent_pte)
6aa8b732 677{
4db35314 678 struct kvm_mmu_page *sp;
6aa8b732 679
ad312c7c
ZX
680 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
681 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
682 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
4db35314 683 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
f05e70ac 684 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
4db35314
AK
685 ASSERT(is_empty_shadow_page(sp->spt));
686 sp->slot_bitmap = 0;
687 sp->multimapped = 0;
688 sp->parent_pte = parent_pte;
f05e70ac 689 --vcpu->kvm->arch.n_free_mmu_pages;
4db35314 690 return sp;
6aa8b732
AK
691}
692
714b93da 693static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
4db35314 694 struct kvm_mmu_page *sp, u64 *parent_pte)
cea0f0e7
AK
695{
696 struct kvm_pte_chain *pte_chain;
697 struct hlist_node *node;
698 int i;
699
700 if (!parent_pte)
701 return;
4db35314
AK
702 if (!sp->multimapped) {
703 u64 *old = sp->parent_pte;
cea0f0e7
AK
704
705 if (!old) {
4db35314 706 sp->parent_pte = parent_pte;
cea0f0e7
AK
707 return;
708 }
4db35314 709 sp->multimapped = 1;
714b93da 710 pte_chain = mmu_alloc_pte_chain(vcpu);
4db35314
AK
711 INIT_HLIST_HEAD(&sp->parent_ptes);
712 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
cea0f0e7
AK
713 pte_chain->parent_ptes[0] = old;
714 }
4db35314 715 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
cea0f0e7
AK
716 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
717 continue;
718 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
719 if (!pte_chain->parent_ptes[i]) {
720 pte_chain->parent_ptes[i] = parent_pte;
721 return;
722 }
723 }
714b93da 724 pte_chain = mmu_alloc_pte_chain(vcpu);
cea0f0e7 725 BUG_ON(!pte_chain);
4db35314 726 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
cea0f0e7
AK
727 pte_chain->parent_ptes[0] = parent_pte;
728}
729
4db35314 730static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
cea0f0e7
AK
731 u64 *parent_pte)
732{
733 struct kvm_pte_chain *pte_chain;
734 struct hlist_node *node;
735 int i;
736
4db35314
AK
737 if (!sp->multimapped) {
738 BUG_ON(sp->parent_pte != parent_pte);
739 sp->parent_pte = NULL;
cea0f0e7
AK
740 return;
741 }
4db35314 742 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
cea0f0e7
AK
743 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
744 if (!pte_chain->parent_ptes[i])
745 break;
746 if (pte_chain->parent_ptes[i] != parent_pte)
747 continue;
697fe2e2
AK
748 while (i + 1 < NR_PTE_CHAIN_ENTRIES
749 && pte_chain->parent_ptes[i + 1]) {
cea0f0e7
AK
750 pte_chain->parent_ptes[i]
751 = pte_chain->parent_ptes[i + 1];
752 ++i;
753 }
754 pte_chain->parent_ptes[i] = NULL;
697fe2e2
AK
755 if (i == 0) {
756 hlist_del(&pte_chain->link);
90cb0529 757 mmu_free_pte_chain(pte_chain);
4db35314
AK
758 if (hlist_empty(&sp->parent_ptes)) {
759 sp->multimapped = 0;
760 sp->parent_pte = NULL;
697fe2e2
AK
761 }
762 }
cea0f0e7
AK
763 return;
764 }
765 BUG();
766}
767
4db35314 768static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
cea0f0e7
AK
769{
770 unsigned index;
771 struct hlist_head *bucket;
4db35314 772 struct kvm_mmu_page *sp;
cea0f0e7
AK
773 struct hlist_node *node;
774
775 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
1ae0a13d 776 index = kvm_page_table_hashfn(gfn);
f05e70ac 777 bucket = &kvm->arch.mmu_page_hash[index];
4db35314 778 hlist_for_each_entry(sp, node, bucket, hash_link)
2e53d63a
MT
779 if (sp->gfn == gfn && !sp->role.metaphysical
780 && !sp->role.invalid) {
cea0f0e7 781 pgprintk("%s: found role %x\n",
4db35314
AK
782 __FUNCTION__, sp->role.word);
783 return sp;
cea0f0e7
AK
784 }
785 return NULL;
786}
787
788static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
789 gfn_t gfn,
790 gva_t gaddr,
791 unsigned level,
792 int metaphysical,
41074d07 793 unsigned access,
f7d9c7b7 794 u64 *parent_pte)
cea0f0e7
AK
795{
796 union kvm_mmu_page_role role;
797 unsigned index;
798 unsigned quadrant;
799 struct hlist_head *bucket;
4db35314 800 struct kvm_mmu_page *sp;
cea0f0e7
AK
801 struct hlist_node *node;
802
803 role.word = 0;
ad312c7c 804 role.glevels = vcpu->arch.mmu.root_level;
cea0f0e7
AK
805 role.level = level;
806 role.metaphysical = metaphysical;
41074d07 807 role.access = access;
ad312c7c 808 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
cea0f0e7
AK
809 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
810 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
811 role.quadrant = quadrant;
812 }
813 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
814 gfn, role.word);
1ae0a13d 815 index = kvm_page_table_hashfn(gfn);
f05e70ac 816 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
4db35314
AK
817 hlist_for_each_entry(sp, node, bucket, hash_link)
818 if (sp->gfn == gfn && sp->role.word == role.word) {
819 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
cea0f0e7 820 pgprintk("%s: found\n", __FUNCTION__);
4db35314 821 return sp;
cea0f0e7 822 }
dfc5aa00 823 ++vcpu->kvm->stat.mmu_cache_miss;
4db35314
AK
824 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
825 if (!sp)
826 return sp;
cea0f0e7 827 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
4db35314
AK
828 sp->gfn = gfn;
829 sp->role = role;
830 hlist_add_head(&sp->hash_link, bucket);
ad312c7c 831 vcpu->arch.mmu.prefetch_page(vcpu, sp);
374cbac0 832 if (!metaphysical)
4a4c9924 833 rmap_write_protect(vcpu->kvm, gfn);
4db35314 834 return sp;
cea0f0e7
AK
835}
836
90cb0529 837static void kvm_mmu_page_unlink_children(struct kvm *kvm,
4db35314 838 struct kvm_mmu_page *sp)
a436036b 839{
697fe2e2
AK
840 unsigned i;
841 u64 *pt;
842 u64 ent;
843
4db35314 844 pt = sp->spt;
697fe2e2 845
4db35314 846 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
697fe2e2 847 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
c7addb90 848 if (is_shadow_present_pte(pt[i]))
290fc38d 849 rmap_remove(kvm, &pt[i]);
c7addb90 850 pt[i] = shadow_trap_nonpresent_pte;
697fe2e2 851 }
90cb0529 852 kvm_flush_remote_tlbs(kvm);
697fe2e2
AK
853 return;
854 }
855
856 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
857 ent = pt[i];
858
05da4558
MT
859 if (is_shadow_present_pte(ent)) {
860 if (!is_large_pte(ent)) {
861 ent &= PT64_BASE_ADDR_MASK;
862 mmu_page_remove_parent_pte(page_header(ent),
863 &pt[i]);
864 } else {
865 --kvm->stat.lpages;
866 rmap_remove(kvm, &pt[i]);
867 }
868 }
c7addb90 869 pt[i] = shadow_trap_nonpresent_pte;
697fe2e2 870 }
90cb0529 871 kvm_flush_remote_tlbs(kvm);
a436036b
AK
872}
873
4db35314 874static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
cea0f0e7 875{
4db35314 876 mmu_page_remove_parent_pte(sp, parent_pte);
a436036b
AK
877}
878
12b7d28f
AK
879static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
880{
881 int i;
882
883 for (i = 0; i < KVM_MAX_VCPUS; ++i)
884 if (kvm->vcpus[i])
ad312c7c 885 kvm->vcpus[i]->arch.last_pte_updated = NULL;
12b7d28f
AK
886}
887
4db35314 888static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
a436036b
AK
889{
890 u64 *parent_pte;
891
4cee5764 892 ++kvm->stat.mmu_shadow_zapped;
4db35314
AK
893 while (sp->multimapped || sp->parent_pte) {
894 if (!sp->multimapped)
895 parent_pte = sp->parent_pte;
a436036b
AK
896 else {
897 struct kvm_pte_chain *chain;
898
4db35314 899 chain = container_of(sp->parent_ptes.first,
a436036b
AK
900 struct kvm_pte_chain, link);
901 parent_pte = chain->parent_ptes[0];
902 }
697fe2e2 903 BUG_ON(!parent_pte);
4db35314 904 kvm_mmu_put_page(sp, parent_pte);
c7addb90 905 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
a436036b 906 }
4db35314
AK
907 kvm_mmu_page_unlink_children(kvm, sp);
908 if (!sp->root_count) {
05da4558
MT
909 if (!sp->role.metaphysical)
910 unaccount_shadowed(kvm, sp->gfn);
4db35314
AK
911 hlist_del(&sp->hash_link);
912 kvm_mmu_free_page(kvm, sp);
2e53d63a 913 } else {
f05e70ac 914 list_move(&sp->link, &kvm->arch.active_mmu_pages);
2e53d63a
MT
915 sp->role.invalid = 1;
916 kvm_reload_remote_mmus(kvm);
917 }
12b7d28f 918 kvm_mmu_reset_last_pte_updated(kvm);
a436036b
AK
919}
920
82ce2c96
IE
921/*
922 * Changing the number of mmu pages allocated to the vm
923 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
924 */
925void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
926{
927 /*
928 * If we set the number of mmu pages to be smaller be than the
929 * number of actived pages , we must to free some mmu pages before we
930 * change the value
931 */
932
f05e70ac 933 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
82ce2c96 934 kvm_nr_mmu_pages) {
f05e70ac
ZX
935 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
936 - kvm->arch.n_free_mmu_pages;
82ce2c96
IE
937
938 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
939 struct kvm_mmu_page *page;
940
f05e70ac 941 page = container_of(kvm->arch.active_mmu_pages.prev,
82ce2c96
IE
942 struct kvm_mmu_page, link);
943 kvm_mmu_zap_page(kvm, page);
944 n_used_mmu_pages--;
945 }
f05e70ac 946 kvm->arch.n_free_mmu_pages = 0;
82ce2c96
IE
947 }
948 else
f05e70ac
ZX
949 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
950 - kvm->arch.n_alloc_mmu_pages;
82ce2c96 951
f05e70ac 952 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
82ce2c96
IE
953}
954
f67a46f4 955static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
a436036b
AK
956{
957 unsigned index;
958 struct hlist_head *bucket;
4db35314 959 struct kvm_mmu_page *sp;
a436036b
AK
960 struct hlist_node *node, *n;
961 int r;
962
963 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
964 r = 0;
1ae0a13d 965 index = kvm_page_table_hashfn(gfn);
f05e70ac 966 bucket = &kvm->arch.mmu_page_hash[index];
4db35314
AK
967 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
968 if (sp->gfn == gfn && !sp->role.metaphysical) {
697fe2e2 969 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
4db35314
AK
970 sp->role.word);
971 kvm_mmu_zap_page(kvm, sp);
a436036b
AK
972 r = 1;
973 }
974 return r;
cea0f0e7
AK
975}
976
f67a46f4 977static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
97a0a01e 978{
4db35314 979 struct kvm_mmu_page *sp;
97a0a01e 980
4db35314
AK
981 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
982 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
983 kvm_mmu_zap_page(kvm, sp);
97a0a01e
AK
984 }
985}
986
38c335f1 987static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
6aa8b732 988{
38c335f1 989 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
4db35314 990 struct kvm_mmu_page *sp = page_header(__pa(pte));
6aa8b732 991
4db35314 992 __set_bit(slot, &sp->slot_bitmap);
6aa8b732
AK
993}
994
039576c0
AK
995struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
996{
72dc67a6
IE
997 struct page *page;
998
ad312c7c 999 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
039576c0
AK
1000
1001 if (gpa == UNMAPPED_GVA)
1002 return NULL;
72dc67a6
IE
1003
1004 down_read(&current->mm->mmap_sem);
1005 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1006 up_read(&current->mm->mmap_sem);
1007
1008 return page;
039576c0
AK
1009}
1010
1c4f1fd6
AK
1011static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1012 unsigned pt_access, unsigned pte_access,
1013 int user_fault, int write_fault, int dirty,
05da4558
MT
1014 int *ptwrite, int largepage, gfn_t gfn,
1015 struct page *page)
1c4f1fd6
AK
1016{
1017 u64 spte;
15aaa819 1018 int was_rmapped = 0;
75e68e60 1019 int was_writeble = is_writeble_pte(*shadow_pte);
15aaa819 1020 hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1c4f1fd6 1021
bc750ba8 1022 pgprintk("%s: spte %llx access %x write_fault %d"
1c4f1fd6 1023 " user_fault %d gfn %lx\n",
bc750ba8 1024 __FUNCTION__, *shadow_pte, pt_access,
1c4f1fd6
AK
1025 write_fault, user_fault, gfn);
1026
15aaa819 1027 if (is_rmap_pte(*shadow_pte)) {
05da4558
MT
1028 /*
1029 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1030 * the parent of the now unreachable PTE.
1031 */
1032 if (largepage && !is_large_pte(*shadow_pte)) {
1033 struct kvm_mmu_page *child;
1034 u64 pte = *shadow_pte;
1035
1036 child = page_header(pte & PT64_BASE_ADDR_MASK);
1037 mmu_page_remove_parent_pte(child, shadow_pte);
1038 } else if (host_pfn != page_to_pfn(page)) {
15aaa819
MT
1039 pgprintk("hfn old %lx new %lx\n",
1040 host_pfn, page_to_pfn(page));
1041 rmap_remove(vcpu->kvm, shadow_pte);
05da4558
MT
1042 } else {
1043 if (largepage)
1044 was_rmapped = is_large_pte(*shadow_pte);
1045 else
1046 was_rmapped = 1;
15aaa819 1047 }
15aaa819
MT
1048 }
1049
05da4558 1050
1c4f1fd6
AK
1051 /*
1052 * We don't set the accessed bit, since we sometimes want to see
1053 * whether the guest actually used the pte (in order to detect
1054 * demand paging).
1055 */
1056 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
1057 if (!dirty)
1058 pte_access &= ~ACC_WRITE_MASK;
1059 if (!(pte_access & ACC_EXEC_MASK))
1060 spte |= PT64_NX_MASK;
1061
1c4f1fd6
AK
1062 spte |= PT_PRESENT_MASK;
1063 if (pte_access & ACC_USER_MASK)
1064 spte |= PT_USER_MASK;
05da4558
MT
1065 if (largepage)
1066 spte |= PT_PAGE_SIZE_MASK;
1c4f1fd6 1067
1c4f1fd6
AK
1068 spte |= page_to_phys(page);
1069
1070 if ((pte_access & ACC_WRITE_MASK)
1071 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1072 struct kvm_mmu_page *shadow;
1073
1074 spte |= PT_WRITABLE_MASK;
1075 if (user_fault) {
1076 mmu_unshadow(vcpu->kvm, gfn);
1077 goto unshadowed;
1078 }
1079
1080 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
05da4558
MT
1081 if (shadow ||
1082 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
1c4f1fd6
AK
1083 pgprintk("%s: found shadow page for %lx, marking ro\n",
1084 __FUNCTION__, gfn);
1085 pte_access &= ~ACC_WRITE_MASK;
1086 if (is_writeble_pte(spte)) {
1087 spte &= ~PT_WRITABLE_MASK;
1088 kvm_x86_ops->tlb_flush(vcpu);
1089 }
1090 if (write_fault)
1091 *ptwrite = 1;
1092 }
1093 }
1094
1095unshadowed:
1096
1097 if (pte_access & ACC_WRITE_MASK)
1098 mark_page_dirty(vcpu->kvm, gfn);
1099
1100 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
05da4558
MT
1101 pgprintk("instantiating %s PTE (%s) at %d (%llx) addr %llx\n",
1102 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1103 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
1c4f1fd6 1104 set_shadow_pte(shadow_pte, spte);
05da4558
MT
1105 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1106 && (spte & PT_PRESENT_MASK))
1107 ++vcpu->kvm->stat.lpages;
1108
1c4f1fd6
AK
1109 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1110 if (!was_rmapped) {
05da4558 1111 rmap_add(vcpu, shadow_pte, gfn, largepage);
1c4f1fd6
AK
1112 if (!is_rmap_pte(*shadow_pte))
1113 kvm_release_page_clean(page);
75e68e60
IE
1114 } else {
1115 if (was_writeble)
1116 kvm_release_page_dirty(page);
1117 else
1118 kvm_release_page_clean(page);
1c4f1fd6 1119 }
1c4f1fd6 1120 if (!ptwrite || !*ptwrite)
ad312c7c 1121 vcpu->arch.last_pte_updated = shadow_pte;
1c4f1fd6
AK
1122}
1123
6aa8b732
AK
1124static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1125{
1126}
1127
4d9976bb 1128static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
05da4558
MT
1129 int largepage, gfn_t gfn, struct page *page,
1130 int level)
6aa8b732 1131{
ad312c7c 1132 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
e833240f 1133 int pt_write = 0;
6aa8b732
AK
1134
1135 for (; ; level--) {
1136 u32 index = PT64_INDEX(v, level);
1137 u64 *table;
1138
1139 ASSERT(VALID_PAGE(table_addr));
1140 table = __va(table_addr);
1141
1142 if (level == 1) {
e833240f 1143 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
05da4558
MT
1144 0, write, 1, &pt_write, 0, gfn, page);
1145 return pt_write;
1146 }
1147
1148 if (largepage && level == 2) {
1149 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1150 0, write, 1, &pt_write, 1, gfn, page);
d196e343 1151 return pt_write;
6aa8b732
AK
1152 }
1153
c7addb90 1154 if (table[index] == shadow_trap_nonpresent_pte) {
25c0de2c 1155 struct kvm_mmu_page *new_table;
cea0f0e7 1156 gfn_t pseudo_gfn;
6aa8b732 1157
cea0f0e7
AK
1158 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1159 >> PAGE_SHIFT;
1160 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1161 v, level - 1,
f7d9c7b7 1162 1, ACC_ALL, &table[index]);
25c0de2c 1163 if (!new_table) {
6aa8b732 1164 pgprintk("nonpaging_map: ENOMEM\n");
d7824fff 1165 kvm_release_page_clean(page);
6aa8b732
AK
1166 return -ENOMEM;
1167 }
1168
47ad8e68 1169 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
25c0de2c 1170 | PT_WRITABLE_MASK | PT_USER_MASK;
6aa8b732
AK
1171 }
1172 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1173 }
1174}
1175
10589a46
MT
1176static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1177{
1178 int r;
05da4558 1179 int largepage = 0;
10589a46 1180
aaee2c94
MT
1181 struct page *page;
1182
72dc67a6
IE
1183 down_read(&vcpu->kvm->slots_lock);
1184
aaee2c94 1185 down_read(&current->mm->mmap_sem);
05da4558
MT
1186 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1187 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1188 largepage = 1;
1189 }
1190
aaee2c94 1191 page = gfn_to_page(vcpu->kvm, gfn);
72dc67a6 1192 up_read(&current->mm->mmap_sem);
aaee2c94 1193
d196e343
AK
1194 /* mmio */
1195 if (is_error_page(page)) {
1196 kvm_release_page_clean(page);
1197 up_read(&vcpu->kvm->slots_lock);
1198 return 1;
1199 }
1200
aaee2c94 1201 spin_lock(&vcpu->kvm->mmu_lock);
eb787d10 1202 kvm_mmu_free_some_pages(vcpu);
05da4558
MT
1203 r = __direct_map(vcpu, v, write, largepage, gfn, page,
1204 PT32E_ROOT_LEVEL);
aaee2c94
MT
1205 spin_unlock(&vcpu->kvm->mmu_lock);
1206
72dc67a6 1207 up_read(&vcpu->kvm->slots_lock);
aaee2c94 1208
10589a46
MT
1209 return r;
1210}
1211
1212
c7addb90
AK
1213static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1214 struct kvm_mmu_page *sp)
1215{
1216 int i;
1217
1218 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1219 sp->spt[i] = shadow_trap_nonpresent_pte;
1220}
1221
17ac10ad
AK
1222static void mmu_free_roots(struct kvm_vcpu *vcpu)
1223{
1224 int i;
4db35314 1225 struct kvm_mmu_page *sp;
17ac10ad 1226
ad312c7c 1227 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
7b53aa56 1228 return;
aaee2c94 1229 spin_lock(&vcpu->kvm->mmu_lock);
17ac10ad 1230#ifdef CONFIG_X86_64
ad312c7c
ZX
1231 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1232 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad 1233
4db35314
AK
1234 sp = page_header(root);
1235 --sp->root_count;
2e53d63a
MT
1236 if (!sp->root_count && sp->role.invalid)
1237 kvm_mmu_zap_page(vcpu->kvm, sp);
ad312c7c 1238 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
aaee2c94 1239 spin_unlock(&vcpu->kvm->mmu_lock);
17ac10ad
AK
1240 return;
1241 }
1242#endif
1243 for (i = 0; i < 4; ++i) {
ad312c7c 1244 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad 1245
417726a3 1246 if (root) {
417726a3 1247 root &= PT64_BASE_ADDR_MASK;
4db35314
AK
1248 sp = page_header(root);
1249 --sp->root_count;
2e53d63a
MT
1250 if (!sp->root_count && sp->role.invalid)
1251 kvm_mmu_zap_page(vcpu->kvm, sp);
417726a3 1252 }
ad312c7c 1253 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 1254 }
aaee2c94 1255 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 1256 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
17ac10ad
AK
1257}
1258
1259static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1260{
1261 int i;
cea0f0e7 1262 gfn_t root_gfn;
4db35314 1263 struct kvm_mmu_page *sp;
fb72d167 1264 int metaphysical = 0;
3bb65a22 1265
ad312c7c 1266 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
17ac10ad
AK
1267
1268#ifdef CONFIG_X86_64
ad312c7c
ZX
1269 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1270 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad
AK
1271
1272 ASSERT(!VALID_PAGE(root));
fb72d167
JR
1273 if (tdp_enabled)
1274 metaphysical = 1;
4db35314 1275 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
fb72d167
JR
1276 PT64_ROOT_LEVEL, metaphysical,
1277 ACC_ALL, NULL);
4db35314
AK
1278 root = __pa(sp->spt);
1279 ++sp->root_count;
ad312c7c 1280 vcpu->arch.mmu.root_hpa = root;
17ac10ad
AK
1281 return;
1282 }
1283#endif
fb72d167
JR
1284 metaphysical = !is_paging(vcpu);
1285 if (tdp_enabled)
1286 metaphysical = 1;
17ac10ad 1287 for (i = 0; i < 4; ++i) {
ad312c7c 1288 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad
AK
1289
1290 ASSERT(!VALID_PAGE(root));
ad312c7c
ZX
1291 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1292 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1293 vcpu->arch.mmu.pae_root[i] = 0;
417726a3
AK
1294 continue;
1295 }
ad312c7c
ZX
1296 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1297 } else if (vcpu->arch.mmu.root_level == 0)
cea0f0e7 1298 root_gfn = 0;
4db35314 1299 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
fb72d167 1300 PT32_ROOT_LEVEL, metaphysical,
f7d9c7b7 1301 ACC_ALL, NULL);
4db35314
AK
1302 root = __pa(sp->spt);
1303 ++sp->root_count;
ad312c7c 1304 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
17ac10ad 1305 }
ad312c7c 1306 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
17ac10ad
AK
1307}
1308
6aa8b732
AK
1309static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1310{
1311 return vaddr;
1312}
1313
1314static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
3f3e7124 1315 u32 error_code)
6aa8b732 1316{
e833240f 1317 gfn_t gfn;
e2dec939 1318 int r;
6aa8b732 1319
e833240f 1320 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
e2dec939
AK
1321 r = mmu_topup_memory_caches(vcpu);
1322 if (r)
1323 return r;
714b93da 1324
6aa8b732 1325 ASSERT(vcpu);
ad312c7c 1326 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 1327
e833240f 1328 gfn = gva >> PAGE_SHIFT;
6aa8b732 1329
e833240f
AK
1330 return nonpaging_map(vcpu, gva & PAGE_MASK,
1331 error_code & PFERR_WRITE_MASK, gfn);
6aa8b732
AK
1332}
1333
fb72d167
JR
1334static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1335 u32 error_code)
1336{
1337 struct page *page;
1338 int r;
05da4558
MT
1339 int largepage = 0;
1340 gfn_t gfn = gpa >> PAGE_SHIFT;
fb72d167
JR
1341
1342 ASSERT(vcpu);
1343 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1344
1345 r = mmu_topup_memory_caches(vcpu);
1346 if (r)
1347 return r;
1348
1349 down_read(&current->mm->mmap_sem);
05da4558
MT
1350 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1351 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1352 largepage = 1;
1353 }
1354 page = gfn_to_page(vcpu->kvm, gfn);
fb72d167
JR
1355 if (is_error_page(page)) {
1356 kvm_release_page_clean(page);
1357 up_read(&current->mm->mmap_sem);
1358 return 1;
1359 }
1360 spin_lock(&vcpu->kvm->mmu_lock);
1361 kvm_mmu_free_some_pages(vcpu);
1362 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
05da4558 1363 largepage, gfn, page, TDP_ROOT_LEVEL);
fb72d167
JR
1364 spin_unlock(&vcpu->kvm->mmu_lock);
1365 up_read(&current->mm->mmap_sem);
1366
1367 return r;
1368}
1369
6aa8b732
AK
1370static void nonpaging_free(struct kvm_vcpu *vcpu)
1371{
17ac10ad 1372 mmu_free_roots(vcpu);
6aa8b732
AK
1373}
1374
1375static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1376{
ad312c7c 1377 struct kvm_mmu *context = &vcpu->arch.mmu;
6aa8b732
AK
1378
1379 context->new_cr3 = nonpaging_new_cr3;
1380 context->page_fault = nonpaging_page_fault;
6aa8b732
AK
1381 context->gva_to_gpa = nonpaging_gva_to_gpa;
1382 context->free = nonpaging_free;
c7addb90 1383 context->prefetch_page = nonpaging_prefetch_page;
cea0f0e7 1384 context->root_level = 0;
6aa8b732 1385 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 1386 context->root_hpa = INVALID_PAGE;
6aa8b732
AK
1387 return 0;
1388}
1389
d835dfec 1390void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
6aa8b732 1391{
1165f5fe 1392 ++vcpu->stat.tlb_flush;
cbdd1bea 1393 kvm_x86_ops->tlb_flush(vcpu);
6aa8b732
AK
1394}
1395
1396static void paging_new_cr3(struct kvm_vcpu *vcpu)
1397{
24993d53 1398 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->arch.cr3);
cea0f0e7 1399 mmu_free_roots(vcpu);
6aa8b732
AK
1400}
1401
6aa8b732
AK
1402static void inject_page_fault(struct kvm_vcpu *vcpu,
1403 u64 addr,
1404 u32 err_code)
1405{
c3c91fee 1406 kvm_inject_page_fault(vcpu, addr, err_code);
6aa8b732
AK
1407}
1408
6aa8b732
AK
1409static void paging_free(struct kvm_vcpu *vcpu)
1410{
1411 nonpaging_free(vcpu);
1412}
1413
1414#define PTTYPE 64
1415#include "paging_tmpl.h"
1416#undef PTTYPE
1417
1418#define PTTYPE 32
1419#include "paging_tmpl.h"
1420#undef PTTYPE
1421
17ac10ad 1422static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
6aa8b732 1423{
ad312c7c 1424 struct kvm_mmu *context = &vcpu->arch.mmu;
6aa8b732
AK
1425
1426 ASSERT(is_pae(vcpu));
1427 context->new_cr3 = paging_new_cr3;
1428 context->page_fault = paging64_page_fault;
6aa8b732 1429 context->gva_to_gpa = paging64_gva_to_gpa;
c7addb90 1430 context->prefetch_page = paging64_prefetch_page;
6aa8b732 1431 context->free = paging_free;
17ac10ad
AK
1432 context->root_level = level;
1433 context->shadow_root_level = level;
17c3ba9d 1434 context->root_hpa = INVALID_PAGE;
6aa8b732
AK
1435 return 0;
1436}
1437
17ac10ad
AK
1438static int paging64_init_context(struct kvm_vcpu *vcpu)
1439{
1440 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1441}
1442
6aa8b732
AK
1443static int paging32_init_context(struct kvm_vcpu *vcpu)
1444{
ad312c7c 1445 struct kvm_mmu *context = &vcpu->arch.mmu;
6aa8b732
AK
1446
1447 context->new_cr3 = paging_new_cr3;
1448 context->page_fault = paging32_page_fault;
6aa8b732
AK
1449 context->gva_to_gpa = paging32_gva_to_gpa;
1450 context->free = paging_free;
c7addb90 1451 context->prefetch_page = paging32_prefetch_page;
6aa8b732
AK
1452 context->root_level = PT32_ROOT_LEVEL;
1453 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 1454 context->root_hpa = INVALID_PAGE;
6aa8b732
AK
1455 return 0;
1456}
1457
1458static int paging32E_init_context(struct kvm_vcpu *vcpu)
1459{
17ac10ad 1460 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
6aa8b732
AK
1461}
1462
fb72d167
JR
1463static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1464{
1465 struct kvm_mmu *context = &vcpu->arch.mmu;
1466
1467 context->new_cr3 = nonpaging_new_cr3;
1468 context->page_fault = tdp_page_fault;
1469 context->free = nonpaging_free;
1470 context->prefetch_page = nonpaging_prefetch_page;
1471 context->shadow_root_level = TDP_ROOT_LEVEL;
1472 context->root_hpa = INVALID_PAGE;
1473
1474 if (!is_paging(vcpu)) {
1475 context->gva_to_gpa = nonpaging_gva_to_gpa;
1476 context->root_level = 0;
1477 } else if (is_long_mode(vcpu)) {
1478 context->gva_to_gpa = paging64_gva_to_gpa;
1479 context->root_level = PT64_ROOT_LEVEL;
1480 } else if (is_pae(vcpu)) {
1481 context->gva_to_gpa = paging64_gva_to_gpa;
1482 context->root_level = PT32E_ROOT_LEVEL;
1483 } else {
1484 context->gva_to_gpa = paging32_gva_to_gpa;
1485 context->root_level = PT32_ROOT_LEVEL;
1486 }
1487
1488 return 0;
1489}
1490
1491static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
6aa8b732
AK
1492{
1493 ASSERT(vcpu);
ad312c7c 1494 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732
AK
1495
1496 if (!is_paging(vcpu))
1497 return nonpaging_init_context(vcpu);
a9058ecd 1498 else if (is_long_mode(vcpu))
6aa8b732
AK
1499 return paging64_init_context(vcpu);
1500 else if (is_pae(vcpu))
1501 return paging32E_init_context(vcpu);
1502 else
1503 return paging32_init_context(vcpu);
1504}
1505
fb72d167
JR
1506static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1507{
1508 if (tdp_enabled)
1509 return init_kvm_tdp_mmu(vcpu);
1510 else
1511 return init_kvm_softmmu(vcpu);
1512}
1513
6aa8b732
AK
1514static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1515{
1516 ASSERT(vcpu);
ad312c7c
ZX
1517 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1518 vcpu->arch.mmu.free(vcpu);
1519 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
6aa8b732
AK
1520 }
1521}
1522
1523int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
17c3ba9d
AK
1524{
1525 destroy_kvm_mmu(vcpu);
1526 return init_kvm_mmu(vcpu);
1527}
8668a3c4 1528EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
17c3ba9d
AK
1529
1530int kvm_mmu_load(struct kvm_vcpu *vcpu)
6aa8b732 1531{
714b93da
AK
1532 int r;
1533
e2dec939 1534 r = mmu_topup_memory_caches(vcpu);
17c3ba9d
AK
1535 if (r)
1536 goto out;
aaee2c94 1537 spin_lock(&vcpu->kvm->mmu_lock);
eb787d10 1538 kvm_mmu_free_some_pages(vcpu);
17c3ba9d 1539 mmu_alloc_roots(vcpu);
aaee2c94 1540 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 1541 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
17c3ba9d 1542 kvm_mmu_flush_tlb(vcpu);
714b93da
AK
1543out:
1544 return r;
6aa8b732 1545}
17c3ba9d
AK
1546EXPORT_SYMBOL_GPL(kvm_mmu_load);
1547
1548void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1549{
1550 mmu_free_roots(vcpu);
1551}
6aa8b732 1552
09072daf 1553static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
4db35314 1554 struct kvm_mmu_page *sp,
ac1b714e
AK
1555 u64 *spte)
1556{
1557 u64 pte;
1558 struct kvm_mmu_page *child;
1559
1560 pte = *spte;
c7addb90 1561 if (is_shadow_present_pte(pte)) {
05da4558
MT
1562 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1563 is_large_pte(pte))
290fc38d 1564 rmap_remove(vcpu->kvm, spte);
ac1b714e
AK
1565 else {
1566 child = page_header(pte & PT64_BASE_ADDR_MASK);
90cb0529 1567 mmu_page_remove_parent_pte(child, spte);
ac1b714e
AK
1568 }
1569 }
c7addb90 1570 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
05da4558
MT
1571 if (is_large_pte(pte))
1572 --vcpu->kvm->stat.lpages;
ac1b714e
AK
1573}
1574
0028425f 1575static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
4db35314 1576 struct kvm_mmu_page *sp,
0028425f 1577 u64 *spte,
489f1d65 1578 const void *new)
0028425f 1579{
05da4558
MT
1580 if ((sp->role.level != PT_PAGE_TABLE_LEVEL)
1581 && !vcpu->arch.update_pte.largepage) {
4cee5764 1582 ++vcpu->kvm->stat.mmu_pde_zapped;
0028425f 1583 return;
4cee5764 1584 }
0028425f 1585
4cee5764 1586 ++vcpu->kvm->stat.mmu_pte_updated;
4db35314 1587 if (sp->role.glevels == PT32_ROOT_LEVEL)
489f1d65 1588 paging32_update_pte(vcpu, sp, spte, new);
0028425f 1589 else
489f1d65 1590 paging64_update_pte(vcpu, sp, spte, new);
0028425f
AK
1591}
1592
79539cec
AK
1593static bool need_remote_flush(u64 old, u64 new)
1594{
1595 if (!is_shadow_present_pte(old))
1596 return false;
1597 if (!is_shadow_present_pte(new))
1598 return true;
1599 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1600 return true;
1601 old ^= PT64_NX_MASK;
1602 new ^= PT64_NX_MASK;
1603 return (old & ~new & PT64_PERM_MASK) != 0;
1604}
1605
1606static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1607{
1608 if (need_remote_flush(old, new))
1609 kvm_flush_remote_tlbs(vcpu->kvm);
1610 else
1611 kvm_mmu_flush_tlb(vcpu);
1612}
1613
12b7d28f
AK
1614static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1615{
ad312c7c 1616 u64 *spte = vcpu->arch.last_pte_updated;
12b7d28f
AK
1617
1618 return !!(spte && (*spte & PT_ACCESSED_MASK));
1619}
1620
d7824fff
AK
1621static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1622 const u8 *new, int bytes)
1623{
1624 gfn_t gfn;
1625 int r;
1626 u64 gpte = 0;
72dc67a6 1627 struct page *page;
d7824fff 1628
05da4558
MT
1629 vcpu->arch.update_pte.largepage = 0;
1630
d7824fff
AK
1631 if (bytes != 4 && bytes != 8)
1632 return;
1633
1634 /*
1635 * Assume that the pte write on a page table of the same type
1636 * as the current vcpu paging mode. This is nearly always true
1637 * (might be false while changing modes). Note it is verified later
1638 * by update_pte().
1639 */
1640 if (is_pae(vcpu)) {
1641 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1642 if ((bytes == 4) && (gpa % 4 == 0)) {
1643 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1644 if (r)
1645 return;
1646 memcpy((void *)&gpte + (gpa % 8), new, 4);
1647 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1648 memcpy((void *)&gpte, new, 8);
1649 }
1650 } else {
1651 if ((bytes == 4) && (gpa % 4 == 0))
1652 memcpy((void *)&gpte, new, 4);
1653 }
1654 if (!is_present_pte(gpte))
1655 return;
1656 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
72dc67a6 1657
05da4558
MT
1658 down_read(&current->mm->mmap_sem);
1659 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1660 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1661 vcpu->arch.update_pte.largepage = 1;
1662 }
72dc67a6 1663 page = gfn_to_page(vcpu->kvm, gfn);
05da4558 1664 up_read(&current->mm->mmap_sem);
72dc67a6 1665
d196e343
AK
1666 if (is_error_page(page)) {
1667 kvm_release_page_clean(page);
1668 return;
1669 }
d7824fff 1670 vcpu->arch.update_pte.gfn = gfn;
e48bb497 1671 vcpu->arch.update_pte.page = page;
d7824fff
AK
1672}
1673
09072daf 1674void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
fe551881 1675 const u8 *new, int bytes)
da4a00f0 1676{
9b7a0325 1677 gfn_t gfn = gpa >> PAGE_SHIFT;
4db35314 1678 struct kvm_mmu_page *sp;
0e7bc4b9 1679 struct hlist_node *node, *n;
9b7a0325
AK
1680 struct hlist_head *bucket;
1681 unsigned index;
489f1d65 1682 u64 entry, gentry;
9b7a0325 1683 u64 *spte;
9b7a0325 1684 unsigned offset = offset_in_page(gpa);
0e7bc4b9 1685 unsigned pte_size;
9b7a0325 1686 unsigned page_offset;
0e7bc4b9 1687 unsigned misaligned;
fce0657f 1688 unsigned quadrant;
9b7a0325 1689 int level;
86a5ba02 1690 int flooded = 0;
ac1b714e 1691 int npte;
489f1d65 1692 int r;
9b7a0325 1693
da4a00f0 1694 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
d7824fff 1695 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
aaee2c94 1696 spin_lock(&vcpu->kvm->mmu_lock);
eb787d10 1697 kvm_mmu_free_some_pages(vcpu);
4cee5764 1698 ++vcpu->kvm->stat.mmu_pte_write;
c7addb90 1699 kvm_mmu_audit(vcpu, "pre pte write");
ad312c7c 1700 if (gfn == vcpu->arch.last_pt_write_gfn
12b7d28f 1701 && !last_updated_pte_accessed(vcpu)) {
ad312c7c
ZX
1702 ++vcpu->arch.last_pt_write_count;
1703 if (vcpu->arch.last_pt_write_count >= 3)
86a5ba02
AK
1704 flooded = 1;
1705 } else {
ad312c7c
ZX
1706 vcpu->arch.last_pt_write_gfn = gfn;
1707 vcpu->arch.last_pt_write_count = 1;
1708 vcpu->arch.last_pte_updated = NULL;
86a5ba02 1709 }
1ae0a13d 1710 index = kvm_page_table_hashfn(gfn);
f05e70ac 1711 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
4db35314
AK
1712 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1713 if (sp->gfn != gfn || sp->role.metaphysical)
9b7a0325 1714 continue;
4db35314 1715 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
0e7bc4b9 1716 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
e925c5ba 1717 misaligned |= bytes < 4;
86a5ba02 1718 if (misaligned || flooded) {
0e7bc4b9
AK
1719 /*
1720 * Misaligned accesses are too much trouble to fix
1721 * up; also, they usually indicate a page is not used
1722 * as a page table.
86a5ba02
AK
1723 *
1724 * If we're seeing too many writes to a page,
1725 * it may no longer be a page table, or we may be
1726 * forking, in which case it is better to unmap the
1727 * page.
0e7bc4b9
AK
1728 */
1729 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
4db35314
AK
1730 gpa, bytes, sp->role.word);
1731 kvm_mmu_zap_page(vcpu->kvm, sp);
4cee5764 1732 ++vcpu->kvm->stat.mmu_flooded;
0e7bc4b9
AK
1733 continue;
1734 }
9b7a0325 1735 page_offset = offset;
4db35314 1736 level = sp->role.level;
ac1b714e 1737 npte = 1;
4db35314 1738 if (sp->role.glevels == PT32_ROOT_LEVEL) {
ac1b714e
AK
1739 page_offset <<= 1; /* 32->64 */
1740 /*
1741 * A 32-bit pde maps 4MB while the shadow pdes map
1742 * only 2MB. So we need to double the offset again
1743 * and zap two pdes instead of one.
1744 */
1745 if (level == PT32_ROOT_LEVEL) {
6b8d0f9b 1746 page_offset &= ~7; /* kill rounding error */
ac1b714e
AK
1747 page_offset <<= 1;
1748 npte = 2;
1749 }
fce0657f 1750 quadrant = page_offset >> PAGE_SHIFT;
9b7a0325 1751 page_offset &= ~PAGE_MASK;
4db35314 1752 if (quadrant != sp->role.quadrant)
fce0657f 1753 continue;
9b7a0325 1754 }
4db35314 1755 spte = &sp->spt[page_offset / sizeof(*spte)];
489f1d65
DE
1756 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1757 gentry = 0;
1758 r = kvm_read_guest_atomic(vcpu->kvm,
1759 gpa & ~(u64)(pte_size - 1),
1760 &gentry, pte_size);
1761 new = (const void *)&gentry;
1762 if (r < 0)
1763 new = NULL;
1764 }
ac1b714e 1765 while (npte--) {
79539cec 1766 entry = *spte;
4db35314 1767 mmu_pte_write_zap_pte(vcpu, sp, spte);
489f1d65
DE
1768 if (new)
1769 mmu_pte_write_new_pte(vcpu, sp, spte, new);
79539cec 1770 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
ac1b714e 1771 ++spte;
9b7a0325 1772 }
9b7a0325 1773 }
c7addb90 1774 kvm_mmu_audit(vcpu, "post pte write");
aaee2c94 1775 spin_unlock(&vcpu->kvm->mmu_lock);
d7824fff
AK
1776 if (vcpu->arch.update_pte.page) {
1777 kvm_release_page_clean(vcpu->arch.update_pte.page);
1778 vcpu->arch.update_pte.page = NULL;
1779 }
da4a00f0
AK
1780}
1781
a436036b
AK
1782int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1783{
10589a46
MT
1784 gpa_t gpa;
1785 int r;
a436036b 1786
72dc67a6 1787 down_read(&vcpu->kvm->slots_lock);
10589a46 1788 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
72dc67a6 1789 up_read(&vcpu->kvm->slots_lock);
10589a46 1790
aaee2c94 1791 spin_lock(&vcpu->kvm->mmu_lock);
10589a46 1792 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
aaee2c94 1793 spin_unlock(&vcpu->kvm->mmu_lock);
10589a46 1794 return r;
a436036b
AK
1795}
1796
22d95b12 1797void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
ebeace86 1798{
f05e70ac 1799 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
4db35314 1800 struct kvm_mmu_page *sp;
ebeace86 1801
f05e70ac 1802 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
4db35314
AK
1803 struct kvm_mmu_page, link);
1804 kvm_mmu_zap_page(vcpu->kvm, sp);
4cee5764 1805 ++vcpu->kvm->stat.mmu_recycled;
ebeace86
AK
1806 }
1807}
ebeace86 1808
3067714c
AK
1809int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1810{
1811 int r;
1812 enum emulation_result er;
1813
ad312c7c 1814 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
3067714c
AK
1815 if (r < 0)
1816 goto out;
1817
1818 if (!r) {
1819 r = 1;
1820 goto out;
1821 }
1822
b733bfb5
AK
1823 r = mmu_topup_memory_caches(vcpu);
1824 if (r)
1825 goto out;
1826
3067714c 1827 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
3067714c
AK
1828
1829 switch (er) {
1830 case EMULATE_DONE:
1831 return 1;
1832 case EMULATE_DO_MMIO:
1833 ++vcpu->stat.mmio_exits;
1834 return 0;
1835 case EMULATE_FAIL:
1836 kvm_report_emulation_failure(vcpu, "pagetable");
1837 return 1;
1838 default:
1839 BUG();
1840 }
1841out:
3067714c
AK
1842 return r;
1843}
1844EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1845
18552672
JR
1846void kvm_enable_tdp(void)
1847{
1848 tdp_enabled = true;
1849}
1850EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1851
6aa8b732
AK
1852static void free_mmu_pages(struct kvm_vcpu *vcpu)
1853{
4db35314 1854 struct kvm_mmu_page *sp;
6aa8b732 1855
f05e70ac
ZX
1856 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1857 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
4db35314
AK
1858 struct kvm_mmu_page, link);
1859 kvm_mmu_zap_page(vcpu->kvm, sp);
f51234c2 1860 }
ad312c7c 1861 free_page((unsigned long)vcpu->arch.mmu.pae_root);
6aa8b732
AK
1862}
1863
1864static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1865{
17ac10ad 1866 struct page *page;
6aa8b732
AK
1867 int i;
1868
1869 ASSERT(vcpu);
1870
f05e70ac
ZX
1871 if (vcpu->kvm->arch.n_requested_mmu_pages)
1872 vcpu->kvm->arch.n_free_mmu_pages =
1873 vcpu->kvm->arch.n_requested_mmu_pages;
82ce2c96 1874 else
f05e70ac
ZX
1875 vcpu->kvm->arch.n_free_mmu_pages =
1876 vcpu->kvm->arch.n_alloc_mmu_pages;
17ac10ad
AK
1877 /*
1878 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1879 * Therefore we need to allocate shadow page tables in the first
1880 * 4GB of memory, which happens to fit the DMA32 zone.
1881 */
1882 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1883 if (!page)
1884 goto error_1;
ad312c7c 1885 vcpu->arch.mmu.pae_root = page_address(page);
17ac10ad 1886 for (i = 0; i < 4; ++i)
ad312c7c 1887 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 1888
6aa8b732
AK
1889 return 0;
1890
1891error_1:
1892 free_mmu_pages(vcpu);
1893 return -ENOMEM;
1894}
1895
8018c27b 1896int kvm_mmu_create(struct kvm_vcpu *vcpu)
6aa8b732 1897{
6aa8b732 1898 ASSERT(vcpu);
ad312c7c 1899 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 1900
8018c27b
IM
1901 return alloc_mmu_pages(vcpu);
1902}
6aa8b732 1903
8018c27b
IM
1904int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1905{
1906 ASSERT(vcpu);
ad312c7c 1907 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2c264957 1908
8018c27b 1909 return init_kvm_mmu(vcpu);
6aa8b732
AK
1910}
1911
1912void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1913{
1914 ASSERT(vcpu);
1915
1916 destroy_kvm_mmu(vcpu);
1917 free_mmu_pages(vcpu);
714b93da 1918 mmu_free_memory_caches(vcpu);
6aa8b732
AK
1919}
1920
90cb0529 1921void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
6aa8b732 1922{
4db35314 1923 struct kvm_mmu_page *sp;
6aa8b732 1924
f05e70ac 1925 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
6aa8b732
AK
1926 int i;
1927 u64 *pt;
1928
4db35314 1929 if (!test_bit(slot, &sp->slot_bitmap))
6aa8b732
AK
1930 continue;
1931
4db35314 1932 pt = sp->spt;
6aa8b732
AK
1933 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1934 /* avoid RMW */
9647c14c 1935 if (pt[i] & PT_WRITABLE_MASK)
6aa8b732 1936 pt[i] &= ~PT_WRITABLE_MASK;
6aa8b732
AK
1937 }
1938}
37a7d8b0 1939
90cb0529 1940void kvm_mmu_zap_all(struct kvm *kvm)
e0fa826f 1941{
4db35314 1942 struct kvm_mmu_page *sp, *node;
e0fa826f 1943
aaee2c94 1944 spin_lock(&kvm->mmu_lock);
f05e70ac 1945 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
4db35314 1946 kvm_mmu_zap_page(kvm, sp);
aaee2c94 1947 spin_unlock(&kvm->mmu_lock);
e0fa826f 1948
90cb0529 1949 kvm_flush_remote_tlbs(kvm);
e0fa826f
DL
1950}
1951
b5a33a75
AK
1952void kvm_mmu_module_exit(void)
1953{
1954 if (pte_chain_cache)
1955 kmem_cache_destroy(pte_chain_cache);
1956 if (rmap_desc_cache)
1957 kmem_cache_destroy(rmap_desc_cache);
d3d25b04
AK
1958 if (mmu_page_header_cache)
1959 kmem_cache_destroy(mmu_page_header_cache);
b5a33a75
AK
1960}
1961
1962int kvm_mmu_module_init(void)
1963{
1964 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1965 sizeof(struct kvm_pte_chain),
20c2df83 1966 0, 0, NULL);
b5a33a75
AK
1967 if (!pte_chain_cache)
1968 goto nomem;
1969 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1970 sizeof(struct kvm_rmap_desc),
20c2df83 1971 0, 0, NULL);
b5a33a75
AK
1972 if (!rmap_desc_cache)
1973 goto nomem;
1974
d3d25b04
AK
1975 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1976 sizeof(struct kvm_mmu_page),
20c2df83 1977 0, 0, NULL);
d3d25b04
AK
1978 if (!mmu_page_header_cache)
1979 goto nomem;
1980
b5a33a75
AK
1981 return 0;
1982
1983nomem:
1984 kvm_mmu_module_exit();
1985 return -ENOMEM;
1986}
1987
3ad82a7e
ZX
1988/*
1989 * Caculate mmu pages needed for kvm.
1990 */
1991unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1992{
1993 int i;
1994 unsigned int nr_mmu_pages;
1995 unsigned int nr_pages = 0;
1996
1997 for (i = 0; i < kvm->nmemslots; i++)
1998 nr_pages += kvm->memslots[i].npages;
1999
2000 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2001 nr_mmu_pages = max(nr_mmu_pages,
2002 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2003
2004 return nr_mmu_pages;
2005}
2006
37a7d8b0
AK
2007#ifdef AUDIT
2008
2009static const char *audit_msg;
2010
2011static gva_t canonicalize(gva_t gva)
2012{
2013#ifdef CONFIG_X86_64
2014 gva = (long long)(gva << 16) >> 16;
2015#endif
2016 return gva;
2017}
2018
2019static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2020 gva_t va, int level)
2021{
2022 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2023 int i;
2024 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2025
2026 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2027 u64 ent = pt[i];
2028
c7addb90 2029 if (ent == shadow_trap_nonpresent_pte)
37a7d8b0
AK
2030 continue;
2031
2032 va = canonicalize(va);
c7addb90
AK
2033 if (level > 1) {
2034 if (ent == shadow_notrap_nonpresent_pte)
2035 printk(KERN_ERR "audit: (%s) nontrapping pte"
2036 " in nonleaf level: levels %d gva %lx"
2037 " level %d pte %llx\n", audit_msg,
ad312c7c 2038 vcpu->arch.mmu.root_level, va, level, ent);
c7addb90 2039
37a7d8b0 2040 audit_mappings_page(vcpu, ent, va, level - 1);
c7addb90 2041 } else {
ad312c7c 2042 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
1d28f5f4
AK
2043 struct page *page = gpa_to_page(vcpu, gpa);
2044 hpa_t hpa = page_to_phys(page);
37a7d8b0 2045
c7addb90 2046 if (is_shadow_present_pte(ent)
37a7d8b0 2047 && (ent & PT64_BASE_ADDR_MASK) != hpa)
c7addb90
AK
2048 printk(KERN_ERR "xx audit error: (%s) levels %d"
2049 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
ad312c7c 2050 audit_msg, vcpu->arch.mmu.root_level,
d77c26fc
MD
2051 va, gpa, hpa, ent,
2052 is_shadow_present_pte(ent));
c7addb90
AK
2053 else if (ent == shadow_notrap_nonpresent_pte
2054 && !is_error_hpa(hpa))
2055 printk(KERN_ERR "audit: (%s) notrap shadow,"
2056 " valid guest gva %lx\n", audit_msg, va);
b4231d61 2057 kvm_release_page_clean(page);
c7addb90 2058
37a7d8b0
AK
2059 }
2060 }
2061}
2062
2063static void audit_mappings(struct kvm_vcpu *vcpu)
2064{
1ea252af 2065 unsigned i;
37a7d8b0 2066
ad312c7c
ZX
2067 if (vcpu->arch.mmu.root_level == 4)
2068 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
37a7d8b0
AK
2069 else
2070 for (i = 0; i < 4; ++i)
ad312c7c 2071 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
37a7d8b0 2072 audit_mappings_page(vcpu,
ad312c7c 2073 vcpu->arch.mmu.pae_root[i],
37a7d8b0
AK
2074 i << 30,
2075 2);
2076}
2077
2078static int count_rmaps(struct kvm_vcpu *vcpu)
2079{
2080 int nmaps = 0;
2081 int i, j, k;
2082
2083 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2084 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2085 struct kvm_rmap_desc *d;
2086
2087 for (j = 0; j < m->npages; ++j) {
290fc38d 2088 unsigned long *rmapp = &m->rmap[j];
37a7d8b0 2089
290fc38d 2090 if (!*rmapp)
37a7d8b0 2091 continue;
290fc38d 2092 if (!(*rmapp & 1)) {
37a7d8b0
AK
2093 ++nmaps;
2094 continue;
2095 }
290fc38d 2096 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
37a7d8b0
AK
2097 while (d) {
2098 for (k = 0; k < RMAP_EXT; ++k)
2099 if (d->shadow_ptes[k])
2100 ++nmaps;
2101 else
2102 break;
2103 d = d->more;
2104 }
2105 }
2106 }
2107 return nmaps;
2108}
2109
2110static int count_writable_mappings(struct kvm_vcpu *vcpu)
2111{
2112 int nmaps = 0;
4db35314 2113 struct kvm_mmu_page *sp;
37a7d8b0
AK
2114 int i;
2115
f05e70ac 2116 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
4db35314 2117 u64 *pt = sp->spt;
37a7d8b0 2118
4db35314 2119 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
37a7d8b0
AK
2120 continue;
2121
2122 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2123 u64 ent = pt[i];
2124
2125 if (!(ent & PT_PRESENT_MASK))
2126 continue;
2127 if (!(ent & PT_WRITABLE_MASK))
2128 continue;
2129 ++nmaps;
2130 }
2131 }
2132 return nmaps;
2133}
2134
2135static void audit_rmap(struct kvm_vcpu *vcpu)
2136{
2137 int n_rmap = count_rmaps(vcpu);
2138 int n_actual = count_writable_mappings(vcpu);
2139
2140 if (n_rmap != n_actual)
2141 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2142 __FUNCTION__, audit_msg, n_rmap, n_actual);
2143}
2144
2145static void audit_write_protection(struct kvm_vcpu *vcpu)
2146{
4db35314 2147 struct kvm_mmu_page *sp;
290fc38d
IE
2148 struct kvm_memory_slot *slot;
2149 unsigned long *rmapp;
2150 gfn_t gfn;
37a7d8b0 2151
f05e70ac 2152 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
4db35314 2153 if (sp->role.metaphysical)
37a7d8b0
AK
2154 continue;
2155
4db35314
AK
2156 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2157 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
290fc38d
IE
2158 rmapp = &slot->rmap[gfn - slot->base_gfn];
2159 if (*rmapp)
37a7d8b0
AK
2160 printk(KERN_ERR "%s: (%s) shadow page has writable"
2161 " mappings: gfn %lx role %x\n",
4db35314
AK
2162 __FUNCTION__, audit_msg, sp->gfn,
2163 sp->role.word);
37a7d8b0
AK
2164 }
2165}
2166
2167static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2168{
2169 int olddbg = dbg;
2170
2171 dbg = 0;
2172 audit_msg = msg;
2173 audit_rmap(vcpu);
2174 audit_write_protection(vcpu);
2175 audit_mappings(vcpu);
2176 dbg = olddbg;
2177}
2178
2179#endif