Blame SOURCES/kvm-dump-update-phys_base-header-field-based-on-VMCOREIN.patch

4a2fec
From a1b9c54811a4fc6797259dae98548229dba53d76 Mon Sep 17 00:00:00 2001
4a2fec
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
4a2fec
Date: Mon, 27 Nov 2017 22:51:07 +0100
4a2fec
Subject: [PATCH 09/21] dump: update phys_base header field based on VMCOREINFO
4a2fec
 content
4a2fec
MIME-Version: 1.0
4a2fec
Content-Type: text/plain; charset=UTF-8
4a2fec
Content-Transfer-Encoding: 8bit
4a2fec
4a2fec
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
4a2fec
Message-id: <20171127225111.24518-6-marcandre.lureau@redhat.com>
4a2fec
Patchwork-id: 77928
4a2fec
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 5/9] dump: update phys_base header field based on VMCOREINFO content
4a2fec
Bugzilla: 1398633
4a2fec
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
4a2fec
RH-Acked-by: Andrew Jones <drjones@redhat.com>
4a2fec
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
If the guest note is VMCOREINFO, try to get phys_base from it.
4a2fec
4a2fec
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
4a2fec
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
4a2fec
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
4a2fec
4a2fec
(cherry picked from commit d9feb51772b4ade9700c7fa54529327a6c8183a7)
4a2fec
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 docs/specs/vmcoreinfo.txt |  8 +++++++
4a2fec
 dump.c                    | 56 +++++++++++++++++++++++++++++++++++++++++++++--
4a2fec
 2 files changed, 62 insertions(+), 2 deletions(-)
4a2fec
4a2fec
diff --git a/docs/specs/vmcoreinfo.txt b/docs/specs/vmcoreinfo.txt
4a2fec
index 2868a77..8212610 100644
4a2fec
--- a/docs/specs/vmcoreinfo.txt
4a2fec
+++ b/docs/specs/vmcoreinfo.txt
4a2fec
@@ -39,3 +39,11 @@ qemu dumps.
4a2fec
 
4a2fec
 The note format/class must be of the target bitness and the size must
4a2fec
 be less than 1Mb.
4a2fec
+
4a2fec
+If the ELF note name is "VMCOREINFO", it is expected to be the Linux
4a2fec
+vmcoreinfo note (see Documentation/ABI/testing/sysfs-kernel-vmcoreinfo
4a2fec
+in Linux source). In this case, qemu dump code will read the content
4a2fec
+as a key=value text file, looking for "NUMBER(phys_base)" key
4a2fec
+value. The value is expected to be more accurate than architecture
4a2fec
+guess of the value. This is useful for KASLR-enabled guest with
4a2fec
+ancient tools not handling the VMCOREINFO note.
4a2fec
diff --git a/dump.c b/dump.c
4a2fec
index a4f175d..e3175d7 100644
4a2fec
--- a/dump.c
4a2fec
+++ b/dump.c
4a2fec
@@ -780,6 +780,23 @@ static void get_note_sizes(DumpState *s, const void *note,
4a2fec
     }
4a2fec
 }
4a2fec
 
4a2fec
+static bool note_name_equal(DumpState *s,
4a2fec
+                            const uint8_t *note, const char *name)
4a2fec
+{
4a2fec
+    int len = strlen(name) + 1;
4a2fec
+    uint64_t head_size, name_size;
4a2fec
+
4a2fec
+    get_note_sizes(s, note, &head_size, &name_size, NULL);
4a2fec
+    head_size = ROUND_UP(head_size, 4);
4a2fec
+
4a2fec
+    if (name_size != len ||
4a2fec
+        memcmp(note + head_size, "VMCOREINFO", len)) {
4a2fec
+        return false;
4a2fec
+    }
4a2fec
+
4a2fec
+    return true;
4a2fec
+}
4a2fec
+
4a2fec
 /* write common header, sub header and elf note to vmcore */
4a2fec
 static void create_header32(DumpState *s, Error **errp)
4a2fec
 {
4a2fec
@@ -1554,6 +1571,39 @@ static int64_t dump_calculate_size(DumpState *s)
4a2fec
     return total;
4a2fec
 }
4a2fec
 
4a2fec
+static void vmcoreinfo_update_phys_base(DumpState *s)
4a2fec
+{
4a2fec
+    uint64_t size, note_head_size, name_size, phys_base;
4a2fec
+    char **lines;
4a2fec
+    uint8_t *vmci;
4a2fec
+    size_t i;
4a2fec
+
4a2fec
+    if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
4a2fec
+        return;
4a2fec
+    }
4a2fec
+
4a2fec
+    get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
4a2fec
+    note_head_size = ROUND_UP(note_head_size, 4);
4a2fec
+
4a2fec
+    vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
4a2fec
+    *(vmci + size) = '\0';
4a2fec
+
4a2fec
+    lines = g_strsplit((char *)vmci, "\n", -1);
4a2fec
+    for (i = 0; lines[i]; i++) {
4a2fec
+        if (g_str_has_prefix(lines[i], "NUMBER(phys_base)=")) {
4a2fec
+            if (qemu_strtou64(lines[i] + 18, NULL, 16,
4a2fec
+                              &phys_base) < 0) {
4a2fec
+                warn_report("Failed to read NUMBER(phys_base)=");
4a2fec
+            } else {
4a2fec
+                s->dump_info.phys_base = phys_base;
4a2fec
+            }
4a2fec
+            break;
4a2fec
+        }
4a2fec
+    }
4a2fec
+
4a2fec
+    g_strfreev(lines);
4a2fec
+}
4a2fec
+
4a2fec
 static void dump_init(DumpState *s, int fd, bool has_format,
4a2fec
                       DumpGuestMemoryFormat format, bool paging, bool has_filter,
4a2fec
                       int64_t begin, int64_t length, Error **errp)
4a2fec
@@ -1637,8 +1687,9 @@ static void dump_init(DumpState *s, int fd, bool has_format,
4a2fec
     }
4a2fec
 
4a2fec
     /*
4a2fec
-     * The goal of this block is to copy the guest note out of
4a2fec
-     * the guest.  Failure to do so is not fatal for dumping.
4a2fec
+     * The goal of this block is to (a) update the previously guessed
4a2fec
+     * phys_base, (b) copy the guest note out of the guest.
4a2fec
+     * Failure to do so is not fatal for dumping.
4a2fec
      */
4a2fec
     if (vmci) {
4a2fec
         uint64_t addr, note_head_size, name_size, desc_size;
4a2fec
@@ -1671,6 +1722,7 @@ static void dump_init(DumpState *s, int fd, bool has_format,
4a2fec
                 g_free(s->guest_note);
4a2fec
                 s->guest_note = NULL;
4a2fec
             } else {
4a2fec
+                vmcoreinfo_update_phys_base(s);
4a2fec
                 s->note_size += s->guest_note_size;
4a2fec
             }
4a2fec
         }
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec