From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Kzgz0-0002EP-IC for qemu-devel@nongnu.org; Mon, 10 Nov 2008 19:19:50 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Kzgyz-0002Dd-RE for qemu-devel@nongnu.org; Mon, 10 Nov 2008 19:19:50 -0500 Received: from [199.232.76.173] (port=41146 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kzgyz-0002DV-FL for qemu-devel@nongnu.org; Mon, 10 Nov 2008 19:19:49 -0500 Received: from mx2.redhat.com ([66.187.237.31]:39760) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Kzgyz-00088Q-6X for qemu-devel@nongnu.org; Mon, 10 Nov 2008 19:19:49 -0500 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id mAB0Jm2j029317 for ; Mon, 10 Nov 2008 19:19:48 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id mAB0JlEF031717 for ; Mon, 10 Nov 2008 19:19:48 -0500 Received: from localhost.localdomain (vpn-12-7.rdu.redhat.com [10.11.12.7]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id mAB0JgV1023994 for ; Mon, 10 Nov 2008 19:19:47 -0500 From: Glauber Costa Date: Tue, 11 Nov 2008 00:16:08 -0200 Message-Id: <1226369769-5109-5-git-send-email-glommer@redhat.com> In-Reply-To: <1226369769-5109-4-git-send-email-glommer@redhat.com> References: <1226369769-5109-1-git-send-email-glommer@redhat.com> <1226369769-5109-2-git-send-email-glommer@redhat.com> <1226369769-5109-3-git-send-email-glommer@redhat.com> <1226369769-5109-4-git-send-email-glommer@redhat.com> Subject: [Qemu-devel] [PATCH 4/5] Introduce kvm logging interface. Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Introduce functions to stop and start logging of memory regions. We select region based on its start address. Signed-off-by: Glauber Costa --- kvm-all.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kvm.h | 5 ++ 2 files changed, 144 insertions(+), 0 deletions(-) diff --git a/kvm-all.c b/kvm-all.c index 6d50609..cb7bf6a 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -31,8 +31,13 @@ do { } while (0) #endif +#define warning(fmt, ...) \ + do { dprintf("%s:%d" fmt, __func__, __LINE__, ## __VA_ARGS__); } while (0) + typedef struct kvm_userspace_memory_region KVMSlot; +typedef struct kvm_dirty_log KVMDirtyLog; + int kvm_allowed = 0; struct KVMState @@ -109,6 +114,140 @@ err: return ret; } +/* + * dirty pages logging control + */ +static int kvm_dirty_pages_log_change(KVMSlot *mem, + unsigned flags, + unsigned mask) +{ + int r = -1; + KVMState *s = kvm_state; + + flags = (mem->flags & ~mask) | flags; + if (flags == mem->flags) + return 0; + + mem->flags = flags; + + r = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, mem); + if (r == -1) + fprintf(stderr, "%s: %m\n", __FUNCTION__); + + return r; +} + +int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len) +{ + KVMState *s = kvm_state; + KVMSlot *mem = kvm_lookup_slot(s, phys_addr); + + if (mem == NULL) { + fprintf(stderr, "BUG: %s: invalid parameters\n", __func__); + return -EINVAL; + } + + /* Already logging, no need to issue ioctl */ + if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) + return 0; + + dprintf("slot %d: enable logging (phys %llx, uaddr %llx)\n", + mem->slot, mem->guest_phys_addr, mem->userspace_addr); + + return kvm_dirty_pages_log_change(mem, + KVM_MEM_LOG_DIRTY_PAGES, + KVM_MEM_LOG_DIRTY_PAGES); +} + +int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len) +{ + + KVMState *s = kvm_state; + KVMSlot *mem = kvm_lookup_slot(s, phys_addr); + + if (mem == NULL) { + fprintf(stderr, "BUG: %s: invalid parameters\n", __func__); + return -EINVAL; + } + + /* Not logging, no need to issue ioctl */ + if (!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES)) + return 0; + + dprintf("slot %d: disabling logging\n", mem->slot); + return kvm_dirty_pages_log_change(mem, + 0, + KVM_MEM_LOG_DIRTY_PAGES); +} + +static inline int lookup_bitmap_phys(unsigned long *dirty, unsigned nr) +{ + unsigned word = nr / (sizeof(*dirty) * 8); + unsigned bit = nr % (sizeof(*dirty) * 8); + int ret; + + ret = (dirty[word] >> bit) & 1; + return ret; +} + +/** + * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space + * If a phys_offset parameter is given, this function updates qemu's dirty + * bitmap using cpu_physical_memory_set_dirty(). This means all bits are set + * to dirty. + * + * @start_add: start of logged region. This is what we use to search the memslot + * @end_addr: end of logged region. Only matters if we are updating qemu dirty bitmap. + * @phys_offset: the phys_offset we want to use for qemu dirty bitmap update. Passing + * NULL makes the update not happen. In this case, we only grab the bitmap + * from kernel. + */ +void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr, + ram_addr_t phys_offset) +{ + KVMState *s = kvm_state; + KVMDirtyLog d; + KVMSlot *mem = kvm_lookup_slot(s, start_addr); + unsigned long alloc_size = mem->memory_size >> TARGET_PAGE_BITS / sizeof(d.dirty_bitmap); + ram_addr_t addr; + target_phys_addr_t phys_addr; + + printf("sync addr: %llx into %lx\n", start_addr, phys_offset); + if (mem == NULL) { + fprintf(stderr, "BUG: %s: invalid parameters\n", __func__); + return; + } + + d.dirty_bitmap = qemu_mallocz(alloc_size); + + if (d.dirty_bitmap == NULL) { + warning("Could not allocate dirty bitmap\n"); + return; + } + + d.slot = mem->slot; + dprintf("slot %d, phys_addr %llx, uaddr: %llx\n", + d.slot, mem->guest_phys_addr, mem->userspace_addr); + + if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) { + warning("ioctl failed %d\n", errno); + goto out; + } + + /* Caller don't want to update dirty bitmap */ + if (!phys_offset) + goto out; + + phys_addr = start_addr; + for (addr = phys_offset; phys_addr < end_addr; phys_addr+= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { + unsigned long *bitmap = (unsigned long *)d.dirty_bitmap; + if (lookup_bitmap_phys(bitmap, (phys_addr - start_addr) >> TARGET_PAGE_BITS)) + cpu_physical_memory_set_dirty(addr); + } +out: + qemu_free(d.dirty_bitmap); +} + int kvm_init(int smp_cpus) { KVMState *s; diff --git a/kvm.h b/kvm.h index 37102b4..90503e8 100644 --- a/kvm.h +++ b/kvm.h @@ -38,6 +38,11 @@ void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset); +void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr, + ram_addr_t phys_offset); + +int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len); +int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len); /* internal API */ struct KVMState; -- 1.5.6.5