Running a guest with multiple banks of memory based above 0 causes the
guest_flat_to_host address conversion to fail, as it is assumed that
guest memory addresses are offset linearly from 0.

This patch changes the translation function so that the kvm_mem_bank
structures registered by kvm__register_mem are used to translate guest
addresses, rather than use an offset from the start of host memory.

Signed-off-by: Will Deacon <will.dea...@arm.com>
---
 tools/kvm/include/kvm/kvm.h          |  7 ++-----
 tools/kvm/kvm.c                      | 17 +++++++++++++++++
 tools/kvm/x86/include/kvm/kvm-arch.h |  9 ---------
 tools/kvm/x86/kvm.c                  |  7 +++++++
 4 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index 9b4a9a4..5fb2fb2 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -105,6 +105,8 @@ int kvm__arch_free_firmware(struct kvm *kvm);
 bool kvm__arch_cpu_supports_vm(void);
 void kvm__arch_periodic_poll(struct kvm *kvm);
 
+void *guest_flat_to_host(struct kvm *kvm, u64 offset);
+
 int load_flat_binary(struct kvm *kvm, int fd_kernel, int fd_initrd, const char 
*kernel_cmdline);
 bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd, const char 
*kernel_cmdline, u16 vidmode);
 
@@ -120,11 +122,6 @@ static inline bool host_ptr_in_ram(struct kvm *kvm, void 
*p)
        return kvm->ram_start <= p && p < (kvm->ram_start + kvm->ram_size);
 }
 
-static inline void *guest_flat_to_host(struct kvm *kvm, unsigned long offset)
-{
-       return kvm->ram_start + offset;
-}
-
 bool kvm__supports_extension(struct kvm *kvm, unsigned int extension);
 
 static inline void kvm__set_thread_name(const char *name)
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 1a10ec0..a7e2628 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -184,6 +184,23 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 
size, void *userspace
        return 0;
 }
 
+void *guest_flat_to_host(struct kvm *kvm, u64 offset)
+{
+       struct kvm_mem_bank *bank;
+
+       list_for_each_entry(bank, &kvm->mem_banks, list) {
+               u64 bank_start = bank->guest_phys_addr;
+               u64 bank_end = bank_start + bank->size;
+
+               if (offset >= bank_start && offset < bank_end)
+                       return bank->host_addr + (offset - bank_start);
+       }
+
+       pr_warning("unable to translate guest address 0x%llx to host",
+                       (unsigned long long)offset);
+       return NULL;
+}
+
 int kvm__recommended_cpus(struct kvm *kvm)
 {
        int ret;
diff --git a/tools/kvm/x86/include/kvm/kvm-arch.h 
b/tools/kvm/x86/include/kvm/kvm-arch.h
index 2aaedcc..1e0949e 100644
--- a/tools/kvm/x86/include/kvm/kvm-arch.h
+++ b/tools/kvm/x86/include/kvm/kvm-arch.h
@@ -33,13 +33,4 @@ struct kvm_arch {
        struct interrupt_table  interrupt_table;
 };
 
-static inline void *guest_flat_to_host(struct kvm *kvm, unsigned long offset); 
/* In kvm.h */
-
-static inline void *guest_real_to_host(struct kvm *kvm, u16 selector, u16 
offset)
-{
-       unsigned long flat = segment_to_flat(selector, offset);
-
-       return guest_flat_to_host(kvm, flat);
-}
-
 #endif /* KVM__KVM_ARCH_H */
diff --git a/tools/kvm/x86/kvm.c b/tools/kvm/x86/kvm.c
index ecada45..9971ffd 100644
--- a/tools/kvm/x86/kvm.c
+++ b/tools/kvm/x86/kvm.c
@@ -199,6 +199,13 @@ void kvm__irq_trigger(struct kvm *kvm, int irq)
 #define BOOT_PROTOCOL_REQUIRED 0x206
 #define LOAD_HIGH              0x01
 
+static inline void *guest_real_to_host(struct kvm *kvm, u16 selector, u16 
offset)
+{
+       unsigned long flat = segment_to_flat(selector, offset);
+
+       return guest_flat_to_host(kvm, flat);
+}
+
 int load_flat_binary(struct kvm *kvm, int fd_kernel, int fd_initrd, const char 
*kernel_cmdline)
 {
        void *p;
-- 
1.8.0

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to