1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
7 #include <linux/mman.h>
8 #include <linux/kvm_host.h>
10 #include <linux/hugetlb.h>
11 #include <linux/sched/signal.h>
12 #include <trace/events/kvm.h>
13 #include <asm/pgalloc.h>
14 #include <asm/cacheflush.h>
15 #include <asm/kvm_arm.h>
16 #include <asm/kvm_mmu.h>
17 #include <asm/kvm_mmio.h>
18 #include <asm/kvm_ras.h>
19 #include <asm/kvm_asm.h>
20 #include <asm/kvm_emulate.h>
25 static pgd_t *boot_hyp_pgd;
26 static pgd_t *hyp_pgd;
27 static pgd_t *merged_hyp_pgd;
28 static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
30 static unsigned long hyp_idmap_start;
31 static unsigned long hyp_idmap_end;
32 static phys_addr_t hyp_idmap_vector;
34 static unsigned long io_map_base;
36 #define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
38 #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
39 #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
41 static bool memslot_is_logging(struct kvm_memory_slot *memslot)
43 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
47 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
48 * @kvm: pointer to kvm structure.
50 * Interface to HYP function to flush all VM TLB entries
52 void kvm_flush_remote_tlbs(struct kvm *kvm)
54 kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
57 static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
59 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
63 * D-Cache management functions. They take the page table entries by
64 * value, as they are flushing the cache using the kernel mapping (or
67 static void kvm_flush_dcache_pte(pte_t pte)
69 __kvm_flush_dcache_pte(pte);
72 static void kvm_flush_dcache_pmd(pmd_t pmd)
74 __kvm_flush_dcache_pmd(pmd);
77 static void kvm_flush_dcache_pud(pud_t pud)
79 __kvm_flush_dcache_pud(pud);
82 static bool kvm_is_device_pfn(unsigned long pfn)
84 return !pfn_valid(pfn);
88 * stage2_dissolve_pmd() - clear and flush huge PMD entry
89 * @kvm: pointer to kvm structure.
91 * @pmd: pmd pointer for IPA
93 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs.
95 static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
97 if (!pmd_thp_or_huge(*pmd))
101 kvm_tlb_flush_vmid_ipa(kvm, addr);
102 put_page(virt_to_page(pmd));
106 * stage2_dissolve_pud() - clear and flush huge PUD entry
107 * @kvm: pointer to kvm structure.
109 * @pud: pud pointer for IPA
111 * Function clears a PUD entry, flushes addr 1st and 2nd stage TLBs.
113 static void stage2_dissolve_pud(struct kvm *kvm, phys_addr_t addr, pud_t *pudp)
115 if (!stage2_pud_huge(kvm, *pudp))
118 stage2_pud_clear(kvm, pudp);
119 kvm_tlb_flush_vmid_ipa(kvm, addr);
120 put_page(virt_to_page(pudp));
123 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
128 BUG_ON(max > KVM_NR_MEM_OBJS);
129 if (cache->nobjs >= min)
131 while (cache->nobjs < max) {
132 page = (void *)__get_free_page(GFP_PGTABLE_USER);
135 cache->objects[cache->nobjs++] = page;
140 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
143 free_page((unsigned long)mc->objects[--mc->nobjs]);
146 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
150 BUG_ON(!mc || !mc->nobjs);
151 p = mc->objects[--mc->nobjs];
155 static void clear_stage2_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
157 pud_t *pud_table __maybe_unused = stage2_pud_offset(kvm, pgd, 0UL);
158 stage2_pgd_clear(kvm, pgd);
159 kvm_tlb_flush_vmid_ipa(kvm, addr);
160 stage2_pud_free(kvm, pud_table);
161 put_page(virt_to_page(pgd));
164 static void clear_stage2_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
166 pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(kvm, pud, 0);
167 VM_BUG_ON(stage2_pud_huge(kvm, *pud));
168 stage2_pud_clear(kvm, pud);
169 kvm_tlb_flush_vmid_ipa(kvm, addr);
170 stage2_pmd_free(kvm, pmd_table);
171 put_page(virt_to_page(pud));
174 static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
176 pte_t *pte_table = pte_offset_kernel(pmd, 0);
177 VM_BUG_ON(pmd_thp_or_huge(*pmd));
179 kvm_tlb_flush_vmid_ipa(kvm, addr);
180 free_page((unsigned long)pte_table);
181 put_page(virt_to_page(pmd));
184 static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte)
186 WRITE_ONCE(*ptep, new_pte);
190 static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd)
192 WRITE_ONCE(*pmdp, new_pmd);
196 static inline void kvm_pmd_populate(pmd_t *pmdp, pte_t *ptep)
198 kvm_set_pmd(pmdp, kvm_mk_pmd(ptep));
201 static inline void kvm_pud_populate(pud_t *pudp, pmd_t *pmdp)
203 WRITE_ONCE(*pudp, kvm_mk_pud(pmdp));
207 static inline void kvm_pgd_populate(pgd_t *pgdp, pud_t *pudp)
209 WRITE_ONCE(*pgdp, kvm_mk_pgd(pudp));
214 * Unmapping vs dcache management:
216 * If a guest maps certain memory pages as uncached, all writes will
217 * bypass the data cache and go directly to RAM. However, the CPUs
218 * can still speculate reads (not writes) and fill cache lines with
221 * Those cache lines will be *clean* cache lines though, so a
222 * clean+invalidate operation is equivalent to an invalidate
223 * operation, because no cache lines are marked dirty.
225 * Those clean cache lines could be filled prior to an uncached write
226 * by the guest, and the cache coherent IO subsystem would therefore
227 * end up writing old data to disk.
229 * This is why right after unmapping a page/section and invalidating
230 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
231 * the IO subsystem will never hit in the cache.
233 * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
234 * we then fully enforce cacheability of RAM, no matter what the guest
237 static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd,
238 phys_addr_t addr, phys_addr_t end)
240 phys_addr_t start_addr = addr;
241 pte_t *pte, *start_pte;
243 start_pte = pte = pte_offset_kernel(pmd, addr);
245 if (!pte_none(*pte)) {
246 pte_t old_pte = *pte;
248 kvm_set_pte(pte, __pte(0));
249 kvm_tlb_flush_vmid_ipa(kvm, addr);
251 /* No need to invalidate the cache for device mappings */
252 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
253 kvm_flush_dcache_pte(old_pte);
255 put_page(virt_to_page(pte));
257 } while (pte++, addr += PAGE_SIZE, addr != end);
259 if (stage2_pte_table_empty(kvm, start_pte))
260 clear_stage2_pmd_entry(kvm, pmd, start_addr);
263 static void unmap_stage2_pmds(struct kvm *kvm, pud_t *pud,
264 phys_addr_t addr, phys_addr_t end)
266 phys_addr_t next, start_addr = addr;
267 pmd_t *pmd, *start_pmd;
269 start_pmd = pmd = stage2_pmd_offset(kvm, pud, addr);
271 next = stage2_pmd_addr_end(kvm, addr, end);
272 if (!pmd_none(*pmd)) {
273 if (pmd_thp_or_huge(*pmd)) {
274 pmd_t old_pmd = *pmd;
277 kvm_tlb_flush_vmid_ipa(kvm, addr);
279 kvm_flush_dcache_pmd(old_pmd);
281 put_page(virt_to_page(pmd));
283 unmap_stage2_ptes(kvm, pmd, addr, next);
286 } while (pmd++, addr = next, addr != end);
288 if (stage2_pmd_table_empty(kvm, start_pmd))
289 clear_stage2_pud_entry(kvm, pud, start_addr);
292 static void unmap_stage2_puds(struct kvm *kvm, pgd_t *pgd,
293 phys_addr_t addr, phys_addr_t end)
295 phys_addr_t next, start_addr = addr;
296 pud_t *pud, *start_pud;
298 start_pud = pud = stage2_pud_offset(kvm, pgd, addr);
300 next = stage2_pud_addr_end(kvm, addr, end);
301 if (!stage2_pud_none(kvm, *pud)) {
302 if (stage2_pud_huge(kvm, *pud)) {
303 pud_t old_pud = *pud;
305 stage2_pud_clear(kvm, pud);
306 kvm_tlb_flush_vmid_ipa(kvm, addr);
307 kvm_flush_dcache_pud(old_pud);
308 put_page(virt_to_page(pud));
310 unmap_stage2_pmds(kvm, pud, addr, next);
313 } while (pud++, addr = next, addr != end);
315 if (stage2_pud_table_empty(kvm, start_pud))
316 clear_stage2_pgd_entry(kvm, pgd, start_addr);
320 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
321 * @kvm: The VM pointer
322 * @start: The intermediate physical base address of the range to unmap
323 * @size: The size of the area to unmap
325 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
326 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
327 * destroying the VM), otherwise another faulting VCPU may come in and mess
328 * with things behind our backs.
330 static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
333 phys_addr_t addr = start, end = start + size;
336 assert_spin_locked(&kvm->mmu_lock);
337 WARN_ON(size & ~PAGE_MASK);
339 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
342 * Make sure the page table is still active, as another thread
343 * could have possibly freed the page table, while we released
346 if (!READ_ONCE(kvm->arch.pgd))
348 next = stage2_pgd_addr_end(kvm, addr, end);
349 if (!stage2_pgd_none(kvm, *pgd))
350 unmap_stage2_puds(kvm, pgd, addr, next);
352 * If the range is too large, release the kvm->mmu_lock
353 * to prevent starvation and lockup detector warnings.
356 cond_resched_lock(&kvm->mmu_lock);
357 } while (pgd++, addr = next, addr != end);
360 static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
361 phys_addr_t addr, phys_addr_t end)
365 pte = pte_offset_kernel(pmd, addr);
367 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
368 kvm_flush_dcache_pte(*pte);
369 } while (pte++, addr += PAGE_SIZE, addr != end);
372 static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
373 phys_addr_t addr, phys_addr_t end)
378 pmd = stage2_pmd_offset(kvm, pud, addr);
380 next = stage2_pmd_addr_end(kvm, addr, end);
381 if (!pmd_none(*pmd)) {
382 if (pmd_thp_or_huge(*pmd))
383 kvm_flush_dcache_pmd(*pmd);
385 stage2_flush_ptes(kvm, pmd, addr, next);
387 } while (pmd++, addr = next, addr != end);
390 static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
391 phys_addr_t addr, phys_addr_t end)
396 pud = stage2_pud_offset(kvm, pgd, addr);
398 next = stage2_pud_addr_end(kvm, addr, end);
399 if (!stage2_pud_none(kvm, *pud)) {
400 if (stage2_pud_huge(kvm, *pud))
401 kvm_flush_dcache_pud(*pud);
403 stage2_flush_pmds(kvm, pud, addr, next);
405 } while (pud++, addr = next, addr != end);
408 static void stage2_flush_memslot(struct kvm *kvm,
409 struct kvm_memory_slot *memslot)
411 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
412 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
416 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
418 next = stage2_pgd_addr_end(kvm, addr, end);
419 if (!stage2_pgd_none(kvm, *pgd))
420 stage2_flush_puds(kvm, pgd, addr, next);
421 } while (pgd++, addr = next, addr != end);
425 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
426 * @kvm: The struct kvm pointer
428 * Go through the stage 2 page tables and invalidate any cache lines
429 * backing memory already mapped to the VM.
431 static void stage2_flush_vm(struct kvm *kvm)
433 struct kvm_memslots *slots;
434 struct kvm_memory_slot *memslot;
437 idx = srcu_read_lock(&kvm->srcu);
438 spin_lock(&kvm->mmu_lock);
440 slots = kvm_memslots(kvm);
441 kvm_for_each_memslot(memslot, slots)
442 stage2_flush_memslot(kvm, memslot);
444 spin_unlock(&kvm->mmu_lock);
445 srcu_read_unlock(&kvm->srcu, idx);
448 static void clear_hyp_pgd_entry(pgd_t *pgd)
450 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0UL);
452 pud_free(NULL, pud_table);
453 put_page(virt_to_page(pgd));
456 static void clear_hyp_pud_entry(pud_t *pud)
458 pmd_t *pmd_table __maybe_unused = pmd_offset(pud, 0);
459 VM_BUG_ON(pud_huge(*pud));
461 pmd_free(NULL, pmd_table);
462 put_page(virt_to_page(pud));
465 static void clear_hyp_pmd_entry(pmd_t *pmd)
467 pte_t *pte_table = pte_offset_kernel(pmd, 0);
468 VM_BUG_ON(pmd_thp_or_huge(*pmd));
470 pte_free_kernel(NULL, pte_table);
471 put_page(virt_to_page(pmd));
474 static void unmap_hyp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
476 pte_t *pte, *start_pte;
478 start_pte = pte = pte_offset_kernel(pmd, addr);
480 if (!pte_none(*pte)) {
481 kvm_set_pte(pte, __pte(0));
482 put_page(virt_to_page(pte));
484 } while (pte++, addr += PAGE_SIZE, addr != end);
486 if (hyp_pte_table_empty(start_pte))
487 clear_hyp_pmd_entry(pmd);
490 static void unmap_hyp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
493 pmd_t *pmd, *start_pmd;
495 start_pmd = pmd = pmd_offset(pud, addr);
497 next = pmd_addr_end(addr, end);
498 /* Hyp doesn't use huge pmds */
500 unmap_hyp_ptes(pmd, addr, next);
501 } while (pmd++, addr = next, addr != end);
503 if (hyp_pmd_table_empty(start_pmd))
504 clear_hyp_pud_entry(pud);
507 static void unmap_hyp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
510 pud_t *pud, *start_pud;
512 start_pud = pud = pud_offset(pgd, addr);
514 next = pud_addr_end(addr, end);
515 /* Hyp doesn't use huge puds */
517 unmap_hyp_pmds(pud, addr, next);
518 } while (pud++, addr = next, addr != end);
520 if (hyp_pud_table_empty(start_pud))
521 clear_hyp_pgd_entry(pgd);
524 static unsigned int kvm_pgd_index(unsigned long addr, unsigned int ptrs_per_pgd)
526 return (addr >> PGDIR_SHIFT) & (ptrs_per_pgd - 1);
529 static void __unmap_hyp_range(pgd_t *pgdp, unsigned long ptrs_per_pgd,
530 phys_addr_t start, u64 size)
533 phys_addr_t addr = start, end = start + size;
537 * We don't unmap anything from HYP, except at the hyp tear down.
538 * Hence, we don't have to invalidate the TLBs here.
540 pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
542 next = pgd_addr_end(addr, end);
544 unmap_hyp_puds(pgd, addr, next);
545 } while (pgd++, addr = next, addr != end);
548 static void unmap_hyp_range(pgd_t *pgdp, phys_addr_t start, u64 size)
550 __unmap_hyp_range(pgdp, PTRS_PER_PGD, start, size);
553 static void unmap_hyp_idmap_range(pgd_t *pgdp, phys_addr_t start, u64 size)
555 __unmap_hyp_range(pgdp, __kvm_idmap_ptrs_per_pgd(), start, size);
559 * free_hyp_pgds - free Hyp-mode page tables
561 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
562 * therefore contains either mappings in the kernel memory area (above
563 * PAGE_OFFSET), or device mappings in the idmap range.
565 * boot_hyp_pgd should only map the idmap range, and is only used in
566 * the extended idmap case.
568 void free_hyp_pgds(void)
572 mutex_lock(&kvm_hyp_pgd_mutex);
574 id_pgd = boot_hyp_pgd ? boot_hyp_pgd : hyp_pgd;
577 /* In case we never called hyp_mmu_init() */
579 io_map_base = hyp_idmap_start;
580 unmap_hyp_idmap_range(id_pgd, io_map_base,
581 hyp_idmap_start + PAGE_SIZE - io_map_base);
585 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
590 unmap_hyp_range(hyp_pgd, kern_hyp_va(PAGE_OFFSET),
591 (uintptr_t)high_memory - PAGE_OFFSET);
593 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
596 if (merged_hyp_pgd) {
597 clear_page(merged_hyp_pgd);
598 free_page((unsigned long)merged_hyp_pgd);
599 merged_hyp_pgd = NULL;
602 mutex_unlock(&kvm_hyp_pgd_mutex);
605 static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
606 unsigned long end, unsigned long pfn,
614 pte = pte_offset_kernel(pmd, addr);
615 kvm_set_pte(pte, kvm_pfn_pte(pfn, prot));
616 get_page(virt_to_page(pte));
618 } while (addr += PAGE_SIZE, addr != end);
621 static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
622 unsigned long end, unsigned long pfn,
627 unsigned long addr, next;
631 pmd = pmd_offset(pud, addr);
633 BUG_ON(pmd_sect(*pmd));
635 if (pmd_none(*pmd)) {
636 pte = pte_alloc_one_kernel(NULL);
638 kvm_err("Cannot allocate Hyp pte\n");
641 kvm_pmd_populate(pmd, pte);
642 get_page(virt_to_page(pmd));
645 next = pmd_addr_end(addr, end);
647 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
648 pfn += (next - addr) >> PAGE_SHIFT;
649 } while (addr = next, addr != end);
654 static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
655 unsigned long end, unsigned long pfn,
660 unsigned long addr, next;
665 pud = pud_offset(pgd, addr);
667 if (pud_none_or_clear_bad(pud)) {
668 pmd = pmd_alloc_one(NULL, addr);
670 kvm_err("Cannot allocate Hyp pmd\n");
673 kvm_pud_populate(pud, pmd);
674 get_page(virt_to_page(pud));
677 next = pud_addr_end(addr, end);
678 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
681 pfn += (next - addr) >> PAGE_SHIFT;
682 } while (addr = next, addr != end);
687 static int __create_hyp_mappings(pgd_t *pgdp, unsigned long ptrs_per_pgd,
688 unsigned long start, unsigned long end,
689 unsigned long pfn, pgprot_t prot)
693 unsigned long addr, next;
696 mutex_lock(&kvm_hyp_pgd_mutex);
697 addr = start & PAGE_MASK;
698 end = PAGE_ALIGN(end);
700 pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
702 if (pgd_none(*pgd)) {
703 pud = pud_alloc_one(NULL, addr);
705 kvm_err("Cannot allocate Hyp pud\n");
709 kvm_pgd_populate(pgd, pud);
710 get_page(virt_to_page(pgd));
713 next = pgd_addr_end(addr, end);
714 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
717 pfn += (next - addr) >> PAGE_SHIFT;
718 } while (addr = next, addr != end);
720 mutex_unlock(&kvm_hyp_pgd_mutex);
724 static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
726 if (!is_vmalloc_addr(kaddr)) {
727 BUG_ON(!virt_addr_valid(kaddr));
730 return page_to_phys(vmalloc_to_page(kaddr)) +
731 offset_in_page(kaddr);
736 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
737 * @from: The virtual kernel start address of the range
738 * @to: The virtual kernel end address of the range (exclusive)
739 * @prot: The protection to be applied to this range
741 * The same virtual address as the kernel virtual address is also used
742 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
745 int create_hyp_mappings(void *from, void *to, pgprot_t prot)
747 phys_addr_t phys_addr;
748 unsigned long virt_addr;
749 unsigned long start = kern_hyp_va((unsigned long)from);
750 unsigned long end = kern_hyp_va((unsigned long)to);
752 if (is_kernel_in_hyp_mode())
755 start = start & PAGE_MASK;
756 end = PAGE_ALIGN(end);
758 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
761 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
762 err = __create_hyp_mappings(hyp_pgd, PTRS_PER_PGD,
763 virt_addr, virt_addr + PAGE_SIZE,
764 __phys_to_pfn(phys_addr),
773 static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
774 unsigned long *haddr, pgprot_t prot)
776 pgd_t *pgd = hyp_pgd;
780 mutex_lock(&kvm_hyp_pgd_mutex);
783 * This assumes that we we have enough space below the idmap
784 * page to allocate our VAs. If not, the check below will
785 * kick. A potential alternative would be to detect that
786 * overflow and switch to an allocation above the idmap.
788 * The allocated size is always a multiple of PAGE_SIZE.
790 size = PAGE_ALIGN(size + offset_in_page(phys_addr));
791 base = io_map_base - size;
794 * Verify that BIT(VA_BITS - 1) hasn't been flipped by
795 * allocating the new area, as it would indicate we've
796 * overflowed the idmap/IO address range.
798 if ((base ^ io_map_base) & BIT(VA_BITS - 1))
803 mutex_unlock(&kvm_hyp_pgd_mutex);
808 if (__kvm_cpu_uses_extended_idmap())
811 ret = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
813 __phys_to_pfn(phys_addr), prot);
817 *haddr = base + offset_in_page(phys_addr);
824 * create_hyp_io_mappings - Map IO into both kernel and HYP
825 * @phys_addr: The physical start address which gets mapped
826 * @size: Size of the region being mapped
827 * @kaddr: Kernel VA for this mapping
828 * @haddr: HYP VA for this mapping
830 int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
831 void __iomem **kaddr,
832 void __iomem **haddr)
837 *kaddr = ioremap(phys_addr, size);
841 if (is_kernel_in_hyp_mode()) {
846 ret = __create_hyp_private_mapping(phys_addr, size,
847 &addr, PAGE_HYP_DEVICE);
855 *haddr = (void __iomem *)addr;
860 * create_hyp_exec_mappings - Map an executable range into HYP
861 * @phys_addr: The physical start address which gets mapped
862 * @size: Size of the region being mapped
863 * @haddr: HYP VA for this mapping
865 int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
871 BUG_ON(is_kernel_in_hyp_mode());
873 ret = __create_hyp_private_mapping(phys_addr, size,
874 &addr, PAGE_HYP_EXEC);
880 *haddr = (void *)addr;
885 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
886 * @kvm: The KVM struct pointer for the VM.
888 * Allocates only the stage-2 HW PGD level table(s) of size defined by
889 * stage2_pgd_size(kvm).
891 * Note we don't need locking here as this is only called when the VM is
892 * created, which can only be done once.
894 int kvm_alloc_stage2_pgd(struct kvm *kvm)
896 phys_addr_t pgd_phys;
899 if (kvm->arch.pgd != NULL) {
900 kvm_err("kvm_arch already initialized?\n");
904 /* Allocate the HW PGD, making sure that each page gets its own refcount */
905 pgd = alloc_pages_exact(stage2_pgd_size(kvm), GFP_KERNEL | __GFP_ZERO);
909 pgd_phys = virt_to_phys(pgd);
910 if (WARN_ON(pgd_phys & ~kvm_vttbr_baddr_mask(kvm)))
914 kvm->arch.pgd_phys = pgd_phys;
918 static void stage2_unmap_memslot(struct kvm *kvm,
919 struct kvm_memory_slot *memslot)
921 hva_t hva = memslot->userspace_addr;
922 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
923 phys_addr_t size = PAGE_SIZE * memslot->npages;
924 hva_t reg_end = hva + size;
927 * A memory region could potentially cover multiple VMAs, and any holes
928 * between them, so iterate over all of them to find out if we should
931 * +--------------------------------------------+
932 * +---------------+----------------+ +----------------+
933 * | : VMA 1 | VMA 2 | | VMA 3 : |
934 * +---------------+----------------+ +----------------+
936 * +--------------------------------------------+
939 struct vm_area_struct *vma = find_vma(current->mm, hva);
940 hva_t vm_start, vm_end;
942 if (!vma || vma->vm_start >= reg_end)
946 * Take the intersection of this VMA with the memory region
948 vm_start = max(hva, vma->vm_start);
949 vm_end = min(reg_end, vma->vm_end);
951 if (!(vma->vm_flags & VM_PFNMAP)) {
952 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
953 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
956 } while (hva < reg_end);
960 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
961 * @kvm: The struct kvm pointer
963 * Go through the memregions and unmap any reguler RAM
964 * backing memory already mapped to the VM.
966 void stage2_unmap_vm(struct kvm *kvm)
968 struct kvm_memslots *slots;
969 struct kvm_memory_slot *memslot;
972 idx = srcu_read_lock(&kvm->srcu);
973 down_read(¤t->mm->mmap_sem);
974 spin_lock(&kvm->mmu_lock);
976 slots = kvm_memslots(kvm);
977 kvm_for_each_memslot(memslot, slots)
978 stage2_unmap_memslot(kvm, memslot);
980 spin_unlock(&kvm->mmu_lock);
981 up_read(¤t->mm->mmap_sem);
982 srcu_read_unlock(&kvm->srcu, idx);
986 * kvm_free_stage2_pgd - free all stage-2 tables
987 * @kvm: The KVM struct pointer for the VM.
989 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
990 * underlying level-2 and level-3 tables before freeing the actual level-1 table
991 * and setting the struct pointer to NULL.
993 void kvm_free_stage2_pgd(struct kvm *kvm)
997 spin_lock(&kvm->mmu_lock);
999 unmap_stage2_range(kvm, 0, kvm_phys_size(kvm));
1000 pgd = READ_ONCE(kvm->arch.pgd);
1001 kvm->arch.pgd = NULL;
1002 kvm->arch.pgd_phys = 0;
1004 spin_unlock(&kvm->mmu_lock);
1006 /* Free the HW pgd, one page at a time */
1008 free_pages_exact(pgd, stage2_pgd_size(kvm));
1011 static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1017 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
1018 if (stage2_pgd_none(kvm, *pgd)) {
1021 pud = mmu_memory_cache_alloc(cache);
1022 stage2_pgd_populate(kvm, pgd, pud);
1023 get_page(virt_to_page(pgd));
1026 return stage2_pud_offset(kvm, pgd, addr);
1029 static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1035 pud = stage2_get_pud(kvm, cache, addr);
1036 if (!pud || stage2_pud_huge(kvm, *pud))
1039 if (stage2_pud_none(kvm, *pud)) {
1042 pmd = mmu_memory_cache_alloc(cache);
1043 stage2_pud_populate(kvm, pud, pmd);
1044 get_page(virt_to_page(pud));
1047 return stage2_pmd_offset(kvm, pud, addr);
1050 static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
1051 *cache, phys_addr_t addr, const pmd_t *new_pmd)
1053 pmd_t *pmd, old_pmd;
1056 pmd = stage2_get_pmd(kvm, cache, addr);
1061 * Multiple vcpus faulting on the same PMD entry, can
1062 * lead to them sequentially updating the PMD with the
1063 * same value. Following the break-before-make
1064 * (pmd_clear() followed by tlb_flush()) process can
1065 * hinder forward progress due to refaults generated
1066 * on missing translations.
1068 * Skip updating the page table if the entry is
1071 if (pmd_val(old_pmd) == pmd_val(*new_pmd))
1074 if (pmd_present(old_pmd)) {
1076 * If we already have PTE level mapping for this block,
1077 * we must unmap it to avoid inconsistent TLB state and
1078 * leaking the table page. We could end up in this situation
1079 * if the memory slot was marked for dirty logging and was
1080 * reverted, leaving PTE level mappings for the pages accessed
1081 * during the period. So, unmap the PTE level mapping for this
1082 * block and retry, as we could have released the upper level
1083 * table in the process.
1085 * Normal THP split/merge follows mmu_notifier callbacks and do
1086 * get handled accordingly.
1088 if (!pmd_thp_or_huge(old_pmd)) {
1089 unmap_stage2_range(kvm, addr & S2_PMD_MASK, S2_PMD_SIZE);
1093 * Mapping in huge pages should only happen through a
1094 * fault. If a page is merged into a transparent huge
1095 * page, the individual subpages of that huge page
1096 * should be unmapped through MMU notifiers before we
1099 * Merging of CompoundPages is not supported; they
1100 * should become splitting first, unmapped, merged,
1101 * and mapped back in on-demand.
1103 WARN_ON_ONCE(pmd_pfn(old_pmd) != pmd_pfn(*new_pmd));
1105 kvm_tlb_flush_vmid_ipa(kvm, addr);
1107 get_page(virt_to_page(pmd));
1110 kvm_set_pmd(pmd, *new_pmd);
1114 static int stage2_set_pud_huge(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1115 phys_addr_t addr, const pud_t *new_pudp)
1117 pud_t *pudp, old_pud;
1120 pudp = stage2_get_pud(kvm, cache, addr);
1126 * A large number of vcpus faulting on the same stage 2 entry,
1127 * can lead to a refault due to the stage2_pud_clear()/tlb_flush().
1128 * Skip updating the page tables if there is no change.
1130 if (pud_val(old_pud) == pud_val(*new_pudp))
1133 if (stage2_pud_present(kvm, old_pud)) {
1135 * If we already have table level mapping for this block, unmap
1136 * the range for this block and retry.
1138 if (!stage2_pud_huge(kvm, old_pud)) {
1139 unmap_stage2_range(kvm, addr & S2_PUD_MASK, S2_PUD_SIZE);
1143 WARN_ON_ONCE(kvm_pud_pfn(old_pud) != kvm_pud_pfn(*new_pudp));
1144 stage2_pud_clear(kvm, pudp);
1145 kvm_tlb_flush_vmid_ipa(kvm, addr);
1147 get_page(virt_to_page(pudp));
1150 kvm_set_pud(pudp, *new_pudp);
1155 * stage2_get_leaf_entry - walk the stage2 VM page tables and return
1156 * true if a valid and present leaf-entry is found. A pointer to the
1157 * leaf-entry is returned in the appropriate level variable - pudpp,
1160 static bool stage2_get_leaf_entry(struct kvm *kvm, phys_addr_t addr,
1161 pud_t **pudpp, pmd_t **pmdpp, pte_t **ptepp)
1171 pudp = stage2_get_pud(kvm, NULL, addr);
1172 if (!pudp || stage2_pud_none(kvm, *pudp) || !stage2_pud_present(kvm, *pudp))
1175 if (stage2_pud_huge(kvm, *pudp)) {
1180 pmdp = stage2_pmd_offset(kvm, pudp, addr);
1181 if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp))
1184 if (pmd_thp_or_huge(*pmdp)) {
1189 ptep = pte_offset_kernel(pmdp, addr);
1190 if (!ptep || pte_none(*ptep) || !pte_present(*ptep))
1197 static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)
1204 found = stage2_get_leaf_entry(kvm, addr, &pudp, &pmdp, &ptep);
1209 return kvm_s2pud_exec(pudp);
1211 return kvm_s2pmd_exec(pmdp);
1213 return kvm_s2pte_exec(ptep);
1216 static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
1217 phys_addr_t addr, const pte_t *new_pte,
1218 unsigned long flags)
1222 pte_t *pte, old_pte;
1223 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
1224 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
1226 VM_BUG_ON(logging_active && !cache);
1228 /* Create stage-2 page table mapping - Levels 0 and 1 */
1229 pud = stage2_get_pud(kvm, cache, addr);
1232 * Ignore calls from kvm_set_spte_hva for unallocated
1239 * While dirty page logging - dissolve huge PUD, then continue
1240 * on to allocate page.
1243 stage2_dissolve_pud(kvm, addr, pud);
1245 if (stage2_pud_none(kvm, *pud)) {
1247 return 0; /* ignore calls from kvm_set_spte_hva */
1248 pmd = mmu_memory_cache_alloc(cache);
1249 stage2_pud_populate(kvm, pud, pmd);
1250 get_page(virt_to_page(pud));
1253 pmd = stage2_pmd_offset(kvm, pud, addr);
1256 * Ignore calls from kvm_set_spte_hva for unallocated
1263 * While dirty page logging - dissolve huge PMD, then continue on to
1267 stage2_dissolve_pmd(kvm, addr, pmd);
1269 /* Create stage-2 page mappings - Level 2 */
1270 if (pmd_none(*pmd)) {
1272 return 0; /* ignore calls from kvm_set_spte_hva */
1273 pte = mmu_memory_cache_alloc(cache);
1274 kvm_pmd_populate(pmd, pte);
1275 get_page(virt_to_page(pmd));
1278 pte = pte_offset_kernel(pmd, addr);
1280 if (iomap && pte_present(*pte))
1283 /* Create 2nd stage page table mapping - Level 3 */
1285 if (pte_present(old_pte)) {
1286 /* Skip page table update if there is no change */
1287 if (pte_val(old_pte) == pte_val(*new_pte))
1290 kvm_set_pte(pte, __pte(0));
1291 kvm_tlb_flush_vmid_ipa(kvm, addr);
1293 get_page(virt_to_page(pte));
1296 kvm_set_pte(pte, *new_pte);
1300 #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1301 static int stage2_ptep_test_and_clear_young(pte_t *pte)
1303 if (pte_young(*pte)) {
1304 *pte = pte_mkold(*pte);
1310 static int stage2_ptep_test_and_clear_young(pte_t *pte)
1312 return __ptep_test_and_clear_young(pte);
1316 static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
1318 return stage2_ptep_test_and_clear_young((pte_t *)pmd);
1321 static int stage2_pudp_test_and_clear_young(pud_t *pud)
1323 return stage2_ptep_test_and_clear_young((pte_t *)pud);
1327 * kvm_phys_addr_ioremap - map a device range to guest IPA
1329 * @kvm: The KVM pointer
1330 * @guest_ipa: The IPA at which to insert the mapping
1331 * @pa: The physical address of the device
1332 * @size: The size of the mapping
1334 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
1335 phys_addr_t pa, unsigned long size, bool writable)
1337 phys_addr_t addr, end;
1340 struct kvm_mmu_memory_cache cache = { 0, };
1342 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
1343 pfn = __phys_to_pfn(pa);
1345 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
1346 pte_t pte = kvm_pfn_pte(pfn, PAGE_S2_DEVICE);
1349 pte = kvm_s2pte_mkwrite(pte);
1351 ret = mmu_topup_memory_cache(&cache,
1352 kvm_mmu_cache_min_pages(kvm),
1356 spin_lock(&kvm->mmu_lock);
1357 ret = stage2_set_pte(kvm, &cache, addr, &pte,
1358 KVM_S2PTE_FLAG_IS_IOMAP);
1359 spin_unlock(&kvm->mmu_lock);
1367 mmu_free_memory_cache(&cache);
1371 static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
1373 kvm_pfn_t pfn = *pfnp;
1374 gfn_t gfn = *ipap >> PAGE_SHIFT;
1375 struct page *page = pfn_to_page(pfn);
1378 * PageTransCompoundMap() returns true for THP and
1379 * hugetlbfs. Make sure the adjustment is done only for THP
1382 if (!PageHuge(page) && PageTransCompoundMap(page)) {
1385 * The address we faulted on is backed by a transparent huge
1386 * page. However, because we map the compound huge page and
1387 * not the individual tail page, we need to transfer the
1388 * refcount to the head page. We have to be careful that the
1389 * THP doesn't start to split while we are adjusting the
1392 * We are sure this doesn't happen, because mmu_notifier_retry
1393 * was successful and we are holding the mmu_lock, so if this
1394 * THP is trying to split, it will be blocked in the mmu
1395 * notifier before touching any of the pages, specifically
1396 * before being able to call __split_huge_page_refcount().
1398 * We can therefore safely transfer the refcount from PG_tail
1399 * to PG_head and switch the pfn from a tail page to the head
1402 mask = PTRS_PER_PMD - 1;
1403 VM_BUG_ON((gfn & mask) != (pfn & mask));
1406 kvm_release_pfn_clean(pfn);
1419 * stage2_wp_ptes - write protect PMD range
1420 * @pmd: pointer to pmd entry
1421 * @addr: range start address
1422 * @end: range end address
1424 static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1428 pte = pte_offset_kernel(pmd, addr);
1430 if (!pte_none(*pte)) {
1431 if (!kvm_s2pte_readonly(pte))
1432 kvm_set_s2pte_readonly(pte);
1434 } while (pte++, addr += PAGE_SIZE, addr != end);
1438 * stage2_wp_pmds - write protect PUD range
1439 * kvm: kvm instance for the VM
1440 * @pud: pointer to pud entry
1441 * @addr: range start address
1442 * @end: range end address
1444 static void stage2_wp_pmds(struct kvm *kvm, pud_t *pud,
1445 phys_addr_t addr, phys_addr_t end)
1450 pmd = stage2_pmd_offset(kvm, pud, addr);
1453 next = stage2_pmd_addr_end(kvm, addr, end);
1454 if (!pmd_none(*pmd)) {
1455 if (pmd_thp_or_huge(*pmd)) {
1456 if (!kvm_s2pmd_readonly(pmd))
1457 kvm_set_s2pmd_readonly(pmd);
1459 stage2_wp_ptes(pmd, addr, next);
1462 } while (pmd++, addr = next, addr != end);
1466 * stage2_wp_puds - write protect PGD range
1467 * @pgd: pointer to pgd entry
1468 * @addr: range start address
1469 * @end: range end address
1471 static void stage2_wp_puds(struct kvm *kvm, pgd_t *pgd,
1472 phys_addr_t addr, phys_addr_t end)
1477 pud = stage2_pud_offset(kvm, pgd, addr);
1479 next = stage2_pud_addr_end(kvm, addr, end);
1480 if (!stage2_pud_none(kvm, *pud)) {
1481 if (stage2_pud_huge(kvm, *pud)) {
1482 if (!kvm_s2pud_readonly(pud))
1483 kvm_set_s2pud_readonly(pud);
1485 stage2_wp_pmds(kvm, pud, addr, next);
1488 } while (pud++, addr = next, addr != end);
1492 * stage2_wp_range() - write protect stage2 memory region range
1493 * @kvm: The KVM pointer
1494 * @addr: Start address of range
1495 * @end: End address of range
1497 static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1502 pgd = kvm->arch.pgd + stage2_pgd_index(kvm, addr);
1505 * Release kvm_mmu_lock periodically if the memory region is
1506 * large. Otherwise, we may see kernel panics with
1507 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1508 * CONFIG_LOCKDEP. Additionally, holding the lock too long
1509 * will also starve other vCPUs. We have to also make sure
1510 * that the page tables are not freed while we released
1513 cond_resched_lock(&kvm->mmu_lock);
1514 if (!READ_ONCE(kvm->arch.pgd))
1516 next = stage2_pgd_addr_end(kvm, addr, end);
1517 if (stage2_pgd_present(kvm, *pgd))
1518 stage2_wp_puds(kvm, pgd, addr, next);
1519 } while (pgd++, addr = next, addr != end);
1523 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1524 * @kvm: The KVM pointer
1525 * @slot: The memory slot to write protect
1527 * Called to start logging dirty pages after memory region
1528 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1529 * all present PUD, PMD and PTEs are write protected in the memory region.
1530 * Afterwards read of dirty page log can be called.
1532 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1533 * serializing operations for VM memory regions.
1535 void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1537 struct kvm_memslots *slots = kvm_memslots(kvm);
1538 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
1539 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1540 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1542 spin_lock(&kvm->mmu_lock);
1543 stage2_wp_range(kvm, start, end);
1544 spin_unlock(&kvm->mmu_lock);
1545 kvm_flush_remote_tlbs(kvm);
1549 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
1550 * @kvm: The KVM pointer
1551 * @slot: The memory slot associated with mask
1552 * @gfn_offset: The gfn offset in memory slot
1553 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1554 * slot to be write protected
1556 * Walks bits set in mask write protects the associated pte's. Caller must
1557 * acquire kvm_mmu_lock.
1559 static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
1560 struct kvm_memory_slot *slot,
1561 gfn_t gfn_offset, unsigned long mask)
1563 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1564 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1565 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1567 stage2_wp_range(kvm, start, end);
1571 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1574 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1575 * enable dirty logging for them.
1577 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1578 struct kvm_memory_slot *slot,
1579 gfn_t gfn_offset, unsigned long mask)
1581 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1584 static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
1586 __clean_dcache_guest_page(pfn, size);
1589 static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
1591 __invalidate_icache_guest_page(pfn, size);
1594 static void kvm_send_hwpoison_signal(unsigned long address,
1595 struct vm_area_struct *vma)
1599 if (is_vm_hugetlb_page(vma))
1600 lsb = huge_page_shift(hstate_vma(vma));
1604 send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, current);
1607 static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,
1609 unsigned long map_size)
1612 hva_t uaddr_start, uaddr_end;
1615 size = memslot->npages * PAGE_SIZE;
1617 gpa_start = memslot->base_gfn << PAGE_SHIFT;
1619 uaddr_start = memslot->userspace_addr;
1620 uaddr_end = uaddr_start + size;
1623 * Pages belonging to memslots that don't have the same alignment
1624 * within a PMD/PUD for userspace and IPA cannot be mapped with stage-2
1625 * PMD/PUD entries, because we'll end up mapping the wrong pages.
1627 * Consider a layout like the following:
1629 * memslot->userspace_addr:
1630 * +-----+--------------------+--------------------+---+
1631 * |abcde|fgh Stage-1 block | Stage-1 block tv|xyz|
1632 * +-----+--------------------+--------------------+---+
1634 * memslot->base_gfn << PAGE_SIZE:
1635 * +---+--------------------+--------------------+-----+
1636 * |abc|def Stage-2 block | Stage-2 block |tvxyz|
1637 * +---+--------------------+--------------------+-----+
1639 * If we create those stage-2 blocks, we'll end up with this incorrect
1645 if ((gpa_start & (map_size - 1)) != (uaddr_start & (map_size - 1)))
1649 * Next, let's make sure we're not trying to map anything not covered
1650 * by the memslot. This means we have to prohibit block size mappings
1651 * for the beginning and end of a non-block aligned and non-block sized
1652 * memory slot (illustrated by the head and tail parts of the
1653 * userspace view above containing pages 'abcde' and 'xyz',
1656 * Note that it doesn't matter if we do the check using the
1657 * userspace_addr or the base_gfn, as both are equally aligned (per
1658 * the check above) and equally sized.
1660 return (hva & ~(map_size - 1)) >= uaddr_start &&
1661 (hva & ~(map_size - 1)) + map_size <= uaddr_end;
1664 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1665 struct kvm_memory_slot *memslot, unsigned long hva,
1666 unsigned long fault_status)
1669 bool write_fault, writable, force_pte = false;
1670 bool exec_fault, needs_exec;
1671 unsigned long mmu_seq;
1672 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
1673 struct kvm *kvm = vcpu->kvm;
1674 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
1675 struct vm_area_struct *vma;
1677 pgprot_t mem_type = PAGE_S2;
1678 bool logging_active = memslot_is_logging(memslot);
1679 unsigned long vma_pagesize, flags = 0;
1681 write_fault = kvm_is_write_fault(vcpu);
1682 exec_fault = kvm_vcpu_trap_is_iabt(vcpu);
1683 VM_BUG_ON(write_fault && exec_fault);
1685 if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
1686 kvm_err("Unexpected L2 read permission error\n");
1690 /* Let's check if we will get back a huge page backed by hugetlbfs */
1691 down_read(¤t->mm->mmap_sem);
1692 vma = find_vma_intersection(current->mm, hva, hva + 1);
1693 if (unlikely(!vma)) {
1694 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1695 up_read(¤t->mm->mmap_sem);
1699 vma_pagesize = vma_kernel_pagesize(vma);
1700 if (logging_active ||
1701 !fault_supports_stage2_huge_mapping(memslot, hva, vma_pagesize)) {
1703 vma_pagesize = PAGE_SIZE;
1707 * The stage2 has a minimum of 2 level table (For arm64 see
1708 * kvm_arm_setup_stage2()). Hence, we are guaranteed that we can
1709 * use PMD_SIZE huge mappings (even when the PMD is folded into PGD).
1710 * As for PUD huge maps, we must make sure that we have at least
1711 * 3 levels, i.e, PMD is not folded.
1713 if (vma_pagesize == PMD_SIZE ||
1714 (vma_pagesize == PUD_SIZE && kvm_stage2_has_pmd(kvm)))
1715 gfn = (fault_ipa & huge_page_mask(hstate_vma(vma))) >> PAGE_SHIFT;
1716 up_read(¤t->mm->mmap_sem);
1718 /* We need minimum second+third level pages */
1719 ret = mmu_topup_memory_cache(memcache, kvm_mmu_cache_min_pages(kvm),
1724 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1726 * Ensure the read of mmu_notifier_seq happens before we call
1727 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1728 * the page we just got a reference to gets unmapped before we have a
1729 * chance to grab the mmu_lock, which ensure that if the page gets
1730 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1731 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1732 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1736 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
1737 if (pfn == KVM_PFN_ERR_HWPOISON) {
1738 kvm_send_hwpoison_signal(hva, vma);
1741 if (is_error_noslot_pfn(pfn))
1744 if (kvm_is_device_pfn(pfn)) {
1745 mem_type = PAGE_S2_DEVICE;
1746 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1747 } else if (logging_active) {
1749 * Faults on pages in a memslot with logging enabled
1750 * should not be mapped with huge pages (it introduces churn
1751 * and performance degradation), so force a pte mapping.
1753 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1756 * Only actually map the page as writable if this was a write
1763 spin_lock(&kvm->mmu_lock);
1764 if (mmu_notifier_retry(kvm, mmu_seq))
1767 if (vma_pagesize == PAGE_SIZE && !force_pte) {
1769 * Only PMD_SIZE transparent hugepages(THP) are
1770 * currently supported. This code will need to be
1771 * updated to support other THP sizes.
1773 * Make sure the host VA and the guest IPA are sufficiently
1774 * aligned and that the block is contained within the memslot.
1776 if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE) &&
1777 transparent_hugepage_adjust(&pfn, &fault_ipa))
1778 vma_pagesize = PMD_SIZE;
1782 kvm_set_pfn_dirty(pfn);
1784 if (fault_status != FSC_PERM)
1785 clean_dcache_guest_page(pfn, vma_pagesize);
1788 invalidate_icache_guest_page(pfn, vma_pagesize);
1791 * If we took an execution fault we have made the
1792 * icache/dcache coherent above and should now let the s2
1793 * mapping be executable.
1795 * Write faults (!exec_fault && FSC_PERM) are orthogonal to
1796 * execute permissions, and we preserve whatever we have.
1798 needs_exec = exec_fault ||
1799 (fault_status == FSC_PERM && stage2_is_exec(kvm, fault_ipa));
1801 if (vma_pagesize == PUD_SIZE) {
1802 pud_t new_pud = kvm_pfn_pud(pfn, mem_type);
1804 new_pud = kvm_pud_mkhuge(new_pud);
1806 new_pud = kvm_s2pud_mkwrite(new_pud);
1809 new_pud = kvm_s2pud_mkexec(new_pud);
1811 ret = stage2_set_pud_huge(kvm, memcache, fault_ipa, &new_pud);
1812 } else if (vma_pagesize == PMD_SIZE) {
1813 pmd_t new_pmd = kvm_pfn_pmd(pfn, mem_type);
1815 new_pmd = kvm_pmd_mkhuge(new_pmd);
1818 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
1821 new_pmd = kvm_s2pmd_mkexec(new_pmd);
1823 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1825 pte_t new_pte = kvm_pfn_pte(pfn, mem_type);
1828 new_pte = kvm_s2pte_mkwrite(new_pte);
1829 mark_page_dirty(kvm, gfn);
1833 new_pte = kvm_s2pte_mkexec(new_pte);
1835 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
1839 spin_unlock(&kvm->mmu_lock);
1840 kvm_set_pfn_accessed(pfn);
1841 kvm_release_pfn_clean(pfn);
1846 * Resolve the access fault by making the page young again.
1847 * Note that because the faulting entry is guaranteed not to be
1848 * cached in the TLB, we don't need to invalidate anything.
1849 * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
1850 * so there is no need for atomic (pte|pmd)_mkyoung operations.
1852 static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1858 bool pfn_valid = false;
1860 trace_kvm_access_fault(fault_ipa);
1862 spin_lock(&vcpu->kvm->mmu_lock);
1864 if (!stage2_get_leaf_entry(vcpu->kvm, fault_ipa, &pud, &pmd, &pte))
1867 if (pud) { /* HugeTLB */
1868 *pud = kvm_s2pud_mkyoung(*pud);
1869 pfn = kvm_pud_pfn(*pud);
1871 } else if (pmd) { /* THP, HugeTLB */
1872 *pmd = pmd_mkyoung(*pmd);
1873 pfn = pmd_pfn(*pmd);
1876 *pte = pte_mkyoung(*pte); /* Just a page... */
1877 pfn = pte_pfn(*pte);
1882 spin_unlock(&vcpu->kvm->mmu_lock);
1884 kvm_set_pfn_accessed(pfn);
1888 * kvm_handle_guest_abort - handles all 2nd stage aborts
1889 * @vcpu: the VCPU pointer
1890 * @run: the kvm_run structure
1892 * Any abort that gets to the host is almost guaranteed to be caused by a
1893 * missing second stage translation table entry, which can mean that either the
1894 * guest simply needs more memory and we must allocate an appropriate page or it
1895 * can mean that the guest tried to access I/O memory, which is emulated by user
1896 * space. The distinction is based on the IPA causing the fault and whether this
1897 * memory region has been registered as standard RAM by user space.
1899 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1901 unsigned long fault_status;
1902 phys_addr_t fault_ipa;
1903 struct kvm_memory_slot *memslot;
1905 bool is_iabt, write_fault, writable;
1909 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
1911 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
1912 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
1914 /* Synchronous External Abort? */
1915 if (kvm_vcpu_dabt_isextabt(vcpu)) {
1917 * For RAS the host kernel may handle this abort.
1918 * There is no need to pass the error into the guest.
1920 if (!kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
1923 if (unlikely(!is_iabt)) {
1924 kvm_inject_vabt(vcpu);
1929 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1930 kvm_vcpu_get_hfar(vcpu), fault_ipa);
1932 /* Check the stage-2 fault is trans. fault or write fault */
1933 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1934 fault_status != FSC_ACCESS) {
1935 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1936 kvm_vcpu_trap_get_class(vcpu),
1937 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1938 (unsigned long)kvm_vcpu_get_hsr(vcpu));
1942 idx = srcu_read_lock(&vcpu->kvm->srcu);
1944 gfn = fault_ipa >> PAGE_SHIFT;
1945 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1946 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
1947 write_fault = kvm_is_write_fault(vcpu);
1948 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
1950 /* Prefetch Abort on I/O address */
1951 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
1957 * Check for a cache maintenance operation. Since we
1958 * ended-up here, we know it is outside of any memory
1959 * slot. But we can't find out if that is for a device,
1960 * or if the guest is just being stupid. The only thing
1961 * we know for sure is that this range cannot be cached.
1963 * So let's assume that the guest is just being
1964 * cautious, and skip the instruction.
1966 if (kvm_vcpu_dabt_is_cm(vcpu)) {
1967 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1973 * The IPA is reported as [MAX:12], so we need to
1974 * complement it with the bottom 12 bits from the
1975 * faulting VA. This is always 12 bits, irrespective
1978 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
1979 ret = io_mem_abort(vcpu, run, fault_ipa);
1983 /* Userspace should not be able to register out-of-bounds IPAs */
1984 VM_BUG_ON(fault_ipa >= kvm_phys_size(vcpu->kvm));
1986 if (fault_status == FSC_ACCESS) {
1987 handle_access_fault(vcpu, fault_ipa);
1992 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
1996 srcu_read_unlock(&vcpu->kvm->srcu, idx);
2000 static int handle_hva_to_gpa(struct kvm *kvm,
2001 unsigned long start,
2003 int (*handler)(struct kvm *kvm,
2004 gpa_t gpa, u64 size,
2008 struct kvm_memslots *slots;
2009 struct kvm_memory_slot *memslot;
2012 slots = kvm_memslots(kvm);
2014 /* we only care about the pages that the guest sees */
2015 kvm_for_each_memslot(memslot, slots) {
2016 unsigned long hva_start, hva_end;
2019 hva_start = max(start, memslot->userspace_addr);
2020 hva_end = min(end, memslot->userspace_addr +
2021 (memslot->npages << PAGE_SHIFT));
2022 if (hva_start >= hva_end)
2025 gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
2026 ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
2032 static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
2034 unmap_stage2_range(kvm, gpa, size);
2038 int kvm_unmap_hva_range(struct kvm *kvm,
2039 unsigned long start, unsigned long end)
2044 trace_kvm_unmap_hva_range(start, end);
2045 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
2049 static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
2051 pte_t *pte = (pte_t *)data;
2053 WARN_ON(size != PAGE_SIZE);
2055 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
2056 * flag clear because MMU notifiers will have unmapped a huge PMD before
2057 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
2058 * therefore stage2_set_pte() never needs to clear out a huge PMD
2059 * through this calling path.
2061 stage2_set_pte(kvm, NULL, gpa, pte, 0);
2066 int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
2068 unsigned long end = hva + PAGE_SIZE;
2069 kvm_pfn_t pfn = pte_pfn(pte);
2075 trace_kvm_set_spte_hva(hva);
2078 * We've moved a page around, probably through CoW, so let's treat it
2079 * just like a translation fault and clean the cache to the PoC.
2081 clean_dcache_guest_page(pfn, PAGE_SIZE);
2082 stage2_pte = kvm_pfn_pte(pfn, PAGE_S2);
2083 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
2088 static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
2094 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
2095 if (!stage2_get_leaf_entry(kvm, gpa, &pud, &pmd, &pte))
2099 return stage2_pudp_test_and_clear_young(pud);
2101 return stage2_pmdp_test_and_clear_young(pmd);
2103 return stage2_ptep_test_and_clear_young(pte);
2106 static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
2112 WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
2113 if (!stage2_get_leaf_entry(kvm, gpa, &pud, &pmd, &pte))
2117 return kvm_s2pud_young(*pud);
2119 return pmd_young(*pmd);
2121 return pte_young(*pte);
2124 int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
2128 trace_kvm_age_hva(start, end);
2129 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
2132 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
2136 trace_kvm_test_age_hva(hva);
2137 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
2140 void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
2142 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
2145 phys_addr_t kvm_mmu_get_httbr(void)
2147 if (__kvm_cpu_uses_extended_idmap())
2148 return virt_to_phys(merged_hyp_pgd);
2150 return virt_to_phys(hyp_pgd);
2153 phys_addr_t kvm_get_idmap_vector(void)
2155 return hyp_idmap_vector;
2158 static int kvm_map_idmap_text(pgd_t *pgd)
2162 /* Create the idmap in the boot page tables */
2163 err = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
2164 hyp_idmap_start, hyp_idmap_end,
2165 __phys_to_pfn(hyp_idmap_start),
2168 kvm_err("Failed to idmap %lx-%lx\n",
2169 hyp_idmap_start, hyp_idmap_end);
2174 int kvm_mmu_init(void)
2178 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
2179 hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
2180 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
2181 hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
2182 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
2185 * We rely on the linker script to ensure at build time that the HYP
2186 * init code does not cross a page boundary.
2188 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
2190 kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
2191 kvm_debug("HYP VA range: %lx:%lx\n",
2192 kern_hyp_va(PAGE_OFFSET),
2193 kern_hyp_va((unsigned long)high_memory - 1));
2195 if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
2196 hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
2197 hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
2199 * The idmap page is intersecting with the VA space,
2200 * it is not safe to continue further.
2202 kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
2207 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
2209 kvm_err("Hyp mode PGD not allocated\n");
2214 if (__kvm_cpu_uses_extended_idmap()) {
2215 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2217 if (!boot_hyp_pgd) {
2218 kvm_err("Hyp boot PGD not allocated\n");
2223 err = kvm_map_idmap_text(boot_hyp_pgd);
2227 merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
2228 if (!merged_hyp_pgd) {
2229 kvm_err("Failed to allocate extra HYP pgd\n");
2232 __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
2235 err = kvm_map_idmap_text(hyp_pgd);
2240 io_map_base = hyp_idmap_start;
2247 void kvm_arch_commit_memory_region(struct kvm *kvm,
2248 const struct kvm_userspace_memory_region *mem,
2249 const struct kvm_memory_slot *old,
2250 const struct kvm_memory_slot *new,
2251 enum kvm_mr_change change)
2254 * At this point memslot has been committed and there is an
2255 * allocated dirty_bitmap[], dirty pages will be be tracked while the
2256 * memory slot is write protected.
2258 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
2259 kvm_mmu_wp_memory_region(kvm, mem->slot);
2262 int kvm_arch_prepare_memory_region(struct kvm *kvm,
2263 struct kvm_memory_slot *memslot,
2264 const struct kvm_userspace_memory_region *mem,
2265 enum kvm_mr_change change)
2267 hva_t hva = mem->userspace_addr;
2268 hva_t reg_end = hva + mem->memory_size;
2269 bool writable = !(mem->flags & KVM_MEM_READONLY);
2272 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
2273 change != KVM_MR_FLAGS_ONLY)
2277 * Prevent userspace from creating a memory region outside of the IPA
2278 * space addressable by the KVM guest IPA space.
2280 if (memslot->base_gfn + memslot->npages >=
2281 (kvm_phys_size(kvm) >> PAGE_SHIFT))
2284 down_read(¤t->mm->mmap_sem);
2286 * A memory region could potentially cover multiple VMAs, and any holes
2287 * between them, so iterate over all of them to find out if we can map
2288 * any of them right now.
2290 * +--------------------------------------------+
2291 * +---------------+----------------+ +----------------+
2292 * | : VMA 1 | VMA 2 | | VMA 3 : |
2293 * +---------------+----------------+ +----------------+
2295 * +--------------------------------------------+
2298 struct vm_area_struct *vma = find_vma(current->mm, hva);
2299 hva_t vm_start, vm_end;
2301 if (!vma || vma->vm_start >= reg_end)
2305 * Mapping a read-only VMA is only allowed if the
2306 * memory region is configured as read-only.
2308 if (writable && !(vma->vm_flags & VM_WRITE)) {
2314 * Take the intersection of this VMA with the memory region
2316 vm_start = max(hva, vma->vm_start);
2317 vm_end = min(reg_end, vma->vm_end);
2319 if (vma->vm_flags & VM_PFNMAP) {
2320 gpa_t gpa = mem->guest_phys_addr +
2321 (vm_start - mem->userspace_addr);
2324 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
2325 pa += vm_start - vma->vm_start;
2327 /* IO region dirty page logging not allowed */
2328 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
2333 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
2340 } while (hva < reg_end);
2342 if (change == KVM_MR_FLAGS_ONLY)
2345 spin_lock(&kvm->mmu_lock);
2347 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
2349 stage2_flush_memslot(kvm, memslot);
2350 spin_unlock(&kvm->mmu_lock);
2352 up_read(¤t->mm->mmap_sem);
2356 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
2357 struct kvm_memory_slot *dont)
2361 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2362 unsigned long npages)
2367 void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
2371 void kvm_arch_flush_shadow_all(struct kvm *kvm)
2373 kvm_free_stage2_pgd(kvm);
2376 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
2377 struct kvm_memory_slot *slot)
2379 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
2380 phys_addr_t size = slot->npages << PAGE_SHIFT;
2382 spin_lock(&kvm->mmu_lock);
2383 unmap_stage2_range(kvm, gpa, size);
2384 spin_unlock(&kvm->mmu_lock);
2388 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
2391 * - S/W ops are local to a CPU (not broadcast)
2392 * - We have line migration behind our back (speculation)
2393 * - System caches don't support S/W at all (damn!)
2395 * In the face of the above, the best we can do is to try and convert
2396 * S/W ops to VA ops. Because the guest is not allowed to infer the
2397 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
2398 * which is a rather good thing for us.
2400 * Also, it is only used when turning caches on/off ("The expected
2401 * usage of the cache maintenance instructions that operate by set/way
2402 * is associated with the cache maintenance instructions associated
2403 * with the powerdown and powerup of caches, if this is required by
2404 * the implementation.").
2406 * We use the following policy:
2408 * - If we trap a S/W operation, we enable VM trapping to detect
2409 * caches being turned on/off, and do a full clean.
2411 * - We flush the caches on both caches being turned on and off.
2413 * - Once the caches are enabled, we stop trapping VM ops.
2415 void kvm_set_way_flush(struct kvm_vcpu *vcpu)
2417 unsigned long hcr = *vcpu_hcr(vcpu);
2420 * If this is the first time we do a S/W operation
2421 * (i.e. HCR_TVM not set) flush the whole memory, and set the
2424 * Otherwise, rely on the VM trapping to wait for the MMU +
2425 * Caches to be turned off. At that point, we'll be able to
2426 * clean the caches again.
2428 if (!(hcr & HCR_TVM)) {
2429 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
2430 vcpu_has_cache_enabled(vcpu));
2431 stage2_flush_vm(vcpu->kvm);
2432 *vcpu_hcr(vcpu) = hcr | HCR_TVM;
2436 void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
2438 bool now_enabled = vcpu_has_cache_enabled(vcpu);
2441 * If switching the MMU+caches on, need to invalidate the caches.
2442 * If switching it off, need to clean the caches.
2443 * Clean + invalidate does the trick always.
2445 if (now_enabled != was_enabled)
2446 stage2_flush_vm(vcpu->kvm);
2448 /* Caches are now on, stop trapping VM ops (until a S/W op) */
2450 *vcpu_hcr(vcpu) &= ~HCR_TVM;
2452 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);