Skip to content

Commit 957db10

Browse files
committed
arm/arm64: KVM: Introduce stage2_unmap_vm
Introduce a new function to unmap user RAM regions in the stage2 page tables. This is needed on reboot (or when the guest turns off the MMU) to ensure we fault in pages again and make the dcache, RAM, and icache coherent. Using unmap_stage2_range for the whole guest physical range does not work, because that unmaps IO regions (such as the GIC) which will not be recreated or in the best case faulted in on a page-by-page basis. Call this function on secondary and subsequent calls to the KVM_ARM_VCPU_INIT ioctl so that a reset VCPU will detect the guest Stage-1 MMU is off when faulting in pages and make the caches coherent. Acked-by: Marc Zyngier <[email protected]> Signed-off-by: Christoffer Dall <[email protected]>
1 parent cf5d318 commit 957db10

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

arch/arm/include/asm/kvm_mmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
5252
void free_boot_hyp_pgd(void);
5353
void free_hyp_pgds(void);
5454

55+
void stage2_unmap_vm(struct kvm *kvm);
5556
int kvm_alloc_stage2_pgd(struct kvm *kvm);
5657
void kvm_free_stage2_pgd(struct kvm *kvm);
5758
int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,

arch/arm/kvm/arm.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,13 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
701701
if (ret)
702702
return ret;
703703

704+
/*
705+
* Ensure a rebooted VM will fault in RAM pages and detect if the
706+
* guest MMU is turned off and flush the caches as needed.
707+
*/
708+
if (vcpu->arch.has_run_once)
709+
stage2_unmap_vm(vcpu->kvm);
710+
704711
vcpu_reset_hcr(vcpu);
705712

706713
/*

arch/arm/kvm/mmu.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,71 @@ static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
611611
unmap_range(kvm, kvm->arch.pgd, start, size);
612612
}
613613

614+
static void stage2_unmap_memslot(struct kvm *kvm,
615+
struct kvm_memory_slot *memslot)
616+
{
617+
hva_t hva = memslot->userspace_addr;
618+
phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
619+
phys_addr_t size = PAGE_SIZE * memslot->npages;
620+
hva_t reg_end = hva + size;
621+
622+
/*
623+
* A memory region could potentially cover multiple VMAs, and any holes
624+
* between them, so iterate over all of them to find out if we should
625+
* unmap any of them.
626+
*
627+
* +--------------------------------------------+
628+
* +---------------+----------------+ +----------------+
629+
* | : VMA 1 | VMA 2 | | VMA 3 : |
630+
* +---------------+----------------+ +----------------+
631+
* | memory region |
632+
* +--------------------------------------------+
633+
*/
634+
do {
635+
struct vm_area_struct *vma = find_vma(current->mm, hva);
636+
hva_t vm_start, vm_end;
637+
638+
if (!vma || vma->vm_start >= reg_end)
639+
break;
640+
641+
/*
642+
* Take the intersection of this VMA with the memory region
643+
*/
644+
vm_start = max(hva, vma->vm_start);
645+
vm_end = min(reg_end, vma->vm_end);
646+
647+
if (!(vma->vm_flags & VM_PFNMAP)) {
648+
gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
649+
unmap_stage2_range(kvm, gpa, vm_end - vm_start);
650+
}
651+
hva = vm_end;
652+
} while (hva < reg_end);
653+
}
654+
655+
/**
656+
* stage2_unmap_vm - Unmap Stage-2 RAM mappings
657+
* @kvm: The struct kvm pointer
658+
*
659+
* Go through the memregions and unmap any reguler RAM
660+
* backing memory already mapped to the VM.
661+
*/
662+
void stage2_unmap_vm(struct kvm *kvm)
663+
{
664+
struct kvm_memslots *slots;
665+
struct kvm_memory_slot *memslot;
666+
int idx;
667+
668+
idx = srcu_read_lock(&kvm->srcu);
669+
spin_lock(&kvm->mmu_lock);
670+
671+
slots = kvm_memslots(kvm);
672+
kvm_for_each_memslot(memslot, slots)
673+
stage2_unmap_memslot(kvm, memslot);
674+
675+
spin_unlock(&kvm->mmu_lock);
676+
srcu_read_unlock(&kvm->srcu, idx);
677+
}
678+
614679
/**
615680
* kvm_free_stage2_pgd - free all stage-2 tables
616681
* @kvm: The KVM struct pointer for the VM.

arch/arm64/include/asm/kvm_mmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
8383
void free_boot_hyp_pgd(void);
8484
void free_hyp_pgds(void);
8585

86+
void stage2_unmap_vm(struct kvm *kvm);
8687
int kvm_alloc_stage2_pgd(struct kvm *kvm);
8788
void kvm_free_stage2_pgd(struct kvm *kvm);
8889
int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,

0 commit comments

Comments
 (0)