From patchwork Thu Aug 8 15:13:28 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 265753 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 187AC2C00A1 for ; Fri, 9 Aug 2013 01:14:24 +1000 (EST) Received: from localhost ([::1]:35171 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7Ruw-0007Wy-3t for incoming@patchwork.ozlabs.org; Thu, 08 Aug 2013 11:14:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46127) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7RuH-0007Pe-KT for qemu-devel@nongnu.org; Thu, 08 Aug 2013 11:13:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V7RuB-0007sB-Jw for qemu-devel@nongnu.org; Thu, 08 Aug 2013 11:13:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35095) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7RuB-0007rc-C5 for qemu-devel@nongnu.org; Thu, 08 Aug 2013 11:13:35 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r78FDYNq020588 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 8 Aug 2013 11:13:34 -0400 Received: from localhost (ovpn-113-165.phx2.redhat.com [10.3.113.165]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r78FDYNW003639; Thu, 8 Aug 2013 11:13:34 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Thu, 8 Aug 2013 11:13:28 -0400 Message-Id: <1375974809-1757-4-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1375974809-1757-1-git-send-email-lcapitulino@redhat.com> References: <1375974809-1757-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: aliguori@us.ibm.com Subject: [Qemu-devel] [PULL 3/4] dump: populate guest_phys_blocks X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Laszlo Ersek While the machine is paused, in guest_phys_blocks_append() we register a one-shot MemoryListener, solely for the initial collection of the valid guest-physical memory ranges that happens at listener registration time. For each range that is reported to guest_phys_blocks_region_add(), we attempt to merge the range with the preceding one. Ranges can only be joined if they are contiguous in both guest-physical address space, and contiguous in host virtual address space. The "maximal" ranges that remain in the end constitute the guest-physical memory map that the dump will be based on. Related RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=981582 Signed-off-by: Laszlo Ersek Signed-off-by: Luiz Capitulino --- dump.c | 2 +- include/sysemu/memory_mapping.h | 1 + memory_mapping.c | 84 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/dump.c b/dump.c index 716fb1d..3fa33fc 100644 --- a/dump.c +++ b/dump.c @@ -746,7 +746,7 @@ static int dump_init(DumpState *s, int fd, bool paging, bool has_filter, s->length = length; guest_phys_blocks_init(&s->guest_phys_blocks); - /* FILL LIST */ + guest_phys_blocks_append(&s->guest_phys_blocks); s->start = get_start_block(s); if (s->start == -1) { diff --git a/include/sysemu/memory_mapping.h b/include/sysemu/memory_mapping.h index d2d06cd..b2d7d85 100644 --- a/include/sysemu/memory_mapping.h +++ b/include/sysemu/memory_mapping.h @@ -66,6 +66,7 @@ void memory_mapping_list_init(MemoryMappingList *list); void guest_phys_blocks_free(GuestPhysBlockList *list); void guest_phys_blocks_init(GuestPhysBlockList *list); +void guest_phys_blocks_append(GuestPhysBlockList *list); void qemu_get_guest_memory_mapping(MemoryMappingList *list, Error **errp); diff --git a/memory_mapping.c b/memory_mapping.c index c70505b..876f5aa 100644 --- a/memory_mapping.c +++ b/memory_mapping.c @@ -11,9 +11,15 @@ * */ +#include + #include "cpu.h" #include "exec/cpu-all.h" #include "sysemu/memory_mapping.h" +#include "exec/memory.h" +#include "exec/address-spaces.h" + +//#define DEBUG_GUEST_PHYS_REGION_ADD static void memory_mapping_list_add_mapping_sorted(MemoryMappingList *list, MemoryMapping *mapping) @@ -182,6 +188,84 @@ void guest_phys_blocks_init(GuestPhysBlockList *list) QTAILQ_INIT(&list->head); } +typedef struct GuestPhysListener { + GuestPhysBlockList *list; + MemoryListener listener; +} GuestPhysListener; + +static void guest_phys_blocks_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + GuestPhysListener *g; + uint64_t section_size; + hwaddr target_start, target_end; + uint8_t *host_addr; + GuestPhysBlock *predecessor; + + /* we only care about RAM */ + if (!memory_region_is_ram(section->mr)) { + return; + } + + g = container_of(listener, GuestPhysListener, listener); + section_size = int128_get64(section->size); + target_start = section->offset_within_address_space; + target_end = target_start + section_size; + host_addr = memory_region_get_ram_ptr(section->mr) + + section->offset_within_region; + predecessor = NULL; + + /* find continuity in guest physical address space */ + if (!QTAILQ_EMPTY(&g->list->head)) { + hwaddr predecessor_size; + + predecessor = QTAILQ_LAST(&g->list->head, GuestPhysBlockHead); + predecessor_size = predecessor->target_end - predecessor->target_start; + + /* the memory API guarantees monotonically increasing traversal */ + g_assert(predecessor->target_end <= target_start); + + /* we want continuity in both guest-physical and host-virtual memory */ + if (predecessor->target_end < target_start || + predecessor->host_addr + predecessor_size != host_addr) { + predecessor = NULL; + } + } + + if (predecessor == NULL) { + /* isolated mapping, allocate it and add it to the list */ + GuestPhysBlock *block = g_malloc0(sizeof *block); + + block->target_start = target_start; + block->target_end = target_end; + block->host_addr = host_addr; + + QTAILQ_INSERT_TAIL(&g->list->head, block, next); + ++g->list->num; + } else { + /* expand predecessor until @target_end; predecessor's start doesn't + * change + */ + predecessor->target_end = target_end; + } + +#ifdef DEBUG_GUEST_PHYS_REGION_ADD + fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end=" + TARGET_FMT_plx ": %s (count: %u)\n", __FUNCTION__, target_start, + target_end, predecessor ? "joined" : "added", g->list->num); +#endif +} + +void guest_phys_blocks_append(GuestPhysBlockList *list) +{ + GuestPhysListener g = { 0 }; + + g.list = list; + g.listener.region_add = &guest_phys_blocks_region_add; + memory_listener_register(&g.listener, &address_space_memory); + memory_listener_unregister(&g.listener); +} + static CPUState *find_paging_enabled_cpu(CPUState *start_cpu) { CPUState *cpu;