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

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