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