KVM: MIPS: Implement kvm_arch_flush_shadow_all/memslot
authorJames Hogan <james.hogan@imgtec.com>
Mon, 24 Oct 2016 23:01:37 +0000 (00:01 +0100)
committerJames Hogan <james.hogan@imgtec.com>
Fri, 3 Feb 2017 15:21:17 +0000 (15:21 +0000)
Implement the kvm_arch_flush_shadow_all() and
kvm_arch_flush_shadow_memslot() KVM functions for MIPS to allow guest
physical mappings to be safely changed.

The general MIPS KVM code takes care of flushing of GPA page table
entries. kvm_arch_flush_shadow_all() flushes the whole GPA page table,
and is always called on the cleanup path so there is no need to acquire
the kvm->mmu_lock. kvm_arch_flush_shadow_memslot() flushes only the
range of mappings in the GPA page table corresponding to the slot being
flushed, and happens when memory regions are moved or deleted.

MIPS KVM implementation callbacks are added for handling the
implementation specific flushing of mappings derived from the GPA page
tables. These are implemented for trap_emul.c using
kvm_flush_remote_tlbs() which should now be functional, and will flush
the per-VCPU GVA page tables and ASIDS synchronously (before next
entering guest mode or directly accessing GVA space).

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org
arch/mips/include/asm/kvm_host.h
arch/mips/kvm/mips.c
arch/mips/kvm/trap_emul.c

index 33d3d8ac742e49bca02a492c4d83517b7d38480f..ea1b495c042c9be0b1d153b3850d8c353af32951 100644 (file)
@@ -531,6 +531,14 @@ struct kvm_mips_callbacks {
        int (*vcpu_init)(struct kvm_vcpu *vcpu);
        void (*vcpu_uninit)(struct kvm_vcpu *vcpu);
        int (*vcpu_setup)(struct kvm_vcpu *vcpu);
+       void (*flush_shadow_all)(struct kvm *kvm);
+       /*
+        * Must take care of flushing any cached GPA PTEs (e.g. guest entries in
+        * VZ root TLB, or T&E GVA page tables and corresponding root TLB
+        * mappings).
+        */
+       void (*flush_shadow_memslot)(struct kvm *kvm,
+                                    const struct kvm_memory_slot *slot);
        gpa_t (*gva_to_gpa)(gva_t gva);
        void (*queue_timer_int)(struct kvm_vcpu *vcpu);
        void (*dequeue_timer_int)(struct kvm_vcpu *vcpu);
@@ -827,9 +835,6 @@ static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_free_memslot(struct kvm *kvm,
                struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {}
 static inline void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots) {}
-static inline void kvm_arch_flush_shadow_all(struct kvm *kvm) {}
-static inline void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
-               struct kvm_memory_slot *slot) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
index ff5e3429322794a67341691040185b90d614ba3a..01f3fa1b9f0ead69ed2cba29a1cdfd85ce234863 100644 (file)
@@ -157,6 +157,32 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
        return 0;
 }
 
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
+{
+       /* Flush whole GPA */
+       kvm_mips_flush_gpa_pt(kvm, 0, ~0);
+
+       /* Let implementation do the rest */
+       kvm_mips_callbacks->flush_shadow_all(kvm);
+}
+
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+                                  struct kvm_memory_slot *slot)
+{
+       /*
+        * The slot has been made invalid (ready for moving or deletion), so we
+        * need to ensure that it can no longer be accessed by any guest VCPUs.
+        */
+
+       spin_lock(&kvm->mmu_lock);
+       /* Flush slot from GPA */
+       kvm_mips_flush_gpa_pt(kvm, slot->base_gfn,
+                             slot->base_gfn + slot->npages - 1);
+       /* Let implementation do the rest */
+       kvm_mips_callbacks->flush_shadow_memslot(kvm, slot);
+       spin_unlock(&kvm->mmu_lock);
+}
+
 int kvm_arch_prepare_memory_region(struct kvm *kvm,
                                   struct kvm_memory_slot *memslot,
                                   const struct kvm_userspace_memory_region *mem,
index e20369d45f24843abb5d5dc8c28c645a7f2d54e5..1efe78d4bda891b7f0ecbc12e06d7796f31f9a2e 100644 (file)
@@ -586,6 +586,18 @@ static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
        return 0;
 }
 
+static void kvm_trap_emul_flush_shadow_all(struct kvm *kvm)
+{
+       /* Flush GVA page tables and invalidate GVA ASIDs on all VCPUs */
+       kvm_flush_remote_tlbs(kvm);
+}
+
+static void kvm_trap_emul_flush_shadow_memslot(struct kvm *kvm,
+                                       const struct kvm_memory_slot *slot)
+{
+       kvm_trap_emul_flush_shadow_all(kvm);
+}
+
 static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
 {
        return 0;
@@ -963,6 +975,8 @@ static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
        .vcpu_init = kvm_trap_emul_vcpu_init,
        .vcpu_uninit = kvm_trap_emul_vcpu_uninit,
        .vcpu_setup = kvm_trap_emul_vcpu_setup,
+       .flush_shadow_all = kvm_trap_emul_flush_shadow_all,
+       .flush_shadow_memslot = kvm_trap_emul_flush_shadow_memslot,
        .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
        .queue_timer_int = kvm_mips_queue_timer_int_cb,
        .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,