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 DEFINE_MUTEX(kvm_hyp_pgd_mutex
);
40 static void *init_bounce_page
;
41 static unsigned long hyp_idmap_start
;
42 static unsigned long hyp_idmap_end
;
43 static phys_addr_t hyp_idmap_vector
;
45 #define pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
47 #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
49 static void kvm_tlb_flush_vmid_ipa(struct kvm
*kvm
, phys_addr_t ipa
)
52 * This function also gets called when dealing with HYP page
53 * tables. As HYP doesn't have an associated struct kvm (and
54 * the HYP page tables are fairly static), we don't do
58 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa
, kvm
, ipa
);
61 static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache
*cache
,
66 BUG_ON(max
> KVM_NR_MEM_OBJS
);
67 if (cache
->nobjs
>= min
)
69 while (cache
->nobjs
< max
) {
70 page
= (void *)__get_free_page(PGALLOC_GFP
);
73 cache
->objects
[cache
->nobjs
++] = page
;
78 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache
*mc
)
81 free_page((unsigned long)mc
->objects
[--mc
->nobjs
]);
84 static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache
*mc
)
88 BUG_ON(!mc
|| !mc
->nobjs
);
89 p
= mc
->objects
[--mc
->nobjs
];
93 static void clear_pgd_entry(struct kvm
*kvm
, pgd_t
*pgd
, phys_addr_t addr
)
95 pud_t
*pud_table __maybe_unused
= pud_offset(pgd
, 0);
97 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
98 pud_free(NULL
, pud_table
);
99 put_page(virt_to_page(pgd
));
102 static void clear_pud_entry(struct kvm
*kvm
, pud_t
*pud
, phys_addr_t addr
)
104 pmd_t
*pmd_table
= pmd_offset(pud
, 0);
105 VM_BUG_ON(pud_huge(*pud
));
107 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
108 pmd_free(NULL
, pmd_table
);
109 put_page(virt_to_page(pud
));
112 static void clear_pmd_entry(struct kvm
*kvm
, pmd_t
*pmd
, phys_addr_t addr
)
114 pte_t
*pte_table
= pte_offset_kernel(pmd
, 0);
115 VM_BUG_ON(kvm_pmd_huge(*pmd
));
117 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
118 pte_free_kernel(NULL
, pte_table
);
119 put_page(virt_to_page(pmd
));
122 static void unmap_ptes(struct kvm
*kvm
, pmd_t
*pmd
,
123 phys_addr_t addr
, phys_addr_t end
)
125 phys_addr_t start_addr
= addr
;
126 pte_t
*pte
, *start_pte
;
128 start_pte
= pte
= pte_offset_kernel(pmd
, addr
);
130 if (!pte_none(*pte
)) {
131 kvm_set_pte(pte
, __pte(0));
132 put_page(virt_to_page(pte
));
133 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
135 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
137 if (kvm_pte_table_empty(start_pte
))
138 clear_pmd_entry(kvm
, pmd
, start_addr
);
141 static void unmap_pmds(struct kvm
*kvm
, pud_t
*pud
,
142 phys_addr_t addr
, phys_addr_t end
)
144 phys_addr_t next
, start_addr
= addr
;
145 pmd_t
*pmd
, *start_pmd
;
147 start_pmd
= pmd
= pmd_offset(pud
, addr
);
149 next
= kvm_pmd_addr_end(addr
, end
);
150 if (!pmd_none(*pmd
)) {
151 if (kvm_pmd_huge(*pmd
)) {
153 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
154 put_page(virt_to_page(pmd
));
156 unmap_ptes(kvm
, pmd
, addr
, next
);
159 } while (pmd
++, addr
= next
, addr
!= end
);
161 if (kvm_pmd_table_empty(start_pmd
))
162 clear_pud_entry(kvm
, pud
, start_addr
);
165 static void unmap_puds(struct kvm
*kvm
, pgd_t
*pgd
,
166 phys_addr_t addr
, phys_addr_t end
)
168 phys_addr_t next
, start_addr
= addr
;
169 pud_t
*pud
, *start_pud
;
171 start_pud
= pud
= pud_offset(pgd
, addr
);
173 next
= kvm_pud_addr_end(addr
, end
);
174 if (!pud_none(*pud
)) {
175 if (pud_huge(*pud
)) {
177 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
178 put_page(virt_to_page(pud
));
180 unmap_pmds(kvm
, pud
, addr
, next
);
183 } while (pud
++, addr
= next
, addr
!= end
);
185 if (kvm_pud_table_empty(start_pud
))
186 clear_pgd_entry(kvm
, pgd
, start_addr
);
190 static void unmap_range(struct kvm
*kvm
, pgd_t
*pgdp
,
191 phys_addr_t start
, u64 size
)
194 phys_addr_t addr
= start
, end
= start
+ size
;
197 pgd
= pgdp
+ pgd_index(addr
);
199 next
= kvm_pgd_addr_end(addr
, end
);
200 unmap_puds(kvm
, pgd
, addr
, next
);
201 } while (pgd
++, addr
= next
, addr
!= end
);
204 static void stage2_flush_ptes(struct kvm
*kvm
, pmd_t
*pmd
,
205 phys_addr_t addr
, phys_addr_t end
)
209 pte
= pte_offset_kernel(pmd
, addr
);
211 if (!pte_none(*pte
)) {
212 hva_t hva
= gfn_to_hva(kvm
, addr
>> PAGE_SHIFT
);
213 kvm_flush_dcache_to_poc((void*)hva
, PAGE_SIZE
);
215 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
218 static void stage2_flush_pmds(struct kvm
*kvm
, pud_t
*pud
,
219 phys_addr_t addr
, phys_addr_t end
)
224 pmd
= pmd_offset(pud
, addr
);
226 next
= kvm_pmd_addr_end(addr
, end
);
227 if (!pmd_none(*pmd
)) {
228 if (kvm_pmd_huge(*pmd
)) {
229 hva_t hva
= gfn_to_hva(kvm
, addr
>> PAGE_SHIFT
);
230 kvm_flush_dcache_to_poc((void*)hva
, PMD_SIZE
);
232 stage2_flush_ptes(kvm
, pmd
, addr
, next
);
235 } while (pmd
++, addr
= next
, addr
!= end
);
238 static void stage2_flush_puds(struct kvm
*kvm
, pgd_t
*pgd
,
239 phys_addr_t addr
, phys_addr_t end
)
244 pud
= pud_offset(pgd
, addr
);
246 next
= kvm_pud_addr_end(addr
, end
);
247 if (!pud_none(*pud
)) {
248 if (pud_huge(*pud
)) {
249 hva_t hva
= gfn_to_hva(kvm
, addr
>> PAGE_SHIFT
);
250 kvm_flush_dcache_to_poc((void*)hva
, PUD_SIZE
);
252 stage2_flush_pmds(kvm
, pud
, addr
, next
);
255 } while (pud
++, addr
= next
, addr
!= end
);
258 static void stage2_flush_memslot(struct kvm
*kvm
,
259 struct kvm_memory_slot
*memslot
)
261 phys_addr_t addr
= memslot
->base_gfn
<< PAGE_SHIFT
;
262 phys_addr_t end
= addr
+ PAGE_SIZE
* memslot
->npages
;
266 pgd
= kvm
->arch
.pgd
+ pgd_index(addr
);
268 next
= kvm_pgd_addr_end(addr
, end
);
269 stage2_flush_puds(kvm
, pgd
, addr
, next
);
270 } while (pgd
++, addr
= next
, addr
!= end
);
274 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
275 * @kvm: The struct kvm pointer
277 * Go through the stage 2 page tables and invalidate any cache lines
278 * backing memory already mapped to the VM.
280 void stage2_flush_vm(struct kvm
*kvm
)
282 struct kvm_memslots
*slots
;
283 struct kvm_memory_slot
*memslot
;
286 idx
= srcu_read_lock(&kvm
->srcu
);
287 spin_lock(&kvm
->mmu_lock
);
289 slots
= kvm_memslots(kvm
);
290 kvm_for_each_memslot(memslot
, slots
)
291 stage2_flush_memslot(kvm
, memslot
);
293 spin_unlock(&kvm
->mmu_lock
);
294 srcu_read_unlock(&kvm
->srcu
, idx
);
298 * free_boot_hyp_pgd - free HYP boot page tables
300 * Free the HYP boot page tables. The bounce page is also freed.
302 void free_boot_hyp_pgd(void)
304 mutex_lock(&kvm_hyp_pgd_mutex
);
307 unmap_range(NULL
, boot_hyp_pgd
, hyp_idmap_start
, PAGE_SIZE
);
308 unmap_range(NULL
, boot_hyp_pgd
, TRAMPOLINE_VA
, PAGE_SIZE
);
309 free_pages((unsigned long)boot_hyp_pgd
, pgd_order
);
314 unmap_range(NULL
, hyp_pgd
, TRAMPOLINE_VA
, PAGE_SIZE
);
316 free_page((unsigned long)init_bounce_page
);
317 init_bounce_page
= NULL
;
319 mutex_unlock(&kvm_hyp_pgd_mutex
);
323 * free_hyp_pgds - free Hyp-mode page tables
325 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
326 * therefore contains either mappings in the kernel memory area (above
327 * PAGE_OFFSET), or device mappings in the vmalloc range (from
328 * VMALLOC_START to VMALLOC_END).
330 * boot_hyp_pgd should only map two pages for the init code.
332 void free_hyp_pgds(void)
338 mutex_lock(&kvm_hyp_pgd_mutex
);
341 for (addr
= PAGE_OFFSET
; virt_addr_valid(addr
); addr
+= PGDIR_SIZE
)
342 unmap_range(NULL
, hyp_pgd
, KERN_TO_HYP(addr
), PGDIR_SIZE
);
343 for (addr
= VMALLOC_START
; is_vmalloc_addr((void*)addr
); addr
+= PGDIR_SIZE
)
344 unmap_range(NULL
, hyp_pgd
, KERN_TO_HYP(addr
), PGDIR_SIZE
);
346 free_pages((unsigned long)hyp_pgd
, pgd_order
);
350 mutex_unlock(&kvm_hyp_pgd_mutex
);
353 static void create_hyp_pte_mappings(pmd_t
*pmd
, unsigned long start
,
354 unsigned long end
, unsigned long pfn
,
362 pte
= pte_offset_kernel(pmd
, addr
);
363 kvm_set_pte(pte
, pfn_pte(pfn
, prot
));
364 get_page(virt_to_page(pte
));
365 kvm_flush_dcache_to_poc(pte
, sizeof(*pte
));
367 } while (addr
+= PAGE_SIZE
, addr
!= end
);
370 static int create_hyp_pmd_mappings(pud_t
*pud
, unsigned long start
,
371 unsigned long end
, unsigned long pfn
,
376 unsigned long addr
, next
;
380 pmd
= pmd_offset(pud
, addr
);
382 BUG_ON(pmd_sect(*pmd
));
384 if (pmd_none(*pmd
)) {
385 pte
= pte_alloc_one_kernel(NULL
, addr
);
387 kvm_err("Cannot allocate Hyp pte\n");
390 pmd_populate_kernel(NULL
, pmd
, pte
);
391 get_page(virt_to_page(pmd
));
392 kvm_flush_dcache_to_poc(pmd
, sizeof(*pmd
));
395 next
= pmd_addr_end(addr
, end
);
397 create_hyp_pte_mappings(pmd
, addr
, next
, pfn
, prot
);
398 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
399 } while (addr
= next
, addr
!= end
);
404 static int __create_hyp_mappings(pgd_t
*pgdp
,
405 unsigned long start
, unsigned long end
,
406 unsigned long pfn
, pgprot_t prot
)
411 unsigned long addr
, next
;
414 mutex_lock(&kvm_hyp_pgd_mutex
);
415 addr
= start
& PAGE_MASK
;
416 end
= PAGE_ALIGN(end
);
418 pgd
= pgdp
+ pgd_index(addr
);
419 pud
= pud_offset(pgd
, addr
);
421 if (pud_none_or_clear_bad(pud
)) {
422 pmd
= pmd_alloc_one(NULL
, addr
);
424 kvm_err("Cannot allocate Hyp pmd\n");
428 pud_populate(NULL
, pud
, pmd
);
429 get_page(virt_to_page(pud
));
430 kvm_flush_dcache_to_poc(pud
, sizeof(*pud
));
433 next
= pgd_addr_end(addr
, end
);
434 err
= create_hyp_pmd_mappings(pud
, addr
, next
, pfn
, prot
);
437 pfn
+= (next
- addr
) >> PAGE_SHIFT
;
438 } while (addr
= next
, addr
!= end
);
440 mutex_unlock(&kvm_hyp_pgd_mutex
);
444 static phys_addr_t
kvm_kaddr_to_phys(void *kaddr
)
446 if (!is_vmalloc_addr(kaddr
)) {
447 BUG_ON(!virt_addr_valid(kaddr
));
450 return page_to_phys(vmalloc_to_page(kaddr
)) +
451 offset_in_page(kaddr
);
456 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
457 * @from: The virtual kernel start address of the range
458 * @to: The virtual kernel end address of the range (exclusive)
460 * The same virtual address as the kernel virtual address is also used
461 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
464 int create_hyp_mappings(void *from
, void *to
)
466 phys_addr_t phys_addr
;
467 unsigned long virt_addr
;
468 unsigned long start
= KERN_TO_HYP((unsigned long)from
);
469 unsigned long end
= KERN_TO_HYP((unsigned long)to
);
471 start
= start
& PAGE_MASK
;
472 end
= PAGE_ALIGN(end
);
474 for (virt_addr
= start
; virt_addr
< end
; virt_addr
+= PAGE_SIZE
) {
477 phys_addr
= kvm_kaddr_to_phys(from
+ virt_addr
- start
);
478 err
= __create_hyp_mappings(hyp_pgd
, virt_addr
,
479 virt_addr
+ PAGE_SIZE
,
480 __phys_to_pfn(phys_addr
),
490 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
491 * @from: The kernel start VA of the range
492 * @to: The kernel end VA of the range (exclusive)
493 * @phys_addr: The physical start address which gets mapped
495 * The resulting HYP VA is the same as the kernel VA, modulo
498 int create_hyp_io_mappings(void *from
, void *to
, phys_addr_t phys_addr
)
500 unsigned long start
= KERN_TO_HYP((unsigned long)from
);
501 unsigned long end
= KERN_TO_HYP((unsigned long)to
);
503 /* Check for a valid kernel IO mapping */
504 if (!is_vmalloc_addr(from
) || !is_vmalloc_addr(to
- 1))
507 return __create_hyp_mappings(hyp_pgd
, start
, end
,
508 __phys_to_pfn(phys_addr
), PAGE_HYP_DEVICE
);
512 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
513 * @kvm: The KVM struct pointer for the VM.
515 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
516 * support either full 40-bit input addresses or limited to 32-bit input
517 * addresses). Clears the allocated pages.
519 * Note we don't need locking here as this is only called when the VM is
520 * created, which can only be done once.
522 int kvm_alloc_stage2_pgd(struct kvm
*kvm
)
526 if (kvm
->arch
.pgd
!= NULL
) {
527 kvm_err("kvm_arch already initialized?\n");
531 pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, S2_PGD_ORDER
);
542 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
543 * @kvm: The VM pointer
544 * @start: The intermediate physical base address of the range to unmap
545 * @size: The size of the area to unmap
547 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
548 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
549 * destroying the VM), otherwise another faulting VCPU may come in and mess
550 * with things behind our backs.
552 static void unmap_stage2_range(struct kvm
*kvm
, phys_addr_t start
, u64 size
)
554 unmap_range(kvm
, kvm
->arch
.pgd
, start
, size
);
558 * kvm_free_stage2_pgd - free all stage-2 tables
559 * @kvm: The KVM struct pointer for the VM.
561 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
562 * underlying level-2 and level-3 tables before freeing the actual level-1 table
563 * and setting the struct pointer to NULL.
565 * Note we don't need locking here as this is only called when the VM is
566 * destroyed, which can only be done once.
568 void kvm_free_stage2_pgd(struct kvm
*kvm
)
570 if (kvm
->arch
.pgd
== NULL
)
573 unmap_stage2_range(kvm
, 0, KVM_PHYS_SIZE
);
574 free_pages((unsigned long)kvm
->arch
.pgd
, S2_PGD_ORDER
);
575 kvm
->arch
.pgd
= NULL
;
578 static pmd_t
*stage2_get_pmd(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
585 pgd
= kvm
->arch
.pgd
+ pgd_index(addr
);
586 pud
= pud_offset(pgd
, addr
);
587 if (pud_none(*pud
)) {
590 pmd
= mmu_memory_cache_alloc(cache
);
591 pud_populate(NULL
, pud
, pmd
);
592 get_page(virt_to_page(pud
));
595 return pmd_offset(pud
, addr
);
598 static int stage2_set_pmd_huge(struct kvm
*kvm
, struct kvm_mmu_memory_cache
599 *cache
, phys_addr_t addr
, const pmd_t
*new_pmd
)
603 pmd
= stage2_get_pmd(kvm
, cache
, addr
);
607 * Mapping in huge pages should only happen through a fault. If a
608 * page is merged into a transparent huge page, the individual
609 * subpages of that huge page should be unmapped through MMU
610 * notifiers before we get here.
612 * Merging of CompoundPages is not supported; they should become
613 * splitting first, unmapped, merged, and mapped back in on-demand.
615 VM_BUG_ON(pmd_present(*pmd
) && pmd_pfn(*pmd
) != pmd_pfn(*new_pmd
));
618 kvm_set_pmd(pmd
, *new_pmd
);
619 if (pmd_present(old_pmd
))
620 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
622 get_page(virt_to_page(pmd
));
626 static int stage2_set_pte(struct kvm
*kvm
, struct kvm_mmu_memory_cache
*cache
,
627 phys_addr_t addr
, const pte_t
*new_pte
, bool iomap
)
632 /* Create stage-2 page table mapping - Level 1 */
633 pmd
= stage2_get_pmd(kvm
, cache
, addr
);
636 * Ignore calls from kvm_set_spte_hva for unallocated
642 /* Create stage-2 page mappings - Level 2 */
643 if (pmd_none(*pmd
)) {
645 return 0; /* ignore calls from kvm_set_spte_hva */
646 pte
= mmu_memory_cache_alloc(cache
);
648 pmd_populate_kernel(NULL
, pmd
, pte
);
649 get_page(virt_to_page(pmd
));
652 pte
= pte_offset_kernel(pmd
, addr
);
654 if (iomap
&& pte_present(*pte
))
657 /* Create 2nd stage page table mapping - Level 3 */
659 kvm_set_pte(pte
, *new_pte
);
660 if (pte_present(old_pte
))
661 kvm_tlb_flush_vmid_ipa(kvm
, addr
);
663 get_page(virt_to_page(pte
));
669 * kvm_phys_addr_ioremap - map a device range to guest IPA
671 * @kvm: The KVM pointer
672 * @guest_ipa: The IPA at which to insert the mapping
673 * @pa: The physical address of the device
674 * @size: The size of the mapping
676 int kvm_phys_addr_ioremap(struct kvm
*kvm
, phys_addr_t guest_ipa
,
677 phys_addr_t pa
, unsigned long size
)
679 phys_addr_t addr
, end
;
682 struct kvm_mmu_memory_cache cache
= { 0, };
684 end
= (guest_ipa
+ size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
685 pfn
= __phys_to_pfn(pa
);
687 for (addr
= guest_ipa
; addr
< end
; addr
+= PAGE_SIZE
) {
688 pte_t pte
= pfn_pte(pfn
, PAGE_S2_DEVICE
);
690 ret
= mmu_topup_memory_cache(&cache
, 2, 2);
693 spin_lock(&kvm
->mmu_lock
);
694 ret
= stage2_set_pte(kvm
, &cache
, addr
, &pte
, true);
695 spin_unlock(&kvm
->mmu_lock
);
703 mmu_free_memory_cache(&cache
);
707 static bool transparent_hugepage_adjust(pfn_t
*pfnp
, phys_addr_t
*ipap
)
710 gfn_t gfn
= *ipap
>> PAGE_SHIFT
;
712 if (PageTransCompound(pfn_to_page(pfn
))) {
715 * The address we faulted on is backed by a transparent huge
716 * page. However, because we map the compound huge page and
717 * not the individual tail page, we need to transfer the
718 * refcount to the head page. We have to be careful that the
719 * THP doesn't start to split while we are adjusting the
722 * We are sure this doesn't happen, because mmu_notifier_retry
723 * was successful and we are holding the mmu_lock, so if this
724 * THP is trying to split, it will be blocked in the mmu
725 * notifier before touching any of the pages, specifically
726 * before being able to call __split_huge_page_refcount().
728 * We can therefore safely transfer the refcount from PG_tail
729 * to PG_head and switch the pfn from a tail page to the head
732 mask
= PTRS_PER_PMD
- 1;
733 VM_BUG_ON((gfn
& mask
) != (pfn
& mask
));
736 kvm_release_pfn_clean(pfn
);
748 static bool kvm_is_write_fault(struct kvm_vcpu
*vcpu
)
750 if (kvm_vcpu_trap_is_iabt(vcpu
))
753 return kvm_vcpu_dabt_iswrite(vcpu
);
756 static int user_mem_abort(struct kvm_vcpu
*vcpu
, phys_addr_t fault_ipa
,
757 struct kvm_memory_slot
*memslot
, unsigned long hva
,
758 unsigned long fault_status
)
761 bool write_fault
, writable
, hugetlb
= false, force_pte
= false;
762 unsigned long mmu_seq
;
763 gfn_t gfn
= fault_ipa
>> PAGE_SHIFT
;
764 struct kvm
*kvm
= vcpu
->kvm
;
765 struct kvm_mmu_memory_cache
*memcache
= &vcpu
->arch
.mmu_page_cache
;
766 struct vm_area_struct
*vma
;
768 pgprot_t mem_type
= PAGE_S2
;
770 write_fault
= kvm_is_write_fault(vcpu
);
771 if (fault_status
== FSC_PERM
&& !write_fault
) {
772 kvm_err("Unexpected L2 read permission error\n");
776 /* Let's check if we will get back a huge page backed by hugetlbfs */
777 down_read(¤t
->mm
->mmap_sem
);
778 vma
= find_vma_intersection(current
->mm
, hva
, hva
+ 1);
779 if (unlikely(!vma
)) {
780 kvm_err("Failed to find VMA for hva 0x%lx\n", hva
);
781 up_read(¤t
->mm
->mmap_sem
);
785 if (is_vm_hugetlb_page(vma
)) {
787 gfn
= (fault_ipa
& PMD_MASK
) >> PAGE_SHIFT
;
790 * Pages belonging to memslots that don't have the same
791 * alignment for userspace and IPA cannot be mapped using
792 * block descriptors even if the pages belong to a THP for
793 * the process, because the stage-2 block descriptor will
794 * cover more than a single THP and we loose atomicity for
795 * unmapping, updates, and splits of the THP or other pages
796 * in the stage-2 block range.
798 if ((memslot
->userspace_addr
& ~PMD_MASK
) !=
799 ((memslot
->base_gfn
<< PAGE_SHIFT
) & ~PMD_MASK
))
802 up_read(¤t
->mm
->mmap_sem
);
804 /* We need minimum second+third level pages */
805 ret
= mmu_topup_memory_cache(memcache
, 2, KVM_NR_MEM_OBJS
);
809 mmu_seq
= vcpu
->kvm
->mmu_notifier_seq
;
811 * Ensure the read of mmu_notifier_seq happens before we call
812 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
813 * the page we just got a reference to gets unmapped before we have a
814 * chance to grab the mmu_lock, which ensure that if the page gets
815 * unmapped afterwards, the call to kvm_unmap_hva will take it away
816 * from us again properly. This smp_rmb() interacts with the smp_wmb()
817 * in kvm_mmu_notifier_invalidate_<page|range_end>.
821 pfn
= gfn_to_pfn_prot(kvm
, gfn
, write_fault
, &writable
);
822 if (is_error_pfn(pfn
))
825 if (kvm_is_mmio_pfn(pfn
))
826 mem_type
= PAGE_S2_DEVICE
;
828 spin_lock(&kvm
->mmu_lock
);
829 if (mmu_notifier_retry(kvm
, mmu_seq
))
831 if (!hugetlb
&& !force_pte
)
832 hugetlb
= transparent_hugepage_adjust(&pfn
, &fault_ipa
);
835 pmd_t new_pmd
= pfn_pmd(pfn
, mem_type
);
836 new_pmd
= pmd_mkhuge(new_pmd
);
838 kvm_set_s2pmd_writable(&new_pmd
);
839 kvm_set_pfn_dirty(pfn
);
841 coherent_cache_guest_page(vcpu
, hva
& PMD_MASK
, PMD_SIZE
);
842 ret
= stage2_set_pmd_huge(kvm
, memcache
, fault_ipa
, &new_pmd
);
844 pte_t new_pte
= pfn_pte(pfn
, mem_type
);
846 kvm_set_s2pte_writable(&new_pte
);
847 kvm_set_pfn_dirty(pfn
);
849 coherent_cache_guest_page(vcpu
, hva
, PAGE_SIZE
);
850 ret
= stage2_set_pte(kvm
, memcache
, fault_ipa
, &new_pte
,
851 mem_type
== PAGE_S2_DEVICE
);
856 spin_unlock(&kvm
->mmu_lock
);
857 kvm_release_pfn_clean(pfn
);
862 * kvm_handle_guest_abort - handles all 2nd stage aborts
863 * @vcpu: the VCPU pointer
864 * @run: the kvm_run structure
866 * Any abort that gets to the host is almost guaranteed to be caused by a
867 * missing second stage translation table entry, which can mean that either the
868 * guest simply needs more memory and we must allocate an appropriate page or it
869 * can mean that the guest tried to access I/O memory, which is emulated by user
870 * space. The distinction is based on the IPA causing the fault and whether this
871 * memory region has been registered as standard RAM by user space.
873 int kvm_handle_guest_abort(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
875 unsigned long fault_status
;
876 phys_addr_t fault_ipa
;
877 struct kvm_memory_slot
*memslot
;
879 bool is_iabt
, write_fault
, writable
;
883 is_iabt
= kvm_vcpu_trap_is_iabt(vcpu
);
884 fault_ipa
= kvm_vcpu_get_fault_ipa(vcpu
);
886 trace_kvm_guest_fault(*vcpu_pc(vcpu
), kvm_vcpu_get_hsr(vcpu
),
887 kvm_vcpu_get_hfar(vcpu
), fault_ipa
);
889 /* Check the stage-2 fault is trans. fault or write fault */
890 fault_status
= kvm_vcpu_trap_get_fault_type(vcpu
);
891 if (fault_status
!= FSC_FAULT
&& fault_status
!= FSC_PERM
) {
892 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
893 kvm_vcpu_trap_get_class(vcpu
),
894 (unsigned long)kvm_vcpu_trap_get_fault(vcpu
),
895 (unsigned long)kvm_vcpu_get_hsr(vcpu
));
899 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
901 gfn
= fault_ipa
>> PAGE_SHIFT
;
902 memslot
= gfn_to_memslot(vcpu
->kvm
, gfn
);
903 hva
= gfn_to_hva_memslot_prot(memslot
, gfn
, &writable
);
904 write_fault
= kvm_is_write_fault(vcpu
);
905 if (kvm_is_error_hva(hva
) || (write_fault
&& !writable
)) {
907 /* Prefetch Abort on I/O address */
908 kvm_inject_pabt(vcpu
, kvm_vcpu_get_hfar(vcpu
));
914 * The IPA is reported as [MAX:12], so we need to
915 * complement it with the bottom 12 bits from the
916 * faulting VA. This is always 12 bits, irrespective
919 fault_ipa
|= kvm_vcpu_get_hfar(vcpu
) & ((1 << 12) - 1);
920 ret
= io_mem_abort(vcpu
, run
, fault_ipa
);
924 ret
= user_mem_abort(vcpu
, fault_ipa
, memslot
, hva
, fault_status
);
928 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
932 static void handle_hva_to_gpa(struct kvm
*kvm
,
935 void (*handler
)(struct kvm
*kvm
,
936 gpa_t gpa
, void *data
),
939 struct kvm_memslots
*slots
;
940 struct kvm_memory_slot
*memslot
;
942 slots
= kvm_memslots(kvm
);
944 /* we only care about the pages that the guest sees */
945 kvm_for_each_memslot(memslot
, slots
) {
946 unsigned long hva_start
, hva_end
;
949 hva_start
= max(start
, memslot
->userspace_addr
);
950 hva_end
= min(end
, memslot
->userspace_addr
+
951 (memslot
->npages
<< PAGE_SHIFT
));
952 if (hva_start
>= hva_end
)
956 * {gfn(page) | page intersects with [hva_start, hva_end)} =
957 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
959 gfn
= hva_to_gfn_memslot(hva_start
, memslot
);
960 gfn_end
= hva_to_gfn_memslot(hva_end
+ PAGE_SIZE
- 1, memslot
);
962 for (; gfn
< gfn_end
; ++gfn
) {
963 gpa_t gpa
= gfn
<< PAGE_SHIFT
;
964 handler(kvm
, gpa
, data
);
969 static void kvm_unmap_hva_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
971 unmap_stage2_range(kvm
, gpa
, PAGE_SIZE
);
974 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
976 unsigned long end
= hva
+ PAGE_SIZE
;
981 trace_kvm_unmap_hva(hva
);
982 handle_hva_to_gpa(kvm
, hva
, end
, &kvm_unmap_hva_handler
, NULL
);
986 int kvm_unmap_hva_range(struct kvm
*kvm
,
987 unsigned long start
, unsigned long end
)
992 trace_kvm_unmap_hva_range(start
, end
);
993 handle_hva_to_gpa(kvm
, start
, end
, &kvm_unmap_hva_handler
, NULL
);
997 static void kvm_set_spte_handler(struct kvm
*kvm
, gpa_t gpa
, void *data
)
999 pte_t
*pte
= (pte_t
*)data
;
1001 stage2_set_pte(kvm
, NULL
, gpa
, pte
, false);
1005 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
1007 unsigned long end
= hva
+ PAGE_SIZE
;
1013 trace_kvm_set_spte_hva(hva
);
1014 stage2_pte
= pfn_pte(pte_pfn(pte
), PAGE_S2
);
1015 handle_hva_to_gpa(kvm
, hva
, end
, &kvm_set_spte_handler
, &stage2_pte
);
1018 void kvm_mmu_free_memory_caches(struct kvm_vcpu
*vcpu
)
1020 mmu_free_memory_cache(&vcpu
->arch
.mmu_page_cache
);
1023 phys_addr_t
kvm_mmu_get_httbr(void)
1025 return virt_to_phys(hyp_pgd
);
1028 phys_addr_t
kvm_mmu_get_boot_httbr(void)
1030 return virt_to_phys(boot_hyp_pgd
);
1033 phys_addr_t
kvm_get_idmap_vector(void)
1035 return hyp_idmap_vector
;
1038 int kvm_mmu_init(void)
1042 hyp_idmap_start
= kvm_virt_to_phys(__hyp_idmap_text_start
);
1043 hyp_idmap_end
= kvm_virt_to_phys(__hyp_idmap_text_end
);
1044 hyp_idmap_vector
= kvm_virt_to_phys(__kvm_hyp_init
);
1046 if ((hyp_idmap_start
^ hyp_idmap_end
) & PAGE_MASK
) {
1048 * Our init code is crossing a page boundary. Allocate
1049 * a bounce page, copy the code over and use that.
1051 size_t len
= __hyp_idmap_text_end
- __hyp_idmap_text_start
;
1052 phys_addr_t phys_base
;
1054 init_bounce_page
= (void *)__get_free_page(GFP_KERNEL
);
1055 if (!init_bounce_page
) {
1056 kvm_err("Couldn't allocate HYP init bounce page\n");
1061 memcpy(init_bounce_page
, __hyp_idmap_text_start
, len
);
1063 * Warning: the code we just copied to the bounce page
1064 * must be flushed to the point of coherency.
1065 * Otherwise, the data may be sitting in L2, and HYP
1066 * mode won't be able to observe it as it runs with
1067 * caches off at that point.
1069 kvm_flush_dcache_to_poc(init_bounce_page
, len
);
1071 phys_base
= kvm_virt_to_phys(init_bounce_page
);
1072 hyp_idmap_vector
+= phys_base
- hyp_idmap_start
;
1073 hyp_idmap_start
= phys_base
;
1074 hyp_idmap_end
= phys_base
+ len
;
1076 kvm_info("Using HYP init bounce page @%lx\n",
1077 (unsigned long)phys_base
);
1080 hyp_pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, pgd_order
);
1081 boot_hyp_pgd
= (pgd_t
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, pgd_order
);
1083 if (!hyp_pgd
|| !boot_hyp_pgd
) {
1084 kvm_err("Hyp mode PGD not allocated\n");
1089 /* Create the idmap in the boot page tables */
1090 err
= __create_hyp_mappings(boot_hyp_pgd
,
1091 hyp_idmap_start
, hyp_idmap_end
,
1092 __phys_to_pfn(hyp_idmap_start
),
1096 kvm_err("Failed to idmap %lx-%lx\n",
1097 hyp_idmap_start
, hyp_idmap_end
);
1101 /* Map the very same page at the trampoline VA */
1102 err
= __create_hyp_mappings(boot_hyp_pgd
,
1103 TRAMPOLINE_VA
, TRAMPOLINE_VA
+ PAGE_SIZE
,
1104 __phys_to_pfn(hyp_idmap_start
),
1107 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1112 /* Map the same page again into the runtime page tables */
1113 err
= __create_hyp_mappings(hyp_pgd
,
1114 TRAMPOLINE_VA
, TRAMPOLINE_VA
+ PAGE_SIZE
,
1115 __phys_to_pfn(hyp_idmap_start
),
1118 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1129 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
1130 struct kvm_userspace_memory_region
*mem
,
1131 const struct kvm_memory_slot
*old
,
1132 enum kvm_mr_change change
)
1134 gpa_t gpa
= old
->base_gfn
<< PAGE_SHIFT
;
1135 phys_addr_t size
= old
->npages
<< PAGE_SHIFT
;
1136 if (change
== KVM_MR_DELETE
|| change
== KVM_MR_MOVE
) {
1137 spin_lock(&kvm
->mmu_lock
);
1138 unmap_stage2_range(kvm
, gpa
, size
);
1139 spin_unlock(&kvm
->mmu_lock
);
1143 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
1144 struct kvm_memory_slot
*memslot
,
1145 struct kvm_userspace_memory_region
*mem
,
1146 enum kvm_mr_change change
)
1151 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
1152 struct kvm_memory_slot
*dont
)
1156 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
1157 unsigned long npages
)
1162 void kvm_arch_memslots_updated(struct kvm
*kvm
)
1166 void kvm_arch_flush_shadow_all(struct kvm
*kvm
)
1170 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
1171 struct kvm_memory_slot
*slot
)