KVM: x86: remove the dead code of KVM_EXIT_HYPERCALL
[linux-block.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.
9611c187 10 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6aa8b732
AK
11 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
e495606d 20
af585b92 21#include "irq.h"
1d737c8a 22#include "mmu.h"
836a1b3c 23#include "x86.h"
6de4f3ad 24#include "kvm_cache_regs.h"
e495606d 25
edf88417 26#include <linux/kvm_host.h>
6aa8b732
AK
27#include <linux/types.h>
28#include <linux/string.h>
6aa8b732
AK
29#include <linux/mm.h>
30#include <linux/highmem.h>
31#include <linux/module.h>
448353ca 32#include <linux/swap.h>
05da4558 33#include <linux/hugetlb.h>
2f333bcb 34#include <linux/compiler.h>
bc6678a3 35#include <linux/srcu.h>
5a0e3ad6 36#include <linux/slab.h>
bf998156 37#include <linux/uaccess.h>
6aa8b732 38
e495606d
AK
39#include <asm/page.h>
40#include <asm/cmpxchg.h>
4e542370 41#include <asm/io.h>
13673a90 42#include <asm/vmx.h>
6aa8b732 43
18552672
JR
44/*
45 * When setting this variable to true it enables Two-Dimensional-Paging
46 * where the hardware walks 2 page tables:
47 * 1. the guest-virtual to guest-physical
48 * 2. while doing 1. it walks guest-physical to host-physical
49 * If the hardware supports that we don't need to do shadow paging.
50 */
2f333bcb 51bool tdp_enabled = false;
18552672 52
8b1fe17c
XG
53enum {
54 AUDIT_PRE_PAGE_FAULT,
55 AUDIT_POST_PAGE_FAULT,
56 AUDIT_PRE_PTE_WRITE,
6903074c
XG
57 AUDIT_POST_PTE_WRITE,
58 AUDIT_PRE_SYNC,
59 AUDIT_POST_SYNC
8b1fe17c 60};
37a7d8b0 61
8b1fe17c
XG
62char *audit_point_name[] = {
63 "pre page fault",
64 "post page fault",
65 "pre pte write",
6903074c
XG
66 "post pte write",
67 "pre sync",
68 "post sync"
8b1fe17c 69};
37a7d8b0 70
0375f7fa
XG
71#ifdef CONFIG_KVM_MMU_AUDIT
72static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point);
73#else
74static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
75#endif
76
8b1fe17c 77#undef MMU_DEBUG
37a7d8b0
AK
78
79#ifdef MMU_DEBUG
80
81#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
82#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
83
84#else
85
86#define pgprintk(x...) do { } while (0)
87#define rmap_printk(x...) do { } while (0)
88
89#endif
90
8b1fe17c 91#ifdef MMU_DEBUG
6ada8cca
AK
92static int dbg = 0;
93module_param(dbg, bool, 0644);
37a7d8b0 94#endif
6aa8b732 95
582801a9
MT
96static int oos_shadow = 1;
97module_param(oos_shadow, bool, 0644);
98
d6c69ee9
YD
99#ifndef MMU_DEBUG
100#define ASSERT(x) do { } while (0)
101#else
6aa8b732
AK
102#define ASSERT(x) \
103 if (!(x)) { \
104 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
105 __FILE__, __LINE__, #x); \
106 }
d6c69ee9 107#endif
6aa8b732 108
957ed9ef
XG
109#define PTE_PREFETCH_NUM 8
110
6aa8b732
AK
111#define PT_FIRST_AVAIL_BITS_SHIFT 9
112#define PT64_SECOND_AVAIL_BITS_SHIFT 52
113
6aa8b732
AK
114#define PT64_LEVEL_BITS 9
115
116#define PT64_LEVEL_SHIFT(level) \
d77c26fc 117 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
6aa8b732 118
6aa8b732
AK
119#define PT64_INDEX(address, level)\
120 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
121
122
123#define PT32_LEVEL_BITS 10
124
125#define PT32_LEVEL_SHIFT(level) \
d77c26fc 126 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
6aa8b732 127
e04da980
JR
128#define PT32_LVL_OFFSET_MASK(level) \
129 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT32_LEVEL_BITS))) - 1))
6aa8b732
AK
131
132#define PT32_INDEX(address, level)\
133 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
134
135
27aba766 136#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
6aa8b732
AK
137#define PT64_DIR_BASE_ADDR_MASK \
138 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
e04da980
JR
139#define PT64_LVL_ADDR_MASK(level) \
140 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
141 * PT64_LEVEL_BITS))) - 1))
142#define PT64_LVL_OFFSET_MASK(level) \
143 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
144 * PT64_LEVEL_BITS))) - 1))
6aa8b732
AK
145
146#define PT32_BASE_ADDR_MASK PAGE_MASK
147#define PT32_DIR_BASE_ADDR_MASK \
148 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
e04da980
JR
149#define PT32_LVL_ADDR_MASK(level) \
150 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
151 * PT32_LEVEL_BITS))) - 1))
6aa8b732 152
79539cec
AK
153#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
154 | PT64_NX_MASK)
6aa8b732 155
53c07b18 156#define PTE_LIST_EXT 4
cd4a4e53 157
fe135d2c
AK
158#define ACC_EXEC_MASK 1
159#define ACC_WRITE_MASK PT_WRITABLE_MASK
160#define ACC_USER_MASK PT_USER_MASK
161#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
162
90bb6fc5
AK
163#include <trace/events/kvm.h>
164
07420171
AK
165#define CREATE_TRACE_POINTS
166#include "mmutrace.h"
167
1403283a
IE
168#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
169
135f8c2b
AK
170#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
171
53c07b18
XG
172struct pte_list_desc {
173 u64 *sptes[PTE_LIST_EXT];
174 struct pte_list_desc *more;
cd4a4e53
AK
175};
176
2d11123a
AK
177struct kvm_shadow_walk_iterator {
178 u64 addr;
179 hpa_t shadow_addr;
2d11123a 180 u64 *sptep;
dd3bfd59 181 int level;
2d11123a
AK
182 unsigned index;
183};
184
185#define for_each_shadow_entry(_vcpu, _addr, _walker) \
186 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
187 shadow_walk_okay(&(_walker)); \
188 shadow_walk_next(&(_walker)))
189
c2a2ac2b
XG
190#define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
191 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
192 shadow_walk_okay(&(_walker)) && \
193 ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
194 __shadow_walk_next(&(_walker), spte))
195
53c07b18 196static struct kmem_cache *pte_list_desc_cache;
d3d25b04 197static struct kmem_cache *mmu_page_header_cache;
45221ab6 198static struct percpu_counter kvm_total_used_mmu_pages;
b5a33a75 199
7b52345e
SY
200static u64 __read_mostly shadow_nx_mask;
201static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
202static u64 __read_mostly shadow_user_mask;
203static u64 __read_mostly shadow_accessed_mask;
204static u64 __read_mostly shadow_dirty_mask;
ce88decf
XG
205static u64 __read_mostly shadow_mmio_mask;
206
207static void mmu_spte_set(u64 *sptep, u64 spte);
208
209void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask)
210{
211 shadow_mmio_mask = mmio_mask;
212}
213EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
214
215static void mark_mmio_spte(u64 *sptep, u64 gfn, unsigned access)
216{
217 access &= ACC_WRITE_MASK | ACC_USER_MASK;
218
4f022648 219 trace_mark_mmio_spte(sptep, gfn, access);
ce88decf
XG
220 mmu_spte_set(sptep, shadow_mmio_mask | access | gfn << PAGE_SHIFT);
221}
222
223static bool is_mmio_spte(u64 spte)
224{
225 return (spte & shadow_mmio_mask) == shadow_mmio_mask;
226}
227
228static gfn_t get_mmio_spte_gfn(u64 spte)
229{
230 return (spte & ~shadow_mmio_mask) >> PAGE_SHIFT;
231}
232
233static unsigned get_mmio_spte_access(u64 spte)
234{
235 return (spte & ~shadow_mmio_mask) & ~PAGE_MASK;
236}
237
238static bool set_mmio_spte(u64 *sptep, gfn_t gfn, pfn_t pfn, unsigned access)
239{
240 if (unlikely(is_noslot_pfn(pfn))) {
241 mark_mmio_spte(sptep, gfn, access);
242 return true;
243 }
244
245 return false;
246}
c7addb90 247
82725b20
DE
248static inline u64 rsvd_bits(int s, int e)
249{
250 return ((1ULL << (e - s + 1)) - 1) << s;
251}
252
7b52345e 253void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
4b12f0de 254 u64 dirty_mask, u64 nx_mask, u64 x_mask)
7b52345e
SY
255{
256 shadow_user_mask = user_mask;
257 shadow_accessed_mask = accessed_mask;
258 shadow_dirty_mask = dirty_mask;
259 shadow_nx_mask = nx_mask;
260 shadow_x_mask = x_mask;
261}
262EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
263
6aa8b732
AK
264static int is_cpuid_PSE36(void)
265{
266 return 1;
267}
268
73b1087e
AK
269static int is_nx(struct kvm_vcpu *vcpu)
270{
f6801dff 271 return vcpu->arch.efer & EFER_NX;
73b1087e
AK
272}
273
c7addb90
AK
274static int is_shadow_present_pte(u64 pte)
275{
ce88decf 276 return pte & PT_PRESENT_MASK && !is_mmio_spte(pte);
c7addb90
AK
277}
278
05da4558
MT
279static int is_large_pte(u64 pte)
280{
281 return pte & PT_PAGE_SIZE_MASK;
282}
283
43a3795a 284static int is_dirty_gpte(unsigned long pte)
e3c5e7ec 285{
439e218a 286 return pte & PT_DIRTY_MASK;
e3c5e7ec
AK
287}
288
43a3795a 289static int is_rmap_spte(u64 pte)
cd4a4e53 290{
4b1a80fa 291 return is_shadow_present_pte(pte);
cd4a4e53
AK
292}
293
776e6633
MT
294static int is_last_spte(u64 pte, int level)
295{
296 if (level == PT_PAGE_TABLE_LEVEL)
297 return 1;
852e3c19 298 if (is_large_pte(pte))
776e6633
MT
299 return 1;
300 return 0;
301}
302
35149e21 303static pfn_t spte_to_pfn(u64 pte)
0b49ea86 304{
35149e21 305 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
0b49ea86
AK
306}
307
da928521
AK
308static gfn_t pse36_gfn_delta(u32 gpte)
309{
310 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
311
312 return (gpte & PT32_DIR_PSE36_MASK) << shift;
313}
314
603e0651 315#ifdef CONFIG_X86_64
d555c333 316static void __set_spte(u64 *sptep, u64 spte)
e663ee64 317{
603e0651 318 *sptep = spte;
e663ee64
AK
319}
320
603e0651 321static void __update_clear_spte_fast(u64 *sptep, u64 spte)
a9221dd5 322{
603e0651
XG
323 *sptep = spte;
324}
325
326static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
327{
328 return xchg(sptep, spte);
329}
c2a2ac2b
XG
330
331static u64 __get_spte_lockless(u64 *sptep)
332{
333 return ACCESS_ONCE(*sptep);
334}
ce88decf
XG
335
336static bool __check_direct_spte_mmio_pf(u64 spte)
337{
338 /* It is valid if the spte is zapped. */
339 return spte == 0ull;
340}
a9221dd5 341#else
603e0651
XG
342union split_spte {
343 struct {
344 u32 spte_low;
345 u32 spte_high;
346 };
347 u64 spte;
348};
a9221dd5 349
c2a2ac2b
XG
350static void count_spte_clear(u64 *sptep, u64 spte)
351{
352 struct kvm_mmu_page *sp = page_header(__pa(sptep));
353
354 if (is_shadow_present_pte(spte))
355 return;
356
357 /* Ensure the spte is completely set before we increase the count */
358 smp_wmb();
359 sp->clear_spte_count++;
360}
361
603e0651
XG
362static void __set_spte(u64 *sptep, u64 spte)
363{
364 union split_spte *ssptep, sspte;
a9221dd5 365
603e0651
XG
366 ssptep = (union split_spte *)sptep;
367 sspte = (union split_spte)spte;
368
369 ssptep->spte_high = sspte.spte_high;
370
371 /*
372 * If we map the spte from nonpresent to present, We should store
373 * the high bits firstly, then set present bit, so cpu can not
374 * fetch this spte while we are setting the spte.
375 */
376 smp_wmb();
377
378 ssptep->spte_low = sspte.spte_low;
a9221dd5
AK
379}
380
603e0651
XG
381static void __update_clear_spte_fast(u64 *sptep, u64 spte)
382{
383 union split_spte *ssptep, sspte;
384
385 ssptep = (union split_spte *)sptep;
386 sspte = (union split_spte)spte;
387
388 ssptep->spte_low = sspte.spte_low;
389
390 /*
391 * If we map the spte from present to nonpresent, we should clear
392 * present bit firstly to avoid vcpu fetch the old high bits.
393 */
394 smp_wmb();
395
396 ssptep->spte_high = sspte.spte_high;
c2a2ac2b 397 count_spte_clear(sptep, spte);
603e0651
XG
398}
399
400static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
401{
402 union split_spte *ssptep, sspte, orig;
403
404 ssptep = (union split_spte *)sptep;
405 sspte = (union split_spte)spte;
406
407 /* xchg acts as a barrier before the setting of the high bits */
408 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
41bc3186
ZJ
409 orig.spte_high = ssptep->spte_high;
410 ssptep->spte_high = sspte.spte_high;
c2a2ac2b 411 count_spte_clear(sptep, spte);
603e0651
XG
412
413 return orig.spte;
414}
c2a2ac2b
XG
415
416/*
417 * The idea using the light way get the spte on x86_32 guest is from
418 * gup_get_pte(arch/x86/mm/gup.c).
419 * The difference is we can not catch the spte tlb flush if we leave
420 * guest mode, so we emulate it by increase clear_spte_count when spte
421 * is cleared.
422 */
423static u64 __get_spte_lockless(u64 *sptep)
424{
425 struct kvm_mmu_page *sp = page_header(__pa(sptep));
426 union split_spte spte, *orig = (union split_spte *)sptep;
427 int count;
428
429retry:
430 count = sp->clear_spte_count;
431 smp_rmb();
432
433 spte.spte_low = orig->spte_low;
434 smp_rmb();
435
436 spte.spte_high = orig->spte_high;
437 smp_rmb();
438
439 if (unlikely(spte.spte_low != orig->spte_low ||
440 count != sp->clear_spte_count))
441 goto retry;
442
443 return spte.spte;
444}
ce88decf
XG
445
446static bool __check_direct_spte_mmio_pf(u64 spte)
447{
448 union split_spte sspte = (union split_spte)spte;
449 u32 high_mmio_mask = shadow_mmio_mask >> 32;
450
451 /* It is valid if the spte is zapped. */
452 if (spte == 0ull)
453 return true;
454
455 /* It is valid if the spte is being zapped. */
456 if (sspte.spte_low == 0ull &&
457 (sspte.spte_high & high_mmio_mask) == high_mmio_mask)
458 return true;
459
460 return false;
461}
603e0651
XG
462#endif
463
8672b721
XG
464static bool spte_has_volatile_bits(u64 spte)
465{
466 if (!shadow_accessed_mask)
467 return false;
468
469 if (!is_shadow_present_pte(spte))
470 return false;
471
4132779b
XG
472 if ((spte & shadow_accessed_mask) &&
473 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
8672b721
XG
474 return false;
475
476 return true;
477}
478
4132779b
XG
479static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
480{
481 return (old_spte & bit_mask) && !(new_spte & bit_mask);
482}
483
1df9f2dc
XG
484/* Rules for using mmu_spte_set:
485 * Set the sptep from nonpresent to present.
486 * Note: the sptep being assigned *must* be either not present
487 * or in a state where the hardware will not attempt to update
488 * the spte.
489 */
490static void mmu_spte_set(u64 *sptep, u64 new_spte)
491{
492 WARN_ON(is_shadow_present_pte(*sptep));
493 __set_spte(sptep, new_spte);
494}
495
496/* Rules for using mmu_spte_update:
497 * Update the state bits, it means the mapped pfn is not changged.
498 */
499static void mmu_spte_update(u64 *sptep, u64 new_spte)
b79b93f9 500{
4132779b
XG
501 u64 mask, old_spte = *sptep;
502
503 WARN_ON(!is_rmap_spte(new_spte));
b79b93f9 504
1df9f2dc
XG
505 if (!is_shadow_present_pte(old_spte))
506 return mmu_spte_set(sptep, new_spte);
507
4132779b
XG
508 new_spte |= old_spte & shadow_dirty_mask;
509
510 mask = shadow_accessed_mask;
511 if (is_writable_pte(old_spte))
512 mask |= shadow_dirty_mask;
513
514 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
603e0651 515 __update_clear_spte_fast(sptep, new_spte);
4132779b 516 else
603e0651 517 old_spte = __update_clear_spte_slow(sptep, new_spte);
4132779b
XG
518
519 if (!shadow_accessed_mask)
520 return;
521
522 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
523 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
524 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
525 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
b79b93f9
AK
526}
527
1df9f2dc
XG
528/*
529 * Rules for using mmu_spte_clear_track_bits:
530 * It sets the sptep from present to nonpresent, and track the
531 * state bits, it is used to clear the last level sptep.
532 */
533static int mmu_spte_clear_track_bits(u64 *sptep)
534{
535 pfn_t pfn;
536 u64 old_spte = *sptep;
537
538 if (!spte_has_volatile_bits(old_spte))
603e0651 539 __update_clear_spte_fast(sptep, 0ull);
1df9f2dc 540 else
603e0651 541 old_spte = __update_clear_spte_slow(sptep, 0ull);
1df9f2dc
XG
542
543 if (!is_rmap_spte(old_spte))
544 return 0;
545
546 pfn = spte_to_pfn(old_spte);
547 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
548 kvm_set_pfn_accessed(pfn);
549 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
550 kvm_set_pfn_dirty(pfn);
551 return 1;
552}
553
554/*
555 * Rules for using mmu_spte_clear_no_track:
556 * Directly clear spte without caring the state bits of sptep,
557 * it is used to set the upper level spte.
558 */
559static void mmu_spte_clear_no_track(u64 *sptep)
560{
603e0651 561 __update_clear_spte_fast(sptep, 0ull);
1df9f2dc
XG
562}
563
c2a2ac2b
XG
564static u64 mmu_spte_get_lockless(u64 *sptep)
565{
566 return __get_spte_lockless(sptep);
567}
568
569static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
570{
571 rcu_read_lock();
572 atomic_inc(&vcpu->kvm->arch.reader_counter);
573
574 /* Increase the counter before walking shadow page table */
575 smp_mb__after_atomic_inc();
576}
577
578static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
579{
580 /* Decrease the counter after walking shadow page table finished */
581 smp_mb__before_atomic_dec();
582 atomic_dec(&vcpu->kvm->arch.reader_counter);
583 rcu_read_unlock();
584}
585
e2dec939 586static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
2e3e5882 587 struct kmem_cache *base_cache, int min)
714b93da
AK
588{
589 void *obj;
590
591 if (cache->nobjs >= min)
e2dec939 592 return 0;
714b93da 593 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
2e3e5882 594 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
714b93da 595 if (!obj)
e2dec939 596 return -ENOMEM;
714b93da
AK
597 cache->objects[cache->nobjs++] = obj;
598 }
e2dec939 599 return 0;
714b93da
AK
600}
601
f759e2b4
XG
602static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
603{
604 return cache->nobjs;
605}
606
e8ad9a70
XG
607static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
608 struct kmem_cache *cache)
714b93da
AK
609{
610 while (mc->nobjs)
e8ad9a70 611 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
714b93da
AK
612}
613
c1158e63 614static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
2e3e5882 615 int min)
c1158e63 616{
842f22ed 617 void *page;
c1158e63
AK
618
619 if (cache->nobjs >= min)
620 return 0;
621 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
842f22ed 622 page = (void *)__get_free_page(GFP_KERNEL);
c1158e63
AK
623 if (!page)
624 return -ENOMEM;
842f22ed 625 cache->objects[cache->nobjs++] = page;
c1158e63
AK
626 }
627 return 0;
628}
629
630static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
631{
632 while (mc->nobjs)
c4d198d5 633 free_page((unsigned long)mc->objects[--mc->nobjs]);
c1158e63
AK
634}
635
2e3e5882 636static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
714b93da 637{
e2dec939
AK
638 int r;
639
53c07b18 640 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
67052b35 641 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
d3d25b04
AK
642 if (r)
643 goto out;
ad312c7c 644 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
d3d25b04
AK
645 if (r)
646 goto out;
ad312c7c 647 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
2e3e5882 648 mmu_page_header_cache, 4);
e2dec939
AK
649out:
650 return r;
714b93da
AK
651}
652
653static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
654{
53c07b18
XG
655 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
656 pte_list_desc_cache);
ad312c7c 657 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
e8ad9a70
XG
658 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
659 mmu_page_header_cache);
714b93da
AK
660}
661
662static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
663 size_t size)
664{
665 void *p;
666
667 BUG_ON(!mc->nobjs);
668 p = mc->objects[--mc->nobjs];
714b93da
AK
669 return p;
670}
671
53c07b18 672static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
714b93da 673{
53c07b18
XG
674 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
675 sizeof(struct pte_list_desc));
714b93da
AK
676}
677
53c07b18 678static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
714b93da 679{
53c07b18 680 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
714b93da
AK
681}
682
2032a93d
LJ
683static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
684{
685 if (!sp->role.direct)
686 return sp->gfns[index];
687
688 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
689}
690
691static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
692{
693 if (sp->role.direct)
694 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
695 else
696 sp->gfns[index] = gfn;
697}
698
05da4558 699/*
d4dbf470
TY
700 * Return the pointer to the large page information for a given gfn,
701 * handling slots that are not large page aligned.
05da4558 702 */
d4dbf470
TY
703static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
704 struct kvm_memory_slot *slot,
705 int level)
05da4558
MT
706{
707 unsigned long idx;
708
82855413
JR
709 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
710 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
d4dbf470 711 return &slot->lpage_info[level - 2][idx];
05da4558
MT
712}
713
714static void account_shadowed(struct kvm *kvm, gfn_t gfn)
715{
d25797b2 716 struct kvm_memory_slot *slot;
d4dbf470 717 struct kvm_lpage_info *linfo;
d25797b2 718 int i;
05da4558 719
a1f4d395 720 slot = gfn_to_memslot(kvm, gfn);
d25797b2
JR
721 for (i = PT_DIRECTORY_LEVEL;
722 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
d4dbf470
TY
723 linfo = lpage_info_slot(gfn, slot, i);
724 linfo->write_count += 1;
d25797b2 725 }
332b207d 726 kvm->arch.indirect_shadow_pages++;
05da4558
MT
727}
728
729static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
730{
d25797b2 731 struct kvm_memory_slot *slot;
d4dbf470 732 struct kvm_lpage_info *linfo;
d25797b2 733 int i;
05da4558 734
a1f4d395 735 slot = gfn_to_memslot(kvm, gfn);
d25797b2
JR
736 for (i = PT_DIRECTORY_LEVEL;
737 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
d4dbf470
TY
738 linfo = lpage_info_slot(gfn, slot, i);
739 linfo->write_count -= 1;
740 WARN_ON(linfo->write_count < 0);
d25797b2 741 }
332b207d 742 kvm->arch.indirect_shadow_pages--;
05da4558
MT
743}
744
d25797b2
JR
745static int has_wrprotected_page(struct kvm *kvm,
746 gfn_t gfn,
747 int level)
05da4558 748{
2843099f 749 struct kvm_memory_slot *slot;
d4dbf470 750 struct kvm_lpage_info *linfo;
05da4558 751
a1f4d395 752 slot = gfn_to_memslot(kvm, gfn);
05da4558 753 if (slot) {
d4dbf470
TY
754 linfo = lpage_info_slot(gfn, slot, level);
755 return linfo->write_count;
05da4558
MT
756 }
757
758 return 1;
759}
760
d25797b2 761static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
05da4558 762{
8f0b1ab6 763 unsigned long page_size;
d25797b2 764 int i, ret = 0;
05da4558 765
8f0b1ab6 766 page_size = kvm_host_page_size(kvm, gfn);
05da4558 767
d25797b2
JR
768 for (i = PT_PAGE_TABLE_LEVEL;
769 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
770 if (page_size >= KVM_HPAGE_SIZE(i))
771 ret = i;
772 else
773 break;
774 }
775
4c2155ce 776 return ret;
05da4558
MT
777}
778
5d163b1c
XG
779static struct kvm_memory_slot *
780gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
781 bool no_dirty_log)
05da4558
MT
782{
783 struct kvm_memory_slot *slot;
5d163b1c
XG
784
785 slot = gfn_to_memslot(vcpu->kvm, gfn);
786 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
787 (no_dirty_log && slot->dirty_bitmap))
788 slot = NULL;
789
790 return slot;
791}
792
793static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
794{
a0a8eaba 795 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
936a5fe6
AA
796}
797
798static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
799{
800 int host_level, level, max_level;
05da4558 801
d25797b2
JR
802 host_level = host_mapping_level(vcpu->kvm, large_gfn);
803
804 if (host_level == PT_PAGE_TABLE_LEVEL)
805 return host_level;
806
878403b7
SY
807 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
808 kvm_x86_ops->get_lpage_level() : host_level;
809
810 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
d25797b2
JR
811 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
812 break;
d25797b2
JR
813
814 return level - 1;
05da4558
MT
815}
816
290fc38d 817/*
53c07b18 818 * Pte mapping structures:
cd4a4e53 819 *
53c07b18 820 * If pte_list bit zero is zero, then pte_list point to the spte.
cd4a4e53 821 *
53c07b18
XG
822 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
823 * pte_list_desc containing more mappings.
53a27b39 824 *
53c07b18 825 * Returns the number of pte entries before the spte was added or zero if
53a27b39
MT
826 * the spte was not added.
827 *
cd4a4e53 828 */
53c07b18
XG
829static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
830 unsigned long *pte_list)
cd4a4e53 831{
53c07b18 832 struct pte_list_desc *desc;
53a27b39 833 int i, count = 0;
cd4a4e53 834
53c07b18
XG
835 if (!*pte_list) {
836 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
837 *pte_list = (unsigned long)spte;
838 } else if (!(*pte_list & 1)) {
839 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
840 desc = mmu_alloc_pte_list_desc(vcpu);
841 desc->sptes[0] = (u64 *)*pte_list;
d555c333 842 desc->sptes[1] = spte;
53c07b18 843 *pte_list = (unsigned long)desc | 1;
cb16a7b3 844 ++count;
cd4a4e53 845 } else {
53c07b18
XG
846 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
847 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
848 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
cd4a4e53 849 desc = desc->more;
53c07b18 850 count += PTE_LIST_EXT;
53a27b39 851 }
53c07b18
XG
852 if (desc->sptes[PTE_LIST_EXT-1]) {
853 desc->more = mmu_alloc_pte_list_desc(vcpu);
cd4a4e53
AK
854 desc = desc->more;
855 }
d555c333 856 for (i = 0; desc->sptes[i]; ++i)
cb16a7b3 857 ++count;
d555c333 858 desc->sptes[i] = spte;
cd4a4e53 859 }
53a27b39 860 return count;
cd4a4e53
AK
861}
862
53c07b18
XG
863static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
864{
865 struct pte_list_desc *desc;
866 u64 *prev_spte;
867 int i;
868
869 if (!*pte_list)
870 return NULL;
871 else if (!(*pte_list & 1)) {
872 if (!spte)
873 return (u64 *)*pte_list;
874 return NULL;
875 }
876 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
877 prev_spte = NULL;
878 while (desc) {
879 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
880 if (prev_spte == spte)
881 return desc->sptes[i];
882 prev_spte = desc->sptes[i];
883 }
884 desc = desc->more;
885 }
886 return NULL;
887}
888
889static void
890pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
891 int i, struct pte_list_desc *prev_desc)
cd4a4e53
AK
892{
893 int j;
894
53c07b18 895 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
cd4a4e53 896 ;
d555c333
AK
897 desc->sptes[i] = desc->sptes[j];
898 desc->sptes[j] = NULL;
cd4a4e53
AK
899 if (j != 0)
900 return;
901 if (!prev_desc && !desc->more)
53c07b18 902 *pte_list = (unsigned long)desc->sptes[0];
cd4a4e53
AK
903 else
904 if (prev_desc)
905 prev_desc->more = desc->more;
906 else
53c07b18
XG
907 *pte_list = (unsigned long)desc->more | 1;
908 mmu_free_pte_list_desc(desc);
cd4a4e53
AK
909}
910
53c07b18 911static void pte_list_remove(u64 *spte, unsigned long *pte_list)
cd4a4e53 912{
53c07b18
XG
913 struct pte_list_desc *desc;
914 struct pte_list_desc *prev_desc;
cd4a4e53
AK
915 int i;
916
53c07b18
XG
917 if (!*pte_list) {
918 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
cd4a4e53 919 BUG();
53c07b18
XG
920 } else if (!(*pte_list & 1)) {
921 rmap_printk("pte_list_remove: %p 1->0\n", spte);
922 if ((u64 *)*pte_list != spte) {
923 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
cd4a4e53
AK
924 BUG();
925 }
53c07b18 926 *pte_list = 0;
cd4a4e53 927 } else {
53c07b18
XG
928 rmap_printk("pte_list_remove: %p many->many\n", spte);
929 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
cd4a4e53
AK
930 prev_desc = NULL;
931 while (desc) {
53c07b18 932 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
d555c333 933 if (desc->sptes[i] == spte) {
53c07b18 934 pte_list_desc_remove_entry(pte_list,
714b93da 935 desc, i,
cd4a4e53
AK
936 prev_desc);
937 return;
938 }
939 prev_desc = desc;
940 desc = desc->more;
941 }
53c07b18 942 pr_err("pte_list_remove: %p many->many\n", spte);
cd4a4e53
AK
943 BUG();
944 }
945}
946
67052b35
XG
947typedef void (*pte_list_walk_fn) (u64 *spte);
948static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
949{
950 struct pte_list_desc *desc;
951 int i;
952
953 if (!*pte_list)
954 return;
955
956 if (!(*pte_list & 1))
957 return fn((u64 *)*pte_list);
958
959 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
960 while (desc) {
961 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
962 fn(desc->sptes[i]);
963 desc = desc->more;
964 }
965}
966
9b9b1492
TY
967static unsigned long *__gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level,
968 struct kvm_memory_slot *slot)
53c07b18 969{
53c07b18
XG
970 struct kvm_lpage_info *linfo;
971
53c07b18
XG
972 if (likely(level == PT_PAGE_TABLE_LEVEL))
973 return &slot->rmap[gfn - slot->base_gfn];
974
975 linfo = lpage_info_slot(gfn, slot, level);
53c07b18
XG
976 return &linfo->rmap_pde;
977}
978
9b9b1492
TY
979/*
980 * Take gfn and return the reverse mapping to it.
981 */
982static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
983{
984 struct kvm_memory_slot *slot;
985
986 slot = gfn_to_memslot(kvm, gfn);
987 return __gfn_to_rmap(kvm, gfn, level, slot);
988}
989
f759e2b4
XG
990static bool rmap_can_add(struct kvm_vcpu *vcpu)
991{
992 struct kvm_mmu_memory_cache *cache;
993
994 cache = &vcpu->arch.mmu_pte_list_desc_cache;
995 return mmu_memory_cache_free_objects(cache);
996}
997
53c07b18
XG
998static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
999{
1000 struct kvm_mmu_page *sp;
1001 unsigned long *rmapp;
1002
53c07b18
XG
1003 sp = page_header(__pa(spte));
1004 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
1005 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
1006 return pte_list_add(vcpu, spte, rmapp);
1007}
1008
1009static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
1010{
1011 return pte_list_next(rmapp, spte);
1012}
1013
1014static void rmap_remove(struct kvm *kvm, u64 *spte)
1015{
1016 struct kvm_mmu_page *sp;
1017 gfn_t gfn;
1018 unsigned long *rmapp;
1019
1020 sp = page_header(__pa(spte));
1021 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
1022 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
1023 pte_list_remove(spte, rmapp);
1024}
1025
c3707958 1026static void drop_spte(struct kvm *kvm, u64 *sptep)
e4b502ea 1027{
1df9f2dc 1028 if (mmu_spte_clear_track_bits(sptep))
eb45fda4 1029 rmap_remove(kvm, sptep);
be38d276
AK
1030}
1031
95d4c16c
TY
1032int kvm_mmu_rmap_write_protect(struct kvm *kvm, u64 gfn,
1033 struct kvm_memory_slot *slot)
98348e95 1034{
290fc38d 1035 unsigned long *rmapp;
374cbac0 1036 u64 *spte;
44ad9944 1037 int i, write_protected = 0;
374cbac0 1038
9b9b1492 1039 rmapp = __gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL, slot);
98348e95
IE
1040 spte = rmap_next(kvm, rmapp, NULL);
1041 while (spte) {
374cbac0 1042 BUG_ON(!(*spte & PT_PRESENT_MASK));
374cbac0 1043 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
8dae4445 1044 if (is_writable_pte(*spte)) {
1df9f2dc 1045 mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK);
caa5b8a5
ED
1046 write_protected = 1;
1047 }
9647c14c 1048 spte = rmap_next(kvm, rmapp, spte);
374cbac0 1049 }
855149aa 1050
05da4558 1051 /* check for huge page mappings */
44ad9944
JR
1052 for (i = PT_DIRECTORY_LEVEL;
1053 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
9b9b1492 1054 rmapp = __gfn_to_rmap(kvm, gfn, i, slot);
44ad9944
JR
1055 spte = rmap_next(kvm, rmapp, NULL);
1056 while (spte) {
44ad9944 1057 BUG_ON(!(*spte & PT_PRESENT_MASK));
d6eebf8b 1058 BUG_ON(!is_large_pte(*spte));
44ad9944 1059 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
8dae4445 1060 if (is_writable_pte(*spte)) {
c3707958 1061 drop_spte(kvm, spte);
44ad9944 1062 --kvm->stat.lpages;
44ad9944
JR
1063 spte = NULL;
1064 write_protected = 1;
1065 }
1066 spte = rmap_next(kvm, rmapp, spte);
05da4558 1067 }
05da4558
MT
1068 }
1069
b1a36821 1070 return write_protected;
374cbac0
AK
1071}
1072
95d4c16c
TY
1073static int rmap_write_protect(struct kvm *kvm, u64 gfn)
1074{
1075 struct kvm_memory_slot *slot;
1076
1077 slot = gfn_to_memslot(kvm, gfn);
1078 return kvm_mmu_rmap_write_protect(kvm, gfn, slot);
1079}
1080
8a8365c5
FD
1081static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
1082 unsigned long data)
e930bffe
AA
1083{
1084 u64 *spte;
1085 int need_tlb_flush = 0;
1086
1087 while ((spte = rmap_next(kvm, rmapp, NULL))) {
1088 BUG_ON(!(*spte & PT_PRESENT_MASK));
1089 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
c3707958 1090 drop_spte(kvm, spte);
e930bffe
AA
1091 need_tlb_flush = 1;
1092 }
1093 return need_tlb_flush;
1094}
1095
8a8365c5
FD
1096static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
1097 unsigned long data)
3da0dd43
IE
1098{
1099 int need_flush = 0;
e4b502ea 1100 u64 *spte, new_spte;
3da0dd43
IE
1101 pte_t *ptep = (pte_t *)data;
1102 pfn_t new_pfn;
1103
1104 WARN_ON(pte_huge(*ptep));
1105 new_pfn = pte_pfn(*ptep);
1106 spte = rmap_next(kvm, rmapp, NULL);
1107 while (spte) {
1108 BUG_ON(!is_shadow_present_pte(*spte));
1109 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
1110 need_flush = 1;
1111 if (pte_write(*ptep)) {
c3707958 1112 drop_spte(kvm, spte);
3da0dd43
IE
1113 spte = rmap_next(kvm, rmapp, NULL);
1114 } else {
1115 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
1116 new_spte |= (u64)new_pfn << PAGE_SHIFT;
1117
1118 new_spte &= ~PT_WRITABLE_MASK;
1119 new_spte &= ~SPTE_HOST_WRITEABLE;
b79b93f9 1120 new_spte &= ~shadow_accessed_mask;
1df9f2dc
XG
1121 mmu_spte_clear_track_bits(spte);
1122 mmu_spte_set(spte, new_spte);
3da0dd43
IE
1123 spte = rmap_next(kvm, rmapp, spte);
1124 }
1125 }
1126 if (need_flush)
1127 kvm_flush_remote_tlbs(kvm);
1128
1129 return 0;
1130}
1131
8a8365c5
FD
1132static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
1133 unsigned long data,
3da0dd43 1134 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
8a8365c5 1135 unsigned long data))
e930bffe 1136{
be6ba0f0 1137 int j;
90bb6fc5 1138 int ret;
e930bffe 1139 int retval = 0;
bc6678a3 1140 struct kvm_memslots *slots;
be6ba0f0 1141 struct kvm_memory_slot *memslot;
bc6678a3 1142
90d83dc3 1143 slots = kvm_memslots(kvm);
e930bffe 1144
be6ba0f0 1145 kvm_for_each_memslot(memslot, slots) {
e930bffe
AA
1146 unsigned long start = memslot->userspace_addr;
1147 unsigned long end;
1148
e930bffe
AA
1149 end = start + (memslot->npages << PAGE_SHIFT);
1150 if (hva >= start && hva < end) {
1151 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
d4dbf470 1152 gfn_t gfn = memslot->base_gfn + gfn_offset;
852e3c19 1153
90bb6fc5 1154 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
852e3c19
JR
1155
1156 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
d4dbf470
TY
1157 struct kvm_lpage_info *linfo;
1158
1159 linfo = lpage_info_slot(gfn, memslot,
1160 PT_DIRECTORY_LEVEL + j);
1161 ret |= handler(kvm, &linfo->rmap_pde, data);
852e3c19 1162 }
90bb6fc5
AK
1163 trace_kvm_age_page(hva, memslot, ret);
1164 retval |= ret;
e930bffe
AA
1165 }
1166 }
1167
1168 return retval;
1169}
1170
1171int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1172{
3da0dd43
IE
1173 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
1174}
1175
1176void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1177{
8a8365c5 1178 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
e930bffe
AA
1179}
1180
8a8365c5
FD
1181static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1182 unsigned long data)
e930bffe
AA
1183{
1184 u64 *spte;
1185 int young = 0;
1186
6316e1c8
RR
1187 /*
1188 * Emulate the accessed bit for EPT, by checking if this page has
1189 * an EPT mapping, and clearing it if it does. On the next access,
1190 * a new EPT mapping will be established.
1191 * This has some overhead, but not as much as the cost of swapping
1192 * out actively used pages or breaking up actively used hugepages.
1193 */
534e38b4 1194 if (!shadow_accessed_mask)
6316e1c8 1195 return kvm_unmap_rmapp(kvm, rmapp, data);
534e38b4 1196
e930bffe
AA
1197 spte = rmap_next(kvm, rmapp, NULL);
1198 while (spte) {
1199 int _young;
1200 u64 _spte = *spte;
1201 BUG_ON(!(_spte & PT_PRESENT_MASK));
1202 _young = _spte & PT_ACCESSED_MASK;
1203 if (_young) {
1204 young = 1;
1205 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1206 }
1207 spte = rmap_next(kvm, rmapp, spte);
1208 }
1209 return young;
1210}
1211
8ee53820
AA
1212static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1213 unsigned long data)
1214{
1215 u64 *spte;
1216 int young = 0;
1217
1218 /*
1219 * If there's no access bit in the secondary pte set by the
1220 * hardware it's up to gup-fast/gup to set the access bit in
1221 * the primary pte or in the page structure.
1222 */
1223 if (!shadow_accessed_mask)
1224 goto out;
1225
1226 spte = rmap_next(kvm, rmapp, NULL);
1227 while (spte) {
1228 u64 _spte = *spte;
1229 BUG_ON(!(_spte & PT_PRESENT_MASK));
1230 young = _spte & PT_ACCESSED_MASK;
1231 if (young) {
1232 young = 1;
1233 break;
1234 }
1235 spte = rmap_next(kvm, rmapp, spte);
1236 }
1237out:
1238 return young;
1239}
1240
53a27b39
MT
1241#define RMAP_RECYCLE_THRESHOLD 1000
1242
852e3c19 1243static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
53a27b39
MT
1244{
1245 unsigned long *rmapp;
852e3c19
JR
1246 struct kvm_mmu_page *sp;
1247
1248 sp = page_header(__pa(spte));
53a27b39 1249
852e3c19 1250 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
53a27b39 1251
3da0dd43 1252 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
53a27b39
MT
1253 kvm_flush_remote_tlbs(vcpu->kvm);
1254}
1255
e930bffe
AA
1256int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1257{
3da0dd43 1258 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
e930bffe
AA
1259}
1260
8ee53820
AA
1261int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1262{
1263 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1264}
1265
d6c69ee9 1266#ifdef MMU_DEBUG
47ad8e68 1267static int is_empty_shadow_page(u64 *spt)
6aa8b732 1268{
139bdb2d
AK
1269 u64 *pos;
1270 u64 *end;
1271
47ad8e68 1272 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
3c915510 1273 if (is_shadow_present_pte(*pos)) {
b8688d51 1274 printk(KERN_ERR "%s: %p %llx\n", __func__,
139bdb2d 1275 pos, *pos);
6aa8b732 1276 return 0;
139bdb2d 1277 }
6aa8b732
AK
1278 return 1;
1279}
d6c69ee9 1280#endif
6aa8b732 1281
45221ab6
DH
1282/*
1283 * This value is the sum of all of the kvm instances's
1284 * kvm->arch.n_used_mmu_pages values. We need a global,
1285 * aggregate version in order to make the slab shrinker
1286 * faster
1287 */
1288static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1289{
1290 kvm->arch.n_used_mmu_pages += nr;
1291 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1292}
1293
bd4c86ea
XG
1294/*
1295 * Remove the sp from shadow page cache, after call it,
1296 * we can not find this sp from the cache, and the shadow
1297 * page table is still valid.
1298 * It should be under the protection of mmu lock.
1299 */
1300static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
260746c0 1301{
4db35314 1302 ASSERT(is_empty_shadow_page(sp->spt));
7775834a 1303 hlist_del(&sp->hash_link);
2032a93d 1304 if (!sp->role.direct)
842f22ed 1305 free_page((unsigned long)sp->gfns);
bd4c86ea
XG
1306}
1307
1308/*
1309 * Free the shadow page table and the sp, we can do it
1310 * out of the protection of mmu lock.
1311 */
1312static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
1313{
1314 list_del(&sp->link);
1315 free_page((unsigned long)sp->spt);
e8ad9a70 1316 kmem_cache_free(mmu_page_header_cache, sp);
260746c0
AK
1317}
1318
cea0f0e7
AK
1319static unsigned kvm_page_table_hashfn(gfn_t gfn)
1320{
1ae0a13d 1321 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
cea0f0e7
AK
1322}
1323
714b93da 1324static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
4db35314 1325 struct kvm_mmu_page *sp, u64 *parent_pte)
cea0f0e7 1326{
cea0f0e7
AK
1327 if (!parent_pte)
1328 return;
cea0f0e7 1329
67052b35 1330 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
cea0f0e7
AK
1331}
1332
4db35314 1333static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
cea0f0e7
AK
1334 u64 *parent_pte)
1335{
67052b35 1336 pte_list_remove(parent_pte, &sp->parent_ptes);
cea0f0e7
AK
1337}
1338
bcdd9a93
XG
1339static void drop_parent_pte(struct kvm_mmu_page *sp,
1340 u64 *parent_pte)
1341{
1342 mmu_page_remove_parent_pte(sp, parent_pte);
1df9f2dc 1343 mmu_spte_clear_no_track(parent_pte);
bcdd9a93
XG
1344}
1345
67052b35
XG
1346static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
1347 u64 *parent_pte, int direct)
ad8cfbe3 1348{
67052b35
XG
1349 struct kvm_mmu_page *sp;
1350 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1351 sizeof *sp);
1352 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
1353 if (!direct)
1354 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1355 PAGE_SIZE);
1356 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
1357 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
93a5cef0 1358 bitmap_zero(sp->slot_bitmap, KVM_MEM_SLOTS_NUM);
67052b35
XG
1359 sp->parent_ptes = 0;
1360 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1361 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
1362 return sp;
ad8cfbe3
MT
1363}
1364
67052b35 1365static void mark_unsync(u64 *spte);
1047df1f 1366static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
0074ff63 1367{
67052b35 1368 pte_list_walk(&sp->parent_ptes, mark_unsync);
0074ff63
MT
1369}
1370
67052b35 1371static void mark_unsync(u64 *spte)
0074ff63 1372{
67052b35 1373 struct kvm_mmu_page *sp;
1047df1f 1374 unsigned int index;
0074ff63 1375
67052b35 1376 sp = page_header(__pa(spte));
1047df1f
XG
1377 index = spte - sp->spt;
1378 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
0074ff63 1379 return;
1047df1f 1380 if (sp->unsync_children++)
0074ff63 1381 return;
1047df1f 1382 kvm_mmu_mark_parents_unsync(sp);
0074ff63
MT
1383}
1384
e8bc217a 1385static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
a4a8e6f7 1386 struct kvm_mmu_page *sp)
e8bc217a
MT
1387{
1388 return 1;
1389}
1390
a7052897
MT
1391static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1392{
1393}
1394
0f53b5b1
XG
1395static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1396 struct kvm_mmu_page *sp, u64 *spte,
7c562522 1397 const void *pte)
0f53b5b1
XG
1398{
1399 WARN_ON(1);
1400}
1401
60c8aec6
MT
1402#define KVM_PAGE_ARRAY_NR 16
1403
1404struct kvm_mmu_pages {
1405 struct mmu_page_and_offset {
1406 struct kvm_mmu_page *sp;
1407 unsigned int idx;
1408 } page[KVM_PAGE_ARRAY_NR];
1409 unsigned int nr;
1410};
1411
0074ff63
MT
1412#define for_each_unsync_children(bitmap, idx) \
1413 for (idx = find_first_bit(bitmap, 512); \
1414 idx < 512; \
1415 idx = find_next_bit(bitmap, 512, idx+1))
1416
cded19f3
HE
1417static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1418 int idx)
4731d4c7 1419{
60c8aec6 1420 int i;
4731d4c7 1421
60c8aec6
MT
1422 if (sp->unsync)
1423 for (i=0; i < pvec->nr; i++)
1424 if (pvec->page[i].sp == sp)
1425 return 0;
1426
1427 pvec->page[pvec->nr].sp = sp;
1428 pvec->page[pvec->nr].idx = idx;
1429 pvec->nr++;
1430 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1431}
1432
1433static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1434 struct kvm_mmu_pages *pvec)
1435{
1436 int i, ret, nr_unsync_leaf = 0;
4731d4c7 1437
0074ff63 1438 for_each_unsync_children(sp->unsync_child_bitmap, i) {
7a8f1a74 1439 struct kvm_mmu_page *child;
4731d4c7
MT
1440 u64 ent = sp->spt[i];
1441
7a8f1a74
XG
1442 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1443 goto clear_child_bitmap;
1444
1445 child = page_header(ent & PT64_BASE_ADDR_MASK);
1446
1447 if (child->unsync_children) {
1448 if (mmu_pages_add(pvec, child, i))
1449 return -ENOSPC;
1450
1451 ret = __mmu_unsync_walk(child, pvec);
1452 if (!ret)
1453 goto clear_child_bitmap;
1454 else if (ret > 0)
1455 nr_unsync_leaf += ret;
1456 else
1457 return ret;
1458 } else if (child->unsync) {
1459 nr_unsync_leaf++;
1460 if (mmu_pages_add(pvec, child, i))
1461 return -ENOSPC;
1462 } else
1463 goto clear_child_bitmap;
1464
1465 continue;
1466
1467clear_child_bitmap:
1468 __clear_bit(i, sp->unsync_child_bitmap);
1469 sp->unsync_children--;
1470 WARN_ON((int)sp->unsync_children < 0);
4731d4c7
MT
1471 }
1472
4731d4c7 1473
60c8aec6
MT
1474 return nr_unsync_leaf;
1475}
1476
1477static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1478 struct kvm_mmu_pages *pvec)
1479{
1480 if (!sp->unsync_children)
1481 return 0;
1482
1483 mmu_pages_add(pvec, sp, 0);
1484 return __mmu_unsync_walk(sp, pvec);
4731d4c7
MT
1485}
1486
4731d4c7
MT
1487static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1488{
1489 WARN_ON(!sp->unsync);
5e1b3ddb 1490 trace_kvm_mmu_sync_page(sp);
4731d4c7
MT
1491 sp->unsync = 0;
1492 --kvm->stat.mmu_unsync;
1493}
1494
7775834a
XG
1495static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1496 struct list_head *invalid_list);
1497static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1498 struct list_head *invalid_list);
4731d4c7 1499
f41d335a
XG
1500#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1501 hlist_for_each_entry(sp, pos, \
7ae680eb
XG
1502 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1503 if ((sp)->gfn != (gfn)) {} else
1504
f41d335a
XG
1505#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1506 hlist_for_each_entry(sp, pos, \
7ae680eb
XG
1507 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1508 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1509 (sp)->role.invalid) {} else
1510
f918b443 1511/* @sp->gfn should be write-protected at the call site */
1d9dc7e0 1512static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
d98ba053 1513 struct list_head *invalid_list, bool clear_unsync)
4731d4c7 1514{
5b7e0102 1515 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
d98ba053 1516 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
4731d4c7
MT
1517 return 1;
1518 }
1519
f918b443 1520 if (clear_unsync)
1d9dc7e0 1521 kvm_unlink_unsync_page(vcpu->kvm, sp);
1d9dc7e0 1522
a4a8e6f7 1523 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
d98ba053 1524 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
4731d4c7
MT
1525 return 1;
1526 }
1527
1528 kvm_mmu_flush_tlb(vcpu);
4731d4c7
MT
1529 return 0;
1530}
1531
1d9dc7e0
XG
1532static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1533 struct kvm_mmu_page *sp)
1534{
d98ba053 1535 LIST_HEAD(invalid_list);
1d9dc7e0
XG
1536 int ret;
1537
d98ba053 1538 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
be71e061 1539 if (ret)
d98ba053
XG
1540 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1541
1d9dc7e0
XG
1542 return ret;
1543}
1544
d98ba053
XG
1545static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1546 struct list_head *invalid_list)
1d9dc7e0 1547{
d98ba053 1548 return __kvm_sync_page(vcpu, sp, invalid_list, true);
1d9dc7e0
XG
1549}
1550
9f1a122f
XG
1551/* @gfn should be write-protected at the call site */
1552static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1553{
9f1a122f 1554 struct kvm_mmu_page *s;
f41d335a 1555 struct hlist_node *node;
d98ba053 1556 LIST_HEAD(invalid_list);
9f1a122f
XG
1557 bool flush = false;
1558
f41d335a 1559 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
7ae680eb 1560 if (!s->unsync)
9f1a122f
XG
1561 continue;
1562
1563 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
a4a8e6f7 1564 kvm_unlink_unsync_page(vcpu->kvm, s);
9f1a122f 1565 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
a4a8e6f7 1566 (vcpu->arch.mmu.sync_page(vcpu, s))) {
d98ba053 1567 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
9f1a122f
XG
1568 continue;
1569 }
9f1a122f
XG
1570 flush = true;
1571 }
1572
d98ba053 1573 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
9f1a122f
XG
1574 if (flush)
1575 kvm_mmu_flush_tlb(vcpu);
1576}
1577
60c8aec6
MT
1578struct mmu_page_path {
1579 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1580 unsigned int idx[PT64_ROOT_LEVEL-1];
4731d4c7
MT
1581};
1582
60c8aec6
MT
1583#define for_each_sp(pvec, sp, parents, i) \
1584 for (i = mmu_pages_next(&pvec, &parents, -1), \
1585 sp = pvec.page[i].sp; \
1586 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1587 i = mmu_pages_next(&pvec, &parents, i))
1588
cded19f3
HE
1589static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1590 struct mmu_page_path *parents,
1591 int i)
60c8aec6
MT
1592{
1593 int n;
1594
1595 for (n = i+1; n < pvec->nr; n++) {
1596 struct kvm_mmu_page *sp = pvec->page[n].sp;
1597
1598 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1599 parents->idx[0] = pvec->page[n].idx;
1600 return n;
1601 }
1602
1603 parents->parent[sp->role.level-2] = sp;
1604 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1605 }
1606
1607 return n;
1608}
1609
cded19f3 1610static void mmu_pages_clear_parents(struct mmu_page_path *parents)
4731d4c7 1611{
60c8aec6
MT
1612 struct kvm_mmu_page *sp;
1613 unsigned int level = 0;
1614
1615 do {
1616 unsigned int idx = parents->idx[level];
4731d4c7 1617
60c8aec6
MT
1618 sp = parents->parent[level];
1619 if (!sp)
1620 return;
1621
1622 --sp->unsync_children;
1623 WARN_ON((int)sp->unsync_children < 0);
1624 __clear_bit(idx, sp->unsync_child_bitmap);
1625 level++;
1626 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
4731d4c7
MT
1627}
1628
60c8aec6
MT
1629static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1630 struct mmu_page_path *parents,
1631 struct kvm_mmu_pages *pvec)
4731d4c7 1632{
60c8aec6
MT
1633 parents->parent[parent->role.level-1] = NULL;
1634 pvec->nr = 0;
1635}
4731d4c7 1636
60c8aec6
MT
1637static void mmu_sync_children(struct kvm_vcpu *vcpu,
1638 struct kvm_mmu_page *parent)
1639{
1640 int i;
1641 struct kvm_mmu_page *sp;
1642 struct mmu_page_path parents;
1643 struct kvm_mmu_pages pages;
d98ba053 1644 LIST_HEAD(invalid_list);
60c8aec6
MT
1645
1646 kvm_mmu_pages_init(parent, &parents, &pages);
1647 while (mmu_unsync_walk(parent, &pages)) {
b1a36821
MT
1648 int protected = 0;
1649
1650 for_each_sp(pages, sp, parents, i)
1651 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1652
1653 if (protected)
1654 kvm_flush_remote_tlbs(vcpu->kvm);
1655
60c8aec6 1656 for_each_sp(pages, sp, parents, i) {
d98ba053 1657 kvm_sync_page(vcpu, sp, &invalid_list);
60c8aec6
MT
1658 mmu_pages_clear_parents(&parents);
1659 }
d98ba053 1660 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
4731d4c7 1661 cond_resched_lock(&vcpu->kvm->mmu_lock);
60c8aec6
MT
1662 kvm_mmu_pages_init(parent, &parents, &pages);
1663 }
4731d4c7
MT
1664}
1665
c3707958
XG
1666static void init_shadow_page_table(struct kvm_mmu_page *sp)
1667{
1668 int i;
1669
1670 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1671 sp->spt[i] = 0ull;
1672}
1673
a30f47cb
XG
1674static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
1675{
1676 sp->write_flooding_count = 0;
1677}
1678
1679static void clear_sp_write_flooding_count(u64 *spte)
1680{
1681 struct kvm_mmu_page *sp = page_header(__pa(spte));
1682
1683 __clear_sp_write_flooding_count(sp);
1684}
1685
cea0f0e7
AK
1686static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1687 gfn_t gfn,
1688 gva_t gaddr,
1689 unsigned level,
f6e2c02b 1690 int direct,
41074d07 1691 unsigned access,
f7d9c7b7 1692 u64 *parent_pte)
cea0f0e7
AK
1693{
1694 union kvm_mmu_page_role role;
cea0f0e7 1695 unsigned quadrant;
9f1a122f 1696 struct kvm_mmu_page *sp;
f41d335a 1697 struct hlist_node *node;
9f1a122f 1698 bool need_sync = false;
cea0f0e7 1699
a770f6f2 1700 role = vcpu->arch.mmu.base_role;
cea0f0e7 1701 role.level = level;
f6e2c02b 1702 role.direct = direct;
84b0c8c6 1703 if (role.direct)
5b7e0102 1704 role.cr4_pae = 0;
41074d07 1705 role.access = access;
c5a78f2b
JR
1706 if (!vcpu->arch.mmu.direct_map
1707 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
cea0f0e7
AK
1708 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1709 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1710 role.quadrant = quadrant;
1711 }
f41d335a 1712 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
7ae680eb
XG
1713 if (!need_sync && sp->unsync)
1714 need_sync = true;
4731d4c7 1715
7ae680eb
XG
1716 if (sp->role.word != role.word)
1717 continue;
4731d4c7 1718
7ae680eb
XG
1719 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1720 break;
e02aa901 1721
7ae680eb
XG
1722 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1723 if (sp->unsync_children) {
a8eeb04a 1724 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
7ae680eb
XG
1725 kvm_mmu_mark_parents_unsync(sp);
1726 } else if (sp->unsync)
1727 kvm_mmu_mark_parents_unsync(sp);
e02aa901 1728
a30f47cb 1729 __clear_sp_write_flooding_count(sp);
7ae680eb
XG
1730 trace_kvm_mmu_get_page(sp, false);
1731 return sp;
1732 }
dfc5aa00 1733 ++vcpu->kvm->stat.mmu_cache_miss;
2032a93d 1734 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
4db35314
AK
1735 if (!sp)
1736 return sp;
4db35314
AK
1737 sp->gfn = gfn;
1738 sp->role = role;
7ae680eb
XG
1739 hlist_add_head(&sp->hash_link,
1740 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
f6e2c02b 1741 if (!direct) {
b1a36821
MT
1742 if (rmap_write_protect(vcpu->kvm, gfn))
1743 kvm_flush_remote_tlbs(vcpu->kvm);
9f1a122f
XG
1744 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1745 kvm_sync_pages(vcpu, gfn);
1746
4731d4c7
MT
1747 account_shadowed(vcpu->kvm, gfn);
1748 }
c3707958 1749 init_shadow_page_table(sp);
f691fe1d 1750 trace_kvm_mmu_get_page(sp, true);
4db35314 1751 return sp;
cea0f0e7
AK
1752}
1753
2d11123a
AK
1754static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1755 struct kvm_vcpu *vcpu, u64 addr)
1756{
1757 iterator->addr = addr;
1758 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1759 iterator->level = vcpu->arch.mmu.shadow_root_level;
81407ca5
JR
1760
1761 if (iterator->level == PT64_ROOT_LEVEL &&
1762 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1763 !vcpu->arch.mmu.direct_map)
1764 --iterator->level;
1765
2d11123a
AK
1766 if (iterator->level == PT32E_ROOT_LEVEL) {
1767 iterator->shadow_addr
1768 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1769 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1770 --iterator->level;
1771 if (!iterator->shadow_addr)
1772 iterator->level = 0;
1773 }
1774}
1775
1776static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1777{
1778 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1779 return false;
4d88954d 1780
2d11123a
AK
1781 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1782 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1783 return true;
1784}
1785
c2a2ac2b
XG
1786static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
1787 u64 spte)
2d11123a 1788{
c2a2ac2b 1789 if (is_last_spte(spte, iterator->level)) {
052331be
XG
1790 iterator->level = 0;
1791 return;
1792 }
1793
c2a2ac2b 1794 iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
2d11123a
AK
1795 --iterator->level;
1796}
1797
c2a2ac2b
XG
1798static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1799{
1800 return __shadow_walk_next(iterator, *iterator->sptep);
1801}
1802
32ef26a3
AK
1803static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1804{
1805 u64 spte;
1806
1807 spte = __pa(sp->spt)
1808 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1809 | PT_WRITABLE_MASK | PT_USER_MASK;
1df9f2dc 1810 mmu_spte_set(sptep, spte);
32ef26a3
AK
1811}
1812
a3aa51cf
AK
1813static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1814{
1815 if (is_large_pte(*sptep)) {
c3707958 1816 drop_spte(vcpu->kvm, sptep);
a3aa51cf
AK
1817 kvm_flush_remote_tlbs(vcpu->kvm);
1818 }
1819}
1820
a357bd22
AK
1821static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1822 unsigned direct_access)
1823{
1824 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1825 struct kvm_mmu_page *child;
1826
1827 /*
1828 * For the direct sp, if the guest pte's dirty bit
1829 * changed form clean to dirty, it will corrupt the
1830 * sp's access: allow writable in the read-only sp,
1831 * so we should update the spte at this point to get
1832 * a new sp with the correct access.
1833 */
1834 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1835 if (child->role.access == direct_access)
1836 return;
1837
bcdd9a93 1838 drop_parent_pte(child, sptep);
a357bd22
AK
1839 kvm_flush_remote_tlbs(vcpu->kvm);
1840 }
1841}
1842
505aef8f 1843static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
38e3b2b2
XG
1844 u64 *spte)
1845{
1846 u64 pte;
1847 struct kvm_mmu_page *child;
1848
1849 pte = *spte;
1850 if (is_shadow_present_pte(pte)) {
505aef8f 1851 if (is_last_spte(pte, sp->role.level)) {
c3707958 1852 drop_spte(kvm, spte);
505aef8f
XG
1853 if (is_large_pte(pte))
1854 --kvm->stat.lpages;
1855 } else {
38e3b2b2 1856 child = page_header(pte & PT64_BASE_ADDR_MASK);
bcdd9a93 1857 drop_parent_pte(child, spte);
38e3b2b2 1858 }
505aef8f
XG
1859 return true;
1860 }
1861
1862 if (is_mmio_spte(pte))
ce88decf 1863 mmu_spte_clear_no_track(spte);
c3707958 1864
505aef8f 1865 return false;
38e3b2b2
XG
1866}
1867
90cb0529 1868static void kvm_mmu_page_unlink_children(struct kvm *kvm,
4db35314 1869 struct kvm_mmu_page *sp)
a436036b 1870{
697fe2e2 1871 unsigned i;
697fe2e2 1872
38e3b2b2
XG
1873 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1874 mmu_page_zap_pte(kvm, sp, sp->spt + i);
a436036b
AK
1875}
1876
4db35314 1877static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
cea0f0e7 1878{
4db35314 1879 mmu_page_remove_parent_pte(sp, parent_pte);
a436036b
AK
1880}
1881
31aa2b44 1882static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
a436036b
AK
1883{
1884 u64 *parent_pte;
1885
bcdd9a93
XG
1886 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1887 drop_parent_pte(sp, parent_pte);
31aa2b44
AK
1888}
1889
60c8aec6 1890static int mmu_zap_unsync_children(struct kvm *kvm,
7775834a
XG
1891 struct kvm_mmu_page *parent,
1892 struct list_head *invalid_list)
4731d4c7 1893{
60c8aec6
MT
1894 int i, zapped = 0;
1895 struct mmu_page_path parents;
1896 struct kvm_mmu_pages pages;
4731d4c7 1897
60c8aec6 1898 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
4731d4c7 1899 return 0;
60c8aec6
MT
1900
1901 kvm_mmu_pages_init(parent, &parents, &pages);
1902 while (mmu_unsync_walk(parent, &pages)) {
1903 struct kvm_mmu_page *sp;
1904
1905 for_each_sp(pages, sp, parents, i) {
7775834a 1906 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
60c8aec6 1907 mmu_pages_clear_parents(&parents);
77662e00 1908 zapped++;
60c8aec6 1909 }
60c8aec6
MT
1910 kvm_mmu_pages_init(parent, &parents, &pages);
1911 }
1912
1913 return zapped;
4731d4c7
MT
1914}
1915
7775834a
XG
1916static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1917 struct list_head *invalid_list)
31aa2b44 1918{
4731d4c7 1919 int ret;
f691fe1d 1920
7775834a 1921 trace_kvm_mmu_prepare_zap_page(sp);
31aa2b44 1922 ++kvm->stat.mmu_shadow_zapped;
7775834a 1923 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
4db35314 1924 kvm_mmu_page_unlink_children(kvm, sp);
31aa2b44 1925 kvm_mmu_unlink_parents(kvm, sp);
f6e2c02b 1926 if (!sp->role.invalid && !sp->role.direct)
5b5c6a5a 1927 unaccount_shadowed(kvm, sp->gfn);
4731d4c7
MT
1928 if (sp->unsync)
1929 kvm_unlink_unsync_page(kvm, sp);
4db35314 1930 if (!sp->root_count) {
54a4f023
GJ
1931 /* Count self */
1932 ret++;
7775834a 1933 list_move(&sp->link, invalid_list);
aa6bd187 1934 kvm_mod_used_mmu_pages(kvm, -1);
2e53d63a 1935 } else {
5b5c6a5a 1936 list_move(&sp->link, &kvm->arch.active_mmu_pages);
2e53d63a
MT
1937 kvm_reload_remote_mmus(kvm);
1938 }
7775834a
XG
1939
1940 sp->role.invalid = 1;
4731d4c7 1941 return ret;
a436036b
AK
1942}
1943
c2a2ac2b
XG
1944static void kvm_mmu_isolate_pages(struct list_head *invalid_list)
1945{
1946 struct kvm_mmu_page *sp;
1947
1948 list_for_each_entry(sp, invalid_list, link)
1949 kvm_mmu_isolate_page(sp);
1950}
1951
1952static void free_pages_rcu(struct rcu_head *head)
1953{
1954 struct kvm_mmu_page *next, *sp;
1955
1956 sp = container_of(head, struct kvm_mmu_page, rcu);
1957 while (sp) {
1958 if (!list_empty(&sp->link))
1959 next = list_first_entry(&sp->link,
1960 struct kvm_mmu_page, link);
1961 else
1962 next = NULL;
1963 kvm_mmu_free_page(sp);
1964 sp = next;
1965 }
1966}
1967
7775834a
XG
1968static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1969 struct list_head *invalid_list)
1970{
1971 struct kvm_mmu_page *sp;
1972
1973 if (list_empty(invalid_list))
1974 return;
1975
1976 kvm_flush_remote_tlbs(kvm);
1977
c2a2ac2b
XG
1978 if (atomic_read(&kvm->arch.reader_counter)) {
1979 kvm_mmu_isolate_pages(invalid_list);
1980 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1981 list_del_init(invalid_list);
4f022648
XG
1982
1983 trace_kvm_mmu_delay_free_pages(sp);
c2a2ac2b
XG
1984 call_rcu(&sp->rcu, free_pages_rcu);
1985 return;
1986 }
1987
7775834a
XG
1988 do {
1989 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1990 WARN_ON(!sp->role.invalid || sp->root_count);
bd4c86ea 1991 kvm_mmu_isolate_page(sp);
aa6bd187 1992 kvm_mmu_free_page(sp);
7775834a
XG
1993 } while (!list_empty(invalid_list));
1994
1995}
1996
82ce2c96
IE
1997/*
1998 * Changing the number of mmu pages allocated to the vm
49d5ca26 1999 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
82ce2c96 2000 */
49d5ca26 2001void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
82ce2c96 2002{
d98ba053 2003 LIST_HEAD(invalid_list);
82ce2c96
IE
2004 /*
2005 * If we set the number of mmu pages to be smaller be than the
2006 * number of actived pages , we must to free some mmu pages before we
2007 * change the value
2008 */
2009
49d5ca26
DH
2010 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
2011 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
77662e00 2012 !list_empty(&kvm->arch.active_mmu_pages)) {
82ce2c96
IE
2013 struct kvm_mmu_page *page;
2014
f05e70ac 2015 page = container_of(kvm->arch.active_mmu_pages.prev,
82ce2c96 2016 struct kvm_mmu_page, link);
80b63faf 2017 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
82ce2c96 2018 }
aa6bd187 2019 kvm_mmu_commit_zap_page(kvm, &invalid_list);
49d5ca26 2020 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
82ce2c96 2021 }
82ce2c96 2022
49d5ca26 2023 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
82ce2c96
IE
2024}
2025
1cb3f3ae 2026int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
a436036b 2027{
4db35314 2028 struct kvm_mmu_page *sp;
f41d335a 2029 struct hlist_node *node;
d98ba053 2030 LIST_HEAD(invalid_list);
a436036b
AK
2031 int r;
2032
9ad17b10 2033 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
a436036b 2034 r = 0;
1cb3f3ae 2035 spin_lock(&kvm->mmu_lock);
f41d335a 2036 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
9ad17b10 2037 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
7ae680eb
XG
2038 sp->role.word);
2039 r = 1;
f41d335a 2040 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
7ae680eb 2041 }
d98ba053 2042 kvm_mmu_commit_zap_page(kvm, &invalid_list);
1cb3f3ae
XG
2043 spin_unlock(&kvm->mmu_lock);
2044
a436036b 2045 return r;
cea0f0e7 2046}
1cb3f3ae 2047EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
cea0f0e7 2048
38c335f1 2049static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
6aa8b732 2050{
bc6678a3 2051 int slot = memslot_id(kvm, gfn);
4db35314 2052 struct kvm_mmu_page *sp = page_header(__pa(pte));
6aa8b732 2053
291f26bc 2054 __set_bit(slot, sp->slot_bitmap);
6aa8b732
AK
2055}
2056
74be52e3
SY
2057/*
2058 * The function is based on mtrr_type_lookup() in
2059 * arch/x86/kernel/cpu/mtrr/generic.c
2060 */
2061static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
2062 u64 start, u64 end)
2063{
2064 int i;
2065 u64 base, mask;
2066 u8 prev_match, curr_match;
2067 int num_var_ranges = KVM_NR_VAR_MTRR;
2068
2069 if (!mtrr_state->enabled)
2070 return 0xFF;
2071
2072 /* Make end inclusive end, instead of exclusive */
2073 end--;
2074
2075 /* Look in fixed ranges. Just return the type as per start */
2076 if (mtrr_state->have_fixed && (start < 0x100000)) {
2077 int idx;
2078
2079 if (start < 0x80000) {
2080 idx = 0;
2081 idx += (start >> 16);
2082 return mtrr_state->fixed_ranges[idx];
2083 } else if (start < 0xC0000) {
2084 idx = 1 * 8;
2085 idx += ((start - 0x80000) >> 14);
2086 return mtrr_state->fixed_ranges[idx];
2087 } else if (start < 0x1000000) {
2088 idx = 3 * 8;
2089 idx += ((start - 0xC0000) >> 12);
2090 return mtrr_state->fixed_ranges[idx];
2091 }
2092 }
2093
2094 /*
2095 * Look in variable ranges
2096 * Look of multiple ranges matching this address and pick type
2097 * as per MTRR precedence
2098 */
2099 if (!(mtrr_state->enabled & 2))
2100 return mtrr_state->def_type;
2101
2102 prev_match = 0xFF;
2103 for (i = 0; i < num_var_ranges; ++i) {
2104 unsigned short start_state, end_state;
2105
2106 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
2107 continue;
2108
2109 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
2110 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
2111 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
2112 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
2113
2114 start_state = ((start & mask) == (base & mask));
2115 end_state = ((end & mask) == (base & mask));
2116 if (start_state != end_state)
2117 return 0xFE;
2118
2119 if ((start & mask) != (base & mask))
2120 continue;
2121
2122 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
2123 if (prev_match == 0xFF) {
2124 prev_match = curr_match;
2125 continue;
2126 }
2127
2128 if (prev_match == MTRR_TYPE_UNCACHABLE ||
2129 curr_match == MTRR_TYPE_UNCACHABLE)
2130 return MTRR_TYPE_UNCACHABLE;
2131
2132 if ((prev_match == MTRR_TYPE_WRBACK &&
2133 curr_match == MTRR_TYPE_WRTHROUGH) ||
2134 (prev_match == MTRR_TYPE_WRTHROUGH &&
2135 curr_match == MTRR_TYPE_WRBACK)) {
2136 prev_match = MTRR_TYPE_WRTHROUGH;
2137 curr_match = MTRR_TYPE_WRTHROUGH;
2138 }
2139
2140 if (prev_match != curr_match)
2141 return MTRR_TYPE_UNCACHABLE;
2142 }
2143
2144 if (prev_match != 0xFF)
2145 return prev_match;
2146
2147 return mtrr_state->def_type;
2148}
2149
4b12f0de 2150u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
74be52e3
SY
2151{
2152 u8 mtrr;
2153
2154 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
2155 (gfn << PAGE_SHIFT) + PAGE_SIZE);
2156 if (mtrr == 0xfe || mtrr == 0xff)
2157 mtrr = MTRR_TYPE_WRBACK;
2158 return mtrr;
2159}
4b12f0de 2160EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
74be52e3 2161
9cf5cf5a
XG
2162static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
2163{
2164 trace_kvm_mmu_unsync_page(sp);
2165 ++vcpu->kvm->stat.mmu_unsync;
2166 sp->unsync = 1;
2167
2168 kvm_mmu_mark_parents_unsync(sp);
9cf5cf5a
XG
2169}
2170
2171static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
4731d4c7 2172{
4731d4c7 2173 struct kvm_mmu_page *s;
f41d335a 2174 struct hlist_node *node;
9cf5cf5a 2175
f41d335a 2176 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
7ae680eb 2177 if (s->unsync)
4731d4c7 2178 continue;
9cf5cf5a
XG
2179 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
2180 __kvm_unsync_page(vcpu, s);
4731d4c7 2181 }
4731d4c7
MT
2182}
2183
2184static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
2185 bool can_unsync)
2186{
9cf5cf5a 2187 struct kvm_mmu_page *s;
f41d335a 2188 struct hlist_node *node;
9cf5cf5a
XG
2189 bool need_unsync = false;
2190
f41d335a 2191 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
36a2e677
XG
2192 if (!can_unsync)
2193 return 1;
2194
9cf5cf5a 2195 if (s->role.level != PT_PAGE_TABLE_LEVEL)
4731d4c7 2196 return 1;
9cf5cf5a
XG
2197
2198 if (!need_unsync && !s->unsync) {
36a2e677 2199 if (!oos_shadow)
9cf5cf5a
XG
2200 return 1;
2201 need_unsync = true;
2202 }
4731d4c7 2203 }
9cf5cf5a
XG
2204 if (need_unsync)
2205 kvm_unsync_pages(vcpu, gfn);
4731d4c7
MT
2206 return 0;
2207}
2208
d555c333 2209static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1e73f9dd 2210 unsigned pte_access, int user_fault,
640d9b0d 2211 int write_fault, int level,
c2d0ee46 2212 gfn_t gfn, pfn_t pfn, bool speculative,
9bdbba13 2213 bool can_unsync, bool host_writable)
1c4f1fd6 2214{
b330aa0c 2215 u64 spte, entry = *sptep;
1e73f9dd 2216 int ret = 0;
64d4d521 2217
ce88decf
XG
2218 if (set_mmio_spte(sptep, gfn, pfn, pte_access))
2219 return 0;
2220
982c2565 2221 spte = PT_PRESENT_MASK;
947da538 2222 if (!speculative)
3201b5d9 2223 spte |= shadow_accessed_mask;
640d9b0d 2224
7b52345e
SY
2225 if (pte_access & ACC_EXEC_MASK)
2226 spte |= shadow_x_mask;
2227 else
2228 spte |= shadow_nx_mask;
1c4f1fd6 2229 if (pte_access & ACC_USER_MASK)
7b52345e 2230 spte |= shadow_user_mask;
852e3c19 2231 if (level > PT_PAGE_TABLE_LEVEL)
05da4558 2232 spte |= PT_PAGE_SIZE_MASK;
b0bc3ee2 2233 if (tdp_enabled)
4b12f0de
SY
2234 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
2235 kvm_is_mmio_pfn(pfn));
1c4f1fd6 2236
9bdbba13 2237 if (host_writable)
1403283a 2238 spte |= SPTE_HOST_WRITEABLE;
f8e453b0
XG
2239 else
2240 pte_access &= ~ACC_WRITE_MASK;
1403283a 2241
35149e21 2242 spte |= (u64)pfn << PAGE_SHIFT;
1c4f1fd6
AK
2243
2244 if ((pte_access & ACC_WRITE_MASK)
c5a78f2b
JR
2245 || (!vcpu->arch.mmu.direct_map && write_fault
2246 && !is_write_protection(vcpu) && !user_fault)) {
1c4f1fd6 2247
852e3c19
JR
2248 if (level > PT_PAGE_TABLE_LEVEL &&
2249 has_wrprotected_page(vcpu->kvm, gfn, level)) {
38187c83 2250 ret = 1;
c3707958 2251 drop_spte(vcpu->kvm, sptep);
be38d276 2252 goto done;
38187c83
MT
2253 }
2254
1c4f1fd6 2255 spte |= PT_WRITABLE_MASK;
1c4f1fd6 2256
c5a78f2b 2257 if (!vcpu->arch.mmu.direct_map
411c588d 2258 && !(pte_access & ACC_WRITE_MASK)) {
69325a12 2259 spte &= ~PT_USER_MASK;
411c588d
AK
2260 /*
2261 * If we converted a user page to a kernel page,
2262 * so that the kernel can write to it when cr0.wp=0,
2263 * then we should prevent the kernel from executing it
2264 * if SMEP is enabled.
2265 */
2266 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
2267 spte |= PT64_NX_MASK;
2268 }
69325a12 2269
ecc5589f
MT
2270 /*
2271 * Optimization: for pte sync, if spte was writable the hash
2272 * lookup is unnecessary (and expensive). Write protection
2273 * is responsibility of mmu_get_page / kvm_sync_page.
2274 * Same reasoning can be applied to dirty page accounting.
2275 */
8dae4445 2276 if (!can_unsync && is_writable_pte(*sptep))
ecc5589f
MT
2277 goto set_pte;
2278
4731d4c7 2279 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
9ad17b10 2280 pgprintk("%s: found shadow page for %llx, marking ro\n",
b8688d51 2281 __func__, gfn);
1e73f9dd 2282 ret = 1;
1c4f1fd6 2283 pte_access &= ~ACC_WRITE_MASK;
8dae4445 2284 if (is_writable_pte(spte))
1c4f1fd6 2285 spte &= ~PT_WRITABLE_MASK;
1c4f1fd6
AK
2286 }
2287 }
2288
1c4f1fd6
AK
2289 if (pte_access & ACC_WRITE_MASK)
2290 mark_page_dirty(vcpu->kvm, gfn);
2291
38187c83 2292set_pte:
1df9f2dc 2293 mmu_spte_update(sptep, spte);
b330aa0c
XG
2294 /*
2295 * If we overwrite a writable spte with a read-only one we
2296 * should flush remote TLBs. Otherwise rmap_write_protect
2297 * will find a read-only spte, even though the writable spte
2298 * might be cached on a CPU's TLB.
2299 */
2300 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2301 kvm_flush_remote_tlbs(vcpu->kvm);
be38d276 2302done:
1e73f9dd
MT
2303 return ret;
2304}
2305
d555c333 2306static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1e73f9dd 2307 unsigned pt_access, unsigned pte_access,
640d9b0d 2308 int user_fault, int write_fault,
b90a0e6c 2309 int *emulate, int level, gfn_t gfn,
1403283a 2310 pfn_t pfn, bool speculative,
9bdbba13 2311 bool host_writable)
1e73f9dd
MT
2312{
2313 int was_rmapped = 0;
53a27b39 2314 int rmap_count;
1e73f9dd
MT
2315
2316 pgprintk("%s: spte %llx access %x write_fault %d"
9ad17b10 2317 " user_fault %d gfn %llx\n",
d555c333 2318 __func__, *sptep, pt_access,
1e73f9dd
MT
2319 write_fault, user_fault, gfn);
2320
d555c333 2321 if (is_rmap_spte(*sptep)) {
1e73f9dd
MT
2322 /*
2323 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2324 * the parent of the now unreachable PTE.
2325 */
852e3c19
JR
2326 if (level > PT_PAGE_TABLE_LEVEL &&
2327 !is_large_pte(*sptep)) {
1e73f9dd 2328 struct kvm_mmu_page *child;
d555c333 2329 u64 pte = *sptep;
1e73f9dd
MT
2330
2331 child = page_header(pte & PT64_BASE_ADDR_MASK);
bcdd9a93 2332 drop_parent_pte(child, sptep);
3be2264b 2333 kvm_flush_remote_tlbs(vcpu->kvm);
d555c333 2334 } else if (pfn != spte_to_pfn(*sptep)) {
9ad17b10 2335 pgprintk("hfn old %llx new %llx\n",
d555c333 2336 spte_to_pfn(*sptep), pfn);
c3707958 2337 drop_spte(vcpu->kvm, sptep);
91546356 2338 kvm_flush_remote_tlbs(vcpu->kvm);
6bed6b9e
JR
2339 } else
2340 was_rmapped = 1;
1e73f9dd 2341 }
852e3c19 2342
d555c333 2343 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
640d9b0d 2344 level, gfn, pfn, speculative, true,
9bdbba13 2345 host_writable)) {
1e73f9dd 2346 if (write_fault)
b90a0e6c 2347 *emulate = 1;
5304efde 2348 kvm_mmu_flush_tlb(vcpu);
a378b4e6 2349 }
1e73f9dd 2350
ce88decf
XG
2351 if (unlikely(is_mmio_spte(*sptep) && emulate))
2352 *emulate = 1;
2353
d555c333 2354 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
9ad17b10 2355 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
d555c333 2356 is_large_pte(*sptep)? "2MB" : "4kB",
a205bc19
JR
2357 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2358 *sptep, sptep);
d555c333 2359 if (!was_rmapped && is_large_pte(*sptep))
05da4558
MT
2360 ++vcpu->kvm->stat.lpages;
2361
ffb61bb3
XG
2362 if (is_shadow_present_pte(*sptep)) {
2363 page_header_update_slot(vcpu->kvm, sptep, gfn);
2364 if (!was_rmapped) {
2365 rmap_count = rmap_add(vcpu, sptep, gfn);
2366 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2367 rmap_recycle(vcpu, sptep, gfn);
2368 }
1c4f1fd6 2369 }
9ed5520d 2370 kvm_release_pfn_clean(pfn);
1c4f1fd6
AK
2371}
2372
6aa8b732
AK
2373static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2374{
2375}
2376
957ed9ef
XG
2377static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2378 bool no_dirty_log)
2379{
2380 struct kvm_memory_slot *slot;
2381 unsigned long hva;
2382
5d163b1c 2383 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
957ed9ef 2384 if (!slot) {
fce92dce
XG
2385 get_page(fault_page);
2386 return page_to_pfn(fault_page);
957ed9ef
XG
2387 }
2388
2389 hva = gfn_to_hva_memslot(slot, gfn);
2390
2391 return hva_to_pfn_atomic(vcpu->kvm, hva);
2392}
2393
2394static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2395 struct kvm_mmu_page *sp,
2396 u64 *start, u64 *end)
2397{
2398 struct page *pages[PTE_PREFETCH_NUM];
2399 unsigned access = sp->role.access;
2400 int i, ret;
2401 gfn_t gfn;
2402
2403 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
5d163b1c 2404 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
957ed9ef
XG
2405 return -1;
2406
2407 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2408 if (ret <= 0)
2409 return -1;
2410
2411 for (i = 0; i < ret; i++, gfn++, start++)
2412 mmu_set_spte(vcpu, start, ACC_ALL,
640d9b0d 2413 access, 0, 0, NULL,
957ed9ef
XG
2414 sp->role.level, gfn,
2415 page_to_pfn(pages[i]), true, true);
2416
2417 return 0;
2418}
2419
2420static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2421 struct kvm_mmu_page *sp, u64 *sptep)
2422{
2423 u64 *spte, *start = NULL;
2424 int i;
2425
2426 WARN_ON(!sp->role.direct);
2427
2428 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2429 spte = sp->spt + i;
2430
2431 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
c3707958 2432 if (is_shadow_present_pte(*spte) || spte == sptep) {
957ed9ef
XG
2433 if (!start)
2434 continue;
2435 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2436 break;
2437 start = NULL;
2438 } else if (!start)
2439 start = spte;
2440 }
2441}
2442
2443static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2444{
2445 struct kvm_mmu_page *sp;
2446
2447 /*
2448 * Since it's no accessed bit on EPT, it's no way to
2449 * distinguish between actually accessed translations
2450 * and prefetched, so disable pte prefetch if EPT is
2451 * enabled.
2452 */
2453 if (!shadow_accessed_mask)
2454 return;
2455
2456 sp = page_header(__pa(sptep));
2457 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2458 return;
2459
2460 __direct_pte_prefetch(vcpu, sp, sptep);
2461}
2462
9f652d21 2463static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
2ec4739d
XG
2464 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2465 bool prefault)
140754bc 2466{
9f652d21 2467 struct kvm_shadow_walk_iterator iterator;
140754bc 2468 struct kvm_mmu_page *sp;
b90a0e6c 2469 int emulate = 0;
140754bc 2470 gfn_t pseudo_gfn;
6aa8b732 2471
9f652d21 2472 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
852e3c19 2473 if (iterator.level == level) {
612819c3
MT
2474 unsigned pte_access = ACC_ALL;
2475
612819c3 2476 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
b90a0e6c 2477 0, write, &emulate,
2ec4739d 2478 level, gfn, pfn, prefault, map_writable);
957ed9ef 2479 direct_pte_prefetch(vcpu, iterator.sptep);
9f652d21
AK
2480 ++vcpu->stat.pf_fixed;
2481 break;
6aa8b732
AK
2482 }
2483
c3707958 2484 if (!is_shadow_present_pte(*iterator.sptep)) {
c9fa0b3b
LJ
2485 u64 base_addr = iterator.addr;
2486
2487 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2488 pseudo_gfn = base_addr >> PAGE_SHIFT;
9f652d21
AK
2489 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2490 iterator.level - 1,
2491 1, ACC_ALL, iterator.sptep);
2492 if (!sp) {
2493 pgprintk("nonpaging_map: ENOMEM\n");
2494 kvm_release_pfn_clean(pfn);
2495 return -ENOMEM;
2496 }
140754bc 2497
1df9f2dc
XG
2498 mmu_spte_set(iterator.sptep,
2499 __pa(sp->spt)
2500 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2501 | shadow_user_mask | shadow_x_mask
2502 | shadow_accessed_mask);
9f652d21
AK
2503 }
2504 }
b90a0e6c 2505 return emulate;
6aa8b732
AK
2506}
2507
77db5cbd 2508static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
bf998156 2509{
77db5cbd
HY
2510 siginfo_t info;
2511
2512 info.si_signo = SIGBUS;
2513 info.si_errno = 0;
2514 info.si_code = BUS_MCEERR_AR;
2515 info.si_addr = (void __user *)address;
2516 info.si_addr_lsb = PAGE_SHIFT;
bf998156 2517
77db5cbd 2518 send_sig_info(SIGBUS, &info, tsk);
bf998156
HY
2519}
2520
d7c55201 2521static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, pfn_t pfn)
bf998156
HY
2522{
2523 kvm_release_pfn_clean(pfn);
2524 if (is_hwpoison_pfn(pfn)) {
bebb106a 2525 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
bf998156 2526 return 0;
d7c55201 2527 }
edba23e5 2528
d7c55201 2529 return -EFAULT;
bf998156
HY
2530}
2531
936a5fe6
AA
2532static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2533 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2534{
2535 pfn_t pfn = *pfnp;
2536 gfn_t gfn = *gfnp;
2537 int level = *levelp;
2538
2539 /*
2540 * Check if it's a transparent hugepage. If this would be an
2541 * hugetlbfs page, level wouldn't be set to
2542 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2543 * here.
2544 */
2545 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2546 level == PT_PAGE_TABLE_LEVEL &&
2547 PageTransCompound(pfn_to_page(pfn)) &&
2548 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2549 unsigned long mask;
2550 /*
2551 * mmu_notifier_retry was successful and we hold the
2552 * mmu_lock here, so the pmd can't become splitting
2553 * from under us, and in turn
2554 * __split_huge_page_refcount() can't run from under
2555 * us and we can safely transfer the refcount from
2556 * PG_tail to PG_head as we switch the pfn to tail to
2557 * head.
2558 */
2559 *levelp = level = PT_DIRECTORY_LEVEL;
2560 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2561 VM_BUG_ON((gfn & mask) != (pfn & mask));
2562 if (pfn & mask) {
2563 gfn &= ~mask;
2564 *gfnp = gfn;
2565 kvm_release_pfn_clean(pfn);
2566 pfn &= ~mask;
2567 if (!get_page_unless_zero(pfn_to_page(pfn)))
2568 BUG();
2569 *pfnp = pfn;
2570 }
2571 }
2572}
2573
d7c55201
XG
2574static bool mmu_invalid_pfn(pfn_t pfn)
2575{
ce88decf 2576 return unlikely(is_invalid_pfn(pfn));
d7c55201
XG
2577}
2578
2579static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
2580 pfn_t pfn, unsigned access, int *ret_val)
2581{
2582 bool ret = true;
2583
2584 /* The pfn is invalid, report the error! */
2585 if (unlikely(is_invalid_pfn(pfn))) {
2586 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
2587 goto exit;
2588 }
2589
ce88decf 2590 if (unlikely(is_noslot_pfn(pfn)))
d7c55201 2591 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
d7c55201
XG
2592
2593 ret = false;
2594exit:
2595 return ret;
2596}
2597
78b2c54a 2598static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
060c2abe
XG
2599 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2600
2601static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
78b2c54a 2602 bool prefault)
10589a46
MT
2603{
2604 int r;
852e3c19 2605 int level;
936a5fe6 2606 int force_pt_level;
35149e21 2607 pfn_t pfn;
e930bffe 2608 unsigned long mmu_seq;
612819c3 2609 bool map_writable;
aaee2c94 2610
936a5fe6
AA
2611 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2612 if (likely(!force_pt_level)) {
2613 level = mapping_level(vcpu, gfn);
2614 /*
2615 * This path builds a PAE pagetable - so we can map
2616 * 2mb pages at maximum. Therefore check if the level
2617 * is larger than that.
2618 */
2619 if (level > PT_DIRECTORY_LEVEL)
2620 level = PT_DIRECTORY_LEVEL;
852e3c19 2621
936a5fe6
AA
2622 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2623 } else
2624 level = PT_PAGE_TABLE_LEVEL;
05da4558 2625
e930bffe 2626 mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 2627 smp_rmb();
060c2abe 2628
78b2c54a 2629 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
060c2abe 2630 return 0;
aaee2c94 2631
d7c55201
XG
2632 if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
2633 return r;
d196e343 2634
aaee2c94 2635 spin_lock(&vcpu->kvm->mmu_lock);
e930bffe
AA
2636 if (mmu_notifier_retry(vcpu, mmu_seq))
2637 goto out_unlock;
eb787d10 2638 kvm_mmu_free_some_pages(vcpu);
936a5fe6
AA
2639 if (likely(!force_pt_level))
2640 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
2ec4739d
XG
2641 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2642 prefault);
aaee2c94
MT
2643 spin_unlock(&vcpu->kvm->mmu_lock);
2644
aaee2c94 2645
10589a46 2646 return r;
e930bffe
AA
2647
2648out_unlock:
2649 spin_unlock(&vcpu->kvm->mmu_lock);
2650 kvm_release_pfn_clean(pfn);
2651 return 0;
10589a46
MT
2652}
2653
2654
17ac10ad
AK
2655static void mmu_free_roots(struct kvm_vcpu *vcpu)
2656{
2657 int i;
4db35314 2658 struct kvm_mmu_page *sp;
d98ba053 2659 LIST_HEAD(invalid_list);
17ac10ad 2660
ad312c7c 2661 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
7b53aa56 2662 return;
aaee2c94 2663 spin_lock(&vcpu->kvm->mmu_lock);
81407ca5
JR
2664 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2665 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2666 vcpu->arch.mmu.direct_map)) {
ad312c7c 2667 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad 2668
4db35314
AK
2669 sp = page_header(root);
2670 --sp->root_count;
d98ba053
XG
2671 if (!sp->root_count && sp->role.invalid) {
2672 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2673 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2674 }
ad312c7c 2675 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
aaee2c94 2676 spin_unlock(&vcpu->kvm->mmu_lock);
17ac10ad
AK
2677 return;
2678 }
17ac10ad 2679 for (i = 0; i < 4; ++i) {
ad312c7c 2680 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad 2681
417726a3 2682 if (root) {
417726a3 2683 root &= PT64_BASE_ADDR_MASK;
4db35314
AK
2684 sp = page_header(root);
2685 --sp->root_count;
2e53d63a 2686 if (!sp->root_count && sp->role.invalid)
d98ba053
XG
2687 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2688 &invalid_list);
417726a3 2689 }
ad312c7c 2690 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 2691 }
d98ba053 2692 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
aaee2c94 2693 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 2694 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
17ac10ad
AK
2695}
2696
8986ecc0
MT
2697static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2698{
2699 int ret = 0;
2700
2701 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
a8eeb04a 2702 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
8986ecc0
MT
2703 ret = 1;
2704 }
2705
2706 return ret;
2707}
2708
651dd37a
JR
2709static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2710{
2711 struct kvm_mmu_page *sp;
7ebaf15e 2712 unsigned i;
651dd37a
JR
2713
2714 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2715 spin_lock(&vcpu->kvm->mmu_lock);
2716 kvm_mmu_free_some_pages(vcpu);
2717 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2718 1, ACC_ALL, NULL);
2719 ++sp->root_count;
2720 spin_unlock(&vcpu->kvm->mmu_lock);
2721 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2722 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2723 for (i = 0; i < 4; ++i) {
2724 hpa_t root = vcpu->arch.mmu.pae_root[i];
2725
2726 ASSERT(!VALID_PAGE(root));
2727 spin_lock(&vcpu->kvm->mmu_lock);
2728 kvm_mmu_free_some_pages(vcpu);
649497d1
AK
2729 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2730 i << 30,
651dd37a
JR
2731 PT32_ROOT_LEVEL, 1, ACC_ALL,
2732 NULL);
2733 root = __pa(sp->spt);
2734 ++sp->root_count;
2735 spin_unlock(&vcpu->kvm->mmu_lock);
2736 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
651dd37a 2737 }
6292757f 2738 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
651dd37a
JR
2739 } else
2740 BUG();
2741
2742 return 0;
2743}
2744
2745static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
17ac10ad 2746{
4db35314 2747 struct kvm_mmu_page *sp;
81407ca5
JR
2748 u64 pdptr, pm_mask;
2749 gfn_t root_gfn;
2750 int i;
3bb65a22 2751
5777ed34 2752 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
17ac10ad 2753
651dd37a
JR
2754 if (mmu_check_root(vcpu, root_gfn))
2755 return 1;
2756
2757 /*
2758 * Do we shadow a long mode page table? If so we need to
2759 * write-protect the guests page table root.
2760 */
2761 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
ad312c7c 2762 hpa_t root = vcpu->arch.mmu.root_hpa;
17ac10ad
AK
2763
2764 ASSERT(!VALID_PAGE(root));
651dd37a 2765
8facbbff 2766 spin_lock(&vcpu->kvm->mmu_lock);
24955b6c 2767 kvm_mmu_free_some_pages(vcpu);
651dd37a
JR
2768 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2769 0, ACC_ALL, NULL);
4db35314
AK
2770 root = __pa(sp->spt);
2771 ++sp->root_count;
8facbbff 2772 spin_unlock(&vcpu->kvm->mmu_lock);
ad312c7c 2773 vcpu->arch.mmu.root_hpa = root;
8986ecc0 2774 return 0;
17ac10ad 2775 }
f87f9288 2776
651dd37a
JR
2777 /*
2778 * We shadow a 32 bit page table. This may be a legacy 2-level
81407ca5
JR
2779 * or a PAE 3-level page table. In either case we need to be aware that
2780 * the shadow page table may be a PAE or a long mode page table.
651dd37a 2781 */
81407ca5
JR
2782 pm_mask = PT_PRESENT_MASK;
2783 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2784 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2785
17ac10ad 2786 for (i = 0; i < 4; ++i) {
ad312c7c 2787 hpa_t root = vcpu->arch.mmu.pae_root[i];
17ac10ad
AK
2788
2789 ASSERT(!VALID_PAGE(root));
ad312c7c 2790 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
e4e517b4 2791 pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i);
43a3795a 2792 if (!is_present_gpte(pdptr)) {
ad312c7c 2793 vcpu->arch.mmu.pae_root[i] = 0;
417726a3
AK
2794 continue;
2795 }
6de4f3ad 2796 root_gfn = pdptr >> PAGE_SHIFT;
f87f9288
JR
2797 if (mmu_check_root(vcpu, root_gfn))
2798 return 1;
5a7388c2 2799 }
8facbbff 2800 spin_lock(&vcpu->kvm->mmu_lock);
24955b6c 2801 kvm_mmu_free_some_pages(vcpu);
4db35314 2802 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
651dd37a 2803 PT32_ROOT_LEVEL, 0,
f7d9c7b7 2804 ACC_ALL, NULL);
4db35314
AK
2805 root = __pa(sp->spt);
2806 ++sp->root_count;
8facbbff
AK
2807 spin_unlock(&vcpu->kvm->mmu_lock);
2808
81407ca5 2809 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
17ac10ad 2810 }
6292757f 2811 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
81407ca5
JR
2812
2813 /*
2814 * If we shadow a 32 bit page table with a long mode page
2815 * table we enter this path.
2816 */
2817 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2818 if (vcpu->arch.mmu.lm_root == NULL) {
2819 /*
2820 * The additional page necessary for this is only
2821 * allocated on demand.
2822 */
2823
2824 u64 *lm_root;
2825
2826 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2827 if (lm_root == NULL)
2828 return 1;
2829
2830 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2831
2832 vcpu->arch.mmu.lm_root = lm_root;
2833 }
2834
2835 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2836 }
2837
8986ecc0 2838 return 0;
17ac10ad
AK
2839}
2840
651dd37a
JR
2841static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2842{
2843 if (vcpu->arch.mmu.direct_map)
2844 return mmu_alloc_direct_roots(vcpu);
2845 else
2846 return mmu_alloc_shadow_roots(vcpu);
2847}
2848
0ba73cda
MT
2849static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2850{
2851 int i;
2852 struct kvm_mmu_page *sp;
2853
81407ca5
JR
2854 if (vcpu->arch.mmu.direct_map)
2855 return;
2856
0ba73cda
MT
2857 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2858 return;
6903074c 2859
bebb106a 2860 vcpu_clear_mmio_info(vcpu, ~0ul);
0375f7fa 2861 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
81407ca5 2862 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
0ba73cda
MT
2863 hpa_t root = vcpu->arch.mmu.root_hpa;
2864 sp = page_header(root);
2865 mmu_sync_children(vcpu, sp);
0375f7fa 2866 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
0ba73cda
MT
2867 return;
2868 }
2869 for (i = 0; i < 4; ++i) {
2870 hpa_t root = vcpu->arch.mmu.pae_root[i];
2871
8986ecc0 2872 if (root && VALID_PAGE(root)) {
0ba73cda
MT
2873 root &= PT64_BASE_ADDR_MASK;
2874 sp = page_header(root);
2875 mmu_sync_children(vcpu, sp);
2876 }
2877 }
0375f7fa 2878 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
0ba73cda
MT
2879}
2880
2881void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2882{
2883 spin_lock(&vcpu->kvm->mmu_lock);
2884 mmu_sync_roots(vcpu);
6cffe8ca 2885 spin_unlock(&vcpu->kvm->mmu_lock);
0ba73cda
MT
2886}
2887
1871c602 2888static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
ab9ae313 2889 u32 access, struct x86_exception *exception)
6aa8b732 2890{
ab9ae313
AK
2891 if (exception)
2892 exception->error_code = 0;
6aa8b732
AK
2893 return vaddr;
2894}
2895
6539e738 2896static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
ab9ae313
AK
2897 u32 access,
2898 struct x86_exception *exception)
6539e738 2899{
ab9ae313
AK
2900 if (exception)
2901 exception->error_code = 0;
6539e738
JR
2902 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2903}
2904
ce88decf
XG
2905static bool quickly_check_mmio_pf(struct kvm_vcpu *vcpu, u64 addr, bool direct)
2906{
2907 if (direct)
2908 return vcpu_match_mmio_gpa(vcpu, addr);
2909
2910 return vcpu_match_mmio_gva(vcpu, addr);
2911}
2912
2913
2914/*
2915 * On direct hosts, the last spte is only allows two states
2916 * for mmio page fault:
2917 * - It is the mmio spte
2918 * - It is zapped or it is being zapped.
2919 *
2920 * This function completely checks the spte when the last spte
2921 * is not the mmio spte.
2922 */
2923static bool check_direct_spte_mmio_pf(u64 spte)
2924{
2925 return __check_direct_spte_mmio_pf(spte);
2926}
2927
2928static u64 walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr)
2929{
2930 struct kvm_shadow_walk_iterator iterator;
2931 u64 spte = 0ull;
2932
2933 walk_shadow_page_lockless_begin(vcpu);
2934 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte)
2935 if (!is_shadow_present_pte(spte))
2936 break;
2937 walk_shadow_page_lockless_end(vcpu);
2938
2939 return spte;
2940}
2941
2942/*
2943 * If it is a real mmio page fault, return 1 and emulat the instruction
2944 * directly, return 0 to let CPU fault again on the address, -1 is
2945 * returned if bug is detected.
2946 */
2947int handle_mmio_page_fault_common(struct kvm_vcpu *vcpu, u64 addr, bool direct)
2948{
2949 u64 spte;
2950
2951 if (quickly_check_mmio_pf(vcpu, addr, direct))
2952 return 1;
2953
2954 spte = walk_shadow_page_get_mmio_spte(vcpu, addr);
2955
2956 if (is_mmio_spte(spte)) {
2957 gfn_t gfn = get_mmio_spte_gfn(spte);
2958 unsigned access = get_mmio_spte_access(spte);
2959
2960 if (direct)
2961 addr = 0;
4f022648
XG
2962
2963 trace_handle_mmio_page_fault(addr, gfn, access);
ce88decf
XG
2964 vcpu_cache_mmio_info(vcpu, addr, gfn, access);
2965 return 1;
2966 }
2967
2968 /*
2969 * It's ok if the gva is remapped by other cpus on shadow guest,
2970 * it's a BUG if the gfn is not a mmio page.
2971 */
2972 if (direct && !check_direct_spte_mmio_pf(spte))
2973 return -1;
2974
2975 /*
2976 * If the page table is zapped by other cpus, let CPU fault again on
2977 * the address.
2978 */
2979 return 0;
2980}
2981EXPORT_SYMBOL_GPL(handle_mmio_page_fault_common);
2982
2983static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr,
2984 u32 error_code, bool direct)
2985{
2986 int ret;
2987
2988 ret = handle_mmio_page_fault_common(vcpu, addr, direct);
2989 WARN_ON(ret < 0);
2990 return ret;
2991}
2992
6aa8b732 2993static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
78b2c54a 2994 u32 error_code, bool prefault)
6aa8b732 2995{
e833240f 2996 gfn_t gfn;
e2dec939 2997 int r;
6aa8b732 2998
b8688d51 2999 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
ce88decf
XG
3000
3001 if (unlikely(error_code & PFERR_RSVD_MASK))
3002 return handle_mmio_page_fault(vcpu, gva, error_code, true);
3003
e2dec939
AK
3004 r = mmu_topup_memory_caches(vcpu);
3005 if (r)
3006 return r;
714b93da 3007
6aa8b732 3008 ASSERT(vcpu);
ad312c7c 3009 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 3010
e833240f 3011 gfn = gva >> PAGE_SHIFT;
6aa8b732 3012
e833240f 3013 return nonpaging_map(vcpu, gva & PAGE_MASK,
78b2c54a 3014 error_code & PFERR_WRITE_MASK, gfn, prefault);
6aa8b732
AK
3015}
3016
7e1fbeac 3017static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
af585b92
GN
3018{
3019 struct kvm_arch_async_pf arch;
fb67e14f 3020
7c90705b 3021 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
af585b92 3022 arch.gfn = gfn;
c4806acd 3023 arch.direct_map = vcpu->arch.mmu.direct_map;
fb67e14f 3024 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
af585b92
GN
3025
3026 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
3027}
3028
3029static bool can_do_async_pf(struct kvm_vcpu *vcpu)
3030{
3031 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
3032 kvm_event_needs_reinjection(vcpu)))
3033 return false;
3034
3035 return kvm_x86_ops->interrupt_allowed(vcpu);
3036}
3037
78b2c54a 3038static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
612819c3 3039 gva_t gva, pfn_t *pfn, bool write, bool *writable)
af585b92
GN
3040{
3041 bool async;
3042
612819c3 3043 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
af585b92
GN
3044
3045 if (!async)
3046 return false; /* *pfn has correct page already */
3047
3048 put_page(pfn_to_page(*pfn));
3049
78b2c54a 3050 if (!prefault && can_do_async_pf(vcpu)) {
c9b263d2 3051 trace_kvm_try_async_get_page(gva, gfn);
af585b92
GN
3052 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
3053 trace_kvm_async_pf_doublefault(gva, gfn);
3054 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
3055 return true;
3056 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
3057 return true;
3058 }
3059
612819c3 3060 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
af585b92
GN
3061
3062 return false;
3063}
3064
56028d08 3065static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
78b2c54a 3066 bool prefault)
fb72d167 3067{
35149e21 3068 pfn_t pfn;
fb72d167 3069 int r;
852e3c19 3070 int level;
936a5fe6 3071 int force_pt_level;
05da4558 3072 gfn_t gfn = gpa >> PAGE_SHIFT;
e930bffe 3073 unsigned long mmu_seq;
612819c3
MT
3074 int write = error_code & PFERR_WRITE_MASK;
3075 bool map_writable;
fb72d167
JR
3076
3077 ASSERT(vcpu);
3078 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
3079
ce88decf
XG
3080 if (unlikely(error_code & PFERR_RSVD_MASK))
3081 return handle_mmio_page_fault(vcpu, gpa, error_code, true);
3082
fb72d167
JR
3083 r = mmu_topup_memory_caches(vcpu);
3084 if (r)
3085 return r;
3086
936a5fe6
AA
3087 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
3088 if (likely(!force_pt_level)) {
3089 level = mapping_level(vcpu, gfn);
3090 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
3091 } else
3092 level = PT_PAGE_TABLE_LEVEL;
852e3c19 3093
e930bffe 3094 mmu_seq = vcpu->kvm->mmu_notifier_seq;
4c2155ce 3095 smp_rmb();
af585b92 3096
78b2c54a 3097 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
af585b92
GN
3098 return 0;
3099
d7c55201
XG
3100 if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
3101 return r;
3102
fb72d167 3103 spin_lock(&vcpu->kvm->mmu_lock);
e930bffe
AA
3104 if (mmu_notifier_retry(vcpu, mmu_seq))
3105 goto out_unlock;
fb72d167 3106 kvm_mmu_free_some_pages(vcpu);
936a5fe6
AA
3107 if (likely(!force_pt_level))
3108 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
612819c3 3109 r = __direct_map(vcpu, gpa, write, map_writable,
2ec4739d 3110 level, gfn, pfn, prefault);
fb72d167 3111 spin_unlock(&vcpu->kvm->mmu_lock);
fb72d167
JR
3112
3113 return r;
e930bffe
AA
3114
3115out_unlock:
3116 spin_unlock(&vcpu->kvm->mmu_lock);
3117 kvm_release_pfn_clean(pfn);
3118 return 0;
fb72d167
JR
3119}
3120
6aa8b732
AK
3121static void nonpaging_free(struct kvm_vcpu *vcpu)
3122{
17ac10ad 3123 mmu_free_roots(vcpu);
6aa8b732
AK
3124}
3125
52fde8df
JR
3126static int nonpaging_init_context(struct kvm_vcpu *vcpu,
3127 struct kvm_mmu *context)
6aa8b732 3128{
6aa8b732
AK
3129 context->new_cr3 = nonpaging_new_cr3;
3130 context->page_fault = nonpaging_page_fault;
6aa8b732
AK
3131 context->gva_to_gpa = nonpaging_gva_to_gpa;
3132 context->free = nonpaging_free;
e8bc217a 3133 context->sync_page = nonpaging_sync_page;
a7052897 3134 context->invlpg = nonpaging_invlpg;
0f53b5b1 3135 context->update_pte = nonpaging_update_pte;
cea0f0e7 3136 context->root_level = 0;
6aa8b732 3137 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 3138 context->root_hpa = INVALID_PAGE;
c5a78f2b 3139 context->direct_map = true;
2d48a985 3140 context->nx = false;
6aa8b732
AK
3141 return 0;
3142}
3143
d835dfec 3144void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
6aa8b732 3145{
1165f5fe 3146 ++vcpu->stat.tlb_flush;
a8eeb04a 3147 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
6aa8b732
AK
3148}
3149
3150static void paging_new_cr3(struct kvm_vcpu *vcpu)
3151{
9f8fe504 3152 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
cea0f0e7 3153 mmu_free_roots(vcpu);
6aa8b732
AK
3154}
3155
5777ed34
JR
3156static unsigned long get_cr3(struct kvm_vcpu *vcpu)
3157{
9f8fe504 3158 return kvm_read_cr3(vcpu);
5777ed34
JR
3159}
3160
6389ee94
AK
3161static void inject_page_fault(struct kvm_vcpu *vcpu,
3162 struct x86_exception *fault)
6aa8b732 3163{
6389ee94 3164 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
6aa8b732
AK
3165}
3166
6aa8b732
AK
3167static void paging_free(struct kvm_vcpu *vcpu)
3168{
3169 nonpaging_free(vcpu);
3170}
3171
3241f22d 3172static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
82725b20
DE
3173{
3174 int bit7;
3175
3176 bit7 = (gpte >> 7) & 1;
3241f22d 3177 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
82725b20
DE
3178}
3179
ce88decf
XG
3180static bool sync_mmio_spte(u64 *sptep, gfn_t gfn, unsigned access,
3181 int *nr_present)
3182{
3183 if (unlikely(is_mmio_spte(*sptep))) {
3184 if (gfn != get_mmio_spte_gfn(*sptep)) {
3185 mmu_spte_clear_no_track(sptep);
3186 return true;
3187 }
3188
3189 (*nr_present)++;
3190 mark_mmio_spte(sptep, gfn, access);
3191 return true;
3192 }
3193
3194 return false;
3195}
3196
6aa8b732
AK
3197#define PTTYPE 64
3198#include "paging_tmpl.h"
3199#undef PTTYPE
3200
3201#define PTTYPE 32
3202#include "paging_tmpl.h"
3203#undef PTTYPE
3204
52fde8df
JR
3205static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
3206 struct kvm_mmu *context,
3207 int level)
82725b20 3208{
82725b20
DE
3209 int maxphyaddr = cpuid_maxphyaddr(vcpu);
3210 u64 exb_bit_rsvd = 0;
3211
2d48a985 3212 if (!context->nx)
82725b20
DE
3213 exb_bit_rsvd = rsvd_bits(63, 63);
3214 switch (level) {
3215 case PT32_ROOT_LEVEL:
3216 /* no rsvd bits for 2 level 4K page table entries */
3217 context->rsvd_bits_mask[0][1] = 0;
3218 context->rsvd_bits_mask[0][0] = 0;
f815bce8
XG
3219 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
3220
3221 if (!is_pse(vcpu)) {
3222 context->rsvd_bits_mask[1][1] = 0;
3223 break;
3224 }
3225
82725b20
DE
3226 if (is_cpuid_PSE36())
3227 /* 36bits PSE 4MB page */
3228 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
3229 else
3230 /* 32 bits PSE 4MB page */
3231 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
82725b20
DE
3232 break;
3233 case PT32E_ROOT_LEVEL:
20c466b5
DE
3234 context->rsvd_bits_mask[0][2] =
3235 rsvd_bits(maxphyaddr, 63) |
3236 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
82725b20 3237 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4c26b4cd 3238 rsvd_bits(maxphyaddr, 62); /* PDE */
82725b20
DE
3239 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
3240 rsvd_bits(maxphyaddr, 62); /* PTE */
3241 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
3242 rsvd_bits(maxphyaddr, 62) |
3243 rsvd_bits(13, 20); /* large page */
f815bce8 3244 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
82725b20
DE
3245 break;
3246 case PT64_ROOT_LEVEL:
3247 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
3248 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
3249 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
3250 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
3251 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4c26b4cd 3252 rsvd_bits(maxphyaddr, 51);
82725b20
DE
3253 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
3254 rsvd_bits(maxphyaddr, 51);
3255 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
e04da980
JR
3256 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
3257 rsvd_bits(maxphyaddr, 51) |
3258 rsvd_bits(13, 29);
82725b20 3259 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
4c26b4cd
SY
3260 rsvd_bits(maxphyaddr, 51) |
3261 rsvd_bits(13, 20); /* large page */
f815bce8 3262 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
82725b20
DE
3263 break;
3264 }
3265}
3266
52fde8df
JR
3267static int paging64_init_context_common(struct kvm_vcpu *vcpu,
3268 struct kvm_mmu *context,
3269 int level)
6aa8b732 3270{
2d48a985
JR
3271 context->nx = is_nx(vcpu);
3272
52fde8df 3273 reset_rsvds_bits_mask(vcpu, context, level);
6aa8b732
AK
3274
3275 ASSERT(is_pae(vcpu));
3276 context->new_cr3 = paging_new_cr3;
3277 context->page_fault = paging64_page_fault;
6aa8b732 3278 context->gva_to_gpa = paging64_gva_to_gpa;
e8bc217a 3279 context->sync_page = paging64_sync_page;
a7052897 3280 context->invlpg = paging64_invlpg;
0f53b5b1 3281 context->update_pte = paging64_update_pte;
6aa8b732 3282 context->free = paging_free;
17ac10ad
AK
3283 context->root_level = level;
3284 context->shadow_root_level = level;
17c3ba9d 3285 context->root_hpa = INVALID_PAGE;
c5a78f2b 3286 context->direct_map = false;
6aa8b732
AK
3287 return 0;
3288}
3289
52fde8df
JR
3290static int paging64_init_context(struct kvm_vcpu *vcpu,
3291 struct kvm_mmu *context)
17ac10ad 3292{
52fde8df 3293 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
17ac10ad
AK
3294}
3295
52fde8df
JR
3296static int paging32_init_context(struct kvm_vcpu *vcpu,
3297 struct kvm_mmu *context)
6aa8b732 3298{
2d48a985
JR
3299 context->nx = false;
3300
52fde8df 3301 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
6aa8b732
AK
3302
3303 context->new_cr3 = paging_new_cr3;
3304 context->page_fault = paging32_page_fault;
6aa8b732
AK
3305 context->gva_to_gpa = paging32_gva_to_gpa;
3306 context->free = paging_free;
e8bc217a 3307 context->sync_page = paging32_sync_page;
a7052897 3308 context->invlpg = paging32_invlpg;
0f53b5b1 3309 context->update_pte = paging32_update_pte;
6aa8b732
AK
3310 context->root_level = PT32_ROOT_LEVEL;
3311 context->shadow_root_level = PT32E_ROOT_LEVEL;
17c3ba9d 3312 context->root_hpa = INVALID_PAGE;
c5a78f2b 3313 context->direct_map = false;
6aa8b732
AK
3314 return 0;
3315}
3316
52fde8df
JR
3317static int paging32E_init_context(struct kvm_vcpu *vcpu,
3318 struct kvm_mmu *context)
6aa8b732 3319{
52fde8df 3320 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
6aa8b732
AK
3321}
3322
fb72d167
JR
3323static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
3324{
14dfe855 3325 struct kvm_mmu *context = vcpu->arch.walk_mmu;
fb72d167 3326
c445f8ef 3327 context->base_role.word = 0;
fb72d167
JR
3328 context->new_cr3 = nonpaging_new_cr3;
3329 context->page_fault = tdp_page_fault;
3330 context->free = nonpaging_free;
e8bc217a 3331 context->sync_page = nonpaging_sync_page;
a7052897 3332 context->invlpg = nonpaging_invlpg;
0f53b5b1 3333 context->update_pte = nonpaging_update_pte;
67253af5 3334 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
fb72d167 3335 context->root_hpa = INVALID_PAGE;
c5a78f2b 3336 context->direct_map = true;
1c97f0a0 3337 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
5777ed34 3338 context->get_cr3 = get_cr3;
e4e517b4 3339 context->get_pdptr = kvm_pdptr_read;
cb659db8 3340 context->inject_page_fault = kvm_inject_page_fault;
2d48a985 3341 context->nx = is_nx(vcpu);
fb72d167
JR
3342
3343 if (!is_paging(vcpu)) {
2d48a985 3344 context->nx = false;
fb72d167
JR
3345 context->gva_to_gpa = nonpaging_gva_to_gpa;
3346 context->root_level = 0;
3347 } else if (is_long_mode(vcpu)) {
2d48a985 3348 context->nx = is_nx(vcpu);
52fde8df 3349 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
fb72d167
JR
3350 context->gva_to_gpa = paging64_gva_to_gpa;
3351 context->root_level = PT64_ROOT_LEVEL;
3352 } else if (is_pae(vcpu)) {
2d48a985 3353 context->nx = is_nx(vcpu);
52fde8df 3354 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
fb72d167
JR
3355 context->gva_to_gpa = paging64_gva_to_gpa;
3356 context->root_level = PT32E_ROOT_LEVEL;
3357 } else {
2d48a985 3358 context->nx = false;
52fde8df 3359 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
fb72d167
JR
3360 context->gva_to_gpa = paging32_gva_to_gpa;
3361 context->root_level = PT32_ROOT_LEVEL;
3362 }
3363
3364 return 0;
3365}
3366
52fde8df 3367int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
6aa8b732 3368{
a770f6f2 3369 int r;
411c588d 3370 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
6aa8b732 3371 ASSERT(vcpu);
ad312c7c 3372 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732
AK
3373
3374 if (!is_paging(vcpu))
52fde8df 3375 r = nonpaging_init_context(vcpu, context);
a9058ecd 3376 else if (is_long_mode(vcpu))
52fde8df 3377 r = paging64_init_context(vcpu, context);
6aa8b732 3378 else if (is_pae(vcpu))
52fde8df 3379 r = paging32E_init_context(vcpu, context);
6aa8b732 3380 else
52fde8df 3381 r = paging32_init_context(vcpu, context);
a770f6f2 3382
5b7e0102 3383 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
f43addd4 3384 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
411c588d
AK
3385 vcpu->arch.mmu.base_role.smep_andnot_wp
3386 = smep && !is_write_protection(vcpu);
52fde8df
JR
3387
3388 return r;
3389}
3390EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3391
3392static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3393{
14dfe855 3394 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
52fde8df 3395
14dfe855
JR
3396 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
3397 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
e4e517b4 3398 vcpu->arch.walk_mmu->get_pdptr = kvm_pdptr_read;
14dfe855 3399 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
a770f6f2
AK
3400
3401 return r;
6aa8b732
AK
3402}
3403
02f59dc9
JR
3404static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3405{
3406 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3407
3408 g_context->get_cr3 = get_cr3;
e4e517b4 3409 g_context->get_pdptr = kvm_pdptr_read;
02f59dc9
JR
3410 g_context->inject_page_fault = kvm_inject_page_fault;
3411
3412 /*
3413 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3414 * translation of l2_gpa to l1_gpa addresses is done using the
3415 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3416 * functions between mmu and nested_mmu are swapped.
3417 */
3418 if (!is_paging(vcpu)) {
2d48a985 3419 g_context->nx = false;
02f59dc9
JR
3420 g_context->root_level = 0;
3421 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3422 } else if (is_long_mode(vcpu)) {
2d48a985 3423 g_context->nx = is_nx(vcpu);
02f59dc9
JR
3424 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3425 g_context->root_level = PT64_ROOT_LEVEL;
3426 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3427 } else if (is_pae(vcpu)) {
2d48a985 3428 g_context->nx = is_nx(vcpu);
02f59dc9
JR
3429 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3430 g_context->root_level = PT32E_ROOT_LEVEL;
3431 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3432 } else {
2d48a985 3433 g_context->nx = false;
02f59dc9
JR
3434 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3435 g_context->root_level = PT32_ROOT_LEVEL;
3436 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3437 }
3438
3439 return 0;
3440}
3441
fb72d167
JR
3442static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3443{
02f59dc9
JR
3444 if (mmu_is_nested(vcpu))
3445 return init_kvm_nested_mmu(vcpu);
3446 else if (tdp_enabled)
fb72d167
JR
3447 return init_kvm_tdp_mmu(vcpu);
3448 else
3449 return init_kvm_softmmu(vcpu);
3450}
3451
6aa8b732
AK
3452static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3453{
3454 ASSERT(vcpu);
62ad0755
SY
3455 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3456 /* mmu.free() should set root_hpa = INVALID_PAGE */
ad312c7c 3457 vcpu->arch.mmu.free(vcpu);
6aa8b732
AK
3458}
3459
3460int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
17c3ba9d
AK
3461{
3462 destroy_kvm_mmu(vcpu);
f8f7e5ee 3463 return init_kvm_mmu(vcpu);
17c3ba9d 3464}
8668a3c4 3465EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
17c3ba9d
AK
3466
3467int kvm_mmu_load(struct kvm_vcpu *vcpu)
6aa8b732 3468{
714b93da
AK
3469 int r;
3470
e2dec939 3471 r = mmu_topup_memory_caches(vcpu);
17c3ba9d
AK
3472 if (r)
3473 goto out;
8986ecc0 3474 r = mmu_alloc_roots(vcpu);
8facbbff 3475 spin_lock(&vcpu->kvm->mmu_lock);
0ba73cda 3476 mmu_sync_roots(vcpu);
aaee2c94 3477 spin_unlock(&vcpu->kvm->mmu_lock);
8986ecc0
MT
3478 if (r)
3479 goto out;
3662cb1c 3480 /* set_cr3() should ensure TLB has been flushed */
f43addd4 3481 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
714b93da
AK
3482out:
3483 return r;
6aa8b732 3484}
17c3ba9d
AK
3485EXPORT_SYMBOL_GPL(kvm_mmu_load);
3486
3487void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3488{
3489 mmu_free_roots(vcpu);
3490}
4b16184c 3491EXPORT_SYMBOL_GPL(kvm_mmu_unload);
6aa8b732 3492
0028425f 3493static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
7c562522
XG
3494 struct kvm_mmu_page *sp, u64 *spte,
3495 const void *new)
0028425f 3496{
30945387 3497 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
7e4e4056
JR
3498 ++vcpu->kvm->stat.mmu_pde_zapped;
3499 return;
30945387 3500 }
0028425f 3501
4cee5764 3502 ++vcpu->kvm->stat.mmu_pte_updated;
7c562522 3503 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
0028425f
AK
3504}
3505
79539cec
AK
3506static bool need_remote_flush(u64 old, u64 new)
3507{
3508 if (!is_shadow_present_pte(old))
3509 return false;
3510 if (!is_shadow_present_pte(new))
3511 return true;
3512 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3513 return true;
3514 old ^= PT64_NX_MASK;
3515 new ^= PT64_NX_MASK;
3516 return (old & ~new & PT64_PERM_MASK) != 0;
3517}
3518
0671a8e7
XG
3519static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3520 bool remote_flush, bool local_flush)
79539cec 3521{
0671a8e7
XG
3522 if (zap_page)
3523 return;
3524
3525 if (remote_flush)
79539cec 3526 kvm_flush_remote_tlbs(vcpu->kvm);
0671a8e7 3527 else if (local_flush)
79539cec
AK
3528 kvm_mmu_flush_tlb(vcpu);
3529}
3530
889e5cbc
XG
3531static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
3532 const u8 *new, int *bytes)
da4a00f0 3533{
889e5cbc
XG
3534 u64 gentry;
3535 int r;
72016f3a 3536
72016f3a
AK
3537 /*
3538 * Assume that the pte write on a page table of the same type
49b26e26
XG
3539 * as the current vcpu paging mode since we update the sptes only
3540 * when they have the same mode.
72016f3a 3541 */
889e5cbc 3542 if (is_pae(vcpu) && *bytes == 4) {
72016f3a 3543 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
889e5cbc
XG
3544 *gpa &= ~(gpa_t)7;
3545 *bytes = 8;
3546 r = kvm_read_guest(vcpu->kvm, *gpa, &gentry, min(*bytes, 8));
72016f3a
AK
3547 if (r)
3548 gentry = 0;
08e850c6
AK
3549 new = (const u8 *)&gentry;
3550 }
3551
889e5cbc 3552 switch (*bytes) {
08e850c6
AK
3553 case 4:
3554 gentry = *(const u32 *)new;
3555 break;
3556 case 8:
3557 gentry = *(const u64 *)new;
3558 break;
3559 default:
3560 gentry = 0;
3561 break;
72016f3a
AK
3562 }
3563
889e5cbc
XG
3564 return gentry;
3565}
3566
3567/*
3568 * If we're seeing too many writes to a page, it may no longer be a page table,
3569 * or we may be forking, in which case it is better to unmap the page.
3570 */
a30f47cb 3571static bool detect_write_flooding(struct kvm_mmu_page *sp, u64 *spte)
889e5cbc 3572{
a30f47cb
XG
3573 /*
3574 * Skip write-flooding detected for the sp whose level is 1, because
3575 * it can become unsync, then the guest page is not write-protected.
3576 */
3577 if (sp->role.level == 1)
3578 return false;
3246af0e 3579
a30f47cb 3580 return ++sp->write_flooding_count >= 3;
889e5cbc
XG
3581}
3582
3583/*
3584 * Misaligned accesses are too much trouble to fix up; also, they usually
3585 * indicate a page is not used as a page table.
3586 */
3587static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
3588 int bytes)
3589{
3590 unsigned offset, pte_size, misaligned;
3591
3592 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
3593 gpa, bytes, sp->role.word);
3594
3595 offset = offset_in_page(gpa);
3596 pte_size = sp->role.cr4_pae ? 8 : 4;
5d9ca30e
XG
3597
3598 /*
3599 * Sometimes, the OS only writes the last one bytes to update status
3600 * bits, for example, in linux, andb instruction is used in clear_bit().
3601 */
3602 if (!(offset & (pte_size - 1)) && bytes == 1)
3603 return false;
3604
889e5cbc
XG
3605 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
3606 misaligned |= bytes < 4;
3607
3608 return misaligned;
3609}
3610
3611static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
3612{
3613 unsigned page_offset, quadrant;
3614 u64 *spte;
3615 int level;
3616
3617 page_offset = offset_in_page(gpa);
3618 level = sp->role.level;
3619 *nspte = 1;
3620 if (!sp->role.cr4_pae) {
3621 page_offset <<= 1; /* 32->64 */
3622 /*
3623 * A 32-bit pde maps 4MB while the shadow pdes map
3624 * only 2MB. So we need to double the offset again
3625 * and zap two pdes instead of one.
3626 */
3627 if (level == PT32_ROOT_LEVEL) {
3628 page_offset &= ~7; /* kill rounding error */
3629 page_offset <<= 1;
3630 *nspte = 2;
3631 }
3632 quadrant = page_offset >> PAGE_SHIFT;
3633 page_offset &= ~PAGE_MASK;
3634 if (quadrant != sp->role.quadrant)
3635 return NULL;
3636 }
3637
3638 spte = &sp->spt[page_offset / sizeof(*spte)];
3639 return spte;
3640}
3641
3642void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
3643 const u8 *new, int bytes)
3644{
3645 gfn_t gfn = gpa >> PAGE_SHIFT;
3646 union kvm_mmu_page_role mask = { .word = 0 };
3647 struct kvm_mmu_page *sp;
3648 struct hlist_node *node;
3649 LIST_HEAD(invalid_list);
3650 u64 entry, gentry, *spte;
3651 int npte;
a30f47cb 3652 bool remote_flush, local_flush, zap_page;
889e5cbc
XG
3653
3654 /*
3655 * If we don't have indirect shadow pages, it means no page is
3656 * write-protected, so we can exit simply.
3657 */
3658 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3659 return;
3660
3661 zap_page = remote_flush = local_flush = false;
3662
3663 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
3664
3665 gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
3666
3667 /*
3668 * No need to care whether allocation memory is successful
3669 * or not since pte prefetch is skiped if it does not have
3670 * enough objects in the cache.
3671 */
3672 mmu_topup_memory_caches(vcpu);
3673
3674 spin_lock(&vcpu->kvm->mmu_lock);
3675 ++vcpu->kvm->stat.mmu_pte_write;
0375f7fa 3676 kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
889e5cbc 3677
fa1de2bf 3678 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
f41d335a 3679 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
a30f47cb 3680 spte = get_written_sptes(sp, gpa, &npte);
889e5cbc 3681
a30f47cb
XG
3682 if (detect_write_misaligned(sp, gpa, bytes) ||
3683 detect_write_flooding(sp, spte)) {
0671a8e7 3684 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
f41d335a 3685 &invalid_list);
4cee5764 3686 ++vcpu->kvm->stat.mmu_flooded;
0e7bc4b9
AK
3687 continue;
3688 }
889e5cbc
XG
3689
3690 spte = get_written_sptes(sp, gpa, &npte);
3691 if (!spte)
3692 continue;
3693
0671a8e7 3694 local_flush = true;
ac1b714e 3695 while (npte--) {
79539cec 3696 entry = *spte;
38e3b2b2 3697 mmu_page_zap_pte(vcpu->kvm, sp, spte);
fa1de2bf
XG
3698 if (gentry &&
3699 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
f759e2b4 3700 & mask.word) && rmap_can_add(vcpu))
7c562522 3701 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
0671a8e7
XG
3702 if (!remote_flush && need_remote_flush(entry, *spte))
3703 remote_flush = true;
ac1b714e 3704 ++spte;
9b7a0325 3705 }
9b7a0325 3706 }
0671a8e7 3707 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
d98ba053 3708 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
0375f7fa 3709 kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
aaee2c94 3710 spin_unlock(&vcpu->kvm->mmu_lock);
da4a00f0
AK
3711}
3712
a436036b
AK
3713int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3714{
10589a46
MT
3715 gpa_t gpa;
3716 int r;
a436036b 3717
c5a78f2b 3718 if (vcpu->arch.mmu.direct_map)
60f24784
AK
3719 return 0;
3720
1871c602 3721 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
10589a46 3722
10589a46 3723 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1cb3f3ae 3724
10589a46 3725 return r;
a436036b 3726}
577bdc49 3727EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
a436036b 3728
22d95b12 3729void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
ebeace86 3730{
d98ba053 3731 LIST_HEAD(invalid_list);
103ad25a 3732
e0df7b9f 3733 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
3b80fffe 3734 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
4db35314 3735 struct kvm_mmu_page *sp;
ebeace86 3736
f05e70ac 3737 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
4db35314 3738 struct kvm_mmu_page, link);
e0df7b9f 3739 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
4cee5764 3740 ++vcpu->kvm->stat.mmu_recycled;
ebeace86 3741 }
aa6bd187 3742 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
ebeace86 3743}
ebeace86 3744
1cb3f3ae
XG
3745static bool is_mmio_page_fault(struct kvm_vcpu *vcpu, gva_t addr)
3746{
3747 if (vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu))
3748 return vcpu_match_mmio_gpa(vcpu, addr);
3749
3750 return vcpu_match_mmio_gva(vcpu, addr);
3751}
3752
dc25e89e
AP
3753int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3754 void *insn, int insn_len)
3067714c 3755{
1cb3f3ae 3756 int r, emulation_type = EMULTYPE_RETRY;
3067714c
AK
3757 enum emulation_result er;
3758
56028d08 3759 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
3067714c
AK
3760 if (r < 0)
3761 goto out;
3762
3763 if (!r) {
3764 r = 1;
3765 goto out;
3766 }
3767
1cb3f3ae
XG
3768 if (is_mmio_page_fault(vcpu, cr2))
3769 emulation_type = 0;
3770
3771 er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
3067714c
AK
3772
3773 switch (er) {
3774 case EMULATE_DONE:
3775 return 1;
3776 case EMULATE_DO_MMIO:
3777 ++vcpu->stat.mmio_exits;
6d77dbfc 3778 /* fall through */
3067714c 3779 case EMULATE_FAIL:
3f5d18a9 3780 return 0;
3067714c
AK
3781 default:
3782 BUG();
3783 }
3784out:
3067714c
AK
3785 return r;
3786}
3787EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3788
a7052897
MT
3789void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3790{
a7052897 3791 vcpu->arch.mmu.invlpg(vcpu, gva);
a7052897
MT
3792 kvm_mmu_flush_tlb(vcpu);
3793 ++vcpu->stat.invlpg;
3794}
3795EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3796
18552672
JR
3797void kvm_enable_tdp(void)
3798{
3799 tdp_enabled = true;
3800}
3801EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3802
5f4cb662
JR
3803void kvm_disable_tdp(void)
3804{
3805 tdp_enabled = false;
3806}
3807EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3808
6aa8b732
AK
3809static void free_mmu_pages(struct kvm_vcpu *vcpu)
3810{
ad312c7c 3811 free_page((unsigned long)vcpu->arch.mmu.pae_root);
81407ca5
JR
3812 if (vcpu->arch.mmu.lm_root != NULL)
3813 free_page((unsigned long)vcpu->arch.mmu.lm_root);
6aa8b732
AK
3814}
3815
3816static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3817{
17ac10ad 3818 struct page *page;
6aa8b732
AK
3819 int i;
3820
3821 ASSERT(vcpu);
3822
17ac10ad
AK
3823 /*
3824 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3825 * Therefore we need to allocate shadow page tables in the first
3826 * 4GB of memory, which happens to fit the DMA32 zone.
3827 */
3828 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3829 if (!page)
d7fa6ab2
WY
3830 return -ENOMEM;
3831
ad312c7c 3832 vcpu->arch.mmu.pae_root = page_address(page);
17ac10ad 3833 for (i = 0; i < 4; ++i)
ad312c7c 3834 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
17ac10ad 3835
6aa8b732 3836 return 0;
6aa8b732
AK
3837}
3838
8018c27b 3839int kvm_mmu_create(struct kvm_vcpu *vcpu)
6aa8b732 3840{
6aa8b732 3841 ASSERT(vcpu);
ad312c7c 3842 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
6aa8b732 3843
8018c27b
IM
3844 return alloc_mmu_pages(vcpu);
3845}
6aa8b732 3846
8018c27b
IM
3847int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3848{
3849 ASSERT(vcpu);
ad312c7c 3850 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
2c264957 3851
8018c27b 3852 return init_kvm_mmu(vcpu);
6aa8b732
AK
3853}
3854
90cb0529 3855void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
6aa8b732 3856{
4db35314 3857 struct kvm_mmu_page *sp;
6aa8b732 3858
f05e70ac 3859 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
6aa8b732
AK
3860 int i;
3861 u64 *pt;
3862
291f26bc 3863 if (!test_bit(slot, sp->slot_bitmap))
6aa8b732
AK
3864 continue;
3865
4db35314 3866 pt = sp->spt;
8234b22e 3867 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
da8dc75f
XG
3868 if (!is_shadow_present_pte(pt[i]) ||
3869 !is_last_spte(pt[i], sp->role.level))
3870 continue;
3871
3872 if (is_large_pte(pt[i])) {
c3707958 3873 drop_spte(kvm, &pt[i]);
8234b22e 3874 --kvm->stat.lpages;
da8dc75f 3875 continue;
8234b22e 3876 }
da8dc75f 3877
6aa8b732 3878 /* avoid RMW */
01c168ac 3879 if (is_writable_pte(pt[i]))
1df9f2dc
XG
3880 mmu_spte_update(&pt[i],
3881 pt[i] & ~PT_WRITABLE_MASK);
8234b22e 3882 }
6aa8b732 3883 }
171d595d 3884 kvm_flush_remote_tlbs(kvm);
6aa8b732 3885}
37a7d8b0 3886
90cb0529 3887void kvm_mmu_zap_all(struct kvm *kvm)
e0fa826f 3888{
4db35314 3889 struct kvm_mmu_page *sp, *node;
d98ba053 3890 LIST_HEAD(invalid_list);
e0fa826f 3891
aaee2c94 3892 spin_lock(&kvm->mmu_lock);
3246af0e 3893restart:
f05e70ac 3894 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
d98ba053 3895 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
3246af0e
XG
3896 goto restart;
3897
d98ba053 3898 kvm_mmu_commit_zap_page(kvm, &invalid_list);
aaee2c94 3899 spin_unlock(&kvm->mmu_lock);
e0fa826f
DL
3900}
3901
d98ba053
XG
3902static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3903 struct list_head *invalid_list)
3ee16c81
IE
3904{
3905 struct kvm_mmu_page *page;
3906
3907 page = container_of(kvm->arch.active_mmu_pages.prev,
3908 struct kvm_mmu_page, link);
d98ba053 3909 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
3ee16c81
IE
3910}
3911
1495f230 3912static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
3ee16c81
IE
3913{
3914 struct kvm *kvm;
3915 struct kvm *kvm_freed = NULL;
1495f230 3916 int nr_to_scan = sc->nr_to_scan;
45221ab6
DH
3917
3918 if (nr_to_scan == 0)
3919 goto out;
3ee16c81 3920
e935b837 3921 raw_spin_lock(&kvm_lock);
3ee16c81
IE
3922
3923 list_for_each_entry(kvm, &vm_list, vm_list) {
45221ab6 3924 int idx, freed_pages;
d98ba053 3925 LIST_HEAD(invalid_list);
3ee16c81 3926
f656ce01 3927 idx = srcu_read_lock(&kvm->srcu);
3ee16c81 3928 spin_lock(&kvm->mmu_lock);
45221ab6
DH
3929 if (!kvm_freed && nr_to_scan > 0 &&
3930 kvm->arch.n_used_mmu_pages > 0) {
d98ba053
XG
3931 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3932 &invalid_list);
3ee16c81
IE
3933 kvm_freed = kvm;
3934 }
3935 nr_to_scan--;
3936
d98ba053 3937 kvm_mmu_commit_zap_page(kvm, &invalid_list);
3ee16c81 3938 spin_unlock(&kvm->mmu_lock);
f656ce01 3939 srcu_read_unlock(&kvm->srcu, idx);
3ee16c81
IE
3940 }
3941 if (kvm_freed)
3942 list_move_tail(&kvm_freed->vm_list, &vm_list);
3943
e935b837 3944 raw_spin_unlock(&kvm_lock);
3ee16c81 3945
45221ab6
DH
3946out:
3947 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
3ee16c81
IE
3948}
3949
3950static struct shrinker mmu_shrinker = {
3951 .shrink = mmu_shrink,
3952 .seeks = DEFAULT_SEEKS * 10,
3953};
3954
2ddfd20e 3955static void mmu_destroy_caches(void)
b5a33a75 3956{
53c07b18
XG
3957 if (pte_list_desc_cache)
3958 kmem_cache_destroy(pte_list_desc_cache);
d3d25b04
AK
3959 if (mmu_page_header_cache)
3960 kmem_cache_destroy(mmu_page_header_cache);
b5a33a75
AK
3961}
3962
3963int kvm_mmu_module_init(void)
3964{
53c07b18
XG
3965 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3966 sizeof(struct pte_list_desc),
20c2df83 3967 0, 0, NULL);
53c07b18 3968 if (!pte_list_desc_cache)
b5a33a75
AK
3969 goto nomem;
3970
d3d25b04
AK
3971 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3972 sizeof(struct kvm_mmu_page),
20c2df83 3973 0, 0, NULL);
d3d25b04
AK
3974 if (!mmu_page_header_cache)
3975 goto nomem;
3976
45bf21a8
WY
3977 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3978 goto nomem;
3979
3ee16c81
IE
3980 register_shrinker(&mmu_shrinker);
3981
b5a33a75
AK
3982 return 0;
3983
3984nomem:
3ee16c81 3985 mmu_destroy_caches();
b5a33a75
AK
3986 return -ENOMEM;
3987}
3988
3ad82a7e
ZX
3989/*
3990 * Caculate mmu pages needed for kvm.
3991 */
3992unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3993{
3ad82a7e
ZX
3994 unsigned int nr_mmu_pages;
3995 unsigned int nr_pages = 0;
bc6678a3 3996 struct kvm_memslots *slots;
be6ba0f0 3997 struct kvm_memory_slot *memslot;
3ad82a7e 3998
90d83dc3
LJ
3999 slots = kvm_memslots(kvm);
4000
be6ba0f0
XG
4001 kvm_for_each_memslot(memslot, slots)
4002 nr_pages += memslot->npages;
3ad82a7e
ZX
4003
4004 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
4005 nr_mmu_pages = max(nr_mmu_pages,
4006 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
4007
4008 return nr_mmu_pages;
4009}
4010
94d8b056
MT
4011int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
4012{
4013 struct kvm_shadow_walk_iterator iterator;
c2a2ac2b 4014 u64 spte;
94d8b056
MT
4015 int nr_sptes = 0;
4016
c2a2ac2b
XG
4017 walk_shadow_page_lockless_begin(vcpu);
4018 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
4019 sptes[iterator.level-1] = spte;
94d8b056 4020 nr_sptes++;
c2a2ac2b 4021 if (!is_shadow_present_pte(spte))
94d8b056
MT
4022 break;
4023 }
c2a2ac2b 4024 walk_shadow_page_lockless_end(vcpu);
94d8b056
MT
4025
4026 return nr_sptes;
4027}
4028EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
4029
c42fffe3
XG
4030void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
4031{
4032 ASSERT(vcpu);
4033
4034 destroy_kvm_mmu(vcpu);
4035 free_mmu_pages(vcpu);
4036 mmu_free_memory_caches(vcpu);
b034cf01
XG
4037}
4038
4039#ifdef CONFIG_KVM_MMU_AUDIT
4040#include "mmu_audit.c"
4041#else
4042static void mmu_audit_disable(void) { }
4043#endif
4044
4045void kvm_mmu_module_exit(void)
4046{
4047 mmu_destroy_caches();
4048 percpu_counter_destroy(&kvm_total_used_mmu_pages);
4049 unregister_shrinker(&mmu_shrinker);
c42fffe3
XG
4050 mmu_audit_disable();
4051}