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>
34 extern char __hyp_idmap_text_start
[], __hyp_idmap_text_end
[];
36 static pgd_t
*boot_hyp_pgd
;
37 static pgd_t
*hyp_pgd
;
38 static pgd_t
*merged_hyp_pgd
;
39 static DEFINE_MUTEX(kvm_hyp_pgd_mutex
);
41 static unsigned long hyp_idmap_start
;
42 static unsigned long hyp_idmap_end
;
43 static phys_addr_t hyp_idmap_vector
;
45 #define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
47 #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
48 #define kvm_pud_huge(_x) pud_huge(_x)
50 #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
51 #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
53 static bool memslot_is_logging(struct kvm_memory_slot
*memslot
)
55 return memslot
->dirty_bitmap
&& !(memslot
->flags
& KVM_MEM_READONLY
);
59 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
60 * @kvm: pointer to kvm structure.
62 * Interface to HYP function to flush all VM TLB entries
64 void kvm_flush_remote_tlbs(struct kvm
*kvm
)
66 kvm_call_hyp(__kvm_tlb_flush_vmid
, kvm
);
69 static void kvm_tlb_flush_vmid_ipa(struct kvm
*kvm
, phys_addr_t ipa
)
72 * This function also gets called when dealing with HYP page
73 * tables. As HYP doesn't have an associated struct kvm (and
74 * the HYP page tables are fairly static), we don't do
78 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa
, kvm
, ipa
);
82 * D-Cache management functions. They take the page table entries by
83 * value, as they are flushing the cache using the kernel mapping (or
86 static void kvm_flush_dcache_pte(pte_t pte
)
88 __kvm_flush_dcache_pte(pte
);
91 static void kvm_flush_dcache_pmd(pmd_t pmd
)
93 __kvm_flush_dcache_pmd(pmd
);
96 static void kvm_flush_dcache_pud(pud_t pud
)
98 __kvm_flush_dcache_pud(pud
);
101 static bool kvm_is_device_pfn(unsigned long pfn
)
103 return !pfn_valid(pfn
);
107 * stage2_dissolve_pmd() - clear and flush huge PMD entry
108 * @kvm: pointer to kvm structure.
110 * @pmd: pmd pointer for IPA
112 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
113 * pages in the range dirty.
115 static void stage2_dissolve_pmd(struct kvm
*kvm
, phys_addr_t addr
, pmd_t
*pmd
)
117 if (!kvm_pmd_huge(*pmd
))
121 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
122 put_page(virt_to_page(pmd
));
125 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
130 BUG_ON(max
> KVM_NR_MEM_OBJS
);
131 if (cache
->nobjs
>= min
)
133 while (cache
->nobjs
< max
) {
134 page
= (void *)__get_free_page(PGALLOC_GFP
);
137 cache
->objects
[cache
->nobjs
++] = page
;
142 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
145 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
148 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
)
152 BUG_ON(!mc
|| !mc
->nobjs
);
153 p
= mc
->objects
[--mc
->nobjs
];
157 static void clear_pgd_entry(struct kvm
*kvm
, pgd_t
*pgd
, phys_addr_t addr
)
159 pud_t
*pud_table __maybe_unused
= pud_offset(pgd
, 0);
161 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
162 pud_free(NULL
, pud_table
);
163 put_page(virt_to_page(pgd
));
166 static void clear_pud_entry(struct kvm
*kvm
, pud_t
*pud
, phys_addr_t addr
)
168 pmd_t
*pmd_table
= pmd_offset(pud
, 0);
169 VM_BUG_ON(pud_huge(*pud
));
171 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
172 pmd_free(NULL
, pmd_table
);
173 put_page(virt_to_page(pud
));
176 static void clear_pmd_entry(struct kvm
*kvm
, pmd_t
*pmd
, phys_addr_t addr
)
178 pte_t
*pte_table
= pte_offset_kernel(pmd
, 0);
179 VM_BUG_ON(kvm_pmd_huge(*pmd
));
181 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
182 pte_free_kernel(NULL
, pte_table
);
183 put_page(virt_to_page(pmd
));
187 * Unmapping vs dcache management:
189 * If a guest maps certain memory pages as uncached, all writes will
190 * bypass the data cache and go directly to RAM. However, the CPUs
191 * can still speculate reads (not writes) and fill cache lines with
194 * Those cache lines will be *clean* cache lines though, so a
195 * clean+invalidate operation is equivalent to an invalidate
196 * operation, because no cache lines are marked dirty.
198 * Those clean cache lines could be filled prior to an uncached write
199 * by the guest, and the cache coherent IO subsystem would therefore
200 * end up writing old data to disk.
202 * This is why right after unmapping a page/section and invalidating
203 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
204 * the IO subsystem will never hit in the cache.
206 static void unmap_ptes(struct kvm
*kvm
, pmd_t
*pmd
,
207 phys_addr_t addr
, phys_addr_t end
)
209 phys_addr_t start_addr
= addr
;
210 pte_t
*pte
, *start_pte
;
212 start_pte
= pte
= pte_offset_kernel(pmd
, addr
);
214 if (!pte_none(*pte
)) {
215 pte_t old_pte
= *pte
;
217 kvm_set_pte(pte
, __pte(0));
218 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
220 /* No need to invalidate the cache for device mappings */
221 if (!kvm_is_device_pfn(pte_pfn(old_pte
)))
222 kvm_flush_dcache_pte(old_pte
);
224 put_page(virt_to_page(pte
));
226 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
228 if (kvm_pte_table_empty(kvm
, start_pte
))
229 clear_pmd_entry(kvm
, pmd
, start_addr
);
232 static void unmap_pmds(struct kvm
*kvm
, pud_t
*pud
,
233 phys_addr_t addr
, phys_addr_t end
)
235 phys_addr_t next
, start_addr
= addr
;
236 pmd_t
*pmd
, *start_pmd
;
238 start_pmd
= pmd
= pmd_offset(pud
, addr
);
240 next
= kvm_pmd_addr_end(addr
, end
);
241 if (!pmd_none(*pmd
)) {
242 if (kvm_pmd_huge(*pmd
)) {
243 pmd_t old_pmd
= *pmd
;
246 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
248 kvm_flush_dcache_pmd(old_pmd
);
250 put_page(virt_to_page(pmd
));
252 unmap_ptes(kvm
, pmd
, addr
, next
);
255 } while (pmd
++, addr
= next
, addr
!= end
);
257 if (kvm_pmd_table_empty(kvm
, start_pmd
))
258 clear_pud_entry(kvm
, pud
, start_addr
);
261 static void unmap_puds(struct kvm
*kvm
, pgd_t
*pgd
,
262 phys_addr_t addr
, phys_addr_t end
)
264 phys_addr_t next
, start_addr
= addr
;
265 pud_t
*pud
, *start_pud
;
267 start_pud
= pud
= pud_offset(pgd
, addr
);
269 next
= kvm_pud_addr_end(addr
, end
);
270 if (!pud_none(*pud
)) {
271 if (pud_huge(*pud
)) {
272 pud_t old_pud
= *pud
;
275 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
277 kvm_flush_dcache_pud(old_pud
);
279 put_page(virt_to_page(pud
));
281 unmap_pmds(kvm
, pud
, addr
, next
);
284 } while (pud
++, addr
= next
, addr
!= end
);
286 if (kvm_pud_table_empty(kvm
, start_pud
))
287 clear_pgd_entry(kvm
, pgd
, start_addr
);
291 static void unmap_range(struct kvm
*kvm
, pgd_t
*pgdp
,
292 phys_addr_t start
, u64 size
)
295 phys_addr_t addr
= start
, end
= start
+ size
;
298 pgd
= pgdp
+ kvm_pgd_index(addr
);
300 next
= kvm_pgd_addr_end(addr
, end
);
302 unmap_puds(kvm
, pgd
, addr
, next
);
303 } while (pgd
++, addr
= next
, addr
!= end
);
306 static void stage2_flush_ptes(struct kvm
*kvm
, pmd_t
*pmd
,
307 phys_addr_t addr
, phys_addr_t end
)
311 pte
= pte_offset_kernel(pmd
, addr
);
313 if (!pte_none(*pte
) && !kvm_is_device_pfn(pte_pfn(*pte
)))
314 kvm_flush_dcache_pte(*pte
);
315 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
318 static void stage2_flush_pmds(struct kvm
*kvm
, pud_t
*pud
,
319 phys_addr_t addr
, phys_addr_t end
)
324 pmd
= pmd_offset(pud
, addr
);
326 next
= kvm_pmd_addr_end(addr
, end
);
327 if (!pmd_none(*pmd
)) {
328 if (kvm_pmd_huge(*pmd
))
329 kvm_flush_dcache_pmd(*pmd
);
331 stage2_flush_ptes(kvm
, pmd
, addr
, next
);
333 } while (pmd
++, addr
= next
, addr
!= end
);
336 static void stage2_flush_puds(struct kvm
*kvm
, pgd_t
*pgd
,
337 phys_addr_t addr
, phys_addr_t end
)
342 pud
= pud_offset(pgd
, addr
);
344 next
= kvm_pud_addr_end(addr
, end
);
345 if (!pud_none(*pud
)) {
347 kvm_flush_dcache_pud(*pud
);
349 stage2_flush_pmds(kvm
, pud
, addr
, next
);
351 } while (pud
++, addr
= next
, addr
!= end
);
354 static void stage2_flush_memslot(struct kvm
*kvm
,
355 struct kvm_memory_slot
*memslot
)
357 phys_addr_t addr
= memslot
->base_gfn
<< PAGE_SHIFT
;
358 phys_addr_t end
= addr
+ PAGE_SIZE
* memslot
->npages
;
362 pgd
= kvm
->arch
.pgd
+ kvm_pgd_index(addr
);
364 next
= kvm_pgd_addr_end(addr
, end
);
365 stage2_flush_puds(kvm
, pgd
, addr
, next
);
366 } while (pgd
++, addr
= next
, addr
!= end
);
370 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
371 * @kvm: The struct kvm pointer
373 * Go through the stage 2 page tables and invalidate any cache lines
374 * backing memory already mapped to the VM.
376 static void stage2_flush_vm(struct kvm
*kvm
)
378 struct kvm_memslots
*slots
;
379 struct kvm_memory_slot
*memslot
;
382 idx
= srcu_read_lock(&kvm
->srcu
);
383 spin_lock(&kvm
->mmu_lock
);
385 slots
= kvm_memslots(kvm
);
386 kvm_for_each_memslot(memslot
, slots
)
387 stage2_flush_memslot(kvm
, memslot
);
389 spin_unlock(&kvm
->mmu_lock
);
390 srcu_read_unlock(&kvm
->srcu
, idx
);
394 * free_boot_hyp_pgd - free HYP boot page tables
396 * Free the HYP boot page tables. The bounce page is also freed.
398 void free_boot_hyp_pgd(void)
400 mutex_lock(&kvm_hyp_pgd_mutex
);
403 unmap_range(NULL
, boot_hyp_pgd
, hyp_idmap_start
, PAGE_SIZE
);
404 unmap_range(NULL
, boot_hyp_pgd
, TRAMPOLINE_VA
, PAGE_SIZE
);
405 free_pages((unsigned long)boot_hyp_pgd
, hyp_pgd_order
);
410 unmap_range(NULL
, hyp_pgd
, TRAMPOLINE_VA
, PAGE_SIZE
);
412 mutex_unlock(&kvm_hyp_pgd_mutex
);
416 * free_hyp_pgds - free Hyp-mode page tables
418 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
419 * therefore contains either mappings in the kernel memory area (above
420 * PAGE_OFFSET), or device mappings in the vmalloc range (from
421 * VMALLOC_START to VMALLOC_END).
423 * boot_hyp_pgd should only map two pages for the init code.
425 void free_hyp_pgds(void)
431 mutex_lock(&kvm_hyp_pgd_mutex
);
434 for (addr
= PAGE_OFFSET
; virt_addr_valid(addr
); addr
+= PGDIR_SIZE
)
435 unmap_range(NULL
, hyp_pgd
, KERN_TO_HYP(addr
), PGDIR_SIZE
);
436 for (addr
= VMALLOC_START
; is_vmalloc_addr((void*)addr
); addr
+= PGDIR_SIZE
)
437 unmap_range(NULL
, hyp_pgd
, KERN_TO_HYP(addr
), PGDIR_SIZE
);
439 free_pages((unsigned long)hyp_pgd
, hyp_pgd_order
);
442 if (merged_hyp_pgd
) {
443 clear_page(merged_hyp_pgd
);
444 free_page((unsigned long)merged_hyp_pgd
);
445 merged_hyp_pgd
= NULL
;
448 mutex_unlock(&kvm_hyp_pgd_mutex
);
451 static void create_hyp_pte_mappings(pmd_t
*pmd
, unsigned long start
,
452 unsigned long end
, unsigned long pfn
,
460 pte
= pte_offset_kernel(pmd
, addr
);
461 kvm_set_pte(pte
, pfn_pte(pfn
, prot
));
462 get_page(virt_to_page(pte
));
463 kvm_flush_dcache_to_poc(pte
, sizeof(*pte
));
465 } while (addr
+= PAGE_SIZE
, addr
!= end
);
468 static int create_hyp_pmd_mappings(pud_t
*pud
, unsigned long start
,
469 unsigned long end
, unsigned long pfn
,
474 unsigned long addr
, next
;
478 pmd
= pmd_offset(pud
, addr
);
480 BUG_ON(pmd_sect(*pmd
));
482 if (pmd_none(*pmd
)) {
483 pte
= pte_alloc_one_kernel(NULL
, addr
);
485 kvm_err("Cannot allocate Hyp pte\n");
488 pmd_populate_kernel(NULL
, pmd
, pte
);
489 get_page(virt_to_page(pmd
));
490 kvm_flush_dcache_to_poc(pmd
, sizeof(*pmd
));
493 next
= pmd_addr_end(addr
, end
);
495 create_hyp_pte_mappings(pmd
, addr
, next
, pfn
, prot
);
496 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
497 } while (addr
= next
, addr
!= end
);
502 static int create_hyp_pud_mappings(pgd_t
*pgd
, unsigned long start
,
503 unsigned long end
, unsigned long pfn
,
508 unsigned long addr
, next
;
513 pud
= pud_offset(pgd
, addr
);
515 if (pud_none_or_clear_bad(pud
)) {
516 pmd
= pmd_alloc_one(NULL
, addr
);
518 kvm_err("Cannot allocate Hyp pmd\n");
521 pud_populate(NULL
, pud
, pmd
);
522 get_page(virt_to_page(pud
));
523 kvm_flush_dcache_to_poc(pud
, sizeof(*pud
));
526 next
= pud_addr_end(addr
, end
);
527 ret
= create_hyp_pmd_mappings(pud
, addr
, next
, pfn
, prot
);
530 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
531 } while (addr
= next
, addr
!= end
);
536 static int __create_hyp_mappings(pgd_t
*pgdp
,
537 unsigned long start
, unsigned long end
,
538 unsigned long pfn
, pgprot_t prot
)
542 unsigned long addr
, next
;
545 mutex_lock(&kvm_hyp_pgd_mutex
);
546 addr
= start
& PAGE_MASK
;
547 end
= PAGE_ALIGN(end
);
549 pgd
= pgdp
+ pgd_index(addr
);
551 if (pgd_none(*pgd
)) {
552 pud
= pud_alloc_one(NULL
, addr
);
554 kvm_err("Cannot allocate Hyp pud\n");
558 pgd_populate(NULL
, pgd
, pud
);
559 get_page(virt_to_page(pgd
));
560 kvm_flush_dcache_to_poc(pgd
, sizeof(*pgd
));
563 next
= pgd_addr_end(addr
, end
);
564 err
= create_hyp_pud_mappings(pgd
, addr
, next
, pfn
, prot
);
567 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
568 } while (addr
= next
, addr
!= end
);
570 mutex_unlock(&kvm_hyp_pgd_mutex
);
574 static phys_addr_t
kvm_kaddr_to_phys(void *kaddr
)
576 if (!is_vmalloc_addr(kaddr
)) {
577 BUG_ON(!virt_addr_valid(kaddr
));
580 return page_to_phys(vmalloc_to_page(kaddr
)) +
581 offset_in_page(kaddr
);
586 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
587 * @from: The virtual kernel start address of the range
588 * @to: The virtual kernel end address of the range (exclusive)
590 * The same virtual address as the kernel virtual address is also used
591 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
594 int create_hyp_mappings(void *from
, void *to
)
596 phys_addr_t phys_addr
;
597 unsigned long virt_addr
;
598 unsigned long start
= KERN_TO_HYP((unsigned long)from
);
599 unsigned long end
= KERN_TO_HYP((unsigned long)to
);
601 start
= start
& PAGE_MASK
;
602 end
= PAGE_ALIGN(end
);
604 for (virt_addr
= start
; virt_addr
< end
; virt_addr
+= PAGE_SIZE
) {
607 phys_addr
= kvm_kaddr_to_phys(from
+ virt_addr
- start
);
608 err
= __create_hyp_mappings(hyp_pgd
, virt_addr
,
609 virt_addr
+ PAGE_SIZE
,
610 __phys_to_pfn(phys_addr
),
620 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
621 * @from: The kernel start VA of the range
622 * @to: The kernel end VA of the range (exclusive)
623 * @phys_addr: The physical start address which gets mapped
625 * The resulting HYP VA is the same as the kernel VA, modulo
628 int create_hyp_io_mappings(void *from
, void *to
, phys_addr_t phys_addr
)
630 unsigned long start
= KERN_TO_HYP((unsigned long)from
);
631 unsigned long end
= KERN_TO_HYP((unsigned long)to
);
633 /* Check for a valid kernel IO mapping */
634 if (!is_vmalloc_addr(from
) || !is_vmalloc_addr(to
- 1))
637 return __create_hyp_mappings(hyp_pgd
, start
, end
,
638 __phys_to_pfn(phys_addr
), PAGE_HYP_DEVICE
);
641 /* Free the HW pgd, one page at a time */
642 static void kvm_free_hwpgd(void *hwpgd
)
644 free_pages_exact(hwpgd
, kvm_get_hwpgd_size());
647 /* Allocate the HW PGD, making sure that each page gets its own refcount */
648 static void *kvm_alloc_hwpgd(void)
650 unsigned int size
= kvm_get_hwpgd_size();
652 return alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
656 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
657 * @kvm: The KVM struct pointer for the VM.
659 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
660 * support either full 40-bit input addresses or limited to 32-bit input
661 * addresses). Clears the allocated pages.
663 * Note we don't need locking here as this is only called when the VM is
664 * created, which can only be done once.
666 int kvm_alloc_stage2_pgd(struct kvm
*kvm
)
671 if (kvm
->arch
.pgd
!= NULL
) {
672 kvm_err("kvm_arch already initialized?\n");
676 hwpgd
= kvm_alloc_hwpgd();
680 /* When the kernel uses more levels of page tables than the
681 * guest, we allocate a fake PGD and pre-populate it to point
682 * to the next-level page table, which will be the real
683 * initial page table pointed to by the VTTBR.
685 * When KVM_PREALLOC_LEVEL==2, we allocate a single page for
686 * the PMD and the kernel will use folded pud.
687 * When KVM_PREALLOC_LEVEL==1, we allocate 2 consecutive PUD
690 if (KVM_PREALLOC_LEVEL
> 0) {
694 * Allocate fake pgd for the page table manipulation macros to
695 * work. This is not used by the hardware and we have no
696 * alignment requirement for this allocation.
698 pgd
= (pgd_t
*)kmalloc(PTRS_PER_S2_PGD
* sizeof(pgd_t
),
699 GFP_KERNEL
| __GFP_ZERO
);
702 kvm_free_hwpgd(hwpgd
);
706 /* Plug the HW PGD into the fake one. */
707 for (i
= 0; i
< PTRS_PER_S2_PGD
; i
++) {
708 if (KVM_PREALLOC_LEVEL
== 1)
709 pgd_populate(NULL
, pgd
+ i
,
710 (pud_t
*)hwpgd
+ i
* PTRS_PER_PUD
);
711 else if (KVM_PREALLOC_LEVEL
== 2)
712 pud_populate(NULL
, pud_offset(pgd
, 0) + i
,
713 (pmd_t
*)hwpgd
+ i
* PTRS_PER_PMD
);
717 * Allocate actual first-level Stage-2 page table used by the
718 * hardware for Stage-2 page table walks.
720 pgd
= (pgd_t
*)hwpgd
;
729 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
730 * @kvm: The VM pointer
731 * @start: The intermediate physical base address of the range to unmap
732 * @size: The size of the area to unmap
734 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
735 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
736 * destroying the VM), otherwise another faulting VCPU may come in and mess
737 * with things behind our backs.
739 static void unmap_stage2_range(struct kvm
*kvm
, phys_addr_t start
, u64 size
)
741 unmap_range(kvm
, kvm
->arch
.pgd
, start
, size
);
744 static void stage2_unmap_memslot(struct kvm
*kvm
,
745 struct kvm_memory_slot
*memslot
)
747 hva_t hva
= memslot
->userspace_addr
;
748 phys_addr_t addr
= memslot
->base_gfn
<< PAGE_SHIFT
;
749 phys_addr_t size
= PAGE_SIZE
* memslot
->npages
;
750 hva_t reg_end
= hva
+ size
;
753 * A memory region could potentially cover multiple VMAs, and any holes
754 * between them, so iterate over all of them to find out if we should
757 * +--------------------------------------------+
758 * +---------------+----------------+ +----------------+
759 * | : VMA 1 | VMA 2 | | VMA 3 : |
760 * +---------------+----------------+ +----------------+
762 * +--------------------------------------------+
765 struct vm_area_struct
*vma
= find_vma(current
->mm
, hva
);
766 hva_t vm_start
, vm_end
;
768 if (!vma
|| vma
->vm_start
>= reg_end
)
772 * Take the intersection of this VMA with the memory region
774 vm_start
= max(hva
, vma
->vm_start
);
775 vm_end
= min(reg_end
, vma
->vm_end
);
777 if (!(vma
->vm_flags
& VM_PFNMAP
)) {
778 gpa_t gpa
= addr
+ (vm_start
- memslot
->userspace_addr
);
779 unmap_stage2_range(kvm
, gpa
, vm_end
- vm_start
);
782 } while (hva
< reg_end
);
786 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
787 * @kvm: The struct kvm pointer
789 * Go through the memregions and unmap any reguler RAM
790 * backing memory already mapped to the VM.
792 void stage2_unmap_vm(struct kvm
*kvm
)
794 struct kvm_memslots
*slots
;
795 struct kvm_memory_slot
*memslot
;
798 idx
= srcu_read_lock(&kvm
->srcu
);
799 spin_lock(&kvm
->mmu_lock
);
801 slots
= kvm_memslots(kvm
);
802 kvm_for_each_memslot(memslot
, slots
)
803 stage2_unmap_memslot(kvm
, memslot
);
805 spin_unlock(&kvm
->mmu_lock
);
806 srcu_read_unlock(&kvm
->srcu
, idx
);
810 * kvm_free_stage2_pgd - free all stage-2 tables
811 * @kvm: The KVM struct pointer for the VM.
813 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
814 * underlying level-2 and level-3 tables before freeing the actual level-1 table
815 * and setting the struct pointer to NULL.
817 * Note we don't need locking here as this is only called when the VM is
818 * destroyed, which can only be done once.
820 void kvm_free_stage2_pgd(struct kvm
*kvm
)
822 if (kvm
->arch
.pgd
== NULL
)
825 unmap_stage2_range(kvm
, 0, KVM_PHYS_SIZE
);
826 kvm_free_hwpgd(kvm_get_hwpgd(kvm
));
827 if (KVM_PREALLOC_LEVEL
> 0)
828 kfree(kvm
->arch
.pgd
);
830 kvm
->arch
.pgd
= NULL
;
833 static pud_t
*stage2_get_pud(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
839 pgd
= kvm
->arch
.pgd
+ kvm_pgd_index(addr
);
840 if (WARN_ON(pgd_none(*pgd
))) {
843 pud
= mmu_memory_cache_alloc(cache
);
844 pgd_populate(NULL
, pgd
, pud
);
845 get_page(virt_to_page(pgd
));
848 return pud_offset(pgd
, addr
);
851 static pmd_t
*stage2_get_pmd(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
857 pud
= stage2_get_pud(kvm
, cache
, addr
);
858 if (pud_none(*pud
)) {
861 pmd
= mmu_memory_cache_alloc(cache
);
862 pud_populate(NULL
, pud
, pmd
);
863 get_page(virt_to_page(pud
));
866 return pmd_offset(pud
, addr
);
869 static int stage2_set_pmd_huge(struct kvm
*kvm
, struct kvm_mmu_memory_cache
870 *cache
, phys_addr_t addr
, const pmd_t
*new_pmd
)
874 pmd
= stage2_get_pmd(kvm
, cache
, addr
);
878 * Mapping in huge pages should only happen through a fault. If a
879 * page is merged into a transparent huge page, the individual
880 * subpages of that huge page should be unmapped through MMU
881 * notifiers before we get here.
883 * Merging of CompoundPages is not supported; they should become
884 * splitting first, unmapped, merged, and mapped back in on-demand.
886 VM_BUG_ON(pmd_present(*pmd
) && pmd_pfn(*pmd
) != pmd_pfn(*new_pmd
));
889 if (pmd_present(old_pmd
)) {
891 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
893 get_page(virt_to_page(pmd
));
896 kvm_set_pmd(pmd
, *new_pmd
);
900 static int stage2_set_pte(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
901 phys_addr_t addr
, const pte_t
*new_pte
,
906 bool iomap
= flags
& KVM_S2PTE_FLAG_IS_IOMAP
;
907 bool logging_active
= flags
& KVM_S2_FLAG_LOGGING_ACTIVE
;
909 VM_BUG_ON(logging_active
&& !cache
);
911 /* Create stage-2 page table mapping - Levels 0 and 1 */
912 pmd
= stage2_get_pmd(kvm
, cache
, addr
);
915 * Ignore calls from kvm_set_spte_hva for unallocated
922 * While dirty page logging - dissolve huge PMD, then continue on to
926 stage2_dissolve_pmd(kvm
, addr
, pmd
);
928 /* Create stage-2 page mappings - Level 2 */
929 if (pmd_none(*pmd
)) {
931 return 0; /* ignore calls from kvm_set_spte_hva */
932 pte
= mmu_memory_cache_alloc(cache
);
934 pmd_populate_kernel(NULL
, pmd
, pte
);
935 get_page(virt_to_page(pmd
));
938 pte
= pte_offset_kernel(pmd
, addr
);
940 if (iomap
&& pte_present(*pte
))
943 /* Create 2nd stage page table mapping - Level 3 */
945 if (pte_present(old_pte
)) {
946 kvm_set_pte(pte
, __pte(0));
947 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
949 get_page(virt_to_page(pte
));
952 kvm_set_pte(pte
, *new_pte
);
957 * kvm_phys_addr_ioremap - map a device range to guest IPA
959 * @kvm: The KVM pointer
960 * @guest_ipa: The IPA at which to insert the mapping
961 * @pa: The physical address of the device
962 * @size: The size of the mapping
964 int kvm_phys_addr_ioremap(struct kvm
*kvm
, phys_addr_t guest_ipa
,
965 phys_addr_t pa
, unsigned long size
, bool writable
)
967 phys_addr_t addr
, end
;
970 struct kvm_mmu_memory_cache cache
= { 0, };
972 end
= (guest_ipa
+ size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
973 pfn
= __phys_to_pfn(pa
);
975 for (addr
= guest_ipa
; addr
< end
; addr
+= PAGE_SIZE
) {
976 pte_t pte
= pfn_pte(pfn
, PAGE_S2_DEVICE
);
979 kvm_set_s2pte_writable(&pte
);
981 ret
= mmu_topup_memory_cache(&cache
, KVM_MMU_CACHE_MIN_PAGES
,
985 spin_lock(&kvm
->mmu_lock
);
986 ret
= stage2_set_pte(kvm
, &cache
, addr
, &pte
,
987 KVM_S2PTE_FLAG_IS_IOMAP
);
988 spin_unlock(&kvm
->mmu_lock
);
996 mmu_free_memory_cache(&cache
);
1000 static bool transparent_hugepage_adjust(pfn_t
*pfnp
, phys_addr_t
*ipap
)
1003 gfn_t gfn
= *ipap
>> PAGE_SHIFT
;
1005 if (PageTransCompound(pfn_to_page(pfn
))) {
1008 * The address we faulted on is backed by a transparent huge
1009 * page. However, because we map the compound huge page and
1010 * not the individual tail page, we need to transfer the
1011 * refcount to the head page. We have to be careful that the
1012 * THP doesn't start to split while we are adjusting the
1015 * We are sure this doesn't happen, because mmu_notifier_retry
1016 * was successful and we are holding the mmu_lock, so if this
1017 * THP is trying to split, it will be blocked in the mmu
1018 * notifier before touching any of the pages, specifically
1019 * before being able to call __split_huge_page_refcount().
1021 * We can therefore safely transfer the refcount from PG_tail
1022 * to PG_head and switch the pfn from a tail page to the head
1025 mask
= PTRS_PER_PMD
- 1;
1026 VM_BUG_ON((gfn
& mask
) != (pfn
& mask
));
1029 kvm_release_pfn_clean(pfn
);
1041 static bool kvm_is_write_fault(struct kvm_vcpu
*vcpu
)
1043 if (kvm_vcpu_trap_is_iabt(vcpu
))
1046 return kvm_vcpu_dabt_iswrite(vcpu
);
1050 * stage2_wp_ptes - write protect PMD range
1051 * @pmd: pointer to pmd entry
1052 * @addr: range start address
1053 * @end: range end address
1055 static void stage2_wp_ptes(pmd_t
*pmd
, phys_addr_t addr
, phys_addr_t end
)
1059 pte
= pte_offset_kernel(pmd
, addr
);
1061 if (!pte_none(*pte
)) {
1062 if (!kvm_s2pte_readonly(pte
))
1063 kvm_set_s2pte_readonly(pte
);
1065 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1069 * stage2_wp_pmds - write protect PUD range
1070 * @pud: pointer to pud entry
1071 * @addr: range start address
1072 * @end: range end address
1074 static void stage2_wp_pmds(pud_t
*pud
, phys_addr_t addr
, phys_addr_t end
)
1079 pmd
= pmd_offset(pud
, addr
);
1082 next
= kvm_pmd_addr_end(addr
, end
);
1083 if (!pmd_none(*pmd
)) {
1084 if (kvm_pmd_huge(*pmd
)) {
1085 if (!kvm_s2pmd_readonly(pmd
))
1086 kvm_set_s2pmd_readonly(pmd
);
1088 stage2_wp_ptes(pmd
, addr
, next
);
1091 } while (pmd
++, addr
= next
, addr
!= end
);
1095 * stage2_wp_puds - write protect PGD range
1096 * @pgd: pointer to pgd entry
1097 * @addr: range start address
1098 * @end: range end address
1100 * Process PUD entries, for a huge PUD we cause a panic.
1102 static void stage2_wp_puds(pgd_t
*pgd
, phys_addr_t addr
, phys_addr_t end
)
1107 pud
= pud_offset(pgd
, addr
);
1109 next
= kvm_pud_addr_end(addr
, end
);
1110 if (!pud_none(*pud
)) {
1111 /* TODO:PUD not supported, revisit later if supported */
1112 BUG_ON(kvm_pud_huge(*pud
));
1113 stage2_wp_pmds(pud
, addr
, next
);
1115 } while (pud
++, addr
= next
, addr
!= end
);
1119 * stage2_wp_range() - write protect stage2 memory region range
1120 * @kvm: The KVM pointer
1121 * @addr: Start address of range
1122 * @end: End address of range
1124 static void stage2_wp_range(struct kvm
*kvm
, phys_addr_t addr
, phys_addr_t end
)
1129 pgd
= kvm
->arch
.pgd
+ kvm_pgd_index(addr
);
1132 * Release kvm_mmu_lock periodically if the memory region is
1133 * large. Otherwise, we may see kernel panics with
1134 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1135 * CONFIG_LOCKDEP. Additionally, holding the lock too long
1136 * will also starve other vCPUs.
1138 if (need_resched() || spin_needbreak(&kvm
->mmu_lock
))
1139 cond_resched_lock(&kvm
->mmu_lock
);
1141 next
= kvm_pgd_addr_end(addr
, end
);
1142 if (pgd_present(*pgd
))
1143 stage2_wp_puds(pgd
, addr
, next
);
1144 } while (pgd
++, addr
= next
, addr
!= end
);
1148 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1149 * @kvm: The KVM pointer
1150 * @slot: The memory slot to write protect
1152 * Called to start logging dirty pages after memory region
1153 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1154 * all present PMD and PTEs are write protected in the memory region.
1155 * Afterwards read of dirty page log can be called.
1157 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1158 * serializing operations for VM memory regions.
1160 void kvm_mmu_wp_memory_region(struct kvm
*kvm
, int slot
)
1162 struct kvm_memory_slot
*memslot
= id_to_memslot(kvm
->memslots
, slot
);
1163 phys_addr_t start
= memslot
->base_gfn
<< PAGE_SHIFT
;
1164 phys_addr_t end
= (memslot
->base_gfn
+ memslot
->npages
) << PAGE_SHIFT
;
1166 spin_lock(&kvm
->mmu_lock
);
1167 stage2_wp_range(kvm
, start
, end
);
1168 spin_unlock(&kvm
->mmu_lock
);
1169 kvm_flush_remote_tlbs(kvm
);
1173 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
1174 * @kvm: The KVM pointer
1175 * @slot: The memory slot associated with mask
1176 * @gfn_offset: The gfn offset in memory slot
1177 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1178 * slot to be write protected
1180 * Walks bits set in mask write protects the associated pte's. Caller must
1181 * acquire kvm_mmu_lock.
1183 static void kvm_mmu_write_protect_pt_masked(struct kvm
*kvm
,
1184 struct kvm_memory_slot
*slot
,
1185 gfn_t gfn_offset
, unsigned long mask
)
1187 phys_addr_t base_gfn
= slot
->base_gfn
+ gfn_offset
;
1188 phys_addr_t start
= (base_gfn
+ __ffs(mask
)) << PAGE_SHIFT
;
1189 phys_addr_t end
= (base_gfn
+ __fls(mask
) + 1) << PAGE_SHIFT
;
1191 stage2_wp_range(kvm
, start
, end
);
1195 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1198 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1199 * enable dirty logging for them.
1201 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm
*kvm
,
1202 struct kvm_memory_slot
*slot
,
1203 gfn_t gfn_offset
, unsigned long mask
)
1205 kvm_mmu_write_protect_pt_masked(kvm
, slot
, gfn_offset
, mask
);
1208 static void coherent_cache_guest_page(struct kvm_vcpu
*vcpu
, pfn_t pfn
,
1209 unsigned long size
, bool uncached
)
1211 __coherent_cache_guest_page(vcpu
, pfn
, size
, uncached
);
1214 static int user_mem_abort(struct kvm_vcpu
*vcpu
, phys_addr_t fault_ipa
,
1215 struct kvm_memory_slot
*memslot
, unsigned long hva
,
1216 unsigned long fault_status
)
1219 bool write_fault
, writable
, hugetlb
= false, force_pte
= false;
1220 unsigned long mmu_seq
;
1221 gfn_t gfn
= fault_ipa
>> PAGE_SHIFT
;
1222 struct kvm
*kvm
= vcpu
->kvm
;
1223 struct kvm_mmu_memory_cache
*memcache
= &vcpu
->arch
.mmu_page_cache
;
1224 struct vm_area_struct
*vma
;
1226 pgprot_t mem_type
= PAGE_S2
;
1227 bool fault_ipa_uncached
;
1228 bool logging_active
= memslot_is_logging(memslot
);
1229 unsigned long flags
= 0;
1231 write_fault
= kvm_is_write_fault(vcpu
);
1232 if (fault_status
== FSC_PERM
&& !write_fault
) {
1233 kvm_err("Unexpected L2 read permission error\n");
1237 /* Let's check if we will get back a huge page backed by hugetlbfs */
1238 down_read(¤t
->mm
->mmap_sem
);
1239 vma
= find_vma_intersection(current
->mm
, hva
, hva
+ 1);
1240 if (unlikely(!vma
)) {
1241 kvm_err("Failed to find VMA for hva 0x%lx\n", hva
);
1242 up_read(¤t
->mm
->mmap_sem
);
1246 if (is_vm_hugetlb_page(vma
) && !logging_active
) {
1248 gfn
= (fault_ipa
& PMD_MASK
) >> PAGE_SHIFT
;
1251 * Pages belonging to memslots that don't have the same
1252 * alignment for userspace and IPA cannot be mapped using
1253 * block descriptors even if the pages belong to a THP for
1254 * the process, because the stage-2 block descriptor will
1255 * cover more than a single THP and we loose atomicity for
1256 * unmapping, updates, and splits of the THP or other pages
1257 * in the stage-2 block range.
1259 if ((memslot
->userspace_addr
& ~PMD_MASK
) !=
1260 ((memslot
->base_gfn
<< PAGE_SHIFT
) & ~PMD_MASK
))
1263 up_read(¤t
->mm
->mmap_sem
);
1265 /* We need minimum second+third level pages */
1266 ret
= mmu_topup_memory_cache(memcache
, KVM_MMU_CACHE_MIN_PAGES
,
1271 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
1273 * Ensure the read of mmu_notifier_seq happens before we call
1274 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1275 * the page we just got a reference to gets unmapped before we have a
1276 * chance to grab the mmu_lock, which ensure that if the page gets
1277 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1278 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1279 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1283 pfn
= gfn_to_pfn_prot(kvm
, gfn
, write_fault
, &writable
);
1284 if (is_error_pfn(pfn
))
1287 if (kvm_is_device_pfn(pfn
)) {
1288 mem_type
= PAGE_S2_DEVICE
;
1289 flags
|= KVM_S2PTE_FLAG_IS_IOMAP
;
1290 } else if (logging_active
) {
1292 * Faults on pages in a memslot with logging enabled
1293 * should not be mapped with huge pages (it introduces churn
1294 * and performance degradation), so force a pte mapping.
1297 flags
|= KVM_S2_FLAG_LOGGING_ACTIVE
;
1300 * Only actually map the page as writable if this was a write
1307 spin_lock(&kvm
->mmu_lock
);
1308 if (mmu_notifier_retry(kvm
, mmu_seq
))
1311 if (!hugetlb
&& !force_pte
)
1312 hugetlb
= transparent_hugepage_adjust(&pfn
, &fault_ipa
);
1314 fault_ipa_uncached
= memslot
->flags
& KVM_MEMSLOT_INCOHERENT
;
1317 pmd_t new_pmd
= pfn_pmd(pfn
, mem_type
);
1318 new_pmd
= pmd_mkhuge(new_pmd
);
1320 kvm_set_s2pmd_writable(&new_pmd
);
1321 kvm_set_pfn_dirty(pfn
);
1323 coherent_cache_guest_page(vcpu
, pfn
, PMD_SIZE
, fault_ipa_uncached
);
1324 ret
= stage2_set_pmd_huge(kvm
, memcache
, fault_ipa
, &new_pmd
);
1326 pte_t new_pte
= pfn_pte(pfn
, mem_type
);
1329 kvm_set_s2pte_writable(&new_pte
);
1330 kvm_set_pfn_dirty(pfn
);
1331 mark_page_dirty(kvm
, gfn
);
1333 coherent_cache_guest_page(vcpu
, pfn
, PAGE_SIZE
, fault_ipa_uncached
);
1334 ret
= stage2_set_pte(kvm
, memcache
, fault_ipa
, &new_pte
, flags
);
1338 spin_unlock(&kvm
->mmu_lock
);
1339 kvm_set_pfn_accessed(pfn
);
1340 kvm_release_pfn_clean(pfn
);
1345 * Resolve the access fault by making the page young again.
1346 * Note that because the faulting entry is guaranteed not to be
1347 * cached in the TLB, we don't need to invalidate anything.
1349 static void handle_access_fault(struct kvm_vcpu
*vcpu
, phys_addr_t fault_ipa
)
1354 bool pfn_valid
= false;
1356 trace_kvm_access_fault(fault_ipa
);
1358 spin_lock(&vcpu
->kvm
->mmu_lock
);
1360 pmd
= stage2_get_pmd(vcpu
->kvm
, NULL
, fault_ipa
);
1361 if (!pmd
|| pmd_none(*pmd
)) /* Nothing there */
1364 if (kvm_pmd_huge(*pmd
)) { /* THP, HugeTLB */
1365 *pmd
= pmd_mkyoung(*pmd
);
1366 pfn
= pmd_pfn(*pmd
);
1371 pte
= pte_offset_kernel(pmd
, fault_ipa
);
1372 if (pte_none(*pte
)) /* Nothing there either */
1375 *pte
= pte_mkyoung(*pte
); /* Just a page... */
1376 pfn
= pte_pfn(*pte
);
1379 spin_unlock(&vcpu
->kvm
->mmu_lock
);
1381 kvm_set_pfn_accessed(pfn
);
1385 * kvm_handle_guest_abort - handles all 2nd stage aborts
1386 * @vcpu: the VCPU pointer
1387 * @run: the kvm_run structure
1389 * Any abort that gets to the host is almost guaranteed to be caused by a
1390 * missing second stage translation table entry, which can mean that either the
1391 * guest simply needs more memory and we must allocate an appropriate page or it
1392 * can mean that the guest tried to access I/O memory, which is emulated by user
1393 * space. The distinction is based on the IPA causing the fault and whether this
1394 * memory region has been registered as standard RAM by user space.
1396 int kvm_handle_guest_abort(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1398 unsigned long fault_status
;
1399 phys_addr_t fault_ipa
;
1400 struct kvm_memory_slot
*memslot
;
1402 bool is_iabt
, write_fault
, writable
;
1406 is_iabt
= kvm_vcpu_trap_is_iabt(vcpu
);
1407 fault_ipa
= kvm_vcpu_get_fault_ipa(vcpu
);
1409 trace_kvm_guest_fault(*vcpu_pc(vcpu
), kvm_vcpu_get_hsr(vcpu
),
1410 kvm_vcpu_get_hfar(vcpu
), fault_ipa
);
1412 /* Check the stage-2 fault is trans. fault or write fault */
1413 fault_status
= kvm_vcpu_trap_get_fault_type(vcpu
);
1414 if (fault_status
!= FSC_FAULT
&& fault_status
!= FSC_PERM
&&
1415 fault_status
!= FSC_ACCESS
) {
1416 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1417 kvm_vcpu_trap_get_class(vcpu
),
1418 (unsigned long)kvm_vcpu_trap_get_fault(vcpu
),
1419 (unsigned long)kvm_vcpu_get_hsr(vcpu
));
1423 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1425 gfn
= fault_ipa
>> PAGE_SHIFT
;
1426 memslot
= gfn_to_memslot(vcpu
->kvm
, gfn
);
1427 hva
= gfn_to_hva_memslot_prot(memslot
, gfn
, &writable
);
1428 write_fault
= kvm_is_write_fault(vcpu
);
1429 if (kvm_is_error_hva(hva
) || (write_fault
&& !writable
)) {
1431 /* Prefetch Abort on I/O address */
1432 kvm_inject_pabt(vcpu
, kvm_vcpu_get_hfar(vcpu
));
1438 * Check for a cache maintenance operation. Since we
1439 * ended-up here, we know it is outside of any memory
1440 * slot. But we can't find out if that is for a device,
1441 * or if the guest is just being stupid. The only thing
1442 * we know for sure is that this range cannot be cached.
1444 * So let's assume that the guest is just being
1445 * cautious, and skip the instruction.
1447 if (kvm_vcpu_dabt_is_cm(vcpu
)) {
1448 kvm_skip_instr(vcpu
, kvm_vcpu_trap_il_is32bit(vcpu
));
1454 * The IPA is reported as [MAX:12], so we need to
1455 * complement it with the bottom 12 bits from the
1456 * faulting VA. This is always 12 bits, irrespective
1459 fault_ipa
|= kvm_vcpu_get_hfar(vcpu
) & ((1 << 12) - 1);
1460 ret
= io_mem_abort(vcpu
, run
, fault_ipa
);
1464 /* Userspace should not be able to register out-of-bounds IPAs */
1465 VM_BUG_ON(fault_ipa
>= KVM_PHYS_SIZE
);
1467 if (fault_status
== FSC_ACCESS
) {
1468 handle_access_fault(vcpu
, fault_ipa
);
1473 ret
= user_mem_abort(vcpu
, fault_ipa
, memslot
, hva
, fault_status
);
1477 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1481 static int handle_hva_to_gpa(struct kvm
*kvm
,
1482 unsigned long start
,
1484 int (*handler
)(struct kvm
*kvm
,
1485 gpa_t gpa
, void *data
),
1488 struct kvm_memslots
*slots
;
1489 struct kvm_memory_slot
*memslot
;
1492 slots
= kvm_memslots(kvm
);
1494 /* we only care about the pages that the guest sees */
1495 kvm_for_each_memslot(memslot
, slots
) {
1496 unsigned long hva_start
, hva_end
;
1499 hva_start
= max(start
, memslot
->userspace_addr
);
1500 hva_end
= min(end
, memslot
->userspace_addr
+
1501 (memslot
->npages
<< PAGE_SHIFT
));
1502 if (hva_start
>= hva_end
)
1506 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1507 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1509 gfn
= hva_to_gfn_memslot(hva_start
, memslot
);
1510 gfn_end
= hva_to_gfn_memslot(hva_end
+ PAGE_SIZE
- 1, memslot
);
1512 for (; gfn
< gfn_end
; ++gfn
) {
1513 gpa_t gpa
= gfn
<< PAGE_SHIFT
;
1514 ret
|= handler(kvm
, gpa
, data
);
1521 static int kvm_unmap_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1523 unmap_stage2_range(kvm
, gpa
, PAGE_SIZE
);
1527 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
1529 unsigned long end
= hva
+ PAGE_SIZE
;
1534 trace_kvm_unmap_hva(hva
);
1535 handle_hva_to_gpa(kvm
, hva
, end
, &kvm_unmap_hva_handler
, NULL
);
1539 int kvm_unmap_hva_range(struct kvm
*kvm
,
1540 unsigned long start
, unsigned long end
)
1545 trace_kvm_unmap_hva_range(start
, end
);
1546 handle_hva_to_gpa(kvm
, start
, end
, &kvm_unmap_hva_handler
, NULL
);
1550 static int kvm_set_spte_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1552 pte_t
*pte
= (pte_t
*)data
;
1555 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1556 * flag clear because MMU notifiers will have unmapped a huge PMD before
1557 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1558 * therefore stage2_set_pte() never needs to clear out a huge PMD
1559 * through this calling path.
1561 stage2_set_pte(kvm
, NULL
, gpa
, pte
, 0);
1566 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
1568 unsigned long end
= hva
+ PAGE_SIZE
;
1574 trace_kvm_set_spte_hva(hva
);
1575 stage2_pte
= pfn_pte(pte_pfn(pte
), PAGE_S2
);
1576 handle_hva_to_gpa(kvm
, hva
, end
, &kvm_set_spte_handler
, &stage2_pte
);
1579 static int kvm_age_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1584 pmd
= stage2_get_pmd(kvm
, NULL
, gpa
);
1585 if (!pmd
|| pmd_none(*pmd
)) /* Nothing there */
1588 if (kvm_pmd_huge(*pmd
)) { /* THP, HugeTLB */
1589 if (pmd_young(*pmd
)) {
1590 *pmd
= pmd_mkold(*pmd
);
1597 pte
= pte_offset_kernel(pmd
, gpa
);
1601 if (pte_young(*pte
)) {
1602 *pte
= pte_mkold(*pte
); /* Just a page... */
1609 static int kvm_test_age_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
1614 pmd
= stage2_get_pmd(kvm
, NULL
, gpa
);
1615 if (!pmd
|| pmd_none(*pmd
)) /* Nothing there */
1618 if (kvm_pmd_huge(*pmd
)) /* THP, HugeTLB */
1619 return pmd_young(*pmd
);
1621 pte
= pte_offset_kernel(pmd
, gpa
);
1622 if (!pte_none(*pte
)) /* Just a page... */
1623 return pte_young(*pte
);
1628 int kvm_age_hva(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
1630 trace_kvm_age_hva(start
, end
);
1631 return handle_hva_to_gpa(kvm
, start
, end
, kvm_age_hva_handler
, NULL
);
1634 int kvm_test_age_hva(struct kvm
*kvm
, unsigned long hva
)
1636 trace_kvm_test_age_hva(hva
);
1637 return handle_hva_to_gpa(kvm
, hva
, hva
, kvm_test_age_hva_handler
, NULL
);
1640 void kvm_mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
1642 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_cache
);
1645 phys_addr_t
kvm_mmu_get_httbr(void)
1647 if (__kvm_cpu_uses_extended_idmap())
1648 return virt_to_phys(merged_hyp_pgd
);
1650 return virt_to_phys(hyp_pgd
);
1653 phys_addr_t
kvm_mmu_get_boot_httbr(void)
1655 if (__kvm_cpu_uses_extended_idmap())
1656 return virt_to_phys(merged_hyp_pgd
);
1658 return virt_to_phys(boot_hyp_pgd
);
1661 phys_addr_t
kvm_get_idmap_vector(void)
1663 return hyp_idmap_vector
;
1666 int kvm_mmu_init(void)
1670 hyp_idmap_start
= kvm_virt_to_phys(__hyp_idmap_text_start
);
1671 hyp_idmap_end
= kvm_virt_to_phys(__hyp_idmap_text_end
);
1672 hyp_idmap_vector
= kvm_virt_to_phys(__kvm_hyp_init
);
1675 * We rely on the linker script to ensure at build time that the HYP
1676 * init code does not cross a page boundary.
1678 BUG_ON((hyp_idmap_start
^ (hyp_idmap_end
- 1)) & PAGE_MASK
);
1680 hyp_pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, hyp_pgd_order
);
1681 boot_hyp_pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, hyp_pgd_order
);
1683 if (!hyp_pgd
|| !boot_hyp_pgd
) {
1684 kvm_err("Hyp mode PGD not allocated\n");
1689 /* Create the idmap in the boot page tables */
1690 err
= __create_hyp_mappings(boot_hyp_pgd
,
1691 hyp_idmap_start
, hyp_idmap_end
,
1692 __phys_to_pfn(hyp_idmap_start
),
1696 kvm_err("Failed to idmap %lx-%lx\n",
1697 hyp_idmap_start
, hyp_idmap_end
);
1701 if (__kvm_cpu_uses_extended_idmap()) {
1702 merged_hyp_pgd
= (pgd_t
*)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
1703 if (!merged_hyp_pgd
) {
1704 kvm_err("Failed to allocate extra HYP pgd\n");
1707 __kvm_extend_hypmap(boot_hyp_pgd
, hyp_pgd
, merged_hyp_pgd
,
1712 /* Map the very same page at the trampoline VA */
1713 err
= __create_hyp_mappings(boot_hyp_pgd
,
1714 TRAMPOLINE_VA
, TRAMPOLINE_VA
+ PAGE_SIZE
,
1715 __phys_to_pfn(hyp_idmap_start
),
1718 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1723 /* Map the same page again into the runtime page tables */
1724 err
= __create_hyp_mappings(hyp_pgd
,
1725 TRAMPOLINE_VA
, TRAMPOLINE_VA
+ PAGE_SIZE
,
1726 __phys_to_pfn(hyp_idmap_start
),
1729 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1740 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
1741 struct kvm_userspace_memory_region
*mem
,
1742 const struct kvm_memory_slot
*old
,
1743 enum kvm_mr_change change
)
1746 * At this point memslot has been committed and there is an
1747 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1748 * memory slot is write protected.
1750 if (change
!= KVM_MR_DELETE
&& mem
->flags
& KVM_MEM_LOG_DIRTY_PAGES
)
1751 kvm_mmu_wp_memory_region(kvm
, mem
->slot
);
1754 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
1755 struct kvm_memory_slot
*memslot
,
1756 struct kvm_userspace_memory_region
*mem
,
1757 enum kvm_mr_change change
)
1759 hva_t hva
= mem
->userspace_addr
;
1760 hva_t reg_end
= hva
+ mem
->memory_size
;
1761 bool writable
= !(mem
->flags
& KVM_MEM_READONLY
);
1764 if (change
!= KVM_MR_CREATE
&& change
!= KVM_MR_MOVE
&&
1765 change
!= KVM_MR_FLAGS_ONLY
)
1769 * Prevent userspace from creating a memory region outside of the IPA
1770 * space addressable by the KVM guest IPA space.
1772 if (memslot
->base_gfn
+ memslot
->npages
>=
1773 (KVM_PHYS_SIZE
>> PAGE_SHIFT
))
1777 * A memory region could potentially cover multiple VMAs, and any holes
1778 * between them, so iterate over all of them to find out if we can map
1779 * any of them right now.
1781 * +--------------------------------------------+
1782 * +---------------+----------------+ +----------------+
1783 * | : VMA 1 | VMA 2 | | VMA 3 : |
1784 * +---------------+----------------+ +----------------+
1786 * +--------------------------------------------+
1789 struct vm_area_struct
*vma
= find_vma(current
->mm
, hva
);
1790 hva_t vm_start
, vm_end
;
1792 if (!vma
|| vma
->vm_start
>= reg_end
)
1796 * Mapping a read-only VMA is only allowed if the
1797 * memory region is configured as read-only.
1799 if (writable
&& !(vma
->vm_flags
& VM_WRITE
)) {
1805 * Take the intersection of this VMA with the memory region
1807 vm_start
= max(hva
, vma
->vm_start
);
1808 vm_end
= min(reg_end
, vma
->vm_end
);
1810 if (vma
->vm_flags
& VM_PFNMAP
) {
1811 gpa_t gpa
= mem
->guest_phys_addr
+
1812 (vm_start
- mem
->userspace_addr
);
1815 pa
= (phys_addr_t
)vma
->vm_pgoff
<< PAGE_SHIFT
;
1816 pa
+= vm_start
- vma
->vm_start
;
1818 /* IO region dirty page logging not allowed */
1819 if (memslot
->flags
& KVM_MEM_LOG_DIRTY_PAGES
)
1822 ret
= kvm_phys_addr_ioremap(kvm
, gpa
, pa
,
1829 } while (hva
< reg_end
);
1831 if (change
== KVM_MR_FLAGS_ONLY
)
1834 spin_lock(&kvm
->mmu_lock
);
1836 unmap_stage2_range(kvm
, mem
->guest_phys_addr
, mem
->memory_size
);
1838 stage2_flush_memslot(kvm
, memslot
);
1839 spin_unlock(&kvm
->mmu_lock
);
1843 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
1844 struct kvm_memory_slot
*dont
)
1848 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
1849 unsigned long npages
)
1852 * Readonly memslots are not incoherent with the caches by definition,
1853 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1854 * that the guest may consider devices and hence map as uncached.
1855 * To prevent incoherency issues in these cases, tag all readonly
1856 * regions as incoherent.
1858 if (slot
->flags
& KVM_MEM_READONLY
)
1859 slot
->flags
|= KVM_MEMSLOT_INCOHERENT
;
1863 void kvm_arch_memslots_updated(struct kvm
*kvm
)
1867 void kvm_arch_flush_shadow_all(struct kvm
*kvm
)
1869 kvm_free_stage2_pgd(kvm
);
1872 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
1873 struct kvm_memory_slot
*slot
)
1875 gpa_t gpa
= slot
->base_gfn
<< PAGE_SHIFT
;
1876 phys_addr_t size
= slot
->npages
<< PAGE_SHIFT
;
1878 spin_lock(&kvm
->mmu_lock
);
1879 unmap_stage2_range(kvm
, gpa
, size
);
1880 spin_unlock(&kvm
->mmu_lock
);
1884 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1887 * - S/W ops are local to a CPU (not broadcast)
1888 * - We have line migration behind our back (speculation)
1889 * - System caches don't support S/W at all (damn!)
1891 * In the face of the above, the best we can do is to try and convert
1892 * S/W ops to VA ops. Because the guest is not allowed to infer the
1893 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1894 * which is a rather good thing for us.
1896 * Also, it is only used when turning caches on/off ("The expected
1897 * usage of the cache maintenance instructions that operate by set/way
1898 * is associated with the cache maintenance instructions associated
1899 * with the powerdown and powerup of caches, if this is required by
1900 * the implementation.").
1902 * We use the following policy:
1904 * - If we trap a S/W operation, we enable VM trapping to detect
1905 * caches being turned on/off, and do a full clean.
1907 * - We flush the caches on both caches being turned on and off.
1909 * - Once the caches are enabled, we stop trapping VM ops.
1911 void kvm_set_way_flush(struct kvm_vcpu
*vcpu
)
1913 unsigned long hcr
= vcpu_get_hcr(vcpu
);
1916 * If this is the first time we do a S/W operation
1917 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1920 * Otherwise, rely on the VM trapping to wait for the MMU +
1921 * Caches to be turned off. At that point, we'll be able to
1922 * clean the caches again.
1924 if (!(hcr
& HCR_TVM
)) {
1925 trace_kvm_set_way_flush(*vcpu_pc(vcpu
),
1926 vcpu_has_cache_enabled(vcpu
));
1927 stage2_flush_vm(vcpu
->kvm
);
1928 vcpu_set_hcr(vcpu
, hcr
| HCR_TVM
);
1932 void kvm_toggle_cache(struct kvm_vcpu
*vcpu
, bool was_enabled
)
1934 bool now_enabled
= vcpu_has_cache_enabled(vcpu
);
1937 * If switching the MMU+caches on, need to invalidate the caches.
1938 * If switching it off, need to clean the caches.
1939 * Clean + invalidate does the trick always.
1941 if (now_enabled
!= was_enabled
)
1942 stage2_flush_vm(vcpu
->kvm
);
1944 /* Caches are now on, stop trapping VM ops (until a S/W op) */
1946 vcpu_set_hcr(vcpu
, vcpu_get_hcr(vcpu
) & ~HCR_TVM
);
1948 trace_kvm_toggle_cache(*vcpu_pc(vcpu
), was_enabled
, now_enabled
);