2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <linux/mman.h>
20 #include <linux/kvm_host.h>
22 #include <linux/hugetlb.h>
23 #include <trace/events/kvm.h>
24 #include <asm/pgalloc.h>
25 #include <asm/cacheflush.h>
26 #include <asm/kvm_arm.h>
27 #include <asm/kvm_mmu.h>
28 #include <asm/kvm_mmio.h>
29 #include <asm/kvm_asm.h>
30 #include <asm/kvm_emulate.h>
35 extern char __hyp_idmap_text_start
[], __hyp_idmap_text_end
[];
37 static pgd_t
*boot_hyp_pgd
;
38 static pgd_t
*hyp_pgd
;
39 static pgd_t
*merged_hyp_pgd
;
40 static DEFINE_MUTEX(kvm_hyp_pgd_mutex
);
42 static unsigned long hyp_idmap_start
;
43 static unsigned long hyp_idmap_end
;
44 static phys_addr_t hyp_idmap_vector
;
46 #define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
48 #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
49 #define kvm_pud_huge(_x) pud_huge(_x)
51 #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
52 #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
54 static bool memslot_is_logging(struct kvm_memory_slot
*memslot
)
56 return memslot
->dirty_bitmap
&& !(memslot
->flags
& KVM_MEM_READONLY
);
60 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
61 * @kvm: pointer to kvm structure.
63 * Interface to HYP function to flush all VM TLB entries
65 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
67 kvm_call_hyp(__kvm_tlb_flush_vmid
, kvm
);
70 static void kvm_tlb_flush_vmid_ipa(struct kvm
*kvm
, phys_addr_t ipa
)
73 * This function also gets called when dealing with HYP page
74 * tables. As HYP doesn't have an associated struct kvm (and
75 * the HYP page tables are fairly static), we don't do
79 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa
, kvm
, ipa
);
83 * D-Cache management functions. They take the page table entries by
84 * value, as they are flushing the cache using the kernel mapping (or
87 static void kvm_flush_dcache_pte(pte_t pte
)
89 __kvm_flush_dcache_pte(pte
);
92 static void kvm_flush_dcache_pmd(pmd_t pmd
)
94 __kvm_flush_dcache_pmd(pmd
);
97 static void kvm_flush_dcache_pud(pud_t pud
)
99 __kvm_flush_dcache_pud(pud
);
102 static bool kvm_is_device_pfn(unsigned long pfn
)
104 return !pfn_valid(pfn
);
108 * stage2_dissolve_pmd() - clear and flush huge PMD entry
109 * @kvm: pointer to kvm structure.
111 * @pmd: pmd pointer for IPA
113 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
114 * pages in the range dirty.
116 static void stage2_dissolve_pmd(struct kvm
*kvm
, phys_addr_t addr
, pmd_t
*pmd
)
118 if (!kvm_pmd_huge(*pmd
))
122 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
123 put_page(virt_to_page(pmd
));
126 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
131 BUG_ON(max
> KVM_NR_MEM_OBJS
);
132 if (cache
->nobjs
>= min
)
134 while (cache
->nobjs
< max
) {
135 page
= (void *)__get_free_page(PGALLOC_GFP
);
138 cache
->objects
[cache
->nobjs
++] = page
;
143 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
146 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
149 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
)
153 BUG_ON(!mc
|| !mc
->nobjs
);
154 p
= mc
->objects
[--mc
->nobjs
];
158 static void clear_pgd_entry(struct kvm
*kvm
, pgd_t
*pgd
, phys_addr_t addr
)
160 pud_t
*pud_table __maybe_unused
= pud_offset(pgd
, 0);
162 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
163 pud_free(NULL
, pud_table
);
164 put_page(virt_to_page(pgd
));
167 static void clear_pud_entry(struct kvm
*kvm
, pud_t
*pud
, phys_addr_t addr
)
169 pmd_t
*pmd_table
= pmd_offset(pud
, 0);
170 VM_BUG_ON(pud_huge(*pud
));
172 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
173 pmd_free(NULL
, pmd_table
);
174 put_page(virt_to_page(pud
));
177 static void clear_pmd_entry(struct kvm
*kvm
, pmd_t
*pmd
, phys_addr_t addr
)
179 pte_t
*pte_table
= pte_offset_kernel(pmd
, 0);
180 VM_BUG_ON(kvm_pmd_huge(*pmd
));
182 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
183 pte_free_kernel(NULL
, pte_table
);
184 put_page(virt_to_page(pmd
));
188 * Unmapping vs dcache management:
190 * If a guest maps certain memory pages as uncached, all writes will
191 * bypass the data cache and go directly to RAM. However, the CPUs
192 * can still speculate reads (not writes) and fill cache lines with
195 * Those cache lines will be *clean* cache lines though, so a
196 * clean+invalidate operation is equivalent to an invalidate
197 * operation, because no cache lines are marked dirty.
199 * Those clean cache lines could be filled prior to an uncached write
200 * by the guest, and the cache coherent IO subsystem would therefore
201 * end up writing old data to disk.
203 * This is why right after unmapping a page/section and invalidating
204 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
205 * the IO subsystem will never hit in the cache.
207 static void unmap_ptes(struct kvm
*kvm
, pmd_t
*pmd
,
208 phys_addr_t addr
, phys_addr_t end
)
210 phys_addr_t start_addr
= addr
;
211 pte_t
*pte
, *start_pte
;
213 start_pte
= pte
= pte_offset_kernel(pmd
, addr
);
215 if (!pte_none(*pte
)) {
216 pte_t old_pte
= *pte
;
218 kvm_set_pte(pte
, __pte(0));
219 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
221 /* No need to invalidate the cache for device mappings */
222 if (!kvm_is_device_pfn(pte_pfn(old_pte
)))
223 kvm_flush_dcache_pte(old_pte
);
225 put_page(virt_to_page(pte
));
227 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
229 if (kvm_pte_table_empty(kvm
, start_pte
))
230 clear_pmd_entry(kvm
, pmd
, start_addr
);
233 static void unmap_pmds(struct kvm
*kvm
, pud_t
*pud
,
234 phys_addr_t addr
, phys_addr_t end
)
236 phys_addr_t next
, start_addr
= addr
;
237 pmd_t
*pmd
, *start_pmd
;
239 start_pmd
= pmd
= pmd_offset(pud
, addr
);
241 next
= kvm_pmd_addr_end(addr
, end
);
242 if (!pmd_none(*pmd
)) {
243 if (kvm_pmd_huge(*pmd
)) {
244 pmd_t old_pmd
= *pmd
;
247 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
249 kvm_flush_dcache_pmd(old_pmd
);
251 put_page(virt_to_page(pmd
));
253 unmap_ptes(kvm
, pmd
, addr
, next
);
256 } while (pmd
++, addr
= next
, addr
!= end
);
258 if (kvm_pmd_table_empty(kvm
, start_pmd
))
259 clear_pud_entry(kvm
, pud
, start_addr
);
262 static void unmap_puds(struct kvm
*kvm
, pgd_t
*pgd
,
263 phys_addr_t addr
, phys_addr_t end
)
265 phys_addr_t next
, start_addr
= addr
;
266 pud_t
*pud
, *start_pud
;
268 start_pud
= pud
= pud_offset(pgd
, addr
);
270 next
= kvm_pud_addr_end(addr
, end
);
271 if (!pud_none(*pud
)) {
272 if (pud_huge(*pud
)) {
273 pud_t old_pud
= *pud
;
276 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
278 kvm_flush_dcache_pud(old_pud
);
280 put_page(virt_to_page(pud
));
282 unmap_pmds(kvm
, pud
, addr
, next
);
285 } while (pud
++, addr
= next
, addr
!= end
);
287 if (kvm_pud_table_empty(kvm
, start_pud
))
288 clear_pgd_entry(kvm
, pgd
, start_addr
);
292 static void unmap_range(struct kvm
*kvm
, pgd_t
*pgdp
,
293 phys_addr_t start
, u64 size
)
296 phys_addr_t addr
= start
, end
= start
+ size
;
299 pgd
= pgdp
+ kvm_pgd_index(addr
);
301 next
= kvm_pgd_addr_end(addr
, end
);
303 unmap_puds(kvm
, pgd
, addr
, next
);
304 } while (pgd
++, addr
= next
, addr
!= end
);
307 static void stage2_flush_ptes(struct kvm
*kvm
, pmd_t
*pmd
,
308 phys_addr_t addr
, phys_addr_t end
)
312 pte
= pte_offset_kernel(pmd
, addr
);
314 if (!pte_none(*pte
) && !kvm_is_device_pfn(pte_pfn(*pte
)))
315 kvm_flush_dcache_pte(*pte
);
316 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
319 static void stage2_flush_pmds(struct kvm
*kvm
, pud_t
*pud
,
320 phys_addr_t addr
, phys_addr_t end
)
325 pmd
= pmd_offset(pud
, addr
);
327 next
= kvm_pmd_addr_end(addr
, end
);
328 if (!pmd_none(*pmd
)) {
329 if (kvm_pmd_huge(*pmd
))
330 kvm_flush_dcache_pmd(*pmd
);
332 stage2_flush_ptes(kvm
, pmd
, addr
, next
);
334 } while (pmd
++, addr
= next
, addr
!= end
);
337 static void stage2_flush_puds(struct kvm
*kvm
, pgd_t
*pgd
,
338 phys_addr_t addr
, phys_addr_t end
)
343 pud
= pud_offset(pgd
, addr
);
345 next
= kvm_pud_addr_end(addr
, end
);
346 if (!pud_none(*pud
)) {
348 kvm_flush_dcache_pud(*pud
);
350 stage2_flush_pmds(kvm
, pud
, addr
, next
);
352 } while (pud
++, addr
= next
, addr
!= end
);
355 static void stage2_flush_memslot(struct kvm
*kvm
,
356 struct kvm_memory_slot
*memslot
)
358 phys_addr_t addr
= memslot
->base_gfn
<< PAGE_SHIFT
;
359 phys_addr_t end
= addr
+ PAGE_SIZE
* memslot
->npages
;
363 pgd
= kvm
->arch
.pgd
+ kvm_pgd_index(addr
);
365 next
= kvm_pgd_addr_end(addr
, end
);
366 stage2_flush_puds(kvm
, pgd
, addr
, next
);
367 } while (pgd
++, addr
= next
, addr
!= end
);
371 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
372 * @kvm: The struct kvm pointer
374 * Go through the stage 2 page tables and invalidate any cache lines
375 * backing memory already mapped to the VM.
377 static void stage2_flush_vm(struct kvm
*kvm
)
379 struct kvm_memslots
*slots
;
380 struct kvm_memory_slot
*memslot
;
383 idx
= srcu_read_lock(&kvm
->srcu
);
384 spin_lock(&kvm
->mmu_lock
);
386 slots
= kvm_memslots(kvm
);
387 kvm_for_each_memslot(memslot
, slots
)
388 stage2_flush_memslot(kvm
, memslot
);
390 spin_unlock(&kvm
->mmu_lock
);
391 srcu_read_unlock(&kvm
->srcu
, idx
);
395 * free_boot_hyp_pgd - free HYP boot page tables
397 * Free the HYP boot page tables. The bounce page is also freed.
399 void free_boot_hyp_pgd(void)
401 mutex_lock(&kvm_hyp_pgd_mutex
);
404 unmap_range(NULL
, boot_hyp_pgd
, hyp_idmap_start
, PAGE_SIZE
);
405 unmap_range(NULL
, boot_hyp_pgd
, TRAMPOLINE_VA
, PAGE_SIZE
);
406 free_pages((unsigned long)boot_hyp_pgd
, hyp_pgd_order
);
411 unmap_range(NULL
, hyp_pgd
, TRAMPOLINE_VA
, PAGE_SIZE
);
413 mutex_unlock(&kvm_hyp_pgd_mutex
);
417 * free_hyp_pgds - free Hyp-mode page tables
419 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
420 * therefore contains either mappings in the kernel memory area (above
421 * PAGE_OFFSET), or device mappings in the vmalloc range (from
422 * VMALLOC_START to VMALLOC_END).
424 * boot_hyp_pgd should only map two pages for the init code.
426 void free_hyp_pgds(void)
432 mutex_lock(&kvm_hyp_pgd_mutex
);
435 for (addr
= PAGE_OFFSET
; virt_addr_valid(addr
); addr
+= PGDIR_SIZE
)
436 unmap_range(NULL
, hyp_pgd
, KERN_TO_HYP(addr
), PGDIR_SIZE
);
437 for (addr
= VMALLOC_START
; is_vmalloc_addr((void*)addr
); addr
+= PGDIR_SIZE
)
438 unmap_range(NULL
, hyp_pgd
, KERN_TO_HYP(addr
), PGDIR_SIZE
);
440 free_pages((unsigned long)hyp_pgd
, hyp_pgd_order
);
443 if (merged_hyp_pgd
) {
444 clear_page(merged_hyp_pgd
);
445 free_page((unsigned long)merged_hyp_pgd
);
446 merged_hyp_pgd
= NULL
;
449 mutex_unlock(&kvm_hyp_pgd_mutex
);
452 static void create_hyp_pte_mappings(pmd_t
*pmd
, unsigned long start
,
453 unsigned long end
, unsigned long pfn
,
461 pte
= pte_offset_kernel(pmd
, addr
);
462 kvm_set_pte(pte
, pfn_pte(pfn
, prot
));
463 get_page(virt_to_page(pte
));
464 kvm_flush_dcache_to_poc(pte
, sizeof(*pte
));
466 } while (addr
+= PAGE_SIZE
, addr
!= end
);
469 static int create_hyp_pmd_mappings(pud_t
*pud
, unsigned long start
,
470 unsigned long end
, unsigned long pfn
,
475 unsigned long addr
, next
;
479 pmd
= pmd_offset(pud
, addr
);
481 BUG_ON(pmd_sect(*pmd
));
483 if (pmd_none(*pmd
)) {
484 pte
= pte_alloc_one_kernel(NULL
, addr
);
486 kvm_err("Cannot allocate Hyp pte\n");
489 pmd_populate_kernel(NULL
, pmd
, pte
);
490 get_page(virt_to_page(pmd
));
491 kvm_flush_dcache_to_poc(pmd
, sizeof(*pmd
));
494 next
= pmd_addr_end(addr
, end
);
496 create_hyp_pte_mappings(pmd
, addr
, next
, pfn
, prot
);
497 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
498 } while (addr
= next
, addr
!= end
);
503 static int create_hyp_pud_mappings(pgd_t
*pgd
, unsigned long start
,
504 unsigned long end
, unsigned long pfn
,
509 unsigned long addr
, next
;
514 pud
= pud_offset(pgd
, addr
);
516 if (pud_none_or_clear_bad(pud
)) {
517 pmd
= pmd_alloc_one(NULL
, addr
);
519 kvm_err("Cannot allocate Hyp pmd\n");
522 pud_populate(NULL
, pud
, pmd
);
523 get_page(virt_to_page(pud
));
524 kvm_flush_dcache_to_poc(pud
, sizeof(*pud
));
527 next
= pud_addr_end(addr
, end
);
528 ret
= create_hyp_pmd_mappings(pud
, addr
, next
, pfn
, prot
);
531 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
532 } while (addr
= next
, addr
!= end
);
537 static int __create_hyp_mappings(pgd_t
*pgdp
,
538 unsigned long start
, unsigned long end
,
539 unsigned long pfn
, pgprot_t prot
)
543 unsigned long addr
, next
;
546 mutex_lock(&kvm_hyp_pgd_mutex
);
547 addr
= start
& PAGE_MASK
;
548 end
= PAGE_ALIGN(end
);
550 pgd
= pgdp
+ pgd_index(addr
);
552 if (pgd_none(*pgd
)) {
553 pud
= pud_alloc_one(NULL
, addr
);
555 kvm_err("Cannot allocate Hyp pud\n");
559 pgd_populate(NULL
, pgd
, pud
);
560 get_page(virt_to_page(pgd
));
561 kvm_flush_dcache_to_poc(pgd
, sizeof(*pgd
));
564 next
= pgd_addr_end(addr
, end
);
565 err
= create_hyp_pud_mappings(pgd
, addr
, next
, pfn
, prot
);
568 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
569 } while (addr
= next
, addr
!= end
);
571 mutex_unlock(&kvm_hyp_pgd_mutex
);
575 static phys_addr_t
kvm_kaddr_to_phys(void *kaddr
)
577 if (!is_vmalloc_addr(kaddr
)) {
578 BUG_ON(!virt_addr_valid(kaddr
));
581 return page_to_phys(vmalloc_to_page(kaddr
)) +
582 offset_in_page(kaddr
);
587 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
588 * @from: The virtual kernel start address of the range
589 * @to: The virtual kernel end address of the range (exclusive)
591 * The same virtual address as the kernel virtual address is also used
592 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
595 int create_hyp_mappings(void *from
, void *to
)
597 phys_addr_t phys_addr
;
598 unsigned long virt_addr
;
599 unsigned long start
= KERN_TO_HYP((unsigned long)from
);
600 unsigned long end
= KERN_TO_HYP((unsigned long)to
);
602 if (is_kernel_in_hyp_mode())
605 start
= start
& PAGE_MASK
;
606 end
= PAGE_ALIGN(end
);
608 for (virt_addr
= start
; virt_addr
< end
; virt_addr
+= PAGE_SIZE
) {
611 phys_addr
= kvm_kaddr_to_phys(from
+ virt_addr
- start
);
612 err
= __create_hyp_mappings(hyp_pgd
, virt_addr
,
613 virt_addr
+ PAGE_SIZE
,
614 __phys_to_pfn(phys_addr
),
624 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
625 * @from: The kernel start VA of the range
626 * @to: The kernel end VA of the range (exclusive)
627 * @phys_addr: The physical start address which gets mapped
629 * The resulting HYP VA is the same as the kernel VA, modulo
632 int create_hyp_io_mappings(void *from
, void *to
, phys_addr_t phys_addr
)
634 unsigned long start
= KERN_TO_HYP((unsigned long)from
);
635 unsigned long end
= KERN_TO_HYP((unsigned long)to
);
637 if (is_kernel_in_hyp_mode())
640 /* Check for a valid kernel IO mapping */
641 if (!is_vmalloc_addr(from
) || !is_vmalloc_addr(to
- 1))
644 return __create_hyp_mappings(hyp_pgd
, start
, end
,
645 __phys_to_pfn(phys_addr
), PAGE_HYP_DEVICE
);
648 /* Free the HW pgd, one page at a time */
649 static void kvm_free_hwpgd(void *hwpgd
)
651 free_pages_exact(hwpgd
, kvm_get_hwpgd_size());
654 /* Allocate the HW PGD, making sure that each page gets its own refcount */
655 static void *kvm_alloc_hwpgd(void)
657 unsigned int size
= kvm_get_hwpgd_size();
659 return alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
663 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
664 * @kvm: The KVM struct pointer for the VM.
666 * Allocates only the stage-2 HW PGD level table(s) (can support either full
667 * 40-bit input addresses or limited to 32-bit input addresses). Clears the
670 * Note we don't need locking here as this is only called when the VM is
671 * created, which can only be done once.
673 int kvm_alloc_stage2_pgd(struct kvm
*kvm
)
678 if (kvm
->arch
.pgd
!= NULL
) {
679 kvm_err("kvm_arch already initialized?\n");
683 hwpgd
= kvm_alloc_hwpgd();
687 /* When the kernel uses more levels of page tables than the
688 * guest, we allocate a fake PGD and pre-populate it to point
689 * to the next-level page table, which will be the real
690 * initial page table pointed to by the VTTBR.
692 * When KVM_PREALLOC_LEVEL==2, we allocate a single page for
693 * the PMD and the kernel will use folded pud.
694 * When KVM_PREALLOC_LEVEL==1, we allocate 2 consecutive PUD
697 if (KVM_PREALLOC_LEVEL
> 0) {
701 * Allocate fake pgd for the page table manipulation macros to
702 * work. This is not used by the hardware and we have no
703 * alignment requirement for this allocation.
705 pgd
= kmalloc(PTRS_PER_S2_PGD
* sizeof(pgd_t
),
706 GFP_KERNEL
| __GFP_ZERO
);
709 kvm_free_hwpgd(hwpgd
);
713 /* Plug the HW PGD into the fake one. */
714 for (i
= 0; i
< PTRS_PER_S2_PGD
; i
++) {
715 if (KVM_PREALLOC_LEVEL
== 1)
716 pgd_populate(NULL
, pgd
+ i
,
717 (pud_t
*)hwpgd
+ i
* PTRS_PER_PUD
);
718 else if (KVM_PREALLOC_LEVEL
== 2)
719 pud_populate(NULL
, pud_offset(pgd
, 0) + i
,
720 (pmd_t
*)hwpgd
+ i
* PTRS_PER_PMD
);
724 * Allocate actual first-level Stage-2 page table used by the
725 * hardware for Stage-2 page table walks.
727 pgd
= (pgd_t
*)hwpgd
;
736 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
737 * @kvm: The VM pointer
738 * @start: The intermediate physical base address of the range to unmap
739 * @size: The size of the area to unmap
741 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
742 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
743 * destroying the VM), otherwise another faulting VCPU may come in and mess
744 * with things behind our backs.
746 static void unmap_stage2_range(struct kvm
*kvm
, phys_addr_t start
, u64 size
)
748 unmap_range(kvm
, kvm
->arch
.pgd
, start
, size
);
751 static void stage2_unmap_memslot(struct kvm
*kvm
,
752 struct kvm_memory_slot
*memslot
)
754 hva_t hva
= memslot
->userspace_addr
;
755 phys_addr_t addr
= memslot
->base_gfn
<< PAGE_SHIFT
;
756 phys_addr_t size
= PAGE_SIZE
* memslot
->npages
;
757 hva_t reg_end
= hva
+ size
;
760 * A memory region could potentially cover multiple VMAs, and any holes
761 * between them, so iterate over all of them to find out if we should
764 * +--------------------------------------------+
765 * +---------------+----------------+ +----------------+
766 * | : VMA 1 | VMA 2 | | VMA 3 : |
767 * +---------------+----------------+ +----------------+
769 * +--------------------------------------------+
772 struct vm_area_struct
*vma
= find_vma(current
->mm
, hva
);
773 hva_t vm_start
, vm_end
;
775 if (!vma
|| vma
->vm_start
>= reg_end
)
779 * Take the intersection of this VMA with the memory region
781 vm_start
= max(hva
, vma
->vm_start
);
782 vm_end
= min(reg_end
, vma
->vm_end
);
784 if (!(vma
->vm_flags
& VM_PFNMAP
)) {
785 gpa_t gpa
= addr
+ (vm_start
- memslot
->userspace_addr
);
786 unmap_stage2_range(kvm
, gpa
, vm_end
- vm_start
);
789 } while (hva
< reg_end
);
793 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
794 * @kvm: The struct kvm pointer
796 * Go through the memregions and unmap any reguler RAM
797 * backing memory already mapped to the VM.
799 void stage2_unmap_vm(struct kvm
*kvm
)
801 struct kvm_memslots
*slots
;
802 struct kvm_memory_slot
*memslot
;
805 idx
= srcu_read_lock(&kvm
->srcu
);
806 spin_lock(&kvm
->mmu_lock
);
808 slots
= kvm_memslots(kvm
);
809 kvm_for_each_memslot(memslot
, slots
)
810 stage2_unmap_memslot(kvm
, memslot
);
812 spin_unlock(&kvm
->mmu_lock
);
813 srcu_read_unlock(&kvm
->srcu
, idx
);
817 * kvm_free_stage2_pgd - free all stage-2 tables
818 * @kvm: The KVM struct pointer for the VM.
820 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
821 * underlying level-2 and level-3 tables before freeing the actual level-1 table
822 * and setting the struct pointer to NULL.
824 * Note we don't need locking here as this is only called when the VM is
825 * destroyed, which can only be done once.
827 void kvm_free_stage2_pgd(struct kvm
*kvm
)
829 if (kvm
->arch
.pgd
== NULL
)
832 unmap_stage2_range(kvm
, 0, KVM_PHYS_SIZE
);
833 kvm_free_hwpgd(kvm_get_hwpgd(kvm
));
834 if (KVM_PREALLOC_LEVEL
> 0)
835 kfree(kvm
->arch
.pgd
);
837 kvm
->arch
.pgd
= NULL
;
840 static pud_t
*stage2_get_pud(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
846 pgd
= kvm
->arch
.pgd
+ kvm_pgd_index(addr
);
847 if (WARN_ON(pgd_none(*pgd
))) {
850 pud
= mmu_memory_cache_alloc(cache
);
851 pgd_populate(NULL
, pgd
, pud
);
852 get_page(virt_to_page(pgd
));
855 return pud_offset(pgd
, addr
);
858 static pmd_t
*stage2_get_pmd(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
864 pud
= stage2_get_pud(kvm
, cache
, addr
);
865 if (pud_none(*pud
)) {
868 pmd
= mmu_memory_cache_alloc(cache
);
869 pud_populate(NULL
, pud
, pmd
);
870 get_page(virt_to_page(pud
));
873 return pmd_offset(pud
, addr
);
876 static int stage2_set_pmd_huge(struct kvm
*kvm
, struct kvm_mmu_memory_cache
877 *cache
, phys_addr_t addr
, const pmd_t
*new_pmd
)
881 pmd
= stage2_get_pmd(kvm
, cache
, addr
);
885 * Mapping in huge pages should only happen through a fault. If a
886 * page is merged into a transparent huge page, the individual
887 * subpages of that huge page should be unmapped through MMU
888 * notifiers before we get here.
890 * Merging of CompoundPages is not supported; they should become
891 * splitting first, unmapped, merged, and mapped back in on-demand.
893 VM_BUG_ON(pmd_present(*pmd
) && pmd_pfn(*pmd
) != pmd_pfn(*new_pmd
));
896 kvm_set_pmd(pmd
, *new_pmd
);
897 if (pmd_present(old_pmd
))
898 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
900 get_page(virt_to_page(pmd
));
904 static int stage2_set_pte(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
905 phys_addr_t addr
, const pte_t
*new_pte
,
910 bool iomap
= flags
& KVM_S2PTE_FLAG_IS_IOMAP
;
911 bool logging_active
= flags
& KVM_S2_FLAG_LOGGING_ACTIVE
;
913 VM_BUG_ON(logging_active
&& !cache
);
915 /* Create stage-2 page table mapping - Levels 0 and 1 */
916 pmd
= stage2_get_pmd(kvm
, cache
, addr
);
919 * Ignore calls from kvm_set_spte_hva for unallocated
926 * While dirty page logging - dissolve huge PMD, then continue on to
930 stage2_dissolve_pmd(kvm
, addr
, pmd
);
932 /* Create stage-2 page mappings - Level 2 */
933 if (pmd_none(*pmd
)) {
935 return 0; /* ignore calls from kvm_set_spte_hva */
936 pte
= mmu_memory_cache_alloc(cache
);
938 pmd_populate_kernel(NULL
, pmd
, pte
);
939 get_page(virt_to_page(pmd
));
942 pte
= pte_offset_kernel(pmd
, addr
);
944 if (iomap
&& pte_present(*pte
))
947 /* Create 2nd stage page table mapping - Level 3 */
949 kvm_set_pte(pte
, *new_pte
);
950 if (pte_present(old_pte
))
951 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
953 get_page(virt_to_page(pte
));
959 * kvm_phys_addr_ioremap - map a device range to guest IPA
961 * @kvm: The KVM pointer
962 * @guest_ipa: The IPA at which to insert the mapping
963 * @pa: The physical address of the device
964 * @size: The size of the mapping
966 int kvm_phys_addr_ioremap(struct kvm
*kvm
, phys_addr_t guest_ipa
,
967 phys_addr_t pa
, unsigned long size
, bool writable
)
969 phys_addr_t addr
, end
;
972 struct kvm_mmu_memory_cache cache
= { 0, };
974 end
= (guest_ipa
+ size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
975 pfn
= __phys_to_pfn(pa
);
977 for (addr
= guest_ipa
; addr
< end
; addr
+= PAGE_SIZE
) {
978 pte_t pte
= pfn_pte(pfn
, PAGE_S2_DEVICE
);
981 kvm_set_s2pte_writable(&pte
);
983 ret
= mmu_topup_memory_cache(&cache
, KVM_MMU_CACHE_MIN_PAGES
,
987 spin_lock(&kvm
->mmu_lock
);
988 ret
= stage2_set_pte(kvm
, &cache
, addr
, &pte
,
989 KVM_S2PTE_FLAG_IS_IOMAP
);
990 spin_unlock(&kvm
->mmu_lock
);
998 mmu_free_memory_cache(&cache
);
1002 static bool transparent_hugepage_adjust(kvm_pfn_t
*pfnp
, phys_addr_t
*ipap
)
1004 kvm_pfn_t pfn
= *pfnp
;
1005 gfn_t gfn
= *ipap
>> PAGE_SHIFT
;
1007 if (PageTransCompoundMap(pfn_to_page(pfn
))) {
1010 * The address we faulted on is backed by a transparent huge
1011 * page. However, because we map the compound huge page and
1012 * not the individual tail page, we need to transfer the
1013 * refcount to the head page. We have to be careful that the
1014 * THP doesn't start to split while we are adjusting the
1017 * We are sure this doesn't happen, because mmu_notifier_retry
1018 * was successful and we are holding the mmu_lock, so if this
1019 * THP is trying to split, it will be blocked in the mmu
1020 * notifier before touching any of the pages, specifically
1021 * before being able to call __split_huge_page_refcount().
1023 * We can therefore safely transfer the refcount from PG_tail
1024 * to PG_head and switch the pfn from a tail page to the head
1027 mask
= PTRS_PER_PMD
- 1;
1028 VM_BUG_ON((gfn
& mask
) != (pfn
& mask
));
1031 kvm_release_pfn_clean(pfn
);
1043 static bool kvm_is_write_fault(struct kvm_vcpu
*vcpu
)
1045 if (kvm_vcpu_trap_is_iabt(vcpu
))
1048 return kvm_vcpu_dabt_iswrite(vcpu
);
1052 * stage2_wp_ptes - write protect PMD range
1053 * @pmd: pointer to pmd entry
1054 * @addr: range start address
1055 * @end: range end address
1057 static void stage2_wp_ptes(pmd_t
*pmd
, phys_addr_t addr
, phys_addr_t end
)
1061 pte
= pte_offset_kernel(pmd
, addr
);
1063 if (!pte_none(*pte
)) {
1064 if (!kvm_s2pte_readonly(pte
))
1065 kvm_set_s2pte_readonly(pte
);
1067 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1071 * stage2_wp_pmds - write protect PUD range
1072 * @pud: pointer to pud entry
1073 * @addr: range start address
1074 * @end: range end address
1076 static void stage2_wp_pmds(pud_t
*pud
, phys_addr_t addr
, phys_addr_t end
)
1081 pmd
= pmd_offset(pud
, addr
);
1084 next
= kvm_pmd_addr_end(addr
, end
);
1085 if (!pmd_none(*pmd
)) {
1086 if (kvm_pmd_huge(*pmd
)) {
1087 if (!kvm_s2pmd_readonly(pmd
))
1088 kvm_set_s2pmd_readonly(pmd
);
1090 stage2_wp_ptes(pmd
, addr
, next
);
1093 } while (pmd
++, addr
= next
, addr
!= end
);
1097 * stage2_wp_puds - write protect PGD range
1098 * @pgd: pointer to pgd entry
1099 * @addr: range start address
1100 * @end: range end address
1102 * Process PUD entries, for a huge PUD we cause a panic.
1104 static void stage2_wp_puds(pgd_t
*pgd
, phys_addr_t addr
, phys_addr_t end
)
1109 pud
= pud_offset(pgd
, addr
);
1111 next
= kvm_pud_addr_end(addr
, end
);
1112 if (!pud_none(*pud
)) {
1113 /* TODO:PUD not supported, revisit later if supported */
1114 BUG_ON(kvm_pud_huge(*pud
));
1115 stage2_wp_pmds(pud
, addr
, next
);
1117 } while (pud
++, addr
= next
, addr
!= end
);
1121 * stage2_wp_range() - write protect stage2 memory region range
1122 * @kvm: The KVM pointer
1123 * @addr: Start address of range
1124 * @end: End address of range
1126 static void stage2_wp_range(struct kvm
*kvm
, phys_addr_t addr
, phys_addr_t end
)
1131 pgd
= kvm
->arch
.pgd
+ kvm_pgd_index(addr
);
1134 * Release kvm_mmu_lock periodically if the memory region is
1135 * large. Otherwise, we may see kernel panics with
1136 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1137 * CONFIG_LOCKDEP. Additionally, holding the lock too long
1138 * will also starve other vCPUs.
1140 if (need_resched() || spin_needbreak(&kvm
->mmu_lock
))
1141 cond_resched_lock(&kvm
->mmu_lock
);
1143 next
= kvm_pgd_addr_end(addr
, end
);
1144 if (pgd_present(*pgd
))
1145 stage2_wp_puds(pgd
, addr
, next
);
1146 } while (pgd
++, addr
= next
, addr
!= end
);
1150 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1151 * @kvm: The KVM pointer
1152 * @slot: The memory slot to write protect
1154 * Called to start logging dirty pages after memory region
1155 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1156 * all present PMD and PTEs are write protected in the memory region.
1157 * Afterwards read of dirty page log can be called.
1159 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1160 * serializing operations for VM memory regions.
1162 void kvm_mmu_wp_memory_region(struct kvm
*kvm
, int slot
)
1164 struct kvm_memslots
*slots
= kvm_memslots(kvm
);
1165 struct kvm_memory_slot
*memslot
= id_to_memslot(slots
, slot
);
1166 phys_addr_t start
= memslot
->base_gfn
<< PAGE_SHIFT
;
1167 phys_addr_t end
= (memslot
->base_gfn
+ memslot
->npages
) << PAGE_SHIFT
;
1169 spin_lock(&kvm
->mmu_lock
);
1170 stage2_wp_range(kvm
, start
, end
);
1171 spin_unlock(&kvm
->mmu_lock
);
1172 kvm_flush_remote_tlbs(kvm
);
1176 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
1177 * @kvm: The KVM pointer
1178 * @slot: The memory slot associated with mask
1179 * @gfn_offset: The gfn offset in memory slot
1180 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1181 * slot to be write protected
1183 * Walks bits set in mask write protects the associated pte's. Caller must
1184 * acquire kvm_mmu_lock.
1186 static void kvm_mmu_write_protect_pt_masked(struct kvm
*kvm
,
1187 struct kvm_memory_slot
*slot
,
1188 gfn_t gfn_offset
, unsigned long mask
)
1190 phys_addr_t base_gfn
= slot
->base_gfn
+ gfn_offset
;
1191 phys_addr_t start
= (base_gfn
+ __ffs(mask
)) << PAGE_SHIFT
;
1192 phys_addr_t end
= (base_gfn
+ __fls(mask
) + 1) << PAGE_SHIFT
;
1194 stage2_wp_range(kvm
, start
, end
);
1198 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1201 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1202 * enable dirty logging for them.
1204 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm
*kvm
,
1205 struct kvm_memory_slot
*slot
,
1206 gfn_t gfn_offset
, unsigned long mask
)
1208 kvm_mmu_write_protect_pt_masked(kvm
, slot
, gfn_offset
, mask
);
1211 static void coherent_cache_guest_page(struct kvm_vcpu
*vcpu
, kvm_pfn_t pfn
,
1212 unsigned long size
, bool uncached
)
1214 __coherent_cache_guest_page(vcpu
, pfn
, size
, uncached
);
1217 static int user_mem_abort(struct kvm_vcpu
*vcpu
, phys_addr_t fault_ipa
,
1218 struct kvm_memory_slot
*memslot
, unsigned long hva
,
1219 unsigned long fault_status
)
1222 bool write_fault
, writable
, hugetlb
= false, force_pte
= false;
1223 unsigned long mmu_seq
;
1224 gfn_t gfn
= fault_ipa
>> PAGE_SHIFT
;
1225 struct kvm
*kvm
= vcpu
->kvm
;
1226 struct kvm_mmu_memory_cache
*memcache
= &vcpu
->arch
.mmu_page_cache
;
1227 struct vm_area_struct
*vma
;
1229 pgprot_t mem_type
= PAGE_S2
;
1230 bool fault_ipa_uncached
;
1231 bool logging_active
= memslot_is_logging(memslot
);
1232 unsigned long flags
= 0;
1234 write_fault
= kvm_is_write_fault(vcpu
);
1235 if (fault_status
== FSC_PERM
&& !write_fault
) {
1236 kvm_err("Unexpected L2 read permission error\n");
1240 /* Let's check if we will get back a huge page backed by hugetlbfs */
1241 down_read(¤t
->mm
->mmap_sem
);
1242 vma
= find_vma_intersection(current
->mm
, hva
, hva
+ 1);
1243 if (unlikely(!vma
)) {
1244 kvm_err("Failed to find VMA for hva 0x%lx\n", hva
);
1245 up_read(¤t
->mm
->mmap_sem
);
1249 if (is_vm_hugetlb_page(vma
) && !logging_active
) {
1251 gfn
= (fault_ipa
& PMD_MASK
) >> PAGE_SHIFT
;
1254 * Pages belonging to memslots that don't have the same
1255 * alignment for userspace and IPA cannot be mapped using
1256 * block descriptors even if the pages belong to a THP for
1257 * the process, because the stage-2 block descriptor will
1258 * cover more than a single THP and we loose atomicity for
1259 * unmapping, updates, and splits of the THP or other pages
1260 * in the stage-2 block range.
1262 if ((memslot
->userspace_addr
& ~PMD_MASK
) !=
1263 ((memslot
->base_gfn
<< PAGE_SHIFT
) & ~PMD_MASK
))
1266 up_read(¤t
->mm
->mmap_sem
);
1268 /* We need minimum second+third level pages */
1269 ret
= mmu_topup_memory_cache(memcache
, KVM_MMU_CACHE_MIN_PAGES
,
1274 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1276 * Ensure the read of mmu_notifier_seq happens before we call
1277 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1278 * the page we just got a reference to gets unmapped before we have a
1279 * chance to grab the mmu_lock, which ensure that if the page gets
1280 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1281 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1282 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1286 pfn
= gfn_to_pfn_prot(kvm
, gfn
, write_fault
, &writable
);
1287 if (is_error_pfn(pfn
))
1290 if (kvm_is_device_pfn(pfn
)) {
1291 mem_type
= PAGE_S2_DEVICE
;
1292 flags
|= KVM_S2PTE_FLAG_IS_IOMAP
;
1293 } else if (logging_active
) {
1295 * Faults on pages in a memslot with logging enabled
1296 * should not be mapped with huge pages (it introduces churn
1297 * and performance degradation), so force a pte mapping.
1300 flags
|= KVM_S2_FLAG_LOGGING_ACTIVE
;
1303 * Only actually map the page as writable if this was a write
1310 spin_lock(&kvm
->mmu_lock
);
1311 if (mmu_notifier_retry(kvm
, mmu_seq
))
1314 if (!hugetlb
&& !force_pte
)
1315 hugetlb
= transparent_hugepage_adjust(&pfn
, &fault_ipa
);
1317 fault_ipa_uncached
= memslot
->flags
& KVM_MEMSLOT_INCOHERENT
;
1320 pmd_t new_pmd
= pfn_pmd(pfn
, mem_type
);
1321 new_pmd
= pmd_mkhuge(new_pmd
);
1323 kvm_set_s2pmd_writable(&new_pmd
);
1324 kvm_set_pfn_dirty(pfn
);
1326 coherent_cache_guest_page(vcpu
, pfn
, PMD_SIZE
, fault_ipa_uncached
);
1327 ret
= stage2_set_pmd_huge(kvm
, memcache
, fault_ipa
, &new_pmd
);
1329 pte_t new_pte
= pfn_pte(pfn
, mem_type
);
1332 kvm_set_s2pte_writable(&new_pte
);
1333 kvm_set_pfn_dirty(pfn
);
1334 mark_page_dirty(kvm
, gfn
);
1336 coherent_cache_guest_page(vcpu
, pfn
, PAGE_SIZE
, fault_ipa_uncached
);
1337 ret
= stage2_set_pte(kvm
, memcache
, fault_ipa
, &new_pte
, flags
);
1341 spin_unlock(&kvm
->mmu_lock
);
1342 kvm_set_pfn_accessed(pfn
);
1343 kvm_release_pfn_clean(pfn
);
1348 * Resolve the access fault by making the page young again.
1349 * Note that because the faulting entry is guaranteed not to be
1350 * cached in the TLB, we don't need to invalidate anything.
1352 static void handle_access_fault(struct kvm_vcpu
*vcpu
, phys_addr_t fault_ipa
)
1357 bool pfn_valid
= false;
1359 trace_kvm_access_fault(fault_ipa
);
1361 spin_lock(&vcpu
->kvm
->mmu_lock
);
1363 pmd
= stage2_get_pmd(vcpu
->kvm
, NULL
, fault_ipa
);
1364 if (!pmd
|| pmd_none(*pmd
)) /* Nothing there */
1367 if (kvm_pmd_huge(*pmd
)) { /* THP, HugeTLB */
1368 *pmd
= pmd_mkyoung(*pmd
);
1369 pfn
= pmd_pfn(*pmd
);
1374 pte
= pte_offset_kernel(pmd
, fault_ipa
);
1375 if (pte_none(*pte
)) /* Nothing there either */
1378 *pte
= pte_mkyoung(*pte
); /* Just a page... */
1379 pfn
= pte_pfn(*pte
);
1382 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1384 kvm_set_pfn_accessed(pfn
);
1388 * kvm_handle_guest_abort - handles all 2nd stage aborts
1389 * @vcpu: the VCPU pointer
1390 * @run: the kvm_run structure
1392 * Any abort that gets to the host is almost guaranteed to be caused by a
1393 * missing second stage translation table entry, which can mean that either the
1394 * guest simply needs more memory and we must allocate an appropriate page or it
1395 * can mean that the guest tried to access I/O memory, which is emulated by user
1396 * space. The distinction is based on the IPA causing the fault and whether this
1397 * memory region has been registered as standard RAM by user space.
1399 int kvm_handle_guest_abort(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1401 unsigned long fault_status
;
1402 phys_addr_t fault_ipa
;
1403 struct kvm_memory_slot
*memslot
;
1405 bool is_iabt
, write_fault
, writable
;
1409 is_iabt
= kvm_vcpu_trap_is_iabt(vcpu
);
1410 fault_ipa
= kvm_vcpu_get_fault_ipa(vcpu
);
1412 trace_kvm_guest_fault(*vcpu_pc(vcpu
), kvm_vcpu_get_hsr(vcpu
),
1413 kvm_vcpu_get_hfar(vcpu
), fault_ipa
);
1415 /* Check the stage-2 fault is trans. fault or write fault */
1416 fault_status
= kvm_vcpu_trap_get_fault_type(vcpu
);
1417 if (fault_status
!= FSC_FAULT
&& fault_status
!= FSC_PERM
&&
1418 fault_status
!= FSC_ACCESS
) {
1419 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1420 kvm_vcpu_trap_get_class(vcpu
),
1421 (unsigned long)kvm_vcpu_trap_get_fault(vcpu
),
1422 (unsigned long)kvm_vcpu_get_hsr(vcpu
));
1426 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1428 gfn
= fault_ipa
>> PAGE_SHIFT
;
1429 memslot
= gfn_to_memslot(vcpu
->kvm
, gfn
);
1430 hva
= gfn_to_hva_memslot_prot(memslot
, gfn
, &writable
);
1431 write_fault
= kvm_is_write_fault(vcpu
);
1432 if (kvm_is_error_hva(hva
) || (write_fault
&& !writable
)) {
1434 /* Prefetch Abort on I/O address */
1435 kvm_inject_pabt(vcpu
, kvm_vcpu_get_hfar(vcpu
));
1441 * Check for a cache maintenance operation. Since we
1442 * ended-up here, we know it is outside of any memory
1443 * slot. But we can't find out if that is for a device,
1444 * or if the guest is just being stupid. The only thing
1445 * we know for sure is that this range cannot be cached.
1447 * So let's assume that the guest is just being
1448 * cautious, and skip the instruction.
1450 if (kvm_vcpu_dabt_is_cm(vcpu
)) {
1451 kvm_skip_instr(vcpu
, kvm_vcpu_trap_il_is32bit(vcpu
));
1457 * The IPA is reported as [MAX:12], so we need to
1458 * complement it with the bottom 12 bits from the
1459 * faulting VA. This is always 12 bits, irrespective
1462 fault_ipa
|= kvm_vcpu_get_hfar(vcpu
) & ((1 << 12) - 1);
1463 ret
= io_mem_abort(vcpu
, run
, fault_ipa
);
1467 /* Userspace should not be able to register out-of-bounds IPAs */
1468 VM_BUG_ON(fault_ipa
>= KVM_PHYS_SIZE
);
1470 if (fault_status
== FSC_ACCESS
) {
1471 handle_access_fault(vcpu
, fault_ipa
);
1476 ret
= user_mem_abort(vcpu
, fault_ipa
, memslot
, hva
, fault_status
);
1480 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1484 static int handle_hva_to_gpa(struct kvm
*kvm
,
1485 unsigned long start
,
1487 int (*handler
)(struct kvm
*kvm
,
1488 gpa_t gpa
, void *data
),
1491 struct kvm_memslots
*slots
;
1492 struct kvm_memory_slot
*memslot
;
1495 slots
= kvm_memslots(kvm
);
1497 /* we only care about the pages that the guest sees */
1498 kvm_for_each_memslot(memslot
, slots
) {
1499 unsigned long hva_start
, hva_end
;
1502 hva_start
= max(start
, memslot
->userspace_addr
);
1503 hva_end
= min(end
, memslot
->userspace_addr
+
1504 (memslot
->npages
<< PAGE_SHIFT
));
1505 if (hva_start
>= hva_end
)
1509 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1510 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1512 gfn
= hva_to_gfn_memslot(hva_start
, memslot
);
1513 gfn_end
= hva_to_gfn_memslot(hva_end
+ PAGE_SIZE
- 1, memslot
);
1515 for (; gfn
< gfn_end
; ++gfn
) {
1516 gpa_t gpa
= gfn
<< PAGE_SHIFT
;
1517 ret
|= handler(kvm
, gpa
, data
);
1524 static int kvm_unmap_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1526 unmap_stage2_range(kvm
, gpa
, PAGE_SIZE
);
1530 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
1532 unsigned long end
= hva
+ PAGE_SIZE
;
1537 trace_kvm_unmap_hva(hva
);
1538 handle_hva_to_gpa(kvm
, hva
, end
, &kvm_unmap_hva_handler
, NULL
);
1542 int kvm_unmap_hva_range(struct kvm
*kvm
,
1543 unsigned long start
, unsigned long end
)
1548 trace_kvm_unmap_hva_range(start
, end
);
1549 handle_hva_to_gpa(kvm
, start
, end
, &kvm_unmap_hva_handler
, NULL
);
1553 static int kvm_set_spte_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1555 pte_t
*pte
= (pte_t
*)data
;
1558 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1559 * flag clear because MMU notifiers will have unmapped a huge PMD before
1560 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1561 * therefore stage2_set_pte() never needs to clear out a huge PMD
1562 * through this calling path.
1564 stage2_set_pte(kvm
, NULL
, gpa
, pte
, 0);
1569 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
1571 unsigned long end
= hva
+ PAGE_SIZE
;
1577 trace_kvm_set_spte_hva(hva
);
1578 stage2_pte
= pfn_pte(pte_pfn(pte
), PAGE_S2
);
1579 handle_hva_to_gpa(kvm
, hva
, end
, &kvm_set_spte_handler
, &stage2_pte
);
1582 static int kvm_age_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1587 pmd
= stage2_get_pmd(kvm
, NULL
, gpa
);
1588 if (!pmd
|| pmd_none(*pmd
)) /* Nothing there */
1591 if (kvm_pmd_huge(*pmd
)) { /* THP, HugeTLB */
1592 if (pmd_young(*pmd
)) {
1593 *pmd
= pmd_mkold(*pmd
);
1600 pte
= pte_offset_kernel(pmd
, gpa
);
1604 if (pte_young(*pte
)) {
1605 *pte
= pte_mkold(*pte
); /* Just a page... */
1612 static int kvm_test_age_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1617 pmd
= stage2_get_pmd(kvm
, NULL
, gpa
);
1618 if (!pmd
|| pmd_none(*pmd
)) /* Nothing there */
1621 if (kvm_pmd_huge(*pmd
)) /* THP, HugeTLB */
1622 return pmd_young(*pmd
);
1624 pte
= pte_offset_kernel(pmd
, gpa
);
1625 if (!pte_none(*pte
)) /* Just a page... */
1626 return pte_young(*pte
);
1631 int kvm_age_hva(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
1633 trace_kvm_age_hva(start
, end
);
1634 return handle_hva_to_gpa(kvm
, start
, end
, kvm_age_hva_handler
, NULL
);
1637 int kvm_test_age_hva(struct kvm
*kvm
, unsigned long hva
)
1639 trace_kvm_test_age_hva(hva
);
1640 return handle_hva_to_gpa(kvm
, hva
, hva
, kvm_test_age_hva_handler
, NULL
);
1643 void kvm_mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
1645 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_cache
);
1648 phys_addr_t
kvm_mmu_get_httbr(void)
1650 if (__kvm_cpu_uses_extended_idmap())
1651 return virt_to_phys(merged_hyp_pgd
);
1653 return virt_to_phys(hyp_pgd
);
1656 phys_addr_t
kvm_mmu_get_boot_httbr(void)
1658 if (__kvm_cpu_uses_extended_idmap())
1659 return virt_to_phys(merged_hyp_pgd
);
1661 return virt_to_phys(boot_hyp_pgd
);
1664 phys_addr_t
kvm_get_idmap_vector(void)
1666 return hyp_idmap_vector
;
1669 phys_addr_t
kvm_get_idmap_start(void)
1671 return hyp_idmap_start
;
1674 int kvm_mmu_init(void)
1678 hyp_idmap_start
= kvm_virt_to_phys(__hyp_idmap_text_start
);
1679 hyp_idmap_end
= kvm_virt_to_phys(__hyp_idmap_text_end
);
1680 hyp_idmap_vector
= kvm_virt_to_phys(__kvm_hyp_init
);
1683 * We rely on the linker script to ensure at build time that the HYP
1684 * init code does not cross a page boundary.
1686 BUG_ON((hyp_idmap_start
^ (hyp_idmap_end
- 1)) & PAGE_MASK
);
1688 hyp_pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, hyp_pgd_order
);
1689 boot_hyp_pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, hyp_pgd_order
);
1691 if (!hyp_pgd
|| !boot_hyp_pgd
) {
1692 kvm_err("Hyp mode PGD not allocated\n");
1697 /* Create the idmap in the boot page tables */
1698 err
= __create_hyp_mappings(boot_hyp_pgd
,
1699 hyp_idmap_start
, hyp_idmap_end
,
1700 __phys_to_pfn(hyp_idmap_start
),
1704 kvm_err("Failed to idmap %lx-%lx\n",
1705 hyp_idmap_start
, hyp_idmap_end
);
1709 if (__kvm_cpu_uses_extended_idmap()) {
1710 merged_hyp_pgd
= (pgd_t
*)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
1711 if (!merged_hyp_pgd
) {
1712 kvm_err("Failed to allocate extra HYP pgd\n");
1715 __kvm_extend_hypmap(boot_hyp_pgd
, hyp_pgd
, merged_hyp_pgd
,
1720 /* Map the very same page at the trampoline VA */
1721 err
= __create_hyp_mappings(boot_hyp_pgd
,
1722 TRAMPOLINE_VA
, TRAMPOLINE_VA
+ PAGE_SIZE
,
1723 __phys_to_pfn(hyp_idmap_start
),
1726 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1731 /* Map the same page again into the runtime page tables */
1732 err
= __create_hyp_mappings(hyp_pgd
,
1733 TRAMPOLINE_VA
, TRAMPOLINE_VA
+ PAGE_SIZE
,
1734 __phys_to_pfn(hyp_idmap_start
),
1737 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1748 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
1749 const struct kvm_userspace_memory_region
*mem
,
1750 const struct kvm_memory_slot
*old
,
1751 const struct kvm_memory_slot
*new,
1752 enum kvm_mr_change change
)
1755 * At this point memslot has been committed and there is an
1756 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1757 * memory slot is write protected.
1759 if (change
!= KVM_MR_DELETE
&& mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
)
1760 kvm_mmu_wp_memory_region(kvm
, mem
->slot
);
1763 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
1764 struct kvm_memory_slot
*memslot
,
1765 const struct kvm_userspace_memory_region
*mem
,
1766 enum kvm_mr_change change
)
1768 hva_t hva
= mem
->userspace_addr
;
1769 hva_t reg_end
= hva
+ mem
->memory_size
;
1770 bool writable
= !(mem
->flags
& KVM_MEM_READONLY
);
1773 if (change
!= KVM_MR_CREATE
&& change
!= KVM_MR_MOVE
&&
1774 change
!= KVM_MR_FLAGS_ONLY
)
1778 * Prevent userspace from creating a memory region outside of the IPA
1779 * space addressable by the KVM guest IPA space.
1781 if (memslot
->base_gfn
+ memslot
->npages
>=
1782 (KVM_PHYS_SIZE
>> PAGE_SHIFT
))
1786 * A memory region could potentially cover multiple VMAs, and any holes
1787 * between them, so iterate over all of them to find out if we can map
1788 * any of them right now.
1790 * +--------------------------------------------+
1791 * +---------------+----------------+ +----------------+
1792 * | : VMA 1 | VMA 2 | | VMA 3 : |
1793 * +---------------+----------------+ +----------------+
1795 * +--------------------------------------------+
1798 struct vm_area_struct
*vma
= find_vma(current
->mm
, hva
);
1799 hva_t vm_start
, vm_end
;
1801 if (!vma
|| vma
->vm_start
>= reg_end
)
1805 * Mapping a read-only VMA is only allowed if the
1806 * memory region is configured as read-only.
1808 if (writable
&& !(vma
->vm_flags
& VM_WRITE
)) {
1814 * Take the intersection of this VMA with the memory region
1816 vm_start
= max(hva
, vma
->vm_start
);
1817 vm_end
= min(reg_end
, vma
->vm_end
);
1819 if (vma
->vm_flags
& VM_PFNMAP
) {
1820 gpa_t gpa
= mem
->guest_phys_addr
+
1821 (vm_start
- mem
->userspace_addr
);
1824 pa
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
1825 pa
+= vm_start
- vma
->vm_start
;
1827 /* IO region dirty page logging not allowed */
1828 if (memslot
->flags
& KVM_MEM_LOG_DIRTY_PAGES
)
1831 ret
= kvm_phys_addr_ioremap(kvm
, gpa
, pa
,
1838 } while (hva
< reg_end
);
1840 if (change
== KVM_MR_FLAGS_ONLY
)
1843 spin_lock(&kvm
->mmu_lock
);
1845 unmap_stage2_range(kvm
, mem
->guest_phys_addr
, mem
->memory_size
);
1847 stage2_flush_memslot(kvm
, memslot
);
1848 spin_unlock(&kvm
->mmu_lock
);
1852 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
1853 struct kvm_memory_slot
*dont
)
1857 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
1858 unsigned long npages
)
1861 * Readonly memslots are not incoherent with the caches by definition,
1862 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1863 * that the guest may consider devices and hence map as uncached.
1864 * To prevent incoherency issues in these cases, tag all readonly
1865 * regions as incoherent.
1867 if (slot
->flags
& KVM_MEM_READONLY
)
1868 slot
->flags
|= KVM_MEMSLOT_INCOHERENT
;
1872 void kvm_arch_memslots_updated(struct kvm
*kvm
, struct kvm_memslots
*slots
)
1876 void kvm_arch_flush_shadow_all(struct kvm
*kvm
)
1880 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
1881 struct kvm_memory_slot
*slot
)
1883 gpa_t gpa
= slot
->base_gfn
<< PAGE_SHIFT
;
1884 phys_addr_t size
= slot
->npages
<< PAGE_SHIFT
;
1886 spin_lock(&kvm
->mmu_lock
);
1887 unmap_stage2_range(kvm
, gpa
, size
);
1888 spin_unlock(&kvm
->mmu_lock
);
1892 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1895 * - S/W ops are local to a CPU (not broadcast)
1896 * - We have line migration behind our back (speculation)
1897 * - System caches don't support S/W at all (damn!)
1899 * In the face of the above, the best we can do is to try and convert
1900 * S/W ops to VA ops. Because the guest is not allowed to infer the
1901 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1902 * which is a rather good thing for us.
1904 * Also, it is only used when turning caches on/off ("The expected
1905 * usage of the cache maintenance instructions that operate by set/way
1906 * is associated with the cache maintenance instructions associated
1907 * with the powerdown and powerup of caches, if this is required by
1908 * the implementation.").
1910 * We use the following policy:
1912 * - If we trap a S/W operation, we enable VM trapping to detect
1913 * caches being turned on/off, and do a full clean.
1915 * - We flush the caches on both caches being turned on and off.
1917 * - Once the caches are enabled, we stop trapping VM ops.
1919 void kvm_set_way_flush(struct kvm_vcpu
*vcpu
)
1921 unsigned long hcr
= vcpu_get_hcr(vcpu
);
1924 * If this is the first time we do a S/W operation
1925 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1928 * Otherwise, rely on the VM trapping to wait for the MMU +
1929 * Caches to be turned off. At that point, we'll be able to
1930 * clean the caches again.
1932 if (!(hcr
& HCR_TVM
)) {
1933 trace_kvm_set_way_flush(*vcpu_pc(vcpu
),
1934 vcpu_has_cache_enabled(vcpu
));
1935 stage2_flush_vm(vcpu
->kvm
);
1936 vcpu_set_hcr(vcpu
, hcr
| HCR_TVM
);
1940 void kvm_toggle_cache(struct kvm_vcpu
*vcpu
, bool was_enabled
)
1942 bool now_enabled
= vcpu_has_cache_enabled(vcpu
);
1945 * If switching the MMU+caches on, need to invalidate the caches.
1946 * If switching it off, need to clean the caches.
1947 * Clean + invalidate does the trick always.
1949 if (now_enabled
!= was_enabled
)
1950 stage2_flush_vm(vcpu
->kvm
);
1952 /* Caches are now on, stop trapping VM ops (until a S/W op) */
1954 vcpu_set_hcr(vcpu
, vcpu_get_hcr(vcpu
) & ~HCR_TVM
);
1956 trace_kvm_toggle_cache(*vcpu_pc(vcpu
), was_enabled
, now_enabled
);