yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
218e99
From f13968b5693d5316f4904cdc32b4327b3974fcc6 Mon Sep 17 00:00:00 2001
218e99
From: Laszlo Ersek <lersek@redhat.com>
218e99
Date: Mon, 12 Aug 2013 15:59:39 +0200
218e99
Subject: dump: populate guest_phys_blocks
218e99
218e99
RH-Author: Laszlo Ersek <lersek@redhat.com>
218e99
Message-id: <1376323180-12863-10-git-send-email-lersek@redhat.com>
218e99
Patchwork-id: 53167
218e99
O-Subject: [RHEL-7 qemu-kvm PATCH 09/10] dump: populate guest_phys_blocks
218e99
Bugzilla: 981582
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
RH-Acked-by: Radim Krcmar <rkrcmar@redhat.com>
218e99
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
218e99
While the machine is paused, in guest_phys_blocks_append() we register a
218e99
one-shot MemoryListener, solely for the initial collection of the valid
218e99
guest-physical memory ranges that happens at listener registration time.
218e99
218e99
For each range that is reported to guest_phys_blocks_region_add(), we
218e99
attempt to merge the range with the preceding one.
218e99
218e99
Ranges can only be joined if they are contiguous in both guest-physical
218e99
address space, and contiguous in host virtual address space.
218e99
218e99
The "maximal" ranges that remain in the end constitute the guest-physical
218e99
memory map that the dump will be based on.
218e99
218e99
Related RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=981582
218e99
218e99
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
218e99
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
218e99
(cherry picked from commit c5d7f60f0614250bd925071e25220ce5958f75d0)
218e99
218e99
Conflicts:
218e99
218e99
	memory_mapping.c
218e99
218e99
The conflicts are due to RHEL-7 not having:
218e99
- upstream commit 182735ef ("cpu: Make first_cpu and next_cpu CPUState"),
218e99
  whose backport I rejected due to its many dependencies,
218e99
- upstream commit 052e87b0 ("memory: make section size a 128-bit
218e99
  integer"), which seems quite intrusive, and to belong to the middle of a
218e99
  series.
218e99
218e99
diff --git a/dump.c b/dump.c
218e99
index 351233b..e6b7a00 100644
218e99
--- a/dump.c
218e99
+++ b/dump.c
218e99
@@ -750,7 +750,7 @@ static int dump_init(DumpState *s, int fd, bool paging, bool has_filter,
218e99
     s->length = length;
218e99
 
218e99
     guest_phys_blocks_init(&s->guest_phys_blocks);
218e99
-    /* FILL LIST */
218e99
+    guest_phys_blocks_append(&s->guest_phys_blocks);
218e99
 
218e99
     s->start = get_start_block(s);
218e99
     if (s->start == -1) {
218e99
diff --git a/include/sysemu/memory_mapping.h b/include/sysemu/memory_mapping.h
218e99
index d2d06cd..b2d7d85 100644
218e99
--- a/include/sysemu/memory_mapping.h
218e99
+++ b/include/sysemu/memory_mapping.h
218e99
@@ -66,6 +66,7 @@ void memory_mapping_list_init(MemoryMappingList *list);
218e99
 
218e99
 void guest_phys_blocks_free(GuestPhysBlockList *list);
218e99
 void guest_phys_blocks_init(GuestPhysBlockList *list);
218e99
+void guest_phys_blocks_append(GuestPhysBlockList *list);
218e99
 
218e99
 void qemu_get_guest_memory_mapping(MemoryMappingList *list, Error **errp);
218e99
 
218e99
diff --git a/memory_mapping.c b/memory_mapping.c
218e99
index 78a9829..411aba6 100644
218e99
--- a/memory_mapping.c
218e99
+++ b/memory_mapping.c
218e99
@@ -11,9 +11,15 @@
218e99
  *
218e99
  */
218e99
 
218e99
+#include <glib.h>
218e99
+
218e99
 #include "cpu.h"
218e99
 #include "exec/cpu-all.h"
218e99
 #include "sysemu/memory_mapping.h"
218e99
+#include "exec/memory.h"
218e99
+#include "exec/address-spaces.h"
218e99
+
218e99
+//#define DEBUG_GUEST_PHYS_REGION_ADD
218e99
 
218e99
 static void memory_mapping_list_add_mapping_sorted(MemoryMappingList *list,
218e99
                                                    MemoryMapping *mapping)
218e99
@@ -182,6 +188,84 @@ void guest_phys_blocks_init(GuestPhysBlockList *list)
218e99
     QTAILQ_INIT(&list->head);
218e99
 }
218e99
 
218e99
+typedef struct GuestPhysListener {
218e99
+    GuestPhysBlockList *list;
218e99
+    MemoryListener listener;
218e99
+} GuestPhysListener;
218e99
+
218e99
+static void guest_phys_blocks_region_add(MemoryListener *listener,
218e99
+                                         MemoryRegionSection *section)
218e99
+{
218e99
+    GuestPhysListener *g;
218e99
+    uint64_t section_size;
218e99
+    hwaddr target_start, target_end;
218e99
+    uint8_t *host_addr;
218e99
+    GuestPhysBlock *predecessor;
218e99
+
218e99
+    /* we only care about RAM */
218e99
+    if (!memory_region_is_ram(section->mr)) {
218e99
+        return;
218e99
+    }
218e99
+
218e99
+    g            = container_of(listener, GuestPhysListener, listener);
218e99
+    section_size = section->size;
218e99
+    target_start = section->offset_within_address_space;
218e99
+    target_end   = target_start + section_size;
218e99
+    host_addr    = memory_region_get_ram_ptr(section->mr) +
218e99
+                   section->offset_within_region;
218e99
+    predecessor  = NULL;
218e99
+
218e99
+    /* find continuity in guest physical address space */
218e99
+    if (!QTAILQ_EMPTY(&g->list->head)) {
218e99
+        hwaddr predecessor_size;
218e99
+
218e99
+        predecessor = QTAILQ_LAST(&g->list->head, GuestPhysBlockHead);
218e99
+        predecessor_size = predecessor->target_end - predecessor->target_start;
218e99
+
218e99
+        /* the memory API guarantees monotonically increasing traversal */
218e99
+        g_assert(predecessor->target_end <= target_start);
218e99
+
218e99
+        /* we want continuity in both guest-physical and host-virtual memory */
218e99
+        if (predecessor->target_end < target_start ||
218e99
+            predecessor->host_addr + predecessor_size != host_addr) {
218e99
+            predecessor = NULL;
218e99
+        }
218e99
+    }
218e99
+
218e99
+    if (predecessor == NULL) {
218e99
+        /* isolated mapping, allocate it and add it to the list */
218e99
+        GuestPhysBlock *block = g_malloc0(sizeof *block);
218e99
+
218e99
+        block->target_start = target_start;
218e99
+        block->target_end   = target_end;
218e99
+        block->host_addr    = host_addr;
218e99
+
218e99
+        QTAILQ_INSERT_TAIL(&g->list->head, block, next);
218e99
+        ++g->list->num;
218e99
+    } else {
218e99
+        /* expand predecessor until @target_end; predecessor's start doesn't
218e99
+         * change
218e99
+         */
218e99
+        predecessor->target_end = target_end;
218e99
+    }
218e99
+
218e99
+#ifdef DEBUG_GUEST_PHYS_REGION_ADD
218e99
+    fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end="
218e99
+            TARGET_FMT_plx ": %s (count: %u)\n", __FUNCTION__, target_start,
218e99
+            target_end, predecessor ? "joined" : "added", g->list->num);
218e99
+#endif
218e99
+}
218e99
+
218e99
+void guest_phys_blocks_append(GuestPhysBlockList *list)
218e99
+{
218e99
+    GuestPhysListener g = { 0 };
218e99
+
218e99
+    g.list = list;
218e99
+    g.listener.region_add = &guest_phys_blocks_region_add;
218e99
+    memory_listener_register(&g.listener, &address_space_memory);
218e99
+    memory_listener_unregister(&g.listener);
218e99
+}
218e99
+
218e99
 static CPUArchState *find_paging_enabled_cpu(CPUArchState *start_cpu)
218e99
 {
218e99
     CPUArchState *env;