From: Glauber Costa Subject: [PATCH 5/9] substitute is_allocated_mem with more general is_containing_region Date: Tue, 12 Aug 2008 21:48:05 -0300 Message-ID: <1218588489-17182-6-git-send-email-gcosta@redhat.com> References: <1218588489-17182-1-git-send-email-gcosta@redhat.com> <1218588489-17182-2-git-send-email-gcosta@redhat.com> <1218588489-17182-3-git-send-email-gcosta@redhat.com> <1218588489-17182-4-git-send-email-gcosta@redhat.com> <1218588489-17182-5-git-send-email-gcosta@redhat.com> Cc: avi@qumranet.com, aliguori@us.ibm.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([66.187.233.31]:40132 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753269AbYHMA57 (ORCPT ); Tue, 12 Aug 2008 20:57:59 -0400 In-Reply-To: <1218588489-17182-5-git-send-email-gcosta@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Signed-off-by: Glauber Costa --- libkvm/libkvm.c | 34 +++++++++++++++++++++------------- libkvm/libkvm.h | 2 +- qemu/qemu-kvm.c | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/libkvm/libkvm.c b/libkvm/libkvm.c index 33f00b7..c885dee 100644 --- a/libkvm/libkvm.c +++ b/libkvm/libkvm.c @@ -140,6 +140,27 @@ int get_intersecting_slot(unsigned long phys_addr) return -1; } +/* Returns -1 if this slot is not totally contained on any other, + * and the number of the slot otherwise */ +int get_container_slot(uint64_t phys_addr, unsigned long size) +{ + int i; + + for (i = 0; i < KVM_MAX_NUM_MEM_REGIONS ; ++i) + if (slots[i].len && slots[i].phys_addr <= phys_addr && + (slots[i].phys_addr + slots[i].len) >= phys_addr + size) + return i; + return -1; +} + +int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_addr, unsigned long size) +{ + int slot = get_container_slot(phys_addr, size); + if (slot == -1) + return 0; + return 1; +} + /* * dirty pages logging control */ @@ -491,19 +512,6 @@ int kvm_is_intersecting_mem(kvm_context_t kvm, unsigned long phys_start) return get_intersecting_slot(phys_start) != -1; } -int kvm_is_allocated_mem(kvm_context_t kvm, unsigned long phys_start, - unsigned long len) -{ - int slot; - - slot = get_slot(phys_start); - if (slot == -1) - return 0; - if (slots[slot].len == len) - return 1; - return 0; -} - int kvm_register_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start, void *userspace_addr, unsigned long len, int log) diff --git a/libkvm/libkvm.h b/libkvm/libkvm.h index 9f06fcc..d762323 100644 --- a/libkvm/libkvm.h +++ b/libkvm/libkvm.h @@ -454,7 +454,7 @@ void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start, unsigned long len, int log, int writable); void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start, unsigned long len); -int kvm_is_intersecting_mem(kvm_context_t kvm, unsigned long phys_start); +int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start, unsigned long size); int kvm_is_allocated_mem(kvm_context_t kvm, unsigned long phys_start, unsigned long len); int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start, diff --git a/qemu/qemu-kvm.c b/qemu/qemu-kvm.c index e6221f8..bfbaacc 100644 --- a/qemu/qemu-kvm.c +++ b/qemu/qemu-kvm.c @@ -783,7 +783,7 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r = kvm_check_extension(kvm_context, KVM_CAP_USER_MEMORY); if (r) { phys_offset &= ~IO_MEM_ROM; - r = kvm_is_allocated_mem(kvm_context, start_addr, size); + r = kvm_is_containing_region(kvm_context, start_addr, size); if (r) return; r = kvm_is_intersecting_mem(kvm_context, start_addr); -- 1.5.5.1