2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * KVM/MIPS MMU handling in the KVM module.
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
12 #include <linux/highmem.h>
13 #include <linux/kvm_host.h>
14 #include <linux/uaccess.h>
15 #include <asm/mmu_context.h>
16 #include <asm/pgalloc.h>
19 * KVM_MMU_CACHE_MIN_PAGES is the number of GPA page table translation levels
20 * for which pages need to be cached.
22 #if defined(__PAGETABLE_PMD_FOLDED)
23 #define KVM_MMU_CACHE_MIN_PAGES 1
25 #define KVM_MMU_CACHE_MIN_PAGES 2
28 void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
30 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
34 * kvm_pgd_init() - Initialise KVM GPA page directory.
35 * @page: Pointer to page directory (PGD) for KVM GPA.
37 * Initialise a KVM GPA page directory with pointers to the invalid table, i.e.
38 * representing no mappings. This is similar to pgd_init(), however it
39 * initialises all the page directory pointers, not just the ones corresponding
40 * to the userland address space (since it is for the guest physical address
41 * space rather than a virtual address space).
43 static void kvm_pgd_init(void *page)
45 unsigned long *p, *end;
48 #ifdef __PAGETABLE_PMD_FOLDED
49 entry = (unsigned long)invalid_pte_table;
51 entry = (unsigned long)invalid_pmd_table;
54 p = (unsigned long *)page;
55 end = p + PTRS_PER_PGD;
71 * kvm_pgd_alloc() - Allocate and initialise a KVM GPA page directory.
73 * Allocate a blank KVM GPA page directory (PGD) for representing guest physical
74 * to host physical page mappings.
76 * Returns: Pointer to new KVM GPA page directory.
77 * NULL on allocation failure.
79 pgd_t *kvm_pgd_alloc(void)
83 ret = (pgd_t *)__get_free_pages(GFP_KERNEL, PGD_TABLE_ORDER);
91 * kvm_mips_walk_pgd() - Walk page table with optional allocation.
92 * @pgd: Page directory pointer.
93 * @addr: Address to index page table using.
94 * @cache: MMU page cache to allocate new page tables from, or NULL.
96 * Walk the page tables pointed to by @pgd to find the PTE corresponding to the
97 * address @addr. If page tables don't exist for @addr, they will be created
98 * from the MMU cache if @cache is not NULL.
100 * Returns: Pointer to pte_t corresponding to @addr.
101 * NULL if a page table doesn't exist for @addr and !@cache.
102 * NULL if a page table allocation failed.
104 static pte_t *kvm_mips_walk_pgd(pgd_t *pgd, struct kvm_mmu_memory_cache *cache,
111 pgd += pgd_index(addr);
112 if (pgd_none(*pgd)) {
113 /* Not used on MIPS yet */
117 p4d = p4d_offset(pgd, addr);
118 pud = pud_offset(p4d, addr);
119 if (pud_none(*pud)) {
124 new_pmd = kvm_mmu_memory_cache_alloc(cache);
126 pud_populate(NULL, pud, new_pmd);
128 pmd = pmd_offset(pud, addr);
129 if (pmd_none(*pmd)) {
134 new_pte = kvm_mmu_memory_cache_alloc(cache);
136 pmd_populate_kernel(NULL, pmd, new_pte);
138 return pte_offset_kernel(pmd, addr);
141 /* Caller must hold kvm->mm_lock */
142 static pte_t *kvm_mips_pte_for_gpa(struct kvm *kvm,
143 struct kvm_mmu_memory_cache *cache,
146 return kvm_mips_walk_pgd(kvm->arch.gpa_mm.pgd, cache, addr);
150 * kvm_mips_flush_gpa_{pte,pmd,pud,pgd,pt}.
151 * Flush a range of guest physical address space from the VM's GPA page tables.
154 static bool kvm_mips_flush_gpa_pte(pte_t *pte, unsigned long start_gpa,
155 unsigned long end_gpa)
157 int i_min = pte_index(start_gpa);
158 int i_max = pte_index(end_gpa);
159 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
162 for (i = i_min; i <= i_max; ++i) {
163 if (!pte_present(pte[i]))
166 set_pte(pte + i, __pte(0));
168 return safe_to_remove;
171 static bool kvm_mips_flush_gpa_pmd(pmd_t *pmd, unsigned long start_gpa,
172 unsigned long end_gpa)
175 unsigned long end = ~0ul;
176 int i_min = pmd_index(start_gpa);
177 int i_max = pmd_index(end_gpa);
178 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
181 for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
182 if (!pmd_present(pmd[i]))
185 pte = pte_offset_kernel(pmd + i, 0);
189 if (kvm_mips_flush_gpa_pte(pte, start_gpa, end)) {
191 pte_free_kernel(NULL, pte);
193 safe_to_remove = false;
196 return safe_to_remove;
199 static bool kvm_mips_flush_gpa_pud(pud_t *pud, unsigned long start_gpa,
200 unsigned long end_gpa)
203 unsigned long end = ~0ul;
204 int i_min = pud_index(start_gpa);
205 int i_max = pud_index(end_gpa);
206 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
209 for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
210 if (!pud_present(pud[i]))
213 pmd = pmd_offset(pud + i, 0);
217 if (kvm_mips_flush_gpa_pmd(pmd, start_gpa, end)) {
221 safe_to_remove = false;
224 return safe_to_remove;
227 static bool kvm_mips_flush_gpa_pgd(pgd_t *pgd, unsigned long start_gpa,
228 unsigned long end_gpa)
232 unsigned long end = ~0ul;
233 int i_min = pgd_index(start_gpa);
234 int i_max = pgd_index(end_gpa);
235 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
238 for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
239 if (!pgd_present(pgd[i]))
242 p4d = p4d_offset(pgd, 0);
243 pud = pud_offset(p4d + i, 0);
247 if (kvm_mips_flush_gpa_pud(pud, start_gpa, end)) {
251 safe_to_remove = false;
254 return safe_to_remove;
258 * kvm_mips_flush_gpa_pt() - Flush a range of guest physical addresses.
260 * @start_gfn: Guest frame number of first page in GPA range to flush.
261 * @end_gfn: Guest frame number of last page in GPA range to flush.
263 * Flushes a range of GPA mappings from the GPA page tables.
265 * The caller must hold the @kvm->mmu_lock spinlock.
267 * Returns: Whether its safe to remove the top level page directory because
268 * all lower levels have been removed.
270 bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
272 return kvm_mips_flush_gpa_pgd(kvm->arch.gpa_mm.pgd,
273 start_gfn << PAGE_SHIFT,
274 end_gfn << PAGE_SHIFT);
277 #define BUILD_PTE_RANGE_OP(name, op) \
278 static int kvm_mips_##name##_pte(pte_t *pte, unsigned long start, \
282 int i_min = pte_index(start); \
283 int i_max = pte_index(end); \
287 for (i = i_min; i <= i_max; ++i) { \
288 if (!pte_present(pte[i])) \
293 if (pte_val(new) == pte_val(old)) \
295 set_pte(pte + i, new); \
301 /* returns true if anything was done */ \
302 static int kvm_mips_##name##_pmd(pmd_t *pmd, unsigned long start, \
307 unsigned long cur_end = ~0ul; \
308 int i_min = pmd_index(start); \
309 int i_max = pmd_index(end); \
312 for (i = i_min; i <= i_max; ++i, start = 0) { \
313 if (!pmd_present(pmd[i])) \
316 pte = pte_offset_kernel(pmd + i, 0); \
320 ret |= kvm_mips_##name##_pte(pte, start, cur_end); \
325 static int kvm_mips_##name##_pud(pud_t *pud, unsigned long start, \
330 unsigned long cur_end = ~0ul; \
331 int i_min = pud_index(start); \
332 int i_max = pud_index(end); \
335 for (i = i_min; i <= i_max; ++i, start = 0) { \
336 if (!pud_present(pud[i])) \
339 pmd = pmd_offset(pud + i, 0); \
343 ret |= kvm_mips_##name##_pmd(pmd, start, cur_end); \
348 static int kvm_mips_##name##_pgd(pgd_t *pgd, unsigned long start, \
354 unsigned long cur_end = ~0ul; \
355 int i_min = pgd_index(start); \
356 int i_max = pgd_index(end); \
359 for (i = i_min; i <= i_max; ++i, start = 0) { \
360 if (!pgd_present(pgd[i])) \
363 p4d = p4d_offset(pgd, 0); \
364 pud = pud_offset(p4d + i, 0); \
368 ret |= kvm_mips_##name##_pud(pud, start, cur_end); \
374 * kvm_mips_mkclean_gpa_pt.
375 * Mark a range of guest physical address space clean (writes fault) in the VM's
376 * GPA page table to allow dirty page tracking.
379 BUILD_PTE_RANGE_OP(mkclean, pte_mkclean)
382 * kvm_mips_mkclean_gpa_pt() - Make a range of guest physical addresses clean.
384 * @start_gfn: Guest frame number of first page in GPA range to flush.
385 * @end_gfn: Guest frame number of last page in GPA range to flush.
387 * Make a range of GPA mappings clean so that guest writes will fault and
388 * trigger dirty page logging.
390 * The caller must hold the @kvm->mmu_lock spinlock.
392 * Returns: Whether any GPA mappings were modified, which would require
393 * derived mappings (GVA page tables & TLB enties) to be
396 int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
398 return kvm_mips_mkclean_pgd(kvm->arch.gpa_mm.pgd,
399 start_gfn << PAGE_SHIFT,
400 end_gfn << PAGE_SHIFT);
404 * kvm_arch_mmu_enable_log_dirty_pt_masked() - write protect dirty pages
405 * @kvm: The KVM pointer
406 * @slot: The memory slot associated with mask
407 * @gfn_offset: The gfn offset in memory slot
408 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
409 * slot to be write protected
411 * Walks bits set in mask write protects the associated pte's. Caller must
412 * acquire @kvm->mmu_lock.
414 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
415 struct kvm_memory_slot *slot,
416 gfn_t gfn_offset, unsigned long mask)
418 gfn_t base_gfn = slot->base_gfn + gfn_offset;
419 gfn_t start = base_gfn + __ffs(mask);
420 gfn_t end = base_gfn + __fls(mask);
422 kvm_mips_mkclean_gpa_pt(kvm, start, end);
426 * kvm_mips_mkold_gpa_pt.
427 * Mark a range of guest physical address space old (all accesses fault) in the
428 * VM's GPA page table to allow detection of commonly used pages.
431 BUILD_PTE_RANGE_OP(mkold, pte_mkold)
433 static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
436 return kvm_mips_mkold_pgd(kvm->arch.gpa_mm.pgd,
437 start_gfn << PAGE_SHIFT,
438 end_gfn << PAGE_SHIFT);
441 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
443 kvm_mips_flush_gpa_pt(kvm, range->start, range->end);
447 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
449 return kvm_mips_mkold_gpa_pt(kvm, range->start, range->end);
452 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
454 gpa_t gpa = range->start << PAGE_SHIFT;
455 pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
459 return pte_young(*gpa_pte);
463 * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
464 * @vcpu: VCPU pointer.
465 * @gpa: Guest physical address of fault.
466 * @write_fault: Whether the fault was due to a write.
467 * @out_entry: New PTE for @gpa (written on success unless NULL).
468 * @out_buddy: New PTE for @gpa's buddy (written on success unless
471 * Perform fast path GPA fault handling, doing all that can be done without
472 * calling into KVM. This handles marking old pages young (for idle page
473 * tracking), and dirtying of clean pages (for dirty page logging).
475 * Returns: 0 on success, in which case we can update derived mappings and
476 * resume guest execution.
477 * -EFAULT on failure due to absent GPA mapping or write to
478 * read-only page, in which case KVM must be consulted.
480 static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa,
482 pte_t *out_entry, pte_t *out_buddy)
484 struct kvm *kvm = vcpu->kvm;
485 gfn_t gfn = gpa >> PAGE_SHIFT;
487 kvm_pfn_t pfn = 0; /* silence bogus GCC warning */
488 bool pfn_valid = false;
491 spin_lock(&kvm->mmu_lock);
493 /* Fast path - just check GPA page table for an existing entry */
494 ptep = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
495 if (!ptep || !pte_present(*ptep)) {
500 /* Track access to pages marked old */
501 if (!pte_young(*ptep)) {
502 set_pte(ptep, pte_mkyoung(*ptep));
503 pfn = pte_pfn(*ptep);
505 /* call kvm_set_pfn_accessed() after unlock */
507 if (write_fault && !pte_dirty(*ptep)) {
508 if (!pte_write(*ptep)) {
513 /* Track dirtying of writeable pages */
514 set_pte(ptep, pte_mkdirty(*ptep));
515 pfn = pte_pfn(*ptep);
516 mark_page_dirty(kvm, gfn);
517 kvm_set_pfn_dirty(pfn);
523 *out_buddy = *ptep_buddy(ptep);
526 spin_unlock(&kvm->mmu_lock);
528 kvm_set_pfn_accessed(pfn);
533 * kvm_mips_map_page() - Map a guest physical page.
534 * @vcpu: VCPU pointer.
535 * @gpa: Guest physical address of fault.
536 * @write_fault: Whether the fault was due to a write.
537 * @out_entry: New PTE for @gpa (written on success unless NULL).
538 * @out_buddy: New PTE for @gpa's buddy (written on success unless
541 * Handle GPA faults by creating a new GPA mapping (or updating an existing
544 * This takes care of marking pages young or dirty (idle/dirty page tracking),
545 * asking KVM for the corresponding PFN, and creating a mapping in the GPA page
546 * tables. Derived mappings (GVA page tables and TLBs) must be handled by the
549 * Returns: 0 on success, in which case the caller may use the @out_entry
550 * and @out_buddy PTEs to update derived mappings and resume guest
552 * -EFAULT if there is no memory region at @gpa or a write was
553 * attempted to a read-only memory region. This is usually handled
556 static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa,
558 pte_t *out_entry, pte_t *out_buddy)
560 struct kvm *kvm = vcpu->kvm;
561 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
562 gfn_t gfn = gpa >> PAGE_SHIFT;
567 unsigned long prot_bits;
568 unsigned long mmu_seq;
570 /* Try the fast path to handle old / clean pages */
571 srcu_idx = srcu_read_lock(&kvm->srcu);
572 err = _kvm_mips_map_page_fast(vcpu, gpa, write_fault, out_entry,
577 /* We need a minimum of cached pages ready for page table creation */
578 err = kvm_mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES);
584 * Used to check for invalidations in progress, of the pfn that is
585 * returned by pfn_to_pfn_prot below.
587 mmu_seq = kvm->mmu_invalidate_seq;
589 * Ensure the read of mmu_invalidate_seq isn't reordered with PTE reads
590 * in gfn_to_pfn_prot() (which calls get_user_pages()), so that we don't
591 * risk the page we get a reference to getting unmapped before we have a
592 * chance to grab the mmu_lock without mmu_invalidate_retry() noticing.
594 * This smp_rmb() pairs with the effective smp_wmb() of the combination
595 * of the pte_unmap_unlock() after the PTE is zapped, and the
596 * spin_lock() in kvm_mmu_notifier_invalidate_<page|range_end>() before
597 * mmu_invalidate_seq is incremented.
601 /* Slow path - ask KVM core whether we can access this GPA */
602 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writeable);
603 if (is_error_noslot_pfn(pfn)) {
608 spin_lock(&kvm->mmu_lock);
609 /* Check if an invalidation has taken place since we got pfn */
610 if (mmu_invalidate_retry(kvm, mmu_seq)) {
612 * This can happen when mappings are changed asynchronously, but
613 * also synchronously if a COW is triggered by
616 spin_unlock(&kvm->mmu_lock);
617 kvm_release_pfn_clean(pfn);
621 /* Ensure page tables are allocated */
622 ptep = kvm_mips_pte_for_gpa(kvm, memcache, gpa);
625 prot_bits = _PAGE_PRESENT | __READABLE | _page_cachable_default;
627 prot_bits |= _PAGE_WRITE;
629 prot_bits |= __WRITEABLE;
630 mark_page_dirty(kvm, gfn);
631 kvm_set_pfn_dirty(pfn);
634 entry = pfn_pte(pfn, __pgprot(prot_bits));
637 set_pte(ptep, entry);
643 *out_buddy = *ptep_buddy(ptep);
645 spin_unlock(&kvm->mmu_lock);
646 kvm_release_pfn_clean(pfn);
647 kvm_set_pfn_accessed(pfn);
649 srcu_read_unlock(&kvm->srcu, srcu_idx);
653 int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr,
654 struct kvm_vcpu *vcpu,
659 ret = kvm_mips_map_page(vcpu, badvaddr, write_fault, NULL, NULL);
663 /* Invalidate this entry in the TLB */
664 return kvm_vz_host_tlb_inv(vcpu, badvaddr);
668 * kvm_mips_migrate_count() - Migrate timer.
669 * @vcpu: Virtual CPU.
671 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
672 * if it was running prior to being cancelled.
674 * Must be called when the VCPU is migrated to a different CPU to ensure that
675 * timer expiry during guest execution interrupts the guest and causes the
676 * interrupt to be delivered in a timely manner.
678 static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
680 if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
681 hrtimer_restart(&vcpu->arch.comparecount_timer);
684 /* Restore ASID once we are scheduled back after preemption */
685 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
689 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
691 local_irq_save(flags);
694 if (vcpu->arch.last_sched_cpu != cpu) {
695 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
696 vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
698 * Migrate the timer interrupt to the current CPU so that it
699 * always interrupts the guest and synchronously triggers a
700 * guest timer interrupt.
702 kvm_mips_migrate_count(vcpu);
705 /* restore guest state to registers */
706 kvm_mips_callbacks->vcpu_load(vcpu, cpu);
708 local_irq_restore(flags);
711 /* ASID can change if another task is scheduled during preemption */
712 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
717 local_irq_save(flags);
719 cpu = smp_processor_id();
720 vcpu->arch.last_sched_cpu = cpu;
723 /* save guest state in registers */
724 kvm_mips_callbacks->vcpu_put(vcpu, cpu);
726 local_irq_restore(flags);