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