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.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
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 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
33 BUG_ON(max > KVM_NR_MEM_OBJS);
34 if (cache->nobjs >= min)
36 while (cache->nobjs < max) {
37 page = (void *)__get_free_page(GFP_KERNEL);
40 cache->objects[cache->nobjs++] = page;
45 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
48 free_page((unsigned long)mc->objects[--mc->nobjs]);
51 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
55 BUG_ON(!mc || !mc->nobjs);
56 p = mc->objects[--mc->nobjs];
60 void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
62 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
66 * kvm_pgd_init() - Initialise KVM GPA page directory.
67 * @page: Pointer to page directory (PGD) for KVM GPA.
69 * Initialise a KVM GPA page directory with pointers to the invalid table, i.e.
70 * representing no mappings. This is similar to pgd_init(), however it
71 * initialises all the page directory pointers, not just the ones corresponding
72 * to the userland address space (since it is for the guest physical address
73 * space rather than a virtual address space).
75 static void kvm_pgd_init(void *page)
77 unsigned long *p, *end;
80 #ifdef __PAGETABLE_PMD_FOLDED
81 entry = (unsigned long)invalid_pte_table;
83 entry = (unsigned long)invalid_pmd_table;
86 p = (unsigned long *)page;
87 end = p + PTRS_PER_PGD;
103 * kvm_pgd_alloc() - Allocate and initialise a KVM GPA page directory.
105 * Allocate a blank KVM GPA page directory (PGD) for representing guest physical
106 * to host physical page mappings.
108 * Returns: Pointer to new KVM GPA page directory.
109 * NULL on allocation failure.
111 pgd_t *kvm_pgd_alloc(void)
115 ret = (pgd_t *)__get_free_pages(GFP_KERNEL, PGD_ORDER);
123 * kvm_mips_walk_pgd() - Walk page table with optional allocation.
124 * @pgd: Page directory pointer.
125 * @addr: Address to index page table using.
126 * @cache: MMU page cache to allocate new page tables from, or NULL.
128 * Walk the page tables pointed to by @pgd to find the PTE corresponding to the
129 * address @addr. If page tables don't exist for @addr, they will be created
130 * from the MMU cache if @cache is not NULL.
132 * Returns: Pointer to pte_t corresponding to @addr.
133 * NULL if a page table doesn't exist for @addr and !@cache.
134 * NULL if a page table allocation failed.
136 static pte_t *kvm_mips_walk_pgd(pgd_t *pgd, struct kvm_mmu_memory_cache *cache,
142 pgd += pgd_index(addr);
143 if (pgd_none(*pgd)) {
144 /* Not used on MIPS yet */
148 pud = pud_offset(pgd, addr);
149 if (pud_none(*pud)) {
154 new_pmd = mmu_memory_cache_alloc(cache);
155 pmd_init((unsigned long)new_pmd,
156 (unsigned long)invalid_pte_table);
157 pud_populate(NULL, pud, new_pmd);
159 pmd = pmd_offset(pud, addr);
160 if (pmd_none(*pmd)) {
165 new_pte = mmu_memory_cache_alloc(cache);
167 pmd_populate_kernel(NULL, pmd, new_pte);
169 return pte_offset(pmd, addr);
172 /* Caller must hold kvm->mm_lock */
173 static pte_t *kvm_mips_pte_for_gpa(struct kvm *kvm,
174 struct kvm_mmu_memory_cache *cache,
177 return kvm_mips_walk_pgd(kvm->arch.gpa_mm.pgd, cache, addr);
181 * kvm_mips_flush_gpa_{pte,pmd,pud,pgd,pt}.
182 * Flush a range of guest physical address space from the VM's GPA page tables.
185 static bool kvm_mips_flush_gpa_pte(pte_t *pte, unsigned long start_gpa,
186 unsigned long end_gpa)
188 int i_min = __pte_offset(start_gpa);
189 int i_max = __pte_offset(end_gpa);
190 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
193 for (i = i_min; i <= i_max; ++i) {
194 if (!pte_present(pte[i]))
197 set_pte(pte + i, __pte(0));
199 return safe_to_remove;
202 static bool kvm_mips_flush_gpa_pmd(pmd_t *pmd, unsigned long start_gpa,
203 unsigned long end_gpa)
206 unsigned long end = ~0ul;
207 int i_min = __pmd_offset(start_gpa);
208 int i_max = __pmd_offset(end_gpa);
209 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
212 for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
213 if (!pmd_present(pmd[i]))
216 pte = pte_offset(pmd + i, 0);
220 if (kvm_mips_flush_gpa_pte(pte, start_gpa, end)) {
222 pte_free_kernel(NULL, pte);
224 safe_to_remove = false;
227 return safe_to_remove;
230 static bool kvm_mips_flush_gpa_pud(pud_t *pud, unsigned long start_gpa,
231 unsigned long end_gpa)
234 unsigned long end = ~0ul;
235 int i_min = __pud_offset(start_gpa);
236 int i_max = __pud_offset(end_gpa);
237 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
240 for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
241 if (!pud_present(pud[i]))
244 pmd = pmd_offset(pud + i, 0);
248 if (kvm_mips_flush_gpa_pmd(pmd, start_gpa, end)) {
252 safe_to_remove = false;
255 return safe_to_remove;
258 static bool kvm_mips_flush_gpa_pgd(pgd_t *pgd, unsigned long start_gpa,
259 unsigned long end_gpa)
262 unsigned long end = ~0ul;
263 int i_min = pgd_index(start_gpa);
264 int i_max = pgd_index(end_gpa);
265 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
268 for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
269 if (!pgd_present(pgd[i]))
272 pud = pud_offset(pgd + i, 0);
276 if (kvm_mips_flush_gpa_pud(pud, start_gpa, end)) {
280 safe_to_remove = false;
283 return safe_to_remove;
287 * kvm_mips_flush_gpa_pt() - Flush a range of guest physical addresses.
289 * @start_gfn: Guest frame number of first page in GPA range to flush.
290 * @end_gfn: Guest frame number of last page in GPA range to flush.
292 * Flushes a range of GPA mappings from the GPA page tables.
294 * The caller must hold the @kvm->mmu_lock spinlock.
296 * Returns: Whether its safe to remove the top level page directory because
297 * all lower levels have been removed.
299 bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
301 return kvm_mips_flush_gpa_pgd(kvm->arch.gpa_mm.pgd,
302 start_gfn << PAGE_SHIFT,
303 end_gfn << PAGE_SHIFT);
306 #define BUILD_PTE_RANGE_OP(name, op) \
307 static int kvm_mips_##name##_pte(pte_t *pte, unsigned long start, \
311 int i_min = __pte_offset(start); \
312 int i_max = __pte_offset(end); \
316 for (i = i_min; i <= i_max; ++i) { \
317 if (!pte_present(pte[i])) \
322 if (pte_val(new) == pte_val(old)) \
324 set_pte(pte + i, new); \
330 /* returns true if anything was done */ \
331 static int kvm_mips_##name##_pmd(pmd_t *pmd, unsigned long start, \
336 unsigned long cur_end = ~0ul; \
337 int i_min = __pmd_offset(start); \
338 int i_max = __pmd_offset(end); \
341 for (i = i_min; i <= i_max; ++i, start = 0) { \
342 if (!pmd_present(pmd[i])) \
345 pte = pte_offset(pmd + i, 0); \
349 ret |= kvm_mips_##name##_pte(pte, start, cur_end); \
354 static int kvm_mips_##name##_pud(pud_t *pud, unsigned long start, \
359 unsigned long cur_end = ~0ul; \
360 int i_min = __pud_offset(start); \
361 int i_max = __pud_offset(end); \
364 for (i = i_min; i <= i_max; ++i, start = 0) { \
365 if (!pud_present(pud[i])) \
368 pmd = pmd_offset(pud + i, 0); \
372 ret |= kvm_mips_##name##_pmd(pmd, start, cur_end); \
377 static int kvm_mips_##name##_pgd(pgd_t *pgd, unsigned long start, \
382 unsigned long cur_end = ~0ul; \
383 int i_min = pgd_index(start); \
384 int i_max = pgd_index(end); \
387 for (i = i_min; i <= i_max; ++i, start = 0) { \
388 if (!pgd_present(pgd[i])) \
391 pud = pud_offset(pgd + i, 0); \
395 ret |= kvm_mips_##name##_pud(pud, start, cur_end); \
401 * kvm_mips_mkclean_gpa_pt.
402 * Mark a range of guest physical address space clean (writes fault) in the VM's
403 * GPA page table to allow dirty page tracking.
406 BUILD_PTE_RANGE_OP(mkclean, pte_mkclean)
409 * kvm_mips_mkclean_gpa_pt() - Make a range of guest physical addresses clean.
411 * @start_gfn: Guest frame number of first page in GPA range to flush.
412 * @end_gfn: Guest frame number of last page in GPA range to flush.
414 * Make a range of GPA mappings clean so that guest writes will fault and
415 * trigger dirty page logging.
417 * The caller must hold the @kvm->mmu_lock spinlock.
419 * Returns: Whether any GPA mappings were modified, which would require
420 * derived mappings (GVA page tables & TLB enties) to be
423 int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
425 return kvm_mips_mkclean_pgd(kvm->arch.gpa_mm.pgd,
426 start_gfn << PAGE_SHIFT,
427 end_gfn << PAGE_SHIFT);
431 * kvm_arch_mmu_enable_log_dirty_pt_masked() - write protect dirty pages
432 * @kvm: The KVM pointer
433 * @slot: The memory slot associated with mask
434 * @gfn_offset: The gfn offset in memory slot
435 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
436 * slot to be write protected
438 * Walks bits set in mask write protects the associated pte's. Caller must
439 * acquire @kvm->mmu_lock.
441 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
442 struct kvm_memory_slot *slot,
443 gfn_t gfn_offset, unsigned long mask)
445 gfn_t base_gfn = slot->base_gfn + gfn_offset;
446 gfn_t start = base_gfn + __ffs(mask);
447 gfn_t end = base_gfn + __fls(mask);
449 kvm_mips_mkclean_gpa_pt(kvm, start, end);
453 * kvm_mips_mkold_gpa_pt.
454 * Mark a range of guest physical address space old (all accesses fault) in the
455 * VM's GPA page table to allow detection of commonly used pages.
458 BUILD_PTE_RANGE_OP(mkold, pte_mkold)
460 static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
463 return kvm_mips_mkold_pgd(kvm->arch.gpa_mm.pgd,
464 start_gfn << PAGE_SHIFT,
465 end_gfn << PAGE_SHIFT);
468 static int handle_hva_to_gpa(struct kvm *kvm,
471 int (*handler)(struct kvm *kvm, gfn_t gfn,
473 struct kvm_memory_slot *memslot,
477 struct kvm_memslots *slots;
478 struct kvm_memory_slot *memslot;
481 slots = kvm_memslots(kvm);
483 /* we only care about the pages that the guest sees */
484 kvm_for_each_memslot(memslot, slots) {
485 unsigned long hva_start, hva_end;
488 hva_start = max(start, memslot->userspace_addr);
489 hva_end = min(end, memslot->userspace_addr +
490 (memslot->npages << PAGE_SHIFT));
491 if (hva_start >= hva_end)
495 * {gfn(page) | page intersects with [hva_start, hva_end)} =
496 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
498 gfn = hva_to_gfn_memslot(hva_start, memslot);
499 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
501 ret |= handler(kvm, gfn, gfn_end, memslot, data);
508 static int kvm_unmap_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
509 struct kvm_memory_slot *memslot, void *data)
511 kvm_mips_flush_gpa_pt(kvm, gfn, gfn_end);
515 int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end,
518 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
520 kvm_mips_callbacks->flush_shadow_all(kvm);
524 static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
525 struct kvm_memory_slot *memslot, void *data)
527 gpa_t gpa = gfn << PAGE_SHIFT;
528 pte_t hva_pte = *(pte_t *)data;
529 pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
535 /* Mapping may need adjusting depending on memslot flags */
537 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
538 hva_pte = pte_mkclean(hva_pte);
539 else if (memslot->flags & KVM_MEM_READONLY)
540 hva_pte = pte_wrprotect(hva_pte);
542 set_pte(gpa_pte, hva_pte);
544 /* Replacing an absent or old page doesn't need flushes */
545 if (!pte_present(old_pte) || !pte_young(old_pte))
548 /* Pages swapped, aged, moved, or cleaned require flushes */
549 return !pte_present(hva_pte) ||
550 !pte_young(hva_pte) ||
551 pte_pfn(old_pte) != pte_pfn(hva_pte) ||
552 (pte_dirty(old_pte) && !pte_dirty(hva_pte));
555 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
557 unsigned long end = hva + PAGE_SIZE;
560 ret = handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &pte);
562 kvm_mips_callbacks->flush_shadow_all(kvm);
565 static int kvm_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
566 struct kvm_memory_slot *memslot, void *data)
568 return kvm_mips_mkold_gpa_pt(kvm, gfn, gfn_end);
571 static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
572 struct kvm_memory_slot *memslot, void *data)
574 gpa_t gpa = gfn << PAGE_SHIFT;
575 pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
579 return pte_young(*gpa_pte);
582 int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
584 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
587 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
589 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
593 * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
594 * @vcpu: VCPU pointer.
595 * @gpa: Guest physical address of fault.
596 * @write_fault: Whether the fault was due to a write.
597 * @out_entry: New PTE for @gpa (written on success unless NULL).
598 * @out_buddy: New PTE for @gpa's buddy (written on success unless
601 * Perform fast path GPA fault handling, doing all that can be done without
602 * calling into KVM. This handles marking old pages young (for idle page
603 * tracking), and dirtying of clean pages (for dirty page logging).
605 * Returns: 0 on success, in which case we can update derived mappings and
606 * resume guest execution.
607 * -EFAULT on failure due to absent GPA mapping or write to
608 * read-only page, in which case KVM must be consulted.
610 static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa,
612 pte_t *out_entry, pte_t *out_buddy)
614 struct kvm *kvm = vcpu->kvm;
615 gfn_t gfn = gpa >> PAGE_SHIFT;
617 kvm_pfn_t pfn = 0; /* silence bogus GCC warning */
618 bool pfn_valid = false;
621 spin_lock(&kvm->mmu_lock);
623 /* Fast path - just check GPA page table for an existing entry */
624 ptep = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
625 if (!ptep || !pte_present(*ptep)) {
630 /* Track access to pages marked old */
631 if (!pte_young(*ptep)) {
632 set_pte(ptep, pte_mkyoung(*ptep));
633 pfn = pte_pfn(*ptep);
635 /* call kvm_set_pfn_accessed() after unlock */
637 if (write_fault && !pte_dirty(*ptep)) {
638 if (!pte_write(*ptep)) {
643 /* Track dirtying of writeable pages */
644 set_pte(ptep, pte_mkdirty(*ptep));
645 pfn = pte_pfn(*ptep);
646 mark_page_dirty(kvm, gfn);
647 kvm_set_pfn_dirty(pfn);
653 *out_buddy = *ptep_buddy(ptep);
656 spin_unlock(&kvm->mmu_lock);
658 kvm_set_pfn_accessed(pfn);
663 * kvm_mips_map_page() - Map a guest physical page.
664 * @vcpu: VCPU pointer.
665 * @gpa: Guest physical address of fault.
666 * @write_fault: Whether the fault was due to a write.
667 * @out_entry: New PTE for @gpa (written on success unless NULL).
668 * @out_buddy: New PTE for @gpa's buddy (written on success unless
671 * Handle GPA faults by creating a new GPA mapping (or updating an existing
674 * This takes care of marking pages young or dirty (idle/dirty page tracking),
675 * asking KVM for the corresponding PFN, and creating a mapping in the GPA page
676 * tables. Derived mappings (GVA page tables and TLBs) must be handled by the
679 * Returns: 0 on success, in which case the caller may use the @out_entry
680 * and @out_buddy PTEs to update derived mappings and resume guest
682 * -EFAULT if there is no memory region at @gpa or a write was
683 * attempted to a read-only memory region. This is usually handled
686 static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa,
688 pte_t *out_entry, pte_t *out_buddy)
690 struct kvm *kvm = vcpu->kvm;
691 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
692 gfn_t gfn = gpa >> PAGE_SHIFT;
695 pte_t *ptep, entry, old_pte;
697 unsigned long prot_bits;
698 unsigned long mmu_seq;
700 /* Try the fast path to handle old / clean pages */
701 srcu_idx = srcu_read_lock(&kvm->srcu);
702 err = _kvm_mips_map_page_fast(vcpu, gpa, write_fault, out_entry,
707 /* We need a minimum of cached pages ready for page table creation */
708 err = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
715 * Used to check for invalidations in progress, of the pfn that is
716 * returned by pfn_to_pfn_prot below.
718 mmu_seq = kvm->mmu_notifier_seq;
720 * Ensure the read of mmu_notifier_seq isn't reordered with PTE reads in
721 * gfn_to_pfn_prot() (which calls get_user_pages()), so that we don't
722 * risk the page we get a reference to getting unmapped before we have a
723 * chance to grab the mmu_lock without mmu_notifier_retry() noticing.
725 * This smp_rmb() pairs with the effective smp_wmb() of the combination
726 * of the pte_unmap_unlock() after the PTE is zapped, and the
727 * spin_lock() in kvm_mmu_notifier_invalidate_<page|range_end>() before
728 * mmu_notifier_seq is incremented.
732 /* Slow path - ask KVM core whether we can access this GPA */
733 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writeable);
734 if (is_error_noslot_pfn(pfn)) {
739 spin_lock(&kvm->mmu_lock);
740 /* Check if an invalidation has taken place since we got pfn */
741 if (mmu_notifier_retry(kvm, mmu_seq)) {
743 * This can happen when mappings are changed asynchronously, but
744 * also synchronously if a COW is triggered by
747 spin_unlock(&kvm->mmu_lock);
748 kvm_release_pfn_clean(pfn);
752 /* Ensure page tables are allocated */
753 ptep = kvm_mips_pte_for_gpa(kvm, memcache, gpa);
756 prot_bits = _PAGE_PRESENT | __READABLE | _page_cachable_default;
758 prot_bits |= _PAGE_WRITE;
760 prot_bits |= __WRITEABLE;
761 mark_page_dirty(kvm, gfn);
762 kvm_set_pfn_dirty(pfn);
765 entry = pfn_pte(pfn, __pgprot(prot_bits));
769 set_pte(ptep, entry);
775 *out_buddy = *ptep_buddy(ptep);
777 spin_unlock(&kvm->mmu_lock);
778 kvm_release_pfn_clean(pfn);
779 kvm_set_pfn_accessed(pfn);
781 srcu_read_unlock(&kvm->srcu, srcu_idx);
785 static pte_t *kvm_trap_emul_pte_for_gva(struct kvm_vcpu *vcpu,
788 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
792 /* We need a minimum of cached pages ready for page table creation */
793 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
798 if (KVM_GUEST_KERNEL_MODE(vcpu))
799 pgdp = vcpu->arch.guest_kernel_mm.pgd;
801 pgdp = vcpu->arch.guest_user_mm.pgd;
803 return kvm_mips_walk_pgd(pgdp, memcache, addr);
806 void kvm_trap_emul_invalidate_gva(struct kvm_vcpu *vcpu, unsigned long addr,
812 addr &= PAGE_MASK << 1;
814 pgdp = vcpu->arch.guest_kernel_mm.pgd;
815 ptep = kvm_mips_walk_pgd(pgdp, NULL, addr);
817 ptep[0] = pfn_pte(0, __pgprot(0));
818 ptep[1] = pfn_pte(0, __pgprot(0));
822 pgdp = vcpu->arch.guest_user_mm.pgd;
823 ptep = kvm_mips_walk_pgd(pgdp, NULL, addr);
825 ptep[0] = pfn_pte(0, __pgprot(0));
826 ptep[1] = pfn_pte(0, __pgprot(0));
832 * kvm_mips_flush_gva_{pte,pmd,pud,pgd,pt}.
833 * Flush a range of guest physical address space from the VM's GPA page tables.
836 static bool kvm_mips_flush_gva_pte(pte_t *pte, unsigned long start_gva,
837 unsigned long end_gva)
839 int i_min = __pte_offset(start_gva);
840 int i_max = __pte_offset(end_gva);
841 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
845 * There's no freeing to do, so there's no point clearing individual
846 * entries unless only part of the last level page table needs flushing.
851 for (i = i_min; i <= i_max; ++i) {
852 if (!pte_present(pte[i]))
855 set_pte(pte + i, __pte(0));
860 static bool kvm_mips_flush_gva_pmd(pmd_t *pmd, unsigned long start_gva,
861 unsigned long end_gva)
864 unsigned long end = ~0ul;
865 int i_min = __pmd_offset(start_gva);
866 int i_max = __pmd_offset(end_gva);
867 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
870 for (i = i_min; i <= i_max; ++i, start_gva = 0) {
871 if (!pmd_present(pmd[i]))
874 pte = pte_offset(pmd + i, 0);
878 if (kvm_mips_flush_gva_pte(pte, start_gva, end)) {
880 pte_free_kernel(NULL, pte);
882 safe_to_remove = false;
885 return safe_to_remove;
888 static bool kvm_mips_flush_gva_pud(pud_t *pud, unsigned long start_gva,
889 unsigned long end_gva)
892 unsigned long end = ~0ul;
893 int i_min = __pud_offset(start_gva);
894 int i_max = __pud_offset(end_gva);
895 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
898 for (i = i_min; i <= i_max; ++i, start_gva = 0) {
899 if (!pud_present(pud[i]))
902 pmd = pmd_offset(pud + i, 0);
906 if (kvm_mips_flush_gva_pmd(pmd, start_gva, end)) {
910 safe_to_remove = false;
913 return safe_to_remove;
916 static bool kvm_mips_flush_gva_pgd(pgd_t *pgd, unsigned long start_gva,
917 unsigned long end_gva)
920 unsigned long end = ~0ul;
921 int i_min = pgd_index(start_gva);
922 int i_max = pgd_index(end_gva);
923 bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
926 for (i = i_min; i <= i_max; ++i, start_gva = 0) {
927 if (!pgd_present(pgd[i]))
930 pud = pud_offset(pgd + i, 0);
934 if (kvm_mips_flush_gva_pud(pud, start_gva, end)) {
938 safe_to_remove = false;
941 return safe_to_remove;
944 void kvm_mips_flush_gva_pt(pgd_t *pgd, enum kvm_mips_flush flags)
946 if (flags & KMF_GPA) {
947 /* all of guest virtual address space could be affected */
948 if (flags & KMF_KERN)
949 /* useg, kseg0, seg2/3 */
950 kvm_mips_flush_gva_pgd(pgd, 0, 0x7fffffff);
953 kvm_mips_flush_gva_pgd(pgd, 0, 0x3fffffff);
956 kvm_mips_flush_gva_pgd(pgd, 0, 0x3fffffff);
959 if (flags & KMF_KERN)
960 kvm_mips_flush_gva_pgd(pgd, 0x60000000, 0x7fffffff);
964 static pte_t kvm_mips_gpa_pte_to_gva_unmapped(pte_t pte)
967 * Don't leak writeable but clean entries from GPA page tables. We don't
968 * want the normal Linux tlbmod handler to handle dirtying when KVM
969 * accesses guest memory.
972 pte = pte_wrprotect(pte);
977 static pte_t kvm_mips_gpa_pte_to_gva_mapped(pte_t pte, long entrylo)
979 /* Guest EntryLo overrides host EntryLo */
980 if (!(entrylo & ENTRYLO_D))
981 pte = pte_mkclean(pte);
983 return kvm_mips_gpa_pte_to_gva_unmapped(pte);
986 #ifdef CONFIG_KVM_MIPS_VZ
987 int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr,
988 struct kvm_vcpu *vcpu,
993 ret = kvm_mips_map_page(vcpu, badvaddr, write_fault, NULL, NULL);
997 /* Invalidate this entry in the TLB */
998 return kvm_vz_host_tlb_inv(vcpu, badvaddr);
1002 /* XXXKYMA: Must be called with interrupts disabled */
1003 int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
1004 struct kvm_vcpu *vcpu,
1008 pte_t pte_gpa[2], *ptep_gva;
1011 if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
1012 kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
1013 kvm_mips_dump_host_tlbs();
1017 /* Get the GPA page table entry */
1018 gpa = KVM_GUEST_CPHYSADDR(badvaddr);
1019 idx = (badvaddr >> PAGE_SHIFT) & 1;
1020 if (kvm_mips_map_page(vcpu, gpa, write_fault, &pte_gpa[idx],
1021 &pte_gpa[!idx]) < 0)
1024 /* Get the GVA page table entry */
1025 ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, badvaddr & ~PAGE_SIZE);
1027 kvm_err("No ptep for gva %lx\n", badvaddr);
1031 /* Copy a pair of entries from GPA page table to GVA page table */
1032 ptep_gva[0] = kvm_mips_gpa_pte_to_gva_unmapped(pte_gpa[0]);
1033 ptep_gva[1] = kvm_mips_gpa_pte_to_gva_unmapped(pte_gpa[1]);
1035 /* Invalidate this entry in the TLB, guest kernel ASID only */
1036 kvm_mips_host_tlb_inv(vcpu, badvaddr, false, true);
1040 int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
1041 struct kvm_mips_tlb *tlb,
1045 struct kvm *kvm = vcpu->kvm;
1047 pte_t pte_gpa[2], *ptep_buddy, *ptep_gva;
1048 unsigned int idx = TLB_LO_IDX(*tlb, gva);
1049 bool kernel = KVM_GUEST_KERNEL_MODE(vcpu);
1051 tlb_lo[0] = tlb->tlb_lo[0];
1052 tlb_lo[1] = tlb->tlb_lo[1];
1055 * The commpage address must not be mapped to anything else if the guest
1056 * TLB contains entries nearby, or commpage accesses will break.
1058 if (!((gva ^ KVM_GUEST_COMMPAGE_ADDR) & VPN2_MASK & (PAGE_MASK << 1)))
1059 tlb_lo[TLB_LO_IDX(*tlb, KVM_GUEST_COMMPAGE_ADDR)] = 0;
1061 /* Get the GPA page table entry */
1062 if (kvm_mips_map_page(vcpu, mips3_tlbpfn_to_paddr(tlb_lo[idx]),
1063 write_fault, &pte_gpa[idx], NULL) < 0)
1066 /* And its GVA buddy's GPA page table entry if it also exists */
1067 pte_gpa[!idx] = pfn_pte(0, __pgprot(0));
1068 if (tlb_lo[!idx] & ENTRYLO_V) {
1069 spin_lock(&kvm->mmu_lock);
1070 ptep_buddy = kvm_mips_pte_for_gpa(kvm, NULL,
1071 mips3_tlbpfn_to_paddr(tlb_lo[!idx]));
1073 pte_gpa[!idx] = *ptep_buddy;
1074 spin_unlock(&kvm->mmu_lock);
1077 /* Get the GVA page table entry pair */
1078 ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, gva & ~PAGE_SIZE);
1080 kvm_err("No ptep for gva %lx\n", gva);
1084 /* Copy a pair of entries from GPA page table to GVA page table */
1085 ptep_gva[0] = kvm_mips_gpa_pte_to_gva_mapped(pte_gpa[0], tlb_lo[0]);
1086 ptep_gva[1] = kvm_mips_gpa_pte_to_gva_mapped(pte_gpa[1], tlb_lo[1]);
1088 /* Invalidate this entry in the TLB, current guest mode ASID only */
1089 kvm_mips_host_tlb_inv(vcpu, gva, !kernel, kernel);
1091 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
1092 tlb->tlb_lo[0], tlb->tlb_lo[1]);
1097 int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
1098 struct kvm_vcpu *vcpu)
1103 ptep = kvm_trap_emul_pte_for_gva(vcpu, badvaddr);
1105 kvm_err("No ptep for commpage %lx\n", badvaddr);
1109 pfn = PFN_DOWN(virt_to_phys(vcpu->arch.kseg0_commpage));
1110 /* Also set valid and dirty, so refill handler doesn't have to */
1111 *ptep = pte_mkyoung(pte_mkdirty(pfn_pte(pfn, PAGE_SHARED)));
1113 /* Invalidate this entry in the TLB, guest kernel ASID only */
1114 kvm_mips_host_tlb_inv(vcpu, badvaddr, false, true);
1119 * kvm_mips_migrate_count() - Migrate timer.
1120 * @vcpu: Virtual CPU.
1122 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
1123 * if it was running prior to being cancelled.
1125 * Must be called when the VCPU is migrated to a different CPU to ensure that
1126 * timer expiry during guest execution interrupts the guest and causes the
1127 * interrupt to be delivered in a timely manner.
1129 static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
1131 if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
1132 hrtimer_restart(&vcpu->arch.comparecount_timer);
1135 /* Restore ASID once we are scheduled back after preemption */
1136 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1138 unsigned long flags;
1140 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
1142 local_irq_save(flags);
1145 if (vcpu->arch.last_sched_cpu != cpu) {
1146 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
1147 vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
1149 * Migrate the timer interrupt to the current CPU so that it
1150 * always interrupts the guest and synchronously triggers a
1151 * guest timer interrupt.
1153 kvm_mips_migrate_count(vcpu);
1156 /* restore guest state to registers */
1157 kvm_mips_callbacks->vcpu_load(vcpu, cpu);
1159 local_irq_restore(flags);
1162 /* ASID can change if another task is scheduled during preemption */
1163 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1165 unsigned long flags;
1168 local_irq_save(flags);
1170 cpu = smp_processor_id();
1171 vcpu->arch.last_sched_cpu = cpu;
1174 /* save guest state in registers */
1175 kvm_mips_callbacks->vcpu_put(vcpu, cpu);
1177 local_irq_restore(flags);
1181 * kvm_trap_emul_gva_fault() - Safely attempt to handle a GVA access fault.
1182 * @vcpu: Virtual CPU.
1183 * @gva: Guest virtual address to be accessed.
1184 * @write: True if write attempted (must be dirtied and made writable).
1186 * Safely attempt to handle a GVA fault, mapping GVA pages if necessary, and
1187 * dirtying the page if @write so that guest instructions can be modified.
1189 * Returns: KVM_MIPS_MAPPED on success.
1190 * KVM_MIPS_GVA if bad guest virtual address.
1191 * KVM_MIPS_GPA if bad guest physical address.
1192 * KVM_MIPS_TLB if guest TLB not present.
1193 * KVM_MIPS_TLBINV if guest TLB present but not valid.
1194 * KVM_MIPS_TLBMOD if guest TLB read only.
1196 enum kvm_mips_fault_result kvm_trap_emul_gva_fault(struct kvm_vcpu *vcpu,
1200 struct mips_coproc *cop0 = vcpu->arch.cop0;
1201 struct kvm_mips_tlb *tlb;
1204 if (KVM_GUEST_KSEGX(gva) == KVM_GUEST_KSEG0) {
1205 if (kvm_mips_handle_kseg0_tlb_fault(gva, vcpu, write) < 0)
1206 return KVM_MIPS_GPA;
1207 } else if ((KVM_GUEST_KSEGX(gva) < KVM_GUEST_KSEG0) ||
1208 KVM_GUEST_KSEGX(gva) == KVM_GUEST_KSEG23) {
1209 /* Address should be in the guest TLB */
1210 index = kvm_mips_guest_tlb_lookup(vcpu, (gva & VPN2_MASK) |
1211 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID));
1213 return KVM_MIPS_TLB;
1214 tlb = &vcpu->arch.guest_tlb[index];
1216 /* Entry should be valid, and dirty for writes */
1217 if (!TLB_IS_VALID(*tlb, gva))
1218 return KVM_MIPS_TLBINV;
1219 if (write && !TLB_IS_DIRTY(*tlb, gva))
1220 return KVM_MIPS_TLBMOD;
1222 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, gva, write))
1223 return KVM_MIPS_GPA;
1225 return KVM_MIPS_GVA;
1228 return KVM_MIPS_MAPPED;
1231 int kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
1235 if (WARN(IS_ENABLED(CONFIG_KVM_MIPS_VZ),
1236 "Expect BadInstr/BadInstrP registers to be used with VZ\n"))
1240 kvm_trap_emul_gva_lockless_begin(vcpu);
1241 err = get_user(*out, opc);
1242 kvm_trap_emul_gva_lockless_end(vcpu);
1244 if (unlikely(err)) {
1246 * Try to handle the fault, maybe we just raced with a GVA
1249 err = kvm_trap_emul_gva_fault(vcpu, (unsigned long)opc,
1251 if (unlikely(err)) {
1252 kvm_err("%s: illegal address: %p\n",
1257 /* Hopefully it'll work now */