^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * KVM/MIPS MMU handling in the KVM module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Sanjay Lal <sanjayl@kymasys.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/pgalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * KVM_MMU_CACHE_MIN_PAGES is the number of GPA page table translation levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * for which pages need to be cached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #if defined(__PAGETABLE_PMD_FOLDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define KVM_MMU_CACHE_MIN_PAGES 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define KVM_MMU_CACHE_MIN_PAGES 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * kvm_pgd_init() - Initialise KVM GPA page directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @page: Pointer to page directory (PGD) for KVM GPA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Initialise a KVM GPA page directory with pointers to the invalid table, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * representing no mappings. This is similar to pgd_init(), however it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * initialises all the page directory pointers, not just the ones corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * to the userland address space (since it is for the guest physical address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * space rather than a virtual address space).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void kvm_pgd_init(void *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long *p, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #ifdef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) entry = (unsigned long)invalid_pte_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) entry = (unsigned long)invalid_pmd_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) p = (unsigned long *)page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) end = p + PTRS_PER_PGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) p[0] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) p[1] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) p[2] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) p[3] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) p[4] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) p += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) p[-3] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) p[-2] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) p[-1] = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) } while (p != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * kvm_pgd_alloc() - Allocate and initialise a KVM GPA page directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Allocate a blank KVM GPA page directory (PGD) for representing guest physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * to host physical page mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * Returns: Pointer to new KVM GPA page directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * NULL on allocation failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pgd_t *kvm_pgd_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pgd_t *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = (pgd_t *)__get_free_pages(GFP_KERNEL, PGD_ORDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) kvm_pgd_init(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * kvm_mips_walk_pgd() - Walk page table with optional allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * @pgd: Page directory pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @addr: Address to index page table using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @cache: MMU page cache to allocate new page tables from, or NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Walk the page tables pointed to by @pgd to find the PTE corresponding to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * address @addr. If page tables don't exist for @addr, they will be created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * from the MMU cache if @cache is not NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Returns: Pointer to pte_t corresponding to @addr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * NULL if a page table doesn't exist for @addr and !@cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * NULL if a page table allocation failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static pte_t *kvm_mips_walk_pgd(pgd_t *pgd, struct kvm_mmu_memory_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pgd += pgd_index(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (pgd_none(*pgd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Not used on MIPS yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) p4d = p4d_offset(pgd, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pud = pud_offset(p4d, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (pud_none(*pud)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) pmd_t *new_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (!cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) new_pmd = kvm_mmu_memory_cache_alloc(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pmd_init((unsigned long)new_pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) (unsigned long)invalid_pte_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pud_populate(NULL, pud, new_pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) pmd = pmd_offset(pud, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (pmd_none(*pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pte_t *new_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) new_pte = kvm_mmu_memory_cache_alloc(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) clear_page(new_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pmd_populate_kernel(NULL, pmd, new_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return pte_offset_kernel(pmd, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Caller must hold kvm->mm_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static pte_t *kvm_mips_pte_for_gpa(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct kvm_mmu_memory_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return kvm_mips_walk_pgd(kvm->arch.gpa_mm.pgd, cache, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * kvm_mips_flush_gpa_{pte,pmd,pud,pgd,pt}.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Flush a range of guest physical address space from the VM's GPA page tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static bool kvm_mips_flush_gpa_pte(pte_t *pte, unsigned long start_gpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned long end_gpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int i_min = pte_index(start_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int i_max = pte_index(end_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) for (i = i_min; i <= i_max; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!pte_present(pte[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) set_pte(pte + i, __pte(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static bool kvm_mips_flush_gpa_pmd(pmd_t *pmd, unsigned long start_gpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned long end_gpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unsigned long end = ~0ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int i_min = pmd_index(start_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int i_max = pmd_index(end_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!pmd_present(pmd[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pte = pte_offset_kernel(pmd + i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (i == i_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) end = end_gpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (kvm_mips_flush_gpa_pte(pte, start_gpa, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) pmd_clear(pmd + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pte_free_kernel(NULL, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) safe_to_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static bool kvm_mips_flush_gpa_pud(pud_t *pud, unsigned long start_gpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned long end_gpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) unsigned long end = ~0ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int i_min = pud_index(start_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int i_max = pud_index(end_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!pud_present(pud[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pmd = pmd_offset(pud + i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (i == i_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) end = end_gpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (kvm_mips_flush_gpa_pmd(pmd, start_gpa, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) pud_clear(pud + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pmd_free(NULL, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) safe_to_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static bool kvm_mips_flush_gpa_pgd(pgd_t *pgd, unsigned long start_gpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unsigned long end_gpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unsigned long end = ~0ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int i_min = pgd_index(start_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int i_max = pgd_index(end_gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) for (i = i_min; i <= i_max; ++i, start_gpa = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!pgd_present(pgd[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) p4d = p4d_offset(pgd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pud = pud_offset(p4d + i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (i == i_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) end = end_gpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (kvm_mips_flush_gpa_pud(pud, start_gpa, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) pgd_clear(pgd + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) pud_free(NULL, pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) safe_to_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * kvm_mips_flush_gpa_pt() - Flush a range of guest physical addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @kvm: KVM pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @start_gfn: Guest frame number of first page in GPA range to flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @end_gfn: Guest frame number of last page in GPA range to flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * Flushes a range of GPA mappings from the GPA page tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * The caller must hold the @kvm->mmu_lock spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Returns: Whether its safe to remove the top level page directory because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * all lower levels have been removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return kvm_mips_flush_gpa_pgd(kvm->arch.gpa_mm.pgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) start_gfn << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) end_gfn << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #define BUILD_PTE_RANGE_OP(name, op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static int kvm_mips_##name##_pte(pte_t *pte, unsigned long start, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) unsigned long end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int ret = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int i_min = pte_index(start); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int i_max = pte_index(end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pte_t old, new; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) for (i = i_min; i <= i_max; ++i) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (!pte_present(pte[i])) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) continue; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) old = pte[i]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) new = op(old); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (pte_val(new) == pte_val(old)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) continue; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) set_pte(pte + i, new); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ret = 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* returns true if anything was done */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static int kvm_mips_##name##_pmd(pmd_t *pmd, unsigned long start, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) unsigned long end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int ret = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) pte_t *pte; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) unsigned long cur_end = ~0ul; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int i_min = pmd_index(start); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int i_max = pmd_index(end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) for (i = i_min; i <= i_max; ++i, start = 0) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!pmd_present(pmd[i])) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) continue; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) pte = pte_offset_kernel(pmd + i, 0); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (i == i_max) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) cur_end = end; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ret |= kvm_mips_##name##_pte(pte, start, cur_end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int kvm_mips_##name##_pud(pud_t *pud, unsigned long start, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) unsigned long end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int ret = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) pmd_t *pmd; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) unsigned long cur_end = ~0ul; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int i_min = pud_index(start); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) int i_max = pud_index(end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) for (i = i_min; i <= i_max; ++i, start = 0) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!pud_present(pud[i])) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) continue; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) pmd = pmd_offset(pud + i, 0); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (i == i_max) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) cur_end = end; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) ret |= kvm_mips_##name##_pmd(pmd, start, cur_end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int kvm_mips_##name##_pgd(pgd_t *pgd, unsigned long start, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) unsigned long end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int ret = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) p4d_t *p4d; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) pud_t *pud; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) unsigned long cur_end = ~0ul; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int i_min = pgd_index(start); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int i_max = pgd_index(end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) for (i = i_min; i <= i_max; ++i, start = 0) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (!pgd_present(pgd[i])) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) continue; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) p4d = p4d_offset(pgd, 0); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) pud = pud_offset(p4d + i, 0); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (i == i_max) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cur_end = end; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) ret |= kvm_mips_##name##_pud(pud, start, cur_end); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * kvm_mips_mkclean_gpa_pt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * Mark a range of guest physical address space clean (writes fault) in the VM's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * GPA page table to allow dirty page tracking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) BUILD_PTE_RANGE_OP(mkclean, pte_mkclean)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * kvm_mips_mkclean_gpa_pt() - Make a range of guest physical addresses clean.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * @kvm: KVM pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * @start_gfn: Guest frame number of first page in GPA range to flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * @end_gfn: Guest frame number of last page in GPA range to flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * Make a range of GPA mappings clean so that guest writes will fault and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * trigger dirty page logging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * The caller must hold the @kvm->mmu_lock spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * Returns: Whether any GPA mappings were modified, which would require
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * derived mappings (GVA page tables & TLB enties) to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * invalidated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return kvm_mips_mkclean_pgd(kvm->arch.gpa_mm.pgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) start_gfn << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) end_gfn << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * kvm_arch_mmu_enable_log_dirty_pt_masked() - write protect dirty pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * @kvm: The KVM pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * @slot: The memory slot associated with mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * @gfn_offset: The gfn offset in memory slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * slot to be write protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * Walks bits set in mask write protects the associated pte's. Caller must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * acquire @kvm->mmu_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct kvm_memory_slot *slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) gfn_t gfn_offset, unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) gfn_t base_gfn = slot->base_gfn + gfn_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) gfn_t start = base_gfn + __ffs(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) gfn_t end = base_gfn + __fls(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) kvm_mips_mkclean_gpa_pt(kvm, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * kvm_mips_mkold_gpa_pt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * Mark a range of guest physical address space old (all accesses fault) in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * VM's GPA page table to allow detection of commonly used pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) BUILD_PTE_RANGE_OP(mkold, pte_mkold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static int kvm_mips_mkold_gpa_pt(struct kvm *kvm, gfn_t start_gfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) gfn_t end_gfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return kvm_mips_mkold_pgd(kvm->arch.gpa_mm.pgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) start_gfn << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) end_gfn << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static int handle_hva_to_gpa(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int (*handler)(struct kvm *kvm, gfn_t gfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) gpa_t gfn_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct kvm_memory_slot *memslot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct kvm_memslots *slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct kvm_memory_slot *memslot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) slots = kvm_memslots(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* we only care about the pages that the guest sees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) kvm_for_each_memslot(memslot, slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) unsigned long hva_start, hva_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) gfn_t gfn, gfn_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) hva_start = max(start, memslot->userspace_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) hva_end = min(end, memslot->userspace_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) (memslot->npages << PAGE_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (hva_start >= hva_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * {gfn(page) | page intersects with [hva_start, hva_end)} =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * {gfn_start, gfn_start+1, ..., gfn_end-1}.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) gfn = hva_to_gfn_memslot(hva_start, memslot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ret |= handler(kvm, gfn, gfn_end, memslot, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static int kvm_unmap_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct kvm_memory_slot *memslot, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) kvm_mips_flush_gpa_pt(kvm, gfn, gfn_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) kvm_mips_callbacks->flush_shadow_all(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct kvm_memory_slot *memslot, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) gpa_t gpa = gfn << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) pte_t hva_pte = *(pte_t *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) pte_t old_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (!gpa_pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* Mapping may need adjusting depending on memslot flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) old_pte = *gpa_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES && !pte_dirty(old_pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) hva_pte = pte_mkclean(hva_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) else if (memslot->flags & KVM_MEM_READONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) hva_pte = pte_wrprotect(hva_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) set_pte(gpa_pte, hva_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /* Replacing an absent or old page doesn't need flushes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (!pte_present(old_pte) || !pte_young(old_pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /* Pages swapped, aged, moved, or cleaned require flushes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return !pte_present(hva_pte) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) !pte_young(hva_pte) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) pte_pfn(old_pte) != pte_pfn(hva_pte) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) (pte_dirty(old_pte) && !pte_dirty(hva_pte));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) unsigned long end = hva + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ret = handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) kvm_mips_callbacks->flush_shadow_all(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static int kvm_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct kvm_memory_slot *memslot, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return kvm_mips_mkold_gpa_pt(kvm, gfn, gfn_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static int kvm_test_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct kvm_memory_slot *memslot, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) gpa_t gpa = gfn << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) pte_t *gpa_pte = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!gpa_pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return pte_young(*gpa_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * _kvm_mips_map_page_fast() - Fast path GPA fault handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * @vcpu: VCPU pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * @gpa: Guest physical address of fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * @write_fault: Whether the fault was due to a write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * @out_entry: New PTE for @gpa (written on success unless NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * @out_buddy: New PTE for @gpa's buddy (written on success unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * Perform fast path GPA fault handling, doing all that can be done without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * calling into KVM. This handles marking old pages young (for idle page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * tracking), and dirtying of clean pages (for dirty page logging).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * Returns: 0 on success, in which case we can update derived mappings and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * resume guest execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * -EFAULT on failure due to absent GPA mapping or write to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * read-only page, in which case KVM must be consulted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) bool write_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) pte_t *out_entry, pte_t *out_buddy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) gfn_t gfn = gpa >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) kvm_pfn_t pfn = 0; /* silence bogus GCC warning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) bool pfn_valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) spin_lock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* Fast path - just check GPA page table for an existing entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) ptep = kvm_mips_pte_for_gpa(kvm, NULL, gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (!ptep || !pte_present(*ptep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) /* Track access to pages marked old */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (!pte_young(*ptep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) set_pte(ptep, pte_mkyoung(*ptep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) pfn = pte_pfn(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) pfn_valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* call kvm_set_pfn_accessed() after unlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (write_fault && !pte_dirty(*ptep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (!pte_write(*ptep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) /* Track dirtying of writeable pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) set_pte(ptep, pte_mkdirty(*ptep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) pfn = pte_pfn(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) mark_page_dirty(kvm, gfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) kvm_set_pfn_dirty(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (out_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) *out_entry = *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (out_buddy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) *out_buddy = *ptep_buddy(ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) spin_unlock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (pfn_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) kvm_set_pfn_accessed(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * kvm_mips_map_page() - Map a guest physical page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * @vcpu: VCPU pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * @gpa: Guest physical address of fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * @write_fault: Whether the fault was due to a write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * @out_entry: New PTE for @gpa (written on success unless NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * @out_buddy: New PTE for @gpa's buddy (written on success unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * Handle GPA faults by creating a new GPA mapping (or updating an existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * one).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * This takes care of marking pages young or dirty (idle/dirty page tracking),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * asking KVM for the corresponding PFN, and creating a mapping in the GPA page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * tables. Derived mappings (GVA page tables and TLBs) must be handled by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * Returns: 0 on success, in which case the caller may use the @out_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * and @out_buddy PTEs to update derived mappings and resume guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * -EFAULT if there is no memory region at @gpa or a write was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * attempted to a read-only memory region. This is usually handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * as an MMIO access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) bool write_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) pte_t *out_entry, pte_t *out_buddy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) gfn_t gfn = gpa >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) int srcu_idx, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) kvm_pfn_t pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) pte_t *ptep, entry, old_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) bool writeable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) unsigned long prot_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) unsigned long mmu_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /* Try the fast path to handle old / clean pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) srcu_idx = srcu_read_lock(&kvm->srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) err = _kvm_mips_map_page_fast(vcpu, gpa, write_fault, out_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) out_buddy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) /* We need a minimum of cached pages ready for page table creation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) err = kvm_mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * Used to check for invalidations in progress, of the pfn that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * returned by pfn_to_pfn_prot below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) mmu_seq = kvm->mmu_notifier_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * Ensure the read of mmu_notifier_seq isn't reordered with PTE reads in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * gfn_to_pfn_prot() (which calls get_user_pages()), so that we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * risk the page we get a reference to getting unmapped before we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) * chance to grab the mmu_lock without mmu_notifier_retry() noticing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * This smp_rmb() pairs with the effective smp_wmb() of the combination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * of the pte_unmap_unlock() after the PTE is zapped, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * spin_lock() in kvm_mmu_notifier_invalidate_<page|range_end>() before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * mmu_notifier_seq is incremented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /* Slow path - ask KVM core whether we can access this GPA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writeable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (is_error_noslot_pfn(pfn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) spin_lock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) /* Check if an invalidation has taken place since we got pfn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (mmu_notifier_retry(kvm, mmu_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * This can happen when mappings are changed asynchronously, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * also synchronously if a COW is triggered by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * gfn_to_pfn_prot().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) spin_unlock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) kvm_release_pfn_clean(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) /* Ensure page tables are allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) ptep = kvm_mips_pte_for_gpa(kvm, memcache, gpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) /* Set up the PTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) prot_bits = _PAGE_PRESENT | __READABLE | _page_cachable_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (writeable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) prot_bits |= _PAGE_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (write_fault) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) prot_bits |= __WRITEABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) mark_page_dirty(kvm, gfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) kvm_set_pfn_dirty(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) entry = pfn_pte(pfn, __pgprot(prot_bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) /* Write the PTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) old_pte = *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) set_pte(ptep, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (out_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) *out_entry = *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (out_buddy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) *out_buddy = *ptep_buddy(ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) spin_unlock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) kvm_release_pfn_clean(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) kvm_set_pfn_accessed(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) srcu_read_unlock(&kvm->srcu, srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) static pte_t *kvm_trap_emul_pte_for_gva(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) pgd_t *pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /* We need a minimum of cached pages ready for page table creation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) ret = kvm_mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (KVM_GUEST_KERNEL_MODE(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) pgdp = vcpu->arch.guest_kernel_mm.pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) pgdp = vcpu->arch.guest_user_mm.pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) return kvm_mips_walk_pgd(pgdp, memcache, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) void kvm_trap_emul_invalidate_gva(struct kvm_vcpu *vcpu, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) bool user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) pgd_t *pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) addr &= PAGE_MASK << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) pgdp = vcpu->arch.guest_kernel_mm.pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) ptep = kvm_mips_walk_pgd(pgdp, NULL, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (ptep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) ptep[0] = pfn_pte(0, __pgprot(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) ptep[1] = pfn_pte(0, __pgprot(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (user) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) pgdp = vcpu->arch.guest_user_mm.pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) ptep = kvm_mips_walk_pgd(pgdp, NULL, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (ptep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) ptep[0] = pfn_pte(0, __pgprot(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) ptep[1] = pfn_pte(0, __pgprot(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * kvm_mips_flush_gva_{pte,pmd,pud,pgd,pt}.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * Flush a range of guest physical address space from the VM's GPA page tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static bool kvm_mips_flush_gva_pte(pte_t *pte, unsigned long start_gva,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) unsigned long end_gva)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) int i_min = pte_index(start_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) int i_max = pte_index(end_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PTE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * There's no freeing to do, so there's no point clearing individual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * entries unless only part of the last level page table needs flushing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) if (safe_to_remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) for (i = i_min; i <= i_max; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (!pte_present(pte[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) set_pte(pte + i, __pte(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) static bool kvm_mips_flush_gva_pmd(pmd_t *pmd, unsigned long start_gva,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) unsigned long end_gva)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) unsigned long end = ~0ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) int i_min = pmd_index(start_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) int i_max = pmd_index(end_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PMD - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) for (i = i_min; i <= i_max; ++i, start_gva = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (!pmd_present(pmd[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) pte = pte_offset_kernel(pmd + i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (i == i_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) end = end_gva;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (kvm_mips_flush_gva_pte(pte, start_gva, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) pmd_clear(pmd + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) pte_free_kernel(NULL, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) safe_to_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) static bool kvm_mips_flush_gva_pud(pud_t *pud, unsigned long start_gva,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) unsigned long end_gva)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) unsigned long end = ~0ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) int i_min = pud_index(start_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) int i_max = pud_index(end_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PUD - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) for (i = i_min; i <= i_max; ++i, start_gva = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (!pud_present(pud[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) pmd = pmd_offset(pud + i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (i == i_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) end = end_gva;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (kvm_mips_flush_gva_pmd(pmd, start_gva, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) pud_clear(pud + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) pmd_free(NULL, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) safe_to_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) static bool kvm_mips_flush_gva_pgd(pgd_t *pgd, unsigned long start_gva,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) unsigned long end_gva)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) unsigned long end = ~0ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) int i_min = pgd_index(start_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) int i_max = pgd_index(end_gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) bool safe_to_remove = (i_min == 0 && i_max == PTRS_PER_PGD - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) for (i = i_min; i <= i_max; ++i, start_gva = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (!pgd_present(pgd[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) p4d = p4d_offset(pgd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) pud = pud_offset(p4d + i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (i == i_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) end = end_gva;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (kvm_mips_flush_gva_pud(pud, start_gva, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) pgd_clear(pgd + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) pud_free(NULL, pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) safe_to_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) return safe_to_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) void kvm_mips_flush_gva_pt(pgd_t *pgd, enum kvm_mips_flush flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (flags & KMF_GPA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) /* all of guest virtual address space could be affected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (flags & KMF_KERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) /* useg, kseg0, seg2/3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) kvm_mips_flush_gva_pgd(pgd, 0, 0x7fffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /* useg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) kvm_mips_flush_gva_pgd(pgd, 0, 0x3fffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) /* useg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) kvm_mips_flush_gva_pgd(pgd, 0, 0x3fffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) /* kseg2/3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (flags & KMF_KERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) kvm_mips_flush_gva_pgd(pgd, 0x60000000, 0x7fffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) static pte_t kvm_mips_gpa_pte_to_gva_unmapped(pte_t pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * Don't leak writeable but clean entries from GPA page tables. We don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * want the normal Linux tlbmod handler to handle dirtying when KVM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) * accesses guest memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (!pte_dirty(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) pte = pte_wrprotect(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) return pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) static pte_t kvm_mips_gpa_pte_to_gva_mapped(pte_t pte, long entrylo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) /* Guest EntryLo overrides host EntryLo */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) if (!(entrylo & ENTRYLO_D))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) pte = pte_mkclean(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) return kvm_mips_gpa_pte_to_gva_unmapped(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) #ifdef CONFIG_KVM_MIPS_VZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) bool write_fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) ret = kvm_mips_map_page(vcpu, badvaddr, write_fault, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) /* Invalidate this entry in the TLB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) return kvm_vz_host_tlb_inv(vcpu, badvaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) /* XXXKYMA: Must be called with interrupts disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) bool write_fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) unsigned long gpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) pte_t pte_gpa[2], *ptep_gva;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) kvm_mips_dump_host_tlbs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) /* Get the GPA page table entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) gpa = KVM_GUEST_CPHYSADDR(badvaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) idx = (badvaddr >> PAGE_SHIFT) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (kvm_mips_map_page(vcpu, gpa, write_fault, &pte_gpa[idx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) &pte_gpa[!idx]) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) /* Get the GVA page table entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, badvaddr & ~PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (!ptep_gva) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) kvm_err("No ptep for gva %lx\n", badvaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) /* Copy a pair of entries from GPA page table to GVA page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) ptep_gva[0] = kvm_mips_gpa_pte_to_gva_unmapped(pte_gpa[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) ptep_gva[1] = kvm_mips_gpa_pte_to_gva_unmapped(pte_gpa[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) /* Invalidate this entry in the TLB, guest kernel ASID only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) kvm_mips_host_tlb_inv(vcpu, badvaddr, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) struct kvm_mips_tlb *tlb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) unsigned long gva,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) bool write_fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) long tlb_lo[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) pte_t pte_gpa[2], *ptep_buddy, *ptep_gva;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) unsigned int idx = TLB_LO_IDX(*tlb, gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) bool kernel = KVM_GUEST_KERNEL_MODE(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) tlb_lo[0] = tlb->tlb_lo[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) tlb_lo[1] = tlb->tlb_lo[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) * The commpage address must not be mapped to anything else if the guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) * TLB contains entries nearby, or commpage accesses will break.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (!((gva ^ KVM_GUEST_COMMPAGE_ADDR) & VPN2_MASK & (PAGE_MASK << 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) tlb_lo[TLB_LO_IDX(*tlb, KVM_GUEST_COMMPAGE_ADDR)] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) /* Get the GPA page table entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (kvm_mips_map_page(vcpu, mips3_tlbpfn_to_paddr(tlb_lo[idx]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) write_fault, &pte_gpa[idx], NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) /* And its GVA buddy's GPA page table entry if it also exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) pte_gpa[!idx] = pfn_pte(0, __pgprot(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (tlb_lo[!idx] & ENTRYLO_V) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) spin_lock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) ptep_buddy = kvm_mips_pte_for_gpa(kvm, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) mips3_tlbpfn_to_paddr(tlb_lo[!idx]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (ptep_buddy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) pte_gpa[!idx] = *ptep_buddy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) spin_unlock(&kvm->mmu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) /* Get the GVA page table entry pair */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, gva & ~PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (!ptep_gva) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) kvm_err("No ptep for gva %lx\n", gva);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) /* Copy a pair of entries from GPA page table to GVA page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) ptep_gva[0] = kvm_mips_gpa_pte_to_gva_mapped(pte_gpa[0], tlb_lo[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) ptep_gva[1] = kvm_mips_gpa_pte_to_gva_mapped(pte_gpa[1], tlb_lo[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) /* Invalidate this entry in the TLB, current guest mode ASID only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) kvm_mips_host_tlb_inv(vcpu, gva, !kernel, kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) tlb->tlb_lo[0], tlb->tlb_lo[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) kvm_pfn_t pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) ptep = kvm_trap_emul_pte_for_gva(vcpu, badvaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) if (!ptep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) kvm_err("No ptep for commpage %lx\n", badvaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) pfn = PFN_DOWN(virt_to_phys(vcpu->arch.kseg0_commpage));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /* Also set valid and dirty, so refill handler doesn't have to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) *ptep = pte_mkyoung(pte_mkdirty(pfn_pte(pfn, PAGE_SHARED)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) /* Invalidate this entry in the TLB, guest kernel ASID only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) kvm_mips_host_tlb_inv(vcpu, badvaddr, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * kvm_mips_migrate_count() - Migrate timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * @vcpu: Virtual CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * if it was running prior to being cancelled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) * Must be called when the VCPU is migrated to a different CPU to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) * timer expiry during guest execution interrupts the guest and causes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) * interrupt to be delivered in a timely manner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) hrtimer_restart(&vcpu->arch.comparecount_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) /* Restore ASID once we are scheduled back after preemption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) vcpu->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (vcpu->arch.last_sched_cpu != cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * Migrate the timer interrupt to the current CPU so that it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * always interrupts the guest and synchronously triggers a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * guest timer interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) kvm_mips_migrate_count(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) /* restore guest state to registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) kvm_mips_callbacks->vcpu_load(vcpu, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) /* ASID can change if another task is scheduled during preemption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) vcpu->arch.last_sched_cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) vcpu->cpu = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /* save guest state in registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) kvm_mips_callbacks->vcpu_put(vcpu, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) * kvm_trap_emul_gva_fault() - Safely attempt to handle a GVA access fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) * @vcpu: Virtual CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) * @gva: Guest virtual address to be accessed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) * @write: True if write attempted (must be dirtied and made writable).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * Safely attempt to handle a GVA fault, mapping GVA pages if necessary, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * dirtying the page if @write so that guest instructions can be modified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * Returns: KVM_MIPS_MAPPED on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * KVM_MIPS_GVA if bad guest virtual address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * KVM_MIPS_GPA if bad guest physical address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * KVM_MIPS_TLB if guest TLB not present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * KVM_MIPS_TLBINV if guest TLB present but not valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * KVM_MIPS_TLBMOD if guest TLB read only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) enum kvm_mips_fault_result kvm_trap_emul_gva_fault(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) unsigned long gva,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) bool write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) struct mips_coproc *cop0 = vcpu->arch.cop0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) struct kvm_mips_tlb *tlb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (KVM_GUEST_KSEGX(gva) == KVM_GUEST_KSEG0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (kvm_mips_handle_kseg0_tlb_fault(gva, vcpu, write) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) return KVM_MIPS_GPA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) } else if ((KVM_GUEST_KSEGX(gva) < KVM_GUEST_KSEG0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) KVM_GUEST_KSEGX(gva) == KVM_GUEST_KSEG23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) /* Address should be in the guest TLB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) index = kvm_mips_guest_tlb_lookup(vcpu, (gva & VPN2_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) return KVM_MIPS_TLB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) tlb = &vcpu->arch.guest_tlb[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) /* Entry should be valid, and dirty for writes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) if (!TLB_IS_VALID(*tlb, gva))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) return KVM_MIPS_TLBINV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (write && !TLB_IS_DIRTY(*tlb, gva))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) return KVM_MIPS_TLBMOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, gva, write))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) return KVM_MIPS_GPA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) return KVM_MIPS_GVA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return KVM_MIPS_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) int kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) if (WARN(IS_ENABLED(CONFIG_KVM_MIPS_VZ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) "Expect BadInstr/BadInstrP registers to be used with VZ\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) kvm_trap_emul_gva_lockless_begin(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) err = get_user(*out, opc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) kvm_trap_emul_gva_lockless_end(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) * Try to handle the fault, maybe we just raced with a GVA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) * invalidation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) err = kvm_trap_emul_gva_fault(vcpu, (unsigned long)opc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) kvm_err("%s: illegal address: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) __func__, opc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) /* Hopefully it'll work now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }